]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BasePeCoffLib/RiscV/PeCoffLoaderEx.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / BasePeCoffLib / RiscV / PeCoffLoaderEx.c
1 /** @file
2 PE/Coff loader for RISC-V PE image
3
4 Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6 **/
7 #include "BasePeCoffLibInternals.h"
8 #include <Library/BaseLib.h>
9
10 /**
11 Performs an RISC-V specific relocation fixup and is a no-op on
12 other instruction sets.
13 RISC-V splits 32-bit fixup into 20bit and 12-bit with two relocation
14 types. We have to know the lower 12-bit fixup first then we can deal
15 carry over on high 20-bit fixup. So we log the high 20-bit in
16 FixupData.
17
18 @param Reloc The pointer to the relocation record.
19 @param Fixup The pointer to the address to fix up.
20 @param FixupData The pointer to a buffer to log the fixups.
21 @param Adjust The offset to adjust the fixup.
22
23 @return Status code.
24
25 **/
26 RETURN_STATUS
27 PeCoffLoaderRelocateImageEx (
28 IN UINT16 *Reloc,
29 IN OUT CHAR8 *Fixup,
30 IN OUT CHAR8 **FixupData,
31 IN UINT64 Adjust
32 )
33 {
34 UINT32 Value;
35 UINT32 Value2;
36 UINT32 *RiscVHi20Fixup;
37
38 switch ((*Reloc) >> 12) {
39 case EFI_IMAGE_REL_BASED_RISCV_HI20:
40 *(UINT64 *)(*FixupData) = (UINT64)(UINTN)Fixup;
41 break;
42
43 case EFI_IMAGE_REL_BASED_RISCV_LOW12I:
44 RiscVHi20Fixup = (UINT32 *)(*(UINT64 *)(*FixupData));
45 if (RiscVHi20Fixup != NULL) {
46 Value = (UINT32)(RV_X (*RiscVHi20Fixup, 12, 20) << 12);
47 Value2 = (UINT32)(RV_X (*(UINT32 *)Fixup, 20, 12));
48 if (Value2 & (RISCV_IMM_REACH/2)) {
49 Value2 |= ~(RISCV_IMM_REACH-1);
50 }
51
52 Value += Value2;
53 Value += (UINT32)Adjust;
54 Value2 = RISCV_CONST_HIGH_PART (Value);
55 *(UINT32 *)RiscVHi20Fixup = (RV_X (Value2, 12, 20) << 12) | \
56 (RV_X (*(UINT32 *)RiscVHi20Fixup, 0, 12));
57 *(UINT32 *)Fixup = (RV_X (Value, 0, 12) << 20) | \
58 (RV_X (*(UINT32 *)Fixup, 0, 20));
59 }
60
61 break;
62
63 case EFI_IMAGE_REL_BASED_RISCV_LOW12S:
64 RiscVHi20Fixup = (UINT32 *)(*(UINT64 *)(*FixupData));
65 if (RiscVHi20Fixup != NULL) {
66 Value = (UINT32)(RV_X (*RiscVHi20Fixup, 12, 20) << 12);
67 Value2 = (UINT32)(RV_X (*(UINT32 *)Fixup, 7, 5) | (RV_X (*(UINT32 *)Fixup, 25, 7) << 5));
68 if (Value2 & (RISCV_IMM_REACH/2)) {
69 Value2 |= ~(RISCV_IMM_REACH-1);
70 }
71
72 Value += Value2;
73 Value += (UINT32)Adjust;
74 Value2 = RISCV_CONST_HIGH_PART (Value);
75 *(UINT32 *)RiscVHi20Fixup = (RV_X (Value2, 12, 20) << 12) | \
76 (RV_X (*(UINT32 *)RiscVHi20Fixup, 0, 12));
77 Value2 = *(UINT32 *)Fixup & 0x01fff07f;
78 Value &= RISCV_IMM_REACH - 1;
79 *(UINT32 *)Fixup = Value2 | (UINT32)(((RV_X (Value, 0, 5) << 7) | (RV_X (Value, 5, 7) << 25)));
80 }
81
82 break;
83
84 default:
85 return RETURN_UNSUPPORTED;
86 }
87
88 return RETURN_SUCCESS;
89 }
90
91 /**
92 Returns TRUE if the machine type of PE/COFF image is supported. Supported
93 does not mean the image can be executed it means the PE/COFF loader supports
94 loading and relocating of the image type. It's up to the caller to support
95 the entry point.
96
97 @param Machine Machine type from the PE Header.
98
99 @return TRUE if this PE/COFF loader can load the image
100
101 **/
102 BOOLEAN
103 PeCoffLoaderImageFormatSupported (
104 IN UINT16 Machine
105 )
106 {
107 if (Machine == IMAGE_FILE_MACHINE_RISCV64) {
108 return TRUE;
109 }
110
111 return FALSE;
112 }
113
114 /**
115 Performs an Itanium-based specific re-relocation fixup and is a no-op on other
116 instruction sets. This is used to re-relocated the image into the EFI virtual
117 space for runtime calls.
118
119 @param Reloc The pointer to the relocation record.
120 @param Fixup The pointer to the address to fix up.
121 @param FixupData The pointer to a buffer to log the fixups.
122 @param Adjust The offset to adjust the fixup.
123
124 @return Status code.
125
126 **/
127 RETURN_STATUS
128 PeHotRelocateImageEx (
129 IN UINT16 *Reloc,
130 IN OUT CHAR8 *Fixup,
131 IN OUT CHAR8 **FixupData,
132 IN UINT64 Adjust
133 )
134 {
135 return RETURN_UNSUPPORTED;
136 }