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