Class ElfProgramHeader

  • All Implemented Interfaces:
    MemoryLoadable, Writeable, StructConverter, java.lang.Comparable<ElfProgramHeader>

    public class ElfProgramHeader
    extends java.lang.Object
    implements StructConverter, java.lang.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;
     
    • Constructor Summary

      Constructors 
      Constructor Description
      ElfProgramHeader()
      DO NOT USE THIS CONSTRUCTOR, USE create*(GenericFactory ...) FACTORY METHODS INSTEAD.
      ElfProgramHeader​(ElfHeader header, int type)
      Constructs a new program header with the specified type.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(ElfProgramHeader that)  
      boolean equals​(java.lang.Object obj)  
      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.
      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.
      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.
      java.lang.String getComment()
      Get descriptive comment which includes type and description
      java.lang.String getDescription()
      Get header description
      ElfHeader getElfHeader()
      Return ElfHeader associated with this program header
      long getFileSize()
      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 getMemorySize()
      Get the unadjusted memory size in bytes specified by this program header; it may be zero.
      long getOffset()
      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 getPhysicalAddress()
      On systems for which physical addressing is relevant, this member is reserved for the segment's physical address.
      BinaryReader getReader()
      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.
      java.lang.String getTypeAsString()
      Get header type as string.
      long getVirtualAddress()
      This member gives the virtual address at which the first byte of the segment resides in memory.
      int hashCode()  
      protected void initElfProgramHeader​(FactoryBundledWithBinaryReader reader, ElfHeader header)  
      boolean isExecute()
      Returns true if this segment is executable when loaded
      boolean isRead()
      Returns true if this segment is readable when loaded
      boolean isWrite()
      Returns true if this segment is writable when loaded
      void setAddress​(long paddr, long vaddr)
      Sets the new physical and virtual addresses
      void setFlags​(int flags)  
      void setOffset​(long offset)
      Set the offset.
      void setSize​(long fileSize, long memSize)
      Sets the file and memory size.
      DataType toDataType()
      Returns a structure datatype representing the contents of the implementor of this interface.
      java.lang.String toString()  
      void write​(java.io.RandomAccessFile raf, DataConverter dc)
      Writes this object to the specified random access file using the data converter to handle endianness.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ElfProgramHeader

        public ElfProgramHeader()
        DO NOT USE THIS CONSTRUCTOR, USE create*(GenericFactory ...) FACTORY METHODS INSTEAD.
      • ElfProgramHeader

        public ElfProgramHeader​(ElfHeader header,
                                int type)
        Constructs a new program header with the specified type.
        Parameters:
        type - the new type of the program header
    • Method Detail

      • getElfHeader

        public ElfHeader getElfHeader()
        Return ElfHeader associated with this program header
        Returns:
        ElfHeader
      • getTypeAsString

        public java.lang.String 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

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getDescription

        public java.lang.String getDescription()
        Get header description
        Returns:
        header description
      • getComment

        public java.lang.String 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

        public BinaryReader 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
      • 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:
        for obtaining PT_LOAD segment which contains virtualAddress
      • 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 size
        memSize - 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

        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 interface StructConverter
        Returns:
        returns a structure datatype representing the implementor of this interface
        See Also:
        StructConverter.toDataType()
      • setAddress

        public void setAddress​(long paddr,
                               long vaddr)
        Sets the new physical and virtual addresses
        Parameters:
        paddr - the new physical address
        vaddr - the new virtual address
      • compareTo

        public int compareTo​(ElfProgramHeader that)
        Specified by:
        compareTo in interface java.lang.Comparable<ElfProgramHeader>
        See Also:
        Comparable.compareTo(java.lang.Object)
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object