3 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "WinNtInclude.h"
27 #include <Common/UefiBaseTypes.h>
28 #include <IndustryStandard/PeImage.h>
30 #include "EfiUtilityMsgs.h"
33 #include "ElfConvert.h"
34 #include "Elf32Convert.h"
35 #include "Elf64Convert.h"
38 // Result Coff file in memory.
40 UINT8
*mCoffFile
= NULL
;
43 // COFF relocation data
45 EFI_IMAGE_BASE_RELOCATION
*mCoffBaseRel
;
46 UINT16
*mCoffEntryRel
;
49 // Current offset in coff file.
54 // Offset in Coff file of headers and sections.
59 //*****************************************************************************
60 // Common ELF Functions
61 //*****************************************************************************
71 mCoffBaseRel
->SizeOfBlock
+= 2;
81 if (mCoffBaseRel
== NULL
82 || mCoffBaseRel
->VirtualAddress
!= (Offset
& ~0xfff)) {
83 if (mCoffBaseRel
!= NULL
) {
85 // Add a null entry (is it required ?)
87 CoffAddFixupEntry (0);
92 if (mCoffOffset
% 4 != 0)
93 CoffAddFixupEntry (0);
98 mCoffOffset
+ sizeof(EFI_IMAGE_BASE_RELOCATION
) + 2*0x1000
101 mCoffFile
+ mCoffOffset
, 0,
102 sizeof(EFI_IMAGE_BASE_RELOCATION
) + 2*0x1000
105 mCoffBaseRel
= (EFI_IMAGE_BASE_RELOCATION
*)(mCoffFile
+ mCoffOffset
);
106 mCoffBaseRel
->VirtualAddress
= Offset
& ~0xfff;
107 mCoffBaseRel
->SizeOfBlock
= sizeof(EFI_IMAGE_BASE_RELOCATION
);
109 mCoffEntryRel
= (UINT16
*)(mCoffBaseRel
+ 1);
110 mCoffOffset
+= sizeof(EFI_IMAGE_BASE_RELOCATION
);
116 CoffAddFixupEntry((UINT16
) ((Type
<< 12) | (Offset
& 0xfff)));
120 CreateSectionHeader (
127 EFI_IMAGE_SECTION_HEADER
*Hdr
;
128 Hdr
= (EFI_IMAGE_SECTION_HEADER
*)(mCoffFile
+ mTableOffset
);
130 strcpy((char *)Hdr
->Name
, Name
);
131 Hdr
->Misc
.VirtualSize
= Size
;
132 Hdr
->VirtualAddress
= Offset
;
133 Hdr
->SizeOfRawData
= Size
;
134 Hdr
->PointerToRawData
= Offset
;
135 Hdr
->PointerToRelocations
= 0;
136 Hdr
->PointerToLinenumbers
= 0;
137 Hdr
->NumberOfRelocations
= 0;
138 Hdr
->NumberOfLinenumbers
= 0;
139 Hdr
->Characteristics
= Flags
;
141 mTableOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
145 //*****************************************************************************
146 // Functions called from GenFw main code.
147 //*****************************************************************************
155 return (FileBuffer
[EI_MAG0
] == ELFMAG0
&&
156 FileBuffer
[EI_MAG1
] == ELFMAG1
&&
157 FileBuffer
[EI_MAG2
] == ELFMAG2
&&
158 FileBuffer
[EI_MAG3
] == ELFMAG3
);
167 ELF_FUNCTION_TABLE ElfFunctions
;
171 // Determine ELF type and set function table pointer correctly.
173 VerboseMsg ("Check Elf Image Header");
174 EiClass
= (*FileBuffer
)[EI_CLASS
];
175 if (EiClass
== ELFCLASS32
) {
176 if (!InitializeElf32 (*FileBuffer
, &ElfFunctions
)) {
179 } else if (EiClass
== ELFCLASS64
) {
180 if (!InitializeElf64 (*FileBuffer
, &ElfFunctions
)) {
184 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_CLASS not supported.");
189 // Compute sections new address.
191 VerboseMsg ("Compute sections new address.");
192 ElfFunctions
.ScanSections ();
195 // Write and relocate sections.
197 VerboseMsg ("Write and relocate sections.");
198 ElfFunctions
.WriteSections (SECTION_TEXT
);
199 ElfFunctions
.WriteSections (SECTION_DATA
);
200 ElfFunctions
.WriteSections (SECTION_HII
);
203 // Translate and write relocations.
205 VerboseMsg ("Translate and write relocations.");
206 ElfFunctions
.WriteRelocations ();
211 VerboseMsg ("Write debug info.");
212 ElfFunctions
.WriteDebug ();
215 // Make sure image size is correct before returning the new image.
217 VerboseMsg ("Set image size.");
218 ElfFunctions
.SetImageSize ();
224 *FileBuffer
= mCoffFile
;
225 *FileLength
= mCoffOffset
;
228 // Free resources used by ELF functions.
230 ElfFunctions
.CleanUp ();