]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/GenFw/Elf32Convert.c
BaseTools/GenFw: move .debug contents to .data to save space
[mirror_edk2.git] / BaseTools / Source / C / GenFw / Elf32Convert.c
1 /** @file
2 Elf32 Convert solution
3
4 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2013, 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 "Elf32Convert.h"
39
40 STATIC
41 VOID
42 ScanSections32 (
43 VOID
44 );
45
46 STATIC
47 BOOLEAN
48 WriteSections32 (
49 SECTION_FILTER_TYPES FilterType
50 );
51
52 STATIC
53 VOID
54 WriteRelocations32 (
55 VOID
56 );
57
58 STATIC
59 VOID
60 WriteDebug32 (
61 VOID
62 );
63
64 STATIC
65 VOID
66 SetImageSize32 (
67 VOID
68 );
69
70 STATIC
71 VOID
72 CleanUp32 (
73 VOID
74 );
75
76 //
77 // Rename ELF32 strucutres to common names to help when porting to ELF64.
78 //
79 typedef Elf32_Shdr Elf_Shdr;
80 typedef Elf32_Ehdr Elf_Ehdr;
81 typedef Elf32_Rel Elf_Rel;
82 typedef Elf32_Sym Elf_Sym;
83 typedef Elf32_Phdr Elf_Phdr;
84 typedef Elf32_Dyn Elf_Dyn;
85 #define ELFCLASS ELFCLASS32
86 #define ELF_R_TYPE(r) ELF32_R_TYPE(r)
87 #define ELF_R_SYM(r) ELF32_R_SYM(r)
88
89 //
90 // Well known ELF structures.
91 //
92 STATIC Elf_Ehdr *mEhdr;
93 STATIC Elf_Shdr *mShdrBase;
94 STATIC Elf_Phdr *mPhdrBase;
95
96 //
97 // Coff information
98 //
99 STATIC UINT32 mCoffAlignment = 0x20;
100
101 //
102 // PE section alignment.
103 //
104 STATIC const UINT16 mCoffNbrSections = 4;
105
106 //
107 // ELF sections to offset in Coff file.
108 //
109 STATIC UINT32 *mCoffSectionsOffset = NULL;
110
111 //
112 // Offsets in COFF file
113 //
114 STATIC UINT32 mNtHdrOffset;
115 STATIC UINT32 mTextOffset;
116 STATIC UINT32 mDataOffset;
117 STATIC UINT32 mHiiRsrcOffset;
118 STATIC UINT32 mRelocOffset;
119 STATIC UINT32 mDebugOffset;
120
121 //
122 // Initialization Function
123 //
124 BOOLEAN
125 InitializeElf32 (
126 UINT8 *FileBuffer,
127 ELF_FUNCTION_TABLE *ElfFunctions
128 )
129 {
130 //
131 // Initialize data pointer and structures.
132 //
133 mEhdr = (Elf_Ehdr*) FileBuffer;
134
135 //
136 // Check the ELF32 specific header information.
137 //
138 if (mEhdr->e_ident[EI_CLASS] != ELFCLASS32) {
139 Error (NULL, 0, 3000, "Unsupported", "ELF EI_DATA not ELFCLASS32");
140 return FALSE;
141 }
142 if (mEhdr->e_ident[EI_DATA] != ELFDATA2LSB) {
143 Error (NULL, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");
144 return FALSE;
145 }
146 if ((mEhdr->e_type != ET_EXEC) && (mEhdr->e_type != ET_DYN)) {
147 Error (NULL, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");
148 return FALSE;
149 }
150 if (!((mEhdr->e_machine == EM_386) || (mEhdr->e_machine == EM_ARM))) {
151 Error (NULL, 0, 3000, "Unsupported", "ELF e_machine not EM_386 or EM_ARM");
152 return FALSE;
153 }
154 if (mEhdr->e_version != EV_CURRENT) {
155 Error (NULL, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr->e_version, EV_CURRENT);
156 return FALSE;
157 }
158
159 //
160 // Update section header pointers
161 //
162 mShdrBase = (Elf_Shdr *)((UINT8 *)mEhdr + mEhdr->e_shoff);
163 mPhdrBase = (Elf_Phdr *)((UINT8 *)mEhdr + mEhdr->e_phoff);
164
165 //
166 // Create COFF Section offset buffer and zero.
167 //
168 mCoffSectionsOffset = (UINT32 *)malloc(mEhdr->e_shnum * sizeof (UINT32));
169 memset(mCoffSectionsOffset, 0, mEhdr->e_shnum * sizeof(UINT32));
170
171 //
172 // Fill in function pointers.
173 //
174 ElfFunctions->ScanSections = ScanSections32;
175 ElfFunctions->WriteSections = WriteSections32;
176 ElfFunctions->WriteRelocations = WriteRelocations32;
177 ElfFunctions->WriteDebug = WriteDebug32;
178 ElfFunctions->SetImageSize = SetImageSize32;
179 ElfFunctions->CleanUp = CleanUp32;
180
181 return TRUE;
182 }
183
184
185 //
186 // Header by Index functions
187 //
188 STATIC
189 Elf_Shdr*
190 GetShdrByIndex (
191 UINT32 Num
192 )
193 {
194 if (Num >= mEhdr->e_shnum)
195 return NULL;
196 return (Elf_Shdr*)((UINT8*)mShdrBase + Num * mEhdr->e_shentsize);
197 }
198
199 STATIC
200 Elf_Phdr*
201 GetPhdrByIndex (
202 UINT32 num
203 )
204 {
205 if (num >= mEhdr->e_phnum) {
206 return NULL;
207 }
208
209 return (Elf_Phdr *)((UINT8*)mPhdrBase + num * mEhdr->e_phentsize);
210 }
211
212 STATIC
213 UINT32
214 CoffAlign (
215 UINT32 Offset
216 )
217 {
218 return (Offset + mCoffAlignment - 1) & ~(mCoffAlignment - 1);
219 }
220
221 //
222 // filter functions
223 //
224 STATIC
225 BOOLEAN
226 IsTextShdr (
227 Elf_Shdr *Shdr
228 )
229 {
230 return (BOOLEAN) ((Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == SHF_ALLOC);
231 }
232
233 STATIC
234 BOOLEAN
235 IsHiiRsrcShdr (
236 Elf_Shdr *Shdr
237 )
238 {
239 Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx);
240
241 return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_HII_SECTION_NAME) == 0);
242 }
243
244 STATIC
245 BOOLEAN
246 IsDataShdr (
247 Elf_Shdr *Shdr
248 )
249 {
250 if (IsHiiRsrcShdr(Shdr)) {
251 return FALSE;
252 }
253 return (BOOLEAN) (Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == (SHF_ALLOC | SHF_WRITE);
254 }
255
256 //
257 // Elf functions interface implementation
258 //
259
260 STATIC
261 VOID
262 ScanSections32 (
263 VOID
264 )
265 {
266 UINT32 i;
267 EFI_IMAGE_DOS_HEADER *DosHdr;
268 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
269 UINT32 CoffEntry;
270 UINT32 SectionCount;
271 BOOLEAN FoundSection;
272
273 CoffEntry = 0;
274 mCoffOffset = 0;
275
276 //
277 // Coff file start with a DOS header.
278 //
279 mCoffOffset = sizeof(EFI_IMAGE_DOS_HEADER) + 0x40;
280 mNtHdrOffset = mCoffOffset;
281 switch (mEhdr->e_machine) {
282 case EM_386:
283 case EM_ARM:
284 mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS32);
285 break;
286 default:
287 VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN)mEhdr->e_machine);
288 mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS32);
289 break;
290 }
291
292 mTableOffset = mCoffOffset;
293 mCoffOffset += mCoffNbrSections * sizeof(EFI_IMAGE_SECTION_HEADER);
294
295 //
296 // Set mCoffAlignment to the maximum alignment of the input sections
297 // we care about
298 //
299 for (i = 0; i < mEhdr->e_shnum; i++) {
300 Elf_Shdr *shdr = GetShdrByIndex(i);
301 if (shdr->sh_addralign <= mCoffAlignment) {
302 continue;
303 }
304 if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr)) {
305 mCoffAlignment = (UINT32)shdr->sh_addralign;
306 }
307 }
308
309 //
310 // First text sections.
311 //
312 mCoffOffset = CoffAlign(mCoffOffset);
313 mTextOffset = mCoffOffset;
314 FoundSection = FALSE;
315 SectionCount = 0;
316 for (i = 0; i < mEhdr->e_shnum; i++) {
317 Elf_Shdr *shdr = GetShdrByIndex(i);
318 if (IsTextShdr(shdr)) {
319 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
320 // the alignment field is valid
321 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
322 // if the section address is aligned we must align PE/COFF
323 mCoffOffset = (mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1);
324 } else if ((shdr->sh_addr % shdr->sh_addralign) != (mCoffOffset % shdr->sh_addralign)) {
325 // ARM RVCT tools have behavior outside of the ELF specification to try
326 // and make images smaller. If sh_addr is not aligned to sh_addralign
327 // then the section needs to preserve sh_addr MOD sh_addralign.
328 // Normally doing nothing here works great.
329 Error (NULL, 0, 3000, "Invalid", "Unsupported section alignment.");
330 }
331 }
332
333 /* Relocate entry. */
334 if ((mEhdr->e_entry >= shdr->sh_addr) &&
335 (mEhdr->e_entry < shdr->sh_addr + shdr->sh_size)) {
336 CoffEntry = mCoffOffset + mEhdr->e_entry - shdr->sh_addr;
337 }
338
339 //
340 // Set mTextOffset with the offset of the first '.text' section
341 //
342 if (!FoundSection) {
343 mTextOffset = mCoffOffset;
344 FoundSection = TRUE;
345 }
346
347 mCoffSectionsOffset[i] = mCoffOffset;
348 mCoffOffset += shdr->sh_size;
349 SectionCount ++;
350 }
351 }
352
353 if (!FoundSection) {
354 Error (NULL, 0, 3000, "Invalid", "Did not find any '.text' section.");
355 assert (FALSE);
356 }
357
358 mDebugOffset = mCoffOffset;
359
360 if (mEhdr->e_machine != EM_ARM) {
361 mCoffOffset = CoffAlign(mCoffOffset);
362 }
363
364 if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {
365 Warning (NULL, 0, 0, NULL, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName);
366 }
367
368 //
369 // Then data sections.
370 //
371 mDataOffset = mCoffOffset;
372 FoundSection = FALSE;
373 SectionCount = 0;
374 for (i = 0; i < mEhdr->e_shnum; i++) {
375 Elf_Shdr *shdr = GetShdrByIndex(i);
376 if (IsDataShdr(shdr)) {
377 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
378 // the alignment field is valid
379 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
380 // if the section address is aligned we must align PE/COFF
381 mCoffOffset = (mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1);
382 } else if ((shdr->sh_addr % shdr->sh_addralign) != (mCoffOffset % shdr->sh_addralign)) {
383 // ARM RVCT tools have behavior outside of the ELF specification to try
384 // and make images smaller. If sh_addr is not aligned to sh_addralign
385 // then the section needs to preserve sh_addr MOD sh_addralign.
386 // Normally doing nothing here works great.
387 Error (NULL, 0, 3000, "Invalid", "Unsupported section alignment.");
388 }
389 }
390
391 //
392 // Set mDataOffset with the offset of the first '.data' section
393 //
394 if (!FoundSection) {
395 mDataOffset = mCoffOffset;
396 FoundSection = TRUE;
397 }
398
399 mCoffSectionsOffset[i] = mCoffOffset;
400 mCoffOffset += shdr->sh_size;
401 SectionCount ++;
402 }
403 }
404
405 if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {
406 Warning (NULL, 0, 0, NULL, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName);
407 }
408
409 //
410 // Make room for .debug data in .data (or .text if .data is empty) instead of
411 // putting it in a section of its own. This is explicitly allowed by the
412 // PE/COFF spec, and prevents bloat in the binary when using large values for
413 // section alignment.
414 //
415 if (SectionCount > 0) {
416 mDebugOffset = mCoffOffset;
417 }
418 mCoffOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY) +
419 sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) +
420 strlen(mInImageName) + 1;
421
422 mCoffOffset = CoffAlign(mCoffOffset);
423 if (SectionCount == 0) {
424 mDataOffset = mCoffOffset;
425 }
426
427 //
428 // The HII resource sections.
429 //
430 mHiiRsrcOffset = mCoffOffset;
431 for (i = 0; i < mEhdr->e_shnum; i++) {
432 Elf_Shdr *shdr = GetShdrByIndex(i);
433 if (IsHiiRsrcShdr(shdr)) {
434 if ((shdr->sh_addralign != 0) && (shdr->sh_addralign != 1)) {
435 // the alignment field is valid
436 if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) {
437 // if the section address is aligned we must align PE/COFF
438 mCoffOffset = (mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1);
439 } else if ((shdr->sh_addr % shdr->sh_addralign) != (mCoffOffset % shdr->sh_addralign)) {
440 // ARM RVCT tools have behavior outside of the ELF specification to try
441 // and make images smaller. If sh_addr is not aligned to sh_addralign
442 // then the section needs to preserve sh_addr MOD sh_addralign.
443 // Normally doing nothing here works great.
444 Error (NULL, 0, 3000, "Invalid", "Unsupported section alignment.");
445 }
446 }
447 if (shdr->sh_size != 0) {
448 mHiiRsrcOffset = mCoffOffset;
449 mCoffSectionsOffset[i] = mCoffOffset;
450 mCoffOffset += shdr->sh_size;
451 mCoffOffset = CoffAlign(mCoffOffset);
452 SetHiiResourceHeader ((UINT8*) mEhdr + shdr->sh_offset, mHiiRsrcOffset);
453 }
454 break;
455 }
456 }
457
458 mRelocOffset = mCoffOffset;
459
460 //
461 // Allocate base Coff file. Will be expanded later for relocations.
462 //
463 mCoffFile = (UINT8 *)malloc(mCoffOffset);
464 memset(mCoffFile, 0, mCoffOffset);
465
466 //
467 // Fill headers.
468 //
469 DosHdr = (EFI_IMAGE_DOS_HEADER *)mCoffFile;
470 DosHdr->e_magic = EFI_IMAGE_DOS_SIGNATURE;
471 DosHdr->e_lfanew = mNtHdrOffset;
472
473 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION*)(mCoffFile + mNtHdrOffset);
474
475 NtHdr->Pe32.Signature = EFI_IMAGE_NT_SIGNATURE;
476
477 switch (mEhdr->e_machine) {
478 case EM_386:
479 NtHdr->Pe32.FileHeader.Machine = EFI_IMAGE_MACHINE_IA32;
480 NtHdr->Pe32.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;
481 break;
482 case EM_ARM:
483 NtHdr->Pe32.FileHeader.Machine = EFI_IMAGE_MACHINE_ARMT;
484 NtHdr->Pe32.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;
485 break;
486 default:
487 VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN)mEhdr->e_machine);
488 NtHdr->Pe32.FileHeader.Machine = EFI_IMAGE_MACHINE_IA32;
489 NtHdr->Pe32.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;
490 }
491
492 NtHdr->Pe32.FileHeader.NumberOfSections = mCoffNbrSections;
493 NtHdr->Pe32.FileHeader.TimeDateStamp = (UINT32) time(NULL);
494 mImageTimeStamp = NtHdr->Pe32.FileHeader.TimeDateStamp;
495 NtHdr->Pe32.FileHeader.PointerToSymbolTable = 0;
496 NtHdr->Pe32.FileHeader.NumberOfSymbols = 0;
497 NtHdr->Pe32.FileHeader.SizeOfOptionalHeader = sizeof(NtHdr->Pe32.OptionalHeader);
498 NtHdr->Pe32.FileHeader.Characteristics = EFI_IMAGE_FILE_EXECUTABLE_IMAGE
499 | EFI_IMAGE_FILE_LINE_NUMS_STRIPPED
500 | EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED
501 | EFI_IMAGE_FILE_32BIT_MACHINE;
502
503 NtHdr->Pe32.OptionalHeader.SizeOfCode = mDataOffset - mTextOffset;
504 NtHdr->Pe32.OptionalHeader.SizeOfInitializedData = mRelocOffset - mDataOffset;
505 NtHdr->Pe32.OptionalHeader.SizeOfUninitializedData = 0;
506 NtHdr->Pe32.OptionalHeader.AddressOfEntryPoint = CoffEntry;
507
508 NtHdr->Pe32.OptionalHeader.BaseOfCode = mTextOffset;
509
510 NtHdr->Pe32.OptionalHeader.BaseOfData = mDataOffset;
511 NtHdr->Pe32.OptionalHeader.ImageBase = 0;
512 NtHdr->Pe32.OptionalHeader.SectionAlignment = mCoffAlignment;
513 NtHdr->Pe32.OptionalHeader.FileAlignment = mCoffAlignment;
514 NtHdr->Pe32.OptionalHeader.SizeOfImage = 0;
515
516 NtHdr->Pe32.OptionalHeader.SizeOfHeaders = mTextOffset;
517 NtHdr->Pe32.OptionalHeader.NumberOfRvaAndSizes = EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES;
518
519 //
520 // Section headers.
521 //
522 if ((mDataOffset - mTextOffset) > 0) {
523 CreateSectionHeader (".text", mTextOffset, mDataOffset - mTextOffset,
524 EFI_IMAGE_SCN_CNT_CODE
525 | EFI_IMAGE_SCN_MEM_EXECUTE
526 | EFI_IMAGE_SCN_MEM_READ);
527 } else {
528 // Don't make a section of size 0.
529 NtHdr->Pe32.FileHeader.NumberOfSections--;
530 }
531
532 if ((mHiiRsrcOffset - mDataOffset) > 0) {
533 CreateSectionHeader (".data", mDataOffset, mHiiRsrcOffset - mDataOffset,
534 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
535 | EFI_IMAGE_SCN_MEM_WRITE
536 | EFI_IMAGE_SCN_MEM_READ);
537 } else {
538 // Don't make a section of size 0.
539 NtHdr->Pe32.FileHeader.NumberOfSections--;
540 }
541
542 if ((mRelocOffset - mHiiRsrcOffset) > 0) {
543 CreateSectionHeader (".rsrc", mHiiRsrcOffset, mRelocOffset - mHiiRsrcOffset,
544 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
545 | EFI_IMAGE_SCN_MEM_READ);
546
547 NtHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = mRelocOffset - mHiiRsrcOffset;
548 NtHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = mHiiRsrcOffset;
549 } else {
550 // Don't make a section of size 0.
551 NtHdr->Pe32.FileHeader.NumberOfSections--;
552 }
553
554 }
555
556 STATIC
557 BOOLEAN
558 WriteSections32 (
559 SECTION_FILTER_TYPES FilterType
560 )
561 {
562 UINT32 Idx;
563 Elf_Shdr *SecShdr;
564 UINT32 SecOffset;
565 BOOLEAN (*Filter)(Elf_Shdr *);
566
567 //
568 // Initialize filter pointer
569 //
570 switch (FilterType) {
571 case SECTION_TEXT:
572 Filter = IsTextShdr;
573 break;
574 case SECTION_HII:
575 Filter = IsHiiRsrcShdr;
576 break;
577 case SECTION_DATA:
578 Filter = IsDataShdr;
579 break;
580 default:
581 return FALSE;
582 }
583
584 //
585 // First: copy sections.
586 //
587 for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {
588 Elf_Shdr *Shdr = GetShdrByIndex(Idx);
589 if ((*Filter)(Shdr)) {
590 switch (Shdr->sh_type) {
591 case SHT_PROGBITS:
592 /* Copy. */
593 memcpy(mCoffFile + mCoffSectionsOffset[Idx],
594 (UINT8*)mEhdr + Shdr->sh_offset,
595 Shdr->sh_size);
596 break;
597
598 case SHT_NOBITS:
599 memset(mCoffFile + mCoffSectionsOffset[Idx], 0, Shdr->sh_size);
600 break;
601
602 default:
603 //
604 // Ignore for unkown section type.
605 //
606 VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName, (unsigned)Shdr->sh_type);
607 break;
608 }
609 }
610 }
611
612 //
613 // Second: apply relocations.
614 //
615 for (Idx = 0; Idx < mEhdr->e_shnum; Idx++) {
616 //
617 // Determine if this is a relocation section.
618 //
619 Elf_Shdr *RelShdr = GetShdrByIndex(Idx);
620 if ((RelShdr->sh_type != SHT_REL) && (RelShdr->sh_type != SHT_RELA)) {
621 continue;
622 }
623
624 //
625 // Relocation section found. Now extract section information that the relocations
626 // apply to in the ELF data and the new COFF data.
627 //
628 SecShdr = GetShdrByIndex(RelShdr->sh_info);
629 SecOffset = mCoffSectionsOffset[RelShdr->sh_info];
630
631 //
632 // Only process relocations for the current filter type.
633 //
634 if (RelShdr->sh_type == SHT_REL && (*Filter)(SecShdr)) {
635 UINT32 RelOffset;
636
637 //
638 // Determine the symbol table referenced by the relocation data.
639 //
640 Elf_Shdr *SymtabShdr = GetShdrByIndex(RelShdr->sh_link);
641 UINT8 *Symtab = (UINT8*)mEhdr + SymtabShdr->sh_offset;
642
643 //
644 // Process all relocation entries for this section.
645 //
646 for (RelOffset = 0; RelOffset < RelShdr->sh_size; RelOffset += RelShdr->sh_entsize) {
647 //
648 // Set pointer to relocation entry
649 //
650 Elf_Rel *Rel = (Elf_Rel *)((UINT8*)mEhdr + RelShdr->sh_offset + RelOffset);
651
652 //
653 // Set pointer to symbol table entry associated with the relocation entry.
654 //
655 Elf_Sym *Sym = (Elf_Sym *)(Symtab + ELF_R_SYM(Rel->r_info) * SymtabShdr->sh_entsize);
656
657 Elf_Shdr *SymShdr;
658 UINT8 *Targ;
659 UINT16 Address;
660
661 //
662 // Check section header index found in symbol table and get the section
663 // header location.
664 //
665 if (Sym->st_shndx == SHN_UNDEF
666 || Sym->st_shndx == SHN_ABS
667 || Sym->st_shndx > mEhdr->e_shnum) {
668 Error (NULL, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName);
669 }
670 SymShdr = GetShdrByIndex(Sym->st_shndx);
671
672 //
673 // Convert the relocation data to a pointer into the coff file.
674 //
675 // Note:
676 // r_offset is the virtual address of the storage unit to be relocated.
677 // sh_addr is the virtual address for the base of the section.
678 //
679 Targ = mCoffFile + SecOffset + (Rel->r_offset - SecShdr->sh_addr);
680
681 //
682 // Determine how to handle each relocation type based on the machine type.
683 //
684 if (mEhdr->e_machine == EM_386) {
685 switch (ELF_R_TYPE(Rel->r_info)) {
686 case R_386_NONE:
687 break;
688 case R_386_32:
689 //
690 // Absolute relocation.
691 // Converts Targ from a absolute virtual address to the absolute
692 // COFF address.
693 //
694 *(UINT32 *)Targ = *(UINT32 *)Targ - SymShdr->sh_addr
695 + mCoffSectionsOffset[Sym->st_shndx];
696 break;
697 case R_386_PC32:
698 //
699 // Relative relocation: Symbol - Ip + Addend
700 //
701 *(UINT32 *)Targ = *(UINT32 *)Targ
702 + (mCoffSectionsOffset[Sym->st_shndx] - SymShdr->sh_addr)
703 - (SecOffset - SecShdr->sh_addr);
704 break;
705 default:
706 Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
707 }
708 } else if (mEhdr->e_machine == EM_ARM) {
709 switch (ELF32_R_TYPE(Rel->r_info)) {
710 case R_ARM_RBASE:
711 // No relocation - no action required
712 // break skipped
713
714 case R_ARM_PC24:
715 case R_ARM_XPC25:
716 case R_ARM_THM_PC22:
717 case R_ARM_THM_JUMP19:
718 case R_ARM_CALL:
719 case R_ARM_JMP24:
720 case R_ARM_THM_JUMP24:
721 case R_ARM_PREL31:
722 case R_ARM_MOVW_PREL_NC:
723 case R_ARM_MOVT_PREL:
724 case R_ARM_THM_MOVW_PREL_NC:
725 case R_ARM_THM_MOVT_PREL:
726 case R_ARM_THM_JMP6:
727 case R_ARM_THM_ALU_PREL_11_0:
728 case R_ARM_THM_PC12:
729 case R_ARM_REL32_NOI:
730 case R_ARM_ALU_PC_G0_NC:
731 case R_ARM_ALU_PC_G0:
732 case R_ARM_ALU_PC_G1_NC:
733 case R_ARM_ALU_PC_G1:
734 case R_ARM_ALU_PC_G2:
735 case R_ARM_LDR_PC_G1:
736 case R_ARM_LDR_PC_G2:
737 case R_ARM_LDRS_PC_G0:
738 case R_ARM_LDRS_PC_G1:
739 case R_ARM_LDRS_PC_G2:
740 case R_ARM_LDC_PC_G0:
741 case R_ARM_LDC_PC_G1:
742 case R_ARM_LDC_PC_G2:
743 case R_ARM_GOT_PREL:
744 case R_ARM_THM_JUMP11:
745 case R_ARM_THM_JUMP8:
746 case R_ARM_TLS_GD32:
747 case R_ARM_TLS_LDM32:
748 case R_ARM_TLS_IE32:
749 // Thease are all PC-relative relocations and don't require modification
750 // GCC does not seem to have the concept of a application that just needs to get relocated.
751 break;
752
753 case R_ARM_THM_MOVW_ABS_NC:
754 // MOVW is only lower 16-bits of the addres
755 Address = (UINT16)(Sym->st_value - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
756 ThumbMovtImmediatePatch ((UINT16 *)Targ, Address);
757 break;
758
759 case R_ARM_THM_MOVT_ABS:
760 // MOVT is only upper 16-bits of the addres
761 Address = (UINT16)((Sym->st_value - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]) >> 16);
762 ThumbMovtImmediatePatch ((UINT16 *)Targ, Address);
763 break;
764
765 case R_ARM_ABS32:
766 case R_ARM_RABS32:
767 //
768 // Absolute relocation.
769 //
770 *(UINT32 *)Targ = *(UINT32 *)Targ - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx];
771 break;
772
773 default:
774 Error (NULL, 0, 3000, "Invalid", "WriteSections (): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName, (unsigned) ELF32_R_TYPE(Rel->r_info));
775 }
776 }
777 }
778 }
779 }
780
781 return TRUE;
782 }
783
784 UINTN gMovwOffset = 0;
785
786 STATIC
787 VOID
788 WriteRelocations32 (
789 VOID
790 )
791 {
792 UINT32 Index;
793 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
794 EFI_IMAGE_DATA_DIRECTORY *Dir;
795 BOOLEAN FoundRelocations;
796 Elf_Dyn *Dyn;
797 Elf_Rel *Rel;
798 UINTN RelElementSize;
799 UINTN RelSize;
800 UINTN RelOffset;
801 UINTN K;
802 UINT8 *Targ;
803 Elf32_Phdr *DynamicSegment;
804 Elf32_Phdr *TargetSegment;
805
806 for (Index = 0, FoundRelocations = FALSE; Index < mEhdr->e_shnum; Index++) {
807 Elf_Shdr *RelShdr = GetShdrByIndex(Index);
808 if ((RelShdr->sh_type == SHT_REL) || (RelShdr->sh_type == SHT_RELA)) {
809 Elf_Shdr *SecShdr = GetShdrByIndex (RelShdr->sh_info);
810 if (IsTextShdr(SecShdr) || IsDataShdr(SecShdr)) {
811 UINT32 RelIdx;
812
813 FoundRelocations = TRUE;
814 for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += RelShdr->sh_entsize) {
815 Elf_Rel *Rel = (Elf_Rel *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx);
816
817 if (mEhdr->e_machine == EM_386) {
818 switch (ELF_R_TYPE(Rel->r_info)) {
819 case R_386_NONE:
820 case R_386_PC32:
821 //
822 // No fixup entry required.
823 //
824 break;
825 case R_386_32:
826 //
827 // Creates a relative relocation entry from the absolute entry.
828 //
829 CoffAddFixup(mCoffSectionsOffset[RelShdr->sh_info]
830 + (Rel->r_offset - SecShdr->sh_addr),
831 EFI_IMAGE_REL_BASED_HIGHLOW);
832 break;
833 default:
834 Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_386 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
835 }
836 } else if (mEhdr->e_machine == EM_ARM) {
837 switch (ELF32_R_TYPE(Rel->r_info)) {
838 case R_ARM_RBASE:
839 // No relocation - no action required
840 // break skipped
841
842 case R_ARM_PC24:
843 case R_ARM_XPC25:
844 case R_ARM_THM_PC22:
845 case R_ARM_THM_JUMP19:
846 case R_ARM_CALL:
847 case R_ARM_JMP24:
848 case R_ARM_THM_JUMP24:
849 case R_ARM_PREL31:
850 case R_ARM_MOVW_PREL_NC:
851 case R_ARM_MOVT_PREL:
852 case R_ARM_THM_MOVW_PREL_NC:
853 case R_ARM_THM_MOVT_PREL:
854 case R_ARM_THM_JMP6:
855 case R_ARM_THM_ALU_PREL_11_0:
856 case R_ARM_THM_PC12:
857 case R_ARM_REL32_NOI:
858 case R_ARM_ALU_PC_G0_NC:
859 case R_ARM_ALU_PC_G0:
860 case R_ARM_ALU_PC_G1_NC:
861 case R_ARM_ALU_PC_G1:
862 case R_ARM_ALU_PC_G2:
863 case R_ARM_LDR_PC_G1:
864 case R_ARM_LDR_PC_G2:
865 case R_ARM_LDRS_PC_G0:
866 case R_ARM_LDRS_PC_G1:
867 case R_ARM_LDRS_PC_G2:
868 case R_ARM_LDC_PC_G0:
869 case R_ARM_LDC_PC_G1:
870 case R_ARM_LDC_PC_G2:
871 case R_ARM_GOT_PREL:
872 case R_ARM_THM_JUMP11:
873 case R_ARM_THM_JUMP8:
874 case R_ARM_TLS_GD32:
875 case R_ARM_TLS_LDM32:
876 case R_ARM_TLS_IE32:
877 // Thease are all PC-relative relocations and don't require modification
878 break;
879
880 case R_ARM_THM_MOVW_ABS_NC:
881 CoffAddFixup (
882 mCoffSectionsOffset[RelShdr->sh_info]
883 + (Rel->r_offset - SecShdr->sh_addr),
884 EFI_IMAGE_REL_BASED_ARM_MOV32T
885 );
886
887 // PE/COFF treats MOVW/MOVT relocation as single 64-bit instruction
888 // Track this address so we can log an error for unsupported sequence of MOVW/MOVT
889 gMovwOffset = mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr);
890 break;
891
892 case R_ARM_THM_MOVT_ABS:
893 if ((gMovwOffset + 4) != (mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr))) {
894 Error (NULL, 0, 3000, "Not Supported", "PE/COFF requires MOVW+MOVT instruction sequence %x +4 != %x.", gMovwOffset, mCoffSectionsOffset[RelShdr->sh_info] + (Rel->r_offset - SecShdr->sh_addr));
895 }
896 break;
897
898 case R_ARM_ABS32:
899 case R_ARM_RABS32:
900 CoffAddFixup (
901 mCoffSectionsOffset[RelShdr->sh_info]
902 + (Rel->r_offset - SecShdr->sh_addr),
903 EFI_IMAGE_REL_BASED_HIGHLOW
904 );
905 break;
906
907 default:
908 Error (NULL, 0, 3000, "Invalid", "WriteRelocations(): %s unsupported ELF EM_ARM relocation 0x%x.", mInImageName, (unsigned) ELF32_R_TYPE(Rel->r_info));
909 }
910 } else {
911 Error (NULL, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr->e_machine);
912 }
913 }
914 }
915 }
916 }
917
918 if (!FoundRelocations && (mEhdr->e_machine == EM_ARM)) {
919 /* Try again, but look for PT_DYNAMIC instead of SHT_REL */
920
921 for (Index = 0; Index < mEhdr->e_phnum; Index++) {
922 RelElementSize = 0;
923 RelSize = 0;
924 RelOffset = 0;
925
926 DynamicSegment = GetPhdrByIndex (Index);
927
928 if (DynamicSegment->p_type == PT_DYNAMIC) {
929 Dyn = (Elf32_Dyn *) ((UINT8 *)mEhdr + DynamicSegment->p_offset);
930
931 while (Dyn->d_tag != DT_NULL) {
932 switch (Dyn->d_tag) {
933 case DT_REL:
934 RelOffset = Dyn->d_un.d_val;
935 break;
936
937 case DT_RELSZ:
938 RelSize = Dyn->d_un.d_val;
939 break;
940
941 case DT_RELENT:
942 RelElementSize = Dyn->d_un.d_val;
943 break;
944
945 default:
946 break;
947 }
948 Dyn++;
949 }
950 if (( RelOffset == 0 ) || ( RelSize == 0 ) || ( RelElementSize == 0 )) {
951 Error (NULL, 0, 3000, "Invalid", "%s bad ARM dynamic relocations.", mInImageName);
952 }
953
954 for (K = 0; K < RelSize; K += RelElementSize) {
955
956 if (DynamicSegment->p_paddr == 0) {
957 // Older versions of the ARM ELF (SWS ESPC 0003 B-02) specification define DT_REL
958 // as an offset in the dynamic segment. p_paddr is defined to be zero for ARM tools
959 Rel = (Elf32_Rel *) ((UINT8 *) mEhdr + DynamicSegment->p_offset + RelOffset + K);
960 } else {
961 // This is how it reads in the generic ELF specification
962 Rel = (Elf32_Rel *) ((UINT8 *) mEhdr + RelOffset + K);
963 }
964
965 switch (ELF32_R_TYPE (Rel->r_info)) {
966 case R_ARM_RBASE:
967 break;
968
969 case R_ARM_RABS32:
970 TargetSegment = GetPhdrByIndex (ELF32_R_SYM (Rel->r_info) - 1);
971
972 // Note: r_offset in a memory address. Convert it to a pointer in the coff file.
973 Targ = mCoffFile + mCoffSectionsOffset[ ELF32_R_SYM( Rel->r_info ) ] + Rel->r_offset - TargetSegment->p_vaddr;
974
975 *(UINT32 *)Targ = *(UINT32 *)Targ + mCoffSectionsOffset [ELF32_R_SYM( Rel->r_info )];
976
977 CoffAddFixup (mCoffSectionsOffset[ELF32_R_SYM (Rel->r_info)] + (Rel->r_offset - TargetSegment->p_vaddr), EFI_IMAGE_REL_BASED_HIGHLOW);
978 break;
979
980 default:
981 Error (NULL, 0, 3000, "Invalid", "%s bad ARM dynamic relocations, unkown type %d.", mInImageName, ELF32_R_TYPE (Rel->r_info));
982 break;
983 }
984 }
985 break;
986 }
987 }
988 }
989
990 //
991 // Pad by adding empty entries.
992 //
993 while (mCoffOffset & (mCoffAlignment - 1)) {
994 CoffAddFixupEntry(0);
995 }
996
997 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
998 Dir = &NtHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
999 Dir->Size = mCoffOffset - mRelocOffset;
1000 if (Dir->Size == 0) {
1001 // If no relocations, null out the directory entry and don't add the .reloc section
1002 Dir->VirtualAddress = 0;
1003 NtHdr->Pe32.FileHeader.NumberOfSections--;
1004 } else {
1005 Dir->VirtualAddress = mRelocOffset;
1006 CreateSectionHeader (".reloc", mRelocOffset, mCoffOffset - mRelocOffset,
1007 EFI_IMAGE_SCN_CNT_INITIALIZED_DATA
1008 | EFI_IMAGE_SCN_MEM_DISCARDABLE
1009 | EFI_IMAGE_SCN_MEM_READ);
1010 }
1011
1012 }
1013
1014 STATIC
1015 VOID
1016 WriteDebug32 (
1017 VOID
1018 )
1019 {
1020 UINT32 Len;
1021 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
1022 EFI_IMAGE_DATA_DIRECTORY *DataDir;
1023 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *Dir;
1024 EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *Nb10;
1025
1026 Len = strlen(mInImageName) + 1;
1027
1028 Dir = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(mCoffFile + mDebugOffset);
1029 Dir->Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW;
1030 Dir->SizeOfData = sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + Len;
1031 Dir->RVA = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
1032 Dir->FileOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
1033
1034 Nb10 = (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY*)(Dir + 1);
1035 Nb10->Signature = CODEVIEW_SIGNATURE_NB10;
1036 strcpy ((char *)(Nb10 + 1), mInImageName);
1037
1038
1039 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
1040 DataDir = &NtHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG];
1041 DataDir->VirtualAddress = mDebugOffset;
1042 DataDir->Size = Dir->SizeOfData + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
1043 }
1044
1045 STATIC
1046 VOID
1047 SetImageSize32 (
1048 VOID
1049 )
1050 {
1051 EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;
1052
1053 //
1054 // Set image size
1055 //
1056 NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);
1057 NtHdr->Pe32.OptionalHeader.SizeOfImage = mCoffOffset;
1058 }
1059
1060 STATIC
1061 VOID
1062 CleanUp32 (
1063 VOID
1064 )
1065 {
1066 if (mCoffSectionsOffset != NULL) {
1067 free (mCoffSectionsOffset);
1068 }
1069 }
1070
1071