]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/GenFw/Elf64Convert.c
License header updated to match correct format.
[mirror_edk2.git] / BaseTools / Source / C / GenFw / Elf64Convert.c
1 /** @file
2 Elf64 convert solution
3
4 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>
6
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License which accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include "WinNtInclude.h"
18
19 #ifndef __GNUC__
20 #include <windows.h>
21 #include <io.h>
22 #endif
23 #include <assert.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <ctype.h>
29
30 #include <Common/UefiBaseTypes.h>
31 #include <IndustryStandard/PeImage.h>
32
33 #include "PeCoffLib.h"
34 #include "EfiUtilityMsgs.h"
35
36 #include "GenFw.h"
37 #include "ElfConvert.h"
38 #include "Elf64Convert.h"
39
40 STATIC
41 VOID
42 ScanSections64 (
43 VOID
44 );
45
46 STATIC
47 BOOLEAN
48 WriteSections64 (
49 SECTION_FILTER_TYPES FilterType
50 );
51
52 STATIC
53 VOID
54 WriteRelocations64 (
55 VOID
56 );
57
58 STATIC
59 VOID
60 WriteDebug64 (
61 VOID
62 );
63
64 STATIC
65 VOID
66 SetImageSize64 (
67 VOID
68 );
69
70 STATIC
71 VOID
72 CleanUp64 (
73 VOID
74 );
75
76 //
77 // Rename ELF32 strucutres to common names to help when porting to ELF64.
78 //
79 typedef Elf64_Shdr Elf_Shdr;
80 typedef Elf64_Ehdr Elf_Ehdr;
81 typedef Elf64_Rel Elf_Rel;
82 typedef Elf64_Rela Elf_Rela;
83 typedef Elf64_Sym Elf_Sym;
84 typedef Elf64_Phdr Elf_Phdr;
85 typedef Elf64_Dyn Elf_Dyn;
86 #define ELFCLASS ELFCLASS64
87 #define ELF_R_TYPE(r) ELF64_R_TYPE(r)
88 #define ELF_R_SYM(r) ELF64_R_SYM(r)
89
90 //
91 // Well known ELF structures.
92 //
93 STATIC Elf_Ehdr *mEhdr;
94 STATIC Elf_Shdr *mShdrBase;
95 STATIC Elf_Phdr *mPhdrBase;
96
97 //
98 // Coff information
99 //
100 STATIC const UINT32 mCoffAlignment = 0x20;
101
102 //
103 // PE section alignment.
104 //
105 STATIC const UINT16 mCoffNbrSections = 5;
106
107 //
108 // ELF sections to offset in Coff file.
109 //
110 STATIC UINT32 *mCoffSectionsOffset = NULL;
111
112 //
113 // Offsets in COFF file
114 //
115 STATIC UINT32 mNtHdrOffset;
116 STATIC UINT32 mTextOffset;
117 STATIC UINT32 mDataOffset;
118 STATIC UINT32 mHiiRsrcOffset;
119 STATIC UINT32 mRelocOffset;
120
121 //
122 // Initialization Function
123 //
124 BOOLEAN
125 InitializeElf64 (
126 UINT8 *FileBuffer,
127 ELF_FUNCTION_TABLE *ElfFunctions
128 )
129 {
130 //
131 // Initialize data pointer and structures.
132 //
133 VerboseMsg ("Set EHDR");
134 mEhdr = (Elf_Ehdr*) FileBuffer;
135
136 //
137 // Check the ELF64 specific header information.
138 //
139 VerboseMsg ("Check ELF64 Header Information");
140 if (mEhdr->e_ident[EI_CLASS] != ELFCLASS64) {
141 Error (NULL, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS64");
142 return FALSE;
143 }
144 if (mEhdr->e_ident[EI_DATA] != ELFDATA2LSB) {
145 Error (NULL, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
146 return FALSE;
147 }
148 if ((mEhdr->e_type != ET_EXEC) && (mEhdr->e_type != ET_DYN)) {
149 Error (NULL, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
150 return FALSE;
151 }
152 if (!((mEhdr->e_machine == EM_X86_64) || (mEhdr->e_machine == EM_AARCH64))) {
153 Error (NULL, 0, 3000, "Unsupported", "ELF e_machine not EM_X86_64 or EM_AARCH64");
154 return FALSE;
155 }
156 if (mEhdr->e_version != EV_CURRENT) {
157 Error (NULL, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr->e_version, EV_CURRENT);
158 return FALSE;
159 }
160
161 //
162 // Update section header pointers
163 //
164 VerboseMsg ("Update Header Pointers");
165 mShdrBase = (Elf_Shdr *)((UINT8 *)mEhdr + mEhdr->e_shoff);
166 mPhdrBase = (Elf_Phdr *)((UINT8 *)mEhdr + mEhdr->e_phoff);
167
168 //
169 // Create COFF Section offset buffer and zero.
170 //
171 VerboseMsg ("Create COFF Section Offset Buffer");
172 mCoffSectionsOffset = (UINT32 *)malloc(mEhdr->e_shnum * sizeof (UINT32));
173 memset(mCoffSectionsOffset, 0, mEhdr->e_shnum * sizeof(UINT32));
174
175 //
176 // Fill in function pointers.
177 //
178 VerboseMsg ("Fill in Function Pointers");
179 ElfFunctions->ScanSections = ScanSections64;
180 ElfFunctions->WriteSections = WriteSections64;
181 ElfFunctions->WriteRelocations = WriteRelocations64;
182 ElfFunctions->WriteDebug = WriteDebug64;
183 ElfFunctions->SetImageSize = SetImageSize64;
184 ElfFunctions->CleanUp = CleanUp64;
185
186 return TRUE;
187 }
188
189
190 //
191 // Header by Index functions
192 //
193 STATIC
194 Elf_Shdr*
195 GetShdrByIndex (
196 UINT32 Num
197 )
198 {
199 if (Num >= mEhdr->e_shnum)
200 return NULL;
201 return (Elf_Shdr*)((UINT8*)mShdrBase + Num * mEhdr->e_shentsize);
202 }
203
204 STATIC
205 UINT32
206 CoffAlign (
207 UINT32 Offset
208 )
209 {
210 return (Offset + mCoffAlignment - 1) & ~(mCoffAlignment - 1);
211 }
212
213 //
214 // filter functions
215 //
216 STATIC
217 BOOLEAN
218 IsTextShdr (
219 Elf_Shdr *Shdr
220 )
221 {
222 return (BOOLEAN) ((Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == SHF_ALLOC);
223 }
224
225 STATIC
226 BOOLEAN
227 IsHiiRsrcShdr (
228 Elf_Shdr *Shdr
229 )
230 {
231 Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx);
232
233 return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_HII_SECTION_NAME) == 0);
234 }
235
236 STATIC
237 BOOLEAN
238 IsDataShdr (
239 Elf_Shdr *Shdr
240 )
241 {
242 if (IsHiiRsrcShdr(Shdr)) {
243 return FALSE;
244 }
245 return (BOOLEAN) (Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == (SHF_ALLOC | SHF_WRITE);
246 }
247
248 //
249 // Elf functions interface implementation
250 //
251
252 STATIC
253 VOID
254 ScanSections64 (
255 VOID
256 )
257 {
258 UINT32 i;
259 EFI_IMAGE_DOS_HEADER *DosHdr;
260 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
261 UINT32 CoffEntry;
262 UINT32 SectionCount;
263 BOOLEAN FoundText;
264
265 CoffEntry = 0;
266 mCoffOffset = 0;
267 mTextOffset = 0;
268 FoundText = FALSE;
269
270 //
271 // Coff file start with a DOS header.
272 //
273 mCoffOffset = sizeof(EFI_IMAGE_DOS_HEADER) + 0x40;
274 mNtHdrOffset = mCoffOffset;
275 switch (mEhdr->e_machine) {
276 case EM_X86_64:
277 case EM_IA_64:
278 case EM_AARCH64:
279 mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS64);
280 break;
281 default:
282 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN)mEhdr->e_machine);
283 mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS64);
284 break;
285 }
286
287 mTableOffset = mCoffOffset;
288 mCoffOffset += mCoffNbrSections * sizeof(EFI_IMAGE_SECTION_HEADER);
289
290 //
291 // First text sections.
292 //
293 mCoffOffset = CoffAlign(mCoffOffset);
294 SectionCount = 0;
295 for (i = 0; i < mEhdr->e_shnum; i++) {
296 Elf_Shdr *shdr = GetShdrByIndex(i);
297 if (IsTextShdr(shdr)) {
298 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
299 // the alignment field is valid
300 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
301 // if the section address is aligned we must align PE/COFF
302 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));
303 } else if ((shdr->sh_addr % shdr->sh_addralign) != (mCoffOffset % shdr->sh_addralign)) {
304 // ARM RVCT tools have behavior outside of the ELF specification to try
305 // and make images smaller. If sh_addr is not aligned to sh_addralign
306 // then the section needs to preserve sh_addr MOD sh_addralign.
307 // Normally doing nothing here works great.
308 Error (NULL, 0, 3000, "Invalid", "Unsupported section alignment.");
309 }
310 }
311
312 /* Relocate entry. */
313 if ((mEhdr->e_entry >= shdr->sh_addr) &&
314 (mEhdr->e_entry < shdr->sh_addr + shdr->sh_size)) {
315 CoffEntry = (UINT32) (mCoffOffset + mEhdr->e_entry - shdr->sh_addr);
316 }
317
318 //
319 // Set mTextOffset with the offset of the first '.text' section
320 //
321 if (!FoundText) {
322 mTextOffset = mCoffOffset;
323 FoundText = TRUE;
324 }
325
326 mCoffSectionsOffset[i] = mCoffOffset;
327 mCoffOffset += (UINT32) shdr->sh_size;
328 SectionCount ++;
329 }
330 }
331
332 if (!FoundText) {
333 Error (NULL, 0, 3000, "Invalid", "Did not find any '.text' section.");
334 assert (FALSE);
335 }
336
337 if (mEhdr->e_machine != EM_ARM) {
338 mCoffOffset = CoffAlign(mCoffOffset);
339 }
340
341 if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {
342 Warning (NULL, 0, 0, NULL, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName);
343 }
344
345 //
346 // Then data sections.
347 //
348 mDataOffset = mCoffOffset;
349 SectionCount = 0;
350 for (i = 0; i < mEhdr->e_shnum; i++) {
351 Elf_Shdr *shdr = GetShdrByIndex(i);
352 if (IsDataShdr(shdr)) {
353 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
354 // the alignment field is valid
355 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
356 // if the section address is aligned we must align PE/COFF
357 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));
358 } else if ((shdr->sh_addr % shdr->sh_addralign) != (mCoffOffset % shdr->sh_addralign)) {
359 // ARM RVCT tools have behavior outside of the ELF specification to try
360 // and make images smaller. If sh_addr is not aligned to sh_addralign
361 // then the section needs to preserve sh_addr MOD sh_addralign.
362 // Normally doing nothing here works great.
363 Error (NULL, 0, 3000, "Invalid", "Unsupported section alignment.");
364 }
365 }
366 mCoffSectionsOffset[i] = mCoffOffset;
367 mCoffOffset += (UINT32) shdr->sh_size;
368 SectionCount ++;
369 }
370 }
371 mCoffOffset = CoffAlign(mCoffOffset);
372
373 if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {
374 Warning (NULL, 0, 0, NULL, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName);
375 }
376
377 //
378 // The HII resource sections.
379 //
380 mHiiRsrcOffset = mCoffOffset;
381 for (i = 0; i < mEhdr->e_shnum; i++) {
382 Elf_Shdr *shdr = GetShdrByIndex(i);
383 if (IsHiiRsrcShdr(shdr)) {
384 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
385 // the alignment field is valid
386 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
387 // if the section address is aligned we must align PE/COFF
388 mCoffOffset = (UINT32) ((mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1));
389 } else if ((shdr->sh_addr % shdr->sh_addralign) != (mCoffOffset % shdr->sh_addralign)) {
390 // ARM RVCT tools have behavior outside of the ELF specification to try
391 // and make images smaller. If sh_addr is not aligned to sh_addralign
392 // then the section needs to preserve sh_addr MOD sh_addralign.
393 // Normally doing nothing here works great.
394 Error (NULL, 0, 3000, "Invalid", "Unsupported section alignment.");
395 }
396 }
397 if (shdr->sh_size != 0) {
398 mCoffSectionsOffset[i] = mCoffOffset;
399 mCoffOffset += (UINT32) shdr->sh_size;
400 mCoffOffset = CoffAlign(mCoffOffset);
401 SetHiiResourceHeader ((UINT8*) mEhdr + shdr->sh_offset, mHiiRsrcOffset);
402 }
403 break;
404 }
405 }
406
407 mRelocOffset = mCoffOffset;
408
409 //
410 // Allocate base Coff file. Will be expanded later for relocations.
411 //
412 mCoffFile = (UINT8 *)malloc(mCoffOffset);
413 memset(mCoffFile, 0, mCoffOffset);
414
415 //
416 // Fill headers.
417 //
418 DosHdr = (EFI_IMAGE_DOS_HEADER *)mCoffFile;
419 DosHdr->e_magic = EFI_IMAGE_DOS_SIGNATURE;
420 DosHdr->e_lfanew = mNtHdrOffset;
421
422 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION*)(mCoffFile + mNtHdrOffset);
423
424 NtHdr->Pe32Plus.Signature = EFI_IMAGE_NT_SIGNATURE;
425
426 switch (mEhdr->e_machine) {
427 case EM_X86_64:
428 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_X64;
429 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
430 break;
431 case EM_IA_64:
432 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_IPF;
433 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
434 break;
435 case EM_AARCH64:
436 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_AARCH64;
437 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
438 break;
439 default:
440 VerboseMsg ("%s unknown e_machine type. Assume X64", (UINTN)mEhdr->e_machine);
441 NtHdr->Pe32Plus.FileHeader.Machine = EFI_IMAGE_MACHINE_X64;
442 NtHdr->Pe32Plus.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;
443 }
444
445 NtHdr->Pe32Plus.FileHeader.NumberOfSections = mCoffNbrSections;
446 NtHdr->Pe32Plus.FileHeader.TimeDateStamp = (UINT32) time(NULL);
447 mImageTimeStamp = NtHdr->Pe32Plus.FileHeader.TimeDateStamp;
448 NtHdr->Pe32Plus.FileHeader.PointerToSymbolTable = 0;
449 NtHdr->Pe32Plus.FileHeader.NumberOfSymbols = 0;
450 NtHdr->Pe32Plus.FileHeader.SizeOfOptionalHeader = sizeof(NtHdr->Pe32Plus.OptionalHeader);
451 NtHdr->Pe32Plus.FileHeader.Characteristics = EFI_IMAGE_FILE_EXECUTABLE_IMAGE
452 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
453 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
454 | EFI_IMAGE_FILE_LARGE_ADDRESS_AWARE;
455
456 NtHdr->Pe32Plus.OptionalHeader.SizeOfCode = mDataOffset - mTextOffset;
457 NtHdr->Pe32Plus.OptionalHeader.SizeOfInitializedData = mRelocOffset - mDataOffset;
458 NtHdr->Pe32Plus.OptionalHeader.SizeOfUninitializedData = 0;
459 NtHdr->Pe32Plus.OptionalHeader.AddressOfEntryPoint = CoffEntry;
460
461 NtHdr->Pe32Plus.OptionalHeader.BaseOfCode = mTextOffset;
462
463 NtHdr->Pe32Plus.OptionalHeader.ImageBase = 0;
464 NtHdr->Pe32Plus.OptionalHeader.SectionAlignment = mCoffAlignment;
465 NtHdr->Pe32Plus.OptionalHeader.FileAlignment = mCoffAlignment;
466 NtHdr->Pe32Plus.OptionalHeader.SizeOfImage = 0;
467
468 NtHdr->Pe32Plus.OptionalHeader.SizeOfHeaders = mTextOffset;
469 NtHdr->Pe32Plus.OptionalHeader.NumberOfRvaAndSizes = EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES;
470
471 //
472 // Section headers.
473 //
474 if ((mDataOffset - mTextOffset) > 0) {
475 CreateSectionHeader (".text", mTextOffset, mDataOffset - mTextOffset,
476 EFI_IMAGE_SCN_CNT_CODE
477 | EFI_IMAGE_SCN_MEM_EXECUTE
478 | EFI_IMAGE_SCN_MEM_READ);
479 } else {
480 // Don't make a section of size 0.
481 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
482 }
483
484 if ((mHiiRsrcOffset - mDataOffset) > 0) {
485 CreateSectionHeader (".data", mDataOffset, mHiiRsrcOffset - mDataOffset,
486 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
487 | EFI_IMAGE_SCN_MEM_WRITE
488 | EFI_IMAGE_SCN_MEM_READ);
489 } else {
490 // Don't make a section of size 0.
491 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
492 }
493
494 if ((mRelocOffset - mHiiRsrcOffset) > 0) {
495 CreateSectionHeader (".rsrc", mHiiRsrcOffset, mRelocOffset - mHiiRsrcOffset,
496 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
497 | EFI_IMAGE_SCN_MEM_READ);
498
499 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = mRelocOffset - mHiiRsrcOffset;
500 NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = mHiiRsrcOffset;
501 } else {
502 // Don't make a section of size 0.
503 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
504 }
505
506 }
507
508 STATIC
509 BOOLEAN
510 WriteSections64 (
511 SECTION_FILTER_TYPES FilterType
512 )
513 {
514 UINT32 Idx;
515 Elf_Shdr *SecShdr;
516 UINT32 SecOffset;
517 BOOLEAN (*Filter)(Elf_Shdr *);
518
519 //
520 // Initialize filter pointer
521 //
522 switch (FilterType) {
523 case SECTION_TEXT:
524 Filter = IsTextShdr;
525 break;
526 case SECTION_HII:
527 Filter = IsHiiRsrcShdr;
528 break;
529 case SECTION_DATA:
530 Filter = IsDataShdr;
531 break;
532 default:
533 return FALSE;
534 }
535
536 //
537 // First: copy sections.
538 //
539 for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {
540 Elf_Shdr *Shdr = GetShdrByIndex(Idx);
541 if ((*Filter)(Shdr)) {
542 switch (Shdr->sh_type) {
543 case SHT_PROGBITS:
544 /* Copy. */
545 memcpy(mCoffFile + mCoffSectionsOffset[Idx],
546 (UINT8*)mEhdr + Shdr->sh_offset,
547 (size_t) Shdr->sh_size);
548 break;
549
550 case SHT_NOBITS:
551 memset(mCoffFile + mCoffSectionsOffset[Idx], 0, (size_t) Shdr->sh_size);
552 break;
553
554 default:
555 //
556 // Ignore for unkown section type.
557 //
558 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName, (unsigned)Shdr->sh_type);
559 break;
560 }
561 }
562 }
563
564 //
565 // Second: apply relocations.
566 //
567 VerboseMsg ("Applying Relocations...");
568 for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {
569 //
570 // Determine if this is a relocation section.
571 //
572 Elf_Shdr *RelShdr = GetShdrByIndex(Idx);
573 if ((RelShdr->sh_type != SHT_REL) && (RelShdr->sh_type != SHT_RELA)) {
574 continue;
575 }
576
577 //
578 // Relocation section found. Now extract section information that the relocations
579 // apply to in the ELF data and the new COFF data.
580 //
581 SecShdr = GetShdrByIndex(RelShdr->sh_info);
582 SecOffset = mCoffSectionsOffset[RelShdr->sh_info];
583
584 //
585 // Only process relocations for the current filter type.
586 //
587 if (RelShdr->sh_type == SHT_RELA && (*Filter)(SecShdr)) {
588 UINT64 RelIdx;
589
590 //
591 // Determine the symbol table referenced by the relocation data.
592 //
593 Elf_Shdr *SymtabShdr = GetShdrByIndex(RelShdr->sh_link);
594 UINT8 *Symtab = (UINT8*)mEhdr + SymtabShdr->sh_offset;
595
596 //
597 // Process all relocation entries for this section.
598 //
599 for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += (UINT32) RelShdr->sh_entsize) {
600
601 //
602 // Set pointer to relocation entry
603 //
604 Elf_Rela *Rel = (Elf_Rela *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx);
605
606 //
607 // Set pointer to symbol table entry associated with the relocation entry.
608 //
609 Elf_Sym *Sym = (Elf_Sym *)(Symtab + ELF_R_SYM(Rel->r_info) * SymtabShdr->sh_entsize);
610
611 Elf_Shdr *SymShdr;
612 UINT8 *Targ;
613
614 //
615 // Check section header index found in symbol table and get the section
616 // header location.
617 //
618 if (Sym->st_shndx == SHN_UNDEF
619 || Sym->st_shndx == SHN_ABS
620 || Sym->st_shndx > mEhdr->e_shnum) {
621 Error (NULL, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName);
622 }
623 SymShdr = GetShdrByIndex(Sym->st_shndx);
624
625 //
626 // Convert the relocation data to a pointer into the coff file.
627 //
628 // Note:
629 // r_offset is the virtual address of the storage unit to be relocated.
630 // sh_addr is the virtual address for the base of the section.
631 //
632 // r_offset in a memory address.
633 // Convert it to a pointer in the coff file.
634 //
635 Targ = mCoffFile + SecOffset + (Rel->r_offset - SecShdr->sh_addr);
636
637 //
638 // Determine how to handle each relocation type based on the machine type.
639 //
640 if (mEhdr->e_machine == EM_X86_64) {
641 switch (ELF_R_TYPE(Rel->r_info)) {
642 case R_X86_64_NONE:
643 break;
644 case R_X86_64_64:
645 //
646 // Absolute relocation.
647 //
648 VerboseMsg ("R_X86_64_64");
649 VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
650 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
651 *(UINT64 *)Targ);
652 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
653 VerboseMsg ("Relocation: 0x%016LX", *(UINT64*)Targ);
654 break;
655 case R_X86_64_32:
656 VerboseMsg ("R_X86_64_32");
657 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
658 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
659 *(UINT32 *)Targ);
660 *(UINT32 *)Targ = (UINT32)((UINT64)(*(UINT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
661 VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);
662 break;
663 case R_X86_64_32S:
664 VerboseMsg ("R_X86_64_32S");
665 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
666 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
667 *(UINT32 *)Targ);
668 *(INT32 *)Targ = (INT32)((INT64)(*(INT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
669 VerboseMsg ("Relocation: 0x%08X", *(UINT32*)Targ);
670 break;
671 case R_X86_64_PC32:
672 //
673 // Relative relocation: Symbol - Ip + Addend
674 //
675 VerboseMsg ("R_X86_64_PC32");
676 VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
677 (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)),
678 *(UINT32 *)Targ);
679 *(UINT32 *)Targ = (UINT32) (*(UINT32 *)Targ
680 + (mCoffSectionsOffset[Sym->st_shndx] - SymShdr->sh_addr)
681 - (SecOffset - SecShdr->sh_addr));
682 VerboseMsg ("Relocation: 0x%08X", *(UINT32 *)Targ);
683 break;
684 default:
685 Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
686 }
687 } else if (mEhdr->e_machine == EM_AARCH64) {
688
689 // AARCH64 GCC uses RELA relocation, so all relocations have to be fixed up.
690 // As opposed to ARM32 using REL.
691
692 switch (ELF_R_TYPE(Rel->r_info)) {
693
694 case R_AARCH64_ADR_PREL_LO21:
695 if (Rel->r_addend != 0 ) { /* TODO */
696 Error (NULL, 0, 3000, "Invalid", "AArch64: R_AARCH64_ADR_PREL_LO21 Need to fixup with addend!.");
697 }
698 break;
699
700 case R_AARCH64_CONDBR19:
701 if (Rel->r_addend != 0 ) { /* TODO */
702 Error (NULL, 0, 3000, "Invalid", "AArch64: R_AARCH64_CONDBR19 Need to fixup with addend!.");
703 }
704 break;
705
706 case R_AARCH64_LD_PREL_LO19:
707 if (Rel->r_addend != 0 ) { /* TODO */
708 Error (NULL, 0, 3000, "Invalid", "AArch64: R_AARCH64_LD_PREL_LO19 Need to fixup with addend!.");
709 }
710 break;
711
712 case R_AARCH64_CALL26:
713 if (Rel->r_addend != 0 ) { /* TODO */
714 Error (NULL, 0, 3000, "Invalid", "AArch64: R_AARCH64_CALL26 Need to fixup with addend!.");
715 }
716 break;
717
718 case R_AARCH64_JUMP26:
719 if (Rel->r_addend != 0 ) { /* TODO : AArch64 '-O2' optimisation. */
720 Error (NULL, 0, 3000, "Invalid", "AArch64: R_AARCH64_JUMP26 Need to fixup with addend!.");
721 }
722 break;
723
724 case R_AARCH64_ADR_PREL_PG_HI21:
725 // TODO : AArch64 'small' memory model.
726 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADR_PREL_PG_HI21.", mInImageName);
727 break;
728
729 case R_AARCH64_ADD_ABS_LO12_NC:
730 // TODO : AArch64 'small' memory model.
731 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADD_ABS_LO12_NC.", mInImageName);
732 break;
733
734 // Absolute relocations.
735 case R_AARCH64_ABS64:
736 *(UINT64 *)Targ = *(UINT64 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
737 break;
738
739 default:
740 Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
741 }
742 } else {
743 Error (NULL, 0, 3000, "Invalid", "Not a supported machine type");
744 }
745 }
746 }
747 }
748
749 return TRUE;
750 }
751
752 STATIC
753 VOID
754 WriteRelocations64 (
755 VOID
756 )
757 {
758 UINT32 Index;
759 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
760 EFI_IMAGE_DATA_DIRECTORY *Dir;
761
762 for (Index = 0; Index < mEhdr->e_shnum; Index++) {
763 Elf_Shdr *RelShdr = GetShdrByIndex(Index);
764 if ((RelShdr->sh_type == SHT_REL) || (RelShdr->sh_type == SHT_RELA)) {
765 Elf_Shdr *SecShdr = GetShdrByIndex (RelShdr->sh_info);
766 if (IsTextShdr(SecShdr) || IsDataShdr(SecShdr)) {
767 UINT64 RelIdx;
768
769 for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += RelShdr->sh_entsize) {
770 Elf_Rela *Rel = (Elf_Rela *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx);
771
772 if (mEhdr->e_machine == EM_X86_64) {
773 switch (ELF_R_TYPE(Rel->r_info)) {
774 case R_X86_64_NONE:
775 case R_X86_64_PC32:
776 break;
777 case R_X86_64_64:
778 VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",
779 mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr));
780 CoffAddFixup(
781 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
782 + (Rel->r_offset - SecShdr->sh_addr)),
783 EFI_IMAGE_REL_BASED_DIR64);
784 break;
785 case R_X86_64_32S:
786 case R_X86_64_32:
787 VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",
788 mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr));
789 CoffAddFixup(
790 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
791 + (Rel->r_offset - SecShdr->sh_addr)),
792 EFI_IMAGE_REL_BASED_HIGHLOW);
793 break;
794 default:
795 Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
796 }
797 } else if (mEhdr->e_machine == EM_AARCH64) {
798 // AArch64 GCC uses RELA relocation, so all relocations has to be fixed up. ARM32 uses REL.
799 switch (ELF_R_TYPE(Rel->r_info)) {
800 case R_AARCH64_ADR_PREL_LO21:
801 break;
802
803 case R_AARCH64_CONDBR19:
804 break;
805
806 case R_AARCH64_LD_PREL_LO19:
807 break;
808
809 case R_AARCH64_CALL26:
810 break;
811
812 case R_AARCH64_JUMP26:
813 break;
814
815 case R_AARCH64_ADR_PREL_PG_HI21:
816 // TODO : AArch64 'small' memory model.
817 Error (NULL, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADR_PREL_PG_HI21.", mInImageName);
818 break;
819
820 case R_AARCH64_ADD_ABS_LO12_NC:
821 // TODO : AArch64 'small' memory model.
822 Error (NULL, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation R_AARCH64_ADD_ABS_LO12_NC.", mInImageName);
823 break;
824
825 case R_AARCH64_ABS64:
826 CoffAddFixup(
827 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
828 + (Rel->r_offset - SecShdr->sh_addr)),
829 EFI_IMAGE_REL_BASED_DIR64);
830 break;
831
832 case R_AARCH64_ABS32:
833 CoffAddFixup(
834 (UINT32) ((UINT64) mCoffSectionsOffset[RelShdr->sh_info]
835 + (Rel->r_offset - SecShdr->sh_addr)),
836 EFI_IMAGE_REL_BASED_HIGHLOW);
837 break;
838
839 default:
840 Error (NULL, 0, 3000, "Invalid", "WriteRelocations64(): %s unsupported ELF EM_AARCH64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
841 }
842 } else {
843 Error (NULL, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr->e_machine);
844 }
845 }
846 }
847 }
848 }
849
850 //
851 // Pad by adding empty entries.
852 //
853 while (mCoffOffset & (mCoffAlignment - 1)) {
854 CoffAddFixupEntry(0);
855 }
856
857 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
858 Dir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
859 Dir->Size = mCoffOffset - mRelocOffset;
860 if (Dir->Size == 0) {
861 // If no relocations, null out the directory entry and don't add the .reloc section
862 Dir->VirtualAddress = 0;
863 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
864 } else {
865 Dir->VirtualAddress = mRelocOffset;
866 CreateSectionHeader (".reloc", mRelocOffset, mCoffOffset - mRelocOffset,
867 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
868 | EFI_IMAGE_SCN_MEM_DISCARDABLE
869 | EFI_IMAGE_SCN_MEM_READ);
870 }
871 }
872
873 STATIC
874 VOID
875 WriteDebug64 (
876 VOID
877 )
878 {
879 UINT32 Len;
880 UINT32 DebugOffset;
881 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
882 EFI_IMAGE_DATA_DIRECTORY *DataDir;
883 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *Dir;
884 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *Nb10;
885
886 Len = strlen(mInImageName) + 1;
887 DebugOffset = mCoffOffset;
888
889 mCoffOffset += sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)
890 + sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY)
891 + Len;
892 mCoffOffset = CoffAlign(mCoffOffset);
893
894 mCoffFile = realloc(mCoffFile, mCoffOffset);
895 memset(mCoffFile + DebugOffset, 0, mCoffOffset - DebugOffset);
896
897 Dir = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(mCoffFile + DebugOffset);
898 Dir->Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW;
899 Dir->SizeOfData = sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + Len;
900 Dir->RVA = DebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
901 Dir->FileOffset = DebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
902
903 Nb10 = (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY*)(Dir + 1);
904 Nb10->Signature = CODEVIEW_SIGNATURE_NB10;
905 strcpy ((char *)(Nb10 + 1), mInImageName);
906
907
908 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
909 DataDir = &NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG];
910 DataDir->VirtualAddress = DebugOffset;
911 DataDir->Size = mCoffOffset - DebugOffset;
912 if (DataDir->Size == 0) {
913 // If no debug, null out the directory entry and don't add the .debug section
914 DataDir->VirtualAddress = 0;
915 NtHdr->Pe32Plus.FileHeader.NumberOfSections--;
916 } else {
917 DataDir->VirtualAddress = DebugOffset;
918 CreateSectionHeader (".debug", DebugOffset, mCoffOffset - DebugOffset,
919 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
920 | EFI_IMAGE_SCN_MEM_DISCARDABLE
921 | EFI_IMAGE_SCN_MEM_READ);
922
923 }
924 }
925
926 STATIC
927 VOID
928 SetImageSize64 (
929 VOID
930 )
931 {
932 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
933
934 //
935 // Set image size
936 //
937 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
938 NtHdr->Pe32Plus.OptionalHeader.SizeOfImage = mCoffOffset;
939 }
940
941 STATIC
942 VOID
943 CleanUp64 (
944 VOID
945 )
946 {
947 if (mCoffSectionsOffset != NULL) {
948 free (mCoffSectionsOffset);
949 }
950 }
951
952