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 { |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 54 | SectionInfo(StringRef seg, StringRef sect, SectionType type, |
| 55 | const MachOLinkingContext &ctxt, uint32_t attr=0); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 56 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 57 | StringRef segmentName; |
| 58 | StringRef sectionName; |
| 59 | SectionType type; |
| 60 | uint32_t attributes; |
| 61 | uint64_t address; |
| 62 | uint64_t size; |
| 63 | uint32_t alignment; |
| 64 | std::vector<AtomInfo> atomsAndOffsets; |
| 65 | uint32_t normalizedSectionIndex; |
| 66 | uint32_t finalSectionIndex; |
| 67 | }; |
| 68 | |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 69 | SectionInfo::SectionInfo(StringRef sg, StringRef sct, SectionType t, |
| 70 | const MachOLinkingContext &ctxt, uint32_t attrs) |
| 71 | : segmentName(sg), sectionName(sct), type(t), attributes(attrs), |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 72 | address(0), size(0), alignment(0), |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 73 | normalizedSectionIndex(0), finalSectionIndex(0) { |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 74 | uint8_t align; |
| 75 | if (ctxt.sectionAligned(segmentName, sectionName, align)) { |
| 76 | alignment = align; |
| 77 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | struct SegmentInfo { |
| 81 | SegmentInfo(StringRef name); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 82 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 83 | StringRef name; |
| 84 | uint64_t address; |
| 85 | uint64_t size; |
| 86 | uint32_t access; |
| 87 | std::vector<SectionInfo*> sections; |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 88 | uint32_t normalizedSegmentIndex; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 91 | SegmentInfo::SegmentInfo(StringRef n) |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 92 | : name(n), address(0), size(0), access(0), normalizedSegmentIndex(0) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
| 96 | class Util { |
| 97 | public: |
Nick Kledzik | 2d43235 | 2014-07-17 23:16:21 +0000 | [diff] [blame] | 98 | Util(const MachOLinkingContext &ctxt) : _context(ctxt), |
| 99 | _archHandler(ctxt.archHandler()), _entryAtom(nullptr) {} |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 100 | |
| 101 | void assignAtomsToSections(const lld::File &atomFile); |
| 102 | void organizeSections(); |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 103 | void assignAddressesToSections(const NormalizedFile &file); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 104 | uint32_t fileFlags(); |
| 105 | void copySegmentInfo(NormalizedFile &file); |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 106 | void copySectionInfo(NormalizedFile &file); |
| 107 | void updateSectionInfo(NormalizedFile &file); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 108 | void buildAtomToAddressMap(); |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 109 | std::error_code addSymbols(const lld::File &atomFile, NormalizedFile &file); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 110 | void addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file); |
| 111 | void addRebaseAndBindingInfo(const lld::File &, NormalizedFile &file); |
| 112 | void addSectionRelocs(const lld::File &, NormalizedFile &file); |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 113 | void buildDataInCodeArray(const lld::File &, NormalizedFile &file); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 114 | void addDependentDylibs(const lld::File &, NormalizedFile &file); |
| 115 | void copyEntryPointAddress(NormalizedFile &file); |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 116 | void copySectionContent(NormalizedFile &file); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 117 | |
| 118 | private: |
| 119 | typedef std::map<DefinedAtom::ContentType, SectionInfo*> TypeToSection; |
| 120 | typedef llvm::DenseMap<const Atom*, uint64_t> AtomToAddress; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 121 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 122 | struct DylibInfo { int ordinal; bool hasWeak; bool hasNonWeak; }; |
| 123 | typedef llvm::StringMap<DylibInfo> DylibPathToInfo; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 124 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 125 | SectionInfo *sectionForAtom(const DefinedAtom*); |
Nick Kledzik | 936d520 | 2014-06-11 01:30:55 +0000 | [diff] [blame] | 126 | SectionInfo *getRelocatableSection(DefinedAtom::ContentType type); |
| 127 | SectionInfo *getFinalSection(DefinedAtom::ContentType type); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 128 | void appendAtom(SectionInfo *sect, const DefinedAtom *atom); |
| 129 | SegmentInfo *segmentForName(StringRef segName); |
| 130 | void layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr); |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 131 | void layoutSectionsInTextSegment(size_t, SegmentInfo *, uint64_t &); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 132 | void copySectionContent(SectionInfo *si, ContentBytes &content); |
Nick Kledzik | 6085539 | 2014-06-11 00:24:16 +0000 | [diff] [blame] | 133 | uint16_t descBits(const DefinedAtom* atom); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 134 | int dylibOrdinal(const SharedLibraryAtom *sa); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 135 | void segIndexForSection(const SectionInfo *sect, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 136 | uint8_t &segmentIndex, uint64_t &segmentStartAddr); |
| 137 | const Atom *targetOfLazyPointer(const DefinedAtom *lpAtom); |
| 138 | const Atom *targetOfStub(const DefinedAtom *stubAtom); |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 139 | std::error_code getSymbolTableRegion(const DefinedAtom* atom, |
| 140 | bool &inGlobalsRegion, |
| 141 | SymbolScope &symbolScope); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 142 | void appendSection(SectionInfo *si, NormalizedFile &file); |
Nick Kledzik | 2d43235 | 2014-07-17 23:16:21 +0000 | [diff] [blame] | 143 | uint32_t sectionIndexForAtom(const Atom *atom); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 144 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 145 | static uint64_t alignTo(uint64_t value, uint8_t align2); |
| 146 | typedef llvm::DenseMap<const Atom*, uint32_t> AtomToIndex; |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 147 | struct AtomAndIndex { const Atom *atom; uint32_t index; SymbolScope scope; }; |
Joey Gouly | 9d263e0 | 2013-12-25 19:39:08 +0000 | [diff] [blame] | 148 | struct AtomSorter { |
| 149 | bool operator()(const AtomAndIndex &left, const AtomAndIndex &right); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 150 | }; |
Joey Gouly | 9d263e0 | 2013-12-25 19:39:08 +0000 | [diff] [blame] | 151 | struct SegmentSorter { |
| 152 | bool operator()(const SegmentInfo *left, const SegmentInfo *right); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 153 | static unsigned weight(const SegmentInfo *); |
| 154 | }; |
Joey Gouly | 9d263e0 | 2013-12-25 19:39:08 +0000 | [diff] [blame] | 155 | struct TextSectionSorter { |
| 156 | bool operator()(const SectionInfo *left, const SectionInfo *right); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 157 | static unsigned weight(const SectionInfo *); |
| 158 | }; |
| 159 | |
| 160 | const MachOLinkingContext &_context; |
Nick Kledzik | 2d43235 | 2014-07-17 23:16:21 +0000 | [diff] [blame] | 161 | mach_o::ArchHandler &_archHandler; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 162 | llvm::BumpPtrAllocator _allocator; |
| 163 | std::vector<SectionInfo*> _sectionInfos; |
| 164 | std::vector<SegmentInfo*> _segmentInfos; |
| 165 | TypeToSection _sectionMap; |
Nick Kledzik | acfad80 | 2014-05-30 22:51:04 +0000 | [diff] [blame] | 166 | std::vector<SectionInfo*> _customSections; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 167 | AtomToAddress _atomToAddress; |
| 168 | DylibPathToInfo _dylibInfo; |
| 169 | const DefinedAtom *_entryAtom; |
| 170 | AtomToIndex _atomToSymbolIndex; |
| 171 | }; |
| 172 | |
Nick Kledzik | ec14083 | 2014-06-10 01:50:00 +0000 | [diff] [blame] | 173 | |
Nick Kledzik | 936d520 | 2014-06-11 01:30:55 +0000 | [diff] [blame] | 174 | SectionInfo *Util::getRelocatableSection(DefinedAtom::ContentType type) { |
Nick Kledzik | ec14083 | 2014-06-10 01:50:00 +0000 | [diff] [blame] | 175 | StringRef segmentName; |
| 176 | StringRef sectionName; |
| 177 | SectionType sectionType; |
| 178 | SectionAttr sectionAttrs; |
| 179 | |
| 180 | // Use same table used by when parsing .o files. |
| 181 | relocatableSectionInfoForContentType(type, segmentName, sectionName, |
| 182 | sectionType, sectionAttrs); |
| 183 | // If we already have a SectionInfo with this name, re-use it. |
| 184 | // This can happen if two ContentType map to the same mach-o section. |
| 185 | for (auto sect : _sectionMap) { |
| 186 | if (sect.second->sectionName.equals(sectionName) && |
| 187 | sect.second->segmentName.equals(segmentName)) { |
| 188 | return sect.second; |
| 189 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 190 | } |
Nick Kledzik | ec14083 | 2014-06-10 01:50:00 +0000 | [diff] [blame] | 191 | // Otherwise allocate new SectionInfo object. |
Nick Kledzik | 936d520 | 2014-06-11 01:30:55 +0000 | [diff] [blame] | 192 | SectionInfo *sect = new (_allocator) SectionInfo(segmentName, sectionName, |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 193 | sectionType, _context, |
| 194 | sectionAttrs); |
Nick Kledzik | 936d520 | 2014-06-11 01:30:55 +0000 | [diff] [blame] | 195 | _sectionInfos.push_back(sect); |
| 196 | _sectionMap[type] = sect; |
| 197 | return sect; |
Nick Kledzik | ec14083 | 2014-06-10 01:50:00 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | #define ENTRY(seg, sect, type, atomType) \ |
| 201 | {seg, sect, type, DefinedAtom::atomType } |
| 202 | |
| 203 | struct MachOFinalSectionFromAtomType { |
| 204 | StringRef segmentName; |
| 205 | StringRef sectionName; |
| 206 | SectionType sectionType; |
| 207 | DefinedAtom::ContentType atomType; |
| 208 | }; |
| 209 | |
| 210 | const MachOFinalSectionFromAtomType sectsToAtomType[] = { |
| 211 | ENTRY("__TEXT", "__text", S_REGULAR, typeCode), |
| 212 | ENTRY("__TEXT", "__cstring", S_CSTRING_LITERALS, typeCString), |
| 213 | ENTRY("__TEXT", "__ustring", S_REGULAR, typeUTF16String), |
| 214 | ENTRY("__TEXT", "__const", S_REGULAR, typeConstant), |
| 215 | ENTRY("__TEXT", "__const", S_4BYTE_LITERALS, typeLiteral4), |
| 216 | ENTRY("__TEXT", "__const", S_8BYTE_LITERALS, typeLiteral8), |
| 217 | ENTRY("__TEXT", "__const", S_16BYTE_LITERALS, typeLiteral16), |
| 218 | ENTRY("__TEXT", "__stubs", S_SYMBOL_STUBS, typeStub), |
| 219 | ENTRY("__TEXT", "__stub_helper", S_REGULAR, typeStubHelper), |
| 220 | ENTRY("__TEXT", "__gcc_except_tab", S_REGULAR, typeLSDA), |
| 221 | ENTRY("__TEXT", "__eh_frame", S_COALESCED, typeCFI), |
| 222 | ENTRY("__DATA", "__data", S_REGULAR, typeData), |
| 223 | ENTRY("__DATA", "__const", S_REGULAR, typeConstData), |
| 224 | ENTRY("__DATA", "__cfstring", S_REGULAR, typeCFString), |
| 225 | ENTRY("__DATA", "__la_symbol_ptr", S_LAZY_SYMBOL_POINTERS, |
| 226 | typeLazyPointer), |
| 227 | ENTRY("__DATA", "__mod_init_func", S_MOD_INIT_FUNC_POINTERS, |
| 228 | typeInitializerPtr), |
| 229 | ENTRY("__DATA", "__mod_term_func", S_MOD_TERM_FUNC_POINTERS, |
| 230 | typeTerminatorPtr), |
| 231 | ENTRY("__DATA", "___got", S_NON_LAZY_SYMBOL_POINTERS, |
| 232 | typeGOT), |
Tim Northover | 9ee9935 | 2014-06-30 09:49:37 +0000 | [diff] [blame] | 233 | ENTRY("__DATA", "___bss", S_ZEROFILL, typeZeroFill), |
| 234 | |
| 235 | // FIXME: __compact_unwind actually needs to be processed by a pass and put |
| 236 | // into __TEXT,__unwind_info. For now, forwarding it back to |
| 237 | // __LD,__compact_unwind is harmless (it's ignored by the unwinder, which then |
| 238 | // proceeds to process __TEXT,__eh_frame for its instructions). |
| 239 | ENTRY("__LD", "__compact_unwind", S_REGULAR, typeCompactUnwindInfo), |
Nick Kledzik | ec14083 | 2014-06-10 01:50:00 +0000 | [diff] [blame] | 240 | }; |
| 241 | #undef ENTRY |
| 242 | |
| 243 | |
Nick Kledzik | 936d520 | 2014-06-11 01:30:55 +0000 | [diff] [blame] | 244 | SectionInfo *Util::getFinalSection(DefinedAtom::ContentType atomType) { |
Tim Northover | b5bf686 | 2014-06-30 10:30:00 +0000 | [diff] [blame] | 245 | for (auto &p : sectsToAtomType) { |
| 246 | if (p.atomType != atomType) |
Nick Kledzik | ec14083 | 2014-06-10 01:50:00 +0000 | [diff] [blame] | 247 | continue; |
| 248 | SectionAttr sectionAttrs = 0; |
| 249 | switch (atomType) { |
| 250 | case DefinedAtom::typeCode: |
| 251 | case DefinedAtom::typeStub: |
| 252 | sectionAttrs = S_ATTR_PURE_INSTRUCTIONS; |
| 253 | break; |
| 254 | default: |
| 255 | break; |
| 256 | } |
| 257 | // If we already have a SectionInfo with this name, re-use it. |
| 258 | // This can happen if two ContentType map to the same mach-o section. |
| 259 | for (auto sect : _sectionMap) { |
Tim Northover | b5bf686 | 2014-06-30 10:30:00 +0000 | [diff] [blame] | 260 | if (sect.second->sectionName.equals(p.sectionName) && |
| 261 | sect.second->segmentName.equals(p.segmentName)) { |
Nick Kledzik | ec14083 | 2014-06-10 01:50:00 +0000 | [diff] [blame] | 262 | return sect.second; |
| 263 | } |
| 264 | } |
| 265 | // Otherwise allocate new SectionInfo object. |
Tim Northover | b5bf686 | 2014-06-30 10:30:00 +0000 | [diff] [blame] | 266 | SectionInfo *sect = new (_allocator) SectionInfo(p.segmentName, |
| 267 | p.sectionName, |
| 268 | p.sectionType, |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 269 | _context, |
Nick Kledzik | 936d520 | 2014-06-11 01:30:55 +0000 | [diff] [blame] | 270 | sectionAttrs); |
| 271 | _sectionInfos.push_back(sect); |
| 272 | _sectionMap[atomType] = sect; |
| 273 | return sect; |
Nick Kledzik | ec14083 | 2014-06-10 01:50:00 +0000 | [diff] [blame] | 274 | } |
| 275 | llvm_unreachable("content type not yet supported"); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | |
| 279 | |
| 280 | SectionInfo *Util::sectionForAtom(const DefinedAtom *atom) { |
Nick Kledzik | acfad80 | 2014-05-30 22:51:04 +0000 | [diff] [blame] | 281 | if (atom->sectionChoice() == DefinedAtom::sectionBasedOnContent) { |
| 282 | // Section for this atom is derived from content type. |
| 283 | DefinedAtom::ContentType type = atom->contentType(); |
| 284 | auto pos = _sectionMap.find(type); |
| 285 | if ( pos != _sectionMap.end() ) |
| 286 | return pos->second; |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 287 | bool rMode = (_context.outputMachOType() == llvm::MachO::MH_OBJECT); |
Nick Kledzik | 936d520 | 2014-06-11 01:30:55 +0000 | [diff] [blame] | 288 | return rMode ? getRelocatableSection(type) : getFinalSection(type); |
Nick Kledzik | acfad80 | 2014-05-30 22:51:04 +0000 | [diff] [blame] | 289 | } else { |
| 290 | // This atom needs to be in a custom section. |
| 291 | StringRef customName = atom->customSectionName(); |
| 292 | // Look to see if we have already allocated the needed custom section. |
| 293 | for(SectionInfo *sect : _customSections) { |
| 294 | const DefinedAtom *firstAtom = sect->atomsAndOffsets.front().atom; |
| 295 | if (firstAtom->customSectionName().equals(customName)) { |
| 296 | return sect; |
| 297 | } |
| 298 | } |
| 299 | // Not found, so need to create a new custom section. |
| 300 | size_t seperatorIndex = customName.find('/'); |
| 301 | assert(seperatorIndex != StringRef::npos); |
Tim Northover | 7b0a130 | 2014-06-30 09:49:33 +0000 | [diff] [blame] | 302 | StringRef segName = customName.slice(0, seperatorIndex); |
| 303 | StringRef sectName = customName.drop_front(seperatorIndex + 1); |
Nick Kledzik | acfad80 | 2014-05-30 22:51:04 +0000 | [diff] [blame] | 304 | SectionInfo *sect = new (_allocator) SectionInfo(segName, sectName, |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 305 | S_REGULAR, _context); |
Nick Kledzik | acfad80 | 2014-05-30 22:51:04 +0000 | [diff] [blame] | 306 | _customSections.push_back(sect); |
Tim Northover | 7b0a130 | 2014-06-30 09:49:33 +0000 | [diff] [blame] | 307 | _sectionInfos.push_back(sect); |
Nick Kledzik | acfad80 | 2014-05-30 22:51:04 +0000 | [diff] [blame] | 308 | return sect; |
| 309 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 310 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 311 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 312 | |
| 313 | void Util::appendAtom(SectionInfo *sect, const DefinedAtom *atom) { |
| 314 | // Figure out offset for atom in this section given alignment constraints. |
| 315 | uint64_t offset = sect->size; |
| 316 | DefinedAtom::Alignment atomAlign = atom->alignment(); |
| 317 | uint64_t align2 = 1 << atomAlign.powerOf2; |
| 318 | uint64_t requiredModulus = atomAlign.modulus; |
| 319 | uint64_t currentModulus = (offset % align2); |
| 320 | if ( currentModulus != requiredModulus ) { |
| 321 | if ( requiredModulus > currentModulus ) |
| 322 | offset += requiredModulus-currentModulus; |
| 323 | else |
| 324 | offset += align2+requiredModulus-currentModulus; |
| 325 | } |
| 326 | // Record max alignment of any atom in this section. |
| 327 | if ( atomAlign.powerOf2 > sect->alignment ) |
| 328 | sect->alignment = atomAlign.powerOf2; |
| 329 | // Assign atom to this section with this offset. |
| 330 | AtomInfo ai = {atom, offset}; |
| 331 | sect->atomsAndOffsets.push_back(ai); |
| 332 | // Update section size to include this atom. |
| 333 | sect->size = offset + atom->size(); |
| 334 | } |
| 335 | |
| 336 | void Util::assignAtomsToSections(const lld::File &atomFile) { |
| 337 | for (const DefinedAtom *atom : atomFile.defined()) { |
| 338 | appendAtom(sectionForAtom(atom), atom); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | SegmentInfo *Util::segmentForName(StringRef segName) { |
| 343 | for (SegmentInfo *si : _segmentInfos) { |
| 344 | if ( si->name.equals(segName) ) |
| 345 | return si; |
| 346 | } |
| 347 | SegmentInfo *info = new (_allocator) SegmentInfo(segName); |
| 348 | if (segName.equals("__TEXT")) |
| 349 | info->access = VM_PROT_READ | VM_PROT_EXECUTE; |
| 350 | else if (segName.equals("__DATA")) |
| 351 | info->access = VM_PROT_READ | VM_PROT_WRITE; |
| 352 | else if (segName.equals("__PAGEZERO")) |
| 353 | info->access = 0; |
| 354 | _segmentInfos.push_back(info); |
| 355 | return info; |
| 356 | } |
| 357 | |
| 358 | unsigned Util::SegmentSorter::weight(const SegmentInfo *seg) { |
| 359 | return llvm::StringSwitch<unsigned>(seg->name) |
| 360 | .Case("__PAGEZERO", 1) |
| 361 | .Case("__TEXT", 2) |
| 362 | .Case("__DATA", 3) |
| 363 | .Default(100); |
| 364 | } |
| 365 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 366 | bool Util::SegmentSorter::operator()(const SegmentInfo *left, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 367 | const SegmentInfo *right) { |
| 368 | return (weight(left) < weight(right)); |
| 369 | } |
| 370 | |
| 371 | unsigned Util::TextSectionSorter::weight(const SectionInfo *sect) { |
| 372 | return llvm::StringSwitch<unsigned>(sect->sectionName) |
| 373 | .Case("__text", 1) |
| 374 | .Case("__stubs", 2) |
| 375 | .Case("__stub_helper", 3) |
| 376 | .Case("__const", 4) |
| 377 | .Case("__cstring", 5) |
| 378 | .Case("__unwind_info", 98) |
| 379 | .Case("__eh_frame", 99) |
| 380 | .Default(10); |
| 381 | } |
| 382 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 383 | bool Util::TextSectionSorter::operator()(const SectionInfo *left, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 384 | const SectionInfo *right) { |
| 385 | return (weight(left) < weight(right)); |
| 386 | } |
| 387 | |
| 388 | |
| 389 | void Util::organizeSections() { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 390 | if (_context.outputMachOType() == llvm::MachO::MH_OBJECT) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 391 | // Leave sections ordered as normalized file specified. |
| 392 | uint32_t sectionIndex = 1; |
| 393 | for (SectionInfo *si : _sectionInfos) { |
| 394 | si->finalSectionIndex = sectionIndex++; |
| 395 | } |
| 396 | } else { |
| 397 | // Main executables, need a zero-page segment |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 398 | if (_context.outputMachOType() == llvm::MachO::MH_EXECUTE) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 399 | segmentForName("__PAGEZERO"); |
| 400 | // Group sections into segments. |
| 401 | for (SectionInfo *si : _sectionInfos) { |
| 402 | SegmentInfo *seg = segmentForName(si->segmentName); |
| 403 | seg->sections.push_back(si); |
| 404 | } |
| 405 | // Sort segments. |
| 406 | std::sort(_segmentInfos.begin(), _segmentInfos.end(), SegmentSorter()); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 407 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 408 | // Sort sections within segments. |
| 409 | for (SegmentInfo *seg : _segmentInfos) { |
| 410 | if (seg->name.equals("__TEXT")) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 411 | std::sort(seg->sections.begin(), seg->sections.end(), |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 412 | TextSectionSorter()); |
| 413 | } |
| 414 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 415 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 416 | // Record final section indexes. |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 417 | uint32_t segmentIndex = 0; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 418 | uint32_t sectionIndex = 1; |
| 419 | for (SegmentInfo *seg : _segmentInfos) { |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 420 | seg->normalizedSegmentIndex = segmentIndex++; |
| 421 | for (SectionInfo *sect : seg->sections) { |
| 422 | sect->finalSectionIndex = sectionIndex++; |
| 423 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
| 427 | } |
| 428 | |
| 429 | uint64_t Util::alignTo(uint64_t value, uint8_t align2) { |
| 430 | return llvm::RoundUpToAlignment(value, 1 << align2); |
| 431 | } |
| 432 | |
| 433 | |
| 434 | void Util::layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr) { |
| 435 | seg->address = addr; |
| 436 | for (SectionInfo *sect : seg->sections) { |
| 437 | sect->address = alignTo(addr, sect->alignment); |
| 438 | addr += sect->size; |
| 439 | } |
| 440 | seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize()); |
| 441 | } |
| 442 | |
| 443 | |
| 444 | // __TEXT segment lays out backwards so padding is at front after load commands. |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 445 | void Util::layoutSectionsInTextSegment(size_t hlcSize, SegmentInfo *seg, |
| 446 | uint64_t &addr) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 447 | seg->address = addr; |
| 448 | // Walks sections starting at end to calculate padding for start. |
| 449 | int64_t taddr = 0; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 450 | for (auto it = seg->sections.rbegin(); it != seg->sections.rend(); ++it) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 451 | SectionInfo *sect = *it; |
| 452 | taddr -= sect->size; |
| 453 | taddr = taddr & (0 - (1 << sect->alignment)); |
| 454 | } |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 455 | int64_t padding = taddr - hlcSize; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 456 | while (padding < 0) |
| 457 | padding += _context.pageSize(); |
| 458 | // Start assigning section address starting at padded offset. |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 459 | addr += (padding + hlcSize); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 460 | for (SectionInfo *sect : seg->sections) { |
| 461 | sect->address = alignTo(addr, sect->alignment); |
| 462 | addr = sect->address + sect->size; |
| 463 | } |
| 464 | seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize()); |
| 465 | } |
| 466 | |
| 467 | |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 468 | void Util::assignAddressesToSections(const NormalizedFile &file) { |
| 469 | size_t hlcSize = headerAndLoadCommandsSize(file); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 470 | uint64_t address = 0; // FIXME |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 471 | if (_context.outputMachOType() != llvm::MachO::MH_OBJECT) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 472 | for (SegmentInfo *seg : _segmentInfos) { |
| 473 | if (seg->name.equals("__PAGEZERO")) { |
| 474 | seg->size = _context.pageZeroSize(); |
| 475 | address += seg->size; |
| 476 | } |
| 477 | else if (seg->name.equals("__TEXT")) |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 478 | layoutSectionsInTextSegment(hlcSize, seg, address); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 479 | else |
| 480 | layoutSectionsInSegment(seg, address); |
Tim Northover | 7b0a130 | 2014-06-30 09:49:33 +0000 | [diff] [blame] | 481 | |
| 482 | address = llvm::RoundUpToAlignment(address, _context.pageSize()); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 483 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 484 | DEBUG_WITH_TYPE("WriterMachO-norm", |
Nick Kledzik | 020a49c | 2013-11-06 21:57:52 +0000 | [diff] [blame] | 485 | llvm::dbgs() << "assignAddressesToSections()\n"; |
| 486 | for (SegmentInfo *sgi : _segmentInfos) { |
| 487 | llvm::dbgs() << " address=" << llvm::format("0x%08llX", sgi->address) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 488 | << ", size=" << llvm::format("0x%08llX", sgi->size) |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 489 | << ", segment-name='" << sgi->name |
Nick Kledzik | 020a49c | 2013-11-06 21:57:52 +0000 | [diff] [blame] | 490 | << "'\n"; |
| 491 | for (SectionInfo *si : sgi->sections) { |
| 492 | llvm::dbgs()<< " addr=" << llvm::format("0x%08llX", si->address) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 493 | << ", size=" << llvm::format("0x%08llX", si->size) |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 494 | << ", section-name='" << si->sectionName |
Nick Kledzik | 020a49c | 2013-11-06 21:57:52 +0000 | [diff] [blame] | 495 | << "\n"; |
| 496 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 497 | } |
Nick Kledzik | 020a49c | 2013-11-06 21:57:52 +0000 | [diff] [blame] | 498 | ); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 499 | } else { |
| 500 | for (SectionInfo *sect : _sectionInfos) { |
| 501 | sect->address = alignTo(address, sect->alignment); |
| 502 | address = sect->address + sect->size; |
| 503 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 504 | DEBUG_WITH_TYPE("WriterMachO-norm", |
Nick Kledzik | 020a49c | 2013-11-06 21:57:52 +0000 | [diff] [blame] | 505 | llvm::dbgs() << "assignAddressesToSections()\n"; |
| 506 | for (SectionInfo *si : _sectionInfos) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 507 | llvm::dbgs() << " section=" << si->sectionName |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 508 | << " address= " << llvm::format("0x%08X", si->address) |
| 509 | << " size= " << llvm::format("0x%08X", si->size) |
Nick Kledzik | 020a49c | 2013-11-06 21:57:52 +0000 | [diff] [blame] | 510 | << "\n"; |
| 511 | } |
| 512 | ); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 513 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | |
| 517 | void Util::copySegmentInfo(NormalizedFile &file) { |
| 518 | for (SegmentInfo *sgi : _segmentInfos) { |
| 519 | Segment seg; |
| 520 | seg.name = sgi->name; |
| 521 | seg.address = sgi->address; |
| 522 | seg.size = sgi->size; |
| 523 | seg.access = sgi->access; |
| 524 | file.segments.push_back(seg); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | void Util::appendSection(SectionInfo *si, NormalizedFile &file) { |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 529 | // Add new empty section to end of file.sections. |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 530 | Section temp; |
| 531 | file.sections.push_back(std::move(temp)); |
| 532 | Section* normSect = &file.sections.back(); |
| 533 | // Copy fields to normalized section. |
| 534 | normSect->segmentName = si->segmentName; |
| 535 | normSect->sectionName = si->sectionName; |
| 536 | normSect->type = si->type; |
| 537 | normSect->attributes = si->attributes; |
| 538 | normSect->address = si->address; |
| 539 | normSect->alignment = si->alignment; |
| 540 | // Record where normalized section is. |
| 541 | si->normalizedSectionIndex = file.sections.size()-1; |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | void Util::copySectionContent(NormalizedFile &file) { |
| 545 | const bool r = (_context.outputMachOType() == llvm::MachO::MH_OBJECT); |
| 546 | |
| 547 | // Utility function for ArchHandler to find address of atom in output file. |
| 548 | auto addrForAtom = [&] (const Atom &atom) -> uint64_t { |
| 549 | auto pos = _atomToAddress.find(&atom); |
| 550 | assert(pos != _atomToAddress.end()); |
| 551 | return pos->second; |
| 552 | }; |
| 553 | |
| 554 | for (SectionInfo *si : _sectionInfos) { |
| 555 | if (si->type == llvm::MachO::S_ZEROFILL) |
| 556 | continue; |
| 557 | // Copy content from atoms to content buffer for section. |
| 558 | uint8_t *sectionContent = file.ownedAllocations.Allocate<uint8_t>(si->size); |
| 559 | Section *normSect = &file.sections[si->normalizedSectionIndex]; |
| 560 | normSect->content = llvm::makeArrayRef(sectionContent, si->size); |
| 561 | for (AtomInfo &ai : si->atomsAndOffsets) { |
| 562 | uint8_t *atomContent = reinterpret_cast<uint8_t*> |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 563 | (§ionContent[ai.offsetInSection]); |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 564 | _archHandler.generateAtomContent(*ai.atom, r, addrForAtom, atomContent); |
| 565 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 569 | |
| 570 | void Util::copySectionInfo(NormalizedFile &file) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 571 | file.sections.reserve(_sectionInfos.size()); |
| 572 | // For final linked images, write sections grouped by segment. |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 573 | if (_context.outputMachOType() != llvm::MachO::MH_OBJECT) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 574 | for (SegmentInfo *sgi : _segmentInfos) { |
| 575 | for (SectionInfo *si : sgi->sections) { |
| 576 | appendSection(si, file); |
| 577 | } |
| 578 | } |
| 579 | } else { |
| 580 | // Object files write sections in default order. |
| 581 | for (SectionInfo *si : _sectionInfos) { |
| 582 | appendSection(si, file); |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 587 | void Util::updateSectionInfo(NormalizedFile &file) { |
| 588 | file.sections.reserve(_sectionInfos.size()); |
| 589 | if (_context.outputMachOType() != llvm::MachO::MH_OBJECT) { |
| 590 | // For final linked images, sections grouped by segment. |
| 591 | for (SegmentInfo *sgi : _segmentInfos) { |
| 592 | Segment *normSeg = &file.segments[sgi->normalizedSegmentIndex]; |
| 593 | normSeg->address = sgi->address; |
| 594 | normSeg->size = sgi->size; |
| 595 | for (SectionInfo *si : sgi->sections) { |
| 596 | Section *normSect = &file.sections[si->normalizedSectionIndex]; |
| 597 | normSect->address = si->address; |
| 598 | } |
| 599 | } |
| 600 | } else { |
| 601 | // Object files write sections in default order. |
| 602 | for (SectionInfo *si : _sectionInfos) { |
| 603 | Section *normSect = &file.sections[si->normalizedSectionIndex]; |
| 604 | normSect->address = si->address; |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 609 | void Util::copyEntryPointAddress(NormalizedFile &nFile) { |
| 610 | if (_context.outputTypeHasEntry()) { |
Nick Kledzik | 54fd4e5 | 2014-07-28 23:06:09 +0000 | [diff] [blame] | 611 | if (_archHandler.isThumbFunction(*_entryAtom)) |
| 612 | nFile.entryAddress = (_atomToAddress[_entryAtom] | 1); |
| 613 | else |
| 614 | nFile.entryAddress = _atomToAddress[_entryAtom]; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
| 618 | void Util::buildAtomToAddressMap() { |
| 619 | DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs() |
| 620 | << "assign atom addresses:\n"); |
| 621 | const bool lookForEntry = _context.outputTypeHasEntry(); |
| 622 | for (SectionInfo *sect : _sectionInfos) { |
| 623 | for (const AtomInfo &info : sect->atomsAndOffsets) { |
| 624 | _atomToAddress[info.atom] = sect->address + info.offsetInSection; |
| 625 | if (lookForEntry && (info.atom->contentType() == DefinedAtom::typeCode) && |
| 626 | (info.atom->size() != 0) && |
| 627 | info.atom->name() == _context.entrySymbolName()) { |
| 628 | _entryAtom = info.atom; |
| 629 | } |
| 630 | DEBUG_WITH_TYPE("WriterMachO-address", llvm::dbgs() |
| 631 | << " address=" |
| 632 | << llvm::format("0x%016X", _atomToAddress[info.atom]) |
| 633 | << " atom=" << info.atom |
| 634 | << " name=" << info.atom->name() << "\n"); |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
Nick Kledzik | 6085539 | 2014-06-11 00:24:16 +0000 | [diff] [blame] | 639 | uint16_t Util::descBits(const DefinedAtom* atom) { |
| 640 | uint16_t desc = 0; |
| 641 | switch (atom->merge()) { |
| 642 | case lld::DefinedAtom::mergeNo: |
| 643 | case lld::DefinedAtom::mergeAsTentative: |
| 644 | break; |
| 645 | case lld::DefinedAtom::mergeAsWeak: |
| 646 | case lld::DefinedAtom::mergeAsWeakAndAddressUsed: |
| 647 | desc |= N_WEAK_DEF; |
| 648 | break; |
| 649 | case lld::DefinedAtom::mergeSameNameAndSize: |
| 650 | case lld::DefinedAtom::mergeByLargestSection: |
| 651 | case lld::DefinedAtom::mergeByContent: |
| 652 | llvm_unreachable("Unsupported DefinedAtom::merge()"); |
| 653 | break; |
| 654 | } |
| 655 | if (atom->contentType() == lld::DefinedAtom::typeResolver) |
| 656 | desc |= N_SYMBOL_RESOLVER; |
Nick Kledzik | 7e9808f | 2014-07-23 00:51:37 +0000 | [diff] [blame] | 657 | if (_archHandler.isThumbFunction(*atom)) |
| 658 | desc |= N_ARM_THUMB_DEF; |
Nick Kledzik | 7820c80 | 2014-08-21 22:18:30 +0000 | [diff] [blame^] | 659 | if (atom->deadStrip() == DefinedAtom::deadStripNever) { |
| 660 | if ((atom->contentType() != DefinedAtom::typeInitializerPtr) |
| 661 | && (atom->contentType() != DefinedAtom::typeTerminatorPtr)) |
| 662 | desc |= N_NO_DEAD_STRIP; |
| 663 | } |
Nick Kledzik | 6085539 | 2014-06-11 00:24:16 +0000 | [diff] [blame] | 664 | return desc; |
| 665 | } |
| 666 | |
| 667 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 668 | bool Util::AtomSorter::operator()(const AtomAndIndex &left, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 669 | const AtomAndIndex &right) { |
| 670 | return (left.atom->name().compare(right.atom->name()) < 0); |
| 671 | } |
| 672 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 673 | |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 674 | std::error_code Util::getSymbolTableRegion(const DefinedAtom* atom, |
| 675 | bool &inGlobalsRegion, |
| 676 | SymbolScope &scope) { |
| 677 | bool rMode = (_context.outputMachOType() == llvm::MachO::MH_OBJECT); |
| 678 | switch (atom->scope()) { |
| 679 | case Atom::scopeTranslationUnit: |
| 680 | scope = 0; |
| 681 | inGlobalsRegion = false; |
| 682 | return std::error_code(); |
| 683 | case Atom::scopeLinkageUnit: |
| 684 | if ((_context.exportMode() == MachOLinkingContext::ExportMode::whiteList) |
| 685 | && _context.exportSymbolNamed(atom->name())) { |
| 686 | return make_dynamic_error_code(Twine("cannot export hidden symbol ") |
| 687 | + atom->name()); |
| 688 | } |
| 689 | if (rMode) { |
| 690 | if (_context.keepPrivateExterns()) { |
| 691 | // -keep_private_externs means keep in globals region as N_PEXT. |
| 692 | scope = N_PEXT | N_EXT; |
| 693 | inGlobalsRegion = true; |
| 694 | return std::error_code(); |
| 695 | } |
| 696 | } |
| 697 | // scopeLinkageUnit symbols are no longer global once linked. |
| 698 | scope = N_PEXT; |
| 699 | inGlobalsRegion = false; |
| 700 | return std::error_code(); |
| 701 | case Atom::scopeGlobal: |
| 702 | if (_context.exportRestrictMode()) { |
| 703 | if (_context.exportSymbolNamed(atom->name())) { |
| 704 | scope = N_EXT; |
| 705 | inGlobalsRegion = true; |
| 706 | return std::error_code(); |
| 707 | } else { |
| 708 | scope = N_PEXT; |
| 709 | inGlobalsRegion = false; |
| 710 | return std::error_code(); |
| 711 | } |
| 712 | } else { |
| 713 | scope = N_EXT; |
| 714 | inGlobalsRegion = true; |
| 715 | return std::error_code(); |
| 716 | } |
| 717 | break; |
| 718 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 721 | std::error_code Util::addSymbols(const lld::File &atomFile, |
| 722 | NormalizedFile &file) { |
Nick Kledzik | 2d43235 | 2014-07-17 23:16:21 +0000 | [diff] [blame] | 723 | bool rMode = (_context.outputMachOType() == llvm::MachO::MH_OBJECT); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 724 | // Mach-O symbol table has three regions: locals, globals, undefs. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 725 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 726 | // Add all local (non-global) symbols in address order |
| 727 | std::vector<AtomAndIndex> globals; |
| 728 | globals.reserve(512); |
| 729 | for (SectionInfo *sect : _sectionInfos) { |
| 730 | for (const AtomInfo &info : sect->atomsAndOffsets) { |
| 731 | const DefinedAtom *atom = info.atom; |
| 732 | if (!atom->name().empty()) { |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 733 | SymbolScope symbolScope; |
| 734 | bool inGlobalsRegion; |
| 735 | if (auto ec = getSymbolTableRegion(atom, inGlobalsRegion, symbolScope)){ |
| 736 | return ec; |
| 737 | } |
| 738 | if (inGlobalsRegion) { |
| 739 | AtomAndIndex ai = { atom, sect->finalSectionIndex, symbolScope }; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 740 | globals.push_back(ai); |
| 741 | } else { |
| 742 | Symbol sym; |
| 743 | sym.name = atom->name(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 744 | sym.type = N_SECT; |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 745 | sym.scope = symbolScope; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 746 | sym.sect = sect->finalSectionIndex; |
Nick Kledzik | 7e9808f | 2014-07-23 00:51:37 +0000 | [diff] [blame] | 747 | sym.desc = descBits(atom); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 748 | sym.value = _atomToAddress[atom]; |
Nick Kledzik | 2d43235 | 2014-07-17 23:16:21 +0000 | [diff] [blame] | 749 | _atomToSymbolIndex[atom] = file.localSymbols.size(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 750 | file.localSymbols.push_back(sym); |
| 751 | } |
Nick Kledzik | 2d43235 | 2014-07-17 23:16:21 +0000 | [diff] [blame] | 752 | } else if (rMode && _archHandler.needsLocalSymbolInRelocatableFile(atom)){ |
| 753 | // Create 'Lxxx' labels for anonymous atoms if archHandler says so. |
| 754 | static unsigned tempNum = 1; |
| 755 | char tmpName[16]; |
| 756 | sprintf(tmpName, "L%04u", tempNum++); |
| 757 | StringRef tempRef(tmpName); |
| 758 | Symbol sym; |
| 759 | sym.name = tempRef.copy(file.ownedAllocations); |
| 760 | sym.type = N_SECT; |
| 761 | sym.scope = 0; |
| 762 | sym.sect = sect->finalSectionIndex; |
| 763 | sym.desc = 0; |
| 764 | sym.value = _atomToAddress[atom]; |
| 765 | _atomToSymbolIndex[atom] = file.localSymbols.size(); |
| 766 | file.localSymbols.push_back(sym); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 770 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 771 | // Sort global symbol alphabetically, then add to symbol table. |
| 772 | std::sort(globals.begin(), globals.end(), AtomSorter()); |
Nick Kledzik | 2d43235 | 2014-07-17 23:16:21 +0000 | [diff] [blame] | 773 | const uint32_t globalStartIndex = file.localSymbols.size(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 774 | for (AtomAndIndex &ai : globals) { |
| 775 | Symbol sym; |
| 776 | sym.name = ai.atom->name(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 777 | sym.type = N_SECT; |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 778 | sym.scope = ai.scope; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 779 | sym.sect = ai.index; |
Nick Kledzik | 6085539 | 2014-06-11 00:24:16 +0000 | [diff] [blame] | 780 | sym.desc = descBits(static_cast<const DefinedAtom*>(ai.atom)); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 781 | sym.value = _atomToAddress[ai.atom]; |
Nick Kledzik | 2d43235 | 2014-07-17 23:16:21 +0000 | [diff] [blame] | 782 | _atomToSymbolIndex[ai.atom] = globalStartIndex + file.globalSymbols.size(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 783 | file.globalSymbols.push_back(sym); |
| 784 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 785 | |
| 786 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 787 | // Sort undefined symbol alphabetically, then add to symbol table. |
| 788 | std::vector<AtomAndIndex> undefs; |
| 789 | undefs.reserve(128); |
| 790 | for (const UndefinedAtom *atom : atomFile.undefined()) { |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 791 | AtomAndIndex ai = { atom, 0, N_EXT }; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 792 | undefs.push_back(ai); |
| 793 | } |
| 794 | for (const SharedLibraryAtom *atom : atomFile.sharedLibrary()) { |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 795 | AtomAndIndex ai = { atom, 0, N_EXT }; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 796 | undefs.push_back(ai); |
| 797 | } |
| 798 | std::sort(undefs.begin(), undefs.end(), AtomSorter()); |
| 799 | const uint32_t start = file.globalSymbols.size() + file.localSymbols.size(); |
| 800 | for (AtomAndIndex &ai : undefs) { |
| 801 | Symbol sym; |
Nick Kledzik | b476832 | 2014-08-13 23:11:42 +0000 | [diff] [blame] | 802 | uint16_t desc = 0; |
| 803 | if (!rMode) { |
| 804 | uint8_t ordinal = dylibOrdinal(dyn_cast<SharedLibraryAtom>(ai.atom)); |
| 805 | llvm::MachO::SET_LIBRARY_ORDINAL(desc, ordinal); |
| 806 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 807 | sym.name = ai.atom->name(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 808 | sym.type = N_UNDF; |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 809 | sym.scope = ai.scope; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 810 | sym.sect = 0; |
Nick Kledzik | b476832 | 2014-08-13 23:11:42 +0000 | [diff] [blame] | 811 | sym.desc = desc; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 812 | sym.value = 0; |
| 813 | _atomToSymbolIndex[ai.atom] = file.undefinedSymbols.size() + start; |
| 814 | file.undefinedSymbols.push_back(sym); |
| 815 | } |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 816 | |
| 817 | return std::error_code(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | const Atom *Util::targetOfLazyPointer(const DefinedAtom *lpAtom) { |
| 821 | for (const Reference *ref : *lpAtom) { |
Nick Kledzik | 2d43235 | 2014-07-17 23:16:21 +0000 | [diff] [blame] | 822 | if (_archHandler.isLazyPointer(*ref)) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 823 | return ref->target(); |
| 824 | } |
| 825 | } |
| 826 | return nullptr; |
| 827 | } |
| 828 | |
| 829 | const Atom *Util::targetOfStub(const DefinedAtom *stubAtom) { |
| 830 | for (const Reference *ref : *stubAtom) { |
| 831 | if (const Atom *ta = ref->target()) { |
| 832 | if (const DefinedAtom *lpAtom = dyn_cast<DefinedAtom>(ta)) { |
| 833 | const Atom *target = targetOfLazyPointer(lpAtom); |
| 834 | if (target) |
| 835 | return target; |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | return nullptr; |
| 840 | } |
| 841 | |
| 842 | |
| 843 | void Util::addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file) { |
| 844 | for (SectionInfo *si : _sectionInfos) { |
| 845 | Section &normSect = file.sections[si->normalizedSectionIndex]; |
| 846 | switch (si->type) { |
| 847 | case llvm::MachO::S_NON_LAZY_SYMBOL_POINTERS: |
| 848 | for (const AtomInfo &info : si->atomsAndOffsets) { |
| 849 | bool foundTarget = false; |
| 850 | for (const Reference *ref : *info.atom) { |
| 851 | const Atom *target = ref->target(); |
| 852 | if (target) { |
| 853 | if (isa<const SharedLibraryAtom>(target)) { |
| 854 | uint32_t index = _atomToSymbolIndex[target]; |
| 855 | normSect.indirectSymbols.push_back(index); |
| 856 | foundTarget = true; |
| 857 | } else { |
| 858 | normSect.indirectSymbols.push_back( |
| 859 | llvm::MachO::INDIRECT_SYMBOL_LOCAL); |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | if (!foundTarget) { |
| 864 | normSect.indirectSymbols.push_back( |
| 865 | llvm::MachO::INDIRECT_SYMBOL_ABS); |
| 866 | } |
| 867 | } |
| 868 | break; |
| 869 | case llvm::MachO::S_LAZY_SYMBOL_POINTERS: |
| 870 | for (const AtomInfo &info : si->atomsAndOffsets) { |
| 871 | const Atom *target = targetOfLazyPointer(info.atom); |
| 872 | if (target) { |
| 873 | uint32_t index = _atomToSymbolIndex[target]; |
| 874 | normSect.indirectSymbols.push_back(index); |
| 875 | } |
| 876 | } |
| 877 | break; |
| 878 | case llvm::MachO::S_SYMBOL_STUBS: |
| 879 | for (const AtomInfo &info : si->atomsAndOffsets) { |
| 880 | const Atom *target = targetOfStub(info.atom); |
| 881 | if (target) { |
| 882 | uint32_t index = _atomToSymbolIndex[target]; |
| 883 | normSect.indirectSymbols.push_back(index); |
| 884 | } |
| 885 | } |
| 886 | break; |
| 887 | default: |
| 888 | break; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | } |
| 893 | |
| 894 | void Util::addDependentDylibs(const lld::File &atomFile,NormalizedFile &nFile) { |
| 895 | // Scan all imported symbols and build up list of dylibs they are from. |
| 896 | int ordinal = 1; |
| 897 | for (const SharedLibraryAtom *slAtom : atomFile.sharedLibrary()) { |
| 898 | StringRef loadPath = slAtom->loadName(); |
| 899 | DylibPathToInfo::iterator pos = _dylibInfo.find(loadPath); |
| 900 | if (pos == _dylibInfo.end()) { |
| 901 | DylibInfo info; |
| 902 | info.ordinal = ordinal++; |
| 903 | info.hasWeak = slAtom->canBeNullAtRuntime(); |
| 904 | info.hasNonWeak = !info.hasWeak; |
| 905 | _dylibInfo[loadPath] = info; |
| 906 | DependentDylib depInfo; |
| 907 | depInfo.path = loadPath; |
| 908 | depInfo.kind = llvm::MachO::LC_LOAD_DYLIB; |
| 909 | nFile.dependentDylibs.push_back(depInfo); |
| 910 | } else { |
| 911 | if ( slAtom->canBeNullAtRuntime() ) |
| 912 | pos->second.hasWeak = true; |
| 913 | else |
| 914 | pos->second.hasNonWeak = true; |
| 915 | } |
| 916 | } |
| 917 | // Automatically weak link dylib in which all symbols are weak (canBeNull). |
| 918 | for (DependentDylib &dep : nFile.dependentDylibs) { |
| 919 | DylibInfo &info = _dylibInfo[dep.path]; |
| 920 | if (info.hasWeak && !info.hasNonWeak) |
| 921 | dep.kind = llvm::MachO::LC_LOAD_WEAK_DYLIB; |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | |
| 926 | int Util::dylibOrdinal(const SharedLibraryAtom *sa) { |
| 927 | return _dylibInfo[sa->loadName()].ordinal; |
| 928 | } |
| 929 | |
| 930 | void Util::segIndexForSection(const SectionInfo *sect, uint8_t &segmentIndex, |
| 931 | uint64_t &segmentStartAddr) { |
| 932 | segmentIndex = 0; |
| 933 | for (const SegmentInfo *seg : _segmentInfos) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 934 | if ((seg->address <= sect->address) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 935 | && (seg->address+seg->size >= sect->address+sect->size)) { |
| 936 | segmentStartAddr = seg->address; |
| 937 | return; |
| 938 | } |
| 939 | ++segmentIndex; |
| 940 | } |
| 941 | llvm_unreachable("section not in any segment"); |
| 942 | } |
| 943 | |
| 944 | |
Nick Kledzik | 2d43235 | 2014-07-17 23:16:21 +0000 | [diff] [blame] | 945 | uint32_t Util::sectionIndexForAtom(const Atom *atom) { |
| 946 | uint64_t address = _atomToAddress[atom]; |
| 947 | uint32_t index = 1; |
| 948 | for (const SectionInfo *si : _sectionInfos) { |
| 949 | if ((si->address <= address) && (address < si->address+si->size)) |
| 950 | return index; |
| 951 | ++index; |
| 952 | } |
| 953 | llvm_unreachable("atom not in any section"); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | void Util::addSectionRelocs(const lld::File &, NormalizedFile &file) { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 957 | if (_context.outputMachOType() != llvm::MachO::MH_OBJECT) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 958 | return; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 959 | |
Nick Kledzik | 2d43235 | 2014-07-17 23:16:21 +0000 | [diff] [blame] | 960 | |
| 961 | // Utility function for ArchHandler to find symbol index for an atom. |
| 962 | auto symIndexForAtom = [&] (const Atom &atom) -> uint32_t { |
| 963 | auto pos = _atomToSymbolIndex.find(&atom); |
| 964 | assert(pos != _atomToSymbolIndex.end()); |
| 965 | return pos->second; |
| 966 | }; |
| 967 | |
| 968 | // Utility function for ArchHandler to find section index for an atom. |
| 969 | auto sectIndexForAtom = [&] (const Atom &atom) -> uint32_t { |
| 970 | return sectionIndexForAtom(&atom); |
| 971 | }; |
| 972 | |
| 973 | // Utility function for ArchHandler to find address of atom in output file. |
| 974 | auto addressForAtom = [&] (const Atom &atom) -> uint64_t { |
| 975 | auto pos = _atomToAddress.find(&atom); |
| 976 | assert(pos != _atomToAddress.end()); |
| 977 | return pos->second; |
| 978 | }; |
| 979 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 980 | for (SectionInfo *si : _sectionInfos) { |
| 981 | Section &normSect = file.sections[si->normalizedSectionIndex]; |
| 982 | for (const AtomInfo &info : si->atomsAndOffsets) { |
| 983 | const DefinedAtom *atom = info.atom; |
| 984 | for (const Reference *ref : *atom) { |
Nick Kledzik | 2d43235 | 2014-07-17 23:16:21 +0000 | [diff] [blame] | 985 | _archHandler.appendSectionRelocations(*atom, info.offsetInSection, *ref, |
| 986 | symIndexForAtom, |
| 987 | sectIndexForAtom, |
| 988 | addressForAtom, |
| 989 | normSect.relocations); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 995 | void Util::buildDataInCodeArray(const lld::File &, NormalizedFile &file) { |
| 996 | for (SectionInfo *si : _sectionInfos) { |
| 997 | for (const AtomInfo &info : si->atomsAndOffsets) { |
| 998 | // Atoms that contain data-in-code have "transition" references |
| 999 | // which mark a point where the embedded data starts of ends. |
| 1000 | // This needs to be converted to the mach-o format which is an array |
| 1001 | // of data-in-code ranges. |
| 1002 | uint32_t startOffset = 0; |
| 1003 | DataRegionType mode = DataRegionType(0); |
| 1004 | for (const Reference *ref : *info.atom) { |
| 1005 | if (ref->kindNamespace() != Reference::KindNamespace::mach_o) |
| 1006 | continue; |
| 1007 | if (_archHandler.isDataInCodeTransition(ref->kindValue())) { |
| 1008 | DataRegionType nextMode = (DataRegionType)ref->addend(); |
| 1009 | if (mode != nextMode) { |
| 1010 | if (mode != 0) { |
| 1011 | // Found end data range, so make range entry. |
| 1012 | DataInCode entry; |
| 1013 | entry.offset = si->address + info.offsetInSection + startOffset; |
| 1014 | entry.length = ref->offsetInAtom() - startOffset; |
| 1015 | entry.kind = mode; |
| 1016 | file.dataInCode.push_back(entry); |
| 1017 | } |
| 1018 | } |
| 1019 | mode = nextMode; |
| 1020 | startOffset = ref->offsetInAtom(); |
| 1021 | } |
| 1022 | } |
| 1023 | if (mode != 0) { |
| 1024 | // Function ends with data (no end transition). |
| 1025 | DataInCode entry; |
| 1026 | entry.offset = si->address + info.offsetInSection + startOffset; |
| 1027 | entry.length = info.atom->size() - startOffset; |
| 1028 | entry.kind = mode; |
| 1029 | file.dataInCode.push_back(entry); |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 1035 | void Util::addRebaseAndBindingInfo(const lld::File &atomFile, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1036 | NormalizedFile &nFile) { |
Tim Northover | d30a1f2 | 2014-06-20 15:59:00 +0000 | [diff] [blame] | 1037 | if (_context.outputMachOType() == llvm::MachO::MH_OBJECT) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1038 | return; |
| 1039 | |
| 1040 | uint8_t segmentIndex; |
| 1041 | uint64_t segmentStartAddr; |
| 1042 | for (SectionInfo *sect : _sectionInfos) { |
| 1043 | segIndexForSection(sect, segmentIndex, segmentStartAddr); |
| 1044 | for (const AtomInfo &info : sect->atomsAndOffsets) { |
| 1045 | const DefinedAtom *atom = info.atom; |
| 1046 | for (const Reference *ref : *atom) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 1047 | uint64_t segmentOffset = _atomToAddress[atom] + ref->offsetInAtom() |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1048 | - segmentStartAddr; |
| 1049 | const Atom* targ = ref->target(); |
Nick Kledzik | 2d43235 | 2014-07-17 23:16:21 +0000 | [diff] [blame] | 1050 | if (_archHandler.isPointer(*ref)) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1051 | // A pointer to a DefinedAtom requires rebasing. |
| 1052 | if (dyn_cast<DefinedAtom>(targ)) { |
| 1053 | RebaseLocation rebase; |
| 1054 | rebase.segIndex = segmentIndex; |
| 1055 | rebase.segOffset = segmentOffset; |
| 1056 | rebase.kind = llvm::MachO::REBASE_TYPE_POINTER; |
| 1057 | nFile.rebasingInfo.push_back(rebase); |
| 1058 | } |
| 1059 | // A pointer to an SharedLibraryAtom requires binding. |
| 1060 | if (const SharedLibraryAtom *sa = dyn_cast<SharedLibraryAtom>(targ)) { |
| 1061 | BindLocation bind; |
| 1062 | bind.segIndex = segmentIndex; |
| 1063 | bind.segOffset = segmentOffset; |
| 1064 | bind.kind = llvm::MachO::BIND_TYPE_POINTER; |
| 1065 | bind.canBeNull = sa->canBeNullAtRuntime(); |
| 1066 | bind.ordinal = dylibOrdinal(sa); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 1067 | bind.symbolName = targ->name(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1068 | bind.addend = ref->addend(); |
| 1069 | nFile.bindingInfo.push_back(bind); |
| 1070 | } |
| 1071 | } |
Nick Kledzik | 2d43235 | 2014-07-17 23:16:21 +0000 | [diff] [blame] | 1072 | if (_archHandler.isLazyPointer(*ref)) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1073 | BindLocation bind; |
| 1074 | bind.segIndex = segmentIndex; |
| 1075 | bind.segOffset = segmentOffset; |
| 1076 | bind.kind = llvm::MachO::BIND_TYPE_POINTER; |
| 1077 | bind.canBeNull = false; //sa->canBeNullAtRuntime(); |
Nick Kledzik | 2d43235 | 2014-07-17 23:16:21 +0000 | [diff] [blame] | 1078 | bind.ordinal = 1; // FIXME |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 1079 | bind.symbolName = targ->name(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1080 | bind.addend = ref->addend(); |
| 1081 | nFile.lazyBindingInfo.push_back(bind); |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | uint32_t Util::fileFlags() { |
Nick Kledzik | e1aaced | 2014-07-22 00:49:49 +0000 | [diff] [blame] | 1089 | // FIXME: these need to determined at runtime. |
| 1090 | if (_context.outputMachOType() == MH_OBJECT) { |
| 1091 | return MH_SUBSECTIONS_VIA_SYMBOLS; |
| 1092 | } else { |
| 1093 | return MH_DYLDLINK | MH_NOUNDEFS | MH_TWOLEVEL; |
| 1094 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | } // end anonymous namespace |
| 1098 | |
| 1099 | |
| 1100 | namespace lld { |
| 1101 | namespace mach_o { |
| 1102 | namespace normalized { |
| 1103 | |
| 1104 | /// Convert a set of Atoms into a normalized mach-o file. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 1105 | ErrorOr<std::unique_ptr<NormalizedFile>> |
| 1106 | normalizedFromAtoms(const lld::File &atomFile, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1107 | const MachOLinkingContext &context) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 1108 | // The util object buffers info until the normalized file can be made. |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1109 | Util util(context); |
| 1110 | util.assignAtomsToSections(atomFile); |
| 1111 | util.organizeSections(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 1112 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1113 | std::unique_ptr<NormalizedFile> f(new NormalizedFile()); |
| 1114 | NormalizedFile &normFile = *f.get(); |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 1115 | normFile.arch = context.arch(); |
| 1116 | normFile.fileType = context.outputMachOType(); |
| 1117 | normFile.flags = util.fileFlags(); |
| 1118 | normFile.installName = context.installName(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1119 | util.addDependentDylibs(atomFile, normFile); |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 1120 | util.copySegmentInfo(normFile); |
| 1121 | util.copySectionInfo(normFile); |
| 1122 | util.assignAddressesToSections(normFile); |
| 1123 | util.buildAtomToAddressMap(); |
| 1124 | util.updateSectionInfo(normFile); |
| 1125 | util.copySectionContent(normFile); |
Nick Kledzik | 8c0bf75 | 2014-08-21 01:59:11 +0000 | [diff] [blame] | 1126 | if (auto ec = util.addSymbols(atomFile, normFile)) { |
| 1127 | return ec; |
| 1128 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1129 | util.addIndirectSymbols(atomFile, normFile); |
| 1130 | util.addRebaseAndBindingInfo(atomFile, normFile); |
| 1131 | util.addSectionRelocs(atomFile, normFile); |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 1132 | util.buildDataInCodeArray(atomFile, normFile); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1133 | util.copyEntryPointAddress(normFile); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 1134 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1135 | return std::move(f); |
| 1136 | } |
| 1137 | |
| 1138 | |
| 1139 | } // namespace normalized |
| 1140 | } // namespace mach_o |
| 1141 | } // namespace lld |
| 1142 | |