Skip to content

Commit e4e5af3

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 68378b8 commit e4e5af3

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
@@ -33,22 +33,17 @@
3333
#if __has_include(<objc/objc-internal.h>) && __has_include(<mach-o/dyld_priv.h>)
3434
#include <mach-o/dyld_priv.h>
3535
#include <objc/objc-internal.h>
36-
#else
37-
38-
// Bring our own definition of enum _dyld_section_location_kind and some of its
39-
// values. They'll be unused when we can't include dyld_priv.h, but the code
40-
// needs them in order to compile.
41-
enum _dyld_section_location_kind {
42-
_dyld_section_location_text_swift5_protos,
43-
_dyld_section_location_text_swift5_proto,
44-
_dyld_section_location_text_swift5_types,
45-
_dyld_section_location_text_swift5_replace,
46-
_dyld_section_location_text_swift5_replace2,
47-
_dyld_section_location_text_swift5_ac_funcs,
48-
};
49-
5036
#endif
5137

38+
// Bring our own definition of _dyld_section_location constants in case we're
39+
// using an older SDK that doesn't have them.
40+
#define _dyld_section_location_text_swift5_protos 0
41+
#define _dyld_section_location_text_swift5_proto 1
42+
#define _dyld_section_location_text_swift5_types 2
43+
#define _dyld_section_location_text_swift5_replace 3
44+
#define _dyld_section_location_text_swift5_replace2 4
45+
#define _dyld_section_location_text_swift5_ac_funcs 5
46+
5247
#if !OBJC_ADDLOADIMAGEFUNC2_DEFINED
5348
// If we don't have objc_addLoadImageFunc2, fall back to objc_addLoadImageFunc.
5449
// Use a #define so we don't have to conditionalize the calling code below.
@@ -79,8 +74,7 @@ using mach_header_platform = mach_header;
7974
#endif
8075

8176
// 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,
77+
template <const char *SEGMENT_NAME, const char *SECTION_NAME, int SECTION_KIND,
8478
void CONSUME_BLOCK(const void *baseAddress, const void *start,
8579
uintptr_t size)>
8680
void addImageCallback(const mach_header *mh) {
@@ -102,8 +96,7 @@ void addImageCallback(const mach_header *mh) {
10296

10397
// Callback for objc_addLoadImageFunc2 that takes a mach_header and dyld info.
10498
#if OBJC_ADDLOADIMAGEFUNC2_DEFINED
105-
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
106-
enum _dyld_section_location_kind SECTION_KIND,
99+
template <const char *SEGMENT_NAME, const char *SECTION_NAME, int SECTION_KIND,
107100
void CONSUME_BLOCK(const void *baseAddress, const void *start,
108101
uintptr_t size)>
109102
void addImageCallback(const mach_header *mh,
@@ -112,16 +105,17 @@ void addImageCallback(const mach_header *mh,
112105
assert(mh->magic == MH_MAGIC_64 && "loaded non-64-bit image?!");
113106
#endif
114107

115-
auto result = _dyld_lookup_section_info(mh, dyldInfo, SECTION_KIND);
108+
auto result = _dyld_lookup_section_info(
109+
mh, dyldInfo,
110+
static_cast<enum _dyld_section_location_kind>(SECTION_KIND));
116111
if (result.buffer)
117112
CONSUME_BLOCK(mh, result.buffer, result.bufferSize);
118113
}
119114
#endif
120115

121116
// Callback for _dyld_register_func_for_add_image that takes a mach_header and a
122117
// slide.
123-
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
124-
enum _dyld_section_location_kind SECTION_KIND,
118+
template <const char *SEGMENT_NAME, const char *SECTION_NAME, int SECTION_KIND,
125119
void CONSUME_BLOCK(const void *baseAddress, const void *start,
126120
uintptr_t size)>
127121
void addImageCallback(const mach_header *mh, intptr_t vmaddr_slide) {
@@ -132,11 +126,10 @@ void addImageCallback(const mach_header *mh, intptr_t vmaddr_slide) {
132126

133127
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
134128
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)>
129+
int SECTION_KIND, int SECTION_KIND2,
130+
void CONSUME_BLOCK(const void *baseAddress, const void *start,
131+
uintptr_t size, const void *start2,
132+
uintptr_t size2)>
140133
void addImageCallback2Sections(const mach_header *mh) {
141134
#if __POINTER_WIDTH__ == 64
142135
assert(mh->magic == MH_MAGIC_64 && "loaded non-64-bit image?!");
@@ -168,23 +161,26 @@ void addImageCallback2Sections(const mach_header *mh) {
168161
#if OBJC_ADDLOADIMAGEFUNC2_DEFINED
169162
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
170163
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)>
164+
int SECTION_KIND, int SECTION_KIND2,
165+
void CONSUME_BLOCK(const void *baseAddress, const void *start,
166+
uintptr_t size, const void *start2,
167+
uintptr_t size2)>
176168
void addImageCallback2Sections(const mach_header *mh,
177169
struct _dyld_section_location_info_s *dyldInfo) {
178170
#if __POINTER_WIDTH__ == 64
179171
assert(mh->magic == MH_MAGIC_64 && "loaded non-64-bit image?!");
180172
#endif
181173

182174
// Look for a section.
183-
auto result = _dyld_lookup_section_info(mh, dyldInfo, SECTION_KIND);
175+
auto result = _dyld_lookup_section_info(
176+
mh, dyldInfo,
177+
static_cast<enum _dyld_section_location_kind>(SECTION_KIND));
184178
if (!result.buffer)
185179
return;
186180

187-
auto result2 = _dyld_lookup_section_info(mh, dyldInfo, SECTION_KIND2);
181+
auto result2 = _dyld_lookup_section_info(
182+
mh, dyldInfo,
183+
static_cast<enum _dyld_section_location_kind>(SECTION_KIND2));
188184
// No NULL check here, we allow this one not to be present. dyld gives us
189185
// a NULL pointer and 0 size when the section isn't in the dylib so we don't
190186
// need to zero anything out.
@@ -198,8 +194,7 @@ void addImageCallback2Sections(const mach_header *mh,
198194
// slide.
199195
template <const char *SEGMENT_NAME, const char *SECTION_NAME,
200196
const char *SEGMENT_NAME2, const char *SECTION_NAME2,
201-
enum _dyld_section_location_kind SECTION_KIND,
202-
enum _dyld_section_location_kind SECTION_KIND2,
197+
int SECTION_KIND, int SECTION_KIND2,
203198
void CONSUME_BLOCK(const void *baseAddress, const void *start,
204199
uintptr_t size, const void *start2,
205200
uintptr_t size2)>

0 commit comments

Comments
 (0)