elfcpp.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200
  1. // elfcpp.h -- main header file for elfcpp -*- C++ -*-
  2. // Copyright (C) 2006-2017 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of elfcpp.
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Library General Public License
  7. // as published by the Free Software Foundation; either version 2, or
  8. // (at your option) any later version.
  9. // In addition to the permissions in the GNU Library General Public
  10. // License, the Free Software Foundation gives you unlimited
  11. // permission to link the compiled version of this file into
  12. // combinations with other programs, and to distribute those
  13. // combinations without any restriction coming from the use of this
  14. // file. (The Library Public License restrictions do apply in other
  15. // respects; for example, they cover modification of the file, and
  16. // distribution when not linked into a combined executable.)
  17. // This program is distributed in the hope that it will be useful, but
  18. // WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. // Library General Public License for more details.
  21. // You should have received a copy of the GNU Library General Public
  22. // License along with this program; if not, write to the Free Software
  23. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  24. // 02110-1301, USA.
  25. // This is the external interface for elfcpp.
  26. #ifndef ELFCPP_H
  27. #define ELFCPP_H
  28. #include "elfcpp_swap.h"
  29. #include <stdint.h>
  30. namespace elfcpp
  31. {
  32. // Basic ELF types.
  33. // These types are always the same size.
  34. typedef uint16_t Elf_Half;
  35. typedef uint32_t Elf_Word;
  36. typedef int32_t Elf_Sword;
  37. typedef uint64_t Elf_Xword;
  38. typedef int64_t Elf_Sxword;
  39. // These types vary in size depending on the ELF file class. The
  40. // template parameter should be 32 or 64.
  41. template<int size>
  42. struct Elf_types;
  43. template<>
  44. struct Elf_types<32>
  45. {
  46. typedef uint32_t Elf_Addr;
  47. typedef uint32_t Elf_Off;
  48. typedef uint32_t Elf_WXword;
  49. typedef int32_t Elf_Swxword;
  50. };
  51. template<>
  52. struct Elf_types<64>
  53. {
  54. typedef uint64_t Elf_Addr;
  55. typedef uint64_t Elf_Off;
  56. typedef uint64_t Elf_WXword;
  57. typedef int64_t Elf_Swxword;
  58. };
  59. // Offsets within the Ehdr e_ident field.
  60. const int EI_MAG0 = 0;
  61. const int EI_MAG1 = 1;
  62. const int EI_MAG2 = 2;
  63. const int EI_MAG3 = 3;
  64. const int EI_CLASS = 4;
  65. const int EI_DATA = 5;
  66. const int EI_VERSION = 6;
  67. const int EI_OSABI = 7;
  68. const int EI_ABIVERSION = 8;
  69. const int EI_PAD = 9;
  70. const int EI_NIDENT = 16;
  71. // The valid values found in Ehdr e_ident[EI_MAG0 through EI_MAG3].
  72. const int ELFMAG0 = 0x7f;
  73. const int ELFMAG1 = 'E';
  74. const int ELFMAG2 = 'L';
  75. const int ELFMAG3 = 'F';
  76. // The valid values found in Ehdr e_ident[EI_CLASS].
  77. enum
  78. {
  79. ELFCLASSNONE = 0,
  80. ELFCLASS32 = 1,
  81. ELFCLASS64 = 2
  82. };
  83. // The valid values found in Ehdr e_ident[EI_DATA].
  84. enum
  85. {
  86. ELFDATANONE = 0,
  87. ELFDATA2LSB = 1,
  88. ELFDATA2MSB = 2
  89. };
  90. // The valid values found in Ehdr e_ident[EI_VERSION] and e_version.
  91. enum
  92. {
  93. EV_NONE = 0,
  94. EV_CURRENT = 1
  95. };
  96. // The valid values found in Ehdr e_ident[EI_OSABI].
  97. enum ELFOSABI
  98. {
  99. ELFOSABI_NONE = 0,
  100. ELFOSABI_HPUX = 1,
  101. ELFOSABI_NETBSD = 2,
  102. ELFOSABI_GNU = 3,
  103. // ELFOSABI_LINUX is an alias for ELFOSABI_GNU.
  104. ELFOSABI_LINUX = 3,
  105. ELFOSABI_SOLARIS = 6,
  106. ELFOSABI_AIX = 7,
  107. ELFOSABI_IRIX = 8,
  108. ELFOSABI_FREEBSD = 9,
  109. ELFOSABI_TRU64 = 10,
  110. ELFOSABI_MODESTO = 11,
  111. ELFOSABI_OPENBSD = 12,
  112. ELFOSABI_OPENVMS = 13,
  113. ELFOSABI_NSK = 14,
  114. ELFOSABI_AROS = 15,
  115. // A GNU extension for the ARM.
  116. ELFOSABI_ARM = 97,
  117. // A GNU extension for the MSP.
  118. ELFOSABI_STANDALONE = 255
  119. };
  120. // The valid values found in the Ehdr e_type field.
  121. enum ET
  122. {
  123. ET_NONE = 0,
  124. ET_REL = 1,
  125. ET_EXEC = 2,
  126. ET_DYN = 3,
  127. ET_CORE = 4,
  128. ET_LOOS = 0xfe00,
  129. ET_HIOS = 0xfeff,
  130. ET_LOPROC = 0xff00,
  131. ET_HIPROC = 0xffff
  132. };
  133. // The valid values found in the Ehdr e_machine field.
  134. enum EM
  135. {
  136. EM_NONE = 0,
  137. EM_M32 = 1,
  138. EM_SPARC = 2,
  139. EM_386 = 3,
  140. EM_68K = 4,
  141. EM_88K = 5,
  142. EM_IAMCU = 6,
  143. EM_860 = 7,
  144. EM_MIPS = 8,
  145. EM_S370 = 9,
  146. EM_MIPS_RS3_LE = 10,
  147. // 11 was the old Sparc V9 ABI.
  148. // 12 through 14 are reserved.
  149. EM_PARISC = 15,
  150. // 16 is reserved.
  151. // Some old PowerPC object files use 17.
  152. EM_VPP500 = 17,
  153. EM_SPARC32PLUS = 18,
  154. EM_960 = 19,
  155. EM_PPC = 20,
  156. EM_PPC64 = 21,
  157. EM_S390 = 22,
  158. // 23 through 35 are served.
  159. EM_V800 = 36,
  160. EM_FR20 = 37,
  161. EM_RH32 = 38,
  162. EM_RCE = 39,
  163. EM_ARM = 40,
  164. EM_ALPHA = 41,
  165. EM_SH = 42,
  166. EM_SPARCV9 = 43,
  167. EM_TRICORE = 44,
  168. EM_ARC = 45,
  169. EM_H8_300 = 46,
  170. EM_H8_300H = 47,
  171. EM_H8S = 48,
  172. EM_H8_500 = 49,
  173. EM_IA_64 = 50,
  174. EM_MIPS_X = 51,
  175. EM_COLDFIRE = 52,
  176. EM_68HC12 = 53,
  177. EM_MMA = 54,
  178. EM_PCP = 55,
  179. EM_NCPU = 56,
  180. EM_NDR1 = 57,
  181. EM_STARCORE = 58,
  182. EM_ME16 = 59,
  183. EM_ST100 = 60,
  184. EM_TINYJ = 61,
  185. EM_X86_64 = 62,
  186. EM_PDSP = 63,
  187. EM_PDP10 = 64,
  188. EM_PDP11 = 65,
  189. EM_FX66 = 66,
  190. EM_ST9PLUS = 67,
  191. EM_ST7 = 68,
  192. EM_68HC16 = 69,
  193. EM_68HC11 = 70,
  194. EM_68HC08 = 71,
  195. EM_68HC05 = 72,
  196. EM_SVX = 73,
  197. EM_ST19 = 74,
  198. EM_VAX = 75,
  199. EM_CRIS = 76,
  200. EM_JAVELIN = 77,
  201. EM_FIREPATH = 78,
  202. EM_ZSP = 79,
  203. EM_MMIX = 80,
  204. EM_HUANY = 81,
  205. EM_PRISM = 82,
  206. EM_AVR = 83,
  207. EM_FR30 = 84,
  208. EM_D10V = 85,
  209. EM_D30V = 86,
  210. EM_V850 = 87,
  211. EM_M32R = 88,
  212. EM_MN10300 = 89,
  213. EM_MN10200 = 90,
  214. EM_PJ = 91,
  215. EM_OR1K = 92,
  216. EM_ARC_A5 = 93,
  217. EM_XTENSA = 94,
  218. EM_VIDEOCORE = 95,
  219. EM_TMM_GPP = 96,
  220. EM_NS32K = 97,
  221. EM_TPC = 98,
  222. // Some old picoJava object files use 99 (EM_PJ is correct).
  223. EM_SNP1K = 99,
  224. EM_ST200 = 100,
  225. EM_IP2K = 101,
  226. EM_MAX = 102,
  227. EM_CR = 103,
  228. EM_F2MC16 = 104,
  229. EM_MSP430 = 105,
  230. EM_BLACKFIN = 106,
  231. EM_SE_C33 = 107,
  232. EM_SEP = 108,
  233. EM_ARCA = 109,
  234. EM_UNICORE = 110,
  235. EM_ALTERA_NIOS2 = 113,
  236. EM_CRX = 114,
  237. EM_TI_PRU = 144,
  238. EM_AARCH64 = 183,
  239. EM_TILEGX = 191,
  240. // The Morph MT.
  241. EM_MT = 0x2530,
  242. // DLX.
  243. EM_DLX = 0x5aa5,
  244. // FRV.
  245. EM_FRV = 0x5441,
  246. // Infineon Technologies 16-bit microcontroller with C166-V2 core.
  247. EM_X16X = 0x4688,
  248. // Xstorym16
  249. EM_XSTORMY16 = 0xad45,
  250. // Renesas M32C
  251. EM_M32C = 0xfeb0,
  252. // Vitesse IQ2000
  253. EM_IQ2000 = 0xfeba,
  254. // NIOS
  255. EM_NIOS32 = 0xfebb
  256. // Old AVR objects used 0x1057 (EM_AVR is correct).
  257. // Old MSP430 objects used 0x1059 (EM_MSP430 is correct).
  258. // Old FR30 objects used 0x3330 (EM_FR30 is correct).
  259. // Old OpenRISC objects used 0x3426 and 0x8472 (EM_OR1K is correct).
  260. // Old D10V objects used 0x7650 (EM_D10V is correct).
  261. // Old D30V objects used 0x7676 (EM_D30V is correct).
  262. // Old IP2X objects used 0x8217 (EM_IP2K is correct).
  263. // Old PowerPC objects used 0x9025 (EM_PPC is correct).
  264. // Old Alpha objects used 0x9026 (EM_ALPHA is correct).
  265. // Old M32R objects used 0x9041 (EM_M32R is correct).
  266. // Old V850 objects used 0x9080 (EM_V850 is correct).
  267. // Old S/390 objects used 0xa390 (EM_S390 is correct).
  268. // Old Xtensa objects used 0xabc7 (EM_XTENSA is correct).
  269. // Old MN10300 objects used 0xbeef (EM_MN10300 is correct).
  270. // Old MN10200 objects used 0xdead (EM_MN10200 is correct).
  271. };
  272. // A special value found in the Ehdr e_phnum field.
  273. enum
  274. {
  275. // Number of program segments stored in sh_info field of first
  276. // section headre.
  277. PN_XNUM = 0xffff
  278. };
  279. // Special section indices.
  280. enum
  281. {
  282. SHN_UNDEF = 0,
  283. SHN_LORESERVE = 0xff00,
  284. SHN_LOPROC = 0xff00,
  285. SHN_HIPROC = 0xff1f,
  286. SHN_LOOS = 0xff20,
  287. SHN_HIOS = 0xff3f,
  288. SHN_ABS = 0xfff1,
  289. SHN_COMMON = 0xfff2,
  290. SHN_XINDEX = 0xffff,
  291. SHN_HIRESERVE = 0xffff,
  292. // Provide for initial and final section ordering in conjunction
  293. // with the SHF_LINK_ORDER and SHF_ORDERED section flags.
  294. SHN_BEFORE = 0xff00,
  295. SHN_AFTER = 0xff01,
  296. // x86_64 specific large common symbol.
  297. SHN_X86_64_LCOMMON = 0xff02
  298. };
  299. // The valid values found in the Shdr sh_type field.
  300. enum SHT
  301. {
  302. SHT_NULL = 0,
  303. SHT_PROGBITS = 1,
  304. SHT_SYMTAB = 2,
  305. SHT_STRTAB = 3,
  306. SHT_RELA = 4,
  307. SHT_HASH = 5,
  308. SHT_DYNAMIC = 6,
  309. SHT_NOTE = 7,
  310. SHT_NOBITS = 8,
  311. SHT_REL = 9,
  312. SHT_SHLIB = 10,
  313. SHT_DYNSYM = 11,
  314. SHT_INIT_ARRAY = 14,
  315. SHT_FINI_ARRAY = 15,
  316. SHT_PREINIT_ARRAY = 16,
  317. SHT_GROUP = 17,
  318. SHT_SYMTAB_SHNDX = 18,
  319. SHT_LOOS = 0x60000000,
  320. SHT_HIOS = 0x6fffffff,
  321. SHT_LOPROC = 0x70000000,
  322. SHT_HIPROC = 0x7fffffff,
  323. SHT_LOUSER = 0x80000000,
  324. SHT_HIUSER = 0xffffffff,
  325. // The remaining values are not in the standard.
  326. // Incremental build data.
  327. SHT_GNU_INCREMENTAL_INPUTS = 0x6fff4700,
  328. SHT_GNU_INCREMENTAL_SYMTAB = 0x6fff4701,
  329. SHT_GNU_INCREMENTAL_RELOCS = 0x6fff4702,
  330. SHT_GNU_INCREMENTAL_GOT_PLT = 0x6fff4703,
  331. // Object attributes.
  332. SHT_GNU_ATTRIBUTES = 0x6ffffff5,
  333. // GNU style dynamic hash table.
  334. SHT_GNU_HASH = 0x6ffffff6,
  335. // List of prelink dependencies.
  336. SHT_GNU_LIBLIST = 0x6ffffff7,
  337. // Versions defined by file.
  338. SHT_SUNW_verdef = 0x6ffffffd,
  339. SHT_GNU_verdef = 0x6ffffffd,
  340. // Versions needed by file.
  341. SHT_SUNW_verneed = 0x6ffffffe,
  342. SHT_GNU_verneed = 0x6ffffffe,
  343. // Symbol versions,
  344. SHT_SUNW_versym = 0x6fffffff,
  345. SHT_GNU_versym = 0x6fffffff,
  346. SHT_SPARC_GOTDATA = 0x70000000,
  347. // ARM-specific section types.
  348. // Exception Index table.
  349. SHT_ARM_EXIDX = 0x70000001,
  350. // BPABI DLL dynamic linking pre-emption map.
  351. SHT_ARM_PREEMPTMAP = 0x70000002,
  352. // Object file compatibility attributes.
  353. SHT_ARM_ATTRIBUTES = 0x70000003,
  354. // Support for debugging overlaid programs.
  355. SHT_ARM_DEBUGOVERLAY = 0x70000004,
  356. SHT_ARM_OVERLAYSECTION = 0x70000005,
  357. // x86_64 unwind information.
  358. SHT_X86_64_UNWIND = 0x70000001,
  359. // MIPS-specific section types.
  360. // Section contains register usage information.
  361. SHT_MIPS_REGINFO = 0x70000006,
  362. // Section contains miscellaneous options.
  363. SHT_MIPS_OPTIONS = 0x7000000d,
  364. // ABI related flags section.
  365. SHT_MIPS_ABIFLAGS = 0x7000002a,
  366. // AARCH64-specific section type.
  367. SHT_AARCH64_ATTRIBUTES = 0x70000003,
  368. // Link editor is to sort the entries in this section based on the
  369. // address specified in the associated symbol table entry.
  370. SHT_ORDERED = 0x7fffffff
  371. };
  372. // The valid bit flags found in the Shdr sh_flags field.
  373. enum SHF
  374. {
  375. SHF_WRITE = 0x1,
  376. SHF_ALLOC = 0x2,
  377. SHF_EXECINSTR = 0x4,
  378. SHF_MERGE = 0x10,
  379. SHF_STRINGS = 0x20,
  380. SHF_INFO_LINK = 0x40,
  381. SHF_LINK_ORDER = 0x80,
  382. SHF_OS_NONCONFORMING = 0x100,
  383. SHF_GROUP = 0x200,
  384. SHF_TLS = 0x400,
  385. SHF_COMPRESSED = 0x800,
  386. SHF_MASKOS = 0x0ff00000,
  387. SHF_MASKPROC = 0xf0000000,
  388. // Indicates this section requires ordering in relation to
  389. // other sections of the same type. Ordered sections are
  390. // combined within the section pointed to by the sh_link entry.
  391. // The sh_info values SHN_BEFORE and SHN_AFTER imply that the
  392. // sorted section is to precede or follow, respectively, all
  393. // other sections in the set being ordered.
  394. SHF_ORDERED = 0x40000000,
  395. // This section is excluded from input to the link-edit of an
  396. // executable or shared object. This flag is ignored if SHF_ALLOC
  397. // is also set, or if relocations exist against the section.
  398. SHF_EXCLUDE = 0x80000000,
  399. // Section with data that is GP relative addressable.
  400. SHF_MIPS_GPREL = 0x10000000,
  401. // x86_64 specific large section.
  402. SHF_X86_64_LARGE = 0x10000000
  403. };
  404. // Values which appear in the first Elf_WXword of the section data
  405. // of a SHF_COMPRESSED section.
  406. enum
  407. {
  408. ELFCOMPRESS_ZLIB = 1,
  409. ELFCOMPRESS_LOOS = 0x60000000,
  410. ELFCOMPRESS_HIOS = 0x6fffffff,
  411. ELFCOMPRESS_LOPROC = 0x70000000,
  412. ELFCOMPRESS_HIPROC = 0x7fffffff,
  413. };
  414. // Bit flags which appear in the first 32-bit word of the section data
  415. // of a SHT_GROUP section.
  416. enum
  417. {
  418. GRP_COMDAT = 0x1,
  419. GRP_MASKOS = 0x0ff00000,
  420. GRP_MASKPROC = 0xf0000000
  421. };
  422. // The valid values found in the Phdr p_type field.
  423. enum PT
  424. {
  425. PT_NULL = 0,
  426. PT_LOAD = 1,
  427. PT_DYNAMIC = 2,
  428. PT_INTERP = 3,
  429. PT_NOTE = 4,
  430. PT_SHLIB = 5,
  431. PT_PHDR = 6,
  432. PT_TLS = 7,
  433. PT_LOOS = 0x60000000,
  434. PT_HIOS = 0x6fffffff,
  435. PT_LOPROC = 0x70000000,
  436. PT_HIPROC = 0x7fffffff,
  437. // The remaining values are not in the standard.
  438. // Frame unwind information.
  439. PT_GNU_EH_FRAME = 0x6474e550,
  440. PT_SUNW_EH_FRAME = 0x6474e550,
  441. // Stack flags.
  442. PT_GNU_STACK = 0x6474e551,
  443. // Read only after relocation.
  444. PT_GNU_RELRO = 0x6474e552,
  445. // Platform architecture compatibility information
  446. PT_ARM_ARCHEXT = 0x70000000,
  447. // Exception unwind tables
  448. PT_ARM_EXIDX = 0x70000001,
  449. // Register usage information. Identifies one .reginfo section.
  450. PT_MIPS_REGINFO =0x70000000,
  451. // Runtime procedure table.
  452. PT_MIPS_RTPROC = 0x70000001,
  453. // .MIPS.options section.
  454. PT_MIPS_OPTIONS = 0x70000002,
  455. // .MIPS.abiflags section.
  456. PT_MIPS_ABIFLAGS = 0x70000003,
  457. // Platform architecture compatibility information
  458. PT_AARCH64_ARCHEXT = 0x70000000,
  459. // Exception unwind tables
  460. PT_AARCH64_UNWIND = 0x70000001,
  461. // 4k page table size
  462. PT_S390_PGSTE = 0x70000000,
  463. };
  464. // The valid bit flags found in the Phdr p_flags field.
  465. enum PF
  466. {
  467. PF_X = 0x1,
  468. PF_W = 0x2,
  469. PF_R = 0x4,
  470. PF_MASKOS = 0x0ff00000,
  471. PF_MASKPROC = 0xf0000000
  472. };
  473. // Symbol binding from Sym st_info field.
  474. enum STB
  475. {
  476. STB_LOCAL = 0,
  477. STB_GLOBAL = 1,
  478. STB_WEAK = 2,
  479. STB_LOOS = 10,
  480. STB_GNU_UNIQUE = 10,
  481. STB_HIOS = 12,
  482. STB_LOPROC = 13,
  483. STB_HIPROC = 15
  484. };
  485. // Symbol types from Sym st_info field.
  486. enum STT
  487. {
  488. STT_NOTYPE = 0,
  489. STT_OBJECT = 1,
  490. STT_FUNC = 2,
  491. STT_SECTION = 3,
  492. STT_FILE = 4,
  493. STT_COMMON = 5,
  494. STT_TLS = 6,
  495. // GNU extension: symbol value points to a function which is called
  496. // at runtime to determine the final value of the symbol.
  497. STT_GNU_IFUNC = 10,
  498. STT_LOOS = 10,
  499. STT_HIOS = 12,
  500. STT_LOPROC = 13,
  501. STT_HIPROC = 15,
  502. // The section type that must be used for register symbols on
  503. // Sparc. These symbols initialize a global register.
  504. STT_SPARC_REGISTER = 13,
  505. // ARM: a THUMB function. This is not defined in ARM ELF Specification but
  506. // used by the GNU tool-chain.
  507. STT_ARM_TFUNC = 13
  508. };
  509. inline STB
  510. elf_st_bind(unsigned char info)
  511. {
  512. return static_cast<STB>(info >> 4);
  513. }
  514. inline STT
  515. elf_st_type(unsigned char info)
  516. {
  517. return static_cast<STT>(info & 0xf);
  518. }
  519. inline unsigned char
  520. elf_st_info(STB bind, STT type)
  521. {
  522. return ((static_cast<unsigned char>(bind) << 4)
  523. + (static_cast<unsigned char>(type) & 0xf));
  524. }
  525. // Symbol visibility from Sym st_other field.
  526. enum STV
  527. {
  528. STV_DEFAULT = 0,
  529. STV_INTERNAL = 1,
  530. STV_HIDDEN = 2,
  531. STV_PROTECTED = 3
  532. };
  533. inline STV
  534. elf_st_visibility(unsigned char other)
  535. {
  536. return static_cast<STV>(other & 0x3);
  537. }
  538. inline unsigned char
  539. elf_st_nonvis(unsigned char other)
  540. {
  541. return static_cast<STV>(other >> 2);
  542. }
  543. inline unsigned char
  544. elf_st_other(STV vis, unsigned char nonvis)
  545. {
  546. return ((nonvis << 2)
  547. + (static_cast<unsigned char>(vis) & 3));
  548. }
  549. // Reloc information from Rel/Rela r_info field.
  550. template<int size>
  551. unsigned int
  552. elf_r_sym(typename Elf_types<size>::Elf_WXword);
  553. template<>
  554. inline unsigned int
  555. elf_r_sym<32>(Elf_Word v)
  556. {
  557. return v >> 8;
  558. }
  559. template<>
  560. inline unsigned int
  561. elf_r_sym<64>(Elf_Xword v)
  562. {
  563. return v >> 32;
  564. }
  565. template<int size>
  566. unsigned int
  567. elf_r_type(typename Elf_types<size>::Elf_WXword);
  568. template<>
  569. inline unsigned int
  570. elf_r_type<32>(Elf_Word v)
  571. {
  572. return v & 0xff;
  573. }
  574. template<>
  575. inline unsigned int
  576. elf_r_type<64>(Elf_Xword v)
  577. {
  578. return v & 0xffffffff;
  579. }
  580. template<int size>
  581. typename Elf_types<size>::Elf_WXword
  582. elf_r_info(unsigned int s, unsigned int t);
  583. template<>
  584. inline Elf_Word
  585. elf_r_info<32>(unsigned int s, unsigned int t)
  586. {
  587. return (s << 8) + (t & 0xff);
  588. }
  589. template<>
  590. inline Elf_Xword
  591. elf_r_info<64>(unsigned int s, unsigned int t)
  592. {
  593. return (static_cast<Elf_Xword>(s) << 32) + (t & 0xffffffff);
  594. }
  595. // Dynamic tags found in the PT_DYNAMIC segment.
  596. enum DT
  597. {
  598. DT_NULL = 0,
  599. DT_NEEDED = 1,
  600. DT_PLTRELSZ = 2,
  601. DT_PLTGOT = 3,
  602. DT_HASH = 4,
  603. DT_STRTAB = 5,
  604. DT_SYMTAB = 6,
  605. DT_RELA = 7,
  606. DT_RELASZ = 8,
  607. DT_RELAENT = 9,
  608. DT_STRSZ = 10,
  609. DT_SYMENT = 11,
  610. DT_INIT = 12,
  611. DT_FINI = 13,
  612. DT_SONAME = 14,
  613. DT_RPATH = 15,
  614. DT_SYMBOLIC = 16,
  615. DT_REL = 17,
  616. DT_RELSZ = 18,
  617. DT_RELENT = 19,
  618. DT_PLTREL = 20,
  619. DT_DEBUG = 21,
  620. DT_TEXTREL = 22,
  621. DT_JMPREL = 23,
  622. DT_BIND_NOW = 24,
  623. DT_INIT_ARRAY = 25,
  624. DT_FINI_ARRAY = 26,
  625. DT_INIT_ARRAYSZ = 27,
  626. DT_FINI_ARRAYSZ = 28,
  627. DT_RUNPATH = 29,
  628. DT_FLAGS = 30,
  629. // This is used to mark a range of dynamic tags. It is not really
  630. // a tag value.
  631. DT_ENCODING = 32,
  632. DT_PREINIT_ARRAY = 32,
  633. DT_PREINIT_ARRAYSZ = 33,
  634. DT_LOOS = 0x6000000d,
  635. DT_HIOS = 0x6ffff000,
  636. DT_LOPROC = 0x70000000,
  637. DT_HIPROC = 0x7fffffff,
  638. // The remaining values are extensions used by GNU or Solaris.
  639. DT_VALRNGLO = 0x6ffffd00,
  640. DT_GNU_PRELINKED = 0x6ffffdf5,
  641. DT_GNU_CONFLICTSZ = 0x6ffffdf6,
  642. DT_GNU_LIBLISTSZ = 0x6ffffdf7,
  643. DT_CHECKSUM = 0x6ffffdf8,
  644. DT_PLTPADSZ = 0x6ffffdf9,
  645. DT_MOVEENT = 0x6ffffdfa,
  646. DT_MOVESZ = 0x6ffffdfb,
  647. DT_FEATURE = 0x6ffffdfc,
  648. DT_POSFLAG_1 = 0x6ffffdfd,
  649. DT_SYMINSZ = 0x6ffffdfe,
  650. DT_SYMINENT = 0x6ffffdff,
  651. DT_VALRNGHI = 0x6ffffdff,
  652. DT_ADDRRNGLO = 0x6ffffe00,
  653. DT_GNU_HASH = 0x6ffffef5,
  654. DT_TLSDESC_PLT = 0x6ffffef6,
  655. DT_TLSDESC_GOT = 0x6ffffef7,
  656. DT_GNU_CONFLICT = 0x6ffffef8,
  657. DT_GNU_LIBLIST = 0x6ffffef9,
  658. DT_CONFIG = 0x6ffffefa,
  659. DT_DEPAUDIT = 0x6ffffefb,
  660. DT_AUDIT = 0x6ffffefc,
  661. DT_PLTPAD = 0x6ffffefd,
  662. DT_MOVETAB = 0x6ffffefe,
  663. DT_SYMINFO = 0x6ffffeff,
  664. DT_ADDRRNGHI = 0x6ffffeff,
  665. DT_RELACOUNT = 0x6ffffff9,
  666. DT_RELCOUNT = 0x6ffffffa,
  667. DT_FLAGS_1 = 0x6ffffffb,
  668. DT_VERDEF = 0x6ffffffc,
  669. DT_VERDEFNUM = 0x6ffffffd,
  670. DT_VERNEED = 0x6ffffffe,
  671. DT_VERNEEDNUM = 0x6fffffff,
  672. DT_VERSYM = 0x6ffffff0,
  673. // Specify the value of _GLOBAL_OFFSET_TABLE_.
  674. DT_PPC_GOT = 0x70000000,
  675. // Specify the start of the .glink section.
  676. DT_PPC64_GLINK = 0x70000000,
  677. // Specify the start and size of the .opd section.
  678. DT_PPC64_OPD = 0x70000001,
  679. DT_PPC64_OPDSZ = 0x70000002,
  680. // Specify whether various optimisations are possible.
  681. DT_PPC64_OPT = 0x70000003,
  682. // The index of an STT_SPARC_REGISTER symbol within the DT_SYMTAB
  683. // symbol table. One dynamic entry exists for every STT_SPARC_REGISTER
  684. // symbol in the symbol table.
  685. DT_SPARC_REGISTER = 0x70000001,
  686. // MIPS specific dynamic array tags.
  687. // 32 bit version number for runtime linker interface.
  688. DT_MIPS_RLD_VERSION = 0x70000001,
  689. // Time stamp.
  690. DT_MIPS_TIME_STAMP = 0x70000002,
  691. // Checksum of external strings and common sizes.
  692. DT_MIPS_ICHECKSUM = 0x70000003,
  693. // Index of version string in string table.
  694. DT_MIPS_IVERSION = 0x70000004,
  695. // 32 bits of flags.
  696. DT_MIPS_FLAGS = 0x70000005,
  697. // Base address of the segment.
  698. DT_MIPS_BASE_ADDRESS = 0x70000006,
  699. // ???
  700. DT_MIPS_MSYM = 0x70000007,
  701. // Address of .conflict section.
  702. DT_MIPS_CONFLICT = 0x70000008,
  703. // Address of .liblist section.
  704. DT_MIPS_LIBLIST = 0x70000009,
  705. // Number of local global offset table entries.
  706. DT_MIPS_LOCAL_GOTNO = 0x7000000a,
  707. // Number of entries in the .conflict section.
  708. DT_MIPS_CONFLICTNO = 0x7000000b,
  709. // Number of entries in the .liblist section.
  710. DT_MIPS_LIBLISTNO = 0x70000010,
  711. // Number of entries in the .dynsym section.
  712. DT_MIPS_SYMTABNO = 0x70000011,
  713. // Index of first external dynamic symbol not referenced locally.
  714. DT_MIPS_UNREFEXTNO = 0x70000012,
  715. // Index of first dynamic symbol in global offset table.
  716. DT_MIPS_GOTSYM = 0x70000013,
  717. // Number of page table entries in global offset table.
  718. DT_MIPS_HIPAGENO = 0x70000014,
  719. // Address of run time loader map, used for debugging.
  720. DT_MIPS_RLD_MAP = 0x70000016,
  721. // Delta C++ class definition.
  722. DT_MIPS_DELTA_CLASS = 0x70000017,
  723. // Number of entries in DT_MIPS_DELTA_CLASS.
  724. DT_MIPS_DELTA_CLASS_NO = 0x70000018,
  725. // Delta C++ class instances.
  726. DT_MIPS_DELTA_INSTANCE = 0x70000019,
  727. // Number of entries in DT_MIPS_DELTA_INSTANCE.
  728. DT_MIPS_DELTA_INSTANCE_NO = 0x7000001a,
  729. // Delta relocations.
  730. DT_MIPS_DELTA_RELOC = 0x7000001b,
  731. // Number of entries in DT_MIPS_DELTA_RELOC.
  732. DT_MIPS_DELTA_RELOC_NO = 0x7000001c,
  733. // Delta symbols that Delta relocations refer to.
  734. DT_MIPS_DELTA_SYM = 0x7000001d,
  735. // Number of entries in DT_MIPS_DELTA_SYM.
  736. DT_MIPS_DELTA_SYM_NO = 0x7000001e,
  737. // Delta symbols that hold class declarations.
  738. DT_MIPS_DELTA_CLASSSYM = 0x70000020,
  739. // Number of entries in DT_MIPS_DELTA_CLASSSYM.
  740. DT_MIPS_DELTA_CLASSSYM_NO = 0x70000021,
  741. // Flags indicating information about C++ flavor.
  742. DT_MIPS_CXX_FLAGS = 0x70000022,
  743. // Pixie information (???).
  744. DT_MIPS_PIXIE_INIT = 0x70000023,
  745. // Address of .MIPS.symlib
  746. DT_MIPS_SYMBOL_LIB = 0x70000024,
  747. // The GOT index of the first PTE for a segment
  748. DT_MIPS_LOCALPAGE_GOTIDX = 0x70000025,
  749. // The GOT index of the first PTE for a local symbol
  750. DT_MIPS_LOCAL_GOTIDX = 0x70000026,
  751. // The GOT index of the first PTE for a hidden symbol
  752. DT_MIPS_HIDDEN_GOTIDX = 0x70000027,
  753. // The GOT index of the first PTE for a protected symbol
  754. DT_MIPS_PROTECTED_GOTIDX = 0x70000028,
  755. // Address of `.MIPS.options'.
  756. DT_MIPS_OPTIONS = 0x70000029,
  757. // Address of `.interface'.
  758. DT_MIPS_INTERFACE = 0x7000002a,
  759. // ???
  760. DT_MIPS_DYNSTR_ALIGN = 0x7000002b,
  761. // Size of the .interface section.
  762. DT_MIPS_INTERFACE_SIZE = 0x7000002c,
  763. // Size of rld_text_resolve function stored in the GOT.
  764. DT_MIPS_RLD_TEXT_RESOLVE_ADDR = 0x7000002d,
  765. // Default suffix of DSO to be added by rld on dlopen() calls.
  766. DT_MIPS_PERF_SUFFIX = 0x7000002e,
  767. // Size of compact relocation section (O32).
  768. DT_MIPS_COMPACT_SIZE = 0x7000002f,
  769. // GP value for auxiliary GOTs.
  770. DT_MIPS_GP_VALUE = 0x70000030,
  771. // Address of auxiliary .dynamic.
  772. DT_MIPS_AUX_DYNAMIC = 0x70000031,
  773. // Address of the base of the PLTGOT.
  774. DT_MIPS_PLTGOT = 0x70000032,
  775. // Points to the base of a writable PLT.
  776. DT_MIPS_RWPLT = 0x70000034,
  777. // Relative offset of run time loader map, used for debugging.
  778. DT_MIPS_RLD_MAP_REL = 0x70000035,
  779. DT_AUXILIARY = 0x7ffffffd,
  780. DT_USED = 0x7ffffffe,
  781. DT_FILTER = 0x7fffffff
  782. };
  783. // Flags found in the DT_FLAGS dynamic element.
  784. enum DF
  785. {
  786. DF_ORIGIN = 0x1,
  787. DF_SYMBOLIC = 0x2,
  788. DF_TEXTREL = 0x4,
  789. DF_BIND_NOW = 0x8,
  790. DF_STATIC_TLS = 0x10
  791. };
  792. // Flags found in the DT_FLAGS_1 dynamic element.
  793. enum DF_1
  794. {
  795. DF_1_NOW = 0x1,
  796. DF_1_GLOBAL = 0x2,
  797. DF_1_GROUP = 0x4,
  798. DF_1_NODELETE = 0x8,
  799. DF_1_LOADFLTR = 0x10,
  800. DF_1_INITFIRST = 0x20,
  801. DF_1_NOOPEN = 0x40,
  802. DF_1_ORIGIN = 0x80,
  803. DF_1_DIRECT = 0x100,
  804. DF_1_TRANS = 0x200,
  805. DF_1_INTERPOSE = 0x400,
  806. DF_1_NODEFLIB = 0x800,
  807. DF_1_NODUMP = 0x1000,
  808. DF_1_CONLFAT = 0x2000
  809. };
  810. // Version numbers which appear in the vd_version field of a Verdef
  811. // structure.
  812. const int VER_DEF_NONE = 0;
  813. const int VER_DEF_CURRENT = 1;
  814. // Version numbers which appear in the vn_version field of a Verneed
  815. // structure.
  816. const int VER_NEED_NONE = 0;
  817. const int VER_NEED_CURRENT = 1;
  818. // Bit flags which appear in vd_flags of Verdef and vna_flags of
  819. // Vernaux.
  820. const int VER_FLG_BASE = 0x1;
  821. const int VER_FLG_WEAK = 0x2;
  822. const int VER_FLG_INFO = 0x4;
  823. // Special constants found in the SHT_GNU_versym entries.
  824. const int VER_NDX_LOCAL = 0;
  825. const int VER_NDX_GLOBAL = 1;
  826. // A SHT_GNU_versym section holds 16-bit words. This bit is set if
  827. // the symbol is hidden and can only be seen when referenced using an
  828. // explicit version number. This is a GNU extension.
  829. const int VERSYM_HIDDEN = 0x8000;
  830. // This is the mask for the rest of the data in a word read from a
  831. // SHT_GNU_versym section.
  832. const int VERSYM_VERSION = 0x7fff;
  833. // Note descriptor type codes for notes in a non-core file with an
  834. // empty name.
  835. enum
  836. {
  837. // A version string.
  838. NT_VERSION = 1,
  839. // An architecture string.
  840. NT_ARCH = 2
  841. };
  842. // Note descriptor type codes for notes in a non-core file with the
  843. // name "GNU".
  844. enum
  845. {
  846. // The minimum ABI level. This is used by the dynamic linker to
  847. // describe the minimal kernel version on which a shared library may
  848. // be used. Th value should be four words. Word 0 is an OS
  849. // descriptor (see below). Word 1 is the major version of the ABI.
  850. // Word 2 is the minor version. Word 3 is the subminor version.
  851. NT_GNU_ABI_TAG = 1,
  852. // Hardware capabilities information. Word 0 is the number of
  853. // entries. Word 1 is a bitmask of enabled entries. The rest of
  854. // the descriptor is a series of entries, where each entry is a
  855. // single byte followed by a nul terminated string. The byte gives
  856. // the bit number to test if enabled in the bitmask.
  857. NT_GNU_HWCAP = 2,
  858. // The build ID as set by the linker's --build-id option. The
  859. // format of the descriptor depends on the build ID style.
  860. NT_GNU_BUILD_ID = 3,
  861. // The version of gold used to link. Th descriptor is just a
  862. // string.
  863. NT_GNU_GOLD_VERSION = 4
  864. };
  865. // The OS values which may appear in word 0 of a NT_GNU_ABI_TAG note.
  866. enum
  867. {
  868. ELF_NOTE_OS_LINUX = 0,
  869. ELF_NOTE_OS_GNU = 1,
  870. ELF_NOTE_OS_SOLARIS2 = 2,
  871. ELF_NOTE_OS_FREEBSD = 3,
  872. ELF_NOTE_OS_NETBSD = 4,
  873. ELF_NOTE_OS_SYLLABLE = 5
  874. };
  875. } // End namespace elfcpp.
  876. // Include internal details after defining the types.
  877. #include "elfcpp_internal.h"
  878. namespace elfcpp
  879. {
  880. // The offset of the ELF file header in the ELF file.
  881. const int file_header_offset = 0;
  882. // ELF structure sizes.
  883. template<int size>
  884. struct Elf_sizes
  885. {
  886. // Size of ELF file header.
  887. static const int ehdr_size = sizeof(internal::Ehdr_data<size>);
  888. // Size of ELF segment header.
  889. static const int phdr_size = sizeof(internal::Phdr_data<size>);
  890. // Size of ELF section header.
  891. static const int shdr_size = sizeof(internal::Shdr_data<size>);
  892. // Size of ELF compression header.
  893. static const int chdr_size = sizeof(internal::Chdr_data<size>);
  894. // Size of ELF symbol table entry.
  895. static const int sym_size = sizeof(internal::Sym_data<size>);
  896. // Sizes of ELF reloc entries.
  897. static const int rel_size = sizeof(internal::Rel_data<size>);
  898. static const int rela_size = sizeof(internal::Rela_data<size>);
  899. // Size of ELF dynamic entry.
  900. static const int dyn_size = sizeof(internal::Dyn_data<size>);
  901. // Size of ELF version structures.
  902. static const int verdef_size = sizeof(internal::Verdef_data);
  903. static const int verdaux_size = sizeof(internal::Verdaux_data);
  904. static const int verneed_size = sizeof(internal::Verneed_data);
  905. static const int vernaux_size = sizeof(internal::Vernaux_data);
  906. };
  907. // Accessor class for the ELF file header.
  908. template<int size, bool big_endian>
  909. class Ehdr
  910. {
  911. public:
  912. Ehdr(const unsigned char* p)
  913. : p_(reinterpret_cast<const internal::Ehdr_data<size>*>(p))
  914. { }
  915. template<typename File>
  916. Ehdr(File* file, typename File::Location loc)
  917. : p_(reinterpret_cast<const internal::Ehdr_data<size>*>(
  918. file->view(loc.file_offset, loc.data_size).data()))
  919. { }
  920. const unsigned char*
  921. get_e_ident() const
  922. { return this->p_->e_ident; }
  923. Elf_Half
  924. get_e_type() const
  925. { return Convert<16, big_endian>::convert_host(this->p_->e_type); }
  926. Elf_Half
  927. get_e_machine() const
  928. { return Convert<16, big_endian>::convert_host(this->p_->e_machine); }
  929. Elf_Word
  930. get_e_version() const
  931. { return Convert<32, big_endian>::convert_host(this->p_->e_version); }
  932. typename Elf_types<size>::Elf_Addr
  933. get_e_entry() const
  934. { return Convert<size, big_endian>::convert_host(this->p_->e_entry); }
  935. typename Elf_types<size>::Elf_Off
  936. get_e_phoff() const
  937. { return Convert<size, big_endian>::convert_host(this->p_->e_phoff); }
  938. typename Elf_types<size>::Elf_Off
  939. get_e_shoff() const
  940. { return Convert<size, big_endian>::convert_host(this->p_->e_shoff); }
  941. Elf_Word
  942. get_e_flags() const
  943. { return Convert<32, big_endian>::convert_host(this->p_->e_flags); }
  944. Elf_Half
  945. get_e_ehsize() const
  946. { return Convert<16, big_endian>::convert_host(this->p_->e_ehsize); }
  947. Elf_Half
  948. get_e_phentsize() const
  949. { return Convert<16, big_endian>::convert_host(this->p_->e_phentsize); }
  950. Elf_Half
  951. get_e_phnum() const
  952. { return Convert<16, big_endian>::convert_host(this->p_->e_phnum); }
  953. Elf_Half
  954. get_e_shentsize() const
  955. { return Convert<16, big_endian>::convert_host(this->p_->e_shentsize); }
  956. Elf_Half
  957. get_e_shnum() const
  958. { return Convert<16, big_endian>::convert_host(this->p_->e_shnum); }
  959. Elf_Half
  960. get_e_shstrndx() const
  961. { return Convert<16, big_endian>::convert_host(this->p_->e_shstrndx); }
  962. private:
  963. const internal::Ehdr_data<size>* p_;
  964. };
  965. // Write class for the ELF file header.
  966. template<int size, bool big_endian>
  967. class Ehdr_write
  968. {
  969. public:
  970. Ehdr_write(unsigned char* p)
  971. : p_(reinterpret_cast<internal::Ehdr_data<size>*>(p))
  972. { }
  973. void
  974. put_e_ident(const unsigned char v[EI_NIDENT]) const
  975. { memcpy(this->p_->e_ident, v, EI_NIDENT); }
  976. void
  977. put_e_type(Elf_Half v)
  978. { this->p_->e_type = Convert<16, big_endian>::convert_host(v); }
  979. void
  980. put_e_machine(Elf_Half v)
  981. { this->p_->e_machine = Convert<16, big_endian>::convert_host(v); }
  982. void
  983. put_e_version(Elf_Word v)
  984. { this->p_->e_version = Convert<32, big_endian>::convert_host(v); }
  985. void
  986. put_e_entry(typename Elf_types<size>::Elf_Addr v)
  987. { this->p_->e_entry = Convert<size, big_endian>::convert_host(v); }
  988. void
  989. put_e_phoff(typename Elf_types<size>::Elf_Off v)
  990. { this->p_->e_phoff = Convert<size, big_endian>::convert_host(v); }
  991. void
  992. put_e_shoff(typename Elf_types<size>::Elf_Off v)
  993. { this->p_->e_shoff = Convert<size, big_endian>::convert_host(v); }
  994. void
  995. put_e_flags(Elf_Word v)
  996. { this->p_->e_flags = Convert<32, big_endian>::convert_host(v); }
  997. void
  998. put_e_ehsize(Elf_Half v)
  999. { this->p_->e_ehsize = Convert<16, big_endian>::convert_host(v); }
  1000. void
  1001. put_e_phentsize(Elf_Half v)
  1002. { this->p_->e_phentsize = Convert<16, big_endian>::convert_host(v); }
  1003. void
  1004. put_e_phnum(Elf_Half v)
  1005. { this->p_->e_phnum = Convert<16, big_endian>::convert_host(v); }
  1006. void
  1007. put_e_shentsize(Elf_Half v)
  1008. { this->p_->e_shentsize = Convert<16, big_endian>::convert_host(v); }
  1009. void
  1010. put_e_shnum(Elf_Half v)
  1011. { this->p_->e_shnum = Convert<16, big_endian>::convert_host(v); }
  1012. void
  1013. put_e_shstrndx(Elf_Half v)
  1014. { this->p_->e_shstrndx = Convert<16, big_endian>::convert_host(v); }
  1015. private:
  1016. internal::Ehdr_data<size>* p_;
  1017. };
  1018. // Accessor class for an ELF section header.
  1019. template<int size, bool big_endian>
  1020. class Shdr
  1021. {
  1022. public:
  1023. Shdr(const unsigned char* p)
  1024. : p_(reinterpret_cast<const internal::Shdr_data<size>*>(p))
  1025. { }
  1026. template<typename File>
  1027. Shdr(File* file, typename File::Location loc)
  1028. : p_(reinterpret_cast<const internal::Shdr_data<size>*>(
  1029. file->view(loc.file_offset, loc.data_size).data()))
  1030. { }
  1031. Elf_Word
  1032. get_sh_name() const
  1033. { return Convert<32, big_endian>::convert_host(this->p_->sh_name); }
  1034. Elf_Word
  1035. get_sh_type() const
  1036. { return Convert<32, big_endian>::convert_host(this->p_->sh_type); }
  1037. typename Elf_types<size>::Elf_WXword
  1038. get_sh_flags() const
  1039. { return Convert<size, big_endian>::convert_host(this->p_->sh_flags); }
  1040. typename Elf_types<size>::Elf_Addr
  1041. get_sh_addr() const
  1042. { return Convert<size, big_endian>::convert_host(this->p_->sh_addr); }
  1043. typename Elf_types<size>::Elf_Off
  1044. get_sh_offset() const
  1045. { return Convert<size, big_endian>::convert_host(this->p_->sh_offset); }
  1046. typename Elf_types<size>::Elf_WXword
  1047. get_sh_size() const
  1048. { return Convert<size, big_endian>::convert_host(this->p_->sh_size); }
  1049. Elf_Word
  1050. get_sh_link() const
  1051. { return Convert<32, big_endian>::convert_host(this->p_->sh_link); }
  1052. Elf_Word
  1053. get_sh_info() const
  1054. { return Convert<32, big_endian>::convert_host(this->p_->sh_info); }
  1055. typename Elf_types<size>::Elf_WXword
  1056. get_sh_addralign() const
  1057. { return
  1058. Convert<size, big_endian>::convert_host(this->p_->sh_addralign); }
  1059. typename Elf_types<size>::Elf_WXword
  1060. get_sh_entsize() const
  1061. { return Convert<size, big_endian>::convert_host(this->p_->sh_entsize); }
  1062. private:
  1063. const internal::Shdr_data<size>* p_;
  1064. };
  1065. // Write class for an ELF section header.
  1066. template<int size, bool big_endian>
  1067. class Shdr_write
  1068. {
  1069. public:
  1070. Shdr_write(unsigned char* p)
  1071. : p_(reinterpret_cast<internal::Shdr_data<size>*>(p))
  1072. { }
  1073. void
  1074. put_sh_name(Elf_Word v)
  1075. { this->p_->sh_name = Convert<32, big_endian>::convert_host(v); }
  1076. void
  1077. put_sh_type(Elf_Word v)
  1078. { this->p_->sh_type = Convert<32, big_endian>::convert_host(v); }
  1079. void
  1080. put_sh_flags(typename Elf_types<size>::Elf_WXword v)
  1081. { this->p_->sh_flags = Convert<size, big_endian>::convert_host(v); }
  1082. void
  1083. put_sh_addr(typename Elf_types<size>::Elf_Addr v)
  1084. { this->p_->sh_addr = Convert<size, big_endian>::convert_host(v); }
  1085. void
  1086. put_sh_offset(typename Elf_types<size>::Elf_Off v)
  1087. { this->p_->sh_offset = Convert<size, big_endian>::convert_host(v); }
  1088. void
  1089. put_sh_size(typename Elf_types<size>::Elf_WXword v)
  1090. { this->p_->sh_size = Convert<size, big_endian>::convert_host(v); }
  1091. void
  1092. put_sh_link(Elf_Word v)
  1093. { this->p_->sh_link = Convert<32, big_endian>::convert_host(v); }
  1094. void
  1095. put_sh_info(Elf_Word v)
  1096. { this->p_->sh_info = Convert<32, big_endian>::convert_host(v); }
  1097. void
  1098. put_sh_addralign(typename Elf_types<size>::Elf_WXword v)
  1099. { this->p_->sh_addralign = Convert<size, big_endian>::convert_host(v); }
  1100. void
  1101. put_sh_entsize(typename Elf_types<size>::Elf_WXword v)
  1102. { this->p_->sh_entsize = Convert<size, big_endian>::convert_host(v); }
  1103. private:
  1104. internal::Shdr_data<size>* p_;
  1105. };
  1106. // Accessor class for an ELF compression header.
  1107. template<int size, bool big_endian>
  1108. class Chdr
  1109. {
  1110. public:
  1111. Chdr(const unsigned char* p)
  1112. : p_(reinterpret_cast<const internal::Chdr_data<size>*>(p))
  1113. { }
  1114. template<typename File>
  1115. Chdr(File* file, typename File::Location loc)
  1116. : p_(reinterpret_cast<const internal::Chdr_data<size>*>(
  1117. file->view(loc.file_offset, loc.data_size).data()))
  1118. { }
  1119. Elf_Word
  1120. get_ch_type() const
  1121. { return Convert<size, big_endian>::convert_host(this->p_->ch_type); }
  1122. typename Elf_types<size>::Elf_WXword
  1123. get_ch_size() const
  1124. { return Convert<size, big_endian>::convert_host(this->p_->ch_size); }
  1125. typename Elf_types<size>::Elf_WXword
  1126. get_ch_addralign() const
  1127. { return
  1128. Convert<size, big_endian>::convert_host(this->p_->ch_addralign); }
  1129. private:
  1130. const internal::Chdr_data<size>* p_;
  1131. };
  1132. // Write class for an ELF compression header.
  1133. template<int size, bool big_endian>
  1134. class Chdr_write
  1135. {
  1136. public:
  1137. Chdr_write(unsigned char* p)
  1138. : p_(reinterpret_cast<internal::Chdr_data<size>*>(p))
  1139. { }
  1140. void
  1141. put_ch_type(typename Elf_types<size>::Elf_WXword v)
  1142. { this->p_->ch_type = Convert<size, big_endian>::convert_host(v); }
  1143. void
  1144. put_ch_size(typename Elf_types<size>::Elf_WXword v)
  1145. { this->p_->ch_size = Convert<size, big_endian>::convert_host(v); }
  1146. void
  1147. put_ch_addralign(typename Elf_types<size>::Elf_WXword v)
  1148. { this->p_->ch_addralign = Convert<size, big_endian>::convert_host(v); }
  1149. void
  1150. put_ch_reserved(Elf_Word);
  1151. private:
  1152. internal::Chdr_data<size>* p_;
  1153. };
  1154. template<>
  1155. inline void
  1156. elfcpp::Chdr_write<64, true>::put_ch_reserved(Elf_Word v)
  1157. {
  1158. this->p_->ch_reserved = v;
  1159. }
  1160. template<>
  1161. inline void
  1162. elfcpp::Chdr_write<64, false>::put_ch_reserved(Elf_Word v)
  1163. {
  1164. this->p_->ch_reserved = v;
  1165. }
  1166. // Accessor class for an ELF segment header.
  1167. template<int size, bool big_endian>
  1168. class Phdr
  1169. {
  1170. public:
  1171. Phdr(const unsigned char* p)
  1172. : p_(reinterpret_cast<const internal::Phdr_data<size>*>(p))
  1173. { }
  1174. template<typename File>
  1175. Phdr(File* file, typename File::Location loc)
  1176. : p_(reinterpret_cast<internal::Phdr_data<size>*>(
  1177. file->view(loc.file_offset, loc.data_size).data()))
  1178. { }
  1179. Elf_Word
  1180. get_p_type() const
  1181. { return Convert<32, big_endian>::convert_host(this->p_->p_type); }
  1182. typename Elf_types<size>::Elf_Off
  1183. get_p_offset() const
  1184. { return Convert<size, big_endian>::convert_host(this->p_->p_offset); }
  1185. typename Elf_types<size>::Elf_Addr
  1186. get_p_vaddr() const
  1187. { return Convert<size, big_endian>::convert_host(this->p_->p_vaddr); }
  1188. typename Elf_types<size>::Elf_Addr
  1189. get_p_paddr() const
  1190. { return Convert<size, big_endian>::convert_host(this->p_->p_paddr); }
  1191. typename Elf_types<size>::Elf_WXword
  1192. get_p_filesz() const
  1193. { return Convert<size, big_endian>::convert_host(this->p_->p_filesz); }
  1194. typename Elf_types<size>::Elf_WXword
  1195. get_p_memsz() const
  1196. { return Convert<size, big_endian>::convert_host(this->p_->p_memsz); }
  1197. Elf_Word
  1198. get_p_flags() const
  1199. { return Convert<32, big_endian>::convert_host(this->p_->p_flags); }
  1200. typename Elf_types<size>::Elf_WXword
  1201. get_p_align() const
  1202. { return Convert<size, big_endian>::convert_host(this->p_->p_align); }
  1203. private:
  1204. const internal::Phdr_data<size>* p_;
  1205. };
  1206. // Write class for an ELF segment header.
  1207. template<int size, bool big_endian>
  1208. class Phdr_write
  1209. {
  1210. public:
  1211. Phdr_write(unsigned char* p)
  1212. : p_(reinterpret_cast<internal::Phdr_data<size>*>(p))
  1213. { }
  1214. void
  1215. put_p_type(Elf_Word v)
  1216. { this->p_->p_type = Convert<32, big_endian>::convert_host(v); }
  1217. void
  1218. put_p_offset(typename Elf_types<size>::Elf_Off v)
  1219. { this->p_->p_offset = Convert<size, big_endian>::convert_host(v); }
  1220. void
  1221. put_p_vaddr(typename Elf_types<size>::Elf_Addr v)
  1222. { this->p_->p_vaddr = Convert<size, big_endian>::convert_host(v); }
  1223. void
  1224. put_p_paddr(typename Elf_types<size>::Elf_Addr v)
  1225. { this->p_->p_paddr = Convert<size, big_endian>::convert_host(v); }
  1226. void
  1227. put_p_filesz(typename Elf_types<size>::Elf_WXword v)
  1228. { this->p_->p_filesz = Convert<size, big_endian>::convert_host(v); }
  1229. void
  1230. put_p_memsz(typename Elf_types<size>::Elf_WXword v)
  1231. { this->p_->p_memsz = Convert<size, big_endian>::convert_host(v); }
  1232. void
  1233. put_p_flags(Elf_Word v)
  1234. { this->p_->p_flags = Convert<32, big_endian>::convert_host(v); }
  1235. void
  1236. put_p_align(typename Elf_types<size>::Elf_WXword v)
  1237. { this->p_->p_align = Convert<size, big_endian>::convert_host(v); }
  1238. private:
  1239. internal::Phdr_data<size>* p_;
  1240. };
  1241. // Accessor class for an ELF symbol table entry.
  1242. template<int size, bool big_endian>
  1243. class Sym
  1244. {
  1245. public:
  1246. Sym(const unsigned char* p)
  1247. : p_(reinterpret_cast<const internal::Sym_data<size>*>(p))
  1248. { }
  1249. template<typename File>
  1250. Sym(File* file, typename File::Location loc)
  1251. : p_(reinterpret_cast<const internal::Sym_data<size>*>(
  1252. file->view(loc.file_offset, loc.data_size).data()))
  1253. { }
  1254. Elf_Word
  1255. get_st_name() const
  1256. { return Convert<32, big_endian>::convert_host(this->p_->st_name); }
  1257. typename Elf_types<size>::Elf_Addr
  1258. get_st_value() const
  1259. { return Convert<size, big_endian>::convert_host(this->p_->st_value); }
  1260. typename Elf_types<size>::Elf_WXword
  1261. get_st_size() const
  1262. { return Convert<size, big_endian>::convert_host(this->p_->st_size); }
  1263. unsigned char
  1264. get_st_info() const
  1265. { return this->p_->st_info; }
  1266. STB
  1267. get_st_bind() const
  1268. { return elf_st_bind(this->get_st_info()); }
  1269. STT
  1270. get_st_type() const
  1271. { return elf_st_type(this->get_st_info()); }
  1272. unsigned char
  1273. get_st_other() const
  1274. { return this->p_->st_other; }
  1275. STV
  1276. get_st_visibility() const
  1277. { return elf_st_visibility(this->get_st_other()); }
  1278. unsigned char
  1279. get_st_nonvis() const
  1280. { return elf_st_nonvis(this->get_st_other()); }
  1281. Elf_Half
  1282. get_st_shndx() const
  1283. { return Convert<16, big_endian>::convert_host(this->p_->st_shndx); }
  1284. private:
  1285. const internal::Sym_data<size>* p_;
  1286. };
  1287. // Writer class for an ELF symbol table entry.
  1288. template<int size, bool big_endian>
  1289. class Sym_write
  1290. {
  1291. public:
  1292. Sym_write(unsigned char* p)
  1293. : p_(reinterpret_cast<internal::Sym_data<size>*>(p))
  1294. { }
  1295. void
  1296. put_st_name(Elf_Word v)
  1297. { this->p_->st_name = Convert<32, big_endian>::convert_host(v); }
  1298. void
  1299. put_st_value(typename Elf_types<size>::Elf_Addr v)
  1300. { this->p_->st_value = Convert<size, big_endian>::convert_host(v); }
  1301. void
  1302. put_st_size(typename Elf_types<size>::Elf_WXword v)
  1303. { this->p_->st_size = Convert<size, big_endian>::convert_host(v); }
  1304. void
  1305. put_st_info(unsigned char v)
  1306. { this->p_->st_info = v; }
  1307. void
  1308. put_st_info(STB bind, STT type)
  1309. { this->p_->st_info = elf_st_info(bind, type); }
  1310. void
  1311. put_st_other(unsigned char v)
  1312. { this->p_->st_other = v; }
  1313. void
  1314. put_st_other(STV vis, unsigned char nonvis)
  1315. { this->p_->st_other = elf_st_other(vis, nonvis); }
  1316. void
  1317. put_st_shndx(Elf_Half v)
  1318. { this->p_->st_shndx = Convert<16, big_endian>::convert_host(v); }
  1319. Sym<size, big_endian>
  1320. sym()
  1321. { return Sym<size, big_endian>(reinterpret_cast<unsigned char*>(this->p_)); }
  1322. private:
  1323. internal::Sym_data<size>* p_;
  1324. };
  1325. // Accessor classes for an ELF REL relocation entry.
  1326. template<int size, bool big_endian>
  1327. class Rel
  1328. {
  1329. public:
  1330. Rel(const unsigned char* p)
  1331. : p_(reinterpret_cast<const internal::Rel_data<size>*>(p))
  1332. { }
  1333. template<typename File>
  1334. Rel(File* file, typename File::Location loc)
  1335. : p_(reinterpret_cast<const internal::Rel_data<size>*>(
  1336. file->view(loc.file_offset, loc.data_size).data()))
  1337. { }
  1338. typename Elf_types<size>::Elf_Addr
  1339. get_r_offset() const
  1340. { return Convert<size, big_endian>::convert_host(this->p_->r_offset); }
  1341. typename Elf_types<size>::Elf_WXword
  1342. get_r_info() const
  1343. { return Convert<size, big_endian>::convert_host(this->p_->r_info); }
  1344. private:
  1345. const internal::Rel_data<size>* p_;
  1346. };
  1347. // Writer class for an ELF Rel relocation.
  1348. template<int size, bool big_endian>
  1349. class Rel_write
  1350. {
  1351. public:
  1352. Rel_write(unsigned char* p)
  1353. : p_(reinterpret_cast<internal::Rel_data<size>*>(p))
  1354. { }
  1355. void
  1356. put_r_offset(typename Elf_types<size>::Elf_Addr v)
  1357. { this->p_->r_offset = Convert<size, big_endian>::convert_host(v); }
  1358. void
  1359. put_r_info(typename Elf_types<size>::Elf_WXword v)
  1360. { this->p_->r_info = Convert<size, big_endian>::convert_host(v); }
  1361. private:
  1362. internal::Rel_data<size>* p_;
  1363. };
  1364. // Accessor class for an ELF Rela relocation.
  1365. template<int size, bool big_endian>
  1366. class Rela
  1367. {
  1368. public:
  1369. Rela(const unsigned char* p)
  1370. : p_(reinterpret_cast<const internal::Rela_data<size>*>(p))
  1371. { }
  1372. template<typename File>
  1373. Rela(File* file, typename File::Location loc)
  1374. : p_(reinterpret_cast<const internal::Rela_data<size>*>(
  1375. file->view(loc.file_offset, loc.data_size).data()))
  1376. { }
  1377. typename Elf_types<size>::Elf_Addr
  1378. get_r_offset() const
  1379. { return Convert<size, big_endian>::convert_host(this->p_->r_offset); }
  1380. typename Elf_types<size>::Elf_WXword
  1381. get_r_info() const
  1382. { return Convert<size, big_endian>::convert_host(this->p_->r_info); }
  1383. typename Elf_types<size>::Elf_Swxword
  1384. get_r_addend() const
  1385. { return Convert<size, big_endian>::convert_host(this->p_->r_addend); }
  1386. private:
  1387. const internal::Rela_data<size>* p_;
  1388. };
  1389. // Writer class for an ELF Rela relocation.
  1390. template<int size, bool big_endian>
  1391. class Rela_write
  1392. {
  1393. public:
  1394. Rela_write(unsigned char* p)
  1395. : p_(reinterpret_cast<internal::Rela_data<size>*>(p))
  1396. { }
  1397. void
  1398. put_r_offset(typename Elf_types<size>::Elf_Addr v)
  1399. { this->p_->r_offset = Convert<size, big_endian>::convert_host(v); }
  1400. void
  1401. put_r_info(typename Elf_types<size>::Elf_WXword v)
  1402. { this->p_->r_info = Convert<size, big_endian>::convert_host(v); }
  1403. void
  1404. put_r_addend(typename Elf_types<size>::Elf_Swxword v)
  1405. { this->p_->r_addend = Convert<size, big_endian>::convert_host(v); }
  1406. private:
  1407. internal::Rela_data<size>* p_;
  1408. };
  1409. // MIPS-64 has a non-standard relocation layout.
  1410. template<bool big_endian>
  1411. class Mips64_rel
  1412. {
  1413. public:
  1414. Mips64_rel(const unsigned char* p)
  1415. : p_(reinterpret_cast<const internal::Mips64_rel_data*>(p))
  1416. { }
  1417. template<typename File>
  1418. Mips64_rel(File* file, typename File::Location loc)
  1419. : p_(reinterpret_cast<const internal::Mips64_rel_data*>(
  1420. file->view(loc.file_offset, loc.data_size).data()))
  1421. { }
  1422. typename Elf_types<64>::Elf_Addr
  1423. get_r_offset() const
  1424. { return Convert<64, big_endian>::convert_host(this->p_->r_offset); }
  1425. Elf_Word
  1426. get_r_sym() const
  1427. { return Convert<32, big_endian>::convert_host(this->p_->r_sym); }
  1428. unsigned char
  1429. get_r_ssym() const
  1430. { return this->p_->r_ssym; }
  1431. unsigned char
  1432. get_r_type() const
  1433. { return this->p_->r_type; }
  1434. unsigned char
  1435. get_r_type2() const
  1436. { return this->p_->r_type2; }
  1437. unsigned char
  1438. get_r_type3() const
  1439. { return this->p_->r_type3; }
  1440. private:
  1441. const internal::Mips64_rel_data* p_;
  1442. };
  1443. template<bool big_endian>
  1444. class Mips64_rel_write
  1445. {
  1446. public:
  1447. Mips64_rel_write(unsigned char* p)
  1448. : p_(reinterpret_cast<internal::Mips64_rel_data*>(p))
  1449. { }
  1450. void
  1451. put_r_offset(typename Elf_types<64>::Elf_Addr v)
  1452. { this->p_->r_offset = Convert<64, big_endian>::convert_host(v); }
  1453. void
  1454. put_r_sym(Elf_Word v)
  1455. { this->p_->r_sym = Convert<32, big_endian>::convert_host(v); }
  1456. void
  1457. put_r_ssym(unsigned char v)
  1458. { this->p_->r_ssym = v; }
  1459. void
  1460. put_r_type(unsigned char v)
  1461. { this->p_->r_type = v; }
  1462. void
  1463. put_r_type2(unsigned char v)
  1464. { this->p_->r_type2 = v; }
  1465. void
  1466. put_r_type3(unsigned char v)
  1467. { this->p_->r_type3 = v; }
  1468. private:
  1469. internal::Mips64_rel_data* p_;
  1470. };
  1471. template<bool big_endian>
  1472. class Mips64_rela
  1473. {
  1474. public:
  1475. Mips64_rela(const unsigned char* p)
  1476. : p_(reinterpret_cast<const internal::Mips64_rela_data*>(p))
  1477. { }
  1478. template<typename File>
  1479. Mips64_rela(File* file, typename File::Location loc)
  1480. : p_(reinterpret_cast<const internal::Mips64_rela_data*>(
  1481. file->view(loc.file_offset, loc.data_size).data()))
  1482. { }
  1483. typename Elf_types<64>::Elf_Addr
  1484. get_r_offset() const
  1485. { return Convert<64, big_endian>::convert_host(this->p_->r_offset); }
  1486. Elf_Word
  1487. get_r_sym() const
  1488. { return Convert<32, big_endian>::convert_host(this->p_->r_sym); }
  1489. unsigned char
  1490. get_r_ssym() const
  1491. { return this->p_->r_ssym; }
  1492. unsigned char
  1493. get_r_type() const
  1494. { return this->p_->r_type; }
  1495. unsigned char
  1496. get_r_type2() const
  1497. { return this->p_->r_type2; }
  1498. unsigned char
  1499. get_r_type3() const
  1500. { return this->p_->r_type3; }
  1501. typename Elf_types<64>::Elf_Swxword
  1502. get_r_addend() const
  1503. { return Convert<64, big_endian>::convert_host(this->p_->r_addend); }
  1504. private:
  1505. const internal::Mips64_rela_data* p_;
  1506. };
  1507. template<bool big_endian>
  1508. class Mips64_rela_write
  1509. {
  1510. public:
  1511. Mips64_rela_write(unsigned char* p)
  1512. : p_(reinterpret_cast<internal::Mips64_rela_data*>(p))
  1513. { }
  1514. void
  1515. put_r_offset(typename Elf_types<64>::Elf_Addr v)
  1516. { this->p_->r_offset = Convert<64, big_endian>::convert_host(v); }
  1517. void
  1518. put_r_sym(Elf_Word v)
  1519. { this->p_->r_sym = Convert<32, big_endian>::convert_host(v); }
  1520. void
  1521. put_r_ssym(unsigned char v)
  1522. { this->p_->r_ssym = v; }
  1523. void
  1524. put_r_type(unsigned char v)
  1525. { this->p_->r_type = v; }
  1526. void
  1527. put_r_type2(unsigned char v)
  1528. { this->p_->r_type2 = v; }
  1529. void
  1530. put_r_type3(unsigned char v)
  1531. { this->p_->r_type3 = v; }
  1532. void
  1533. put_r_addend(typename Elf_types<64>::Elf_Swxword v)
  1534. { this->p_->r_addend = Convert<64, big_endian>::convert_host(v); }
  1535. private:
  1536. internal::Mips64_rela_data* p_;
  1537. };
  1538. // Accessor classes for entries in the ELF SHT_DYNAMIC section aka
  1539. // PT_DYNAMIC segment.
  1540. template<int size, bool big_endian>
  1541. class Dyn
  1542. {
  1543. public:
  1544. Dyn(const unsigned char* p)
  1545. : p_(reinterpret_cast<const internal::Dyn_data<size>*>(p))
  1546. { }
  1547. template<typename File>
  1548. Dyn(File* file, typename File::Location loc)
  1549. : p_(reinterpret_cast<const internal::Dyn_data<size>*>(
  1550. file->view(loc.file_offset, loc.data_size).data()))
  1551. { }
  1552. typename Elf_types<size>::Elf_Swxword
  1553. get_d_tag() const
  1554. { return Convert<size, big_endian>::convert_host(this->p_->d_tag); }
  1555. typename Elf_types<size>::Elf_WXword
  1556. get_d_val() const
  1557. { return Convert<size, big_endian>::convert_host(this->p_->d_val); }
  1558. typename Elf_types<size>::Elf_Addr
  1559. get_d_ptr() const
  1560. { return Convert<size, big_endian>::convert_host(this->p_->d_val); }
  1561. private:
  1562. const internal::Dyn_data<size>* p_;
  1563. };
  1564. // Write class for an entry in the SHT_DYNAMIC section.
  1565. template<int size, bool big_endian>
  1566. class Dyn_write
  1567. {
  1568. public:
  1569. Dyn_write(unsigned char* p)
  1570. : p_(reinterpret_cast<internal::Dyn_data<size>*>(p))
  1571. { }
  1572. void
  1573. put_d_tag(typename Elf_types<size>::Elf_Swxword v)
  1574. { this->p_->d_tag = Convert<size, big_endian>::convert_host(v); }
  1575. void
  1576. put_d_val(typename Elf_types<size>::Elf_WXword v)
  1577. { this->p_->d_val = Convert<size, big_endian>::convert_host(v); }
  1578. void
  1579. put_d_ptr(typename Elf_types<size>::Elf_Addr v)
  1580. { this->p_->d_val = Convert<size, big_endian>::convert_host(v); }
  1581. private:
  1582. internal::Dyn_data<size>* p_;
  1583. };
  1584. // Accessor classes for entries in the ELF SHT_GNU_verdef section.
  1585. template<int size, bool big_endian>
  1586. class Verdef
  1587. {
  1588. public:
  1589. Verdef(const unsigned char* p)
  1590. : p_(reinterpret_cast<const internal::Verdef_data*>(p))
  1591. { }
  1592. template<typename File>
  1593. Verdef(File* file, typename File::Location loc)
  1594. : p_(reinterpret_cast<const internal::Verdef_data*>(
  1595. file->view(loc.file_offset, loc.data_size).data()))
  1596. { }
  1597. Elf_Half
  1598. get_vd_version() const
  1599. { return Convert<16, big_endian>::convert_host(this->p_->vd_version); }
  1600. Elf_Half
  1601. get_vd_flags() const
  1602. { return Convert<16, big_endian>::convert_host(this->p_->vd_flags); }
  1603. Elf_Half
  1604. get_vd_ndx() const
  1605. { return Convert<16, big_endian>::convert_host(this->p_->vd_ndx); }
  1606. Elf_Half
  1607. get_vd_cnt() const
  1608. { return Convert<16, big_endian>::convert_host(this->p_->vd_cnt); }
  1609. Elf_Word
  1610. get_vd_hash() const
  1611. { return Convert<32, big_endian>::convert_host(this->p_->vd_hash); }
  1612. Elf_Word
  1613. get_vd_aux() const
  1614. { return Convert<32, big_endian>::convert_host(this->p_->vd_aux); }
  1615. Elf_Word
  1616. get_vd_next() const
  1617. { return Convert<32, big_endian>::convert_host(this->p_->vd_next); }
  1618. private:
  1619. const internal::Verdef_data* p_;
  1620. };
  1621. template<int size, bool big_endian>
  1622. class Verdef_write
  1623. {
  1624. public:
  1625. Verdef_write(unsigned char* p)
  1626. : p_(reinterpret_cast<internal::Verdef_data*>(p))
  1627. { }
  1628. void
  1629. set_vd_version(Elf_Half v)
  1630. { this->p_->vd_version = Convert<16, big_endian>::convert_host(v); }
  1631. void
  1632. set_vd_flags(Elf_Half v)
  1633. { this->p_->vd_flags = Convert<16, big_endian>::convert_host(v); }
  1634. void
  1635. set_vd_ndx(Elf_Half v)
  1636. { this->p_->vd_ndx = Convert<16, big_endian>::convert_host(v); }
  1637. void
  1638. set_vd_cnt(Elf_Half v)
  1639. { this->p_->vd_cnt = Convert<16, big_endian>::convert_host(v); }
  1640. void
  1641. set_vd_hash(Elf_Word v)
  1642. { this->p_->vd_hash = Convert<32, big_endian>::convert_host(v); }
  1643. void
  1644. set_vd_aux(Elf_Word v)
  1645. { this->p_->vd_aux = Convert<32, big_endian>::convert_host(v); }
  1646. void
  1647. set_vd_next(Elf_Word v)
  1648. { this->p_->vd_next = Convert<32, big_endian>::convert_host(v); }
  1649. private:
  1650. internal::Verdef_data* p_;
  1651. };
  1652. // Accessor classes for auxiliary entries in the ELF SHT_GNU_verdef
  1653. // section.
  1654. template<int size, bool big_endian>
  1655. class Verdaux
  1656. {
  1657. public:
  1658. Verdaux(const unsigned char* p)
  1659. : p_(reinterpret_cast<const internal::Verdaux_data*>(p))
  1660. { }
  1661. template<typename File>
  1662. Verdaux(File* file, typename File::Location loc)
  1663. : p_(reinterpret_cast<const internal::Verdaux_data*>(
  1664. file->view(loc.file_offset, loc.data_size).data()))
  1665. { }
  1666. Elf_Word
  1667. get_vda_name() const
  1668. { return Convert<32, big_endian>::convert_host(this->p_->vda_name); }
  1669. Elf_Word
  1670. get_vda_next() const
  1671. { return Convert<32, big_endian>::convert_host(this->p_->vda_next); }
  1672. private:
  1673. const internal::Verdaux_data* p_;
  1674. };
  1675. template<int size, bool big_endian>
  1676. class Verdaux_write
  1677. {
  1678. public:
  1679. Verdaux_write(unsigned char* p)
  1680. : p_(reinterpret_cast<internal::Verdaux_data*>(p))
  1681. { }
  1682. void
  1683. set_vda_name(Elf_Word v)
  1684. { this->p_->vda_name = Convert<32, big_endian>::convert_host(v); }
  1685. void
  1686. set_vda_next(Elf_Word v)
  1687. { this->p_->vda_next = Convert<32, big_endian>::convert_host(v); }
  1688. private:
  1689. internal::Verdaux_data* p_;
  1690. };
  1691. // Accessor classes for entries in the ELF SHT_GNU_verneed section.
  1692. template<int size, bool big_endian>
  1693. class Verneed
  1694. {
  1695. public:
  1696. Verneed(const unsigned char* p)
  1697. : p_(reinterpret_cast<const internal::Verneed_data*>(p))
  1698. { }
  1699. template<typename File>
  1700. Verneed(File* file, typename File::Location loc)
  1701. : p_(reinterpret_cast<const internal::Verneed_data*>(
  1702. file->view(loc.file_offset, loc.data_size).data()))
  1703. { }
  1704. Elf_Half
  1705. get_vn_version() const
  1706. { return Convert<16, big_endian>::convert_host(this->p_->vn_version); }
  1707. Elf_Half
  1708. get_vn_cnt() const
  1709. { return Convert<16, big_endian>::convert_host(this->p_->vn_cnt); }
  1710. Elf_Word
  1711. get_vn_file() const
  1712. { return Convert<32, big_endian>::convert_host(this->p_->vn_file); }
  1713. Elf_Word
  1714. get_vn_aux() const
  1715. { return Convert<32, big_endian>::convert_host(this->p_->vn_aux); }
  1716. Elf_Word
  1717. get_vn_next() const
  1718. { return Convert<32, big_endian>::convert_host(this->p_->vn_next); }
  1719. private:
  1720. const internal::Verneed_data* p_;
  1721. };
  1722. template<int size, bool big_endian>
  1723. class Verneed_write
  1724. {
  1725. public:
  1726. Verneed_write(unsigned char* p)
  1727. : p_(reinterpret_cast<internal::Verneed_data*>(p))
  1728. { }
  1729. void
  1730. set_vn_version(Elf_Half v)
  1731. { this->p_->vn_version = Convert<16, big_endian>::convert_host(v); }
  1732. void
  1733. set_vn_cnt(Elf_Half v)
  1734. { this->p_->vn_cnt = Convert<16, big_endian>::convert_host(v); }
  1735. void
  1736. set_vn_file(Elf_Word v)
  1737. { this->p_->vn_file = Convert<32, big_endian>::convert_host(v); }
  1738. void
  1739. set_vn_aux(Elf_Word v)
  1740. { this->p_->vn_aux = Convert<32, big_endian>::convert_host(v); }
  1741. void
  1742. set_vn_next(Elf_Word v)
  1743. { this->p_->vn_next = Convert<32, big_endian>::convert_host(v); }
  1744. private:
  1745. internal::Verneed_data* p_;
  1746. };
  1747. // Accessor classes for auxiliary entries in the ELF SHT_GNU_verneed
  1748. // section.
  1749. template<int size, bool big_endian>
  1750. class Vernaux
  1751. {
  1752. public:
  1753. Vernaux(const unsigned char* p)
  1754. : p_(reinterpret_cast<const internal::Vernaux_data*>(p))
  1755. { }
  1756. template<typename File>
  1757. Vernaux(File* file, typename File::Location loc)
  1758. : p_(reinterpret_cast<const internal::Vernaux_data*>(
  1759. file->view(loc.file_offset, loc.data_size).data()))
  1760. { }
  1761. Elf_Word
  1762. get_vna_hash() const
  1763. { return Convert<32, big_endian>::convert_host(this->p_->vna_hash); }
  1764. Elf_Half
  1765. get_vna_flags() const
  1766. { return Convert<16, big_endian>::convert_host(this->p_->vna_flags); }
  1767. Elf_Half
  1768. get_vna_other() const
  1769. { return Convert<16, big_endian>::convert_host(this->p_->vna_other); }
  1770. Elf_Word
  1771. get_vna_name() const
  1772. { return Convert<32, big_endian>::convert_host(this->p_->vna_name); }
  1773. Elf_Word
  1774. get_vna_next() const
  1775. { return Convert<32, big_endian>::convert_host(this->p_->vna_next); }
  1776. private:
  1777. const internal::Vernaux_data* p_;
  1778. };
  1779. template<int size, bool big_endian>
  1780. class Vernaux_write
  1781. {
  1782. public:
  1783. Vernaux_write(unsigned char* p)
  1784. : p_(reinterpret_cast<internal::Vernaux_data*>(p))
  1785. { }
  1786. void
  1787. set_vna_hash(Elf_Word v)
  1788. { this->p_->vna_hash = Convert<32, big_endian>::convert_host(v); }
  1789. void
  1790. set_vna_flags(Elf_Half v)
  1791. { this->p_->vna_flags = Convert<16, big_endian>::convert_host(v); }
  1792. void
  1793. set_vna_other(Elf_Half v)
  1794. { this->p_->vna_other = Convert<16, big_endian>::convert_host(v); }
  1795. void
  1796. set_vna_name(Elf_Word v)
  1797. { this->p_->vna_name = Convert<32, big_endian>::convert_host(v); }
  1798. void
  1799. set_vna_next(Elf_Word v)
  1800. { this->p_->vna_next = Convert<32, big_endian>::convert_host(v); }
  1801. private:
  1802. internal::Vernaux_data* p_;
  1803. };
  1804. } // End namespace elfcpp.
  1805. #endif // !defined(ELFPCP_H)