3 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
4 Portions copyright (c) 2013, ARM Ltd. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available
7 under the terms and conditions of the BSD License which accompanies this
8 distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "WinNtInclude.h"
29 #include <Common/UefiBaseTypes.h>
30 #include <IndustryStandard/PeImage.h>
32 #include "PeCoffLib.h"
33 #include "EfiUtilityMsgs.h"
36 #include "ElfConvert.h"
37 #include "Elf32Convert.h"
48 SECTION_FILTER_TYPES FilterType
76 // Rename ELF32 strucutres to common names to help when porting to ELF64.
78 typedef Elf32_Shdr Elf_Shdr
;
79 typedef Elf32_Ehdr Elf_Ehdr
;
80 typedef Elf32_Rel Elf_Rel
;
81 typedef Elf32_Sym Elf_Sym
;
82 typedef Elf32_Phdr Elf_Phdr
;
83 typedef Elf32_Dyn Elf_Dyn
;
84 #define ELFCLASS ELFCLASS32
85 #define ELF_R_TYPE(r) ELF32_R_TYPE(r)
86 #define ELF_R_SYM(r) ELF32_R_SYM(r)
89 // Well known ELF structures.
91 STATIC Elf_Ehdr
*mEhdr
;
92 STATIC Elf_Shdr
*mShdrBase
;
93 STATIC Elf_Phdr
*mPhdrBase
;
98 STATIC
const UINT32 mCoffAlignment
= 0x20;
101 // PE section alignment.
103 STATIC
const UINT16 mCoffNbrSections
= 5;
106 // ELF sections to offset in Coff file.
108 STATIC UINT32
*mCoffSectionsOffset
= NULL
;
111 // Offsets in COFF file
113 STATIC UINT32 mNtHdrOffset
;
114 STATIC UINT32 mTextOffset
;
115 STATIC UINT32 mDataOffset
;
116 STATIC UINT32 mHiiRsrcOffset
;
117 STATIC UINT32 mRelocOffset
;
120 // Initialization Function
125 ELF_FUNCTION_TABLE
*ElfFunctions
129 // Initialize data pointer and structures.
131 mEhdr
= (Elf_Ehdr
*) FileBuffer
;
134 // Check the ELF32 specific header information.
136 if (mEhdr
->e_ident
[EI_CLASS
] != ELFCLASS32
) {
137 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS32");
140 if (mEhdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
141 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
144 if ((mEhdr
->e_type
!= ET_EXEC
) && (mEhdr
->e_type
!= ET_DYN
)) {
145 Error (NULL
, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
148 if (!((mEhdr
->e_machine
== EM_386
) || (mEhdr
->e_machine
== EM_ARM
))) {
149 Error (NULL
, 0, 3000, "Unsupported", "ELF e_machine not EM_386 or EM_ARM");
152 if (mEhdr
->e_version
!= EV_CURRENT
) {
153 Error (NULL
, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr
->e_version
, EV_CURRENT
);
158 // Update section header pointers
160 mShdrBase
= (Elf_Shdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_shoff
);
161 mPhdrBase
= (Elf_Phdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_phoff
);
164 // Create COFF Section offset buffer and zero.
166 mCoffSectionsOffset
= (UINT32
*)malloc(mEhdr
->e_shnum
* sizeof (UINT32
));
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
)
194 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
203 if (num
>= mEhdr
->e_phnum
) {
207 return (Elf_Phdr
*)((UINT8
*)mPhdrBase
+ num
* mEhdr
->e_phentsize
);
216 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
228 return (BOOLEAN
) ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
);
237 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
239 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
248 if (IsHiiRsrcShdr(Shdr
)) {
251 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
255 // Elf functions interface implementation
265 EFI_IMAGE_DOS_HEADER
*DosHdr
;
266 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
277 // Coff file start with a DOS header.
279 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
280 mNtHdrOffset
= mCoffOffset
;
281 switch (mEhdr
->e_machine
) {
284 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS32
);
287 VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN
)mEhdr
->e_machine
);
288 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS32
);
292 mTableOffset
= mCoffOffset
;
293 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
296 // First text sections.
298 mCoffOffset
= CoffAlign(mCoffOffset
);
300 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
301 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
302 if (IsTextShdr(shdr
)) {
303 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
304 // the alignment field is valid
305 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
306 // if the section address is aligned we must align PE/COFF
307 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
308 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
309 // ARM RVCT tools have behavior outside of the ELF specification to try
310 // and make images smaller. If sh_addr is not aligned to sh_addralign
311 // then the section needs to preserve sh_addr MOD sh_addralign.
312 // Normally doing nothing here works great.
313 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
317 /* Relocate entry. */
318 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
319 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
320 CoffEntry
= mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
;
324 // Set mTextOffset with the offset of the first '.text' section
327 mTextOffset
= mCoffOffset
;
331 mCoffSectionsOffset
[i
] = mCoffOffset
;
332 mCoffOffset
+= shdr
->sh_size
;
338 Error (NULL
, 0, 3000, "Invalid", "Did not find any '.text' section.");
342 if (mEhdr
->e_machine
!= EM_ARM
) {
343 mCoffOffset
= CoffAlign(mCoffOffset
);
346 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
347 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
351 // Then data sections.
353 mDataOffset
= mCoffOffset
;
355 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
356 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
357 if (IsDataShdr(shdr
)) {
358 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
359 // the alignment field is valid
360 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
361 // if the section address is aligned we must align PE/COFF
362 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
363 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
364 // ARM RVCT tools have behavior outside of the ELF specification to try
365 // and make images smaller. If sh_addr is not aligned to sh_addralign
366 // then the section needs to preserve sh_addr MOD sh_addralign.
367 // Normally doing nothing here works great.
368 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
371 mCoffSectionsOffset
[i
] = mCoffOffset
;
372 mCoffOffset
+= shdr
->sh_size
;
376 mCoffOffset
= CoffAlign(mCoffOffset
);
378 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
379 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
383 // The HII resource sections.
385 mHiiRsrcOffset
= mCoffOffset
;
386 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
387 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
388 if (IsHiiRsrcShdr(shdr
)) {
389 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
390 // the alignment field is valid
391 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
392 // if the section address is aligned we must align PE/COFF
393 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
394 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
395 // ARM RVCT tools have behavior outside of the ELF specification to try
396 // and make images smaller. If sh_addr is not aligned to sh_addralign
397 // then the section needs to preserve sh_addr MOD sh_addralign.
398 // Normally doing nothing here works great.
399 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
402 if (shdr
->sh_size
!= 0) {
403 mCoffSectionsOffset
[i
] = mCoffOffset
;
404 mCoffOffset
+= shdr
->sh_size
;
405 mCoffOffset
= CoffAlign(mCoffOffset
);
406 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
412 mRelocOffset
= mCoffOffset
;
415 // Allocate base Coff file. Will be expanded later for relocations.
417 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
418 memset(mCoffFile
, 0, mCoffOffset
);
423 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
424 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
425 DosHdr
->e_lfanew
= mNtHdrOffset
;
427 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
429 NtHdr
->Pe32
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
431 switch (mEhdr
->e_machine
) {
433 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IA32
;
434 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
437 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_ARMT
;
438 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
441 VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN
)mEhdr
->e_machine
);
442 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IA32
;
443 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
446 NtHdr
->Pe32
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
447 NtHdr
->Pe32
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
448 mImageTimeStamp
= NtHdr
->Pe32
.FileHeader
.TimeDateStamp
;
449 NtHdr
->Pe32
.FileHeader
.PointerToSymbolTable
= 0;
450 NtHdr
->Pe32
.FileHeader
.NumberOfSymbols
= 0;
451 NtHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32
.OptionalHeader
);
452 NtHdr
->Pe32
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
453 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
454 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
455 | EFI_IMAGE_FILE_32BIT_MACHINE
;
457 NtHdr
->Pe32
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
458 NtHdr
->Pe32
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
459 NtHdr
->Pe32
.OptionalHeader
.SizeOfUninitializedData
= 0;
460 NtHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
462 NtHdr
->Pe32
.OptionalHeader
.BaseOfCode
= mTextOffset
;
464 NtHdr
->Pe32
.OptionalHeader
.BaseOfData
= mDataOffset
;
465 NtHdr
->Pe32
.OptionalHeader
.ImageBase
= 0;
466 NtHdr
->Pe32
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
467 NtHdr
->Pe32
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
468 NtHdr
->Pe32
.OptionalHeader
.SizeOfImage
= 0;
470 NtHdr
->Pe32
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
471 NtHdr
->Pe32
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
476 if ((mDataOffset
- mTextOffset
) > 0) {
477 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
478 EFI_IMAGE_SCN_CNT_CODE
479 | EFI_IMAGE_SCN_MEM_EXECUTE
480 | EFI_IMAGE_SCN_MEM_READ
);
482 // Don't make a section of size 0.
483 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
486 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
487 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
488 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
489 | EFI_IMAGE_SCN_MEM_WRITE
490 | EFI_IMAGE_SCN_MEM_READ
);
492 // Don't make a section of size 0.
493 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
496 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
497 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
498 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
499 | EFI_IMAGE_SCN_MEM_READ
);
501 NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
502 NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
504 // Don't make a section of size 0.
505 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
513 SECTION_FILTER_TYPES FilterType
519 BOOLEAN (*Filter
)(Elf_Shdr
*);
522 // Initialize filter pointer
524 switch (FilterType
) {
529 Filter
= IsHiiRsrcShdr
;
539 // First: copy sections.
541 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
542 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
543 if ((*Filter
)(Shdr
)) {
544 switch (Shdr
->sh_type
) {
547 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
548 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
553 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, Shdr
->sh_size
);
558 // Ignore for unkown section type.
560 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName
, (unsigned)Shdr
->sh_type
);
567 // Second: apply relocations.
569 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
571 // Determine if this is a relocation section.
573 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
574 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
579 // Relocation section found. Now extract section information that the relocations
580 // apply to in the ELF data and the new COFF data.
582 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
583 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
586 // Only process relocations for the current filter type.
588 if (RelShdr
->sh_type
== SHT_REL
&& (*Filter
)(SecShdr
)) {
592 // Determine the symbol table referenced by the relocation data.
594 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
595 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
598 // Process all relocation entries for this section.
600 for (RelOffset
= 0; RelOffset
< RelShdr
->sh_size
; RelOffset
+= RelShdr
->sh_entsize
) {
602 // Set pointer to relocation entry
604 Elf_Rel
*Rel
= (Elf_Rel
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelOffset
);
607 // Set pointer to symbol table entry associated with the relocation entry.
609 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
616 // Check section header index found in symbol table and get the section
619 if (Sym
->st_shndx
== SHN_UNDEF
620 || Sym
->st_shndx
== SHN_ABS
621 || Sym
->st_shndx
> mEhdr
->e_shnum
) {
622 Error (NULL
, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName
);
624 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
627 // Convert the relocation data to a pointer into the coff file.
630 // r_offset is the virtual address of the storage unit to be relocated.
631 // sh_addr is the virtual address for the base of the section.
633 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
636 // Determine how to handle each relocation type based on the machine type.
638 if (mEhdr
->e_machine
== EM_386
) {
639 switch (ELF_R_TYPE(Rel
->r_info
)) {
644 // Absolute relocation.
645 // Converts Targ from a absolute virtual address to the absolute
648 *(UINT32
*)Targ
= *(UINT32
*)Targ
- SymShdr
->sh_addr
649 + mCoffSectionsOffset
[Sym
->st_shndx
];
653 // Relative relocation: Symbol - Ip + Addend
655 *(UINT32
*)Targ
= *(UINT32
*)Targ
656 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
657 - (SecOffset
- SecShdr
->sh_addr
);
660 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
662 } else if (mEhdr
->e_machine
== EM_ARM
) {
663 switch (ELF32_R_TYPE(Rel
->r_info
)) {
665 // No relocation - no action required
671 case R_ARM_THM_JUMP19
:
674 case R_ARM_THM_JUMP24
:
676 case R_ARM_MOVW_PREL_NC
:
677 case R_ARM_MOVT_PREL
:
678 case R_ARM_THM_MOVW_PREL_NC
:
679 case R_ARM_THM_MOVT_PREL
:
681 case R_ARM_THM_ALU_PREL_11_0
:
683 case R_ARM_REL32_NOI
:
684 case R_ARM_ALU_PC_G0_NC
:
685 case R_ARM_ALU_PC_G0
:
686 case R_ARM_ALU_PC_G1_NC
:
687 case R_ARM_ALU_PC_G1
:
688 case R_ARM_ALU_PC_G2
:
689 case R_ARM_LDR_PC_G1
:
690 case R_ARM_LDR_PC_G2
:
691 case R_ARM_LDRS_PC_G0
:
692 case R_ARM_LDRS_PC_G1
:
693 case R_ARM_LDRS_PC_G2
:
694 case R_ARM_LDC_PC_G0
:
695 case R_ARM_LDC_PC_G1
:
696 case R_ARM_LDC_PC_G2
:
698 case R_ARM_THM_JUMP11
:
699 case R_ARM_THM_JUMP8
:
701 case R_ARM_TLS_LDM32
:
703 // Thease are all PC-relative relocations and don't require modification
704 // GCC does not seem to have the concept of a application that just needs to get relocated.
707 case R_ARM_THM_MOVW_ABS_NC
:
708 // MOVW is only lower 16-bits of the addres
709 Address
= (UINT16
)(Sym
->st_value
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
710 ThumbMovtImmediatePatch ((UINT16
*)Targ
, Address
);
713 case R_ARM_THM_MOVT_ABS
:
714 // MOVT is only upper 16-bits of the addres
715 Address
= (UINT16
)((Sym
->st_value
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]) >> 16);
716 ThumbMovtImmediatePatch ((UINT16
*)Targ
, Address
);
722 // Absolute relocation.
724 *(UINT32
*)Targ
= *(UINT32
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
728 Error (NULL
, 0, 3000, "Invalid", "WriteSections (): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName
, (unsigned) ELF32_R_TYPE(Rel
->r_info
));
738 UINTN gMovwOffset
= 0;
747 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
748 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
749 BOOLEAN FoundRelocations
;
752 UINTN RelElementSize
;
757 Elf32_Phdr
*DynamicSegment
;
758 Elf32_Phdr
*TargetSegment
;
760 for (Index
= 0, FoundRelocations
= FALSE
; Index
< mEhdr
->e_shnum
; Index
++) {
761 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
762 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
763 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
764 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
767 FoundRelocations
= TRUE
;
768 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
769 Elf_Rel
*Rel
= (Elf_Rel
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
771 if (mEhdr
->e_machine
== EM_386
) {
772 switch (ELF_R_TYPE(Rel
->r_info
)) {
776 // No fixup entry required.
781 // Creates a relative relocation entry from the absolute entry.
783 CoffAddFixup(mCoffSectionsOffset
[RelShdr
->sh_info
]
784 + (Rel
->r_offset
- SecShdr
->sh_addr
),
785 EFI_IMAGE_REL_BASED_HIGHLOW
);
788 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
790 } else if (mEhdr
->e_machine
== EM_ARM
) {
791 switch (ELF32_R_TYPE(Rel
->r_info
)) {
793 // No relocation - no action required
799 case R_ARM_THM_JUMP19
:
802 case R_ARM_THM_JUMP24
:
804 case R_ARM_MOVW_PREL_NC
:
805 case R_ARM_MOVT_PREL
:
806 case R_ARM_THM_MOVW_PREL_NC
:
807 case R_ARM_THM_MOVT_PREL
:
809 case R_ARM_THM_ALU_PREL_11_0
:
811 case R_ARM_REL32_NOI
:
812 case R_ARM_ALU_PC_G0_NC
:
813 case R_ARM_ALU_PC_G0
:
814 case R_ARM_ALU_PC_G1_NC
:
815 case R_ARM_ALU_PC_G1
:
816 case R_ARM_ALU_PC_G2
:
817 case R_ARM_LDR_PC_G1
:
818 case R_ARM_LDR_PC_G2
:
819 case R_ARM_LDRS_PC_G0
:
820 case R_ARM_LDRS_PC_G1
:
821 case R_ARM_LDRS_PC_G2
:
822 case R_ARM_LDC_PC_G0
:
823 case R_ARM_LDC_PC_G1
:
824 case R_ARM_LDC_PC_G2
:
826 case R_ARM_THM_JUMP11
:
827 case R_ARM_THM_JUMP8
:
829 case R_ARM_TLS_LDM32
:
831 // Thease are all PC-relative relocations and don't require modification
834 case R_ARM_THM_MOVW_ABS_NC
:
836 mCoffSectionsOffset
[RelShdr
->sh_info
]
837 + (Rel
->r_offset
- SecShdr
->sh_addr
),
838 EFI_IMAGE_REL_BASED_ARM_MOV32T
841 // PE/COFF treats MOVW/MOVT relocation as single 64-bit instruction
842 // Track this address so we can log an error for unsupported sequence of MOVW/MOVT
843 gMovwOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
);
846 case R_ARM_THM_MOVT_ABS
:
847 if ((gMovwOffset
+ 4) != (mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
))) {
848 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
));
855 mCoffSectionsOffset
[RelShdr
->sh_info
]
856 + (Rel
->r_offset
- SecShdr
->sh_addr
),
857 EFI_IMAGE_REL_BASED_HIGHLOW
862 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations(): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName
, (unsigned) ELF32_R_TYPE(Rel
->r_info
));
865 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
872 if (!FoundRelocations
&& (mEhdr
->e_machine
== EM_ARM
)) {
873 /* Try again, but look for PT_DYNAMIC instead of SHT_REL */
875 for (Index
= 0; Index
< mEhdr
->e_phnum
; Index
++) {
880 DynamicSegment
= GetPhdrByIndex (Index
);
882 if (DynamicSegment
->p_type
== PT_DYNAMIC
) {
883 Dyn
= (Elf32_Dyn
*) ((UINT8
*)mEhdr
+ DynamicSegment
->p_offset
);
885 while (Dyn
->d_tag
!= DT_NULL
) {
886 switch (Dyn
->d_tag
) {
888 RelOffset
= Dyn
->d_un
.d_val
;
892 RelSize
= Dyn
->d_un
.d_val
;
896 RelElementSize
= Dyn
->d_un
.d_val
;
904 if (( RelOffset
== 0 ) || ( RelSize
== 0 ) || ( RelElementSize
== 0 )) {
905 Error (NULL
, 0, 3000, "Invalid", "%s bad ARM dynamic relocations.", mInImageName
);
908 for (K
= 0; K
< RelSize
; K
+= RelElementSize
) {
910 if (DynamicSegment
->p_paddr
== 0) {
911 // Older versions of the ARM ELF (SWS ESPC 0003 B-02) specification define DT_REL
912 // as an offset in the dynamic segment. p_paddr is defined to be zero for ARM tools
913 Rel
= (Elf32_Rel
*) ((UINT8
*) mEhdr
+ DynamicSegment
->p_offset
+ RelOffset
+ K
);
915 // This is how it reads in the generic ELF specification
916 Rel
= (Elf32_Rel
*) ((UINT8
*) mEhdr
+ RelOffset
+ K
);
919 switch (ELF32_R_TYPE (Rel
->r_info
)) {
924 TargetSegment
= GetPhdrByIndex (ELF32_R_SYM (Rel
->r_info
) - 1);
926 // Note: r_offset in a memory address. Convert it to a pointer in the coff file.
927 Targ
= mCoffFile
+ mCoffSectionsOffset
[ ELF32_R_SYM( Rel
->r_info
) ] + Rel
->r_offset
- TargetSegment
->p_vaddr
;
929 *(UINT32
*)Targ
= *(UINT32
*)Targ
+ mCoffSectionsOffset
[ELF32_R_SYM( Rel
->r_info
)];
931 CoffAddFixup (mCoffSectionsOffset
[ELF32_R_SYM (Rel
->r_info
)] + (Rel
->r_offset
- TargetSegment
->p_vaddr
), EFI_IMAGE_REL_BASED_HIGHLOW
);
935 Error (NULL
, 0, 3000, "Invalid", "%s bad ARM dynamic relocations, unkown type %d.", mInImageName
, ELF32_R_TYPE (Rel
->r_info
));
945 // Pad by adding empty entries.
947 while (mCoffOffset
& (mCoffAlignment
- 1)) {
948 CoffAddFixupEntry(0);
951 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
952 Dir
= &NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
953 Dir
->Size
= mCoffOffset
- mRelocOffset
;
954 if (Dir
->Size
== 0) {
955 // If no relocations, null out the directory entry and don't add the .reloc section
956 Dir
->VirtualAddress
= 0;
957 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
959 Dir
->VirtualAddress
= mRelocOffset
;
960 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
961 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
962 | EFI_IMAGE_SCN_MEM_DISCARDABLE
963 | EFI_IMAGE_SCN_MEM_READ
);
976 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
977 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
978 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
979 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
981 Len
= strlen(mInImageName
) + 1;
982 DebugOffset
= mCoffOffset
;
984 mCoffOffset
+= sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)
985 + sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
)
987 mCoffOffset
= CoffAlign(mCoffOffset
);
989 mCoffFile
= realloc(mCoffFile
, mCoffOffset
);
990 memset(mCoffFile
+ DebugOffset
, 0, mCoffOffset
- DebugOffset
);
992 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ DebugOffset
);
993 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
994 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
995 Dir
->RVA
= DebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
996 Dir
->FileOffset
= DebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
998 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
999 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
1000 strcpy ((char *)(Nb10
+ 1), mInImageName
);
1003 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1004 DataDir
= &NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
1005 DataDir
->VirtualAddress
= DebugOffset
;
1006 DataDir
->Size
= mCoffOffset
- DebugOffset
;
1007 if (DataDir
->Size
== 0) {
1008 // If no debug, null out the directory entry and don't add the .debug section
1009 DataDir
->VirtualAddress
= 0;
1010 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
1012 DataDir
->VirtualAddress
= DebugOffset
;
1013 CreateSectionHeader (".debug", DebugOffset
, mCoffOffset
- DebugOffset
,
1014 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1015 | EFI_IMAGE_SCN_MEM_DISCARDABLE
1016 | EFI_IMAGE_SCN_MEM_READ
);
1027 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1032 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1033 NtHdr
->Pe32
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
1042 if (mCoffSectionsOffset
!= NULL
) {
1043 free (mCoffSectionsOffset
);