4 Copyright (c) 2010 - 2021, 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;
132 STATIC INT32 mRiscVPass1Offset
;
133 STATIC INT32 mRiscVPass1GotFixup
;
136 // Initialization Function
141 ELF_FUNCTION_TABLE
*ElfFunctions
145 // Initialize data pointer and structures.
147 VerboseMsg ("Set EHDR");
148 mEhdr
= (Elf_Ehdr
*) FileBuffer
;
151 // Check the ELF64 specific header information.
153 VerboseMsg ("Check ELF64 Header Information");
154 if (mEhdr
->e_ident
[EI_CLASS
] != ELFCLASS64
) {
155 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS64");
158 if (mEhdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
159 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
162 if ((mEhdr
->e_type
!= ET_EXEC
) && (mEhdr
->e_type
!= ET_DYN
)) {
163 Error (NULL
, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
166 if (!((mEhdr
->e_machine
== EM_X86_64
) || (mEhdr
->e_machine
== EM_AARCH64
) || (mEhdr
->e_machine
== EM_RISCV64
))) {
167 Warning (NULL
, 0, 3000, "Unsupported", "ELF e_machine is not Elf64 machine.");
169 if (mEhdr
->e_version
!= EV_CURRENT
) {
170 Error (NULL
, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr
->e_version
, EV_CURRENT
);
175 // Update section header pointers
177 VerboseMsg ("Update Header Pointers");
178 mShdrBase
= (Elf_Shdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_shoff
);
179 mPhdrBase
= (Elf_Phdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_phoff
);
182 // Create COFF Section offset buffer and zero.
184 VerboseMsg ("Create COFF Section Offset Buffer");
185 mCoffSectionsOffset
= (UINT32
*)malloc(mEhdr
->e_shnum
* sizeof (UINT32
));
186 if (mCoffSectionsOffset
== NULL
) {
187 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
190 memset(mCoffSectionsOffset
, 0, mEhdr
->e_shnum
* sizeof(UINT32
));
193 // Fill in function pointers.
195 VerboseMsg ("Fill in Function Pointers");
196 ElfFunctions
->ScanSections
= ScanSections64
;
197 ElfFunctions
->WriteSections
= WriteSections64
;
198 ElfFunctions
->WriteRelocations
= WriteRelocations64
;
199 ElfFunctions
->WriteDebug
= WriteDebug64
;
200 ElfFunctions
->SetImageSize
= SetImageSize64
;
201 ElfFunctions
->CleanUp
= CleanUp64
;
208 // Header by Index functions
216 if (Num
>= mEhdr
->e_shnum
) {
217 Error (NULL
, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num
);
221 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
230 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
239 return (Offset
+ 3) & ~3;
251 return (BOOLEAN
) (((Shdr
->sh_flags
& (SHF_EXECINSTR
| SHF_ALLOC
)) == (SHF_EXECINSTR
| SHF_ALLOC
)) ||
252 ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
));
261 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
263 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
272 if (IsHiiRsrcShdr(Shdr
)) {
275 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_EXECINSTR
| SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
284 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
286 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_STRTAB_SECTION_NAME
) == 0);
296 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
297 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
298 if (IsStrtabShdr(shdr
)) {
311 Elf_Shdr
*StrtabShdr
;
312 UINT8
*StrtabContents
;
316 if (Sym
->st_name
== 0) {
320 StrtabShdr
= FindStrtabShdr();
321 if (StrtabShdr
== NULL
) {
325 assert(Sym
->st_name
< StrtabShdr
->sh_size
);
327 StrtabContents
= (UINT8
*)mEhdr
+ StrtabShdr
->sh_offset
;
330 for (i
= Sym
->st_name
; (i
< StrtabShdr
->sh_size
) && !foundEnd
; i
++) {
331 foundEnd
= (BOOLEAN
)(StrtabContents
[i
] == 0);
335 return StrtabContents
+ Sym
->st_name
;
339 // Find the ELF section hosting the GOT from an ELF Rva
340 // of a single GOT entry. Normally, GOT is placed in
341 // ELF .text section, so assume once we find in which
342 // section the GOT is, all GOT entries are there, and
347 FindElfGOTSectionFromGOTEntryElfRva (
348 Elf64_Addr GOTEntryElfRva
352 if (mGOTShdr
!= NULL
) {
353 if (GOTEntryElfRva
>= mGOTShdr
->sh_addr
&&
354 GOTEntryElfRva
< mGOTShdr
->sh_addr
+ mGOTShdr
->sh_size
) {
357 Error (NULL
, 0, 3000, "Unsupported", "FindElfGOTSectionFromGOTEntryElfRva: GOT entries found in multiple sections.");
360 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
361 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
362 if (GOTEntryElfRva
>= shdr
->sh_addr
&&
363 GOTEntryElfRva
< shdr
->sh_addr
+ shdr
->sh_size
) {
369 Error (NULL
, 0, 3000, "Invalid", "FindElfGOTSectionFromGOTEntryElfRva: ElfRva 0x%016LX for GOT entry not found in any section.", GOTEntryElfRva
);
374 // Stores locations of GOT entries in COFF image.
375 // Returns TRUE if GOT entry is new.
376 // Simple implementation as number of GOT
377 // entries is expected to be low.
382 AccumulateCoffGOTEntries (
387 if (mGOTCoffEntries
!= NULL
) {
388 for (i
= 0; i
< mGOTNumCoffEntries
; i
++) {
389 if (mGOTCoffEntries
[i
] == GOTCoffEntry
) {
394 if (mGOTCoffEntries
== NULL
) {
395 mGOTCoffEntries
= (UINT32
*)malloc(5 * sizeof *mGOTCoffEntries
);
396 if (mGOTCoffEntries
== NULL
) {
397 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
399 assert (mGOTCoffEntries
!= NULL
);
400 mGOTMaxCoffEntries
= 5;
401 mGOTNumCoffEntries
= 0;
402 } else if (mGOTNumCoffEntries
== mGOTMaxCoffEntries
) {
403 mGOTCoffEntries
= (UINT32
*)realloc(mGOTCoffEntries
, 2 * mGOTMaxCoffEntries
* sizeof *mGOTCoffEntries
);
404 if (mGOTCoffEntries
== NULL
) {
405 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
407 assert (mGOTCoffEntries
!= NULL
);
408 mGOTMaxCoffEntries
+= mGOTMaxCoffEntries
;
410 mGOTCoffEntries
[mGOTNumCoffEntries
++] = GOTCoffEntry
;
415 // 32-bit Unsigned integer comparator for qsort.
424 if (*(const UINT32
*)lhs
< *(const UINT32
*)rhs
) {
427 return *(const UINT32
*)lhs
> *(const UINT32
*)rhs
;
431 // Emit accumulated Coff GOT entry relocations into
432 // Coff image. This function performs its job
433 // once and then releases the entry list, so
434 // it can safely be called multiple times.
443 if (mGOTCoffEntries
== NULL
) {
447 // Emit Coff relocations with Rvas ordered.
452 sizeof *mGOTCoffEntries
,
454 for (i
= 0; i
< mGOTNumCoffEntries
; i
++) {
455 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X", mGOTCoffEntries
[i
]);
458 EFI_IMAGE_REL_BASED_DIR64
);
460 free(mGOTCoffEntries
);
461 mGOTCoffEntries
= NULL
;
462 mGOTMaxCoffEntries
= 0;
463 mGOTNumCoffEntries
= 0;
466 // RISC-V 64 specific Elf WriteSection function.
470 WriteSectionRiscV64 (
479 Elf64_Addr GOTEntryRva
;
481 switch (ELF_R_TYPE(Rel
->r_info
)) {
486 *(UINT64
*)Targ
= Sym
->st_value
+ Rel
->r_addend
;
490 *(UINT64
*)Targ
= Sym
->st_value
+ Rel
->r_addend
;
494 mRiscVPass1Targ
= Targ
;
495 mRiscVPass1Sym
= SymShdr
;
496 mRiscVPass1SymSecIndex
= Sym
->st_shndx
;
500 if (mRiscVPass1Sym
== SymShdr
&& mRiscVPass1Targ
!= NULL
&& mRiscVPass1SymSecIndex
== Sym
->st_shndx
&& mRiscVPass1SymSecIndex
!= 0) {
501 Value
= (UINT32
)(RV_X(*(UINT32
*)mRiscVPass1Targ
, 12, 20) << 12);
502 Value2
= (UINT32
)(RV_X(*(UINT32
*)Targ
, 20, 12));
503 if (Value2
& (RISCV_IMM_REACH
/2)) {
504 Value2
|= ~(RISCV_IMM_REACH
-1);
507 Value
= Value
- (UINT32
)SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
508 Value2
= RISCV_CONST_HIGH_PART (Value
);
509 *(UINT32
*)mRiscVPass1Targ
= (RV_X (Value2
, 12, 20) << 12) | \
510 (RV_X (*(UINT32
*)mRiscVPass1Targ
, 0, 12));
511 *(UINT32
*)Targ
= (RV_X (Value
, 0, 12) << 20) | \
512 (RV_X (*(UINT32
*)Targ
, 0, 20));
514 mRiscVPass1Sym
= NULL
;
515 mRiscVPass1Targ
= NULL
;
516 mRiscVPass1SymSecIndex
= 0;
520 if (mRiscVPass1Sym
== SymShdr
&& mRiscVPass1Targ
!= NULL
&& mRiscVPass1SymSecIndex
== Sym
->st_shndx
&& mRiscVPass1SymSecIndex
!= 0) {
521 Value
= (UINT32
)(RV_X(*(UINT32
*)mRiscVPass1Targ
, 12, 20) << 12);
522 Value2
= (UINT32
)(RV_X(*(UINT32
*)Targ
, 7, 5) | (RV_X(*(UINT32
*)Targ
, 25, 7) << 5));
523 if (Value2
& (RISCV_IMM_REACH
/2)) {
524 Value2
|= ~(RISCV_IMM_REACH
-1);
527 Value
= Value
- (UINT32
)SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
528 Value2
= RISCV_CONST_HIGH_PART (Value
);
529 *(UINT32
*)mRiscVPass1Targ
= (RV_X (Value2
, 12, 20) << 12) | \
530 (RV_X (*(UINT32
*)mRiscVPass1Targ
, 0, 12));
531 Value2
= *(UINT32
*)Targ
& 0x01fff07f;
532 Value
&= RISCV_IMM_REACH
- 1;
533 *(UINT32
*)Targ
= Value2
| (UINT32
)(((RV_X(Value
, 0, 5) << 7) | (RV_X(Value
, 5, 7) << 25)));
535 mRiscVPass1Sym
= NULL
;
536 mRiscVPass1Targ
= NULL
;
537 mRiscVPass1SymSecIndex
= 0;
540 case R_RISCV_GOT_HI20
:
541 GOTEntryRva
= (Sym
->st_value
- Rel
->r_offset
);
542 mRiscVPass1Offset
= RV_X(GOTEntryRva
, 0, 12);
543 Value
= (UINT32
)RV_X(GOTEntryRva
, 12, 20);
544 *(UINT32
*)Targ
= (Value
<< 12) | (RV_X(*(UINT32
*)Targ
, 0, 12));
546 mRiscVPass1Targ
= Targ
;
547 mRiscVPass1Sym
= SymShdr
;
548 mRiscVPass1SymSecIndex
= Sym
->st_shndx
;
549 mRiscVPass1GotFixup
= 1;
552 case R_RISCV_PCREL_HI20
:
553 mRiscVPass1Targ
= Targ
;
554 mRiscVPass1Sym
= SymShdr
;
555 mRiscVPass1SymSecIndex
= Sym
->st_shndx
;
557 Value
= (UINT32
)(RV_X(*(UINT32
*)mRiscVPass1Targ
, 12, 20));
560 case R_RISCV_PCREL_LO12_I
:
561 if (mRiscVPass1Targ
!= NULL
&& mRiscVPass1Sym
!= NULL
&& mRiscVPass1SymSecIndex
!= 0) {
563 Value2
= (UINT32
)(RV_X(*(UINT32
*)mRiscVPass1Targ
, 12, 20));
565 if(mRiscVPass1GotFixup
) {
566 Value
= (UINT32
)(mRiscVPass1Offset
);
568 Value
= (UINT32
)(RV_X(*(UINT32
*)Targ
, 20, 12));
569 if(Value
& (RISCV_IMM_REACH
/2)) {
570 Value
|= ~(RISCV_IMM_REACH
-1);
573 Value
= Value
- (UINT32
)mRiscVPass1Sym
->sh_addr
+ mCoffSectionsOffset
[mRiscVPass1SymSecIndex
];
575 if(-2048 > (INT32
)Value
) {
576 i
= (((INT32
)Value
* -1) / 4096);
579 if(-2048 > (INT32
)Value
) {
584 else if( 2047 < (INT32
)Value
) {
588 if(2047 < (INT32
)Value
) {
594 if(mRiscVPass1GotFixup
) {
595 *(UINT32
*)Targ
= (RV_X((UINT32
)Value
, 0, 12) << 20)
596 | (RV_X(*(UINT32
*)Targ
, 0, 20));
597 // Convert LD instruction to ADDI
599 // |31 20|19 15|14 12|11 7|6 0|
600 // |-----------------------------------------|
601 // |imm[11:0] | rs1 | 011 | rd | 0000011 | LD
602 // -----------------------------------------
604 // |-----------------------------------------|
605 // |imm[11:0] | rs1 | 000 | rd | 0010011 | ADDI
606 // -----------------------------------------
608 // To convert, let's first reset bits 12-14 and 0-6 using ~0x707f
609 // Then modify the opcode to ADDI (0010011)
610 // All other fields will remain same.
612 *(UINT32
*)Targ
= ((*(UINT32
*)Targ
& ~0x707f) | 0x13);
614 *(UINT32
*)Targ
= (RV_X(Value
, 0, 12) << 20) | (RV_X(*(UINT32
*)Targ
, 0, 20));
616 *(UINT32
*)mRiscVPass1Targ
= (RV_X(Value2
, 0, 20)<<12) | (RV_X(*(UINT32
*)mRiscVPass1Targ
, 0, 12));
618 mRiscVPass1Sym
= NULL
;
619 mRiscVPass1Targ
= NULL
;
620 mRiscVPass1SymSecIndex
= 0;
621 mRiscVPass1Offset
= 0;
622 mRiscVPass1GotFixup
= 0;
631 case R_RISCV_GPREL_I
:
632 case R_RISCV_GPREL_S
:
634 case R_RISCV_CALL_PLT
:
635 case R_RISCV_RVC_BRANCH
:
636 case R_RISCV_RVC_JUMP
:
646 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_RISCV64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
651 // Elf functions interface implementation
661 EFI_IMAGE_DOS_HEADER
*DosHdr
;
662 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
665 BOOLEAN FoundSection
;
671 // Coff file start with a DOS header.
673 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
674 mNtHdrOffset
= mCoffOffset
;
675 switch (mEhdr
->e_machine
) {
679 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
682 VerboseMsg ("%s unknown e_machine type %hu. Assume X64", mInImageName
, mEhdr
->e_machine
);
683 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
687 mTableOffset
= mCoffOffset
;
688 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
691 // Set mCoffAlignment to the maximum alignment of the input sections
694 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
695 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
696 if (shdr
->sh_addralign
<= mCoffAlignment
) {
699 if (IsTextShdr(shdr
) || IsDataShdr(shdr
) || IsHiiRsrcShdr(shdr
)) {
700 mCoffAlignment
= (UINT32
)shdr
->sh_addralign
;
705 // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT
707 if (mCoffAlignment
> MAX_COFF_ALIGNMENT
) {
708 Error (NULL
, 0, 3000, "Invalid", "Section alignment is larger than MAX_COFF_ALIGNMENT.");
714 // Move the PE/COFF header right before the first section. This will help us
715 // save space when converting to TE.
717 if (mCoffAlignment
> mCoffOffset
) {
718 mNtHdrOffset
+= mCoffAlignment
- mCoffOffset
;
719 mTableOffset
+= mCoffAlignment
- mCoffOffset
;
720 mCoffOffset
= mCoffAlignment
;
724 // First text sections.
726 mCoffOffset
= CoffAlign(mCoffOffset
);
727 mTextOffset
= mCoffOffset
;
728 FoundSection
= FALSE
;
730 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
731 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
732 if (IsTextShdr(shdr
)) {
733 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
734 // the alignment field is valid
735 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
736 // if the section address is aligned we must align PE/COFF
737 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
739 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
743 /* Relocate entry. */
744 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
745 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
746 CoffEntry
= (UINT32
) (mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
);
750 // Set mTextOffset with the offset of the first '.text' section
753 mTextOffset
= mCoffOffset
;
757 mCoffSectionsOffset
[i
] = mCoffOffset
;
758 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
763 if (!FoundSection
&& mOutImageType
!= FW_ACPI_IMAGE
) {
764 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
768 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
769 mCoffOffset
= CoffAlign(mCoffOffset
);
771 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
772 Warning (NULL
, 0, 0, NULL
, "Multiple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
776 // Then data sections.
778 mDataOffset
= mCoffOffset
;
779 FoundSection
= FALSE
;
781 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
782 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
783 if (IsDataShdr(shdr
)) {
784 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
785 // the alignment field is valid
786 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
787 // if the section address is aligned we must align PE/COFF
788 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
790 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
795 // Set mDataOffset with the offset of the first '.data' section
798 mDataOffset
= mCoffOffset
;
801 mCoffSectionsOffset
[i
] = mCoffOffset
;
802 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
808 // Make room for .debug data in .data (or .text if .data is empty) instead of
809 // putting it in a section of its own. This is explicitly allowed by the
810 // PE/COFF spec, and prevents bloat in the binary when using large values for
811 // section alignment.
813 if (SectionCount
> 0) {
814 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
816 mCoffOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
) +
817 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) +
818 strlen(mInImageName
) + 1;
820 mCoffOffset
= CoffAlign(mCoffOffset
);
821 if (SectionCount
== 0) {
822 mDataOffset
= mCoffOffset
;
825 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
826 Warning (NULL
, 0, 0, NULL
, "Multiple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
830 // The HII resource sections.
832 mHiiRsrcOffset
= mCoffOffset
;
833 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
834 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
835 if (IsHiiRsrcShdr(shdr
)) {
836 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
837 // the alignment field is valid
838 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
839 // if the section address is aligned we must align PE/COFF
840 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
842 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
845 if (shdr
->sh_size
!= 0) {
846 mHiiRsrcOffset
= mCoffOffset
;
847 mCoffSectionsOffset
[i
] = mCoffOffset
;
848 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
849 mCoffOffset
= CoffAlign(mCoffOffset
);
850 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
856 mRelocOffset
= mCoffOffset
;
859 // Allocate base Coff file. Will be expanded later for relocations.
861 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
862 if (mCoffFile
== NULL
) {
863 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
865 assert (mCoffFile
!= NULL
);
866 memset(mCoffFile
, 0, mCoffOffset
);
871 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
872 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
873 DosHdr
->e_lfanew
= mNtHdrOffset
;
875 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
877 NtHdr
->Pe32Plus
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
879 switch (mEhdr
->e_machine
) {
881 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
882 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
885 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_AARCH64
;
886 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
889 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_RISCV64
;
890 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
894 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN
)mEhdr
->e_machine
);
895 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
896 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
899 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
900 NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
901 mImageTimeStamp
= NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
;
902 NtHdr
->Pe32Plus
.FileHeader
.PointerToSymbolTable
= 0;
903 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSymbols
= 0;
904 NtHdr
->Pe32Plus
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32Plus
.OptionalHeader
);
905 NtHdr
->Pe32Plus
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
906 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
907 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
908 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE
;
910 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
911 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
912 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfUninitializedData
= 0;
913 NtHdr
->Pe32Plus
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
915 NtHdr
->Pe32Plus
.OptionalHeader
.BaseOfCode
= mTextOffset
;
917 NtHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= 0;
918 NtHdr
->Pe32Plus
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
919 NtHdr
->Pe32Plus
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
920 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= 0;
922 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
923 NtHdr
->Pe32Plus
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
928 if ((mDataOffset
- mTextOffset
) > 0) {
929 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
930 EFI_IMAGE_SCN_CNT_CODE
931 | EFI_IMAGE_SCN_MEM_EXECUTE
932 | EFI_IMAGE_SCN_MEM_READ
);
934 // Don't make a section of size 0.
935 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
938 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
939 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
940 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
941 | EFI_IMAGE_SCN_MEM_WRITE
942 | EFI_IMAGE_SCN_MEM_READ
);
944 // Don't make a section of size 0.
945 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
948 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
949 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
950 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
951 | EFI_IMAGE_SCN_MEM_READ
);
953 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
954 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
956 // Don't make a section of size 0.
957 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
965 SECTION_FILTER_TYPES FilterType
971 BOOLEAN (*Filter
)(Elf_Shdr
*);
972 Elf64_Addr GOTEntryRva
;
975 // Initialize filter pointer
977 switch (FilterType
) {
982 Filter
= IsHiiRsrcShdr
;
992 // First: copy sections.
994 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
995 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
996 if ((*Filter
)(Shdr
)) {
997 switch (Shdr
->sh_type
) {
1000 if (Shdr
->sh_offset
+ Shdr
->sh_size
> mFileBufferSize
) {
1003 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
1004 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
1005 (size_t) Shdr
->sh_size
);
1009 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, (size_t) Shdr
->sh_size
);
1014 // Ignore for unknown section type.
1016 VerboseMsg ("%s unknown section type %x. We ignore this unknown section type.", mInImageName
, (unsigned)Shdr
->sh_type
);
1023 // Second: apply relocations.
1025 VerboseMsg ("Applying Relocations...");
1026 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
1028 // Determine if this is a relocation section.
1030 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
1031 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
1036 // If this is a ET_DYN (PIE) executable, we will encounter a dynamic SHT_RELA
1037 // section that applies to the entire binary, and which will have its section
1038 // index set to #0 (which is a NULL section with the SHF_ALLOC bit cleared).
1040 // In the absence of GOT based relocations,
1041 // this RELA section will contain redundant R_xxx_RELATIVE relocations, one
1042 // for every R_xxx_xx64 relocation appearing in the per-section RELA sections.
1043 // (i.e., .rela.text and .rela.data)
1045 if (RelShdr
->sh_info
== 0) {
1050 // Relocation section found. Now extract section information that the relocations
1051 // apply to in the ELF data and the new COFF data.
1053 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
1054 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
1057 // Only process relocations for the current filter type.
1059 if (RelShdr
->sh_type
== SHT_RELA
&& (*Filter
)(SecShdr
)) {
1063 // Determine the symbol table referenced by the relocation data.
1065 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
1066 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
1069 // Process all relocation entries for this section.
1071 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= (UINT32
) RelShdr
->sh_entsize
) {
1074 // Set pointer to relocation entry
1076 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
1079 // Set pointer to symbol table entry associated with the relocation entry.
1081 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
1087 // Check section header index found in symbol table and get the section
1090 if (Sym
->st_shndx
== SHN_UNDEF
1091 || Sym
->st_shndx
>= mEhdr
->e_shnum
) {
1092 const UINT8
*SymName
= GetSymName(Sym
);
1093 if (SymName
== NULL
) {
1094 SymName
= (const UINT8
*)"<unknown>";
1098 // Skip error on EM_RISCV64 becasue no symble name is built
1099 // from RISC-V toolchain.
1101 if (mEhdr
->e_machine
!= EM_RISCV64
) {
1102 Error (NULL
, 0, 3000, "Invalid",
1103 "%s: Bad definition for symbol '%s'@%#llx or unsupported symbol type. "
1104 "For example, absolute and undefined symbols are not supported.",
1105 mInImageName
, SymName
, Sym
->st_value
);
1111 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
1114 // Convert the relocation data to a pointer into the coff file.
1117 // r_offset is the virtual address of the storage unit to be relocated.
1118 // sh_addr is the virtual address for the base of the section.
1120 // r_offset in a memory address.
1121 // Convert it to a pointer in the coff file.
1123 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
1126 // Determine how to handle each relocation type based on the machine type.
1128 if (mEhdr
->e_machine
== EM_X86_64
) {
1129 switch (ELF_R_TYPE(Rel
->r_info
)) {
1134 // Absolute relocation.
1136 VerboseMsg ("R_X86_64_64");
1137 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1138 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1140 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1141 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
1144 VerboseMsg ("R_X86_64_32");
1145 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1146 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1148 *(UINT32
*)Targ
= (UINT32
)((UINT64
)(*(UINT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
1149 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1152 VerboseMsg ("R_X86_64_32S");
1153 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1154 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1156 *(INT32
*)Targ
= (INT32
)((INT64
)(*(INT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
1157 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1160 case R_X86_64_PLT32
:
1162 // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is
1163 // possible since we know all code symbol references resolve to
1164 // definitions in the same module (UEFI has no shared libraries),
1165 // and so there is never a reason to jump via a PLT entry,
1166 // allowing us to resolve the reference using the symbol directly.
1168 VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");
1172 // Relative relocation: Symbol - Ip + Addend
1174 VerboseMsg ("R_X86_64_PC32");
1175 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1176 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1178 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
1179 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
1180 - (SecOffset
- SecShdr
->sh_addr
));
1181 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1183 case R_X86_64_GOTPCREL
:
1184 case R_X86_64_GOTPCRELX
:
1185 case R_X86_64_REX_GOTPCRELX
:
1186 VerboseMsg ("R_X86_64_GOTPCREL family");
1187 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1188 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1190 GOTEntryRva
= Rel
->r_offset
- Rel
->r_addend
+ *(INT32
*)Targ
;
1191 FindElfGOTSectionFromGOTEntryElfRva(GOTEntryRva
);
1192 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
1193 + (mCoffSectionsOffset
[mGOTShindex
] - mGOTShdr
->sh_addr
)
1194 - (SecOffset
- SecShdr
->sh_addr
));
1195 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1196 GOTEntryRva
+= (mCoffSectionsOffset
[mGOTShindex
] - mGOTShdr
->sh_addr
); // ELF Rva -> COFF Rva
1197 if (AccumulateCoffGOTEntries((UINT32
)GOTEntryRva
)) {
1199 // Relocate GOT entry if it's the first time we run into it
1201 Targ
= mCoffFile
+ GOTEntryRva
;
1203 // Limitation: The following three statements assume memory
1204 // at *Targ is valid because the section containing the GOT
1205 // has already been copied from the ELF image to the Coff image.
1206 // This pre-condition presently holds because the GOT is placed
1207 // in section .text, and the ELF text sections are all copied
1208 // prior to reaching this point.
1209 // If the pre-condition is violated in the future, this fixup
1210 // either needs to be deferred after the GOT section is copied
1211 // to the Coff image, or the fixup should be performed on the
1212 // source Elf image instead of the destination Coff image.
1214 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1215 (UINT32
)GOTEntryRva
,
1217 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1218 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
1222 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1224 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
1226 switch (ELF_R_TYPE(Rel
->r_info
)) {
1229 case R_AARCH64_LD64_GOT_LO12_NC
:
1231 // Convert into an ADD instruction - see R_AARCH64_ADR_GOT_PAGE below.
1233 *(UINT32
*)Targ
&= 0x3ff;
1234 *(UINT32
*)Targ
|= 0x91000000 | ((Sym
->st_value
& 0xfff) << 10);
1237 case R_AARCH64_ADR_GOT_PAGE
:
1239 // This relocation points to the GOT entry that contains the absolute
1240 // address of the symbol we are referring to. Since EDK2 only uses
1241 // fully linked binaries, we can avoid the indirection, and simply
1242 // refer to the symbol directly. This implies having to patch the
1243 // subsequent LDR instruction (covered by a R_AARCH64_LD64_GOT_LO12_NC
1244 // relocation) into an ADD instruction - this is handled above.
1246 Offset
= (Sym
->st_value
- (Rel
->r_offset
& ~0xfff)) >> 12;
1248 *(UINT32
*)Targ
&= 0x9000001f;
1249 *(UINT32
*)Targ
|= ((Offset
& 0x1ffffc) << (5 - 2)) | ((Offset
& 0x3) << 29);
1253 case R_AARCH64_ADR_PREL_PG_HI21
:
1255 // In order to handle Cortex-A53 erratum #843419, the LD linker may
1256 // convert ADRP instructions into ADR instructions, but without
1257 // updating the static relocation type, and so we may end up here
1258 // while the instruction in question is actually ADR. So let's
1259 // just disregard it: the section offset check we apply below to
1260 // ADR instructions will trigger for its R_AARCH64_xxx_ABS_LO12_NC
1261 // companion instruction as well, so it is safe to omit it here.
1263 if ((*(UINT32
*)Targ
& BIT31
) == 0) {
1268 // AArch64 PG_H21 relocations are typically paired with ABS_LO12
1269 // relocations, where a PC-relative reference with +/- 4 GB range is
1270 // split into a relative high part and an absolute low part. Since
1271 // the absolute low part represents the offset into a 4 KB page, we
1272 // either have to convert the ADRP into an ADR instruction, or we
1273 // need to use a section alignment of at least 4 KB, so that the
1274 // binary appears at a correct offset at runtime. In any case, we
1275 // have to make sure that the 4 KB relative offsets of both the
1276 // section containing the reference as well as the section to which
1277 // it refers have not been changed during PE/COFF conversion (i.e.,
1278 // in ScanSections64() above).
1280 if (mCoffAlignment
< 0x1000) {
1282 // Attempt to convert the ADRP into an ADR instruction.
1283 // This is only possible if the symbol is within +/- 1 MB.
1286 // Decode the ADRP instruction
1287 Offset
= (INT32
)((*(UINT32
*)Targ
& 0xffffe0) << 8);
1288 Offset
= (Offset
<< (6 - 5)) | ((*(UINT32
*)Targ
& 0x60000000) >> (29 - 12));
1291 // ADRP offset is relative to the previous page boundary,
1292 // whereas ADR offset is relative to the instruction itself.
1293 // So fix up the offset so it points to the page containing
1296 Offset
-= (UINTN
)(Targ
- mCoffFile
) & 0xfff;
1298 if (Offset
< -0x100000 || Offset
> 0xfffff) {
1299 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s due to its size (> 1 MB), this module requires 4 KB section alignment.",
1304 // Re-encode the offset as an ADR instruction
1305 *(UINT32
*)Targ
&= 0x1000001f;
1306 *(UINT32
*)Targ
|= ((Offset
& 0x1ffffc) << (5 - 2)) | ((Offset
& 0x3) << 29);
1310 case R_AARCH64_ADD_ABS_LO12_NC
:
1311 case R_AARCH64_LDST8_ABS_LO12_NC
:
1312 case R_AARCH64_LDST16_ABS_LO12_NC
:
1313 case R_AARCH64_LDST32_ABS_LO12_NC
:
1314 case R_AARCH64_LDST64_ABS_LO12_NC
:
1315 case R_AARCH64_LDST128_ABS_LO12_NC
:
1316 if (((SecShdr
->sh_addr
^ SecOffset
) & 0xfff) != 0 ||
1317 ((SymShdr
->sh_addr
^ mCoffSectionsOffset
[Sym
->st_shndx
]) & 0xfff) != 0) {
1318 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 small code model requires identical ELF and PE/COFF section offsets modulo 4 KB.",
1324 case R_AARCH64_ADR_PREL_LO21
:
1325 case R_AARCH64_CONDBR19
:
1326 case R_AARCH64_LD_PREL_LO19
:
1327 case R_AARCH64_CALL26
:
1328 case R_AARCH64_JUMP26
:
1329 case R_AARCH64_PREL64
:
1330 case R_AARCH64_PREL32
:
1331 case R_AARCH64_PREL16
:
1333 // The GCC toolchains (i.e., binutils) may corrupt section relative
1334 // relocations when emitting relocation sections into fully linked
1335 // binaries. More specifically, they tend to fail to take into
1336 // account the fact that a '.rodata + XXX' relocation needs to have
1337 // its addend recalculated once .rodata is merged into the .text
1338 // section, and the relocation emitted into the .rela.text section.
1340 // We cannot really recover from this loss of information, so the
1341 // only workaround is to prevent having to recalculate any relative
1342 // relocations at all, by using a linker script that ensures that
1343 // the offset between the Place and the Symbol is the same in both
1344 // the ELF and the PE/COFF versions of the binary.
1346 if ((SymShdr
->sh_addr
- SecShdr
->sh_addr
) !=
1347 (mCoffSectionsOffset
[Sym
->st_shndx
] - SecOffset
)) {
1348 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 relative relocations require identical ELF and PE/COFF section offsets",
1353 // Absolute relocations.
1354 case R_AARCH64_ABS64
:
1355 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1359 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1361 } else if (mEhdr
->e_machine
== EM_RISCV64
) {
1363 // Write section for RISC-V 64 architecture.
1365 WriteSectionRiscV64 (Rel
, Targ
, SymShdr
, Sym
);
1367 Error (NULL
, 0, 3000, "Invalid", "Not a supported machine type");
1378 WriteRelocations64 (
1383 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1384 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
1385 UINT32 RiscVRelType
;
1387 for (Index
= 0; Index
< mEhdr
->e_shnum
; Index
++) {
1388 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
1389 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
1390 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
1391 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
1394 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
1395 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
1397 if (mEhdr
->e_machine
== EM_X86_64
) {
1398 switch (ELF_R_TYPE(Rel
->r_info
)) {
1401 case R_X86_64_PLT32
:
1402 case R_X86_64_GOTPCREL
:
1403 case R_X86_64_GOTPCRELX
:
1404 case R_X86_64_REX_GOTPCRELX
:
1407 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",
1408 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
1410 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1411 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1412 EFI_IMAGE_REL_BASED_DIR64
);
1415 // R_X86_64_32 and R_X86_64_32S are ELF64 relocations emitted when using
1416 // the SYSV X64 ABI small non-position-independent code model.
1417 // R_X86_64_32 is used for unsigned 32-bit immediates with a 32-bit operand
1418 // size. The value is either not extended, or zero-extended to 64 bits.
1419 // R_X86_64_32S is used for either signed 32-bit non-rip-relative displacements
1420 // or signed 32-bit immediates with a 64-bit operand size. The value is
1421 // sign-extended to 64 bits.
1422 // EFI_IMAGE_REL_BASED_HIGHLOW is a PE relocation that uses 32-bit arithmetic
1423 // for rebasing an image.
1424 // EFI PE binaries declare themselves EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE and
1425 // may load above 2GB. If an EFI PE binary with a converted R_X86_64_32S
1426 // relocation is loaded above 2GB, the value will get sign-extended to the
1427 // negative part of the 64-bit address space. The negative part of the 64-bit
1428 // address space is unmapped, so accessing such an address page-faults.
1429 // In order to support R_X86_64_32S, it is necessary to unset
1430 // EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE, and the EFI PE loader must implement
1431 // this flag and abstain from loading such a PE binary above 2GB.
1432 // Since this feature is not supported, support for R_X86_64_32S (and hence
1433 // the small non-position-independent code model) is disabled.
1435 // case R_X86_64_32S:
1437 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",
1438 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
1440 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1441 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1442 EFI_IMAGE_REL_BASED_HIGHLOW
);
1445 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1447 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
1449 switch (ELF_R_TYPE(Rel
->r_info
)) {
1450 case R_AARCH64_ADR_PREL_LO21
:
1451 case R_AARCH64_CONDBR19
:
1452 case R_AARCH64_LD_PREL_LO19
:
1453 case R_AARCH64_CALL26
:
1454 case R_AARCH64_JUMP26
:
1455 case R_AARCH64_PREL64
:
1456 case R_AARCH64_PREL32
:
1457 case R_AARCH64_PREL16
:
1458 case R_AARCH64_ADR_PREL_PG_HI21
:
1459 case R_AARCH64_ADD_ABS_LO12_NC
:
1460 case R_AARCH64_LDST8_ABS_LO12_NC
:
1461 case R_AARCH64_LDST16_ABS_LO12_NC
:
1462 case R_AARCH64_LDST32_ABS_LO12_NC
:
1463 case R_AARCH64_LDST64_ABS_LO12_NC
:
1464 case R_AARCH64_LDST128_ABS_LO12_NC
:
1465 case R_AARCH64_ADR_GOT_PAGE
:
1466 case R_AARCH64_LD64_GOT_LO12_NC
:
1468 // No fixups are required for relative relocations, provided that
1469 // the relative offsets between sections have been preserved in
1470 // the ELF to PE/COFF conversion. We have already asserted that
1471 // this is the case in WriteSections64 ().
1475 case R_AARCH64_ABS64
:
1477 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1478 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1479 EFI_IMAGE_REL_BASED_DIR64
);
1482 case R_AARCH64_ABS32
:
1484 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1485 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1486 EFI_IMAGE_REL_BASED_HIGHLOW
);
1490 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1492 } else if (mEhdr
->e_machine
== EM_RISCV64
) {
1493 RiscVRelType
= ELF_R_TYPE(Rel
->r_info
);
1494 switch (RiscVRelType
) {
1500 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1501 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1502 EFI_IMAGE_REL_BASED_HIGHLOW
);
1507 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1508 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1509 EFI_IMAGE_REL_BASED_DIR64
);
1514 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1515 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1516 EFI_IMAGE_REL_BASED_RISCV_HI20
);
1519 case R_RISCV_LO12_I
:
1521 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1522 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1523 EFI_IMAGE_REL_BASED_RISCV_LOW12I
);
1526 case R_RISCV_LO12_S
:
1528 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1529 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1530 EFI_IMAGE_REL_BASED_RISCV_LOW12S
);
1535 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1536 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1537 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1542 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1543 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1544 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1549 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1550 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1551 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1556 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1557 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1558 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1561 case R_RISCV_BRANCH
:
1563 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1564 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1565 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1570 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1571 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1572 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1575 case R_RISCV_GPREL_I
:
1576 case R_RISCV_GPREL_S
:
1578 case R_RISCV_CALL_PLT
:
1579 case R_RISCV_RVC_BRANCH
:
1580 case R_RISCV_RVC_JUMP
:
1587 case R_RISCV_PCREL_HI20
:
1588 case R_RISCV_GOT_HI20
:
1589 case R_RISCV_PCREL_LO12_I
:
1593 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_RISCV64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1596 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
1599 if (mEhdr
->e_machine
== EM_X86_64
&& RelShdr
->sh_info
== mGOTShindex
) {
1601 // Tack relocations for GOT entries after other relocations for
1602 // the section the GOT is in, as it's usually found at the end
1603 // of the section. This is done in order to maintain Rva order
1604 // of Coff relocations.
1606 EmitGOTRelocations();
1612 if (mEhdr
->e_machine
== EM_X86_64
) {
1614 // This is a safety net just in case the GOT is in a section
1615 // with no other relocations and the first invocation of
1616 // EmitGOTRelocations() above was skipped. This invocation
1617 // does not maintain Rva order of Coff relocations.
1618 // At present, with a single text section, all references to
1619 // the GOT and the GOT itself reside in section .text, so
1620 // if there's a GOT at all, the first invocation above
1623 EmitGOTRelocations();
1626 // Pad by adding empty entries.
1628 while (mCoffOffset
& (mCoffAlignment
- 1)) {
1629 CoffAddFixupEntry(0);
1632 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1633 Dir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1634 Dir
->Size
= mCoffOffset
- mRelocOffset
;
1635 if (Dir
->Size
== 0) {
1636 // If no relocations, null out the directory entry and don't add the .reloc section
1637 Dir
->VirtualAddress
= 0;
1638 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
1640 Dir
->VirtualAddress
= mRelocOffset
;
1641 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
1642 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1643 | EFI_IMAGE_SCN_MEM_DISCARDABLE
1644 | EFI_IMAGE_SCN_MEM_READ
);
1655 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1656 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
1657 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
1658 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
1660 Len
= strlen(mInImageName
) + 1;
1662 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ mDebugOffset
);
1663 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
1664 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
1665 Dir
->RVA
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1666 Dir
->FileOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1668 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
1669 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
1670 strcpy ((char *)(Nb10
+ 1), mInImageName
);
1673 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1674 DataDir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
1675 DataDir
->VirtualAddress
= mDebugOffset
;
1676 DataDir
->Size
= sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1685 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1690 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1691 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
1700 if (mCoffSectionsOffset
!= NULL
) {
1701 free (mCoffSectionsOffset
);