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