elfcpp.h 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253
  1. // elfcpp.h -- main header file for elfcpp -*- C++ -*-
  2. // Copyright (C) 2006-2022 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. // CSKY-specific section types.
  369. // Object file compatibility attributes.
  370. SHT_CSKY_ATTRIBUTES = 0x70000001,
  371. // Link editor is to sort the entries in this section based on the
  372. // address specified in the associated symbol table entry.
  373. SHT_ORDERED = 0x7fffffff
  374. };
  375. // The valid bit flags found in the Shdr sh_flags field.
  376. enum SHF
  377. {
  378. SHF_WRITE = 0x1,
  379. SHF_ALLOC = 0x2,
  380. SHF_EXECINSTR = 0x4,
  381. SHF_MERGE = 0x10,
  382. SHF_STRINGS = 0x20,
  383. SHF_INFO_LINK = 0x40,
  384. SHF_LINK_ORDER = 0x80,
  385. SHF_OS_NONCONFORMING = 0x100,
  386. SHF_GROUP = 0x200,
  387. SHF_TLS = 0x400,
  388. SHF_COMPRESSED = 0x800,
  389. SHF_MASKOS = 0x0ff00000,
  390. SHF_GNU_RETAIN = 0x200000,
  391. SHF_MASKPROC = 0xf0000000,
  392. // Indicates this section requires ordering in relation to
  393. // other sections of the same type. Ordered sections are
  394. // combined within the section pointed to by the sh_link entry.
  395. // The sh_info values SHN_BEFORE and SHN_AFTER imply that the
  396. // sorted section is to precede or follow, respectively, all
  397. // other sections in the set being ordered.
  398. SHF_ORDERED = 0x40000000,
  399. // This section is excluded from input to the link-edit of an
  400. // executable or shared object. This flag is ignored if SHF_ALLOC
  401. // is also set, or if relocations exist against the section.
  402. SHF_EXCLUDE = 0x80000000,
  403. // Section with data that is GP relative addressable.
  404. SHF_MIPS_GPREL = 0x10000000,
  405. // x86_64 specific large section.
  406. SHF_X86_64_LARGE = 0x10000000
  407. };
  408. // Values which appear in the first Elf_WXword of the section data
  409. // of a SHF_COMPRESSED section.
  410. enum
  411. {
  412. ELFCOMPRESS_ZLIB = 1,
  413. ELFCOMPRESS_LOOS = 0x60000000,
  414. ELFCOMPRESS_HIOS = 0x6fffffff,
  415. ELFCOMPRESS_LOPROC = 0x70000000,
  416. ELFCOMPRESS_HIPROC = 0x7fffffff,
  417. };
  418. // Bit flags which appear in the first 32-bit word of the section data
  419. // of a SHT_GROUP section.
  420. enum
  421. {
  422. GRP_COMDAT = 0x1,
  423. GRP_MASKOS = 0x0ff00000,
  424. GRP_MASKPROC = 0xf0000000
  425. };
  426. // The valid values found in the Phdr p_type field.
  427. enum PT
  428. {
  429. PT_NULL = 0,
  430. PT_LOAD = 1,
  431. PT_DYNAMIC = 2,
  432. PT_INTERP = 3,
  433. PT_NOTE = 4,
  434. PT_SHLIB = 5,
  435. PT_PHDR = 6,
  436. PT_TLS = 7,
  437. PT_LOOS = 0x60000000,
  438. PT_HIOS = 0x6fffffff,
  439. PT_LOPROC = 0x70000000,
  440. PT_HIPROC = 0x7fffffff,
  441. // The remaining values are not in the standard.
  442. // Frame unwind information.
  443. PT_GNU_EH_FRAME = 0x6474e550,
  444. PT_SUNW_EH_FRAME = 0x6474e550,
  445. // Stack flags.
  446. PT_GNU_STACK = 0x6474e551,
  447. // Read only after relocation.
  448. PT_GNU_RELRO = 0x6474e552,
  449. // Platform architecture compatibility information
  450. PT_ARM_ARCHEXT = 0x70000000,
  451. // Exception unwind tables
  452. PT_ARM_EXIDX = 0x70000001,
  453. // Register usage information. Identifies one .reginfo section.
  454. PT_MIPS_REGINFO =0x70000000,
  455. // Runtime procedure table.
  456. PT_MIPS_RTPROC = 0x70000001,
  457. // .MIPS.options section.
  458. PT_MIPS_OPTIONS = 0x70000002,
  459. // .MIPS.abiflags section.
  460. PT_MIPS_ABIFLAGS = 0x70000003,
  461. // Platform architecture compatibility information
  462. PT_AARCH64_ARCHEXT = 0x70000000,
  463. // Exception unwind tables
  464. PT_AARCH64_UNWIND = 0x70000001,
  465. // 4k page table size
  466. PT_S390_PGSTE = 0x70000000,
  467. };
  468. // The valid bit flags found in the Phdr p_flags field.
  469. enum PF
  470. {
  471. PF_X = 0x1,
  472. PF_W = 0x2,
  473. PF_R = 0x4,
  474. PF_MASKOS = 0x0ff00000,
  475. PF_MASKPROC = 0xf0000000
  476. };
  477. // Symbol binding from Sym st_info field.
  478. enum STB
  479. {
  480. STB_LOCAL = 0,
  481. STB_GLOBAL = 1,
  482. STB_WEAK = 2,
  483. STB_LOOS = 10,
  484. STB_GNU_UNIQUE = 10,
  485. STB_HIOS = 12,
  486. STB_LOPROC = 13,
  487. STB_HIPROC = 15
  488. };
  489. // Symbol types from Sym st_info field.
  490. enum STT
  491. {
  492. STT_NOTYPE = 0,
  493. STT_OBJECT = 1,
  494. STT_FUNC = 2,
  495. STT_SECTION = 3,
  496. STT_FILE = 4,
  497. STT_COMMON = 5,
  498. STT_TLS = 6,
  499. // GNU extension: symbol value points to a function which is called
  500. // at runtime to determine the final value of the symbol.
  501. STT_GNU_IFUNC = 10,
  502. STT_LOOS = 10,
  503. STT_HIOS = 12,
  504. STT_LOPROC = 13,
  505. STT_HIPROC = 15,
  506. // The section type that must be used for register symbols on
  507. // Sparc. These symbols initialize a global register.
  508. STT_SPARC_REGISTER = 13,
  509. // ARM: a THUMB function. This is not defined in ARM ELF Specification but
  510. // used by the GNU tool-chain.
  511. STT_ARM_TFUNC = 13
  512. };
  513. inline STB
  514. elf_st_bind(unsigned char info)
  515. {
  516. return static_cast<STB>(info >> 4);
  517. }
  518. inline STT
  519. elf_st_type(unsigned char info)
  520. {
  521. return static_cast<STT>(info & 0xf);
  522. }
  523. inline unsigned char
  524. elf_st_info(STB bind, STT type)
  525. {
  526. return ((static_cast<unsigned char>(bind) << 4)
  527. + (static_cast<unsigned char>(type) & 0xf));
  528. }
  529. // Symbol visibility from Sym st_other field.
  530. enum STV
  531. {
  532. STV_DEFAULT = 0,
  533. STV_INTERNAL = 1,
  534. STV_HIDDEN = 2,
  535. STV_PROTECTED = 3
  536. };
  537. inline STV
  538. elf_st_visibility(unsigned char other)
  539. {
  540. return static_cast<STV>(other & 0x3);
  541. }
  542. inline unsigned char
  543. elf_st_nonvis(unsigned char other)
  544. {
  545. return static_cast<STV>(other >> 2);
  546. }
  547. inline unsigned char
  548. elf_st_other(STV vis, unsigned char nonvis)
  549. {
  550. return ((nonvis << 2)
  551. + (static_cast<unsigned char>(vis) & 3));
  552. }
  553. // Reloc information from Rel/Rela r_info field.
  554. template<int size>
  555. unsigned int
  556. elf_r_sym(typename Elf_types<size>::Elf_WXword);
  557. template<>
  558. inline unsigned int
  559. elf_r_sym<32>(Elf_Word v)
  560. {
  561. return v >> 8;
  562. }
  563. template<>
  564. inline unsigned int
  565. elf_r_sym<64>(Elf_Xword v)
  566. {
  567. return v >> 32;
  568. }
  569. template<int size>
  570. unsigned int
  571. elf_r_type(typename Elf_types<size>::Elf_WXword);
  572. template<>
  573. inline unsigned int
  574. elf_r_type<32>(Elf_Word v)
  575. {
  576. return v & 0xff;
  577. }
  578. template<>
  579. inline unsigned int
  580. elf_r_type<64>(Elf_Xword v)
  581. {
  582. return v & 0xffffffff;
  583. }
  584. template<int size>
  585. typename Elf_types<size>::Elf_WXword
  586. elf_r_info(unsigned int s, unsigned int t);
  587. template<>
  588. inline Elf_Word
  589. elf_r_info<32>(unsigned int s, unsigned int t)
  590. {
  591. return (s << 8) + (t & 0xff);
  592. }
  593. template<>
  594. inline Elf_Xword
  595. elf_r_info<64>(unsigned int s, unsigned int t)
  596. {
  597. return (static_cast<Elf_Xword>(s) << 32) + (t & 0xffffffff);
  598. }
  599. // Dynamic tags found in the PT_DYNAMIC segment.
  600. enum DT
  601. {
  602. DT_NULL = 0,
  603. DT_NEEDED = 1,
  604. DT_PLTRELSZ = 2,
  605. DT_PLTGOT = 3,
  606. DT_HASH = 4,
  607. DT_STRTAB = 5,
  608. DT_SYMTAB = 6,
  609. DT_RELA = 7,
  610. DT_RELASZ = 8,
  611. DT_RELAENT = 9,
  612. DT_STRSZ = 10,
  613. DT_SYMENT = 11,
  614. DT_INIT = 12,
  615. DT_FINI = 13,
  616. DT_SONAME = 14,
  617. DT_RPATH = 15,
  618. DT_SYMBOLIC = 16,
  619. DT_REL = 17,
  620. DT_RELSZ = 18,
  621. DT_RELENT = 19,
  622. DT_PLTREL = 20,
  623. DT_DEBUG = 21,
  624. DT_TEXTREL = 22,
  625. DT_JMPREL = 23,
  626. DT_BIND_NOW = 24,
  627. DT_INIT_ARRAY = 25,
  628. DT_FINI_ARRAY = 26,
  629. DT_INIT_ARRAYSZ = 27,
  630. DT_FINI_ARRAYSZ = 28,
  631. DT_RUNPATH = 29,
  632. DT_FLAGS = 30,
  633. // This is used to mark a range of dynamic tags. It is not really
  634. // a tag value.
  635. DT_ENCODING = 32,
  636. DT_PREINIT_ARRAY = 32,
  637. DT_PREINIT_ARRAYSZ = 33,
  638. DT_LOOS = 0x6000000d,
  639. DT_HIOS = 0x6ffff000,
  640. DT_LOPROC = 0x70000000,
  641. DT_HIPROC = 0x7fffffff,
  642. // The remaining values are extensions used by GNU or Solaris.
  643. DT_VALRNGLO = 0x6ffffd00,
  644. DT_GNU_FLAGS_1 = 0x6ffffdf4,
  645. DT_GNU_PRELINKED = 0x6ffffdf5,
  646. DT_GNU_CONFLICTSZ = 0x6ffffdf6,
  647. DT_GNU_LIBLISTSZ = 0x6ffffdf7,
  648. DT_CHECKSUM = 0x6ffffdf8,
  649. DT_PLTPADSZ = 0x6ffffdf9,
  650. DT_MOVEENT = 0x6ffffdfa,
  651. DT_MOVESZ = 0x6ffffdfb,
  652. DT_FEATURE = 0x6ffffdfc,
  653. DT_POSFLAG_1 = 0x6ffffdfd,
  654. DT_SYMINSZ = 0x6ffffdfe,
  655. DT_SYMINENT = 0x6ffffdff,
  656. DT_VALRNGHI = 0x6ffffdff,
  657. DT_ADDRRNGLO = 0x6ffffe00,
  658. DT_GNU_HASH = 0x6ffffef5,
  659. DT_TLSDESC_PLT = 0x6ffffef6,
  660. DT_TLSDESC_GOT = 0x6ffffef7,
  661. DT_GNU_CONFLICT = 0x6ffffef8,
  662. DT_GNU_LIBLIST = 0x6ffffef9,
  663. DT_CONFIG = 0x6ffffefa,
  664. DT_DEPAUDIT = 0x6ffffefb,
  665. DT_AUDIT = 0x6ffffefc,
  666. DT_PLTPAD = 0x6ffffefd,
  667. DT_MOVETAB = 0x6ffffefe,
  668. DT_SYMINFO = 0x6ffffeff,
  669. DT_ADDRRNGHI = 0x6ffffeff,
  670. DT_RELACOUNT = 0x6ffffff9,
  671. DT_RELCOUNT = 0x6ffffffa,
  672. DT_FLAGS_1 = 0x6ffffffb,
  673. DT_VERDEF = 0x6ffffffc,
  674. DT_VERDEFNUM = 0x6ffffffd,
  675. DT_VERNEED = 0x6ffffffe,
  676. DT_VERNEEDNUM = 0x6fffffff,
  677. DT_VERSYM = 0x6ffffff0,
  678. // Specify the value of _GLOBAL_OFFSET_TABLE_.
  679. DT_PPC_GOT = 0x70000000,
  680. // Specify whether various optimisations are possible.
  681. DT_PPC_OPT = 0x70000001,
  682. // Specify the start of the .glink section.
  683. DT_PPC64_GLINK = 0x70000000,
  684. // Specify the start and size of the .opd section.
  685. DT_PPC64_OPD = 0x70000001,
  686. DT_PPC64_OPDSZ = 0x70000002,
  687. // Specify whether various optimisations are possible.
  688. DT_PPC64_OPT = 0x70000003,
  689. // The index of an STT_SPARC_REGISTER symbol within the DT_SYMTAB
  690. // symbol table. One dynamic entry exists for every STT_SPARC_REGISTER
  691. // symbol in the symbol table.
  692. DT_SPARC_REGISTER = 0x70000001,
  693. // MIPS specific dynamic array tags.
  694. // 32 bit version number for runtime linker interface.
  695. DT_MIPS_RLD_VERSION = 0x70000001,
  696. // Time stamp.
  697. DT_MIPS_TIME_STAMP = 0x70000002,
  698. // Checksum of external strings and common sizes.
  699. DT_MIPS_ICHECKSUM = 0x70000003,
  700. // Index of version string in string table.
  701. DT_MIPS_IVERSION = 0x70000004,
  702. // 32 bits of flags.
  703. DT_MIPS_FLAGS = 0x70000005,
  704. // Base address of the segment.
  705. DT_MIPS_BASE_ADDRESS = 0x70000006,
  706. // ???
  707. DT_MIPS_MSYM = 0x70000007,
  708. // Address of .conflict section.
  709. DT_MIPS_CONFLICT = 0x70000008,
  710. // Address of .liblist section.
  711. DT_MIPS_LIBLIST = 0x70000009,
  712. // Number of local global offset table entries.
  713. DT_MIPS_LOCAL_GOTNO = 0x7000000a,
  714. // Number of entries in the .conflict section.
  715. DT_MIPS_CONFLICTNO = 0x7000000b,
  716. // Number of entries in the .liblist section.
  717. DT_MIPS_LIBLISTNO = 0x70000010,
  718. // Number of entries in the .dynsym section.
  719. DT_MIPS_SYMTABNO = 0x70000011,
  720. // Index of first external dynamic symbol not referenced locally.
  721. DT_MIPS_UNREFEXTNO = 0x70000012,
  722. // Index of first dynamic symbol in global offset table.
  723. DT_MIPS_GOTSYM = 0x70000013,
  724. // Number of page table entries in global offset table.
  725. DT_MIPS_HIPAGENO = 0x70000014,
  726. // Address of run time loader map, used for debugging.
  727. DT_MIPS_RLD_MAP = 0x70000016,
  728. // Delta C++ class definition.
  729. DT_MIPS_DELTA_CLASS = 0x70000017,
  730. // Number of entries in DT_MIPS_DELTA_CLASS.
  731. DT_MIPS_DELTA_CLASS_NO = 0x70000018,
  732. // Delta C++ class instances.
  733. DT_MIPS_DELTA_INSTANCE = 0x70000019,
  734. // Number of entries in DT_MIPS_DELTA_INSTANCE.
  735. DT_MIPS_DELTA_INSTANCE_NO = 0x7000001a,
  736. // Delta relocations.
  737. DT_MIPS_DELTA_RELOC = 0x7000001b,
  738. // Number of entries in DT_MIPS_DELTA_RELOC.
  739. DT_MIPS_DELTA_RELOC_NO = 0x7000001c,
  740. // Delta symbols that Delta relocations refer to.
  741. DT_MIPS_DELTA_SYM = 0x7000001d,
  742. // Number of entries in DT_MIPS_DELTA_SYM.
  743. DT_MIPS_DELTA_SYM_NO = 0x7000001e,
  744. // Delta symbols that hold class declarations.
  745. DT_MIPS_DELTA_CLASSSYM = 0x70000020,
  746. // Number of entries in DT_MIPS_DELTA_CLASSSYM.
  747. DT_MIPS_DELTA_CLASSSYM_NO = 0x70000021,
  748. // Flags indicating information about C++ flavor.
  749. DT_MIPS_CXX_FLAGS = 0x70000022,
  750. // Pixie information (???).
  751. DT_MIPS_PIXIE_INIT = 0x70000023,
  752. // Address of .MIPS.symlib
  753. DT_MIPS_SYMBOL_LIB = 0x70000024,
  754. // The GOT index of the first PTE for a segment
  755. DT_MIPS_LOCALPAGE_GOTIDX = 0x70000025,
  756. // The GOT index of the first PTE for a local symbol
  757. DT_MIPS_LOCAL_GOTIDX = 0x70000026,
  758. // The GOT index of the first PTE for a hidden symbol
  759. DT_MIPS_HIDDEN_GOTIDX = 0x70000027,
  760. // The GOT index of the first PTE for a protected symbol
  761. DT_MIPS_PROTECTED_GOTIDX = 0x70000028,
  762. // Address of `.MIPS.options'.
  763. DT_MIPS_OPTIONS = 0x70000029,
  764. // Address of `.interface'.
  765. DT_MIPS_INTERFACE = 0x7000002a,
  766. // ???
  767. DT_MIPS_DYNSTR_ALIGN = 0x7000002b,
  768. // Size of the .interface section.
  769. DT_MIPS_INTERFACE_SIZE = 0x7000002c,
  770. // Size of rld_text_resolve function stored in the GOT.
  771. DT_MIPS_RLD_TEXT_RESOLVE_ADDR = 0x7000002d,
  772. // Default suffix of DSO to be added by rld on dlopen() calls.
  773. DT_MIPS_PERF_SUFFIX = 0x7000002e,
  774. // Size of compact relocation section (O32).
  775. DT_MIPS_COMPACT_SIZE = 0x7000002f,
  776. // GP value for auxiliary GOTs.
  777. DT_MIPS_GP_VALUE = 0x70000030,
  778. // Address of auxiliary .dynamic.
  779. DT_MIPS_AUX_DYNAMIC = 0x70000031,
  780. // Address of the base of the PLTGOT.
  781. DT_MIPS_PLTGOT = 0x70000032,
  782. // Points to the base of a writable PLT.
  783. DT_MIPS_RWPLT = 0x70000034,
  784. // Relative offset of run time loader map, used for debugging.
  785. DT_MIPS_RLD_MAP_REL = 0x70000035,
  786. DT_AUXILIARY = 0x7ffffffd,
  787. DT_USED = 0x7ffffffe,
  788. DT_FILTER = 0x7fffffff
  789. };
  790. // Flags found in the DT_FLAGS dynamic element.
  791. enum DF
  792. {
  793. DF_ORIGIN = 0x1,
  794. DF_SYMBOLIC = 0x2,
  795. DF_TEXTREL = 0x4,
  796. DF_BIND_NOW = 0x8,
  797. DF_STATIC_TLS = 0x10
  798. };
  799. // Flags found in the DT_FLAGS_1 dynamic element.
  800. enum DF_1
  801. {
  802. DF_1_NOW = 0x1,
  803. DF_1_GLOBAL = 0x2,
  804. DF_1_GROUP = 0x4,
  805. DF_1_NODELETE = 0x8,
  806. DF_1_LOADFLTR = 0x10,
  807. DF_1_INITFIRST = 0x20,
  808. DF_1_NOOPEN = 0x40,
  809. DF_1_ORIGIN = 0x80,
  810. DF_1_DIRECT = 0x100,
  811. DF_1_TRANS = 0x200,
  812. DF_1_INTERPOSE = 0x400,
  813. DF_1_NODEFLIB = 0x800,
  814. DF_1_NODUMP = 0x1000,
  815. DF_1_CONLFAT = 0x2000,
  816. DF_1_PIE = 0x08000000
  817. };
  818. // Flags found in the DT_GNU_FLAGS_1 dynamic element.
  819. enum DF_GNU_1
  820. {
  821. DF_GNU_1_UNIQUE = 0x1,
  822. };
  823. // Version numbers which appear in the vd_version field of a Verdef
  824. // structure.
  825. const int VER_DEF_NONE = 0;
  826. const int VER_DEF_CURRENT = 1;
  827. // Version numbers which appear in the vn_version field of a Verneed
  828. // structure.
  829. const int VER_NEED_NONE = 0;
  830. const int VER_NEED_CURRENT = 1;
  831. // Bit flags which appear in vd_flags of Verdef and vna_flags of
  832. // Vernaux.
  833. const int VER_FLG_BASE = 0x1;
  834. const int VER_FLG_WEAK = 0x2;
  835. const int VER_FLG_INFO = 0x4;
  836. // Special constants found in the SHT_GNU_versym entries.
  837. const int VER_NDX_LOCAL = 0;
  838. const int VER_NDX_GLOBAL = 1;
  839. // A SHT_GNU_versym section holds 16-bit words. This bit is set if
  840. // the symbol is hidden and can only be seen when referenced using an
  841. // explicit version number. This is a GNU extension.
  842. const int VERSYM_HIDDEN = 0x8000;
  843. // This is the mask for the rest of the data in a word read from a
  844. // SHT_GNU_versym section.
  845. const int VERSYM_VERSION = 0x7fff;
  846. // Note descriptor type codes for notes in a non-core file with an
  847. // empty name.
  848. enum
  849. {
  850. // A version string.
  851. NT_VERSION = 1,
  852. // An architecture string.
  853. NT_ARCH = 2
  854. };
  855. // Note descriptor type codes for notes in a non-core file with the
  856. // name "GNU".
  857. enum
  858. {
  859. // The minimum ABI level. This is used by the dynamic linker to
  860. // describe the minimal kernel version on which a shared library may
  861. // be used. Th value should be four words. Word 0 is an OS
  862. // descriptor (see below). Word 1 is the major version of the ABI.
  863. // Word 2 is the minor version. Word 3 is the subminor version.
  864. NT_GNU_ABI_TAG = 1,
  865. // Hardware capabilities information. Word 0 is the number of
  866. // entries. Word 1 is a bitmask of enabled entries. The rest of
  867. // the descriptor is a series of entries, where each entry is a
  868. // single byte followed by a nul terminated string. The byte gives
  869. // the bit number to test if enabled in the bitmask.
  870. NT_GNU_HWCAP = 2,
  871. // The build ID as set by the linker's --build-id option. The
  872. // format of the descriptor depends on the build ID style.
  873. NT_GNU_BUILD_ID = 3,
  874. // The version of gold used to link. Th descriptor is just a
  875. // string.
  876. NT_GNU_GOLD_VERSION = 4,
  877. // Program property note, as described in "Linux Extensions to the gABI".
  878. NT_GNU_PROPERTY_TYPE_0 = 5
  879. };
  880. // The OS values which may appear in word 0 of a NT_GNU_ABI_TAG note.
  881. enum
  882. {
  883. ELF_NOTE_OS_LINUX = 0,
  884. ELF_NOTE_OS_GNU = 1,
  885. ELF_NOTE_OS_SOLARIS2 = 2,
  886. ELF_NOTE_OS_FREEBSD = 3,
  887. ELF_NOTE_OS_NETBSD = 4,
  888. ELF_NOTE_OS_SYLLABLE = 5
  889. };
  890. // Program property types for NT_GNU_PROPERTY_TYPE_0.
  891. enum
  892. {
  893. GNU_PROPERTY_STACK_SIZE = 1,
  894. GNU_PROPERTY_NO_COPY_ON_PROTECTED = 2,
  895. GNU_PROPERTY_LOPROC = 0xc0000000,
  896. GNU_PROPERTY_X86_COMPAT_ISA_1_USED = 0xc0000000,
  897. GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED = 0xc0000001,
  898. GNU_PROPERTY_X86_UINT32_AND_LO = 0xc0000002,
  899. GNU_PROPERTY_X86_UINT32_AND_HI = 0xc0007fff,
  900. GNU_PROPERTY_X86_UINT32_OR_LO = 0xc0008000,
  901. GNU_PROPERTY_X86_UINT32_OR_HI = 0xc000ffff,
  902. GNU_PROPERTY_X86_UINT32_OR_AND_LO = 0xc0010000,
  903. GNU_PROPERTY_X86_UINT32_OR_AND_HI = 0xc0017fff,
  904. GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED = GNU_PROPERTY_X86_UINT32_OR_LO + 0,
  905. GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED = GNU_PROPERTY_X86_UINT32_OR_AND_LO + 0,
  906. GNU_PROPERTY_X86_FEATURE_1_AND = GNU_PROPERTY_X86_UINT32_AND_LO + 0,
  907. GNU_PROPERTY_X86_ISA_1_NEEDED = GNU_PROPERTY_X86_UINT32_OR_LO + 2,
  908. GNU_PROPERTY_X86_FEATURE_2_NEEDED = GNU_PROPERTY_X86_UINT32_OR_LO + 1,
  909. GNU_PROPERTY_X86_ISA_1_USED = GNU_PROPERTY_X86_UINT32_OR_AND_LO + 2,
  910. GNU_PROPERTY_X86_FEATURE_2_USED = GNU_PROPERTY_X86_UINT32_OR_AND_LO + 1,
  911. GNU_PROPERTY_HIPROC = 0xdfffffff,
  912. GNU_PROPERTY_LOUSER = 0xe0000000,
  913. GNU_PROPERTY_HIUSER = 0xffffffff
  914. };
  915. } // End namespace elfcpp.
  916. // Include internal details after defining the types.
  917. #include "elfcpp_internal.h"
  918. namespace elfcpp
  919. {
  920. // The offset of the ELF file header in the ELF file.
  921. const int file_header_offset = 0;
  922. // ELF structure sizes.
  923. template<int size>
  924. struct Elf_sizes
  925. {
  926. // Size of ELF file header.
  927. static const int ehdr_size = sizeof(internal::Ehdr_data<size>);
  928. // Size of ELF segment header.
  929. static const int phdr_size = sizeof(internal::Phdr_data<size>);
  930. // Size of ELF section header.
  931. static const int shdr_size = sizeof(internal::Shdr_data<size>);
  932. // Size of ELF compression header.
  933. static const int chdr_size = sizeof(internal::Chdr_data<size>);
  934. // Size of ELF symbol table entry.
  935. static const int sym_size = sizeof(internal::Sym_data<size>);
  936. // Sizes of ELF reloc entries.
  937. static const int rel_size = sizeof(internal::Rel_data<size>);
  938. static const int rela_size = sizeof(internal::Rela_data<size>);
  939. // Size of ELF dynamic entry.
  940. static const int dyn_size = sizeof(internal::Dyn_data<size>);
  941. // Size of ELF version structures.
  942. static const int verdef_size = sizeof(internal::Verdef_data);
  943. static const int verdaux_size = sizeof(internal::Verdaux_data);
  944. static const int verneed_size = sizeof(internal::Verneed_data);
  945. static const int vernaux_size = sizeof(internal::Vernaux_data);
  946. };
  947. // Accessor class for the ELF file header.
  948. template<int size, bool big_endian>
  949. class Ehdr
  950. {
  951. public:
  952. Ehdr(const unsigned char* p)
  953. : p_(reinterpret_cast<const internal::Ehdr_data<size>*>(p))
  954. { }
  955. template<typename File>
  956. Ehdr(File* file, typename File::Location loc)
  957. : p_(reinterpret_cast<const internal::Ehdr_data<size>*>(
  958. file->view(loc.file_offset, loc.data_size).data()))
  959. { }
  960. const unsigned char*
  961. get_e_ident() const
  962. { return this->p_->e_ident; }
  963. unsigned char
  964. get_ei_osabi() const
  965. { return this->p_->e_ident[EI_OSABI]; }
  966. unsigned char
  967. get_ei_abiversion() const
  968. { return this->p_->e_ident[EI_ABIVERSION]; }
  969. Elf_Half
  970. get_e_type() const
  971. { return Convert<16, big_endian>::convert_host(this->p_->e_type); }
  972. Elf_Half
  973. get_e_machine() const
  974. { return Convert<16, big_endian>::convert_host(this->p_->e_machine); }
  975. Elf_Word
  976. get_e_version() const
  977. { return Convert<32, big_endian>::convert_host(this->p_->e_version); }
  978. typename Elf_types<size>::Elf_Addr
  979. get_e_entry() const
  980. { return Convert<size, big_endian>::convert_host(this->p_->e_entry); }
  981. typename Elf_types<size>::Elf_Off
  982. get_e_phoff() const
  983. { return Convert<size, big_endian>::convert_host(this->p_->e_phoff); }
  984. typename Elf_types<size>::Elf_Off
  985. get_e_shoff() const
  986. { return Convert<size, big_endian>::convert_host(this->p_->e_shoff); }
  987. Elf_Word
  988. get_e_flags() const
  989. { return Convert<32, big_endian>::convert_host(this->p_->e_flags); }
  990. Elf_Half
  991. get_e_ehsize() const
  992. { return Convert<16, big_endian>::convert_host(this->p_->e_ehsize); }
  993. Elf_Half
  994. get_e_phentsize() const
  995. { return Convert<16, big_endian>::convert_host(this->p_->e_phentsize); }
  996. Elf_Half
  997. get_e_phnum() const
  998. { return Convert<16, big_endian>::convert_host(this->p_->e_phnum); }
  999. Elf_Half
  1000. get_e_shentsize() const
  1001. { return Convert<16, big_endian>::convert_host(this->p_->e_shentsize); }
  1002. Elf_Half
  1003. get_e_shnum() const
  1004. { return Convert<16, big_endian>::convert_host(this->p_->e_shnum); }
  1005. Elf_Half
  1006. get_e_shstrndx() const
  1007. { return Convert<16, big_endian>::convert_host(this->p_->e_shstrndx); }
  1008. private:
  1009. const internal::Ehdr_data<size>* p_;
  1010. };
  1011. // Write class for the ELF file header.
  1012. template<int size, bool big_endian>
  1013. class Ehdr_write
  1014. {
  1015. public:
  1016. Ehdr_write(unsigned char* p)
  1017. : p_(reinterpret_cast<internal::Ehdr_data<size>*>(p))
  1018. { }
  1019. void
  1020. put_e_ident(const unsigned char v[EI_NIDENT]) const
  1021. { memcpy(this->p_->e_ident, v, EI_NIDENT); }
  1022. void
  1023. put_e_type(Elf_Half v)
  1024. { this->p_->e_type = Convert<16, big_endian>::convert_host(v); }
  1025. void
  1026. put_e_machine(Elf_Half v)
  1027. { this->p_->e_machine = Convert<16, big_endian>::convert_host(v); }
  1028. void
  1029. put_e_version(Elf_Word v)
  1030. { this->p_->e_version = Convert<32, big_endian>::convert_host(v); }
  1031. void
  1032. put_e_entry(typename Elf_types<size>::Elf_Addr v)
  1033. { this->p_->e_entry = Convert<size, big_endian>::convert_host(v); }
  1034. void
  1035. put_e_phoff(typename Elf_types<size>::Elf_Off v)
  1036. { this->p_->e_phoff = Convert<size, big_endian>::convert_host(v); }
  1037. void
  1038. put_e_shoff(typename Elf_types<size>::Elf_Off v)
  1039. { this->p_->e_shoff = Convert<size, big_endian>::convert_host(v); }
  1040. void
  1041. put_e_flags(Elf_Word v)
  1042. { this->p_->e_flags = Convert<32, big_endian>::convert_host(v); }
  1043. void
  1044. put_e_ehsize(Elf_Half v)
  1045. { this->p_->e_ehsize = Convert<16, big_endian>::convert_host(v); }
  1046. void
  1047. put_e_phentsize(Elf_Half v)
  1048. { this->p_->e_phentsize = Convert<16, big_endian>::convert_host(v); }
  1049. void
  1050. put_e_phnum(Elf_Half v)
  1051. { this->p_->e_phnum = Convert<16, big_endian>::convert_host(v); }
  1052. void
  1053. put_e_shentsize(Elf_Half v)
  1054. { this->p_->e_shentsize = Convert<16, big_endian>::convert_host(v); }
  1055. void
  1056. put_e_shnum(Elf_Half v)
  1057. { this->p_->e_shnum = Convert<16, big_endian>::convert_host(v); }
  1058. void
  1059. put_e_shstrndx(Elf_Half v)
  1060. { this->p_->e_shstrndx = Convert<16, big_endian>::convert_host(v); }
  1061. private:
  1062. internal::Ehdr_data<size>* p_;
  1063. };
  1064. // Accessor class for an ELF section header.
  1065. template<int size, bool big_endian>
  1066. class Shdr
  1067. {
  1068. public:
  1069. Shdr(const unsigned char* p)
  1070. : p_(reinterpret_cast<const internal::Shdr_data<size>*>(p))
  1071. { }
  1072. template<typename File>
  1073. Shdr(File* file, typename File::Location loc)
  1074. : p_(reinterpret_cast<const internal::Shdr_data<size>*>(
  1075. file->view(loc.file_offset, loc.data_size).data()))
  1076. { }
  1077. Elf_Word
  1078. get_sh_name() const
  1079. { return Convert<32, big_endian>::convert_host(this->p_->sh_name); }
  1080. Elf_Word
  1081. get_sh_type() const
  1082. { return Convert<32, big_endian>::convert_host(this->p_->sh_type); }
  1083. typename Elf_types<size>::Elf_WXword
  1084. get_sh_flags() const
  1085. { return Convert<size, big_endian>::convert_host(this->p_->sh_flags); }
  1086. typename Elf_types<size>::Elf_Addr
  1087. get_sh_addr() const
  1088. { return Convert<size, big_endian>::convert_host(this->p_->sh_addr); }
  1089. typename Elf_types<size>::Elf_Off
  1090. get_sh_offset() const
  1091. { return Convert<size, big_endian>::convert_host(this->p_->sh_offset); }
  1092. typename Elf_types<size>::Elf_WXword
  1093. get_sh_size() const
  1094. { return Convert<size, big_endian>::convert_host(this->p_->sh_size); }
  1095. Elf_Word
  1096. get_sh_link() const
  1097. { return Convert<32, big_endian>::convert_host(this->p_->sh_link); }
  1098. Elf_Word
  1099. get_sh_info() const
  1100. { return Convert<32, big_endian>::convert_host(this->p_->sh_info); }
  1101. typename Elf_types<size>::Elf_WXword
  1102. get_sh_addralign() const
  1103. { return
  1104. Convert<size, big_endian>::convert_host(this->p_->sh_addralign); }
  1105. typename Elf_types<size>::Elf_WXword
  1106. get_sh_entsize() const
  1107. { return Convert<size, big_endian>::convert_host(this->p_->sh_entsize); }
  1108. private:
  1109. const internal::Shdr_data<size>* p_;
  1110. };
  1111. // Write class for an ELF section header.
  1112. template<int size, bool big_endian>
  1113. class Shdr_write
  1114. {
  1115. public:
  1116. Shdr_write(unsigned char* p)
  1117. : p_(reinterpret_cast<internal::Shdr_data<size>*>(p))
  1118. { }
  1119. void
  1120. put_sh_name(Elf_Word v)
  1121. { this->p_->sh_name = Convert<32, big_endian>::convert_host(v); }
  1122. void
  1123. put_sh_type(Elf_Word v)
  1124. { this->p_->sh_type = Convert<32, big_endian>::convert_host(v); }
  1125. void
  1126. put_sh_flags(typename Elf_types<size>::Elf_WXword v)
  1127. { this->p_->sh_flags = Convert<size, big_endian>::convert_host(v); }
  1128. void
  1129. put_sh_addr(typename Elf_types<size>::Elf_Addr v)
  1130. { this->p_->sh_addr = Convert<size, big_endian>::convert_host(v); }
  1131. void
  1132. put_sh_offset(typename Elf_types<size>::Elf_Off v)
  1133. { this->p_->sh_offset = Convert<size, big_endian>::convert_host(v); }
  1134. void
  1135. put_sh_size(typename Elf_types<size>::Elf_WXword v)
  1136. { this->p_->sh_size = Convert<size, big_endian>::convert_host(v); }
  1137. void
  1138. put_sh_link(Elf_Word v)
  1139. { this->p_->sh_link = Convert<32, big_endian>::convert_host(v); }
  1140. void
  1141. put_sh_info(Elf_Word v)
  1142. { this->p_->sh_info = Convert<32, big_endian>::convert_host(v); }
  1143. void
  1144. put_sh_addralign(typename Elf_types<size>::Elf_WXword v)
  1145. { this->p_->sh_addralign = Convert<size, big_endian>::convert_host(v); }
  1146. void
  1147. put_sh_entsize(typename Elf_types<size>::Elf_WXword v)
  1148. { this->p_->sh_entsize = Convert<size, big_endian>::convert_host(v); }
  1149. private:
  1150. internal::Shdr_data<size>* p_;
  1151. };
  1152. // Accessor class for an ELF compression header.
  1153. template<int size, bool big_endian>
  1154. class Chdr
  1155. {
  1156. public:
  1157. Chdr(const unsigned char* p)
  1158. : p_(reinterpret_cast<const internal::Chdr_data<size>*>(p))
  1159. { }
  1160. template<typename File>
  1161. Chdr(File* file, typename File::Location loc)
  1162. : p_(reinterpret_cast<const internal::Chdr_data<size>*>(
  1163. file->view(loc.file_offset, loc.data_size).data()))
  1164. { }
  1165. Elf_Word
  1166. get_ch_type() const
  1167. { return Convert<size, big_endian>::convert_host(this->p_->ch_type); }
  1168. typename Elf_types<size>::Elf_WXword
  1169. get_ch_size() const
  1170. { return Convert<size, big_endian>::convert_host(this->p_->ch_size); }
  1171. typename Elf_types<size>::Elf_WXword
  1172. get_ch_addralign() const
  1173. { return
  1174. Convert<size, big_endian>::convert_host(this->p_->ch_addralign); }
  1175. private:
  1176. const internal::Chdr_data<size>* p_;
  1177. };
  1178. // Write class for an ELF compression header.
  1179. template<int size, bool big_endian>
  1180. class Chdr_write
  1181. {
  1182. public:
  1183. Chdr_write(unsigned char* p)
  1184. : p_(reinterpret_cast<internal::Chdr_data<size>*>(p))
  1185. { }
  1186. void
  1187. put_ch_type(typename Elf_types<size>::Elf_WXword v)
  1188. { this->p_->ch_type = Convert<size, big_endian>::convert_host(v); }
  1189. void
  1190. put_ch_size(typename Elf_types<size>::Elf_WXword v)
  1191. { this->p_->ch_size = Convert<size, big_endian>::convert_host(v); }
  1192. void
  1193. put_ch_addralign(typename Elf_types<size>::Elf_WXword v)
  1194. { this->p_->ch_addralign = Convert<size, big_endian>::convert_host(v); }
  1195. void
  1196. put_ch_reserved(Elf_Word);
  1197. private:
  1198. internal::Chdr_data<size>* p_;
  1199. };
  1200. template<>
  1201. inline void
  1202. elfcpp::Chdr_write<64, true>::put_ch_reserved(Elf_Word v)
  1203. {
  1204. this->p_->ch_reserved = v;
  1205. }
  1206. template<>
  1207. inline void
  1208. elfcpp::Chdr_write<64, false>::put_ch_reserved(Elf_Word v)
  1209. {
  1210. this->p_->ch_reserved = v;
  1211. }
  1212. // Accessor class for an ELF segment header.
  1213. template<int size, bool big_endian>
  1214. class Phdr
  1215. {
  1216. public:
  1217. Phdr(const unsigned char* p)
  1218. : p_(reinterpret_cast<const internal::Phdr_data<size>*>(p))
  1219. { }
  1220. template<typename File>
  1221. Phdr(File* file, typename File::Location loc)
  1222. : p_(reinterpret_cast<internal::Phdr_data<size>*>(
  1223. file->view(loc.file_offset, loc.data_size).data()))
  1224. { }
  1225. Elf_Word
  1226. get_p_type() const
  1227. { return Convert<32, big_endian>::convert_host(this->p_->p_type); }
  1228. typename Elf_types<size>::Elf_Off
  1229. get_p_offset() const
  1230. { return Convert<size, big_endian>::convert_host(this->p_->p_offset); }
  1231. typename Elf_types<size>::Elf_Addr
  1232. get_p_vaddr() const
  1233. { return Convert<size, big_endian>::convert_host(this->p_->p_vaddr); }
  1234. typename Elf_types<size>::Elf_Addr
  1235. get_p_paddr() const
  1236. { return Convert<size, big_endian>::convert_host(this->p_->p_paddr); }
  1237. typename Elf_types<size>::Elf_WXword
  1238. get_p_filesz() const
  1239. { return Convert<size, big_endian>::convert_host(this->p_->p_filesz); }
  1240. typename Elf_types<size>::Elf_WXword
  1241. get_p_memsz() const
  1242. { return Convert<size, big_endian>::convert_host(this->p_->p_memsz); }
  1243. Elf_Word
  1244. get_p_flags() const
  1245. { return Convert<32, big_endian>::convert_host(this->p_->p_flags); }
  1246. typename Elf_types<size>::Elf_WXword
  1247. get_p_align() const
  1248. { return Convert<size, big_endian>::convert_host(this->p_->p_align); }
  1249. private:
  1250. const internal::Phdr_data<size>* p_;
  1251. };
  1252. // Write class for an ELF segment header.
  1253. template<int size, bool big_endian>
  1254. class Phdr_write
  1255. {
  1256. public:
  1257. Phdr_write(unsigned char* p)
  1258. : p_(reinterpret_cast<internal::Phdr_data<size>*>(p))
  1259. { }
  1260. void
  1261. put_p_type(Elf_Word v)
  1262. { this->p_->p_type = Convert<32, big_endian>::convert_host(v); }
  1263. void
  1264. put_p_offset(typename Elf_types<size>::Elf_Off v)
  1265. { this->p_->p_offset = Convert<size, big_endian>::convert_host(v); }
  1266. void
  1267. put_p_vaddr(typename Elf_types<size>::Elf_Addr v)
  1268. { this->p_->p_vaddr = Convert<size, big_endian>::convert_host(v); }
  1269. void
  1270. put_p_paddr(typename Elf_types<size>::Elf_Addr v)
  1271. { this->p_->p_paddr = Convert<size, big_endian>::convert_host(v); }
  1272. void
  1273. put_p_filesz(typename Elf_types<size>::Elf_WXword v)
  1274. { this->p_->p_filesz = Convert<size, big_endian>::convert_host(v); }
  1275. void
  1276. put_p_memsz(typename Elf_types<size>::Elf_WXword v)
  1277. { this->p_->p_memsz = Convert<size, big_endian>::convert_host(v); }
  1278. void
  1279. put_p_flags(Elf_Word v)
  1280. { this->p_->p_flags = Convert<32, big_endian>::convert_host(v); }
  1281. void
  1282. put_p_align(typename Elf_types<size>::Elf_WXword v)
  1283. { this->p_->p_align = Convert<size, big_endian>::convert_host(v); }
  1284. private:
  1285. internal::Phdr_data<size>* p_;
  1286. };
  1287. // Accessor class for an ELF symbol table entry.
  1288. template<int size, bool big_endian>
  1289. class Sym
  1290. {
  1291. public:
  1292. Sym(const unsigned char* p)
  1293. : p_(reinterpret_cast<const internal::Sym_data<size>*>(p))
  1294. { }
  1295. template<typename File>
  1296. Sym(File* file, typename File::Location loc)
  1297. : p_(reinterpret_cast<const internal::Sym_data<size>*>(
  1298. file->view(loc.file_offset, loc.data_size).data()))
  1299. { }
  1300. Elf_Word
  1301. get_st_name() const
  1302. { return Convert<32, big_endian>::convert_host(this->p_->st_name); }
  1303. typename Elf_types<size>::Elf_Addr
  1304. get_st_value() const
  1305. { return Convert<size, big_endian>::convert_host(this->p_->st_value); }
  1306. typename Elf_types<size>::Elf_WXword
  1307. get_st_size() const
  1308. { return Convert<size, big_endian>::convert_host(this->p_->st_size); }
  1309. unsigned char
  1310. get_st_info() const
  1311. { return this->p_->st_info; }
  1312. STB
  1313. get_st_bind() const
  1314. { return elf_st_bind(this->get_st_info()); }
  1315. STT
  1316. get_st_type() const
  1317. { return elf_st_type(this->get_st_info()); }
  1318. unsigned char
  1319. get_st_other() const
  1320. { return this->p_->st_other; }
  1321. STV
  1322. get_st_visibility() const
  1323. { return elf_st_visibility(this->get_st_other()); }
  1324. unsigned char
  1325. get_st_nonvis() const
  1326. { return elf_st_nonvis(this->get_st_other()); }
  1327. Elf_Half
  1328. get_st_shndx() const
  1329. { return Convert<16, big_endian>::convert_host(this->p_->st_shndx); }
  1330. private:
  1331. const internal::Sym_data<size>* p_;
  1332. };
  1333. // Writer class for an ELF symbol table entry.
  1334. template<int size, bool big_endian>
  1335. class Sym_write
  1336. {
  1337. public:
  1338. Sym_write(unsigned char* p)
  1339. : p_(reinterpret_cast<internal::Sym_data<size>*>(p))
  1340. { }
  1341. void
  1342. put_st_name(Elf_Word v)
  1343. { this->p_->st_name = Convert<32, big_endian>::convert_host(v); }
  1344. void
  1345. put_st_value(typename Elf_types<size>::Elf_Addr v)
  1346. { this->p_->st_value = Convert<size, big_endian>::convert_host(v); }
  1347. void
  1348. put_st_size(typename Elf_types<size>::Elf_WXword v)
  1349. { this->p_->st_size = Convert<size, big_endian>::convert_host(v); }
  1350. void
  1351. put_st_info(unsigned char v)
  1352. { this->p_->st_info = v; }
  1353. void
  1354. put_st_info(STB bind, STT type)
  1355. { this->p_->st_info = elf_st_info(bind, type); }
  1356. void
  1357. put_st_other(unsigned char v)
  1358. { this->p_->st_other = v; }
  1359. void
  1360. put_st_other(STV vis, unsigned char nonvis)
  1361. { this->p_->st_other = elf_st_other(vis, nonvis); }
  1362. void
  1363. put_st_shndx(Elf_Half v)
  1364. { this->p_->st_shndx = Convert<16, big_endian>::convert_host(v); }
  1365. Sym<size, big_endian>
  1366. sym()
  1367. { return Sym<size, big_endian>(reinterpret_cast<unsigned char*>(this->p_)); }
  1368. private:
  1369. internal::Sym_data<size>* p_;
  1370. };
  1371. // Accessor classes for an ELF REL relocation entry.
  1372. template<int size, bool big_endian>
  1373. class Rel
  1374. {
  1375. public:
  1376. Rel(const unsigned char* p)
  1377. : p_(reinterpret_cast<const internal::Rel_data<size>*>(p))
  1378. { }
  1379. template<typename File>
  1380. Rel(File* file, typename File::Location loc)
  1381. : p_(reinterpret_cast<const internal::Rel_data<size>*>(
  1382. file->view(loc.file_offset, loc.data_size).data()))
  1383. { }
  1384. typename Elf_types<size>::Elf_Addr
  1385. get_r_offset() const
  1386. { return Convert<size, big_endian>::convert_host(this->p_->r_offset); }
  1387. typename Elf_types<size>::Elf_WXword
  1388. get_r_info() const
  1389. { return Convert<size, big_endian>::convert_host(this->p_->r_info); }
  1390. private:
  1391. const internal::Rel_data<size>* p_;
  1392. };
  1393. // Writer class for an ELF Rel relocation.
  1394. template<int size, bool big_endian>
  1395. class Rel_write
  1396. {
  1397. public:
  1398. Rel_write(unsigned char* p)
  1399. : p_(reinterpret_cast<internal::Rel_data<size>*>(p))
  1400. { }
  1401. void
  1402. put_r_offset(typename Elf_types<size>::Elf_Addr v)
  1403. { this->p_->r_offset = Convert<size, big_endian>::convert_host(v); }
  1404. void
  1405. put_r_info(typename Elf_types<size>::Elf_WXword v)
  1406. { this->p_->r_info = Convert<size, big_endian>::convert_host(v); }
  1407. private:
  1408. internal::Rel_data<size>* p_;
  1409. };
  1410. // Accessor class for an ELF Rela relocation.
  1411. template<int size, bool big_endian>
  1412. class Rela
  1413. {
  1414. public:
  1415. Rela(const unsigned char* p)
  1416. : p_(reinterpret_cast<const internal::Rela_data<size>*>(p))
  1417. { }
  1418. template<typename File>
  1419. Rela(File* file, typename File::Location loc)
  1420. : p_(reinterpret_cast<const internal::Rela_data<size>*>(
  1421. file->view(loc.file_offset, loc.data_size).data()))
  1422. { }
  1423. typename Elf_types<size>::Elf_Addr
  1424. get_r_offset() const
  1425. { return Convert<size, big_endian>::convert_host(this->p_->r_offset); }
  1426. typename Elf_types<size>::Elf_WXword
  1427. get_r_info() const
  1428. { return Convert<size, big_endian>::convert_host(this->p_->r_info); }
  1429. typename Elf_types<size>::Elf_Swxword
  1430. get_r_addend() const
  1431. { return Convert<size, big_endian>::convert_host(this->p_->r_addend); }
  1432. private:
  1433. const internal::Rela_data<size>* p_;
  1434. };
  1435. // Writer class for an ELF Rela relocation.
  1436. template<int size, bool big_endian>
  1437. class Rela_write
  1438. {
  1439. public:
  1440. Rela_write(unsigned char* p)
  1441. : p_(reinterpret_cast<internal::Rela_data<size>*>(p))
  1442. { }
  1443. void
  1444. put_r_offset(typename Elf_types<size>::Elf_Addr v)
  1445. { this->p_->r_offset = Convert<size, big_endian>::convert_host(v); }
  1446. void
  1447. put_r_info(typename Elf_types<size>::Elf_WXword v)
  1448. { this->p_->r_info = Convert<size, big_endian>::convert_host(v); }
  1449. void
  1450. put_r_addend(typename Elf_types<size>::Elf_Swxword v)
  1451. { this->p_->r_addend = Convert<size, big_endian>::convert_host(v); }
  1452. private:
  1453. internal::Rela_data<size>* p_;
  1454. };
  1455. // MIPS-64 has a non-standard relocation layout.
  1456. template<bool big_endian>
  1457. class Mips64_rel
  1458. {
  1459. public:
  1460. Mips64_rel(const unsigned char* p)
  1461. : p_(reinterpret_cast<const internal::Mips64_rel_data*>(p))
  1462. { }
  1463. template<typename File>
  1464. Mips64_rel(File* file, typename File::Location loc)
  1465. : p_(reinterpret_cast<const internal::Mips64_rel_data*>(
  1466. file->view(loc.file_offset, loc.data_size).data()))
  1467. { }
  1468. typename Elf_types<64>::Elf_Addr
  1469. get_r_offset() const
  1470. { return Convert<64, big_endian>::convert_host(this->p_->r_offset); }
  1471. Elf_Word
  1472. get_r_sym() const
  1473. { return Convert<32, big_endian>::convert_host(this->p_->r_sym); }
  1474. unsigned char
  1475. get_r_ssym() const
  1476. { return this->p_->r_ssym; }
  1477. unsigned char
  1478. get_r_type() const
  1479. { return this->p_->r_type; }
  1480. unsigned char
  1481. get_r_type2() const
  1482. { return this->p_->r_type2; }
  1483. unsigned char
  1484. get_r_type3() const
  1485. { return this->p_->r_type3; }
  1486. private:
  1487. const internal::Mips64_rel_data* p_;
  1488. };
  1489. template<bool big_endian>
  1490. class Mips64_rel_write
  1491. {
  1492. public:
  1493. Mips64_rel_write(unsigned char* p)
  1494. : p_(reinterpret_cast<internal::Mips64_rel_data*>(p))
  1495. { }
  1496. void
  1497. put_r_offset(typename Elf_types<64>::Elf_Addr v)
  1498. { this->p_->r_offset = Convert<64, big_endian>::convert_host(v); }
  1499. void
  1500. put_r_sym(Elf_Word v)
  1501. { this->p_->r_sym = Convert<32, big_endian>::convert_host(v); }
  1502. void
  1503. put_r_ssym(unsigned char v)
  1504. { this->p_->r_ssym = v; }
  1505. void
  1506. put_r_type(unsigned char v)
  1507. { this->p_->r_type = v; }
  1508. void
  1509. put_r_type2(unsigned char v)
  1510. { this->p_->r_type2 = v; }
  1511. void
  1512. put_r_type3(unsigned char v)
  1513. { this->p_->r_type3 = v; }
  1514. private:
  1515. internal::Mips64_rel_data* p_;
  1516. };
  1517. template<bool big_endian>
  1518. class Mips64_rela
  1519. {
  1520. public:
  1521. Mips64_rela(const unsigned char* p)
  1522. : p_(reinterpret_cast<const internal::Mips64_rela_data*>(p))
  1523. { }
  1524. template<typename File>
  1525. Mips64_rela(File* file, typename File::Location loc)
  1526. : p_(reinterpret_cast<const internal::Mips64_rela_data*>(
  1527. file->view(loc.file_offset, loc.data_size).data()))
  1528. { }
  1529. typename Elf_types<64>::Elf_Addr
  1530. get_r_offset() const
  1531. { return Convert<64, big_endian>::convert_host(this->p_->r_offset); }
  1532. Elf_Word
  1533. get_r_sym() const
  1534. { return Convert<32, big_endian>::convert_host(this->p_->r_sym); }
  1535. unsigned char
  1536. get_r_ssym() const
  1537. { return this->p_->r_ssym; }
  1538. unsigned char
  1539. get_r_type() const
  1540. { return this->p_->r_type; }
  1541. unsigned char
  1542. get_r_type2() const
  1543. { return this->p_->r_type2; }
  1544. unsigned char
  1545. get_r_type3() const
  1546. { return this->p_->r_type3; }
  1547. typename Elf_types<64>::Elf_Swxword
  1548. get_r_addend() const
  1549. { return Convert<64, big_endian>::convert_host(this->p_->r_addend); }
  1550. private:
  1551. const internal::Mips64_rela_data* p_;
  1552. };
  1553. template<bool big_endian>
  1554. class Mips64_rela_write
  1555. {
  1556. public:
  1557. Mips64_rela_write(unsigned char* p)
  1558. : p_(reinterpret_cast<internal::Mips64_rela_data*>(p))
  1559. { }
  1560. void
  1561. put_r_offset(typename Elf_types<64>::Elf_Addr v)
  1562. { this->p_->r_offset = Convert<64, big_endian>::convert_host(v); }
  1563. void
  1564. put_r_sym(Elf_Word v)
  1565. { this->p_->r_sym = Convert<32, big_endian>::convert_host(v); }
  1566. void
  1567. put_r_ssym(unsigned char v)
  1568. { this->p_->r_ssym = v; }
  1569. void
  1570. put_r_type(unsigned char v)
  1571. { this->p_->r_type = v; }
  1572. void
  1573. put_r_type2(unsigned char v)
  1574. { this->p_->r_type2 = v; }
  1575. void
  1576. put_r_type3(unsigned char v)
  1577. { this->p_->r_type3 = v; }
  1578. void
  1579. put_r_addend(typename Elf_types<64>::Elf_Swxword v)
  1580. { this->p_->r_addend = Convert<64, big_endian>::convert_host(v); }
  1581. private:
  1582. internal::Mips64_rela_data* p_;
  1583. };
  1584. // Accessor classes for entries in the ELF SHT_DYNAMIC section aka
  1585. // PT_DYNAMIC segment.
  1586. template<int size, bool big_endian>
  1587. class Dyn
  1588. {
  1589. public:
  1590. Dyn(const unsigned char* p)
  1591. : p_(reinterpret_cast<const internal::Dyn_data<size>*>(p))
  1592. { }
  1593. template<typename File>
  1594. Dyn(File* file, typename File::Location loc)
  1595. : p_(reinterpret_cast<const internal::Dyn_data<size>*>(
  1596. file->view(loc.file_offset, loc.data_size).data()))
  1597. { }
  1598. typename Elf_types<size>::Elf_Swxword
  1599. get_d_tag() const
  1600. { return Convert<size, big_endian>::convert_host(this->p_->d_tag); }
  1601. typename Elf_types<size>::Elf_WXword
  1602. get_d_val() const
  1603. { return Convert<size, big_endian>::convert_host(this->p_->d_val); }
  1604. typename Elf_types<size>::Elf_Addr
  1605. get_d_ptr() const
  1606. { return Convert<size, big_endian>::convert_host(this->p_->d_val); }
  1607. private:
  1608. const internal::Dyn_data<size>* p_;
  1609. };
  1610. // Write class for an entry in the SHT_DYNAMIC section.
  1611. template<int size, bool big_endian>
  1612. class Dyn_write
  1613. {
  1614. public:
  1615. Dyn_write(unsigned char* p)
  1616. : p_(reinterpret_cast<internal::Dyn_data<size>*>(p))
  1617. { }
  1618. void
  1619. put_d_tag(typename Elf_types<size>::Elf_Swxword v)
  1620. { this->p_->d_tag = Convert<size, big_endian>::convert_host(v); }
  1621. void
  1622. put_d_val(typename Elf_types<size>::Elf_WXword v)
  1623. { this->p_->d_val = Convert<size, big_endian>::convert_host(v); }
  1624. void
  1625. put_d_ptr(typename Elf_types<size>::Elf_Addr v)
  1626. { this->p_->d_val = Convert<size, big_endian>::convert_host(v); }
  1627. private:
  1628. internal::Dyn_data<size>* p_;
  1629. };
  1630. // Accessor classes for entries in the ELF SHT_GNU_verdef section.
  1631. template<int size, bool big_endian>
  1632. class Verdef
  1633. {
  1634. public:
  1635. Verdef(const unsigned char* p)
  1636. : p_(reinterpret_cast<const internal::Verdef_data*>(p))
  1637. { }
  1638. template<typename File>
  1639. Verdef(File* file, typename File::Location loc)
  1640. : p_(reinterpret_cast<const internal::Verdef_data*>(
  1641. file->view(loc.file_offset, loc.data_size).data()))
  1642. { }
  1643. Elf_Half
  1644. get_vd_version() const
  1645. { return Convert<16, big_endian>::convert_host(this->p_->vd_version); }
  1646. Elf_Half
  1647. get_vd_flags() const
  1648. { return Convert<16, big_endian>::convert_host(this->p_->vd_flags); }
  1649. Elf_Half
  1650. get_vd_ndx() const
  1651. { return Convert<16, big_endian>::convert_host(this->p_->vd_ndx); }
  1652. Elf_Half
  1653. get_vd_cnt() const
  1654. { return Convert<16, big_endian>::convert_host(this->p_->vd_cnt); }
  1655. Elf_Word
  1656. get_vd_hash() const
  1657. { return Convert<32, big_endian>::convert_host(this->p_->vd_hash); }
  1658. Elf_Word
  1659. get_vd_aux() const
  1660. { return Convert<32, big_endian>::convert_host(this->p_->vd_aux); }
  1661. Elf_Word
  1662. get_vd_next() const
  1663. { return Convert<32, big_endian>::convert_host(this->p_->vd_next); }
  1664. private:
  1665. const internal::Verdef_data* p_;
  1666. };
  1667. template<int size, bool big_endian>
  1668. class Verdef_write
  1669. {
  1670. public:
  1671. Verdef_write(unsigned char* p)
  1672. : p_(reinterpret_cast<internal::Verdef_data*>(p))
  1673. { }
  1674. void
  1675. set_vd_version(Elf_Half v)
  1676. { this->p_->vd_version = Convert<16, big_endian>::convert_host(v); }
  1677. void
  1678. set_vd_flags(Elf_Half v)
  1679. { this->p_->vd_flags = Convert<16, big_endian>::convert_host(v); }
  1680. void
  1681. set_vd_ndx(Elf_Half v)
  1682. { this->p_->vd_ndx = Convert<16, big_endian>::convert_host(v); }
  1683. void
  1684. set_vd_cnt(Elf_Half v)
  1685. { this->p_->vd_cnt = Convert<16, big_endian>::convert_host(v); }
  1686. void
  1687. set_vd_hash(Elf_Word v)
  1688. { this->p_->vd_hash = Convert<32, big_endian>::convert_host(v); }
  1689. void
  1690. set_vd_aux(Elf_Word v)
  1691. { this->p_->vd_aux = Convert<32, big_endian>::convert_host(v); }
  1692. void
  1693. set_vd_next(Elf_Word v)
  1694. { this->p_->vd_next = Convert<32, big_endian>::convert_host(v); }
  1695. private:
  1696. internal::Verdef_data* p_;
  1697. };
  1698. // Accessor classes for auxiliary entries in the ELF SHT_GNU_verdef
  1699. // section.
  1700. template<int size, bool big_endian>
  1701. class Verdaux
  1702. {
  1703. public:
  1704. Verdaux(const unsigned char* p)
  1705. : p_(reinterpret_cast<const internal::Verdaux_data*>(p))
  1706. { }
  1707. template<typename File>
  1708. Verdaux(File* file, typename File::Location loc)
  1709. : p_(reinterpret_cast<const internal::Verdaux_data*>(
  1710. file->view(loc.file_offset, loc.data_size).data()))
  1711. { }
  1712. Elf_Word
  1713. get_vda_name() const
  1714. { return Convert<32, big_endian>::convert_host(this->p_->vda_name); }
  1715. Elf_Word
  1716. get_vda_next() const
  1717. { return Convert<32, big_endian>::convert_host(this->p_->vda_next); }
  1718. private:
  1719. const internal::Verdaux_data* p_;
  1720. };
  1721. template<int size, bool big_endian>
  1722. class Verdaux_write
  1723. {
  1724. public:
  1725. Verdaux_write(unsigned char* p)
  1726. : p_(reinterpret_cast<internal::Verdaux_data*>(p))
  1727. { }
  1728. void
  1729. set_vda_name(Elf_Word v)
  1730. { this->p_->vda_name = Convert<32, big_endian>::convert_host(v); }
  1731. void
  1732. set_vda_next(Elf_Word v)
  1733. { this->p_->vda_next = Convert<32, big_endian>::convert_host(v); }
  1734. private:
  1735. internal::Verdaux_data* p_;
  1736. };
  1737. // Accessor classes for entries in the ELF SHT_GNU_verneed section.
  1738. template<int size, bool big_endian>
  1739. class Verneed
  1740. {
  1741. public:
  1742. Verneed(const unsigned char* p)
  1743. : p_(reinterpret_cast<const internal::Verneed_data*>(p))
  1744. { }
  1745. template<typename File>
  1746. Verneed(File* file, typename File::Location loc)
  1747. : p_(reinterpret_cast<const internal::Verneed_data*>(
  1748. file->view(loc.file_offset, loc.data_size).data()))
  1749. { }
  1750. Elf_Half
  1751. get_vn_version() const
  1752. { return Convert<16, big_endian>::convert_host(this->p_->vn_version); }
  1753. Elf_Half
  1754. get_vn_cnt() const
  1755. { return Convert<16, big_endian>::convert_host(this->p_->vn_cnt); }
  1756. Elf_Word
  1757. get_vn_file() const
  1758. { return Convert<32, big_endian>::convert_host(this->p_->vn_file); }
  1759. Elf_Word
  1760. get_vn_aux() const
  1761. { return Convert<32, big_endian>::convert_host(this->p_->vn_aux); }
  1762. Elf_Word
  1763. get_vn_next() const
  1764. { return Convert<32, big_endian>::convert_host(this->p_->vn_next); }
  1765. private:
  1766. const internal::Verneed_data* p_;
  1767. };
  1768. template<int size, bool big_endian>
  1769. class Verneed_write
  1770. {
  1771. public:
  1772. Verneed_write(unsigned char* p)
  1773. : p_(reinterpret_cast<internal::Verneed_data*>(p))
  1774. { }
  1775. void
  1776. set_vn_version(Elf_Half v)
  1777. { this->p_->vn_version = Convert<16, big_endian>::convert_host(v); }
  1778. void
  1779. set_vn_cnt(Elf_Half v)
  1780. { this->p_->vn_cnt = Convert<16, big_endian>::convert_host(v); }
  1781. void
  1782. set_vn_file(Elf_Word v)
  1783. { this->p_->vn_file = Convert<32, big_endian>::convert_host(v); }
  1784. void
  1785. set_vn_aux(Elf_Word v)
  1786. { this->p_->vn_aux = Convert<32, big_endian>::convert_host(v); }
  1787. void
  1788. set_vn_next(Elf_Word v)
  1789. { this->p_->vn_next = Convert<32, big_endian>::convert_host(v); }
  1790. private:
  1791. internal::Verneed_data* p_;
  1792. };
  1793. // Accessor classes for auxiliary entries in the ELF SHT_GNU_verneed
  1794. // section.
  1795. template<int size, bool big_endian>
  1796. class Vernaux
  1797. {
  1798. public:
  1799. Vernaux(const unsigned char* p)
  1800. : p_(reinterpret_cast<const internal::Vernaux_data*>(p))
  1801. { }
  1802. template<typename File>
  1803. Vernaux(File* file, typename File::Location loc)
  1804. : p_(reinterpret_cast<const internal::Vernaux_data*>(
  1805. file->view(loc.file_offset, loc.data_size).data()))
  1806. { }
  1807. Elf_Word
  1808. get_vna_hash() const
  1809. { return Convert<32, big_endian>::convert_host(this->p_->vna_hash); }
  1810. Elf_Half
  1811. get_vna_flags() const
  1812. { return Convert<16, big_endian>::convert_host(this->p_->vna_flags); }
  1813. Elf_Half
  1814. get_vna_other() const
  1815. { return Convert<16, big_endian>::convert_host(this->p_->vna_other); }
  1816. Elf_Word
  1817. get_vna_name() const
  1818. { return Convert<32, big_endian>::convert_host(this->p_->vna_name); }
  1819. Elf_Word
  1820. get_vna_next() const
  1821. { return Convert<32, big_endian>::convert_host(this->p_->vna_next); }
  1822. private:
  1823. const internal::Vernaux_data* p_;
  1824. };
  1825. template<int size, bool big_endian>
  1826. class Vernaux_write
  1827. {
  1828. public:
  1829. Vernaux_write(unsigned char* p)
  1830. : p_(reinterpret_cast<internal::Vernaux_data*>(p))
  1831. { }
  1832. void
  1833. set_vna_hash(Elf_Word v)
  1834. { this->p_->vna_hash = Convert<32, big_endian>::convert_host(v); }
  1835. void
  1836. set_vna_flags(Elf_Half v)
  1837. { this->p_->vna_flags = Convert<16, big_endian>::convert_host(v); }
  1838. void
  1839. set_vna_other(Elf_Half v)
  1840. { this->p_->vna_other = Convert<16, big_endian>::convert_host(v); }
  1841. void
  1842. set_vna_name(Elf_Word v)
  1843. { this->p_->vna_name = Convert<32, big_endian>::convert_host(v); }
  1844. void
  1845. set_vna_next(Elf_Word v)
  1846. { this->p_->vna_next = Convert<32, big_endian>::convert_host(v); }
  1847. private:
  1848. internal::Vernaux_data* p_;
  1849. };
  1850. } // End namespace elfcpp.
  1851. #endif // !defined(ELFPCP_H)