3 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "WinNtInclude.h"
27 #include <Common/UefiBaseTypes.h>
28 #include <IndustryStandard/PeImage.h>
30 #include "PeCoffLib.h"
31 #include "EfiUtilityMsgs.h"
34 #include "ElfConvert.h"
35 #include "Elf32Convert.h"
46 SECTION_FILTER_TYPES FilterType
74 // Rename ELF32 strucutres to common names to help when porting to ELF64.
76 typedef Elf32_Shdr Elf_Shdr
;
77 typedef Elf32_Ehdr Elf_Ehdr
;
78 typedef Elf32_Rel Elf_Rel
;
79 typedef Elf32_Sym Elf_Sym
;
80 typedef Elf32_Phdr Elf_Phdr
;
81 typedef Elf32_Dyn Elf_Dyn
;
82 #define ELFCLASS ELFCLASS32
83 #define ELF_R_TYPE(r) ELF32_R_TYPE(r)
84 #define ELF_R_SYM(r) ELF32_R_SYM(r)
87 // Well known ELF structures.
89 STATIC Elf_Ehdr
*mEhdr
;
90 STATIC Elf_Shdr
*mShdrBase
;
91 STATIC Elf_Phdr
*mPhdrBase
;
96 STATIC
const UINT32 mCoffAlignment
= 0x20;
99 // PE section alignment.
101 STATIC
const UINT16 mCoffNbrSections
= 5;
104 // ELF sections to offset in Coff file.
106 STATIC UINT32
*mCoffSectionsOffset
= NULL
;
109 // Offsets in COFF file
111 STATIC UINT32 mNtHdrOffset
;
112 STATIC UINT32 mTextOffset
;
113 STATIC UINT32 mDataOffset
;
114 STATIC UINT32 mHiiRsrcOffset
;
115 STATIC UINT32 mRelocOffset
;
118 // Initialization Function
123 ELF_FUNCTION_TABLE
*ElfFunctions
127 // Initialize data pointer and structures.
129 mEhdr
= (Elf_Ehdr
*) FileBuffer
;
132 // Check the ELF32 specific header information.
134 if (mEhdr
->e_ident
[EI_CLASS
] != ELFCLASS32
) {
135 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS32");
138 if (mEhdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
139 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
142 if ((mEhdr
->e_type
!= ET_EXEC
) && (mEhdr
->e_type
!= ET_DYN
)) {
143 Error (NULL
, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
146 if (!((mEhdr
->e_machine
== EM_386
) || (mEhdr
->e_machine
== EM_ARM
))) {
147 Error (NULL
, 0, 3000, "Unsupported", "ELF e_machine not EM_386 or EM_ARM");
150 if (mEhdr
->e_version
!= EV_CURRENT
) {
151 Error (NULL
, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr
->e_version
, EV_CURRENT
);
156 // Update section header pointers
158 mShdrBase
= (Elf_Shdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_shoff
);
159 mPhdrBase
= (Elf_Phdr
*)((UINT8
*)mEhdr
+ mEhdr
->e_phoff
);
162 // Create COFF Section offset buffer and zero.
164 mCoffSectionsOffset
= (UINT32
*)malloc(mEhdr
->e_shnum
* sizeof (UINT32
));
165 memset(mCoffSectionsOffset
, 0, mEhdr
->e_shnum
* sizeof(UINT32
));
168 // Fill in function pointers.
170 ElfFunctions
->ScanSections
= ScanSections32
;
171 ElfFunctions
->WriteSections
= WriteSections32
;
172 ElfFunctions
->WriteRelocations
= WriteRelocations32
;
173 ElfFunctions
->WriteDebug
= WriteDebug32
;
174 ElfFunctions
->SetImageSize
= SetImageSize32
;
175 ElfFunctions
->CleanUp
= CleanUp32
;
182 // Header by Index functions
190 if (Num
>= mEhdr
->e_shnum
)
192 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
201 if (num
>= mEhdr
->e_phnum
) {
205 return (Elf_Phdr
*)((UINT8
*)mPhdrBase
+ num
* mEhdr
->e_phentsize
);
214 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
226 return (BOOLEAN
) ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
);
235 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
237 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
246 if (IsHiiRsrcShdr(Shdr
)) {
249 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
253 // Elf functions interface implementation
263 EFI_IMAGE_DOS_HEADER
*DosHdr
;
264 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
272 // Coff file start with a DOS header.
274 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
275 mNtHdrOffset
= mCoffOffset
;
276 switch (mEhdr
->e_machine
) {
279 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS32
);
282 VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN
)mEhdr
->e_machine
);
283 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS32
);
287 mTableOffset
= mCoffOffset
;
288 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
291 // First text sections.
293 mCoffOffset
= CoffAlign(mCoffOffset
);
294 mTextOffset
= mCoffOffset
;
296 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
297 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
298 if (IsTextShdr(shdr
)) {
299 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
300 // the alignment field is valid
301 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
302 // if the section address is aligned we must align PE/COFF
303 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
304 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
305 // ARM RVCT tools have behavior outside of the ELF specification to try
306 // and make images smaller. If sh_addr is not aligned to sh_addralign
307 // then the section needs to preserve sh_addr MOD sh_addralign.
308 // Normally doing nothing here works great.
309 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
313 /* Relocate entry. */
314 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
315 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
316 CoffEntry
= mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
;
318 mCoffSectionsOffset
[i
] = mCoffOffset
;
319 mCoffOffset
+= shdr
->sh_size
;
324 if (mEhdr
->e_machine
!= EM_ARM
) {
325 mCoffOffset
= CoffAlign(mCoffOffset
);
328 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
329 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
333 // Then data sections.
335 mDataOffset
= mCoffOffset
;
337 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
338 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
339 if (IsDataShdr(shdr
)) {
340 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
341 // the alignment field is valid
342 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
343 // if the section address is aligned we must align PE/COFF
344 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
345 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
346 // ARM RVCT tools have behavior outside of the ELF specification to try
347 // and make images smaller. If sh_addr is not aligned to sh_addralign
348 // then the section needs to preserve sh_addr MOD sh_addralign.
349 // Normally doing nothing here works great.
350 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
353 mCoffSectionsOffset
[i
] = mCoffOffset
;
354 mCoffOffset
+= shdr
->sh_size
;
358 mCoffOffset
= CoffAlign(mCoffOffset
);
360 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
361 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
365 // The HII resource sections.
367 mHiiRsrcOffset
= mCoffOffset
;
368 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
369 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
370 if (IsHiiRsrcShdr(shdr
)) {
371 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
372 // the alignment field is valid
373 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
374 // if the section address is aligned we must align PE/COFF
375 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
376 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
377 // ARM RVCT tools have behavior outside of the ELF specification to try
378 // and make images smaller. If sh_addr is not aligned to sh_addralign
379 // then the section needs to preserve sh_addr MOD sh_addralign.
380 // Normally doing nothing here works great.
381 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
384 if (shdr
->sh_size
!= 0) {
385 mCoffSectionsOffset
[i
] = mCoffOffset
;
386 mCoffOffset
+= shdr
->sh_size
;
387 mCoffOffset
= CoffAlign(mCoffOffset
);
388 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
394 mRelocOffset
= mCoffOffset
;
397 // Allocate base Coff file. Will be expanded later for relocations.
399 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
400 memset(mCoffFile
, 0, mCoffOffset
);
405 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
406 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
407 DosHdr
->e_lfanew
= mNtHdrOffset
;
409 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
411 NtHdr
->Pe32
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
413 switch (mEhdr
->e_machine
) {
415 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IA32
;
416 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
419 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_ARMT
;
420 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
423 VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN
)mEhdr
->e_machine
);
424 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IA32
;
425 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
428 NtHdr
->Pe32
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
429 NtHdr
->Pe32
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
430 mImageTimeStamp
= NtHdr
->Pe32
.FileHeader
.TimeDateStamp
;
431 NtHdr
->Pe32
.FileHeader
.PointerToSymbolTable
= 0;
432 NtHdr
->Pe32
.FileHeader
.NumberOfSymbols
= 0;
433 NtHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32
.OptionalHeader
);
434 NtHdr
->Pe32
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
435 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
436 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
437 | EFI_IMAGE_FILE_32BIT_MACHINE
;
439 NtHdr
->Pe32
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
440 NtHdr
->Pe32
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
441 NtHdr
->Pe32
.OptionalHeader
.SizeOfUninitializedData
= 0;
442 NtHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
444 NtHdr
->Pe32
.OptionalHeader
.BaseOfCode
= mTextOffset
;
446 NtHdr
->Pe32
.OptionalHeader
.BaseOfData
= mDataOffset
;
447 NtHdr
->Pe32
.OptionalHeader
.ImageBase
= 0;
448 NtHdr
->Pe32
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
449 NtHdr
->Pe32
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
450 NtHdr
->Pe32
.OptionalHeader
.SizeOfImage
= 0;
452 NtHdr
->Pe32
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
453 NtHdr
->Pe32
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
458 if ((mDataOffset
- mTextOffset
) > 0) {
459 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
460 EFI_IMAGE_SCN_CNT_CODE
461 | EFI_IMAGE_SCN_MEM_EXECUTE
462 | EFI_IMAGE_SCN_MEM_READ
);
464 // Don't make a section of size 0.
465 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
468 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
469 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
470 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
471 | EFI_IMAGE_SCN_MEM_WRITE
472 | EFI_IMAGE_SCN_MEM_READ
);
474 // Don't make a section of size 0.
475 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
478 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
479 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
480 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
481 | EFI_IMAGE_SCN_MEM_READ
);
483 NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
484 NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
486 // Don't make a section of size 0.
487 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
495 SECTION_FILTER_TYPES FilterType
501 BOOLEAN (*Filter
)(Elf_Shdr
*);
504 // Initialize filter pointer
506 switch (FilterType
) {
511 Filter
= IsHiiRsrcShdr
;
521 // First: copy sections.
523 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
524 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
525 if ((*Filter
)(Shdr
)) {
526 switch (Shdr
->sh_type
) {
529 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
530 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
535 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, Shdr
->sh_size
);
540 // Ignore for unkown section type.
542 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName
, (unsigned)Shdr
->sh_type
);
549 // Second: apply relocations.
551 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
553 // Determine if this is a relocation section.
555 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
556 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
561 // Relocation section found. Now extract section information that the relocations
562 // apply to in the ELF data and the new COFF data.
564 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
565 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
568 // Only process relocations for the current filter type.
570 if (RelShdr
->sh_type
== SHT_REL
&& (*Filter
)(SecShdr
)) {
574 // Determine the symbol table referenced by the relocation data.
576 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
577 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
580 // Process all relocation entries for this section.
582 for (RelOffset
= 0; RelOffset
< RelShdr
->sh_size
; RelOffset
+= RelShdr
->sh_entsize
) {
584 // Set pointer to relocation entry
586 Elf_Rel
*Rel
= (Elf_Rel
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelOffset
);
589 // Set pointer to symbol table entry associated with the relocation entry.
591 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
598 // Check section header index found in symbol table and get the section
601 if (Sym
->st_shndx
== SHN_UNDEF
602 || Sym
->st_shndx
== SHN_ABS
603 || Sym
->st_shndx
> mEhdr
->e_shnum
) {
604 Error (NULL
, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName
);
606 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
609 // Convert the relocation data to a pointer into the coff file.
612 // r_offset is the virtual address of the storage unit to be relocated.
613 // sh_addr is the virtual address for the base of the section.
615 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
618 // Determine how to handle each relocation type based on the machine type.
620 if (mEhdr
->e_machine
== EM_386
) {
621 switch (ELF_R_TYPE(Rel
->r_info
)) {
626 // Absolute relocation.
627 // Converts Targ from a absolute virtual address to the absolute
630 *(UINT32
*)Targ
= *(UINT32
*)Targ
- SymShdr
->sh_addr
631 + mCoffSectionsOffset
[Sym
->st_shndx
];
635 // Relative relocation: Symbol - Ip + Addend
637 *(UINT32
*)Targ
= *(UINT32
*)Targ
638 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
639 - (SecOffset
- SecShdr
->sh_addr
);
642 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
644 } else if (mEhdr
->e_machine
== EM_ARM
) {
645 switch (ELF32_R_TYPE(Rel
->r_info
)) {
647 // No relocation - no action required
653 case R_ARM_THM_JUMP19
:
656 case R_ARM_THM_JUMP24
:
658 case R_ARM_MOVW_PREL_NC
:
659 case R_ARM_MOVT_PREL
:
660 case R_ARM_THM_MOVW_PREL_NC
:
661 case R_ARM_THM_MOVT_PREL
:
663 case R_ARM_THM_ALU_PREL_11_0
:
665 case R_ARM_REL32_NOI
:
666 case R_ARM_ALU_PC_G0_NC
:
667 case R_ARM_ALU_PC_G0
:
668 case R_ARM_ALU_PC_G1_NC
:
669 case R_ARM_ALU_PC_G1
:
670 case R_ARM_ALU_PC_G2
:
671 case R_ARM_LDR_PC_G1
:
672 case R_ARM_LDR_PC_G2
:
673 case R_ARM_LDRS_PC_G0
:
674 case R_ARM_LDRS_PC_G1
:
675 case R_ARM_LDRS_PC_G2
:
676 case R_ARM_LDC_PC_G0
:
677 case R_ARM_LDC_PC_G1
:
678 case R_ARM_LDC_PC_G2
:
680 case R_ARM_THM_JUMP11
:
681 case R_ARM_THM_JUMP8
:
683 case R_ARM_TLS_LDM32
:
685 // Thease are all PC-relative relocations and don't require modification
686 // GCC does not seem to have the concept of a application that just needs to get relocated.
689 case R_ARM_THM_MOVW_ABS_NC
:
690 // MOVW is only lower 16-bits of the addres
691 Address
= (UINT16
)(Sym
->st_value
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
692 ThumbMovtImmediatePatch ((UINT16
*)Targ
, Address
);
695 case R_ARM_THM_MOVT_ABS
:
696 // MOVT is only upper 16-bits of the addres
697 Address
= (UINT16
)((Sym
->st_value
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]) >> 16);
698 ThumbMovtImmediatePatch ((UINT16
*)Targ
, Address
);
704 // Absolute relocation.
706 *(UINT32
*)Targ
= *(UINT32
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
710 Error (NULL
, 0, 3000, "Invalid", "WriteSections (): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName
, (unsigned) ELF32_R_TYPE(Rel
->r_info
));
720 UINTN gMovwOffset
= 0;
729 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
730 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
731 BOOLEAN FoundRelocations
;
734 UINTN RelElementSize
;
739 Elf32_Phdr
*DynamicSegment
;
740 Elf32_Phdr
*TargetSegment
;
742 Elf_Shdr
*SymtabShdr
;
746 for (Index
= 0, FoundRelocations
= FALSE
; Index
< mEhdr
->e_shnum
; Index
++) {
747 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
748 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
749 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
750 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
753 SymtabShdr
= GetShdrByIndex (RelShdr
->sh_link
);
754 Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
755 FoundRelocations
= TRUE
;
756 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
757 Elf_Rel
*Rel
= (Elf_Rel
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
760 Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
761 SymShdr
= GetShdrByIndex (Sym
->st_shndx
);
763 if (mEhdr
->e_machine
== EM_386
) {
764 switch (ELF_R_TYPE(Rel
->r_info
)) {
768 // No fixup entry required.
773 // Creates a relative relocation entry from the absolute entry.
775 CoffAddFixup(mCoffSectionsOffset
[RelShdr
->sh_info
]
776 + (Rel
->r_offset
- SecShdr
->sh_addr
),
777 EFI_IMAGE_REL_BASED_HIGHLOW
);
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
791 case R_ARM_THM_JUMP19
:
794 case R_ARM_THM_JUMP24
:
796 case R_ARM_MOVW_PREL_NC
:
797 case R_ARM_MOVT_PREL
:
798 case R_ARM_THM_MOVW_PREL_NC
:
799 case R_ARM_THM_MOVT_PREL
:
801 case R_ARM_THM_ALU_PREL_11_0
:
803 case R_ARM_REL32_NOI
:
804 case R_ARM_ALU_PC_G0_NC
:
805 case R_ARM_ALU_PC_G0
:
806 case R_ARM_ALU_PC_G1_NC
:
807 case R_ARM_ALU_PC_G1
:
808 case R_ARM_ALU_PC_G2
:
809 case R_ARM_LDR_PC_G1
:
810 case R_ARM_LDR_PC_G2
:
811 case R_ARM_LDRS_PC_G0
:
812 case R_ARM_LDRS_PC_G1
:
813 case R_ARM_LDRS_PC_G2
:
814 case R_ARM_LDC_PC_G0
:
815 case R_ARM_LDC_PC_G1
:
816 case R_ARM_LDC_PC_G2
:
818 case R_ARM_THM_JUMP11
:
819 case R_ARM_THM_JUMP8
:
821 case R_ARM_TLS_LDM32
:
823 // Thease are all PC-relative relocations and don't require modification
826 case R_ARM_THM_MOVW_ABS_NC
:
828 mCoffSectionsOffset
[RelShdr
->sh_info
]
829 + (Rel
->r_offset
- SecShdr
->sh_addr
),
830 EFI_IMAGE_REL_BASED_ARM_MOV32T
833 // PE/COFF treats MOVW/MOVT relocation as single 64-bit instruction
834 // Track this address so we can log an error for unsupported sequence of MOVW/MOVT
835 gMovwOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
);
838 case R_ARM_THM_MOVT_ABS
:
839 if ((gMovwOffset
+ 4) != (mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
))) {
840 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
));
847 mCoffSectionsOffset
[RelShdr
->sh_info
]
848 + (Rel
->r_offset
- SecShdr
->sh_addr
),
849 EFI_IMAGE_REL_BASED_HIGHLOW
854 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations(): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName
, (unsigned) ELF32_R_TYPE(Rel
->r_info
));
857 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
864 if (!FoundRelocations
&& (mEhdr
->e_machine
== EM_ARM
)) {
865 /* Try again, but look for PT_DYNAMIC instead of SHT_REL */
867 for (Index
= 0; Index
< mEhdr
->e_phnum
; Index
++) {
872 DynamicSegment
= GetPhdrByIndex (Index
);
874 if (DynamicSegment
->p_type
== PT_DYNAMIC
) {
875 Dyn
= (Elf32_Dyn
*) ((UINT8
*)mEhdr
+ DynamicSegment
->p_offset
);
877 while (Dyn
->d_tag
!= DT_NULL
) {
878 switch (Dyn
->d_tag
) {
880 RelOffset
= Dyn
->d_un
.d_val
;
884 RelSize
= Dyn
->d_un
.d_val
;
888 RelElementSize
= Dyn
->d_un
.d_val
;
896 if (( RelOffset
== 0 ) || ( RelSize
== 0 ) || ( RelElementSize
== 0 )) {
897 Error (NULL
, 0, 3000, "Invalid", "%s bad ARM dynamic relocations.", mInImageName
);
900 for (K
= 0; K
< RelSize
; K
+= RelElementSize
) {
902 if (DynamicSegment
->p_paddr
== 0) {
903 // Older versions of the ARM ELF (SWS ESPC 0003 B-02) specification define DT_REL
904 // as an offset in the dynamic segment. p_paddr is defined to be zero for ARM tools
905 Rel
= (Elf32_Rel
*) ((UINT8
*) mEhdr
+ DynamicSegment
->p_offset
+ RelOffset
+ K
);
907 // This is how it reads in the generic ELF specification
908 Rel
= (Elf32_Rel
*) ((UINT8
*) mEhdr
+ RelOffset
+ K
);
911 switch (ELF32_R_TYPE (Rel
->r_info
)) {
916 TargetSegment
= GetPhdrByIndex (ELF32_R_SYM (Rel
->r_info
) - 1);
918 // Note: r_offset in a memory address. Convert it to a pointer in the coff file.
919 Targ
= mCoffFile
+ mCoffSectionsOffset
[ ELF32_R_SYM( Rel
->r_info
) ] + Rel
->r_offset
- TargetSegment
->p_vaddr
;
921 *(UINT32
*)Targ
= *(UINT32
*)Targ
+ mCoffSectionsOffset
[ELF32_R_SYM( Rel
->r_info
)];
923 CoffAddFixup (mCoffSectionsOffset
[ELF32_R_SYM (Rel
->r_info
)] + (Rel
->r_offset
- TargetSegment
->p_vaddr
), EFI_IMAGE_REL_BASED_HIGHLOW
);
927 Error (NULL
, 0, 3000, "Invalid", "%s bad ARM dynamic relocations, unkown type %d.", mInImageName
, ELF32_R_TYPE (Rel
->r_info
));
937 // Pad by adding empty entries.
939 while (mCoffOffset
& (mCoffAlignment
- 1)) {
940 CoffAddFixupEntry(0);
943 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
944 Dir
= &NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
945 Dir
->Size
= mCoffOffset
- mRelocOffset
;
946 if (Dir
->Size
== 0) {
947 // If no relocations, null out the directory entry and don't add the .reloc section
948 Dir
->VirtualAddress
= 0;
949 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
951 Dir
->VirtualAddress
= mRelocOffset
;
952 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
953 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
954 | EFI_IMAGE_SCN_MEM_DISCARDABLE
955 | EFI_IMAGE_SCN_MEM_READ
);
968 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
969 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
970 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
971 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
973 Len
= strlen(mInImageName
) + 1;
974 DebugOffset
= mCoffOffset
;
976 mCoffOffset
+= sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)
977 + sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
)
979 mCoffOffset
= CoffAlign(mCoffOffset
);
981 mCoffFile
= realloc(mCoffFile
, mCoffOffset
);
982 memset(mCoffFile
+ DebugOffset
, 0, mCoffOffset
- DebugOffset
);
984 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ DebugOffset
);
985 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
986 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
987 Dir
->RVA
= DebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
988 Dir
->FileOffset
= DebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
990 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
991 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
992 strcpy ((char *)(Nb10
+ 1), mInImageName
);
995 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
996 DataDir
= &NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
997 DataDir
->VirtualAddress
= DebugOffset
;
998 DataDir
->Size
= mCoffOffset
- DebugOffset
;
999 if (DataDir
->Size
== 0) {
1000 // If no debug, null out the directory entry and don't add the .debug section
1001 DataDir
->VirtualAddress
= 0;
1002 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
1004 DataDir
->VirtualAddress
= DebugOffset
;
1005 CreateSectionHeader (".debug", DebugOffset
, mCoffOffset
- DebugOffset
,
1006 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1007 | EFI_IMAGE_SCN_MEM_DISCARDABLE
1008 | EFI_IMAGE_SCN_MEM_READ
);
1019 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1024 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1025 NtHdr
->Pe32
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
1034 if (mCoffSectionsOffset
!= NULL
) {
1035 free (mCoffSectionsOffset
);