Package ghidra.app.util.bin.format.elf
Class ElfProgramHeader
java.lang.Object
ghidra.app.util.bin.format.elf.ElfProgramHeader
- All Implemented Interfaces:
MemoryLoadable
,Writeable
,StructConverter
,Comparable<ElfProgramHeader>
public class ElfProgramHeader
extends Object
implements StructConverter, Comparable<ElfProgramHeader>, Writeable, MemoryLoadable
An executable or shared object file's program header table is an
array of structures, each describing a segment
or other information the system needs to prepare the program for execution.
An object file segment contains one or more sections.
Program headers are meaningful only for executable
and shared object files. A file specifies its
own program header size with the ELF
header's e_phentsize and e_phnum members.
Some entries describe process segments; others give supplementary information and do not contribute to
the process image. Segment entries may appear in any order. Except for PT_LOAD segment
entries which must appear in ascending order, sorted on the p_vaddr member.
typedef struct { Elf32_Word p_type; Elf32_Off p_offset; Elf32_Addr p_vaddr; Elf32_Addr p_paddr; Elf32_Word p_filesz; Elf32_Word p_memsz; Elf32_Word p_flags; Elf32_Word p_align; } Elf32_Phdr; typedef struct { Elf64_Word p_type; //Segment type Elf64_Word p_flags; //Segment flags Elf64_Off p_offset; //Segment file offset Elf64_Addr p_vaddr; //Segment virtual address Elf64_Addr p_paddr; //Segment physical address Elf64_Xword p_filesz; //Segment size in file Elf64_Xword p_memsz; //Segment size in memory Elf64_Xword p_align; //Segment alignment } Elf64_Phdr;
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionElfProgramHeader
(BinaryReader reader, ElfHeader header) ElfProgramHeader
(ElfHeader header, int type) Constructs a new program header with the specified type. -
Method Summary
Modifier and TypeMethodDescriptionint
compareTo
(ElfProgramHeader that) boolean
long
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.long
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.long
getAlign()
As ''Program Loading'' later in this part describes, loadable process segments must have congruent values for p_vaddr and p_offset, modulo the page size.Get descriptive comment which includes type and descriptionGet header descriptionReturn ElfHeader associated with this program headerlong
This member gives the number of bytes in the file image of the segment; it may be zero.int
getFlags()
This member gives flags relevant to the segment.long
Get the unadjusted memory size in bytes specified by this program header; it may be zero.long
This member gives the offset from the beginning of the file at which the first byte of the segment resides.long
getOffset
(long virtualAddress) Compute the file offset associated with the specified loaded virtual address defined by this PT_LOAD program header.long
On systems for which physical addressing is relevant, this member is reserved for the segment's physical address.Returns the binary reader.int
getType()
This member tells what kind of segment this array element describes or how to interpret the array element's information.Get header type as string.long
This member gives the virtual address at which the first byte of the segment resides in memory.int
hashCode()
boolean
Returns true if this segment is executable when loadedboolean
Return true if this program header's offset is invalid.boolean
isRead()
Returns true if this segment is readable when loadedboolean
isWrite()
Returns true if this segment is writable when loadedvoid
setAddress
(long paddr, long vaddr) Sets the new physical and virtual addressesvoid
setFlags
(int flags) void
setOffset
(long offset) Set the offset.void
setSize
(long fileSize, long memSize) Sets the file and memory size.Returns a structure datatype representing the contents of the implementor of this interface.toString()
void
write
(RandomAccessFile raf, DataConverter dc) Writes this object to the specified random access file using the data converter to handle endianness.
-
Field Details
-
header
-
-
Constructor Details
-
ElfProgramHeader
- Throws:
IOException
-
ElfProgramHeader
Constructs a new program header with the specified type.- Parameters:
header
- ELF headertype
- the new type of the program header
-
-
Method Details
-
getElfHeader
Return ElfHeader associated with this program header- Returns:
- ElfHeader
-
write
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:
IOException
- if an I/O error occurs- See Also:
-
getTypeAsString
Get header type as string. ElfProgramHeaderType name will be returned if know, otherwise a numeric name of the form "PT_0x12345678" will be returned.- Returns:
- header type as string
-
toString
-
getDescription
Get header description- Returns:
- header description
-
getComment
Get descriptive comment which includes type and description- Returns:
- descriptive comment
-
getAlign
public long getAlign()As ''Program Loading'' later in this part describes, loadable process segments must have congruent values for p_vaddr and p_offset, modulo the page size. This member gives the value to which the segments are aligned in memory and in the file. Values 0 and 1 mean no alignment is required. Otherwise, p_align should be a positive, integral power of 2, and p_vaddr should equal p_offset, modulo p_align.- Returns:
- the segment alignment value
-
getFileSize
public long getFileSize()This member gives the number of bytes in the file image of the segment; it may be zero.- Returns:
- the number of bytes in the file image
-
getFlags
public int getFlags()This member gives flags relevant to the segment. Defined flag values appear below.- Returns:
- the segment flags
-
setFlags
public void setFlags(int flags) -
isRead
public boolean isRead()Returns true if this segment is readable when loaded- Returns:
- true if this segment is readable when loaded
-
isWrite
public boolean isWrite()Returns true if this segment is writable when loaded- Returns:
- true if this segment is writable when loaded
-
isExecute
public boolean isExecute()Returns true if this segment is executable when loaded- Returns:
- true if this segment is executable when loaded
-
getMemorySize
public long getMemorySize()Get the unadjusted memory size in bytes specified by this program header; it may be zero.- Returns:
- the unadjusted memory size in bytes specified by this program header
-
getAdjustedMemorySize
public long getAdjustedMemorySize()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 based upon the specific processor/language implementation which may require filtering of file bytes as loaded into memory.- Returns:
- the number of bytes in the resulting memory block
-
getAdjustedLoadSize
public long getAdjustedLoadSize()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 the ElfExtension may require based upon the specific processor/language implementation which may require filtering of file bytes as loaded into memory.- Returns:
- the number of bytes to be loaded into the resulting memory block
-
getReader
Returns the binary reader.- Returns:
- the binary reader
-
getOffset
public long getOffset()This member gives the offset from the beginning of the file at which the first byte of the segment resides.- Returns:
- the offset from the beginning of the file
-
isInvalidOffset
public boolean isInvalidOffset()Return true if this program header's offset is invalid.- Returns:
- true if this program header's offset is invalid
-
getOffset
public long getOffset(long virtualAddress) 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 addresses defined by the PT_DYNAMIC section.- Parameters:
virtualAddress
- a memory address which has already had the PRElink adjustment applied- Returns:
- computed file offset or -1 if virtual address not contained within this header
- See Also:
-
setOffset
public void setOffset(long offset) Set the offset. This value is the byte offset into the ELF file.- Parameters:
offset
- the new offset value
-
setSize
public void setSize(long fileSize, long memSize) 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.- Parameters:
fileSize
- the new file sizememSize
- the new memory size
-
getPhysicalAddress
public long getPhysicalAddress()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 programs, this member has unspecified contents for executable files and shared objects.- Returns:
- the segment's physical address
-
getType
public int getType()This member tells what kind of segment this array element describes or how to interpret the array element's information. Type values and their meanings appear below.- Returns:
- the program header type
-
getVirtualAddress
public long getVirtualAddress()This member gives the virtual address at which the first byte of the segment resides in memory.- Returns:
- the virtual address
-
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:
-
setAddress
public void setAddress(long paddr, long vaddr) Sets the new physical and virtual addresses- Parameters:
paddr
- the new physical addressvaddr
- the new virtual address
-
compareTo
- Specified by:
compareTo
in interfaceComparable<ElfProgramHeader>
- See Also:
-
hashCode
public int hashCode() -
equals
-