Class ElfSectionHeader

java.lang.Object
ghidra.app.util.bin.format.elf.ElfSectionHeader
All Implemented Interfaces:
MemoryLoadable, Writeable, StructConverter

public class ElfSectionHeader extends 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;
 
  • Field Summary

    Fields inherited from interface ghidra.app.util.bin.StructConverter

    ASCII, BYTE, DWORD, IBO32, IBO64, POINTER, QWORD, STRING, UTF16, UTF8, VOID, WORD
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    long
    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
    Some sections have address alignment constraints.
    long
    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[]
    Returns the actual data bytes from the file for this section
    Returns an input stream starting at offset into the byte provider.
    Return ElfHeader associated with this section
    long
    Some sections hold a table of fixed-size entries, such as a symbol table.
    long
    Sections support 1-bit flags that describe miscellaneous attributes.
    int
    This member holds extra information, whose interpretation depends on the section type.
    int
    This member holds extra information, whose interpretation depends on the section type.
    int
    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 actual string name for this section.
    long
    The byte offset from the beginning of the file to the first byte in the section.
    Returns the binary reader.
    long
    This member gives the section's size in bytes.
    int
    This member categorizes the section's contents and semantics.
    Get header type as string.
    int
     
    boolean
    Returns true if this section is allocated (e.g., SHF_ALLOC is set)
    boolean
    Returns true if the data bytes have changed for this section.
    boolean
    Returns true if this section is executable.
    boolean
    Returns true if this section header's offset is invalid.
    boolean
    Returns true if this section has been modified.
    boolean
    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
    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.
    Returns a structure datatype representing the contents of the implementor of this interface.
     
    void
    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 Details

  • Method Details

    • getElfHeader

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

      public void write(RandomAccessFile raf, DataConverter dc) throws 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 interface Writeable
      Parameters:
      raf - the random access file
      dc - the data converter
      Throws:
      IOException - if an I/O error occurs
      See Also:
    • 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 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 String toString()
      Overrides:
      toString in class Object
      See Also:
    • 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
    • isInvalidOffset

      public boolean isInvalidOffset()
      Returns true if this section header's offset is invalid.
      Returns:
      true if this section header's offset is invalid
    • 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 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 IOException
      Returns the actual data bytes from the file for this section
      Returns:
      the actual data bytes from the file for this section
      Throws:
      IOException - if an I/O error occurs while reading the file
    • getDataStream

      public InputStream getDataStream() throws 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:
      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 IOException
      Sets the offset of this section. The offset is the actual byte offset into the file.
      Parameters:
      offset - the file byte offset
      Throws:
      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(String name)
      Sets the name of this section (may get changed due to conflict)
      Parameters:
      name - section 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:
    • hashCode

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object