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_S
:
561 if (mRiscVPass1Targ
!= NULL
&& mRiscVPass1Sym
!= NULL
&& mRiscVPass1SymSecIndex
!= 0) {
563 Value2
= (UINT32
)(RV_X(*(UINT32
*)mRiscVPass1Targ
, 12, 20));
565 Value
= ((UINT32
)(RV_X(*(UINT32
*)Targ
, 25, 7)) << 5);
566 Value
= (Value
| (UINT32
)(RV_X(*(UINT32
*)Targ
, 7, 5)));
568 if(Value
& (RISCV_IMM_REACH
/2)) {
569 Value
|= ~(RISCV_IMM_REACH
-1);
571 Value
= Value
- (UINT32
)mRiscVPass1Sym
->sh_addr
+ mCoffSectionsOffset
[mRiscVPass1SymSecIndex
];
573 if(-2048 > (INT32
)Value
) {
574 i
= (((INT32
)Value
* -1) / 4096);
577 if(-2048 > (INT32
)Value
) {
582 else if( 2047 < (INT32
)Value
) {
586 if(2047 < (INT32
)Value
) {
592 // Update the IMM of SD instruction
594 // |31 25|24 20|19 15|14 12 |11 7|6 0|
595 // |-------------------------------------------|-------|
596 // |imm[11:5] | rs2 | rs1 | funct3 |imm[4:0] | opcode|
597 // ---------------------------------------------------
599 // First Zero out current IMM
600 *(UINT32
*)Targ
&= ~0xfe000f80;
602 // Update with new IMM
603 *(UINT32
*)Targ
|= (RV_X(Value
, 5, 7) << 25);
604 *(UINT32
*)Targ
|= (RV_X(Value
, 0, 5) << 7);
606 // Update previous instruction
607 *(UINT32
*)mRiscVPass1Targ
= (RV_X(Value2
, 0, 20)<<12) | (RV_X(*(UINT32
*)mRiscVPass1Targ
, 0, 12));
609 mRiscVPass1Sym
= NULL
;
610 mRiscVPass1Targ
= NULL
;
611 mRiscVPass1SymSecIndex
= 0;
614 case R_RISCV_PCREL_LO12_I
:
615 if (mRiscVPass1Targ
!= NULL
&& mRiscVPass1Sym
!= NULL
&& mRiscVPass1SymSecIndex
!= 0) {
617 Value2
= (UINT32
)(RV_X(*(UINT32
*)mRiscVPass1Targ
, 12, 20));
619 if(mRiscVPass1GotFixup
) {
620 Value
= (UINT32
)(mRiscVPass1Offset
);
622 Value
= (UINT32
)(RV_X(*(UINT32
*)Targ
, 20, 12));
623 if(Value
& (RISCV_IMM_REACH
/2)) {
624 Value
|= ~(RISCV_IMM_REACH
-1);
627 Value
= Value
- (UINT32
)mRiscVPass1Sym
->sh_addr
+ mCoffSectionsOffset
[mRiscVPass1SymSecIndex
];
629 if(-2048 > (INT32
)Value
) {
630 i
= (((INT32
)Value
* -1) / 4096);
633 if(-2048 > (INT32
)Value
) {
638 else if( 2047 < (INT32
)Value
) {
642 if(2047 < (INT32
)Value
) {
648 if(mRiscVPass1GotFixup
) {
649 *(UINT32
*)Targ
= (RV_X((UINT32
)Value
, 0, 12) << 20)
650 | (RV_X(*(UINT32
*)Targ
, 0, 20));
651 // Convert LD instruction to ADDI
653 // |31 20|19 15|14 12|11 7|6 0|
654 // |-----------------------------------------|
655 // |imm[11:0] | rs1 | 011 | rd | 0000011 | LD
656 // -----------------------------------------
658 // |-----------------------------------------|
659 // |imm[11:0] | rs1 | 000 | rd | 0010011 | ADDI
660 // -----------------------------------------
662 // To convert, let's first reset bits 12-14 and 0-6 using ~0x707f
663 // Then modify the opcode to ADDI (0010011)
664 // All other fields will remain same.
666 *(UINT32
*)Targ
= ((*(UINT32
*)Targ
& ~0x707f) | 0x13);
668 *(UINT32
*)Targ
= (RV_X(Value
, 0, 12) << 20) | (RV_X(*(UINT32
*)Targ
, 0, 20));
670 *(UINT32
*)mRiscVPass1Targ
= (RV_X(Value2
, 0, 20)<<12) | (RV_X(*(UINT32
*)mRiscVPass1Targ
, 0, 12));
672 mRiscVPass1Sym
= NULL
;
673 mRiscVPass1Targ
= NULL
;
674 mRiscVPass1SymSecIndex
= 0;
675 mRiscVPass1Offset
= 0;
676 mRiscVPass1GotFixup
= 0;
685 case R_RISCV_GPREL_I
:
686 case R_RISCV_GPREL_S
:
688 case R_RISCV_CALL_PLT
:
689 case R_RISCV_RVC_BRANCH
:
690 case R_RISCV_RVC_JUMP
:
700 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_RISCV64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
705 // Elf functions interface implementation
715 EFI_IMAGE_DOS_HEADER
*DosHdr
;
716 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
719 BOOLEAN FoundSection
;
725 // Coff file start with a DOS header.
727 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
728 mNtHdrOffset
= mCoffOffset
;
729 switch (mEhdr
->e_machine
) {
733 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
736 VerboseMsg ("%s unknown e_machine type %hu. Assume X64", mInImageName
, mEhdr
->e_machine
);
737 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
741 mTableOffset
= mCoffOffset
;
742 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
745 // Set mCoffAlignment to the maximum alignment of the input sections
748 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
749 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
750 if (shdr
->sh_addralign
<= mCoffAlignment
) {
753 if (IsTextShdr(shdr
) || IsDataShdr(shdr
) || IsHiiRsrcShdr(shdr
)) {
754 mCoffAlignment
= (UINT32
)shdr
->sh_addralign
;
759 // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT
761 if (mCoffAlignment
> MAX_COFF_ALIGNMENT
) {
762 Error (NULL
, 0, 3000, "Invalid", "Section alignment is larger than MAX_COFF_ALIGNMENT.");
768 // Move the PE/COFF header right before the first section. This will help us
769 // save space when converting to TE.
771 if (mCoffAlignment
> mCoffOffset
) {
772 mNtHdrOffset
+= mCoffAlignment
- mCoffOffset
;
773 mTableOffset
+= mCoffAlignment
- mCoffOffset
;
774 mCoffOffset
= mCoffAlignment
;
778 // First text sections.
780 mCoffOffset
= CoffAlign(mCoffOffset
);
781 mTextOffset
= mCoffOffset
;
782 FoundSection
= FALSE
;
784 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
785 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
786 if (IsTextShdr(shdr
)) {
787 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
788 // the alignment field is valid
789 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
790 // if the section address is aligned we must align PE/COFF
791 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
793 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
797 /* Relocate entry. */
798 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
799 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
800 CoffEntry
= (UINT32
) (mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
);
804 // Set mTextOffset with the offset of the first '.text' section
807 mTextOffset
= mCoffOffset
;
811 mCoffSectionsOffset
[i
] = mCoffOffset
;
812 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
817 if (!FoundSection
&& mOutImageType
!= FW_ACPI_IMAGE
) {
818 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
822 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
823 mCoffOffset
= CoffAlign(mCoffOffset
);
825 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
826 Warning (NULL
, 0, 0, NULL
, "Multiple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
830 // Then data sections.
832 mDataOffset
= mCoffOffset
;
833 FoundSection
= FALSE
;
835 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
836 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
837 if (IsDataShdr(shdr
)) {
838 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
839 // the alignment field is valid
840 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
841 // if the section address is aligned we must align PE/COFF
842 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
844 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
849 // Set mDataOffset with the offset of the first '.data' section
852 mDataOffset
= mCoffOffset
;
855 mCoffSectionsOffset
[i
] = mCoffOffset
;
856 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
862 // Make room for .debug data in .data (or .text if .data is empty) instead of
863 // putting it in a section of its own. This is explicitly allowed by the
864 // PE/COFF spec, and prevents bloat in the binary when using large values for
865 // section alignment.
867 if (SectionCount
> 0) {
868 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
870 mCoffOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
) +
871 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) +
872 strlen(mInImageName
) + 1;
874 mCoffOffset
= CoffAlign(mCoffOffset
);
875 if (SectionCount
== 0) {
876 mDataOffset
= mCoffOffset
;
879 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
880 Warning (NULL
, 0, 0, NULL
, "Multiple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
884 // The HII resource sections.
886 mHiiRsrcOffset
= mCoffOffset
;
887 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
888 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
889 if (IsHiiRsrcShdr(shdr
)) {
890 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
891 // the alignment field is valid
892 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
893 // if the section address is aligned we must align PE/COFF
894 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
896 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
899 if (shdr
->sh_size
!= 0) {
900 mHiiRsrcOffset
= mCoffOffset
;
901 mCoffSectionsOffset
[i
] = mCoffOffset
;
902 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
903 mCoffOffset
= CoffAlign(mCoffOffset
);
904 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
910 mRelocOffset
= mCoffOffset
;
913 // Allocate base Coff file. Will be expanded later for relocations.
915 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
916 if (mCoffFile
== NULL
) {
917 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
919 assert (mCoffFile
!= NULL
);
920 memset(mCoffFile
, 0, mCoffOffset
);
925 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
926 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
927 DosHdr
->e_lfanew
= mNtHdrOffset
;
929 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
931 NtHdr
->Pe32Plus
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
933 switch (mEhdr
->e_machine
) {
935 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
936 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
939 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_AARCH64
;
940 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
943 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_RISCV64
;
944 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
948 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN
)mEhdr
->e_machine
);
949 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
950 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
953 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
954 NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
955 mImageTimeStamp
= NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
;
956 NtHdr
->Pe32Plus
.FileHeader
.PointerToSymbolTable
= 0;
957 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSymbols
= 0;
958 NtHdr
->Pe32Plus
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32Plus
.OptionalHeader
);
959 NtHdr
->Pe32Plus
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
960 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
961 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
962 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE
;
964 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
965 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
966 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfUninitializedData
= 0;
967 NtHdr
->Pe32Plus
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
969 NtHdr
->Pe32Plus
.OptionalHeader
.BaseOfCode
= mTextOffset
;
971 NtHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= 0;
972 NtHdr
->Pe32Plus
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
973 NtHdr
->Pe32Plus
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
974 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= 0;
976 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
977 NtHdr
->Pe32Plus
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
982 if ((mDataOffset
- mTextOffset
) > 0) {
983 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
984 EFI_IMAGE_SCN_CNT_CODE
985 | EFI_IMAGE_SCN_MEM_EXECUTE
986 | EFI_IMAGE_SCN_MEM_READ
);
988 // Don't make a section of size 0.
989 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
992 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
993 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
994 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
995 | EFI_IMAGE_SCN_MEM_WRITE
996 | EFI_IMAGE_SCN_MEM_READ
);
998 // Don't make a section of size 0.
999 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
1002 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
1003 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
1004 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1005 | EFI_IMAGE_SCN_MEM_READ
);
1007 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
1008 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
1010 // Don't make a section of size 0.
1011 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
1019 SECTION_FILTER_TYPES FilterType
1025 BOOLEAN (*Filter
)(Elf_Shdr
*);
1026 Elf64_Addr GOTEntryRva
;
1029 // Initialize filter pointer
1031 switch (FilterType
) {
1033 Filter
= IsTextShdr
;
1036 Filter
= IsHiiRsrcShdr
;
1039 Filter
= IsDataShdr
;
1046 // First: copy sections.
1048 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
1049 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
1050 if ((*Filter
)(Shdr
)) {
1051 switch (Shdr
->sh_type
) {
1054 if (Shdr
->sh_offset
+ Shdr
->sh_size
> mFileBufferSize
) {
1057 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
1058 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
1059 (size_t) Shdr
->sh_size
);
1063 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, (size_t) Shdr
->sh_size
);
1068 // Ignore for unknown section type.
1070 VerboseMsg ("%s unknown section type %x. We ignore this unknown section type.", mInImageName
, (unsigned)Shdr
->sh_type
);
1077 // Second: apply relocations.
1079 VerboseMsg ("Applying Relocations...");
1080 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
1082 // Determine if this is a relocation section.
1084 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
1085 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
1090 // If this is a ET_DYN (PIE) executable, we will encounter a dynamic SHT_RELA
1091 // section that applies to the entire binary, and which will have its section
1092 // index set to #0 (which is a NULL section with the SHF_ALLOC bit cleared).
1094 // In the absence of GOT based relocations,
1095 // this RELA section will contain redundant R_xxx_RELATIVE relocations, one
1096 // for every R_xxx_xx64 relocation appearing in the per-section RELA sections.
1097 // (i.e., .rela.text and .rela.data)
1099 if (RelShdr
->sh_info
== 0) {
1104 // Relocation section found. Now extract section information that the relocations
1105 // apply to in the ELF data and the new COFF data.
1107 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
1108 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
1111 // Only process relocations for the current filter type.
1113 if (RelShdr
->sh_type
== SHT_RELA
&& (*Filter
)(SecShdr
)) {
1117 // Determine the symbol table referenced by the relocation data.
1119 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
1120 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
1123 // Process all relocation entries for this section.
1125 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= (UINT32
) RelShdr
->sh_entsize
) {
1128 // Set pointer to relocation entry
1130 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
1133 // Set pointer to symbol table entry associated with the relocation entry.
1135 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
1141 // Check section header index found in symbol table and get the section
1144 if (Sym
->st_shndx
== SHN_UNDEF
1145 || Sym
->st_shndx
>= mEhdr
->e_shnum
) {
1146 const UINT8
*SymName
= GetSymName(Sym
);
1147 if (SymName
== NULL
) {
1148 SymName
= (const UINT8
*)"<unknown>";
1152 // Skip error on EM_RISCV64 becasue no symble name is built
1153 // from RISC-V toolchain.
1155 if (mEhdr
->e_machine
!= EM_RISCV64
) {
1156 Error (NULL
, 0, 3000, "Invalid",
1157 "%s: Bad definition for symbol '%s'@%#llx or unsupported symbol type. "
1158 "For example, absolute and undefined symbols are not supported.",
1159 mInImageName
, SymName
, Sym
->st_value
);
1165 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
1168 // Convert the relocation data to a pointer into the coff file.
1171 // r_offset is the virtual address of the storage unit to be relocated.
1172 // sh_addr is the virtual address for the base of the section.
1174 // r_offset in a memory address.
1175 // Convert it to a pointer in the coff file.
1177 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
1180 // Determine how to handle each relocation type based on the machine type.
1182 if (mEhdr
->e_machine
== EM_X86_64
) {
1183 switch (ELF_R_TYPE(Rel
->r_info
)) {
1188 // Absolute relocation.
1190 VerboseMsg ("R_X86_64_64");
1191 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1192 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1194 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1195 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
1198 VerboseMsg ("R_X86_64_32");
1199 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1200 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1202 *(UINT32
*)Targ
= (UINT32
)((UINT64
)(*(UINT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
1203 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1206 VerboseMsg ("R_X86_64_32S");
1207 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1208 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1210 *(INT32
*)Targ
= (INT32
)((INT64
)(*(INT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
1211 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1214 case R_X86_64_PLT32
:
1216 // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is
1217 // possible since we know all code symbol references resolve to
1218 // definitions in the same module (UEFI has no shared libraries),
1219 // and so there is never a reason to jump via a PLT entry,
1220 // allowing us to resolve the reference using the symbol directly.
1222 VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");
1226 // Relative relocation: Symbol - Ip + Addend
1228 VerboseMsg ("R_X86_64_PC32");
1229 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1230 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1232 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
1233 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
1234 - (SecOffset
- SecShdr
->sh_addr
));
1235 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1237 case R_X86_64_GOTPCREL
:
1238 case R_X86_64_GOTPCRELX
:
1239 case R_X86_64_REX_GOTPCRELX
:
1240 VerboseMsg ("R_X86_64_GOTPCREL family");
1241 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1242 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
1244 GOTEntryRva
= Rel
->r_offset
- Rel
->r_addend
+ *(INT32
*)Targ
;
1245 FindElfGOTSectionFromGOTEntryElfRva(GOTEntryRva
);
1246 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
1247 + (mCoffSectionsOffset
[mGOTShindex
] - mGOTShdr
->sh_addr
)
1248 - (SecOffset
- SecShdr
->sh_addr
));
1249 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
1250 GOTEntryRva
+= (mCoffSectionsOffset
[mGOTShindex
] - mGOTShdr
->sh_addr
); // ELF Rva -> COFF Rva
1251 if (AccumulateCoffGOTEntries((UINT32
)GOTEntryRva
)) {
1253 // Relocate GOT entry if it's the first time we run into it
1255 Targ
= mCoffFile
+ GOTEntryRva
;
1257 // Limitation: The following three statements assume memory
1258 // at *Targ is valid because the section containing the GOT
1259 // has already been copied from the ELF image to the Coff image.
1260 // This pre-condition presently holds because the GOT is placed
1261 // in section .text, and the ELF text sections are all copied
1262 // prior to reaching this point.
1263 // If the pre-condition is violated in the future, this fixup
1264 // either needs to be deferred after the GOT section is copied
1265 // to the Coff image, or the fixup should be performed on the
1266 // source Elf image instead of the destination Coff image.
1268 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1269 (UINT32
)GOTEntryRva
,
1271 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1272 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
1276 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1278 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
1280 switch (ELF_R_TYPE(Rel
->r_info
)) {
1283 case R_AARCH64_LD64_GOT_LO12_NC
:
1285 // Convert into an ADD instruction - see R_AARCH64_ADR_GOT_PAGE below.
1287 *(UINT32
*)Targ
&= 0x3ff;
1288 *(UINT32
*)Targ
|= 0x91000000 | ((Sym
->st_value
& 0xfff) << 10);
1291 case R_AARCH64_ADR_GOT_PAGE
:
1293 // This relocation points to the GOT entry that contains the absolute
1294 // address of the symbol we are referring to. Since EDK2 only uses
1295 // fully linked binaries, we can avoid the indirection, and simply
1296 // refer to the symbol directly. This implies having to patch the
1297 // subsequent LDR instruction (covered by a R_AARCH64_LD64_GOT_LO12_NC
1298 // relocation) into an ADD instruction - this is handled above.
1300 Offset
= (Sym
->st_value
- (Rel
->r_offset
& ~0xfff)) >> 12;
1302 *(UINT32
*)Targ
&= 0x9000001f;
1303 *(UINT32
*)Targ
|= ((Offset
& 0x1ffffc) << (5 - 2)) | ((Offset
& 0x3) << 29);
1307 case R_AARCH64_ADR_PREL_PG_HI21
:
1309 // In order to handle Cortex-A53 erratum #843419, the LD linker may
1310 // convert ADRP instructions into ADR instructions, but without
1311 // updating the static relocation type, and so we may end up here
1312 // while the instruction in question is actually ADR. So let's
1313 // just disregard it: the section offset check we apply below to
1314 // ADR instructions will trigger for its R_AARCH64_xxx_ABS_LO12_NC
1315 // companion instruction as well, so it is safe to omit it here.
1317 if ((*(UINT32
*)Targ
& BIT31
) == 0) {
1322 // AArch64 PG_H21 relocations are typically paired with ABS_LO12
1323 // relocations, where a PC-relative reference with +/- 4 GB range is
1324 // split into a relative high part and an absolute low part. Since
1325 // the absolute low part represents the offset into a 4 KB page, we
1326 // either have to convert the ADRP into an ADR instruction, or we
1327 // need to use a section alignment of at least 4 KB, so that the
1328 // binary appears at a correct offset at runtime. In any case, we
1329 // have to make sure that the 4 KB relative offsets of both the
1330 // section containing the reference as well as the section to which
1331 // it refers have not been changed during PE/COFF conversion (i.e.,
1332 // in ScanSections64() above).
1334 if (mCoffAlignment
< 0x1000) {
1336 // Attempt to convert the ADRP into an ADR instruction.
1337 // This is only possible if the symbol is within +/- 1 MB.
1340 // Decode the ADRP instruction
1341 Offset
= (INT32
)((*(UINT32
*)Targ
& 0xffffe0) << 8);
1342 Offset
= (Offset
<< (6 - 5)) | ((*(UINT32
*)Targ
& 0x60000000) >> (29 - 12));
1345 // ADRP offset is relative to the previous page boundary,
1346 // whereas ADR offset is relative to the instruction itself.
1347 // So fix up the offset so it points to the page containing
1350 Offset
-= (UINTN
)(Targ
- mCoffFile
) & 0xfff;
1352 if (Offset
< -0x100000 || Offset
> 0xfffff) {
1353 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s due to its size (> 1 MB), this module requires 4 KB section alignment.",
1358 // Re-encode the offset as an ADR instruction
1359 *(UINT32
*)Targ
&= 0x1000001f;
1360 *(UINT32
*)Targ
|= ((Offset
& 0x1ffffc) << (5 - 2)) | ((Offset
& 0x3) << 29);
1364 case R_AARCH64_ADD_ABS_LO12_NC
:
1365 case R_AARCH64_LDST8_ABS_LO12_NC
:
1366 case R_AARCH64_LDST16_ABS_LO12_NC
:
1367 case R_AARCH64_LDST32_ABS_LO12_NC
:
1368 case R_AARCH64_LDST64_ABS_LO12_NC
:
1369 case R_AARCH64_LDST128_ABS_LO12_NC
:
1370 if (((SecShdr
->sh_addr
^ SecOffset
) & 0xfff) != 0 ||
1371 ((SymShdr
->sh_addr
^ mCoffSectionsOffset
[Sym
->st_shndx
]) & 0xfff) != 0) {
1372 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 small code model requires identical ELF and PE/COFF section offsets modulo 4 KB.",
1378 case R_AARCH64_ADR_PREL_LO21
:
1379 case R_AARCH64_CONDBR19
:
1380 case R_AARCH64_LD_PREL_LO19
:
1381 case R_AARCH64_CALL26
:
1382 case R_AARCH64_JUMP26
:
1383 case R_AARCH64_PREL64
:
1384 case R_AARCH64_PREL32
:
1385 case R_AARCH64_PREL16
:
1387 // The GCC toolchains (i.e., binutils) may corrupt section relative
1388 // relocations when emitting relocation sections into fully linked
1389 // binaries. More specifically, they tend to fail to take into
1390 // account the fact that a '.rodata + XXX' relocation needs to have
1391 // its addend recalculated once .rodata is merged into the .text
1392 // section, and the relocation emitted into the .rela.text section.
1394 // We cannot really recover from this loss of information, so the
1395 // only workaround is to prevent having to recalculate any relative
1396 // relocations at all, by using a linker script that ensures that
1397 // the offset between the Place and the Symbol is the same in both
1398 // the ELF and the PE/COFF versions of the binary.
1400 if ((SymShdr
->sh_addr
- SecShdr
->sh_addr
) !=
1401 (mCoffSectionsOffset
[Sym
->st_shndx
] - SecOffset
)) {
1402 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 relative relocations require identical ELF and PE/COFF section offsets",
1407 // Absolute relocations.
1408 case R_AARCH64_ABS64
:
1409 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
1413 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1415 } else if (mEhdr
->e_machine
== EM_RISCV64
) {
1417 // Write section for RISC-V 64 architecture.
1419 WriteSectionRiscV64 (Rel
, Targ
, SymShdr
, Sym
);
1421 Error (NULL
, 0, 3000, "Invalid", "Not a supported machine type");
1432 WriteRelocations64 (
1437 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1438 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
1439 UINT32 RiscVRelType
;
1441 for (Index
= 0; Index
< mEhdr
->e_shnum
; Index
++) {
1442 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
1443 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
1444 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
1445 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
1448 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
1449 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
1451 if (mEhdr
->e_machine
== EM_X86_64
) {
1452 switch (ELF_R_TYPE(Rel
->r_info
)) {
1455 case R_X86_64_PLT32
:
1456 case R_X86_64_GOTPCREL
:
1457 case R_X86_64_GOTPCRELX
:
1458 case R_X86_64_REX_GOTPCRELX
:
1461 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",
1462 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
1464 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1465 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1466 EFI_IMAGE_REL_BASED_DIR64
);
1469 // R_X86_64_32 and R_X86_64_32S are ELF64 relocations emitted when using
1470 // the SYSV X64 ABI small non-position-independent code model.
1471 // R_X86_64_32 is used for unsigned 32-bit immediates with a 32-bit operand
1472 // size. The value is either not extended, or zero-extended to 64 bits.
1473 // R_X86_64_32S is used for either signed 32-bit non-rip-relative displacements
1474 // or signed 32-bit immediates with a 64-bit operand size. The value is
1475 // sign-extended to 64 bits.
1476 // EFI_IMAGE_REL_BASED_HIGHLOW is a PE relocation that uses 32-bit arithmetic
1477 // for rebasing an image.
1478 // EFI PE binaries declare themselves EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE and
1479 // may load above 2GB. If an EFI PE binary with a converted R_X86_64_32S
1480 // relocation is loaded above 2GB, the value will get sign-extended to the
1481 // negative part of the 64-bit address space. The negative part of the 64-bit
1482 // address space is unmapped, so accessing such an address page-faults.
1483 // In order to support R_X86_64_32S, it is necessary to unset
1484 // EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE, and the EFI PE loader must implement
1485 // this flag and abstain from loading such a PE binary above 2GB.
1486 // Since this feature is not supported, support for R_X86_64_32S (and hence
1487 // the small non-position-independent code model) is disabled.
1489 // case R_X86_64_32S:
1491 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",
1492 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
1494 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1495 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1496 EFI_IMAGE_REL_BASED_HIGHLOW
);
1499 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1501 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
1503 switch (ELF_R_TYPE(Rel
->r_info
)) {
1504 case R_AARCH64_ADR_PREL_LO21
:
1505 case R_AARCH64_CONDBR19
:
1506 case R_AARCH64_LD_PREL_LO19
:
1507 case R_AARCH64_CALL26
:
1508 case R_AARCH64_JUMP26
:
1509 case R_AARCH64_PREL64
:
1510 case R_AARCH64_PREL32
:
1511 case R_AARCH64_PREL16
:
1512 case R_AARCH64_ADR_PREL_PG_HI21
:
1513 case R_AARCH64_ADD_ABS_LO12_NC
:
1514 case R_AARCH64_LDST8_ABS_LO12_NC
:
1515 case R_AARCH64_LDST16_ABS_LO12_NC
:
1516 case R_AARCH64_LDST32_ABS_LO12_NC
:
1517 case R_AARCH64_LDST64_ABS_LO12_NC
:
1518 case R_AARCH64_LDST128_ABS_LO12_NC
:
1519 case R_AARCH64_ADR_GOT_PAGE
:
1520 case R_AARCH64_LD64_GOT_LO12_NC
:
1522 // No fixups are required for relative relocations, provided that
1523 // the relative offsets between sections have been preserved in
1524 // the ELF to PE/COFF conversion. We have already asserted that
1525 // this is the case in WriteSections64 ().
1529 case R_AARCH64_ABS64
:
1531 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1532 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1533 EFI_IMAGE_REL_BASED_DIR64
);
1536 case R_AARCH64_ABS32
:
1538 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1539 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1540 EFI_IMAGE_REL_BASED_HIGHLOW
);
1544 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1546 } else if (mEhdr
->e_machine
== EM_RISCV64
) {
1547 RiscVRelType
= ELF_R_TYPE(Rel
->r_info
);
1548 switch (RiscVRelType
) {
1554 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1555 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1556 EFI_IMAGE_REL_BASED_HIGHLOW
);
1561 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1562 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1563 EFI_IMAGE_REL_BASED_DIR64
);
1568 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1569 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1570 EFI_IMAGE_REL_BASED_RISCV_HI20
);
1573 case R_RISCV_LO12_I
:
1575 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1576 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1577 EFI_IMAGE_REL_BASED_RISCV_LOW12I
);
1580 case R_RISCV_LO12_S
:
1582 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1583 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1584 EFI_IMAGE_REL_BASED_RISCV_LOW12S
);
1589 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1590 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1591 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1596 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1597 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1598 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1603 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1604 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1605 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1610 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1611 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1612 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1615 case R_RISCV_BRANCH
:
1617 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1618 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1619 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1624 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1625 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1626 EFI_IMAGE_REL_BASED_ABSOLUTE
);
1629 case R_RISCV_GPREL_I
:
1630 case R_RISCV_GPREL_S
:
1632 case R_RISCV_CALL_PLT
:
1633 case R_RISCV_RVC_BRANCH
:
1634 case R_RISCV_RVC_JUMP
:
1641 case R_RISCV_PCREL_HI20
:
1642 case R_RISCV_GOT_HI20
:
1643 case R_RISCV_PCREL_LO12_I
:
1644 case R_RISCV_PCREL_LO12_S
:
1648 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_RISCV64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1651 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
1654 if (mEhdr
->e_machine
== EM_X86_64
&& RelShdr
->sh_info
== mGOTShindex
) {
1656 // Tack relocations for GOT entries after other relocations for
1657 // the section the GOT is in, as it's usually found at the end
1658 // of the section. This is done in order to maintain Rva order
1659 // of Coff relocations.
1661 EmitGOTRelocations();
1667 if (mEhdr
->e_machine
== EM_X86_64
) {
1669 // This is a safety net just in case the GOT is in a section
1670 // with no other relocations and the first invocation of
1671 // EmitGOTRelocations() above was skipped. This invocation
1672 // does not maintain Rva order of Coff relocations.
1673 // At present, with a single text section, all references to
1674 // the GOT and the GOT itself reside in section .text, so
1675 // if there's a GOT at all, the first invocation above
1678 EmitGOTRelocations();
1681 // Pad by adding empty entries.
1683 while (mCoffOffset
& (mCoffAlignment
- 1)) {
1684 CoffAddFixupEntry(0);
1687 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1688 Dir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1689 Dir
->Size
= mCoffOffset
- mRelocOffset
;
1690 if (Dir
->Size
== 0) {
1691 // If no relocations, null out the directory entry and don't add the .reloc section
1692 Dir
->VirtualAddress
= 0;
1693 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
1695 Dir
->VirtualAddress
= mRelocOffset
;
1696 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
1697 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1698 | EFI_IMAGE_SCN_MEM_DISCARDABLE
1699 | EFI_IMAGE_SCN_MEM_READ
);
1710 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1711 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
1712 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
1713 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
1715 Len
= strlen(mInImageName
) + 1;
1717 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ mDebugOffset
);
1718 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
1719 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
1720 Dir
->RVA
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1721 Dir
->FileOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1723 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
1724 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
1725 strcpy ((char *)(Nb10
+ 1), mInImageName
);
1728 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1729 DataDir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
1730 DataDir
->VirtualAddress
= mDebugOffset
;
1731 DataDir
->Size
= sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1740 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1745 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1746 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
1755 if (mCoffSectionsOffset
!= NULL
) {
1756 free (mCoffSectionsOffset
);