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
const UINT32 mCoffAlignment
= 0x20;
103 // PE section alignment.
105 STATIC
const UINT16 mCoffNbrSections
= 5;
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
;
122 // Initialization Function
127 ELF_FUNCTION_TABLE
*ElfFunctions
131 // Initialize data pointer and structures.
133 VerboseMsg ("Set EHDR");
134 mEhdr
= (Elf_Ehdr
*) FileBuffer
;
137 // Check the ELF64 specific header information.
139 VerboseMsg ("Check ELF64 Header Information");
140 if (mEhdr
->e_ident
[EI_CLASS
] != ELFCLASS64
) {
141 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS64");
144 if (mEhdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
145 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
148 if ((mEhdr
->e_type
!= ET_EXEC
) && (mEhdr
->e_type
!= ET_DYN
)) {
149 Error (NULL
, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
152 if (!((mEhdr
->e_machine
== EM_X86_64
) || (mEhdr
->e_machine
== EM_AARCH64
))) {
153 Error (NULL
, 0, 3000, "Unsupported", "ELF e_machine not EM_X86_64 or EM_AARCH64");
156 if (mEhdr
->e_version
!= EV_CURRENT
) {
157 Error (NULL
, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr
->e_version
, EV_CURRENT
);
162 // Update section header pointers
164 VerboseMsg ("Update Header Pointers");
165 mShdrBase
= (Elf_Shdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_shoff
);
166 mPhdrBase
= (Elf_Phdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_phoff
);
169 // Create COFF Section offset buffer and zero.
171 VerboseMsg ("Create COFF Section Offset Buffer");
172 mCoffSectionsOffset
= (UINT32
*)malloc(mEhdr
->e_shnum
* sizeof (UINT32
));
173 memset(mCoffSectionsOffset
, 0, mEhdr
->e_shnum
* sizeof(UINT32
));
176 // Fill in function pointers.
178 VerboseMsg ("Fill in Function Pointers");
179 ElfFunctions
->ScanSections
= ScanSections64
;
180 ElfFunctions
->WriteSections
= WriteSections64
;
181 ElfFunctions
->WriteRelocations
= WriteRelocations64
;
182 ElfFunctions
->WriteDebug
= WriteDebug64
;
183 ElfFunctions
->SetImageSize
= SetImageSize64
;
184 ElfFunctions
->CleanUp
= CleanUp64
;
191 // Header by Index functions
199 if (Num
>= mEhdr
->e_shnum
)
201 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
210 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
222 return (BOOLEAN
) ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
);
231 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
233 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
242 if (IsHiiRsrcShdr(Shdr
)) {
245 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
249 // Elf functions interface implementation
259 EFI_IMAGE_DOS_HEADER
*DosHdr
;
260 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
263 BOOLEAN FoundSection
;
269 // Coff file start with a DOS header.
271 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
272 mNtHdrOffset
= mCoffOffset
;
273 switch (mEhdr
->e_machine
) {
277 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
280 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN
)mEhdr
->e_machine
);
281 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
285 mTableOffset
= mCoffOffset
;
286 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
289 // First text sections.
291 mCoffOffset
= CoffAlign(mCoffOffset
);
292 mTextOffset
= mCoffOffset
;
293 FoundSection
= FALSE
;
295 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
296 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
297 if (IsTextShdr(shdr
)) {
298 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
299 // the alignment field is valid
300 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
301 // if the section address is aligned we must align PE/COFF
302 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
303 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
304 // ARM RVCT tools have behavior outside of the ELF specification to try
305 // and make images smaller. If sh_addr is not aligned to sh_addralign
306 // then the section needs to preserve sh_addr MOD sh_addralign.
307 // Normally doing nothing here works great.
308 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
312 /* Relocate entry. */
313 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
314 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
315 CoffEntry
= (UINT32
) (mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
);
319 // Set mTextOffset with the offset of the first '.text' section
322 mTextOffset
= mCoffOffset
;
326 mCoffSectionsOffset
[i
] = mCoffOffset
;
327 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
333 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
337 if (mEhdr
->e_machine
!= EM_ARM
) {
338 mCoffOffset
= CoffAlign(mCoffOffset
);
341 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
342 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
346 // Then data sections.
348 mDataOffset
= mCoffOffset
;
349 FoundSection
= FALSE
;
351 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
352 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
353 if (IsDataShdr(shdr
)) {
354 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
355 // the alignment field is valid
356 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
357 // if the section address is aligned we must align PE/COFF
358 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
359 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
360 // ARM RVCT tools have behavior outside of the ELF specification to try
361 // and make images smaller. If sh_addr is not aligned to sh_addralign
362 // then the section needs to preserve sh_addr MOD sh_addralign.
363 // Normally doing nothing here works great.
364 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
369 // Set mDataOffset with the offset of the first '.data' section
372 mDataOffset
= mCoffOffset
;
375 mCoffSectionsOffset
[i
] = mCoffOffset
;
376 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
380 mCoffOffset
= CoffAlign(mCoffOffset
);
382 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
383 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
387 // The HII resource sections.
389 mHiiRsrcOffset
= mCoffOffset
;
390 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
391 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
392 if (IsHiiRsrcShdr(shdr
)) {
393 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
394 // the alignment field is valid
395 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
396 // if the section address is aligned we must align PE/COFF
397 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
398 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
399 // ARM RVCT tools have behavior outside of the ELF specification to try
400 // and make images smaller. If sh_addr is not aligned to sh_addralign
401 // then the section needs to preserve sh_addr MOD sh_addralign.
402 // Normally doing nothing here works great.
403 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
406 if (shdr
->sh_size
!= 0) {
407 mHiiRsrcOffset
= mCoffOffset
;
408 mCoffSectionsOffset
[i
] = mCoffOffset
;
409 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
410 mCoffOffset
= CoffAlign(mCoffOffset
);
411 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
417 mRelocOffset
= mCoffOffset
;
420 // Allocate base Coff file. Will be expanded later for relocations.
422 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
423 memset(mCoffFile
, 0, mCoffOffset
);
428 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
429 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
430 DosHdr
->e_lfanew
= mNtHdrOffset
;
432 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
434 NtHdr
->Pe32Plus
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
436 switch (mEhdr
->e_machine
) {
438 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
439 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
442 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IPF
;
443 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
446 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_AARCH64
;
447 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
450 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN
)mEhdr
->e_machine
);
451 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
452 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
455 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
456 NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
457 mImageTimeStamp
= NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
;
458 NtHdr
->Pe32Plus
.FileHeader
.PointerToSymbolTable
= 0;
459 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSymbols
= 0;
460 NtHdr
->Pe32Plus
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32Plus
.OptionalHeader
);
461 NtHdr
->Pe32Plus
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
462 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
463 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
464 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE
;
466 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
467 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
468 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfUninitializedData
= 0;
469 NtHdr
->Pe32Plus
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
471 NtHdr
->Pe32Plus
.OptionalHeader
.BaseOfCode
= mTextOffset
;
473 NtHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= 0;
474 NtHdr
->Pe32Plus
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
475 NtHdr
->Pe32Plus
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
476 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= 0;
478 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
479 NtHdr
->Pe32Plus
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
484 if ((mDataOffset
- mTextOffset
) > 0) {
485 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
486 EFI_IMAGE_SCN_CNT_CODE
487 | EFI_IMAGE_SCN_MEM_EXECUTE
488 | EFI_IMAGE_SCN_MEM_READ
);
490 // Don't make a section of size 0.
491 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
494 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
495 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
496 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
497 | EFI_IMAGE_SCN_MEM_WRITE
498 | EFI_IMAGE_SCN_MEM_READ
);
500 // Don't make a section of size 0.
501 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
504 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
505 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
506 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
507 | EFI_IMAGE_SCN_MEM_READ
);
509 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
510 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
512 // Don't make a section of size 0.
513 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
521 SECTION_FILTER_TYPES FilterType
527 BOOLEAN (*Filter
)(Elf_Shdr
*);
530 // Initialize filter pointer
532 switch (FilterType
) {
537 Filter
= IsHiiRsrcShdr
;
547 // First: copy sections.
549 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
550 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
551 if ((*Filter
)(Shdr
)) {
552 switch (Shdr
->sh_type
) {
555 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
556 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
557 (size_t) Shdr
->sh_size
);
561 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, (size_t) Shdr
->sh_size
);
566 // Ignore for unkown section type.
568 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName
, (unsigned)Shdr
->sh_type
);
575 // Second: apply relocations.
577 VerboseMsg ("Applying Relocations...");
578 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
580 // Determine if this is a relocation section.
582 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
583 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
588 // Relocation section found. Now extract section information that the relocations
589 // apply to in the ELF data and the new COFF data.
591 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
592 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
595 // Only process relocations for the current filter type.
597 if (RelShdr
->sh_type
== SHT_RELA
&& (*Filter
)(SecShdr
)) {
601 // Determine the symbol table referenced by the relocation data.
603 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
604 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
607 // Process all relocation entries for this section.
609 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= (UINT32
) RelShdr
->sh_entsize
) {
612 // Set pointer to relocation entry
614 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
617 // Set pointer to symbol table entry associated with the relocation entry.
619 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
625 // Check section header index found in symbol table and get the section
628 if (Sym
->st_shndx
== SHN_UNDEF
629 || Sym
->st_shndx
== SHN_ABS
630 || Sym
->st_shndx
> mEhdr
->e_shnum
) {
631 Error (NULL
, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName
);
633 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
636 // Convert the relocation data to a pointer into the coff file.
639 // r_offset is the virtual address of the storage unit to be relocated.
640 // sh_addr is the virtual address for the base of the section.
642 // r_offset in a memory address.
643 // Convert it to a pointer in the coff file.
645 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
648 // Determine how to handle each relocation type based on the machine type.
650 if (mEhdr
->e_machine
== EM_X86_64
) {
651 switch (ELF_R_TYPE(Rel
->r_info
)) {
656 // Absolute relocation.
658 VerboseMsg ("R_X86_64_64");
659 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
660 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
662 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
663 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
666 VerboseMsg ("R_X86_64_32");
667 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
668 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
670 *(UINT32
*)Targ
= (UINT32
)((UINT64
)(*(UINT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
671 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
674 VerboseMsg ("R_X86_64_32S");
675 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
676 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
678 *(INT32
*)Targ
= (INT32
)((INT64
)(*(INT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
679 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
683 // Relative relocation: Symbol - Ip + Addend
685 VerboseMsg ("R_X86_64_PC32");
686 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
687 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
689 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
690 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
691 - (SecOffset
- SecShdr
->sh_addr
));
692 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
695 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
697 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
699 // AARCH64 GCC uses RELA relocation, so all relocations have to be fixed up.
700 // As opposed to ARM32 using REL.
702 switch (ELF_R_TYPE(Rel
->r_info
)) {
704 case R_AARCH64_ADR_PREL_LO21
:
705 if (Rel
->r_addend
!= 0 ) { /* TODO */
706 Error (NULL
, 0, 3000, "Invalid", "AArch64: R_AARCH64_ADR_PREL_LO21 Need to fixup with addend!.");
710 case R_AARCH64_CONDBR19
:
711 if (Rel
->r_addend
!= 0 ) { /* TODO */
712 Error (NULL
, 0, 3000, "Invalid", "AArch64: R_AARCH64_CONDBR19 Need to fixup with addend!.");
716 case R_AARCH64_LD_PREL_LO19
:
717 if (Rel
->r_addend
!= 0 ) { /* TODO */
718 Error (NULL
, 0, 3000, "Invalid", "AArch64: R_AARCH64_LD_PREL_LO19 Need to fixup with addend!.");
722 case R_AARCH64_CALL26
:
723 case R_AARCH64_JUMP26
:
724 if (Rel
->r_addend
!= 0 ) {
725 // Some references to static functions sometime start at the base of .text + addend.
726 // It is safe to ignore these relocations because they patch a `BL` instructions that
727 // contains an offset from the instruction itself and there is only a single .text section.
728 // So we check if the symbol is a "section symbol"
729 if (ELF64_ST_TYPE (Sym
->st_info
) == STT_SECTION
) {
732 Error (NULL
, 0, 3000, "Invalid", "AArch64: R_AARCH64_JUMP26 Need to fixup with addend!.");
736 case R_AARCH64_ADR_PREL_PG_HI21
:
737 // TODO : AArch64 'small' memory model.
738 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADR_PREL_PG_HI21.", mInImageName
);
741 case R_AARCH64_ADD_ABS_LO12_NC
:
742 // TODO : AArch64 'small' memory model.
743 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADD_ABS_LO12_NC.", mInImageName
);
746 // Absolute relocations.
747 case R_AARCH64_ABS64
:
748 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
752 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
755 Error (NULL
, 0, 3000, "Invalid", "Not a supported machine type");
771 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
772 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
774 for (Index
= 0; Index
< mEhdr
->e_shnum
; Index
++) {
775 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
776 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
777 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
778 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
781 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
782 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
784 if (mEhdr
->e_machine
== EM_X86_64
) {
785 switch (ELF_R_TYPE(Rel
->r_info
)) {
790 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",
791 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
793 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
794 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
795 EFI_IMAGE_REL_BASED_DIR64
);
799 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",
800 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
802 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
803 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
804 EFI_IMAGE_REL_BASED_HIGHLOW
);
807 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
809 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
810 // AArch64 GCC uses RELA relocation, so all relocations has to be fixed up. ARM32 uses REL.
811 switch (ELF_R_TYPE(Rel
->r_info
)) {
812 case R_AARCH64_ADR_PREL_LO21
:
815 case R_AARCH64_CONDBR19
:
818 case R_AARCH64_LD_PREL_LO19
:
821 case R_AARCH64_CALL26
:
824 case R_AARCH64_JUMP26
:
827 case R_AARCH64_ADR_PREL_PG_HI21
:
828 // TODO : AArch64 'small' memory model.
829 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADR_PREL_PG_HI21.", mInImageName
);
832 case R_AARCH64_ADD_ABS_LO12_NC
:
833 // TODO : AArch64 'small' memory model.
834 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADD_ABS_LO12_NC.", mInImageName
);
837 case R_AARCH64_ABS64
:
839 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
840 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
841 EFI_IMAGE_REL_BASED_DIR64
);
844 case R_AARCH64_ABS32
:
846 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
847 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
848 EFI_IMAGE_REL_BASED_HIGHLOW
);
852 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
855 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
863 // Pad by adding empty entries.
865 while (mCoffOffset
& (mCoffAlignment
- 1)) {
866 CoffAddFixupEntry(0);
869 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
870 Dir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
871 Dir
->Size
= mCoffOffset
- mRelocOffset
;
872 if (Dir
->Size
== 0) {
873 // If no relocations, null out the directory entry and don't add the .reloc section
874 Dir
->VirtualAddress
= 0;
875 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
877 Dir
->VirtualAddress
= mRelocOffset
;
878 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
879 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
880 | EFI_IMAGE_SCN_MEM_DISCARDABLE
881 | EFI_IMAGE_SCN_MEM_READ
);
893 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
894 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
895 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
896 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
898 Len
= strlen(mInImageName
) + 1;
899 DebugOffset
= mCoffOffset
;
901 mCoffOffset
+= sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)
902 + sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
)
904 mCoffOffset
= CoffAlign(mCoffOffset
);
906 mCoffFile
= realloc(mCoffFile
, mCoffOffset
);
907 memset(mCoffFile
+ DebugOffset
, 0, mCoffOffset
- DebugOffset
);
909 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ DebugOffset
);
910 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
911 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
912 Dir
->RVA
= DebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
913 Dir
->FileOffset
= DebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
915 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
916 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
917 strcpy ((char *)(Nb10
+ 1), mInImageName
);
920 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
921 DataDir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
922 DataDir
->VirtualAddress
= DebugOffset
;
923 DataDir
->Size
= mCoffOffset
- DebugOffset
;
924 if (DataDir
->Size
== 0) {
925 // If no debug, null out the directory entry and don't add the .debug section
926 DataDir
->VirtualAddress
= 0;
927 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
929 DataDir
->VirtualAddress
= DebugOffset
;
930 CreateSectionHeader (".debug", DebugOffset
, mCoffOffset
- DebugOffset
,
931 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
932 | EFI_IMAGE_SCN_MEM_DISCARDABLE
933 | EFI_IMAGE_SCN_MEM_READ
);
944 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
949 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
950 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
959 if (mCoffSectionsOffset
!= NULL
) {
960 free (mCoffSectionsOffset
);