blob: f187df9d674d7034c16944f5156659571753c875 [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"
Nick Kledzike34182f2013-11-06 21:36:55 +000026#include "lld/Core/Error.h"
27#include "lld/Core/LLVM.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000028#include "llvm/ADT/StringRef.h"
29#include "llvm/ADT/StringSwitch.h"
30#include "llvm/Support/Casting.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/Format.h"
34#include "llvm/Support/MachO.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000035#include <map>
Rafael Espindola54427cc2014-06-12 17:15:58 +000036#include <system_error>
Nick Kledzike34182f2013-11-06 21:36:55 +000037
38using llvm::StringRef;
Nick Kledzike34182f2013-11-06 21:36:55 +000039using llvm::isa;
40using namespace llvm::MachO;
41using namespace lld::mach_o::normalized;
42using namespace lld;
43
44namespace {
45
46struct AtomInfo {
47 const DefinedAtom *atom;
48 uint64_t offsetInSection;
49};
50
51struct SectionInfo {
52 SectionInfo(StringRef seg, StringRef sect, SectionType type, uint32_t attr=0);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000053
Nick Kledzike34182f2013-11-06 21:36:55 +000054 StringRef segmentName;
55 StringRef sectionName;
56 SectionType type;
57 uint32_t attributes;
58 uint64_t address;
59 uint64_t size;
60 uint32_t alignment;
61 std::vector<AtomInfo> atomsAndOffsets;
62 uint32_t normalizedSectionIndex;
63 uint32_t finalSectionIndex;
64};
65
Shankar Easwaran3d8de472014-01-27 03:09:26 +000066SectionInfo::SectionInfo(StringRef sg, StringRef sct, SectionType t, uint32_t a)
67 : segmentName(sg), sectionName(sct), type(t), attributes(a),
68 address(0), size(0), alignment(0),
Nick Kledzike34182f2013-11-06 21:36:55 +000069 normalizedSectionIndex(0), finalSectionIndex(0) {
70}
71
72struct SegmentInfo {
73 SegmentInfo(StringRef name);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000074
Nick Kledzike34182f2013-11-06 21:36:55 +000075 StringRef name;
76 uint64_t address;
77 uint64_t size;
78 uint32_t access;
79 std::vector<SectionInfo*> sections;
80};
81
Shankar Easwaran3d8de472014-01-27 03:09:26 +000082SegmentInfo::SegmentInfo(StringRef n)
Nick Kledzike34182f2013-11-06 21:36:55 +000083 : name(n), address(0), size(0), access(0) {
84}
85
86
87class Util {
88public:
89 Util(const MachOLinkingContext &ctxt) : _context(ctxt), _entryAtom(nullptr) {}
90
91 void assignAtomsToSections(const lld::File &atomFile);
92 void organizeSections();
93 void assignAddressesToSections();
94 uint32_t fileFlags();
95 void copySegmentInfo(NormalizedFile &file);
96 void copySections(NormalizedFile &file);
97 void buildAtomToAddressMap();
98 void addSymbols(const lld::File &atomFile, NormalizedFile &file);
99 void addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file);
100 void addRebaseAndBindingInfo(const lld::File &, NormalizedFile &file);
101 void addSectionRelocs(const lld::File &, NormalizedFile &file);
102 void addDependentDylibs(const lld::File &, NormalizedFile &file);
103 void copyEntryPointAddress(NormalizedFile &file);
104
105private:
106 typedef std::map<DefinedAtom::ContentType, SectionInfo*> TypeToSection;
107 typedef llvm::DenseMap<const Atom*, uint64_t> AtomToAddress;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000108
Nick Kledzike34182f2013-11-06 21:36:55 +0000109 struct DylibInfo { int ordinal; bool hasWeak; bool hasNonWeak; };
110 typedef llvm::StringMap<DylibInfo> DylibPathToInfo;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000111
Nick Kledzike34182f2013-11-06 21:36:55 +0000112 SectionInfo *sectionForAtom(const DefinedAtom*);
Nick Kledzik936d5202014-06-11 01:30:55 +0000113 SectionInfo *getRelocatableSection(DefinedAtom::ContentType type);
114 SectionInfo *getFinalSection(DefinedAtom::ContentType type);
Nick Kledzike34182f2013-11-06 21:36:55 +0000115 void appendAtom(SectionInfo *sect, const DefinedAtom *atom);
116 SegmentInfo *segmentForName(StringRef segName);
117 void layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr);
118 void layoutSectionsInTextSegment(SegmentInfo *seg, uint64_t &addr);
119 void copySectionContent(SectionInfo *si, ContentBytes &content);
120 uint8_t scopeBits(const DefinedAtom* atom);
Nick Kledzik60855392014-06-11 00:24:16 +0000121 uint16_t descBits(const DefinedAtom* atom);
Nick Kledzike34182f2013-11-06 21:36:55 +0000122 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
Nick Kledzikec140832014-06-10 01:50:00 +0000159
Nick Kledzik936d5202014-06-11 01:30:55 +0000160SectionInfo *Util::getRelocatableSection(DefinedAtom::ContentType type) {
Nick Kledzikec140832014-06-10 01:50:00 +0000161 StringRef segmentName;
162 StringRef sectionName;
163 SectionType sectionType;
164 SectionAttr sectionAttrs;
165
166 // Use same table used by when parsing .o files.
167 relocatableSectionInfoForContentType(type, segmentName, sectionName,
168 sectionType, sectionAttrs);
169 // If we already have a SectionInfo with this name, re-use it.
170 // This can happen if two ContentType map to the same mach-o section.
171 for (auto sect : _sectionMap) {
172 if (sect.second->sectionName.equals(sectionName) &&
173 sect.second->segmentName.equals(segmentName)) {
174 return sect.second;
175 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000176 }
Nick Kledzikec140832014-06-10 01:50:00 +0000177 // Otherwise allocate new SectionInfo object.
Nick Kledzik936d5202014-06-11 01:30:55 +0000178 SectionInfo *sect = new (_allocator) SectionInfo(segmentName, sectionName,
179 sectionType, sectionAttrs);
180 _sectionInfos.push_back(sect);
181 _sectionMap[type] = sect;
182 return sect;
Nick Kledzikec140832014-06-10 01:50:00 +0000183}
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
Nick Kledzik936d5202014-06-11 01:30:55 +0000223SectionInfo *Util::getFinalSection(DefinedAtom::ContentType atomType) {
Nick Kledzikec140832014-06-10 01:50:00 +0000224 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.
Nick Kledzik936d5202014-06-11 01:30:55 +0000246 SectionInfo *sect = new (_allocator) SectionInfo(p->segmentName,
247 p->sectionName,
248 p->sectionType,
249 sectionAttrs);
250 _sectionInfos.push_back(sect);
251 _sectionMap[atomType] = sect;
252 return sect;
Nick Kledzikec140832014-06-10 01:50:00 +0000253 }
254 llvm_unreachable("content type not yet supported");
Nick Kledzike34182f2013-11-06 21:36:55 +0000255}
256
257
258
259SectionInfo *Util::sectionForAtom(const DefinedAtom *atom) {
Nick Kledzikacfad802014-05-30 22:51:04 +0000260 if (atom->sectionChoice() == DefinedAtom::sectionBasedOnContent) {
261 // Section for this atom is derived from content type.
262 DefinedAtom::ContentType type = atom->contentType();
263 auto pos = _sectionMap.find(type);
264 if ( pos != _sectionMap.end() )
265 return pos->second;
Tim Northoverd30a1f22014-06-20 15:59:00 +0000266 bool rMode = (_context.outputMachOType() == llvm::MachO::MH_OBJECT);
Nick Kledzik936d5202014-06-11 01:30:55 +0000267 return rMode ? getRelocatableSection(type) : getFinalSection(type);
Nick Kledzikacfad802014-05-30 22:51:04 +0000268 } else {
269 // This atom needs to be in a custom section.
270 StringRef customName = atom->customSectionName();
271 // Look to see if we have already allocated the needed custom section.
272 for(SectionInfo *sect : _customSections) {
273 const DefinedAtom *firstAtom = sect->atomsAndOffsets.front().atom;
274 if (firstAtom->customSectionName().equals(customName)) {
275 return sect;
276 }
277 }
278 // Not found, so need to create a new custom section.
279 size_t seperatorIndex = customName.find('/');
280 assert(seperatorIndex != StringRef::npos);
281 StringRef segName = customName.slice(0, seperatorIndex-1);
282 StringRef sectName = customName.drop_front(seperatorIndex);
283 SectionInfo *sect = new (_allocator) SectionInfo(segName, sectName,
284 S_REGULAR);
285 _customSections.push_back(sect);
286 return sect;
287 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000288}
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000289
Nick Kledzike34182f2013-11-06 21:36:55 +0000290
291void Util::appendAtom(SectionInfo *sect, const DefinedAtom *atom) {
292 // Figure out offset for atom in this section given alignment constraints.
293 uint64_t offset = sect->size;
294 DefinedAtom::Alignment atomAlign = atom->alignment();
295 uint64_t align2 = 1 << atomAlign.powerOf2;
296 uint64_t requiredModulus = atomAlign.modulus;
297 uint64_t currentModulus = (offset % align2);
298 if ( currentModulus != requiredModulus ) {
299 if ( requiredModulus > currentModulus )
300 offset += requiredModulus-currentModulus;
301 else
302 offset += align2+requiredModulus-currentModulus;
303 }
304 // Record max alignment of any atom in this section.
305 if ( atomAlign.powerOf2 > sect->alignment )
306 sect->alignment = atomAlign.powerOf2;
307 // Assign atom to this section with this offset.
308 AtomInfo ai = {atom, offset};
309 sect->atomsAndOffsets.push_back(ai);
310 // Update section size to include this atom.
311 sect->size = offset + atom->size();
312}
313
314void Util::assignAtomsToSections(const lld::File &atomFile) {
315 for (const DefinedAtom *atom : atomFile.defined()) {
316 appendAtom(sectionForAtom(atom), atom);
317 }
318}
319
320SegmentInfo *Util::segmentForName(StringRef segName) {
321 for (SegmentInfo *si : _segmentInfos) {
322 if ( si->name.equals(segName) )
323 return si;
324 }
325 SegmentInfo *info = new (_allocator) SegmentInfo(segName);
326 if (segName.equals("__TEXT"))
327 info->access = VM_PROT_READ | VM_PROT_EXECUTE;
328 else if (segName.equals("__DATA"))
329 info->access = VM_PROT_READ | VM_PROT_WRITE;
330 else if (segName.equals("__PAGEZERO"))
331 info->access = 0;
332 _segmentInfos.push_back(info);
333 return info;
334}
335
336unsigned Util::SegmentSorter::weight(const SegmentInfo *seg) {
337 return llvm::StringSwitch<unsigned>(seg->name)
338 .Case("__PAGEZERO", 1)
339 .Case("__TEXT", 2)
340 .Case("__DATA", 3)
341 .Default(100);
342}
343
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000344bool Util::SegmentSorter::operator()(const SegmentInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000345 const SegmentInfo *right) {
346 return (weight(left) < weight(right));
347}
348
349unsigned Util::TextSectionSorter::weight(const SectionInfo *sect) {
350 return llvm::StringSwitch<unsigned>(sect->sectionName)
351 .Case("__text", 1)
352 .Case("__stubs", 2)
353 .Case("__stub_helper", 3)
354 .Case("__const", 4)
355 .Case("__cstring", 5)
356 .Case("__unwind_info", 98)
357 .Case("__eh_frame", 99)
358 .Default(10);
359}
360
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000361bool Util::TextSectionSorter::operator()(const SectionInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000362 const SectionInfo *right) {
363 return (weight(left) < weight(right));
364}
365
366
367void Util::organizeSections() {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000368 if (_context.outputMachOType() == llvm::MachO::MH_OBJECT) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000369 // Leave sections ordered as normalized file specified.
370 uint32_t sectionIndex = 1;
371 for (SectionInfo *si : _sectionInfos) {
372 si->finalSectionIndex = sectionIndex++;
373 }
374 } else {
375 // Main executables, need a zero-page segment
Tim Northoverd30a1f22014-06-20 15:59:00 +0000376 if (_context.outputMachOType() == llvm::MachO::MH_EXECUTE)
Nick Kledzike34182f2013-11-06 21:36:55 +0000377 segmentForName("__PAGEZERO");
378 // Group sections into segments.
379 for (SectionInfo *si : _sectionInfos) {
380 SegmentInfo *seg = segmentForName(si->segmentName);
381 seg->sections.push_back(si);
382 }
383 // Sort segments.
384 std::sort(_segmentInfos.begin(), _segmentInfos.end(), SegmentSorter());
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000385
Nick Kledzike34182f2013-11-06 21:36:55 +0000386 // Sort sections within segments.
387 for (SegmentInfo *seg : _segmentInfos) {
388 if (seg->name.equals("__TEXT")) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000389 std::sort(seg->sections.begin(), seg->sections.end(),
Nick Kledzike34182f2013-11-06 21:36:55 +0000390 TextSectionSorter());
391 }
392 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000393
Nick Kledzike34182f2013-11-06 21:36:55 +0000394 // Record final section indexes.
395 uint32_t sectionIndex = 1;
396 for (SegmentInfo *seg : _segmentInfos) {
397 for (SectionInfo *sect : seg->sections) {
398 sect->finalSectionIndex = sectionIndex++;
399 }
400 }
401 }
402
403}
404
405uint64_t Util::alignTo(uint64_t value, uint8_t align2) {
406 return llvm::RoundUpToAlignment(value, 1 << align2);
407}
408
409
410void Util::layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr) {
411 seg->address = addr;
412 for (SectionInfo *sect : seg->sections) {
413 sect->address = alignTo(addr, sect->alignment);
414 addr += sect->size;
415 }
416 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
417}
418
419
420// __TEXT segment lays out backwards so padding is at front after load commands.
421void Util::layoutSectionsInTextSegment(SegmentInfo *seg, uint64_t &addr) {
422 seg->address = addr;
423 // Walks sections starting at end to calculate padding for start.
424 int64_t taddr = 0;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000425 for (auto it = seg->sections.rbegin(); it != seg->sections.rend(); ++it) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000426 SectionInfo *sect = *it;
427 taddr -= sect->size;
428 taddr = taddr & (0 - (1 << sect->alignment));
429 }
430 int64_t padding = taddr;
431 while (padding < 0)
432 padding += _context.pageSize();
433 // Start assigning section address starting at padded offset.
434 addr += padding;
435 for (SectionInfo *sect : seg->sections) {
436 sect->address = alignTo(addr, sect->alignment);
437 addr = sect->address + sect->size;
438 }
439 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
440}
441
442
443void Util::assignAddressesToSections() {
444 uint64_t address = 0; // FIXME
Tim Northoverd30a1f22014-06-20 15:59:00 +0000445 if (_context.outputMachOType() != llvm::MachO::MH_OBJECT) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000446 for (SegmentInfo *seg : _segmentInfos) {
447 if (seg->name.equals("__PAGEZERO")) {
448 seg->size = _context.pageZeroSize();
449 address += seg->size;
450 }
451 else if (seg->name.equals("__TEXT"))
452 layoutSectionsInTextSegment(seg, address);
453 else
454 layoutSectionsInSegment(seg, address);
455 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000456 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000457 llvm::dbgs() << "assignAddressesToSections()\n";
458 for (SegmentInfo *sgi : _segmentInfos) {
459 llvm::dbgs() << " address=" << llvm::format("0x%08llX", sgi->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000460 << ", size=" << llvm::format("0x%08llX", sgi->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000461 << ", segment-name='" << sgi->name
Nick Kledzik020a49c2013-11-06 21:57:52 +0000462 << "'\n";
463 for (SectionInfo *si : sgi->sections) {
464 llvm::dbgs()<< " addr=" << llvm::format("0x%08llX", si->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000465 << ", size=" << llvm::format("0x%08llX", si->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000466 << ", section-name='" << si->sectionName
Nick Kledzik020a49c2013-11-06 21:57:52 +0000467 << "\n";
468 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000469 }
Nick Kledzik020a49c2013-11-06 21:57:52 +0000470 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000471 } else {
472 for (SectionInfo *sect : _sectionInfos) {
473 sect->address = alignTo(address, sect->alignment);
474 address = sect->address + sect->size;
475 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000476 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000477 llvm::dbgs() << "assignAddressesToSections()\n";
478 for (SectionInfo *si : _sectionInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000479 llvm::dbgs() << " section=" << si->sectionName
Nick Kledzike34182f2013-11-06 21:36:55 +0000480 << " address= " << llvm::format("0x%08X", si->address)
481 << " size= " << llvm::format("0x%08X", si->size)
Nick Kledzik020a49c2013-11-06 21:57:52 +0000482 << "\n";
483 }
484 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000485 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000486}
487
488
489void Util::copySegmentInfo(NormalizedFile &file) {
490 for (SegmentInfo *sgi : _segmentInfos) {
491 Segment seg;
492 seg.name = sgi->name;
493 seg.address = sgi->address;
494 seg.size = sgi->size;
495 seg.access = sgi->access;
496 file.segments.push_back(seg);
497 }
498}
499
500void Util::appendSection(SectionInfo *si, NormalizedFile &file) {
Nick Kledzik3f690762014-06-27 18:25:01 +0000501 const bool rMode = (_context.outputMachOType() == llvm::MachO::MH_OBJECT);
Nick Kledzike34182f2013-11-06 21:36:55 +0000502 // Add new empty section to end of file.sections.
503 Section temp;
504 file.sections.push_back(std::move(temp));
505 Section* normSect = &file.sections.back();
506 // Copy fields to normalized section.
507 normSect->segmentName = si->segmentName;
508 normSect->sectionName = si->sectionName;
509 normSect->type = si->type;
510 normSect->attributes = si->attributes;
511 normSect->address = si->address;
512 normSect->alignment = si->alignment;
513 // Record where normalized section is.
514 si->normalizedSectionIndex = file.sections.size()-1;
515 // Copy content from atoms to content buffer for section.
Nick Kledzik61fdef62014-05-15 20:59:23 +0000516 if (si->type == llvm::MachO::S_ZEROFILL)
517 return;
Nick Kledzik6edd7222014-01-11 01:07:43 +0000518 uint8_t *sectionContent = file.ownedAllocations.Allocate<uint8_t>(si->size);
519 normSect->content = llvm::makeArrayRef(sectionContent, si->size);
Nick Kledzike34182f2013-11-06 21:36:55 +0000520 for (AtomInfo &ai : si->atomsAndOffsets) {
521 // Copy raw bytes.
522 uint8_t *atomContent = reinterpret_cast<uint8_t*>
523 (&sectionContent[ai.offsetInSection]);
524 memcpy(atomContent, ai.atom->rawContent().data(), ai.atom->size());
525 // Apply fix-ups.
526 for (const Reference *ref : *ai.atom) {
527 uint32_t offset = ref->offsetInAtom();
528 uint64_t targetAddress = 0;
529 if ( ref->target() != nullptr )
530 targetAddress = _atomToAddress[ref->target()];
531 uint64_t fixupAddress = _atomToAddress[ai.atom] + offset;
Nick Kledzik3f690762014-06-27 18:25:01 +0000532 if ( rMode ) {
533 // FIXME: Need a handler method to update content for .o file
534 // output and any needed section relocations.
535 } else {
536 _context.kindHandler().applyFixup(
Rui Ueyama170a1a82013-12-20 07:48:29 +0000537 ref->kindNamespace(), ref->kindArch(), ref->kindValue(),
538 ref->addend(), &atomContent[offset], fixupAddress, targetAddress);
Nick Kledzik3f690762014-06-27 18:25:01 +0000539 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000540 }
541 }
542}
543
544void Util::copySections(NormalizedFile &file) {
545 file.sections.reserve(_sectionInfos.size());
546 // For final linked images, write sections grouped by segment.
Tim Northoverd30a1f22014-06-20 15:59:00 +0000547 if (_context.outputMachOType() != llvm::MachO::MH_OBJECT) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000548 for (SegmentInfo *sgi : _segmentInfos) {
549 for (SectionInfo *si : sgi->sections) {
550 appendSection(si, file);
551 }
552 }
553 } else {
554 // Object files write sections in default order.
555 for (SectionInfo *si : _sectionInfos) {
556 appendSection(si, file);
557 }
558 }
559}
560
561void Util::copyEntryPointAddress(NormalizedFile &nFile) {
562 if (_context.outputTypeHasEntry()) {
563 nFile.entryAddress = _atomToAddress[_entryAtom];
564 }
565}
566
567void Util::buildAtomToAddressMap() {
568 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
569 << "assign atom addresses:\n");
570 const bool lookForEntry = _context.outputTypeHasEntry();
571 for (SectionInfo *sect : _sectionInfos) {
572 for (const AtomInfo &info : sect->atomsAndOffsets) {
573 _atomToAddress[info.atom] = sect->address + info.offsetInSection;
574 if (lookForEntry && (info.atom->contentType() == DefinedAtom::typeCode) &&
575 (info.atom->size() != 0) &&
576 info.atom->name() == _context.entrySymbolName()) {
577 _entryAtom = info.atom;
578 }
579 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
580 << " address="
581 << llvm::format("0x%016X", _atomToAddress[info.atom])
582 << " atom=" << info.atom
583 << " name=" << info.atom->name() << "\n");
584 }
585 }
586}
587
588uint8_t Util::scopeBits(const DefinedAtom* atom) {
589 switch (atom->scope()) {
590 case Atom::scopeTranslationUnit:
591 return 0;
592 case Atom::scopeLinkageUnit:
593 return N_PEXT | N_EXT;
594 case Atom::scopeGlobal:
595 return N_EXT;
596 }
Nick Kledzik020fa7f2013-11-06 22:18:09 +0000597 llvm_unreachable("Unknown scope");
Nick Kledzike34182f2013-11-06 21:36:55 +0000598}
599
Nick Kledzik60855392014-06-11 00:24:16 +0000600uint16_t Util::descBits(const DefinedAtom* atom) {
601 uint16_t desc = 0;
602 switch (atom->merge()) {
603 case lld::DefinedAtom::mergeNo:
604 case lld::DefinedAtom::mergeAsTentative:
605 break;
606 case lld::DefinedAtom::mergeAsWeak:
607 case lld::DefinedAtom::mergeAsWeakAndAddressUsed:
608 desc |= N_WEAK_DEF;
609 break;
610 case lld::DefinedAtom::mergeSameNameAndSize:
611 case lld::DefinedAtom::mergeByLargestSection:
612 case lld::DefinedAtom::mergeByContent:
613 llvm_unreachable("Unsupported DefinedAtom::merge()");
614 break;
615 }
616 if (atom->contentType() == lld::DefinedAtom::typeResolver)
617 desc |= N_SYMBOL_RESOLVER;
618 return desc;
619}
620
621
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000622bool Util::AtomSorter::operator()(const AtomAndIndex &left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000623 const AtomAndIndex &right) {
624 return (left.atom->name().compare(right.atom->name()) < 0);
625}
626
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000627
Nick Kledzike34182f2013-11-06 21:36:55 +0000628bool Util::belongsInGlobalSymbolsSection(const DefinedAtom* atom) {
Nick Kledzik936d5202014-06-11 01:30:55 +0000629 // ScopeLinkageUnit symbols are in globals area of symbol table
630 // in object files, but in locals area for final linked images.
Tim Northoverd30a1f22014-06-20 15:59:00 +0000631 if (_context.outputMachOType() == llvm::MachO::MH_OBJECT)
Nick Kledzik936d5202014-06-11 01:30:55 +0000632 return (atom->scope() != Atom::scopeTranslationUnit);
633 else
634 return (atom->scope() == Atom::scopeGlobal);
Nick Kledzike34182f2013-11-06 21:36:55 +0000635}
636
637void Util::addSymbols(const lld::File &atomFile, NormalizedFile &file) {
638 // Mach-O symbol table has three regions: locals, globals, undefs.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000639
Nick Kledzike34182f2013-11-06 21:36:55 +0000640 // Add all local (non-global) symbols in address order
641 std::vector<AtomAndIndex> globals;
642 globals.reserve(512);
643 for (SectionInfo *sect : _sectionInfos) {
644 for (const AtomInfo &info : sect->atomsAndOffsets) {
645 const DefinedAtom *atom = info.atom;
646 if (!atom->name().empty()) {
647 if (belongsInGlobalSymbolsSection(atom)) {
648 AtomAndIndex ai = { atom, sect->finalSectionIndex };
649 globals.push_back(ai);
650 } else {
651 Symbol sym;
652 sym.name = atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000653 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000654 sym.scope = scopeBits(atom);
655 sym.sect = sect->finalSectionIndex;
656 sym.desc = 0;
657 sym.value = _atomToAddress[atom];
658 file.localSymbols.push_back(sym);
659 }
660 }
661 }
662 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000663
Nick Kledzike34182f2013-11-06 21:36:55 +0000664 // Sort global symbol alphabetically, then add to symbol table.
665 std::sort(globals.begin(), globals.end(), AtomSorter());
666 for (AtomAndIndex &ai : globals) {
667 Symbol sym;
668 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000669 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000670 sym.scope = scopeBits(static_cast<const DefinedAtom*>(ai.atom));
671 sym.sect = ai.index;
Nick Kledzik60855392014-06-11 00:24:16 +0000672 sym.desc = descBits(static_cast<const DefinedAtom*>(ai.atom));
Nick Kledzike34182f2013-11-06 21:36:55 +0000673 sym.value = _atomToAddress[ai.atom];
674 file.globalSymbols.push_back(sym);
675 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000676
677
Nick Kledzike34182f2013-11-06 21:36:55 +0000678 // Sort undefined symbol alphabetically, then add to symbol table.
679 std::vector<AtomAndIndex> undefs;
680 undefs.reserve(128);
681 for (const UndefinedAtom *atom : atomFile.undefined()) {
682 AtomAndIndex ai = { atom, 0 };
683 undefs.push_back(ai);
684 }
685 for (const SharedLibraryAtom *atom : atomFile.sharedLibrary()) {
686 AtomAndIndex ai = { atom, 0 };
687 undefs.push_back(ai);
688 }
689 std::sort(undefs.begin(), undefs.end(), AtomSorter());
690 const uint32_t start = file.globalSymbols.size() + file.localSymbols.size();
691 for (AtomAndIndex &ai : undefs) {
692 Symbol sym;
693 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000694 sym.type = N_UNDF;
Nick Kledzike34182f2013-11-06 21:36:55 +0000695 sym.scope = N_EXT;
696 sym.sect = 0;
697 sym.desc = 0;
698 sym.value = 0;
699 _atomToSymbolIndex[ai.atom] = file.undefinedSymbols.size() + start;
700 file.undefinedSymbols.push_back(sym);
701 }
702}
703
704const Atom *Util::targetOfLazyPointer(const DefinedAtom *lpAtom) {
705 for (const Reference *ref : *lpAtom) {
Nick Kledzike5552772013-12-19 21:58:00 +0000706 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000707 return ref->target();
708 }
709 }
710 return nullptr;
711}
712
713const Atom *Util::targetOfStub(const DefinedAtom *stubAtom) {
714 for (const Reference *ref : *stubAtom) {
715 if (const Atom *ta = ref->target()) {
716 if (const DefinedAtom *lpAtom = dyn_cast<DefinedAtom>(ta)) {
717 const Atom *target = targetOfLazyPointer(lpAtom);
718 if (target)
719 return target;
720 }
721 }
722 }
723 return nullptr;
724}
725
726
727void Util::addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file) {
728 for (SectionInfo *si : _sectionInfos) {
729 Section &normSect = file.sections[si->normalizedSectionIndex];
730 switch (si->type) {
731 case llvm::MachO::S_NON_LAZY_SYMBOL_POINTERS:
732 for (const AtomInfo &info : si->atomsAndOffsets) {
733 bool foundTarget = false;
734 for (const Reference *ref : *info.atom) {
735 const Atom *target = ref->target();
736 if (target) {
737 if (isa<const SharedLibraryAtom>(target)) {
738 uint32_t index = _atomToSymbolIndex[target];
739 normSect.indirectSymbols.push_back(index);
740 foundTarget = true;
741 } else {
742 normSect.indirectSymbols.push_back(
743 llvm::MachO::INDIRECT_SYMBOL_LOCAL);
744 }
745 }
746 }
747 if (!foundTarget) {
748 normSect.indirectSymbols.push_back(
749 llvm::MachO::INDIRECT_SYMBOL_ABS);
750 }
751 }
752 break;
753 case llvm::MachO::S_LAZY_SYMBOL_POINTERS:
754 for (const AtomInfo &info : si->atomsAndOffsets) {
755 const Atom *target = targetOfLazyPointer(info.atom);
756 if (target) {
757 uint32_t index = _atomToSymbolIndex[target];
758 normSect.indirectSymbols.push_back(index);
759 }
760 }
761 break;
762 case llvm::MachO::S_SYMBOL_STUBS:
763 for (const AtomInfo &info : si->atomsAndOffsets) {
764 const Atom *target = targetOfStub(info.atom);
765 if (target) {
766 uint32_t index = _atomToSymbolIndex[target];
767 normSect.indirectSymbols.push_back(index);
768 }
769 }
770 break;
771 default:
772 break;
773 }
774 }
775
776}
777
778void Util::addDependentDylibs(const lld::File &atomFile,NormalizedFile &nFile) {
779 // Scan all imported symbols and build up list of dylibs they are from.
780 int ordinal = 1;
781 for (const SharedLibraryAtom *slAtom : atomFile.sharedLibrary()) {
782 StringRef loadPath = slAtom->loadName();
783 DylibPathToInfo::iterator pos = _dylibInfo.find(loadPath);
784 if (pos == _dylibInfo.end()) {
785 DylibInfo info;
786 info.ordinal = ordinal++;
787 info.hasWeak = slAtom->canBeNullAtRuntime();
788 info.hasNonWeak = !info.hasWeak;
789 _dylibInfo[loadPath] = info;
790 DependentDylib depInfo;
791 depInfo.path = loadPath;
792 depInfo.kind = llvm::MachO::LC_LOAD_DYLIB;
793 nFile.dependentDylibs.push_back(depInfo);
794 } else {
795 if ( slAtom->canBeNullAtRuntime() )
796 pos->second.hasWeak = true;
797 else
798 pos->second.hasNonWeak = true;
799 }
800 }
801 // Automatically weak link dylib in which all symbols are weak (canBeNull).
802 for (DependentDylib &dep : nFile.dependentDylibs) {
803 DylibInfo &info = _dylibInfo[dep.path];
804 if (info.hasWeak && !info.hasNonWeak)
805 dep.kind = llvm::MachO::LC_LOAD_WEAK_DYLIB;
806 }
807}
808
809
810int Util::dylibOrdinal(const SharedLibraryAtom *sa) {
811 return _dylibInfo[sa->loadName()].ordinal;
812}
813
814void Util::segIndexForSection(const SectionInfo *sect, uint8_t &segmentIndex,
815 uint64_t &segmentStartAddr) {
816 segmentIndex = 0;
817 for (const SegmentInfo *seg : _segmentInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000818 if ((seg->address <= sect->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000819 && (seg->address+seg->size >= sect->address+sect->size)) {
820 segmentStartAddr = seg->address;
821 return;
822 }
823 ++segmentIndex;
824 }
825 llvm_unreachable("section not in any segment");
826}
827
828
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000829void Util::appendReloc(const DefinedAtom *atom, const Reference *ref,
Nick Kledzike34182f2013-11-06 21:36:55 +0000830 Relocations &relocations) {
831 // TODO: convert Reference to normalized relocation
832}
833
834void Util::addSectionRelocs(const lld::File &, NormalizedFile &file) {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000835 if (_context.outputMachOType() != llvm::MachO::MH_OBJECT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000836 return;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000837
Nick Kledzike34182f2013-11-06 21:36:55 +0000838 for (SectionInfo *si : _sectionInfos) {
839 Section &normSect = file.sections[si->normalizedSectionIndex];
840 for (const AtomInfo &info : si->atomsAndOffsets) {
841 const DefinedAtom *atom = info.atom;
842 for (const Reference *ref : *atom) {
843 appendReloc(atom, ref, normSect.relocations);
844 }
845 }
846 }
847}
848
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000849void Util::addRebaseAndBindingInfo(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000850 NormalizedFile &nFile) {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000851 if (_context.outputMachOType() == llvm::MachO::MH_OBJECT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000852 return;
853
854 uint8_t segmentIndex;
855 uint64_t segmentStartAddr;
856 for (SectionInfo *sect : _sectionInfos) {
857 segIndexForSection(sect, segmentIndex, segmentStartAddr);
858 for (const AtomInfo &info : sect->atomsAndOffsets) {
859 const DefinedAtom *atom = info.atom;
860 for (const Reference *ref : *atom) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000861 uint64_t segmentOffset = _atomToAddress[atom] + ref->offsetInAtom()
Nick Kledzike34182f2013-11-06 21:36:55 +0000862 - segmentStartAddr;
863 const Atom* targ = ref->target();
Nick Kledzike5552772013-12-19 21:58:00 +0000864 if (_context.kindHandler().isPointer(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000865 // A pointer to a DefinedAtom requires rebasing.
866 if (dyn_cast<DefinedAtom>(targ)) {
867 RebaseLocation rebase;
868 rebase.segIndex = segmentIndex;
869 rebase.segOffset = segmentOffset;
870 rebase.kind = llvm::MachO::REBASE_TYPE_POINTER;
871 nFile.rebasingInfo.push_back(rebase);
872 }
873 // A pointer to an SharedLibraryAtom requires binding.
874 if (const SharedLibraryAtom *sa = dyn_cast<SharedLibraryAtom>(targ)) {
875 BindLocation bind;
876 bind.segIndex = segmentIndex;
877 bind.segOffset = segmentOffset;
878 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
879 bind.canBeNull = sa->canBeNullAtRuntime();
880 bind.ordinal = dylibOrdinal(sa);
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.bindingInfo.push_back(bind);
884 }
885 }
Nick Kledzike5552772013-12-19 21:58:00 +0000886 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000887 BindLocation bind;
888 bind.segIndex = segmentIndex;
889 bind.segOffset = segmentOffset;
890 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
891 bind.canBeNull = false; //sa->canBeNullAtRuntime();
892 bind.ordinal = 1;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000893 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000894 bind.addend = ref->addend();
895 nFile.lazyBindingInfo.push_back(bind);
896 }
897 }
898 }
899 }
900}
901
902uint32_t Util::fileFlags() {
903 return 0; //FIX ME
904}
905
906} // end anonymous namespace
907
908
909namespace lld {
910namespace mach_o {
911namespace normalized {
912
913/// Convert a set of Atoms into a normalized mach-o file.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000914ErrorOr<std::unique_ptr<NormalizedFile>>
915normalizedFromAtoms(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000916 const MachOLinkingContext &context) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000917 // The util object buffers info until the normalized file can be made.
Nick Kledzike34182f2013-11-06 21:36:55 +0000918 Util util(context);
919 util.assignAtomsToSections(atomFile);
920 util.organizeSections();
921 util.assignAddressesToSections();
922 util.buildAtomToAddressMap();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000923
Nick Kledzike34182f2013-11-06 21:36:55 +0000924 std::unique_ptr<NormalizedFile> f(new NormalizedFile());
925 NormalizedFile &normFile = *f.get();
926 f->arch = context.arch();
Tim Northoverd30a1f22014-06-20 15:59:00 +0000927 f->fileType = context.outputMachOType();
Nick Kledzike34182f2013-11-06 21:36:55 +0000928 f->flags = util.fileFlags();
929 util.copySegmentInfo(normFile);
930 util.copySections(normFile);
931 util.addDependentDylibs(atomFile, normFile);
932 util.addSymbols(atomFile, normFile);
933 util.addIndirectSymbols(atomFile, normFile);
934 util.addRebaseAndBindingInfo(atomFile, normFile);
935 util.addSectionRelocs(atomFile, normFile);
936 util.copyEntryPointAddress(normFile);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000937
Nick Kledzike34182f2013-11-06 21:36:55 +0000938 return std::move(f);
939}
940
941
942} // namespace normalized
943} // namespace mach_o
944} // namespace lld
945