4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>
6 Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include "WinNtInclude.h"
25 #include <Common/UefiBaseTypes.h>
26 #include <IndustryStandard/PeImage.h>
28 #include "PeCoffLib.h"
29 #include "EfiUtilityMsgs.h"
32 #include "ElfConvert.h"
33 #include "Elf64Convert.h"
44 SECTION_FILTER_TYPES FilterType
72 // Rename ELF32 structures to common names to help when porting to ELF64.
74 typedef Elf64_Shdr Elf_Shdr
;
75 typedef Elf64_Ehdr Elf_Ehdr
;
76 typedef Elf64_Rel Elf_Rel
;
77 typedef Elf64_Rela Elf_Rela
;
78 typedef Elf64_Sym Elf_Sym
;
79 typedef Elf64_Phdr Elf_Phdr
;
80 typedef Elf64_Dyn Elf_Dyn
;
81 #define ELFCLASS ELFCLASS64
82 #define ELF_R_TYPE(r) ELF64_R_TYPE(r)
83 #define ELF_R_SYM(r) ELF64_R_SYM(r)
86 // Well known ELF structures.
88 STATIC Elf_Ehdr
*mEhdr
;
89 STATIC Elf_Shdr
*mShdrBase
;
90 STATIC Elf_Phdr
*mPhdrBase
;
95 STATIC Elf_Shdr
*mGOTShdr
= NULL
;
96 STATIC UINT32 mGOTShindex
= 0;
97 STATIC UINT32
*mGOTCoffEntries
= NULL
;
98 STATIC UINT32 mGOTMaxCoffEntries
= 0;
99 STATIC UINT32 mGOTNumCoffEntries
= 0;
104 STATIC UINT32 mCoffAlignment
= 0x20;
107 // PE section alignment.
109 STATIC
const UINT16 mCoffNbrSections
= 4;
112 // ELF sections to offset in Coff file.
114 STATIC UINT32
*mCoffSectionsOffset
= NULL
;
117 // Offsets in COFF file
119 STATIC UINT32 mNtHdrOffset
;
120 STATIC UINT32 mTextOffset
;
121 STATIC UINT32 mDataOffset
;
122 STATIC UINT32 mHiiRsrcOffset
;
123 STATIC UINT32 mRelocOffset
;
124 STATIC UINT32 mDebugOffset
;
127 // Used for RISC-V relocations.
129 STATIC UINT8
*mRiscVPass1Targ
= NULL
;
130 STATIC Elf_Shdr
*mRiscVPass1Sym
= NULL
;
131 STATIC Elf64_Half mRiscVPass1SymSecIndex
= 0;
134 // Initialization Function
139 ELF_FUNCTION_TABLE
*ElfFunctions
143 // Initialize data pointer and structures.
145 VerboseMsg ("Set EHDR");
146 mEhdr
= (Elf_Ehdr
*) FileBuffer
;
149 // Check the ELF64 specific header information.
151 VerboseMsg ("Check ELF64 Header Information");
152 if (mEhdr
->e_ident
[EI_CLASS
] != ELFCLASS64
) {
153 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS64");
156 if (mEhdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
157 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
160 if ((mEhdr
->e_type
!= ET_EXEC
) && (mEhdr
->e_type
!= ET_DYN
)) {
161 Error (NULL
, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
164 if (!((mEhdr
->e_machine
== EM_X86_64
) || (mEhdr
->e_machine
== EM_AARCH64
) || (mEhdr
->e_machine
== EM_RISCV64
))) {
165 Error (NULL
, 0, 3000, "Unsupported", "ELF e_machine is not Elf64 machine.");
168 if (mEhdr
->e_version
!= EV_CURRENT
) {
169 Error (NULL
, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr
->e_version
, EV_CURRENT
);
174 // Update section header pointers
176 VerboseMsg ("Update Header Pointers");
177 mShdrBase
= (Elf_Shdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_shoff
);
178 mPhdrBase
= (Elf_Phdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_phoff
);
181 // Create COFF Section offset buffer and zero.
183 VerboseMsg ("Create COFF Section Offset Buffer");
184 mCoffSectionsOffset
= (UINT32
*)malloc(mEhdr
->e_shnum
* sizeof (UINT32
));
185 if (mCoffSectionsOffset
== NULL
) {
186 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
189 memset(mCoffSectionsOffset
, 0, mEhdr
->e_shnum
* sizeof(UINT32
));
192 // Fill in function pointers.
194 VerboseMsg ("Fill in Function Pointers");
195 ElfFunctions
->ScanSections
= ScanSections64
;
196 ElfFunctions
->WriteSections
= WriteSections64
;
197 ElfFunctions
->WriteRelocations
= WriteRelocations64
;
198 ElfFunctions
->WriteDebug
= WriteDebug64
;
199 ElfFunctions
->SetImageSize
= SetImageSize64
;
200 ElfFunctions
->CleanUp
= CleanUp64
;
207 // Header by Index functions
215 if (Num
>= mEhdr
->e_shnum
) {
216 Error (NULL
, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num
);
220 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
229 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
238 return (Offset
+ 3) & ~3;
250 return (BOOLEAN
) ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
);
259 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
261 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
270 if (IsHiiRsrcShdr(Shdr
)) {
273 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
282 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
284 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_STRTAB_SECTION_NAME
) == 0);
294 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
295 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
296 if (IsStrtabShdr(shdr
)) {
309 Elf_Shdr
*StrtabShdr
;
310 UINT8
*StrtabContents
;
314 if (Sym
->st_name
== 0) {
318 StrtabShdr
= FindStrtabShdr();
319 if (StrtabShdr
== NULL
) {
323 assert(Sym
->st_name
< StrtabShdr
->sh_size
);
325 StrtabContents
= (UINT8
*)mEhdr
+ StrtabShdr
->sh_offset
;
328 for (i
= Sym
->st_name
; (i
< StrtabShdr
->sh_size
) && !foundEnd
; i
++) {
329 foundEnd
= (BOOLEAN
)(StrtabContents
[i
] == 0);
333 return StrtabContents
+ Sym
->st_name
;
337 // Find the ELF section hosting the GOT from an ELF Rva
338 // of a single GOT entry. Normally, GOT is placed in
339 // ELF .text section, so assume once we find in which
340 // section the GOT is, all GOT entries are there, and
345 FindElfGOTSectionFromGOTEntryElfRva (
346 Elf64_Addr GOTEntryElfRva
350 if (mGOTShdr
!= NULL
) {
351 if (GOTEntryElfRva
>= mGOTShdr
->sh_addr
&&
352 GOTEntryElfRva
< mGOTShdr
->sh_addr
+ mGOTShdr
->sh_size
) {
355 Error (NULL
, 0, 3000, "Unsupported", "FindElfGOTSectionFromGOTEntryElfRva: GOT entries found in multiple sections.");
358 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
359 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
360 if (GOTEntryElfRva
>= shdr
->sh_addr
&&
361 GOTEntryElfRva
< shdr
->sh_addr
+ shdr
->sh_size
) {
367 Error (NULL
, 0, 3000, "Invalid", "FindElfGOTSectionFromGOTEntryElfRva: ElfRva 0x%016LX for GOT entry not found in any section.", GOTEntryElfRva
);
372 // Stores locations of GOT entries in COFF image.
373 // Returns TRUE if GOT entry is new.
374 // Simple implementation as number of GOT
375 // entries is expected to be low.
380 AccumulateCoffGOTEntries (
385 if (mGOTCoffEntries
!= NULL
) {
386 for (i
= 0; i
< mGOTNumCoffEntries
; i
++) {
387 if (mGOTCoffEntries
[i
] == GOTCoffEntry
) {
392 if (mGOTCoffEntries
== NULL
) {
393 mGOTCoffEntries
= (UINT32
*)malloc(5 * sizeof *mGOTCoffEntries
);
394 if (mGOTCoffEntries
== NULL
) {
395 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
397 assert (mGOTCoffEntries
!= NULL
);
398 mGOTMaxCoffEntries
= 5;
399 mGOTNumCoffEntries
= 0;
400 } else if (mGOTNumCoffEntries
== mGOTMaxCoffEntries
) {
401 mGOTCoffEntries
= (UINT32
*)realloc(mGOTCoffEntries
, 2 * mGOTMaxCoffEntries
* sizeof *mGOTCoffEntries
);
402 if (mGOTCoffEntries
== NULL
) {
403 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
405 assert (mGOTCoffEntries
!= NULL
);
406 mGOTMaxCoffEntries
+= mGOTMaxCoffEntries
;
408 mGOTCoffEntries
[mGOTNumCoffEntries
++] = GOTCoffEntry
;
413 // 32-bit Unsigned integer comparator for qsort.
422 if (*(const UINT32
*)lhs
< *(const UINT32
*)rhs
) {
425 return *(const UINT32
*)lhs
> *(const UINT32
*)rhs
;
429 // Emit accumulated Coff GOT entry relocations into
430 // Coff image. This function performs its job
431 // once and then releases the entry list, so
432 // it can safely be called multiple times.
441 if (mGOTCoffEntries
== NULL
) {
445 // Emit Coff relocations with Rvas ordered.
450 sizeof *mGOTCoffEntries
,
452 for (i
= 0; i
< mGOTNumCoffEntries
; i
++) {
453 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X", mGOTCoffEntries
[i
]);
456 EFI_IMAGE_REL_BASED_DIR64
);
458 free(mGOTCoffEntries
);
459 mGOTCoffEntries
= NULL
;
460 mGOTMaxCoffEntries
= 0;
461 mGOTNumCoffEntries
= 0;
464 // RISC-V 64 specific Elf WriteSection function.
468 WriteSectionRiscV64 (
478 switch (ELF_R_TYPE(Rel
->r_info
)) {
483 *(UINT32
*)Targ
= (UINT32
)((UINT64
)(*(UINT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
487 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
491 mRiscVPass1Targ
= Targ
;
492 mRiscVPass1Sym
= SymShdr
;
493 mRiscVPass1SymSecIndex
= Sym
->st_shndx
;
497 if (mRiscVPass1Sym
== SymShdr
&& mRiscVPass1Targ
!= NULL
&& mRiscVPass1SymSecIndex
== Sym
->st_shndx
&& mRiscVPass1SymSecIndex
!= 0) {
498 Value
= (UINT32
)(RV_X(*(UINT32
*)mRiscVPass1Targ
, 12, 20) << 12);
499 Value2
= (UINT32
)(RV_X(*(UINT32
*)Targ
, 20, 12));
500 if (Value2
& (RISCV_IMM_REACH
/2)) {
501 Value2
|= ~(RISCV_IMM_REACH
-1);
504 Value
= Value
- (UINT32
)SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
505 Value2
= RISCV_CONST_HIGH_PART (Value
);
506 *(UINT32
*)mRiscVPass1Targ
= (RV_X (Value2
, 12, 20) << 12) | \
507 (RV_X (*(UINT32
*)mRiscVPass1Targ
, 0, 12));
508 *(UINT32
*)Targ
= (RV_X (Value
, 0, 12) << 20) | \
509 (RV_X (*(UINT32
*)Targ
, 0, 20));
511 mRiscVPass1Sym
= NULL
;
512 mRiscVPass1Targ
= NULL
;
513 mRiscVPass1SymSecIndex
= 0;
517 if (mRiscVPass1Sym
== SymShdr
&& mRiscVPass1Targ
!= NULL
&& mRiscVPass1SymSecIndex
== Sym
->st_shndx
&& mRiscVPass1SymSecIndex
!= 0) {
518 Value
= (UINT32
)(RV_X(*(UINT32
*)mRiscVPass1Targ
, 12, 20) << 12);
519 Value2
= (UINT32
)(RV_X(*(UINT32
*)Targ
, 7, 5) | (RV_X(*(UINT32
*)Targ
, 25, 7) << 5));
520 if (Value2
& (RISCV_IMM_REACH
/2)) {
521 Value2
|= ~(RISCV_IMM_REACH
-1);
524 Value
= Value
- (UINT32
)SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
525 Value2
= RISCV_CONST_HIGH_PART (Value
);
526 *(UINT32
*)mRiscVPass1Targ
= (RV_X (Value2
, 12, 20) << 12) | \
527 (RV_X (*(UINT32
*)mRiscVPass1Targ
, 0, 12));
528 Value2
= *(UINT32
*)Targ
& 0x01fff07f;
529 Value
&= RISCV_IMM_REACH
- 1;
530 *(UINT32
*)Targ
= Value2
| (UINT32
)(((RV_X(Value
, 0, 5) << 7) | (RV_X(Value
, 5, 7) << 25)));
532 mRiscVPass1Sym
= NULL
;
533 mRiscVPass1Targ
= NULL
;
534 mRiscVPass1SymSecIndex
= 0;
537 case R_RISCV_PCREL_HI20
:
538 mRiscVPass1Targ
= Targ
;
539 mRiscVPass1Sym
= SymShdr
;
540 mRiscVPass1SymSecIndex
= Sym
->st_shndx
;
542 Value
= (UINT32
)(RV_X(*(UINT32
*)mRiscVPass1Targ
, 12, 20));
545 case R_RISCV_PCREL_LO12_I
:
546 if (mRiscVPass1Targ
!= NULL
&& mRiscVPass1Sym
!= NULL
&& mRiscVPass1SymSecIndex
!= 0) {
548 Value2
= (UINT32
)(RV_X(*(UINT32
*)mRiscVPass1Targ
, 12, 20));
549 Value
= (UINT32
)(RV_X(*(UINT32
*)Targ
, 20, 12));
550 if(Value
& (RISCV_IMM_REACH
/2)) {
551 Value
|= ~(RISCV_IMM_REACH
-1);
553 Value
= Value
- (UINT32
)mRiscVPass1Sym
->sh_addr
+ mCoffSectionsOffset
[mRiscVPass1SymSecIndex
];
554 if(-2048 > (INT32
)Value
) {
555 i
= (((INT32
)Value
* -1) / 4096);
558 if(-2048 > (INT32
)Value
) {
563 else if( 2047 < (INT32
)Value
) {
567 if(2047 < (INT32
)Value
) {
573 *(UINT32
*)Targ
= (RV_X(Value
, 0, 12) << 20) | (RV_X(*(UINT32
*)Targ
, 0, 20));
574 *(UINT32
*)mRiscVPass1Targ
= (RV_X(Value2
, 0, 20)<<12) | (RV_X(*(UINT32
*)mRiscVPass1Targ
, 0, 12));
576 mRiscVPass1Sym
= NULL
;
577 mRiscVPass1Targ
= NULL
;
578 mRiscVPass1SymSecIndex
= 0;
587 case R_RISCV_GPREL_I
:
588 case R_RISCV_GPREL_S
:
590 case R_RISCV_RVC_BRANCH
:
591 case R_RISCV_RVC_JUMP
:
601 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_RISCV64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
606 // Elf functions interface implementation
616 EFI_IMAGE_DOS_HEADER
*DosHdr
;
617 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
620 BOOLEAN FoundSection
;
626 // Coff file start with a DOS header.
628 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
629 mNtHdrOffset
= mCoffOffset
;
630 switch (mEhdr
->e_machine
) {
634 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
637 VerboseMsg ("%s unknown e_machine type %hu. Assume X64", mInImageName
, mEhdr
->e_machine
);
638 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
642 mTableOffset
= mCoffOffset
;
643 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
646 // Set mCoffAlignment to the maximum alignment of the input sections
649 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
650 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
651 if (shdr
->sh_addralign
<= mCoffAlignment
) {
654 if (IsTextShdr(shdr
) || IsDataShdr(shdr
) || IsHiiRsrcShdr(shdr
)) {
655 mCoffAlignment
= (UINT32
)shdr
->sh_addralign
;
660 // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT
662 if (mCoffAlignment
> MAX_COFF_ALIGNMENT
) {
663 Error (NULL
, 0, 3000, "Invalid", "Section alignment is larger than MAX_COFF_ALIGNMENT.");
669 // Move the PE/COFF header right before the first section. This will help us
670 // save space when converting to TE.
672 if (mCoffAlignment
> mCoffOffset
) {
673 mNtHdrOffset
+= mCoffAlignment
- mCoffOffset
;
674 mTableOffset
+= mCoffAlignment
- mCoffOffset
;
675 mCoffOffset
= mCoffAlignment
;
679 // First text sections.
681 mCoffOffset
= CoffAlign(mCoffOffset
);
682 mTextOffset
= mCoffOffset
;
683 FoundSection
= FALSE
;
685 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
686 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
687 if (IsTextShdr(shdr
)) {
688 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
689 // the alignment field is valid
690 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
691 // if the section address is aligned we must align PE/COFF
692 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
694 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
698 /* Relocate entry. */
699 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
700 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
701 CoffEntry
= (UINT32
) (mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
);
705 // Set mTextOffset with the offset of the first '.text' section
708 mTextOffset
= mCoffOffset
;
712 mCoffSectionsOffset
[i
] = mCoffOffset
;
713 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
719 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
723 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
724 mCoffOffset
= CoffAlign(mCoffOffset
);
726 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
727 Warning (NULL
, 0, 0, NULL
, "Multiple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
731 // Then data sections.
733 mDataOffset
= mCoffOffset
;
734 FoundSection
= FALSE
;
736 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
737 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
738 if (IsDataShdr(shdr
)) {
739 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
740 // the alignment field is valid
741 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
742 // if the section address is aligned we must align PE/COFF
743 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
745 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
750 // Set mDataOffset with the offset of the first '.data' section
753 mDataOffset
= mCoffOffset
;
756 mCoffSectionsOffset
[i
] = mCoffOffset
;
757 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
763 // Make room for .debug data in .data (or .text if .data is empty) instead of
764 // putting it in a section of its own. This is explicitly allowed by the
765 // PE/COFF spec, and prevents bloat in the binary when using large values for
766 // section alignment.
768 if (SectionCount
> 0) {
769 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
771 mCoffOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
) +
772 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) +
773 strlen(mInImageName
) + 1;
775 mCoffOffset
= CoffAlign(mCoffOffset
);
776 if (SectionCount
== 0) {
777 mDataOffset
= mCoffOffset
;
780 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
781 Warning (NULL
, 0, 0, NULL
, "Multiple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
785 // The HII resource sections.
787 mHiiRsrcOffset
= mCoffOffset
;
788 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
789 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
790 if (IsHiiRsrcShdr(shdr
)) {
791 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
792 // the alignment field is valid
793 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
794 // if the section address is aligned we must align PE/COFF
795 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
797 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
800 if (shdr
->sh_size
!= 0) {
801 mHiiRsrcOffset
= mCoffOffset
;
802 mCoffSectionsOffset
[i
] = mCoffOffset
;
803 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
804 mCoffOffset
= CoffAlign(mCoffOffset
);
805 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
811 mRelocOffset
= mCoffOffset
;
814 // Allocate base Coff file. Will be expanded later for relocations.
816 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
817 if (mCoffFile
== NULL
) {
818 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
820 assert (mCoffFile
!= NULL
);
821 memset(mCoffFile
, 0, mCoffOffset
);
826 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
827 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
828 DosHdr
->e_lfanew
= mNtHdrOffset
;
830 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
832 NtHdr
->Pe32Plus
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
834 switch (mEhdr
->e_machine
) {
836 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
837 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
840 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_AARCH64
;
841 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
844 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_RISCV64
;
845 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
849 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN
)mEhdr
->e_machine
);
850 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
851 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
854 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
855 NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
856 mImageTimeStamp
= NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
;
857 NtHdr
->Pe32Plus
.FileHeader
.PointerToSymbolTable
= 0;
858 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSymbols
= 0;
859 NtHdr
->Pe32Plus
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32Plus
.OptionalHeader
);
860 NtHdr
->Pe32Plus
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
861 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
862 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
863 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE
;
865 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
866 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
867 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfUninitializedData
= 0;
868 NtHdr
->Pe32Plus
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
870 NtHdr
->Pe32Plus
.OptionalHeader
.BaseOfCode
= mTextOffset
;
872 NtHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= 0;
873 NtHdr
->Pe32Plus
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
874 NtHdr
->Pe32Plus
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
875 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= 0;
877 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
878 NtHdr
->Pe32Plus
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
883 if ((mDataOffset
- mTextOffset
) > 0) {
884 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
885 EFI_IMAGE_SCN_CNT_CODE
886 | EFI_IMAGE_SCN_MEM_EXECUTE
887 | EFI_IMAGE_SCN_MEM_READ
);
889 // Don't make a section of size 0.
890 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
893 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
894 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
895 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
896 | EFI_IMAGE_SCN_MEM_WRITE
897 | EFI_IMAGE_SCN_MEM_READ
);
899 // Don't make a section of size 0.
900 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
903 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
904 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
905 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
906 | EFI_IMAGE_SCN_MEM_READ
);
908 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
909 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
911 // Don't make a section of size 0.
912 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
920 SECTION_FILTER_TYPES FilterType
926 BOOLEAN (*Filter
)(Elf_Shdr
*);
927 Elf64_Addr GOTEntryRva
;
930 // Initialize filter pointer
932 switch (FilterType
) {
937 Filter
= IsHiiRsrcShdr
;
947 // First: copy sections.
949 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
950 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
951 if ((*Filter
)(Shdr
)) {
952 switch (Shdr
->sh_type
) {
955 if (Shdr
->sh_offset
+ Shdr
->sh_size
> mFileBufferSize
) {
958 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
959 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
960 (size_t) Shdr
->sh_size
);
964 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, (size_t) Shdr
->sh_size
);
969 // Ignore for unknown section type.
971 VerboseMsg ("%s unknown section type %x. We ignore this unknown section type.", mInImageName
, (unsigned)Shdr
->sh_type
);
978 // Second: apply relocations.
980 VerboseMsg ("Applying Relocations...");
981 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
983 // Determine if this is a relocation section.
985 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
986 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
991 // If this is a ET_DYN (PIE) executable, we will encounter a dynamic SHT_RELA
992 // section that applies to the entire binary, and which will have its section
993 // index set to #0 (which is a NULL section with the SHF_ALLOC bit cleared).
995 // In the absence of GOT based relocations,
996 // this RELA section will contain redundant R_xxx_RELATIVE relocations, one
997 // for every R_xxx_xx64 relocation appearing in the per-section RELA sections.
998 // (i.e., .rela.text and .rela.data)
1000 if (RelShdr
->sh_info
== 0) {
1005 // Relocation section found. Now extract section information that the relocations
1006 // apply to in the ELF data and the new COFF data.
1008 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
1009 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
1012 // Only process relocations for the current filter type.
1014 if (RelShdr
->sh_type
== SHT_RELA
&& (*Filter
)(SecShdr
)) {
1018 // Determine the symbol table referenced by the relocation data.
1020 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
1021 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
1024 // Process all relocation entries for this section.
1026 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= (UINT32
) RelShdr
->sh_entsize
) {
1029 // Set pointer to relocation entry
1031 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
1034 // Set pointer to symbol table entry associated with the relocation entry.
1036 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
1042 // Check section header index found in symbol table and get the section
1045 if (Sym
->st_shndx
== SHN_UNDEF
1046 || Sym
->st_shndx
>= mEhdr
->e_shnum
) {
1047 const UINT8
*SymName
= GetSymName(Sym
);
1048 if (SymName
== NULL
) {
1049 SymName
= (const UINT8
*)"<unknown>";
1053 // Skip error on EM_RISCV64 becasue no symble name is built
1054 // from RISC-V toolchain.
1056 if (mEhdr
->e_machine
!= EM_RISCV64
) {
1057 Error (NULL
, 0, 3000, "Invalid",
1058 "%s: Bad definition for symbol '%s'@%#llx or unsupported symbol type. "
1059 "For example, absolute and undefined symbols are not supported.",
1060 mInImageName
, SymName
, Sym
->st_value
);
1065 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
1068 // Convert the relocation data to a pointer into the coff file.
1071 // r_offset is the virtual address of the storage unit to be relocated.
1072 // sh_addr is the virtual address for the base of the section.
1074 // r_offset in a memory address.
1075 // Convert it to a pointer in the coff file.
1077 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
1080 // Determine how to handle each relocation type based on the machine type.
1082 if (mEhdr
->e_machine
== EM_X86_64
) {
1083 switch (ELF_R_TYPE(Rel
->r_info
)) {
1088 // Absolute relocation.
1090 VerboseMsg ("R_X86_64_64");
1091 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1092 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1094 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1095 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
1098 VerboseMsg ("R_X86_64_32");
1099 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1100 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1102 *(UINT32
*)Targ
= (UINT32
)((UINT64
)(*(UINT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
1103 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1106 VerboseMsg ("R_X86_64_32S");
1107 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1108 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1110 *(INT32
*)Targ
= (INT32
)((INT64
)(*(INT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
1111 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1114 case R_X86_64_PLT32
:
1116 // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is
1117 // possible since we know all code symbol references resolve to
1118 // definitions in the same module (UEFI has no shared libraries),
1119 // and so there is never a reason to jump via a PLT entry,
1120 // allowing us to resolve the reference using the symbol directly.
1122 VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");
1126 // Relative relocation: Symbol - Ip + Addend
1128 VerboseMsg ("R_X86_64_PC32");
1129 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1130 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1132 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
1133 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
1134 - (SecOffset
- SecShdr
->sh_addr
));
1135 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1137 case R_X86_64_GOTPCREL
:
1138 case R_X86_64_GOTPCRELX
:
1139 case R_X86_64_REX_GOTPCRELX
:
1140 VerboseMsg ("R_X86_64_GOTPCREL family");
1141 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1142 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1144 GOTEntryRva
= Rel
->r_offset
- Rel
->r_addend
+ *(INT32
*)Targ
;
1145 FindElfGOTSectionFromGOTEntryElfRva(GOTEntryRva
);
1146 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
1147 + (mCoffSectionsOffset
[mGOTShindex
] - mGOTShdr
->sh_addr
)
1148 - (SecOffset
- SecShdr
->sh_addr
));
1149 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1150 GOTEntryRva
+= (mCoffSectionsOffset
[mGOTShindex
] - mGOTShdr
->sh_addr
); // ELF Rva -> COFF Rva
1151 if (AccumulateCoffGOTEntries((UINT32
)GOTEntryRva
)) {
1153 // Relocate GOT entry if it's the first time we run into it
1155 Targ
= mCoffFile
+ GOTEntryRva
;
1157 // Limitation: The following three statements assume memory
1158 // at *Targ is valid because the section containing the GOT
1159 // has already been copied from the ELF image to the Coff image.
1160 // This pre-condition presently holds because the GOT is placed
1161 // in section .text, and the ELF text sections are all copied
1162 // prior to reaching this point.
1163 // If the pre-condition is violated in the future, this fixup
1164 // either needs to be deferred after the GOT section is copied
1165 // to the Coff image, or the fixup should be performed on the
1166 // source Elf image instead of the destination Coff image.
1168 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1169 (UINT32
)GOTEntryRva
,
1171 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1172 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
1176 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1178 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
1180 switch (ELF_R_TYPE(Rel
->r_info
)) {
1183 case R_AARCH64_LD64_GOT_LO12_NC
:
1185 // Convert into an ADD instruction - see R_AARCH64_ADR_GOT_PAGE below.
1187 *(UINT32
*)Targ
&= 0x3ff;
1188 *(UINT32
*)Targ
|= 0x91000000 | ((Sym
->st_value
& 0xfff) << 10);
1191 case R_AARCH64_ADR_GOT_PAGE
:
1193 // This relocation points to the GOT entry that contains the absolute
1194 // address of the symbol we are referring to. Since EDK2 only uses
1195 // fully linked binaries, we can avoid the indirection, and simply
1196 // refer to the symbol directly. This implies having to patch the
1197 // subsequent LDR instruction (covered by a R_AARCH64_LD64_GOT_LO12_NC
1198 // relocation) into an ADD instruction - this is handled above.
1200 Offset
= (Sym
->st_value
- (Rel
->r_offset
& ~0xfff)) >> 12;
1202 *(UINT32
*)Targ
&= 0x9000001f;
1203 *(UINT32
*)Targ
|= ((Offset
& 0x1ffffc) << (5 - 2)) | ((Offset
& 0x3) << 29);
1207 case R_AARCH64_ADR_PREL_PG_HI21
:
1209 // In order to handle Cortex-A53 erratum #843419, the LD linker may
1210 // convert ADRP instructions into ADR instructions, but without
1211 // updating the static relocation type, and so we may end up here
1212 // while the instruction in question is actually ADR. So let's
1213 // just disregard it: the section offset check we apply below to
1214 // ADR instructions will trigger for its R_AARCH64_xxx_ABS_LO12_NC
1215 // companion instruction as well, so it is safe to omit it here.
1217 if ((*(UINT32
*)Targ
& BIT31
) == 0) {
1222 // AArch64 PG_H21 relocations are typically paired with ABS_LO12
1223 // relocations, where a PC-relative reference with +/- 4 GB range is
1224 // split into a relative high part and an absolute low part. Since
1225 // the absolute low part represents the offset into a 4 KB page, we
1226 // either have to convert the ADRP into an ADR instruction, or we
1227 // need to use a section alignment of at least 4 KB, so that the
1228 // binary appears at a correct offset at runtime. In any case, we
1229 // have to make sure that the 4 KB relative offsets of both the
1230 // section containing the reference as well as the section to which
1231 // it refers have not been changed during PE/COFF conversion (i.e.,
1232 // in ScanSections64() above).
1234 if (mCoffAlignment
< 0x1000) {
1236 // Attempt to convert the ADRP into an ADR instruction.
1237 // This is only possible if the symbol is within +/- 1 MB.
1240 // Decode the ADRP instruction
1241 Offset
= (INT32
)((*(UINT32
*)Targ
& 0xffffe0) << 8);
1242 Offset
= (Offset
<< (6 - 5)) | ((*(UINT32
*)Targ
& 0x60000000) >> (29 - 12));
1245 // ADRP offset is relative to the previous page boundary,
1246 // whereas ADR offset is relative to the instruction itself.
1247 // So fix up the offset so it points to the page containing
1250 Offset
-= (UINTN
)(Targ
- mCoffFile
) & 0xfff;
1252 if (Offset
< -0x100000 || Offset
> 0xfffff) {
1253 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s due to its size (> 1 MB), this module requires 4 KB section alignment.",
1258 // Re-encode the offset as an ADR instruction
1259 *(UINT32
*)Targ
&= 0x1000001f;
1260 *(UINT32
*)Targ
|= ((Offset
& 0x1ffffc) << (5 - 2)) | ((Offset
& 0x3) << 29);
1264 case R_AARCH64_ADD_ABS_LO12_NC
:
1265 case R_AARCH64_LDST8_ABS_LO12_NC
:
1266 case R_AARCH64_LDST16_ABS_LO12_NC
:
1267 case R_AARCH64_LDST32_ABS_LO12_NC
:
1268 case R_AARCH64_LDST64_ABS_LO12_NC
:
1269 case R_AARCH64_LDST128_ABS_LO12_NC
:
1270 if (((SecShdr
->sh_addr
^ SecOffset
) & 0xfff) != 0 ||
1271 ((SymShdr
->sh_addr
^ mCoffSectionsOffset
[Sym
->st_shndx
]) & 0xfff) != 0) {
1272 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 small code model requires identical ELF and PE/COFF section offsets modulo 4 KB.",
1278 case R_AARCH64_ADR_PREL_LO21
:
1279 case R_AARCH64_CONDBR19
:
1280 case R_AARCH64_LD_PREL_LO19
:
1281 case R_AARCH64_CALL26
:
1282 case R_AARCH64_JUMP26
:
1283 case R_AARCH64_PREL64
:
1284 case R_AARCH64_PREL32
:
1285 case R_AARCH64_PREL16
:
1287 // The GCC toolchains (i.e., binutils) may corrupt section relative
1288 // relocations when emitting relocation sections into fully linked
1289 // binaries. More specifically, they tend to fail to take into
1290 // account the fact that a '.rodata + XXX' relocation needs to have
1291 // its addend recalculated once .rodata is merged into the .text
1292 // section, and the relocation emitted into the .rela.text section.
1294 // We cannot really recover from this loss of information, so the
1295 // only workaround is to prevent having to recalculate any relative
1296 // relocations at all, by using a linker script that ensures that
1297 // the offset between the Place and the Symbol is the same in both
1298 // the ELF and the PE/COFF versions of the binary.
1300 if ((SymShdr
->sh_addr
- SecShdr
->sh_addr
) !=
1301 (mCoffSectionsOffset
[Sym
->st_shndx
] - SecOffset
)) {
1302 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 relative relocations require identical ELF and PE/COFF section offsets",
1307 // Absolute relocations.
1308 case R_AARCH64_ABS64
:
1309 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1313 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1315 } else if (mEhdr
->e_machine
== EM_RISCV64
) {
1317 // Write section for RISC-V 64 architecture.
1319 WriteSectionRiscV64 (Rel
, Targ
, SymShdr
, Sym
);
1321 Error (NULL
, 0, 3000, "Invalid", "Not a supported machine type");
1332 WriteRelocations64 (
1337 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1338 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
1339 UINT32 RiscVRelType
;
1341 for (Index
= 0; Index
< mEhdr
->e_shnum
; Index
++) {
1342 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
1343 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
1344 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
1345 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
1348 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
1349 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
1351 if (mEhdr
->e_machine
== EM_X86_64
) {
1352 switch (ELF_R_TYPE(Rel
->r_info
)) {
1355 case R_X86_64_PLT32
:
1356 case R_X86_64_GOTPCREL
:
1357 case R_X86_64_GOTPCRELX
:
1358 case R_X86_64_REX_GOTPCRELX
:
1361 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",
1362 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
1364 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1365 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1366 EFI_IMAGE_REL_BASED_DIR64
);
1369 // R_X86_64_32 and R_X86_64_32S are ELF64 relocations emitted when using
1370 // the SYSV X64 ABI small non-position-independent code model.
1371 // R_X86_64_32 is used for unsigned 32-bit immediates with a 32-bit operand
1372 // size. The value is either not extended, or zero-extended to 64 bits.
1373 // R_X86_64_32S is used for either signed 32-bit non-rip-relative displacements
1374 // or signed 32-bit immediates with a 64-bit operand size. The value is
1375 // sign-extended to 64 bits.
1376 // EFI_IMAGE_REL_BASED_HIGHLOW is a PE relocation that uses 32-bit arithmetic
1377 // for rebasing an image.
1378 // EFI PE binaries declare themselves EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE and
1379 // may load above 2GB. If an EFI PE binary with a converted R_X86_64_32S
1380 // relocation is loaded above 2GB, the value will get sign-extended to the
1381 // negative part of the 64-bit address space. The negative part of the 64-bit
1382 // address space is unmapped, so accessing such an address page-faults.
1383 // In order to support R_X86_64_32S, it is necessary to unset
1384 // EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE, and the EFI PE loader must implement
1385 // this flag and abstain from loading such a PE binary above 2GB.
1386 // Since this feature is not supported, support for R_X86_64_32S (and hence
1387 // the small non-position-independent code model) is disabled.
1389 // case R_X86_64_32S:
1391 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",
1392 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
1394 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1395 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1396 EFI_IMAGE_REL_BASED_HIGHLOW
);
1399 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1401 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
1403 switch (ELF_R_TYPE(Rel
->r_info
)) {
1404 case R_AARCH64_ADR_PREL_LO21
:
1405 case R_AARCH64_CONDBR19
:
1406 case R_AARCH64_LD_PREL_LO19
:
1407 case R_AARCH64_CALL26
:
1408 case R_AARCH64_JUMP26
:
1409 case R_AARCH64_PREL64
:
1410 case R_AARCH64_PREL32
:
1411 case R_AARCH64_PREL16
:
1412 case R_AARCH64_ADR_PREL_PG_HI21
:
1413 case R_AARCH64_ADD_ABS_LO12_NC
:
1414 case R_AARCH64_LDST8_ABS_LO12_NC
:
1415 case R_AARCH64_LDST16_ABS_LO12_NC
:
1416 case R_AARCH64_LDST32_ABS_LO12_NC
:
1417 case R_AARCH64_LDST64_ABS_LO12_NC
:
1418 case R_AARCH64_LDST128_ABS_LO12_NC
:
1419 case R_AARCH64_ADR_GOT_PAGE
:
1420 case R_AARCH64_LD64_GOT_LO12_NC
:
1422 // No fixups are required for relative relocations, provided that
1423 // the relative offsets between sections have been preserved in
1424 // the ELF to PE/COFF conversion. We have already asserted that
1425 // this is the case in WriteSections64 ().
1429 case R_AARCH64_ABS64
:
1431 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1432 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1433 EFI_IMAGE_REL_BASED_DIR64
);
1436 case R_AARCH64_ABS32
:
1438 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1439 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1440 EFI_IMAGE_REL_BASED_HIGHLOW
);
1444 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1446 } else if (mEhdr
->e_machine
== EM_RISCV64
) {
1447 RiscVRelType
= ELF_R_TYPE(Rel
->r_info
);
1448 switch (RiscVRelType
) {
1454 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1455 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1456 EFI_IMAGE_REL_BASED_HIGHLOW
);
1461 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1462 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1463 EFI_IMAGE_REL_BASED_DIR64
);
1468 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1469 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1470 EFI_IMAGE_REL_BASED_RISCV_HI20
);
1473 case R_RISCV_LO12_I
:
1475 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1476 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1477 EFI_IMAGE_REL_BASED_RISCV_LOW12I
);
1480 case R_RISCV_LO12_S
:
1482 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1483 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1484 EFI_IMAGE_REL_BASED_RISCV_LOW12S
);
1489 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1490 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1491 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1496 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1497 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1498 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1503 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1504 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1505 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1510 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1511 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1512 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1515 case R_RISCV_BRANCH
:
1517 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1518 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1519 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1524 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1525 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1526 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1529 case R_RISCV_GPREL_I
:
1530 case R_RISCV_GPREL_S
:
1532 case R_RISCV_RVC_BRANCH
:
1533 case R_RISCV_RVC_JUMP
:
1540 case R_RISCV_PCREL_HI20
:
1541 case R_RISCV_PCREL_LO12_I
:
1545 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_RISCV64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1548 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
1551 if (mEhdr
->e_machine
== EM_X86_64
&& RelShdr
->sh_info
== mGOTShindex
) {
1553 // Tack relocations for GOT entries after other relocations for
1554 // the section the GOT is in, as it's usually found at the end
1555 // of the section. This is done in order to maintain Rva order
1556 // of Coff relocations.
1558 EmitGOTRelocations();
1564 if (mEhdr
->e_machine
== EM_X86_64
) {
1566 // This is a safety net just in case the GOT is in a section
1567 // with no other relocations and the first invocation of
1568 // EmitGOTRelocations() above was skipped. This invocation
1569 // does not maintain Rva order of Coff relocations.
1570 // At present, with a single text section, all references to
1571 // the GOT and the GOT itself reside in section .text, so
1572 // if there's a GOT at all, the first invocation above
1575 EmitGOTRelocations();
1578 // Pad by adding empty entries.
1580 while (mCoffOffset
& (mCoffAlignment
- 1)) {
1581 CoffAddFixupEntry(0);
1584 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1585 Dir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1586 Dir
->Size
= mCoffOffset
- mRelocOffset
;
1587 if (Dir
->Size
== 0) {
1588 // If no relocations, null out the directory entry and don't add the .reloc section
1589 Dir
->VirtualAddress
= 0;
1590 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
1592 Dir
->VirtualAddress
= mRelocOffset
;
1593 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
1594 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1595 | EFI_IMAGE_SCN_MEM_DISCARDABLE
1596 | EFI_IMAGE_SCN_MEM_READ
);
1607 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1608 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
1609 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
1610 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
1612 Len
= strlen(mInImageName
) + 1;
1614 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ mDebugOffset
);
1615 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
1616 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
1617 Dir
->RVA
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1618 Dir
->FileOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1620 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
1621 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
1622 strcpy ((char *)(Nb10
+ 1), mInImageName
);
1625 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1626 DataDir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
1627 DataDir
->VirtualAddress
= mDebugOffset
;
1628 DataDir
->Size
= sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1637 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1642 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1643 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
1652 if (mCoffSectionsOffset
!= NULL
) {
1653 free (mCoffSectionsOffset
);