blob: c5bd697d5353a7629472dea6ec82c6e7499dceb6 [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"
Nick Kledzikec140832014-06-10 01:50:00 +000024#include "MachONormalizedFileBinaryUtils.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000025#include "ReferenceKinds.h"
26
27#include "lld/Core/Error.h"
28#include "lld/Core/LLVM.h"
29
30#include "llvm/ADT/StringRef.h"
31#include "llvm/ADT/StringSwitch.h"
32#include "llvm/Support/Casting.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/Format.h"
36#include "llvm/Support/MachO.h"
37#include "llvm/Support/system_error.h"
38
39#include <map>
40
41using llvm::StringRef;
Nick Kledzike34182f2013-11-06 21:36:55 +000042using llvm::isa;
43using namespace llvm::MachO;
44using namespace lld::mach_o::normalized;
45using namespace lld;
46
47namespace {
48
49struct AtomInfo {
50 const DefinedAtom *atom;
51 uint64_t offsetInSection;
52};
53
54struct SectionInfo {
55 SectionInfo(StringRef seg, StringRef sect, SectionType type, uint32_t attr=0);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000056
Nick Kledzike34182f2013-11-06 21:36:55 +000057 StringRef segmentName;
58 StringRef sectionName;
59 SectionType type;
60 uint32_t attributes;
61 uint64_t address;
62 uint64_t size;
63 uint32_t alignment;
64 std::vector<AtomInfo> atomsAndOffsets;
65 uint32_t normalizedSectionIndex;
66 uint32_t finalSectionIndex;
67};
68
Shankar Easwaran3d8de472014-01-27 03:09:26 +000069SectionInfo::SectionInfo(StringRef sg, StringRef sct, SectionType t, uint32_t a)
70 : segmentName(sg), sectionName(sct), type(t), attributes(a),
71 address(0), size(0), alignment(0),
Nick Kledzike34182f2013-11-06 21:36:55 +000072 normalizedSectionIndex(0), finalSectionIndex(0) {
73}
74
75struct SegmentInfo {
76 SegmentInfo(StringRef name);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000077
Nick Kledzike34182f2013-11-06 21:36:55 +000078 StringRef name;
79 uint64_t address;
80 uint64_t size;
81 uint32_t access;
82 std::vector<SectionInfo*> sections;
83};
84
Shankar Easwaran3d8de472014-01-27 03:09:26 +000085SegmentInfo::SegmentInfo(StringRef n)
Nick Kledzike34182f2013-11-06 21:36:55 +000086 : name(n), address(0), size(0), access(0) {
87}
88
89
90class Util {
91public:
92 Util(const MachOLinkingContext &ctxt) : _context(ctxt), _entryAtom(nullptr) {}
93
94 void assignAtomsToSections(const lld::File &atomFile);
95 void organizeSections();
96 void assignAddressesToSections();
97 uint32_t fileFlags();
98 void copySegmentInfo(NormalizedFile &file);
99 void copySections(NormalizedFile &file);
100 void buildAtomToAddressMap();
101 void addSymbols(const lld::File &atomFile, NormalizedFile &file);
102 void addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file);
103 void addRebaseAndBindingInfo(const lld::File &, NormalizedFile &file);
104 void addSectionRelocs(const lld::File &, NormalizedFile &file);
105 void addDependentDylibs(const lld::File &, NormalizedFile &file);
106 void copyEntryPointAddress(NormalizedFile &file);
107
108private:
109 typedef std::map<DefinedAtom::ContentType, SectionInfo*> TypeToSection;
110 typedef llvm::DenseMap<const Atom*, uint64_t> AtomToAddress;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000111
Nick Kledzike34182f2013-11-06 21:36:55 +0000112 struct DylibInfo { int ordinal; bool hasWeak; bool hasNonWeak; };
113 typedef llvm::StringMap<DylibInfo> DylibPathToInfo;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000114
Nick Kledzike34182f2013-11-06 21:36:55 +0000115 SectionInfo *sectionForAtom(const DefinedAtom*);
Nick Kledzik936d5202014-06-11 01:30:55 +0000116 SectionInfo *getRelocatableSection(DefinedAtom::ContentType type);
117 SectionInfo *getFinalSection(DefinedAtom::ContentType type);
Nick Kledzike34182f2013-11-06 21:36:55 +0000118 void appendAtom(SectionInfo *sect, const DefinedAtom *atom);
119 SegmentInfo *segmentForName(StringRef segName);
120 void layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr);
121 void layoutSectionsInTextSegment(SegmentInfo *seg, uint64_t &addr);
122 void copySectionContent(SectionInfo *si, ContentBytes &content);
123 uint8_t scopeBits(const DefinedAtom* atom);
Nick Kledzik60855392014-06-11 00:24:16 +0000124 uint16_t descBits(const DefinedAtom* atom);
Nick Kledzike34182f2013-11-06 21:36:55 +0000125 int dylibOrdinal(const SharedLibraryAtom *sa);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000126 void segIndexForSection(const SectionInfo *sect,
Nick Kledzike34182f2013-11-06 21:36:55 +0000127 uint8_t &segmentIndex, uint64_t &segmentStartAddr);
128 const Atom *targetOfLazyPointer(const DefinedAtom *lpAtom);
129 const Atom *targetOfStub(const DefinedAtom *stubAtom);
130 bool belongsInGlobalSymbolsSection(const DefinedAtom* atom);
131 void appendSection(SectionInfo *si, NormalizedFile &file);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000132 void appendReloc(const DefinedAtom *atom, const Reference *ref,
Nick Kledzike34182f2013-11-06 21:36:55 +0000133 Relocations &relocations);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000134
Nick Kledzike34182f2013-11-06 21:36:55 +0000135 static uint64_t alignTo(uint64_t value, uint8_t align2);
136 typedef llvm::DenseMap<const Atom*, uint32_t> AtomToIndex;
137 struct AtomAndIndex { const Atom *atom; uint32_t index; };
Joey Gouly9d263e02013-12-25 19:39:08 +0000138 struct AtomSorter {
139 bool operator()(const AtomAndIndex &left, const AtomAndIndex &right);
Nick Kledzike34182f2013-11-06 21:36:55 +0000140 };
Joey Gouly9d263e02013-12-25 19:39:08 +0000141 struct SegmentSorter {
142 bool operator()(const SegmentInfo *left, const SegmentInfo *right);
Nick Kledzike34182f2013-11-06 21:36:55 +0000143 static unsigned weight(const SegmentInfo *);
144 };
Joey Gouly9d263e02013-12-25 19:39:08 +0000145 struct TextSectionSorter {
146 bool operator()(const SectionInfo *left, const SectionInfo *right);
Nick Kledzike34182f2013-11-06 21:36:55 +0000147 static unsigned weight(const SectionInfo *);
148 };
149
150 const MachOLinkingContext &_context;
151 llvm::BumpPtrAllocator _allocator;
152 std::vector<SectionInfo*> _sectionInfos;
153 std::vector<SegmentInfo*> _segmentInfos;
154 TypeToSection _sectionMap;
Nick Kledzikacfad802014-05-30 22:51:04 +0000155 std::vector<SectionInfo*> _customSections;
Nick Kledzike34182f2013-11-06 21:36:55 +0000156 AtomToAddress _atomToAddress;
157 DylibPathToInfo _dylibInfo;
158 const DefinedAtom *_entryAtom;
159 AtomToIndex _atomToSymbolIndex;
160};
161
Nick Kledzikec140832014-06-10 01:50:00 +0000162
Nick Kledzik936d5202014-06-11 01:30:55 +0000163SectionInfo *Util::getRelocatableSection(DefinedAtom::ContentType type) {
Nick Kledzikec140832014-06-10 01:50:00 +0000164 StringRef segmentName;
165 StringRef sectionName;
166 SectionType sectionType;
167 SectionAttr sectionAttrs;
168
169 // Use same table used by when parsing .o files.
170 relocatableSectionInfoForContentType(type, segmentName, sectionName,
171 sectionType, sectionAttrs);
172 // If we already have a SectionInfo with this name, re-use it.
173 // This can happen if two ContentType map to the same mach-o section.
174 for (auto sect : _sectionMap) {
175 if (sect.second->sectionName.equals(sectionName) &&
176 sect.second->segmentName.equals(segmentName)) {
177 return sect.second;
178 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000179 }
Nick Kledzikec140832014-06-10 01:50:00 +0000180 // Otherwise allocate new SectionInfo object.
Nick Kledzik936d5202014-06-11 01:30:55 +0000181 SectionInfo *sect = new (_allocator) SectionInfo(segmentName, sectionName,
182 sectionType, sectionAttrs);
183 _sectionInfos.push_back(sect);
184 _sectionMap[type] = sect;
185 return sect;
Nick Kledzikec140832014-06-10 01:50:00 +0000186}
187
188#define ENTRY(seg, sect, type, atomType) \
189 {seg, sect, type, DefinedAtom::atomType }
190
191struct MachOFinalSectionFromAtomType {
192 StringRef segmentName;
193 StringRef sectionName;
194 SectionType sectionType;
195 DefinedAtom::ContentType atomType;
196};
197
198const MachOFinalSectionFromAtomType sectsToAtomType[] = {
199 ENTRY("__TEXT", "__text", S_REGULAR, typeCode),
200 ENTRY("__TEXT", "__cstring", S_CSTRING_LITERALS, typeCString),
201 ENTRY("__TEXT", "__ustring", S_REGULAR, typeUTF16String),
202 ENTRY("__TEXT", "__const", S_REGULAR, typeConstant),
203 ENTRY("__TEXT", "__const", S_4BYTE_LITERALS, typeLiteral4),
204 ENTRY("__TEXT", "__const", S_8BYTE_LITERALS, typeLiteral8),
205 ENTRY("__TEXT", "__const", S_16BYTE_LITERALS, typeLiteral16),
206 ENTRY("__TEXT", "__stubs", S_SYMBOL_STUBS, typeStub),
207 ENTRY("__TEXT", "__stub_helper", S_REGULAR, typeStubHelper),
208 ENTRY("__TEXT", "__gcc_except_tab", S_REGULAR, typeLSDA),
209 ENTRY("__TEXT", "__eh_frame", S_COALESCED, typeCFI),
210 ENTRY("__DATA", "__data", S_REGULAR, typeData),
211 ENTRY("__DATA", "__const", S_REGULAR, typeConstData),
212 ENTRY("__DATA", "__cfstring", S_REGULAR, typeCFString),
213 ENTRY("__DATA", "__la_symbol_ptr", S_LAZY_SYMBOL_POINTERS,
214 typeLazyPointer),
215 ENTRY("__DATA", "__mod_init_func", S_MOD_INIT_FUNC_POINTERS,
216 typeInitializerPtr),
217 ENTRY("__DATA", "__mod_term_func", S_MOD_TERM_FUNC_POINTERS,
218 typeTerminatorPtr),
219 ENTRY("__DATA", "___got", S_NON_LAZY_SYMBOL_POINTERS,
220 typeGOT),
221 ENTRY("__DATA", "___bss", S_ZEROFILL, typeZeroFill)
222};
223#undef ENTRY
224
225
Nick Kledzik936d5202014-06-11 01:30:55 +0000226SectionInfo *Util::getFinalSection(DefinedAtom::ContentType atomType) {
Nick Kledzikec140832014-06-10 01:50:00 +0000227 for (const MachOFinalSectionFromAtomType *p = sectsToAtomType ;
228 p->atomType != DefinedAtom::typeUnknown; ++p) {
229 if (p->atomType != atomType)
230 continue;
231 SectionAttr sectionAttrs = 0;
232 switch (atomType) {
233 case DefinedAtom::typeCode:
234 case DefinedAtom::typeStub:
235 sectionAttrs = S_ATTR_PURE_INSTRUCTIONS;
236 break;
237 default:
238 break;
239 }
240 // If we already have a SectionInfo with this name, re-use it.
241 // This can happen if two ContentType map to the same mach-o section.
242 for (auto sect : _sectionMap) {
243 if (sect.second->sectionName.equals(p->sectionName) &&
244 sect.second->segmentName.equals(p->segmentName)) {
245 return sect.second;
246 }
247 }
248 // Otherwise allocate new SectionInfo object.
Nick Kledzik936d5202014-06-11 01:30:55 +0000249 SectionInfo *sect = new (_allocator) SectionInfo(p->segmentName,
250 p->sectionName,
251 p->sectionType,
252 sectionAttrs);
253 _sectionInfos.push_back(sect);
254 _sectionMap[atomType] = sect;
255 return sect;
Nick Kledzikec140832014-06-10 01:50:00 +0000256 }
257 llvm_unreachable("content type not yet supported");
Nick Kledzike34182f2013-11-06 21:36:55 +0000258}
259
260
261
262SectionInfo *Util::sectionForAtom(const DefinedAtom *atom) {
Nick Kledzikacfad802014-05-30 22:51:04 +0000263 if (atom->sectionChoice() == DefinedAtom::sectionBasedOnContent) {
264 // Section for this atom is derived from content type.
265 DefinedAtom::ContentType type = atom->contentType();
266 auto pos = _sectionMap.find(type);
267 if ( pos != _sectionMap.end() )
268 return pos->second;
Nick Kledzikec140832014-06-10 01:50:00 +0000269 bool rMode = (_context.outputFileType() == llvm::MachO::MH_OBJECT);
Nick Kledzik936d5202014-06-11 01:30:55 +0000270 return rMode ? getRelocatableSection(type) : getFinalSection(type);
Nick Kledzikacfad802014-05-30 22:51:04 +0000271 } else {
272 // This atom needs to be in a custom section.
273 StringRef customName = atom->customSectionName();
274 // Look to see if we have already allocated the needed custom section.
275 for(SectionInfo *sect : _customSections) {
276 const DefinedAtom *firstAtom = sect->atomsAndOffsets.front().atom;
277 if (firstAtom->customSectionName().equals(customName)) {
278 return sect;
279 }
280 }
281 // Not found, so need to create a new custom section.
282 size_t seperatorIndex = customName.find('/');
283 assert(seperatorIndex != StringRef::npos);
284 StringRef segName = customName.slice(0, seperatorIndex-1);
285 StringRef sectName = customName.drop_front(seperatorIndex);
286 SectionInfo *sect = new (_allocator) SectionInfo(segName, sectName,
287 S_REGULAR);
288 _customSections.push_back(sect);
289 return sect;
290 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000291}
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000292
Nick Kledzike34182f2013-11-06 21:36:55 +0000293
294void Util::appendAtom(SectionInfo *sect, const DefinedAtom *atom) {
295 // Figure out offset for atom in this section given alignment constraints.
296 uint64_t offset = sect->size;
297 DefinedAtom::Alignment atomAlign = atom->alignment();
298 uint64_t align2 = 1 << atomAlign.powerOf2;
299 uint64_t requiredModulus = atomAlign.modulus;
300 uint64_t currentModulus = (offset % align2);
301 if ( currentModulus != requiredModulus ) {
302 if ( requiredModulus > currentModulus )
303 offset += requiredModulus-currentModulus;
304 else
305 offset += align2+requiredModulus-currentModulus;
306 }
307 // Record max alignment of any atom in this section.
308 if ( atomAlign.powerOf2 > sect->alignment )
309 sect->alignment = atomAlign.powerOf2;
310 // Assign atom to this section with this offset.
311 AtomInfo ai = {atom, offset};
312 sect->atomsAndOffsets.push_back(ai);
313 // Update section size to include this atom.
314 sect->size = offset + atom->size();
315}
316
317void Util::assignAtomsToSections(const lld::File &atomFile) {
318 for (const DefinedAtom *atom : atomFile.defined()) {
319 appendAtom(sectionForAtom(atom), atom);
320 }
321}
322
323SegmentInfo *Util::segmentForName(StringRef segName) {
324 for (SegmentInfo *si : _segmentInfos) {
325 if ( si->name.equals(segName) )
326 return si;
327 }
328 SegmentInfo *info = new (_allocator) SegmentInfo(segName);
329 if (segName.equals("__TEXT"))
330 info->access = VM_PROT_READ | VM_PROT_EXECUTE;
331 else if (segName.equals("__DATA"))
332 info->access = VM_PROT_READ | VM_PROT_WRITE;
333 else if (segName.equals("__PAGEZERO"))
334 info->access = 0;
335 _segmentInfos.push_back(info);
336 return info;
337}
338
339unsigned Util::SegmentSorter::weight(const SegmentInfo *seg) {
340 return llvm::StringSwitch<unsigned>(seg->name)
341 .Case("__PAGEZERO", 1)
342 .Case("__TEXT", 2)
343 .Case("__DATA", 3)
344 .Default(100);
345}
346
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000347bool Util::SegmentSorter::operator()(const SegmentInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000348 const SegmentInfo *right) {
349 return (weight(left) < weight(right));
350}
351
352unsigned Util::TextSectionSorter::weight(const SectionInfo *sect) {
353 return llvm::StringSwitch<unsigned>(sect->sectionName)
354 .Case("__text", 1)
355 .Case("__stubs", 2)
356 .Case("__stub_helper", 3)
357 .Case("__const", 4)
358 .Case("__cstring", 5)
359 .Case("__unwind_info", 98)
360 .Case("__eh_frame", 99)
361 .Default(10);
362}
363
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000364bool Util::TextSectionSorter::operator()(const SectionInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000365 const SectionInfo *right) {
366 return (weight(left) < weight(right));
367}
368
369
370void Util::organizeSections() {
371 if (_context.outputFileType() == llvm::MachO::MH_OBJECT) {
372 // Leave sections ordered as normalized file specified.
373 uint32_t sectionIndex = 1;
374 for (SectionInfo *si : _sectionInfos) {
375 si->finalSectionIndex = sectionIndex++;
376 }
377 } else {
378 // Main executables, need a zero-page segment
379 if (_context.outputFileType() == llvm::MachO::MH_EXECUTE)
380 segmentForName("__PAGEZERO");
381 // Group sections into segments.
382 for (SectionInfo *si : _sectionInfos) {
383 SegmentInfo *seg = segmentForName(si->segmentName);
384 seg->sections.push_back(si);
385 }
386 // Sort segments.
387 std::sort(_segmentInfos.begin(), _segmentInfos.end(), SegmentSorter());
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000388
Nick Kledzike34182f2013-11-06 21:36:55 +0000389 // Sort sections within segments.
390 for (SegmentInfo *seg : _segmentInfos) {
391 if (seg->name.equals("__TEXT")) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000392 std::sort(seg->sections.begin(), seg->sections.end(),
Nick Kledzike34182f2013-11-06 21:36:55 +0000393 TextSectionSorter());
394 }
395 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000396
Nick Kledzike34182f2013-11-06 21:36:55 +0000397 // Record final section indexes.
398 uint32_t sectionIndex = 1;
399 for (SegmentInfo *seg : _segmentInfos) {
400 for (SectionInfo *sect : seg->sections) {
401 sect->finalSectionIndex = sectionIndex++;
402 }
403 }
404 }
405
406}
407
408uint64_t Util::alignTo(uint64_t value, uint8_t align2) {
409 return llvm::RoundUpToAlignment(value, 1 << align2);
410}
411
412
413void Util::layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr) {
414 seg->address = addr;
415 for (SectionInfo *sect : seg->sections) {
416 sect->address = alignTo(addr, sect->alignment);
417 addr += sect->size;
418 }
419 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
420}
421
422
423// __TEXT segment lays out backwards so padding is at front after load commands.
424void Util::layoutSectionsInTextSegment(SegmentInfo *seg, uint64_t &addr) {
425 seg->address = addr;
426 // Walks sections starting at end to calculate padding for start.
427 int64_t taddr = 0;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000428 for (auto it = seg->sections.rbegin(); it != seg->sections.rend(); ++it) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000429 SectionInfo *sect = *it;
430 taddr -= sect->size;
431 taddr = taddr & (0 - (1 << sect->alignment));
432 }
433 int64_t padding = taddr;
434 while (padding < 0)
435 padding += _context.pageSize();
436 // Start assigning section address starting at padded offset.
437 addr += padding;
438 for (SectionInfo *sect : seg->sections) {
439 sect->address = alignTo(addr, sect->alignment);
440 addr = sect->address + sect->size;
441 }
442 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
443}
444
445
446void Util::assignAddressesToSections() {
447 uint64_t address = 0; // FIXME
448 if (_context.outputFileType() != llvm::MachO::MH_OBJECT) {
449 for (SegmentInfo *seg : _segmentInfos) {
450 if (seg->name.equals("__PAGEZERO")) {
451 seg->size = _context.pageZeroSize();
452 address += seg->size;
453 }
454 else if (seg->name.equals("__TEXT"))
455 layoutSectionsInTextSegment(seg, address);
456 else
457 layoutSectionsInSegment(seg, address);
458 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000459 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000460 llvm::dbgs() << "assignAddressesToSections()\n";
461 for (SegmentInfo *sgi : _segmentInfos) {
462 llvm::dbgs() << " address=" << llvm::format("0x%08llX", sgi->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000463 << ", size=" << llvm::format("0x%08llX", sgi->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000464 << ", segment-name='" << sgi->name
Nick Kledzik020a49c2013-11-06 21:57:52 +0000465 << "'\n";
466 for (SectionInfo *si : sgi->sections) {
467 llvm::dbgs()<< " addr=" << llvm::format("0x%08llX", si->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000468 << ", size=" << llvm::format("0x%08llX", si->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000469 << ", section-name='" << si->sectionName
Nick Kledzik020a49c2013-11-06 21:57:52 +0000470 << "\n";
471 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000472 }
Nick Kledzik020a49c2013-11-06 21:57:52 +0000473 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000474 } else {
475 for (SectionInfo *sect : _sectionInfos) {
476 sect->address = alignTo(address, sect->alignment);
477 address = sect->address + sect->size;
478 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000479 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000480 llvm::dbgs() << "assignAddressesToSections()\n";
481 for (SectionInfo *si : _sectionInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000482 llvm::dbgs() << " section=" << si->sectionName
Nick Kledzike34182f2013-11-06 21:36:55 +0000483 << " address= " << llvm::format("0x%08X", si->address)
484 << " size= " << llvm::format("0x%08X", si->size)
Nick Kledzik020a49c2013-11-06 21:57:52 +0000485 << "\n";
486 }
487 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000488 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000489}
490
491
492void Util::copySegmentInfo(NormalizedFile &file) {
493 for (SegmentInfo *sgi : _segmentInfos) {
494 Segment seg;
495 seg.name = sgi->name;
496 seg.address = sgi->address;
497 seg.size = sgi->size;
498 seg.access = sgi->access;
499 file.segments.push_back(seg);
500 }
501}
502
503void Util::appendSection(SectionInfo *si, NormalizedFile &file) {
504 // Add new empty section to end of file.sections.
505 Section temp;
506 file.sections.push_back(std::move(temp));
507 Section* normSect = &file.sections.back();
508 // Copy fields to normalized section.
509 normSect->segmentName = si->segmentName;
510 normSect->sectionName = si->sectionName;
511 normSect->type = si->type;
512 normSect->attributes = si->attributes;
513 normSect->address = si->address;
514 normSect->alignment = si->alignment;
515 // Record where normalized section is.
516 si->normalizedSectionIndex = file.sections.size()-1;
517 // Copy content from atoms to content buffer for section.
Nick Kledzik61fdef62014-05-15 20:59:23 +0000518 if (si->type == llvm::MachO::S_ZEROFILL)
519 return;
Nick Kledzik6edd7222014-01-11 01:07:43 +0000520 uint8_t *sectionContent = file.ownedAllocations.Allocate<uint8_t>(si->size);
521 normSect->content = llvm::makeArrayRef(sectionContent, si->size);
Nick Kledzike34182f2013-11-06 21:36:55 +0000522 for (AtomInfo &ai : si->atomsAndOffsets) {
523 // Copy raw bytes.
524 uint8_t *atomContent = reinterpret_cast<uint8_t*>
525 (&sectionContent[ai.offsetInSection]);
526 memcpy(atomContent, ai.atom->rawContent().data(), ai.atom->size());
527 // Apply fix-ups.
528 for (const Reference *ref : *ai.atom) {
529 uint32_t offset = ref->offsetInAtom();
530 uint64_t targetAddress = 0;
531 if ( ref->target() != nullptr )
532 targetAddress = _atomToAddress[ref->target()];
533 uint64_t fixupAddress = _atomToAddress[ai.atom] + offset;
Rui Ueyama170a1a82013-12-20 07:48:29 +0000534 _context.kindHandler().applyFixup(
535 ref->kindNamespace(), ref->kindArch(), ref->kindValue(),
536 ref->addend(), &atomContent[offset], fixupAddress, targetAddress);
Nick Kledzike34182f2013-11-06 21:36:55 +0000537 }
538 }
539}
540
541void Util::copySections(NormalizedFile &file) {
542 file.sections.reserve(_sectionInfos.size());
543 // For final linked images, write sections grouped by segment.
544 if (_context.outputFileType() != llvm::MachO::MH_OBJECT) {
545 for (SegmentInfo *sgi : _segmentInfos) {
546 for (SectionInfo *si : sgi->sections) {
547 appendSection(si, file);
548 }
549 }
550 } else {
551 // Object files write sections in default order.
552 for (SectionInfo *si : _sectionInfos) {
553 appendSection(si, file);
554 }
555 }
556}
557
558void Util::copyEntryPointAddress(NormalizedFile &nFile) {
559 if (_context.outputTypeHasEntry()) {
560 nFile.entryAddress = _atomToAddress[_entryAtom];
561 }
562}
563
564void Util::buildAtomToAddressMap() {
565 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
566 << "assign atom addresses:\n");
567 const bool lookForEntry = _context.outputTypeHasEntry();
568 for (SectionInfo *sect : _sectionInfos) {
569 for (const AtomInfo &info : sect->atomsAndOffsets) {
570 _atomToAddress[info.atom] = sect->address + info.offsetInSection;
571 if (lookForEntry && (info.atom->contentType() == DefinedAtom::typeCode) &&
572 (info.atom->size() != 0) &&
573 info.atom->name() == _context.entrySymbolName()) {
574 _entryAtom = info.atom;
575 }
576 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
577 << " address="
578 << llvm::format("0x%016X", _atomToAddress[info.atom])
579 << " atom=" << info.atom
580 << " name=" << info.atom->name() << "\n");
581 }
582 }
583}
584
585uint8_t Util::scopeBits(const DefinedAtom* atom) {
586 switch (atom->scope()) {
587 case Atom::scopeTranslationUnit:
588 return 0;
589 case Atom::scopeLinkageUnit:
590 return N_PEXT | N_EXT;
591 case Atom::scopeGlobal:
592 return N_EXT;
593 }
Nick Kledzik020fa7f2013-11-06 22:18:09 +0000594 llvm_unreachable("Unknown scope");
Nick Kledzike34182f2013-11-06 21:36:55 +0000595}
596
Nick Kledzik60855392014-06-11 00:24:16 +0000597uint16_t Util::descBits(const DefinedAtom* atom) {
598 uint16_t desc = 0;
599 switch (atom->merge()) {
600 case lld::DefinedAtom::mergeNo:
601 case lld::DefinedAtom::mergeAsTentative:
602 break;
603 case lld::DefinedAtom::mergeAsWeak:
604 case lld::DefinedAtom::mergeAsWeakAndAddressUsed:
605 desc |= N_WEAK_DEF;
606 break;
607 case lld::DefinedAtom::mergeSameNameAndSize:
608 case lld::DefinedAtom::mergeByLargestSection:
609 case lld::DefinedAtom::mergeByContent:
610 llvm_unreachable("Unsupported DefinedAtom::merge()");
611 break;
612 }
613 if (atom->contentType() == lld::DefinedAtom::typeResolver)
614 desc |= N_SYMBOL_RESOLVER;
615 return desc;
616}
617
618
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000619bool Util::AtomSorter::operator()(const AtomAndIndex &left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000620 const AtomAndIndex &right) {
621 return (left.atom->name().compare(right.atom->name()) < 0);
622}
623
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000624
Nick Kledzike34182f2013-11-06 21:36:55 +0000625bool Util::belongsInGlobalSymbolsSection(const DefinedAtom* atom) {
Nick Kledzik936d5202014-06-11 01:30:55 +0000626 // ScopeLinkageUnit symbols are in globals area of symbol table
627 // in object files, but in locals area for final linked images.
628 if (_context.outputFileType() == llvm::MachO::MH_OBJECT)
629 return (atom->scope() != Atom::scopeTranslationUnit);
630 else
631 return (atom->scope() == Atom::scopeGlobal);
Nick Kledzike34182f2013-11-06 21:36:55 +0000632}
633
634void Util::addSymbols(const lld::File &atomFile, NormalizedFile &file) {
635 // Mach-O symbol table has three regions: locals, globals, undefs.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000636
Nick Kledzike34182f2013-11-06 21:36:55 +0000637 // Add all local (non-global) symbols in address order
638 std::vector<AtomAndIndex> globals;
639 globals.reserve(512);
640 for (SectionInfo *sect : _sectionInfos) {
641 for (const AtomInfo &info : sect->atomsAndOffsets) {
642 const DefinedAtom *atom = info.atom;
643 if (!atom->name().empty()) {
644 if (belongsInGlobalSymbolsSection(atom)) {
645 AtomAndIndex ai = { atom, sect->finalSectionIndex };
646 globals.push_back(ai);
647 } else {
648 Symbol sym;
649 sym.name = atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000650 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000651 sym.scope = scopeBits(atom);
652 sym.sect = sect->finalSectionIndex;
653 sym.desc = 0;
654 sym.value = _atomToAddress[atom];
655 file.localSymbols.push_back(sym);
656 }
657 }
658 }
659 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000660
Nick Kledzike34182f2013-11-06 21:36:55 +0000661 // Sort global symbol alphabetically, then add to symbol table.
662 std::sort(globals.begin(), globals.end(), AtomSorter());
663 for (AtomAndIndex &ai : globals) {
664 Symbol sym;
665 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000666 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000667 sym.scope = scopeBits(static_cast<const DefinedAtom*>(ai.atom));
668 sym.sect = ai.index;
Nick Kledzik60855392014-06-11 00:24:16 +0000669 sym.desc = descBits(static_cast<const DefinedAtom*>(ai.atom));
Nick Kledzike34182f2013-11-06 21:36:55 +0000670 sym.value = _atomToAddress[ai.atom];
671 file.globalSymbols.push_back(sym);
672 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000673
674
Nick Kledzike34182f2013-11-06 21:36:55 +0000675 // Sort undefined symbol alphabetically, then add to symbol table.
676 std::vector<AtomAndIndex> undefs;
677 undefs.reserve(128);
678 for (const UndefinedAtom *atom : atomFile.undefined()) {
679 AtomAndIndex ai = { atom, 0 };
680 undefs.push_back(ai);
681 }
682 for (const SharedLibraryAtom *atom : atomFile.sharedLibrary()) {
683 AtomAndIndex ai = { atom, 0 };
684 undefs.push_back(ai);
685 }
686 std::sort(undefs.begin(), undefs.end(), AtomSorter());
687 const uint32_t start = file.globalSymbols.size() + file.localSymbols.size();
688 for (AtomAndIndex &ai : undefs) {
689 Symbol sym;
690 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000691 sym.type = N_UNDF;
Nick Kledzike34182f2013-11-06 21:36:55 +0000692 sym.scope = N_EXT;
693 sym.sect = 0;
694 sym.desc = 0;
695 sym.value = 0;
696 _atomToSymbolIndex[ai.atom] = file.undefinedSymbols.size() + start;
697 file.undefinedSymbols.push_back(sym);
698 }
699}
700
701const Atom *Util::targetOfLazyPointer(const DefinedAtom *lpAtom) {
702 for (const Reference *ref : *lpAtom) {
Nick Kledzike5552772013-12-19 21:58:00 +0000703 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000704 return ref->target();
705 }
706 }
707 return nullptr;
708}
709
710const Atom *Util::targetOfStub(const DefinedAtom *stubAtom) {
711 for (const Reference *ref : *stubAtom) {
712 if (const Atom *ta = ref->target()) {
713 if (const DefinedAtom *lpAtom = dyn_cast<DefinedAtom>(ta)) {
714 const Atom *target = targetOfLazyPointer(lpAtom);
715 if (target)
716 return target;
717 }
718 }
719 }
720 return nullptr;
721}
722
723
724void Util::addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file) {
725 for (SectionInfo *si : _sectionInfos) {
726 Section &normSect = file.sections[si->normalizedSectionIndex];
727 switch (si->type) {
728 case llvm::MachO::S_NON_LAZY_SYMBOL_POINTERS:
729 for (const AtomInfo &info : si->atomsAndOffsets) {
730 bool foundTarget = false;
731 for (const Reference *ref : *info.atom) {
732 const Atom *target = ref->target();
733 if (target) {
734 if (isa<const SharedLibraryAtom>(target)) {
735 uint32_t index = _atomToSymbolIndex[target];
736 normSect.indirectSymbols.push_back(index);
737 foundTarget = true;
738 } else {
739 normSect.indirectSymbols.push_back(
740 llvm::MachO::INDIRECT_SYMBOL_LOCAL);
741 }
742 }
743 }
744 if (!foundTarget) {
745 normSect.indirectSymbols.push_back(
746 llvm::MachO::INDIRECT_SYMBOL_ABS);
747 }
748 }
749 break;
750 case llvm::MachO::S_LAZY_SYMBOL_POINTERS:
751 for (const AtomInfo &info : si->atomsAndOffsets) {
752 const Atom *target = targetOfLazyPointer(info.atom);
753 if (target) {
754 uint32_t index = _atomToSymbolIndex[target];
755 normSect.indirectSymbols.push_back(index);
756 }
757 }
758 break;
759 case llvm::MachO::S_SYMBOL_STUBS:
760 for (const AtomInfo &info : si->atomsAndOffsets) {
761 const Atom *target = targetOfStub(info.atom);
762 if (target) {
763 uint32_t index = _atomToSymbolIndex[target];
764 normSect.indirectSymbols.push_back(index);
765 }
766 }
767 break;
768 default:
769 break;
770 }
771 }
772
773}
774
775void Util::addDependentDylibs(const lld::File &atomFile,NormalizedFile &nFile) {
776 // Scan all imported symbols and build up list of dylibs they are from.
777 int ordinal = 1;
778 for (const SharedLibraryAtom *slAtom : atomFile.sharedLibrary()) {
779 StringRef loadPath = slAtom->loadName();
780 DylibPathToInfo::iterator pos = _dylibInfo.find(loadPath);
781 if (pos == _dylibInfo.end()) {
782 DylibInfo info;
783 info.ordinal = ordinal++;
784 info.hasWeak = slAtom->canBeNullAtRuntime();
785 info.hasNonWeak = !info.hasWeak;
786 _dylibInfo[loadPath] = info;
787 DependentDylib depInfo;
788 depInfo.path = loadPath;
789 depInfo.kind = llvm::MachO::LC_LOAD_DYLIB;
790 nFile.dependentDylibs.push_back(depInfo);
791 } else {
792 if ( slAtom->canBeNullAtRuntime() )
793 pos->second.hasWeak = true;
794 else
795 pos->second.hasNonWeak = true;
796 }
797 }
798 // Automatically weak link dylib in which all symbols are weak (canBeNull).
799 for (DependentDylib &dep : nFile.dependentDylibs) {
800 DylibInfo &info = _dylibInfo[dep.path];
801 if (info.hasWeak && !info.hasNonWeak)
802 dep.kind = llvm::MachO::LC_LOAD_WEAK_DYLIB;
803 }
804}
805
806
807int Util::dylibOrdinal(const SharedLibraryAtom *sa) {
808 return _dylibInfo[sa->loadName()].ordinal;
809}
810
811void Util::segIndexForSection(const SectionInfo *sect, uint8_t &segmentIndex,
812 uint64_t &segmentStartAddr) {
813 segmentIndex = 0;
814 for (const SegmentInfo *seg : _segmentInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000815 if ((seg->address <= sect->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000816 && (seg->address+seg->size >= sect->address+sect->size)) {
817 segmentStartAddr = seg->address;
818 return;
819 }
820 ++segmentIndex;
821 }
822 llvm_unreachable("section not in any segment");
823}
824
825
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000826void Util::appendReloc(const DefinedAtom *atom, const Reference *ref,
Nick Kledzike34182f2013-11-06 21:36:55 +0000827 Relocations &relocations) {
828 // TODO: convert Reference to normalized relocation
829}
830
831void Util::addSectionRelocs(const lld::File &, NormalizedFile &file) {
832 if (_context.outputFileType() != llvm::MachO::MH_OBJECT)
833 return;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000834
Nick Kledzike34182f2013-11-06 21:36:55 +0000835 for (SectionInfo *si : _sectionInfos) {
836 Section &normSect = file.sections[si->normalizedSectionIndex];
837 for (const AtomInfo &info : si->atomsAndOffsets) {
838 const DefinedAtom *atom = info.atom;
839 for (const Reference *ref : *atom) {
840 appendReloc(atom, ref, normSect.relocations);
841 }
842 }
843 }
844}
845
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000846void Util::addRebaseAndBindingInfo(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000847 NormalizedFile &nFile) {
848 if (_context.outputFileType() == llvm::MachO::MH_OBJECT)
849 return;
850
851 uint8_t segmentIndex;
852 uint64_t segmentStartAddr;
853 for (SectionInfo *sect : _sectionInfos) {
854 segIndexForSection(sect, segmentIndex, segmentStartAddr);
855 for (const AtomInfo &info : sect->atomsAndOffsets) {
856 const DefinedAtom *atom = info.atom;
857 for (const Reference *ref : *atom) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000858 uint64_t segmentOffset = _atomToAddress[atom] + ref->offsetInAtom()
Nick Kledzike34182f2013-11-06 21:36:55 +0000859 - segmentStartAddr;
860 const Atom* targ = ref->target();
Nick Kledzike5552772013-12-19 21:58:00 +0000861 if (_context.kindHandler().isPointer(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000862 // A pointer to a DefinedAtom requires rebasing.
863 if (dyn_cast<DefinedAtom>(targ)) {
864 RebaseLocation rebase;
865 rebase.segIndex = segmentIndex;
866 rebase.segOffset = segmentOffset;
867 rebase.kind = llvm::MachO::REBASE_TYPE_POINTER;
868 nFile.rebasingInfo.push_back(rebase);
869 }
870 // A pointer to an SharedLibraryAtom requires binding.
871 if (const SharedLibraryAtom *sa = dyn_cast<SharedLibraryAtom>(targ)) {
872 BindLocation bind;
873 bind.segIndex = segmentIndex;
874 bind.segOffset = segmentOffset;
875 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
876 bind.canBeNull = sa->canBeNullAtRuntime();
877 bind.ordinal = dylibOrdinal(sa);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000878 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000879 bind.addend = ref->addend();
880 nFile.bindingInfo.push_back(bind);
881 }
882 }
Nick Kledzike5552772013-12-19 21:58:00 +0000883 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000884 BindLocation bind;
885 bind.segIndex = segmentIndex;
886 bind.segOffset = segmentOffset;
887 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
888 bind.canBeNull = false; //sa->canBeNullAtRuntime();
889 bind.ordinal = 1;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000890 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000891 bind.addend = ref->addend();
892 nFile.lazyBindingInfo.push_back(bind);
893 }
894 }
895 }
896 }
897}
898
899uint32_t Util::fileFlags() {
900 return 0; //FIX ME
901}
902
903} // end anonymous namespace
904
905
906namespace lld {
907namespace mach_o {
908namespace normalized {
909
910/// Convert a set of Atoms into a normalized mach-o file.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000911ErrorOr<std::unique_ptr<NormalizedFile>>
912normalizedFromAtoms(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000913 const MachOLinkingContext &context) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000914 // The util object buffers info until the normalized file can be made.
Nick Kledzike34182f2013-11-06 21:36:55 +0000915 Util util(context);
916 util.assignAtomsToSections(atomFile);
917 util.organizeSections();
918 util.assignAddressesToSections();
919 util.buildAtomToAddressMap();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000920
Nick Kledzike34182f2013-11-06 21:36:55 +0000921 std::unique_ptr<NormalizedFile> f(new NormalizedFile());
922 NormalizedFile &normFile = *f.get();
923 f->arch = context.arch();
924 f->fileType = context.outputFileType();
925 f->flags = util.fileFlags();
926 util.copySegmentInfo(normFile);
927 util.copySections(normFile);
928 util.addDependentDylibs(atomFile, normFile);
929 util.addSymbols(atomFile, normFile);
930 util.addIndirectSymbols(atomFile, normFile);
931 util.addRebaseAndBindingInfo(atomFile, normFile);
932 util.addSectionRelocs(atomFile, normFile);
933 util.copyEntryPointAddress(normFile);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000934
Nick Kledzike34182f2013-11-06 21:36:55 +0000935 return std::move(f);
936}
937
938
939} // namespace normalized
940} // namespace mach_o
941} // namespace lld
942