]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/GenFw/Elf64Convert.c
BaseTools GenFw: Add support for R_RISCV_PCREL_LO12_S relocation
[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-2014, ARM Ltd. All rights reserved.<BR>
6 Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
7
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
12 #include "WinNtInclude.h"
13
14 #ifndef __GNUC__
15 #include <windows.h>
16 #include <io.h>
17 #endif
18 #include <assert.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <time.h>
23 #include <ctype.h>
24
25 #include <Common/UefiBaseTypes.h>
26 #include <IndustryStandard/PeImage.h>
27
28 #include "PeCoffLib.h"
29 #include "EfiUtilityMsgs.h"
30
31 #include "GenFw.h"
32 #include "ElfConvert.h"
33 #include "Elf64Convert.h"
34
35 STATIC
36 VOID
37 ScanSections64 (
38 VOID
39 );
40
41 STATIC
42 BOOLEAN
43 WriteSections64 (
44 SECTION_FILTER_TYPES FilterType
45 );
46
47 STATIC
48 VOID
49 WriteRelocations64 (
50 VOID
51 );
52
53 STATIC
54 VOID
55 WriteDebug64 (
56 VOID
57 );
58
59 STATIC
60 VOID
61 SetImageSize64 (
62 VOID
63 );
64
65 STATIC
66 VOID
67 CleanUp64 (
68 VOID
69 );
70
71 //
72 // Rename ELF32 structures to common names to help when porting to ELF64.
73 //
74 typedef Elf64_Shdr Elf_Shdr;
75 typedef Elf64_Ehdr Elf_Ehdr;
76 typedef Elf64_Rel Elf_Rel;
77 typedef Elf64_Rela Elf_Rela;
78 typedef Elf64_Sym Elf_Sym;
79 typedef Elf64_Phdr Elf_Phdr;
80 typedef Elf64_Dyn Elf_Dyn;
81 #define ELFCLASS ELFCLASS64
82 #define ELF_R_TYPE(r) ELF64_R_TYPE(r)
83 #define ELF_R_SYM(r) ELF64_R_SYM(r)
84
85 //
86 // Well known ELF structures.
87 //
88 STATIC Elf_Ehdr *mEhdr;
89 STATIC Elf_Shdr *mShdrBase;
90 STATIC Elf_Phdr *mPhdrBase;
91
92 //
93 // GOT information
94 //
95 STATIC Elf_Shdr *mGOTShdr = NULL;
96 STATIC UINT32 mGOTShindex = 0;
97 STATIC UINT32 *mGOTCoffEntries = NULL;
98 STATIC UINT32 mGOTMaxCoffEntries = 0;
99 STATIC UINT32 mGOTNumCoffEntries = 0;
100
101 //
102 // Coff information
103 //
104 STATIC UINT32 mCoffAlignment = 0x20;
105
106 //
107 // PE section alignment.
108 //
109 STATIC const UINT16 mCoffNbrSections = 4;
110
111 //
112 // ELF sections to offset in Coff file.
113 //
114 STATIC UINT32 *mCoffSectionsOffset = NULL;
115
116 //
117 // Offsets in COFF file
118 //
119 STATIC UINT32 mNtHdrOffset;
120 STATIC UINT32 mTextOffset;
121 STATIC UINT32 mDataOffset;
122 STATIC UINT32 mHiiRsrcOffset;
123 STATIC UINT32 mRelocOffset;
124 STATIC UINT32 mDebugOffset;
125
126 //
127 // Used for RISC-V relocations.
128 //
129 STATIC UINT8 *mRiscVPass1Targ = NULL;
130 STATIC Elf_Shdr *mRiscVPass1Sym = NULL;
131 STATIC Elf64_Half mRiscVPass1SymSecIndex = 0;
132 STATIC INT32 mRiscVPass1Offset;
133 STATIC INT32 mRiscVPass1GotFixup;
134
135 //
136 // Initialization Function
137 //
138 BOOLEAN
139 InitializeElf64 (
140 UINT8 *FileBuffer,
141 ELF_FUNCTION_TABLE *ElfFunctions
142 )
143 {
144 //
145 // Initialize data pointer and structures.
146 //
147 VerboseMsg ("Set EHDR");
148 mEhdr = (Elf_Ehdr*) FileBuffer;
149
150 //
151 // Check the ELF64 specific header information.
152 //
153 VerboseMsg ("Check ELF64 Header Information");
154 if (mEhdr->e_ident[EI_CLASS] != ELFCLASS64) {
155 Error (NULL, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS64");
156 return FALSE;
157 }
158 if (mEhdr->e_ident[EI_DATA] != ELFDATA2LSB) {
159 Error (NULL, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
160 return FALSE;
161 }
162 if ((mEhdr->e_type != ET_EXEC) && (mEhdr->e_type != ET_DYN)) {
163 Error (NULL, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
164 return FALSE;
165 }
166 if (!((mEhdr->e_machine == EM_X86_64) || (mEhdr->e_machine == EM_AARCH64) || (mEhdr->e_machine == EM_RISCV64))) {
167 Warning (NULL, 0, 3000, "Unsupported", "ELF e_machine is not Elf64 machine.");
168 }
169 if (mEhdr->e_version != EV_CURRENT) {
170 Error (NULL, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr->e_version, EV_CURRENT);
171 return FALSE;
172 }
173
174 //
175 // Update section header pointers
176 //
177 VerboseMsg ("Update Header Pointers");
178 mShdrBase = (Elf_Shdr *)((UINT8 *)mEhdr + mEhdr->e_shoff);
179 mPhdrBase = (Elf_Phdr *)((UINT8 *)mEhdr + mEhdr->e_phoff);
180
181 //
182 // Create COFF Section offset buffer and zero.
183 //
184 VerboseMsg ("Create COFF Section Offset Buffer");
185 mCoffSectionsOffset = (UINT32 *)malloc(mEhdr->e_shnum * sizeof (UINT32));
186 if (mCoffSectionsOffset == NULL) {
187 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
188 return FALSE;
189 }
190 memset(mCoffSectionsOffset, 0, mEhdr->e_shnum * sizeof(UINT32));
191
192 //
193 // Fill in function pointers.
194 //
195 VerboseMsg ("Fill in Function Pointers");
196 ElfFunctions->ScanSections = ScanSections64;
197 ElfFunctions->WriteSections = WriteSections64;
198 ElfFunctions->WriteRelocations = WriteRelocations64;
199 ElfFunctions->WriteDebug = WriteDebug64;
200 ElfFunctions->SetImageSize = SetImageSize64;
201 ElfFunctions->CleanUp = CleanUp64;
202
203 return TRUE;
204 }
205
206
207 //
208 // Header by Index functions
209 //
210 STATIC
211 Elf_Shdr*
212 GetShdrByIndex (
213 UINT32 Num
214 )
215 {
216 if (Num >= mEhdr->e_shnum) {
217 Error (NULL, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num);
218 exit(EXIT_FAILURE);
219 }
220
221 return (Elf_Shdr*)((UINT8*)mShdrBase + Num * mEhdr->e_shentsize);
222 }
223
224 STATIC
225 UINT32
226 CoffAlign (
227 UINT32 Offset
228 )
229 {
230 return (Offset + mCoffAlignment - 1) & ~(mCoffAlignment - 1);
231 }
232
233 STATIC
234 UINT32
235 DebugRvaAlign (
236 UINT32 Offset
237 )
238 {
239 return (Offset + 3) & ~3;
240 }
241
242 //
243 // filter functions
244 //
245 STATIC
246 BOOLEAN
247 IsTextShdr (
248 Elf_Shdr *Shdr
249 )
250 {
251 return (BOOLEAN) (((Shdr->sh_flags & (SHF_EXECINSTR | SHF_ALLOC)) == (SHF_EXECINSTR | SHF_ALLOC)) ||
252 ((Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == SHF_ALLOC));
253 }
254
255 STATIC
256 BOOLEAN
257 IsHiiRsrcShdr (
258 Elf_Shdr *Shdr
259 )
260 {
261 Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx);
262
263 return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_HII_SECTION_NAME) == 0);
264 }
265
266 STATIC
267 BOOLEAN
268 IsDataShdr (
269 Elf_Shdr *Shdr
270 )
271 {
272 if (IsHiiRsrcShdr(Shdr)) {
273 return FALSE;
274 }
275 return (BOOLEAN) (Shdr->sh_flags & (SHF_EXECINSTR | SHF_WRITE | SHF_ALLOC)) == (SHF_ALLOC | SHF_WRITE);
276 }
277
278 STATIC
279 BOOLEAN
280 IsStrtabShdr (
281 Elf_Shdr *Shdr
282 )
283 {
284 Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx);
285
286 return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_STRTAB_SECTION_NAME) == 0);
287 }
288
289 STATIC
290 Elf_Shdr *
291 FindStrtabShdr (
292 VOID
293 )
294 {
295 UINT32 i;
296 for (i = 0; i < mEhdr->e_shnum; i++) {
297 Elf_Shdr *shdr = GetShdrByIndex(i);
298 if (IsStrtabShdr(shdr)) {
299 return shdr;
300 }
301 }
302 return NULL;
303 }
304
305 STATIC
306 const UINT8 *
307 GetSymName (
308 Elf_Sym *Sym
309 )
310 {
311 Elf_Shdr *StrtabShdr;
312 UINT8 *StrtabContents;
313 BOOLEAN foundEnd;
314 UINT32 i;
315
316 if (Sym->st_name == 0) {
317 return NULL;
318 }
319
320 StrtabShdr = FindStrtabShdr();
321 if (StrtabShdr == NULL) {
322 return NULL;
323 }
324
325 assert(Sym->st_name < StrtabShdr->sh_size);
326
327 StrtabContents = (UINT8*)mEhdr + StrtabShdr->sh_offset;
328
329 foundEnd = FALSE;
330 for (i= Sym->st_name; (i < StrtabShdr->sh_size) && !foundEnd; i++) {
331 foundEnd = (BOOLEAN)(StrtabContents[i] == 0);
332 }
333 assert(foundEnd);
334
335 return StrtabContents + Sym->st_name;
336 }
337
338 //
339 // Find the ELF section hosting the GOT from an ELF Rva
340 // of a single GOT entry. Normally, GOT is placed in
341 // ELF .text section, so assume once we find in which
342 // section the GOT is, all GOT entries are there, and
343 // just verify this.
344 //
345 STATIC
346 VOID
347 FindElfGOTSectionFromGOTEntryElfRva (
348 Elf64_Addr GOTEntryElfRva
349 )
350 {
351 UINT32 i;
352 if (mGOTShdr != NULL) {
353 if (GOTEntryElfRva >= mGOTShdr->sh_addr &&
354 GOTEntryElfRva < mGOTShdr->sh_addr + mGOTShdr->sh_size) {
355 return;
356 }
357 Error (NULL, 0, 3000, "Unsupported", "FindElfGOTSectionFromGOTEntryElfRva: GOT entries found in multiple sections.");
358 exit(EXIT_FAILURE);
359 }
360 for (i = 0; i < mEhdr->e_shnum; i++) {
361 Elf_Shdr *shdr = GetShdrByIndex(i);
362 if (GOTEntryElfRva >= shdr->sh_addr &&
363 GOTEntryElfRva < shdr->sh_addr + shdr->sh_size) {
364 mGOTShdr = shdr;
365 mGOTShindex = i;
366 return;
367 }
368 }
369 Error (NULL, 0, 3000, "Invalid", "FindElfGOTSectionFromGOTEntryElfRva: ElfRva 0x%016LX for GOT entry not found in any section.", GOTEntryElfRva);
370 exit(EXIT_FAILURE);
371 }
372
373 //
374 // Stores locations of GOT entries in COFF image.
375 // Returns TRUE if GOT entry is new.
376 // Simple implementation as number of GOT
377 // entries is expected to be low.
378 //
379
380 STATIC
381 BOOLEAN
382 AccumulateCoffGOTEntries (
383 UINT32 GOTCoffEntry
384 )
385 {
386 UINT32 i;
387 if (mGOTCoffEntries != NULL) {
388 for (i = 0; i < mGOTNumCoffEntries; i++) {
389 if (mGOTCoffEntries[i] == GOTCoffEntry) {
390 return FALSE;
391 }
392 }
393 }
394 if (mGOTCoffEntries == NULL) {
395 mGOTCoffEntries = (UINT32*)malloc(5 * sizeof *mGOTCoffEntries);
396 if (mGOTCoffEntries == NULL) {
397 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
398 }
399 assert (mGOTCoffEntries != NULL);
400 mGOTMaxCoffEntries = 5;
401 mGOTNumCoffEntries = 0;
402 } else if (mGOTNumCoffEntries == mGOTMaxCoffEntries) {
403 mGOTCoffEntries = (UINT32*)realloc(mGOTCoffEntries, 2 * mGOTMaxCoffEntries * sizeof *mGOTCoffEntries);
404 if (mGOTCoffEntries == NULL) {
405 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
406 }
407 assert (mGOTCoffEntries != NULL);
408 mGOTMaxCoffEntries += mGOTMaxCoffEntries;
409 }
410 mGOTCoffEntries[mGOTNumCoffEntries++] = GOTCoffEntry;
411 return TRUE;
412 }
413
414 //
415 // 32-bit Unsigned integer comparator for qsort.
416 //
417 STATIC
418 int
419 UINT32Comparator (
420 const void* lhs,
421 const void* rhs
422 )
423 {
424 if (*(const UINT32*)lhs < *(const UINT32*)rhs) {
425 return -1;
426 }
427 return *(const UINT32*)lhs > *(const UINT32*)rhs;
428 }
429
430 //
431 // Emit accumulated Coff GOT entry relocations into
432 // Coff image. This function performs its job
433 // once and then releases the entry list, so
434 // it can safely be called multiple times.
435 //
436 STATIC
437 VOID
438 EmitGOTRelocations (
439 VOID
440 )
441 {
442 UINT32 i;
443 if (mGOTCoffEntries == NULL) {
444 return;
445 }
446 //
447 // Emit Coff relocations with Rvas ordered.
448 //
449 qsort(
450 mGOTCoffEntries,
451 mGOTNumCoffEntries,
452 sizeof *mGOTCoffEntries,
453 UINT32Comparator);
454 for (i = 0; i < mGOTNumCoffEntries; i++) {
455 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X", mGOTCoffEntries[i]);
456 CoffAddFixup(
457 mGOTCoffEntries[i],
458 EFI_IMAGE_REL_BASED_DIR64);
459 }
460 free(mGOTCoffEntries);
461 mGOTCoffEntries = NULL;
462 mGOTMaxCoffEntries = 0;
463 mGOTNumCoffEntries = 0;
464 }
465 //
466 // RISC-V 64 specific Elf WriteSection function.
467 //
468 STATIC
469 VOID
470 WriteSectionRiscV64 (
471 Elf_Rela *Rel,
472 UINT8 *Targ,
473 Elf_Shdr *SymShdr,
474 Elf_Sym *Sym
475 )
476 {
477 UINT32 Value;
478 UINT32 Value2;
479 Elf64_Addr GOTEntryRva;
480
481 switch (ELF_R_TYPE(Rel->r_info)) {
482 case R_RISCV_NONE:
483 break;
484
485 case R_RISCV_32:
486 *(UINT64 *)Targ = Sym->st_value + Rel->r_addend;
487 break;
488
489 case R_RISCV_64:
490 *(UINT64 *)Targ = Sym->st_value + Rel->r_addend;
491 break;
492
493 case R_RISCV_HI20:
494 mRiscVPass1Targ = Targ;
495 mRiscVPass1Sym = SymShdr;
496 mRiscVPass1SymSecIndex = Sym->st_shndx;
497 break;
498
499 case R_RISCV_LO12_I:
500 if (mRiscVPass1Sym == SymShdr && mRiscVPass1Targ != NULL && mRiscVPass1SymSecIndex == Sym->st_shndx && mRiscVPass1SymSecIndex != 0) {
501 Value = (UINT32)(RV_X(*(UINT32 *)mRiscVPass1Targ, 12, 20) << 12);
502 Value2 = (UINT32)(RV_X(*(UINT32 *)Targ, 20, 12));
503 if (Value2 & (RISCV_IMM_REACH/2)) {
504 Value2 |= ~(RISCV_IMM_REACH-1);
505 }
506 Value += Value2;
507 Value = Value - (UINT32)SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
508 Value2 = RISCV_CONST_HIGH_PART (Value);
509 *(UINT32 *)mRiscVPass1Targ = (RV_X (Value2, 12, 20) << 12) | \
510 (RV_X (*(UINT32 *)mRiscVPass1Targ, 0, 12));
511 *(UINT32 *)Targ = (RV_X (Value, 0, 12) << 20) | \
512 (RV_X (*(UINT32 *)Targ, 0, 20));
513 }
514 mRiscVPass1Sym = NULL;
515 mRiscVPass1Targ = NULL;
516 mRiscVPass1SymSecIndex = 0;
517 break;
518
519 case R_RISCV_LO12_S:
520 if (mRiscVPass1Sym == SymShdr && mRiscVPass1Targ != NULL && mRiscVPass1SymSecIndex == Sym->st_shndx && mRiscVPass1SymSecIndex != 0) {
521 Value = (UINT32)(RV_X(*(UINT32 *)mRiscVPass1Targ, 12, 20) << 12);
522 Value2 = (UINT32)(RV_X(*(UINT32 *)Targ, 7, 5) | (RV_X(*(UINT32 *)Targ, 25, 7) << 5));
523 if (Value2 & (RISCV_IMM_REACH/2)) {
524 Value2 |= ~(RISCV_IMM_REACH-1);
525 }
526 Value += Value2;
527 Value = Value - (UINT32)SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
528 Value2 = RISCV_CONST_HIGH_PART (Value);
529 *(UINT32 *)mRiscVPass1Targ = (RV_X (Value2, 12, 20) << 12) | \
530 (RV_X (*(UINT32 *)mRiscVPass1Targ, 0, 12));
531 Value2 = *(UINT32 *)Targ & 0x01fff07f;
532 Value &= RISCV_IMM_REACH - 1;
533 *(UINT32 *)Targ = Value2 | (UINT32)(((RV_X(Value, 0, 5) << 7) | (RV_X(Value, 5, 7) << 25)));
534 }
535 mRiscVPass1Sym = NULL;
536 mRiscVPass1Targ = NULL;
537 mRiscVPass1SymSecIndex = 0;
538 break;
539
540 case R_RISCV_GOT_HI20:
541 GOTEntryRva = (Sym->st_value - Rel->r_offset);
542 mRiscVPass1Offset = RV_X(GOTEntryRva, 0, 12);
543 Value = (UINT32)RV_X(GOTEntryRva, 12, 20);
544 *(UINT32 *)Targ = (Value << 12) | (RV_X(*(UINT32*)Targ, 0, 12));
545
546 mRiscVPass1Targ = Targ;
547 mRiscVPass1Sym = SymShdr;
548 mRiscVPass1SymSecIndex = Sym->st_shndx;
549 mRiscVPass1GotFixup = 1;
550 break;
551
552 case R_RISCV_PCREL_HI20:
553 mRiscVPass1Targ = Targ;
554 mRiscVPass1Sym = SymShdr;
555 mRiscVPass1SymSecIndex = Sym->st_shndx;
556
557 Value = (UINT32)(RV_X(*(UINT32 *)mRiscVPass1Targ, 12, 20));
558 break;
559
560 case R_RISCV_PCREL_LO12_S:
561 if (mRiscVPass1Targ != NULL && mRiscVPass1Sym != NULL && mRiscVPass1SymSecIndex != 0) {
562 int i;
563 Value2 = (UINT32)(RV_X(*(UINT32 *)mRiscVPass1Targ, 12, 20));
564
565 Value = ((UINT32)(RV_X(*(UINT32 *)Targ, 25, 7)) << 5);
566 Value = (Value | (UINT32)(RV_X(*(UINT32 *)Targ, 7, 5)));
567
568 if(Value & (RISCV_IMM_REACH/2)) {
569 Value |= ~(RISCV_IMM_REACH-1);
570 }
571 Value = Value - (UINT32)mRiscVPass1Sym->sh_addr + mCoffSectionsOffset[mRiscVPass1SymSecIndex];
572
573 if(-2048 > (INT32)Value) {
574 i = (((INT32)Value * -1) / 4096);
575 Value2 -= i;
576 Value += 4096 * i;
577 if(-2048 > (INT32)Value) {
578 Value2 -= 1;
579 Value += 4096;
580 }
581 }
582 else if( 2047 < (INT32)Value) {
583 i = (Value / 4096);
584 Value2 += i;
585 Value -= 4096 * i;
586 if(2047 < (INT32)Value) {
587 Value2 += 1;
588 Value -= 4096;
589 }
590 }
591
592 // Update the IMM of SD instruction
593 //
594 // |31 25|24 20|19 15|14 12 |11 7|6 0|
595 // |-------------------------------------------|-------|
596 // |imm[11:5] | rs2 | rs1 | funct3 |imm[4:0] | opcode|
597 // ---------------------------------------------------
598
599 // First Zero out current IMM
600 *(UINT32 *)Targ &= ~0xfe000f80;
601
602 // Update with new IMM
603 *(UINT32 *)Targ |= (RV_X(Value, 5, 7) << 25);
604 *(UINT32 *)Targ |= (RV_X(Value, 0, 5) << 7);
605
606 // Update previous instruction
607 *(UINT32 *)mRiscVPass1Targ = (RV_X(Value2, 0, 20)<<12) | (RV_X(*(UINT32 *)mRiscVPass1Targ, 0, 12));
608 }
609 mRiscVPass1Sym = NULL;
610 mRiscVPass1Targ = NULL;
611 mRiscVPass1SymSecIndex = 0;
612 break;
613
614 case R_RISCV_PCREL_LO12_I:
615 if (mRiscVPass1Targ != NULL && mRiscVPass1Sym != NULL && mRiscVPass1SymSecIndex != 0) {
616 int i;
617 Value2 = (UINT32)(RV_X(*(UINT32 *)mRiscVPass1Targ, 12, 20));
618
619 if(mRiscVPass1GotFixup) {
620 Value = (UINT32)(mRiscVPass1Offset);
621 } else {
622 Value = (UINT32)(RV_X(*(UINT32 *)Targ, 20, 12));
623 if(Value & (RISCV_IMM_REACH/2)) {
624 Value |= ~(RISCV_IMM_REACH-1);
625 }
626 }
627 Value = Value - (UINT32)mRiscVPass1Sym->sh_addr + mCoffSectionsOffset[mRiscVPass1SymSecIndex];
628
629 if(-2048 > (INT32)Value) {
630 i = (((INT32)Value * -1) / 4096);
631 Value2 -= i;
632 Value += 4096 * i;
633 if(-2048 > (INT32)Value) {
634 Value2 -= 1;
635 Value += 4096;
636 }
637 }
638 else if( 2047 < (INT32)Value) {
639 i = (Value / 4096);
640 Value2 += i;
641 Value -= 4096 * i;
642 if(2047 < (INT32)Value) {
643 Value2 += 1;
644 Value -= 4096;
645 }
646 }
647
648 if(mRiscVPass1GotFixup) {
649 *(UINT32 *)Targ = (RV_X((UINT32)Value, 0, 12) << 20)
650 | (RV_X(*(UINT32*)Targ, 0, 20));
651 // Convert LD instruction to ADDI
652 //
653 // |31 20|19 15|14 12|11 7|6 0|
654 // |-----------------------------------------|
655 // |imm[11:0] | rs1 | 011 | rd | 0000011 | LD
656 // -----------------------------------------
657
658 // |-----------------------------------------|
659 // |imm[11:0] | rs1 | 000 | rd | 0010011 | ADDI
660 // -----------------------------------------
661
662 // To convert, let's first reset bits 12-14 and 0-6 using ~0x707f
663 // Then modify the opcode to ADDI (0010011)
664 // All other fields will remain same.
665
666 *(UINT32 *)Targ = ((*(UINT32 *)Targ & ~0x707f) | 0x13);
667 } else {
668 *(UINT32 *)Targ = (RV_X(Value, 0, 12) << 20) | (RV_X(*(UINT32*)Targ, 0, 20));
669 }
670 *(UINT32 *)mRiscVPass1Targ = (RV_X(Value2, 0, 20)<<12) | (RV_X(*(UINT32 *)mRiscVPass1Targ, 0, 12));
671 }
672 mRiscVPass1Sym = NULL;
673 mRiscVPass1Targ = NULL;
674 mRiscVPass1SymSecIndex = 0;
675 mRiscVPass1Offset = 0;
676 mRiscVPass1GotFixup = 0;
677 break;
678
679 case R_RISCV_ADD64:
680 case R_RISCV_SUB64:
681 case R_RISCV_ADD32:
682 case R_RISCV_SUB32:
683 case R_RISCV_BRANCH:
684 case R_RISCV_JAL:
685 case R_RISCV_GPREL_I:
686 case R_RISCV_GPREL_S:
687 case R_RISCV_CALL:
688 case R_RISCV_CALL_PLT:
689 case R_RISCV_RVC_BRANCH:
690 case R_RISCV_RVC_JUMP:
691 case R_RISCV_RELAX:
692 case R_RISCV_SUB6:
693 case R_RISCV_SET6:
694 case R_RISCV_SET8:
695 case R_RISCV_SET16:
696 case R_RISCV_SET32:
697 break;
698
699 default:
700 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_RISCV64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
701 }
702 }
703
704 //
705 // Elf functions interface implementation
706 //
707
708 STATIC
709 VOID
710 ScanSections64 (
711 VOID
712 )
713 {
714 UINT32 i;
715 EFI_IMAGE_DOS_HEADER *DosHdr;
716 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
717 UINT32 CoffEntry;
718 UINT32 SectionCount;
719 BOOLEAN FoundSection;
720
721 CoffEntry = 0;
722 mCoffOffset = 0;
723
724 //
725 // Coff file start with a DOS header.
726 //
727 mCoffOffset = sizeof(EFI_IMAGE_DOS_HEADER) + 0x40;
728 mNtHdrOffset = mCoffOffset;
729 switch (mEhdr->e_machine) {
730 case EM_X86_64:
731 case EM_AARCH64:
732 case EM_RISCV64:
733 mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS64);
734 break;
735 default:
736 VerboseMsg ("%s unknown e_machine type %hu. Assume X64", mInImageName, mEhdr->e_machine);
737 mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS64);
738 break;
739 }
740
741 mTableOffset = mCoffOffset;
742 mCoffOffset += mCoffNbrSections * sizeof(EFI_IMAGE_SECTION_HEADER);
743
744 //
745 // Set mCoffAlignment to the maximum alignment of the input sections
746 // we care about
747 //
748 for (i = 0; i < mEhdr->e_shnum; i++) {
749 Elf_Shdr *shdr = GetShdrByIndex(i);
750 if (shdr->sh_addralign <= mCoffAlignment) {
751 continue;
752 }
753 if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr)) {
754 mCoffAlignment = (UINT32)shdr->sh_addralign;
755 }
756 }
757
758 //
759 // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT
760 //
761 if (mCoffAlignment > MAX_COFF_ALIGNMENT) {
762 Error (NULL, 0, 3000, "Invalid", "Section alignment is larger than MAX_COFF_ALIGNMENT.");
763 assert (FALSE);
764 }
765
766
767 //
768 // Move the PE/COFF header right before the first section. This will help us
769 // save space when converting to TE.
770 //
771 if (mCoffAlignment > mCoffOffset) {
772 mNtHdrOffset += mCoffAlignment - mCoffOffset;
773 mTableOffset += mCoffAlignment - mCoffOffset;
774 mCoffOffset = mCoffAlignment;
775 }
776
777 //
778 // First text sections.
779 //
780 mCoffOffset = CoffAlign(mCoffOffset);
781 mTextOffset = mCoffOffset;
782 FoundSection = FALSE;
783 SectionCount = 0;
784 for (i = 0; i < mEhdr->e_shnum; i++) {
785 Elf_Shdr *shdr = GetShdrByIndex(i);
786 if (IsTextShdr(shdr)) {
787 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
788 // the alignment field is valid
789 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
790 // if the section address is aligned we must align PE/COFF
791 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));
792 } else {
793 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
794 }
795 }
796
797 /* Relocate entry. */
798 if ((mEhdr->e_entry >= shdr->sh_addr) &&
799 (mEhdr->e_entry < shdr->sh_addr + shdr->sh_size)) {
800 CoffEntry = (UINT32) (mCoffOffset + mEhdr->e_entry - shdr->sh_addr);
801 }
802
803 //
804 // Set mTextOffset with the offset of the first '.text' section
805 //
806 if (!FoundSection) {
807 mTextOffset = mCoffOffset;
808 FoundSection = TRUE;
809 }
810
811 mCoffSectionsOffset[i] = mCoffOffset;
812 mCoffOffset += (UINT32) shdr->sh_size;
813 SectionCount ++;
814 }
815 }
816
817 if (!FoundSection && mOutImageType != FW_ACPI_IMAGE) {
818 Error (NULL, 0, 3000, "Invalid", "Did not find any '.text' section.");
819 assert (FALSE);
820 }
821
822 mDebugOffset = DebugRvaAlign(mCoffOffset);
823 mCoffOffset = CoffAlign(mCoffOffset);
824
825 if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {
826 Warning (NULL, 0, 0, NULL, "Multiple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName);
827 }
828
829 //
830 // Then data sections.
831 //
832 mDataOffset = mCoffOffset;
833 FoundSection = FALSE;
834 SectionCount = 0;
835 for (i = 0; i < mEhdr->e_shnum; i++) {
836 Elf_Shdr *shdr = GetShdrByIndex(i);
837 if (IsDataShdr(shdr)) {
838 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
839 // the alignment field is valid
840 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
841 // if the section address is aligned we must align PE/COFF
842 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));
843 } else {
844 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
845 }
846 }
847
848 //
849 // Set mDataOffset with the offset of the first '.data' section
850 //
851 if (!FoundSection) {
852 mDataOffset = mCoffOffset;
853 FoundSection = TRUE;
854 }
855 mCoffSectionsOffset[i] = mCoffOffset;
856 mCoffOffset += (UINT32) shdr->sh_size;
857 SectionCount ++;
858 }
859 }
860
861 //
862 // Make room for .debug data in .data (or .text if .data is empty) instead of
863 // putting it in a section of its own. This is explicitly allowed by the
864 // PE/COFF spec, and prevents bloat in the binary when using large values for
865 // section alignment.
866 //
867 if (SectionCount > 0) {
868 mDebugOffset = DebugRvaAlign(mCoffOffset);
869 }
870 mCoffOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY) +
871 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) +
872 strlen(mInImageName) + 1;
873
874 mCoffOffset = CoffAlign(mCoffOffset);
875 if (SectionCount == 0) {
876 mDataOffset = mCoffOffset;
877 }
878
879 if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {
880 Warning (NULL, 0, 0, NULL, "Multiple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName);
881 }
882
883 //
884 // The HII resource sections.
885 //
886 mHiiRsrcOffset = mCoffOffset;
887 for (i = 0; i < mEhdr->e_shnum; i++) {
888 Elf_Shdr *shdr = GetShdrByIndex(i);
889 if (IsHiiRsrcShdr(shdr)) {
890 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
891 // the alignment field is valid
892 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
893 // if the section address is aligned we must align PE/COFF
894 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));
895 } else {
896 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");
897 }
898 }
899 if (shdr->sh_size != 0) {
900 mHiiRsrcOffset = mCoffOffset;
901 mCoffSectionsOffset[i] = mCoffOffset;
902 mCoffOffset += (UINT32) shdr->sh_size;
903 mCoffOffset = CoffAlign(mCoffOffset);
904 SetHiiResourceHeader ((UINT8*) mEhdr + shdr->sh_offset, mHiiRsrcOffset);
905 }
906 break;
907 }
908 }
909
910 mRelocOffset = mCoffOffset;
911
912 //
913 // Allocate base Coff file. Will be expanded later for relocations.
914 //
915 mCoffFile = (UINT8 *)malloc(mCoffOffset);
916 if (mCoffFile == NULL) {
917 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
918 }
919 assert (mCoffFile != NULL);
920 memset(mCoffFile, 0, mCoffOffset);
921
922 //
923 // Fill headers.
924 //
925 DosHdr = (EFI_IMAGE_DOS_HEADER *)mCoffFile;
926 DosHdr->e_magic = EFI_IMAGE_DOS_SIGNATURE;
927 DosHdr->e_lfanew = mNtHdrOffset;
928
929 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION*)(mCoffFile + mNtHdrOffset);
930
931 NtHdr->Pe32Plus.Signature = EFI_IMAGE_NT_SIGNATURE;
932
933 switch (mEhdr->e_machine) {
934 case EM_X86_64:
935 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_X64;
936 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
937 break;
938 case EM_AARCH64:
939 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_AARCH64;
940 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
941 break;
942 case EM_RISCV64:
943 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_RISCV64;
944 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
945 break;
946
947 default:
948 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN)mEhdr->e_machine);
949 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_X64;
950 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
951 }
952
953 NtHdr->Pe32Plus.FileHeader.NumberOfSections = mCoffNbrSections;
954 NtHdr->Pe32Plus.FileHeader.TimeDateStamp = (UINT32) time(NULL);
955 mImageTimeStamp = NtHdr->Pe32Plus.FileHeader.TimeDateStamp;
956 NtHdr->Pe32Plus.FileHeader.PointerToSymbolTable = 0;
957 NtHdr->Pe32Plus.FileHeader.NumberOfSymbols = 0;
958 NtHdr->Pe32Plus.FileHeader.SizeOfOptionalHeader = sizeof(NtHdr->Pe32Plus.OptionalHeader);
959 NtHdr->Pe32Plus.FileHeader.Characteristics = EFI_IMAGE_FILE_EXECUTABLE_IMAGE
960 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
961 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
962 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE;
963
964 NtHdr->Pe32Plus.OptionalHeader.SizeOfCode = mDataOffset - mTextOffset;
965 NtHdr->Pe32Plus.OptionalHeader.SizeOfInitializedData = mRelocOffset - mDataOffset;
966 NtHdr->Pe32Plus.OptionalHeader.SizeOfUninitializedData = 0;
967 NtHdr->Pe32Plus.OptionalHeader.AddressOfEntryPoint = CoffEntry;
968
969 NtHdr->Pe32Plus.OptionalHeader.BaseOfCode = mTextOffset;
970
971 NtHdr->Pe32Plus.OptionalHeader.ImageBase = 0;
972 NtHdr->Pe32Plus.OptionalHeader.SectionAlignment = mCoffAlignment;
973 NtHdr->Pe32Plus.OptionalHeader.FileAlignment = mCoffAlignment;
974 NtHdr->Pe32Plus.OptionalHeader.SizeOfImage = 0;
975
976 NtHdr->Pe32Plus.OptionalHeader.SizeOfHeaders = mTextOffset;
977 NtHdr->Pe32Plus.OptionalHeader.NumberOfRvaAndSizes = EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES;
978
979 //
980 // Section headers.
981 //
982 if ((mDataOffset - mTextOffset) > 0) {
983 CreateSectionHeader (".text", mTextOffset, mDataOffset - mTextOffset,
984 EFI_IMAGE_SCN_CNT_CODE
985 | EFI_IMAGE_SCN_MEM_EXECUTE
986 | EFI_IMAGE_SCN_MEM_READ);
987 } else {
988 // Don't make a section of size 0.
989 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
990 }
991
992 if ((mHiiRsrcOffset - mDataOffset) > 0) {
993 CreateSectionHeader (".data", mDataOffset, mHiiRsrcOffset - mDataOffset,
994 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
995 | EFI_IMAGE_SCN_MEM_WRITE
996 | EFI_IMAGE_SCN_MEM_READ);
997 } else {
998 // Don't make a section of size 0.
999 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
1000 }
1001
1002 if ((mRelocOffset - mHiiRsrcOffset) > 0) {
1003 CreateSectionHeader (".rsrc", mHiiRsrcOffset, mRelocOffset - mHiiRsrcOffset,
1004 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1005 | EFI_IMAGE_SCN_MEM_READ);
1006
1007 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = mRelocOffset - mHiiRsrcOffset;
1008 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = mHiiRsrcOffset;
1009 } else {
1010 // Don't make a section of size 0.
1011 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
1012 }
1013
1014 }
1015
1016 STATIC
1017 BOOLEAN
1018 WriteSections64 (
1019 SECTION_FILTER_TYPES FilterType
1020 )
1021 {
1022 UINT32 Idx;
1023 Elf_Shdr *SecShdr;
1024 UINT32 SecOffset;
1025 BOOLEAN (*Filter)(Elf_Shdr *);
1026 Elf64_Addr GOTEntryRva;
1027
1028 //
1029 // Initialize filter pointer
1030 //
1031 switch (FilterType) {
1032 case SECTION_TEXT:
1033 Filter = IsTextShdr;
1034 break;
1035 case SECTION_HII:
1036 Filter = IsHiiRsrcShdr;
1037 break;
1038 case SECTION_DATA:
1039 Filter = IsDataShdr;
1040 break;
1041 default:
1042 return FALSE;
1043 }
1044
1045 //
1046 // First: copy sections.
1047 //
1048 for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {
1049 Elf_Shdr *Shdr = GetShdrByIndex(Idx);
1050 if ((*Filter)(Shdr)) {
1051 switch (Shdr->sh_type) {
1052 case SHT_PROGBITS:
1053 /* Copy. */
1054 if (Shdr->sh_offset + Shdr->sh_size > mFileBufferSize) {
1055 return FALSE;
1056 }
1057 memcpy(mCoffFile + mCoffSectionsOffset[Idx],
1058 (UINT8*)mEhdr + Shdr->sh_offset,
1059 (size_t) Shdr->sh_size);
1060 break;
1061
1062 case SHT_NOBITS:
1063 memset(mCoffFile + mCoffSectionsOffset[Idx], 0, (size_t) Shdr->sh_size);
1064 break;
1065
1066 default:
1067 //
1068 // Ignore for unknown section type.
1069 //
1070 VerboseMsg ("%s unknown section type %x. We ignore this unknown section type.", mInImageName, (unsigned)Shdr->sh_type);
1071 break;
1072 }
1073 }
1074 }
1075
1076 //
1077 // Second: apply relocations.
1078 //
1079 VerboseMsg ("Applying Relocations...");
1080 for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {
1081 //
1082 // Determine if this is a relocation section.
1083 //
1084 Elf_Shdr *RelShdr = GetShdrByIndex(Idx);
1085 if ((RelShdr->sh_type != SHT_REL) && (RelShdr->sh_type != SHT_RELA)) {
1086 continue;
1087 }
1088
1089 //
1090 // If this is a ET_DYN (PIE) executable, we will encounter a dynamic SHT_RELA
1091 // section that applies to the entire binary, and which will have its section
1092 // index set to #0 (which is a NULL section with the SHF_ALLOC bit cleared).
1093 //
1094 // In the absence of GOT based relocations,
1095 // this RELA section will contain redundant R_xxx_RELATIVE relocations, one
1096 // for every R_xxx_xx64 relocation appearing in the per-section RELA sections.
1097 // (i.e., .rela.text and .rela.data)
1098 //
1099 if (RelShdr->sh_info == 0) {
1100 continue;
1101 }
1102
1103 //
1104 // Relocation section found. Now extract section information that the relocations
1105 // apply to in the ELF data and the new COFF data.
1106 //
1107 SecShdr = GetShdrByIndex(RelShdr->sh_info);
1108 SecOffset = mCoffSectionsOffset[RelShdr->sh_info];
1109
1110 //
1111 // Only process relocations for the current filter type.
1112 //
1113 if (RelShdr->sh_type == SHT_RELA && (*Filter)(SecShdr)) {
1114 UINT64 RelIdx;
1115
1116 //
1117 // Determine the symbol table referenced by the relocation data.
1118 //
1119 Elf_Shdr *SymtabShdr = GetShdrByIndex(RelShdr->sh_link);
1120 UINT8 *Symtab = (UINT8*)mEhdr + SymtabShdr->sh_offset;
1121
1122 //
1123 // Process all relocation entries for this section.
1124 //
1125 for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += (UINT32) RelShdr->sh_entsize) {
1126
1127 //
1128 // Set pointer to relocation entry
1129 //
1130 Elf_Rela *Rel = (Elf_Rela *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx);
1131
1132 //
1133 // Set pointer to symbol table entry associated with the relocation entry.
1134 //
1135 Elf_Sym *Sym = (Elf_Sym *)(Symtab + ELF_R_SYM(Rel->r_info) * SymtabShdr->sh_entsize);
1136
1137 Elf_Shdr *SymShdr;
1138 UINT8 *Targ;
1139
1140 //
1141 // Check section header index found in symbol table and get the section
1142 // header location.
1143 //
1144 if (Sym->st_shndx == SHN_UNDEF
1145 || Sym->st_shndx >= mEhdr->e_shnum) {
1146 const UINT8 *SymName = GetSymName(Sym);
1147 if (SymName == NULL) {
1148 SymName = (const UINT8 *)"<unknown>";
1149 }
1150
1151 //
1152 // Skip error on EM_RISCV64 becasue no symble name is built
1153 // from RISC-V toolchain.
1154 //
1155 if (mEhdr->e_machine != EM_RISCV64) {
1156 Error (NULL, 0, 3000, "Invalid",
1157 "%s: Bad definition for symbol '%s'@%#llx or unsupported symbol type. "
1158 "For example, absolute and undefined symbols are not supported.",
1159 mInImageName, SymName, Sym->st_value);
1160
1161 exit(EXIT_FAILURE);
1162 }
1163 continue;
1164 }
1165 SymShdr = GetShdrByIndex(Sym->st_shndx);
1166
1167 //
1168 // Convert the relocation data to a pointer into the coff file.
1169 //
1170 // Note:
1171 // r_offset is the virtual address of the storage unit to be relocated.
1172 // sh_addr is the virtual address for the base of the section.
1173 //
1174 // r_offset in a memory address.
1175 // Convert it to a pointer in the coff file.
1176 //
1177 Targ = mCoffFile + SecOffset + (Rel->r_offset - SecShdr->sh_addr);
1178
1179 //
1180 // Determine how to handle each relocation type based on the machine type.
1181 //
1182 if (mEhdr->e_machine == EM_X86_64) {
1183 switch (ELF_R_TYPE(Rel->r_info)) {
1184 case R_X86_64_NONE:
1185 break;
1186 case R_X86_64_64:
1187 //
1188 // Absolute relocation.
1189 //
1190 VerboseMsg ("R_X86_64_64");
1191 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1192 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
1193 *(UINT64 *)Targ);
1194 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
1195 VerboseMsg ("Relocation: 0x%016LX", *(UINT64*)Targ);
1196 break;
1197 case R_X86_64_32:
1198 VerboseMsg ("R_X86_64_32");
1199 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1200 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
1201 *(UINT32 *)Targ);
1202 *(UINT32 *)Targ = (UINT32)((UINT64)(*(UINT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
1203 VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);
1204 break;
1205 case R_X86_64_32S:
1206 VerboseMsg ("R_X86_64_32S");
1207 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1208 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
1209 *(UINT32 *)Targ);
1210 *(INT32 *)Targ = (INT32)((INT64)(*(INT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
1211 VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);
1212 break;
1213
1214 case R_X86_64_PLT32:
1215 //
1216 // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is
1217 // possible since we know all code symbol references resolve to
1218 // definitions in the same module (UEFI has no shared libraries),
1219 // and so there is never a reason to jump via a PLT entry,
1220 // allowing us to resolve the reference using the symbol directly.
1221 //
1222 VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");
1223 /* fall through */
1224 case R_X86_64_PC32:
1225 //
1226 // Relative relocation: Symbol - Ip + Addend
1227 //
1228 VerboseMsg ("R_X86_64_PC32");
1229 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1230 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
1231 *(UINT32 *)Targ);
1232 *(UINT32 *)Targ = (UINT32) (*(UINT32 *)Targ
1233 + (mCoffSectionsOffset[Sym->st_shndx] - SymShdr->sh_addr)
1234 - (SecOffset - SecShdr->sh_addr));
1235 VerboseMsg ("Relocation: 0x%08X", *(UINT32 *)Targ);
1236 break;
1237 case R_X86_64_GOTPCREL:
1238 case R_X86_64_GOTPCRELX:
1239 case R_X86_64_REX_GOTPCRELX:
1240 VerboseMsg ("R_X86_64_GOTPCREL family");
1241 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
1242 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
1243 *(UINT32 *)Targ);
1244 GOTEntryRva = Rel->r_offset - Rel->r_addend + *(INT32 *)Targ;
1245 FindElfGOTSectionFromGOTEntryElfRva(GOTEntryRva);
1246 *(UINT32 *)Targ = (UINT32) (*(UINT32 *)Targ
1247 + (mCoffSectionsOffset[mGOTShindex] - mGOTShdr->sh_addr)
1248 - (SecOffset - SecShdr->sh_addr));
1249 VerboseMsg ("Relocation: 0x%08X", *(UINT32 *)Targ);
1250 GOTEntryRva += (mCoffSectionsOffset[mGOTShindex] - mGOTShdr->sh_addr); // ELF Rva -> COFF Rva
1251 if (AccumulateCoffGOTEntries((UINT32)GOTEntryRva)) {
1252 //
1253 // Relocate GOT entry if it's the first time we run into it
1254 //
1255 Targ = mCoffFile + GOTEntryRva;
1256 //
1257 // Limitation: The following three statements assume memory
1258 // at *Targ is valid because the section containing the GOT
1259 // has already been copied from the ELF image to the Coff image.
1260 // This pre-condition presently holds because the GOT is placed
1261 // in section .text, and the ELF text sections are all copied
1262 // prior to reaching this point.
1263 // If the pre-condition is violated in the future, this fixup
1264 // either needs to be deferred after the GOT section is copied
1265 // to the Coff image, or the fixup should be performed on the
1266 // source Elf image instead of the destination Coff image.
1267 //
1268 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
1269 (UINT32)GOTEntryRva,
1270 *(UINT64 *)Targ);
1271 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
1272 VerboseMsg ("Relocation: 0x%016LX", *(UINT64*)Targ);
1273 }
1274 break;
1275 default:
1276 Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1277 }
1278 } else if (mEhdr->e_machine == EM_AARCH64) {
1279
1280 switch (ELF_R_TYPE(Rel->r_info)) {
1281 INT64 Offset;
1282
1283 case R_AARCH64_LD64_GOT_LO12_NC:
1284 //
1285 // Convert into an ADD instruction - see R_AARCH64_ADR_GOT_PAGE below.
1286 //
1287 *(UINT32 *)Targ &= 0x3ff;
1288 *(UINT32 *)Targ |= 0x91000000 | ((Sym->st_value & 0xfff) << 10);
1289 break;
1290
1291 case R_AARCH64_ADR_GOT_PAGE:
1292 //
1293 // This relocation points to the GOT entry that contains the absolute
1294 // address of the symbol we are referring to. Since EDK2 only uses
1295 // fully linked binaries, we can avoid the indirection, and simply
1296 // refer to the symbol directly. This implies having to patch the
1297 // subsequent LDR instruction (covered by a R_AARCH64_LD64_GOT_LO12_NC
1298 // relocation) into an ADD instruction - this is handled above.
1299 //
1300 Offset = (Sym->st_value - (Rel->r_offset & ~0xfff)) >> 12;
1301
1302 *(UINT32 *)Targ &= 0x9000001f;
1303 *(UINT32 *)Targ |= ((Offset & 0x1ffffc) << (5 - 2)) | ((Offset & 0x3) << 29);
1304
1305 /* fall through */
1306
1307 case R_AARCH64_ADR_PREL_PG_HI21:
1308 //
1309 // In order to handle Cortex-A53 erratum #843419, the LD linker may
1310 // convert ADRP instructions into ADR instructions, but without
1311 // updating the static relocation type, and so we may end up here
1312 // while the instruction in question is actually ADR. So let's
1313 // just disregard it: the section offset check we apply below to
1314 // ADR instructions will trigger for its R_AARCH64_xxx_ABS_LO12_NC
1315 // companion instruction as well, so it is safe to omit it here.
1316 //
1317 if ((*(UINT32 *)Targ & BIT31) == 0) {
1318 break;
1319 }
1320
1321 //
1322 // AArch64 PG_H21 relocations are typically paired with ABS_LO12
1323 // relocations, where a PC-relative reference with +/- 4 GB range is
1324 // split into a relative high part and an absolute low part. Since
1325 // the absolute low part represents the offset into a 4 KB page, we
1326 // either have to convert the ADRP into an ADR instruction, or we
1327 // need to use a section alignment of at least 4 KB, so that the
1328 // binary appears at a correct offset at runtime. In any case, we
1329 // have to make sure that the 4 KB relative offsets of both the
1330 // section containing the reference as well as the section to which
1331 // it refers have not been changed during PE/COFF conversion (i.e.,
1332 // in ScanSections64() above).
1333 //
1334 if (mCoffAlignment < 0x1000) {
1335 //
1336 // Attempt to convert the ADRP into an ADR instruction.
1337 // This is only possible if the symbol is within +/- 1 MB.
1338 //
1339
1340 // Decode the ADRP instruction
1341 Offset = (INT32)((*(UINT32 *)Targ & 0xffffe0) << 8);
1342 Offset = (Offset << (6 - 5)) | ((*(UINT32 *)Targ & 0x60000000) >> (29 - 12));
1343
1344 //
1345 // ADRP offset is relative to the previous page boundary,
1346 // whereas ADR offset is relative to the instruction itself.
1347 // So fix up the offset so it points to the page containing
1348 // the symbol.
1349 //
1350 Offset -= (UINTN)(Targ - mCoffFile) & 0xfff;
1351
1352 if (Offset < -0x100000 || Offset > 0xfffff) {
1353 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s due to its size (> 1 MB), this module requires 4 KB section alignment.",
1354 mInImageName);
1355 break;
1356 }
1357
1358 // Re-encode the offset as an ADR instruction
1359 *(UINT32 *)Targ &= 0x1000001f;
1360 *(UINT32 *)Targ |= ((Offset & 0x1ffffc) << (5 - 2)) | ((Offset & 0x3) << 29);
1361 }
1362 /* fall through */
1363
1364 case R_AARCH64_ADD_ABS_LO12_NC:
1365 case R_AARCH64_LDST8_ABS_LO12_NC:
1366 case R_AARCH64_LDST16_ABS_LO12_NC:
1367 case R_AARCH64_LDST32_ABS_LO12_NC:
1368 case R_AARCH64_LDST64_ABS_LO12_NC:
1369 case R_AARCH64_LDST128_ABS_LO12_NC:
1370 if (((SecShdr->sh_addr ^ SecOffset) & 0xfff) != 0 ||
1371 ((SymShdr->sh_addr ^ mCoffSectionsOffset[Sym->st_shndx]) & 0xfff) != 0) {
1372 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 small code model requires identical ELF and PE/COFF section offsets modulo 4 KB.",
1373 mInImageName);
1374 break;
1375 }
1376 /* fall through */
1377
1378 case R_AARCH64_ADR_PREL_LO21:
1379 case R_AARCH64_CONDBR19:
1380 case R_AARCH64_LD_PREL_LO19:
1381 case R_AARCH64_CALL26:
1382 case R_AARCH64_JUMP26:
1383 case R_AARCH64_PREL64:
1384 case R_AARCH64_PREL32:
1385 case R_AARCH64_PREL16:
1386 //
1387 // The GCC toolchains (i.e., binutils) may corrupt section relative
1388 // relocations when emitting relocation sections into fully linked
1389 // binaries. More specifically, they tend to fail to take into
1390 // account the fact that a '.rodata + XXX' relocation needs to have
1391 // its addend recalculated once .rodata is merged into the .text
1392 // section, and the relocation emitted into the .rela.text section.
1393 //
1394 // We cannot really recover from this loss of information, so the
1395 // only workaround is to prevent having to recalculate any relative
1396 // relocations at all, by using a linker script that ensures that
1397 // the offset between the Place and the Symbol is the same in both
1398 // the ELF and the PE/COFF versions of the binary.
1399 //
1400 if ((SymShdr->sh_addr - SecShdr->sh_addr) !=
1401 (mCoffSectionsOffset[Sym->st_shndx] - SecOffset)) {
1402 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 relative relocations require identical ELF and PE/COFF section offsets",
1403 mInImageName);
1404 }
1405 break;
1406
1407 // Absolute relocations.
1408 case R_AARCH64_ABS64:
1409 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
1410 break;
1411
1412 default:
1413 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1414 }
1415 } else if (mEhdr->e_machine == EM_RISCV64) {
1416 //
1417 // Write section for RISC-V 64 architecture.
1418 //
1419 WriteSectionRiscV64 (Rel, Targ, SymShdr, Sym);
1420 } else {
1421 Error (NULL, 0, 3000, "Invalid", "Not a supported machine type");
1422 }
1423 }
1424 }
1425 }
1426
1427 return TRUE;
1428 }
1429
1430 STATIC
1431 VOID
1432 WriteRelocations64 (
1433 VOID
1434 )
1435 {
1436 UINT32 Index;
1437 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
1438 EFI_IMAGE_DATA_DIRECTORY *Dir;
1439 UINT32 RiscVRelType;
1440
1441 for (Index = 0; Index < mEhdr->e_shnum; Index++) {
1442 Elf_Shdr *RelShdr = GetShdrByIndex(Index);
1443 if ((RelShdr->sh_type == SHT_REL) || (RelShdr->sh_type == SHT_RELA)) {
1444 Elf_Shdr *SecShdr = GetShdrByIndex (RelShdr->sh_info);
1445 if (IsTextShdr(SecShdr) || IsDataShdr(SecShdr)) {
1446 UINT64 RelIdx;
1447
1448 for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += RelShdr->sh_entsize) {
1449 Elf_Rela *Rel = (Elf_Rela *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx);
1450
1451 if (mEhdr->e_machine == EM_X86_64) {
1452 switch (ELF_R_TYPE(Rel->r_info)) {
1453 case R_X86_64_NONE:
1454 case R_X86_64_PC32:
1455 case R_X86_64_PLT32:
1456 case R_X86_64_GOTPCREL:
1457 case R_X86_64_GOTPCRELX:
1458 case R_X86_64_REX_GOTPCRELX:
1459 break;
1460 case R_X86_64_64:
1461 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",
1462 mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr));
1463 CoffAddFixup(
1464 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1465 + (Rel->r_offset - SecShdr->sh_addr)),
1466 EFI_IMAGE_REL_BASED_DIR64);
1467 break;
1468 //
1469 // R_X86_64_32 and R_X86_64_32S are ELF64 relocations emitted when using
1470 // the SYSV X64 ABI small non-position-independent code model.
1471 // R_X86_64_32 is used for unsigned 32-bit immediates with a 32-bit operand
1472 // size. The value is either not extended, or zero-extended to 64 bits.
1473 // R_X86_64_32S is used for either signed 32-bit non-rip-relative displacements
1474 // or signed 32-bit immediates with a 64-bit operand size. The value is
1475 // sign-extended to 64 bits.
1476 // EFI_IMAGE_REL_BASED_HIGHLOW is a PE relocation that uses 32-bit arithmetic
1477 // for rebasing an image.
1478 // EFI PE binaries declare themselves EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE and
1479 // may load above 2GB. If an EFI PE binary with a converted R_X86_64_32S
1480 // relocation is loaded above 2GB, the value will get sign-extended to the
1481 // negative part of the 64-bit address space. The negative part of the 64-bit
1482 // address space is unmapped, so accessing such an address page-faults.
1483 // In order to support R_X86_64_32S, it is necessary to unset
1484 // EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE, and the EFI PE loader must implement
1485 // this flag and abstain from loading such a PE binary above 2GB.
1486 // Since this feature is not supported, support for R_X86_64_32S (and hence
1487 // the small non-position-independent code model) is disabled.
1488 //
1489 // case R_X86_64_32S:
1490 case R_X86_64_32:
1491 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",
1492 mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr));
1493 CoffAddFixup(
1494 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1495 + (Rel->r_offset - SecShdr->sh_addr)),
1496 EFI_IMAGE_REL_BASED_HIGHLOW);
1497 break;
1498 default:
1499 Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1500 }
1501 } else if (mEhdr->e_machine == EM_AARCH64) {
1502
1503 switch (ELF_R_TYPE(Rel->r_info)) {
1504 case R_AARCH64_ADR_PREL_LO21:
1505 case R_AARCH64_CONDBR19:
1506 case R_AARCH64_LD_PREL_LO19:
1507 case R_AARCH64_CALL26:
1508 case R_AARCH64_JUMP26:
1509 case R_AARCH64_PREL64:
1510 case R_AARCH64_PREL32:
1511 case R_AARCH64_PREL16:
1512 case R_AARCH64_ADR_PREL_PG_HI21:
1513 case R_AARCH64_ADD_ABS_LO12_NC:
1514 case R_AARCH64_LDST8_ABS_LO12_NC:
1515 case R_AARCH64_LDST16_ABS_LO12_NC:
1516 case R_AARCH64_LDST32_ABS_LO12_NC:
1517 case R_AARCH64_LDST64_ABS_LO12_NC:
1518 case R_AARCH64_LDST128_ABS_LO12_NC:
1519 case R_AARCH64_ADR_GOT_PAGE:
1520 case R_AARCH64_LD64_GOT_LO12_NC:
1521 //
1522 // No fixups are required for relative relocations, provided that
1523 // the relative offsets between sections have been preserved in
1524 // the ELF to PE/COFF conversion. We have already asserted that
1525 // this is the case in WriteSections64 ().
1526 //
1527 break;
1528
1529 case R_AARCH64_ABS64:
1530 CoffAddFixup(
1531 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1532 + (Rel->r_offset - SecShdr->sh_addr)),
1533 EFI_IMAGE_REL_BASED_DIR64);
1534 break;
1535
1536 case R_AARCH64_ABS32:
1537 CoffAddFixup(
1538 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1539 + (Rel->r_offset - SecShdr->sh_addr)),
1540 EFI_IMAGE_REL_BASED_HIGHLOW);
1541 break;
1542
1543 default:
1544 Error (NULL, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1545 }
1546 } else if (mEhdr->e_machine == EM_RISCV64) {
1547 RiscVRelType = ELF_R_TYPE(Rel->r_info);
1548 switch (RiscVRelType) {
1549 case R_RISCV_NONE:
1550 break;
1551
1552 case R_RISCV_32:
1553 CoffAddFixup(
1554 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1555 + (Rel->r_offset - SecShdr->sh_addr)),
1556 EFI_IMAGE_REL_BASED_HIGHLOW);
1557 break;
1558
1559 case R_RISCV_64:
1560 CoffAddFixup(
1561 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1562 + (Rel->r_offset - SecShdr->sh_addr)),
1563 EFI_IMAGE_REL_BASED_DIR64);
1564 break;
1565
1566 case R_RISCV_HI20:
1567 CoffAddFixup(
1568 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1569 + (Rel->r_offset - SecShdr->sh_addr)),
1570 EFI_IMAGE_REL_BASED_RISCV_HI20);
1571 break;
1572
1573 case R_RISCV_LO12_I:
1574 CoffAddFixup(
1575 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1576 + (Rel->r_offset - SecShdr->sh_addr)),
1577 EFI_IMAGE_REL_BASED_RISCV_LOW12I);
1578 break;
1579
1580 case R_RISCV_LO12_S:
1581 CoffAddFixup(
1582 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1583 + (Rel->r_offset - SecShdr->sh_addr)),
1584 EFI_IMAGE_REL_BASED_RISCV_LOW12S);
1585 break;
1586
1587 case R_RISCV_ADD64:
1588 CoffAddFixup(
1589 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1590 + (Rel->r_offset - SecShdr->sh_addr)),
1591 EFI_IMAGE_REL_BASED_ABSOLUTE);
1592 break;
1593
1594 case R_RISCV_SUB64:
1595 CoffAddFixup(
1596 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1597 + (Rel->r_offset - SecShdr->sh_addr)),
1598 EFI_IMAGE_REL_BASED_ABSOLUTE);
1599 break;
1600
1601 case R_RISCV_ADD32:
1602 CoffAddFixup(
1603 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1604 + (Rel->r_offset - SecShdr->sh_addr)),
1605 EFI_IMAGE_REL_BASED_ABSOLUTE);
1606 break;
1607
1608 case R_RISCV_SUB32:
1609 CoffAddFixup(
1610 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1611 + (Rel->r_offset - SecShdr->sh_addr)),
1612 EFI_IMAGE_REL_BASED_ABSOLUTE);
1613 break;
1614
1615 case R_RISCV_BRANCH:
1616 CoffAddFixup(
1617 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1618 + (Rel->r_offset - SecShdr->sh_addr)),
1619 EFI_IMAGE_REL_BASED_ABSOLUTE);
1620 break;
1621
1622 case R_RISCV_JAL:
1623 CoffAddFixup(
1624 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
1625 + (Rel->r_offset - SecShdr->sh_addr)),
1626 EFI_IMAGE_REL_BASED_ABSOLUTE);
1627 break;
1628
1629 case R_RISCV_GPREL_I:
1630 case R_RISCV_GPREL_S:
1631 case R_RISCV_CALL:
1632 case R_RISCV_CALL_PLT:
1633 case R_RISCV_RVC_BRANCH:
1634 case R_RISCV_RVC_JUMP:
1635 case R_RISCV_RELAX:
1636 case R_RISCV_SUB6:
1637 case R_RISCV_SET6:
1638 case R_RISCV_SET8:
1639 case R_RISCV_SET16:
1640 case R_RISCV_SET32:
1641 case R_RISCV_PCREL_HI20:
1642 case R_RISCV_GOT_HI20:
1643 case R_RISCV_PCREL_LO12_I:
1644 case R_RISCV_PCREL_LO12_S:
1645 break;
1646
1647 default:
1648 Error (NULL, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_RISCV64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
1649 }
1650 } else {
1651 Error (NULL, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr->e_machine);
1652 }
1653 }
1654 if (mEhdr->e_machine == EM_X86_64 && RelShdr->sh_info == mGOTShindex) {
1655 //
1656 // Tack relocations for GOT entries after other relocations for
1657 // the section the GOT is in, as it's usually found at the end
1658 // of the section. This is done in order to maintain Rva order
1659 // of Coff relocations.
1660 //
1661 EmitGOTRelocations();
1662 }
1663 }
1664 }
1665 }
1666
1667 if (mEhdr->e_machine == EM_X86_64) {
1668 //
1669 // This is a safety net just in case the GOT is in a section
1670 // with no other relocations and the first invocation of
1671 // EmitGOTRelocations() above was skipped. This invocation
1672 // does not maintain Rva order of Coff relocations.
1673 // At present, with a single text section, all references to
1674 // the GOT and the GOT itself reside in section .text, so
1675 // if there's a GOT at all, the first invocation above
1676 // is executed.
1677 //
1678 EmitGOTRelocations();
1679 }
1680 //
1681 // Pad by adding empty entries.
1682 //
1683 while (mCoffOffset & (mCoffAlignment - 1)) {
1684 CoffAddFixupEntry(0);
1685 }
1686
1687 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
1688 Dir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
1689 Dir->Size = mCoffOffset - mRelocOffset;
1690 if (Dir->Size == 0) {
1691 // If no relocations, null out the directory entry and don't add the .reloc section
1692 Dir->VirtualAddress = 0;
1693 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
1694 } else {
1695 Dir->VirtualAddress = mRelocOffset;
1696 CreateSectionHeader (".reloc", mRelocOffset, mCoffOffset - mRelocOffset,
1697 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1698 | EFI_IMAGE_SCN_MEM_DISCARDABLE
1699 | EFI_IMAGE_SCN_MEM_READ);
1700 }
1701 }
1702
1703 STATIC
1704 VOID
1705 WriteDebug64 (
1706 VOID
1707 )
1708 {
1709 UINT32 Len;
1710 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
1711 EFI_IMAGE_DATA_DIRECTORY *DataDir;
1712 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *Dir;
1713 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *Nb10;
1714
1715 Len = strlen(mInImageName) + 1;
1716
1717 Dir = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(mCoffFile + mDebugOffset);
1718 Dir->Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW;
1719 Dir->SizeOfData = sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + Len;
1720 Dir->RVA = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
1721 Dir->FileOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
1722
1723 Nb10 = (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY*)(Dir + 1);
1724 Nb10->Signature = CODEVIEW_SIGNATURE_NB10;
1725 strcpy ((char *)(Nb10 + 1), mInImageName);
1726
1727
1728 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
1729 DataDir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG];
1730 DataDir->VirtualAddress = mDebugOffset;
1731 DataDir->Size = sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
1732 }
1733
1734 STATIC
1735 VOID
1736 SetImageSize64 (
1737 VOID
1738 )
1739 {
1740 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
1741
1742 //
1743 // Set image size
1744 //
1745 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
1746 NtHdr->Pe32Plus.OptionalHeader.SizeOfImage = mCoffOffset;
1747 }
1748
1749 STATIC
1750 VOID
1751 CleanUp64 (
1752 VOID
1753 )
1754 {
1755 if (mCoffSectionsOffset != NULL) {
1756 free (mCoffSectionsOffset);
1757 }
1758 }
1759
1760