4 Copyright (c) 2010 - 2021, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2013, ARM Ltd. All rights reserved.<BR>
6 Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include "WinNtInclude.h"
25 #include <Common/UefiBaseTypes.h>
26 #include <IndustryStandard/PeImage.h>
28 #include "PeCoffLib.h"
29 #include "EfiUtilityMsgs.h"
32 #include "ElfConvert.h"
33 #include "Elf32Convert.h"
44 SECTION_FILTER_TYPES FilterType
72 // Rename ELF32 structures to common names to help when porting to ELF64.
74 typedef Elf32_Shdr Elf_Shdr
;
75 typedef Elf32_Ehdr Elf_Ehdr
;
76 typedef Elf32_Rel Elf_Rel
;
77 typedef Elf32_Sym Elf_Sym
;
78 typedef Elf32_Phdr Elf_Phdr
;
79 typedef Elf32_Dyn Elf_Dyn
;
80 #define ELFCLASS ELFCLASS32
81 #define ELF_R_TYPE(r) ELF32_R_TYPE(r)
82 #define ELF_R_SYM(r) ELF32_R_SYM(r)
85 // Well known ELF structures.
87 STATIC Elf_Ehdr
*mEhdr
;
88 STATIC Elf_Shdr
*mShdrBase
;
89 STATIC Elf_Phdr
*mPhdrBase
;
94 STATIC UINT32 mCoffAlignment
= 0x20;
97 // PE section alignment.
99 STATIC
const UINT16 mCoffNbrSections
= 4;
102 // ELF sections to offset in Coff file.
104 STATIC UINT32
*mCoffSectionsOffset
= NULL
;
107 // Offsets in COFF file
109 STATIC UINT32 mNtHdrOffset
;
110 STATIC UINT32 mTextOffset
;
111 STATIC UINT32 mDataOffset
;
112 STATIC UINT32 mHiiRsrcOffset
;
113 STATIC UINT32 mRelocOffset
;
114 STATIC UINT32 mDebugOffset
;
117 // Initialization Function
122 ELF_FUNCTION_TABLE
*ElfFunctions
126 // Initialize data pointer and structures.
128 mEhdr
= (Elf_Ehdr
*) FileBuffer
;
131 // Check the ELF32 specific header information.
133 if (mEhdr
->e_ident
[EI_CLASS
] != ELFCLASS32
) {
134 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS32");
137 if (mEhdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
138 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
141 if ((mEhdr
->e_type
!= ET_EXEC
) && (mEhdr
->e_type
!= ET_DYN
)) {
142 Error (NULL
, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
145 if (!((mEhdr
->e_machine
== EM_386
) || (mEhdr
->e_machine
== EM_ARM
) || (mEhdr
->e_machine
== EM_RISCV
))) {
146 Warning (NULL
, 0, 3000, "Unsupported", "ELF e_machine is not Elf32 machine.");
148 if (mEhdr
->e_version
!= EV_CURRENT
) {
149 Error (NULL
, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr
->e_version
, EV_CURRENT
);
154 // Update section header pointers
156 mShdrBase
= (Elf_Shdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_shoff
);
157 mPhdrBase
= (Elf_Phdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_phoff
);
160 // Create COFF Section offset buffer and zero.
162 mCoffSectionsOffset
= (UINT32
*)malloc(mEhdr
->e_shnum
* sizeof (UINT32
));
163 if (mCoffSectionsOffset
== NULL
) {
164 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
167 memset(mCoffSectionsOffset
, 0, mEhdr
->e_shnum
* sizeof(UINT32
));
170 // Fill in function pointers.
172 ElfFunctions
->ScanSections
= ScanSections32
;
173 ElfFunctions
->WriteSections
= WriteSections32
;
174 ElfFunctions
->WriteRelocations
= WriteRelocations32
;
175 ElfFunctions
->WriteDebug
= WriteDebug32
;
176 ElfFunctions
->SetImageSize
= SetImageSize32
;
177 ElfFunctions
->CleanUp
= CleanUp32
;
184 // Header by Index functions
192 if (Num
>= mEhdr
->e_shnum
) {
193 Error (NULL
, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num
);
197 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
206 if (num
>= mEhdr
->e_phnum
) {
207 Error (NULL
, 0, 3000, "Invalid", "GetPhdrByIndex: Index %u is too high.", num
);
211 return (Elf_Phdr
*)((UINT8
*)mPhdrBase
+ num
* mEhdr
->e_phentsize
);
220 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
229 return (Offset
+ 3) & ~3;
241 return (BOOLEAN
) (((Shdr
->sh_flags
& (SHF_EXECINSTR
| SHF_ALLOC
)) == (SHF_EXECINSTR
| SHF_ALLOC
)) ||
242 ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
));
251 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
253 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
262 if (IsHiiRsrcShdr(Shdr
)) {
265 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_EXECINSTR
| SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
274 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
276 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_STRTAB_SECTION_NAME
) == 0);
286 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
287 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
288 if (IsStrtabShdr(shdr
)) {
301 Elf_Shdr
*StrtabShdr
;
302 UINT8
*StrtabContents
;
306 if (Sym
->st_name
== 0) {
310 StrtabShdr
= FindStrtabShdr();
311 if (StrtabShdr
== NULL
) {
315 assert(Sym
->st_name
< StrtabShdr
->sh_size
);
317 StrtabContents
= (UINT8
*)mEhdr
+ StrtabShdr
->sh_offset
;
320 for (i
= Sym
->st_name
; (i
< StrtabShdr
->sh_size
) && !foundEnd
; i
++) {
321 foundEnd
= (BOOLEAN
)(StrtabContents
[i
] == 0);
325 return StrtabContents
+ Sym
->st_name
;
329 // Elf functions interface implementation
339 EFI_IMAGE_DOS_HEADER
*DosHdr
;
340 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
343 BOOLEAN FoundSection
;
349 // Coff file start with a DOS header.
351 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
352 mNtHdrOffset
= mCoffOffset
;
353 switch (mEhdr
->e_machine
) {
356 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS32
);
359 VerboseMsg ("%u unknown e_machine type. Assume IA-32", (UINTN
)mEhdr
->e_machine
);
360 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS32
);
364 mTableOffset
= mCoffOffset
;
365 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
368 // Set mCoffAlignment to the maximum alignment of the input sections
371 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
372 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
373 if (shdr
->sh_addralign
<= mCoffAlignment
) {
376 if (IsTextShdr(shdr
) || IsDataShdr(shdr
) || IsHiiRsrcShdr(shdr
)) {
377 mCoffAlignment
= (UINT32
)shdr
->sh_addralign
;
382 // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT
384 if (mCoffAlignment
> MAX_COFF_ALIGNMENT
) {
385 Error (NULL
, 0, 3000, "Invalid", "Section alignment is larger than MAX_COFF_ALIGNMENT.");
390 // Move the PE/COFF header right before the first section. This will help us
391 // save space when converting to TE.
393 if (mCoffAlignment
> mCoffOffset
) {
394 mNtHdrOffset
+= mCoffAlignment
- mCoffOffset
;
395 mTableOffset
+= mCoffAlignment
- mCoffOffset
;
396 mCoffOffset
= mCoffAlignment
;
400 // First text sections.
402 mCoffOffset
= CoffAlign(mCoffOffset
);
403 mTextOffset
= mCoffOffset
;
404 FoundSection
= FALSE
;
406 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
407 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
408 if (IsTextShdr(shdr
)) {
409 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
410 // the alignment field is valid
411 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
412 // if the section address is aligned we must align PE/COFF
413 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
415 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
419 /* Relocate entry. */
420 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
421 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
422 CoffEntry
= mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
;
426 // Set mTextOffset with the offset of the first '.text' section
429 mTextOffset
= mCoffOffset
;
433 mCoffSectionsOffset
[i
] = mCoffOffset
;
434 mCoffOffset
+= shdr
->sh_size
;
439 if (!FoundSection
&& mOutImageType
!= FW_ACPI_IMAGE
) {
440 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
444 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
445 mCoffOffset
= CoffAlign(mCoffOffset
);
447 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
448 Warning (NULL
, 0, 0, NULL
, "Multiple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
452 // Then data sections.
454 mDataOffset
= mCoffOffset
;
455 FoundSection
= FALSE
;
457 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
458 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
459 if (IsDataShdr(shdr
)) {
460 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
461 // the alignment field is valid
462 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
463 // if the section address is aligned we must align PE/COFF
464 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
466 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
471 // Set mDataOffset with the offset of the first '.data' section
474 mDataOffset
= mCoffOffset
;
478 mCoffSectionsOffset
[i
] = mCoffOffset
;
479 mCoffOffset
+= shdr
->sh_size
;
484 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
485 Warning (NULL
, 0, 0, NULL
, "Multiple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
489 // Make room for .debug data in .data (or .text if .data is empty) instead of
490 // putting it in a section of its own. This is explicitly allowed by the
491 // PE/COFF spec, and prevents bloat in the binary when using large values for
492 // section alignment.
494 if (SectionCount
> 0) {
495 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
497 mCoffOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
) +
498 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) +
499 strlen(mInImageName
) + 1;
501 mCoffOffset
= CoffAlign(mCoffOffset
);
502 if (SectionCount
== 0) {
503 mDataOffset
= mCoffOffset
;
507 // The HII resource sections.
509 mHiiRsrcOffset
= mCoffOffset
;
510 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
511 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
512 if (IsHiiRsrcShdr(shdr
)) {
513 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
514 // the alignment field is valid
515 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
516 // if the section address is aligned we must align PE/COFF
517 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
519 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
522 if (shdr
->sh_size
!= 0) {
523 mHiiRsrcOffset
= mCoffOffset
;
524 mCoffSectionsOffset
[i
] = mCoffOffset
;
525 mCoffOffset
+= shdr
->sh_size
;
526 mCoffOffset
= CoffAlign(mCoffOffset
);
527 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
533 mRelocOffset
= mCoffOffset
;
536 // Allocate base Coff file. Will be expanded later for relocations.
538 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
539 if (mCoffFile
== NULL
) {
540 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
542 assert (mCoffFile
!= NULL
);
543 memset(mCoffFile
, 0, mCoffOffset
);
548 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
549 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
550 DosHdr
->e_lfanew
= mNtHdrOffset
;
552 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
554 NtHdr
->Pe32
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
556 switch (mEhdr
->e_machine
) {
558 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IA32
;
559 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
562 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_ARMT
;
563 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
566 VerboseMsg ("%s unknown e_machine type %hu. Assume IA-32", mInImageName
, mEhdr
->e_machine
);
567 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IA32
;
568 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
571 NtHdr
->Pe32
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
572 NtHdr
->Pe32
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
573 mImageTimeStamp
= NtHdr
->Pe32
.FileHeader
.TimeDateStamp
;
574 NtHdr
->Pe32
.FileHeader
.PointerToSymbolTable
= 0;
575 NtHdr
->Pe32
.FileHeader
.NumberOfSymbols
= 0;
576 NtHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32
.OptionalHeader
);
577 NtHdr
->Pe32
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
578 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
579 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
580 | EFI_IMAGE_FILE_32BIT_MACHINE
;
582 NtHdr
->Pe32
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
583 NtHdr
->Pe32
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
584 NtHdr
->Pe32
.OptionalHeader
.SizeOfUninitializedData
= 0;
585 NtHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
587 NtHdr
->Pe32
.OptionalHeader
.BaseOfCode
= mTextOffset
;
589 NtHdr
->Pe32
.OptionalHeader
.BaseOfData
= mDataOffset
;
590 NtHdr
->Pe32
.OptionalHeader
.ImageBase
= 0;
591 NtHdr
->Pe32
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
592 NtHdr
->Pe32
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
593 NtHdr
->Pe32
.OptionalHeader
.SizeOfImage
= 0;
595 NtHdr
->Pe32
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
596 NtHdr
->Pe32
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
601 if ((mDataOffset
- mTextOffset
) > 0) {
602 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
603 EFI_IMAGE_SCN_CNT_CODE
604 | EFI_IMAGE_SCN_MEM_EXECUTE
605 | EFI_IMAGE_SCN_MEM_READ
);
607 // Don't make a section of size 0.
608 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
611 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
612 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
613 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
614 | EFI_IMAGE_SCN_MEM_WRITE
615 | EFI_IMAGE_SCN_MEM_READ
);
617 // Don't make a section of size 0.
618 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
621 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
622 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
623 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
624 | EFI_IMAGE_SCN_MEM_READ
);
626 NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
627 NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
629 // Don't make a section of size 0.
630 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
638 SECTION_FILTER_TYPES FilterType
644 BOOLEAN (*Filter
)(Elf_Shdr
*);
647 // Initialize filter pointer
649 switch (FilterType
) {
654 Filter
= IsHiiRsrcShdr
;
664 // First: copy sections.
666 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
667 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
668 if ((*Filter
)(Shdr
)) {
669 switch (Shdr
->sh_type
) {
672 if (Shdr
->sh_offset
+ Shdr
->sh_size
> mFileBufferSize
) {
675 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
676 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
681 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, Shdr
->sh_size
);
686 // Ignore for unknown section type.
688 VerboseMsg ("%s unknown section type %x. We ignore this unknown section type.", mInImageName
, (unsigned)Shdr
->sh_type
);
695 // Second: apply relocations.
697 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
699 // Determine if this is a relocation section.
701 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
702 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
707 // Relocation section found. Now extract section information that the relocations
708 // apply to in the ELF data and the new COFF data.
710 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
711 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
714 // Only process relocations for the current filter type.
716 if (RelShdr
->sh_type
== SHT_REL
&& (*Filter
)(SecShdr
)) {
720 // Determine the symbol table referenced by the relocation data.
722 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
723 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
726 // Process all relocation entries for this section.
728 for (RelOffset
= 0; RelOffset
< RelShdr
->sh_size
; RelOffset
+= RelShdr
->sh_entsize
) {
730 // Set pointer to relocation entry
732 Elf_Rel
*Rel
= (Elf_Rel
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelOffset
);
735 // Set pointer to symbol table entry associated with the relocation entry.
737 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
744 // Check section header index found in symbol table and get the section
747 if (Sym
->st_shndx
== SHN_UNDEF
748 || Sym
->st_shndx
>= mEhdr
->e_shnum
) {
749 const UINT8
*SymName
= GetSymName(Sym
);
750 if (SymName
== NULL
) {
751 SymName
= (const UINT8
*)"<unknown>";
755 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
758 // Convert the relocation data to a pointer into the coff file.
761 // r_offset is the virtual address of the storage unit to be relocated.
762 // sh_addr is the virtual address for the base of the section.
764 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
767 // Determine how to handle each relocation type based on the machine type.
769 if (mEhdr
->e_machine
== EM_386
) {
770 switch (ELF_R_TYPE(Rel
->r_info
)) {
775 // Absolute relocation.
776 // Converts Targ from a absolute virtual address to the absolute
779 *(UINT32
*)Targ
= *(UINT32
*)Targ
- SymShdr
->sh_addr
780 + mCoffSectionsOffset
[Sym
->st_shndx
];
784 // Relative relocation: Symbol - Ip + Addend
786 *(UINT32
*)Targ
= *(UINT32
*)Targ
787 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
788 - (SecOffset
- SecShdr
->sh_addr
);
791 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
793 } else if (mEhdr
->e_machine
== EM_ARM
) {
794 switch (ELF32_R_TYPE(Rel
->r_info
)) {
796 // No relocation - no action required
803 case R_ARM_THM_JUMP19
:
806 case R_ARM_THM_JUMP24
:
808 case R_ARM_MOVW_PREL_NC
:
809 case R_ARM_MOVT_PREL
:
810 case R_ARM_THM_MOVW_PREL_NC
:
811 case R_ARM_THM_MOVT_PREL
:
813 case R_ARM_THM_ALU_PREL_11_0
:
815 case R_ARM_REL32_NOI
:
816 case R_ARM_ALU_PC_G0_NC
:
817 case R_ARM_ALU_PC_G0
:
818 case R_ARM_ALU_PC_G1_NC
:
819 case R_ARM_ALU_PC_G1
:
820 case R_ARM_ALU_PC_G2
:
821 case R_ARM_LDR_PC_G1
:
822 case R_ARM_LDR_PC_G2
:
823 case R_ARM_LDRS_PC_G0
:
824 case R_ARM_LDRS_PC_G1
:
825 case R_ARM_LDRS_PC_G2
:
826 case R_ARM_LDC_PC_G0
:
827 case R_ARM_LDC_PC_G1
:
828 case R_ARM_LDC_PC_G2
:
829 case R_ARM_THM_JUMP11
:
830 case R_ARM_THM_JUMP8
:
832 case R_ARM_TLS_LDM32
:
834 // Thease are all PC-relative relocations and don't require modification
835 // GCC does not seem to have the concept of a application that just needs to get relocated.
838 case R_ARM_THM_MOVW_ABS_NC
:
839 // MOVW is only lower 16-bits of the addres
840 Address
= (UINT16
)(Sym
->st_value
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
841 ThumbMovtImmediatePatch ((UINT16
*)Targ
, Address
);
844 case R_ARM_THM_MOVT_ABS
:
845 // MOVT is only upper 16-bits of the addres
846 Address
= (UINT16
)((Sym
->st_value
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]) >> 16);
847 ThumbMovtImmediatePatch ((UINT16
*)Targ
, Address
);
853 // Absolute relocation.
855 *(UINT32
*)Targ
= *(UINT32
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
859 Error (NULL
, 0, 3000, "Invalid", "WriteSections (): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName
, (unsigned) ELF32_R_TYPE(Rel
->r_info
));
869 UINTN gMovwOffset
= 0;
878 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
879 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
880 BOOLEAN FoundRelocations
;
883 UINTN RelElementSize
;
887 Elf32_Phdr
*DynamicSegment
;
889 for (Index
= 0, FoundRelocations
= FALSE
; Index
< mEhdr
->e_shnum
; Index
++) {
890 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
891 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
892 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
893 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
896 FoundRelocations
= TRUE
;
897 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
898 Rel
= (Elf_Rel
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
900 if (mEhdr
->e_machine
== EM_386
) {
901 switch (ELF_R_TYPE(Rel
->r_info
)) {
905 // No fixup entry required.
910 // Creates a relative relocation entry from the absolute entry.
912 CoffAddFixup(mCoffSectionsOffset
[RelShdr
->sh_info
]
913 + (Rel
->r_offset
- SecShdr
->sh_addr
),
914 EFI_IMAGE_REL_BASED_HIGHLOW
);
917 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
919 } else if (mEhdr
->e_machine
== EM_ARM
) {
920 switch (ELF32_R_TYPE(Rel
->r_info
)) {
922 // No relocation - no action required
929 case R_ARM_THM_JUMP19
:
932 case R_ARM_THM_JUMP24
:
934 case R_ARM_MOVW_PREL_NC
:
935 case R_ARM_MOVT_PREL
:
936 case R_ARM_THM_MOVW_PREL_NC
:
937 case R_ARM_THM_MOVT_PREL
:
939 case R_ARM_THM_ALU_PREL_11_0
:
941 case R_ARM_REL32_NOI
:
942 case R_ARM_ALU_PC_G0_NC
:
943 case R_ARM_ALU_PC_G0
:
944 case R_ARM_ALU_PC_G1_NC
:
945 case R_ARM_ALU_PC_G1
:
946 case R_ARM_ALU_PC_G2
:
947 case R_ARM_LDR_PC_G1
:
948 case R_ARM_LDR_PC_G2
:
949 case R_ARM_LDRS_PC_G0
:
950 case R_ARM_LDRS_PC_G1
:
951 case R_ARM_LDRS_PC_G2
:
952 case R_ARM_LDC_PC_G0
:
953 case R_ARM_LDC_PC_G1
:
954 case R_ARM_LDC_PC_G2
:
955 case R_ARM_THM_JUMP11
:
956 case R_ARM_THM_JUMP8
:
958 case R_ARM_TLS_LDM32
:
960 // Thease are all PC-relative relocations and don't require modification
963 case R_ARM_THM_MOVW_ABS_NC
:
965 mCoffSectionsOffset
[RelShdr
->sh_info
]
966 + (Rel
->r_offset
- SecShdr
->sh_addr
),
967 EFI_IMAGE_REL_BASED_ARM_MOV32T
970 // PE/COFF treats MOVW/MOVT relocation as single 64-bit instruction
971 // Track this address so we can log an error for unsupported sequence of MOVW/MOVT
972 gMovwOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
);
975 case R_ARM_THM_MOVT_ABS
:
976 if ((gMovwOffset
+ 4) != (mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
))) {
977 Error (NULL
, 0, 3000, "Not Supported", "PE/COFF requires MOVW+MOVT instruction sequence %x +4 != %x.", gMovwOffset
, mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
984 mCoffSectionsOffset
[RelShdr
->sh_info
]
985 + (Rel
->r_offset
- SecShdr
->sh_addr
),
986 EFI_IMAGE_REL_BASED_HIGHLOW
991 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations(): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName
, (unsigned) ELF32_R_TYPE(Rel
->r_info
));
994 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
1001 if (!FoundRelocations
&& (mEhdr
->e_machine
== EM_ARM
)) {
1002 /* Try again, but look for PT_DYNAMIC instead of SHT_REL */
1004 for (Index
= 0; Index
< mEhdr
->e_phnum
; Index
++) {
1009 DynamicSegment
= GetPhdrByIndex (Index
);
1011 if (DynamicSegment
->p_type
== PT_DYNAMIC
) {
1012 Dyn
= (Elf32_Dyn
*) ((UINT8
*)mEhdr
+ DynamicSegment
->p_offset
);
1014 while (Dyn
->d_tag
!= DT_NULL
) {
1015 switch (Dyn
->d_tag
) {
1017 RelOffset
= Dyn
->d_un
.d_val
;
1021 RelSize
= Dyn
->d_un
.d_val
;
1025 RelElementSize
= Dyn
->d_un
.d_val
;
1033 if (( RelOffset
== 0 ) || ( RelSize
== 0 ) || ( RelElementSize
== 0 )) {
1034 Error (NULL
, 0, 3000, "Invalid", "%s bad ARM dynamic relocations.", mInImageName
);
1037 for (Index
= 0; Index
< mEhdr
->e_shnum
; Index
++) {
1038 Elf_Shdr
*shdr
= GetShdrByIndex(Index
);
1041 // The PT_DYNAMIC section contains DT_REL relocations whose r_offset
1042 // field is relative to the base of a segment (or the entire image),
1043 // and not to the base of an ELF input section as is the case for
1044 // SHT_REL sections. This means that we cannot fix up such relocations
1045 // unless we cross-reference ELF sections and segments, considering
1046 // that the output placement recorded in mCoffSectionsOffset[] is
1047 // section based, not segment based.
1049 // Fortunately, there is a simple way around this: we require that the
1050 // in-memory layout of the ELF and PE/COFF versions of the binary is
1051 // identical. That way, r_offset will retain its validity as a PE/COFF
1052 // image offset, and we can record it in the COFF fixup table
1055 if (shdr
->sh_addr
!= mCoffSectionsOffset
[Index
]) {
1056 Error (NULL
, 0, 3000,
1057 "Invalid", "%s: PT_DYNAMIC relocations require identical ELF and PE/COFF section offsets.",
1062 for (K
= 0; K
< RelSize
; K
+= RelElementSize
) {
1064 if (DynamicSegment
->p_paddr
== 0) {
1065 // Older versions of the ARM ELF (SWS ESPC 0003 B-02) specification define DT_REL
1066 // as an offset in the dynamic segment. p_paddr is defined to be zero for ARM tools
1067 Rel
= (Elf32_Rel
*) ((UINT8
*) mEhdr
+ DynamicSegment
->p_offset
+ RelOffset
+ K
);
1069 // This is how it reads in the generic ELF specification
1070 Rel
= (Elf32_Rel
*) ((UINT8
*) mEhdr
+ RelOffset
+ K
);
1073 switch (ELF32_R_TYPE (Rel
->r_info
)) {
1078 CoffAddFixup (Rel
->r_offset
, EFI_IMAGE_REL_BASED_HIGHLOW
);
1082 Error (NULL
, 0, 3000, "Invalid", "%s bad ARM dynamic relocations, unknown type %d.", mInImageName
, ELF32_R_TYPE (Rel
->r_info
));
1092 // Pad by adding empty entries.
1094 while (mCoffOffset
& (mCoffAlignment
- 1)) {
1095 CoffAddFixupEntry(0);
1098 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1099 Dir
= &NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1100 Dir
->Size
= mCoffOffset
- mRelocOffset
;
1101 if (Dir
->Size
== 0) {
1102 // If no relocations, null out the directory entry and don't add the .reloc section
1103 Dir
->VirtualAddress
= 0;
1104 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
1106 Dir
->VirtualAddress
= mRelocOffset
;
1107 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
1108 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1109 | EFI_IMAGE_SCN_MEM_DISCARDABLE
1110 | EFI_IMAGE_SCN_MEM_READ
);
1122 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1123 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
1124 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
1125 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
1127 Len
= strlen(mInImageName
) + 1;
1129 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ mDebugOffset
);
1130 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
1131 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
1132 Dir
->RVA
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1133 Dir
->FileOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1135 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
1136 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
1137 strcpy ((char *)(Nb10
+ 1), mInImageName
);
1140 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1141 DataDir
= &NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
1142 DataDir
->VirtualAddress
= mDebugOffset
;
1143 DataDir
->Size
= sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1152 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1157 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1158 NtHdr
->Pe32
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
1167 if (mCoffSectionsOffset
!= NULL
) {
1168 free (mCoffSectionsOffset
);