4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available
7 under the terms and conditions of the BSD License which accompanies this
8 distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "WinNtInclude.h"
29 #include <Common/UefiBaseTypes.h>
30 #include <IndustryStandard/PeImage.h>
32 #include "EfiUtilityMsgs.h"
35 #include "ElfConvert.h"
36 #include "Elf32Convert.h"
37 #include "Elf64Convert.h"
40 // Result Coff file in memory.
42 UINT8
*mCoffFile
= NULL
;
45 // COFF relocation data
47 EFI_IMAGE_BASE_RELOCATION
*mCoffBaseRel
;
48 UINT16
*mCoffEntryRel
;
51 // Current offset in coff file.
56 // Offset in Coff file of headers and sections.
63 UINT32 mFileBufferSize
;
66 //*****************************************************************************
67 // Common ELF Functions
68 //*****************************************************************************
78 mCoffBaseRel
->SizeOfBlock
+= 2;
88 if (mCoffBaseRel
== NULL
89 || mCoffBaseRel
->VirtualAddress
!= (Offset
& ~0xfff)) {
90 if (mCoffBaseRel
!= NULL
) {
92 // Add a null entry (is it required ?)
94 CoffAddFixupEntry (0);
99 if (mCoffOffset
% 4 != 0)
100 CoffAddFixupEntry (0);
103 mCoffFile
= realloc (
105 mCoffOffset
+ sizeof(EFI_IMAGE_BASE_RELOCATION
) + 2 * MAX_COFF_ALIGNMENT
107 if (mCoffFile
== NULL
) {
108 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
110 assert (mCoffFile
!= NULL
);
112 mCoffFile
+ mCoffOffset
, 0,
113 sizeof(EFI_IMAGE_BASE_RELOCATION
) + 2 * MAX_COFF_ALIGNMENT
116 mCoffBaseRel
= (EFI_IMAGE_BASE_RELOCATION
*)(mCoffFile
+ mCoffOffset
);
117 mCoffBaseRel
->VirtualAddress
= Offset
& ~0xfff;
118 mCoffBaseRel
->SizeOfBlock
= sizeof(EFI_IMAGE_BASE_RELOCATION
);
120 mCoffEntryRel
= (UINT16
*)(mCoffBaseRel
+ 1);
121 mCoffOffset
+= sizeof(EFI_IMAGE_BASE_RELOCATION
);
127 CoffAddFixupEntry((UINT16
) ((Type
<< 12) | (Offset
& 0xfff)));
131 CreateSectionHeader (
138 EFI_IMAGE_SECTION_HEADER
*Hdr
;
139 Hdr
= (EFI_IMAGE_SECTION_HEADER
*)(mCoffFile
+ mTableOffset
);
141 strcpy((char *)Hdr
->Name
, Name
);
142 Hdr
->Misc
.VirtualSize
= Size
;
143 Hdr
->VirtualAddress
= Offset
;
144 Hdr
->SizeOfRawData
= Size
;
145 Hdr
->PointerToRawData
= Offset
;
146 Hdr
->PointerToRelocations
= 0;
147 Hdr
->PointerToLinenumbers
= 0;
148 Hdr
->NumberOfRelocations
= 0;
149 Hdr
->NumberOfLinenumbers
= 0;
150 Hdr
->Characteristics
= Flags
;
152 mTableOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
156 //*****************************************************************************
157 // Functions called from GenFw main code.
158 //*****************************************************************************
166 return (FileBuffer
[EI_MAG0
] == ELFMAG0
&&
167 FileBuffer
[EI_MAG1
] == ELFMAG1
&&
168 FileBuffer
[EI_MAG2
] == ELFMAG2
&&
169 FileBuffer
[EI_MAG3
] == ELFMAG3
);
178 ELF_FUNCTION_TABLE ElfFunctions
;
181 mFileBufferSize
= *FileLength
;
183 // Determine ELF type and set function table pointer correctly.
185 VerboseMsg ("Check Elf Image Header");
186 EiClass
= (*FileBuffer
)[EI_CLASS
];
187 if (EiClass
== ELFCLASS32
) {
188 if (!InitializeElf32 (*FileBuffer
, &ElfFunctions
)) {
191 } else if (EiClass
== ELFCLASS64
) {
192 if (!InitializeElf64 (*FileBuffer
, &ElfFunctions
)) {
196 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_CLASS not supported.");
201 // Compute sections new address.
203 VerboseMsg ("Compute sections new address.");
204 ElfFunctions
.ScanSections ();
207 // Write and relocate sections.
209 VerboseMsg ("Write and relocate sections.");
210 if (!ElfFunctions
.WriteSections (SECTION_TEXT
)) {
213 if (!ElfFunctions
.WriteSections (SECTION_DATA
)) {
216 if (!ElfFunctions
.WriteSections (SECTION_HII
)) {
221 // Translate and write relocations.
223 VerboseMsg ("Translate and write relocations.");
224 ElfFunctions
.WriteRelocations ();
229 VerboseMsg ("Write debug info.");
230 ElfFunctions
.WriteDebug ();
233 // Make sure image size is correct before returning the new image.
235 VerboseMsg ("Set image size.");
236 ElfFunctions
.SetImageSize ();
242 *FileBuffer
= mCoffFile
;
243 *FileLength
= mCoffOffset
;
246 // Free resources used by ELF functions.
248 ElfFunctions
.CleanUp ();