VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/BaseTools/Source/C/GenFw/Elf64Convert.c

Last change on this file was 108794, checked in by vboxsync, 8 weeks ago

Devices/EFI/FirmwareNew: Merge edk2-stable202502 from the vendor branch and make it build for the important platforms, bugref:4643

  • Property svn:eol-style set to native
File size: 80.5 KB
Line 
1/** @file
2Elf64 convert solution
3
4Copyright (c) 2010 - 2021, Intel Corporation. All rights reserved.<BR>
5Portions copyright (c) 2013-2022, ARM Ltd. All rights reserved.<BR>
6Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
7Portions Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
8
9SPDX-License-Identifier: BSD-2-Clause-Patent
10
11**/
12
13#ifndef __GNUC__
14#define RUNTIME_FUNCTION _WINNT_DUP_RUNTIME_FUNCTION
15#include <windows.h>
16#undef RUNTIME_FUNCTION
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
36STATIC
37VOID
38ScanSections64 (
39 VOID
40 );
41
42STATIC
43BOOLEAN
44WriteSections64 (
45 SECTION_FILTER_TYPES FilterType
46 );
47
48STATIC
49VOID
50WriteRelocations64 (
51 VOID
52 );
53
54STATIC
55VOID
56WriteDebug64 (
57 VOID
58 );
59
60STATIC
61VOID
62WriteExport64 (
63 VOID
64 );
65
66STATIC
67VOID
68SetImageSize64 (
69 VOID
70 );
71
72STATIC
73VOID
74CleanUp64 (
75 VOID
76 );
77
78//
79// Rename ELF32 structures to common names to help when porting to ELF64.
80//
81typedef Elf64_Shdr Elf_Shdr;
82typedef Elf64_Ehdr Elf_Ehdr;
83typedef Elf64_Rel Elf_Rel;
84typedef Elf64_Rela Elf_Rela;
85typedef Elf64_Sym Elf_Sym;
86typedef Elf64_Phdr Elf_Phdr;
87typedef 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//
95STATIC Elf_Ehdr *mEhdr;
96STATIC Elf_Shdr *mShdrBase;
97STATIC Elf_Phdr *mPhdrBase;
98
99//
100// GOT information
101//
102STATIC Elf_Shdr *mGOTShdr = NULL;
103STATIC UINT32 mGOTShindex = 0;
104STATIC UINT32 *mGOTCoffEntries = NULL;
105STATIC UINT32 mGOTMaxCoffEntries = 0;
106STATIC UINT32 mGOTNumCoffEntries = 0;
107
108//
109// Coff information
110//
111STATIC UINT32 mCoffAlignment = 0x20;
112
113//
114// PE section alignment.
115//
116STATIC UINT16 mCoffNbrSections = 4;
117
118//
119// ELF sections to offset in Coff file.
120//
121STATIC UINT32 *mCoffSectionsOffset = NULL;
122
123//
124// Offsets in COFF file
125//
126STATIC UINT32 mNtHdrOffset;
127STATIC UINT32 mTextOffset;
128STATIC UINT32 mDataOffset;
129STATIC UINT32 mHiiRsrcOffset;
130STATIC UINT32 mRelocOffset;
131STATIC UINT32 mDebugOffset;
132STATIC UINT32 mExportOffset;
133//
134// Used for RISC-V relocations.
135//
136STATIC UINT8 *mRiscVPass1Targ = NULL;
137STATIC Elf_Shdr *mRiscVPass1Sym = NULL;
138STATIC Elf64_Half mRiscVPass1SymSecIndex = 0;
139STATIC INT32 mRiscVPass1Offset;
140STATIC INT32 mRiscVPass1GotFixup;
141
142//
143// Used for Export section.
144//
145STATIC UINT32 mExportSize;
146STATIC UINT32 mExportRVA[PRM_MODULE_EXPORT_SYMBOL_NUM];
147STATIC UINT32 mExportSymNum;
148STATIC CHAR8 mExportSymName[PRM_MODULE_EXPORT_SYMBOL_NUM][PRM_HANDLER_NAME_MAXIMUM_LENGTH];
149
150//
151// Initialization Function
152//
153BOOLEAN
154InitializeElf64 (
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//
237STATIC
238Elf_Shdr*
239GetShdrByIndex (
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
251STATIC
252UINT32
253CoffAlign (
254 UINT32 Offset
255 )
256{
257 return (Offset + mCoffAlignment - 1) & ~(mCoffAlignment - 1);
258}
259
260STATIC
261UINT32
262DebugRvaAlign (
263 UINT32 Offset
264 )
265{
266 return (Offset + 3) & ~3;
267}
268
269//
270// filter functions
271//
272STATIC
273BOOLEAN
274IsTextShdr (
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
282STATIC
283BOOLEAN
284IsHiiRsrcShdr (
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
293STATIC
294BOOLEAN
295IsSymbolShdr (
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
304STATIC
305BOOLEAN
306IsDataShdr (
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
316STATIC
317BOOLEAN
318IsStrtabShdr (
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
327STATIC
328Elf_Shdr *
329FindStrtabShdr (
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
343STATIC
344const UINT8 *
345GetSymName (
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//
379STATIC
380VOID
381FindPrmHandler (
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//
414STATIC
415VOID
416FindElfGOTSectionFromGOTEntryElfRva (
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
449STATIC
450BOOLEAN
451AccumulateCoffGOTEntries (
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//
486STATIC
487int
488UINT32Comparator (
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//
505STATIC
506VOID
507EmitGOTRelocations (
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//
537STATIC
538VOID
539WriteSectionRiscV64 (
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
773STATIC UINT16 mDllCharacteristicsEx;
774
775STATIC
776VOID
777ParseNoteSection (
778 CONST Elf_Shdr *Shdr
779 )
780{
781 CONST Elf_Note *Note;
782 CONST UINT32 *Prop;
783 UINT32 Prop0;
784 UINT32 Prop2;
785
786 Note = (Elf_Note *)((UINT8 *)mEhdr + Shdr->sh_offset);
787
788 if ((Note->n_type == NT_GNU_PROPERTY_TYPE_0) &&
789 (Note->n_namesz == sizeof ("GNU")) &&
790 (strcmp ((CHAR8 *)(Note + 1), "GNU") == 0) &&
791 (Note->n_descsz > sizeof (UINT32[2]))) {
792 Prop = (UINT32 *)((UINT8 *)(Note + 1) + sizeof("GNU"));
793
794 switch (mEhdr->e_machine) {
795 case EM_AARCH64:
796 Prop0 = GNU_PROPERTY_AARCH64_FEATURE_1_AND;
797 Prop2 = GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
798 break;
799
800 case EM_X86_64:
801 Prop0 = GNU_PROPERTY_X86_FEATURE_1_AND;
802 Prop2 = GNU_PROPERTY_X86_FEATURE_1_IBT;
803 break;
804
805 default:
806 return;
807 }
808 if ((Prop[0] == Prop0) &&
809 (Prop[1] >= sizeof (UINT32)) &&
810 ((Prop[2] & Prop2) != 0)) {
811 mDllCharacteristicsEx |= EFI_IMAGE_DLLCHARACTERISTICS_EX_FORWARD_CFI_COMPAT;
812 }
813 }
814}
815
816//
817// Elf functions interface implementation
818//
819
820STATIC
821VOID
822ScanSections64 (
823 VOID
824 )
825{
826 UINT32 i;
827 EFI_IMAGE_DOS_HEADER *DosHdr;
828 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
829 UINT32 CoffEntry;
830 UINT32 SectionCount;
831 BOOLEAN FoundSection;
832 UINT32 Offset;
833
834 CoffEntry = 0;
835 mCoffOffset = 0;
836
837 //
838 // Coff file start with a DOS header.
839 //
840 mCoffOffset = sizeof(EFI_IMAGE_DOS_HEADER) + 0x40;
841 mNtHdrOffset = mCoffOffset;
842 switch (mEhdr->e_machine) {
843 case EM_X86_64:
844 case EM_AARCH64:
845 case EM_RISCV64:
846 case EM_LOONGARCH:
847 mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS64);
848 break;
849 default:
850 VerboseMsg ("%s unknown e_machine type %hu. Assume X64", mInImageName, mEhdr->e_machine);
851 mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS64);
852 break;
853 }
854
855 mTableOffset = mCoffOffset;
856 mCoffOffset += mCoffNbrSections * sizeof(EFI_IMAGE_SECTION_HEADER);
857
858 //
859 // Set mCoffAlignment to the maximum alignment of the input sections
860 // we care about
861 //
862 for (i = 0; i < mEhdr->e_shnum; i++) {
863 Elf_Shdr *shdr = GetShdrByIndex(i);
864 if (shdr->sh_addralign <= mCoffAlignment) {
865 continue;
866 }
867 if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr)) {
868 mCoffAlignment = (UINT32)shdr->sh_addralign;
869 }
870 }
871
872 for (i = 0; i < mEhdr->e_shnum; i++) {
873 Elf_Shdr *shdr = GetShdrByIndex(i);
874 if (shdr->sh_type == SHT_NOTE) {
875 ParseNoteSection (shdr);
876 }
877 }
878
879 //
880 // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT
881 //
882 if (mCoffAlignment > MAX_COFF_ALIGNMENT) {
883 Error (NULL, 0, 3000, "Invalid", "Section alignment is larger than MAX_COFF_ALIGNMENT.");
884 assert (FALSE);
885 }
886
887
888 //
889 // Move the PE/COFF header right before the first section. This will help us
890 // save space when converting to TE.
891 //
892 if (mCoffAlignment > mCoffOffset) {
893 mNtHdrOffset += mCoffAlignment - mCoffOffset;
894 mTableOffset += mCoffAlignment - mCoffOffset;
895 mCoffOffset = mCoffAlignment;
896 }
897
898 //
899 // First text sections.
900 //
901 mCoffOffset = CoffAlign(mCoffOffset);
902 mTextOffset = mCoffOffset;
903 FoundSection = FALSE;
904 SectionCount = 0;
905 for (i = 0; i < mEhdr->e_shnum; i++) {
906 Elf_Shdr *shdr = GetShdrByIndex(i);
907 if (IsTextShdr(shdr)) {
908 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
909 // the alignment field is valid
910 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
911 // if the section address is aligned we must align PE/COFF
912 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));
913 } else {
914#ifdef VBOX
915 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment. sec#%i addr=%#llx align=%#llx", i, shdr->sh_addr, shdr->sh_addralign);
916#else
917 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
918#endif
919 }
920 }
921
922 /* Relocate entry. */
923 if ((mEhdr->e_entry >= shdr->sh_addr) &&
924 (mEhdr->e_entry < shdr->sh_addr + shdr->sh_size)) {
925 CoffEntry = (UINT32) (mCoffOffset + mEhdr->e_entry - shdr->sh_addr);
926 }
927
928 //
929 // Set mTextOffset with the offset of the first '.text' section
930 //
931 if (!FoundSection) {
932 mTextOffset = mCoffOffset;
933 FoundSection = TRUE;
934 }
935
936 mCoffSectionsOffset[i] = mCoffOffset;
937 mCoffOffset += (UINT32) shdr->sh_size;
938 SectionCount ++;
939 }
940 }
941
942 if (!FoundSection && mOutImageType != FW_ACPI_IMAGE) {
943 Error (NULL, 0, 3000, "Invalid", "Did not find any '.text' section.");
944 assert (FALSE);
945 }
946
947 mDebugOffset = DebugRvaAlign(mCoffOffset);
948 mCoffOffset = CoffAlign(mCoffOffset);
949
950 if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {
951 Warning (NULL, 0, 0, NULL, "Multiple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName);
952 }
953
954 //
955 // Then data sections.
956 //
957 mDataOffset = mCoffOffset;
958 FoundSection = FALSE;
959 SectionCount = 0;
960 for (i = 0; i < mEhdr->e_shnum; i++) {
961 Elf_Shdr *shdr = GetShdrByIndex(i);
962 if (IsDataShdr(shdr)) {
963 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
964 // the alignment field is valid
965 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
966 // if the section address is aligned we must align PE/COFF
967 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));
968 } else {
969#ifdef VBOX
970 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment. sec#%i addr=%#llx align=%#llx", i, shdr->sh_addr, shdr->sh_addralign);
971#else
972 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
973#endif
974 }
975 }
976
977 //
978 // Set mDataOffset with the offset of the first '.data' section
979 //
980 if (!FoundSection) {
981 mDataOffset = mCoffOffset;
982 FoundSection = TRUE;
983 }
984 mCoffSectionsOffset[i] = mCoffOffset;
985 mCoffOffset += (UINT32) shdr->sh_size;
986 SectionCount ++;
987 }
988 }
989
990 //
991 // Make room for .debug data in .data (or .text if .data is empty) instead of
992 // putting it in a section of its own. This is explicitly allowed by the
993 // PE/COFF spec, and prevents bloat in the binary when using large values for
994 // section alignment.
995 //
996 if (SectionCount > 0) {
997 mDebugOffset = DebugRvaAlign(mCoffOffset);
998 }
999 mCoffOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY) +
1000 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) +
1001 strlen(mInImageName) + 1;
1002
1003 //
1004 // Add more space in the .debug data region for the DllCharacteristicsEx
1005 // field.
1006 //
1007 if (mDllCharacteristicsEx != 0) {
1008 mCoffOffset = DebugRvaAlign(mCoffOffset) +
1009 sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY) +
1010 sizeof (EFI_IMAGE_DEBUG_EX_DLLCHARACTERISTICS_ENTRY);
1011 }
1012
1013 mCoffOffset = CoffAlign(mCoffOffset);
1014 if (SectionCount == 0) {
1015 mDataOffset = mCoffOffset;
1016 }
1017
1018 if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {
1019 Warning (NULL, 0, 0, NULL, "Multiple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName);
1020 }
1021
1022 //
1023 // The Symbol sections.
1024 //
1025 if (mExportFlag) {
1026 UINT32 SymIndex;
1027 Elf_Sym *Sym;
1028 UINT64 SymNum;
1029 const UINT8 *SymName;
1030
1031 mExportOffset = mCoffOffset;
1032 mExportSize = sizeof(EFI_IMAGE_EXPORT_DIRECTORY) + strlen(mInImageName) + 1;
1033
1034 for (i = 0; i < mEhdr->e_shnum; i++) {
1035
1036 //
1037 // Determine if this is a symbol section.
1038 //
1039 Elf_Shdr *shdr = GetShdrByIndex(i);
1040 if (!IsSymbolShdr(shdr)) {
1041 continue;
1042 }
1043
1044 UINT8 *Symtab = (UINT8*)mEhdr + shdr->sh_offset;
1045 SymNum = (shdr->sh_size) / (shdr->sh_entsize);
1046
1047 //
1048 // First Get PrmModuleExportDescriptor
1049 //
1050 for (SymIndex = 0; SymIndex < SymNum; SymIndex++) {
1051 Sym = (Elf_Sym *)(Symtab + SymIndex * shdr->sh_entsize);
1052 SymName = GetSymName(Sym);
1053 if (SymName == NULL) {
1054 continue;
1055 }
1056
1057 if (strcmp((CHAR8*)SymName, PRM_MODULE_EXPORT_DESCRIPTOR_NAME) == 0) {
1058 //
1059 // Find PrmHandler Number and Name
1060 //
1061 FindPrmHandler(Sym->st_value);
1062
1063 strcpy(mExportSymName[mExportSymNum], (CHAR8*)SymName);
1064 mExportRVA[mExportSymNum] = (UINT32)(Sym->st_value);
1065 mExportSize += 2 * EFI_IMAGE_EXPORT_ADDR_SIZE + EFI_IMAGE_EXPORT_ORDINAL_SIZE + strlen((CHAR8 *)SymName) + 1;
1066 mExportSymNum ++;
1067 break;
1068 }
1069 }
1070
1071 //
1072 // Second Get PrmHandler
1073 //
1074 for (SymIndex = 0; SymIndex < SymNum; SymIndex++) {
1075 UINT32 ExpIndex;
1076 Sym = (Elf_Sym *)(Symtab + SymIndex * shdr->sh_entsize);
1077 SymName = GetSymName(Sym);
1078 if (SymName == NULL) {
1079 continue;
1080 }
1081
1082 for (ExpIndex = 0; ExpIndex < (mExportSymNum -1); ExpIndex++) {
1083 if (strcmp((CHAR8*)SymName, mExportSymName[ExpIndex]) != 0) {
1084 continue;
1085 }
1086 mExportRVA[ExpIndex] = (UINT32)(Sym->st_value);
1087 mExportSize += 2 * EFI_IMAGE_EXPORT_ADDR_SIZE + EFI_IMAGE_EXPORT_ORDINAL_SIZE + strlen((CHAR8 *)SymName) + 1;
1088 }
1089 }
1090
1091 break;
1092 }
1093
1094 mCoffOffset += mExportSize;
1095 mCoffOffset = CoffAlign(mCoffOffset);
1096 }
1097
1098 //
1099 // The HII resource sections.
1100 //
1101 mHiiRsrcOffset = mCoffOffset;
1102 for (i = 0; i < mEhdr->e_shnum; i++) {
1103 Elf_Shdr *shdr = GetShdrByIndex(i);
1104 if (IsHiiRsrcShdr(shdr)) {
1105 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
1106 // the alignment field is valid
1107 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
1108 // if the section address is aligned we must align PE/COFF
1109 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));
1110 } else {
1111#ifdef VBOX
1112 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment. sec#%i addr=%#llx align=%#llx", i, shdr->sh_addr, shdr->sh_addralign);
1113#else
1114 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
1115#endif
1116 }
1117 }
1118 if (shdr->sh_size != 0) {
1119 mHiiRsrcOffset = mCoffOffset;
1120 mCoffSectionsOffset[i] = mCoffOffset;
1121 mCoffOffset += (UINT32) shdr->sh_size;
1122 mCoffOffset = CoffAlign(mCoffOffset);
1123 SetHiiResourceHeader ((UINT8*) mEhdr + shdr->sh_offset, mHiiRsrcOffset);
1124 }
1125 break;
1126 }
1127 }
1128
1129 mRelocOffset = mCoffOffset;
1130
1131 //
1132 // Allocate base Coff file. Will be expanded later for relocations.
1133 //
1134 mCoffFile = (UINT8 *)malloc(mCoffOffset);
1135 if (mCoffFile == NULL) {
1136 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
1137 }
1138 assert (mCoffFile != NULL);
1139 memset(mCoffFile, 0, mCoffOffset);
1140
1141 //
1142 // Fill headers.
1143 //
1144 DosHdr = (EFI_IMAGE_DOS_HEADER *)mCoffFile;
1145 DosHdr->e_magic = EFI_IMAGE_DOS_SIGNATURE;
1146 DosHdr->e_lfanew = mNtHdrOffset;
1147
1148 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION*)(mCoffFile + mNtHdrOffset);
1149
1150 NtHdr->Pe32Plus.Signature = EFI_IMAGE_NT_SIGNATURE;
1151
1152 switch (mEhdr->e_machine) {
1153 case EM_X86_64:
1154 NtHdr->Pe32Plus.FileHeader.Machine = IMAGE_FILE_MACHINE_X64;
1155 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1156 break;
1157 case EM_AARCH64:
1158 NtHdr->Pe32Plus.FileHeader.Machine = IMAGE_FILE_MACHINE_ARM64;
1159 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1160 break;
1161 case EM_RISCV64:
1162 NtHdr->Pe32Plus.FileHeader.Machine = IMAGE_FILE_MACHINE_RISCV64;
1163 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1164 break;
1165 case EM_LOONGARCH:
1166 NtHdr->Pe32Plus.FileHeader.Machine = IMAGE_FILE_MACHINE_LOONGARCH64;
1167 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1168 break;
1169
1170 default:
1171 VerboseMsg ("%u unknown e_machine type. Assume X64", (UINTN)mEhdr->e_machine);
1172 NtHdr->Pe32Plus.FileHeader.Machine = IMAGE_FILE_MACHINE_X64;
1173 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
1174 }
1175
1176 NtHdr->Pe32Plus.FileHeader.NumberOfSections = mCoffNbrSections;
1177 NtHdr->Pe32Plus.FileHeader.TimeDateStamp = (UINT32) time(NULL);
1178 mImageTimeStamp = NtHdr->Pe32Plus.FileHeader.TimeDateStamp;
1179 NtHdr->Pe32Plus.FileHeader.PointerToSymbolTable = 0;
1180 NtHdr->Pe32Plus.FileHeader.NumberOfSymbols = 0;
1181 NtHdr->Pe32Plus.FileHeader.SizeOfOptionalHeader = sizeof(NtHdr->Pe32Plus.OptionalHeader);
1182 NtHdr->Pe32Plus.FileHeader.Characteristics = EFI_IMAGE_FILE_EXECUTABLE_IMAGE
1183 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
1184 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
1185 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE;
1186
1187 NtHdr->Pe32Plus.OptionalHeader.SizeOfCode = mDataOffset - mTextOffset;
1188 NtHdr->Pe32Plus.OptionalHeader.SizeOfInitializedData = mRelocOffset - mDataOffset;
1189 NtHdr->Pe32Plus.OptionalHeader.SizeOfUninitializedData = 0;
1190 NtHdr->Pe32Plus.OptionalHeader.AddressOfEntryPoint = CoffEntry;
1191
1192 NtHdr->Pe32Plus.OptionalHeader.BaseOfCode = mTextOffset;
1193
1194 NtHdr->Pe32Plus.OptionalHeader.ImageBase = 0;
1195 NtHdr->Pe32Plus.OptionalHeader.SectionAlignment = mCoffAlignment;
1196 NtHdr->Pe32Plus.OptionalHeader.FileAlignment = mCoffAlignment;
1197 NtHdr->Pe32Plus.OptionalHeader.SizeOfImage = 0;
1198
1199 NtHdr->Pe32Plus.OptionalHeader.SizeOfHeaders = mTextOffset;
1200 NtHdr->Pe32Plus.OptionalHeader.NumberOfRvaAndSizes = EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES;
1201
1202 //
1203 // Section headers.
1204 //
1205 if ((mDataOffset - mTextOffset) > 0) {
1206 CreateSectionHeader (".text", mTextOffset, mDataOffset - mTextOffset,
1207 EFI_IMAGE_SCN_CNT_CODE
1208 | EFI_IMAGE_SCN_MEM_EXECUTE
1209 | EFI_IMAGE_SCN_MEM_READ);
1210 } else {
1211 // Don't make a section of size 0.
1212 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
1213 }
1214
1215 //
1216 // If found symbol, add edata section between data and rsrc section
1217 //
1218 if(mExportFlag) {
1219 Offset = mExportOffset;
1220 } else {
1221 Offset = mHiiRsrcOffset;
1222 }
1223
1224 if ((mHiiRsrcOffset - mDataOffset) > 0) {
1225 CreateSectionHeader (".data", mDataOffset, Offset - mDataOffset,
1226 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1227 | EFI_IMAGE_SCN_MEM_WRITE
1228 | EFI_IMAGE_SCN_MEM_READ);
1229 } else {
1230 // Don't make a section of size 0.
1231 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
1232 }
1233
1234 if(mExportFlag) {
1235 if ((mHiiRsrcOffset - mExportOffset) > 0) {
1236 CreateSectionHeader (".edata", mExportOffset, mHiiRsrcOffset - mExportOffset,
1237 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1238 | EFI_IMAGE_SCN_MEM_READ);
1239 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT].Size = mHiiRsrcOffset - mExportOffset;
1240 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress = mExportOffset;
1241
1242 } else {
1243 // Don't make a section of size 0.
1244 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
1245 }
1246 }
1247
1248 if ((mRelocOffset - mHiiRsrcOffset) > 0) {
1249 CreateSectionHeader (".rsrc", mHiiRsrcOffset, mRelocOffset - mHiiRsrcOffset,
1250 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1251 | EFI_IMAGE_SCN_MEM_READ);
1252
1253 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = mRelocOffset - mHiiRsrcOffset;
1254 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = mHiiRsrcOffset;
1255 } else {
1256 // Don't make a section of size 0.
1257 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
1258 }
1259
1260}
1261
1262STATIC
1263BOOLEAN
1264WriteSections64 (
1265 SECTION_FILTER_TYPES FilterType
1266 )
1267{
1268 UINT32 Idx;
1269 Elf_Shdr *SecShdr;
1270 UINT32 SecOffset;
1271 BOOLEAN (*Filter)(Elf_Shdr *);
1272 Elf64_Addr GOTEntryRva;
1273
1274 //
1275 // Initialize filter pointer
1276 //
1277 switch (FilterType) {
1278 case SECTION_TEXT:
1279 Filter = IsTextShdr;
1280 break;
1281 case SECTION_HII:
1282 Filter = IsHiiRsrcShdr;
1283 break;
1284 case SECTION_DATA:
1285 Filter = IsDataShdr;
1286 break;
1287 default:
1288 return FALSE;
1289 }
1290
1291 //
1292 // First: copy sections.
1293 //
1294 for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {
1295 Elf_Shdr *Shdr = GetShdrByIndex(Idx);
1296 if ((*Filter)(Shdr)) {
1297 switch (Shdr->sh_type) {
1298 case SHT_PROGBITS:
1299 /* Copy. */
1300 if (Shdr->sh_offset + Shdr->sh_size > mFileBufferSize) {
1301 return FALSE;
1302 }
1303 memcpy(mCoffFile + mCoffSectionsOffset[Idx],
1304 (UINT8*)mEhdr + Shdr->sh_offset,
1305 (size_t) Shdr->sh_size);
1306 break;
1307
1308 case SHT_NOBITS:
1309 memset(mCoffFile + mCoffSectionsOffset[Idx], 0, (size_t) Shdr->sh_size);
1310 break;
1311
1312 default:
1313 //
1314 // Ignore for unknown section type.
1315 //
1316 VerboseMsg ("%s unknown section type %x. We ignore this unknown section type.", mInImageName, (unsigned)Shdr->sh_type);
1317 break;
1318 }
1319 }
1320 }
1321
1322 //
1323 // Second: apply relocations.
1324 //
1325 VerboseMsg ("Applying Relocations...");
1326 for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {
1327 //
1328 // Determine if this is a relocation section.
1329 //
1330 Elf_Shdr *RelShdr = GetShdrByIndex(Idx);
1331 if ((RelShdr->sh_type != SHT_REL) && (RelShdr->sh_type != SHT_RELA)) {
1332 continue;
1333 }
1334
1335 //
1336 // If this is a ET_DYN (PIE) executable, we will encounter a dynamic SHT_RELA
1337 // section that applies to the entire binary, and which will have its section
1338 // index set to #0 (which is a NULL section with the SHF_ALLOC bit cleared).
1339 //
1340 // In the absence of GOT based relocations,
1341 // this RELA section will contain redundant R_xxx_RELATIVE relocations, one
1342 // for every R_xxx_xx64 relocation appearing in the per-section RELA sections.
1343 // (i.e., .rela.text and .rela.data)
1344 //
1345 if (RelShdr->sh_info == 0) {
1346 continue;
1347 }
1348
1349 //
1350 // Relocation section found. Now extract section information that the relocations
1351 // apply to in the ELF data and the new COFF data.
1352 //
1353 SecShdr = GetShdrByIndex(RelShdr->sh_info);
1354 SecOffset = mCoffSectionsOffset[RelShdr->sh_info];
1355
1356 //
1357 // Only process relocations for the current filter type.
1358 //
1359 if (RelShdr->sh_type == SHT_RELA && (*Filter)(SecShdr)) {
1360 UINT64 RelIdx;
1361
1362 //
1363 // Determine the symbol table referenced by the relocation data.
1364 //
1365 Elf_Shdr *SymtabShdr = GetShdrByIndex(RelShdr->sh_link);
1366 UINT8 *Symtab = (UINT8*)mEhdr + SymtabShdr->sh_offset;
1367
1368 //
1369 // Process all relocation entries for this section.
1370 //
1371 for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += (UINT32) RelShdr->sh_entsize) {
1372
1373 //
1374 // Set pointer to relocation entry
1375 //
1376 Elf_Rela *Rel = (Elf_Rela *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx);
1377
1378 //
1379 // Set pointer to symbol table entry associated with the relocation entry.
1380 //
1381 Elf_Sym *Sym = (Elf_Sym *)(Symtab + ELF_R_SYM(Rel->r_info) * SymtabShdr->sh_entsize);
1382
1383 Elf_Shdr *SymShdr;
1384 UINT8 *Targ;
1385
1386 //
1387 // The _GLOBAL_OFFSET_TABLE_ symbol is not actually an absolute symbol,
1388 // but carries the SHN_ABS section index for historical reasons.
1389 // It must be accompanied by a R_*_GOT_* type relocation on a
1390 // subsequent instruction, which we handle below, specifically to avoid
1391 // the GOT indirection, and to refer to the symbol directly. This means
1392 // we can simply disregard direct references to the GOT symbol itself,
1393 // as the resulting value will never be used.
1394 //
1395 if (Sym->st_shndx == SHN_ABS) {
1396 const UINT8 *SymName = GetSymName (Sym);
1397 if (strcmp ((CHAR8 *)SymName, "_GLOBAL_OFFSET_TABLE_") == 0) {
1398 continue;
1399 }
1400 }
1401
1402 //
1403 // Check section header index found in symbol table and get the section
1404 // header location.
1405 //
1406 if (Sym->st_shndx == SHN_UNDEF
1407 || Sym->st_shndx >= mEhdr->e_shnum) {
1408 const UINT8 *SymName = GetSymName(Sym);
1409 if (SymName == NULL) {
1410 SymName = (const UINT8 *)"<unknown>";
1411 }
1412
1413 //
1414 // Skip error on EM_RISCV64 and EM_LOONGARCH because no symbol name is built
1415 // from RISC-V and LoongArch toolchain.
1416 //
1417 if ((mEhdr->e_machine != EM_RISCV64) && (mEhdr->e_machine != EM_LOONGARCH)) {
1418 Error (NULL, 0, 3000, "Invalid",
1419 "%s: Bad definition for symbol '%s'@%#llx or unsupported symbol type. "
1420 "For example, absolute and undefined symbols are not supported.",
1421 mInImageName, SymName, Sym->st_value);
1422
1423 exit(EXIT_FAILURE);
1424 }
1425 continue;
1426 }
1427 SymShdr = GetShdrByIndex(Sym->st_shndx);
1428
1429 //
1430 // Convert the relocation data to a pointer into the coff file.
1431 //
1432 // Note:
1433 // r_offset is the virtual address of the storage unit to be relocated.
1434 // sh_addr is the virtual address for the base of the section.
1435 //
1436 // r_offset in a memory address.
1437 // Convert it to a pointer in the coff file.
1438 //
1439 Targ = mCoffFile + SecOffset + (Rel->r_offset - SecShdr->sh_addr);
1440
1441 //
1442 // Determine how to handle each relocation type based on the machine type.
1443 //
1444 if (mEhdr->e_machine == EM_X86_64) {
1445 switch (ELF_R_TYPE(Rel->r_info)) {
1446 case R_X86_64_NONE:
1447 break;
1448 case R_X86_64_64:
1449 //
1450 // Absolute relocation.
1451 //
1452 VerboseMsg ("R_X86_64_64");
1453 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1454 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
1455 *(UINT64 *)Targ);
1456 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
1457 VerboseMsg ("Relocation: 0x%016LX", *(UINT64*)Targ);
1458 break;
1459 case R_X86_64_32:
1460 VerboseMsg ("R_X86_64_32");
1461 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1462 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
1463 *(UINT32 *)Targ);
1464 *(UINT32 *)Targ = (UINT32)((UINT64)(*(UINT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
1465 VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);
1466 break;
1467 case R_X86_64_32S:
1468 VerboseMsg ("R_X86_64_32S");
1469 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1470 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
1471 *(UINT32 *)Targ);
1472 *(INT32 *)Targ = (INT32)((INT64)(*(INT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
1473 VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);
1474 break;
1475
1476 case R_X86_64_PLT32:
1477 //
1478 // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is
1479 // possible since we know all code symbol references resolve to
1480 // definitions in the same module (UEFI has no shared libraries),
1481 // and so there is never a reason to jump via a PLT entry,
1482 // allowing us to resolve the reference using the symbol directly.
1483 //
1484 VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");
1485 /* fall through */
1486 case R_X86_64_PC32:
1487 //
1488 // Relative relocation: Symbol - Ip + Addend
1489 //
1490 VerboseMsg ("R_X86_64_PC32");
1491 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1492 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
1493 *(UINT32 *)Targ);
1494 *(UINT32 *)Targ = (UINT32) (*(UINT32 *)Targ
1495 + (mCoffSectionsOffset[Sym->st_shndx] - SymShdr->sh_addr)
1496 - (SecOffset - SecShdr->sh_addr));
1497 VerboseMsg ("Relocation: 0x%08X", *(UINT32 *)Targ);
1498 break;
1499 case R_X86_64_REX_GOTPCRELX:
1500 //
1501 // This is a relaxable GOTPCREL relocation, and the linker may have
1502 // applied this relaxation without updating the relocation type.
1503 // In the position independent code model, only transformations
1504 // from MOV to LEA are possible for REX-prefixed instructions.
1505 //
1506 if (Targ[-2] == 0x8d) { // LEA
1507 break;
1508 }
1509 /* fall through */ /* VBox added */
1510 case R_X86_64_GOTPCREL:
1511 case R_X86_64_GOTPCRELX:
1512 VerboseMsg ("R_X86_64_GOTPCREL family");
1513 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1514 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
1515 *(UINT32 *)Targ);
1516 GOTEntryRva = Rel->r_offset - Rel->r_addend + *(INT32 *)Targ;
1517 FindElfGOTSectionFromGOTEntryElfRva(GOTEntryRva);
1518 *(UINT32 *)Targ = (UINT32) (*(UINT32 *)Targ
1519 + (mCoffSectionsOffset[mGOTShindex] - mGOTShdr->sh_addr)
1520 - (SecOffset - SecShdr->sh_addr));
1521 VerboseMsg ("Relocation: 0x%08X", *(UINT32 *)Targ);
1522 GOTEntryRva += (mCoffSectionsOffset[mGOTShindex] - mGOTShdr->sh_addr); // ELF Rva -> COFF Rva
1523 if (AccumulateCoffGOTEntries((UINT32)GOTEntryRva)) {
1524 //
1525 // Relocate GOT entry if it's the first time we run into it
1526 //
1527 Targ = mCoffFile + GOTEntryRva;
1528 //
1529 // Limitation: The following three statements assume memory
1530 // at *Targ is valid because the section containing the GOT
1531 // has already been copied from the ELF image to the Coff image.
1532 // This pre-condition presently holds because the GOT is placed
1533 // in section .text, and the ELF text sections are all copied
1534 // prior to reaching this point.
1535 // If the pre-condition is violated in the future, this fixup
1536 // either needs to be deferred after the GOT section is copied
1537 // to the Coff image, or the fixup should be performed on the
1538 // source Elf image instead of the destination Coff image.
1539 //
1540 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1541 (UINT32)GOTEntryRva,
1542 *(UINT64 *)Targ);
1543 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
1544 VerboseMsg ("Relocation: 0x%016LX", *(UINT64*)Targ);
1545 }
1546 break;
1547 default:
1548 Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1549 }
1550 } else if (mEhdr->e_machine == EM_AARCH64) {
1551
1552 switch (ELF_R_TYPE(Rel->r_info)) {
1553 INT64 Offset;
1554
1555 case R_AARCH64_LD64_GOTOFF_LO15:
1556 case R_AARCH64_LD64_GOTPAGE_LO15:
1557 //
1558 // Convert into an ADR instruction that refers to the symbol directly.
1559 //
1560 Offset = Sym->st_value - Rel->r_offset;
1561
1562 *(UINT32 *)Targ &= 0x1000001f;
1563 *(UINT32 *)Targ |= ((Offset & 0x1ffffc) << (5 - 2)) | ((Offset & 0x3) << 29);
1564
1565 if (Offset < -0x100000 || Offset > 0xfffff) {
1566 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s failed to relax GOT based symbol reference - image is too big (>1 MiB).",
1567 mInImageName);
1568 break;
1569 }
1570 break;
1571
1572 case R_AARCH64_LD64_GOT_LO12_NC:
1573 //
1574 // Convert into an ADD instruction - see R_AARCH64_ADR_GOT_PAGE below.
1575 //
1576 *(UINT32 *)Targ &= 0x3ff;
1577 *(UINT32 *)Targ |= 0x91000000 | ((Sym->st_value & 0xfff) << 10);
1578 break;
1579
1580 case R_AARCH64_ADR_GOT_PAGE:
1581 //
1582 // This relocation points to the GOT entry that contains the absolute
1583 // address of the symbol we are referring to. Since EDK2 only uses
1584 // fully linked binaries, we can avoid the indirection, and simply
1585 // refer to the symbol directly. This implies having to patch the
1586 // subsequent LDR instruction (covered by a R_AARCH64_LD64_GOT_LO12_NC
1587 // relocation) into an ADD instruction - this is handled above.
1588 //
1589 // In order to handle Cortex-A53 erratum #843419, the GCC toolchain
1590 // may convert an ADRP instruction at the end of a page (0xffc
1591 // offset) into an ADR instruction. If so, be sure to calculate the
1592 // offset for an ADR instead of ADRP.
1593 //
1594 if ((*(UINT32 *)Targ & BIT31) == 0) {
1595 //
1596 // Calculate the offset for an ADR.
1597 //
1598 Offset = (Sym->st_value & ~0xfff) - Rel->r_offset;
1599 if (Offset < -0x100000 || Offset > 0xfffff) {
1600 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s due to its size (> 1 MB), unable to relocate ADR.",
1601 mInImageName);
1602 break;
1603 }
1604 } else {
1605 //
1606 // Calculate the offset for an ADRP.
1607 //
1608 Offset = (Sym->st_value - (Rel->r_offset & ~0xfff)) >> 12;
1609 }
1610
1611 *(UINT32 *)Targ &= 0x9000001f;
1612 *(UINT32 *)Targ |= ((Offset & 0x1ffffc) << (5 - 2)) | ((Offset & 0x3) << 29);
1613
1614 /* fall through */
1615
1616 case R_AARCH64_ADR_PREL_PG_HI21:
1617 //
1618 // In order to handle Cortex-A53 erratum #843419, the LD linker may
1619 // convert ADRP instructions into ADR instructions, but without
1620 // updating the static relocation type, and so we may end up here
1621 // while the instruction in question is actually ADR. So let's
1622 // just disregard it: the section offset check we apply below to
1623 // ADR instructions will trigger for its R_AARCH64_xxx_ABS_LO12_NC
1624 // companion instruction as well, so it is safe to omit it here.
1625 //
1626 if ((*(UINT32 *)Targ & BIT31) == 0) {
1627 break;
1628 }
1629
1630 //
1631 // AArch64 PG_H21 relocations are typically paired with ABS_LO12
1632 // relocations, where a PC-relative reference with +/- 4 GB range is
1633 // split into a relative high part and an absolute low part. Since
1634 // the absolute low part represents the offset into a 4 KB page, we
1635 // either have to convert the ADRP into an ADR instruction, or we
1636 // need to use a section alignment of at least 4 KB, so that the
1637 // binary appears at a correct offset at runtime. In any case, we
1638 // have to make sure that the 4 KB relative offsets of both the
1639 // section containing the reference as well as the section to which
1640 // it refers have not been changed during PE/COFF conversion (i.e.,
1641 // in ScanSections64() above).
1642 //
1643 if (mCoffAlignment < 0x1000) {
1644 //
1645 // Attempt to convert the ADRP into an ADR instruction.
1646 // This is only possible if the symbol is within +/- 1 MB.
1647 //
1648
1649 // Decode the ADRP instruction
1650 Offset = (INT32)((*(UINT32 *)Targ & 0xffffe0) << 8);
1651 Offset = (Offset << (6 - 5)) | ((*(UINT32 *)Targ & 0x60000000) >> (29 - 12));
1652
1653 //
1654 // ADRP offset is relative to the previous page boundary,
1655 // whereas ADR offset is relative to the instruction itself.
1656 // So fix up the offset so it points to the page containing
1657 // the symbol.
1658 //
1659 Offset -= (UINTN)(Targ - mCoffFile) & 0xfff;
1660
1661 if (Offset < -0x100000 || Offset > 0xfffff) {
1662 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s due to its size (> 1 MB), this module requires 4 KB section alignment.",
1663 mInImageName);
1664 break;
1665 }
1666
1667 // Re-encode the offset as an ADR instruction
1668 *(UINT32 *)Targ &= 0x1000001f;
1669 *(UINT32 *)Targ |= ((Offset & 0x1ffffc) << (5 - 2)) | ((Offset & 0x3) << 29);
1670 }
1671 /* fall through */
1672
1673 case R_AARCH64_ADD_ABS_LO12_NC:
1674 case R_AARCH64_LDST8_ABS_LO12_NC:
1675 case R_AARCH64_LDST16_ABS_LO12_NC:
1676 case R_AARCH64_LDST32_ABS_LO12_NC:
1677 case R_AARCH64_LDST64_ABS_LO12_NC:
1678 case R_AARCH64_LDST128_ABS_LO12_NC:
1679 if (((SecShdr->sh_addr ^ SecOffset) & 0xfff) != 0 ||
1680 ((SymShdr->sh_addr ^ mCoffSectionsOffset[Sym->st_shndx]) & 0xfff) != 0) {
1681 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 small code model requires identical ELF and PE/COFF section offsets modulo 4 KB.",
1682 mInImageName);
1683 break;
1684 }
1685 /* fall through */
1686
1687 case R_AARCH64_ADR_PREL_LO21:
1688 case R_AARCH64_CONDBR19:
1689 case R_AARCH64_LD_PREL_LO19:
1690 case R_AARCH64_CALL26:
1691 case R_AARCH64_JUMP26:
1692 case R_AARCH64_PREL64:
1693 case R_AARCH64_PREL32:
1694 case R_AARCH64_PREL16:
1695 //
1696 // The GCC toolchains (i.e., binutils) may corrupt section relative
1697 // relocations when emitting relocation sections into fully linked
1698 // binaries. More specifically, they tend to fail to take into
1699 // account the fact that a '.rodata + XXX' relocation needs to have
1700 // its addend recalculated once .rodata is merged into the .text
1701 // section, and the relocation emitted into the .rela.text section.
1702 //
1703 // We cannot really recover from this loss of information, so the
1704 // only workaround is to prevent having to recalculate any relative
1705 // relocations at all, by using a linker script that ensures that
1706 // the offset between the Place and the Symbol is the same in both
1707 // the ELF and the PE/COFF versions of the binary.
1708 //
1709 if ((SymShdr->sh_addr - SecShdr->sh_addr) !=
1710 (mCoffSectionsOffset[Sym->st_shndx] - SecOffset)) {
1711 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 relative relocations require identical ELF and PE/COFF section offsets",
1712 mInImageName);
1713 }
1714 break;
1715
1716 // Absolute relocations.
1717 case R_AARCH64_ABS64:
1718 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
1719 break;
1720
1721 default:
1722 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1723 }
1724 } else if (mEhdr->e_machine == EM_RISCV64) {
1725 //
1726 // Write section for RISC-V 64 architecture.
1727 //
1728 WriteSectionRiscV64 (Rel, Targ, SymShdr, Sym);
1729 } else if (mEhdr->e_machine == EM_LOONGARCH) {
1730 switch (ELF_R_TYPE(Rel->r_info)) {
1731 INT64 Offset;
1732 INT32 Lo, Hi;
1733
1734 case R_LARCH_SOP_PUSH_ABSOLUTE:
1735 //
1736 // Absolute relocation.
1737 //
1738 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
1739 break;
1740
1741 case R_LARCH_MARK_LA:
1742 case R_LARCH_64:
1743 case R_LARCH_NONE:
1744 case R_LARCH_32:
1745 case R_LARCH_RELATIVE:
1746 case R_LARCH_COPY:
1747 case R_LARCH_JUMP_SLOT:
1748 case R_LARCH_TLS_DTPMOD32:
1749 case R_LARCH_TLS_DTPMOD64:
1750 case R_LARCH_TLS_DTPREL32:
1751 case R_LARCH_TLS_DTPREL64:
1752 case R_LARCH_TLS_TPREL32:
1753 case R_LARCH_TLS_TPREL64:
1754 case R_LARCH_IRELATIVE:
1755 case R_LARCH_MARK_PCREL:
1756 case R_LARCH_SOP_PUSH_PCREL:
1757 case R_LARCH_SOP_PUSH_DUP:
1758 case R_LARCH_SOP_PUSH_GPREL:
1759 case R_LARCH_SOP_PUSH_TLS_TPREL:
1760 case R_LARCH_SOP_PUSH_TLS_GOT:
1761 case R_LARCH_SOP_PUSH_TLS_GD:
1762 case R_LARCH_SOP_PUSH_PLT_PCREL:
1763 case R_LARCH_SOP_ASSERT:
1764 case R_LARCH_SOP_NOT:
1765 case R_LARCH_SOP_SUB:
1766 case R_LARCH_SOP_SL:
1767 case R_LARCH_SOP_SR:
1768 case R_LARCH_SOP_ADD:
1769 case R_LARCH_SOP_AND:
1770 case R_LARCH_SOP_IF_ELSE:
1771 case R_LARCH_SOP_POP_32_S_10_5:
1772 case R_LARCH_SOP_POP_32_U_10_12:
1773 case R_LARCH_SOP_POP_32_S_10_12:
1774 case R_LARCH_SOP_POP_32_S_10_16:
1775 case R_LARCH_SOP_POP_32_S_10_16_S2:
1776 case R_LARCH_SOP_POP_32_S_5_20:
1777 case R_LARCH_SOP_POP_32_S_0_5_10_16_S2:
1778 case R_LARCH_SOP_POP_32_S_0_10_10_16_S2:
1779 case R_LARCH_SOP_POP_32_U:
1780 case R_LARCH_ADD8:
1781 case R_LARCH_ADD16:
1782 case R_LARCH_ADD24:
1783 case R_LARCH_ADD32:
1784 case R_LARCH_ADD64:
1785 case R_LARCH_SUB8:
1786 case R_LARCH_SUB16:
1787 case R_LARCH_SUB24:
1788 case R_LARCH_SUB32:
1789 case R_LARCH_SUB64:
1790 case R_LARCH_GNU_VTINHERIT:
1791 case R_LARCH_GNU_VTENTRY:
1792 case R_LARCH_B16:
1793 case R_LARCH_B21:
1794 case R_LARCH_B26:
1795 case R_LARCH_ABS_HI20:
1796 case R_LARCH_ABS_LO12:
1797 case R_LARCH_ABS64_LO20:
1798 case R_LARCH_ABS64_HI12:
1799 case R_LARCH_PCALA_LO12:
1800 case R_LARCH_PCALA64_LO20:
1801 case R_LARCH_PCALA64_HI12:
1802 case R_LARCH_GOT_PC_LO12:
1803 case R_LARCH_GOT64_PC_LO20:
1804 case R_LARCH_GOT64_PC_HI12:
1805 case R_LARCH_GOT64_HI20:
1806 case R_LARCH_GOT64_LO12:
1807 case R_LARCH_GOT64_LO20:
1808 case R_LARCH_GOT64_HI12:
1809 case R_LARCH_TLS_LE_HI20:
1810 case R_LARCH_TLS_LE_LO12:
1811 case R_LARCH_TLS_LE64_LO20:
1812 case R_LARCH_TLS_LE64_HI12:
1813 case R_LARCH_TLS_IE_PC_HI20:
1814 case R_LARCH_TLS_IE_PC_LO12:
1815 case R_LARCH_TLS_IE64_PC_LO20:
1816 case R_LARCH_TLS_IE64_PC_HI12:
1817 case R_LARCH_TLS_IE64_HI20:
1818 case R_LARCH_TLS_IE64_LO12:
1819 case R_LARCH_TLS_IE64_LO20:
1820 case R_LARCH_TLS_IE64_HI12:
1821 case R_LARCH_TLS_LD_PC_HI20:
1822 case R_LARCH_TLS_LD64_HI20:
1823 case R_LARCH_TLS_GD_PC_HI20:
1824 case R_LARCH_TLS_GD64_HI20:
1825 case R_LARCH_32_PCREL:
1826 case R_LARCH_RELAX:
1827 case R_LARCH_DELETE:
1828 case R_LARCH_ALIGN:
1829 case R_LARCH_PCREL20_S2:
1830 case R_LARCH_CFA:
1831 case R_LARCH_ADD6:
1832 case R_LARCH_SUB6:
1833 case R_LARCH_ADD_ULEB128:
1834 case R_LARCH_SUB_ULEB128:
1835 case R_LARCH_64_PCREL:
1836 //
1837 // These types are not used or do not require fixup.
1838 //
1839 break;
1840
1841 case R_LARCH_GOT_PC_HI20:
1842 Offset = Sym->st_value - (UINTN)(Targ - mCoffFile);
1843 if (Offset < 0) {
1844 Offset = (UINTN)(Targ - mCoffFile) - Sym->st_value;
1845 Hi = Offset & ~0xfff;
1846 Lo = (INT32)((Offset & 0xfff) << 20) >> 20;
1847 if ((Lo < 0) && (Lo > -2048)) {
1848 Hi += 0x1000;
1849 Lo = ~(0x1000 - Lo) + 1;
1850 }
1851 Hi = ~Hi + 1;
1852 Lo = ~Lo + 1;
1853 } else {
1854 Hi = Offset & ~0xfff;
1855 Lo = (INT32)((Offset & 0xfff) << 20) >> 20;
1856 if (Lo < 0) {
1857 Hi += 0x1000;
1858 Lo = ~(0x1000 - Lo) + 1;
1859 }
1860 }
1861 // Re-encode the offset as PCADDU12I + ADDI.D(Convert LD.D) instruction
1862 *(UINT32 *)Targ &= 0x1f;
1863 *(UINT32 *)Targ |= 0x1c000000;
1864 *(UINT32 *)Targ |= (((Hi >> 12) & 0xfffff) << 5);
1865 *(UINT32 *)(Targ + 4) &= 0x3ff;
1866 *(UINT32 *)(Targ + 4) |= 0x2c00000 | ((Lo & 0xfff) << 10);
1867 break;
1868
1869 //
1870 // Attempt to convert instruction.
1871 //
1872 case R_LARCH_PCALA_HI20:
1873 // Decode the PCALAU12I instruction and the instruction that following it.
1874 Offset = ((INT32)((*(UINT32 *)Targ & 0x1ffffe0) << 7));
1875 Offset += ((INT32)((*(UINT32 *)(Targ + 4) & 0x3ffc00) << 10) >> 20);
1876 //
1877 // PCALA offset is relative to the previous page boundary,
1878 // whereas PCADD offset is relative to the instruction itself.
1879 // So fix up the offset so it points to the page containing
1880 // the symbol.
1881 //
1882 Offset -= (UINTN)(Targ - mCoffFile) & 0xfff;
1883 if (Offset < 0) {
1884 Offset = -Offset;
1885 Hi = Offset & ~0xfff;
1886 Lo = (INT32)((Offset & 0xfff) << 20) >> 20;
1887 if ((Lo < 0) && (Lo > -2048)) {
1888 Hi += 0x1000;
1889 Lo = ~(0x1000 - Lo) + 1;
1890 }
1891 Hi = ~Hi + 1;
1892 Lo = ~Lo + 1;
1893 } else {
1894 Hi = Offset & ~0xfff;
1895 Lo = (INT32)((Offset & 0xfff) << 20) >> 20;
1896 if (Lo < 0) {
1897 Hi += 0x1000;
1898 Lo = ~(0x1000 - Lo) + 1;
1899 }
1900 }
1901 // Convert the first instruction from PCALAU12I to PCADDU12I and re-encode the offset into them.
1902 *(UINT32 *)Targ &= 0x1f;
1903 *(UINT32 *)Targ |= 0x1c000000;
1904 *(UINT32 *)Targ |= (((Hi >> 12) & 0xfffff) << 5);
1905 *(UINT32 *)(Targ + 4) &= 0xffc003ff;
1906 *(UINT32 *)(Targ + 4) |= (Lo & 0xfff) << 10;
1907 break;
1908 default:
1909 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_LOONGARCH relocation 0x%x.", mInImageName, (unsigned) ELF64_R_TYPE(Rel->r_info));
1910 }
1911 } else {
1912 Error (NULL, 0, 3000, "Invalid", "Not a supported machine type");
1913 }
1914 }
1915 }
1916 }
1917
1918 return TRUE;
1919}
1920
1921STATIC
1922VOID
1923WriteRelocations64 (
1924 VOID
1925 )
1926{
1927 UINT32 Index;
1928 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
1929 EFI_IMAGE_DATA_DIRECTORY *Dir;
1930 UINT32 RiscVRelType;
1931
1932 for (Index = 0; Index < mEhdr->e_shnum; Index++) {
1933 Elf_Shdr *RelShdr = GetShdrByIndex(Index);
1934 if ((RelShdr->sh_type == SHT_REL) || (RelShdr->sh_type == SHT_RELA)) {
1935 Elf_Shdr *SecShdr = GetShdrByIndex (RelShdr->sh_info);
1936 if (IsTextShdr(SecShdr) || IsDataShdr(SecShdr)) {
1937 UINT64 RelIdx;
1938
1939 for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += RelShdr->sh_entsize) {
1940 Elf_Rela *Rel = (Elf_Rela *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx);
1941
1942 if (mEhdr->e_machine == EM_X86_64) {
1943 switch (ELF_R_TYPE(Rel->r_info)) {
1944 case R_X86_64_NONE:
1945 case R_X86_64_PC32:
1946 case R_X86_64_PLT32:
1947 case R_X86_64_GOTPCREL:
1948 case R_X86_64_GOTPCRELX:
1949 case R_X86_64_REX_GOTPCRELX:
1950 break;
1951 case R_X86_64_64:
1952 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08llX",
1953 mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr));
1954 CoffAddFixup(
1955 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1956 + (Rel->r_offset - SecShdr->sh_addr)),
1957 EFI_IMAGE_REL_BASED_DIR64);
1958 break;
1959 //
1960 // R_X86_64_32 and R_X86_64_32S are ELF64 relocations emitted when using
1961 // the SYSV X64 ABI small non-position-independent code model.
1962 // R_X86_64_32 is used for unsigned 32-bit immediates with a 32-bit operand
1963 // size. The value is either not extended, or zero-extended to 64 bits.
1964 // R_X86_64_32S is used for either signed 32-bit non-rip-relative displacements
1965 // or signed 32-bit immediates with a 64-bit operand size. The value is
1966 // sign-extended to 64 bits.
1967 // EFI_IMAGE_REL_BASED_HIGHLOW is a PE relocation that uses 32-bit arithmetic
1968 // for rebasing an image.
1969 // EFI PE binaries declare themselves EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE and
1970 // may load above 2GB. If an EFI PE binary with a converted R_X86_64_32S
1971 // relocation is loaded above 2GB, the value will get sign-extended to the
1972 // negative part of the 64-bit address space. The negative part of the 64-bit
1973 // address space is unmapped, so accessing such an address page-faults.
1974 // In order to support R_X86_64_32S, it is necessary to unset
1975 // EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE, and the EFI PE loader must implement
1976 // this flag and abstain from loading such a PE binary above 2GB.
1977 // Since this feature is not supported, support for R_X86_64_32S (and hence
1978 // the small non-position-independent code model) is disabled.
1979 //
1980 // case R_X86_64_32S:
1981 case R_X86_64_32:
1982 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08llX",
1983 mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr));
1984 CoffAddFixup(
1985 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1986 + (Rel->r_offset - SecShdr->sh_addr)),
1987 EFI_IMAGE_REL_BASED_HIGHLOW);
1988 break;
1989 default:
1990 Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1991 }
1992 } else if (mEhdr->e_machine == EM_AARCH64) {
1993
1994 switch (ELF_R_TYPE(Rel->r_info)) {
1995 case R_AARCH64_ADR_PREL_LO21:
1996 case R_AARCH64_CONDBR19:
1997 case R_AARCH64_LD_PREL_LO19:
1998 case R_AARCH64_CALL26:
1999 case R_AARCH64_JUMP26:
2000 case R_AARCH64_PREL64:
2001 case R_AARCH64_PREL32:
2002 case R_AARCH64_PREL16:
2003 case R_AARCH64_ADR_PREL_PG_HI21:
2004 case R_AARCH64_ADD_ABS_LO12_NC:
2005 case R_AARCH64_LDST8_ABS_LO12_NC:
2006 case R_AARCH64_LDST16_ABS_LO12_NC:
2007 case R_AARCH64_LDST32_ABS_LO12_NC:
2008 case R_AARCH64_LDST64_ABS_LO12_NC:
2009 case R_AARCH64_LDST128_ABS_LO12_NC:
2010 case R_AARCH64_ADR_GOT_PAGE:
2011 case R_AARCH64_LD64_GOT_LO12_NC:
2012 case R_AARCH64_LD64_GOTOFF_LO15:
2013 case R_AARCH64_LD64_GOTPAGE_LO15:
2014 //
2015 // No fixups are required for relative relocations, provided that
2016 // the relative offsets between sections have been preserved in
2017 // the ELF to PE/COFF conversion. We have already asserted that
2018 // this is the case in WriteSections64 ().
2019 //
2020 break;
2021
2022 case R_AARCH64_ABS64:
2023 CoffAddFixup(
2024 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2025 + (Rel->r_offset - SecShdr->sh_addr)),
2026 EFI_IMAGE_REL_BASED_DIR64);
2027 break;
2028
2029 case R_AARCH64_ABS32:
2030 CoffAddFixup(
2031 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2032 + (Rel->r_offset - SecShdr->sh_addr)),
2033 EFI_IMAGE_REL_BASED_HIGHLOW);
2034 break;
2035
2036 default:
2037 Error (NULL, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
2038 }
2039 } else if (mEhdr->e_machine == EM_RISCV64) {
2040 RiscVRelType = ELF_R_TYPE(Rel->r_info);
2041 switch (RiscVRelType) {
2042 case R_RISCV_NONE:
2043 break;
2044
2045 case R_RISCV_32:
2046 CoffAddFixup(
2047 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2048 + (Rel->r_offset - SecShdr->sh_addr)),
2049 EFI_IMAGE_REL_BASED_HIGHLOW);
2050 break;
2051
2052 case R_RISCV_64:
2053 CoffAddFixup(
2054 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2055 + (Rel->r_offset - SecShdr->sh_addr)),
2056 EFI_IMAGE_REL_BASED_DIR64);
2057 break;
2058
2059 case R_RISCV_HI20:
2060 CoffAddFixup(
2061 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2062 + (Rel->r_offset - SecShdr->sh_addr)),
2063 EFI_IMAGE_REL_BASED_RISCV_HI20);
2064 break;
2065
2066 case R_RISCV_LO12_I:
2067 CoffAddFixup(
2068 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2069 + (Rel->r_offset - SecShdr->sh_addr)),
2070 EFI_IMAGE_REL_BASED_RISCV_LOW12I);
2071 break;
2072
2073 case R_RISCV_LO12_S:
2074 CoffAddFixup(
2075 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2076 + (Rel->r_offset - SecShdr->sh_addr)),
2077 EFI_IMAGE_REL_BASED_RISCV_LOW12S);
2078 break;
2079
2080 case R_RISCV_ADD64:
2081 CoffAddFixup(
2082 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2083 + (Rel->r_offset - SecShdr->sh_addr)),
2084 EFI_IMAGE_REL_BASED_ABSOLUTE);
2085 break;
2086
2087 case R_RISCV_SUB64:
2088 CoffAddFixup(
2089 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2090 + (Rel->r_offset - SecShdr->sh_addr)),
2091 EFI_IMAGE_REL_BASED_ABSOLUTE);
2092 break;
2093
2094 case R_RISCV_ADD32:
2095 CoffAddFixup(
2096 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2097 + (Rel->r_offset - SecShdr->sh_addr)),
2098 EFI_IMAGE_REL_BASED_ABSOLUTE);
2099 break;
2100
2101 case R_RISCV_SUB32:
2102 CoffAddFixup(
2103 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2104 + (Rel->r_offset - SecShdr->sh_addr)),
2105 EFI_IMAGE_REL_BASED_ABSOLUTE);
2106 break;
2107
2108 case R_RISCV_BRANCH:
2109 CoffAddFixup(
2110 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2111 + (Rel->r_offset - SecShdr->sh_addr)),
2112 EFI_IMAGE_REL_BASED_ABSOLUTE);
2113 break;
2114
2115 case R_RISCV_JAL:
2116 CoffAddFixup(
2117 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2118 + (Rel->r_offset - SecShdr->sh_addr)),
2119 EFI_IMAGE_REL_BASED_ABSOLUTE);
2120 break;
2121
2122 case R_RISCV_GPREL_I:
2123 case R_RISCV_GPREL_S:
2124 case R_RISCV_CALL:
2125 case R_RISCV_CALL_PLT:
2126 case R_RISCV_RVC_BRANCH:
2127 case R_RISCV_RVC_JUMP:
2128 case R_RISCV_RELAX:
2129 case R_RISCV_SUB6:
2130 case R_RISCV_SET6:
2131 case R_RISCV_SET8:
2132 case R_RISCV_SET16:
2133 case R_RISCV_SET32:
2134 case R_RISCV_PCREL_HI20:
2135 case R_RISCV_GOT_HI20:
2136 case R_RISCV_PCREL_LO12_I:
2137 case R_RISCV_PCREL_LO12_S:
2138 break;
2139
2140 default:
2141 Error (NULL, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_RISCV64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
2142 }
2143 } else if (mEhdr->e_machine == EM_LOONGARCH) {
2144 switch (ELF_R_TYPE(Rel->r_info)) {
2145 case R_LARCH_MARK_LA:
2146 CoffAddFixup(
2147 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2148 + (Rel->r_offset - SecShdr->sh_addr)),
2149 EFI_IMAGE_REL_BASED_LOONGARCH64_MARK_LA);
2150 break;
2151 case R_LARCH_64:
2152 CoffAddFixup(
2153 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
2154 + (Rel->r_offset - SecShdr->sh_addr)),
2155 EFI_IMAGE_REL_BASED_DIR64);
2156 break;
2157 case R_LARCH_NONE:
2158 case R_LARCH_32:
2159 case R_LARCH_RELATIVE:
2160 case R_LARCH_COPY:
2161 case R_LARCH_JUMP_SLOT:
2162 case R_LARCH_TLS_DTPMOD32:
2163 case R_LARCH_TLS_DTPMOD64:
2164 case R_LARCH_TLS_DTPREL32:
2165 case R_LARCH_TLS_DTPREL64:
2166 case R_LARCH_TLS_TPREL32:
2167 case R_LARCH_TLS_TPREL64:
2168 case R_LARCH_IRELATIVE:
2169 case R_LARCH_MARK_PCREL:
2170 case R_LARCH_SOP_PUSH_PCREL:
2171 case R_LARCH_SOP_PUSH_ABSOLUTE:
2172 case R_LARCH_SOP_PUSH_DUP:
2173 case R_LARCH_SOP_PUSH_GPREL:
2174 case R_LARCH_SOP_PUSH_TLS_TPREL:
2175 case R_LARCH_SOP_PUSH_TLS_GOT:
2176 case R_LARCH_SOP_PUSH_TLS_GD:
2177 case R_LARCH_SOP_PUSH_PLT_PCREL:
2178 case R_LARCH_SOP_ASSERT:
2179 case R_LARCH_SOP_NOT:
2180 case R_LARCH_SOP_SUB:
2181 case R_LARCH_SOP_SL:
2182 case R_LARCH_SOP_SR:
2183 case R_LARCH_SOP_ADD:
2184 case R_LARCH_SOP_AND:
2185 case R_LARCH_SOP_IF_ELSE:
2186 case R_LARCH_SOP_POP_32_S_10_5:
2187 case R_LARCH_SOP_POP_32_U_10_12:
2188 case R_LARCH_SOP_POP_32_S_10_12:
2189 case R_LARCH_SOP_POP_32_S_10_16:
2190 case R_LARCH_SOP_POP_32_S_10_16_S2:
2191 case R_LARCH_SOP_POP_32_S_5_20:
2192 case R_LARCH_SOP_POP_32_S_0_5_10_16_S2:
2193 case R_LARCH_SOP_POP_32_S_0_10_10_16_S2:
2194 case R_LARCH_SOP_POP_32_U:
2195 case R_LARCH_ADD8:
2196 case R_LARCH_ADD16:
2197 case R_LARCH_ADD24:
2198 case R_LARCH_ADD32:
2199 case R_LARCH_ADD64:
2200 case R_LARCH_SUB8:
2201 case R_LARCH_SUB16:
2202 case R_LARCH_SUB24:
2203 case R_LARCH_SUB32:
2204 case R_LARCH_SUB64:
2205 case R_LARCH_GNU_VTINHERIT:
2206 case R_LARCH_GNU_VTENTRY:
2207 case R_LARCH_B16:
2208 case R_LARCH_B21:
2209 case R_LARCH_B26:
2210 case R_LARCH_ABS_HI20:
2211 case R_LARCH_ABS_LO12:
2212 case R_LARCH_ABS64_LO20:
2213 case R_LARCH_ABS64_HI12:
2214 case R_LARCH_PCALA_HI20:
2215 case R_LARCH_PCALA_LO12:
2216 case R_LARCH_PCALA64_LO20:
2217 case R_LARCH_PCALA64_HI12:
2218 case R_LARCH_GOT_PC_HI20:
2219 case R_LARCH_GOT_PC_LO12:
2220 case R_LARCH_GOT64_PC_LO20:
2221 case R_LARCH_GOT64_PC_HI12:
2222 case R_LARCH_GOT64_HI20:
2223 case R_LARCH_GOT64_LO12:
2224 case R_LARCH_GOT64_LO20:
2225 case R_LARCH_GOT64_HI12:
2226 case R_LARCH_TLS_LE_HI20:
2227 case R_LARCH_TLS_LE_LO12:
2228 case R_LARCH_TLS_LE64_LO20:
2229 case R_LARCH_TLS_LE64_HI12:
2230 case R_LARCH_TLS_IE_PC_HI20:
2231 case R_LARCH_TLS_IE_PC_LO12:
2232 case R_LARCH_TLS_IE64_PC_LO20:
2233 case R_LARCH_TLS_IE64_PC_HI12:
2234 case R_LARCH_TLS_IE64_HI20:
2235 case R_LARCH_TLS_IE64_LO12:
2236 case R_LARCH_TLS_IE64_LO20:
2237 case R_LARCH_TLS_IE64_HI12:
2238 case R_LARCH_TLS_LD_PC_HI20:
2239 case R_LARCH_TLS_LD64_HI20:
2240 case R_LARCH_TLS_GD_PC_HI20:
2241 case R_LARCH_TLS_GD64_HI20:
2242 case R_LARCH_32_PCREL:
2243 case R_LARCH_RELAX:
2244 case R_LARCH_DELETE:
2245 case R_LARCH_ALIGN:
2246 case R_LARCH_PCREL20_S2:
2247 case R_LARCH_CFA:
2248 case R_LARCH_ADD6:
2249 case R_LARCH_SUB6:
2250 case R_LARCH_ADD_ULEB128:
2251 case R_LARCH_SUB_ULEB128:
2252 case R_LARCH_64_PCREL:
2253 //
2254 // These types are not used or do not require fixup in PE format files.
2255 //
2256 break;
2257 default:
2258 Error (NULL, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_LOONGARCH relocation 0x%x.", mInImageName, (unsigned) ELF64_R_TYPE(Rel->r_info));
2259 }
2260 } else {
2261 Error (NULL, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr->e_machine);
2262 }
2263 }
2264 if (mEhdr->e_machine == EM_X86_64 && RelShdr->sh_info == mGOTShindex) {
2265 //
2266 // Tack relocations for GOT entries after other relocations for
2267 // the section the GOT is in, as it's usually found at the end
2268 // of the section. This is done in order to maintain Rva order
2269 // of Coff relocations.
2270 //
2271 EmitGOTRelocations();
2272 }
2273 }
2274 }
2275 }
2276
2277 if (mEhdr->e_machine == EM_X86_64) {
2278 //
2279 // This is a safety net just in case the GOT is in a section
2280 // with no other relocations and the first invocation of
2281 // EmitGOTRelocations() above was skipped. This invocation
2282 // does not maintain Rva order of Coff relocations.
2283 // At present, with a single text section, all references to
2284 // the GOT and the GOT itself reside in section .text, so
2285 // if there's a GOT at all, the first invocation above
2286 // is executed.
2287 //
2288 EmitGOTRelocations();
2289 }
2290 //
2291 // Pad by adding empty entries.
2292 //
2293 while (mCoffOffset & (mCoffAlignment - 1)) {
2294 CoffAddFixupEntry(0);
2295 }
2296
2297 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
2298 Dir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
2299 Dir->Size = mCoffOffset - mRelocOffset;
2300 if (Dir->Size == 0) {
2301 // If no relocations, null out the directory entry and don't add the .reloc section
2302 Dir->VirtualAddress = 0;
2303 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
2304 } else {
2305 Dir->VirtualAddress = mRelocOffset;
2306 CreateSectionHeader (".reloc", mRelocOffset, mCoffOffset - mRelocOffset,
2307 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
2308 | EFI_IMAGE_SCN_MEM_DISCARDABLE
2309 | EFI_IMAGE_SCN_MEM_READ);
2310 }
2311}
2312
2313STATIC
2314VOID
2315WriteDebug64 (
2316 VOID
2317 )
2318{
2319 UINT32 Len;
2320 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
2321 EFI_IMAGE_DATA_DIRECTORY *DataDir;
2322 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *Dir;
2323 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *Nb10;
2324 EFI_IMAGE_DEBUG_EX_DLLCHARACTERISTICS_ENTRY *DllEntry;
2325
2326 Len = strlen(mInImageName) + 1;
2327
2328 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
2329 DataDir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG];
2330 DataDir->VirtualAddress = mDebugOffset;
2331 DataDir->Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
2332
2333 Dir = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(mCoffFile + mDebugOffset);
2334
2335 if (mDllCharacteristicsEx != 0) {
2336 DataDir->Size += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
2337
2338 Dir->Type = EFI_IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS;
2339 Dir->SizeOfData = sizeof (EFI_IMAGE_DEBUG_EX_DLLCHARACTERISTICS_ENTRY);
2340 Dir->FileOffset = mDebugOffset + DataDir->Size +
2341 sizeof (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) +
2342 DebugRvaAlign(Len);
2343 Dir->RVA = Dir->FileOffset;
2344
2345 DllEntry = (VOID *)(mCoffFile + Dir->FileOffset);
2346
2347 DllEntry->DllCharacteristicsEx = mDllCharacteristicsEx;
2348
2349 Dir++;
2350 }
2351
2352 Dir->Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW;
2353 Dir->SizeOfData = sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + Len;
2354 Dir->RVA = mDebugOffset + DataDir->Size;
2355 Dir->FileOffset = mDebugOffset + DataDir->Size;
2356
2357 Nb10 = (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY*)(Dir + 1);
2358 Nb10->Signature = CODEVIEW_SIGNATURE_NB10;
2359 strcpy ((char *)(Nb10 + 1), mInImageName);
2360}
2361
2362STATIC
2363VOID
2364SetImageSize64 (
2365 VOID
2366 )
2367{
2368 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
2369
2370 //
2371 // Set image size
2372 //
2373 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
2374 NtHdr->Pe32Plus.OptionalHeader.SizeOfImage = mCoffOffset;
2375}
2376
2377STATIC
2378VOID
2379CleanUp64 (
2380 VOID
2381 )
2382{
2383 if (mCoffSectionsOffset != NULL) {
2384 free (mCoffSectionsOffset);
2385 }
2386}
2387
2388STATIC
2389VOID
2390WriteExport64 (
2391 VOID
2392 )
2393{
2394 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
2395 EFI_IMAGE_EXPORT_DIRECTORY *ExportDir;
2396 EFI_IMAGE_DATA_DIRECTORY *DataDir;
2397 UINT32 FileNameOffset;
2398 UINT32 NameOffset;
2399 UINT16 Index;
2400 UINT8 *Tdata = NULL;
2401
2402 ExportDir = (EFI_IMAGE_EXPORT_DIRECTORY*)(mCoffFile + mExportOffset);
2403 ExportDir->Characteristics = 0;
2404 ExportDir->TimeDateStamp = 0;
2405 ExportDir->MajorVersion = 0;
2406 ExportDir->MinorVersion =0;
2407 ExportDir->Name = 0;
2408 ExportDir->NumberOfFunctions = mExportSymNum;
2409 ExportDir->NumberOfNames = mExportSymNum;
2410 ExportDir->Base = EFI_IMAGE_EXPORT_ORDINAL_BASE;
2411 ExportDir->AddressOfFunctions = mExportOffset + sizeof(EFI_IMAGE_EXPORT_DIRECTORY);
2412 ExportDir->AddressOfNames = ExportDir->AddressOfFunctions + EFI_IMAGE_EXPORT_ADDR_SIZE * mExportSymNum;
2413 ExportDir->AddressOfNameOrdinals = ExportDir->AddressOfNames + EFI_IMAGE_EXPORT_ADDR_SIZE * mExportSymNum;
2414
2415 FileNameOffset = ExportDir->AddressOfNameOrdinals + EFI_IMAGE_EXPORT_ORDINAL_SIZE * mExportSymNum;
2416 NameOffset = FileNameOffset + strlen(mInImageName) + 1;
2417
2418 // Write Input image Name RVA
2419 ExportDir->Name = FileNameOffset;
2420
2421 // Write Input image Name
2422 strcpy((char *)(mCoffFile + FileNameOffset), mInImageName);
2423
2424 for (Index = 0; Index < mExportSymNum; Index++) {
2425 //
2426 // Write Export Address Table
2427 //
2428 Tdata = mCoffFile + ExportDir->AddressOfFunctions + Index * EFI_IMAGE_EXPORT_ADDR_SIZE;
2429 *(UINT32 *)Tdata = mExportRVA[Index];
2430
2431 //
2432 // Write Export Name Pointer Table
2433 //
2434 Tdata = mCoffFile + ExportDir->AddressOfNames + Index * EFI_IMAGE_EXPORT_ADDR_SIZE;
2435 *(UINT32 *)Tdata = NameOffset;
2436
2437 //
2438 // Write Export Ordinal table
2439 //
2440 Tdata = mCoffFile + ExportDir->AddressOfNameOrdinals + Index * EFI_IMAGE_EXPORT_ORDINAL_SIZE;
2441 *(UINT16 *)Tdata = Index;
2442
2443 //
2444 // Write Export Name Table
2445 //
2446 strcpy((char *)(mCoffFile + NameOffset), mExportSymName[Index]);
2447 NameOffset += strlen(mExportSymName[Index]) + 1;
2448 }
2449
2450 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
2451 DataDir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT];
2452 DataDir->VirtualAddress = mExportOffset;
2453 DataDir->Size = mExportSize;
2454
2455}
2456
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette