Ghidra is a software reverse engineering (SRE) framework
GT-2933 Improved ELF loader ability to handle missing section names and negative file offsets
ghidra178a551b6d8eb
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 7da4f9def5..eb3b42114a 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@@ -384,6 +384,10 @@ public class ElfHeader implements StructConverter, Writeable {" linked to symbol table section " + symbolTableSection.getNameAsString() +" affecting " + relocaBaseName);+ if (section.getOffset() < 0) {+ return;+ }+relocationTableList.add(ElfRelocationTable.createElfRelocationTable(reader, this,section, section.getOffset(), section.getAddress(), section.getSize(),section.getEntrySize(), addendTypeReloc, symbolTable, sectionToBeRelocated));@@ -447,6 +451,9 @@ public class ElfHeader implements StructConverter, Writeable {Long.toHexString(relocTableAddr));return;}+ if (relocTableLoadHeader.getOffset() < 0) {+ return;+ }if (dynamicSymbolTable == null) {Msg.warn(this, "Failed to process " + relocTableAddrType.name +@@ -649,6 +656,9 @@ public class ElfHeader implements StructConverter, Writeable {if (sectionHeaders[i].getType() == ElfSectionHeaderConstants.SHT_SYMTAB ||sectionHeaders[i].getType() == ElfSectionHeaderConstants.SHT_DYNSYM) {ElfSectionHeader symbolTableSectionHeader = sectionHeaders[i];+ if (symbolTableSectionHeader.getOffset() < 0) {+ continue;+ }ElfSectionHeader stringTableSectionHeader =sectionHeaders[symbolTableSectionHeader.getLink()];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 24b18236a4..56cd368e6c 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@@ -64,7 +64,7 @@ import ghidra.util.*;*/public class ElfProgramHeaderimplements StructConverter, Comparable<ElfProgramHeader>, Writeable, MemoryLoadable {-+protected ElfHeader header;private int p_type;@@ -98,24 +98,24 @@ public class ElfProgramHeaderthis.reader = reader;if (header.is32Bit()) {- this.p_type = reader.readNextInt();- this.p_offset = reader.readNextInt() & Conv.INT_MASK;- this.p_vaddr = reader.readNextInt() & Conv.INT_MASK;- this.p_paddr = reader.readNextInt() & Conv.INT_MASK;- this.p_filesz = reader.readNextInt() & Conv.INT_MASK;- this.p_memsz = reader.readNextInt() & Conv.INT_MASK;- this.p_flags = reader.readNextInt();- this.p_align = reader.readNextInt() & Conv.INT_MASK;+ p_type = reader.readNextInt();+ p_offset = reader.readNextInt() & Conv.INT_MASK;+ p_vaddr = reader.readNextInt() & Conv.INT_MASK;+ p_paddr = reader.readNextInt() & Conv.INT_MASK;+ p_filesz = reader.readNextInt() & Conv.INT_MASK;+ p_memsz = reader.readNextInt() & Conv.INT_MASK;+ p_flags = reader.readNextInt();+ p_align = reader.readNextInt() & Conv.INT_MASK;}else if (header.is64Bit()) {- this.p_type = reader.readNextInt();- this.p_flags = reader.readNextInt();- this.p_offset = reader.readNextLong();- this.p_vaddr = reader.readNextLong();- this.p_paddr = reader.readNextLong();- this.p_filesz = reader.readNextLong();- this.p_memsz = reader.readNextLong();- this.p_align = reader.readNextLong();+ p_type = reader.readNextInt();+ p_flags = reader.readNextInt();+ p_offset = reader.readNextLong();+ p_vaddr = reader.readNextLong();+ p_paddr = reader.readNextLong();+ p_filesz = reader.readNextLong();+ p_memsz = reader.readNextLong();+ p_align = reader.readNextLong();}if (p_memsz > p_filesz) {@@ -134,15 +134,15 @@ public class ElfProgramHeader*/public ElfProgramHeader(ElfHeader header, int type) {this.header = header;- this.p_type = type;- this.p_flags =- ElfProgramHeaderConstants.PF_R | ElfProgramHeaderConstants.PF_W |- ElfProgramHeaderConstants.PF_X;- this.p_align = 0x1000;- this.p_paddr = 0xffffffff;- this.p_vaddr = 0xffffffff;- }-++ p_type = type;+ p_flags = ElfProgramHeaderConstants.PF_R | ElfProgramHeaderConstants.PF_W |+ ElfProgramHeaderConstants.PF_X;+ p_align = 0x1000;+ p_paddr = 0xffffffff;+ p_vaddr = 0xffffffff;+ }+/*** Return ElfHeader associated with this program header* @return ElfHeader@@ -286,7 +286,7 @@ public class ElfProgramHeaderpublic long getMemorySize() {return p_memsz;}-+/*** Get the adjusted memory size in bytes of the memory block which relates to this program header; it may be zero* if no block should be created. The returned value reflects any adjustment the ElfExtension may require@@ -297,7 +297,7 @@ public class ElfProgramHeaderpublic long getAdjustedMemorySize() {return header.getLoadAdapter().getAdjustedMemorySize(this);}-+/*** Get the adjusted file load size (i.e., filtered load size) to be loaded into memory block which relates to* this program header; it may be zero if no block should be created. The returned value reflects any adjustment@@ -325,7 +325,7 @@ public class ElfProgramHeaderpublic long getOffset() {return p_offset;}-+/*** Compute the file offset associated with the specified loaded virtual address* defined by this PT_LOAD program header. This can be useful when attempting to locate@@ -346,7 +346,7 @@ public class ElfProgramHeaderlong addressableUnitSize = p_filesz / p_memsz; // FIXME: verify unit size computation approach !return (addressableUnitSize * (virtualAddress - getVirtualAddress())) + p_offset;}-+/*** Set the offset. This value is the byte offset into* the ELF file.@@ -497,8 +497,7 @@ public class ElfProgramHeader}ElfProgramHeader other = (ElfProgramHeader) obj;return reader == other.reader && p_type == other.p_type && p_flags == other.p_flags &&- p_offset == other.p_offset &&- p_vaddr == other.p_vaddr && p_paddr == other.p_paddr && p_filesz == other.p_filesz &&- p_memsz == other.p_memsz && p_align == other.p_align;+ p_offset == other.p_offset && p_vaddr == other.p_vaddr && p_paddr == other.p_paddr &&+ p_filesz == other.p_filesz && p_memsz == other.p_memsz && p_align == other.p_align;}}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 3211c4431c..55e337a512 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@@ -107,57 +107,58 @@ public class ElfSectionHeader implements StructConverter, Writeable, MemoryLoadathis.reader = reader;this.header = header;- this.sh_name = reader.readNextInt();- this.sh_type = reader.readNextInt();+ sh_name = reader.readNextInt();+ sh_type = reader.readNextInt();if (header.is32Bit()) {- this.sh_flags = reader.readNextInt() & Conv.INT_MASK;- this.sh_addr = reader.readNextInt() & Conv.INT_MASK;- this.sh_offset = reader.readNextInt() & Conv.INT_MASK;- this.sh_size = reader.readNextInt() & Conv.INT_MASK;+ sh_flags = reader.readNextInt() & Conv.INT_MASK;+ sh_addr = reader.readNextInt() & Conv.INT_MASK;+ sh_offset = reader.readNextInt() & Conv.INT_MASK;+ sh_size = reader.readNextInt() & Conv.INT_MASK;}else if (header.is64Bit()) {- this.sh_flags = reader.readNextLong();- this.sh_addr = reader.readNextLong();- this.sh_offset = reader.readNextLong();- this.sh_size = reader.readNextLong();+ sh_flags = reader.readNextLong();+ sh_addr = reader.readNextLong();+ sh_offset = reader.readNextLong();+ sh_size = reader.readNextLong();}- this.sh_link = reader.readNextInt();- this.sh_info = reader.readNextInt();+ sh_link = reader.readNextInt();+ sh_info = reader.readNextInt();if (header.is32Bit()) {- this.sh_addralign = reader.readNextInt() & Conv.INT_MASK;- this.sh_entsize = reader.readNextInt() & Conv.INT_MASK;+ sh_addralign = reader.readNextInt() & Conv.INT_MASK;+ sh_entsize = reader.readNextInt() & Conv.INT_MASK;}else if (header.is64Bit()) {- this.sh_addralign = reader.readNextLong();- this.sh_entsize = reader.readNextLong();+ sh_addralign = reader.readNextLong();+ sh_entsize = reader.readNextLong();}-//checkSize();}ElfSectionHeader(ElfHeader header, MemoryBlock block, int sh_name, long imageBase)throws MemoryAccessException {+this.header = header;this.sh_name = sh_name;+if (block.isInitialized()) {- this.sh_type = ElfSectionHeaderConstants.SHT_PROGBITS;+ sh_type = ElfSectionHeaderConstants.SHT_PROGBITS;}else {- this.sh_type = ElfSectionHeaderConstants.SHT_NOBITS;+ sh_type = ElfSectionHeaderConstants.SHT_NOBITS;}- this.sh_flags = ElfSectionHeaderConstants.SHF_ALLOC | ElfSectionHeaderConstants.SHF_WRITE |+ sh_flags = ElfSectionHeaderConstants.SHF_ALLOC | ElfSectionHeaderConstants.SHF_WRITE |ElfSectionHeaderConstants.SHF_EXECINSTR;- this.sh_addr = block.getStart().getOffset();- this.sh_offset = block.getStart().getAddressableWordOffset() - imageBase;- this.sh_size = block.getSize();- this.sh_link = 0;- this.sh_info = 0;- this.sh_addralign = 0;- this.sh_entsize = 0;- this.name = block.getName();+ sh_addr = block.getStart().getOffset();+ sh_offset = block.getStart().getAddressableWordOffset() - imageBase;+ sh_size = block.getSize();+ sh_link = 0;+ sh_info = 0;+ sh_addralign = 0;+ sh_entsize = 0;+ name = block.getName();data = new byte[(int) sh_size];if (block.isInitialized()) {@@ -169,21 +170,23 @@ public class ElfSectionHeader implements StructConverter, Writeable, MemoryLoadaElfSectionHeader(ElfHeader header, String name, int sh_name, int type) {this.header = header;+ this.name = name;this.sh_name = sh_name;this.sh_type = type;- this.sh_flags = ElfSectionHeaderConstants.SHF_ALLOC | ElfSectionHeaderConstants.SHF_WRITE |++ sh_flags = ElfSectionHeaderConstants.SHF_ALLOC | ElfSectionHeaderConstants.SHF_WRITE |ElfSectionHeaderConstants.SHF_EXECINSTR;- this.sh_link = 0;- this.sh_info = 0;- this.sh_addralign = 0;- this.sh_entsize = 0;- this.name = name;- this.data = new byte[0];- this.sh_size = 0;- this.sh_addr = -1;- this.sh_offset = -1;+ sh_link = 0;+ sh_info = 0;+ sh_addralign = 0;+ sh_entsize = 0;++ data = new byte[0];+ sh_size = 0;+ sh_addr = -1;+ sh_offset = -1;}-+/*** Return ElfHeader associated with this section* @return ElfHeader@@ -337,38 +340,39 @@ public class ElfSectionHeader implements StructConverter, Writeable, MemoryLoada}void updateName() throws IOException {- if (sh_name == -1) {- return;+ if (reader == null) {+ throw new UnsupportedOperationException("This ElfSectionHeader does not have a reader");}+ElfSectionHeader[] sections = header.getSections();short e_shstrndx = header.e_shstrndx();- if (e_shstrndx >= sections.length) {- int index = 0;+ name = null;+ try {+ if (sh_name >= 0 && e_shstrndx >= 0 && e_shstrndx < sections.length) {+ ElfSectionHeader stringTableSectionHeader = sections[e_shstrndx];+ if (sh_name < stringTableSectionHeader.sh_size) {+ // read section name from string table+ long stringTableOffset = sections[e_shstrndx].getOffset();+ if (stringTableOffset >= 0) {+ name = reader.readAsciiString(stringTableOffset + sh_name);+ if ("".equals(name)) {+ name = null;+ }+ }+ }+ }+ }+ catch (IOException e) {+ // ignore+ }+ if (name == null) {+ name = "NO-NAME";for (int i = 0; i < sections.length; ++i) {//find this section's indexif (sections[i] == this) {- index = i;+ name = "SECTION" + i;break;}}- name = "SECTION" + index;- }- else {- if (reader == null) {- throw new UnsupportedOperationException(- "This ElfSectionHeader does not have a reader");- }- long stringTableOffset = sections[e_shstrndx].getOffset();- if (stringTableOffset + sh_name < 0) {- Msg.error(this, "Invalid name offset " + (stringTableOffset + sh_name));- return;- }- name = reader.readAsciiString(stringTableOffset + sh_name);- if ("".equals(name)) {- name = "EMPTYNAME_" + String.format("0x%x", sh_offset);- }- if (sh_type == ElfSectionHeaderConstants.SHT_SYMTAB_SHNDX) {- Msg.debug(this, "sh_type==SHT_SYMTAB_SHNDX");- }}}@@ -422,7 +426,7 @@ public class ElfSectionHeader implements StructConverter, Writeable, MemoryLoadapublic long getSize() {return sh_size;}-+/*** Get the adjusted size of the section in bytes (i.e., memory block) which relates to this section header; it may be zero* if no block should be created. The returned value reflects any adjustment the ElfExtension may require@@ -556,8 +560,8 @@ public class ElfSectionHeader implements StructConverter, Writeable, MemoryLoada*/public void setAddress(long addr) {if (!header.isRelocatable() && sh_addr == 0) {- throw new RuntimeException("Attempting to place non-loaded section into memory :" +- name);+ throw new RuntimeException(+ "Attempting to place non-loaded section into memory :" + name);}this.sh_addr = header.unadjustAddressForPrelink(addr);}@@ -628,8 +632,8 @@ public class ElfSectionHeader implements StructConverter, Writeable, MemoryLoadaprivate void checkSize() {if (sh_size > (Integer.MAX_VALUE & Conv.INT_MASK)) {- throw new UnsupportedOperationException("ELF Section is too large: 0x" +- Long.toHexString(sh_size));+ throw new UnsupportedOperationException(+ "ELF Section is too large: 0x" + Long.toHexString(sh_size));}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfStringTable.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfStringTable.javaindex da70f23cd7..90732fd576 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfStringTable.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfStringTable.java@@ -45,8 +45,7 @@ public class ElfStringTable implements ElfFileSection {ElfStringTable elfStringTable =(ElfStringTable) reader.getFactory().create(ElfStringTable.class);elfStringTable.initElfStringTable(reader, header, stringTableSection, fileOffset,- addrOffset,- length);+ addrOffset, length);return elfStringTable;}@@ -81,6 +80,9 @@ public class ElfStringTable implements ElfFileSection {* @return string or null on error*/public String readString(BinaryReader reader, long stringOffset) {+ if (fileOffset < 0) {+ return null;+ }try {if (stringOffset >= length) {throw new IOException("String read beyond table bounds");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 055a088ccd..42279d6e88 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@@ -107,7 +107,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {elf.parse();monitor.setCancelEnabled(true);- int id = program.startTransaction("loading program from ELF");+ int id = program.startTransaction("Load ELF program");boolean success = false;try {@@ -116,16 +116,17 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {setImageBase();program.setExecutableFormat(ElfLoader.ELF_NAME);- // process headers and define "section" within memory elfProgramBuilder- processProgramHeaders(monitor);- processSectionHeaders(monitor);-// resolve segment/sections and create program memory blocksByteProvider byteProvider = elf.getReader().getByteProvider();try (InputStream fileIn = byteProvider.getInputStream(0)) {fileBytes = program.getMemory().createFileBytes(byteProvider.getName(), 0,byteProvider.length(), fileIn);}++ // process headers and define "section" within memory elfProgramBuilder+ processProgramHeaders(monitor);+ processSectionHeaders(monitor);+resolve(monitor);if (elf.e_shnum() == 0) {@@ -558,12 +559,20 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {createOneByteFunction(name, entryAddress, true);}catch (Exception e) {- log("Could not create symbol at entry point: " + e);+ log("Could not create symbol at entry point: " + getMessage(e));}return entryAddress;}+ private String getMessage(Exception e) {+ String msg = e.getMessage();+ if (msg == null) {+ msg = e.toString();+ }+ return msg;+ }+private void markupInterpreter(TaskMonitor monitor) {if (monitor.isCancelled()) {return;@@ -748,7 +757,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {if (type != 0) { // ignore if type 0 which is always NONE (no relocation performed)log("Unable to perform relocation: Type = " + type + " (0x" +Long.toHexString(type) + ") at " + relocAddr + " (Symbol = " + symbolName +- ") - " + e.getMessage());+ ") - " + getMessage(e));}}finally {@@ -808,7 +817,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {createData(headerAddr, dt);}catch (Exception e) {- log("Failed to markup Elf header: " + e.getMessage());+ log("Failed to markup Elf header: " + getMessage(e));}}@@ -866,7 +875,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {}catch (Exception e) {e.printStackTrace();- log("Failed to markup Elf program/segment headers: " + e.getMessage());+ log("Failed to markup Elf program/segment headers: " + getMessage(e));}}@@ -923,7 +932,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {}}catch (Exception e) {- log("Failed to markup Elf section headers: " + e.getMessage());+ log("Failed to markup Elf section headers: " + getMessage(e));}}@@ -932,11 +941,8 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {try {listing.createData(relocTableAddr, relocTable.toDataType());}- catch (CodeUnitInsertionException e) {- log("Failed to properly markup relocation table: " + e.getMessage());- }catch (Exception e) {- log("Failed to properly markup relocation table: " + e);+ log("Failed to properly markup relocation table: " + getMessage(e));}}@@ -1130,7 +1136,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {"NOTE: This block is artificial and is used to make relocations work correctly");}catch (Exception e) {- log("Error creating external memory block: " + " - " + e.getMessage());+ log("Error creating external memory block: " + " - " + getMessage(e));}}@@ -1219,7 +1225,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {}catch (Exception e) {log("Error creating symbol: " + elfSymbol.getNameAsString() + " - " +- e.getMessage());+ getMessage(e));}}}@@ -1602,7 +1608,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {}}catch (Exception e) {- log("Error while creating function at " + address + ": " + e.getMessage());+ log("Error while creating function at " + address + ": " + getMessage(e));}try {@@ -1614,7 +1620,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {}}catch (Exception e) {- log("Error while creating symbol " + name + " at " + address + ": " + e.getMessage());+ log("Error while creating symbol " + name + " at " + address + ": " + getMessage(e));}return function;}@@ -1641,7 +1647,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {SourceType.IMPORTED);}catch (InvalidInputException e) {- log.appendMsg("Failed to create external function '" + name + "': " + e.getMessage());+ log.appendMsg("Failed to create external function '" + name + "': " + getMessage(e));return null;}catch (DuplicateNameException e) {@@ -1674,7 +1680,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {}catch (Exception e) {log.appendMsg("Failed to establish linkage to external function '" + name + "': " +- e.getMessage());+ getMessage(e));return null;}// Create constant pointer@@ -1749,7 +1755,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {Msg.warn(this, "ELF data markup conflict at " + address);}catch (DataTypeConflictException e) {- Msg.error(this, "ELF data type markup conflict:" + e.getMessage());+ Msg.error(this, "ELF data type markup conflict:" + getMessage(e));}return null;}@@ -1873,7 +1879,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {d.setComment(CodeUnit.EOL_COMMENT, "Hash Table - chains");}catch (Exception e) {- log("Failed to properly markup Hash table at " + hashTableAddr + ": " + e);+ log("Failed to properly markup Hash table at " + hashTableAddr + ": " + getMessage(e));return;}@@ -1943,7 +1949,8 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {}catch (Exception e) {- log("Failed to properly markup GNU Hash table at " + hashTableAddr + ": " + e);+ log("Failed to properly markup GNU Hash table at " + hashTableAddr + ": " ++ getMessage(e));return;}@@ -1957,7 +1964,8 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {array = listing.createData(symbolTableAddr, symbolTable.toDataType());}catch (Exception e) {- log("Failed to properly markup symbol table at " + symbolTableAddr + ": " + e);+ log("Failed to properly markup symbol table at " + symbolTableAddr + ": " ++ getMessage(e));return;}@@ -2040,7 +2048,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {}}catch (Exception e) {- log("Failed to process dynamic section: " + e.getMessage());+ log("Failed to process dynamic section: " + getMessage(e));}}@@ -2306,6 +2314,9 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {continue;}long startOffset = section.getOffset();+ if (startOffset < 0) {+ continue;+ }long endOffset = startOffset + section.getSize() - 1;if (fileOffset >= startOffset && headerEndOffset <= endOffset) {headerAddr = findLoadAddress(section, fileOffset - section.getOffset());@@ -2321,6 +2332,9 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {continue;}long startOffset = segment.getOffset();+ if (startOffset < 0) {+ continue;+ }long endOffset = startOffset + segment.getFileSize() - 1;if (fileOffset >= startOffset && headerEndOffset <= endOffset) {headerAddr = findLoadAddress(segment, fileOffset - segment.getOffset());@@ -2464,22 +2478,32 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {if (elfProgramHeader.getType() == ElfProgramHeaderConstants.PT_NULL) {continue;}+ long fileOffset = elfProgramHeader.getOffset();if (elfProgramHeader.getType() != ElfProgramHeaderConstants.PT_LOAD) {if (!includeOtherBlocks) {continue;}- if (elf.getProgramLoadHeaderContainingFileOffset(- elfProgramHeader.getOffset()) != null) {+ if (fileOffset < 0 || fileOffset >= fileBytes.getSize()) {+ log("Skipping segment[" + i + ", " + elfProgramHeader.getDescription() ++ "] with invalid file offset");+ continue;+ }+ if (elf.getProgramLoadHeaderContainingFileOffset(fileOffset) != null) {continue;}- ElfSectionHeader section = elf.getSectionHeaderContainingFileRange(- elfProgramHeader.getOffset(), elfProgramHeader.getFileSize());+ ElfSectionHeader section = elf.getSectionHeaderContainingFileRange(fileOffset,+ elfProgramHeader.getFileSize());if (section != null) {log("Skipping segment[" + i + ", " + elfProgramHeader.getDescription() +"] included by section " + section.getNameAsString());continue;}}+ if (fileOffset < 0 || fileOffset >= fileBytes.getSize()) {+ log("Skipping PT_LOAD segment[" + i + ", " + elfProgramHeader.getDescription() ++ "] with invalid file offset");+ continue;+ }nextRelocStart = processProgramHeader(elfProgramHeader, nextRelocStart, i, monitor);}}@@ -2560,7 +2584,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {// be created which correspond to these areas.}catch (AddressOverflowException e) {- log(e.getMessage());+ log("Failed to load segment [" + segmentNumber + "]: " + getMessage(e));}return nextRelocStart;}@@ -2675,6 +2699,12 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {ElfSectionHeader elfSectionToLoad = sections[i];if (elfSectionToLoad.getType() != ElfSectionHeaderConstants.SHN_UNDEF &&(includeOtherBlocks || elfSectionToLoad.isAlloc())) {+ long fileOffset = elfSectionToLoad.getOffset();+ if (fileOffset < 0 || fileOffset >= fileBytes.getSize()) {+ log("Skipping section [" + elfSectionToLoad.getNameAsString() ++ "] with invalid file offset");+ continue;+ }nextRelocStart = processSectionHeader(elfSectionToLoad, i, nextRelocStart, monitor);}}@@ -2731,7 +2761,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {}if (sectionByteLength == 0) {- log("Skipping empty section[" + elfSectionToLoad.getNameAsString() + "]");+ log("Skipping empty section [" + elfSectionToLoad.getNameAsString() + "]");return relocStart;}@@ -2741,7 +2771,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {AddressSpace space = address.getAddressSpace();if (!space.isValidRange(address.getOffset(), sectionByteLength)) {- log("Skipping unloadable section[" + elfSectionToLoad.getNameAsString() ++ log("Skipping unloadable section [" + elfSectionToLoad.getNameAsString() +"] at address " + address.toString(true) + " (size=" + sectionByteLength + ")");return relocStart;}@@ -2772,7 +2802,8 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {}}catch (AddressOverflowException e) {- log(e.getMessage());+ log("Failed to load section [" + elfSectionToLoad.getNameAsString() + "]: " ++ getMessage(e));}return nextRelocStart;@@ -2888,7 +2919,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {int maxSectionSizeGBytes = Memory.MAX_BLOCK_SIZE_GB;long maxSectionSizeBytes = Memory.MAX_BLOCK_SIZE;- if (dataLength < 0 || dataLength > maxSectionSizeBytes) {+ if (dataLength > maxSectionSizeBytes) {float sizeGB = (float) dataLength / (float) Memory.GBYTE;String msg = "Truncating " + formatFloat(sizeGB, 1) + " GByte '" + sectionName +"' section to " + maxSectionSizeGBytes + " GByte fixed size limit";@@ -2908,7 +2939,6 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {// Are we immune from such errors? If not, how should they be handled?long revisedLength = checkBlockLimit(name, dataLength, true);- long sizeGB = revisedLength >> Memory.GBYTE_SHIFT_FACTOR;if (start.isNonLoadedMemoryAddress()) {r = false;@@ -2919,9 +2949,15 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {Msg.debug(this,"Loading block " + name + " at " + start + " from file offset " + fileOffset);+ long endOffset = fileOffset + revisedLength - 1;+ if (endOffset >= fileBytes.getSize()) {+ revisedLength = fileBytes.getSize() - fileOffset;+ log("Truncating block load for " + name + " which exceeds file length");+ }+String blockComment = comment;if (dataLength != revisedLength) {- blockComment += " (section truncated to " + sizeGB + " GByte)";+ blockComment += " (section truncated)";}if (elf.getLoadAdapter().hasFilteredLoadInputStream(this, loadable, start)) {@@ -2948,8 +2984,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {// Are we immune from such errors? If not, how should they be handled?long revisedLength = checkBlockLimit(name, dataLength, false);- long sizeGB = revisedLength >> Memory.GBYTE_SHIFT_FACTOR;-+if (start.isNonLoadedMemoryAddress()) {r = false;w = false;@@ -2957,7 +2992,7 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {}if (dataLength != revisedLength) {- comment += " (section truncated to " + sizeGB + " GByte)";+ comment += " (section truncated)";}return MemoryBlockUtils.createUninitializedBlock(program, isOverlay, name, start,diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MemorySection.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MemorySection.javaindex bc74068352..861944a6ea 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MemorySection.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MemorySection.java@@ -18,7 +18,7 @@ package ghidra.app.util.opinion;import ghidra.app.util.bin.format.MemoryLoadable;import ghidra.program.model.address.*;-public class MemorySection {+class MemorySection {protected final MemoryLoadable key;@@ -54,7 +54,7 @@ public class MemorySection {* @param isFragmentationOK if true this memory section may be fragmented due to* conflict/overlap with other memory sections of higher precedence.*/- protected MemorySection(MemoryLoadable key, boolean isInitialized, long fileOffset, long length,+ MemorySection(MemoryLoadable key, boolean isInitialized, long fileOffset, long length,AddressRange range, String sectionName, boolean isReadable, boolean isWritable,boolean isExecute, String comment, boolean isFragmentationOK) {AddressSpace space = range.getAddressSpace();@@ -73,7 +73,7 @@ public class MemorySection {this.comment = comment;this.isFragmentationOK = isFragmentationOK;}-+public MemoryLoadable getKey() {return key;}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MemorySectionResolver.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MemorySectionResolver.javaindex a93f804795..a991160e09 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MemorySectionResolver.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MemorySectionResolver.java@@ -115,6 +115,15 @@ public abstract class MemorySectionResolver {}private String getUniqueSectionName(String baseName) {+ if (baseName != null) {+ baseName = baseName.trim();+ if (baseName.length() == 0) {+ baseName = "NO-NAME";+ }+ }+ else {+ baseName = "NO-NAME";+ }Memory mem = program.getMemory();String name = baseName;int index = 0;