Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /****************************    elf.h    ***********************************
  2. * Author:        Agner Fog
  3. * Date created:  2006-07-18
  4. * Last modified: 2009-07-15
  5. * Project:       objconv
  6. * Module:        elf.h
  7. * Description:
  8. * Header file for definition of structures in 32 and 64 bit ELF object file
  9. * format.
  10. *
  11. * Copyright 2006-2009 GNU General Public License http://www.gnu.org/licenses
  12. *****************************************************************************/
  13. #ifndef ELF_H
  14. #define ELF_H
  15.  
  16. /********************** FILE HEADER **********************/
  17.  
  18. struct Elf32_Ehdr {
  19.   uint8   e_ident[16];   // Magic number and other info
  20.   uint16  e_type;        // Object file type
  21.   uint16  e_machine;     // Architecture
  22.   uint32  e_version;     // Object file version
  23.   uint32  e_entry;       // Entry point virtual address
  24.   uint32  e_phoff;       // Program header table file offset
  25.   uint32  e_shoff;       // Section header table file offset
  26.   uint32  e_flags;       // Processor-specific flags
  27.   uint16  e_ehsize;      // ELF header size in bytes
  28.   uint16  e_phentsize;   // Program header table entry size
  29.   uint16  e_phnum;       // Program header table entry count
  30.   uint16  e_shentsize;   // Section header table entry size
  31.   uint16  e_shnum;       // Section header table entry count
  32.   uint16  e_shstrndx;    // Section header string table index
  33. };
  34.  
  35. struct Elf64_Ehdr {
  36.   uint8   e_ident[16];   // Magic number and other info
  37.   uint16  e_type;        // Object file type
  38.   uint16  e_machine;     // Architecture
  39.   uint32  e_version;     // Object file version
  40.   uint64  e_entry;       // Entry point virtual address
  41.   uint64  e_phoff;       // Program header table file offset
  42.   uint64  e_shoff;       // Section header table file offset
  43.   uint32  e_flags;       // Processor-specific flags
  44.   uint16  e_ehsize;      // ELF header size in bytes
  45.   uint16  e_phentsize;   // Program header table entry size
  46.   uint16  e_phnum;       // Program header table entry count
  47.   uint16  e_shentsize;   // Section header table entry size
  48.   uint16  e_shnum;       // Section header table entry count
  49.   uint16  e_shstrndx;    // Section header string table index
  50. };
  51.  
  52.  
  53. /* Fields in the e_ident array.  The EI_* macros are indices into the
  54.    array.  The macros under each EI_* macro are the values the byte
  55.    may have.  */
  56.  
  57. /* Conglomeration of the identification bytes, for easy testing as a word.  */
  58. #define ELFMAG        "\177ELF"
  59.  
  60. #define EI_CLASS      4    /* File class byte index */
  61. #define ELFCLASSNONE  0    /* Invalid class */
  62. #define ELFCLASS32    1    /* 32-bit objects */
  63. #define ELFCLASS64    2    /* 64-bit objects */
  64. #define ELFCLASSNUM   3
  65.  
  66. #define EI_DATA       5    /* Data encoding byte index */
  67. #define ELFDATANONE   0    /* Invalid data encoding */
  68. #define ELFDATA2LSB   1    /* 2's complement, little endian */
  69. #define ELFDATA2MSB   2    /* 2's complement, big endian */
  70. #define ELFDATANUM    3
  71.  
  72. #define EI_VERSION    6    /* File version byte index */
  73.           /* Value must be EV_CURRENT */
  74.  
  75. #define EI_OSABI  7         /* OS ABI identification */
  76. #define ELFOSABI_SYSV    0  /* UNIX System V ABI */
  77. #define ELFOSABI_HPUX    1  /* HP-UX */
  78. #define ELFOSABI_ARM    97  /* ARM */
  79. #define ELFOSABI_STANDALONE  255  /* Standalone (embedded) application */
  80.  
  81. #define EI_ABIVERSION    8    /* ABI version */
  82.  
  83. #define EI_PAD           9    /* Byte index of padding bytes */
  84.  
  85. /* Legal values for e_type (object file type).  */
  86.  
  87. #define ET_NONE          0    /* No file type */
  88. #define ET_REL           1    /* Relocatable file */
  89. #define ET_EXEC          2    /* Executable file */
  90. #define ET_DYN           3    /* Shared object file */
  91. #define ET_CORE          4    /* Core file */
  92. #define ET_NUM           5    /* Number of defined types */
  93. #define ET_LOOS     0xfe00    /* OS-specific range start */
  94. #define ET_HIOS     0xfeff    /* OS-specific range end */
  95. #define ET_LOPROC   0xff00    /* Processor-specific range start */
  96. #define ET_HIPROC   0xffff    /* Processor-specific range end */
  97.  
  98. /* Legal values for e_machine (architecture).  */
  99.  
  100. #define EM_NONE          0    /* No machine */
  101. #define EM_M32           1    /* AT&T WE 32100 */
  102. #define EM_SPARC         2    /* SUN SPARC */
  103. #define EM_386           3    /* Intel 80386 */
  104. #define EM_68K           4    /* Motorola m68k family */
  105. #define EM_88K           5    /* Motorola m88k family */
  106. #define EM_860           7    /* Intel 80860 */
  107. #define EM_MIPS          8    /* MIPS R3000 big-endian */
  108. #define EM_S370          9    /* IBM System/370 */
  109. #define EM_MIPS_RS3_LE  10    /* MIPS R3000 little-endian */
  110. #define EM_PARISC       15    /* HPPA */
  111. #define EM_VPP500       17    /* Fujitsu VPP500 */
  112. #define EM_SPARC32PLUS  18    /* Sun's "v8plus" */
  113. #define EM_960          19    /* Intel 80960 */
  114. #define EM_PPC          20    /* PowerPC */
  115. #define EM_PPC64        21    /* PowerPC 64-bit */
  116. #define EM_S390         22    /* IBM S390 */
  117. #define EM_V800         36    /* NEC V800 series */
  118. #define EM_FR20         37    /* Fujitsu FR20 */
  119. #define EM_RH32         38    /* TRW RH-32 */
  120. #define EM_RCE          39    /* Motorola RCE */
  121. #define EM_ARM          40    /* ARM */
  122. #define EM_FAKE_ALPHA   41    /* Digital Alpha */
  123. #define EM_SH           42    /* Hitachi SH */
  124. #define EM_SPARCV9      43    /* SPARC v9 64-bit */
  125. #define EM_TRICORE      44    /* Siemens Tricore */
  126. #define EM_ARC          45    /* Argonaut RISC Core */
  127. #define EM_H8_300       46    /* Hitachi H8/300 */
  128. #define EM_H8_300H      47    /* Hitachi H8/300H */
  129. #define EM_H8S          48    /* Hitachi H8S */
  130. #define EM_H8_500       49    /* Hitachi H8/500 */
  131. #define EM_IA_64        50    /* Intel Merced */
  132. #define EM_MIPS_X       51    /* Stanford MIPS-X */
  133. #define EM_COLDFIRE     52    /* Motorola Coldfire */
  134. #define EM_68HC12       53    /* Motorola M68HC12 */
  135. #define EM_MMA          54    /* Fujitsu MMA Multimedia Accelerator*/
  136. #define EM_PCP          55    /* Siemens PCP */
  137. #define EM_NCPU         56    /* Sony nCPU embeeded RISC */
  138. #define EM_NDR1         57    /* Denso NDR1 microprocessor */
  139. #define EM_STARCORE     58    /* Motorola Start*Core processor */
  140. #define EM_ME16         59    /* Toyota ME16 processor */
  141. #define EM_ST100        60    /* STMicroelectronic ST100 processor */
  142. #define EM_TINYJ        61    /* Advanced Logic Corp. Tinyj emb.fam*/
  143. #define EM_X86_64       62    /* AMD x86-64 architecture */
  144. #define EM_PDSP         63    /* Sony DSP Processor */
  145. #define EM_FX66         66    /* Siemens FX66 microcontroller */
  146. #define EM_ST9PLUS      67    /* STMicroelectronics ST9+ 8/16 mc */
  147. #define EM_ST7          68    /* STmicroelectronics ST7 8 bit mc */
  148. #define EM_68HC16       69    /* Motorola MC68HC16 microcontroller */
  149. #define EM_68HC11       70    /* Motorola MC68HC11 microcontroller */
  150. #define EM_68HC08       71    /* Motorola MC68HC08 microcontroller */
  151. #define EM_68HC05       72    /* Motorola MC68HC05 microcontroller */
  152. #define EM_SVX          73    /* Silicon Graphics SVx */
  153. #define EM_AT19         74    /* STMicroelectronics ST19 8 bit mc */
  154. #define EM_VAX          75    /* Digital VAX */
  155. #define EM_CRIS         76    /* Axis Communications 32-bit embedded processor */
  156. #define EM_JAVELIN      77    /* Infineon Technologies 32-bit embedded processor */
  157. #define EM_FIREPATH     78    /* Element 14 64-bit DSP Processor */
  158. #define EM_ZSP          79    /* LSI Logic 16-bit DSP Processor */
  159. #define EM_MMIX         80    /* Donald Knuth's educational 64-bit processor */
  160. #define EM_HUANY        81    /* Harvard University machine-independent object files */
  161. #define EM_PRISM        82    /* SiTera Prism */
  162. #define EM_AVR          83    /* Atmel AVR 8-bit microcontroller */
  163. #define EM_FR30         84    /* Fujitsu FR30 */
  164. #define EM_D10V         85    /* Mitsubishi D10V */
  165. #define EM_D30V         86    /* Mitsubishi D30V */
  166. #define EM_V850         87    /* NEC v850 */
  167. #define EM_M32R         88    /* Mitsubishi M32R */
  168. #define EM_MN10300      89    /* Matsushita MN10300 */
  169. #define EM_MN10200      90    /* Matsushita MN10200 */
  170. #define EM_PJ           91    /* picoJava */
  171. #define EM_NUM          92
  172. #define EM_ALPHA    0x9026
  173.  
  174. /* Legal values for e_version (version).  */
  175.  
  176. #define EV_NONE          0    /* Invalid ELF version */
  177. #define EV_CURRENT       1    /* Current version */
  178. #define EV_NUM           2
  179.  
  180. /* Section header.  */
  181.  
  182. struct Elf32_Shdr {
  183.   uint32  sh_name;      // Section name (string tbl index)
  184.   uint32  sh_type;      // Section type
  185.   uint32  sh_flags;     // Section flags
  186.   uint32  sh_addr;      // Section virtual addr at execution
  187.   uint32  sh_offset;    // Section file offset
  188.   uint32  sh_size;      // Section size in bytes
  189.   uint32  sh_link;      // Link to another section
  190.   uint32  sh_info;      // Additional section information
  191.   uint32  sh_addralign; // Section alignment
  192.   uint32  sh_entsize;   // Entry size if section holds table
  193. };
  194.  
  195. struct Elf64_Shdr {
  196.   uint32  sh_name;      // Section name (string tbl index)
  197.   uint32  sh_type;      // Section type
  198.   uint64  sh_flags;     // Section flags
  199.   uint64  sh_addr;      // Section virtual addr at execution
  200.   uint64  sh_offset;    // Section file offset
  201.   uint64  sh_size;      // Section size in bytes
  202.   uint32  sh_link;      // Link to another section
  203.   uint32  sh_info;      // Additional section information
  204.   uint64  sh_addralign; // Section alignment
  205.   uint64  sh_entsize;   // Entry size if section holds table
  206. };
  207.  
  208.  
  209. /* Special section indices.  */
  210.  
  211. #define SHN_UNDEF                   0  // Undefined section
  212. #define SHN_LORESERVE  ((int16)0xff00) // Start of reserved indices
  213. #define SHN_LOPROC     ((int16)0xff00) // Start of processor-specific
  214. #define SHN_HIPROC     ((int16)0xff1f) // End of processor-specific
  215. #define SHN_LOOS       ((int16)0xff20) // Start of OS-specific
  216. #define SHN_HIOS       ((int16)0xff3f) // End of OS-specific
  217. #define SHN_ABS        ((int16)0xfff1) // Associated symbol is absolute
  218. #define SHN_COMMON     ((int16)0xfff2) // Associated symbol is common
  219. #define SHN_XINDEX     ((int16)0xffff) // Index is in extra table
  220. #define SHN_HIRESERVE  ((int16)0xffff) // End of reserved indices
  221.  
  222. // Legal values for sh_type (section type).
  223.  
  224. #define SHT_NULL                    0  // Section header table entry unused
  225. #define SHT_PROGBITS                1  // Program data
  226. #define SHT_SYMTAB                  2  // Symbol table
  227. #define SHT_STRTAB                  3  // String table
  228. #define SHT_RELA                    4  // Relocation entries with addends. Warning: Works only in 64 bit mode in my tests!
  229. #define SHT_HASH                    5  // Symbol hash table
  230. #define SHT_DYNAMIC                 6  // Dynamic linking information
  231. #define SHT_NOTE                    7  // Notes
  232. #define SHT_NOBITS                  8  // Program space with no data (bss)
  233. #define SHT_REL                     9  // Relocation entries, no addends
  234. #define SHT_SHLIB                  10  // Reserved
  235. #define SHT_DYNSYM                 11  // Dynamic linker symbol table
  236. #define SHT_INIT_ARRAY             14  // Array of constructors
  237. #define SHT_FINI_ARRAY             15  // Array of destructors
  238. #define SHT_PREINIT_ARRAY          16  // Array of pre-constructors
  239. #define SHT_GROUP                  17  // Section group
  240. #define SHT_SYMTAB_SHNDX           18  // Extended section indeces
  241. #define SHT_NUM                    19  // Number of defined types.
  242. #define SHT_LOOS           0x60000000  // Start OS-specific
  243. #define SHT_CHECKSUM       0x6ffffff8  // Checksum for DSO content.
  244. #define SHT_LOSUNW         0x6ffffffa  // Sun-specific low bound.
  245. #define SHT_SUNW_move      0x6ffffffa
  246. #define SHT_SUNW_COMDAT    0x6ffffffb
  247. #define SHT_SUNW_syminfo   0x6ffffffc
  248. #define SHT_GNU_verdef     0x6ffffffd  // Version definition section.
  249. #define SHT_GNU_verneed    0x6ffffffe  // Version needs section.
  250. #define SHT_GNU_versym     0x6fffffff  // Version symbol table.
  251. #define SHT_HISUNW         0x6fffffff  // Sun-specific high bound.
  252. #define SHT_HIOS           0x6fffffff  // End OS-specific type
  253. #define SHT_LOPROC         0x70000000  // Start of processor-specific
  254. #define SHT_HIPROC         0x7fffffff  // End of processor-specific
  255. #define SHT_LOUSER         0x80000000  // Start of application-specific
  256. #define SHT_HIUSER         0x8fffffff  // End of application-specific
  257. #define SHT_REMOVE_ME      0xffffff99  // Specific to objconv program: Removed debug or exception handler section
  258.  
  259. // Legal values for sh_flags (section flags).
  260.  
  261. #define SHF_WRITE            (1 << 0)  // Writable
  262. #define SHF_ALLOC            (1 << 1)  // Occupies memory during execution
  263. #define SHF_EXECINSTR        (1 << 2)  // Executable
  264. #define SHF_MERGE            (1 << 4)  // Might be merged
  265. #define SHF_STRINGS          (1 << 5)  // Contains nul-terminated strings
  266. #define SHF_INFO_LINK        (1 << 6)  // `sh_info' contains SHT index
  267. #define SHF_LINK_ORDER       (1 << 7)  // Preserve order after combining
  268. #define SHF_OS_NONCONFORMING (1 << 8)  // Non-standard OS specific handling required
  269. #define SHF_MASKOS         0x0ff00000  // OS-specific.
  270. #define SHF_MASKPROC       0xf0000000  // Processor-specific
  271.  
  272. /* Section group handling.  */
  273. #define GRP_COMDAT  0x1    /* Mark group as COMDAT.  */
  274.  
  275.  
  276. /* Symbol table entry.  */
  277.  
  278. struct Elf32_Sym {
  279.   uint32  st_name;       // Symbol name (string tbl index)
  280.   uint32  st_value;      // Symbol value
  281.   uint32  st_size;       // Symbol size
  282.   uint8   st_type: 4,    // Symbol type
  283.           st_bind: 4;    // Symbol binding
  284.   uint8   st_other;      // Symbol visibility
  285.   uint16  st_shndx;      // Section index
  286. };
  287.  
  288. struct Elf64_Sym {
  289.   uint32  st_name;       // Symbol name (string tbl index)
  290.   uint8   st_type: 4,    // Symbol type
  291.           st_bind: 4;    // Symbol binding
  292.   uint8   st_other;      // Symbol visibility
  293.   uint16  st_shndx;      // Section index
  294.   uint64  st_value;      // Symbol value
  295.   uint64  st_size;       // Symbol size
  296. };
  297.  
  298.  
  299. /* The syminfo section if available contains additional information about
  300.    every dynamic symbol.  */
  301.  
  302. struct Elf32_Syminfo {
  303.   uint16 si_boundto;    /* Direct bindings, symbol bound to */
  304.   uint16 si_flags;      /* Per symbol flags */
  305. };
  306.  
  307. struct Elf64_Syminfo {
  308.   uint16 si_boundto;    /* Direct bindings, symbol bound to */
  309.   uint16 si_flags;      /* Per symbol flags */
  310. };
  311.  
  312. /* Possible values for si_boundto.  */
  313. #define SYMINFO_BT_SELF        0xffff  /* Symbol bound to self */
  314. #define SYMINFO_BT_PARENT      0xfffe  /* Symbol bound to parent */
  315. #define SYMINFO_BT_LOWRESERVE  0xff00  /* Beginning of reserved entries */
  316.  
  317. /* Possible bitmasks for si_flags.  */
  318. #define SYMINFO_FLG_DIRECT     0x0001  /* Direct bound symbol */
  319. #define SYMINFO_FLG_PASSTHRU   0x0002  /* Pass-thru symbol for translator */
  320. #define SYMINFO_FLG_COPY       0x0004  /* Symbol is a copy-reloc */
  321. #define SYMINFO_FLG_LAZYLOAD   0x0008  /* Symbol bound to object to be lazy loaded */
  322. /* Syminfo version values.  */
  323. #define SYMINFO_NONE                0
  324. #define SYMINFO_CURRENT             1
  325. #define SYMINFO_NUM                 2
  326.  
  327.  
  328. /* Special section index.  */
  329.  
  330. #define SHN_UNDEF  0    /* No section, undefined symbol.  */
  331.  
  332. // How to extract and insert information held in the st_info field.
  333. // Both Elf32_Sym and Elf64_Sym use the same one-byte st_info field.
  334.  
  335. //#define ELF32_ST_BIND(val)        (((uint8) (val)) >> 4)
  336. //#define ELF32_ST_TYPE(val)        ((val) & 0xf)
  337. //#define ELF32_ST_INFO(bind,type)  (((bind) << 4) + ((type) & 0xf))
  338.  
  339. // Legal values for ST_BIND subfield of st_info (symbol binding).
  340.  
  341. #define STB_LOCAL    0    // Local symbol
  342. #define STB_GLOBAL   1    // Global symbol
  343. #define STB_WEAK     2    // Weak symbol
  344. #define STB_NUM      3    // Number of defined types.
  345. #define STB_LOOS    10    // Start of OS-specific
  346. #define STB_HIOS    12    // End of OS-specific
  347. #define STB_LOPROC  13    // Start of processor-specific
  348. #define STB_HIPROC  15    // End of processor-specific
  349.  
  350. // Legal values for ST_TYPE subfield of st_info (symbol type).
  351.  
  352. #define STT_NOTYPE   0    // Symbol type is unspecified
  353. #define STT_OBJECT   1    // Symbol is a data object
  354. #define STT_FUNC     2    // Symbol is a code object
  355. #define STT_SECTION  3    // Symbol associated with a section
  356. #define STT_FILE     4    // Symbol's name is file name
  357. #define STT_COMMON   5    // Symbol is a common data object
  358. #define STT_NUM      6    // Number of defined types.
  359. #define STT_LOOS    10    // Start of OS-specific
  360. #define STT_GNU_IFUNC 10  // Symbol is an indirect code object (function dispatcher)
  361. #define STT_HIOS    12    // End of OS-specific
  362. #define STT_LOPROC  13    // Start of processor-specific
  363. #define STT_HIPROC  15    // End of processor-specific
  364.  
  365.  
  366. /* Symbol table indices are found in the hash buckets and chain table
  367.    of a symbol hash table section.  This special index value indicates
  368.    the end of a chain, meaning no further symbols are found in that bucket.  */
  369.  
  370. #define STN_UNDEF  0    /* End of a chain.  */
  371.  
  372.  
  373. /* How to extract and insert information held in the st_other field.  */
  374.  
  375. #define ELF32_ST_VISIBILITY(o)  ((o) & 0x03)
  376.  
  377. /* For ELF64 the definitions are the same.  */
  378. #define ELF64_ST_VISIBILITY(o)  ELF32_ST_VISIBILITY (o)
  379.  
  380. /* Symbol visibility specification encoded in the st_other field.  */
  381. #define STV_DEFAULT    0    /* Default symbol visibility rules */
  382. #define STV_INTERNAL   1    /* Processor specific hidden class */
  383. #define STV_HIDDEN     2    /* Sym unavailable in other modules */
  384. #define STV_PROTECTED  3    /* Not preemptible, not exported */
  385.  
  386.  
  387. // Relocation table entry structures
  388. // How to extract and insert information held in the r_info field.
  389. //#define ELF32_R_SYM(val)        ((val) >> 8)
  390. //#define ELF32_R_TYPE(val)       ((val) & 0xff)
  391. //#define ELF32_R_INFO(sym,type)  (((sym) << 8) + ((type) & 0xff))
  392.  
  393. //#define ELF64_R_SYM(i)          ((uint32)((i) >> 32))
  394. //#define ELF64_R_TYPE(i)         ((i) & 0xffffffff)
  395. //#define ELF64_R_INFO(sym,type)  ((((uint64) (sym)) << 32) + (type))
  396.  
  397.  
  398. // Relocation table entry without addend (in section of type SHT_REL)
  399. struct Elf32_Rel {
  400.   uint32  r_offset;             // Address
  401.   uint32  r_type: 8,            // Relocation type
  402.           r_sym: 24;            // Symbol index
  403. };
  404.  
  405. struct Elf64_Rel {
  406.   uint64  r_offset;             // Address
  407.   uint32  r_type;               // Relocation type
  408.   uint32  r_sym;                // Symbol index
  409. };
  410.  
  411. // Relocation table entry with addend (in section of type SHT_RELA)
  412.  
  413. // Warning: Elf32_Rela doesn't work in any of the systems I have tried.
  414. // Use Elf32_Rel instead with addend in relocated field.
  415. // Use Elf64_Rela in 64 bit mode. Elf64_Rel not accepted?
  416.  
  417. struct Elf32_Rela {
  418.   uint32  r_offset;               // Address
  419.   uint32  r_type: 8,              // Relocation type
  420.           r_sym: 24;              // Symbol index
  421.   int32   r_addend;               // Addend
  422. };
  423.  
  424. struct Elf64_Rela {
  425.   uint64  r_offset;               // Address
  426.   uint32  r_type;                 // Relocation type
  427.   uint32  r_sym;                  // Symbol index
  428.   int64   r_addend;               // Addend
  429. };
  430.  
  431. // i386 Relocation types
  432.  
  433. #define R_386_NONE      0    // No reloc
  434. #define R_386_32        1    // Direct 32 bit
  435. #define R_386_PC32      2    // Self-relative 32 bit (not EIP relative in the sense used in COFF files)
  436. #define R_386_GOT32     3    // 32 bit GOT entry
  437. #define R_386_PLT32     4    // 32 bit PLT address
  438. #define R_386_COPY      5    // Copy symbol at runtime
  439. #define R_386_GLOB_DAT  6    // Create GOT entry
  440. #define R_386_JMP_SLOT  7    // Create PLT entry
  441. #define R_386_RELATIVE  8    // Adjust by program base
  442. #define R_386_GOTOFF    9    // 32 bit offset to GOT
  443. #define R_386_GOTPC    10    // 32 bit self relative offset to GOT
  444. #define R_386_IRELATIVE 42   // Reference to PLT entry of indirect function (STT_GNU_IFUNC)
  445. //#define R_386_NUM      11    // Number of entries
  446.  
  447. // AMD x86-64 relocation types
  448. #define R_X86_64_NONE       0  // No reloc
  449. #define R_X86_64_64         1  // Direct 64 bit
  450. #define R_X86_64_PC32       2  // Self relative 32 bit signed (not RIP relative in the sense used in COFF files)
  451. #define R_X86_64_GOT32      3  // 32 bit GOT entry
  452. #define R_X86_64_PLT32      4  // 32 bit PLT address
  453. #define R_X86_64_COPY       5  // Copy symbol at runtime
  454. #define R_X86_64_GLOB_DAT   6  // Create GOT entry
  455. #define R_X86_64_JUMP_SLOT  7  // Create PLT entry
  456. #define R_X86_64_RELATIVE   8  // Adjust by program base
  457. #define R_X86_64_GOTPCREL   9  // 32 bit signed self relative offset to GOT
  458. #define R_X86_64_32        10  // Direct 32 bit zero extended
  459. #define R_X86_64_32S       11  // Direct 32 bit sign extended
  460. #define R_X86_64_16        12  // Direct 16 bit zero extended
  461. #define R_X86_64_PC16      13  // 16 bit sign extended self relative
  462. #define R_X86_64_8         14  // Direct 8 bit sign extended
  463. #define R_X86_64_PC8       15  // 8 bit sign extended self relative
  464. #define R_X86_64_IRELATIVE 37  // Reference to PLT entry of indirect function (STT_GNU_IFUNC)
  465. //#define R_X86_64_NUM       16  // Number of entries
  466. // Pseudo-record when ELF is used as intermediary between COFF and MachO:
  467. #define R_UNSUPPORTED_IMAGEREL 21  // Image-relative not supported
  468.  
  469.  
  470.  
  471. // Program segment header.
  472.  
  473. struct Elf32_Phdr {
  474.   uint32  p_type;      /* Segment type */
  475.   uint32  p_offset;    /* Segment file offset */
  476.   uint32  p_vaddr;     /* Segment virtual address */
  477.   uint32  p_paddr;     /* Segment physical address */
  478.   uint32  p_filesz;    /* Segment size in file */
  479.   uint32  p_memsz;     /* Segment size in memory */
  480.   uint32  p_flags;     /* Segment flags */
  481.   uint32  p_align;     /* Segment alignment */
  482. };
  483.  
  484. struct Elf64_Phdr {
  485.   uint32  p_type;      /* Segment type */
  486.   uint32  p_flags;     /* Segment flags */
  487.   uint64  p_offset;    /* Segment file offset */
  488.   uint64  p_vaddr;     /* Segment virtual address */
  489.   uint64  p_paddr;     /* Segment physical address */
  490.   uint64  p_filesz;    /* Segment size in file */
  491.   uint64  p_memsz;     /* Segment size in memory */
  492.   uint64  p_align;     /* Segment alignment */
  493. };
  494.  
  495. /* Legal values for p_type (segment type).  */
  496.  
  497. #define PT_NULL             0    /* Program header table entry unused */
  498. #define PT_LOAD             1    /* Loadable program segment */
  499. #define PT_DYNAMIC          2    /* Dynamic linking information */
  500. #define PT_INTERP           3    /* Program interpreter */
  501. #define PT_NOTE             4    /* Auxiliary information */
  502. #define PT_SHLIB            5    /* Reserved */
  503. #define PT_PHDR             6    /* Entry for header table itself */
  504. #define PT_NUM              7    /* Number of defined types */
  505. #define PT_LOOS    0x60000000    /* Start of OS-specific */
  506. #define PT_HIOS    0x6fffffff    /* End of OS-specific */
  507. #define PT_LOPROC  0x70000000    /* Start of processor-specific */
  508. #define PT_HIPROC  0x7fffffff    /* End of processor-specific */
  509.  
  510. /* Legal values for p_flags (segment flags).  */
  511.  
  512. #define PF_X           (1 << 0)  /* Segment is executable */
  513. #define PF_W           (1 << 1)  /* Segment is writable */
  514. #define PF_R           (1 << 2)  /* Segment is readable */
  515. #define PF_MASKOS    0x0ff00000  /* OS-specific */
  516. #define PF_MASKPROC  0xf0000000  /* Processor-specific */
  517.  
  518. /* Legal values for note segment descriptor types for core files. */
  519.  
  520. #define NT_PRSTATUS    1    /* Contains copy of prstatus struct */
  521. #define NT_FPREGSET    2    /* Contains copy of fpregset struct */
  522. #define NT_PRPSINFO    3    /* Contains copy of prpsinfo struct */
  523. #define NT_PRXREG      4    /* Contains copy of prxregset struct */
  524. #define NT_PLATFORM    5    /* String from sysinfo(SI_PLATFORM) */
  525. #define NT_AUXV        6    /* Contains copy of auxv array */
  526. #define NT_GWINDOWS    7    /* Contains copy of gwindows struct */
  527. #define NT_PSTATUS    10    /* Contains copy of pstatus struct */
  528. #define NT_PSINFO     13    /* Contains copy of psinfo struct */
  529. #define NT_PRCRED     14    /* Contains copy of prcred struct */
  530. #define NT_UTSNAME    15    /* Contains copy of utsname struct */
  531. #define NT_LWPSTATUS  16    /* Contains copy of lwpstatus struct */
  532. #define NT_LWPSINFO   17    /* Contains copy of lwpinfo struct */
  533. #define NT_PRFPXREG   20    /* Contains copy of fprxregset struct*/
  534.  
  535. /* Legal values for the note segment descriptor types for object files.  */
  536.  
  537. #define NT_VERSION  1    /* Contains a version string.  */
  538.  
  539.  
  540. /* Dynamic section entry.  */
  541.  
  542. struct Elf32_Dyn {
  543.   int32  d_tag;          /* Dynamic entry type */
  544.   union   {
  545.       uint32 d_val;      /* Integer value */
  546.       uint32 d_ptr;      /* Address value */
  547.     } d_un;
  548. };
  549.  
  550. struct Elf64_Dyn {
  551.   int64  d_tag;          /* Dynamic entry type */
  552.   union    {
  553.       uint64 d_val;      /* Integer value */
  554.       uint64 d_ptr;      /* Address value */
  555.     } d_un;
  556. };
  557.  
  558. /* Legal values for d_tag (dynamic entry type).  */
  559.  
  560. #define DT_NULL             0    /* Marks end of dynamic section */
  561. #define DT_NEEDED           1    /* Name of needed library */
  562. #define DT_PLTRELSZ         2    /* Size in bytes of PLT relocs */
  563. #define DT_PLTGOT           3    /* Processor defined value */
  564. #define DT_HASH             4    /* Address of symbol hash table */
  565. #define DT_STRTAB           5    /* Address of string table */
  566. #define DT_SYMTAB           6    /* Address of symbol table */
  567. #define DT_RELA             7    /* Address of Rela relocs */
  568. #define DT_RELASZ           8    /* Total size of Rela relocs */
  569. #define DT_RELAENT          9    /* Size of one Rela reloc */
  570. #define DT_STRSZ           10    /* Size of string table */
  571. #define DT_SYMENT          11    /* Size of one symbol table entry */
  572. #define DT_INIT            12    /* Address of init function */
  573. #define DT_FINI            13    /* Address of termination function */
  574. #define DT_SONAME          14    /* Name of shared object */
  575. #define DT_RPATH           15    /* Library search path (deprecated) */
  576. #define DT_SYMBOLIC        16    /* Start symbol search here */
  577. #define DT_REL             17    /* Address of Rel relocs */
  578. #define DT_RELSZ           18    /* Total size of Rel relocs */
  579. #define DT_RELENT          19    /* Size of one Rel reloc */
  580. #define DT_PLTREL          20    /* Type of reloc in PLT */
  581. #define DT_DEBUG           21    /* For debugging; unspecified */
  582. #define DT_TEXTREL         22    /* Reloc might modify .text */
  583. #define DT_JMPREL          23    /* Address of PLT relocs */
  584. #define DT_BIND_NOW        24    /* Process relocations of object */
  585. #define DT_INIT_ARRAY      25    /* Array with addresses of init fct */
  586. #define DT_FINI_ARRAY      26    /* Array with addresses of fini fct */
  587. #define DT_INIT_ARRAYSZ    27    /* Size in bytes of DT_INIT_ARRAY */
  588. #define DT_FINI_ARRAYSZ    28    /* Size in bytes of DT_FINI_ARRAY */
  589. #define DT_RUNPATH         29    /* Library search path */
  590. #define DT_FLAGS           30    /* Flags for the object being loaded */
  591. #define DT_ENCODING        32    /* Start of encoded range */
  592. #define DT_PREINIT_ARRAY   32    /* Array with addresses of preinit fct*/
  593. #define DT_PREINIT_ARRAYSZ 33    /* size in bytes of DT_PREINIT_ARRAY */
  594. #define DT_NUM             34    /* Number used */
  595. #define DT_LOOS    0x60000000    /* Start of OS-specific */
  596. #define DT_HIOS    0x6fffffff    /* End of OS-specific */
  597. #define DT_LOPROC  0x70000000    /* Start of processor-specific */
  598. #define DT_HIPROC  0x7fffffff    /* End of processor-specific */
  599. #define DT_PROCNUM DT_MIPS_NUM  /* Most used by any processor */
  600.  
  601. /* DT_* entries which fall between DT_VALRNGHI & DT_VALRNGLO use the
  602.    Dyn.d_un.d_val field of the Elf*_Dyn structure.  This follows Sun's
  603.    approach.  */
  604. #define DT_VALRNGLO   0x6ffffd00
  605. #define DT_CHECKSUM   0x6ffffdf8
  606. #define DT_PLTPADSZ   0x6ffffdf9
  607. #define DT_MOVEENT    0x6ffffdfa
  608. #define DT_MOVESZ     0x6ffffdfb
  609. #define DT_FEATURE_1  0x6ffffdfc  /* Feature selection (DTF_*).  */
  610. #define DT_POSFLAG_1  0x6ffffdfd  /* Flags for DT_* entries, effecting the following DT_* entry.  */
  611. #define DT_SYMINSZ    0x6ffffdfe  /* Size of syminfo table (in bytes) */
  612. #define DT_SYMINENT   0x6ffffdff  /* Entry size of syminfo */
  613. #define DT_VALRNGHI   0x6ffffdff
  614.  
  615. /* DT_* entries which fall between DT_ADDRRNGHI & DT_ADDRRNGLO use the
  616.    Dyn.d_un.d_ptr field of the Elf*_Dyn structure.
  617.  
  618.    If any adjustment is made to the ELF object after it has been
  619.    built these entries will need to be adjusted.  */
  620. #define DT_ADDRRNGLO  0x6ffffe00
  621. #define DT_SYMINFO    0x6ffffeff  /* syminfo table */
  622. #define DT_ADDRRNGHI  0x6ffffeff
  623.  
  624. /* The versioning entry types.  The next are defined as part of the GNU extension.  */
  625. #define DT_VERSYM     0x6ffffff0
  626.  
  627. #define DT_RELACOUNT  0x6ffffff9
  628. #define DT_RELCOUNT   0x6ffffffa
  629.  
  630. /* These were chosen by Sun.  */
  631. #define DT_FLAGS_1    0x6ffffffb  /* State flags, see DF_1_* below.  */
  632. #define DT_VERDEF     0x6ffffffc  /* Address of version definition table */
  633. #define DT_VERDEFNUM  0x6ffffffd  /* Number of version definitions */
  634. #define DT_VERNEED    0x6ffffffe  /* Address of table with needed versions */
  635. #define DT_VERNEEDNUM 0x6fffffff  /* Number of needed versions */
  636. #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag))  /* Reverse order! */
  637. #define DT_VERSIONTAGNUM 16
  638.  
  639. /* Sun added these machine-independent extensions in the "processor-specific"
  640.    range.  Be compatible.  */
  641. #define DT_AUXILIARY    0x7ffffffd      /* Shared object to load before self */
  642. #define DT_FILTER       0x7fffffff      /* Shared object to get values from */
  643. #define DT_EXTRATAGIDX(tag)  ((uint32)-((int32) (tag) <<1>>1)-1)
  644. #define DT_EXTRANUM  3
  645.  
  646. /* Values of `d_un.d_val' in the DT_FLAGS entry.  */
  647. #define DF_ORIGIN    0x00000001  /* Object may use DF_ORIGIN */
  648. #define DF_SYMBOLIC  0x00000002  /* Symbol resolutions starts here */
  649. #define DF_TEXTREL   0x00000004  /* Object contains text relocations */
  650. #define DF_BIND_NOW  0x00000008  /* No lazy binding for this object */
  651.  
  652. /* State flags selectable in the `d_un.d_val' element of the DT_FLAGS_1
  653.    entry in the dynamic section.  */
  654. #define DF_1_NOW        0x00000001  /* Set RTLD_NOW for this object.  */
  655. #define DF_1_GLOBAL     0x00000002  /* Set RTLD_GLOBAL for this object.  */
  656. #define DF_1_GROUP      0x00000004  /* Set RTLD_GROUP for this object.  */
  657. #define DF_1_NODELETE   0x00000008  /* Set RTLD_NODELETE for this object.*/
  658. #define DF_1_LOADFLTR   0x00000010  /* Trigger filtee loading at runtime.*/
  659. #define DF_1_INITFIRST  0x00000020  /* Set RTLD_INITFIRST for this object*/
  660. #define DF_1_NOOPEN     0x00000040  /* Set RTLD_NOOPEN for this object.  */
  661. #define DF_1_ORIGIN     0x00000080  /* $ORIGIN must be handled.  */
  662. #define DF_1_DIRECT     0x00000100  /* Direct binding enabled.  */
  663. #define DF_1_TRANS      0x00000200
  664. #define DF_1_INTERPOSE  0x00000400  /* Object is used to interpose.  */
  665. #define DF_1_NODEFLIB   0x00000800  /* Ignore default lib search path.  */
  666. #define DF_1_NODUMP     0x00001000
  667. #define DF_1_CONFALT    0x00002000
  668. #define DF_1_ENDFILTEE  0x00004000
  669.  
  670. /* Flags for the feature selection in DT_FEATURE_1.  */
  671. #define DTF_1_PARINIT   0x00000001
  672. #define DTF_1_CONFEXP   0x00000002
  673.  
  674. /* Flags in the DT_POSFLAG_1 entry effecting only the next DT_* entry.  */
  675. #define DF_P1_LAZYLOAD  0x00000001  /* Lazyload following object.  */
  676. #define DF_P1_GROUPPERM 0x00000002  /* Symbols from next object are not generally available.  */
  677.  
  678. /* Version definition sections.  */
  679.  
  680. struct Elf32_Verdef  {
  681.   uint16  vd_version;  /* Version revision */
  682.   uint16  vd_flags;    /* Version information */
  683.   uint16  vd_ndx;      /* Version Index */
  684.   uint16  vd_cnt;      /* Number of associated aux entries */
  685.   uint32  vd_hash;     /* Version name hash value */
  686.   uint32  vd_aux;      /* Offset in bytes to verdaux array */
  687.   uint32  vd_next;     /* Offset in bytes to next verdef entry */
  688. };
  689.  
  690. struct Elf64_Verdef {
  691.   uint16  vd_version;  /* Version revision */
  692.   uint16  vd_flags;    /* Version information */
  693.   uint16  vd_ndx;      /* Version Index */
  694.   uint16  vd_cnt;      /* Number of associated aux entries */
  695.   uint32  vd_hash;     /* Version name hash value */
  696.   uint32  vd_aux;      /* Offset in bytes to verdaux array */
  697.   uint32  vd_next;     /* Offset in bytes to next verdef entry */
  698. };
  699.  
  700.  
  701. /* Legal values for vd_version (version revision).  */
  702. #define VER_DEF_NONE     0    /* No version */
  703. #define VER_DEF_CURRENT  1    /* Current version */
  704. #define VER_DEF_NUM      2    /* Given version number */
  705.  
  706. /* Legal values for vd_flags (version information flags).  */
  707. #define VER_FLG_BASE   0x1    /* Version definition of file itself */
  708. #define VER_FLG_WEAK   0x2    /* Weak version identifier */
  709.  
  710. /* Auxialiary version information.  */
  711.  
  712. struct Elf32_Verdaux {
  713.   uint32  vda_name;    /* Version or dependency names */
  714.   uint32  vda_next;    /* Offset in bytes to next verdaux entry */
  715. };
  716.  
  717. struct Elf64_Verdaux {
  718.   uint32  vda_name;    /* Version or dependency names */
  719.   uint32  vda_next;    /* Offset in bytes to next verdaux entry */
  720. };
  721.  
  722.  
  723. /* Version dependency section.  */
  724.  
  725. struct Elf32_Verneed {
  726.   uint16  vn_version;    /* Version of structure */
  727.   uint16  vn_cnt;        /* Number of associated aux entries */
  728.   uint32  vn_file;       /* Offset of filename for this dependency */
  729.   uint32  vn_aux;        /* Offset in bytes to vernaux array */
  730.   uint32  vn_next;       /* Offset in bytes to next verneed entry */
  731. };
  732.  
  733. struct Elf64_Verneed {
  734.   uint16  vn_version;    /* Version of structure */
  735.   uint16  vn_cnt;        /* Number of associated aux entries */
  736.   uint32  vn_file;       /* Offset of filename for this dependency */
  737.   uint32  vn_aux;        /* Offset in bytes to vernaux array */
  738.   uint32  vn_next;       /* Offset in bytes to next verneed entry */
  739. };
  740.  
  741.  
  742. /* Legal values for vn_version (version revision).  */
  743. #define VER_NEED_NONE    0    /* No version */
  744. #define VER_NEED_CURRENT 1    /* Current version */
  745. #define VER_NEED_NUM     2    /* Given version number */
  746.  
  747. /* Auxiliary needed version information.  */
  748.  
  749. struct Elf32_Vernaux {
  750.   uint32  vna_hash;     /* Hash value of dependency name */
  751.   uint16  vna_flags;    /* Dependency specific information */
  752.   uint16  vna_other;    /* Unused */
  753.   uint32  vna_name;     /* Dependency name string offset */
  754.   uint32  vna_next;     /* Offset in bytes to next vernaux entry */
  755. };
  756.  
  757. struct Elf64_Vernaux {
  758.   uint32  vna_hash;     /* Hash value of dependency name */
  759.   uint16  vna_flags;    /* Dependency specific information */
  760.   uint16  vna_other;    /* Unused */
  761.   uint32  vna_name;     /* Dependency name string offset */
  762.   uint32  vna_next;     /* Offset in bytes to next vernaux entry */
  763. };
  764.  
  765.  
  766. /* Legal values for vna_flags.  */
  767. #define VER_FLG_WEAK  0x2    /* Weak version identifier */
  768.  
  769.  
  770. /* Note section contents.  Each entry in the note section begins with
  771.    a header of a fixed form.  */
  772.  
  773. struct Elf32_Nhdr {
  774.   uint32 n_namesz;      /* Length of the note's name.  */
  775.   uint32 n_descsz;      /* Length of the note's descriptor.  */
  776.   uint32 n_type;        /* Type of the note.  */
  777. };
  778.  
  779. struct Elf64_Nhdr {
  780.   uint32 n_namesz;      /* Length of the note's name.  */
  781.   uint32 n_descsz;      /* Length of the note's descriptor.  */
  782.   uint32 n_type;        /* Type of the note.  */
  783. };
  784.  
  785. /* Known names of notes.  */
  786.  
  787. /* Solaris entries in the note section have this name.  */
  788. #define ELF_NOTE_SOLARIS  "SUNW Solaris"
  789.  
  790. /* Note entries for GNU systems have this name.  */
  791. #define ELF_NOTE_GNU      "GNU"
  792.  
  793.  
  794. /* Defined types of notes for Solaris.  */
  795.  
  796. /* Value of descriptor (one word) is desired pagesize for the binary.  */
  797. #define ELF_NOTE_PAGESIZE_HINT  1
  798.  
  799.  
  800. /* Defined note types for GNU systems.  */
  801.  
  802. /* ABI information.  The descriptor consists of words:
  803.    word 0: OS descriptor
  804.    word 1: major version of the ABI
  805.    word 2: minor version of the ABI
  806.    word 3: subminor version of the ABI
  807. */
  808. #define ELF_NOTE_ABI    1
  809.  
  810. /* Known OSes.  These value can appear in word 0 of an ELF_NOTE_ABI
  811.    note section entry.  */
  812. #define ELF_NOTE_OS_LINUX     0
  813. #define ELF_NOTE_OS_GNU       1
  814. #define ELF_NOTE_OS_SOLARIS2  2
  815.  
  816.  
  817. /* Move records.  */
  818. struct Elf32_Move {
  819.   uint64 m_value;      /* Symbol value.  */
  820.   uint32 m_info;       /* Size and index.  */
  821.   uint32 m_poffset;    /* Symbol offset.  */
  822.   uint16 m_repeat;     /* Repeat count.  */
  823.   uint16 m_stride;     /* Stride info.  */
  824. };
  825.  
  826. struct Elf64_Move {
  827.   uint64 m_value;     /* Symbol value.  */
  828.   uint64 m_info;      /* Size and index.  */
  829.   uint64 m_poffset;   /* Symbol offset.  */
  830.   uint16 m_repeat;    /* Repeat count.  */
  831.   uint16 m_stride;    /* Stride info.  */
  832. };
  833.  
  834. /* Macro to construct move records.  */
  835. #define ELF32_M_SYM(info)        ((info) >> 8)
  836. #define ELF32_M_SIZE(info)       ((uint8) (info))
  837. #define ELF32_M_INFO(sym, size)  (((sym) << 8) + (uint8) (size))
  838.  
  839. #define ELF64_M_SYM(info)        ELF32_M_SYM (info)
  840. #define ELF64_M_SIZE(info)       ELF32_M_SIZE (info)
  841. #define ELF64_M_INFO(sym, size)  ELF32_M_INFO (sym, size)
  842.  
  843.  
  844. /********************** Strings **********************/
  845. #define ELF_CONSTRUCTOR_NAME    ".ctors"   // Name of constructors segment
  846.  
  847.  
  848. // Macros listing all word-size dependent structures, used as template parameter list
  849. #define ELFSTRUCTURES    TELF_Header, TELF_SectionHeader, TELF_Symbol, TELF_Relocation
  850. #define ELF32STRUCTURES  Elf32_Ehdr, Elf32_Shdr, Elf32_Sym, Elf32_Rela
  851. #define ELF64STRUCTURES  Elf64_Ehdr, Elf64_Shdr, Elf64_Sym, Elf64_Rela
  852.  
  853. #endif // #ifndef ELF_H
  854.