elf.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  1. /*++
  2. Copyright (c) 2012 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. elf.h
  9. Abstract:
  10. This header contains definitions for the ELF file format.
  11. Author:
  12. Evan Green 13-Oct-2012
  13. --*/
  14. //
  15. // ------------------------------------------------------------------- Includes
  16. //
  17. //
  18. // --------------------------------------------------------------------- Macros
  19. //
  20. //
  21. // These macros manipulate the bind and type information from a symbol's
  22. // information field.
  23. //
  24. #define ELF_GET_SYMBOL_BIND(_Information) ((_Information) >> 4)
  25. #define ELF_GET_SYMBOL_TYPE(_Information) ((_Information) & 0xF)
  26. #define ELF_SYMBOL_INFORMATION(_Bind, _Type) \
  27. (((_Bind) << 4) + ((_Type) & 0x0F))
  28. //
  29. // These macros manipulate the symbol and type fields in a relocation entry's
  30. // information in 32-bit ELF images.
  31. //
  32. #define ELF32_GET_RELOCATION_SYMBOL(_Information) ((_Information) >> 8)
  33. #define ELF32_GET_RELOCATION_TYPE(_Information) ((_Information) & 0xFF)
  34. #define ELF32_RELOCATION_INFORMATION(_Symbol, _Type) \
  35. (((_Symbol) << 8) + ((_Type) & 0xFF))
  36. //
  37. // These macros manipulate the symbol and type fields in a relocation entry's
  38. // information in 64-bit ELF images.
  39. //
  40. #define ELF64_GET_RELOCATION_SYMBOL(_Information) ((_Information) >> 32)
  41. #define ELF64_GET_RELOCATION_TYPE(_Information) ((_Information) & 0xFFFFFFFF)
  42. #define ELF64_RELOCATION_INFORMATION(_Symbol, _Type) \
  43. (((_Symbol) << 32) + ((_Type) & 0xFFFFFFFF))
  44. //
  45. // ---------------------------------------------------------------- Definitions
  46. //
  47. #define ELF32_IDENTIFICATION_LENGTH 16
  48. #define ELF64_IDENTIFICATION_LENGTH 16
  49. #define ELF_MAGIC0 0x7F
  50. #define ELF_MAGIC1 'E'
  51. #define ELF_MAGIC2 'L'
  52. #define ELF_MAGIC3 'F'
  53. #define ELF_MAGIC 0x464C457F
  54. #define ELF_CLASS_OFFSET 4
  55. #define ELF_ENDIANNESS_OFFSET 5
  56. #define ELF_VERSION_OFFSET 6
  57. #define ELF_OS_ABI_OFFSET 7
  58. #define ELF_ABI_VERSION_OFFSET 8
  59. #define ELF_IMAGE_RELOCATABLE 1
  60. #define ELF_IMAGE_EXECUTABLE 2
  61. #define ELF_IMAGE_SHARED_OBJECT 3
  62. #define ELF_IMAGE_CORE 4
  63. #define ELF_MACHINE_I386 3
  64. #define ELF_MACHINE_ARM 40
  65. #define ELF_MACHINE_X86_64 62
  66. #define ELF_MACHINE_AARCH64 183
  67. #define ELF_LITTLE_ENDIAN 0x1
  68. #define ELF_BIG_ENDIAN 0x2
  69. #define ELF_32BIT 0x1
  70. #define ELF_64BIT 0x2
  71. #define ELF_SECTION_TYPE_NULL 0
  72. #define ELF_SECTION_TYPE_PROGRAM 1
  73. #define ELF_SECTION_TYPE_SYMBOLS 2
  74. #define ELF_SECTION_TYPE_STRINGS 3
  75. #define ELF_SECTION_TYPE_RELOCATIONS_ADDENDS 4
  76. #define ELF_SECTION_TYPE_HASH_TABLE 5
  77. #define ELF_SECTION_TYPE_DYNAMIC_LINK 6
  78. #define ELF_SECTION_TYPE_NOTE 7
  79. #define ELF_SECTION_TYPE_NO_BITS 8
  80. #define ELF_SECTION_TYPE_RELOCATIONS_NO_ADDENDS 9
  81. #define ELF_SECTION_TYPE_SHLIB 10
  82. #define ELF_SECTION_TYPE_DYNAMIC_SYMBOLS 11
  83. #define ELF_SECTION_TYPE_OS_LOW 0x60000000
  84. #define ELF_SECTION_TYPE_OS_HIGH 0x6FFFFFFF
  85. #define ELF_SECTION_TYPE_PROCESSOR_LOW 0x70000000
  86. #define ELF_SECTION_TYPE_PROCESSOR_HIGH 0x7FFFFFFF
  87. #define ELF_SECTION_TYPE_USER_LOW 0x80000000
  88. #define ELF_SECTION_TYPE_USER_HIGH 0x8FFFFFFF
  89. #define ELF_SECTION_UNDEFINED 0
  90. #define ELF_SECTION_RESERVED_LOW 0xFF00
  91. #define ELF_SECTION_ABSOLUTE 0xFFF1
  92. #define ELF_SECTION_COMMON 0xFFF2
  93. #define ELF_SECTION_RESERVED_HIGH 0xFFFF
  94. #define ELF_SECTION_FLAG_WRITABLE 0x1
  95. #define ELF_SECTION_FLAG_LOAD 0x2
  96. #define ELF_SECTION_FLAG_EXECUTABLE 0x4
  97. #define ELF_SECTION_FLAG_OS_MASK 0x0F000000
  98. #define ELF_SECTION_FLAG_PROCESSOR_MASK 0xF0000000
  99. #define ELF_SEGMENT_TYPE_NULL 0
  100. #define ELF_SEGMENT_TYPE_LOAD 1
  101. #define ELF_SEGMENT_TYPE_DYNAMIC 2
  102. #define ELF_SEGMENT_TYPE_INTERPRETER 3
  103. #define ELF_SEGMENT_TYPE_NOTE 4
  104. #define ELF_SEGMENT_TYPE_SHLIB 5
  105. #define ELF_SEGMENT_TYPE_PROGRAM_HEADER 6
  106. #define ELF_SEGMENT_TYPE_TLS 7
  107. #define ELF_SEGMENT_PROCESSOR_LOW 0x70000000
  108. #define ELF_SEGMENT_PROCESSOR_HIGH 0x7FFFFFFF
  109. #define ELF_PROGRAM_HEADER_FLAG_EXECUTE 0x00000001
  110. #define ELF_PROGRAM_HEADER_FLAG_WRITE 0x00000002
  111. #define ELF_PROGRAM_HEADER_FLAG_READ 0x00000004
  112. #define ELF_DYNAMIC_NULL 0
  113. #define ELF_DYNAMIC_NEEDED 1
  114. #define ELF_DYNAMIC_PLT_REL_SIZE 2
  115. #define ELF_DYNAMIC_PLT_GOT 3
  116. #define ELF_DYNAMIC_HASH_TABLE 4
  117. #define ELF_DYNAMIC_STRING_TABLE 5
  118. #define ELF_DYNAMIC_SYMBOL_TABLE 6
  119. #define ELF_DYNAMIC_RELA_TABLE 7
  120. #define ELF_DYNAMIC_RELA_TABLE_SIZE 8
  121. #define ELF_DYNAMIC_RELA_ENTRY_SIZE 9
  122. #define ELF_DYNAMIC_STRING_TABLE_SIZE 10
  123. #define ELF_DYNAMIC_SYMBOL_ENTRY_SIZE 11
  124. #define ELF_DYNAMIC_INIT 12
  125. #define ELF_DYNAMIC_FINI 13
  126. #define ELF_DYNAMIC_LIBRARY_NAME 14
  127. #define ELF_DYNAMIC_RPATH 15
  128. #define ELF_DYNAMIC_SYMBOLIC 16
  129. #define ELF_DYNAMIC_REL_TABLE 17
  130. #define ELF_DYNAMIC_REL_TABLE_SIZE 18
  131. #define ELF_DYNAMIC_REL_ENTRY_SIZE 19
  132. #define ELF_DYNAMIC_PLT_RELOCATION_TYPE 20
  133. #define ELF_DYNAMIC_DEBUG 21
  134. #define ELF_DYNAMIC_TEXT_RELOCATIONS 22
  135. #define ELF_DYNAMIC_JUMP_RELOCATIONS 23
  136. #define ELF_DYNAMIC_BIND_NOW 24
  137. #define ELF_DYNAMIC_INIT_ARRAY 25
  138. #define ELF_DYNAMIC_FINI_ARRAY 26
  139. #define ELF_DYNAMIC_INIT_ARRAY_SIZE 27
  140. #define ELF_DYNAMIC_FINI_ARRAY_SIZE 28
  141. #define ELF_DYNAMIC_RUN_PATH 29
  142. #define ELF_DYNAMIC_FLAGS 30
  143. #define ELF_DYNAMIC_PREINIT_ARRAY 32
  144. #define ELF_DYNAMIC_PREINIT_ARRAY_SIZE 33
  145. #define ELF_DYNAMIC_GNU_HASH_TABLE 0x6FFFFEF5
  146. #define ELF_DYNAMIC_PROCESSOR_LOW 0x70000000
  147. #define ELF_DYNAMIC_PROCESSOR_HIGH 0x7FFFFFFF
  148. #define ELF32_WORD_SIZE_SHIFT 5
  149. #define ELF32_WORD_SIZE_MASK ((1 << ELF32_WORD_SIZE_SHIFT) - 1)
  150. #define ELF64_WORD_SIZE_SHIFT 6
  151. #define ELF64_WORD_SIZE_MASK ((1 << ELF64_WORD_SIZE_SHIFT) - 1)
  152. //
  153. // Define ELF dynamic flags.
  154. //
  155. //
  156. // This flag indicates that the $ORIGIN subsitution string may be used.
  157. //
  158. #define ELF_DYNAMIC_FLAG_ORIGIN 0x01
  159. //
  160. // This flag indicates that symbol searches should start from the image itself,
  161. // then start from the executable if the symbol was not found in this image.
  162. //
  163. #define ELF_DYNAMIC_FLAG_SYMBOLIC 0x02
  164. //
  165. // This flag indicates that one or more relocations might require modifying a
  166. // non-writable segment.
  167. //
  168. #define ELF_DYNAMIC_FLAG_TEXT_RELOCATIONS 0x04
  169. //
  170. // This flag indicates that the dynamic linker should process all relocations
  171. // for this object before transferring control to the program. The presence of
  172. // this flag takes precedence over the lazy flag passed to the dynamic load
  173. // routine.
  174. //
  175. #define ELF_DYNAMIC_FLAG_BIND_NOW 0x08
  176. //
  177. // This flag indicates that the image uses the static TLS model, and attempts
  178. // to load this file dynamically should be blocked.
  179. //
  180. #define ELF_DYNAMIC_FLAG_STATIC_TLS 0x10
  181. //
  182. // ------------------------------------------------------ Data Type Definitions
  183. //
  184. typedef ULONG ELF32_ADDR, *PELF32_ADDR;
  185. typedef USHORT ELF32_HALF, *PELF32_HALF;
  186. typedef ULONG ELF32_OFF, *PELF32_OFF;
  187. typedef LONG ELF32_SWORD, *PELF32_SWORD;
  188. typedef ULONG ELF32_WORD, *PELF32_WORD;
  189. typedef ULONGLONG ELF64_ADDR, *PELF64_ADDR;
  190. typedef ULONGLONG ELF64_OFF, *PELF64_OFF;
  191. typedef USHORT ELF64_HALF, *PELF64_HALF;
  192. typedef ULONG ELF64_WORD, *PELF64_WORD;
  193. typedef LONG ELF64_SWORD, *PELF64_SWORD;
  194. typedef ULONGLONG ELF64_XWORD, *PELF64_XWORD;
  195. typedef LONGLONG ELF64_SXWORD, *PELF64_SXWORD;
  196. typedef enum _ELF_SYMBOL_BIND_TYPE {
  197. ElfBindLocal = 0,
  198. ElfBindGlobal = 1,
  199. ElfBindWeak = 2,
  200. } ELF_SYMBOL_BIND_TYPE, *PELF_SYMBOL_BIND_TYPE;
  201. typedef enum _ELF_SYMBOL_TYPE {
  202. ElfSymbolNone = 0,
  203. ElfSymbolObject = 1,
  204. ElfSymbolFunction = 2,
  205. ElfSymbolSection = 3,
  206. ElfSymbolFile = 4,
  207. ElfSymbolCommon = 5,
  208. ElfSymbolTls = 6,
  209. } ELF_SYMBOL_TYPE, *PELF_SYMBOL_TYPE;
  210. typedef enum _ELF_386_RELOCATION_TYPE {
  211. Elf386RelocationNone = 0,
  212. Elf386Relocation32 = 1,
  213. Elf386RelocationPc32 = 2,
  214. Elf386RelocationGot32 = 3,
  215. Elf386RelocationPlt32 = 4,
  216. Elf386RelocationCopy = 5,
  217. Elf386RelocationGlobalData = 6,
  218. Elf386RelocationJumpSlot = 7,
  219. Elf386RelocationRelative = 8,
  220. Elf386RelocationGotOffset = 9,
  221. Elf386RelocationGotPc = 10,
  222. Elf386RelocationTlsTpOff = 14,
  223. Elf386RelocationTlsDtpMod32 = 35,
  224. Elf386RelocationTlsDtpOff32 = 36,
  225. Elf386RelocationTlsTpOff32 = 37,
  226. } ELF_386_RELOCATION_TYPE, *PELF_386_RELOCATION_TYPE;
  227. typedef enum _ELF_X86_64_RELOCATION_TYPE {
  228. ElfX64RelocationNone = 0,
  229. ElfX64Relocation64 = 1,
  230. ElfX64RelocationPc32 = 2,
  231. ElfX64RelocationGot32 = 3,
  232. ElfX64RelocationPlt32 = 4,
  233. ElfX64RelocationCopy = 5,
  234. ElfX64RelocationGlobalData = 6,
  235. ElfX64RelocationJumpSlot = 7,
  236. ElfX64RelocationRelative = 8,
  237. ElfX64RelocationGotPcRelative = 9,
  238. ElfX64Relocation32 = 10,
  239. ElfX64Relocation32S = 11,
  240. ElfX64Relocation16 = 12,
  241. ElfX64RelocationPc16 = 13,
  242. ElfX64Relocation8 = 14,
  243. ElfX64RelocationPc8 = 15,
  244. ElfX64RelocationDtpMod64 = 16,
  245. ElfX64RelocationDtpOff64 = 17,
  246. ElfX64RelocationTpOff64 = 18,
  247. ElfX64RelocationTlsGd = 19,
  248. ElfX64RelocationTlsLd = 20,
  249. ElfX64RelocationDtpOff32 = 21,
  250. ElfX64RelocationGotTpOff = 22,
  251. ElfX64RelocationTpOff32 = 23,
  252. ElfX64RelocationPc64 = 24,
  253. ElfX64RelocationGotOff64 = 25,
  254. ElfX64RelocationGotPc32 = 26,
  255. ElfX64RelocationSize32 = 32,
  256. ElfX64RelocationSize64 = 33,
  257. ElfX64RelocationGotPc32TlsDesc = 34,
  258. ElfX64RelocationTlsDescCall = 35,
  259. ElfX64RelocationTlsDesc = 36,
  260. ElfX64RelocationIRelative = 37,
  261. ElfX64RelocationRelative64 = 38,
  262. } ELF_X86_64_RELOCATION_TYPE, *PELF_X86_64_RELOCATION_TYPE;
  263. typedef enum _ELF_ARM_RELOCATION_TYPE {
  264. ElfArmRelocationNone = 0,
  265. ElfArmRelocationAbsolute32 = 2,
  266. ElfArmRelocationTlsDtpMod32 = 17,
  267. ElfArmRelocationTlsDtpOff32 = 18,
  268. ElfArmRelocationTlsTpOff32 = 19,
  269. ElfArmRelocationCopy = 20,
  270. ElfArmRelocationGlobalData = 21,
  271. ElfArmRelocationJumpSlot = 22,
  272. ElfArmRelocationRelative = 23,
  273. ElfArmRelocationGotOffset = 24,
  274. ElfArmRelocationGotPc = 25,
  275. ElfArmRelocationGot32 = 26,
  276. ElfArmRelocationPlt32 = 27
  277. } ELF_ARM_RELOCATION_TYPE, *PELF_ARM_RELOCATION_TYPE;
  278. //
  279. // 32-bit structures
  280. //
  281. /*++
  282. Structure Description:
  283. This structure stores the main file header for an ELF image. It is located
  284. at offset 0 in the file and stores the locations of other ELF headers.
  285. Members:
  286. Identification - Stores a magic number identifying the file as an ELF file,
  287. as well as several other pieces of information such as the file version,
  288. ELF version, endianness, etc.
  289. ImageType - Stores the type of file this is (relocatable, executable,
  290. shared object, etc.)
  291. Machine - Stores the machine architecture of the code in this image.
  292. Version - Stores the version of the ELF format.
  293. EntryPoint - Stores the virtual address where the system should transfer
  294. control to once the image is loaded.
  295. ProgramHeaderOffset - Stores the offset within the file to the first
  296. program header.
  297. SectionHeaderOffset - Stores the offset within the file to the first
  298. section header.
  299. Flags - Stores processor-specific flags associated with the file.
  300. ElfHeaderSize - Stores the size of this header, in bytes.
  301. ProgramHeaderSize - Stores the size of one program header, in bytes.
  302. ProgramHeaderCount - Stores the number of program headers in the file.
  303. SectionHeaderSize - Stores the size of one section header, in bytes.
  304. SectionHeaderCount - Stores the number of section headers in the file.
  305. StringSectionIndex - Stores the section header table index of the entry
  306. associated with the section name string table.
  307. --*/
  308. #pragma pack(push, 1)
  309. typedef struct _ELF32_HEADER {
  310. UCHAR Identification[ELF32_IDENTIFICATION_LENGTH];
  311. ELF32_HALF ImageType;
  312. ELF32_HALF Machine;
  313. ELF32_WORD Version;
  314. ELF32_ADDR EntryPoint;
  315. ELF32_OFF ProgramHeaderOffset;
  316. ELF32_OFF SectionHeaderOffset;
  317. ELF32_WORD Flags;
  318. ELF32_HALF ElfHeaderSize;
  319. ELF32_HALF ProgramHeaderSize;
  320. ELF32_HALF ProgramHeaderCount;
  321. ELF32_HALF SectionHeaderSize;
  322. ELF32_HALF SectionHeaderCount;
  323. ELF32_HALF StringSectionIndex;
  324. } PACKED ELF32_HEADER, *PELF32_HEADER;
  325. /*++
  326. Structure Description:
  327. This structure stores information about a section of data in an ELF32 file.
  328. Members:
  329. NameOffset - Stores the index into the string table section, giving the
  330. location of a null-terminated string of the name of the section.
  331. Type - Stores a description of the section's contents and semantics. See
  332. the ELF_SECTION_TYPE_* definitions.
  333. Flags - Stores miscellaneous attributes of the section. See the
  334. ELF_SECTION_FLAG_* definitions.
  335. VirtualAddress - Stores the virtual address of the beginning of this
  336. section.
  337. Offset - Stores the byte offset from the beginning of the file to the first
  338. byte in the section. If the section type is NOBITS, and this field
  339. locates the conceptual placement in the file.
  340. Size - Stores the size of the section in bytes both in memory an on disk
  341. (unless the type is NOBITS, in which case the file contains no bytes).
  342. Link - Stores a section header table index link, whose interpretation
  343. depends on the section type.
  344. Information - Stores extra information, whose interpretation depends on the
  345. section type.
  346. Alignment - Stores the alignment constraints of the section. Only 0 and
  347. positive powers of 2 are supported. Values of 0 or 1 indicate no
  348. alignment constraints.
  349. EntrySize - Stores the size of one entry, if the section holds a table of
  350. fixed size entries, such as a symbol table. This field contains 0 if
  351. the section does not hold a table of fixed size entries.
  352. --*/
  353. typedef struct _ELF32_SECTION_HEADER {
  354. ELF32_WORD NameOffset;
  355. ELF32_WORD SectionType;
  356. ELF32_WORD Flags;
  357. ELF32_ADDR VirtualAddress;
  358. ELF32_OFF Offset;
  359. ELF32_WORD Size;
  360. ELF32_WORD Link;
  361. ELF32_WORD Information;
  362. ELF32_WORD Alignment;
  363. ELF32_WORD EntrySize;
  364. } PACKED ELF32_SECTION_HEADER, *PELF32_SECTION_HEADER;
  365. /*++
  366. Structure Description:
  367. This structure stores information about a program segment, used to load
  368. ELF32 files. Segments are distinct from sections. One segment may contain
  369. multiple sections.
  370. Members:
  371. Type - Stores what type of segment this array element describes. See
  372. ELF_SEGMENT_TYPE_* definitions.
  373. Offset - Stores the offset from the beginning of the file, in bytes, at
  374. which the first byte of the segment resides.
  375. VirtualAddress - Stores the virtual address at which the first byte of the
  376. segment resides in memory.
  377. PhysicalAddress - Stores the physical address at which the segment resides.
  378. This field is almost always the same as the virtual address.
  379. FileSize - Stores the number of bytes in the file image of the segment. It
  380. may be zero.
  381. MemorySize - Stores the number of bytes in the memory image of the segment.
  382. It may be zero.
  383. Flags - Stores flags regarding this segment. See ELF_PROGRAM_HEADER_FLAG_*
  384. definitions.
  385. Alignment - Stores the power of two alignment requirement for the segment.
  386. Values of 0 and 1 mean that no alignment is required.
  387. --*/
  388. typedef struct _ELF32_PROGRAM_HEADER {
  389. ELF32_WORD Type;
  390. ELF32_OFF Offset;
  391. ELF32_ADDR VirtualAddress;
  392. ELF32_ADDR PhysicalAddress;
  393. ELF32_WORD FileSize;
  394. ELF32_WORD MemorySize;
  395. ELF32_WORD Flags;
  396. ELF32_WORD Alignment;
  397. } PACKED ELF32_PROGRAM_HEADER, *PELF32_PROGRAM_HEADER;
  398. /*++
  399. Structure Description:
  400. This structure stores information about a relocation entry.
  401. Members:
  402. Offset - Stores the location at which to apply the relocation action. For
  403. an executable or shared file object, the value is the virtual address
  404. of the storage unit affected by the relocation.
  405. Information - Stores both the symbol table index with respect to which
  406. relocation must be made, and the type of relocation to apply.
  407. --*/
  408. typedef struct _ELF32_RELOCATION_ENTRY {
  409. ELF32_ADDR Offset;
  410. ELF32_WORD Information;
  411. } PACKED ELF32_RELOCATION_ENTRY, *PELF32_RELOCATION_ENTRY;
  412. /*++
  413. Structure Description:
  414. This structure stores information about a relocation entry with an addend.
  415. Members:
  416. Offset - Stores the location at which to apply the relocation action. For
  417. an executable or shared file object, the value is the virtual address
  418. of the storage unit affected by the relocation.
  419. Information - Stores both the symbol table index with respect to which
  420. relocation must be made, and the type of relocation to apply.
  421. Addend - Stores a constant addend used to compute the value to be stored in
  422. the relocatable field.
  423. --*/
  424. typedef struct _ELF32_RELOCATION_ADDEND_ENTRY {
  425. ELF32_ADDR Offset;
  426. ELF32_WORD Information;
  427. ELF32_SWORD Addend;
  428. } PACKED ELF32_RELOCATION_ADDEND_ENTRY, *PELF32_RELOCATION_ADDEND_ENTRY;
  429. /*++
  430. Structure Description:
  431. This structure stores a symbol entry in the ELF format.
  432. Members:
  433. NameOffset - Stores an offset into the string table where the name of the
  434. symbol is stored.
  435. Value - Stores the value or address of the symbol.
  436. Size - Stores a size associated with the symbol. For data symbols, this
  437. store the size of the type. This member holds 0 if the symbol is an
  438. unknown size.
  439. Information - Stores the symbols type and binding attributes.
  440. Other - Stores 0 and has no defined meaning.
  441. SectionIndex - Stores the section index of the section related to this
  442. symbol. All symbols are defined in the context of a section.
  443. --*/
  444. typedef struct _ELF32_SYMBOL {
  445. ELF32_WORD NameOffset;
  446. ELF32_ADDR Value;
  447. ELF32_WORD Size;
  448. UCHAR Information;
  449. UCHAR Other;
  450. ELF32_HALF SectionIndex;
  451. } PACKED ELF32_SYMBOL, *PELF32_SYMBOL;
  452. /*++
  453. Structure Description:
  454. This structure stores a single entry located in a dynamic section
  455. Members:
  456. Tag - Stores the type of entry and the interpretation of the Value. See
  457. ELF_DYNAMIC_* definitions.
  458. Value - Stores the entry value, whose meaning varies with the type.
  459. --*/
  460. typedef struct _ELF32_DYNAMIC_ENTRY {
  461. ELF32_SWORD Tag;
  462. ELF32_WORD Value;
  463. } PACKED ELF32_DYNAMIC_ENTRY, *PELF32_DYNAMIC_ENTRY;
  464. //
  465. // 64-bit structures
  466. //
  467. /*++
  468. Structure Description:
  469. This structure stores the main file header for an ELF image. It is located
  470. at offset 0 in the file and stores the locations of other ELF headers.
  471. Members:
  472. Identification - Stores a magic number identifying the file as an ELF file,
  473. as well as several other pieces of information such as the file version,
  474. ELF version, endianness, etc.
  475. ImageType - Stores the type of file this is (relocatable, executable,
  476. shared object, etc.)
  477. Machine - Stores the machine architecture of the code in this image.
  478. Version - Stores the version of the ELF format.
  479. EntryPoint - Stores the virtual address where the system should transfer
  480. control to once the image is loaded.
  481. ProgramHeaderOffset - Stores the offset within the file to the first
  482. program header.
  483. SectionHeaderOffset - Stores the offset within the file to the first
  484. section header.
  485. Flags - Stores processor-specific flags associated with the file.
  486. ElfHeaderSize - Stores the size of this header, in bytes.
  487. ProgramHeaderSize - Stores the size of one program header, in bytes.
  488. ProgramHeaderCount - Stores the number of program headers in the file.
  489. SectionHeaderSize - Stores the size of one section header, in bytes.
  490. SectionHeaderCount - Stores the number of section headers in the file.
  491. StringSectionIndex - Stores the section header table index of the entry
  492. associated with the section name string table.
  493. --*/
  494. typedef struct _ELF64_HEADER {
  495. UCHAR Identification[ELF64_IDENTIFICATION_LENGTH];
  496. ELF64_HALF ImageType;
  497. ELF64_HALF Machine;
  498. ELF64_WORD Version;
  499. ELF64_ADDR EntryPoint;
  500. ELF64_OFF ProgramHeaderOffset;
  501. ELF64_OFF SectionHeaderOffset;
  502. ELF64_WORD Flags;
  503. ELF64_HALF ElfHeaderSize;
  504. ELF64_HALF ProgramHeaderSize;
  505. ELF64_HALF ProgramHeaderCount;
  506. ELF64_HALF SectionHeaderSize;
  507. ELF64_HALF SectionHeaderCount;
  508. ELF64_HALF StringSectionIndex;
  509. } PACKED ELF64_HEADER, *PELF64_HEADER;
  510. /*++
  511. Structure Description:
  512. This structure stores information about a section of data in an ELF32 file.
  513. Members:
  514. NameOffset - Stores the index into the string table section, giving the
  515. location of a null-terminated string of the name of the section.
  516. Type - Stores a description of the section's contents and semantics. See
  517. the ELF_SECTION_TYPE_* definitions.
  518. Flags - Stores miscellaneous attributes of the section. See the
  519. ELF_SECTION_FLAG_* definitions.
  520. VirtualAddress - Stores the virtual address of the beginning of this
  521. section.
  522. Offset - Stores the byte offset from the beginning of the file to the first
  523. byte in the section. If the section type is NOBITS, and this field
  524. locates the conceptual placement in the file.
  525. Size - Stores the size of the section in bytes both in memory an on disk
  526. (unless the type is NOBITS, in which case the file contains no bytes).
  527. Link - Stores a section header table index link, whose interpretation
  528. depends on the section type.
  529. Information - Stores extra information, whose interpretation depends on the
  530. section type.
  531. Alignment - Stores the alignment constraints of the section. Only 0 and
  532. positive powers of 2 are supported. Values of 0 or 1 indicate no
  533. alignment constraints.
  534. EntrySize - Stores the size of one entry, if the section holds a table of
  535. fixed size entries, such as a symbol table. This field contains 0 if
  536. the section does not hold a table of fixed size entries.
  537. --*/
  538. typedef struct _ELF64_SECTION_HEADER {
  539. ELF64_WORD NameOffset;
  540. ELF64_WORD SectionType;
  541. ELF64_XWORD Flags;
  542. ELF64_ADDR VirtualAddress;
  543. ELF64_OFF Offset;
  544. ELF64_XWORD Size;
  545. ELF64_WORD Link;
  546. ELF64_WORD Information;
  547. ELF64_XWORD Alignment;
  548. ELF64_XWORD EntrySize;
  549. } PACKED ELF64_SECTION_HEADER, *PELF64_SECTION_HEADER;
  550. /*++
  551. Structure Description:
  552. This structure stores information about a program segment, used to load
  553. ELF64 files. Segments are distinct from sections. One segment may contain
  554. multiple sections.
  555. Members:
  556. Type - Stores what type of segment this array element describes. See
  557. ELF_SEGMENT_TYPE_* definitions.
  558. Flags - Stores flags regarding this segment. See ELF_PROGRAM_HEADER_FLAG_*
  559. definitions.
  560. Offset - Stores the offset from the beginning of the file, in bytes, at
  561. which the first byte of the segment resides.
  562. VirtualAddress - Stores the virtual address at which the first byte of the
  563. segment resides in memory.
  564. PhysicalAddress - Stores the physical address at which the segment resides.
  565. This field is almost always the same as the virtual address.
  566. FileSize - Stores the number of bytes in the file image of the segment. It
  567. may be zero.
  568. MemorySize - Stores the number of bytes in the memory image of the segment.
  569. It may be zero.
  570. Alignment - Stores the power of two alignment requirement for the segment.
  571. Values of 0 and 1 mean that no alignment is required.
  572. --*/
  573. typedef struct _ELF64_PROGRAM_HEADER {
  574. ELF64_WORD Type;
  575. ELF64_WORD Flags;
  576. ELF64_OFF Offset;
  577. ELF64_ADDR VirtualAddress;
  578. ELF64_ADDR PhysicalAddress;
  579. ELF64_XWORD FileSize;
  580. ELF64_XWORD MemorySize;
  581. ELF64_XWORD Alignment;
  582. } PACKED ELF64_PROGRAM_HEADER, *PELF64_PROGRAM_HEADER;
  583. /*++
  584. Structure Description:
  585. This structure stores information about a relocation entry.
  586. Members:
  587. Offset - Stores the location at which to apply the relocation action. For
  588. an executable or shared file object, the value is the virtual address
  589. of the storage unit affected by the relocation.
  590. Information - Stores both the symbol table index with respect to which
  591. relocation must be made, and the type of relocation to apply.
  592. --*/
  593. typedef struct _ELF64_RELOCATION_ENTRY {
  594. ELF64_ADDR Offset;
  595. ELF64_XWORD Information;
  596. } PACKED ELF64_RELOCATION_ENTRY, *PELF64_RELOCATION_ENTRY;
  597. /*++
  598. Structure Description:
  599. This structure stores information about a relocation entry with an addend.
  600. Members:
  601. Offset - Stores the location at which to apply the relocation action. For
  602. an executable or shared file object, the value is the virtual address
  603. of the storage unit affected by the relocation.
  604. Information - Stores both the symbol table index with respect to which
  605. relocation must be made, and the type of relocation to apply.
  606. Addend - Stores a constant addend used to compute the value to be stored in
  607. the relocatable field.
  608. --*/
  609. typedef struct _ELF64_RELOCATION_ADDEND_ENTRY {
  610. ELF64_ADDR Offset;
  611. ELF64_XWORD Information;
  612. ELF64_SXWORD Addend;
  613. } PACKED ELF64_RELOCATION_ADDEND_ENTRY, *PELF64_RELOCATION_ADDEND_ENTRY;
  614. /*++
  615. Structure Description:
  616. This structure stores a symbol entry in the ELF format.
  617. Members:
  618. NameOffset - Stores an offset into the string table where the name of the
  619. symbol is stored.
  620. Information - Stores the symbols type and binding attributes.
  621. Other - Stores 0 and has no defined meaning.
  622. SectionIndex - Stores the section index of the section related to this
  623. symbol. All symbols are defined in the context of a section.
  624. Value - Stores the value or address of the symbol.
  625. Size - Stores a size associated with the symbol. For data symbols, this
  626. store the size of the type. This member holds 0 if the symbol is an
  627. unknown size.
  628. --*/
  629. typedef struct _ELF64_SYMBOL {
  630. ELF64_WORD NameOffset;
  631. UCHAR Information;
  632. UCHAR Other;
  633. ELF64_HALF SectionIndex;
  634. ELF64_ADDR Value;
  635. ELF64_XWORD Size;
  636. } PACKED ELF64_SYMBOL, *PELF64_SYMBOL;
  637. /*++
  638. Structure Description:
  639. This structure stores a single entry located in a dynamic section
  640. Members:
  641. Tag - Stores the type of entry and the interpretation of the Value. See
  642. ELF_DYNAMIC_* definitions.
  643. Value - Stores the entry value, whose meaning varies with the type.
  644. --*/
  645. typedef struct _ELF64_DYNAMIC_ENTRY {
  646. ELF64_SXWORD Tag;
  647. ELF64_XWORD Value;
  648. } PACKED ELF64_DYNAMIC_ENTRY, *PELF64_DYNAMIC_ENTRY;
  649. #pragma pack(pop)
  650. //
  651. // -------------------------------------------------------------------- Globals
  652. //
  653. //
  654. // -------------------------------------------------------- Function Prototypes
  655. //
  656. //
  657. // 32-bit ELF functions.
  658. //
  659. KSTATUS
  660. ImpElf32OpenLibrary (
  661. PLIST_ENTRY ListHead,
  662. PLOADED_IMAGE Parent,
  663. PCSTR LibraryName,
  664. PIMAGE_FILE_INFORMATION File,
  665. PSTR *Path
  666. );
  667. /*++
  668. Routine Description:
  669. This routine attempts to open a dynamic library.
  670. Arguments:
  671. ListHead - Supplies an optional pointer to the head of the list of loaded
  672. images.
  673. Parent - Supplies a pointer to the parent image requiring this image for
  674. load.
  675. LibraryName - Supplies the name of the library to open.
  676. File - Supplies a pointer where the information for the file including its
  677. open handle will be returned.
  678. Path - Supplies a pointer where the real path to the opened file will be
  679. returned. The caller is responsible for freeing this memory.
  680. Return Value:
  681. Status code.
  682. --*/
  683. KSTATUS
  684. ImpElf32GetImageSize (
  685. PLIST_ENTRY ListHead,
  686. PLOADED_IMAGE Image,
  687. PIMAGE_BUFFER Buffer,
  688. PSTR *InterpreterPath
  689. );
  690. /*++
  691. Routine Description:
  692. This routine determines the size of an ELF executable image. The image size,
  693. preferred lowest address, and relocatable flag will all be filled in.
  694. Arguments:
  695. ListHead - Supplies a pointer to the head of the list of loaded images.
  696. Image - Supplies a pointer to the image to get the size of.
  697. Buffer - Supplies a pointer to the loaded image buffer.
  698. InterpreterPath - Supplies a pointer where the interpreter name will be
  699. returned if the program is requesting an interpreter.
  700. Return Value:
  701. Returns the size of the expanded image in memory on success.
  702. 0 on failure.
  703. --*/
  704. KSTATUS
  705. ImpElf32LoadImage (
  706. PLIST_ENTRY ListHead,
  707. PLOADED_IMAGE Image,
  708. PIMAGE_BUFFER Buffer
  709. );
  710. /*++
  711. Routine Description:
  712. This routine loads an ELF image into its executable form.
  713. Arguments:
  714. ListHead - Supplies a pointer to the head of the list of loaded images.
  715. Image - Supplies a pointer to the loaded image. This must be partially
  716. filled out. Notable fields that must be filled out by the caller
  717. include the loaded virtual address and image size. This routine will
  718. fill out many other fields.
  719. Buffer - Supplies a pointer to the image buffer.
  720. Return Value:
  721. STATUS_SUCCESS on success.
  722. STATUS_FILE_CORRUPT if the file headers were corrupt or unexpected.
  723. Other errors on failure.
  724. --*/
  725. KSTATUS
  726. ImpElf32AddImage (
  727. PIMAGE_BUFFER ImageBuffer,
  728. PLOADED_IMAGE Image
  729. );
  730. /*++
  731. Routine Description:
  732. This routine adds the accounting structures for an image that has already
  733. been loaded into memory.
  734. Arguments:
  735. ImageBuffer - Supplies a pointer to the loaded image buffer.
  736. Image - Supplies a pointer to the image to initialize.
  737. Return Value:
  738. Status code.
  739. --*/
  740. VOID
  741. ImpElf32UnloadImage (
  742. PLOADED_IMAGE Image
  743. );
  744. /*++
  745. Routine Description:
  746. This routine unloads an ELF executable.
  747. Arguments:
  748. Image - Supplies a pointer to the loaded image.
  749. Return Value:
  750. None.
  751. --*/
  752. BOOL
  753. ImpElf32GetHeader (
  754. PIMAGE_BUFFER Buffer,
  755. PELF32_HEADER *ElfHeader
  756. );
  757. /*++
  758. Routine Description:
  759. This routine returns a pointer to the ELF image header given a buffer
  760. containing the executable image mapped in memory.
  761. Arguments:
  762. Buffer - Supplies a pointer to the loaded image buffer.
  763. ElfHeader - Supplies a pointer where the location of the ELF header will
  764. be returned.
  765. Return Value:
  766. TRUE on success.
  767. FALSE otherwise.
  768. --*/
  769. BOOL
  770. ImpElf32GetSection (
  771. PIMAGE_BUFFER Buffer,
  772. PSTR SectionName,
  773. PVOID *Section,
  774. PULONGLONG VirtualAddress,
  775. PULONG SectionSizeInFile,
  776. PULONG SectionSizeInMemory
  777. );
  778. /*++
  779. Routine Description:
  780. This routine gets a pointer to the given section in an ELF image given a
  781. memory mapped file.
  782. Arguments:
  783. Buffer - Supplies a pointer to the image buffer.
  784. SectionName - Supplies the name of the desired section.
  785. Section - Supplies a pointer where the pointer to the section will be
  786. returned.
  787. VirtualAddress - Supplies a pointer where the virtual address of the section
  788. will be returned, if applicable.
  789. SectionSizeInFile - Supplies a pointer where the size of the section as it
  790. appears in the file will be returned.
  791. SectionSizeInMemory - Supplies a pointer where the size of the section as it
  792. appears after being loaded in memory will be returned.
  793. Return Value:
  794. TRUE on success.
  795. FALSE otherwise.
  796. --*/
  797. KSTATUS
  798. ImpElf32LoadAllImports (
  799. PLIST_ENTRY ListHead
  800. );
  801. /*++
  802. Routine Description:
  803. This routine loads all import libraries for all images.
  804. Arguments:
  805. ListHead - Supplies a pointer to the head of the list of loaded images.
  806. Return Value:
  807. Status code.
  808. --*/
  809. KSTATUS
  810. ImpElf32RelocateImages (
  811. PLIST_ENTRY ListHead
  812. );
  813. /*++
  814. Routine Description:
  815. This routine relocates all images on the given image list that have not
  816. yet been relocated.
  817. Arguments:
  818. ListHead - Supplies a pointer to the head of the list to relocate.
  819. Return Value:
  820. Status code.
  821. --*/
  822. VOID
  823. ImpElf32RelocateSelf (
  824. PIMAGE_BUFFER Buffer,
  825. PIM_RESOLVE_PLT_ENTRY PltResolver,
  826. PLOADED_IMAGE Image
  827. );
  828. /*++
  829. Routine Description:
  830. This routine relocates the currently running image.
  831. Arguments:
  832. Buffer - Supplies a pointer to the image buffer.
  833. PltResolver - Supplies a pointer to the function used to resolve PLT
  834. entries.
  835. Image - Supplies a pointer to the zeroed but otherwise uninitialized
  836. image buffer.
  837. Return Value:
  838. None.
  839. --*/
  840. KSTATUS
  841. ImpElf32GetSymbolByName (
  842. PLOADED_IMAGE Image,
  843. PSTR SymbolName,
  844. PLOADED_IMAGE Skip,
  845. PIMAGE_SYMBOL Symbol
  846. );
  847. /*++
  848. Routine Description:
  849. This routine attempts to find an exported symbol with the given name in the
  850. given binary.
  851. Arguments:
  852. Image - Supplies a pointer to the image to query.
  853. SymbolName - Supplies a pointer to the string containing the name of the
  854. symbol to search for.
  855. Skip - Supplies an optional pointer to an image to skip when searching.
  856. Symbol - Supplies a pointer to a structure that receives the symbol's
  857. information on success.
  858. Return Value:
  859. Status code.
  860. --*/
  861. KSTATUS
  862. ImpElf32GetSymbolByAddress (
  863. PLOADED_IMAGE Image,
  864. PVOID Address,
  865. PIMAGE_SYMBOL Symbol
  866. );
  867. /*++
  868. Routine Description:
  869. This routine attempts to find the given address in the given image and
  870. resolve it to a symbol.
  871. Arguments:
  872. Image - Supplies a pointer to the image to query.
  873. Address - Supplies the address to search for.
  874. Symbol - Supplies a pointer to a structure that receives the address's
  875. symbol information on success.
  876. Return Value:
  877. Status code.
  878. --*/
  879. PVOID
  880. ImpElf32ResolvePltEntry (
  881. PLOADED_IMAGE Image,
  882. ULONG RelocationOffset
  883. );
  884. /*++
  885. Routine Description:
  886. This routine implements the slow path for a Procedure Linkable Table entry
  887. that has not yet been resolved to its target function address. This routine
  888. is only called once for each PLT entry, as subsequent calls jump directly
  889. to the destination function address. It resolves the appropriate GOT
  890. relocation and returns a pointer to the function to jump to.
  891. Arguments:
  892. Image - Supplies a pointer to the loaded image whose PLT needs resolution.
  893. This is really whatever pointer is in GOT + 4.
  894. RelocationOffset - Supplies the byte offset from the start of the
  895. relocation section where the relocation for this PLT entry resides, or
  896. the PLT index, depending on the architecture.
  897. Return Value:
  898. Returns a pointer to the function to jump to (in addition to writing that
  899. address in the GOT at the appropriate spot).
  900. --*/
  901. //
  902. // 64-bit ELF functions. These are exactly the same as the 32-bit functions.
  903. //
  904. KSTATUS
  905. ImpElf64OpenLibrary (
  906. PLIST_ENTRY ListHead,
  907. PLOADED_IMAGE Parent,
  908. PCSTR LibraryName,
  909. PIMAGE_FILE_INFORMATION File,
  910. PSTR *Path
  911. );
  912. /*++
  913. Routine Description:
  914. This routine attempts to open a dynamic library.
  915. Arguments:
  916. ListHead - Supplies an optional pointer to the head of the list of loaded
  917. images.
  918. Parent - Supplies a pointer to the parent image requiring this image for
  919. load.
  920. LibraryName - Supplies the name of the library to open.
  921. File - Supplies a pointer where the information for the file including its
  922. open handle will be returned.
  923. Path - Supplies a pointer where the real path to the opened file will be
  924. returned. The caller is responsible for freeing this memory.
  925. Return Value:
  926. Status code.
  927. --*/
  928. KSTATUS
  929. ImpElf64GetImageSize (
  930. PLIST_ENTRY ListHead,
  931. PLOADED_IMAGE Image,
  932. PIMAGE_BUFFER Buffer,
  933. PSTR *InterpreterPath
  934. );
  935. /*++
  936. Routine Description:
  937. This routine determines the size of an ELF executable image. The image size,
  938. preferred lowest address, and relocatable flag will all be filled in.
  939. Arguments:
  940. ListHead - Supplies a pointer to the head of the list of loaded images.
  941. Image - Supplies a pointer to the image to get the size of.
  942. Buffer - Supplies a pointer to the loaded image buffer.
  943. InterpreterPath - Supplies a pointer where the interpreter name will be
  944. returned if the program is requesting an interpreter.
  945. Return Value:
  946. Returns the size of the expanded image in memory on success.
  947. 0 on failure.
  948. --*/
  949. KSTATUS
  950. ImpElf64LoadImage (
  951. PLIST_ENTRY ListHead,
  952. PLOADED_IMAGE Image,
  953. PIMAGE_BUFFER Buffer
  954. );
  955. /*++
  956. Routine Description:
  957. This routine loads an ELF image into its executable form.
  958. Arguments:
  959. ListHead - Supplies a pointer to the head of the list of loaded images.
  960. Image - Supplies a pointer to the loaded image. This must be partially
  961. filled out. Notable fields that must be filled out by the caller
  962. include the loaded virtual address and image size. This routine will
  963. fill out many other fields.
  964. Buffer - Supplies a pointer to the image buffer.
  965. Return Value:
  966. STATUS_SUCCESS on success.
  967. STATUS_FILE_CORRUPT if the file headers were corrupt or unexpected.
  968. Other errors on failure.
  969. --*/
  970. KSTATUS
  971. ImpElf64AddImage (
  972. PIMAGE_BUFFER ImageBuffer,
  973. PLOADED_IMAGE Image
  974. );
  975. /*++
  976. Routine Description:
  977. This routine adds the accounting structures for an image that has already
  978. been loaded into memory.
  979. Arguments:
  980. ImageBuffer - Supplies a pointer to the loaded image buffer.
  981. Image - Supplies a pointer to the image to initialize.
  982. Return Value:
  983. Status code.
  984. --*/
  985. VOID
  986. ImpElf64UnloadImage (
  987. PLOADED_IMAGE Image
  988. );
  989. /*++
  990. Routine Description:
  991. This routine unloads an ELF executable.
  992. Arguments:
  993. Image - Supplies a pointer to the loaded image.
  994. Return Value:
  995. None.
  996. --*/
  997. BOOL
  998. ImpElf64GetHeader (
  999. PIMAGE_BUFFER Buffer,
  1000. PELF64_HEADER *ElfHeader
  1001. );
  1002. /*++
  1003. Routine Description:
  1004. This routine returns a pointer to the ELF image header given a buffer
  1005. containing the executable image mapped in memory.
  1006. Arguments:
  1007. Buffer - Supplies a pointer to the loaded image buffer.
  1008. ElfHeader - Supplies a pointer where the location of the ELF header will
  1009. be returned.
  1010. Return Value:
  1011. TRUE on success.
  1012. FALSE otherwise.
  1013. --*/
  1014. BOOL
  1015. ImpElf64GetSection (
  1016. PIMAGE_BUFFER Buffer,
  1017. PSTR SectionName,
  1018. PVOID *Section,
  1019. PULONGLONG VirtualAddress,
  1020. PULONG SectionSizeInFile,
  1021. PULONG SectionSizeInMemory
  1022. );
  1023. /*++
  1024. Routine Description:
  1025. This routine gets a pointer to the given section in an ELF image given a
  1026. memory mapped file.
  1027. Arguments:
  1028. Buffer - Supplies a pointer to the image buffer.
  1029. SectionName - Supplies the name of the desired section.
  1030. Section - Supplies a pointer where the pointer to the section will be
  1031. returned.
  1032. VirtualAddress - Supplies a pointer where the virtual address of the section
  1033. will be returned, if applicable.
  1034. SectionSizeInFile - Supplies a pointer where the size of the section as it
  1035. appears in the file will be returned.
  1036. SectionSizeInMemory - Supplies a pointer where the size of the section as it
  1037. appears after being loaded in memory will be returned.
  1038. Return Value:
  1039. TRUE on success.
  1040. FALSE otherwise.
  1041. --*/
  1042. KSTATUS
  1043. ImpElf64LoadAllImports (
  1044. PLIST_ENTRY ListHead
  1045. );
  1046. /*++
  1047. Routine Description:
  1048. This routine loads all import libraries for all images.
  1049. Arguments:
  1050. ListHead - Supplies a pointer to the head of the list of loaded images.
  1051. Return Value:
  1052. Status code.
  1053. --*/
  1054. KSTATUS
  1055. ImpElf64RelocateImages (
  1056. PLIST_ENTRY ListHead
  1057. );
  1058. /*++
  1059. Routine Description:
  1060. This routine relocates all images on the given image list that have not
  1061. yet been relocated.
  1062. Arguments:
  1063. ListHead - Supplies a pointer to the head of the list to relocate.
  1064. Return Value:
  1065. Status code.
  1066. --*/
  1067. VOID
  1068. ImpElf64RelocateSelf (
  1069. PIMAGE_BUFFER Buffer,
  1070. PIM_RESOLVE_PLT_ENTRY PltResolver,
  1071. PLOADED_IMAGE Image
  1072. );
  1073. /*++
  1074. Routine Description:
  1075. This routine relocates the currently running image.
  1076. Arguments:
  1077. Buffer - Supplies a pointer to the image buffer.
  1078. PltResolver - Supplies a pointer to the function used to resolve PLT
  1079. entries.
  1080. Image - Supplies a pointer to the zeroed but otherwise uninitialized
  1081. image buffer.
  1082. Return Value:
  1083. None.
  1084. --*/
  1085. KSTATUS
  1086. ImpElf64GetSymbolByName (
  1087. PLOADED_IMAGE Image,
  1088. PSTR SymbolName,
  1089. PLOADED_IMAGE Skip,
  1090. PIMAGE_SYMBOL Symbol
  1091. );
  1092. /*++
  1093. Routine Description:
  1094. This routine attempts to find an exported symbol with the given name in the
  1095. given binary.
  1096. Arguments:
  1097. Image - Supplies a pointer to the image to query.
  1098. SymbolName - Supplies a pointer to the string containing the name of the
  1099. symbol to search for.
  1100. Skip - Supplies an optional pointer to an image to skip when searching.
  1101. Symbol - Supplies a pointer to a structure that receives the symbol's
  1102. information on success.
  1103. Return Value:
  1104. Status code.
  1105. --*/
  1106. KSTATUS
  1107. ImpElf64GetSymbolByAddress (
  1108. PLOADED_IMAGE Image,
  1109. PVOID Address,
  1110. PIMAGE_SYMBOL Symbol
  1111. );
  1112. /*++
  1113. Routine Description:
  1114. This routine attempts to find the given address in the given image and
  1115. resolve it to a symbol.
  1116. Arguments:
  1117. Image - Supplies a pointer to the image to query.
  1118. Address - Supplies the address to search for.
  1119. Symbol - Supplies a pointer to a structure that receives the address's
  1120. symbol information on success.
  1121. Return Value:
  1122. Status code.
  1123. --*/
  1124. PVOID
  1125. ImpElf64ResolvePltEntry (
  1126. PLOADED_IMAGE Image,
  1127. ULONG RelocationOffset
  1128. );
  1129. /*++
  1130. Routine Description:
  1131. This routine implements the slow path for a Procedure Linkable Table entry
  1132. that has not yet been resolved to its target function address. This routine
  1133. is only called once for each PLT entry, as subsequent calls jump directly
  1134. to the destination function address. It resolves the appropriate GOT
  1135. relocation and returns a pointer to the function to jump to.
  1136. Arguments:
  1137. Image - Supplies a pointer to the loaded image whose PLT needs resolution.
  1138. This is really whatever pointer is in GOT + 4.
  1139. RelocationOffset - Supplies the byte offset from the start of the
  1140. relocation section where the relocation for this PLT entry resides, or
  1141. the PLT index, depending on the architecture.
  1142. Return Value:
  1143. Returns a pointer to the function to jump to (in addition to writing that
  1144. address in the GOT at the appropriate spot).
  1145. --*/