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