Ghidra is a software reverse engineering (SRE) framework
GT-2909 AVR8 ELF import and pcode test improvements
ghidra181f577655595
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/extend/ElfLoadAdapter.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/extend/ElfLoadAdapter.javaindex 515b200e73..1eaa428bca 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/extend/ElfLoadAdapter.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/extend/ElfLoadAdapter.java@@ -251,6 +251,22 @@ public class ElfLoadAdapter {return null;}+ /**+ * Perform any required offset adjustment to account for differences between offset+ * values contained within ELF headers and the language modeling of the+ * associated address space.+ * <br>+ * WARNING: This is an experimental method and is not yet fully supported.+ * <br>+ * NOTE: This has currently been utilized for symbol address offset adjustment only.+ * @param elfOffset memory offset from ELF header+ * @param space associated address space+ * @return offset appropriate for use in space (does not account for image base alterations)+ */+ public long getAdjustedMemoryOffset(long elfOffset, AddressSpace space) {+ return elfOffset;+ }+/*** Perform extension specific processing of Elf image during program load.* The following loading steps will have already been completed:diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfProgramBuilder.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfProgramBuilder.javaindex 79c4024c68..766dec87f9 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfProgramBuilder.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfProgramBuilder.java@@ -1271,6 +1271,8 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {return null;}+ ElfLoadAdapter loadAdapter = elf.getLoadAdapter();+ElfSectionHeader[] elfSections = elf.getSections();short sectionIndex = elfSymbol.getSectionHeaderIndex();Address symSectionBase = null;@@ -1292,11 +1294,13 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {}symbolSpace = symSectionBase.getAddressSpace();} // else assume sections have been stripped- if (symbolSpace.getPhysicalSpace() == defaultSpace) {+ AddressSpace space = symbolSpace.getPhysicalSpace();+ symOffset = loadAdapter.getAdjustedMemoryOffset(symOffset, space);+ if (space == defaultSpace) {symOffset =elf.adjustAddressForPrelink(symOffset) + getImageBaseWordAdjustmentOffset();}- else if (symbolSpace.getPhysicalSpace() == defaultDataSpace) {+ else if (space == defaultDataSpace) {symOffset += getImageDataBase();}}@@ -1304,6 +1308,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {// FIXME: No sections defined or refers to external symbol// Uncertain what if any offset adjustments should apply, although the// EXTERNAL block is affected by the program image base+ symOffset = loadAdapter.getAdjustedMemoryOffset(symOffset, defaultSpace);symOffset += getImageBaseWordAdjustmentOffset();}else if (sectionIndex == ElfSectionHeaderConstants.SHN_ABS) { // Absolute value/address - 0xfff1@@ -1311,6 +1316,10 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {// The should potentially be assign a constant address instead (not possible currently)// Note: Assume data space - symbols will be "pinned"++ // TODO: it may be inappropriate to adjust since value may not actually be a memory address - what to do?+ // symOffset = loadAdapter.adjustMemoryOffset(symOffset, space);+symbolSpace = getDefaultDataSpace();}else if (sectionIndex == ElfSectionHeaderConstants.SHN_COMMON) { // Common symbols - 0xfff2 (diff --git a/Ghidra/Features/Base/src/main/java/ghidra/test/processors/support/EmulatorTestRunner.java b/Ghidra/Features/Base/src/main/java/ghidra/test/processors/support/EmulatorTestRunner.javaindex ff266b05e8..152e735b0c 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/test/processors/support/EmulatorTestRunner.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/test/processors/support/EmulatorTestRunner.java@@ -266,6 +266,11 @@ public class EmulatorTestRunner {Address breakOnErrorAddr =alignAddress(testGroup.mainTestControlBlock.getBreakOnErrorAddress(), alignment);+ executionListener.log(testGroup, "TestInfo pointers of interest:");+ executionListener.log(testGroup, " onDone -> " + breakOnDoneAddr);+ executionListener.log(testGroup, " onPass -> " + breakOnPassAddr);+ executionListener.log(testGroup, " onError -> " + breakOnErrorAddr);+emuHelper.setBreakpoint(breakOnDoneAddr);emuHelper.setBreakpoint(breakOnPassAddr);emuHelper.setBreakpoint(breakOnErrorAddr);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/test/processors/support/PCodeTestAbstractControlBlock.java b/Ghidra/Features/Base/src/main/java/ghidra/test/processors/support/PCodeTestAbstractControlBlock.javaindex 9cc01ffe14..a0c371d894 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/test/processors/support/PCodeTestAbstractControlBlock.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/test/processors/support/PCodeTestAbstractControlBlock.java@@ -21,11 +21,12 @@ import ghidra.app.emulator.EmulatorHelper;import ghidra.docking.settings.SettingsImpl;import ghidra.pcode.memstate.MemoryState;import ghidra.pcode.utils.Utils;+import ghidra.program.disassemble.Disassembler;import ghidra.program.model.address.*;import ghidra.program.model.data.*;import ghidra.program.model.data.DataUtilities.ClearDataMode;-import ghidra.program.model.listing.Data;-import ghidra.program.model.listing.Program;+import ghidra.program.model.lang.InstructionBlock;+import ghidra.program.model.listing.*;import ghidra.program.model.mem.*;import ghidra.program.model.symbol.*;import ghidra.program.model.util.CodeUnitInsertionException;@@ -42,6 +43,8 @@ public abstract class PCodeTestAbstractControlBlock {static final int SIZEOF_U4 = 4;+ private final Disassembler disassembler;+protected final Program program;protected final AddressSpace codeSpace;protected final AddressSpace dataSpace;@@ -69,6 +72,9 @@ public abstract class PCodeTestAbstractControlBlock {codeSpace = program.getAddressFactory().getDefaultAddressSpace();dataSpace = program.getLanguage().getDefaultDataSpace();++ disassembler = Disassembler.getDisassembler(program, TaskMonitor.DUMMY, m -> {+ /* ignore */ });}public Address getInfoStructureAddress() {@@ -175,7 +181,7 @@ public abstract class PCodeTestAbstractControlBlock {return (Address) data.getValue();}- protected Address readCodePointer(MemBuffer buffer, int bufferOffset, boolean updateReference) {+ protected Address readCodePointer(MemBuffer buffer, int bufferOffset, boolean updateReference) throws MemoryAccessException {Address codePtr = readPointer(buffer, bufferOffset, codeSpace, updateReference);// shift the pointer if code pointers are stored in memory shifted.@@ -191,8 +197,26 @@ public abstract class PCodeTestAbstractControlBlock {Address ptr = readDefinedDataPointer(codePtr);if (ptr != null) {+ // use stored pointer from procedure descriptor tablecodePtr = ptr;}+ else {+ // if pointer refers to simple jump/thunk - follow it+ InstructionBlock codeBlock = disassembler.pseudoDisassembleBlock(codePtr, null, 1);+ if (codeBlock.isEmpty() || codeBlock.hasInstructionError()) {+ throw new MemoryAccessException(+ "Code pointer " + codePtr.toString(true) + " does not refer to valid code");+ }+ // TODO: may need to handle more complex thunks+ Instruction instr = codeBlock.getInstructionAt(codePtr);+ FlowType flowType = instr.getFlowType();+ if (flowType.isJump()) {+ Address[] flows = instr.getFlows();+ if (flows.length == 1) {+ codePtr = flows[0];+ }+ }+ }return codePtr;}diff --git a/Ghidra/Processors/Atmel/data/languages/avr8.ldefs b/Ghidra/Processors/Atmel/data/languages/avr8.ldefsindex 9bcb83e8fc..eeb1463cc7 100644--- a/Ghidra/Processors/Atmel/data/languages/avr8.ldefs+++ b/Ghidra/Processors/Atmel/data/languages/avr8.ldefs@@ -15,7 +15,7 @@<compiler name="gcc" spec="avr8gcc.cspec" id="gcc"/><compiler name="iarV1" spec="avr8iarV1.cspec" id="iarV1"/><compiler name="imgCraftV8" spec="avr8imgCraftV8.cspec" id="imgCraftV8"/>- <external_name tool="gnu" name="avr:6"/>+ <external_name tool="gnu" name="avr:31"/><external_name tool="IDA-PRO" name="avr"/></language>@@ -30,13 +30,13 @@id="avr8:LE:16:extended"><description>AVR8 with 22-bit word addressable code space</description><compiler name="gcc" spec="avr8egcc.cspec" id="gcc"/>- <external_name tool="gnu" name="avr:6"/>+ <external_name tool="gnu" name="avr:51"/><external_name tool="IDA-PRO" name="avr"/></language><language processor="AVR8"endian="little"- size="16"+ size="24"variant="atmega256"version="1.3"slafile="avr8eind.sla"@@ -45,12 +45,13 @@id="avr8:LE:16:atmega256"><description>AVR8 for an Atmega 256</description><compiler name="gcc" spec="avr8egcc.cspec" id="gcc"/>+ <external_name tool="gnu" name="avr:6"/><external_name tool="IDA-PRO" name="avr"/></language><language processor="AVR8"endian="little"- size="16"+ size="24"variant="Xmega"version="1.3"slafile="avr8xmega.sla"@@ -58,6 +59,7 @@id="avr8:LE:24:xmega"><description>AVR8 for an Xmega</description><compiler name="gcc" spec="avr8egcc.cspec" id="gcc"/>+ <external_name tool="gnu" name="avr:107"/><external_name tool="IDA-PRO" name="avr"/></language>diff --git a/Ghidra/Processors/Atmel/data/languages/avr8.opinion b/Ghidra/Processors/Atmel/data/languages/avr8.opinionindex 3dec941f9f..0541e25cea 100644--- a/Ghidra/Processors/Atmel/data/languages/avr8.opinion+++ b/Ghidra/Processors/Atmel/data/languages/avr8.opinion@@ -1,9 +1,10 @@<opinions><constraint loader="Executable and Linking Format (ELF)" compilerSpecID="gcc"><constraint primary="83" processor="AVR8" endian="little" />- <!-- Elf e_flags are used for the secondary attribute -->+ <!-- Elf e_flags are used for the secondary attribute TODO: need to mask with 0x7f --><constraint primary="83" secondary= "31" processor="AVR8" size="16" variant="default"/><constraint primary="83" secondary= "51" processor="AVR8" size="16" variant="extended"/><constraint primary="83" secondary= "6" processor="AVR8" size="24" variant="atmega256"/>+ <constraint primary="83" secondary= "107" processor="AVR8" size="24" variant="Xmega"/></constraint></opinions>diff --git a/Ghidra/Processors/Atmel/src/main/java/ghidra/app/util/bin/format/elf/AVR8_ElfRelocationConstants.java b/Ghidra/Processors/Atmel/src/main/java/ghidra/app/util/bin/format/elf/AVR8_ElfRelocationConstants.javanew file mode 100644index 0000000000..dcceba4e83--- /dev/null+++ b/Ghidra/Processors/Atmel/src/main/java/ghidra/app/util/bin/format/elf/AVR8_ElfRelocationConstants.java@@ -0,0 +1,57 @@+/* ###+ * 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.app.util.bin.format.elf;++public class AVR8_ElfRelocationConstants {++ public static final int R_AVR_NONE = 0;+ public static final int R_AVR_32 = 1;+ public static final int R_AVR_7_PCREL = 2;+ public static final int R_AVR_13_PCREL = 3;+ public static final int R_AVR_16 = 4;+ public static final int R_AVR_16_PM = 5;+ public static final int R_AVR_LO8_LDI = 6;+ public static final int R_AVR_HI8_LDI = 7;+ public static final int R_AVR_HH8_LDI = 8;+ public static final int R_AVR_LO8_LDI_NEG = 9;+ public static final int R_AVR_HI8_LDI_NEG = 10;+ public static final int R_AVR_HH8_LDI_NEG = 11;+ public static final int R_AVR_LO8_LDI_PM = 12;+ public static final int R_AVR_HI8_LDI_PM = 13;+ public static final int R_AVR_HH8_LDI_PM = 14;+ public static final int R_AVR_LO8_LDI_PM_NEG = 15;+ public static final int R_AVR_HI8_LDI_PM_NEG = 16;+ public static final int R_AVR_HH8_LDI_PM_NEG = 17;+ public static final int R_AVR_CALL = 18;+ public static final int R_AVR_LDI = 19;+ public static final int R_AVR_6 = 20;+ public static final int R_AVR_6_ADIW = 21;+ public static final int R_AVR_MS8_LDI = 22;+ public static final int R_AVR_MS8_LDI_NEG = 23;+ public static final int R_AVR_LO8_LDI_GS = 24;+ public static final int R_AVR_HI8_LDI_GS = 25;+ public static final int R_AVR_8 = 26;+ public static final int R_AVR_8_LO8 = 27;+ public static final int R_AVR_8_HI8 = 28;+ public static final int R_AVR_8_HLO8 = 29;+ public static final int R_AVR_DIFF8 = 30;+ public static final int R_AVR_DIFF16 = 31;+ public static final int R_AVR_DIFF32 = 32;+ public static final int R_AVR_LDS_STS_16 = 33;+ public static final int R_AVR_PORT6 = 34;+ public static final int R_AVR_PORT5 = 35;++}diff --git a/Ghidra/Processors/Atmel/src/main/java/ghidra/app/util/bin/format/elf/extend/AVR8_ElfExtension.java b/Ghidra/Processors/Atmel/src/main/java/ghidra/app/util/bin/format/elf/extend/AVR8_ElfExtension.javanew file mode 100644index 0000000000..524a9ac645--- /dev/null+++ b/Ghidra/Processors/Atmel/src/main/java/ghidra/app/util/bin/format/elf/extend/AVR8_ElfExtension.java@@ -0,0 +1,77 @@+/* ###+ * 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.app.util.bin.format.elf.extend;++import ghidra.app.util.bin.format.elf.*;+import ghidra.program.model.address.AddressSpace;+import ghidra.program.model.lang.Language;++public class AVR8_ElfExtension extends ElfExtension {++ // Processor specific flag mask+ public static final int EF_AVR_MACH = 0x7F;++ // bit #7 indicates elf file uses local symbols for relocations+ public static final int EF_AVR_LINKRELAX_PREPARED = 0x80;++ public static final int E_AVR_MACH_AVR1 = 1;+ public static final int E_AVR_MACH_AVR2 = 2;+ public static final int E_AVR_MACH_AVR25 = 25;+ public static final int E_AVR_MACH_AVR3 = 3;+ public static final int E_AVR_MACH_AVR31 = 31;+ public static final int E_AVR_MACH_AVR35 = 35;+ public static final int E_AVR_MACH_AVR4 = 4;+ public static final int E_AVR_MACH_AVR5 = 5;+ public static final int E_AVR_MACH_AVR51 = 51;+ public static final int E_AVR_MACH_AVR6 = 6;+ public static final int E_AVR_MACH_XMEGA1 = 101;+ public static final int E_AVR_MACH_XMEGA2 = 102;+ public static final int E_AVR_MACH_XMEGA3 = 103;+ public static final int E_AVR_MACH_XMEGA4 = 104;+ public static final int E_AVR_MACH_XMEGA5 = 105;+ public static final int E_AVR_MACH_XMEGA6 = 106;+ public static final int E_AVR_MACH_XMEGA7 = 107;++ @Override+ public boolean canHandle(ElfHeader elf) {+ if (elf.e_machine() != ElfConstants.EM_AVR) {+ return false;+ }+ // TODO: limit to specific architectures?+ return true;+ }++ @Override+ public boolean canHandle(ElfLoadHelper elfLoadHelper) {+ Language language = elfLoadHelper.getProgram().getLanguage();+ return canHandle(elfLoadHelper.getElfHeader()) &&+ "AVR8".equals(language.getProcessor().toString());+ }++ @Override+ public String getDataTypeSuffix() {+ return "_AVR";+ }++ @Override+ public long getAdjustedMemoryOffset(long elfOffset, AddressSpace space) {+ if ("code".equals(space.getName())) {+ elfOffset >>= 1; // code space modeled with wordsize=2 which differs from ELF model+ }+ return elfOffset;+ }++}diff --git a/Ghidra/Processors/Atmel/src/test.processors/java/ghidra/test/processors/AVR8_xmega_GCC_O0_EmulatorTest.java b/Ghidra/Processors/Atmel/src/test.processors/java/ghidra/test/processors/AVR8_xmega_GCC_O0_EmulatorTest.javanew file mode 100644index 0000000000..772b9cd58c--- /dev/null+++ b/Ghidra/Processors/Atmel/src/test.processors/java/ghidra/test/processors/AVR8_xmega_GCC_O0_EmulatorTest.java@@ -0,0 +1,61 @@+/* ###+ * 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;++import ghidra.framework.options.Options;+import ghidra.program.model.listing.Program;+import ghidra.test.processors.support.EmulatorTestRunner;+import ghidra.test.processors.support.ProcessorEmulatorTestAdapter;+import junit.framework.Test;++public class AVR8_xmega_GCC_O0_EmulatorTest extends ProcessorEmulatorTestAdapter {++ private static final String LANGUAGE_ID = "avr8:LE:24:xmega";+ private static final String COMPILER_SPEC_ID = "gcc";++ private static final String[] REG_DUMP_SET = new String[] {};++ public AVR8_xmega_GCC_O0_EmulatorTest(String name) throws Exception {+ super(name, LANGUAGE_ID, COMPILER_SPEC_ID, REG_DUMP_SET);+ }++ @Override+ protected String getProcessorDesignator() {+ return "AVR8_xmega_GCC_O0";+ }++ @Override+ protected void initializeState(EmulatorTestRunner testRunner, Program program)+ throws Exception {+ // These eliminate "uninitialized register" errors. Not strictly needed, but helps find actual problems.+ testRunner.setRegister("SP", 0x0);+ testRunner.setRegister("R1", 0x0);+ testRunner.setRegister("Y", 0x0);+ testRunner.setRegister("W", 0x0);+ }++ @Override+ protected void setAnalysisOptions(Options analysisOptions) {+ super.setAnalysisOptions(analysisOptions);+ analysisOptions.setBoolean("Reference", false); // too many bad disassemblies+ analysisOptions.setBoolean("Data Reference", false);+ }++ public static Test suite() {+ return ProcessorEmulatorTestAdapter.buildEmulatorTestSuite(+ AVR8_xmega_GCC_O0_EmulatorTest.class);+ }+}diff --git a/Ghidra/Processors/Atmel/src/test.processors/java/ghidra/test/processors/AVR8_xmega_GCC_O3_EmulatorTest.java b/Ghidra/Processors/Atmel/src/test.processors/java/ghidra/test/processors/AVR8_xmega_GCC_O3_EmulatorTest.javanew file mode 100644index 0000000000..ee75b0aeb5--- /dev/null+++ b/Ghidra/Processors/Atmel/src/test.processors/java/ghidra/test/processors/AVR8_xmega_GCC_O3_EmulatorTest.java@@ -0,0 +1,61 @@+/* ###+ * 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;++import ghidra.framework.options.Options;+import ghidra.program.model.listing.Program;+import ghidra.test.processors.support.EmulatorTestRunner;+import ghidra.test.processors.support.ProcessorEmulatorTestAdapter;+import junit.framework.Test;++public class AVR8_xmega_GCC_O3_EmulatorTest extends ProcessorEmulatorTestAdapter {++ private static final String LANGUAGE_ID = "avr8:LE:24:xmega";+ private static final String COMPILER_SPEC_ID = "gcc";++ private static final String[] REG_DUMP_SET = new String[] {};++ public AVR8_xmega_GCC_O3_EmulatorTest(String name) throws Exception {+ super(name, LANGUAGE_ID, COMPILER_SPEC_ID, REG_DUMP_SET);+ }++ @Override+ protected String getProcessorDesignator() {+ return "AVR8_xmega_GCC_O3";+ }++ @Override+ protected void initializeState(EmulatorTestRunner testRunner, Program program)+ throws Exception {+ // These eliminate "uninitialized register" errors. Not strictly needed, but helps find actual problems.+ testRunner.setRegister("SP", 0x0);+ testRunner.setRegister("R1", 0x0);+ testRunner.setRegister("Y", 0x0);+ testRunner.setRegister("W", 0x0);+ }++ @Override+ protected void setAnalysisOptions(Options analysisOptions) {+ super.setAnalysisOptions(analysisOptions);+ analysisOptions.setBoolean("Reference", false); // too many bad disassemblies+ analysisOptions.setBoolean("Data Reference", false);+ }++ public static Test suite() {+ return ProcessorEmulatorTestAdapter.buildEmulatorTestSuite(+ AVR8_xmega_GCC_O3_EmulatorTest.class);+ }+}