4 Copyright (c) 2010 - 2021, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2013-2022, ARM Ltd. All rights reserved.<BR>
6 Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
7 Portions Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include "WinNtInclude.h"
26 #include <Common/UefiBaseTypes.h>
27 #include <IndustryStandard/PeImage.h>
29 #include "PeCoffLib.h"
30 #include "EfiUtilityMsgs.h"
33 #include "ElfConvert.h"
34 #include "Elf64Convert.h"
45 SECTION_FILTER_TYPES FilterType
79 // Rename ELF32 structures to common names to help when porting to ELF64.
81 typedef Elf64_Shdr Elf_Shdr
;
82 typedef Elf64_Ehdr Elf_Ehdr
;
83 typedef Elf64_Rel Elf_Rel
;
84 typedef Elf64_Rela Elf_Rela
;
85 typedef Elf64_Sym Elf_Sym
;
86 typedef Elf64_Phdr Elf_Phdr
;
87 typedef Elf64_Dyn Elf_Dyn
;
88 #define ELFCLASS ELFCLASS64
89 #define ELF_R_TYPE(r) ELF64_R_TYPE(r)
90 #define ELF_R_SYM(r) ELF64_R_SYM(r)
93 // Well known ELF structures.
95 STATIC Elf_Ehdr
*mEhdr
;
96 STATIC Elf_Shdr
*mShdrBase
;
97 STATIC Elf_Phdr
*mPhdrBase
;
102 STATIC Elf_Shdr
*mGOTShdr
= NULL
;
103 STATIC UINT32 mGOTShindex
= 0;
104 STATIC UINT32
*mGOTCoffEntries
= NULL
;
105 STATIC UINT32 mGOTMaxCoffEntries
= 0;
106 STATIC UINT32 mGOTNumCoffEntries
= 0;
111 STATIC UINT32 mCoffAlignment
= 0x20;
114 // PE section alignment.
116 STATIC UINT16 mCoffNbrSections
= 4;
119 // ELF sections to offset in Coff file.
121 STATIC UINT32
*mCoffSectionsOffset
= NULL
;
124 // Offsets in COFF file
126 STATIC UINT32 mNtHdrOffset
;
127 STATIC UINT32 mTextOffset
;
128 STATIC UINT32 mDataOffset
;
129 STATIC UINT32 mHiiRsrcOffset
;
130 STATIC UINT32 mRelocOffset
;
131 STATIC UINT32 mDebugOffset
;
132 STATIC UINT32 mExportOffset
;
134 // Used for RISC-V relocations.
136 STATIC UINT8
*mRiscVPass1Targ
= NULL
;
137 STATIC Elf_Shdr
*mRiscVPass1Sym
= NULL
;
138 STATIC Elf64_Half mRiscVPass1SymSecIndex
= 0;
139 STATIC INT32 mRiscVPass1Offset
;
140 STATIC INT32 mRiscVPass1GotFixup
;
143 // Used for Export section.
145 STATIC UINT32 mExportSize
;
146 STATIC UINT32 mExportRVA
[PRM_MODULE_EXPORT_SYMBOL_NUM
];
147 STATIC UINT32 mExportSymNum
;
148 STATIC CHAR8 mExportSymName
[PRM_MODULE_EXPORT_SYMBOL_NUM
][PRM_HANDLER_NAME_MAXIMUM_LENGTH
];
151 // Initialization Function
156 ELF_FUNCTION_TABLE
*ElfFunctions
160 // Initialize data pointer and structures.
162 VerboseMsg ("Set EHDR");
163 mEhdr
= (Elf_Ehdr
*) FileBuffer
;
166 // Check the ELF64 specific header information.
168 VerboseMsg ("Check ELF64 Header Information");
169 if (mEhdr
->e_ident
[EI_CLASS
] != ELFCLASS64
) {
170 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS64");
173 if (mEhdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
174 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
177 if ((mEhdr
->e_type
!= ET_EXEC
) && (mEhdr
->e_type
!= ET_DYN
)) {
178 Error (NULL
, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
181 if (!((mEhdr
->e_machine
== EM_X86_64
) || (mEhdr
->e_machine
== EM_AARCH64
) || (mEhdr
->e_machine
== EM_RISCV64
) || (mEhdr
->e_machine
== EM_LOONGARCH
))) {
182 Warning (NULL
, 0, 3000, "Unsupported", "ELF e_machine is not Elf64 machine.");
184 if (mEhdr
->e_version
!= EV_CURRENT
) {
185 Error (NULL
, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr
->e_version
, EV_CURRENT
);
190 if ((mEhdr
->e_machine
!= EM_X86_64
) && (mEhdr
->e_machine
!= EM_AARCH64
)) {
191 Error (NULL
, 0, 3000, "Unsupported", "--prm option currently only supports X64 and AArch64 archs.");
197 // Update section header pointers
199 VerboseMsg ("Update Header Pointers");
200 mShdrBase
= (Elf_Shdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_shoff
);
201 mPhdrBase
= (Elf_Phdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_phoff
);
204 // Create COFF Section offset buffer and zero.
206 VerboseMsg ("Create COFF Section Offset Buffer");
207 mCoffSectionsOffset
= (UINT32
*)malloc(mEhdr
->e_shnum
* sizeof (UINT32
));
208 if (mCoffSectionsOffset
== NULL
) {
209 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
212 memset(mCoffSectionsOffset
, 0, mEhdr
->e_shnum
* sizeof(UINT32
));
215 // Fill in function pointers.
217 VerboseMsg ("Fill in Function Pointers");
218 ElfFunctions
->ScanSections
= ScanSections64
;
219 ElfFunctions
->WriteSections
= WriteSections64
;
220 ElfFunctions
->WriteRelocations
= WriteRelocations64
;
221 ElfFunctions
->WriteDebug
= WriteDebug64
;
222 ElfFunctions
->SetImageSize
= SetImageSize64
;
223 ElfFunctions
->CleanUp
= CleanUp64
;
227 ElfFunctions
->WriteExport
= WriteExport64
;
235 // Header by Index functions
243 if (Num
>= mEhdr
->e_shnum
) {
244 Error (NULL
, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num
);
248 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
257 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
266 return (Offset
+ 3) & ~3;
278 return (BOOLEAN
) (((Shdr
->sh_flags
& (SHF_EXECINSTR
| SHF_ALLOC
)) == (SHF_EXECINSTR
| SHF_ALLOC
)) ||
279 ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
));
288 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
290 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
299 Elf_Shdr
*Namehdr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
301 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namehdr
->sh_offset
+ Shdr
->sh_name
, ELF_SYMBOL_SECTION_NAME
) == 0);
310 if (IsHiiRsrcShdr(Shdr
)) {
313 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_EXECINSTR
| SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
322 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
324 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_STRTAB_SECTION_NAME
) == 0);
334 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
335 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
336 if (IsStrtabShdr(shdr
)) {
349 Elf_Shdr
*StrtabShdr
;
350 UINT8
*StrtabContents
;
354 if (Sym
->st_name
== 0) {
358 StrtabShdr
= FindStrtabShdr();
359 if (StrtabShdr
== NULL
) {
363 assert(Sym
->st_name
< StrtabShdr
->sh_size
);
365 StrtabContents
= (UINT8
*)mEhdr
+ StrtabShdr
->sh_offset
;
368 for (i
= Sym
->st_name
; (i
< StrtabShdr
->sh_size
) && !foundEnd
; i
++) {
369 foundEnd
= (BOOLEAN
)(StrtabContents
[i
] == 0);
373 return StrtabContents
+ Sym
->st_name
;
377 // Get Prm Handler number and name
385 PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER
*PrmExport
;
386 PRM_HANDLER_EXPORT_DESCRIPTOR_STRUCT
*PrmHandler
;
389 PrmExport
= (PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER
*)((UINT8
*)mEhdr
+ Offset
);
390 PrmHandler
= (PRM_HANDLER_EXPORT_DESCRIPTOR_STRUCT
*)(PrmExport
+ 1);
392 for (HandlerNum
= 0; HandlerNum
< PrmExport
->NumberPrmHandlers
; HandlerNum
++) {
393 strcpy(mExportSymName
[mExportSymNum
], PrmHandler
->PrmHandlerName
);
398 // Check if PRM handler number is larger than (PRM_MODULE_EXPORT_SYMBOL_NUM - 1)
400 if (mExportSymNum
>= (PRM_MODULE_EXPORT_SYMBOL_NUM
- 1)) {
401 Error (NULL
, 0, 3000, "Invalid", "FindPrmHandler: Number %u is too high.", mExportSymNum
);
408 // Find the ELF section hosting the GOT from an ELF Rva
409 // of a single GOT entry. Normally, GOT is placed in
410 // ELF .text section, so assume once we find in which
411 // section the GOT is, all GOT entries are there, and
416 FindElfGOTSectionFromGOTEntryElfRva (
417 Elf64_Addr GOTEntryElfRva
421 if (mGOTShdr
!= NULL
) {
422 if (GOTEntryElfRva
>= mGOTShdr
->sh_addr
&&
423 GOTEntryElfRva
< mGOTShdr
->sh_addr
+ mGOTShdr
->sh_size
) {
426 Error (NULL
, 0, 3000, "Unsupported", "FindElfGOTSectionFromGOTEntryElfRva: GOT entries found in multiple sections.");
429 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
430 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
431 if (GOTEntryElfRva
>= shdr
->sh_addr
&&
432 GOTEntryElfRva
< shdr
->sh_addr
+ shdr
->sh_size
) {
438 Error (NULL
, 0, 3000, "Invalid", "FindElfGOTSectionFromGOTEntryElfRva: ElfRva 0x%016LX for GOT entry not found in any section.", GOTEntryElfRva
);
443 // Stores locations of GOT entries in COFF image.
444 // Returns TRUE if GOT entry is new.
445 // Simple implementation as number of GOT
446 // entries is expected to be low.
451 AccumulateCoffGOTEntries (
456 if (mGOTCoffEntries
!= NULL
) {
457 for (i
= 0; i
< mGOTNumCoffEntries
; i
++) {
458 if (mGOTCoffEntries
[i
] == GOTCoffEntry
) {
463 if (mGOTCoffEntries
== NULL
) {
464 mGOTCoffEntries
= (UINT32
*)malloc(5 * sizeof *mGOTCoffEntries
);
465 if (mGOTCoffEntries
== NULL
) {
466 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
468 assert (mGOTCoffEntries
!= NULL
);
469 mGOTMaxCoffEntries
= 5;
470 mGOTNumCoffEntries
= 0;
471 } else if (mGOTNumCoffEntries
== mGOTMaxCoffEntries
) {
472 mGOTCoffEntries
= (UINT32
*)realloc(mGOTCoffEntries
, 2 * mGOTMaxCoffEntries
* sizeof *mGOTCoffEntries
);
473 if (mGOTCoffEntries
== NULL
) {
474 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
476 assert (mGOTCoffEntries
!= NULL
);
477 mGOTMaxCoffEntries
+= mGOTMaxCoffEntries
;
479 mGOTCoffEntries
[mGOTNumCoffEntries
++] = GOTCoffEntry
;
484 // 32-bit Unsigned integer comparator for qsort.
493 if (*(const UINT32
*)lhs
< *(const UINT32
*)rhs
) {
496 return *(const UINT32
*)lhs
> *(const UINT32
*)rhs
;
500 // Emit accumulated Coff GOT entry relocations into
501 // Coff image. This function performs its job
502 // once and then releases the entry list, so
503 // it can safely be called multiple times.
512 if (mGOTCoffEntries
== NULL
) {
516 // Emit Coff relocations with Rvas ordered.
521 sizeof *mGOTCoffEntries
,
523 for (i
= 0; i
< mGOTNumCoffEntries
; i
++) {
524 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X", mGOTCoffEntries
[i
]);
527 EFI_IMAGE_REL_BASED_DIR64
);
529 free(mGOTCoffEntries
);
530 mGOTCoffEntries
= NULL
;
531 mGOTMaxCoffEntries
= 0;
532 mGOTNumCoffEntries
= 0;
535 // RISC-V 64 specific Elf WriteSection function.
539 WriteSectionRiscV64 (
548 Elf64_Addr GOTEntryRva
;
550 switch (ELF_R_TYPE(Rel
->r_info
)) {
555 *(UINT64
*)Targ
= Sym
->st_value
+ Rel
->r_addend
;
559 *(UINT64
*)Targ
= Sym
->st_value
+ Rel
->r_addend
;
563 mRiscVPass1Targ
= Targ
;
564 mRiscVPass1Sym
= SymShdr
;
565 mRiscVPass1SymSecIndex
= Sym
->st_shndx
;
569 if (mRiscVPass1Sym
== SymShdr
&& mRiscVPass1Targ
!= NULL
&& mRiscVPass1SymSecIndex
== Sym
->st_shndx
&& mRiscVPass1SymSecIndex
!= 0) {
570 Value
= (UINT32
)(RV_X(*(UINT32
*)mRiscVPass1Targ
, 12, 20) << 12);
571 Value2
= (UINT32
)(RV_X(*(UINT32
*)Targ
, 20, 12));
572 if (Value2
& (RISCV_IMM_REACH
/2)) {
573 Value2
|= ~(RISCV_IMM_REACH
-1);
576 Value
= Value
- (UINT32
)SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
577 Value2
= RISCV_CONST_HIGH_PART (Value
);
578 *(UINT32
*)mRiscVPass1Targ
= (RV_X (Value2
, 12, 20) << 12) | \
579 (RV_X (*(UINT32
*)mRiscVPass1Targ
, 0, 12));
580 *(UINT32
*)Targ
= (RV_X (Value
, 0, 12) << 20) | \
581 (RV_X (*(UINT32
*)Targ
, 0, 20));
583 mRiscVPass1Sym
= NULL
;
584 mRiscVPass1Targ
= NULL
;
585 mRiscVPass1SymSecIndex
= 0;
589 if (mRiscVPass1Sym
== SymShdr
&& mRiscVPass1Targ
!= NULL
&& mRiscVPass1SymSecIndex
== Sym
->st_shndx
&& mRiscVPass1SymSecIndex
!= 0) {
590 Value
= (UINT32
)(RV_X(*(UINT32
*)mRiscVPass1Targ
, 12, 20) << 12);
591 Value2
= (UINT32
)(RV_X(*(UINT32
*)Targ
, 7, 5) | (RV_X(*(UINT32
*)Targ
, 25, 7) << 5));
592 if (Value2
& (RISCV_IMM_REACH
/2)) {
593 Value2
|= ~(RISCV_IMM_REACH
-1);
596 Value
= Value
- (UINT32
)SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
597 Value2
= RISCV_CONST_HIGH_PART (Value
);
598 *(UINT32
*)mRiscVPass1Targ
= (RV_X (Value2
, 12, 20) << 12) | \
599 (RV_X (*(UINT32
*)mRiscVPass1Targ
, 0, 12));
600 Value2
= *(UINT32
*)Targ
& 0x01fff07f;
601 Value
&= RISCV_IMM_REACH
- 1;
602 *(UINT32
*)Targ
= Value2
| (UINT32
)(((RV_X(Value
, 0, 5) << 7) | (RV_X(Value
, 5, 7) << 25)));
604 mRiscVPass1Sym
= NULL
;
605 mRiscVPass1Targ
= NULL
;
606 mRiscVPass1SymSecIndex
= 0;
609 case R_RISCV_GOT_HI20
:
610 GOTEntryRva
= (Sym
->st_value
- Rel
->r_offset
);
611 mRiscVPass1Offset
= RV_X(GOTEntryRva
, 0, 12);
612 Value
= (UINT32
)RV_X(GOTEntryRva
, 12, 20);
613 *(UINT32
*)Targ
= (Value
<< 12) | (RV_X(*(UINT32
*)Targ
, 0, 12));
615 mRiscVPass1Targ
= Targ
;
616 mRiscVPass1Sym
= SymShdr
;
617 mRiscVPass1SymSecIndex
= Sym
->st_shndx
;
618 mRiscVPass1GotFixup
= 1;
621 case R_RISCV_PCREL_HI20
:
622 mRiscVPass1Targ
= Targ
;
623 mRiscVPass1Sym
= SymShdr
;
624 mRiscVPass1SymSecIndex
= Sym
->st_shndx
;
626 Value
= (UINT32
)(RV_X(*(UINT32
*)mRiscVPass1Targ
, 12, 20));
629 case R_RISCV_PCREL_LO12_S
:
630 if (mRiscVPass1Targ
!= NULL
&& mRiscVPass1Sym
!= NULL
&& mRiscVPass1SymSecIndex
!= 0) {
632 Value2
= (UINT32
)(RV_X(*(UINT32
*)mRiscVPass1Targ
, 12, 20));
634 Value
= ((UINT32
)(RV_X(*(UINT32
*)Targ
, 25, 7)) << 5);
635 Value
= (Value
| (UINT32
)(RV_X(*(UINT32
*)Targ
, 7, 5)));
637 if(Value
& (RISCV_IMM_REACH
/2)) {
638 Value
|= ~(RISCV_IMM_REACH
-1);
640 Value
= Value
- (UINT32
)mRiscVPass1Sym
->sh_addr
+ mCoffSectionsOffset
[mRiscVPass1SymSecIndex
];
642 if(-2048 > (INT32
)Value
) {
643 i
= (((INT32
)Value
* -1) / 4096);
646 if(-2048 > (INT32
)Value
) {
651 else if( 2047 < (INT32
)Value
) {
655 if(2047 < (INT32
)Value
) {
661 // Update the IMM of SD instruction
663 // |31 25|24 20|19 15|14 12 |11 7|6 0|
664 // |-------------------------------------------|-------|
665 // |imm[11:5] | rs2 | rs1 | funct3 |imm[4:0] | opcode|
666 // ---------------------------------------------------
668 // First Zero out current IMM
669 *(UINT32
*)Targ
&= ~0xfe000f80;
671 // Update with new IMM
672 *(UINT32
*)Targ
|= (RV_X(Value
, 5, 7) << 25);
673 *(UINT32
*)Targ
|= (RV_X(Value
, 0, 5) << 7);
675 // Update previous instruction
676 *(UINT32
*)mRiscVPass1Targ
= (RV_X(Value2
, 0, 20)<<12) | (RV_X(*(UINT32
*)mRiscVPass1Targ
, 0, 12));
678 mRiscVPass1Sym
= NULL
;
679 mRiscVPass1Targ
= NULL
;
680 mRiscVPass1SymSecIndex
= 0;
683 case R_RISCV_PCREL_LO12_I
:
684 if (mRiscVPass1Targ
!= NULL
&& mRiscVPass1Sym
!= NULL
&& mRiscVPass1SymSecIndex
!= 0) {
686 Value2
= (UINT32
)(RV_X(*(UINT32
*)mRiscVPass1Targ
, 12, 20));
688 if(mRiscVPass1GotFixup
) {
689 Value
= (UINT32
)(mRiscVPass1Offset
);
691 Value
= (UINT32
)(RV_X(*(UINT32
*)Targ
, 20, 12));
692 if(Value
& (RISCV_IMM_REACH
/2)) {
693 Value
|= ~(RISCV_IMM_REACH
-1);
696 Value
= Value
- (UINT32
)mRiscVPass1Sym
->sh_addr
+ mCoffSectionsOffset
[mRiscVPass1SymSecIndex
];
698 if(-2048 > (INT32
)Value
) {
699 i
= (((INT32
)Value
* -1) / 4096);
702 if(-2048 > (INT32
)Value
) {
707 else if( 2047 < (INT32
)Value
) {
711 if(2047 < (INT32
)Value
) {
717 if(mRiscVPass1GotFixup
) {
718 *(UINT32
*)Targ
= (RV_X((UINT32
)Value
, 0, 12) << 20)
719 | (RV_X(*(UINT32
*)Targ
, 0, 20));
720 // Convert LD instruction to ADDI
722 // |31 20|19 15|14 12|11 7|6 0|
723 // |-----------------------------------------|
724 // |imm[11:0] | rs1 | 011 | rd | 0000011 | LD
725 // -----------------------------------------
727 // |-----------------------------------------|
728 // |imm[11:0] | rs1 | 000 | rd | 0010011 | ADDI
729 // -----------------------------------------
731 // To convert, let's first reset bits 12-14 and 0-6 using ~0x707f
732 // Then modify the opcode to ADDI (0010011)
733 // All other fields will remain same.
735 *(UINT32
*)Targ
= ((*(UINT32
*)Targ
& ~0x707f) | 0x13);
737 *(UINT32
*)Targ
= (RV_X(Value
, 0, 12) << 20) | (RV_X(*(UINT32
*)Targ
, 0, 20));
739 *(UINT32
*)mRiscVPass1Targ
= (RV_X(Value2
, 0, 20)<<12) | (RV_X(*(UINT32
*)mRiscVPass1Targ
, 0, 12));
741 mRiscVPass1Sym
= NULL
;
742 mRiscVPass1Targ
= NULL
;
743 mRiscVPass1SymSecIndex
= 0;
744 mRiscVPass1Offset
= 0;
745 mRiscVPass1GotFixup
= 0;
754 case R_RISCV_GPREL_I
:
755 case R_RISCV_GPREL_S
:
757 case R_RISCV_CALL_PLT
:
758 case R_RISCV_RVC_BRANCH
:
759 case R_RISCV_RVC_JUMP
:
769 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_RISCV64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
774 // Elf functions interface implementation
784 EFI_IMAGE_DOS_HEADER
*DosHdr
;
785 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
788 BOOLEAN FoundSection
;
795 // Coff file start with a DOS header.
797 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
798 mNtHdrOffset
= mCoffOffset
;
799 switch (mEhdr
->e_machine
) {
804 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
807 VerboseMsg ("%s unknown e_machine type %hu. Assume X64", mInImageName
, mEhdr
->e_machine
);
808 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
812 mTableOffset
= mCoffOffset
;
813 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
816 // Set mCoffAlignment to the maximum alignment of the input sections
819 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
820 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
821 if (shdr
->sh_addralign
<= mCoffAlignment
) {
824 if (IsTextShdr(shdr
) || IsDataShdr(shdr
) || IsHiiRsrcShdr(shdr
)) {
825 mCoffAlignment
= (UINT32
)shdr
->sh_addralign
;
830 // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT
832 if (mCoffAlignment
> MAX_COFF_ALIGNMENT
) {
833 Error (NULL
, 0, 3000, "Invalid", "Section alignment is larger than MAX_COFF_ALIGNMENT.");
839 // Move the PE/COFF header right before the first section. This will help us
840 // save space when converting to TE.
842 if (mCoffAlignment
> mCoffOffset
) {
843 mNtHdrOffset
+= mCoffAlignment
- mCoffOffset
;
844 mTableOffset
+= mCoffAlignment
- mCoffOffset
;
845 mCoffOffset
= mCoffAlignment
;
849 // First text sections.
851 mCoffOffset
= CoffAlign(mCoffOffset
);
852 mTextOffset
= mCoffOffset
;
853 FoundSection
= FALSE
;
855 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
856 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
857 if (IsTextShdr(shdr
)) {
858 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
859 // the alignment field is valid
860 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
861 // if the section address is aligned we must align PE/COFF
862 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
864 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
868 /* Relocate entry. */
869 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
870 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
871 CoffEntry
= (UINT32
) (mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
);
875 // Set mTextOffset with the offset of the first '.text' section
878 mTextOffset
= mCoffOffset
;
882 mCoffSectionsOffset
[i
] = mCoffOffset
;
883 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
888 if (!FoundSection
&& mOutImageType
!= FW_ACPI_IMAGE
) {
889 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
893 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
894 mCoffOffset
= CoffAlign(mCoffOffset
);
896 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
897 Warning (NULL
, 0, 0, NULL
, "Multiple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
901 // Then data sections.
903 mDataOffset
= mCoffOffset
;
904 FoundSection
= FALSE
;
906 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
907 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
908 if (IsDataShdr(shdr
)) {
909 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
910 // the alignment field is valid
911 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
912 // if the section address is aligned we must align PE/COFF
913 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
915 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
920 // Set mDataOffset with the offset of the first '.data' section
923 mDataOffset
= mCoffOffset
;
926 mCoffSectionsOffset
[i
] = mCoffOffset
;
927 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
933 // Make room for .debug data in .data (or .text if .data is empty) instead of
934 // putting it in a section of its own. This is explicitly allowed by the
935 // PE/COFF spec, and prevents bloat in the binary when using large values for
936 // section alignment.
938 if (SectionCount
> 0) {
939 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
941 mCoffOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
) +
942 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) +
943 strlen(mInImageName
) + 1;
945 mCoffOffset
= CoffAlign(mCoffOffset
);
946 if (SectionCount
== 0) {
947 mDataOffset
= mCoffOffset
;
950 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
951 Warning (NULL
, 0, 0, NULL
, "Multiple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
955 // The Symbol sections.
961 const UINT8
*SymName
;
963 mExportOffset
= mCoffOffset
;
964 mExportSize
= sizeof(EFI_IMAGE_EXPORT_DIRECTORY
) + strlen(mInImageName
) + 1;
966 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
969 // Determine if this is a symbol section.
971 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
972 if (!IsSymbolShdr(shdr
)) {
976 UINT8
*Symtab
= (UINT8
*)mEhdr
+ shdr
->sh_offset
;
977 SymNum
= (shdr
->sh_size
) / (shdr
->sh_entsize
);
980 // First Get PrmModuleExportDescriptor
982 for (SymIndex
= 0; SymIndex
< SymNum
; SymIndex
++) {
983 Sym
= (Elf_Sym
*)(Symtab
+ SymIndex
* shdr
->sh_entsize
);
984 SymName
= GetSymName(Sym
);
985 if (SymName
== NULL
) {
989 if (strcmp((CHAR8
*)SymName
, PRM_MODULE_EXPORT_DESCRIPTOR_NAME
) == 0) {
991 // Find PrmHandler Number and Name
993 FindPrmHandler(Sym
->st_value
);
995 strcpy(mExportSymName
[mExportSymNum
], (CHAR8
*)SymName
);
996 mExportRVA
[mExportSymNum
] = (UINT32
)(Sym
->st_value
);
997 mExportSize
+= 2 * EFI_IMAGE_EXPORT_ADDR_SIZE
+ EFI_IMAGE_EXPORT_ORDINAL_SIZE
+ strlen((CHAR8
*)SymName
) + 1;
1004 // Second Get PrmHandler
1006 for (SymIndex
= 0; SymIndex
< SymNum
; SymIndex
++) {
1008 Sym
= (Elf_Sym
*)(Symtab
+ SymIndex
* shdr
->sh_entsize
);
1009 SymName
= GetSymName(Sym
);
1010 if (SymName
== NULL
) {
1014 for (ExpIndex
= 0; ExpIndex
< (mExportSymNum
-1); ExpIndex
++) {
1015 if (strcmp((CHAR8
*)SymName
, mExportSymName
[ExpIndex
]) != 0) {
1018 mExportRVA
[ExpIndex
] = (UINT32
)(Sym
->st_value
);
1019 mExportSize
+= 2 * EFI_IMAGE_EXPORT_ADDR_SIZE
+ EFI_IMAGE_EXPORT_ORDINAL_SIZE
+ strlen((CHAR8
*)SymName
) + 1;
1026 mCoffOffset
+= mExportSize
;
1027 mCoffOffset
= CoffAlign(mCoffOffset
);
1031 // The HII resource sections.
1033 mHiiRsrcOffset
= mCoffOffset
;
1034 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
1035 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
1036 if (IsHiiRsrcShdr(shdr
)) {
1037 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
1038 // the alignment field is valid
1039 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
1040 // if the section address is aligned we must align PE/COFF
1041 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
1043 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
1046 if (shdr
->sh_size
!= 0) {
1047 mHiiRsrcOffset
= mCoffOffset
;
1048 mCoffSectionsOffset
[i
] = mCoffOffset
;
1049 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
1050 mCoffOffset
= CoffAlign(mCoffOffset
);
1051 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
1057 mRelocOffset
= mCoffOffset
;
1060 // Allocate base Coff file. Will be expanded later for relocations.
1062 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
1063 if (mCoffFile
== NULL
) {
1064 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1066 assert (mCoffFile
!= NULL
);
1067 memset(mCoffFile
, 0, mCoffOffset
);
1072 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
1073 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
1074 DosHdr
->e_lfanew
= mNtHdrOffset
;
1076 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1078 NtHdr
->Pe32Plus
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
1080 switch (mEhdr
->e_machine
) {
1082 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
1083 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
1086 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_AARCH64
;
1087 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
1090 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_RISCV64
;
1091 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
1094 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_LOONGARCH64
;
1095 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
1099 VerboseMsg ("%u unknown e_machine type. Assume X64", (UINTN
)mEhdr
->e_machine
);
1100 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
1101 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
1104 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
1105 NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
1106 mImageTimeStamp
= NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
;
1107 NtHdr
->Pe32Plus
.FileHeader
.PointerToSymbolTable
= 0;
1108 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSymbols
= 0;
1109 NtHdr
->Pe32Plus
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32Plus
.OptionalHeader
);
1110 NtHdr
->Pe32Plus
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
1111 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
1112 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
1113 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE
;
1115 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
1116 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
1117 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfUninitializedData
= 0;
1118 NtHdr
->Pe32Plus
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
1120 NtHdr
->Pe32Plus
.OptionalHeader
.BaseOfCode
= mTextOffset
;
1122 NtHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= 0;
1123 NtHdr
->Pe32Plus
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
1124 NtHdr
->Pe32Plus
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
1125 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= 0;
1127 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
1128 NtHdr
->Pe32Plus
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
1133 if ((mDataOffset
- mTextOffset
) > 0) {
1134 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
1135 EFI_IMAGE_SCN_CNT_CODE
1136 | EFI_IMAGE_SCN_MEM_EXECUTE
1137 | EFI_IMAGE_SCN_MEM_READ
);
1139 // Don't make a section of size 0.
1140 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
1144 // If found symbol, add edata section between data and rsrc section
1147 Offset
= mExportOffset
;
1149 Offset
= mHiiRsrcOffset
;
1152 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
1153 CreateSectionHeader (".data", mDataOffset
, Offset
- mDataOffset
,
1154 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1155 | EFI_IMAGE_SCN_MEM_WRITE
1156 | EFI_IMAGE_SCN_MEM_READ
);
1158 // Don't make a section of size 0.
1159 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
1163 if ((mHiiRsrcOffset
- mExportOffset
) > 0) {
1164 CreateSectionHeader (".edata", mExportOffset
, mHiiRsrcOffset
- mExportOffset
,
1165 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1166 | EFI_IMAGE_SCN_MEM_READ
);
1167 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT
].Size
= mHiiRsrcOffset
- mExportOffset
;
1168 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT
].VirtualAddress
= mExportOffset
;
1171 // Don't make a section of size 0.
1172 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
1176 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
1177 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
1178 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1179 | EFI_IMAGE_SCN_MEM_READ
);
1181 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
1182 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
1184 // Don't make a section of size 0.
1185 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
1193 SECTION_FILTER_TYPES FilterType
1199 BOOLEAN (*Filter
)(Elf_Shdr
*);
1200 Elf64_Addr GOTEntryRva
;
1203 // Initialize filter pointer
1205 switch (FilterType
) {
1207 Filter
= IsTextShdr
;
1210 Filter
= IsHiiRsrcShdr
;
1213 Filter
= IsDataShdr
;
1220 // First: copy sections.
1222 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
1223 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
1224 if ((*Filter
)(Shdr
)) {
1225 switch (Shdr
->sh_type
) {
1228 if (Shdr
->sh_offset
+ Shdr
->sh_size
> mFileBufferSize
) {
1231 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
1232 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
1233 (size_t) Shdr
->sh_size
);
1237 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, (size_t) Shdr
->sh_size
);
1242 // Ignore for unknown section type.
1244 VerboseMsg ("%s unknown section type %x. We ignore this unknown section type.", mInImageName
, (unsigned)Shdr
->sh_type
);
1251 // Second: apply relocations.
1253 VerboseMsg ("Applying Relocations...");
1254 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
1256 // Determine if this is a relocation section.
1258 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
1259 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
1264 // If this is a ET_DYN (PIE) executable, we will encounter a dynamic SHT_RELA
1265 // section that applies to the entire binary, and which will have its section
1266 // index set to #0 (which is a NULL section with the SHF_ALLOC bit cleared).
1268 // In the absence of GOT based relocations,
1269 // this RELA section will contain redundant R_xxx_RELATIVE relocations, one
1270 // for every R_xxx_xx64 relocation appearing in the per-section RELA sections.
1271 // (i.e., .rela.text and .rela.data)
1273 if (RelShdr
->sh_info
== 0) {
1278 // Relocation section found. Now extract section information that the relocations
1279 // apply to in the ELF data and the new COFF data.
1281 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
1282 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
1285 // Only process relocations for the current filter type.
1287 if (RelShdr
->sh_type
== SHT_RELA
&& (*Filter
)(SecShdr
)) {
1291 // Determine the symbol table referenced by the relocation data.
1293 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
1294 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
1297 // Process all relocation entries for this section.
1299 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= (UINT32
) RelShdr
->sh_entsize
) {
1302 // Set pointer to relocation entry
1304 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
1307 // Set pointer to symbol table entry associated with the relocation entry.
1309 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
1315 // The _GLOBAL_OFFSET_TABLE_ symbol is not actually an absolute symbol,
1316 // but carries the SHN_ABS section index for historical reasons.
1317 // It must be accompanied by a R_*_GOT_* type relocation on a
1318 // subsequent instruction, which we handle below, specifically to avoid
1319 // the GOT indirection, and to refer to the symbol directly. This means
1320 // we can simply disregard direct references to the GOT symbol itself,
1321 // as the resulting value will never be used.
1323 if (Sym
->st_shndx
== SHN_ABS
) {
1324 const UINT8
*SymName
= GetSymName (Sym
);
1325 if (strcmp ((CHAR8
*)SymName
, "_GLOBAL_OFFSET_TABLE_") == 0) {
1331 // Check section header index found in symbol table and get the section
1334 if (Sym
->st_shndx
== SHN_UNDEF
1335 || Sym
->st_shndx
>= mEhdr
->e_shnum
) {
1336 const UINT8
*SymName
= GetSymName(Sym
);
1337 if (SymName
== NULL
) {
1338 SymName
= (const UINT8
*)"<unknown>";
1342 // Skip error on EM_RISCV64 and EM_LOONGARCH because no symbol name is built
1343 // from RISC-V and LoongArch toolchain.
1345 if ((mEhdr
->e_machine
!= EM_RISCV64
) && (mEhdr
->e_machine
!= EM_LOONGARCH
)) {
1346 Error (NULL
, 0, 3000, "Invalid",
1347 "%s: Bad definition for symbol '%s'@%#llx or unsupported symbol type. "
1348 "For example, absolute and undefined symbols are not supported.",
1349 mInImageName
, SymName
, Sym
->st_value
);
1355 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
1358 // Convert the relocation data to a pointer into the coff file.
1361 // r_offset is the virtual address of the storage unit to be relocated.
1362 // sh_addr is the virtual address for the base of the section.
1364 // r_offset in a memory address.
1365 // Convert it to a pointer in the coff file.
1367 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
1370 // Determine how to handle each relocation type based on the machine type.
1372 if (mEhdr
->e_machine
== EM_X86_64
) {
1373 switch (ELF_R_TYPE(Rel
->r_info
)) {
1378 // Absolute relocation.
1380 VerboseMsg ("R_X86_64_64");
1381 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1382 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1384 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1385 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
1388 VerboseMsg ("R_X86_64_32");
1389 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1390 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1392 *(UINT32
*)Targ
= (UINT32
)((UINT64
)(*(UINT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
1393 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1396 VerboseMsg ("R_X86_64_32S");
1397 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1398 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1400 *(INT32
*)Targ
= (INT32
)((INT64
)(*(INT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
1401 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1404 case R_X86_64_PLT32
:
1406 // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is
1407 // possible since we know all code symbol references resolve to
1408 // definitions in the same module (UEFI has no shared libraries),
1409 // and so there is never a reason to jump via a PLT entry,
1410 // allowing us to resolve the reference using the symbol directly.
1412 VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");
1416 // Relative relocation: Symbol - Ip + Addend
1418 VerboseMsg ("R_X86_64_PC32");
1419 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1420 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1422 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
1423 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
1424 - (SecOffset
- SecShdr
->sh_addr
));
1425 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1427 case R_X86_64_GOTPCREL
:
1428 case R_X86_64_GOTPCRELX
:
1429 case R_X86_64_REX_GOTPCRELX
:
1430 VerboseMsg ("R_X86_64_GOTPCREL family");
1431 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1432 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1434 GOTEntryRva
= Rel
->r_offset
- Rel
->r_addend
+ *(INT32
*)Targ
;
1435 FindElfGOTSectionFromGOTEntryElfRva(GOTEntryRva
);
1436 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
1437 + (mCoffSectionsOffset
[mGOTShindex
] - mGOTShdr
->sh_addr
)
1438 - (SecOffset
- SecShdr
->sh_addr
));
1439 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1440 GOTEntryRva
+= (mCoffSectionsOffset
[mGOTShindex
] - mGOTShdr
->sh_addr
); // ELF Rva -> COFF Rva
1441 if (AccumulateCoffGOTEntries((UINT32
)GOTEntryRva
)) {
1443 // Relocate GOT entry if it's the first time we run into it
1445 Targ
= mCoffFile
+ GOTEntryRva
;
1447 // Limitation: The following three statements assume memory
1448 // at *Targ is valid because the section containing the GOT
1449 // has already been copied from the ELF image to the Coff image.
1450 // This pre-condition presently holds because the GOT is placed
1451 // in section .text, and the ELF text sections are all copied
1452 // prior to reaching this point.
1453 // If the pre-condition is violated in the future, this fixup
1454 // either needs to be deferred after the GOT section is copied
1455 // to the Coff image, or the fixup should be performed on the
1456 // source Elf image instead of the destination Coff image.
1458 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1459 (UINT32
)GOTEntryRva
,
1461 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1462 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
1466 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1468 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
1470 switch (ELF_R_TYPE(Rel
->r_info
)) {
1473 case R_AARCH64_LD64_GOTOFF_LO15
:
1474 case R_AARCH64_LD64_GOTPAGE_LO15
:
1476 // Convert into an ADR instruction that refers to the symbol directly.
1478 Offset
= Sym
->st_value
- Rel
->r_offset
;
1480 *(UINT32
*)Targ
&= 0x1000001f;
1481 *(UINT32
*)Targ
|= ((Offset
& 0x1ffffc) << (5 - 2)) | ((Offset
& 0x3) << 29);
1483 if (Offset
< -0x100000 || Offset
> 0xfffff) {
1484 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s failed to relax GOT based symbol reference - image is too big (>1 MiB).",
1490 case R_AARCH64_LD64_GOT_LO12_NC
:
1492 // Convert into an ADD instruction - see R_AARCH64_ADR_GOT_PAGE below.
1494 *(UINT32
*)Targ
&= 0x3ff;
1495 *(UINT32
*)Targ
|= 0x91000000 | ((Sym
->st_value
& 0xfff) << 10);
1498 case R_AARCH64_ADR_GOT_PAGE
:
1500 // This relocation points to the GOT entry that contains the absolute
1501 // address of the symbol we are referring to. Since EDK2 only uses
1502 // fully linked binaries, we can avoid the indirection, and simply
1503 // refer to the symbol directly. This implies having to patch the
1504 // subsequent LDR instruction (covered by a R_AARCH64_LD64_GOT_LO12_NC
1505 // relocation) into an ADD instruction - this is handled above.
1507 Offset
= (Sym
->st_value
- (Rel
->r_offset
& ~0xfff)) >> 12;
1509 *(UINT32
*)Targ
&= 0x9000001f;
1510 *(UINT32
*)Targ
|= ((Offset
& 0x1ffffc) << (5 - 2)) | ((Offset
& 0x3) << 29);
1514 case R_AARCH64_ADR_PREL_PG_HI21
:
1516 // In order to handle Cortex-A53 erratum #843419, the LD linker may
1517 // convert ADRP instructions into ADR instructions, but without
1518 // updating the static relocation type, and so we may end up here
1519 // while the instruction in question is actually ADR. So let's
1520 // just disregard it: the section offset check we apply below to
1521 // ADR instructions will trigger for its R_AARCH64_xxx_ABS_LO12_NC
1522 // companion instruction as well, so it is safe to omit it here.
1524 if ((*(UINT32
*)Targ
& BIT31
) == 0) {
1529 // AArch64 PG_H21 relocations are typically paired with ABS_LO12
1530 // relocations, where a PC-relative reference with +/- 4 GB range is
1531 // split into a relative high part and an absolute low part. Since
1532 // the absolute low part represents the offset into a 4 KB page, we
1533 // either have to convert the ADRP into an ADR instruction, or we
1534 // need to use a section alignment of at least 4 KB, so that the
1535 // binary appears at a correct offset at runtime. In any case, we
1536 // have to make sure that the 4 KB relative offsets of both the
1537 // section containing the reference as well as the section to which
1538 // it refers have not been changed during PE/COFF conversion (i.e.,
1539 // in ScanSections64() above).
1541 if (mCoffAlignment
< 0x1000) {
1543 // Attempt to convert the ADRP into an ADR instruction.
1544 // This is only possible if the symbol is within +/- 1 MB.
1547 // Decode the ADRP instruction
1548 Offset
= (INT32
)((*(UINT32
*)Targ
& 0xffffe0) << 8);
1549 Offset
= (Offset
<< (6 - 5)) | ((*(UINT32
*)Targ
& 0x60000000) >> (29 - 12));
1552 // ADRP offset is relative to the previous page boundary,
1553 // whereas ADR offset is relative to the instruction itself.
1554 // So fix up the offset so it points to the page containing
1557 Offset
-= (UINTN
)(Targ
- mCoffFile
) & 0xfff;
1559 if (Offset
< -0x100000 || Offset
> 0xfffff) {
1560 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s due to its size (> 1 MB), this module requires 4 KB section alignment.",
1565 // Re-encode the offset as an ADR instruction
1566 *(UINT32
*)Targ
&= 0x1000001f;
1567 *(UINT32
*)Targ
|= ((Offset
& 0x1ffffc) << (5 - 2)) | ((Offset
& 0x3) << 29);
1571 case R_AARCH64_ADD_ABS_LO12_NC
:
1572 case R_AARCH64_LDST8_ABS_LO12_NC
:
1573 case R_AARCH64_LDST16_ABS_LO12_NC
:
1574 case R_AARCH64_LDST32_ABS_LO12_NC
:
1575 case R_AARCH64_LDST64_ABS_LO12_NC
:
1576 case R_AARCH64_LDST128_ABS_LO12_NC
:
1577 if (((SecShdr
->sh_addr
^ SecOffset
) & 0xfff) != 0 ||
1578 ((SymShdr
->sh_addr
^ mCoffSectionsOffset
[Sym
->st_shndx
]) & 0xfff) != 0) {
1579 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 small code model requires identical ELF and PE/COFF section offsets modulo 4 KB.",
1585 case R_AARCH64_ADR_PREL_LO21
:
1586 case R_AARCH64_CONDBR19
:
1587 case R_AARCH64_LD_PREL_LO19
:
1588 case R_AARCH64_CALL26
:
1589 case R_AARCH64_JUMP26
:
1590 case R_AARCH64_PREL64
:
1591 case R_AARCH64_PREL32
:
1592 case R_AARCH64_PREL16
:
1594 // The GCC toolchains (i.e., binutils) may corrupt section relative
1595 // relocations when emitting relocation sections into fully linked
1596 // binaries. More specifically, they tend to fail to take into
1597 // account the fact that a '.rodata + XXX' relocation needs to have
1598 // its addend recalculated once .rodata is merged into the .text
1599 // section, and the relocation emitted into the .rela.text section.
1601 // We cannot really recover from this loss of information, so the
1602 // only workaround is to prevent having to recalculate any relative
1603 // relocations at all, by using a linker script that ensures that
1604 // the offset between the Place and the Symbol is the same in both
1605 // the ELF and the PE/COFF versions of the binary.
1607 if ((SymShdr
->sh_addr
- SecShdr
->sh_addr
) !=
1608 (mCoffSectionsOffset
[Sym
->st_shndx
] - SecOffset
)) {
1609 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 relative relocations require identical ELF and PE/COFF section offsets",
1614 // Absolute relocations.
1615 case R_AARCH64_ABS64
:
1616 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1620 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1622 } else if (mEhdr
->e_machine
== EM_RISCV64
) {
1624 // Write section for RISC-V 64 architecture.
1626 WriteSectionRiscV64 (Rel
, Targ
, SymShdr
, Sym
);
1627 } else if (mEhdr
->e_machine
== EM_LOONGARCH
) {
1628 switch (ELF_R_TYPE(Rel
->r_info
)) {
1632 case R_LARCH_SOP_PUSH_ABSOLUTE
:
1634 // Absolute relocation.
1636 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1639 case R_LARCH_MARK_LA
:
1643 case R_LARCH_RELATIVE
:
1645 case R_LARCH_JUMP_SLOT
:
1646 case R_LARCH_TLS_DTPMOD32
:
1647 case R_LARCH_TLS_DTPMOD64
:
1648 case R_LARCH_TLS_DTPREL32
:
1649 case R_LARCH_TLS_DTPREL64
:
1650 case R_LARCH_TLS_TPREL32
:
1651 case R_LARCH_TLS_TPREL64
:
1652 case R_LARCH_IRELATIVE
:
1653 case R_LARCH_MARK_PCREL
:
1654 case R_LARCH_SOP_PUSH_PCREL
:
1655 case R_LARCH_SOP_PUSH_DUP
:
1656 case R_LARCH_SOP_PUSH_GPREL
:
1657 case R_LARCH_SOP_PUSH_TLS_TPREL
:
1658 case R_LARCH_SOP_PUSH_TLS_GOT
:
1659 case R_LARCH_SOP_PUSH_TLS_GD
:
1660 case R_LARCH_SOP_PUSH_PLT_PCREL
:
1661 case R_LARCH_SOP_ASSERT
:
1662 case R_LARCH_SOP_NOT
:
1663 case R_LARCH_SOP_SUB
:
1664 case R_LARCH_SOP_SL
:
1665 case R_LARCH_SOP_SR
:
1666 case R_LARCH_SOP_ADD
:
1667 case R_LARCH_SOP_AND
:
1668 case R_LARCH_SOP_IF_ELSE
:
1669 case R_LARCH_SOP_POP_32_S_10_5
:
1670 case R_LARCH_SOP_POP_32_U_10_12
:
1671 case R_LARCH_SOP_POP_32_S_10_12
:
1672 case R_LARCH_SOP_POP_32_S_10_16
:
1673 case R_LARCH_SOP_POP_32_S_10_16_S2
:
1674 case R_LARCH_SOP_POP_32_S_5_20
:
1675 case R_LARCH_SOP_POP_32_S_0_5_10_16_S2
:
1676 case R_LARCH_SOP_POP_32_S_0_10_10_16_S2
:
1677 case R_LARCH_SOP_POP_32_U
:
1688 case R_LARCH_GNU_VTINHERIT
:
1689 case R_LARCH_GNU_VTENTRY
:
1693 case R_LARCH_ABS_HI20
:
1694 case R_LARCH_ABS_LO12
:
1695 case R_LARCH_ABS64_LO20
:
1696 case R_LARCH_ABS64_HI12
:
1697 case R_LARCH_PCALA_LO12
:
1698 case R_LARCH_PCALA64_LO20
:
1699 case R_LARCH_PCALA64_HI12
:
1700 case R_LARCH_GOT_PC_LO12
:
1701 case R_LARCH_GOT64_PC_LO20
:
1702 case R_LARCH_GOT64_PC_HI12
:
1703 case R_LARCH_GOT64_HI20
:
1704 case R_LARCH_GOT64_LO12
:
1705 case R_LARCH_GOT64_LO20
:
1706 case R_LARCH_GOT64_HI12
:
1707 case R_LARCH_TLS_LE_HI20
:
1708 case R_LARCH_TLS_LE_LO12
:
1709 case R_LARCH_TLS_LE64_LO20
:
1710 case R_LARCH_TLS_LE64_HI12
:
1711 case R_LARCH_TLS_IE_PC_HI20
:
1712 case R_LARCH_TLS_IE_PC_LO12
:
1713 case R_LARCH_TLS_IE64_PC_LO20
:
1714 case R_LARCH_TLS_IE64_PC_HI12
:
1715 case R_LARCH_TLS_IE64_HI20
:
1716 case R_LARCH_TLS_IE64_LO12
:
1717 case R_LARCH_TLS_IE64_LO20
:
1718 case R_LARCH_TLS_IE64_HI12
:
1719 case R_LARCH_TLS_LD_PC_HI20
:
1720 case R_LARCH_TLS_LD64_HI20
:
1721 case R_LARCH_TLS_GD_PC_HI20
:
1722 case R_LARCH_TLS_GD64_HI20
:
1725 // These types are not used or do not require fixup.
1729 case R_LARCH_GOT_PC_HI20
:
1730 Offset
= Sym
->st_value
- (UINTN
)(Targ
- mCoffFile
);
1732 Offset
= (UINTN
)(Targ
- mCoffFile
) - Sym
->st_value
;
1733 Hi
= Offset
& ~0xfff;
1734 Lo
= (INT32
)((Offset
& 0xfff) << 20) >> 20;
1735 if ((Lo
< 0) && (Lo
> -2048)) {
1737 Lo
= ~(0x1000 - Lo
) + 1;
1742 Hi
= Offset
& ~0xfff;
1743 Lo
= (INT32
)((Offset
& 0xfff) << 20) >> 20;
1746 Lo
= ~(0x1000 - Lo
) + 1;
1749 // Re-encode the offset as PCADDU12I + ADDI.D(Convert LD.D) instruction
1750 *(UINT32
*)Targ
&= 0x1f;
1751 *(UINT32
*)Targ
|= 0x1c000000;
1752 *(UINT32
*)Targ
|= (((Hi
>> 12) & 0xfffff) << 5);
1753 *(UINT32
*)(Targ
+ 4) &= 0x3ff;
1754 *(UINT32
*)(Targ
+ 4) |= 0x2c00000 | ((Lo
& 0xfff) << 10);
1758 // Attempt to convert instruction.
1760 case R_LARCH_PCALA_HI20
:
1761 // Decode the PCALAU12I instruction and the instruction that following it.
1762 Offset
= ((INT32
)((*(UINT32
*)Targ
& 0x1ffffe0) << 7));
1763 Offset
+= ((INT32
)((*(UINT32
*)(Targ
+ 4) & 0x3ffc00) << 10) >> 20);
1765 // PCALA offset is relative to the previous page boundary,
1766 // whereas PCADD offset is relative to the instruction itself.
1767 // So fix up the offset so it points to the page containing
1770 Offset
-= (UINTN
)(Targ
- mCoffFile
) & 0xfff;
1773 Hi
= Offset
& ~0xfff;
1774 Lo
= (INT32
)((Offset
& 0xfff) << 20) >> 20;
1775 if ((Lo
< 0) && (Lo
> -2048)) {
1777 Lo
= ~(0x1000 - Lo
) + 1;
1782 Hi
= Offset
& ~0xfff;
1783 Lo
= (INT32
)((Offset
& 0xfff) << 20) >> 20;
1786 Lo
= ~(0x1000 - Lo
) + 1;
1789 // Convert the first instruction from PCALAU12I to PCADDU12I and re-encode the offset into them.
1790 *(UINT32
*)Targ
&= 0x1f;
1791 *(UINT32
*)Targ
|= 0x1c000000;
1792 *(UINT32
*)Targ
|= (((Hi
>> 12) & 0xfffff) << 5);
1793 *(UINT32
*)(Targ
+ 4) &= 0xffc003ff;
1794 *(UINT32
*)(Targ
+ 4) |= (Lo
& 0xfff) << 10;
1797 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_LOONGARCH relocation 0x%x.", mInImageName
, (unsigned) ELF64_R_TYPE(Rel
->r_info
));
1800 Error (NULL
, 0, 3000, "Invalid", "Not a supported machine type");
1811 WriteRelocations64 (
1816 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1817 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
1818 UINT32 RiscVRelType
;
1820 for (Index
= 0; Index
< mEhdr
->e_shnum
; Index
++) {
1821 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
1822 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
1823 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
1824 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
1827 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
1828 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
1830 if (mEhdr
->e_machine
== EM_X86_64
) {
1831 switch (ELF_R_TYPE(Rel
->r_info
)) {
1834 case R_X86_64_PLT32
:
1835 case R_X86_64_GOTPCREL
:
1836 case R_X86_64_GOTPCRELX
:
1837 case R_X86_64_REX_GOTPCRELX
:
1840 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08llX",
1841 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
1843 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1844 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1845 EFI_IMAGE_REL_BASED_DIR64
);
1848 // R_X86_64_32 and R_X86_64_32S are ELF64 relocations emitted when using
1849 // the SYSV X64 ABI small non-position-independent code model.
1850 // R_X86_64_32 is used for unsigned 32-bit immediates with a 32-bit operand
1851 // size. The value is either not extended, or zero-extended to 64 bits.
1852 // R_X86_64_32S is used for either signed 32-bit non-rip-relative displacements
1853 // or signed 32-bit immediates with a 64-bit operand size. The value is
1854 // sign-extended to 64 bits.
1855 // EFI_IMAGE_REL_BASED_HIGHLOW is a PE relocation that uses 32-bit arithmetic
1856 // for rebasing an image.
1857 // EFI PE binaries declare themselves EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE and
1858 // may load above 2GB. If an EFI PE binary with a converted R_X86_64_32S
1859 // relocation is loaded above 2GB, the value will get sign-extended to the
1860 // negative part of the 64-bit address space. The negative part of the 64-bit
1861 // address space is unmapped, so accessing such an address page-faults.
1862 // In order to support R_X86_64_32S, it is necessary to unset
1863 // EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE, and the EFI PE loader must implement
1864 // this flag and abstain from loading such a PE binary above 2GB.
1865 // Since this feature is not supported, support for R_X86_64_32S (and hence
1866 // the small non-position-independent code model) is disabled.
1868 // case R_X86_64_32S:
1870 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08llX",
1871 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
1873 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1874 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1875 EFI_IMAGE_REL_BASED_HIGHLOW
);
1878 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1880 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
1882 switch (ELF_R_TYPE(Rel
->r_info
)) {
1883 case R_AARCH64_ADR_PREL_LO21
:
1884 case R_AARCH64_CONDBR19
:
1885 case R_AARCH64_LD_PREL_LO19
:
1886 case R_AARCH64_CALL26
:
1887 case R_AARCH64_JUMP26
:
1888 case R_AARCH64_PREL64
:
1889 case R_AARCH64_PREL32
:
1890 case R_AARCH64_PREL16
:
1891 case R_AARCH64_ADR_PREL_PG_HI21
:
1892 case R_AARCH64_ADD_ABS_LO12_NC
:
1893 case R_AARCH64_LDST8_ABS_LO12_NC
:
1894 case R_AARCH64_LDST16_ABS_LO12_NC
:
1895 case R_AARCH64_LDST32_ABS_LO12_NC
:
1896 case R_AARCH64_LDST64_ABS_LO12_NC
:
1897 case R_AARCH64_LDST128_ABS_LO12_NC
:
1898 case R_AARCH64_ADR_GOT_PAGE
:
1899 case R_AARCH64_LD64_GOT_LO12_NC
:
1900 case R_AARCH64_LD64_GOTOFF_LO15
:
1901 case R_AARCH64_LD64_GOTPAGE_LO15
:
1903 // No fixups are required for relative relocations, provided that
1904 // the relative offsets between sections have been preserved in
1905 // the ELF to PE/COFF conversion. We have already asserted that
1906 // this is the case in WriteSections64 ().
1910 case R_AARCH64_ABS64
:
1912 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1913 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1914 EFI_IMAGE_REL_BASED_DIR64
);
1917 case R_AARCH64_ABS32
:
1919 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1920 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1921 EFI_IMAGE_REL_BASED_HIGHLOW
);
1925 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1927 } else if (mEhdr
->e_machine
== EM_RISCV64
) {
1928 RiscVRelType
= ELF_R_TYPE(Rel
->r_info
);
1929 switch (RiscVRelType
) {
1935 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1936 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1937 EFI_IMAGE_REL_BASED_HIGHLOW
);
1942 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1943 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1944 EFI_IMAGE_REL_BASED_DIR64
);
1949 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1950 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1951 EFI_IMAGE_REL_BASED_RISCV_HI20
);
1954 case R_RISCV_LO12_I
:
1956 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1957 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1958 EFI_IMAGE_REL_BASED_RISCV_LOW12I
);
1961 case R_RISCV_LO12_S
:
1963 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1964 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1965 EFI_IMAGE_REL_BASED_RISCV_LOW12S
);
1970 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1971 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1972 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1977 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1978 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1979 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1984 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1985 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1986 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1991 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1992 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1993 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1996 case R_RISCV_BRANCH
:
1998 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1999 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
2000 EFI_IMAGE_REL_BASED_ABSOLUTE
);
2005 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
2006 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
2007 EFI_IMAGE_REL_BASED_ABSOLUTE
);
2010 case R_RISCV_GPREL_I
:
2011 case R_RISCV_GPREL_S
:
2013 case R_RISCV_CALL_PLT
:
2014 case R_RISCV_RVC_BRANCH
:
2015 case R_RISCV_RVC_JUMP
:
2022 case R_RISCV_PCREL_HI20
:
2023 case R_RISCV_GOT_HI20
:
2024 case R_RISCV_PCREL_LO12_I
:
2025 case R_RISCV_PCREL_LO12_S
:
2029 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_RISCV64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
2031 } else if (mEhdr
->e_machine
== EM_LOONGARCH
) {
2032 switch (ELF_R_TYPE(Rel
->r_info
)) {
2033 case R_LARCH_MARK_LA
:
2035 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
2036 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
2037 EFI_IMAGE_REL_BASED_LOONGARCH64_MARK_LA
);
2041 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
2042 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
2043 EFI_IMAGE_REL_BASED_DIR64
);
2047 case R_LARCH_RELATIVE
:
2049 case R_LARCH_JUMP_SLOT
:
2050 case R_LARCH_TLS_DTPMOD32
:
2051 case R_LARCH_TLS_DTPMOD64
:
2052 case R_LARCH_TLS_DTPREL32
:
2053 case R_LARCH_TLS_DTPREL64
:
2054 case R_LARCH_TLS_TPREL32
:
2055 case R_LARCH_TLS_TPREL64
:
2056 case R_LARCH_IRELATIVE
:
2057 case R_LARCH_MARK_PCREL
:
2058 case R_LARCH_SOP_PUSH_PCREL
:
2059 case R_LARCH_SOP_PUSH_ABSOLUTE
:
2060 case R_LARCH_SOP_PUSH_DUP
:
2061 case R_LARCH_SOP_PUSH_GPREL
:
2062 case R_LARCH_SOP_PUSH_TLS_TPREL
:
2063 case R_LARCH_SOP_PUSH_TLS_GOT
:
2064 case R_LARCH_SOP_PUSH_TLS_GD
:
2065 case R_LARCH_SOP_PUSH_PLT_PCREL
:
2066 case R_LARCH_SOP_ASSERT
:
2067 case R_LARCH_SOP_NOT
:
2068 case R_LARCH_SOP_SUB
:
2069 case R_LARCH_SOP_SL
:
2070 case R_LARCH_SOP_SR
:
2071 case R_LARCH_SOP_ADD
:
2072 case R_LARCH_SOP_AND
:
2073 case R_LARCH_SOP_IF_ELSE
:
2074 case R_LARCH_SOP_POP_32_S_10_5
:
2075 case R_LARCH_SOP_POP_32_U_10_12
:
2076 case R_LARCH_SOP_POP_32_S_10_12
:
2077 case R_LARCH_SOP_POP_32_S_10_16
:
2078 case R_LARCH_SOP_POP_32_S_10_16_S2
:
2079 case R_LARCH_SOP_POP_32_S_5_20
:
2080 case R_LARCH_SOP_POP_32_S_0_5_10_16_S2
:
2081 case R_LARCH_SOP_POP_32_S_0_10_10_16_S2
:
2082 case R_LARCH_SOP_POP_32_U
:
2093 case R_LARCH_GNU_VTINHERIT
:
2094 case R_LARCH_GNU_VTENTRY
:
2098 case R_LARCH_ABS_HI20
:
2099 case R_LARCH_ABS_LO12
:
2100 case R_LARCH_ABS64_LO20
:
2101 case R_LARCH_ABS64_HI12
:
2102 case R_LARCH_PCALA_HI20
:
2103 case R_LARCH_PCALA_LO12
:
2104 case R_LARCH_PCALA64_LO20
:
2105 case R_LARCH_PCALA64_HI12
:
2106 case R_LARCH_GOT_PC_HI20
:
2107 case R_LARCH_GOT_PC_LO12
:
2108 case R_LARCH_GOT64_PC_LO20
:
2109 case R_LARCH_GOT64_PC_HI12
:
2110 case R_LARCH_GOT64_HI20
:
2111 case R_LARCH_GOT64_LO12
:
2112 case R_LARCH_GOT64_LO20
:
2113 case R_LARCH_GOT64_HI12
:
2114 case R_LARCH_TLS_LE_HI20
:
2115 case R_LARCH_TLS_LE_LO12
:
2116 case R_LARCH_TLS_LE64_LO20
:
2117 case R_LARCH_TLS_LE64_HI12
:
2118 case R_LARCH_TLS_IE_PC_HI20
:
2119 case R_LARCH_TLS_IE_PC_LO12
:
2120 case R_LARCH_TLS_IE64_PC_LO20
:
2121 case R_LARCH_TLS_IE64_PC_HI12
:
2122 case R_LARCH_TLS_IE64_HI20
:
2123 case R_LARCH_TLS_IE64_LO12
:
2124 case R_LARCH_TLS_IE64_LO20
:
2125 case R_LARCH_TLS_IE64_HI12
:
2126 case R_LARCH_TLS_LD_PC_HI20
:
2127 case R_LARCH_TLS_LD64_HI20
:
2128 case R_LARCH_TLS_GD_PC_HI20
:
2129 case R_LARCH_TLS_GD64_HI20
:
2132 // These types are not used or do not require fixup in PE format files.
2136 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_LOONGARCH relocation 0x%x.", mInImageName
, (unsigned) ELF64_R_TYPE(Rel
->r_info
));
2139 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
2142 if (mEhdr
->e_machine
== EM_X86_64
&& RelShdr
->sh_info
== mGOTShindex
) {
2144 // Tack relocations for GOT entries after other relocations for
2145 // the section the GOT is in, as it's usually found at the end
2146 // of the section. This is done in order to maintain Rva order
2147 // of Coff relocations.
2149 EmitGOTRelocations();
2155 if (mEhdr
->e_machine
== EM_X86_64
) {
2157 // This is a safety net just in case the GOT is in a section
2158 // with no other relocations and the first invocation of
2159 // EmitGOTRelocations() above was skipped. This invocation
2160 // does not maintain Rva order of Coff relocations.
2161 // At present, with a single text section, all references to
2162 // the GOT and the GOT itself reside in section .text, so
2163 // if there's a GOT at all, the first invocation above
2166 EmitGOTRelocations();
2169 // Pad by adding empty entries.
2171 while (mCoffOffset
& (mCoffAlignment
- 1)) {
2172 CoffAddFixupEntry(0);
2175 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
2176 Dir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
2177 Dir
->Size
= mCoffOffset
- mRelocOffset
;
2178 if (Dir
->Size
== 0) {
2179 // If no relocations, null out the directory entry and don't add the .reloc section
2180 Dir
->VirtualAddress
= 0;
2181 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
2183 Dir
->VirtualAddress
= mRelocOffset
;
2184 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
2185 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
2186 | EFI_IMAGE_SCN_MEM_DISCARDABLE
2187 | EFI_IMAGE_SCN_MEM_READ
);
2198 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
2199 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
2200 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
2201 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
2203 Len
= strlen(mInImageName
) + 1;
2205 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ mDebugOffset
);
2206 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
2207 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
2208 Dir
->RVA
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
2209 Dir
->FileOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
2211 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
2212 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
2213 strcpy ((char *)(Nb10
+ 1), mInImageName
);
2216 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
2217 DataDir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
2218 DataDir
->VirtualAddress
= mDebugOffset
;
2219 DataDir
->Size
= sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
2228 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
2233 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
2234 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
2243 if (mCoffSectionsOffset
!= NULL
) {
2244 free (mCoffSectionsOffset
);
2254 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
2255 EFI_IMAGE_EXPORT_DIRECTORY
*ExportDir
;
2256 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
2257 UINT32 FileNameOffset
;
2260 UINT8
*Tdata
= NULL
;
2262 ExportDir
= (EFI_IMAGE_EXPORT_DIRECTORY
*)(mCoffFile
+ mExportOffset
);
2263 ExportDir
->Characteristics
= 0;
2264 ExportDir
->TimeDateStamp
= 0;
2265 ExportDir
->MajorVersion
= 0;
2266 ExportDir
->MinorVersion
=0;
2267 ExportDir
->Name
= 0;
2268 ExportDir
->NumberOfFunctions
= mExportSymNum
;
2269 ExportDir
->NumberOfNames
= mExportSymNum
;
2270 ExportDir
->Base
= EFI_IMAGE_EXPORT_ORDINAL_BASE
;
2271 ExportDir
->AddressOfFunctions
= mExportOffset
+ sizeof(EFI_IMAGE_EXPORT_DIRECTORY
);
2272 ExportDir
->AddressOfNames
= ExportDir
->AddressOfFunctions
+ EFI_IMAGE_EXPORT_ADDR_SIZE
* mExportSymNum
;
2273 ExportDir
->AddressOfNameOrdinals
= ExportDir
->AddressOfNames
+ EFI_IMAGE_EXPORT_ADDR_SIZE
* mExportSymNum
;
2275 FileNameOffset
= ExportDir
->AddressOfNameOrdinals
+ EFI_IMAGE_EXPORT_ORDINAL_SIZE
* mExportSymNum
;
2276 NameOffset
= FileNameOffset
+ strlen(mInImageName
) + 1;
2278 // Write Input image Name RVA
2279 ExportDir
->Name
= FileNameOffset
;
2281 // Write Input image Name
2282 strcpy((char *)(mCoffFile
+ FileNameOffset
), mInImageName
);
2284 for (Index
= 0; Index
< mExportSymNum
; Index
++) {
2286 // Write Export Address Table
2288 Tdata
= mCoffFile
+ ExportDir
->AddressOfFunctions
+ Index
* EFI_IMAGE_EXPORT_ADDR_SIZE
;
2289 *(UINT32
*)Tdata
= mExportRVA
[Index
];
2292 // Write Export Name Pointer Table
2294 Tdata
= mCoffFile
+ ExportDir
->AddressOfNames
+ Index
* EFI_IMAGE_EXPORT_ADDR_SIZE
;
2295 *(UINT32
*)Tdata
= NameOffset
;
2298 // Write Export Ordinal table
2300 Tdata
= mCoffFile
+ ExportDir
->AddressOfNameOrdinals
+ Index
* EFI_IMAGE_EXPORT_ORDINAL_SIZE
;
2301 *(UINT16
*)Tdata
= Index
;
2304 // Write Export Name Table
2306 strcpy((char *)(mCoffFile
+ NameOffset
), mExportSymName
[Index
]);
2307 NameOffset
+= strlen(mExportSymName
[Index
]) + 1;
2310 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
2311 DataDir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT
];
2312 DataDir
->VirtualAddress
= mExportOffset
;
2313 DataDir
->Size
= mExportSize
;