blob: 726092ccf2d9576c940c9fb50008c705158136ca [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) {
501 // Add new empty section to end of file.sections.
502 Section temp;
503 file.sections.push_back(std::move(temp));
504 Section* normSect = &file.sections.back();
505 // Copy fields to normalized section.
506 normSect->segmentName = si->segmentName;
507 normSect->sectionName = si->sectionName;
508 normSect->type = si->type;
509 normSect->attributes = si->attributes;
510 normSect->address = si->address;
511 normSect->alignment = si->alignment;
512 // Record where normalized section is.
513 si->normalizedSectionIndex = file.sections.size()-1;
514 // Copy content from atoms to content buffer for section.
Nick Kledzik61fdef62014-05-15 20:59:23 +0000515 if (si->type == llvm::MachO::S_ZEROFILL)
516 return;
Nick Kledzik6edd7222014-01-11 01:07:43 +0000517 uint8_t *sectionContent = file.ownedAllocations.Allocate<uint8_t>(si->size);
518 normSect->content = llvm::makeArrayRef(sectionContent, si->size);
Nick Kledzike34182f2013-11-06 21:36:55 +0000519 for (AtomInfo &ai : si->atomsAndOffsets) {
520 // Copy raw bytes.
521 uint8_t *atomContent = reinterpret_cast<uint8_t*>
522 (&sectionContent[ai.offsetInSection]);
523 memcpy(atomContent, ai.atom->rawContent().data(), ai.atom->size());
524 // Apply fix-ups.
525 for (const Reference *ref : *ai.atom) {
526 uint32_t offset = ref->offsetInAtom();
527 uint64_t targetAddress = 0;
528 if ( ref->target() != nullptr )
529 targetAddress = _atomToAddress[ref->target()];
530 uint64_t fixupAddress = _atomToAddress[ai.atom] + offset;
Rui Ueyama170a1a82013-12-20 07:48:29 +0000531 _context.kindHandler().applyFixup(
532 ref->kindNamespace(), ref->kindArch(), ref->kindValue(),
533 ref->addend(), &atomContent[offset], fixupAddress, targetAddress);
Nick Kledzike34182f2013-11-06 21:36:55 +0000534 }
535 }
536}
537
538void Util::copySections(NormalizedFile &file) {
539 file.sections.reserve(_sectionInfos.size());
540 // For final linked images, write sections grouped by segment.
Tim Northoverd30a1f22014-06-20 15:59:00 +0000541 if (_context.outputMachOType() != llvm::MachO::MH_OBJECT) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000542 for (SegmentInfo *sgi : _segmentInfos) {
543 for (SectionInfo *si : sgi->sections) {
544 appendSection(si, file);
545 }
546 }
547 } else {
548 // Object files write sections in default order.
549 for (SectionInfo *si : _sectionInfos) {
550 appendSection(si, file);
551 }
552 }
553}
554
555void Util::copyEntryPointAddress(NormalizedFile &nFile) {
556 if (_context.outputTypeHasEntry()) {
557 nFile.entryAddress = _atomToAddress[_entryAtom];
558 }
559}
560
561void Util::buildAtomToAddressMap() {
562 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
563 << "assign atom addresses:\n");
564 const bool lookForEntry = _context.outputTypeHasEntry();
565 for (SectionInfo *sect : _sectionInfos) {
566 for (const AtomInfo &info : sect->atomsAndOffsets) {
567 _atomToAddress[info.atom] = sect->address + info.offsetInSection;
568 if (lookForEntry && (info.atom->contentType() == DefinedAtom::typeCode) &&
569 (info.atom->size() != 0) &&
570 info.atom->name() == _context.entrySymbolName()) {
571 _entryAtom = info.atom;
572 }
573 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
574 << " address="
575 << llvm::format("0x%016X", _atomToAddress[info.atom])
576 << " atom=" << info.atom
577 << " name=" << info.atom->name() << "\n");
578 }
579 }
580}
581
582uint8_t Util::scopeBits(const DefinedAtom* atom) {
583 switch (atom->scope()) {
584 case Atom::scopeTranslationUnit:
585 return 0;
586 case Atom::scopeLinkageUnit:
587 return N_PEXT | N_EXT;
588 case Atom::scopeGlobal:
589 return N_EXT;
590 }
Nick Kledzik020fa7f2013-11-06 22:18:09 +0000591 llvm_unreachable("Unknown scope");
Nick Kledzike34182f2013-11-06 21:36:55 +0000592}
593
Nick Kledzik60855392014-06-11 00:24:16 +0000594uint16_t Util::descBits(const DefinedAtom* atom) {
595 uint16_t desc = 0;
596 switch (atom->merge()) {
597 case lld::DefinedAtom::mergeNo:
598 case lld::DefinedAtom::mergeAsTentative:
599 break;
600 case lld::DefinedAtom::mergeAsWeak:
601 case lld::DefinedAtom::mergeAsWeakAndAddressUsed:
602 desc |= N_WEAK_DEF;
603 break;
604 case lld::DefinedAtom::mergeSameNameAndSize:
605 case lld::DefinedAtom::mergeByLargestSection:
606 case lld::DefinedAtom::mergeByContent:
607 llvm_unreachable("Unsupported DefinedAtom::merge()");
608 break;
609 }
610 if (atom->contentType() == lld::DefinedAtom::typeResolver)
611 desc |= N_SYMBOL_RESOLVER;
612 return desc;
613}
614
615
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000616bool Util::AtomSorter::operator()(const AtomAndIndex &left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000617 const AtomAndIndex &right) {
618 return (left.atom->name().compare(right.atom->name()) < 0);
619}
620
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000621
Nick Kledzike34182f2013-11-06 21:36:55 +0000622bool Util::belongsInGlobalSymbolsSection(const DefinedAtom* atom) {
Nick Kledzik936d5202014-06-11 01:30:55 +0000623 // ScopeLinkageUnit symbols are in globals area of symbol table
624 // in object files, but in locals area for final linked images.
Tim Northoverd30a1f22014-06-20 15:59:00 +0000625 if (_context.outputMachOType() == llvm::MachO::MH_OBJECT)
Nick Kledzik936d5202014-06-11 01:30:55 +0000626 return (atom->scope() != Atom::scopeTranslationUnit);
627 else
628 return (atom->scope() == Atom::scopeGlobal);
Nick Kledzike34182f2013-11-06 21:36:55 +0000629}
630
631void Util::addSymbols(const lld::File &atomFile, NormalizedFile &file) {
632 // Mach-O symbol table has three regions: locals, globals, undefs.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000633
Nick Kledzike34182f2013-11-06 21:36:55 +0000634 // Add all local (non-global) symbols in address order
635 std::vector<AtomAndIndex> globals;
636 globals.reserve(512);
637 for (SectionInfo *sect : _sectionInfos) {
638 for (const AtomInfo &info : sect->atomsAndOffsets) {
639 const DefinedAtom *atom = info.atom;
640 if (!atom->name().empty()) {
641 if (belongsInGlobalSymbolsSection(atom)) {
642 AtomAndIndex ai = { atom, sect->finalSectionIndex };
643 globals.push_back(ai);
644 } else {
645 Symbol sym;
646 sym.name = atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000647 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000648 sym.scope = scopeBits(atom);
649 sym.sect = sect->finalSectionIndex;
650 sym.desc = 0;
651 sym.value = _atomToAddress[atom];
652 file.localSymbols.push_back(sym);
653 }
654 }
655 }
656 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000657
Nick Kledzike34182f2013-11-06 21:36:55 +0000658 // Sort global symbol alphabetically, then add to symbol table.
659 std::sort(globals.begin(), globals.end(), AtomSorter());
660 for (AtomAndIndex &ai : globals) {
661 Symbol sym;
662 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000663 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000664 sym.scope = scopeBits(static_cast<const DefinedAtom*>(ai.atom));
665 sym.sect = ai.index;
Nick Kledzik60855392014-06-11 00:24:16 +0000666 sym.desc = descBits(static_cast<const DefinedAtom*>(ai.atom));
Nick Kledzike34182f2013-11-06 21:36:55 +0000667 sym.value = _atomToAddress[ai.atom];
668 file.globalSymbols.push_back(sym);
669 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000670
671
Nick Kledzike34182f2013-11-06 21:36:55 +0000672 // Sort undefined symbol alphabetically, then add to symbol table.
673 std::vector<AtomAndIndex> undefs;
674 undefs.reserve(128);
675 for (const UndefinedAtom *atom : atomFile.undefined()) {
676 AtomAndIndex ai = { atom, 0 };
677 undefs.push_back(ai);
678 }
679 for (const SharedLibraryAtom *atom : atomFile.sharedLibrary()) {
680 AtomAndIndex ai = { atom, 0 };
681 undefs.push_back(ai);
682 }
683 std::sort(undefs.begin(), undefs.end(), AtomSorter());
684 const uint32_t start = file.globalSymbols.size() + file.localSymbols.size();
685 for (AtomAndIndex &ai : undefs) {
686 Symbol sym;
687 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000688 sym.type = N_UNDF;
Nick Kledzike34182f2013-11-06 21:36:55 +0000689 sym.scope = N_EXT;
690 sym.sect = 0;
691 sym.desc = 0;
692 sym.value = 0;
693 _atomToSymbolIndex[ai.atom] = file.undefinedSymbols.size() + start;
694 file.undefinedSymbols.push_back(sym);
695 }
696}
697
698const Atom *Util::targetOfLazyPointer(const DefinedAtom *lpAtom) {
699 for (const Reference *ref : *lpAtom) {
Nick Kledzike5552772013-12-19 21:58:00 +0000700 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000701 return ref->target();
702 }
703 }
704 return nullptr;
705}
706
707const Atom *Util::targetOfStub(const DefinedAtom *stubAtom) {
708 for (const Reference *ref : *stubAtom) {
709 if (const Atom *ta = ref->target()) {
710 if (const DefinedAtom *lpAtom = dyn_cast<DefinedAtom>(ta)) {
711 const Atom *target = targetOfLazyPointer(lpAtom);
712 if (target)
713 return target;
714 }
715 }
716 }
717 return nullptr;
718}
719
720
721void Util::addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file) {
722 for (SectionInfo *si : _sectionInfos) {
723 Section &normSect = file.sections[si->normalizedSectionIndex];
724 switch (si->type) {
725 case llvm::MachO::S_NON_LAZY_SYMBOL_POINTERS:
726 for (const AtomInfo &info : si->atomsAndOffsets) {
727 bool foundTarget = false;
728 for (const Reference *ref : *info.atom) {
729 const Atom *target = ref->target();
730 if (target) {
731 if (isa<const SharedLibraryAtom>(target)) {
732 uint32_t index = _atomToSymbolIndex[target];
733 normSect.indirectSymbols.push_back(index);
734 foundTarget = true;
735 } else {
736 normSect.indirectSymbols.push_back(
737 llvm::MachO::INDIRECT_SYMBOL_LOCAL);
738 }
739 }
740 }
741 if (!foundTarget) {
742 normSect.indirectSymbols.push_back(
743 llvm::MachO::INDIRECT_SYMBOL_ABS);
744 }
745 }
746 break;
747 case llvm::MachO::S_LAZY_SYMBOL_POINTERS:
748 for (const AtomInfo &info : si->atomsAndOffsets) {
749 const Atom *target = targetOfLazyPointer(info.atom);
750 if (target) {
751 uint32_t index = _atomToSymbolIndex[target];
752 normSect.indirectSymbols.push_back(index);
753 }
754 }
755 break;
756 case llvm::MachO::S_SYMBOL_STUBS:
757 for (const AtomInfo &info : si->atomsAndOffsets) {
758 const Atom *target = targetOfStub(info.atom);
759 if (target) {
760 uint32_t index = _atomToSymbolIndex[target];
761 normSect.indirectSymbols.push_back(index);
762 }
763 }
764 break;
765 default:
766 break;
767 }
768 }
769
770}
771
772void Util::addDependentDylibs(const lld::File &atomFile,NormalizedFile &nFile) {
773 // Scan all imported symbols and build up list of dylibs they are from.
774 int ordinal = 1;
775 for (const SharedLibraryAtom *slAtom : atomFile.sharedLibrary()) {
776 StringRef loadPath = slAtom->loadName();
777 DylibPathToInfo::iterator pos = _dylibInfo.find(loadPath);
778 if (pos == _dylibInfo.end()) {
779 DylibInfo info;
780 info.ordinal = ordinal++;
781 info.hasWeak = slAtom->canBeNullAtRuntime();
782 info.hasNonWeak = !info.hasWeak;
783 _dylibInfo[loadPath] = info;
784 DependentDylib depInfo;
785 depInfo.path = loadPath;
786 depInfo.kind = llvm::MachO::LC_LOAD_DYLIB;
787 nFile.dependentDylibs.push_back(depInfo);
788 } else {
789 if ( slAtom->canBeNullAtRuntime() )
790 pos->second.hasWeak = true;
791 else
792 pos->second.hasNonWeak = true;
793 }
794 }
795 // Automatically weak link dylib in which all symbols are weak (canBeNull).
796 for (DependentDylib &dep : nFile.dependentDylibs) {
797 DylibInfo &info = _dylibInfo[dep.path];
798 if (info.hasWeak && !info.hasNonWeak)
799 dep.kind = llvm::MachO::LC_LOAD_WEAK_DYLIB;
800 }
801}
802
803
804int Util::dylibOrdinal(const SharedLibraryAtom *sa) {
805 return _dylibInfo[sa->loadName()].ordinal;
806}
807
808void Util::segIndexForSection(const SectionInfo *sect, uint8_t &segmentIndex,
809 uint64_t &segmentStartAddr) {
810 segmentIndex = 0;
811 for (const SegmentInfo *seg : _segmentInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000812 if ((seg->address <= sect->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000813 && (seg->address+seg->size >= sect->address+sect->size)) {
814 segmentStartAddr = seg->address;
815 return;
816 }
817 ++segmentIndex;
818 }
819 llvm_unreachable("section not in any segment");
820}
821
822
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000823void Util::appendReloc(const DefinedAtom *atom, const Reference *ref,
Nick Kledzike34182f2013-11-06 21:36:55 +0000824 Relocations &relocations) {
825 // TODO: convert Reference to normalized relocation
826}
827
828void Util::addSectionRelocs(const lld::File &, NormalizedFile &file) {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000829 if (_context.outputMachOType() != llvm::MachO::MH_OBJECT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000830 return;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000831
Nick Kledzike34182f2013-11-06 21:36:55 +0000832 for (SectionInfo *si : _sectionInfos) {
833 Section &normSect = file.sections[si->normalizedSectionIndex];
834 for (const AtomInfo &info : si->atomsAndOffsets) {
835 const DefinedAtom *atom = info.atom;
836 for (const Reference *ref : *atom) {
837 appendReloc(atom, ref, normSect.relocations);
838 }
839 }
840 }
841}
842
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000843void Util::addRebaseAndBindingInfo(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000844 NormalizedFile &nFile) {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000845 if (_context.outputMachOType() == llvm::MachO::MH_OBJECT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000846 return;
847
848 uint8_t segmentIndex;
849 uint64_t segmentStartAddr;
850 for (SectionInfo *sect : _sectionInfos) {
851 segIndexForSection(sect, segmentIndex, segmentStartAddr);
852 for (const AtomInfo &info : sect->atomsAndOffsets) {
853 const DefinedAtom *atom = info.atom;
854 for (const Reference *ref : *atom) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000855 uint64_t segmentOffset = _atomToAddress[atom] + ref->offsetInAtom()
Nick Kledzike34182f2013-11-06 21:36:55 +0000856 - segmentStartAddr;
857 const Atom* targ = ref->target();
Nick Kledzike5552772013-12-19 21:58:00 +0000858 if (_context.kindHandler().isPointer(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000859 // A pointer to a DefinedAtom requires rebasing.
860 if (dyn_cast<DefinedAtom>(targ)) {
861 RebaseLocation rebase;
862 rebase.segIndex = segmentIndex;
863 rebase.segOffset = segmentOffset;
864 rebase.kind = llvm::MachO::REBASE_TYPE_POINTER;
865 nFile.rebasingInfo.push_back(rebase);
866 }
867 // A pointer to an SharedLibraryAtom requires binding.
868 if (const SharedLibraryAtom *sa = dyn_cast<SharedLibraryAtom>(targ)) {
869 BindLocation bind;
870 bind.segIndex = segmentIndex;
871 bind.segOffset = segmentOffset;
872 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
873 bind.canBeNull = sa->canBeNullAtRuntime();
874 bind.ordinal = dylibOrdinal(sa);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000875 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000876 bind.addend = ref->addend();
877 nFile.bindingInfo.push_back(bind);
878 }
879 }
Nick Kledzike5552772013-12-19 21:58:00 +0000880 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000881 BindLocation bind;
882 bind.segIndex = segmentIndex;
883 bind.segOffset = segmentOffset;
884 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
885 bind.canBeNull = false; //sa->canBeNullAtRuntime();
886 bind.ordinal = 1;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000887 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000888 bind.addend = ref->addend();
889 nFile.lazyBindingInfo.push_back(bind);
890 }
891 }
892 }
893 }
894}
895
896uint32_t Util::fileFlags() {
897 return 0; //FIX ME
898}
899
900} // end anonymous namespace
901
902
903namespace lld {
904namespace mach_o {
905namespace normalized {
906
907/// Convert a set of Atoms into a normalized mach-o file.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000908ErrorOr<std::unique_ptr<NormalizedFile>>
909normalizedFromAtoms(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000910 const MachOLinkingContext &context) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000911 // The util object buffers info until the normalized file can be made.
Nick Kledzike34182f2013-11-06 21:36:55 +0000912 Util util(context);
913 util.assignAtomsToSections(atomFile);
914 util.organizeSections();
915 util.assignAddressesToSections();
916 util.buildAtomToAddressMap();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000917
Nick Kledzike34182f2013-11-06 21:36:55 +0000918 std::unique_ptr<NormalizedFile> f(new NormalizedFile());
919 NormalizedFile &normFile = *f.get();
920 f->arch = context.arch();
Tim Northoverd30a1f22014-06-20 15:59:00 +0000921 f->fileType = context.outputMachOType();
Nick Kledzike34182f2013-11-06 21:36:55 +0000922 f->flags = util.fileFlags();
923 util.copySegmentInfo(normFile);
924 util.copySections(normFile);
925 util.addDependentDylibs(atomFile, normFile);
926 util.addSymbols(atomFile, normFile);
927 util.addIndirectSymbols(atomFile, normFile);
928 util.addRebaseAndBindingInfo(atomFile, normFile);
929 util.addSectionRelocs(atomFile, normFile);
930 util.copyEntryPointAddress(normFile);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000931
Nick Kledzike34182f2013-11-06 21:36:55 +0000932 return std::move(f);
933}
934
935
936} // namespace normalized
937} // namespace mach_o
938} // namespace lld
939