4 Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2013, 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"
31 #include <Common/UefiBaseTypes.h>
32 #include <IndustryStandard/PeImage.h>
34 #include "PeCoffLib.h"
35 #include "EfiUtilityMsgs.h"
38 #include "ElfConvert.h"
39 #include "Elf32Convert.h"
50 SECTION_FILTER_TYPES FilterType
78 // Rename ELF32 strucutres to common names to help when porting to ELF64.
80 typedef Elf32_Shdr Elf_Shdr
;
81 typedef Elf32_Ehdr Elf_Ehdr
;
82 typedef Elf32_Rel Elf_Rel
;
83 typedef Elf32_Sym Elf_Sym
;
84 typedef Elf32_Phdr Elf_Phdr
;
85 typedef Elf32_Dyn Elf_Dyn
;
86 #define ELFCLASS ELFCLASS32
87 #define ELF_R_TYPE(r) ELF32_R_TYPE(r)
88 #define ELF_R_SYM(r) ELF32_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 mEhdr
= (Elf_Ehdr
*) FileBuffer
;
137 // Check the ELF32 specific header information.
139 if (mEhdr
->e_ident
[EI_CLASS
] != ELFCLASS32
) {
140 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS32");
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_386
) || (mEhdr
->e_machine
== EM_ARM
))) {
152 Error (NULL
, 0, 3000, "Unsupported", "ELF e_machine not EM_386 or EM_ARM");
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 mShdrBase
= (Elf_Shdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_shoff
);
164 mPhdrBase
= (Elf_Phdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_phoff
);
167 // Create COFF Section offset buffer and zero.
169 mCoffSectionsOffset
= (UINT32
*)malloc(mEhdr
->e_shnum
* sizeof (UINT32
));
170 memset(mCoffSectionsOffset
, 0, mEhdr
->e_shnum
* sizeof(UINT32
));
173 // Fill in function pointers.
175 ElfFunctions
->ScanSections
= ScanSections32
;
176 ElfFunctions
->WriteSections
= WriteSections32
;
177 ElfFunctions
->WriteRelocations
= WriteRelocations32
;
178 ElfFunctions
->WriteDebug
= WriteDebug32
;
179 ElfFunctions
->SetImageSize
= SetImageSize32
;
180 ElfFunctions
->CleanUp
= CleanUp32
;
187 // Header by Index functions
195 if (Num
>= mEhdr
->e_shnum
) {
196 Error (NULL
, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num
);
200 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
209 if (num
>= mEhdr
->e_phnum
) {
210 Error (NULL
, 0, 3000, "Invalid", "GetPhdrByIndex: Index %u is too high.", num
);
214 return (Elf_Phdr
*)((UINT8
*)mPhdrBase
+ num
* mEhdr
->e_phentsize
);
223 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
232 return (Offset
+ 3) & ~3;
244 return (BOOLEAN
) ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
);
253 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
255 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
264 if (IsHiiRsrcShdr(Shdr
)) {
267 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
276 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
278 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_STRTAB_SECTION_NAME
) == 0);
288 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
289 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
290 if (IsStrtabShdr(shdr
)) {
303 if (Sym
->st_name
== 0) {
307 Elf_Shdr
*StrtabShdr
= FindStrtabShdr();
308 if (StrtabShdr
== NULL
) {
312 assert(Sym
->st_name
< StrtabShdr
->sh_size
);
314 UINT8
* StrtabContents
= (UINT8
*)mEhdr
+ StrtabShdr
->sh_offset
;
316 bool foundEnd
= false;
318 for (i
= Sym
->st_name
; (i
< StrtabShdr
->sh_size
) && !foundEnd
; i
++) {
319 foundEnd
= StrtabContents
[i
] == 0;
323 return StrtabContents
+ Sym
->st_name
;
327 // Elf functions interface implementation
337 EFI_IMAGE_DOS_HEADER
*DosHdr
;
338 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
341 BOOLEAN FoundSection
;
347 // Coff file start with a DOS header.
349 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
350 mNtHdrOffset
= mCoffOffset
;
351 switch (mEhdr
->e_machine
) {
354 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS32
);
357 VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN
)mEhdr
->e_machine
);
358 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS32
);
362 mTableOffset
= mCoffOffset
;
363 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
366 // Set mCoffAlignment to the maximum alignment of the input sections
369 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
370 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
371 if (shdr
->sh_addralign
<= mCoffAlignment
) {
374 if (IsTextShdr(shdr
) || IsDataShdr(shdr
) || IsHiiRsrcShdr(shdr
)) {
375 mCoffAlignment
= (UINT32
)shdr
->sh_addralign
;
380 // Move the PE/COFF header right before the first section. This will help us
381 // save space when converting to TE.
383 if (mCoffAlignment
> mCoffOffset
) {
384 mNtHdrOffset
+= mCoffAlignment
- mCoffOffset
;
385 mTableOffset
+= mCoffAlignment
- mCoffOffset
;
386 mCoffOffset
= mCoffAlignment
;
390 // First text sections.
392 mCoffOffset
= CoffAlign(mCoffOffset
);
393 mTextOffset
= mCoffOffset
;
394 FoundSection
= FALSE
;
396 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
397 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
398 if (IsTextShdr(shdr
)) {
399 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
400 // the alignment field is valid
401 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
402 // if the section address is aligned we must align PE/COFF
403 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
405 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
409 /* Relocate entry. */
410 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
411 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
412 CoffEntry
= mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
;
416 // Set mTextOffset with the offset of the first '.text' section
419 mTextOffset
= mCoffOffset
;
423 mCoffSectionsOffset
[i
] = mCoffOffset
;
424 mCoffOffset
+= shdr
->sh_size
;
430 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
434 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
435 mCoffOffset
= CoffAlign(mCoffOffset
);
437 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
438 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
442 // Then data sections.
444 mDataOffset
= mCoffOffset
;
445 FoundSection
= FALSE
;
447 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
448 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
449 if (IsDataShdr(shdr
)) {
450 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
451 // the alignment field is valid
452 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
453 // if the section address is aligned we must align PE/COFF
454 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
456 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
461 // Set mDataOffset with the offset of the first '.data' section
464 mDataOffset
= mCoffOffset
;
468 mCoffSectionsOffset
[i
] = mCoffOffset
;
469 mCoffOffset
+= shdr
->sh_size
;
474 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
475 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
479 // Make room for .debug data in .data (or .text if .data is empty) instead of
480 // putting it in a section of its own. This is explicitly allowed by the
481 // PE/COFF spec, and prevents bloat in the binary when using large values for
482 // section alignment.
484 if (SectionCount
> 0) {
485 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
487 mCoffOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
) +
488 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) +
489 strlen(mInImageName
) + 1;
491 mCoffOffset
= CoffAlign(mCoffOffset
);
492 if (SectionCount
== 0) {
493 mDataOffset
= mCoffOffset
;
497 // The HII resource sections.
499 mHiiRsrcOffset
= mCoffOffset
;
500 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
501 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
502 if (IsHiiRsrcShdr(shdr
)) {
503 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
504 // the alignment field is valid
505 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
506 // if the section address is aligned we must align PE/COFF
507 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
509 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
512 if (shdr
->sh_size
!= 0) {
513 mHiiRsrcOffset
= mCoffOffset
;
514 mCoffSectionsOffset
[i
] = mCoffOffset
;
515 mCoffOffset
+= shdr
->sh_size
;
516 mCoffOffset
= CoffAlign(mCoffOffset
);
517 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
523 mRelocOffset
= mCoffOffset
;
526 // Allocate base Coff file. Will be expanded later for relocations.
528 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
529 memset(mCoffFile
, 0, mCoffOffset
);
534 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
535 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
536 DosHdr
->e_lfanew
= mNtHdrOffset
;
538 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
540 NtHdr
->Pe32
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
542 switch (mEhdr
->e_machine
) {
544 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IA32
;
545 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
548 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_ARMT
;
549 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
552 VerboseMsg ("%s unknown e_machine type %hu. Assume IA-32", mInImageName
, mEhdr
->e_machine
);
553 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IA32
;
554 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
557 NtHdr
->Pe32
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
558 NtHdr
->Pe32
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
559 mImageTimeStamp
= NtHdr
->Pe32
.FileHeader
.TimeDateStamp
;
560 NtHdr
->Pe32
.FileHeader
.PointerToSymbolTable
= 0;
561 NtHdr
->Pe32
.FileHeader
.NumberOfSymbols
= 0;
562 NtHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32
.OptionalHeader
);
563 NtHdr
->Pe32
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
564 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
565 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
566 | EFI_IMAGE_FILE_32BIT_MACHINE
;
568 NtHdr
->Pe32
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
569 NtHdr
->Pe32
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
570 NtHdr
->Pe32
.OptionalHeader
.SizeOfUninitializedData
= 0;
571 NtHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
573 NtHdr
->Pe32
.OptionalHeader
.BaseOfCode
= mTextOffset
;
575 NtHdr
->Pe32
.OptionalHeader
.BaseOfData
= mDataOffset
;
576 NtHdr
->Pe32
.OptionalHeader
.ImageBase
= 0;
577 NtHdr
->Pe32
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
578 NtHdr
->Pe32
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
579 NtHdr
->Pe32
.OptionalHeader
.SizeOfImage
= 0;
581 NtHdr
->Pe32
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
582 NtHdr
->Pe32
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
587 if ((mDataOffset
- mTextOffset
) > 0) {
588 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
589 EFI_IMAGE_SCN_CNT_CODE
590 | EFI_IMAGE_SCN_MEM_EXECUTE
591 | EFI_IMAGE_SCN_MEM_READ
);
593 // Don't make a section of size 0.
594 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
597 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
598 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
599 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
600 | EFI_IMAGE_SCN_MEM_WRITE
601 | EFI_IMAGE_SCN_MEM_READ
);
603 // Don't make a section of size 0.
604 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
607 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
608 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
609 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
610 | EFI_IMAGE_SCN_MEM_READ
);
612 NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
613 NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
615 // Don't make a section of size 0.
616 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
624 SECTION_FILTER_TYPES FilterType
630 BOOLEAN (*Filter
)(Elf_Shdr
*);
633 // Initialize filter pointer
635 switch (FilterType
) {
640 Filter
= IsHiiRsrcShdr
;
650 // First: copy sections.
652 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
653 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
654 if ((*Filter
)(Shdr
)) {
655 switch (Shdr
->sh_type
) {
658 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
659 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
664 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, Shdr
->sh_size
);
669 // Ignore for unkown section type.
671 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName
, (unsigned)Shdr
->sh_type
);
678 // Second: apply relocations.
680 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
682 // Determine if this is a relocation section.
684 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
685 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
690 // Relocation section found. Now extract section information that the relocations
691 // apply to in the ELF data and the new COFF data.
693 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
694 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
697 // Only process relocations for the current filter type.
699 if (RelShdr
->sh_type
== SHT_REL
&& (*Filter
)(SecShdr
)) {
703 // Determine the symbol table referenced by the relocation data.
705 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
706 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
709 // Process all relocation entries for this section.
711 for (RelOffset
= 0; RelOffset
< RelShdr
->sh_size
; RelOffset
+= RelShdr
->sh_entsize
) {
713 // Set pointer to relocation entry
715 Elf_Rel
*Rel
= (Elf_Rel
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelOffset
);
718 // Set pointer to symbol table entry associated with the relocation entry.
720 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
727 // Check section header index found in symbol table and get the section
730 if (Sym
->st_shndx
== SHN_UNDEF
731 || Sym
->st_shndx
>= mEhdr
->e_shnum
) {
732 const UINT8
*SymName
= GetSymName(Sym
);
733 if (SymName
== NULL
) {
734 SymName
= (const UINT8
*)"<unknown>";
737 Error (NULL
, 0, 3000, "Invalid",
738 "%s: Bad definition for symbol '%s'@%#x or unsupported symbol type. "
739 "For example, absolute and undefined symbols are not supported.",
740 mInImageName
, SymName
, Sym
->st_value
);
744 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
747 // Convert the relocation data to a pointer into the coff file.
750 // r_offset is the virtual address of the storage unit to be relocated.
751 // sh_addr is the virtual address for the base of the section.
753 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
756 // Determine how to handle each relocation type based on the machine type.
758 if (mEhdr
->e_machine
== EM_386
) {
759 switch (ELF_R_TYPE(Rel
->r_info
)) {
764 // Absolute relocation.
765 // Converts Targ from a absolute virtual address to the absolute
768 *(UINT32
*)Targ
= *(UINT32
*)Targ
- SymShdr
->sh_addr
769 + mCoffSectionsOffset
[Sym
->st_shndx
];
773 // Relative relocation: Symbol - Ip + Addend
775 *(UINT32
*)Targ
= *(UINT32
*)Targ
776 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
777 - (SecOffset
- SecShdr
->sh_addr
);
780 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
782 } else if (mEhdr
->e_machine
== EM_ARM
) {
783 switch (ELF32_R_TYPE(Rel
->r_info
)) {
785 // No relocation - no action required
792 case R_ARM_THM_JUMP19
:
795 case R_ARM_THM_JUMP24
:
797 case R_ARM_MOVW_PREL_NC
:
798 case R_ARM_MOVT_PREL
:
799 case R_ARM_THM_MOVW_PREL_NC
:
800 case R_ARM_THM_MOVT_PREL
:
802 case R_ARM_THM_ALU_PREL_11_0
:
804 case R_ARM_REL32_NOI
:
805 case R_ARM_ALU_PC_G0_NC
:
806 case R_ARM_ALU_PC_G0
:
807 case R_ARM_ALU_PC_G1_NC
:
808 case R_ARM_ALU_PC_G1
:
809 case R_ARM_ALU_PC_G2
:
810 case R_ARM_LDR_PC_G1
:
811 case R_ARM_LDR_PC_G2
:
812 case R_ARM_LDRS_PC_G0
:
813 case R_ARM_LDRS_PC_G1
:
814 case R_ARM_LDRS_PC_G2
:
815 case R_ARM_LDC_PC_G0
:
816 case R_ARM_LDC_PC_G1
:
817 case R_ARM_LDC_PC_G2
:
819 case R_ARM_THM_JUMP11
:
820 case R_ARM_THM_JUMP8
:
822 case R_ARM_TLS_LDM32
:
824 // Thease are all PC-relative relocations and don't require modification
825 // GCC does not seem to have the concept of a application that just needs to get relocated.
828 case R_ARM_THM_MOVW_ABS_NC
:
829 // MOVW is only lower 16-bits of the addres
830 Address
= (UINT16
)(Sym
->st_value
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
831 ThumbMovtImmediatePatch ((UINT16
*)Targ
, Address
);
834 case R_ARM_THM_MOVT_ABS
:
835 // MOVT is only upper 16-bits of the addres
836 Address
= (UINT16
)((Sym
->st_value
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]) >> 16);
837 ThumbMovtImmediatePatch ((UINT16
*)Targ
, Address
);
843 // Absolute relocation.
845 *(UINT32
*)Targ
= *(UINT32
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
849 Error (NULL
, 0, 3000, "Invalid", "WriteSections (): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName
, (unsigned) ELF32_R_TYPE(Rel
->r_info
));
859 UINTN gMovwOffset
= 0;
868 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
869 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
870 BOOLEAN FoundRelocations
;
873 UINTN RelElementSize
;
877 Elf32_Phdr
*DynamicSegment
;
879 for (Index
= 0, FoundRelocations
= FALSE
; Index
< mEhdr
->e_shnum
; Index
++) {
880 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
881 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
882 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
883 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
886 FoundRelocations
= TRUE
;
887 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
888 Rel
= (Elf_Rel
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
890 if (mEhdr
->e_machine
== EM_386
) {
891 switch (ELF_R_TYPE(Rel
->r_info
)) {
895 // No fixup entry required.
900 // Creates a relative relocation entry from the absolute entry.
902 CoffAddFixup(mCoffSectionsOffset
[RelShdr
->sh_info
]
903 + (Rel
->r_offset
- SecShdr
->sh_addr
),
904 EFI_IMAGE_REL_BASED_HIGHLOW
);
907 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
909 } else if (mEhdr
->e_machine
== EM_ARM
) {
910 switch (ELF32_R_TYPE(Rel
->r_info
)) {
912 // No relocation - no action required
919 case R_ARM_THM_JUMP19
:
922 case R_ARM_THM_JUMP24
:
924 case R_ARM_MOVW_PREL_NC
:
925 case R_ARM_MOVT_PREL
:
926 case R_ARM_THM_MOVW_PREL_NC
:
927 case R_ARM_THM_MOVT_PREL
:
929 case R_ARM_THM_ALU_PREL_11_0
:
931 case R_ARM_REL32_NOI
:
932 case R_ARM_ALU_PC_G0_NC
:
933 case R_ARM_ALU_PC_G0
:
934 case R_ARM_ALU_PC_G1_NC
:
935 case R_ARM_ALU_PC_G1
:
936 case R_ARM_ALU_PC_G2
:
937 case R_ARM_LDR_PC_G1
:
938 case R_ARM_LDR_PC_G2
:
939 case R_ARM_LDRS_PC_G0
:
940 case R_ARM_LDRS_PC_G1
:
941 case R_ARM_LDRS_PC_G2
:
942 case R_ARM_LDC_PC_G0
:
943 case R_ARM_LDC_PC_G1
:
944 case R_ARM_LDC_PC_G2
:
946 case R_ARM_THM_JUMP11
:
947 case R_ARM_THM_JUMP8
:
949 case R_ARM_TLS_LDM32
:
951 // Thease are all PC-relative relocations and don't require modification
954 case R_ARM_THM_MOVW_ABS_NC
:
956 mCoffSectionsOffset
[RelShdr
->sh_info
]
957 + (Rel
->r_offset
- SecShdr
->sh_addr
),
958 EFI_IMAGE_REL_BASED_ARM_MOV32T
961 // PE/COFF treats MOVW/MOVT relocation as single 64-bit instruction
962 // Track this address so we can log an error for unsupported sequence of MOVW/MOVT
963 gMovwOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
);
966 case R_ARM_THM_MOVT_ABS
:
967 if ((gMovwOffset
+ 4) != (mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
))) {
968 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
));
975 mCoffSectionsOffset
[RelShdr
->sh_info
]
976 + (Rel
->r_offset
- SecShdr
->sh_addr
),
977 EFI_IMAGE_REL_BASED_HIGHLOW
982 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations(): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName
, (unsigned) ELF32_R_TYPE(Rel
->r_info
));
985 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
992 if (!FoundRelocations
&& (mEhdr
->e_machine
== EM_ARM
)) {
993 /* Try again, but look for PT_DYNAMIC instead of SHT_REL */
995 for (Index
= 0; Index
< mEhdr
->e_phnum
; Index
++) {
1000 DynamicSegment
= GetPhdrByIndex (Index
);
1002 if (DynamicSegment
->p_type
== PT_DYNAMIC
) {
1003 Dyn
= (Elf32_Dyn
*) ((UINT8
*)mEhdr
+ DynamicSegment
->p_offset
);
1005 while (Dyn
->d_tag
!= DT_NULL
) {
1006 switch (Dyn
->d_tag
) {
1008 RelOffset
= Dyn
->d_un
.d_val
;
1012 RelSize
= Dyn
->d_un
.d_val
;
1016 RelElementSize
= Dyn
->d_un
.d_val
;
1024 if (( RelOffset
== 0 ) || ( RelSize
== 0 ) || ( RelElementSize
== 0 )) {
1025 Error (NULL
, 0, 3000, "Invalid", "%s bad ARM dynamic relocations.", mInImageName
);
1028 for (Index
= 0; Index
< mEhdr
->e_shnum
; Index
++) {
1029 Elf_Shdr
*shdr
= GetShdrByIndex(Index
);
1032 // The PT_DYNAMIC section contains DT_REL relocations whose r_offset
1033 // field is relative to the base of a segment (or the entire image),
1034 // and not to the base of an ELF input section as is the case for
1035 // SHT_REL sections. This means that we cannot fix up such relocations
1036 // unless we cross-reference ELF sections and segments, considering
1037 // that the output placement recorded in mCoffSectionsOffset[] is
1038 // section based, not segment based.
1040 // Fortunately, there is a simple way around this: we require that the
1041 // in-memory layout of the ELF and PE/COFF versions of the binary is
1042 // identical. That way, r_offset will retain its validity as a PE/COFF
1043 // image offset, and we can record it in the COFF fixup table
1046 if (shdr
->sh_addr
!= mCoffSectionsOffset
[Index
]) {
1047 Error (NULL
, 0, 3000,
1048 "Invalid", "%s: PT_DYNAMIC relocations require identical ELF and PE/COFF section offsets.",
1053 for (K
= 0; K
< RelSize
; K
+= RelElementSize
) {
1055 if (DynamicSegment
->p_paddr
== 0) {
1056 // Older versions of the ARM ELF (SWS ESPC 0003 B-02) specification define DT_REL
1057 // as an offset in the dynamic segment. p_paddr is defined to be zero for ARM tools
1058 Rel
= (Elf32_Rel
*) ((UINT8
*) mEhdr
+ DynamicSegment
->p_offset
+ RelOffset
+ K
);
1060 // This is how it reads in the generic ELF specification
1061 Rel
= (Elf32_Rel
*) ((UINT8
*) mEhdr
+ RelOffset
+ K
);
1064 switch (ELF32_R_TYPE (Rel
->r_info
)) {
1069 CoffAddFixup (Rel
->r_offset
, EFI_IMAGE_REL_BASED_HIGHLOW
);
1073 Error (NULL
, 0, 3000, "Invalid", "%s bad ARM dynamic relocations, unkown type %d.", mInImageName
, ELF32_R_TYPE (Rel
->r_info
));
1083 // Pad by adding empty entries.
1085 while (mCoffOffset
& (mCoffAlignment
- 1)) {
1086 CoffAddFixupEntry(0);
1089 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1090 Dir
= &NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1091 Dir
->Size
= mCoffOffset
- mRelocOffset
;
1092 if (Dir
->Size
== 0) {
1093 // If no relocations, null out the directory entry and don't add the .reloc section
1094 Dir
->VirtualAddress
= 0;
1095 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
1097 Dir
->VirtualAddress
= mRelocOffset
;
1098 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
1099 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1100 | EFI_IMAGE_SCN_MEM_DISCARDABLE
1101 | EFI_IMAGE_SCN_MEM_READ
);
1113 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1114 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
1115 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
1116 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
1118 Len
= strlen(mInImageName
) + 1;
1120 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ mDebugOffset
);
1121 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
1122 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
1123 Dir
->RVA
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1124 Dir
->FileOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1126 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
1127 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
1128 strcpy ((char *)(Nb10
+ 1), mInImageName
);
1131 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1132 DataDir
= &NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
1133 DataDir
->VirtualAddress
= mDebugOffset
;
1134 DataDir
->Size
= Dir
->SizeOfData
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1143 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1148 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1149 NtHdr
->Pe32
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
1158 if (mCoffSectionsOffset
!= NULL
) {
1159 free (mCoffSectionsOffset
);