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