Ghidra is a software reverse engineering (SRE) framework
GP-7024 updated hexagon cspec and added some relocations
James5527824e15e0
diff --git a/Ghidra/Processors/Hexagon/data/languages/hexagon.cspec b/Ghidra/Processors/Hexagon/data/languages/hexagon.cspecindex 91e10ecfef..9677e13f07 100755--- a/Ghidra/Processors/Hexagon/data/languages/hexagon.cspec+++ b/Ghidra/Processors/Hexagon/data/languages/hexagon.cspec@@ -70,30 +70,34 @@<pentry minsize="1" maxsize="4"><register name="R1"/></pentry>- <pentry minsize="5" maxsize="8">- <register name="R1R0"/>- </pentry><pentry minsize="1" maxsize="4"><register name="R2"/></pentry><pentry minsize="1" maxsize="4"><register name="R3"/></pentry>- <pentry minsize="5" maxsize="8">- <register name="R3R2"/>- </pentry><pentry minsize="1" maxsize="4"><register name="R4"/></pentry><pentry minsize="1" maxsize="4"><register name="R5"/></pentry>- <pentry minsize="5" maxsize="8">- <register name="R5R4"/>- </pentry><pentry minsize="1" maxsize="500" align="1"> <!-- unable to stipulate alignment properly - see ABI spec --><addr offset="0" space="stack"/></pentry>+ <rule>+ <datatype name="any"/>+ <varargs first="0"/>+ <goto_stack/>+ </rule>+ <rule>+ <datatype name="any" minsize="9"/>+ <goto_stack/>+ </rule>+ <rule>+ <datatype name="any" maxsize="8"/>+ <join align="true" stackspill="false"/>+ </rule></input><output><pentry minsize="1" maxsize="8">@@ -151,4 +155,4 @@</body></pcode></callfixup>-</compiler_spec>\ No newline at end of file+</compiler_spec>diff --git a/Ghidra/Processors/Hexagon/src/main/java/ghidra/app/util/bin/format/elf/relocation/Hexagon_ElfRelocationHandler.java b/Ghidra/Processors/Hexagon/src/main/java/ghidra/app/util/bin/format/elf/relocation/Hexagon_ElfRelocationHandler.javaindex 0b8cc1e736..6da16e9e51 100644--- a/Ghidra/Processors/Hexagon/src/main/java/ghidra/app/util/bin/format/elf/relocation/Hexagon_ElfRelocationHandler.java+++ b/Ghidra/Processors/Hexagon/src/main/java/ghidra/app/util/bin/format/elf/relocation/Hexagon_ElfRelocationHandler.java@@ -56,7 +56,7 @@ public class Hexagon_ElfRelocationHandlerlong addend = relocation.getAddend();long offset = (int) relocationAddress.getOffset();-+int symbolIndex = relocation.getSymbolIndex();int byteLength = 4; // applied relocation length@@ -69,20 +69,20 @@ public class Hexagon_ElfRelocationHandlerint value = (int) (addend + imageBaseAdjustment);memory.setInt(relocationAddress, value);return new RelocationResult(Status.APPLIED, byteLength);-+case R_HEXAGON_COPY:markAsUnsupportedCopy(program, relocationAddress, type, symbolName, symbolIndex,elfSymbol.getSize(), elfRelocationContext.getLog());return RelocationResult.UNSUPPORTED;-+default:break;}-+// Check for unresolved symbolAddr and symbolValue required by remaining relocation types handled belowif (handleUnresolvedSymbol(elfRelocationContext, relocation, relocationAddress)) {return RelocationResult.FAILURE;- }+ }int value = (int) (symbolValue + addend);int memValue = memory.getInt(relocationAddress);@@ -94,9 +94,10 @@ public class Hexagon_ElfRelocationHandlerif ((dist < -0x00800000) || (dist >= 0x00800000)) {return RelocationResult.FAILURE;}- memValue &= ~0x01ff3fff;- memValue |= 0x00003fff & dist;- memValue |= 0x01ff0000 & (dist << 2);+ memValue &= ~0x01ff3ffe;+ dist = dist >>> 2;+ memValue |= 0x00003ffe & (dist << 1);+ memValue |= 0x01ff0000 & (dist << 3);memory.setInt(relocationAddress, memValue);break;@@ -149,8 +150,17 @@ public class Hexagon_ElfRelocationHandler// break;// case R_HEXAGON_B9_PCREL:// break;-// case R_HEXAGON_B32_PCREL_X:-// break;+ case R_HEXAGON_B32_PCREL_X:+ // (S + A - P) >> 6+ dist = (int) (Integer.toUnsignedLong(value) - Integer.toUnsignedLong((int) offset));+ dist = dist >>> 6;+ memValue &= ~0x0fff3fff;+ memValue |= dist & 0x3fff;+ dist = dist >>> 14;+ memValue |= (dist << 20);+ memory.setInt(relocationAddress, memValue);+ byteLength = 4;+ break;// case R_HEXAGON_32_6_X:// break;// case R_HEXAGON_B22_PCREL_X:@@ -207,7 +217,18 @@ public class Hexagon_ElfRelocationHandler// break;// case R_HEXAGON_GOT_16:// break;-+ case R_HEXAGON_6_PCREL_X:+ int opcode = memValue >> 16;+ // bitmap depends on instruction opcode+ if (opcode == 0x6a49) { // add Rd5,PacketPC,Uimm32_0712x+ dist = (int) (Integer.toUnsignedLong(value) -+ Integer.toUnsignedLong((int) offset));+ dist = dist & 0x3f;+ memValue &= ~(0x3f << 7);+ memValue |= (dist << 7);+ memory.setInt(relocationAddress, memValue);+ break;+ } // otherwise fall throughdefault:markAsUnhandled(program, relocationAddress, type, symbolIndex, symbolName, log);return RelocationResult.UNSUPPORTED;diff --git a/Ghidra/Processors/Hexagon/src/test.processors/java/ghidra/test/processors/cspec/Hexagon_CSpecTest.java b/Ghidra/Processors/Hexagon/src/test.processors/java/ghidra/test/processors/cspec/Hexagon_CSpecTest.javanew file mode 100644index 0000000000..edff48854b--- /dev/null+++ b/Ghidra/Processors/Hexagon/src/test.processors/java/ghidra/test/processors/cspec/Hexagon_CSpecTest.java@@ -0,0 +1,66 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.test.processors.cspec;++import ghidra.test.compilers.support.CSpecPrototypeTest;++public class Hexagon_CSpecTest extends CSpecPrototypeTest {++ private static final String LANGUAGE_ID = "Hexagon:LE:32:default";+ private static final String COMPILER_SPEC_ID = "default";++ private static final String CALLING_CONVENTION = "__stdcall";++ private static final String[] EXPECTED_PROTOTYPE_ERRORS = {+ "paramsPrimitiveIdentical_0",+ "paramsPrimitiveIdentical_1",+ "paramsPrimitiveAlternate_8",+ "paramsPrimitiveAlternate_14",+ "paramsPrimitiveAlternate_16",+ "paramsSingletonStruct_18",+ "paramsSingletonStruct_19",+ "paramsPairStruct_25",+ "paramsPairStruct_26",+ "paramsTripStruct_32",+ "paramsTripStruct_33",+ "paramsQuadStruct_39",+ "paramsVariadic_I_I_C_C_S_S_I_I_L_L_50",+ "paramsMisc_54",+ "paramsUnion_56",+ "paramsUnion_57",+ "paramsUnion_63",+ "paramsUnion_64"+ };++ public Hexagon_CSpecTest() throws Exception {+ super(EXPECTED_PROTOTYPE_ERRORS);+ }++ @Override+ public String getLanguageID() {+ return LANGUAGE_ID;+ }++ @Override+ public String getCompilerSpecID() {+ return COMPILER_SPEC_ID;+ }++ @Override+ public String getCallingConvention() {+ return CALLING_CONVENTION;+ }+}