You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1522 lines
51 KiB

  1. /*
  2. * ELF constants and data structures
  3. *
  4. * Derived from:
  5. * $FreeBSD: src/sys/sys/elf32.h,v 1.8.14.1 2005/12/30 22:13:58 marcel Exp $
  6. * $FreeBSD: src/sys/sys/elf64.h,v 1.10.14.1 2005/12/30 22:13:58 marcel Exp $
  7. * $FreeBSD: src/sys/sys/elf_common.h,v 1.15.8.1 2005/12/30 22:13:58 marcel Exp $
  8. * $FreeBSD: src/sys/alpha/include/elf.h,v 1.14 2003/09/25 01:10:22 peter Exp $
  9. * $FreeBSD: src/sys/amd64/include/elf.h,v 1.18 2004/08/03 08:21:48 dfr Exp $
  10. * $FreeBSD: src/sys/arm/include/elf.h,v 1.5.2.1 2006/06/30 21:42:52 cognet Exp $
  11. * $FreeBSD: src/sys/i386/include/elf.h,v 1.16 2004/08/02 19:12:17 dfr Exp $
  12. * $FreeBSD: src/sys/powerpc/include/elf.h,v 1.7 2004/11/02 09:47:01 ssouhlal Exp $
  13. * $FreeBSD: src/sys/sparc64/include/elf.h,v 1.12 2003/09/25 01:10:26 peter Exp $
  14. *
  15. * Copyright (c) 1996-1998 John D. Polstra. All rights reserved.
  16. * Copyright (c) 2001 David E. O'Brien
  17. * Portions Copyright 2018 Google Inc. All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  29. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. * SUCH DAMAGE.
  39. */
  40. package elf // import "cloud.google.com/go/cmd/go-cloud-debug-agent/internal/debug/elf"
  41. import "strconv"
  42. /*
  43. * Constants
  44. */
  45. // Indexes into the Header.Ident array.
  46. const (
  47. EI_CLASS = 4 /* Class of machine. */
  48. EI_DATA = 5 /* Data format. */
  49. EI_VERSION = 6 /* ELF format version. */
  50. EI_OSABI = 7 /* Operating system / ABI identification */
  51. EI_ABIVERSION = 8 /* ABI version */
  52. EI_PAD = 9 /* Start of padding (per SVR4 ABI). */
  53. EI_NIDENT = 16 /* Size of e_ident array. */
  54. )
  55. // Initial magic number for ELF files.
  56. const ELFMAG = "\177ELF"
  57. // Version is found in Header.Ident[EI_VERSION] and Header.Version.
  58. type Version byte
  59. const (
  60. EV_NONE Version = 0
  61. EV_CURRENT Version = 1
  62. )
  63. var versionStrings = []intName{
  64. {0, "EV_NONE"},
  65. {1, "EV_CURRENT"},
  66. }
  67. func (i Version) String() string { return stringName(uint32(i), versionStrings, false) }
  68. func (i Version) GoString() string { return stringName(uint32(i), versionStrings, true) }
  69. // Class is found in Header.Ident[EI_CLASS] and Header.Class.
  70. type Class byte
  71. const (
  72. ELFCLASSNONE Class = 0 /* Unknown class. */
  73. ELFCLASS32 Class = 1 /* 32-bit architecture. */
  74. ELFCLASS64 Class = 2 /* 64-bit architecture. */
  75. )
  76. var classStrings = []intName{
  77. {0, "ELFCLASSNONE"},
  78. {1, "ELFCLASS32"},
  79. {2, "ELFCLASS64"},
  80. }
  81. func (i Class) String() string { return stringName(uint32(i), classStrings, false) }
  82. func (i Class) GoString() string { return stringName(uint32(i), classStrings, true) }
  83. // Data is found in Header.Ident[EI_DATA] and Header.Data.
  84. type Data byte
  85. const (
  86. ELFDATANONE Data = 0 /* Unknown data format. */
  87. ELFDATA2LSB Data = 1 /* 2's complement little-endian. */
  88. ELFDATA2MSB Data = 2 /* 2's complement big-endian. */
  89. )
  90. var dataStrings = []intName{
  91. {0, "ELFDATANONE"},
  92. {1, "ELFDATA2LSB"},
  93. {2, "ELFDATA2MSB"},
  94. }
  95. func (i Data) String() string { return stringName(uint32(i), dataStrings, false) }
  96. func (i Data) GoString() string { return stringName(uint32(i), dataStrings, true) }
  97. // OSABI is found in Header.Ident[EI_OSABI] and Header.OSABI.
  98. type OSABI byte
  99. const (
  100. ELFOSABI_NONE OSABI = 0 /* UNIX System V ABI */
  101. ELFOSABI_HPUX OSABI = 1 /* HP-UX operating system */
  102. ELFOSABI_NETBSD OSABI = 2 /* NetBSD */
  103. ELFOSABI_LINUX OSABI = 3 /* GNU/Linux */
  104. ELFOSABI_HURD OSABI = 4 /* GNU/Hurd */
  105. ELFOSABI_86OPEN OSABI = 5 /* 86Open common IA32 ABI */
  106. ELFOSABI_SOLARIS OSABI = 6 /* Solaris */
  107. ELFOSABI_AIX OSABI = 7 /* AIX */
  108. ELFOSABI_IRIX OSABI = 8 /* IRIX */
  109. ELFOSABI_FREEBSD OSABI = 9 /* FreeBSD */
  110. ELFOSABI_TRU64 OSABI = 10 /* TRU64 UNIX */
  111. ELFOSABI_MODESTO OSABI = 11 /* Novell Modesto */
  112. ELFOSABI_OPENBSD OSABI = 12 /* OpenBSD */
  113. ELFOSABI_OPENVMS OSABI = 13 /* Open VMS */
  114. ELFOSABI_NSK OSABI = 14 /* HP Non-Stop Kernel */
  115. ELFOSABI_ARM OSABI = 97 /* ARM */
  116. ELFOSABI_STANDALONE OSABI = 255 /* Standalone (embedded) application */
  117. )
  118. var osabiStrings = []intName{
  119. {0, "ELFOSABI_NONE"},
  120. {1, "ELFOSABI_HPUX"},
  121. {2, "ELFOSABI_NETBSD"},
  122. {3, "ELFOSABI_LINUX"},
  123. {4, "ELFOSABI_HURD"},
  124. {5, "ELFOSABI_86OPEN"},
  125. {6, "ELFOSABI_SOLARIS"},
  126. {7, "ELFOSABI_AIX"},
  127. {8, "ELFOSABI_IRIX"},
  128. {9, "ELFOSABI_FREEBSD"},
  129. {10, "ELFOSABI_TRU64"},
  130. {11, "ELFOSABI_MODESTO"},
  131. {12, "ELFOSABI_OPENBSD"},
  132. {13, "ELFOSABI_OPENVMS"},
  133. {14, "ELFOSABI_NSK"},
  134. {97, "ELFOSABI_ARM"},
  135. {255, "ELFOSABI_STANDALONE"},
  136. }
  137. func (i OSABI) String() string { return stringName(uint32(i), osabiStrings, false) }
  138. func (i OSABI) GoString() string { return stringName(uint32(i), osabiStrings, true) }
  139. // Type is found in Header.Type.
  140. type Type uint16
  141. const (
  142. ET_NONE Type = 0 /* Unknown type. */
  143. ET_REL Type = 1 /* Relocatable. */
  144. ET_EXEC Type = 2 /* Executable. */
  145. ET_DYN Type = 3 /* Shared object. */
  146. ET_CORE Type = 4 /* Core file. */
  147. ET_LOOS Type = 0xfe00 /* First operating system specific. */
  148. ET_HIOS Type = 0xfeff /* Last operating system-specific. */
  149. ET_LOPROC Type = 0xff00 /* First processor-specific. */
  150. ET_HIPROC Type = 0xffff /* Last processor-specific. */
  151. )
  152. var typeStrings = []intName{
  153. {0, "ET_NONE"},
  154. {1, "ET_REL"},
  155. {2, "ET_EXEC"},
  156. {3, "ET_DYN"},
  157. {4, "ET_CORE"},
  158. {0xfe00, "ET_LOOS"},
  159. {0xfeff, "ET_HIOS"},
  160. {0xff00, "ET_LOPROC"},
  161. {0xffff, "ET_HIPROC"},
  162. }
  163. func (i Type) String() string { return stringName(uint32(i), typeStrings, false) }
  164. func (i Type) GoString() string { return stringName(uint32(i), typeStrings, true) }
  165. // Machine is found in Header.Machine.
  166. type Machine uint16
  167. const (
  168. EM_NONE Machine = 0 /* Unknown machine. */
  169. EM_M32 Machine = 1 /* AT&T WE32100. */
  170. EM_SPARC Machine = 2 /* Sun SPARC. */
  171. EM_386 Machine = 3 /* Intel i386. */
  172. EM_68K Machine = 4 /* Motorola 68000. */
  173. EM_88K Machine = 5 /* Motorola 88000. */
  174. EM_860 Machine = 7 /* Intel i860. */
  175. EM_MIPS Machine = 8 /* MIPS R3000 Big-Endian only. */
  176. EM_S370 Machine = 9 /* IBM System/370. */
  177. EM_MIPS_RS3_LE Machine = 10 /* MIPS R3000 Little-Endian. */
  178. EM_PARISC Machine = 15 /* HP PA-RISC. */
  179. EM_VPP500 Machine = 17 /* Fujitsu VPP500. */
  180. EM_SPARC32PLUS Machine = 18 /* SPARC v8plus. */
  181. EM_960 Machine = 19 /* Intel 80960. */
  182. EM_PPC Machine = 20 /* PowerPC 32-bit. */
  183. EM_PPC64 Machine = 21 /* PowerPC 64-bit. */
  184. EM_S390 Machine = 22 /* IBM System/390. */
  185. EM_V800 Machine = 36 /* NEC V800. */
  186. EM_FR20 Machine = 37 /* Fujitsu FR20. */
  187. EM_RH32 Machine = 38 /* TRW RH-32. */
  188. EM_RCE Machine = 39 /* Motorola RCE. */
  189. EM_ARM Machine = 40 /* ARM. */
  190. EM_SH Machine = 42 /* Hitachi SH. */
  191. EM_SPARCV9 Machine = 43 /* SPARC v9 64-bit. */
  192. EM_TRICORE Machine = 44 /* Siemens TriCore embedded processor. */
  193. EM_ARC Machine = 45 /* Argonaut RISC Core. */
  194. EM_H8_300 Machine = 46 /* Hitachi H8/300. */
  195. EM_H8_300H Machine = 47 /* Hitachi H8/300H. */
  196. EM_H8S Machine = 48 /* Hitachi H8S. */
  197. EM_H8_500 Machine = 49 /* Hitachi H8/500. */
  198. EM_IA_64 Machine = 50 /* Intel IA-64 Processor. */
  199. EM_MIPS_X Machine = 51 /* Stanford MIPS-X. */
  200. EM_COLDFIRE Machine = 52 /* Motorola ColdFire. */
  201. EM_68HC12 Machine = 53 /* Motorola M68HC12. */
  202. EM_MMA Machine = 54 /* Fujitsu MMA. */
  203. EM_PCP Machine = 55 /* Siemens PCP. */
  204. EM_NCPU Machine = 56 /* Sony nCPU. */
  205. EM_NDR1 Machine = 57 /* Denso NDR1 microprocessor. */
  206. EM_STARCORE Machine = 58 /* Motorola Star*Core processor. */
  207. EM_ME16 Machine = 59 /* Toyota ME16 processor. */
  208. EM_ST100 Machine = 60 /* STMicroelectronics ST100 processor. */
  209. EM_TINYJ Machine = 61 /* Advanced Logic Corp. TinyJ processor. */
  210. EM_X86_64 Machine = 62 /* Advanced Micro Devices x86-64 */
  211. /* Non-standard or deprecated. */
  212. EM_486 Machine = 6 /* Intel i486. */
  213. EM_MIPS_RS4_BE Machine = 10 /* MIPS R4000 Big-Endian */
  214. EM_ALPHA_STD Machine = 41 /* Digital Alpha (standard value). */
  215. EM_ALPHA Machine = 0x9026 /* Alpha (written in the absence of an ABI) */
  216. )
  217. var machineStrings = []intName{
  218. {0, "EM_NONE"},
  219. {1, "EM_M32"},
  220. {2, "EM_SPARC"},
  221. {3, "EM_386"},
  222. {4, "EM_68K"},
  223. {5, "EM_88K"},
  224. {7, "EM_860"},
  225. {8, "EM_MIPS"},
  226. {9, "EM_S370"},
  227. {10, "EM_MIPS_RS3_LE"},
  228. {15, "EM_PARISC"},
  229. {17, "EM_VPP500"},
  230. {18, "EM_SPARC32PLUS"},
  231. {19, "EM_960"},
  232. {20, "EM_PPC"},
  233. {21, "EM_PPC64"},
  234. {22, "EM_S390"},
  235. {36, "EM_V800"},
  236. {37, "EM_FR20"},
  237. {38, "EM_RH32"},
  238. {39, "EM_RCE"},
  239. {40, "EM_ARM"},
  240. {42, "EM_SH"},
  241. {43, "EM_SPARCV9"},
  242. {44, "EM_TRICORE"},
  243. {45, "EM_ARC"},
  244. {46, "EM_H8_300"},
  245. {47, "EM_H8_300H"},
  246. {48, "EM_H8S"},
  247. {49, "EM_H8_500"},
  248. {50, "EM_IA_64"},
  249. {51, "EM_MIPS_X"},
  250. {52, "EM_COLDFIRE"},
  251. {53, "EM_68HC12"},
  252. {54, "EM_MMA"},
  253. {55, "EM_PCP"},
  254. {56, "EM_NCPU"},
  255. {57, "EM_NDR1"},
  256. {58, "EM_STARCORE"},
  257. {59, "EM_ME16"},
  258. {60, "EM_ST100"},
  259. {61, "EM_TINYJ"},
  260. {62, "EM_X86_64"},
  261. /* Non-standard or deprecated. */
  262. {6, "EM_486"},
  263. {10, "EM_MIPS_RS4_BE"},
  264. {41, "EM_ALPHA_STD"},
  265. {0x9026, "EM_ALPHA"},
  266. }
  267. func (i Machine) String() string { return stringName(uint32(i), machineStrings, false) }
  268. func (i Machine) GoString() string { return stringName(uint32(i), machineStrings, true) }
  269. // Special section indices.
  270. type SectionIndex int
  271. const (
  272. SHN_UNDEF SectionIndex = 0 /* Undefined, missing, irrelevant. */
  273. SHN_LORESERVE SectionIndex = 0xff00 /* First of reserved range. */
  274. SHN_LOPROC SectionIndex = 0xff00 /* First processor-specific. */
  275. SHN_HIPROC SectionIndex = 0xff1f /* Last processor-specific. */
  276. SHN_LOOS SectionIndex = 0xff20 /* First operating system-specific. */
  277. SHN_HIOS SectionIndex = 0xff3f /* Last operating system-specific. */
  278. SHN_ABS SectionIndex = 0xfff1 /* Absolute values. */
  279. SHN_COMMON SectionIndex = 0xfff2 /* Common data. */
  280. SHN_XINDEX SectionIndex = 0xffff /* Escape -- index stored elsewhere. */
  281. SHN_HIRESERVE SectionIndex = 0xffff /* Last of reserved range. */
  282. )
  283. var shnStrings = []intName{
  284. {0, "SHN_UNDEF"},
  285. {0xff00, "SHN_LOPROC"},
  286. {0xff20, "SHN_LOOS"},
  287. {0xfff1, "SHN_ABS"},
  288. {0xfff2, "SHN_COMMON"},
  289. {0xffff, "SHN_XINDEX"},
  290. }
  291. func (i SectionIndex) String() string { return stringName(uint32(i), shnStrings, false) }
  292. func (i SectionIndex) GoString() string { return stringName(uint32(i), shnStrings, true) }
  293. // Section type.
  294. type SectionType uint32
  295. const (
  296. SHT_NULL SectionType = 0 /* inactive */
  297. SHT_PROGBITS SectionType = 1 /* program defined information */
  298. SHT_SYMTAB SectionType = 2 /* symbol table section */
  299. SHT_STRTAB SectionType = 3 /* string table section */
  300. SHT_RELA SectionType = 4 /* relocation section with addends */
  301. SHT_HASH SectionType = 5 /* symbol hash table section */
  302. SHT_DYNAMIC SectionType = 6 /* dynamic section */
  303. SHT_NOTE SectionType = 7 /* note section */
  304. SHT_NOBITS SectionType = 8 /* no space section */
  305. SHT_REL SectionType = 9 /* relocation section - no addends */
  306. SHT_SHLIB SectionType = 10 /* reserved - purpose unknown */
  307. SHT_DYNSYM SectionType = 11 /* dynamic symbol table section */
  308. SHT_INIT_ARRAY SectionType = 14 /* Initialization function pointers. */
  309. SHT_FINI_ARRAY SectionType = 15 /* Termination function pointers. */
  310. SHT_PREINIT_ARRAY SectionType = 16 /* Pre-initialization function ptrs. */
  311. SHT_GROUP SectionType = 17 /* Section group. */
  312. SHT_SYMTAB_SHNDX SectionType = 18 /* Section indexes (see SHN_XINDEX). */
  313. SHT_LOOS SectionType = 0x60000000 /* First of OS specific semantics */
  314. SHT_GNU_ATTRIBUTES SectionType = 0x6ffffff5 /* GNU object attributes */
  315. SHT_GNU_HASH SectionType = 0x6ffffff6 /* GNU hash table */
  316. SHT_GNU_LIBLIST SectionType = 0x6ffffff7 /* GNU prelink library list */
  317. SHT_GNU_VERDEF SectionType = 0x6ffffffd /* GNU version definition section */
  318. SHT_GNU_VERNEED SectionType = 0x6ffffffe /* GNU version needs section */
  319. SHT_GNU_VERSYM SectionType = 0x6fffffff /* GNU version symbol table */
  320. SHT_HIOS SectionType = 0x6fffffff /* Last of OS specific semantics */
  321. SHT_LOPROC SectionType = 0x70000000 /* reserved range for processor */
  322. SHT_HIPROC SectionType = 0x7fffffff /* specific section header types */
  323. SHT_LOUSER SectionType = 0x80000000 /* reserved range for application */
  324. SHT_HIUSER SectionType = 0xffffffff /* specific indexes */
  325. )
  326. var shtStrings = []intName{
  327. {0, "SHT_NULL"},
  328. {1, "SHT_PROGBITS"},
  329. {2, "SHT_SYMTAB"},
  330. {3, "SHT_STRTAB"},
  331. {4, "SHT_RELA"},
  332. {5, "SHT_HASH"},
  333. {6, "SHT_DYNAMIC"},
  334. {7, "SHT_NOTE"},
  335. {8, "SHT_NOBITS"},
  336. {9, "SHT_REL"},
  337. {10, "SHT_SHLIB"},
  338. {11, "SHT_DYNSYM"},
  339. {14, "SHT_INIT_ARRAY"},
  340. {15, "SHT_FINI_ARRAY"},
  341. {16, "SHT_PREINIT_ARRAY"},
  342. {17, "SHT_GROUP"},
  343. {18, "SHT_SYMTAB_SHNDX"},
  344. {0x60000000, "SHT_LOOS"},
  345. {0x6ffffff5, "SHT_GNU_ATTRIBUTES"},
  346. {0x6ffffff6, "SHT_GNU_HASH"},
  347. {0x6ffffff7, "SHT_GNU_LIBLIST"},
  348. {0x6ffffffd, "SHT_GNU_VERDEF"},
  349. {0x6ffffffe, "SHT_GNU_VERNEED"},
  350. {0x6fffffff, "SHT_GNU_VERSYM"},
  351. {0x70000000, "SHT_LOPROC"},
  352. {0x7fffffff, "SHT_HIPROC"},
  353. {0x80000000, "SHT_LOUSER"},
  354. {0xffffffff, "SHT_HIUSER"},
  355. }
  356. func (i SectionType) String() string { return stringName(uint32(i), shtStrings, false) }
  357. func (i SectionType) GoString() string { return stringName(uint32(i), shtStrings, true) }
  358. // Section flags.
  359. type SectionFlag uint32
  360. const (
  361. SHF_WRITE SectionFlag = 0x1 /* Section contains writable data. */
  362. SHF_ALLOC SectionFlag = 0x2 /* Section occupies memory. */
  363. SHF_EXECINSTR SectionFlag = 0x4 /* Section contains instructions. */
  364. SHF_MERGE SectionFlag = 0x10 /* Section may be merged. */
  365. SHF_STRINGS SectionFlag = 0x20 /* Section contains strings. */
  366. SHF_INFO_LINK SectionFlag = 0x40 /* sh_info holds section index. */
  367. SHF_LINK_ORDER SectionFlag = 0x80 /* Special ordering requirements. */
  368. SHF_OS_NONCONFORMING SectionFlag = 0x100 /* OS-specific processing required. */
  369. SHF_GROUP SectionFlag = 0x200 /* Member of section group. */
  370. SHF_TLS SectionFlag = 0x400 /* Section contains TLS data. */
  371. SHF_MASKOS SectionFlag = 0x0ff00000 /* OS-specific semantics. */
  372. SHF_MASKPROC SectionFlag = 0xf0000000 /* Processor-specific semantics. */
  373. )
  374. var shfStrings = []intName{
  375. {0x1, "SHF_WRITE"},
  376. {0x2, "SHF_ALLOC"},
  377. {0x4, "SHF_EXECINSTR"},
  378. {0x10, "SHF_MERGE"},
  379. {0x20, "SHF_STRINGS"},
  380. {0x40, "SHF_INFO_LINK"},
  381. {0x80, "SHF_LINK_ORDER"},
  382. {0x100, "SHF_OS_NONCONFORMING"},
  383. {0x200, "SHF_GROUP"},
  384. {0x400, "SHF_TLS"},
  385. }
  386. func (i SectionFlag) String() string { return flagName(uint32(i), shfStrings, false) }
  387. func (i SectionFlag) GoString() string { return flagName(uint32(i), shfStrings, true) }
  388. // Prog.Type
  389. type ProgType int
  390. const (
  391. PT_NULL ProgType = 0 /* Unused entry. */
  392. PT_LOAD ProgType = 1 /* Loadable segment. */
  393. PT_DYNAMIC ProgType = 2 /* Dynamic linking information segment. */
  394. PT_INTERP ProgType = 3 /* Pathname of interpreter. */
  395. PT_NOTE ProgType = 4 /* Auxiliary information. */
  396. PT_SHLIB ProgType = 5 /* Reserved (not used). */
  397. PT_PHDR ProgType = 6 /* Location of program header itself. */
  398. PT_TLS ProgType = 7 /* Thread local storage segment */
  399. PT_LOOS ProgType = 0x60000000 /* First OS-specific. */
  400. PT_HIOS ProgType = 0x6fffffff /* Last OS-specific. */
  401. PT_LOPROC ProgType = 0x70000000 /* First processor-specific type. */
  402. PT_HIPROC ProgType = 0x7fffffff /* Last processor-specific type. */
  403. )
  404. var ptStrings = []intName{
  405. {0, "PT_NULL"},
  406. {1, "PT_LOAD"},
  407. {2, "PT_DYNAMIC"},
  408. {3, "PT_INTERP"},
  409. {4, "PT_NOTE"},
  410. {5, "PT_SHLIB"},
  411. {6, "PT_PHDR"},
  412. {7, "PT_TLS"},
  413. {0x60000000, "PT_LOOS"},
  414. {0x6fffffff, "PT_HIOS"},
  415. {0x70000000, "PT_LOPROC"},
  416. {0x7fffffff, "PT_HIPROC"},
  417. }
  418. func (i ProgType) String() string { return stringName(uint32(i), ptStrings, false) }
  419. func (i ProgType) GoString() string { return stringName(uint32(i), ptStrings, true) }
  420. // Prog.Flag
  421. type ProgFlag uint32
  422. const (
  423. PF_X ProgFlag = 0x1 /* Executable. */
  424. PF_W ProgFlag = 0x2 /* Writable. */
  425. PF_R ProgFlag = 0x4 /* Readable. */
  426. PF_MASKOS ProgFlag = 0x0ff00000 /* Operating system-specific. */
  427. PF_MASKPROC ProgFlag = 0xf0000000 /* Processor-specific. */
  428. )
  429. var pfStrings = []intName{
  430. {0x1, "PF_X"},
  431. {0x2, "PF_W"},
  432. {0x4, "PF_R"},
  433. }
  434. func (i ProgFlag) String() string { return flagName(uint32(i), pfStrings, false) }
  435. func (i ProgFlag) GoString() string { return flagName(uint32(i), pfStrings, true) }
  436. // Dyn.Tag
  437. type DynTag int
  438. const (
  439. DT_NULL DynTag = 0 /* Terminating entry. */
  440. DT_NEEDED DynTag = 1 /* String table offset of a needed shared library. */
  441. DT_PLTRELSZ DynTag = 2 /* Total size in bytes of PLT relocations. */
  442. DT_PLTGOT DynTag = 3 /* Processor-dependent address. */
  443. DT_HASH DynTag = 4 /* Address of symbol hash table. */
  444. DT_STRTAB DynTag = 5 /* Address of string table. */
  445. DT_SYMTAB DynTag = 6 /* Address of symbol table. */
  446. DT_RELA DynTag = 7 /* Address of ElfNN_Rela relocations. */
  447. DT_RELASZ DynTag = 8 /* Total size of ElfNN_Rela relocations. */
  448. DT_RELAENT DynTag = 9 /* Size of each ElfNN_Rela relocation entry. */
  449. DT_STRSZ DynTag = 10 /* Size of string table. */
  450. DT_SYMENT DynTag = 11 /* Size of each symbol table entry. */
  451. DT_INIT DynTag = 12 /* Address of initialization function. */
  452. DT_FINI DynTag = 13 /* Address of finalization function. */
  453. DT_SONAME DynTag = 14 /* String table offset of shared object name. */
  454. DT_RPATH DynTag = 15 /* String table offset of library path. [sup] */
  455. DT_SYMBOLIC DynTag = 16 /* Indicates "symbolic" linking. [sup] */
  456. DT_REL DynTag = 17 /* Address of ElfNN_Rel relocations. */
  457. DT_RELSZ DynTag = 18 /* Total size of ElfNN_Rel relocations. */
  458. DT_RELENT DynTag = 19 /* Size of each ElfNN_Rel relocation. */
  459. DT_PLTREL DynTag = 20 /* Type of relocation used for PLT. */
  460. DT_DEBUG DynTag = 21 /* Reserved (not used). */
  461. DT_TEXTREL DynTag = 22 /* Indicates there may be relocations in non-writable segments. [sup] */
  462. DT_JMPREL DynTag = 23 /* Address of PLT relocations. */
  463. DT_BIND_NOW DynTag = 24 /* [sup] */
  464. DT_INIT_ARRAY DynTag = 25 /* Address of the array of pointers to initialization functions */
  465. DT_FINI_ARRAY DynTag = 26 /* Address of the array of pointers to termination functions */
  466. DT_INIT_ARRAYSZ DynTag = 27 /* Size in bytes of the array of initialization functions. */
  467. DT_FINI_ARRAYSZ DynTag = 28 /* Size in bytes of the array of terminationfunctions. */
  468. DT_RUNPATH DynTag = 29 /* String table offset of a null-terminated library search path string. */
  469. DT_FLAGS DynTag = 30 /* Object specific flag values. */
  470. DT_ENCODING DynTag = 32 /* Values greater than or equal to DT_ENCODING
  471. and less than DT_LOOS follow the rules for
  472. the interpretation of the d_un union
  473. as follows: even == 'd_ptr', even == 'd_val'
  474. or none */
  475. DT_PREINIT_ARRAY DynTag = 32 /* Address of the array of pointers to pre-initialization functions. */
  476. DT_PREINIT_ARRAYSZ DynTag = 33 /* Size in bytes of the array of pre-initialization functions. */
  477. DT_LOOS DynTag = 0x6000000d /* First OS-specific */
  478. DT_HIOS DynTag = 0x6ffff000 /* Last OS-specific */
  479. DT_VERSYM DynTag = 0x6ffffff0
  480. DT_VERNEED DynTag = 0x6ffffffe
  481. DT_VERNEEDNUM DynTag = 0x6fffffff
  482. DT_LOPROC DynTag = 0x70000000 /* First processor-specific type. */
  483. DT_HIPROC DynTag = 0x7fffffff /* Last processor-specific type. */
  484. )
  485. var dtStrings = []intName{
  486. {0, "DT_NULL"},
  487. {1, "DT_NEEDED"},
  488. {2, "DT_PLTRELSZ"},
  489. {3, "DT_PLTGOT"},
  490. {4, "DT_HASH"},
  491. {5, "DT_STRTAB"},
  492. {6, "DT_SYMTAB"},
  493. {7, "DT_RELA"},
  494. {8, "DT_RELASZ"},
  495. {9, "DT_RELAENT"},
  496. {10, "DT_STRSZ"},
  497. {11, "DT_SYMENT"},
  498. {12, "DT_INIT"},
  499. {13, "DT_FINI"},
  500. {14, "DT_SONAME"},
  501. {15, "DT_RPATH"},
  502. {16, "DT_SYMBOLIC"},
  503. {17, "DT_REL"},
  504. {18, "DT_RELSZ"},
  505. {19, "DT_RELENT"},
  506. {20, "DT_PLTREL"},
  507. {21, "DT_DEBUG"},
  508. {22, "DT_TEXTREL"},
  509. {23, "DT_JMPREL"},
  510. {24, "DT_BIND_NOW"},
  511. {25, "DT_INIT_ARRAY"},
  512. {26, "DT_FINI_ARRAY"},
  513. {27, "DT_INIT_ARRAYSZ"},
  514. {28, "DT_FINI_ARRAYSZ"},
  515. {29, "DT_RUNPATH"},
  516. {30, "DT_FLAGS"},
  517. {32, "DT_ENCODING"},
  518. {32, "DT_PREINIT_ARRAY"},
  519. {33, "DT_PREINIT_ARRAYSZ"},
  520. {0x6000000d, "DT_LOOS"},
  521. {0x6ffff000, "DT_HIOS"},
  522. {0x6ffffff0, "DT_VERSYM"},
  523. {0x6ffffffe, "DT_VERNEED"},
  524. {0x6fffffff, "DT_VERNEEDNUM"},
  525. {0x70000000, "DT_LOPROC"},
  526. {0x7fffffff, "DT_HIPROC"},
  527. }
  528. func (i DynTag) String() string { return stringName(uint32(i), dtStrings, false) }
  529. func (i DynTag) GoString() string { return stringName(uint32(i), dtStrings, true) }
  530. // DT_FLAGS values.
  531. type DynFlag int
  532. const (
  533. DF_ORIGIN DynFlag = 0x0001 /* Indicates that the object being loaded may
  534. make reference to the
  535. $ORIGIN substitution string */
  536. DF_SYMBOLIC DynFlag = 0x0002 /* Indicates "symbolic" linking. */
  537. DF_TEXTREL DynFlag = 0x0004 /* Indicates there may be relocations in non-writable segments. */
  538. DF_BIND_NOW DynFlag = 0x0008 /* Indicates that the dynamic linker should
  539. process all relocations for the object
  540. containing this entry before transferring
  541. control to the program. */
  542. DF_STATIC_TLS DynFlag = 0x0010 /* Indicates that the shared object or
  543. executable contains code using a static
  544. thread-local storage scheme. */
  545. )
  546. var dflagStrings = []intName{
  547. {0x0001, "DF_ORIGIN"},
  548. {0x0002, "DF_SYMBOLIC"},
  549. {0x0004, "DF_TEXTREL"},
  550. {0x0008, "DF_BIND_NOW"},
  551. {0x0010, "DF_STATIC_TLS"},
  552. }
  553. func (i DynFlag) String() string { return flagName(uint32(i), dflagStrings, false) }
  554. func (i DynFlag) GoString() string { return flagName(uint32(i), dflagStrings, true) }
  555. // NType values; used in core files.
  556. type NType int
  557. const (
  558. NT_PRSTATUS NType = 1 /* Process status. */
  559. NT_FPREGSET NType = 2 /* Floating point registers. */
  560. NT_PRPSINFO NType = 3 /* Process state info. */
  561. )
  562. var ntypeStrings = []intName{
  563. {1, "NT_PRSTATUS"},
  564. {2, "NT_FPREGSET"},
  565. {3, "NT_PRPSINFO"},
  566. }
  567. func (i NType) String() string { return stringName(uint32(i), ntypeStrings, false) }
  568. func (i NType) GoString() string { return stringName(uint32(i), ntypeStrings, true) }
  569. /* Symbol Binding - ELFNN_ST_BIND - st_info */
  570. type SymBind int
  571. const (
  572. STB_LOCAL SymBind = 0 /* Local symbol */
  573. STB_GLOBAL SymBind = 1 /* Global symbol */
  574. STB_WEAK SymBind = 2 /* like global - lower precedence */
  575. STB_LOOS SymBind = 10 /* Reserved range for operating system */
  576. STB_HIOS SymBind = 12 /* specific semantics. */
  577. STB_LOPROC SymBind = 13 /* reserved range for processor */
  578. STB_HIPROC SymBind = 15 /* specific semantics. */
  579. )
  580. var stbStrings = []intName{
  581. {0, "STB_LOCAL"},
  582. {1, "STB_GLOBAL"},
  583. {2, "STB_WEAK"},
  584. {10, "STB_LOOS"},
  585. {12, "STB_HIOS"},
  586. {13, "STB_LOPROC"},
  587. {15, "STB_HIPROC"},
  588. }
  589. func (i SymBind) String() string { return stringName(uint32(i), stbStrings, false) }
  590. func (i SymBind) GoString() string { return stringName(uint32(i), stbStrings, true) }
  591. /* Symbol type - ELFNN_ST_TYPE - st_info */
  592. type SymType int
  593. const (
  594. STT_NOTYPE SymType = 0 /* Unspecified type. */
  595. STT_OBJECT SymType = 1 /* Data object. */
  596. STT_FUNC SymType = 2 /* Function. */
  597. STT_SECTION SymType = 3 /* Section. */
  598. STT_FILE SymType = 4 /* Source file. */
  599. STT_COMMON SymType = 5 /* Uninitialized common block. */
  600. STT_TLS SymType = 6 /* TLS object. */
  601. STT_LOOS SymType = 10 /* Reserved range for operating system */
  602. STT_HIOS SymType = 12 /* specific semantics. */
  603. STT_LOPROC SymType = 13 /* reserved range for processor */
  604. STT_HIPROC SymType = 15 /* specific semantics. */
  605. )
  606. var sttStrings = []intName{
  607. {0, "STT_NOTYPE"},
  608. {1, "STT_OBJECT"},
  609. {2, "STT_FUNC"},
  610. {3, "STT_SECTION"},
  611. {4, "STT_FILE"},
  612. {5, "STT_COMMON"},
  613. {6, "STT_TLS"},
  614. {10, "STT_LOOS"},
  615. {12, "STT_HIOS"},
  616. {13, "STT_LOPROC"},
  617. {15, "STT_HIPROC"},
  618. }
  619. func (i SymType) String() string { return stringName(uint32(i), sttStrings, false) }
  620. func (i SymType) GoString() string { return stringName(uint32(i), sttStrings, true) }
  621. /* Symbol visibility - ELFNN_ST_VISIBILITY - st_other */
  622. type SymVis int
  623. const (
  624. STV_DEFAULT SymVis = 0x0 /* Default visibility (see binding). */
  625. STV_INTERNAL SymVis = 0x1 /* Special meaning in relocatable objects. */
  626. STV_HIDDEN SymVis = 0x2 /* Not visible. */
  627. STV_PROTECTED SymVis = 0x3 /* Visible but not preemptible. */
  628. )
  629. var stvStrings = []intName{
  630. {0x0, "STV_DEFAULT"},
  631. {0x1, "STV_INTERNAL"},
  632. {0x2, "STV_HIDDEN"},
  633. {0x3, "STV_PROTECTED"},
  634. }
  635. func (i SymVis) String() string { return stringName(uint32(i), stvStrings, false) }
  636. func (i SymVis) GoString() string { return stringName(uint32(i), stvStrings, true) }
  637. /*
  638. * Relocation types.
  639. */
  640. // Relocation types for x86-64.
  641. type R_X86_64 int
  642. const (
  643. R_X86_64_NONE R_X86_64 = 0 /* No relocation. */
  644. R_X86_64_64 R_X86_64 = 1 /* Add 64 bit symbol value. */
  645. R_X86_64_PC32 R_X86_64 = 2 /* PC-relative 32 bit signed sym value. */
  646. R_X86_64_GOT32 R_X86_64 = 3 /* PC-relative 32 bit GOT offset. */
  647. R_X86_64_PLT32 R_X86_64 = 4 /* PC-relative 32 bit PLT offset. */
  648. R_X86_64_COPY R_X86_64 = 5 /* Copy data from shared object. */
  649. R_X86_64_GLOB_DAT R_X86_64 = 6 /* Set GOT entry to data address. */
  650. R_X86_64_JMP_SLOT R_X86_64 = 7 /* Set GOT entry to code address. */
  651. R_X86_64_RELATIVE R_X86_64 = 8 /* Add load address of shared object. */
  652. R_X86_64_GOTPCREL R_X86_64 = 9 /* Add 32 bit signed pcrel offset to GOT. */
  653. R_X86_64_32 R_X86_64 = 10 /* Add 32 bit zero extended symbol value */
  654. R_X86_64_32S R_X86_64 = 11 /* Add 32 bit sign extended symbol value */
  655. R_X86_64_16 R_X86_64 = 12 /* Add 16 bit zero extended symbol value */
  656. R_X86_64_PC16 R_X86_64 = 13 /* Add 16 bit signed extended pc relative symbol value */
  657. R_X86_64_8 R_X86_64 = 14 /* Add 8 bit zero extended symbol value */
  658. R_X86_64_PC8 R_X86_64 = 15 /* Add 8 bit signed extended pc relative symbol value */
  659. R_X86_64_DTPMOD64 R_X86_64 = 16 /* ID of module containing symbol */
  660. R_X86_64_DTPOFF64 R_X86_64 = 17 /* Offset in TLS block */
  661. R_X86_64_TPOFF64 R_X86_64 = 18 /* Offset in static TLS block */
  662. R_X86_64_TLSGD R_X86_64 = 19 /* PC relative offset to GD GOT entry */
  663. R_X86_64_TLSLD R_X86_64 = 20 /* PC relative offset to LD GOT entry */
  664. R_X86_64_DTPOFF32 R_X86_64 = 21 /* Offset in TLS block */
  665. R_X86_64_GOTTPOFF R_X86_64 = 22 /* PC relative offset to IE GOT entry */
  666. R_X86_64_TPOFF32 R_X86_64 = 23 /* Offset in static TLS block */
  667. )
  668. var rx86_64Strings = []intName{
  669. {0, "R_X86_64_NONE"},
  670. {1, "R_X86_64_64"},
  671. {2, "R_X86_64_PC32"},
  672. {3, "R_X86_64_GOT32"},
  673. {4, "R_X86_64_PLT32"},
  674. {5, "R_X86_64_COPY"},
  675. {6, "R_X86_64_GLOB_DAT"},
  676. {7, "R_X86_64_JMP_SLOT"},
  677. {8, "R_X86_64_RELATIVE"},
  678. {9, "R_X86_64_GOTPCREL"},
  679. {10, "R_X86_64_32"},
  680. {11, "R_X86_64_32S"},
  681. {12, "R_X86_64_16"},
  682. {13, "R_X86_64_PC16"},
  683. {14, "R_X86_64_8"},
  684. {15, "R_X86_64_PC8"},
  685. {16, "R_X86_64_DTPMOD64"},
  686. {17, "R_X86_64_DTPOFF64"},
  687. {18, "R_X86_64_TPOFF64"},
  688. {19, "R_X86_64_TLSGD"},
  689. {20, "R_X86_64_TLSLD"},
  690. {21, "R_X86_64_DTPOFF32"},
  691. {22, "R_X86_64_GOTTPOFF"},
  692. {23, "R_X86_64_TPOFF32"},
  693. }
  694. func (i R_X86_64) String() string { return stringName(uint32(i), rx86_64Strings, false) }
  695. func (i R_X86_64) GoString() string { return stringName(uint32(i), rx86_64Strings, true) }
  696. // Relocation types for Alpha.
  697. type R_ALPHA int
  698. const (
  699. R_ALPHA_NONE R_ALPHA = 0 /* No reloc */
  700. R_ALPHA_REFLONG R_ALPHA = 1 /* Direct 32 bit */
  701. R_ALPHA_REFQUAD R_ALPHA = 2 /* Direct 64 bit */
  702. R_ALPHA_GPREL32 R_ALPHA = 3 /* GP relative 32 bit */
  703. R_ALPHA_LITERAL R_ALPHA = 4 /* GP relative 16 bit w/optimization */
  704. R_ALPHA_LITUSE R_ALPHA = 5 /* Optimization hint for LITERAL */
  705. R_ALPHA_GPDISP R_ALPHA = 6 /* Add displacement to GP */
  706. R_ALPHA_BRADDR R_ALPHA = 7 /* PC+4 relative 23 bit shifted */
  707. R_ALPHA_HINT R_ALPHA = 8 /* PC+4 relative 16 bit shifted */
  708. R_ALPHA_SREL16 R_ALPHA = 9 /* PC relative 16 bit */
  709. R_ALPHA_SREL32 R_ALPHA = 10 /* PC relative 32 bit */
  710. R_ALPHA_SREL64 R_ALPHA = 11 /* PC relative 64 bit */
  711. R_ALPHA_OP_PUSH R_ALPHA = 12 /* OP stack push */
  712. R_ALPHA_OP_STORE R_ALPHA = 13 /* OP stack pop and store */
  713. R_ALPHA_OP_PSUB R_ALPHA = 14 /* OP stack subtract */
  714. R_ALPHA_OP_PRSHIFT R_ALPHA = 15 /* OP stack right shift */
  715. R_ALPHA_GPVALUE R_ALPHA = 16
  716. R_ALPHA_GPRELHIGH R_ALPHA = 17
  717. R_ALPHA_GPRELLOW R_ALPHA = 18
  718. R_ALPHA_IMMED_GP_16 R_ALPHA = 19
  719. R_ALPHA_IMMED_GP_HI32 R_ALPHA = 20
  720. R_ALPHA_IMMED_SCN_HI32 R_ALPHA = 21
  721. R_ALPHA_IMMED_BR_HI32 R_ALPHA = 22
  722. R_ALPHA_IMMED_LO32 R_ALPHA = 23
  723. R_ALPHA_COPY R_ALPHA = 24 /* Copy symbol at runtime */
  724. R_ALPHA_GLOB_DAT R_ALPHA = 25 /* Create GOT entry */
  725. R_ALPHA_JMP_SLOT R_ALPHA = 26 /* Create PLT entry */
  726. R_ALPHA_RELATIVE R_ALPHA = 27 /* Adjust by program base */
  727. )
  728. var ralphaStrings = []intName{
  729. {0, "R_ALPHA_NONE"},
  730. {1, "R_ALPHA_REFLONG"},
  731. {2, "R_ALPHA_REFQUAD"},
  732. {3, "R_ALPHA_GPREL32"},
  733. {4, "R_ALPHA_LITERAL"},
  734. {5, "R_ALPHA_LITUSE"},
  735. {6, "R_ALPHA_GPDISP"},
  736. {7, "R_ALPHA_BRADDR"},
  737. {8, "R_ALPHA_HINT"},
  738. {9, "R_ALPHA_SREL16"},
  739. {10, "R_ALPHA_SREL32"},
  740. {11, "R_ALPHA_SREL64"},
  741. {12, "R_ALPHA_OP_PUSH"},
  742. {13, "R_ALPHA_OP_STORE"},
  743. {14, "R_ALPHA_OP_PSUB"},
  744. {15, "R_ALPHA_OP_PRSHIFT"},
  745. {16, "R_ALPHA_GPVALUE"},
  746. {17, "R_ALPHA_GPRELHIGH"},
  747. {18, "R_ALPHA_GPRELLOW"},
  748. {19, "R_ALPHA_IMMED_GP_16"},
  749. {20, "R_ALPHA_IMMED_GP_HI32"},
  750. {21, "R_ALPHA_IMMED_SCN_HI32"},
  751. {22, "R_ALPHA_IMMED_BR_HI32"},
  752. {23, "R_ALPHA_IMMED_LO32"},
  753. {24, "R_ALPHA_COPY"},
  754. {25, "R_ALPHA_GLOB_DAT"},
  755. {26, "R_ALPHA_JMP_SLOT"},
  756. {27, "R_ALPHA_RELATIVE"},
  757. }
  758. func (i R_ALPHA) String() string { return stringName(uint32(i), ralphaStrings, false) }
  759. func (i R_ALPHA) GoString() string { return stringName(uint32(i), ralphaStrings, true) }
  760. // Relocation types for ARM.
  761. type R_ARM int
  762. const (
  763. R_ARM_NONE R_ARM = 0 /* No relocation. */
  764. R_ARM_PC24 R_ARM = 1
  765. R_ARM_ABS32 R_ARM = 2
  766. R_ARM_REL32 R_ARM = 3
  767. R_ARM_PC13 R_ARM = 4
  768. R_ARM_ABS16 R_ARM = 5
  769. R_ARM_ABS12 R_ARM = 6
  770. R_ARM_THM_ABS5 R_ARM = 7
  771. R_ARM_ABS8 R_ARM = 8
  772. R_ARM_SBREL32 R_ARM = 9
  773. R_ARM_THM_PC22 R_ARM = 10
  774. R_ARM_THM_PC8 R_ARM = 11
  775. R_ARM_AMP_VCALL9 R_ARM = 12
  776. R_ARM_SWI24 R_ARM = 13
  777. R_ARM_THM_SWI8 R_ARM = 14
  778. R_ARM_XPC25 R_ARM = 15
  779. R_ARM_THM_XPC22 R_ARM = 16
  780. R_ARM_COPY R_ARM = 20 /* Copy data from shared object. */
  781. R_ARM_GLOB_DAT R_ARM = 21 /* Set GOT entry to data address. */
  782. R_ARM_JUMP_SLOT R_ARM = 22 /* Set GOT entry to code address. */
  783. R_ARM_RELATIVE R_ARM = 23 /* Add load address of shared object. */
  784. R_ARM_GOTOFF R_ARM = 24 /* Add GOT-relative symbol address. */
  785. R_ARM_GOTPC R_ARM = 25 /* Add PC-relative GOT table address. */
  786. R_ARM_GOT32 R_ARM = 26 /* Add PC-relative GOT offset. */
  787. R_ARM_PLT32 R_ARM = 27 /* Add PC-relative PLT offset. */
  788. R_ARM_GNU_VTENTRY R_ARM = 100
  789. R_ARM_GNU_VTINHERIT R_ARM = 101
  790. R_ARM_RSBREL32 R_ARM = 250
  791. R_ARM_THM_RPC22 R_ARM = 251
  792. R_ARM_RREL32 R_ARM = 252
  793. R_ARM_RABS32 R_ARM = 253
  794. R_ARM_RPC24 R_ARM = 254
  795. R_ARM_RBASE R_ARM = 255
  796. )
  797. var rarmStrings = []intName{
  798. {0, "R_ARM_NONE"},
  799. {1, "R_ARM_PC24"},
  800. {2, "R_ARM_ABS32"},
  801. {3, "R_ARM_REL32"},
  802. {4, "R_ARM_PC13"},
  803. {5, "R_ARM_ABS16"},
  804. {6, "R_ARM_ABS12"},
  805. {7, "R_ARM_THM_ABS5"},
  806. {8, "R_ARM_ABS8"},
  807. {9, "R_ARM_SBREL32"},
  808. {10, "R_ARM_THM_PC22"},
  809. {11, "R_ARM_THM_PC8"},
  810. {12, "R_ARM_AMP_VCALL9"},
  811. {13, "R_ARM_SWI24"},
  812. {14, "R_ARM_THM_SWI8"},
  813. {15, "R_ARM_XPC25"},
  814. {16, "R_ARM_THM_XPC22"},
  815. {20, "R_ARM_COPY"},
  816. {21, "R_ARM_GLOB_DAT"},
  817. {22, "R_ARM_JUMP_SLOT"},
  818. {23, "R_ARM_RELATIVE"},
  819. {24, "R_ARM_GOTOFF"},
  820. {25, "R_ARM_GOTPC"},
  821. {26, "R_ARM_GOT32"},
  822. {27, "R_ARM_PLT32"},
  823. {100, "R_ARM_GNU_VTENTRY"},
  824. {101, "R_ARM_GNU_VTINHERIT"},
  825. {250, "R_ARM_RSBREL32"},
  826. {251, "R_ARM_THM_RPC22"},
  827. {252, "R_ARM_RREL32"},
  828. {253, "R_ARM_RABS32"},
  829. {254, "R_ARM_RPC24"},
  830. {255, "R_ARM_RBASE"},
  831. }
  832. func (i R_ARM) String() string { return stringName(uint32(i), rarmStrings, false) }
  833. func (i R_ARM) GoString() string { return stringName(uint32(i), rarmStrings, true) }
  834. // Relocation types for 386.
  835. type R_386 int
  836. const (
  837. R_386_NONE R_386 = 0 /* No relocation. */
  838. R_386_32 R_386 = 1 /* Add symbol value. */
  839. R_386_PC32 R_386 = 2 /* Add PC-relative symbol value. */
  840. R_386_GOT32 R_386 = 3 /* Add PC-relative GOT offset. */
  841. R_386_PLT32 R_386 = 4 /* Add PC-relative PLT offset. */
  842. R_386_COPY R_386 = 5 /* Copy data from shared object. */
  843. R_386_GLOB_DAT R_386 = 6 /* Set GOT entry to data address. */
  844. R_386_JMP_SLOT R_386 = 7 /* Set GOT entry to code address. */
  845. R_386_RELATIVE R_386 = 8 /* Add load address of shared object. */
  846. R_386_GOTOFF R_386 = 9 /* Add GOT-relative symbol address. */
  847. R_386_GOTPC R_386 = 10 /* Add PC-relative GOT table address. */
  848. R_386_TLS_TPOFF R_386 = 14 /* Negative offset in static TLS block */
  849. R_386_TLS_IE R_386 = 15 /* Absolute address of GOT for -ve static TLS */
  850. R_386_TLS_GOTIE R_386 = 16 /* GOT entry for negative static TLS block */
  851. R_386_TLS_LE R_386 = 17 /* Negative offset relative to static TLS */
  852. R_386_TLS_GD R_386 = 18 /* 32 bit offset to GOT (index,off) pair */
  853. R_386_TLS_LDM R_386 = 19 /* 32 bit offset to GOT (index,zero) pair */
  854. R_386_TLS_GD_32 R_386 = 24 /* 32 bit offset to GOT (index,off) pair */
  855. R_386_TLS_GD_PUSH R_386 = 25 /* pushl instruction for Sun ABI GD sequence */
  856. R_386_TLS_GD_CALL R_386 = 26 /* call instruction for Sun ABI GD sequence */
  857. R_386_TLS_GD_POP R_386 = 27 /* popl instruction for Sun ABI GD sequence */
  858. R_386_TLS_LDM_32 R_386 = 28 /* 32 bit offset to GOT (index,zero) pair */
  859. R_386_TLS_LDM_PUSH R_386 = 29 /* pushl instruction for Sun ABI LD sequence */
  860. R_386_TLS_LDM_CALL R_386 = 30 /* call instruction for Sun ABI LD sequence */
  861. R_386_TLS_LDM_POP R_386 = 31 /* popl instruction for Sun ABI LD sequence */
  862. R_386_TLS_LDO_32 R_386 = 32 /* 32 bit offset from start of TLS block */
  863. R_386_TLS_IE_32 R_386 = 33 /* 32 bit offset to GOT static TLS offset entry */
  864. R_386_TLS_LE_32 R_386 = 34 /* 32 bit offset within static TLS block */
  865. R_386_TLS_DTPMOD32 R_386 = 35 /* GOT entry containing TLS index */
  866. R_386_TLS_DTPOFF32 R_386 = 36 /* GOT entry containing TLS offset */
  867. R_386_TLS_TPOFF32 R_386 = 37 /* GOT entry of -ve static TLS offset */
  868. )
  869. var r386Strings = []intName{
  870. {0, "R_386_NONE"},
  871. {1, "R_386_32"},
  872. {2, "R_386_PC32"},
  873. {3, "R_386_GOT32"},
  874. {4, "R_386_PLT32"},
  875. {5, "R_386_COPY"},
  876. {6, "R_386_GLOB_DAT"},
  877. {7, "R_386_JMP_SLOT"},
  878. {8, "R_386_RELATIVE"},
  879. {9, "R_386_GOTOFF"},
  880. {10, "R_386_GOTPC"},
  881. {14, "R_386_TLS_TPOFF"},
  882. {15, "R_386_TLS_IE"},
  883. {16, "R_386_TLS_GOTIE"},
  884. {17, "R_386_TLS_LE"},
  885. {18, "R_386_TLS_GD"},
  886. {19, "R_386_TLS_LDM"},
  887. {24, "R_386_TLS_GD_32"},
  888. {25, "R_386_TLS_GD_PUSH"},
  889. {26, "R_386_TLS_GD_CALL"},
  890. {27, "R_386_TLS_GD_POP"},
  891. {28, "R_386_TLS_LDM_32"},
  892. {29, "R_386_TLS_LDM_PUSH"},
  893. {30, "R_386_TLS_LDM_CALL"},
  894. {31, "R_386_TLS_LDM_POP"},
  895. {32, "R_386_TLS_LDO_32"},
  896. {33, "R_386_TLS_IE_32"},
  897. {34, "R_386_TLS_LE_32"},
  898. {35, "R_386_TLS_DTPMOD32"},
  899. {36, "R_386_TLS_DTPOFF32"},
  900. {37, "R_386_TLS_TPOFF32"},
  901. }
  902. func (i R_386) String() string { return stringName(uint32(i), r386Strings, false) }
  903. func (i R_386) GoString() string { return stringName(uint32(i), r386Strings, true) }
  904. // Relocation types for PowerPC.
  905. type R_PPC int
  906. const (
  907. R_PPC_NONE R_PPC = 0 /* No relocation. */
  908. R_PPC_ADDR32 R_PPC = 1
  909. R_PPC_ADDR24 R_PPC = 2
  910. R_PPC_ADDR16 R_PPC = 3
  911. R_PPC_ADDR16_LO R_PPC = 4
  912. R_PPC_ADDR16_HI R_PPC = 5
  913. R_PPC_ADDR16_HA R_PPC = 6
  914. R_PPC_ADDR14 R_PPC = 7
  915. R_PPC_ADDR14_BRTAKEN R_PPC = 8
  916. R_PPC_ADDR14_BRNTAKEN R_PPC = 9
  917. R_PPC_REL24 R_PPC = 10
  918. R_PPC_REL14 R_PPC = 11
  919. R_PPC_REL14_BRTAKEN R_PPC = 12
  920. R_PPC_REL14_BRNTAKEN R_PPC = 13
  921. R_PPC_GOT16 R_PPC = 14
  922. R_PPC_GOT16_LO R_PPC = 15
  923. R_PPC_GOT16_HI R_PPC = 16
  924. R_PPC_GOT16_HA R_PPC = 17
  925. R_PPC_PLTREL24 R_PPC = 18
  926. R_PPC_COPY R_PPC = 19
  927. R_PPC_GLOB_DAT R_PPC = 20
  928. R_PPC_JMP_SLOT R_PPC = 21
  929. R_PPC_RELATIVE R_PPC = 22
  930. R_PPC_LOCAL24PC R_PPC = 23
  931. R_PPC_UADDR32 R_PPC = 24
  932. R_PPC_UADDR16 R_PPC = 25
  933. R_PPC_REL32 R_PPC = 26
  934. R_PPC_PLT32 R_PPC = 27
  935. R_PPC_PLTREL32 R_PPC = 28
  936. R_PPC_PLT16_LO R_PPC = 29
  937. R_PPC_PLT16_HI R_PPC = 30
  938. R_PPC_PLT16_HA R_PPC = 31
  939. R_PPC_SDAREL16 R_PPC = 32
  940. R_PPC_SECTOFF R_PPC = 33
  941. R_PPC_SECTOFF_LO R_PPC = 34
  942. R_PPC_SECTOFF_HI R_PPC = 35
  943. R_PPC_SECTOFF_HA R_PPC = 36
  944. R_PPC_TLS R_PPC = 67
  945. R_PPC_DTPMOD32 R_PPC = 68
  946. R_PPC_TPREL16 R_PPC = 69
  947. R_PPC_TPREL16_LO R_PPC = 70
  948. R_PPC_TPREL16_HI R_PPC = 71
  949. R_PPC_TPREL16_HA R_PPC = 72
  950. R_PPC_TPREL32 R_PPC = 73
  951. R_PPC_DTPREL16 R_PPC = 74
  952. R_PPC_DTPREL16_LO R_PPC = 75
  953. R_PPC_DTPREL16_HI R_PPC = 76
  954. R_PPC_DTPREL16_HA R_PPC = 77
  955. R_PPC_DTPREL32 R_PPC = 78
  956. R_PPC_GOT_TLSGD16 R_PPC = 79
  957. R_PPC_GOT_TLSGD16_LO R_PPC = 80
  958. R_PPC_GOT_TLSGD16_HI R_PPC = 81
  959. R_PPC_GOT_TLSGD16_HA R_PPC = 82
  960. R_PPC_GOT_TLSLD16 R_PPC = 83
  961. R_PPC_GOT_TLSLD16_LO R_PPC = 84
  962. R_PPC_GOT_TLSLD16_HI R_PPC = 85
  963. R_PPC_GOT_TLSLD16_HA R_PPC = 86
  964. R_PPC_GOT_TPREL16 R_PPC = 87
  965. R_PPC_GOT_TPREL16_LO R_PPC = 88
  966. R_PPC_GOT_TPREL16_HI R_PPC = 89
  967. R_PPC_GOT_TPREL16_HA R_PPC = 90
  968. R_PPC_EMB_NADDR32 R_PPC = 101
  969. R_PPC_EMB_NADDR16 R_PPC = 102
  970. R_PPC_EMB_NADDR16_LO R_PPC = 103
  971. R_PPC_EMB_NADDR16_HI R_PPC = 104
  972. R_PPC_EMB_NADDR16_HA R_PPC = 105
  973. R_PPC_EMB_SDAI16 R_PPC = 106
  974. R_PPC_EMB_SDA2I16 R_PPC = 107
  975. R_PPC_EMB_SDA2REL R_PPC = 108
  976. R_PPC_EMB_SDA21 R_PPC = 109
  977. R_PPC_EMB_MRKREF R_PPC = 110
  978. R_PPC_EMB_RELSEC16 R_PPC = 111
  979. R_PPC_EMB_RELST_LO R_PPC = 112
  980. R_PPC_EMB_RELST_HI R_PPC = 113
  981. R_PPC_EMB_RELST_HA R_PPC = 114
  982. R_PPC_EMB_BIT_FLD R_PPC = 115
  983. R_PPC_EMB_RELSDA R_PPC = 116
  984. )
  985. var rppcStrings = []intName{
  986. {0, "R_PPC_NONE"},
  987. {1, "R_PPC_ADDR32"},
  988. {2, "R_PPC_ADDR24"},
  989. {3, "R_PPC_ADDR16"},
  990. {4, "R_PPC_ADDR16_LO"},
  991. {5, "R_PPC_ADDR16_HI"},
  992. {6, "R_PPC_ADDR16_HA"},
  993. {7, "R_PPC_ADDR14"},
  994. {8, "R_PPC_ADDR14_BRTAKEN"},
  995. {9, "R_PPC_ADDR14_BRNTAKEN"},
  996. {10, "R_PPC_REL24"},
  997. {11, "R_PPC_REL14"},
  998. {12, "R_PPC_REL14_BRTAKEN"},
  999. {13, "R_PPC_REL14_BRNTAKEN"},
  1000. {14, "R_PPC_GOT16"},
  1001. {15, "R_PPC_GOT16_LO"},
  1002. {16, "R_PPC_GOT16_HI"},
  1003. {17, "R_PPC_GOT16_HA"},
  1004. {18, "R_PPC_PLTREL24"},
  1005. {19, "R_PPC_COPY"},
  1006. {20, "R_PPC_GLOB_DAT"},
  1007. {21, "R_PPC_JMP_SLOT"},
  1008. {22, "R_PPC_RELATIVE"},
  1009. {23, "R_PPC_LOCAL24PC"},
  1010. {24, "R_PPC_UADDR32"},
  1011. {25, "R_PPC_UADDR16"},
  1012. {26, "R_PPC_REL32"},
  1013. {27, "R_PPC_PLT32"},
  1014. {28, "R_PPC_PLTREL32"},
  1015. {29, "R_PPC_PLT16_LO"},
  1016. {30, "R_PPC_PLT16_HI"},
  1017. {31, "R_PPC_PLT16_HA"},
  1018. {32, "R_PPC_SDAREL16"},
  1019. {33, "R_PPC_SECTOFF"},
  1020. {34, "R_PPC_SECTOFF_LO"},
  1021. {35, "R_PPC_SECTOFF_HI"},
  1022. {36, "R_PPC_SECTOFF_HA"},
  1023. {67, "R_PPC_TLS"},
  1024. {68, "R_PPC_DTPMOD32"},
  1025. {69, "R_PPC_TPREL16"},
  1026. {70, "R_PPC_TPREL16_LO"},
  1027. {71, "R_PPC_TPREL16_HI"},
  1028. {72, "R_PPC_TPREL16_HA"},
  1029. {73, "R_PPC_TPREL32"},
  1030. {74, "R_PPC_DTPREL16"},
  1031. {75, "R_PPC_DTPREL16_LO"},
  1032. {76, "R_PPC_DTPREL16_HI"},
  1033. {77, "R_PPC_DTPREL16_HA"},
  1034. {78, "R_PPC_DTPREL32"},
  1035. {79, "R_PPC_GOT_TLSGD16"},
  1036. {80, "R_PPC_GOT_TLSGD16_LO"},
  1037. {81, "R_PPC_GOT_TLSGD16_HI"},
  1038. {82, "R_PPC_GOT_TLSGD16_HA"},
  1039. {83, "R_PPC_GOT_TLSLD16"},
  1040. {84, "R_PPC_GOT_TLSLD16_LO"},
  1041. {85, "R_PPC_GOT_TLSLD16_HI"},
  1042. {86, "R_PPC_GOT_TLSLD16_HA"},
  1043. {87, "R_PPC_GOT_TPREL16"},
  1044. {88, "R_PPC_GOT_TPREL16_LO"},
  1045. {89, "R_PPC_GOT_TPREL16_HI"},
  1046. {90, "R_PPC_GOT_TPREL16_HA"},
  1047. {101, "R_PPC_EMB_NADDR32"},
  1048. {102, "R_PPC_EMB_NADDR16"},
  1049. {103, "R_PPC_EMB_NADDR16_LO"},
  1050. {104, "R_PPC_EMB_NADDR16_HI"},
  1051. {105, "R_PPC_EMB_NADDR16_HA"},
  1052. {106, "R_PPC_EMB_SDAI16"},
  1053. {107, "R_PPC_EMB_SDA2I16"},
  1054. {108, "R_PPC_EMB_SDA2REL"},
  1055. {109, "R_PPC_EMB_SDA21"},
  1056. {110, "R_PPC_EMB_MRKREF"},
  1057. {111, "R_PPC_EMB_RELSEC16"},
  1058. {112, "R_PPC_EMB_RELST_LO"},
  1059. {113, "R_PPC_EMB_RELST_HI"},
  1060. {114, "R_PPC_EMB_RELST_HA"},
  1061. {115, "R_PPC_EMB_BIT_FLD"},
  1062. {116, "R_PPC_EMB_RELSDA"},
  1063. }
  1064. func (i R_PPC) String() string { return stringName(uint32(i), rppcStrings, false) }
  1065. func (i R_PPC) GoString() string { return stringName(uint32(i), rppcStrings, true) }
  1066. // Relocation types for SPARC.
  1067. type R_SPARC int
  1068. const (
  1069. R_SPARC_NONE R_SPARC = 0
  1070. R_SPARC_8 R_SPARC = 1
  1071. R_SPARC_16 R_SPARC = 2
  1072. R_SPARC_32 R_SPARC = 3
  1073. R_SPARC_DISP8 R_SPARC = 4
  1074. R_SPARC_DISP16 R_SPARC = 5
  1075. R_SPARC_DISP32 R_SPARC = 6
  1076. R_SPARC_WDISP30 R_SPARC = 7
  1077. R_SPARC_WDISP22 R_SPARC = 8
  1078. R_SPARC_HI22 R_SPARC = 9
  1079. R_SPARC_22 R_SPARC = 10
  1080. R_SPARC_13 R_SPARC = 11
  1081. R_SPARC_LO10 R_SPARC = 12
  1082. R_SPARC_GOT10 R_SPARC = 13
  1083. R_SPARC_GOT13 R_SPARC = 14
  1084. R_SPARC_GOT22 R_SPARC = 15
  1085. R_SPARC_PC10 R_SPARC = 16
  1086. R_SPARC_PC22 R_SPARC = 17
  1087. R_SPARC_WPLT30 R_SPARC = 18
  1088. R_SPARC_COPY R_SPARC = 19
  1089. R_SPARC_GLOB_DAT R_SPARC = 20
  1090. R_SPARC_JMP_SLOT R_SPARC = 21
  1091. R_SPARC_RELATIVE R_SPARC = 22
  1092. R_SPARC_UA32 R_SPARC = 23
  1093. R_SPARC_PLT32 R_SPARC = 24
  1094. R_SPARC_HIPLT22 R_SPARC = 25
  1095. R_SPARC_LOPLT10 R_SPARC = 26
  1096. R_SPARC_PCPLT32 R_SPARC = 27
  1097. R_SPARC_PCPLT22 R_SPARC = 28
  1098. R_SPARC_PCPLT10 R_SPARC = 29
  1099. R_SPARC_10 R_SPARC = 30
  1100. R_SPARC_11 R_SPARC = 31
  1101. R_SPARC_64 R_SPARC = 32
  1102. R_SPARC_OLO10 R_SPARC = 33
  1103. R_SPARC_HH22 R_SPARC = 34
  1104. R_SPARC_HM10 R_SPARC = 35
  1105. R_SPARC_LM22 R_SPARC = 36
  1106. R_SPARC_PC_HH22 R_SPARC = 37
  1107. R_SPARC_PC_HM10 R_SPARC = 38
  1108. R_SPARC_PC_LM22 R_SPARC = 39
  1109. R_SPARC_WDISP16 R_SPARC = 40
  1110. R_SPARC_WDISP19 R_SPARC = 41
  1111. R_SPARC_GLOB_JMP R_SPARC = 42
  1112. R_SPARC_7 R_SPARC = 43
  1113. R_SPARC_5 R_SPARC = 44
  1114. R_SPARC_6 R_SPARC = 45
  1115. R_SPARC_DISP64 R_SPARC = 46
  1116. R_SPARC_PLT64 R_SPARC = 47
  1117. R_SPARC_HIX22 R_SPARC = 48
  1118. R_SPARC_LOX10 R_SPARC = 49
  1119. R_SPARC_H44 R_SPARC = 50
  1120. R_SPARC_M44 R_SPARC = 51
  1121. R_SPARC_L44 R_SPARC = 52
  1122. R_SPARC_REGISTER R_SPARC = 53
  1123. R_SPARC_UA64 R_SPARC = 54
  1124. R_SPARC_UA16 R_SPARC = 55
  1125. )
  1126. var rsparcStrings = []intName{
  1127. {0, "R_SPARC_NONE"},
  1128. {1, "R_SPARC_8"},
  1129. {2, "R_SPARC_16"},
  1130. {3, "R_SPARC_32"},
  1131. {4, "R_SPARC_DISP8"},
  1132. {5, "R_SPARC_DISP16"},
  1133. {6, "R_SPARC_DISP32"},
  1134. {7, "R_SPARC_WDISP30"},
  1135. {8, "R_SPARC_WDISP22"},
  1136. {9, "R_SPARC_HI22"},
  1137. {10, "R_SPARC_22"},
  1138. {11, "R_SPARC_13"},
  1139. {12, "R_SPARC_LO10"},
  1140. {13, "R_SPARC_GOT10"},
  1141. {14, "R_SPARC_GOT13"},
  1142. {15, "R_SPARC_GOT22"},
  1143. {16, "R_SPARC_PC10"},
  1144. {17, "R_SPARC_PC22"},
  1145. {18, "R_SPARC_WPLT30"},
  1146. {19, "R_SPARC_COPY"},
  1147. {20, "R_SPARC_GLOB_DAT"},
  1148. {21, "R_SPARC_JMP_SLOT"},
  1149. {22, "R_SPARC_RELATIVE"},
  1150. {23, "R_SPARC_UA32"},
  1151. {24, "R_SPARC_PLT32"},
  1152. {25, "R_SPARC_HIPLT22"},
  1153. {26, "R_SPARC_LOPLT10"},
  1154. {27, "R_SPARC_PCPLT32"},
  1155. {28, "R_SPARC_PCPLT22"},
  1156. {29, "R_SPARC_PCPLT10"},
  1157. {30, "R_SPARC_10"},
  1158. {31, "R_SPARC_11"},
  1159. {32, "R_SPARC_64"},
  1160. {33, "R_SPARC_OLO10"},
  1161. {34, "R_SPARC_HH22"},
  1162. {35, "R_SPARC_HM10"},
  1163. {36, "R_SPARC_LM22"},
  1164. {37, "R_SPARC_PC_HH22"},
  1165. {38, "R_SPARC_PC_HM10"},
  1166. {39, "R_SPARC_PC_LM22"},
  1167. {40, "R_SPARC_WDISP16"},
  1168. {41, "R_SPARC_WDISP19"},
  1169. {42, "R_SPARC_GLOB_JMP"},
  1170. {43, "R_SPARC_7"},
  1171. {44, "R_SPARC_5"},
  1172. {45, "R_SPARC_6"},
  1173. {46, "R_SPARC_DISP64"},
  1174. {47, "R_SPARC_PLT64"},
  1175. {48, "R_SPARC_HIX22"},
  1176. {49, "R_SPARC_LOX10"},
  1177. {50, "R_SPARC_H44"},
  1178. {51, "R_SPARC_M44"},
  1179. {52, "R_SPARC_L44"},
  1180. {53, "R_SPARC_REGISTER"},
  1181. {54, "R_SPARC_UA64"},
  1182. {55, "R_SPARC_UA16"},
  1183. }
  1184. func (i R_SPARC) String() string { return stringName(uint32(i), rsparcStrings, false) }
  1185. func (i R_SPARC) GoString() string { return stringName(uint32(i), rsparcStrings, true) }
  1186. // Magic number for the elf trampoline, chosen wisely to be an immediate value.
  1187. const ARM_MAGIC_TRAMP_NUMBER = 0x5c000003
  1188. // ELF32 File header.
  1189. type Header32 struct {
  1190. Ident [EI_NIDENT]byte /* File identification. */
  1191. Type uint16 /* File type. */
  1192. Machine uint16 /* Machine architecture. */
  1193. Version uint32 /* ELF format version. */
  1194. Entry uint32 /* Entry point. */
  1195. Phoff uint32 /* Program header file offset. */
  1196. Shoff uint32 /* Section header file offset. */
  1197. Flags uint32 /* Architecture-specific flags. */
  1198. Ehsize uint16 /* Size of ELF header in bytes. */
  1199. Phentsize uint16 /* Size of program header entry. */
  1200. Phnum uint16 /* Number of program header entries. */
  1201. Shentsize uint16 /* Size of section header entry. */
  1202. Shnum uint16 /* Number of section header entries. */
  1203. Shstrndx uint16 /* Section name strings section. */
  1204. }
  1205. // ELF32 Section header.
  1206. type Section32 struct {
  1207. Name uint32 /* Section name (index into the section header string table). */
  1208. Type uint32 /* Section type. */
  1209. Flags uint32 /* Section flags. */
  1210. Addr uint32 /* Address in memory image. */
  1211. Off uint32 /* Offset in file. */
  1212. Size uint32 /* Size in bytes. */
  1213. Link uint32 /* Index of a related section. */
  1214. Info uint32 /* Depends on section type. */
  1215. Addralign uint32 /* Alignment in bytes. */
  1216. Entsize uint32 /* Size of each entry in section. */
  1217. }
  1218. // ELF32 Program header.
  1219. type Prog32 struct {
  1220. Type uint32 /* Entry type. */
  1221. Off uint32 /* File offset of contents. */
  1222. Vaddr uint32 /* Virtual address in memory image. */
  1223. Paddr uint32 /* Physical address (not used). */
  1224. Filesz uint32 /* Size of contents in file. */
  1225. Memsz uint32 /* Size of contents in memory. */
  1226. Flags uint32 /* Access permission flags. */
  1227. Align uint32 /* Alignment in memory and file. */
  1228. }
  1229. // ELF32 Dynamic structure. The ".dynamic" section contains an array of them.
  1230. type Dyn32 struct {
  1231. Tag int32 /* Entry type. */
  1232. Val uint32 /* Integer/Address value. */
  1233. }
  1234. /*
  1235. * Relocation entries.
  1236. */
  1237. // ELF32 Relocations that don't need an addend field.
  1238. type Rel32 struct {
  1239. Off uint32 /* Location to be relocated. */
  1240. Info uint32 /* Relocation type and symbol index. */
  1241. }
  1242. // ELF32 Relocations that need an addend field.
  1243. type Rela32 struct {
  1244. Off uint32 /* Location to be relocated. */
  1245. Info uint32 /* Relocation type and symbol index. */
  1246. Addend int32 /* Addend. */
  1247. }
  1248. func R_SYM32(info uint32) uint32 { return uint32(info >> 8) }
  1249. func R_TYPE32(info uint32) uint32 { return uint32(info & 0xff) }
  1250. func R_INFO32(sym, typ uint32) uint32 { return sym<<8 | typ }
  1251. // ELF32 Symbol.
  1252. type Sym32 struct {
  1253. Name uint32
  1254. Value uint32
  1255. Size uint32
  1256. Info uint8
  1257. Other uint8
  1258. Shndx uint16
  1259. }
  1260. const Sym32Size = 16
  1261. func ST_BIND(info uint8) SymBind { return SymBind(info >> 4) }
  1262. func ST_TYPE(info uint8) SymType { return SymType(info & 0xF) }
  1263. func ST_INFO(bind SymBind, typ SymType) uint8 {
  1264. return uint8(bind)<<4 | uint8(typ)&0xf
  1265. }
  1266. func ST_VISIBILITY(other uint8) SymVis { return SymVis(other & 3) }
  1267. /*
  1268. * ELF64
  1269. */
  1270. // ELF64 file header.
  1271. type Header64 struct {
  1272. Ident [EI_NIDENT]byte /* File identification. */
  1273. Type uint16 /* File type. */
  1274. Machine uint16 /* Machine architecture. */
  1275. Version uint32 /* ELF format version. */
  1276. Entry uint64 /* Entry point. */
  1277. Phoff uint64 /* Program header file offset. */
  1278. Shoff uint64 /* Section header file offset. */
  1279. Flags uint32 /* Architecture-specific flags. */
  1280. Ehsize uint16 /* Size of ELF header in bytes. */
  1281. Phentsize uint16 /* Size of program header entry. */
  1282. Phnum uint16 /* Number of program header entries. */
  1283. Shentsize uint16 /* Size of section header entry. */
  1284. Shnum uint16 /* Number of section header entries. */
  1285. Shstrndx uint16 /* Section name strings section. */
  1286. }
  1287. // ELF64 Section header.
  1288. type Section64 struct {
  1289. Name uint32 /* Section name (index into the section header string table). */
  1290. Type uint32 /* Section type. */
  1291. Flags uint64 /* Section flags. */
  1292. Addr uint64 /* Address in memory image. */
  1293. Off uint64 /* Offset in file. */
  1294. Size uint64 /* Size in bytes. */
  1295. Link uint32 /* Index of a related section. */
  1296. Info uint32 /* Depends on section type. */
  1297. Addralign uint64 /* Alignment in bytes. */
  1298. Entsize uint64 /* Size of each entry in section. */
  1299. }
  1300. // ELF64 Program header.
  1301. type Prog64 struct {
  1302. Type uint32 /* Entry type. */
  1303. Flags uint32 /* Access permission flags. */
  1304. Off uint64 /* File offset of contents. */
  1305. Vaddr uint64 /* Virtual address in memory image. */
  1306. Paddr uint64 /* Physical address (not used). */
  1307. Filesz uint64 /* Size of contents in file. */
  1308. Memsz uint64 /* Size of contents in memory. */
  1309. Align uint64 /* Alignment in memory and file. */
  1310. }
  1311. // ELF64 Dynamic structure. The ".dynamic" section contains an array of them.
  1312. type Dyn64 struct {
  1313. Tag int64 /* Entry type. */
  1314. Val uint64 /* Integer/address value */
  1315. }
  1316. /*
  1317. * Relocation entries.
  1318. */
  1319. /* ELF64 relocations that don't need an addend field. */
  1320. type Rel64 struct {
  1321. Off uint64 /* Location to be relocated. */
  1322. Info uint64 /* Relocation type and symbol index. */
  1323. }
  1324. /* ELF64 relocations that need an addend field. */
  1325. type Rela64 struct {
  1326. Off uint64 /* Location to be relocated. */
  1327. Info uint64 /* Relocation type and symbol index. */
  1328. Addend int64 /* Addend. */
  1329. }
  1330. func R_SYM64(info uint64) uint32 { return uint32(info >> 32) }
  1331. func R_TYPE64(info uint64) uint32 { return uint32(info) }
  1332. func R_INFO(sym, typ uint32) uint64 { return uint64(sym)<<32 | uint64(typ) }
  1333. // ELF64 symbol table entries.
  1334. type Sym64 struct {
  1335. Name uint32 /* String table index of name. */
  1336. Info uint8 /* Type and binding information. */
  1337. Other uint8 /* Reserved (not used). */
  1338. Shndx uint16 /* Section index of symbol. */
  1339. Value uint64 /* Symbol value. */
  1340. Size uint64 /* Size of associated object. */
  1341. }
  1342. const Sym64Size = 24
  1343. type intName struct {
  1344. i uint32
  1345. s string
  1346. }
  1347. func stringName(i uint32, names []intName, goSyntax bool) string {
  1348. for _, n := range names {
  1349. if n.i == i {
  1350. if goSyntax {
  1351. return "elf." + n.s
  1352. }
  1353. return n.s
  1354. }
  1355. }
  1356. // second pass - look for smaller to add with.
  1357. // assume sorted already
  1358. for j := len(names) - 1; j >= 0; j-- {
  1359. n := names[j]
  1360. if n.i < i {
  1361. s := n.s
  1362. if goSyntax {
  1363. s = "elf." + s
  1364. }
  1365. return s + "+" + strconv.FormatUint(uint64(i-n.i), 10)
  1366. }
  1367. }
  1368. return strconv.FormatUint(uint64(i), 10)
  1369. }
  1370. func flagName(i uint32, names []intName, goSyntax bool) string {
  1371. s := ""
  1372. for _, n := range names {
  1373. if n.i&i == n.i {
  1374. if len(s) > 0 {
  1375. s += "+"
  1376. }
  1377. if goSyntax {
  1378. s += "elf."
  1379. }
  1380. s += n.s
  1381. i -= n.i
  1382. }
  1383. }
  1384. if len(s) == 0 {
  1385. return "0x" + strconv.FormatUint(uint64(i), 16)
  1386. }
  1387. if i != 0 {
  1388. s += "+0x" + strconv.FormatUint(uint64(i), 16)
  1389. }
  1390. return s
  1391. }