Skip to content

Ghidra is a software reverse engineering (SRE) framework

Merge remote-tracking branch 'origin/patch'

ghidra1193e7221fd17
diff --cc Ghidra/Processors/PowerPC/src/main/java/ghidra/app/util/bin/format/elf/relocation/PowerPC_ElfRelocationHandler.java
index 3680866b10,882d9328cb..e630d73484
--- a/Ghidra/Processors/PowerPC/src/main/java/ghidra/app/util/bin/format/elf/relocation/PowerPC_ElfRelocationHandler.java
+++ b/Ghidra/Processors/PowerPC/src/main/java/ghidra/app/util/bin/format/elf/relocation/PowerPC_ElfRelocationHandler.java
@@@ -75,12 -93,18 +75,14 @@@ public class PowerPC_ElfRelocationHandl
//
// // Relocation addend already includes original symbol value but needs to account
// // for any image base adjustment
-// symbolValue = (int) ppcRelocationContext.getImageBaseWordAdjustmentOffset();
-// }
-// else {
- Address symbolAddr = (elfRelocationContext.getSymbolAddress(sym));
- int symbolValue = (int) elfRelocationContext.getSymbolValue(sym);
+// symbolValue = elfRelocationContext.getImageBaseWordAdjustmentOffset();
// }
- String symbolName = elfRelocationContext.getSymbolName(symbolIndex);
+
+ long relocbase = elfRelocationContext.getImageBaseWordAdjustmentOffset();
+
+ int offset = (int) relocationAddress.getOffset();
int oldValue = memory.getInt(relocationAddress);
int newValue = 0;
-
int byteLength = 4; // most relocations affect 4-bytes (change if different)
switch (type) {
@@@ -99,54 -123,46 +101,46 @@@
applyComponentOffsetPointer(program, relocationAddress, addend);
}
break;
- case PowerPC_ElfRelocationConstants.R_PPC_ADDR24:
- newValue = (symbolValue + addend) >> 2;
- newValue = (oldValue & ~PowerPC_ElfRelocationConstants.PPC_LOW24) | (newValue << 2);
+ case R_PPC_ADDR24:
+ newValue = ((int) symbolValue + addend) >> 2;
+ newValue = (oldValue & ~PPC_LOW24) | (newValue << 2);
memory.setInt(relocationAddress, newValue);
break;
- case PowerPC_ElfRelocationConstants.R_PPC_ADDR16:
- case PowerPC_ElfRelocationConstants.R_PPC_UADDR16:
- newValue = symbolValue + addend;
+ case R_PPC_ADDR16:
+ case R_PPC_UADDR16:
- case R_PPC_ADDR16_LO:
+ newValue = (int) symbolValue + addend;
memory.setShort(relocationAddress, (short) newValue);
byteLength = 2;
break;
- case PowerPC_ElfRelocationConstants.R_PPC_ADDR16_LO:
- if (Long.compareUnsigned(symbolValue, relocbase) > 0 &&
- Long.compareUnsigned(symbolValue, relocbase + addend) <= 0) {
++ case R_PPC_ADDR16_LO:
++ if (Long.compareUnsigned(symbolValue, relocbase) > 0 &&
++ Long.compareUnsigned(symbolValue, relocbase + addend) <= 0) {
+ /**
+ * (freebsd) Addend values are sometimes relative to sections in rela,
+ * where in reality they are relative to relocbase. Detect this condition.
+ */
+ symbolValue = (int) relocbase;
+ }
- newValue = symbolValue + addend;
++ newValue = (int) (symbolValue + addend);
+ memory.setShort(relocationAddress, (short) newValue);
+ byteLength = 2;
+ break;
- case PowerPC_ElfRelocationConstants.R_PPC_ADDR16_HI:
- newValue = (symbolValue + addend) >> 16;
+ case R_PPC_ADDR16_HI:
+ newValue = ((int) symbolValue + addend) >> 16;
memory.setShort(relocationAddress, (short) newValue);
byteLength = 2;
break;
- /**
- *
- R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
- static inline void addr16_ha(unsigned char* view, Address value)
- { This::addr16_hi(view, value + 0x8000); }
-
- static inline void
- addr16_hi(unsigned char* view, Address value)
- { This::template rela<16,16>(view, 16, 0xffff, value + 0x8000, CHECK_NONE); }
-
- rela(unsigned char* view,
- unsigned int right_shift,
- typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
- Address value,
- Overflow_check overflow)
- {
- typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
- Valtype* wv = reinterpret_cast<Valtype*>(view);
- Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv); // original bytes
-
- Valtype reloc = value >> 16;
- val &= ~0xffff;
- reloc &= dst_mask;
- elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc); // write instr btes
- return overflowed<valsize>(value >> 16, overflow);
- }
-
-
- */
- case PowerPC_ElfRelocationConstants.R_PPC_ADDR16_HA:
- if (Long.compareUnsigned(symbolValue, relocbase) > 0 &&
- Long.compareUnsigned(symbolValue, relocbase + addend) <= 0) {
+ case R_PPC_ADDR16_HA:
- newValue = ((int) symbolValue + addend + 0x8000) >> 16;
++ if (Long.compareUnsigned(symbolValue, relocbase) > 0 &&
++ Long.compareUnsigned(symbolValue, relocbase + addend) <= 0) {
+ /**
+ * (freebsd) Addend values are sometimes relative to sections in rela,
+ * where in reality they are relative to relocbase. Detect this condition.
+ */
+ symbolValue = (int) relocbase;
+ }
- newValue = symbolValue + addend;
++ newValue = (int) (symbolValue + addend);
+ newValue = (newValue >> 16) + ((newValue & 0x8000) != 0 ? 1 : 0);
memory.setShort(relocationAddress, (short) newValue);
byteLength = 2;
break;
@@@ -264,4 -281,141 +258,4 @@@
return new RelocationResult(Status.APPLIED, byteLength);
}
- /**
- * <code>PowerPC_ElfRelocationContext</code> provides extended relocation context..
- */
- private static class PowerPC_ElfRelocationContext extends ElfRelocationContext {
-
- private Integer sdaBase;
- private Integer sda2Base;
-
- protected PowerPC_ElfRelocationContext(ElfRelocationHandler handler,
- ElfLoadHelper loadHelper, Map<ElfSymbol, Address> symbolMap) {
- super(handler, loadHelper, symbolMap);
- }
-
- /**
- * Get or establish _SDA_BASE_ value and apply as r13 context value to all memory blocks
- * with execute permission.
- * @return _SDA_BASE_ offset or null if unable to determine or establish
- */
- Integer getSDABase() {
- if (sdaBase != null) {
- if (sdaBase == -1) {
- return null;
- }
- return sdaBase;
- }
- sdaBase = getBaseOffset("_SDA_BASE_", ".sdata", ".sbss");
- if (sdaBase == -1) {
- getLog().appendMsg("ERROR: failed to establish _SDA_BASE_");
- return null;
- }
- setRegisterContext("r13", BigInteger.valueOf(sdaBase), b -> b.isExecute());
- return sdaBase;
- }
-
- /**
- * Get or establish _SDA2_BASE_ value and apply as r2 context value to all memory blocks
- * with execute permission.
- * @return _SDA2_BASE_ offset or null if unable to determine or establish
- */
- Integer getSDA2Base() {
- if (sda2Base != null) {
- if (sda2Base == -1) {
- return null;
- }
- return sda2Base;
- }
- sda2Base = getBaseOffset("_SDA2_BASE_", ".sdata2", ".sbss2");
- if (sda2Base == -1) {
- getLog().appendMsg("ERROR: failed to establish _SDA2_BASE_");
- return null;
- }
- setRegisterContext("r2", BigInteger.valueOf(sda2Base), b -> b.isExecute());
- return sda2Base;
- }
-
- /**
- * Apply register context to all memory blocks which satisfy blockPredicate check.
- * @param regName register name
- * @param value context value
- * @param blockPredicate determine which memory blocks get context applied
- */
- private void setRegisterContext(String regName, BigInteger value,
- Predicate<MemoryBlock> blockPredicate) {
- Register reg = program.getRegister(regName);
- for (MemoryBlock block : program.getMemory().getBlocks()) {
- if (block.isExecute()) {
- try {
- program.getProgramContext().setValue(reg, block.getStart(), block.getEnd(),
- value);
- }
- catch (ContextChangeException e) {
- throw new AssertException(e); // no instructions should exist yet
- }
- }
- }
- }
-
- /**
- * Establish base offset from symbol or range of specified memory blocks.
- * @param symbolName base symbol name
- * @param blockNames block names which may be used to establish base range
- * @return base offset or -1 on failure
- */
- private Integer getBaseOffset(String symbolName, String... blockNames) {
-
- MessageLog log = getLog();
-
- Symbol baseSymbol = SymbolUtilities.getLabelOrFunctionSymbol(program, symbolName,
- msg -> log.appendMsg(msg));
- if (baseSymbol != null) {
- int baseOffset = (int) baseSymbol.getAddress().getOffset();
- String absString = "";
- if (baseSymbol.isPinned()) {
- absString = "absolute ";
- }
- log.appendMsg(
- "Using " + absString + symbolName + " of 0x" + Integer.toHexString(baseOffset));
- return baseOffset;
- }
-
- Memory mem = program.getMemory();
- AddressSpace defaultSpace = program.getAddressFactory().getDefaultAddressSpace();
- AddressSet blockSet = new AddressSet();
- for (String blockName : blockNames) {
- MemoryBlock block = mem.getBlock(blockName);
- if (block != null) {
- if (!block.getStart().getAddressSpace().equals(defaultSpace)) {
- log.appendMsg("ERROR: " + blockName + " not in default space");
- return -1;
- }
- blockSet.add(block.getStart(), block.getEnd());
- }
- }
- if (blockSet.isEmpty()) {
- return -1;
- }
-
- Address baseAddr = blockSet.getMinAddress();
- long range = blockSet.getMaxAddress().subtract(baseAddr) + 1;
- if (range > Short.MAX_VALUE) {
- // use aligned midpoint of range
- baseAddr = baseAddr.add((range / 2) & ~0x0f);
- }
-
- try {
- program.getSymbolTable().createLabel(baseAddr, symbolName, SourceType.ANALYSIS);
- }
- catch (InvalidInputException e) {
- throw new AssertException(e);
- }
-
- int baseOffset = (int) baseAddr.getOffset();
- log.appendMsg("Defined " + symbolName + " of 0x" + Integer.toHexString(baseOffset));
- return baseOffset;
- }
-
- }
--}
++}