]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/GenFw/Elf64Convert.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / BaseTools / Source / C / GenFw / Elf64Convert.c
1 /** @file
2 Elf64 convert solution
3
4 Copyright (c) 2010 - 2021, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2013-2022, ARM Ltd. All rights reserved.<BR>
6 Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
7 Portions Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
8
9 SPDX-License-Identifier: BSD-2-Clause-Patent
10
11 **/
12
13 #include "WinNtInclude.h"
14
15 #ifndef __GNUC__
16 #include <windows.h>
17 #include <io.h>
18 #endif
19 #include <assert.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include <ctype.h>
25
26 #include <Common/UefiBaseTypes.h>
27 #include <IndustryStandard/PeImage.h>
28
29 #include "PeCoffLib.h"
30 #include "EfiUtilityMsgs.h"
31
32 #include "GenFw.h"
33 #include "ElfConvert.h"
34 #include "Elf64Convert.h"
35
36 STATIC
37 VOID
38 ScanSections64 (
39 VOID
40 );
41
42 STATIC
43 BOOLEAN
44 WriteSections64 (
45 SECTION_FILTER_TYPES FilterType
46 );
47
48 STATIC
49 VOID
50 WriteRelocations64 (
51 VOID
52 );
53
54 STATIC
55 VOID
56 WriteDebug64 (
57 VOID
58 );
59
60 STATIC
61 VOID
62 WriteExport64 (
63 VOID
64 );
65
66 STATIC
67 VOID
68 SetImageSize64 (
69 VOID
70 );
71
72 STATIC
73 VOID
74 CleanUp64 (
75 VOID
76 );
77
78 //
79 // Rename ELF32 structures to common names to help when porting to ELF64.
80 //
81 typedef Elf64_Shdr Elf_Shdr;
82 typedef Elf64_Ehdr Elf_Ehdr;
83 typedef Elf64_Rel Elf_Rel;
84 typedef Elf64_Rela Elf_Rela;
85 typedef Elf64_Sym Elf_Sym;
86 typedef Elf64_Phdr Elf_Phdr;
87 typedef Elf64_Dyn Elf_Dyn;
88 #define ELFCLASS ELFCLASS64
89 #define ELF_R_TYPE(r) ELF64_R_TYPE(r)
90 #define ELF_R_SYM(r) ELF64_R_SYM(r)
91
92 //
93 // Well known ELF structures.
94 //
95 STATIC Elf_Ehdr *mEhdr;
96 STATIC Elf_Shdr *mShdrBase;
97 STATIC Elf_Phdr *mPhdrBase;
98
99 //
100 // GOT information
101 //
102 STATIC Elf_Shdr *mGOTShdr = NULL;
103 STATIC UINT32 mGOTShindex = 0;
104 STATIC UINT32 *mGOTCoffEntries = NULL;
105 STATIC UINT32 mGOTMaxCoffEntries = 0;
106 STATIC UINT32 mGOTNumCoffEntries = 0;
107
108 //
109 // Coff information
110 //
111 STATIC UINT32 mCoffAlignment = 0x20;
112
113 //
114 // PE section alignment.
115 //
116 STATIC UINT16 mCoffNbrSections = 4;
117
118 //
119 // ELF sections to offset in Coff file.
120 //
121 STATIC UINT32 *mCoffSectionsOffset = NULL;
122
123 //
124 // Offsets in COFF file
125 //
126 STATIC UINT32 mNtHdrOffset;
127 STATIC UINT32 mTextOffset;
128 STATIC UINT32 mDataOffset;
129 STATIC UINT32 mHiiRsrcOffset;
130 STATIC UINT32 mRelocOffset;
131 STATIC UINT32 mDebugOffset;
132 STATIC UINT32 mExportOffset;
133 //
134 // Used for RISC-V relocations.
135 //
136 STATIC UINT8 *mRiscVPass1Targ = NULL;
137 STATIC Elf_Shdr *mRiscVPass1Sym = NULL;
138 STATIC Elf64_Half mRiscVPass1SymSecIndex = 0;
139 STATIC INT32 mRiscVPass1Offset;
140 STATIC INT32 mRiscVPass1GotFixup;
141
142 //
143 // Used for Export section.
144 //
145 STATIC UINT32 mExportSize;
146 STATIC UINT32 mExportRVA[PRM_MODULE_EXPORT_SYMBOL_NUM];
147 STATIC UINT32 mExportSymNum;
148 STATIC CHAR8 mExportSymName[PRM_MODULE_EXPORT_SYMBOL_NUM][PRM_HANDLER_NAME_MAXIMUM_LENGTH];
149
150 //
151 // Initialization Function
152 //
153 BOOLEAN
154 InitializeElf64 (
155 UINT8 *FileBuffer,
156 ELF_FUNCTION_TABLE *ElfFunctions
157 )
158 {
159 //
160 // Initialize data pointer and structures.
161 //
162 VerboseMsg ("Set EHDR");
163 mEhdr = (Elf_Ehdr*) FileBuffer;
164
165 //
166 // Check the ELF64 specific header information.
167 //
168 VerboseMsg ("Check ELF64 Header Information");
169 if (mEhdr->e_ident[EI_CLASS] != ELFCLASS64) {
170 Error (NULL, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS64");
171 return FALSE;
172 }
173 if (mEhdr->e_ident[EI_DATA] != ELFDATA2LSB) {
174 Error (NULL, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
175 return FALSE;
176 }
177 if ((mEhdr->e_type != ET_EXEC) && (mEhdr->e_type != ET_DYN)) {
178 Error (NULL, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
179 return FALSE;
180 }
181 if (!((mEhdr->e_machine == EM_X86_64) || (mEhdr->e_machine == EM_AARCH64) || (mEhdr->e_machine == EM_RISCV64) || (mEhdr->e_machine == EM_LOONGARCH))) {
182 Warning (NULL, 0, 3000, "Unsupported", "ELF e_machine is not Elf64 machine.");
183 }
184 if (mEhdr->e_version != EV_CURRENT) {
185 Error (NULL, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr->e_version, EV_CURRENT);
186 return FALSE;
187 }
188
189 if (mExportFlag) {
190 if ((mEhdr->e_machine != EM_X86_64) && (mEhdr->e_machine != EM_AARCH64)) {
191 Error (NULL, 0, 3000, "Unsupported", "--prm option currently only supports X64 and AArch64 archs.");
192 return FALSE;
193 }
194 }
195
196 //
197 // Update section header pointers
198 //
199 VerboseMsg ("Update Header Pointers");
200 mShdrBase = (Elf_Shdr *)((UINT8 *)mEhdr + mEhdr->e_shoff);
201 mPhdrBase = (Elf_Phdr *)((UINT8 *)mEhdr + mEhdr->e_phoff);
202
203 //
204 // Create COFF Section offset buffer and zero.
205 //
206 VerboseMsg ("Create COFF Section Offset Buffer");
207 mCoffSectionsOffset = (UINT32 *)malloc(mEhdr->e_shnum * sizeof (UINT32));
208 if (mCoffSectionsOffset == NULL) {
209 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
210 return FALSE;
211 }
212 memset(mCoffSectionsOffset, 0, mEhdr->e_shnum * sizeof(UINT32));
213
214 //
215 // Fill in function pointers.
216 //
217 VerboseMsg ("Fill in Function Pointers");
218 ElfFunctions->ScanSections = ScanSections64;
219 ElfFunctions->WriteSections = WriteSections64;
220 ElfFunctions->WriteRelocations = WriteRelocations64;
221 ElfFunctions->WriteDebug = WriteDebug64;
222 ElfFunctions->SetImageSize = SetImageSize64;
223 ElfFunctions->CleanUp = CleanUp64;
224
225 if (mExportFlag) {
226 mCoffNbrSections ++;
227 ElfFunctions->WriteExport = WriteExport64;
228 }
229
230 return TRUE;
231 }
232
233
234 //
235 // Header by Index functions
236 //
237 STATIC
238 Elf_Shdr*
239 GetShdrByIndex (
240 UINT32 Num
241 )
242 {
243 if (Num >= mEhdr->e_shnum) {
244 Error (NULL, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num);
245 exit(EXIT_FAILURE);
246 }
247
248 return (Elf_Shdr*)((UINT8*)mShdrBase + Num * mEhdr->e_shentsize);
249 }
250
251 STATIC
252 UINT32
253 CoffAlign (
254 UINT32 Offset
255 )
256 {
257 return (Offset + mCoffAlignment - 1) & ~(mCoffAlignment - 1);
258 }
259
260 STATIC
261 UINT32
262 DebugRvaAlign (
263 UINT32 Offset
264 )
265 {
266 return (Offset + 3) & ~3;
267 }
268
269 //
270 // filter functions
271 //
272 STATIC
273 BOOLEAN
274 IsTextShdr (
275 Elf_Shdr *Shdr
276 )
277 {
278 return (BOOLEAN) (((Shdr->sh_flags & (SHF_EXECINSTR | SHF_ALLOC)) == (SHF_EXECINSTR | SHF_ALLOC)) ||
279 ((Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == SHF_ALLOC));
280 }
281
282 STATIC
283 BOOLEAN
284 IsHiiRsrcShdr (
285 Elf_Shdr *Shdr
286 )
287 {
288 Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx);
289
290 return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_HII_SECTION_NAME) == 0);
291 }
292
293 STATIC
294 BOOLEAN
295 IsSymbolShdr (
296 Elf_Shdr *Shdr
297 )
298 {
299 Elf_Shdr *Namehdr = GetShdrByIndex(mEhdr->e_shstrndx);
300
301 return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namehdr->sh_offset + Shdr->sh_name, ELF_SYMBOL_SECTION_NAME) == 0);
302 }
303
304 STATIC
305 BOOLEAN
306 IsDataShdr (
307 Elf_Shdr *Shdr
308 )
309 {
310 if (IsHiiRsrcShdr(Shdr)) {
311 return FALSE;
312 }
313 return (BOOLEAN) (Shdr->sh_flags & (SHF_EXECINSTR | SHF_WRITE | SHF_ALLOC)) == (SHF_ALLOC | SHF_WRITE);
314 }
315
316 STATIC
317 BOOLEAN
318 IsStrtabShdr (
319 Elf_Shdr *Shdr
320 )
321 {
322 Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx);
323
324 return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_STRTAB_SECTION_NAME) == 0);
325 }
326
327 STATIC
328 Elf_Shdr *
329 FindStrtabShdr (
330 VOID
331 )
332 {
333 UINT32 i;
334 for (i = 0; i < mEhdr->e_shnum; i++) {
335 Elf_Shdr *shdr = GetShdrByIndex(i);
336 if (IsStrtabShdr(shdr)) {
337 return shdr;
338 }
339 }
340 return NULL;
341 }
342
343 STATIC
344 const UINT8 *
345 GetSymName (
346 Elf_Sym *Sym
347 )
348 {
349 Elf_Shdr *StrtabShdr;
350 UINT8 *StrtabContents;
351 BOOLEAN foundEnd;
352 UINT32 i;
353
354 if (Sym->st_name == 0) {
355 return NULL;
356 }
357
358 StrtabShdr = FindStrtabShdr();
359 if (StrtabShdr == NULL) {
360 return NULL;
361 }
362
363 assert(Sym->st_name < StrtabShdr->sh_size);
364
365 StrtabContents = (UINT8*)mEhdr + StrtabShdr->sh_offset;
366
367 foundEnd = FALSE;
368 for (i= Sym->st_name; (i < StrtabShdr->sh_size) && !foundEnd; i++) {
369 foundEnd = (BOOLEAN)(StrtabContents[i] == 0);
370 }
371 assert(foundEnd);
372
373 return StrtabContents + Sym->st_name;
374 }
375
376 //
377 // Get Prm Handler number and name
378 //
379 STATIC
380 VOID
381 FindPrmHandler (
382 UINT64 Offset
383 )
384 {
385 PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER *PrmExport;
386 PRM_HANDLER_EXPORT_DESCRIPTOR_STRUCT *PrmHandler;
387 UINT32 HandlerNum;
388
389 PrmExport = (PRM_MODULE_EXPORT_DESCRIPTOR_STRUCT_HEADER*)((UINT8*)mEhdr + Offset);
390 PrmHandler = (PRM_HANDLER_EXPORT_DESCRIPTOR_STRUCT *)(PrmExport + 1);
391
392 for (HandlerNum = 0; HandlerNum < PrmExport->NumberPrmHandlers; HandlerNum++) {
393 strcpy(mExportSymName[mExportSymNum], PrmHandler->PrmHandlerName);
394 mExportSymNum ++;
395 PrmHandler += 1;
396
397 //
398 // Check if PRM handler number is larger than (PRM_MODULE_EXPORT_SYMBOL_NUM - 1)
399 //
400 if (mExportSymNum >= (PRM_MODULE_EXPORT_SYMBOL_NUM - 1)) {
401 Error (NULL, 0, 3000, "Invalid", "FindPrmHandler: Number %u is too high.", mExportSymNum);
402 exit(EXIT_FAILURE);
403 }
404 }
405 }
406
407 //
408 // Find the ELF section hosting the GOT from an ELF Rva
409 // of a single GOT entry. Normally, GOT is placed in
410 // ELF .text section, so assume once we find in which
411 // section the GOT is, all GOT entries are there, and
412 // just verify this.
413 //
414 STATIC
415 VOID
416 FindElfGOTSectionFromGOTEntryElfRva (
417 Elf64_Addr GOTEntryElfRva
418 )
419 {
420 UINT32 i;
421 if (mGOTShdr != NULL) {
422 if (GOTEntryElfRva >= mGOTShdr->sh_addr &&
423 GOTEntryElfRva < mGOTShdr->sh_addr + mGOTShdr->sh_size) {
424 return;
425 }
426 Error (NULL, 0, 3000, "Unsupported", "FindElfGOTSectionFromGOTEntryElfRva: GOT entries found in multiple sections.");
427 exit(EXIT_FAILURE);
428 }
429 for (i = 0; i < mEhdr->e_shnum; i++) {
430 Elf_Shdr *shdr = GetShdrByIndex(i);
431 if (GOTEntryElfRva >= shdr->sh_addr &&
432 GOTEntryElfRva < shdr->sh_addr + shdr->sh_size) {
433 mGOTShdr = shdr;
434 mGOTShindex = i;
435 return;
436 }
437 }
438 Error (NULL, 0, 3000, "Invalid", "FindElfGOTSectionFromGOTEntryElfRva: ElfRva 0x%016LX for GOT entry not found in any section.", GOTEntryElfRva);
439 exit(EXIT_FAILURE);
440 }
441
442 //
443 // Stores locations of GOT entries in COFF image.
444 // Returns TRUE if GOT entry is new.
445 // Simple implementation as number of GOT
446 // entries is expected to be low.
447 //
448
449 STATIC
450 BOOLEAN
451 AccumulateCoffGOTEntries (
452 UINT32 GOTCoffEntry
453 )
454 {
455 UINT32 i;
456 if (mGOTCoffEntries != NULL) {
457 for (i = 0; i < mGOTNumCoffEntries; i++) {
458 if (mGOTCoffEntries[i] == GOTCoffEntry) {
459 return FALSE;
460 }
461 }
462 }
463 if (mGOTCoffEntries == NULL) {
464 mGOTCoffEntries = (UINT32*)malloc(5 * sizeof *mGOTCoffEntries);
465 if (mGOTCoffEntries == NULL) {
466 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
467 }
468 assert (mGOTCoffEntries != NULL);
469 mGOTMaxCoffEntries = 5;
470 mGOTNumCoffEntries = 0;
471 } else if (mGOTNumCoffEntries == mGOTMaxCoffEntries) {
472 mGOTCoffEntries = (UINT32*)realloc(mGOTCoffEntries, 2 * mGOTMaxCoffEntries * sizeof *mGOTCoffEntries);
473 if (mGOTCoffEntries == NULL) {
474 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
475 }
476 assert (mGOTCoffEntries != NULL);
477 mGOTMaxCoffEntries += mGOTMaxCoffEntries;
478 }
479 mGOTCoffEntries[mGOTNumCoffEntries++] = GOTCoffEntry;
480 return TRUE;
481 }
482
483 //
484 // 32-bit Unsigned integer comparator for qsort.
485 //
486 STATIC
487 int
488 UINT32Comparator (
489 const void* lhs,
490 const void* rhs
491 )
492 {
493 if (*(const UINT32*)lhs < *(const UINT32*)rhs) {
494 return -1;
495 }
496 return *(const UINT32*)lhs > *(const UINT32*)rhs;
497 }
498
499 //
500 // Emit accumulated Coff GOT entry relocations into
501 // Coff image. This function performs its job
502 // once and then releases the entry list, so
503 // it can safely be called multiple times.
504 //
505 STATIC
506 VOID
507 EmitGOTRelocations (
508 VOID
509 )
510 {
511 UINT32 i;
512 if (mGOTCoffEntries == NULL) {
513 return;
514 }
515 //
516 // Emit Coff relocations with Rvas ordered.
517 //
518 qsort(
519 mGOTCoffEntries,
520 mGOTNumCoffEntries,
521 sizeof *mGOTCoffEntries,
522 UINT32Comparator);
523 for (i = 0; i < mGOTNumCoffEntries; i++) {
524 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X", mGOTCoffEntries[i]);
525 CoffAddFixup(
526 mGOTCoffEntries[i],
527 EFI_IMAGE_REL_BASED_DIR64);
528 }
529 free(mGOTCoffEntries);
530 mGOTCoffEntries = NULL;
531 mGOTMaxCoffEntries = 0;
532 mGOTNumCoffEntries = 0;
533 }
534 //
535 // RISC-V 64 specific Elf WriteSection function.
536 //
537 STATIC
538 VOID
539 WriteSectionRiscV64 (
540 Elf_Rela *Rel,
541 UINT8 *Targ,
542 Elf_Shdr *SymShdr,
543 Elf_Sym *Sym
544 )
545 {
546 UINT32 Value;
547 UINT32 Value2;
548 Elf64_Addr GOTEntryRva;
549
550 switch (ELF_R_TYPE(Rel->r_info)) {
551 case R_RISCV_NONE:
552 break;
553
554 case R_RISCV_32:
555 *(UINT64 *)Targ = Sym->st_value + Rel->r_addend;
556 break;
557
558 case R_RISCV_64:
559 *(UINT64 *)Targ = Sym->st_value + Rel->r_addend;
560 break;
561
562 case R_RISCV_HI20:
563 mRiscVPass1Targ = Targ;
564 mRiscVPass1Sym = SymShdr;
565 mRiscVPass1SymSecIndex = Sym->st_shndx;
566 break;
567
568 case R_RISCV_LO12_I:
569 if (mRiscVPass1Sym == SymShdr && mRiscVPass1Targ != NULL && mRiscVPass1SymSecIndex == Sym->st_shndx && mRiscVPass1SymSecIndex != 0) {
570 Value = (UINT32)(RV_X(*(UINT32 *)mRiscVPass1Targ, 12, 20) << 12);
571 Value2 = (UINT32)(RV_X(*(UINT32 *)Targ, 20, 12));
572 if (Value2 & (RISCV_IMM_REACH/2)) {
573 Value2 |= ~(RISCV_IMM_REACH-1);
574 }
575 Value += Value2;
576 Value = Value - (UINT32)SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
577 Value2 = RISCV_CONST_HIGH_PART (Value);
578 *(UINT32 *)mRiscVPass1Targ = (RV_X (Value2, 12, 20) << 12) | \
579 (RV_X (*(UINT32 *)mRiscVPass1Targ, 0, 12));
580 *(UINT32 *)Targ = (RV_X (Value, 0, 12) << 20) | \
581 (RV_X (*(UINT32 *)Targ, 0, 20));
582 }
583 mRiscVPass1Sym = NULL;
584 mRiscVPass1Targ = NULL;
585 mRiscVPass1SymSecIndex = 0;
586 break;
587
588 case R_RISCV_LO12_S:
589 if (mRiscVPass1Sym == SymShdr && mRiscVPass1Targ != NULL && mRiscVPass1SymSecIndex == Sym->st_shndx && mRiscVPass1SymSecIndex != 0) {
590 Value = (UINT32)(RV_X(*(UINT32 *)mRiscVPass1Targ, 12, 20) << 12);
591 Value2 = (UINT32)(RV_X(*(UINT32 *)Targ, 7, 5) | (RV_X(*(UINT32 *)Targ, 25, 7) << 5));
592 if (Value2 & (RISCV_IMM_REACH/2)) {
593 Value2 |= ~(RISCV_IMM_REACH-1);
594 }
595 Value += Value2;
596 Value = Value - (UINT32)SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
597 Value2 = RISCV_CONST_HIGH_PART (Value);
598 *(UINT32 *)mRiscVPass1Targ = (RV_X (Value2, 12, 20) << 12) | \
599 (RV_X (*(UINT32 *)mRiscVPass1Targ, 0, 12));
600 Value2 = *(UINT32 *)Targ & 0x01fff07f;
601 Value &= RISCV_IMM_REACH - 1;
602 *(UINT32 *)Targ = Value2 | (UINT32)(((RV_X(Value, 0, 5) << 7) | (RV_X(Value, 5, 7) << 25)));
603 }
604 mRiscVPass1Sym = NULL;
605 mRiscVPass1Targ = NULL;
606 mRiscVPass1SymSecIndex = 0;
607 break;
608
609 case R_RISCV_GOT_HI20:
610 GOTEntryRva = (Sym->st_value - Rel->r_offset);
611 mRiscVPass1Offset = RV_X(GOTEntryRva, 0, 12);
612 Value = (UINT32)RV_X(GOTEntryRva, 12, 20);
613 *(UINT32 *)Targ = (Value << 12) | (RV_X(*(UINT32*)Targ, 0, 12));
614
615 mRiscVPass1Targ = Targ;
616 mRiscVPass1Sym = SymShdr;
617 mRiscVPass1SymSecIndex = Sym->st_shndx;
618 mRiscVPass1GotFixup = 1;
619 break;
620
621 case R_RISCV_PCREL_HI20:
622 mRiscVPass1Targ = Targ;
623 mRiscVPass1Sym = SymShdr;
624 mRiscVPass1SymSecIndex = Sym->st_shndx;
625
626 Value = (UINT32)(RV_X(*(UINT32 *)mRiscVPass1Targ, 12, 20));
627 break;
628
629 case R_RISCV_PCREL_LO12_S:
630 if (mRiscVPass1Targ != NULL && mRiscVPass1Sym != NULL && mRiscVPass1SymSecIndex != 0) {
631 int i;
632 Value2 = (UINT32)(RV_X(*(UINT32 *)mRiscVPass1Targ, 12, 20));
633
634 Value = ((UINT32)(RV_X(*(UINT32 *)Targ, 25, 7)) << 5);
635 Value = (Value | (UINT32)(RV_X(*(UINT32 *)Targ, 7, 5)));
636
637 if(Value & (RISCV_IMM_REACH/2)) {
638 Value |= ~(RISCV_IMM_REACH-1);
639 }
640 Value = Value - (UINT32)mRiscVPass1Sym->sh_addr + mCoffSectionsOffset[mRiscVPass1SymSecIndex];
641
642 if(-2048 > (INT32)Value) {
643 i = (((INT32)Value * -1) / 4096);
644 Value2 -= i;
645 Value += 4096 * i;
646 if(-2048 > (INT32)Value) {
647 Value2 -= 1;
648 Value += 4096;
649 }
650 }
651 else if( 2047 < (INT32)Value) {
652 i = (Value / 4096);
653 Value2 += i;
654 Value -= 4096 * i;
655 if(2047 < (INT32)Value) {
656 Value2 += 1;
657 Value -= 4096;
658 }
659 }
660
661 // Update the IMM of SD instruction
662 //
663 // |31 25|24 20|19 15|14 12 |11 7|6 0|
664 // |-------------------------------------------|-------|
665 // |imm[11:5] | rs2 | rs1 | funct3 |imm[4:0] | opcode|
666 // ---------------------------------------------------
667
668 // First Zero out current IMM
669 *(UINT32 *)Targ &= ~0xfe000f80;
670
671 // Update with new IMM
672 *(UINT32 *)Targ |= (RV_X(Value, 5, 7) << 25);
673 *(UINT32 *)Targ |= (RV_X(Value, 0, 5) << 7);
674
675 // Update previous instruction
676 *(UINT32 *)mRiscVPass1Targ = (RV_X(Value2, 0, 20)<<12) | (RV_X(*(UINT32 *)mRiscVPass1Targ, 0, 12));
677 }
678 mRiscVPass1Sym = NULL;
679 mRiscVPass1Targ = NULL;
680 mRiscVPass1SymSecIndex = 0;
681 break;
682
683 case R_RISCV_PCREL_LO12_I:
684 if (mRiscVPass1Targ != NULL && mRiscVPass1Sym != NULL && mRiscVPass1SymSecIndex != 0) {
685 int i;
686 Value2 = (UINT32)(RV_X(*(UINT32 *)mRiscVPass1Targ, 12, 20));
687
688 if(mRiscVPass1GotFixup) {
689 Value = (UINT32)(mRiscVPass1Offset);
690 } else {
691 Value = (UINT32)(RV_X(*(UINT32 *)Targ, 20, 12));
692 if(Value & (RISCV_IMM_REACH/2)) {
693 Value |= ~(RISCV_IMM_REACH-1);
694 }
695 }
696 Value = Value - (UINT32)mRiscVPass1Sym->sh_addr + mCoffSectionsOffset[mRiscVPass1SymSecIndex];
697
698 if(-2048 > (INT32)Value) {
699 i = (((INT32)Value * -1) / 4096);
700 Value2 -= i;
701 Value += 4096 * i;
702 if(-2048 > (INT32)Value) {
703 Value2 -= 1;
704 Value += 4096;
705 }
706 }
707 else if( 2047 < (INT32)Value) {
708 i = (Value / 4096);
709 Value2 += i;
710 Value -= 4096 * i;
711 if(2047 < (INT32)Value) {
712 Value2 += 1;
713 Value -= 4096;
714 }
715 }
716
717 if(mRiscVPass1GotFixup) {
718 *(UINT32 *)Targ = (RV_X((UINT32)Value, 0, 12) << 20)
719 | (RV_X(*(UINT32*)Targ, 0, 20));
720 // Convert LD instruction to ADDI
721 //
722 // |31 20|19 15|14 12|11 7|6 0|
723 // |-----------------------------------------|
724 // |imm[11:0] | rs1 | 011 | rd | 0000011 | LD
725 // -----------------------------------------
726
727 // |-----------------------------------------|
728 // |imm[11:0] | rs1 | 000 | rd | 0010011 | ADDI
729 // -----------------------------------------
730
731 // To convert, let's first reset bits 12-14 and 0-6 using ~0x707f
732 // Then modify the opcode to ADDI (0010011)
733 // All other fields will remain same.
734
735 *(UINT32 *)Targ = ((*(UINT32 *)Targ & ~0x707f) | 0x13);
736 } else {
737 *(UINT32 *)Targ = (RV_X(Value, 0, 12) << 20) | (RV_X(*(UINT32*)Targ, 0, 20));
738 }
739 *(UINT32 *)mRiscVPass1Targ = (RV_X(Value2, 0, 20)<<12) | (RV_X(*(UINT32 *)mRiscVPass1Targ, 0, 12));
740 }
741 mRiscVPass1Sym = NULL;
742 mRiscVPass1Targ = NULL;
743 mRiscVPass1SymSecIndex = 0;
744 mRiscVPass1Offset = 0;
745 mRiscVPass1GotFixup = 0;
746 break;
747
748 case R_RISCV_ADD64:
749 case R_RISCV_SUB64:
750 case R_RISCV_ADD32:
751 case R_RISCV_SUB32:
752 case R_RISCV_BRANCH:
753 case R_RISCV_JAL:
754 case R_RISCV_GPREL_I:
755 case R_RISCV_GPREL_S:
756 case R_RISCV_CALL:
757 case R_RISCV_CALL_PLT:
758 case R_RISCV_RVC_BRANCH:
759 case R_RISCV_RVC_JUMP:
760 case R_RISCV_RELAX:
761 case R_RISCV_SUB6:
762 case R_RISCV_SET6:
763 case R_RISCV_SET8:
764 case R_RISCV_SET16:
765 case R_RISCV_SET32:
766 break;
767
768 default:
769 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_RISCV64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
770 }
771 }
772
773 //
774 // Elf functions interface implementation
775 //
776
777 STATIC
778 VOID
779 ScanSections64 (
780 VOID
781 )
782 {
783 UINT32 i;
784 EFI_IMAGE_DOS_HEADER *DosHdr;
785 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
786 UINT32 CoffEntry;
787 UINT32 SectionCount;
788 BOOLEAN FoundSection;
789 UINT32 Offset;
790
791 CoffEntry = 0;
792 mCoffOffset = 0;
793
794 //
795 // Coff file start with a DOS header.
796 //
797 mCoffOffset = sizeof(EFI_IMAGE_DOS_HEADER) + 0x40;
798 mNtHdrOffset = mCoffOffset;
799 switch (mEhdr->e_machine) {
800 case EM_X86_64:
801 case EM_AARCH64:
802 case EM_RISCV64:
803 case EM_LOONGARCH:
804 mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS64);
805 break;
806 default:
807 VerboseMsg ("%s unknown e_machine type %hu. Assume X64", mInImageName, mEhdr->e_machine);
808 mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS64);
809 break;
810 }
811
812 mTableOffset = mCoffOffset;
813 mCoffOffset += mCoffNbrSections * sizeof(EFI_IMAGE_SECTION_HEADER);
814
815 //
816 // Set mCoffAlignment to the maximum alignment of the input sections
817 // we care about
818 //
819 for (i = 0; i < mEhdr->e_shnum; i++) {
820 Elf_Shdr *shdr = GetShdrByIndex(i);
821 if (shdr->sh_addralign <= mCoffAlignment) {
822 continue;
823 }
824 if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr)) {
825 mCoffAlignment = (UINT32)shdr->sh_addralign;
826 }
827 }
828
829 //
830 // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT
831 //
832 if (mCoffAlignment > MAX_COFF_ALIGNMENT) {
833 Error (NULL, 0, 3000, "Invalid", "Section alignment is larger than MAX_COFF_ALIGNMENT.");
834 assert (FALSE);
835 }
836
837
838 //
839 // Move the PE/COFF header right before the first section. This will help us
840 // save space when converting to TE.
841 //
842 if (mCoffAlignment > mCoffOffset) {
843 mNtHdrOffset += mCoffAlignment - mCoffOffset;
844 mTableOffset += mCoffAlignment - mCoffOffset;
845 mCoffOffset = mCoffAlignment;
846 }
847
848 //
849 // First text sections.
850 //
851 mCoffOffset = CoffAlign(mCoffOffset);
852 mTextOffset = mCoffOffset;
853 FoundSection = FALSE;
854 SectionCount = 0;
855 for (i = 0; i < mEhdr->e_shnum; i++) {
856 Elf_Shdr *shdr = GetShdrByIndex(i);
857 if (IsTextShdr(shdr)) {
858 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
859 // the alignment field is valid
860 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
861 // if the section address is aligned we must align PE/COFF
862 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));
863 } else {
864 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
865 }
866 }
867
868 /* Relocate entry. */
869 if ((mEhdr->e_entry >= shdr->sh_addr) &&
870 (mEhdr->e_entry < shdr->sh_addr + shdr->sh_size)) {
871 CoffEntry = (UINT32) (mCoffOffset + mEhdr->e_entry - shdr->sh_addr);
872 }
873
874 //
875 // Set mTextOffset with the offset of the first '.text' section
876 //
877 if (!FoundSection) {
878 mTextOffset = mCoffOffset;
879 FoundSection = TRUE;
880 }
881
882 mCoffSectionsOffset[i] = mCoffOffset;
883 mCoffOffset += (UINT32) shdr->sh_size;
884 SectionCount ++;
885 }
886 }
887
888 if (!FoundSection && mOutImageType != FW_ACPI_IMAGE) {
889 Error (NULL, 0, 3000, "Invalid", "Did not find any '.text' section.");
890 assert (FALSE);
891 }
892
893 mDebugOffset = DebugRvaAlign(mCoffOffset);
894 mCoffOffset = CoffAlign(mCoffOffset);
895
896 if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {
897 Warning (NULL, 0, 0, NULL, "Multiple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName);
898 }
899
900 //
901 // Then data sections.
902 //
903 mDataOffset = mCoffOffset;
904 FoundSection = FALSE;
905 SectionCount = 0;
906 for (i = 0; i < mEhdr->e_shnum; i++) {
907 Elf_Shdr *shdr = GetShdrByIndex(i);
908 if (IsDataShdr(shdr)) {
909 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
910 // the alignment field is valid
911 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
912 // if the section address is aligned we must align PE/COFF
913 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));
914 } else {
915 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
916 }
917 }
918
919 //
920 // Set mDataOffset with the offset of the first '.data' section
921 //
922 if (!FoundSection) {
923 mDataOffset = mCoffOffset;
924 FoundSection = TRUE;
925 }
926 mCoffSectionsOffset[i] = mCoffOffset;
927 mCoffOffset += (UINT32) shdr->sh_size;
928 SectionCount ++;
929 }
930 }
931
932 //
933 // Make room for .debug data in .data (or .text if .data is empty) instead of
934 // putting it in a section of its own. This is explicitly allowed by the
935 // PE/COFF spec, and prevents bloat in the binary when using large values for
936 // section alignment.
937 //
938 if (SectionCount > 0) {
939 mDebugOffset = DebugRvaAlign(mCoffOffset);
940 }
941 mCoffOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY) +
942 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) +
943 strlen(mInImageName) + 1;
944
945 mCoffOffset = CoffAlign(mCoffOffset);
946 if (SectionCount == 0) {
947 mDataOffset = mCoffOffset;
948 }
949
950 if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {
951 Warning (NULL, 0, 0, NULL, "Multiple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName);
952 }
953
954 //
955 // The Symbol sections.
956 //
957 if (mExportFlag) {
958 UINT32 SymIndex;
959 Elf_Sym *Sym;
960 UINT64 SymNum;
961 const UINT8 *SymName;
962
963 mExportOffset = mCoffOffset;
964 mExportSize = sizeof(EFI_IMAGE_EXPORT_DIRECTORY) + strlen(mInImageName) + 1;
965
966 for (i = 0; i < mEhdr->e_shnum; i++) {
967
968 //
969 // Determine if this is a symbol section.
970 //
971 Elf_Shdr *shdr = GetShdrByIndex(i);
972 if (!IsSymbolShdr(shdr)) {
973 continue;
974 }
975
976 UINT8 *Symtab = (UINT8*)mEhdr + shdr->sh_offset;
977 SymNum = (shdr->sh_size) / (shdr->sh_entsize);
978
979 //
980 // First Get PrmModuleExportDescriptor
981 //
982 for (SymIndex = 0; SymIndex < SymNum; SymIndex++) {
983 Sym = (Elf_Sym *)(Symtab + SymIndex * shdr->sh_entsize);
984 SymName = GetSymName(Sym);
985 if (SymName == NULL) {
986 continue;
987 }
988
989 if (strcmp((CHAR8*)SymName, PRM_MODULE_EXPORT_DESCRIPTOR_NAME) == 0) {
990 //
991 // Find PrmHandler Number and Name
992 //
993 FindPrmHandler(Sym->st_value);
994
995 strcpy(mExportSymName[mExportSymNum], (CHAR8*)SymName);
996 mExportRVA[mExportSymNum] = (UINT32)(Sym->st_value);
997 mExportSize += 2 * EFI_IMAGE_EXPORT_ADDR_SIZE + EFI_IMAGE_EXPORT_ORDINAL_SIZE + strlen((CHAR8 *)SymName) + 1;
998 mExportSymNum ++;
999 break;
1000 }
1001 }
1002
1003 //
1004 // Second Get PrmHandler
1005 //
1006 for (SymIndex = 0; SymIndex < SymNum; SymIndex++) {
1007 UINT32 ExpIndex;
1008 Sym = (Elf_Sym *)(Symtab + SymIndex * shdr->sh_entsize);
1009 SymName = GetSymName(Sym);
1010 if (SymName == NULL) {
1011 continue;
1012 }
1013
1014 for (ExpIndex = 0; ExpIndex < (mExportSymNum -1); ExpIndex++) {
1015 if (strcmp((CHAR8*)SymName, mExportSymName[ExpIndex]) != 0) {
1016 continue;
1017 }
1018 mExportRVA[ExpIndex] = (UINT32)(Sym->st_value);
1019 mExportSize += 2 * EFI_IMAGE_EXPORT_ADDR_SIZE + EFI_IMAGE_EXPORT_ORDINAL_SIZE + strlen((CHAR8 *)SymName) + 1;
1020 }
1021 }
1022
1023 break;
1024 }
1025
1026 mCoffOffset += mExportSize;
1027 mCoffOffset = CoffAlign(mCoffOffset);
1028 }
1029
1030 //
1031 // The HII resource sections.
1032 //
1033 mHiiRsrcOffset = mCoffOffset;
1034 for (i = 0; i < mEhdr->e_shnum; i++) {
1035 Elf_Shdr *shdr = GetShdrByIndex(i);
1036 if (IsHiiRsrcShdr(shdr)) {
1037 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
1038 // the alignment field is valid
1039 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
1040 // if the section address is aligned we must align PE/COFF
1041 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));
1042 } else {
1043 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
1044 }
1045 }
1046 if (shdr->sh_size != 0) {
1047 mHiiRsrcOffset = mCoffOffset;
1048 mCoffSectionsOffset[i] = mCoffOffset;
1049 mCoffOffset += (UINT32) shdr->sh_size;
1050 mCoffOffset = CoffAlign(mCoffOffset);
1051 SetHiiResourceHeader ((UINT8*) mEhdr + shdr->sh_offset, mHiiRsrcOffset);
1052 }
1053 break;
1054 }
1055 }
1056
1057 mRelocOffset = mCoffOffset;
1058
1059 //
1060 // Allocate base Coff file. Will be expanded later for relocations.
1061 //
1062 mCoffFile = (UINT8 *)malloc(mCoffOffset);
1063 if (mCoffFile == NULL) {
1064 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
1065 }
1066 assert (mCoffFile != NULL);
1067 memset(mCoffFile, 0, mCoffOffset);
1068
1069 //
1070 // Fill headers.
1071 //
1072 DosHdr = (EFI_IMAGE_DOS_HEADER *)mCoffFile;
1073 DosHdr->e_magic = EFI_IMAGE_DOS_SIGNATURE;
1074 DosHdr->e_lfanew = mNtHdrOffset;
1075
1076 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION*)(mCoffFile + mNtHdrOffset);
1077
1078 NtHdr->Pe32Plus.Signature = EFI_IMAGE_NT_SIGNATURE;
1079
1080 switch (mEhdr->e_machine) {
1081 case EM_X86_64:
1082 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_X64;
1083 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1084 break;
1085 case EM_AARCH64:
1086 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_AARCH64;
1087 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1088 break;
1089 case EM_RISCV64:
1090 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_RISCV64;
1091 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1092 break;
1093 case EM_LOONGARCH:
1094 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_LOONGARCH64;
1095 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1096 break;
1097
1098 default:
1099 VerboseMsg ("%u unknown e_machine type. Assume X64", (UINTN)mEhdr->e_machine);
1100 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_X64;
1101 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1102 }
1103
1104 NtHdr->Pe32Plus.FileHeader.NumberOfSections = mCoffNbrSections;
1105 NtHdr->Pe32Plus.FileHeader.TimeDateStamp = (UINT32) time(NULL);
1106 mImageTimeStamp = NtHdr->Pe32Plus.FileHeader.TimeDateStamp;
1107 NtHdr->Pe32Plus.FileHeader.PointerToSymbolTable = 0;
1108 NtHdr->Pe32Plus.FileHeader.NumberOfSymbols = 0;
1109 NtHdr->Pe32Plus.FileHeader.SizeOfOptionalHeader = sizeof(NtHdr->Pe32Plus.OptionalHeader);
1110 NtHdr->Pe32Plus.FileHeader.Characteristics = EFI_IMAGE_FILE_EXECUTABLE_IMAGE
1111 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
1112 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
1113 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE;
1114
1115 NtHdr->Pe32Plus.OptionalHeader.SizeOfCode = mDataOffset - mTextOffset;
1116 NtHdr->Pe32Plus.OptionalHeader.SizeOfInitializedData = mRelocOffset - mDataOffset;
1117 NtHdr->Pe32Plus.OptionalHeader.SizeOfUninitializedData = 0;
1118 NtHdr->Pe32Plus.OptionalHeader.AddressOfEntryPoint = CoffEntry;
1119
1120 NtHdr->Pe32Plus.OptionalHeader.BaseOfCode = mTextOffset;
1121
1122 NtHdr->Pe32Plus.OptionalHeader.ImageBase = 0;
1123 NtHdr->Pe32Plus.OptionalHeader.SectionAlignment = mCoffAlignment;
1124 NtHdr->Pe32Plus.OptionalHeader.FileAlignment = mCoffAlignment;
1125 NtHdr->Pe32Plus.OptionalHeader.SizeOfImage = 0;
1126
1127 NtHdr->Pe32Plus.OptionalHeader.SizeOfHeaders = mTextOffset;
1128 NtHdr->Pe32Plus.OptionalHeader.NumberOfRvaAndSizes = EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES;
1129
1130 //
1131 // Section headers.
1132 //
1133 if ((mDataOffset - mTextOffset) > 0) {
1134 CreateSectionHeader (".text", mTextOffset, mDataOffset - mTextOffset,
1135 EFI_IMAGE_SCN_CNT_CODE
1136 | EFI_IMAGE_SCN_MEM_EXECUTE
1137 | EFI_IMAGE_SCN_MEM_READ);
1138 } else {
1139 // Don't make a section of size 0.
1140 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
1141 }
1142
1143 //
1144 // If found symbol, add edata section between data and rsrc section
1145 //
1146 if(mExportFlag) {
1147 Offset = mExportOffset;
1148 } else {
1149 Offset = mHiiRsrcOffset;
1150 }
1151
1152 if ((mHiiRsrcOffset - mDataOffset) > 0) {
1153 CreateSectionHeader (".data", mDataOffset, Offset - mDataOffset,
1154 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1155 | EFI_IMAGE_SCN_MEM_WRITE
1156 | EFI_IMAGE_SCN_MEM_READ);
1157 } else {
1158 // Don't make a section of size 0.
1159 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
1160 }
1161
1162 if(mExportFlag) {
1163 if ((mHiiRsrcOffset - mExportOffset) > 0) {
1164 CreateSectionHeader (".edata", mExportOffset, mHiiRsrcOffset - mExportOffset,
1165 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1166 | EFI_IMAGE_SCN_MEM_READ);
1167 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT].Size = mHiiRsrcOffset - mExportOffset;
1168 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress = mExportOffset;
1169
1170 } else {
1171 // Don't make a section of size 0.
1172 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
1173 }
1174 }
1175
1176 if ((mRelocOffset - mHiiRsrcOffset) > 0) {
1177 CreateSectionHeader (".rsrc", mHiiRsrcOffset, mRelocOffset - mHiiRsrcOffset,
1178 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1179 | EFI_IMAGE_SCN_MEM_READ);
1180
1181 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = mRelocOffset - mHiiRsrcOffset;
1182 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = mHiiRsrcOffset;
1183 } else {
1184 // Don't make a section of size 0.
1185 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
1186 }
1187
1188 }
1189
1190 STATIC
1191 BOOLEAN
1192 WriteSections64 (
1193 SECTION_FILTER_TYPES FilterType
1194 )
1195 {
1196 UINT32 Idx;
1197 Elf_Shdr *SecShdr;
1198 UINT32 SecOffset;
1199 BOOLEAN (*Filter)(Elf_Shdr *);
1200 Elf64_Addr GOTEntryRva;
1201
1202 //
1203 // Initialize filter pointer
1204 //
1205 switch (FilterType) {
1206 case SECTION_TEXT:
1207 Filter = IsTextShdr;
1208 break;
1209 case SECTION_HII:
1210 Filter = IsHiiRsrcShdr;
1211 break;
1212 case SECTION_DATA:
1213 Filter = IsDataShdr;
1214 break;
1215 default:
1216 return FALSE;
1217 }
1218
1219 //
1220 // First: copy sections.
1221 //
1222 for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {
1223 Elf_Shdr *Shdr = GetShdrByIndex(Idx);
1224 if ((*Filter)(Shdr)) {
1225 switch (Shdr->sh_type) {
1226 case SHT_PROGBITS:
1227 /* Copy. */
1228 if (Shdr->sh_offset + Shdr->sh_size > mFileBufferSize) {
1229 return FALSE;
1230 }
1231 memcpy(mCoffFile + mCoffSectionsOffset[Idx],
1232 (UINT8*)mEhdr + Shdr->sh_offset,
1233 (size_t) Shdr->sh_size);
1234 break;
1235
1236 case SHT_NOBITS:
1237 memset(mCoffFile + mCoffSectionsOffset[Idx], 0, (size_t) Shdr->sh_size);
1238 break;
1239
1240 default:
1241 //
1242 // Ignore for unknown section type.
1243 //
1244 VerboseMsg ("%s unknown section type %x. We ignore this unknown section type.", mInImageName, (unsigned)Shdr->sh_type);
1245 break;
1246 }
1247 }
1248 }
1249
1250 //
1251 // Second: apply relocations.
1252 //
1253 VerboseMsg ("Applying Relocations...");
1254 for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {
1255 //
1256 // Determine if this is a relocation section.
1257 //
1258 Elf_Shdr *RelShdr = GetShdrByIndex(Idx);
1259 if ((RelShdr->sh_type != SHT_REL) && (RelShdr->sh_type != SHT_RELA)) {
1260 continue;
1261 }
1262
1263 //
1264 // If this is a ET_DYN (PIE) executable, we will encounter a dynamic SHT_RELA
1265 // section that applies to the entire binary, and which will have its section
1266 // index set to #0 (which is a NULL section with the SHF_ALLOC bit cleared).
1267 //
1268 // In the absence of GOT based relocations,
1269 // this RELA section will contain redundant R_xxx_RELATIVE relocations, one
1270 // for every R_xxx_xx64 relocation appearing in the per-section RELA sections.
1271 // (i.e., .rela.text and .rela.data)
1272 //
1273 if (RelShdr->sh_info == 0) {
1274 continue;
1275 }
1276
1277 //
1278 // Relocation section found. Now extract section information that the relocations
1279 // apply to in the ELF data and the new COFF data.
1280 //
1281 SecShdr = GetShdrByIndex(RelShdr->sh_info);
1282 SecOffset = mCoffSectionsOffset[RelShdr->sh_info];
1283
1284 //
1285 // Only process relocations for the current filter type.
1286 //
1287 if (RelShdr->sh_type == SHT_RELA && (*Filter)(SecShdr)) {
1288 UINT64 RelIdx;
1289
1290 //
1291 // Determine the symbol table referenced by the relocation data.
1292 //
1293 Elf_Shdr *SymtabShdr = GetShdrByIndex(RelShdr->sh_link);
1294 UINT8 *Symtab = (UINT8*)mEhdr + SymtabShdr->sh_offset;
1295
1296 //
1297 // Process all relocation entries for this section.
1298 //
1299 for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += (UINT32) RelShdr->sh_entsize) {
1300
1301 //
1302 // Set pointer to relocation entry
1303 //
1304 Elf_Rela *Rel = (Elf_Rela *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx);
1305
1306 //
1307 // Set pointer to symbol table entry associated with the relocation entry.
1308 //
1309 Elf_Sym *Sym = (Elf_Sym *)(Symtab + ELF_R_SYM(Rel->r_info) * SymtabShdr->sh_entsize);
1310
1311 Elf_Shdr *SymShdr;
1312 UINT8 *Targ;
1313
1314 //
1315 // The _GLOBAL_OFFSET_TABLE_ symbol is not actually an absolute symbol,
1316 // but carries the SHN_ABS section index for historical reasons.
1317 // It must be accompanied by a R_*_GOT_* type relocation on a
1318 // subsequent instruction, which we handle below, specifically to avoid
1319 // the GOT indirection, and to refer to the symbol directly. This means
1320 // we can simply disregard direct references to the GOT symbol itself,
1321 // as the resulting value will never be used.
1322 //
1323 if (Sym->st_shndx == SHN_ABS) {
1324 const UINT8 *SymName = GetSymName (Sym);
1325 if (strcmp ((CHAR8 *)SymName, "_GLOBAL_OFFSET_TABLE_") == 0) {
1326 continue;
1327 }
1328 }
1329
1330 //
1331 // Check section header index found in symbol table and get the section
1332 // header location.
1333 //
1334 if (Sym->st_shndx == SHN_UNDEF
1335 || Sym->st_shndx >= mEhdr->e_shnum) {
1336 const UINT8 *SymName = GetSymName(Sym);
1337 if (SymName == NULL) {
1338 SymName = (const UINT8 *)"<unknown>";
1339 }
1340
1341 //
1342 // Skip error on EM_RISCV64 and EM_LOONGARCH because no symbol name is built
1343 // from RISC-V and LoongArch toolchain.
1344 //
1345 if ((mEhdr->e_machine != EM_RISCV64) && (mEhdr->e_machine != EM_LOONGARCH)) {
1346 Error (NULL, 0, 3000, "Invalid",
1347 "%s: Bad definition for symbol '%s'@%#llx or unsupported symbol type. "
1348 "For example, absolute and undefined symbols are not supported.",
1349 mInImageName, SymName, Sym->st_value);
1350
1351 exit(EXIT_FAILURE);
1352 }
1353 continue;
1354 }
1355 SymShdr = GetShdrByIndex(Sym->st_shndx);
1356
1357 //
1358 // Convert the relocation data to a pointer into the coff file.
1359 //
1360 // Note:
1361 // r_offset is the virtual address of the storage unit to be relocated.
1362 // sh_addr is the virtual address for the base of the section.
1363 //
1364 // r_offset in a memory address.
1365 // Convert it to a pointer in the coff file.
1366 //
1367 Targ = mCoffFile + SecOffset + (Rel->r_offset - SecShdr->sh_addr);
1368
1369 //
1370 // Determine how to handle each relocation type based on the machine type.
1371 //
1372 if (mEhdr->e_machine == EM_X86_64) {
1373 switch (ELF_R_TYPE(Rel->r_info)) {
1374 case R_X86_64_NONE:
1375 break;
1376 case R_X86_64_64:
1377 //
1378 // Absolute relocation.
1379 //
1380 VerboseMsg ("R_X86_64_64");
1381 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1382 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
1383 *(UINT64 *)Targ);
1384 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
1385 VerboseMsg ("Relocation: 0x%016LX", *(UINT64*)Targ);
1386 break;
1387 case R_X86_64_32:
1388 VerboseMsg ("R_X86_64_32");
1389 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1390 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
1391 *(UINT32 *)Targ);
1392 *(UINT32 *)Targ = (UINT32)((UINT64)(*(UINT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
1393 VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);
1394 break;
1395 case R_X86_64_32S:
1396 VerboseMsg ("R_X86_64_32S");
1397 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1398 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
1399 *(UINT32 *)Targ);
1400 *(INT32 *)Targ = (INT32)((INT64)(*(INT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
1401 VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);
1402 break;
1403
1404 case R_X86_64_PLT32:
1405 //
1406 // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is
1407 // possible since we know all code symbol references resolve to
1408 // definitions in the same module (UEFI has no shared libraries),
1409 // and so there is never a reason to jump via a PLT entry,
1410 // allowing us to resolve the reference using the symbol directly.
1411 //
1412 VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");
1413 /* fall through */
1414 case R_X86_64_PC32:
1415 //
1416 // Relative relocation: Symbol - Ip + Addend
1417 //
1418 VerboseMsg ("R_X86_64_PC32");
1419 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1420 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
1421 *(UINT32 *)Targ);
1422 *(UINT32 *)Targ = (UINT32) (*(UINT32 *)Targ
1423 + (mCoffSectionsOffset[Sym->st_shndx] - SymShdr->sh_addr)
1424 - (SecOffset - SecShdr->sh_addr));
1425 VerboseMsg ("Relocation: 0x%08X", *(UINT32 *)Targ);
1426 break;
1427 case R_X86_64_GOTPCREL:
1428 case R_X86_64_GOTPCRELX:
1429 case R_X86_64_REX_GOTPCRELX:
1430 VerboseMsg ("R_X86_64_GOTPCREL family");
1431 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1432 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
1433 *(UINT32 *)Targ);
1434 GOTEntryRva = Rel->r_offset - Rel->r_addend + *(INT32 *)Targ;
1435 FindElfGOTSectionFromGOTEntryElfRva(GOTEntryRva);
1436 *(UINT32 *)Targ = (UINT32) (*(UINT32 *)Targ
1437 + (mCoffSectionsOffset[mGOTShindex] - mGOTShdr->sh_addr)
1438 - (SecOffset - SecShdr->sh_addr));
1439 VerboseMsg ("Relocation: 0x%08X", *(UINT32 *)Targ);
1440 GOTEntryRva += (mCoffSectionsOffset[mGOTShindex] - mGOTShdr->sh_addr); // ELF Rva -> COFF Rva
1441 if (AccumulateCoffGOTEntries((UINT32)GOTEntryRva)) {
1442 //
1443 // Relocate GOT entry if it's the first time we run into it
1444 //
1445 Targ = mCoffFile + GOTEntryRva;
1446 //
1447 // Limitation: The following three statements assume memory
1448 // at *Targ is valid because the section containing the GOT
1449 // has already been copied from the ELF image to the Coff image.
1450 // This pre-condition presently holds because the GOT is placed
1451 // in section .text, and the ELF text sections are all copied
1452 // prior to reaching this point.
1453 // If the pre-condition is violated in the future, this fixup
1454 // either needs to be deferred after the GOT section is copied
1455 // to the Coff image, or the fixup should be performed on the
1456 // source Elf image instead of the destination Coff image.
1457 //
1458 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1459 (UINT32)GOTEntryRva,
1460 *(UINT64 *)Targ);
1461 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
1462 VerboseMsg ("Relocation: 0x%016LX", *(UINT64*)Targ);
1463 }
1464 break;
1465 default:
1466 Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1467 }
1468 } else if (mEhdr->e_machine == EM_AARCH64) {
1469
1470 switch (ELF_R_TYPE(Rel->r_info)) {
1471 INT64 Offset;
1472
1473 case R_AARCH64_LD64_GOTOFF_LO15:
1474 case R_AARCH64_LD64_GOTPAGE_LO15:
1475 //
1476 // Convert into an ADR instruction that refers to the symbol directly.
1477 //
1478 Offset = Sym->st_value - Rel->r_offset;
1479
1480 *(UINT32 *)Targ &= 0x1000001f;
1481 *(UINT32 *)Targ |= ((Offset & 0x1ffffc) << (5 - 2)) | ((Offset & 0x3) << 29);
1482
1483 if (Offset < -0x100000 || Offset > 0xfffff) {
1484 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s failed to relax GOT based symbol reference - image is too big (>1 MiB).",
1485 mInImageName);
1486 break;
1487 }
1488 break;
1489
1490 case R_AARCH64_LD64_GOT_LO12_NC:
1491 //
1492 // Convert into an ADD instruction - see R_AARCH64_ADR_GOT_PAGE below.
1493 //
1494 *(UINT32 *)Targ &= 0x3ff;
1495 *(UINT32 *)Targ |= 0x91000000 | ((Sym->st_value & 0xfff) << 10);
1496 break;
1497
1498 case R_AARCH64_ADR_GOT_PAGE:
1499 //
1500 // This relocation points to the GOT entry that contains the absolute
1501 // address of the symbol we are referring to. Since EDK2 only uses
1502 // fully linked binaries, we can avoid the indirection, and simply
1503 // refer to the symbol directly. This implies having to patch the
1504 // subsequent LDR instruction (covered by a R_AARCH64_LD64_GOT_LO12_NC
1505 // relocation) into an ADD instruction - this is handled above.
1506 //
1507 Offset = (Sym->st_value - (Rel->r_offset & ~0xfff)) >> 12;
1508
1509 *(UINT32 *)Targ &= 0x9000001f;
1510 *(UINT32 *)Targ |= ((Offset & 0x1ffffc) << (5 - 2)) | ((Offset & 0x3) << 29);
1511
1512 /* fall through */
1513
1514 case R_AARCH64_ADR_PREL_PG_HI21:
1515 //
1516 // In order to handle Cortex-A53 erratum #843419, the LD linker may
1517 // convert ADRP instructions into ADR instructions, but without
1518 // updating the static relocation type, and so we may end up here
1519 // while the instruction in question is actually ADR. So let's
1520 // just disregard it: the section offset check we apply below to
1521 // ADR instructions will trigger for its R_AARCH64_xxx_ABS_LO12_NC
1522 // companion instruction as well, so it is safe to omit it here.
1523 //
1524 if ((*(UINT32 *)Targ & BIT31) == 0) {
1525 break;
1526 }
1527
1528 //
1529 // AArch64 PG_H21 relocations are typically paired with ABS_LO12
1530 // relocations, where a PC-relative reference with +/- 4 GB range is
1531 // split into a relative high part and an absolute low part. Since
1532 // the absolute low part represents the offset into a 4 KB page, we
1533 // either have to convert the ADRP into an ADR instruction, or we
1534 // need to use a section alignment of at least 4 KB, so that the
1535 // binary appears at a correct offset at runtime. In any case, we
1536 // have to make sure that the 4 KB relative offsets of both the
1537 // section containing the reference as well as the section to which
1538 // it refers have not been changed during PE/COFF conversion (i.e.,
1539 // in ScanSections64() above).
1540 //
1541 if (mCoffAlignment < 0x1000) {
1542 //
1543 // Attempt to convert the ADRP into an ADR instruction.
1544 // This is only possible if the symbol is within +/- 1 MB.
1545 //
1546
1547 // Decode the ADRP instruction
1548 Offset = (INT32)((*(UINT32 *)Targ & 0xffffe0) << 8);
1549 Offset = (Offset << (6 - 5)) | ((*(UINT32 *)Targ & 0x60000000) >> (29 - 12));
1550
1551 //
1552 // ADRP offset is relative to the previous page boundary,
1553 // whereas ADR offset is relative to the instruction itself.
1554 // So fix up the offset so it points to the page containing
1555 // the symbol.
1556 //
1557 Offset -= (UINTN)(Targ - mCoffFile) & 0xfff;
1558
1559 if (Offset < -0x100000 || Offset > 0xfffff) {
1560 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s due to its size (> 1 MB), this module requires 4 KB section alignment.",
1561 mInImageName);
1562 break;
1563 }
1564
1565 // Re-encode the offset as an ADR instruction
1566 *(UINT32 *)Targ &= 0x1000001f;
1567 *(UINT32 *)Targ |= ((Offset & 0x1ffffc) << (5 - 2)) | ((Offset & 0x3) << 29);
1568 }
1569 /* fall through */
1570
1571 case R_AARCH64_ADD_ABS_LO12_NC:
1572 case R_AARCH64_LDST8_ABS_LO12_NC:
1573 case R_AARCH64_LDST16_ABS_LO12_NC:
1574 case R_AARCH64_LDST32_ABS_LO12_NC:
1575 case R_AARCH64_LDST64_ABS_LO12_NC:
1576 case R_AARCH64_LDST128_ABS_LO12_NC:
1577 if (((SecShdr->sh_addr ^ SecOffset) & 0xfff) != 0 ||
1578 ((SymShdr->sh_addr ^ mCoffSectionsOffset[Sym->st_shndx]) & 0xfff) != 0) {
1579 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 small code model requires identical ELF and PE/COFF section offsets modulo 4 KB.",
1580 mInImageName);
1581 break;
1582 }
1583 /* fall through */
1584
1585 case R_AARCH64_ADR_PREL_LO21:
1586 case R_AARCH64_CONDBR19:
1587 case R_AARCH64_LD_PREL_LO19:
1588 case R_AARCH64_CALL26:
1589 case R_AARCH64_JUMP26:
1590 case R_AARCH64_PREL64:
1591 case R_AARCH64_PREL32:
1592 case R_AARCH64_PREL16:
1593 //
1594 // The GCC toolchains (i.e., binutils) may corrupt section relative
1595 // relocations when emitting relocation sections into fully linked
1596 // binaries. More specifically, they tend to fail to take into
1597 // account the fact that a '.rodata + XXX' relocation needs to have
1598 // its addend recalculated once .rodata is merged into the .text
1599 // section, and the relocation emitted into the .rela.text section.
1600 //
1601 // We cannot really recover from this loss of information, so the
1602 // only workaround is to prevent having to recalculate any relative
1603 // relocations at all, by using a linker script that ensures that
1604 // the offset between the Place and the Symbol is the same in both
1605 // the ELF and the PE/COFF versions of the binary.
1606 //
1607 if ((SymShdr->sh_addr - SecShdr->sh_addr) !=
1608 (mCoffSectionsOffset[Sym->st_shndx] - SecOffset)) {
1609 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 relative relocations require identical ELF and PE/COFF section offsets",
1610 mInImageName);
1611 }
1612 break;
1613
1614 // Absolute relocations.
1615 case R_AARCH64_ABS64:
1616 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
1617 break;
1618
1619 default:
1620 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1621 }
1622 } else if (mEhdr->e_machine == EM_RISCV64) {
1623 //
1624 // Write section for RISC-V 64 architecture.
1625 //
1626 WriteSectionRiscV64 (Rel, Targ, SymShdr, Sym);
1627 } else if (mEhdr->e_machine == EM_LOONGARCH) {
1628 switch (ELF_R_TYPE(Rel->r_info)) {
1629 INT64 Offset;
1630 INT32 Lo, Hi;
1631
1632 case R_LARCH_SOP_PUSH_ABSOLUTE:
1633 //
1634 // Absolute relocation.
1635 //
1636 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
1637 break;
1638
1639 case R_LARCH_MARK_LA:
1640 case R_LARCH_64:
1641 case R_LARCH_NONE:
1642 case R_LARCH_32:
1643 case R_LARCH_RELATIVE:
1644 case R_LARCH_COPY:
1645 case R_LARCH_JUMP_SLOT:
1646 case R_LARCH_TLS_DTPMOD32:
1647 case R_LARCH_TLS_DTPMOD64:
1648 case R_LARCH_TLS_DTPREL32:
1649 case R_LARCH_TLS_DTPREL64:
1650 case R_LARCH_TLS_TPREL32:
1651 case R_LARCH_TLS_TPREL64:
1652 case R_LARCH_IRELATIVE:
1653 case R_LARCH_MARK_PCREL:
1654 case R_LARCH_SOP_PUSH_PCREL:
1655 case R_LARCH_SOP_PUSH_DUP:
1656 case R_LARCH_SOP_PUSH_GPREL:
1657 case R_LARCH_SOP_PUSH_TLS_TPREL:
1658 case R_LARCH_SOP_PUSH_TLS_GOT:
1659 case R_LARCH_SOP_PUSH_TLS_GD:
1660 case R_LARCH_SOP_PUSH_PLT_PCREL:
1661 case R_LARCH_SOP_ASSERT:
1662 case R_LARCH_SOP_NOT:
1663 case R_LARCH_SOP_SUB:
1664 case R_LARCH_SOP_SL:
1665 case R_LARCH_SOP_SR:
1666 case R_LARCH_SOP_ADD:
1667 case R_LARCH_SOP_AND:
1668 case R_LARCH_SOP_IF_ELSE:
1669 case R_LARCH_SOP_POP_32_S_10_5:
1670 case R_LARCH_SOP_POP_32_U_10_12:
1671 case R_LARCH_SOP_POP_32_S_10_12:
1672 case R_LARCH_SOP_POP_32_S_10_16:
1673 case R_LARCH_SOP_POP_32_S_10_16_S2:
1674 case R_LARCH_SOP_POP_32_S_5_20:
1675 case R_LARCH_SOP_POP_32_S_0_5_10_16_S2:
1676 case R_LARCH_SOP_POP_32_S_0_10_10_16_S2:
1677 case R_LARCH_SOP_POP_32_U:
1678 case R_LARCH_ADD8:
1679 case R_LARCH_ADD16:
1680 case R_LARCH_ADD24:
1681 case R_LARCH_ADD32:
1682 case R_LARCH_ADD64:
1683 case R_LARCH_SUB8:
1684 case R_LARCH_SUB16:
1685 case R_LARCH_SUB24:
1686 case R_LARCH_SUB32:
1687 case R_LARCH_SUB64:
1688 case R_LARCH_GNU_VTINHERIT:
1689 case R_LARCH_GNU_VTENTRY:
1690 case R_LARCH_B16:
1691 case R_LARCH_B21:
1692 case R_LARCH_B26:
1693 case R_LARCH_ABS_HI20:
1694 case R_LARCH_ABS_LO12:
1695 case R_LARCH_ABS64_LO20:
1696 case R_LARCH_ABS64_HI12:
1697 case R_LARCH_PCALA_LO12:
1698 case R_LARCH_PCALA64_LO20:
1699 case R_LARCH_PCALA64_HI12:
1700 case R_LARCH_GOT_PC_LO12:
1701 case R_LARCH_GOT64_PC_LO20:
1702 case R_LARCH_GOT64_PC_HI12:
1703 case R_LARCH_GOT64_HI20:
1704 case R_LARCH_GOT64_LO12:
1705 case R_LARCH_GOT64_LO20:
1706 case R_LARCH_GOT64_HI12:
1707 case R_LARCH_TLS_LE_HI20:
1708 case R_LARCH_TLS_LE_LO12:
1709 case R_LARCH_TLS_LE64_LO20:
1710 case R_LARCH_TLS_LE64_HI12:
1711 case R_LARCH_TLS_IE_PC_HI20:
1712 case R_LARCH_TLS_IE_PC_LO12:
1713 case R_LARCH_TLS_IE64_PC_LO20:
1714 case R_LARCH_TLS_IE64_PC_HI12:
1715 case R_LARCH_TLS_IE64_HI20:
1716 case R_LARCH_TLS_IE64_LO12:
1717 case R_LARCH_TLS_IE64_LO20:
1718 case R_LARCH_TLS_IE64_HI12:
1719 case R_LARCH_TLS_LD_PC_HI20:
1720 case R_LARCH_TLS_LD64_HI20:
1721 case R_LARCH_TLS_GD_PC_HI20:
1722 case R_LARCH_TLS_GD64_HI20:
1723 case R_LARCH_RELAX:
1724 //
1725 // These types are not used or do not require fixup.
1726 //
1727 break;
1728
1729 case R_LARCH_GOT_PC_HI20:
1730 Offset = Sym->st_value - (UINTN)(Targ - mCoffFile);
1731 if (Offset < 0) {
1732 Offset = (UINTN)(Targ - mCoffFile) - Sym->st_value;
1733 Hi = Offset & ~0xfff;
1734 Lo = (INT32)((Offset & 0xfff) << 20) >> 20;
1735 if ((Lo < 0) && (Lo > -2048)) {
1736 Hi += 0x1000;
1737 Lo = ~(0x1000 - Lo) + 1;
1738 }
1739 Hi = ~Hi + 1;
1740 Lo = ~Lo + 1;
1741 } else {
1742 Hi = Offset & ~0xfff;
1743 Lo = (INT32)((Offset & 0xfff) << 20) >> 20;
1744 if (Lo < 0) {
1745 Hi += 0x1000;
1746 Lo = ~(0x1000 - Lo) + 1;
1747 }
1748 }
1749 // Re-encode the offset as PCADDU12I + ADDI.D(Convert LD.D) instruction
1750 *(UINT32 *)Targ &= 0x1f;
1751 *(UINT32 *)Targ |= 0x1c000000;
1752 *(UINT32 *)Targ |= (((Hi >> 12) & 0xfffff) << 5);
1753 *(UINT32 *)(Targ + 4) &= 0x3ff;
1754 *(UINT32 *)(Targ + 4) |= 0x2c00000 | ((Lo & 0xfff) << 10);
1755 break;
1756
1757 //
1758 // Attempt to convert instruction.
1759 //
1760 case R_LARCH_PCALA_HI20:
1761 // Decode the PCALAU12I instruction and the instruction that following it.
1762 Offset = ((INT32)((*(UINT32 *)Targ & 0x1ffffe0) << 7));
1763 Offset += ((INT32)((*(UINT32 *)(Targ + 4) & 0x3ffc00) << 10) >> 20);
1764 //
1765 // PCALA offset is relative to the previous page boundary,
1766 // whereas PCADD offset is relative to the instruction itself.
1767 // So fix up the offset so it points to the page containing
1768 // the symbol.
1769 //
1770 Offset -= (UINTN)(Targ - mCoffFile) & 0xfff;
1771 if (Offset < 0) {
1772 Offset = -Offset;
1773 Hi = Offset & ~0xfff;
1774 Lo = (INT32)((Offset & 0xfff) << 20) >> 20;
1775 if ((Lo < 0) && (Lo > -2048)) {
1776 Hi += 0x1000;
1777 Lo = ~(0x1000 - Lo) + 1;
1778 }
1779 Hi = ~Hi + 1;
1780 Lo = ~Lo + 1;
1781 } else {
1782 Hi = Offset & ~0xfff;
1783 Lo = (INT32)((Offset & 0xfff) << 20) >> 20;
1784 if (Lo < 0) {
1785 Hi += 0x1000;
1786 Lo = ~(0x1000 - Lo) + 1;
1787 }
1788 }
1789 // Convert the first instruction from PCALAU12I to PCADDU12I and re-encode the offset into them.
1790 *(UINT32 *)Targ &= 0x1f;
1791 *(UINT32 *)Targ |= 0x1c000000;
1792 *(UINT32 *)Targ |= (((Hi >> 12) & 0xfffff) << 5);
1793 *(UINT32 *)(Targ + 4) &= 0xffc003ff;
1794 *(UINT32 *)(Targ + 4) |= (Lo & 0xfff) << 10;
1795 break;
1796 default:
1797 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_LOONGARCH relocation 0x%x.", mInImageName, (unsigned) ELF64_R_TYPE(Rel->r_info));
1798 }
1799 } else {
1800 Error (NULL, 0, 3000, "Invalid", "Not a supported machine type");
1801 }
1802 }
1803 }
1804 }
1805
1806 return TRUE;
1807 }
1808
1809 STATIC
1810 VOID
1811 WriteRelocations64 (
1812 VOID
1813 )
1814 {
1815 UINT32 Index;
1816 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
1817 EFI_IMAGE_DATA_DIRECTORY *Dir;
1818 UINT32 RiscVRelType;
1819
1820 for (Index = 0; Index < mEhdr->e_shnum; Index++) {
1821 Elf_Shdr *RelShdr = GetShdrByIndex(Index);
1822 if ((RelShdr->sh_type == SHT_REL) || (RelShdr->sh_type == SHT_RELA)) {
1823 Elf_Shdr *SecShdr = GetShdrByIndex (RelShdr->sh_info);
1824 if (IsTextShdr(SecShdr) || IsDataShdr(SecShdr)) {
1825 UINT64 RelIdx;
1826
1827 for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += RelShdr->sh_entsize) {
1828 Elf_Rela *Rel = (Elf_Rela *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx);
1829
1830 if (mEhdr->e_machine == EM_X86_64) {
1831 switch (ELF_R_TYPE(Rel->r_info)) {
1832 case R_X86_64_NONE:
1833 case R_X86_64_PC32:
1834 case R_X86_64_PLT32:
1835 case R_X86_64_GOTPCREL:
1836 case R_X86_64_GOTPCRELX:
1837 case R_X86_64_REX_GOTPCRELX:
1838 break;
1839 case R_X86_64_64:
1840 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08llX",
1841 mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr));
1842 CoffAddFixup(
1843 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1844 + (Rel->r_offset - SecShdr->sh_addr)),
1845 EFI_IMAGE_REL_BASED_DIR64);
1846 break;
1847 //
1848 // R_X86_64_32 and R_X86_64_32S are ELF64 relocations emitted when using
1849 // the SYSV X64 ABI small non-position-independent code model.
1850 // R_X86_64_32 is used for unsigned 32-bit immediates with a 32-bit operand
1851 // size. The value is either not extended, or zero-extended to 64 bits.
1852 // R_X86_64_32S is used for either signed 32-bit non-rip-relative displacements
1853 // or signed 32-bit immediates with a 64-bit operand size. The value is
1854 // sign-extended to 64 bits.
1855 // EFI_IMAGE_REL_BASED_HIGHLOW is a PE relocation that uses 32-bit arithmetic
1856 // for rebasing an image.
1857 // EFI PE binaries declare themselves EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE and
1858 // may load above 2GB. If an EFI PE binary with a converted R_X86_64_32S
1859 // relocation is loaded above 2GB, the value will get sign-extended to the
1860 // negative part of the 64-bit address space. The negative part of the 64-bit
1861 // address space is unmapped, so accessing such an address page-faults.
1862 // In order to support R_X86_64_32S, it is necessary to unset
1863 // EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE, and the EFI PE loader must implement
1864 // this flag and abstain from loading such a PE binary above 2GB.
1865 // Since this feature is not supported, support for R_X86_64_32S (and hence
1866 // the small non-position-independent code model) is disabled.
1867 //
1868 // case R_X86_64_32S:
1869 case R_X86_64_32:
1870 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08llX",
1871 mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr));
1872 CoffAddFixup(
1873 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1874 + (Rel->r_offset - SecShdr->sh_addr)),
1875 EFI_IMAGE_REL_BASED_HIGHLOW);
1876 break;
1877 default:
1878 Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1879 }
1880 } else if (mEhdr->e_machine == EM_AARCH64) {
1881
1882 switch (ELF_R_TYPE(Rel->r_info)) {
1883 case R_AARCH64_ADR_PREL_LO21:
1884 case R_AARCH64_CONDBR19:
1885 case R_AARCH64_LD_PREL_LO19:
1886 case R_AARCH64_CALL26:
1887 case R_AARCH64_JUMP26:
1888 case R_AARCH64_PREL64:
1889 case R_AARCH64_PREL32:
1890 case R_AARCH64_PREL16:
1891 case R_AARCH64_ADR_PREL_PG_HI21:
1892 case R_AARCH64_ADD_ABS_LO12_NC:
1893 case R_AARCH64_LDST8_ABS_LO12_NC:
1894 case R_AARCH64_LDST16_ABS_LO12_NC:
1895 case R_AARCH64_LDST32_ABS_LO12_NC:
1896 case R_AARCH64_LDST64_ABS_LO12_NC:
1897 case R_AARCH64_LDST128_ABS_LO12_NC:
1898 case R_AARCH64_ADR_GOT_PAGE:
1899 case R_AARCH64_LD64_GOT_LO12_NC:
1900 case R_AARCH64_LD64_GOTOFF_LO15:
1901 case R_AARCH64_LD64_GOTPAGE_LO15:
1902 //
1903 // No fixups are required for relative relocations, provided that
1904 // the relative offsets between sections have been preserved in
1905 // the ELF to PE/COFF conversion. We have already asserted that
1906 // this is the case in WriteSections64 ().
1907 //
1908 break;
1909
1910 case R_AARCH64_ABS64:
1911 CoffAddFixup(
1912 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1913 + (Rel->r_offset - SecShdr->sh_addr)),
1914 EFI_IMAGE_REL_BASED_DIR64);
1915 break;
1916
1917 case R_AARCH64_ABS32:
1918 CoffAddFixup(
1919 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1920 + (Rel->r_offset - SecShdr->sh_addr)),
1921 EFI_IMAGE_REL_BASED_HIGHLOW);
1922 break;
1923
1924 default:
1925 Error (NULL, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1926 }
1927 } else if (mEhdr->e_machine == EM_RISCV64) {
1928 RiscVRelType = ELF_R_TYPE(Rel->r_info);
1929 switch (RiscVRelType) {
1930 case R_RISCV_NONE:
1931 break;
1932
1933 case R_RISCV_32:
1934 CoffAddFixup(
1935 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1936 + (Rel->r_offset - SecShdr->sh_addr)),
1937 EFI_IMAGE_REL_BASED_HIGHLOW);
1938 break;
1939
1940 case R_RISCV_64:
1941 CoffAddFixup(
1942 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1943 + (Rel->r_offset - SecShdr->sh_addr)),
1944 EFI_IMAGE_REL_BASED_DIR64);
1945 break;
1946
1947 case R_RISCV_HI20:
1948 CoffAddFixup(
1949 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1950 + (Rel->r_offset - SecShdr->sh_addr)),
1951 EFI_IMAGE_REL_BASED_RISCV_HI20);
1952 break;
1953
1954 case R_RISCV_LO12_I:
1955 CoffAddFixup(
1956 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1957 + (Rel->r_offset - SecShdr->sh_addr)),
1958 EFI_IMAGE_REL_BASED_RISCV_LOW12I);
1959 break;
1960
1961 case R_RISCV_LO12_S:
1962 CoffAddFixup(
1963 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1964 + (Rel->r_offset - SecShdr->sh_addr)),
1965 EFI_IMAGE_REL_BASED_RISCV_LOW12S);
1966 break;
1967
1968 case R_RISCV_ADD64:
1969 CoffAddFixup(
1970 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1971 + (Rel->r_offset - SecShdr->sh_addr)),
1972 EFI_IMAGE_REL_BASED_ABSOLUTE);
1973 break;
1974
1975 case R_RISCV_SUB64:
1976 CoffAddFixup(
1977 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1978 + (Rel->r_offset - SecShdr->sh_addr)),
1979 EFI_IMAGE_REL_BASED_ABSOLUTE);
1980 break;
1981
1982 case R_RISCV_ADD32:
1983 CoffAddFixup(
1984 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1985 + (Rel->r_offset - SecShdr->sh_addr)),
1986 EFI_IMAGE_REL_BASED_ABSOLUTE);
1987 break;
1988
1989 case R_RISCV_SUB32:
1990 CoffAddFixup(
1991 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1992 + (Rel->r_offset - SecShdr->sh_addr)),
1993 EFI_IMAGE_REL_BASED_ABSOLUTE);
1994 break;
1995
1996 case R_RISCV_BRANCH:
1997 CoffAddFixup(
1998 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1999 + (Rel->r_offset - SecShdr->sh_addr)),
2000 EFI_IMAGE_REL_BASED_ABSOLUTE);
2001 break;
2002
2003 case R_RISCV_JAL:
2004 CoffAddFixup(
2005 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2006 + (Rel->r_offset - SecShdr->sh_addr)),
2007 EFI_IMAGE_REL_BASED_ABSOLUTE);
2008 break;
2009
2010 case R_RISCV_GPREL_I:
2011 case R_RISCV_GPREL_S:
2012 case R_RISCV_CALL:
2013 case R_RISCV_CALL_PLT:
2014 case R_RISCV_RVC_BRANCH:
2015 case R_RISCV_RVC_JUMP:
2016 case R_RISCV_RELAX:
2017 case R_RISCV_SUB6:
2018 case R_RISCV_SET6:
2019 case R_RISCV_SET8:
2020 case R_RISCV_SET16:
2021 case R_RISCV_SET32:
2022 case R_RISCV_PCREL_HI20:
2023 case R_RISCV_GOT_HI20:
2024 case R_RISCV_PCREL_LO12_I:
2025 case R_RISCV_PCREL_LO12_S:
2026 break;
2027
2028 default:
2029 Error (NULL, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_RISCV64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
2030 }
2031 } else if (mEhdr->e_machine == EM_LOONGARCH) {
2032 switch (ELF_R_TYPE(Rel->r_info)) {
2033 case R_LARCH_MARK_LA:
2034 CoffAddFixup(
2035 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2036 + (Rel->r_offset - SecShdr->sh_addr)),
2037 EFI_IMAGE_REL_BASED_LOONGARCH64_MARK_LA);
2038 break;
2039 case R_LARCH_64:
2040 CoffAddFixup(
2041 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2042 + (Rel->r_offset - SecShdr->sh_addr)),
2043 EFI_IMAGE_REL_BASED_DIR64);
2044 break;
2045 case R_LARCH_NONE:
2046 case R_LARCH_32:
2047 case R_LARCH_RELATIVE:
2048 case R_LARCH_COPY:
2049 case R_LARCH_JUMP_SLOT:
2050 case R_LARCH_TLS_DTPMOD32:
2051 case R_LARCH_TLS_DTPMOD64:
2052 case R_LARCH_TLS_DTPREL32:
2053 case R_LARCH_TLS_DTPREL64:
2054 case R_LARCH_TLS_TPREL32:
2055 case R_LARCH_TLS_TPREL64:
2056 case R_LARCH_IRELATIVE:
2057 case R_LARCH_MARK_PCREL:
2058 case R_LARCH_SOP_PUSH_PCREL:
2059 case R_LARCH_SOP_PUSH_ABSOLUTE:
2060 case R_LARCH_SOP_PUSH_DUP:
2061 case R_LARCH_SOP_PUSH_GPREL:
2062 case R_LARCH_SOP_PUSH_TLS_TPREL:
2063 case R_LARCH_SOP_PUSH_TLS_GOT:
2064 case R_LARCH_SOP_PUSH_TLS_GD:
2065 case R_LARCH_SOP_PUSH_PLT_PCREL:
2066 case R_LARCH_SOP_ASSERT:
2067 case R_LARCH_SOP_NOT:
2068 case R_LARCH_SOP_SUB:
2069 case R_LARCH_SOP_SL:
2070 case R_LARCH_SOP_SR:
2071 case R_LARCH_SOP_ADD:
2072 case R_LARCH_SOP_AND:
2073 case R_LARCH_SOP_IF_ELSE:
2074 case R_LARCH_SOP_POP_32_S_10_5:
2075 case R_LARCH_SOP_POP_32_U_10_12:
2076 case R_LARCH_SOP_POP_32_S_10_12:
2077 case R_LARCH_SOP_POP_32_S_10_16:
2078 case R_LARCH_SOP_POP_32_S_10_16_S2:
2079 case R_LARCH_SOP_POP_32_S_5_20:
2080 case R_LARCH_SOP_POP_32_S_0_5_10_16_S2:
2081 case R_LARCH_SOP_POP_32_S_0_10_10_16_S2:
2082 case R_LARCH_SOP_POP_32_U:
2083 case R_LARCH_ADD8:
2084 case R_LARCH_ADD16:
2085 case R_LARCH_ADD24:
2086 case R_LARCH_ADD32:
2087 case R_LARCH_ADD64:
2088 case R_LARCH_SUB8:
2089 case R_LARCH_SUB16:
2090 case R_LARCH_SUB24:
2091 case R_LARCH_SUB32:
2092 case R_LARCH_SUB64:
2093 case R_LARCH_GNU_VTINHERIT:
2094 case R_LARCH_GNU_VTENTRY:
2095 case R_LARCH_B16:
2096 case R_LARCH_B21:
2097 case R_LARCH_B26:
2098 case R_LARCH_ABS_HI20:
2099 case R_LARCH_ABS_LO12:
2100 case R_LARCH_ABS64_LO20:
2101 case R_LARCH_ABS64_HI12:
2102 case R_LARCH_PCALA_HI20:
2103 case R_LARCH_PCALA_LO12:
2104 case R_LARCH_PCALA64_LO20:
2105 case R_LARCH_PCALA64_HI12:
2106 case R_LARCH_GOT_PC_HI20:
2107 case R_LARCH_GOT_PC_LO12:
2108 case R_LARCH_GOT64_PC_LO20:
2109 case R_LARCH_GOT64_PC_HI12:
2110 case R_LARCH_GOT64_HI20:
2111 case R_LARCH_GOT64_LO12:
2112 case R_LARCH_GOT64_LO20:
2113 case R_LARCH_GOT64_HI12:
2114 case R_LARCH_TLS_LE_HI20:
2115 case R_LARCH_TLS_LE_LO12:
2116 case R_LARCH_TLS_LE64_LO20:
2117 case R_LARCH_TLS_LE64_HI12:
2118 case R_LARCH_TLS_IE_PC_HI20:
2119 case R_LARCH_TLS_IE_PC_LO12:
2120 case R_LARCH_TLS_IE64_PC_LO20:
2121 case R_LARCH_TLS_IE64_PC_HI12:
2122 case R_LARCH_TLS_IE64_HI20:
2123 case R_LARCH_TLS_IE64_LO12:
2124 case R_LARCH_TLS_IE64_LO20:
2125 case R_LARCH_TLS_IE64_HI12:
2126 case R_LARCH_TLS_LD_PC_HI20:
2127 case R_LARCH_TLS_LD64_HI20:
2128 case R_LARCH_TLS_GD_PC_HI20:
2129 case R_LARCH_TLS_GD64_HI20:
2130 case R_LARCH_RELAX:
2131 //
2132 // These types are not used or do not require fixup in PE format files.
2133 //
2134 break;
2135 default:
2136 Error (NULL, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_LOONGARCH relocation 0x%x.", mInImageName, (unsigned) ELF64_R_TYPE(Rel->r_info));
2137 }
2138 } else {
2139 Error (NULL, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr->e_machine);
2140 }
2141 }
2142 if (mEhdr->e_machine == EM_X86_64 && RelShdr->sh_info == mGOTShindex) {
2143 //
2144 // Tack relocations for GOT entries after other relocations for
2145 // the section the GOT is in, as it's usually found at the end
2146 // of the section. This is done in order to maintain Rva order
2147 // of Coff relocations.
2148 //
2149 EmitGOTRelocations();
2150 }
2151 }
2152 }
2153 }
2154
2155 if (mEhdr->e_machine == EM_X86_64) {
2156 //
2157 // This is a safety net just in case the GOT is in a section
2158 // with no other relocations and the first invocation of
2159 // EmitGOTRelocations() above was skipped. This invocation
2160 // does not maintain Rva order of Coff relocations.
2161 // At present, with a single text section, all references to
2162 // the GOT and the GOT itself reside in section .text, so
2163 // if there's a GOT at all, the first invocation above
2164 // is executed.
2165 //
2166 EmitGOTRelocations();
2167 }
2168 //
2169 // Pad by adding empty entries.
2170 //
2171 while (mCoffOffset & (mCoffAlignment - 1)) {
2172 CoffAddFixupEntry(0);
2173 }
2174
2175 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
2176 Dir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
2177 Dir->Size = mCoffOffset - mRelocOffset;
2178 if (Dir->Size == 0) {
2179 // If no relocations, null out the directory entry and don't add the .reloc section
2180 Dir->VirtualAddress = 0;
2181 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
2182 } else {
2183 Dir->VirtualAddress = mRelocOffset;
2184 CreateSectionHeader (".reloc", mRelocOffset, mCoffOffset - mRelocOffset,
2185 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
2186 | EFI_IMAGE_SCN_MEM_DISCARDABLE
2187 | EFI_IMAGE_SCN_MEM_READ);
2188 }
2189 }
2190
2191 STATIC
2192 VOID
2193 WriteDebug64 (
2194 VOID
2195 )
2196 {
2197 UINT32 Len;
2198 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
2199 EFI_IMAGE_DATA_DIRECTORY *DataDir;
2200 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *Dir;
2201 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *Nb10;
2202
2203 Len = strlen(mInImageName) + 1;
2204
2205 Dir = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(mCoffFile + mDebugOffset);
2206 Dir->Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW;
2207 Dir->SizeOfData = sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + Len;
2208 Dir->RVA = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
2209 Dir->FileOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
2210
2211 Nb10 = (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY*)(Dir + 1);
2212 Nb10->Signature = CODEVIEW_SIGNATURE_NB10;
2213 strcpy ((char *)(Nb10 + 1), mInImageName);
2214
2215
2216 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
2217 DataDir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG];
2218 DataDir->VirtualAddress = mDebugOffset;
2219 DataDir->Size = sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
2220 }
2221
2222 STATIC
2223 VOID
2224 SetImageSize64 (
2225 VOID
2226 )
2227 {
2228 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
2229
2230 //
2231 // Set image size
2232 //
2233 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
2234 NtHdr->Pe32Plus.OptionalHeader.SizeOfImage = mCoffOffset;
2235 }
2236
2237 STATIC
2238 VOID
2239 CleanUp64 (
2240 VOID
2241 )
2242 {
2243 if (mCoffSectionsOffset != NULL) {
2244 free (mCoffSectionsOffset);
2245 }
2246 }
2247
2248 STATIC
2249 VOID
2250 WriteExport64 (
2251 VOID
2252 )
2253 {
2254 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
2255 EFI_IMAGE_EXPORT_DIRECTORY *ExportDir;
2256 EFI_IMAGE_DATA_DIRECTORY *DataDir;
2257 UINT32 FileNameOffset;
2258 UINT32 NameOffset;
2259 UINT16 Index;
2260 UINT8 *Tdata = NULL;
2261
2262 ExportDir = (EFI_IMAGE_EXPORT_DIRECTORY*)(mCoffFile + mExportOffset);
2263 ExportDir->Characteristics = 0;
2264 ExportDir->TimeDateStamp = 0;
2265 ExportDir->MajorVersion = 0;
2266 ExportDir->MinorVersion =0;
2267 ExportDir->Name = 0;
2268 ExportDir->NumberOfFunctions = mExportSymNum;
2269 ExportDir->NumberOfNames = mExportSymNum;
2270 ExportDir->Base = EFI_IMAGE_EXPORT_ORDINAL_BASE;
2271 ExportDir->AddressOfFunctions = mExportOffset + sizeof(EFI_IMAGE_EXPORT_DIRECTORY);
2272 ExportDir->AddressOfNames = ExportDir->AddressOfFunctions + EFI_IMAGE_EXPORT_ADDR_SIZE * mExportSymNum;
2273 ExportDir->AddressOfNameOrdinals = ExportDir->AddressOfNames + EFI_IMAGE_EXPORT_ADDR_SIZE * mExportSymNum;
2274
2275 FileNameOffset = ExportDir->AddressOfNameOrdinals + EFI_IMAGE_EXPORT_ORDINAL_SIZE * mExportSymNum;
2276 NameOffset = FileNameOffset + strlen(mInImageName) + 1;
2277
2278 // Write Input image Name RVA
2279 ExportDir->Name = FileNameOffset;
2280
2281 // Write Input image Name
2282 strcpy((char *)(mCoffFile + FileNameOffset), mInImageName);
2283
2284 for (Index = 0; Index < mExportSymNum; Index++) {
2285 //
2286 // Write Export Address Table
2287 //
2288 Tdata = mCoffFile + ExportDir->AddressOfFunctions + Index * EFI_IMAGE_EXPORT_ADDR_SIZE;
2289 *(UINT32 *)Tdata = mExportRVA[Index];
2290
2291 //
2292 // Write Export Name Pointer Table
2293 //
2294 Tdata = mCoffFile + ExportDir->AddressOfNames + Index * EFI_IMAGE_EXPORT_ADDR_SIZE;
2295 *(UINT32 *)Tdata = NameOffset;
2296
2297 //
2298 // Write Export Ordinal table
2299 //
2300 Tdata = mCoffFile + ExportDir->AddressOfNameOrdinals + Index * EFI_IMAGE_EXPORT_ORDINAL_SIZE;
2301 *(UINT16 *)Tdata = Index;
2302
2303 //
2304 // Write Export Name Table
2305 //
2306 strcpy((char *)(mCoffFile + NameOffset), mExportSymName[Index]);
2307 NameOffset += strlen(mExportSymName[Index]) + 1;
2308 }
2309
2310 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
2311 DataDir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT];
2312 DataDir->VirtualAddress = mExportOffset;
2313 DataDir->Size = mExportSize;
2314
2315 }
2316