Package ghidra.app.util.bin.format.elf
Class ElfHeader
- java.lang.Object
-
- ghidra.app.util.bin.format.elf.ElfHeader
-
- All Implemented Interfaces:
Writeable
,StructConverter
public class ElfHeader extends java.lang.Object implements StructConverter, Writeable
A class to represent the Executable and Linking Format (ELF) header and specification.
-
-
Constructor Summary
Constructors Constructor Description ElfHeader()
DO NOT USE THIS CONSTRUCTOR, USE create*(GenericFactory ...) FACTORY METHODS INSTEAD.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addProgramHeader(ElfProgramHeader ph)
Appends the new program header to the end of the existing program header table.ElfSectionHeader
addSection(MemoryBlock block, int sh_name)
Adds a new section using the specified memory block.ElfSectionHeader
addSection(java.lang.String name, int sh_name)
Adds a new section the specifed name and name index.ElfSectionHeader
addSection(java.lang.String name, int sh_name, int type)
Adds a new section the specifed name and name index.long
adjustAddressForPrelink(long address)
Adjust address offset for certain pre-linked binaries which do not adjust certain header fields (e.g., dynamic table address entries).static ElfHeader
createElfHeader(GenericFactory factory, ByteProvider provider)
Constructs a new ELF header using the specified byte provider.short
e_ehsize()
This member holds the ELF header's size in bytes.long
e_entry()
This member gives the virtual address to which the system first transfers control, thus starting the process.int
e_flags()
This member holds processor-specific flags associated with the file.short
e_machine()
This member's value specifies the required architecture for an individual file.short
e_phentsize()
This member holds the size in bytes of one entry in the file's program header table; all entries are the same size.short
e_phnum()
This member holds the number of entries in the program header table.long
e_phoff()
This member holds the program header table's file offset in bytes.short
e_shentsize()
This member holds the section header's size in bytes.short
e_shnum()
This member holds the number of entries in the section header table.long
e_shoff()
This member holds the section header table's file offset in bytes.short
e_shstrndx()
This member holds the section header table index of the entry associated with the section name string table.short
e_type()
This member identifies the object file type; executable, shared object, etc.int
e_version()
This member identifies the object file version, where "EV_NONE == Invalid Version" and "EV_CURRENT == Current Version" The value 1 signifies the original file format; extensions will create new versions with higher numbers.long
findImageBase()
Inspect the Elf image and determine the default image base prior to theparse()
method being invoked (i.e., only the main Elf header structure has been parsed).java.lang.String[]
getDynamicLibraryNames()
Returns array of dynamic library names defined by DT_NEEDEDElfStringTable
getDynamicStringTable()
Returns the dynamic string table as defined in this ELF file.ElfSymbolTable
getDynamicSymbolTable()
Returns the dynamic symbol table as defined in this ELF file.ElfDynamicTable
getDynamicTable()
Returns the dynamic table defined by program header of type PT_DYNAMIC or the .dynamic program section.ElfDynamicType
getDynamicType(int type)
protected java.util.HashMap<java.lang.Integer,ElfDynamicType>
getDynamicTypeMap()
int
getEntryComponentOrdinal()
Get the Elf header structure component ordinal corresponding to the e_entry elementjava.lang.String
getFlags()
Returns a string representation of the numeric flags field.long
getImageBase()
Returns the image base of this ELF.ElfLoadAdapter
getLoadAdapter()
Get the installed extension provider.java.lang.String
getMachineName()
Returns a string name of the processor specified in this ELF header.int
getPhoffComponentOrdinal()
Get the Elf header structure component ordinal corresponding to the e_phoff elementElfProgramHeader
getProgramHeaderAt(long virtualAddr)
Returns the program header at the specified address, or null if no program header exists at that address.ElfProgramHeader
getProgramHeaderProgramHeader()
Returns the program header with type of PT_PHDR.ElfProgramHeader[]
getProgramHeaders()
Returns the program headers as defined in this ELF file.ElfProgramHeader[]
getProgramHeaders(int type)
Returns the program headers with the specified type.ElfProgramHeaderType
getProgramHeaderType(int type)
protected java.util.HashMap<java.lang.Integer,ElfProgramHeaderType>
getProgramHeaderTypeMap()
ElfProgramHeader
getProgramLoadHeaderContaining(long virtualAddr)
Returns the PT_LOAD program header which loads a range containing the specified address, or null if not found.ElfProgramHeader
getProgramLoadHeaderContainingFileOffset(long offset)
Returns the PT_LOAD program header which loads a range containing the specified file offset, or null if not found.BinaryReader
getReader()
Returns the binary reader.ElfRelocationTable
getRelocationTable(ElfSectionHeader relocSection)
Returns the relocation table associated to the specified section header.ElfRelocationTable[]
getRelocationTables()
Returns the relocation tables as defined in this ELF file.ElfSectionHeader
getSection(java.lang.String name)
Returns the section header with the specified name, or null if no section exists with that name.ElfSectionHeader
getSectionAt(long address)
Returns the section header at the specified address, or null if no section exists at that address.ElfSectionHeader
getSectionHeaderContainingFileRange(long fileOffset, long fileRangeLength)
Returns the section header which fully contains the specified file offset range.ElfSectionHeaderType
getSectionHeaderType(int type)
protected java.util.HashMap<java.lang.Integer,ElfSectionHeaderType>
getSectionHeaderTypeMap()
int
getSectionIndex(ElfSectionHeader section)
Returns the index of the specified section.ElfSectionHeader
getSectionLoadHeaderContaining(long address)
Returns the section header that loads/contains the specified address, or null if no section contains the address.ElfSectionHeader[]
getSections()
Returns the section headers as defined in this ELF file.ElfSectionHeader[]
getSections(int type)
Returns the section headers with the specified type.int
getShoffComponentOrdinal()
Get the Elf header structure component ordinal corresponding to the e_shoff elementElfStringTable
getStringTable(ElfSectionHeader section)
Returns the string table associated to the specified section header.ElfStringTable[]
getStringTables()
Returns the string tables as defined in this ELF file.ElfSymbolTable
getSymbolTable(ElfSectionHeader symbolTableSection)
Returns the symbol table associated to the specified section header.ElfSymbolTable[]
getSymbolTables()
Returns the symbol tables as defined in this ELF file.protected void
initElfHeader(GenericFactory factory, ByteProvider provider)
boolean
is32Bit()
Returns true if this ELF was created for a 32-bit processor.boolean
is64Bit()
Returns true if this ELF was created for a 64-bit processor.boolean
isBigEndian()
Returns true if this ELF was created for a big endian processor.boolean
isExecutable()
Returns true if this is an executable file.boolean
isLittleEndian()
Returns true if this ELF was created for a little endian processor.boolean
isPreLinked()
Determine if the image has been pre-linked.boolean
isRelocatable()
Returns true if this is a relocatable file.boolean
isSectionLoaded(ElfSectionHeader section)
boolean
isSharedObject()
Returns true if this is a shared object file.void
parse()
protected void
parseSectionHeaders()
void
setProgramHeaderOffset(long offset)
Sets the program header offset.void
setSectionHeaderOffset(long offset)
Sets the section header offset.DataType
toDataType()
Returns a structure datatype representing the contents of the implementor of this interface.long
unadjustAddressForPrelink(long address)
Unadjust address offset for certain pre-linked binaries which do not adjust certain header fields (e.g., dynamic table address entries).void
write(java.io.RandomAccessFile raf, DataConverter dc)
Writes this object to the specified random access file using the data converter to handle endianness.
-
-
-
Method Detail
-
createElfHeader
public static ElfHeader createElfHeader(GenericFactory factory, ByteProvider provider) throws ElfException
Constructs a new ELF header using the specified byte provider.- Parameters:
provider
- the byte provider to supply the bytes- Throws:
ElfException
- if the underlying bytes in the byte provider do not constitute a valid ELF.
-
getReader
public BinaryReader getReader()
Returns the binary reader.- Returns:
- the binary reader
-
initElfHeader
protected void initElfHeader(GenericFactory factory, ByteProvider provider) throws ElfException
- Throws:
ElfException
-
parse
public void parse() throws java.io.IOException
- Throws:
java.io.IOException
-
getLoadAdapter
public ElfLoadAdapter getLoadAdapter()
Get the installed extension provider. If the parse method has not yet been invoked, the default adapter will be returned.- Returns:
- ELF load adapter
-
adjustAddressForPrelink
public long adjustAddressForPrelink(long address)
Adjust address offset for certain pre-linked binaries which do not adjust certain header fields (e.g., dynamic table address entries). Standard GNU/Linux pre-linked shared libraries have adjusted header entries and this method should have no effect.- Parameters:
address
-- Returns:
- address with appropriate pre-link adjustment added
-
unadjustAddressForPrelink
public long unadjustAddressForPrelink(long address)
Unadjust address offset for certain pre-linked binaries which do not adjust certain header fields (e.g., dynamic table address entries). This may be needed when updating a header address field which requires pre-link adjustment.- Parameters:
address
-- Returns:
- address with appropriate pre-link adjustment subtracted
-
getProgramHeaderTypeMap
protected java.util.HashMap<java.lang.Integer,ElfProgramHeaderType> getProgramHeaderTypeMap()
-
getSectionHeaderTypeMap
protected java.util.HashMap<java.lang.Integer,ElfSectionHeaderType> getSectionHeaderTypeMap()
-
getProgramHeaderType
public ElfProgramHeaderType getProgramHeaderType(int type)
-
getSectionHeaderType
public ElfSectionHeaderType getSectionHeaderType(int type)
-
getDynamicTypeMap
protected java.util.HashMap<java.lang.Integer,ElfDynamicType> getDynamicTypeMap()
-
getDynamicType
public ElfDynamicType getDynamicType(int type)
-
parseSectionHeaders
protected void parseSectionHeaders() throws java.io.IOException
- Throws:
java.io.IOException
-
isBigEndian
public boolean isBigEndian()
Returns true if this ELF was created for a big endian processor.- Returns:
- true if this ELF was created for a big endian processor
-
isLittleEndian
public boolean isLittleEndian()
Returns true if this ELF was created for a little endian processor.- Returns:
- true if this ELF was created for a little endian processor
-
is32Bit
public boolean is32Bit()
Returns true if this ELF was created for a 32-bit processor.- Returns:
- true if this ELF was created for a 32-bit processor
-
is64Bit
public boolean is64Bit()
Returns true if this ELF was created for a 64-bit processor.- Returns:
- true if this ELF was created for a 64-bit processor
-
findImageBase
public long findImageBase()
Inspect the Elf image and determine the default image base prior to theparse()
method being invoked (i.e., only the main Elf header structure has been parsed). The image base is the virtual address of the PT_LOAD program header with the smallest address or 0 if no program headers exist. By default, the image base address should be treated as a addressable unit offset.- Returns:
- preferred image base
-
getImageBase
public long getImageBase()
Returns the image base of this ELF. The image base is the virtual address of the first PT_LOAD program header or 0 if no program headers. By default, the image base address should be treated as a addressable unit offset.s- Returns:
- the image base of this ELF
-
isPreLinked
public boolean isPreLinked()
Determine if the image has been pre-linked. NOTE: Currently has very limited support. Certain pre-link cases can not be detected until after a full parse has been performed.- Returns:
- true if image has been pre-linked
-
isSectionLoaded
public boolean isSectionLoaded(ElfSectionHeader section)
-
e_ehsize
public short e_ehsize()
This member holds the ELF header's size in bytes.- Returns:
- the ELF header's size in bytes
-
e_entry
public long e_entry()
This member gives the virtual address to which the system first transfers control, thus starting the process. If the file has no associated entry point, this member holds zero.- Returns:
- the virtual address to which the system first transfers control
-
e_flags
public int e_flags()
This member holds processor-specific flags associated with the file. Flag names take the form EF_machine_flag.- Returns:
- the processor-specific flags associated with the file
- See Also:
for flag definitions
-
e_machine
public short e_machine()
This member's value specifies the required architecture for an individual file.- Returns:
- the required architecture for an individual file
- See Also:
for machine definitions
-
e_phentsize
public short e_phentsize()
This member holds the size in bytes of one entry in the file's program header table; all entries are the same size.- Returns:
- the size in bytes of one program header table entry
-
e_phnum
public short e_phnum()
This member holds the number of entries in the program header table. Thus the product of e_phentsize and e_phnum gives the table's size in bytes. If a file has no program header table, e_phnum holds the value zero.- Returns:
- the number of entries in the program header table
-
e_phoff
public long e_phoff()
This member holds the program header table's file offset in bytes. If the file has no program header table, this member holds zero.- Returns:
- the program header table's file offset in bytes
-
e_shentsize
public short e_shentsize()
This member holds the section header's size in bytes. A section header is one entry in the section header table; all entries are the same size.- Returns:
- the section header's size in bytes
-
e_shnum
public short e_shnum()
This member holds the number of entries in the section header table. Thus the product of e_shentsize and e_shnum gives the section header table's size in bytes. If a file has no section header table, e_shnum holds the value zero.- Returns:
- the number of entries in the section header table
-
e_shoff
public long e_shoff()
This member holds the section header table's file offset in bytes. If the file has no section header table, this member holds zero.- Returns:
- the section header table's file offset in bytes
-
e_shstrndx
public short e_shstrndx()
This member holds the section header table index of the entry associated with the section name string table. If the file has no section name string table, this member holds the value SHN_UNDEF.- Returns:
- the section header table index of the entry associated with the section name string table
-
e_type
public short e_type()
This member identifies the object file type; executable, shared object, etc.- Returns:
- the object file type
-
isRelocatable
public boolean isRelocatable()
Returns true if this is a relocatable file.
e_type == NewElfHeaderConstants.ET_REL- Returns:
- true if this is a relocatable file
-
isSharedObject
public boolean isSharedObject()
Returns true if this is a shared object file.
e_type == NewElfHeaderConstants.ET_DYN- Returns:
- true if this is a shared object file
-
isExecutable
public boolean isExecutable()
Returns true if this is an executable file.
e_type == NewElfHeaderConstants.ET_EXEC- Returns:
- true if this is a executable file
-
e_version
public int e_version()
This member identifies the object file version, where "EV_NONE == Invalid Version" and "EV_CURRENT == Current Version" The value 1 signifies the original file format; extensions will create new versions with higher numbers. The value of EV_CURRENT, though given as 1 above, will change as necessary to reflect the current version number.- Returns:
- the object file version
-
getSections
public ElfSectionHeader[] getSections()
Returns the section headers as defined in this ELF file.- Returns:
- the section headers as defined in this ELF file
-
getSections
public ElfSectionHeader[] getSections(int type)
Returns the section headers with the specified type. The array could be zero-length, but will not be null.- Parameters:
type
-- Returns:
- the section headers with the specified type
- See Also:
ElfSectionHeader
-
getSection
public ElfSectionHeader getSection(java.lang.String name)
Returns the section header with the specified name, or null if no section exists with that name.- Parameters:
name
- the name of the requested section- Returns:
- the section header with the specified name
-
getSectionAt
public ElfSectionHeader getSectionAt(long address)
Returns the section header at the specified address, or null if no section exists at that address.- Parameters:
address
- the address of the requested section- Returns:
- the section header with the specified address
-
getSectionLoadHeaderContaining
public ElfSectionHeader getSectionLoadHeaderContaining(long address)
Returns the section header that loads/contains the specified address, or null if no section contains the address.- Parameters:
address
- the address of the requested section- Returns:
- the section header that contains the address
-
getSectionHeaderContainingFileRange
public ElfSectionHeader getSectionHeaderContainingFileRange(long fileOffset, long fileRangeLength)
Returns the section header which fully contains the specified file offset range.- Parameters:
fileOffset
- file offsetfileRangeLength
- length of file range in bytes- Returns:
- section or null if not found
-
getSectionIndex
public int getSectionIndex(ElfSectionHeader section)
Returns the index of the specified section. The index is the order in which the section was defined in the section header table.- Parameters:
section
- the section header- Returns:
- the index of the specified section header
-
getProgramHeaders
public ElfProgramHeader[] getProgramHeaders()
Returns the program headers as defined in this ELF file.- Returns:
- the program headers as defined in this ELF file
-
getProgramHeaders
public ElfProgramHeader[] getProgramHeaders(int type)
Returns the program headers with the specified type. The array could be zero-length, but will not be null.- Parameters:
type
-- Returns:
- the program headers with the specified type
- See Also:
ElfProgramHeader
-
getDynamicTable
public ElfDynamicTable getDynamicTable()
Returns the dynamic table defined by program header of type PT_DYNAMIC or the .dynamic program section. Or, null if one does not exist.- Returns:
- the dynamic table
-
getProgramHeaderProgramHeader
public ElfProgramHeader getProgramHeaderProgramHeader()
Returns the program header with type of PT_PHDR. Or, null if one does not exist.- Returns:
- the program header with type of PT_PHDR
-
getProgramHeaderAt
public ElfProgramHeader getProgramHeaderAt(long virtualAddr)
Returns the program header at the specified address, or null if no program header exists at that address.- Parameters:
virtualAddr
- the address of the requested program header- Returns:
- the program header with the specified address
-
getProgramLoadHeaderContaining
public ElfProgramHeader getProgramLoadHeaderContaining(long virtualAddr)
Returns the PT_LOAD program header which loads a range containing the specified address, or null if not found.- Parameters:
virtualAddr
- the address of the requested program header- Returns:
- the program header with the specified address
-
getProgramLoadHeaderContainingFileOffset
public ElfProgramHeader getProgramLoadHeaderContainingFileOffset(long offset)
Returns the PT_LOAD program header which loads a range containing the specified file offset, or null if not found.- Parameters:
offset
- the file offset to be loaded- Returns:
- the program header with the specified file offset
-
getDynamicLibraryNames
public java.lang.String[] getDynamicLibraryNames()
Returns array of dynamic library names defined by DT_NEEDED- Returns:
- array of dynamic library names
-
getDynamicStringTable
public ElfStringTable getDynamicStringTable()
Returns the dynamic string table as defined in this ELF file.- Returns:
- the dynamic string table as defined in this ELF file
-
getStringTables
public ElfStringTable[] getStringTables()
Returns the string tables as defined in this ELF file.- Returns:
- the string tables as defined in this ELF file
-
getStringTable
public ElfStringTable getStringTable(ElfSectionHeader section)
Returns the string table associated to the specified section header. Or, null if one does not exist.- Returns:
- the string table associated to the specified section header
-
getDynamicSymbolTable
public ElfSymbolTable getDynamicSymbolTable()
Returns the dynamic symbol table as defined in this ELF file.- Returns:
- the dynamic symbol table as defined in this ELF file
-
getSymbolTables
public ElfSymbolTable[] getSymbolTables()
Returns the symbol tables as defined in this ELF file.- Returns:
- the symbol tables as defined in this ELF file
-
getSymbolTable
public ElfSymbolTable getSymbolTable(ElfSectionHeader symbolTableSection)
Returns the symbol table associated to the specified section header. Or, null if one does not exist.- Returns:
- the symbol table associated to the specified section header
-
getRelocationTables
public ElfRelocationTable[] getRelocationTables()
Returns the relocation tables as defined in this ELF file.- Returns:
- the relocation tables as defined in this ELF file
-
getRelocationTable
public ElfRelocationTable getRelocationTable(ElfSectionHeader relocSection)
Returns the relocation table associated to the specified section header. Or, null if one does not exist.- Returns:
- the relocation table associated to the specified section header
-
getMachineName
public java.lang.String getMachineName()
Returns a string name of the processor specified in this ELF header. For example, if "e_machine==EM_386", then it returns "80386".- Returns:
- a string name of the processor specified in this ELF header
-
getFlags
public java.lang.String getFlags()
Returns a string representation of the numeric flags field.- Returns:
- elf flags field value
-
toDataType
public DataType toDataType()
Description copied from interface:StructConverter
Returns a structure datatype representing the contents of the implementor of this interface.For example, given:
class A { int foo; double bar; }
The return value should be a structure data type with two data type components; an INT and a DOUBLE. The structure should contain field names and, if possible, field comments.
- Specified by:
toDataType
in interfaceStructConverter
- Returns:
- returns a structure datatype representing the implementor of this interface
- See Also:
StructConverter.toDataType()
-
getEntryComponentOrdinal
public int getEntryComponentOrdinal()
Get the Elf header structure component ordinal corresponding to the e_entry element- Returns:
- e_entry component ordinal
-
getPhoffComponentOrdinal
public int getPhoffComponentOrdinal()
Get the Elf header structure component ordinal corresponding to the e_phoff element- Returns:
- e_phoff component ordinal
-
getShoffComponentOrdinal
public int getShoffComponentOrdinal()
Get the Elf header structure component ordinal corresponding to the e_shoff element- Returns:
- e_shoff component ordinal
-
addSection
public ElfSectionHeader addSection(MemoryBlock block, int sh_name) throws MemoryAccessException
Adds a new section using the specified memory block. The memory block is used to setting the address and size. As well as, setting the data.- Parameters:
block
- the memory blocksh_name
- the byte index into the string table where the name begins- Returns:
- the newly created section
- Throws:
MemoryAccessException
- if any of the requested memory block bytes are uninitialized.
-
addSection
public ElfSectionHeader addSection(java.lang.String name, int sh_name)
Adds a new section the specifed name and name index. The type of the section will be SHT_PROGBITS.- Parameters:
name
- the actual name of the new sectionsh_name
- the byte index into the string table where the name begins- Returns:
- the newly created section
-
addSection
public ElfSectionHeader addSection(java.lang.String name, int sh_name, int type)
Adds a new section the specifed name and name index. The type of the section will be SHT_PROGBITS.- Parameters:
name
- the actual name of the new sectionsh_name
- the byte index into the string table where the name beginstype
- the type of the new section- Returns:
- the newly created section
-
addProgramHeader
public void addProgramHeader(ElfProgramHeader ph)
Appends the new program header to the end of the existing program header table.- Parameters:
ph
- the new program header
-
write
public void write(java.io.RandomAccessFile raf, DataConverter dc) throws java.io.IOException
Description copied from interface:Writeable
Writes this object to the specified random access file using the data converter to handle endianness.- Specified by:
write
in interfaceWriteable
- Parameters:
raf
- the random access filedc
- the data converter- Throws:
java.io.IOException
- if an I/O error occurs- See Also:
Writeable.write(java.io.RandomAccessFile, ghidra.util.DataConverter)
-
setSectionHeaderOffset
public void setSectionHeaderOffset(long offset)
Sets the section header offset.- Parameters:
offset
- the new section header offset
-
setProgramHeaderOffset
public void setProgramHeaderOffset(long offset)
Sets the program header offset.- Parameters:
offset
- the new program header offset
-
-