3 Copyright (c) 2010, 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
;
271 // Coff file start with a DOS header.
273 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
274 mNtHdrOffset
= mCoffOffset
;
275 switch (mEhdr
->e_machine
) {
278 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS32
);
281 VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN
)mEhdr
->e_machine
);
282 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS32
);
286 mTableOffset
= mCoffOffset
;
287 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
290 // First text sections.
292 mCoffOffset
= CoffAlign(mCoffOffset
);
293 mTextOffset
= mCoffOffset
;
294 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
295 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
296 if (IsTextShdr(shdr
)) {
297 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
298 // the alignment field is valid
299 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
300 // if the section address is aligned we must align PE/COFF
301 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
302 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
303 // ARM RVCT tools have behavior outside of the ELF specification to try
304 // and make images smaller. If sh_addr is not aligned to sh_addralign
305 // then the section needs to preserve sh_addr MOD sh_addralign.
306 // Normally doing nothing here works great.
307 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
311 /* Relocate entry. */
312 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
313 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
314 CoffEntry
= mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
;
316 mCoffSectionsOffset
[i
] = mCoffOffset
;
317 mCoffOffset
+= shdr
->sh_size
;
321 if (mEhdr
->e_machine
!= EM_ARM
) {
322 mCoffOffset
= CoffAlign(mCoffOffset
);
326 // Then data sections.
328 mDataOffset
= mCoffOffset
;
329 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
330 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
331 if (IsDataShdr(shdr
)) {
332 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
333 // the alignment field is valid
334 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
335 // if the section address is aligned we must align PE/COFF
336 mCoffOffset
= (mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1);
337 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
338 // ARM RVCT tools have behavior outside of the ELF specification to try
339 // and make images smaller. If sh_addr is not aligned to sh_addralign
340 // then the section needs to preserve sh_addr MOD sh_addralign.
341 // Normally doing nothing here works great.
342 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
345 mCoffSectionsOffset
[i
] = mCoffOffset
;
346 mCoffOffset
+= shdr
->sh_size
;
349 mCoffOffset
= CoffAlign(mCoffOffset
);
352 // The HII resource sections.
354 mHiiRsrcOffset
= mCoffOffset
;
355 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
356 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
357 if (IsHiiRsrcShdr(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 if (shdr
->sh_size
!= 0) {
372 mCoffSectionsOffset
[i
] = mCoffOffset
;
373 mCoffOffset
+= shdr
->sh_size
;
374 mCoffOffset
= CoffAlign(mCoffOffset
);
375 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
381 mRelocOffset
= mCoffOffset
;
384 // Allocate base Coff file. Will be expanded later for relocations.
386 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
387 memset(mCoffFile
, 0, mCoffOffset
);
392 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
393 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
394 DosHdr
->e_lfanew
= mNtHdrOffset
;
396 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
398 NtHdr
->Pe32
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
400 switch (mEhdr
->e_machine
) {
402 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IA32
;
403 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
406 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_ARMT
;
407 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
410 VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN
)mEhdr
->e_machine
);
411 NtHdr
->Pe32
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IA32
;
412 NtHdr
->Pe32
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
;
415 NtHdr
->Pe32
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
416 NtHdr
->Pe32
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
417 mImageTimeStamp
= NtHdr
->Pe32
.FileHeader
.TimeDateStamp
;
418 NtHdr
->Pe32
.FileHeader
.PointerToSymbolTable
= 0;
419 NtHdr
->Pe32
.FileHeader
.NumberOfSymbols
= 0;
420 NtHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32
.OptionalHeader
);
421 NtHdr
->Pe32
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
422 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
423 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
424 | EFI_IMAGE_FILE_32BIT_MACHINE
;
426 NtHdr
->Pe32
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
427 NtHdr
->Pe32
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
428 NtHdr
->Pe32
.OptionalHeader
.SizeOfUninitializedData
= 0;
429 NtHdr
->Pe32
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
431 NtHdr
->Pe32
.OptionalHeader
.BaseOfCode
= mTextOffset
;
433 NtHdr
->Pe32
.OptionalHeader
.BaseOfData
= mDataOffset
;
434 NtHdr
->Pe32
.OptionalHeader
.ImageBase
= 0;
435 NtHdr
->Pe32
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
436 NtHdr
->Pe32
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
437 NtHdr
->Pe32
.OptionalHeader
.SizeOfImage
= 0;
439 NtHdr
->Pe32
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
440 NtHdr
->Pe32
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
445 if ((mDataOffset
- mTextOffset
) > 0) {
446 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
447 EFI_IMAGE_SCN_CNT_CODE
448 | EFI_IMAGE_SCN_MEM_EXECUTE
449 | EFI_IMAGE_SCN_MEM_READ
);
451 // Don't make a section of size 0.
452 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
455 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
456 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
457 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
458 | EFI_IMAGE_SCN_MEM_WRITE
459 | EFI_IMAGE_SCN_MEM_READ
);
461 // Don't make a section of size 0.
462 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
465 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
466 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
467 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
468 | EFI_IMAGE_SCN_MEM_READ
);
470 NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
471 NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
473 // Don't make a section of size 0.
474 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
482 SECTION_FILTER_TYPES FilterType
488 BOOLEAN (*Filter
)(Elf_Shdr
*);
491 // Initialize filter pointer
493 switch (FilterType
) {
498 Filter
= IsHiiRsrcShdr
;
508 // First: copy sections.
510 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
511 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
512 if ((*Filter
)(Shdr
)) {
513 switch (Shdr
->sh_type
) {
516 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
517 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
522 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, Shdr
->sh_size
);
527 // Ignore for unkown section type.
529 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName
, (unsigned)Shdr
->sh_type
);
536 // Second: apply relocations.
538 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
540 // Determine if this is a relocation section.
542 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
543 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
548 // Relocation section found. Now extract section information that the relocations
549 // apply to in the ELF data and the new COFF data.
551 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
552 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
555 // Only process relocations for the current filter type.
557 if (RelShdr
->sh_type
== SHT_REL
&& (*Filter
)(SecShdr
)) {
561 // Determine the symbol table referenced by the relocation data.
563 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
564 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
567 // Process all relocation entries for this section.
569 for (RelOffset
= 0; RelOffset
< RelShdr
->sh_size
; RelOffset
+= RelShdr
->sh_entsize
) {
571 // Set pointer to relocation entry
573 Elf_Rel
*Rel
= (Elf_Rel
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelOffset
);
576 // Set pointer to symbol table entry associated with the relocation entry.
578 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
585 // Check section header index found in symbol table and get the section
588 if (Sym
->st_shndx
== SHN_UNDEF
589 || Sym
->st_shndx
== SHN_ABS
590 || Sym
->st_shndx
> mEhdr
->e_shnum
) {
591 Error (NULL
, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName
);
593 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
596 // Convert the relocation data to a pointer into the coff file.
599 // r_offset is the virtual address of the storage unit to be relocated.
600 // sh_addr is the virtual address for the base of the section.
602 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
605 // Determine how to handle each relocation type based on the machine type.
607 if (mEhdr
->e_machine
== EM_386
) {
608 switch (ELF_R_TYPE(Rel
->r_info
)) {
613 // Absolute relocation.
614 // Converts Targ from a absolute virtual address to the absolute
617 *(UINT32
*)Targ
= *(UINT32
*)Targ
- SymShdr
->sh_addr
618 + mCoffSectionsOffset
[Sym
->st_shndx
];
622 // Relative relocation: Symbol - Ip + Addend
624 *(UINT32
*)Targ
= *(UINT32
*)Targ
625 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
626 - (SecOffset
- SecShdr
->sh_addr
);
629 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
631 } else if (mEhdr
->e_machine
== EM_ARM
) {
632 switch (ELF32_R_TYPE(Rel
->r_info
)) {
634 // No relocation - no action required
640 case R_ARM_THM_JUMP19
:
643 case R_ARM_THM_JUMP24
:
645 case R_ARM_MOVW_PREL_NC
:
646 case R_ARM_MOVT_PREL
:
647 case R_ARM_THM_MOVW_PREL_NC
:
648 case R_ARM_THM_MOVT_PREL
:
650 case R_ARM_THM_ALU_PREL_11_0
:
652 case R_ARM_REL32_NOI
:
653 case R_ARM_ALU_PC_G0_NC
:
654 case R_ARM_ALU_PC_G0
:
655 case R_ARM_ALU_PC_G1_NC
:
656 case R_ARM_ALU_PC_G1
:
657 case R_ARM_ALU_PC_G2
:
658 case R_ARM_LDR_PC_G1
:
659 case R_ARM_LDR_PC_G2
:
660 case R_ARM_LDRS_PC_G0
:
661 case R_ARM_LDRS_PC_G1
:
662 case R_ARM_LDRS_PC_G2
:
663 case R_ARM_LDC_PC_G0
:
664 case R_ARM_LDC_PC_G1
:
665 case R_ARM_LDC_PC_G2
:
667 case R_ARM_THM_JUMP11
:
668 case R_ARM_THM_JUMP8
:
670 case R_ARM_TLS_LDM32
:
672 // Thease are all PC-relative relocations and don't require modification
673 // GCC does not seem to have the concept of a application that just needs to get relocated.
676 case R_ARM_THM_MOVW_ABS_NC
:
677 // MOVW is only lower 16-bits of the addres
678 Address
= (UINT16
)(Sym
->st_value
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
679 ThumbMovtImmediatePatch ((UINT16
*)Targ
, Address
);
682 case R_ARM_THM_MOVT_ABS
:
683 // MOVT is only upper 16-bits of the addres
684 Address
= (UINT16
)((Sym
->st_value
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]) >> 16);
685 ThumbMovtImmediatePatch ((UINT16
*)Targ
, Address
);
691 // Absolute relocation.
693 *(UINT32
*)Targ
= *(UINT32
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
697 Error (NULL
, 0, 3000, "Invalid", "WriteSections (): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName
, (unsigned) ELF32_R_TYPE(Rel
->r_info
));
714 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
715 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
716 BOOLEAN FoundRelocations
;
719 UINTN RelElementSize
;
724 Elf32_Phdr
*DynamicSegment
;
725 Elf32_Phdr
*TargetSegment
;
727 Elf_Shdr
*SymtabShdr
;
731 for (Index
= 0, FoundRelocations
= FALSE
; Index
< mEhdr
->e_shnum
; Index
++) {
732 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
733 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
734 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
735 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
738 SymtabShdr
= GetShdrByIndex (RelShdr
->sh_link
);
739 Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
740 FoundRelocations
= TRUE
;
741 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
742 Elf_Rel
*Rel
= (Elf_Rel
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
745 Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
746 SymShdr
= GetShdrByIndex (Sym
->st_shndx
);
748 if (mEhdr
->e_machine
== EM_386
) {
749 switch (ELF_R_TYPE(Rel
->r_info
)) {
753 // No fixup entry required.
758 // Creates a relative relocation entry from the absolute entry.
760 CoffAddFixup(mCoffSectionsOffset
[RelShdr
->sh_info
]
761 + (Rel
->r_offset
- SecShdr
->sh_addr
),
762 EFI_IMAGE_REL_BASED_HIGHLOW
);
765 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
767 } else if (mEhdr
->e_machine
== EM_ARM
) {
768 switch (ELF32_R_TYPE(Rel
->r_info
)) {
770 // No relocation - no action required
776 case R_ARM_THM_JUMP19
:
779 case R_ARM_THM_JUMP24
:
781 case R_ARM_MOVW_PREL_NC
:
782 case R_ARM_MOVT_PREL
:
783 case R_ARM_THM_MOVW_PREL_NC
:
784 case R_ARM_THM_MOVT_PREL
:
786 case R_ARM_THM_ALU_PREL_11_0
:
788 case R_ARM_REL32_NOI
:
789 case R_ARM_ALU_PC_G0_NC
:
790 case R_ARM_ALU_PC_G0
:
791 case R_ARM_ALU_PC_G1_NC
:
792 case R_ARM_ALU_PC_G1
:
793 case R_ARM_ALU_PC_G2
:
794 case R_ARM_LDR_PC_G1
:
795 case R_ARM_LDR_PC_G2
:
796 case R_ARM_LDRS_PC_G0
:
797 case R_ARM_LDRS_PC_G1
:
798 case R_ARM_LDRS_PC_G2
:
799 case R_ARM_LDC_PC_G0
:
800 case R_ARM_LDC_PC_G1
:
801 case R_ARM_LDC_PC_G2
:
803 case R_ARM_THM_JUMP11
:
804 case R_ARM_THM_JUMP8
:
806 case R_ARM_TLS_LDM32
:
808 // Thease are all PC-relative relocations and don't require modification
811 case R_ARM_THM_MOVW_ABS_NC
:
813 mCoffSectionsOffset
[RelShdr
->sh_info
]
814 + (Rel
->r_offset
- SecShdr
->sh_addr
),
815 EFI_IMAGE_REL_BASED_ARM_THUMB_MOVW
819 case R_ARM_THM_MOVT_ABS
:
821 mCoffSectionsOffset
[RelShdr
->sh_info
]
822 + (Rel
->r_offset
- SecShdr
->sh_addr
),
823 EFI_IMAGE_REL_BASED_ARM_THUMB_MOVT
826 // The relocation entry needs to contain the lower 16-bits so we can do math
827 CoffAddFixupEntry ((UINT16
)(Sym
->st_value
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]));
833 mCoffSectionsOffset
[RelShdr
->sh_info
]
834 + (Rel
->r_offset
- SecShdr
->sh_addr
),
835 EFI_IMAGE_REL_BASED_HIGHLOW
840 Error (NULL
, 0, 3000, "Invalid", "WriteRelocations(): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName
, (unsigned) ELF32_R_TYPE(Rel
->r_info
));
843 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
850 if (!FoundRelocations
&& (mEhdr
->e_machine
== EM_ARM
)) {
851 /* Try again, but look for PT_DYNAMIC instead of SHT_REL */
853 for (Index
= 0; Index
< mEhdr
->e_phnum
; Index
++) {
858 DynamicSegment
= GetPhdrByIndex (Index
);
860 if (DynamicSegment
->p_type
== PT_DYNAMIC
) {
861 Dyn
= (Elf32_Dyn
*) ((UINT8
*)mEhdr
+ DynamicSegment
->p_offset
);
863 while (Dyn
->d_tag
!= DT_NULL
) {
864 switch (Dyn
->d_tag
) {
866 RelOffset
= Dyn
->d_un
.d_val
;
870 RelSize
= Dyn
->d_un
.d_val
;
874 RelElementSize
= Dyn
->d_un
.d_val
;
882 if (( RelOffset
== 0 ) || ( RelSize
== 0 ) || ( RelElementSize
== 0 )) {
883 Error (NULL
, 0, 3000, "Invalid", "%s bad ARM dynamic relocations.", mInImageName
);
886 for (K
= 0; K
< RelSize
; K
+= RelElementSize
) {
888 if (DynamicSegment
->p_paddr
== 0) {
889 // Older versions of the ARM ELF (SWS ESPC 0003 B-02) specification define DT_REL
890 // as an offset in the dynamic segment. p_paddr is defined to be zero for ARM tools
891 Rel
= (Elf32_Rel
*) ((UINT8
*) mEhdr
+ DynamicSegment
->p_offset
+ RelOffset
+ K
);
893 // This is how it reads in the generic ELF specification
894 Rel
= (Elf32_Rel
*) ((UINT8
*) mEhdr
+ RelOffset
+ K
);
897 switch (ELF32_R_TYPE (Rel
->r_info
)) {
902 TargetSegment
= GetPhdrByIndex (ELF32_R_SYM (Rel
->r_info
) - 1);
904 // Note: r_offset in a memory address. Convert it to a pointer in the coff file.
905 Targ
= mCoffFile
+ mCoffSectionsOffset
[ ELF32_R_SYM( Rel
->r_info
) ] + Rel
->r_offset
- TargetSegment
->p_vaddr
;
907 *(UINT32
*)Targ
= *(UINT32
*)Targ
+ mCoffSectionsOffset
[ELF32_R_SYM( Rel
->r_info
)];
909 CoffAddFixup (mCoffSectionsOffset
[ELF32_R_SYM (Rel
->r_info
)] + (Rel
->r_offset
- TargetSegment
->p_vaddr
), EFI_IMAGE_REL_BASED_HIGHLOW
);
913 Error (NULL
, 0, 3000, "Invalid", "%s bad ARM dynamic relocations, unkown type %d.", mInImageName
, ELF32_R_TYPE (Rel
->r_info
));
923 // Pad by adding empty entries.
925 while (mCoffOffset
& (mCoffAlignment
- 1)) {
926 CoffAddFixupEntry(0);
929 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
930 Dir
= &NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
931 Dir
->Size
= mCoffOffset
- mRelocOffset
;
932 if (Dir
->Size
== 0) {
933 // If no relocations, null out the directory entry and don't add the .reloc section
934 Dir
->VirtualAddress
= 0;
935 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
937 Dir
->VirtualAddress
= mRelocOffset
;
938 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
939 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
940 | EFI_IMAGE_SCN_MEM_DISCARDABLE
941 | EFI_IMAGE_SCN_MEM_READ
);
954 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
955 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
956 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
957 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
959 Len
= strlen(mInImageName
) + 1;
960 DebugOffset
= mCoffOffset
;
962 mCoffOffset
+= sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)
963 + sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
)
965 mCoffOffset
= CoffAlign(mCoffOffset
);
967 mCoffFile
= realloc(mCoffFile
, mCoffOffset
);
968 memset(mCoffFile
+ DebugOffset
, 0, mCoffOffset
- DebugOffset
);
970 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ DebugOffset
);
971 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
972 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
973 Dir
->RVA
= DebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
974 Dir
->FileOffset
= DebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
976 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
977 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
978 strcpy ((char *)(Nb10
+ 1), mInImageName
);
981 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
982 DataDir
= &NtHdr
->Pe32
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
983 DataDir
->VirtualAddress
= DebugOffset
;
984 DataDir
->Size
= mCoffOffset
- DebugOffset
;
985 if (DataDir
->Size
== 0) {
986 // If no debug, null out the directory entry and don't add the .debug section
987 DataDir
->VirtualAddress
= 0;
988 NtHdr
->Pe32
.FileHeader
.NumberOfSections
--;
990 DataDir
->VirtualAddress
= DebugOffset
;
991 CreateSectionHeader (".debug", DebugOffset
, mCoffOffset
- DebugOffset
,
992 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
993 | EFI_IMAGE_SCN_MEM_DISCARDABLE
994 | EFI_IMAGE_SCN_MEM_READ
);
1005 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
1010 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
1011 NtHdr
->Pe32
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
1020 if (mCoffSectionsOffset
!= NULL
) {
1021 free (mCoffSectionsOffset
);