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