Data Encoding
A file's data encoding specifies how to interpret the basic objects in a file. Class ELFCLASS32 files use objects that occupy 1, 2, and 4 bytes. Class ELFCLASS64
files use objects that occupy 1, 2, 4, and 8 bytes. Under the defined encodings, objects are represented as shown below. Byte numbers appear in the upper left corners.
Encoding ELFDATA2LSB specifies 2's complement values, with the least significant byte occupying the lowest address.
Figure 7-2 Data Encoding ELFDATA2LSB
 Encoding ELFDATA2MSB specifies 2's complement values, with the most significant byte occupying the lowest address.
Figure 7-3 Data Encoding ELFDATA2MSB
 Sections
An object file's section header table helps you locate all of the sections of the file. The section header table is an array of Elf32_Shdr or Elf64_Shdr structures,
as described below. A section header table index is a subscript into this array. The ELF header's e_shoff member gives the byte offset from the beginning of
the file to the section header table; e_shnum tells how many entries the section header table contains; e_shentsize gives the size in bytes of each
entry.
If the number of sections is greater than or equal to SHN_LORESERVE (0xff00), e_shnum has the value SHN_UNDEF (0)
and the actual number of section header table entries is contained in the sh_size field of the section header at index 0. Otherwise, the sh_size member
of the initial entry contains 0.
Some section header table indexes are reserved in contexts where index size is restricted. For example, the st_shndx member of a symbol table entry and the e_shnum and e_shstrndx members of the ELF header. In such contexts, the reserved values do not represent actual sections in the object file. Also
in such contexts, an escape value indicates that the actual section index is to be found elsewhere, in a larger field.
Table 7-11 ELF Special Section Indexes
Name | Value |
SHN_UNDEF | 0 |
SHN_LORESERVE | 0xff00 |
SHN_LOPROC | 0xff00 |
SHN_BEFORE | 0xff00 |
SHN_AFTER | 0xff01 |
SHN_HIPROC | 0xff1f |
SHN_LOOS | 0xff20 |
SHN_HIOS | 0xff3f |
SHN_ABS | 0xfff1 |
SHN_COMMON | 0xfff2 |
SHN_XINDEX | 0xffff |
SHN_HIRESERVE | 0xffff |
Note - Although index 0 is reserved as the undefined value, the section header table contains an entry for index 0. That is, if the e_shnum member of the ELF header
says a file has 6 entries in the section header table, they have the indexes 0 through 5. The contents of the initial entry are specified later in this section.
- SHN_UNDEF
An undefined, missing, irrelevant, or otherwise meaningless section reference. For example, a symbol defined relative
to section number SHN_UNDEF is an undefined symbol.
- SHN_LORESERVE
The lower boundary of the range of reserved indexes.
- SHN_LOPROC - SHN_HIPROC
Values in this inclusive range are reserved for processor-specific semantics.
- SHN_LOOS - SHN_HIOS
Values in this inclusive range are reserved for operating system-specific semantics.
- SHN_BEFORE, SHN_AFTER
Provide for initial and final section ordering in conjunction with the SHF_LINK_ORDER and SHF_ORDERED section flags, listed in Table 7-14.
- SHN_ABS
Absolute values for the corresponding reference. For example, symbols defined relative to section number SHN_ABS have
absolute values and are not affected by relocation.
- SHN_COMMON
Symbols defined relative to this section are common symbols, such as FORTRAN COMMON or unallocated C external variables.
These symbols are sometimes referred to as tentative.
- SHN_XINDEX
An escape value indicating that the actual section header index is too large to fit in the containing field. The header section index
is found in another location specific to the structure where it appears.
- SHN_HIRESERVE
The upper boundary of the range of reserved indexes. The system reserves indexes between SHN_LORESERVE and SHN_HIRESERVE, inclusive. The values do not reference the section header table. The section header table does not contain entries for the reserved indexes.
Sections contain all information in an object file except the ELF header, the program header table, and the section header table. Moreover, the sections in object
files satisfy several conditions:
Every section in an object file has exactly one section header describing it. Section headers can exist that do not have a section.
Each section occupies one contiguous, possibly empty, sequence of bytes within a file.
Sections in a file cannot overlap. No byte in a file resides in more than one section.
An object file can have inactive space. The various headers and the sections might not cover every byte in an object file. The contents of the inactive data are unspecified.
A section header has the following structure, defined in sys/elf.h:
typedef struct {
Elf32_Word sh_name;
Elf32_Word sh_type;
Elf32_Word sh_flags;
Elf32_Addr sh_addr;
Elf32_Off sh_offset;
Elf32_Word sh_size;
Elf32_Word sh_link;
Elf32_Word sh_info;
Elf32_Word sh_addralign;
Elf32_Word sh_entsize;
} Elf32_Shdr;
typedef struct {
Elf64_Word sh_name;
Elf64_Word sh_type;
Elf64_Xword sh_flags;
Elf64_Addr sh_addr;
Elf64_Off sh_offset;
Elf64_Xword sh_size;
Elf64_Word sh_link;
Elf64_Word sh_info;
Elf64_Xword sh_addralign;
Elf64_Xword sh_entsize;
} Elf64_Shdr;
|
The elements of this structure are:
- sh_name
The name of the section. Its value is an index into the section header string table section giving the location of a null-terminated
string. Section names and their descriptions are listed in Table 7-16.
- sh_type
Categorizes the section's contents and semantics. Section types and their descriptions are listed in Table 7-12.
- sh_flags
Sections support 1-bit flags that describe miscellaneous attributes. Flag definitions are listed in Table 7-14.
- sh_addr
If the section is to 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.
- sh_offset
The byte offset from the beginning of the file to the first byte in the section. For a SHT_NOBITS section, this
member indicates the conceptual offset in the file, as the section occupies no space in the file.
- sh_size
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 can have a nonzero size, but it occupies no space in the file.
- sh_link
A section header table index link, whose interpretation depends on the section type. Table 7-15 describes the values.
- sh_info
Extra information, whose interpretation depends on the section type. Table 7-15 describes the values.
- sh_addralign
Some sections have address alignment constraints. For example, if a section holds a double-word, the system must ensure double-word
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.
- sh_entsize
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.
A section header's sh_type member specifies the section's semantics, as shown in the following table.
Table 7-12 ELF Section Types, sh_type
Name | Value |
SHT_NULL | 0 |
SHT_PROGBITS | 1 |
SHT_SYMTAB | 2 |
SHT_STRTAB | 3 |
SHT_RELA | 4 |
SHT_HASH | 5 |
SHT_DYNAMIC | 6 |
SHT_NOTE | 7 |
SHT_NOBITS | 8 |
SHT_REL | 9 |
SHT_SHLIB | 10 |
SHT_DYNSYM | 11 |
SHT_INIT_ARRAY | 14 |
SHT_FINI_ARRAY | 15 |
SHT_PREINIT_ARRAY | 16 |
SHT_GROUP | 17 |
SHT_SYMTAB_SHNDX | 18 |
SHT_LOOS | 0x60000000 |
SHT_LOSUNW | 0x6ffffff7 |
SHT_SUNW_ANNOTATE | 0x6ffffff7 |
SHT_SUNW_DEBUGSTR | 0x6ffffff8 |
SHT_SUNW_DEBUG | 0x6ffffff9 |
SHT_SUNW_move | 0x6ffffffa |
SHT_SUNW_COMDAT | 0x6ffffffb |
SHT_SUNW_syminfo | 0x6ffffffc |
SHT_SUNW_verdef | 0x6ffffffd |
SHT_SUNW_verneed | 0x6ffffffe |
SHT_SUNW_versym | 0x6fffffff |
SHT_HISUNW | 0x6fffffff |
SHT_HIOS | 0x6fffffff |
SHT_LOPROC | 0x70000000 |
SHT_SPARC_GOTDATA | 0x70000000 |
SHT_HIPROC | 0x7fffffff |
SHT_LOUSER | 0x80000000 |
SHT_HIUSER | 0xffffffff |
|