4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License which accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "WinNtInclude.h"
30 #include <Common/UefiBaseTypes.h>
31 #include <IndustryStandard/PeImage.h>
33 #include "PeCoffLib.h"
34 #include "EfiUtilityMsgs.h"
37 #include "ElfConvert.h"
38 #include "Elf64Convert.h"
49 SECTION_FILTER_TYPES FilterType
77 // Rename ELF32 strucutres to common names to help when porting to ELF64.
79 typedef Elf64_Shdr Elf_Shdr
;
80 typedef Elf64_Ehdr Elf_Ehdr
;
81 typedef Elf64_Rel Elf_Rel
;
82 typedef Elf64_Rela Elf_Rela
;
83 typedef Elf64_Sym Elf_Sym
;
84 typedef Elf64_Phdr Elf_Phdr
;
85 typedef Elf64_Dyn Elf_Dyn
;
86 #define ELFCLASS ELFCLASS64
87 #define ELF_R_TYPE(r) ELF64_R_TYPE(r)
88 #define ELF_R_SYM(r) ELF64_R_SYM(r)
91 // Well known ELF structures.
93 STATIC Elf_Ehdr
*mEhdr
;
94 STATIC Elf_Shdr
*mShdrBase
;
95 STATIC Elf_Phdr
*mPhdrBase
;
100 STATIC Elf_Shdr
*mGOTShdr
= NULL
;
101 STATIC UINT32 mGOTShindex
= 0;
102 STATIC UINT32
*mGOTCoffEntries
= NULL
;
103 STATIC UINT32 mGOTMaxCoffEntries
= 0;
104 STATIC UINT32 mGOTNumCoffEntries
= 0;
109 STATIC UINT32 mCoffAlignment
= 0x20;
112 // PE section alignment.
114 STATIC
const UINT16 mCoffNbrSections
= 4;
117 // ELF sections to offset in Coff file.
119 STATIC UINT32
*mCoffSectionsOffset
= NULL
;
122 // Offsets in COFF file
124 STATIC UINT32 mNtHdrOffset
;
125 STATIC UINT32 mTextOffset
;
126 STATIC UINT32 mDataOffset
;
127 STATIC UINT32 mHiiRsrcOffset
;
128 STATIC UINT32 mRelocOffset
;
129 STATIC UINT32 mDebugOffset
;
132 // Initialization Function
137 ELF_FUNCTION_TABLE
*ElfFunctions
141 // Initialize data pointer and structures.
143 VerboseMsg ("Set EHDR");
144 mEhdr
= (Elf_Ehdr
*) FileBuffer
;
147 // Check the ELF64 specific header information.
149 VerboseMsg ("Check ELF64 Header Information");
150 if (mEhdr
->e_ident
[EI_CLASS
] != ELFCLASS64
) {
151 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS64");
154 if (mEhdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
155 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
158 if ((mEhdr
->e_type
!= ET_EXEC
) && (mEhdr
->e_type
!= ET_DYN
)) {
159 Error (NULL
, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
162 if (!((mEhdr
->e_machine
== EM_X86_64
) || (mEhdr
->e_machine
== EM_AARCH64
))) {
163 Error (NULL
, 0, 3000, "Unsupported", "ELF e_machine not EM_X86_64 or EM_AARCH64");
166 if (mEhdr
->e_version
!= EV_CURRENT
) {
167 Error (NULL
, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr
->e_version
, EV_CURRENT
);
172 // Update section header pointers
174 VerboseMsg ("Update Header Pointers");
175 mShdrBase
= (Elf_Shdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_shoff
);
176 mPhdrBase
= (Elf_Phdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_phoff
);
179 // Create COFF Section offset buffer and zero.
181 VerboseMsg ("Create COFF Section Offset Buffer");
182 mCoffSectionsOffset
= (UINT32
*)malloc(mEhdr
->e_shnum
* sizeof (UINT32
));
183 if (mCoffSectionsOffset
== NULL
) {
184 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
187 memset(mCoffSectionsOffset
, 0, mEhdr
->e_shnum
* sizeof(UINT32
));
190 // Fill in function pointers.
192 VerboseMsg ("Fill in Function Pointers");
193 ElfFunctions
->ScanSections
= ScanSections64
;
194 ElfFunctions
->WriteSections
= WriteSections64
;
195 ElfFunctions
->WriteRelocations
= WriteRelocations64
;
196 ElfFunctions
->WriteDebug
= WriteDebug64
;
197 ElfFunctions
->SetImageSize
= SetImageSize64
;
198 ElfFunctions
->CleanUp
= CleanUp64
;
205 // Header by Index functions
213 if (Num
>= mEhdr
->e_shnum
) {
214 Error (NULL
, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num
);
218 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
227 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
236 return (Offset
+ 3) & ~3;
248 return (BOOLEAN
) ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
);
257 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
259 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
268 if (IsHiiRsrcShdr(Shdr
)) {
271 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
280 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
282 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_STRTAB_SECTION_NAME
) == 0);
292 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
293 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
294 if (IsStrtabShdr(shdr
)) {
307 Elf_Shdr
*StrtabShdr
;
308 UINT8
*StrtabContents
;
312 if (Sym
->st_name
== 0) {
316 StrtabShdr
= FindStrtabShdr();
317 if (StrtabShdr
== NULL
) {
321 assert(Sym
->st_name
< StrtabShdr
->sh_size
);
323 StrtabContents
= (UINT8
*)mEhdr
+ StrtabShdr
->sh_offset
;
326 for (i
= Sym
->st_name
; (i
< StrtabShdr
->sh_size
) && !foundEnd
; i
++) {
327 foundEnd
= (BOOLEAN
)(StrtabContents
[i
] == 0);
331 return StrtabContents
+ Sym
->st_name
;
335 // Find the ELF section hosting the GOT from an ELF Rva
336 // of a single GOT entry. Normally, GOT is placed in
337 // ELF .text section, so assume once we find in which
338 // section the GOT is, all GOT entries are there, and
343 FindElfGOTSectionFromGOTEntryElfRva (
344 Elf64_Addr GOTEntryElfRva
348 if (mGOTShdr
!= NULL
) {
349 if (GOTEntryElfRva
>= mGOTShdr
->sh_addr
&&
350 GOTEntryElfRva
< mGOTShdr
->sh_addr
+ mGOTShdr
->sh_size
) {
353 Error (NULL
, 0, 3000, "Unsupported", "FindElfGOTSectionFromGOTEntryElfRva: GOT entries found in multiple sections.");
356 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
357 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
358 if (GOTEntryElfRva
>= shdr
->sh_addr
&&
359 GOTEntryElfRva
< shdr
->sh_addr
+ shdr
->sh_size
) {
365 Error (NULL
, 0, 3000, "Invalid", "FindElfGOTSectionFromGOTEntryElfRva: ElfRva 0x%016LX for GOT entry not found in any section.", GOTEntryElfRva
);
370 // Stores locations of GOT entries in COFF image.
371 // Returns TRUE if GOT entry is new.
372 // Simple implementation as number of GOT
373 // entries is expected to be low.
378 AccumulateCoffGOTEntries (
383 if (mGOTCoffEntries
!= NULL
) {
384 for (i
= 0; i
< mGOTNumCoffEntries
; i
++) {
385 if (mGOTCoffEntries
[i
] == GOTCoffEntry
) {
390 if (mGOTCoffEntries
== NULL
) {
391 mGOTCoffEntries
= (UINT32
*)malloc(5 * sizeof *mGOTCoffEntries
);
392 if (mGOTCoffEntries
== NULL
) {
393 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
395 assert (mGOTCoffEntries
!= NULL
);
396 mGOTMaxCoffEntries
= 5;
397 mGOTNumCoffEntries
= 0;
398 } else if (mGOTNumCoffEntries
== mGOTMaxCoffEntries
) {
399 mGOTCoffEntries
= (UINT32
*)realloc(mGOTCoffEntries
, 2 * mGOTMaxCoffEntries
* sizeof *mGOTCoffEntries
);
400 if (mGOTCoffEntries
== NULL
) {
401 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
403 assert (mGOTCoffEntries
!= NULL
);
404 mGOTMaxCoffEntries
+= mGOTMaxCoffEntries
;
406 mGOTCoffEntries
[mGOTNumCoffEntries
++] = GOTCoffEntry
;
411 // 32-bit Unsigned integer comparator for qsort.
420 if (*(const UINT32
*)lhs
< *(const UINT32
*)rhs
) {
423 return *(const UINT32
*)lhs
> *(const UINT32
*)rhs
;
427 // Emit accumulated Coff GOT entry relocations into
428 // Coff image. This function performs its job
429 // once and then releases the entry list, so
430 // it can safely be called multiple times.
439 if (mGOTCoffEntries
== NULL
) {
443 // Emit Coff relocations with Rvas ordered.
448 sizeof *mGOTCoffEntries
,
450 for (i
= 0; i
< mGOTNumCoffEntries
; i
++) {
451 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X", mGOTCoffEntries
[i
]);
454 EFI_IMAGE_REL_BASED_DIR64
);
456 free(mGOTCoffEntries
);
457 mGOTCoffEntries
= NULL
;
458 mGOTMaxCoffEntries
= 0;
459 mGOTNumCoffEntries
= 0;
463 // Elf functions interface implementation
473 EFI_IMAGE_DOS_HEADER
*DosHdr
;
474 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
477 BOOLEAN FoundSection
;
483 // Coff file start with a DOS header.
485 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
486 mNtHdrOffset
= mCoffOffset
;
487 switch (mEhdr
->e_machine
) {
491 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
494 VerboseMsg ("%s unknown e_machine type %hu. Assume X64", mInImageName
, mEhdr
->e_machine
);
495 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
499 mTableOffset
= mCoffOffset
;
500 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
503 // Set mCoffAlignment to the maximum alignment of the input sections
506 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
507 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
508 if (shdr
->sh_addralign
<= mCoffAlignment
) {
511 if (IsTextShdr(shdr
) || IsDataShdr(shdr
) || IsHiiRsrcShdr(shdr
)) {
512 mCoffAlignment
= (UINT32
)shdr
->sh_addralign
;
517 // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT
519 if (mCoffAlignment
> MAX_COFF_ALIGNMENT
) {
520 Error (NULL
, 0, 3000, "Invalid", "Section alignment is larger than MAX_COFF_ALIGNMENT.");
526 // Move the PE/COFF header right before the first section. This will help us
527 // save space when converting to TE.
529 if (mCoffAlignment
> mCoffOffset
) {
530 mNtHdrOffset
+= mCoffAlignment
- mCoffOffset
;
531 mTableOffset
+= mCoffAlignment
- mCoffOffset
;
532 mCoffOffset
= mCoffAlignment
;
536 // First text sections.
538 mCoffOffset
= CoffAlign(mCoffOffset
);
539 mTextOffset
= mCoffOffset
;
540 FoundSection
= FALSE
;
542 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
543 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
544 if (IsTextShdr(shdr
)) {
545 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
546 // the alignment field is valid
547 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
548 // if the section address is aligned we must align PE/COFF
549 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
551 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
555 /* Relocate entry. */
556 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
557 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
558 CoffEntry
= (UINT32
) (mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
);
562 // Set mTextOffset with the offset of the first '.text' section
565 mTextOffset
= mCoffOffset
;
569 mCoffSectionsOffset
[i
] = mCoffOffset
;
570 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
576 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
580 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
581 mCoffOffset
= CoffAlign(mCoffOffset
);
583 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
584 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
588 // Then data sections.
590 mDataOffset
= mCoffOffset
;
591 FoundSection
= FALSE
;
593 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
594 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
595 if (IsDataShdr(shdr
)) {
596 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
597 // the alignment field is valid
598 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
599 // if the section address is aligned we must align PE/COFF
600 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
602 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
607 // Set mDataOffset with the offset of the first '.data' section
610 mDataOffset
= mCoffOffset
;
613 mCoffSectionsOffset
[i
] = mCoffOffset
;
614 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
620 // Make room for .debug data in .data (or .text if .data is empty) instead of
621 // putting it in a section of its own. This is explicitly allowed by the
622 // PE/COFF spec, and prevents bloat in the binary when using large values for
623 // section alignment.
625 if (SectionCount
> 0) {
626 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
628 mCoffOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
) +
629 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) +
630 strlen(mInImageName
) + 1;
632 mCoffOffset
= CoffAlign(mCoffOffset
);
633 if (SectionCount
== 0) {
634 mDataOffset
= mCoffOffset
;
637 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
638 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
642 // The HII resource sections.
644 mHiiRsrcOffset
= mCoffOffset
;
645 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
646 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
647 if (IsHiiRsrcShdr(shdr
)) {
648 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
649 // the alignment field is valid
650 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
651 // if the section address is aligned we must align PE/COFF
652 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
654 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
657 if (shdr
->sh_size
!= 0) {
658 mHiiRsrcOffset
= mCoffOffset
;
659 mCoffSectionsOffset
[i
] = mCoffOffset
;
660 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
661 mCoffOffset
= CoffAlign(mCoffOffset
);
662 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
668 mRelocOffset
= mCoffOffset
;
671 // Allocate base Coff file. Will be expanded later for relocations.
673 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
674 if (mCoffFile
== NULL
) {
675 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
677 assert (mCoffFile
!= NULL
);
678 memset(mCoffFile
, 0, mCoffOffset
);
683 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
684 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
685 DosHdr
->e_lfanew
= mNtHdrOffset
;
687 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
689 NtHdr
->Pe32Plus
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
691 switch (mEhdr
->e_machine
) {
693 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
694 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
697 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IPF
;
698 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
701 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_AARCH64
;
702 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
705 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN
)mEhdr
->e_machine
);
706 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
707 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
710 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
711 NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
712 mImageTimeStamp
= NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
;
713 NtHdr
->Pe32Plus
.FileHeader
.PointerToSymbolTable
= 0;
714 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSymbols
= 0;
715 NtHdr
->Pe32Plus
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32Plus
.OptionalHeader
);
716 NtHdr
->Pe32Plus
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
717 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
718 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
719 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE
;
721 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
722 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
723 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfUninitializedData
= 0;
724 NtHdr
->Pe32Plus
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
726 NtHdr
->Pe32Plus
.OptionalHeader
.BaseOfCode
= mTextOffset
;
728 NtHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= 0;
729 NtHdr
->Pe32Plus
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
730 NtHdr
->Pe32Plus
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
731 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= 0;
733 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
734 NtHdr
->Pe32Plus
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
739 if ((mDataOffset
- mTextOffset
) > 0) {
740 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
741 EFI_IMAGE_SCN_CNT_CODE
742 | EFI_IMAGE_SCN_MEM_EXECUTE
743 | EFI_IMAGE_SCN_MEM_READ
);
745 // Don't make a section of size 0.
746 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
749 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
750 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
751 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
752 | EFI_IMAGE_SCN_MEM_WRITE
753 | EFI_IMAGE_SCN_MEM_READ
);
755 // Don't make a section of size 0.
756 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
759 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
760 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
761 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
762 | EFI_IMAGE_SCN_MEM_READ
);
764 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
765 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
767 // Don't make a section of size 0.
768 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
776 SECTION_FILTER_TYPES FilterType
782 BOOLEAN (*Filter
)(Elf_Shdr
*);
783 Elf64_Addr GOTEntryRva
;
786 // Initialize filter pointer
788 switch (FilterType
) {
793 Filter
= IsHiiRsrcShdr
;
803 // First: copy sections.
805 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
806 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
807 if ((*Filter
)(Shdr
)) {
808 switch (Shdr
->sh_type
) {
811 if (Shdr
->sh_offset
+ Shdr
->sh_size
> mFileBufferSize
) {
814 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
815 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
816 (size_t) Shdr
->sh_size
);
820 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, (size_t) Shdr
->sh_size
);
825 // Ignore for unkown section type.
827 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName
, (unsigned)Shdr
->sh_type
);
834 // Second: apply relocations.
836 VerboseMsg ("Applying Relocations...");
837 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
839 // Determine if this is a relocation section.
841 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
842 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
847 // If this is a ET_DYN (PIE) executable, we will encounter a dynamic SHT_RELA
848 // section that applies to the entire binary, and which will have its section
849 // index set to #0 (which is a NULL section with the SHF_ALLOC bit cleared).
851 // In the absence of GOT based relocations,
852 // this RELA section will contain redundant R_xxx_RELATIVE relocations, one
853 // for every R_xxx_xx64 relocation appearing in the per-section RELA sections.
854 // (i.e., .rela.text and .rela.data)
856 if (RelShdr
->sh_info
== 0) {
861 // Relocation section found. Now extract section information that the relocations
862 // apply to in the ELF data and the new COFF data.
864 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
865 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
868 // Only process relocations for the current filter type.
870 if (RelShdr
->sh_type
== SHT_RELA
&& (*Filter
)(SecShdr
)) {
874 // Determine the symbol table referenced by the relocation data.
876 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
877 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
880 // Process all relocation entries for this section.
882 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= (UINT32
) RelShdr
->sh_entsize
) {
885 // Set pointer to relocation entry
887 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
890 // Set pointer to symbol table entry associated with the relocation entry.
892 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
898 // Check section header index found in symbol table and get the section
901 if (Sym
->st_shndx
== SHN_UNDEF
902 || Sym
->st_shndx
>= mEhdr
->e_shnum
) {
903 const UINT8
*SymName
= GetSymName(Sym
);
904 if (SymName
== NULL
) {
905 SymName
= (const UINT8
*)"<unknown>";
908 Error (NULL
, 0, 3000, "Invalid",
909 "%s: Bad definition for symbol '%s'@%#llx or unsupported symbol type. "
910 "For example, absolute and undefined symbols are not supported.",
911 mInImageName
, SymName
, Sym
->st_value
);
915 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
918 // Convert the relocation data to a pointer into the coff file.
921 // r_offset is the virtual address of the storage unit to be relocated.
922 // sh_addr is the virtual address for the base of the section.
924 // r_offset in a memory address.
925 // Convert it to a pointer in the coff file.
927 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
930 // Determine how to handle each relocation type based on the machine type.
932 if (mEhdr
->e_machine
== EM_X86_64
) {
933 switch (ELF_R_TYPE(Rel
->r_info
)) {
938 // Absolute relocation.
940 VerboseMsg ("R_X86_64_64");
941 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
942 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
944 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
945 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
948 VerboseMsg ("R_X86_64_32");
949 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
950 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
952 *(UINT32
*)Targ
= (UINT32
)((UINT64
)(*(UINT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
953 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
956 VerboseMsg ("R_X86_64_32S");
957 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
958 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
960 *(INT32
*)Targ
= (INT32
)((INT64
)(*(INT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
961 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
966 // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is
967 // possible since we know all code symbol references resolve to
968 // definitions in the same module (UEFI has no shared libraries),
969 // and so there is never a reason to jump via a PLT entry,
970 // allowing us to resolve the reference using the symbol directly.
972 VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");
976 // Relative relocation: Symbol - Ip + Addend
978 VerboseMsg ("R_X86_64_PC32");
979 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
980 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
982 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
983 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
984 - (SecOffset
- SecShdr
->sh_addr
));
985 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
987 case R_X86_64_GOTPCREL
:
988 case R_X86_64_GOTPCRELX
:
989 case R_X86_64_REX_GOTPCRELX
:
990 VerboseMsg ("R_X86_64_GOTPCREL family");
991 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
992 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
994 GOTEntryRva
= Rel
->r_offset
- Rel
->r_addend
+ *(INT32
*)Targ
;
995 FindElfGOTSectionFromGOTEntryElfRva(GOTEntryRva
);
996 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
997 + (mCoffSectionsOffset
[mGOTShindex
] - mGOTShdr
->sh_addr
)
998 - (SecOffset
- SecShdr
->sh_addr
));
999 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1000 GOTEntryRva
+= (mCoffSectionsOffset
[mGOTShindex
] - mGOTShdr
->sh_addr
); // ELF Rva -> COFF Rva
1001 if (AccumulateCoffGOTEntries((UINT32
)GOTEntryRva
)) {
1003 // Relocate GOT entry if it's the first time we run into it
1005 Targ
= mCoffFile
+ GOTEntryRva
;
1007 // Limitation: The following three statements assume memory
1008 // at *Targ is valid because the section containing the GOT
1009 // has already been copied from the ELF image to the Coff image.
1010 // This pre-condition presently holds because the GOT is placed
1011 // in section .text, and the ELF text sections are all copied
1012 // prior to reaching this point.
1013 // If the pre-condition is violated in the future, this fixup
1014 // either needs to be deferred after the GOT section is copied
1015 // to the Coff image, or the fixup should be performed on the
1016 // source Elf image instead of the destination Coff image.
1018 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1019 (UINT32
)GOTEntryRva
,
1021 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1022 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
1026 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1028 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
1030 switch (ELF_R_TYPE(Rel
->r_info
)) {
1032 case R_AARCH64_ADR_PREL_PG_HI21
:
1034 // AArch64 PG_H21 relocations are typically paired with ABS_LO12
1035 // relocations, where a PC-relative reference with +/- 4 GB range is
1036 // split into a relative high part and an absolute low part. Since
1037 // the absolute low part represents the offset into a 4 KB page, we
1038 // either have to convert the ADRP into an ADR instruction, or we
1039 // need to use a section alignment of at least 4 KB, so that the
1040 // binary appears at a correct offset at runtime. In any case, we
1041 // have to make sure that the 4 KB relative offsets of both the
1042 // section containing the reference as well as the section to which
1043 // it refers have not been changed during PE/COFF conversion (i.e.,
1044 // in ScanSections64() above).
1046 if (mCoffAlignment
< 0x1000) {
1048 // Attempt to convert the ADRP into an ADR instruction.
1049 // This is only possible if the symbol is within +/- 1 MB.
1053 // Decode the ADRP instruction
1054 Offset
= (INT32
)((*(UINT32
*)Targ
& 0xffffe0) << 8);
1055 Offset
= (Offset
<< (6 - 5)) | ((*(UINT32
*)Targ
& 0x60000000) >> (29 - 12));
1058 // ADRP offset is relative to the previous page boundary,
1059 // whereas ADR offset is relative to the instruction itself.
1060 // So fix up the offset so it points to the page containing
1063 Offset
-= (UINTN
)(Targ
- mCoffFile
) & 0xfff;
1065 if (Offset
< -0x100000 || Offset
> 0xfffff) {
1066 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s due to its size (> 1 MB), this module requires 4 KB section alignment.",
1071 // Re-encode the offset as an ADR instruction
1072 *(UINT32
*)Targ
&= 0x1000001f;
1073 *(UINT32
*)Targ
|= ((Offset
& 0x1ffffc) << (5 - 2)) | ((Offset
& 0x3) << 29);
1077 case R_AARCH64_ADD_ABS_LO12_NC
:
1078 case R_AARCH64_LDST8_ABS_LO12_NC
:
1079 case R_AARCH64_LDST16_ABS_LO12_NC
:
1080 case R_AARCH64_LDST32_ABS_LO12_NC
:
1081 case R_AARCH64_LDST64_ABS_LO12_NC
:
1082 case R_AARCH64_LDST128_ABS_LO12_NC
:
1083 if (((SecShdr
->sh_addr
^ SecOffset
) & 0xfff) != 0 ||
1084 ((SymShdr
->sh_addr
^ mCoffSectionsOffset
[Sym
->st_shndx
]) & 0xfff) != 0) {
1085 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 small code model requires identical ELF and PE/COFF section offsets modulo 4 KB.",
1091 case R_AARCH64_ADR_PREL_LO21
:
1092 case R_AARCH64_CONDBR19
:
1093 case R_AARCH64_LD_PREL_LO19
:
1094 case R_AARCH64_CALL26
:
1095 case R_AARCH64_JUMP26
:
1096 case R_AARCH64_PREL64
:
1097 case R_AARCH64_PREL32
:
1098 case R_AARCH64_PREL16
:
1100 // The GCC toolchains (i.e., binutils) may corrupt section relative
1101 // relocations when emitting relocation sections into fully linked
1102 // binaries. More specifically, they tend to fail to take into
1103 // account the fact that a '.rodata + XXX' relocation needs to have
1104 // its addend recalculated once .rodata is merged into the .text
1105 // section, and the relocation emitted into the .rela.text section.
1107 // We cannot really recover from this loss of information, so the
1108 // only workaround is to prevent having to recalculate any relative
1109 // relocations at all, by using a linker script that ensures that
1110 // the offset between the Place and the Symbol is the same in both
1111 // the ELF and the PE/COFF versions of the binary.
1113 if ((SymShdr
->sh_addr
- SecShdr
->sh_addr
) !=
1114 (mCoffSectionsOffset
[Sym
->st_shndx
] - SecOffset
)) {
1115 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 relative relocations require identical ELF and PE/COFF section offsets",
1120 // Absolute relocations.
1121 case R_AARCH64_ABS64
:
1122 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1126 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1129 Error (NULL
, 0, 3000, "Invalid", "Not a supported machine type");
1140 WriteRelocations64 (
1145 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1146 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
1148 for (Index
= 0; Index
< mEhdr
->e_shnum
; Index
++) {
1149 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
1150 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
1151 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
1152 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
1155 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
1156 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
1158 if (mEhdr
->e_machine
== EM_X86_64
) {
1159 switch (ELF_R_TYPE(Rel
->r_info
)) {
1162 case R_X86_64_PLT32
:
1163 case R_X86_64_GOTPCREL
:
1164 case R_X86_64_GOTPCRELX
:
1165 case R_X86_64_REX_GOTPCRELX
:
1168 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",
1169 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
1171 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1172 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1173 EFI_IMAGE_REL_BASED_DIR64
);
1176 // R_X86_64_32 and R_X86_64_32S are ELF64 relocations emitted when using
1177 // the SYSV X64 ABI small non-position-independent code model.
1178 // R_X86_64_32 is used for unsigned 32-bit immediates with a 32-bit operand
1179 // size. The value is either not extended, or zero-extended to 64 bits.
1180 // R_X86_64_32S is used for either signed 32-bit non-rip-relative displacements
1181 // or signed 32-bit immediates with a 64-bit operand size. The value is
1182 // sign-extended to 64 bits.
1183 // EFI_IMAGE_REL_BASED_HIGHLOW is a PE relocation that uses 32-bit arithmetic
1184 // for rebasing an image.
1185 // EFI PE binaries declare themselves EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE and
1186 // may load above 2GB. If an EFI PE binary with a converted R_X86_64_32S
1187 // relocation is loaded above 2GB, the value will get sign-extended to the
1188 // negative part of the 64-bit address space. The negative part of the 64-bit
1189 // address space is unmapped, so accessing such an address page-faults.
1190 // In order to support R_X86_64_32S, it is necessary to unset
1191 // EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE, and the EFI PE loader must implement
1192 // this flag and abstain from loading such a PE binary above 2GB.
1193 // Since this feature is not supported, support for R_X86_64_32S (and hence
1194 // the small non-position-independent code model) is disabled.
1196 // case R_X86_64_32S:
1198 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",
1199 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
1201 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1202 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1203 EFI_IMAGE_REL_BASED_HIGHLOW
);
1206 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1208 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
1210 switch (ELF_R_TYPE(Rel
->r_info
)) {
1211 case R_AARCH64_ADR_PREL_LO21
:
1212 case R_AARCH64_CONDBR19
:
1213 case R_AARCH64_LD_PREL_LO19
:
1214 case R_AARCH64_CALL26
:
1215 case R_AARCH64_JUMP26
:
1216 case R_AARCH64_PREL64
:
1217 case R_AARCH64_PREL32
:
1218 case R_AARCH64_PREL16
:
1219 case R_AARCH64_ADR_PREL_PG_HI21
:
1220 case R_AARCH64_ADD_ABS_LO12_NC
:
1221 case R_AARCH64_LDST8_ABS_LO12_NC
:
1222 case R_AARCH64_LDST16_ABS_LO12_NC
:
1223 case R_AARCH64_LDST32_ABS_LO12_NC
:
1224 case R_AARCH64_LDST64_ABS_LO12_NC
:
1225 case R_AARCH64_LDST128_ABS_LO12_NC
:
1227 // No fixups are required for relative relocations, provided that
1228 // the relative offsets between sections have been preserved in
1229 // the ELF to PE/COFF conversion. We have already asserted that
1230 // this is the case in WriteSections64 ().
1234 case R_AARCH64_ABS64
:
1236 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1237 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1238 EFI_IMAGE_REL_BASED_DIR64
);
1241 case R_AARCH64_ABS32
:
1243 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1244 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1245 EFI_IMAGE_REL_BASED_HIGHLOW
);
1249 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1252 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
1255 if (mEhdr
->e_machine
== EM_X86_64
&& RelShdr
->sh_info
== mGOTShindex
) {
1257 // Tack relocations for GOT entries after other relocations for
1258 // the section the GOT is in, as it's usually found at the end
1259 // of the section. This is done in order to maintain Rva order
1260 // of Coff relocations.
1262 EmitGOTRelocations();
1268 if (mEhdr
->e_machine
== EM_X86_64
) {
1270 // This is a safety net just in case the GOT is in a section
1271 // with no other relocations and the first invocation of
1272 // EmitGOTRelocations() above was skipped. This invocation
1273 // does not maintain Rva order of Coff relocations.
1274 // At present, with a single text section, all references to
1275 // the GOT and the GOT itself reside in section .text, so
1276 // if there's a GOT at all, the first invocation above
1279 EmitGOTRelocations();
1282 // Pad by adding empty entries.
1284 while (mCoffOffset
& (mCoffAlignment
- 1)) {
1285 CoffAddFixupEntry(0);
1288 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1289 Dir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1290 Dir
->Size
= mCoffOffset
- mRelocOffset
;
1291 if (Dir
->Size
== 0) {
1292 // If no relocations, null out the directory entry and don't add the .reloc section
1293 Dir
->VirtualAddress
= 0;
1294 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
1296 Dir
->VirtualAddress
= mRelocOffset
;
1297 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
1298 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1299 | EFI_IMAGE_SCN_MEM_DISCARDABLE
1300 | EFI_IMAGE_SCN_MEM_READ
);
1311 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1312 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
1313 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
1314 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
1316 Len
= strlen(mInImageName
) + 1;
1318 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ mDebugOffset
);
1319 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
1320 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
1321 Dir
->RVA
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1322 Dir
->FileOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1324 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
1325 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
1326 strcpy ((char *)(Nb10
+ 1), mInImageName
);
1329 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1330 DataDir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
1331 DataDir
->VirtualAddress
= mDebugOffset
;
1332 DataDir
->Size
= sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1341 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1346 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1347 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
1356 if (mCoffSectionsOffset
!= NULL
) {
1357 free (mCoffSectionsOffset
);