4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "WinNtInclude.h"
24 #include <Common/UefiBaseTypes.h>
25 #include <IndustryStandard/PeImage.h>
27 #include "PeCoffLib.h"
28 #include "EfiUtilityMsgs.h"
31 #include "ElfConvert.h"
32 #include "Elf64Convert.h"
43 SECTION_FILTER_TYPES FilterType
71 // Rename ELF32 structures to common names to help when porting to ELF64.
73 typedef Elf64_Shdr Elf_Shdr
;
74 typedef Elf64_Ehdr Elf_Ehdr
;
75 typedef Elf64_Rel Elf_Rel
;
76 typedef Elf64_Rela Elf_Rela
;
77 typedef Elf64_Sym Elf_Sym
;
78 typedef Elf64_Phdr Elf_Phdr
;
79 typedef Elf64_Dyn Elf_Dyn
;
80 #define ELFCLASS ELFCLASS64
81 #define ELF_R_TYPE(r) ELF64_R_TYPE(r)
82 #define ELF_R_SYM(r) ELF64_R_SYM(r)
85 // Well known ELF structures.
87 STATIC Elf_Ehdr
*mEhdr
;
88 STATIC Elf_Shdr
*mShdrBase
;
89 STATIC Elf_Phdr
*mPhdrBase
;
94 STATIC Elf_Shdr
*mGOTShdr
= NULL
;
95 STATIC UINT32 mGOTShindex
= 0;
96 STATIC UINT32
*mGOTCoffEntries
= NULL
;
97 STATIC UINT32 mGOTMaxCoffEntries
= 0;
98 STATIC UINT32 mGOTNumCoffEntries
= 0;
103 STATIC UINT32 mCoffAlignment
= 0x20;
106 // PE section alignment.
108 STATIC
const UINT16 mCoffNbrSections
= 4;
111 // ELF sections to offset in Coff file.
113 STATIC UINT32
*mCoffSectionsOffset
= NULL
;
116 // Offsets in COFF file
118 STATIC UINT32 mNtHdrOffset
;
119 STATIC UINT32 mTextOffset
;
120 STATIC UINT32 mDataOffset
;
121 STATIC UINT32 mHiiRsrcOffset
;
122 STATIC UINT32 mRelocOffset
;
123 STATIC UINT32 mDebugOffset
;
126 // Initialization Function
131 ELF_FUNCTION_TABLE
*ElfFunctions
135 // Initialize data pointer and structures.
137 VerboseMsg ("Set EHDR");
138 mEhdr
= (Elf_Ehdr
*) FileBuffer
;
141 // Check the ELF64 specific header information.
143 VerboseMsg ("Check ELF64 Header Information");
144 if (mEhdr
->e_ident
[EI_CLASS
] != ELFCLASS64
) {
145 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS64");
148 if (mEhdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
149 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
152 if ((mEhdr
->e_type
!= ET_EXEC
) && (mEhdr
->e_type
!= ET_DYN
)) {
153 Error (NULL
, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
156 if (!((mEhdr
->e_machine
== EM_X86_64
) || (mEhdr
->e_machine
== EM_AARCH64
))) {
157 Error (NULL
, 0, 3000, "Unsupported", "ELF e_machine not EM_X86_64 or EM_AARCH64");
160 if (mEhdr
->e_version
!= EV_CURRENT
) {
161 Error (NULL
, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr
->e_version
, EV_CURRENT
);
166 // Update section header pointers
168 VerboseMsg ("Update Header Pointers");
169 mShdrBase
= (Elf_Shdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_shoff
);
170 mPhdrBase
= (Elf_Phdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_phoff
);
173 // Create COFF Section offset buffer and zero.
175 VerboseMsg ("Create COFF Section Offset Buffer");
176 mCoffSectionsOffset
= (UINT32
*)malloc(mEhdr
->e_shnum
* sizeof (UINT32
));
177 if (mCoffSectionsOffset
== NULL
) {
178 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
181 memset(mCoffSectionsOffset
, 0, mEhdr
->e_shnum
* sizeof(UINT32
));
184 // Fill in function pointers.
186 VerboseMsg ("Fill in Function Pointers");
187 ElfFunctions
->ScanSections
= ScanSections64
;
188 ElfFunctions
->WriteSections
= WriteSections64
;
189 ElfFunctions
->WriteRelocations
= WriteRelocations64
;
190 ElfFunctions
->WriteDebug
= WriteDebug64
;
191 ElfFunctions
->SetImageSize
= SetImageSize64
;
192 ElfFunctions
->CleanUp
= CleanUp64
;
199 // Header by Index functions
207 if (Num
>= mEhdr
->e_shnum
) {
208 Error (NULL
, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num
);
212 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
221 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
230 return (Offset
+ 3) & ~3;
242 return (BOOLEAN
) ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
);
251 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
253 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
262 if (IsHiiRsrcShdr(Shdr
)) {
265 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
274 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
276 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_STRTAB_SECTION_NAME
) == 0);
286 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
287 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
288 if (IsStrtabShdr(shdr
)) {
301 Elf_Shdr
*StrtabShdr
;
302 UINT8
*StrtabContents
;
306 if (Sym
->st_name
== 0) {
310 StrtabShdr
= FindStrtabShdr();
311 if (StrtabShdr
== NULL
) {
315 assert(Sym
->st_name
< StrtabShdr
->sh_size
);
317 StrtabContents
= (UINT8
*)mEhdr
+ StrtabShdr
->sh_offset
;
320 for (i
= Sym
->st_name
; (i
< StrtabShdr
->sh_size
) && !foundEnd
; i
++) {
321 foundEnd
= (BOOLEAN
)(StrtabContents
[i
] == 0);
325 return StrtabContents
+ Sym
->st_name
;
329 // Find the ELF section hosting the GOT from an ELF Rva
330 // of a single GOT entry. Normally, GOT is placed in
331 // ELF .text section, so assume once we find in which
332 // section the GOT is, all GOT entries are there, and
337 FindElfGOTSectionFromGOTEntryElfRva (
338 Elf64_Addr GOTEntryElfRva
342 if (mGOTShdr
!= NULL
) {
343 if (GOTEntryElfRva
>= mGOTShdr
->sh_addr
&&
344 GOTEntryElfRva
< mGOTShdr
->sh_addr
+ mGOTShdr
->sh_size
) {
347 Error (NULL
, 0, 3000, "Unsupported", "FindElfGOTSectionFromGOTEntryElfRva: GOT entries found in multiple sections.");
350 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
351 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
352 if (GOTEntryElfRva
>= shdr
->sh_addr
&&
353 GOTEntryElfRva
< shdr
->sh_addr
+ shdr
->sh_size
) {
359 Error (NULL
, 0, 3000, "Invalid", "FindElfGOTSectionFromGOTEntryElfRva: ElfRva 0x%016LX for GOT entry not found in any section.", GOTEntryElfRva
);
364 // Stores locations of GOT entries in COFF image.
365 // Returns TRUE if GOT entry is new.
366 // Simple implementation as number of GOT
367 // entries is expected to be low.
372 AccumulateCoffGOTEntries (
377 if (mGOTCoffEntries
!= NULL
) {
378 for (i
= 0; i
< mGOTNumCoffEntries
; i
++) {
379 if (mGOTCoffEntries
[i
] == GOTCoffEntry
) {
384 if (mGOTCoffEntries
== NULL
) {
385 mGOTCoffEntries
= (UINT32
*)malloc(5 * sizeof *mGOTCoffEntries
);
386 if (mGOTCoffEntries
== NULL
) {
387 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
389 assert (mGOTCoffEntries
!= NULL
);
390 mGOTMaxCoffEntries
= 5;
391 mGOTNumCoffEntries
= 0;
392 } else if (mGOTNumCoffEntries
== mGOTMaxCoffEntries
) {
393 mGOTCoffEntries
= (UINT32
*)realloc(mGOTCoffEntries
, 2 * mGOTMaxCoffEntries
* sizeof *mGOTCoffEntries
);
394 if (mGOTCoffEntries
== NULL
) {
395 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
397 assert (mGOTCoffEntries
!= NULL
);
398 mGOTMaxCoffEntries
+= mGOTMaxCoffEntries
;
400 mGOTCoffEntries
[mGOTNumCoffEntries
++] = GOTCoffEntry
;
405 // 32-bit Unsigned integer comparator for qsort.
414 if (*(const UINT32
*)lhs
< *(const UINT32
*)rhs
) {
417 return *(const UINT32
*)lhs
> *(const UINT32
*)rhs
;
421 // Emit accumulated Coff GOT entry relocations into
422 // Coff image. This function performs its job
423 // once and then releases the entry list, so
424 // it can safely be called multiple times.
433 if (mGOTCoffEntries
== NULL
) {
437 // Emit Coff relocations with Rvas ordered.
442 sizeof *mGOTCoffEntries
,
444 for (i
= 0; i
< mGOTNumCoffEntries
; i
++) {
445 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X", mGOTCoffEntries
[i
]);
448 EFI_IMAGE_REL_BASED_DIR64
);
450 free(mGOTCoffEntries
);
451 mGOTCoffEntries
= NULL
;
452 mGOTMaxCoffEntries
= 0;
453 mGOTNumCoffEntries
= 0;
457 // Elf functions interface implementation
467 EFI_IMAGE_DOS_HEADER
*DosHdr
;
468 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
471 BOOLEAN FoundSection
;
477 // Coff file start with a DOS header.
479 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
480 mNtHdrOffset
= mCoffOffset
;
481 switch (mEhdr
->e_machine
) {
484 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
487 VerboseMsg ("%s unknown e_machine type %hu. Assume X64", mInImageName
, mEhdr
->e_machine
);
488 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
492 mTableOffset
= mCoffOffset
;
493 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
496 // Set mCoffAlignment to the maximum alignment of the input sections
499 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
500 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
501 if (shdr
->sh_addralign
<= mCoffAlignment
) {
504 if (IsTextShdr(shdr
) || IsDataShdr(shdr
) || IsHiiRsrcShdr(shdr
)) {
505 mCoffAlignment
= (UINT32
)shdr
->sh_addralign
;
510 // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT
512 if (mCoffAlignment
> MAX_COFF_ALIGNMENT
) {
513 Error (NULL
, 0, 3000, "Invalid", "Section alignment is larger than MAX_COFF_ALIGNMENT.");
519 // Move the PE/COFF header right before the first section. This will help us
520 // save space when converting to TE.
522 if (mCoffAlignment
> mCoffOffset
) {
523 mNtHdrOffset
+= mCoffAlignment
- mCoffOffset
;
524 mTableOffset
+= mCoffAlignment
- mCoffOffset
;
525 mCoffOffset
= mCoffAlignment
;
529 // First text sections.
531 mCoffOffset
= CoffAlign(mCoffOffset
);
532 mTextOffset
= mCoffOffset
;
533 FoundSection
= FALSE
;
535 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
536 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
537 if (IsTextShdr(shdr
)) {
538 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
539 // the alignment field is valid
540 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
541 // if the section address is aligned we must align PE/COFF
542 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
544 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
548 /* Relocate entry. */
549 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
550 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
551 CoffEntry
= (UINT32
) (mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
);
555 // Set mTextOffset with the offset of the first '.text' section
558 mTextOffset
= mCoffOffset
;
562 mCoffSectionsOffset
[i
] = mCoffOffset
;
563 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
569 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
573 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
574 mCoffOffset
= CoffAlign(mCoffOffset
);
576 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
577 Warning (NULL
, 0, 0, NULL
, "Multiple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
581 // Then data sections.
583 mDataOffset
= mCoffOffset
;
584 FoundSection
= FALSE
;
586 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
587 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
588 if (IsDataShdr(shdr
)) {
589 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
590 // the alignment field is valid
591 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
592 // if the section address is aligned we must align PE/COFF
593 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
595 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
600 // Set mDataOffset with the offset of the first '.data' section
603 mDataOffset
= mCoffOffset
;
606 mCoffSectionsOffset
[i
] = mCoffOffset
;
607 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
613 // Make room for .debug data in .data (or .text if .data is empty) instead of
614 // putting it in a section of its own. This is explicitly allowed by the
615 // PE/COFF spec, and prevents bloat in the binary when using large values for
616 // section alignment.
618 if (SectionCount
> 0) {
619 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
621 mCoffOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
) +
622 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) +
623 strlen(mInImageName
) + 1;
625 mCoffOffset
= CoffAlign(mCoffOffset
);
626 if (SectionCount
== 0) {
627 mDataOffset
= mCoffOffset
;
630 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
631 Warning (NULL
, 0, 0, NULL
, "Multiple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
635 // The HII resource sections.
637 mHiiRsrcOffset
= mCoffOffset
;
638 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
639 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
640 if (IsHiiRsrcShdr(shdr
)) {
641 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
642 // the alignment field is valid
643 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
644 // if the section address is aligned we must align PE/COFF
645 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
647 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
650 if (shdr
->sh_size
!= 0) {
651 mHiiRsrcOffset
= mCoffOffset
;
652 mCoffSectionsOffset
[i
] = mCoffOffset
;
653 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
654 mCoffOffset
= CoffAlign(mCoffOffset
);
655 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
661 mRelocOffset
= mCoffOffset
;
664 // Allocate base Coff file. Will be expanded later for relocations.
666 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
667 if (mCoffFile
== NULL
) {
668 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
670 assert (mCoffFile
!= NULL
);
671 memset(mCoffFile
, 0, mCoffOffset
);
676 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
677 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
678 DosHdr
->e_lfanew
= mNtHdrOffset
;
680 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
682 NtHdr
->Pe32Plus
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
684 switch (mEhdr
->e_machine
) {
686 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
687 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
690 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_AARCH64
;
691 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
694 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN
)mEhdr
->e_machine
);
695 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
696 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
699 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
700 NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
701 mImageTimeStamp
= NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
;
702 NtHdr
->Pe32Plus
.FileHeader
.PointerToSymbolTable
= 0;
703 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSymbols
= 0;
704 NtHdr
->Pe32Plus
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32Plus
.OptionalHeader
);
705 NtHdr
->Pe32Plus
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
706 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
707 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
708 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE
;
710 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
711 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
712 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfUninitializedData
= 0;
713 NtHdr
->Pe32Plus
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
715 NtHdr
->Pe32Plus
.OptionalHeader
.BaseOfCode
= mTextOffset
;
717 NtHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= 0;
718 NtHdr
->Pe32Plus
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
719 NtHdr
->Pe32Plus
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
720 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= 0;
722 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
723 NtHdr
->Pe32Plus
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
728 if ((mDataOffset
- mTextOffset
) > 0) {
729 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
730 EFI_IMAGE_SCN_CNT_CODE
731 | EFI_IMAGE_SCN_MEM_EXECUTE
732 | EFI_IMAGE_SCN_MEM_READ
);
734 // Don't make a section of size 0.
735 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
738 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
739 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
740 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
741 | EFI_IMAGE_SCN_MEM_WRITE
742 | EFI_IMAGE_SCN_MEM_READ
);
744 // Don't make a section of size 0.
745 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
748 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
749 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
750 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
751 | EFI_IMAGE_SCN_MEM_READ
);
753 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
754 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
756 // Don't make a section of size 0.
757 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
765 SECTION_FILTER_TYPES FilterType
771 BOOLEAN (*Filter
)(Elf_Shdr
*);
772 Elf64_Addr GOTEntryRva
;
775 // Initialize filter pointer
777 switch (FilterType
) {
782 Filter
= IsHiiRsrcShdr
;
792 // First: copy sections.
794 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
795 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
796 if ((*Filter
)(Shdr
)) {
797 switch (Shdr
->sh_type
) {
800 if (Shdr
->sh_offset
+ Shdr
->sh_size
> mFileBufferSize
) {
803 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
804 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
805 (size_t) Shdr
->sh_size
);
809 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, (size_t) Shdr
->sh_size
);
814 // Ignore for unknown section type.
816 VerboseMsg ("%s unknown section type %x. We ignore this unknown section type.", mInImageName
, (unsigned)Shdr
->sh_type
);
823 // Second: apply relocations.
825 VerboseMsg ("Applying Relocations...");
826 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
828 // Determine if this is a relocation section.
830 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
831 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
836 // If this is a ET_DYN (PIE) executable, we will encounter a dynamic SHT_RELA
837 // section that applies to the entire binary, and which will have its section
838 // index set to #0 (which is a NULL section with the SHF_ALLOC bit cleared).
840 // In the absence of GOT based relocations,
841 // this RELA section will contain redundant R_xxx_RELATIVE relocations, one
842 // for every R_xxx_xx64 relocation appearing in the per-section RELA sections.
843 // (i.e., .rela.text and .rela.data)
845 if (RelShdr
->sh_info
== 0) {
850 // Relocation section found. Now extract section information that the relocations
851 // apply to in the ELF data and the new COFF data.
853 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
854 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
857 // Only process relocations for the current filter type.
859 if (RelShdr
->sh_type
== SHT_RELA
&& (*Filter
)(SecShdr
)) {
863 // Determine the symbol table referenced by the relocation data.
865 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
866 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
869 // Process all relocation entries for this section.
871 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= (UINT32
) RelShdr
->sh_entsize
) {
874 // Set pointer to relocation entry
876 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
879 // Set pointer to symbol table entry associated with the relocation entry.
881 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
887 // Check section header index found in symbol table and get the section
890 if (Sym
->st_shndx
== SHN_UNDEF
891 || Sym
->st_shndx
>= mEhdr
->e_shnum
) {
892 const UINT8
*SymName
= GetSymName(Sym
);
893 if (SymName
== NULL
) {
894 SymName
= (const UINT8
*)"<unknown>";
897 Error (NULL
, 0, 3000, "Invalid",
898 "%s: Bad definition for symbol '%s'@%#llx or unsupported symbol type. "
899 "For example, absolute and undefined symbols are not supported.",
900 mInImageName
, SymName
, Sym
->st_value
);
904 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
907 // Convert the relocation data to a pointer into the coff file.
910 // r_offset is the virtual address of the storage unit to be relocated.
911 // sh_addr is the virtual address for the base of the section.
913 // r_offset in a memory address.
914 // Convert it to a pointer in the coff file.
916 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
919 // Determine how to handle each relocation type based on the machine type.
921 if (mEhdr
->e_machine
== EM_X86_64
) {
922 switch (ELF_R_TYPE(Rel
->r_info
)) {
927 // Absolute relocation.
929 VerboseMsg ("R_X86_64_64");
930 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
931 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
933 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
934 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
937 VerboseMsg ("R_X86_64_32");
938 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
939 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
941 *(UINT32
*)Targ
= (UINT32
)((UINT64
)(*(UINT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
942 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
945 VerboseMsg ("R_X86_64_32S");
946 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
947 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
949 *(INT32
*)Targ
= (INT32
)((INT64
)(*(INT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
950 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
955 // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is
956 // possible since we know all code symbol references resolve to
957 // definitions in the same module (UEFI has no shared libraries),
958 // and so there is never a reason to jump via a PLT entry,
959 // allowing us to resolve the reference using the symbol directly.
961 VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");
965 // Relative relocation: Symbol - Ip + Addend
967 VerboseMsg ("R_X86_64_PC32");
968 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
969 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
971 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
972 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
973 - (SecOffset
- SecShdr
->sh_addr
));
974 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
976 case R_X86_64_GOTPCREL
:
977 case R_X86_64_GOTPCRELX
:
978 case R_X86_64_REX_GOTPCRELX
:
979 VerboseMsg ("R_X86_64_GOTPCREL family");
980 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
981 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
983 GOTEntryRva
= Rel
->r_offset
- Rel
->r_addend
+ *(INT32
*)Targ
;
984 FindElfGOTSectionFromGOTEntryElfRva(GOTEntryRva
);
985 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
986 + (mCoffSectionsOffset
[mGOTShindex
] - mGOTShdr
->sh_addr
)
987 - (SecOffset
- SecShdr
->sh_addr
));
988 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
989 GOTEntryRva
+= (mCoffSectionsOffset
[mGOTShindex
] - mGOTShdr
->sh_addr
); // ELF Rva -> COFF Rva
990 if (AccumulateCoffGOTEntries((UINT32
)GOTEntryRva
)) {
992 // Relocate GOT entry if it's the first time we run into it
994 Targ
= mCoffFile
+ GOTEntryRva
;
996 // Limitation: The following three statements assume memory
997 // at *Targ is valid because the section containing the GOT
998 // has already been copied from the ELF image to the Coff image.
999 // This pre-condition presently holds because the GOT is placed
1000 // in section .text, and the ELF text sections are all copied
1001 // prior to reaching this point.
1002 // If the pre-condition is violated in the future, this fixup
1003 // either needs to be deferred after the GOT section is copied
1004 // to the Coff image, or the fixup should be performed on the
1005 // source Elf image instead of the destination Coff image.
1007 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1008 (UINT32
)GOTEntryRva
,
1010 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1011 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
1015 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1017 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
1019 switch (ELF_R_TYPE(Rel
->r_info
)) {
1022 case R_AARCH64_LD64_GOT_LO12_NC
:
1024 // Convert into an ADD instruction - see R_AARCH64_ADR_GOT_PAGE below.
1026 *(UINT32
*)Targ
&= 0x3ff;
1027 *(UINT32
*)Targ
|= 0x91000000 | ((Sym
->st_value
& 0xfff) << 10);
1030 case R_AARCH64_ADR_GOT_PAGE
:
1032 // This relocation points to the GOT entry that contains the absolute
1033 // address of the symbol we are referring to. Since EDK2 only uses
1034 // fully linked binaries, we can avoid the indirection, and simply
1035 // refer to the symbol directly. This implies having to patch the
1036 // subsequent LDR instruction (covered by a R_AARCH64_LD64_GOT_LO12_NC
1037 // relocation) into an ADD instruction - this is handled above.
1039 Offset
= (Sym
->st_value
- (Rel
->r_offset
& ~0xfff)) >> 12;
1041 *(UINT32
*)Targ
&= 0x9000001f;
1042 *(UINT32
*)Targ
|= ((Offset
& 0x1ffffc) << (5 - 2)) | ((Offset
& 0x3) << 29);
1046 case R_AARCH64_ADR_PREL_PG_HI21
:
1048 // AArch64 PG_H21 relocations are typically paired with ABS_LO12
1049 // relocations, where a PC-relative reference with +/- 4 GB range is
1050 // split into a relative high part and an absolute low part. Since
1051 // the absolute low part represents the offset into a 4 KB page, we
1052 // either have to convert the ADRP into an ADR instruction, or we
1053 // need to use a section alignment of at least 4 KB, so that the
1054 // binary appears at a correct offset at runtime. In any case, we
1055 // have to make sure that the 4 KB relative offsets of both the
1056 // section containing the reference as well as the section to which
1057 // it refers have not been changed during PE/COFF conversion (i.e.,
1058 // in ScanSections64() above).
1060 if (mCoffAlignment
< 0x1000) {
1062 // Attempt to convert the ADRP into an ADR instruction.
1063 // This is only possible if the symbol is within +/- 1 MB.
1066 // Decode the ADRP instruction
1067 Offset
= (INT32
)((*(UINT32
*)Targ
& 0xffffe0) << 8);
1068 Offset
= (Offset
<< (6 - 5)) | ((*(UINT32
*)Targ
& 0x60000000) >> (29 - 12));
1071 // ADRP offset is relative to the previous page boundary,
1072 // whereas ADR offset is relative to the instruction itself.
1073 // So fix up the offset so it points to the page containing
1076 Offset
-= (UINTN
)(Targ
- mCoffFile
) & 0xfff;
1078 if (Offset
< -0x100000 || Offset
> 0xfffff) {
1079 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s due to its size (> 1 MB), this module requires 4 KB section alignment.",
1084 // Re-encode the offset as an ADR instruction
1085 *(UINT32
*)Targ
&= 0x1000001f;
1086 *(UINT32
*)Targ
|= ((Offset
& 0x1ffffc) << (5 - 2)) | ((Offset
& 0x3) << 29);
1090 case R_AARCH64_ADD_ABS_LO12_NC
:
1091 case R_AARCH64_LDST8_ABS_LO12_NC
:
1092 case R_AARCH64_LDST16_ABS_LO12_NC
:
1093 case R_AARCH64_LDST32_ABS_LO12_NC
:
1094 case R_AARCH64_LDST64_ABS_LO12_NC
:
1095 case R_AARCH64_LDST128_ABS_LO12_NC
:
1096 if (((SecShdr
->sh_addr
^ SecOffset
) & 0xfff) != 0 ||
1097 ((SymShdr
->sh_addr
^ mCoffSectionsOffset
[Sym
->st_shndx
]) & 0xfff) != 0) {
1098 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 small code model requires identical ELF and PE/COFF section offsets modulo 4 KB.",
1104 case R_AARCH64_ADR_PREL_LO21
:
1105 case R_AARCH64_CONDBR19
:
1106 case R_AARCH64_LD_PREL_LO19
:
1107 case R_AARCH64_CALL26
:
1108 case R_AARCH64_JUMP26
:
1109 case R_AARCH64_PREL64
:
1110 case R_AARCH64_PREL32
:
1111 case R_AARCH64_PREL16
:
1113 // The GCC toolchains (i.e., binutils) may corrupt section relative
1114 // relocations when emitting relocation sections into fully linked
1115 // binaries. More specifically, they tend to fail to take into
1116 // account the fact that a '.rodata + XXX' relocation needs to have
1117 // its addend recalculated once .rodata is merged into the .text
1118 // section, and the relocation emitted into the .rela.text section.
1120 // We cannot really recover from this loss of information, so the
1121 // only workaround is to prevent having to recalculate any relative
1122 // relocations at all, by using a linker script that ensures that
1123 // the offset between the Place and the Symbol is the same in both
1124 // the ELF and the PE/COFF versions of the binary.
1126 if ((SymShdr
->sh_addr
- SecShdr
->sh_addr
) !=
1127 (mCoffSectionsOffset
[Sym
->st_shndx
] - SecOffset
)) {
1128 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 relative relocations require identical ELF and PE/COFF section offsets",
1133 // Absolute relocations.
1134 case R_AARCH64_ABS64
:
1135 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1139 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1142 Error (NULL
, 0, 3000, "Invalid", "Not a supported machine type");
1153 WriteRelocations64 (
1158 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1159 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
1161 for (Index
= 0; Index
< mEhdr
->e_shnum
; Index
++) {
1162 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
1163 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
1164 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
1165 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
1168 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
1169 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
1171 if (mEhdr
->e_machine
== EM_X86_64
) {
1172 switch (ELF_R_TYPE(Rel
->r_info
)) {
1175 case R_X86_64_PLT32
:
1176 case R_X86_64_GOTPCREL
:
1177 case R_X86_64_GOTPCRELX
:
1178 case R_X86_64_REX_GOTPCRELX
:
1181 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",
1182 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
1184 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1185 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1186 EFI_IMAGE_REL_BASED_DIR64
);
1189 // R_X86_64_32 and R_X86_64_32S are ELF64 relocations emitted when using
1190 // the SYSV X64 ABI small non-position-independent code model.
1191 // R_X86_64_32 is used for unsigned 32-bit immediates with a 32-bit operand
1192 // size. The value is either not extended, or zero-extended to 64 bits.
1193 // R_X86_64_32S is used for either signed 32-bit non-rip-relative displacements
1194 // or signed 32-bit immediates with a 64-bit operand size. The value is
1195 // sign-extended to 64 bits.
1196 // EFI_IMAGE_REL_BASED_HIGHLOW is a PE relocation that uses 32-bit arithmetic
1197 // for rebasing an image.
1198 // EFI PE binaries declare themselves EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE and
1199 // may load above 2GB. If an EFI PE binary with a converted R_X86_64_32S
1200 // relocation is loaded above 2GB, the value will get sign-extended to the
1201 // negative part of the 64-bit address space. The negative part of the 64-bit
1202 // address space is unmapped, so accessing such an address page-faults.
1203 // In order to support R_X86_64_32S, it is necessary to unset
1204 // EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE, and the EFI PE loader must implement
1205 // this flag and abstain from loading such a PE binary above 2GB.
1206 // Since this feature is not supported, support for R_X86_64_32S (and hence
1207 // the small non-position-independent code model) is disabled.
1209 // case R_X86_64_32S:
1211 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",
1212 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
1214 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1215 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1216 EFI_IMAGE_REL_BASED_HIGHLOW
);
1219 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1221 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
1223 switch (ELF_R_TYPE(Rel
->r_info
)) {
1224 case R_AARCH64_ADR_PREL_LO21
:
1225 case R_AARCH64_CONDBR19
:
1226 case R_AARCH64_LD_PREL_LO19
:
1227 case R_AARCH64_CALL26
:
1228 case R_AARCH64_JUMP26
:
1229 case R_AARCH64_PREL64
:
1230 case R_AARCH64_PREL32
:
1231 case R_AARCH64_PREL16
:
1232 case R_AARCH64_ADR_PREL_PG_HI21
:
1233 case R_AARCH64_ADD_ABS_LO12_NC
:
1234 case R_AARCH64_LDST8_ABS_LO12_NC
:
1235 case R_AARCH64_LDST16_ABS_LO12_NC
:
1236 case R_AARCH64_LDST32_ABS_LO12_NC
:
1237 case R_AARCH64_LDST64_ABS_LO12_NC
:
1238 case R_AARCH64_LDST128_ABS_LO12_NC
:
1239 case R_AARCH64_ADR_GOT_PAGE
:
1240 case R_AARCH64_LD64_GOT_LO12_NC
:
1242 // No fixups are required for relative relocations, provided that
1243 // the relative offsets between sections have been preserved in
1244 // the ELF to PE/COFF conversion. We have already asserted that
1245 // this is the case in WriteSections64 ().
1249 case R_AARCH64_ABS64
:
1251 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1252 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1253 EFI_IMAGE_REL_BASED_DIR64
);
1256 case R_AARCH64_ABS32
:
1258 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1259 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1260 EFI_IMAGE_REL_BASED_HIGHLOW
);
1264 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1267 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
1270 if (mEhdr
->e_machine
== EM_X86_64
&& RelShdr
->sh_info
== mGOTShindex
) {
1272 // Tack relocations for GOT entries after other relocations for
1273 // the section the GOT is in, as it's usually found at the end
1274 // of the section. This is done in order to maintain Rva order
1275 // of Coff relocations.
1277 EmitGOTRelocations();
1283 if (mEhdr
->e_machine
== EM_X86_64
) {
1285 // This is a safety net just in case the GOT is in a section
1286 // with no other relocations and the first invocation of
1287 // EmitGOTRelocations() above was skipped. This invocation
1288 // does not maintain Rva order of Coff relocations.
1289 // At present, with a single text section, all references to
1290 // the GOT and the GOT itself reside in section .text, so
1291 // if there's a GOT at all, the first invocation above
1294 EmitGOTRelocations();
1297 // Pad by adding empty entries.
1299 while (mCoffOffset
& (mCoffAlignment
- 1)) {
1300 CoffAddFixupEntry(0);
1303 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1304 Dir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1305 Dir
->Size
= mCoffOffset
- mRelocOffset
;
1306 if (Dir
->Size
== 0) {
1307 // If no relocations, null out the directory entry and don't add the .reloc section
1308 Dir
->VirtualAddress
= 0;
1309 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
1311 Dir
->VirtualAddress
= mRelocOffset
;
1312 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
1313 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1314 | EFI_IMAGE_SCN_MEM_DISCARDABLE
1315 | EFI_IMAGE_SCN_MEM_READ
);
1326 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1327 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
1328 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
1329 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
1331 Len
= strlen(mInImageName
) + 1;
1333 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ mDebugOffset
);
1334 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
1335 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
1336 Dir
->RVA
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1337 Dir
->FileOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1339 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
1340 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
1341 strcpy ((char *)(Nb10
+ 1), mInImageName
);
1344 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1345 DataDir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
1346 DataDir
->VirtualAddress
= mDebugOffset
;
1347 DataDir
->Size
= sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1356 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1361 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1362 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
1371 if (mCoffSectionsOffset
!= NULL
) {
1372 free (mCoffSectionsOffset
);