ELF.h 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. //===-- llvm/Support/ELF.h - ELF constants and data structures --*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This header contains common, non-processor-specific data structures and
  11. // constants for the ELF file format.
  12. //
  13. // The details of the ELF32 bits in this file are largely based on the Tool
  14. // Interface Standard (TIS) Executable and Linking Format (ELF) Specification
  15. // Version 1.2, May 1995. The ELF64 stuff is based on ELF-64 Object File Format
  16. // Version 1.5, Draft 2, May 1998 as well as OpenBSD header files.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_SUPPORT_ELF_H
  20. #define LLVM_SUPPORT_ELF_H
  21. #include "llvm/Support/Compiler.h"
  22. #include "llvm/Support/DataTypes.h"
  23. #include <cstring>
  24. namespace llvm {
  25. namespace ELF {
  26. typedef uint32_t Elf32_Addr; // Program address
  27. typedef uint32_t Elf32_Off; // File offset
  28. typedef uint16_t Elf32_Half;
  29. typedef uint32_t Elf32_Word;
  30. typedef int32_t Elf32_Sword;
  31. typedef uint64_t Elf64_Addr;
  32. typedef uint64_t Elf64_Off;
  33. typedef uint16_t Elf64_Half;
  34. typedef uint32_t Elf64_Word;
  35. typedef int32_t Elf64_Sword;
  36. typedef uint64_t Elf64_Xword;
  37. typedef int64_t Elf64_Sxword;
  38. // Object file magic string.
  39. static const char ElfMagic[] = { 0x7f, 'E', 'L', 'F', '\0' };
  40. // e_ident size and indices.
  41. enum {
  42. EI_MAG0 = 0, // File identification index.
  43. EI_MAG1 = 1, // File identification index.
  44. EI_MAG2 = 2, // File identification index.
  45. EI_MAG3 = 3, // File identification index.
  46. EI_CLASS = 4, // File class.
  47. EI_DATA = 5, // Data encoding.
  48. EI_VERSION = 6, // File version.
  49. EI_OSABI = 7, // OS/ABI identification.
  50. EI_ABIVERSION = 8, // ABI version.
  51. EI_PAD = 9, // Start of padding bytes.
  52. EI_NIDENT = 16 // Number of bytes in e_ident.
  53. };
  54. struct Elf32_Ehdr {
  55. unsigned char e_ident[EI_NIDENT]; // ELF Identification bytes
  56. Elf32_Half e_type; // Type of file (see ET_* below)
  57. Elf32_Half e_machine; // Required architecture for this file (see EM_*)
  58. Elf32_Word e_version; // Must be equal to 1
  59. Elf32_Addr e_entry; // Address to jump to in order to start program
  60. Elf32_Off e_phoff; // Program header table's file offset, in bytes
  61. Elf32_Off e_shoff; // Section header table's file offset, in bytes
  62. Elf32_Word e_flags; // Processor-specific flags
  63. Elf32_Half e_ehsize; // Size of ELF header, in bytes
  64. Elf32_Half e_phentsize; // Size of an entry in the program header table
  65. Elf32_Half e_phnum; // Number of entries in the program header table
  66. Elf32_Half e_shentsize; // Size of an entry in the section header table
  67. Elf32_Half e_shnum; // Number of entries in the section header table
  68. Elf32_Half e_shstrndx; // Sect hdr table index of sect name string table
  69. bool checkMagic() const {
  70. return (memcmp(e_ident, ElfMagic, strlen(ElfMagic))) == 0;
  71. }
  72. unsigned char getFileClass() const { return e_ident[EI_CLASS]; }
  73. unsigned char getDataEncoding() const { return e_ident[EI_DATA]; }
  74. };
  75. // 64-bit ELF header. Fields are the same as for ELF32, but with different
  76. // types (see above).
  77. struct Elf64_Ehdr {
  78. unsigned char e_ident[EI_NIDENT];
  79. Elf64_Half e_type;
  80. Elf64_Half e_machine;
  81. Elf64_Word e_version;
  82. Elf64_Addr e_entry;
  83. Elf64_Off e_phoff;
  84. Elf64_Off e_shoff;
  85. Elf64_Word e_flags;
  86. Elf64_Half e_ehsize;
  87. Elf64_Half e_phentsize;
  88. Elf64_Half e_phnum;
  89. Elf64_Half e_shentsize;
  90. Elf64_Half e_shnum;
  91. Elf64_Half e_shstrndx;
  92. bool checkMagic() const {
  93. return (memcmp(e_ident, ElfMagic, strlen(ElfMagic))) == 0;
  94. }
  95. unsigned char getFileClass() const { return e_ident[EI_CLASS]; }
  96. unsigned char getDataEncoding() const { return e_ident[EI_DATA]; }
  97. };
  98. // File types
  99. enum {
  100. ET_NONE = 0, // No file type
  101. ET_REL = 1, // Relocatable file
  102. ET_EXEC = 2, // Executable file
  103. ET_DYN = 3, // Shared object file
  104. ET_CORE = 4, // Core file
  105. ET_LOPROC = 0xff00, // Beginning of processor-specific codes
  106. ET_HIPROC = 0xffff // Processor-specific
  107. };
  108. // Versioning
  109. enum {
  110. EV_NONE = 0,
  111. EV_CURRENT = 1
  112. };
  113. // Machine architectures
  114. // See current registered ELF machine architectures at:
  115. // http://www.uxsglobal.com/developers/gabi/latest/ch4.eheader.html
  116. enum {
  117. EM_NONE = 0, // No machine
  118. EM_M32 = 1, // AT&T WE 32100
  119. EM_SPARC = 2, // SPARC
  120. EM_386 = 3, // Intel 386
  121. EM_68K = 4, // Motorola 68000
  122. EM_88K = 5, // Motorola 88000
  123. EM_IAMCU = 6, // Intel MCU
  124. EM_860 = 7, // Intel 80860
  125. EM_MIPS = 8, // MIPS R3000
  126. EM_S370 = 9, // IBM System/370
  127. EM_MIPS_RS3_LE = 10, // MIPS RS3000 Little-endian
  128. EM_PARISC = 15, // Hewlett-Packard PA-RISC
  129. EM_VPP500 = 17, // Fujitsu VPP500
  130. EM_SPARC32PLUS = 18, // Enhanced instruction set SPARC
  131. EM_960 = 19, // Intel 80960
  132. EM_PPC = 20, // PowerPC
  133. EM_PPC64 = 21, // PowerPC64
  134. EM_S390 = 22, // IBM System/390
  135. EM_SPU = 23, // IBM SPU/SPC
  136. EM_V800 = 36, // NEC V800
  137. EM_FR20 = 37, // Fujitsu FR20
  138. EM_RH32 = 38, // TRW RH-32
  139. EM_RCE = 39, // Motorola RCE
  140. EM_ARM = 40, // ARM
  141. EM_ALPHA = 41, // DEC Alpha
  142. EM_SH = 42, // Hitachi SH
  143. EM_SPARCV9 = 43, // SPARC V9
  144. EM_TRICORE = 44, // Siemens TriCore
  145. EM_ARC = 45, // Argonaut RISC Core
  146. EM_H8_300 = 46, // Hitachi H8/300
  147. EM_H8_300H = 47, // Hitachi H8/300H
  148. EM_H8S = 48, // Hitachi H8S
  149. EM_H8_500 = 49, // Hitachi H8/500
  150. EM_IA_64 = 50, // Intel IA-64 processor architecture
  151. EM_MIPS_X = 51, // Stanford MIPS-X
  152. EM_COLDFIRE = 52, // Motorola ColdFire
  153. EM_68HC12 = 53, // Motorola M68HC12
  154. EM_MMA = 54, // Fujitsu MMA Multimedia Accelerator
  155. EM_PCP = 55, // Siemens PCP
  156. EM_NCPU = 56, // Sony nCPU embedded RISC processor
  157. EM_NDR1 = 57, // Denso NDR1 microprocessor
  158. EM_STARCORE = 58, // Motorola Star*Core processor
  159. EM_ME16 = 59, // Toyota ME16 processor
  160. EM_ST100 = 60, // STMicroelectronics ST100 processor
  161. EM_TINYJ = 61, // Advanced Logic Corp. TinyJ embedded processor family
  162. EM_X86_64 = 62, // AMD x86-64 architecture
  163. EM_PDSP = 63, // Sony DSP Processor
  164. EM_PDP10 = 64, // Digital Equipment Corp. PDP-10
  165. EM_PDP11 = 65, // Digital Equipment Corp. PDP-11
  166. EM_FX66 = 66, // Siemens FX66 microcontroller
  167. EM_ST9PLUS = 67, // STMicroelectronics ST9+ 8/16 bit microcontroller
  168. EM_ST7 = 68, // STMicroelectronics ST7 8-bit microcontroller
  169. EM_68HC16 = 69, // Motorola MC68HC16 Microcontroller
  170. EM_68HC11 = 70, // Motorola MC68HC11 Microcontroller
  171. EM_68HC08 = 71, // Motorola MC68HC08 Microcontroller
  172. EM_68HC05 = 72, // Motorola MC68HC05 Microcontroller
  173. EM_SVX = 73, // Silicon Graphics SVx
  174. EM_ST19 = 74, // STMicroelectronics ST19 8-bit microcontroller
  175. EM_VAX = 75, // Digital VAX
  176. EM_CRIS = 76, // Axis Communications 32-bit embedded processor
  177. EM_JAVELIN = 77, // Infineon Technologies 32-bit embedded processor
  178. EM_FIREPATH = 78, // Element 14 64-bit DSP Processor
  179. EM_ZSP = 79, // LSI Logic 16-bit DSP Processor
  180. EM_MMIX = 80, // Donald Knuth's educational 64-bit processor
  181. EM_HUANY = 81, // Harvard University machine-independent object files
  182. EM_PRISM = 82, // SiTera Prism
  183. EM_AVR = 83, // Atmel AVR 8-bit microcontroller
  184. EM_FR30 = 84, // Fujitsu FR30
  185. EM_D10V = 85, // Mitsubishi D10V
  186. EM_D30V = 86, // Mitsubishi D30V
  187. EM_V850 = 87, // NEC v850
  188. EM_M32R = 88, // Mitsubishi M32R
  189. EM_MN10300 = 89, // Matsushita MN10300
  190. EM_MN10200 = 90, // Matsushita MN10200
  191. EM_PJ = 91, // picoJava
  192. EM_OPENRISC = 92, // OpenRISC 32-bit embedded processor
  193. EM_ARC_COMPACT = 93, // ARC International ARCompact processor (old
  194. // spelling/synonym: EM_ARC_A5)
  195. EM_XTENSA = 94, // Tensilica Xtensa Architecture
  196. EM_VIDEOCORE = 95, // Alphamosaic VideoCore processor
  197. EM_TMM_GPP = 96, // Thompson Multimedia General Purpose Processor
  198. EM_NS32K = 97, // National Semiconductor 32000 series
  199. EM_TPC = 98, // Tenor Network TPC processor
  200. EM_SNP1K = 99, // Trebia SNP 1000 processor
  201. EM_ST200 = 100, // STMicroelectronics (www.st.com) ST200
  202. EM_IP2K = 101, // Ubicom IP2xxx microcontroller family
  203. EM_MAX = 102, // MAX Processor
  204. EM_CR = 103, // National Semiconductor CompactRISC microprocessor
  205. EM_F2MC16 = 104, // Fujitsu F2MC16
  206. EM_MSP430 = 105, // Texas Instruments embedded microcontroller msp430
  207. EM_BLACKFIN = 106, // Analog Devices Blackfin (DSP) processor
  208. EM_SE_C33 = 107, // S1C33 Family of Seiko Epson processors
  209. EM_SEP = 108, // Sharp embedded microprocessor
  210. EM_ARCA = 109, // Arca RISC Microprocessor
  211. EM_UNICORE = 110, // Microprocessor series from PKU-Unity Ltd. and MPRC
  212. // of Peking University
  213. EM_EXCESS = 111, // eXcess: 16/32/64-bit configurable embedded CPU
  214. EM_DXP = 112, // Icera Semiconductor Inc. Deep Execution Processor
  215. EM_ALTERA_NIOS2 = 113, // Altera Nios II soft-core processor
  216. EM_CRX = 114, // National Semiconductor CompactRISC CRX
  217. EM_XGATE = 115, // Motorola XGATE embedded processor
  218. EM_C166 = 116, // Infineon C16x/XC16x processor
  219. EM_M16C = 117, // Renesas M16C series microprocessors
  220. EM_DSPIC30F = 118, // Microchip Technology dsPIC30F Digital Signal
  221. // Controller
  222. EM_CE = 119, // Freescale Communication Engine RISC core
  223. EM_M32C = 120, // Renesas M32C series microprocessors
  224. EM_TSK3000 = 131, // Altium TSK3000 core
  225. EM_RS08 = 132, // Freescale RS08 embedded processor
  226. EM_SHARC = 133, // Analog Devices SHARC family of 32-bit DSP
  227. // processors
  228. EM_ECOG2 = 134, // Cyan Technology eCOG2 microprocessor
  229. EM_SCORE7 = 135, // Sunplus S+core7 RISC processor
  230. EM_DSP24 = 136, // New Japan Radio (NJR) 24-bit DSP Processor
  231. EM_VIDEOCORE3 = 137, // Broadcom VideoCore III processor
  232. EM_LATTICEMICO32 = 138, // RISC processor for Lattice FPGA architecture
  233. EM_SE_C17 = 139, // Seiko Epson C17 family
  234. EM_TI_C6000 = 140, // The Texas Instruments TMS320C6000 DSP family
  235. EM_TI_C2000 = 141, // The Texas Instruments TMS320C2000 DSP family
  236. EM_TI_C5500 = 142, // The Texas Instruments TMS320C55x DSP family
  237. EM_MMDSP_PLUS = 160, // STMicroelectronics 64bit VLIW Data Signal Processor
  238. EM_CYPRESS_M8C = 161, // Cypress M8C microprocessor
  239. EM_R32C = 162, // Renesas R32C series microprocessors
  240. EM_TRIMEDIA = 163, // NXP Semiconductors TriMedia architecture family
  241. EM_HEXAGON = 164, // Qualcomm Hexagon processor
  242. EM_8051 = 165, // Intel 8051 and variants
  243. EM_STXP7X = 166, // STMicroelectronics STxP7x family of configurable
  244. // and extensible RISC processors
  245. EM_NDS32 = 167, // Andes Technology compact code size embedded RISC
  246. // processor family
  247. EM_ECOG1 = 168, // Cyan Technology eCOG1X family
  248. EM_ECOG1X = 168, // Cyan Technology eCOG1X family
  249. EM_MAXQ30 = 169, // Dallas Semiconductor MAXQ30 Core Micro-controllers
  250. EM_XIMO16 = 170, // New Japan Radio (NJR) 16-bit DSP Processor
  251. EM_MANIK = 171, // M2000 Reconfigurable RISC Microprocessor
  252. EM_CRAYNV2 = 172, // Cray Inc. NV2 vector architecture
  253. EM_RX = 173, // Renesas RX family
  254. EM_METAG = 174, // Imagination Technologies META processor
  255. // architecture
  256. EM_MCST_ELBRUS = 175, // MCST Elbrus general purpose hardware architecture
  257. EM_ECOG16 = 176, // Cyan Technology eCOG16 family
  258. EM_CR16 = 177, // National Semiconductor CompactRISC CR16 16-bit
  259. // microprocessor
  260. EM_ETPU = 178, // Freescale Extended Time Processing Unit
  261. EM_SLE9X = 179, // Infineon Technologies SLE9X core
  262. EM_L10M = 180, // Intel L10M
  263. EM_K10M = 181, // Intel K10M
  264. EM_AARCH64 = 183, // ARM AArch64
  265. EM_AVR32 = 185, // Atmel Corporation 32-bit microprocessor family
  266. EM_STM8 = 186, // STMicroeletronics STM8 8-bit microcontroller
  267. EM_TILE64 = 187, // Tilera TILE64 multicore architecture family
  268. EM_TILEPRO = 188, // Tilera TILEPro multicore architecture family
  269. EM_CUDA = 190, // NVIDIA CUDA architecture
  270. EM_TILEGX = 191, // Tilera TILE-Gx multicore architecture family
  271. EM_CLOUDSHIELD = 192, // CloudShield architecture family
  272. EM_COREA_1ST = 193, // KIPO-KAIST Core-A 1st generation processor family
  273. EM_COREA_2ND = 194, // KIPO-KAIST Core-A 2nd generation processor family
  274. EM_ARC_COMPACT2 = 195, // Synopsys ARCompact V2
  275. EM_OPEN8 = 196, // Open8 8-bit RISC soft processor core
  276. EM_RL78 = 197, // Renesas RL78 family
  277. EM_VIDEOCORE5 = 198, // Broadcom VideoCore V processor
  278. EM_78KOR = 199, // Renesas 78KOR family
  279. EM_56800EX = 200, // Freescale 56800EX Digital Signal Controller (DSC)
  280. EM_BA1 = 201, // Beyond BA1 CPU architecture
  281. EM_BA2 = 202, // Beyond BA2 CPU architecture
  282. EM_XCORE = 203, // XMOS xCORE processor family
  283. EM_MCHP_PIC = 204, // Microchip 8-bit PIC(r) family
  284. EM_INTEL205 = 205, // Reserved by Intel
  285. EM_INTEL206 = 206, // Reserved by Intel
  286. EM_INTEL207 = 207, // Reserved by Intel
  287. EM_INTEL208 = 208, // Reserved by Intel
  288. EM_INTEL209 = 209, // Reserved by Intel
  289. EM_KM32 = 210, // KM211 KM32 32-bit processor
  290. EM_KMX32 = 211, // KM211 KMX32 32-bit processor
  291. EM_KMX16 = 212, // KM211 KMX16 16-bit processor
  292. EM_KMX8 = 213, // KM211 KMX8 8-bit processor
  293. EM_KVARC = 214, // KM211 KVARC processor
  294. EM_CDP = 215, // Paneve CDP architecture family
  295. EM_COGE = 216, // Cognitive Smart Memory Processor
  296. EM_COOL = 217, // iCelero CoolEngine
  297. EM_NORC = 218, // Nanoradio Optimized RISC
  298. EM_CSR_KALIMBA = 219, // CSR Kalimba architecture family
  299. EM_AMDGPU = 224 // AMD GPU architecture
  300. };
  301. // Object file classes.
  302. enum {
  303. ELFCLASSNONE = 0,
  304. ELFCLASS32 = 1, // 32-bit object file
  305. ELFCLASS64 = 2 // 64-bit object file
  306. };
  307. // Object file byte orderings.
  308. enum {
  309. ELFDATANONE = 0, // Invalid data encoding.
  310. ELFDATA2LSB = 1, // Little-endian object file
  311. ELFDATA2MSB = 2 // Big-endian object file
  312. };
  313. // OS ABI identification.
  314. enum {
  315. ELFOSABI_NONE = 0, // UNIX System V ABI
  316. ELFOSABI_HPUX = 1, // HP-UX operating system
  317. ELFOSABI_NETBSD = 2, // NetBSD
  318. ELFOSABI_GNU = 3, // GNU/Linux
  319. ELFOSABI_LINUX = 3, // Historical alias for ELFOSABI_GNU.
  320. ELFOSABI_HURD = 4, // GNU/Hurd
  321. ELFOSABI_SOLARIS = 6, // Solaris
  322. ELFOSABI_AIX = 7, // AIX
  323. ELFOSABI_IRIX = 8, // IRIX
  324. ELFOSABI_FREEBSD = 9, // FreeBSD
  325. ELFOSABI_TRU64 = 10, // TRU64 UNIX
  326. ELFOSABI_MODESTO = 11, // Novell Modesto
  327. ELFOSABI_OPENBSD = 12, // OpenBSD
  328. ELFOSABI_OPENVMS = 13, // OpenVMS
  329. ELFOSABI_NSK = 14, // Hewlett-Packard Non-Stop Kernel
  330. ELFOSABI_AROS = 15, // AROS
  331. ELFOSABI_FENIXOS = 16, // FenixOS
  332. ELFOSABI_CLOUDABI = 17, // Nuxi CloudABI
  333. ELFOSABI_C6000_ELFABI = 64, // Bare-metal TMS320C6000
  334. ELFOSABI_AMDGPU_HSA = 64, // AMD HSA runtime
  335. ELFOSABI_C6000_LINUX = 65, // Linux TMS320C6000
  336. ELFOSABI_ARM = 97, // ARM
  337. ELFOSABI_STANDALONE = 255 // Standalone (embedded) application
  338. };
  339. #define ELF_RELOC(name, value) name = value,
  340. // X86_64 relocations.
  341. enum {
  342. #include "ELFRelocs/x86_64.def"
  343. };
  344. // i386 relocations.
  345. enum {
  346. #include "ELFRelocs/i386.def"
  347. };
  348. // ELF Relocation types for PPC32
  349. enum {
  350. #include "ELFRelocs/PowerPC.def"
  351. };
  352. // Specific e_flags for PPC64
  353. enum {
  354. // e_flags bits specifying ABI:
  355. // 1 for original ABI using function descriptors,
  356. // 2 for revised ABI without function descriptors,
  357. // 0 for unspecified or not using any features affected by the differences.
  358. EF_PPC64_ABI = 3
  359. };
  360. // Special values for the st_other field in the symbol table entry for PPC64.
  361. enum {
  362. STO_PPC64_LOCAL_BIT = 5,
  363. STO_PPC64_LOCAL_MASK = (7 << STO_PPC64_LOCAL_BIT)
  364. };
  365. static inline int64_t
  366. decodePPC64LocalEntryOffset(unsigned Other) {
  367. unsigned Val = (Other & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT;
  368. return ((1 << Val) >> 2) << 2;
  369. }
  370. static inline unsigned
  371. encodePPC64LocalEntryOffset(int64_t Offset) {
  372. unsigned Val = (Offset >= 4 * 4
  373. ? (Offset >= 8 * 4
  374. ? (Offset >= 16 * 4 ? 6 : 5)
  375. : 4)
  376. : (Offset >= 2 * 4
  377. ? 3
  378. : (Offset >= 1 * 4 ? 2 : 0)));
  379. return Val << STO_PPC64_LOCAL_BIT;
  380. }
  381. // ELF Relocation types for PPC64
  382. enum {
  383. #include "ELFRelocs/PowerPC64.def"
  384. };
  385. // ELF Relocation types for AArch64
  386. enum {
  387. #include "ELFRelocs/AArch64.def"
  388. };
  389. // ARM Specific e_flags
  390. enum : unsigned {
  391. EF_ARM_SOFT_FLOAT = 0x00000200U,
  392. EF_ARM_VFP_FLOAT = 0x00000400U,
  393. EF_ARM_EABI_UNKNOWN = 0x00000000U,
  394. EF_ARM_EABI_VER1 = 0x01000000U,
  395. EF_ARM_EABI_VER2 = 0x02000000U,
  396. EF_ARM_EABI_VER3 = 0x03000000U,
  397. EF_ARM_EABI_VER4 = 0x04000000U,
  398. EF_ARM_EABI_VER5 = 0x05000000U,
  399. EF_ARM_EABIMASK = 0xFF000000U
  400. };
  401. // ELF Relocation types for ARM
  402. enum {
  403. #include "ELFRelocs/ARM.def"
  404. };
  405. // Mips Specific e_flags
  406. enum : unsigned {
  407. EF_MIPS_NOREORDER = 0x00000001, // Don't reorder instructions
  408. EF_MIPS_PIC = 0x00000002, // Position independent code
  409. EF_MIPS_CPIC = 0x00000004, // Call object with Position independent code
  410. EF_MIPS_ABI2 = 0x00000020, // File uses N32 ABI
  411. EF_MIPS_32BITMODE = 0x00000100, // Code compiled for a 64-bit machine
  412. // in 32-bit mode
  413. EF_MIPS_FP64 = 0x00000200, // Code compiled for a 32-bit machine
  414. // but uses 64-bit FP registers
  415. EF_MIPS_NAN2008 = 0x00000400, // Uses IEE 754-2008 NaN encoding
  416. // ABI flags
  417. EF_MIPS_ABI_O32 = 0x00001000, // This file follows the first MIPS 32 bit ABI
  418. EF_MIPS_ABI_O64 = 0x00002000, // O32 ABI extended for 64-bit architecture.
  419. EF_MIPS_ABI_EABI32 = 0x00003000, // EABI in 32 bit mode.
  420. EF_MIPS_ABI_EABI64 = 0x00004000, // EABI in 64 bit mode.
  421. EF_MIPS_ABI = 0x0000f000, // Mask for selecting EF_MIPS_ABI_ variant.
  422. // MIPS machine variant
  423. EF_MIPS_MACH_3900 = 0x00810000, // Toshiba R3900
  424. EF_MIPS_MACH_4010 = 0x00820000, // LSI R4010
  425. EF_MIPS_MACH_4100 = 0x00830000, // NEC VR4100
  426. EF_MIPS_MACH_4650 = 0x00850000, // MIPS R4650
  427. EF_MIPS_MACH_4120 = 0x00870000, // NEC VR4120
  428. EF_MIPS_MACH_4111 = 0x00880000, // NEC VR4111/VR4181
  429. EF_MIPS_MACH_SB1 = 0x008a0000, // Broadcom SB-1
  430. EF_MIPS_MACH_OCTEON = 0x008b0000, // Cavium Networks Octeon
  431. EF_MIPS_MACH_XLR = 0x008c0000, // RMI Xlr
  432. EF_MIPS_MACH_OCTEON2 = 0x008d0000, // Cavium Networks Octeon2
  433. EF_MIPS_MACH_OCTEON3 = 0x008e0000, // Cavium Networks Octeon3
  434. EF_MIPS_MACH_5400 = 0x00910000, // NEC VR5400
  435. EF_MIPS_MACH_5900 = 0x00920000, // MIPS R5900
  436. EF_MIPS_MACH_5500 = 0x00980000, // NEC VR5500
  437. EF_MIPS_MACH_9000 = 0x00990000, // Unknown
  438. EF_MIPS_MACH_LS2E = 0x00a00000, // ST Microelectronics Loongson 2E
  439. EF_MIPS_MACH_LS2F = 0x00a10000, // ST Microelectronics Loongson 2F
  440. EF_MIPS_MACH_LS3A = 0x00a20000, // Loongson 3A
  441. EF_MIPS_MACH = 0x00ff0000, // EF_MIPS_MACH_xxx selection mask
  442. // ARCH_ASE
  443. EF_MIPS_MICROMIPS = 0x02000000, // microMIPS
  444. EF_MIPS_ARCH_ASE_M16 =
  445. 0x04000000, // Has Mips-16 ISA extensions
  446. EF_MIPS_ARCH_ASE_MDMX =
  447. 0x08000000, // Has MDMX multimedia extensions
  448. EF_MIPS_ARCH_ASE = 0x0f000000, // Mask for EF_MIPS_ARCH_ASE_xxx flags
  449. // ARCH
  450. EF_MIPS_ARCH_1 = 0x00000000, // MIPS1 instruction set
  451. EF_MIPS_ARCH_2 = 0x10000000, // MIPS2 instruction set
  452. EF_MIPS_ARCH_3 = 0x20000000, // MIPS3 instruction set
  453. EF_MIPS_ARCH_4 = 0x30000000, // MIPS4 instruction set
  454. EF_MIPS_ARCH_5 = 0x40000000, // MIPS5 instruction set
  455. EF_MIPS_ARCH_32 = 0x50000000, // MIPS32 instruction set per linux not elf.h
  456. EF_MIPS_ARCH_64 = 0x60000000, // MIPS64 instruction set per linux not elf.h
  457. EF_MIPS_ARCH_32R2 = 0x70000000, // mips32r2, mips32r3, mips32r5
  458. EF_MIPS_ARCH_64R2 = 0x80000000, // mips64r2, mips64r3, mips64r5
  459. EF_MIPS_ARCH_32R6 = 0x90000000, // mips32r6
  460. EF_MIPS_ARCH_64R6 = 0xa0000000, // mips64r6
  461. EF_MIPS_ARCH = 0xf0000000 // Mask for applying EF_MIPS_ARCH_ variant
  462. };
  463. // ELF Relocation types for Mips
  464. enum {
  465. #include "ELFRelocs/Mips.def"
  466. };
  467. // Special values for the st_other field in the symbol table entry for MIPS.
  468. enum {
  469. STO_MIPS_OPTIONAL = 0x04, // Symbol whose definition is optional
  470. STO_MIPS_PLT = 0x08, // PLT entry related dynamic table record
  471. STO_MIPS_PIC = 0x20, // PIC func in an object mixes PIC/non-PIC
  472. STO_MIPS_MICROMIPS = 0x80, // MIPS Specific ISA for MicroMips
  473. STO_MIPS_MIPS16 = 0xf0 // MIPS Specific ISA for Mips16
  474. };
  475. // .MIPS.options section descriptor kinds
  476. enum {
  477. ODK_NULL = 0, // Undefined
  478. ODK_REGINFO = 1, // Register usage information
  479. ODK_EXCEPTIONS = 2, // Exception processing options
  480. ODK_PAD = 3, // Section padding options
  481. ODK_HWPATCH = 4, // Hardware patches applied
  482. ODK_FILL = 5, // Linker fill value
  483. ODK_TAGS = 6, // Space for tool identification
  484. ODK_HWAND = 7, // Hardware AND patches applied
  485. ODK_HWOR = 8, // Hardware OR patches applied
  486. ODK_GP_GROUP = 9, // GP group to use for text/data sections
  487. ODK_IDENT = 10, // ID information
  488. ODK_PAGESIZE = 11 // Page size information
  489. };
  490. // Hexagon Specific e_flags
  491. // Release 5 ABI
  492. enum {
  493. // Object processor version flags, bits[3:0]
  494. EF_HEXAGON_MACH_V2 = 0x00000001, // Hexagon V2
  495. EF_HEXAGON_MACH_V3 = 0x00000002, // Hexagon V3
  496. EF_HEXAGON_MACH_V4 = 0x00000003, // Hexagon V4
  497. EF_HEXAGON_MACH_V5 = 0x00000004, // Hexagon V5
  498. // Highest ISA version flags
  499. EF_HEXAGON_ISA_MACH = 0x00000000, // Same as specified in bits[3:0]
  500. // of e_flags
  501. EF_HEXAGON_ISA_V2 = 0x00000010, // Hexagon V2 ISA
  502. EF_HEXAGON_ISA_V3 = 0x00000020, // Hexagon V3 ISA
  503. EF_HEXAGON_ISA_V4 = 0x00000030, // Hexagon V4 ISA
  504. EF_HEXAGON_ISA_V5 = 0x00000040 // Hexagon V5 ISA
  505. };
  506. // Hexagon specific Section indexes for common small data
  507. // Release 5 ABI
  508. enum {
  509. SHN_HEXAGON_SCOMMON = 0xff00, // Other access sizes
  510. SHN_HEXAGON_SCOMMON_1 = 0xff01, // Byte-sized access
  511. SHN_HEXAGON_SCOMMON_2 = 0xff02, // Half-word-sized access
  512. SHN_HEXAGON_SCOMMON_4 = 0xff03, // Word-sized access
  513. SHN_HEXAGON_SCOMMON_8 = 0xff04 // Double-word-size access
  514. };
  515. // ELF Relocation types for Hexagon
  516. enum {
  517. #include "ELFRelocs/Hexagon.def"
  518. };
  519. // ELF Relocation types for S390/zSeries
  520. enum {
  521. #include "ELFRelocs/SystemZ.def"
  522. };
  523. // ELF Relocation type for Sparc.
  524. enum {
  525. #include "ELFRelocs/Sparc.def"
  526. };
  527. #undef ELF_RELOC
  528. // Section header.
  529. struct Elf32_Shdr {
  530. Elf32_Word sh_name; // Section name (index into string table)
  531. Elf32_Word sh_type; // Section type (SHT_*)
  532. Elf32_Word sh_flags; // Section flags (SHF_*)
  533. Elf32_Addr sh_addr; // Address where section is to be loaded
  534. Elf32_Off sh_offset; // File offset of section data, in bytes
  535. Elf32_Word sh_size; // Size of section, in bytes
  536. Elf32_Word sh_link; // Section type-specific header table index link
  537. Elf32_Word sh_info; // Section type-specific extra information
  538. Elf32_Word sh_addralign; // Section address alignment
  539. Elf32_Word sh_entsize; // Size of records contained within the section
  540. };
  541. // Section header for ELF64 - same fields as ELF32, different types.
  542. struct Elf64_Shdr {
  543. Elf64_Word sh_name;
  544. Elf64_Word sh_type;
  545. Elf64_Xword sh_flags;
  546. Elf64_Addr sh_addr;
  547. Elf64_Off sh_offset;
  548. Elf64_Xword sh_size;
  549. Elf64_Word sh_link;
  550. Elf64_Word sh_info;
  551. Elf64_Xword sh_addralign;
  552. Elf64_Xword sh_entsize;
  553. };
  554. // Special section indices.
  555. enum {
  556. SHN_UNDEF = 0, // Undefined, missing, irrelevant, or meaningless
  557. SHN_LORESERVE = 0xff00, // Lowest reserved index
  558. SHN_LOPROC = 0xff00, // Lowest processor-specific index
  559. SHN_HIPROC = 0xff1f, // Highest processor-specific index
  560. SHN_LOOS = 0xff20, // Lowest operating system-specific index
  561. SHN_HIOS = 0xff3f, // Highest operating system-specific index
  562. SHN_ABS = 0xfff1, // Symbol has absolute value; does not need relocation
  563. SHN_COMMON = 0xfff2, // FORTRAN COMMON or C external global variables
  564. SHN_XINDEX = 0xffff, // Mark that the index is >= SHN_LORESERVE
  565. SHN_HIRESERVE = 0xffff // Highest reserved index
  566. };
  567. // Section types.
  568. enum : unsigned {
  569. SHT_NULL = 0, // No associated section (inactive entry).
  570. SHT_PROGBITS = 1, // Program-defined contents.
  571. SHT_SYMTAB = 2, // Symbol table.
  572. SHT_STRTAB = 3, // String table.
  573. SHT_RELA = 4, // Relocation entries; explicit addends.
  574. SHT_HASH = 5, // Symbol hash table.
  575. SHT_DYNAMIC = 6, // Information for dynamic linking.
  576. SHT_NOTE = 7, // Information about the file.
  577. SHT_NOBITS = 8, // Data occupies no space in the file.
  578. SHT_REL = 9, // Relocation entries; no explicit addends.
  579. SHT_SHLIB = 10, // Reserved.
  580. SHT_DYNSYM = 11, // Symbol table.
  581. SHT_INIT_ARRAY = 14, // Pointers to initialization functions.
  582. SHT_FINI_ARRAY = 15, // Pointers to termination functions.
  583. SHT_PREINIT_ARRAY = 16, // Pointers to pre-init functions.
  584. SHT_GROUP = 17, // Section group.
  585. SHT_SYMTAB_SHNDX = 18, // Indices for SHN_XINDEX entries.
  586. SHT_LOOS = 0x60000000, // Lowest operating system-specific type.
  587. SHT_GNU_ATTRIBUTES= 0x6ffffff5, // Object attributes.
  588. SHT_GNU_HASH = 0x6ffffff6, // GNU-style hash table.
  589. SHT_GNU_verdef = 0x6ffffffd, // GNU version definitions.
  590. SHT_GNU_verneed = 0x6ffffffe, // GNU version references.
  591. SHT_GNU_versym = 0x6fffffff, // GNU symbol versions table.
  592. SHT_HIOS = 0x6fffffff, // Highest operating system-specific type.
  593. SHT_LOPROC = 0x70000000, // Lowest processor arch-specific type.
  594. // Fixme: All this is duplicated in MCSectionELF. Why??
  595. // Exception Index table
  596. SHT_ARM_EXIDX = 0x70000001U,
  597. // BPABI DLL dynamic linking pre-emption map
  598. SHT_ARM_PREEMPTMAP = 0x70000002U,
  599. // Object file compatibility attributes
  600. SHT_ARM_ATTRIBUTES = 0x70000003U,
  601. SHT_ARM_DEBUGOVERLAY = 0x70000004U,
  602. SHT_ARM_OVERLAYSECTION = 0x70000005U,
  603. SHT_HEX_ORDERED = 0x70000000, // Link editor is to sort the entries in
  604. // this section based on their sizes
  605. SHT_X86_64_UNWIND = 0x70000001, // Unwind information
  606. SHT_MIPS_REGINFO = 0x70000006, // Register usage information
  607. SHT_MIPS_OPTIONS = 0x7000000d, // General options
  608. SHT_MIPS_ABIFLAGS = 0x7000002a, // ABI information.
  609. SHT_HIPROC = 0x7fffffff, // Highest processor arch-specific type.
  610. SHT_LOUSER = 0x80000000, // Lowest type reserved for applications.
  611. SHT_HIUSER = 0xffffffff // Highest type reserved for applications.
  612. };
  613. // Section flags.
  614. enum : unsigned {
  615. // Section data should be writable during execution.
  616. SHF_WRITE = 0x1,
  617. // Section occupies memory during program execution.
  618. SHF_ALLOC = 0x2,
  619. // Section contains executable machine instructions.
  620. SHF_EXECINSTR = 0x4,
  621. // The data in this section may be merged.
  622. SHF_MERGE = 0x10,
  623. // The data in this section is null-terminated strings.
  624. SHF_STRINGS = 0x20,
  625. // A field in this section holds a section header table index.
  626. SHF_INFO_LINK = 0x40U,
  627. // Adds special ordering requirements for link editors.
  628. SHF_LINK_ORDER = 0x80U,
  629. // This section requires special OS-specific processing to avoid incorrect
  630. // behavior.
  631. SHF_OS_NONCONFORMING = 0x100U,
  632. // This section is a member of a section group.
  633. SHF_GROUP = 0x200U,
  634. // This section holds Thread-Local Storage.
  635. SHF_TLS = 0x400U,
  636. // This section is excluded from the final executable or shared library.
  637. SHF_EXCLUDE = 0x80000000U,
  638. // Start of target-specific flags.
  639. /// XCORE_SHF_CP_SECTION - All sections with the "c" flag are grouped
  640. /// together by the linker to form the constant pool and the cp register is
  641. /// set to the start of the constant pool by the boot code.
  642. XCORE_SHF_CP_SECTION = 0x800U,
  643. /// XCORE_SHF_DP_SECTION - All sections with the "d" flag are grouped
  644. /// together by the linker to form the data section and the dp register is
  645. /// set to the start of the section by the boot code.
  646. XCORE_SHF_DP_SECTION = 0x1000U,
  647. SHF_MASKOS = 0x0ff00000,
  648. // Bits indicating processor-specific flags.
  649. SHF_MASKPROC = 0xf0000000,
  650. // If an object file section does not have this flag set, then it may not hold
  651. // more than 2GB and can be freely referred to in objects using smaller code
  652. // models. Otherwise, only objects using larger code models can refer to them.
  653. // For example, a medium code model object can refer to data in a section that
  654. // sets this flag besides being able to refer to data in a section that does
  655. // not set it; likewise, a small code model object can refer only to code in a
  656. // section that does not set this flag.
  657. SHF_X86_64_LARGE = 0x10000000,
  658. // All sections with the GPREL flag are grouped into a global data area
  659. // for faster accesses
  660. SHF_HEX_GPREL = 0x10000000,
  661. // Section contains text/data which may be replicated in other sections.
  662. // Linker must retain only one copy.
  663. SHF_MIPS_NODUPES = 0x01000000,
  664. // Linker must generate implicit hidden weak names.
  665. SHF_MIPS_NAMES = 0x02000000,
  666. // Section data local to process.
  667. SHF_MIPS_LOCAL = 0x04000000,
  668. // Do not strip this section.
  669. SHF_MIPS_NOSTRIP = 0x08000000,
  670. // Section must be part of global data area.
  671. SHF_MIPS_GPREL = 0x10000000,
  672. // This section should be merged.
  673. SHF_MIPS_MERGE = 0x20000000,
  674. // Address size to be inferred from section entry size.
  675. SHF_MIPS_ADDR = 0x40000000,
  676. // Section data is string data by default.
  677. SHF_MIPS_STRING = 0x80000000
  678. };
  679. // Section Group Flags
  680. enum : unsigned {
  681. GRP_COMDAT = 0x1,
  682. GRP_MASKOS = 0x0ff00000,
  683. GRP_MASKPROC = 0xf0000000
  684. };
  685. // Symbol table entries for ELF32.
  686. struct Elf32_Sym {
  687. Elf32_Word st_name; // Symbol name (index into string table)
  688. Elf32_Addr st_value; // Value or address associated with the symbol
  689. Elf32_Word st_size; // Size of the symbol
  690. unsigned char st_info; // Symbol's type and binding attributes
  691. unsigned char st_other; // Must be zero; reserved
  692. Elf32_Half st_shndx; // Which section (header table index) it's defined in
  693. // These accessors and mutators correspond to the ELF32_ST_BIND,
  694. // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
  695. unsigned char getBinding() const { return st_info >> 4; }
  696. unsigned char getType() const { return st_info & 0x0f; }
  697. void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
  698. void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
  699. void setBindingAndType(unsigned char b, unsigned char t) {
  700. st_info = (b << 4) + (t & 0x0f);
  701. }
  702. };
  703. // Symbol table entries for ELF64.
  704. struct Elf64_Sym {
  705. Elf64_Word st_name; // Symbol name (index into string table)
  706. unsigned char st_info; // Symbol's type and binding attributes
  707. unsigned char st_other; // Must be zero; reserved
  708. Elf64_Half st_shndx; // Which section (header tbl index) it's defined in
  709. Elf64_Addr st_value; // Value or address associated with the symbol
  710. Elf64_Xword st_size; // Size of the symbol
  711. // These accessors and mutators are identical to those defined for ELF32
  712. // symbol table entries.
  713. unsigned char getBinding() const { return st_info >> 4; }
  714. unsigned char getType() const { return st_info & 0x0f; }
  715. void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
  716. void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
  717. void setBindingAndType(unsigned char b, unsigned char t) {
  718. st_info = (b << 4) + (t & 0x0f);
  719. }
  720. };
  721. // The size (in bytes) of symbol table entries.
  722. enum {
  723. SYMENTRY_SIZE32 = 16, // 32-bit symbol entry size
  724. SYMENTRY_SIZE64 = 24 // 64-bit symbol entry size.
  725. };
  726. // Symbol bindings.
  727. enum {
  728. STB_LOCAL = 0, // Local symbol, not visible outside obj file containing def
  729. STB_GLOBAL = 1, // Global symbol, visible to all object files being combined
  730. STB_WEAK = 2, // Weak symbol, like global but lower-precedence
  731. STB_GNU_UNIQUE = 10,
  732. STB_LOOS = 10, // Lowest operating system-specific binding type
  733. STB_HIOS = 12, // Highest operating system-specific binding type
  734. STB_LOPROC = 13, // Lowest processor-specific binding type
  735. STB_HIPROC = 15 // Highest processor-specific binding type
  736. };
  737. // Symbol types.
  738. enum {
  739. STT_NOTYPE = 0, // Symbol's type is not specified
  740. STT_OBJECT = 1, // Symbol is a data object (variable, array, etc.)
  741. STT_FUNC = 2, // Symbol is executable code (function, etc.)
  742. STT_SECTION = 3, // Symbol refers to a section
  743. STT_FILE = 4, // Local, absolute symbol that refers to a file
  744. STT_COMMON = 5, // An uninitialized common block
  745. STT_TLS = 6, // Thread local data object
  746. STT_GNU_IFUNC = 10, // GNU indirect function
  747. STT_LOOS = 10, // Lowest operating system-specific symbol type
  748. STT_HIOS = 12, // Highest operating system-specific symbol type
  749. STT_LOPROC = 13, // Lowest processor-specific symbol type
  750. STT_HIPROC = 15 // Highest processor-specific symbol type
  751. };
  752. enum {
  753. STV_DEFAULT = 0, // Visibility is specified by binding type
  754. STV_INTERNAL = 1, // Defined by processor supplements
  755. STV_HIDDEN = 2, // Not visible to other components
  756. STV_PROTECTED = 3 // Visible in other components but not preemptable
  757. };
  758. // Symbol number.
  759. enum {
  760. STN_UNDEF = 0
  761. };
  762. // Special relocation symbols used in the MIPS64 ELF relocation entries
  763. enum {
  764. RSS_UNDEF = 0, // None
  765. RSS_GP = 1, // Value of gp
  766. RSS_GP0 = 2, // Value of gp used to create object being relocated
  767. RSS_LOC = 3 // Address of location being relocated
  768. };
  769. // Relocation entry, without explicit addend.
  770. struct Elf32_Rel {
  771. Elf32_Addr r_offset; // Location (file byte offset, or program virtual addr)
  772. Elf32_Word r_info; // Symbol table index and type of relocation to apply
  773. // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
  774. // and ELF32_R_INFO macros defined in the ELF specification:
  775. Elf32_Word getSymbol() const { return (r_info >> 8); }
  776. unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
  777. void setSymbol(Elf32_Word s) { setSymbolAndType(s, getType()); }
  778. void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
  779. void setSymbolAndType(Elf32_Word s, unsigned char t) {
  780. r_info = (s << 8) + t;
  781. }
  782. };
  783. // Relocation entry with explicit addend.
  784. struct Elf32_Rela {
  785. Elf32_Addr r_offset; // Location (file byte offset, or program virtual addr)
  786. Elf32_Word r_info; // Symbol table index and type of relocation to apply
  787. Elf32_Sword r_addend; // Compute value for relocatable field by adding this
  788. // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
  789. // and ELF32_R_INFO macros defined in the ELF specification:
  790. Elf32_Word getSymbol() const { return (r_info >> 8); }
  791. unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
  792. void setSymbol(Elf32_Word s) { setSymbolAndType(s, getType()); }
  793. void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
  794. void setSymbolAndType(Elf32_Word s, unsigned char t) {
  795. r_info = (s << 8) + t;
  796. }
  797. };
  798. // Relocation entry, without explicit addend.
  799. struct Elf64_Rel {
  800. Elf64_Addr r_offset; // Location (file byte offset, or program virtual addr).
  801. Elf64_Xword r_info; // Symbol table index and type of relocation to apply.
  802. // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
  803. // and ELF64_R_INFO macros defined in the ELF specification:
  804. Elf64_Word getSymbol() const { return (r_info >> 32); }
  805. Elf64_Word getType() const {
  806. return (Elf64_Word) (r_info & 0xffffffffL);
  807. }
  808. void setSymbol(Elf64_Word s) { setSymbolAndType(s, getType()); }
  809. void setType(Elf64_Word t) { setSymbolAndType(getSymbol(), t); }
  810. void setSymbolAndType(Elf64_Word s, Elf64_Word t) {
  811. r_info = ((Elf64_Xword)s << 32) + (t&0xffffffffL);
  812. }
  813. };
  814. // Relocation entry with explicit addend.
  815. struct Elf64_Rela {
  816. Elf64_Addr r_offset; // Location (file byte offset, or program virtual addr).
  817. Elf64_Xword r_info; // Symbol table index and type of relocation to apply.
  818. Elf64_Sxword r_addend; // Compute value for relocatable field by adding this.
  819. // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
  820. // and ELF64_R_INFO macros defined in the ELF specification:
  821. Elf64_Word getSymbol() const { return (r_info >> 32); }
  822. Elf64_Word getType() const {
  823. return (Elf64_Word) (r_info & 0xffffffffL);
  824. }
  825. void setSymbol(Elf64_Word s) { setSymbolAndType(s, getType()); }
  826. void setType(Elf64_Word t) { setSymbolAndType(getSymbol(), t); }
  827. void setSymbolAndType(Elf64_Word s, Elf64_Word t) {
  828. r_info = ((Elf64_Xword)s << 32) + (t&0xffffffffL);
  829. }
  830. };
  831. // Program header for ELF32.
  832. struct Elf32_Phdr {
  833. Elf32_Word p_type; // Type of segment
  834. Elf32_Off p_offset; // File offset where segment is located, in bytes
  835. Elf32_Addr p_vaddr; // Virtual address of beginning of segment
  836. Elf32_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
  837. Elf32_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
  838. Elf32_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
  839. Elf32_Word p_flags; // Segment flags
  840. Elf32_Word p_align; // Segment alignment constraint
  841. };
  842. // Program header for ELF64.
  843. struct Elf64_Phdr {
  844. Elf64_Word p_type; // Type of segment
  845. Elf64_Word p_flags; // Segment flags
  846. Elf64_Off p_offset; // File offset where segment is located, in bytes
  847. Elf64_Addr p_vaddr; // Virtual address of beginning of segment
  848. Elf64_Addr p_paddr; // Physical addr of beginning of segment (OS-specific)
  849. Elf64_Xword p_filesz; // Num. of bytes in file image of segment (may be zero)
  850. Elf64_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero)
  851. Elf64_Xword p_align; // Segment alignment constraint
  852. };
  853. // Segment types.
  854. enum {
  855. PT_NULL = 0, // Unused segment.
  856. PT_LOAD = 1, // Loadable segment.
  857. PT_DYNAMIC = 2, // Dynamic linking information.
  858. PT_INTERP = 3, // Interpreter pathname.
  859. PT_NOTE = 4, // Auxiliary information.
  860. PT_SHLIB = 5, // Reserved.
  861. PT_PHDR = 6, // The program header table itself.
  862. PT_TLS = 7, // The thread-local storage template.
  863. PT_LOOS = 0x60000000, // Lowest operating system-specific pt entry type.
  864. PT_HIOS = 0x6fffffff, // Highest operating system-specific pt entry type.
  865. PT_LOPROC = 0x70000000, // Lowest processor-specific program hdr entry type.
  866. PT_HIPROC = 0x7fffffff, // Highest processor-specific program hdr entry type.
  867. // x86-64 program header types.
  868. // These all contain stack unwind tables.
  869. PT_GNU_EH_FRAME = 0x6474e550,
  870. PT_SUNW_EH_FRAME = 0x6474e550,
  871. PT_SUNW_UNWIND = 0x6464e550,
  872. PT_GNU_STACK = 0x6474e551, // Indicates stack executability.
  873. PT_GNU_RELRO = 0x6474e552, // Read-only after relocation.
  874. // ARM program header types.
  875. PT_ARM_ARCHEXT = 0x70000000, // Platform architecture compatibility info
  876. // These all contain stack unwind tables.
  877. PT_ARM_EXIDX = 0x70000001,
  878. PT_ARM_UNWIND = 0x70000001,
  879. // MIPS program header types.
  880. PT_MIPS_REGINFO = 0x70000000, // Register usage information.
  881. PT_MIPS_RTPROC = 0x70000001, // Runtime procedure table.
  882. PT_MIPS_OPTIONS = 0x70000002, // Options segment.
  883. PT_MIPS_ABIFLAGS = 0x70000003 // Abiflags segment.
  884. };
  885. // Segment flag bits.
  886. enum : unsigned {
  887. PF_X = 1, // Execute
  888. PF_W = 2, // Write
  889. PF_R = 4, // Read
  890. PF_MASKOS = 0x0ff00000,// Bits for operating system-specific semantics.
  891. PF_MASKPROC = 0xf0000000 // Bits for processor-specific semantics.
  892. };
  893. // Dynamic table entry for ELF32.
  894. struct Elf32_Dyn
  895. {
  896. Elf32_Sword d_tag; // Type of dynamic table entry.
  897. union
  898. {
  899. Elf32_Word d_val; // Integer value of entry.
  900. Elf32_Addr d_ptr; // Pointer value of entry.
  901. } d_un;
  902. };
  903. // Dynamic table entry for ELF64.
  904. struct Elf64_Dyn
  905. {
  906. Elf64_Sxword d_tag; // Type of dynamic table entry.
  907. union
  908. {
  909. Elf64_Xword d_val; // Integer value of entry.
  910. Elf64_Addr d_ptr; // Pointer value of entry.
  911. } d_un;
  912. };
  913. // Dynamic table entry tags.
  914. enum {
  915. DT_NULL = 0, // Marks end of dynamic array.
  916. DT_NEEDED = 1, // String table offset of needed library.
  917. DT_PLTRELSZ = 2, // Size of relocation entries in PLT.
  918. DT_PLTGOT = 3, // Address associated with linkage table.
  919. DT_HASH = 4, // Address of symbolic hash table.
  920. DT_STRTAB = 5, // Address of dynamic string table.
  921. DT_SYMTAB = 6, // Address of dynamic symbol table.
  922. DT_RELA = 7, // Address of relocation table (Rela entries).
  923. DT_RELASZ = 8, // Size of Rela relocation table.
  924. DT_RELAENT = 9, // Size of a Rela relocation entry.
  925. DT_STRSZ = 10, // Total size of the string table.
  926. DT_SYMENT = 11, // Size of a symbol table entry.
  927. DT_INIT = 12, // Address of initialization function.
  928. DT_FINI = 13, // Address of termination function.
  929. DT_SONAME = 14, // String table offset of a shared objects name.
  930. DT_RPATH = 15, // String table offset of library search path.
  931. DT_SYMBOLIC = 16, // Changes symbol resolution algorithm.
  932. DT_REL = 17, // Address of relocation table (Rel entries).
  933. DT_RELSZ = 18, // Size of Rel relocation table.
  934. DT_RELENT = 19, // Size of a Rel relocation entry.
  935. DT_PLTREL = 20, // Type of relocation entry used for linking.
  936. DT_DEBUG = 21, // Reserved for debugger.
  937. DT_TEXTREL = 22, // Relocations exist for non-writable segments.
  938. DT_JMPREL = 23, // Address of relocations associated with PLT.
  939. DT_BIND_NOW = 24, // Process all relocations before execution.
  940. DT_INIT_ARRAY = 25, // Pointer to array of initialization functions.
  941. DT_FINI_ARRAY = 26, // Pointer to array of termination functions.
  942. DT_INIT_ARRAYSZ = 27, // Size of DT_INIT_ARRAY.
  943. DT_FINI_ARRAYSZ = 28, // Size of DT_FINI_ARRAY.
  944. DT_RUNPATH = 29, // String table offset of lib search path.
  945. DT_FLAGS = 30, // Flags.
  946. DT_ENCODING = 32, // Values from here to DT_LOOS follow the rules
  947. // for the interpretation of the d_un union.
  948. DT_PREINIT_ARRAY = 32, // Pointer to array of preinit functions.
  949. DT_PREINIT_ARRAYSZ = 33, // Size of the DT_PREINIT_ARRAY array.
  950. DT_LOOS = 0x60000000, // Start of environment specific tags.
  951. DT_HIOS = 0x6FFFFFFF, // End of environment specific tags.
  952. DT_LOPROC = 0x70000000, // Start of processor specific tags.
  953. DT_HIPROC = 0x7FFFFFFF, // End of processor specific tags.
  954. DT_GNU_HASH = 0x6FFFFEF5, // Reference to the GNU hash table.
  955. DT_RELACOUNT = 0x6FFFFFF9, // ELF32_Rela count.
  956. DT_RELCOUNT = 0x6FFFFFFA, // ELF32_Rel count.
  957. DT_FLAGS_1 = 0X6FFFFFFB, // Flags_1.
  958. DT_VERSYM = 0x6FFFFFF0, // The address of .gnu.version section.
  959. DT_VERDEF = 0X6FFFFFFC, // The address of the version definition table.
  960. DT_VERDEFNUM = 0X6FFFFFFD, // The number of entries in DT_VERDEF.
  961. DT_VERNEED = 0X6FFFFFFE, // The address of the version Dependency table.
  962. DT_VERNEEDNUM = 0X6FFFFFFF, // The number of entries in DT_VERNEED.
  963. // Mips specific dynamic table entry tags.
  964. DT_MIPS_RLD_VERSION = 0x70000001, // 32 bit version number for runtime
  965. // linker interface.
  966. DT_MIPS_TIME_STAMP = 0x70000002, // Time stamp.
  967. DT_MIPS_ICHECKSUM = 0x70000003, // Checksum of external strings
  968. // and common sizes.
  969. DT_MIPS_IVERSION = 0x70000004, // Index of version string
  970. // in string table.
  971. DT_MIPS_FLAGS = 0x70000005, // 32 bits of flags.
  972. DT_MIPS_BASE_ADDRESS = 0x70000006, // Base address of the segment.
  973. DT_MIPS_MSYM = 0x70000007, // Address of .msym section.
  974. DT_MIPS_CONFLICT = 0x70000008, // Address of .conflict section.
  975. DT_MIPS_LIBLIST = 0x70000009, // Address of .liblist section.
  976. DT_MIPS_LOCAL_GOTNO = 0x7000000a, // Number of local global offset
  977. // table entries.
  978. DT_MIPS_CONFLICTNO = 0x7000000b, // Number of entries
  979. // in the .conflict section.
  980. DT_MIPS_LIBLISTNO = 0x70000010, // Number of entries
  981. // in the .liblist section.
  982. DT_MIPS_SYMTABNO = 0x70000011, // Number of entries
  983. // in the .dynsym section.
  984. DT_MIPS_UNREFEXTNO = 0x70000012, // Index of first external dynamic symbol
  985. // not referenced locally.
  986. DT_MIPS_GOTSYM = 0x70000013, // Index of first dynamic symbol
  987. // in global offset table.
  988. DT_MIPS_HIPAGENO = 0x70000014, // Number of page table entries
  989. // in global offset table.
  990. DT_MIPS_RLD_MAP = 0x70000016, // Address of run time loader map,
  991. // used for debugging.
  992. DT_MIPS_DELTA_CLASS = 0x70000017, // Delta C++ class definition.
  993. DT_MIPS_DELTA_CLASS_NO = 0x70000018, // Number of entries
  994. // in DT_MIPS_DELTA_CLASS.
  995. DT_MIPS_DELTA_INSTANCE = 0x70000019, // Delta C++ class instances.
  996. DT_MIPS_DELTA_INSTANCE_NO = 0x7000001A, // Number of entries
  997. // in DT_MIPS_DELTA_INSTANCE.
  998. DT_MIPS_DELTA_RELOC = 0x7000001B, // Delta relocations.
  999. DT_MIPS_DELTA_RELOC_NO = 0x7000001C, // Number of entries
  1000. // in DT_MIPS_DELTA_RELOC.
  1001. DT_MIPS_DELTA_SYM = 0x7000001D, // Delta symbols that Delta
  1002. // relocations refer to.
  1003. DT_MIPS_DELTA_SYM_NO = 0x7000001E, // Number of entries
  1004. // in DT_MIPS_DELTA_SYM.
  1005. DT_MIPS_DELTA_CLASSSYM = 0x70000020, // Delta symbols that hold
  1006. // class declarations.
  1007. DT_MIPS_DELTA_CLASSSYM_NO = 0x70000021, // Number of entries
  1008. // in DT_MIPS_DELTA_CLASSSYM.
  1009. DT_MIPS_CXX_FLAGS = 0x70000022, // Flags indicating information
  1010. // about C++ flavor.
  1011. DT_MIPS_PIXIE_INIT = 0x70000023, // Pixie information.
  1012. DT_MIPS_SYMBOL_LIB = 0x70000024, // Address of .MIPS.symlib
  1013. DT_MIPS_LOCALPAGE_GOTIDX = 0x70000025, // The GOT index of the first PTE
  1014. // for a segment
  1015. DT_MIPS_LOCAL_GOTIDX = 0x70000026, // The GOT index of the first PTE
  1016. // for a local symbol
  1017. DT_MIPS_HIDDEN_GOTIDX = 0x70000027, // The GOT index of the first PTE
  1018. // for a hidden symbol
  1019. DT_MIPS_PROTECTED_GOTIDX = 0x70000028, // The GOT index of the first PTE
  1020. // for a protected symbol
  1021. DT_MIPS_OPTIONS = 0x70000029, // Address of `.MIPS.options'.
  1022. DT_MIPS_INTERFACE = 0x7000002A, // Address of `.interface'.
  1023. DT_MIPS_DYNSTR_ALIGN = 0x7000002B, // Unknown.
  1024. DT_MIPS_INTERFACE_SIZE = 0x7000002C, // Size of the .interface section.
  1025. DT_MIPS_RLD_TEXT_RESOLVE_ADDR = 0x7000002D, // Size of rld_text_resolve
  1026. // function stored in the GOT.
  1027. DT_MIPS_PERF_SUFFIX = 0x7000002E, // Default suffix of DSO to be added
  1028. // by rld on dlopen() calls.
  1029. DT_MIPS_COMPACT_SIZE = 0x7000002F, // Size of compact relocation
  1030. // section (O32).
  1031. DT_MIPS_GP_VALUE = 0x70000030, // GP value for auxiliary GOTs.
  1032. DT_MIPS_AUX_DYNAMIC = 0x70000031, // Address of auxiliary .dynamic.
  1033. DT_MIPS_PLTGOT = 0x70000032, // Address of the base of the PLTGOT.
  1034. DT_MIPS_RWPLT = 0x70000034 // Points to the base
  1035. // of a writable PLT.
  1036. };
  1037. // DT_FLAGS values.
  1038. enum {
  1039. DF_ORIGIN = 0x01, // The object may reference $ORIGIN.
  1040. DF_SYMBOLIC = 0x02, // Search the shared lib before searching the exe.
  1041. DF_TEXTREL = 0x04, // Relocations may modify a non-writable segment.
  1042. DF_BIND_NOW = 0x08, // Process all relocations on load.
  1043. DF_STATIC_TLS = 0x10 // Reject attempts to load dynamically.
  1044. };
  1045. // State flags selectable in the `d_un.d_val' element of the DT_FLAGS_1 entry.
  1046. enum {
  1047. DF_1_NOW = 0x00000001, // Set RTLD_NOW for this object.
  1048. DF_1_GLOBAL = 0x00000002, // Set RTLD_GLOBAL for this object.
  1049. DF_1_GROUP = 0x00000004, // Set RTLD_GROUP for this object.
  1050. DF_1_NODELETE = 0x00000008, // Set RTLD_NODELETE for this object.
  1051. DF_1_LOADFLTR = 0x00000010, // Trigger filtee loading at runtime.
  1052. DF_1_INITFIRST = 0x00000020, // Set RTLD_INITFIRST for this object.
  1053. DF_1_NOOPEN = 0x00000040, // Set RTLD_NOOPEN for this object.
  1054. DF_1_ORIGIN = 0x00000080, // $ORIGIN must be handled.
  1055. DF_1_DIRECT = 0x00000100, // Direct binding enabled.
  1056. DF_1_TRANS = 0x00000200,
  1057. DF_1_INTERPOSE = 0x00000400, // Object is used to interpose.
  1058. DF_1_NODEFLIB = 0x00000800, // Ignore default lib search path.
  1059. DF_1_NODUMP = 0x00001000, // Object can't be dldump'ed.
  1060. DF_1_CONFALT = 0x00002000, // Configuration alternative created.
  1061. DF_1_ENDFILTEE = 0x00004000, // Filtee terminates filters search.
  1062. DF_1_DISPRELDNE = 0x00008000, // Disp reloc applied at build time.
  1063. DF_1_DISPRELPND = 0x00010000, // Disp reloc applied at run-time.
  1064. DF_1_NODIRECT = 0x00020000, // Object has no-direct binding.
  1065. DF_1_IGNMULDEF = 0x00040000,
  1066. DF_1_NOKSYMS = 0x00080000,
  1067. DF_1_NOHDR = 0x00100000,
  1068. DF_1_EDITED = 0x00200000, // Object is modified after built.
  1069. DF_1_NORELOC = 0x00400000,
  1070. DF_1_SYMINTPOSE = 0x00800000, // Object has individual interposers.
  1071. DF_1_GLOBAUDIT = 0x01000000, // Global auditing required.
  1072. DF_1_SINGLETON = 0x02000000 // Singleton symbols are used.
  1073. };
  1074. // DT_MIPS_FLAGS values.
  1075. enum {
  1076. RHF_NONE = 0x00000000, // No flags.
  1077. RHF_QUICKSTART = 0x00000001, // Uses shortcut pointers.
  1078. RHF_NOTPOT = 0x00000002, // Hash size is not a power of two.
  1079. RHS_NO_LIBRARY_REPLACEMENT = 0x00000004, // Ignore LD_LIBRARY_PATH.
  1080. RHF_NO_MOVE = 0x00000008, // DSO address may not be relocated.
  1081. RHF_SGI_ONLY = 0x00000010, // SGI specific features.
  1082. RHF_GUARANTEE_INIT = 0x00000020, // Guarantee that .init will finish
  1083. // executing before any non-init
  1084. // code in DSO is called.
  1085. RHF_DELTA_C_PLUS_PLUS = 0x00000040, // Contains Delta C++ code.
  1086. RHF_GUARANTEE_START_INIT = 0x00000080, // Guarantee that .init will start
  1087. // executing before any non-init
  1088. // code in DSO is called.
  1089. RHF_PIXIE = 0x00000100, // Generated by pixie.
  1090. RHF_DEFAULT_DELAY_LOAD = 0x00000200, // Delay-load DSO by default.
  1091. RHF_REQUICKSTART = 0x00000400, // Object may be requickstarted
  1092. RHF_REQUICKSTARTED = 0x00000800, // Object has been requickstarted
  1093. RHF_CORD = 0x00001000, // Generated by cord.
  1094. RHF_NO_UNRES_UNDEF = 0x00002000, // Object contains no unresolved
  1095. // undef symbols.
  1096. RHF_RLD_ORDER_SAFE = 0x00004000 // Symbol table is in a safe order.
  1097. };
  1098. // ElfXX_VerDef structure version (GNU versioning)
  1099. enum {
  1100. VER_DEF_NONE = 0,
  1101. VER_DEF_CURRENT = 1
  1102. };
  1103. // VerDef Flags (ElfXX_VerDef::vd_flags)
  1104. enum {
  1105. VER_FLG_BASE = 0x1,
  1106. VER_FLG_WEAK = 0x2,
  1107. VER_FLG_INFO = 0x4
  1108. };
  1109. // Special constants for the version table. (SHT_GNU_versym/.gnu.version)
  1110. enum {
  1111. VER_NDX_LOCAL = 0, // Unversioned local symbol
  1112. VER_NDX_GLOBAL = 1, // Unversioned global symbol
  1113. VERSYM_VERSION = 0x7fff, // Version Index mask
  1114. VERSYM_HIDDEN = 0x8000 // Hidden bit (non-default version)
  1115. };
  1116. // ElfXX_VerNeed structure version (GNU versioning)
  1117. enum {
  1118. VER_NEED_NONE = 0,
  1119. VER_NEED_CURRENT = 1
  1120. };
  1121. } // end namespace ELF
  1122. } // end namespace llvm
  1123. #endif