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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
//===- lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp --------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file Converts from in-memory normalized mach-o to in-memory Atoms.
///
/// +------------+
/// | normalized |
/// +------------+
/// |
/// |
/// v
/// +-------+
/// | Atoms |
/// +-------+
#include "MachONormalizedFile.h"
#include "File.h"
#include "Atoms.h"
#include "lld/Core/Error.h"
#include "lld/Core/LLVM.h"
#include "llvm/Support/MachO.h"
using namespace llvm::MachO;
namespace lld {
namespace mach_o {
namespace normalized {
static uint64_t nextSymbolAddress(const NormalizedFile &normalizedFile,
const Symbol &symbol) {
uint64_t symbolAddr = symbol.value;
uint8_t symbolSectionIndex = symbol.sect;
const Section §ion = normalizedFile.sections[symbolSectionIndex - 1];
// If no symbol after this address, use end of section address.
uint64_t closestAddr = section.address + section.content.size();
for (const Symbol &s : normalizedFile.globalSymbols) {
if (s.sect != symbolSectionIndex)
continue;
uint64_t sValue = s.value;
if (sValue <= symbolAddr)
continue;
if (sValue < closestAddr)
closestAddr = s.value;
}
for (const Symbol &s : normalizedFile.localSymbols) {
if (s.sect != symbolSectionIndex)
continue;
uint64_t sValue = s.value;
if (sValue <= symbolAddr)
continue;
if (sValue < closestAddr)
closestAddr = s.value;
}
return closestAddr;
}
static Atom::Scope atomScope(uint8_t scope) {
switch (scope) {
case N_EXT:
return Atom::scopeGlobal;
case N_PEXT | N_EXT:
return Atom::scopeLinkageUnit;
case 0:
return Atom::scopeTranslationUnit;
}
llvm_unreachable("unknown scope value!");
}
static DefinedAtom::ContentType atomTypeFromSection(const Section §ion) {
// FIX ME
return DefinedAtom::typeCode;
}
static void processSymbol(const NormalizedFile &normalizedFile, MachOFile &file,
const Symbol &sym, bool copyRefs) {
// Mach-O symbol table does have size in it, so need to scan ahead
// to find symbol with next highest address.
const Section §ion = normalizedFile.sections[sym.sect - 1];
uint64_t offset = sym.value - section.address;
uint64_t size = nextSymbolAddress(normalizedFile, sym) - sym.value;
if (section.type == llvm::MachO::S_ZEROFILL) {
file.addZeroFillDefinedAtom(sym.name, atomScope(sym.scope), size, copyRefs);
}
else if ((section.type == llvm::MachO::S_CSTRING_LITERALS) &&
(sym.name[0] == 'L')) {
// Ignore L labels on cstrings.
} else {
ArrayRef<uint8_t> atomContent = section.content.slice(offset, size);
DefinedAtom::Merge m = DefinedAtom::mergeNo;
if (sym.desc & N_WEAK_DEF)
m = DefinedAtom::mergeAsWeak;
file.addDefinedAtom(sym.name, atomScope(sym.scope),
atomTypeFromSection(section), m, atomContent, copyRefs);
}
}
static void processUndefindeSymbol(MachOFile &file, const Symbol &sym,
bool copyRefs) {
// Undefinded symbols with n_value!=0 are actually tentative definitions.
if (sym.value == Hex64(0)) {
file.addUndefinedAtom(sym.name, copyRefs);
} else {
file.addTentativeDefAtom(sym.name, atomScope(sym.scope), sym.value,
DefinedAtom::Alignment(sym.desc >> 8), copyRefs);
}
}
// A __TEXT/__ustring section contains UTF16 strings. Atom boundaries are
// determined by finding the terminating 0x0000 in each string.
static error_code processUTF16Section(MachOFile &file, const Section §ion,
bool is64, bool copyRefs) {
if ((section.content.size() % 4) != 0)
return make_dynamic_error_code(Twine("Section ") + section.segmentName
+ "/" + section.sectionName
+ " has a size that is not even");
unsigned offset = 0;
for (size_t i = 0, e = section.content.size(); i != e; i +=2) {
if ((section.content[i] == 0) && (section.content[i+1] == 0)) {
unsigned size = i - offset + 2;
ArrayRef<uint8_t> utf16Content = section.content.slice(offset, size);
file.addDefinedAtom(StringRef(), DefinedAtom::scopeLinkageUnit,
DefinedAtom::typeUTF16String,
DefinedAtom::mergeByContent, utf16Content,
copyRefs);
offset = i + 2;
}
}
if (offset != section.content.size()) {
return make_dynamic_error_code(Twine("Section ") + section.segmentName
+ "/" + section.sectionName
+ " is supposed to contain 0x0000 "
"terminated UTF16 strings, but the "
"last string in the section is not zero "
"terminated.");
}
return error_code::success();
}
// A __DATA/__cfstring section contain NS/CFString objects. Atom boundaries
// are determined because each object is known to be 4 pointers in size.
static error_code processCFStringSection(MachOFile &file,const Section §ion,
bool is64, bool copyRefs) {
const uint32_t cfsObjSize = (is64 ? 32 : 16);
if ((section.content.size() % cfsObjSize) != 0) {
return make_dynamic_error_code(Twine("Section __DATA/__cfstring has a size "
"(" + Twine(section.content.size())
+ ") that is not a multiple of "
+ Twine(cfsObjSize)));
}
unsigned offset = 0;
for (size_t i = 0, e = section.content.size(); i != e; i += cfsObjSize) {
ArrayRef<uint8_t> byteContent = section.content.slice(offset, cfsObjSize);
file.addDefinedAtom(StringRef(), DefinedAtom::scopeLinkageUnit,
DefinedAtom::typeCFString,
DefinedAtom::mergeByContent, byteContent, copyRefs);
offset += cfsObjSize;
}
return error_code::success();
}
static error_code processSection(MachOFile &file, const Section §ion,
bool is64, bool copyRefs) {
unsigned offset = 0;
const unsigned pointerSize = (is64 ? 8 : 4);
switch (section.type) {
case llvm::MachO::S_REGULAR:
if (section.segmentName.equals("__TEXT") &&
section.sectionName.equals("__ustring")) {
return processUTF16Section(file, section, is64, copyRefs);
}
else if (section.segmentName.equals("__DATA") &&
section.sectionName.equals("__cfstring")) {
return processCFStringSection(file, section, is64, copyRefs);
}
break;
case llvm::MachO::S_COALESCED:
case llvm::MachO::S_ZEROFILL:
// These sections are broken into atoms based on symbols.
break;
case S_MOD_INIT_FUNC_POINTERS:
if ((section.content.size() % pointerSize) != 0) {
return make_dynamic_error_code(Twine("Section ") + section.segmentName
+ "/" + section.sectionName
+ " has type S_MOD_INIT_FUNC_POINTERS but "
"its size ("
+ Twine(section.content.size())
+ ") is not a multiple of "
+ Twine(pointerSize));
}
for (size_t i = 0, e = section.content.size(); i != e; i += pointerSize) {
ArrayRef<uint8_t> bytes = section.content.slice(offset, pointerSize);
file.addDefinedAtom(StringRef(), DefinedAtom::scopeTranslationUnit,
DefinedAtom::typeInitializerPtr, DefinedAtom::mergeNo,
bytes, copyRefs);
offset += pointerSize;
}
break;
case S_MOD_TERM_FUNC_POINTERS:
if ((section.content.size() % pointerSize) != 0) {
return make_dynamic_error_code(Twine("Section ") + section.segmentName
+ "/" + section.sectionName
+ " has type S_MOD_TERM_FUNC_POINTERS but "
"its size ("
+ Twine(section.content.size())
+ ") is not a multiple of "
+ Twine(pointerSize));
}
for (size_t i = 0, e = section.content.size(); i != e; i += pointerSize) {
ArrayRef<uint8_t> bytes = section.content.slice(offset, pointerSize);
file.addDefinedAtom(StringRef(), DefinedAtom::scopeTranslationUnit,
DefinedAtom::typeTerminatorPtr, DefinedAtom::mergeNo,
bytes, copyRefs);
offset += pointerSize;
}
break;
case S_NON_LAZY_SYMBOL_POINTERS:
if ((section.content.size() % pointerSize) != 0) {
return make_dynamic_error_code(Twine("Section ") + section.segmentName
+ "/" + section.sectionName
+ " has type S_NON_LAZY_SYMBOL_POINTERS "
"but its size ("
+ Twine(section.content.size())
+ ") is not a multiple of "
+ Twine(pointerSize));
}
for (size_t i = 0, e = section.content.size(); i != e; i += pointerSize) {
ArrayRef<uint8_t> bytes = section.content.slice(offset, pointerSize);
file.addDefinedAtom(StringRef(), DefinedAtom::scopeLinkageUnit,
DefinedAtom::typeGOT, DefinedAtom::mergeByContent,
bytes, copyRefs);
offset += pointerSize;
}
break;
case llvm::MachO::S_CSTRING_LITERALS:
for (size_t i = 0, e = section.content.size(); i != e; ++i) {
if (section.content[i] == 0) {
unsigned size = i - offset + 1;
ArrayRef<uint8_t> strContent = section.content.slice(offset, size);
file.addDefinedAtom(StringRef(), DefinedAtom::scopeLinkageUnit,
DefinedAtom::typeCString,
DefinedAtom::mergeByContent, strContent, copyRefs);
offset = i + 1;
}
}
if (offset != section.content.size()) {
return make_dynamic_error_code(Twine("Section ") + section.segmentName
+ "/" + section.sectionName
+ " has type S_CSTRING_LITERALS but the "
"last string in the section is not zero "
"terminated.");
}
break;
case llvm::MachO::S_4BYTE_LITERALS:
if ((section.content.size() % 4) != 0)
return make_dynamic_error_code(Twine("Section ") + section.segmentName
+ "/" + section.sectionName
+ " has type S_4BYTE_LITERALS but its "
"size (" + Twine(section.content.size())
+ ") is not a multiple of 4");
for (size_t i = 0, e = section.content.size(); i != e; i += 4) {
ArrayRef<uint8_t> byteContent = section.content.slice(offset, 4);
file.addDefinedAtom(StringRef(), DefinedAtom::scopeLinkageUnit,
DefinedAtom::typeLiteral4,
DefinedAtom::mergeByContent, byteContent, copyRefs);
offset += 4;
}
break;
case llvm::MachO::S_8BYTE_LITERALS:
if ((section.content.size() % 8) != 0)
return make_dynamic_error_code(Twine("Section ") + section.segmentName
+ "/" + section.sectionName
+ " has type S_8YTE_LITERALS but its "
"size (" + Twine(section.content.size())
+ ") is not a multiple of 8");
for (size_t i = 0, e = section.content.size(); i != e; i += 8) {
ArrayRef<uint8_t> byteContent = section.content.slice(offset, 8);
file.addDefinedAtom(StringRef(), DefinedAtom::scopeLinkageUnit,
DefinedAtom::typeLiteral8,
DefinedAtom::mergeByContent, byteContent, copyRefs);
offset += 8;
}
break;
case llvm::MachO::S_16BYTE_LITERALS:
if ((section.content.size() % 16) != 0)
return make_dynamic_error_code(Twine("Section ") + section.segmentName
+ "/" + section.sectionName
+ " has type S_16BYTE_LITERALS but its "
"size (" + Twine(section.content.size())
+ ") is not a multiple of 16");
for (size_t i = 0, e = section.content.size(); i != e; i += 16) {
ArrayRef<uint8_t> byteContent = section.content.slice(offset, 16);
file.addDefinedAtom(StringRef(), DefinedAtom::scopeLinkageUnit,
DefinedAtom::typeLiteral16,
DefinedAtom::mergeByContent, byteContent, copyRefs);
offset += 16;
}
break;
default:
llvm_unreachable("mach-o section type not supported yet");
break;
}
return error_code::success();
}
static ErrorOr<std::unique_ptr<lld::File>>
normalizedObjectToAtoms(const NormalizedFile &normalizedFile, StringRef path,
bool copyRefs) {
std::unique_ptr<MachOFile> file(new MachOFile(path));
// Create atoms from global symbols.
for (const Symbol &sym : normalizedFile.globalSymbols) {
processSymbol(normalizedFile, *file, sym, copyRefs);
}
// Create atoms from local symbols.
for (const Symbol &sym : normalizedFile.localSymbols) {
processSymbol(normalizedFile, *file, sym, copyRefs);
}
// Create atoms from undefinded symbols.
for (auto &sym : normalizedFile.undefinedSymbols) {
processUndefindeSymbol(*file, sym, copyRefs);
}
// Create atoms from sections that don't have symbols.
bool is64 = MachOLinkingContext::is64Bit(normalizedFile.arch);
for (auto § : normalizedFile.sections) {
if (error_code ec = processSection(*file, sect, is64, copyRefs))
return ec;
}
return std::unique_ptr<File>(std::move(file));
}
ErrorOr<std::unique_ptr<lld::File>>
normalizedToAtoms(const NormalizedFile &normalizedFile, StringRef path,
bool copyRefs) {
switch (normalizedFile.fileType) {
case MH_OBJECT:
return normalizedObjectToAtoms(normalizedFile, path, copyRefs);
default:
llvm_unreachable("unhandled MachO file type!");
}
}
} // namespace normalized
} // namespace mach_o
} // namespace lld
|