Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1 | //===- 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 Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 19 | /// +-------+ |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 20 | /// | Atoms | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 21 | /// +-------+ |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 22 | |
| 23 | #include "MachONormalizedFile.h" |
| 24 | #include "ReferenceKinds.h" |
| 25 | |
| 26 | #include "lld/Core/Error.h" |
| 27 | #include "lld/Core/LLVM.h" |
| 28 | |
| 29 | #include "llvm/ADT/StringRef.h" |
| 30 | #include "llvm/ADT/StringSwitch.h" |
| 31 | #include "llvm/Support/Casting.h" |
| 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/Support/ErrorHandling.h" |
| 34 | #include "llvm/Support/Format.h" |
| 35 | #include "llvm/Support/MachO.h" |
| 36 | #include "llvm/Support/system_error.h" |
| 37 | |
| 38 | #include <map> |
| 39 | |
| 40 | using llvm::StringRef; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 41 | using llvm::isa; |
| 42 | using namespace llvm::MachO; |
| 43 | using namespace lld::mach_o::normalized; |
| 44 | using namespace lld; |
| 45 | |
| 46 | namespace { |
| 47 | |
| 48 | struct AtomInfo { |
| 49 | const DefinedAtom *atom; |
| 50 | uint64_t offsetInSection; |
| 51 | }; |
| 52 | |
| 53 | struct SectionInfo { |
| 54 | SectionInfo(StringRef seg, StringRef sect, SectionType type, uint32_t attr=0); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 55 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 56 | 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 Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 68 | SectionInfo::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 Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 71 | normalizedSectionIndex(0), finalSectionIndex(0) { |
| 72 | } |
| 73 | |
| 74 | struct SegmentInfo { |
| 75 | SegmentInfo(StringRef name); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 76 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 77 | StringRef name; |
| 78 | uint64_t address; |
| 79 | uint64_t size; |
| 80 | uint32_t access; |
| 81 | std::vector<SectionInfo*> sections; |
| 82 | }; |
| 83 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 84 | SegmentInfo::SegmentInfo(StringRef n) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 85 | : name(n), address(0), size(0), access(0) { |
| 86 | } |
| 87 | |
| 88 | |
| 89 | class Util { |
| 90 | public: |
| 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 | |
| 107 | private: |
| 108 | typedef std::map<DefinedAtom::ContentType, SectionInfo*> TypeToSection; |
| 109 | typedef llvm::DenseMap<const Atom*, uint64_t> AtomToAddress; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 110 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 111 | struct DylibInfo { int ordinal; bool hasWeak; bool hasNonWeak; }; |
| 112 | typedef llvm::StringMap<DylibInfo> DylibPathToInfo; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 113 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 114 | SectionInfo *sectionForAtom(const DefinedAtom*); |
| 115 | SectionInfo *makeSection(DefinedAtom::ContentType); |
| 116 | void appendAtom(SectionInfo *sect, const DefinedAtom *atom); |
| 117 | SegmentInfo *segmentForName(StringRef segName); |
| 118 | void layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr); |
| 119 | void layoutSectionsInTextSegment(SegmentInfo *seg, uint64_t &addr); |
| 120 | void copySectionContent(SectionInfo *si, ContentBytes &content); |
| 121 | uint8_t scopeBits(const DefinedAtom* atom); |
| 122 | int dylibOrdinal(const SharedLibraryAtom *sa); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 123 | void segIndexForSection(const SectionInfo *sect, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 124 | 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 Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 129 | void appendReloc(const DefinedAtom *atom, const Reference *ref, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 130 | Relocations &relocations); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 131 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 132 | 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 Gouly | 9d263e0 | 2013-12-25 19:39:08 +0000 | [diff] [blame] | 135 | struct AtomSorter { |
| 136 | bool operator()(const AtomAndIndex &left, const AtomAndIndex &right); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 137 | }; |
Joey Gouly | 9d263e0 | 2013-12-25 19:39:08 +0000 | [diff] [blame] | 138 | struct SegmentSorter { |
| 139 | bool operator()(const SegmentInfo *left, const SegmentInfo *right); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 140 | static unsigned weight(const SegmentInfo *); |
| 141 | }; |
Joey Gouly | 9d263e0 | 2013-12-25 19:39:08 +0000 | [diff] [blame] | 142 | struct TextSectionSorter { |
| 143 | bool operator()(const SectionInfo *left, const SectionInfo *right); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 144 | 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; |
| 152 | AtomToAddress _atomToAddress; |
| 153 | DylibPathToInfo _dylibInfo; |
| 154 | const DefinedAtom *_entryAtom; |
| 155 | AtomToIndex _atomToSymbolIndex; |
| 156 | }; |
| 157 | |
| 158 | SectionInfo *Util::makeSection(DefinedAtom::ContentType type) { |
| 159 | switch ( type ) { |
| 160 | case DefinedAtom::typeCode: |
| 161 | return new (_allocator) SectionInfo("__TEXT", "__text", |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 162 | S_REGULAR, S_ATTR_PURE_INSTRUCTIONS |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 163 | | S_ATTR_SOME_INSTRUCTIONS); |
| 164 | case DefinedAtom::typeCString: |
| 165 | return new (_allocator) SectionInfo("__TEXT", "__cstring", |
| 166 | S_CSTRING_LITERALS); |
| 167 | case DefinedAtom::typeStub: |
| 168 | return new (_allocator) SectionInfo("__TEXT", "__stubs", |
| 169 | S_SYMBOL_STUBS, S_ATTR_PURE_INSTRUCTIONS); |
| 170 | case DefinedAtom::typeStubHelper: |
| 171 | return new (_allocator) SectionInfo("__TEXT", "__stub_helper", |
| 172 | S_REGULAR, S_ATTR_PURE_INSTRUCTIONS); |
| 173 | case DefinedAtom::typeLazyPointer: |
| 174 | return new (_allocator) SectionInfo("__DATA", "__la_symbol_ptr", |
| 175 | S_LAZY_SYMBOL_POINTERS); |
| 176 | case DefinedAtom::typeGOT: |
| 177 | return new (_allocator) SectionInfo("__DATA", "__got", |
| 178 | S_NON_LAZY_SYMBOL_POINTERS); |
Nick Kledzik | 61fdef6 | 2014-05-15 20:59:23 +0000 | [diff] [blame] | 179 | case DefinedAtom::typeZeroFill: |
| 180 | return new (_allocator) SectionInfo("__DATA", "__bss", |
| 181 | S_ZEROFILL); |
Nick Kledzik | a0c13a2 | 2014-05-22 01:42:06 +0000 | [diff] [blame^] | 182 | case DefinedAtom::typeLiteral4: |
| 183 | return new (_allocator) SectionInfo("__TEXT", "__literal4", |
| 184 | S_4BYTE_LITERALS); |
| 185 | case DefinedAtom::typeLiteral8: |
| 186 | return new (_allocator) SectionInfo("__TEXT", "__literal8", |
| 187 | S_8BYTE_LITERALS); |
| 188 | case DefinedAtom::typeLiteral16: |
| 189 | return new (_allocator) SectionInfo("__TEXT", "__literal16", |
| 190 | S_16BYTE_LITERALS); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 191 | default: |
| 192 | llvm_unreachable("TO DO: add support for more sections"); |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | |
| 198 | |
| 199 | SectionInfo *Util::sectionForAtom(const DefinedAtom *atom) { |
| 200 | DefinedAtom::ContentType type = atom->contentType(); |
| 201 | auto pos = _sectionMap.find(type); |
| 202 | if ( pos != _sectionMap.end() ) |
| 203 | return pos->second; |
| 204 | SectionInfo *si = makeSection(type); |
| 205 | _sectionInfos.push_back(si); |
| 206 | _sectionMap[type] = si; |
| 207 | return si; |
| 208 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 209 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 210 | |
| 211 | void Util::appendAtom(SectionInfo *sect, const DefinedAtom *atom) { |
| 212 | // Figure out offset for atom in this section given alignment constraints. |
| 213 | uint64_t offset = sect->size; |
| 214 | DefinedAtom::Alignment atomAlign = atom->alignment(); |
| 215 | uint64_t align2 = 1 << atomAlign.powerOf2; |
| 216 | uint64_t requiredModulus = atomAlign.modulus; |
| 217 | uint64_t currentModulus = (offset % align2); |
| 218 | if ( currentModulus != requiredModulus ) { |
| 219 | if ( requiredModulus > currentModulus ) |
| 220 | offset += requiredModulus-currentModulus; |
| 221 | else |
| 222 | offset += align2+requiredModulus-currentModulus; |
| 223 | } |
| 224 | // Record max alignment of any atom in this section. |
| 225 | if ( atomAlign.powerOf2 > sect->alignment ) |
| 226 | sect->alignment = atomAlign.powerOf2; |
| 227 | // Assign atom to this section with this offset. |
| 228 | AtomInfo ai = {atom, offset}; |
| 229 | sect->atomsAndOffsets.push_back(ai); |
| 230 | // Update section size to include this atom. |
| 231 | sect->size = offset + atom->size(); |
| 232 | } |
| 233 | |
| 234 | void Util::assignAtomsToSections(const lld::File &atomFile) { |
| 235 | for (const DefinedAtom *atom : atomFile.defined()) { |
| 236 | appendAtom(sectionForAtom(atom), atom); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | SegmentInfo *Util::segmentForName(StringRef segName) { |
| 241 | for (SegmentInfo *si : _segmentInfos) { |
| 242 | if ( si->name.equals(segName) ) |
| 243 | return si; |
| 244 | } |
| 245 | SegmentInfo *info = new (_allocator) SegmentInfo(segName); |
| 246 | if (segName.equals("__TEXT")) |
| 247 | info->access = VM_PROT_READ | VM_PROT_EXECUTE; |
| 248 | else if (segName.equals("__DATA")) |
| 249 | info->access = VM_PROT_READ | VM_PROT_WRITE; |
| 250 | else if (segName.equals("__PAGEZERO")) |
| 251 | info->access = 0; |
| 252 | _segmentInfos.push_back(info); |
| 253 | return info; |
| 254 | } |
| 255 | |
| 256 | unsigned Util::SegmentSorter::weight(const SegmentInfo *seg) { |
| 257 | return llvm::StringSwitch<unsigned>(seg->name) |
| 258 | .Case("__PAGEZERO", 1) |
| 259 | .Case("__TEXT", 2) |
| 260 | .Case("__DATA", 3) |
| 261 | .Default(100); |
| 262 | } |
| 263 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 264 | bool Util::SegmentSorter::operator()(const SegmentInfo *left, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 265 | const SegmentInfo *right) { |
| 266 | return (weight(left) < weight(right)); |
| 267 | } |
| 268 | |
| 269 | unsigned Util::TextSectionSorter::weight(const SectionInfo *sect) { |
| 270 | return llvm::StringSwitch<unsigned>(sect->sectionName) |
| 271 | .Case("__text", 1) |
| 272 | .Case("__stubs", 2) |
| 273 | .Case("__stub_helper", 3) |
| 274 | .Case("__const", 4) |
| 275 | .Case("__cstring", 5) |
| 276 | .Case("__unwind_info", 98) |
| 277 | .Case("__eh_frame", 99) |
| 278 | .Default(10); |
| 279 | } |
| 280 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 281 | bool Util::TextSectionSorter::operator()(const SectionInfo *left, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 282 | const SectionInfo *right) { |
| 283 | return (weight(left) < weight(right)); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | void Util::organizeSections() { |
| 288 | if (_context.outputFileType() == llvm::MachO::MH_OBJECT) { |
| 289 | // Leave sections ordered as normalized file specified. |
| 290 | uint32_t sectionIndex = 1; |
| 291 | for (SectionInfo *si : _sectionInfos) { |
| 292 | si->finalSectionIndex = sectionIndex++; |
| 293 | } |
| 294 | } else { |
| 295 | // Main executables, need a zero-page segment |
| 296 | if (_context.outputFileType() == llvm::MachO::MH_EXECUTE) |
| 297 | segmentForName("__PAGEZERO"); |
| 298 | // Group sections into segments. |
| 299 | for (SectionInfo *si : _sectionInfos) { |
| 300 | SegmentInfo *seg = segmentForName(si->segmentName); |
| 301 | seg->sections.push_back(si); |
| 302 | } |
| 303 | // Sort segments. |
| 304 | std::sort(_segmentInfos.begin(), _segmentInfos.end(), SegmentSorter()); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 305 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 306 | // Sort sections within segments. |
| 307 | for (SegmentInfo *seg : _segmentInfos) { |
| 308 | if (seg->name.equals("__TEXT")) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 309 | std::sort(seg->sections.begin(), seg->sections.end(), |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 310 | TextSectionSorter()); |
| 311 | } |
| 312 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 313 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 314 | // Record final section indexes. |
| 315 | uint32_t sectionIndex = 1; |
| 316 | for (SegmentInfo *seg : _segmentInfos) { |
| 317 | for (SectionInfo *sect : seg->sections) { |
| 318 | sect->finalSectionIndex = sectionIndex++; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | } |
| 324 | |
| 325 | uint64_t Util::alignTo(uint64_t value, uint8_t align2) { |
| 326 | return llvm::RoundUpToAlignment(value, 1 << align2); |
| 327 | } |
| 328 | |
| 329 | |
| 330 | void Util::layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr) { |
| 331 | seg->address = addr; |
| 332 | for (SectionInfo *sect : seg->sections) { |
| 333 | sect->address = alignTo(addr, sect->alignment); |
| 334 | addr += sect->size; |
| 335 | } |
| 336 | seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize()); |
| 337 | } |
| 338 | |
| 339 | |
| 340 | // __TEXT segment lays out backwards so padding is at front after load commands. |
| 341 | void Util::layoutSectionsInTextSegment(SegmentInfo *seg, uint64_t &addr) { |
| 342 | seg->address = addr; |
| 343 | // Walks sections starting at end to calculate padding for start. |
| 344 | int64_t taddr = 0; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 345 | for (auto it = seg->sections.rbegin(); it != seg->sections.rend(); ++it) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 346 | SectionInfo *sect = *it; |
| 347 | taddr -= sect->size; |
| 348 | taddr = taddr & (0 - (1 << sect->alignment)); |
| 349 | } |
| 350 | int64_t padding = taddr; |
| 351 | while (padding < 0) |
| 352 | padding += _context.pageSize(); |
| 353 | // Start assigning section address starting at padded offset. |
| 354 | addr += padding; |
| 355 | for (SectionInfo *sect : seg->sections) { |
| 356 | sect->address = alignTo(addr, sect->alignment); |
| 357 | addr = sect->address + sect->size; |
| 358 | } |
| 359 | seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize()); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | void Util::assignAddressesToSections() { |
| 364 | uint64_t address = 0; // FIXME |
| 365 | if (_context.outputFileType() != llvm::MachO::MH_OBJECT) { |
| 366 | for (SegmentInfo *seg : _segmentInfos) { |
| 367 | if (seg->name.equals("__PAGEZERO")) { |
| 368 | seg->size = _context.pageZeroSize(); |
| 369 | address += seg->size; |
| 370 | } |
| 371 | else if (seg->name.equals("__TEXT")) |
| 372 | layoutSectionsInTextSegment(seg, address); |
| 373 | else |
| 374 | layoutSectionsInSegment(seg, address); |
| 375 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 376 | DEBUG_WITH_TYPE("WriterMachO-norm", |
Nick Kledzik | 020a49c | 2013-11-06 21:57:52 +0000 | [diff] [blame] | 377 | llvm::dbgs() << "assignAddressesToSections()\n"; |
| 378 | for (SegmentInfo *sgi : _segmentInfos) { |
| 379 | llvm::dbgs() << " address=" << llvm::format("0x%08llX", sgi->address) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 380 | << ", size=" << llvm::format("0x%08llX", sgi->size) |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 381 | << ", segment-name='" << sgi->name |
Nick Kledzik | 020a49c | 2013-11-06 21:57:52 +0000 | [diff] [blame] | 382 | << "'\n"; |
| 383 | for (SectionInfo *si : sgi->sections) { |
| 384 | llvm::dbgs()<< " addr=" << llvm::format("0x%08llX", si->address) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 385 | << ", size=" << llvm::format("0x%08llX", si->size) |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 386 | << ", section-name='" << si->sectionName |
Nick Kledzik | 020a49c | 2013-11-06 21:57:52 +0000 | [diff] [blame] | 387 | << "\n"; |
| 388 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 389 | } |
Nick Kledzik | 020a49c | 2013-11-06 21:57:52 +0000 | [diff] [blame] | 390 | ); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 391 | } else { |
| 392 | for (SectionInfo *sect : _sectionInfos) { |
| 393 | sect->address = alignTo(address, sect->alignment); |
| 394 | address = sect->address + sect->size; |
| 395 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 396 | DEBUG_WITH_TYPE("WriterMachO-norm", |
Nick Kledzik | 020a49c | 2013-11-06 21:57:52 +0000 | [diff] [blame] | 397 | llvm::dbgs() << "assignAddressesToSections()\n"; |
| 398 | for (SectionInfo *si : _sectionInfos) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 399 | llvm::dbgs() << " section=" << si->sectionName |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 400 | << " address= " << llvm::format("0x%08X", si->address) |
| 401 | << " size= " << llvm::format("0x%08X", si->size) |
Nick Kledzik | 020a49c | 2013-11-06 21:57:52 +0000 | [diff] [blame] | 402 | << "\n"; |
| 403 | } |
| 404 | ); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 405 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | |
| 409 | void Util::copySegmentInfo(NormalizedFile &file) { |
| 410 | for (SegmentInfo *sgi : _segmentInfos) { |
| 411 | Segment seg; |
| 412 | seg.name = sgi->name; |
| 413 | seg.address = sgi->address; |
| 414 | seg.size = sgi->size; |
| 415 | seg.access = sgi->access; |
| 416 | file.segments.push_back(seg); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | void Util::appendSection(SectionInfo *si, NormalizedFile &file) { |
| 421 | // Add new empty section to end of file.sections. |
| 422 | Section temp; |
| 423 | file.sections.push_back(std::move(temp)); |
| 424 | Section* normSect = &file.sections.back(); |
| 425 | // Copy fields to normalized section. |
| 426 | normSect->segmentName = si->segmentName; |
| 427 | normSect->sectionName = si->sectionName; |
| 428 | normSect->type = si->type; |
| 429 | normSect->attributes = si->attributes; |
| 430 | normSect->address = si->address; |
| 431 | normSect->alignment = si->alignment; |
| 432 | // Record where normalized section is. |
| 433 | si->normalizedSectionIndex = file.sections.size()-1; |
| 434 | // Copy content from atoms to content buffer for section. |
Nick Kledzik | 61fdef6 | 2014-05-15 20:59:23 +0000 | [diff] [blame] | 435 | if (si->type == llvm::MachO::S_ZEROFILL) |
| 436 | return; |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 437 | uint8_t *sectionContent = file.ownedAllocations.Allocate<uint8_t>(si->size); |
| 438 | normSect->content = llvm::makeArrayRef(sectionContent, si->size); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 439 | for (AtomInfo &ai : si->atomsAndOffsets) { |
| 440 | // Copy raw bytes. |
| 441 | uint8_t *atomContent = reinterpret_cast<uint8_t*> |
| 442 | (§ionContent[ai.offsetInSection]); |
| 443 | memcpy(atomContent, ai.atom->rawContent().data(), ai.atom->size()); |
| 444 | // Apply fix-ups. |
| 445 | for (const Reference *ref : *ai.atom) { |
| 446 | uint32_t offset = ref->offsetInAtom(); |
| 447 | uint64_t targetAddress = 0; |
| 448 | if ( ref->target() != nullptr ) |
| 449 | targetAddress = _atomToAddress[ref->target()]; |
| 450 | uint64_t fixupAddress = _atomToAddress[ai.atom] + offset; |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 451 | _context.kindHandler().applyFixup( |
| 452 | ref->kindNamespace(), ref->kindArch(), ref->kindValue(), |
| 453 | ref->addend(), &atomContent[offset], fixupAddress, targetAddress); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | void Util::copySections(NormalizedFile &file) { |
| 459 | file.sections.reserve(_sectionInfos.size()); |
| 460 | // For final linked images, write sections grouped by segment. |
| 461 | if (_context.outputFileType() != llvm::MachO::MH_OBJECT) { |
| 462 | for (SegmentInfo *sgi : _segmentInfos) { |
| 463 | for (SectionInfo *si : sgi->sections) { |
| 464 | appendSection(si, file); |
| 465 | } |
| 466 | } |
| 467 | } else { |
| 468 | // Object files write sections in default order. |
| 469 | for (SectionInfo *si : _sectionInfos) { |
| 470 | appendSection(si, file); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | void Util::copyEntryPointAddress(NormalizedFile &nFile) { |
| 476 | if (_context.outputTypeHasEntry()) { |
| 477 | nFile.entryAddress = _atomToAddress[_entryAtom]; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | void Util::buildAtomToAddressMap() { |
| 482 | DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs() |
| 483 | << "assign atom addresses:\n"); |
| 484 | const bool lookForEntry = _context.outputTypeHasEntry(); |
| 485 | for (SectionInfo *sect : _sectionInfos) { |
| 486 | for (const AtomInfo &info : sect->atomsAndOffsets) { |
| 487 | _atomToAddress[info.atom] = sect->address + info.offsetInSection; |
| 488 | if (lookForEntry && (info.atom->contentType() == DefinedAtom::typeCode) && |
| 489 | (info.atom->size() != 0) && |
| 490 | info.atom->name() == _context.entrySymbolName()) { |
| 491 | _entryAtom = info.atom; |
| 492 | } |
| 493 | DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs() |
| 494 | << " address=" |
| 495 | << llvm::format("0x%016X", _atomToAddress[info.atom]) |
| 496 | << " atom=" << info.atom |
| 497 | << " name=" << info.atom->name() << "\n"); |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | uint8_t Util::scopeBits(const DefinedAtom* atom) { |
| 503 | switch (atom->scope()) { |
| 504 | case Atom::scopeTranslationUnit: |
| 505 | return 0; |
| 506 | case Atom::scopeLinkageUnit: |
| 507 | return N_PEXT | N_EXT; |
| 508 | case Atom::scopeGlobal: |
| 509 | return N_EXT; |
| 510 | } |
Nick Kledzik | 020fa7f | 2013-11-06 22:18:09 +0000 | [diff] [blame] | 511 | llvm_unreachable("Unknown scope"); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 514 | bool Util::AtomSorter::operator()(const AtomAndIndex &left, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 515 | const AtomAndIndex &right) { |
| 516 | return (left.atom->name().compare(right.atom->name()) < 0); |
| 517 | } |
| 518 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 519 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 520 | bool Util::belongsInGlobalSymbolsSection(const DefinedAtom* atom) { |
| 521 | return (atom->scope() == Atom::scopeGlobal); |
| 522 | } |
| 523 | |
| 524 | void Util::addSymbols(const lld::File &atomFile, NormalizedFile &file) { |
| 525 | // Mach-O symbol table has three regions: locals, globals, undefs. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 526 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 527 | // Add all local (non-global) symbols in address order |
| 528 | std::vector<AtomAndIndex> globals; |
| 529 | globals.reserve(512); |
| 530 | for (SectionInfo *sect : _sectionInfos) { |
| 531 | for (const AtomInfo &info : sect->atomsAndOffsets) { |
| 532 | const DefinedAtom *atom = info.atom; |
| 533 | if (!atom->name().empty()) { |
| 534 | if (belongsInGlobalSymbolsSection(atom)) { |
| 535 | AtomAndIndex ai = { atom, sect->finalSectionIndex }; |
| 536 | globals.push_back(ai); |
| 537 | } else { |
| 538 | Symbol sym; |
| 539 | sym.name = atom->name(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 540 | sym.type = N_SECT; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 541 | sym.scope = scopeBits(atom); |
| 542 | sym.sect = sect->finalSectionIndex; |
| 543 | sym.desc = 0; |
| 544 | sym.value = _atomToAddress[atom]; |
| 545 | file.localSymbols.push_back(sym); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 550 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 551 | // Sort global symbol alphabetically, then add to symbol table. |
| 552 | std::sort(globals.begin(), globals.end(), AtomSorter()); |
| 553 | for (AtomAndIndex &ai : globals) { |
| 554 | Symbol sym; |
| 555 | sym.name = ai.atom->name(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 556 | sym.type = N_SECT; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 557 | sym.scope = scopeBits(static_cast<const DefinedAtom*>(ai.atom)); |
| 558 | sym.sect = ai.index; |
| 559 | sym.desc = 0; |
| 560 | sym.value = _atomToAddress[ai.atom]; |
| 561 | file.globalSymbols.push_back(sym); |
| 562 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 563 | |
| 564 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 565 | // Sort undefined symbol alphabetically, then add to symbol table. |
| 566 | std::vector<AtomAndIndex> undefs; |
| 567 | undefs.reserve(128); |
| 568 | for (const UndefinedAtom *atom : atomFile.undefined()) { |
| 569 | AtomAndIndex ai = { atom, 0 }; |
| 570 | undefs.push_back(ai); |
| 571 | } |
| 572 | for (const SharedLibraryAtom *atom : atomFile.sharedLibrary()) { |
| 573 | AtomAndIndex ai = { atom, 0 }; |
| 574 | undefs.push_back(ai); |
| 575 | } |
| 576 | std::sort(undefs.begin(), undefs.end(), AtomSorter()); |
| 577 | const uint32_t start = file.globalSymbols.size() + file.localSymbols.size(); |
| 578 | for (AtomAndIndex &ai : undefs) { |
| 579 | Symbol sym; |
| 580 | sym.name = ai.atom->name(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 581 | sym.type = N_UNDF; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 582 | sym.scope = N_EXT; |
| 583 | sym.sect = 0; |
| 584 | sym.desc = 0; |
| 585 | sym.value = 0; |
| 586 | _atomToSymbolIndex[ai.atom] = file.undefinedSymbols.size() + start; |
| 587 | file.undefinedSymbols.push_back(sym); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | const Atom *Util::targetOfLazyPointer(const DefinedAtom *lpAtom) { |
| 592 | for (const Reference *ref : *lpAtom) { |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 593 | if (_context.kindHandler().isLazyTarget(*ref)) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 594 | return ref->target(); |
| 595 | } |
| 596 | } |
| 597 | return nullptr; |
| 598 | } |
| 599 | |
| 600 | const Atom *Util::targetOfStub(const DefinedAtom *stubAtom) { |
| 601 | for (const Reference *ref : *stubAtom) { |
| 602 | if (const Atom *ta = ref->target()) { |
| 603 | if (const DefinedAtom *lpAtom = dyn_cast<DefinedAtom>(ta)) { |
| 604 | const Atom *target = targetOfLazyPointer(lpAtom); |
| 605 | if (target) |
| 606 | return target; |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | return nullptr; |
| 611 | } |
| 612 | |
| 613 | |
| 614 | void Util::addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file) { |
| 615 | for (SectionInfo *si : _sectionInfos) { |
| 616 | Section &normSect = file.sections[si->normalizedSectionIndex]; |
| 617 | switch (si->type) { |
| 618 | case llvm::MachO::S_NON_LAZY_SYMBOL_POINTERS: |
| 619 | for (const AtomInfo &info : si->atomsAndOffsets) { |
| 620 | bool foundTarget = false; |
| 621 | for (const Reference *ref : *info.atom) { |
| 622 | const Atom *target = ref->target(); |
| 623 | if (target) { |
| 624 | if (isa<const SharedLibraryAtom>(target)) { |
| 625 | uint32_t index = _atomToSymbolIndex[target]; |
| 626 | normSect.indirectSymbols.push_back(index); |
| 627 | foundTarget = true; |
| 628 | } else { |
| 629 | normSect.indirectSymbols.push_back( |
| 630 | llvm::MachO::INDIRECT_SYMBOL_LOCAL); |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | if (!foundTarget) { |
| 635 | normSect.indirectSymbols.push_back( |
| 636 | llvm::MachO::INDIRECT_SYMBOL_ABS); |
| 637 | } |
| 638 | } |
| 639 | break; |
| 640 | case llvm::MachO::S_LAZY_SYMBOL_POINTERS: |
| 641 | for (const AtomInfo &info : si->atomsAndOffsets) { |
| 642 | const Atom *target = targetOfLazyPointer(info.atom); |
| 643 | if (target) { |
| 644 | uint32_t index = _atomToSymbolIndex[target]; |
| 645 | normSect.indirectSymbols.push_back(index); |
| 646 | } |
| 647 | } |
| 648 | break; |
| 649 | case llvm::MachO::S_SYMBOL_STUBS: |
| 650 | for (const AtomInfo &info : si->atomsAndOffsets) { |
| 651 | const Atom *target = targetOfStub(info.atom); |
| 652 | if (target) { |
| 653 | uint32_t index = _atomToSymbolIndex[target]; |
| 654 | normSect.indirectSymbols.push_back(index); |
| 655 | } |
| 656 | } |
| 657 | break; |
| 658 | default: |
| 659 | break; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | } |
| 664 | |
| 665 | void Util::addDependentDylibs(const lld::File &atomFile,NormalizedFile &nFile) { |
| 666 | // Scan all imported symbols and build up list of dylibs they are from. |
| 667 | int ordinal = 1; |
| 668 | for (const SharedLibraryAtom *slAtom : atomFile.sharedLibrary()) { |
| 669 | StringRef loadPath = slAtom->loadName(); |
| 670 | DylibPathToInfo::iterator pos = _dylibInfo.find(loadPath); |
| 671 | if (pos == _dylibInfo.end()) { |
| 672 | DylibInfo info; |
| 673 | info.ordinal = ordinal++; |
| 674 | info.hasWeak = slAtom->canBeNullAtRuntime(); |
| 675 | info.hasNonWeak = !info.hasWeak; |
| 676 | _dylibInfo[loadPath] = info; |
| 677 | DependentDylib depInfo; |
| 678 | depInfo.path = loadPath; |
| 679 | depInfo.kind = llvm::MachO::LC_LOAD_DYLIB; |
| 680 | nFile.dependentDylibs.push_back(depInfo); |
| 681 | } else { |
| 682 | if ( slAtom->canBeNullAtRuntime() ) |
| 683 | pos->second.hasWeak = true; |
| 684 | else |
| 685 | pos->second.hasNonWeak = true; |
| 686 | } |
| 687 | } |
| 688 | // Automatically weak link dylib in which all symbols are weak (canBeNull). |
| 689 | for (DependentDylib &dep : nFile.dependentDylibs) { |
| 690 | DylibInfo &info = _dylibInfo[dep.path]; |
| 691 | if (info.hasWeak && !info.hasNonWeak) |
| 692 | dep.kind = llvm::MachO::LC_LOAD_WEAK_DYLIB; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | |
| 697 | int Util::dylibOrdinal(const SharedLibraryAtom *sa) { |
| 698 | return _dylibInfo[sa->loadName()].ordinal; |
| 699 | } |
| 700 | |
| 701 | void Util::segIndexForSection(const SectionInfo *sect, uint8_t &segmentIndex, |
| 702 | uint64_t &segmentStartAddr) { |
| 703 | segmentIndex = 0; |
| 704 | for (const SegmentInfo *seg : _segmentInfos) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 705 | if ((seg->address <= sect->address) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 706 | && (seg->address+seg->size >= sect->address+sect->size)) { |
| 707 | segmentStartAddr = seg->address; |
| 708 | return; |
| 709 | } |
| 710 | ++segmentIndex; |
| 711 | } |
| 712 | llvm_unreachable("section not in any segment"); |
| 713 | } |
| 714 | |
| 715 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 716 | void Util::appendReloc(const DefinedAtom *atom, const Reference *ref, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 717 | Relocations &relocations) { |
| 718 | // TODO: convert Reference to normalized relocation |
| 719 | } |
| 720 | |
| 721 | void Util::addSectionRelocs(const lld::File &, NormalizedFile &file) { |
| 722 | if (_context.outputFileType() != llvm::MachO::MH_OBJECT) |
| 723 | return; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 724 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 725 | for (SectionInfo *si : _sectionInfos) { |
| 726 | Section &normSect = file.sections[si->normalizedSectionIndex]; |
| 727 | for (const AtomInfo &info : si->atomsAndOffsets) { |
| 728 | const DefinedAtom *atom = info.atom; |
| 729 | for (const Reference *ref : *atom) { |
| 730 | appendReloc(atom, ref, normSect.relocations); |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 736 | void Util::addRebaseAndBindingInfo(const lld::File &atomFile, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 737 | NormalizedFile &nFile) { |
| 738 | if (_context.outputFileType() == llvm::MachO::MH_OBJECT) |
| 739 | return; |
| 740 | |
| 741 | uint8_t segmentIndex; |
| 742 | uint64_t segmentStartAddr; |
| 743 | for (SectionInfo *sect : _sectionInfos) { |
| 744 | segIndexForSection(sect, segmentIndex, segmentStartAddr); |
| 745 | for (const AtomInfo &info : sect->atomsAndOffsets) { |
| 746 | const DefinedAtom *atom = info.atom; |
| 747 | for (const Reference *ref : *atom) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 748 | uint64_t segmentOffset = _atomToAddress[atom] + ref->offsetInAtom() |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 749 | - segmentStartAddr; |
| 750 | const Atom* targ = ref->target(); |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 751 | if (_context.kindHandler().isPointer(*ref)) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 752 | // A pointer to a DefinedAtom requires rebasing. |
| 753 | if (dyn_cast<DefinedAtom>(targ)) { |
| 754 | RebaseLocation rebase; |
| 755 | rebase.segIndex = segmentIndex; |
| 756 | rebase.segOffset = segmentOffset; |
| 757 | rebase.kind = llvm::MachO::REBASE_TYPE_POINTER; |
| 758 | nFile.rebasingInfo.push_back(rebase); |
| 759 | } |
| 760 | // A pointer to an SharedLibraryAtom requires binding. |
| 761 | if (const SharedLibraryAtom *sa = dyn_cast<SharedLibraryAtom>(targ)) { |
| 762 | BindLocation bind; |
| 763 | bind.segIndex = segmentIndex; |
| 764 | bind.segOffset = segmentOffset; |
| 765 | bind.kind = llvm::MachO::BIND_TYPE_POINTER; |
| 766 | bind.canBeNull = sa->canBeNullAtRuntime(); |
| 767 | bind.ordinal = dylibOrdinal(sa); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 768 | bind.symbolName = targ->name(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 769 | bind.addend = ref->addend(); |
| 770 | nFile.bindingInfo.push_back(bind); |
| 771 | } |
| 772 | } |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 773 | if (_context.kindHandler().isLazyTarget(*ref)) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 774 | BindLocation bind; |
| 775 | bind.segIndex = segmentIndex; |
| 776 | bind.segOffset = segmentOffset; |
| 777 | bind.kind = llvm::MachO::BIND_TYPE_POINTER; |
| 778 | bind.canBeNull = false; //sa->canBeNullAtRuntime(); |
| 779 | bind.ordinal = 1; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 780 | bind.symbolName = targ->name(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 781 | bind.addend = ref->addend(); |
| 782 | nFile.lazyBindingInfo.push_back(bind); |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | uint32_t Util::fileFlags() { |
| 790 | return 0; //FIX ME |
| 791 | } |
| 792 | |
| 793 | } // end anonymous namespace |
| 794 | |
| 795 | |
| 796 | namespace lld { |
| 797 | namespace mach_o { |
| 798 | namespace normalized { |
| 799 | |
| 800 | /// Convert a set of Atoms into a normalized mach-o file. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 801 | ErrorOr<std::unique_ptr<NormalizedFile>> |
| 802 | normalizedFromAtoms(const lld::File &atomFile, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 803 | const MachOLinkingContext &context) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 804 | // The util object buffers info until the normalized file can be made. |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 805 | Util util(context); |
| 806 | util.assignAtomsToSections(atomFile); |
| 807 | util.organizeSections(); |
| 808 | util.assignAddressesToSections(); |
| 809 | util.buildAtomToAddressMap(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 810 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 811 | std::unique_ptr<NormalizedFile> f(new NormalizedFile()); |
| 812 | NormalizedFile &normFile = *f.get(); |
| 813 | f->arch = context.arch(); |
| 814 | f->fileType = context.outputFileType(); |
| 815 | f->flags = util.fileFlags(); |
| 816 | util.copySegmentInfo(normFile); |
| 817 | util.copySections(normFile); |
| 818 | util.addDependentDylibs(atomFile, normFile); |
| 819 | util.addSymbols(atomFile, normFile); |
| 820 | util.addIndirectSymbols(atomFile, normFile); |
| 821 | util.addRebaseAndBindingInfo(atomFile, normFile); |
| 822 | util.addSectionRelocs(atomFile, normFile); |
| 823 | util.copyEntryPointAddress(normFile); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 824 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 825 | return std::move(f); |
| 826 | } |
| 827 | |
| 828 | |
| 829 | } // namespace normalized |
| 830 | } // namespace mach_o |
| 831 | } // namespace lld |
| 832 | |