4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "WinNtInclude.h"
23 #include <Common/UefiBaseTypes.h>
24 #include <IndustryStandard/PeImage.h>
26 #include "EfiUtilityMsgs.h"
29 #include "ElfConvert.h"
30 #include "Elf32Convert.h"
31 #include "Elf64Convert.h"
34 // Result Coff file in memory.
36 UINT8
*mCoffFile
= NULL
;
39 // COFF relocation data
41 EFI_IMAGE_BASE_RELOCATION
*mCoffBaseRel
;
42 UINT16
*mCoffEntryRel
;
45 // Current offset in coff file.
50 // Offset in Coff file of headers and sections.
57 UINT32 mFileBufferSize
;
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 * MAX_COFF_ALIGNMENT
101 if (mCoffFile
== NULL
) {
102 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
104 assert (mCoffFile
!= NULL
);
106 mCoffFile
+ mCoffOffset
, 0,
107 sizeof(EFI_IMAGE_BASE_RELOCATION
) + 2 * MAX_COFF_ALIGNMENT
110 mCoffBaseRel
= (EFI_IMAGE_BASE_RELOCATION
*)(mCoffFile
+ mCoffOffset
);
111 mCoffBaseRel
->VirtualAddress
= Offset
& ~0xfff;
112 mCoffBaseRel
->SizeOfBlock
= sizeof(EFI_IMAGE_BASE_RELOCATION
);
114 mCoffEntryRel
= (UINT16
*)(mCoffBaseRel
+ 1);
115 mCoffOffset
+= sizeof(EFI_IMAGE_BASE_RELOCATION
);
121 CoffAddFixupEntry((UINT16
) ((Type
<< 12) | (Offset
& 0xfff)));
125 CreateSectionHeader (
132 EFI_IMAGE_SECTION_HEADER
*Hdr
;
133 Hdr
= (EFI_IMAGE_SECTION_HEADER
*)(mCoffFile
+ mTableOffset
);
135 strcpy((char *)Hdr
->Name
, Name
);
136 Hdr
->Misc
.VirtualSize
= Size
;
137 Hdr
->VirtualAddress
= Offset
;
138 Hdr
->SizeOfRawData
= Size
;
139 Hdr
->PointerToRawData
= Offset
;
140 Hdr
->PointerToRelocations
= 0;
141 Hdr
->PointerToLinenumbers
= 0;
142 Hdr
->NumberOfRelocations
= 0;
143 Hdr
->NumberOfLinenumbers
= 0;
144 Hdr
->Characteristics
= Flags
;
146 mTableOffset
+= sizeof (EFI_IMAGE_SECTION_HEADER
);
150 //*****************************************************************************
151 // Functions called from GenFw main code.
152 //*****************************************************************************
160 return (FileBuffer
[EI_MAG0
] == ELFMAG0
&&
161 FileBuffer
[EI_MAG1
] == ELFMAG1
&&
162 FileBuffer
[EI_MAG2
] == ELFMAG2
&&
163 FileBuffer
[EI_MAG3
] == ELFMAG3
);
172 ELF_FUNCTION_TABLE ElfFunctions
;
175 mFileBufferSize
= *FileLength
;
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 if (!ElfFunctions
.WriteSections (SECTION_TEXT
)) {
207 if (!ElfFunctions
.WriteSections (SECTION_DATA
)) {
210 if (!ElfFunctions
.WriteSections (SECTION_HII
)) {
215 // Translate and write relocations.
217 VerboseMsg ("Translate and write relocations.");
218 ElfFunctions
.WriteRelocations ();
223 VerboseMsg ("Write debug info.");
224 ElfFunctions
.WriteDebug ();
227 // Make sure image size is correct before returning the new image.
229 VerboseMsg ("Set image size.");
230 ElfFunctions
.SetImageSize ();
236 *FileBuffer
= mCoffFile
;
237 *FileLength
= mCoffOffset
;
240 // Free resources used by ELF functions.
242 ElfFunctions
.CleanUp ();