Class ElfSectionHeader

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

public class ElfSectionHeader extends Object implements StructConverter, 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 Details

    • ElfSectionHeader

      public ElfSectionHeader(BinaryReader reader, ElfHeader header) throws IOException
      Parameters:
      reader - dedicated reader instance positioned to the start of the program header data. (the reader supplied will be retained and altered).
      header - ELF header
      Throws:
      IOException - if an IO error occurs during parse
  • Method Details

    • 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.
    • isCompressed

      public boolean isCompressed()
      Returns true if this section is compressed in a supported manner. This does NOT include sections that carry compressed data, such as ".zdebuginfo" type sections.
      Returns:
      true if the section was compressed and needs to be decompressed, false if normal section
    • 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
    • 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
    • getLogicalSize

      public long getLogicalSize()
      Returns the logical size of this section, possibly affected by compression.
      Returns:
      logical size of this section, see getSize()
    • hasFilteredLoadInputStream

      public boolean hasFilteredLoadInputStream(ElfLoadHelper elfLoadHelper, Address start)
      Description copied from interface: MemoryLoadable
      Determine if the use of input stream decompression or filtering via an extension is neccessary. If this method returns true and a filtered stream is required and will prevent the use of a direct mapping to file bytes for affected memory regions.
      Specified by:
      hasFilteredLoadInputStream in interface MemoryLoadable
      Parameters:
      elfLoadHelper - ELF load helper
      start - memory load address
      Returns:
      true if the use of a filtered input stream is required
    • getFilteredLoadInputStream

      public InputStream getFilteredLoadInputStream(ElfLoadHelper elfLoadHelper, Address start, long dataLength, BiConsumer<String,Throwable> errorConsumer) throws IOException
      Description copied from interface: MemoryLoadable
      Return filtered InputStream for loading a memory block (includes non-loaded OTHER blocks). See MemoryLoadable.hasFilteredLoadInputStream(ElfLoadHelper, Address).
      Specified by:
      getFilteredLoadInputStream in interface MemoryLoadable
      Parameters:
      elfLoadHelper - ELF load helper
      start - memory load address
      dataLength - the in-memory data length in bytes (actual bytes read from dataInput may be more)
      errorConsumer - consumer that will accept errors which may occur during stream decompression, if null Msg.error() will be used.
      Returns:
      filtered input stream or original input stream
      Throws:
      IOException - if error initializing filtered input stream
    • getRawInputStream

      public InputStream getRawInputStream() throws IOException
      Description copied from interface: MemoryLoadable
      Returns raw data input stream associated with this loadable object..
      Specified by:
      getRawInputStream in interface MemoryLoadable
      Returns:
      raw data input stream associated with this loadable object.
      Throws:
      IOException - if error initializing input stream
    • 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
    • getReader

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

      public void setAddress(long addr)
      Sets the start address of this section.
      Parameters:
      addr - the new start address of this section
    • 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