4 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License which accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "WinNtInclude.h"
31 #include <Common/UefiBaseTypes.h>
32 #include <IndustryStandard/PeImage.h>
34 #include "PeCoffLib.h"
35 #include "EfiUtilityMsgs.h"
38 #include "ElfConvert.h"
39 #include "Elf64Convert.h"
50 SECTION_FILTER_TYPES FilterType
78 // Rename ELF32 strucutres to common names to help when porting to ELF64.
80 typedef Elf64_Shdr Elf_Shdr
;
81 typedef Elf64_Ehdr Elf_Ehdr
;
82 typedef Elf64_Rel Elf_Rel
;
83 typedef Elf64_Rela Elf_Rela
;
84 typedef Elf64_Sym Elf_Sym
;
85 typedef Elf64_Phdr Elf_Phdr
;
86 typedef Elf64_Dyn Elf_Dyn
;
87 #define ELFCLASS ELFCLASS64
88 #define ELF_R_TYPE(r) ELF64_R_TYPE(r)
89 #define ELF_R_SYM(r) ELF64_R_SYM(r)
92 // Well known ELF structures.
94 STATIC Elf_Ehdr
*mEhdr
;
95 STATIC Elf_Shdr
*mShdrBase
;
96 STATIC Elf_Phdr
*mPhdrBase
;
101 STATIC UINT32 mCoffAlignment
= 0x20;
104 // PE section alignment.
106 STATIC
const UINT16 mCoffNbrSections
= 4;
109 // ELF sections to offset in Coff file.
111 STATIC UINT32
*mCoffSectionsOffset
= NULL
;
114 // Offsets in COFF file
116 STATIC UINT32 mNtHdrOffset
;
117 STATIC UINT32 mTextOffset
;
118 STATIC UINT32 mDataOffset
;
119 STATIC UINT32 mHiiRsrcOffset
;
120 STATIC UINT32 mRelocOffset
;
121 STATIC UINT32 mDebugOffset
;
124 // Initialization Function
129 ELF_FUNCTION_TABLE
*ElfFunctions
133 // Initialize data pointer and structures.
135 VerboseMsg ("Set EHDR");
136 mEhdr
= (Elf_Ehdr
*) FileBuffer
;
139 // Check the ELF64 specific header information.
141 VerboseMsg ("Check ELF64 Header Information");
142 if (mEhdr
->e_ident
[EI_CLASS
] != ELFCLASS64
) {
143 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS64");
146 if (mEhdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
147 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
150 if ((mEhdr
->e_type
!= ET_EXEC
) && (mEhdr
->e_type
!= ET_DYN
)) {
151 Error (NULL
, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
154 if (!((mEhdr
->e_machine
== EM_X86_64
) || (mEhdr
->e_machine
== EM_AARCH64
))) {
155 Error (NULL
, 0, 3000, "Unsupported", "ELF e_machine not EM_X86_64 or EM_AARCH64");
158 if (mEhdr
->e_version
!= EV_CURRENT
) {
159 Error (NULL
, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr
->e_version
, EV_CURRENT
);
164 // Update section header pointers
166 VerboseMsg ("Update Header Pointers");
167 mShdrBase
= (Elf_Shdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_shoff
);
168 mPhdrBase
= (Elf_Phdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_phoff
);
171 // Create COFF Section offset buffer and zero.
173 VerboseMsg ("Create COFF Section Offset Buffer");
174 mCoffSectionsOffset
= (UINT32
*)malloc(mEhdr
->e_shnum
* sizeof (UINT32
));
175 memset(mCoffSectionsOffset
, 0, mEhdr
->e_shnum
* sizeof(UINT32
));
178 // Fill in function pointers.
180 VerboseMsg ("Fill in Function Pointers");
181 ElfFunctions
->ScanSections
= ScanSections64
;
182 ElfFunctions
->WriteSections
= WriteSections64
;
183 ElfFunctions
->WriteRelocations
= WriteRelocations64
;
184 ElfFunctions
->WriteDebug
= WriteDebug64
;
185 ElfFunctions
->SetImageSize
= SetImageSize64
;
186 ElfFunctions
->CleanUp
= CleanUp64
;
193 // Header by Index functions
201 if (Num
>= mEhdr
->e_shnum
) {
202 Error (NULL
, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num
);
206 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
215 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
224 return (Offset
+ 3) & ~3;
236 return (BOOLEAN
) ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
);
245 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
247 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
256 if (IsHiiRsrcShdr(Shdr
)) {
259 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
268 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
270 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_STRTAB_SECTION_NAME
) == 0);
280 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
281 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
282 if (IsStrtabShdr(shdr
)) {
295 if (Sym
->st_name
== 0) {
299 Elf_Shdr
*StrtabShdr
= FindStrtabShdr();
300 if (StrtabShdr
== NULL
) {
304 assert(Sym
->st_name
< StrtabShdr
->sh_size
);
306 UINT8
* StrtabContents
= (UINT8
*)mEhdr
+ StrtabShdr
->sh_offset
;
308 bool foundEnd
= false;
310 for (i
= Sym
->st_name
; (i
< StrtabShdr
->sh_size
) && !foundEnd
; i
++) {
311 foundEnd
= StrtabContents
[i
] == 0;
315 return StrtabContents
+ Sym
->st_name
;
319 // Elf functions interface implementation
329 EFI_IMAGE_DOS_HEADER
*DosHdr
;
330 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
333 BOOLEAN FoundSection
;
339 // Coff file start with a DOS header.
341 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
342 mNtHdrOffset
= mCoffOffset
;
343 switch (mEhdr
->e_machine
) {
347 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
350 VerboseMsg ("%s unknown e_machine type %hu. Assume X64", mInImageName
, mEhdr
->e_machine
);
351 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
355 mTableOffset
= mCoffOffset
;
356 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
359 // Set mCoffAlignment to the maximum alignment of the input sections
362 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
363 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
364 if (shdr
->sh_addralign
<= mCoffAlignment
) {
367 if (IsTextShdr(shdr
) || IsDataShdr(shdr
) || IsHiiRsrcShdr(shdr
)) {
368 mCoffAlignment
= (UINT32
)shdr
->sh_addralign
;
373 // Move the PE/COFF header right before the first section. This will help us
374 // save space when converting to TE.
376 if (mCoffAlignment
> mCoffOffset
) {
377 mNtHdrOffset
+= mCoffAlignment
- mCoffOffset
;
378 mTableOffset
+= mCoffAlignment
- mCoffOffset
;
379 mCoffOffset
= mCoffAlignment
;
383 // First text sections.
385 mCoffOffset
= CoffAlign(mCoffOffset
);
386 mTextOffset
= mCoffOffset
;
387 FoundSection
= FALSE
;
389 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
390 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
391 if (IsTextShdr(shdr
)) {
392 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
393 // the alignment field is valid
394 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
395 // if the section address is aligned we must align PE/COFF
396 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
398 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
402 /* Relocate entry. */
403 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
404 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
405 CoffEntry
= (UINT32
) (mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
);
409 // Set mTextOffset with the offset of the first '.text' section
412 mTextOffset
= mCoffOffset
;
416 mCoffSectionsOffset
[i
] = mCoffOffset
;
417 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
423 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
427 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
428 mCoffOffset
= CoffAlign(mCoffOffset
);
430 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
431 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
435 // Then data sections.
437 mDataOffset
= mCoffOffset
;
438 FoundSection
= FALSE
;
440 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
441 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
442 if (IsDataShdr(shdr
)) {
443 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
444 // the alignment field is valid
445 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
446 // if the section address is aligned we must align PE/COFF
447 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
449 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
454 // Set mDataOffset with the offset of the first '.data' section
457 mDataOffset
= mCoffOffset
;
460 mCoffSectionsOffset
[i
] = mCoffOffset
;
461 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
467 // Make room for .debug data in .data (or .text if .data is empty) instead of
468 // putting it in a section of its own. This is explicitly allowed by the
469 // PE/COFF spec, and prevents bloat in the binary when using large values for
470 // section alignment.
472 if (SectionCount
> 0) {
473 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
475 mCoffOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
) +
476 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) +
477 strlen(mInImageName
) + 1;
479 mCoffOffset
= CoffAlign(mCoffOffset
);
480 if (SectionCount
== 0) {
481 mDataOffset
= mCoffOffset
;
484 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
485 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
489 // The HII resource sections.
491 mHiiRsrcOffset
= mCoffOffset
;
492 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
493 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
494 if (IsHiiRsrcShdr(shdr
)) {
495 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
496 // the alignment field is valid
497 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
498 // if the section address is aligned we must align PE/COFF
499 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
501 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
504 if (shdr
->sh_size
!= 0) {
505 mHiiRsrcOffset
= mCoffOffset
;
506 mCoffSectionsOffset
[i
] = mCoffOffset
;
507 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
508 mCoffOffset
= CoffAlign(mCoffOffset
);
509 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
515 mRelocOffset
= mCoffOffset
;
518 // Allocate base Coff file. Will be expanded later for relocations.
520 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
521 memset(mCoffFile
, 0, mCoffOffset
);
526 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
527 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
528 DosHdr
->e_lfanew
= mNtHdrOffset
;
530 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
532 NtHdr
->Pe32Plus
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
534 switch (mEhdr
->e_machine
) {
536 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
537 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
540 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IPF
;
541 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
544 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_AARCH64
;
545 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
548 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN
)mEhdr
->e_machine
);
549 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
550 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
553 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
554 NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
555 mImageTimeStamp
= NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
;
556 NtHdr
->Pe32Plus
.FileHeader
.PointerToSymbolTable
= 0;
557 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSymbols
= 0;
558 NtHdr
->Pe32Plus
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32Plus
.OptionalHeader
);
559 NtHdr
->Pe32Plus
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
560 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
561 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
562 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE
;
564 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
565 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
566 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfUninitializedData
= 0;
567 NtHdr
->Pe32Plus
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
569 NtHdr
->Pe32Plus
.OptionalHeader
.BaseOfCode
= mTextOffset
;
571 NtHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= 0;
572 NtHdr
->Pe32Plus
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
573 NtHdr
->Pe32Plus
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
574 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= 0;
576 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
577 NtHdr
->Pe32Plus
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
582 if ((mDataOffset
- mTextOffset
) > 0) {
583 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
584 EFI_IMAGE_SCN_CNT_CODE
585 | EFI_IMAGE_SCN_MEM_EXECUTE
586 | EFI_IMAGE_SCN_MEM_READ
);
588 // Don't make a section of size 0.
589 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
592 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
593 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
594 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
595 | EFI_IMAGE_SCN_MEM_WRITE
596 | EFI_IMAGE_SCN_MEM_READ
);
598 // Don't make a section of size 0.
599 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
602 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
603 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
604 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
605 | EFI_IMAGE_SCN_MEM_READ
);
607 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
608 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
610 // Don't make a section of size 0.
611 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
619 SECTION_FILTER_TYPES FilterType
625 BOOLEAN (*Filter
)(Elf_Shdr
*);
628 // Initialize filter pointer
630 switch (FilterType
) {
635 Filter
= IsHiiRsrcShdr
;
645 // First: copy sections.
647 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
648 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
649 if ((*Filter
)(Shdr
)) {
650 switch (Shdr
->sh_type
) {
653 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
654 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
655 (size_t) Shdr
->sh_size
);
659 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, (size_t) Shdr
->sh_size
);
664 // Ignore for unkown section type.
666 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName
, (unsigned)Shdr
->sh_type
);
673 // Second: apply relocations.
675 VerboseMsg ("Applying Relocations...");
676 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
678 // Determine if this is a relocation section.
680 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
681 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
686 // Relocation section found. Now extract section information that the relocations
687 // apply to in the ELF data and the new COFF data.
689 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
690 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
693 // Only process relocations for the current filter type.
695 if (RelShdr
->sh_type
== SHT_RELA
&& (*Filter
)(SecShdr
)) {
699 // Determine the symbol table referenced by the relocation data.
701 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
702 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
705 // Process all relocation entries for this section.
707 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= (UINT32
) RelShdr
->sh_entsize
) {
710 // Set pointer to relocation entry
712 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
715 // Set pointer to symbol table entry associated with the relocation entry.
717 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
723 // Check section header index found in symbol table and get the section
726 if (Sym
->st_shndx
== SHN_UNDEF
727 || Sym
->st_shndx
>= mEhdr
->e_shnum
) {
728 const UINT8
*SymName
= GetSymName(Sym
);
729 if (SymName
== NULL
) {
730 SymName
= (const UINT8
*)"<unknown>";
733 Error (NULL
, 0, 3000, "Invalid",
734 "%s: Bad definition for symbol '%s'@%#llx or unsupported symbol type. "
735 "For example, absolute and undefined symbols are not supported.",
736 mInImageName
, SymName
, Sym
->st_value
);
740 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
743 // Convert the relocation data to a pointer into the coff file.
746 // r_offset is the virtual address of the storage unit to be relocated.
747 // sh_addr is the virtual address for the base of the section.
749 // r_offset in a memory address.
750 // Convert it to a pointer in the coff file.
752 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
755 // Determine how to handle each relocation type based on the machine type.
757 if (mEhdr
->e_machine
== EM_X86_64
) {
758 switch (ELF_R_TYPE(Rel
->r_info
)) {
763 // Absolute relocation.
765 VerboseMsg ("R_X86_64_64");
766 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
767 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
769 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
770 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
773 VerboseMsg ("R_X86_64_32");
774 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
775 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
777 *(UINT32
*)Targ
= (UINT32
)((UINT64
)(*(UINT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
778 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
781 VerboseMsg ("R_X86_64_32S");
782 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
783 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
785 *(INT32
*)Targ
= (INT32
)((INT64
)(*(INT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
786 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
791 // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is
792 // possible since we know all code symbol references resolve to
793 // definitions in the same module (UEFI has no shared libraries),
794 // and so there is never a reason to jump via a PLT entry,
795 // allowing us to resolve the reference using the symbol directly.
797 VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");
801 // Relative relocation: Symbol - Ip + Addend
803 VerboseMsg ("R_X86_64_PC32");
804 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
805 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
807 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
808 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
809 - (SecOffset
- SecShdr
->sh_addr
));
810 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
813 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
815 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
817 switch (ELF_R_TYPE(Rel
->r_info
)) {
819 case R_AARCH64_ADR_PREL_PG_HI21
:
821 // AArch64 PG_H21 relocations are typically paired with ABS_LO12
822 // relocations, where a PC-relative reference with +/- 4 GB range is
823 // split into a relative high part and an absolute low part. Since
824 // the absolute low part represents the offset into a 4 KB page, we
825 // either have to convert the ADRP into an ADR instruction, or we
826 // need to use a section alignment of at least 4 KB, so that the
827 // binary appears at a correct offset at runtime. In any case, we
828 // have to make sure that the 4 KB relative offsets of both the
829 // section containing the reference as well as the section to which
830 // it refers have not been changed during PE/COFF conversion (i.e.,
831 // in ScanSections64() above).
833 if (mCoffAlignment
< 0x1000) {
835 // Attempt to convert the ADRP into an ADR instruction.
836 // This is only possible if the symbol is within +/- 1 MB.
840 // Decode the ADRP instruction
841 Offset
= (INT32
)((*(UINT32
*)Targ
& 0xffffe0) << 8);
842 Offset
= (Offset
<< (6 - 5)) | ((*(UINT32
*)Targ
& 0x60000000) >> (29 - 12));
845 // ADRP offset is relative to the previous page boundary,
846 // whereas ADR offset is relative to the instruction itself.
847 // So fix up the offset so it points to the page containing
850 Offset
-= (UINTN
)(Targ
- mCoffFile
) & 0xfff;
852 if (Offset
< -0x100000 || Offset
> 0xfffff) {
853 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s due to its size (> 1 MB), this module requires 4 KB section alignment.",
858 // Re-encode the offset as an ADR instruction
859 *(UINT32
*)Targ
&= 0x1000001f;
860 *(UINT32
*)Targ
|= ((Offset
& 0x1ffffc) << (5 - 2)) | ((Offset
& 0x3) << 29);
864 case R_AARCH64_ADD_ABS_LO12_NC
:
865 case R_AARCH64_LDST8_ABS_LO12_NC
:
866 case R_AARCH64_LDST16_ABS_LO12_NC
:
867 case R_AARCH64_LDST32_ABS_LO12_NC
:
868 case R_AARCH64_LDST64_ABS_LO12_NC
:
869 case R_AARCH64_LDST128_ABS_LO12_NC
:
870 if (((SecShdr
->sh_addr
^ SecOffset
) & 0xfff) != 0 ||
871 ((SymShdr
->sh_addr
^ mCoffSectionsOffset
[Sym
->st_shndx
]) & 0xfff) != 0) {
872 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 small code model requires identical ELF and PE/COFF section offsets modulo 4 KB.",
878 case R_AARCH64_ADR_PREL_LO21
:
879 case R_AARCH64_CONDBR19
:
880 case R_AARCH64_LD_PREL_LO19
:
881 case R_AARCH64_CALL26
:
882 case R_AARCH64_JUMP26
:
883 case R_AARCH64_PREL64
:
884 case R_AARCH64_PREL32
:
885 case R_AARCH64_PREL16
:
887 // The GCC toolchains (i.e., binutils) may corrupt section relative
888 // relocations when emitting relocation sections into fully linked
889 // binaries. More specifically, they tend to fail to take into
890 // account the fact that a '.rodata + XXX' relocation needs to have
891 // its addend recalculated once .rodata is merged into the .text
892 // section, and the relocation emitted into the .rela.text section.
894 // We cannot really recover from this loss of information, so the
895 // only workaround is to prevent having to recalculate any relative
896 // relocations at all, by using a linker script that ensures that
897 // the offset between the Place and the Symbol is the same in both
898 // the ELF and the PE/COFF versions of the binary.
900 if ((SymShdr
->sh_addr
- SecShdr
->sh_addr
) !=
901 (mCoffSectionsOffset
[Sym
->st_shndx
] - SecOffset
)) {
902 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 relative relocations require identical ELF and PE/COFF section offsets",
907 // Absolute relocations.
908 case R_AARCH64_ABS64
:
909 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
913 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
916 Error (NULL
, 0, 3000, "Invalid", "Not a supported machine type");
932 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
933 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
935 for (Index
= 0; Index
< mEhdr
->e_shnum
; Index
++) {
936 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
937 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
938 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
939 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
942 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
943 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
945 if (mEhdr
->e_machine
== EM_X86_64
) {
946 switch (ELF_R_TYPE(Rel
->r_info
)) {
952 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",
953 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
955 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
956 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
957 EFI_IMAGE_REL_BASED_DIR64
);
961 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",
962 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
964 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
965 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
966 EFI_IMAGE_REL_BASED_HIGHLOW
);
969 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
971 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
973 switch (ELF_R_TYPE(Rel
->r_info
)) {
974 case R_AARCH64_ADR_PREL_LO21
:
975 case R_AARCH64_CONDBR19
:
976 case R_AARCH64_LD_PREL_LO19
:
977 case R_AARCH64_CALL26
:
978 case R_AARCH64_JUMP26
:
979 case R_AARCH64_PREL64
:
980 case R_AARCH64_PREL32
:
981 case R_AARCH64_PREL16
:
982 case R_AARCH64_ADR_PREL_PG_HI21
:
983 case R_AARCH64_ADD_ABS_LO12_NC
:
984 case R_AARCH64_LDST8_ABS_LO12_NC
:
985 case R_AARCH64_LDST16_ABS_LO12_NC
:
986 case R_AARCH64_LDST32_ABS_LO12_NC
:
987 case R_AARCH64_LDST64_ABS_LO12_NC
:
988 case R_AARCH64_LDST128_ABS_LO12_NC
:
990 // No fixups are required for relative relocations, provided that
991 // the relative offsets between sections have been preserved in
992 // the ELF to PE/COFF conversion. We have already asserted that
993 // this is the case in WriteSections64 ().
997 case R_AARCH64_ABS64
:
999 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1000 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1001 EFI_IMAGE_REL_BASED_DIR64
);
1004 case R_AARCH64_ABS32
:
1006 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
1007 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
1008 EFI_IMAGE_REL_BASED_HIGHLOW
);
1012 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
1015 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
1023 // Pad by adding empty entries.
1025 while (mCoffOffset
& (mCoffAlignment
- 1)) {
1026 CoffAddFixupEntry(0);
1029 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1030 Dir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1031 Dir
->Size
= mCoffOffset
- mRelocOffset
;
1032 if (Dir
->Size
== 0) {
1033 // If no relocations, null out the directory entry and don't add the .reloc section
1034 Dir
->VirtualAddress
= 0;
1035 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
1037 Dir
->VirtualAddress
= mRelocOffset
;
1038 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
1039 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1040 | EFI_IMAGE_SCN_MEM_DISCARDABLE
1041 | EFI_IMAGE_SCN_MEM_READ
);
1052 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1053 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
1054 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
1055 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
1057 Len
= strlen(mInImageName
) + 1;
1059 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ mDebugOffset
);
1060 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
1061 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
1062 Dir
->RVA
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1063 Dir
->FileOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1065 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
1066 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
1067 strcpy ((char *)(Nb10
+ 1), mInImageName
);
1070 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1071 DataDir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
1072 DataDir
->VirtualAddress
= mDebugOffset
;
1073 DataDir
->Size
= Dir
->SizeOfData
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1082 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1087 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1088 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
1097 if (mCoffSectionsOffset
!= NULL
) {
1098 free (mCoffSectionsOffset
);