Skip to content

Commit

Permalink
GP-3587: Libraries extracted from a DyldCacheFileSystem now contain an
Browse files Browse the repository at this point in the history
optimized __LINKEDIT segment, resulting in a significantly smaller
binary
  • Loading branch information
ryanmkurtz committed Jun 28, 2023
1 parent c3fef96 commit 763804e
Show file tree
Hide file tree
Showing 7 changed files with 342 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ public void markupRawBinary(MachHeader header, FlatProgramAPI api, Address baseA
try {
super.markupRawBinary(header, api, baseAddress, parentModule, monitor, log);

List<Address> addrs =
api.getCurrentProgram().getMemory().locateAddressesForFileOffset(getDataOffset());
List<Address> addrs = api.getCurrentProgram()
.getMemory()
.locateAddressesForFileOffset(getLinkerDataOffset());
if (addrs.size() <= 0) {
throw new Exception("Chain Header does not exist in program");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,16 @@ public List<RelocationInfo> getLocalRelocations() {
return localRelocations;
}

@Override
public int getLinkerDataOffset() {
return indirectsymoff;
}

@Override
public int getLinkerDataSize() {
return nindirectsyms * Integer.BYTES;
}

@Override
public DataType toDataType() throws DuplicateNameException, IOException {
StructureDataType struct = new StructureDataType(getCommandName(), 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ public class LinkEditDataCommand extends LoadCommand {
this.dataReader.setPointerIndex(dataoff);
}

public int getDataOffset() {
@Override
public int getLinkerDataOffset() {
return dataoff;
}

public int getDataSize() {
@Override
public int getLinkerDataSize() {
return datasize;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ public int getCommandSize() {
*/
public abstract String getCommandName();

/**
* Gets the file offset of this load command's "linker data". Not all load commands with data
* will have linker data. Linker data typically resides in the __LINKEDIT segment.
*
* @return The file offset of this load command's "linker data", or 0 if it has no linker data
*/
public int getLinkerDataOffset() {
return 0;
}

/**
* Gets the file size of this load command's "linker data". Not all load commands with data
* will have linker data. Linker data typically resides in the __LINKEDIT segment.
*
* @return The file size of this load command's "linker data", or 0 if it has no linker data
*/
public int getLinkerDataSize() {
return 0;
}

/**
* Gets the {@link Address} of this load command's "data"
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public long getVMsize() {
return vmsize;
}

public void setVMsize(long vmSize) {
vmsize = vmSize;
}

public long getFileOffset() {
return fileoff;
}
Expand All @@ -128,6 +132,10 @@ public long getFileSize() {
return filesize;
}

public void setFileSize(long fileSize) {
filesize = fileSize;
}

/**
* Returns a octal model value reflecting the
* segment's maximum protection value allowed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ public SymbolTableCommand(BinaryReader loadCommandReader, BinaryReader dataReade
List<NList> sortedList =
nlistList.stream().sorted((o1, o2) -> Integer.compare(o1.getStringTableIndex(),
o2.getStringTableIndex())).collect(Collectors.toList());

// initialize the sorted NList strings from string table
for (NList nList : sortedList) {
nList.initString(dataReader, stroff);
}

// the symbol table should be in the original order.
// The table is indexed by other tables in the MachO headers
symbols = nlistList;
Expand Down Expand Up @@ -153,6 +153,23 @@ public String getCommandName() {
return "symtab_command";
}

@Override
public int getLinkerDataOffset() {
return symoff;
}

@Override
public int getLinkerDataSize() {
if (!symbols.isEmpty()) {
int totalStringSize = 0;
for (NList nlist : symbols) {
totalStringSize += nlist.getString().length() + 1; // Add 1 for null terminator
}
return nsyms * symbols.get(0).getSize() + totalStringSize;
}
return 0;
}

@Override
public Address getDataAddress(MachHeader header, AddressSpace space) {
if (symoff != 0 && nsyms != 0) {
Expand Down
Loading

0 comments on commit 763804e

Please sign in to comment.