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