# Object files
Hexagon object files are stored in the ELF file format (short for
*Executable and Linkable Format*). This chapter describes object file
compatibility and how various ELF elements are defined in Hexagon. It
covers the following topics:
- Compatibility
- ELF header
- ELF sections
- Relocation
## Compatibility
Object files for the Hexagon V4, V5x, and V6x processors are backward
compatible when executed in User mode.
Kernel-mode object files are not backward compatible due to changes
in the system-level instruction set.
## ELF header
The ELF header member e\_machine is set to the symbolic value
EM\_HEXAGON (which has the decimal numeric value 164).
The member e\_type can additionally be set to the symbolic value
ET\_HEXAGON\_IR (numeric value 0xff00) to indicate that an object file
contains compiler-generated intermediary representation language.
Bits [11:0] of the member e\_flags indicate the version of the Hexagon
processor that the object file was created for. The following table
lists the possible values for this bit field.
**Object processor version flags**
| **Name** | **Value** | **Processor Version** |
| --- | --- | --- |
| EF\_HEXAGON\_MACH\_V4 | 0x3 | Hexagon V4 |
| EF\_HEXAGON\_MACH\_V5 | 0x4 | Hexagon V5 |
| EF\_HEXAGON\_MACH\_V55 | 0x5 | Hexagon V55 |
| EF\_HEXAGON\_MACH\_V60 | 0x60 | Hexagon V60 |
| EF\_HEXAGON\_MACH\_V61 | 0x61 | Hexagon V61 |
| EF\_HEXAGON\_MACH\_V62 | 0x62 | Hexagon V62 |
| EF\_HEXAGON\_MACH\_V65 | 0x65 | Hexagon V65 |
| EF\_HEXAGON\_MACH\_V66 | 0x66 | Hexagon V66 |
| EF\_HEXAGON\_MACH\_V67 | 0x67 | Hexagon V67 |
| EF\_HEXAGON\_MACH\_V67T | 0x8067 | Hexagon V67 Small Core (V67t) |
Note
ISA is short for *Instruction Set Architecture*. In this
context, it is considered equivalent to *processor version*.
## Sections
In addition to supporting the standard ELF section indexes, Hexagon
object files can also include processor-specific section indexes for
common symbols stored in the small data area ([Special registers](https://docs.qualcomm.com/doc/80-N2040-23/topic/process-initialization.html#special-registers)).
These indexes are listed in the following table.
The suffix on the processor-specific section index names indicates
the access size (in bytes) of the data in the corresponding section.
The one name lacking a suffix is used to specify sections that have
an access size other than one of the explicitly-specified values.
**Section indexes for common small data**
| **Name** | **Value** | **Description** |
| --- | --- | --- |
| SHN\_HEXAGON\_SCOMMON | 0xff00 | Other access sizes |
| SHN\_HEXAGON\_SCOMMON\_1 | 0xff01 | Byte-sized access |
| SHN\_HEXAGON\_SCOMMON\_2 | 0xff02 | Half-word-sized access |
| SHN\_HEXAGON\_SCOMMON\_4 | 0xff03 | Word-sized access |
| SHN\_HEXAGON\_SCOMMON\_8 | 0xff04 | Double-word-size access |
To mark a section as residing in the small data area, the member,
sh\_flags, can be set to the symbolic value, SHF\_HEXAGON\_GPREL
(numeric value 0x10000000).
## Relocation
Many different types of relocations are performed on the instructions
and data stored in a Hexagon object file. The relocation types differ
according to the instruction and data fields that are affected, and
the kinds of calculations that are applied to the fields to perform
the relocation.
### Relocation fields
The relocation types are expressed using the data fields defined in
the *relocation fields* table:
>
>
> - Name – Specifies the name of the relocatable field (referenced in
> the *relocation types* table).
> - Width – Specifies the width (in bits) of the relocatable bit field.
> Depending on the relocation type, either all or some of these bits
> are modified by the relocation.
> - Effective bits – Specifies how many of the bits in the relocation
> field are modified.
> - Bitmap – Specifies a bit pattern which indicates the bits in the
> relocation field that are actually modified by the relocation.
> - Byte address alignment – Specifies the byte address alignment (1, 2,
> or 4) that is required for the relocation field.
**Relocation fields**
| **Name** | **Width** | **Effective bits** | **Bitmap** | **Byte address alignment** |
| --- | --- | --- | --- | --- |
| Word8 | 8 | 8 | 0xff | 1 |
| Word16 | 16 | 16 | 0xffff | 2 |
| Word32 | 32 | 32 | 0xffffffff | 4 |
| Word32\_LO | 32 | 16 | 0x00c03fff | 4 |
| Word32\_HL | 64 ^a^ | 16 and 16 | 0x00c03fff
0x00c03fff | 4 |
| Word32\_GP | 32 | 16 | N/A ^b^ | 4 |
| Word32\_B7 | 32 | 7 | 0x00001f18 | 4 |
| Word32\_B9 | 32 | 9 | 0x003000fe | 4 |
| Word32\_B13 | 32 | 13 | 0x00202ffe | 4 |
| Word32\_B15 | 32 | 15 | 0x00df20fe | 4 |
| Word32\_B22 | 32 | 22 | 0x01ff3ffe | |
| Word32\_M21 | 32 | 21 | 0x0fff3fe0 | 4 |
| Word32\_M25 | 32 | 25 | 0x0fff3fef | 4 |
| Word32\_R6 | 32 | 6 | 0x000007e0 | 4 |
| Word32\_U6 | 32 | 6 | N/A^b^ | 4 |
| Word32\_U16 | 32 | 16 | N/A^b^ | 4 |
| Word32\_X26 | 32 | 26 | 0x0fff3fff | 4 |
- ^a^ The relocation calculations are applied to two 32-bit words in
sequential addresses: first “(S + A) >> 16”, then “(S + A)”.
- ^b^ – The bitmap varies according to the instruction opcode.
Note
Bitmaps are used to specify operands in instruction
op-codes that must be relocated. When applying a relocation in these
cases, the bits that are not specified in the bitmap must be
protected from accidental modification.
### Relocation symbols
Each relocation type is defined as a formula which is expressed in
terms of the symbols listed in the following table.
**Relocation symbols**
| **Symbol** | **Description** |
| --- | --- |
| A | Addend used to compute value of relocatable field. |
| B | Base address of object loaded into memory. |
| G | Offset into global offset table ([Global offset table](https://docs.qualcomm.com/doc/80-N2040-23/topic/dynamic-linking.html#global-offset-table)) for the entry of a symbol. |
| GOT | Address of entry zero in global offset table ([Global offset table](https://docs.qualcomm.com/doc/80-N2040-23/topic/dynamic-linking.html#global-offset-table)). |
| GP | Value of GP register, typically the base address of the small
data area (\_SDA\_BASE\_). |
| L | Offset into procedure linkage table ([Procedure linkage table](https://docs.qualcomm.com/doc/80-N2040-23/topic/dynamic-linking.html#linkage-table)) for the entry of a symbol. |
| MB | Base address of all strings consumed by compiler message base
optimization (\_MSG\_BASE\_). |
| P | Place address of the field being relocated.
Note
This value is computed using r\_offset in the
relocation entry. |
| S | Value of the symbol whose index resides in the relocation entry
(unless the object is a shared object and the symbol index is
SHN\_UNDEF, in which case this is equivalent to symbol B). |
| TLS | Thread-pointer-relative offset to a thread-local symbol. |
| T | Base address of the static thread-local template that contains a
thread-local symbol. |
### Relocation types
The relocation types are applied in a similar manner to relocatable,
executable, and shared object files, except where noted otherwise.
Only Elf32\_Rela relocation entries are used. Therefore, the original
content of the instruction and data fields is irrelevant when
calculating the relocation.
The relocation types are defined in the following table:
- Name – Specifies the symbolic name of the relocation type.
- Value – Specifies the corresponding numeric identifier for the
relocation type.
- Field – Specifies the data field affected by the relocation type
(see *relocation type* table).
- Relocation – Sspecifies the algebraic formula used to perform the
relocation (with the formula symbols defined in the *relocation symbols* table).
- Result – Specifies the numeric format (signed or unsigned) of the
relocated value.
- Action – Specifies an additional operation performed on the relocated
value as part of the relocation:
- Truncate – Truncate the relocated value to fit the relocation
field.
- Verify – Check that the relocated value can fit in the relocation
field, and generate an error if the value is too big to fit in its
relocation field.
- None – Perform no operation on the relocated value.
**Relocation types**
| **Name** | **Value** | **Field** | **Relocation** | **Result** | **Action** |
| --- | --- | --- | --- | --- | --- |
| R\_HEX\_NONE | 0 | None | None | None | None |
| R\_HEX\_B22\_PCREL | 1 | Word32\_B22 | (S + A - P) >> 2 | Signed | Verify |
| R\_HEX\_B15\_PCREL | 2 | Word32\_B15 | (S + A - P) >> 2 | Signed | Verify |
| R\_HEX\_B7\_PCREL | 3 | Word32\_B7 | (S + A - P) >> 2 | Signed | Verify |
| R\_HEX\_LO16 | 4 | Word32\_LO | (S + A) | Unsigned | Truncate |
| R\_HEX\_HI16 | 5 | Word32\_LO | (S + A) >> 16 | Unsigned | Truncate |
| R\_HEX\_32 | 6 | Word32 | (S + A) | Unsigned | Truncate |
| R\_HEX\_16 | 7 | Word16 | (S + A) | Unsigned | Truncate |
| R\_HEX\_8 | 8 | Word8 | (S + A) | Unsigned | Truncate |
| R\_HEX\_GPREL16\_0 | 9 | Word32\_GP | (S + A - GP) | Unsigned | Verify |
| R\_HEX\_GPREL16\_1 | 10 | Word32\_GP | (S + A - GP) >> 1 | Unsigned | Verify |
| R\_HEX\_GPREL16\_2 | 11 | Word32\_GP | (S + A - GP) >> 2 | Unsigned | Verify |
| R\_HEX\_GPREL16\_3 | 12 | Word32\_GP | (S + A - GP) >> 3 | Unsigned | Verify |
| R\_HEX\_HL16 | 13 | Word32\_HL | (S + A) >> 16 and (S + A) | Unsigned | Truncate |
| R\_HEX\_B13\_PCREL | 14 | Word32\_B13 | (S + A - P) >> 2 | Signed | Verify |
| R\_HEX\_B9\_PCREL | 15 | Word32\_B9 | (S + A - P) >> 2 | Signed | Verify |
| R\_HEX\_B32\_PCREL\_X | 16 | Word32\_X26 | (S + A - P) >> 6 | Signed | Truncate |
| R\_HEX\_32\_6\_X | 17 | Word32\_X26 | (S + A) >> 6 | Unsigned | Verify |
| R\_HEX\_B22\_PCREL\_X | 18 | Word32\_B22 | (S + A - P) & 0x3f | Signed | Verify |
| R\_HEX\_B15\_PCREL\_X | 19 | Word32\_B15 | (S + A - P) & 0x3f | Signed | Verify |
| R\_HEX\_B13\_PCREL\_X | 20 | Word32\_B13 | (S + A - P) & 0x3f | Signed | Verify |
| R\_HEX\_B9\_PCREL\_X | 21 | Word32\_B9 | (S + A - P) & 0x3f | Signed | Verify |
| R\_HEX\_B7\_PCREL\_X | 22 | Word32\_B7 | (S + A - P) & 0x3f | Signed | Verify |
| R\_HEX\_16\_X | 23 | Word32\_U6 | (S + A) | Unsigned | Truncate |
| R\_HEX\_12\_X | 24 | Word32\_R6 | (S + A) | Unsigned | Truncate |
| R\_HEX\_11\_X | 25 | Word32\_U6 | (S + A) | Unsigned | Truncate |
| R\_HEX\_10\_X | 26 | Word32\_U6 | (S + A) | Unsigned | Truncate |
| R\_HEX\_9\_X | 27 | Word32\_U6 | (S + A) | Unsigned | Truncate |
| R\_HEX\_8\_X | 28 | Word32\_U6 | (S + A) | Unsigned | Truncate |
| R\_HEX\_7\_X | 29 | Word32\_U6 | (S + A) | Unsigned | Truncate |
| R\_HEX\_6\_X | 30 | Word32\_U6 | (S + A) | Unsigned | Truncate |
| R\_HEX\_32\_PCREL | 31 | Word32 | (S + A - P) | Signed | Verify |
| R\_HEX\_COPY | 32 | Word32 | (see below) | | |
| R\_HEX\_GLOB\_DAT | 33 | Word32 | (S + A) (see below) | Unsigned | Truncate |
| R\_HEX\_JMP\_SLOT | 34 | Word32 | (S + A) (see below) | Unsigned | Truncate |
| R\_HEX\_RELATIVE | 35 | Word32 | (B + A) (see below) | Unsigned | Truncate |
| R\_HEX\_PLT\_B22\_PCREL | 36 | Word32\_B22 | (L + A - P) >> 2 | Signed | Verify |
| R\_HEX\_GOTREL\_LO16 | 37 | Word32\_LO | (S + A - GOT) | Signed | Truncate |
| R\_HEX\_GOTREL\_HI16 | 38 | Word32\_LO | (S + A - GOT) >> 16 | Signed | Truncate |
| R\_HEX\_GOTREL\_32 | 39 | Word32 | (S + A - GOT) | Signed | Truncate |
| R\_HEX\_GOT\_LO16 | 40 | Word32\_LO |
>> 16
>> 16
>> 16
>> 6
>> 6
>> 6
>> 16
>> 6