4 Copyright (c) 2010 - 2017, 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"
30 #include <Common/UefiBaseTypes.h>
31 #include <IndustryStandard/PeImage.h>
33 #include "PeCoffLib.h"
34 #include "EfiUtilityMsgs.h"
37 #include "ElfConvert.h"
38 #include "Elf32Convert.h"
49 SECTION_FILTER_TYPES FilterType
77 // Rename ELF32 strucutres to common names to help when porting to ELF64.
79 typedef Elf32_Shdr Elf_Shdr
;
80 typedef Elf32_Ehdr Elf_Ehdr
;
81 typedef Elf32_Rel Elf_Rel
;
82 typedef Elf32_Sym Elf_Sym
;
83 typedef Elf32_Phdr Elf_Phdr
;
84 typedef Elf32_Dyn Elf_Dyn
;
85 #define ELFCLASS ELFCLASS32
86 #define ELF_R_TYPE(r) ELF32_R_TYPE(r)
87 #define ELF_R_SYM(r) ELF32_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 UINT32 mCoffAlignment
= 0x20;
102 // PE section alignment.
104 STATIC
const UINT16 mCoffNbrSections
= 4;
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
;
119 STATIC UINT32 mDebugOffset
;
122 // Initialization Function
127 ELF_FUNCTION_TABLE
*ElfFunctions
131 // Initialize data pointer and structures.
133 mEhdr
= (Elf_Ehdr
*) FileBuffer
;
136 // Check the ELF32 specific header information.
138 if (mEhdr
->e_ident
[EI_CLASS
] != ELFCLASS32
) {
139 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS32");
142 if (mEhdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
143 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
146 if ((mEhdr
->e_type
!= ET_EXEC
) && (mEhdr
->e_type
!= ET_DYN
)) {
147 Error (NULL
, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
150 if (!((mEhdr
->e_machine
== EM_386
) || (mEhdr
->e_machine
== EM_ARM
))) {
151 Error (NULL
, 0, 3000, "Unsupported", "ELF e_machine not EM_386 or EM_ARM");
154 if (mEhdr
->e_version
!= EV_CURRENT
) {
155 Error (NULL
, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr
->e_version
, EV_CURRENT
);
160 // Update section header pointers
162 mShdrBase
= (Elf_Shdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_shoff
);
163 mPhdrBase
= (Elf_Phdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_phoff
);
166 // Create COFF Section offset buffer and zero.
168 mCoffSectionsOffset
= (UINT32
*)malloc(mEhdr
->e_shnum
* sizeof (UINT32
));
169 if (mCoffSectionsOffset
== NULL
) {
170 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
173 memset(mCoffSectionsOffset
, 0, mEhdr
->e_shnum
* sizeof(UINT32
));
176 // Fill in function pointers.
178 ElfFunctions
->ScanSections
= ScanSections32
;
179 ElfFunctions
->WriteSections
= WriteSections32
;
180 ElfFunctions
->WriteRelocations
= WriteRelocations32
;
181 ElfFunctions
->WriteDebug
= WriteDebug32
;
182 ElfFunctions
->SetImageSize
= SetImageSize32
;
183 ElfFunctions
->CleanUp
= CleanUp32
;
190 // Header by Index functions
198 if (Num
>= mEhdr
->e_shnum
) {
199 Error (NULL
, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num
);
203 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
212 if (num
>= mEhdr
->e_phnum
) {
213 Error (NULL
, 0, 3000, "Invalid", "GetPhdrByIndex: Index %u is too high.", num
);
217 return (Elf_Phdr
*)((UINT8
*)mPhdrBase
+ num
* mEhdr
->e_phentsize
);
226 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
235 return (Offset
+ 3) & ~3;
247 return (BOOLEAN
) ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
);
256 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
258 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
267 if (IsHiiRsrcShdr(Shdr
)) {
270 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
279 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
281 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_STRTAB_SECTION_NAME
) == 0);
291 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
292 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
293 if (IsStrtabShdr(shdr
)) {
306 Elf_Shdr
*StrtabShdr
;
307 UINT8
*StrtabContents
;
311 if (Sym
->st_name
== 0) {
315 StrtabShdr
= FindStrtabShdr();
316 if (StrtabShdr
== NULL
) {
320 assert(Sym
->st_name
< StrtabShdr
->sh_size
);
322 StrtabContents
= (UINT8
*)mEhdr
+ StrtabShdr
->sh_offset
;
325 for (i
= Sym
->st_name
; (i
< StrtabShdr
->sh_size
) && !foundEnd
; i
++) {
326 foundEnd
= (BOOLEAN
)(StrtabContents
[i
] == 0);
330 return StrtabContents
+ Sym
->st_name
;
334 // Elf functions interface implementation
344 EFI_IMAGE_DOS_HEADER
*DosHdr
;
345 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
348 BOOLEAN FoundSection
;
354 // Coff file start with a DOS header.
356 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
357 mNtHdrOffset
= mCoffOffset
;
358 switch (mEhdr
->e_machine
) {
361 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS32
);
364 VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN
)mEhdr
->e_machine
);
365 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS32
);
369 mTableOffset
= mCoffOffset
;
370 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
373 // Set mCoffAlignment to the maximum alignment of the input sections
376 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
377 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
378 if (shdr
->sh_addralign
<= mCoffAlignment
) {
381 if (IsTextShdr(shdr
) || IsDataShdr(shdr
) || IsHiiRsrcShdr(shdr
)) {
382 mCoffAlignment
= (UINT32
)shdr
->sh_addralign
;
387 // Move the PE/COFF header right before the first section. This will help us
388 // save space when converting to TE.
390 if (mCoffAlignment
> mCoffOffset
) {
391 mNtHdrOffset
+= mCoffAlignment
- mCoffOffset
;
392 mTableOffset
+= mCoffAlignment
- mCoffOffset
;
393 mCoffOffset
= mCoffAlignment
;
397 // First text sections.
399 mCoffOffset
= CoffAlign(mCoffOffset
);
400 mTextOffset
= mCoffOffset
;
401 FoundSection
= FALSE
;
403 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
404 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
405 if (IsTextShdr(shdr
)) {
406 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
407 // the alignment field is valid
408 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
409 // if the section address is aligned we must align PE/COFF
410 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
412 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
416 /* Relocate entry. */
417 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
418 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
419 CoffEntry
= mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
;
423 // Set mTextOffset with the offset of the first '.text' section
426 mTextOffset
= mCoffOffset
;
430 mCoffSectionsOffset
[i
] = mCoffOffset
;
431 mCoffOffset
+= shdr
->sh_size
;
437 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
441 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
442 mCoffOffset
= CoffAlign(mCoffOffset
);
444 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
445 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
449 // Then data sections.
451 mDataOffset
= mCoffOffset
;
452 FoundSection
= FALSE
;
454 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
455 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
456 if (IsDataShdr(shdr
)) {
457 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
458 // the alignment field is valid
459 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
460 // if the section address is aligned we must align PE/COFF
461 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
463 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
468 // Set mDataOffset with the offset of the first '.data' section
471 mDataOffset
= mCoffOffset
;
475 mCoffSectionsOffset
[i
] = mCoffOffset
;
476 mCoffOffset
+= shdr
->sh_size
;
481 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
482 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
486 // Make room for .debug data in .data (or .text if .data is empty) instead of
487 // putting it in a section of its own. This is explicitly allowed by the
488 // PE/COFF spec, and prevents bloat in the binary when using large values for
489 // section alignment.
491 if (SectionCount
> 0) {
492 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
494 mCoffOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
) +
495 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) +
496 strlen(mInImageName
) + 1;
498 mCoffOffset
= CoffAlign(mCoffOffset
);
499 if (SectionCount
== 0) {
500 mDataOffset
= mCoffOffset
;
504 // The HII resource sections.
506 mHiiRsrcOffset
= mCoffOffset
;
507 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
508 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
509 if (IsHiiRsrcShdr(shdr
)) {
510 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
511 // the alignment field is valid
512 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
513 // if the section address is aligned we must align PE/COFF
514 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
516 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
519 if (shdr
->sh_size
!= 0) {
520 mHiiRsrcOffset
= mCoffOffset
;
521 mCoffSectionsOffset
[i
] = mCoffOffset
;
522 mCoffOffset
+= shdr
->sh_size
;
523 mCoffOffset
= CoffAlign(mCoffOffset
);
524 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
530 mRelocOffset
= mCoffOffset
;
533 // Allocate base Coff file. Will be expanded later for relocations.
535 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
536 if (mCoffFile
== NULL
) {
537 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
539 assert (mCoffFile
!= NULL
);
540 memset(mCoffFile
, 0, mCoffOffset
);
545 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
546 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
547 DosHdr
->e_lfanew
= mNtHdrOffset
;
549 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
551 NtHdr
->Pe32
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
553 switch (mEhdr
->e_machine
) {
555 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IA32
;
556 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
559 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_ARMT
;
560 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
563 VerboseMsg ("%s unknown e_machine type %hu. Assume IA-32", mInImageName
, mEhdr
->e_machine
);
564 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IA32
;
565 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
568 NtHdr
->Pe32
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
569 NtHdr
->Pe32
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
570 mImageTimeStamp
= NtHdr
->Pe32
.FileHeader
.TimeDateStamp
;
571 NtHdr
->Pe32
.FileHeader
.PointerToSymbolTable
= 0;
572 NtHdr
->Pe32
.FileHeader
.NumberOfSymbols
= 0;
573 NtHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32
.OptionalHeader
);
574 NtHdr
->Pe32
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
575 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
576 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
577 | EFI_IMAGE_FILE_32BIT_MACHINE
;
579 NtHdr
->Pe32
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
580 NtHdr
->Pe32
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
581 NtHdr
->Pe32
.OptionalHeader
.SizeOfUninitializedData
= 0;
582 NtHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
584 NtHdr
->Pe32
.OptionalHeader
.BaseOfCode
= mTextOffset
;
586 NtHdr
->Pe32
.OptionalHeader
.BaseOfData
= mDataOffset
;
587 NtHdr
->Pe32
.OptionalHeader
.ImageBase
= 0;
588 NtHdr
->Pe32
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
589 NtHdr
->Pe32
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
590 NtHdr
->Pe32
.OptionalHeader
.SizeOfImage
= 0;
592 NtHdr
->Pe32
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
593 NtHdr
->Pe32
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
598 if ((mDataOffset
- mTextOffset
) > 0) {
599 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
600 EFI_IMAGE_SCN_CNT_CODE
601 | EFI_IMAGE_SCN_MEM_EXECUTE
602 | EFI_IMAGE_SCN_MEM_READ
);
604 // Don't make a section of size 0.
605 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
608 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
609 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
610 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
611 | EFI_IMAGE_SCN_MEM_WRITE
612 | EFI_IMAGE_SCN_MEM_READ
);
614 // Don't make a section of size 0.
615 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
618 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
619 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
620 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
621 | EFI_IMAGE_SCN_MEM_READ
);
623 NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
624 NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
626 // Don't make a section of size 0.
627 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
635 SECTION_FILTER_TYPES FilterType
641 BOOLEAN (*Filter
)(Elf_Shdr
*);
644 // Initialize filter pointer
646 switch (FilterType
) {
651 Filter
= IsHiiRsrcShdr
;
661 // First: copy sections.
663 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
664 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
665 if ((*Filter
)(Shdr
)) {
666 switch (Shdr
->sh_type
) {
669 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
670 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
675 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, Shdr
->sh_size
);
680 // Ignore for unkown section type.
682 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName
, (unsigned)Shdr
->sh_type
);
689 // Second: apply relocations.
691 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
693 // Determine if this is a relocation section.
695 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
696 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
701 // Relocation section found. Now extract section information that the relocations
702 // apply to in the ELF data and the new COFF data.
704 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
705 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
708 // Only process relocations for the current filter type.
710 if (RelShdr
->sh_type
== SHT_REL
&& (*Filter
)(SecShdr
)) {
714 // Determine the symbol table referenced by the relocation data.
716 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
717 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
720 // Process all relocation entries for this section.
722 for (RelOffset
= 0; RelOffset
< RelShdr
->sh_size
; RelOffset
+= RelShdr
->sh_entsize
) {
724 // Set pointer to relocation entry
726 Elf_Rel
*Rel
= (Elf_Rel
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelOffset
);
729 // Set pointer to symbol table entry associated with the relocation entry.
731 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
738 // Check section header index found in symbol table and get the section
741 if (Sym
->st_shndx
== SHN_UNDEF
742 || Sym
->st_shndx
>= mEhdr
->e_shnum
) {
743 const UINT8
*SymName
= GetSymName(Sym
);
744 if (SymName
== NULL
) {
745 SymName
= (const UINT8
*)"<unknown>";
748 Error (NULL
, 0, 3000, "Invalid",
749 "%s: Bad definition for symbol '%s'@%#x or unsupported symbol type. "
750 "For example, absolute and undefined symbols are not supported.",
751 mInImageName
, SymName
, Sym
->st_value
);
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
:
830 case R_ARM_THM_JUMP11
:
831 case R_ARM_THM_JUMP8
:
833 case R_ARM_TLS_LDM32
:
835 // Thease are all PC-relative relocations and don't require modification
836 // GCC does not seem to have the concept of a application that just needs to get relocated.
839 case R_ARM_THM_MOVW_ABS_NC
:
840 // MOVW is only lower 16-bits of the addres
841 Address
= (UINT16
)(Sym
->st_value
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
842 ThumbMovtImmediatePatch ((UINT16
*)Targ
, Address
);
845 case R_ARM_THM_MOVT_ABS
:
846 // MOVT is only upper 16-bits of the addres
847 Address
= (UINT16
)((Sym
->st_value
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]) >> 16);
848 ThumbMovtImmediatePatch ((UINT16
*)Targ
, Address
);
854 // Absolute relocation.
856 *(UINT32
*)Targ
= *(UINT32
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
860 Error (NULL
, 0, 3000, "Invalid", "WriteSections (): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName
, (unsigned) ELF32_R_TYPE(Rel
->r_info
));
870 UINTN gMovwOffset
= 0;
879 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
880 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
881 BOOLEAN FoundRelocations
;
884 UINTN RelElementSize
;
888 Elf32_Phdr
*DynamicSegment
;
890 for (Index
= 0, FoundRelocations
= FALSE
; Index
< mEhdr
->e_shnum
; Index
++) {
891 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
892 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
893 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
894 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
897 FoundRelocations
= TRUE
;
898 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
899 Rel
= (Elf_Rel
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
901 if (mEhdr
->e_machine
== EM_386
) {
902 switch (ELF_R_TYPE(Rel
->r_info
)) {
906 // No fixup entry required.
911 // Creates a relative relocation entry from the absolute entry.
913 CoffAddFixup(mCoffSectionsOffset
[RelShdr
->sh_info
]
914 + (Rel
->r_offset
- SecShdr
->sh_addr
),
915 EFI_IMAGE_REL_BASED_HIGHLOW
);
918 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
920 } else if (mEhdr
->e_machine
== EM_ARM
) {
921 switch (ELF32_R_TYPE(Rel
->r_info
)) {
923 // No relocation - no action required
930 case R_ARM_THM_JUMP19
:
933 case R_ARM_THM_JUMP24
:
935 case R_ARM_MOVW_PREL_NC
:
936 case R_ARM_MOVT_PREL
:
937 case R_ARM_THM_MOVW_PREL_NC
:
938 case R_ARM_THM_MOVT_PREL
:
940 case R_ARM_THM_ALU_PREL_11_0
:
942 case R_ARM_REL32_NOI
:
943 case R_ARM_ALU_PC_G0_NC
:
944 case R_ARM_ALU_PC_G0
:
945 case R_ARM_ALU_PC_G1_NC
:
946 case R_ARM_ALU_PC_G1
:
947 case R_ARM_ALU_PC_G2
:
948 case R_ARM_LDR_PC_G1
:
949 case R_ARM_LDR_PC_G2
:
950 case R_ARM_LDRS_PC_G0
:
951 case R_ARM_LDRS_PC_G1
:
952 case R_ARM_LDRS_PC_G2
:
953 case R_ARM_LDC_PC_G0
:
954 case R_ARM_LDC_PC_G1
:
955 case R_ARM_LDC_PC_G2
:
957 case R_ARM_THM_JUMP11
:
958 case R_ARM_THM_JUMP8
:
960 case R_ARM_TLS_LDM32
:
962 // Thease are all PC-relative relocations and don't require modification
965 case R_ARM_THM_MOVW_ABS_NC
:
967 mCoffSectionsOffset
[RelShdr
->sh_info
]
968 + (Rel
->r_offset
- SecShdr
->sh_addr
),
969 EFI_IMAGE_REL_BASED_ARM_MOV32T
972 // PE/COFF treats MOVW/MOVT relocation as single 64-bit instruction
973 // Track this address so we can log an error for unsupported sequence of MOVW/MOVT
974 gMovwOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
);
977 case R_ARM_THM_MOVT_ABS
:
978 if ((gMovwOffset
+ 4) != (mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
))) {
979 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
));
986 mCoffSectionsOffset
[RelShdr
->sh_info
]
987 + (Rel
->r_offset
- SecShdr
->sh_addr
),
988 EFI_IMAGE_REL_BASED_HIGHLOW
993 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations(): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName
, (unsigned) ELF32_R_TYPE(Rel
->r_info
));
996 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
1003 if (!FoundRelocations
&& (mEhdr
->e_machine
== EM_ARM
)) {
1004 /* Try again, but look for PT_DYNAMIC instead of SHT_REL */
1006 for (Index
= 0; Index
< mEhdr
->e_phnum
; Index
++) {
1011 DynamicSegment
= GetPhdrByIndex (Index
);
1013 if (DynamicSegment
->p_type
== PT_DYNAMIC
) {
1014 Dyn
= (Elf32_Dyn
*) ((UINT8
*)mEhdr
+ DynamicSegment
->p_offset
);
1016 while (Dyn
->d_tag
!= DT_NULL
) {
1017 switch (Dyn
->d_tag
) {
1019 RelOffset
= Dyn
->d_un
.d_val
;
1023 RelSize
= Dyn
->d_un
.d_val
;
1027 RelElementSize
= Dyn
->d_un
.d_val
;
1035 if (( RelOffset
== 0 ) || ( RelSize
== 0 ) || ( RelElementSize
== 0 )) {
1036 Error (NULL
, 0, 3000, "Invalid", "%s bad ARM dynamic relocations.", mInImageName
);
1039 for (Index
= 0; Index
< mEhdr
->e_shnum
; Index
++) {
1040 Elf_Shdr
*shdr
= GetShdrByIndex(Index
);
1043 // The PT_DYNAMIC section contains DT_REL relocations whose r_offset
1044 // field is relative to the base of a segment (or the entire image),
1045 // and not to the base of an ELF input section as is the case for
1046 // SHT_REL sections. This means that we cannot fix up such relocations
1047 // unless we cross-reference ELF sections and segments, considering
1048 // that the output placement recorded in mCoffSectionsOffset[] is
1049 // section based, not segment based.
1051 // Fortunately, there is a simple way around this: we require that the
1052 // in-memory layout of the ELF and PE/COFF versions of the binary is
1053 // identical. That way, r_offset will retain its validity as a PE/COFF
1054 // image offset, and we can record it in the COFF fixup table
1057 if (shdr
->sh_addr
!= mCoffSectionsOffset
[Index
]) {
1058 Error (NULL
, 0, 3000,
1059 "Invalid", "%s: PT_DYNAMIC relocations require identical ELF and PE/COFF section offsets.",
1064 for (K
= 0; K
< RelSize
; K
+= RelElementSize
) {
1066 if (DynamicSegment
->p_paddr
== 0) {
1067 // Older versions of the ARM ELF (SWS ESPC 0003 B-02) specification define DT_REL
1068 // as an offset in the dynamic segment. p_paddr is defined to be zero for ARM tools
1069 Rel
= (Elf32_Rel
*) ((UINT8
*) mEhdr
+ DynamicSegment
->p_offset
+ RelOffset
+ K
);
1071 // This is how it reads in the generic ELF specification
1072 Rel
= (Elf32_Rel
*) ((UINT8
*) mEhdr
+ RelOffset
+ K
);
1075 switch (ELF32_R_TYPE (Rel
->r_info
)) {
1080 CoffAddFixup (Rel
->r_offset
, EFI_IMAGE_REL_BASED_HIGHLOW
);
1084 Error (NULL
, 0, 3000, "Invalid", "%s bad ARM dynamic relocations, unkown type %d.", mInImageName
, ELF32_R_TYPE (Rel
->r_info
));
1094 // Pad by adding empty entries.
1096 while (mCoffOffset
& (mCoffAlignment
- 1)) {
1097 CoffAddFixupEntry(0);
1100 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1101 Dir
= &NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1102 Dir
->Size
= mCoffOffset
- mRelocOffset
;
1103 if (Dir
->Size
== 0) {
1104 // If no relocations, null out the directory entry and don't add the .reloc section
1105 Dir
->VirtualAddress
= 0;
1106 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
1108 Dir
->VirtualAddress
= mRelocOffset
;
1109 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
1110 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1111 | EFI_IMAGE_SCN_MEM_DISCARDABLE
1112 | EFI_IMAGE_SCN_MEM_READ
);
1124 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1125 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
1126 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
1127 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
1129 Len
= strlen(mInImageName
) + 1;
1131 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ mDebugOffset
);
1132 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
1133 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
1134 Dir
->RVA
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1135 Dir
->FileOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1137 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
1138 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
1139 strcpy ((char *)(Nb10
+ 1), mInImageName
);
1142 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1143 DataDir
= &NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
1144 DataDir
->VirtualAddress
= mDebugOffset
;
1145 DataDir
->Size
= sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1154 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1159 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1160 NtHdr
->Pe32
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
1169 if (mCoffSectionsOffset
!= NULL
) {
1170 free (mCoffSectionsOffset
);