Coverage Report

Created: 2018-10-23 15:26

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/lib/ReaderWriter/MachO/LayoutPass.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ReaderWriter/MachO/LayoutPass.cpp - Layout atoms ------------------===//
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
#include "LayoutPass.h"
11
#include "lld/Core/Instrumentation.h"
12
#include "lld/Core/PassManager.h"
13
#include "lld/ReaderWriter/MachOLinkingContext.h"
14
#include "llvm/ADT/STLExtras.h"
15
#include "llvm/ADT/Twine.h"
16
#include "llvm/Support/Debug.h"
17
#include "llvm/Support/Parallel.h"
18
#include <algorithm>
19
#include <set>
20
#include <utility>
21
22
using namespace lld;
23
24
#define DEBUG_TYPE "LayoutPass"
25
26
namespace lld {
27
namespace mach_o {
28
29
static bool compareAtoms(const LayoutPass::SortKey &,
30
                         const LayoutPass::SortKey &,
31
                         LayoutPass::SortOverride customSorter);
32
33
#ifndef NDEBUG
34
// Return "reason (leftval, rightval)"
35
static std::string formatReason(StringRef reason, int leftVal, int rightVal) {
36
  return (Twine(reason) + " (" + Twine(leftVal) + ", " + Twine(rightVal) + ")")
37
      .str();
38
}
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.
43
static void checkTransitivity(std::vector<LayoutPass::SortKey> &vec,
44
                              LayoutPass::SortOverride customSorter) {
45
  for (auto i = vec.begin(), e = vec.end(); (i + 1) != e; ++i) {
46
    for (auto j = i + 1; j != e; ++j) {
47
      assert(compareAtoms(*i, *j, customSorter));
48
      assert(!compareAtoms(*j, *i, customSorter));
49
    }
50
  }
51
}
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
73
static void showCycleDetectedError(const Registry &registry,
74
                                   AtomToAtomT &followOnNexts,
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) {
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";
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.
97
static void checkNoCycleInFollowonChain(const Registry &registry,
98
                                        AtomToAtomT &followOnNexts,
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)
106
      showCycleDetectedError(registry, followOnNexts, tortoise);
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()) {
117
    llvm_unreachable(((Twine("Atom <") + atomToDebugString(atom) +
118
                       "> has no follow-on root!"))
119
                         .str()
120
                         .c_str());
121
  }
122
  const DefinedAtom *ap = i->second;
123
  while (true) {
124
    const DefinedAtom *next = followOnRoots[ap];
125
    if (!next) {
126
      llvm_unreachable((Twine("Atom <" + atomToDebugString(atom) +
127
                              "> is not reachable from its root!"))
128
                           .str()
129
                           .c_str());
130
    }
131
    if (next == ap)
132
      return;
133
    ap = next;
134
  }
135
}
136
137
static void printDefinedAtoms(const File::AtomRange<DefinedAtom> &atomRange) {
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.
150
void LayoutPass::checkFollowonChain(const File::AtomRange<DefinedAtom> &range) {
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)
158
    checkNoCycleInFollowonChain(_registry, _followOnNexts, root);
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
}
167
#endif // #ifndef NDEBUG
168
169
/// The function compares atoms by sorting atoms in the following order
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
176
static bool compareAtomsSub(const LayoutPass::SortKey &lc,
177
                            const LayoutPass::SortKey &rc,
178
                            LayoutPass::SortOverride customSorter,
179
1.28k
                            std::string &reason) {
180
1.28k
  const DefinedAtom *left = lc._atom.get();
181
1.28k
  const DefinedAtom *right = rc._atom.get();
182
1.28k
  if (left == right) {
183
44
    reason = "same";
184
44
    return false;
185
44
  }
186
1.24k
187
1.24k
  // Find the root of the chain if it is a part of a follow-on chain.
188
1.24k
  const DefinedAtom *leftRoot = lc._root;
189
1.24k
  const DefinedAtom *rightRoot = rc._root;
190
1.24k
191
1.24k
  // Sort atoms by their ordinal overrides only if they fall in the same
192
1.24k
  // chain.
193
1.24k
  if (leftRoot == rightRoot) {
194
4
    LLVM_DEBUG(reason = formatReason("override", lc._override, rc._override));
195
4
    return lc._override < rc._override;
196
4
  }
197
1.23k
198
1.23k
  // Sort same permissions together.
199
1.23k
  DefinedAtom::ContentPermissions leftPerms = leftRoot->permissions();
200
1.23k
  DefinedAtom::ContentPermissions rightPerms = rightRoot->permissions();
201
1.23k
202
1.23k
  if (leftPerms != rightPerms) {
203
658
    LLVM_DEBUG(
204
658
        reason = formatReason("contentPerms", (int)leftPerms, (int)rightPerms));
205
658
    return leftPerms < rightPerms;
206
658
  }
207
578
208
578
  // Sort same content types together.
209
578
  DefinedAtom::ContentType leftType = leftRoot->contentType();
210
578
  DefinedAtom::ContentType rightType = rightRoot->contentType();
211
578
212
578
  if (leftType != rightType) {
213
280
    LLVM_DEBUG(reason =
214
280
                   formatReason("contentType", (int)leftType, (int)rightType));
215
280
    return leftType < rightType;
216
280
  }
217
298
218
298
  // Use custom sorter if supplied.
219
298
  if (customSorter) {
220
298
    bool leftBeforeRight;
221
298
    if (customSorter(leftRoot, rightRoot, leftBeforeRight))
222
9
      return leftBeforeRight;
223
289
  }
224
289
225
289
  // Sort by .o order.
226
289
  const File *leftFile = &leftRoot->file();
227
289
  const File *rightFile = &rightRoot->file();
228
289
229
289
  if (leftFile != rightFile) {
230
24
    LLVM_DEBUG(reason = formatReason(".o order", (int)leftFile->ordinal(),
231
24
                                     (int)rightFile->ordinal()));
232
24
    return leftFile->ordinal() < rightFile->ordinal();
233
24
  }
234
265
235
265
  // Sort by atom order with .o file.
236
265
  uint64_t leftOrdinal = leftRoot->ordinal();
237
265
  uint64_t rightOrdinal = rightRoot->ordinal();
238
265
239
265
  if (leftOrdinal != rightOrdinal) {
240
265
    LLVM_DEBUG(reason = formatReason("ordinal", (int)leftRoot->ordinal(),
241
265
                                     (int)rightRoot->ordinal()));
242
265
    return leftOrdinal < rightOrdinal;
243
265
  }
244
0
245
0
  llvm::errs() << "Unordered: <" << left->name() << "> <"
246
0
               << right->name() << ">\n";
247
0
  llvm_unreachable("Atoms with Same Ordinal!");
248
0
}
249
250
static bool compareAtoms(const LayoutPass::SortKey &lc,
251
                         const LayoutPass::SortKey &rc,
252
1.28k
                         LayoutPass::SortOverride customSorter) {
253
1.28k
  std::string reason;
254
1.28k
  bool result = compareAtomsSub(lc, rc, customSorter, reason);
255
1.28k
  LLVM_DEBUG({
256
1.28k
    StringRef comp = result ? "<" : ">=";
257
1.28k
    llvm::dbgs() << "Layout: '" << lc._atom.get()->name()
258
1.28k
                 << "' " << comp << " '"
259
1.28k
                 << rc._atom.get()->name() << "' (" << reason << ")\n";
260
1.28k
  });
261
1.28k
  return result;
262
1.28k
}
263
264
LayoutPass::LayoutPass(const Registry &registry, SortOverride sorter)
265
169
    : _registry(registry), _customSorter(std::move(sorter)) {}
266
267
// Returns the atom immediately followed by the given atom in the followon
268
// chain.
269
const DefinedAtom *LayoutPass::findAtomFollowedBy(
270
0
    const DefinedAtom *targetAtom) {
271
0
  // Start from the beginning of the chain and follow the chain until
272
0
  // we find the targetChain.
273
0
  const DefinedAtom *atom = _followOnRoots[targetAtom];
274
0
  while (true) {
275
0
    const DefinedAtom *prevAtom = atom;
276
0
    AtomToAtomT::iterator targetFollowOnAtomsIter = _followOnNexts.find(atom);
277
0
    // The target atom must be in the chain of its root.
278
0
    assert(targetFollowOnAtomsIter != _followOnNexts.end());
279
0
    atom = targetFollowOnAtomsIter->second;
280
0
    if (atom == targetAtom)
281
0
      return prevAtom;
282
0
  }
283
0
}
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
0
bool LayoutPass::checkAllPrevAtomsZeroSize(const DefinedAtom *targetAtom) {
291
0
  const DefinedAtom *atom = _followOnRoots[targetAtom];
292
0
  while (true) {
293
0
    if (atom == targetAtom)
294
0
      return true;
295
0
    if (atom->size() != 0)
296
0
      // TODO: print warning that an impossible layout is being desired by the
297
0
      // user.
298
0
      return false;
299
0
    AtomToAtomT::iterator targetFollowOnAtomsIter = _followOnNexts.find(atom);
300
0
    // The target atom must be in the chain of its root.
301
0
    assert(targetFollowOnAtomsIter != _followOnNexts.end());
302
0
    atom = targetFollowOnAtomsIter->second;
303
0
  }
304
0
}
305
306
// Set the root of all atoms in targetAtom's chain to the given root.
307
void LayoutPass::setChainRoot(const DefinedAtom *targetAtom,
308
0
                              const DefinedAtom *root) {
309
0
  // Walk through the followon chain and override each node's root.
310
0
  while (true) {
311
0
    _followOnRoots[targetAtom] = root;
312
0
    AtomToAtomT::iterator targetFollowOnAtomsIter =
313
0
        _followOnNexts.find(targetAtom);
314
0
    if (targetFollowOnAtomsIter == _followOnNexts.end())
315
0
      return;
316
0
    targetAtom = targetFollowOnAtomsIter->second;
317
0
  }
318
0
}
319
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
327
/// b) The targetAtom which this Atom contains, has the root thats set to the
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
335
169
void LayoutPass::buildFollowOnTable(const File::AtomRange<DefinedAtom> &range) {
336
169
  ScopedTask task(getDefaultDomain(), "LayoutPass::buildFollowOnTable");
337
169
  // Set the initial size of the followon and the followonNext hash to the
338
169
  // number of atoms that we have.
339
169
  _followOnRoots.reserve(range.size());
340
169
  _followOnNexts.reserve(range.size());
341
729
  for (const DefinedAtom *ai : range) {
342
729
    for (const Reference *r : *ai) {
343
596
      if (r->kindNamespace() != lld::Reference::KindNamespace::all ||
344
596
          
r->kindValue() != lld::Reference::kindLayoutAfter4
)
345
592
        continue;
346
4
      const DefinedAtom *targetAtom = dyn_cast<DefinedAtom>(r->target());
347
4
      _followOnNexts[ai] = targetAtom;
348
4
349
4
      // If we find a followon for the first time, let's make that atom as the
350
4
      // root atom.
351
4
      if (_followOnRoots.count(ai) == 0)
352
3
        _followOnRoots[ai] = ai;
353
4
354
4
      auto iter = _followOnRoots.find(targetAtom);
355
4
      if (iter == _followOnRoots.end()) {
356
4
        // If the targetAtom is not a root of any chain, let's make the root of
357
4
        // the targetAtom to the root of the current chain.
358
4
359
4
        // The expression m[i] = m[j] where m is a DenseMap and i != j is not
360
4
        // safe. m[j] returns a reference, which would be invalidated when a
361
4
        // rehashing occurs. If rehashing occurs to make room for m[i], m[j]
362
4
        // becomes invalid, and that invalid reference would be used as the RHS
363
4
        // value of the expression.
364
4
        // Copy the value to workaround.
365
4
        const DefinedAtom *tmp = _followOnRoots[ai];
366
4
        _followOnRoots[targetAtom] = tmp;
367
4
        continue;
368
4
      }
369
0
      if (iter->second == targetAtom) {
370
0
        // If the targetAtom is the root of a chain, the chain becomes part of
371
0
        // the current chain. Rewrite the subchain's root to the current
372
0
        // chain's root.
373
0
        setChainRoot(targetAtom, _followOnRoots[ai]);
374
0
        continue;
375
0
      }
376
0
      // The targetAtom is already a part of a chain. If the current atom is
377
0
      // of size zero, we can insert it in the middle of the chain just
378
0
      // before the target atom, while not breaking other atom's followon
379
0
      // relationships. If it's not, we can only insert the current atom at
380
0
      // the beginning of the chain. All the atoms followed by the target
381
0
      // atom must be of size zero in that case to satisfy the followon
382
0
      // relationships.
383
0
      size_t currentAtomSize = ai->size();
384
0
      if (currentAtomSize == 0) {
385
0
        const DefinedAtom *targetPrevAtom = findAtomFollowedBy(targetAtom);
386
0
        _followOnNexts[targetPrevAtom] = ai;
387
0
        const DefinedAtom *tmp = _followOnRoots[targetPrevAtom];
388
0
        _followOnRoots[ai] = tmp;
389
0
        continue;
390
0
      }
391
0
      if (!checkAllPrevAtomsZeroSize(targetAtom))
392
0
        break;
393
0
      _followOnNexts[ai] = _followOnRoots[targetAtom];
394
0
      setChainRoot(_followOnRoots[targetAtom], _followOnRoots[ai]);
395
0
    }
396
729
  }
397
169
}
398
399
/// Build an ordinal override map by traversing the followon chain, and
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
402
/// main map thats used to sort the atoms while comparing two atoms together
403
void
404
169
LayoutPass::buildOrdinalOverrideMap(const File::AtomRange<DefinedAtom> &range) {
405
169
  ScopedTask task(getDefaultDomain(), "LayoutPass::buildOrdinalOverrideMap");
406
169
  uint64_t index = 0;
407
729
  for (const DefinedAtom *ai : range) {
408
729
    const DefinedAtom *atom = ai;
409
729
    if (_ordinalOverrideMap.find(atom) != _ordinalOverrideMap.end())
410
4
      continue;
411
725
    AtomToAtomT::iterator start = _followOnRoots.find(atom);
412
725
    if (start == _followOnRoots.end())
413
722
      continue;
414
10
    
for (const DefinedAtom *nextAtom = start->second; 3
nextAtom;
415
7
         nextAtom = _followOnNexts[nextAtom]) {
416
7
      AtomToOrdinalT::iterator pos = _ordinalOverrideMap.find(nextAtom);
417
7
      if (pos == _ordinalOverrideMap.end())
418
7
        _ordinalOverrideMap[nextAtom] = index++;
419
7
    }
420
3
  }
421
169
}
422
423
std::vector<LayoutPass::SortKey>
424
169
LayoutPass::decorate(File::AtomRange<DefinedAtom> &atomRange) const {
425
169
  std::vector<SortKey> ret;
426
729
  for (OwningAtomPtr<DefinedAtom> &atom : atomRange.owning_ptrs()) {
427
729
    auto ri = _followOnRoots.find(atom.get());
428
729
    auto oi = _ordinalOverrideMap.find(atom.get());
429
729
    const auto *root = (ri == _followOnRoots.end()) ? 
atom.get()722
:
ri->second7
;
430
729
    uint64_t override = (oi == _ordinalOverrideMap.end()) ? 
0722
:
oi->second7
;
431
729
    ret.push_back(SortKey(std::move(atom), root, override));
432
729
  }
433
169
  return ret;
434
169
}
435
436
void LayoutPass::undecorate(File::AtomRange<DefinedAtom> &atomRange,
437
169
                            std::vector<SortKey> &keys) const {
438
169
  size_t i = 0;
439
169
  for (SortKey &k : keys)
440
729
    atomRange[i++] = std::move(k._atom);
441
169
}
442
443
/// Perform the actual pass
444
169
llvm::Error LayoutPass::perform(SimpleFile &mergedFile) {
445
169
  LLVM_DEBUG(llvm::dbgs() << "******** Laying out atoms:\n");
446
169
  // sort the atoms
447
169
  ScopedTask task(getDefaultDomain(), "LayoutPass");
448
169
  File::AtomRange<DefinedAtom> atomRange = mergedFile.defined();
449
169
450
169
  // Build follow on tables
451
169
  buildFollowOnTable(atomRange);
452
169
453
169
  // Check the structure of followon graph if running in debug mode.
454
169
  LLVM_DEBUG(checkFollowonChain(atomRange));
455
169
456
169
  // Build override maps
457
169
  buildOrdinalOverrideMap(atomRange);
458
169
459
169
  LLVM_DEBUG({
460
169
    llvm::dbgs() << "unsorted atoms:\n";
461
169
    printDefinedAtoms(atomRange);
462
169
  });
463
169
464
169
  std::vector<LayoutPass::SortKey> vec = decorate(atomRange);
465
169
  sort(llvm::parallel::par, vec.begin(), vec.end(),
466
1.28k
       [&](const LayoutPass::SortKey &l, const LayoutPass::SortKey &r) -> bool {
467
1.28k
         return compareAtoms(l, r, _customSorter);
468
1.28k
       });
469
169
  LLVM_DEBUG(checkTransitivity(vec, _customSorter));
470
169
  undecorate(atomRange, vec);
471
169
472
169
  LLVM_DEBUG({
473
169
    llvm::dbgs() << "sorted atoms:\n";
474
169
    printDefinedAtoms(atomRange);
475
169
  });
476
169
477
169
  LLVM_DEBUG(llvm::dbgs() << "******** Finished laying out atoms\n");
478
169
  return llvm::Error::success();
479
169
}
480
481
169
void addLayoutPass(PassManager &pm, const MachOLinkingContext &ctx) {
482
169
  pm.add(llvm::make_unique<LayoutPass>(
483
169
      ctx.registry(), [&](const DefinedAtom * left, const DefinedAtom * right,
484
298
                          bool & leftBeforeRight) ->bool {
485
298
    return ctx.customAtomOrderer(left, right, leftBeforeRight);
486
298
  }));
487
169
}
488
489
} // namespace mach_o
490
} // namespace lld