Rui Ueyama | 0076215 | 2015-02-05 20:05:33 +0000 | [diff] [blame] | 1 | //===-- ReaderWriter/MachO/LayoutPass.cpp - Layout atoms ------------------===// |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 9 | |
Rui Ueyama | 0076215 | 2015-02-05 20:05:33 +0000 | [diff] [blame] | 10 | #include "LayoutPass.h" |
Michael J. Spencer | d4eb47c | 2013-04-06 00:56:40 +0000 | [diff] [blame] | 11 | #include "lld/Core/Instrumentation.h" |
Rui Ueyama | 87d10ef | 2015-01-26 20:18:37 +0000 | [diff] [blame] | 12 | #include "lld/Core/Parallel.h" |
Rui Ueyama | 0076215 | 2015-02-05 20:05:33 +0000 | [diff] [blame] | 13 | #include "lld/Core/PassManager.h" |
| 14 | #include "lld/ReaderWriter/MachOLinkingContext.h" |
Benjamin Kramer | 06a42af | 2015-03-02 00:48:06 +0000 | [diff] [blame^] | 15 | #include "llvm/ADT/STLExtras.h" |
Rui Ueyama | a6b71ca | 2013-06-07 20:18:39 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Twine.h" |
Michael J. Spencer | 7f09a3d | 2013-02-26 01:35:30 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
Shankar Easwaran | 2b67fca | 2014-10-18 05:33:55 +0000 | [diff] [blame] | 18 | #include <algorithm> |
| 19 | #include <set> |
Michael J. Spencer | 7f09a3d | 2013-02-26 01:35:30 +0000 | [diff] [blame] | 20 | |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 21 | using namespace lld; |
| 22 | |
Chandler Carruth | 9afe32d | 2014-04-22 03:21:31 +0000 | [diff] [blame] | 23 | #define DEBUG_TYPE "LayoutPass" |
| 24 | |
Rui Ueyama | 0076215 | 2015-02-05 20:05:33 +0000 | [diff] [blame] | 25 | namespace lld { |
| 26 | namespace mach_o { |
| 27 | |
Rui Ueyama | 5af4622 | 2013-12-08 03:24:09 +0000 | [diff] [blame] | 28 | static bool compareAtoms(const LayoutPass::SortKey &, |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 29 | const LayoutPass::SortKey &, |
Rui Ueyama | 540842c | 2015-02-05 20:08:04 +0000 | [diff] [blame] | 30 | LayoutPass::SortOverride customSorter); |
Rui Ueyama | 5af4622 | 2013-12-08 03:24:09 +0000 | [diff] [blame] | 31 | |
Rui Ueyama | 2994f6f | 2013-12-08 03:37:58 +0000 | [diff] [blame] | 32 | #ifndef NDEBUG |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 33 | // Return "reason (leftval, rightval)" |
Rui Ueyama | 5af4622 | 2013-12-08 03:24:09 +0000 | [diff] [blame] | 34 | static std::string formatReason(StringRef reason, int leftVal, int rightVal) { |
Rui Ueyama | 2ea8639 | 2014-09-18 23:21:39 +0000 | [diff] [blame] | 35 | return (Twine(reason) + " (" + Twine(leftVal) + ", " + Twine(rightVal) + ")") |
| 36 | .str(); |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 37 | } |
Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 38 | |
| 39 | // Less-than relationship of two atoms must be transitive, which is, if a < b |
| 40 | // and b < c, a < c must be true. This function checks the transitivity by |
| 41 | // checking the sort results. |
Rui Ueyama | 540842c | 2015-02-05 20:08:04 +0000 | [diff] [blame] | 42 | static void checkTransitivity(std::vector<LayoutPass::SortKey> &vec, |
| 43 | LayoutPass::SortOverride customSorter) { |
Rui Ueyama | 37c43e9f | 2013-12-08 03:12:08 +0000 | [diff] [blame] | 44 | for (auto i = vec.begin(), e = vec.end(); (i + 1) != e; ++i) { |
| 45 | for (auto j = i + 1; j != e; ++j) { |
Rui Ueyama | 540842c | 2015-02-05 20:08:04 +0000 | [diff] [blame] | 46 | assert(compareAtoms(*i, *j, customSorter)); |
| 47 | assert(!compareAtoms(*j, *i, customSorter)); |
Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 50 | } |
Rui Ueyama | a930d12 | 2013-12-09 00:37:19 +0000 | [diff] [blame] | 51 | |
| 52 | // Helper functions to check follow-on graph. |
| 53 | typedef llvm::DenseMap<const DefinedAtom *, const DefinedAtom *> AtomToAtomT; |
| 54 | |
| 55 | static std::string atomToDebugString(const Atom *atom) { |
| 56 | const DefinedAtom *definedAtom = dyn_cast<DefinedAtom>(atom); |
| 57 | std::string str; |
| 58 | llvm::raw_string_ostream s(str); |
| 59 | if (definedAtom->name().empty()) |
| 60 | s << "<anonymous " << definedAtom << ">"; |
| 61 | else |
| 62 | s << definedAtom->name(); |
| 63 | s << " in "; |
| 64 | if (definedAtom->customSectionName().empty()) |
| 65 | s << "<anonymous>"; |
| 66 | else |
| 67 | s << definedAtom->customSectionName(); |
| 68 | s.flush(); |
| 69 | return str; |
| 70 | } |
| 71 | |
Nico Rieck | b9d84f4 | 2014-02-24 21:14:37 +0000 | [diff] [blame] | 72 | static void showCycleDetectedError(const Registry ®istry, |
| 73 | AtomToAtomT &followOnNexts, |
Rui Ueyama | a930d12 | 2013-12-09 00:37:19 +0000 | [diff] [blame] | 74 | const DefinedAtom *atom) { |
| 75 | const DefinedAtom *start = atom; |
| 76 | llvm::dbgs() << "There's a cycle in a follow-on chain!\n"; |
| 77 | do { |
| 78 | llvm::dbgs() << " " << atomToDebugString(atom) << "\n"; |
| 79 | for (const Reference *ref : *atom) { |
Nico Rieck | b9d84f4 | 2014-02-24 21:14:37 +0000 | [diff] [blame] | 80 | StringRef kindValStr; |
| 81 | if (!registry.referenceKindToString(ref->kindNamespace(), ref->kindArch(), |
| 82 | ref->kindValue(), kindValStr)) { |
| 83 | kindValStr = "<unknown>"; |
| 84 | } |
| 85 | llvm::dbgs() << " " << kindValStr |
| 86 | << ": " << atomToDebugString(ref->target()) << "\n"; |
Rui Ueyama | a930d12 | 2013-12-09 00:37:19 +0000 | [diff] [blame] | 87 | } |
| 88 | atom = followOnNexts[atom]; |
| 89 | } while (atom != start); |
| 90 | llvm::report_fatal_error("Cycle detected"); |
| 91 | } |
| 92 | |
| 93 | /// Exit if there's a cycle in a followon chain reachable from the |
| 94 | /// given root atom. Uses the tortoise and hare algorithm to detect a |
| 95 | /// cycle. |
Nico Rieck | b9d84f4 | 2014-02-24 21:14:37 +0000 | [diff] [blame] | 96 | static void checkNoCycleInFollowonChain(const Registry ®istry, |
| 97 | AtomToAtomT &followOnNexts, |
Rui Ueyama | a930d12 | 2013-12-09 00:37:19 +0000 | [diff] [blame] | 98 | const DefinedAtom *root) { |
| 99 | const DefinedAtom *tortoise = root; |
| 100 | const DefinedAtom *hare = followOnNexts[root]; |
| 101 | while (true) { |
| 102 | if (!tortoise || !hare) |
| 103 | return; |
| 104 | if (tortoise == hare) |
Nico Rieck | b9d84f4 | 2014-02-24 21:14:37 +0000 | [diff] [blame] | 105 | showCycleDetectedError(registry, followOnNexts, tortoise); |
Rui Ueyama | a930d12 | 2013-12-09 00:37:19 +0000 | [diff] [blame] | 106 | tortoise = followOnNexts[tortoise]; |
| 107 | hare = followOnNexts[followOnNexts[hare]]; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static void checkReachabilityFromRoot(AtomToAtomT &followOnRoots, |
| 112 | const DefinedAtom *atom) { |
| 113 | if (!atom) return; |
| 114 | auto i = followOnRoots.find(atom); |
| 115 | if (i == followOnRoots.end()) { |
Rui Ueyama | 2ea8639 | 2014-09-18 23:21:39 +0000 | [diff] [blame] | 116 | llvm_unreachable(((Twine("Atom <") + atomToDebugString(atom) + |
| 117 | "> has no follow-on root!")) |
| 118 | .str() |
| 119 | .c_str()); |
Rui Ueyama | a930d12 | 2013-12-09 00:37:19 +0000 | [diff] [blame] | 120 | } |
| 121 | const DefinedAtom *ap = i->second; |
| 122 | while (true) { |
| 123 | const DefinedAtom *next = followOnRoots[ap]; |
| 124 | if (!next) { |
Rui Ueyama | 2ea8639 | 2014-09-18 23:21:39 +0000 | [diff] [blame] | 125 | llvm_unreachable((Twine("Atom <" + atomToDebugString(atom) + |
| 126 | "> is not reachable from its root!")) |
| 127 | .str() |
| 128 | .c_str()); |
Rui Ueyama | a930d12 | 2013-12-09 00:37:19 +0000 | [diff] [blame] | 129 | } |
| 130 | if (next == ap) |
| 131 | return; |
| 132 | ap = next; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | static void printDefinedAtoms(const MutableFile::DefinedAtomRange &atomRange) { |
| 137 | for (const DefinedAtom *atom : atomRange) { |
| 138 | llvm::dbgs() << " file=" << atom->file().path() |
| 139 | << ", name=" << atom->name() |
| 140 | << ", size=" << atom->size() |
| 141 | << ", type=" << atom->contentType() |
| 142 | << ", ordinal=" << atom->ordinal() |
| 143 | << "\n"; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /// Verify that the followon chain is sane. Should not be called in |
| 148 | /// release binary. |
| 149 | void LayoutPass::checkFollowonChain(MutableFile::DefinedAtomRange &range) { |
| 150 | ScopedTask task(getDefaultDomain(), "LayoutPass::checkFollowonChain"); |
| 151 | |
| 152 | // Verify that there's no cycle in follow-on chain. |
| 153 | std::set<const DefinedAtom *> roots; |
| 154 | for (const auto &ai : _followOnRoots) |
| 155 | roots.insert(ai.second); |
| 156 | for (const DefinedAtom *root : roots) |
Nico Rieck | b9d84f4 | 2014-02-24 21:14:37 +0000 | [diff] [blame] | 157 | checkNoCycleInFollowonChain(_registry, _followOnNexts, root); |
Rui Ueyama | a930d12 | 2013-12-09 00:37:19 +0000 | [diff] [blame] | 158 | |
| 159 | // Verify that all the atoms in followOnNexts have references to |
| 160 | // their roots. |
| 161 | for (const auto &ai : _followOnNexts) { |
| 162 | checkReachabilityFromRoot(_followOnRoots, ai.first); |
| 163 | checkReachabilityFromRoot(_followOnRoots, ai.second); |
| 164 | } |
| 165 | } |
Rui Ueyama | 2994f6f | 2013-12-08 03:37:58 +0000 | [diff] [blame] | 166 | #endif // #ifndef NDEBUG |
Rui Ueyama | 37c43e9f | 2013-12-08 03:12:08 +0000 | [diff] [blame] | 167 | |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 168 | /// The function compares atoms by sorting atoms in the following order |
Shankar Easwaran | d6d1b52 | 2013-09-12 15:59:34 +0000 | [diff] [blame] | 169 | /// a) Sorts atoms by Section position preference |
Rui Ueyama | 8be4ce2 | 2014-03-27 22:08:26 +0000 | [diff] [blame] | 170 | /// b) Sorts atoms by their ordinal overrides (layout-after/ingroup) |
Shankar Easwaran | d6d1b52 | 2013-09-12 15:59:34 +0000 | [diff] [blame] | 171 | /// c) Sorts atoms by their permissions |
| 172 | /// d) Sorts atoms by their content |
Rui Ueyama | 540842c | 2015-02-05 20:08:04 +0000 | [diff] [blame] | 173 | /// e) Sorts atoms by custom sorter |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 174 | /// f) Sorts atoms on how they appear using File Ordinality |
| 175 | /// g) Sorts atoms on how they appear within the File |
Rui Ueyama | 5af4622 | 2013-12-08 03:24:09 +0000 | [diff] [blame] | 176 | static bool compareAtomsSub(const LayoutPass::SortKey &lc, |
| 177 | const LayoutPass::SortKey &rc, |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 178 | LayoutPass::SortOverride customSorter, |
Rui Ueyama | 5af4622 | 2013-12-08 03:24:09 +0000 | [diff] [blame] | 179 | std::string &reason) { |
Rui Ueyama | 37c43e9f | 2013-12-08 03:12:08 +0000 | [diff] [blame] | 180 | const DefinedAtom *left = lc._atom; |
| 181 | const DefinedAtom *right = rc._atom; |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 182 | if (left == right) { |
| 183 | reason = "same"; |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 184 | return false; |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 185 | } |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 186 | |
Shankar Easwaran | d8da989 | 2013-05-22 17:41:04 +0000 | [diff] [blame] | 187 | // Sort by section position preference. |
| 188 | DefinedAtom::SectionPosition leftPos = left->sectionPosition(); |
| 189 | DefinedAtom::SectionPosition rightPos = right->sectionPosition(); |
| 190 | |
Shankar Easwaran | d8da989 | 2013-05-22 17:41:04 +0000 | [diff] [blame] | 191 | bool leftSpecialPos = (leftPos != DefinedAtom::sectionPositionAny); |
| 192 | bool rightSpecialPos = (rightPos != DefinedAtom::sectionPositionAny); |
| 193 | if (leftSpecialPos || rightSpecialPos) { |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 194 | if (leftPos != rightPos) { |
| 195 | DEBUG(reason = formatReason("sectionPos", (int)leftPos, (int)rightPos)); |
Shankar Easwaran | d8da989 | 2013-05-22 17:41:04 +0000 | [diff] [blame] | 196 | return leftPos < rightPos; |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 197 | } |
Shankar Easwaran | d8da989 | 2013-05-22 17:41:04 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 200 | // Find the root of the chain if it is a part of a follow-on chain. |
Rui Ueyama | 37c43e9f | 2013-12-08 03:12:08 +0000 | [diff] [blame] | 201 | const DefinedAtom *leftRoot = lc._root; |
| 202 | const DefinedAtom *rightRoot = rc._root; |
Shankar Easwaran | f1b341c | 2013-09-12 15:43:09 +0000 | [diff] [blame] | 203 | |
| 204 | // Sort atoms by their ordinal overrides only if they fall in the same |
| 205 | // chain. |
Rui Ueyama | 37c43e9f | 2013-12-08 03:12:08 +0000 | [diff] [blame] | 206 | if (leftRoot == rightRoot) { |
| 207 | DEBUG(reason = formatReason("override", lc._override, rc._override)); |
| 208 | return lc._override < rc._override; |
Shankar Easwaran | 3c5d2c8 | 2013-05-10 16:44:02 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 211 | // Sort same permissions together. |
Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 212 | DefinedAtom::ContentPermissions leftPerms = leftRoot->permissions(); |
| 213 | DefinedAtom::ContentPermissions rightPerms = rightRoot->permissions(); |
Shankar Easwaran | 8c25685 | 2013-03-13 04:05:38 +0000 | [diff] [blame] | 214 | |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 215 | if (leftPerms != rightPerms) { |
| 216 | DEBUG(reason = |
| 217 | formatReason("contentPerms", (int)leftPerms, (int)rightPerms)); |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 218 | return leftPerms < rightPerms; |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 219 | } |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 220 | |
| 221 | // Sort same content types together. |
Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 222 | DefinedAtom::ContentType leftType = leftRoot->contentType(); |
| 223 | DefinedAtom::ContentType rightType = rightRoot->contentType(); |
Shankar Easwaran | 8c25685 | 2013-03-13 04:05:38 +0000 | [diff] [blame] | 224 | |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 225 | if (leftType != rightType) { |
| 226 | DEBUG(reason = formatReason("contentType", (int)leftType, (int)rightType)); |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 227 | return leftType < rightType; |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 228 | } |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 229 | |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 230 | // Use custom sorter if supplied. |
| 231 | if (customSorter) { |
| 232 | bool leftBeforeRight; |
| 233 | if (customSorter(leftRoot, rightRoot, leftBeforeRight)) |
| 234 | return leftBeforeRight; |
| 235 | } |
| 236 | |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 237 | // Sort by .o order. |
Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 238 | const File *leftFile = &leftRoot->file(); |
| 239 | const File *rightFile = &rightRoot->file(); |
Shankar Easwaran | 8c25685 | 2013-03-13 04:05:38 +0000 | [diff] [blame] | 240 | |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 241 | if (leftFile != rightFile) { |
| 242 | DEBUG(reason = formatReason(".o order", (int)leftFile->ordinal(), |
| 243 | (int)rightFile->ordinal())); |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 244 | return leftFile->ordinal() < rightFile->ordinal(); |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 245 | } |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 246 | |
| 247 | // Sort by atom order with .o file. |
Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 248 | uint64_t leftOrdinal = leftRoot->ordinal(); |
| 249 | uint64_t rightOrdinal = rightRoot->ordinal(); |
Shankar Easwaran | 8c25685 | 2013-03-13 04:05:38 +0000 | [diff] [blame] | 250 | |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 251 | if (leftOrdinal != rightOrdinal) { |
Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 252 | DEBUG(reason = formatReason("ordinal", (int)leftRoot->ordinal(), |
| 253 | (int)rightRoot->ordinal())); |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 254 | return leftOrdinal < rightOrdinal; |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 255 | } |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 256 | |
Rui Ueyama | cd48075 | 2013-11-27 01:33:42 +0000 | [diff] [blame] | 257 | llvm::errs() << "Unordered: <" << left->name() << "> <" |
| 258 | << right->name() << ">\n"; |
Shankar Easwaran | bcf3656 | 2013-10-11 01:50:04 +0000 | [diff] [blame] | 259 | llvm_unreachable("Atoms with Same Ordinal!"); |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Rui Ueyama | 5af4622 | 2013-12-08 03:24:09 +0000 | [diff] [blame] | 262 | static bool compareAtoms(const LayoutPass::SortKey &lc, |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 263 | const LayoutPass::SortKey &rc, |
| 264 | LayoutPass::SortOverride customSorter) { |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 265 | std::string reason; |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 266 | bool result = compareAtomsSub(lc, rc, customSorter, reason); |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 267 | DEBUG({ |
Rui Ueyama | 4050b20 | 2013-10-18 03:18:52 +0000 | [diff] [blame] | 268 | StringRef comp = result ? "<" : ">="; |
Rui Ueyama | 37c43e9f | 2013-12-08 03:12:08 +0000 | [diff] [blame] | 269 | llvm::dbgs() << "Layout: '" << lc._atom->name() << "' " << comp << " '" |
| 270 | << rc._atom->name() << "' (" << reason << ")\n"; |
Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 271 | }); |
| 272 | return result; |
| 273 | } |
| 274 | |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 275 | LayoutPass::LayoutPass(const Registry ®istry, SortOverride sorter) |
| 276 | : _registry(registry), _customSorter(sorter) {} |
Nico Rieck | b9d84f4 | 2014-02-24 21:14:37 +0000 | [diff] [blame] | 277 | |
Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 278 | // Returns the atom immediately followed by the given atom in the followon |
| 279 | // chain. |
| 280 | const DefinedAtom *LayoutPass::findAtomFollowedBy( |
| 281 | const DefinedAtom *targetAtom) { |
| 282 | // Start from the beginning of the chain and follow the chain until |
| 283 | // we find the targetChain. |
| 284 | const DefinedAtom *atom = _followOnRoots[targetAtom]; |
| 285 | while (true) { |
| 286 | const DefinedAtom *prevAtom = atom; |
| 287 | AtomToAtomT::iterator targetFollowOnAtomsIter = _followOnNexts.find(atom); |
| 288 | // The target atom must be in the chain of its root. |
| 289 | assert(targetFollowOnAtomsIter != _followOnNexts.end()); |
| 290 | atom = targetFollowOnAtomsIter->second; |
| 291 | if (atom == targetAtom) |
| 292 | return prevAtom; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // Check if all the atoms followed by the given target atom are of size zero. |
| 297 | // When this method is called, an atom being added is not of size zero and |
| 298 | // will be added to the head of the followon chain. All the atoms between the |
| 299 | // atom and the targetAtom (specified by layout-after) need to be of size zero |
| 300 | // in this case. Otherwise the desired layout is impossible. |
| 301 | bool LayoutPass::checkAllPrevAtomsZeroSize(const DefinedAtom *targetAtom) { |
| 302 | const DefinedAtom *atom = _followOnRoots[targetAtom]; |
| 303 | while (true) { |
Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 304 | if (atom == targetAtom) |
| 305 | return true; |
Rui Ueyama | 0196d106 | 2013-05-14 16:53:59 +0000 | [diff] [blame] | 306 | if (atom->size() != 0) |
Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 307 | // TODO: print warning that an impossible layout is being desired by the |
| 308 | // user. |
| 309 | return false; |
Rui Ueyama | 5ec6d1a | 2013-05-14 01:51:56 +0000 | [diff] [blame] | 310 | AtomToAtomT::iterator targetFollowOnAtomsIter = _followOnNexts.find(atom); |
| 311 | // The target atom must be in the chain of its root. |
| 312 | assert(targetFollowOnAtomsIter != _followOnNexts.end()); |
| 313 | atom = targetFollowOnAtomsIter->second; |
Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
| 317 | // Set the root of all atoms in targetAtom's chain to the given root. |
| 318 | void LayoutPass::setChainRoot(const DefinedAtom *targetAtom, |
| 319 | const DefinedAtom *root) { |
| 320 | // Walk through the followon chain and override each node's root. |
| 321 | while (true) { |
| 322 | _followOnRoots[targetAtom] = root; |
| 323 | AtomToAtomT::iterator targetFollowOnAtomsIter = |
| 324 | _followOnNexts.find(targetAtom); |
| 325 | if (targetFollowOnAtomsIter == _followOnNexts.end()) |
| 326 | return; |
| 327 | targetAtom = targetFollowOnAtomsIter->second; |
| 328 | } |
| 329 | } |
| 330 | |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 331 | /// This pass builds the followon tables described by two DenseMaps |
| 332 | /// followOnRoots and followonNexts. |
| 333 | /// The followOnRoots map contains a mapping of a DefinedAtom to its root |
| 334 | /// The followOnNexts map contains a mapping of what DefinedAtom follows the |
| 335 | /// current Atom |
| 336 | /// The algorithm follows a very simple approach |
| 337 | /// a) If the atom is first seen, then make that as the root atom |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 338 | /// b) The targetAtom which this Atom contains, has the root thats set to the |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 339 | /// root of the current atom |
| 340 | /// c) If the targetAtom is part of a different tree and the root of the |
| 341 | /// targetAtom is itself, Chain all the atoms that are contained in the tree |
| 342 | /// to the current Tree |
| 343 | /// d) If the targetAtom is part of a different chain and the root of the |
| 344 | /// targetAtom until the targetAtom has all atoms of size 0, then chain the |
| 345 | /// targetAtoms and its tree to the current chain |
| 346 | void LayoutPass::buildFollowOnTable(MutableFile::DefinedAtomRange &range) { |
Michael J. Spencer | d4eb47c | 2013-04-06 00:56:40 +0000 | [diff] [blame] | 347 | ScopedTask task(getDefaultDomain(), "LayoutPass::buildFollowOnTable"); |
Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 348 | // Set the initial size of the followon and the followonNext hash to the |
| 349 | // number of atoms that we have. |
Shankar Easwaran | 45a5f93 | 2013-04-29 03:27:57 +0000 | [diff] [blame] | 350 | _followOnRoots.resize(range.size()); |
| 351 | _followOnNexts.resize(range.size()); |
Rui Ueyama | 0196d106 | 2013-05-14 16:53:59 +0000 | [diff] [blame] | 352 | for (const DefinedAtom *ai : range) { |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 353 | for (const Reference *r : *ai) { |
Rui Ueyama | c9411c3 | 2014-03-21 21:46:49 +0000 | [diff] [blame] | 354 | if (r->kindNamespace() != lld::Reference::KindNamespace::all || |
| 355 | r->kindValue() != lld::Reference::kindLayoutAfter) |
Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 356 | continue; |
Rui Ueyama | c1800be | 2013-11-05 01:37:40 +0000 | [diff] [blame] | 357 | const DefinedAtom *targetAtom = dyn_cast<DefinedAtom>(r->target()); |
Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 358 | _followOnNexts[ai] = targetAtom; |
| 359 | |
Alp Toker | 2259376 | 2013-12-02 01:28:14 +0000 | [diff] [blame] | 360 | // If we find a followon for the first time, let's make that atom as the |
Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 361 | // root atom. |
| 362 | if (_followOnRoots.count(ai) == 0) |
| 363 | _followOnRoots[ai] = ai; |
| 364 | |
| 365 | auto iter = _followOnRoots.find(targetAtom); |
| 366 | if (iter == _followOnRoots.end()) { |
Alp Toker | 2259376 | 2013-12-02 01:28:14 +0000 | [diff] [blame] | 367 | // If the targetAtom is not a root of any chain, let's make the root of |
Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 368 | // the targetAtom to the root of the current chain. |
Rui Ueyama | a821d32 | 2014-07-25 19:46:31 +0000 | [diff] [blame] | 369 | |
| 370 | // The expression m[i] = m[j] where m is a DenseMap and i != j is not |
| 371 | // safe. m[j] returns a reference, which would be invalidated when a |
| 372 | // rehashing occurs. If rehashing occurs to make room for m[i], m[j] |
| 373 | // becomes invalid, and that invalid reference would be used as the RHS |
| 374 | // value of the expression. |
| 375 | // Copy the value to workaround. |
| 376 | const DefinedAtom *tmp = _followOnRoots[ai]; |
| 377 | _followOnRoots[targetAtom] = tmp; |
Rui Ueyama | c9411c3 | 2014-03-21 21:46:49 +0000 | [diff] [blame] | 378 | continue; |
| 379 | } |
| 380 | if (iter->second == targetAtom) { |
Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 381 | // If the targetAtom is the root of a chain, the chain becomes part of |
| 382 | // the current chain. Rewrite the subchain's root to the current |
| 383 | // chain's root. |
| 384 | setChainRoot(targetAtom, _followOnRoots[ai]); |
Rui Ueyama | c9411c3 | 2014-03-21 21:46:49 +0000 | [diff] [blame] | 385 | continue; |
Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 386 | } |
Rui Ueyama | c9411c3 | 2014-03-21 21:46:49 +0000 | [diff] [blame] | 387 | // The targetAtom is already a part of a chain. If the current atom is |
| 388 | // of size zero, we can insert it in the middle of the chain just |
| 389 | // before the target atom, while not breaking other atom's followon |
| 390 | // relationships. If it's not, we can only insert the current atom at |
| 391 | // the beginning of the chain. All the atoms followed by the target |
| 392 | // atom must be of size zero in that case to satisfy the followon |
| 393 | // relationships. |
| 394 | size_t currentAtomSize = ai->size(); |
| 395 | if (currentAtomSize == 0) { |
| 396 | const DefinedAtom *targetPrevAtom = findAtomFollowedBy(targetAtom); |
| 397 | _followOnNexts[targetPrevAtom] = ai; |
Rui Ueyama | a821d32 | 2014-07-25 19:46:31 +0000 | [diff] [blame] | 398 | const DefinedAtom *tmp = _followOnRoots[targetPrevAtom]; |
| 399 | _followOnRoots[ai] = tmp; |
Rui Ueyama | c9411c3 | 2014-03-21 21:46:49 +0000 | [diff] [blame] | 400 | continue; |
| 401 | } |
| 402 | if (!checkAllPrevAtomsZeroSize(targetAtom)) |
| 403 | break; |
| 404 | _followOnNexts[ai] = _followOnRoots[targetAtom]; |
| 405 | setChainRoot(_followOnRoots[targetAtom], _followOnRoots[ai]); |
Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 410 | /// Build an ordinal override map by traversing the followon chain, and |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 411 | /// assigning ordinals to each atom, if the atoms have their ordinals |
| 412 | /// already assigned skip the atom and move to the next. This is the |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 413 | /// main map thats used to sort the atoms while comparing two atoms together |
| 414 | void LayoutPass::buildOrdinalOverrideMap(MutableFile::DefinedAtomRange &range) { |
Michael J. Spencer | d4eb47c | 2013-04-06 00:56:40 +0000 | [diff] [blame] | 415 | ScopedTask task(getDefaultDomain(), "LayoutPass::buildOrdinalOverrideMap"); |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 416 | uint64_t index = 0; |
Rui Ueyama | 0196d106 | 2013-05-14 16:53:59 +0000 | [diff] [blame] | 417 | for (const DefinedAtom *ai : range) { |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 418 | const DefinedAtom *atom = ai; |
Michael J. Spencer | 1ecf890 | 2013-03-12 00:10:00 +0000 | [diff] [blame] | 419 | if (_ordinalOverrideMap.find(atom) != _ordinalOverrideMap.end()) |
| 420 | continue; |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 421 | AtomToAtomT::iterator start = _followOnRoots.find(atom); |
Rui Ueyama | c9411c3 | 2014-03-21 21:46:49 +0000 | [diff] [blame] | 422 | if (start == _followOnRoots.end()) |
| 423 | continue; |
| 424 | for (const DefinedAtom *nextAtom = start->second; nextAtom != NULL; |
| 425 | nextAtom = _followOnNexts[nextAtom]) { |
| 426 | AtomToOrdinalT::iterator pos = _ordinalOverrideMap.find(nextAtom); |
| 427 | if (pos == _ordinalOverrideMap.end()) |
| 428 | _ordinalOverrideMap[nextAtom] = index++; |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
Rui Ueyama | 37c43e9f | 2013-12-08 03:12:08 +0000 | [diff] [blame] | 433 | std::vector<LayoutPass::SortKey> |
| 434 | LayoutPass::decorate(MutableFile::DefinedAtomRange &atomRange) const { |
| 435 | std::vector<SortKey> ret; |
| 436 | for (const DefinedAtom *atom : atomRange) { |
| 437 | auto ri = _followOnRoots.find(atom); |
| 438 | auto oi = _ordinalOverrideMap.find(atom); |
| 439 | const DefinedAtom *root = (ri == _followOnRoots.end()) ? atom : ri->second; |
| 440 | uint64_t override = (oi == _ordinalOverrideMap.end()) ? 0 : oi->second; |
| 441 | ret.push_back(SortKey(atom, root, override)); |
| 442 | } |
| 443 | return ret; |
| 444 | } |
| 445 | |
| 446 | void LayoutPass::undecorate(MutableFile::DefinedAtomRange &atomRange, |
| 447 | std::vector<SortKey> &keys) const { |
| 448 | size_t i = 0; |
| 449 | for (SortKey &k : keys) |
| 450 | atomRange[i++] = k._atom; |
| 451 | } |
| 452 | |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 453 | /// Perform the actual pass |
Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 454 | void LayoutPass::perform(std::unique_ptr<MutableFile> &mergedFile) { |
Rui Ueyama | 37c43e9f | 2013-12-08 03:12:08 +0000 | [diff] [blame] | 455 | // sort the atoms |
Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 456 | ScopedTask task(getDefaultDomain(), "LayoutPass"); |
Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 457 | MutableFile::DefinedAtomRange atomRange = mergedFile->definedAtoms(); |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 458 | |
| 459 | // Build follow on tables |
| 460 | buildFollowOnTable(atomRange); |
| 461 | |
Rui Ueyama | a6b71ca | 2013-06-07 20:18:39 +0000 | [diff] [blame] | 462 | // Check the structure of followon graph if running in debug mode. |
| 463 | DEBUG(checkFollowonChain(atomRange)); |
| 464 | |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 465 | // Build override maps |
| 466 | buildOrdinalOverrideMap(atomRange); |
| 467 | |
Rui Ueyama | 9c4f89a | 2013-05-23 01:31:25 +0000 | [diff] [blame] | 468 | DEBUG({ |
Nick Kledzik | f4fa8c0 | 2013-04-04 20:32:18 +0000 | [diff] [blame] | 469 | llvm::dbgs() << "unsorted atoms:\n"; |
Rui Ueyama | a6b71ca | 2013-06-07 20:18:39 +0000 | [diff] [blame] | 470 | printDefinedAtoms(atomRange); |
Nick Kledzik | f4fa8c0 | 2013-04-04 20:32:18 +0000 | [diff] [blame] | 471 | }); |
Shankar Easwaran | 45a5f93 | 2013-04-29 03:27:57 +0000 | [diff] [blame] | 472 | |
Rui Ueyama | 37c43e9f | 2013-12-08 03:12:08 +0000 | [diff] [blame] | 473 | std::vector<LayoutPass::SortKey> vec = decorate(atomRange); |
Rui Ueyama | 87d10ef | 2015-01-26 20:18:37 +0000 | [diff] [blame] | 474 | parallel_sort(vec.begin(), vec.end(), |
Nick Kledzik | 82d24bc | 2014-11-07 21:01:21 +0000 | [diff] [blame] | 475 | [&](const LayoutPass::SortKey &l, const LayoutPass::SortKey &r) -> bool { |
| 476 | return compareAtoms(l, r, _customSorter); |
| 477 | }); |
Rui Ueyama | 540842c | 2015-02-05 20:08:04 +0000 | [diff] [blame] | 478 | DEBUG(checkTransitivity(vec, _customSorter)); |
Rui Ueyama | 37c43e9f | 2013-12-08 03:12:08 +0000 | [diff] [blame] | 479 | undecorate(atomRange, vec); |
Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 480 | |
Rui Ueyama | 9c4f89a | 2013-05-23 01:31:25 +0000 | [diff] [blame] | 481 | DEBUG({ |
Nick Kledzik | f4fa8c0 | 2013-04-04 20:32:18 +0000 | [diff] [blame] | 482 | llvm::dbgs() << "sorted atoms:\n"; |
Rui Ueyama | a6b71ca | 2013-06-07 20:18:39 +0000 | [diff] [blame] | 483 | printDefinedAtoms(atomRange); |
Nick Kledzik | f4fa8c0 | 2013-04-04 20:32:18 +0000 | [diff] [blame] | 484 | }); |
Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 485 | } |
Rui Ueyama | 0076215 | 2015-02-05 20:05:33 +0000 | [diff] [blame] | 486 | |
| 487 | void addLayoutPass(PassManager &pm, const MachOLinkingContext &ctx) { |
Rui Ueyama | 92634be | 2015-02-06 22:44:16 +0000 | [diff] [blame] | 488 | pm.add(llvm::make_unique<LayoutPass>( |
Rui Ueyama | 0076215 | 2015-02-05 20:05:33 +0000 | [diff] [blame] | 489 | ctx.registry(), [&](const DefinedAtom * left, const DefinedAtom * right, |
Rui Ueyama | 540842c | 2015-02-05 20:08:04 +0000 | [diff] [blame] | 490 | bool & leftBeforeRight) ->bool { |
Rui Ueyama | 0076215 | 2015-02-05 20:05:33 +0000 | [diff] [blame] | 491 | return ctx.customAtomOrderer(left, right, leftBeforeRight); |
Rui Ueyama | 92634be | 2015-02-06 22:44:16 +0000 | [diff] [blame] | 492 | })); |
Rui Ueyama | 0076215 | 2015-02-05 20:05:33 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | } // namespace mach_o |
| 496 | } // namespace lld |