Class ElfSectionHeader

  • All Implemented Interfaces:
    MemoryLoadable, Writeable, StructConverter

    public class ElfSectionHeader
    extends java.lang.Object
    implements StructConverter, Writeable, MemoryLoadable
    A class to represent the Elf32_Shdr data structure.
     typedef  int32_t  Elf32_Sword;
     typedef uint32_t  Elf32_Word;
     typedef uint32_t  Elf32_Addr;
     
     typedef struct {
         Elf32_Word    sh_name;       //Section name (string tbl index)
         Elf32_Word    sh_type;       //Section type
         Elf32_Word    sh_flags;      //Section flags
         Elf32_Addr    sh_addr;       //Section virtual addr at execution
         Elf32_Off     sh_offset;     //Section file offset
         Elf32_Word    sh_size;       //Section size in bytes
         Elf32_Word    sh_link;       //Link to another section
         Elf32_Word    sh_info;       //Additional section information
         Elf32_Word    sh_addralign;  //Section alignment
         Elf32_Word    sh_entsize;    //Entry size if section holds table *
     } Elf32_Shdr;
     
     typedef  uint32_t  Elf64_Word;
     typedef  uint64_t  Elf64_Xword;
     typedef  uint64_t  Elf64_Addr;
     typedef  uint64_t  Elf64_Off;
     
     typedef struct {
         Elf64_Word    sh_name;       //Section name (string tbl index)
         Elf64_Word    sh_type;       //Section type
         Elf64_Xword   sh_flags;      //Section flags
         Elf64_Addr    sh_addr;       //Section virtual addr at execution
         Elf64_Off     sh_offset;     //Section file offset
         Elf64_Xword   sh_size;       //Section size in bytes
         Elf64_Word    sh_link;       //Link to another section
         Elf64_Word    sh_info;       //Additional section information
         Elf64_Xword   sh_addralign;  //Section alignment
         Elf64_Xword   sh_entsize;    //Entry size if section holds table *
     } Elf64_Shdr;
     
    • Constructor Summary

      Constructors 
      Constructor Description
      ElfSectionHeader()
      DO NOT USE THIS CONSTRUCTOR, USE create*(GenericFactory ...) FACTORY METHODS INSTEAD.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object obj)  
      long getAddress()
      If the section will appear in the memory image of a process, this member gives the address at which the section's first byte should reside.
      long getAddressAlignment()
      Some sections have address alignment constraints.
      long getAdjustedSize()
      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.
      byte[] getData()
      Returns the actual data bytes from the file for this section
      java.io.InputStream getDataStream()
      Returns an input stream starting at offset into the byte provider.
      ElfHeader getElfHeader()
      Return ElfHeader associated with this section
      long getEntrySize()
      Some sections hold a table of fixed-size entries, such as a symbol table.
      long getFlags()
      Sections support 1-bit flags that describe miscellaneous attributes.
      int getInfo()
      This member holds extra information, whose interpretation depends on the section type.
      int getLink()
      This member holds extra information, whose interpretation depends on the section type.
      int getName()
      An index into the section header string table section, giving the location of a null-terminated string which is the name of this section.
      java.lang.String getNameAsString()
      Returns the actual string name for this section.
      long getOffset()
      The byte offset from the beginning of the file to the first byte in the section.
      BinaryReader getReader()
      Returns the binary reader.
      long getSize()
      This member gives the section's size in bytes.
      int getType()
      This member categorizes the section's contents and semantics.
      java.lang.String getTypeAsString()
      Get header type as string.
      int hashCode()  
      boolean isAlloc()
      Returns true if this section is allocated (e.g., SHF_ALLOC is set)
      boolean isBytesChanged()
      Returns true if the data bytes have changed for this section.
      boolean isExecutable()
      Returns true if this section is executable.
      boolean isModified()
      Returns true if this section has been modified.
      boolean isWritable()
      Returns true if this section is writable.
      void setAddress​(long addr)
      Sets the start address of this section.
      void setData​(byte[] data)
      Sets the actual data bytes for this section.
      void setName​(java.lang.String name)
      Sets the name of this section (may get changed due to conflict)
      void setOffset​(long offset)
      Sets the offset of this section.
      void setSize​(long size)
      Sets the section's 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

      • ElfSectionHeader

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

      • getElfHeader

        public ElfHeader getElfHeader()
        Return ElfHeader associated with this section
        Returns:
        ElfHeader
      • getAddress

        public long getAddress()
        If the section will appear in the memory image of a process, this member gives the address at which the section's first byte should reside. Otherwise, the member contains 0.
        Returns:
        the address of the section in memory
      • getAddressAlignment

        public long getAddressAlignment()
        Some sections have address alignment constraints. For example, if a section holds a doubleword, the system must ensure doubleword alignment for the entire section. That is, the value of sh_addr must be congruent to 0, modulo the value of sh_addralign. Currently, only 0 and positive integral powers of two are allowed. Values 0 and 1 mean the section has no alignment constraints.
        Returns:
        the section address alignment constraints
      • getEntrySize

        public long getEntrySize()
        Some sections hold a table of fixed-size entries, such as a symbol table. For such a section, this member gives the size in bytes of each entry. The member contains 0 if the section does not hold a table of fixed-size entries.
        Returns:
        the section entry size
      • getFlags

        public long getFlags()
        Sections support 1-bit flags that describe miscellaneous attributes. Flag definitions appear aove.
        Returns:
        the section flags
      • isWritable

        public boolean isWritable()
        Returns true if this section is writable.
        Returns:
        true if this section is writable.
      • isExecutable

        public boolean isExecutable()
        Returns true if this section is executable.
        Returns:
        true if this section is executable.
      • isAlloc

        public boolean isAlloc()
        Returns true if this section is allocated (e.g., SHF_ALLOC is set)
        Returns:
        true if this section is allocated.
      • getInfo

        public int getInfo()
        This member holds extra information, whose interpretation depends on the section type. If sh_type is SHT_REL or SHT_RELA, then sh_info holds the section header index of the section to which the relocation applies. If sh_type is SHT_SYMTAB or SHT_DYNSYM, then sh_info holds one greater than the symbol table index of the last local symbol (binding STB_LOCAL).
        Returns:
        the section header info
      • getLink

        public int getLink()
        This member holds extra information, whose interpretation depends on the section type. If sh_type is SHT_SYMTAB, SHT_DYNSYM, or SHT_DYNAMIC, then sh_link holds the section header table index of its associated string table. If sh_type is SHT_REL, SHT_RELA, or SHT_HASH sh_link holds the section header index of the associated symbol table.
        Returns:
        the section header link
      • getName

        public int getName()
        An index into the section header string table section, giving the location of a null-terminated string which is the name of this section.
        Returns:
        the index of the section name
      • getNameAsString

        public java.lang.String getNameAsString()
        Returns the actual string name for this section. The section only stores an byte index into the string table where the name string is located.
        Returns:
        the actual string name for this section
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        See Also:
        Object.toString()
      • getOffset

        public long getOffset()
        The byte offset from the beginning of the file to the first byte in the section. One section type, SHT_NOBITS described below, occupies no space in the file, and its sh_offset member locates the conceptual placement in the file.
        Returns:
        byte offset from the beginning of the file to the first byte in the section
      • setSize

        public void setSize​(long size)
        Sets the section's size.
        Parameters:
        size - the new size of the section
      • getSize

        public long getSize()
        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 SHT_NOBITS may have a non-zero size, but it occupies no space in the file.
        Returns:
        the section's size in bytes
      • getAdjustedSize

        public long getAdjustedSize()
        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 based upon the specific processor/language implementation which may require filtering of file bytes as read into memory.
        Returns:
        the number of bytes in the resulting memory block
      • getType

        public int getType()
        This member categorizes the section's contents and semantics.
        Returns:
        the section's contents and semantics
      • getTypeAsString

        public java.lang.String getTypeAsString()
        Get header type as string. ElfSectionHeaderType name will be returned if know, otherwise a numeric name of the form "SHT_0x12345678" will be returned.
        Returns:
        header type as string
      • getData

        public byte[] getData()
                       throws java.io.IOException
        Returns the actual data bytes from the file for this section
        Returns:
        the actual data bytes from the file for this section
        Throws:
        java.io.IOException - if an I/O error occurs while reading the file
      • getDataStream

        public java.io.InputStream getDataStream()
                                          throws java.io.IOException
        Returns an input stream starting at offset into the byte provider. NOTE: Do not use this method if you have called setData().
        Returns:
        the input stream
        Throws:
        java.io.IOException - if an I/O error occurs
      • getReader

        public BinaryReader getReader()
        Returns the binary reader.
        Returns:
        the binary reader
      • setData

        public void setData​(byte[] data)
        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.
        Parameters:
        data - the new data byte for this section
      • isBytesChanged

        public boolean isBytesChanged()
        Returns true if the data bytes have changed for this section.
        Returns:
        true if the data bytes have changed for this section
      • isModified

        public boolean isModified()
        Returns true if this section has been modified. A modified section requires that a new program header get created.
        Returns:
        true if this section has been modified
      • setOffset

        public void setOffset​(long offset)
                       throws java.io.IOException
        Sets the offset of this section. The offset is the actual byte offset into the file.
        Parameters:
        offset - the file byte offset
        Throws:
        java.io.IOException - if an I/O occurs
      • setAddress

        public void setAddress​(long addr)
        Sets the start address of this section.
        Parameters:
        addr - the new start address of this section
      • setName

        public void setName​(java.lang.String name)
        Sets the name of this section (may get changed due to conflict)
        Parameters:
        name -
      • 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()
      • 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