]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/GenFw/Elf64Convert.c
BaseTools/GenFw: Add X64 GOTPCREL Support to GenFw
[mirror_edk2.git] / BaseTools / Source / C / GenFw / Elf64Convert.c
1 /** @file
2 Elf64 convert solution
3
4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>
6
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License which accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include "WinNtInclude.h"
18
19 #ifndef __GNUC__
20 #include <windows.h>
21 #include <io.h>
22 #endif
23 #include <assert.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <ctype.h>
29
30 #include <Common/UefiBaseTypes.h>
31 #include <IndustryStandard/PeImage.h>
32
33 #include "PeCoffLib.h"
34 #include "EfiUtilityMsgs.h"
35
36 #include "GenFw.h"
37 #include "ElfConvert.h"
38 #include "Elf64Convert.h"
39
40 STATIC
41 VOID
42 ScanSections64 (
43 VOID
44 );
45
46 STATIC
47 BOOLEAN
48 WriteSections64 (
49 SECTION_FILTER_TYPES FilterType
50 );
51
52 STATIC
53 VOID
54 WriteRelocations64 (
55 VOID
56 );
57
58 STATIC
59 VOID
60 WriteDebug64 (
61 VOID
62 );
63
64 STATIC
65 VOID
66 SetImageSize64 (
67 VOID
68 );
69
70 STATIC
71 VOID
72 CleanUp64 (
73 VOID
74 );
75
76 //
77 // Rename ELF32 strucutres to common names to help when porting to ELF64.
78 //
79 typedef Elf64_Shdr Elf_Shdr;
80 typedef Elf64_Ehdr Elf_Ehdr;
81 typedef Elf64_Rel Elf_Rel;
82 typedef Elf64_Rela Elf_Rela;
83 typedef Elf64_Sym Elf_Sym;
84 typedef Elf64_Phdr Elf_Phdr;
85 typedef Elf64_Dyn Elf_Dyn;
86 #define ELFCLASS ELFCLASS64
87 #define ELF_R_TYPE(r) ELF64_R_TYPE(r)
88 #define ELF_R_SYM(r) ELF64_R_SYM(r)
89
90 //
91 // Well known ELF structures.
92 //
93 STATIC Elf_Ehdr *mEhdr;
94 STATIC Elf_Shdr *mShdrBase;
95 STATIC Elf_Phdr *mPhdrBase;
96
97 //
98 // GOT information
99 //
100 STATIC Elf_Shdr *mGOTShdr = NULL;
101 STATIC UINT32 mGOTShindex = 0;
102 STATIC UINT32 *mGOTCoffEntries = NULL;
103 STATIC UINT32 mGOTMaxCoffEntries = 0;
104 STATIC UINT32 mGOTNumCoffEntries = 0;
105
106 //
107 // Coff information
108 //
109 STATIC UINT32 mCoffAlignment = 0x20;
110
111 //
112 // PE section alignment.
113 //
114 STATIC const UINT16 mCoffNbrSections = 4;
115
116 //
117 // ELF sections to offset in Coff file.
118 //
119 STATIC UINT32 *mCoffSectionsOffset = NULL;
120
121 //
122 // Offsets in COFF file
123 //
124 STATIC UINT32 mNtHdrOffset;
125 STATIC UINT32 mTextOffset;
126 STATIC UINT32 mDataOffset;
127 STATIC UINT32 mHiiRsrcOffset;
128 STATIC UINT32 mRelocOffset;
129 STATIC UINT32 mDebugOffset;
130
131 //
132 // Initialization Function
133 //
134 BOOLEAN
135 InitializeElf64 (
136 UINT8 *FileBuffer,
137 ELF_FUNCTION_TABLE *ElfFunctions
138 )
139 {
140 //
141 // Initialize data pointer and structures.
142 //
143 VerboseMsg ("Set EHDR");
144 mEhdr = (Elf_Ehdr*) FileBuffer;
145
146 //
147 // Check the ELF64 specific header information.
148 //
149 VerboseMsg ("Check ELF64 Header Information");
150 if (mEhdr->e_ident[EI_CLASS] != ELFCLASS64) {
151 Error (NULL, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS64");
152 return FALSE;
153 }
154 if (mEhdr->e_ident[EI_DATA] != ELFDATA2LSB) {
155 Error (NULL, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
156 return FALSE;
157 }
158 if ((mEhdr->e_type != ET_EXEC) && (mEhdr->e_type != ET_DYN)) {
159 Error (NULL, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
160 return FALSE;
161 }
162 if (!((mEhdr->e_machine == EM_X86_64) || (mEhdr->e_machine == EM_AARCH64))) {
163 Error (NULL, 0, 3000, "Unsupported", "ELF e_machine not EM_X86_64 or EM_AARCH64");
164 return FALSE;
165 }
166 if (mEhdr->e_version != EV_CURRENT) {
167 Error (NULL, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr->e_version, EV_CURRENT);
168 return FALSE;
169 }
170
171 //
172 // Update section header pointers
173 //
174 VerboseMsg ("Update Header Pointers");
175 mShdrBase = (Elf_Shdr *)((UINT8 *)mEhdr + mEhdr->e_shoff);
176 mPhdrBase = (Elf_Phdr *)((UINT8 *)mEhdr + mEhdr->e_phoff);
177
178 //
179 // Create COFF Section offset buffer and zero.
180 //
181 VerboseMsg ("Create COFF Section Offset Buffer");
182 mCoffSectionsOffset = (UINT32 *)malloc(mEhdr->e_shnum * sizeof (UINT32));
183 if (mCoffSectionsOffset == NULL) {
184 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
185 return FALSE;
186 }
187 memset(mCoffSectionsOffset, 0, mEhdr->e_shnum * sizeof(UINT32));
188
189 //
190 // Fill in function pointers.
191 //
192 VerboseMsg ("Fill in Function Pointers");
193 ElfFunctions->ScanSections = ScanSections64;
194 ElfFunctions->WriteSections = WriteSections64;
195 ElfFunctions->WriteRelocations = WriteRelocations64;
196 ElfFunctions->WriteDebug = WriteDebug64;
197 ElfFunctions->SetImageSize = SetImageSize64;
198 ElfFunctions->CleanUp = CleanUp64;
199
200 return TRUE;
201 }
202
203
204 //
205 // Header by Index functions
206 //
207 STATIC
208 Elf_Shdr*
209 GetShdrByIndex (
210 UINT32 Num
211 )
212 {
213 if (Num >= mEhdr->e_shnum) {
214 Error (NULL, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num);
215 exit(EXIT_FAILURE);
216 }
217
218 return (Elf_Shdr*)((UINT8*)mShdrBase + Num * mEhdr->e_shentsize);
219 }
220
221 STATIC
222 UINT32
223 CoffAlign (
224 UINT32 Offset
225 )
226 {
227 return (Offset + mCoffAlignment - 1) & ~(mCoffAlignment - 1);
228 }
229
230 STATIC
231 UINT32
232 DebugRvaAlign (
233 UINT32 Offset
234 )
235 {
236 return (Offset + 3) & ~3;
237 }
238
239 //
240 // filter functions
241 //
242 STATIC
243 BOOLEAN
244 IsTextShdr (
245 Elf_Shdr *Shdr
246 )
247 {
248 return (BOOLEAN) ((Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == SHF_ALLOC);
249 }
250
251 STATIC
252 BOOLEAN
253 IsHiiRsrcShdr (
254 Elf_Shdr *Shdr
255 )
256 {
257 Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx);
258
259 return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_HII_SECTION_NAME) == 0);
260 }
261
262 STATIC
263 BOOLEAN
264 IsDataShdr (
265 Elf_Shdr *Shdr
266 )
267 {
268 if (IsHiiRsrcShdr(Shdr)) {
269 return FALSE;
270 }
271 return (BOOLEAN) (Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == (SHF_ALLOC | SHF_WRITE);
272 }
273
274 STATIC
275 BOOLEAN
276 IsStrtabShdr (
277 Elf_Shdr *Shdr
278 )
279 {
280 Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx);
281
282 return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_STRTAB_SECTION_NAME) == 0);
283 }
284
285 STATIC
286 Elf_Shdr *
287 FindStrtabShdr (
288 VOID
289 )
290 {
291 UINT32 i;
292 for (i = 0; i < mEhdr->e_shnum; i++) {
293 Elf_Shdr *shdr = GetShdrByIndex(i);
294 if (IsStrtabShdr(shdr)) {
295 return shdr;
296 }
297 }
298 return NULL;
299 }
300
301 STATIC
302 const UINT8 *
303 GetSymName (
304 Elf_Sym *Sym
305 )
306 {
307 Elf_Shdr *StrtabShdr;
308 UINT8 *StrtabContents;
309 BOOLEAN foundEnd;
310 UINT32 i;
311
312 if (Sym->st_name == 0) {
313 return NULL;
314 }
315
316 StrtabShdr = FindStrtabShdr();
317 if (StrtabShdr == NULL) {
318 return NULL;
319 }
320
321 assert(Sym->st_name < StrtabShdr->sh_size);
322
323 StrtabContents = (UINT8*)mEhdr + StrtabShdr->sh_offset;
324
325 foundEnd = FALSE;
326 for (i= Sym->st_name; (i < StrtabShdr->sh_size) && !foundEnd; i++) {
327 foundEnd = (BOOLEAN)(StrtabContents[i] == 0);
328 }
329 assert(foundEnd);
330
331 return StrtabContents + Sym->st_name;
332 }
333
334 //
335 // Find the ELF section hosting the GOT from an ELF Rva
336 // of a single GOT entry. Normally, GOT is placed in
337 // ELF .text section, so assume once we find in which
338 // section the GOT is, all GOT entries are there, and
339 // just verify this.
340 //
341 STATIC
342 VOID
343 FindElfGOTSectionFromGOTEntryElfRva (
344 Elf64_Addr GOTEntryElfRva
345 )
346 {
347 UINT32 i;
348 if (mGOTShdr != NULL) {
349 if (GOTEntryElfRva >= mGOTShdr->sh_addr &&
350 GOTEntryElfRva < mGOTShdr->sh_addr + mGOTShdr->sh_size) {
351 return;
352 }
353 Error (NULL, 0, 3000, "Unsupported", "FindElfGOTSectionFromGOTEntryElfRva: GOT entries found in multiple sections.");
354 exit(EXIT_FAILURE);
355 }
356 for (i = 0; i < mEhdr->e_shnum; i++) {
357 Elf_Shdr *shdr = GetShdrByIndex(i);
358 if (GOTEntryElfRva >= shdr->sh_addr &&
359 GOTEntryElfRva < shdr->sh_addr + shdr->sh_size) {
360 mGOTShdr = shdr;
361 mGOTShindex = i;
362 return;
363 }
364 }
365 Error (NULL, 0, 3000, "Invalid", "FindElfGOTSectionFromGOTEntryElfRva: ElfRva 0x%016LX for GOT entry not found in any section.", GOTEntryElfRva);
366 exit(EXIT_FAILURE);
367 }
368
369 //
370 // Stores locations of GOT entries in COFF image.
371 // Returns TRUE if GOT entry is new.
372 // Simple implementation as number of GOT
373 // entries is expected to be low.
374 //
375
376 STATIC
377 BOOLEAN
378 AccumulateCoffGOTEntries (
379 UINT32 GOTCoffEntry
380 )
381 {
382 UINT32 i;
383 if (mGOTCoffEntries != NULL) {
384 for (i = 0; i < mGOTNumCoffEntries; i++) {
385 if (mGOTCoffEntries[i] == GOTCoffEntry) {
386 return FALSE;
387 }
388 }
389 }
390 if (mGOTCoffEntries == NULL) {
391 mGOTCoffEntries = (UINT32*)malloc(5 * sizeof *mGOTCoffEntries);
392 if (mGOTCoffEntries == NULL) {
393 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
394 }
395 assert (mGOTCoffEntries != NULL);
396 mGOTMaxCoffEntries = 5;
397 mGOTNumCoffEntries = 0;
398 } else if (mGOTNumCoffEntries == mGOTMaxCoffEntries) {
399 mGOTCoffEntries = (UINT32*)realloc(mGOTCoffEntries, 2 * mGOTMaxCoffEntries * sizeof *mGOTCoffEntries);
400 if (mGOTCoffEntries == NULL) {
401 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
402 }
403 assert (mGOTCoffEntries != NULL);
404 mGOTMaxCoffEntries += mGOTMaxCoffEntries;
405 }
406 mGOTCoffEntries[mGOTNumCoffEntries++] = GOTCoffEntry;
407 return TRUE;
408 }
409
410 //
411 // 32-bit Unsigned integer comparator for qsort.
412 //
413 STATIC
414 int
415 UINT32Comparator (
416 const void* lhs,
417 const void* rhs
418 )
419 {
420 if (*(const UINT32*)lhs < *(const UINT32*)rhs) {
421 return -1;
422 }
423 return *(const UINT32*)lhs > *(const UINT32*)rhs;
424 }
425
426 //
427 // Emit accumulated Coff GOT entry relocations into
428 // Coff image. This function performs its job
429 // once and then releases the entry list, so
430 // it can safely be called multiple times.
431 //
432 STATIC
433 VOID
434 EmitGOTRelocations (
435 VOID
436 )
437 {
438 UINT32 i;
439 if (mGOTCoffEntries == NULL) {
440 return;
441 }
442 //
443 // Emit Coff relocations with Rvas ordered.
444 //
445 qsort(
446 mGOTCoffEntries,
447 mGOTNumCoffEntries,
448 sizeof *mGOTCoffEntries,
449 UINT32Comparator);
450 for (i = 0; i < mGOTNumCoffEntries; i++) {
451 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X", mGOTCoffEntries[i]);
452 CoffAddFixup(
453 mGOTCoffEntries[i],
454 EFI_IMAGE_REL_BASED_DIR64);
455 }
456 free(mGOTCoffEntries);
457 mGOTCoffEntries = NULL;
458 mGOTMaxCoffEntries = 0;
459 mGOTNumCoffEntries = 0;
460 }
461
462 //
463 // Elf functions interface implementation
464 //
465
466 STATIC
467 VOID
468 ScanSections64 (
469 VOID
470 )
471 {
472 UINT32 i;
473 EFI_IMAGE_DOS_HEADER *DosHdr;
474 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
475 UINT32 CoffEntry;
476 UINT32 SectionCount;
477 BOOLEAN FoundSection;
478
479 CoffEntry = 0;
480 mCoffOffset = 0;
481
482 //
483 // Coff file start with a DOS header.
484 //
485 mCoffOffset = sizeof(EFI_IMAGE_DOS_HEADER) + 0x40;
486 mNtHdrOffset = mCoffOffset;
487 switch (mEhdr->e_machine) {
488 case EM_X86_64:
489 case EM_IA_64:
490 case EM_AARCH64:
491 mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS64);
492 break;
493 default:
494 VerboseMsg ("%s unknown e_machine type %hu. Assume X64", mInImageName, mEhdr->e_machine);
495 mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS64);
496 break;
497 }
498
499 mTableOffset = mCoffOffset;
500 mCoffOffset += mCoffNbrSections * sizeof(EFI_IMAGE_SECTION_HEADER);
501
502 //
503 // Set mCoffAlignment to the maximum alignment of the input sections
504 // we care about
505 //
506 for (i = 0; i < mEhdr->e_shnum; i++) {
507 Elf_Shdr *shdr = GetShdrByIndex(i);
508 if (shdr->sh_addralign <= mCoffAlignment) {
509 continue;
510 }
511 if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr)) {
512 mCoffAlignment = (UINT32)shdr->sh_addralign;
513 }
514 }
515
516 //
517 // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT
518 //
519 if (mCoffAlignment > MAX_COFF_ALIGNMENT) {
520 Error (NULL, 0, 3000, "Invalid", "Section alignment is larger than MAX_COFF_ALIGNMENT.");
521 assert (FALSE);
522 }
523
524
525 //
526 // Move the PE/COFF header right before the first section. This will help us
527 // save space when converting to TE.
528 //
529 if (mCoffAlignment > mCoffOffset) {
530 mNtHdrOffset += mCoffAlignment - mCoffOffset;
531 mTableOffset += mCoffAlignment - mCoffOffset;
532 mCoffOffset = mCoffAlignment;
533 }
534
535 //
536 // First text sections.
537 //
538 mCoffOffset = CoffAlign(mCoffOffset);
539 mTextOffset = mCoffOffset;
540 FoundSection = FALSE;
541 SectionCount = 0;
542 for (i = 0; i < mEhdr->e_shnum; i++) {
543 Elf_Shdr *shdr = GetShdrByIndex(i);
544 if (IsTextShdr(shdr)) {
545 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
546 // the alignment field is valid
547 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
548 // if the section address is aligned we must align PE/COFF
549 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));
550 } else {
551 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
552 }
553 }
554
555 /* Relocate entry. */
556 if ((mEhdr->e_entry >= shdr->sh_addr) &&
557 (mEhdr->e_entry < shdr->sh_addr + shdr->sh_size)) {
558 CoffEntry = (UINT32) (mCoffOffset + mEhdr->e_entry - shdr->sh_addr);
559 }
560
561 //
562 // Set mTextOffset with the offset of the first '.text' section
563 //
564 if (!FoundSection) {
565 mTextOffset = mCoffOffset;
566 FoundSection = TRUE;
567 }
568
569 mCoffSectionsOffset[i] = mCoffOffset;
570 mCoffOffset += (UINT32) shdr->sh_size;
571 SectionCount ++;
572 }
573 }
574
575 if (!FoundSection) {
576 Error (NULL, 0, 3000, "Invalid", "Did not find any '.text' section.");
577 assert (FALSE);
578 }
579
580 mDebugOffset = DebugRvaAlign(mCoffOffset);
581 mCoffOffset = CoffAlign(mCoffOffset);
582
583 if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {
584 Warning (NULL, 0, 0, NULL, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName);
585 }
586
587 //
588 // Then data sections.
589 //
590 mDataOffset = mCoffOffset;
591 FoundSection = FALSE;
592 SectionCount = 0;
593 for (i = 0; i < mEhdr->e_shnum; i++) {
594 Elf_Shdr *shdr = GetShdrByIndex(i);
595 if (IsDataShdr(shdr)) {
596 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
597 // the alignment field is valid
598 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
599 // if the section address is aligned we must align PE/COFF
600 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));
601 } else {
602 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
603 }
604 }
605
606 //
607 // Set mDataOffset with the offset of the first '.data' section
608 //
609 if (!FoundSection) {
610 mDataOffset = mCoffOffset;
611 FoundSection = TRUE;
612 }
613 mCoffSectionsOffset[i] = mCoffOffset;
614 mCoffOffset += (UINT32) shdr->sh_size;
615 SectionCount ++;
616 }
617 }
618
619 //
620 // Make room for .debug data in .data (or .text if .data is empty) instead of
621 // putting it in a section of its own. This is explicitly allowed by the
622 // PE/COFF spec, and prevents bloat in the binary when using large values for
623 // section alignment.
624 //
625 if (SectionCount > 0) {
626 mDebugOffset = DebugRvaAlign(mCoffOffset);
627 }
628 mCoffOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY) +
629 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) +
630 strlen(mInImageName) + 1;
631
632 mCoffOffset = CoffAlign(mCoffOffset);
633 if (SectionCount == 0) {
634 mDataOffset = mCoffOffset;
635 }
636
637 if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {
638 Warning (NULL, 0, 0, NULL, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName);
639 }
640
641 //
642 // The HII resource sections.
643 //
644 mHiiRsrcOffset = mCoffOffset;
645 for (i = 0; i < mEhdr->e_shnum; i++) {
646 Elf_Shdr *shdr = GetShdrByIndex(i);
647 if (IsHiiRsrcShdr(shdr)) {
648 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
649 // the alignment field is valid
650 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
651 // if the section address is aligned we must align PE/COFF
652 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));
653 } else {
654 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
655 }
656 }
657 if (shdr->sh_size != 0) {
658 mHiiRsrcOffset = mCoffOffset;
659 mCoffSectionsOffset[i] = mCoffOffset;
660 mCoffOffset += (UINT32) shdr->sh_size;
661 mCoffOffset = CoffAlign(mCoffOffset);
662 SetHiiResourceHeader ((UINT8*) mEhdr + shdr->sh_offset, mHiiRsrcOffset);
663 }
664 break;
665 }
666 }
667
668 mRelocOffset = mCoffOffset;
669
670 //
671 // Allocate base Coff file. Will be expanded later for relocations.
672 //
673 mCoffFile = (UINT8 *)malloc(mCoffOffset);
674 if (mCoffFile == NULL) {
675 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
676 }
677 assert (mCoffFile != NULL);
678 memset(mCoffFile, 0, mCoffOffset);
679
680 //
681 // Fill headers.
682 //
683 DosHdr = (EFI_IMAGE_DOS_HEADER *)mCoffFile;
684 DosHdr->e_magic = EFI_IMAGE_DOS_SIGNATURE;
685 DosHdr->e_lfanew = mNtHdrOffset;
686
687 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION*)(mCoffFile + mNtHdrOffset);
688
689 NtHdr->Pe32Plus.Signature = EFI_IMAGE_NT_SIGNATURE;
690
691 switch (mEhdr->e_machine) {
692 case EM_X86_64:
693 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_X64;
694 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
695 break;
696 case EM_IA_64:
697 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_IPF;
698 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
699 break;
700 case EM_AARCH64:
701 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_AARCH64;
702 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
703 break;
704 default:
705 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN)mEhdr->e_machine);
706 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_X64;
707 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
708 }
709
710 NtHdr->Pe32Plus.FileHeader.NumberOfSections = mCoffNbrSections;
711 NtHdr->Pe32Plus.FileHeader.TimeDateStamp = (UINT32) time(NULL);
712 mImageTimeStamp = NtHdr->Pe32Plus.FileHeader.TimeDateStamp;
713 NtHdr->Pe32Plus.FileHeader.PointerToSymbolTable = 0;
714 NtHdr->Pe32Plus.FileHeader.NumberOfSymbols = 0;
715 NtHdr->Pe32Plus.FileHeader.SizeOfOptionalHeader = sizeof(NtHdr->Pe32Plus.OptionalHeader);
716 NtHdr->Pe32Plus.FileHeader.Characteristics = EFI_IMAGE_FILE_EXECUTABLE_IMAGE
717 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
718 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
719 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE;
720
721 NtHdr->Pe32Plus.OptionalHeader.SizeOfCode = mDataOffset - mTextOffset;
722 NtHdr->Pe32Plus.OptionalHeader.SizeOfInitializedData = mRelocOffset - mDataOffset;
723 NtHdr->Pe32Plus.OptionalHeader.SizeOfUninitializedData = 0;
724 NtHdr->Pe32Plus.OptionalHeader.AddressOfEntryPoint = CoffEntry;
725
726 NtHdr->Pe32Plus.OptionalHeader.BaseOfCode = mTextOffset;
727
728 NtHdr->Pe32Plus.OptionalHeader.ImageBase = 0;
729 NtHdr->Pe32Plus.OptionalHeader.SectionAlignment = mCoffAlignment;
730 NtHdr->Pe32Plus.OptionalHeader.FileAlignment = mCoffAlignment;
731 NtHdr->Pe32Plus.OptionalHeader.SizeOfImage = 0;
732
733 NtHdr->Pe32Plus.OptionalHeader.SizeOfHeaders = mTextOffset;
734 NtHdr->Pe32Plus.OptionalHeader.NumberOfRvaAndSizes = EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES;
735
736 //
737 // Section headers.
738 //
739 if ((mDataOffset - mTextOffset) > 0) {
740 CreateSectionHeader (".text", mTextOffset, mDataOffset - mTextOffset,
741 EFI_IMAGE_SCN_CNT_CODE
742 | EFI_IMAGE_SCN_MEM_EXECUTE
743 | EFI_IMAGE_SCN_MEM_READ);
744 } else {
745 // Don't make a section of size 0.
746 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
747 }
748
749 if ((mHiiRsrcOffset - mDataOffset) > 0) {
750 CreateSectionHeader (".data", mDataOffset, mHiiRsrcOffset - mDataOffset,
751 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
752 | EFI_IMAGE_SCN_MEM_WRITE
753 | EFI_IMAGE_SCN_MEM_READ);
754 } else {
755 // Don't make a section of size 0.
756 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
757 }
758
759 if ((mRelocOffset - mHiiRsrcOffset) > 0) {
760 CreateSectionHeader (".rsrc", mHiiRsrcOffset, mRelocOffset - mHiiRsrcOffset,
761 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
762 | EFI_IMAGE_SCN_MEM_READ);
763
764 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = mRelocOffset - mHiiRsrcOffset;
765 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = mHiiRsrcOffset;
766 } else {
767 // Don't make a section of size 0.
768 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
769 }
770
771 }
772
773 STATIC
774 BOOLEAN
775 WriteSections64 (
776 SECTION_FILTER_TYPES FilterType
777 )
778 {
779 UINT32 Idx;
780 Elf_Shdr *SecShdr;
781 UINT32 SecOffset;
782 BOOLEAN (*Filter)(Elf_Shdr *);
783 Elf64_Addr GOTEntryRva;
784
785 //
786 // Initialize filter pointer
787 //
788 switch (FilterType) {
789 case SECTION_TEXT:
790 Filter = IsTextShdr;
791 break;
792 case SECTION_HII:
793 Filter = IsHiiRsrcShdr;
794 break;
795 case SECTION_DATA:
796 Filter = IsDataShdr;
797 break;
798 default:
799 return FALSE;
800 }
801
802 //
803 // First: copy sections.
804 //
805 for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {
806 Elf_Shdr *Shdr = GetShdrByIndex(Idx);
807 if ((*Filter)(Shdr)) {
808 switch (Shdr->sh_type) {
809 case SHT_PROGBITS:
810 /* Copy. */
811 if (Shdr->sh_offset + Shdr->sh_size > mFileBufferSize) {
812 return FALSE;
813 }
814 memcpy(mCoffFile + mCoffSectionsOffset[Idx],
815 (UINT8*)mEhdr + Shdr->sh_offset,
816 (size_t) Shdr->sh_size);
817 break;
818
819 case SHT_NOBITS:
820 memset(mCoffFile + mCoffSectionsOffset[Idx], 0, (size_t) Shdr->sh_size);
821 break;
822
823 default:
824 //
825 // Ignore for unkown section type.
826 //
827 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName, (unsigned)Shdr->sh_type);
828 break;
829 }
830 }
831 }
832
833 //
834 // Second: apply relocations.
835 //
836 VerboseMsg ("Applying Relocations...");
837 for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {
838 //
839 // Determine if this is a relocation section.
840 //
841 Elf_Shdr *RelShdr = GetShdrByIndex(Idx);
842 if ((RelShdr->sh_type != SHT_REL) && (RelShdr->sh_type != SHT_RELA)) {
843 continue;
844 }
845
846 //
847 // If this is a ET_DYN (PIE) executable, we will encounter a dynamic SHT_RELA
848 // section that applies to the entire binary, and which will have its section
849 // index set to #0 (which is a NULL section with the SHF_ALLOC bit cleared).
850 //
851 // In the absence of GOT based relocations,
852 // this RELA section will contain redundant R_xxx_RELATIVE relocations, one
853 // for every R_xxx_xx64 relocation appearing in the per-section RELA sections.
854 // (i.e., .rela.text and .rela.data)
855 //
856 if (RelShdr->sh_info == 0) {
857 continue;
858 }
859
860 //
861 // Relocation section found. Now extract section information that the relocations
862 // apply to in the ELF data and the new COFF data.
863 //
864 SecShdr = GetShdrByIndex(RelShdr->sh_info);
865 SecOffset = mCoffSectionsOffset[RelShdr->sh_info];
866
867 //
868 // Only process relocations for the current filter type.
869 //
870 if (RelShdr->sh_type == SHT_RELA && (*Filter)(SecShdr)) {
871 UINT64 RelIdx;
872
873 //
874 // Determine the symbol table referenced by the relocation data.
875 //
876 Elf_Shdr *SymtabShdr = GetShdrByIndex(RelShdr->sh_link);
877 UINT8 *Symtab = (UINT8*)mEhdr + SymtabShdr->sh_offset;
878
879 //
880 // Process all relocation entries for this section.
881 //
882 for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += (UINT32) RelShdr->sh_entsize) {
883
884 //
885 // Set pointer to relocation entry
886 //
887 Elf_Rela *Rel = (Elf_Rela *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx);
888
889 //
890 // Set pointer to symbol table entry associated with the relocation entry.
891 //
892 Elf_Sym *Sym = (Elf_Sym *)(Symtab + ELF_R_SYM(Rel->r_info) * SymtabShdr->sh_entsize);
893
894 Elf_Shdr *SymShdr;
895 UINT8 *Targ;
896
897 //
898 // Check section header index found in symbol table and get the section
899 // header location.
900 //
901 if (Sym->st_shndx == SHN_UNDEF
902 || Sym->st_shndx >= mEhdr->e_shnum) {
903 const UINT8 *SymName = GetSymName(Sym);
904 if (SymName == NULL) {
905 SymName = (const UINT8 *)"<unknown>";
906 }
907
908 Error (NULL, 0, 3000, "Invalid",
909 "%s: Bad definition for symbol '%s'@%#llx or unsupported symbol type. "
910 "For example, absolute and undefined symbols are not supported.",
911 mInImageName, SymName, Sym->st_value);
912
913 exit(EXIT_FAILURE);
914 }
915 SymShdr = GetShdrByIndex(Sym->st_shndx);
916
917 //
918 // Convert the relocation data to a pointer into the coff file.
919 //
920 // Note:
921 // r_offset is the virtual address of the storage unit to be relocated.
922 // sh_addr is the virtual address for the base of the section.
923 //
924 // r_offset in a memory address.
925 // Convert it to a pointer in the coff file.
926 //
927 Targ = mCoffFile + SecOffset + (Rel->r_offset - SecShdr->sh_addr);
928
929 //
930 // Determine how to handle each relocation type based on the machine type.
931 //
932 if (mEhdr->e_machine == EM_X86_64) {
933 switch (ELF_R_TYPE(Rel->r_info)) {
934 case R_X86_64_NONE:
935 break;
936 case R_X86_64_64:
937 //
938 // Absolute relocation.
939 //
940 VerboseMsg ("R_X86_64_64");
941 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
942 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
943 *(UINT64 *)Targ);
944 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
945 VerboseMsg ("Relocation: 0x%016LX", *(UINT64*)Targ);
946 break;
947 case R_X86_64_32:
948 VerboseMsg ("R_X86_64_32");
949 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
950 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
951 *(UINT32 *)Targ);
952 *(UINT32 *)Targ = (UINT32)((UINT64)(*(UINT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
953 VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);
954 break;
955 case R_X86_64_32S:
956 VerboseMsg ("R_X86_64_32S");
957 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
958 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
959 *(UINT32 *)Targ);
960 *(INT32 *)Targ = (INT32)((INT64)(*(INT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
961 VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);
962 break;
963
964 case R_X86_64_PLT32:
965 //
966 // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is
967 // possible since we know all code symbol references resolve to
968 // definitions in the same module (UEFI has no shared libraries),
969 // and so there is never a reason to jump via a PLT entry,
970 // allowing us to resolve the reference using the symbol directly.
971 //
972 VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");
973 /* fall through */
974 case R_X86_64_PC32:
975 //
976 // Relative relocation: Symbol - Ip + Addend
977 //
978 VerboseMsg ("R_X86_64_PC32");
979 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
980 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
981 *(UINT32 *)Targ);
982 *(UINT32 *)Targ = (UINT32) (*(UINT32 *)Targ
983 + (mCoffSectionsOffset[Sym->st_shndx] - SymShdr->sh_addr)
984 - (SecOffset - SecShdr->sh_addr));
985 VerboseMsg ("Relocation: 0x%08X", *(UINT32 *)Targ);
986 break;
987 case R_X86_64_GOTPCREL:
988 case R_X86_64_GOTPCRELX:
989 case R_X86_64_REX_GOTPCRELX:
990 VerboseMsg ("R_X86_64_GOTPCREL family");
991 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
992 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
993 *(UINT32 *)Targ);
994 GOTEntryRva = Rel->r_offset - Rel->r_addend + *(INT32 *)Targ;
995 FindElfGOTSectionFromGOTEntryElfRva(GOTEntryRva);
996 *(UINT32 *)Targ = (UINT32) (*(UINT32 *)Targ
997 + (mCoffSectionsOffset[mGOTShindex] - mGOTShdr->sh_addr)
998 - (SecOffset - SecShdr->sh_addr));
999 VerboseMsg ("Relocation: 0x%08X", *(UINT32 *)Targ);
1000 GOTEntryRva += (mCoffSectionsOffset[mGOTShindex] - mGOTShdr->sh_addr); // ELF Rva -> COFF Rva
1001 if (AccumulateCoffGOTEntries((UINT32)GOTEntryRva)) {
1002 //
1003 // Relocate GOT entry if it's the first time we run into it
1004 //
1005 Targ = mCoffFile + GOTEntryRva;
1006 //
1007 // Limitation: The following three statements assume memory
1008 // at *Targ is valid because the section containing the GOT
1009 // has already been copied from the ELF image to the Coff image.
1010 // This pre-condition presently holds because the GOT is placed
1011 // in section .text, and the ELF text sections are all copied
1012 // prior to reaching this point.
1013 // If the pre-condition is violated in the future, this fixup
1014 // either needs to be deferred after the GOT section is copied
1015 // to the Coff image, or the fixup should be performed on the
1016 // source Elf image instead of the destination Coff image.
1017 //
1018 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1019 (UINT32)GOTEntryRva,
1020 *(UINT64 *)Targ);
1021 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
1022 VerboseMsg ("Relocation: 0x%016LX", *(UINT64*)Targ);
1023 }
1024 break;
1025 default:
1026 Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1027 }
1028 } else if (mEhdr->e_machine == EM_AARCH64) {
1029
1030 switch (ELF_R_TYPE(Rel->r_info)) {
1031
1032 case R_AARCH64_ADR_PREL_PG_HI21:
1033 //
1034 // AArch64 PG_H21 relocations are typically paired with ABS_LO12
1035 // relocations, where a PC-relative reference with +/- 4 GB range is
1036 // split into a relative high part and an absolute low part. Since
1037 // the absolute low part represents the offset into a 4 KB page, we
1038 // either have to convert the ADRP into an ADR instruction, or we
1039 // need to use a section alignment of at least 4 KB, so that the
1040 // binary appears at a correct offset at runtime. In any case, we
1041 // have to make sure that the 4 KB relative offsets of both the
1042 // section containing the reference as well as the section to which
1043 // it refers have not been changed during PE/COFF conversion (i.e.,
1044 // in ScanSections64() above).
1045 //
1046 if (mCoffAlignment < 0x1000) {
1047 //
1048 // Attempt to convert the ADRP into an ADR instruction.
1049 // This is only possible if the symbol is within +/- 1 MB.
1050 //
1051 INT64 Offset;
1052
1053 // Decode the ADRP instruction
1054 Offset = (INT32)((*(UINT32 *)Targ & 0xffffe0) << 8);
1055 Offset = (Offset << (6 - 5)) | ((*(UINT32 *)Targ & 0x60000000) >> (29 - 12));
1056
1057 //
1058 // ADRP offset is relative to the previous page boundary,
1059 // whereas ADR offset is relative to the instruction itself.
1060 // So fix up the offset so it points to the page containing
1061 // the symbol.
1062 //
1063 Offset -= (UINTN)(Targ - mCoffFile) & 0xfff;
1064
1065 if (Offset < -0x100000 || Offset > 0xfffff) {
1066 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s due to its size (> 1 MB), this module requires 4 KB section alignment.",
1067 mInImageName);
1068 break;
1069 }
1070
1071 // Re-encode the offset as an ADR instruction
1072 *(UINT32 *)Targ &= 0x1000001f;
1073 *(UINT32 *)Targ |= ((Offset & 0x1ffffc) << (5 - 2)) | ((Offset & 0x3) << 29);
1074 }
1075 /* fall through */
1076
1077 case R_AARCH64_ADD_ABS_LO12_NC:
1078 case R_AARCH64_LDST8_ABS_LO12_NC:
1079 case R_AARCH64_LDST16_ABS_LO12_NC:
1080 case R_AARCH64_LDST32_ABS_LO12_NC:
1081 case R_AARCH64_LDST64_ABS_LO12_NC:
1082 case R_AARCH64_LDST128_ABS_LO12_NC:
1083 if (((SecShdr->sh_addr ^ SecOffset) & 0xfff) != 0 ||
1084 ((SymShdr->sh_addr ^ mCoffSectionsOffset[Sym->st_shndx]) & 0xfff) != 0) {
1085 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 small code model requires identical ELF and PE/COFF section offsets modulo 4 KB.",
1086 mInImageName);
1087 break;
1088 }
1089 /* fall through */
1090
1091 case R_AARCH64_ADR_PREL_LO21:
1092 case R_AARCH64_CONDBR19:
1093 case R_AARCH64_LD_PREL_LO19:
1094 case R_AARCH64_CALL26:
1095 case R_AARCH64_JUMP26:
1096 case R_AARCH64_PREL64:
1097 case R_AARCH64_PREL32:
1098 case R_AARCH64_PREL16:
1099 //
1100 // The GCC toolchains (i.e., binutils) may corrupt section relative
1101 // relocations when emitting relocation sections into fully linked
1102 // binaries. More specifically, they tend to fail to take into
1103 // account the fact that a '.rodata + XXX' relocation needs to have
1104 // its addend recalculated once .rodata is merged into the .text
1105 // section, and the relocation emitted into the .rela.text section.
1106 //
1107 // We cannot really recover from this loss of information, so the
1108 // only workaround is to prevent having to recalculate any relative
1109 // relocations at all, by using a linker script that ensures that
1110 // the offset between the Place and the Symbol is the same in both
1111 // the ELF and the PE/COFF versions of the binary.
1112 //
1113 if ((SymShdr->sh_addr - SecShdr->sh_addr) !=
1114 (mCoffSectionsOffset[Sym->st_shndx] - SecOffset)) {
1115 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 relative relocations require identical ELF and PE/COFF section offsets",
1116 mInImageName);
1117 }
1118 break;
1119
1120 // Absolute relocations.
1121 case R_AARCH64_ABS64:
1122 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
1123 break;
1124
1125 default:
1126 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1127 }
1128 } else {
1129 Error (NULL, 0, 3000, "Invalid", "Not a supported machine type");
1130 }
1131 }
1132 }
1133 }
1134
1135 return TRUE;
1136 }
1137
1138 STATIC
1139 VOID
1140 WriteRelocations64 (
1141 VOID
1142 )
1143 {
1144 UINT32 Index;
1145 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
1146 EFI_IMAGE_DATA_DIRECTORY *Dir;
1147
1148 for (Index = 0; Index < mEhdr->e_shnum; Index++) {
1149 Elf_Shdr *RelShdr = GetShdrByIndex(Index);
1150 if ((RelShdr->sh_type == SHT_REL) || (RelShdr->sh_type == SHT_RELA)) {
1151 Elf_Shdr *SecShdr = GetShdrByIndex (RelShdr->sh_info);
1152 if (IsTextShdr(SecShdr) || IsDataShdr(SecShdr)) {
1153 UINT64 RelIdx;
1154
1155 for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += RelShdr->sh_entsize) {
1156 Elf_Rela *Rel = (Elf_Rela *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx);
1157
1158 if (mEhdr->e_machine == EM_X86_64) {
1159 switch (ELF_R_TYPE(Rel->r_info)) {
1160 case R_X86_64_NONE:
1161 case R_X86_64_PC32:
1162 case R_X86_64_PLT32:
1163 case R_X86_64_GOTPCREL:
1164 case R_X86_64_GOTPCRELX:
1165 case R_X86_64_REX_GOTPCRELX:
1166 break;
1167 case R_X86_64_64:
1168 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",
1169 mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr));
1170 CoffAddFixup(
1171 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1172 + (Rel->r_offset - SecShdr->sh_addr)),
1173 EFI_IMAGE_REL_BASED_DIR64);
1174 break;
1175 case R_X86_64_32S:
1176 case R_X86_64_32:
1177 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",
1178 mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr));
1179 CoffAddFixup(
1180 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1181 + (Rel->r_offset - SecShdr->sh_addr)),
1182 EFI_IMAGE_REL_BASED_HIGHLOW);
1183 break;
1184 default:
1185 Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1186 }
1187 } else if (mEhdr->e_machine == EM_AARCH64) {
1188
1189 switch (ELF_R_TYPE(Rel->r_info)) {
1190 case R_AARCH64_ADR_PREL_LO21:
1191 case R_AARCH64_CONDBR19:
1192 case R_AARCH64_LD_PREL_LO19:
1193 case R_AARCH64_CALL26:
1194 case R_AARCH64_JUMP26:
1195 case R_AARCH64_PREL64:
1196 case R_AARCH64_PREL32:
1197 case R_AARCH64_PREL16:
1198 case R_AARCH64_ADR_PREL_PG_HI21:
1199 case R_AARCH64_ADD_ABS_LO12_NC:
1200 case R_AARCH64_LDST8_ABS_LO12_NC:
1201 case R_AARCH64_LDST16_ABS_LO12_NC:
1202 case R_AARCH64_LDST32_ABS_LO12_NC:
1203 case R_AARCH64_LDST64_ABS_LO12_NC:
1204 case R_AARCH64_LDST128_ABS_LO12_NC:
1205 //
1206 // No fixups are required for relative relocations, provided that
1207 // the relative offsets between sections have been preserved in
1208 // the ELF to PE/COFF conversion. We have already asserted that
1209 // this is the case in WriteSections64 ().
1210 //
1211 break;
1212
1213 case R_AARCH64_ABS64:
1214 CoffAddFixup(
1215 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1216 + (Rel->r_offset - SecShdr->sh_addr)),
1217 EFI_IMAGE_REL_BASED_DIR64);
1218 break;
1219
1220 case R_AARCH64_ABS32:
1221 CoffAddFixup(
1222 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1223 + (Rel->r_offset - SecShdr->sh_addr)),
1224 EFI_IMAGE_REL_BASED_HIGHLOW);
1225 break;
1226
1227 default:
1228 Error (NULL, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1229 }
1230 } else {
1231 Error (NULL, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr->e_machine);
1232 }
1233 }
1234 if (mEhdr->e_machine == EM_X86_64 && RelShdr->sh_info == mGOTShindex) {
1235 //
1236 // Tack relocations for GOT entries after other relocations for
1237 // the section the GOT is in, as it's usually found at the end
1238 // of the section. This is done in order to maintain Rva order
1239 // of Coff relocations.
1240 //
1241 EmitGOTRelocations();
1242 }
1243 }
1244 }
1245 }
1246
1247 if (mEhdr->e_machine == EM_X86_64) {
1248 //
1249 // This is a safety net just in case the GOT is in a section
1250 // with no other relocations and the first invocation of
1251 // EmitGOTRelocations() above was skipped. This invocation
1252 // does not maintain Rva order of Coff relocations.
1253 // At present, with a single text section, all references to
1254 // the GOT and the GOT itself reside in section .text, so
1255 // if there's a GOT at all, the first invocation above
1256 // is executed.
1257 //
1258 EmitGOTRelocations();
1259 }
1260 //
1261 // Pad by adding empty entries.
1262 //
1263 while (mCoffOffset & (mCoffAlignment - 1)) {
1264 CoffAddFixupEntry(0);
1265 }
1266
1267 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
1268 Dir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
1269 Dir->Size = mCoffOffset - mRelocOffset;
1270 if (Dir->Size == 0) {
1271 // If no relocations, null out the directory entry and don't add the .reloc section
1272 Dir->VirtualAddress = 0;
1273 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
1274 } else {
1275 Dir->VirtualAddress = mRelocOffset;
1276 CreateSectionHeader (".reloc", mRelocOffset, mCoffOffset - mRelocOffset,
1277 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1278 | EFI_IMAGE_SCN_MEM_DISCARDABLE
1279 | EFI_IMAGE_SCN_MEM_READ);
1280 }
1281 }
1282
1283 STATIC
1284 VOID
1285 WriteDebug64 (
1286 VOID
1287 )
1288 {
1289 UINT32 Len;
1290 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
1291 EFI_IMAGE_DATA_DIRECTORY *DataDir;
1292 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *Dir;
1293 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *Nb10;
1294
1295 Len = strlen(mInImageName) + 1;
1296
1297 Dir = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(mCoffFile + mDebugOffset);
1298 Dir->Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW;
1299 Dir->SizeOfData = sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + Len;
1300 Dir->RVA = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
1301 Dir->FileOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
1302
1303 Nb10 = (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY*)(Dir + 1);
1304 Nb10->Signature = CODEVIEW_SIGNATURE_NB10;
1305 strcpy ((char *)(Nb10 + 1), mInImageName);
1306
1307
1308 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
1309 DataDir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG];
1310 DataDir->VirtualAddress = mDebugOffset;
1311 DataDir->Size = sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
1312 }
1313
1314 STATIC
1315 VOID
1316 SetImageSize64 (
1317 VOID
1318 )
1319 {
1320 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
1321
1322 //
1323 // Set image size
1324 //
1325 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
1326 NtHdr->Pe32Plus.OptionalHeader.SizeOfImage = mCoffOffset;
1327 }
1328
1329 STATIC
1330 VOID
1331 CleanUp64 (
1332 VOID
1333 )
1334 {
1335 if (mCoffSectionsOffset != NULL) {
1336 free (mCoffSectionsOffset);
1337 }
1338 }
1339
1340