1 /** @file
2   Declaration of internal functions in PE/COFF Lib.
3 
4   Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5   Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
6   SPDX-License-Identifier: BSD-2-Clause-Patent
7 
8 **/
9 
10 #ifndef __BASE_PECOFF_LIB_INTERNALS__
11 #define __BASE_PECOFF_LIB_INTERNALS__
12 
13 #include <Base.h>
14 #include <Library/PeCoffLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/PeCoffExtraActionLib.h>
18 #include <IndustryStandard/PeImage.h>
19 
20 //
21 // Macro definitions for RISC-V architecture.
22 //
23 #define RV_X(x, s, n) (((x) >> (s)) & ((1<<(n))-1))
24 #define RISCV_IMM_BITS 12
25 #define RISCV_IMM_REACH (1LL<<RISCV_IMM_BITS)
26 #define RISCV_CONST_HIGH_PART(VALUE) \
27   (((VALUE) + (RISCV_IMM_REACH/2)) & ~(RISCV_IMM_REACH-1))
28 
29 
30 /**
31   Performs an Itanium-based specific relocation fixup and is a no-op on other
32   instruction sets.
33 
34   @param  Reloc       The pointer to the relocation record.
35   @param  Fixup       The pointer to the address to fix up.
36   @param  FixupData   The pointer to a buffer to log the fixups.
37   @param  Adjust      The offset to adjust the fixup.
38 
39   @return Status code.
40 
41 **/
42 RETURN_STATUS
43 PeCoffLoaderRelocateImageEx (
44   IN UINT16      *Reloc,
45   IN OUT CHAR8   *Fixup,
46   IN OUT CHAR8   **FixupData,
47   IN UINT64      Adjust
48   );
49 
50 
51 /**
52   Performs an Itanium-based specific re-relocation fixup and is a no-op on other
53   instruction sets. This is used to re-relocated the image into the EFI virtual
54   space for runtime calls.
55 
56   @param  Reloc       The pointer to the relocation record.
57   @param  Fixup       The pointer to the address to fix up.
58   @param  FixupData   The pointer to a buffer to log the fixups.
59   @param  Adjust      The offset to adjust the fixup.
60 
61   @return Status code.
62 
63 **/
64 RETURN_STATUS
65 PeHotRelocateImageEx (
66   IN UINT16      *Reloc,
67   IN OUT CHAR8   *Fixup,
68   IN OUT CHAR8   **FixupData,
69   IN UINT64      Adjust
70   );
71 
72 
73 /**
74   Returns TRUE if the machine type of PE/COFF image is supported. Supported
75   does not mean the image can be executed it means the PE/COFF loader supports
76   loading and relocating of the image type. It's up to the caller to support
77   the entry point.
78 
79   @param  Machine   Machine type from the PE Header.
80 
81   @return TRUE if this PE/COFF loader can load the image
82 
83 **/
84 BOOLEAN
85 PeCoffLoaderImageFormatSupported (
86   IN  UINT16  Machine
87   );
88 
89 /**
90   Retrieves the magic value from the PE/COFF header.
91 
92   @param  Hdr             The buffer in which to return the PE32, PE32+, or TE header.
93 
94   @return EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC - Image is PE32
95   @return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC - Image is PE32+
96 
97 **/
98 UINT16
99 PeCoffLoaderGetPeHeaderMagicValue (
100   IN  EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION  Hdr
101   );
102 
103 /**
104   Retrieves the PE or TE Header from a PE/COFF or TE image.
105 
106   @param  ImageContext    The context of the image being loaded.
107   @param  Hdr             The buffer in which to return the PE32, PE32+, or TE header.
108 
109   @retval RETURN_SUCCESS  The PE or TE Header is read.
110   @retval Other           The error status from reading the PE/COFF or TE image using the ImageRead function.
111 
112 **/
113 RETURN_STATUS
114 PeCoffLoaderGetPeHeader (
115   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT         *ImageContext,
116   OUT    EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION  Hdr
117   );
118 
119 /**
120   Converts an image address to the loaded address.
121 
122   @param  ImageContext      The context of the image being loaded.
123   @param  Address           The address to be converted to the loaded address.
124   @param  TeStrippedOffset  Stripped offset for TE image.
125 
126   @return The converted address or NULL if the address can not be converted.
127 
128 **/
129 VOID *
130 PeCoffLoaderImageAddress (
131   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT          *ImageContext,
132   IN     UINTN                                 Address,
133   IN     UINTN                                 TeStrippedOffset
134   );
135 
136 #endif
137