blob: 239e3c3230e77d82721dbf0d3094b38d5b79cecd [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);
124 int dylibOrdinal(const SharedLibraryAtom *sa);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000125 void segIndexForSection(const SectionInfo *sect,
Nick Kledzike34182f2013-11-06 21:36:55 +0000126 uint8_t &segmentIndex, uint64_t &segmentStartAddr);
127 const Atom *targetOfLazyPointer(const DefinedAtom *lpAtom);
128 const Atom *targetOfStub(const DefinedAtom *stubAtom);
129 bool belongsInGlobalSymbolsSection(const DefinedAtom* atom);
130 void appendSection(SectionInfo *si, NormalizedFile &file);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000131 void appendReloc(const DefinedAtom *atom, const Reference *ref,
Nick Kledzike34182f2013-11-06 21:36:55 +0000132 Relocations &relocations);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000133
Nick Kledzike34182f2013-11-06 21:36:55 +0000134 static uint64_t alignTo(uint64_t value, uint8_t align2);
135 typedef llvm::DenseMap<const Atom*, uint32_t> AtomToIndex;
136 struct AtomAndIndex { const Atom *atom; uint32_t index; };
Joey Gouly9d263e02013-12-25 19:39:08 +0000137 struct AtomSorter {
138 bool operator()(const AtomAndIndex &left, const AtomAndIndex &right);
Nick Kledzike34182f2013-11-06 21:36:55 +0000139 };
Joey Gouly9d263e02013-12-25 19:39:08 +0000140 struct SegmentSorter {
141 bool operator()(const SegmentInfo *left, const SegmentInfo *right);
Nick Kledzike34182f2013-11-06 21:36:55 +0000142 static unsigned weight(const SegmentInfo *);
143 };
Joey Gouly9d263e02013-12-25 19:39:08 +0000144 struct TextSectionSorter {
145 bool operator()(const SectionInfo *left, const SectionInfo *right);
Nick Kledzike34182f2013-11-06 21:36:55 +0000146 static unsigned weight(const SectionInfo *);
147 };
148
149 const MachOLinkingContext &_context;
150 llvm::BumpPtrAllocator _allocator;
151 std::vector<SectionInfo*> _sectionInfos;
152 std::vector<SegmentInfo*> _segmentInfos;
153 TypeToSection _sectionMap;
Nick Kledzikacfad802014-05-30 22:51:04 +0000154 std::vector<SectionInfo*> _customSections;
Nick Kledzike34182f2013-11-06 21:36:55 +0000155 AtomToAddress _atomToAddress;
156 DylibPathToInfo _dylibInfo;
157 const DefinedAtom *_entryAtom;
158 AtomToIndex _atomToSymbolIndex;
159};
160
Nick Kledzikec140832014-06-10 01:50:00 +0000161
162SectionInfo *Util::makeRelocatableSection(DefinedAtom::ContentType type) {
163 StringRef segmentName;
164 StringRef sectionName;
165 SectionType sectionType;
166 SectionAttr sectionAttrs;
167
168 // Use same table used by when parsing .o files.
169 relocatableSectionInfoForContentType(type, segmentName, sectionName,
170 sectionType, sectionAttrs);
171 // If we already have a SectionInfo with this name, re-use it.
172 // This can happen if two ContentType map to the same mach-o section.
173 for (auto sect : _sectionMap) {
174 if (sect.second->sectionName.equals(sectionName) &&
175 sect.second->segmentName.equals(segmentName)) {
176 return sect.second;
177 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000178 }
Nick Kledzikec140832014-06-10 01:50:00 +0000179 // Otherwise allocate new SectionInfo object.
180 return new (_allocator) SectionInfo(segmentName, sectionName, sectionType,
181 sectionAttrs);
182}
183
184#define ENTRY(seg, sect, type, atomType) \
185 {seg, sect, type, DefinedAtom::atomType }
186
187struct MachOFinalSectionFromAtomType {
188 StringRef segmentName;
189 StringRef sectionName;
190 SectionType sectionType;
191 DefinedAtom::ContentType atomType;
192};
193
194const MachOFinalSectionFromAtomType sectsToAtomType[] = {
195 ENTRY("__TEXT", "__text", S_REGULAR, typeCode),
196 ENTRY("__TEXT", "__cstring", S_CSTRING_LITERALS, typeCString),
197 ENTRY("__TEXT", "__ustring", S_REGULAR, typeUTF16String),
198 ENTRY("__TEXT", "__const", S_REGULAR, typeConstant),
199 ENTRY("__TEXT", "__const", S_4BYTE_LITERALS, typeLiteral4),
200 ENTRY("__TEXT", "__const", S_8BYTE_LITERALS, typeLiteral8),
201 ENTRY("__TEXT", "__const", S_16BYTE_LITERALS, typeLiteral16),
202 ENTRY("__TEXT", "__stubs", S_SYMBOL_STUBS, typeStub),
203 ENTRY("__TEXT", "__stub_helper", S_REGULAR, typeStubHelper),
204 ENTRY("__TEXT", "__gcc_except_tab", S_REGULAR, typeLSDA),
205 ENTRY("__TEXT", "__eh_frame", S_COALESCED, typeCFI),
206 ENTRY("__DATA", "__data", S_REGULAR, typeData),
207 ENTRY("__DATA", "__const", S_REGULAR, typeConstData),
208 ENTRY("__DATA", "__cfstring", S_REGULAR, typeCFString),
209 ENTRY("__DATA", "__la_symbol_ptr", S_LAZY_SYMBOL_POINTERS,
210 typeLazyPointer),
211 ENTRY("__DATA", "__mod_init_func", S_MOD_INIT_FUNC_POINTERS,
212 typeInitializerPtr),
213 ENTRY("__DATA", "__mod_term_func", S_MOD_TERM_FUNC_POINTERS,
214 typeTerminatorPtr),
215 ENTRY("__DATA", "___got", S_NON_LAZY_SYMBOL_POINTERS,
216 typeGOT),
217 ENTRY("__DATA", "___bss", S_ZEROFILL, typeZeroFill)
218};
219#undef ENTRY
220
221
222SectionInfo *Util::makeFinalSection(DefinedAtom::ContentType atomType) {
223 for (const MachOFinalSectionFromAtomType *p = sectsToAtomType ;
224 p->atomType != DefinedAtom::typeUnknown; ++p) {
225 if (p->atomType != atomType)
226 continue;
227 SectionAttr sectionAttrs = 0;
228 switch (atomType) {
229 case DefinedAtom::typeCode:
230 case DefinedAtom::typeStub:
231 sectionAttrs = S_ATTR_PURE_INSTRUCTIONS;
232 break;
233 default:
234 break;
235 }
236 // If we already have a SectionInfo with this name, re-use it.
237 // This can happen if two ContentType map to the same mach-o section.
238 for (auto sect : _sectionMap) {
239 if (sect.second->sectionName.equals(p->sectionName) &&
240 sect.second->segmentName.equals(p->segmentName)) {
241 return sect.second;
242 }
243 }
244 // Otherwise allocate new SectionInfo object.
245 return new (_allocator) SectionInfo(p->segmentName, p->sectionName,
246 p->sectionType, sectionAttrs);
247 }
248 llvm_unreachable("content type not yet supported");
Nick Kledzike34182f2013-11-06 21:36:55 +0000249}
250
251
252
253SectionInfo *Util::sectionForAtom(const DefinedAtom *atom) {
Nick Kledzikacfad802014-05-30 22:51:04 +0000254 if (atom->sectionChoice() == DefinedAtom::sectionBasedOnContent) {
255 // Section for this atom is derived from content type.
256 DefinedAtom::ContentType type = atom->contentType();
257 auto pos = _sectionMap.find(type);
258 if ( pos != _sectionMap.end() )
259 return pos->second;
Nick Kledzikec140832014-06-10 01:50:00 +0000260 bool rMode = (_context.outputFileType() == llvm::MachO::MH_OBJECT);
261 SectionInfo *si = rMode ? makeRelocatableSection(type)
262 : makeFinalSection(type);
Nick Kledzikacfad802014-05-30 22:51:04 +0000263 _sectionInfos.push_back(si);
264 _sectionMap[type] = si;
265 return si;
266 } else {
267 // This atom needs to be in a custom section.
268 StringRef customName = atom->customSectionName();
269 // Look to see if we have already allocated the needed custom section.
270 for(SectionInfo *sect : _customSections) {
271 const DefinedAtom *firstAtom = sect->atomsAndOffsets.front().atom;
272 if (firstAtom->customSectionName().equals(customName)) {
273 return sect;
274 }
275 }
276 // Not found, so need to create a new custom section.
277 size_t seperatorIndex = customName.find('/');
278 assert(seperatorIndex != StringRef::npos);
279 StringRef segName = customName.slice(0, seperatorIndex-1);
280 StringRef sectName = customName.drop_front(seperatorIndex);
281 SectionInfo *sect = new (_allocator) SectionInfo(segName, sectName,
282 S_REGULAR);
283 _customSections.push_back(sect);
284 return sect;
285 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000286}
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000287
Nick Kledzike34182f2013-11-06 21:36:55 +0000288
289void Util::appendAtom(SectionInfo *sect, const DefinedAtom *atom) {
290 // Figure out offset for atom in this section given alignment constraints.
291 uint64_t offset = sect->size;
292 DefinedAtom::Alignment atomAlign = atom->alignment();
293 uint64_t align2 = 1 << atomAlign.powerOf2;
294 uint64_t requiredModulus = atomAlign.modulus;
295 uint64_t currentModulus = (offset % align2);
296 if ( currentModulus != requiredModulus ) {
297 if ( requiredModulus > currentModulus )
298 offset += requiredModulus-currentModulus;
299 else
300 offset += align2+requiredModulus-currentModulus;
301 }
302 // Record max alignment of any atom in this section.
303 if ( atomAlign.powerOf2 > sect->alignment )
304 sect->alignment = atomAlign.powerOf2;
305 // Assign atom to this section with this offset.
306 AtomInfo ai = {atom, offset};
307 sect->atomsAndOffsets.push_back(ai);
308 // Update section size to include this atom.
309 sect->size = offset + atom->size();
310}
311
312void Util::assignAtomsToSections(const lld::File &atomFile) {
313 for (const DefinedAtom *atom : atomFile.defined()) {
314 appendAtom(sectionForAtom(atom), atom);
315 }
316}
317
318SegmentInfo *Util::segmentForName(StringRef segName) {
319 for (SegmentInfo *si : _segmentInfos) {
320 if ( si->name.equals(segName) )
321 return si;
322 }
323 SegmentInfo *info = new (_allocator) SegmentInfo(segName);
324 if (segName.equals("__TEXT"))
325 info->access = VM_PROT_READ | VM_PROT_EXECUTE;
326 else if (segName.equals("__DATA"))
327 info->access = VM_PROT_READ | VM_PROT_WRITE;
328 else if (segName.equals("__PAGEZERO"))
329 info->access = 0;
330 _segmentInfos.push_back(info);
331 return info;
332}
333
334unsigned Util::SegmentSorter::weight(const SegmentInfo *seg) {
335 return llvm::StringSwitch<unsigned>(seg->name)
336 .Case("__PAGEZERO", 1)
337 .Case("__TEXT", 2)
338 .Case("__DATA", 3)
339 .Default(100);
340}
341
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000342bool Util::SegmentSorter::operator()(const SegmentInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000343 const SegmentInfo *right) {
344 return (weight(left) < weight(right));
345}
346
347unsigned Util::TextSectionSorter::weight(const SectionInfo *sect) {
348 return llvm::StringSwitch<unsigned>(sect->sectionName)
349 .Case("__text", 1)
350 .Case("__stubs", 2)
351 .Case("__stub_helper", 3)
352 .Case("__const", 4)
353 .Case("__cstring", 5)
354 .Case("__unwind_info", 98)
355 .Case("__eh_frame", 99)
356 .Default(10);
357}
358
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000359bool Util::TextSectionSorter::operator()(const SectionInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000360 const SectionInfo *right) {
361 return (weight(left) < weight(right));
362}
363
364
365void Util::organizeSections() {
366 if (_context.outputFileType() == llvm::MachO::MH_OBJECT) {
367 // Leave sections ordered as normalized file specified.
368 uint32_t sectionIndex = 1;
369 for (SectionInfo *si : _sectionInfos) {
370 si->finalSectionIndex = sectionIndex++;
371 }
372 } else {
373 // Main executables, need a zero-page segment
374 if (_context.outputFileType() == llvm::MachO::MH_EXECUTE)
375 segmentForName("__PAGEZERO");
376 // Group sections into segments.
377 for (SectionInfo *si : _sectionInfos) {
378 SegmentInfo *seg = segmentForName(si->segmentName);
379 seg->sections.push_back(si);
380 }
381 // Sort segments.
382 std::sort(_segmentInfos.begin(), _segmentInfos.end(), SegmentSorter());
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000383
Nick Kledzike34182f2013-11-06 21:36:55 +0000384 // Sort sections within segments.
385 for (SegmentInfo *seg : _segmentInfos) {
386 if (seg->name.equals("__TEXT")) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000387 std::sort(seg->sections.begin(), seg->sections.end(),
Nick Kledzike34182f2013-11-06 21:36:55 +0000388 TextSectionSorter());
389 }
390 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000391
Nick Kledzike34182f2013-11-06 21:36:55 +0000392 // Record final section indexes.
393 uint32_t sectionIndex = 1;
394 for (SegmentInfo *seg : _segmentInfos) {
395 for (SectionInfo *sect : seg->sections) {
396 sect->finalSectionIndex = sectionIndex++;
397 }
398 }
399 }
400
401}
402
403uint64_t Util::alignTo(uint64_t value, uint8_t align2) {
404 return llvm::RoundUpToAlignment(value, 1 << align2);
405}
406
407
408void Util::layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr) {
409 seg->address = addr;
410 for (SectionInfo *sect : seg->sections) {
411 sect->address = alignTo(addr, sect->alignment);
412 addr += sect->size;
413 }
414 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
415}
416
417
418// __TEXT segment lays out backwards so padding is at front after load commands.
419void Util::layoutSectionsInTextSegment(SegmentInfo *seg, uint64_t &addr) {
420 seg->address = addr;
421 // Walks sections starting at end to calculate padding for start.
422 int64_t taddr = 0;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000423 for (auto it = seg->sections.rbegin(); it != seg->sections.rend(); ++it) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000424 SectionInfo *sect = *it;
425 taddr -= sect->size;
426 taddr = taddr & (0 - (1 << sect->alignment));
427 }
428 int64_t padding = taddr;
429 while (padding < 0)
430 padding += _context.pageSize();
431 // Start assigning section address starting at padded offset.
432 addr += padding;
433 for (SectionInfo *sect : seg->sections) {
434 sect->address = alignTo(addr, sect->alignment);
435 addr = sect->address + sect->size;
436 }
437 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
438}
439
440
441void Util::assignAddressesToSections() {
442 uint64_t address = 0; // FIXME
443 if (_context.outputFileType() != llvm::MachO::MH_OBJECT) {
444 for (SegmentInfo *seg : _segmentInfos) {
445 if (seg->name.equals("__PAGEZERO")) {
446 seg->size = _context.pageZeroSize();
447 address += seg->size;
448 }
449 else if (seg->name.equals("__TEXT"))
450 layoutSectionsInTextSegment(seg, address);
451 else
452 layoutSectionsInSegment(seg, address);
453 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000454 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000455 llvm::dbgs() << "assignAddressesToSections()\n";
456 for (SegmentInfo *sgi : _segmentInfos) {
457 llvm::dbgs() << " address=" << llvm::format("0x%08llX", sgi->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000458 << ", size=" << llvm::format("0x%08llX", sgi->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000459 << ", segment-name='" << sgi->name
Nick Kledzik020a49c2013-11-06 21:57:52 +0000460 << "'\n";
461 for (SectionInfo *si : sgi->sections) {
462 llvm::dbgs()<< " addr=" << llvm::format("0x%08llX", si->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000463 << ", size=" << llvm::format("0x%08llX", si->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000464 << ", section-name='" << si->sectionName
Nick Kledzik020a49c2013-11-06 21:57:52 +0000465 << "\n";
466 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000467 }
Nick Kledzik020a49c2013-11-06 21:57:52 +0000468 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000469 } else {
470 for (SectionInfo *sect : _sectionInfos) {
471 sect->address = alignTo(address, sect->alignment);
472 address = sect->address + sect->size;
473 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000474 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000475 llvm::dbgs() << "assignAddressesToSections()\n";
476 for (SectionInfo *si : _sectionInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000477 llvm::dbgs() << " section=" << si->sectionName
Nick Kledzike34182f2013-11-06 21:36:55 +0000478 << " address= " << llvm::format("0x%08X", si->address)
479 << " size= " << llvm::format("0x%08X", si->size)
Nick Kledzik020a49c2013-11-06 21:57:52 +0000480 << "\n";
481 }
482 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000483 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000484}
485
486
487void Util::copySegmentInfo(NormalizedFile &file) {
488 for (SegmentInfo *sgi : _segmentInfos) {
489 Segment seg;
490 seg.name = sgi->name;
491 seg.address = sgi->address;
492 seg.size = sgi->size;
493 seg.access = sgi->access;
494 file.segments.push_back(seg);
495 }
496}
497
498void Util::appendSection(SectionInfo *si, NormalizedFile &file) {
499 // Add new empty section to end of file.sections.
500 Section temp;
501 file.sections.push_back(std::move(temp));
502 Section* normSect = &file.sections.back();
503 // Copy fields to normalized section.
504 normSect->segmentName = si->segmentName;
505 normSect->sectionName = si->sectionName;
506 normSect->type = si->type;
507 normSect->attributes = si->attributes;
508 normSect->address = si->address;
509 normSect->alignment = si->alignment;
510 // Record where normalized section is.
511 si->normalizedSectionIndex = file.sections.size()-1;
512 // Copy content from atoms to content buffer for section.
Nick Kledzik61fdef62014-05-15 20:59:23 +0000513 if (si->type == llvm::MachO::S_ZEROFILL)
514 return;
Nick Kledzik6edd7222014-01-11 01:07:43 +0000515 uint8_t *sectionContent = file.ownedAllocations.Allocate<uint8_t>(si->size);
516 normSect->content = llvm::makeArrayRef(sectionContent, si->size);
Nick Kledzike34182f2013-11-06 21:36:55 +0000517 for (AtomInfo &ai : si->atomsAndOffsets) {
518 // Copy raw bytes.
519 uint8_t *atomContent = reinterpret_cast<uint8_t*>
520 (&sectionContent[ai.offsetInSection]);
521 memcpy(atomContent, ai.atom->rawContent().data(), ai.atom->size());
522 // Apply fix-ups.
523 for (const Reference *ref : *ai.atom) {
524 uint32_t offset = ref->offsetInAtom();
525 uint64_t targetAddress = 0;
526 if ( ref->target() != nullptr )
527 targetAddress = _atomToAddress[ref->target()];
528 uint64_t fixupAddress = _atomToAddress[ai.atom] + offset;
Rui Ueyama170a1a82013-12-20 07:48:29 +0000529 _context.kindHandler().applyFixup(
530 ref->kindNamespace(), ref->kindArch(), ref->kindValue(),
531 ref->addend(), &atomContent[offset], fixupAddress, targetAddress);
Nick Kledzike34182f2013-11-06 21:36:55 +0000532 }
533 }
534}
535
536void Util::copySections(NormalizedFile &file) {
537 file.sections.reserve(_sectionInfos.size());
538 // For final linked images, write sections grouped by segment.
539 if (_context.outputFileType() != llvm::MachO::MH_OBJECT) {
540 for (SegmentInfo *sgi : _segmentInfos) {
541 for (SectionInfo *si : sgi->sections) {
542 appendSection(si, file);
543 }
544 }
545 } else {
546 // Object files write sections in default order.
547 for (SectionInfo *si : _sectionInfos) {
548 appendSection(si, file);
549 }
550 }
551}
552
553void Util::copyEntryPointAddress(NormalizedFile &nFile) {
554 if (_context.outputTypeHasEntry()) {
555 nFile.entryAddress = _atomToAddress[_entryAtom];
556 }
557}
558
559void Util::buildAtomToAddressMap() {
560 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
561 << "assign atom addresses:\n");
562 const bool lookForEntry = _context.outputTypeHasEntry();
563 for (SectionInfo *sect : _sectionInfos) {
564 for (const AtomInfo &info : sect->atomsAndOffsets) {
565 _atomToAddress[info.atom] = sect->address + info.offsetInSection;
566 if (lookForEntry && (info.atom->contentType() == DefinedAtom::typeCode) &&
567 (info.atom->size() != 0) &&
568 info.atom->name() == _context.entrySymbolName()) {
569 _entryAtom = info.atom;
570 }
571 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
572 << " address="
573 << llvm::format("0x%016X", _atomToAddress[info.atom])
574 << " atom=" << info.atom
575 << " name=" << info.atom->name() << "\n");
576 }
577 }
578}
579
580uint8_t Util::scopeBits(const DefinedAtom* atom) {
581 switch (atom->scope()) {
582 case Atom::scopeTranslationUnit:
583 return 0;
584 case Atom::scopeLinkageUnit:
585 return N_PEXT | N_EXT;
586 case Atom::scopeGlobal:
587 return N_EXT;
588 }
Nick Kledzik020fa7f2013-11-06 22:18:09 +0000589 llvm_unreachable("Unknown scope");
Nick Kledzike34182f2013-11-06 21:36:55 +0000590}
591
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000592bool Util::AtomSorter::operator()(const AtomAndIndex &left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000593 const AtomAndIndex &right) {
594 return (left.atom->name().compare(right.atom->name()) < 0);
595}
596
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000597
Nick Kledzike34182f2013-11-06 21:36:55 +0000598bool Util::belongsInGlobalSymbolsSection(const DefinedAtom* atom) {
599 return (atom->scope() == Atom::scopeGlobal);
600}
601
602void Util::addSymbols(const lld::File &atomFile, NormalizedFile &file) {
603 // Mach-O symbol table has three regions: locals, globals, undefs.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000604
Nick Kledzike34182f2013-11-06 21:36:55 +0000605 // Add all local (non-global) symbols in address order
606 std::vector<AtomAndIndex> globals;
607 globals.reserve(512);
608 for (SectionInfo *sect : _sectionInfos) {
609 for (const AtomInfo &info : sect->atomsAndOffsets) {
610 const DefinedAtom *atom = info.atom;
611 if (!atom->name().empty()) {
612 if (belongsInGlobalSymbolsSection(atom)) {
613 AtomAndIndex ai = { atom, sect->finalSectionIndex };
614 globals.push_back(ai);
615 } else {
616 Symbol sym;
617 sym.name = atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000618 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000619 sym.scope = scopeBits(atom);
620 sym.sect = sect->finalSectionIndex;
621 sym.desc = 0;
622 sym.value = _atomToAddress[atom];
623 file.localSymbols.push_back(sym);
624 }
625 }
626 }
627 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000628
Nick Kledzike34182f2013-11-06 21:36:55 +0000629 // Sort global symbol alphabetically, then add to symbol table.
630 std::sort(globals.begin(), globals.end(), AtomSorter());
631 for (AtomAndIndex &ai : globals) {
632 Symbol sym;
633 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000634 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000635 sym.scope = scopeBits(static_cast<const DefinedAtom*>(ai.atom));
636 sym.sect = ai.index;
637 sym.desc = 0;
638 sym.value = _atomToAddress[ai.atom];
639 file.globalSymbols.push_back(sym);
640 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000641
642
Nick Kledzike34182f2013-11-06 21:36:55 +0000643 // Sort undefined symbol alphabetically, then add to symbol table.
644 std::vector<AtomAndIndex> undefs;
645 undefs.reserve(128);
646 for (const UndefinedAtom *atom : atomFile.undefined()) {
647 AtomAndIndex ai = { atom, 0 };
648 undefs.push_back(ai);
649 }
650 for (const SharedLibraryAtom *atom : atomFile.sharedLibrary()) {
651 AtomAndIndex ai = { atom, 0 };
652 undefs.push_back(ai);
653 }
654 std::sort(undefs.begin(), undefs.end(), AtomSorter());
655 const uint32_t start = file.globalSymbols.size() + file.localSymbols.size();
656 for (AtomAndIndex &ai : undefs) {
657 Symbol sym;
658 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000659 sym.type = N_UNDF;
Nick Kledzike34182f2013-11-06 21:36:55 +0000660 sym.scope = N_EXT;
661 sym.sect = 0;
662 sym.desc = 0;
663 sym.value = 0;
664 _atomToSymbolIndex[ai.atom] = file.undefinedSymbols.size() + start;
665 file.undefinedSymbols.push_back(sym);
666 }
667}
668
669const Atom *Util::targetOfLazyPointer(const DefinedAtom *lpAtom) {
670 for (const Reference *ref : *lpAtom) {
Nick Kledzike5552772013-12-19 21:58:00 +0000671 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000672 return ref->target();
673 }
674 }
675 return nullptr;
676}
677
678const Atom *Util::targetOfStub(const DefinedAtom *stubAtom) {
679 for (const Reference *ref : *stubAtom) {
680 if (const Atom *ta = ref->target()) {
681 if (const DefinedAtom *lpAtom = dyn_cast<DefinedAtom>(ta)) {
682 const Atom *target = targetOfLazyPointer(lpAtom);
683 if (target)
684 return target;
685 }
686 }
687 }
688 return nullptr;
689}
690
691
692void Util::addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file) {
693 for (SectionInfo *si : _sectionInfos) {
694 Section &normSect = file.sections[si->normalizedSectionIndex];
695 switch (si->type) {
696 case llvm::MachO::S_NON_LAZY_SYMBOL_POINTERS:
697 for (const AtomInfo &info : si->atomsAndOffsets) {
698 bool foundTarget = false;
699 for (const Reference *ref : *info.atom) {
700 const Atom *target = ref->target();
701 if (target) {
702 if (isa<const SharedLibraryAtom>(target)) {
703 uint32_t index = _atomToSymbolIndex[target];
704 normSect.indirectSymbols.push_back(index);
705 foundTarget = true;
706 } else {
707 normSect.indirectSymbols.push_back(
708 llvm::MachO::INDIRECT_SYMBOL_LOCAL);
709 }
710 }
711 }
712 if (!foundTarget) {
713 normSect.indirectSymbols.push_back(
714 llvm::MachO::INDIRECT_SYMBOL_ABS);
715 }
716 }
717 break;
718 case llvm::MachO::S_LAZY_SYMBOL_POINTERS:
719 for (const AtomInfo &info : si->atomsAndOffsets) {
720 const Atom *target = targetOfLazyPointer(info.atom);
721 if (target) {
722 uint32_t index = _atomToSymbolIndex[target];
723 normSect.indirectSymbols.push_back(index);
724 }
725 }
726 break;
727 case llvm::MachO::S_SYMBOL_STUBS:
728 for (const AtomInfo &info : si->atomsAndOffsets) {
729 const Atom *target = targetOfStub(info.atom);
730 if (target) {
731 uint32_t index = _atomToSymbolIndex[target];
732 normSect.indirectSymbols.push_back(index);
733 }
734 }
735 break;
736 default:
737 break;
738 }
739 }
740
741}
742
743void Util::addDependentDylibs(const lld::File &atomFile,NormalizedFile &nFile) {
744 // Scan all imported symbols and build up list of dylibs they are from.
745 int ordinal = 1;
746 for (const SharedLibraryAtom *slAtom : atomFile.sharedLibrary()) {
747 StringRef loadPath = slAtom->loadName();
748 DylibPathToInfo::iterator pos = _dylibInfo.find(loadPath);
749 if (pos == _dylibInfo.end()) {
750 DylibInfo info;
751 info.ordinal = ordinal++;
752 info.hasWeak = slAtom->canBeNullAtRuntime();
753 info.hasNonWeak = !info.hasWeak;
754 _dylibInfo[loadPath] = info;
755 DependentDylib depInfo;
756 depInfo.path = loadPath;
757 depInfo.kind = llvm::MachO::LC_LOAD_DYLIB;
758 nFile.dependentDylibs.push_back(depInfo);
759 } else {
760 if ( slAtom->canBeNullAtRuntime() )
761 pos->second.hasWeak = true;
762 else
763 pos->second.hasNonWeak = true;
764 }
765 }
766 // Automatically weak link dylib in which all symbols are weak (canBeNull).
767 for (DependentDylib &dep : nFile.dependentDylibs) {
768 DylibInfo &info = _dylibInfo[dep.path];
769 if (info.hasWeak && !info.hasNonWeak)
770 dep.kind = llvm::MachO::LC_LOAD_WEAK_DYLIB;
771 }
772}
773
774
775int Util::dylibOrdinal(const SharedLibraryAtom *sa) {
776 return _dylibInfo[sa->loadName()].ordinal;
777}
778
779void Util::segIndexForSection(const SectionInfo *sect, uint8_t &segmentIndex,
780 uint64_t &segmentStartAddr) {
781 segmentIndex = 0;
782 for (const SegmentInfo *seg : _segmentInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000783 if ((seg->address <= sect->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000784 && (seg->address+seg->size >= sect->address+sect->size)) {
785 segmentStartAddr = seg->address;
786 return;
787 }
788 ++segmentIndex;
789 }
790 llvm_unreachable("section not in any segment");
791}
792
793
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000794void Util::appendReloc(const DefinedAtom *atom, const Reference *ref,
Nick Kledzike34182f2013-11-06 21:36:55 +0000795 Relocations &relocations) {
796 // TODO: convert Reference to normalized relocation
797}
798
799void Util::addSectionRelocs(const lld::File &, NormalizedFile &file) {
800 if (_context.outputFileType() != llvm::MachO::MH_OBJECT)
801 return;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000802
Nick Kledzike34182f2013-11-06 21:36:55 +0000803 for (SectionInfo *si : _sectionInfos) {
804 Section &normSect = file.sections[si->normalizedSectionIndex];
805 for (const AtomInfo &info : si->atomsAndOffsets) {
806 const DefinedAtom *atom = info.atom;
807 for (const Reference *ref : *atom) {
808 appendReloc(atom, ref, normSect.relocations);
809 }
810 }
811 }
812}
813
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000814void Util::addRebaseAndBindingInfo(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000815 NormalizedFile &nFile) {
816 if (_context.outputFileType() == llvm::MachO::MH_OBJECT)
817 return;
818
819 uint8_t segmentIndex;
820 uint64_t segmentStartAddr;
821 for (SectionInfo *sect : _sectionInfos) {
822 segIndexForSection(sect, segmentIndex, segmentStartAddr);
823 for (const AtomInfo &info : sect->atomsAndOffsets) {
824 const DefinedAtom *atom = info.atom;
825 for (const Reference *ref : *atom) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000826 uint64_t segmentOffset = _atomToAddress[atom] + ref->offsetInAtom()
Nick Kledzike34182f2013-11-06 21:36:55 +0000827 - segmentStartAddr;
828 const Atom* targ = ref->target();
Nick Kledzike5552772013-12-19 21:58:00 +0000829 if (_context.kindHandler().isPointer(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000830 // A pointer to a DefinedAtom requires rebasing.
831 if (dyn_cast<DefinedAtom>(targ)) {
832 RebaseLocation rebase;
833 rebase.segIndex = segmentIndex;
834 rebase.segOffset = segmentOffset;
835 rebase.kind = llvm::MachO::REBASE_TYPE_POINTER;
836 nFile.rebasingInfo.push_back(rebase);
837 }
838 // A pointer to an SharedLibraryAtom requires binding.
839 if (const SharedLibraryAtom *sa = dyn_cast<SharedLibraryAtom>(targ)) {
840 BindLocation bind;
841 bind.segIndex = segmentIndex;
842 bind.segOffset = segmentOffset;
843 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
844 bind.canBeNull = sa->canBeNullAtRuntime();
845 bind.ordinal = dylibOrdinal(sa);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000846 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000847 bind.addend = ref->addend();
848 nFile.bindingInfo.push_back(bind);
849 }
850 }
Nick Kledzike5552772013-12-19 21:58:00 +0000851 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000852 BindLocation bind;
853 bind.segIndex = segmentIndex;
854 bind.segOffset = segmentOffset;
855 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
856 bind.canBeNull = false; //sa->canBeNullAtRuntime();
857 bind.ordinal = 1;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000858 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000859 bind.addend = ref->addend();
860 nFile.lazyBindingInfo.push_back(bind);
861 }
862 }
863 }
864 }
865}
866
867uint32_t Util::fileFlags() {
868 return 0; //FIX ME
869}
870
871} // end anonymous namespace
872
873
874namespace lld {
875namespace mach_o {
876namespace normalized {
877
878/// Convert a set of Atoms into a normalized mach-o file.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000879ErrorOr<std::unique_ptr<NormalizedFile>>
880normalizedFromAtoms(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000881 const MachOLinkingContext &context) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000882 // The util object buffers info until the normalized file can be made.
Nick Kledzike34182f2013-11-06 21:36:55 +0000883 Util util(context);
884 util.assignAtomsToSections(atomFile);
885 util.organizeSections();
886 util.assignAddressesToSections();
887 util.buildAtomToAddressMap();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000888
Nick Kledzike34182f2013-11-06 21:36:55 +0000889 std::unique_ptr<NormalizedFile> f(new NormalizedFile());
890 NormalizedFile &normFile = *f.get();
891 f->arch = context.arch();
892 f->fileType = context.outputFileType();
893 f->flags = util.fileFlags();
894 util.copySegmentInfo(normFile);
895 util.copySections(normFile);
896 util.addDependentDylibs(atomFile, normFile);
897 util.addSymbols(atomFile, normFile);
898 util.addIndirectSymbols(atomFile, normFile);
899 util.addRebaseAndBindingInfo(atomFile, normFile);
900 util.addSectionRelocs(atomFile, normFile);
901 util.copyEntryPointAddress(normFile);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000902
Nick Kledzike34182f2013-11-06 21:36:55 +0000903 return std::move(f);
904}
905
906
907} // namespace normalized
908} // namespace mach_o
909} // namespace lld
910