-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersion.h
More file actions
232 lines (197 loc) · 7.81 KB
/
Version.h
File metadata and controls
232 lines (197 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) 2026 Nikita Hmelnitkii. MIT License - see LICENSE.
//
// Version.h - linker family and version detection.
// Scans dyld images for the "PROJECT:ld-NNNN.N.N" marker embedded in
// every Apple linker binary.
#ifndef LD_VERSION_H
#define LD_VERSION_H
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <mach-o/dyld.h>
#include <mach-o/loader.h>
namespace ld {
enum class Family : uint8_t {
Unknown,
Prime, // ld-prime (ld-1100+)
Classic, // ld64
};
struct LinkerVersion {
Family family;
uint32_t major;
uint16_t minor;
uint16_t patch;
bool isPrime() const { return family == Family::Prime; }
bool isClassic() const { return family == Family::Classic; }
bool operator==(const LinkerVersion &o) const {
return family == o.family && major == o.major
&& minor == o.minor && patch == o.patch;
}
bool operator!=(const LinkerVersion &o) const { return !(*this == o); }
bool operator<(const LinkerVersion &o) const {
if (family != o.family) return family < o.family;
if (major != o.major) return major < o.major;
if (minor != o.minor) return minor < o.minor;
return patch < o.patch;
}
};
namespace version {
inline constexpr LinkerVersion Prime_1053 = {Family::Prime, 1053, 12, 0};
inline constexpr LinkerVersion Prime_1115 = {Family::Prime, 1115, 7, 3};
inline constexpr LinkerVersion Prime_1167 = {Family::Prime, 1167, 5, 0};
inline constexpr LinkerVersion Prime_1221 = {Family::Prime, 1221, 4, 0};
inline constexpr LinkerVersion Prime_1230 = {Family::Prime, 1230, 1, 0};
inline constexpr LinkerVersion Prime_1266 = {Family::Prime, 1266, 8, 0};
inline constexpr LinkerVersion ld64_820 = {Family::Classic, 820, 1, 0};
}
inline bool isKnownVersion(const LinkerVersion &v) {
return v == version::Prime_1053
|| v == version::Prime_1115
|| v == version::Prime_1167
|| v == version::Prime_1221
|| v == version::Prime_1230
|| v == version::Prime_1266
|| v == version::ld64_820;
}
// "ld-1221.4" / "ld64-820.1" / "unknown". Buffer must be >= 24.
inline const char *versionLabel(const LinkerVersion &v, char *buf, size_t bufLen) {
if (!buf || bufLen < 24) return "";
if (v.family == Family::Classic) {
snprintf(buf, bufLen, "ld64-%u.%u", v.major, v.minor);
} else if (v.family == Family::Prime) {
if (v.patch != 0)
snprintf(buf, bufLen, "ld-%u.%u.%u", v.major, v.minor, v.patch);
else
snprintf(buf, bufLen, "ld-%u.%u", v.major, v.minor);
} else {
snprintf(buf, bufLen, "unknown");
}
return buf;
}
inline bool isPrimeAtLeast(const LinkerVersion &v, uint32_t major) {
return v.isPrime() && v.major >= major;
}
inline LinkerVersion parseProjectString(const char *s) {
LinkerVersion v = {Family::Unknown, 0, 0, 0};
if (!s) return v;
const char *p = s;
if (p[0] == 'l' && p[1] == 'd') {
if (p[2] == '6' && p[3] == '4' && p[4] == '-') {
v.family = Family::Classic;
p += 5;
} else if (p[2] == '-') {
v.family = Family::Prime;
p += 3;
} else {
return v;
}
} else {
return v;
}
char *end = nullptr;
v.major = static_cast<uint32_t>(strtoul(p, &end, 10));
if (end == p || v.major == 0) return {Family::Unknown, 0, 0, 0};
p = end;
if (*p == '.') {
v.minor = static_cast<uint16_t>(strtoul(++p, &end, 10));
p = end;
}
if (*p == '.') {
v.patch = static_cast<uint16_t>(strtoul(++p, &end, 10));
}
return v;
}
inline LinkerVersion findProjectString(const uint8_t *start, const uint8_t *end) {
static const char needle[] = "PROJECT:ld";
constexpr size_t nlen = sizeof(needle) - 1;
for (const uint8_t *p = start; p + nlen <= end; ++p) {
if (memcmp(p, needle, nlen) != 0) continue;
const char *proj = reinterpret_cast<const char *>(p) + nlen;
const char *limit = reinterpret_cast<const char *>(end);
while (proj < limit && *proj == ' ') ++proj;
if (proj >= limit) continue;
size_t remaining = static_cast<size_t>(limit - proj);
char buf[64];
int n = snprintf(buf, sizeof(buf), "ld%.*s",
static_cast<int>(remaining < sizeof(buf) - 3
? remaining : sizeof(buf) - 3),
proj);
if (n <= 0) continue;
for (size_t k = 0; k < sizeof(buf) && buf[k] != '\0'; ++k) {
char c = buf[k];
if (c != '-' && c != '.' && c != '_'
&& !((c >= '0' && c <= '9')
|| (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z'))) {
buf[k] = '\0';
break;
}
}
LinkerVersion v = parseProjectString(buf);
if (v.family != Family::Unknown) return v;
}
return {Family::Unknown, 0, 0, 0};
}
inline LinkerVersion detectVersion() {
const uint32_t imageCount = _dyld_image_count();
auto checkImage = [](uint32_t i) -> LinkerVersion {
const char *name = _dyld_get_image_name(i);
if (!name) return {Family::Unknown, 0, 0, 0};
size_t len = strlen(name);
// Only scan images whose basename looks like a linker.
const char *slash = nullptr;
for (size_t k = 0; k < len; ++k) if (name[k] == '/') slash = name + k;
const char *base = slash ? slash + 1 : name;
bool isLinker =
(strcmp(base, "ld") == 0) ||
(strncmp(base, "ld-", 3) == 0) ||
(strncmp(base, "ld64", 4) == 0) ||
(strcmp(base, "ld.sh") == 0);
if (!isLinker) return {Family::Unknown, 0, 0, 0};
const auto *mh = reinterpret_cast<const struct mach_header_64 *>(
_dyld_get_image_header(i));
if (!mh || mh->magic != MH_MAGIC_64)
return {Family::Unknown, 0, 0, 0};
const uint8_t *cmds =
reinterpret_cast<const uint8_t *>(mh) + sizeof(struct mach_header_64);
intptr_t slide = _dyld_get_image_vmaddr_slide(i);
const struct load_command *lc =
reinterpret_cast<const struct load_command *>(cmds);
for (uint32_t j = 0; j < mh->ncmds; ++j) {
if (lc->cmdsize < sizeof(struct load_command)) break;
if (lc->cmd == LC_SEGMENT_64) {
const auto *seg =
reinterpret_cast<const struct segment_command_64 *>(lc);
// PROJECT string is embedded in __TEXT or __DATA.
if (strncmp(seg->segname, "__TEXT", 6) != 0
&& strncmp(seg->segname, "__DATA", 6) != 0) {
lc = reinterpret_cast<const struct load_command *>(
reinterpret_cast<const uint8_t *>(lc) + lc->cmdsize);
continue;
}
uint64_t searchSize = seg->filesize < seg->vmsize
? seg->filesize : seg->vmsize;
const uint8_t *start =
reinterpret_cast<const uint8_t *>(seg->vmaddr + slide);
LinkerVersion v = findProjectString(start, start + searchSize);
if (v.family != Family::Unknown) return v;
}
lc = reinterpret_cast<const struct load_command *>(
reinterpret_cast<const uint8_t *>(lc) + lc->cmdsize);
}
return {Family::Unknown, 0, 0, 0};
};
// Image 0 is the main executable; try it first.
if (imageCount > 0) {
LinkerVersion v = checkImage(0);
if (v.family != Family::Unknown) return v;
}
for (uint32_t i = 1; i < imageCount; ++i) {
LinkerVersion v = checkImage(i);
if (v.family != Family::Unknown) return v;
}
return {Family::Unknown, 0, 0, 0};
}
} // namespace ld
#endif