]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BasePeCoffLib/RiscV/PeCoffLoaderEx.c
MdePkg/BasePeCoff: Add RISC-V PE/Coff related code.
[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
47 Value = (UINT32)(RV_X(*RiscVHi20Fixup, 12, 20) << 12);
48 Value2 = (UINT32)(RV_X(*(UINT32 *)Fixup, 20, 12));
49 if (Value2 & (RISCV_IMM_REACH/2)) {
50 Value2 |= ~(RISCV_IMM_REACH-1);
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 break;
61
62 case EFI_IMAGE_REL_BASED_RISCV_LOW12S:
63 RiscVHi20Fixup = (UINT32 *)(*(UINT64 *)(*FixupData));
64 if (RiscVHi20Fixup != NULL) {
65 Value = (UINT32)(RV_X(*RiscVHi20Fixup, 12, 20) << 12);
66 Value2 = (UINT32)(RV_X(*(UINT32 *)Fixup, 7, 5) | (RV_X(*(UINT32 *)Fixup, 25, 7) << 5));
67 if (Value2 & (RISCV_IMM_REACH/2)) {
68 Value2 |= ~(RISCV_IMM_REACH-1);
69 }
70 Value += Value2;
71 Value += (UINT32)Adjust;
72 Value2 = RISCV_CONST_HIGH_PART (Value);
73 *(UINT32 *)RiscVHi20Fixup = (RV_X (Value2, 12, 20) << 12) | \
74 (RV_X (*(UINT32 *)RiscVHi20Fixup, 0, 12));
75 Value2 = *(UINT32 *)Fixup & 0x01fff07f;
76 Value &= RISCV_IMM_REACH - 1;
77 *(UINT32 *)Fixup = Value2 | (UINT32)(((RV_X(Value, 0, 5) << 7) | (RV_X(Value, 5, 7) << 25)));
78 }
79 break;
80
81 default:
82 return RETURN_UNSUPPORTED;
83
84 }
85 return RETURN_SUCCESS;
86 }
87
88 /**
89 Returns TRUE if the machine type of PE/COFF image is supported. Supported
90 does not mean the image can be executed it means the PE/COFF loader supports
91 loading and relocating of the image type. It's up to the caller to support
92 the entry point.
93
94 @param Machine Machine type from the PE Header.
95
96 @return TRUE if this PE/COFF loader can load the image
97
98 **/
99 BOOLEAN
100 PeCoffLoaderImageFormatSupported (
101 IN UINT16 Machine
102 )
103 {
104 if (Machine == IMAGE_FILE_MACHINE_RISCV64) {
105 return TRUE;
106 }
107
108 return FALSE;
109 }
110
111 /**
112 Performs an Itanium-based specific re-relocation fixup and is a no-op on other
113 instruction sets. This is used to re-relocated the image into the EFI virtual
114 space for runtime calls.
115
116 @param Reloc The pointer to the relocation record.
117 @param Fixup The pointer to the address to fix up.
118 @param FixupData The pointer to a buffer to log the fixups.
119 @param Adjust The offset to adjust the fixup.
120
121 @return Status code.
122
123 **/
124 RETURN_STATUS
125 PeHotRelocateImageEx (
126 IN UINT16 *Reloc,
127 IN OUT CHAR8 *Fixup,
128 IN OUT CHAR8 **FixupData,
129 IN UINT64 Adjust
130 )
131 {
132 return RETURN_UNSUPPORTED;
133 }