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"
30 #include <Common/UefiBaseTypes.h>
31 #include <IndustryStandard/PeImage.h>
33 #include "PeCoffLib.h"
34 #include "EfiUtilityMsgs.h"
37 #include "ElfConvert.h"
38 #include "Elf64Convert.h"
49 SECTION_FILTER_TYPES FilterType
77 // Rename ELF32 strucutres to common names to help when porting to ELF64.
79 typedef Elf64_Shdr Elf_Shdr
;
80 typedef Elf64_Ehdr Elf_Ehdr
;
81 typedef Elf64_Rel Elf_Rel
;
82 typedef Elf64_Rela Elf_Rela
;
83 typedef Elf64_Sym Elf_Sym
;
84 typedef Elf64_Phdr Elf_Phdr
;
85 typedef Elf64_Dyn Elf_Dyn
;
86 #define ELFCLASS ELFCLASS64
87 #define ELF_R_TYPE(r) ELF64_R_TYPE(r)
88 #define ELF_R_SYM(r) ELF64_R_SYM(r)
91 // Well known ELF structures.
93 STATIC Elf_Ehdr
*mEhdr
;
94 STATIC Elf_Shdr
*mShdrBase
;
95 STATIC Elf_Phdr
*mPhdrBase
;
100 STATIC UINT32 mCoffAlignment
= 0x20;
103 // PE section alignment.
105 STATIC
const UINT16 mCoffNbrSections
= 4;
108 // ELF sections to offset in Coff file.
110 STATIC UINT32
*mCoffSectionsOffset
= NULL
;
113 // Offsets in COFF file
115 STATIC UINT32 mNtHdrOffset
;
116 STATIC UINT32 mTextOffset
;
117 STATIC UINT32 mDataOffset
;
118 STATIC UINT32 mHiiRsrcOffset
;
119 STATIC UINT32 mRelocOffset
;
120 STATIC UINT32 mDebugOffset
;
123 // Initialization Function
128 ELF_FUNCTION_TABLE
*ElfFunctions
132 // Initialize data pointer and structures.
134 VerboseMsg ("Set EHDR");
135 mEhdr
= (Elf_Ehdr
*) FileBuffer
;
138 // Check the ELF64 specific header information.
140 VerboseMsg ("Check ELF64 Header Information");
141 if (mEhdr
->e_ident
[EI_CLASS
] != ELFCLASS64
) {
142 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS64");
145 if (mEhdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
146 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
149 if ((mEhdr
->e_type
!= ET_EXEC
) && (mEhdr
->e_type
!= ET_DYN
)) {
150 Error (NULL
, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
153 if (!((mEhdr
->e_machine
== EM_X86_64
) || (mEhdr
->e_machine
== EM_AARCH64
))) {
154 Error (NULL
, 0, 3000, "Unsupported", "ELF e_machine not EM_X86_64 or EM_AARCH64");
157 if (mEhdr
->e_version
!= EV_CURRENT
) {
158 Error (NULL
, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr
->e_version
, EV_CURRENT
);
163 // Update section header pointers
165 VerboseMsg ("Update Header Pointers");
166 mShdrBase
= (Elf_Shdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_shoff
);
167 mPhdrBase
= (Elf_Phdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_phoff
);
170 // Create COFF Section offset buffer and zero.
172 VerboseMsg ("Create COFF Section Offset Buffer");
173 mCoffSectionsOffset
= (UINT32
*)malloc(mEhdr
->e_shnum
* sizeof (UINT32
));
174 memset(mCoffSectionsOffset
, 0, mEhdr
->e_shnum
* sizeof(UINT32
));
177 // Fill in function pointers.
179 VerboseMsg ("Fill in Function Pointers");
180 ElfFunctions
->ScanSections
= ScanSections64
;
181 ElfFunctions
->WriteSections
= WriteSections64
;
182 ElfFunctions
->WriteRelocations
= WriteRelocations64
;
183 ElfFunctions
->WriteDebug
= WriteDebug64
;
184 ElfFunctions
->SetImageSize
= SetImageSize64
;
185 ElfFunctions
->CleanUp
= CleanUp64
;
192 // Header by Index functions
200 if (Num
>= mEhdr
->e_shnum
)
202 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
211 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
223 return (BOOLEAN
) ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
);
232 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
234 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
243 if (IsHiiRsrcShdr(Shdr
)) {
246 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
250 // Elf functions interface implementation
260 EFI_IMAGE_DOS_HEADER
*DosHdr
;
261 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
264 BOOLEAN FoundSection
;
270 // Coff file start with a DOS header.
272 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
273 mNtHdrOffset
= mCoffOffset
;
274 switch (mEhdr
->e_machine
) {
278 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
281 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN
)mEhdr
->e_machine
);
282 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
286 mTableOffset
= mCoffOffset
;
287 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
290 // Set mCoffAlignment to the maximum alignment of the input sections
293 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
294 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
295 if (shdr
->sh_addralign
<= mCoffAlignment
) {
298 if (IsTextShdr(shdr
) || IsDataShdr(shdr
) || IsHiiRsrcShdr(shdr
)) {
299 mCoffAlignment
= (UINT32
)shdr
->sh_addralign
;
304 // Move the PE/COFF header right before the first section. This will help us
305 // save space when converting to TE.
307 if (mCoffAlignment
> mCoffOffset
) {
308 mNtHdrOffset
+= mCoffAlignment
- mCoffOffset
;
309 mTableOffset
+= mCoffAlignment
- mCoffOffset
;
310 mCoffOffset
= mCoffAlignment
;
314 // First text sections.
316 mCoffOffset
= CoffAlign(mCoffOffset
);
317 mTextOffset
= mCoffOffset
;
318 FoundSection
= FALSE
;
320 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
321 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
322 if (IsTextShdr(shdr
)) {
323 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
324 // the alignment field is valid
325 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
326 // if the section address is aligned we must align PE/COFF
327 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
328 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
329 // ARM RVCT tools have behavior outside of the ELF specification to try
330 // and make images smaller. If sh_addr is not aligned to sh_addralign
331 // then the section needs to preserve sh_addr MOD sh_addralign.
332 // Normally doing nothing here works great.
333 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
337 /* Relocate entry. */
338 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
339 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
340 CoffEntry
= (UINT32
) (mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
);
344 // Set mTextOffset with the offset of the first '.text' section
347 mTextOffset
= mCoffOffset
;
351 mCoffSectionsOffset
[i
] = mCoffOffset
;
352 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
358 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
362 mDebugOffset
= mCoffOffset
;
364 if (mEhdr
->e_machine
!= EM_ARM
) {
365 mCoffOffset
= CoffAlign(mCoffOffset
);
368 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
369 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
373 // Then data sections.
375 mDataOffset
= mCoffOffset
;
376 FoundSection
= FALSE
;
378 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
379 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
380 if (IsDataShdr(shdr
)) {
381 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
382 // the alignment field is valid
383 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
384 // if the section address is aligned we must align PE/COFF
385 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
386 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
387 // ARM RVCT tools have behavior outside of the ELF specification to try
388 // and make images smaller. If sh_addr is not aligned to sh_addralign
389 // then the section needs to preserve sh_addr MOD sh_addralign.
390 // Normally doing nothing here works great.
391 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
396 // Set mDataOffset with the offset of the first '.data' section
399 mDataOffset
= mCoffOffset
;
402 mCoffSectionsOffset
[i
] = mCoffOffset
;
403 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
409 // Make room for .debug data in .data (or .text if .data is empty) instead of
410 // putting it in a section of its own. This is explicitly allowed by the
411 // PE/COFF spec, and prevents bloat in the binary when using large values for
412 // section alignment.
414 if (SectionCount
> 0) {
415 mDebugOffset
= mCoffOffset
;
417 mCoffOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
) +
418 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) +
419 strlen(mInImageName
) + 1;
421 mCoffOffset
= CoffAlign(mCoffOffset
);
422 if (SectionCount
== 0) {
423 mDataOffset
= mCoffOffset
;
426 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
427 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
431 // The HII resource sections.
433 mHiiRsrcOffset
= mCoffOffset
;
434 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
435 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
436 if (IsHiiRsrcShdr(shdr
)) {
437 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
438 // the alignment field is valid
439 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
440 // if the section address is aligned we must align PE/COFF
441 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
442 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
443 // ARM RVCT tools have behavior outside of the ELF specification to try
444 // and make images smaller. If sh_addr is not aligned to sh_addralign
445 // then the section needs to preserve sh_addr MOD sh_addralign.
446 // Normally doing nothing here works great.
447 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
450 if (shdr
->sh_size
!= 0) {
451 mHiiRsrcOffset
= mCoffOffset
;
452 mCoffSectionsOffset
[i
] = mCoffOffset
;
453 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
454 mCoffOffset
= CoffAlign(mCoffOffset
);
455 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
461 mRelocOffset
= mCoffOffset
;
464 // Allocate base Coff file. Will be expanded later for relocations.
466 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
467 memset(mCoffFile
, 0, mCoffOffset
);
472 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
473 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
474 DosHdr
->e_lfanew
= mNtHdrOffset
;
476 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
478 NtHdr
->Pe32Plus
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
480 switch (mEhdr
->e_machine
) {
482 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
483 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
486 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IPF
;
487 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
490 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_AARCH64
;
491 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
494 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN
)mEhdr
->e_machine
);
495 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
496 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
499 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
500 NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
501 mImageTimeStamp
= NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
;
502 NtHdr
->Pe32Plus
.FileHeader
.PointerToSymbolTable
= 0;
503 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSymbols
= 0;
504 NtHdr
->Pe32Plus
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32Plus
.OptionalHeader
);
505 NtHdr
->Pe32Plus
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
506 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
507 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
508 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE
;
510 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
511 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
512 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfUninitializedData
= 0;
513 NtHdr
->Pe32Plus
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
515 NtHdr
->Pe32Plus
.OptionalHeader
.BaseOfCode
= mTextOffset
;
517 NtHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= 0;
518 NtHdr
->Pe32Plus
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
519 NtHdr
->Pe32Plus
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
520 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= 0;
522 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
523 NtHdr
->Pe32Plus
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
528 if ((mDataOffset
- mTextOffset
) > 0) {
529 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
530 EFI_IMAGE_SCN_CNT_CODE
531 | EFI_IMAGE_SCN_MEM_EXECUTE
532 | EFI_IMAGE_SCN_MEM_READ
);
534 // Don't make a section of size 0.
535 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
538 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
539 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
540 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
541 | EFI_IMAGE_SCN_MEM_WRITE
542 | EFI_IMAGE_SCN_MEM_READ
);
544 // Don't make a section of size 0.
545 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
548 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
549 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
550 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
551 | EFI_IMAGE_SCN_MEM_READ
);
553 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
554 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
556 // Don't make a section of size 0.
557 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
565 SECTION_FILTER_TYPES FilterType
571 BOOLEAN (*Filter
)(Elf_Shdr
*);
574 // Initialize filter pointer
576 switch (FilterType
) {
581 Filter
= IsHiiRsrcShdr
;
591 // First: copy sections.
593 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
594 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
595 if ((*Filter
)(Shdr
)) {
596 switch (Shdr
->sh_type
) {
599 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
600 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
601 (size_t) Shdr
->sh_size
);
605 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, (size_t) Shdr
->sh_size
);
610 // Ignore for unkown section type.
612 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName
, (unsigned)Shdr
->sh_type
);
619 // Second: apply relocations.
621 VerboseMsg ("Applying Relocations...");
622 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
624 // Determine if this is a relocation section.
626 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
627 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
632 // Relocation section found. Now extract section information that the relocations
633 // apply to in the ELF data and the new COFF data.
635 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
636 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
639 // Only process relocations for the current filter type.
641 if (RelShdr
->sh_type
== SHT_RELA
&& (*Filter
)(SecShdr
)) {
645 // Determine the symbol table referenced by the relocation data.
647 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
648 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
651 // Process all relocation entries for this section.
653 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= (UINT32
) RelShdr
->sh_entsize
) {
656 // Set pointer to relocation entry
658 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
661 // Set pointer to symbol table entry associated with the relocation entry.
663 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
669 // Check section header index found in symbol table and get the section
672 if (Sym
->st_shndx
== SHN_UNDEF
673 || Sym
->st_shndx
== SHN_ABS
674 || Sym
->st_shndx
> mEhdr
->e_shnum
) {
675 Error (NULL
, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName
);
677 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
680 // Convert the relocation data to a pointer into the coff file.
683 // r_offset is the virtual address of the storage unit to be relocated.
684 // sh_addr is the virtual address for the base of the section.
686 // r_offset in a memory address.
687 // Convert it to a pointer in the coff file.
689 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
692 // Determine how to handle each relocation type based on the machine type.
694 if (mEhdr
->e_machine
== EM_X86_64
) {
695 switch (ELF_R_TYPE(Rel
->r_info
)) {
700 // Absolute relocation.
702 VerboseMsg ("R_X86_64_64");
703 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
704 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
706 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
707 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
710 VerboseMsg ("R_X86_64_32");
711 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
712 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
714 *(UINT32
*)Targ
= (UINT32
)((UINT64
)(*(UINT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
715 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
718 VerboseMsg ("R_X86_64_32S");
719 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
720 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
722 *(INT32
*)Targ
= (INT32
)((INT64
)(*(INT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
723 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
727 // Relative relocation: Symbol - Ip + Addend
729 VerboseMsg ("R_X86_64_PC32");
730 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
731 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
733 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
734 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
735 - (SecOffset
- SecShdr
->sh_addr
));
736 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
739 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
741 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
743 switch (ELF_R_TYPE(Rel
->r_info
)) {
745 case R_AARCH64_ADR_PREL_PG_HI21
:
746 case R_AARCH64_ADD_ABS_LO12_NC
:
747 case R_AARCH64_LDST8_ABS_LO12_NC
:
748 case R_AARCH64_LDST16_ABS_LO12_NC
:
749 case R_AARCH64_LDST32_ABS_LO12_NC
:
750 case R_AARCH64_LDST64_ABS_LO12_NC
:
751 case R_AARCH64_LDST128_ABS_LO12_NC
:
753 // AArch64 PG_H21 relocations are typically paired with ABS_LO12
754 // relocations, where a PC-relative reference with +/- 4 GB range is
755 // split into a relative high part and an absolute low part. Since
756 // the absolute low part represents the offset into a 4 KB page, we
757 // have to make sure that the 4 KB relative offsets of both the
758 // section containing the reference as well as the section to which
759 // it refers have not been changed during PE/COFF conversion (i.e.,
760 // in ScanSections64() above).
762 if (((SecShdr
->sh_addr
^ SecOffset
) & 0xfff) != 0 ||
763 ((SymShdr
->sh_addr
^ mCoffSectionsOffset
[Sym
->st_shndx
]) & 0xfff) != 0 ||
764 mCoffAlignment
< 0x1000) {
765 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 small code model requires 4 KB section alignment.",
771 case R_AARCH64_ADR_PREL_LO21
:
772 case R_AARCH64_CONDBR19
:
773 case R_AARCH64_LD_PREL_LO19
:
774 case R_AARCH64_CALL26
:
775 case R_AARCH64_JUMP26
:
777 // The GCC toolchains (i.e., binutils) may corrupt section relative
778 // relocations when emitting relocation sections into fully linked
779 // binaries. More specifically, they tend to fail to take into
780 // account the fact that a '.rodata + XXX' relocation needs to have
781 // its addend recalculated once .rodata is merged into the .text
782 // section, and the relocation emitted into the .rela.text section.
784 // We cannot really recover from this loss of information, so the
785 // only workaround is to prevent having to recalculate any relative
786 // relocations at all, by using a linker script that ensures that
787 // the offset between the Place and the Symbol is the same in both
788 // the ELF and the PE/COFF versions of the binary.
790 if ((SymShdr
->sh_addr
- SecShdr
->sh_addr
) !=
791 (mCoffSectionsOffset
[Sym
->st_shndx
] - SecOffset
)) {
792 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 relative relocations require identical ELF and PE/COFF section offsets",
797 // Absolute relocations.
798 case R_AARCH64_ABS64
:
799 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
803 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
806 Error (NULL
, 0, 3000, "Invalid", "Not a supported machine type");
822 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
823 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
825 for (Index
= 0; Index
< mEhdr
->e_shnum
; Index
++) {
826 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
827 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
828 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
829 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
832 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
833 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
835 if (mEhdr
->e_machine
== EM_X86_64
) {
836 switch (ELF_R_TYPE(Rel
->r_info
)) {
841 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",
842 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
844 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
845 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
846 EFI_IMAGE_REL_BASED_DIR64
);
850 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",
851 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
853 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
854 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
855 EFI_IMAGE_REL_BASED_HIGHLOW
);
858 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
860 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
862 switch (ELF_R_TYPE(Rel
->r_info
)) {
863 case R_AARCH64_ADR_PREL_LO21
:
866 case R_AARCH64_CONDBR19
:
869 case R_AARCH64_LD_PREL_LO19
:
872 case R_AARCH64_CALL26
:
875 case R_AARCH64_JUMP26
:
878 case R_AARCH64_ADR_PREL_PG_HI21
:
879 case R_AARCH64_ADD_ABS_LO12_NC
:
880 case R_AARCH64_LDST8_ABS_LO12_NC
:
881 case R_AARCH64_LDST16_ABS_LO12_NC
:
882 case R_AARCH64_LDST32_ABS_LO12_NC
:
883 case R_AARCH64_LDST64_ABS_LO12_NC
:
884 case R_AARCH64_LDST128_ABS_LO12_NC
:
887 case R_AARCH64_ABS64
:
889 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
890 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
891 EFI_IMAGE_REL_BASED_DIR64
);
894 case R_AARCH64_ABS32
:
896 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
897 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
898 EFI_IMAGE_REL_BASED_HIGHLOW
);
902 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
905 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
913 // Pad by adding empty entries.
915 while (mCoffOffset
& (mCoffAlignment
- 1)) {
916 CoffAddFixupEntry(0);
919 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
920 Dir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
921 Dir
->Size
= mCoffOffset
- mRelocOffset
;
922 if (Dir
->Size
== 0) {
923 // If no relocations, null out the directory entry and don't add the .reloc section
924 Dir
->VirtualAddress
= 0;
925 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
927 Dir
->VirtualAddress
= mRelocOffset
;
928 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
929 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
930 | EFI_IMAGE_SCN_MEM_DISCARDABLE
931 | EFI_IMAGE_SCN_MEM_READ
);
942 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
943 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
944 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
945 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
947 Len
= strlen(mInImageName
) + 1;
949 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ mDebugOffset
);
950 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
951 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
952 Dir
->RVA
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
953 Dir
->FileOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
955 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
956 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
957 strcpy ((char *)(Nb10
+ 1), mInImageName
);
960 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
961 DataDir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
962 DataDir
->VirtualAddress
= mDebugOffset
;
963 DataDir
->Size
= Dir
->SizeOfData
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
972 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
977 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
978 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
987 if (mCoffSectionsOffset
!= NULL
) {
988 free (mCoffSectionsOffset
);