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