2017-11-01 22:16:06 +01:00
|
|
|
//=- tools/dsymutil/DebugMap.h - Generic debug map representation -*- C++ -*-=//
|
2014-12-12 18:31:24 +01:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-12-12 18:31:24 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-11-01 22:16:06 +01:00
|
|
|
//
|
2014-12-12 18:31:24 +01:00
|
|
|
/// \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.
|
2017-11-01 22:16:06 +01:00
|
|
|
//
|
2014-12-12 18:31:24 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2014-12-12 18:31:24 +01:00
|
|
|
#ifndef LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
|
|
|
|
#define LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
|
|
|
|
|
2015-02-14 00:18:16 +01:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2014-12-12 18:31:24 +01:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2015-01-20 00:33:14 +01:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2015-01-14 12:23:27 +01:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
2018-02-08 11:48:54 +01:00
|
|
|
#include "llvm/Object/MachO.h"
|
2016-11-09 12:43:52 +01:00
|
|
|
#include "llvm/Support/Chrono.h"
|
2014-12-12 18:31:24 +01:00
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2015-06-01 23:12:45 +02:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include <chrono>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2014-12-12 18:31:24 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2014-12-12 18:31:24 +01:00
|
|
|
class raw_ostream;
|
|
|
|
|
|
|
|
namespace dsymutil {
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2014-12-12 18:31:24 +01:00
|
|
|
class DebugMapObject;
|
|
|
|
|
2018-02-22 12:43:43 +01:00
|
|
|
/// 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.
|
2014-12-12 18:31:24 +01:00
|
|
|
///
|
|
|
|
/// 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 00:33:14 +01:00
|
|
|
Triple BinaryTriple;
|
2015-08-26 07:09:59 +02:00
|
|
|
std::string BinaryPath;
|
2019-01-08 00:27:25 +01:00
|
|
|
std::vector<uint8_t> BinaryUUID;
|
2017-11-01 22:16:06 +01:00
|
|
|
using ObjectContainer = std::vector<std::unique_ptr<DebugMapObject>>;
|
|
|
|
|
2014-12-12 18:31:24 +01:00
|
|
|
ObjectContainer Objects;
|
|
|
|
|
2015-06-01 23:12:45 +02:00
|
|
|
/// For YAML IO support.
|
|
|
|
///@{
|
2015-06-03 22:29:24 +02:00
|
|
|
friend yaml::MappingTraits<std::unique_ptr<DebugMap>>;
|
2015-06-01 23:12:45 +02:00
|
|
|
friend yaml::MappingTraits<DebugMap>;
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2015-06-01 23:12:45 +02:00
|
|
|
DebugMap() = default;
|
|
|
|
///@}
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2014-12-12 18:31:24 +01:00
|
|
|
public:
|
2019-01-08 00:27:25 +01:00
|
|
|
DebugMap(const Triple &BinaryTriple, StringRef BinaryPath,
|
|
|
|
ArrayRef<uint8_t> BinaryUUID = ArrayRef<uint8_t>())
|
2020-01-28 20:23:46 +01:00
|
|
|
: BinaryTriple(BinaryTriple), BinaryPath(std::string(BinaryPath)),
|
2019-01-08 00:27:25 +01:00
|
|
|
BinaryUUID(BinaryUUID.begin(), BinaryUUID.end()) {}
|
2015-01-20 00:33:14 +01:00
|
|
|
|
2017-11-01 22:16:06 +01:00
|
|
|
using const_iterator = ObjectContainer::const_iterator;
|
2014-12-12 18:31:24 +01:00
|
|
|
|
|
|
|
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(); }
|
|
|
|
|
2018-03-13 11:52:49 +01:00
|
|
|
unsigned getNumberOfObjects() const { return Objects.size(); }
|
|
|
|
|
2014-12-12 18:31:24 +01:00
|
|
|
/// This function adds an DebugMapObject to the list owned by this
|
|
|
|
/// debug map.
|
2016-11-09 12:43:52 +01:00
|
|
|
DebugMapObject &
|
|
|
|
addDebugMapObject(StringRef ObjectFilePath,
|
2017-10-06 16:49:20 +02:00
|
|
|
sys::TimePoint<std::chrono::seconds> Timestamp,
|
2018-02-08 11:48:54 +01:00
|
|
|
uint8_t Type = llvm::MachO::N_OSO);
|
2014-12-12 18:31:24 +01:00
|
|
|
|
2015-02-28 01:28:56 +01:00
|
|
|
const Triple &getTriple() const { return BinaryTriple; }
|
2015-01-20 00:33:14 +01:00
|
|
|
|
2019-01-08 00:27:25 +01:00
|
|
|
const ArrayRef<uint8_t> getUUID() const {
|
|
|
|
return ArrayRef<uint8_t>(BinaryUUID);
|
|
|
|
}
|
|
|
|
|
2015-08-26 07:09:59 +02:00
|
|
|
StringRef getBinaryPath() const { return BinaryPath; }
|
|
|
|
|
2014-12-12 18:31:24 +01:00
|
|
|
void print(raw_ostream &OS) const;
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
void dump() const;
|
|
|
|
#endif
|
2015-06-05 22:27:04 +02:00
|
|
|
|
|
|
|
/// Read a debug map for \a InputFile.
|
2015-08-05 20:27:44 +02:00
|
|
|
static ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
|
2015-06-05 22:27:04 +02:00
|
|
|
parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose);
|
2014-12-12 18:31:24 +01:00
|
|
|
};
|
|
|
|
|
2018-02-22 12:43:43 +01:00
|
|
|
/// 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.
|
2014-12-12 18:31:24 +01:00
|
|
|
class DebugMapObject {
|
|
|
|
public:
|
|
|
|
struct SymbolMapping {
|
2016-01-31 05:29:22 +01:00
|
|
|
Optional<yaml::Hex64> ObjectAddress;
|
2015-06-01 23:12:45 +02:00
|
|
|
yaml::Hex64 BinaryAddress;
|
|
|
|
yaml::Hex32 Size;
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2016-01-31 05:29:22 +01:00
|
|
|
SymbolMapping(Optional<uint64_t> ObjectAddr, uint64_t BinaryAddress,
|
|
|
|
uint32_t Size)
|
|
|
|
: BinaryAddress(BinaryAddress), Size(Size) {
|
|
|
|
if (ObjectAddr)
|
|
|
|
ObjectAddress = *ObjectAddr;
|
|
|
|
}
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2015-06-01 23:12:45 +02:00
|
|
|
/// For YAML IO support
|
|
|
|
SymbolMapping() = default;
|
2014-12-12 18:31:24 +01:00
|
|
|
};
|
|
|
|
|
2017-11-01 22:16:06 +01:00
|
|
|
using YAMLSymbolMapping = std::pair<std::string, SymbolMapping>;
|
|
|
|
using DebugMapEntry = StringMapEntry<SymbolMapping>;
|
2015-02-14 00:18:16 +01:00
|
|
|
|
2018-02-22 12:43:43 +01:00
|
|
|
/// Adds a symbol mapping to this DebugMapObject.
|
2014-12-12 18:31:24 +01:00
|
|
|
/// \returns false if the symbol was already registered. The request
|
|
|
|
/// is discarded in this case.
|
2017-11-01 22:16:06 +01:00
|
|
|
bool addSymbol(StringRef SymName, Optional<uint64_t> ObjectAddress,
|
2015-03-15 02:29:30 +01:00
|
|
|
uint64_t LinkedAddress, uint32_t Size);
|
2014-12-12 18:31:24 +01:00
|
|
|
|
2018-02-22 12:43:43 +01:00
|
|
|
/// Lookup a symbol mapping.
|
2014-12-12 18:31:24 +01:00
|
|
|
/// \returns null if the symbol isn't found.
|
2015-02-14 00:18:16 +01:00
|
|
|
const DebugMapEntry *lookupSymbol(StringRef SymbolName) const;
|
|
|
|
|
2018-02-22 12:43:43 +01:00
|
|
|
/// Lookup an object file address.
|
2015-02-14 00:18:16 +01:00
|
|
|
/// \returns null if the address isn't found.
|
|
|
|
const DebugMapEntry *lookupObjectAddress(uint64_t Address) const;
|
2014-12-12 18:31:24 +01:00
|
|
|
|
2017-11-01 22:16:06 +01:00
|
|
|
StringRef getObjectFilename() const { return Filename; }
|
2014-12-12 18:31:24 +01:00
|
|
|
|
2016-11-09 12:43:52 +01:00
|
|
|
sys::TimePoint<std::chrono::seconds> getTimestamp() const {
|
|
|
|
return Timestamp;
|
|
|
|
}
|
2015-07-23 01:24:00 +02:00
|
|
|
|
2017-10-06 16:49:20 +02:00
|
|
|
uint8_t getType() const { return Type; }
|
|
|
|
|
2015-03-15 03:02:53 +01:00
|
|
|
iterator_range<StringMap<SymbolMapping>::const_iterator> symbols() const {
|
|
|
|
return make_range(Symbols.begin(), Symbols.end());
|
|
|
|
}
|
|
|
|
|
2018-04-02 12:40:43 +02:00
|
|
|
bool empty() const { return Symbols.empty(); }
|
|
|
|
|
2020-01-28 20:23:46 +01:00
|
|
|
void addWarning(StringRef Warning) {
|
|
|
|
Warnings.push_back(std::string(Warning));
|
|
|
|
}
|
2018-04-02 12:40:43 +02:00
|
|
|
const std::vector<std::string> &getWarnings() const { return Warnings; }
|
|
|
|
|
2014-12-12 18:31:24 +01:00
|
|
|
void print(raw_ostream &OS) const;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
void dump() const;
|
|
|
|
#endif
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2014-12-12 18:31:24 +01:00
|
|
|
private:
|
|
|
|
friend class DebugMap;
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2014-12-12 18:31:24 +01:00
|
|
|
/// DebugMapObjects can only be constructed by the owning DebugMap.
|
2016-11-09 12:43:52 +01:00
|
|
|
DebugMapObject(StringRef ObjectFilename,
|
2017-10-06 16:49:20 +02:00
|
|
|
sys::TimePoint<std::chrono::seconds> Timestamp, uint8_t Type);
|
2014-12-12 18:31:24 +01:00
|
|
|
|
|
|
|
std::string Filename;
|
2016-11-09 12:43:52 +01:00
|
|
|
sys::TimePoint<std::chrono::seconds> Timestamp;
|
2014-12-12 18:31:24 +01:00
|
|
|
StringMap<SymbolMapping> Symbols;
|
2015-02-14 00:18:16 +01:00
|
|
|
DenseMap<uint64_t, DebugMapEntry *> AddressToMapping;
|
2017-10-06 16:49:20 +02:00
|
|
|
uint8_t Type;
|
2015-06-01 23:12:45 +02:00
|
|
|
|
2018-04-02 12:40:43 +02:00
|
|
|
std::vector<std::string> Warnings;
|
|
|
|
|
2015-06-01 23:12:45 +02:00
|
|
|
/// For YAMLIO support.
|
|
|
|
///@{
|
|
|
|
friend yaml::MappingTraits<dsymutil::DebugMapObject>;
|
|
|
|
friend yaml::SequenceTraits<std::vector<std::unique_ptr<DebugMapObject>>>;
|
2017-11-01 22:16:06 +01:00
|
|
|
|
2015-06-01 23:12:45 +02:00
|
|
|
DebugMapObject() = default;
|
2015-06-05 22:27:07 +02:00
|
|
|
|
|
|
|
public:
|
2016-10-27 17:23:44 +02:00
|
|
|
DebugMapObject(DebugMapObject &&) = default;
|
|
|
|
DebugMapObject &operator=(DebugMapObject &&) = default;
|
2015-06-01 23:12:45 +02:00
|
|
|
///@}
|
|
|
|
};
|
2017-11-01 22:16:06 +01:00
|
|
|
|
|
|
|
} // end namespace dsymutil
|
|
|
|
} // end namespace llvm
|
2015-06-01 23:12:45 +02:00
|
|
|
|
2015-06-01 23:25:53 +02:00
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dsymutil::DebugMapObject::YAMLSymbolMapping)
|
2015-06-01 23:12:45 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace yaml {
|
|
|
|
|
|
|
|
using namespace llvm::dsymutil;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>> {
|
2015-06-05 22:27:04 +02:00
|
|
|
static void mapping(IO &io,
|
|
|
|
std::pair<std::string, DebugMapObject::SymbolMapping> &s);
|
2015-06-01 23:12:45 +02:00
|
|
|
static const bool flow = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MappingTraits<dsymutil::DebugMapObject> {
|
2015-06-05 22:27:04 +02:00
|
|
|
struct YamlDMO;
|
|
|
|
static void mapping(IO &io, dsymutil::DebugMapObject &DMO);
|
2015-06-01 23:12:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct ScalarTraits<Triple> {
|
2017-11-01 22:16:06 +01:00
|
|
|
static void output(const Triple &val, void *, raw_ostream &out);
|
2015-06-05 22:27:04 +02:00
|
|
|
static StringRef input(StringRef scalar, void *, Triple &value);
|
2017-12-18 18:38:03 +01:00
|
|
|
static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
|
2015-06-01 23:12:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>> {
|
|
|
|
static size_t
|
2015-06-05 22:27:04 +02:00
|
|
|
size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq);
|
2015-06-01 23:12:45 +02:00
|
|
|
static dsymutil::DebugMapObject &
|
|
|
|
element(IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
|
2015-06-05 22:27:04 +02:00
|
|
|
size_t index);
|
2015-06-01 23:12:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MappingTraits<dsymutil::DebugMap> {
|
2015-06-05 22:27:04 +02:00
|
|
|
static void mapping(IO &io, dsymutil::DebugMap &DM);
|
2014-12-12 18:31:24 +01:00
|
|
|
};
|
2015-06-03 22:29:24 +02:00
|
|
|
|
2015-06-05 22:27:04 +02:00
|
|
|
template <> struct MappingTraits<std::unique_ptr<dsymutil::DebugMap>> {
|
|
|
|
static void mapping(IO &io, std::unique_ptr<dsymutil::DebugMap> &DM);
|
2015-06-03 22:29:24 +02:00
|
|
|
};
|
2017-11-01 22:16:06 +01:00
|
|
|
|
|
|
|
} // end namespace yaml
|
|
|
|
} // end namespace llvm
|
2014-12-12 18:31:24 +01:00
|
|
|
|
|
|
|
#endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
|