blob: 4797a14ba87d93514b53f93ab9e9e7c8656b648f [file] [log] [blame]
Nick Kledzike34182f2013-11-06 21:36:55 +00001//===- lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp ------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10///
11/// \file Converts from in-memory Atoms to in-memory normalized mach-o.
12///
13/// +------------+
14/// | normalized |
15/// +------------+
16/// ^
17/// |
18/// |
Shankar Easwaran3d8de472014-01-27 03:09:26 +000019/// +-------+
Nick Kledzike34182f2013-11-06 21:36:55 +000020/// | Atoms |
Shankar Easwaran3d8de472014-01-27 03:09:26 +000021/// +-------+
Nick Kledzike34182f2013-11-06 21:36:55 +000022
23#include "MachONormalizedFile.h"
24#include "ReferenceKinds.h"
25
26#include "lld/Core/Error.h"
27#include "lld/Core/LLVM.h"
28
29#include "llvm/ADT/StringRef.h"
30#include "llvm/ADT/StringSwitch.h"
31#include "llvm/Support/Casting.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/Format.h"
35#include "llvm/Support/MachO.h"
36#include "llvm/Support/system_error.h"
37
38#include <map>
39
40using llvm::StringRef;
Nick Kledzike34182f2013-11-06 21:36:55 +000041using llvm::isa;
42using namespace llvm::MachO;
43using namespace lld::mach_o::normalized;
44using namespace lld;
45
46namespace {
47
48struct AtomInfo {
49 const DefinedAtom *atom;
50 uint64_t offsetInSection;
51};
52
53struct SectionInfo {
54 SectionInfo(StringRef seg, StringRef sect, SectionType type, uint32_t attr=0);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000055
Nick Kledzike34182f2013-11-06 21:36:55 +000056 StringRef segmentName;
57 StringRef sectionName;
58 SectionType type;
59 uint32_t attributes;
60 uint64_t address;
61 uint64_t size;
62 uint32_t alignment;
63 std::vector<AtomInfo> atomsAndOffsets;
64 uint32_t normalizedSectionIndex;
65 uint32_t finalSectionIndex;
66};
67
Shankar Easwaran3d8de472014-01-27 03:09:26 +000068SectionInfo::SectionInfo(StringRef sg, StringRef sct, SectionType t, uint32_t a)
69 : segmentName(sg), sectionName(sct), type(t), attributes(a),
70 address(0), size(0), alignment(0),
Nick Kledzike34182f2013-11-06 21:36:55 +000071 normalizedSectionIndex(0), finalSectionIndex(0) {
72}
73
74struct SegmentInfo {
75 SegmentInfo(StringRef name);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000076
Nick Kledzike34182f2013-11-06 21:36:55 +000077 StringRef name;
78 uint64_t address;
79 uint64_t size;
80 uint32_t access;
81 std::vector<SectionInfo*> sections;
82};
83
Shankar Easwaran3d8de472014-01-27 03:09:26 +000084SegmentInfo::SegmentInfo(StringRef n)
Nick Kledzike34182f2013-11-06 21:36:55 +000085 : name(n), address(0), size(0), access(0) {
86}
87
88
89class Util {
90public:
91 Util(const MachOLinkingContext &ctxt) : _context(ctxt), _entryAtom(nullptr) {}
92
93 void assignAtomsToSections(const lld::File &atomFile);
94 void organizeSections();
95 void assignAddressesToSections();
96 uint32_t fileFlags();
97 void copySegmentInfo(NormalizedFile &file);
98 void copySections(NormalizedFile &file);
99 void buildAtomToAddressMap();
100 void addSymbols(const lld::File &atomFile, NormalizedFile &file);
101 void addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file);
102 void addRebaseAndBindingInfo(const lld::File &, NormalizedFile &file);
103 void addSectionRelocs(const lld::File &, NormalizedFile &file);
104 void addDependentDylibs(const lld::File &, NormalizedFile &file);
105 void copyEntryPointAddress(NormalizedFile &file);
106
107private:
108 typedef std::map<DefinedAtom::ContentType, SectionInfo*> TypeToSection;
109 typedef llvm::DenseMap<const Atom*, uint64_t> AtomToAddress;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000110
Nick Kledzike34182f2013-11-06 21:36:55 +0000111 struct DylibInfo { int ordinal; bool hasWeak; bool hasNonWeak; };
112 typedef llvm::StringMap<DylibInfo> DylibPathToInfo;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000113
Nick Kledzike34182f2013-11-06 21:36:55 +0000114 SectionInfo *sectionForAtom(const DefinedAtom*);
115 SectionInfo *makeSection(DefinedAtom::ContentType);
116 void appendAtom(SectionInfo *sect, const DefinedAtom *atom);
117 SegmentInfo *segmentForName(StringRef segName);
118 void layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr);
119 void layoutSectionsInTextSegment(SegmentInfo *seg, uint64_t &addr);
120 void copySectionContent(SectionInfo *si, ContentBytes &content);
121 uint8_t scopeBits(const DefinedAtom* atom);
122 int dylibOrdinal(const SharedLibraryAtom *sa);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000123 void segIndexForSection(const SectionInfo *sect,
Nick Kledzike34182f2013-11-06 21:36:55 +0000124 uint8_t &segmentIndex, uint64_t &segmentStartAddr);
125 const Atom *targetOfLazyPointer(const DefinedAtom *lpAtom);
126 const Atom *targetOfStub(const DefinedAtom *stubAtom);
127 bool belongsInGlobalSymbolsSection(const DefinedAtom* atom);
128 void appendSection(SectionInfo *si, NormalizedFile &file);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000129 void appendReloc(const DefinedAtom *atom, const Reference *ref,
Nick Kledzike34182f2013-11-06 21:36:55 +0000130 Relocations &relocations);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000131
Nick Kledzike34182f2013-11-06 21:36:55 +0000132 static uint64_t alignTo(uint64_t value, uint8_t align2);
133 typedef llvm::DenseMap<const Atom*, uint32_t> AtomToIndex;
134 struct AtomAndIndex { const Atom *atom; uint32_t index; };
Joey Gouly9d263e02013-12-25 19:39:08 +0000135 struct AtomSorter {
136 bool operator()(const AtomAndIndex &left, const AtomAndIndex &right);
Nick Kledzike34182f2013-11-06 21:36:55 +0000137 };
Joey Gouly9d263e02013-12-25 19:39:08 +0000138 struct SegmentSorter {
139 bool operator()(const SegmentInfo *left, const SegmentInfo *right);
Nick Kledzike34182f2013-11-06 21:36:55 +0000140 static unsigned weight(const SegmentInfo *);
141 };
Joey Gouly9d263e02013-12-25 19:39:08 +0000142 struct TextSectionSorter {
143 bool operator()(const SectionInfo *left, const SectionInfo *right);
Nick Kledzike34182f2013-11-06 21:36:55 +0000144 static unsigned weight(const SectionInfo *);
145 };
146
147 const MachOLinkingContext &_context;
148 llvm::BumpPtrAllocator _allocator;
149 std::vector<SectionInfo*> _sectionInfos;
150 std::vector<SegmentInfo*> _segmentInfos;
151 TypeToSection _sectionMap;
152 AtomToAddress _atomToAddress;
153 DylibPathToInfo _dylibInfo;
154 const DefinedAtom *_entryAtom;
155 AtomToIndex _atomToSymbolIndex;
156};
157
158SectionInfo *Util::makeSection(DefinedAtom::ContentType type) {
159 switch ( type ) {
160 case DefinedAtom::typeCode:
161 return new (_allocator) SectionInfo("__TEXT", "__text",
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000162 S_REGULAR, S_ATTR_PURE_INSTRUCTIONS
Nick Kledzike34182f2013-11-06 21:36:55 +0000163 | S_ATTR_SOME_INSTRUCTIONS);
164 case DefinedAtom::typeCString:
165 return new (_allocator) SectionInfo("__TEXT", "__cstring",
166 S_CSTRING_LITERALS);
167 case DefinedAtom::typeStub:
168 return new (_allocator) SectionInfo("__TEXT", "__stubs",
169 S_SYMBOL_STUBS, S_ATTR_PURE_INSTRUCTIONS);
170 case DefinedAtom::typeStubHelper:
171 return new (_allocator) SectionInfo("__TEXT", "__stub_helper",
172 S_REGULAR, S_ATTR_PURE_INSTRUCTIONS);
173 case DefinedAtom::typeLazyPointer:
174 return new (_allocator) SectionInfo("__DATA", "__la_symbol_ptr",
175 S_LAZY_SYMBOL_POINTERS);
176 case DefinedAtom::typeGOT:
177 return new (_allocator) SectionInfo("__DATA", "__got",
178 S_NON_LAZY_SYMBOL_POINTERS);
Nick Kledzik61fdef62014-05-15 20:59:23 +0000179 case DefinedAtom::typeZeroFill:
180 return new (_allocator) SectionInfo("__DATA", "__bss",
181 S_ZEROFILL);
Nick Kledzika4a08d32014-05-27 23:20:52 +0000182 case DefinedAtom::typeInitializerPtr:
183 return new (_allocator) SectionInfo("__DATA", "__mod_init_func",
184 S_MOD_INIT_FUNC_POINTERS);
185 case DefinedAtom::typeTerminatorPtr:
186 return new (_allocator) SectionInfo("__DATA", "__mod_term_func",
187 S_MOD_TERM_FUNC_POINTERS);
Nick Kledzika0c13a22014-05-22 01:42:06 +0000188 case DefinedAtom::typeLiteral4:
189 return new (_allocator) SectionInfo("__TEXT", "__literal4",
190 S_4BYTE_LITERALS);
191 case DefinedAtom::typeLiteral8:
192 return new (_allocator) SectionInfo("__TEXT", "__literal8",
193 S_8BYTE_LITERALS);
194 case DefinedAtom::typeLiteral16:
195 return new (_allocator) SectionInfo("__TEXT", "__literal16",
196 S_16BYTE_LITERALS);
Nick Kledzik3e90e5f2014-05-27 20:25:06 +0000197 case DefinedAtom::typeUTF16String:
198 return new (_allocator) SectionInfo("__TEXT", "__ustring",
199 S_REGULAR);
Nick Kledzik9ede7022014-05-29 20:44:21 +0000200 case DefinedAtom::typeCFString:
201 return new (_allocator) SectionInfo("__DATA", "__cfstring",
202 S_REGULAR);
Nick Kledzikb367c252014-05-29 23:07:20 +0000203 case DefinedAtom::typeCFI:
204 return new (_allocator) SectionInfo("__TEXT", "__eh_frame",
205 S_COALESCED);
Nick Kledzikf317d662014-05-29 23:50:48 +0000206 case DefinedAtom::typeCompactUnwindInfo:
207 return new (_allocator) SectionInfo("__LD", "__compact_unwind",
208 S_REGULAR);
Nick Kledzike34182f2013-11-06 21:36:55 +0000209 default:
210 llvm_unreachable("TO DO: add support for more sections");
211 break;
212 }
213}
214
215
216
217SectionInfo *Util::sectionForAtom(const DefinedAtom *atom) {
218 DefinedAtom::ContentType type = atom->contentType();
219 auto pos = _sectionMap.find(type);
220 if ( pos != _sectionMap.end() )
221 return pos->second;
222 SectionInfo *si = makeSection(type);
223 _sectionInfos.push_back(si);
224 _sectionMap[type] = si;
225 return si;
226}
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000227
Nick Kledzike34182f2013-11-06 21:36:55 +0000228
229void Util::appendAtom(SectionInfo *sect, const DefinedAtom *atom) {
230 // Figure out offset for atom in this section given alignment constraints.
231 uint64_t offset = sect->size;
232 DefinedAtom::Alignment atomAlign = atom->alignment();
233 uint64_t align2 = 1 << atomAlign.powerOf2;
234 uint64_t requiredModulus = atomAlign.modulus;
235 uint64_t currentModulus = (offset % align2);
236 if ( currentModulus != requiredModulus ) {
237 if ( requiredModulus > currentModulus )
238 offset += requiredModulus-currentModulus;
239 else
240 offset += align2+requiredModulus-currentModulus;
241 }
242 // Record max alignment of any atom in this section.
243 if ( atomAlign.powerOf2 > sect->alignment )
244 sect->alignment = atomAlign.powerOf2;
245 // Assign atom to this section with this offset.
246 AtomInfo ai = {atom, offset};
247 sect->atomsAndOffsets.push_back(ai);
248 // Update section size to include this atom.
249 sect->size = offset + atom->size();
250}
251
252void Util::assignAtomsToSections(const lld::File &atomFile) {
253 for (const DefinedAtom *atom : atomFile.defined()) {
254 appendAtom(sectionForAtom(atom), atom);
255 }
256}
257
258SegmentInfo *Util::segmentForName(StringRef segName) {
259 for (SegmentInfo *si : _segmentInfos) {
260 if ( si->name.equals(segName) )
261 return si;
262 }
263 SegmentInfo *info = new (_allocator) SegmentInfo(segName);
264 if (segName.equals("__TEXT"))
265 info->access = VM_PROT_READ | VM_PROT_EXECUTE;
266 else if (segName.equals("__DATA"))
267 info->access = VM_PROT_READ | VM_PROT_WRITE;
268 else if (segName.equals("__PAGEZERO"))
269 info->access = 0;
270 _segmentInfos.push_back(info);
271 return info;
272}
273
274unsigned Util::SegmentSorter::weight(const SegmentInfo *seg) {
275 return llvm::StringSwitch<unsigned>(seg->name)
276 .Case("__PAGEZERO", 1)
277 .Case("__TEXT", 2)
278 .Case("__DATA", 3)
279 .Default(100);
280}
281
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000282bool Util::SegmentSorter::operator()(const SegmentInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000283 const SegmentInfo *right) {
284 return (weight(left) < weight(right));
285}
286
287unsigned Util::TextSectionSorter::weight(const SectionInfo *sect) {
288 return llvm::StringSwitch<unsigned>(sect->sectionName)
289 .Case("__text", 1)
290 .Case("__stubs", 2)
291 .Case("__stub_helper", 3)
292 .Case("__const", 4)
293 .Case("__cstring", 5)
294 .Case("__unwind_info", 98)
295 .Case("__eh_frame", 99)
296 .Default(10);
297}
298
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000299bool Util::TextSectionSorter::operator()(const SectionInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000300 const SectionInfo *right) {
301 return (weight(left) < weight(right));
302}
303
304
305void Util::organizeSections() {
306 if (_context.outputFileType() == llvm::MachO::MH_OBJECT) {
307 // Leave sections ordered as normalized file specified.
308 uint32_t sectionIndex = 1;
309 for (SectionInfo *si : _sectionInfos) {
310 si->finalSectionIndex = sectionIndex++;
311 }
312 } else {
313 // Main executables, need a zero-page segment
314 if (_context.outputFileType() == llvm::MachO::MH_EXECUTE)
315 segmentForName("__PAGEZERO");
316 // Group sections into segments.
317 for (SectionInfo *si : _sectionInfos) {
318 SegmentInfo *seg = segmentForName(si->segmentName);
319 seg->sections.push_back(si);
320 }
321 // Sort segments.
322 std::sort(_segmentInfos.begin(), _segmentInfos.end(), SegmentSorter());
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000323
Nick Kledzike34182f2013-11-06 21:36:55 +0000324 // Sort sections within segments.
325 for (SegmentInfo *seg : _segmentInfos) {
326 if (seg->name.equals("__TEXT")) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000327 std::sort(seg->sections.begin(), seg->sections.end(),
Nick Kledzike34182f2013-11-06 21:36:55 +0000328 TextSectionSorter());
329 }
330 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000331
Nick Kledzike34182f2013-11-06 21:36:55 +0000332 // Record final section indexes.
333 uint32_t sectionIndex = 1;
334 for (SegmentInfo *seg : _segmentInfos) {
335 for (SectionInfo *sect : seg->sections) {
336 sect->finalSectionIndex = sectionIndex++;
337 }
338 }
339 }
340
341}
342
343uint64_t Util::alignTo(uint64_t value, uint8_t align2) {
344 return llvm::RoundUpToAlignment(value, 1 << align2);
345}
346
347
348void Util::layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr) {
349 seg->address = addr;
350 for (SectionInfo *sect : seg->sections) {
351 sect->address = alignTo(addr, sect->alignment);
352 addr += sect->size;
353 }
354 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
355}
356
357
358// __TEXT segment lays out backwards so padding is at front after load commands.
359void Util::layoutSectionsInTextSegment(SegmentInfo *seg, uint64_t &addr) {
360 seg->address = addr;
361 // Walks sections starting at end to calculate padding for start.
362 int64_t taddr = 0;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000363 for (auto it = seg->sections.rbegin(); it != seg->sections.rend(); ++it) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000364 SectionInfo *sect = *it;
365 taddr -= sect->size;
366 taddr = taddr & (0 - (1 << sect->alignment));
367 }
368 int64_t padding = taddr;
369 while (padding < 0)
370 padding += _context.pageSize();
371 // Start assigning section address starting at padded offset.
372 addr += padding;
373 for (SectionInfo *sect : seg->sections) {
374 sect->address = alignTo(addr, sect->alignment);
375 addr = sect->address + sect->size;
376 }
377 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
378}
379
380
381void Util::assignAddressesToSections() {
382 uint64_t address = 0; // FIXME
383 if (_context.outputFileType() != llvm::MachO::MH_OBJECT) {
384 for (SegmentInfo *seg : _segmentInfos) {
385 if (seg->name.equals("__PAGEZERO")) {
386 seg->size = _context.pageZeroSize();
387 address += seg->size;
388 }
389 else if (seg->name.equals("__TEXT"))
390 layoutSectionsInTextSegment(seg, address);
391 else
392 layoutSectionsInSegment(seg, address);
393 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000394 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000395 llvm::dbgs() << "assignAddressesToSections()\n";
396 for (SegmentInfo *sgi : _segmentInfos) {
397 llvm::dbgs() << " address=" << llvm::format("0x%08llX", sgi->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000398 << ", size=" << llvm::format("0x%08llX", sgi->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000399 << ", segment-name='" << sgi->name
Nick Kledzik020a49c2013-11-06 21:57:52 +0000400 << "'\n";
401 for (SectionInfo *si : sgi->sections) {
402 llvm::dbgs()<< " addr=" << llvm::format("0x%08llX", si->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000403 << ", size=" << llvm::format("0x%08llX", si->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000404 << ", section-name='" << si->sectionName
Nick Kledzik020a49c2013-11-06 21:57:52 +0000405 << "\n";
406 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000407 }
Nick Kledzik020a49c2013-11-06 21:57:52 +0000408 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000409 } else {
410 for (SectionInfo *sect : _sectionInfos) {
411 sect->address = alignTo(address, sect->alignment);
412 address = sect->address + sect->size;
413 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000414 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000415 llvm::dbgs() << "assignAddressesToSections()\n";
416 for (SectionInfo *si : _sectionInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000417 llvm::dbgs() << " section=" << si->sectionName
Nick Kledzike34182f2013-11-06 21:36:55 +0000418 << " address= " << llvm::format("0x%08X", si->address)
419 << " size= " << llvm::format("0x%08X", si->size)
Nick Kledzik020a49c2013-11-06 21:57:52 +0000420 << "\n";
421 }
422 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000423 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000424}
425
426
427void Util::copySegmentInfo(NormalizedFile &file) {
428 for (SegmentInfo *sgi : _segmentInfos) {
429 Segment seg;
430 seg.name = sgi->name;
431 seg.address = sgi->address;
432 seg.size = sgi->size;
433 seg.access = sgi->access;
434 file.segments.push_back(seg);
435 }
436}
437
438void Util::appendSection(SectionInfo *si, NormalizedFile &file) {
439 // Add new empty section to end of file.sections.
440 Section temp;
441 file.sections.push_back(std::move(temp));
442 Section* normSect = &file.sections.back();
443 // Copy fields to normalized section.
444 normSect->segmentName = si->segmentName;
445 normSect->sectionName = si->sectionName;
446 normSect->type = si->type;
447 normSect->attributes = si->attributes;
448 normSect->address = si->address;
449 normSect->alignment = si->alignment;
450 // Record where normalized section is.
451 si->normalizedSectionIndex = file.sections.size()-1;
452 // Copy content from atoms to content buffer for section.
Nick Kledzik61fdef62014-05-15 20:59:23 +0000453 if (si->type == llvm::MachO::S_ZEROFILL)
454 return;
Nick Kledzik6edd7222014-01-11 01:07:43 +0000455 uint8_t *sectionContent = file.ownedAllocations.Allocate<uint8_t>(si->size);
456 normSect->content = llvm::makeArrayRef(sectionContent, si->size);
Nick Kledzike34182f2013-11-06 21:36:55 +0000457 for (AtomInfo &ai : si->atomsAndOffsets) {
458 // Copy raw bytes.
459 uint8_t *atomContent = reinterpret_cast<uint8_t*>
460 (&sectionContent[ai.offsetInSection]);
461 memcpy(atomContent, ai.atom->rawContent().data(), ai.atom->size());
462 // Apply fix-ups.
463 for (const Reference *ref : *ai.atom) {
464 uint32_t offset = ref->offsetInAtom();
465 uint64_t targetAddress = 0;
466 if ( ref->target() != nullptr )
467 targetAddress = _atomToAddress[ref->target()];
468 uint64_t fixupAddress = _atomToAddress[ai.atom] + offset;
Rui Ueyama170a1a82013-12-20 07:48:29 +0000469 _context.kindHandler().applyFixup(
470 ref->kindNamespace(), ref->kindArch(), ref->kindValue(),
471 ref->addend(), &atomContent[offset], fixupAddress, targetAddress);
Nick Kledzike34182f2013-11-06 21:36:55 +0000472 }
473 }
474}
475
476void Util::copySections(NormalizedFile &file) {
477 file.sections.reserve(_sectionInfos.size());
478 // For final linked images, write sections grouped by segment.
479 if (_context.outputFileType() != llvm::MachO::MH_OBJECT) {
480 for (SegmentInfo *sgi : _segmentInfos) {
481 for (SectionInfo *si : sgi->sections) {
482 appendSection(si, file);
483 }
484 }
485 } else {
486 // Object files write sections in default order.
487 for (SectionInfo *si : _sectionInfos) {
488 appendSection(si, file);
489 }
490 }
491}
492
493void Util::copyEntryPointAddress(NormalizedFile &nFile) {
494 if (_context.outputTypeHasEntry()) {
495 nFile.entryAddress = _atomToAddress[_entryAtom];
496 }
497}
498
499void Util::buildAtomToAddressMap() {
500 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
501 << "assign atom addresses:\n");
502 const bool lookForEntry = _context.outputTypeHasEntry();
503 for (SectionInfo *sect : _sectionInfos) {
504 for (const AtomInfo &info : sect->atomsAndOffsets) {
505 _atomToAddress[info.atom] = sect->address + info.offsetInSection;
506 if (lookForEntry && (info.atom->contentType() == DefinedAtom::typeCode) &&
507 (info.atom->size() != 0) &&
508 info.atom->name() == _context.entrySymbolName()) {
509 _entryAtom = info.atom;
510 }
511 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
512 << " address="
513 << llvm::format("0x%016X", _atomToAddress[info.atom])
514 << " atom=" << info.atom
515 << " name=" << info.atom->name() << "\n");
516 }
517 }
518}
519
520uint8_t Util::scopeBits(const DefinedAtom* atom) {
521 switch (atom->scope()) {
522 case Atom::scopeTranslationUnit:
523 return 0;
524 case Atom::scopeLinkageUnit:
525 return N_PEXT | N_EXT;
526 case Atom::scopeGlobal:
527 return N_EXT;
528 }
Nick Kledzik020fa7f2013-11-06 22:18:09 +0000529 llvm_unreachable("Unknown scope");
Nick Kledzike34182f2013-11-06 21:36:55 +0000530}
531
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000532bool Util::AtomSorter::operator()(const AtomAndIndex &left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000533 const AtomAndIndex &right) {
534 return (left.atom->name().compare(right.atom->name()) < 0);
535}
536
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000537
Nick Kledzike34182f2013-11-06 21:36:55 +0000538bool Util::belongsInGlobalSymbolsSection(const DefinedAtom* atom) {
539 return (atom->scope() == Atom::scopeGlobal);
540}
541
542void Util::addSymbols(const lld::File &atomFile, NormalizedFile &file) {
543 // Mach-O symbol table has three regions: locals, globals, undefs.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000544
Nick Kledzike34182f2013-11-06 21:36:55 +0000545 // Add all local (non-global) symbols in address order
546 std::vector<AtomAndIndex> globals;
547 globals.reserve(512);
548 for (SectionInfo *sect : _sectionInfos) {
549 for (const AtomInfo &info : sect->atomsAndOffsets) {
550 const DefinedAtom *atom = info.atom;
551 if (!atom->name().empty()) {
552 if (belongsInGlobalSymbolsSection(atom)) {
553 AtomAndIndex ai = { atom, sect->finalSectionIndex };
554 globals.push_back(ai);
555 } else {
556 Symbol sym;
557 sym.name = atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000558 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000559 sym.scope = scopeBits(atom);
560 sym.sect = sect->finalSectionIndex;
561 sym.desc = 0;
562 sym.value = _atomToAddress[atom];
563 file.localSymbols.push_back(sym);
564 }
565 }
566 }
567 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000568
Nick Kledzike34182f2013-11-06 21:36:55 +0000569 // Sort global symbol alphabetically, then add to symbol table.
570 std::sort(globals.begin(), globals.end(), AtomSorter());
571 for (AtomAndIndex &ai : globals) {
572 Symbol sym;
573 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000574 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000575 sym.scope = scopeBits(static_cast<const DefinedAtom*>(ai.atom));
576 sym.sect = ai.index;
577 sym.desc = 0;
578 sym.value = _atomToAddress[ai.atom];
579 file.globalSymbols.push_back(sym);
580 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000581
582
Nick Kledzike34182f2013-11-06 21:36:55 +0000583 // Sort undefined symbol alphabetically, then add to symbol table.
584 std::vector<AtomAndIndex> undefs;
585 undefs.reserve(128);
586 for (const UndefinedAtom *atom : atomFile.undefined()) {
587 AtomAndIndex ai = { atom, 0 };
588 undefs.push_back(ai);
589 }
590 for (const SharedLibraryAtom *atom : atomFile.sharedLibrary()) {
591 AtomAndIndex ai = { atom, 0 };
592 undefs.push_back(ai);
593 }
594 std::sort(undefs.begin(), undefs.end(), AtomSorter());
595 const uint32_t start = file.globalSymbols.size() + file.localSymbols.size();
596 for (AtomAndIndex &ai : undefs) {
597 Symbol sym;
598 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000599 sym.type = N_UNDF;
Nick Kledzike34182f2013-11-06 21:36:55 +0000600 sym.scope = N_EXT;
601 sym.sect = 0;
602 sym.desc = 0;
603 sym.value = 0;
604 _atomToSymbolIndex[ai.atom] = file.undefinedSymbols.size() + start;
605 file.undefinedSymbols.push_back(sym);
606 }
607}
608
609const Atom *Util::targetOfLazyPointer(const DefinedAtom *lpAtom) {
610 for (const Reference *ref : *lpAtom) {
Nick Kledzike5552772013-12-19 21:58:00 +0000611 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000612 return ref->target();
613 }
614 }
615 return nullptr;
616}
617
618const Atom *Util::targetOfStub(const DefinedAtom *stubAtom) {
619 for (const Reference *ref : *stubAtom) {
620 if (const Atom *ta = ref->target()) {
621 if (const DefinedAtom *lpAtom = dyn_cast<DefinedAtom>(ta)) {
622 const Atom *target = targetOfLazyPointer(lpAtom);
623 if (target)
624 return target;
625 }
626 }
627 }
628 return nullptr;
629}
630
631
632void Util::addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file) {
633 for (SectionInfo *si : _sectionInfos) {
634 Section &normSect = file.sections[si->normalizedSectionIndex];
635 switch (si->type) {
636 case llvm::MachO::S_NON_LAZY_SYMBOL_POINTERS:
637 for (const AtomInfo &info : si->atomsAndOffsets) {
638 bool foundTarget = false;
639 for (const Reference *ref : *info.atom) {
640 const Atom *target = ref->target();
641 if (target) {
642 if (isa<const SharedLibraryAtom>(target)) {
643 uint32_t index = _atomToSymbolIndex[target];
644 normSect.indirectSymbols.push_back(index);
645 foundTarget = true;
646 } else {
647 normSect.indirectSymbols.push_back(
648 llvm::MachO::INDIRECT_SYMBOL_LOCAL);
649 }
650 }
651 }
652 if (!foundTarget) {
653 normSect.indirectSymbols.push_back(
654 llvm::MachO::INDIRECT_SYMBOL_ABS);
655 }
656 }
657 break;
658 case llvm::MachO::S_LAZY_SYMBOL_POINTERS:
659 for (const AtomInfo &info : si->atomsAndOffsets) {
660 const Atom *target = targetOfLazyPointer(info.atom);
661 if (target) {
662 uint32_t index = _atomToSymbolIndex[target];
663 normSect.indirectSymbols.push_back(index);
664 }
665 }
666 break;
667 case llvm::MachO::S_SYMBOL_STUBS:
668 for (const AtomInfo &info : si->atomsAndOffsets) {
669 const Atom *target = targetOfStub(info.atom);
670 if (target) {
671 uint32_t index = _atomToSymbolIndex[target];
672 normSect.indirectSymbols.push_back(index);
673 }
674 }
675 break;
676 default:
677 break;
678 }
679 }
680
681}
682
683void Util::addDependentDylibs(const lld::File &atomFile,NormalizedFile &nFile) {
684 // Scan all imported symbols and build up list of dylibs they are from.
685 int ordinal = 1;
686 for (const SharedLibraryAtom *slAtom : atomFile.sharedLibrary()) {
687 StringRef loadPath = slAtom->loadName();
688 DylibPathToInfo::iterator pos = _dylibInfo.find(loadPath);
689 if (pos == _dylibInfo.end()) {
690 DylibInfo info;
691 info.ordinal = ordinal++;
692 info.hasWeak = slAtom->canBeNullAtRuntime();
693 info.hasNonWeak = !info.hasWeak;
694 _dylibInfo[loadPath] = info;
695 DependentDylib depInfo;
696 depInfo.path = loadPath;
697 depInfo.kind = llvm::MachO::LC_LOAD_DYLIB;
698 nFile.dependentDylibs.push_back(depInfo);
699 } else {
700 if ( slAtom->canBeNullAtRuntime() )
701 pos->second.hasWeak = true;
702 else
703 pos->second.hasNonWeak = true;
704 }
705 }
706 // Automatically weak link dylib in which all symbols are weak (canBeNull).
707 for (DependentDylib &dep : nFile.dependentDylibs) {
708 DylibInfo &info = _dylibInfo[dep.path];
709 if (info.hasWeak && !info.hasNonWeak)
710 dep.kind = llvm::MachO::LC_LOAD_WEAK_DYLIB;
711 }
712}
713
714
715int Util::dylibOrdinal(const SharedLibraryAtom *sa) {
716 return _dylibInfo[sa->loadName()].ordinal;
717}
718
719void Util::segIndexForSection(const SectionInfo *sect, uint8_t &segmentIndex,
720 uint64_t &segmentStartAddr) {
721 segmentIndex = 0;
722 for (const SegmentInfo *seg : _segmentInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000723 if ((seg->address <= sect->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000724 && (seg->address+seg->size >= sect->address+sect->size)) {
725 segmentStartAddr = seg->address;
726 return;
727 }
728 ++segmentIndex;
729 }
730 llvm_unreachable("section not in any segment");
731}
732
733
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000734void Util::appendReloc(const DefinedAtom *atom, const Reference *ref,
Nick Kledzike34182f2013-11-06 21:36:55 +0000735 Relocations &relocations) {
736 // TODO: convert Reference to normalized relocation
737}
738
739void Util::addSectionRelocs(const lld::File &, NormalizedFile &file) {
740 if (_context.outputFileType() != llvm::MachO::MH_OBJECT)
741 return;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000742
Nick Kledzike34182f2013-11-06 21:36:55 +0000743 for (SectionInfo *si : _sectionInfos) {
744 Section &normSect = file.sections[si->normalizedSectionIndex];
745 for (const AtomInfo &info : si->atomsAndOffsets) {
746 const DefinedAtom *atom = info.atom;
747 for (const Reference *ref : *atom) {
748 appendReloc(atom, ref, normSect.relocations);
749 }
750 }
751 }
752}
753
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000754void Util::addRebaseAndBindingInfo(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000755 NormalizedFile &nFile) {
756 if (_context.outputFileType() == llvm::MachO::MH_OBJECT)
757 return;
758
759 uint8_t segmentIndex;
760 uint64_t segmentStartAddr;
761 for (SectionInfo *sect : _sectionInfos) {
762 segIndexForSection(sect, segmentIndex, segmentStartAddr);
763 for (const AtomInfo &info : sect->atomsAndOffsets) {
764 const DefinedAtom *atom = info.atom;
765 for (const Reference *ref : *atom) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000766 uint64_t segmentOffset = _atomToAddress[atom] + ref->offsetInAtom()
Nick Kledzike34182f2013-11-06 21:36:55 +0000767 - segmentStartAddr;
768 const Atom* targ = ref->target();
Nick Kledzike5552772013-12-19 21:58:00 +0000769 if (_context.kindHandler().isPointer(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000770 // A pointer to a DefinedAtom requires rebasing.
771 if (dyn_cast<DefinedAtom>(targ)) {
772 RebaseLocation rebase;
773 rebase.segIndex = segmentIndex;
774 rebase.segOffset = segmentOffset;
775 rebase.kind = llvm::MachO::REBASE_TYPE_POINTER;
776 nFile.rebasingInfo.push_back(rebase);
777 }
778 // A pointer to an SharedLibraryAtom requires binding.
779 if (const SharedLibraryAtom *sa = dyn_cast<SharedLibraryAtom>(targ)) {
780 BindLocation bind;
781 bind.segIndex = segmentIndex;
782 bind.segOffset = segmentOffset;
783 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
784 bind.canBeNull = sa->canBeNullAtRuntime();
785 bind.ordinal = dylibOrdinal(sa);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000786 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000787 bind.addend = ref->addend();
788 nFile.bindingInfo.push_back(bind);
789 }
790 }
Nick Kledzike5552772013-12-19 21:58:00 +0000791 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000792 BindLocation bind;
793 bind.segIndex = segmentIndex;
794 bind.segOffset = segmentOffset;
795 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
796 bind.canBeNull = false; //sa->canBeNullAtRuntime();
797 bind.ordinal = 1;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000798 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000799 bind.addend = ref->addend();
800 nFile.lazyBindingInfo.push_back(bind);
801 }
802 }
803 }
804 }
805}
806
807uint32_t Util::fileFlags() {
808 return 0; //FIX ME
809}
810
811} // end anonymous namespace
812
813
814namespace lld {
815namespace mach_o {
816namespace normalized {
817
818/// Convert a set of Atoms into a normalized mach-o file.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000819ErrorOr<std::unique_ptr<NormalizedFile>>
820normalizedFromAtoms(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000821 const MachOLinkingContext &context) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000822 // The util object buffers info until the normalized file can be made.
Nick Kledzike34182f2013-11-06 21:36:55 +0000823 Util util(context);
824 util.assignAtomsToSections(atomFile);
825 util.organizeSections();
826 util.assignAddressesToSections();
827 util.buildAtomToAddressMap();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000828
Nick Kledzike34182f2013-11-06 21:36:55 +0000829 std::unique_ptr<NormalizedFile> f(new NormalizedFile());
830 NormalizedFile &normFile = *f.get();
831 f->arch = context.arch();
832 f->fileType = context.outputFileType();
833 f->flags = util.fileFlags();
834 util.copySegmentInfo(normFile);
835 util.copySections(normFile);
836 util.addDependentDylibs(atomFile, normFile);
837 util.addSymbols(atomFile, normFile);
838 util.addIndirectSymbols(atomFile, normFile);
839 util.addRebaseAndBindingInfo(atomFile, normFile);
840 util.addSectionRelocs(atomFile, normFile);
841 util.copyEntryPointAddress(normFile);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000842
Nick Kledzike34182f2013-11-06 21:36:55 +0000843 return std::move(f);
844}
845
846
847} // namespace normalized
848} // namespace mach_o
849} // namespace lld
850