Skip to content

Commit 46ebb9d

Browse files
authored
Merge pull request #68040 from mikeash/weak-dyld-lookup-section-info-5.9
[5.9][Runtime] Redeclare _dyld_lookup_section_info as weak, don't use enum _dyld_section_location_kind.
2 parents 7ffce19 + 4deb127 commit 46ebb9d

File tree

1 file changed

+41
-22
lines changed

1 file changed

+41
-22
lines changed

stdlib/public/runtime/ImageInspectionMachO.cpp

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,26 @@ enum _dyld_section_location_kind {
4949

5050
#endif
5151

52-
#if !OBJC_ADDLOADIMAGEFUNC2_DEFINED
52+
// Bring our own definition of _dyld_section_location constants in case we're
53+
// using an older SDK that doesn't have them.
54+
#define _dyld_section_location_text_swift5_protos 0
55+
#define _dyld_section_location_text_swift5_proto 1
56+
#define _dyld_section_location_text_swift5_types 2
57+
#define _dyld_section_location_text_swift5_replace 3
58+
#define _dyld_section_location_text_swift5_replace2 4
59+
#define _dyld_section_location_text_swift5_ac_funcs 5
60+
61+
#if OBJC_ADDLOADIMAGEFUNC2_DEFINED
62+
// Redefine _dyld_lookup_section_info as weak so we can build against it but
63+
// still run when it's not present at runtime. Note that we don't have to check
64+
// for its presence at runtime, as it's guaranteed to be available if we get
65+
// the callbacks from objc_addLoadImageFunc2.
66+
LLVM_ATTRIBUTE_WEAK
67+
struct _dyld_section_info_result
68+
_dyld_lookup_section_info(const struct mach_header *mh,
69+
_dyld_section_location_info_t locationHandle,
70+
enum _dyld_section_location_kind kind);
71+
#else
5372
// If we don't have objc_addLoadImageFunc2, fall back to objc_addLoadImageFunc.
5473
// Use a #define so we don't have to conditionalize the calling code below.
5574
#define objc_addLoadImageFunc2 objc_addLoadImageFunc
@@ -79,8 +98,7 @@ using mach_header_platform = mach_header;
7998
#endif
8099

81100
// Callback for objc_addLoadImageFunc that just takes a mach_header.
82-
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
83-
enum _dyld_section_location_kind SECTION_KIND,
101+
template <const char *SEGMENT_NAME, const char *SECTION_NAME, int SECTION_KIND,
84102
void CONSUME_BLOCK(const void *baseAddress, const void *start,
85103
uintptr_t size)>
86104
void addImageCallback(const mach_header *mh) {
@@ -102,8 +120,7 @@ void addImageCallback(const mach_header *mh) {
102120

103121
// Callback for objc_addLoadImageFunc2 that takes a mach_header and dyld info.
104122
#if OBJC_ADDLOADIMAGEFUNC2_DEFINED
105-
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
106-
enum _dyld_section_location_kind SECTION_KIND,
123+
template <const char *SEGMENT_NAME, const char *SECTION_NAME, int SECTION_KIND,
107124
void CONSUME_BLOCK(const void *baseAddress, const void *start,
108125
uintptr_t size)>
109126
void addImageCallback(const mach_header *mh,
@@ -112,16 +129,17 @@ void addImageCallback(const mach_header *mh,
112129
assert(mh->magic == MH_MAGIC_64 && "loaded non-64-bit image?!");
113130
#endif
114131

115-
auto result = _dyld_lookup_section_info(mh, dyldInfo, SECTION_KIND);
132+
auto result = _dyld_lookup_section_info(
133+
mh, dyldInfo,
134+
static_cast<enum _dyld_section_location_kind>(SECTION_KIND));
116135
if (result.buffer)
117136
CONSUME_BLOCK(mh, result.buffer, result.bufferSize);
118137
}
119138
#endif
120139

121140
// Callback for _dyld_register_func_for_add_image that takes a mach_header and a
122141
// slide.
123-
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
124-
enum _dyld_section_location_kind SECTION_KIND,
142+
template <const char *SEGMENT_NAME, const char *SECTION_NAME, int SECTION_KIND,
125143
void CONSUME_BLOCK(const void *baseAddress, const void *start,
126144
uintptr_t size)>
127145
void addImageCallback(const mach_header *mh, intptr_t vmaddr_slide) {
@@ -132,11 +150,10 @@ void addImageCallback(const mach_header *mh, intptr_t vmaddr_slide) {
132150

133151
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
134152
const char *SEGMENT_NAME2, const char *SECTION_NAME2,
135-
enum _dyld_section_location_kind SECTION_KIND,
136-
enum _dyld_section_location_kind SECTION_KIND2,
137-
void CONSUME_BLOCK(const void *baseAddress,
138-
const void *start, uintptr_t size,
139-
const void *start2, uintptr_t size2)>
153+
int SECTION_KIND, int SECTION_KIND2,
154+
void CONSUME_BLOCK(const void *baseAddress, const void *start,
155+
uintptr_t size, const void *start2,
156+
uintptr_t size2)>
140157
void addImageCallback2Sections(const mach_header *mh) {
141158
#if __POINTER_WIDTH__ == 64
142159
assert(mh->magic == MH_MAGIC_64 && "loaded non-64-bit image?!");
@@ -168,23 +185,26 @@ void addImageCallback2Sections(const mach_header *mh) {
168185
#if OBJC_ADDLOADIMAGEFUNC2_DEFINED
169186
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
170187
const char *SEGMENT_NAME2, const char *SECTION_NAME2,
171-
enum _dyld_section_location_kind SECTION_KIND,
172-
enum _dyld_section_location_kind SECTION_KIND2,
173-
void CONSUME_BLOCK(const void *baseAddress,
174-
const void *start, uintptr_t size,
175-
const void *start2, uintptr_t size2)>
188+
int SECTION_KIND, int SECTION_KIND2,
189+
void CONSUME_BLOCK(const void *baseAddress, const void *start,
190+
uintptr_t size, const void *start2,
191+
uintptr_t size2)>
176192
void addImageCallback2Sections(const mach_header *mh,
177193
struct _dyld_section_location_info_s *dyldInfo) {
178194
#if __POINTER_WIDTH__ == 64
179195
assert(mh->magic == MH_MAGIC_64 && "loaded non-64-bit image?!");
180196
#endif
181197

182198
// Look for a section.
183-
auto result = _dyld_lookup_section_info(mh, dyldInfo, SECTION_KIND);
199+
auto result = _dyld_lookup_section_info(
200+
mh, dyldInfo,
201+
static_cast<enum _dyld_section_location_kind>(SECTION_KIND));
184202
if (!result.buffer)
185203
return;
186204

187-
auto result2 = _dyld_lookup_section_info(mh, dyldInfo, SECTION_KIND2);
205+
auto result2 = _dyld_lookup_section_info(
206+
mh, dyldInfo,
207+
static_cast<enum _dyld_section_location_kind>(SECTION_KIND2));
188208
// No NULL check here, we allow this one not to be present. dyld gives us
189209
// a NULL pointer and 0 size when the section isn't in the dylib so we don't
190210
// need to zero anything out.
@@ -198,8 +218,7 @@ void addImageCallback2Sections(const mach_header *mh,
198218
// slide.
199219
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
200220
const char *SEGMENT_NAME2, const char *SECTION_NAME2,
201-
enum _dyld_section_location_kind SECTION_KIND,
202-
enum _dyld_section_location_kind SECTION_KIND2,
221+
int SECTION_KIND, int SECTION_KIND2,
203222
void CONSUME_BLOCK(const void *baseAddress, const void *start,
204223
uintptr_t size, const void *start2,
205224
uintptr_t size2)>

0 commit comments

Comments
 (0)