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 // First text sections.
306 mCoffOffset
= CoffAlign(mCoffOffset
);
307 mTextOffset
= mCoffOffset
;
308 FoundSection
= FALSE
;
310 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
311 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
312 if (IsTextShdr(shdr
)) {
313 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
314 // the alignment field is valid
315 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
316 // if the section address is aligned we must align PE/COFF
317 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
318 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
319 // ARM RVCT tools have behavior outside of the ELF specification to try
320 // and make images smaller. If sh_addr is not aligned to sh_addralign
321 // then the section needs to preserve sh_addr MOD sh_addralign.
322 // Normally doing nothing here works great.
323 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
327 /* Relocate entry. */
328 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
329 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
330 CoffEntry
= (UINT32
) (mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
);
334 // Set mTextOffset with the offset of the first '.text' section
337 mTextOffset
= mCoffOffset
;
341 mCoffSectionsOffset
[i
] = mCoffOffset
;
342 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
348 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
352 mDebugOffset
= mCoffOffset
;
354 if (mEhdr
->e_machine
!= EM_ARM
) {
355 mCoffOffset
= CoffAlign(mCoffOffset
);
358 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
359 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
363 // Then data sections.
365 mDataOffset
= mCoffOffset
;
366 FoundSection
= FALSE
;
368 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
369 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
370 if (IsDataShdr(shdr
)) {
371 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
372 // the alignment field is valid
373 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
374 // if the section address is aligned we must align PE/COFF
375 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
376 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
377 // ARM RVCT tools have behavior outside of the ELF specification to try
378 // and make images smaller. If sh_addr is not aligned to sh_addralign
379 // then the section needs to preserve sh_addr MOD sh_addralign.
380 // Normally doing nothing here works great.
381 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
386 // Set mDataOffset with the offset of the first '.data' section
389 mDataOffset
= mCoffOffset
;
392 mCoffSectionsOffset
[i
] = mCoffOffset
;
393 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
399 // Make room for .debug data in .data (or .text if .data is empty) instead of
400 // putting it in a section of its own. This is explicitly allowed by the
401 // PE/COFF spec, and prevents bloat in the binary when using large values for
402 // section alignment.
404 if (SectionCount
> 0) {
405 mDebugOffset
= mCoffOffset
;
407 mCoffOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
) +
408 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) +
409 strlen(mInImageName
) + 1;
411 mCoffOffset
= CoffAlign(mCoffOffset
);
412 if (SectionCount
== 0) {
413 mDataOffset
= mCoffOffset
;
416 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
417 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
421 // The HII resource sections.
423 mHiiRsrcOffset
= mCoffOffset
;
424 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
425 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
426 if (IsHiiRsrcShdr(shdr
)) {
427 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
428 // the alignment field is valid
429 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
430 // if the section address is aligned we must align PE/COFF
431 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
432 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
433 // ARM RVCT tools have behavior outside of the ELF specification to try
434 // and make images smaller. If sh_addr is not aligned to sh_addralign
435 // then the section needs to preserve sh_addr MOD sh_addralign.
436 // Normally doing nothing here works great.
437 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
440 if (shdr
->sh_size
!= 0) {
441 mHiiRsrcOffset
= mCoffOffset
;
442 mCoffSectionsOffset
[i
] = mCoffOffset
;
443 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
444 mCoffOffset
= CoffAlign(mCoffOffset
);
445 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
451 mRelocOffset
= mCoffOffset
;
454 // Allocate base Coff file. Will be expanded later for relocations.
456 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
457 memset(mCoffFile
, 0, mCoffOffset
);
462 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
463 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
464 DosHdr
->e_lfanew
= mNtHdrOffset
;
466 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
468 NtHdr
->Pe32Plus
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
470 switch (mEhdr
->e_machine
) {
472 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
473 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
476 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IPF
;
477 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
480 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_AARCH64
;
481 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
484 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN
)mEhdr
->e_machine
);
485 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
486 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
489 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
490 NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
491 mImageTimeStamp
= NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
;
492 NtHdr
->Pe32Plus
.FileHeader
.PointerToSymbolTable
= 0;
493 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSymbols
= 0;
494 NtHdr
->Pe32Plus
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32Plus
.OptionalHeader
);
495 NtHdr
->Pe32Plus
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
496 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
497 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
498 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE
;
500 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
501 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
502 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfUninitializedData
= 0;
503 NtHdr
->Pe32Plus
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
505 NtHdr
->Pe32Plus
.OptionalHeader
.BaseOfCode
= mTextOffset
;
507 NtHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= 0;
508 NtHdr
->Pe32Plus
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
509 NtHdr
->Pe32Plus
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
510 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= 0;
512 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
513 NtHdr
->Pe32Plus
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
518 if ((mDataOffset
- mTextOffset
) > 0) {
519 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
520 EFI_IMAGE_SCN_CNT_CODE
521 | EFI_IMAGE_SCN_MEM_EXECUTE
522 | EFI_IMAGE_SCN_MEM_READ
);
524 // Don't make a section of size 0.
525 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
528 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
529 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
530 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
531 | EFI_IMAGE_SCN_MEM_WRITE
532 | EFI_IMAGE_SCN_MEM_READ
);
534 // Don't make a section of size 0.
535 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
538 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
539 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
540 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
541 | EFI_IMAGE_SCN_MEM_READ
);
543 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
544 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
546 // Don't make a section of size 0.
547 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
555 SECTION_FILTER_TYPES FilterType
561 BOOLEAN (*Filter
)(Elf_Shdr
*);
564 // Initialize filter pointer
566 switch (FilterType
) {
571 Filter
= IsHiiRsrcShdr
;
581 // First: copy sections.
583 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
584 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
585 if ((*Filter
)(Shdr
)) {
586 switch (Shdr
->sh_type
) {
589 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
590 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
591 (size_t) Shdr
->sh_size
);
595 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, (size_t) Shdr
->sh_size
);
600 // Ignore for unkown section type.
602 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName
, (unsigned)Shdr
->sh_type
);
609 // Second: apply relocations.
611 VerboseMsg ("Applying Relocations...");
612 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
614 // Determine if this is a relocation section.
616 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
617 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
622 // Relocation section found. Now extract section information that the relocations
623 // apply to in the ELF data and the new COFF data.
625 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
626 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
629 // Only process relocations for the current filter type.
631 if (RelShdr
->sh_type
== SHT_RELA
&& (*Filter
)(SecShdr
)) {
635 // Determine the symbol table referenced by the relocation data.
637 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
638 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
641 // Process all relocation entries for this section.
643 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= (UINT32
) RelShdr
->sh_entsize
) {
646 // Set pointer to relocation entry
648 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
651 // Set pointer to symbol table entry associated with the relocation entry.
653 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
659 // Check section header index found in symbol table and get the section
662 if (Sym
->st_shndx
== SHN_UNDEF
663 || Sym
->st_shndx
== SHN_ABS
664 || Sym
->st_shndx
> mEhdr
->e_shnum
) {
665 Error (NULL
, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName
);
667 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
670 // Convert the relocation data to a pointer into the coff file.
673 // r_offset is the virtual address of the storage unit to be relocated.
674 // sh_addr is the virtual address for the base of the section.
676 // r_offset in a memory address.
677 // Convert it to a pointer in the coff file.
679 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
682 // Determine how to handle each relocation type based on the machine type.
684 if (mEhdr
->e_machine
== EM_X86_64
) {
685 switch (ELF_R_TYPE(Rel
->r_info
)) {
690 // Absolute relocation.
692 VerboseMsg ("R_X86_64_64");
693 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
694 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
696 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
697 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
700 VerboseMsg ("R_X86_64_32");
701 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
702 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
704 *(UINT32
*)Targ
= (UINT32
)((UINT64
)(*(UINT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
705 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
708 VerboseMsg ("R_X86_64_32S");
709 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
710 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
712 *(INT32
*)Targ
= (INT32
)((INT64
)(*(INT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
713 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
717 // Relative relocation: Symbol - Ip + Addend
719 VerboseMsg ("R_X86_64_PC32");
720 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
721 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
723 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
724 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
725 - (SecOffset
- SecShdr
->sh_addr
));
726 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
729 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
731 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
733 // AARCH64 GCC uses RELA relocation, so all relocations have to be fixed up.
734 // As opposed to ARM32 using REL.
736 switch (ELF_R_TYPE(Rel
->r_info
)) {
738 case R_AARCH64_ADR_PREL_LO21
:
739 if (Rel
->r_addend
!= 0 ) { /* TODO */
740 Error (NULL
, 0, 3000, "Invalid", "AArch64: R_AARCH64_ADR_PREL_LO21 Need to fixup with addend!.");
744 case R_AARCH64_CONDBR19
:
745 if (Rel
->r_addend
!= 0 ) { /* TODO */
746 Error (NULL
, 0, 3000, "Invalid", "AArch64: R_AARCH64_CONDBR19 Need to fixup with addend!.");
750 case R_AARCH64_LD_PREL_LO19
:
751 if (Rel
->r_addend
!= 0 ) { /* TODO */
752 Error (NULL
, 0, 3000, "Invalid", "AArch64: R_AARCH64_LD_PREL_LO19 Need to fixup with addend!.");
756 case R_AARCH64_CALL26
:
757 case R_AARCH64_JUMP26
:
758 if (Rel
->r_addend
!= 0 ) {
759 // Some references to static functions sometime start at the base of .text + addend.
760 // It is safe to ignore these relocations because they patch a `BL` instructions that
761 // contains an offset from the instruction itself and there is only a single .text section.
762 // So we check if the symbol is a "section symbol"
763 if (ELF64_ST_TYPE (Sym
->st_info
) == STT_SECTION
) {
766 Error (NULL
, 0, 3000, "Invalid", "AArch64: R_AARCH64_JUMP26 Need to fixup with addend!.");
770 case R_AARCH64_ADR_PREL_PG_HI21
:
771 // TODO : AArch64 'small' memory model.
772 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADR_PREL_PG_HI21.", mInImageName
);
775 case R_AARCH64_ADD_ABS_LO12_NC
:
776 // TODO : AArch64 'small' memory model.
777 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADD_ABS_LO12_NC.", mInImageName
);
780 // Absolute relocations.
781 case R_AARCH64_ABS64
:
782 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
786 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
789 Error (NULL
, 0, 3000, "Invalid", "Not a supported machine type");
805 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
806 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
808 for (Index
= 0; Index
< mEhdr
->e_shnum
; Index
++) {
809 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
810 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
811 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
812 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
815 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
816 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
818 if (mEhdr
->e_machine
== EM_X86_64
) {
819 switch (ELF_R_TYPE(Rel
->r_info
)) {
824 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",
825 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
827 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
828 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
829 EFI_IMAGE_REL_BASED_DIR64
);
833 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",
834 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
836 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
837 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
838 EFI_IMAGE_REL_BASED_HIGHLOW
);
841 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
843 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
844 // AArch64 GCC uses RELA relocation, so all relocations has to be fixed up. ARM32 uses REL.
845 switch (ELF_R_TYPE(Rel
->r_info
)) {
846 case R_AARCH64_ADR_PREL_LO21
:
849 case R_AARCH64_CONDBR19
:
852 case R_AARCH64_LD_PREL_LO19
:
855 case R_AARCH64_CALL26
:
858 case R_AARCH64_JUMP26
:
861 case R_AARCH64_ADR_PREL_PG_HI21
:
862 // TODO : AArch64 'small' memory model.
863 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADR_PREL_PG_HI21.", mInImageName
);
866 case R_AARCH64_ADD_ABS_LO12_NC
:
867 // TODO : AArch64 'small' memory model.
868 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADD_ABS_LO12_NC.", mInImageName
);
871 case R_AARCH64_ABS64
:
873 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
874 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
875 EFI_IMAGE_REL_BASED_DIR64
);
878 case R_AARCH64_ABS32
:
880 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
881 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
882 EFI_IMAGE_REL_BASED_HIGHLOW
);
886 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
889 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
897 // Pad by adding empty entries.
899 while (mCoffOffset
& (mCoffAlignment
- 1)) {
900 CoffAddFixupEntry(0);
903 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
904 Dir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
905 Dir
->Size
= mCoffOffset
- mRelocOffset
;
906 if (Dir
->Size
== 0) {
907 // If no relocations, null out the directory entry and don't add the .reloc section
908 Dir
->VirtualAddress
= 0;
909 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
911 Dir
->VirtualAddress
= mRelocOffset
;
912 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
913 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
914 | EFI_IMAGE_SCN_MEM_DISCARDABLE
915 | EFI_IMAGE_SCN_MEM_READ
);
926 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
927 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
928 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
929 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
931 Len
= strlen(mInImageName
) + 1;
933 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ mDebugOffset
);
934 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
935 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
936 Dir
->RVA
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
937 Dir
->FileOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
939 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
940 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
941 strcpy ((char *)(Nb10
+ 1), mInImageName
);
944 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
945 DataDir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
946 DataDir
->VirtualAddress
= mDebugOffset
;
947 DataDir
->Size
= Dir
->SizeOfData
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
956 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
961 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
962 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
971 if (mCoffSectionsOffset
!= NULL
) {
972 free (mCoffSectionsOffset
);