blob: ab00cb52ead1d490a93a5faf67f1f87896e4772a [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 Kledzikec140832014-06-10 01:50:00 +0000116 SectionInfo *makeRelocatableSection(DefinedAtom::ContentType type);
117 SectionInfo *makeFinalSection(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
163SectionInfo *Util::makeRelocatableSection(DefinedAtom::ContentType type) {
164 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.
181 return new (_allocator) SectionInfo(segmentName, sectionName, sectionType,
182 sectionAttrs);
183}
184
185#define ENTRY(seg, sect, type, atomType) \
186 {seg, sect, type, DefinedAtom::atomType }
187
188struct MachOFinalSectionFromAtomType {
189 StringRef segmentName;
190 StringRef sectionName;
191 SectionType sectionType;
192 DefinedAtom::ContentType atomType;
193};
194
195const MachOFinalSectionFromAtomType sectsToAtomType[] = {
196 ENTRY("__TEXT", "__text", S_REGULAR, typeCode),
197 ENTRY("__TEXT", "__cstring", S_CSTRING_LITERALS, typeCString),
198 ENTRY("__TEXT", "__ustring", S_REGULAR, typeUTF16String),
199 ENTRY("__TEXT", "__const", S_REGULAR, typeConstant),
200 ENTRY("__TEXT", "__const", S_4BYTE_LITERALS, typeLiteral4),
201 ENTRY("__TEXT", "__const", S_8BYTE_LITERALS, typeLiteral8),
202 ENTRY("__TEXT", "__const", S_16BYTE_LITERALS, typeLiteral16),
203 ENTRY("__TEXT", "__stubs", S_SYMBOL_STUBS, typeStub),
204 ENTRY("__TEXT", "__stub_helper", S_REGULAR, typeStubHelper),
205 ENTRY("__TEXT", "__gcc_except_tab", S_REGULAR, typeLSDA),
206 ENTRY("__TEXT", "__eh_frame", S_COALESCED, typeCFI),
207 ENTRY("__DATA", "__data", S_REGULAR, typeData),
208 ENTRY("__DATA", "__const", S_REGULAR, typeConstData),
209 ENTRY("__DATA", "__cfstring", S_REGULAR, typeCFString),
210 ENTRY("__DATA", "__la_symbol_ptr", S_LAZY_SYMBOL_POINTERS,
211 typeLazyPointer),
212 ENTRY("__DATA", "__mod_init_func", S_MOD_INIT_FUNC_POINTERS,
213 typeInitializerPtr),
214 ENTRY("__DATA", "__mod_term_func", S_MOD_TERM_FUNC_POINTERS,
215 typeTerminatorPtr),
216 ENTRY("__DATA", "___got", S_NON_LAZY_SYMBOL_POINTERS,
217 typeGOT),
218 ENTRY("__DATA", "___bss", S_ZEROFILL, typeZeroFill)
219};
220#undef ENTRY
221
222
223SectionInfo *Util::makeFinalSection(DefinedAtom::ContentType atomType) {
224 for (const MachOFinalSectionFromAtomType *p = sectsToAtomType ;
225 p->atomType != DefinedAtom::typeUnknown; ++p) {
226 if (p->atomType != atomType)
227 continue;
228 SectionAttr sectionAttrs = 0;
229 switch (atomType) {
230 case DefinedAtom::typeCode:
231 case DefinedAtom::typeStub:
232 sectionAttrs = S_ATTR_PURE_INSTRUCTIONS;
233 break;
234 default:
235 break;
236 }
237 // If we already have a SectionInfo with this name, re-use it.
238 // This can happen if two ContentType map to the same mach-o section.
239 for (auto sect : _sectionMap) {
240 if (sect.second->sectionName.equals(p->sectionName) &&
241 sect.second->segmentName.equals(p->segmentName)) {
242 return sect.second;
243 }
244 }
245 // Otherwise allocate new SectionInfo object.
246 return new (_allocator) SectionInfo(p->segmentName, p->sectionName,
247 p->sectionType, sectionAttrs);
248 }
249 llvm_unreachable("content type not yet supported");
Nick Kledzike34182f2013-11-06 21:36:55 +0000250}
251
252
253
254SectionInfo *Util::sectionForAtom(const DefinedAtom *atom) {
Nick Kledzikacfad802014-05-30 22:51:04 +0000255 if (atom->sectionChoice() == DefinedAtom::sectionBasedOnContent) {
256 // Section for this atom is derived from content type.
257 DefinedAtom::ContentType type = atom->contentType();
258 auto pos = _sectionMap.find(type);
259 if ( pos != _sectionMap.end() )
260 return pos->second;
Nick Kledzikec140832014-06-10 01:50:00 +0000261 bool rMode = (_context.outputFileType() == llvm::MachO::MH_OBJECT);
262 SectionInfo *si = rMode ? makeRelocatableSection(type)
263 : makeFinalSection(type);
Nick Kledzikacfad802014-05-30 22:51:04 +0000264 _sectionInfos.push_back(si);
265 _sectionMap[type] = si;
266 return si;
267 } else {
268 // This atom needs to be in a custom section.
269 StringRef customName = atom->customSectionName();
270 // Look to see if we have already allocated the needed custom section.
271 for(SectionInfo *sect : _customSections) {
272 const DefinedAtom *firstAtom = sect->atomsAndOffsets.front().atom;
273 if (firstAtom->customSectionName().equals(customName)) {
274 return sect;
275 }
276 }
277 // Not found, so need to create a new custom section.
278 size_t seperatorIndex = customName.find('/');
279 assert(seperatorIndex != StringRef::npos);
280 StringRef segName = customName.slice(0, seperatorIndex-1);
281 StringRef sectName = customName.drop_front(seperatorIndex);
282 SectionInfo *sect = new (_allocator) SectionInfo(segName, sectName,
283 S_REGULAR);
284 _customSections.push_back(sect);
285 return sect;
286 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000287}
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000288
Nick Kledzike34182f2013-11-06 21:36:55 +0000289
290void Util::appendAtom(SectionInfo *sect, const DefinedAtom *atom) {
291 // Figure out offset for atom in this section given alignment constraints.
292 uint64_t offset = sect->size;
293 DefinedAtom::Alignment atomAlign = atom->alignment();
294 uint64_t align2 = 1 << atomAlign.powerOf2;
295 uint64_t requiredModulus = atomAlign.modulus;
296 uint64_t currentModulus = (offset % align2);
297 if ( currentModulus != requiredModulus ) {
298 if ( requiredModulus > currentModulus )
299 offset += requiredModulus-currentModulus;
300 else
301 offset += align2+requiredModulus-currentModulus;
302 }
303 // Record max alignment of any atom in this section.
304 if ( atomAlign.powerOf2 > sect->alignment )
305 sect->alignment = atomAlign.powerOf2;
306 // Assign atom to this section with this offset.
307 AtomInfo ai = {atom, offset};
308 sect->atomsAndOffsets.push_back(ai);
309 // Update section size to include this atom.
310 sect->size = offset + atom->size();
311}
312
313void Util::assignAtomsToSections(const lld::File &atomFile) {
314 for (const DefinedAtom *atom : atomFile.defined()) {
315 appendAtom(sectionForAtom(atom), atom);
316 }
317}
318
319SegmentInfo *Util::segmentForName(StringRef segName) {
320 for (SegmentInfo *si : _segmentInfos) {
321 if ( si->name.equals(segName) )
322 return si;
323 }
324 SegmentInfo *info = new (_allocator) SegmentInfo(segName);
325 if (segName.equals("__TEXT"))
326 info->access = VM_PROT_READ | VM_PROT_EXECUTE;
327 else if (segName.equals("__DATA"))
328 info->access = VM_PROT_READ | VM_PROT_WRITE;
329 else if (segName.equals("__PAGEZERO"))
330 info->access = 0;
331 _segmentInfos.push_back(info);
332 return info;
333}
334
335unsigned Util::SegmentSorter::weight(const SegmentInfo *seg) {
336 return llvm::StringSwitch<unsigned>(seg->name)
337 .Case("__PAGEZERO", 1)
338 .Case("__TEXT", 2)
339 .Case("__DATA", 3)
340 .Default(100);
341}
342
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000343bool Util::SegmentSorter::operator()(const SegmentInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000344 const SegmentInfo *right) {
345 return (weight(left) < weight(right));
346}
347
348unsigned Util::TextSectionSorter::weight(const SectionInfo *sect) {
349 return llvm::StringSwitch<unsigned>(sect->sectionName)
350 .Case("__text", 1)
351 .Case("__stubs", 2)
352 .Case("__stub_helper", 3)
353 .Case("__const", 4)
354 .Case("__cstring", 5)
355 .Case("__unwind_info", 98)
356 .Case("__eh_frame", 99)
357 .Default(10);
358}
359
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000360bool Util::TextSectionSorter::operator()(const SectionInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000361 const SectionInfo *right) {
362 return (weight(left) < weight(right));
363}
364
365
366void Util::organizeSections() {
367 if (_context.outputFileType() == llvm::MachO::MH_OBJECT) {
368 // Leave sections ordered as normalized file specified.
369 uint32_t sectionIndex = 1;
370 for (SectionInfo *si : _sectionInfos) {
371 si->finalSectionIndex = sectionIndex++;
372 }
373 } else {
374 // Main executables, need a zero-page segment
375 if (_context.outputFileType() == llvm::MachO::MH_EXECUTE)
376 segmentForName("__PAGEZERO");
377 // Group sections into segments.
378 for (SectionInfo *si : _sectionInfos) {
379 SegmentInfo *seg = segmentForName(si->segmentName);
380 seg->sections.push_back(si);
381 }
382 // Sort segments.
383 std::sort(_segmentInfos.begin(), _segmentInfos.end(), SegmentSorter());
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000384
Nick Kledzike34182f2013-11-06 21:36:55 +0000385 // Sort sections within segments.
386 for (SegmentInfo *seg : _segmentInfos) {
387 if (seg->name.equals("__TEXT")) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000388 std::sort(seg->sections.begin(), seg->sections.end(),
Nick Kledzike34182f2013-11-06 21:36:55 +0000389 TextSectionSorter());
390 }
391 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000392
Nick Kledzike34182f2013-11-06 21:36:55 +0000393 // Record final section indexes.
394 uint32_t sectionIndex = 1;
395 for (SegmentInfo *seg : _segmentInfos) {
396 for (SectionInfo *sect : seg->sections) {
397 sect->finalSectionIndex = sectionIndex++;
398 }
399 }
400 }
401
402}
403
404uint64_t Util::alignTo(uint64_t value, uint8_t align2) {
405 return llvm::RoundUpToAlignment(value, 1 << align2);
406}
407
408
409void Util::layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr) {
410 seg->address = addr;
411 for (SectionInfo *sect : seg->sections) {
412 sect->address = alignTo(addr, sect->alignment);
413 addr += sect->size;
414 }
415 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
416}
417
418
419// __TEXT segment lays out backwards so padding is at front after load commands.
420void Util::layoutSectionsInTextSegment(SegmentInfo *seg, uint64_t &addr) {
421 seg->address = addr;
422 // Walks sections starting at end to calculate padding for start.
423 int64_t taddr = 0;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000424 for (auto it = seg->sections.rbegin(); it != seg->sections.rend(); ++it) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000425 SectionInfo *sect = *it;
426 taddr -= sect->size;
427 taddr = taddr & (0 - (1 << sect->alignment));
428 }
429 int64_t padding = taddr;
430 while (padding < 0)
431 padding += _context.pageSize();
432 // Start assigning section address starting at padded offset.
433 addr += padding;
434 for (SectionInfo *sect : seg->sections) {
435 sect->address = alignTo(addr, sect->alignment);
436 addr = sect->address + sect->size;
437 }
438 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
439}
440
441
442void Util::assignAddressesToSections() {
443 uint64_t address = 0; // FIXME
444 if (_context.outputFileType() != llvm::MachO::MH_OBJECT) {
445 for (SegmentInfo *seg : _segmentInfos) {
446 if (seg->name.equals("__PAGEZERO")) {
447 seg->size = _context.pageZeroSize();
448 address += seg->size;
449 }
450 else if (seg->name.equals("__TEXT"))
451 layoutSectionsInTextSegment(seg, address);
452 else
453 layoutSectionsInSegment(seg, address);
454 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000455 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000456 llvm::dbgs() << "assignAddressesToSections()\n";
457 for (SegmentInfo *sgi : _segmentInfos) {
458 llvm::dbgs() << " address=" << llvm::format("0x%08llX", sgi->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000459 << ", size=" << llvm::format("0x%08llX", sgi->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000460 << ", segment-name='" << sgi->name
Nick Kledzik020a49c2013-11-06 21:57:52 +0000461 << "'\n";
462 for (SectionInfo *si : sgi->sections) {
463 llvm::dbgs()<< " addr=" << llvm::format("0x%08llX", si->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000464 << ", size=" << llvm::format("0x%08llX", si->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000465 << ", section-name='" << si->sectionName
Nick Kledzik020a49c2013-11-06 21:57:52 +0000466 << "\n";
467 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000468 }
Nick Kledzik020a49c2013-11-06 21:57:52 +0000469 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000470 } else {
471 for (SectionInfo *sect : _sectionInfos) {
472 sect->address = alignTo(address, sect->alignment);
473 address = sect->address + sect->size;
474 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000475 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000476 llvm::dbgs() << "assignAddressesToSections()\n";
477 for (SectionInfo *si : _sectionInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000478 llvm::dbgs() << " section=" << si->sectionName
Nick Kledzike34182f2013-11-06 21:36:55 +0000479 << " address= " << llvm::format("0x%08X", si->address)
480 << " size= " << llvm::format("0x%08X", si->size)
Nick Kledzik020a49c2013-11-06 21:57:52 +0000481 << "\n";
482 }
483 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000484 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000485}
486
487
488void Util::copySegmentInfo(NormalizedFile &file) {
489 for (SegmentInfo *sgi : _segmentInfos) {
490 Segment seg;
491 seg.name = sgi->name;
492 seg.address = sgi->address;
493 seg.size = sgi->size;
494 seg.access = sgi->access;
495 file.segments.push_back(seg);
496 }
497}
498
499void Util::appendSection(SectionInfo *si, NormalizedFile &file) {
500 // Add new empty section to end of file.sections.
501 Section temp;
502 file.sections.push_back(std::move(temp));
503 Section* normSect = &file.sections.back();
504 // Copy fields to normalized section.
505 normSect->segmentName = si->segmentName;
506 normSect->sectionName = si->sectionName;
507 normSect->type = si->type;
508 normSect->attributes = si->attributes;
509 normSect->address = si->address;
510 normSect->alignment = si->alignment;
511 // Record where normalized section is.
512 si->normalizedSectionIndex = file.sections.size()-1;
513 // Copy content from atoms to content buffer for section.
Nick Kledzik61fdef62014-05-15 20:59:23 +0000514 if (si->type == llvm::MachO::S_ZEROFILL)
515 return;
Nick Kledzik6edd7222014-01-11 01:07:43 +0000516 uint8_t *sectionContent = file.ownedAllocations.Allocate<uint8_t>(si->size);
517 normSect->content = llvm::makeArrayRef(sectionContent, si->size);
Nick Kledzike34182f2013-11-06 21:36:55 +0000518 for (AtomInfo &ai : si->atomsAndOffsets) {
519 // Copy raw bytes.
520 uint8_t *atomContent = reinterpret_cast<uint8_t*>
521 (&sectionContent[ai.offsetInSection]);
522 memcpy(atomContent, ai.atom->rawContent().data(), ai.atom->size());
523 // Apply fix-ups.
524 for (const Reference *ref : *ai.atom) {
525 uint32_t offset = ref->offsetInAtom();
526 uint64_t targetAddress = 0;
527 if ( ref->target() != nullptr )
528 targetAddress = _atomToAddress[ref->target()];
529 uint64_t fixupAddress = _atomToAddress[ai.atom] + offset;
Rui Ueyama170a1a82013-12-20 07:48:29 +0000530 _context.kindHandler().applyFixup(
531 ref->kindNamespace(), ref->kindArch(), ref->kindValue(),
532 ref->addend(), &atomContent[offset], fixupAddress, targetAddress);
Nick Kledzike34182f2013-11-06 21:36:55 +0000533 }
534 }
535}
536
537void Util::copySections(NormalizedFile &file) {
538 file.sections.reserve(_sectionInfos.size());
539 // For final linked images, write sections grouped by segment.
540 if (_context.outputFileType() != llvm::MachO::MH_OBJECT) {
541 for (SegmentInfo *sgi : _segmentInfos) {
542 for (SectionInfo *si : sgi->sections) {
543 appendSection(si, file);
544 }
545 }
546 } else {
547 // Object files write sections in default order.
548 for (SectionInfo *si : _sectionInfos) {
549 appendSection(si, file);
550 }
551 }
552}
553
554void Util::copyEntryPointAddress(NormalizedFile &nFile) {
555 if (_context.outputTypeHasEntry()) {
556 nFile.entryAddress = _atomToAddress[_entryAtom];
557 }
558}
559
560void Util::buildAtomToAddressMap() {
561 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
562 << "assign atom addresses:\n");
563 const bool lookForEntry = _context.outputTypeHasEntry();
564 for (SectionInfo *sect : _sectionInfos) {
565 for (const AtomInfo &info : sect->atomsAndOffsets) {
566 _atomToAddress[info.atom] = sect->address + info.offsetInSection;
567 if (lookForEntry && (info.atom->contentType() == DefinedAtom::typeCode) &&
568 (info.atom->size() != 0) &&
569 info.atom->name() == _context.entrySymbolName()) {
570 _entryAtom = info.atom;
571 }
572 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
573 << " address="
574 << llvm::format("0x%016X", _atomToAddress[info.atom])
575 << " atom=" << info.atom
576 << " name=" << info.atom->name() << "\n");
577 }
578 }
579}
580
581uint8_t Util::scopeBits(const DefinedAtom* atom) {
582 switch (atom->scope()) {
583 case Atom::scopeTranslationUnit:
584 return 0;
585 case Atom::scopeLinkageUnit:
586 return N_PEXT | N_EXT;
587 case Atom::scopeGlobal:
588 return N_EXT;
589 }
Nick Kledzik020fa7f2013-11-06 22:18:09 +0000590 llvm_unreachable("Unknown scope");
Nick Kledzike34182f2013-11-06 21:36:55 +0000591}
592
Nick Kledzik60855392014-06-11 00:24:16 +0000593uint16_t Util::descBits(const DefinedAtom* atom) {
594 uint16_t desc = 0;
595 switch (atom->merge()) {
596 case lld::DefinedAtom::mergeNo:
597 case lld::DefinedAtom::mergeAsTentative:
598 break;
599 case lld::DefinedAtom::mergeAsWeak:
600 case lld::DefinedAtom::mergeAsWeakAndAddressUsed:
601 desc |= N_WEAK_DEF;
602 break;
603 case lld::DefinedAtom::mergeSameNameAndSize:
604 case lld::DefinedAtom::mergeByLargestSection:
605 case lld::DefinedAtom::mergeByContent:
606 llvm_unreachable("Unsupported DefinedAtom::merge()");
607 break;
608 }
609 if (atom->contentType() == lld::DefinedAtom::typeResolver)
610 desc |= N_SYMBOL_RESOLVER;
611 return desc;
612}
613
614
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000615bool Util::AtomSorter::operator()(const AtomAndIndex &left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000616 const AtomAndIndex &right) {
617 return (left.atom->name().compare(right.atom->name()) < 0);
618}
619
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000620
Nick Kledzike34182f2013-11-06 21:36:55 +0000621bool Util::belongsInGlobalSymbolsSection(const DefinedAtom* atom) {
622 return (atom->scope() == Atom::scopeGlobal);
623}
624
625void Util::addSymbols(const lld::File &atomFile, NormalizedFile &file) {
626 // Mach-O symbol table has three regions: locals, globals, undefs.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000627
Nick Kledzike34182f2013-11-06 21:36:55 +0000628 // Add all local (non-global) symbols in address order
629 std::vector<AtomAndIndex> globals;
630 globals.reserve(512);
631 for (SectionInfo *sect : _sectionInfos) {
632 for (const AtomInfo &info : sect->atomsAndOffsets) {
633 const DefinedAtom *atom = info.atom;
634 if (!atom->name().empty()) {
635 if (belongsInGlobalSymbolsSection(atom)) {
636 AtomAndIndex ai = { atom, sect->finalSectionIndex };
637 globals.push_back(ai);
638 } else {
639 Symbol sym;
640 sym.name = atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000641 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000642 sym.scope = scopeBits(atom);
643 sym.sect = sect->finalSectionIndex;
644 sym.desc = 0;
645 sym.value = _atomToAddress[atom];
646 file.localSymbols.push_back(sym);
647 }
648 }
649 }
650 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000651
Nick Kledzike34182f2013-11-06 21:36:55 +0000652 // Sort global symbol alphabetically, then add to symbol table.
653 std::sort(globals.begin(), globals.end(), AtomSorter());
654 for (AtomAndIndex &ai : globals) {
655 Symbol sym;
656 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000657 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000658 sym.scope = scopeBits(static_cast<const DefinedAtom*>(ai.atom));
659 sym.sect = ai.index;
Nick Kledzik60855392014-06-11 00:24:16 +0000660 sym.desc = descBits(static_cast<const DefinedAtom*>(ai.atom));
Nick Kledzike34182f2013-11-06 21:36:55 +0000661 sym.value = _atomToAddress[ai.atom];
662 file.globalSymbols.push_back(sym);
663 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000664
665
Nick Kledzike34182f2013-11-06 21:36:55 +0000666 // Sort undefined symbol alphabetically, then add to symbol table.
667 std::vector<AtomAndIndex> undefs;
668 undefs.reserve(128);
669 for (const UndefinedAtom *atom : atomFile.undefined()) {
670 AtomAndIndex ai = { atom, 0 };
671 undefs.push_back(ai);
672 }
673 for (const SharedLibraryAtom *atom : atomFile.sharedLibrary()) {
674 AtomAndIndex ai = { atom, 0 };
675 undefs.push_back(ai);
676 }
677 std::sort(undefs.begin(), undefs.end(), AtomSorter());
678 const uint32_t start = file.globalSymbols.size() + file.localSymbols.size();
679 for (AtomAndIndex &ai : undefs) {
680 Symbol sym;
681 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000682 sym.type = N_UNDF;
Nick Kledzike34182f2013-11-06 21:36:55 +0000683 sym.scope = N_EXT;
684 sym.sect = 0;
685 sym.desc = 0;
686 sym.value = 0;
687 _atomToSymbolIndex[ai.atom] = file.undefinedSymbols.size() + start;
688 file.undefinedSymbols.push_back(sym);
689 }
690}
691
692const Atom *Util::targetOfLazyPointer(const DefinedAtom *lpAtom) {
693 for (const Reference *ref : *lpAtom) {
Nick Kledzike5552772013-12-19 21:58:00 +0000694 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000695 return ref->target();
696 }
697 }
698 return nullptr;
699}
700
701const Atom *Util::targetOfStub(const DefinedAtom *stubAtom) {
702 for (const Reference *ref : *stubAtom) {
703 if (const Atom *ta = ref->target()) {
704 if (const DefinedAtom *lpAtom = dyn_cast<DefinedAtom>(ta)) {
705 const Atom *target = targetOfLazyPointer(lpAtom);
706 if (target)
707 return target;
708 }
709 }
710 }
711 return nullptr;
712}
713
714
715void Util::addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file) {
716 for (SectionInfo *si : _sectionInfos) {
717 Section &normSect = file.sections[si->normalizedSectionIndex];
718 switch (si->type) {
719 case llvm::MachO::S_NON_LAZY_SYMBOL_POINTERS:
720 for (const AtomInfo &info : si->atomsAndOffsets) {
721 bool foundTarget = false;
722 for (const Reference *ref : *info.atom) {
723 const Atom *target = ref->target();
724 if (target) {
725 if (isa<const SharedLibraryAtom>(target)) {
726 uint32_t index = _atomToSymbolIndex[target];
727 normSect.indirectSymbols.push_back(index);
728 foundTarget = true;
729 } else {
730 normSect.indirectSymbols.push_back(
731 llvm::MachO::INDIRECT_SYMBOL_LOCAL);
732 }
733 }
734 }
735 if (!foundTarget) {
736 normSect.indirectSymbols.push_back(
737 llvm::MachO::INDIRECT_SYMBOL_ABS);
738 }
739 }
740 break;
741 case llvm::MachO::S_LAZY_SYMBOL_POINTERS:
742 for (const AtomInfo &info : si->atomsAndOffsets) {
743 const Atom *target = targetOfLazyPointer(info.atom);
744 if (target) {
745 uint32_t index = _atomToSymbolIndex[target];
746 normSect.indirectSymbols.push_back(index);
747 }
748 }
749 break;
750 case llvm::MachO::S_SYMBOL_STUBS:
751 for (const AtomInfo &info : si->atomsAndOffsets) {
752 const Atom *target = targetOfStub(info.atom);
753 if (target) {
754 uint32_t index = _atomToSymbolIndex[target];
755 normSect.indirectSymbols.push_back(index);
756 }
757 }
758 break;
759 default:
760 break;
761 }
762 }
763
764}
765
766void Util::addDependentDylibs(const lld::File &atomFile,NormalizedFile &nFile) {
767 // Scan all imported symbols and build up list of dylibs they are from.
768 int ordinal = 1;
769 for (const SharedLibraryAtom *slAtom : atomFile.sharedLibrary()) {
770 StringRef loadPath = slAtom->loadName();
771 DylibPathToInfo::iterator pos = _dylibInfo.find(loadPath);
772 if (pos == _dylibInfo.end()) {
773 DylibInfo info;
774 info.ordinal = ordinal++;
775 info.hasWeak = slAtom->canBeNullAtRuntime();
776 info.hasNonWeak = !info.hasWeak;
777 _dylibInfo[loadPath] = info;
778 DependentDylib depInfo;
779 depInfo.path = loadPath;
780 depInfo.kind = llvm::MachO::LC_LOAD_DYLIB;
781 nFile.dependentDylibs.push_back(depInfo);
782 } else {
783 if ( slAtom->canBeNullAtRuntime() )
784 pos->second.hasWeak = true;
785 else
786 pos->second.hasNonWeak = true;
787 }
788 }
789 // Automatically weak link dylib in which all symbols are weak (canBeNull).
790 for (DependentDylib &dep : nFile.dependentDylibs) {
791 DylibInfo &info = _dylibInfo[dep.path];
792 if (info.hasWeak && !info.hasNonWeak)
793 dep.kind = llvm::MachO::LC_LOAD_WEAK_DYLIB;
794 }
795}
796
797
798int Util::dylibOrdinal(const SharedLibraryAtom *sa) {
799 return _dylibInfo[sa->loadName()].ordinal;
800}
801
802void Util::segIndexForSection(const SectionInfo *sect, uint8_t &segmentIndex,
803 uint64_t &segmentStartAddr) {
804 segmentIndex = 0;
805 for (const SegmentInfo *seg : _segmentInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000806 if ((seg->address <= sect->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000807 && (seg->address+seg->size >= sect->address+sect->size)) {
808 segmentStartAddr = seg->address;
809 return;
810 }
811 ++segmentIndex;
812 }
813 llvm_unreachable("section not in any segment");
814}
815
816
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000817void Util::appendReloc(const DefinedAtom *atom, const Reference *ref,
Nick Kledzike34182f2013-11-06 21:36:55 +0000818 Relocations &relocations) {
819 // TODO: convert Reference to normalized relocation
820}
821
822void Util::addSectionRelocs(const lld::File &, NormalizedFile &file) {
823 if (_context.outputFileType() != llvm::MachO::MH_OBJECT)
824 return;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000825
Nick Kledzike34182f2013-11-06 21:36:55 +0000826 for (SectionInfo *si : _sectionInfos) {
827 Section &normSect = file.sections[si->normalizedSectionIndex];
828 for (const AtomInfo &info : si->atomsAndOffsets) {
829 const DefinedAtom *atom = info.atom;
830 for (const Reference *ref : *atom) {
831 appendReloc(atom, ref, normSect.relocations);
832 }
833 }
834 }
835}
836
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000837void Util::addRebaseAndBindingInfo(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000838 NormalizedFile &nFile) {
839 if (_context.outputFileType() == llvm::MachO::MH_OBJECT)
840 return;
841
842 uint8_t segmentIndex;
843 uint64_t segmentStartAddr;
844 for (SectionInfo *sect : _sectionInfos) {
845 segIndexForSection(sect, segmentIndex, segmentStartAddr);
846 for (const AtomInfo &info : sect->atomsAndOffsets) {
847 const DefinedAtom *atom = info.atom;
848 for (const Reference *ref : *atom) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000849 uint64_t segmentOffset = _atomToAddress[atom] + ref->offsetInAtom()
Nick Kledzike34182f2013-11-06 21:36:55 +0000850 - segmentStartAddr;
851 const Atom* targ = ref->target();
Nick Kledzike5552772013-12-19 21:58:00 +0000852 if (_context.kindHandler().isPointer(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000853 // A pointer to a DefinedAtom requires rebasing.
854 if (dyn_cast<DefinedAtom>(targ)) {
855 RebaseLocation rebase;
856 rebase.segIndex = segmentIndex;
857 rebase.segOffset = segmentOffset;
858 rebase.kind = llvm::MachO::REBASE_TYPE_POINTER;
859 nFile.rebasingInfo.push_back(rebase);
860 }
861 // A pointer to an SharedLibraryAtom requires binding.
862 if (const SharedLibraryAtom *sa = dyn_cast<SharedLibraryAtom>(targ)) {
863 BindLocation bind;
864 bind.segIndex = segmentIndex;
865 bind.segOffset = segmentOffset;
866 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
867 bind.canBeNull = sa->canBeNullAtRuntime();
868 bind.ordinal = dylibOrdinal(sa);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000869 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000870 bind.addend = ref->addend();
871 nFile.bindingInfo.push_back(bind);
872 }
873 }
Nick Kledzike5552772013-12-19 21:58:00 +0000874 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000875 BindLocation bind;
876 bind.segIndex = segmentIndex;
877 bind.segOffset = segmentOffset;
878 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
879 bind.canBeNull = false; //sa->canBeNullAtRuntime();
880 bind.ordinal = 1;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000881 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000882 bind.addend = ref->addend();
883 nFile.lazyBindingInfo.push_back(bind);
884 }
885 }
886 }
887 }
888}
889
890uint32_t Util::fileFlags() {
891 return 0; //FIX ME
892}
893
894} // end anonymous namespace
895
896
897namespace lld {
898namespace mach_o {
899namespace normalized {
900
901/// Convert a set of Atoms into a normalized mach-o file.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000902ErrorOr<std::unique_ptr<NormalizedFile>>
903normalizedFromAtoms(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000904 const MachOLinkingContext &context) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000905 // The util object buffers info until the normalized file can be made.
Nick Kledzike34182f2013-11-06 21:36:55 +0000906 Util util(context);
907 util.assignAtomsToSections(atomFile);
908 util.organizeSections();
909 util.assignAddressesToSections();
910 util.buildAtomToAddressMap();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000911
Nick Kledzike34182f2013-11-06 21:36:55 +0000912 std::unique_ptr<NormalizedFile> f(new NormalizedFile());
913 NormalizedFile &normFile = *f.get();
914 f->arch = context.arch();
915 f->fileType = context.outputFileType();
916 f->flags = util.fileFlags();
917 util.copySegmentInfo(normFile);
918 util.copySections(normFile);
919 util.addDependentDylibs(atomFile, normFile);
920 util.addSymbols(atomFile, normFile);
921 util.addIndirectSymbols(atomFile, normFile);
922 util.addRebaseAndBindingInfo(atomFile, normFile);
923 util.addSectionRelocs(atomFile, normFile);
924 util.copyEntryPointAddress(normFile);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000925
Nick Kledzike34182f2013-11-06 21:36:55 +0000926 return std::move(f);
927}
928
929
930} // namespace normalized
931} // namespace mach_o
932} // namespace lld
933