2014-12-13 01:31:24 +08:00
|
|
|
//===- tools/dsymutil/DebugMap.h - Generic debug map representation -------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
///
|
|
|
|
/// This file contains the class declaration of the DebugMap
|
|
|
|
/// entity. A DebugMap lists all the object files linked together to
|
|
|
|
/// produce an executable along with the linked address of all the
|
|
|
|
/// atoms used in these object files.
|
|
|
|
/// The DebugMap is an input to the DwarfLinker class that will
|
|
|
|
/// extract the Dwarf debug information from the referenced object
|
|
|
|
/// files and link their usefull debug info together.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
|
|
|
|
#define LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
|
|
|
|
|
2015-02-14 07:18:16 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2014-12-13 01:31:24 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2015-01-20 07:33:14 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
2014-12-13 01:31:24 +08:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
2015-06-02 05:12:45 +08:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2014-12-13 01:31:24 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class raw_ostream;
|
|
|
|
|
|
|
|
namespace dsymutil {
|
|
|
|
class DebugMapObject;
|
|
|
|
|
|
|
|
/// \brief The DebugMap object stores the list of object files to
|
|
|
|
/// query for debug information along with the mapping between the
|
|
|
|
/// symbols' addresses in the object file to their linked address in
|
|
|
|
/// the linked binary.
|
|
|
|
///
|
|
|
|
/// A DebugMap producer could look like this:
|
|
|
|
/// DebugMap *DM = new DebugMap();
|
|
|
|
/// for (const auto &Obj: LinkedObjects) {
|
|
|
|
/// DebugMapObject &DMO = DM->addDebugMapObject(Obj.getPath());
|
|
|
|
/// for (const auto &Sym: Obj.getLinkedSymbols())
|
|
|
|
/// DMO.addSymbol(Sym.getName(), Sym.getObjectFileAddress(),
|
|
|
|
/// Sym.getBinaryAddress());
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// A DebugMap consumer can then use the map to link the debug
|
|
|
|
/// information. For example something along the lines of:
|
|
|
|
/// for (const auto &DMO: DM->objects()) {
|
|
|
|
/// auto Obj = createBinary(DMO.getObjectFilename());
|
|
|
|
/// for (auto &DIE: Obj.getDwarfDIEs()) {
|
|
|
|
/// if (SymbolMapping *Sym = DMO.lookup(DIE.getName()))
|
|
|
|
/// DIE.relocate(Sym->ObjectAddress, Sym->BinaryAddress);
|
|
|
|
/// else
|
|
|
|
/// DIE.discardSubtree();
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
class DebugMap {
|
2015-01-20 07:33:14 +08:00
|
|
|
Triple BinaryTriple;
|
2014-12-13 01:31:24 +08:00
|
|
|
typedef std::vector<std::unique_ptr<DebugMapObject>> ObjectContainer;
|
|
|
|
ObjectContainer Objects;
|
|
|
|
|
2015-06-02 05:12:45 +08:00
|
|
|
/// For YAML IO support.
|
|
|
|
///@{
|
|
|
|
friend yaml::MappingTraits<DebugMap>;
|
|
|
|
DebugMap() = default;
|
|
|
|
///@}
|
2014-12-13 01:31:24 +08:00
|
|
|
public:
|
2015-01-20 07:33:14 +08:00
|
|
|
DebugMap(const Triple &BinaryTriple) : BinaryTriple(BinaryTriple) {}
|
|
|
|
|
2014-12-13 01:31:24 +08:00
|
|
|
typedef ObjectContainer::const_iterator const_iterator;
|
|
|
|
|
|
|
|
iterator_range<const_iterator> objects() const {
|
|
|
|
return make_range(begin(), end());
|
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator begin() const { return Objects.begin(); }
|
|
|
|
|
|
|
|
const_iterator end() const { return Objects.end(); }
|
|
|
|
|
|
|
|
/// This function adds an DebugMapObject to the list owned by this
|
|
|
|
/// debug map.
|
|
|
|
DebugMapObject &addDebugMapObject(StringRef ObjectFilePath);
|
|
|
|
|
2015-02-28 08:28:56 +08:00
|
|
|
const Triple &getTriple() const { return BinaryTriple; }
|
2015-01-20 07:33:14 +08:00
|
|
|
|
2014-12-13 01:31:24 +08:00
|
|
|
void print(raw_ostream &OS) const;
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
void dump() const;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief The DebugMapObject represents one object file described by
|
|
|
|
/// the DebugMap. It contains a list of mappings between addresses in
|
|
|
|
/// the object file and in the linked binary for all the linked atoms
|
|
|
|
/// in this object file.
|
|
|
|
class DebugMapObject {
|
|
|
|
public:
|
|
|
|
struct SymbolMapping {
|
2015-06-02 05:12:45 +08:00
|
|
|
yaml::Hex64 ObjectAddress;
|
|
|
|
yaml::Hex64 BinaryAddress;
|
|
|
|
yaml::Hex32 Size;
|
2015-03-15 09:29:30 +08:00
|
|
|
SymbolMapping(uint64_t ObjectAddress, uint64_t BinaryAddress, uint32_t Size)
|
|
|
|
: ObjectAddress(ObjectAddress), BinaryAddress(BinaryAddress),
|
|
|
|
Size(Size) {}
|
2015-06-02 05:12:45 +08:00
|
|
|
/// For YAML IO support
|
|
|
|
SymbolMapping() = default;
|
2014-12-13 01:31:24 +08:00
|
|
|
};
|
|
|
|
|
2015-02-14 07:18:16 +08:00
|
|
|
typedef StringMapEntry<SymbolMapping> DebugMapEntry;
|
|
|
|
|
2014-12-13 01:31:24 +08:00
|
|
|
/// \brief Adds a symbol mapping to this DebugMapObject.
|
|
|
|
/// \returns false if the symbol was already registered. The request
|
|
|
|
/// is discarded in this case.
|
|
|
|
bool addSymbol(llvm::StringRef SymName, uint64_t ObjectAddress,
|
2015-03-15 09:29:30 +08:00
|
|
|
uint64_t LinkedAddress, uint32_t Size);
|
2014-12-13 01:31:24 +08:00
|
|
|
|
2014-12-14 03:19:07 +08:00
|
|
|
/// \brief Lookup a symbol mapping.
|
2014-12-13 01:31:24 +08:00
|
|
|
/// \returns null if the symbol isn't found.
|
2015-02-14 07:18:16 +08:00
|
|
|
const DebugMapEntry *lookupSymbol(StringRef SymbolName) const;
|
|
|
|
|
|
|
|
/// \brief Lookup an objectfile address.
|
|
|
|
/// \returns null if the address isn't found.
|
|
|
|
const DebugMapEntry *lookupObjectAddress(uint64_t Address) const;
|
2014-12-13 01:31:24 +08:00
|
|
|
|
|
|
|
llvm::StringRef getObjectFilename() const { return Filename; }
|
|
|
|
|
2015-03-15 10:02:53 +08:00
|
|
|
iterator_range<StringMap<SymbolMapping>::const_iterator> symbols() const {
|
|
|
|
return make_range(Symbols.begin(), Symbols.end());
|
|
|
|
}
|
|
|
|
|
2014-12-13 01:31:24 +08:00
|
|
|
void print(raw_ostream &OS) const;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
void dump() const;
|
|
|
|
#endif
|
|
|
|
private:
|
|
|
|
friend class DebugMap;
|
|
|
|
/// DebugMapObjects can only be constructed by the owning DebugMap.
|
|
|
|
DebugMapObject(StringRef ObjectFilename);
|
|
|
|
|
|
|
|
std::string Filename;
|
|
|
|
StringMap<SymbolMapping> Symbols;
|
2015-02-14 07:18:16 +08:00
|
|
|
DenseMap<uint64_t, DebugMapEntry *> AddressToMapping;
|
2015-06-02 05:12:45 +08:00
|
|
|
|
|
|
|
/// For YAMLIO support.
|
|
|
|
///@{
|
|
|
|
typedef std::pair<std::string, SymbolMapping> YAMLSymbolMapping;
|
|
|
|
friend yaml::MappingTraits<dsymutil::DebugMapObject>;
|
|
|
|
friend yaml::SequenceTraits<std::vector<std::unique_ptr<DebugMapObject>>>;
|
|
|
|
friend yaml::SequenceTraits<std::vector<YAMLSymbolMapping>>;
|
|
|
|
DebugMapObject() = default;
|
|
|
|
///@}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-02 05:25:53 +08:00
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dsymutil::DebugMapObject::YAMLSymbolMapping)
|
2015-06-02 05:12:45 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace yaml {
|
|
|
|
|
|
|
|
using namespace llvm::dsymutil;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>> {
|
|
|
|
|
|
|
|
static void
|
|
|
|
mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
|
|
|
|
io.mapRequired("sym", s.first);
|
|
|
|
io.mapRequired("objAddr", s.second.ObjectAddress);
|
|
|
|
io.mapRequired("binAddr", s.second.BinaryAddress);
|
|
|
|
io.mapOptional("size", s.second.Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const bool flow = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MappingTraits<dsymutil::DebugMapObject> {
|
2015-06-04 01:08:42 +08:00
|
|
|
typedef StringMap<dsymutil::DebugMapObject::SymbolMapping> SymbolMap;
|
|
|
|
|
|
|
|
struct SequencedStringMap {
|
|
|
|
SequencedStringMap(IO &io) {}
|
|
|
|
|
|
|
|
SequencedStringMap(IO &io, SymbolMap &Map) {
|
|
|
|
Entries.reserve(Map.size());
|
|
|
|
for (auto &Entry : Map)
|
2015-06-02 05:12:45 +08:00
|
|
|
Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:08:42 +08:00
|
|
|
SymbolMap denormalize(IO &) {
|
|
|
|
SymbolMap Res;
|
|
|
|
|
|
|
|
for (auto &Entry : Entries)
|
|
|
|
Res[Entry.first] = Entry.second;
|
|
|
|
|
2015-06-02 06:03:05 +08:00
|
|
|
return Res;
|
2015-06-02 05:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
|
|
|
|
};
|
|
|
|
|
2015-06-04 01:08:42 +08:00
|
|
|
static void mapping(IO &io, dsymutil::DebugMapObject &s) {
|
|
|
|
MappingNormalization<SequencedStringMap, SymbolMap> seq(io, s.Symbols);
|
|
|
|
io.mapRequired("filename", s.Filename);
|
|
|
|
io.mapRequired("symbols", seq->Entries);
|
2015-06-02 05:12:45 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct ScalarTraits<Triple> {
|
|
|
|
|
|
|
|
static void output(const Triple &val, void *, llvm::raw_ostream &out) {
|
|
|
|
out << val.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringRef input(StringRef scalar, void *, Triple &value) {
|
|
|
|
value = Triple(scalar);
|
2015-06-04 01:08:42 +08:00
|
|
|
return value.str();
|
2015-06-02 05:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool mustQuote(StringRef) { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>> {
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
|
|
|
|
return seq.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
static dsymutil::DebugMapObject &
|
|
|
|
element(IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
|
|
|
|
size_t index) {
|
|
|
|
if (index >= seq.size()) {
|
|
|
|
seq.resize(index + 1);
|
|
|
|
seq[index].reset(new dsymutil::DebugMapObject);
|
|
|
|
}
|
|
|
|
return *seq[index];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MappingTraits<dsymutil::DebugMap> {
|
|
|
|
static void mapping(IO &io, dsymutil::DebugMap &DM) {
|
|
|
|
io.mapRequired("triple", DM.BinaryTriple);
|
|
|
|
io.mapOptional("objects", DM.Objects);
|
|
|
|
}
|
2014-12-13 01:31:24 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
|