4 Copyright (c) 2010 - 2016, 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.
61 //*****************************************************************************
62 // Common ELF Functions
63 //*****************************************************************************
73 mCoffBaseRel
->SizeOfBlock
+= 2;
83 if (mCoffBaseRel
== NULL
84 || mCoffBaseRel
->VirtualAddress
!= (Offset
& ~0xfff)) {
85 if (mCoffBaseRel
!= NULL
) {
87 // Add a null entry (is it required ?)
89 CoffAddFixupEntry (0);
94 if (mCoffOffset
% 4 != 0)
95 CoffAddFixupEntry (0);
100 mCoffOffset
+ sizeof(EFI_IMAGE_BASE_RELOCATION
) + 2 * MAX_COFF_ALIGNMENT
102 if (mCoffFile
== NULL
) {
103 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
105 assert (mCoffFile
!= NULL
);
107 mCoffFile
+ mCoffOffset
, 0,
108 sizeof(EFI_IMAGE_BASE_RELOCATION
) + 2 * MAX_COFF_ALIGNMENT
111 mCoffBaseRel
= (EFI_IMAGE_BASE_RELOCATION
*)(mCoffFile
+ mCoffOffset
);
112 mCoffBaseRel
->VirtualAddress
= Offset
& ~0xfff;
113 mCoffBaseRel
->SizeOfBlock
= sizeof(EFI_IMAGE_BASE_RELOCATION
);
115 mCoffEntryRel
= (UINT16
*)(mCoffBaseRel
+ 1);
116 mCoffOffset
+= sizeof(EFI_IMAGE_BASE_RELOCATION
);
122 CoffAddFixupEntry((UINT16
) ((Type
<< 12) | (Offset
& 0xfff)));
126 CreateSectionHeader (
133 EFI_IMAGE_SECTION_HEADER
*Hdr
;
134 Hdr
= (EFI_IMAGE_SECTION_HEADER
*)(mCoffFile
+ mTableOffset
);
136 strcpy((char *)Hdr
->Name
, Name
);
137 Hdr
->Misc
.VirtualSize
= Size
;
138 Hdr
->VirtualAddress
= Offset
;
139 Hdr
->SizeOfRawData
= Size
;
140 Hdr
->PointerToRawData
= Offset
;
141 Hdr
->PointerToRelocations
= 0;
142 Hdr
->PointerToLinenumbers
= 0;
143 Hdr
->NumberOfRelocations
= 0;
144 Hdr
->NumberOfLinenumbers
= 0;
145 Hdr
->Characteristics
= Flags
;
147 mTableOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
151 //*****************************************************************************
152 // Functions called from GenFw main code.
153 //*****************************************************************************
161 return (FileBuffer
[EI_MAG0
] == ELFMAG0
&&
162 FileBuffer
[EI_MAG1
] == ELFMAG1
&&
163 FileBuffer
[EI_MAG2
] == ELFMAG2
&&
164 FileBuffer
[EI_MAG3
] == ELFMAG3
);
173 ELF_FUNCTION_TABLE ElfFunctions
;
177 // Determine ELF type and set function table pointer correctly.
179 VerboseMsg ("Check Elf Image Header");
180 EiClass
= (*FileBuffer
)[EI_CLASS
];
181 if (EiClass
== ELFCLASS32
) {
182 if (!InitializeElf32 (*FileBuffer
, &ElfFunctions
)) {
185 } else if (EiClass
== ELFCLASS64
) {
186 if (!InitializeElf64 (*FileBuffer
, &ElfFunctions
)) {
190 Error (NULL
, 0, 3000, "Unsupported", "ELF EI_CLASS not supported.");
195 // Compute sections new address.
197 VerboseMsg ("Compute sections new address.");
198 ElfFunctions
.ScanSections ();
201 // Write and relocate sections.
203 VerboseMsg ("Write and relocate sections.");
204 ElfFunctions
.WriteSections (SECTION_TEXT
);
205 ElfFunctions
.WriteSections (SECTION_DATA
);
206 ElfFunctions
.WriteSections (SECTION_HII
);
209 // Translate and write relocations.
211 VerboseMsg ("Translate and write relocations.");
212 ElfFunctions
.WriteRelocations ();
217 VerboseMsg ("Write debug info.");
218 ElfFunctions
.WriteDebug ();
221 // Make sure image size is correct before returning the new image.
223 VerboseMsg ("Set image size.");
224 ElfFunctions
.SetImageSize ();
230 *FileBuffer
= mCoffFile
;
231 *FileLength
= mCoffOffset
;
234 // Free resources used by ELF functions.
236 ElfFunctions
.CleanUp ();