Skip to content

Commit

Permalink
Add nxo64 changes for 17.0.0
Browse files Browse the repository at this point in the history
Co-authored-by: SciresM <[email protected]>
  • Loading branch information
TSRBerry and SciresM committed Oct 13, 2023
1 parent 5ad3c12 commit 7554105
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 14 deletions.
55 changes: 46 additions & 9 deletions src/main/java/adubbz/nx/common/ElfCompatibilityProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@
*/
package adubbz.nx.common;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import adubbz.nx.util.FullMemoryByteProvider;
import adubbz.nx.util.LegacyBinaryReader;
import ghidra.app.util.bin.BinaryReader;
Expand All @@ -28,8 +20,13 @@
import ghidra.util.Msg;
import ghidra.util.exception.NotFoundException;

import java.io.IOException;
import java.util.*;

public class ElfCompatibilityProvider
{
public static final int R_FAKE_RELR = -1;

private Program program;
private ByteProvider provider;
private BinaryReader binaryReader;
Expand Down Expand Up @@ -221,7 +218,7 @@ public List<NXRelocation> getRelocations()

try
{
if (dynamicTable.containsDynamicValue(ElfDynamicType.DT_REL.value))
if (dynamicTable.containsDynamicValue(ElfDynamicType.DT_REL))
{
Msg.info(this, "Processing DT_REL relocations...");
processRelocations(this.relocs, this.symbolTable,
Expand All @@ -236,6 +233,13 @@ public List<NXRelocation> getRelocations()
this.dynamicTable.getDynamicValue(ElfDynamicType.DT_RELA),
this.dynamicTable.getDynamicValue(ElfDynamicType.DT_RELASZ));
}

if (dynamicTable.containsDynamicValue(ElfDynamicType.DT_RELR)) {
Msg.info(this, "Processing DT_RELR relocations...");
processReadOnlyRelocations(this.relocs,
this.dynamicTable.getDynamicValue(ElfDynamicType.DT_RELR),
this.dynamicTable.getDynamicValue(ElfDynamicType.DT_RELRSZ));
}
}
catch (NotFoundException | IOException e)
{
Expand Down Expand Up @@ -297,6 +301,39 @@ private Set<Long> processRelocations(List<NXRelocation> relocs, ElfSymbolTable s
}
return locations;
}

private Set<Long> processReadOnlyRelocations(List<NXRelocation> relocs, long relr, long relrsz) throws IOException
{
Set<Long> locations = new HashSet<>();
int relocSize = 0x8;

long where = 0;
for (long i = 0; i < relrsz / relocSize; i++)
{
long base = this.program.getImageBase().getOffset();
long entry = this.binaryReader.readLong(base + relr + i * relocSize);

if ((entry & 1) != 0) {
entry >>= 1;
i = 0;
while (i < (relocSize * 8) - 1) {
if ((entry & (1L << i)) != 0) {
locations.add(where + i * relocSize);
relocs.add(new NXRelocation(where + i * relocSize, 0, R_FAKE_RELR, null, 0));
}
i++;
}
where += relocSize * ((relocSize * 8) - 1);
}
else {
where = entry;
locations.add(where);
relocs.add(new NXRelocation(where, 0, R_FAKE_RELR, null, 0));
where += 1;
}
}
return locations;
}

protected MemoryBlock getDynamicBlock()
{
Expand Down
28 changes: 23 additions & 5 deletions src/main/java/adubbz/nx/loader/common/NXProgramBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import java.util.List;
import java.util.Map;

import static adubbz.nx.common.ElfCompatibilityProvider.R_FAKE_RELR;

public class NXProgramBuilder
{
protected ByteProvider fileByteProvider;
Expand Down Expand Up @@ -95,6 +97,7 @@ public void load(TaskMonitor monitor)
this.tryCreateDynBlock(".fini_array", ElfDynamicType.DT_FINI_ARRAY, ElfDynamicType.DT_FINI_ARRAYSZ);
this.tryCreateDynBlock(".rela.dyn", ElfDynamicType.DT_RELA, ElfDynamicType.DT_RELASZ);
this.tryCreateDynBlock(".rel.dyn", ElfDynamicType.DT_REL, ElfDynamicType.DT_RELSZ);
this.tryCreateDynBlock(".relr.dyn", ElfDynamicType.DT_RELR, ElfDynamicType.DT_RELRSZ);

if (adapter.isAarch32())
{
Expand Down Expand Up @@ -266,10 +269,16 @@ protected void setupRelocations() throws AddressOutOfBoundsException, NotFoundEx
this.pltEntries.add(new PltEntry(off, target));
}
}

long pltStart = this.pltEntries.get(0).off;
long pltEnd = this.pltEntries.get(this.pltEntries.size() - 1).off + 0x10;
this.memBlockHelper.addSection(".plt", pltStart, pltStart, pltEnd - pltStart, true, false, false);

if (!this.pltEntries.isEmpty()) {
long pltStart = this.pltEntries.get(0).off;
long pltEnd = this.pltEntries.get(this.pltEntries.size() - 1).off + 0x10;
this.memBlockHelper.addSection(".plt", pltStart, pltStart, pltEnd - pltStart, true, false, false);
}
else {
// TODO: Find a way to locate the plt in CFI-enabled binaries.
Msg.error(this, "No PLT entries found, does this binary have CFI enabled? This loader currently can't locate the plt in them.");
}
}

protected void createGlobalOffsetTable() throws AddressOutOfBoundsException
Expand Down Expand Up @@ -337,7 +346,16 @@ else if (reloc.r_type == AARCH64_ElfRelocationConstants.R_AARCH64_GLOB_DAT ||
else if (reloc.r_type == AARCH64_ElfRelocationConstants.R_AARCH64_RELATIVE)
{
program.getMemory().setLong(target, this.nxo.getBaseAddress() + reloc.addend);
}
}
else if (reloc.r_type == R_FAKE_RELR) {
if (this.nxo.getAdapter().isAarch32()) {
// TODO: Add RELRO support for 32-bit
Msg.error(this, "TODO: RELRO support for 32-bit");
continue;
}

program.getMemory().setLong(target, this.nxo.getBaseAddress() + originalValue);
}
else
{
Msg.info(this, String.format("TODO: r_type 0x%x", reloc.r_type));
Expand Down

0 comments on commit 7554105

Please sign in to comment.