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