Skip to content

Commit

Permalink
Start fixing precision losses issues: fix and merge fileOffsetToRVA a…
Browse files Browse the repository at this point in the history
…nd RVAToFileOffset into common functions to 32 and 64 bit targets (operating and returning 64 bit addresses and offsets)
  • Loading branch information
gdbinit committed May 11, 2023
1 parent 4e06b76 commit d8b7a59
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 185 deletions.
2 changes: 1 addition & 1 deletion CRTFootPrints.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ typedef uint8_t AsmFootPrint[][FOOTPRINT_STRIDE];

@interface MachOLayout (CRTFootPrints)

- (bool) matchAsmAtOffset:(uint32_t)offset
- (bool) matchAsmAtOffset:(uint64_t)offset
asmFootPrint:(const AsmFootPrint)footprint
lineCount:(NSUInteger)lineCount;

Expand Down
93 changes: 34 additions & 59 deletions CRTFootPrints.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@
@implementation MachOLayout (CRTFootPrints)

//------------------------------------------------------------------------------
- (bool) matchAsmAtOffset:(uint32_t)offset
- (bool) matchAsmAtOffset:(uint64_t)offset
asmFootPrint:(const AsmFootPrint)footprint
lineCount:(NSUInteger)lineCount
{
Expand Down Expand Up @@ -1402,82 +1402,67 @@ - (bool) matchAsmAtOffset:(uint32_t)offset
//------------------------------------------------------------------------------
- (void) determineRuntimeVersion
{
if (!dataController) // dataController is weak
return;
if (!dataController) { // dataController is weak
return;
}

if (entryPoint == 0)
{
return; // not an executable, no entry point, or cannot detect
}

// find file offset of the entry point
uint32_t offset = [self is64bit] == NO
? [self RVAToFileOffset:entryPoint]
: [self RVA64ToFileOffset:entryPoint];

NSLog(@"%@: file offset of OEP: 0x%X", self, offset);
if (entryPoint == 0) {
return; // not an executable, no entry point, or cannot detect
}

// find file offset of the entry point
uint64_t offset = [self RVAToFileOffset:entryPoint];
NSLog(@"%@: file offset of OEP: 0x%llX", self, offset);

uint32_t dataLength = [dataController.fileData length];
uint64_t dataLength = [dataController.fileData length];

if (offset >= dataLength)
{
return;
}
if (offset >= dataLength) {
return;
}

// test against footprints
if ([self is64bit] == NO)
{
if (MATCHASM(SDK104Target104X86v1))
{
if ([self is64bit] == NO) {
if (MATCHASM(SDK104Target104X86v1)) {
NSLog(@"SDK104Target104X86v1 matched");
rootNode.caption = [rootNode.caption stringByAppendingString:@" [SDK10.4 Target10.4]"];
return;
}
else if (MATCHASM(SDK104Target104X86v2))
{
else if (MATCHASM(SDK104Target104X86v2)) {
NSLog(@"SDK104Target104X86v2 matched");
rootNode.caption = [rootNode.caption stringByAppendingString:@" [SDK10.4 Target10.4]"];
return;
}
else if (MATCHASM(SDK104Target104X86v3))
{
else if (MATCHASM(SDK104Target104X86v3)) {
NSLog(@"SDK104Target104X86v3 matched");
rootNode.caption = [rootNode.caption stringByAppendingString:@" [SDK10.4 Target10.4]"];
return;
}
else if (MATCHASM(SDK104Target104X86v4))
{
else if (MATCHASM(SDK104Target104X86v4)) {
NSLog(@"SDK104Target104X86v4 matched");
rootNode.caption = [rootNode.caption stringByAppendingString:@" [SDK10.4 Target10.4]"];
return;
}
else if (MATCHASM(SDK105Target104X86))
{
else if (MATCHASM(SDK105Target104X86)) {
NSLog(@"SDK105Target104X86 matched");
rootNode.caption = [rootNode.caption stringByAppendingString:@" [SDK10.5 Target10.4]"];
return;
}
else if (MATCHASM(SDK105Target105X86))
{
else if (MATCHASM(SDK105Target105X86)) {
NSLog(@"SDK105Target105X86 matched");
rootNode.caption = [rootNode.caption stringByAppendingString:@" [SDK10.5 Target10.5]"];
return;
}
else if (MATCHASM(SDK106Target104X86) || MATCHASM(SDK106Target104X86v2))
{
else if (MATCHASM(SDK106Target104X86) || MATCHASM(SDK106Target104X86v2)) {
NSLog(@"SDK106Target104X86 matched");
rootNode.caption = [rootNode.caption stringByAppendingString:@" [SDK10.6 Target10.4]"];
return;
}
else if (MATCHASM(SDK106Target105X86))
{
else if (MATCHASM(SDK106Target105X86)) {
NSLog(@"SDK106Target105X86 matched");

for (CommandVector::const_iterator cmdIter = commands.begin(); cmdIter != commands.end(); ++cmdIter)
{
for (CommandVector::const_iterator cmdIter = commands.begin(); cmdIter != commands.end(); ++cmdIter) {
struct load_command const * load_command = *cmdIter;
if (load_command->cmd == LC_DYLD_INFO_ONLY)
{
if (load_command->cmd == LC_DYLD_INFO_ONLY) {
rootNode.caption = [rootNode.caption stringByAppendingString:@" [SDK10.6 Target10.6]"];
NSLog(@"LC_DYLD_INFO_ONLY ==> target10.6");
return;
Expand All @@ -1486,43 +1471,35 @@ - (void) determineRuntimeVersion
rootNode.caption = [rootNode.caption stringByAppendingString:@" [SDK10.6 Target10.5]"];
return;
}
else if (MATCHASM (SDK109Target104X86))
{
else if (MATCHASM (SDK109Target104X86)) {
NSLog(@"SDK109Target104X86 matched");

rootNode.caption = [rootNode.caption stringByAppendingString:@" [SDK10.9 Target10.4]"];
return;
}
}
else
{
if (MATCHASM(SDK104Target104X86_64))
{
else {
if (MATCHASM(SDK104Target104X86_64)) {
NSLog(@"SDK104Target104X86_64 matched");
rootNode.caption = [rootNode.caption stringByAppendingString:@" [SDK10.4 Target10.4]"];
return;
}
else if (MATCHASM(SDK105Target104X86_64))
{
else if (MATCHASM(SDK105Target104X86_64)) {
NSLog(@"SDK105Target104X86_64 matched");
rootNode.caption = [rootNode.caption stringByAppendingString:@" [SDK10.5 Target10.4]"];
return;
}
else if (MATCHASM(SDK106Target104X86_64))
{
else if (MATCHASM(SDK106Target104X86_64)) {
NSLog(@"SDK106Target104X86_64 matched");
rootNode.caption = [rootNode.caption stringByAppendingString:@" [SDK10.6 Target10.4]"];
return;
}
else if (MATCHASM(SDK105Target105X86_64))
{
else if (MATCHASM(SDK105Target105X86_64)) {
NSLog(@"SDK105Target105X86_64 matched");

for (CommandVector::const_iterator cmdIter = commands.begin(); cmdIter != commands.end(); ++cmdIter)
{
for (CommandVector::const_iterator cmdIter = commands.begin(); cmdIter != commands.end(); ++cmdIter) {
struct load_command const * load_command = *cmdIter;
if (load_command->cmd == LC_DYLD_INFO_ONLY)
{
if (load_command->cmd == LC_DYLD_INFO_ONLY) {
NSLog(@"LC_DYLD_INFO_ONLY ==> target10.6");
rootNode.caption = [rootNode.caption stringByAppendingString:@" [SDK10.6]"];
return;
Expand All @@ -1531,9 +1508,7 @@ - (void) determineRuntimeVersion
rootNode.caption = [rootNode.caption stringByAppendingString:@" [SDK10.5]"];
return;
}

}

}
//------------------------------------------------------------------------------

Expand Down
6 changes: 2 additions & 4 deletions DyldInfo.mm
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ - (void)bindAddress:(uint64_t)address
}
else
{
relocLocation = [self RVA64ToFileOffset:address];
relocLocation = [self RVAToFileOffset:address];
relocValue = [symbolIndex longLongValue];
}

Expand Down Expand Up @@ -869,9 +869,7 @@ - (void)printSymbols:(NSString *)prefix
:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Next Node"
:[self is64bit] == NO
? [NSString stringWithFormat:@"0x%X",[self fileOffsetToRVA:location + skip]]
: [NSString stringWithFormat:@"0x%qX",[self fileOffsetToRVA64:location + skip]]];
:[NSString stringWithFormat:@"0x%qX",[self fileOffsetToRVA:location + skip]]];

if (childCount == 0)
{
Expand Down
15 changes: 6 additions & 9 deletions Exceptions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ - (NSString *)guessSymbol64UsingEncoding:(uint8_t)format atOffset:(uint32_t)offs

if (format & DW_EH_PE_pcrel)
{
value += [self fileOffsetToRVA64:offset];
value += [self fileOffsetToRVA:offset];
}

NSString * symbolName = [self findSymbolAtRVA64:value];
Expand Down Expand Up @@ -421,9 +421,7 @@ - (MVNode *)createCFINode:(MVNode *)parent
break;
}

uint64_t FDE_CIEpointer = ([self is64bit] == NO
? [self fileOffsetToRVA:range.location]
: [self fileOffsetToRVA64:range.location]) - FDE_CIEvalue;
uint64_t FDE_CIEpointer = [self fileOffsetToRVA:range.location] - FDE_CIEvalue;

[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
Expand Down Expand Up @@ -568,8 +566,7 @@ - (MVNode *)createLSDANode:(MVNode *)parent
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
:lastReadHex
:@"Type Table Base"
:[self is64bit] == NO ? [NSString stringWithFormat:@"0x%X",[self fileOffsetToRVA:typeTableBaseLocation]] :
[NSString stringWithFormat:@"0x%qX",[self fileOffsetToRVA64:typeTableBaseLocation]]];
:[NSString stringWithFormat:@"0x%qX",[self fileOffsetToRVA:typeTableBaseLocation]]];
}

uint8_t callSiteFormat = [dataController read_uint8:range lastReadHex:&lastReadHex];
Expand Down Expand Up @@ -761,7 +758,7 @@ - (MVNode *)createUnwindInfoHeaderNode:(MVNode *)parent
:@"Common Enc Array Sect Offset"
:[self is64bit] == NO
? [self findSymbolAtRVA:[self fileOffsetToRVA:range.location] + commonEncodingsArraySectionOffset]
: [self findSymbolAtRVA64:[self fileOffsetToRVA64:range.location] + commonEncodingsArraySectionOffset]];
: [self findSymbolAtRVA64:[self fileOffsetToRVA:range.location] + commonEncodingsArraySectionOffset]];

uint32_t commonEncodingsArrayCount = [dataController read_uint32:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
Expand All @@ -775,7 +772,7 @@ - (MVNode *)createUnwindInfoHeaderNode:(MVNode *)parent
:@"Personality Array Sect Offset"
:[self is64bit] == NO
? [self findSymbolAtRVA:[self fileOffsetToRVA:range.location] + personalityArraySectionOffset]
: [self findSymbolAtRVA64:[self fileOffsetToRVA64:range.location] + personalityArraySectionOffset]];
: [self findSymbolAtRVA64:[self fileOffsetToRVA:range.location] + personalityArraySectionOffset]];

uint32_t personalityArrayCount = [dataController read_uint32:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
Expand All @@ -789,7 +786,7 @@ - (MVNode *)createUnwindInfoHeaderNode:(MVNode *)parent
:@"Index Section Offset"
:[self is64bit] == NO
? [self findSymbolAtRVA:[self fileOffsetToRVA:range.location] + indexSectionOffset]
: [self findSymbolAtRVA64:[self fileOffsetToRVA64:range.location] + indexSectionOffset]];
: [self findSymbolAtRVA64:[self fileOffsetToRVA:range.location] + indexSectionOffset]];

uint32_t indexCount = [dataController read_uint32:range lastReadHex:&lastReadHex];
[node.details appendRow:[NSString stringWithFormat:@"%.8lX", range.location]
Expand Down
6 changes: 3 additions & 3 deletions Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<plist version="1.0">
<dict>
<key>CFBuildDate</key>
<string>Thu May 11 15:19:45 WEST 2023</string>
<string>Thu May 11 17:02:39 WEST 2023</string>
<key>CFBuildNumber</key>
<string>9223</string>
<string>9248</string>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleDocumentTypes</key>
Expand Down Expand Up @@ -44,7 +44,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>9223</string>
<string>9248</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.developer-tools</string>
<key>LSMinimumSystemVersion</key>
Expand Down
12 changes: 6 additions & 6 deletions LinkEdit.mm
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ - (MVNode *) createReloc64Node:(MVNode *)parent
//========================================================================
if (relocation_info->r_extern)
{
uint32_t relocLocation = [self RVA64ToFileOffset:baseAddress + relocation_info->r_address];
uint32_t relocLocation = [self RVAToFileOffset:baseAddress + relocation_info->r_address];
NSRange rangeReloc = NSMakeRange(relocLocation,0);

// target symbol
Expand All @@ -363,8 +363,8 @@ - (MVNode *) createReloc64Node:(MVNode *)parent
: symbolName];

[symbolNames setObject:[NSString stringWithFormat:@"%@->%@",
[self findSymbolAtRVA64:[self fileOffsetToRVA64:relocLocation]],symbolName]
forKey:[NSNumber numberWithUnsignedLongLong:[self fileOffsetToRVA64:relocLocation]]];
[self findSymbolAtRVA64:[self fileOffsetToRVA:relocLocation]],symbolName]
forKey:[NSNumber numberWithUnsignedLongLong:[self fileOffsetToRVA:relocLocation]]];

// For the x86_64 architecure on Mac OS X it is possible to
// encode a signed 32-bit expression of the form:
Expand Down Expand Up @@ -571,7 +571,7 @@ - (MVNode *) createReloc64Node:(MVNode *)parent
:@"Section"
:[NSString stringWithFormat:@"%u %@", relocation_info->r_symbolnum, sectionName]];

uint32_t relocLocation = [self RVA64ToFileOffset:baseAddress + relocation_info->r_address];
uint32_t relocLocation = [self RVAToFileOffset:baseAddress + relocation_info->r_address];
NSRange rangeReloc = NSMakeRange(relocLocation,0);
uint64_t relocValue = 0;

Expand Down Expand Up @@ -632,8 +632,8 @@ - (MVNode *) createReloc64Node:(MVNode *)parent
[self addRelocAtFileOffset:relocLocation withLength:relocLength andValue:relocValue];

[symbolNames setObject:[NSString stringWithFormat:@"%@->%@",
[self findSymbolAtRVA64:[self fileOffsetToRVA64:relocLocation]],symbolName]
forKey:[NSNumber numberWithUnsignedLongLong:[self fileOffsetToRVA64:relocLocation]]];
[self findSymbolAtRVA64:[self fileOffsetToRVA:relocLocation]],symbolName]
forKey:[NSNumber numberWithUnsignedLongLong:[self fileOffsetToRVA:relocLocation]]];

//NSLog(@"%@ %.16qX --> (%u) %@",[self findSectionContainsRVA64:[self fileOffsetToRVA64:relocLocation]],[self fileOffsetToRVA64:relocLocation],relocLength,[self findSymbolAtRVA64:relocValue]);
}
Expand Down
12 changes: 5 additions & 7 deletions MachOLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ typedef std::vector<struct dylib_module_64 const *> Module64Vector;
typedef std::vector<struct data_in_code_entry const *> DataInCodeEntryVector;
typedef std::vector<uint32_t const *> IndirectSymbolVector;

typedef std::map<uint32_t,std::pair<uint32_t,uint64_t> > RelocMap; // fileOffset --> <length,value>
typedef std::map<uint32_t,std::pair<uint64_t,uint64_t> > SegmentInfoMap; // fileOffset --> <address,size>
typedef std::map<uint64_t,std::pair<uint32_t,NSDictionary * __weak> > SectionInfoMap; // address --> <fileOffset,sectionUserInfo>
typedef std::map<uint32_t,std::pair<uint64_t,uint64_t> > RelocMap; // fileOffset --> <length,value>
typedef std::map<uint64_t,std::pair<uint64_t,uint64_t> > SegmentInfoMap; // fileOffset --> <address,size>
typedef std::map<uint64_t,std::pair<uint64_t,NSDictionary * __weak> > SectionInfoMap; // address --> <fileOffset,sectionUserInfo>
typedef std::map<uint64_t,uint64_t> ExceptionFrameMap; // LSDA_addr --> PCBegin_addr

@interface MachOLayout : MVLayout
Expand Down Expand Up @@ -81,11 +81,9 @@ typedef std::map<uint64_t,uint64_t> ExceptionFrameMa
- (NSString *)findSymbolAtRVA:(uint32_t)rva;
- (NSString *)findSymbolAtRVA64:(uint64_t)rva64;

- (uint32_t)fileOffsetToRVA:(uint32_t)offset;
- (uint64_t)fileOffsetToRVA64:(uint32_t)offset;
- (uint64_t)fileOffsetToRVA:(uint64_t)offset;

- (uint32_t)RVAToFileOffset:(uint32_t)rva;
- (uint32_t)RVA64ToFileOffset:(uint64_t)rva64;
- (uint64_t)RVAToFileOffset:(uint64_t)rva;

- (void)addRelocAtFileOffset:(uint32_t)offset withLength:(uint32_t)length andValue:(uint64_t)value;

Expand Down
Loading

0 comments on commit d8b7a59

Please sign in to comment.