blob: 3befd6a204d04bbc8ea1831eaf14ed3c680bd99c [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),
Tim Northover9ee99352014-06-30 09:49:37 +0000218 ENTRY("__DATA", "___bss", S_ZEROFILL, typeZeroFill),
219
220 // FIXME: __compact_unwind actually needs to be processed by a pass and put
221 // into __TEXT,__unwind_info. For now, forwarding it back to
222 // __LD,__compact_unwind is harmless (it's ignored by the unwinder, which then
223 // proceeds to process __TEXT,__eh_frame for its instructions).
224 ENTRY("__LD", "__compact_unwind", S_REGULAR, typeCompactUnwindInfo),
Nick Kledzikec140832014-06-10 01:50:00 +0000225};
226#undef ENTRY
227
228
Nick Kledzik936d5202014-06-11 01:30:55 +0000229SectionInfo *Util::getFinalSection(DefinedAtom::ContentType atomType) {
Tim Northoverb5bf6862014-06-30 10:30:00 +0000230 for (auto &p : sectsToAtomType) {
231 if (p.atomType != atomType)
Nick Kledzikec140832014-06-10 01:50:00 +0000232 continue;
233 SectionAttr sectionAttrs = 0;
234 switch (atomType) {
235 case DefinedAtom::typeCode:
236 case DefinedAtom::typeStub:
237 sectionAttrs = S_ATTR_PURE_INSTRUCTIONS;
238 break;
239 default:
240 break;
241 }
242 // If we already have a SectionInfo with this name, re-use it.
243 // This can happen if two ContentType map to the same mach-o section.
244 for (auto sect : _sectionMap) {
Tim Northoverb5bf6862014-06-30 10:30:00 +0000245 if (sect.second->sectionName.equals(p.sectionName) &&
246 sect.second->segmentName.equals(p.segmentName)) {
Nick Kledzikec140832014-06-10 01:50:00 +0000247 return sect.second;
248 }
249 }
250 // Otherwise allocate new SectionInfo object.
Tim Northoverb5bf6862014-06-30 10:30:00 +0000251 SectionInfo *sect = new (_allocator) SectionInfo(p.segmentName,
252 p.sectionName,
253 p.sectionType,
Nick Kledzik936d5202014-06-11 01:30:55 +0000254 sectionAttrs);
255 _sectionInfos.push_back(sect);
256 _sectionMap[atomType] = sect;
257 return sect;
Nick Kledzikec140832014-06-10 01:50:00 +0000258 }
259 llvm_unreachable("content type not yet supported");
Nick Kledzike34182f2013-11-06 21:36:55 +0000260}
261
262
263
264SectionInfo *Util::sectionForAtom(const DefinedAtom *atom) {
Nick Kledzikacfad802014-05-30 22:51:04 +0000265 if (atom->sectionChoice() == DefinedAtom::sectionBasedOnContent) {
266 // Section for this atom is derived from content type.
267 DefinedAtom::ContentType type = atom->contentType();
268 auto pos = _sectionMap.find(type);
269 if ( pos != _sectionMap.end() )
270 return pos->second;
Tim Northoverd30a1f22014-06-20 15:59:00 +0000271 bool rMode = (_context.outputMachOType() == llvm::MachO::MH_OBJECT);
Nick Kledzik936d5202014-06-11 01:30:55 +0000272 return rMode ? getRelocatableSection(type) : getFinalSection(type);
Nick Kledzikacfad802014-05-30 22:51:04 +0000273 } else {
274 // This atom needs to be in a custom section.
275 StringRef customName = atom->customSectionName();
276 // Look to see if we have already allocated the needed custom section.
277 for(SectionInfo *sect : _customSections) {
278 const DefinedAtom *firstAtom = sect->atomsAndOffsets.front().atom;
279 if (firstAtom->customSectionName().equals(customName)) {
280 return sect;
281 }
282 }
283 // Not found, so need to create a new custom section.
284 size_t seperatorIndex = customName.find('/');
285 assert(seperatorIndex != StringRef::npos);
Tim Northover7b0a1302014-06-30 09:49:33 +0000286 StringRef segName = customName.slice(0, seperatorIndex);
287 StringRef sectName = customName.drop_front(seperatorIndex + 1);
Nick Kledzikacfad802014-05-30 22:51:04 +0000288 SectionInfo *sect = new (_allocator) SectionInfo(segName, sectName,
289 S_REGULAR);
290 _customSections.push_back(sect);
Tim Northover7b0a1302014-06-30 09:49:33 +0000291 _sectionInfos.push_back(sect);
Nick Kledzikacfad802014-05-30 22:51:04 +0000292 return sect;
293 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000294}
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000295
Nick Kledzike34182f2013-11-06 21:36:55 +0000296
297void Util::appendAtom(SectionInfo *sect, const DefinedAtom *atom) {
298 // Figure out offset for atom in this section given alignment constraints.
299 uint64_t offset = sect->size;
300 DefinedAtom::Alignment atomAlign = atom->alignment();
301 uint64_t align2 = 1 << atomAlign.powerOf2;
302 uint64_t requiredModulus = atomAlign.modulus;
303 uint64_t currentModulus = (offset % align2);
304 if ( currentModulus != requiredModulus ) {
305 if ( requiredModulus > currentModulus )
306 offset += requiredModulus-currentModulus;
307 else
308 offset += align2+requiredModulus-currentModulus;
309 }
310 // Record max alignment of any atom in this section.
311 if ( atomAlign.powerOf2 > sect->alignment )
312 sect->alignment = atomAlign.powerOf2;
313 // Assign atom to this section with this offset.
314 AtomInfo ai = {atom, offset};
315 sect->atomsAndOffsets.push_back(ai);
316 // Update section size to include this atom.
317 sect->size = offset + atom->size();
318}
319
320void Util::assignAtomsToSections(const lld::File &atomFile) {
321 for (const DefinedAtom *atom : atomFile.defined()) {
322 appendAtom(sectionForAtom(atom), atom);
323 }
324}
325
326SegmentInfo *Util::segmentForName(StringRef segName) {
327 for (SegmentInfo *si : _segmentInfos) {
328 if ( si->name.equals(segName) )
329 return si;
330 }
331 SegmentInfo *info = new (_allocator) SegmentInfo(segName);
332 if (segName.equals("__TEXT"))
333 info->access = VM_PROT_READ | VM_PROT_EXECUTE;
334 else if (segName.equals("__DATA"))
335 info->access = VM_PROT_READ | VM_PROT_WRITE;
336 else if (segName.equals("__PAGEZERO"))
337 info->access = 0;
338 _segmentInfos.push_back(info);
339 return info;
340}
341
342unsigned Util::SegmentSorter::weight(const SegmentInfo *seg) {
343 return llvm::StringSwitch<unsigned>(seg->name)
344 .Case("__PAGEZERO", 1)
345 .Case("__TEXT", 2)
346 .Case("__DATA", 3)
347 .Default(100);
348}
349
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000350bool Util::SegmentSorter::operator()(const SegmentInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000351 const SegmentInfo *right) {
352 return (weight(left) < weight(right));
353}
354
355unsigned Util::TextSectionSorter::weight(const SectionInfo *sect) {
356 return llvm::StringSwitch<unsigned>(sect->sectionName)
357 .Case("__text", 1)
358 .Case("__stubs", 2)
359 .Case("__stub_helper", 3)
360 .Case("__const", 4)
361 .Case("__cstring", 5)
362 .Case("__unwind_info", 98)
363 .Case("__eh_frame", 99)
364 .Default(10);
365}
366
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000367bool Util::TextSectionSorter::operator()(const SectionInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000368 const SectionInfo *right) {
369 return (weight(left) < weight(right));
370}
371
372
373void Util::organizeSections() {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000374 if (_context.outputMachOType() == llvm::MachO::MH_OBJECT) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000375 // Leave sections ordered as normalized file specified.
376 uint32_t sectionIndex = 1;
377 for (SectionInfo *si : _sectionInfos) {
378 si->finalSectionIndex = sectionIndex++;
379 }
380 } else {
381 // Main executables, need a zero-page segment
Tim Northoverd30a1f22014-06-20 15:59:00 +0000382 if (_context.outputMachOType() == llvm::MachO::MH_EXECUTE)
Nick Kledzike34182f2013-11-06 21:36:55 +0000383 segmentForName("__PAGEZERO");
384 // Group sections into segments.
385 for (SectionInfo *si : _sectionInfos) {
386 SegmentInfo *seg = segmentForName(si->segmentName);
387 seg->sections.push_back(si);
388 }
389 // Sort segments.
390 std::sort(_segmentInfos.begin(), _segmentInfos.end(), SegmentSorter());
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000391
Nick Kledzike34182f2013-11-06 21:36:55 +0000392 // Sort sections within segments.
393 for (SegmentInfo *seg : _segmentInfos) {
394 if (seg->name.equals("__TEXT")) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000395 std::sort(seg->sections.begin(), seg->sections.end(),
Nick Kledzike34182f2013-11-06 21:36:55 +0000396 TextSectionSorter());
397 }
398 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000399
Nick Kledzike34182f2013-11-06 21:36:55 +0000400 // Record final section indexes.
401 uint32_t sectionIndex = 1;
402 for (SegmentInfo *seg : _segmentInfos) {
403 for (SectionInfo *sect : seg->sections) {
404 sect->finalSectionIndex = sectionIndex++;
405 }
406 }
407 }
408
409}
410
411uint64_t Util::alignTo(uint64_t value, uint8_t align2) {
412 return llvm::RoundUpToAlignment(value, 1 << align2);
413}
414
415
416void Util::layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr) {
417 seg->address = addr;
418 for (SectionInfo *sect : seg->sections) {
419 sect->address = alignTo(addr, sect->alignment);
420 addr += sect->size;
421 }
422 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
423}
424
425
426// __TEXT segment lays out backwards so padding is at front after load commands.
427void Util::layoutSectionsInTextSegment(SegmentInfo *seg, uint64_t &addr) {
428 seg->address = addr;
429 // Walks sections starting at end to calculate padding for start.
430 int64_t taddr = 0;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000431 for (auto it = seg->sections.rbegin(); it != seg->sections.rend(); ++it) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000432 SectionInfo *sect = *it;
433 taddr -= sect->size;
434 taddr = taddr & (0 - (1 << sect->alignment));
435 }
436 int64_t padding = taddr;
437 while (padding < 0)
438 padding += _context.pageSize();
439 // Start assigning section address starting at padded offset.
440 addr += padding;
441 for (SectionInfo *sect : seg->sections) {
442 sect->address = alignTo(addr, sect->alignment);
443 addr = sect->address + sect->size;
444 }
445 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
446}
447
448
449void Util::assignAddressesToSections() {
450 uint64_t address = 0; // FIXME
Tim Northoverd30a1f22014-06-20 15:59:00 +0000451 if (_context.outputMachOType() != llvm::MachO::MH_OBJECT) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000452 for (SegmentInfo *seg : _segmentInfos) {
453 if (seg->name.equals("__PAGEZERO")) {
454 seg->size = _context.pageZeroSize();
455 address += seg->size;
456 }
457 else if (seg->name.equals("__TEXT"))
458 layoutSectionsInTextSegment(seg, address);
459 else
460 layoutSectionsInSegment(seg, address);
Tim Northover7b0a1302014-06-30 09:49:33 +0000461
462 address = llvm::RoundUpToAlignment(address, _context.pageSize());
Nick Kledzike34182f2013-11-06 21:36:55 +0000463 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000464 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000465 llvm::dbgs() << "assignAddressesToSections()\n";
466 for (SegmentInfo *sgi : _segmentInfos) {
467 llvm::dbgs() << " address=" << llvm::format("0x%08llX", sgi->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000468 << ", size=" << llvm::format("0x%08llX", sgi->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000469 << ", segment-name='" << sgi->name
Nick Kledzik020a49c2013-11-06 21:57:52 +0000470 << "'\n";
471 for (SectionInfo *si : sgi->sections) {
472 llvm::dbgs()<< " addr=" << llvm::format("0x%08llX", si->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000473 << ", size=" << llvm::format("0x%08llX", si->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000474 << ", section-name='" << si->sectionName
Nick Kledzik020a49c2013-11-06 21:57:52 +0000475 << "\n";
476 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000477 }
Nick Kledzik020a49c2013-11-06 21:57:52 +0000478 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000479 } else {
480 for (SectionInfo *sect : _sectionInfos) {
481 sect->address = alignTo(address, sect->alignment);
482 address = sect->address + sect->size;
483 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000484 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000485 llvm::dbgs() << "assignAddressesToSections()\n";
486 for (SectionInfo *si : _sectionInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000487 llvm::dbgs() << " section=" << si->sectionName
Nick Kledzike34182f2013-11-06 21:36:55 +0000488 << " address= " << llvm::format("0x%08X", si->address)
489 << " size= " << llvm::format("0x%08X", si->size)
Nick Kledzik020a49c2013-11-06 21:57:52 +0000490 << "\n";
491 }
492 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000493 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000494}
495
496
497void Util::copySegmentInfo(NormalizedFile &file) {
498 for (SegmentInfo *sgi : _segmentInfos) {
499 Segment seg;
500 seg.name = sgi->name;
501 seg.address = sgi->address;
502 seg.size = sgi->size;
503 seg.access = sgi->access;
504 file.segments.push_back(seg);
505 }
506}
507
508void Util::appendSection(SectionInfo *si, NormalizedFile &file) {
Nick Kledzik3f690762014-06-27 18:25:01 +0000509 const bool rMode = (_context.outputMachOType() == llvm::MachO::MH_OBJECT);
Nick Kledzike34182f2013-11-06 21:36:55 +0000510 // Add new empty section to end of file.sections.
511 Section temp;
512 file.sections.push_back(std::move(temp));
513 Section* normSect = &file.sections.back();
514 // Copy fields to normalized section.
515 normSect->segmentName = si->segmentName;
516 normSect->sectionName = si->sectionName;
517 normSect->type = si->type;
518 normSect->attributes = si->attributes;
519 normSect->address = si->address;
520 normSect->alignment = si->alignment;
521 // Record where normalized section is.
522 si->normalizedSectionIndex = file.sections.size()-1;
523 // Copy content from atoms to content buffer for section.
Nick Kledzik61fdef62014-05-15 20:59:23 +0000524 if (si->type == llvm::MachO::S_ZEROFILL)
525 return;
Nick Kledzik6edd7222014-01-11 01:07:43 +0000526 uint8_t *sectionContent = file.ownedAllocations.Allocate<uint8_t>(si->size);
527 normSect->content = llvm::makeArrayRef(sectionContent, si->size);
Nick Kledzike34182f2013-11-06 21:36:55 +0000528 for (AtomInfo &ai : si->atomsAndOffsets) {
529 // Copy raw bytes.
530 uint8_t *atomContent = reinterpret_cast<uint8_t*>
531 (&sectionContent[ai.offsetInSection]);
532 memcpy(atomContent, ai.atom->rawContent().data(), ai.atom->size());
533 // Apply fix-ups.
534 for (const Reference *ref : *ai.atom) {
535 uint32_t offset = ref->offsetInAtom();
536 uint64_t targetAddress = 0;
537 if ( ref->target() != nullptr )
538 targetAddress = _atomToAddress[ref->target()];
Nick Kledzikb6e8ce82014-07-03 02:01:21 +0000539 uint64_t atomAddress = _atomToAddress[ai.atom];
540 uint64_t fixupAddress = atomAddress + offset;
Nick Kledzik3f690762014-06-27 18:25:01 +0000541 if ( rMode ) {
542 // FIXME: Need a handler method to update content for .o file
543 // output and any needed section relocations.
544 } else {
545 _context.kindHandler().applyFixup(
Rui Ueyama170a1a82013-12-20 07:48:29 +0000546 ref->kindNamespace(), ref->kindArch(), ref->kindValue(),
Nick Kledzikb6e8ce82014-07-03 02:01:21 +0000547 ref->addend(), &atomContent[offset], fixupAddress, targetAddress,
548 atomAddress);
Nick Kledzik3f690762014-06-27 18:25:01 +0000549 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000550 }
551 }
552}
553
554void Util::copySections(NormalizedFile &file) {
555 file.sections.reserve(_sectionInfos.size());
556 // For final linked images, write sections grouped by segment.
Tim Northoverd30a1f22014-06-20 15:59:00 +0000557 if (_context.outputMachOType() != llvm::MachO::MH_OBJECT) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000558 for (SegmentInfo *sgi : _segmentInfos) {
559 for (SectionInfo *si : sgi->sections) {
560 appendSection(si, file);
561 }
562 }
563 } else {
564 // Object files write sections in default order.
565 for (SectionInfo *si : _sectionInfos) {
566 appendSection(si, file);
567 }
568 }
569}
570
571void Util::copyEntryPointAddress(NormalizedFile &nFile) {
572 if (_context.outputTypeHasEntry()) {
573 nFile.entryAddress = _atomToAddress[_entryAtom];
574 }
575}
576
577void Util::buildAtomToAddressMap() {
578 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
579 << "assign atom addresses:\n");
580 const bool lookForEntry = _context.outputTypeHasEntry();
581 for (SectionInfo *sect : _sectionInfos) {
582 for (const AtomInfo &info : sect->atomsAndOffsets) {
583 _atomToAddress[info.atom] = sect->address + info.offsetInSection;
584 if (lookForEntry && (info.atom->contentType() == DefinedAtom::typeCode) &&
585 (info.atom->size() != 0) &&
586 info.atom->name() == _context.entrySymbolName()) {
587 _entryAtom = info.atom;
588 }
589 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
590 << " address="
591 << llvm::format("0x%016X", _atomToAddress[info.atom])
592 << " atom=" << info.atom
593 << " name=" << info.atom->name() << "\n");
594 }
595 }
596}
597
598uint8_t Util::scopeBits(const DefinedAtom* atom) {
599 switch (atom->scope()) {
600 case Atom::scopeTranslationUnit:
601 return 0;
602 case Atom::scopeLinkageUnit:
603 return N_PEXT | N_EXT;
604 case Atom::scopeGlobal:
605 return N_EXT;
606 }
Nick Kledzik020fa7f2013-11-06 22:18:09 +0000607 llvm_unreachable("Unknown scope");
Nick Kledzike34182f2013-11-06 21:36:55 +0000608}
609
Nick Kledzik60855392014-06-11 00:24:16 +0000610uint16_t Util::descBits(const DefinedAtom* atom) {
611 uint16_t desc = 0;
612 switch (atom->merge()) {
613 case lld::DefinedAtom::mergeNo:
614 case lld::DefinedAtom::mergeAsTentative:
615 break;
616 case lld::DefinedAtom::mergeAsWeak:
617 case lld::DefinedAtom::mergeAsWeakAndAddressUsed:
618 desc |= N_WEAK_DEF;
619 break;
620 case lld::DefinedAtom::mergeSameNameAndSize:
621 case lld::DefinedAtom::mergeByLargestSection:
622 case lld::DefinedAtom::mergeByContent:
623 llvm_unreachable("Unsupported DefinedAtom::merge()");
624 break;
625 }
626 if (atom->contentType() == lld::DefinedAtom::typeResolver)
627 desc |= N_SYMBOL_RESOLVER;
628 return desc;
629}
630
631
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000632bool Util::AtomSorter::operator()(const AtomAndIndex &left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000633 const AtomAndIndex &right) {
634 return (left.atom->name().compare(right.atom->name()) < 0);
635}
636
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000637
Nick Kledzike34182f2013-11-06 21:36:55 +0000638bool Util::belongsInGlobalSymbolsSection(const DefinedAtom* atom) {
Nick Kledzik936d5202014-06-11 01:30:55 +0000639 // ScopeLinkageUnit symbols are in globals area of symbol table
640 // in object files, but in locals area for final linked images.
Tim Northoverd30a1f22014-06-20 15:59:00 +0000641 if (_context.outputMachOType() == llvm::MachO::MH_OBJECT)
Nick Kledzik936d5202014-06-11 01:30:55 +0000642 return (atom->scope() != Atom::scopeTranslationUnit);
643 else
644 return (atom->scope() == Atom::scopeGlobal);
Nick Kledzike34182f2013-11-06 21:36:55 +0000645}
646
647void Util::addSymbols(const lld::File &atomFile, NormalizedFile &file) {
648 // Mach-O symbol table has three regions: locals, globals, undefs.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000649
Nick Kledzike34182f2013-11-06 21:36:55 +0000650 // Add all local (non-global) symbols in address order
651 std::vector<AtomAndIndex> globals;
652 globals.reserve(512);
653 for (SectionInfo *sect : _sectionInfos) {
654 for (const AtomInfo &info : sect->atomsAndOffsets) {
655 const DefinedAtom *atom = info.atom;
656 if (!atom->name().empty()) {
657 if (belongsInGlobalSymbolsSection(atom)) {
658 AtomAndIndex ai = { atom, sect->finalSectionIndex };
659 globals.push_back(ai);
660 } else {
661 Symbol sym;
662 sym.name = 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(atom);
665 sym.sect = sect->finalSectionIndex;
666 sym.desc = 0;
667 sym.value = _atomToAddress[atom];
668 file.localSymbols.push_back(sym);
669 }
670 }
671 }
672 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000673
Nick Kledzike34182f2013-11-06 21:36:55 +0000674 // Sort global symbol alphabetically, then add to symbol table.
675 std::sort(globals.begin(), globals.end(), AtomSorter());
676 for (AtomAndIndex &ai : globals) {
677 Symbol sym;
678 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000679 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000680 sym.scope = scopeBits(static_cast<const DefinedAtom*>(ai.atom));
681 sym.sect = ai.index;
Nick Kledzik60855392014-06-11 00:24:16 +0000682 sym.desc = descBits(static_cast<const DefinedAtom*>(ai.atom));
Nick Kledzike34182f2013-11-06 21:36:55 +0000683 sym.value = _atomToAddress[ai.atom];
684 file.globalSymbols.push_back(sym);
685 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000686
687
Nick Kledzike34182f2013-11-06 21:36:55 +0000688 // Sort undefined symbol alphabetically, then add to symbol table.
689 std::vector<AtomAndIndex> undefs;
690 undefs.reserve(128);
691 for (const UndefinedAtom *atom : atomFile.undefined()) {
692 AtomAndIndex ai = { atom, 0 };
693 undefs.push_back(ai);
694 }
695 for (const SharedLibraryAtom *atom : atomFile.sharedLibrary()) {
696 AtomAndIndex ai = { atom, 0 };
697 undefs.push_back(ai);
698 }
699 std::sort(undefs.begin(), undefs.end(), AtomSorter());
700 const uint32_t start = file.globalSymbols.size() + file.localSymbols.size();
701 for (AtomAndIndex &ai : undefs) {
702 Symbol sym;
703 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000704 sym.type = N_UNDF;
Nick Kledzike34182f2013-11-06 21:36:55 +0000705 sym.scope = N_EXT;
706 sym.sect = 0;
707 sym.desc = 0;
708 sym.value = 0;
709 _atomToSymbolIndex[ai.atom] = file.undefinedSymbols.size() + start;
710 file.undefinedSymbols.push_back(sym);
711 }
712}
713
714const Atom *Util::targetOfLazyPointer(const DefinedAtom *lpAtom) {
715 for (const Reference *ref : *lpAtom) {
Nick Kledzike5552772013-12-19 21:58:00 +0000716 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000717 return ref->target();
718 }
719 }
720 return nullptr;
721}
722
723const Atom *Util::targetOfStub(const DefinedAtom *stubAtom) {
724 for (const Reference *ref : *stubAtom) {
725 if (const Atom *ta = ref->target()) {
726 if (const DefinedAtom *lpAtom = dyn_cast<DefinedAtom>(ta)) {
727 const Atom *target = targetOfLazyPointer(lpAtom);
728 if (target)
729 return target;
730 }
731 }
732 }
733 return nullptr;
734}
735
736
737void Util::addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file) {
738 for (SectionInfo *si : _sectionInfos) {
739 Section &normSect = file.sections[si->normalizedSectionIndex];
740 switch (si->type) {
741 case llvm::MachO::S_NON_LAZY_SYMBOL_POINTERS:
742 for (const AtomInfo &info : si->atomsAndOffsets) {
743 bool foundTarget = false;
744 for (const Reference *ref : *info.atom) {
745 const Atom *target = ref->target();
746 if (target) {
747 if (isa<const SharedLibraryAtom>(target)) {
748 uint32_t index = _atomToSymbolIndex[target];
749 normSect.indirectSymbols.push_back(index);
750 foundTarget = true;
751 } else {
752 normSect.indirectSymbols.push_back(
753 llvm::MachO::INDIRECT_SYMBOL_LOCAL);
754 }
755 }
756 }
757 if (!foundTarget) {
758 normSect.indirectSymbols.push_back(
759 llvm::MachO::INDIRECT_SYMBOL_ABS);
760 }
761 }
762 break;
763 case llvm::MachO::S_LAZY_SYMBOL_POINTERS:
764 for (const AtomInfo &info : si->atomsAndOffsets) {
765 const Atom *target = targetOfLazyPointer(info.atom);
766 if (target) {
767 uint32_t index = _atomToSymbolIndex[target];
768 normSect.indirectSymbols.push_back(index);
769 }
770 }
771 break;
772 case llvm::MachO::S_SYMBOL_STUBS:
773 for (const AtomInfo &info : si->atomsAndOffsets) {
774 const Atom *target = targetOfStub(info.atom);
775 if (target) {
776 uint32_t index = _atomToSymbolIndex[target];
777 normSect.indirectSymbols.push_back(index);
778 }
779 }
780 break;
781 default:
782 break;
783 }
784 }
785
786}
787
788void Util::addDependentDylibs(const lld::File &atomFile,NormalizedFile &nFile) {
789 // Scan all imported symbols and build up list of dylibs they are from.
790 int ordinal = 1;
791 for (const SharedLibraryAtom *slAtom : atomFile.sharedLibrary()) {
792 StringRef loadPath = slAtom->loadName();
793 DylibPathToInfo::iterator pos = _dylibInfo.find(loadPath);
794 if (pos == _dylibInfo.end()) {
795 DylibInfo info;
796 info.ordinal = ordinal++;
797 info.hasWeak = slAtom->canBeNullAtRuntime();
798 info.hasNonWeak = !info.hasWeak;
799 _dylibInfo[loadPath] = info;
800 DependentDylib depInfo;
801 depInfo.path = loadPath;
802 depInfo.kind = llvm::MachO::LC_LOAD_DYLIB;
803 nFile.dependentDylibs.push_back(depInfo);
804 } else {
805 if ( slAtom->canBeNullAtRuntime() )
806 pos->second.hasWeak = true;
807 else
808 pos->second.hasNonWeak = true;
809 }
810 }
811 // Automatically weak link dylib in which all symbols are weak (canBeNull).
812 for (DependentDylib &dep : nFile.dependentDylibs) {
813 DylibInfo &info = _dylibInfo[dep.path];
814 if (info.hasWeak && !info.hasNonWeak)
815 dep.kind = llvm::MachO::LC_LOAD_WEAK_DYLIB;
816 }
817}
818
819
820int Util::dylibOrdinal(const SharedLibraryAtom *sa) {
821 return _dylibInfo[sa->loadName()].ordinal;
822}
823
824void Util::segIndexForSection(const SectionInfo *sect, uint8_t &segmentIndex,
825 uint64_t &segmentStartAddr) {
826 segmentIndex = 0;
827 for (const SegmentInfo *seg : _segmentInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000828 if ((seg->address <= sect->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000829 && (seg->address+seg->size >= sect->address+sect->size)) {
830 segmentStartAddr = seg->address;
831 return;
832 }
833 ++segmentIndex;
834 }
835 llvm_unreachable("section not in any segment");
836}
837
838
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000839void Util::appendReloc(const DefinedAtom *atom, const Reference *ref,
Nick Kledzike34182f2013-11-06 21:36:55 +0000840 Relocations &relocations) {
841 // TODO: convert Reference to normalized relocation
842}
843
844void Util::addSectionRelocs(const lld::File &, NormalizedFile &file) {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000845 if (_context.outputMachOType() != llvm::MachO::MH_OBJECT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000846 return;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000847
Nick Kledzike34182f2013-11-06 21:36:55 +0000848 for (SectionInfo *si : _sectionInfos) {
849 Section &normSect = file.sections[si->normalizedSectionIndex];
850 for (const AtomInfo &info : si->atomsAndOffsets) {
851 const DefinedAtom *atom = info.atom;
852 for (const Reference *ref : *atom) {
853 appendReloc(atom, ref, normSect.relocations);
854 }
855 }
856 }
857}
858
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000859void Util::addRebaseAndBindingInfo(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000860 NormalizedFile &nFile) {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000861 if (_context.outputMachOType() == llvm::MachO::MH_OBJECT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000862 return;
863
864 uint8_t segmentIndex;
865 uint64_t segmentStartAddr;
866 for (SectionInfo *sect : _sectionInfos) {
867 segIndexForSection(sect, segmentIndex, segmentStartAddr);
868 for (const AtomInfo &info : sect->atomsAndOffsets) {
869 const DefinedAtom *atom = info.atom;
870 for (const Reference *ref : *atom) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000871 uint64_t segmentOffset = _atomToAddress[atom] + ref->offsetInAtom()
Nick Kledzike34182f2013-11-06 21:36:55 +0000872 - segmentStartAddr;
873 const Atom* targ = ref->target();
Nick Kledzike5552772013-12-19 21:58:00 +0000874 if (_context.kindHandler().isPointer(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000875 // A pointer to a DefinedAtom requires rebasing.
876 if (dyn_cast<DefinedAtom>(targ)) {
877 RebaseLocation rebase;
878 rebase.segIndex = segmentIndex;
879 rebase.segOffset = segmentOffset;
880 rebase.kind = llvm::MachO::REBASE_TYPE_POINTER;
881 nFile.rebasingInfo.push_back(rebase);
882 }
883 // A pointer to an SharedLibraryAtom requires binding.
884 if (const SharedLibraryAtom *sa = dyn_cast<SharedLibraryAtom>(targ)) {
885 BindLocation bind;
886 bind.segIndex = segmentIndex;
887 bind.segOffset = segmentOffset;
888 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
889 bind.canBeNull = sa->canBeNullAtRuntime();
890 bind.ordinal = dylibOrdinal(sa);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000891 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000892 bind.addend = ref->addend();
893 nFile.bindingInfo.push_back(bind);
894 }
895 }
Nick Kledzike5552772013-12-19 21:58:00 +0000896 if (_context.kindHandler().isLazyTarget(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000897 BindLocation bind;
898 bind.segIndex = segmentIndex;
899 bind.segOffset = segmentOffset;
900 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
901 bind.canBeNull = false; //sa->canBeNullAtRuntime();
902 bind.ordinal = 1;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000903 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000904 bind.addend = ref->addend();
905 nFile.lazyBindingInfo.push_back(bind);
906 }
907 }
908 }
909 }
910}
911
912uint32_t Util::fileFlags() {
913 return 0; //FIX ME
914}
915
916} // end anonymous namespace
917
918
919namespace lld {
920namespace mach_o {
921namespace normalized {
922
923/// Convert a set of Atoms into a normalized mach-o file.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000924ErrorOr<std::unique_ptr<NormalizedFile>>
925normalizedFromAtoms(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000926 const MachOLinkingContext &context) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000927 // The util object buffers info until the normalized file can be made.
Nick Kledzike34182f2013-11-06 21:36:55 +0000928 Util util(context);
929 util.assignAtomsToSections(atomFile);
930 util.organizeSections();
931 util.assignAddressesToSections();
932 util.buildAtomToAddressMap();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000933
Nick Kledzike34182f2013-11-06 21:36:55 +0000934 std::unique_ptr<NormalizedFile> f(new NormalizedFile());
935 NormalizedFile &normFile = *f.get();
936 f->arch = context.arch();
Tim Northoverd30a1f22014-06-20 15:59:00 +0000937 f->fileType = context.outputMachOType();
Nick Kledzike34182f2013-11-06 21:36:55 +0000938 f->flags = util.fileFlags();
Tim Northover301c4e62014-07-01 08:15:41 +0000939 f->installName = context.installName();
Nick Kledzike34182f2013-11-06 21:36:55 +0000940 util.copySegmentInfo(normFile);
941 util.copySections(normFile);
942 util.addDependentDylibs(atomFile, normFile);
943 util.addSymbols(atomFile, normFile);
944 util.addIndirectSymbols(atomFile, normFile);
945 util.addRebaseAndBindingInfo(atomFile, normFile);
946 util.addSectionRelocs(atomFile, normFile);
947 util.copyEntryPointAddress(normFile);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000948
Nick Kledzike34182f2013-11-06 21:36:55 +0000949 return std::move(f);
950}
951
952
953} // namespace normalized
954} // namespace mach_o
955} // namespace lld
956