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