Skip to content

Commit 66e2416

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.
1 parent 1a3cbfa commit 66e2416

File tree

1 file changed

+30
-35
lines changed

1 file changed

+30
-35
lines changed

stdlib/public/runtime/ImageInspectionMachO.cpp

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,17 @@ struct _dyld_section_info_result
4343
_dyld_lookup_section_info(const struct mach_header *mh,
4444
_dyld_section_location_info_t locationHandle,
4545
enum _dyld_section_location_kind kind);
46-
#else
47-
48-
// Bring our own definition of enum _dyld_section_location_kind and some of its
49-
// values. They'll be unused when we can't include dyld_priv.h, but the code
50-
// needs them in order to compile.
51-
enum _dyld_section_location_kind {
52-
_dyld_section_location_text_swift5_protos,
53-
_dyld_section_location_text_swift5_proto,
54-
_dyld_section_location_text_swift5_types,
55-
_dyld_section_location_text_swift5_replace,
56-
_dyld_section_location_text_swift5_replace2,
57-
_dyld_section_location_text_swift5_ac_funcs,
58-
};
59-
6046
#endif
6147

48+
// Bring our own definition of _dyld_section_location constants in case we're
49+
// using an older SDK that doesn't have them.
50+
#define _dyld_section_location_text_swift5_protos 0
51+
#define _dyld_section_location_text_swift5_proto 1
52+
#define _dyld_section_location_text_swift5_types 2
53+
#define _dyld_section_location_text_swift5_replace 3
54+
#define _dyld_section_location_text_swift5_replace2 4
55+
#define _dyld_section_location_text_swift5_ac_funcs 5
56+
6257
#if !OBJC_ADDLOADIMAGEFUNC2_DEFINED
6358
// If we don't have objc_addLoadImageFunc2, fall back to objc_addLoadImageFunc.
6459
// Use a #define so we don't have to conditionalize the calling code below.
@@ -89,8 +84,7 @@ using mach_header_platform = mach_header;
8984
#endif
9085

9186
// Callback for objc_addLoadImageFunc that just takes a mach_header.
92-
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
93-
enum _dyld_section_location_kind SECTION_KIND,
87+
template <const char *SEGMENT_NAME, const char *SECTION_NAME, int SECTION_KIND,
9488
void CONSUME_BLOCK(const void *baseAddress, const void *start,
9589
uintptr_t size)>
9690
void addImageCallback(const mach_header *mh) {
@@ -112,8 +106,7 @@ void addImageCallback(const mach_header *mh) {
112106

113107
// Callback for objc_addLoadImageFunc2 that takes a mach_header and dyld info.
114108
#if OBJC_ADDLOADIMAGEFUNC2_DEFINED
115-
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
116-
enum _dyld_section_location_kind SECTION_KIND,
109+
template <const char *SEGMENT_NAME, const char *SECTION_NAME, int SECTION_KIND,
117110
void CONSUME_BLOCK(const void *baseAddress, const void *start,
118111
uintptr_t size)>
119112
void addImageCallback(const mach_header *mh,
@@ -122,16 +115,17 @@ void addImageCallback(const mach_header *mh,
122115
assert(mh->magic == MH_MAGIC_64 && "loaded non-64-bit image?!");
123116
#endif
124117

125-
auto result = _dyld_lookup_section_info(mh, dyldInfo, SECTION_KIND);
118+
auto result = _dyld_lookup_section_info(
119+
mh, dyldInfo,
120+
static_cast<enum _dyld_section_location_kind>(SECTION_KIND));
126121
if (result.buffer)
127122
CONSUME_BLOCK(mh, result.buffer, result.bufferSize);
128123
}
129124
#endif
130125

131126
// Callback for _dyld_register_func_for_add_image that takes a mach_header and a
132127
// slide.
133-
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
134-
enum _dyld_section_location_kind SECTION_KIND,
128+
template <const char *SEGMENT_NAME, const char *SECTION_NAME, int SECTION_KIND,
135129
void CONSUME_BLOCK(const void *baseAddress, const void *start,
136130
uintptr_t size)>
137131
void addImageCallback(const mach_header *mh, intptr_t vmaddr_slide) {
@@ -142,11 +136,10 @@ void addImageCallback(const mach_header *mh, intptr_t vmaddr_slide) {
142136

143137
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
144138
const char *SEGMENT_NAME2, const char *SECTION_NAME2,
145-
enum _dyld_section_location_kind SECTION_KIND,
146-
enum _dyld_section_location_kind SECTION_KIND2,
147-
void CONSUME_BLOCK(const void *baseAddress,
148-
const void *start, uintptr_t size,
149-
const void *start2, uintptr_t size2)>
139+
int SECTION_KIND, int SECTION_KIND2,
140+
void CONSUME_BLOCK(const void *baseAddress, const void *start,
141+
uintptr_t size, const void *start2,
142+
uintptr_t size2)>
150143
void addImageCallback2Sections(const mach_header *mh) {
151144
#if __POINTER_WIDTH__ == 64
152145
assert(mh->magic == MH_MAGIC_64 && "loaded non-64-bit image?!");
@@ -178,23 +171,26 @@ void addImageCallback2Sections(const mach_header *mh) {
178171
#if OBJC_ADDLOADIMAGEFUNC2_DEFINED
179172
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
180173
const char *SEGMENT_NAME2, const char *SECTION_NAME2,
181-
enum _dyld_section_location_kind SECTION_KIND,
182-
enum _dyld_section_location_kind SECTION_KIND2,
183-
void CONSUME_BLOCK(const void *baseAddress,
184-
const void *start, uintptr_t size,
185-
const void *start2, uintptr_t size2)>
174+
int SECTION_KIND, int SECTION_KIND2,
175+
void CONSUME_BLOCK(const void *baseAddress, const void *start,
176+
uintptr_t size, const void *start2,
177+
uintptr_t size2)>
186178
void addImageCallback2Sections(const mach_header *mh,
187179
struct _dyld_section_location_info_s *dyldInfo) {
188180
#if __POINTER_WIDTH__ == 64
189181
assert(mh->magic == MH_MAGIC_64 && "loaded non-64-bit image?!");
190182
#endif
191183

192184
// Look for a section.
193-
auto result = _dyld_lookup_section_info(mh, dyldInfo, SECTION_KIND);
185+
auto result = _dyld_lookup_section_info(
186+
mh, dyldInfo,
187+
static_cast<enum _dyld_section_location_kind>(SECTION_KIND));
194188
if (!result.buffer)
195189
return;
196190

197-
auto result2 = _dyld_lookup_section_info(mh, dyldInfo, SECTION_KIND2);
191+
auto result2 = _dyld_lookup_section_info(
192+
mh, dyldInfo,
193+
static_cast<enum _dyld_section_location_kind>(SECTION_KIND2));
198194
// No NULL check here, we allow this one not to be present. dyld gives us
199195
// a NULL pointer and 0 size when the section isn't in the dylib so we don't
200196
// need to zero anything out.
@@ -208,8 +204,7 @@ void addImageCallback2Sections(const mach_header *mh,
208204
// slide.
209205
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
210206
const char *SEGMENT_NAME2, const char *SECTION_NAME2,
211-
enum _dyld_section_location_kind SECTION_KIND,
212-
enum _dyld_section_location_kind SECTION_KIND2,
207+
int SECTION_KIND, int SECTION_KIND2,
213208
void CONSUME_BLOCK(const void *baseAddress, const void *start,
214209
uintptr_t size, const void *start2,
215210
uintptr_t size2)>

0 commit comments

Comments
 (0)