1 /*---------------------------------------------------------------------------* 2 Project: TwlSDK - ELF Loader 3 File: elf.h 4 5 Copyright 2006-2008 Nintendo. All rights reserved. 6 7 These coded instructions, statements, and computer programs contain 8 proprietary information of Nintendo of America Inc. and/or Nintendo 9 Company Ltd., and are protected by Federal copyright law. They may 10 not be disclosed to third parties or copied or duplicated in any form, 11 in whole or in part, without the prior written consent of Nintendo. 12 13 $Date:: 2008-09-18#$ 14 $Rev: 8573 $ 15 $Author: okubata_ryoma $ 16 *---------------------------------------------------------------------------*/ 17 #ifndef ELF_H_ 18 #define ELF_H_ 19 20 #include "types.h" 21 22 /*--------------------------------------------------------- 23 Type Definitions 24 --------------------------------------------------------*/ 25 typedef u32 Elf32_Addr; /* size:4, align:4 Unsigned program address */ 26 typedef u16 Elf32_Half; /* size:2, align:2 Unsigned medium int */ 27 typedef u32 Elf32_Off; /* size:4, align:4 Unsigned file offset */ 28 typedef s32 Elf32_Sword; /* size:4, align:4 Signed large int */ 29 typedef u32 Elf32_Word; /* size:4, align:4 Unsigned large int */ 30 31 /*--------------------------------------------------------- 32 ELF Header 33 --------------------------------------------------------*/ 34 /* e_ident index */ 35 #define EI_MAG0 0 /* File identification */ 36 #define EI_MAG1 1 /* File identification */ 37 #define EI_MAG2 2 /* File identification */ 38 #define EI_MAG3 3 /* File identification */ 39 #define EI_CLASS 4 /* File class 0=invalid, 1=32bit, 2=64bit */ 40 #define EI_DATA 5 /* Data encoding 0=invalid, 1=LSB, 2=MSB */ 41 #define EI_VERSION 6 /* File version (currently 1) */ 42 #define EI_PAD 7 /* Start of padding bytes */ 43 #define EI_NIDENT 16 /* Size of e_ident[] */ 44 45 typedef struct { 46 unsigned char e_ident[EI_NIDENT]; 47 Elf32_Half e_type; /* ELF format (relocatable, executable, etc.) */ 48 Elf32_Half e_machine; /* Architecture requested by the file */ 49 Elf32_Word e_version; /* Version of the ELF format (currently 1) */ 50 Elf32_Addr e_entry; /* Program entry point. 0 if unspecified. */ 51 Elf32_Off e_phoff; /* Offset from the start of the file to the program header table */ 52 Elf32_Off e_shoff; /* Offset from the start of the file to the section header table */ 53 Elf32_Word e_flags; /* Processor-specific flags */ 54 Elf32_Half e_ehsize; /* ELF header size */ 55 Elf32_Half e_phentsize; /* Size of a program header */ 56 Elf32_Half e_phnum; /* Number of program headers */ 57 Elf32_Half e_shentsize; /* Size of a section header */ 58 Elf32_Half e_shnum; /* Number of section headers */ 59 Elf32_Half e_shstrndx; /* Index into the table section with strings for the section names */ 60 } Elf32_Ehdr; 61 62 /* Content defined for e_ident[EI_*] */ 63 #define ELFMAG0 0x7f 64 #define ELFMAG1 'E' 65 #define ELFMAG2 'L' 66 #define ELFMAG3 'F' 67 #define ELFCLASSNONE 0 /* invalid */ 68 #define ELFCLASS32 1 /* ARM and Thumb processors use 32-bit ELF. */ 69 #define ELFCLASS64 2 70 #define ELFDATANONE 0 /* invalid */ 71 #define ELFDATA2LSB 1 /* little-endian */ 72 #define ELFDATA2MSB 2 /* big-endian */ 73 74 75 /* [e_type] */ 76 #define ET_NONE 0 /* No file type */ 77 #define ET_REL 1 /* Re-locatable file */ 78 #define ET_EXEC 2 /* Executable file */ 79 #define ET_DYN 3 /* Shared object file */ 80 #define ET_CORE 4 /* Core file */ 81 #define ET_LOPROC 0xff00 /* Processor-specific */ 82 #define ET_HIPROC 0xffff /* Processor-specific */ 83 84 /* [e_machine] */ 85 #define EM_NONE 0 /* No machine */ 86 #define EM_M32 1 87 #define EM_SPARC 2 88 #define EM_386 3 89 #define EM_68K 4 90 #define EM_88K 5 91 #define EM_860 7 92 #define EM_MIPS 8 93 #define EM_MIPS_RS4_BE 10 94 #define EM_ARM 40 /* ARM/Thumb Architecture */ 95 96 97 /* [e_version] This member identifies the object file version.*/ 98 #define EV_NONE 0 /* Invalid version */ 99 #define EV_CURRENT 1 /* Current version */ 100 101 102 /* 103 ARM-specific e_flags 104 e_flags Field Value Meaning 105 EF_ARM_HASENTRY (0x02) e_entry contains a program-loader entry point 106 (See section 4.1.1, Entry points, below.) 107 EF_ARM_SYMSARESORTED (0x04) Each subsection of the symbol table is sorted by symbol value 108 (See section 4.4.8, Symbol table order, below.) 109 EF_ARM_DYNSYMSUSESEGIDX (0x8) Symbols in dynamic symbol tables that are defined in sections 110 included in program segment n have st_shndx = n + 1. 111 (See section 4.4.9, Dynamic symbol table entries, below.) 112 EF_ARM_MAPSYMSFIRST (0x10) Mapping symbols precede other local symbols in the symbol table 113 (see section 4.4.8, Symbol table order, below). 114 115 EF_ARM_EABIMASK (0xFF000000)(current version is 0x02000000) 116 This masks an 8-bit version number, the version of the ARM 117 EABI to which this ELF file conforms. This EABI is version 2. A 118 value of 0 denotes unknown conformance. 119 */ 120 #define EF_ARM_HASENTRY 0x02 121 #define EF_ARM_SYMSARESORTED 0x04 122 #define EF_ARM_DYNSYMSUSESEGIDX 0x8 123 #define EF_ARM_MAPSYMSFIRST 0x10 124 #define EF_ARM_EABIMASK 0xFF000000 125 126 127 /*--------------------------------------------------------- 128 Program headers 129 --------------------------------------------------------*/ 130 typedef struct { 131 Elf32_Word p_type; 132 Elf32_Off p_offset; 133 Elf32_Addr p_vaddr; 134 Elf32_Addr p_paddr; 135 Elf32_Word p_filesz; 136 Elf32_Word p_memsz; 137 Elf32_Word p_flags; 138 Elf32_Word p_align; 139 } Elf32_Phdr; 140 141 /* */ 142 enum { 143 PT_NULL, /* An unused entry; the meaning of other member values is undefined */ 144 PT_LOAD, /* Segment that will be loaded at runtime */ 145 PT_DYNAMIC, /* Segment that stores a dynamic structure array */ 146 PT_INTERP, /* Segment that stores the path of the interpreter used to parse the file */ 147 PT_NOTE, /* Segment that stores information not used to parse the file */ 148 PT_SHLIB, /* Reserved */ 149 PT_PHDR, /* Program header table (exists only when part of the program's memory image is used) */ 150 PT_TLS, /* Template for the thread-local storage segment */ 151 152 PT_LOOS = 0x60000000, 153 /* Region reserved for OS-specific semantics */ 154 PT_HIOS = 0x6fffffff, 155 156 PT_LOPROC = 0x70000000, 157 /* Region reserved for processor-specific semantics */ 158 PT_HIPROC = 0x7fffffff 159 }; 160 161 162 /*--------------------------------------------------------- 163 Section headers 164 --------------------------------------------------------*/ 165 typedef struct { 166 Elf32_Word sh_name; /*Index into the table section with strings for the section headers*/ 167 Elf32_Word sh_type; /* Type (refer to the definitions below) */ 168 Elf32_Word sh_flags; 169 Elf32_Addr sh_addr; /* */ 170 Elf32_Off sh_offset; /* Offset from the start of the file */ 171 Elf32_Word sh_size; /* Size in bytes */ 172 Elf32_Word sh_link; /* The meaning of this value changes with sh_type */ 173 Elf32_Word sh_info; /* The meaning of this value changes with sh_type */ 174 Elf32_Word sh_addralign; /* Alignment restrictions (no restrictions when this is 0 or 1, and 4-byte alignment when this is 4) */ 175 Elf32_Word sh_entsize; /* Size of a single element when there is a fixed-size entry table */ 176 } Elf32_Shdr; 177 178 /* sh_addr mod sh_addralign must equal 0 */ 179 180 /* Section Types, [sh_type] */ 181 #define SHT_NULL 0 182 #define SHT_PROGBITS 1 183 #define SHT_SYMTAB 2 184 #define SHT_STRTAB 3 185 #define SHT_RELA 4 186 #define SHT_HASH 5 187 #define SHT_DYNAMIC 6 188 #define SHT_NOTE 7 189 #define SHT_NOBITS 8 190 #define SHT_REL 9 191 #define SHT_SHLIB 10 192 #define SHT_DYNSYM 11 193 #define SHT_LOPROC 0x70000000 194 #define SHT_HIPROC 0x7fffffff 195 #define SHT_LOUSER 0x80000000 196 #define SHT_HIUSER 0xffffffff 197 198 199 /* [sh_flags] */ 200 #define SHF_WRITE 0x1 201 #define SHF_ALLOC 0x2 202 #define SHF_EXECINSTR 0x4 203 #define SHF_MASKPROC 0xf0000000 204 /* ARM-EABI-specific */ 205 #define SHF_ENTRYSECT 0x10000000 /* The section contains an entry point. */ 206 #define SHF_COMDEF 0x80000000 /* The section may be multiply defined in the input to a link step. */ 207 208 /*Section index*/ 209 //Sym->st_shndx, etc. 210 #define SHN_UNDEF 0 211 #define SHN_LORESERVE 0xff00 212 #define SHN_LOPROC 0xff00 213 #define SHN_HIPROC 0xff1f 214 #define SHN_ABS 0xfff1 215 #define SHN_COMMON 0xfff2 216 #define SHN_HIRESERVE 0xffff 217 218 219 //Structures after this point contain the actual data, not headers 220 221 /*--------------------------------------------------------- 222 Symbol Table Entry 223 --------------------------------------------------------*/ 224 typedef struct { 225 Elf32_Word st_name; /* Index into the symbol string table */ 226 Elf32_Addr st_value; /* Probably the offset value into the associated section */ 227 Elf32_Word st_size; /* 0 if the size does not exist or is unknown */ 228 unsigned char st_info; /* Type and binding attributes */ 229 unsigned char st_other; /* Currently contains 0 */ 230 Elf32_Half st_shndx; /* Related section header table index */ 231 } Elf32_Sym; 232 233 234 /* st_info */ 235 #define ELF32_ST_BIND(i) ((i)>>4) 236 #define ELF32_ST_TYPE(i) ((i)&0xf) 237 #define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf)) 238 239 /* Binding attributes for st_info */ 240 #define STB_LOCAL 0 241 #define STB_GLOBAL 1 242 #define STB_WEAK 2 243 #define STB_LOPROC 13 244 #define STB_MW_SPECIFIC 14 /*This shows up in NITRO applications (OS_TPrintf is one example)*/ 245 #define STB_HIPROC 15 246 247 /* Symbol types for st_info */ 248 #define STT_NOTYPE 0 /*Undefined*/ 249 #define STT_OBJECT 1 /*Data object*/ 250 #define STT_FUNC 2 251 #define STT_SECTION 3 252 #define STT_FILE 4 253 #define STT_LOPROC 13 254 #define STT_HIPROC 15 255 256 257 /*--------------------------------------------------------- 258 Relocation Entry 259 --------------------------------------------------------*/ 260 typedef struct { 261 Elf32_Addr r_offset; 262 Elf32_Word r_info; 263 } Elf32_Rel; 264 265 typedef struct { 266 Elf32_Addr r_offset; 267 Elf32_Word r_info; 268 Elf32_Sword r_addend; 269 } Elf32_Rela; 270 271 #define ELF32_R_SYM(i) ((i)>>8) 272 #define ELF32_R_TYPE(i) ((unsigned char)(i)) 273 #define ELF32_R_INFO(s,t) (((s)<<8)+(unsigned char)(t)) 274 275 276 /* r_info type */ 277 #define R_ARM_NONE 0 /* Any No relocation. Encodes dependencies between sections. */ 278 #define R_ARM_PC24 1 /* ARM B/BL S . P + A */ 279 #define R_ARM_ABS32 2 /* 32-bit word S + A */ 280 #define R_ARM_REL32 3 /* 32-bit word S . P + A */ 281 #define R_ARM_PC13 4 /* ARM LDR r, [pc,...] S . P + A */ 282 #define R_ARM_ABS16 5 /* 16-bit half-word S + A */ 283 #define R_ARM_ABS12 6 /* ARM LDR/STR S + A */ 284 #define R_ARM_THM_ABS5 7 /* Thumb LDR/STR S + A */ 285 #define R_ARM_ABS8 8 /* 8-bit byte S + A */ 286 #define R_ARM_SBREL32 9 /* 32-bit word S . B + A */ 287 #define R_ARM_THM_PC22 10 /* Thumb BL pair S . P+ A */ 288 #define R_ARM_THM_PC8 11 /* Thumb LDR r, [pc,...] S . P + A */ 289 #define R_ARM_AMP_VCALL9 12 /* AMP VCALL Obsolete.SA-1500 only. */ 290 #define R_ARM_SWI24 13 /* ARM SWI S + A */ 291 #define R_ARM_THM_SWI8 14 /* Thumb SWI S + A */ 292 #define R_ARM_XPC25 15 /* ARM BLX S . P+ A */ 293 #define R_ARM_THM_XPC22 16 /* Thumb BLX pair S . P+ A */ 294 295 /* 17-31, reserved to ARM Linux */ 296 //17-19 Reserved to ARM LINUX 297 #define R_ARM_COPY 20 /* 32 bit word Copy symbol at dynamic link time. */ 298 #define R_ARM_GLOB_DAT 21 /* 32 bit word Create GOT entry. */ 299 #define R_ARM_JUMP_SLOT 22 /* 32 bit word Create PLT entry. */ 300 #define R_ARM_RELATIVE 23 /* 32 bit word Adjust by program base. */ 301 #define R_ARM_GOTOFF 24 /* 32 bit word Offset relative to start of GOT. */ 302 #define R_ARM_GOTPC 25 /* 32 bit word Insert address of GOT. */ 303 #define R_ARM_GOT32 26 /* 32 bit word Entry in GOT. */ 304 #define R_ARM_PLT32 27 /* ARM BL Entry in PLT. */ 305 306 /* 28-31 Reserved to ARM LINUX */ 307 #define R_ARM_ALU_PCREL_7_0 32 /* ARM ADD/SUB (S . P + A) & 0x000000FF */ 308 #define R_ARM_ALU_PCREL_15_8 33 /* ARM ADD/SUB (S . P + A) & 0x0000FF00 */ 309 #define R_ARM_ALU_PCREL_23_15 34 /* ARM ADD/SUB (S . P + A) & 0x00FF0000 */ 310 #define R_ARM_LDR_SBREL_11_0 35 /* ARM LDR/STR (S . B + A) & 0x00000FFF */ 311 #define R_ARM_ALU_SBREL_19_12 36 /* ARM ADD/SUB (S . B + A) & 0x000FF000 */ 312 #define R_ARM_ALU_SBREL_27_20 37 /* ARM ADD/SUB (S . B + A) & 0x0FF00000 */ 313 314 #define R_ARM_TARGET1 38 315 #define R_ARM_ROSEGREL32 39 316 #define R_ARM_V4BX 40 317 #define R_ARM_TARGET2 41 318 #define R_ARM_PREL31 42 319 320 /* 96-111, reserved to ARM g++ */ 321 #define R_ARM_GNU_VTENTRY 100 /* 32 bit word Record C++ vtable entry. */ 322 #define R_ARM_GNU_VTINHERIT 101 /* 32 bit word Record C++ member usage. */ 323 #define R_ARM_THM_PC11 102 /* Thumb B S . P + A */ 324 #define R_ARM_THM_PC9 103 /* Thumb B<cond> S . P + A */ 325 326 /* 112-127, reserved for private experiments */ 327 328 /* 128-248, reserved to ARM */ 329 #define R_ARM_RXPC25 249 /* ARM BLX (delta S . delta P) + A #define For calls between program segments. */ 330 #define R_ARM_RSBREL32 250 /* Word (delta S . delta SB) + A For an offset from SB, the static base. */ 331 #define R_ARM_THM_RPC22 251 /* Thumb BL/BLX pair (delta S . delta P) + A For calls between program segments. */ 332 #define R_ARM_RREL32 252 /* Word (delta S . delta P) + A For on offset between two segments. */ 333 #define R_ARM_RABS32 253 /* Word delta S + A For the address of a location in the target segment. */ 334 #define R_ARM_RPC24 254 /* ARM B/BL (delta S . delta P) + A For calls between program segments. */ 335 #define R_ARM_RBASE 255 /* None None.Identifies the segment being relocated by the following 336 relocation directives. The ARM EABI poses two problems for relocating 337 executables and shared objects encoded in */ 338 339 340 // shirait 341 #define R_ARM_LDR_PC_G0 4 //LDR 342 343 #define R_ARM_ABS12 6 //LDR, STR 344 345 #define R_ARM_THM_CALL 10 //Identical to R_ARM_THM_PC22 346 347 #define R_ARM_CALL 28 //BL/BLX 348 #define R_ARM_JUMP24 29 //B/BL<cond> 349 #define R_ARM_THM_JUMP24 30 350 351 #define R_ARM_MOVW_ABS_NC 43 //MOVW 352 #define R_ARM_MOVT_ABS 44 //MOVT 353 #define R_ARM_MOVW_PREL_NC 45 //MOVW 354 #define R_ARM_MOVT_PREL 46 //MOVT 355 356 #define R_ARM_ALU_PC_G0_NC 57 //ADD, SUB 357 #define R_ARM_ALU_PC_G0 58 //ADD, SUB 358 #define R_ARM_ALU_PC_G1_NC 59 //ADD, SUB 359 #define R_ARM_ALU_PC_G1 60 //ADD, SUB 360 #define R_ARM_ALU_PC_G2 61 //ADD, SUB 361 #define R_ARM_LDR_PC_G1 62 //LDR, STR, LDRB, STRB 362 #define R_ARM_LDR_PC_G2 63 //LDR, STR, LDRB, STRB 363 #define R_ARM_LDRS_PC_G0 64 //LDRD, STRD, LDRH, STRH, LDRSH, LDRSB 364 #define R_ARM_LDRS_PC_G1 65 //LDRD, STRD, LDRH, STRH, LDRSH, LDRSB 365 #define R_ARM_LDRS_PC_G2 66 //LDRD, STRD, LDRH, STRH, LDRSH, LDRSB 366 #define R_ARM_LDC_PC_G0 67 //LDC, STC 367 #define R_ARM_LDC_PC_G1 68 //LDC, STC 368 #define R_ARM_LDC_PC_G2 69 //LDC, STC 369 #define R_ARM_ALU_SB_G0_NC 70 //ADD, SUB 370 #define R_ARM_ALU_SB_G0 71 //ADD, SUB 371 #define R_ARM_ALU_SB_G1_NC 72 //ADD, SUB 372 #define R_ARM_ALU_SB_G1 73 //ADD, SUB 373 #define R_ARM_ALU_SB_G2 74 //ADD, SUB 374 #define R_ARM_LDR_SB_G0 75 //LDR, STR, LDRB, STRB 375 #define R_ARM_LDR_SB_G1 76 //LDR, STR, LDRB, STRB 376 #define R_ARM_LDR_SB_G2 77 //LDR, STR, LDRB, STRB 377 #define R_ARM_LDRS_SB_G0 78 //LDRD, STRD, LDRH, STRH, LDRSH, LDRSB 378 #define R_ARM_LDRS_SB_G1 79 //LDRD, STRD, LDRH, STRH, LDRSH, LDRSB 379 #define R_ARM_LDRS_SB_G2 80 //LDRD, STRD, LDRH, STRH, LDRSH, LDRSB 380 #define R_ARM_LDC_SB_G0 81 //LDC, STC 381 #define R_ARM_LDC_SB_G1 82 //LDC, STC 382 #define R_ARM_LDC_SB_G2 83 //LDC, STC 383 #define R_ARM_MOVW_BREL_NC 84 //MOVW 384 #define R_ARM_MOVT_BREL 85 //MOVT 385 #define R_ARM_MOVW_BREL 86 //MOVW 386 387 #define R_ARM_GOT_BREL12 97 //LDR 388 #define R_ARM_GOTOFF12 98 //LDR, STR 389 390 #define R_ARM_TLS_LDO12 109 //LDR, STR 391 #define R_ARM_TLS_LE12 110 //LDR, STR 392 #define R_ARM_TLS_TE12GP 111 //LDR 393 394 395 396 /*--------------------------------------------------------- 397 Dynamic Section elf_v1.2 398 --------------------------------------------------------*/ 399 typedef struct { 400 Elf32_Sword d_tag; 401 union { 402 Elf32_Word d_val; 403 Elf32_Addr d_ptr; 404 } d_un; 405 } Elf32_Dyn; 406 407 408 /* Additional symbol types for Thumb. */ 409 #define STT_ARM_TFUNC STT_LOPROC /* A Thumb function. */ 410 #define STT_ARM_16BIT STT_HIPROC /* A Thumb label. */ 411 412 413 414 415 416 417 418 419 420 /*--------------------------------------------------------- 421 Read out the ELF header 422 --------------------------------------------------------*/ 423 void *ELF_LoadELFHeader(const void *buf, Elf32_Ehdr *ehdr); 424 425 426 427 #endif /* ELF_H_ */ 428