OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
Relocations.c
Go to the documentation of this file.
1
15#include <Uefi.h>
16
18
19#include <Library/DebugLib.h>
20#include <Library/OcMachoLib.h>
21
22#include "OcMachoLibInternal.h"
23
31BOOLEAN
33 IN UINT8 Type
34 )
35{
37}
38
46BOOLEAN
48 IN UINT8 Type
49 )
50{
51 return (Type == MachX8664RelocSubtractor);
52}
53
61BOOLEAN
63 IN UINT8 Type
64 )
65{
66 return (Type == MachX8664RelocUnsigned);
67}
68
75BOOLEAN
77 IN UINT8 Type
78 )
79{
80 return (Type == MachX8664RelocUnsigned);
81}
82
91STATIC
94 IN UINT64 Address,
95 IN UINT32 NumRelocs,
96 IN MACH_RELOCATION_INFO *Relocs
97 )
98{
99 UINT32 Index;
100 MACH_RELOCATION_INFO *Relocation;
101
102 for (Index = 0; Index < NumRelocs; ++Index) {
103 Relocation = &Relocs[Index];
104 //
105 // A section-based relocation entry can be skipped for absolute symbols.
106 //
107 if ( (Relocation->Extern == 0)
108 && (Relocation->SymbolNumber == MACH_RELOC_ABSOLUTE))
109 {
110 continue;
111 }
112
113 if ((UINT64)Relocation->Address == Address) {
114 return Relocation;
115 }
116
117 //
118 // Relocation Pairs can be skipped.
119 // Assumption: Intel X64. Currently verified by the Context
120 // initialization.
121 //
122 if (MachoRelocationIsPairIntel64 ((UINT8)Relocation->Type)) {
123 if (Index == (MAX_UINT32 - 1)) {
124 break;
125 }
126
127 ++Index;
128 }
129 }
130
131 return NULL;
132}
133
134STATIC
137 IN OUT OC_MACHO_CONTEXT *Context,
138 IN UINT64 Address,
139 IN BOOLEAN External
140 )
141{
142 MACH_SECTION_ANY *Section;
143 UINT32 SectionIndex;
144 UINT32 Index;
145
146 MACH_RELOCATION_INFO *Relocations;
147 MACH_RELOCATION_INFO *Relocation;
148 UINT32 RelocationCount;
149
150 SectionIndex = 0;
151 while (TRUE) {
152 Section = MachoGetSectionByIndex (Context, SectionIndex);
153 if (Section == NULL) {
154 break;
155 }
156
157 //
158 // Each section has its own relocations table.
159 //
160 RelocationCount = Context->Is32Bit ? Section->Section32.NumRelocations : Section->Section64.NumRelocations;
161 if (RelocationCount > 0) {
162 Relocations = (MACH_RELOCATION_INFO *)(((UINTN)(Context->FileData))
163 + (Context->Is32Bit ? Section->Section32.RelocationsOffset : Section->Section64.RelocationsOffset));
164
165 for (Index = 0; Index < RelocationCount; Index++) {
166 Relocation = &Relocations[Index];
167 //
168 // A section-based relocation entry can be skipped for absolute symbols.
169 //
170 if ( (Relocation->Extern == 0)
171 && (Relocation->SymbolNumber == MACH_RELOC_ABSOLUTE))
172 {
173 continue;
174 }
175
176 //
177 // Filter out the other relocation type.
178 //
179 if (Relocation->Extern != (UINT32)(External ? 1 : 0)) {
180 continue;
181 }
182
183 if (Context->Is32Bit) {
184 if (((UINT32)Relocation->Address + Section->Section32.Address) == Address) {
185 return Relocation;
186 }
187 } else {
188 if (((UINT64)Relocation->Address + Section->Section64.Address) == Address) {
189 return Relocation;
190 }
191 }
192 }
193 }
194
195 SectionIndex++;
196 }
197
198 return NULL;
199}
200
212 IN OUT OC_MACHO_CONTEXT *Context,
213 IN UINT64 Address
214 )
215{
216 //
217 // MH_OBJECT does not have a DYSYMTAB.
218 //
219 if (Context->DySymtab == NULL) {
221 Context,
222 Address,
223 TRUE
224 );
225 }
226
228 Address,
229 Context->DySymtab->NumExternalRelocations,
230 Context->ExternRelocations
231 );
232}
233
245 IN OUT OC_MACHO_CONTEXT *Context,
246 IN UINT64 Address
247 )
248{
249 //
250 // MH_OBJECT does not have a DYSYMTAB.
251 //
252 if (Context->DySymtab == NULL) {
254 Context,
255 Address,
256 FALSE
257 );
258 }
259
261 Address,
262 Context->DySymtab->NumOfLocalRelocations,
263 Context->LocalRelocations
264 );
265}
#define MACH_RELOC_ABSOLUTE
absolute relocation type for Mach-O files
@ MachGenericRelocSectDiff
@ MachGenericRelocLocalSectDiff
@ MachX8664RelocUnsigned
for absolute addresses
@ MachX8664RelocSubtractor
MACH_SECTION_ANY * MachoGetSectionByIndex(IN OUT OC_MACHO_CONTEXT *Context, IN UINT32 Index)
Definition Header.c:188
MACH_RELOCATION_INFO * InternalGetExternRelocationByOffset(IN OUT OC_MACHO_CONTEXT *Context, IN UINT64 Address)
STATIC MACH_RELOCATION_INFO * InternalLookupSectionRelocationByOffset(IN OUT OC_MACHO_CONTEXT *Context, IN UINT64 Address, IN BOOLEAN External)
STATIC MACH_RELOCATION_INFO * InternalLookupRelocationByOffset(IN UINT64 Address, IN UINT32 NumRelocs, IN MACH_RELOCATION_INFO *Relocs)
Definition Relocations.c:93
BOOLEAN MachoRelocationIsPairIntel64(IN UINT8 Type)
Definition Relocations.c:47
BOOLEAN MachoPreserveRelocationIntel64(IN UINT8 Type)
Definition Relocations.c:76
BOOLEAN MachoRelocationIsPairIntel32(IN UINT8 Type)
Definition Relocations.c:32
BOOLEAN MachoIsRelocationPairTypeIntel64(IN UINT8 Type)
Definition Relocations.c:62
MACH_RELOCATION_INFO * InternalGetLocalRelocationByOffset(IN OUT OC_MACHO_CONTEXT *Context, IN UINT64 Address)
UINT32 Extern
does not include value of sym referenced
UINT32 Type
if not 0, machine specific relocation type
UINT32 RelocationsOffset
file offset of relocation entries
UINT64 Address
memory address of this section
UINT32 NumRelocations
number of relocation entries
UINT32 Address
memory address of this section
UINT32 NumRelocations
number of relocation entries
UINT32 RelocationsOffset
file offset of relocation entries
MACH_SECTION Section32
MACH_SECTION_64 Section64