Elf32Convert.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. /** @file
  2. Elf32 Convert solution
  3. Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
  4. Portions copyright (c) 2013, ARM Ltd. All rights reserved.<BR>
  5. This program and the accompanying materials are licensed and made available
  6. under the terms and conditions of the BSD License which accompanies this
  7. distribution. The full text of the license may be found at
  8. http://opensource.org/licenses/bsd-license.php
  9. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  11. **/
  12. #include "WinNtInclude.h"
  13. #ifndef __GNUC__
  14. #include <windows.h>
  15. #include <io.h>
  16. #endif
  17. #include <assert.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include <ctype.h>
  23. #include <Common/UefiBaseTypes.h>
  24. #include <IndustryStandard/PeImage.h>
  25. #include "PeCoffLib.h"
  26. #include "EfiUtilityMsgs.h"
  27. #include "GenFw.h"
  28. #include "ElfConvert.h"
  29. #include "Elf32Convert.h"
  30. STATIC
  31. VOID
  32. ScanSections32 (
  33. VOID
  34. );
  35. STATIC
  36. BOOLEAN
  37. WriteSections32 (
  38. SECTION_FILTER_TYPES FilterType
  39. );
  40. STATIC
  41. VOID
  42. WriteRelocations32 (
  43. VOID
  44. );
  45. STATIC
  46. VOID
  47. WriteDebug32 (
  48. VOID
  49. );
  50. STATIC
  51. VOID
  52. SetImageSize32 (
  53. VOID
  54. );
  55. STATIC
  56. VOID
  57. CleanUp32 (
  58. VOID
  59. );
  60. //
  61. // Rename ELF32 structures to common names to help when porting to ELF64.
  62. //
  63. typedef Elf32_Shdr Elf_Shdr;
  64. typedef Elf32_Ehdr Elf_Ehdr;
  65. typedef Elf32_Rel Elf_Rel;
  66. typedef Elf32_Sym Elf_Sym;
  67. typedef Elf32_Phdr Elf_Phdr;
  68. typedef Elf32_Dyn Elf_Dyn;
  69. #define ELFCLASS ELFCLASS32
  70. #define ELF_R_TYPE(r) ELF32_R_TYPE(r)
  71. #define ELF_R_SYM(r) ELF32_R_SYM(r)
  72. //
  73. // Well known ELF structures.
  74. //
  75. STATIC Elf_Ehdr *mEhdr;
  76. STATIC Elf_Shdr *mShdrBase;
  77. STATIC Elf_Phdr *mPhdrBase;
  78. //
  79. // Coff information
  80. //
  81. STATIC UINT32 mCoffAlignment = 0x20;
  82. //
  83. // PE section alignment.
  84. //
  85. STATIC const UINT16 mCoffNbrSections = 4;
  86. //
  87. // ELF sections to offset in Coff file.
  88. //
  89. STATIC UINT32 *mCoffSectionsOffset = NULL;
  90. //
  91. // Offsets in COFF file
  92. //
  93. STATIC UINT32 mNtHdrOffset;
  94. STATIC UINT32 mTextOffset;
  95. STATIC UINT32 mDataOffset;
  96. STATIC UINT32 mHiiRsrcOffset;
  97. STATIC UINT32 mRelocOffset;
  98. STATIC UINT32 mDebugOffset;
  99. //
  100. // Initialization Function
  101. //
  102. BOOLEAN
  103. InitializeElf32 (
  104. UINT8 *FileBuffer,
  105. ELF_FUNCTION_TABLE *ElfFunctions
  106. )
  107. {
  108. //
  109. // Initialize data pointer and structures.
  110. //
  111. mEhdr = (Elf_Ehdr*) FileBuffer;
  112. //
  113. // Check the ELF32 specific header information.
  114. //
  115. if (mEhdr->e_ident[EI_CLASS] != ELFCLASS32) {
  116. Error (NULL, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS32");
  117. return FALSE;
  118. }
  119. if (mEhdr->e_ident[EI_DATA] != ELFDATA2LSB) {
  120. Error (NULL, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
  121. return FALSE;
  122. }
  123. if ((mEhdr->e_type != ET_EXEC) && (mEhdr->e_type != ET_DYN)) {
  124. Error (NULL, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
  125. return FALSE;
  126. }
  127. if (!((mEhdr->e_machine == EM_386) || (mEhdr->e_machine == EM_ARM))) {
  128. Error (NULL, 0, 3000, "Unsupported", "ELF e_machine not EM_386 or EM_ARM");
  129. return FALSE;
  130. }
  131. if (mEhdr->e_version != EV_CURRENT) {
  132. Error (NULL, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr->e_version, EV_CURRENT);
  133. return FALSE;
  134. }
  135. //
  136. // Update section header pointers
  137. //
  138. mShdrBase = (Elf_Shdr *)((UINT8 *)mEhdr + mEhdr->e_shoff);
  139. mPhdrBase = (Elf_Phdr *)((UINT8 *)mEhdr + mEhdr->e_phoff);
  140. //
  141. // Create COFF Section offset buffer and zero.
  142. //
  143. mCoffSectionsOffset = (UINT32 *)malloc(mEhdr->e_shnum * sizeof (UINT32));
  144. if (mCoffSectionsOffset == NULL) {
  145. Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
  146. return FALSE;
  147. }
  148. memset(mCoffSectionsOffset, 0, mEhdr->e_shnum * sizeof(UINT32));
  149. //
  150. // Fill in function pointers.
  151. //
  152. ElfFunctions->ScanSections = ScanSections32;
  153. ElfFunctions->WriteSections = WriteSections32;
  154. ElfFunctions->WriteRelocations = WriteRelocations32;
  155. ElfFunctions->WriteDebug = WriteDebug32;
  156. ElfFunctions->SetImageSize = SetImageSize32;
  157. ElfFunctions->CleanUp = CleanUp32;
  158. return TRUE;
  159. }
  160. //
  161. // Header by Index functions
  162. //
  163. STATIC
  164. Elf_Shdr*
  165. GetShdrByIndex (
  166. UINT32 Num
  167. )
  168. {
  169. if (Num >= mEhdr->e_shnum) {
  170. Error (NULL, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num);
  171. exit(EXIT_FAILURE);
  172. }
  173. return (Elf_Shdr*)((UINT8*)mShdrBase + Num * mEhdr->e_shentsize);
  174. }
  175. STATIC
  176. Elf_Phdr*
  177. GetPhdrByIndex (
  178. UINT32 num
  179. )
  180. {
  181. if (num >= mEhdr->e_phnum) {
  182. Error (NULL, 0, 3000, "Invalid", "GetPhdrByIndex: Index %u is too high.", num);
  183. exit(EXIT_FAILURE);
  184. }
  185. return (Elf_Phdr *)((UINT8*)mPhdrBase + num * mEhdr->e_phentsize);
  186. }
  187. STATIC
  188. UINT32
  189. CoffAlign (
  190. UINT32 Offset
  191. )
  192. {
  193. return (Offset + mCoffAlignment - 1) & ~(mCoffAlignment - 1);
  194. }
  195. STATIC
  196. UINT32
  197. DebugRvaAlign (
  198. UINT32 Offset
  199. )
  200. {
  201. return (Offset + 3) & ~3;
  202. }
  203. //
  204. // filter functions
  205. //
  206. STATIC
  207. BOOLEAN
  208. IsTextShdr (
  209. Elf_Shdr *Shdr
  210. )
  211. {
  212. return (BOOLEAN) ((Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == SHF_ALLOC);
  213. }
  214. STATIC
  215. BOOLEAN
  216. IsHiiRsrcShdr (
  217. Elf_Shdr *Shdr
  218. )
  219. {
  220. Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx);
  221. return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_HII_SECTION_NAME) == 0);
  222. }
  223. STATIC
  224. BOOLEAN
  225. IsDataShdr (
  226. Elf_Shdr *Shdr
  227. )
  228. {
  229. if (IsHiiRsrcShdr(Shdr)) {
  230. return FALSE;
  231. }
  232. return (BOOLEAN) (Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == (SHF_ALLOC | SHF_WRITE);
  233. }
  234. STATIC
  235. BOOLEAN
  236. IsStrtabShdr (
  237. Elf_Shdr *Shdr
  238. )
  239. {
  240. Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx);
  241. return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_STRTAB_SECTION_NAME) == 0);
  242. }
  243. STATIC
  244. Elf_Shdr *
  245. FindStrtabShdr (
  246. VOID
  247. )
  248. {
  249. UINT32 i;
  250. for (i = 0; i < mEhdr->e_shnum; i++) {
  251. Elf_Shdr *shdr = GetShdrByIndex(i);
  252. if (IsStrtabShdr(shdr)) {
  253. return shdr;
  254. }
  255. }
  256. return NULL;
  257. }
  258. STATIC
  259. const UINT8 *
  260. GetSymName (
  261. Elf_Sym *Sym
  262. )
  263. {
  264. Elf_Shdr *StrtabShdr;
  265. UINT8 *StrtabContents;
  266. BOOLEAN foundEnd;
  267. UINT32 i;
  268. if (Sym->st_name == 0) {
  269. return NULL;
  270. }
  271. StrtabShdr = FindStrtabShdr();
  272. if (StrtabShdr == NULL) {
  273. return NULL;
  274. }
  275. assert(Sym->st_name < StrtabShdr->sh_size);
  276. StrtabContents = (UINT8*)mEhdr + StrtabShdr->sh_offset;
  277. foundEnd = FALSE;
  278. for (i = Sym->st_name; (i < StrtabShdr->sh_size) && !foundEnd; i++) {
  279. foundEnd = (BOOLEAN)(StrtabContents[i] == 0);
  280. }
  281. assert(foundEnd);
  282. return StrtabContents + Sym->st_name;
  283. }
  284. //
  285. // Elf functions interface implementation
  286. //
  287. STATIC
  288. VOID
  289. ScanSections32 (
  290. VOID
  291. )
  292. {
  293. UINT32 i;
  294. EFI_IMAGE_DOS_HEADER *DosHdr;
  295. EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
  296. UINT32 CoffEntry;
  297. UINT32 SectionCount;
  298. BOOLEAN FoundSection;
  299. CoffEntry = 0;
  300. mCoffOffset = 0;
  301. //
  302. // Coff file start with a DOS header.
  303. //
  304. mCoffOffset = sizeof(EFI_IMAGE_DOS_HEADER) + 0x40;
  305. mNtHdrOffset = mCoffOffset;
  306. switch (mEhdr->e_machine) {
  307. case EM_386:
  308. case EM_ARM:
  309. mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS32);
  310. break;
  311. default:
  312. VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN)mEhdr->e_machine);
  313. mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS32);
  314. break;
  315. }
  316. mTableOffset = mCoffOffset;
  317. mCoffOffset += mCoffNbrSections * sizeof(EFI_IMAGE_SECTION_HEADER);
  318. //
  319. // Set mCoffAlignment to the maximum alignment of the input sections
  320. // we care about
  321. //
  322. for (i = 0; i < mEhdr->e_shnum; i++) {
  323. Elf_Shdr *shdr = GetShdrByIndex(i);
  324. if (shdr->sh_addralign <= mCoffAlignment) {
  325. continue;
  326. }
  327. if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr)) {
  328. mCoffAlignment = (UINT32)shdr->sh_addralign;
  329. }
  330. }
  331. //
  332. // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT
  333. //
  334. if (mCoffAlignment > MAX_COFF_ALIGNMENT) {
  335. Error (NULL, 0, 3000, "Invalid", "Section alignment is larger than MAX_COFF_ALIGNMENT.");
  336. assert (FALSE);
  337. }
  338. //
  339. // Move the PE/COFF header right before the first section. This will help us
  340. // save space when converting to TE.
  341. //
  342. if (mCoffAlignment > mCoffOffset) {
  343. mNtHdrOffset += mCoffAlignment - mCoffOffset;
  344. mTableOffset += mCoffAlignment - mCoffOffset;
  345. mCoffOffset = mCoffAlignment;
  346. }
  347. //
  348. // First text sections.
  349. //
  350. mCoffOffset = CoffAlign(mCoffOffset);
  351. mTextOffset = mCoffOffset;
  352. FoundSection = FALSE;
  353. SectionCount = 0;
  354. for (i = 0; i < mEhdr->e_shnum; i++) {
  355. Elf_Shdr *shdr = GetShdrByIndex(i);
  356. if (IsTextShdr(shdr)) {
  357. if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
  358. // the alignment field is valid
  359. if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
  360. // if the section address is aligned we must align PE/COFF
  361. mCoffOffset = (mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1);
  362. } else {
  363. Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
  364. }
  365. }
  366. /* Relocate entry. */
  367. if ((mEhdr->e_entry >= shdr->sh_addr) &&
  368. (mEhdr->e_entry < shdr->sh_addr + shdr->sh_size)) {
  369. CoffEntry = mCoffOffset + mEhdr->e_entry - shdr->sh_addr;
  370. }
  371. //
  372. // Set mTextOffset with the offset of the first '.text' section
  373. //
  374. if (!FoundSection) {
  375. mTextOffset = mCoffOffset;
  376. FoundSection = TRUE;
  377. }
  378. mCoffSectionsOffset[i] = mCoffOffset;
  379. mCoffOffset += shdr->sh_size;
  380. SectionCount ++;
  381. }
  382. }
  383. if (!FoundSection) {
  384. Error (NULL, 0, 3000, "Invalid", "Did not find any '.text' section.");
  385. assert (FALSE);
  386. }
  387. mDebugOffset = DebugRvaAlign(mCoffOffset);
  388. mCoffOffset = CoffAlign(mCoffOffset);
  389. if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {
  390. Warning (NULL, 0, 0, NULL, "Multiple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName);
  391. }
  392. //
  393. // Then data sections.
  394. //
  395. mDataOffset = mCoffOffset;
  396. FoundSection = FALSE;
  397. SectionCount = 0;
  398. for (i = 0; i < mEhdr->e_shnum; i++) {
  399. Elf_Shdr *shdr = GetShdrByIndex(i);
  400. if (IsDataShdr(shdr)) {
  401. if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
  402. // the alignment field is valid
  403. if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
  404. // if the section address is aligned we must align PE/COFF
  405. mCoffOffset = (mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1);
  406. } else {
  407. Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
  408. }
  409. }
  410. //
  411. // Set mDataOffset with the offset of the first '.data' section
  412. //
  413. if (!FoundSection) {
  414. mDataOffset = mCoffOffset;
  415. FoundSection = TRUE;
  416. }
  417. mCoffSectionsOffset[i] = mCoffOffset;
  418. mCoffOffset += shdr->sh_size;
  419. SectionCount ++;
  420. }
  421. }
  422. if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {
  423. Warning (NULL, 0, 0, NULL, "Multiple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName);
  424. }
  425. //
  426. // Make room for .debug data in .data (or .text if .data is empty) instead of
  427. // putting it in a section of its own. This is explicitly allowed by the
  428. // PE/COFF spec, and prevents bloat in the binary when using large values for
  429. // section alignment.
  430. //
  431. if (SectionCount > 0) {
  432. mDebugOffset = DebugRvaAlign(mCoffOffset);
  433. }
  434. mCoffOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY) +
  435. sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) +
  436. strlen(mInImageName) + 1;
  437. mCoffOffset = CoffAlign(mCoffOffset);
  438. if (SectionCount == 0) {
  439. mDataOffset = mCoffOffset;
  440. }
  441. //
  442. // The HII resource sections.
  443. //
  444. mHiiRsrcOffset = mCoffOffset;
  445. for (i = 0; i < mEhdr->e_shnum; i++) {
  446. Elf_Shdr *shdr = GetShdrByIndex(i);
  447. if (IsHiiRsrcShdr(shdr)) {
  448. if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
  449. // the alignment field is valid
  450. if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
  451. // if the section address is aligned we must align PE/COFF
  452. mCoffOffset = (mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1);
  453. } else {
  454. Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
  455. }
  456. }
  457. if (shdr->sh_size != 0) {
  458. mHiiRsrcOffset = mCoffOffset;
  459. mCoffSectionsOffset[i] = mCoffOffset;
  460. mCoffOffset += shdr->sh_size;
  461. mCoffOffset = CoffAlign(mCoffOffset);
  462. SetHiiResourceHeader ((UINT8*) mEhdr + shdr->sh_offset, mHiiRsrcOffset);
  463. }
  464. break;
  465. }
  466. }
  467. mRelocOffset = mCoffOffset;
  468. //
  469. // Allocate base Coff file. Will be expanded later for relocations.
  470. //
  471. mCoffFile = (UINT8 *)malloc(mCoffOffset);
  472. if (mCoffFile == NULL) {
  473. Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
  474. }
  475. assert (mCoffFile != NULL);
  476. memset(mCoffFile, 0, mCoffOffset);
  477. //
  478. // Fill headers.
  479. //
  480. DosHdr = (EFI_IMAGE_DOS_HEADER *)mCoffFile;
  481. DosHdr->e_magic = EFI_IMAGE_DOS_SIGNATURE;
  482. DosHdr->e_lfanew = mNtHdrOffset;
  483. NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION*)(mCoffFile + mNtHdrOffset);
  484. NtHdr->Pe32.Signature = EFI_IMAGE_NT_SIGNATURE;
  485. switch (mEhdr->e_machine) {
  486. case EM_386:
  487. NtHdr->Pe32.FileHeader.Machine = EFI_IMAGE_MACHINE_IA32;
  488. NtHdr->Pe32.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;
  489. break;
  490. case EM_ARM:
  491. NtHdr->Pe32.FileHeader.Machine = EFI_IMAGE_MACHINE_ARMT;
  492. NtHdr->Pe32.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;
  493. break;
  494. default:
  495. VerboseMsg ("%s unknown e_machine type %hu. Assume IA-32", mInImageName, mEhdr->e_machine);
  496. NtHdr->Pe32.FileHeader.Machine = EFI_IMAGE_MACHINE_IA32;
  497. NtHdr->Pe32.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;
  498. }
  499. NtHdr->Pe32.FileHeader.NumberOfSections = mCoffNbrSections;
  500. NtHdr->Pe32.FileHeader.TimeDateStamp = (UINT32) time(NULL);
  501. mImageTimeStamp = NtHdr->Pe32.FileHeader.TimeDateStamp;
  502. NtHdr->Pe32.FileHeader.PointerToSymbolTable = 0;
  503. NtHdr->Pe32.FileHeader.NumberOfSymbols = 0;
  504. NtHdr->Pe32.FileHeader.SizeOfOptionalHeader = sizeof(NtHdr->Pe32.OptionalHeader);
  505. NtHdr->Pe32.FileHeader.Characteristics = EFI_IMAGE_FILE_EXECUTABLE_IMAGE
  506. | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
  507. | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
  508. | EFI_IMAGE_FILE_32BIT_MACHINE;
  509. NtHdr->Pe32.OptionalHeader.SizeOfCode = mDataOffset - mTextOffset;
  510. NtHdr->Pe32.OptionalHeader.SizeOfInitializedData = mRelocOffset - mDataOffset;
  511. NtHdr->Pe32.OptionalHeader.SizeOfUninitializedData = 0;
  512. NtHdr->Pe32.OptionalHeader.AddressOfEntryPoint = CoffEntry;
  513. NtHdr->Pe32.OptionalHeader.BaseOfCode = mTextOffset;
  514. NtHdr->Pe32.OptionalHeader.BaseOfData = mDataOffset;
  515. NtHdr->Pe32.OptionalHeader.ImageBase = 0;
  516. NtHdr->Pe32.OptionalHeader.SectionAlignment = mCoffAlignment;
  517. NtHdr->Pe32.OptionalHeader.FileAlignment = mCoffAlignment;
  518. NtHdr->Pe32.OptionalHeader.SizeOfImage = 0;
  519. NtHdr->Pe32.OptionalHeader.SizeOfHeaders = mTextOffset;
  520. NtHdr->Pe32.OptionalHeader.NumberOfRvaAndSizes = EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES;
  521. //
  522. // Section headers.
  523. //
  524. if ((mDataOffset - mTextOffset) > 0) {
  525. CreateSectionHeader (".text", mTextOffset, mDataOffset - mTextOffset,
  526. EFI_IMAGE_SCN_CNT_CODE
  527. | EFI_IMAGE_SCN_MEM_EXECUTE
  528. | EFI_IMAGE_SCN_MEM_READ);
  529. } else {
  530. // Don't make a section of size 0.
  531. NtHdr->Pe32.FileHeader.NumberOfSections--;
  532. }
  533. if ((mHiiRsrcOffset - mDataOffset) > 0) {
  534. CreateSectionHeader (".data", mDataOffset, mHiiRsrcOffset - mDataOffset,
  535. EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
  536. | EFI_IMAGE_SCN_MEM_WRITE
  537. | EFI_IMAGE_SCN_MEM_READ);
  538. } else {
  539. // Don't make a section of size 0.
  540. NtHdr->Pe32.FileHeader.NumberOfSections--;
  541. }
  542. if ((mRelocOffset - mHiiRsrcOffset) > 0) {
  543. CreateSectionHeader (".rsrc", mHiiRsrcOffset, mRelocOffset - mHiiRsrcOffset,
  544. EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
  545. | EFI_IMAGE_SCN_MEM_READ);
  546. NtHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = mRelocOffset - mHiiRsrcOffset;
  547. NtHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = mHiiRsrcOffset;
  548. } else {
  549. // Don't make a section of size 0.
  550. NtHdr->Pe32.FileHeader.NumberOfSections--;
  551. }
  552. }
  553. STATIC
  554. BOOLEAN
  555. WriteSections32 (
  556. SECTION_FILTER_TYPES FilterType
  557. )
  558. {
  559. UINT32 Idx;
  560. Elf_Shdr *SecShdr;
  561. UINT32 SecOffset;
  562. BOOLEAN (*Filter)(Elf_Shdr *);
  563. //
  564. // Initialize filter pointer
  565. //
  566. switch (FilterType) {
  567. case SECTION_TEXT:
  568. Filter = IsTextShdr;
  569. break;
  570. case SECTION_HII:
  571. Filter = IsHiiRsrcShdr;
  572. break;
  573. case SECTION_DATA:
  574. Filter = IsDataShdr;
  575. break;
  576. default:
  577. return FALSE;
  578. }
  579. //
  580. // First: copy sections.
  581. //
  582. for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {
  583. Elf_Shdr *Shdr = GetShdrByIndex(Idx);
  584. if ((*Filter)(Shdr)) {
  585. switch (Shdr->sh_type) {
  586. case SHT_PROGBITS:
  587. /* Copy. */
  588. if (Shdr->sh_offset + Shdr->sh_size > mFileBufferSize) {
  589. return FALSE;
  590. }
  591. memcpy(mCoffFile + mCoffSectionsOffset[Idx],
  592. (UINT8*)mEhdr + Shdr->sh_offset,
  593. Shdr->sh_size);
  594. break;
  595. case SHT_NOBITS:
  596. memset(mCoffFile + mCoffSectionsOffset[Idx], 0, Shdr->sh_size);
  597. break;
  598. default:
  599. //
  600. // Ignore for unknown section type.
  601. //
  602. VerboseMsg ("%s unknown section type %x. We ignore this unknown section type.", mInImageName, (unsigned)Shdr->sh_type);
  603. break;
  604. }
  605. }
  606. }
  607. //
  608. // Second: apply relocations.
  609. //
  610. for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {
  611. //
  612. // Determine if this is a relocation section.
  613. //
  614. Elf_Shdr *RelShdr = GetShdrByIndex(Idx);
  615. if ((RelShdr->sh_type != SHT_REL) && (RelShdr->sh_type != SHT_RELA)) {
  616. continue;
  617. }
  618. //
  619. // Relocation section found. Now extract section information that the relocations
  620. // apply to in the ELF data and the new COFF data.
  621. //
  622. SecShdr = GetShdrByIndex(RelShdr->sh_info);
  623. SecOffset = mCoffSectionsOffset[RelShdr->sh_info];
  624. //
  625. // Only process relocations for the current filter type.
  626. //
  627. if (RelShdr->sh_type == SHT_REL && (*Filter)(SecShdr)) {
  628. UINT32 RelOffset;
  629. //
  630. // Determine the symbol table referenced by the relocation data.
  631. //
  632. Elf_Shdr *SymtabShdr = GetShdrByIndex(RelShdr->sh_link);
  633. UINT8 *Symtab = (UINT8*)mEhdr + SymtabShdr->sh_offset;
  634. //
  635. // Process all relocation entries for this section.
  636. //
  637. for (RelOffset = 0; RelOffset < RelShdr->sh_size; RelOffset += RelShdr->sh_entsize) {
  638. //
  639. // Set pointer to relocation entry
  640. //
  641. Elf_Rel *Rel = (Elf_Rel *)((UINT8*)mEhdr + RelShdr->sh_offset + RelOffset);
  642. //
  643. // Set pointer to symbol table entry associated with the relocation entry.
  644. //
  645. Elf_Sym *Sym = (Elf_Sym *)(Symtab + ELF_R_SYM(Rel->r_info) * SymtabShdr->sh_entsize);
  646. Elf_Shdr *SymShdr;
  647. UINT8 *Targ;
  648. UINT16 Address;
  649. //
  650. // Check section header index found in symbol table and get the section
  651. // header location.
  652. //
  653. if (Sym->st_shndx == SHN_UNDEF
  654. || Sym->st_shndx >= mEhdr->e_shnum) {
  655. const UINT8 *SymName = GetSymName(Sym);
  656. if (SymName == NULL) {
  657. SymName = (const UINT8 *)"<unknown>";
  658. }
  659. Error (NULL, 0, 3000, "Invalid",
  660. "%s: Bad definition for symbol '%s'@%#x or unsupported symbol type. "
  661. "For example, absolute and undefined symbols are not supported.",
  662. mInImageName, SymName, Sym->st_value);
  663. exit(EXIT_FAILURE);
  664. }
  665. SymShdr = GetShdrByIndex(Sym->st_shndx);
  666. //
  667. // Convert the relocation data to a pointer into the coff file.
  668. //
  669. // Note:
  670. // r_offset is the virtual address of the storage unit to be relocated.
  671. // sh_addr is the virtual address for the base of the section.
  672. //
  673. Targ = mCoffFile + SecOffset + (Rel->r_offset - SecShdr->sh_addr);
  674. //
  675. // Determine how to handle each relocation type based on the machine type.
  676. //
  677. if (mEhdr->e_machine == EM_386) {
  678. switch (ELF_R_TYPE(Rel->r_info)) {
  679. case R_386_NONE:
  680. break;
  681. case R_386_32:
  682. //
  683. // Absolute relocation.
  684. // Converts Targ from a absolute virtual address to the absolute
  685. // COFF address.
  686. //
  687. *(UINT32 *)Targ = *(UINT32 *)Targ - SymShdr->sh_addr
  688. + mCoffSectionsOffset[Sym->st_shndx];
  689. break;
  690. case R_386_PC32:
  691. //
  692. // Relative relocation: Symbol - Ip + Addend
  693. //
  694. *(UINT32 *)Targ = *(UINT32 *)Targ
  695. + (mCoffSectionsOffset[Sym->st_shndx] - SymShdr->sh_addr)
  696. - (SecOffset - SecShdr->sh_addr);
  697. break;
  698. default:
  699. Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
  700. }
  701. } else if (mEhdr->e_machine == EM_ARM) {
  702. switch (ELF32_R_TYPE(Rel->r_info)) {
  703. case R_ARM_RBASE:
  704. // No relocation - no action required
  705. // break skipped
  706. case R_ARM_PC24:
  707. case R_ARM_REL32:
  708. case R_ARM_XPC25:
  709. case R_ARM_THM_PC22:
  710. case R_ARM_THM_JUMP19:
  711. case R_ARM_CALL:
  712. case R_ARM_JMP24:
  713. case R_ARM_THM_JUMP24:
  714. case R_ARM_PREL31:
  715. case R_ARM_MOVW_PREL_NC:
  716. case R_ARM_MOVT_PREL:
  717. case R_ARM_THM_MOVW_PREL_NC:
  718. case R_ARM_THM_MOVT_PREL:
  719. case R_ARM_THM_JMP6:
  720. case R_ARM_THM_ALU_PREL_11_0:
  721. case R_ARM_THM_PC12:
  722. case R_ARM_REL32_NOI:
  723. case R_ARM_ALU_PC_G0_NC:
  724. case R_ARM_ALU_PC_G0:
  725. case R_ARM_ALU_PC_G1_NC:
  726. case R_ARM_ALU_PC_G1:
  727. case R_ARM_ALU_PC_G2:
  728. case R_ARM_LDR_PC_G1:
  729. case R_ARM_LDR_PC_G2:
  730. case R_ARM_LDRS_PC_G0:
  731. case R_ARM_LDRS_PC_G1:
  732. case R_ARM_LDRS_PC_G2:
  733. case R_ARM_LDC_PC_G0:
  734. case R_ARM_LDC_PC_G1:
  735. case R_ARM_LDC_PC_G2:
  736. case R_ARM_THM_JUMP11:
  737. case R_ARM_THM_JUMP8:
  738. case R_ARM_TLS_GD32:
  739. case R_ARM_TLS_LDM32:
  740. case R_ARM_TLS_IE32:
  741. // Thease are all PC-relative relocations and don't require modification
  742. // GCC does not seem to have the concept of a application that just needs to get relocated.
  743. break;
  744. case R_ARM_THM_MOVW_ABS_NC:
  745. // MOVW is only lower 16-bits of the addres
  746. Address = (UINT16)(Sym->st_value - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
  747. ThumbMovtImmediatePatch ((UINT16 *)Targ, Address);
  748. break;
  749. case R_ARM_THM_MOVT_ABS:
  750. // MOVT is only upper 16-bits of the addres
  751. Address = (UINT16)((Sym->st_value - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]) >> 16);
  752. ThumbMovtImmediatePatch ((UINT16 *)Targ, Address);
  753. break;
  754. case R_ARM_ABS32:
  755. case R_ARM_RABS32:
  756. //
  757. // Absolute relocation.
  758. //
  759. *(UINT32 *)Targ = *(UINT32 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
  760. break;
  761. default:
  762. Error (NULL, 0, 3000, "Invalid", "WriteSections (): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName, (unsigned) ELF32_R_TYPE(Rel->r_info));
  763. }
  764. }
  765. }
  766. }
  767. }
  768. return TRUE;
  769. }
  770. UINTN gMovwOffset = 0;
  771. STATIC
  772. VOID
  773. WriteRelocations32 (
  774. VOID
  775. )
  776. {
  777. UINT32 Index;
  778. EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
  779. EFI_IMAGE_DATA_DIRECTORY *Dir;
  780. BOOLEAN FoundRelocations;
  781. Elf_Dyn *Dyn;
  782. Elf_Rel *Rel;
  783. UINTN RelElementSize;
  784. UINTN RelSize;
  785. UINTN RelOffset;
  786. UINTN K;
  787. Elf32_Phdr *DynamicSegment;
  788. for (Index = 0, FoundRelocations = FALSE; Index < mEhdr->e_shnum; Index++) {
  789. Elf_Shdr *RelShdr = GetShdrByIndex(Index);
  790. if ((RelShdr->sh_type == SHT_REL) || (RelShdr->sh_type == SHT_RELA)) {
  791. Elf_Shdr *SecShdr = GetShdrByIndex (RelShdr->sh_info);
  792. if (IsTextShdr(SecShdr) || IsDataShdr(SecShdr)) {
  793. UINT32 RelIdx;
  794. FoundRelocations = TRUE;
  795. for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += RelShdr->sh_entsize) {
  796. Rel = (Elf_Rel *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx);
  797. if (mEhdr->e_machine == EM_386) {
  798. switch (ELF_R_TYPE(Rel->r_info)) {
  799. case R_386_NONE:
  800. case R_386_PC32:
  801. //
  802. // No fixup entry required.
  803. //
  804. break;
  805. case R_386_32:
  806. //
  807. // Creates a relative relocation entry from the absolute entry.
  808. //
  809. CoffAddFixup(mCoffSectionsOffset[RelShdr->sh_info]
  810. + (Rel->r_offset - SecShdr->sh_addr),
  811. EFI_IMAGE_REL_BASED_HIGHLOW);
  812. break;
  813. default:
  814. Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
  815. }
  816. } else if (mEhdr->e_machine == EM_ARM) {
  817. switch (ELF32_R_TYPE(Rel->r_info)) {
  818. case R_ARM_RBASE:
  819. // No relocation - no action required
  820. // break skipped
  821. case R_ARM_PC24:
  822. case R_ARM_REL32:
  823. case R_ARM_XPC25:
  824. case R_ARM_THM_PC22:
  825. case R_ARM_THM_JUMP19:
  826. case R_ARM_CALL:
  827. case R_ARM_JMP24:
  828. case R_ARM_THM_JUMP24:
  829. case R_ARM_PREL31:
  830. case R_ARM_MOVW_PREL_NC:
  831. case R_ARM_MOVT_PREL:
  832. case R_ARM_THM_MOVW_PREL_NC:
  833. case R_ARM_THM_MOVT_PREL:
  834. case R_ARM_THM_JMP6:
  835. case R_ARM_THM_ALU_PREL_11_0:
  836. case R_ARM_THM_PC12:
  837. case R_ARM_REL32_NOI:
  838. case R_ARM_ALU_PC_G0_NC:
  839. case R_ARM_ALU_PC_G0:
  840. case R_ARM_ALU_PC_G1_NC:
  841. case R_ARM_ALU_PC_G1:
  842. case R_ARM_ALU_PC_G2:
  843. case R_ARM_LDR_PC_G1:
  844. case R_ARM_LDR_PC_G2:
  845. case R_ARM_LDRS_PC_G0:
  846. case R_ARM_LDRS_PC_G1:
  847. case R_ARM_LDRS_PC_G2:
  848. case R_ARM_LDC_PC_G0:
  849. case R_ARM_LDC_PC_G1:
  850. case R_ARM_LDC_PC_G2:
  851. case R_ARM_THM_JUMP11:
  852. case R_ARM_THM_JUMP8:
  853. case R_ARM_TLS_GD32:
  854. case R_ARM_TLS_LDM32:
  855. case R_ARM_TLS_IE32:
  856. // Thease are all PC-relative relocations and don't require modification
  857. break;
  858. case R_ARM_THM_MOVW_ABS_NC:
  859. CoffAddFixup (
  860. mCoffSectionsOffset[RelShdr->sh_info]
  861. + (Rel->r_offset - SecShdr->sh_addr),
  862. EFI_IMAGE_REL_BASED_ARM_MOV32T
  863. );
  864. // PE/COFF treats MOVW/MOVT relocation as single 64-bit instruction
  865. // Track this address so we can log an error for unsupported sequence of MOVW/MOVT
  866. gMovwOffset = mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr);
  867. break;
  868. case R_ARM_THM_MOVT_ABS:
  869. if ((gMovwOffset + 4) != (mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr))) {
  870. Error (NULL, 0, 3000, "Not Supported", "PE/COFF requires MOVW+MOVT instruction sequence %x +4 != %x.", gMovwOffset, mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr));
  871. }
  872. break;
  873. case R_ARM_ABS32:
  874. case R_ARM_RABS32:
  875. CoffAddFixup (
  876. mCoffSectionsOffset[RelShdr->sh_info]
  877. + (Rel->r_offset - SecShdr->sh_addr),
  878. EFI_IMAGE_REL_BASED_HIGHLOW
  879. );
  880. break;
  881. default:
  882. Error (NULL, 0, 3000, "Invalid", "WriteRelocations(): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName, (unsigned) ELF32_R_TYPE(Rel->r_info));
  883. }
  884. } else {
  885. Error (NULL, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr->e_machine);
  886. }
  887. }
  888. }
  889. }
  890. }
  891. if (!FoundRelocations && (mEhdr->e_machine == EM_ARM)) {
  892. /* Try again, but look for PT_DYNAMIC instead of SHT_REL */
  893. for (Index = 0; Index < mEhdr->e_phnum; Index++) {
  894. RelElementSize = 0;
  895. RelSize = 0;
  896. RelOffset = 0;
  897. DynamicSegment = GetPhdrByIndex (Index);
  898. if (DynamicSegment->p_type == PT_DYNAMIC) {
  899. Dyn = (Elf32_Dyn *) ((UINT8 *)mEhdr + DynamicSegment->p_offset);
  900. while (Dyn->d_tag != DT_NULL) {
  901. switch (Dyn->d_tag) {
  902. case DT_REL:
  903. RelOffset = Dyn->d_un.d_val;
  904. break;
  905. case DT_RELSZ:
  906. RelSize = Dyn->d_un.d_val;
  907. break;
  908. case DT_RELENT:
  909. RelElementSize = Dyn->d_un.d_val;
  910. break;
  911. default:
  912. break;
  913. }
  914. Dyn++;
  915. }
  916. if (( RelOffset == 0 ) || ( RelSize == 0 ) || ( RelElementSize == 0 )) {
  917. Error (NULL, 0, 3000, "Invalid", "%s bad ARM dynamic relocations.", mInImageName);
  918. }
  919. for (Index = 0; Index < mEhdr->e_shnum; Index++) {
  920. Elf_Shdr *shdr = GetShdrByIndex(Index);
  921. //
  922. // The PT_DYNAMIC section contains DT_REL relocations whose r_offset
  923. // field is relative to the base of a segment (or the entire image),
  924. // and not to the base of an ELF input section as is the case for
  925. // SHT_REL sections. This means that we cannot fix up such relocations
  926. // unless we cross-reference ELF sections and segments, considering
  927. // that the output placement recorded in mCoffSectionsOffset[] is
  928. // section based, not segment based.
  929. //
  930. // Fortunately, there is a simple way around this: we require that the
  931. // in-memory layout of the ELF and PE/COFF versions of the binary is
  932. // identical. That way, r_offset will retain its validity as a PE/COFF
  933. // image offset, and we can record it in the COFF fixup table
  934. // unmodified.
  935. //
  936. if (shdr->sh_addr != mCoffSectionsOffset[Index]) {
  937. Error (NULL, 0, 3000,
  938. "Invalid", "%s: PT_DYNAMIC relocations require identical ELF and PE/COFF section offsets.",
  939. mInImageName);
  940. }
  941. }
  942. for (K = 0; K < RelSize; K += RelElementSize) {
  943. if (DynamicSegment->p_paddr == 0) {
  944. // Older versions of the ARM ELF (SWS ESPC 0003 B-02) specification define DT_REL
  945. // as an offset in the dynamic segment. p_paddr is defined to be zero for ARM tools
  946. Rel = (Elf32_Rel *) ((UINT8 *) mEhdr + DynamicSegment->p_offset + RelOffset + K);
  947. } else {
  948. // This is how it reads in the generic ELF specification
  949. Rel = (Elf32_Rel *) ((UINT8 *) mEhdr + RelOffset + K);
  950. }
  951. switch (ELF32_R_TYPE (Rel->r_info)) {
  952. case R_ARM_RBASE:
  953. break;
  954. case R_ARM_RABS32:
  955. CoffAddFixup (Rel->r_offset, EFI_IMAGE_REL_BASED_HIGHLOW);
  956. break;
  957. default:
  958. Error (NULL, 0, 3000, "Invalid", "%s bad ARM dynamic relocations, unknown type %d.", mInImageName, ELF32_R_TYPE (Rel->r_info));
  959. break;
  960. }
  961. }
  962. break;
  963. }
  964. }
  965. }
  966. //
  967. // Pad by adding empty entries.
  968. //
  969. while (mCoffOffset & (mCoffAlignment - 1)) {
  970. CoffAddFixupEntry(0);
  971. }
  972. NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
  973. Dir = &NtHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
  974. Dir->Size = mCoffOffset - mRelocOffset;
  975. if (Dir->Size == 0) {
  976. // If no relocations, null out the directory entry and don't add the .reloc section
  977. Dir->VirtualAddress = 0;
  978. NtHdr->Pe32.FileHeader.NumberOfSections--;
  979. } else {
  980. Dir->VirtualAddress = mRelocOffset;
  981. CreateSectionHeader (".reloc", mRelocOffset, mCoffOffset - mRelocOffset,
  982. EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
  983. | EFI_IMAGE_SCN_MEM_DISCARDABLE
  984. | EFI_IMAGE_SCN_MEM_READ);
  985. }
  986. }
  987. STATIC
  988. VOID
  989. WriteDebug32 (
  990. VOID
  991. )
  992. {
  993. UINT32 Len;
  994. EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
  995. EFI_IMAGE_DATA_DIRECTORY *DataDir;
  996. EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *Dir;
  997. EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *Nb10;
  998. Len = strlen(mInImageName) + 1;
  999. Dir = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(mCoffFile + mDebugOffset);
  1000. Dir->Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW;
  1001. Dir->SizeOfData = sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + Len;
  1002. Dir->RVA = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
  1003. Dir->FileOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
  1004. Nb10 = (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY*)(Dir + 1);
  1005. Nb10->Signature = CODEVIEW_SIGNATURE_NB10;
  1006. strcpy ((char *)(Nb10 + 1), mInImageName);
  1007. NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
  1008. DataDir = &NtHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG];
  1009. DataDir->VirtualAddress = mDebugOffset;
  1010. DataDir->Size = sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
  1011. }
  1012. STATIC
  1013. VOID
  1014. SetImageSize32 (
  1015. VOID
  1016. )
  1017. {
  1018. EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
  1019. //
  1020. // Set image size
  1021. //
  1022. NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
  1023. NtHdr->Pe32.OptionalHeader.SizeOfImage = mCoffOffset;
  1024. }
  1025. STATIC
  1026. VOID
  1027. CleanUp32 (
  1028. VOID
  1029. )
  1030. {
  1031. if (mCoffSectionsOffset != NULL) {
  1032. free (mCoffSectionsOffset);
  1033. }
  1034. }