Ghidra is a software reverse engineering (SRE) framework
GP-468 Markup info in ELF gnu.build-id and gnu.debuglink
dev747368fe9324d26367
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/GnuBuildIdDataType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/GnuBuildIdDataType.javanew file mode 100644index 0000000000..2035c6e05e--- /dev/null+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/GnuBuildIdDataType.java@@ -0,0 +1,91 @@+/* ###+ * 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;++import static ghidra.app.util.bin.StructConverter.*;++import ghidra.program.model.data.*;+import ghidra.program.model.mem.MemBuffer;+import ghidra.program.model.mem.MemoryAccessException;+import ghidra.util.exception.DuplicateNameException;++/**+ * Factory data type that marks up a Gnu Build-Id record from a+ * ELF .note.gnu.build-id section+ */+class GnuBuildIdDataType extends FactoryStructureDataType {+ private static final int MAX_SANE_STR_LENS = 1024;+ private long sectionSize;++ /**+ * Creates a new GnuBuildIdDataType instance.+ *+ * @param dtm the {@link DataTypeManager} for the program+ * @param sectionSize the size of the section (for bounds checking, assumes this+ * is the only record in the section)+ */+ GnuBuildIdDataType(DataTypeManager dtm, long sectionSize) {+ super("Gnu_BuildId", dtm);+ this.sectionSize = sectionSize;+ }++ @Override+ public DataType clone(DataTypeManager dtm) {+ if (dtm == dataMgr) {+ return this;+ }+ return new GnuBuildIdDataType(dtm, sectionSize);+ }++ @Override+ protected void populateDynamicStructure(MemBuffer buf, Structure es) {+ try {+ long nameLen = buf.getUnsignedInt(0);+ long descLen = buf.getUnsignedInt(4);+ if (nameLen > MAX_SANE_STR_LENS || descLen > MAX_SANE_STR_LENS ||+ nameLen + descLen + 12 /* sizeof int fields */ > sectionSize) {+ return;+ }++ es.add(DWORD, "namesz", "Length of name field");+ es.add(DWORD, "descsz", "Length of description field");+ es.add(DWORD, "type", "Vendor specific type");+ if (nameLen > 0) {+ es.add(StringDataType.dataType, (int) nameLen, "name", "Build-id vendor name");+ }+ if (descLen > 0) {+ es.add(new ArrayDataType(BYTE, (int) descLen, BYTE.getLength(), dataMgr),+ "description", "Build-id value");+ }+ }+ catch (MemoryAccessException e) {+ // ignore and drop thru with partial defined structure type+ }++ }++ @Override+ protected Structure setCategoryPath(Structure struct, MemBuffer buf) {+ try {+ struct.setCategoryPath(new CategoryPath("/ELF"));+ }+ catch (DuplicateNameException e) {+ // ignore - will not happen+ }+ return struct;+ }++}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/GnuDebugLinkDataType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/GnuDebugLinkDataType.javanew file mode 100644index 0000000000..3a29a981e1--- /dev/null+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/GnuDebugLinkDataType.java@@ -0,0 +1,75 @@+/* ###+ * 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;++import static ghidra.app.util.bin.StructConverter.*;++import ghidra.docking.settings.SettingsImpl;+import ghidra.program.model.data.*;+import ghidra.program.model.mem.MemBuffer;+import ghidra.util.NumericUtilities;+import ghidra.util.exception.DuplicateNameException;++/**+ * Factory data type that marks up a ELF .gnu_debuglink section.+ */+class GnuDebugLinkDataType extends FactoryStructureDataType {+ private long sectionSize;++ /**+ * Creates a new GnuDebugLinkDataType instance.+ *+ * @param dtm the program's {@link DataTypeManager}+ * @param sectionSize the size of the section (for bounds checking)+ */+ GnuDebugLinkDataType(DataTypeManager dtm, long sectionSize) {+ super("Gnu_DebugLink", dtm);+ this.sectionSize = sectionSize;+ }++ @Override+ public DataType clone(DataTypeManager dtm) {+ if (dtm == dataMgr) {+ return this;+ }+ return new GnuDebugLinkDataType(dtm, sectionSize);+ }++ @Override+ protected void populateDynamicStructure(MemBuffer buf, Structure es) {+ StringDataInstance filenameStr = StringDataInstance.getStringDataInstance(+ StringDataType.dataType, buf, SettingsImpl.NO_SETTINGS, -1);+ int filenameLen = filenameStr.getStringLength();+ if (filenameLen <= 0 || filenameLen + 4 /* crc field */ > sectionSize) {+ return;+ }+ filenameLen = (int) NumericUtilities.getUnsignedAlignedValue(filenameLen, 4);+ es.add(StringDataType.dataType, filenameLen, "filename", "Debug file name");+ es.add(DWORD, "crc", null);+ }++ @Override+ protected Structure setCategoryPath(Structure struct, MemBuffer buf) {+ try {+ struct.setCategoryPath(new CategoryPath("/ELF"));+ }+ catch (DuplicateNameException e) {+ // ignore - will not happen+ }+ return struct;+ }++}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 6198ce639d..3d6e8878d8 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@@ -167,6 +167,8 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {markupHashTable(monitor);markupGnuHashTable(monitor);+ markupGnuBuildId(monitor);+ markupGnuDebugLink(monitor);processGNU(monitor);processGNU_readOnly(monitor);@@ -749,7 +751,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {Address baseAddress = relocationSpace.getTruncatedAddress(baseWordOffset, true);// long relocationOffset = baseWordOffset + reloc.getOffset();// r_offset is defined to be a byte offset (assume byte size is 1)- Address relocAddr = context != null ? context.getRelocationAddress(baseAddress, reloc.getOffset()) : baseAddress.addWrap(reloc.getOffset());;+ Address relocAddr = context != null ? context.getRelocationAddress(baseAddress, reloc.getOffset()) : baseAddress.addWrap(reloc.getOffset());// Address relocAddr = relocationSpace.getTruncatedAddress(relocationOffset, true);long[] values = new long[] { reloc.getSymbolIndex() };@@ -867,7 +869,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {}Structure phStructDt = (Structure) elf.getProgramHeaders()[0].toDataType();- phStructDt = (Structure) phStructDt.clone(program.getDataTypeManager());+ phStructDt = phStructDt.clone(program.getDataTypeManager());Array arrayDt = new ArrayDataType(phStructDt, headerCount, size);@@ -925,7 +927,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {}Structure shStructDt = (Structure) elf.getSections()[0].toDataType();- shStructDt = (Structure) shStructDt.clone(program.getDataTypeManager());+ shStructDt = shStructDt.clone(program.getDataTypeManager());Array arrayDt = new ArrayDataType(shStructDt, elf.e_shnum(), elf.e_shentsize());@@ -1899,6 +1901,38 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {// return new AddressRangeImpl(alignedAddress, freeRange.getMaxAddress());// }+ private void markupGnuBuildId(TaskMonitor monitor) {++ ElfSectionHeader sh = elf.getSection(".note.gnu.build-id");+ Address addr = findLoadAddress(sh, 0);+ if (addr == null) {+ return;+ }+ try {+ listing.createData(addr,+ new GnuBuildIdDataType(program.getDataTypeManager(), sh.getSize()));+ }+ catch (Exception e) {+ log("Failed to properly markup Gnu Build-Id at " + addr + ": " + getMessage(e));+ }+ }++ private void markupGnuDebugLink(TaskMonitor monitor) {++ ElfSectionHeader sh = elf.getSection(".gnu_debuglink");+ Address addr = findLoadAddress(sh, 0);+ if (addr == null) {+ return;+ }+ try {+ listing.createData(addr,+ new GnuDebugLinkDataType(program.getDataTypeManager(), sh.getSize()));+ }+ catch (Exception e) {+ log("Failed to properly markup Gnu DebugLink at " + addr + ": " + getMessage(e));+ }+ }+private void markupHashTable(TaskMonitor monitor) {ElfDynamicTable dynamicTable = elf.getDynamicTable();