2 Function prototypes and defines on Memory Only PE COFF loader
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 Portion Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #ifndef __BASE_PE_COFF_LIB_H__
11 #define __BASE_PE_COFF_LIB_H__
14 // Return status codes from the PE/COFF Loader services
15 // BUGBUG: Find where used and see if can be replaced by RETURN_STATUS codes
17 #define IMAGE_ERROR_SUCCESS 0
18 #define IMAGE_ERROR_IMAGE_READ 1
19 #define IMAGE_ERROR_INVALID_PE_HEADER_SIGNATURE 2
20 #define IMAGE_ERROR_INVALID_MACHINE_TYPE 3
21 #define IMAGE_ERROR_INVALID_SUBSYSTEM 4
22 #define IMAGE_ERROR_INVALID_IMAGE_ADDRESS 5
23 #define IMAGE_ERROR_INVALID_IMAGE_SIZE 6
24 #define IMAGE_ERROR_INVALID_SECTION_ALIGNMENT 7
25 #define IMAGE_ERROR_SECTION_NOT_LOADED 8
26 #define IMAGE_ERROR_FAILED_RELOCATION 9
27 #define IMAGE_ERROR_FAILED_ICACHE_FLUSH 10
30 // Macro definitions for RISC-V architecture.
32 #define RV_X(x, s, n) (((x) >> (s)) & ((1<<(n))-1))
33 #define RISCV_IMM_BITS 12
34 #define RISCV_IMM_REACH (1LL<<RISCV_IMM_BITS)
35 #define RISCV_CONST_HIGH_PART(VALUE) \
36 (((VALUE) + (RISCV_IMM_REACH/2)) & ~(RISCV_IMM_REACH-1))
39 // PE/COFF Loader Read Function passed in by caller
43 (EFIAPI
*PE_COFF_LOADER_READ_FILE
) (
46 IN OUT UINTN
*ReadSize
,
51 // Context structure used while PE/COFF image is being loaded and relocated
54 PHYSICAL_ADDRESS ImageAddress
;
56 PHYSICAL_ADDRESS DestinationAddress
;
57 PHYSICAL_ADDRESS EntryPoint
;
58 PE_COFF_LOADER_READ_FILE ImageRead
;
61 UINT32 SectionAlignment
;
62 UINT32 PeCoffHeaderOffset
;
63 UINT32 DebugDirectoryEntryRva
;
67 UINT32 ImageCodeMemoryType
;
68 UINT32 ImageDataMemoryType
;
73 BOOLEAN RelocationsStripped
;
75 } PE_COFF_LOADER_IMAGE_CONTEXT
;
79 Retrieves information on a PE/COFF image
81 @param ImageContext The context of the image being loaded
83 @retval EFI_SUCCESS The information on the PE/COFF image was collected.
84 @retval EFI_INVALID_PARAMETER ImageContext is NULL.
85 @retval EFI_UNSUPPORTED The PE/COFF image is not supported.
86 @retval Otherwise The error status from reading the PE/COFF image using the
87 ImageContext->ImageRead() function
92 PeCoffLoaderGetImageInfo (
93 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
98 Relocates a PE/COFF image in memory
100 @param ImageContext Contains information on the loaded image to relocate
102 @retval EFI_SUCCESS if the PE/COFF image was relocated
103 @retval EFI_LOAD_ERROR if the image is not a valid PE/COFF image
104 @retval EFI_UNSUPPORTED not support
109 PeCoffLoaderRelocateImage (
110 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
115 Loads a PE/COFF image into memory
117 @param ImageContext Contains information on image to load into memory
119 @retval EFI_SUCCESS if the PE/COFF image was loaded
120 @retval EFI_BUFFER_TOO_SMALL if the caller did not provide a large enough buffer
121 @retval EFI_LOAD_ERROR if the image is a runtime driver with no relocations
122 @retval EFI_INVALID_PARAMETER if the image address is invalid
127 PeCoffLoaderLoadImage (
128 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
134 PeCoffLoaderGetPdbPointer (
141 PeCoffLoaderGetEntryPoint (
143 OUT VOID
**EntryPoint
,
144 OUT VOID
**BaseOfImage
149 // These functions are used by the ARM PE/COFF relocation code and by
150 // the ELF to PE/COFF converter so that is why they are public
154 Pass in a pointer to an ARM MOVT or MOVW immediate instruction and
155 return the immediate data encoded in the instruction
157 @param Instruction Pointer to ARM MOVT or MOVW immediate instruction
159 @return Immediate address encoded in the instruction
164 ThumbMovtImmediateAddress (
165 IN UINT16
*Instruction
169 Update an ARM MOVT or MOVW immediate instruction immediate data.
171 @param Instruction Pointer to ARM MOVT or MOVW immediate instruction
172 @param Address New address to patch into the instruction
177 ThumbMovtImmediatePatch (
178 IN OUT UINT16
*Instruction
,
184 Pass in a pointer to an ARM MOVW/MOVT instruction pair and
185 return the immediate data encoded in the two` instruction
187 @param Instructions Pointer to ARM MOVW/MOVT instruction pair
189 @return Immediate address encoded in the instructions
194 ThumbMovwMovtImmediateAddress (
195 IN UINT16
*Instructions
199 Update an ARM MOVW/MOVT immediate instruction instruction pair.
201 @param Instructions Pointer to ARM MOVW/MOVT instruction pair
202 @param Address New address to patch into the instructions
206 ThumbMovwMovtImmediatePatch (
207 IN OUT UINT16
*Instructions
,