40c801c92a1b269c8becc87dd3821a6a066c0a45
[tpg/acess2.git] / Usermode / Libraries / ld-acess.so_src / elf32.h
1 /**
2  Acess v1
3  \file elf32.h
4  \brief ELF Exeutable Loader
5 */
6 #ifndef _ELF32_H
7 #define _ELF32_H
8
9 #include <stdint.h>
10
11 typedef uint16_t        Elf32_Half;
12 typedef uint32_t        Elf32_Addr;
13 typedef uint32_t        Elf32_Off;
14 typedef uint32_t        Elf32_Word;
15 typedef uint32_t        Elf32_Xword;    // < not strictly correct... but meh
16 typedef int32_t         Elf32_Sword;
17
18 #define ELFCLASS32      1
19
20 #define EM_NONE 0
21 #define EM_386  3
22 #define EM_ARM  40
23
24 /**
25  \struct elf_header_s
26  \brief ELF File Header
27 */
28 struct sElf32_Ehdr {
29         uint8_t e_ident[16];    //!< Identifier Bytes
30         Elf32_Half      e_filetype;     //!< File Type
31         Elf32_Half      e_machine;      //!< Machine / Arch
32         Elf32_Word      e_version;      //!< Version (File?)
33         Elf32_Addr      e_entry;        //!< Entry Point
34         Elf32_Off       e_phoff;        //!< Program Header Offset
35         Elf32_Word      e_shoff;        //!< Section Header Offset
36         Elf32_Word      e_flags;        //!< Flags
37         Elf32_Half      e_headersize;   //!< Header Size
38         Elf32_Half      e_phentsize;    //!< Program Header Entry Size
39         Elf32_Half      e_phnum;        //!< Program Header Entry Count
40         Elf32_Half      e_shentsize;    //!< Section Header Entry Size
41         Elf32_Half      e_shentcount;   //!< Section Header Entry Count
42         Elf32_Half      e_shstrindex;   //!< Section Header String Table Index
43 };
44
45 /**
46  \name Executable Types
47  \{
48 */
49 #define ET_NONE         0       //!< NULL Type
50 #define ET_REL          1       //!< Relocatable (Object)
51 #define ET_EXEC         2       //!< Executable
52 #define ET_DYN          3       //!< Dynamic Library
53 #define ET_CORE         4       //!< Core?
54 #define ET_LOPROC       0xFF00  //!< Low Impl Defined
55 #define ET_HIPROC       0xFFFF  //!< High Impl Defined
56 //! \}
57
58 /**
59  \name Section IDs
60  \{
61 */
62 #define SHN_UNDEF               0       //!< Undefined Section
63 #define SHN_LORESERVE   0xFF00  //!< Low Reserved
64 #define SHN_LOPROC              0xFF00  //!< Low Impl Defined
65 #define SHN_HIPROC              0xFF1F  //!< High Impl Defined
66 #define SHN_ABS                 0xFFF1  //!< Absolute Address (Base: 0, Size: -1)
67 #define SHN_COMMON              0xFFF2  //!< Common
68 #define SHN_HIRESERVE   0xFFFF  //!< High Reserved
69 //! \}
70
71 /**
72  \enum eElfSectionTypes
73  \brief ELF Section Types
74 */
75 enum eElfSectionTypes {
76         SHT_NULL,       //0
77         SHT_PROGBITS,   //1
78         SHT_SYMTAB,     //2
79         SHT_STRTAB,     //3
80         SHT_RELA,       //4
81         SHT_HASH,       //5
82         SHT_DYNAMIC,    //6
83         SHT_NOTE,       //7
84         SHT_NOBITS,     //8
85         SHT_REL,        //9
86         SHT_SHLIB,      //A
87         SHT_DYNSYM,     //B
88         SHT_LAST,       //C
89         SHT_LOPROC = 0x70000000,
90         SHT_HIPROC = 0x7fffffff,
91         SHT_LOUSER = 0x80000000,
92         SHT_HIUSER = 0xffffffff
93 };
94
95 #if 0
96 #define SHF_WRITE       0x1
97 #define SHF_ALLOC       0x2
98 #define SHF_EXECINSTR   0x4
99 #define SHF_MASKPROC    0xf0000000
100
101 struct sElf32_Shent {
102         Uint32  name;
103         Uint32  type;
104         Uint32  flags;
105         Uint32  address;
106         Uint32  offset;
107         Uint32  size;
108         Uint32  link;
109         Uint32  info;
110         Uint32  addralign;
111         Uint32  entsize;
112 };      //sizeof = 40
113 #endif
114
115 struct elf_sym_s {
116         Elf32_Word      st_name;
117         Elf32_Addr      st_value;       //Address
118         Elf32_Word      st_size;
119         uint8_t         st_info;
120         uint8_t         st_other;
121         Elf32_Half      st_shndx;
122 };
123 #define STN_UNDEF       0       // Undefined Symbol
124
125 #define ELF32_ST_BIND(i)        ((i)>>4)
126 #define ELF32_ST_TYPE(i)        ((i)&0xF)
127
128 enum {
129         STB_LOCAL,
130         STB_GLOBAL,
131         STB_WEAK,
132 };
133
134 enum {
135         PT_NULL,        //0
136         PT_LOAD,        //1
137         PT_DYNAMIC,     //2
138         PT_INTERP,      //3
139         PT_NOTE,        //4
140         PT_SHLIB,       //5
141         PT_PHDR,        //6
142         PT_LOPROC = 0x70000000,
143         PT_HIPROC = 0x7fffffff
144 };
145
146 #define PF_X    1
147 #define PF_W    2
148 #define PF_R    4
149
150 struct sElf32_Phdr {
151         Elf32_Word      p_type;
152         Elf32_Off       p_offset;
153         Elf32_Addr      p_vaddr;
154         Elf32_Addr      p_paddr;
155         Elf32_Word      p_filesz;
156         Elf32_Word      p_memsz;
157         Elf32_Word      p_flags;
158         Elf32_Word      p_align;
159 };
160
161 struct elf32_rel_s {
162         Elf32_Addr      r_offset;
163         Elf32_Word      r_info;
164 };
165
166 struct elf32_rela_s {
167         Elf32_Addr      r_offset;
168         Elf32_Word      r_info;
169         Elf32_Sword     r_addend;
170 };
171
172 enum {
173         R_386_NONE=0,   // none
174         R_386_32,       // S+A
175         R_386_PC32,     // S+A-P
176         R_386_GOT32,    // G+A-P
177         R_386_PLT32,    // L+A-P
178         R_386_COPY,     // none
179         R_386_GLOB_DAT, // S
180         R_386_JMP_SLOT, // S
181         R_386_RELATIVE, // B+A
182         R_386_GOTOFF,   // S+A-GOT
183         R_386_GOTPC,    // GOT+A-P
184         R_386_LAST      // none
185 };
186
187 // NOTES:
188 //  'T' means the thumb bit
189 //  'B(S)' Origin of a symbol
190 enum {
191         R_ARM_NONE,     //  0 No action
192         R_ARM_PC24,     //  1 ((S + A) | T) - P
193         R_ARM_ABS32,    //  2 (S + A) | T
194         R_ARM_REL32,    //  3 ((S + A) | T) - P
195         R_ARM_LDR_PC_G0,        //  4 S + A - P
196         R_ARM_ABS16,    //  5 S + A
197         R_ARM_ABS12,    //  6 S + A
198         R_ARM_THM_ABS5, //  7 S + A
199         R_ARM_ABS8,     //  8 S + A
200         R_ARM_SBREL32,  //  9 ((S + A) | T) - B(S)
201         R_ARM_THM_CALL, // 10 ((S + A) | T) - P
202         R_ARM_THM_PC8,  // 11 S + A - Pa,
203         R_ARM_BREL_ADJ, // 12 ΔB(S) + A
204         R_ARM_TLS_DESC, // 13 --
205         R_ARM_THM_SWI8, // 14 (Reserved)
206         R_ARM_XPC25,    // 15 (Reserved)
207         R_ARM_THM_XPC22,        // 16 (Reserved)
208         R_ARM_TLS_DTPMOD32,     // 17 Module[S]
209         R_ARM_TLS_DTPOFF32,     // 18 S + A - TLS
210         R_ARM_TLS_TPOFF32,      // 19 S + A - tp
211         R_ARM_COPY,     // 20 Misc
212         R_ARM_GLOB_DAT, // 21 (S + A) | T
213         R_ARM_JUMP_SLOT,        // 22 (S + A) | T
214         R_ARM_RELATIVE, // 23 B(S) + A (extra?)
215         // ... More defined (IHI0044)
216 };
217
218 #define ELF32_R_SYM(i)  ((i)>>8)        // Takes an info value and returns a symbol index
219 #define ELF32_R_TYPE(i) ((i)&0xFF)      // Takes an info value and returns a type
220 #define ELF32_R_INFO(s,t)       (((s)<<8)+((t)&0xFF))   // Takes a type and symbol index and returns an info value
221
222 struct elf32_dyn_s {
223         Elf32_Half      d_tag;
224         Elf32_Word      d_val;  //Also d_ptr
225 };
226
227 enum {
228         DT_NULL,        //!< Marks End of list
229         DT_NEEDED,      //!< Offset in strtab to needed library
230         DT_PLTRELSZ,    //!< Size in bytes of PLT
231         DT_PLTGOT,      //!< Address of PLT/GOT
232         DT_HASH,        //!< Address of symbol hash table
233         DT_STRTAB,      //!< String Table address
234         DT_SYMTAB,      //!< Symbol Table address
235         DT_RELA,        //!< Relocation table address
236         DT_RELASZ,      //!< Size of relocation table
237         DT_RELAENT,     //!< Size of entry in relocation table
238         DT_STRSZ,       //!< Size of string table
239         DT_SYMENT,      //!< Size of symbol table entry
240         DT_INIT,        //!< Address of initialisation function
241         DT_FINI,        //!< Address of termination function
242         DT_SONAME,      //!< String table offset of so name
243         DT_RPATH,       //!< String table offset of library path
244         DT_SYMBOLIC,//!< Reverse order of symbol searching for library, search libs first then executable
245         DT_REL,         //!< Relocation Entries (Elf32_Rel instead of Elf32_Rela)
246         DT_RELSZ,       //!< Size of above table (bytes)
247         DT_RELENT,      //!< Size of entry in above table
248         DT_PLTREL,      //!< Relocation entry of PLT
249         DT_DEBUG,       //!< Debugging Entry - Unknown contents
250         DT_TEXTREL,     //!< Indicates that modifcations to a non-writeable segment may occur
251         DT_JMPREL,      //!< Address of PLT only relocation entries
252         DT_LOPROC = 0x70000000, //!< Low Definable
253         DT_HIPROC = 0x7FFFFFFF  //!< High Definable
254 };
255
256 typedef struct sElf32_Ehdr      Elf32_Ehdr;
257 typedef struct sElf32_Phdr      Elf32_Phdr;
258 typedef struct sElf32_Shent     Elf32_Shent;
259 typedef struct elf_sym_s        elf_symtab;
260 typedef struct elf_sym_s        Elf32_Sym;
261 typedef struct elf32_rel_s      Elf32_Rel;
262 typedef struct elf32_rela_s     Elf32_Rela;
263 typedef struct elf32_dyn_s      Elf32_Dyn;
264
265 #endif  // defined(_EXE_ELF_H)

UCC git Repository :: git.ucc.asn.au