]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - BaseTools/Source/C/GenFw/Elf64Convert.c
MdeModulePkg/CapsuleApp: Fix memory leak issue.
[mirror_edk2.git] / BaseTools / Source / C / GenFw / Elf64Convert.c
... / ...
CommitLineData
1/** @file\r
2Elf64 convert solution\r
3\r
4Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
5Portions copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>\r
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
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
106//\r
107// Coff information\r
108//\r
109STATIC UINT32 mCoffAlignment = 0x20;\r
110\r
111//\r
112// PE section alignment.\r
113//\r
114STATIC const UINT16 mCoffNbrSections = 4;\r
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
129STATIC UINT32 mDebugOffset;\r
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
183 if (mCoffSectionsOffset == NULL) {\r
184 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
185 return FALSE;\r
186 }\r
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
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
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
230STATIC\r
231UINT32\r
232DebugRvaAlign (\r
233 UINT32 Offset\r
234 )\r
235{\r
236 return (Offset + 3) & ~3;\r
237}\r
238\r
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
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
307 Elf_Shdr *StrtabShdr;\r
308 UINT8 *StrtabContents;\r
309 BOOLEAN foundEnd;\r
310 UINT32 i;\r
311\r
312 if (Sym->st_name == 0) {\r
313 return NULL;\r
314 }\r
315\r
316 StrtabShdr = FindStrtabShdr();\r
317 if (StrtabShdr == NULL) {\r
318 return NULL;\r
319 }\r
320\r
321 assert(Sym->st_name < StrtabShdr->sh_size);\r
322\r
323 StrtabContents = (UINT8*)mEhdr + StrtabShdr->sh_offset;\r
324\r
325 foundEnd = FALSE;\r
326 for (i= Sym->st_name; (i < StrtabShdr->sh_size) && !foundEnd; i++) {\r
327 foundEnd = (BOOLEAN)(StrtabContents[i] == 0);\r
328 }\r
329 assert(foundEnd);\r
330\r
331 return StrtabContents + Sym->st_name;\r
332}\r
333\r
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
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
477 BOOLEAN FoundSection;\r
478\r
479 CoffEntry = 0;\r
480 mCoffOffset = 0;\r
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_AARCH64:\r
490 mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS64);\r
491 break;\r
492 default:\r
493 VerboseMsg ("%s unknown e_machine type %hu. Assume X64", mInImageName, mEhdr->e_machine);\r
494 mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS64);\r
495 break;\r
496 }\r
497\r
498 mTableOffset = mCoffOffset;\r
499 mCoffOffset += mCoffNbrSections * sizeof(EFI_IMAGE_SECTION_HEADER);\r
500\r
501 //\r
502 // Set mCoffAlignment to the maximum alignment of the input sections\r
503 // we care about\r
504 //\r
505 for (i = 0; i < mEhdr->e_shnum; i++) {\r
506 Elf_Shdr *shdr = GetShdrByIndex(i);\r
507 if (shdr->sh_addralign <= mCoffAlignment) {\r
508 continue;\r
509 }\r
510 if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr)) {\r
511 mCoffAlignment = (UINT32)shdr->sh_addralign;\r
512 }\r
513 }\r
514\r
515 //\r
516 // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT\r
517 //\r
518 if (mCoffAlignment > MAX_COFF_ALIGNMENT) {\r
519 Error (NULL, 0, 3000, "Invalid", "Section alignment is larger than MAX_COFF_ALIGNMENT.");\r
520 assert (FALSE);\r
521 }\r
522\r
523\r
524 //\r
525 // Move the PE/COFF header right before the first section. This will help us\r
526 // save space when converting to TE.\r
527 //\r
528 if (mCoffAlignment > mCoffOffset) {\r
529 mNtHdrOffset += mCoffAlignment - mCoffOffset;\r
530 mTableOffset += mCoffAlignment - mCoffOffset;\r
531 mCoffOffset = mCoffAlignment;\r
532 }\r
533\r
534 //\r
535 // First text sections.\r
536 //\r
537 mCoffOffset = CoffAlign(mCoffOffset);\r
538 mTextOffset = mCoffOffset;\r
539 FoundSection = FALSE;\r
540 SectionCount = 0;\r
541 for (i = 0; i < mEhdr->e_shnum; i++) {\r
542 Elf_Shdr *shdr = GetShdrByIndex(i);\r
543 if (IsTextShdr(shdr)) {\r
544 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {\r
545 // the alignment field is valid\r
546 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {\r
547 // if the section address is aligned we must align PE/COFF\r
548 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));\r
549 } else {\r
550 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");\r
551 }\r
552 }\r
553\r
554 /* Relocate entry. */\r
555 if ((mEhdr->e_entry >= shdr->sh_addr) &&\r
556 (mEhdr->e_entry < shdr->sh_addr + shdr->sh_size)) {\r
557 CoffEntry = (UINT32) (mCoffOffset + mEhdr->e_entry - shdr->sh_addr);\r
558 }\r
559\r
560 //\r
561 // Set mTextOffset with the offset of the first '.text' section\r
562 //\r
563 if (!FoundSection) {\r
564 mTextOffset = mCoffOffset;\r
565 FoundSection = TRUE;\r
566 }\r
567\r
568 mCoffSectionsOffset[i] = mCoffOffset;\r
569 mCoffOffset += (UINT32) shdr->sh_size;\r
570 SectionCount ++;\r
571 }\r
572 }\r
573\r
574 if (!FoundSection) {\r
575 Error (NULL, 0, 3000, "Invalid", "Did not find any '.text' section.");\r
576 assert (FALSE);\r
577 }\r
578\r
579 mDebugOffset = DebugRvaAlign(mCoffOffset);\r
580 mCoffOffset = CoffAlign(mCoffOffset);\r
581\r
582 if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {\r
583 Warning (NULL, 0, 0, NULL, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName);\r
584 }\r
585\r
586 //\r
587 // Then data sections.\r
588 //\r
589 mDataOffset = mCoffOffset;\r
590 FoundSection = FALSE;\r
591 SectionCount = 0;\r
592 for (i = 0; i < mEhdr->e_shnum; i++) {\r
593 Elf_Shdr *shdr = GetShdrByIndex(i);\r
594 if (IsDataShdr(shdr)) {\r
595 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {\r
596 // the alignment field is valid\r
597 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {\r
598 // if the section address is aligned we must align PE/COFF\r
599 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));\r
600 } else {\r
601 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");\r
602 }\r
603 }\r
604\r
605 //\r
606 // Set mDataOffset with the offset of the first '.data' section\r
607 //\r
608 if (!FoundSection) {\r
609 mDataOffset = mCoffOffset;\r
610 FoundSection = TRUE;\r
611 }\r
612 mCoffSectionsOffset[i] = mCoffOffset;\r
613 mCoffOffset += (UINT32) shdr->sh_size;\r
614 SectionCount ++;\r
615 }\r
616 }\r
617\r
618 //\r
619 // Make room for .debug data in .data (or .text if .data is empty) instead of\r
620 // putting it in a section of its own. This is explicitly allowed by the\r
621 // PE/COFF spec, and prevents bloat in the binary when using large values for\r
622 // section alignment.\r
623 //\r
624 if (SectionCount > 0) {\r
625 mDebugOffset = DebugRvaAlign(mCoffOffset);\r
626 }\r
627 mCoffOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY) +\r
628 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) +\r
629 strlen(mInImageName) + 1;\r
630\r
631 mCoffOffset = CoffAlign(mCoffOffset);\r
632 if (SectionCount == 0) {\r
633 mDataOffset = mCoffOffset;\r
634 }\r
635\r
636 if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {\r
637 Warning (NULL, 0, 0, NULL, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName);\r
638 }\r
639\r
640 //\r
641 // The HII resource sections.\r
642 //\r
643 mHiiRsrcOffset = mCoffOffset;\r
644 for (i = 0; i < mEhdr->e_shnum; i++) {\r
645 Elf_Shdr *shdr = GetShdrByIndex(i);\r
646 if (IsHiiRsrcShdr(shdr)) {\r
647 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {\r
648 // the alignment field is valid\r
649 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {\r
650 // if the section address is aligned we must align PE/COFF\r
651 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));\r
652 } else {\r
653 Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment.");\r
654 }\r
655 }\r
656 if (shdr->sh_size != 0) {\r
657 mHiiRsrcOffset = mCoffOffset;\r
658 mCoffSectionsOffset[i] = mCoffOffset;\r
659 mCoffOffset += (UINT32) shdr->sh_size;\r
660 mCoffOffset = CoffAlign(mCoffOffset);\r
661 SetHiiResourceHeader ((UINT8*) mEhdr + shdr->sh_offset, mHiiRsrcOffset);\r
662 }\r
663 break;\r
664 }\r
665 }\r
666\r
667 mRelocOffset = mCoffOffset;\r
668\r
669 //\r
670 // Allocate base Coff file. Will be expanded later for relocations.\r
671 //\r
672 mCoffFile = (UINT8 *)malloc(mCoffOffset);\r
673 if (mCoffFile == NULL) {\r
674 Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
675 }\r
676 assert (mCoffFile != NULL);\r
677 memset(mCoffFile, 0, mCoffOffset);\r
678\r
679 //\r
680 // Fill headers.\r
681 //\r
682 DosHdr = (EFI_IMAGE_DOS_HEADER *)mCoffFile;\r
683 DosHdr->e_magic = EFI_IMAGE_DOS_SIGNATURE;\r
684 DosHdr->e_lfanew = mNtHdrOffset;\r
685\r
686 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION*)(mCoffFile + mNtHdrOffset);\r
687\r
688 NtHdr->Pe32Plus.Signature = EFI_IMAGE_NT_SIGNATURE;\r
689\r
690 switch (mEhdr->e_machine) {\r
691 case EM_X86_64:\r
692 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_X64;\r
693 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r
694 break;\r
695 case EM_AARCH64:\r
696 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_AARCH64;\r
697 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r
698 break;\r
699 default:\r
700 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN)mEhdr->e_machine);\r
701 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_X64;\r
702 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r
703 }\r
704\r
705 NtHdr->Pe32Plus.FileHeader.NumberOfSections = mCoffNbrSections;\r
706 NtHdr->Pe32Plus.FileHeader.TimeDateStamp = (UINT32) time(NULL);\r
707 mImageTimeStamp = NtHdr->Pe32Plus.FileHeader.TimeDateStamp;\r
708 NtHdr->Pe32Plus.FileHeader.PointerToSymbolTable = 0;\r
709 NtHdr->Pe32Plus.FileHeader.NumberOfSymbols = 0;\r
710 NtHdr->Pe32Plus.FileHeader.SizeOfOptionalHeader = sizeof(NtHdr->Pe32Plus.OptionalHeader);\r
711 NtHdr->Pe32Plus.FileHeader.Characteristics = EFI_IMAGE_FILE_EXECUTABLE_IMAGE\r
712 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED\r
713 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED\r
714 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE;\r
715\r
716 NtHdr->Pe32Plus.OptionalHeader.SizeOfCode = mDataOffset - mTextOffset;\r
717 NtHdr->Pe32Plus.OptionalHeader.SizeOfInitializedData = mRelocOffset - mDataOffset;\r
718 NtHdr->Pe32Plus.OptionalHeader.SizeOfUninitializedData = 0;\r
719 NtHdr->Pe32Plus.OptionalHeader.AddressOfEntryPoint = CoffEntry;\r
720\r
721 NtHdr->Pe32Plus.OptionalHeader.BaseOfCode = mTextOffset;\r
722\r
723 NtHdr->Pe32Plus.OptionalHeader.ImageBase = 0;\r
724 NtHdr->Pe32Plus.OptionalHeader.SectionAlignment = mCoffAlignment;\r
725 NtHdr->Pe32Plus.OptionalHeader.FileAlignment = mCoffAlignment;\r
726 NtHdr->Pe32Plus.OptionalHeader.SizeOfImage = 0;\r
727\r
728 NtHdr->Pe32Plus.OptionalHeader.SizeOfHeaders = mTextOffset;\r
729 NtHdr->Pe32Plus.OptionalHeader.NumberOfRvaAndSizes = EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES;\r
730\r
731 //\r
732 // Section headers.\r
733 //\r
734 if ((mDataOffset - mTextOffset) > 0) {\r
735 CreateSectionHeader (".text", mTextOffset, mDataOffset - mTextOffset,\r
736 EFI_IMAGE_SCN_CNT_CODE\r
737 | EFI_IMAGE_SCN_MEM_EXECUTE\r
738 | EFI_IMAGE_SCN_MEM_READ);\r
739 } else {\r
740 // Don't make a section of size 0.\r
741 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;\r
742 }\r
743\r
744 if ((mHiiRsrcOffset - mDataOffset) > 0) {\r
745 CreateSectionHeader (".data", mDataOffset, mHiiRsrcOffset - mDataOffset,\r
746 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA\r
747 | EFI_IMAGE_SCN_MEM_WRITE\r
748 | EFI_IMAGE_SCN_MEM_READ);\r
749 } else {\r
750 // Don't make a section of size 0.\r
751 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;\r
752 }\r
753\r
754 if ((mRelocOffset - mHiiRsrcOffset) > 0) {\r
755 CreateSectionHeader (".rsrc", mHiiRsrcOffset, mRelocOffset - mHiiRsrcOffset,\r
756 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA\r
757 | EFI_IMAGE_SCN_MEM_READ);\r
758\r
759 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = mRelocOffset - mHiiRsrcOffset;\r
760 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = mHiiRsrcOffset;\r
761 } else {\r
762 // Don't make a section of size 0.\r
763 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;\r
764 }\r
765\r
766}\r
767\r
768STATIC\r
769BOOLEAN\r
770WriteSections64 (\r
771 SECTION_FILTER_TYPES FilterType\r
772 )\r
773{\r
774 UINT32 Idx;\r
775 Elf_Shdr *SecShdr;\r
776 UINT32 SecOffset;\r
777 BOOLEAN (*Filter)(Elf_Shdr *);\r
778 Elf64_Addr GOTEntryRva;\r
779\r
780 //\r
781 // Initialize filter pointer\r
782 //\r
783 switch (FilterType) {\r
784 case SECTION_TEXT:\r
785 Filter = IsTextShdr;\r
786 break;\r
787 case SECTION_HII:\r
788 Filter = IsHiiRsrcShdr;\r
789 break;\r
790 case SECTION_DATA:\r
791 Filter = IsDataShdr;\r
792 break;\r
793 default:\r
794 return FALSE;\r
795 }\r
796\r
797 //\r
798 // First: copy sections.\r
799 //\r
800 for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {\r
801 Elf_Shdr *Shdr = GetShdrByIndex(Idx);\r
802 if ((*Filter)(Shdr)) {\r
803 switch (Shdr->sh_type) {\r
804 case SHT_PROGBITS:\r
805 /* Copy. */\r
806 if (Shdr->sh_offset + Shdr->sh_size > mFileBufferSize) {\r
807 return FALSE;\r
808 }\r
809 memcpy(mCoffFile + mCoffSectionsOffset[Idx],\r
810 (UINT8*)mEhdr + Shdr->sh_offset,\r
811 (size_t) Shdr->sh_size);\r
812 break;\r
813\r
814 case SHT_NOBITS:\r
815 memset(mCoffFile + mCoffSectionsOffset[Idx], 0, (size_t) Shdr->sh_size);\r
816 break;\r
817\r
818 default:\r
819 //\r
820 // Ignore for unkown section type.\r
821 //\r
822 VerboseMsg ("%s unknown section type %x. We ignore this unknown section type.", mInImageName, (unsigned)Shdr->sh_type);\r
823 break;\r
824 }\r
825 }\r
826 }\r
827\r
828 //\r
829 // Second: apply relocations.\r
830 //\r
831 VerboseMsg ("Applying Relocations...");\r
832 for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {\r
833 //\r
834 // Determine if this is a relocation section.\r
835 //\r
836 Elf_Shdr *RelShdr = GetShdrByIndex(Idx);\r
837 if ((RelShdr->sh_type != SHT_REL) && (RelShdr->sh_type != SHT_RELA)) {\r
838 continue;\r
839 }\r
840\r
841 //\r
842 // If this is a ET_DYN (PIE) executable, we will encounter a dynamic SHT_RELA\r
843 // section that applies to the entire binary, and which will have its section\r
844 // index set to #0 (which is a NULL section with the SHF_ALLOC bit cleared).\r
845 //\r
846 // In the absence of GOT based relocations,\r
847 // this RELA section will contain redundant R_xxx_RELATIVE relocations, one\r
848 // for every R_xxx_xx64 relocation appearing in the per-section RELA sections.\r
849 // (i.e., .rela.text and .rela.data)\r
850 //\r
851 if (RelShdr->sh_info == 0) {\r
852 continue;\r
853 }\r
854\r
855 //\r
856 // Relocation section found. Now extract section information that the relocations\r
857 // apply to in the ELF data and the new COFF data.\r
858 //\r
859 SecShdr = GetShdrByIndex(RelShdr->sh_info);\r
860 SecOffset = mCoffSectionsOffset[RelShdr->sh_info];\r
861\r
862 //\r
863 // Only process relocations for the current filter type.\r
864 //\r
865 if (RelShdr->sh_type == SHT_RELA && (*Filter)(SecShdr)) {\r
866 UINT64 RelIdx;\r
867\r
868 //\r
869 // Determine the symbol table referenced by the relocation data.\r
870 //\r
871 Elf_Shdr *SymtabShdr = GetShdrByIndex(RelShdr->sh_link);\r
872 UINT8 *Symtab = (UINT8*)mEhdr + SymtabShdr->sh_offset;\r
873\r
874 //\r
875 // Process all relocation entries for this section.\r
876 //\r
877 for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += (UINT32) RelShdr->sh_entsize) {\r
878\r
879 //\r
880 // Set pointer to relocation entry\r
881 //\r
882 Elf_Rela *Rel = (Elf_Rela *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx);\r
883\r
884 //\r
885 // Set pointer to symbol table entry associated with the relocation entry.\r
886 //\r
887 Elf_Sym *Sym = (Elf_Sym *)(Symtab + ELF_R_SYM(Rel->r_info) * SymtabShdr->sh_entsize);\r
888\r
889 Elf_Shdr *SymShdr;\r
890 UINT8 *Targ;\r
891\r
892 //\r
893 // Check section header index found in symbol table and get the section\r
894 // header location.\r
895 //\r
896 if (Sym->st_shndx == SHN_UNDEF\r
897 || Sym->st_shndx >= mEhdr->e_shnum) {\r
898 const UINT8 *SymName = GetSymName(Sym);\r
899 if (SymName == NULL) {\r
900 SymName = (const UINT8 *)"<unknown>";\r
901 }\r
902\r
903 Error (NULL, 0, 3000, "Invalid",\r
904 "%s: Bad definition for symbol '%s'@%#llx or unsupported symbol type. "\r
905 "For example, absolute and undefined symbols are not supported.",\r
906 mInImageName, SymName, Sym->st_value);\r
907\r
908 exit(EXIT_FAILURE);\r
909 }\r
910 SymShdr = GetShdrByIndex(Sym->st_shndx);\r
911\r
912 //\r
913 // Convert the relocation data to a pointer into the coff file.\r
914 //\r
915 // Note:\r
916 // r_offset is the virtual address of the storage unit to be relocated.\r
917 // sh_addr is the virtual address for the base of the section.\r
918 //\r
919 // r_offset in a memory address.\r
920 // Convert it to a pointer in the coff file.\r
921 //\r
922 Targ = mCoffFile + SecOffset + (Rel->r_offset - SecShdr->sh_addr);\r
923\r
924 //\r
925 // Determine how to handle each relocation type based on the machine type.\r
926 //\r
927 if (mEhdr->e_machine == EM_X86_64) {\r
928 switch (ELF_R_TYPE(Rel->r_info)) {\r
929 case R_X86_64_NONE:\r
930 break;\r
931 case R_X86_64_64:\r
932 //\r
933 // Absolute relocation.\r
934 //\r
935 VerboseMsg ("R_X86_64_64");\r
936 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",\r
937 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),\r
938 *(UINT64 *)Targ);\r
939 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];\r
940 VerboseMsg ("Relocation: 0x%016LX", *(UINT64*)Targ);\r
941 break;\r
942 case R_X86_64_32:\r
943 VerboseMsg ("R_X86_64_32");\r
944 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",\r
945 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),\r
946 *(UINT32 *)Targ);\r
947 *(UINT32 *)Targ = (UINT32)((UINT64)(*(UINT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);\r
948 VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);\r
949 break;\r
950 case R_X86_64_32S:\r
951 VerboseMsg ("R_X86_64_32S");\r
952 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",\r
953 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),\r
954 *(UINT32 *)Targ);\r
955 *(INT32 *)Targ = (INT32)((INT64)(*(INT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);\r
956 VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);\r
957 break;\r
958\r
959 case R_X86_64_PLT32:\r
960 //\r
961 // Treat R_X86_64_PLT32 relocations as R_X86_64_PC32: this is\r
962 // possible since we know all code symbol references resolve to\r
963 // definitions in the same module (UEFI has no shared libraries),\r
964 // and so there is never a reason to jump via a PLT entry,\r
965 // allowing us to resolve the reference using the symbol directly.\r
966 //\r
967 VerboseMsg ("Treating R_X86_64_PLT32 as R_X86_64_PC32 ...");\r
968 /* fall through */\r
969 case R_X86_64_PC32:\r
970 //\r
971 // Relative relocation: Symbol - Ip + Addend\r
972 //\r
973 VerboseMsg ("R_X86_64_PC32");\r
974 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",\r
975 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),\r
976 *(UINT32 *)Targ);\r
977 *(UINT32 *)Targ = (UINT32) (*(UINT32 *)Targ\r
978 + (mCoffSectionsOffset[Sym->st_shndx] - SymShdr->sh_addr)\r
979 - (SecOffset - SecShdr->sh_addr));\r
980 VerboseMsg ("Relocation: 0x%08X", *(UINT32 *)Targ);\r
981 break;\r
982 case R_X86_64_GOTPCREL:\r
983 case R_X86_64_GOTPCRELX:\r
984 case R_X86_64_REX_GOTPCRELX:\r
985 VerboseMsg ("R_X86_64_GOTPCREL family");\r
986 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",\r
987 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),\r
988 *(UINT32 *)Targ);\r
989 GOTEntryRva = Rel->r_offset - Rel->r_addend + *(INT32 *)Targ;\r
990 FindElfGOTSectionFromGOTEntryElfRva(GOTEntryRva);\r
991 *(UINT32 *)Targ = (UINT32) (*(UINT32 *)Targ\r
992 + (mCoffSectionsOffset[mGOTShindex] - mGOTShdr->sh_addr)\r
993 - (SecOffset - SecShdr->sh_addr));\r
994 VerboseMsg ("Relocation: 0x%08X", *(UINT32 *)Targ);\r
995 GOTEntryRva += (mCoffSectionsOffset[mGOTShindex] - mGOTShdr->sh_addr); // ELF Rva -> COFF Rva\r
996 if (AccumulateCoffGOTEntries((UINT32)GOTEntryRva)) {\r
997 //\r
998 // Relocate GOT entry if it's the first time we run into it\r
999 //\r
1000 Targ = mCoffFile + GOTEntryRva;\r
1001 //\r
1002 // Limitation: The following three statements assume memory\r
1003 // at *Targ is valid because the section containing the GOT\r
1004 // has already been copied from the ELF image to the Coff image.\r
1005 // This pre-condition presently holds because the GOT is placed\r
1006 // in section .text, and the ELF text sections are all copied\r
1007 // prior to reaching this point.\r
1008 // If the pre-condition is violated in the future, this fixup\r
1009 // either needs to be deferred after the GOT section is copied\r
1010 // to the Coff image, or the fixup should be performed on the\r
1011 // source Elf image instead of the destination Coff image.\r
1012 //\r
1013 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",\r
1014 (UINT32)GOTEntryRva,\r
1015 *(UINT64 *)Targ);\r
1016 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];\r
1017 VerboseMsg ("Relocation: 0x%016LX", *(UINT64*)Targ);\r
1018 }\r
1019 break;\r
1020 default:\r
1021 Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));\r
1022 }\r
1023 } else if (mEhdr->e_machine == EM_AARCH64) {\r
1024\r
1025 switch (ELF_R_TYPE(Rel->r_info)) {\r
1026\r
1027 case R_AARCH64_ADR_PREL_PG_HI21:\r
1028 //\r
1029 // AArch64 PG_H21 relocations are typically paired with ABS_LO12\r
1030 // relocations, where a PC-relative reference with +/- 4 GB range is\r
1031 // split into a relative high part and an absolute low part. Since\r
1032 // the absolute low part represents the offset into a 4 KB page, we\r
1033 // either have to convert the ADRP into an ADR instruction, or we\r
1034 // need to use a section alignment of at least 4 KB, so that the\r
1035 // binary appears at a correct offset at runtime. In any case, we\r
1036 // have to make sure that the 4 KB relative offsets of both the\r
1037 // section containing the reference as well as the section to which\r
1038 // it refers have not been changed during PE/COFF conversion (i.e.,\r
1039 // in ScanSections64() above).\r
1040 //\r
1041 if (mCoffAlignment < 0x1000) {\r
1042 //\r
1043 // Attempt to convert the ADRP into an ADR instruction.\r
1044 // This is only possible if the symbol is within +/- 1 MB.\r
1045 //\r
1046 INT64 Offset;\r
1047\r
1048 // Decode the ADRP instruction\r
1049 Offset = (INT32)((*(UINT32 *)Targ & 0xffffe0) << 8);\r
1050 Offset = (Offset << (6 - 5)) | ((*(UINT32 *)Targ & 0x60000000) >> (29 - 12));\r
1051\r
1052 //\r
1053 // ADRP offset is relative to the previous page boundary,\r
1054 // whereas ADR offset is relative to the instruction itself.\r
1055 // So fix up the offset so it points to the page containing\r
1056 // the symbol.\r
1057 //\r
1058 Offset -= (UINTN)(Targ - mCoffFile) & 0xfff;\r
1059\r
1060 if (Offset < -0x100000 || Offset > 0xfffff) {\r
1061 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s due to its size (> 1 MB), this module requires 4 KB section alignment.",\r
1062 mInImageName);\r
1063 break;\r
1064 }\r
1065\r
1066 // Re-encode the offset as an ADR instruction\r
1067 *(UINT32 *)Targ &= 0x1000001f;\r
1068 *(UINT32 *)Targ |= ((Offset & 0x1ffffc) << (5 - 2)) | ((Offset & 0x3) << 29);\r
1069 }\r
1070 /* fall through */\r
1071\r
1072 case R_AARCH64_ADD_ABS_LO12_NC:\r
1073 case R_AARCH64_LDST8_ABS_LO12_NC:\r
1074 case R_AARCH64_LDST16_ABS_LO12_NC:\r
1075 case R_AARCH64_LDST32_ABS_LO12_NC:\r
1076 case R_AARCH64_LDST64_ABS_LO12_NC:\r
1077 case R_AARCH64_LDST128_ABS_LO12_NC:\r
1078 if (((SecShdr->sh_addr ^ SecOffset) & 0xfff) != 0 ||\r
1079 ((SymShdr->sh_addr ^ mCoffSectionsOffset[Sym->st_shndx]) & 0xfff) != 0) {\r
1080 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 small code model requires identical ELF and PE/COFF section offsets modulo 4 KB.",\r
1081 mInImageName);\r
1082 break;\r
1083 }\r
1084 /* fall through */\r
1085\r
1086 case R_AARCH64_ADR_PREL_LO21:\r
1087 case R_AARCH64_CONDBR19:\r
1088 case R_AARCH64_LD_PREL_LO19:\r
1089 case R_AARCH64_CALL26:\r
1090 case R_AARCH64_JUMP26:\r
1091 case R_AARCH64_PREL64:\r
1092 case R_AARCH64_PREL32:\r
1093 case R_AARCH64_PREL16:\r
1094 //\r
1095 // The GCC toolchains (i.e., binutils) may corrupt section relative\r
1096 // relocations when emitting relocation sections into fully linked\r
1097 // binaries. More specifically, they tend to fail to take into\r
1098 // account the fact that a '.rodata + XXX' relocation needs to have\r
1099 // its addend recalculated once .rodata is merged into the .text\r
1100 // section, and the relocation emitted into the .rela.text section.\r
1101 //\r
1102 // We cannot really recover from this loss of information, so the\r
1103 // only workaround is to prevent having to recalculate any relative\r
1104 // relocations at all, by using a linker script that ensures that\r
1105 // the offset between the Place and the Symbol is the same in both\r
1106 // the ELF and the PE/COFF versions of the binary.\r
1107 //\r
1108 if ((SymShdr->sh_addr - SecShdr->sh_addr) !=\r
1109 (mCoffSectionsOffset[Sym->st_shndx] - SecOffset)) {\r
1110 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s AARCH64 relative relocations require identical ELF and PE/COFF section offsets",\r
1111 mInImageName);\r
1112 }\r
1113 break;\r
1114\r
1115 // Absolute relocations.\r
1116 case R_AARCH64_ABS64:\r
1117 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];\r
1118 break;\r
1119\r
1120 default:\r
1121 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));\r
1122 }\r
1123 } else {\r
1124 Error (NULL, 0, 3000, "Invalid", "Not a supported machine type");\r
1125 }\r
1126 }\r
1127 }\r
1128 }\r
1129\r
1130 return TRUE;\r
1131}\r
1132\r
1133STATIC\r
1134VOID\r
1135WriteRelocations64 (\r
1136 VOID\r
1137 )\r
1138{\r
1139 UINT32 Index;\r
1140 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;\r
1141 EFI_IMAGE_DATA_DIRECTORY *Dir;\r
1142\r
1143 for (Index = 0; Index < mEhdr->e_shnum; Index++) {\r
1144 Elf_Shdr *RelShdr = GetShdrByIndex(Index);\r
1145 if ((RelShdr->sh_type == SHT_REL) || (RelShdr->sh_type == SHT_RELA)) {\r
1146 Elf_Shdr *SecShdr = GetShdrByIndex (RelShdr->sh_info);\r
1147 if (IsTextShdr(SecShdr) || IsDataShdr(SecShdr)) {\r
1148 UINT64 RelIdx;\r
1149\r
1150 for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += RelShdr->sh_entsize) {\r
1151 Elf_Rela *Rel = (Elf_Rela *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx);\r
1152\r
1153 if (mEhdr->e_machine == EM_X86_64) {\r
1154 switch (ELF_R_TYPE(Rel->r_info)) {\r
1155 case R_X86_64_NONE:\r
1156 case R_X86_64_PC32:\r
1157 case R_X86_64_PLT32:\r
1158 case R_X86_64_GOTPCREL:\r
1159 case R_X86_64_GOTPCRELX:\r
1160 case R_X86_64_REX_GOTPCRELX:\r
1161 break;\r
1162 case R_X86_64_64:\r
1163 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",\r
1164 mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr));\r
1165 CoffAddFixup(\r
1166 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]\r
1167 + (Rel->r_offset - SecShdr->sh_addr)),\r
1168 EFI_IMAGE_REL_BASED_DIR64);\r
1169 break;\r
1170 //\r
1171 // R_X86_64_32 and R_X86_64_32S are ELF64 relocations emitted when using\r
1172 // the SYSV X64 ABI small non-position-independent code model.\r
1173 // R_X86_64_32 is used for unsigned 32-bit immediates with a 32-bit operand\r
1174 // size. The value is either not extended, or zero-extended to 64 bits.\r
1175 // R_X86_64_32S is used for either signed 32-bit non-rip-relative displacements\r
1176 // or signed 32-bit immediates with a 64-bit operand size. The value is\r
1177 // sign-extended to 64 bits.\r
1178 // EFI_IMAGE_REL_BASED_HIGHLOW is a PE relocation that uses 32-bit arithmetic\r
1179 // for rebasing an image.\r
1180 // EFI PE binaries declare themselves EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE and\r
1181 // may load above 2GB. If an EFI PE binary with a converted R_X86_64_32S\r
1182 // relocation is loaded above 2GB, the value will get sign-extended to the\r
1183 // negative part of the 64-bit address space. The negative part of the 64-bit\r
1184 // address space is unmapped, so accessing such an address page-faults.\r
1185 // In order to support R_X86_64_32S, it is necessary to unset\r
1186 // EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE, and the EFI PE loader must implement\r
1187 // this flag and abstain from loading such a PE binary above 2GB.\r
1188 // Since this feature is not supported, support for R_X86_64_32S (and hence\r
1189 // the small non-position-independent code model) is disabled.\r
1190 //\r
1191 // case R_X86_64_32S:\r
1192 case R_X86_64_32:\r
1193 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",\r
1194 mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr));\r
1195 CoffAddFixup(\r
1196 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]\r
1197 + (Rel->r_offset - SecShdr->sh_addr)),\r
1198 EFI_IMAGE_REL_BASED_HIGHLOW);\r
1199 break;\r
1200 default:\r
1201 Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));\r
1202 }\r
1203 } else if (mEhdr->e_machine == EM_AARCH64) {\r
1204\r
1205 switch (ELF_R_TYPE(Rel->r_info)) {\r
1206 case R_AARCH64_ADR_PREL_LO21:\r
1207 case R_AARCH64_CONDBR19:\r
1208 case R_AARCH64_LD_PREL_LO19:\r
1209 case R_AARCH64_CALL26:\r
1210 case R_AARCH64_JUMP26:\r
1211 case R_AARCH64_PREL64:\r
1212 case R_AARCH64_PREL32:\r
1213 case R_AARCH64_PREL16:\r
1214 case R_AARCH64_ADR_PREL_PG_HI21:\r
1215 case R_AARCH64_ADD_ABS_LO12_NC:\r
1216 case R_AARCH64_LDST8_ABS_LO12_NC:\r
1217 case R_AARCH64_LDST16_ABS_LO12_NC:\r
1218 case R_AARCH64_LDST32_ABS_LO12_NC:\r
1219 case R_AARCH64_LDST64_ABS_LO12_NC:\r
1220 case R_AARCH64_LDST128_ABS_LO12_NC:\r
1221 //\r
1222 // No fixups are required for relative relocations, provided that\r
1223 // the relative offsets between sections have been preserved in\r
1224 // the ELF to PE/COFF conversion. We have already asserted that\r
1225 // this is the case in WriteSections64 ().\r
1226 //\r
1227 break;\r
1228\r
1229 case R_AARCH64_ABS64:\r
1230 CoffAddFixup(\r
1231 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]\r
1232 + (Rel->r_offset - SecShdr->sh_addr)),\r
1233 EFI_IMAGE_REL_BASED_DIR64);\r
1234 break;\r
1235\r
1236 case R_AARCH64_ABS32:\r
1237 CoffAddFixup(\r
1238 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]\r
1239 + (Rel->r_offset - SecShdr->sh_addr)),\r
1240 EFI_IMAGE_REL_BASED_HIGHLOW);\r
1241 break;\r
1242\r
1243 default:\r
1244 Error (NULL, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));\r
1245 }\r
1246 } else {\r
1247 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
1248 }\r
1249 }\r
1250 if (mEhdr->e_machine == EM_X86_64 && RelShdr->sh_info == mGOTShindex) {\r
1251 //\r
1252 // Tack relocations for GOT entries after other relocations for\r
1253 // the section the GOT is in, as it's usually found at the end\r
1254 // of the section. This is done in order to maintain Rva order\r
1255 // of Coff relocations.\r
1256 //\r
1257 EmitGOTRelocations();\r
1258 }\r
1259 }\r
1260 }\r
1261 }\r
1262\r
1263 if (mEhdr->e_machine == EM_X86_64) {\r
1264 //\r
1265 // This is a safety net just in case the GOT is in a section\r
1266 // with no other relocations and the first invocation of\r
1267 // EmitGOTRelocations() above was skipped. This invocation\r
1268 // does not maintain Rva order of Coff relocations.\r
1269 // At present, with a single text section, all references to\r
1270 // the GOT and the GOT itself reside in section .text, so\r
1271 // if there's a GOT at all, the first invocation above\r
1272 // is executed.\r
1273 //\r
1274 EmitGOTRelocations();\r
1275 }\r
1276 //\r
1277 // Pad by adding empty entries.\r
1278 //\r
1279 while (mCoffOffset & (mCoffAlignment - 1)) {\r
1280 CoffAddFixupEntry(0);\r
1281 }\r
1282\r
1283 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);\r
1284 Dir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];\r
1285 Dir->Size = mCoffOffset - mRelocOffset;\r
1286 if (Dir->Size == 0) {\r
1287 // If no relocations, null out the directory entry and don't add the .reloc section\r
1288 Dir->VirtualAddress = 0;\r
1289 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;\r
1290 } else {\r
1291 Dir->VirtualAddress = mRelocOffset;\r
1292 CreateSectionHeader (".reloc", mRelocOffset, mCoffOffset - mRelocOffset,\r
1293 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA\r
1294 | EFI_IMAGE_SCN_MEM_DISCARDABLE\r
1295 | EFI_IMAGE_SCN_MEM_READ);\r
1296 }\r
1297}\r
1298\r
1299STATIC\r
1300VOID\r
1301WriteDebug64 (\r
1302 VOID\r
1303 )\r
1304{\r
1305 UINT32 Len;\r
1306 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;\r
1307 EFI_IMAGE_DATA_DIRECTORY *DataDir;\r
1308 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *Dir;\r
1309 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *Nb10;\r
1310\r
1311 Len = strlen(mInImageName) + 1;\r
1312\r
1313 Dir = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(mCoffFile + mDebugOffset);\r
1314 Dir->Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW;\r
1315 Dir->SizeOfData = sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + Len;\r
1316 Dir->RVA = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);\r
1317 Dir->FileOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);\r
1318\r
1319 Nb10 = (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY*)(Dir + 1);\r
1320 Nb10->Signature = CODEVIEW_SIGNATURE_NB10;\r
1321 strcpy ((char *)(Nb10 + 1), mInImageName);\r
1322\r
1323\r
1324 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);\r
1325 DataDir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG];\r
1326 DataDir->VirtualAddress = mDebugOffset;\r
1327 DataDir->Size = sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);\r
1328}\r
1329\r
1330STATIC\r
1331VOID\r
1332SetImageSize64 (\r
1333 VOID\r
1334 )\r
1335{\r
1336 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;\r
1337\r
1338 //\r
1339 // Set image size\r
1340 //\r
1341 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);\r
1342 NtHdr->Pe32Plus.OptionalHeader.SizeOfImage = mCoffOffset;\r
1343}\r
1344\r
1345STATIC\r
1346VOID\r
1347CleanUp64 (\r
1348 VOID\r
1349 )\r
1350{\r
1351 if (mCoffSectionsOffset != NULL) {\r
1352 free (mCoffSectionsOffset);\r
1353 }\r
1354}\r
1355\r
1356\r