Skip to content

Commit fe8263e

Browse files
authored
Merge pull request #18080 from jrose-apple/stare-into-the-void
2 parents 667b53c + 37e81db commit fe8263e

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

stdlib/public/runtime/ObjCRuntimeGetImageNameFromClass.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,6 @@ namespace {
100100
typedef struct segment_command macho_segment_command;
101101
#endif
102102

103-
struct patch_t {
104-
const char *name;
105-
const void *fn;
106-
107-
template<typename T>
108-
patch_t(const char *newName, const T *newFn)
109-
: name(newName), fn((const void*)newFn) {
110-
}
111-
};
112103
} // end anonymous namespace
113104

114105
/// Overwrite a cross-image symbol reference by directly editing symbol tables
@@ -121,7 +112,8 @@ namespace {
121112
///
122113
/// Also, if the symbol being patched has references within the image where it
123114
/// was originaly defined, those references will \e not be patched.
124-
static void patchLazyPointers(const mach_header *mh, patch_t patch) {
115+
static void patchLazyPointers(const mach_header *mh, const char *symbolName,
116+
const void *newValue) {
125117
// Get linkEditBase
126118
const uint32_t cmd_count = mh->ncmds;
127119
const load_command * const cmds =
@@ -205,10 +197,10 @@ static void patchLazyPointers(const mach_header *mh, patch_t patch) {
205197
// Found symbol for this lazy pointer, now lookup address.
206198
const char *lazyTargetName =
207199
&stringTable[symbolTable[symbolIndex].n_un.n_strx];
208-
if (strcmp(patch.name, lazyTargetName) == 0) {
200+
if (strcmp(symbolName, lazyTargetName) == 0) {
209201
// Can't use the value currently stored here because it may
210202
// be a dyld stub binder that will undo our patch if called.
211-
symbolPointers[lazyIndex] = (uintptr_t)patch.fn;
203+
symbolPointers[lazyIndex] = (uintptr_t)newValue;
212204
}
213205
}
214206
}
@@ -256,8 +248,9 @@ static const char *patchedGetImageNameFromClassForOldOSs(Class _Nullable cls) {
256248
static void patchGetImageNameInImage(const struct mach_header *mh,
257249
intptr_t vmaddr_slide) {
258250
(void)vmaddr_slide;
259-
patchLazyPointers(mh, patch_t("_class_getImageName",
260-
&patchedGetImageNameFromClassForOldOSs));
251+
const void *newImplementationAddr =
252+
reinterpret_cast<const void *>(&patchedGetImageNameFromClassForOldOSs);
253+
patchLazyPointers(mh, "_class_getImageName", newImplementationAddr);
261254
}
262255

263256
/***************************************************************************/

0 commit comments

Comments
 (0)