]>
Commit | Line | Data |
---|---|---|
f51461c8 | 1 | /** @file\r |
97fa0ee9 | 2 | Elf convert solution\r |
f51461c8 | 3 | \r |
06b45735 | 4 | Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>\r |
f51461c8 LG |
5 | \r |
6 | This program and the accompanying materials are licensed and made available \r | |
7 | under the terms and conditions of the BSD License which accompanies this \r | |
8 | distribution. The full text of the license may be found at \r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | #include "WinNtInclude.h"\r | |
17 | \r | |
18 | #ifndef __GNUC__\r | |
19 | #include <windows.h>\r | |
20 | #include <io.h>\r | |
21 | #endif\r | |
22 | #include <stdio.h>\r | |
23 | #include <stdlib.h>\r | |
24 | #include <string.h>\r | |
25 | #include <time.h>\r | |
26 | #include <ctype.h>\r | |
06b45735 | 27 | #include <assert.h>\r |
f51461c8 LG |
28 | \r |
29 | #include <Common/UefiBaseTypes.h>\r | |
30 | #include <IndustryStandard/PeImage.h>\r | |
31 | \r | |
32 | #include "EfiUtilityMsgs.h"\r | |
33 | \r | |
34 | #include "GenFw.h"\r | |
35 | #include "ElfConvert.h"\r | |
36 | #include "Elf32Convert.h"\r | |
37 | #include "Elf64Convert.h"\r | |
38 | \r | |
39 | //\r | |
40 | // Result Coff file in memory.\r | |
41 | //\r | |
42 | UINT8 *mCoffFile = NULL;\r | |
43 | \r | |
44 | //\r | |
45 | // COFF relocation data\r | |
46 | //\r | |
47 | EFI_IMAGE_BASE_RELOCATION *mCoffBaseRel;\r | |
48 | UINT16 *mCoffEntryRel;\r | |
49 | \r | |
50 | //\r | |
51 | // Current offset in coff file.\r | |
52 | //\r | |
53 | UINT32 mCoffOffset;\r | |
54 | \r | |
55 | //\r | |
56 | // Offset in Coff file of headers and sections.\r | |
57 | //\r | |
58 | UINT32 mTableOffset;\r | |
59 | \r | |
60 | //\r | |
61 | //*****************************************************************************\r | |
62 | // Common ELF Functions\r | |
63 | //*****************************************************************************\r | |
64 | //\r | |
65 | \r | |
66 | VOID\r | |
67 | CoffAddFixupEntry(\r | |
68 | UINT16 Val\r | |
69 | )\r | |
70 | {\r | |
71 | *mCoffEntryRel = Val;\r | |
72 | mCoffEntryRel++;\r | |
73 | mCoffBaseRel->SizeOfBlock += 2;\r | |
74 | mCoffOffset += 2;\r | |
75 | }\r | |
76 | \r | |
77 | VOID\r | |
78 | CoffAddFixup(\r | |
79 | UINT32 Offset,\r | |
80 | UINT8 Type\r | |
81 | )\r | |
82 | {\r | |
83 | if (mCoffBaseRel == NULL\r | |
84 | || mCoffBaseRel->VirtualAddress != (Offset & ~0xfff)) {\r | |
85 | if (mCoffBaseRel != NULL) {\r | |
86 | //\r | |
87 | // Add a null entry (is it required ?)\r | |
88 | //\r | |
89 | CoffAddFixupEntry (0);\r | |
90 | \r | |
91 | //\r | |
92 | // Pad for alignment.\r | |
93 | //\r | |
94 | if (mCoffOffset % 4 != 0)\r | |
95 | CoffAddFixupEntry (0);\r | |
96 | }\r | |
97 | \r | |
98 | mCoffFile = realloc (\r | |
99 | mCoffFile,\r | |
54b1b57a | 100 | mCoffOffset + sizeof(EFI_IMAGE_BASE_RELOCATION) + 2 * MAX_COFF_ALIGNMENT\r |
f51461c8 | 101 | );\r |
06b45735 HW |
102 | if (mCoffFile == NULL) {\r |
103 | Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r | |
104 | }\r | |
105 | assert (mCoffFile != NULL);\r | |
f51461c8 LG |
106 | memset (\r |
107 | mCoffFile + mCoffOffset, 0,\r | |
54b1b57a | 108 | sizeof(EFI_IMAGE_BASE_RELOCATION) + 2 * MAX_COFF_ALIGNMENT\r |
f51461c8 LG |
109 | );\r |
110 | \r | |
111 | mCoffBaseRel = (EFI_IMAGE_BASE_RELOCATION*)(mCoffFile + mCoffOffset);\r | |
112 | mCoffBaseRel->VirtualAddress = Offset & ~0xfff;\r | |
113 | mCoffBaseRel->SizeOfBlock = sizeof(EFI_IMAGE_BASE_RELOCATION);\r | |
114 | \r | |
115 | mCoffEntryRel = (UINT16 *)(mCoffBaseRel + 1);\r | |
116 | mCoffOffset += sizeof(EFI_IMAGE_BASE_RELOCATION);\r | |
117 | }\r | |
118 | \r | |
119 | //\r | |
120 | // Fill the entry.\r | |
121 | //\r | |
122 | CoffAddFixupEntry((UINT16) ((Type << 12) | (Offset & 0xfff)));\r | |
123 | }\r | |
124 | \r | |
125 | VOID\r | |
126 | CreateSectionHeader (\r | |
127 | const CHAR8 *Name,\r | |
128 | UINT32 Offset,\r | |
129 | UINT32 Size,\r | |
130 | UINT32 Flags\r | |
131 | )\r | |
132 | {\r | |
133 | EFI_IMAGE_SECTION_HEADER *Hdr;\r | |
134 | Hdr = (EFI_IMAGE_SECTION_HEADER*)(mCoffFile + mTableOffset);\r | |
135 | \r | |
136 | strcpy((char *)Hdr->Name, Name);\r | |
137 | Hdr->Misc.VirtualSize = Size;\r | |
138 | Hdr->VirtualAddress = Offset;\r | |
139 | Hdr->SizeOfRawData = Size;\r | |
140 | Hdr->PointerToRawData = Offset;\r | |
141 | Hdr->PointerToRelocations = 0;\r | |
142 | Hdr->PointerToLinenumbers = 0;\r | |
143 | Hdr->NumberOfRelocations = 0;\r | |
144 | Hdr->NumberOfLinenumbers = 0;\r | |
145 | Hdr->Characteristics = Flags;\r | |
146 | \r | |
147 | mTableOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r | |
148 | }\r | |
149 | \r | |
150 | //\r | |
151 | //*****************************************************************************\r | |
152 | // Functions called from GenFw main code.\r | |
153 | //*****************************************************************************\r | |
154 | //\r | |
155 | \r | |
156 | INTN\r | |
157 | IsElfHeader (\r | |
158 | UINT8 *FileBuffer\r | |
159 | )\r | |
160 | {\r | |
161 | return (FileBuffer[EI_MAG0] == ELFMAG0 && \r | |
162 | FileBuffer[EI_MAG1] == ELFMAG1 &&\r | |
163 | FileBuffer[EI_MAG2] == ELFMAG2 &&\r | |
164 | FileBuffer[EI_MAG3] == ELFMAG3);\r | |
165 | }\r | |
166 | \r | |
167 | BOOLEAN\r | |
168 | ConvertElf (\r | |
169 | UINT8 **FileBuffer,\r | |
170 | UINT32 *FileLength\r | |
171 | )\r | |
172 | {\r | |
173 | ELF_FUNCTION_TABLE ElfFunctions;\r | |
174 | UINT8 EiClass;\r | |
175 | \r | |
176 | //\r | |
177 | // Determine ELF type and set function table pointer correctly.\r | |
178 | //\r | |
179 | VerboseMsg ("Check Elf Image Header");\r | |
180 | EiClass = (*FileBuffer)[EI_CLASS];\r | |
181 | if (EiClass == ELFCLASS32) {\r | |
182 | if (!InitializeElf32 (*FileBuffer, &ElfFunctions)) {\r | |
183 | return FALSE;\r | |
184 | }\r | |
185 | } else if (EiClass == ELFCLASS64) {\r | |
186 | if (!InitializeElf64 (*FileBuffer, &ElfFunctions)) {\r | |
187 | return FALSE;\r | |
188 | }\r | |
189 | } else {\r | |
190 | Error (NULL, 0, 3000, "Unsupported", "ELF EI_CLASS not supported.");\r | |
191 | return FALSE;\r | |
192 | }\r | |
193 | \r | |
194 | //\r | |
195 | // Compute sections new address.\r | |
196 | // \r | |
197 | VerboseMsg ("Compute sections new address.");\r | |
198 | ElfFunctions.ScanSections ();\r | |
199 | \r | |
200 | //\r | |
201 | // Write and relocate sections.\r | |
202 | //\r | |
203 | VerboseMsg ("Write and relocate sections.");\r | |
204 | ElfFunctions.WriteSections (SECTION_TEXT);\r | |
205 | ElfFunctions.WriteSections (SECTION_DATA);\r | |
206 | ElfFunctions.WriteSections (SECTION_HII);\r | |
207 | \r | |
208 | //\r | |
209 | // Translate and write relocations.\r | |
210 | //\r | |
211 | VerboseMsg ("Translate and write relocations.");\r | |
212 | ElfFunctions.WriteRelocations ();\r | |
213 | \r | |
214 | //\r | |
215 | // Write debug info.\r | |
216 | //\r | |
217 | VerboseMsg ("Write debug info.");\r | |
218 | ElfFunctions.WriteDebug ();\r | |
219 | \r | |
220 | //\r | |
221 | // Make sure image size is correct before returning the new image.\r | |
222 | //\r | |
223 | VerboseMsg ("Set image size.");\r | |
224 | ElfFunctions.SetImageSize ();\r | |
225 | \r | |
226 | //\r | |
227 | // Replace.\r | |
228 | //\r | |
229 | free (*FileBuffer);\r | |
230 | *FileBuffer = mCoffFile;\r | |
231 | *FileLength = mCoffOffset;\r | |
232 | \r | |
233 | //\r | |
234 | // Free resources used by ELF functions.\r | |
235 | //\r | |
236 | ElfFunctions.CleanUp ();\r | |
237 | \r | |
238 | return TRUE;\r | |
239 | }\r |