4 Copyright (c) 2010 - 2014, 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"
28 #include <Common/UefiBaseTypes.h>
29 #include <IndustryStandard/PeImage.h>
31 #include "EfiUtilityMsgs.h"
34 #include "ElfConvert.h"
35 #include "Elf32Convert.h"
36 #include "Elf64Convert.h"
39 // Result Coff file in memory.
41 UINT8
*mCoffFile
= NULL
;
44 // COFF relocation data
46 EFI_IMAGE_BASE_RELOCATION
*mCoffBaseRel
;
47 UINT16
*mCoffEntryRel
;
50 // Current offset in coff file.
55 // Offset in Coff file of headers and sections.
60 //*****************************************************************************
61 // Common ELF Functions
62 //*****************************************************************************
72 mCoffBaseRel
->SizeOfBlock
+= 2;
82 if (mCoffBaseRel
== NULL
83 || mCoffBaseRel
->VirtualAddress
!= (Offset
& ~0xfff)) {
84 if (mCoffBaseRel
!= NULL
) {
86 // Add a null entry (is it required ?)
88 CoffAddFixupEntry (0);
93 if (mCoffOffset
% 4 != 0)
94 CoffAddFixupEntry (0);
99 mCoffOffset
+ sizeof(EFI_IMAGE_BASE_RELOCATION
) + 2*0x1000
102 mCoffFile
+ mCoffOffset
, 0,
103 sizeof(EFI_IMAGE_BASE_RELOCATION
) + 2*0x1000
106 mCoffBaseRel
= (EFI_IMAGE_BASE_RELOCATION
*)(mCoffFile
+ mCoffOffset
);
107 mCoffBaseRel
->VirtualAddress
= Offset
& ~0xfff;
108 mCoffBaseRel
->SizeOfBlock
= sizeof(EFI_IMAGE_BASE_RELOCATION
);
110 mCoffEntryRel
= (UINT16
*)(mCoffBaseRel
+ 1);
111 mCoffOffset
+= sizeof(EFI_IMAGE_BASE_RELOCATION
);
117 CoffAddFixupEntry((UINT16
) ((Type
<< 12) | (Offset
& 0xfff)));
121 CreateSectionHeader (
128 EFI_IMAGE_SECTION_HEADER
*Hdr
;
129 Hdr
= (EFI_IMAGE_SECTION_HEADER
*)(mCoffFile
+ mTableOffset
);
131 strcpy((char *)Hdr
->Name
, Name
);
132 Hdr
->Misc
.VirtualSize
= Size
;
133 Hdr
->VirtualAddress
= Offset
;
134 Hdr
->SizeOfRawData
= Size
;
135 Hdr
->PointerToRawData
= Offset
;
136 Hdr
->PointerToRelocations
= 0;
137 Hdr
->PointerToLinenumbers
= 0;
138 Hdr
->NumberOfRelocations
= 0;
139 Hdr
->NumberOfLinenumbers
= 0;
140 Hdr
->Characteristics
= Flags
;
142 mTableOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
146 //*****************************************************************************
147 // Functions called from GenFw main code.
148 //*****************************************************************************
156 return (FileBuffer
[EI_MAG0
] == ELFMAG0
&&
157 FileBuffer
[EI_MAG1
] == ELFMAG1
&&
158 FileBuffer
[EI_MAG2
] == ELFMAG2
&&
159 FileBuffer
[EI_MAG3
] == ELFMAG3
);
168 ELF_FUNCTION_TABLE ElfFunctions
;
172 // Determine ELF type and set function table pointer correctly.
174 VerboseMsg ("Check Elf Image Header");
175 EiClass
= (*FileBuffer
)[EI_CLASS
];
176 if (EiClass
== ELFCLASS32
) {
177 if (!InitializeElf32 (*FileBuffer
, &ElfFunctions
)) {
180 } else if (EiClass
== ELFCLASS64
) {
181 if (!InitializeElf64 (*FileBuffer
, &ElfFunctions
)) {
185 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_CLASS not supported.");
190 // Compute sections new address.
192 VerboseMsg ("Compute sections new address.");
193 ElfFunctions
.ScanSections ();
196 // Write and relocate sections.
198 VerboseMsg ("Write and relocate sections.");
199 ElfFunctions
.WriteSections (SECTION_TEXT
);
200 ElfFunctions
.WriteSections (SECTION_DATA
);
201 ElfFunctions
.WriteSections (SECTION_HII
);
204 // Translate and write relocations.
206 VerboseMsg ("Translate and write relocations.");
207 ElfFunctions
.WriteRelocations ();
212 VerboseMsg ("Write debug info.");
213 ElfFunctions
.WriteDebug ();
216 // Make sure image size is correct before returning the new image.
218 VerboseMsg ("Set image size.");
219 ElfFunctions
.SetImageSize ();
225 *FileBuffer
= mCoffFile
;
226 *FileLength
= mCoffOffset
;
229 // Free resources used by ELF functions.
231 ElfFunctions
.CleanUp ();