Skip to content

Commit c9ccb67

Browse files
committed
[Runtime] Avoid referring to enum _dyld_section_location_kind.
If the SDK has dyld_priv.h but doesn't have enum _dyld_section_location_kind, we can't use enum _dyld_section_location_kind and it's hard to detect that. Instead, just use `int`, and use #defines for the values. (cherry picked from commit f7e3256)
1 parent ba7628c commit c9ccb67

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

stdlib/public/runtime/ImageInspectionMachO.cpp

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ enum _dyld_section_location_kind {
4949

5050
#endif
5151

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+
5261
#if !OBJC_ADDLOADIMAGEFUNC2_DEFINED
5362
// If we don't have objc_addLoadImageFunc2, fall back to objc_addLoadImageFunc.
5463
// Use a #define so we don't have to conditionalize the calling code below.
@@ -79,8 +88,7 @@ using mach_header_platform = mach_header;
7988
#endif
8089

8190
// 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,
91+
template <const char *SEGMENT_NAME, const char *SECTION_NAME, int SECTION_KIND,
8492
void CONSUME_BLOCK(const void *baseAddress, const void *start,
8593
uintptr_t size)>
8694
void addImageCallback(const mach_header *mh) {
@@ -102,8 +110,7 @@ void addImageCallback(const mach_header *mh) {
102110

103111
// Callback for objc_addLoadImageFunc2 that takes a mach_header and dyld info.
104112
#if OBJC_ADDLOADIMAGEFUNC2_DEFINED
105-
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
106-
enum _dyld_section_location_kind SECTION_KIND,
113+
template <const char *SEGMENT_NAME, const char *SECTION_NAME, int SECTION_KIND,
107114
void CONSUME_BLOCK(const void *baseAddress, const void *start,
108115
uintptr_t size)>
109116
void addImageCallback(const mach_header *mh,
@@ -112,16 +119,17 @@ void addImageCallback(const mach_header *mh,
112119
assert(mh->magic == MH_MAGIC_64 && "loaded non-64-bit image?!");
113120
#endif
114121

115-
auto result = _dyld_lookup_section_info(mh, dyldInfo, SECTION_KIND);
122+
auto result = _dyld_lookup_section_info(
123+
mh, dyldInfo,
124+
static_cast<enum _dyld_section_location_kind>(SECTION_KIND));
116125
if (result.buffer)
117126
CONSUME_BLOCK(mh, result.buffer, result.bufferSize);
118127
}
119128
#endif
120129

121130
// Callback for _dyld_register_func_for_add_image that takes a mach_header and a
122131
// slide.
123-
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
124-
enum _dyld_section_location_kind SECTION_KIND,
132+
template <const char *SEGMENT_NAME, const char *SECTION_NAME, int SECTION_KIND,
125133
void CONSUME_BLOCK(const void *baseAddress, const void *start,
126134
uintptr_t size)>
127135
void addImageCallback(const mach_header *mh, intptr_t vmaddr_slide) {
@@ -132,11 +140,10 @@ void addImageCallback(const mach_header *mh, intptr_t vmaddr_slide) {
132140

133141
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
134142
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)>
143+
int SECTION_KIND, int SECTION_KIND2,
144+
void CONSUME_BLOCK(const void *baseAddress, const void *start,
145+
uintptr_t size, const void *start2,
146+
uintptr_t size2)>
140147
void addImageCallback2Sections(const mach_header *mh) {
141148
#if __POINTER_WIDTH__ == 64
142149
assert(mh->magic == MH_MAGIC_64 && "loaded non-64-bit image?!");
@@ -168,23 +175,26 @@ void addImageCallback2Sections(const mach_header *mh) {
168175
#if OBJC_ADDLOADIMAGEFUNC2_DEFINED
169176
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
170177
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)>
178+
int SECTION_KIND, int SECTION_KIND2,
179+
void CONSUME_BLOCK(const void *baseAddress, const void *start,
180+
uintptr_t size, const void *start2,
181+
uintptr_t size2)>
176182
void addImageCallback2Sections(const mach_header *mh,
177183
struct _dyld_section_location_info_s *dyldInfo) {
178184
#if __POINTER_WIDTH__ == 64
179185
assert(mh->magic == MH_MAGIC_64 && "loaded non-64-bit image?!");
180186
#endif
181187

182188
// Look for a section.
183-
auto result = _dyld_lookup_section_info(mh, dyldInfo, SECTION_KIND);
189+
auto result = _dyld_lookup_section_info(
190+
mh, dyldInfo,
191+
static_cast<enum _dyld_section_location_kind>(SECTION_KIND));
184192
if (!result.buffer)
185193
return;
186194

187-
auto result2 = _dyld_lookup_section_info(mh, dyldInfo, SECTION_KIND2);
195+
auto result2 = _dyld_lookup_section_info(
196+
mh, dyldInfo,
197+
static_cast<enum _dyld_section_location_kind>(SECTION_KIND2));
188198
// No NULL check here, we allow this one not to be present. dyld gives us
189199
// a NULL pointer and 0 size when the section isn't in the dylib so we don't
190200
// need to zero anything out.
@@ -198,8 +208,7 @@ void addImageCallback2Sections(const mach_header *mh,
198208
// slide.
199209
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
200210
const char *SEGMENT_NAME2, const char *SECTION_NAME2,
201-
enum _dyld_section_location_kind SECTION_KIND,
202-
enum _dyld_section_location_kind SECTION_KIND2,
211+
int SECTION_KIND, int SECTION_KIND2,
203212
void CONSUME_BLOCK(const void *baseAddress, const void *start,
204213
uintptr_t size, const void *start2,
205214
uintptr_t size2)>

0 commit comments

Comments
 (0)