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"
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 memset(mCoffSectionsOffset
, 0, mEhdr
->e_shnum
* sizeof(UINT32
));
172 // Fill in function pointers.
174 ElfFunctions
->ScanSections
= ScanSections32
;
175 ElfFunctions
->WriteSections
= WriteSections32
;
176 ElfFunctions
->WriteRelocations
= WriteRelocations32
;
177 ElfFunctions
->WriteDebug
= WriteDebug32
;
178 ElfFunctions
->SetImageSize
= SetImageSize32
;
179 ElfFunctions
->CleanUp
= CleanUp32
;
186 // Header by Index functions
194 if (Num
>= mEhdr
->e_shnum
) {
195 Error (NULL
, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num
);
199 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
208 if (num
>= mEhdr
->e_phnum
) {
209 Error (NULL
, 0, 3000, "Invalid", "GetPhdrByIndex: Index %u is too high.", num
);
213 return (Elf_Phdr
*)((UINT8
*)mPhdrBase
+ num
* mEhdr
->e_phentsize
);
222 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
231 return (Offset
+ 3) & ~3;
243 return (BOOLEAN
) ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
);
252 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
254 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
263 if (IsHiiRsrcShdr(Shdr
)) {
266 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
270 // Elf functions interface implementation
280 EFI_IMAGE_DOS_HEADER
*DosHdr
;
281 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
284 BOOLEAN FoundSection
;
290 // Coff file start with a DOS header.
292 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
293 mNtHdrOffset
= mCoffOffset
;
294 switch (mEhdr
->e_machine
) {
297 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS32
);
300 VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN
)mEhdr
->e_machine
);
301 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS32
);
305 mTableOffset
= mCoffOffset
;
306 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
309 // Set mCoffAlignment to the maximum alignment of the input sections
312 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
313 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
314 if (shdr
->sh_addralign
<= mCoffAlignment
) {
317 if (IsTextShdr(shdr
) || IsDataShdr(shdr
) || IsHiiRsrcShdr(shdr
)) {
318 mCoffAlignment
= (UINT32
)shdr
->sh_addralign
;
323 // Move the PE/COFF header right before the first section. This will help us
324 // save space when converting to TE.
326 if (mCoffAlignment
> mCoffOffset
) {
327 mNtHdrOffset
+= mCoffAlignment
- mCoffOffset
;
328 mTableOffset
+= mCoffAlignment
- mCoffOffset
;
329 mCoffOffset
= mCoffAlignment
;
333 // First text sections.
335 mCoffOffset
= CoffAlign(mCoffOffset
);
336 mTextOffset
= mCoffOffset
;
337 FoundSection
= FALSE
;
339 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
340 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
341 if (IsTextShdr(shdr
)) {
342 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
343 // the alignment field is valid
344 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
345 // if the section address is aligned we must align PE/COFF
346 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
348 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
352 /* Relocate entry. */
353 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
354 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
355 CoffEntry
= mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
;
359 // Set mTextOffset with the offset of the first '.text' section
362 mTextOffset
= mCoffOffset
;
366 mCoffSectionsOffset
[i
] = mCoffOffset
;
367 mCoffOffset
+= shdr
->sh_size
;
373 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
377 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
378 mCoffOffset
= CoffAlign(mCoffOffset
);
380 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
381 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
385 // Then data sections.
387 mDataOffset
= mCoffOffset
;
388 FoundSection
= FALSE
;
390 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
391 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
392 if (IsDataShdr(shdr
)) {
393 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
394 // the alignment field is valid
395 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
396 // if the section address is aligned we must align PE/COFF
397 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
399 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
404 // Set mDataOffset with the offset of the first '.data' section
407 mDataOffset
= mCoffOffset
;
411 mCoffSectionsOffset
[i
] = mCoffOffset
;
412 mCoffOffset
+= shdr
->sh_size
;
417 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
418 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
422 // Make room for .debug data in .data (or .text if .data is empty) instead of
423 // putting it in a section of its own. This is explicitly allowed by the
424 // PE/COFF spec, and prevents bloat in the binary when using large values for
425 // section alignment.
427 if (SectionCount
> 0) {
428 mDebugOffset
= DebugRvaAlign(mCoffOffset
);
430 mCoffOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
) +
431 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) +
432 strlen(mInImageName
) + 1;
434 mCoffOffset
= CoffAlign(mCoffOffset
);
435 if (SectionCount
== 0) {
436 mDataOffset
= mCoffOffset
;
440 // The HII resource sections.
442 mHiiRsrcOffset
= mCoffOffset
;
443 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
444 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
445 if (IsHiiRsrcShdr(shdr
)) {
446 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
447 // the alignment field is valid
448 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
449 // if the section address is aligned we must align PE/COFF
450 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
452 Error (NULL
, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
455 if (shdr
->sh_size
!= 0) {
456 mHiiRsrcOffset
= mCoffOffset
;
457 mCoffSectionsOffset
[i
] = mCoffOffset
;
458 mCoffOffset
+= shdr
->sh_size
;
459 mCoffOffset
= CoffAlign(mCoffOffset
);
460 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
466 mRelocOffset
= mCoffOffset
;
469 // Allocate base Coff file. Will be expanded later for relocations.
471 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
472 memset(mCoffFile
, 0, mCoffOffset
);
477 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
478 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
479 DosHdr
->e_lfanew
= mNtHdrOffset
;
481 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
483 NtHdr
->Pe32
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
485 switch (mEhdr
->e_machine
) {
487 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IA32
;
488 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
491 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_ARMT
;
492 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
495 VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN
)mEhdr
->e_machine
);
496 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IA32
;
497 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
500 NtHdr
->Pe32
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
501 NtHdr
->Pe32
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
502 mImageTimeStamp
= NtHdr
->Pe32
.FileHeader
.TimeDateStamp
;
503 NtHdr
->Pe32
.FileHeader
.PointerToSymbolTable
= 0;
504 NtHdr
->Pe32
.FileHeader
.NumberOfSymbols
= 0;
505 NtHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32
.OptionalHeader
);
506 NtHdr
->Pe32
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
507 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
508 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
509 | EFI_IMAGE_FILE_32BIT_MACHINE
;
511 NtHdr
->Pe32
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
512 NtHdr
->Pe32
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
513 NtHdr
->Pe32
.OptionalHeader
.SizeOfUninitializedData
= 0;
514 NtHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
516 NtHdr
->Pe32
.OptionalHeader
.BaseOfCode
= mTextOffset
;
518 NtHdr
->Pe32
.OptionalHeader
.BaseOfData
= mDataOffset
;
519 NtHdr
->Pe32
.OptionalHeader
.ImageBase
= 0;
520 NtHdr
->Pe32
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
521 NtHdr
->Pe32
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
522 NtHdr
->Pe32
.OptionalHeader
.SizeOfImage
= 0;
524 NtHdr
->Pe32
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
525 NtHdr
->Pe32
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
530 if ((mDataOffset
- mTextOffset
) > 0) {
531 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
532 EFI_IMAGE_SCN_CNT_CODE
533 | EFI_IMAGE_SCN_MEM_EXECUTE
534 | EFI_IMAGE_SCN_MEM_READ
);
536 // Don't make a section of size 0.
537 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
540 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
541 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
542 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
543 | EFI_IMAGE_SCN_MEM_WRITE
544 | EFI_IMAGE_SCN_MEM_READ
);
546 // Don't make a section of size 0.
547 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
550 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
551 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
552 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
553 | EFI_IMAGE_SCN_MEM_READ
);
555 NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
556 NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
558 // Don't make a section of size 0.
559 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
567 SECTION_FILTER_TYPES FilterType
573 BOOLEAN (*Filter
)(Elf_Shdr
*);
576 // Initialize filter pointer
578 switch (FilterType
) {
583 Filter
= IsHiiRsrcShdr
;
593 // First: copy sections.
595 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
596 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
597 if ((*Filter
)(Shdr
)) {
598 switch (Shdr
->sh_type
) {
601 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
602 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
607 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, Shdr
->sh_size
);
612 // Ignore for unkown section type.
614 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName
, (unsigned)Shdr
->sh_type
);
621 // Second: apply relocations.
623 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
625 // Determine if this is a relocation section.
627 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
628 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
633 // Relocation section found. Now extract section information that the relocations
634 // apply to in the ELF data and the new COFF data.
636 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
637 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
640 // Only process relocations for the current filter type.
642 if (RelShdr
->sh_type
== SHT_REL
&& (*Filter
)(SecShdr
)) {
646 // Determine the symbol table referenced by the relocation data.
648 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
649 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
652 // Process all relocation entries for this section.
654 for (RelOffset
= 0; RelOffset
< RelShdr
->sh_size
; RelOffset
+= RelShdr
->sh_entsize
) {
656 // Set pointer to relocation entry
658 Elf_Rel
*Rel
= (Elf_Rel
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelOffset
);
661 // Set pointer to symbol table entry associated with the relocation entry.
663 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
670 // Check section header index found in symbol table and get the section
673 if (Sym
->st_shndx
== SHN_UNDEF
674 || Sym
->st_shndx
== SHN_ABS
675 || Sym
->st_shndx
> mEhdr
->e_shnum
) {
676 Error (NULL
, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName
);
678 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
681 // Convert the relocation data to a pointer into the coff file.
684 // r_offset is the virtual address of the storage unit to be relocated.
685 // sh_addr is the virtual address for the base of the section.
687 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
690 // Determine how to handle each relocation type based on the machine type.
692 if (mEhdr
->e_machine
== EM_386
) {
693 switch (ELF_R_TYPE(Rel
->r_info
)) {
698 // Absolute relocation.
699 // Converts Targ from a absolute virtual address to the absolute
702 *(UINT32
*)Targ
= *(UINT32
*)Targ
- SymShdr
->sh_addr
703 + mCoffSectionsOffset
[Sym
->st_shndx
];
707 // Relative relocation: Symbol - Ip + Addend
709 *(UINT32
*)Targ
= *(UINT32
*)Targ
710 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
711 - (SecOffset
- SecShdr
->sh_addr
);
714 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
716 } else if (mEhdr
->e_machine
== EM_ARM
) {
717 switch (ELF32_R_TYPE(Rel
->r_info
)) {
719 // No relocation - no action required
726 case R_ARM_THM_JUMP19
:
729 case R_ARM_THM_JUMP24
:
731 case R_ARM_MOVW_PREL_NC
:
732 case R_ARM_MOVT_PREL
:
733 case R_ARM_THM_MOVW_PREL_NC
:
734 case R_ARM_THM_MOVT_PREL
:
736 case R_ARM_THM_ALU_PREL_11_0
:
738 case R_ARM_REL32_NOI
:
739 case R_ARM_ALU_PC_G0_NC
:
740 case R_ARM_ALU_PC_G0
:
741 case R_ARM_ALU_PC_G1_NC
:
742 case R_ARM_ALU_PC_G1
:
743 case R_ARM_ALU_PC_G2
:
744 case R_ARM_LDR_PC_G1
:
745 case R_ARM_LDR_PC_G2
:
746 case R_ARM_LDRS_PC_G0
:
747 case R_ARM_LDRS_PC_G1
:
748 case R_ARM_LDRS_PC_G2
:
749 case R_ARM_LDC_PC_G0
:
750 case R_ARM_LDC_PC_G1
:
751 case R_ARM_LDC_PC_G2
:
753 case R_ARM_THM_JUMP11
:
754 case R_ARM_THM_JUMP8
:
756 case R_ARM_TLS_LDM32
:
758 // Thease are all PC-relative relocations and don't require modification
759 // GCC does not seem to have the concept of a application that just needs to get relocated.
762 case R_ARM_THM_MOVW_ABS_NC
:
763 // MOVW is only lower 16-bits of the addres
764 Address
= (UINT16
)(Sym
->st_value
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
765 ThumbMovtImmediatePatch ((UINT16
*)Targ
, Address
);
768 case R_ARM_THM_MOVT_ABS
:
769 // MOVT is only upper 16-bits of the addres
770 Address
= (UINT16
)((Sym
->st_value
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]) >> 16);
771 ThumbMovtImmediatePatch ((UINT16
*)Targ
, Address
);
777 // Absolute relocation.
779 *(UINT32
*)Targ
= *(UINT32
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
783 Error (NULL
, 0, 3000, "Invalid", "WriteSections (): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName
, (unsigned) ELF32_R_TYPE(Rel
->r_info
));
793 UINTN gMovwOffset
= 0;
802 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
803 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
804 BOOLEAN FoundRelocations
;
807 UINTN RelElementSize
;
811 Elf32_Phdr
*DynamicSegment
;
813 for (Index
= 0, FoundRelocations
= FALSE
; Index
< mEhdr
->e_shnum
; Index
++) {
814 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
815 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
816 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
817 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
820 FoundRelocations
= TRUE
;
821 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
822 Rel
= (Elf_Rel
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
824 if (mEhdr
->e_machine
== EM_386
) {
825 switch (ELF_R_TYPE(Rel
->r_info
)) {
829 // No fixup entry required.
834 // Creates a relative relocation entry from the absolute entry.
836 CoffAddFixup(mCoffSectionsOffset
[RelShdr
->sh_info
]
837 + (Rel
->r_offset
- SecShdr
->sh_addr
),
838 EFI_IMAGE_REL_BASED_HIGHLOW
);
841 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
843 } else if (mEhdr
->e_machine
== EM_ARM
) {
844 switch (ELF32_R_TYPE(Rel
->r_info
)) {
846 // No relocation - no action required
853 case R_ARM_THM_JUMP19
:
856 case R_ARM_THM_JUMP24
:
858 case R_ARM_MOVW_PREL_NC
:
859 case R_ARM_MOVT_PREL
:
860 case R_ARM_THM_MOVW_PREL_NC
:
861 case R_ARM_THM_MOVT_PREL
:
863 case R_ARM_THM_ALU_PREL_11_0
:
865 case R_ARM_REL32_NOI
:
866 case R_ARM_ALU_PC_G0_NC
:
867 case R_ARM_ALU_PC_G0
:
868 case R_ARM_ALU_PC_G1_NC
:
869 case R_ARM_ALU_PC_G1
:
870 case R_ARM_ALU_PC_G2
:
871 case R_ARM_LDR_PC_G1
:
872 case R_ARM_LDR_PC_G2
:
873 case R_ARM_LDRS_PC_G0
:
874 case R_ARM_LDRS_PC_G1
:
875 case R_ARM_LDRS_PC_G2
:
876 case R_ARM_LDC_PC_G0
:
877 case R_ARM_LDC_PC_G1
:
878 case R_ARM_LDC_PC_G2
:
880 case R_ARM_THM_JUMP11
:
881 case R_ARM_THM_JUMP8
:
883 case R_ARM_TLS_LDM32
:
885 // Thease are all PC-relative relocations and don't require modification
888 case R_ARM_THM_MOVW_ABS_NC
:
890 mCoffSectionsOffset
[RelShdr
->sh_info
]
891 + (Rel
->r_offset
- SecShdr
->sh_addr
),
892 EFI_IMAGE_REL_BASED_ARM_MOV32T
895 // PE/COFF treats MOVW/MOVT relocation as single 64-bit instruction
896 // Track this address so we can log an error for unsupported sequence of MOVW/MOVT
897 gMovwOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
);
900 case R_ARM_THM_MOVT_ABS
:
901 if ((gMovwOffset
+ 4) != (mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
))) {
902 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
));
909 mCoffSectionsOffset
[RelShdr
->sh_info
]
910 + (Rel
->r_offset
- SecShdr
->sh_addr
),
911 EFI_IMAGE_REL_BASED_HIGHLOW
916 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations(): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName
, (unsigned) ELF32_R_TYPE(Rel
->r_info
));
919 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
926 if (!FoundRelocations
&& (mEhdr
->e_machine
== EM_ARM
)) {
927 /* Try again, but look for PT_DYNAMIC instead of SHT_REL */
929 for (Index
= 0; Index
< mEhdr
->e_phnum
; Index
++) {
934 DynamicSegment
= GetPhdrByIndex (Index
);
936 if (DynamicSegment
->p_type
== PT_DYNAMIC
) {
937 Dyn
= (Elf32_Dyn
*) ((UINT8
*)mEhdr
+ DynamicSegment
->p_offset
);
939 while (Dyn
->d_tag
!= DT_NULL
) {
940 switch (Dyn
->d_tag
) {
942 RelOffset
= Dyn
->d_un
.d_val
;
946 RelSize
= Dyn
->d_un
.d_val
;
950 RelElementSize
= Dyn
->d_un
.d_val
;
958 if (( RelOffset
== 0 ) || ( RelSize
== 0 ) || ( RelElementSize
== 0 )) {
959 Error (NULL
, 0, 3000, "Invalid", "%s bad ARM dynamic relocations.", mInImageName
);
962 for (Index
= 0; Index
< mEhdr
->e_shnum
; Index
++) {
963 Elf_Shdr
*shdr
= GetShdrByIndex(Index
);
966 // The PT_DYNAMIC section contains DT_REL relocations whose r_offset
967 // field is relative to the base of a segment (or the entire image),
968 // and not to the base of an ELF input section as is the case for
969 // SHT_REL sections. This means that we cannot fix up such relocations
970 // unless we cross-reference ELF sections and segments, considering
971 // that the output placement recorded in mCoffSectionsOffset[] is
972 // section based, not segment based.
974 // Fortunately, there is a simple way around this: we require that the
975 // in-memory layout of the ELF and PE/COFF versions of the binary is
976 // identical. That way, r_offset will retain its validity as a PE/COFF
977 // image offset, and we can record it in the COFF fixup table
980 if (shdr
->sh_addr
!= mCoffSectionsOffset
[Index
]) {
981 Error (NULL
, 0, 3000,
982 "Invalid", "%s: PT_DYNAMIC relocations require identical ELF and PE/COFF section offsets.",
987 for (K
= 0; K
< RelSize
; K
+= RelElementSize
) {
989 if (DynamicSegment
->p_paddr
== 0) {
990 // Older versions of the ARM ELF (SWS ESPC 0003 B-02) specification define DT_REL
991 // as an offset in the dynamic segment. p_paddr is defined to be zero for ARM tools
992 Rel
= (Elf32_Rel
*) ((UINT8
*) mEhdr
+ DynamicSegment
->p_offset
+ RelOffset
+ K
);
994 // This is how it reads in the generic ELF specification
995 Rel
= (Elf32_Rel
*) ((UINT8
*) mEhdr
+ RelOffset
+ K
);
998 switch (ELF32_R_TYPE (Rel
->r_info
)) {
1003 CoffAddFixup (Rel
->r_offset
, EFI_IMAGE_REL_BASED_HIGHLOW
);
1007 Error (NULL
, 0, 3000, "Invalid", "%s bad ARM dynamic relocations, unkown type %d.", mInImageName
, ELF32_R_TYPE (Rel
->r_info
));
1017 // Pad by adding empty entries.
1019 while (mCoffOffset
& (mCoffAlignment
- 1)) {
1020 CoffAddFixupEntry(0);
1023 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1024 Dir
= &NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1025 Dir
->Size
= mCoffOffset
- mRelocOffset
;
1026 if (Dir
->Size
== 0) {
1027 // If no relocations, null out the directory entry and don't add the .reloc section
1028 Dir
->VirtualAddress
= 0;
1029 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
1031 Dir
->VirtualAddress
= mRelocOffset
;
1032 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
1033 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1034 | EFI_IMAGE_SCN_MEM_DISCARDABLE
1035 | EFI_IMAGE_SCN_MEM_READ
);
1047 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1048 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
1049 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
1050 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
1052 Len
= strlen(mInImageName
) + 1;
1054 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ mDebugOffset
);
1055 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
1056 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
1057 Dir
->RVA
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1058 Dir
->FileOffset
= mDebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1060 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
1061 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
1062 strcpy ((char *)(Nb10
+ 1), mInImageName
);
1065 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1066 DataDir
= &NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
1067 DataDir
->VirtualAddress
= mDebugOffset
;
1068 DataDir
->Size
= Dir
->SizeOfData
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
1077 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1082 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1083 NtHdr
->Pe32
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
1092 if (mCoffSectionsOffset
!= NULL
) {
1093 free (mCoffSectionsOffset
);