elfconv.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. elfconv.h
  5. Abstract:
  6. This header contains internal definitions for the ELF conversion program.
  7. Author:
  8. Evan Green 7-Mar-2014
  9. --*/
  10. //
  11. // ------------------------------------------------------------------- Includes
  12. //
  13. #include "peimage.h"
  14. #include "elfimage.h"
  15. //
  16. // ---------------------------------------------------------------- Definitions
  17. //
  18. //
  19. // Set this flag to print additional information.
  20. //
  21. #define ELFCONV_OPTION_VERBOSE 0x00000001
  22. //
  23. // Define the name of the HII .rsrc section.
  24. //
  25. #define ELFCONV_HII_SECTION_NAME ".hii"
  26. //
  27. // ------------------------------------------------------ Data Type Definitions
  28. //
  29. typedef enum _ELFCONV_SECTION_FILTER {
  30. ElfconvSectionInvalid,
  31. ElfconvSectionText,
  32. ElfconvSectionData,
  33. ElfconvSectionHii,
  34. ElfconvSectionStabs,
  35. } ELFCONV_SECTION_FILTER, *PELFCONV_SECTION_FILTER;
  36. /*++
  37. Structure Description:
  38. This structure defines the application context information for the ElfConv
  39. utility.
  40. Members:
  41. Flags - Stores a bitfield of flags. See ELFCONV_OPTION_* definition.
  42. OutputName - Stores the name of the output image.
  43. InputName - Stores the name of the input image.
  44. SubsystemType - Stores the desired subsystem type.
  45. CoffSectionsOffset - Stores the COFF section offset buffer.
  46. CoffOffset - Stores the current offset in the COFF file.
  47. NtHeaderOffset - Stores the offset of the NT header in the COFF file.
  48. TableOffset - Stores the offset of the section table in the COFF file.
  49. TextOffset - Stores the offset of the text section in the COFF file.
  50. DataOffset - Stores the offset of the data section in the COFF file.
  51. HiiRsrcOffset - Stores the offset of the .rsrc HII section in the COFF file.
  52. RelocationOffset - Stores the offset of the relocation information in the
  53. COFF file.
  54. CoffFile - Store the COFF output file buffer.
  55. Timestamp - Stores the image timestamp.
  56. CoffBaseRelocation - Stores a pointer to the first relocation entry for the
  57. current relocation page.
  58. CoffNextRelocation - Stores a pointer to the next available relocation
  59. entry.
  60. StringTable - Stores a pointer to the string table, needed for sections
  61. that are larger than 8 characters.
  62. StringTableSize - Stores the size of the string table in bytes.
  63. --*/
  64. typedef struct _ELFCONV_CONTEXT {
  65. UINT32 Flags;
  66. CHAR8 *OutputName;
  67. CHAR8 *InputName;
  68. CHAR8 SubsystemType;
  69. VOID *InputFile;
  70. UINT32 InputFileSize;
  71. UINT32 *CoffSectionsOffset;
  72. UINT32 CoffOffset;
  73. UINT32 NtHeaderOffset;
  74. UINT32 TableOffset;
  75. UINT32 TextOffset;
  76. UINT32 DataOffset;
  77. UINT32 HiiRsrcOffset;
  78. UINT32 RelocationOffset;
  79. UINT8 *CoffFile;
  80. UINT32 ImageTimestamp;
  81. EFI_IMAGE_BASE_RELOCATION *CoffBaseRelocation;
  82. UINT16 *CoffNextRelocation;
  83. CHAR8 *StringTable;
  84. UINT32 StringTableSize;
  85. } ELFCONV_CONTEXT, *PELFCONV_CONTEXT;
  86. typedef
  87. BOOLEAN
  88. (*PELFCONV_SCAN_SECTIONS) (
  89. PELFCONV_CONTEXT Context
  90. );
  91. /*++
  92. Routine Description:
  93. This routine scans the ELF sections in preparation for conversion.
  94. Arguments:
  95. Context - Supplies a pointer to the conversion context.
  96. Return Value:
  97. TRUE on success.
  98. FALSE on failure.
  99. --*/
  100. typedef
  101. BOOLEAN
  102. (*PELFCONV_WRITE_SECTIONS) (
  103. PELFCONV_CONTEXT Context,
  104. ELFCONV_SECTION_FILTER FilterType
  105. );
  106. /*++
  107. Routine Description:
  108. This routine writes ELF sections to the PE image buffer.
  109. Arguments:
  110. Context - Supplies a pointer to the conversion context.
  111. FilterType - Supplies the section filter to apply.
  112. Return Value:
  113. TRUE on success.
  114. FALSE on failure.
  115. --*/
  116. typedef
  117. BOOLEAN
  118. (*PELFCONV_WRITE_RELOCATIONS) (
  119. PELFCONV_CONTEXT Context
  120. );
  121. /*++
  122. Routine Description:
  123. This routine writes the relocation section for the new PE image.
  124. Arguments:
  125. Context - Supplies a pointer to the conversion context.
  126. Return Value:
  127. TRUE on success.
  128. FALSE on failure.
  129. --*/
  130. typedef
  131. BOOLEAN
  132. (*PELFCONV_WRITE_DEBUG) (
  133. PELFCONV_CONTEXT Context
  134. );
  135. /*++
  136. Routine Description:
  137. This routine writes debug sections into the new PE image.
  138. Arguments:
  139. Context - Supplies a pointer to the conversion context.
  140. Return Value:
  141. TRUE on success.
  142. FALSE on failure.
  143. --*/
  144. typedef
  145. VOID
  146. (*PELFCONV_SET_IMAGE_SIZE) (
  147. PELFCONV_CONTEXT Context
  148. );
  149. /*++
  150. Routine Description:
  151. This routine sets the final PE image size.
  152. Arguments:
  153. Context - Supplies a pointer to the conversion context.
  154. Return Value:
  155. None.
  156. --*/
  157. typedef
  158. VOID
  159. (*PELFCONV_CLEAN_UP) (
  160. PELFCONV_CONTEXT Context
  161. );
  162. /*++
  163. Routine Description:
  164. This routine finalizes the new PE image and performs clean-up actions.
  165. Arguments:
  166. Context - Supplies a pointer to the conversion context.
  167. Return Value:
  168. None.
  169. --*/
  170. /*++
  171. Structure Description:
  172. This structure defines the function table for converting an ELF image to
  173. a PE image.
  174. Members:
  175. ScanSections - Stores a pointer to a function used to scan the ELF image
  176. sections.
  177. WriteSections - Stores a pointer to a function used to write the PE
  178. image sections.
  179. WriteRelocations - Stores a pointer to a function used to write the
  180. relocation information into the PE image.
  181. WriteDebug - Stores a pointer to a function used to write debug information
  182. into the PE image.
  183. SetImageSize - Stores a pointer to a function used to set the final image
  184. size.
  185. CleanUp - Stores a pointer to a function called to clean up the
  186. conversion state and finalize the PE image.
  187. --*/
  188. typedef struct _ELFCONV_FUNCTION_TABLE {
  189. PELFCONV_SCAN_SECTIONS ScanSections;
  190. PELFCONV_WRITE_SECTIONS WriteSections;
  191. PELFCONV_WRITE_RELOCATIONS WriteRelocations;
  192. PELFCONV_WRITE_DEBUG WriteDebug;
  193. PELFCONV_SET_IMAGE_SIZE SetImageSize;
  194. PELFCONV_CLEAN_UP CleanUp;
  195. } ELFCONV_FUNCTION_TABLE, *PELFCONV_FUNCTION_TABLE;
  196. //
  197. // -------------------------------------------------------------------- Globals
  198. //
  199. extern CHAR8 *ElfconvDebugSections[];
  200. //
  201. // -------------------------------------------------------- Function Prototypes
  202. //
  203. //
  204. // Common ELF functions
  205. //
  206. VOID
  207. ElfconvSetHiiResourceHeader (
  208. UINT8 *HiiBinData,
  209. UINT32 OffsetToFile
  210. );
  211. /*++
  212. Routine Description:
  213. This routine sets up the HII resource data in the destination image.
  214. Arguments:
  215. HiiBinData - Supplies a pointer to the raw resource data.
  216. OffsetToFile - Supplies the offset within the file where the data is going
  217. to reside.
  218. Return Value:
  219. None.
  220. --*/
  221. VOID
  222. ElfconvCreateSectionHeader (
  223. PELFCONV_CONTEXT Context,
  224. CHAR8 *Name,
  225. UINT32 Offset,
  226. UINT32 Size,
  227. UINT32 Flags
  228. );
  229. /*++
  230. Routine Description:
  231. This routine initializes a PE section header in the output file buffer.
  232. Arguments:
  233. Context - Supplies a pointer to the conversion context.
  234. Name - Supplies a pointer to a string containing the name of the section.
  235. Offset - Supplies the file offset to the start of the section.
  236. Size - Supplies the size of the section in bytes.
  237. Flags - Supplies the section flags.
  238. Return Value:
  239. None.
  240. --*/
  241. BOOLEAN
  242. ElfconvCoffAddFixup (
  243. PELFCONV_CONTEXT Context,
  244. UINT32 Offset,
  245. UINT8 Type
  246. );
  247. /*++
  248. Routine Description:
  249. This routine adds a COFF relocation to the destination image buffer.
  250. Arguments:
  251. Context - Supplies a pointer to the conversion context.
  252. Offset - Supplies the file offset to the relocation.
  253. Type - Supplies the relocation type.
  254. Return Value:
  255. TRUE on success.
  256. FALSE on allocation failure.
  257. --*/
  258. VOID
  259. ElfconvCoffAddFixupEntry (
  260. PELFCONV_CONTEXT Context,
  261. UINT16 Value
  262. );
  263. /*++
  264. Routine Description:
  265. This routine adds a relocation entry to the current COFF location.
  266. Arguments:
  267. Context - Supplies a pointer to the application context.
  268. Value - Supplies the relocation value to add.
  269. Return Value:
  270. None.
  271. --*/
  272. //
  273. // 32-bit ELF functions
  274. //
  275. BOOLEAN
  276. ElfconvInitializeElf32 (
  277. PELFCONV_CONTEXT Context,
  278. PELFCONV_FUNCTION_TABLE FunctionTable
  279. );
  280. /*++
  281. Routine Description:
  282. This routine attempts to bind an ELF conversion context to an ELF32 image.
  283. Arguments:
  284. Context - Supplies a pointer to the ELF context.
  285. FunctionTable - Supplies a pointer to the ELF conversion functions on
  286. success.
  287. Return Value:
  288. TRUE on success.
  289. FALSE if the image is not a valid ELF32 image.
  290. --*/