Ghidra is a software reverse engineering (SRE) framework
GP-3152 eliminated the public mutability and writing of ELF Headers
ghidra15dd1e8734f25
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDefaultGotPltMarkup.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDefaultGotPltMarkup.javaindex b68e2e113e..db11e6a955 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDefaultGotPltMarkup.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDefaultGotPltMarkup.java@@ -506,8 +506,8 @@ public class ElfDefaultGotPltMarkup {/*** Convert all symbols over a specified range to thunks to external functions.- * @param minAddress- * @param maxAddress+ * @param minAddress range minimum address+ * @param maxAddress range maximum address (inclusive)* @return number of symbols converted*/private int convertSymbolsToExternalFunctions(Address minAddress, Address maxAddress) {diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDynamic.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDynamic.javaindex b6f5572fbc..7761f3ed5a 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDynamic.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDynamic.java@@ -18,8 +18,6 @@ package ghidra.app.util.bin.format.elf;import java.io.IOException;import ghidra.app.util.bin.BinaryReader;-import ghidra.app.util.bin.ByteArrayConverter;-import ghidra.util.DataConverter;import ghidra.util.StringUtilities;/**@@ -52,7 +50,7 @@ import ghidra.util.StringUtilities;** </code></pre>*/-public class ElfDynamic implements ByteArrayConverter {+public class ElfDynamic {private ElfHeader elf;@@ -122,22 +120,6 @@ public class ElfDynamic implements ByteArrayConverter {return d_val;}- /**- * Sets the value of this dynamic. The value could be an address or a number.- * @param value the new value dynamic- */- public void setValue(long value) {- this.d_val = value;- }-- /**- * Sets the value of this dynamic. The value could be an address or a number.- * @param value the new value dynamic- */- public void setValue(int value) {- this.d_val = Integer.toUnsignedLong(value);- }-/*** A convenience method for getting a string representing the d_tag value.* For example, if d_tag == DT_SYMTAB, then this method returns "DT_SYMTAB".@@ -156,27 +138,6 @@ public class ElfDynamic implements ByteArrayConverter {return getTagAsString();}- /**- * @see ghidra.app.util.bin.ByteArrayConverter#toBytes(ghidra.util.DataConverter)- */- public byte [] toBytes(DataConverter dc) {- byte [] bytes = new byte[sizeof()];- write(bytes, 0, dc);- return bytes;- }-- public void write(byte[] data, int offset, DataConverter dc)- throws ArrayIndexOutOfBoundsException {- if (elf.is32Bit()) {- dc.putInt(data, offset, d_tag);- dc.putInt(data, offset + 4, (int) d_val);- }- else {- dc.putLong(data, offset, d_tag);- dc.putLong(data, offset + 8, d_val);- }- }-/*** @return the size in bytes of this object.*/diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDynamicTable.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDynamicTable.javaindex 6a99121800..377954a486 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDynamicTable.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDynamicTable.java@@ -20,7 +20,6 @@ import java.util.*;import ghidra.app.util.bin.BinaryReader;import ghidra.program.model.data.*;-import ghidra.util.DataConverter;import ghidra.util.exception.DuplicateNameException;import ghidra.util.exception.NotFoundException;@@ -113,33 +112,11 @@ public class ElfDynamicTable implements ElfFileSection {return getDynamics(type.value);}- /**- * Sets the dynamic with the specified type to the specified value.- * @param type the dynamic type- * @param value the new value- */- public void setDynamicValue(long type, long value) {- for (int i = 0; i < dynamics.size(); i++) {- ElfDynamic dyn = dynamics.get(i);- if (dyn.getTag() == type) {- dyn.setValue(value);- }- }- }-- /**- * Sets the dynamic with the specified (enum) type to the specified value.- * @param type the dynamic (enum) type- * @param value the new value- */- public void setDynamicValue(ElfDynamicType type, long value) {- setDynamicValue(type.value, value);- }-/*** Returns the value of the specified dynamic type.* @param type the dynamic type* @return the dynamic value+ * @throws NotFoundException if requested value type not found*/public long getDynamicValue(long type) throws NotFoundException {for (int i = 0; i < dynamics.size(); i++) {@@ -179,6 +156,7 @@ public class ElfDynamicTable implements ElfFileSection {* Returns the value of the specified dynamic (enum) type.* @param type the dynamic (enum) type* @return the dynamic value+ * @throws NotFoundException if requested value type not found*/public long getDynamicValue(ElfDynamicType type) throws NotFoundException {return getDynamicValue(type.value);@@ -249,20 +227,4 @@ public class ElfDynamicTable implements ElfFileSection {return header.is32Bit() ? 8 : 16;}- /**- * Get this dynamic table data as a byte array- * @param dc data converter- * @return data array- */- public byte[] toBytes(DataConverter dc)- throws ArrayIndexOutOfBoundsException {- byte[] data = new byte[(int) getLength()];- int entrySize = getEntrySize();- for (int i = 0; i < dynamics.size(); i++) {- ElfDynamic dyn = dynamics.get(i);- dyn.write(data, i * entrySize, dc);- }- return data;- }-}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDynamicType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDynamicType.javaindex f489532776..104fb2caef 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDynamicType.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfDynamicType.java@@ -245,7 +245,7 @@ public class ElfDynamicType {/*** Add the specified dynamic entry type to the specified map.* @param type dynamic entry type- * @param dynamicTypeMap+ * @param dynamicTypeMap map of dynamic types* @throws DuplicateNameException if new type name already defined within* the specified map*/diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfHeader.javaindex 346bc685d7..f12a969eca 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfHeader.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfHeader.java@@ -16,19 +16,16 @@package ghidra.app.util.bin.format.elf;import java.io.IOException;-import java.io.RandomAccessFile;import java.util.*;import java.util.function.Consumer;import ghidra.app.util.bin.*;-import ghidra.app.util.bin.format.Writeable;import ghidra.app.util.bin.format.elf.ElfRelocationTable.TableFormat;import ghidra.app.util.bin.format.elf.extend.ElfExtensionFactory;import ghidra.app.util.bin.format.elf.extend.ElfLoadAdapter;import ghidra.program.model.data.*;import ghidra.program.model.mem.MemoryAccessException;import ghidra.program.model.mem.MemoryBlock;-import ghidra.util.DataConverter;import ghidra.util.Msg;import ghidra.util.exception.AssertException;import ghidra.util.exception.NotFoundException;@@ -37,7 +34,7 @@ import ghidra.util.exception.NotFoundException;* A class to represent the Executable and Linking Format (ELF)* header and specification.*/-public class ElfHeader implements StructConverter, Writeable {+public class ElfHeader implements StructConverter {private static final int MAX_HEADERS_TO_CHECK_FOR_IMAGEBASE = 20;@@ -55,9 +52,11 @@ public class ElfHeader implements StructConverter, Writeable {private String e_ident_magic_str; //magic stringprivate byte e_ident_class; //file classprivate byte e_ident_data; //data encoding+ @SuppressWarnings("unused")private byte e_ident_version; //file versionprivate byte e_ident_osabi; //operating system and abiprivate byte e_ident_abiversion; //abi version+ @SuppressWarnings("unused")private byte[] e_ident_pad; //paddingprivate short e_type; //object file typeprivate short e_machine; //target architecture@@ -2078,66 +2077,4 @@ public class ElfHeader implements StructConverter, Writeable {}- /**- * @see ghidra.app.util.bin.format.Writeable#write(java.io.RandomAccessFile, ghidra.util.DataConverter)- */- @Override- public void write(RandomAccessFile raf, DataConverter dc) throws IOException {- raf.seek(0);- raf.writeByte(e_ident_magic_num);- raf.write(e_ident_magic_str.getBytes());- raf.writeByte(e_ident_class);- raf.writeByte(e_ident_data);- raf.writeByte(e_ident_version);- raf.writeByte(e_ident_osabi);- raf.writeByte(e_ident_abiversion);- raf.write(e_ident_pad);- raf.write(dc.getBytes(e_type));- raf.write(dc.getBytes(e_machine));- raf.write(dc.getBytes(e_version));-- if (is32Bit()) {- raf.write(dc.getBytes((int) e_entry));- raf.write(dc.getBytes((int) e_phoff));- raf.write(dc.getBytes((int) e_shoff));- }- else if (is64Bit()) {- raf.write(dc.getBytes(e_entry));- raf.write(dc.getBytes(e_phoff));- raf.write(dc.getBytes(e_shoff));- }-- raf.write(dc.getBytes(e_flags));- raf.write(dc.getBytes(e_ehsize));- raf.write(dc.getBytes(e_phentsize));- if (e_phnum >= Short.toUnsignedInt(ElfConstants.PN_XNUM)) {- throw new IOException(- "Unsupported program header count serialization: " + e_phnum);- }- raf.write(dc.getBytes(e_phnum));- raf.write(dc.getBytes(e_shentsize));- if (e_shnum >= Short.toUnsignedInt(ElfSectionHeaderConstants.SHN_LORESERVE)) {- throw new IOException(- "Unsupported section header count serialization: " + e_shnum);- }- raf.write(dc.getBytes(e_shnum));- raf.write(dc.getBytes(e_shstrndx));- }-- /**- * Sets the section header offset.- * @param offset the new section header offset- */- public void setSectionHeaderOffset(long offset) {- this.e_shoff = offset;- }-- /**- * Sets the program header offset.- * @param offset the new program header offset- */- public void setProgramHeaderOffset(long offset) {- this.e_phoff = offset;- }-}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfLoadHelper.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfLoadHelper.javaindex 2946451e28..965a05664b 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfLoadHelper.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfLoadHelper.java@@ -75,7 +75,7 @@ public interface ElfLoadHelper {/*** Mark this location as code in the CodeMap.* The analyzers will pick this up and disassemble the code.- * @param address+ * @param address code memory address to be marked*/void markAsCode(Address address);@@ -107,6 +107,7 @@ public interface ElfLoadHelper {* Create an undefined data item to reserve the location as data, without specifying the type* @param address location of undefined data to create* @param length size of the undefined data item+ * @return {@link Data} which was created or null if conflict occurs*/Data createUndefinedData(Address address, int length);@@ -114,7 +115,7 @@ public interface ElfLoadHelper {* Create a data item using the specified data type* @param address location of undefined data to create* @param dt data type- * @return data or null if not successful+ * @return {@link Data} which was created or null if conflict occurs*/Data createData(Address address, DataType dt);@@ -140,7 +141,7 @@ public interface ElfLoadHelper {* @param pinAbsolute true if address is absolute and should not change* @param namespace symbol namespace (should generally be null for global namespace)* @return program symbol- * @throws InvalidInputException+ * @throws InvalidInputException if an invalid name is specified*/Symbol createSymbol(Address addr, String name, boolean isPrimary, boolean pinAbsolute,Namespace namespace) throws InvalidInputException;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfProgramHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfProgramHeader.javaindex 58060255a0..e45341b763 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfProgramHeader.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfProgramHeader.java@@ -15,17 +15,13 @@*/package ghidra.app.util.bin.format.elf;-import java.util.HashMap;-import java.io.IOException;-import java.io.RandomAccessFile;+import java.util.HashMap;import ghidra.app.util.bin.BinaryReader;import ghidra.app.util.bin.StructConverter;import ghidra.app.util.bin.format.MemoryLoadable;-import ghidra.app.util.bin.format.Writeable;import ghidra.program.model.data.*;-import ghidra.util.DataConverter;import ghidra.util.StringUtilities;/**@@ -66,7 +62,7 @@ import ghidra.util.StringUtilities;* </pre>*/public class ElfProgramHeader- implements StructConverter, Comparable<ElfProgramHeader>, Writeable, MemoryLoadable {+ implements StructConverter, Comparable<ElfProgramHeader>, MemoryLoadable {protected ElfHeader header;@@ -141,33 +137,6 @@ public class ElfProgramHeaderreturn header;}- /**- * @see ghidra.app.util.bin.format.Writeable#write(java.io.RandomAccessFile, ghidra.util.DataConverter)- */- @Override- public void write(RandomAccessFile raf, DataConverter dc) throws IOException {- if (header.is32Bit()) {- raf.write(dc.getBytes(p_type));- raf.write(dc.getBytes((int) p_offset));- raf.write(dc.getBytes((int) p_vaddr));- raf.write(dc.getBytes((int) p_paddr));- raf.write(dc.getBytes((int) p_filesz));- raf.write(dc.getBytes((int) p_memsz));- raf.write(dc.getBytes(p_flags));- raf.write(dc.getBytes((int) p_align));- }- else if (header.is64Bit()) {- raf.write(dc.getBytes(p_flags));- raf.write(dc.getBytes(p_type));- raf.write(dc.getBytes(p_offset));- raf.write(dc.getBytes(p_vaddr));- raf.write(dc.getBytes(p_paddr));- raf.write(dc.getBytes(p_filesz));- raf.write(dc.getBytes(p_memsz));- raf.write(dc.getBytes(p_align));- }- }-/*** Get header type as string. ElfProgramHeaderType name will be returned* if know, otherwise a numeric name of the form "PT_0x12345678" will be returned.@@ -241,10 +210,6 @@ public class ElfProgramHeaderreturn p_flags;}- public void setFlags(int flags) {- this.p_flags = flags;- }-/*** Returns true if this segment is readable when loaded* @return true if this segment is readable when loaded@@ -352,25 +317,10 @@ public class ElfProgramHeader* the ELF file.* @param offset the new offset value*/- public void setOffset(long offset) {+ void setOffset(long offset) {this.p_offset = offset;}-- /**- * Sets the file and memory size.- * Note: the file size can be less than or- * equal to the memory size. It cannot be larger.- * If the file size is less than the memory size,- * then the rest of the space is considered to be- * uninitialized.- * @param fileSize the new file size- * @param memSize the new memory size- */- public void setSize(long fileSize, long memSize) {- p_filesz = fileSize;- p_memsz = memSize;- }-+/*** On systems for which physical addressing is relevant, this member is reserved for the* segment's physical address. Because System V ignores physical addressing for application@@ -451,16 +401,6 @@ public class ElfProgramHeaderreturn typeEnum;}- /**- * Sets the new physical and virtual addresses- * @param paddr the new physical address- * @param vaddr the new virtual address- */- public void setAddress(long paddr, long vaddr) {- this.p_paddr = header.unadjustAddressForPrelink(paddr);- this.p_vaddr = header.unadjustAddressForPrelink(vaddr);- }-/*** @see java.lang.Comparable#compareTo(java.lang.Object)*/diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfProgramHeaderType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfProgramHeaderType.javaindex 67e0311a8b..d2b8674899 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfProgramHeaderType.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfProgramHeaderType.java@@ -71,7 +71,7 @@ public class ElfProgramHeaderType {/*** Add the specified program header type to the specified map.* @param type program header type- * @param programHeaderTypeMap+ * @param programHeaderTypeMap map of program header types* @throws DuplicateNameException if new type name already defined within* the specified map*/diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfRelocation.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfRelocation.javaindex ec9c6d1a9a..cd8a3c5284 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfRelocation.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfRelocation.java@@ -18,13 +18,12 @@ package ghidra.app.util.bin.format.elf;import java.io.IOException;import java.lang.reflect.Constructor;-import ghidra.app.util.bin.*;+import ghidra.app.util.bin.BinaryReader;+import ghidra.app.util.bin.StructConverter;import ghidra.app.util.bin.format.elf.extend.ElfLoadAdapter;import ghidra.app.util.bin.format.elf.relocation.ElfRelocationContext;import ghidra.app.util.bin.format.elf.relocation.ElfRelocationHandler;import ghidra.program.model.data.*;-import ghidra.util.Conv;-import ghidra.util.DataConverter;/*** A class to represent the Elf32_Rel and Elf64_Rel data structure.@@ -73,7 +72,7 @@ import ghidra.util.DataConverter;* {@link #initElfRelocation(BinaryReader, ElfHeader, int, boolean)} and/or* {@link #initElfRelocation(ElfHeader, int, boolean, long, long, long)}.*/-public class ElfRelocation implements ByteArrayConverter, StructConverter {+public class ElfRelocation implements StructConverter {protected static final String R_OFFSET_COMMENT = "location to apply the relocation action";protected static final String R_INFO_COMMENT =@@ -81,6 +80,9 @@ public class ElfRelocation implements ByteArrayConverter, StructConverter {protected static final String R_ADDEND_COMMENT ="a constant addend used to compute the relocatable field value";+ private static final long BYTE_MASK = 0xFF;+ private static final long INT_MASK = 0xFFFFFFFFL;+private long r_offset;private long r_info;private long r_addend;@@ -189,29 +191,29 @@ public class ElfRelocation implements ByteArrayConverter, StructConverter {* @param elfHeader ELF header* @param relocationTableIndex index of relocation within relocation table* @param withAddend true if if RELA entry with addend, else false- * @param r_offset The offset for the entry- * @param r_info The info value for the entry- * @param r_addend The addend for the entry+ * @param offset The offset for the entry (r_offset)+ * @param info The info value for the entry (r_info)+ * @param addend The addend for the entry (r_addend)* @throws IOException if an IO or parse error occurs*/protected void initElfRelocation(ElfHeader elfHeader, int relocationTableIndex,- boolean withAddend, long r_offset, long r_info, long r_addend) throws IOException {+ boolean withAddend, long offset, long info, long addend) throws IOException {this.is32bit = elfHeader.is32Bit();this.relocationIndex = relocationTableIndex;this.hasAddend = withAddend;if (is32bit) {- this.r_offset = Integer.toUnsignedLong((int) r_offset);- this.r_info = Integer.toUnsignedLong((int) r_info);+ this.r_offset = Integer.toUnsignedLong((int) offset);+ this.r_info = Integer.toUnsignedLong((int) info);if (hasAddend) {- this.r_addend = Integer.toUnsignedLong((int) r_addend);+ this.r_addend = Integer.toUnsignedLong((int) addend);}}else {- this.r_offset = r_offset;- this.r_info = r_info;+ this.r_offset = offset;+ this.r_info = info;if (hasAddend) {- this.r_addend = r_addend;+ this.r_addend = addend;}}}@@ -262,22 +264,6 @@ public class ElfRelocation implements ByteArrayConverter, StructConverter {return r_offset;}- /**- * Sets the relocation offset to the new specified value.- * @param offset the new offset value- */- public void setOffset(int offset) {- this.r_offset = offset & Conv.INT_MASK;- }-- /**- * Sets the relocation offset to the new specified value.- * @param offset the new offset value- */- public void setOffset(long offset) {- this.r_offset = offset;- }-/*** Returns the symbol index where the relocation must be made.* A value of 0 is generally returned when no symbol is relavent@@ -299,7 +285,7 @@ public class ElfRelocation implements ByteArrayConverter, StructConverter {* @return type of relocation to apply*/public int getType() {- return (int) (is32bit ? (r_info & Conv.BYTE_MASK) : (r_info & Conv.INT_MASK));+ return (int) (is32bit ? (r_info & BYTE_MASK) : (r_info & INT_MASK));}/**@@ -308,10 +294,10 @@ public class ElfRelocation implements ByteArrayConverter, StructConverter {* @param type relocation type to be applied*/public void setType(long type) {- long mask = is32bit ? Conv.BYTE_MASK : Conv.INT_MASK;+ long mask = is32bit ? BYTE_MASK : INT_MASK;r_info = (r_info & ~mask) + (type & mask);}-+/*** Returns the r_info relocation entry field value* @return r_info value@@ -367,23 +353,6 @@ public class ElfRelocation implements ByteArrayConverter, StructConverter {return hasAddend ? 12 : 8;}- /**- * @see ghidra.app.util.bin.ByteArrayConverter#toBytes(ghidra.util.DataConverter)- */- @Override- public byte[] toBytes(DataConverter dc) {- byte[] bytes = new byte[sizeof()];- if (is32bit) {- dc.putInt(bytes, 0, (int) r_offset);- dc.putInt(bytes, 4, (int) r_info);- }- else {- dc.putLong(bytes, 0, r_offset);- dc.putLong(bytes, 8, r_info);- }- return bytes;- }-@Overridepublic String toString() {String str = "Offset: 0x" + Long.toHexString(getOffset()) + " - Type: 0x" +diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfRelocationTable.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfRelocationTable.javaindex c218cc628b..c43ef482f5 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfRelocationTable.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfRelocationTable.java@@ -20,17 +20,15 @@ import java.util.ArrayList;import java.util.List;import ghidra.app.util.bin.BinaryReader;-import ghidra.app.util.bin.ByteArrayConverter;import ghidra.app.util.bin.format.dwarf4.LEB128;import ghidra.program.model.data.ArrayDataType;import ghidra.program.model.data.DataType;-import ghidra.util.DataConverter;import ghidra.util.Msg;/*** A container class to hold ELF relocations.*/-public class ElfRelocationTable implements ElfFileSection, ByteArrayConverter {+public class ElfRelocationTable implements ElfFileSection {public enum TableFormat {DEFAULT, ANDROID, RELR;@@ -311,18 +309,6 @@ public class ElfRelocationTable implements ElfFileSection, ByteArrayConverter {return symbolTable;}- @Override- public byte[] toBytes(DataConverter dc) {- byte[] bytes = new byte[relocs.length * relocs[0].sizeof()];- int index = 0;- for (ElfRelocation reloc : relocs) {- byte[] relocBytes = reloc.toBytes(dc);- System.arraycopy(relocBytes, 0, bytes, index, relocBytes.length);- index += relocBytes.length;- }- return bytes;- }-@Overridepublic long getLength() {return length;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSectionHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSectionHeader.javaindex 1c92949ca6..f537bab9b0 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSectionHeader.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSectionHeader.java@@ -15,18 +15,16 @@*/package ghidra.app.util.bin.format.elf;+import java.io.IOException;+import java.io.InputStream;import java.util.HashMap;-import java.io.*;-import ghidra.app.util.bin.BinaryReader;import ghidra.app.util.bin.StructConverter;import ghidra.app.util.bin.format.MemoryLoadable;-import ghidra.app.util.bin.format.Writeable;import ghidra.program.model.data.*;import ghidra.program.model.mem.MemoryAccessException;import ghidra.program.model.mem.MemoryBlock;-import ghidra.util.DataConverter;import ghidra.util.StringUtilities;/**@@ -70,7 +68,7 @@ import ghidra.util.StringUtilities;* </pre>*/-public class ElfSectionHeader implements StructConverter, Writeable, MemoryLoadable {+public class ElfSectionHeader implements StructConverter, MemoryLoadable {private int sh_name;private int sh_type;@@ -184,40 +182,6 @@ public class ElfSectionHeader implements StructConverter, Writeable, MemoryLoadareturn header;}- /**- * @see ghidra.app.util.bin.format.Writeable#write(java.io.RandomAccessFile, ghidra.util.DataConverter)- */- @Override- public void write(RandomAccessFile raf, DataConverter dc) throws IOException {- raf.write(dc.getBytes(sh_name));- raf.write(dc.getBytes(sh_type));-- if (header.is32Bit()) {- raf.write(dc.getBytes((int) sh_flags));- raf.write(dc.getBytes((int) sh_addr));- raf.write(dc.getBytes((int) sh_offset));- raf.write(dc.getBytes((int) sh_size));- }- else if (header.is64Bit()) {- raf.write(dc.getBytes(sh_flags));- raf.write(dc.getBytes(sh_addr));- raf.write(dc.getBytes(sh_offset));- raf.write(dc.getBytes(sh_size));- }-- raf.write(dc.getBytes(sh_link));- raf.write(dc.getBytes(sh_info));-- if (header.is32Bit()) {- raf.write(dc.getBytes((int) sh_addralign));- raf.write(dc.getBytes((int) sh_entsize));- }- else if (header.is64Bit()) {- raf.write(dc.getBytes(sh_addralign));- raf.write(dc.getBytes(sh_entsize));- }- }-/*** If the section will appear in the memory image of a process, this* member gives the address at which the section's first byte@@ -407,15 +371,6 @@ public class ElfSectionHeader implements StructConverter, Writeable, MemoryLoada(header.is32Bit() && sh_offset == ElfConstants.ELF32_INVALID_OFFSET);}- /**- * Sets the section's size.- * @param size the new size of the section- */- public void setSize(long size) {- this.sh_size = size;- checkSize();- }-/*** This member gives the section's size in bytes. Unless the section type is* SHT_NOBITS, the section occupies sh_size bytes in the file. A section of type@@ -498,28 +453,6 @@ public class ElfSectionHeader implements StructConverter, Writeable, MemoryLoadareturn reader;}- /**- * Sets the actual data bytes for this section.- * If the data is larger than the previous data, then- * the offset is set to -1 and the section will- * need to be relocated.- * @param data the new data byte for this section- */- public void setData(byte[] data) {- bytesChanged = true;- if (sh_type == ElfSectionHeaderConstants.SHT_NOBITS) {- throw new IllegalArgumentException("Cannot set data on section with type: SHT_NOBITS");- }- this.data = data;- //if the data has been increased, then this section- //will need to be relocated in the file- if (data.length > sh_size) {- modified = true;- sh_offset = -1;- }- sh_size = data.length;- }-/*** Returns true if the data bytes have changed for this section.* @return true if the data bytes have changed for this section@@ -538,21 +471,6 @@ public class ElfSectionHeader implements StructConverter, Writeable, MemoryLoadareturn modified;}- /**- * Sets the offset of this section. The offset is the actual byte- * offset into the file.- * @param offset the file byte offset- * @throws IOException if an I/O occurs- */- public void setOffset(long offset) throws IOException {- modified = true;- /*if we are overriding the offset, we must cache the section data*/- if (data == null) {- data = getData();- }- this.sh_offset = offset;- }-/*** Sets the start address of this section.* @param addr the new start address of this section@@ -564,15 +482,7 @@ public class ElfSectionHeader implements StructConverter, Writeable, MemoryLoada}this.sh_addr = header.unadjustAddressForPrelink(addr);}-- /**- * Sets the name of this section (may get changed due to conflict)- * @param name section name- */- public void setName(String name) {- this.name = name;- }-+/*** @see ghidra.app.util.bin.StructConverter#toDataType()*/@@ -629,13 +539,6 @@ public class ElfSectionHeader implements StructConverter, Writeable, MemoryLoadareturn typeEnum;}- private void checkSize() {- if (sh_size > Integer.toUnsignedLong(Integer.MAX_VALUE)) {- throw new UnsupportedOperationException(- "ELF Section is too large: 0x" + Long.toHexString(sh_size));- }- }-@Overridepublic int hashCode() {return (int) ((17 * sh_offset) + (sh_offset >>> 32));diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSectionHeaderType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSectionHeaderType.javaindex a72d46bf06..9b4fe8bac8 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSectionHeaderType.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSectionHeaderType.java@@ -113,7 +113,7 @@ public class ElfSectionHeaderType {/*** Add the specified section header type to the specified map.* @param type section header type- * @param sectionHeaderTypeMap+ * @param sectionHeaderTypeMap map of section header types* @throws DuplicateNameException if new type name already defined within* the specified map*/diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSymbol.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSymbol.javaindex c241656708..9044d6327a 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSymbol.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSymbol.java@@ -20,9 +20,6 @@ import java.io.IOException;import org.apache.commons.lang3.StringUtils;import ghidra.app.util.bin.BinaryReader;-import ghidra.app.util.bin.ByteArrayConverter;-import ghidra.util.DataConverter;-import ghidra.util.exception.NotFoundException;/*** A class to represent the ELF 32bit and 64bit Symbol data structures.@@ -48,7 +45,7 @@ import ghidra.util.exception.NotFoundException;** </pre>*/-public class ElfSymbol implements ByteArrayConverter {+public class ElfSymbol {public static final String FORMATTED_NO_NAME = "<no name>";@@ -94,7 +91,6 @@ public class ElfSymbol implements ByteArrayConverter {/**Not preemptible, not exported*/public static final byte STV_PROTECTED = 3;- private ElfHeader header;private ElfSymbolTable symbolTable;private int symbolTableIndex;@@ -108,61 +104,10 @@ public class ElfSymbol implements ByteArrayConverter {private String nameAsString;/**- * Creates a new section symbol.- * @param header the corresponding ELF header- * @param sectionAddress the start address of the section- * @param sectionHeaderIndex the index of the section in the section header table- * @param name the string name of the section- * @param symbolIndex index of symbol within corresponding symbol table- * @param symbolTable symbol table- * @return the new section symbol+ * Construct a new special null symbol which corresponds to symbol index 0.*/- public static ElfSymbol createSectionSymbol32(ElfHeader header, long sectionAddress,- short sectionHeaderIndex, String name, int symbolIndex, ElfSymbolTable symbolTable) {- return new ElfSymbol(header, name, 0, sectionAddress, 0, STT_SECTION, (byte) 0,- sectionHeaderIndex, symbolIndex, symbolTable);- }-- /**- * Creates a new global function symbol.- * @param header the corresponding ELF header- * @param name the byte index of the name- * @param nameAsString the string name of the section- * @param addr the address of the function- * @param symbolIndex index of symbol within corresponding symbol table- * @param symbolTable symbol table- * @return the new global function symbol- */- public static ElfSymbol createGlobalFunctionSymbol(ElfHeader header, int name,- String nameAsString, long addr, int symbolIndex, ElfSymbolTable symbolTable) {- return new ElfSymbol(header, nameAsString, name, addr, 0,- (byte) ((STB_GLOBAL << 4) | STT_FUNC), (byte) 0, (short) 0, symbolIndex, symbolTable);- }-- /**- * Creates a new special null symbol which corresponds to symbol index 0.- * @param header the corresponding ELF header- * @return the new null symbol- */- public static ElfSymbol createNullSymbol(ElfHeader header) {- return new ElfSymbol(header, "", 0, 0, 0, (byte) 0, (byte) 0, (short) 0, 0, null);- }-- private ElfSymbol(ElfHeader header, String nameAsString, int name, long value, long size,- byte info, byte other, short sectionHeaderIndex, int symbolIndex,- ElfSymbolTable symbolTable) {- this.header = header;- this.nameAsString = nameAsString;-- this.st_name = name;- this.st_value = value;- this.st_size = size;- this.st_info = info;- this.st_other = other;- this.st_shndx = sectionHeaderIndex;-- this.symbolTable = symbolTable;- this.symbolTableIndex = symbolIndex;+ public ElfSymbol() {+ this.nameAsString = "";}/**@@ -177,7 +122,6 @@ public class ElfSymbol implements ByteArrayConverter {*/public ElfSymbol(BinaryReader reader, int symbolIndex,ElfSymbolTable symbolTable, ElfHeader header) throws IOException {- this.header = header;this.symbolTable = symbolTable;this.symbolTableIndex = symbolIndex;@@ -552,50 +496,4 @@ public class ElfSymbol implements ByteArrayConverter {" - " + "st_shndx: 0x" + Integer.toHexString(Short.toUnsignedInt(st_shndx));}- /**- * @see ghidra.app.util.bin.ByteArrayConverter#toBytes(ghidra.util.DataConverter)- */- @Override- public byte[] toBytes(DataConverter dc) {- // FIXME! BUG!! Symbols can exist without a dynamic table !!- ElfDynamicTable dynamic = header.getDynamicTable();- int syment = 0;- try {- syment = (int) dynamic.getDynamicValue(ElfDynamicType.DT_SYMENT);- }- catch (NotFoundException e) {- throw new RuntimeException(e);//should never happen!- }- byte[] bytes = new byte[syment];- int index = 0;- dc.putInt(bytes, 0, st_name);- index += 4;- if (header.is32Bit()) {- dc.putInt(bytes, index, (int) st_value);- index += 4;- dc.putInt(bytes, index, (int) st_size);- index += 4;- }- else {- dc.putLong(bytes, index, st_value);- index += 8;- dc.putLong(bytes, index, st_size);- index += 8;- }- bytes[index] = st_info;- index += 1;- bytes[index] = st_other;- index += 1;- dc.putShort(bytes, index, st_shndx);- return bytes;- }-- /**- * Sets the value of this symbol. The value is generally an address.- * @param value the new value of the symbol- */- public void setValue(long value) {- this.st_value = (int) value;- }-}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSymbolTable.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSymbolTable.javaindex 38c591cba4..f2c2269af1 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSymbolTable.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfSymbolTable.java@@ -21,15 +21,13 @@ import java.util.List;import java.util.stream.Collectors;import ghidra.app.util.bin.BinaryReader;-import ghidra.app.util.bin.ByteArrayConverter;import ghidra.program.model.data.*;-import ghidra.util.DataConverter;import ghidra.util.exception.DuplicateNameException;/*** A container class to hold ELF symbols.*/-public class ElfSymbolTable implements ElfFileSection, ByteArrayConverter {+public class ElfSymbolTable implements ElfFileSection {private ElfStringTable stringTable;private ElfSectionHeader symbolTableSection; // may be null@@ -260,39 +258,6 @@ public class ElfSymbolTable implements ElfFileSection, ByteArrayConverter {return files;}- /**- * Adds the specified symbol into this symbol table.- * @param symbol the new symbol to add- */- public void addSymbol(ElfSymbol symbol) {- ElfSymbol[] tmp = new ElfSymbol[symbols.length + 1];- System.arraycopy(symbols, 0, tmp, 0, symbols.length);- tmp[tmp.length - 1] = symbol;- symbols = tmp;- }-- /**- * @see ghidra.app.util.bin.ByteArrayConverter#toBytes(ghidra.util.DataConverter)- */- @Override- public byte[] toBytes(DataConverter dc) {- byte[] bytes = null;- int index = 0;- for (int i = 0; i < symbols.length; i++) {- byte[] symbytes = symbols[i].toBytes(dc);-- //all symbols are the same size, use the first one to determine the- //total number of bytes- if (i == 0) {- bytes = new byte[symbols.length * symbytes.length];- }-- System.arraycopy(symbytes, 0, bytes, index, symbytes.length);- index += symbytes.length;- }- return bytes;- }-@Overridepublic long getLength() {return length;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/relocation/ElfRelocationContext.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/relocation/ElfRelocationContext.javaindex e8e362d73a..ac86d5c57f 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/relocation/ElfRelocationContext.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/relocation/ElfRelocationContext.java@@ -69,7 +69,7 @@ public class ElfRelocationContext {this.relocationTable = relocTable;symbolTable = relocTable.getAssociatedSymbolTable();if (symbolTable == null) {- nullSymbol = ElfSymbol.createNullSymbol(loadHelper.getElfHeader());+ nullSymbol = new ElfSymbol();}}