elfc32.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. elfc32.c
  5. Abstract:
  6. This module implements support for converting an ELF32 image to a PE
  7. image.
  8. Author:
  9. Evan Green 7-Mar-2014
  10. Environment:
  11. Build
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <assert.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include "uefifw.h"
  22. #include "elfconv.h"
  23. //
  24. // --------------------------------------------------------------------- Macros
  25. //
  26. //
  27. // This macro aligns a value up to the COFF alignment.
  28. //
  29. #define ELFCONV_COFF_ALIGN(_Value) \
  30. (((_Value) + (ELFCONV_COFF_ALIGNMENT - 1)) & ~(ELFCONV_COFF_ALIGNMENT - 1))
  31. //
  32. // This macro returns a pointer to the base of the section header array given
  33. // a pointer to the ELF header.
  34. //
  35. #define ELFCONV_SECTION_BASE(_ElfHeader) \
  36. ((VOID *)(_ElfHeader) + (_ElfHeader)->e_shoff)
  37. //
  38. // This macro returns a pointer to the requested ELF section given the ELF
  39. // file header.
  40. //
  41. #define ELFCONV_ELF_SECTION(_ElfHeader, _SectionIndex) \
  42. ((VOID *)ELFCONV_SECTION_BASE(_ElfHeader) + \
  43. ((_SectionIndex) * (_ElfHeader)->e_shentsize))
  44. //
  45. // This macro returns a pointer to the base of the program header array given
  46. // a pointer to the ELF header.
  47. //
  48. #define ELFCONV_PROGRAM_HEADER_BASE(_ElfHeader) \
  49. ((VOID *)(_ElfHeader) + (_ElfHeader)->e_phoff)
  50. //
  51. // This macro returns a pointer to the requested ELF program header given the
  52. // ELF file header.
  53. //
  54. #define ELFCONV_ELF_PROGRAM_HEADER(_ElfHeader, _ProgramHeaderIndex) \
  55. ((VOID *)ELFCONV_PROGRAM_HEADER_BASE(_ElfHeader) + \
  56. ((_ProgramHeaderIndex) * (_ElfHeader)->e_phentsize))
  57. //
  58. // ---------------------------------------------------------------- Definitions
  59. //
  60. //
  61. // Define the number of sections assumed in the PE image.
  62. //
  63. #define ELFCONV_PE_SECTION_COUNT 16
  64. //
  65. // Define the alignment used throughout the COFF file.
  66. //
  67. #define ELFCONV_COFF_ALIGNMENT 0x20
  68. //
  69. // ------------------------------------------------------ Data Type Definitions
  70. //
  71. typedef
  72. BOOLEAN
  73. (*PELFCONV_SECTION_FILTER_FUNCTION) (
  74. Elf32_Ehdr *ElfHeader,
  75. Elf32_Shdr *SectionHeader
  76. );
  77. /*++
  78. Routine Description:
  79. This routine filters ELF sections based on certain criteria.
  80. Arguments:
  81. ElfHeader - Supplies a pointer to the ELF file header.
  82. SectionHeader - Supplies a pointer to the section header in question.
  83. Return Value:
  84. TRUE if the section matches the filter.
  85. FALSE if the section does not match the filter.
  86. --*/
  87. //
  88. // ----------------------------------------------- Internal Function Prototypes
  89. //
  90. BOOLEAN
  91. ElfconvScanSections32 (
  92. PELFCONV_CONTEXT Context
  93. );
  94. BOOLEAN
  95. ElfconvWriteSections32 (
  96. PELFCONV_CONTEXT Context,
  97. ELFCONV_SECTION_FILTER FilterType
  98. );
  99. BOOLEAN
  100. ElfconvWriteRelocations32 (
  101. PELFCONV_CONTEXT Context
  102. );
  103. BOOLEAN
  104. ElfconvWriteDebug32 (
  105. PELFCONV_CONTEXT Context
  106. );
  107. VOID
  108. ElfconvSetImageSize32 (
  109. PELFCONV_CONTEXT Context
  110. );
  111. VOID
  112. ElfconvCleanUp32 (
  113. PELFCONV_CONTEXT Context
  114. );
  115. BOOLEAN
  116. ElfconvIsTextSection (
  117. Elf32_Ehdr *ElfHeader,
  118. Elf32_Shdr *SectionHeader
  119. );
  120. BOOLEAN
  121. ElfconvIsDataSection (
  122. Elf32_Ehdr *ElfHeader,
  123. Elf32_Shdr *SectionHeader
  124. );
  125. BOOLEAN
  126. ElfconvIsHiiRsrcSection (
  127. Elf32_Ehdr *ElfHeader,
  128. Elf32_Shdr *SectionHeader
  129. );
  130. BOOLEAN
  131. ElfconvIsDebugSection (
  132. Elf32_Ehdr *ElfHeader,
  133. Elf32_Shdr *SectionHeader
  134. );
  135. VOID
  136. ElfconvThumbMovtImmediatePatch (
  137. UINT16 *Instruction,
  138. UINT16 Address
  139. );
  140. BOOLEAN
  141. ElfconvConvertElfAddress (
  142. PELFCONV_CONTEXT Context,
  143. UINT32 *Address
  144. );
  145. //
  146. // -------------------------------------------------------------------- Globals
  147. //
  148. //
  149. // ------------------------------------------------------------------ Functions
  150. //
  151. BOOLEAN
  152. ElfconvInitializeElf32 (
  153. PELFCONV_CONTEXT Context,
  154. PELFCONV_FUNCTION_TABLE FunctionTable
  155. )
  156. /*++
  157. Routine Description:
  158. This routine attempts to bind an ELF conversion context to an ELF32 image.
  159. Arguments:
  160. Context - Supplies a pointer to the ELF context.
  161. FunctionTable - Supplies a pointer to the ELF conversion functions on
  162. success.
  163. Return Value:
  164. TRUE on success.
  165. FALSE if the image is not a valid ELF32 image.
  166. --*/
  167. {
  168. Elf32_Ehdr *ElfHeader;
  169. BOOLEAN Result;
  170. Result = FALSE;
  171. ElfHeader = Context->InputFile;
  172. if (Context->InputFileSize < sizeof(Elf32_Ehdr)) {
  173. if ((ElfHeader->e_ident[EI_CLASS] != ELFCLASS32) ||
  174. (ElfHeader->e_ident[EI_DATA] != ELFDATA2LSB) ||
  175. ((ElfHeader->e_type != ET_EXEC) && (ElfHeader->e_type != ET_DYN)) ||
  176. ((ElfHeader->e_machine != EM_386) &&
  177. (ElfHeader->e_machine != EM_ARM)) ||
  178. (ElfHeader->e_version != EV_CURRENT)) {
  179. fprintf(stderr, "ELF Image not valid.\n");
  180. goto InitializeElf32End;
  181. }
  182. }
  183. Context->CoffSectionsOffset = malloc(ElfHeader->e_shnum * sizeof(UINT32));
  184. if (Context->CoffSectionsOffset == NULL) {
  185. goto InitializeElf32End;
  186. }
  187. memset(Context->CoffSectionsOffset, 0, ElfHeader->e_shnum * sizeof(UINT32));
  188. FunctionTable->ScanSections = ElfconvScanSections32;
  189. FunctionTable->WriteSections = ElfconvWriteSections32;
  190. FunctionTable->WriteRelocations = ElfconvWriteRelocations32;
  191. FunctionTable->WriteDebug = ElfconvWriteDebug32;
  192. FunctionTable->SetImageSize = ElfconvSetImageSize32;
  193. FunctionTable->CleanUp = ElfconvCleanUp32;
  194. Result = TRUE;
  195. InitializeElf32End:
  196. return Result;
  197. }
  198. //
  199. // --------------------------------------------------------- Internal Functions
  200. //
  201. BOOLEAN
  202. ElfconvScanSections32 (
  203. PELFCONV_CONTEXT Context
  204. )
  205. /*++
  206. Routine Description:
  207. This routine scans the ELF sections and sets up the PE image.
  208. Arguments:
  209. Context - Supplies a pointer to the ELF context.
  210. Return Value:
  211. TRUE on success.
  212. FALSE on failure.
  213. --*/
  214. {
  215. UINT32 CoffEntry;
  216. EFI_IMAGE_DOS_HEADER *DosHeader;
  217. Elf32_Ehdr *ElfHeader;
  218. Elf32_Shdr *ElfSection;
  219. UINT32 Flags;
  220. BOOLEAN FoundText;
  221. EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHeader;
  222. UINTN SectionIndex;
  223. CHAR8 *SectionName;
  224. Elf32_Shdr *StringSection;
  225. CoffEntry = 0;
  226. Context->CoffOffset = 0;
  227. Context->TextOffset = 0;
  228. Context->CoffOffset = sizeof(EFI_IMAGE_DOS_HEADER) + 0x40;
  229. Context->NtHeaderOffset = Context->CoffOffset;
  230. FoundText = FALSE;
  231. ElfHeader = Context->InputFile;
  232. assert((ElfHeader->e_machine == EM_386) ||
  233. (ElfHeader->e_machine == EM_ARM));
  234. Context->CoffOffset += sizeof(EFI_IMAGE_NT_HEADERS32);
  235. Context->TableOffset = Context->CoffOffset;
  236. Context->CoffOffset += ELFCONV_PE_SECTION_COUNT *
  237. sizeof(EFI_IMAGE_SECTION_HEADER);
  238. //
  239. // Find and wrangle any text sections.
  240. //
  241. Context->CoffOffset = ELFCONV_COFF_ALIGN(Context->CoffOffset);
  242. for (SectionIndex = 0;
  243. SectionIndex < ElfHeader->e_shnum;
  244. SectionIndex += 1) {
  245. ElfSection = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
  246. if (ElfconvIsTextSection(ElfHeader, ElfSection) != FALSE) {
  247. if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
  248. StringSection = ELFCONV_ELF_SECTION(ElfHeader,
  249. ElfHeader->e_shstrndx);
  250. SectionName = (CHAR8 *)ElfHeader + StringSection->sh_offset +
  251. ElfSection->sh_name;
  252. printf("Found text section %s: Offset 0x%x, size 0x%x.\n",
  253. SectionName,
  254. ElfSection->sh_offset,
  255. ElfSection->sh_size);
  256. }
  257. //
  258. // If the alignment field is valid, align the COFF offset up.
  259. //
  260. if ((ElfSection->sh_addralign != 0) &&
  261. (ElfSection->sh_addralign != 1)) {
  262. if ((ElfSection->sh_addr &
  263. (ElfSection->sh_addralign - 1)) == 0) {
  264. Context->CoffOffset = ALIGN_VALUE(Context->CoffOffset,
  265. ElfSection->sh_addralign);
  266. } else if ((ElfSection->sh_addr % ElfSection->sh_addralign) !=
  267. (Context->CoffOffset % ElfSection->sh_addralign)) {
  268. fprintf(stderr, "Error: Unsupported section alignment.\n");
  269. return FALSE;
  270. }
  271. }
  272. //
  273. // Relocate the entry point.
  274. //
  275. if ((ElfHeader->e_entry >= ElfSection->sh_addr) &&
  276. (ElfHeader->e_entry <
  277. ElfSection->sh_addr + ElfSection->sh_size)) {
  278. CoffEntry = Context->CoffOffset +
  279. ElfHeader->e_entry - ElfSection->sh_addr;
  280. }
  281. if (FoundText == FALSE) {
  282. Context->TextOffset = Context->CoffOffset;
  283. FoundText = TRUE;
  284. }
  285. Context->CoffSectionsOffset[SectionIndex] = Context->CoffOffset;
  286. Context->CoffOffset += ElfSection->sh_size;
  287. }
  288. }
  289. if (FoundText == FALSE) {
  290. fprintf(stderr, "Error: Failed to find a text section.\n");
  291. return FALSE;
  292. }
  293. if (ElfHeader->e_machine != EM_ARM) {
  294. Context->CoffOffset = ELFCONV_COFF_ALIGN(Context->CoffOffset);
  295. }
  296. //
  297. // Find and wrangle data sections.
  298. //
  299. Context->DataOffset = Context->CoffOffset;
  300. for (SectionIndex = 0;
  301. SectionIndex < ElfHeader->e_shnum;
  302. SectionIndex += 1) {
  303. ElfSection = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
  304. if (ElfconvIsDataSection(ElfHeader, ElfSection) != FALSE) {
  305. if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
  306. StringSection = ELFCONV_ELF_SECTION(ElfHeader,
  307. ElfHeader->e_shstrndx);
  308. SectionName = (CHAR8 *)ElfHeader + StringSection->sh_offset +
  309. ElfSection->sh_name;
  310. printf("Found data section %s: Offset 0x%x, size 0x%x.\n",
  311. SectionName,
  312. ElfSection->sh_offset,
  313. ElfSection->sh_size);
  314. }
  315. //
  316. // If the alignment field is valid, align the COFF offset up.
  317. //
  318. if ((ElfSection->sh_addralign != 0) &&
  319. (ElfSection->sh_addralign != 1)) {
  320. if ((ElfSection->sh_addr &
  321. (ElfSection->sh_addralign - 1)) == 0) {
  322. Context->CoffOffset = ALIGN_VALUE(Context->CoffOffset,
  323. ElfSection->sh_addralign);
  324. } else if ((ElfSection->sh_addr % ElfSection->sh_addralign) !=
  325. (Context->CoffOffset % ElfSection->sh_addralign)) {
  326. fprintf(stderr, "Error: Unsupported section alignment.\n");
  327. return FALSE;
  328. }
  329. }
  330. Context->CoffSectionsOffset[SectionIndex] = Context->CoffOffset;
  331. Context->CoffOffset += ElfSection->sh_size;
  332. }
  333. }
  334. Context->CoffOffset = ELFCONV_COFF_ALIGN(Context->CoffOffset);
  335. //
  336. // Find and wrangle HII .rsrc section.
  337. //
  338. Context->HiiRsrcOffset = Context->CoffOffset;
  339. for (SectionIndex = 0;
  340. SectionIndex < ElfHeader->e_shnum;
  341. SectionIndex += 1) {
  342. ElfSection = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
  343. if (ElfconvIsHiiRsrcSection(ElfHeader, ElfSection) != FALSE) {
  344. if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
  345. StringSection = ELFCONV_ELF_SECTION(ElfHeader,
  346. ElfHeader->e_shstrndx);
  347. SectionName = (CHAR8 *)ElfHeader + StringSection->sh_offset +
  348. ElfSection->sh_name;
  349. printf("Found rsrc section %s: Offset 0x%x, size 0x%x.\n",
  350. SectionName,
  351. ElfSection->sh_offset,
  352. ElfSection->sh_size);
  353. }
  354. //
  355. // If the alignment field is valid, align the COFF offset up.
  356. //
  357. if ((ElfSection->sh_addralign != 0) &&
  358. (ElfSection->sh_addralign != 1)) {
  359. if ((ElfSection->sh_addr &
  360. (ElfSection->sh_addralign - 1)) == 0) {
  361. Context->CoffOffset = ALIGN_VALUE(Context->CoffOffset,
  362. ElfSection->sh_addralign);
  363. } else if ((ElfSection->sh_addr % ElfSection->sh_addralign) !=
  364. (Context->CoffOffset % ElfSection->sh_addralign)) {
  365. fprintf(stderr, "Error: Unsupported section alignment.\n");
  366. return FALSE;
  367. }
  368. }
  369. if (ElfSection->sh_size != 0) {
  370. Context->CoffSectionsOffset[SectionIndex] = Context->CoffOffset;
  371. Context->CoffOffset += ElfSection->sh_size;
  372. Context->CoffOffset = ELFCONV_COFF_ALIGN(Context->CoffOffset);
  373. ElfconvSetHiiResourceHeader(
  374. (UINT8 *)ElfHeader + ElfSection->sh_offset,
  375. Context->HiiRsrcOffset);
  376. break;
  377. }
  378. }
  379. }
  380. Context->RelocationOffset = Context->CoffOffset;
  381. //
  382. // Allocate the base COFF file. This will be expanded later for relocations.
  383. //
  384. Context->CoffFile = malloc(Context->CoffOffset);
  385. if (Context->CoffFile == NULL) {
  386. return FALSE;
  387. }
  388. memset(Context->CoffFile, 0, Context->CoffOffset);
  389. //
  390. // Fill in the headers.
  391. //
  392. DosHeader = (EFI_IMAGE_DOS_HEADER *)(Context->CoffFile);
  393. DosHeader->e_magic = EFI_IMAGE_DOS_SIGNATURE;
  394. DosHeader->e_lfanew = Context->NtHeaderOffset;
  395. NtHeader = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(Context->CoffFile +
  396. Context->NtHeaderOffset);
  397. NtHeader->Pe32.Signature = EFI_IMAGE_NT_SIGNATURE;
  398. switch (ElfHeader->e_machine) {
  399. case EM_386:
  400. NtHeader->Pe32.FileHeader.Machine = EFI_IMAGE_MACHINE_IA32;
  401. break;
  402. case EM_ARM:
  403. NtHeader->Pe32.FileHeader.Machine = EFI_IMAGE_MACHINE_ARMTHUMB_MIXED;
  404. break;
  405. default:
  406. assert(FALSE);
  407. return FALSE;
  408. }
  409. NtHeader->Pe32.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;
  410. NtHeader->Pe32.FileHeader.NumberOfSections = ELFCONV_PE_SECTION_COUNT;
  411. NtHeader->Pe32.FileHeader.TimeDateStamp = (UINT32)time(NULL);
  412. Context->ImageTimestamp = NtHeader->Pe32.FileHeader.TimeDateStamp;
  413. NtHeader->Pe32.FileHeader.PointerToSymbolTable = 0;
  414. NtHeader->Pe32.FileHeader.NumberOfSymbols = 0;
  415. NtHeader->Pe32.FileHeader.SizeOfOptionalHeader =
  416. sizeof(NtHeader->Pe32.OptionalHeader);
  417. NtHeader->Pe32.FileHeader.Characteristics =
  418. EFI_IMAGE_FILE_EXECUTABLE_IMAGE |
  419. EFI_IMAGE_FILE_LINE_NUMS_STRIPPED |
  420. EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED |
  421. EFI_IMAGE_FILE_32BIT_MACHINE;
  422. NtHeader->Pe32.OptionalHeader.SizeOfCode = Context->DataOffset -
  423. Context->TextOffset;
  424. NtHeader->Pe32.OptionalHeader.SizeOfInitializedData =
  425. Context->RelocationOffset - Context->DataOffset;
  426. NtHeader->Pe32.OptionalHeader.SizeOfUninitializedData = 0;
  427. NtHeader->Pe32.OptionalHeader.AddressOfEntryPoint = CoffEntry;
  428. NtHeader->Pe32.OptionalHeader.BaseOfCode = Context->TextOffset;
  429. NtHeader->Pe32.OptionalHeader.BaseOfData = Context->DataOffset;
  430. NtHeader->Pe32.OptionalHeader.ImageBase = 0;
  431. NtHeader->Pe32.OptionalHeader.SectionAlignment = ELFCONV_COFF_ALIGNMENT;
  432. NtHeader->Pe32.OptionalHeader.FileAlignment = ELFCONV_COFF_ALIGNMENT;
  433. NtHeader->Pe32.OptionalHeader.SizeOfImage = 0;
  434. NtHeader->Pe32.OptionalHeader.SizeOfHeaders = Context->TextOffset;
  435. NtHeader->Pe32.OptionalHeader.Subsystem = Context->SubsystemType;
  436. NtHeader->Pe32.OptionalHeader.NumberOfRvaAndSizes =
  437. EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES;
  438. //
  439. // Create the section headers.
  440. //
  441. if (Context->DataOffset - Context->TextOffset > 0) {
  442. Flags = EFI_IMAGE_SCN_CNT_CODE |
  443. EFI_IMAGE_SCN_MEM_EXECUTE |
  444. EFI_IMAGE_SCN_MEM_READ;
  445. ElfconvCreateSectionHeader(Context,
  446. ".text",
  447. Context->TextOffset,
  448. Context->DataOffset - Context->TextOffset,
  449. Flags);
  450. } else {
  451. NtHeader->Pe32.FileHeader.NumberOfSections -= 1;
  452. }
  453. if (Context->HiiRsrcOffset - Context->DataOffset > 0) {
  454. Flags = EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
  455. EFI_IMAGE_SCN_MEM_WRITE |
  456. EFI_IMAGE_SCN_MEM_READ;
  457. ElfconvCreateSectionHeader(Context,
  458. ".data",
  459. Context->DataOffset,
  460. Context->HiiRsrcOffset - Context->DataOffset,
  461. Flags);
  462. } else {
  463. NtHeader->Pe32.FileHeader.NumberOfSections -= 1;
  464. }
  465. if (Context->RelocationOffset - Context->HiiRsrcOffset > 0) {
  466. Flags = EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
  467. EFI_IMAGE_SCN_MEM_READ;
  468. ElfconvCreateSectionHeader(
  469. Context,
  470. ".rsrc",
  471. Context->HiiRsrcOffset,
  472. Context->RelocationOffset - Context->HiiRsrcOffset,
  473. Flags);
  474. } else {
  475. NtHeader->Pe32.FileHeader.NumberOfSections -= 1;
  476. }
  477. return TRUE;
  478. }
  479. BOOLEAN
  480. ElfconvWriteSections32 (
  481. PELFCONV_CONTEXT Context,
  482. ELFCONV_SECTION_FILTER FilterType
  483. )
  484. /*++
  485. Routine Description:
  486. This routine writes certain sections to the output image.
  487. Arguments:
  488. Context - Supplies a pointer to the ELF context.
  489. FilterType - Supplies the type of sections to write.
  490. Return Value:
  491. TRUE on success.
  492. FALSE on failure.
  493. --*/
  494. {
  495. UINT16 Address;
  496. UINT32 AddressValue;
  497. VOID *Destination;
  498. Elf32_Ehdr *ElfHeader;
  499. Elf32_Shdr *ElfSection;
  500. PELFCONV_SECTION_FILTER_FUNCTION FilterFunction;
  501. Elf32_Rel *Relocation;
  502. UINT32 RelocationOffset;
  503. Elf32_Shdr *RelocationSection;
  504. UINT32 SectionIndex;
  505. UINT32 SectionOffset;
  506. Elf32_Sym *Symbol;
  507. Elf32_Shdr *SymbolSectionHeader;
  508. UINT8 *SymbolTable;
  509. Elf32_Shdr *SymbolTableSection;
  510. UINT8 *Target;
  511. ElfHeader = Context->InputFile;
  512. assert((ElfHeader->e_machine == EM_386) ||
  513. (ElfHeader->e_machine == EM_ARM));
  514. switch (FilterType) {
  515. case ElfconvSectionText:
  516. FilterFunction = ElfconvIsTextSection;
  517. break;
  518. case ElfconvSectionData:
  519. FilterFunction = ElfconvIsDataSection;
  520. break;
  521. case ElfconvSectionHii:
  522. FilterFunction = ElfconvIsHiiRsrcSection;
  523. break;
  524. default:
  525. assert(FALSE);
  526. return FALSE;
  527. }
  528. //
  529. // Copy the contents of the eligible sections.
  530. //
  531. for (SectionIndex = 0;
  532. SectionIndex < ElfHeader->e_shnum;
  533. SectionIndex += 1) {
  534. ElfSection = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
  535. if (FilterFunction(ElfHeader, ElfSection) != FALSE) {
  536. Destination = Context->CoffFile +
  537. Context->CoffSectionsOffset[SectionIndex];
  538. switch (ElfSection->sh_type) {
  539. case SHT_PROGBITS:
  540. case SHT_DYNAMIC:
  541. case SHT_DYNSYM:
  542. case SHT_REL:
  543. case SHT_RELA:
  544. if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
  545. printf("Copying section from ELF offset %x, size %x to "
  546. "COFF offset %x.\n",
  547. ElfSection->sh_offset,
  548. ElfSection->sh_size,
  549. (UINTN)Destination - (UINTN)Context->CoffFile);
  550. }
  551. memcpy(Destination,
  552. (UINT8 *)ElfHeader + ElfSection->sh_offset,
  553. ElfSection->sh_size);
  554. break;
  555. case SHT_NOBITS:
  556. if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
  557. printf("Zeroing COFF offset %x, size %x",
  558. (UINTN)Destination - (UINTN)Context->CoffFile,
  559. ElfSection->sh_size);
  560. }
  561. memset(Destination, 0, ElfSection->sh_size);
  562. break;
  563. case SHT_SYMTAB:
  564. case SHT_STRTAB:
  565. case SHT_HASH:
  566. break;
  567. default:
  568. fprintf(stderr,
  569. "Warning: Unknown section type %x.\n",
  570. ElfSection->sh_type);
  571. break;
  572. }
  573. }
  574. }
  575. //
  576. // Now apply relocations.
  577. //
  578. for (SectionIndex = 0;
  579. SectionIndex < ElfHeader->e_shnum;
  580. SectionIndex += 1) {
  581. RelocationSection = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
  582. //
  583. // Skip everything except relocation sections.
  584. //
  585. if ((RelocationSection->sh_type != SHT_REL) &&
  586. (RelocationSection->sh_type != SHT_RELA)) {
  587. continue;
  588. }
  589. //
  590. // This is a relocation section. Extract section information that the
  591. // relocations apply to.
  592. //
  593. ElfSection = ELFCONV_ELF_SECTION(ElfHeader, RelocationSection->sh_info);
  594. SectionOffset = Context->CoffSectionsOffset[SectionIndex];
  595. //
  596. // Skip the section if the filter function doesn't match or they're
  597. // addend relocations.
  598. //
  599. if ((RelocationSection->sh_type != SHT_REL) ||
  600. (FilterFunction(ElfHeader, ElfSection) == FALSE)) {
  601. continue;
  602. }
  603. //
  604. // Get the symbol table.
  605. //
  606. SymbolTableSection = ELFCONV_ELF_SECTION(ElfHeader,
  607. RelocationSection->sh_link);
  608. SymbolTable = (UINT8 *)ElfHeader + SymbolTableSection->sh_offset;
  609. //
  610. // Process all relocation entries for this section.
  611. //
  612. for (RelocationOffset = 0;
  613. RelocationOffset < RelocationSection->sh_size;
  614. RelocationOffset += RelocationSection->sh_entsize) {
  615. Relocation = (Elf32_Rel *)((UINT8 *)ElfHeader +
  616. RelocationSection->sh_offset +
  617. RelocationOffset);
  618. Symbol = (Elf32_Sym *)(SymbolTable +
  619. (ELF32_R_SYM(Relocation->r_info) *
  620. SymbolTableSection->sh_entsize));
  621. //
  622. // Skip absolute symbols.
  623. //
  624. if (Symbol->st_shndx == SHN_ABS) {
  625. continue;
  626. }
  627. if ((Symbol->st_shndx == SHN_UNDEF) ||
  628. (Symbol->st_shndx > ElfHeader->e_shnum)) {
  629. fprintf(stderr,
  630. "Error: Invalid symbol definition %x, "
  631. "relocation section %d, offset %x.\n",
  632. Symbol->st_shndx,
  633. SectionIndex,
  634. (UINTN)Relocation - (UINTN)ElfHeader);
  635. return FALSE;
  636. }
  637. SymbolSectionHeader = ELFCONV_ELF_SECTION(ElfHeader,
  638. Symbol->st_shndx);
  639. //
  640. // Convert the relocation data to a pointer into the COFF file.
  641. // r_offset is the virtual address of the storage unit to be
  642. // relocated. sh_addr is the virtual address for the base of the
  643. // section.
  644. //
  645. Target = Context->CoffFile + SectionOffset +
  646. (Relocation->r_offset - ElfSection->sh_addr);
  647. //
  648. // The relocation types are machine dependent.
  649. //
  650. if (ElfHeader->e_machine == EM_386) {
  651. switch (ELF32_R_TYPE(Relocation->r_info)) {
  652. case R_386_NONE:
  653. break;
  654. //
  655. // This is an absolute relocation. Convert the target from an
  656. // absolute virtuall address to an absolute COFF address.
  657. //
  658. case R_386_32:
  659. *(UINT32 *)Target =
  660. *(UINT32 *)Target -
  661. SymbolSectionHeader->sh_addr +
  662. Context->CoffSectionsOffset[Symbol->st_shndx];
  663. break;
  664. //
  665. // This is a relative relocation: Symbol - PC + Addend.
  666. //
  667. case R_386_PC32:
  668. *(UINT32 *)Target =
  669. *(UINT32 *)Target +
  670. (Context->CoffSectionsOffset[Symbol->st_shndx] -
  671. SymbolSectionHeader->sh_addr) -
  672. (SectionOffset - ElfSection->sh_addr);
  673. break;
  674. default:
  675. fprintf(stderr,
  676. "Error: Unsupported relocation type %d.\n",
  677. ELF32_R_TYPE(Relocation->r_info));
  678. return FALSE;
  679. }
  680. } else if (ElfHeader->e_machine == EM_ARM) {
  681. switch (ELF32_R_TYPE(Relocation->r_info)) {
  682. //
  683. // PC-relative relocations don't need modification.
  684. //
  685. case R_ARM_RBASE:
  686. case R_ARM_PC24:
  687. case R_ARM_REL32:
  688. case R_ARM_XPC25:
  689. case R_ARM_THM_PC22:
  690. case R_ARM_THM_JUMP19:
  691. case R_ARM_CALL:
  692. case R_ARM_JMP24:
  693. case R_ARM_THM_JUMP24:
  694. case R_ARM_PREL31:
  695. case R_ARM_MOVW_PREL_NC:
  696. case R_ARM_MOVT_PREL:
  697. case R_ARM_THM_MOVW_PREL_NC:
  698. case R_ARM_THM_MOVT_PREL:
  699. case R_ARM_THM_JMP6:
  700. case R_ARM_THM_ALU_PREL_11_0:
  701. case R_ARM_THM_PC12:
  702. case R_ARM_REL32_NOI:
  703. case R_ARM_ALU_PC_G0_NC:
  704. case R_ARM_ALU_PC_G0:
  705. case R_ARM_ALU_PC_G1_NC:
  706. case R_ARM_ALU_PC_G1:
  707. case R_ARM_ALU_PC_G2:
  708. case R_ARM_LDR_PC_G1:
  709. case R_ARM_LDR_PC_G2:
  710. case R_ARM_LDRS_PC_G0:
  711. case R_ARM_LDRS_PC_G1:
  712. case R_ARM_LDRS_PC_G2:
  713. case R_ARM_LDC_PC_G0:
  714. case R_ARM_LDC_PC_G1:
  715. case R_ARM_LDC_PC_G2:
  716. case R_ARM_GOT_PREL:
  717. case R_ARM_THM_JUMP11:
  718. case R_ARM_THM_JUMP8:
  719. case R_ARM_TLS_GD32:
  720. case R_ARM_TLS_LDM32:
  721. case R_ARM_TLS_IE32:
  722. case R_ARM_GOT_BREL:
  723. case R_ARM_BASE_PREL:
  724. break;
  725. //
  726. // MOVW relocates the lower 16 bits of the address.
  727. //
  728. case R_ARM_THM_MOVW_ABS_NC:
  729. Address =
  730. (UINT16)(Symbol->st_value -
  731. SymbolSectionHeader->sh_addr +
  732. Context->CoffSectionsOffset[Symbol->st_shndx]);
  733. ElfconvThumbMovtImmediatePatch((UINT16 *)Target, Address);
  734. break;
  735. //
  736. // MOVT relocates the upper 16 bits of the address.
  737. //
  738. case R_ARM_THM_MOVT_ABS:
  739. AddressValue =
  740. (Symbol->st_value -
  741. SymbolSectionHeader->sh_addr +
  742. Context->CoffSectionsOffset[Symbol->st_shndx]) >> 16;
  743. Address = (UINT16)AddressValue;
  744. ElfconvThumbMovtImmediatePatch((UINT16 *)Target, Address);
  745. break;
  746. //
  747. // This is a 32 bit absolute relocation.
  748. //
  749. case R_ARM_ABS32:
  750. case R_ARM_RABS32:
  751. *(UINT32 *)Target =
  752. *(UINT32 *)Target -
  753. SymbolSectionHeader->sh_addr +
  754. Context->CoffSectionsOffset[Symbol->st_shndx];
  755. break;
  756. default:
  757. fprintf(stderr,
  758. "Error: Unsupported relocation type %d.\n",
  759. ELF32_R_TYPE(Relocation->r_info));
  760. return FALSE;
  761. }
  762. //
  763. // The machine type is unknown. Execution should have failed before
  764. // this point.
  765. //
  766. } else {
  767. assert(FALSE);
  768. return FALSE;
  769. }
  770. }
  771. }
  772. return TRUE;
  773. }
  774. BOOLEAN
  775. ElfconvWriteRelocations32 (
  776. PELFCONV_CONTEXT Context
  777. )
  778. /*++
  779. Routine Description:
  780. This routine converts the ELF relocations into PE relocations and writes
  781. them into the output PE file buffer.
  782. Arguments:
  783. Context - Supplies a pointer to the ELF context.
  784. Return Value:
  785. TRUE on success.
  786. FALSE on failure.
  787. --*/
  788. {
  789. UINT32 DestinationSectionIndex;
  790. EFI_IMAGE_DATA_DIRECTORY *Directories;
  791. EFI_IMAGE_DATA_DIRECTORY *Directory;
  792. Elf32_Dyn *DynamicSection;
  793. Elf32_Phdr *DynamicSegment;
  794. Elf32_Ehdr *ElfHeader;
  795. UINT32 FixupOffset;
  796. UINT32 Flags;
  797. BOOLEAN FoundRelocations;
  798. UINTN MovwOffset;
  799. EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHeader;
  800. Elf32_Rel *Relocation;
  801. UINTN RelocationElementSize;
  802. UINT32 RelocationIndex;
  803. UINTN RelocationOffset;
  804. Elf32_Shdr *RelocationSectionHeader;
  805. UINTN RelocationSize;
  806. BOOLEAN Result;
  807. Elf32_Shdr *SectionHeader;
  808. UINT32 SectionIndex;
  809. UINT32 TargetAddress;
  810. UINT8 *TargetPointer;
  811. UINT32 TargetValue;
  812. ElfHeader = Context->InputFile;
  813. assert((ElfHeader->e_machine == EM_386) ||
  814. (ElfHeader->e_machine == EM_ARM));
  815. MovwOffset = 0;
  816. //
  817. // Loop across all sections looking for relocation sections.
  818. //
  819. FoundRelocations = FALSE;
  820. for (SectionIndex = 0;
  821. SectionIndex < ElfHeader->e_shnum;
  822. SectionIndex += 1) {
  823. RelocationSectionHeader = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
  824. //
  825. // Skip things that aren't relocation sections.
  826. //
  827. if ((RelocationSectionHeader->sh_type != SHT_REL) &&
  828. (RelocationSectionHeader->sh_type != SHT_RELA)) {
  829. continue;
  830. }
  831. SectionHeader = ELFCONV_ELF_SECTION(ElfHeader,
  832. RelocationSectionHeader->sh_info);
  833. if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
  834. printf("Found relocation section (index %d).\n", SectionIndex);
  835. }
  836. //
  837. // Skip sections that are neither text nor data.
  838. //
  839. if ((ElfconvIsTextSection(ElfHeader, SectionHeader) == FALSE) &&
  840. (ElfconvIsDataSection(ElfHeader, SectionHeader) == FALSE)) {
  841. continue;
  842. }
  843. FoundRelocations = TRUE;
  844. for (RelocationIndex = 0;
  845. RelocationIndex < RelocationSectionHeader->sh_size;
  846. RelocationIndex += RelocationSectionHeader->sh_entsize) {
  847. Relocation = (Elf32_Rel *)((UINT8 *)ElfHeader +
  848. RelocationSectionHeader->sh_offset +
  849. RelocationIndex);
  850. if (ElfHeader->e_machine == EM_386) {
  851. switch (ELF32_R_TYPE(Relocation->r_info)) {
  852. case R_386_NONE:
  853. case R_386_PC32:
  854. break;
  855. //
  856. // Create a relative relocation entry from the absolute entry.
  857. //
  858. case R_386_32:
  859. DestinationSectionIndex = RelocationSectionHeader->sh_info;
  860. FixupOffset =
  861. Context->CoffSectionsOffset[DestinationSectionIndex] +
  862. (Relocation->r_offset - SectionHeader->sh_addr);
  863. ElfconvCoffAddFixup(Context,
  864. FixupOffset,
  865. EFI_IMAGE_REL_BASED_HIGHLOW);
  866. break;
  867. default:
  868. fprintf(stderr,
  869. "Error: Unsupported relocation type %d.\n",
  870. ELF32_R_TYPE(Relocation->r_info));
  871. return FALSE;
  872. }
  873. } else if (ElfHeader->e_machine == EM_ARM) {
  874. switch (ELF32_R_TYPE(Relocation->r_info)) {
  875. //
  876. // PC-relative relocations don't need modification.
  877. //
  878. case R_ARM_RBASE:
  879. case R_ARM_PC24:
  880. case R_ARM_REL32:
  881. case R_ARM_XPC25:
  882. case R_ARM_THM_PC22:
  883. case R_ARM_THM_JUMP19:
  884. case R_ARM_CALL:
  885. case R_ARM_JMP24:
  886. case R_ARM_THM_JUMP24:
  887. case R_ARM_PREL31:
  888. case R_ARM_MOVW_PREL_NC:
  889. case R_ARM_MOVT_PREL:
  890. case R_ARM_THM_MOVW_PREL_NC:
  891. case R_ARM_THM_MOVT_PREL:
  892. case R_ARM_THM_JMP6:
  893. case R_ARM_THM_ALU_PREL_11_0:
  894. case R_ARM_THM_PC12:
  895. case R_ARM_REL32_NOI:
  896. case R_ARM_ALU_PC_G0_NC:
  897. case R_ARM_ALU_PC_G0:
  898. case R_ARM_ALU_PC_G1_NC:
  899. case R_ARM_ALU_PC_G1:
  900. case R_ARM_ALU_PC_G2:
  901. case R_ARM_LDR_PC_G1:
  902. case R_ARM_LDR_PC_G2:
  903. case R_ARM_LDRS_PC_G0:
  904. case R_ARM_LDRS_PC_G1:
  905. case R_ARM_LDRS_PC_G2:
  906. case R_ARM_LDC_PC_G0:
  907. case R_ARM_LDC_PC_G1:
  908. case R_ARM_LDC_PC_G2:
  909. case R_ARM_GOT_PREL:
  910. case R_ARM_THM_JUMP11:
  911. case R_ARM_THM_JUMP8:
  912. case R_ARM_TLS_GD32:
  913. case R_ARM_TLS_LDM32:
  914. case R_ARM_TLS_IE32:
  915. case R_ARM_GOT_BREL:
  916. case R_ARM_BASE_PREL:
  917. break;
  918. //
  919. // MOVW relocates the lower 16 bits of the address.
  920. //
  921. case R_ARM_THM_MOVW_ABS_NC:
  922. DestinationSectionIndex = RelocationSectionHeader->sh_info;
  923. FixupOffset =
  924. Context->CoffSectionsOffset[DestinationSectionIndex] +
  925. (Relocation->r_offset - SectionHeader->sh_addr);
  926. Result = ElfconvCoffAddFixup(
  927. Context,
  928. FixupOffset,
  929. EFI_IMAGE_REL_BASED_ARM_MOV32T);
  930. if (Result == FALSE) {
  931. return FALSE;
  932. }
  933. //
  934. // PE/COFF tracks MOVW/MOVT relocations as a single
  935. // 64-bit instruction. Track this address so that an error
  936. // can be logged if the next relocation is not a MOVT.
  937. //
  938. MovwOffset = FixupOffset;
  939. break;
  940. //
  941. // MOVT relocates the upper 16 bits of the address. This is
  942. // already handled by the MOVW fixup.
  943. //
  944. case R_ARM_THM_MOVT_ABS:
  945. DestinationSectionIndex = RelocationSectionHeader->sh_info;
  946. FixupOffset =
  947. Context->CoffSectionsOffset[DestinationSectionIndex] +
  948. (Relocation->r_offset - SectionHeader->sh_addr);
  949. if (FixupOffset != MovwOffset + 4) {
  950. fprintf(stderr,
  951. "Error: PE requires MOVW+MOVT instruction "
  952. "sequences together.\n");
  953. return FALSE;
  954. }
  955. break;
  956. //
  957. // This is a 32 bit absolute relocation.
  958. //
  959. case R_ARM_ABS32:
  960. case R_ARM_RABS32:
  961. DestinationSectionIndex = RelocationSectionHeader->sh_info;
  962. FixupOffset =
  963. Context->CoffSectionsOffset[DestinationSectionIndex] +
  964. (Relocation->r_offset - SectionHeader->sh_addr);
  965. Result = ElfconvCoffAddFixup(Context,
  966. FixupOffset,
  967. EFI_IMAGE_REL_BASED_HIGHLOW);
  968. if (Result == FALSE) {
  969. return FALSE;
  970. }
  971. break;
  972. default:
  973. fprintf(stderr,
  974. "Error: Unsupported ARM relocation type %d.\n",
  975. ELF32_R_TYPE(Relocation->r_info));
  976. return FALSE;
  977. }
  978. } else {
  979. assert(FALSE);
  980. return FALSE;
  981. }
  982. }
  983. }
  984. //
  985. // If relocations were not found, try again looking for PT_DYNAMIC instead
  986. // of SHT_REL.
  987. //
  988. if (FoundRelocations == FALSE) {
  989. for (SectionIndex = 0;
  990. SectionIndex < ElfHeader->e_phnum;
  991. SectionIndex += 1) {
  992. RelocationElementSize = 0;
  993. RelocationSize = 0;
  994. RelocationOffset = 0;
  995. DynamicSegment =
  996. ELFCONV_ELF_PROGRAM_HEADER(ElfHeader, SectionIndex);
  997. if (DynamicSegment->p_type != PT_DYNAMIC) {
  998. continue;
  999. }
  1000. if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
  1001. printf("Found dynamic section (index %d)\n", SectionIndex);
  1002. }
  1003. DynamicSection =
  1004. (Elf32_Dyn *)((UINT8 *)ElfHeader + DynamicSegment->p_offset);
  1005. while (DynamicSection->d_tag != DT_NULL) {
  1006. switch (DynamicSection->d_tag) {
  1007. case DT_REL:
  1008. RelocationOffset = DynamicSection->d_un.d_val;
  1009. if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
  1010. printf("Relocation offset %x.\n", RelocationOffset);
  1011. }
  1012. Result = ElfconvConvertElfAddress(Context,
  1013. &RelocationOffset);
  1014. if (Result == FALSE) {
  1015. fprintf(stderr,
  1016. "Error: Failed to convert dynamic relocation "
  1017. "address %x to destination image offset.\n",
  1018. RelocationOffset);
  1019. return FALSE;
  1020. }
  1021. if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
  1022. printf("Adjusted relocation offset %x.\n",
  1023. RelocationOffset);
  1024. }
  1025. break;
  1026. case DT_RELSZ:
  1027. RelocationSize = DynamicSection->d_un.d_val;
  1028. if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
  1029. printf("Relocation size %x.\n", RelocationSize);
  1030. }
  1031. break;
  1032. case DT_RELENT:
  1033. RelocationElementSize = DynamicSection->d_un.d_val;
  1034. if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
  1035. printf("Relocation element size %x.\n",
  1036. RelocationElementSize);
  1037. }
  1038. break;
  1039. default:
  1040. break;
  1041. }
  1042. DynamicSection += 1;
  1043. }
  1044. if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
  1045. printf("Relocations: PHDR %d (p_offset %x) Offset %x Size %x "
  1046. "ElemSize %x\n",
  1047. SectionIndex,
  1048. DynamicSegment->p_offset,
  1049. RelocationOffset,
  1050. RelocationSize,
  1051. RelocationElementSize);
  1052. }
  1053. if ((RelocationOffset == 0) || (RelocationSize == 0) ||
  1054. (RelocationElementSize == 0)) {
  1055. fprintf(stderr, "Error: Bad ARM relocations.\n");
  1056. return FALSE;
  1057. }
  1058. for (RelocationIndex = 0;
  1059. RelocationIndex < RelocationSize;
  1060. RelocationIndex += RelocationElementSize) {
  1061. Relocation = (Elf32_Rel *)(Context->CoffFile +
  1062. RelocationOffset +
  1063. RelocationIndex);
  1064. if (ElfHeader->e_machine == EM_386) {
  1065. switch (ELF32_R_TYPE(Relocation->r_info)) {
  1066. //
  1067. // Relative relocations contain a default VA in them. First
  1068. // convert that VA into a COFF offset, then create a
  1069. // relocation for it. If the value conversion failed, it's
  1070. // probably a relocation off in a stabs section, so ignore
  1071. // it.
  1072. //
  1073. case R_386_RELATIVE:
  1074. TargetAddress = Relocation->r_offset;
  1075. Result = ElfconvConvertElfAddress(Context,
  1076. &TargetAddress);
  1077. if (Result == FALSE) {
  1078. fprintf(stderr,
  1079. "R_386_RELATIVE target convert failed.\n");
  1080. return FALSE;
  1081. }
  1082. TargetPointer = Context->CoffFile + TargetAddress;
  1083. TargetValue = *(UINT32 *)TargetPointer;
  1084. Result = ElfconvConvertElfAddress(Context,
  1085. &TargetValue);
  1086. if (Result == FALSE) {
  1087. if ((Context->Flags &
  1088. ELFCONV_OPTION_VERBOSE) != 0) {
  1089. printf("Skipping relocation at address %x that "
  1090. "had value %x that could not be "
  1091. "converted in the destination.\n",
  1092. TargetAddress,
  1093. TargetValue);
  1094. }
  1095. break;
  1096. }
  1097. //
  1098. // Stick the old value (now an offset in the COFF image)
  1099. // into the relocation's spot.
  1100. //
  1101. *(UINT32 *)TargetPointer = TargetValue;
  1102. Result = ElfconvCoffAddFixup(
  1103. Context,
  1104. TargetAddress,
  1105. EFI_IMAGE_REL_BASED_HIGHLOW);
  1106. if (Result == FALSE) {
  1107. return FALSE;
  1108. }
  1109. break;
  1110. default:
  1111. fprintf(stderr,
  1112. "Bad 386 dynamic relocation type %d, offset "
  1113. "%x, program header index %d.\n",
  1114. ELF32_R_TYPE(Relocation->r_info),
  1115. (UINTN)Relocation - (UINTN)ElfHeader,
  1116. SectionIndex);
  1117. return FALSE;
  1118. }
  1119. } else if (ElfHeader->e_machine == EM_ARM) {
  1120. switch (ELF32_R_TYPE(Relocation->r_info)) {
  1121. case R_ARM_RBASE:
  1122. break;
  1123. //
  1124. // Relative relocations contain a default VA in them. First
  1125. // convert that VA into a COFF offset, then create a
  1126. // relocation for it. If the value conversion failed, it's
  1127. // probably a relocation off in a stabs section, so ignore
  1128. // it.
  1129. //
  1130. case R_ARM_RELATIVE:
  1131. TargetAddress = Relocation->r_offset;
  1132. Result = ElfconvConvertElfAddress(Context,
  1133. &TargetAddress);
  1134. if (Result == FALSE) {
  1135. fprintf(stderr,
  1136. "R_ARM_RELATIVE target convert failed.\n");
  1137. return FALSE;
  1138. }
  1139. TargetPointer = Context->CoffFile + TargetAddress;
  1140. TargetValue = *(UINT32 *)TargetPointer;
  1141. Result = ElfconvConvertElfAddress(Context,
  1142. &TargetValue);
  1143. if (Result == FALSE) {
  1144. if ((Context->Flags &
  1145. ELFCONV_OPTION_VERBOSE) != 0) {
  1146. printf("Skipping relocation at address %x that "
  1147. "had value %x that could not be "
  1148. "converted in the destination.\n",
  1149. TargetAddress,
  1150. TargetValue);
  1151. }
  1152. break;
  1153. }
  1154. //
  1155. // Stick the old value (now an offset in the COFF image)
  1156. // into the relocation's spot.
  1157. //
  1158. *(UINT32 *)TargetPointer = TargetValue;
  1159. Result = ElfconvCoffAddFixup(
  1160. Context,
  1161. TargetAddress,
  1162. EFI_IMAGE_REL_BASED_HIGHLOW);
  1163. if (Result == FALSE) {
  1164. return FALSE;
  1165. }
  1166. break;
  1167. default:
  1168. fprintf(stderr,
  1169. "Bad ARM dynamic relocation type %d, offset "
  1170. "%x, program header index %d.\n",
  1171. ELF32_R_TYPE(Relocation->r_info),
  1172. (UINTN)Relocation - (UINTN)ElfHeader,
  1173. SectionIndex);
  1174. return FALSE;
  1175. }
  1176. } else {
  1177. assert(FALSE);
  1178. return FALSE;
  1179. }
  1180. }
  1181. }
  1182. }
  1183. //
  1184. // Pad the page entry out by adding extra entries.
  1185. //
  1186. while ((Context->CoffOffset & (ELFCONV_COFF_ALIGNMENT - 1)) != 0) {
  1187. ElfconvCoffAddFixupEntry(Context, 0);
  1188. }
  1189. //
  1190. // Create the relocation section.
  1191. //
  1192. NtHeader = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(Context->CoffFile +
  1193. Context->NtHeaderOffset);
  1194. Directories = &(NtHeader->Pe32.OptionalHeader.DataDirectory[0]);
  1195. Directory = &(Directories[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]);
  1196. Directory->Size = Context->CoffOffset - Context->RelocationOffset;
  1197. if (Directory->Size == 0) {
  1198. Directory->VirtualAddress = 0;
  1199. NtHeader->Pe32.FileHeader.NumberOfSections -= 1;
  1200. } else {
  1201. Directory->VirtualAddress = Context->RelocationOffset;
  1202. Flags = EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
  1203. EFI_IMAGE_SCN_MEM_DISCARDABLE |
  1204. EFI_IMAGE_SCN_MEM_READ;
  1205. ElfconvCreateSectionHeader(
  1206. Context,
  1207. ".reloc",
  1208. Context->RelocationOffset,
  1209. Context->CoffOffset - Context->RelocationOffset,
  1210. Flags);
  1211. }
  1212. return TRUE;
  1213. }
  1214. BOOLEAN
  1215. ElfconvWriteDebug32 (
  1216. PELFCONV_CONTEXT Context
  1217. )
  1218. /*++
  1219. Routine Description:
  1220. This routine writes out the debug sections.
  1221. Arguments:
  1222. Context - Supplies a pointer to the ELF context.
  1223. Return Value:
  1224. TRUE on success.
  1225. FALSE on failure.
  1226. --*/
  1227. {
  1228. Elf32_Ehdr *ElfHeader;
  1229. Elf32_Shdr *ElfSection;
  1230. UINT32 Flags;
  1231. VOID *NewBuffer;
  1232. EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHeader;
  1233. UINT32 SectionCount;
  1234. UINTN SectionIndex;
  1235. CHAR8 *SectionName;
  1236. UINT32 SectionOffset;
  1237. Elf32_Shdr *StringSection;
  1238. ElfHeader = Context->InputFile;
  1239. assert((ElfHeader->e_machine == EM_386) ||
  1240. (ElfHeader->e_machine == EM_ARM));
  1241. //
  1242. // Find and wrangle debug sections.
  1243. //
  1244. Context->DataOffset = Context->CoffOffset;
  1245. SectionCount = 0;
  1246. for (SectionIndex = 0;
  1247. SectionIndex < ElfHeader->e_shnum;
  1248. SectionIndex += 1) {
  1249. ElfSection = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
  1250. if (ElfconvIsDebugSection(ElfHeader, ElfSection) == FALSE) {
  1251. continue;
  1252. }
  1253. SectionOffset = Context->CoffOffset;
  1254. Context->CoffSectionsOffset[SectionIndex] = SectionOffset;
  1255. Context->CoffOffset += ElfSection->sh_size;
  1256. NewBuffer = realloc(Context->CoffFile, Context->CoffOffset);
  1257. if (NewBuffer == NULL) {
  1258. return FALSE;
  1259. }
  1260. Context->CoffFile = NewBuffer;
  1261. //
  1262. // Create the section.
  1263. //
  1264. StringSection = ELFCONV_ELF_SECTION(ElfHeader, ElfHeader->e_shstrndx);
  1265. SectionName = (CHAR8 *)ElfHeader + StringSection->sh_offset +
  1266. ElfSection->sh_name;
  1267. Flags = EFI_IMAGE_SCN_MEM_READ | EFI_IMAGE_SCN_MEM_DISCARDABLE;
  1268. ElfconvCreateSectionHeader(Context,
  1269. SectionName,
  1270. SectionOffset,
  1271. ElfSection->sh_size,
  1272. Flags);
  1273. memcpy(Context->CoffFile + SectionOffset,
  1274. (UINT8 *)ElfHeader + ElfSection->sh_offset,
  1275. ElfSection->sh_size);
  1276. SectionCount += 1;
  1277. }
  1278. //
  1279. // Also write out the string table at this point.
  1280. //
  1281. if (Context->StringTable != NULL) {
  1282. NewBuffer = realloc(Context->CoffFile,
  1283. Context->CoffOffset + Context->StringTableSize);
  1284. if (NewBuffer == NULL) {
  1285. return FALSE;
  1286. }
  1287. Context->CoffFile = NewBuffer;
  1288. //
  1289. // Set the final size in the string table, then copy the string table
  1290. // to the image.
  1291. //
  1292. memcpy(Context->StringTable,
  1293. &(Context->StringTableSize),
  1294. sizeof(UINT32));
  1295. memcpy(Context->CoffFile + Context->CoffOffset,
  1296. Context->StringTable,
  1297. Context->StringTableSize);
  1298. NtHeader = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(Context->CoffFile +
  1299. Context->NtHeaderOffset);
  1300. NtHeader->Pe32.FileHeader.PointerToSymbolTable = Context->CoffOffset;
  1301. Context->CoffOffset += Context->StringTableSize;
  1302. }
  1303. return TRUE;
  1304. }
  1305. VOID
  1306. ElfconvSetImageSize32 (
  1307. PELFCONV_CONTEXT Context
  1308. )
  1309. /*++
  1310. Routine Description:
  1311. This routine sets the final image size.
  1312. Arguments:
  1313. Context - Supplies a pointer to the conversion context.
  1314. Return Value:
  1315. None.
  1316. --*/
  1317. {
  1318. EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHeader;
  1319. NtHeader = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(Context->CoffFile +
  1320. Context->NtHeaderOffset);
  1321. NtHeader->Pe32.OptionalHeader.SizeOfImage = Context->CoffOffset;
  1322. return;
  1323. }
  1324. VOID
  1325. ElfconvCleanUp32 (
  1326. PELFCONV_CONTEXT Context
  1327. )
  1328. /*++
  1329. Routine Description:
  1330. This routine performs final cleanup actions.
  1331. Arguments:
  1332. Context - Supplies a pointer to the conversion context.
  1333. Return Value:
  1334. None.
  1335. --*/
  1336. {
  1337. if (Context->CoffSectionsOffset != NULL) {
  1338. free(Context->CoffSectionsOffset);
  1339. Context->CoffSectionsOffset = NULL;
  1340. }
  1341. return;
  1342. }
  1343. BOOLEAN
  1344. ElfconvIsTextSection (
  1345. Elf32_Ehdr *ElfHeader,
  1346. Elf32_Shdr *SectionHeader
  1347. )
  1348. /*++
  1349. Routine Description:
  1350. This routine determines if the given section is a text section.
  1351. Arguments:
  1352. ElfHeader - Supplies a pointer to the ELF file header.
  1353. SectionHeader - Supplies a pointer to the section header in question.
  1354. Return Value:
  1355. TRUE if it is a text section.
  1356. FALSE if it is not a text section.
  1357. --*/
  1358. {
  1359. if ((SectionHeader->sh_type != SHT_PROGBITS) &&
  1360. (SectionHeader->sh_type != SHT_REL)) {
  1361. return FALSE;
  1362. }
  1363. if ((SectionHeader->sh_flags & (SHF_WRITE | SHF_ALLOC)) == SHF_ALLOC) {
  1364. return TRUE;
  1365. }
  1366. return FALSE;
  1367. }
  1368. BOOLEAN
  1369. ElfconvIsDataSection (
  1370. Elf32_Ehdr *ElfHeader,
  1371. Elf32_Shdr *SectionHeader
  1372. )
  1373. /*++
  1374. Routine Description:
  1375. This routine determines if the given section is a data section.
  1376. Arguments:
  1377. ElfHeader - Supplies a pointer to the ELF file header.
  1378. SectionHeader - Supplies a pointer to the section header in question.
  1379. Return Value:
  1380. TRUE if it is a data section.
  1381. FALSE if it is not a data section.
  1382. --*/
  1383. {
  1384. if (ElfconvIsHiiRsrcSection(ElfHeader, SectionHeader) != FALSE) {
  1385. return FALSE;
  1386. }
  1387. if ((SectionHeader->sh_type != SHT_PROGBITS) &&
  1388. (SectionHeader->sh_type != SHT_NOBITS) &&
  1389. (SectionHeader->sh_type != SHT_DYNAMIC)) {
  1390. return FALSE;
  1391. }
  1392. if ((SectionHeader->sh_flags & (SHF_WRITE | SHF_ALLOC)) ==
  1393. (SHF_WRITE | SHF_ALLOC)) {
  1394. return TRUE;
  1395. }
  1396. return FALSE;
  1397. }
  1398. BOOLEAN
  1399. ElfconvIsHiiRsrcSection (
  1400. Elf32_Ehdr *ElfHeader,
  1401. Elf32_Shdr *SectionHeader
  1402. )
  1403. /*++
  1404. Routine Description:
  1405. This routine determines if the given section is a HII .rsrc section.
  1406. Arguments:
  1407. ElfHeader - Supplies a pointer to the ELF file header.
  1408. SectionHeader - Supplies a pointer to the section header in question.
  1409. Return Value:
  1410. TRUE if it is a HII rsrc section.
  1411. FALSE if it is not the HII rsrc section.
  1412. --*/
  1413. {
  1414. CHAR8 *SectionName;
  1415. Elf32_Shdr *StringSection;
  1416. StringSection = ELFCONV_ELF_SECTION(ElfHeader, ElfHeader->e_shstrndx);
  1417. SectionName = (CHAR8 *)ElfHeader + StringSection->sh_offset +
  1418. SectionHeader->sh_name;
  1419. if (strcmp(SectionName, ELFCONV_HII_SECTION_NAME) == 0) {
  1420. return TRUE;
  1421. }
  1422. return FALSE;
  1423. }
  1424. BOOLEAN
  1425. ElfconvIsDebugSection (
  1426. Elf32_Ehdr *ElfHeader,
  1427. Elf32_Shdr *SectionHeader
  1428. )
  1429. /*++
  1430. Routine Description:
  1431. This routine determines if the given section is a stabs or stabs string
  1432. section.
  1433. Arguments:
  1434. ElfHeader - Supplies a pointer to the ELF file header.
  1435. SectionHeader - Supplies a pointer to the section header in question.
  1436. Return Value:
  1437. TRUE if it is a HII rsrc section.
  1438. FALSE if it is not the HII rsrc section.
  1439. --*/
  1440. {
  1441. CHAR8 **DebugSection;
  1442. CHAR8 *SectionName;
  1443. Elf32_Shdr *StringSection;
  1444. StringSection = ELFCONV_ELF_SECTION(ElfHeader, ElfHeader->e_shstrndx);
  1445. SectionName = (CHAR8 *)ElfHeader + StringSection->sh_offset +
  1446. SectionHeader->sh_name;
  1447. DebugSection = ElfconvDebugSections;
  1448. while (*DebugSection != NULL) {
  1449. if (strcmp(SectionName, *DebugSection) == 0) {
  1450. return TRUE;
  1451. }
  1452. DebugSection += 1;
  1453. }
  1454. return FALSE;
  1455. }
  1456. VOID
  1457. ElfconvThumbMovtImmediatePatch (
  1458. UINT16 *Instruction,
  1459. UINT16 Address
  1460. )
  1461. /*++
  1462. Routine Description:
  1463. This routine updates an ARM MOVT or MOVW immediate instruction.
  1464. Arguments:
  1465. Instruction - Supplies a pointer to the ARM MOVT or MOVW immediate
  1466. instruction.
  1467. Address - Supplies the new address to patch into the instruction.
  1468. Return Value:
  1469. None.
  1470. --*/
  1471. {
  1472. UINT16 Patch;
  1473. //
  1474. // Patch the first 16-bit chunk of the instruction.
  1475. //
  1476. Patch = (Address >> 12) & 0x000F;
  1477. if ((Address & (1 << 11)) != 0) {
  1478. Patch |= 1 << 10;
  1479. }
  1480. *Instruction = (*Instruction & ~0x040F) | Patch;
  1481. //
  1482. // Patch the second 16-bit chunk of the instruction.
  1483. //
  1484. Patch = Address & 0x000000FF;
  1485. Patch |= (Address << 4) & 0x00007000;
  1486. Instruction += 1;
  1487. *Instruction = (*Instruction & 0x70FF) | Patch;
  1488. return;
  1489. }
  1490. BOOLEAN
  1491. ElfconvConvertElfAddress (
  1492. PELFCONV_CONTEXT Context,
  1493. UINT32 *Address
  1494. )
  1495. /*++
  1496. Routine Description:
  1497. This routine converts a memory address in the ELF image into an offset
  1498. within the COFF file.
  1499. Arguments:
  1500. Context - Supplies a pointer to the conversion context.
  1501. Address - Supplies a pointer that on input contains the address to convert.
  1502. On output, this will contain the COFF offset.
  1503. Return Value:
  1504. TRUE on success.
  1505. FALSE if the address is not valid.
  1506. --*/
  1507. {
  1508. Elf32_Ehdr *ElfHeader;
  1509. Elf32_Shdr *SectionHeader;
  1510. UINT32 SectionIndex;
  1511. ElfHeader = Context->InputFile;
  1512. //
  1513. // Loop through all the sections looking for the one that contains this
  1514. // address.
  1515. //
  1516. for (SectionIndex = 0;
  1517. SectionIndex < ElfHeader->e_shnum;
  1518. SectionIndex += 1) {
  1519. SectionHeader = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
  1520. //
  1521. // If the section header contains the address, then return the
  1522. // equivalent offset in the COFF section.
  1523. //
  1524. if ((SectionHeader->sh_addr <= *Address) &&
  1525. (SectionHeader->sh_addr + SectionHeader->sh_size > *Address)) {
  1526. if (Context->CoffSectionsOffset[SectionIndex] == 0) {
  1527. return FALSE;
  1528. }
  1529. *Address = *Address - SectionHeader->sh_addr +
  1530. Context->CoffSectionsOffset[SectionIndex];
  1531. return TRUE;
  1532. }
  1533. }
  1534. return FALSE;
  1535. }