blob: 0a3068000014e89575b6c8c2e97c5620f25feb81 [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 Kledzik2458bec2014-07-16 19:49:02 +000024
25#include "ArchHandler.h"
Nick Kledzikec140832014-06-10 01:50:00 +000026#include "MachONormalizedFileBinaryUtils.h"
Nick Kledzik2458bec2014-07-16 19:49:02 +000027
Nick Kledzike34182f2013-11-06 21:36:55 +000028#include "lld/Core/Error.h"
29#include "lld/Core/LLVM.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000030#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"
Nick Kledzike34182f2013-11-06 21:36:55 +000037#include <map>
Rafael Espindola54427cc2014-06-12 17:15:58 +000038#include <system_error>
Nick Kledzike34182f2013-11-06 21:36:55 +000039
40using llvm::StringRef;
Nick Kledzike34182f2013-11-06 21:36:55 +000041using llvm::isa;
42using namespace llvm::MachO;
43using namespace lld::mach_o::normalized;
44using namespace lld;
45
46namespace {
47
48struct AtomInfo {
49 const DefinedAtom *atom;
50 uint64_t offsetInSection;
51};
52
53struct SectionInfo {
54 SectionInfo(StringRef seg, StringRef sect, SectionType type, uint32_t attr=0);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000055
Nick Kledzike34182f2013-11-06 21:36:55 +000056 StringRef segmentName;
57 StringRef sectionName;
58 SectionType type;
59 uint32_t attributes;
60 uint64_t address;
61 uint64_t size;
62 uint32_t alignment;
63 std::vector<AtomInfo> atomsAndOffsets;
64 uint32_t normalizedSectionIndex;
65 uint32_t finalSectionIndex;
66};
67
Shankar Easwaran3d8de472014-01-27 03:09:26 +000068SectionInfo::SectionInfo(StringRef sg, StringRef sct, SectionType t, uint32_t a)
69 : segmentName(sg), sectionName(sct), type(t), attributes(a),
70 address(0), size(0), alignment(0),
Nick Kledzike34182f2013-11-06 21:36:55 +000071 normalizedSectionIndex(0), finalSectionIndex(0) {
72}
73
74struct SegmentInfo {
75 SegmentInfo(StringRef name);
Shankar Easwaran3d8de472014-01-27 03:09:26 +000076
Nick Kledzike34182f2013-11-06 21:36:55 +000077 StringRef name;
78 uint64_t address;
79 uint64_t size;
80 uint32_t access;
81 std::vector<SectionInfo*> sections;
82};
83
Shankar Easwaran3d8de472014-01-27 03:09:26 +000084SegmentInfo::SegmentInfo(StringRef n)
Nick Kledzike34182f2013-11-06 21:36:55 +000085 : name(n), address(0), size(0), access(0) {
86}
87
88
89class Util {
90public:
91 Util(const MachOLinkingContext &ctxt) : _context(ctxt), _entryAtom(nullptr) {}
92
93 void assignAtomsToSections(const lld::File &atomFile);
94 void organizeSections();
95 void assignAddressesToSections();
96 uint32_t fileFlags();
97 void copySegmentInfo(NormalizedFile &file);
98 void copySections(NormalizedFile &file);
99 void buildAtomToAddressMap();
100 void addSymbols(const lld::File &atomFile, NormalizedFile &file);
101 void addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file);
102 void addRebaseAndBindingInfo(const lld::File &, NormalizedFile &file);
103 void addSectionRelocs(const lld::File &, NormalizedFile &file);
104 void addDependentDylibs(const lld::File &, NormalizedFile &file);
105 void copyEntryPointAddress(NormalizedFile &file);
106
107private:
108 typedef std::map<DefinedAtom::ContentType, SectionInfo*> TypeToSection;
109 typedef llvm::DenseMap<const Atom*, uint64_t> AtomToAddress;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000110
Nick Kledzike34182f2013-11-06 21:36:55 +0000111 struct DylibInfo { int ordinal; bool hasWeak; bool hasNonWeak; };
112 typedef llvm::StringMap<DylibInfo> DylibPathToInfo;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000113
Nick Kledzike34182f2013-11-06 21:36:55 +0000114 SectionInfo *sectionForAtom(const DefinedAtom*);
Nick Kledzik936d5202014-06-11 01:30:55 +0000115 SectionInfo *getRelocatableSection(DefinedAtom::ContentType type);
116 SectionInfo *getFinalSection(DefinedAtom::ContentType type);
Nick Kledzike34182f2013-11-06 21:36:55 +0000117 void appendAtom(SectionInfo *sect, const DefinedAtom *atom);
118 SegmentInfo *segmentForName(StringRef segName);
119 void layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr);
120 void layoutSectionsInTextSegment(SegmentInfo *seg, uint64_t &addr);
121 void copySectionContent(SectionInfo *si, ContentBytes &content);
122 uint8_t scopeBits(const DefinedAtom* atom);
Nick Kledzik60855392014-06-11 00:24:16 +0000123 uint16_t descBits(const DefinedAtom* atom);
Nick Kledzike34182f2013-11-06 21:36:55 +0000124 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
Nick Kledzik936d5202014-06-11 01:30:55 +0000162SectionInfo *Util::getRelocatableSection(DefinedAtom::ContentType type) {
Nick Kledzikec140832014-06-10 01:50:00 +0000163 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.
Nick Kledzik936d5202014-06-11 01:30:55 +0000180 SectionInfo *sect = new (_allocator) SectionInfo(segmentName, sectionName,
181 sectionType, sectionAttrs);
182 _sectionInfos.push_back(sect);
183 _sectionMap[type] = sect;
184 return sect;
Nick Kledzikec140832014-06-10 01:50:00 +0000185}
186
187#define ENTRY(seg, sect, type, atomType) \
188 {seg, sect, type, DefinedAtom::atomType }
189
190struct MachOFinalSectionFromAtomType {
191 StringRef segmentName;
192 StringRef sectionName;
193 SectionType sectionType;
194 DefinedAtom::ContentType atomType;
195};
196
197const MachOFinalSectionFromAtomType sectsToAtomType[] = {
198 ENTRY("__TEXT", "__text", S_REGULAR, typeCode),
199 ENTRY("__TEXT", "__cstring", S_CSTRING_LITERALS, typeCString),
200 ENTRY("__TEXT", "__ustring", S_REGULAR, typeUTF16String),
201 ENTRY("__TEXT", "__const", S_REGULAR, typeConstant),
202 ENTRY("__TEXT", "__const", S_4BYTE_LITERALS, typeLiteral4),
203 ENTRY("__TEXT", "__const", S_8BYTE_LITERALS, typeLiteral8),
204 ENTRY("__TEXT", "__const", S_16BYTE_LITERALS, typeLiteral16),
205 ENTRY("__TEXT", "__stubs", S_SYMBOL_STUBS, typeStub),
206 ENTRY("__TEXT", "__stub_helper", S_REGULAR, typeStubHelper),
207 ENTRY("__TEXT", "__gcc_except_tab", S_REGULAR, typeLSDA),
208 ENTRY("__TEXT", "__eh_frame", S_COALESCED, typeCFI),
209 ENTRY("__DATA", "__data", S_REGULAR, typeData),
210 ENTRY("__DATA", "__const", S_REGULAR, typeConstData),
211 ENTRY("__DATA", "__cfstring", S_REGULAR, typeCFString),
212 ENTRY("__DATA", "__la_symbol_ptr", S_LAZY_SYMBOL_POINTERS,
213 typeLazyPointer),
214 ENTRY("__DATA", "__mod_init_func", S_MOD_INIT_FUNC_POINTERS,
215 typeInitializerPtr),
216 ENTRY("__DATA", "__mod_term_func", S_MOD_TERM_FUNC_POINTERS,
217 typeTerminatorPtr),
218 ENTRY("__DATA", "___got", S_NON_LAZY_SYMBOL_POINTERS,
219 typeGOT),
Tim Northover9ee99352014-06-30 09:49:37 +0000220 ENTRY("__DATA", "___bss", S_ZEROFILL, typeZeroFill),
221
222 // FIXME: __compact_unwind actually needs to be processed by a pass and put
223 // into __TEXT,__unwind_info. For now, forwarding it back to
224 // __LD,__compact_unwind is harmless (it's ignored by the unwinder, which then
225 // proceeds to process __TEXT,__eh_frame for its instructions).
226 ENTRY("__LD", "__compact_unwind", S_REGULAR, typeCompactUnwindInfo),
Nick Kledzikec140832014-06-10 01:50:00 +0000227};
228#undef ENTRY
229
230
Nick Kledzik936d5202014-06-11 01:30:55 +0000231SectionInfo *Util::getFinalSection(DefinedAtom::ContentType atomType) {
Tim Northoverb5bf6862014-06-30 10:30:00 +0000232 for (auto &p : sectsToAtomType) {
233 if (p.atomType != atomType)
Nick Kledzikec140832014-06-10 01:50:00 +0000234 continue;
235 SectionAttr sectionAttrs = 0;
236 switch (atomType) {
237 case DefinedAtom::typeCode:
238 case DefinedAtom::typeStub:
239 sectionAttrs = S_ATTR_PURE_INSTRUCTIONS;
240 break;
241 default:
242 break;
243 }
244 // If we already have a SectionInfo with this name, re-use it.
245 // This can happen if two ContentType map to the same mach-o section.
246 for (auto sect : _sectionMap) {
Tim Northoverb5bf6862014-06-30 10:30:00 +0000247 if (sect.second->sectionName.equals(p.sectionName) &&
248 sect.second->segmentName.equals(p.segmentName)) {
Nick Kledzikec140832014-06-10 01:50:00 +0000249 return sect.second;
250 }
251 }
252 // Otherwise allocate new SectionInfo object.
Tim Northoverb5bf6862014-06-30 10:30:00 +0000253 SectionInfo *sect = new (_allocator) SectionInfo(p.segmentName,
254 p.sectionName,
255 p.sectionType,
Nick Kledzik936d5202014-06-11 01:30:55 +0000256 sectionAttrs);
257 _sectionInfos.push_back(sect);
258 _sectionMap[atomType] = sect;
259 return sect;
Nick Kledzikec140832014-06-10 01:50:00 +0000260 }
261 llvm_unreachable("content type not yet supported");
Nick Kledzike34182f2013-11-06 21:36:55 +0000262}
263
264
265
266SectionInfo *Util::sectionForAtom(const DefinedAtom *atom) {
Nick Kledzikacfad802014-05-30 22:51:04 +0000267 if (atom->sectionChoice() == DefinedAtom::sectionBasedOnContent) {
268 // Section for this atom is derived from content type.
269 DefinedAtom::ContentType type = atom->contentType();
270 auto pos = _sectionMap.find(type);
271 if ( pos != _sectionMap.end() )
272 return pos->second;
Tim Northoverd30a1f22014-06-20 15:59:00 +0000273 bool rMode = (_context.outputMachOType() == llvm::MachO::MH_OBJECT);
Nick Kledzik936d5202014-06-11 01:30:55 +0000274 return rMode ? getRelocatableSection(type) : getFinalSection(type);
Nick Kledzikacfad802014-05-30 22:51:04 +0000275 } else {
276 // This atom needs to be in a custom section.
277 StringRef customName = atom->customSectionName();
278 // Look to see if we have already allocated the needed custom section.
279 for(SectionInfo *sect : _customSections) {
280 const DefinedAtom *firstAtom = sect->atomsAndOffsets.front().atom;
281 if (firstAtom->customSectionName().equals(customName)) {
282 return sect;
283 }
284 }
285 // Not found, so need to create a new custom section.
286 size_t seperatorIndex = customName.find('/');
287 assert(seperatorIndex != StringRef::npos);
Tim Northover7b0a1302014-06-30 09:49:33 +0000288 StringRef segName = customName.slice(0, seperatorIndex);
289 StringRef sectName = customName.drop_front(seperatorIndex + 1);
Nick Kledzikacfad802014-05-30 22:51:04 +0000290 SectionInfo *sect = new (_allocator) SectionInfo(segName, sectName,
291 S_REGULAR);
292 _customSections.push_back(sect);
Tim Northover7b0a1302014-06-30 09:49:33 +0000293 _sectionInfos.push_back(sect);
Nick Kledzikacfad802014-05-30 22:51:04 +0000294 return sect;
295 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000296}
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000297
Nick Kledzike34182f2013-11-06 21:36:55 +0000298
299void Util::appendAtom(SectionInfo *sect, const DefinedAtom *atom) {
300 // Figure out offset for atom in this section given alignment constraints.
301 uint64_t offset = sect->size;
302 DefinedAtom::Alignment atomAlign = atom->alignment();
303 uint64_t align2 = 1 << atomAlign.powerOf2;
304 uint64_t requiredModulus = atomAlign.modulus;
305 uint64_t currentModulus = (offset % align2);
306 if ( currentModulus != requiredModulus ) {
307 if ( requiredModulus > currentModulus )
308 offset += requiredModulus-currentModulus;
309 else
310 offset += align2+requiredModulus-currentModulus;
311 }
312 // Record max alignment of any atom in this section.
313 if ( atomAlign.powerOf2 > sect->alignment )
314 sect->alignment = atomAlign.powerOf2;
315 // Assign atom to this section with this offset.
316 AtomInfo ai = {atom, offset};
317 sect->atomsAndOffsets.push_back(ai);
318 // Update section size to include this atom.
319 sect->size = offset + atom->size();
320}
321
322void Util::assignAtomsToSections(const lld::File &atomFile) {
323 for (const DefinedAtom *atom : atomFile.defined()) {
324 appendAtom(sectionForAtom(atom), atom);
325 }
326}
327
328SegmentInfo *Util::segmentForName(StringRef segName) {
329 for (SegmentInfo *si : _segmentInfos) {
330 if ( si->name.equals(segName) )
331 return si;
332 }
333 SegmentInfo *info = new (_allocator) SegmentInfo(segName);
334 if (segName.equals("__TEXT"))
335 info->access = VM_PROT_READ | VM_PROT_EXECUTE;
336 else if (segName.equals("__DATA"))
337 info->access = VM_PROT_READ | VM_PROT_WRITE;
338 else if (segName.equals("__PAGEZERO"))
339 info->access = 0;
340 _segmentInfos.push_back(info);
341 return info;
342}
343
344unsigned Util::SegmentSorter::weight(const SegmentInfo *seg) {
345 return llvm::StringSwitch<unsigned>(seg->name)
346 .Case("__PAGEZERO", 1)
347 .Case("__TEXT", 2)
348 .Case("__DATA", 3)
349 .Default(100);
350}
351
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000352bool Util::SegmentSorter::operator()(const SegmentInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000353 const SegmentInfo *right) {
354 return (weight(left) < weight(right));
355}
356
357unsigned Util::TextSectionSorter::weight(const SectionInfo *sect) {
358 return llvm::StringSwitch<unsigned>(sect->sectionName)
359 .Case("__text", 1)
360 .Case("__stubs", 2)
361 .Case("__stub_helper", 3)
362 .Case("__const", 4)
363 .Case("__cstring", 5)
364 .Case("__unwind_info", 98)
365 .Case("__eh_frame", 99)
366 .Default(10);
367}
368
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000369bool Util::TextSectionSorter::operator()(const SectionInfo *left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000370 const SectionInfo *right) {
371 return (weight(left) < weight(right));
372}
373
374
375void Util::organizeSections() {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000376 if (_context.outputMachOType() == llvm::MachO::MH_OBJECT) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000377 // Leave sections ordered as normalized file specified.
378 uint32_t sectionIndex = 1;
379 for (SectionInfo *si : _sectionInfos) {
380 si->finalSectionIndex = sectionIndex++;
381 }
382 } else {
383 // Main executables, need a zero-page segment
Tim Northoverd30a1f22014-06-20 15:59:00 +0000384 if (_context.outputMachOType() == llvm::MachO::MH_EXECUTE)
Nick Kledzike34182f2013-11-06 21:36:55 +0000385 segmentForName("__PAGEZERO");
386 // Group sections into segments.
387 for (SectionInfo *si : _sectionInfos) {
388 SegmentInfo *seg = segmentForName(si->segmentName);
389 seg->sections.push_back(si);
390 }
391 // Sort segments.
392 std::sort(_segmentInfos.begin(), _segmentInfos.end(), SegmentSorter());
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000393
Nick Kledzike34182f2013-11-06 21:36:55 +0000394 // Sort sections within segments.
395 for (SegmentInfo *seg : _segmentInfos) {
396 if (seg->name.equals("__TEXT")) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000397 std::sort(seg->sections.begin(), seg->sections.end(),
Nick Kledzike34182f2013-11-06 21:36:55 +0000398 TextSectionSorter());
399 }
400 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000401
Nick Kledzike34182f2013-11-06 21:36:55 +0000402 // Record final section indexes.
403 uint32_t sectionIndex = 1;
404 for (SegmentInfo *seg : _segmentInfos) {
405 for (SectionInfo *sect : seg->sections) {
406 sect->finalSectionIndex = sectionIndex++;
407 }
408 }
409 }
410
411}
412
413uint64_t Util::alignTo(uint64_t value, uint8_t align2) {
414 return llvm::RoundUpToAlignment(value, 1 << align2);
415}
416
417
418void Util::layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr) {
419 seg->address = addr;
420 for (SectionInfo *sect : seg->sections) {
421 sect->address = alignTo(addr, sect->alignment);
422 addr += sect->size;
423 }
424 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
425}
426
427
428// __TEXT segment lays out backwards so padding is at front after load commands.
429void Util::layoutSectionsInTextSegment(SegmentInfo *seg, uint64_t &addr) {
430 seg->address = addr;
431 // Walks sections starting at end to calculate padding for start.
432 int64_t taddr = 0;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000433 for (auto it = seg->sections.rbegin(); it != seg->sections.rend(); ++it) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000434 SectionInfo *sect = *it;
435 taddr -= sect->size;
436 taddr = taddr & (0 - (1 << sect->alignment));
437 }
438 int64_t padding = taddr;
439 while (padding < 0)
440 padding += _context.pageSize();
441 // Start assigning section address starting at padded offset.
442 addr += padding;
443 for (SectionInfo *sect : seg->sections) {
444 sect->address = alignTo(addr, sect->alignment);
445 addr = sect->address + sect->size;
446 }
447 seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
448}
449
450
451void Util::assignAddressesToSections() {
452 uint64_t address = 0; // FIXME
Tim Northoverd30a1f22014-06-20 15:59:00 +0000453 if (_context.outputMachOType() != llvm::MachO::MH_OBJECT) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000454 for (SegmentInfo *seg : _segmentInfos) {
455 if (seg->name.equals("__PAGEZERO")) {
456 seg->size = _context.pageZeroSize();
457 address += seg->size;
458 }
459 else if (seg->name.equals("__TEXT"))
460 layoutSectionsInTextSegment(seg, address);
461 else
462 layoutSectionsInSegment(seg, address);
Tim Northover7b0a1302014-06-30 09:49:33 +0000463
464 address = llvm::RoundUpToAlignment(address, _context.pageSize());
Nick Kledzike34182f2013-11-06 21:36:55 +0000465 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000466 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000467 llvm::dbgs() << "assignAddressesToSections()\n";
468 for (SegmentInfo *sgi : _segmentInfos) {
469 llvm::dbgs() << " address=" << llvm::format("0x%08llX", sgi->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000470 << ", size=" << llvm::format("0x%08llX", sgi->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000471 << ", segment-name='" << sgi->name
Nick Kledzik020a49c2013-11-06 21:57:52 +0000472 << "'\n";
473 for (SectionInfo *si : sgi->sections) {
474 llvm::dbgs()<< " addr=" << llvm::format("0x%08llX", si->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000475 << ", size=" << llvm::format("0x%08llX", si->size)
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000476 << ", section-name='" << si->sectionName
Nick Kledzik020a49c2013-11-06 21:57:52 +0000477 << "\n";
478 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000479 }
Nick Kledzik020a49c2013-11-06 21:57:52 +0000480 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000481 } else {
482 for (SectionInfo *sect : _sectionInfos) {
483 sect->address = alignTo(address, sect->alignment);
484 address = sect->address + sect->size;
485 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000486 DEBUG_WITH_TYPE("WriterMachO-norm",
Nick Kledzik020a49c2013-11-06 21:57:52 +0000487 llvm::dbgs() << "assignAddressesToSections()\n";
488 for (SectionInfo *si : _sectionInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000489 llvm::dbgs() << " section=" << si->sectionName
Nick Kledzike34182f2013-11-06 21:36:55 +0000490 << " address= " << llvm::format("0x%08X", si->address)
491 << " size= " << llvm::format("0x%08X", si->size)
Nick Kledzik020a49c2013-11-06 21:57:52 +0000492 << "\n";
493 }
494 );
Nick Kledzike34182f2013-11-06 21:36:55 +0000495 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000496}
497
498
499void Util::copySegmentInfo(NormalizedFile &file) {
500 for (SegmentInfo *sgi : _segmentInfos) {
501 Segment seg;
502 seg.name = sgi->name;
503 seg.address = sgi->address;
504 seg.size = sgi->size;
505 seg.access = sgi->access;
506 file.segments.push_back(seg);
507 }
508}
509
510void Util::appendSection(SectionInfo *si, NormalizedFile &file) {
Nick Kledzik3f690762014-06-27 18:25:01 +0000511 const bool rMode = (_context.outputMachOType() == llvm::MachO::MH_OBJECT);
Nick Kledzike34182f2013-11-06 21:36:55 +0000512 // Add new empty section to end of file.sections.
513 Section temp;
514 file.sections.push_back(std::move(temp));
515 Section* normSect = &file.sections.back();
516 // Copy fields to normalized section.
517 normSect->segmentName = si->segmentName;
518 normSect->sectionName = si->sectionName;
519 normSect->type = si->type;
520 normSect->attributes = si->attributes;
521 normSect->address = si->address;
522 normSect->alignment = si->alignment;
523 // Record where normalized section is.
524 si->normalizedSectionIndex = file.sections.size()-1;
525 // Copy content from atoms to content buffer for section.
Nick Kledzik61fdef62014-05-15 20:59:23 +0000526 if (si->type == llvm::MachO::S_ZEROFILL)
527 return;
Nick Kledzik6edd7222014-01-11 01:07:43 +0000528 uint8_t *sectionContent = file.ownedAllocations.Allocate<uint8_t>(si->size);
529 normSect->content = llvm::makeArrayRef(sectionContent, si->size);
Nick Kledzike34182f2013-11-06 21:36:55 +0000530 for (AtomInfo &ai : si->atomsAndOffsets) {
531 // Copy raw bytes.
532 uint8_t *atomContent = reinterpret_cast<uint8_t*>
533 (&sectionContent[ai.offsetInSection]);
534 memcpy(atomContent, ai.atom->rawContent().data(), ai.atom->size());
535 // Apply fix-ups.
536 for (const Reference *ref : *ai.atom) {
537 uint32_t offset = ref->offsetInAtom();
538 uint64_t targetAddress = 0;
539 if ( ref->target() != nullptr )
540 targetAddress = _atomToAddress[ref->target()];
Nick Kledzikb6e8ce82014-07-03 02:01:21 +0000541 uint64_t atomAddress = _atomToAddress[ai.atom];
542 uint64_t fixupAddress = atomAddress + offset;
Nick Kledzik3f690762014-06-27 18:25:01 +0000543 if ( rMode ) {
544 // FIXME: Need a handler method to update content for .o file
545 // output and any needed section relocations.
546 } else {
Nick Kledzik2458bec2014-07-16 19:49:02 +0000547 _context.archHandler().applyFixup(
Rui Ueyama170a1a82013-12-20 07:48:29 +0000548 ref->kindNamespace(), ref->kindArch(), ref->kindValue(),
Nick Kledzikb6e8ce82014-07-03 02:01:21 +0000549 ref->addend(), &atomContent[offset], fixupAddress, targetAddress,
550 atomAddress);
Nick Kledzik3f690762014-06-27 18:25:01 +0000551 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000552 }
553 }
554}
555
556void Util::copySections(NormalizedFile &file) {
557 file.sections.reserve(_sectionInfos.size());
558 // For final linked images, write sections grouped by segment.
Tim Northoverd30a1f22014-06-20 15:59:00 +0000559 if (_context.outputMachOType() != llvm::MachO::MH_OBJECT) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000560 for (SegmentInfo *sgi : _segmentInfos) {
561 for (SectionInfo *si : sgi->sections) {
562 appendSection(si, file);
563 }
564 }
565 } else {
566 // Object files write sections in default order.
567 for (SectionInfo *si : _sectionInfos) {
568 appendSection(si, file);
569 }
570 }
571}
572
573void Util::copyEntryPointAddress(NormalizedFile &nFile) {
574 if (_context.outputTypeHasEntry()) {
575 nFile.entryAddress = _atomToAddress[_entryAtom];
576 }
577}
578
579void Util::buildAtomToAddressMap() {
580 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
581 << "assign atom addresses:\n");
582 const bool lookForEntry = _context.outputTypeHasEntry();
583 for (SectionInfo *sect : _sectionInfos) {
584 for (const AtomInfo &info : sect->atomsAndOffsets) {
585 _atomToAddress[info.atom] = sect->address + info.offsetInSection;
586 if (lookForEntry && (info.atom->contentType() == DefinedAtom::typeCode) &&
587 (info.atom->size() != 0) &&
588 info.atom->name() == _context.entrySymbolName()) {
589 _entryAtom = info.atom;
590 }
591 DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs()
592 << " address="
593 << llvm::format("0x%016X", _atomToAddress[info.atom])
594 << " atom=" << info.atom
595 << " name=" << info.atom->name() << "\n");
596 }
597 }
598}
599
600uint8_t Util::scopeBits(const DefinedAtom* atom) {
601 switch (atom->scope()) {
602 case Atom::scopeTranslationUnit:
603 return 0;
604 case Atom::scopeLinkageUnit:
605 return N_PEXT | N_EXT;
606 case Atom::scopeGlobal:
607 return N_EXT;
608 }
Nick Kledzik020fa7f2013-11-06 22:18:09 +0000609 llvm_unreachable("Unknown scope");
Nick Kledzike34182f2013-11-06 21:36:55 +0000610}
611
Nick Kledzik60855392014-06-11 00:24:16 +0000612uint16_t Util::descBits(const DefinedAtom* atom) {
613 uint16_t desc = 0;
614 switch (atom->merge()) {
615 case lld::DefinedAtom::mergeNo:
616 case lld::DefinedAtom::mergeAsTentative:
617 break;
618 case lld::DefinedAtom::mergeAsWeak:
619 case lld::DefinedAtom::mergeAsWeakAndAddressUsed:
620 desc |= N_WEAK_DEF;
621 break;
622 case lld::DefinedAtom::mergeSameNameAndSize:
623 case lld::DefinedAtom::mergeByLargestSection:
624 case lld::DefinedAtom::mergeByContent:
625 llvm_unreachable("Unsupported DefinedAtom::merge()");
626 break;
627 }
628 if (atom->contentType() == lld::DefinedAtom::typeResolver)
629 desc |= N_SYMBOL_RESOLVER;
630 return desc;
631}
632
633
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000634bool Util::AtomSorter::operator()(const AtomAndIndex &left,
Nick Kledzike34182f2013-11-06 21:36:55 +0000635 const AtomAndIndex &right) {
636 return (left.atom->name().compare(right.atom->name()) < 0);
637}
638
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000639
Nick Kledzike34182f2013-11-06 21:36:55 +0000640bool Util::belongsInGlobalSymbolsSection(const DefinedAtom* atom) {
Nick Kledzik936d5202014-06-11 01:30:55 +0000641 // ScopeLinkageUnit symbols are in globals area of symbol table
642 // in object files, but in locals area for final linked images.
Tim Northoverd30a1f22014-06-20 15:59:00 +0000643 if (_context.outputMachOType() == llvm::MachO::MH_OBJECT)
Nick Kledzik936d5202014-06-11 01:30:55 +0000644 return (atom->scope() != Atom::scopeTranslationUnit);
645 else
646 return (atom->scope() == Atom::scopeGlobal);
Nick Kledzike34182f2013-11-06 21:36:55 +0000647}
648
649void Util::addSymbols(const lld::File &atomFile, NormalizedFile &file) {
650 // Mach-O symbol table has three regions: locals, globals, undefs.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000651
Nick Kledzike34182f2013-11-06 21:36:55 +0000652 // Add all local (non-global) symbols in address order
653 std::vector<AtomAndIndex> globals;
654 globals.reserve(512);
655 for (SectionInfo *sect : _sectionInfos) {
656 for (const AtomInfo &info : sect->atomsAndOffsets) {
657 const DefinedAtom *atom = info.atom;
658 if (!atom->name().empty()) {
659 if (belongsInGlobalSymbolsSection(atom)) {
660 AtomAndIndex ai = { atom, sect->finalSectionIndex };
661 globals.push_back(ai);
662 } else {
663 Symbol sym;
664 sym.name = atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000665 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000666 sym.scope = scopeBits(atom);
667 sym.sect = sect->finalSectionIndex;
668 sym.desc = 0;
669 sym.value = _atomToAddress[atom];
670 file.localSymbols.push_back(sym);
671 }
672 }
673 }
674 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000675
Nick Kledzike34182f2013-11-06 21:36:55 +0000676 // Sort global symbol alphabetically, then add to symbol table.
677 std::sort(globals.begin(), globals.end(), AtomSorter());
678 for (AtomAndIndex &ai : globals) {
679 Symbol sym;
680 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000681 sym.type = N_SECT;
Nick Kledzike34182f2013-11-06 21:36:55 +0000682 sym.scope = scopeBits(static_cast<const DefinedAtom*>(ai.atom));
683 sym.sect = ai.index;
Nick Kledzik60855392014-06-11 00:24:16 +0000684 sym.desc = descBits(static_cast<const DefinedAtom*>(ai.atom));
Nick Kledzike34182f2013-11-06 21:36:55 +0000685 sym.value = _atomToAddress[ai.atom];
686 file.globalSymbols.push_back(sym);
687 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000688
689
Nick Kledzike34182f2013-11-06 21:36:55 +0000690 // Sort undefined symbol alphabetically, then add to symbol table.
691 std::vector<AtomAndIndex> undefs;
692 undefs.reserve(128);
693 for (const UndefinedAtom *atom : atomFile.undefined()) {
694 AtomAndIndex ai = { atom, 0 };
695 undefs.push_back(ai);
696 }
697 for (const SharedLibraryAtom *atom : atomFile.sharedLibrary()) {
698 AtomAndIndex ai = { atom, 0 };
699 undefs.push_back(ai);
700 }
701 std::sort(undefs.begin(), undefs.end(), AtomSorter());
702 const uint32_t start = file.globalSymbols.size() + file.localSymbols.size();
703 for (AtomAndIndex &ai : undefs) {
704 Symbol sym;
705 sym.name = ai.atom->name();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000706 sym.type = N_UNDF;
Nick Kledzike34182f2013-11-06 21:36:55 +0000707 sym.scope = N_EXT;
708 sym.sect = 0;
709 sym.desc = 0;
710 sym.value = 0;
711 _atomToSymbolIndex[ai.atom] = file.undefinedSymbols.size() + start;
712 file.undefinedSymbols.push_back(sym);
713 }
714}
715
716const Atom *Util::targetOfLazyPointer(const DefinedAtom *lpAtom) {
717 for (const Reference *ref : *lpAtom) {
Nick Kledzik2458bec2014-07-16 19:49:02 +0000718 if (_context.archHandler().isLazyPointer(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000719 return ref->target();
720 }
721 }
722 return nullptr;
723}
724
725const Atom *Util::targetOfStub(const DefinedAtom *stubAtom) {
726 for (const Reference *ref : *stubAtom) {
727 if (const Atom *ta = ref->target()) {
728 if (const DefinedAtom *lpAtom = dyn_cast<DefinedAtom>(ta)) {
729 const Atom *target = targetOfLazyPointer(lpAtom);
730 if (target)
731 return target;
732 }
733 }
734 }
735 return nullptr;
736}
737
738
739void Util::addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file) {
740 for (SectionInfo *si : _sectionInfos) {
741 Section &normSect = file.sections[si->normalizedSectionIndex];
742 switch (si->type) {
743 case llvm::MachO::S_NON_LAZY_SYMBOL_POINTERS:
744 for (const AtomInfo &info : si->atomsAndOffsets) {
745 bool foundTarget = false;
746 for (const Reference *ref : *info.atom) {
747 const Atom *target = ref->target();
748 if (target) {
749 if (isa<const SharedLibraryAtom>(target)) {
750 uint32_t index = _atomToSymbolIndex[target];
751 normSect.indirectSymbols.push_back(index);
752 foundTarget = true;
753 } else {
754 normSect.indirectSymbols.push_back(
755 llvm::MachO::INDIRECT_SYMBOL_LOCAL);
756 }
757 }
758 }
759 if (!foundTarget) {
760 normSect.indirectSymbols.push_back(
761 llvm::MachO::INDIRECT_SYMBOL_ABS);
762 }
763 }
764 break;
765 case llvm::MachO::S_LAZY_SYMBOL_POINTERS:
766 for (const AtomInfo &info : si->atomsAndOffsets) {
767 const Atom *target = targetOfLazyPointer(info.atom);
768 if (target) {
769 uint32_t index = _atomToSymbolIndex[target];
770 normSect.indirectSymbols.push_back(index);
771 }
772 }
773 break;
774 case llvm::MachO::S_SYMBOL_STUBS:
775 for (const AtomInfo &info : si->atomsAndOffsets) {
776 const Atom *target = targetOfStub(info.atom);
777 if (target) {
778 uint32_t index = _atomToSymbolIndex[target];
779 normSect.indirectSymbols.push_back(index);
780 }
781 }
782 break;
783 default:
784 break;
785 }
786 }
787
788}
789
790void Util::addDependentDylibs(const lld::File &atomFile,NormalizedFile &nFile) {
791 // Scan all imported symbols and build up list of dylibs they are from.
792 int ordinal = 1;
793 for (const SharedLibraryAtom *slAtom : atomFile.sharedLibrary()) {
794 StringRef loadPath = slAtom->loadName();
795 DylibPathToInfo::iterator pos = _dylibInfo.find(loadPath);
796 if (pos == _dylibInfo.end()) {
797 DylibInfo info;
798 info.ordinal = ordinal++;
799 info.hasWeak = slAtom->canBeNullAtRuntime();
800 info.hasNonWeak = !info.hasWeak;
801 _dylibInfo[loadPath] = info;
802 DependentDylib depInfo;
803 depInfo.path = loadPath;
804 depInfo.kind = llvm::MachO::LC_LOAD_DYLIB;
805 nFile.dependentDylibs.push_back(depInfo);
806 } else {
807 if ( slAtom->canBeNullAtRuntime() )
808 pos->second.hasWeak = true;
809 else
810 pos->second.hasNonWeak = true;
811 }
812 }
813 // Automatically weak link dylib in which all symbols are weak (canBeNull).
814 for (DependentDylib &dep : nFile.dependentDylibs) {
815 DylibInfo &info = _dylibInfo[dep.path];
816 if (info.hasWeak && !info.hasNonWeak)
817 dep.kind = llvm::MachO::LC_LOAD_WEAK_DYLIB;
818 }
819}
820
821
822int Util::dylibOrdinal(const SharedLibraryAtom *sa) {
823 return _dylibInfo[sa->loadName()].ordinal;
824}
825
826void Util::segIndexForSection(const SectionInfo *sect, uint8_t &segmentIndex,
827 uint64_t &segmentStartAddr) {
828 segmentIndex = 0;
829 for (const SegmentInfo *seg : _segmentInfos) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000830 if ((seg->address <= sect->address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000831 && (seg->address+seg->size >= sect->address+sect->size)) {
832 segmentStartAddr = seg->address;
833 return;
834 }
835 ++segmentIndex;
836 }
837 llvm_unreachable("section not in any segment");
838}
839
840
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000841void Util::appendReloc(const DefinedAtom *atom, const Reference *ref,
Nick Kledzike34182f2013-11-06 21:36:55 +0000842 Relocations &relocations) {
843 // TODO: convert Reference to normalized relocation
844}
845
846void Util::addSectionRelocs(const lld::File &, NormalizedFile &file) {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000847 if (_context.outputMachOType() != llvm::MachO::MH_OBJECT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000848 return;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000849
Nick Kledzike34182f2013-11-06 21:36:55 +0000850 for (SectionInfo *si : _sectionInfos) {
851 Section &normSect = file.sections[si->normalizedSectionIndex];
852 for (const AtomInfo &info : si->atomsAndOffsets) {
853 const DefinedAtom *atom = info.atom;
854 for (const Reference *ref : *atom) {
855 appendReloc(atom, ref, normSect.relocations);
856 }
857 }
858 }
859}
860
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000861void Util::addRebaseAndBindingInfo(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000862 NormalizedFile &nFile) {
Tim Northoverd30a1f22014-06-20 15:59:00 +0000863 if (_context.outputMachOType() == llvm::MachO::MH_OBJECT)
Nick Kledzike34182f2013-11-06 21:36:55 +0000864 return;
865
866 uint8_t segmentIndex;
867 uint64_t segmentStartAddr;
868 for (SectionInfo *sect : _sectionInfos) {
869 segIndexForSection(sect, segmentIndex, segmentStartAddr);
870 for (const AtomInfo &info : sect->atomsAndOffsets) {
871 const DefinedAtom *atom = info.atom;
872 for (const Reference *ref : *atom) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000873 uint64_t segmentOffset = _atomToAddress[atom] + ref->offsetInAtom()
Nick Kledzike34182f2013-11-06 21:36:55 +0000874 - segmentStartAddr;
875 const Atom* targ = ref->target();
Nick Kledzik2458bec2014-07-16 19:49:02 +0000876 if (_context.archHandler().isPointer(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000877 // A pointer to a DefinedAtom requires rebasing.
878 if (dyn_cast<DefinedAtom>(targ)) {
879 RebaseLocation rebase;
880 rebase.segIndex = segmentIndex;
881 rebase.segOffset = segmentOffset;
882 rebase.kind = llvm::MachO::REBASE_TYPE_POINTER;
883 nFile.rebasingInfo.push_back(rebase);
884 }
885 // A pointer to an SharedLibraryAtom requires binding.
886 if (const SharedLibraryAtom *sa = dyn_cast<SharedLibraryAtom>(targ)) {
887 BindLocation bind;
888 bind.segIndex = segmentIndex;
889 bind.segOffset = segmentOffset;
890 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
891 bind.canBeNull = sa->canBeNullAtRuntime();
892 bind.ordinal = dylibOrdinal(sa);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000893 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000894 bind.addend = ref->addend();
895 nFile.bindingInfo.push_back(bind);
896 }
897 }
Nick Kledzik2458bec2014-07-16 19:49:02 +0000898 if (_context.archHandler().isLazyPointer(*ref)) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000899 BindLocation bind;
900 bind.segIndex = segmentIndex;
901 bind.segOffset = segmentOffset;
902 bind.kind = llvm::MachO::BIND_TYPE_POINTER;
903 bind.canBeNull = false; //sa->canBeNullAtRuntime();
904 bind.ordinal = 1;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000905 bind.symbolName = targ->name();
Nick Kledzike34182f2013-11-06 21:36:55 +0000906 bind.addend = ref->addend();
907 nFile.lazyBindingInfo.push_back(bind);
908 }
909 }
910 }
911 }
912}
913
914uint32_t Util::fileFlags() {
915 return 0; //FIX ME
916}
917
918} // end anonymous namespace
919
920
921namespace lld {
922namespace mach_o {
923namespace normalized {
924
925/// Convert a set of Atoms into a normalized mach-o file.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000926ErrorOr<std::unique_ptr<NormalizedFile>>
927normalizedFromAtoms(const lld::File &atomFile,
Nick Kledzike34182f2013-11-06 21:36:55 +0000928 const MachOLinkingContext &context) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000929 // The util object buffers info until the normalized file can be made.
Nick Kledzike34182f2013-11-06 21:36:55 +0000930 Util util(context);
931 util.assignAtomsToSections(atomFile);
932 util.organizeSections();
933 util.assignAddressesToSections();
934 util.buildAtomToAddressMap();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000935
Nick Kledzike34182f2013-11-06 21:36:55 +0000936 std::unique_ptr<NormalizedFile> f(new NormalizedFile());
937 NormalizedFile &normFile = *f.get();
938 f->arch = context.arch();
Tim Northoverd30a1f22014-06-20 15:59:00 +0000939 f->fileType = context.outputMachOType();
Nick Kledzike34182f2013-11-06 21:36:55 +0000940 f->flags = util.fileFlags();
Tim Northover301c4e62014-07-01 08:15:41 +0000941 f->installName = context.installName();
Nick Kledzike34182f2013-11-06 21:36:55 +0000942 util.copySegmentInfo(normFile);
943 util.copySections(normFile);
944 util.addDependentDylibs(atomFile, normFile);
945 util.addSymbols(atomFile, normFile);
946 util.addIndirectSymbols(atomFile, normFile);
947 util.addRebaseAndBindingInfo(atomFile, normFile);
948 util.addSectionRelocs(atomFile, normFile);
949 util.copyEntryPointAddress(normFile);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000950
Nick Kledzike34182f2013-11-06 21:36:55 +0000951 return std::move(f);
952}
953
954
955} // namespace normalized
956} // namespace mach_o
957} // namespace lld
958