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 "Elf64Convert.h"
46 SECTION_FILTER_TYPES FilterType
74 // Rename ELF32 strucutres to common names to help when porting to ELF64.
76 typedef Elf64_Shdr Elf_Shdr
;
77 typedef Elf64_Ehdr Elf_Ehdr
;
78 typedef Elf64_Rel Elf_Rel
;
79 typedef Elf64_Rela Elf_Rela
;
80 typedef Elf64_Sym Elf_Sym
;
81 typedef Elf64_Phdr Elf_Phdr
;
82 typedef Elf64_Dyn Elf_Dyn
;
83 #define ELFCLASS ELFCLASS64
84 #define ELF_R_TYPE(r) ELF64_R_TYPE(r)
85 #define ELF_R_SYM(r) ELF64_R_SYM(r)
88 // Well known ELF structures.
90 STATIC Elf_Ehdr
*mEhdr
;
91 STATIC Elf_Shdr
*mShdrBase
;
92 STATIC Elf_Phdr
*mPhdrBase
;
97 STATIC
const UINT32 mCoffAlignment
= 0x20;
100 // PE section alignment.
102 STATIC
const UINT16 mCoffNbrSections
= 5;
105 // ELF sections to offset in Coff file.
107 STATIC UINT32
*mCoffSectionsOffset
= NULL
;
110 // Offsets in COFF file
112 STATIC UINT32 mNtHdrOffset
;
113 STATIC UINT32 mTextOffset
;
114 STATIC UINT32 mDataOffset
;
115 STATIC UINT32 mHiiRsrcOffset
;
116 STATIC UINT32 mRelocOffset
;
119 // Initialization Function
124 ELF_FUNCTION_TABLE
*ElfFunctions
128 // Initialize data pointer and structures.
130 VerboseMsg ("Set EHDR");
131 mEhdr
= (Elf_Ehdr
*) FileBuffer
;
134 // Check the ELF64 specific header information.
136 VerboseMsg ("Check ELF64 Header Information");
137 if (mEhdr
->e_ident
[EI_CLASS
] != ELFCLASS64
) {
138 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS64");
141 if (mEhdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
142 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
145 if ((mEhdr
->e_type
!= ET_EXEC
) && (mEhdr
->e_type
!= ET_DYN
)) {
146 Error (NULL
, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
149 if (!((mEhdr
->e_machine
== EM_X86_64
))) {
150 Error (NULL
, 0, 3000, "Unsupported", "ELF e_machine not EM_X86_64");
153 if (mEhdr
->e_version
!= EV_CURRENT
) {
154 Error (NULL
, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr
->e_version
, EV_CURRENT
);
159 // Update section header pointers
161 VerboseMsg ("Update 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 VerboseMsg ("Create COFF Section Offset Buffer");
169 mCoffSectionsOffset
= (UINT32
*)malloc(mEhdr
->e_shnum
* sizeof (UINT32
));
170 memset(mCoffSectionsOffset
, 0, mEhdr
->e_shnum
* sizeof(UINT32
));
173 // Fill in function pointers.
175 VerboseMsg ("Fill in Function Pointers");
176 ElfFunctions
->ScanSections
= ScanSections64
;
177 ElfFunctions
->WriteSections
= WriteSections64
;
178 ElfFunctions
->WriteRelocations
= WriteRelocations64
;
179 ElfFunctions
->WriteDebug
= WriteDebug64
;
180 ElfFunctions
->SetImageSize
= SetImageSize64
;
181 ElfFunctions
->CleanUp
= CleanUp64
;
188 // Header by Index functions
196 if (Num
>= mEhdr
->e_shnum
)
198 return (Elf_Shdr
*)((UINT8
*)mShdrBase
+ Num
* mEhdr
->e_shentsize
);
207 return (Offset
+ mCoffAlignment
- 1) & ~(mCoffAlignment
- 1);
219 return (BOOLEAN
) ((Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == SHF_ALLOC
);
228 Elf_Shdr
*Namedr
= GetShdrByIndex(mEhdr
->e_shstrndx
);
230 return (BOOLEAN
) (strcmp((CHAR8
*)mEhdr
+ Namedr
->sh_offset
+ Shdr
->sh_name
, ELF_HII_SECTION_NAME
) == 0);
239 if (IsHiiRsrcShdr(Shdr
)) {
242 return (BOOLEAN
) (Shdr
->sh_flags
& (SHF_WRITE
| SHF_ALLOC
)) == (SHF_ALLOC
| SHF_WRITE
);
246 // Elf functions interface implementation
256 EFI_IMAGE_DOS_HEADER
*DosHdr
;
257 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
265 // Coff file start with a DOS header.
267 mCoffOffset
= sizeof(EFI_IMAGE_DOS_HEADER
) + 0x40;
268 mNtHdrOffset
= mCoffOffset
;
269 switch (mEhdr
->e_machine
) {
272 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
275 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN
)mEhdr
->e_machine
);
276 mCoffOffset
+= sizeof (EFI_IMAGE_NT_HEADERS64
);
280 mTableOffset
= mCoffOffset
;
281 mCoffOffset
+= mCoffNbrSections
* sizeof(EFI_IMAGE_SECTION_HEADER
);
284 // First text sections.
286 mCoffOffset
= CoffAlign(mCoffOffset
);
287 mTextOffset
= mCoffOffset
;
289 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
290 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
291 if (IsTextShdr(shdr
)) {
292 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
293 // the alignment field is valid
294 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
295 // if the section address is aligned we must align PE/COFF
296 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
297 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
298 // ARM RVCT tools have behavior outside of the ELF specification to try
299 // and make images smaller. If sh_addr is not aligned to sh_addralign
300 // then the section needs to preserve sh_addr MOD sh_addralign.
301 // Normally doing nothing here works great.
302 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
306 /* Relocate entry. */
307 if ((mEhdr
->e_entry
>= shdr
->sh_addr
) &&
308 (mEhdr
->e_entry
< shdr
->sh_addr
+ shdr
->sh_size
)) {
309 CoffEntry
= (UINT32
) (mCoffOffset
+ mEhdr
->e_entry
- shdr
->sh_addr
);
311 mCoffSectionsOffset
[i
] = mCoffOffset
;
312 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
317 if (mEhdr
->e_machine
!= EM_ARM
) {
318 mCoffOffset
= CoffAlign(mCoffOffset
);
321 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
322 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName
);
326 // Then data sections.
328 mDataOffset
= mCoffOffset
;
330 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
331 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
332 if (IsDataShdr(shdr
)) {
333 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
334 // the alignment field is valid
335 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
336 // if the section address is aligned we must align PE/COFF
337 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
338 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
339 // ARM RVCT tools have behavior outside of the ELF specification to try
340 // and make images smaller. If sh_addr is not aligned to sh_addralign
341 // then the section needs to preserve sh_addr MOD sh_addralign.
342 // Normally doing nothing here works great.
343 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
346 mCoffSectionsOffset
[i
] = mCoffOffset
;
347 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
351 mCoffOffset
= CoffAlign(mCoffOffset
);
353 if (SectionCount
> 1 && mOutImageType
== FW_EFI_IMAGE
) {
354 Warning (NULL
, 0, 0, NULL
, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName
);
358 // The HII resource sections.
360 mHiiRsrcOffset
= mCoffOffset
;
361 for (i
= 0; i
< mEhdr
->e_shnum
; i
++) {
362 Elf_Shdr
*shdr
= GetShdrByIndex(i
);
363 if (IsHiiRsrcShdr(shdr
)) {
364 if ((shdr
->sh_addralign
!= 0) && (shdr
->sh_addralign
!= 1)) {
365 // the alignment field is valid
366 if ((shdr
->sh_addr
& (shdr
->sh_addralign
- 1)) == 0) {
367 // if the section address is aligned we must align PE/COFF
368 mCoffOffset
= (UINT32
) ((mCoffOffset
+ shdr
->sh_addralign
- 1) & ~(shdr
->sh_addralign
- 1));
369 } else if ((shdr
->sh_addr
% shdr
->sh_addralign
) != (mCoffOffset
% shdr
->sh_addralign
)) {
370 // ARM RVCT tools have behavior outside of the ELF specification to try
371 // and make images smaller. If sh_addr is not aligned to sh_addralign
372 // then the section needs to preserve sh_addr MOD sh_addralign.
373 // Normally doing nothing here works great.
374 Error (NULL
, 0, 3000, "Invalid", "Unsupported section alignment.");
377 if (shdr
->sh_size
!= 0) {
378 mCoffSectionsOffset
[i
] = mCoffOffset
;
379 mCoffOffset
+= (UINT32
) shdr
->sh_size
;
380 mCoffOffset
= CoffAlign(mCoffOffset
);
381 SetHiiResourceHeader ((UINT8
*) mEhdr
+ shdr
->sh_offset
, mHiiRsrcOffset
);
387 mRelocOffset
= mCoffOffset
;
390 // Allocate base Coff file. Will be expanded later for relocations.
392 mCoffFile
= (UINT8
*)malloc(mCoffOffset
);
393 memset(mCoffFile
, 0, mCoffOffset
);
398 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)mCoffFile
;
399 DosHdr
->e_magic
= EFI_IMAGE_DOS_SIGNATURE
;
400 DosHdr
->e_lfanew
= mNtHdrOffset
;
402 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
404 NtHdr
->Pe32Plus
.Signature
= EFI_IMAGE_NT_SIGNATURE
;
406 switch (mEhdr
->e_machine
) {
408 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
409 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
412 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_IPF
;
413 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
416 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN
)mEhdr
->e_machine
);
417 NtHdr
->Pe32Plus
.FileHeader
.Machine
= EFI_IMAGE_MACHINE_X64
;
418 NtHdr
->Pe32Plus
.OptionalHeader
.Magic
= EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
;
421 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
= mCoffNbrSections
;
422 NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
= (UINT32
) time(NULL
);
423 mImageTimeStamp
= NtHdr
->Pe32Plus
.FileHeader
.TimeDateStamp
;
424 NtHdr
->Pe32Plus
.FileHeader
.PointerToSymbolTable
= 0;
425 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSymbols
= 0;
426 NtHdr
->Pe32Plus
.FileHeader
.SizeOfOptionalHeader
= sizeof(NtHdr
->Pe32Plus
.OptionalHeader
);
427 NtHdr
->Pe32Plus
.FileHeader
.Characteristics
= EFI_IMAGE_FILE_EXECUTABLE_IMAGE
428 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
429 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
430 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE
;
432 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfCode
= mDataOffset
- mTextOffset
;
433 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfInitializedData
= mRelocOffset
- mDataOffset
;
434 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfUninitializedData
= 0;
435 NtHdr
->Pe32Plus
.OptionalHeader
.AddressOfEntryPoint
= CoffEntry
;
437 NtHdr
->Pe32Plus
.OptionalHeader
.BaseOfCode
= mTextOffset
;
439 NtHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= 0;
440 NtHdr
->Pe32Plus
.OptionalHeader
.SectionAlignment
= mCoffAlignment
;
441 NtHdr
->Pe32Plus
.OptionalHeader
.FileAlignment
= mCoffAlignment
;
442 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= 0;
444 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfHeaders
= mTextOffset
;
445 NtHdr
->Pe32Plus
.OptionalHeader
.NumberOfRvaAndSizes
= EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
;
450 if ((mDataOffset
- mTextOffset
) > 0) {
451 CreateSectionHeader (".text", mTextOffset
, mDataOffset
- mTextOffset
,
452 EFI_IMAGE_SCN_CNT_CODE
453 | EFI_IMAGE_SCN_MEM_EXECUTE
454 | EFI_IMAGE_SCN_MEM_READ
);
456 // Don't make a section of size 0.
457 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
460 if ((mHiiRsrcOffset
- mDataOffset
) > 0) {
461 CreateSectionHeader (".data", mDataOffset
, mHiiRsrcOffset
- mDataOffset
,
462 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
463 | EFI_IMAGE_SCN_MEM_WRITE
464 | EFI_IMAGE_SCN_MEM_READ
);
466 // Don't make a section of size 0.
467 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
470 if ((mRelocOffset
- mHiiRsrcOffset
) > 0) {
471 CreateSectionHeader (".rsrc", mHiiRsrcOffset
, mRelocOffset
- mHiiRsrcOffset
,
472 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
473 | EFI_IMAGE_SCN_MEM_READ
);
475 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= mRelocOffset
- mHiiRsrcOffset
;
476 NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= mHiiRsrcOffset
;
478 // Don't make a section of size 0.
479 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
487 SECTION_FILTER_TYPES FilterType
493 BOOLEAN (*Filter
)(Elf_Shdr
*);
496 // Initialize filter pointer
498 switch (FilterType
) {
503 Filter
= IsHiiRsrcShdr
;
513 // First: copy sections.
515 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
516 Elf_Shdr
*Shdr
= GetShdrByIndex(Idx
);
517 if ((*Filter
)(Shdr
)) {
518 switch (Shdr
->sh_type
) {
521 memcpy(mCoffFile
+ mCoffSectionsOffset
[Idx
],
522 (UINT8
*)mEhdr
+ Shdr
->sh_offset
,
523 (size_t) Shdr
->sh_size
);
527 memset(mCoffFile
+ mCoffSectionsOffset
[Idx
], 0, (size_t) Shdr
->sh_size
);
532 // Ignore for unkown section type.
534 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName
, (unsigned)Shdr
->sh_type
);
541 // Second: apply relocations.
543 VerboseMsg ("Applying Relocations...");
544 for (Idx
= 0; Idx
< mEhdr
->e_shnum
; Idx
++) {
545 Elf_Shdr
*RelShdr
= GetShdrByIndex(Idx
);
546 if ((RelShdr
->sh_type
!= SHT_REL
) && (RelShdr
->sh_type
!= SHT_RELA
)) {
549 SecShdr
= GetShdrByIndex(RelShdr
->sh_info
);
550 SecOffset
= mCoffSectionsOffset
[RelShdr
->sh_info
];
551 if (RelShdr
->sh_type
== SHT_RELA
&& (*Filter
)(SecShdr
)) {
553 Elf_Shdr
*SymtabShdr
= GetShdrByIndex(RelShdr
->sh_link
);
554 UINT8
*Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
555 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= (UINT32
) RelShdr
->sh_entsize
) {
556 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
557 Elf_Sym
*Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
561 if (Sym
->st_shndx
== SHN_UNDEF
562 || Sym
->st_shndx
== SHN_ABS
563 || Sym
->st_shndx
> mEhdr
->e_shnum
) {
564 Error (NULL
, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName
);
566 SymShdr
= GetShdrByIndex(Sym
->st_shndx
);
569 // Note: r_offset in a memory address.
570 // Convert it to a pointer in the coff file.
572 Targ
= mCoffFile
+ SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
);
574 if (mEhdr
->e_machine
== EM_X86_64
) {
575 switch (ELF_R_TYPE(Rel
->r_info
)) {
580 // Absolute relocation.
582 VerboseMsg ("R_X86_64_64");
583 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
584 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
586 *(UINT64
*)Targ
= *(UINT64
*)Targ
- SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
];
587 VerboseMsg ("Relocation: 0x%016LX", *(UINT64
*)Targ
);
590 VerboseMsg ("R_X86_64_32");
591 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
592 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
594 *(UINT32
*)Targ
= (UINT32
)((UINT64
)(*(UINT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
595 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
598 VerboseMsg ("R_X86_64_32S");
599 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
600 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
602 *(INT32
*)Targ
= (INT32
)((INT64
)(*(INT32
*)Targ
) - SymShdr
->sh_addr
+ mCoffSectionsOffset
[Sym
->st_shndx
]);
603 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
607 // Relative relocation: Symbol - Ip + Addend
609 VerboseMsg ("R_X86_64_PC32");
610 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
611 (UINT32
)(SecOffset
+ (Rel
->r_offset
- SecShdr
->sh_addr
)),
613 *(UINT32
*)Targ
= (UINT32
) (*(UINT32
*)Targ
614 + (mCoffSectionsOffset
[Sym
->st_shndx
] - SymShdr
->sh_addr
)
615 - (SecOffset
- SecShdr
->sh_addr
));
616 VerboseMsg ("Relocation: 0x%08X", *(UINT32
*)Targ
);
619 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
622 Error (NULL
, 0, 3000, "Invalid", "Not EM_X86_X64");
638 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
639 EFI_IMAGE_DATA_DIRECTORY
*Dir
;
640 BOOLEAN FoundRelocations
;
642 Elf_Shdr
*SymtabShdr
;
646 for (Index
= 0, FoundRelocations
= FALSE
; Index
< mEhdr
->e_shnum
; Index
++) {
647 Elf_Shdr
*RelShdr
= GetShdrByIndex(Index
);
648 if ((RelShdr
->sh_type
== SHT_REL
) || (RelShdr
->sh_type
== SHT_RELA
)) {
649 Elf_Shdr
*SecShdr
= GetShdrByIndex (RelShdr
->sh_info
);
650 if (IsTextShdr(SecShdr
) || IsDataShdr(SecShdr
)) {
653 SymtabShdr
= GetShdrByIndex (RelShdr
->sh_link
);
654 Symtab
= (UINT8
*)mEhdr
+ SymtabShdr
->sh_offset
;
655 FoundRelocations
= TRUE
;
656 for (RelIdx
= 0; RelIdx
< RelShdr
->sh_size
; RelIdx
+= RelShdr
->sh_entsize
) {
657 Elf_Rela
*Rel
= (Elf_Rela
*)((UINT8
*)mEhdr
+ RelShdr
->sh_offset
+ RelIdx
);
660 Sym
= (Elf_Sym
*)(Symtab
+ ELF_R_SYM(Rel
->r_info
) * SymtabShdr
->sh_entsize
);
661 SymShdr
= GetShdrByIndex (Sym
->st_shndx
);
663 if (mEhdr
->e_machine
== EM_X86_64
) {
664 switch (ELF_R_TYPE(Rel
->r_info
)) {
669 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",
670 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
672 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
673 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
674 EFI_IMAGE_REL_BASED_DIR64
);
678 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",
679 mCoffSectionsOffset
[RelShdr
->sh_info
] + (Rel
->r_offset
- SecShdr
->sh_addr
));
681 (UINT32
) ((UINT64
) mCoffSectionsOffset
[RelShdr
->sh_info
]
682 + (Rel
->r_offset
- SecShdr
->sh_addr
)),
683 EFI_IMAGE_REL_BASED_HIGHLOW
);
686 Error (NULL
, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName
, (unsigned) ELF_R_TYPE(Rel
->r_info
));
689 Error (NULL
, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr
->e_machine
);
697 // Pad by adding empty entries.
699 while (mCoffOffset
& (mCoffAlignment
- 1)) {
700 CoffAddFixupEntry(0);
703 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
704 Dir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
];
705 Dir
->Size
= mCoffOffset
- mRelocOffset
;
706 if (Dir
->Size
== 0) {
707 // If no relocations, null out the directory entry and don't add the .reloc section
708 Dir
->VirtualAddress
= 0;
709 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
711 Dir
->VirtualAddress
= mRelocOffset
;
712 CreateSectionHeader (".reloc", mRelocOffset
, mCoffOffset
- mRelocOffset
,
713 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
714 | EFI_IMAGE_SCN_MEM_DISCARDABLE
715 | EFI_IMAGE_SCN_MEM_READ
);
727 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
728 EFI_IMAGE_DATA_DIRECTORY
*DataDir
;
729 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*Dir
;
730 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*Nb10
;
732 Len
= strlen(mInImageName
) + 1;
733 DebugOffset
= mCoffOffset
;
735 mCoffOffset
+= sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
)
736 + sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
)
738 mCoffOffset
= CoffAlign(mCoffOffset
);
740 mCoffFile
= realloc(mCoffFile
, mCoffOffset
);
741 memset(mCoffFile
+ DebugOffset
, 0, mCoffOffset
- DebugOffset
);
743 Dir
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*)(mCoffFile
+ DebugOffset
);
744 Dir
->Type
= EFI_IMAGE_DEBUG_TYPE_CODEVIEW
;
745 Dir
->SizeOfData
= sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
) + Len
;
746 Dir
->RVA
= DebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
747 Dir
->FileOffset
= DebugOffset
+ sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
);
749 Nb10
= (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
*)(Dir
+ 1);
750 Nb10
->Signature
= CODEVIEW_SIGNATURE_NB10
;
751 strcpy ((char *)(Nb10
+ 1), mInImageName
);
754 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
755 DataDir
= &NtHdr
->Pe32Plus
.OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
];
756 DataDir
->VirtualAddress
= DebugOffset
;
757 DataDir
->Size
= mCoffOffset
- DebugOffset
;
758 if (DataDir
->Size
== 0) {
759 // If no debug, null out the directory entry and don't add the .debug section
760 DataDir
->VirtualAddress
= 0;
761 NtHdr
->Pe32Plus
.FileHeader
.NumberOfSections
--;
763 DataDir
->VirtualAddress
= DebugOffset
;
764 CreateSectionHeader (".debug", DebugOffset
, mCoffOffset
- DebugOffset
,
765 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
766 | EFI_IMAGE_SCN_MEM_DISCARDABLE
767 | EFI_IMAGE_SCN_MEM_READ
);
778 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NtHdr
;
783 NtHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(mCoffFile
+ mNtHdrOffset
);
784 NtHdr
->Pe32Plus
.OptionalHeader
.SizeOfImage
= mCoffOffset
;
793 if (mCoffSectionsOffset
!= NULL
) {
794 free (mCoffSectionsOffset
);