2013-02-08 04:16:12 +08:00
|
|
|
//===- Passes/LayoutPass.cpp - Layout atoms -------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-02-26 09:35:30 +08:00
|
|
|
#define DEBUG_TYPE "LayoutPass"
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/// The function compares atoms by sorting atoms in the following order
|
|
|
|
/// a) Sorts atoms with the same permissions
|
|
|
|
/// b) Sorts atoms with the same content Type
|
|
|
|
/// c) Sorts atoms by Section position preference
|
|
|
|
/// d) Sorts atoms by how they follow / precede each atom
|
|
|
|
/// e) Sorts atoms on how they appear using File Ordinality
|
|
|
|
/// f) Sorts atoms on how they appear within the File
|
|
|
|
bool LayoutPass::CompareAtoms::operator()(const DefinedAtom *left,
|
2013-05-29 03:03:29 +08:00
|
|
|
const DefinedAtom *right) const {
|
2013-02-26 09:35:30 +08:00
|
|
|
DEBUG(llvm::dbgs() << "Sorting " << left->name() << " " << right->name() << "\n");
|
2013-02-08 04:16:12 +08:00
|
|
|
if (left == right)
|
|
|
|
return false;
|
|
|
|
|
2013-05-23 01:41:04 +08:00
|
|
|
// Sort by section position preference.
|
|
|
|
DefinedAtom::SectionPosition leftPos = left->sectionPosition();
|
|
|
|
DefinedAtom::SectionPosition rightPos = right->sectionPosition();
|
|
|
|
|
|
|
|
DEBUG(llvm::dbgs() << "Sorting by sectionPos"
|
|
|
|
<< "(" << leftPos << "," << rightPos << ")\n");
|
|
|
|
|
|
|
|
bool leftSpecialPos = (leftPos != DefinedAtom::sectionPositionAny);
|
|
|
|
bool rightSpecialPos = (rightPos != DefinedAtom::sectionPositionAny);
|
|
|
|
if (leftSpecialPos || rightSpecialPos) {
|
|
|
|
if (leftPos != rightPos)
|
|
|
|
return leftPos < rightPos;
|
|
|
|
}
|
|
|
|
|
2013-05-11 00:44:02 +08:00
|
|
|
DEBUG(llvm::dbgs() << "Sorting by override\n");
|
|
|
|
|
|
|
|
AtomToOrdinalT::const_iterator lPos = _layout._ordinalOverrideMap.find(left);
|
|
|
|
AtomToOrdinalT::const_iterator rPos = _layout._ordinalOverrideMap.find(right);
|
|
|
|
AtomToOrdinalT::const_iterator end = _layout._ordinalOverrideMap.end();
|
|
|
|
if (lPos != end) {
|
|
|
|
if (rPos != end) {
|
|
|
|
// both left and right are overridden, so compare overridden ordinals
|
|
|
|
if (lPos->second != rPos->second)
|
|
|
|
return lPos->second < rPos->second;
|
|
|
|
} else {
|
|
|
|
// left is overridden and right is not, so left < right
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (rPos != end) {
|
|
|
|
// right is overridden and left is not, so right < left
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// neither are overridden,
|
|
|
|
// fall into default sorting below
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 04:16:12 +08:00
|
|
|
// Sort same permissions together.
|
|
|
|
DefinedAtom::ContentPermissions leftPerms = left->permissions();
|
|
|
|
DefinedAtom::ContentPermissions rightPerms = right->permissions();
|
2013-03-13 12:05:38 +08:00
|
|
|
|
|
|
|
DEBUG(llvm::dbgs() << "Sorting by contentPerms"
|
|
|
|
<< "(" << leftPerms << "," << rightPerms << ")\n");
|
|
|
|
|
2013-02-08 04:16:12 +08:00
|
|
|
if (leftPerms != rightPerms)
|
|
|
|
return leftPerms < rightPerms;
|
|
|
|
|
|
|
|
// Sort same content types together.
|
|
|
|
DefinedAtom::ContentType leftType = left->contentType();
|
|
|
|
DefinedAtom::ContentType rightType = right->contentType();
|
2013-03-13 12:05:38 +08:00
|
|
|
|
|
|
|
DEBUG(llvm::dbgs() << "Sorting by contentType"
|
|
|
|
<< "(" << leftType << "," << rightType << ")\n");
|
|
|
|
|
2013-02-08 04:16:12 +08:00
|
|
|
if (leftType != rightType)
|
|
|
|
return leftType < rightType;
|
|
|
|
|
|
|
|
// TO DO: Sort atoms in customs sections together.
|
|
|
|
|
|
|
|
// Sort by .o order.
|
|
|
|
const File *leftFile = &left->file();
|
|
|
|
const File *rightFile = &right->file();
|
2013-03-13 12:05:38 +08:00
|
|
|
|
|
|
|
DEBUG(llvm::dbgs()
|
|
|
|
<< "Sorting by .o order("
|
|
|
|
<< "(" << leftFile->ordinal() << "," << rightFile->ordinal() << ")"
|
|
|
|
<< "[" << leftFile->path() << "," << rightFile->path() << "]\n");
|
|
|
|
|
2013-02-08 04:16:12 +08:00
|
|
|
if (leftFile != rightFile)
|
|
|
|
return leftFile->ordinal() < rightFile->ordinal();
|
|
|
|
|
|
|
|
// Sort by atom order with .o file.
|
|
|
|
uint64_t leftOrdinal = left->ordinal();
|
|
|
|
uint64_t rightOrdinal = right->ordinal();
|
2013-03-13 12:05:38 +08:00
|
|
|
|
|
|
|
DEBUG(llvm::dbgs() << "Sorting by ordinal(" << left->ordinal() << ","
|
|
|
|
<< right->ordinal() << ")\n");
|
|
|
|
|
2013-02-08 04:16:12 +08:00
|
|
|
if (leftOrdinal != rightOrdinal)
|
|
|
|
return leftOrdinal < rightOrdinal;
|
|
|
|
|
2013-02-26 09:35:30 +08:00
|
|
|
DEBUG(llvm::dbgs() << "Unordered\n");
|
|
|
|
|
2013-02-08 04:16:12 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) {
|
2013-05-14 08:41:52 +08:00
|
|
|
if (r->kind() != lld::Reference::kindLayoutAfter)
|
|
|
|
continue;
|
|
|
|
const DefinedAtom *targetAtom = llvm::dyn_cast<DefinedAtom>(r->target());
|
|
|
|
_followOnNexts[ai] = targetAtom;
|
|
|
|
|
|
|
|
// If we find a followon for the first time, lets make that atom as the
|
|
|
|
// root atom.
|
|
|
|
if (_followOnRoots.count(ai) == 0)
|
|
|
|
_followOnRoots[ai] = ai;
|
|
|
|
|
|
|
|
auto iter = _followOnRoots.find(targetAtom);
|
|
|
|
if (iter == _followOnRoots.end()) {
|
|
|
|
// If the targetAtom is not a root of any chain, lets make the root of
|
|
|
|
// the targetAtom to the root of the current chain.
|
|
|
|
_followOnRoots[targetAtom] = _followOnRoots[ai];
|
|
|
|
} else if (iter->second == targetAtom) {
|
|
|
|
// 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]);
|
|
|
|
} else {
|
|
|
|
// 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.
|
2013-05-15 00:53:59 +08:00
|
|
|
size_t currentAtomSize = ai->size();
|
2013-05-14 08:41:52 +08:00
|
|
|
if (currentAtomSize == 0) {
|
|
|
|
const DefinedAtom *targetPrevAtom = findAtomFollowedBy(targetAtom);
|
|
|
|
_followOnNexts[targetPrevAtom] = ai;
|
|
|
|
_followOnRoots[ai] = _followOnRoots[targetPrevAtom];
|
2013-02-08 04:16:12 +08:00
|
|
|
} else {
|
2013-05-14 08:41:52 +08:00
|
|
|
if (!checkAllPrevAtomsZeroSize(targetAtom))
|
|
|
|
break;
|
|
|
|
_followOnNexts[ai] = _followOnRoots[targetAtom];
|
|
|
|
setChainRoot(_followOnRoots[targetAtom], _followOnRoots[ai]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
if (r->kind() == lld::Reference::kindInGroup) {
|
|
|
|
const DefinedAtom *rootAtom = llvm::dyn_cast<DefinedAtom>(r->target());
|
|
|
|
// If the root atom is not part of any root
|
2013-03-15 00:09:49 +08:00
|
|
|
// create a new root
|
2013-02-08 04:16:12 +08:00
|
|
|
if (_followOnRoots.count(rootAtom) == 0) {
|
|
|
|
_followOnRoots[rootAtom] = rootAtom;
|
|
|
|
}
|
2013-03-15 00:09:49 +08:00
|
|
|
// 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
|
2013-02-08 04:16:12 +08:00
|
|
|
// as the targetAtom points to the ingroup root
|
|
|
|
auto iter = _followOnRoots.find(ai);
|
|
|
|
if (iter == _followOnRoots.end()) {
|
|
|
|
_followOnRoots[ai] = rootAtom;
|
2013-05-14 08:41:52 +08:00
|
|
|
} else if (iter->second == ai) {
|
|
|
|
if (iter->second != rootAtom)
|
|
|
|
setChainRoot(iter->second, rootAtom);
|
|
|
|
} else {
|
2013-02-08 04:16:12 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the current atom is part of the chain
|
|
|
|
bool isAtomInChain = false;
|
|
|
|
const DefinedAtom *lastAtom = rootAtom;
|
|
|
|
while (true) {
|
2013-03-15 00:09:49 +08:00
|
|
|
AtomToAtomT::iterator followOnAtomsIter =
|
2013-02-08 04:16:12 +08:00
|
|
|
_followOnNexts.find(lastAtom);
|
|
|
|
if (followOnAtomsIter != _followOnNexts.end()) {
|
|
|
|
lastAtom = followOnAtomsIter->second;
|
|
|
|
if (lastAtom == ai) {
|
|
|
|
isAtomInChain = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-03-15 00:09:49 +08:00
|
|
|
else
|
2013-02-08 04:16:12 +08:00
|
|
|
break;
|
|
|
|
} // findAtomInChain
|
|
|
|
|
2013-03-15 00:09:49 +08:00
|
|
|
if (!isAtomInChain)
|
2013-02-08 04:16:12 +08:00
|
|
|
_followOnNexts[lastAtom] = ai;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This pass builds the followon tables using Preceded By relationships
|
|
|
|
/// The algorithm follows a very simple approach
|
|
|
|
/// a) If the targetAtom is not part of any root and the current atom is not
|
2013-03-15 00:09:49 +08:00
|
|
|
/// part of any root, create a chain with the current atom as root and
|
2013-02-08 04:16:12 +08:00
|
|
|
/// the targetAtom as following the current atom
|
2013-03-15 00:09:49 +08:00
|
|
|
/// b) Chain the targetAtom to the current Atom if the targetAtom is not part
|
2013-02-08 04:16:12 +08:00
|
|
|
/// of any chain and the currentAtom has no followOn's
|
|
|
|
/// c) If the targetAtom is part of a different tree and the root of the
|
|
|
|
/// targetAtom is itself, and if the current atom is not part of any root
|
2013-03-15 00:09:49 +08:00
|
|
|
/// chain all the atoms together
|
|
|
|
/// d) If the current atom has no followon and the root of the targetAtom is
|
|
|
|
/// not equal to the root of the current atom(the targetAtom is not in the
|
|
|
|
/// same chain), chain all the atoms that are lead by the targetAtom into
|
2013-02-08 04:16:12 +08:00
|
|
|
/// the current chain
|
|
|
|
void LayoutPass::buildPrecededByTable(MutableFile::DefinedAtomRange &range) {
|
2013-04-06 08:56:40 +08:00
|
|
|
ScopedTask task(getDefaultDomain(), "LayoutPass::buildPrecededByTable");
|
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) {
|
|
|
|
if (r->kind() == lld::Reference::kindLayoutBefore) {
|
|
|
|
const DefinedAtom *targetAtom = llvm::dyn_cast<DefinedAtom>(r->target());
|
|
|
|
// Is the targetAtom not chained
|
|
|
|
if (_followOnRoots.count(targetAtom) == 0) {
|
|
|
|
// Is the current atom not part of any root ?
|
|
|
|
if (_followOnRoots.count(ai) == 0) {
|
|
|
|
_followOnRoots[ai] = ai;
|
|
|
|
_followOnNexts[ai] = targetAtom;
|
|
|
|
_followOnRoots[targetAtom] = _followOnRoots[ai];
|
|
|
|
} else if (_followOnNexts.count(ai) == 0) {
|
|
|
|
// Chain the targetAtom to the current Atom
|
|
|
|
// if the currentAtom has no followon references
|
|
|
|
_followOnNexts[ai] = targetAtom;
|
|
|
|
_followOnRoots[targetAtom] = _followOnRoots[ai];
|
|
|
|
}
|
|
|
|
} else if (_followOnRoots.find(targetAtom)->second == targetAtom) {
|
|
|
|
// Is the targetAtom in chain with the targetAtom as the root ?
|
|
|
|
bool changeRoots = false;
|
|
|
|
if (_followOnRoots.count(ai) == 0) {
|
|
|
|
_followOnRoots[ai] = ai;
|
|
|
|
_followOnNexts[ai] = targetAtom;
|
|
|
|
_followOnRoots[targetAtom] = _followOnRoots[ai];
|
|
|
|
changeRoots = true;
|
|
|
|
} else if (_followOnNexts.count(ai) == 0) {
|
|
|
|
// Chain the targetAtom to the current Atom
|
|
|
|
// if the currentAtom has no followon references
|
|
|
|
if (_followOnRoots[ai] != _followOnRoots[targetAtom]) {
|
|
|
|
_followOnNexts[ai] = targetAtom;
|
|
|
|
_followOnRoots[targetAtom] = _followOnRoots[ai];
|
|
|
|
changeRoots = true;
|
|
|
|
}
|
|
|
|
}
|
2013-03-15 00:09:49 +08:00
|
|
|
// Change the roots of the targetAtom and its chain to
|
2013-02-08 04:16:12 +08:00
|
|
|
// the current atoms root
|
|
|
|
if (changeRoots) {
|
2013-05-14 08:41:52 +08:00
|
|
|
setChainRoot(_followOnRoots[targetAtom], _followOnRoots[ai]);
|
|
|
|
}
|
2013-03-15 00:09:49 +08:00
|
|
|
} // Is targetAtom root
|
2013-02-08 04:16:12 +08:00
|
|
|
} // kindLayoutBefore
|
2013-05-14 08:41:52 +08:00
|
|
|
} // Reference
|
2013-02-08 04:16:12 +08:00
|
|
|
} // atom iteration
|
2013-03-15 00:09:49 +08:00
|
|
|
} // end function
|
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);
|
|
|
|
if (start != _followOnRoots.end()) {
|
|
|
|
for (const DefinedAtom *nextAtom = start->second; nextAtom != NULL;
|
|
|
|
nextAtom = _followOnNexts[nextAtom]) {
|
|
|
|
AtomToOrdinalT::iterator pos = _ordinalOverrideMap.find(nextAtom);
|
|
|
|
if (pos == _ordinalOverrideMap.end()) {
|
|
|
|
_ordinalOverrideMap[nextAtom] = index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-05-23 01:41:04 +08:00
|
|
|
_ordinalOverrideMap[atom] = index++;
|
2013-02-08 04:16:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-08 04:18:39 +08:00
|
|
|
// Helper functions to check follow-on graph.
|
|
|
|
#ifndef NDEBUG
|
|
|
|
namespace {
|
|
|
|
typedef llvm::DenseMap<const DefinedAtom *, const DefinedAtom *> AtomToAtomT;
|
|
|
|
|
|
|
|
std::string atomToDebugString(const Atom *atom) {
|
|
|
|
const DefinedAtom *definedAtom = llvm::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void showCycleDetectedError(AtomToAtomT &followOnNexts,
|
|
|
|
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) {
|
|
|
|
llvm::dbgs() << " " << ref->kindToString()
|
|
|
|
<< ": " << atomToDebugString(ref->target()) << "\n";
|
|
|
|
}
|
|
|
|
atom = followOnNexts[atom];
|
|
|
|
} while (atom != start);
|
2013-07-30 05:50:33 +08:00
|
|
|
llvm::report_fatal_error("Cycle detected");
|
2013-06-08 04:18:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
void checkNoCycleInFollowonChain(AtomToAtomT &followOnNexts,
|
|
|
|
const DefinedAtom *root) {
|
|
|
|
const DefinedAtom *tortoise = root;
|
|
|
|
const DefinedAtom *hare = followOnNexts[root];
|
|
|
|
while (true) {
|
|
|
|
if (!tortoise || !hare)
|
|
|
|
return;
|
|
|
|
if (tortoise == hare)
|
|
|
|
showCycleDetectedError(followOnNexts, tortoise);
|
|
|
|
tortoise = followOnNexts[tortoise];
|
|
|
|
hare = followOnNexts[followOnNexts[hare]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
/// 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)
|
|
|
|
checkNoCycleInFollowonChain(_followOnNexts, root);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // #ifndef NDEBUG
|
|
|
|
|
2013-03-15 00:09:49 +08:00
|
|
|
/// Perform the actual pass
|
2013-02-08 04:16:12 +08:00
|
|
|
void LayoutPass::perform(MutableFile &mergedFile) {
|
2013-05-29 02:55:39 +08:00
|
|
|
ScopedTask task(getDefaultDomain(), "LayoutPass");
|
2013-02-08 04:16:12 +08:00
|
|
|
MutableFile::DefinedAtomRange atomRange = mergedFile.definedAtoms();
|
|
|
|
|
|
|
|
// Build follow on tables
|
|
|
|
buildFollowOnTable(atomRange);
|
|
|
|
|
|
|
|
// Build Ingroup reference table
|
|
|
|
buildInGroupTable(atomRange);
|
|
|
|
|
|
|
|
// Build preceded by tables
|
|
|
|
buildPrecededByTable(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-02-08 04:16:12 +08:00
|
|
|
// sort the atoms
|
2013-05-31 08:05:49 +08:00
|
|
|
std::sort(atomRange.begin(), atomRange.end(), _compareAtoms);
|
2013-04-29 11:27:57 +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
|
|
|
}
|