2013-10-29 13:12:14 +08:00
|
|
|
//===--Passes/LayoutPass.cpp - Layout atoms -------------------------------===//
|
2013-02-08 04:16:12 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-12-08 11:12:08 +08:00
|
|
|
#include <algorithm>
|
2013-06-08 04:18:39 +08:00
|
|
|
#include <set>
|
|
|
|
|
2013-02-08 04:16:12 +08:00
|
|
|
#include "lld/Passes/LayoutPass.h"
|
2013-04-06 08:56:40 +08:00
|
|
|
#include "lld/Core/Instrumentation.h"
|
2013-06-08 04:18:39 +08:00
|
|
|
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2013-02-26 09:35:30 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
2013-02-08 04:16:12 +08:00
|
|
|
using namespace lld;
|
|
|
|
|
2014-04-22 11:21:31 +08:00
|
|
|
#define DEBUG_TYPE "LayoutPass"
|
|
|
|
|
2013-12-08 11:24:09 +08:00
|
|
|
static bool compareAtoms(const LayoutPass::SortKey &,
|
|
|
|
const LayoutPass::SortKey &);
|
|
|
|
|
2013-12-08 11:37:58 +08:00
|
|
|
#ifndef NDEBUG
|
2013-10-18 10:56:31 +08:00
|
|
|
// Return "reason (leftval, rightval)"
|
2013-12-08 11:24:09 +08:00
|
|
|
static std::string formatReason(StringRef reason, int leftVal, int rightVal) {
|
2013-10-18 10:56:31 +08:00
|
|
|
Twine msg =
|
|
|
|
Twine(reason) + " (" + Twine(leftVal) + ", " + Twine(rightVal) + ")";
|
2013-11-02 04:40:33 +08:00
|
|
|
return msg.str();
|
2013-10-18 10:56:31 +08:00
|
|
|
}
|
Fix bug that CompareAtoms::compare is not transitive.
This patch fixes a bug in r190608. The results of a comparison function
passed to std::sort must be transitive, which is, if a < b and b < c, and if
a != b, a < c must be also true. CompareAtoms::compare did not actually
guarantee the transitivity. As a result the sort results were sometimes just
wrong.
Consider there are three atoms, X, Y, and Z, whose file ordinals are 1, 2, 3,
respectively. Z has a property "layout-after X". In this case, all the
following conditionals become true:
X < Y because X's ordinal is less than Y's
Y < Z because Y's ordinal is less than Z's
Z < X because of the layout-after relationship
This is not of course transitive. The reason why this happened is because
we used follow-on relationships for comparison if two atoms falls in the same
follow-on chain, but we used each atom's properties if they did not. This patch
fixes the issue by using follow-on root atoms for comparison to get consistent
results.
Differential Revision: http://llvm-reviews.chandlerc.com/D1980
llvm-svn: 193029
2013-10-19 11:18:18 +08:00
|
|
|
|
|
|
|
// Less-than relationship of two atoms must be transitive, which is, if a < b
|
|
|
|
// and b < c, a < c must be true. This function checks the transitivity by
|
|
|
|
// checking the sort results.
|
2013-12-08 11:24:09 +08:00
|
|
|
static void checkTransitivity(std::vector<LayoutPass::SortKey> &vec) {
|
2013-12-08 11:12:08 +08:00
|
|
|
for (auto i = vec.begin(), e = vec.end(); (i + 1) != e; ++i) {
|
|
|
|
for (auto j = i + 1; j != e; ++j) {
|
|
|
|
assert(compareAtoms(*i, *j));
|
|
|
|
assert(!compareAtoms(*j, *i));
|
Fix bug that CompareAtoms::compare is not transitive.
This patch fixes a bug in r190608. The results of a comparison function
passed to std::sort must be transitive, which is, if a < b and b < c, and if
a != b, a < c must be also true. CompareAtoms::compare did not actually
guarantee the transitivity. As a result the sort results were sometimes just
wrong.
Consider there are three atoms, X, Y, and Z, whose file ordinals are 1, 2, 3,
respectively. Z has a property "layout-after X". In this case, all the
following conditionals become true:
X < Y because X's ordinal is less than Y's
Y < Z because Y's ordinal is less than Z's
Z < X because of the layout-after relationship
This is not of course transitive. The reason why this happened is because
we used follow-on relationships for comparison if two atoms falls in the same
follow-on chain, but we used each atom's properties if they did not. This patch
fixes the issue by using follow-on root atoms for comparison to get consistent
results.
Differential Revision: http://llvm-reviews.chandlerc.com/D1980
llvm-svn: 193029
2013-10-19 11:18:18 +08:00
|
|
|
}
|
|
|
|
}
|
2013-10-18 10:56:31 +08:00
|
|
|
}
|
2013-12-09 08:37:19 +08:00
|
|
|
|
|
|
|
// Helper functions to check follow-on graph.
|
|
|
|
typedef llvm::DenseMap<const DefinedAtom *, const DefinedAtom *> AtomToAtomT;
|
|
|
|
|
|
|
|
static std::string atomToDebugString(const Atom *atom) {
|
|
|
|
const DefinedAtom *definedAtom = dyn_cast<DefinedAtom>(atom);
|
|
|
|
std::string str;
|
|
|
|
llvm::raw_string_ostream s(str);
|
|
|
|
if (definedAtom->name().empty())
|
|
|
|
s << "<anonymous " << definedAtom << ">";
|
|
|
|
else
|
|
|
|
s << definedAtom->name();
|
|
|
|
s << " in ";
|
|
|
|
if (definedAtom->customSectionName().empty())
|
|
|
|
s << "<anonymous>";
|
|
|
|
else
|
|
|
|
s << definedAtom->customSectionName();
|
|
|
|
s.flush();
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2014-02-25 05:14:37 +08:00
|
|
|
static void showCycleDetectedError(const Registry ®istry,
|
|
|
|
AtomToAtomT &followOnNexts,
|
2013-12-09 08:37:19 +08:00
|
|
|
const DefinedAtom *atom) {
|
|
|
|
const DefinedAtom *start = atom;
|
|
|
|
llvm::dbgs() << "There's a cycle in a follow-on chain!\n";
|
|
|
|
do {
|
|
|
|
llvm::dbgs() << " " << atomToDebugString(atom) << "\n";
|
|
|
|
for (const Reference *ref : *atom) {
|
2014-02-25 05:14:37 +08:00
|
|
|
StringRef kindValStr;
|
|
|
|
if (!registry.referenceKindToString(ref->kindNamespace(), ref->kindArch(),
|
|
|
|
ref->kindValue(), kindValStr)) {
|
|
|
|
kindValStr = "<unknown>";
|
|
|
|
}
|
|
|
|
llvm::dbgs() << " " << kindValStr
|
|
|
|
<< ": " << atomToDebugString(ref->target()) << "\n";
|
2013-12-09 08:37:19 +08:00
|
|
|
}
|
|
|
|
atom = followOnNexts[atom];
|
|
|
|
} while (atom != start);
|
|
|
|
llvm::report_fatal_error("Cycle detected");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Exit if there's a cycle in a followon chain reachable from the
|
|
|
|
/// given root atom. Uses the tortoise and hare algorithm to detect a
|
|
|
|
/// cycle.
|
2014-02-25 05:14:37 +08:00
|
|
|
static void checkNoCycleInFollowonChain(const Registry ®istry,
|
|
|
|
AtomToAtomT &followOnNexts,
|
2013-12-09 08:37:19 +08:00
|
|
|
const DefinedAtom *root) {
|
|
|
|
const DefinedAtom *tortoise = root;
|
|
|
|
const DefinedAtom *hare = followOnNexts[root];
|
|
|
|
while (true) {
|
|
|
|
if (!tortoise || !hare)
|
|
|
|
return;
|
|
|
|
if (tortoise == hare)
|
2014-02-25 05:14:37 +08:00
|
|
|
showCycleDetectedError(registry, followOnNexts, tortoise);
|
2013-12-09 08:37:19 +08:00
|
|
|
tortoise = followOnNexts[tortoise];
|
|
|
|
hare = followOnNexts[followOnNexts[hare]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void checkReachabilityFromRoot(AtomToAtomT &followOnRoots,
|
|
|
|
const DefinedAtom *atom) {
|
|
|
|
if (!atom) return;
|
|
|
|
auto i = followOnRoots.find(atom);
|
|
|
|
if (i == followOnRoots.end()) {
|
|
|
|
Twine msg(Twine("Atom <") + atomToDebugString(atom)
|
|
|
|
+ "> has no follow-on root!");
|
|
|
|
llvm_unreachable(msg.str().c_str());
|
|
|
|
}
|
|
|
|
const DefinedAtom *ap = i->second;
|
|
|
|
while (true) {
|
|
|
|
const DefinedAtom *next = followOnRoots[ap];
|
|
|
|
if (!next) {
|
|
|
|
Twine msg(Twine("Atom <" + atomToDebugString(atom)
|
|
|
|
+ "> is not reachable from its root!"));
|
|
|
|
llvm_unreachable(msg.str().c_str());
|
|
|
|
}
|
|
|
|
if (next == ap)
|
|
|
|
return;
|
|
|
|
ap = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printDefinedAtoms(const MutableFile::DefinedAtomRange &atomRange) {
|
|
|
|
for (const DefinedAtom *atom : atomRange) {
|
|
|
|
llvm::dbgs() << " file=" << atom->file().path()
|
|
|
|
<< ", name=" << atom->name()
|
|
|
|
<< ", size=" << atom->size()
|
|
|
|
<< ", type=" << atom->contentType()
|
|
|
|
<< ", ordinal=" << atom->ordinal()
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Verify that the followon chain is sane. Should not be called in
|
|
|
|
/// release binary.
|
|
|
|
void LayoutPass::checkFollowonChain(MutableFile::DefinedAtomRange &range) {
|
|
|
|
ScopedTask task(getDefaultDomain(), "LayoutPass::checkFollowonChain");
|
|
|
|
|
|
|
|
// Verify that there's no cycle in follow-on chain.
|
|
|
|
std::set<const DefinedAtom *> roots;
|
|
|
|
for (const auto &ai : _followOnRoots)
|
|
|
|
roots.insert(ai.second);
|
|
|
|
for (const DefinedAtom *root : roots)
|
2014-02-25 05:14:37 +08:00
|
|
|
checkNoCycleInFollowonChain(_registry, _followOnNexts, root);
|
2013-12-09 08:37:19 +08:00
|
|
|
|
|
|
|
// Verify that all the atoms in followOnNexts have references to
|
|
|
|
// their roots.
|
|
|
|
for (const auto &ai : _followOnNexts) {
|
|
|
|
checkReachabilityFromRoot(_followOnRoots, ai.first);
|
|
|
|
checkReachabilityFromRoot(_followOnRoots, ai.second);
|
|
|
|
}
|
|
|
|
}
|
2013-12-08 11:37:58 +08:00
|
|
|
#endif // #ifndef NDEBUG
|
2013-12-08 11:12:08 +08:00
|
|
|
|
2013-02-08 04:16:12 +08:00
|
|
|
/// The function compares atoms by sorting atoms in the following order
|
2013-09-12 23:59:34 +08:00
|
|
|
/// a) Sorts atoms by Section position preference
|
Do not use layout-before to layout atoms.
Currently we use both layout-after and layout-before edges to specify atom
orders in the resulting executable. We have a complex piece of code in
LayoutPass.cpp to deal with both types of layout specifiers.
(In the following description, I denote "Atom A having a layout-after edge
to B" as "A -> B", and A's layout-before to B as "A => B".)
However, that complexity is not really needed for this reason: If there
are atoms such that A => B, B -> A is always satisifed, so using only layout-
after relationships will yield the same result as the current code.
Actually we have a piece of complex code that verifies that, for each A -> B,
B => [ X => Y => ... => Z => ] A is satsified, where X, Y, ... Z are all
zero-size atoms. We can get rid of the code from our codebase because layout-
before is basically redundant.
I think we can simplify the code for layout-after even more than this, but
I want to just remove this pass for now for simplicity.
Layout-before edges are still there for dead-stripping, so this change won't
break it. We will remove layout-before in a followup patch once we fix the
dead-stripping pass.
Differential Revision: http://llvm-reviews.chandlerc.com/D3164
llvm-svn: 204966
2014-03-28 06:08:26 +08:00
|
|
|
/// b) Sorts atoms by their ordinal overrides (layout-after/ingroup)
|
2013-09-12 23:59:34 +08:00
|
|
|
/// c) Sorts atoms by their permissions
|
|
|
|
/// d) Sorts atoms by their content
|
2013-02-08 04:16:12 +08:00
|
|
|
/// e) Sorts atoms on how they appear using File Ordinality
|
|
|
|
/// f) Sorts atoms on how they appear within the File
|
2013-12-08 11:24:09 +08:00
|
|
|
static bool compareAtomsSub(const LayoutPass::SortKey &lc,
|
|
|
|
const LayoutPass::SortKey &rc,
|
|
|
|
std::string &reason) {
|
2013-12-08 11:12:08 +08:00
|
|
|
const DefinedAtom *left = lc._atom;
|
|
|
|
const DefinedAtom *right = rc._atom;
|
2013-10-18 10:56:31 +08:00
|
|
|
if (left == right) {
|
|
|
|
reason = "same";
|
2013-02-08 04:16:12 +08:00
|
|
|
return false;
|
2013-10-18 10:56:31 +08:00
|
|
|
}
|
2013-02-08 04:16:12 +08:00
|
|
|
|
2013-05-23 01:41:04 +08:00
|
|
|
// Sort by section position preference.
|
|
|
|
DefinedAtom::SectionPosition leftPos = left->sectionPosition();
|
|
|
|
DefinedAtom::SectionPosition rightPos = right->sectionPosition();
|
|
|
|
|
|
|
|
bool leftSpecialPos = (leftPos != DefinedAtom::sectionPositionAny);
|
|
|
|
bool rightSpecialPos = (rightPos != DefinedAtom::sectionPositionAny);
|
|
|
|
if (leftSpecialPos || rightSpecialPos) {
|
2013-10-18 10:56:31 +08:00
|
|
|
if (leftPos != rightPos) {
|
|
|
|
DEBUG(reason = formatReason("sectionPos", (int)leftPos, (int)rightPos));
|
2013-05-23 01:41:04 +08:00
|
|
|
return leftPos < rightPos;
|
2013-10-18 10:56:31 +08:00
|
|
|
}
|
2013-05-23 01:41:04 +08:00
|
|
|
}
|
|
|
|
|
Fix bug that CompareAtoms::compare is not transitive.
This patch fixes a bug in r190608. The results of a comparison function
passed to std::sort must be transitive, which is, if a < b and b < c, and if
a != b, a < c must be also true. CompareAtoms::compare did not actually
guarantee the transitivity. As a result the sort results were sometimes just
wrong.
Consider there are three atoms, X, Y, and Z, whose file ordinals are 1, 2, 3,
respectively. Z has a property "layout-after X". In this case, all the
following conditionals become true:
X < Y because X's ordinal is less than Y's
Y < Z because Y's ordinal is less than Z's
Z < X because of the layout-after relationship
This is not of course transitive. The reason why this happened is because
we used follow-on relationships for comparison if two atoms falls in the same
follow-on chain, but we used each atom's properties if they did not. This patch
fixes the issue by using follow-on root atoms for comparison to get consistent
results.
Differential Revision: http://llvm-reviews.chandlerc.com/D1980
llvm-svn: 193029
2013-10-19 11:18:18 +08:00
|
|
|
// Find the root of the chain if it is a part of a follow-on chain.
|
2013-12-08 11:12:08 +08:00
|
|
|
const DefinedAtom *leftRoot = lc._root;
|
|
|
|
const DefinedAtom *rightRoot = rc._root;
|
2013-09-12 23:43:09 +08:00
|
|
|
|
|
|
|
// Sort atoms by their ordinal overrides only if they fall in the same
|
|
|
|
// chain.
|
2013-12-08 11:12:08 +08:00
|
|
|
if (leftRoot == rightRoot) {
|
|
|
|
DEBUG(reason = formatReason("override", lc._override, rc._override));
|
|
|
|
return lc._override < rc._override;
|
2013-05-11 00:44:02 +08:00
|
|
|
}
|
|
|
|
|
2013-02-08 04:16:12 +08:00
|
|
|
// Sort same permissions together.
|
Fix bug that CompareAtoms::compare is not transitive.
This patch fixes a bug in r190608. The results of a comparison function
passed to std::sort must be transitive, which is, if a < b and b < c, and if
a != b, a < c must be also true. CompareAtoms::compare did not actually
guarantee the transitivity. As a result the sort results were sometimes just
wrong.
Consider there are three atoms, X, Y, and Z, whose file ordinals are 1, 2, 3,
respectively. Z has a property "layout-after X". In this case, all the
following conditionals become true:
X < Y because X's ordinal is less than Y's
Y < Z because Y's ordinal is less than Z's
Z < X because of the layout-after relationship
This is not of course transitive. The reason why this happened is because
we used follow-on relationships for comparison if two atoms falls in the same
follow-on chain, but we used each atom's properties if they did not. This patch
fixes the issue by using follow-on root atoms for comparison to get consistent
results.
Differential Revision: http://llvm-reviews.chandlerc.com/D1980
llvm-svn: 193029
2013-10-19 11:18:18 +08:00
|
|
|
DefinedAtom::ContentPermissions leftPerms = leftRoot->permissions();
|
|
|
|
DefinedAtom::ContentPermissions rightPerms = rightRoot->permissions();
|
2013-03-13 12:05:38 +08:00
|
|
|
|
2013-10-18 10:56:31 +08:00
|
|
|
if (leftPerms != rightPerms) {
|
|
|
|
DEBUG(reason =
|
|
|
|
formatReason("contentPerms", (int)leftPerms, (int)rightPerms));
|
2013-02-08 04:16:12 +08:00
|
|
|
return leftPerms < rightPerms;
|
2013-10-18 10:56:31 +08:00
|
|
|
}
|
2013-02-08 04:16:12 +08:00
|
|
|
|
|
|
|
// Sort same content types together.
|
Fix bug that CompareAtoms::compare is not transitive.
This patch fixes a bug in r190608. The results of a comparison function
passed to std::sort must be transitive, which is, if a < b and b < c, and if
a != b, a < c must be also true. CompareAtoms::compare did not actually
guarantee the transitivity. As a result the sort results were sometimes just
wrong.
Consider there are three atoms, X, Y, and Z, whose file ordinals are 1, 2, 3,
respectively. Z has a property "layout-after X". In this case, all the
following conditionals become true:
X < Y because X's ordinal is less than Y's
Y < Z because Y's ordinal is less than Z's
Z < X because of the layout-after relationship
This is not of course transitive. The reason why this happened is because
we used follow-on relationships for comparison if two atoms falls in the same
follow-on chain, but we used each atom's properties if they did not. This patch
fixes the issue by using follow-on root atoms for comparison to get consistent
results.
Differential Revision: http://llvm-reviews.chandlerc.com/D1980
llvm-svn: 193029
2013-10-19 11:18:18 +08:00
|
|
|
DefinedAtom::ContentType leftType = leftRoot->contentType();
|
|
|
|
DefinedAtom::ContentType rightType = rightRoot->contentType();
|
2013-03-13 12:05:38 +08:00
|
|
|
|
2013-10-18 10:56:31 +08:00
|
|
|
if (leftType != rightType) {
|
|
|
|
DEBUG(reason = formatReason("contentType", (int)leftType, (int)rightType));
|
2013-02-08 04:16:12 +08:00
|
|
|
return leftType < rightType;
|
2013-10-18 10:56:31 +08:00
|
|
|
}
|
2013-02-08 04:16:12 +08:00
|
|
|
|
|
|
|
// Sort by .o order.
|
Fix bug that CompareAtoms::compare is not transitive.
This patch fixes a bug in r190608. The results of a comparison function
passed to std::sort must be transitive, which is, if a < b and b < c, and if
a != b, a < c must be also true. CompareAtoms::compare did not actually
guarantee the transitivity. As a result the sort results were sometimes just
wrong.
Consider there are three atoms, X, Y, and Z, whose file ordinals are 1, 2, 3,
respectively. Z has a property "layout-after X". In this case, all the
following conditionals become true:
X < Y because X's ordinal is less than Y's
Y < Z because Y's ordinal is less than Z's
Z < X because of the layout-after relationship
This is not of course transitive. The reason why this happened is because
we used follow-on relationships for comparison if two atoms falls in the same
follow-on chain, but we used each atom's properties if they did not. This patch
fixes the issue by using follow-on root atoms for comparison to get consistent
results.
Differential Revision: http://llvm-reviews.chandlerc.com/D1980
llvm-svn: 193029
2013-10-19 11:18:18 +08:00
|
|
|
const File *leftFile = &leftRoot->file();
|
|
|
|
const File *rightFile = &rightRoot->file();
|
2013-03-13 12:05:38 +08:00
|
|
|
|
2013-10-18 10:56:31 +08:00
|
|
|
if (leftFile != rightFile) {
|
|
|
|
DEBUG(reason = formatReason(".o order", (int)leftFile->ordinal(),
|
|
|
|
(int)rightFile->ordinal()));
|
2013-02-08 04:16:12 +08:00
|
|
|
return leftFile->ordinal() < rightFile->ordinal();
|
2013-10-18 10:56:31 +08:00
|
|
|
}
|
2013-02-08 04:16:12 +08:00
|
|
|
|
|
|
|
// Sort by atom order with .o file.
|
Fix bug that CompareAtoms::compare is not transitive.
This patch fixes a bug in r190608. The results of a comparison function
passed to std::sort must be transitive, which is, if a < b and b < c, and if
a != b, a < c must be also true. CompareAtoms::compare did not actually
guarantee the transitivity. As a result the sort results were sometimes just
wrong.
Consider there are three atoms, X, Y, and Z, whose file ordinals are 1, 2, 3,
respectively. Z has a property "layout-after X". In this case, all the
following conditionals become true:
X < Y because X's ordinal is less than Y's
Y < Z because Y's ordinal is less than Z's
Z < X because of the layout-after relationship
This is not of course transitive. The reason why this happened is because
we used follow-on relationships for comparison if two atoms falls in the same
follow-on chain, but we used each atom's properties if they did not. This patch
fixes the issue by using follow-on root atoms for comparison to get consistent
results.
Differential Revision: http://llvm-reviews.chandlerc.com/D1980
llvm-svn: 193029
2013-10-19 11:18:18 +08:00
|
|
|
uint64_t leftOrdinal = leftRoot->ordinal();
|
|
|
|
uint64_t rightOrdinal = rightRoot->ordinal();
|
2013-03-13 12:05:38 +08:00
|
|
|
|
2013-10-18 10:56:31 +08:00
|
|
|
if (leftOrdinal != rightOrdinal) {
|
Fix bug that CompareAtoms::compare is not transitive.
This patch fixes a bug in r190608. The results of a comparison function
passed to std::sort must be transitive, which is, if a < b and b < c, and if
a != b, a < c must be also true. CompareAtoms::compare did not actually
guarantee the transitivity. As a result the sort results were sometimes just
wrong.
Consider there are three atoms, X, Y, and Z, whose file ordinals are 1, 2, 3,
respectively. Z has a property "layout-after X". In this case, all the
following conditionals become true:
X < Y because X's ordinal is less than Y's
Y < Z because Y's ordinal is less than Z's
Z < X because of the layout-after relationship
This is not of course transitive. The reason why this happened is because
we used follow-on relationships for comparison if two atoms falls in the same
follow-on chain, but we used each atom's properties if they did not. This patch
fixes the issue by using follow-on root atoms for comparison to get consistent
results.
Differential Revision: http://llvm-reviews.chandlerc.com/D1980
llvm-svn: 193029
2013-10-19 11:18:18 +08:00
|
|
|
DEBUG(reason = formatReason("ordinal", (int)leftRoot->ordinal(),
|
|
|
|
(int)rightRoot->ordinal()));
|
2013-02-08 04:16:12 +08:00
|
|
|
return leftOrdinal < rightOrdinal;
|
2013-10-18 10:56:31 +08:00
|
|
|
}
|
2013-02-08 04:16:12 +08:00
|
|
|
|
2013-11-27 09:33:42 +08:00
|
|
|
llvm::errs() << "Unordered: <" << left->name() << "> <"
|
|
|
|
<< right->name() << ">\n";
|
2013-10-11 09:50:04 +08:00
|
|
|
llvm_unreachable("Atoms with Same Ordinal!");
|
2013-02-08 04:16:12 +08:00
|
|
|
}
|
|
|
|
|
2013-12-08 11:24:09 +08:00
|
|
|
static bool compareAtoms(const LayoutPass::SortKey &lc,
|
|
|
|
const LayoutPass::SortKey &rc) {
|
2013-10-18 10:56:31 +08:00
|
|
|
std::string reason;
|
2013-12-08 11:12:08 +08:00
|
|
|
bool result = compareAtomsSub(lc, rc, reason);
|
2013-10-18 10:56:31 +08:00
|
|
|
DEBUG({
|
2013-10-18 11:18:52 +08:00
|
|
|
StringRef comp = result ? "<" : ">=";
|
2013-12-08 11:12:08 +08:00
|
|
|
llvm::dbgs() << "Layout: '" << lc._atom->name() << "' " << comp << " '"
|
|
|
|
<< rc._atom->name() << "' (" << reason << ")\n";
|
2013-10-18 10:56:31 +08:00
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-02-25 05:14:37 +08:00
|
|
|
LayoutPass::LayoutPass(const Registry ®istry) : _registry(registry) {}
|
|
|
|
|
2013-05-14 08:41:52 +08:00
|
|
|
// Returns the atom immediately followed by the given atom in the followon
|
|
|
|
// chain.
|
|
|
|
const DefinedAtom *LayoutPass::findAtomFollowedBy(
|
|
|
|
const DefinedAtom *targetAtom) {
|
|
|
|
// Start from the beginning of the chain and follow the chain until
|
|
|
|
// we find the targetChain.
|
|
|
|
const DefinedAtom *atom = _followOnRoots[targetAtom];
|
|
|
|
while (true) {
|
|
|
|
const DefinedAtom *prevAtom = atom;
|
|
|
|
AtomToAtomT::iterator targetFollowOnAtomsIter = _followOnNexts.find(atom);
|
|
|
|
// The target atom must be in the chain of its root.
|
|
|
|
assert(targetFollowOnAtomsIter != _followOnNexts.end());
|
|
|
|
atom = targetFollowOnAtomsIter->second;
|
|
|
|
if (atom == targetAtom)
|
|
|
|
return prevAtom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if all the atoms followed by the given target atom are of size zero.
|
|
|
|
// When this method is called, an atom being added is not of size zero and
|
|
|
|
// will be added to the head of the followon chain. All the atoms between the
|
|
|
|
// atom and the targetAtom (specified by layout-after) need to be of size zero
|
|
|
|
// in this case. Otherwise the desired layout is impossible.
|
|
|
|
bool LayoutPass::checkAllPrevAtomsZeroSize(const DefinedAtom *targetAtom) {
|
|
|
|
const DefinedAtom *atom = _followOnRoots[targetAtom];
|
|
|
|
while (true) {
|
|
|
|
if (atom == targetAtom)
|
|
|
|
return true;
|
2013-05-15 00:53:59 +08:00
|
|
|
if (atom->size() != 0)
|
2013-05-14 08:41:52 +08:00
|
|
|
// TODO: print warning that an impossible layout is being desired by the
|
|
|
|
// user.
|
|
|
|
return false;
|
2013-05-14 09:51:56 +08:00
|
|
|
AtomToAtomT::iterator targetFollowOnAtomsIter = _followOnNexts.find(atom);
|
|
|
|
// The target atom must be in the chain of its root.
|
|
|
|
assert(targetFollowOnAtomsIter != _followOnNexts.end());
|
|
|
|
atom = targetFollowOnAtomsIter->second;
|
2013-05-14 08:41:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the root of all atoms in targetAtom's chain to the given root.
|
|
|
|
void LayoutPass::setChainRoot(const DefinedAtom *targetAtom,
|
|
|
|
const DefinedAtom *root) {
|
|
|
|
// Walk through the followon chain and override each node's root.
|
|
|
|
while (true) {
|
|
|
|
_followOnRoots[targetAtom] = root;
|
|
|
|
AtomToAtomT::iterator targetFollowOnAtomsIter =
|
|
|
|
_followOnNexts.find(targetAtom);
|
|
|
|
if (targetFollowOnAtomsIter == _followOnNexts.end())
|
|
|
|
return;
|
|
|
|
targetAtom = targetFollowOnAtomsIter->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 04:16:12 +08:00
|
|
|
/// This pass builds the followon tables described by two DenseMaps
|
|
|
|
/// followOnRoots and followonNexts.
|
|
|
|
/// The followOnRoots map contains a mapping of a DefinedAtom to its root
|
|
|
|
/// The followOnNexts map contains a mapping of what DefinedAtom follows the
|
|
|
|
/// current Atom
|
|
|
|
/// The algorithm follows a very simple approach
|
|
|
|
/// a) If the atom is first seen, then make that as the root atom
|
2013-03-15 00:09:49 +08:00
|
|
|
/// b) The targetAtom which this Atom contains, has the root thats set to the
|
2013-02-08 04:16:12 +08:00
|
|
|
/// root of the current atom
|
|
|
|
/// c) If the targetAtom is part of a different tree and the root of the
|
|
|
|
/// targetAtom is itself, Chain all the atoms that are contained in the tree
|
|
|
|
/// to the current Tree
|
|
|
|
/// d) If the targetAtom is part of a different chain and the root of the
|
|
|
|
/// targetAtom until the targetAtom has all atoms of size 0, then chain the
|
|
|
|
/// targetAtoms and its tree to the current chain
|
|
|
|
void LayoutPass::buildFollowOnTable(MutableFile::DefinedAtomRange &range) {
|
2013-04-06 08:56:40 +08:00
|
|
|
ScopedTask task(getDefaultDomain(), "LayoutPass::buildFollowOnTable");
|
2013-05-14 08:41:52 +08:00
|
|
|
// Set the initial size of the followon and the followonNext hash to the
|
|
|
|
// number of atoms that we have.
|
2013-04-29 11:27:57 +08:00
|
|
|
_followOnRoots.resize(range.size());
|
|
|
|
_followOnNexts.resize(range.size());
|
2013-05-15 00:53:59 +08:00
|
|
|
for (const DefinedAtom *ai : range) {
|
2013-02-08 04:16:12 +08:00
|
|
|
for (const Reference *r : *ai) {
|
2014-03-22 05:46:49 +08:00
|
|
|
if (r->kindNamespace() != lld::Reference::KindNamespace::all ||
|
|
|
|
r->kindValue() != lld::Reference::kindLayoutAfter)
|
2013-05-14 08:41:52 +08:00
|
|
|
continue;
|
2013-11-05 09:37:40 +08:00
|
|
|
const DefinedAtom *targetAtom = dyn_cast<DefinedAtom>(r->target());
|
2013-05-14 08:41:52 +08:00
|
|
|
_followOnNexts[ai] = targetAtom;
|
|
|
|
|
2013-12-02 09:28:14 +08:00
|
|
|
// If we find a followon for the first time, let's make that atom as the
|
2013-05-14 08:41:52 +08:00
|
|
|
// root atom.
|
|
|
|
if (_followOnRoots.count(ai) == 0)
|
|
|
|
_followOnRoots[ai] = ai;
|
|
|
|
|
|
|
|
auto iter = _followOnRoots.find(targetAtom);
|
|
|
|
if (iter == _followOnRoots.end()) {
|
2013-12-02 09:28:14 +08:00
|
|
|
// If the targetAtom is not a root of any chain, let's make the root of
|
2013-05-14 08:41:52 +08:00
|
|
|
// the targetAtom to the root of the current chain.
|
2014-07-26 03:46:31 +08:00
|
|
|
|
|
|
|
// The expression m[i] = m[j] where m is a DenseMap and i != j is not
|
|
|
|
// safe. m[j] returns a reference, which would be invalidated when a
|
|
|
|
// rehashing occurs. If rehashing occurs to make room for m[i], m[j]
|
|
|
|
// becomes invalid, and that invalid reference would be used as the RHS
|
|
|
|
// value of the expression.
|
|
|
|
// Copy the value to workaround.
|
|
|
|
const DefinedAtom *tmp = _followOnRoots[ai];
|
|
|
|
_followOnRoots[targetAtom] = tmp;
|
2014-03-22 05:46:49 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (iter->second == targetAtom) {
|
2013-05-14 08:41:52 +08:00
|
|
|
// If the targetAtom is the root of a chain, the chain becomes part of
|
|
|
|
// the current chain. Rewrite the subchain's root to the current
|
|
|
|
// chain's root.
|
|
|
|
setChainRoot(targetAtom, _followOnRoots[ai]);
|
2014-03-22 05:46:49 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// The targetAtom is already a part of a chain. If the current atom is
|
|
|
|
// of size zero, we can insert it in the middle of the chain just
|
|
|
|
// before the target atom, while not breaking other atom's followon
|
|
|
|
// relationships. If it's not, we can only insert the current atom at
|
|
|
|
// the beginning of the chain. All the atoms followed by the target
|
|
|
|
// atom must be of size zero in that case to satisfy the followon
|
|
|
|
// relationships.
|
|
|
|
size_t currentAtomSize = ai->size();
|
|
|
|
if (currentAtomSize == 0) {
|
|
|
|
const DefinedAtom *targetPrevAtom = findAtomFollowedBy(targetAtom);
|
|
|
|
_followOnNexts[targetPrevAtom] = ai;
|
2014-07-26 03:46:31 +08:00
|
|
|
const DefinedAtom *tmp = _followOnRoots[targetPrevAtom];
|
|
|
|
_followOnRoots[ai] = tmp;
|
2014-03-22 05:46:49 +08:00
|
|
|
continue;
|
2013-05-14 08:41:52 +08:00
|
|
|
}
|
2014-03-22 05:46:49 +08:00
|
|
|
if (!checkAllPrevAtomsZeroSize(targetAtom))
|
|
|
|
break;
|
|
|
|
_followOnNexts[ai] = _followOnRoots[targetAtom];
|
|
|
|
setChainRoot(_followOnRoots[targetAtom], _followOnRoots[ai]);
|
2013-05-14 08:41:52 +08:00
|
|
|
}
|
|
|
|
}
|
2013-02-08 04:16:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This pass builds the followon tables using InGroup relationships
|
|
|
|
/// The algorithm follows a very simple approach
|
|
|
|
/// a) If the rootAtom is not part of any root, create a new root with the
|
|
|
|
/// as the head
|
|
|
|
/// b) If the current Atom root is not found, then make the current atoms root
|
|
|
|
/// point to the rootAtom
|
|
|
|
/// c) If the root of the current Atom is itself a root of some other tree
|
|
|
|
/// make all the atoms in the chain point to the ingroup reference
|
|
|
|
/// d) Check to see if the current atom is part of the chain from the rootAtom
|
|
|
|
/// if not add the atom to the chain, so that the current atom is part of the
|
|
|
|
/// the chain where the rootAtom is in
|
|
|
|
void LayoutPass::buildInGroupTable(MutableFile::DefinedAtomRange &range) {
|
2013-04-06 08:56:40 +08:00
|
|
|
ScopedTask task(getDefaultDomain(), "LayoutPass::buildInGroupTable");
|
2013-03-15 00:09:49 +08:00
|
|
|
// This table would convert precededby references to follow on
|
|
|
|
// references so that we have only one table
|
2013-05-15 00:53:59 +08:00
|
|
|
for (const DefinedAtom *ai : range) {
|
2013-02-08 04:16:12 +08:00
|
|
|
for (const Reference *r : *ai) {
|
2014-03-22 05:46:49 +08:00
|
|
|
if (r->kindNamespace() != lld::Reference::KindNamespace::all ||
|
|
|
|
r->kindValue() != lld::Reference::kindInGroup)
|
[lld] Introduce registry and Reference kind tuple
The main changes are in:
include/lld/Core/Reference.h
include/lld/ReaderWriter/Reader.h
Everything else is details to support the main change.
1) Registration based Readers
Previously, lld had a tangled interdependency with all the Readers. It would
have been impossible to make a streamlined linker (say for a JIT) which
just supported one file format and one architecture (no yaml, no archives, etc).
The old model also required a LinkingContext to read an object file, which
would have made .o inspection tools awkward.
The new model is that there is a global Registry object. You programmatically
register the Readers you want with the registry object. Whenever you need to
read/parse a file, you ask the registry to do it, and the registry tries each
registered reader.
For ease of use with the existing lld code base, there is one Registry
object inside the LinkingContext object.
2) Changing kind value to be a tuple
Beside Readers, the registry also keeps track of the mapping for Reference
Kind values to and from strings. Along with that, this patch also fixes
an ambiguity with the previous Reference::Kind values. The problem was that
we wanted to reuse existing relocation type values as Reference::Kind values.
But then how can the YAML write know how to convert a value to a string? The
fix is to change the 32-bit Reference::Kind into a tuple with an 8-bit namespace
(e.g. ELF, COFFF, etc), an 8-bit architecture (e.g. x86_64, PowerPC, etc), and
a 16-bit value. This tuple system allows conversion to and from strings with
no ambiguities.
llvm-svn: 197727
2013-12-20 05:58:00 +08:00
|
|
|
continue;
|
2014-03-22 05:46:49 +08:00
|
|
|
const DefinedAtom *rootAtom = dyn_cast<DefinedAtom>(r->target());
|
|
|
|
// If the root atom is not part of any root
|
|
|
|
// create a new root
|
|
|
|
if (_followOnRoots.count(rootAtom) == 0) {
|
|
|
|
_followOnRoots[rootAtom] = rootAtom;
|
|
|
|
}
|
|
|
|
// If the current Atom has not been seen yet and there is no root
|
|
|
|
// that has been set, set the root of the atom to the targetAtom
|
|
|
|
// as the targetAtom points to the ingroup root
|
|
|
|
auto iter = _followOnRoots.find(ai);
|
|
|
|
if (iter == _followOnRoots.end()) {
|
|
|
|
_followOnRoots[ai] = rootAtom;
|
|
|
|
} else if (iter->second == ai) {
|
|
|
|
if (iter->second != rootAtom)
|
|
|
|
setChainRoot(iter->second, rootAtom);
|
|
|
|
} else {
|
|
|
|
// TODO : Flag an error that the root of the tree
|
|
|
|
// is different, Here is an example
|
|
|
|
// Say there are atoms
|
|
|
|
// chain 1 : a->b->c
|
|
|
|
// chain 2 : d->e->f
|
|
|
|
// and e,f have their ingroup reference as a
|
|
|
|
// this could happen only if the root of e,f that is d
|
|
|
|
// has root as 'a'
|
|
|
|
continue;
|
|
|
|
}
|
2013-02-08 04:16:12 +08:00
|
|
|
|
2014-03-22 05:46:49 +08:00
|
|
|
// Check if the current atom is part of the chain
|
|
|
|
bool isAtomInChain = false;
|
|
|
|
const DefinedAtom *lastAtom = rootAtom;
|
|
|
|
for (;;) {
|
|
|
|
AtomToAtomT::iterator followOnAtomsIter =
|
|
|
|
_followOnNexts.find(lastAtom);
|
|
|
|
if (followOnAtomsIter != _followOnNexts.end()) {
|
|
|
|
lastAtom = followOnAtomsIter->second;
|
|
|
|
if (lastAtom != ai)
|
|
|
|
continue;
|
|
|
|
isAtomInChain = true;
|
2013-12-10 14:19:09 +08:00
|
|
|
}
|
2014-03-22 05:46:49 +08:00
|
|
|
break;
|
2013-02-08 04:16:12 +08:00
|
|
|
}
|
2014-03-22 05:46:49 +08:00
|
|
|
|
|
|
|
if (!isAtomInChain)
|
|
|
|
_followOnNexts[lastAtom] = ai;
|
2013-02-08 04:16:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Build an ordinal override map by traversing the followon chain, and
|
2013-03-15 00:09:49 +08:00
|
|
|
/// assigning ordinals to each atom, if the atoms have their ordinals
|
|
|
|
/// already assigned skip the atom and move to the next. This is the
|
2013-02-08 04:16:12 +08:00
|
|
|
/// main map thats used to sort the atoms while comparing two atoms together
|
|
|
|
void LayoutPass::buildOrdinalOverrideMap(MutableFile::DefinedAtomRange &range) {
|
2013-04-06 08:56:40 +08:00
|
|
|
ScopedTask task(getDefaultDomain(), "LayoutPass::buildOrdinalOverrideMap");
|
2013-02-08 04:16:12 +08:00
|
|
|
uint64_t index = 0;
|
2013-05-15 00:53:59 +08:00
|
|
|
for (const DefinedAtom *ai : range) {
|
2013-02-08 04:16:12 +08:00
|
|
|
const DefinedAtom *atom = ai;
|
2013-03-12 08:10:00 +08:00
|
|
|
if (_ordinalOverrideMap.find(atom) != _ordinalOverrideMap.end())
|
|
|
|
continue;
|
2013-02-08 04:16:12 +08:00
|
|
|
AtomToAtomT::iterator start = _followOnRoots.find(atom);
|
2014-03-22 05:46:49 +08:00
|
|
|
if (start == _followOnRoots.end())
|
|
|
|
continue;
|
|
|
|
for (const DefinedAtom *nextAtom = start->second; nextAtom != NULL;
|
|
|
|
nextAtom = _followOnNexts[nextAtom]) {
|
|
|
|
AtomToOrdinalT::iterator pos = _ordinalOverrideMap.find(nextAtom);
|
|
|
|
if (pos == _ordinalOverrideMap.end())
|
|
|
|
_ordinalOverrideMap[nextAtom] = index++;
|
2013-02-08 04:16:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-08 11:12:08 +08:00
|
|
|
std::vector<LayoutPass::SortKey>
|
|
|
|
LayoutPass::decorate(MutableFile::DefinedAtomRange &atomRange) const {
|
|
|
|
std::vector<SortKey> ret;
|
|
|
|
for (const DefinedAtom *atom : atomRange) {
|
|
|
|
auto ri = _followOnRoots.find(atom);
|
|
|
|
auto oi = _ordinalOverrideMap.find(atom);
|
|
|
|
const DefinedAtom *root = (ri == _followOnRoots.end()) ? atom : ri->second;
|
|
|
|
uint64_t override = (oi == _ordinalOverrideMap.end()) ? 0 : oi->second;
|
|
|
|
ret.push_back(SortKey(atom, root, override));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayoutPass::undecorate(MutableFile::DefinedAtomRange &atomRange,
|
|
|
|
std::vector<SortKey> &keys) const {
|
|
|
|
size_t i = 0;
|
|
|
|
for (SortKey &k : keys)
|
|
|
|
atomRange[i++] = k._atom;
|
|
|
|
}
|
|
|
|
|
2013-03-15 00:09:49 +08:00
|
|
|
/// Perform the actual pass
|
2013-10-29 13:12:14 +08:00
|
|
|
void LayoutPass::perform(std::unique_ptr<MutableFile> &mergedFile) {
|
2013-12-08 11:12:08 +08:00
|
|
|
// sort the atoms
|
2013-05-29 02:55:39 +08:00
|
|
|
ScopedTask task(getDefaultDomain(), "LayoutPass");
|
2013-10-29 13:12:14 +08:00
|
|
|
MutableFile::DefinedAtomRange atomRange = mergedFile->definedAtoms();
|
2013-02-08 04:16:12 +08:00
|
|
|
|
|
|
|
// Build follow on tables
|
|
|
|
buildFollowOnTable(atomRange);
|
|
|
|
|
|
|
|
// Build Ingroup reference table
|
|
|
|
buildInGroupTable(atomRange);
|
|
|
|
|
2013-06-08 04:18:39 +08:00
|
|
|
// Check the structure of followon graph if running in debug mode.
|
|
|
|
DEBUG(checkFollowonChain(atomRange));
|
|
|
|
|
2013-02-08 04:16:12 +08:00
|
|
|
// Build override maps
|
|
|
|
buildOrdinalOverrideMap(atomRange);
|
|
|
|
|
2013-05-23 09:31:25 +08:00
|
|
|
DEBUG({
|
2013-04-05 04:32:18 +08:00
|
|
|
llvm::dbgs() << "unsorted atoms:\n";
|
2013-06-08 04:18:39 +08:00
|
|
|
printDefinedAtoms(atomRange);
|
2013-04-05 04:32:18 +08:00
|
|
|
});
|
2013-04-29 11:27:57 +08:00
|
|
|
|
2013-12-08 11:12:08 +08:00
|
|
|
std::vector<LayoutPass::SortKey> vec = decorate(atomRange);
|
|
|
|
std::sort(vec.begin(), vec.end(), compareAtoms);
|
|
|
|
DEBUG(checkTransitivity(vec));
|
|
|
|
undecorate(atomRange, vec);
|
Fix bug that CompareAtoms::compare is not transitive.
This patch fixes a bug in r190608. The results of a comparison function
passed to std::sort must be transitive, which is, if a < b and b < c, and if
a != b, a < c must be also true. CompareAtoms::compare did not actually
guarantee the transitivity. As a result the sort results were sometimes just
wrong.
Consider there are three atoms, X, Y, and Z, whose file ordinals are 1, 2, 3,
respectively. Z has a property "layout-after X". In this case, all the
following conditionals become true:
X < Y because X's ordinal is less than Y's
Y < Z because Y's ordinal is less than Z's
Z < X because of the layout-after relationship
This is not of course transitive. The reason why this happened is because
we used follow-on relationships for comparison if two atoms falls in the same
follow-on chain, but we used each atom's properties if they did not. This patch
fixes the issue by using follow-on root atoms for comparison to get consistent
results.
Differential Revision: http://llvm-reviews.chandlerc.com/D1980
llvm-svn: 193029
2013-10-19 11:18:18 +08:00
|
|
|
|
2013-05-23 09:31:25 +08:00
|
|
|
DEBUG({
|
2013-04-05 04:32:18 +08:00
|
|
|
llvm::dbgs() << "sorted atoms:\n";
|
2013-06-08 04:18:39 +08:00
|
|
|
printDefinedAtoms(atomRange);
|
2013-04-05 04:32:18 +08:00
|
|
|
});
|
2013-02-08 04:16:12 +08:00
|
|
|
}
|