3 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
4 Portions copyright (c) 2013, ARM Ltd. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available
7 under the terms and conditions of the BSD License which accompanies this
8 distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "WinNtInclude.h"
29 #include <Common/UefiBaseTypes.h>
30 #include <IndustryStandard/PeImage.h>
32 #include "PeCoffLib.h"
33 #include "EfiUtilityMsgs.h"
36 #include "ElfConvert.h"
37 #include "Elf64Convert.h"
48 SECTION_FILTER_TYPES FilterType
76 // Rename ELF32 strucutres to common names to help when porting to ELF64.
78 typedef Elf64_Shdr Elf_Shdr
;
79 typedef Elf64_Ehdr Elf_Ehdr
;
80 typedef Elf64_Rel Elf_Rel
;
81 typedef Elf64_Rela Elf_Rela
;
82 typedef Elf64_Sym Elf_Sym
;
83 typedef Elf64_Phdr Elf_Phdr
;
84 typedef Elf64_Dyn Elf_Dyn
;
85 #define ELFCLASS ELFCLASS64
86 #define ELF_R_TYPE(r) ELF64_R_TYPE(r)
87 #define ELF_R_SYM(r) ELF64_R_SYM(r)
90 // Well known ELF structures.
92 STATIC Elf_Ehdr
*mEhdr
;
93 STATIC Elf_Shdr
*mShdrBase
;
94 STATIC Elf_Phdr
*mPhdrBase
;
99 STATIC
const UINT32 mCoffAlignment
= 0x20;
102 // PE section alignment.
104 STATIC
const UINT16 mCoffNbrSections
= 5;
107 // ELF sections to offset in Coff file.
109 STATIC UINT32
*mCoffSectionsOffset
= NULL
;
112 // Offsets in COFF file
114 STATIC UINT32 mNtHdrOffset
;
115 STATIC UINT32 mTextOffset
;
116 STATIC UINT32 mDataOffset
;
117 STATIC UINT32 mHiiRsrcOffset
;
118 STATIC UINT32 mRelocOffset
;
121 // Initialization Function
126 ELF_FUNCTION_TABLE
*ElfFunctions
130 // Initialize data pointer and structures.
132 VerboseMsg ("Set EHDR");
133 mEhdr
= (Elf_Ehdr
*) FileBuffer
;
136 // Check the ELF64 specific header information.
138 VerboseMsg ("Check ELF64 Header Information");
139 if (mEhdr
->e_ident
[EI_CLASS
] != ELFCLASS64
) {
140 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS64");
143 if (mEhdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
144 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
147 if ((mEhdr
->e_type
!= ET_EXEC
) && (mEhdr
->e_type
!= ET_DYN
)) {
148 Error (NULL
, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
151 if (!((mEhdr
->e_machine
== EM_X86_64
) || (mEhdr
->e_machine
== EM_AARCH64
))) {
152 Error (NULL
, 0, 3000, "Unsupported", "ELF e_machine not EM_X86_64 or EM_AARCH64");
155 if (mEhdr
->e_version
!= EV_CURRENT
) {
156 Error (NULL
, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr
->e_version
, EV_CURRENT
);
161 // Update section header pointers
163 VerboseMsg ("Update Header Pointers");
164 mShdrBase
= (Elf_Shdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_shoff
);
165 mPhdrBase
= (Elf_Phdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_phoff
);
168 // Create COFF Section offset buffer and zero.
170 VerboseMsg ("Create COFF Section Offset Buffer");
171 mCoffSectionsOffset
= (UINT32
*)malloc(mEhdr
->e_shnum
* sizeof (UINT32
));
172 memset(mCoffSectionsOffset
, 0, mEhdr
->e_shnum
* sizeof(UINT32
));
175 // Fill in function pointers.
177 VerboseMsg ("Fill in Function Pointers");
178 ElfFunctions
->ScanSections
= ScanSections64
;
179 ElfFunctions
->WriteSections
= WriteSections64
;
180 ElfFunctions
->WriteRelocations
= WriteRelocations64
;
181 ElfFunctions
->WriteDebug
= WriteDebug64
;
182 ElfFunctions
->SetImageSize
= SetImageSize64
;
183 ElfFunctions
->CleanUp
= CleanUp64
;
190 // Header by Index functions
198 if (Num
>= mEhdr
->e_shnum
)
200 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
209 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
221 return (BOOLEAN
) ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
);
230 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
232 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
241 if (IsHiiRsrcShdr(Shdr
)) {
244 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
248 // Elf functions interface implementation
258 EFI_IMAGE_DOS_HEADER
*DosHdr
;
259 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
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 // First text sections.
292 mCoffOffset
= CoffAlign(mCoffOffset
);
294 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
295 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
296 if (IsTextShdr(shdr
)) {
297 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
298 // the alignment field is valid
299 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
300 // if the section address is aligned we must align PE/COFF
301 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
302 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
303 // ARM RVCT tools have behavior outside of the ELF specification to try
304 // and make images smaller. If sh_addr is not aligned to sh_addralign
305 // then the section needs to preserve sh_addr MOD sh_addralign.
306 // Normally doing nothing here works great.
307 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
311 /* Relocate entry. */
312 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
313 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
314 CoffEntry
= (UINT32
) (mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
);
318 // Set mTextOffset with the offset of the first '.text' section
321 mTextOffset
= mCoffOffset
;
325 mCoffSectionsOffset
[i
] = mCoffOffset
;
326 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
332 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
336 if (mEhdr
->e_machine
!= EM_ARM
) {
337 mCoffOffset
= CoffAlign(mCoffOffset
);
340 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
341 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
345 // Then data sections.
347 mDataOffset
= mCoffOffset
;
349 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
350 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
351 if (IsDataShdr(shdr
)) {
352 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
353 // the alignment field is valid
354 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
355 // if the section address is aligned we must align PE/COFF
356 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
357 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
358 // ARM RVCT tools have behavior outside of the ELF specification to try
359 // and make images smaller. If sh_addr is not aligned to sh_addralign
360 // then the section needs to preserve sh_addr MOD sh_addralign.
361 // Normally doing nothing here works great.
362 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
365 mCoffSectionsOffset
[i
] = mCoffOffset
;
366 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
370 mCoffOffset
= CoffAlign(mCoffOffset
);
372 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
373 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
377 // The HII resource sections.
379 mHiiRsrcOffset
= mCoffOffset
;
380 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
381 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
382 if (IsHiiRsrcShdr(shdr
)) {
383 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
384 // the alignment field is valid
385 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
386 // if the section address is aligned we must align PE/COFF
387 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
388 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
389 // ARM RVCT tools have behavior outside of the ELF specification to try
390 // and make images smaller. If sh_addr is not aligned to sh_addralign
391 // then the section needs to preserve sh_addr MOD sh_addralign.
392 // Normally doing nothing here works great.
393 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
396 if (shdr
->sh_size
!= 0) {
397 mCoffSectionsOffset
[i
] = mCoffOffset
;
398 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
399 mCoffOffset
= CoffAlign(mCoffOffset
);
400 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
406 mRelocOffset
= mCoffOffset
;
409 // Allocate base Coff file. Will be expanded later for relocations.
411 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
412 memset(mCoffFile
, 0, mCoffOffset
);
417 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
418 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
419 DosHdr
->e_lfanew
= mNtHdrOffset
;
421 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
423 NtHdr
->Pe32Plus
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
425 switch (mEhdr
->e_machine
) {
427 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
428 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
431 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IPF
;
432 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
435 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_AARCH64
;
436 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
439 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN
)mEhdr
->e_machine
);
440 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
441 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
444 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
445 NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
446 mImageTimeStamp
= NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
;
447 NtHdr
->Pe32Plus
.FileHeader
.PointerToSymbolTable
= 0;
448 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSymbols
= 0;
449 NtHdr
->Pe32Plus
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32Plus
.OptionalHeader
);
450 NtHdr
->Pe32Plus
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
451 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
452 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
453 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE
;
455 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
456 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
457 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfUninitializedData
= 0;
458 NtHdr
->Pe32Plus
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
460 NtHdr
->Pe32Plus
.OptionalHeader
.BaseOfCode
= mTextOffset
;
462 NtHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= 0;
463 NtHdr
->Pe32Plus
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
464 NtHdr
->Pe32Plus
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
465 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= 0;
467 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
468 NtHdr
->Pe32Plus
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
473 if ((mDataOffset
- mTextOffset
) > 0) {
474 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
475 EFI_IMAGE_SCN_CNT_CODE
476 | EFI_IMAGE_SCN_MEM_EXECUTE
477 | EFI_IMAGE_SCN_MEM_READ
);
479 // Don't make a section of size 0.
480 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
483 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
484 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
485 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
486 | EFI_IMAGE_SCN_MEM_WRITE
487 | EFI_IMAGE_SCN_MEM_READ
);
489 // Don't make a section of size 0.
490 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
493 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
494 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
495 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
496 | EFI_IMAGE_SCN_MEM_READ
);
498 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
499 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
501 // Don't make a section of size 0.
502 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
510 SECTION_FILTER_TYPES FilterType
516 BOOLEAN (*Filter
)(Elf_Shdr
*);
519 // Initialize filter pointer
521 switch (FilterType
) {
526 Filter
= IsHiiRsrcShdr
;
536 // First: copy sections.
538 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
539 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
540 if ((*Filter
)(Shdr
)) {
541 switch (Shdr
->sh_type
) {
544 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
545 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
546 (size_t) Shdr
->sh_size
);
550 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, (size_t) Shdr
->sh_size
);
555 // Ignore for unkown section type.
557 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName
, (unsigned)Shdr
->sh_type
);
564 // Second: apply relocations.
566 VerboseMsg ("Applying Relocations...");
567 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
569 // Determine if this is a relocation section.
571 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
572 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
577 // Relocation section found. Now extract section information that the relocations
578 // apply to in the ELF data and the new COFF data.
580 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
581 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
584 // Only process relocations for the current filter type.
586 if (RelShdr
->sh_type
== SHT_RELA
&& (*Filter
)(SecShdr
)) {
590 // Determine the symbol table referenced by the relocation data.
592 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
593 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
596 // Process all relocation entries for this section.
598 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= (UINT32
) RelShdr
->sh_entsize
) {
601 // Set pointer to relocation entry
603 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
606 // Set pointer to symbol table entry associated with the relocation entry.
608 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
614 // Check section header index found in symbol table and get the section
617 if (Sym
->st_shndx
== SHN_UNDEF
618 || Sym
->st_shndx
== SHN_ABS
619 || Sym
->st_shndx
> mEhdr
->e_shnum
) {
620 Error (NULL
, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName
);
622 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
625 // Convert the relocation data to a pointer into the coff file.
628 // r_offset is the virtual address of the storage unit to be relocated.
629 // sh_addr is the virtual address for the base of the section.
631 // r_offset in a memory address.
632 // Convert it to a pointer in the coff file.
634 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
637 // Determine how to handle each relocation type based on the machine type.
639 if (mEhdr
->e_machine
== EM_X86_64
) {
640 switch (ELF_R_TYPE(Rel
->r_info
)) {
645 // Absolute relocation.
647 VerboseMsg ("R_X86_64_64");
648 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
649 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
651 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
652 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
655 VerboseMsg ("R_X86_64_32");
656 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
657 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
659 *(UINT32
*)Targ
= (UINT32
)((UINT64
)(*(UINT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
660 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
663 VerboseMsg ("R_X86_64_32S");
664 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
665 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
667 *(INT32
*)Targ
= (INT32
)((INT64
)(*(INT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
668 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
672 // Relative relocation: Symbol - Ip + Addend
674 VerboseMsg ("R_X86_64_PC32");
675 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
676 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
678 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
679 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
680 - (SecOffset
- SecShdr
->sh_addr
));
681 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
684 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
686 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
688 // AARCH64 GCC uses RELA relocation, so all relocations have to be fixed up.
689 // As opposed to ARM32 using REL.
691 switch (ELF_R_TYPE(Rel
->r_info
)) {
693 case R_AARCH64_LD_PREL_LO19
:
694 if (Rel
->r_addend
!= 0 ) { /* TODO */
695 Error (NULL
, 0, 3000, "Invalid", "AArch64: R_AARCH64_LD_PREL_LO19 Need to fixup with addend!.");
699 case R_AARCH64_CALL26
:
700 if (Rel
->r_addend
!= 0 ) { /* TODO */
701 Error (NULL
, 0, 3000, "Invalid", "AArch64: R_AARCH64_CALL26 Need to fixup with addend!.");
705 case R_AARCH64_JUMP26
:
706 if (Rel
->r_addend
!= 0 ) { /* TODO : AArch64 '-O2' optimisation. */
707 Error (NULL
, 0, 3000, "Invalid", "AArch64: R_AARCH64_JUMP26 Need to fixup with addend!.");
711 case R_AARCH64_ADR_PREL_PG_HI21
:
712 // TODO : AArch64 'small' memory model.
713 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADR_PREL_PG_HI21.", mInImageName
);
716 case R_AARCH64_ADD_ABS_LO12_NC
:
717 // TODO : AArch64 'small' memory model.
718 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADD_ABS_LO12_NC.", mInImageName
);
721 // Absolute relocations.
722 case R_AARCH64_ABS64
:
723 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
727 Error (NULL
, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
730 Error (NULL
, 0, 3000, "Invalid", "Not a supported machine type");
746 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
747 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
749 for (Index
= 0; Index
< mEhdr
->e_shnum
; Index
++) {
750 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
751 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
752 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
753 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
756 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
757 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
759 if (mEhdr
->e_machine
== EM_X86_64
) {
760 switch (ELF_R_TYPE(Rel
->r_info
)) {
765 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",
766 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
768 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
769 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
770 EFI_IMAGE_REL_BASED_DIR64
);
774 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",
775 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
777 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
778 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
779 EFI_IMAGE_REL_BASED_HIGHLOW
);
782 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
784 } else if (mEhdr
->e_machine
== EM_AARCH64
) {
785 // AArch64 GCC uses RELA relocation, so all relocations has to be fixed up. ARM32 uses REL.
786 switch (ELF_R_TYPE(Rel
->r_info
)) {
787 case R_AARCH64_LD_PREL_LO19
:
790 case R_AARCH64_CALL26
:
793 case R_AARCH64_JUMP26
:
796 case R_AARCH64_ADR_PREL_PG_HI21
:
797 // TODO : AArch64 'small' memory model.
798 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADR_PREL_PG_HI21.", mInImageName
);
801 case R_AARCH64_ADD_ABS_LO12_NC
:
802 // TODO : AArch64 'small' memory model.
803 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADD_ABS_LO12_NC.", mInImageName
);
806 case R_AARCH64_ABS64
:
808 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
809 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
810 EFI_IMAGE_REL_BASED_DIR64
);
813 case R_AARCH64_ABS32
:
815 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
816 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
817 EFI_IMAGE_REL_BASED_HIGHLOW
);
821 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
824 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
832 // Pad by adding empty entries.
834 while (mCoffOffset
& (mCoffAlignment
- 1)) {
835 CoffAddFixupEntry(0);
838 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
839 Dir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
840 Dir
->Size
= mCoffOffset
- mRelocOffset
;
841 if (Dir
->Size
== 0) {
842 // If no relocations, null out the directory entry and don't add the .reloc section
843 Dir
->VirtualAddress
= 0;
844 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
846 Dir
->VirtualAddress
= mRelocOffset
;
847 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
848 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
849 | EFI_IMAGE_SCN_MEM_DISCARDABLE
850 | EFI_IMAGE_SCN_MEM_READ
);
862 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
863 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
864 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
865 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
867 Len
= strlen(mInImageName
) + 1;
868 DebugOffset
= mCoffOffset
;
870 mCoffOffset
+= sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)
871 + sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
)
873 mCoffOffset
= CoffAlign(mCoffOffset
);
875 mCoffFile
= realloc(mCoffFile
, mCoffOffset
);
876 memset(mCoffFile
+ DebugOffset
, 0, mCoffOffset
- DebugOffset
);
878 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ DebugOffset
);
879 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
880 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
881 Dir
->RVA
= DebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
882 Dir
->FileOffset
= DebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
884 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
885 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
886 strcpy ((char *)(Nb10
+ 1), mInImageName
);
889 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
890 DataDir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
891 DataDir
->VirtualAddress
= DebugOffset
;
892 DataDir
->Size
= mCoffOffset
- DebugOffset
;
893 if (DataDir
->Size
== 0) {
894 // If no debug, null out the directory entry and don't add the .debug section
895 DataDir
->VirtualAddress
= 0;
896 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
898 DataDir
->VirtualAddress
= DebugOffset
;
899 CreateSectionHeader (".debug", DebugOffset
, mCoffOffset
- DebugOffset
,
900 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
901 | EFI_IMAGE_SCN_MEM_DISCARDABLE
902 | EFI_IMAGE_SCN_MEM_READ
);
913 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
918 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
919 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
928 if (mCoffSectionsOffset
!= NULL
) {
929 free (mCoffSectionsOffset
);