blob: 0173076131854205b0bec4a6e695ed0abc37c0e0 [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;
Nick Kledzikacfad802014-05-30 22:51:04 +0000152 std::vector<SectionInfo*> _customSections;
Nick Kledzike34182f2013-11-06 21:36:55 +0000153 AtomToAddress _atomToAddress;
154 DylibPathToInfo _dylibInfo;
155 const DefinedAtom *_entryAtom;
156 AtomToIndex _atomToSymbolIndex;
157};
158
159SectionInfo *Util::makeSection(DefinedAtom::ContentType type) {
160 switch ( type ) {
161 case DefinedAtom::typeCode:
162 return new (_allocator) SectionInfo("__TEXT", "__text",
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000163 S_REGULAR, S_ATTR_PURE_INSTRUCTIONS
Nick Kledzike34182f2013-11-06 21:36:55 +0000164 | S_ATTR_SOME_INSTRUCTIONS);
165 case DefinedAtom::typeCString:
166 return new (_allocator) SectionInfo("__TEXT", "__cstring",
167 S_CSTRING_LITERALS);
168 case DefinedAtom::typeStub:
169 return new (_allocator) SectionInfo("__TEXT", "__stubs",
170 S_SYMBOL_STUBS, S_ATTR_PURE_INSTRUCTIONS);
171 case DefinedAtom::typeStubHelper:
172 return new (_allocator) SectionInfo("__TEXT", "__stub_helper",
173 S_REGULAR, S_ATTR_PURE_INSTRUCTIONS);
174 case DefinedAtom::typeLazyPointer:
175 return new (_allocator) SectionInfo("__DATA", "__la_symbol_ptr",
176 S_LAZY_SYMBOL_POINTERS);
177 case DefinedAtom::typeGOT:
178 return new (_allocator) SectionInfo("__DATA", "__got",
179 S_NON_LAZY_SYMBOL_POINTERS);
Nick Kledzik61fdef62014-05-15 20:59:23 +0000180 case DefinedAtom::typeZeroFill:
181 return new (_allocator) SectionInfo("__DATA", "__bss",
182 S_ZEROFILL);
Nick Kledzika4a08d32014-05-27 23:20:52 +0000183 case DefinedAtom::typeInitializerPtr:
184 return new (_allocator) SectionInfo("__DATA", "__mod_init_func",
185 S_MOD_INIT_FUNC_POINTERS);
186 case DefinedAtom::typeTerminatorPtr:
187 return new (_allocator) SectionInfo("__DATA", "__mod_term_func",
188 S_MOD_TERM_FUNC_POINTERS);
Nick Kledzika0c13a22014-05-22 01:42:06 +0000189 case DefinedAtom::typeLiteral4:
190 return new (_allocator) SectionInfo("__TEXT", "__literal4",
191 S_4BYTE_LITERALS);
192 case DefinedAtom::typeLiteral8:
193 return new (_allocator) SectionInfo("__TEXT", "__literal8",
194 S_8BYTE_LITERALS);
195 case DefinedAtom::typeLiteral16:
196 return new (_allocator) SectionInfo("__TEXT", "__literal16",
197 S_16BYTE_LITERALS);
Nick Kledzik3e90e5f2014-05-27 20:25:06 +0000198 case DefinedAtom::typeUTF16String:
199 return new (_allocator) SectionInfo("__TEXT", "__ustring",
200 S_REGULAR);
Nick Kledzik9ede7022014-05-29 20:44:21 +0000201 case DefinedAtom::typeCFString:
202 return new (_allocator) SectionInfo("__DATA", "__cfstring",
203 S_REGULAR);
Nick Kledzikb367c252014-05-29 23:07:20 +0000204 case DefinedAtom::typeCFI:
205 return new (_allocator) SectionInfo("__TEXT", "__eh_frame",
206 S_COALESCED);
Nick Kledzikf317d662014-05-29 23:50:48 +0000207 case DefinedAtom::typeCompactUnwindInfo:
208 return new (_allocator) SectionInfo("__LD", "__compact_unwind",
209 S_REGULAR);
Nick Kledzikacfad802014-05-30 22:51:04 +0000210 case DefinedAtom::typeConstant:
211 return new (_allocator) SectionInfo("__TEXT", "__const",
212 S_REGULAR);
213 case DefinedAtom::typeData:
214 return new (_allocator) SectionInfo("__DATA", "__data",
215 S_REGULAR);
216 case DefinedAtom::typeConstData:
217 return new (_allocator) SectionInfo("__DATA", "__const",
218 S_REGULAR);
219 case DefinedAtom::typeLSDA:
220 return new (_allocator) SectionInfo("__TEXT", "__gcc_except_tab",
221 S_REGULAR);
Nick Kledzike34182f2013-11-06 21:36:55 +0000222 default:
223 llvm_unreachable("TO DO: add support for more sections");
224 break;
225 }
226}
227
228
229
230SectionInfo *Util::sectionForAtom(const DefinedAtom *atom) {
Nick Kledzikacfad802014-05-30 22:51:04 +0000231 if (atom->sectionChoice() == DefinedAtom::sectionBasedOnContent) {
232 // Section for this atom is derived from content type.
233 DefinedAtom::ContentType type = atom->contentType();
234 auto pos = _sectionMap.find(type);
235 if ( pos != _sectionMap.end() )
236 return pos->second;
237 SectionInfo *si = makeSection(type);
238 _sectionInfos.push_back(si);
239 _sectionMap[type] = si;
240 return si;
241 } else {
242 // This atom needs to be in a custom section.
243 StringRef customName = atom->customSectionName();
244 // Look to see if we have already allocated the needed custom section.
245 for(SectionInfo *sect : _customSections) {
246 const DefinedAtom *firstAtom = sect->atomsAndOffsets.front().atom;
247 if (firstAtom->customSectionName().equals(customName)) {
248 return sect;
249 }
250 }
251 // Not found, so need to create a new custom section.
252 size_t seperatorIndex = customName.find('/');
253 assert(seperatorIndex != StringRef::npos);
254 StringRef segName = customName.slice(0, seperatorIndex-1);
255 StringRef sectName = customName.drop_front(seperatorIndex);
256 SectionInfo *sect = new (_allocator) SectionInfo(segName, sectName,
257 S_REGULAR);
258 _customSections.push_back(sect);
259 return sect;
260 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000261}
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000262
Nick Kledzike34182f2013-11-06 21:36:55 +0000263
264void Util::appendAtom(SectionInfo *sect, const DefinedAtom *atom) {
265 // Figure out offset for atom in this section given alignment constraints.
266 uint64_t offset = sect->size;
267 DefinedAtom::Alignment atomAlign = atom->alignment();
268 uint64_t align2 = 1 << atomAlign.powerOf2;
269 uint64_t requiredModulus = atomAlign.modulus;
270 uint64_t currentModulus = (offset % align2);
271 if ( currentModulus != requiredModulus ) {
272 if ( requiredModulus > currentModulus )
273 offset += requiredModulus-currentModulus;
274 else
275 offset += align2+requiredModulus-currentModulus;
276 }
277 // Record max alignment of any atom in this section.
278 if ( atomAlign.powerOf2 > sect->alignment )
279 sect->alignment = atomAlign.powerOf2;
280 // Assign atom to this section with this offset.
281 AtomInfo ai = {atom, offset};
282 sect->atomsAndOffsets.push_back(ai);
283 // Update section size to include this atom.
284 sect->size = offset + atom->size();
285}
286
287void Util::assignAtomsToSections(const lld::File &atomFile) {
288 for (const DefinedAtom *atom : atomFile.defined()) {
289 appendAtom(sectionForAtom(atom), atom);
290 }
291}
292
293SegmentInfo *Util::segmentForName(StringRef segName) {
294 for (SegmentInfo *si : _segmentInfos) {
295 if ( si->name.equals(segName) )
296 return si;
297 }
298 SegmentInfo *info = new (_allocator) SegmentInfo(segName);
299 if (segName.equals("__TEXT"))
300 info->access = VM_PROT_READ | VM_PROT_EXECUTE;
301 else if (segName.equals("__DATA"))
302 info->access = VM_PROT_READ | VM_PROT_WRITE;
303 else if (segName.equals("__PAGEZERO"))
304 info->access = 0;
305 _segmentInfos.push_back(info);
306 return info;
307}
308
309unsigned Util::SegmentSorter::weight(const SegmentInfo *seg) {
310 return llvm::StringSwitch<unsigned>(seg->name)
311 .Case("__PAGEZERO", 1)
312 .Case("__TEXT", 2)
313 .Case("__DATA", 3)
314 .Default(100);
315}
316
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000317bool Util::SegmentSorter::operator()(const SegmentInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000318 const SegmentInfo *right) {
319 return (weight(left) < weight(right));
320}
321
322unsigned Util::TextSectionSorter::weight(const SectionInfo *sect) {
323 return llvm::StringSwitch<unsigned>(sect->sectionName)
324 .Case("__text", 1)
325 .Case("__stubs", 2)
326 .Case("__stub_helper", 3)
327 .Case("__const", 4)
328 .Case("__cstring", 5)
329 .Case("__unwind_info", 98)
330 .Case("__eh_frame", 99)
331 .Default(10);
332}
333
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000334bool Util::TextSectionSorter::operator()(const SectionInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000335 const SectionInfo *right) {
336 return (weight(left) < weight(right));
337}
338
339
340void Util::organizeSections() {
341 if (_context.outputFileType() == llvm::MachO::MH_OBJECT) {
342 // Leave sections ordered as normalized file specified.
343 uint32_t sectionIndex = 1;
344 for (SectionInfo *si : _sectionInfos) {
345 si->finalSectionIndex = sectionIndex++;
346 }
347 } else {
348 // Main executables, need a zero-page segment
349 if (_context.outputFileType() == llvm::MachO::MH_EXECUTE)
350 segmentForName("__PAGEZERO");
351 // Group sections into segments.
352 for (SectionInfo *si : _sectionInfos) {
353 SegmentInfo *seg = segmentForName(si->segmentName);
354 seg->sections.push_back(si);
355 }
356 // Sort segments.
357 std::sort(_segmentInfos.begin(), _segmentInfos.end(), SegmentSorter());
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000358
Nick Kledzike34182f2013-11-06 21:36:55 +0000359 // Sort sections within segments.
360 for (SegmentInfo *seg : _segmentInfos) {
361 if (seg->name.equals("__TEXT")) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000362 std::sort(seg->sections.begin(), seg->sections.end(),
Nick Kledzike34182f2013-11-06 21:36:55 +0000363 TextSectionSorter());
364 }
365 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000366
Nick Kledzike34182f2013-11-06 21:36:55 +0000367 // Record final section indexes.
368 uint32_t sectionIndex = 1;
369 for (SegmentInfo *seg : _segmentInfos) {
370 for (SectionInfo *sect : seg->sections) {
371 sect->finalSectionIndex = sectionIndex++;
372 }
373 }
374 }
375
376}
377
378uint64_t Util::alignTo(uint64_t value, uint8_t align2) {
379 return llvm::RoundUpToAlignment(value, 1 << align2);
380}
381
382
383void Util::layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr) {
384 seg->address = addr;
385 for (SectionInfo *sect : seg->sections) {
386 sect->address = alignTo(addr, sect->alignment);
387 addr += sect->size;
388 }
389 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
390}
391
392
393// __TEXT segment lays out backwards so padding is at front after load commands.
394void Util::layoutSectionsInTextSegment(SegmentInfo *seg, uint64_t &addr) {
395 seg->address = addr;
396 // Walks sections starting at end to calculate padding for start.
397 int64_t taddr = 0;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000398 for (auto it = seg->sections.rbegin(); it != seg->sections.rend(); ++it) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000399 SectionInfo *sect = *it;
400 taddr -= sect->size;
401 taddr = taddr & (0 - (1 << sect->alignment));
402 }
403 int64_t padding = taddr;
404 while (padding < 0)
405 padding += _context.pageSize();
406 // Start assigning section address starting at padded offset.
407 addr += padding;
408 for (SectionInfo *sect : seg->sections) {
409 sect->address = alignTo(addr, sect->alignment);
410 addr = sect->address + sect->size;
411 }
412 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
413}
414
415
416void Util::assignAddressesToSections() {
417 uint64_t address = 0; // FIXME
418 if (_context.outputFileType() != llvm::MachO::MH_OBJECT) {
419 for (SegmentInfo *seg : _segmentInfos) {
420 if (seg->name.equals("__PAGEZERO")) {
421 seg->size = _context.pageZeroSize();
422 address += seg->size;
423 }
424 else if (seg->name.equals("__TEXT"))
425 layoutSectionsInTextSegment(seg, address);
426 else
427 layoutSectionsInSegment(seg, address);
428 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000429 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000430 llvm::dbgs() << "assignAddressesToSections()\n";
431 for (SegmentInfo *sgi : _segmentInfos) {
432 llvm::dbgs() << " address=" << llvm::format("0x%08llX", sgi->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000433 << ", size=" << llvm::format("0x%08llX", sgi->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000434 << ", segment-name='" << sgi->name
Nick Kledzik020a49c2013-11-06 21:57:52 +0000435 << "'\n";
436 for (SectionInfo *si : sgi->sections) {
437 llvm::dbgs()<< " addr=" << llvm::format("0x%08llX", si->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000438 << ", size=" << llvm::format("0x%08llX", si->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000439 << ", section-name='" << si->sectionName
Nick Kledzik020a49c2013-11-06 21:57:52 +0000440 << "\n";
441 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000442 }
Nick Kledzik020a49c2013-11-06 21:57:52 +0000443 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000444 } else {
445 for (SectionInfo *sect : _sectionInfos) {
446 sect->address = alignTo(address, sect->alignment);
447 address = sect->address + sect->size;
448 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000449 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000450 llvm::dbgs() << "assignAddressesToSections()\n";
451 for (SectionInfo *si : _sectionInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000452 llvm::dbgs() << " section=" << si->sectionName
Nick Kledzike34182f2013-11-06 21:36:55 +0000453 << " address= " << llvm::format("0x%08X", si->address)
454 << " size= " << llvm::format("0x%08X", si->size)
Nick Kledzik020a49c2013-11-06 21:57:52 +0000455 << "\n";
456 }
457 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000458 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000459}
460
461
462void Util::copySegmentInfo(NormalizedFile &file) {
463 for (SegmentInfo *sgi : _segmentInfos) {
464 Segment seg;
465 seg.name = sgi->name;
466 seg.address = sgi->address;
467 seg.size = sgi->size;
468 seg.access = sgi->access;
469 file.segments.push_back(seg);
470 }
471}
472
473void Util::appendSection(SectionInfo *si, NormalizedFile &file) {
474 // Add new empty section to end of file.sections.
475 Section temp;
476 file.sections.push_back(std::move(temp));
477 Section* normSect = &file.sections.back();
478 // Copy fields to normalized section.
479 normSect->segmentName = si->segmentName;
480 normSect->sectionName = si->sectionName;
481 normSect->type = si->type;
482 normSect->attributes = si->attributes;
483 normSect->address = si->address;
484 normSect->alignment = si->alignment;
485 // Record where normalized section is.
486 si->normalizedSectionIndex = file.sections.size()-1;
487 // Copy content from atoms to content buffer for section.
Nick Kledzik61fdef62014-05-15 20:59:23 +0000488 if (si->type == llvm::MachO::S_ZEROFILL)
489 return;
Nick Kledzik6edd7222014-01-11 01:07:43 +0000490 uint8_t *sectionContent = file.ownedAllocations.Allocate<uint8_t>(si->size);
491 normSect->content = llvm::makeArrayRef(sectionContent, si->size);
Nick Kledzike34182f2013-11-06 21:36:55 +0000492 for (AtomInfo &ai : si->atomsAndOffsets) {
493 // Copy raw bytes.
494 uint8_t *atomContent = reinterpret_cast<uint8_t*>
495 (&sectionContent[ai.offsetInSection]);
496 memcpy(atomContent, ai.atom->rawContent().data(), ai.atom->size());
497 // Apply fix-ups.
498 for (const Reference *ref : *ai.atom) {
499 uint32_t offset = ref->offsetInAtom();
500 uint64_t targetAddress = 0;
501 if ( ref->target() != nullptr )
502 targetAddress = _atomToAddress[ref->target()];
503 uint64_t fixupAddress = _atomToAddress[ai.atom] + offset;
Rui Ueyama170a1a82013-12-20 07:48:29 +0000504 _context.kindHandler().applyFixup(
505 ref->kindNamespace(), ref->kindArch(), ref->kindValue(),
506 ref->addend(), &atomContent[offset], fixupAddress, targetAddress);
Nick Kledzike34182f2013-11-06 21:36:55 +0000507 }
508 }
509}
510
511void Util::copySections(NormalizedFile &file) {
512 file.sections.reserve(_sectionInfos.size());
513 // For final linked images, write sections grouped by segment.
514 if (_context.outputFileType() != llvm::MachO::MH_OBJECT) {
515 for (SegmentInfo *sgi : _segmentInfos) {
516 for (SectionInfo *si : sgi->sections) {
517 appendSection(si, file);
518 }
519 }
520 } else {
521 // Object files write sections in default order.
522 for (SectionInfo *si : _sectionInfos) {
523 appendSection(si, file);
524 }
525 }
526}
527
528void Util::copyEntryPointAddress(NormalizedFile &nFile) {
529 if (_context.outputTypeHasEntry()) {
530 nFile.entryAddress = _atomToAddress[_entryAtom];
531 }
532}
533
534void Util::buildAtomToAddressMap() {
535 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
536 << "assign atom addresses:\n");
537 const bool lookForEntry = _context.outputTypeHasEntry();
538 for (SectionInfo *sect : _sectionInfos) {
539 for (const AtomInfo &info : sect->atomsAndOffsets) {
540 _atomToAddress[info.atom] = sect->address + info.offsetInSection;
541 if (lookForEntry && (info.atom->contentType() == DefinedAtom::typeCode) &&
542 (info.atom->size() != 0) &&
543 info.atom->name() == _context.entrySymbolName()) {
544 _entryAtom = info.atom;
545 }
546 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
547 << " address="
548 << llvm::format("0x%016X", _atomToAddress[info.atom])
549 << " atom=" << info.atom
550 << " name=" << info.atom->name() << "\n");
551 }
552 }
553}
554
555uint8_t Util::scopeBits(const DefinedAtom* atom) {
556 switch (atom->scope()) {
557 case Atom::scopeTranslationUnit:
558 return 0;
559 case Atom::scopeLinkageUnit:
560 return N_PEXT | N_EXT;
561 case Atom::scopeGlobal:
562 return N_EXT;
563 }
Nick Kledzik020fa7f2013-11-06 22:18:09 +0000564 llvm_unreachable("Unknown scope");
Nick Kledzike34182f2013-11-06 21:36:55 +0000565}
566
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000567bool Util::AtomSorter::operator()(const AtomAndIndex &left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000568 const AtomAndIndex &right) {
569 return (left.atom->name().compare(right.atom->name()) < 0);
570}
571
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000572
Nick Kledzike34182f2013-11-06 21:36:55 +0000573bool Util::belongsInGlobalSymbolsSection(const DefinedAtom* atom) {
574 return (atom->scope() == Atom::scopeGlobal);
575}
576
577void Util::addSymbols(const lld::File &atomFile, NormalizedFile &file) {
578 // Mach-O symbol table has three regions: locals, globals, undefs.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000579
Nick Kledzike34182f2013-11-06 21:36:55 +0000580 // Add all local (non-global) symbols in address order
581 std::vector<AtomAndIndex> globals;
582 globals.reserve(512);
583 for (SectionInfo *sect : _sectionInfos) {
584 for (const AtomInfo &info : sect->atomsAndOffsets) {
585 const DefinedAtom *atom = info.atom;
586 if (!atom->name().empty()) {
587 if (belongsInGlobalSymbolsSection(atom)) {
588 AtomAndIndex ai = { atom, sect->finalSectionIndex };
589 globals.push_back(ai);
590 } else {
591 Symbol sym;
592 sym.name = atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000593 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000594 sym.scope = scopeBits(atom);
595 sym.sect = sect->finalSectionIndex;
596 sym.desc = 0;
597 sym.value = _atomToAddress[atom];
598 file.localSymbols.push_back(sym);
599 }
600 }
601 }
602 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000603
Nick Kledzike34182f2013-11-06 21:36:55 +0000604 // Sort global symbol alphabetically, then add to symbol table.
605 std::sort(globals.begin(), globals.end(), AtomSorter());
606 for (AtomAndIndex &ai : globals) {
607 Symbol sym;
608 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000609 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000610 sym.scope = scopeBits(static_cast<const DefinedAtom*>(ai.atom));
611 sym.sect = ai.index;
612 sym.desc = 0;
613 sym.value = _atomToAddress[ai.atom];
614 file.globalSymbols.push_back(sym);
615 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000616
617
Nick Kledzike34182f2013-11-06 21:36:55 +0000618 // Sort undefined symbol alphabetically, then add to symbol table.
619 std::vector<AtomAndIndex> undefs;
620 undefs.reserve(128);
621 for (const UndefinedAtom *atom : atomFile.undefined()) {
622 AtomAndIndex ai = { atom, 0 };
623 undefs.push_back(ai);
624 }
625 for (const SharedLibraryAtom *atom : atomFile.sharedLibrary()) {
626 AtomAndIndex ai = { atom, 0 };
627 undefs.push_back(ai);
628 }
629 std::sort(undefs.begin(), undefs.end(), AtomSorter());
630 const uint32_t start = file.globalSymbols.size() + file.localSymbols.size();
631 for (AtomAndIndex &ai : undefs) {
632 Symbol sym;
633 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000634 sym.type = N_UNDF;
Nick Kledzike34182f2013-11-06 21:36:55 +0000635 sym.scope = N_EXT;
636 sym.sect = 0;
637 sym.desc = 0;
638 sym.value = 0;
639 _atomToSymbolIndex[ai.atom] = file.undefinedSymbols.size() + start;
640 file.undefinedSymbols.push_back(sym);
641 }
642}
643
644const Atom *Util::targetOfLazyPointer(const DefinedAtom *lpAtom) {
645 for (const Reference *ref : *lpAtom) {
Nick Kledzike5552772013-12-19 21:58:00 +0000646 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000647 return ref->target();
648 }
649 }
650 return nullptr;
651}
652
653const Atom *Util::targetOfStub(const DefinedAtom *stubAtom) {
654 for (const Reference *ref : *stubAtom) {
655 if (const Atom *ta = ref->target()) {
656 if (const DefinedAtom *lpAtom = dyn_cast<DefinedAtom>(ta)) {
657 const Atom *target = targetOfLazyPointer(lpAtom);
658 if (target)
659 return target;
660 }
661 }
662 }
663 return nullptr;
664}
665
666
667void Util::addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file) {
668 for (SectionInfo *si : _sectionInfos) {
669 Section &normSect = file.sections[si->normalizedSectionIndex];
670 switch (si->type) {
671 case llvm::MachO::S_NON_LAZY_SYMBOL_POINTERS:
672 for (const AtomInfo &info : si->atomsAndOffsets) {
673 bool foundTarget = false;
674 for (const Reference *ref : *info.atom) {
675 const Atom *target = ref->target();
676 if (target) {
677 if (isa<const SharedLibraryAtom>(target)) {
678 uint32_t index = _atomToSymbolIndex[target];
679 normSect.indirectSymbols.push_back(index);
680 foundTarget = true;
681 } else {
682 normSect.indirectSymbols.push_back(
683 llvm::MachO::INDIRECT_SYMBOL_LOCAL);
684 }
685 }
686 }
687 if (!foundTarget) {
688 normSect.indirectSymbols.push_back(
689 llvm::MachO::INDIRECT_SYMBOL_ABS);
690 }
691 }
692 break;
693 case llvm::MachO::S_LAZY_SYMBOL_POINTERS:
694 for (const AtomInfo &info : si->atomsAndOffsets) {
695 const Atom *target = targetOfLazyPointer(info.atom);
696 if (target) {
697 uint32_t index = _atomToSymbolIndex[target];
698 normSect.indirectSymbols.push_back(index);
699 }
700 }
701 break;
702 case llvm::MachO::S_SYMBOL_STUBS:
703 for (const AtomInfo &info : si->atomsAndOffsets) {
704 const Atom *target = targetOfStub(info.atom);
705 if (target) {
706 uint32_t index = _atomToSymbolIndex[target];
707 normSect.indirectSymbols.push_back(index);
708 }
709 }
710 break;
711 default:
712 break;
713 }
714 }
715
716}
717
718void Util::addDependentDylibs(const lld::File &atomFile,NormalizedFile &nFile) {
719 // Scan all imported symbols and build up list of dylibs they are from.
720 int ordinal = 1;
721 for (const SharedLibraryAtom *slAtom : atomFile.sharedLibrary()) {
722 StringRef loadPath = slAtom->loadName();
723 DylibPathToInfo::iterator pos = _dylibInfo.find(loadPath);
724 if (pos == _dylibInfo.end()) {
725 DylibInfo info;
726 info.ordinal = ordinal++;
727 info.hasWeak = slAtom->canBeNullAtRuntime();
728 info.hasNonWeak = !info.hasWeak;
729 _dylibInfo[loadPath] = info;
730 DependentDylib depInfo;
731 depInfo.path = loadPath;
732 depInfo.kind = llvm::MachO::LC_LOAD_DYLIB;
733 nFile.dependentDylibs.push_back(depInfo);
734 } else {
735 if ( slAtom->canBeNullAtRuntime() )
736 pos->second.hasWeak = true;
737 else
738 pos->second.hasNonWeak = true;
739 }
740 }
741 // Automatically weak link dylib in which all symbols are weak (canBeNull).
742 for (DependentDylib &dep : nFile.dependentDylibs) {
743 DylibInfo &info = _dylibInfo[dep.path];
744 if (info.hasWeak && !info.hasNonWeak)
745 dep.kind = llvm::MachO::LC_LOAD_WEAK_DYLIB;
746 }
747}
748
749
750int Util::dylibOrdinal(const SharedLibraryAtom *sa) {
751 return _dylibInfo[sa->loadName()].ordinal;
752}
753
754void Util::segIndexForSection(const SectionInfo *sect, uint8_t &segmentIndex,
755 uint64_t &segmentStartAddr) {
756 segmentIndex = 0;
757 for (const SegmentInfo *seg : _segmentInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000758 if ((seg->address <= sect->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000759 && (seg->address+seg->size >= sect->address+sect->size)) {
760 segmentStartAddr = seg->address;
761 return;
762 }
763 ++segmentIndex;
764 }
765 llvm_unreachable("section not in any segment");
766}
767
768
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000769void Util::appendReloc(const DefinedAtom *atom, const Reference *ref,
Nick Kledzike34182f2013-11-06 21:36:55 +0000770 Relocations &relocations) {
771 // TODO: convert Reference to normalized relocation
772}
773
774void Util::addSectionRelocs(const lld::File &, NormalizedFile &file) {
775 if (_context.outputFileType() != llvm::MachO::MH_OBJECT)
776 return;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000777
Nick Kledzike34182f2013-11-06 21:36:55 +0000778 for (SectionInfo *si : _sectionInfos) {
779 Section &normSect = file.sections[si->normalizedSectionIndex];
780 for (const AtomInfo &info : si->atomsAndOffsets) {
781 const DefinedAtom *atom = info.atom;
782 for (const Reference *ref : *atom) {
783 appendReloc(atom, ref, normSect.relocations);
784 }
785 }
786 }
787}
788
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000789void Util::addRebaseAndBindingInfo(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000790 NormalizedFile &nFile) {
791 if (_context.outputFileType() == llvm::MachO::MH_OBJECT)
792 return;
793
794 uint8_t segmentIndex;
795 uint64_t segmentStartAddr;
796 for (SectionInfo *sect : _sectionInfos) {
797 segIndexForSection(sect, segmentIndex, segmentStartAddr);
798 for (const AtomInfo &info : sect->atomsAndOffsets) {
799 const DefinedAtom *atom = info.atom;
800 for (const Reference *ref : *atom) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000801 uint64_t segmentOffset = _atomToAddress[atom] + ref->offsetInAtom()
Nick Kledzike34182f2013-11-06 21:36:55 +0000802 - segmentStartAddr;
803 const Atom* targ = ref->target();
Nick Kledzike5552772013-12-19 21:58:00 +0000804 if (_context.kindHandler().isPointer(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000805 // A pointer to a DefinedAtom requires rebasing.
806 if (dyn_cast<DefinedAtom>(targ)) {
807 RebaseLocation rebase;
808 rebase.segIndex = segmentIndex;
809 rebase.segOffset = segmentOffset;
810 rebase.kind = llvm::MachO::REBASE_TYPE_POINTER;
811 nFile.rebasingInfo.push_back(rebase);
812 }
813 // A pointer to an SharedLibraryAtom requires binding.
814 if (const SharedLibraryAtom *sa = dyn_cast<SharedLibraryAtom>(targ)) {
815 BindLocation bind;
816 bind.segIndex = segmentIndex;
817 bind.segOffset = segmentOffset;
818 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
819 bind.canBeNull = sa->canBeNullAtRuntime();
820 bind.ordinal = dylibOrdinal(sa);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000821 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000822 bind.addend = ref->addend();
823 nFile.bindingInfo.push_back(bind);
824 }
825 }
Nick Kledzike5552772013-12-19 21:58:00 +0000826 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000827 BindLocation bind;
828 bind.segIndex = segmentIndex;
829 bind.segOffset = segmentOffset;
830 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
831 bind.canBeNull = false; //sa->canBeNullAtRuntime();
832 bind.ordinal = 1;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000833 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000834 bind.addend = ref->addend();
835 nFile.lazyBindingInfo.push_back(bind);
836 }
837 }
838 }
839 }
840}
841
842uint32_t Util::fileFlags() {
843 return 0; //FIX ME
844}
845
846} // end anonymous namespace
847
848
849namespace lld {
850namespace mach_o {
851namespace normalized {
852
853/// Convert a set of Atoms into a normalized mach-o file.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000854ErrorOr<std::unique_ptr<NormalizedFile>>
855normalizedFromAtoms(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000856 const MachOLinkingContext &context) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000857 // The util object buffers info until the normalized file can be made.
Nick Kledzike34182f2013-11-06 21:36:55 +0000858 Util util(context);
859 util.assignAtomsToSections(atomFile);
860 util.organizeSections();
861 util.assignAddressesToSections();
862 util.buildAtomToAddressMap();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000863
Nick Kledzike34182f2013-11-06 21:36:55 +0000864 std::unique_ptr<NormalizedFile> f(new NormalizedFile());
865 NormalizedFile &normFile = *f.get();
866 f->arch = context.arch();
867 f->fileType = context.outputFileType();
868 f->flags = util.fileFlags();
869 util.copySegmentInfo(normFile);
870 util.copySections(normFile);
871 util.addDependentDylibs(atomFile, normFile);
872 util.addSymbols(atomFile, normFile);
873 util.addIndirectSymbols(atomFile, normFile);
874 util.addRebaseAndBindingInfo(atomFile, normFile);
875 util.addSectionRelocs(atomFile, normFile);
876 util.copyEntryPointAddress(normFile);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000877
Nick Kledzike34182f2013-11-06 21:36:55 +0000878 return std::move(f);
879}
880
881
882} // namespace normalized
883} // namespace mach_o
884} // namespace lld
885