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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
//===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the MachOObjectFile class, which binds the MachOObject
// class to the generic ObjectFile wrapper.
//
//===----------------------------------------------------------------------===//
#include "llvm/Object/MachO.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Object/MachOFormat.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/DataExtractor.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/MemoryBuffer.h"
#include <cctype>
#include <cstring>
#include <limits>
using namespace llvm;
using namespace object;
namespace llvm {
namespace object {
MachOObjectFileBase::MachOObjectFileBase(MemoryBuffer *Object, bool Is64bits,
error_code &ec)
: ObjectFile(getMachOType(true, Is64bits), Object) {
}
bool MachOObjectFileBase::is64Bit() const {
return isa<MachOObjectFile64Le>(this);
}
const MachOObjectFileBase::LoadCommand *
MachOObjectFileBase::getLoadCommandInfo(unsigned Index) const {
uint64_t Offset;
uint64_t NewOffset = getHeaderSize();
const LoadCommand *Load;
unsigned I = 0;
do {
Offset = NewOffset;
StringRef Data = getData(Offset, sizeof(LoadCommand));
Load = reinterpret_cast<const LoadCommand*>(Data.data());
NewOffset = Offset + Load->Size;
++I;
} while (I != Index + 1);
return Load;
}
void MachOObjectFileBase::ReadULEB128s(uint64_t Index,
SmallVectorImpl<uint64_t> &Out) const {
DataExtractor extractor(ObjectFile::getData(), true, 0);
uint32_t offset = Index;
uint64_t data = 0;
while (uint64_t delta = extractor.getULEB128(&offset)) {
data += delta;
Out.push_back(data);
}
}
const MachOObjectFileBase::Header *MachOObjectFileBase::getHeader() const {
StringRef Data = getData(0, sizeof(Header));
return reinterpret_cast<const Header*>(Data.data());
}
unsigned MachOObjectFileBase::getHeaderSize() const {
return is64Bit() ? macho::Header64Size : macho::Header32Size;
}
StringRef MachOObjectFileBase::getData(size_t Offset, size_t Size) const {
return ObjectFile::getData().substr(Offset, Size);
}
ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
StringRef Magic = Buffer->getBuffer().slice(0, 4);
error_code ec;
bool Is64Bits = Magic == "\xFE\xED\xFA\xCF" || Magic == "\xCF\xFA\xED\xFE";
ObjectFile *Ret;
if (Is64Bits)
Ret = new MachOObjectFile64Le(Buffer, ec);
else
Ret = new MachOObjectFile32Le(Buffer, ec);
if (ec)
return NULL;
return Ret;
}
/*===-- Symbols -----------------------------------------------------------===*/
void MachOObjectFileBase::moveToNextSymbol(DataRefImpl &DRI) const {
uint32_t LoadCommandCount = getHeader()->NumLoadCommands;
while (DRI.d.a < LoadCommandCount) {
const LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
if (Command->Type == macho::LCT_Symtab) {
const SymtabLoadCommand *SymtabLoadCmd =
reinterpret_cast<const SymtabLoadCommand*>(Command);
if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
return;
}
DRI.d.a++;
DRI.d.b = 0;
}
}
const MachOObjectFileBase::SymbolTableEntryBase *
MachOObjectFileBase::getSymbolTableEntryBase(DataRefImpl DRI) const {
const LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
const SymtabLoadCommand *SymtabLoadCmd =
reinterpret_cast<const SymtabLoadCommand*>(Command);
return getSymbolTableEntryBase(DRI, SymtabLoadCmd);
}
const MachOObjectFileBase::SymbolTableEntryBase *
MachOObjectFileBase::getSymbolTableEntryBase(DataRefImpl DRI,
const SymtabLoadCommand *SymtabLoadCmd) const {
uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
unsigned Index = DRI.d.b;
unsigned SymbolTableEntrySize = is64Bit() ?
sizeof(MachOObjectFile64Le::SymbolTableEntry) :
sizeof(MachOObjectFile32Le::SymbolTableEntry);
uint64_t Offset = SymbolTableOffset + Index * SymbolTableEntrySize;
StringRef Data = getData(Offset, SymbolTableEntrySize);
return reinterpret_cast<const SymbolTableEntryBase*>(Data.data());
}
error_code MachOObjectFileBase::getSymbolNext(DataRefImpl DRI,
SymbolRef &Result) const {
DRI.d.b++;
moveToNextSymbol(DRI);
Result = SymbolRef(DRI, this);
return object_error::success;
}
error_code MachOObjectFileBase::getSymbolName(DataRefImpl DRI,
StringRef &Result) const {
const LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
const SymtabLoadCommand *SymtabLoadCmd =
reinterpret_cast<const SymtabLoadCommand*>(Command);
StringRef StringTable = getData(SymtabLoadCmd->StringTableOffset,
SymtabLoadCmd->StringTableSize);
const SymbolTableEntryBase *Entry =
getSymbolTableEntryBase(DRI, SymtabLoadCmd);
uint32_t StringIndex = Entry->StringIndex;
const char *Start = &StringTable.data()[StringIndex];
Result = StringRef(Start);
return object_error::success;
}
error_code MachOObjectFileBase::getSymbolNMTypeChar(DataRefImpl DRI,
char &Result) const {
const SymbolTableEntryBase *Entry = getSymbolTableEntryBase(DRI);
uint8_t Type = Entry->Type;
uint16_t Flags = Entry->Flags;
char Char;
switch (Type & macho::STF_TypeMask) {
case macho::STT_Undefined:
Char = 'u';
break;
case macho::STT_Absolute:
case macho::STT_Section:
Char = 's';
break;
default:
Char = '?';
break;
}
if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Char = toupper(static_cast<unsigned char>(Char));
Result = Char;
return object_error::success;
}
error_code MachOObjectFileBase::getSymbolFlags(DataRefImpl DRI,
uint32_t &Result) const {
const SymbolTableEntryBase *Entry = getSymbolTableEntryBase(DRI);
uint8_t MachOType = Entry->Type;
uint16_t MachOFlags = Entry->Flags;
// TODO: Correctly set SF_ThreadLocal
Result = SymbolRef::SF_None;
if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
Result |= SymbolRef::SF_Undefined;
if (MachOFlags & macho::STF_StabsEntryMask)
Result |= SymbolRef::SF_FormatSpecific;
if (MachOType & MachO::NlistMaskExternal) {
Result |= SymbolRef::SF_Global;
if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
Result |= SymbolRef::SF_Common;
}
if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
Result |= SymbolRef::SF_Weak;
if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
Result |= SymbolRef::SF_Absolute;
return object_error::success;
}
error_code MachOObjectFileBase::getSymbolSection(DataRefImpl Symb,
section_iterator &Res) const {
const SymbolTableEntryBase *Entry = getSymbolTableEntryBase(Symb);
uint8_t index = Entry->SectionIndex;
if (index == 0)
Res = end_sections();
else
Res = section_iterator(SectionRef(Sections[index-1], this));
return object_error::success;
}
error_code MachOObjectFileBase::getSymbolType(DataRefImpl Symb,
SymbolRef::Type &Res) const {
const SymbolTableEntryBase *Entry = getSymbolTableEntryBase(Symb);
uint8_t n_type = Entry->Type;
Res = SymbolRef::ST_Other;
// If this is a STAB debugging symbol, we can do nothing more.
if (n_type & MachO::NlistMaskStab) {
Res = SymbolRef::ST_Debug;
return object_error::success;
}
switch (n_type & MachO::NlistMaskType) {
case MachO::NListTypeUndefined :
Res = SymbolRef::ST_Unknown;
break;
case MachO::NListTypeSection :
Res = SymbolRef::ST_Function;
break;
}
return object_error::success;
}
error_code MachOObjectFileBase::getSymbolValue(DataRefImpl Symb,
uint64_t &Val) const {
report_fatal_error("getSymbolValue unimplemented in MachOObjectFileBase");
}
symbol_iterator MachOObjectFileBase::begin_symbols() const {
// DRI.d.a = segment number; DRI.d.b = symbol index.
DataRefImpl DRI;
moveToNextSymbol(DRI);
return symbol_iterator(SymbolRef(DRI, this));
}
symbol_iterator MachOObjectFileBase::end_symbols() const {
DataRefImpl DRI;
DRI.d.a = getHeader()->NumLoadCommands;
return symbol_iterator(SymbolRef(DRI, this));
}
symbol_iterator MachOObjectFileBase::begin_dynamic_symbols() const {
// TODO: implement
report_fatal_error("Dynamic symbols unimplemented in MachOObjectFileBase");
}
symbol_iterator MachOObjectFileBase::end_dynamic_symbols() const {
// TODO: implement
report_fatal_error("Dynamic symbols unimplemented in MachOObjectFileBase");
}
library_iterator MachOObjectFileBase::begin_libraries_needed() const {
// TODO: implement
report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase");
}
library_iterator MachOObjectFileBase::end_libraries_needed() const {
// TODO: implement
report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase");
}
StringRef MachOObjectFileBase::getLoadName() const {
// TODO: Implement
report_fatal_error("get_load_name() unimplemented in MachOObjectFileBase");
}
/*===-- Sections ----------------------------------------------------------===*/
std::size_t MachOObjectFileBase::getSectionIndex(DataRefImpl Sec) const {
SectionList::const_iterator loc =
std::find(Sections.begin(), Sections.end(), Sec);
assert(loc != Sections.end() && "Sec is not a valid section!");
return std::distance(Sections.begin(), loc);
}
const MachOObjectFileBase::SectionBase*
MachOObjectFileBase::getSectionBase(DataRefImpl DRI) const {
const LoadCommand *Command = getLoadCommandInfo(DRI.d.a);
uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(Command);
bool Is64 = is64Bit();
unsigned SegmentLoadSize =
Is64 ? sizeof(MachOObjectFile64Le::SegmentLoadCommand) :
sizeof(MachOObjectFile32Le::SegmentLoadCommand);
unsigned SectionSize = Is64 ? sizeof(MachOObjectFile64Le::Section) :
sizeof(MachOObjectFile32Le::Section);
uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + DRI.d.b * SectionSize;
return reinterpret_cast<const SectionBase*>(SectionAddr);
}
static StringRef parseSegmentOrSectionName(const char *P) {
if (P[15] == 0)
// Null terminated.
return P;
// Not null terminated, so this is a 16 char string.
return StringRef(P, 16);
}
ArrayRef<char> MachOObjectFileBase::getSectionRawName(DataRefImpl DRI) const {
const SectionBase *Base = getSectionBase(DRI);
return ArrayRef<char>(Base->Name);
}
error_code MachOObjectFileBase::getSectionName(DataRefImpl DRI,
StringRef &Result) const {
ArrayRef<char> Raw = getSectionRawName(DRI);
Result = parseSegmentOrSectionName(Raw.data());
return object_error::success;
}
ArrayRef<char>
MachOObjectFileBase::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
const SectionBase *Base = getSectionBase(Sec);
return ArrayRef<char>(Base->SegmentName);
}
StringRef
MachOObjectFileBase::getSectionFinalSegmentName(DataRefImpl DRI) const {
ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
return parseSegmentOrSectionName(Raw.data());
}
error_code MachOObjectFileBase::isSectionData(DataRefImpl DRI,
bool &Result) const {
// FIXME: Unimplemented.
Result = false;
return object_error::success;
}
error_code MachOObjectFileBase::isSectionBSS(DataRefImpl DRI,
bool &Result) const {
// FIXME: Unimplemented.
Result = false;
return object_error::success;
}
error_code
MachOObjectFileBase::isSectionRequiredForExecution(DataRefImpl Sec,
bool &Result) const {
// FIXME: Unimplemented.
Result = true;
return object_error::success;
}
error_code MachOObjectFileBase::isSectionVirtual(DataRefImpl Sec,
bool &Result) const {
// FIXME: Unimplemented.
Result = false;
return object_error::success;
}
error_code MachOObjectFileBase::isSectionReadOnlyData(DataRefImpl Sec,
bool &Result) const {
// Consider using the code from isSectionText to look for __const sections.
// Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
// to use section attributes to distinguish code from data.
// FIXME: Unimplemented.
Result = false;
return object_error::success;
}
relocation_iterator MachOObjectFileBase::getSectionRelBegin(DataRefImpl Sec) const {
DataRefImpl ret;
ret.d.b = getSectionIndex(Sec);
return relocation_iterator(RelocationRef(ret, this));
}
section_iterator MachOObjectFileBase::end_sections() const {
DataRefImpl DRI;
DRI.d.a = getHeader()->NumLoadCommands;
return section_iterator(SectionRef(DRI, this));
}
/*===-- Relocations -------------------------------------------------------===*/
error_code MachOObjectFileBase::getRelocationNext(DataRefImpl Rel,
RelocationRef &Res) const {
++Rel.d.a;
Res = RelocationRef(Rel, this);
return object_error::success;
}
// Helper to advance a section or symbol iterator multiple increments at a time.
template<class T>
error_code advance(T &it, size_t Val) {
error_code ec;
while (Val--) {
it.increment(ec);
}
return ec;
}
template<class T>
void advanceTo(T &it, size_t Val) {
if (error_code ec = advance(it, Val))
report_fatal_error(ec.message());
}
void
MachOObjectFileBase::printRelocationTargetName(const RelocationEntry *RE,
raw_string_ostream &fmt) const {
unsigned Arch = getArch();
bool isScattered = (Arch != Triple::x86_64) &&
(RE->Word0 & macho::RF_Scattered);
// Target of a scattered relocation is an address. In the interest of
// generating pretty output, scan through the symbol table looking for a
// symbol that aligns with that address. If we find one, print it.
// Otherwise, we just print the hex address of the target.
if (isScattered) {
uint32_t Val = RE->Word1;
error_code ec;
for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
SI.increment(ec)) {
if (ec) report_fatal_error(ec.message());
uint64_t Addr;
StringRef Name;
if ((ec = SI->getAddress(Addr)))
report_fatal_error(ec.message());
if (Addr != Val) continue;
if ((ec = SI->getName(Name)))
report_fatal_error(ec.message());
fmt << Name;
return;
}
// If we couldn't find a symbol that this relocation refers to, try
// to find a section beginning instead.
for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
SI.increment(ec)) {
if (ec) report_fatal_error(ec.message());
uint64_t Addr;
StringRef Name;
if ((ec = SI->getAddress(Addr)))
report_fatal_error(ec.message());
if (Addr != Val) continue;
if ((ec = SI->getName(Name)))
report_fatal_error(ec.message());
fmt << Name;
return;
}
fmt << format("0x%x", Val);
return;
}
StringRef S;
bool isExtern = (RE->Word1 >> 27) & 1;
uint32_t Val = RE->Word1 & 0xFFFFFF;
if (isExtern) {
symbol_iterator SI = begin_symbols();
advanceTo(SI, Val);
SI->getName(S);
} else {
section_iterator SI = begin_sections();
advanceTo(SI, Val);
SI->getName(S);
}
fmt << S;
}
error_code MachOObjectFileBase::getLibraryNext(DataRefImpl LibData,
LibraryRef &Res) const {
report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase");
}
error_code MachOObjectFileBase::getLibraryPath(DataRefImpl LibData,
StringRef &Res) const {
report_fatal_error("Needed libraries unimplemented in MachOObjectFileBase");
}
/*===-- Miscellaneous -----------------------------------------------------===*/
uint8_t MachOObjectFileBase::getBytesInAddress() const {
return is64Bit() ? 8 : 4;
}
StringRef MachOObjectFileBase::getFileFormatName() const {
if (!is64Bit()) {
switch (getHeader()->CPUType) {
case llvm::MachO::CPUTypeI386:
return "Mach-O 32-bit i386";
case llvm::MachO::CPUTypeARM:
return "Mach-O arm";
case llvm::MachO::CPUTypePowerPC:
return "Mach-O 32-bit ppc";
default:
assert((getHeader()->CPUType & llvm::MachO::CPUArchABI64) == 0 &&
"64-bit object file when we're not 64-bit?");
return "Mach-O 32-bit unknown";
}
}
// Make sure the cpu type has the correct mask.
assert((getHeader()->CPUType & llvm::MachO::CPUArchABI64)
== llvm::MachO::CPUArchABI64 &&
"32-bit object file when we're 64-bit?");
switch (getHeader()->CPUType) {
case llvm::MachO::CPUTypeX86_64:
return "Mach-O 64-bit x86-64";
case llvm::MachO::CPUTypePowerPC64:
return "Mach-O 64-bit ppc64";
default:
return "Mach-O 64-bit unknown";
}
}
unsigned MachOObjectFileBase::getArch() const {
switch (getHeader()->CPUType) {
case llvm::MachO::CPUTypeI386:
return Triple::x86;
case llvm::MachO::CPUTypeX86_64:
return Triple::x86_64;
case llvm::MachO::CPUTypeARM:
return Triple::arm;
case llvm::MachO::CPUTypePowerPC:
return Triple::ppc;
case llvm::MachO::CPUTypePowerPC64:
return Triple::ppc64;
default:
return Triple::UnknownArch;
}
}
} // end namespace object
} // end namespace llvm
|