2014-12-13 01:31:24 +08:00
|
|
|
//===- tools/dsymutil/DwarfLinker.cpp - Dwarf debug info linker -----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-01-29 02:27:01 +08:00
|
|
|
#include "BinaryHolder.h"
|
|
|
|
#include "DebugMap.h"
|
2015-09-03 00:49:13 +08:00
|
|
|
#include "MachOUtils.h"
|
2015-08-26 13:09:52 +08:00
|
|
|
#include "NonRelocatableStringpool.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "dsymutil.h"
|
2015-03-13 02:45:10 +08:00
|
|
|
#include "llvm/ADT/IntervalMap.h"
|
2015-03-12 02:46:01 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/BinaryFormat/Dwarf.h"
|
2015-02-28 08:29:11 +08:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2015-03-05 06:07:44 +08:00
|
|
|
#include "llvm/CodeGen/DIE.h"
|
2015-07-22 06:41:43 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2015-01-31 02:07:45 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
|
2015-02-14 07:18:29 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
|
2015-02-28 08:29:11 +08:00
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2015-03-16 04:45:43 +08:00
|
|
|
#include "llvm/MC/MCDwarf.h"
|
2015-02-28 08:29:11 +08:00
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2015-05-16 06:19:42 +08:00
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2015-12-22 06:09:27 +08:00
|
|
|
#include "llvm/MC/MCTargetOptionsCommandFlags.h"
|
2015-02-14 07:18:22 +08:00
|
|
|
#include "llvm/Object/MachO.h"
|
2015-02-14 07:18:34 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2015-02-28 08:29:11 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2016-12-02 02:56:29 +08:00
|
|
|
#include <memory>
|
2015-01-29 02:27:01 +08:00
|
|
|
#include <string>
|
2015-03-14 02:35:57 +08:00
|
|
|
#include <tuple>
|
2014-12-13 01:31:24 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace dsymutil {
|
|
|
|
|
2015-01-29 02:27:01 +08:00
|
|
|
namespace {
|
|
|
|
|
2015-03-13 02:45:10 +08:00
|
|
|
template <typename KeyT, typename ValT>
|
|
|
|
using HalfOpenIntervalMap =
|
|
|
|
IntervalMap<KeyT, ValT, IntervalMapImpl::NodeSizer<KeyT, ValT>::LeafSize,
|
|
|
|
IntervalMapHalfOpenInfo<KeyT>>;
|
|
|
|
|
2015-03-14 07:30:31 +08:00
|
|
|
typedef HalfOpenIntervalMap<uint64_t, int64_t> FunctionIntervals;
|
|
|
|
|
2015-06-26 07:46:41 +08:00
|
|
|
// FIXME: Delete this structure.
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
struct PatchLocation {
|
2015-06-26 07:46:41 +08:00
|
|
|
DIE::value_iterator I;
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
|
2015-06-26 07:46:41 +08:00
|
|
|
PatchLocation() = default;
|
|
|
|
PatchLocation(DIE::value_iterator I) : I(I) {}
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
|
|
|
|
void set(uint64_t New) const {
|
2015-06-26 07:46:41 +08:00
|
|
|
assert(I);
|
|
|
|
const auto &Old = *I;
|
2015-05-28 06:31:41 +08:00
|
|
|
assert(Old.getType() == DIEValue::isInteger);
|
2015-06-26 07:46:41 +08:00
|
|
|
*I = DIEValue(Old.getAttribute(), Old.getForm(), DIEInteger(New));
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t get() const {
|
2015-06-26 07:46:41 +08:00
|
|
|
assert(I);
|
|
|
|
return I->getDIEInteger().getValue();
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-22 06:41:43 +08:00
|
|
|
class CompileUnit;
|
|
|
|
struct DeclMapInfo;
|
|
|
|
|
|
|
|
/// A DeclContext is a named program scope that is used for ODR
|
|
|
|
/// uniquing of types.
|
|
|
|
/// The set of DeclContext for the ODR-subject parts of a Dwarf link
|
|
|
|
/// is expanded (and uniqued) with each new object file processed. We
|
|
|
|
/// need to determine the context of each DIE in an linked object file
|
|
|
|
/// to see if the corresponding type has already been emitted.
|
|
|
|
///
|
|
|
|
/// The contexts are conceptually organised as a tree (eg. a function
|
|
|
|
/// scope is contained in a namespace scope that contains other
|
|
|
|
/// scopes), but storing/accessing them in an actual tree is too
|
|
|
|
/// inefficient: we need to be able to very quickly query a context
|
|
|
|
/// for a given child context by name. Storing a StringMap in each
|
|
|
|
/// DeclContext would be too space inefficient.
|
|
|
|
/// The solution here is to give each DeclContext a link to its parent
|
|
|
|
/// (this allows to walk up the tree), but to query the existance of a
|
|
|
|
/// specific DeclContext using a separate DenseMap keyed on the hash
|
|
|
|
/// of the fully qualified name of the context.
|
|
|
|
class DeclContext {
|
2017-07-22 00:51:16 +08:00
|
|
|
unsigned QualifiedNameHash = 0;
|
|
|
|
uint32_t Line = 0;
|
|
|
|
uint32_t ByteSize = 0;
|
|
|
|
uint16_t Tag = dwarf::DW_TAG_compile_unit;
|
2017-07-21 10:07:33 +08:00
|
|
|
unsigned DefinedInClangModule : 1;
|
2015-07-22 06:41:43 +08:00
|
|
|
StringRef Name;
|
|
|
|
StringRef File;
|
|
|
|
const DeclContext &Parent;
|
2016-12-14 02:25:19 +08:00
|
|
|
DWARFDie LastSeenDIE;
|
2017-07-22 00:51:16 +08:00
|
|
|
uint32_t LastSeenCompileUnitID = 0;
|
|
|
|
uint32_t CanonicalDIEOffset = 0;
|
2015-07-22 06:41:43 +08:00
|
|
|
|
|
|
|
friend DeclMapInfo;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef DenseSet<DeclContext *, DeclMapInfo> Map;
|
|
|
|
|
2017-07-22 00:51:16 +08:00
|
|
|
DeclContext() : DefinedInClangModule(0), Parent(*this) {}
|
2015-07-22 06:41:43 +08:00
|
|
|
|
|
|
|
DeclContext(unsigned Hash, uint32_t Line, uint32_t ByteSize, uint16_t Tag,
|
|
|
|
StringRef Name, StringRef File, const DeclContext &Parent,
|
2016-12-14 02:25:19 +08:00
|
|
|
DWARFDie LastSeenDIE = DWARFDie(), unsigned CUId = 0)
|
2015-07-22 06:41:43 +08:00
|
|
|
: QualifiedNameHash(Hash), Line(Line), ByteSize(ByteSize), Tag(Tag),
|
2017-07-21 10:07:33 +08:00
|
|
|
DefinedInClangModule(0), Name(Name), File(File), Parent(Parent),
|
2017-07-22 00:51:16 +08:00
|
|
|
LastSeenDIE(LastSeenDIE), LastSeenCompileUnitID(CUId) {}
|
2015-07-22 06:41:43 +08:00
|
|
|
|
|
|
|
uint32_t getQualifiedNameHash() const { return QualifiedNameHash; }
|
|
|
|
|
2016-12-14 02:25:19 +08:00
|
|
|
bool setLastSeenDIE(CompileUnit &U, const DWARFDie &Die);
|
2015-07-22 06:41:43 +08:00
|
|
|
|
|
|
|
uint32_t getCanonicalDIEOffset() const { return CanonicalDIEOffset; }
|
|
|
|
void setCanonicalDIEOffset(uint32_t Offset) { CanonicalDIEOffset = Offset; }
|
|
|
|
|
2017-07-21 10:07:33 +08:00
|
|
|
bool isDefinedInClangModule() const { return DefinedInClangModule; }
|
|
|
|
void setDefinedInClangModule(bool Val) { DefinedInClangModule = Val; }
|
|
|
|
|
2015-07-22 06:41:43 +08:00
|
|
|
uint16_t getTag() const { return Tag; }
|
|
|
|
StringRef getName() const { return Name; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Info type for the DenseMap storing the DeclContext pointers.
|
|
|
|
struct DeclMapInfo : private DenseMapInfo<DeclContext *> {
|
|
|
|
using DenseMapInfo<DeclContext *>::getEmptyKey;
|
|
|
|
using DenseMapInfo<DeclContext *>::getTombstoneKey;
|
|
|
|
|
|
|
|
static unsigned getHashValue(const DeclContext *Ctxt) {
|
|
|
|
return Ctxt->QualifiedNameHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isEqual(const DeclContext *LHS, const DeclContext *RHS) {
|
|
|
|
if (RHS == getEmptyKey() || RHS == getTombstoneKey())
|
|
|
|
return RHS == LHS;
|
|
|
|
return LHS->QualifiedNameHash == RHS->QualifiedNameHash &&
|
|
|
|
LHS->Line == RHS->Line && LHS->ByteSize == RHS->ByteSize &&
|
|
|
|
LHS->Name.data() == RHS->Name.data() &&
|
|
|
|
LHS->File.data() == RHS->File.data() &&
|
|
|
|
LHS->Parent.QualifiedNameHash == RHS->Parent.QualifiedNameHash;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// This class gives a tree-like API to the DenseMap that stores the
|
|
|
|
/// DeclContext objects. It also holds the BumpPtrAllocator where
|
|
|
|
/// these objects will be allocated.
|
|
|
|
class DeclContextTree {
|
|
|
|
BumpPtrAllocator Allocator;
|
|
|
|
DeclContext Root;
|
|
|
|
DeclContext::Map Contexts;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Get the child of \a Context described by \a DIE in \a Unit. The
|
|
|
|
/// required strings will be interned in \a StringPool.
|
|
|
|
/// \returns The child DeclContext along with one bit that is set if
|
|
|
|
/// this context is invalid.
|
2015-10-02 08:27:08 +08:00
|
|
|
/// An invalid context means it shouldn't be considered for uniquing, but its
|
|
|
|
/// not returning null, because some children of that context might be
|
|
|
|
/// uniquing candidates. FIXME: The invalid bit along the return value is to
|
|
|
|
/// emulate some dsymutil-classic functionality.
|
2015-07-22 06:41:43 +08:00
|
|
|
PointerIntPair<DeclContext *, 1>
|
|
|
|
getChildDeclContext(DeclContext &Context,
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &DIE, CompileUnit &Unit,
|
2015-10-02 08:27:08 +08:00
|
|
|
NonRelocatableStringpool &StringPool, bool InClangModule);
|
2015-07-22 06:41:43 +08:00
|
|
|
|
|
|
|
DeclContext &getRoot() { return Root; }
|
|
|
|
};
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Stores all information relating to a compile unit, be it in its original
|
|
|
|
/// instance in the object file to its brand new cloned and linked DIE tree.
|
2015-01-29 06:15:14 +08:00
|
|
|
class CompileUnit {
|
|
|
|
public:
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Information gathered about a DIE in the object file.
|
2015-01-29 06:15:14 +08:00
|
|
|
struct DIEInfo {
|
2017-09-01 04:22:31 +08:00
|
|
|
int64_t AddrAdjust; ///< Address offset to apply to the described entity.
|
|
|
|
DeclContext *Ctxt; ///< ODR Declaration context.
|
|
|
|
DIE *Clone; ///< Cloned version of that DIE.
|
|
|
|
uint32_t ParentIdx; ///< The index of this DIE's parent.
|
|
|
|
bool Keep : 1; ///< Is the DIE part of the linked output?
|
|
|
|
bool InDebugMap : 1; ///< Was this DIE's entity found in the map?
|
|
|
|
bool Prune : 1; ///< Is this a pure forward declaration we can strip?
|
|
|
|
bool Incomplete : 1; ///< Does DIE transitively refer an incomplete decl?
|
2015-01-29 06:15:14 +08:00
|
|
|
};
|
|
|
|
|
2015-09-24 01:35:52 +08:00
|
|
|
CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR,
|
|
|
|
StringRef ClangModuleName)
|
2017-02-10 03:41:55 +08:00
|
|
|
: OrigUnit(OrigUnit), ID(ID), LowPc(UINT64_MAX), HighPc(0), RangeAlloc(),
|
|
|
|
Ranges(RangeAlloc), ClangModuleName(ClangModuleName) {
|
2015-01-29 06:15:14 +08:00
|
|
|
Info.resize(OrigUnit.getNumDIEs());
|
2015-07-22 06:41:43 +08:00
|
|
|
|
2016-12-14 02:25:19 +08:00
|
|
|
auto CUDie = OrigUnit.getUnitDIE(false);
|
2017-01-14 05:08:18 +08:00
|
|
|
if (auto Lang = dwarf::toUnsigned(CUDie.find(dwarf::DW_AT_language)))
|
|
|
|
HasODR = CanUseODR && (*Lang == dwarf::DW_LANG_C_plus_plus ||
|
|
|
|
*Lang == dwarf::DW_LANG_C_plus_plus_03 ||
|
|
|
|
*Lang == dwarf::DW_LANG_C_plus_plus_11 ||
|
|
|
|
*Lang == dwarf::DW_LANG_C_plus_plus_14 ||
|
|
|
|
*Lang == dwarf::DW_LANG_ObjC_plus_plus);
|
2017-01-16 00:42:36 +08:00
|
|
|
else
|
|
|
|
HasODR = false;
|
2015-01-29 06:15:14 +08:00
|
|
|
}
|
|
|
|
|
2015-02-14 07:18:27 +08:00
|
|
|
DWARFUnit &getOrigUnit() const { return OrigUnit; }
|
2015-01-29 06:15:14 +08:00
|
|
|
|
2015-03-14 11:46:40 +08:00
|
|
|
unsigned getUniqueID() const { return ID; }
|
|
|
|
|
2017-02-10 03:41:55 +08:00
|
|
|
void createOutputDIE() {
|
|
|
|
NewUnit.emplace(OrigUnit.getVersion(), OrigUnit.getAddressByteSize(),
|
|
|
|
OrigUnit.getUnitDIE().getTag());
|
|
|
|
}
|
|
|
|
|
2016-12-02 02:56:29 +08:00
|
|
|
DIE *getOutputUnitDIE() const {
|
2017-02-10 03:41:55 +08:00
|
|
|
if (NewUnit)
|
2017-04-22 10:18:00 +08:00
|
|
|
return &const_cast<BasicDIEUnit &>(*NewUnit).getUnitDie();
|
2017-02-10 03:41:55 +08:00
|
|
|
return nullptr;
|
2016-12-02 02:56:29 +08:00
|
|
|
}
|
2015-03-05 06:07:44 +08:00
|
|
|
|
2015-07-22 06:41:43 +08:00
|
|
|
bool hasODR() const { return HasODR; }
|
2015-09-24 01:35:52 +08:00
|
|
|
bool isClangModule() const { return !ClangModuleName.empty(); }
|
|
|
|
const std::string &getClangModuleName() const { return ClangModuleName; }
|
2015-07-22 06:41:43 +08:00
|
|
|
|
2015-01-29 06:15:14 +08:00
|
|
|
DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
|
|
|
|
const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
|
|
|
|
|
2015-03-05 06:07:44 +08:00
|
|
|
uint64_t getStartOffset() const { return StartOffset; }
|
|
|
|
uint64_t getNextUnitOffset() const { return NextUnitOffset; }
|
2015-03-14 07:30:27 +08:00
|
|
|
void setStartOffset(uint64_t DebugInfoSize) { StartOffset = DebugInfoSize; }
|
2015-03-05 06:07:44 +08:00
|
|
|
|
2015-03-14 02:35:54 +08:00
|
|
|
uint64_t getLowPc() const { return LowPc; }
|
|
|
|
uint64_t getHighPc() const { return HighPc; }
|
|
|
|
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
Optional<PatchLocation> getUnitRangesAttribute() const {
|
|
|
|
return UnitRangeAttribute;
|
|
|
|
}
|
2015-03-14 07:30:31 +08:00
|
|
|
const FunctionIntervals &getFunctionRanges() const { return Ranges; }
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
const std::vector<PatchLocation> &getRangesAttributes() const {
|
2015-03-14 07:30:31 +08:00
|
|
|
return RangeAttributes;
|
|
|
|
}
|
2015-03-07 07:22:50 +08:00
|
|
|
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
const std::vector<std::pair<PatchLocation, int64_t>> &
|
2015-03-14 23:49:07 +08:00
|
|
|
getLocationAttributes() const {
|
|
|
|
return LocationAttributes;
|
|
|
|
}
|
|
|
|
|
2015-09-23 06:20:50 +08:00
|
|
|
void setHasInterestingContent() { HasInterestingContent = true; }
|
|
|
|
bool hasInterestingContent() { return HasInterestingContent; }
|
|
|
|
|
|
|
|
/// Mark every DIE in this unit as kept. This function also
|
|
|
|
/// marks variables as InDebugMap so that they appear in the
|
|
|
|
/// reconstructed accelerator tables.
|
|
|
|
void markEverythingAsKept();
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Compute the end offset for this unit. Must be called after the CU's DIEs
|
|
|
|
/// have been cloned. \returns the next unit offset (which is also the
|
|
|
|
/// current debug_info section size).
|
2015-03-07 07:22:50 +08:00
|
|
|
uint64_t computeNextUnitOffset();
|
2015-03-05 06:07:44 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Keep track of a forward reference to DIE \p Die in \p RefUnit by \p
|
|
|
|
/// Attr. The attribute should be fixed up later to point to the absolute
|
|
|
|
/// offset of \p Die in the debug_info section or to the canonical offset of
|
|
|
|
/// \p Ctxt if it is non-null.
|
2015-03-14 02:35:57 +08:00
|
|
|
void noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
|
2015-07-22 06:41:43 +08:00
|
|
|
DeclContext *Ctxt, PatchLocation Attr);
|
2015-03-07 07:22:53 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Apply all fixups recored by noteForwardReference().
|
2015-03-07 07:22:53 +08:00
|
|
|
void fixupForwardReferences();
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Add a function range [\p LowPC, \p HighPC) that is relocatad by applying
|
|
|
|
/// offset \p PCOffset.
|
2015-03-13 02:45:10 +08:00
|
|
|
void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset);
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Keep track of a DW_AT_range attribute that we will need to patch up later.
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
void noteRangeAttribute(const DIE &Die, PatchLocation Attr);
|
2015-03-14 07:30:31 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Keep track of a location attribute pointing to a location list in the
|
|
|
|
/// debug_loc section.
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset);
|
2015-03-14 23:49:07 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Add a name accelerator entry for \p Die with \p Name which is stored in
|
|
|
|
/// the string table at \p Offset.
|
2015-03-16 10:05:10 +08:00
|
|
|
void addNameAccelerator(const DIE *Die, const char *Name, uint32_t Offset,
|
|
|
|
bool SkipPubnamesSection = false);
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Add a type accelerator entry for \p Die with \p Name which is stored in
|
|
|
|
/// the string table at \p Offset.
|
2015-03-16 10:05:10 +08:00
|
|
|
void addTypeAccelerator(const DIE *Die, const char *Name, uint32_t Offset);
|
|
|
|
|
|
|
|
struct AccelInfo {
|
2015-06-06 04:27:07 +08:00
|
|
|
StringRef Name; ///< Name of the entry.
|
|
|
|
const DIE *Die; ///< DIE this entry describes.
|
2015-03-16 10:05:10 +08:00
|
|
|
uint32_t NameOffset; ///< Offset of Name in the string pool.
|
|
|
|
bool SkipPubSection; ///< Emit this entry only in the apple_* sections.
|
|
|
|
|
|
|
|
AccelInfo(StringRef Name, const DIE *Die, uint32_t NameOffset,
|
|
|
|
bool SkipPubSection = false)
|
|
|
|
: Name(Name), Die(Die), NameOffset(NameOffset),
|
|
|
|
SkipPubSection(SkipPubSection) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::vector<AccelInfo> &getPubnames() const { return Pubnames; }
|
|
|
|
const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; }
|
|
|
|
|
2015-07-22 06:41:43 +08:00
|
|
|
/// Get the full path for file \a FileNum in the line table
|
2016-03-18 11:48:09 +08:00
|
|
|
StringRef getResolvedPath(unsigned FileNum) {
|
2015-07-22 06:41:43 +08:00
|
|
|
if (FileNum >= ResolvedPaths.size())
|
2016-03-18 11:48:09 +08:00
|
|
|
return StringRef();
|
|
|
|
return ResolvedPaths[FileNum];
|
2015-07-22 06:41:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the fully resolved path for the line-table's file \a FileNum
|
|
|
|
/// to \a Path.
|
2016-03-18 11:48:09 +08:00
|
|
|
void setResolvedPath(unsigned FileNum, StringRef Path) {
|
2015-07-22 06:41:43 +08:00
|
|
|
if (ResolvedPaths.size() <= FileNum)
|
|
|
|
ResolvedPaths.resize(FileNum + 1);
|
|
|
|
ResolvedPaths[FileNum] = Path;
|
|
|
|
}
|
|
|
|
|
2015-01-29 06:15:14 +08:00
|
|
|
private:
|
|
|
|
DWARFUnit &OrigUnit;
|
2015-03-14 11:46:40 +08:00
|
|
|
unsigned ID;
|
2015-07-22 06:41:43 +08:00
|
|
|
std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
|
2017-04-22 10:18:00 +08:00
|
|
|
Optional<BasicDIEUnit> NewUnit;
|
2015-03-05 06:07:44 +08:00
|
|
|
|
|
|
|
uint64_t StartOffset;
|
|
|
|
uint64_t NextUnitOffset;
|
2015-03-07 07:22:53 +08:00
|
|
|
|
2015-03-14 02:35:54 +08:00
|
|
|
uint64_t LowPc;
|
|
|
|
uint64_t HighPc;
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// A list of attributes to fixup with the absolute offset of
|
2015-03-07 07:22:53 +08:00
|
|
|
/// a DIE in the debug_info section.
|
|
|
|
///
|
|
|
|
/// The offsets for the attributes in this array couldn't be set while
|
2015-03-14 02:35:57 +08:00
|
|
|
/// cloning because for cross-cu forward refences the target DIE's
|
|
|
|
/// offset isn't known you emit the reference attribute.
|
2015-07-22 06:41:43 +08:00
|
|
|
std::vector<std::tuple<DIE *, const CompileUnit *, DeclContext *,
|
|
|
|
PatchLocation>> ForwardDIEReferences;
|
2015-03-13 02:45:10 +08:00
|
|
|
|
2015-03-14 07:30:31 +08:00
|
|
|
FunctionIntervals::Allocator RangeAlloc;
|
2017-07-22 00:51:17 +08:00
|
|
|
/// The ranges in that interval map are the PC ranges for
|
2015-03-13 02:45:10 +08:00
|
|
|
/// functions in this unit, associated with the PC offset to apply
|
|
|
|
/// to the addresses to get the linked address.
|
2015-03-14 07:30:31 +08:00
|
|
|
FunctionIntervals Ranges;
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// DW_AT_ranges attributes to patch after we have gathered
|
2015-03-14 07:30:31 +08:00
|
|
|
/// all the unit's function addresses.
|
|
|
|
/// @{
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
std::vector<PatchLocation> RangeAttributes;
|
|
|
|
Optional<PatchLocation> UnitRangeAttribute;
|
2015-03-14 07:30:31 +08:00
|
|
|
/// @}
|
2015-03-14 23:49:07 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Location attributes that need to be transferred from the
|
2015-03-14 23:49:07 +08:00
|
|
|
/// original debug_loc section to the liked one. They are stored
|
|
|
|
/// along with the PC offset that is to be applied to their
|
|
|
|
/// function's address.
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
std::vector<std::pair<PatchLocation, int64_t>> LocationAttributes;
|
2015-03-16 10:05:10 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Accelerator entries for the unit, both for the pub*
|
2015-03-16 10:05:10 +08:00
|
|
|
/// sections and the apple* ones.
|
|
|
|
/// @{
|
|
|
|
std::vector<AccelInfo> Pubnames;
|
|
|
|
std::vector<AccelInfo> Pubtypes;
|
|
|
|
/// @}
|
2015-07-22 06:41:43 +08:00
|
|
|
|
|
|
|
/// Cached resolved paths from the line table.
|
2016-03-18 11:48:09 +08:00
|
|
|
/// Note, the StringRefs here point in to the intern (uniquing) string pool.
|
|
|
|
/// This means that a StringRef returned here doesn't need to then be uniqued
|
|
|
|
/// for the purposes of getting a unique address for each string.
|
|
|
|
std::vector<StringRef> ResolvedPaths;
|
2015-07-22 06:41:43 +08:00
|
|
|
|
|
|
|
/// Is this unit subject to the ODR rule?
|
|
|
|
bool HasODR;
|
2015-09-23 06:20:50 +08:00
|
|
|
/// Did a DIE actually contain a valid reloc?
|
|
|
|
bool HasInterestingContent;
|
2015-09-24 01:35:52 +08:00
|
|
|
/// If this is a Clang module, this holds the module's name.
|
|
|
|
std::string ClangModuleName;
|
2015-01-29 06:15:14 +08:00
|
|
|
};
|
|
|
|
|
2015-09-23 06:20:50 +08:00
|
|
|
void CompileUnit::markEverythingAsKept() {
|
|
|
|
for (auto &I : Info)
|
2015-09-24 01:35:52 +08:00
|
|
|
// Mark everything that wasn't explicity marked for pruning.
|
|
|
|
I.Keep = !I.Prune;
|
2015-09-23 06:20:50 +08:00
|
|
|
}
|
|
|
|
|
2015-03-07 07:22:50 +08:00
|
|
|
uint64_t CompileUnit::computeNextUnitOffset() {
|
2015-03-05 06:07:44 +08:00
|
|
|
NextUnitOffset = StartOffset + 11 /* Header size */;
|
|
|
|
// The root DIE might be null, meaning that the Unit had nothing to
|
|
|
|
// contribute to the linked output. In that case, we will emit the
|
|
|
|
// unit header without any actual DIE.
|
2017-02-10 03:41:55 +08:00
|
|
|
if (NewUnit)
|
|
|
|
NextUnitOffset += NewUnit->getUnitDie().getSize();
|
2015-03-05 06:07:44 +08:00
|
|
|
return NextUnitOffset;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Keep track of a forward cross-cu reference from this unit
|
2015-03-14 02:35:57 +08:00
|
|
|
/// to \p Die that lives in \p RefUnit.
|
|
|
|
void CompileUnit::noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
|
2015-07-22 06:41:43 +08:00
|
|
|
DeclContext *Ctxt, PatchLocation Attr) {
|
|
|
|
ForwardDIEReferences.emplace_back(Die, RefUnit, Ctxt, Attr);
|
2015-03-07 07:22:53 +08:00
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Apply all fixups recorded by noteForwardReference().
|
2015-03-07 07:22:53 +08:00
|
|
|
void CompileUnit::fixupForwardReferences() {
|
2015-03-14 02:35:57 +08:00
|
|
|
for (const auto &Ref : ForwardDIEReferences) {
|
|
|
|
DIE *RefDie;
|
|
|
|
const CompileUnit *RefUnit;
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
PatchLocation Attr;
|
2015-07-22 06:41:43 +08:00
|
|
|
DeclContext *Ctxt;
|
|
|
|
std::tie(RefDie, RefUnit, Ctxt, Attr) = Ref;
|
|
|
|
if (Ctxt && Ctxt->getCanonicalDIEOffset())
|
|
|
|
Attr.set(Ctxt->getCanonicalDIEOffset());
|
|
|
|
else
|
|
|
|
Attr.set(RefDie->getOffset() + RefUnit->getStartOffset());
|
2015-03-14 02:35:57 +08:00
|
|
|
}
|
2015-03-07 07:22:53 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:35:54 +08:00
|
|
|
void CompileUnit::addFunctionRange(uint64_t FuncLowPc, uint64_t FuncHighPc,
|
|
|
|
int64_t PcOffset) {
|
|
|
|
Ranges.insert(FuncLowPc, FuncHighPc, PcOffset);
|
|
|
|
this->LowPc = std::min(LowPc, FuncLowPc + PcOffset);
|
|
|
|
this->HighPc = std::max(HighPc, FuncHighPc + PcOffset);
|
2015-03-13 02:45:10 +08:00
|
|
|
}
|
|
|
|
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
void CompileUnit::noteRangeAttribute(const DIE &Die, PatchLocation Attr) {
|
2015-03-14 07:30:31 +08:00
|
|
|
if (Die.getTag() != dwarf::DW_TAG_compile_unit)
|
|
|
|
RangeAttributes.push_back(Attr);
|
|
|
|
else
|
|
|
|
UnitRangeAttribute = Attr;
|
|
|
|
}
|
|
|
|
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
void CompileUnit::noteLocationAttribute(PatchLocation Attr, int64_t PcOffset) {
|
2015-03-14 23:49:07 +08:00
|
|
|
LocationAttributes.emplace_back(Attr, PcOffset);
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Add a name accelerator entry for \p Die with \p Name
|
2015-03-16 10:05:10 +08:00
|
|
|
/// which is stored in the string table at \p Offset.
|
|
|
|
void CompileUnit::addNameAccelerator(const DIE *Die, const char *Name,
|
|
|
|
uint32_t Offset, bool SkipPubSection) {
|
|
|
|
Pubnames.emplace_back(Name, Die, Offset, SkipPubSection);
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Add a type accelerator entry for \p Die with \p Name
|
2015-03-16 10:05:10 +08:00
|
|
|
/// which is stored in the string table at \p Offset.
|
|
|
|
void CompileUnit::addTypeAccelerator(const DIE *Die, const char *Name,
|
|
|
|
uint32_t Offset) {
|
|
|
|
Pubtypes.emplace_back(Name, Die, Offset, false);
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// The Dwarf streaming logic
|
2015-02-28 08:29:11 +08:00
|
|
|
///
|
|
|
|
/// All interactions with the MC layer that is used to build the debug
|
|
|
|
/// information binary representation are handled in this class.
|
|
|
|
class DwarfStreamer {
|
|
|
|
/// \defgroup MCObjects MC layer objects constructed by the streamer
|
|
|
|
/// @{
|
|
|
|
std::unique_ptr<MCRegisterInfo> MRI;
|
|
|
|
std::unique_ptr<MCAsmInfo> MAI;
|
|
|
|
std::unique_ptr<MCObjectFileInfo> MOFI;
|
|
|
|
std::unique_ptr<MCContext> MC;
|
|
|
|
MCAsmBackend *MAB; // Owned by MCStreamer
|
|
|
|
std::unique_ptr<MCInstrInfo> MII;
|
|
|
|
std::unique_ptr<MCSubtargetInfo> MSTI;
|
|
|
|
MCCodeEmitter *MCE; // Owned by MCStreamer
|
|
|
|
MCStreamer *MS; // Owned by AsmPrinter
|
|
|
|
std::unique_ptr<TargetMachine> TM;
|
|
|
|
std::unique_ptr<AsmPrinter> Asm;
|
|
|
|
/// @}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// The file we stream the linked Dwarf to.
|
2015-02-28 08:29:11 +08:00
|
|
|
std::unique_ptr<raw_fd_ostream> OutFile;
|
|
|
|
|
2015-03-14 07:30:31 +08:00
|
|
|
uint32_t RangesSectionSize;
|
2015-03-14 23:49:07 +08:00
|
|
|
uint32_t LocSectionSize;
|
2015-03-16 04:45:43 +08:00
|
|
|
uint32_t LineSectionSize;
|
2015-06-06 07:06:11 +08:00
|
|
|
uint32_t FrameSectionSize;
|
2015-03-14 07:30:31 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit the pubnames or pubtypes section contribution for \p
|
2015-03-16 10:05:10 +08:00
|
|
|
/// Unit into \p Sec. The data is provided in \p Names.
|
2015-05-22 03:20:38 +08:00
|
|
|
void emitPubSectionForUnit(MCSection *Sec, StringRef Name,
|
2015-03-16 10:05:10 +08:00
|
|
|
const CompileUnit &Unit,
|
|
|
|
const std::vector<CompileUnit::AccelInfo> &Names);
|
|
|
|
|
2015-02-28 08:29:11 +08:00
|
|
|
public:
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Actually create the streamer and the ouptut file.
|
2015-02-28 08:29:11 +08:00
|
|
|
///
|
|
|
|
/// This could be done directly in the constructor, but it feels
|
|
|
|
/// more natural to handle errors through return value.
|
|
|
|
bool init(Triple TheTriple, StringRef OutputFilename);
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Dump the file to the disk.
|
2015-09-03 00:49:13 +08:00
|
|
|
bool finish(const DebugMap &);
|
2015-03-05 06:07:44 +08:00
|
|
|
|
|
|
|
AsmPrinter &getAsmPrinter() const { return *Asm; }
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Set the current output section to debug_info and change
|
2015-03-05 06:07:44 +08:00
|
|
|
/// the MC Dwarf version to \p DwarfVersion.
|
|
|
|
void switchToDebugInfoSection(unsigned DwarfVersion);
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit the compilation unit header for \p Unit in the
|
2015-03-05 06:07:44 +08:00
|
|
|
/// debug_info section.
|
|
|
|
///
|
|
|
|
/// As a side effect, this also switches the current Dwarf version
|
|
|
|
/// of the MC layer to the one of U.getOrigUnit().
|
|
|
|
void emitCompileUnitHeader(CompileUnit &Unit);
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Recursively emit the DIE tree rooted at \p Die.
|
2015-03-05 06:07:44 +08:00
|
|
|
void emitDIE(DIE &Die);
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit the abbreviation table \p Abbrevs to the debug_abbrev section.
|
2017-04-21 03:16:51 +08:00
|
|
|
void emitAbbrevs(const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
|
|
|
|
unsigned DwarfVersion);
|
2015-03-07 01:56:30 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit the string table described by \p Pool.
|
2015-03-07 01:56:30 +08:00
|
|
|
void emitStrings(const NonRelocatableStringpool &Pool);
|
2015-03-14 07:30:31 +08:00
|
|
|
|
2017-10-11 21:51:30 +08:00
|
|
|
/// Emit the swift_ast section stored in \p Buffers.
|
|
|
|
void emitSwiftAST(const std::vector<MemoryBufferRef> &Buffers);
|
2017-10-06 22:49:20 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit debug_ranges for \p FuncRange by translating the
|
2015-03-14 07:30:31 +08:00
|
|
|
/// original \p Entries.
|
|
|
|
void emitRangesEntries(
|
|
|
|
int64_t UnitPcOffset, uint64_t OrigLowPc,
|
2016-06-09 03:09:22 +08:00
|
|
|
const FunctionIntervals::const_iterator &FuncRange,
|
2015-03-14 07:30:31 +08:00
|
|
|
const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
|
|
|
|
unsigned AddressSize);
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit debug_aranges entries for \p Unit and if \p DoRangesSection is true,
|
|
|
|
/// also emit the debug_ranges entries for the DW_TAG_compile_unit's
|
|
|
|
/// DW_AT_ranges attribute.
|
2015-03-14 11:46:51 +08:00
|
|
|
void emitUnitRangesEntries(CompileUnit &Unit, bool DoRangesSection);
|
2015-03-14 07:30:31 +08:00
|
|
|
|
|
|
|
uint32_t getRangesSectionSize() const { return RangesSectionSize; }
|
2015-03-14 23:49:07 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit the debug_loc contribution for \p Unit by copying the entries from \p
|
|
|
|
/// Dwarf and offseting them. Update the location attributes to point to the
|
|
|
|
/// new entries.
|
2015-03-14 23:49:07 +08:00
|
|
|
void emitLocationsForUnit(const CompileUnit &Unit, DWARFContext &Dwarf);
|
2015-03-16 04:45:43 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit the line table described in \p Rows into the debug_line section.
|
2015-08-07 23:14:13 +08:00
|
|
|
void emitLineTableForUnit(MCDwarfLineTableParams Params,
|
|
|
|
StringRef PrologueBytes, unsigned MinInstLength,
|
2015-03-16 04:45:43 +08:00
|
|
|
std::vector<DWARFDebugLine::Row> &Rows,
|
|
|
|
unsigned AdddressSize);
|
|
|
|
|
|
|
|
uint32_t getLineSectionSize() const { return LineSectionSize; }
|
2015-03-16 10:05:10 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit the .debug_pubnames contribution for \p Unit.
|
2015-03-16 10:05:10 +08:00
|
|
|
void emitPubNamesForUnit(const CompileUnit &Unit);
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit the .debug_pubtypes contribution for \p Unit.
|
2015-03-16 10:05:10 +08:00
|
|
|
void emitPubTypesForUnit(const CompileUnit &Unit);
|
2015-06-06 07:06:11 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit a CIE.
|
2015-06-06 07:06:11 +08:00
|
|
|
void emitCIE(StringRef CIEBytes);
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit an FDE with data \p Bytes.
|
2015-06-06 07:06:11 +08:00
|
|
|
void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint32_t Address,
|
|
|
|
StringRef Bytes);
|
|
|
|
|
|
|
|
uint32_t getFrameSectionSize() const { return FrameSectionSize; }
|
2015-02-28 08:29:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool DwarfStreamer::init(Triple TheTriple, StringRef OutputFilename) {
|
|
|
|
std::string ErrorStr;
|
|
|
|
std::string TripleName;
|
|
|
|
StringRef Context = "dwarf streamer init";
|
|
|
|
|
|
|
|
// Get the target.
|
|
|
|
const Target *TheTarget =
|
|
|
|
TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
|
|
|
|
if (!TheTarget)
|
|
|
|
return error(ErrorStr, Context);
|
|
|
|
TripleName = TheTriple.getTriple();
|
|
|
|
|
|
|
|
// Create all the MC Objects.
|
|
|
|
MRI.reset(TheTarget->createMCRegInfo(TripleName));
|
|
|
|
if (!MRI)
|
|
|
|
return error(Twine("no register info for target ") + TripleName, Context);
|
|
|
|
|
|
|
|
MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName));
|
|
|
|
if (!MAI)
|
|
|
|
return error("no asm info for target " + TripleName, Context);
|
|
|
|
|
|
|
|
MOFI.reset(new MCObjectFileInfo);
|
|
|
|
MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get()));
|
2017-08-03 04:32:26 +08:00
|
|
|
MOFI->InitMCObjectFileInfo(TheTriple, /*PIC*/ false, *MC);
|
2015-02-28 08:29:11 +08:00
|
|
|
|
2016-07-26 01:18:28 +08:00
|
|
|
MCTargetOptions Options;
|
|
|
|
MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "", Options);
|
2015-02-28 08:29:11 +08:00
|
|
|
if (!MAB)
|
|
|
|
return error("no asm backend for target " + TripleName, Context);
|
|
|
|
|
|
|
|
MII.reset(TheTarget->createMCInstrInfo());
|
|
|
|
if (!MII)
|
|
|
|
return error("no instr info info for target " + TripleName, Context);
|
|
|
|
|
|
|
|
MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
|
|
|
|
if (!MSTI)
|
|
|
|
return error("no subtarget info for target " + TripleName, Context);
|
|
|
|
|
2015-03-11 06:03:14 +08:00
|
|
|
MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC);
|
2015-02-28 08:29:11 +08:00
|
|
|
if (!MCE)
|
|
|
|
return error("no code emitter for target " + TripleName, Context);
|
|
|
|
|
|
|
|
// Create the output file.
|
|
|
|
std::error_code EC;
|
2015-03-05 06:07:44 +08:00
|
|
|
OutFile =
|
|
|
|
llvm::make_unique<raw_fd_ostream>(OutputFilename, EC, sys::fs::F_None);
|
2015-02-28 08:29:11 +08:00
|
|
|
if (EC)
|
|
|
|
return error(Twine(OutputFilename) + ": " + EC.message(), Context);
|
|
|
|
|
2015-12-22 06:09:27 +08:00
|
|
|
MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
|
|
|
|
MS = TheTarget->createMCObjectStreamer(
|
2017-10-11 09:57:21 +08:00
|
|
|
TheTriple, *MC, std::unique_ptr<MCAsmBackend>(MAB), *OutFile, MCE, *MSTI,
|
|
|
|
MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
|
2015-12-22 06:09:27 +08:00
|
|
|
/*DWARFMustBeAtTheEnd*/ false);
|
2015-02-28 08:29:11 +08:00
|
|
|
if (!MS)
|
|
|
|
return error("no object streamer for target " + TripleName, Context);
|
|
|
|
|
|
|
|
// Finally create the AsmPrinter we'll use to emit the DIEs.
|
2016-05-19 06:04:49 +08:00
|
|
|
TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions(),
|
|
|
|
None));
|
2015-02-28 08:29:11 +08:00
|
|
|
if (!TM)
|
|
|
|
return error("no target machine for target " + TripleName, Context);
|
|
|
|
|
|
|
|
Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
|
|
|
|
if (!Asm)
|
|
|
|
return error("no asm printer for target " + TripleName, Context);
|
|
|
|
|
2015-03-14 07:30:31 +08:00
|
|
|
RangesSectionSize = 0;
|
2015-03-14 23:49:07 +08:00
|
|
|
LocSectionSize = 0;
|
2015-03-16 04:45:43 +08:00
|
|
|
LineSectionSize = 0;
|
2015-06-06 07:06:11 +08:00
|
|
|
FrameSectionSize = 0;
|
2015-03-14 07:30:31 +08:00
|
|
|
|
2015-02-28 08:29:11 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-03 00:49:13 +08:00
|
|
|
bool DwarfStreamer::finish(const DebugMap &DM) {
|
|
|
|
if (DM.getTriple().isOSDarwin() && !DM.getBinaryPath().empty())
|
|
|
|
return MachOUtils::generateDsymCompanion(DM, *MS, *OutFile);
|
|
|
|
|
2015-02-28 08:29:11 +08:00
|
|
|
MS->Finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Set the current output section to debug_info and change
|
2015-03-05 06:07:44 +08:00
|
|
|
/// the MC Dwarf version to \p DwarfVersion.
|
|
|
|
void DwarfStreamer::switchToDebugInfoSection(unsigned DwarfVersion) {
|
|
|
|
MS->SwitchSection(MOFI->getDwarfInfoSection());
|
|
|
|
MC->setDwarfVersion(DwarfVersion);
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit the compilation unit header for \p Unit in the debug_info section.
|
2015-03-05 06:07:44 +08:00
|
|
|
///
|
|
|
|
/// A Dwarf scetion header is encoded as:
|
|
|
|
/// uint32_t Unit length (omiting this field)
|
|
|
|
/// uint16_t Version
|
|
|
|
/// uint32_t Abbreviation table offset
|
|
|
|
/// uint8_t Address size
|
|
|
|
///
|
|
|
|
/// Leading to a total of 11 bytes.
|
|
|
|
void DwarfStreamer::emitCompileUnitHeader(CompileUnit &Unit) {
|
|
|
|
unsigned Version = Unit.getOrigUnit().getVersion();
|
|
|
|
switchToDebugInfoSection(Version);
|
|
|
|
|
|
|
|
// Emit size of content not including length itself. The size has
|
|
|
|
// already been computed in CompileUnit::computeOffsets(). Substract
|
|
|
|
// 4 to that size to account for the length field.
|
|
|
|
Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset() - 4);
|
|
|
|
Asm->EmitInt16(Version);
|
|
|
|
// We share one abbreviations table across all units so it's always at the
|
|
|
|
// start of the section.
|
|
|
|
Asm->EmitInt32(0);
|
|
|
|
Asm->EmitInt8(Unit.getOrigUnit().getAddressByteSize());
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit the \p Abbrevs array as the shared abbreviation table
|
2015-03-05 06:07:44 +08:00
|
|
|
/// for the linked Dwarf file.
|
2015-11-18 08:34:10 +08:00
|
|
|
void DwarfStreamer::emitAbbrevs(
|
2017-04-21 03:16:51 +08:00
|
|
|
const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
|
|
|
|
unsigned DwarfVersion) {
|
2015-03-05 06:07:44 +08:00
|
|
|
MS->SwitchSection(MOFI->getDwarfAbbrevSection());
|
2017-04-21 03:16:51 +08:00
|
|
|
MC->setDwarfVersion(DwarfVersion);
|
2015-03-05 06:07:44 +08:00
|
|
|
Asm->emitDwarfAbbrevs(Abbrevs);
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Recursively emit the DIE tree rooted at \p Die.
|
2015-03-05 06:07:44 +08:00
|
|
|
void DwarfStreamer::emitDIE(DIE &Die) {
|
|
|
|
MS->SwitchSection(MOFI->getDwarfInfoSection());
|
|
|
|
Asm->emitDwarfDIE(Die);
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit the debug_str section stored in \p Pool.
|
2015-03-07 01:56:30 +08:00
|
|
|
void DwarfStreamer::emitStrings(const NonRelocatableStringpool &Pool) {
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->SwitchSection(MOFI->getDwarfStrSection());
|
2015-03-07 01:56:30 +08:00
|
|
|
for (auto *Entry = Pool.getFirstEntry(); Entry;
|
|
|
|
Entry = Pool.getNextEntry(Entry))
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitBytes(
|
2015-03-07 01:56:30 +08:00
|
|
|
StringRef(Entry->getKey().data(), Entry->getKey().size() + 1));
|
|
|
|
}
|
|
|
|
|
2017-10-06 22:49:20 +08:00
|
|
|
/// Emit the swift_ast section stored in \p Buffers.
|
2017-10-11 21:51:30 +08:00
|
|
|
void DwarfStreamer::emitSwiftAST(const std::vector<MemoryBufferRef> &Buffers) {
|
2017-10-06 22:49:20 +08:00
|
|
|
MCSection *SwiftASTSection = MOFI->getDwarfSwiftASTSection();
|
|
|
|
SwiftASTSection->setAlignment(1 << 5);
|
|
|
|
MS->SwitchSection(SwiftASTSection);
|
2017-10-11 21:51:30 +08:00
|
|
|
for (auto Buf : Buffers)
|
|
|
|
MS->EmitBytes(Buf.getBuffer());
|
2017-10-06 22:49:20 +08:00
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit the debug_range section contents for \p FuncRange by
|
2015-03-14 07:30:31 +08:00
|
|
|
/// translating the original \p Entries. The debug_range section
|
|
|
|
/// format is totally trivial, consisting just of pairs of address
|
|
|
|
/// sized addresses describing the ranges.
|
|
|
|
void DwarfStreamer::emitRangesEntries(
|
|
|
|
int64_t UnitPcOffset, uint64_t OrigLowPc,
|
2016-06-09 03:09:22 +08:00
|
|
|
const FunctionIntervals::const_iterator &FuncRange,
|
2015-03-14 07:30:31 +08:00
|
|
|
const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
|
|
|
|
unsigned AddressSize) {
|
|
|
|
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
|
|
|
|
|
|
|
|
// Offset each range by the right amount.
|
2015-08-31 13:09:32 +08:00
|
|
|
int64_t PcOffset = Entries.empty() ? 0 : FuncRange.value() + UnitPcOffset;
|
2015-03-14 07:30:31 +08:00
|
|
|
for (const auto &Range : Entries) {
|
|
|
|
if (Range.isBaseAddressSelectionEntry(AddressSize)) {
|
|
|
|
warn("unsupported base address selection operation",
|
|
|
|
"emitting debug_ranges");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Do not emit empty ranges.
|
|
|
|
if (Range.StartAddress == Range.EndAddress)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// All range entries should lie in the function range.
|
|
|
|
if (!(Range.StartAddress + OrigLowPc >= FuncRange.start() &&
|
|
|
|
Range.EndAddress + OrigLowPc <= FuncRange.stop()))
|
|
|
|
warn("inconsistent range data.", "emitting debug_ranges");
|
|
|
|
MS->EmitIntValue(Range.StartAddress + PcOffset, AddressSize);
|
|
|
|
MS->EmitIntValue(Range.EndAddress + PcOffset, AddressSize);
|
|
|
|
RangesSectionSize += 2 * AddressSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the terminator entry.
|
|
|
|
MS->EmitIntValue(0, AddressSize);
|
|
|
|
MS->EmitIntValue(0, AddressSize);
|
|
|
|
RangesSectionSize += 2 * AddressSize;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit the debug_aranges contribution of a unit and
|
2015-03-14 11:46:51 +08:00
|
|
|
/// if \p DoDebugRanges is true the debug_range contents for a
|
|
|
|
/// compile_unit level DW_AT_ranges attribute (Which are basically the
|
|
|
|
/// same thing with a different base address).
|
|
|
|
/// Just aggregate all the ranges gathered inside that unit.
|
|
|
|
void DwarfStreamer::emitUnitRangesEntries(CompileUnit &Unit,
|
|
|
|
bool DoDebugRanges) {
|
2015-03-14 07:30:31 +08:00
|
|
|
unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
|
|
|
|
// Gather the ranges in a vector, so that we can simplify them. The
|
|
|
|
// IntervalMap will have coalesced the non-linked ranges, but here
|
|
|
|
// we want to coalesce the linked addresses.
|
|
|
|
std::vector<std::pair<uint64_t, uint64_t>> Ranges;
|
|
|
|
const auto &FunctionRanges = Unit.getFunctionRanges();
|
|
|
|
for (auto Range = FunctionRanges.begin(), End = FunctionRanges.end();
|
|
|
|
Range != End; ++Range)
|
2015-03-14 11:46:51 +08:00
|
|
|
Ranges.push_back(std::make_pair(Range.start() + Range.value(),
|
|
|
|
Range.stop() + Range.value()));
|
2015-03-14 07:30:31 +08:00
|
|
|
|
|
|
|
// The object addresses where sorted, but again, the linked
|
|
|
|
// addresses might end up in a different order.
|
|
|
|
std::sort(Ranges.begin(), Ranges.end());
|
|
|
|
|
2015-03-14 11:46:51 +08:00
|
|
|
if (!Ranges.empty()) {
|
|
|
|
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfARangesSection());
|
|
|
|
|
2015-03-18 04:07:06 +08:00
|
|
|
MCSymbol *BeginLabel = Asm->createTempSymbol("Barange");
|
|
|
|
MCSymbol *EndLabel = Asm->createTempSymbol("Earange");
|
2015-03-14 11:46:51 +08:00
|
|
|
|
|
|
|
unsigned HeaderSize =
|
|
|
|
sizeof(int32_t) + // Size of contents (w/o this field
|
|
|
|
sizeof(int16_t) + // DWARF ARange version number
|
|
|
|
sizeof(int32_t) + // Offset of CU in the .debug_info section
|
|
|
|
sizeof(int8_t) + // Pointer Size (in bytes)
|
|
|
|
sizeof(int8_t); // Segment Size (in bytes)
|
|
|
|
|
|
|
|
unsigned TupleSize = AddressSize * 2;
|
|
|
|
unsigned Padding = OffsetToAlignment(HeaderSize, TupleSize);
|
|
|
|
|
|
|
|
Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); // Arange length
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitLabel(BeginLabel);
|
2015-03-14 11:46:51 +08:00
|
|
|
Asm->EmitInt16(dwarf::DW_ARANGES_VERSION); // Version number
|
|
|
|
Asm->EmitInt32(Unit.getStartOffset()); // Corresponding unit's offset
|
|
|
|
Asm->EmitInt8(AddressSize); // Address size
|
|
|
|
Asm->EmitInt8(0); // Segment size
|
|
|
|
|
2016-06-01 09:59:58 +08:00
|
|
|
Asm->OutStreamer->emitFill(Padding, 0x0);
|
2015-03-14 11:46:51 +08:00
|
|
|
|
|
|
|
for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End;
|
|
|
|
++Range) {
|
|
|
|
uint64_t RangeStart = Range->first;
|
|
|
|
MS->EmitIntValue(RangeStart, AddressSize);
|
|
|
|
while ((Range + 1) != End && Range->second == (Range + 1)->first)
|
|
|
|
++Range;
|
|
|
|
MS->EmitIntValue(Range->second - RangeStart, AddressSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit terminator
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitIntValue(0, AddressSize);
|
|
|
|
Asm->OutStreamer->EmitIntValue(0, AddressSize);
|
|
|
|
Asm->OutStreamer->EmitLabel(EndLabel);
|
2015-03-14 11:46:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!DoDebugRanges)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
|
|
|
|
// Offset each range by the right amount.
|
|
|
|
int64_t PcOffset = -Unit.getLowPc();
|
2015-03-14 07:30:31 +08:00
|
|
|
// Emit coalesced ranges.
|
|
|
|
for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End; ++Range) {
|
2015-03-14 11:46:51 +08:00
|
|
|
MS->EmitIntValue(Range->first + PcOffset, AddressSize);
|
2015-03-14 07:30:31 +08:00
|
|
|
while (Range + 1 != End && Range->second == (Range + 1)->first)
|
|
|
|
++Range;
|
2015-03-14 11:46:51 +08:00
|
|
|
MS->EmitIntValue(Range->second + PcOffset, AddressSize);
|
2015-03-14 07:30:31 +08:00
|
|
|
RangesSectionSize += 2 * AddressSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the terminator entry.
|
|
|
|
MS->EmitIntValue(0, AddressSize);
|
|
|
|
MS->EmitIntValue(0, AddressSize);
|
|
|
|
RangesSectionSize += 2 * AddressSize;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit location lists for \p Unit and update attribtues to
|
2015-03-14 23:49:07 +08:00
|
|
|
/// point to the new entries.
|
|
|
|
void DwarfStreamer::emitLocationsForUnit(const CompileUnit &Unit,
|
|
|
|
DWARFContext &Dwarf) {
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
const auto &Attributes = Unit.getLocationAttributes();
|
2015-03-14 23:49:07 +08:00
|
|
|
|
|
|
|
if (Attributes.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLocSection());
|
|
|
|
|
|
|
|
unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
|
2017-07-20 06:27:28 +08:00
|
|
|
const DWARFSection &InputSec = Dwarf.getDWARFObj().getLocSection();
|
2015-03-14 23:49:07 +08:00
|
|
|
DataExtractor Data(InputSec.Data, Dwarf.isLittleEndian(), AddressSize);
|
|
|
|
DWARFUnit &OrigUnit = Unit.getOrigUnit();
|
2016-12-14 02:25:19 +08:00
|
|
|
auto OrigUnitDie = OrigUnit.getUnitDIE(false);
|
2015-03-14 23:49:07 +08:00
|
|
|
int64_t UnitPcOffset = 0;
|
2017-01-14 05:08:18 +08:00
|
|
|
if (auto OrigLowPc = dwarf::toAddress(OrigUnitDie.find(dwarf::DW_AT_low_pc)))
|
2016-12-15 06:38:08 +08:00
|
|
|
UnitPcOffset = int64_t(*OrigLowPc) - Unit.getLowPc();
|
2015-03-14 23:49:07 +08:00
|
|
|
|
|
|
|
for (const auto &Attr : Attributes) {
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
uint32_t Offset = Attr.first.get();
|
|
|
|
Attr.first.set(LocSectionSize);
|
2015-03-14 23:49:07 +08:00
|
|
|
// This is the quantity to add to the old location address to get
|
|
|
|
// the correct address for the new one.
|
|
|
|
int64_t LocPcOffset = Attr.second + UnitPcOffset;
|
|
|
|
while (Data.isValidOffset(Offset)) {
|
|
|
|
uint64_t Low = Data.getUnsigned(&Offset, AddressSize);
|
|
|
|
uint64_t High = Data.getUnsigned(&Offset, AddressSize);
|
|
|
|
LocSectionSize += 2 * AddressSize;
|
|
|
|
if (Low == 0 && High == 0) {
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitIntValue(0, AddressSize);
|
|
|
|
Asm->OutStreamer->EmitIntValue(0, AddressSize);
|
2015-03-14 23:49:07 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitIntValue(Low + LocPcOffset, AddressSize);
|
|
|
|
Asm->OutStreamer->EmitIntValue(High + LocPcOffset, AddressSize);
|
2015-03-14 23:49:07 +08:00
|
|
|
uint64_t Length = Data.getU16(&Offset);
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitIntValue(Length, 2);
|
2015-03-14 23:49:07 +08:00
|
|
|
// Just copy the bytes over.
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitBytes(
|
2015-03-14 23:49:07 +08:00
|
|
|
StringRef(InputSec.Data.substr(Offset, Length)));
|
|
|
|
Offset += Length;
|
|
|
|
LocSectionSize += Length + 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-07 23:14:13 +08:00
|
|
|
void DwarfStreamer::emitLineTableForUnit(MCDwarfLineTableParams Params,
|
|
|
|
StringRef PrologueBytes,
|
2015-03-16 04:45:43 +08:00
|
|
|
unsigned MinInstLength,
|
|
|
|
std::vector<DWARFDebugLine::Row> &Rows,
|
|
|
|
unsigned PointerSize) {
|
|
|
|
// Switch to the section where the table will be emitted into.
|
|
|
|
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection());
|
2015-05-19 02:43:14 +08:00
|
|
|
MCSymbol *LineStartSym = MC->createTempSymbol();
|
|
|
|
MCSymbol *LineEndSym = MC->createTempSymbol();
|
2015-03-16 04:45:43 +08:00
|
|
|
|
|
|
|
// The first 4 bytes is the total length of the information for this
|
|
|
|
// compilation unit (not including these 4 bytes for the length).
|
|
|
|
Asm->EmitLabelDifference(LineEndSym, LineStartSym, 4);
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitLabel(LineStartSym);
|
2015-03-16 04:45:43 +08:00
|
|
|
// Copy Prologue.
|
|
|
|
MS->EmitBytes(PrologueBytes);
|
|
|
|
LineSectionSize += PrologueBytes.size() + 4;
|
|
|
|
|
2015-03-16 06:20:28 +08:00
|
|
|
SmallString<128> EncodingBuffer;
|
2015-03-16 04:45:43 +08:00
|
|
|
raw_svector_ostream EncodingOS(EncodingBuffer);
|
|
|
|
|
|
|
|
if (Rows.empty()) {
|
|
|
|
// We only have the dummy entry, dsymutil emits an entry with a 0
|
|
|
|
// address in that case.
|
2015-08-07 23:14:13 +08:00
|
|
|
MCDwarfLineAddr::Encode(*MC, Params, INT64_MAX, 0, EncodingOS);
|
2015-03-16 04:45:43 +08:00
|
|
|
MS->EmitBytes(EncodingOS.str());
|
|
|
|
LineSectionSize += EncodingBuffer.size();
|
|
|
|
MS->EmitLabel(LineEndSym);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Line table state machine fields
|
|
|
|
unsigned FileNum = 1;
|
|
|
|
unsigned LastLine = 1;
|
|
|
|
unsigned Column = 0;
|
|
|
|
unsigned IsStatement = 1;
|
|
|
|
unsigned Isa = 0;
|
|
|
|
uint64_t Address = -1ULL;
|
|
|
|
|
|
|
|
unsigned RowsSinceLastSequence = 0;
|
|
|
|
|
|
|
|
for (unsigned Idx = 0; Idx < Rows.size(); ++Idx) {
|
|
|
|
auto &Row = Rows[Idx];
|
|
|
|
|
|
|
|
int64_t AddressDelta;
|
|
|
|
if (Address == -1ULL) {
|
|
|
|
MS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
|
|
|
|
MS->EmitULEB128IntValue(PointerSize + 1);
|
|
|
|
MS->EmitIntValue(dwarf::DW_LNE_set_address, 1);
|
|
|
|
MS->EmitIntValue(Row.Address, PointerSize);
|
|
|
|
LineSectionSize += 2 + PointerSize + getULEB128Size(PointerSize + 1);
|
|
|
|
AddressDelta = 0;
|
|
|
|
} else {
|
|
|
|
AddressDelta = (Row.Address - Address) / MinInstLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: code copied and transfromed from
|
|
|
|
// MCDwarf.cpp::EmitDwarfLineTable. We should find a way to share
|
|
|
|
// this code, but the current compatibility requirement with
|
|
|
|
// classic dsymutil makes it hard. Revisit that once this
|
|
|
|
// requirement is dropped.
|
|
|
|
|
|
|
|
if (FileNum != Row.File) {
|
|
|
|
FileNum = Row.File;
|
|
|
|
MS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
|
|
|
|
MS->EmitULEB128IntValue(FileNum);
|
|
|
|
LineSectionSize += 1 + getULEB128Size(FileNum);
|
|
|
|
}
|
|
|
|
if (Column != Row.Column) {
|
|
|
|
Column = Row.Column;
|
|
|
|
MS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
|
|
|
|
MS->EmitULEB128IntValue(Column);
|
|
|
|
LineSectionSize += 1 + getULEB128Size(Column);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: We should handle the discriminator here, but dsymutil
|
|
|
|
// doesn' consider it, thus ignore it for now.
|
|
|
|
|
|
|
|
if (Isa != Row.Isa) {
|
|
|
|
Isa = Row.Isa;
|
|
|
|
MS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
|
|
|
|
MS->EmitULEB128IntValue(Isa);
|
|
|
|
LineSectionSize += 1 + getULEB128Size(Isa);
|
|
|
|
}
|
|
|
|
if (IsStatement != Row.IsStmt) {
|
|
|
|
IsStatement = Row.IsStmt;
|
|
|
|
MS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
|
|
|
|
LineSectionSize += 1;
|
|
|
|
}
|
|
|
|
if (Row.BasicBlock) {
|
|
|
|
MS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
|
|
|
|
LineSectionSize += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Row.PrologueEnd) {
|
|
|
|
MS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
|
|
|
|
LineSectionSize += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Row.EpilogueBegin) {
|
|
|
|
MS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
|
|
|
|
LineSectionSize += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t LineDelta = int64_t(Row.Line) - LastLine;
|
|
|
|
if (!Row.EndSequence) {
|
2015-08-07 23:14:13 +08:00
|
|
|
MCDwarfLineAddr::Encode(*MC, Params, LineDelta, AddressDelta, EncodingOS);
|
2015-03-16 04:45:43 +08:00
|
|
|
MS->EmitBytes(EncodingOS.str());
|
|
|
|
LineSectionSize += EncodingBuffer.size();
|
|
|
|
EncodingBuffer.resize(0);
|
|
|
|
Address = Row.Address;
|
|
|
|
LastLine = Row.Line;
|
|
|
|
RowsSinceLastSequence++;
|
|
|
|
} else {
|
|
|
|
if (LineDelta) {
|
|
|
|
MS->EmitIntValue(dwarf::DW_LNS_advance_line, 1);
|
|
|
|
MS->EmitSLEB128IntValue(LineDelta);
|
|
|
|
LineSectionSize += 1 + getSLEB128Size(LineDelta);
|
|
|
|
}
|
|
|
|
if (AddressDelta) {
|
|
|
|
MS->EmitIntValue(dwarf::DW_LNS_advance_pc, 1);
|
|
|
|
MS->EmitULEB128IntValue(AddressDelta);
|
|
|
|
LineSectionSize += 1 + getULEB128Size(AddressDelta);
|
|
|
|
}
|
2015-08-07 23:14:13 +08:00
|
|
|
MCDwarfLineAddr::Encode(*MC, Params, INT64_MAX, 0, EncodingOS);
|
2015-03-16 04:45:43 +08:00
|
|
|
MS->EmitBytes(EncodingOS.str());
|
|
|
|
LineSectionSize += EncodingBuffer.size();
|
|
|
|
EncodingBuffer.resize(0);
|
|
|
|
Address = -1ULL;
|
|
|
|
LastLine = FileNum = IsStatement = 1;
|
|
|
|
RowsSinceLastSequence = Column = Isa = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RowsSinceLastSequence) {
|
2015-08-07 23:14:13 +08:00
|
|
|
MCDwarfLineAddr::Encode(*MC, Params, INT64_MAX, 0, EncodingOS);
|
2015-03-16 04:45:43 +08:00
|
|
|
MS->EmitBytes(EncodingOS.str());
|
|
|
|
LineSectionSize += EncodingBuffer.size();
|
|
|
|
EncodingBuffer.resize(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
MS->EmitLabel(LineEndSym);
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit the pubnames or pubtypes section contribution for \p
|
2015-03-16 10:05:10 +08:00
|
|
|
/// Unit into \p Sec. The data is provided in \p Names.
|
|
|
|
void DwarfStreamer::emitPubSectionForUnit(
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSection *Sec, StringRef SecName, const CompileUnit &Unit,
|
2015-03-16 10:05:10 +08:00
|
|
|
const std::vector<CompileUnit::AccelInfo> &Names) {
|
|
|
|
if (Names.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Start the dwarf pubnames section.
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->SwitchSection(Sec);
|
2015-03-18 04:07:06 +08:00
|
|
|
MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + SecName + "_begin");
|
|
|
|
MCSymbol *EndLabel = Asm->createTempSymbol("pub" + SecName + "_end");
|
2015-03-16 10:05:10 +08:00
|
|
|
|
|
|
|
bool HeaderEmitted = false;
|
|
|
|
// Emit the pubnames for this compilation unit.
|
|
|
|
for (const auto &Name : Names) {
|
|
|
|
if (Name.SkipPubSection)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!HeaderEmitted) {
|
|
|
|
// Emit the header.
|
|
|
|
Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); // Length
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitLabel(BeginLabel);
|
2015-03-16 10:05:10 +08:00
|
|
|
Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION); // Version
|
2015-06-06 04:27:07 +08:00
|
|
|
Asm->EmitInt32(Unit.getStartOffset()); // Unit offset
|
2015-03-16 10:05:10 +08:00
|
|
|
Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset()); // Size
|
|
|
|
HeaderEmitted = true;
|
|
|
|
}
|
|
|
|
Asm->EmitInt32(Name.Die->getOffset());
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitBytes(
|
2015-03-16 10:05:10 +08:00
|
|
|
StringRef(Name.Name.data(), Name.Name.size() + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HeaderEmitted)
|
|
|
|
return;
|
|
|
|
Asm->EmitInt32(0); // End marker.
|
2015-04-25 03:11:51 +08:00
|
|
|
Asm->OutStreamer->EmitLabel(EndLabel);
|
2015-03-16 10:05:10 +08:00
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit .debug_pubnames for \p Unit.
|
2015-03-16 10:05:10 +08:00
|
|
|
void DwarfStreamer::emitPubNamesForUnit(const CompileUnit &Unit) {
|
|
|
|
emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubNamesSection(),
|
|
|
|
"names", Unit, Unit.getPubnames());
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit .debug_pubtypes for \p Unit.
|
2015-03-16 10:05:10 +08:00
|
|
|
void DwarfStreamer::emitPubTypesForUnit(const CompileUnit &Unit) {
|
|
|
|
emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubTypesSection(),
|
|
|
|
"types", Unit, Unit.getPubtypes());
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit a CIE into the debug_frame section.
|
2015-06-06 07:06:11 +08:00
|
|
|
void DwarfStreamer::emitCIE(StringRef CIEBytes) {
|
|
|
|
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
|
|
|
|
|
|
|
|
MS->EmitBytes(CIEBytes);
|
|
|
|
FrameSectionSize += CIEBytes.size();
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit a FDE into the debug_frame section. \p FDEBytes
|
2015-06-06 07:06:11 +08:00
|
|
|
/// contains the FDE data without the length, CIE offset and address
|
2017-03-30 20:59:53 +08:00
|
|
|
/// which will be replaced with the parameter values.
|
2015-06-06 07:06:11 +08:00
|
|
|
void DwarfStreamer::emitFDE(uint32_t CIEOffset, uint32_t AddrSize,
|
|
|
|
uint32_t Address, StringRef FDEBytes) {
|
|
|
|
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
|
|
|
|
|
|
|
|
MS->EmitIntValue(FDEBytes.size() + 4 + AddrSize, 4);
|
|
|
|
MS->EmitIntValue(CIEOffset, 4);
|
|
|
|
MS->EmitIntValue(Address, AddrSize);
|
|
|
|
MS->EmitBytes(FDEBytes);
|
|
|
|
FrameSectionSize += FDEBytes.size() + 8 + AddrSize;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// The core of the Dwarf linking logic.
|
2015-02-14 07:18:22 +08:00
|
|
|
///
|
|
|
|
/// The link of the dwarf information from the object files will be
|
|
|
|
/// driven by the selection of 'root DIEs', which are DIEs that
|
|
|
|
/// describe variables or functions that are present in the linked
|
|
|
|
/// binary (and thus have entries in the debug map). All the debug
|
|
|
|
/// information that will be linked (the DIEs, but also the line
|
|
|
|
/// tables, ranges, ...) is derived from that set of root DIEs.
|
|
|
|
///
|
|
|
|
/// The root DIEs are identified because they contain relocations that
|
|
|
|
/// correspond to a debug map entry at specific places (the low_pc for
|
|
|
|
/// a function, the location for a variable). These relocations are
|
|
|
|
/// called ValidRelocs in the DwarfLinker and are gathered as a very
|
|
|
|
/// first step when we start processing a DebugMapObject.
|
2015-01-29 02:27:01 +08:00
|
|
|
class DwarfLinker {
|
|
|
|
public:
|
2015-02-28 08:29:07 +08:00
|
|
|
DwarfLinker(StringRef OutputFilename, const LinkOptions &Options)
|
|
|
|
: OutputFilename(OutputFilename), Options(Options),
|
2017-07-22 00:51:16 +08:00
|
|
|
BinHolder(Options.Verbose) {}
|
2015-01-29 02:27:01 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Link the contents of the DebugMap.
|
2015-01-29 02:27:01 +08:00
|
|
|
bool link(const DebugMap &);
|
|
|
|
|
2016-12-14 02:25:19 +08:00
|
|
|
void reportWarning(const Twine &Warning,
|
|
|
|
const DWARFDie *DIE = nullptr) const;
|
2015-09-23 02:50:51 +08:00
|
|
|
|
2015-01-29 06:15:14 +08:00
|
|
|
private:
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Called at the start of a debug object link.
|
2015-03-16 04:45:43 +08:00
|
|
|
void startDebugObject(DWARFContext &, DebugMapObject &);
|
2015-01-29 06:15:14 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Called at the end of a debug object link.
|
2015-01-29 06:15:14 +08:00
|
|
|
void endDebugObject();
|
|
|
|
|
2017-04-21 03:16:51 +08:00
|
|
|
/// Remembers the newest DWARF version we've seen in a unit.
|
|
|
|
void maybeUpdateMaxDwarfVersion(unsigned Version) {
|
|
|
|
if (MaxDwarfVersion < Version)
|
|
|
|
MaxDwarfVersion = Version;
|
|
|
|
}
|
|
|
|
|
2015-09-12 07:45:30 +08:00
|
|
|
/// Keeps track of relocations.
|
|
|
|
class RelocationManager {
|
|
|
|
struct ValidReloc {
|
|
|
|
uint32_t Offset;
|
|
|
|
uint32_t Size;
|
|
|
|
uint64_t Addend;
|
|
|
|
const DebugMapObject::DebugMapEntry *Mapping;
|
|
|
|
|
|
|
|
ValidReloc(uint32_t Offset, uint32_t Size, uint64_t Addend,
|
|
|
|
const DebugMapObject::DebugMapEntry *Mapping)
|
|
|
|
: Offset(Offset), Size(Size), Addend(Addend), Mapping(Mapping) {}
|
|
|
|
|
|
|
|
bool operator<(const ValidReloc &RHS) const {
|
|
|
|
return Offset < RHS.Offset;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DwarfLinker &Linker;
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// The valid relocations for the current DebugMapObject.
|
2015-09-12 07:45:30 +08:00
|
|
|
/// This vector is sorted by relocation offset.
|
|
|
|
std::vector<ValidReloc> ValidRelocs;
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Index into ValidRelocs of the next relocation to
|
2015-09-12 07:45:30 +08:00
|
|
|
/// consider. As we walk the DIEs in acsending file offset and as
|
|
|
|
/// ValidRelocs is sorted by file offset, keeping this index
|
|
|
|
/// uptodate is all we have to do to have a cheap lookup during the
|
|
|
|
/// root DIE selection and during DIE cloning.
|
|
|
|
unsigned NextValidReloc;
|
|
|
|
|
|
|
|
public:
|
|
|
|
RelocationManager(DwarfLinker &Linker)
|
|
|
|
: Linker(Linker), NextValidReloc(0) {}
|
|
|
|
|
|
|
|
bool hasValidRelocs() const { return !ValidRelocs.empty(); }
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Reset the NextValidReloc counter.
|
2015-09-12 07:45:30 +08:00
|
|
|
void resetValidRelocs() { NextValidReloc = 0; }
|
|
|
|
|
|
|
|
/// \defgroup FindValidRelocations Translate debug map into a list
|
|
|
|
/// of relevant relocations
|
|
|
|
///
|
|
|
|
/// @{
|
|
|
|
bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
|
|
|
|
const DebugMapObject &DMO);
|
|
|
|
|
|
|
|
bool findValidRelocs(const object::SectionRef &Section,
|
|
|
|
const object::ObjectFile &Obj,
|
|
|
|
const DebugMapObject &DMO);
|
|
|
|
|
|
|
|
void findValidRelocsMachO(const object::SectionRef &Section,
|
|
|
|
const object::MachOObjectFile &Obj,
|
|
|
|
const DebugMapObject &DMO);
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
|
|
|
|
CompileUnit::DIEInfo &Info);
|
|
|
|
|
|
|
|
bool applyValidRelocs(MutableArrayRef<char> Data, uint32_t BaseOffset,
|
|
|
|
bool isLittleEndian);
|
2015-02-14 07:18:22 +08:00
|
|
|
};
|
|
|
|
|
2015-02-14 07:18:34 +08:00
|
|
|
/// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries.
|
|
|
|
///
|
|
|
|
/// @{
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Recursively walk the \p DIE tree and look for DIEs to
|
2015-02-14 07:18:34 +08:00
|
|
|
/// keep. Store that information in \p CU's DIEInfo.
|
2017-09-01 04:22:31 +08:00
|
|
|
///
|
|
|
|
/// The return value indicates whether the DIE is incomplete.
|
|
|
|
bool lookForDIEsToKeep(RelocationManager &RelocMgr, const DWARFDie &DIE,
|
2015-02-14 07:18:34 +08:00
|
|
|
const DebugMapObject &DMO, CompileUnit &CU,
|
|
|
|
unsigned Flags);
|
|
|
|
|
2015-09-23 06:20:50 +08:00
|
|
|
/// If this compile unit is really a skeleton CU that points to a
|
|
|
|
/// clang module, register it in ClangModules and return true.
|
|
|
|
///
|
|
|
|
/// A skeleton CU is a CU without children, a DW_AT_gnu_dwo_name
|
|
|
|
/// pointing to the module, and a DW_AT_gnu_dwo_id with the module
|
|
|
|
/// hash.
|
2016-12-14 02:25:19 +08:00
|
|
|
bool registerModuleReference(const DWARFDie &CUDie,
|
2015-09-23 06:20:50 +08:00
|
|
|
const DWARFUnit &Unit, DebugMap &ModuleMap,
|
|
|
|
unsigned Indent = 0);
|
|
|
|
|
|
|
|
/// Recursively add the debug info in this clang module .pcm
|
|
|
|
/// file (and all the modules imported by it in a bottom-up fashion)
|
|
|
|
/// to Units.
|
2015-09-24 01:35:52 +08:00
|
|
|
void loadClangModule(StringRef Filename, StringRef ModulePath,
|
|
|
|
StringRef ModuleName, uint64_t DwoId,
|
2015-09-23 06:20:50 +08:00
|
|
|
DebugMap &ModuleMap, unsigned Indent = 0);
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Flags passed to DwarfLinker::lookForDIEsToKeep
|
2015-02-14 07:18:34 +08:00
|
|
|
enum TravesalFlags {
|
|
|
|
TF_Keep = 1 << 0, ///< Mark the traversed DIEs as kept.
|
|
|
|
TF_InFunctionScope = 1 << 1, ///< Current scope is a fucntion scope.
|
|
|
|
TF_DependencyWalk = 1 << 2, ///< Walking the dependencies of a kept DIE.
|
|
|
|
TF_ParentWalk = 1 << 3, ///< Walking up the parents of a kept DIE.
|
2015-07-22 06:41:43 +08:00
|
|
|
TF_ODR = 1 << 4, ///< Use the ODR whhile keeping dependants.
|
2015-09-11 12:17:30 +08:00
|
|
|
TF_SkipPC = 1 << 5, ///< Skip all location attributes.
|
2015-02-14 07:18:34 +08:00
|
|
|
};
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Mark the passed DIE as well as all the ones it depends on as kept.
|
2015-09-22 23:31:14 +08:00
|
|
|
void keepDIEAndDependencies(RelocationManager &RelocMgr,
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &DIE,
|
2015-02-14 07:18:34 +08:00
|
|
|
CompileUnit::DIEInfo &MyInfo,
|
|
|
|
const DebugMapObject &DMO, CompileUnit &CU,
|
2015-07-22 06:41:43 +08:00
|
|
|
bool UseODR);
|
2015-02-14 07:18:34 +08:00
|
|
|
|
2015-09-12 07:45:30 +08:00
|
|
|
unsigned shouldKeepDIE(RelocationManager &RelocMgr,
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &DIE,
|
2015-02-14 07:18:34 +08:00
|
|
|
CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
|
|
|
|
unsigned Flags);
|
|
|
|
|
2015-09-12 07:45:30 +08:00
|
|
|
unsigned shouldKeepVariableDIE(RelocationManager &RelocMgr,
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &DIE,
|
2015-02-14 07:18:34 +08:00
|
|
|
CompileUnit &Unit,
|
|
|
|
CompileUnit::DIEInfo &MyInfo, unsigned Flags);
|
|
|
|
|
2015-09-12 07:45:30 +08:00
|
|
|
unsigned shouldKeepSubprogramDIE(RelocationManager &RelocMgr,
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &DIE,
|
2015-02-14 07:18:34 +08:00
|
|
|
CompileUnit &Unit,
|
|
|
|
CompileUnit::DIEInfo &MyInfo,
|
|
|
|
unsigned Flags);
|
|
|
|
|
|
|
|
bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
|
|
|
|
CompileUnit::DIEInfo &Info);
|
|
|
|
/// @}
|
|
|
|
|
2015-03-05 06:07:44 +08:00
|
|
|
/// \defgroup Linking Methods used to link the debug information
|
|
|
|
///
|
|
|
|
/// @{
|
2015-09-12 07:45:30 +08:00
|
|
|
|
2015-09-15 00:46:10 +08:00
|
|
|
class DIECloner {
|
|
|
|
DwarfLinker &Linker;
|
|
|
|
RelocationManager &RelocMgr;
|
|
|
|
/// Allocator used for all the DIEValue objects.
|
|
|
|
BumpPtrAllocator &DIEAlloc;
|
2016-12-02 02:56:29 +08:00
|
|
|
std::vector<std::unique_ptr<CompileUnit>> &CompileUnits;
|
2015-09-15 00:46:10 +08:00
|
|
|
LinkOptions Options;
|
2015-03-12 02:45:52 +08:00
|
|
|
|
2015-09-15 00:46:10 +08:00
|
|
|
public:
|
|
|
|
DIECloner(DwarfLinker &Linker, RelocationManager &RelocMgr,
|
|
|
|
BumpPtrAllocator &DIEAlloc,
|
2016-12-02 02:56:29 +08:00
|
|
|
std::vector<std::unique_ptr<CompileUnit>> &CompileUnits,
|
|
|
|
LinkOptions &Options)
|
2015-09-15 00:46:10 +08:00
|
|
|
: Linker(Linker), RelocMgr(RelocMgr), DIEAlloc(DIEAlloc),
|
|
|
|
CompileUnits(CompileUnits), Options(Options) {}
|
|
|
|
|
|
|
|
/// Recursively clone \p InputDIE into an tree of DIE objects
|
|
|
|
/// where useless (as decided by lookForDIEsToKeep()) bits have been
|
|
|
|
/// stripped out and addresses have been rewritten according to the
|
|
|
|
/// debug map.
|
|
|
|
///
|
|
|
|
/// \param OutOffset is the offset the cloned DIE in the output
|
|
|
|
/// compile unit.
|
|
|
|
/// \param PCOffset (while cloning a function scope) is the offset
|
|
|
|
/// applied to the entry point of the function to get the linked address.
|
2016-12-02 02:56:29 +08:00
|
|
|
/// \param Die the output DIE to use, pass NULL to create one.
|
2015-09-15 00:46:10 +08:00
|
|
|
/// \returns the root of the cloned tree or null if nothing was selected.
|
2016-12-14 02:25:19 +08:00
|
|
|
DIE *cloneDIE(const DWARFDie &InputDIE, CompileUnit &U,
|
2016-12-02 02:56:29 +08:00
|
|
|
int64_t PCOffset, uint32_t OutOffset, unsigned Flags,
|
|
|
|
DIE *Die = nullptr);
|
2015-09-15 00:46:10 +08:00
|
|
|
|
|
|
|
/// Construct the output DIE tree by cloning the DIEs we
|
|
|
|
/// chose to keep above. If there are no valid relocs, then there's
|
|
|
|
/// nothing to clone/emit.
|
2017-07-20 06:27:28 +08:00
|
|
|
void cloneAllCompileUnits(DWARFContext &DwarfContext);
|
2015-09-15 00:46:10 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef DWARFAbbreviationDeclaration::AttributeSpec AttributeSpec;
|
|
|
|
|
|
|
|
/// Information gathered and exchanged between the various
|
|
|
|
/// clone*Attributes helpers about the attributes of a particular DIE.
|
|
|
|
struct AttributesInfo {
|
|
|
|
const char *Name, *MangledName; ///< Names.
|
|
|
|
uint32_t NameOffset, MangledNameOffset; ///< Offsets in the string pool.
|
|
|
|
|
|
|
|
uint64_t OrigLowPc; ///< Value of AT_low_pc in the input DIE
|
|
|
|
uint64_t OrigHighPc; ///< Value of AT_high_pc in the input DIE
|
|
|
|
int64_t PCOffset; ///< Offset to apply to PC addresses inside a function.
|
|
|
|
|
|
|
|
bool HasLowPc; ///< Does the DIE have a low_pc attribute?
|
|
|
|
bool IsDeclaration; ///< Is this DIE only a declaration?
|
|
|
|
|
|
|
|
AttributesInfo()
|
|
|
|
: Name(nullptr), MangledName(nullptr), NameOffset(0),
|
|
|
|
MangledNameOffset(0), OrigLowPc(UINT64_MAX), OrigHighPc(0),
|
|
|
|
PCOffset(0), HasLowPc(false), IsDeclaration(false) {}
|
|
|
|
};
|
2015-03-16 10:05:10 +08:00
|
|
|
|
2015-09-15 00:46:10 +08:00
|
|
|
/// Helper for cloneDIE.
|
|
|
|
unsigned cloneAttribute(DIE &Die,
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &InputDIE,
|
2015-09-15 00:46:10 +08:00
|
|
|
CompileUnit &U, const DWARFFormValue &Val,
|
|
|
|
const AttributeSpec AttrSpec, unsigned AttrSize,
|
|
|
|
AttributesInfo &AttrInfo);
|
|
|
|
|
|
|
|
/// Clone a string attribute described by \p AttrSpec and add
|
|
|
|
/// it to \p Die.
|
|
|
|
/// \returns the size of the new attribute.
|
|
|
|
unsigned cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec,
|
|
|
|
const DWARFFormValue &Val,
|
|
|
|
const DWARFUnit &U);
|
|
|
|
|
|
|
|
/// Clone an attribute referencing another DIE and add
|
|
|
|
/// it to \p Die.
|
|
|
|
/// \returns the size of the new attribute.
|
|
|
|
unsigned
|
|
|
|
cloneDieReferenceAttribute(DIE &Die,
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &InputDIE,
|
2015-09-15 00:46:10 +08:00
|
|
|
AttributeSpec AttrSpec, unsigned AttrSize,
|
|
|
|
const DWARFFormValue &Val, CompileUnit &Unit);
|
|
|
|
|
|
|
|
/// Clone an attribute referencing another DIE and add
|
|
|
|
/// it to \p Die.
|
|
|
|
/// \returns the size of the new attribute.
|
|
|
|
unsigned cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec,
|
|
|
|
const DWARFFormValue &Val, unsigned AttrSize);
|
|
|
|
|
|
|
|
/// Clone an attribute referencing another DIE and add
|
|
|
|
/// it to \p Die.
|
|
|
|
/// \returns the size of the new attribute.
|
|
|
|
unsigned cloneAddressAttribute(DIE &Die, AttributeSpec AttrSpec,
|
|
|
|
const DWARFFormValue &Val,
|
|
|
|
const CompileUnit &Unit,
|
|
|
|
AttributesInfo &Info);
|
|
|
|
|
|
|
|
/// Clone a scalar attribute and add it to \p Die.
|
|
|
|
/// \returns the size of the new attribute.
|
|
|
|
unsigned cloneScalarAttribute(DIE &Die,
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &InputDIE,
|
2015-09-15 00:46:10 +08:00
|
|
|
CompileUnit &U, AttributeSpec AttrSpec,
|
|
|
|
const DWARFFormValue &Val, unsigned AttrSize,
|
|
|
|
AttributesInfo &Info);
|
|
|
|
|
|
|
|
/// Get the potential name and mangled name for the entity
|
|
|
|
/// described by \p Die and store them in \Info if they are not
|
|
|
|
/// already there.
|
|
|
|
/// \returns is a name was found.
|
2016-12-14 02:25:19 +08:00
|
|
|
bool getDIENames(const DWARFDie &Die, AttributesInfo &Info);
|
2015-09-15 00:46:10 +08:00
|
|
|
|
|
|
|
/// Create a copy of abbreviation Abbrev.
|
|
|
|
void copyAbbrev(const DWARFAbbreviationDeclaration &Abbrev, bool hasODR);
|
2015-03-12 02:45:52 +08:00
|
|
|
};
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Assign an abbreviation number to \p Abbrev
|
2015-03-05 06:07:44 +08:00
|
|
|
void AssignAbbrev(DIEAbbrev &Abbrev);
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// FoldingSet that uniques the abbreviations.
|
2015-03-05 06:07:44 +08:00
|
|
|
FoldingSet<DIEAbbrev> AbbreviationsSet;
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Storage for the unique Abbreviations.
|
2015-03-05 06:07:44 +08:00
|
|
|
/// This is passed to AsmPrinter::emitDwarfAbbrevs(), thus it cannot
|
|
|
|
/// be changed to a vecot of unique_ptrs.
|
2015-11-18 08:34:10 +08:00
|
|
|
std::vector<std::unique_ptr<DIEAbbrev>> Abbreviations;
|
2015-03-05 06:07:44 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Compute and emit debug_ranges section for \p Unit, and
|
2015-03-14 07:30:31 +08:00
|
|
|
/// patch the attributes referencing it.
|
|
|
|
void patchRangesForUnit(const CompileUnit &Unit, DWARFContext &Dwarf) const;
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Generate and emit the DW_AT_ranges attribute for a
|
2015-03-14 07:30:31 +08:00
|
|
|
/// compile_unit if it had one.
|
|
|
|
void generateUnitRanges(CompileUnit &Unit) const;
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Extract the line tables fromt he original dwarf, extract
|
2015-03-16 04:45:43 +08:00
|
|
|
/// the relevant parts according to the linked function ranges and
|
|
|
|
/// emit the result in the debug_line section.
|
|
|
|
void patchLineTableForUnit(CompileUnit &Unit, DWARFContext &OrigDwarf);
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Emit the accelerator entries for \p Unit.
|
2015-03-16 10:05:10 +08:00
|
|
|
void emitAcceleratorEntriesForUnit(CompileUnit &Unit);
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Patch the frame info for an object file and emit it.
|
2015-06-06 07:06:11 +08:00
|
|
|
void patchFrameInfoForObject(const DebugMapObject &, DWARFContext &,
|
|
|
|
unsigned AddressSize);
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// DIELoc objects that need to be destructed (but not freed!).
|
2015-03-05 06:07:44 +08:00
|
|
|
std::vector<DIELoc *> DIELocs;
|
2017-07-22 00:51:17 +08:00
|
|
|
/// DIEBlock objects that need to be destructed (but not freed!).
|
2015-03-05 06:07:44 +08:00
|
|
|
std::vector<DIEBlock *> DIEBlocks;
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Allocator used for all the DIEValue objects.
|
2015-03-05 06:07:44 +08:00
|
|
|
BumpPtrAllocator DIEAlloc;
|
|
|
|
/// @}
|
|
|
|
|
2015-07-22 06:41:43 +08:00
|
|
|
/// ODR Contexts for that link.
|
|
|
|
DeclContextTree ODRContexts;
|
|
|
|
|
2015-02-14 07:18:29 +08:00
|
|
|
/// \defgroup Helpers Various helper methods.
|
|
|
|
///
|
|
|
|
/// @{
|
2016-06-09 03:09:22 +08:00
|
|
|
bool createStreamer(const Triple &TheTriple, StringRef OutputFilename);
|
2015-07-24 14:41:11 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Attempt to load a debug object from disk.
|
2015-07-24 14:41:11 +08:00
|
|
|
ErrorOr<const object::ObjectFile &> loadObject(BinaryHolder &BinaryHolder,
|
|
|
|
DebugMapObject &Obj,
|
|
|
|
const DebugMap &Map);
|
2015-02-14 07:18:29 +08:00
|
|
|
/// @}
|
|
|
|
|
2015-01-29 02:27:01 +08:00
|
|
|
std::string OutputFilename;
|
2015-02-28 08:29:07 +08:00
|
|
|
LinkOptions Options;
|
2015-01-29 02:27:01 +08:00
|
|
|
BinaryHolder BinHolder;
|
2015-02-28 08:29:11 +08:00
|
|
|
std::unique_ptr<DwarfStreamer> Streamer;
|
2015-09-12 07:45:30 +08:00
|
|
|
uint64_t OutputDebugInfoSize;
|
2015-09-23 06:20:50 +08:00
|
|
|
unsigned UnitID; ///< A unique ID that identifies each compile unit.
|
2017-04-21 03:16:51 +08:00
|
|
|
unsigned MaxDwarfVersion = 0;
|
2015-01-29 06:15:14 +08:00
|
|
|
|
|
|
|
/// The units of the current debug map object.
|
2016-12-02 02:56:29 +08:00
|
|
|
std::vector<std::unique_ptr<CompileUnit>> Units;
|
|
|
|
|
2015-02-14 07:18:29 +08:00
|
|
|
|
2015-10-24 01:10:44 +08:00
|
|
|
/// The debug map object currently under consideration.
|
2015-02-14 07:18:29 +08:00
|
|
|
DebugMapObject *CurrentDebugObject;
|
2015-03-07 01:56:30 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// The Dwarf string pool.
|
2015-03-07 01:56:30 +08:00
|
|
|
NonRelocatableStringpool StringPool;
|
2015-03-16 04:45:43 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// This map is keyed by the entry PC of functions in that
|
2015-03-16 04:45:43 +08:00
|
|
|
/// debug object and the associated value is a pair storing the
|
|
|
|
/// corresponding end PC and the offset to apply to get the linked
|
|
|
|
/// address.
|
|
|
|
///
|
|
|
|
/// See startDebugObject() for a more complete description of its use.
|
|
|
|
std::map<uint64_t, std::pair<uint64_t, int64_t>> Ranges;
|
2015-06-06 07:06:11 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// The CIEs that have been emitted in the output
|
2015-06-06 07:06:11 +08:00
|
|
|
/// section. The actual CIE data serves a the key to this StringMap,
|
|
|
|
/// this takes care of comparing the semantics of CIEs defined in
|
|
|
|
/// different object files.
|
|
|
|
StringMap<uint32_t> EmittedCIEs;
|
|
|
|
|
|
|
|
/// Offset of the last CIE that has been emitted in the output
|
|
|
|
/// debug_frame section.
|
2017-07-22 00:51:16 +08:00
|
|
|
uint32_t LastCIEOffset = 0;
|
2015-09-23 06:20:50 +08:00
|
|
|
|
2015-09-24 01:11:10 +08:00
|
|
|
/// Mapping the PCM filename to the DwoId.
|
|
|
|
StringMap<uint64_t> ClangModules;
|
2016-01-15 02:31:07 +08:00
|
|
|
|
|
|
|
bool ModuleCacheHintDisplayed = false;
|
|
|
|
bool ArchiveHintDisplayed = false;
|
2015-01-29 02:27:01 +08:00
|
|
|
};
|
|
|
|
|
2015-09-23 02:50:58 +08:00
|
|
|
/// Similar to DWARFUnitSection::getUnitForOffset(), but returning our
|
|
|
|
/// CompileUnit object instead.
|
2016-12-02 02:56:29 +08:00
|
|
|
static CompileUnit *getUnitForOffset(
|
|
|
|
std::vector<std::unique_ptr<CompileUnit>> &Units, unsigned Offset) {
|
2015-02-14 07:18:29 +08:00
|
|
|
auto CU =
|
|
|
|
std::upper_bound(Units.begin(), Units.end(), Offset,
|
2016-12-02 02:56:29 +08:00
|
|
|
[](uint32_t LHS, const std::unique_ptr<CompileUnit> &RHS) {
|
|
|
|
return LHS < RHS->getOrigUnit().getNextUnitOffset();
|
2015-02-14 07:18:29 +08:00
|
|
|
});
|
2016-12-02 02:56:29 +08:00
|
|
|
return CU != Units.end() ? CU->get() : nullptr;
|
2015-02-14 07:18:29 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 02:50:58 +08:00
|
|
|
/// Resolve the DIE attribute reference that has been
|
2015-02-14 07:18:29 +08:00
|
|
|
/// extracted in \p RefValue. The resulting DIE migh be in another
|
|
|
|
/// CompileUnit which is stored into \p ReferencedCU.
|
|
|
|
/// \returns null if resolving fails for any reason.
|
2016-12-14 02:25:19 +08:00
|
|
|
static DWARFDie resolveDIEReference(
|
2016-12-02 02:56:29 +08:00
|
|
|
const DwarfLinker &Linker, std::vector<std::unique_ptr<CompileUnit>> &Units,
|
2015-07-22 06:41:43 +08:00
|
|
|
const DWARFFormValue &RefValue, const DWARFUnit &Unit,
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &DIE, CompileUnit *&RefCU) {
|
2015-02-14 07:18:29 +08:00
|
|
|
assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
|
2016-11-01 00:46:02 +08:00
|
|
|
uint64_t RefOffset = *RefValue.getAsReference();
|
2015-02-14 07:18:29 +08:00
|
|
|
|
2015-09-23 02:50:58 +08:00
|
|
|
if ((RefCU = getUnitForOffset(Units, RefOffset)))
|
2017-09-21 18:28:33 +08:00
|
|
|
if (const auto RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset)) {
|
|
|
|
// In a file with broken references, an attribute might point to a NULL
|
|
|
|
// DIE.
|
|
|
|
if(!RefDie.isNULL())
|
|
|
|
return RefDie;
|
|
|
|
}
|
2015-02-14 07:18:29 +08:00
|
|
|
|
2016-12-14 02:25:19 +08:00
|
|
|
Linker.reportWarning("could not find referenced DIE", &DIE);
|
|
|
|
return DWARFDie();
|
2015-02-14 07:18:29 +08:00
|
|
|
}
|
|
|
|
|
2015-07-22 06:41:43 +08:00
|
|
|
/// \returns whether the passed \a Attr type might contain a DIE
|
|
|
|
/// reference suitable for ODR uniquing.
|
|
|
|
static bool isODRAttribute(uint16_t Attr) {
|
|
|
|
switch (Attr) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case dwarf::DW_AT_type:
|
|
|
|
case dwarf::DW_AT_containing_type:
|
|
|
|
case dwarf::DW_AT_specification:
|
|
|
|
case dwarf::DW_AT_abstract_origin:
|
|
|
|
case dwarf::DW_AT_import:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Improper attribute.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the last DIE/CU a context was seen in and, possibly invalidate
|
|
|
|
/// the context if it is ambiguous.
|
|
|
|
///
|
|
|
|
/// In the current implementation, we don't handle overloaded
|
|
|
|
/// functions well, because the argument types are not taken into
|
|
|
|
/// account when computing the DeclContext tree.
|
|
|
|
///
|
|
|
|
/// Some of this is mitigated byt using mangled names that do contain
|
|
|
|
/// the arguments types, but sometimes (eg. with function templates)
|
|
|
|
/// we don't have that. In that case, just do not unique anything that
|
|
|
|
/// refers to the contexts we are not able to distinguish.
|
|
|
|
///
|
|
|
|
/// If a context that is not a namespace appears twice in the same CU,
|
|
|
|
/// we know it is ambiguous. Make it invalid.
|
|
|
|
bool DeclContext::setLastSeenDIE(CompileUnit &U,
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &Die) {
|
2015-07-22 06:41:43 +08:00
|
|
|
if (LastSeenCompileUnitID == U.getUniqueID()) {
|
|
|
|
DWARFUnit &OrigUnit = U.getOrigUnit();
|
|
|
|
uint32_t FirstIdx = OrigUnit.getDIEIndex(LastSeenDIE);
|
|
|
|
U.getInfo(FirstIdx).Ctxt = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LastSeenCompileUnitID = U.getUniqueID();
|
|
|
|
LastSeenDIE = Die;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerIntPair<DeclContext *, 1> DeclContextTree::getChildDeclContext(
|
2016-12-14 02:25:19 +08:00
|
|
|
DeclContext &Context, const DWARFDie &DIE, CompileUnit &U,
|
2015-10-02 08:27:08 +08:00
|
|
|
NonRelocatableStringpool &StringPool, bool InClangModule) {
|
2016-12-14 02:25:19 +08:00
|
|
|
unsigned Tag = DIE.getTag();
|
2015-07-22 06:41:43 +08:00
|
|
|
|
|
|
|
// FIXME: dsymutil-classic compat: We should bail out here if we
|
|
|
|
// have a specification or an abstract_origin. We will get the
|
|
|
|
// parent context wrong here.
|
|
|
|
|
|
|
|
switch (Tag) {
|
|
|
|
default:
|
|
|
|
// By default stop gathering child contexts.
|
|
|
|
return PointerIntPair<DeclContext *, 1>(nullptr);
|
2015-09-24 01:35:52 +08:00
|
|
|
case dwarf::DW_TAG_module:
|
|
|
|
break;
|
2015-07-22 06:41:43 +08:00
|
|
|
case dwarf::DW_TAG_compile_unit:
|
|
|
|
return PointerIntPair<DeclContext *, 1>(&Context);
|
|
|
|
case dwarf::DW_TAG_subprogram:
|
|
|
|
// Do not unique anything inside CU local functions.
|
|
|
|
if ((Context.getTag() == dwarf::DW_TAG_namespace ||
|
|
|
|
Context.getTag() == dwarf::DW_TAG_compile_unit) &&
|
2017-01-14 05:08:18 +08:00
|
|
|
!dwarf::toUnsigned(DIE.find(dwarf::DW_AT_external), 0))
|
2015-07-22 06:41:43 +08:00
|
|
|
return PointerIntPair<DeclContext *, 1>(nullptr);
|
2016-08-17 13:10:15 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2015-07-22 06:41:43 +08:00
|
|
|
case dwarf::DW_TAG_member:
|
|
|
|
case dwarf::DW_TAG_namespace:
|
|
|
|
case dwarf::DW_TAG_structure_type:
|
|
|
|
case dwarf::DW_TAG_class_type:
|
|
|
|
case dwarf::DW_TAG_union_type:
|
|
|
|
case dwarf::DW_TAG_enumeration_type:
|
|
|
|
case dwarf::DW_TAG_typedef:
|
|
|
|
// Artificial things might be ambiguous, because they might be
|
|
|
|
// created on demand. For example implicitely defined constructors
|
|
|
|
// are ambiguous because of the way we identify contexts, and they
|
|
|
|
// won't be generated everytime everywhere.
|
2017-01-14 05:08:18 +08:00
|
|
|
if (dwarf::toUnsigned(DIE.find(dwarf::DW_AT_artificial), 0))
|
2015-07-22 06:41:43 +08:00
|
|
|
return PointerIntPair<DeclContext *, 1>(nullptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-12-14 02:25:19 +08:00
|
|
|
const char *Name = DIE.getName(DINameKind::LinkageName);
|
|
|
|
const char *ShortName = DIE.getName(DINameKind::ShortName);
|
2015-07-22 06:41:43 +08:00
|
|
|
StringRef NameRef;
|
|
|
|
StringRef ShortNameRef;
|
|
|
|
StringRef FileRef;
|
|
|
|
|
|
|
|
if (Name)
|
|
|
|
NameRef = StringPool.internString(Name);
|
|
|
|
else if (Tag == dwarf::DW_TAG_namespace)
|
|
|
|
// FIXME: For dsymutil-classic compatibility. I think uniquing
|
|
|
|
// within anonymous namespaces is wrong. There is no ODR guarantee
|
|
|
|
// there.
|
|
|
|
NameRef = StringPool.internString("(anonymous namespace)");
|
|
|
|
|
|
|
|
if (ShortName && ShortName != Name)
|
|
|
|
ShortNameRef = StringPool.internString(ShortName);
|
|
|
|
else
|
|
|
|
ShortNameRef = NameRef;
|
|
|
|
|
|
|
|
if (Tag != dwarf::DW_TAG_class_type && Tag != dwarf::DW_TAG_structure_type &&
|
|
|
|
Tag != dwarf::DW_TAG_union_type &&
|
|
|
|
Tag != dwarf::DW_TAG_enumeration_type && NameRef.empty())
|
|
|
|
return PointerIntPair<DeclContext *, 1>(nullptr);
|
|
|
|
|
|
|
|
unsigned Line = 0;
|
2015-10-02 08:27:08 +08:00
|
|
|
unsigned ByteSize = UINT32_MAX;
|
|
|
|
|
|
|
|
if (!InClangModule) {
|
|
|
|
// Gather some discriminating data about the DeclContext we will be
|
|
|
|
// creating: File, line number and byte size. This shouldn't be
|
|
|
|
// necessary, because the ODR is just about names, but given that we
|
|
|
|
// do some approximations with overloaded functions and anonymous
|
|
|
|
// namespaces, use these additional data points to make the process
|
|
|
|
// safer. This is disabled for clang modules, because forward
|
|
|
|
// declarations of module-defined types do not have a file and line.
|
2017-01-14 05:08:18 +08:00
|
|
|
ByteSize = dwarf::toUnsigned(DIE.find(dwarf::DW_AT_byte_size), UINT64_MAX);
|
2015-10-02 08:27:08 +08:00
|
|
|
if (Tag != dwarf::DW_TAG_namespace || !Name) {
|
2017-01-14 05:08:18 +08:00
|
|
|
if (unsigned FileNum = dwarf::toUnsigned(DIE.find(dwarf::DW_AT_decl_file), 0)) {
|
2015-10-02 08:27:08 +08:00
|
|
|
if (const auto *LT = U.getOrigUnit().getContext().getLineTableForUnit(
|
|
|
|
&U.getOrigUnit())) {
|
|
|
|
// FIXME: dsymutil-classic compatibility. I'd rather not
|
|
|
|
// unique anything in anonymous namespaces, but if we do, then
|
|
|
|
// verify that the file and line correspond.
|
|
|
|
if (!Name && Tag == dwarf::DW_TAG_namespace)
|
|
|
|
FileNum = 1;
|
|
|
|
|
|
|
|
// FIXME: Passing U.getOrigUnit().getCompilationDir()
|
|
|
|
// instead of "" would allow more uniquing, but for now, do
|
|
|
|
// it this way to match dsymutil-classic.
|
2016-07-22 09:41:32 +08:00
|
|
|
if (LT->hasFileAtIndex(FileNum)) {
|
2017-01-14 05:08:18 +08:00
|
|
|
Line = dwarf::toUnsigned(DIE.find(dwarf::DW_AT_decl_line), 0);
|
2015-10-02 08:27:08 +08:00
|
|
|
// Cache the resolved paths, because calling realpath is expansive.
|
2016-03-18 11:48:09 +08:00
|
|
|
StringRef ResolvedPath = U.getResolvedPath(FileNum);
|
|
|
|
if (!ResolvedPath.empty()) {
|
|
|
|
FileRef = ResolvedPath;
|
2015-10-02 08:27:08 +08:00
|
|
|
} else {
|
2016-07-22 09:41:32 +08:00
|
|
|
std::string File;
|
|
|
|
bool gotFileName =
|
|
|
|
LT->getFileNameByIndex(FileNum, "",
|
|
|
|
DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
|
|
|
|
File);
|
|
|
|
(void)gotFileName;
|
|
|
|
assert(gotFileName && "Must get file name from line table");
|
2016-07-22 09:52:58 +08:00
|
|
|
#ifdef HAVE_REALPATH
|
2015-10-02 08:27:08 +08:00
|
|
|
char RealPath[PATH_MAX + 1];
|
|
|
|
RealPath[PATH_MAX] = 0;
|
|
|
|
if (::realpath(File.c_str(), RealPath))
|
|
|
|
File = RealPath;
|
2016-03-18 13:04:04 +08:00
|
|
|
#endif
|
2016-03-18 11:48:09 +08:00
|
|
|
FileRef = StringPool.internString(File);
|
|
|
|
U.setResolvedPath(FileNum, FileRef);
|
2015-10-02 08:27:08 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-22 06:41:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Line && NameRef.empty())
|
|
|
|
return PointerIntPair<DeclContext *, 1>(nullptr);
|
|
|
|
|
|
|
|
// We hash NameRef, which is the mangled name, in order to get most
|
2015-09-24 01:35:52 +08:00
|
|
|
// overloaded functions resolve correctly.
|
|
|
|
//
|
|
|
|
// Strictly speaking, hashing the Tag is only necessary for a
|
|
|
|
// DW_TAG_module, to prevent uniquing of a module and a namespace
|
|
|
|
// with the same name.
|
|
|
|
//
|
|
|
|
// FIXME: dsymutil-classic won't unique the same type presented
|
|
|
|
// once as a struct and once as a class. Using the Tag in the fully
|
|
|
|
// qualified name hash to get the same effect.
|
2015-07-22 06:41:43 +08:00
|
|
|
unsigned Hash = hash_combine(Context.getQualifiedNameHash(), Tag, NameRef);
|
|
|
|
|
|
|
|
// FIXME: dsymutil-classic compatibility: when we don't have a name,
|
|
|
|
// use the filename.
|
|
|
|
if (Tag == dwarf::DW_TAG_namespace && NameRef == "(anonymous namespace)")
|
|
|
|
Hash = hash_combine(Hash, FileRef);
|
|
|
|
|
|
|
|
// Now look if this context already exists.
|
|
|
|
DeclContext Key(Hash, Line, ByteSize, Tag, NameRef, FileRef, Context);
|
|
|
|
auto ContextIter = Contexts.find(&Key);
|
|
|
|
|
|
|
|
if (ContextIter == Contexts.end()) {
|
|
|
|
// The context wasn't found.
|
|
|
|
bool Inserted;
|
|
|
|
DeclContext *NewContext =
|
|
|
|
new (Allocator) DeclContext(Hash, Line, ByteSize, Tag, NameRef, FileRef,
|
|
|
|
Context, DIE, U.getUniqueID());
|
|
|
|
std::tie(ContextIter, Inserted) = Contexts.insert(NewContext);
|
|
|
|
assert(Inserted && "Failed to insert DeclContext");
|
|
|
|
(void)Inserted;
|
|
|
|
} else if (Tag != dwarf::DW_TAG_namespace &&
|
|
|
|
!(*ContextIter)->setLastSeenDIE(U, DIE)) {
|
|
|
|
// The context was found, but it is ambiguous with another context
|
|
|
|
// in the same file. Mark it invalid.
|
|
|
|
return PointerIntPair<DeclContext *, 1>(*ContextIter, /* Invalid= */ 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ContextIter != Contexts.end());
|
|
|
|
// FIXME: dsymutil-classic compatibility. Union types aren't
|
|
|
|
// uniques, but their children might be.
|
|
|
|
if ((Tag == dwarf::DW_TAG_subprogram &&
|
|
|
|
Context.getTag() != dwarf::DW_TAG_structure_type &&
|
|
|
|
Context.getTag() != dwarf::DW_TAG_class_type) ||
|
|
|
|
(Tag == dwarf::DW_TAG_union_type))
|
|
|
|
return PointerIntPair<DeclContext *, 1>(*ContextIter, /* Invalid= */ 1);
|
|
|
|
|
|
|
|
return PointerIntPair<DeclContext *, 1>(*ContextIter);
|
|
|
|
}
|
|
|
|
|
2016-12-14 02:25:19 +08:00
|
|
|
bool DwarfLinker::DIECloner::getDIENames(const DWARFDie &Die,
|
|
|
|
AttributesInfo &Info) {
|
2015-09-15 00:46:10 +08:00
|
|
|
// FIXME: a bit wasteful as the first getName might return the
|
2015-03-16 10:05:10 +08:00
|
|
|
// short name.
|
|
|
|
if (!Info.MangledName &&
|
2016-12-14 02:25:19 +08:00
|
|
|
(Info.MangledName = Die.getName(DINameKind::LinkageName)))
|
2015-09-15 00:46:10 +08:00
|
|
|
Info.MangledNameOffset =
|
|
|
|
Linker.StringPool.getStringOffset(Info.MangledName);
|
2015-03-16 10:05:10 +08:00
|
|
|
|
2016-12-14 02:25:19 +08:00
|
|
|
if (!Info.Name && (Info.Name = Die.getName(DINameKind::ShortName)))
|
2015-09-15 00:46:10 +08:00
|
|
|
Info.NameOffset = Linker.StringPool.getStringOffset(Info.Name);
|
2015-03-16 10:05:10 +08:00
|
|
|
|
|
|
|
return Info.Name || Info.MangledName;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Report a warning to the user, optionaly including
|
2015-02-14 07:18:29 +08:00
|
|
|
/// information about a specific \p DIE related to the warning.
|
2016-12-14 02:25:19 +08:00
|
|
|
void DwarfLinker::reportWarning(const Twine &Warning,
|
|
|
|
const DWARFDie *DIE) const {
|
2015-02-28 08:29:01 +08:00
|
|
|
StringRef Context = "<debug map>";
|
2015-02-14 07:18:29 +08:00
|
|
|
if (CurrentDebugObject)
|
2015-02-28 08:29:01 +08:00
|
|
|
Context = CurrentDebugObject->getObjectFilename();
|
|
|
|
warn(Warning, Context);
|
2015-02-14 07:18:29 +08:00
|
|
|
|
2015-02-28 08:29:07 +08:00
|
|
|
if (!Options.Verbose || !DIE)
|
2015-02-14 07:18:29 +08:00
|
|
|
return;
|
|
|
|
|
2017-09-13 17:43:05 +08:00
|
|
|
DIDumpOptions DumpOpts;
|
2017-09-21 01:44:00 +08:00
|
|
|
DumpOpts.RecurseDepth = 0;
|
2017-09-13 17:43:05 +08:00
|
|
|
DumpOpts.Verbose = Options.Verbose;
|
|
|
|
|
2015-02-14 07:18:29 +08:00
|
|
|
errs() << " in DIE:\n";
|
2017-09-21 01:44:00 +08:00
|
|
|
DIE->dump(errs(), 6 /* Indent */, DumpOpts);
|
2015-02-14 07:18:29 +08:00
|
|
|
}
|
|
|
|
|
2016-06-09 03:09:22 +08:00
|
|
|
bool DwarfLinker::createStreamer(const Triple &TheTriple,
|
|
|
|
StringRef OutputFilename) {
|
2015-02-28 08:29:11 +08:00
|
|
|
if (Options.NoOutput)
|
|
|
|
return true;
|
|
|
|
|
2015-02-28 08:42:37 +08:00
|
|
|
Streamer = llvm::make_unique<DwarfStreamer>();
|
2015-02-28 08:29:11 +08:00
|
|
|
return Streamer->init(TheTriple, OutputFilename);
|
|
|
|
}
|
|
|
|
|
2015-09-24 01:35:52 +08:00
|
|
|
/// Recursive helper to build the global DeclContext information and
|
|
|
|
/// gather the child->parent relationships in the original compile unit.
|
|
|
|
///
|
|
|
|
/// \return true when this DIE and all of its children are only
|
|
|
|
/// forward declarations to types defined in external clang modules
|
|
|
|
/// (i.e., forward declarations that are children of a DW_TAG_module).
|
2016-12-14 02:25:19 +08:00
|
|
|
static bool analyzeContextInfo(const DWARFDie &DIE,
|
2015-09-24 01:35:52 +08:00
|
|
|
unsigned ParentIdx, CompileUnit &CU,
|
|
|
|
DeclContext *CurrentDeclContext,
|
|
|
|
NonRelocatableStringpool &StringPool,
|
|
|
|
DeclContextTree &Contexts,
|
2015-09-24 04:44:37 +08:00
|
|
|
bool InImportedModule = false) {
|
2015-01-29 06:15:14 +08:00
|
|
|
unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
|
2015-07-22 06:41:43 +08:00
|
|
|
CompileUnit::DIEInfo &Info = CU.getInfo(MyIdx);
|
|
|
|
|
2015-09-24 01:35:52 +08:00
|
|
|
// Clang imposes an ODR on modules(!) regardless of the language:
|
|
|
|
// "The module-id should consist of only a single identifier,
|
|
|
|
// which provides the name of the module being defined. Each
|
|
|
|
// module shall have a single definition."
|
|
|
|
//
|
|
|
|
// This does not extend to the types inside the modules:
|
|
|
|
// "[I]n C, this implies that if two structs are defined in
|
|
|
|
// different submodules with the same name, those two types are
|
|
|
|
// distinct types (but may be compatible types if their
|
|
|
|
// definitions match)."
|
|
|
|
//
|
|
|
|
// We treat non-C++ modules like namespaces for this reason.
|
2016-12-14 02:25:19 +08:00
|
|
|
if (DIE.getTag() == dwarf::DW_TAG_module && ParentIdx == 0 &&
|
2017-09-30 08:22:24 +08:00
|
|
|
dwarf::toString(DIE.find(dwarf::DW_AT_name), "") !=
|
|
|
|
CU.getClangModuleName()) {
|
2015-09-24 04:44:37 +08:00
|
|
|
InImportedModule = true;
|
|
|
|
}
|
2015-09-24 01:35:52 +08:00
|
|
|
|
2015-07-22 06:41:43 +08:00
|
|
|
Info.ParentIdx = ParentIdx;
|
2015-10-02 08:27:08 +08:00
|
|
|
bool InClangModule = CU.isClangModule() || InImportedModule;
|
|
|
|
if (CU.hasODR() || InClangModule) {
|
2015-07-22 06:41:43 +08:00
|
|
|
if (CurrentDeclContext) {
|
2015-10-02 08:27:08 +08:00
|
|
|
auto PtrInvalidPair = Contexts.getChildDeclContext(
|
|
|
|
*CurrentDeclContext, DIE, CU, StringPool, InClangModule);
|
2015-07-22 06:41:43 +08:00
|
|
|
CurrentDeclContext = PtrInvalidPair.getPointer();
|
|
|
|
Info.Ctxt =
|
|
|
|
PtrInvalidPair.getInt() ? nullptr : PtrInvalidPair.getPointer();
|
2017-07-21 10:07:33 +08:00
|
|
|
if (Info.Ctxt)
|
|
|
|
Info.Ctxt->setDefinedInClangModule(InClangModule);
|
2015-07-22 06:41:43 +08:00
|
|
|
} else
|
|
|
|
Info.Ctxt = CurrentDeclContext = nullptr;
|
|
|
|
}
|
2015-01-29 06:15:14 +08:00
|
|
|
|
2015-09-24 04:44:37 +08:00
|
|
|
Info.Prune = InImportedModule;
|
2016-12-14 02:25:19 +08:00
|
|
|
if (DIE.hasChildren())
|
2017-01-06 07:47:37 +08:00
|
|
|
for (auto Child: DIE.children())
|
2015-09-24 01:35:52 +08:00
|
|
|
Info.Prune &= analyzeContextInfo(Child, MyIdx, CU, CurrentDeclContext,
|
2015-09-24 04:44:37 +08:00
|
|
|
StringPool, Contexts, InImportedModule);
|
2015-09-24 01:35:52 +08:00
|
|
|
|
|
|
|
// Prune this DIE if it is either a forward declaration inside a
|
|
|
|
// DW_TAG_module or a DW_TAG_module that contains nothing but
|
|
|
|
// forward declarations.
|
2017-01-12 01:43:37 +08:00
|
|
|
Info.Prune &=
|
|
|
|
(DIE.getTag() == dwarf::DW_TAG_module) ||
|
2017-01-14 05:08:18 +08:00
|
|
|
dwarf::toUnsigned(DIE.find(dwarf::DW_AT_declaration), 0);
|
2015-09-24 01:35:52 +08:00
|
|
|
|
2015-10-06 07:11:20 +08:00
|
|
|
// Don't prune it if there is no definition for the DIE.
|
|
|
|
Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset();
|
|
|
|
|
2015-09-24 01:35:52 +08:00
|
|
|
return Info.Prune;
|
2015-01-29 06:15:14 +08:00
|
|
|
}
|
|
|
|
|
2015-02-14 07:18:34 +08:00
|
|
|
static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
|
|
|
|
switch (Tag) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case dwarf::DW_TAG_subprogram:
|
|
|
|
case dwarf::DW_TAG_lexical_block:
|
|
|
|
case dwarf::DW_TAG_subroutine_type:
|
|
|
|
case dwarf::DW_TAG_structure_type:
|
|
|
|
case dwarf::DW_TAG_class_type:
|
|
|
|
case dwarf::DW_TAG_union_type:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid Tag");
|
|
|
|
}
|
|
|
|
|
2015-03-16 04:45:43 +08:00
|
|
|
void DwarfLinker::startDebugObject(DWARFContext &Dwarf, DebugMapObject &Obj) {
|
|
|
|
// Iterate over the debug map entries and put all the ones that are
|
|
|
|
// functions (because they have a size) into the Ranges map. This
|
|
|
|
// map is very similar to the FunctionRanges that are stored in each
|
|
|
|
// unit, with 2 notable differences:
|
|
|
|
// - obviously this one is global, while the other ones are per-unit.
|
|
|
|
// - this one contains not only the functions described in the DIE
|
|
|
|
// tree, but also the ones that are only in the debug map.
|
|
|
|
// The latter information is required to reproduce dsymutil's logic
|
|
|
|
// while linking line tables. The cases where this information
|
|
|
|
// matters look like bugs that need to be investigated, but for now
|
|
|
|
// we need to reproduce dsymutil's behavior.
|
|
|
|
// FIXME: Once we understood exactly if that information is needed,
|
|
|
|
// maybe totally remove this (or try to use it to do a real
|
|
|
|
// -gline-tables-only on Darwin.
|
|
|
|
for (const auto &Entry : Obj.symbols()) {
|
|
|
|
const auto &Mapping = Entry.getValue();
|
2016-01-31 12:29:22 +08:00
|
|
|
if (Mapping.Size && Mapping.ObjectAddress)
|
|
|
|
Ranges[*Mapping.ObjectAddress] = std::make_pair(
|
|
|
|
*Mapping.ObjectAddress + Mapping.Size,
|
|
|
|
int64_t(Mapping.BinaryAddress) - *Mapping.ObjectAddress);
|
2015-03-16 04:45:43 +08:00
|
|
|
}
|
2015-01-29 06:15:14 +08:00
|
|
|
}
|
|
|
|
|
2015-02-14 07:18:22 +08:00
|
|
|
void DwarfLinker::endDebugObject() {
|
|
|
|
Units.clear();
|
2015-03-16 04:45:43 +08:00
|
|
|
Ranges.clear();
|
2015-03-05 06:07:44 +08:00
|
|
|
|
2015-06-26 22:51:22 +08:00
|
|
|
for (auto I = DIEBlocks.begin(), E = DIEBlocks.end(); I != E; ++I)
|
|
|
|
(*I)->~DIEBlock();
|
|
|
|
for (auto I = DIELocs.begin(), E = DIELocs.end(); I != E; ++I)
|
|
|
|
(*I)->~DIELoc();
|
2015-03-05 06:07:44 +08:00
|
|
|
|
|
|
|
DIEBlocks.clear();
|
|
|
|
DIELocs.clear();
|
|
|
|
DIEAlloc.Reset();
|
2015-02-14 07:18:22 +08:00
|
|
|
}
|
|
|
|
|
2016-02-01 12:43:14 +08:00
|
|
|
static bool isMachOPairedReloc(uint64_t RelocType, uint64_t Arch) {
|
|
|
|
switch (Arch) {
|
|
|
|
case Triple::x86:
|
|
|
|
return RelocType == MachO::GENERIC_RELOC_SECTDIFF ||
|
|
|
|
RelocType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF;
|
|
|
|
case Triple::x86_64:
|
|
|
|
return RelocType == MachO::X86_64_RELOC_SUBTRACTOR;
|
|
|
|
case Triple::arm:
|
|
|
|
case Triple::thumb:
|
|
|
|
return RelocType == MachO::ARM_RELOC_SECTDIFF ||
|
|
|
|
RelocType == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
|
|
|
|
RelocType == MachO::ARM_RELOC_HALF ||
|
|
|
|
RelocType == MachO::ARM_RELOC_HALF_SECTDIFF;
|
|
|
|
case Triple::aarch64:
|
|
|
|
return RelocType == MachO::ARM64_RELOC_SUBTRACTOR;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Iterate over the relocations of the given \p Section and
|
2015-02-14 07:18:22 +08:00
|
|
|
/// store the ones that correspond to debug map entries into the
|
|
|
|
/// ValidRelocs array.
|
2015-09-12 07:45:30 +08:00
|
|
|
void DwarfLinker::RelocationManager::
|
|
|
|
findValidRelocsMachO(const object::SectionRef &Section,
|
|
|
|
const object::MachOObjectFile &Obj,
|
|
|
|
const DebugMapObject &DMO) {
|
2015-02-14 07:18:22 +08:00
|
|
|
StringRef Contents;
|
|
|
|
Section.getContents(Contents);
|
|
|
|
DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
|
2016-02-01 12:43:14 +08:00
|
|
|
bool SkipNext = false;
|
2015-02-14 07:18:22 +08:00
|
|
|
|
|
|
|
for (const object::RelocationRef &Reloc : Section.relocations()) {
|
2016-02-01 12:43:14 +08:00
|
|
|
if (SkipNext) {
|
|
|
|
SkipNext = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-14 07:18:22 +08:00
|
|
|
object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
|
|
|
|
MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
|
2016-02-01 12:43:14 +08:00
|
|
|
|
|
|
|
if (isMachOPairedReloc(Obj.getAnyRelocationType(MachOReloc),
|
|
|
|
Obj.getArch())) {
|
|
|
|
SkipNext = true;
|
|
|
|
Linker.reportWarning(" unsupported relocation in debug_info section.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-14 07:18:22 +08:00
|
|
|
unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
|
2015-06-30 07:29:12 +08:00
|
|
|
uint64_t Offset64 = Reloc.getOffset();
|
|
|
|
if ((RelocSize != 4 && RelocSize != 8)) {
|
2015-09-12 07:45:30 +08:00
|
|
|
Linker.reportWarning(" unsupported relocation in debug_info section.");
|
2015-02-14 07:18:22 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint32_t Offset = Offset64;
|
|
|
|
// Mach-o uses REL relocations, the addend is at the relocation offset.
|
|
|
|
uint64_t Addend = Data.getUnsigned(&Offset, RelocSize);
|
2016-02-01 11:44:22 +08:00
|
|
|
uint64_t SymAddress;
|
|
|
|
int64_t SymOffset;
|
|
|
|
|
|
|
|
if (Obj.isRelocationScattered(MachOReloc)) {
|
|
|
|
// The address of the base symbol for scattered relocations is
|
|
|
|
// stored in the reloc itself. The actual addend will store the
|
|
|
|
// base address plus the offset.
|
|
|
|
SymAddress = Obj.getScatteredRelocationValue(MachOReloc);
|
|
|
|
SymOffset = int64_t(Addend) - SymAddress;
|
|
|
|
} else {
|
|
|
|
SymAddress = Addend;
|
|
|
|
SymOffset = 0;
|
|
|
|
}
|
2015-02-14 07:18:22 +08:00
|
|
|
|
|
|
|
auto Sym = Reloc.getSymbol();
|
|
|
|
if (Sym != Obj.symbol_end()) {
|
2016-04-21 05:24:34 +08:00
|
|
|
Expected<StringRef> SymbolName = Sym->getName();
|
2015-07-03 04:55:21 +08:00
|
|
|
if (!SymbolName) {
|
2016-04-21 05:24:34 +08:00
|
|
|
consumeError(SymbolName.takeError());
|
2015-09-12 07:45:30 +08:00
|
|
|
Linker.reportWarning("error getting relocation symbol name.");
|
2015-02-14 07:18:22 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-07-03 04:55:21 +08:00
|
|
|
if (const auto *Mapping = DMO.lookupSymbol(*SymbolName))
|
2015-02-14 07:18:22 +08:00
|
|
|
ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
|
2016-02-01 11:44:22 +08:00
|
|
|
} else if (const auto *Mapping = DMO.lookupObjectAddress(SymAddress)) {
|
2015-02-14 07:18:22 +08:00
|
|
|
// Do not store the addend. The addend was the address of the
|
|
|
|
// symbol in the object file, the address in the binary that is
|
|
|
|
// stored in the debug map doesn't need to be offseted.
|
2016-02-01 11:44:22 +08:00
|
|
|
ValidRelocs.emplace_back(Offset64, RelocSize, SymOffset, Mapping);
|
2015-02-14 07:18:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Dispatch the valid relocation finding logic to the
|
2015-02-14 07:18:22 +08:00
|
|
|
/// appropriate handler depending on the object file format.
|
2015-09-12 07:45:30 +08:00
|
|
|
bool DwarfLinker::RelocationManager::findValidRelocs(
|
|
|
|
const object::SectionRef &Section, const object::ObjectFile &Obj,
|
|
|
|
const DebugMapObject &DMO) {
|
2015-02-14 07:18:22 +08:00
|
|
|
// Dispatch to the right handler depending on the file type.
|
|
|
|
if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
|
|
|
|
findValidRelocsMachO(Section, *MachOObj, DMO);
|
|
|
|
else
|
2015-09-12 07:45:30 +08:00
|
|
|
Linker.reportWarning(Twine("unsupported object file type: ") +
|
|
|
|
Obj.getFileName());
|
2015-02-14 07:18:22 +08:00
|
|
|
|
|
|
|
if (ValidRelocs.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Sort the relocations by offset. We will walk the DIEs linearly in
|
|
|
|
// the file, this allows us to just keep an index in the relocation
|
|
|
|
// array that we advance during our walk, rather than resorting to
|
|
|
|
// some associative container. See DwarfLinker::NextValidReloc.
|
|
|
|
std::sort(ValidRelocs.begin(), ValidRelocs.end());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Look for relocations in the debug_info section that match
|
2015-02-14 07:18:22 +08:00
|
|
|
/// entries in the debug map. These relocations will drive the Dwarf
|
|
|
|
/// link by indicating which DIEs refer to symbols present in the
|
|
|
|
/// linked binary.
|
|
|
|
/// \returns wether there are any valid relocations in the debug info.
|
2015-09-12 07:45:30 +08:00
|
|
|
bool DwarfLinker::RelocationManager::
|
|
|
|
findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
|
|
|
|
const DebugMapObject &DMO) {
|
2015-02-14 07:18:22 +08:00
|
|
|
// Find the debug_info section.
|
|
|
|
for (const object::SectionRef &Section : Obj.sections()) {
|
|
|
|
StringRef SectionName;
|
|
|
|
Section.getName(SectionName);
|
|
|
|
SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
|
|
|
|
if (SectionName != "debug_info")
|
|
|
|
continue;
|
|
|
|
return findValidRelocs(Section, Obj, DMO);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-29 06:15:14 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Checks that there is a relocation against an actual debug
|
2015-02-14 07:18:34 +08:00
|
|
|
/// map entry between \p StartOffset and \p NextOffset.
|
|
|
|
///
|
|
|
|
/// This function must be called with offsets in strictly ascending
|
|
|
|
/// order because it never looks back at relocations it already 'went past'.
|
|
|
|
/// \returns true and sets Info.InDebugMap if it is the case.
|
2015-09-12 07:45:30 +08:00
|
|
|
bool DwarfLinker::RelocationManager::
|
|
|
|
hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
|
|
|
|
CompileUnit::DIEInfo &Info) {
|
2015-02-14 07:18:34 +08:00
|
|
|
assert(NextValidReloc == 0 ||
|
|
|
|
StartOffset > ValidRelocs[NextValidReloc - 1].Offset);
|
|
|
|
if (NextValidReloc >= ValidRelocs.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
|
|
|
|
|
|
|
|
// We might need to skip some relocs that we didn't consider. For
|
|
|
|
// example the high_pc of a discarded DIE might contain a reloc that
|
|
|
|
// is in the list because it actually corresponds to the start of a
|
|
|
|
// function that is in the debug map.
|
|
|
|
while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1)
|
|
|
|
RelocOffset = ValidRelocs[++NextValidReloc].Offset;
|
|
|
|
|
|
|
|
if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto &ValidReloc = ValidRelocs[NextValidReloc++];
|
2015-06-02 05:12:45 +08:00
|
|
|
const auto &Mapping = ValidReloc.Mapping->getValue();
|
2016-01-31 12:29:22 +08:00
|
|
|
uint64_t ObjectAddress =
|
|
|
|
Mapping.ObjectAddress ? uint64_t(*Mapping.ObjectAddress) : UINT64_MAX;
|
2015-09-12 07:45:30 +08:00
|
|
|
if (Linker.Options.Verbose)
|
2015-02-14 07:18:34 +08:00
|
|
|
outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey()
|
2016-01-31 12:29:22 +08:00
|
|
|
<< " " << format("\t%016" PRIx64 " => %016" PRIx64, ObjectAddress,
|
2015-06-02 05:12:45 +08:00
|
|
|
uint64_t(Mapping.BinaryAddress));
|
2015-02-14 07:18:34 +08:00
|
|
|
|
2016-01-31 12:29:22 +08:00
|
|
|
Info.AddrAdjust = int64_t(Mapping.BinaryAddress) + ValidReloc.Addend;
|
|
|
|
if (Mapping.ObjectAddress)
|
|
|
|
Info.AddrAdjust -= ObjectAddress;
|
2015-02-14 07:18:34 +08:00
|
|
|
Info.InDebugMap = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Get the starting and ending (exclusive) offset for the
|
2015-02-14 07:18:34 +08:00
|
|
|
/// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
|
|
|
|
/// supposed to point to the position of the first attribute described
|
|
|
|
/// by \p Abbrev.
|
|
|
|
/// \return [StartOffset, EndOffset) as a pair.
|
|
|
|
static std::pair<uint32_t, uint32_t>
|
|
|
|
getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
|
|
|
|
unsigned Offset, const DWARFUnit &Unit) {
|
|
|
|
DataExtractor Data = Unit.getDebugInfoExtractor();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < Idx; ++i)
|
2017-06-27 02:43:01 +08:00
|
|
|
DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset,
|
|
|
|
Unit.getFormParams());
|
2015-02-14 07:18:34 +08:00
|
|
|
|
|
|
|
uint32_t End = Offset;
|
2017-06-27 02:43:01 +08:00
|
|
|
DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End,
|
|
|
|
Unit.getFormParams());
|
2015-02-14 07:18:34 +08:00
|
|
|
|
|
|
|
return std::make_pair(Offset, End);
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Check if a variable describing DIE should be kept.
|
2015-02-14 07:18:34 +08:00
|
|
|
/// \returns updated TraversalFlags.
|
2015-09-12 07:45:30 +08:00
|
|
|
unsigned DwarfLinker::shouldKeepVariableDIE(RelocationManager &RelocMgr,
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &DIE,
|
2015-09-12 07:45:30 +08:00
|
|
|
CompileUnit &Unit,
|
|
|
|
CompileUnit::DIEInfo &MyInfo,
|
|
|
|
unsigned Flags) {
|
2015-02-14 07:18:34 +08:00
|
|
|
const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
|
|
|
|
|
|
|
|
// Global variables with constant value can always be kept.
|
|
|
|
if (!(Flags & TF_InFunctionScope) &&
|
2016-11-15 09:23:06 +08:00
|
|
|
Abbrev->findAttributeIndex(dwarf::DW_AT_const_value)) {
|
2015-02-14 07:18:34 +08:00
|
|
|
MyInfo.InDebugMap = true;
|
|
|
|
return Flags | TF_Keep;
|
|
|
|
}
|
|
|
|
|
2016-11-15 09:23:06 +08:00
|
|
|
Optional<uint32_t> LocationIdx =
|
|
|
|
Abbrev->findAttributeIndex(dwarf::DW_AT_location);
|
|
|
|
if (!LocationIdx)
|
2015-02-14 07:18:34 +08:00
|
|
|
return Flags;
|
|
|
|
|
|
|
|
uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
|
|
|
|
const DWARFUnit &OrigUnit = Unit.getOrigUnit();
|
|
|
|
uint32_t LocationOffset, LocationEndOffset;
|
|
|
|
std::tie(LocationOffset, LocationEndOffset) =
|
2016-11-15 09:23:06 +08:00
|
|
|
getAttributeOffsets(Abbrev, *LocationIdx, Offset, OrigUnit);
|
2015-02-14 07:18:34 +08:00
|
|
|
|
|
|
|
// See if there is a relocation to a valid debug map entry inside
|
|
|
|
// this variable's location. The order is important here. We want to
|
|
|
|
// always check in the variable has a valid relocation, so that the
|
|
|
|
// DIEInfo is filled. However, we don't want a static variable in a
|
|
|
|
// function to force us to keep the enclosing function.
|
2015-09-12 07:45:30 +08:00
|
|
|
if (!RelocMgr.hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) ||
|
2015-02-14 07:18:34 +08:00
|
|
|
(Flags & TF_InFunctionScope))
|
|
|
|
return Flags;
|
|
|
|
|
2017-09-13 17:43:05 +08:00
|
|
|
if (Options.Verbose) {
|
|
|
|
DIDumpOptions DumpOpts;
|
2017-09-21 01:44:00 +08:00
|
|
|
DumpOpts.RecurseDepth = 0;
|
2017-09-13 17:43:05 +08:00
|
|
|
DumpOpts.Verbose = Options.Verbose;
|
2017-09-21 01:44:00 +08:00
|
|
|
DIE.dump(outs(), 8 /* Indent */, DumpOpts);
|
2017-09-13 17:43:05 +08:00
|
|
|
}
|
2015-02-14 07:18:34 +08:00
|
|
|
|
|
|
|
return Flags | TF_Keep;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Check if a function describing DIE should be kept.
|
2015-02-14 07:18:34 +08:00
|
|
|
/// \returns updated TraversalFlags.
|
|
|
|
unsigned DwarfLinker::shouldKeepSubprogramDIE(
|
2015-09-12 07:45:30 +08:00
|
|
|
RelocationManager &RelocMgr,
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &DIE, CompileUnit &Unit,
|
2015-02-14 07:18:34 +08:00
|
|
|
CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
|
|
|
|
const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
|
|
|
|
|
|
|
|
Flags |= TF_InFunctionScope;
|
|
|
|
|
2016-11-15 09:23:06 +08:00
|
|
|
Optional<uint32_t> LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
|
|
|
|
if (!LowPcIdx)
|
2015-02-14 07:18:34 +08:00
|
|
|
return Flags;
|
|
|
|
|
|
|
|
uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
|
|
|
|
const DWARFUnit &OrigUnit = Unit.getOrigUnit();
|
|
|
|
uint32_t LowPcOffset, LowPcEndOffset;
|
|
|
|
std::tie(LowPcOffset, LowPcEndOffset) =
|
2016-11-15 09:23:06 +08:00
|
|
|
getAttributeOffsets(Abbrev, *LowPcIdx, Offset, OrigUnit);
|
2015-02-14 07:18:34 +08:00
|
|
|
|
2017-01-14 05:08:18 +08:00
|
|
|
auto LowPc = dwarf::toAddress(DIE.find(dwarf::DW_AT_low_pc));
|
2016-12-15 06:38:08 +08:00
|
|
|
assert(LowPc.hasValue() && "low_pc attribute is not an address.");
|
|
|
|
if (!LowPc ||
|
2015-09-12 07:45:30 +08:00
|
|
|
!RelocMgr.hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo))
|
2015-02-14 07:18:34 +08:00
|
|
|
return Flags;
|
|
|
|
|
2017-09-13 17:43:05 +08:00
|
|
|
if (Options.Verbose) {
|
|
|
|
DIDumpOptions DumpOpts;
|
2017-09-21 01:44:00 +08:00
|
|
|
DumpOpts.RecurseDepth = 0;
|
2017-09-13 17:43:05 +08:00
|
|
|
DumpOpts.Verbose = Options.Verbose;
|
2017-09-21 01:44:00 +08:00
|
|
|
DIE.dump(outs(), 8 /* Indent */, DumpOpts);
|
2017-09-13 17:43:05 +08:00
|
|
|
}
|
2015-02-14 07:18:34 +08:00
|
|
|
|
2015-03-13 02:45:10 +08:00
|
|
|
Flags |= TF_Keep;
|
|
|
|
|
2016-12-20 04:36:41 +08:00
|
|
|
Optional<uint64_t> HighPc = DIE.getHighPC(*LowPc);
|
|
|
|
if (!HighPc) {
|
2015-03-13 02:45:10 +08:00
|
|
|
reportWarning("Function without high_pc. Range will be discarded.\n",
|
2016-12-14 02:25:19 +08:00
|
|
|
&DIE);
|
2015-03-13 02:45:10 +08:00
|
|
|
return Flags;
|
|
|
|
}
|
|
|
|
|
2015-03-16 04:45:43 +08:00
|
|
|
// Replace the debug map range with a more accurate one.
|
2016-12-20 04:36:41 +08:00
|
|
|
Ranges[*LowPc] = std::make_pair(*HighPc, MyInfo.AddrAdjust);
|
|
|
|
Unit.addFunctionRange(*LowPc, *HighPc, MyInfo.AddrAdjust);
|
2015-03-13 02:45:10 +08:00
|
|
|
return Flags;
|
2015-02-14 07:18:34 +08:00
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Check if a DIE should be kept.
|
2015-02-14 07:18:34 +08:00
|
|
|
/// \returns updated TraversalFlags.
|
2015-09-12 07:45:30 +08:00
|
|
|
unsigned DwarfLinker::shouldKeepDIE(RelocationManager &RelocMgr,
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &DIE,
|
2015-02-14 07:18:34 +08:00
|
|
|
CompileUnit &Unit,
|
|
|
|
CompileUnit::DIEInfo &MyInfo,
|
|
|
|
unsigned Flags) {
|
|
|
|
switch (DIE.getTag()) {
|
|
|
|
case dwarf::DW_TAG_constant:
|
|
|
|
case dwarf::DW_TAG_variable:
|
2015-09-12 07:45:30 +08:00
|
|
|
return shouldKeepVariableDIE(RelocMgr, DIE, Unit, MyInfo, Flags);
|
2015-02-14 07:18:34 +08:00
|
|
|
case dwarf::DW_TAG_subprogram:
|
2015-09-12 07:45:30 +08:00
|
|
|
return shouldKeepSubprogramDIE(RelocMgr, DIE, Unit, MyInfo, Flags);
|
2015-02-14 07:18:34 +08:00
|
|
|
case dwarf::DW_TAG_imported_module:
|
|
|
|
case dwarf::DW_TAG_imported_declaration:
|
|
|
|
case dwarf::DW_TAG_imported_unit:
|
|
|
|
// We always want to keep these.
|
|
|
|
return Flags | TF_Keep;
|
2016-12-02 02:56:29 +08:00
|
|
|
default:
|
|
|
|
break;
|
2015-02-14 07:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Flags;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Mark the passed DIE as well as all the ones it depends on
|
2015-02-14 07:18:34 +08:00
|
|
|
/// as kept.
|
|
|
|
///
|
|
|
|
/// This function is called by lookForDIEsToKeep on DIEs that are
|
|
|
|
/// newly discovered to be needed in the link. It recursively calls
|
|
|
|
/// back to lookForDIEsToKeep while adding TF_DependencyWalk to the
|
|
|
|
/// TraversalFlags to inform it that it's not doing the primary DIE
|
|
|
|
/// tree walk.
|
2015-09-22 23:31:14 +08:00
|
|
|
void DwarfLinker::keepDIEAndDependencies(RelocationManager &RelocMgr,
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &Die,
|
2015-02-14 07:18:34 +08:00
|
|
|
CompileUnit::DIEInfo &MyInfo,
|
|
|
|
const DebugMapObject &DMO,
|
2015-07-22 06:41:43 +08:00
|
|
|
CompileUnit &CU, bool UseODR) {
|
2016-12-14 02:25:19 +08:00
|
|
|
DWARFUnit &Unit = CU.getOrigUnit();
|
2015-02-14 07:18:34 +08:00
|
|
|
MyInfo.Keep = true;
|
|
|
|
|
2017-09-01 04:22:31 +08:00
|
|
|
// We're looking for incomplete types.
|
|
|
|
MyInfo.Incomplete = Die.getTag() != dwarf::DW_TAG_subprogram &&
|
|
|
|
Die.getTag() != dwarf::DW_TAG_member &&
|
|
|
|
dwarf::toUnsigned(Die.find(dwarf::DW_AT_declaration), 0);
|
|
|
|
|
2015-02-14 07:18:34 +08:00
|
|
|
// First mark all the parent chain as kept.
|
|
|
|
unsigned AncestorIdx = MyInfo.ParentIdx;
|
|
|
|
while (!CU.getInfo(AncestorIdx).Keep) {
|
2015-07-22 06:41:43 +08:00
|
|
|
unsigned ODRFlag = UseODR ? TF_ODR : 0;
|
2016-12-14 02:25:19 +08:00
|
|
|
lookForDIEsToKeep(RelocMgr, Unit.getDIEAtIndex(AncestorIdx), DMO, CU,
|
2015-07-22 06:41:43 +08:00
|
|
|
TF_ParentWalk | TF_Keep | TF_DependencyWalk | ODRFlag);
|
2015-02-14 07:18:34 +08:00
|
|
|
AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then we need to mark all the DIEs referenced by this DIE's
|
|
|
|
// attributes as kept.
|
2017-06-30 00:52:08 +08:00
|
|
|
DWARFDataExtractor Data = Unit.getDebugInfoExtractor();
|
2015-09-11 12:17:25 +08:00
|
|
|
const auto *Abbrev = Die.getAbbreviationDeclarationPtr();
|
|
|
|
uint32_t Offset = Die.getOffset() + getULEB128Size(Abbrev->getCode());
|
2015-02-14 07:18:34 +08:00
|
|
|
|
2017-09-01 04:22:31 +08:00
|
|
|
// Mark all DIEs referenced through attributes as kept.
|
2015-02-14 07:18:34 +08:00
|
|
|
for (const auto &AttrSpec : Abbrev->attributes()) {
|
|
|
|
DWARFFormValue Val(AttrSpec.Form);
|
|
|
|
|
|
|
|
if (!Val.isFormClass(DWARFFormValue::FC_Reference)) {
|
2017-06-27 02:43:01 +08:00
|
|
|
DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
|
|
|
|
Unit.getFormParams());
|
2015-02-14 07:18:34 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Val.extractValue(Data, &Offset, &Unit);
|
|
|
|
CompileUnit *ReferencedCU;
|
2017-07-22 00:51:09 +08:00
|
|
|
if (auto RefDie =
|
2016-12-02 02:56:29 +08:00
|
|
|
resolveDIEReference(*this, Units, Val, Unit, Die, ReferencedCU)) {
|
2017-07-22 00:51:09 +08:00
|
|
|
uint32_t RefIdx = ReferencedCU->getOrigUnit().getDIEIndex(RefDie);
|
2015-07-22 06:41:43 +08:00
|
|
|
CompileUnit::DIEInfo &Info = ReferencedCU->getInfo(RefIdx);
|
2017-07-21 10:07:33 +08:00
|
|
|
bool IsModuleRef = Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() &&
|
|
|
|
Info.Ctxt->isDefinedInClangModule();
|
2015-07-22 06:41:43 +08:00
|
|
|
// If the referenced DIE has a DeclContext that has already been
|
|
|
|
// emitted, then do not keep the one in this CU. We'll link to
|
|
|
|
// the canonical DIE in cloneDieReferenceAttribute.
|
|
|
|
// FIXME: compatibility with dsymutil-classic. UseODR shouldn't
|
|
|
|
// be necessary and could be advantageously replaced by
|
|
|
|
// ReferencedCU->hasODR() && CU.hasODR().
|
|
|
|
// FIXME: compatibility with dsymutil-classic. There is no
|
|
|
|
// reason not to unique ref_addr references.
|
2017-07-21 10:07:33 +08:00
|
|
|
if (AttrSpec.Form != dwarf::DW_FORM_ref_addr && (UseODR || IsModuleRef) &&
|
|
|
|
Info.Ctxt &&
|
2015-07-22 06:41:43 +08:00
|
|
|
Info.Ctxt != ReferencedCU->getInfo(Info.ParentIdx).Ctxt &&
|
|
|
|
Info.Ctxt->getCanonicalDIEOffset() && isODRAttribute(AttrSpec.Attr))
|
|
|
|
continue;
|
|
|
|
|
2015-11-11 05:31:05 +08:00
|
|
|
// Keep a module forward declaration if there is no definition.
|
|
|
|
if (!(isODRAttribute(AttrSpec.Attr) && Info.Ctxt &&
|
|
|
|
Info.Ctxt->getCanonicalDIEOffset()))
|
|
|
|
Info.Prune = false;
|
|
|
|
|
2015-07-22 06:41:43 +08:00
|
|
|
unsigned ODRFlag = UseODR ? TF_ODR : 0;
|
2017-07-22 00:51:09 +08:00
|
|
|
lookForDIEsToKeep(RelocMgr, RefDie, DMO, *ReferencedCU,
|
2015-07-22 06:41:43 +08:00
|
|
|
TF_Keep | TF_DependencyWalk | ODRFlag);
|
2017-09-01 04:22:31 +08:00
|
|
|
|
|
|
|
// The incomplete property is propagated if the current DIE is complete
|
|
|
|
// but references an incomplete DIE.
|
|
|
|
if (Info.Incomplete && !MyInfo.Incomplete &&
|
|
|
|
(Die.getTag() == dwarf::DW_TAG_typedef ||
|
|
|
|
Die.getTag() == dwarf::DW_TAG_member ||
|
|
|
|
Die.getTag() == dwarf::DW_TAG_reference_type ||
|
|
|
|
Die.getTag() == dwarf::DW_TAG_ptr_to_member_type ||
|
|
|
|
Die.getTag() == dwarf::DW_TAG_pointer_type))
|
|
|
|
MyInfo.Incomplete = true;
|
2015-07-22 06:41:43 +08:00
|
|
|
}
|
2015-02-14 07:18:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Recursively walk the \p DIE tree and look for DIEs to
|
2015-02-14 07:18:34 +08:00
|
|
|
/// keep. Store that information in \p CU's DIEInfo.
|
|
|
|
///
|
|
|
|
/// This function is the entry point of the DIE selection
|
|
|
|
/// algorithm. It is expected to walk the DIE tree in file order and
|
|
|
|
/// (though the mediation of its helper) call hasValidRelocation() on
|
|
|
|
/// each DIE that might be a 'root DIE' (See DwarfLinker class
|
|
|
|
/// comment).
|
|
|
|
/// While walking the dependencies of root DIEs, this function is
|
|
|
|
/// also called, but during these dependency walks the file order is
|
|
|
|
/// not respected. The TF_DependencyWalk flag tells us which kind of
|
|
|
|
/// traversal we are currently doing.
|
2017-09-01 04:22:31 +08:00
|
|
|
///
|
|
|
|
/// The return value indicates whether the DIE is incomplete.
|
|
|
|
bool DwarfLinker::lookForDIEsToKeep(RelocationManager &RelocMgr,
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &Die,
|
2015-02-14 07:18:34 +08:00
|
|
|
const DebugMapObject &DMO, CompileUnit &CU,
|
|
|
|
unsigned Flags) {
|
2016-12-14 02:25:19 +08:00
|
|
|
unsigned Idx = CU.getOrigUnit().getDIEIndex(Die);
|
2015-02-14 07:18:34 +08:00
|
|
|
CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
|
|
|
|
bool AlreadyKept = MyInfo.Keep;
|
2015-09-24 01:35:52 +08:00
|
|
|
if (MyInfo.Prune)
|
2017-09-01 04:22:31 +08:00
|
|
|
return true;
|
2015-02-14 07:18:34 +08:00
|
|
|
|
|
|
|
// If the Keep flag is set, we are marking a required DIE's
|
|
|
|
// dependencies. If our target is already marked as kept, we're all
|
|
|
|
// set.
|
|
|
|
if ((Flags & TF_DependencyWalk) && AlreadyKept)
|
2017-09-01 04:22:31 +08:00
|
|
|
return MyInfo.Incomplete;
|
2015-02-14 07:18:34 +08:00
|
|
|
|
2015-09-22 23:31:14 +08:00
|
|
|
// We must not call shouldKeepDIE while called from keepDIEAndDependencies,
|
2015-02-14 07:18:34 +08:00
|
|
|
// because it would screw up the relocation finding logic.
|
|
|
|
if (!(Flags & TF_DependencyWalk))
|
2015-09-12 07:45:30 +08:00
|
|
|
Flags = shouldKeepDIE(RelocMgr, Die, CU, MyInfo, Flags);
|
2015-02-14 07:18:34 +08:00
|
|
|
|
|
|
|
// If it is a newly kept DIE mark it as well as all its dependencies as kept.
|
2015-07-22 06:41:43 +08:00
|
|
|
if (!AlreadyKept && (Flags & TF_Keep)) {
|
|
|
|
bool UseOdr = (Flags & TF_DependencyWalk) ? (Flags & TF_ODR) : CU.hasODR();
|
2015-09-22 23:31:14 +08:00
|
|
|
keepDIEAndDependencies(RelocMgr, Die, MyInfo, DMO, CU, UseOdr);
|
2015-07-22 06:41:43 +08:00
|
|
|
}
|
2015-02-14 07:18:34 +08:00
|
|
|
// The TF_ParentWalk flag tells us that we are currently walking up
|
|
|
|
// the parent chain of a required DIE, and we don't want to mark all
|
|
|
|
// the children of the parents as kept (consider for example a
|
|
|
|
// DW_TAG_namespace node in the parent chain). There are however a
|
|
|
|
// set of DIE types for which we want to ignore that directive and still
|
|
|
|
// walk their children.
|
2015-09-11 12:17:25 +08:00
|
|
|
if (dieNeedsChildrenToBeMeaningful(Die.getTag()))
|
2015-02-14 07:18:34 +08:00
|
|
|
Flags &= ~TF_ParentWalk;
|
|
|
|
|
2015-09-11 12:17:25 +08:00
|
|
|
if (!Die.hasChildren() || (Flags & TF_ParentWalk))
|
2017-09-01 04:22:31 +08:00
|
|
|
return MyInfo.Incomplete;
|
2017-09-01 02:06:44 +08:00
|
|
|
|
2017-09-01 04:22:31 +08:00
|
|
|
bool Incomplete = false;
|
|
|
|
for (auto Child : Die.children()) {
|
|
|
|
Incomplete |= lookForDIEsToKeep(RelocMgr, Child, DMO, CU, Flags);
|
|
|
|
|
|
|
|
// If any of the members are incomplete we propagate the incompleteness.
|
|
|
|
if (!MyInfo.Incomplete && Incomplete &&
|
|
|
|
(Die.getTag() == dwarf::DW_TAG_structure_type ||
|
|
|
|
Die.getTag() == dwarf::DW_TAG_class_type))
|
|
|
|
MyInfo.Incomplete = true;
|
|
|
|
}
|
|
|
|
return MyInfo.Incomplete;
|
2015-02-14 07:18:34 +08:00
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Assign an abbreviation numer to \p Abbrev.
|
2015-03-05 06:07:44 +08:00
|
|
|
///
|
|
|
|
/// Our DIEs get freed after every DebugMapObject has been processed,
|
|
|
|
/// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to
|
|
|
|
/// the instances hold by the DIEs. When we encounter an abbreviation
|
|
|
|
/// that we don't know, we create a permanent copy of it.
|
|
|
|
void DwarfLinker::AssignAbbrev(DIEAbbrev &Abbrev) {
|
|
|
|
// Check the set for priors.
|
|
|
|
FoldingSetNodeID ID;
|
|
|
|
Abbrev.Profile(ID);
|
|
|
|
void *InsertToken;
|
|
|
|
DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
|
|
|
|
|
|
|
|
// If it's newly added.
|
|
|
|
if (InSet) {
|
|
|
|
// Assign existing abbreviation number.
|
|
|
|
Abbrev.setNumber(InSet->getNumber());
|
|
|
|
} else {
|
|
|
|
// Add to abbreviation list.
|
|
|
|
Abbreviations.push_back(
|
2015-11-18 08:34:10 +08:00
|
|
|
llvm::make_unique<DIEAbbrev>(Abbrev.getTag(), Abbrev.hasChildren()));
|
2015-03-05 06:07:44 +08:00
|
|
|
for (const auto &Attr : Abbrev.getData())
|
|
|
|
Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm());
|
2015-11-18 08:34:10 +08:00
|
|
|
AbbreviationsSet.InsertNode(Abbreviations.back().get(), InsertToken);
|
2015-03-05 06:07:44 +08:00
|
|
|
// Assign the unique abbreviation number.
|
|
|
|
Abbrev.setNumber(Abbreviations.size());
|
|
|
|
Abbreviations.back()->setNumber(Abbreviations.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:46:10 +08:00
|
|
|
unsigned DwarfLinker::DIECloner::cloneStringAttribute(DIE &Die,
|
|
|
|
AttributeSpec AttrSpec,
|
|
|
|
const DWARFFormValue &Val,
|
|
|
|
const DWARFUnit &U) {
|
2015-03-05 06:07:44 +08:00
|
|
|
// Switch everything to out of line strings.
|
2016-11-01 00:46:02 +08:00
|
|
|
const char *String = *Val.getAsCString();
|
2015-09-15 00:46:10 +08:00
|
|
|
unsigned Offset = Linker.StringPool.getStringOffset(String);
|
2015-06-26 07:46:41 +08:00
|
|
|
Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp,
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
DIEInteger(Offset));
|
2015-03-05 06:07:44 +08:00
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:46:10 +08:00
|
|
|
unsigned DwarfLinker::DIECloner::cloneDieReferenceAttribute(
|
2016-12-14 02:25:19 +08:00
|
|
|
DIE &Die, const DWARFDie &InputDIE,
|
2015-03-07 07:22:53 +08:00
|
|
|
AttributeSpec AttrSpec, unsigned AttrSize, const DWARFFormValue &Val,
|
2015-03-14 02:35:57 +08:00
|
|
|
CompileUnit &Unit) {
|
2015-07-22 06:41:43 +08:00
|
|
|
const DWARFUnit &U = Unit.getOrigUnit();
|
2016-11-01 00:46:02 +08:00
|
|
|
uint32_t Ref = *Val.getAsReference();
|
2015-03-07 07:22:53 +08:00
|
|
|
DIE *NewRefDie = nullptr;
|
|
|
|
CompileUnit *RefUnit = nullptr;
|
2015-07-22 06:41:43 +08:00
|
|
|
DeclContext *Ctxt = nullptr;
|
|
|
|
|
2016-12-14 02:25:19 +08:00
|
|
|
DWARFDie RefDie = resolveDIEReference(Linker, CompileUnits, Val, U, InputDIE,
|
|
|
|
RefUnit);
|
2015-07-22 06:41:43 +08:00
|
|
|
|
|
|
|
// If the referenced DIE is not found, drop the attribute.
|
|
|
|
if (!RefDie)
|
2015-03-07 07:22:53 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
unsigned Idx = RefUnit->getOrigUnit().getDIEIndex(RefDie);
|
|
|
|
CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(Idx);
|
2015-07-22 06:41:43 +08:00
|
|
|
|
|
|
|
// If we already have emitted an equivalent DeclContext, just point
|
|
|
|
// at it.
|
|
|
|
if (isODRAttribute(AttrSpec.Attr)) {
|
|
|
|
Ctxt = RefInfo.Ctxt;
|
|
|
|
if (Ctxt && Ctxt->getCanonicalDIEOffset()) {
|
|
|
|
DIEInteger Attr(Ctxt->getCanonicalDIEOffset());
|
|
|
|
Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
|
|
|
|
dwarf::DW_FORM_ref_addr, Attr);
|
2016-11-01 00:46:02 +08:00
|
|
|
return U.getRefAddrByteSize();
|
2015-07-22 06:41:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-07 07:22:53 +08:00
|
|
|
if (!RefInfo.Clone) {
|
|
|
|
assert(Ref > InputDIE.getOffset());
|
|
|
|
// We haven't cloned this DIE yet. Just create an empty one and
|
|
|
|
// store it. It'll get really cloned when we process it.
|
2016-12-14 02:25:19 +08:00
|
|
|
RefInfo.Clone = DIE::get(DIEAlloc, dwarf::Tag(RefDie.getTag()));
|
2015-03-07 07:22:53 +08:00
|
|
|
}
|
|
|
|
NewRefDie = RefInfo.Clone;
|
|
|
|
|
2015-07-22 06:41:43 +08:00
|
|
|
if (AttrSpec.Form == dwarf::DW_FORM_ref_addr ||
|
|
|
|
(Unit.hasODR() && isODRAttribute(AttrSpec.Attr))) {
|
2015-03-07 07:22:53 +08:00
|
|
|
// We cannot currently rely on a DIEEntry to emit ref_addr
|
|
|
|
// references, because the implementation calls back to DwarfDebug
|
|
|
|
// to find the unit offset. (We don't have a DwarfDebug)
|
|
|
|
// FIXME: we should be able to design DIEEntry reliance on
|
|
|
|
// DwarfDebug away.
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
uint64_t Attr;
|
2015-03-07 07:22:53 +08:00
|
|
|
if (Ref < InputDIE.getOffset()) {
|
|
|
|
// We must have already cloned that DIE.
|
|
|
|
uint32_t NewRefOffset =
|
|
|
|
RefUnit->getStartOffset() + NewRefDie->getOffset();
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
Attr = NewRefOffset;
|
2015-06-26 07:46:41 +08:00
|
|
|
Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
|
|
|
|
dwarf::DW_FORM_ref_addr, DIEInteger(Attr));
|
2015-03-07 07:22:53 +08:00
|
|
|
} else {
|
|
|
|
// A forward reference. Note and fixup later.
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
Attr = 0xBADDEF;
|
2015-06-26 07:46:41 +08:00
|
|
|
Unit.noteForwardReference(
|
2015-07-22 06:41:43 +08:00
|
|
|
NewRefDie, RefUnit, Ctxt,
|
2015-06-26 07:46:41 +08:00
|
|
|
Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
|
|
|
|
dwarf::DW_FORM_ref_addr, DIEInteger(Attr)));
|
2015-03-07 07:22:53 +08:00
|
|
|
}
|
2016-11-01 00:46:02 +08:00
|
|
|
return U.getRefAddrByteSize();
|
2015-03-07 07:22:53 +08:00
|
|
|
}
|
|
|
|
|
2015-06-26 07:46:41 +08:00
|
|
|
Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
|
|
|
|
dwarf::Form(AttrSpec.Form), DIEEntry(*NewRefDie));
|
2015-03-05 06:07:44 +08:00
|
|
|
return AttrSize;
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:46:10 +08:00
|
|
|
unsigned DwarfLinker::DIECloner::cloneBlockAttribute(DIE &Die,
|
|
|
|
AttributeSpec AttrSpec,
|
|
|
|
const DWARFFormValue &Val,
|
|
|
|
unsigned AttrSize) {
|
2015-08-03 04:48:47 +08:00
|
|
|
DIEValueList *Attr;
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
DIEValue Value;
|
2015-03-05 06:07:44 +08:00
|
|
|
DIELoc *Loc = nullptr;
|
|
|
|
DIEBlock *Block = nullptr;
|
|
|
|
// Just copy the block data over.
|
2015-03-14 02:35:39 +08:00
|
|
|
if (AttrSpec.Form == dwarf::DW_FORM_exprloc) {
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
Loc = new (DIEAlloc) DIELoc;
|
2015-09-15 00:46:10 +08:00
|
|
|
Linker.DIELocs.push_back(Loc);
|
2015-03-05 06:07:44 +08:00
|
|
|
} else {
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
Block = new (DIEAlloc) DIEBlock;
|
2015-09-15 00:46:10 +08:00
|
|
|
Linker.DIEBlocks.push_back(Block);
|
2015-03-05 06:07:44 +08:00
|
|
|
}
|
2015-08-03 04:48:47 +08:00
|
|
|
Attr = Loc ? static_cast<DIEValueList *>(Loc)
|
|
|
|
: static_cast<DIEValueList *>(Block);
|
2015-05-28 06:31:41 +08:00
|
|
|
|
|
|
|
if (Loc)
|
|
|
|
Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
|
|
|
|
dwarf::Form(AttrSpec.Form), Loc);
|
|
|
|
else
|
|
|
|
Value = DIEValue(dwarf::Attribute(AttrSpec.Attr),
|
|
|
|
dwarf::Form(AttrSpec.Form), Block);
|
2015-03-05 06:07:44 +08:00
|
|
|
ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
|
|
|
|
for (auto Byte : Bytes)
|
2015-06-26 07:46:41 +08:00
|
|
|
Attr->addValue(DIEAlloc, static_cast<dwarf::Attribute>(0),
|
|
|
|
dwarf::DW_FORM_data1, DIEInteger(Byte));
|
2015-03-05 06:07:44 +08:00
|
|
|
// FIXME: If DIEBlock and DIELoc just reuses the Size field of
|
|
|
|
// the DIE class, this if could be replaced by
|
|
|
|
// Attr->setSize(Bytes.size()).
|
2015-09-15 00:46:10 +08:00
|
|
|
if (Linker.Streamer) {
|
|
|
|
auto *AsmPrinter = &Linker.Streamer->getAsmPrinter();
|
2015-03-05 06:07:44 +08:00
|
|
|
if (Loc)
|
2015-09-15 00:46:10 +08:00
|
|
|
Loc->ComputeSize(AsmPrinter);
|
2015-03-05 06:07:44 +08:00
|
|
|
else
|
2015-09-15 00:46:10 +08:00
|
|
|
Block->ComputeSize(AsmPrinter);
|
2015-03-05 06:07:44 +08:00
|
|
|
}
|
2015-06-26 07:46:41 +08:00
|
|
|
Die.addValue(DIEAlloc, Value);
|
2015-03-05 06:07:44 +08:00
|
|
|
return AttrSize;
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:46:10 +08:00
|
|
|
unsigned DwarfLinker::DIECloner::cloneAddressAttribute(
|
|
|
|
DIE &Die, AttributeSpec AttrSpec, const DWARFFormValue &Val,
|
|
|
|
const CompileUnit &Unit, AttributesInfo &Info) {
|
2016-11-01 00:46:02 +08:00
|
|
|
uint64_t Addr = *Val.getAsAddress();
|
2015-03-12 02:45:52 +08:00
|
|
|
if (AttrSpec.Attr == dwarf::DW_AT_low_pc) {
|
|
|
|
if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine ||
|
|
|
|
Die.getTag() == dwarf::DW_TAG_lexical_block)
|
2015-08-31 09:43:14 +08:00
|
|
|
// The low_pc of a block or inline subroutine might get
|
|
|
|
// relocated because it happens to match the low_pc of the
|
|
|
|
// enclosing subprogram. To prevent issues with that, always use
|
|
|
|
// the low_pc from the input DIE if relocations have been applied.
|
|
|
|
Addr = (Info.OrigLowPc != UINT64_MAX ? Info.OrigLowPc : Addr) +
|
|
|
|
Info.PCOffset;
|
2015-03-14 02:35:54 +08:00
|
|
|
else if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
|
|
|
|
Addr = Unit.getLowPc();
|
|
|
|
if (Addr == UINT64_MAX)
|
|
|
|
return 0;
|
|
|
|
}
|
2015-03-16 10:05:10 +08:00
|
|
|
Info.HasLowPc = true;
|
2015-03-12 02:45:52 +08:00
|
|
|
} else if (AttrSpec.Attr == dwarf::DW_AT_high_pc) {
|
2015-03-14 02:35:54 +08:00
|
|
|
if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
|
|
|
|
if (uint64_t HighPc = Unit.getHighPc())
|
|
|
|
Addr = HighPc;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
// If we have a high_pc recorded for the input DIE, use
|
|
|
|
// it. Otherwise (when no relocations where applied) just use the
|
|
|
|
// one we just decoded.
|
|
|
|
Addr = (Info.OrigHighPc ? Info.OrigHighPc : Addr) + Info.PCOffset;
|
2015-03-12 02:45:52 +08:00
|
|
|
}
|
|
|
|
|
2015-06-26 07:46:41 +08:00
|
|
|
Die.addValue(DIEAlloc, static_cast<dwarf::Attribute>(AttrSpec.Attr),
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
static_cast<dwarf::Form>(AttrSpec.Form), DIEInteger(Addr));
|
2015-03-12 02:45:52 +08:00
|
|
|
return Unit.getOrigUnit().getAddressByteSize();
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:46:10 +08:00
|
|
|
unsigned DwarfLinker::DIECloner::cloneScalarAttribute(
|
2016-12-14 02:25:19 +08:00
|
|
|
DIE &Die, const DWARFDie &InputDIE, CompileUnit &Unit,
|
2015-03-14 23:49:07 +08:00
|
|
|
AttributeSpec AttrSpec, const DWARFFormValue &Val, unsigned AttrSize,
|
2015-03-16 10:05:10 +08:00
|
|
|
AttributesInfo &Info) {
|
2015-03-05 06:07:44 +08:00
|
|
|
uint64_t Value;
|
2015-03-14 02:35:54 +08:00
|
|
|
if (AttrSpec.Attr == dwarf::DW_AT_high_pc &&
|
|
|
|
Die.getTag() == dwarf::DW_TAG_compile_unit) {
|
|
|
|
if (Unit.getLowPc() == -1ULL)
|
|
|
|
return 0;
|
|
|
|
// Dwarf >= 4 high_pc is an size, not an address.
|
|
|
|
Value = Unit.getHighPc() - Unit.getLowPc();
|
|
|
|
} else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset)
|
2015-03-05 06:07:44 +08:00
|
|
|
Value = *Val.getAsSectionOffset();
|
|
|
|
else if (AttrSpec.Form == dwarf::DW_FORM_sdata)
|
|
|
|
Value = *Val.getAsSignedConstant();
|
|
|
|
else if (auto OptionalValue = Val.getAsUnsignedConstant())
|
|
|
|
Value = *OptionalValue;
|
|
|
|
else {
|
2015-09-15 00:46:10 +08:00
|
|
|
Linker.reportWarning(
|
|
|
|
"Unsupported scalar attribute form. Dropping attribute.",
|
2016-12-14 02:25:19 +08:00
|
|
|
&InputDIE);
|
2015-03-05 06:07:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-06-26 07:46:41 +08:00
|
|
|
PatchLocation Patch =
|
|
|
|
Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr),
|
|
|
|
dwarf::Form(AttrSpec.Form), DIEInteger(Value));
|
2015-03-14 07:30:31 +08:00
|
|
|
if (AttrSpec.Attr == dwarf::DW_AT_ranges)
|
2015-06-26 07:46:41 +08:00
|
|
|
Unit.noteRangeAttribute(Die, Patch);
|
2015-09-11 12:17:30 +08:00
|
|
|
|
2015-03-14 23:49:07 +08:00
|
|
|
// A more generic way to check for location attributes would be
|
|
|
|
// nice, but it's very unlikely that any other attribute needs a
|
|
|
|
// location list.
|
|
|
|
else if (AttrSpec.Attr == dwarf::DW_AT_location ||
|
|
|
|
AttrSpec.Attr == dwarf::DW_AT_frame_base)
|
2015-06-26 07:46:41 +08:00
|
|
|
Unit.noteLocationAttribute(Patch, Info.PCOffset);
|
2015-03-16 10:05:10 +08:00
|
|
|
else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
|
|
|
|
Info.IsDeclaration = true;
|
2015-03-14 23:49:07 +08:00
|
|
|
|
2015-03-05 06:07:44 +08:00
|
|
|
return AttrSize;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Clone \p InputDIE's attribute described by \p AttrSpec with
|
2015-03-05 06:07:44 +08:00
|
|
|
/// value \p Val, and add it to \p Die.
|
|
|
|
/// \returns the size of the cloned attribute.
|
2015-09-15 00:46:10 +08:00
|
|
|
unsigned DwarfLinker::DIECloner::cloneAttribute(
|
2016-12-14 02:25:19 +08:00
|
|
|
DIE &Die, const DWARFDie &InputDIE, CompileUnit &Unit,
|
2015-09-15 00:46:10 +08:00
|
|
|
const DWARFFormValue &Val, const AttributeSpec AttrSpec, unsigned AttrSize,
|
|
|
|
AttributesInfo &Info) {
|
2015-03-05 06:07:44 +08:00
|
|
|
const DWARFUnit &U = Unit.getOrigUnit();
|
|
|
|
|
|
|
|
switch (AttrSpec.Form) {
|
|
|
|
case dwarf::DW_FORM_strp:
|
|
|
|
case dwarf::DW_FORM_string:
|
2015-03-07 01:56:30 +08:00
|
|
|
return cloneStringAttribute(Die, AttrSpec, Val, U);
|
2015-03-05 06:07:44 +08:00
|
|
|
case dwarf::DW_FORM_ref_addr:
|
|
|
|
case dwarf::DW_FORM_ref1:
|
|
|
|
case dwarf::DW_FORM_ref2:
|
|
|
|
case dwarf::DW_FORM_ref4:
|
|
|
|
case dwarf::DW_FORM_ref8:
|
2015-03-07 07:22:53 +08:00
|
|
|
return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val,
|
2015-03-14 02:35:57 +08:00
|
|
|
Unit);
|
2015-03-05 06:07:44 +08:00
|
|
|
case dwarf::DW_FORM_block:
|
|
|
|
case dwarf::DW_FORM_block1:
|
|
|
|
case dwarf::DW_FORM_block2:
|
|
|
|
case dwarf::DW_FORM_block4:
|
|
|
|
case dwarf::DW_FORM_exprloc:
|
|
|
|
return cloneBlockAttribute(Die, AttrSpec, Val, AttrSize);
|
|
|
|
case dwarf::DW_FORM_addr:
|
2015-03-12 02:45:52 +08:00
|
|
|
return cloneAddressAttribute(Die, AttrSpec, Val, Unit, Info);
|
2015-03-05 06:07:44 +08:00
|
|
|
case dwarf::DW_FORM_data1:
|
|
|
|
case dwarf::DW_FORM_data2:
|
|
|
|
case dwarf::DW_FORM_data4:
|
|
|
|
case dwarf::DW_FORM_data8:
|
|
|
|
case dwarf::DW_FORM_udata:
|
|
|
|
case dwarf::DW_FORM_sdata:
|
|
|
|
case dwarf::DW_FORM_sec_offset:
|
|
|
|
case dwarf::DW_FORM_flag:
|
|
|
|
case dwarf::DW_FORM_flag_present:
|
2015-03-14 23:49:07 +08:00
|
|
|
return cloneScalarAttribute(Die, InputDIE, Unit, AttrSpec, Val, AttrSize,
|
|
|
|
Info);
|
2015-03-05 06:07:44 +08:00
|
|
|
default:
|
2015-09-15 00:46:10 +08:00
|
|
|
Linker.reportWarning(
|
2016-12-14 02:25:19 +08:00
|
|
|
"Unsupported attribute form in cloneAttribute. Dropping.", &InputDIE);
|
2015-03-05 06:07:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Apply the valid relocations found by findValidRelocs() to
|
2015-03-07 09:25:09 +08:00
|
|
|
/// the buffer \p Data, taking into account that Data is at \p BaseOffset
|
|
|
|
/// in the debug_info section.
|
|
|
|
///
|
|
|
|
/// Like for findValidRelocs(), this function must be called with
|
|
|
|
/// monotonic \p BaseOffset values.
|
|
|
|
///
|
|
|
|
/// \returns wether any reloc has been applied.
|
2015-09-12 07:45:30 +08:00
|
|
|
bool DwarfLinker::RelocationManager::
|
|
|
|
applyValidRelocs(MutableArrayRef<char> Data, uint32_t BaseOffset,
|
|
|
|
bool isLittleEndian) {
|
2015-03-07 23:16:27 +08:00
|
|
|
assert((NextValidReloc == 0 ||
|
2015-03-12 02:45:57 +08:00
|
|
|
BaseOffset > ValidRelocs[NextValidReloc - 1].Offset) &&
|
|
|
|
"BaseOffset should only be increasing.");
|
2015-03-07 09:25:09 +08:00
|
|
|
if (NextValidReloc >= ValidRelocs.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Skip relocs that haven't been applied.
|
|
|
|
while (NextValidReloc < ValidRelocs.size() &&
|
|
|
|
ValidRelocs[NextValidReloc].Offset < BaseOffset)
|
|
|
|
++NextValidReloc;
|
|
|
|
|
|
|
|
bool Applied = false;
|
|
|
|
uint64_t EndOffset = BaseOffset + Data.size();
|
|
|
|
while (NextValidReloc < ValidRelocs.size() &&
|
|
|
|
ValidRelocs[NextValidReloc].Offset >= BaseOffset &&
|
|
|
|
ValidRelocs[NextValidReloc].Offset < EndOffset) {
|
|
|
|
const auto &ValidReloc = ValidRelocs[NextValidReloc++];
|
|
|
|
assert(ValidReloc.Offset - BaseOffset < Data.size());
|
|
|
|
assert(ValidReloc.Offset - BaseOffset + ValidReloc.Size <= Data.size());
|
|
|
|
char Buf[8];
|
|
|
|
uint64_t Value = ValidReloc.Mapping->getValue().BinaryAddress;
|
|
|
|
Value += ValidReloc.Addend;
|
|
|
|
for (unsigned i = 0; i != ValidReloc.Size; ++i) {
|
|
|
|
unsigned Index = isLittleEndian ? i : (ValidReloc.Size - i - 1);
|
|
|
|
Buf[i] = uint8_t(Value >> (Index * 8));
|
|
|
|
}
|
|
|
|
assert(ValidReloc.Size <= sizeof(Buf));
|
|
|
|
memcpy(&Data[ValidReloc.Offset - BaseOffset], Buf, ValidReloc.Size);
|
|
|
|
Applied = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Applied;
|
|
|
|
}
|
|
|
|
|
2015-03-16 10:05:10 +08:00
|
|
|
static bool isTypeTag(uint16_t Tag) {
|
|
|
|
switch (Tag) {
|
|
|
|
case dwarf::DW_TAG_array_type:
|
|
|
|
case dwarf::DW_TAG_class_type:
|
|
|
|
case dwarf::DW_TAG_enumeration_type:
|
|
|
|
case dwarf::DW_TAG_pointer_type:
|
|
|
|
case dwarf::DW_TAG_reference_type:
|
|
|
|
case dwarf::DW_TAG_string_type:
|
|
|
|
case dwarf::DW_TAG_structure_type:
|
|
|
|
case dwarf::DW_TAG_subroutine_type:
|
|
|
|
case dwarf::DW_TAG_typedef:
|
|
|
|
case dwarf::DW_TAG_union_type:
|
|
|
|
case dwarf::DW_TAG_ptr_to_member_type:
|
|
|
|
case dwarf::DW_TAG_set_type:
|
|
|
|
case dwarf::DW_TAG_subrange_type:
|
|
|
|
case dwarf::DW_TAG_base_type:
|
|
|
|
case dwarf::DW_TAG_const_type:
|
|
|
|
case dwarf::DW_TAG_constant:
|
|
|
|
case dwarf::DW_TAG_file_type:
|
|
|
|
case dwarf::DW_TAG_namelist:
|
|
|
|
case dwarf::DW_TAG_packed_type:
|
|
|
|
case dwarf::DW_TAG_volatile_type:
|
|
|
|
case dwarf::DW_TAG_restrict_type:
|
2016-11-01 03:09:38 +08:00
|
|
|
case dwarf::DW_TAG_atomic_type:
|
2015-03-16 10:05:10 +08:00
|
|
|
case dwarf::DW_TAG_interface_type:
|
|
|
|
case dwarf::DW_TAG_unspecified_type:
|
|
|
|
case dwarf::DW_TAG_shared_type:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-11 12:17:30 +08:00
|
|
|
static bool
|
|
|
|
shouldSkipAttribute(DWARFAbbreviationDeclaration::AttributeSpec AttrSpec,
|
|
|
|
uint16_t Tag, bool InDebugMap, bool SkipPC,
|
|
|
|
bool InFunctionScope) {
|
|
|
|
switch (AttrSpec.Attr) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case dwarf::DW_AT_low_pc:
|
|
|
|
case dwarf::DW_AT_high_pc:
|
|
|
|
case dwarf::DW_AT_ranges:
|
|
|
|
return SkipPC;
|
|
|
|
case dwarf::DW_AT_location:
|
|
|
|
case dwarf::DW_AT_frame_base:
|
|
|
|
// FIXME: for some reason dsymutil-classic keeps the location
|
|
|
|
// attributes when they are of block type (ie. not location
|
|
|
|
// lists). This is totally wrong for globals where we will keep a
|
|
|
|
// wrong address. It is mostly harmless for locals, but there is
|
|
|
|
// no point in keeping these anyway when the function wasn't linked.
|
|
|
|
return (SkipPC || (!InFunctionScope && Tag == dwarf::DW_TAG_variable &&
|
|
|
|
!InDebugMap)) &&
|
|
|
|
!DWARFFormValue(AttrSpec.Form).isFormClass(DWARFFormValue::FC_Block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:46:10 +08:00
|
|
|
DIE *DwarfLinker::DIECloner::cloneDIE(
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &InputDIE, CompileUnit &Unit,
|
2016-12-02 02:56:29 +08:00
|
|
|
int64_t PCOffset, uint32_t OutOffset, unsigned Flags, DIE *Die) {
|
2015-03-05 06:07:44 +08:00
|
|
|
DWARFUnit &U = Unit.getOrigUnit();
|
2016-12-14 02:25:19 +08:00
|
|
|
unsigned Idx = U.getDIEIndex(InputDIE);
|
2015-03-07 07:22:53 +08:00
|
|
|
CompileUnit::DIEInfo &Info = Unit.getInfo(Idx);
|
2015-03-05 06:07:44 +08:00
|
|
|
|
|
|
|
// Should the DIE appear in the output?
|
|
|
|
if (!Unit.getInfo(Idx).Keep)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
uint32_t Offset = InputDIE.getOffset();
|
2016-12-02 02:56:29 +08:00
|
|
|
assert(!(Die && Info.Clone) && "Can't supply a DIE and a cloned DIE");
|
|
|
|
if (!Die) {
|
|
|
|
// The DIE might have been already created by a forward reference
|
|
|
|
// (see cloneDieReferenceAttribute()).
|
2016-12-02 06:04:16 +08:00
|
|
|
if (!Info.Clone)
|
|
|
|
Info.Clone = DIE::get(DIEAlloc, dwarf::Tag(InputDIE.getTag()));
|
|
|
|
Die = Info.Clone;
|
2016-12-02 02:56:29 +08:00
|
|
|
}
|
|
|
|
|
2015-03-07 07:22:53 +08:00
|
|
|
assert(Die->getTag() == InputDIE.getTag());
|
2015-03-05 06:07:44 +08:00
|
|
|
Die->setOffset(OutOffset);
|
2017-09-01 04:22:31 +08:00
|
|
|
if ((Unit.hasODR() || Unit.isClangModule()) && !Info.Incomplete &&
|
2015-09-24 01:35:52 +08:00
|
|
|
Die->getTag() != dwarf::DW_TAG_namespace && Info.Ctxt &&
|
2015-07-22 06:41:43 +08:00
|
|
|
Info.Ctxt != Unit.getInfo(Info.ParentIdx).Ctxt &&
|
|
|
|
!Info.Ctxt->getCanonicalDIEOffset()) {
|
|
|
|
// We are about to emit a DIE that is the root of its own valid
|
|
|
|
// DeclContext tree. Make the current offset the canonical offset
|
|
|
|
// for this context.
|
|
|
|
Info.Ctxt->setCanonicalDIEOffset(OutOffset + Unit.getStartOffset());
|
|
|
|
}
|
2015-03-05 06:07:44 +08:00
|
|
|
|
|
|
|
// Extract and clone every attribute.
|
2017-06-30 00:52:08 +08:00
|
|
|
DWARFDataExtractor Data = U.getDebugInfoExtractor();
|
2015-09-23 06:20:50 +08:00
|
|
|
// Point to the next DIE (generally there is always at least a NULL
|
|
|
|
// entry after the current one). If this is a lone
|
|
|
|
// DW_TAG_compile_unit without any children, point to the next unit.
|
|
|
|
uint32_t NextOffset =
|
|
|
|
(Idx + 1 < U.getNumDIEs())
|
2016-12-14 02:25:19 +08:00
|
|
|
? U.getDIEAtIndex(Idx + 1).getOffset()
|
2015-09-23 06:20:50 +08:00
|
|
|
: U.getNextUnitOffset();
|
2015-03-12 02:45:52 +08:00
|
|
|
AttributesInfo AttrInfo;
|
2015-03-07 09:25:09 +08:00
|
|
|
|
|
|
|
// We could copy the data only if we need to aply a relocation to
|
|
|
|
// it. After testing, it seems there is no performance downside to
|
|
|
|
// doing the copy unconditionally, and it makes the code simpler.
|
|
|
|
SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset));
|
2017-06-30 00:52:08 +08:00
|
|
|
Data =
|
|
|
|
DWARFDataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize());
|
2015-03-07 09:25:09 +08:00
|
|
|
// Modify the copy with relocated addresses.
|
2015-09-12 07:45:30 +08:00
|
|
|
if (RelocMgr.applyValidRelocs(DIECopy, Offset, Data.isLittleEndian())) {
|
2015-03-12 02:45:52 +08:00
|
|
|
// If we applied relocations, we store the value of high_pc that was
|
|
|
|
// potentially stored in the input DIE. If high_pc is an address
|
|
|
|
// (Dwarf version == 2), then it might have been relocated to a
|
|
|
|
// totally unrelated value (because the end address in the object
|
|
|
|
// file might be start address of another function which got moved
|
|
|
|
// independantly by the linker). The computation of the actual
|
|
|
|
// high_pc value is done in cloneAddressAttribute().
|
2017-09-30 08:22:24 +08:00
|
|
|
AttrInfo.OrigHighPc =
|
|
|
|
dwarf::toAddress(InputDIE.find(dwarf::DW_AT_high_pc), 0);
|
2015-08-31 09:43:14 +08:00
|
|
|
// Also store the low_pc. It might get relocated in an
|
|
|
|
// inline_subprogram that happens at the beginning of its
|
|
|
|
// inlining function.
|
2017-09-30 08:22:24 +08:00
|
|
|
AttrInfo.OrigLowPc =
|
|
|
|
dwarf::toAddress(InputDIE.find(dwarf::DW_AT_low_pc), UINT64_MAX);
|
2015-03-12 02:45:52 +08:00
|
|
|
}
|
2015-03-07 09:25:09 +08:00
|
|
|
|
|
|
|
// Reset the Offset to 0 as we will be working on the local copy of
|
|
|
|
// the data.
|
|
|
|
Offset = 0;
|
|
|
|
|
2015-03-05 06:07:44 +08:00
|
|
|
const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr();
|
|
|
|
Offset += getULEB128Size(Abbrev->getCode());
|
|
|
|
|
2015-03-12 02:45:52 +08:00
|
|
|
// We are entering a subprogram. Get and propagate the PCOffset.
|
|
|
|
if (Die->getTag() == dwarf::DW_TAG_subprogram)
|
|
|
|
PCOffset = Info.AddrAdjust;
|
|
|
|
AttrInfo.PCOffset = PCOffset;
|
|
|
|
|
2015-09-11 12:17:30 +08:00
|
|
|
if (Abbrev->getTag() == dwarf::DW_TAG_subprogram) {
|
|
|
|
Flags |= TF_InFunctionScope;
|
|
|
|
if (!Info.InDebugMap)
|
|
|
|
Flags |= TF_SkipPC;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Copied = false;
|
2015-03-05 06:07:44 +08:00
|
|
|
for (const auto &AttrSpec : Abbrev->attributes()) {
|
2015-09-11 12:17:30 +08:00
|
|
|
if (shouldSkipAttribute(AttrSpec, Die->getTag(), Info.InDebugMap,
|
|
|
|
Flags & TF_SkipPC, Flags & TF_InFunctionScope)) {
|
2017-06-27 02:43:01 +08:00
|
|
|
DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
|
|
|
|
U.getFormParams());
|
2015-09-11 12:17:30 +08:00
|
|
|
// FIXME: dsymutil-classic keeps the old abbreviation around
|
|
|
|
// even if it's not used. We can remove this (and the copyAbbrev
|
|
|
|
// helper) as soon as bit-for-bit compatibility is not a goal anymore.
|
|
|
|
if (!Copied) {
|
|
|
|
copyAbbrev(*InputDIE.getAbbreviationDeclarationPtr(), Unit.hasODR());
|
|
|
|
Copied = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-03-05 06:07:44 +08:00
|
|
|
DWARFFormValue Val(AttrSpec.Form);
|
|
|
|
uint32_t AttrSize = Offset;
|
|
|
|
Val.extractValue(Data, &Offset, &U);
|
|
|
|
AttrSize = Offset - AttrSize;
|
|
|
|
|
2015-03-12 02:45:52 +08:00
|
|
|
OutOffset +=
|
|
|
|
cloneAttribute(*Die, InputDIE, Unit, Val, AttrSpec, AttrSize, AttrInfo);
|
2015-03-05 06:07:44 +08:00
|
|
|
}
|
|
|
|
|
2015-03-16 10:05:10 +08:00
|
|
|
// Look for accelerator entries.
|
|
|
|
uint16_t Tag = InputDIE.getTag();
|
|
|
|
// FIXME: This is slightly wrong. An inline_subroutine without a
|
|
|
|
// low_pc, but with AT_ranges might be interesting to get into the
|
|
|
|
// accelerator tables too. For now stick with dsymutil's behavior.
|
|
|
|
if ((Info.InDebugMap || AttrInfo.HasLowPc) &&
|
|
|
|
Tag != dwarf::DW_TAG_compile_unit &&
|
2016-12-14 02:25:19 +08:00
|
|
|
getDIENames(InputDIE, AttrInfo)) {
|
2015-03-16 10:05:10 +08:00
|
|
|
if (AttrInfo.MangledName && AttrInfo.MangledName != AttrInfo.Name)
|
|
|
|
Unit.addNameAccelerator(Die, AttrInfo.MangledName,
|
|
|
|
AttrInfo.MangledNameOffset,
|
|
|
|
Tag == dwarf::DW_TAG_inlined_subroutine);
|
|
|
|
if (AttrInfo.Name)
|
|
|
|
Unit.addNameAccelerator(Die, AttrInfo.Name, AttrInfo.NameOffset,
|
|
|
|
Tag == dwarf::DW_TAG_inlined_subroutine);
|
|
|
|
} else if (isTypeTag(Tag) && !AttrInfo.IsDeclaration &&
|
2016-12-14 02:25:19 +08:00
|
|
|
getDIENames(InputDIE, AttrInfo)) {
|
2015-03-16 10:05:10 +08:00
|
|
|
Unit.addTypeAccelerator(Die, AttrInfo.Name, AttrInfo.NameOffset);
|
|
|
|
}
|
|
|
|
|
2015-11-11 05:31:05 +08:00
|
|
|
// Determine whether there are any children that we want to keep.
|
|
|
|
bool HasChildren = false;
|
2017-01-06 07:47:37 +08:00
|
|
|
for (auto Child: InputDIE.children()) {
|
2015-11-11 05:31:05 +08:00
|
|
|
unsigned Idx = U.getDIEIndex(Child);
|
|
|
|
if (Unit.getInfo(Idx).Keep) {
|
|
|
|
HasChildren = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 06:31:41 +08:00
|
|
|
DIEAbbrev NewAbbrev = Die->generateAbbrev();
|
2015-11-11 05:31:05 +08:00
|
|
|
if (HasChildren)
|
2015-03-05 06:07:44 +08:00
|
|
|
NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
|
|
|
|
// Assign a permanent abbrev number
|
2015-09-15 00:46:10 +08:00
|
|
|
Linker.AssignAbbrev(NewAbbrev);
|
2015-05-28 06:31:41 +08:00
|
|
|
Die->setAbbrevNumber(NewAbbrev.getNumber());
|
2015-03-05 06:07:44 +08:00
|
|
|
|
|
|
|
// Add the size of the abbreviation number to the output offset.
|
|
|
|
OutOffset += getULEB128Size(Die->getAbbrevNumber());
|
|
|
|
|
2015-11-11 05:31:05 +08:00
|
|
|
if (!HasChildren) {
|
2015-03-05 06:07:44 +08:00
|
|
|
// Update our size.
|
|
|
|
Die->setSize(OutOffset - Die->getOffset());
|
|
|
|
return Die;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recursively clone children.
|
2017-01-06 07:47:37 +08:00
|
|
|
for (auto Child: InputDIE.children()) {
|
2016-12-14 02:25:19 +08:00
|
|
|
if (DIE *Clone = cloneDIE(Child, Unit, PCOffset, OutOffset, Flags)) {
|
2015-06-26 07:52:10 +08:00
|
|
|
Die->addChild(Clone);
|
2015-03-05 06:07:44 +08:00
|
|
|
OutOffset = Clone->getOffset() + Clone->getSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Account for the end of children marker.
|
|
|
|
OutOffset += sizeof(int8_t);
|
|
|
|
// Update our size.
|
|
|
|
Die->setSize(OutOffset - Die->getOffset());
|
|
|
|
return Die;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Patch the input object file relevant debug_ranges entries
|
2015-03-14 07:30:31 +08:00
|
|
|
/// and emit them in the output file. Update the relevant attributes
|
|
|
|
/// to point at the new entries.
|
|
|
|
void DwarfLinker::patchRangesForUnit(const CompileUnit &Unit,
|
|
|
|
DWARFContext &OrigDwarf) const {
|
|
|
|
DWARFDebugRangeList RangeList;
|
|
|
|
const auto &FunctionRanges = Unit.getFunctionRanges();
|
|
|
|
unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
|
2017-07-20 06:27:28 +08:00
|
|
|
DWARFDataExtractor RangeExtractor(OrigDwarf.getDWARFObj(),
|
|
|
|
OrigDwarf.getDWARFObj().getRangeSection(),
|
2017-06-30 00:52:08 +08:00
|
|
|
OrigDwarf.isLittleEndian(), AddressSize);
|
2015-03-14 07:30:31 +08:00
|
|
|
auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
|
|
|
|
DWARFUnit &OrigUnit = Unit.getOrigUnit();
|
2016-12-14 02:25:19 +08:00
|
|
|
auto OrigUnitDie = OrigUnit.getUnitDIE(false);
|
2017-09-30 08:22:24 +08:00
|
|
|
uint64_t OrigLowPc =
|
|
|
|
dwarf::toAddress(OrigUnitDie.find(dwarf::DW_AT_low_pc), -1ULL);
|
2015-03-14 07:30:31 +08:00
|
|
|
// Ranges addresses are based on the unit's low_pc. Compute the
|
2015-12-08 03:21:39 +08:00
|
|
|
// offset we need to apply to adapt to the new unit's low_pc.
|
2015-03-14 07:30:31 +08:00
|
|
|
int64_t UnitPcOffset = 0;
|
|
|
|
if (OrigLowPc != -1ULL)
|
|
|
|
UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc();
|
|
|
|
|
|
|
|
for (const auto &RangeAttribute : Unit.getRangesAttributes()) {
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
uint32_t Offset = RangeAttribute.get();
|
|
|
|
RangeAttribute.set(Streamer->getRangesSectionSize());
|
2017-06-30 00:52:08 +08:00
|
|
|
RangeList.extract(RangeExtractor, &Offset);
|
2015-03-14 07:30:31 +08:00
|
|
|
const auto &Entries = RangeList.getEntries();
|
2015-08-31 13:09:32 +08:00
|
|
|
if (!Entries.empty()) {
|
|
|
|
const DWARFDebugRangeList::RangeListEntry &First = Entries.front();
|
2015-03-14 07:30:31 +08:00
|
|
|
|
|
|
|
if (CurrRange == InvalidRange ||
|
2015-08-31 13:09:32 +08:00
|
|
|
First.StartAddress + OrigLowPc < CurrRange.start() ||
|
|
|
|
First.StartAddress + OrigLowPc >= CurrRange.stop()) {
|
|
|
|
CurrRange = FunctionRanges.find(First.StartAddress + OrigLowPc);
|
|
|
|
if (CurrRange == InvalidRange ||
|
|
|
|
CurrRange.start() > First.StartAddress + OrigLowPc) {
|
|
|
|
reportWarning("no mapping for range.");
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-14 07:30:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Streamer->emitRangesEntries(UnitPcOffset, OrigLowPc, CurrRange, Entries,
|
|
|
|
AddressSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Generate the debug_aranges entries for \p Unit and if the
|
2015-03-14 11:46:51 +08:00
|
|
|
/// unit has a DW_AT_ranges attribute, also emit the debug_ranges
|
|
|
|
/// contribution for this attribute.
|
2015-03-14 07:30:31 +08:00
|
|
|
/// FIXME: this could actually be done right in patchRangesForUnit,
|
|
|
|
/// but for the sake of initial bit-for-bit compatibility with legacy
|
|
|
|
/// dsymutil, we have to do it in a delayed pass.
|
|
|
|
void DwarfLinker::generateUnitRanges(CompileUnit &Unit) const {
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
auto Attr = Unit.getUnitRangesAttribute();
|
2015-03-14 11:46:51 +08:00
|
|
|
if (Attr)
|
Reapply "AsmPrinter: Change DIEValue to be stored by value"
This reverts commit r238350, effectively reapplying r238349 after fixing
(all?) the problems, all somehow related to how I was using
`AlignedArrayCharUnion<>` inside `DIEValue`:
- MSVC can only handle `sizeof()` on types, not values. Change the
assert.
- GCC doesn't know the `is_trivially_copyable` type trait. Instead of
asserting it, add destructors.
- Call placement new even when constructing POD (i.e., the pointers).
- Instead of copying the char buffer, copy the casted classes.
I've left in a couple of `static_assert`s that I think both MSVC and GCC
know how to handle. If the bots disagree with me, I'll remove them.
- Check that the constructed type is either standard layout or a
pointer. This protects against a programming error: we really want
the "small" `DIEValue`s to be small and simple, so don't
accidentally change them not to be.
- Similarly, check that the size of the buffer is no bigger than a
`uint64_t` or a pointer. (I thought checking against
`sizeof(uint64_t)` would be good enough, but Chandler suggested that
pointers might sometimes be bigger than that in the context of
sanitizers.)
I've also committed r238359 in the meantime, which introduces a
DIEValue.def to simplify dispatching between the various types (thanks
to a review comment by David Blaikie). Without that, this commit would
be almost unintelligible.
Here's the original commit message:
--
Change `DIEValue` to be stored/passed/etc. by value, instead of
reference. It's now a discriminated union, with a `Val` field storing
the actual type. The classes that used to inherit from `DIEValue` no
longer do. There are two categories of these:
- Small values fit in a single pointer and are stored by value.
- Large values require auxiliary storage, and are stored by reference.
The only non-mechanical change is to tools/dsymutil/DwarfLinker.cpp. It
was relying on `DIEInteger`s being passed around by reference, so I
replaced that assumption with a `PatchLocation` type that stores a safe
reference to where the `DIEInteger` lives instead.
This commit causes a temporary regression in memory usage, since I've
left merging `DIEAbbrevData` into `DIEValue` for a follow-up commit. I
measured an increase from 845 MB to 879 MB, around 3.9%. The follow-up
drops it lower than the starting point, and I've only recently brought
the memory this low anyway, so I'm committing these changes separately
to keep them incremental. (I also considered swapping the commits, but
the other one first would cause a lot more code churn.)
(I'm looking at `llc` memory usage on `verify-uselistorder.lto.opt.bc`;
see r236629 for details.)
--
llvm-svn: 238362
2015-05-28 06:14:58 +08:00
|
|
|
Attr->set(Streamer->getRangesSectionSize());
|
|
|
|
Streamer->emitUnitRangesEntries(Unit, static_cast<bool>(Attr));
|
2015-03-14 07:30:31 +08:00
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Insert the new line info sequence \p Seq into the current
|
2015-03-16 04:45:43 +08:00
|
|
|
/// set of already linked line info \p Rows.
|
|
|
|
static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
|
|
|
|
std::vector<DWARFDebugLine::Row> &Rows) {
|
|
|
|
if (Seq.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!Rows.empty() && Rows.back().Address < Seq.front().Address) {
|
|
|
|
Rows.insert(Rows.end(), Seq.begin(), Seq.end());
|
|
|
|
Seq.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto InsertPoint = std::lower_bound(
|
|
|
|
Rows.begin(), Rows.end(), Seq.front(),
|
|
|
|
[](const DWARFDebugLine::Row &LHS, const DWARFDebugLine::Row &RHS) {
|
|
|
|
return LHS.Address < RHS.Address;
|
|
|
|
});
|
|
|
|
|
|
|
|
// FIXME: this only removes the unneeded end_sequence if the
|
|
|
|
// sequences have been inserted in order. using a global sort like
|
|
|
|
// described in patchLineTableForUnit() and delaying the end_sequene
|
|
|
|
// elimination to emitLineTableForUnit() we can get rid of all of them.
|
|
|
|
if (InsertPoint != Rows.end() &&
|
|
|
|
InsertPoint->Address == Seq.front().Address && InsertPoint->EndSequence) {
|
|
|
|
*InsertPoint = Seq.front();
|
|
|
|
Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end());
|
|
|
|
} else {
|
|
|
|
Rows.insert(InsertPoint, Seq.begin(), Seq.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
Seq.clear();
|
|
|
|
}
|
|
|
|
|
2015-06-26 05:42:46 +08:00
|
|
|
static void patchStmtList(DIE &Die, DIEInteger Offset) {
|
|
|
|
for (auto &V : Die.values())
|
|
|
|
if (V.getAttribute() == dwarf::DW_AT_stmt_list) {
|
2015-06-26 07:46:41 +08:00
|
|
|
V = DIEValue(V.getAttribute(), V.getForm(), Offset);
|
2015-06-26 05:42:46 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Didn't find DW_AT_stmt_list in cloned DIE!");
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Extract the line table for \p Unit from \p OrigDwarf, and
|
2015-03-16 04:45:43 +08:00
|
|
|
/// recreate a relocated version of these for the address ranges that
|
|
|
|
/// are present in the binary.
|
|
|
|
void DwarfLinker::patchLineTableForUnit(CompileUnit &Unit,
|
|
|
|
DWARFContext &OrigDwarf) {
|
2016-12-14 02:25:19 +08:00
|
|
|
DWARFDie CUDie = Unit.getOrigUnit().getUnitDIE();
|
2017-01-14 05:08:18 +08:00
|
|
|
auto StmtList = dwarf::toSectionOffset(CUDie.find(dwarf::DW_AT_stmt_list));
|
2016-12-15 06:38:08 +08:00
|
|
|
if (!StmtList)
|
2015-03-16 04:45:43 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Update the cloned DW_AT_stmt_list with the correct debug_line offset.
|
2015-06-26 05:42:46 +08:00
|
|
|
if (auto *OutputDIE = Unit.getOutputUnitDIE())
|
|
|
|
patchStmtList(*OutputDIE, DIEInteger(Streamer->getLineSectionSize()));
|
2015-03-16 04:45:43 +08:00
|
|
|
|
|
|
|
// Parse the original line info for the unit.
|
|
|
|
DWARFDebugLine::LineTable LineTable;
|
2016-12-15 06:38:08 +08:00
|
|
|
uint32_t StmtOffset = *StmtList;
|
2017-07-20 06:27:28 +08:00
|
|
|
DWARFDataExtractor LineExtractor(
|
|
|
|
OrigDwarf.getDWARFObj(), OrigDwarf.getDWARFObj().getLineSection(),
|
|
|
|
OrigDwarf.isLittleEndian(), Unit.getOrigUnit().getAddressByteSize());
|
2017-06-30 00:52:08 +08:00
|
|
|
LineTable.parse(LineExtractor, &StmtOffset);
|
2015-03-16 04:45:43 +08:00
|
|
|
|
|
|
|
// This vector is the output line table.
|
|
|
|
std::vector<DWARFDebugLine::Row> NewRows;
|
|
|
|
NewRows.reserve(LineTable.Rows.size());
|
|
|
|
|
|
|
|
// Current sequence of rows being extracted, before being inserted
|
|
|
|
// in NewRows.
|
|
|
|
std::vector<DWARFDebugLine::Row> Seq;
|
|
|
|
const auto &FunctionRanges = Unit.getFunctionRanges();
|
|
|
|
auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
|
|
|
|
|
|
|
|
// FIXME: This logic is meant to generate exactly the same output as
|
|
|
|
// Darwin's classic dsynutil. There is a nicer way to implement this
|
|
|
|
// by simply putting all the relocated line info in NewRows and simply
|
|
|
|
// sorting NewRows before passing it to emitLineTableForUnit. This
|
|
|
|
// should be correct as sequences for a function should stay
|
|
|
|
// together in the sorted output. There are a few corner cases that
|
|
|
|
// look suspicious though, and that required to implement the logic
|
|
|
|
// this way. Revisit that once initial validation is finished.
|
|
|
|
|
|
|
|
// Iterate over the object file line info and extract the sequences
|
|
|
|
// that correspond to linked functions.
|
|
|
|
for (auto &Row : LineTable.Rows) {
|
|
|
|
// Check wether we stepped out of the range. The range is
|
|
|
|
// half-open, but consider accept the end address of the range if
|
|
|
|
// it is marked as end_sequence in the input (because in that
|
|
|
|
// case, the relocation offset is accurate and that entry won't
|
|
|
|
// serve as the start of another function).
|
|
|
|
if (CurrRange == InvalidRange || Row.Address < CurrRange.start() ||
|
|
|
|
Row.Address > CurrRange.stop() ||
|
|
|
|
(Row.Address == CurrRange.stop() && !Row.EndSequence)) {
|
|
|
|
// We just stepped out of a known range. Insert a end_sequence
|
|
|
|
// corresponding to the end of the range.
|
|
|
|
uint64_t StopAddress = CurrRange != InvalidRange
|
|
|
|
? CurrRange.stop() + CurrRange.value()
|
|
|
|
: -1ULL;
|
|
|
|
CurrRange = FunctionRanges.find(Row.Address);
|
|
|
|
bool CurrRangeValid =
|
|
|
|
CurrRange != InvalidRange && CurrRange.start() <= Row.Address;
|
|
|
|
if (!CurrRangeValid) {
|
|
|
|
CurrRange = InvalidRange;
|
|
|
|
if (StopAddress != -1ULL) {
|
|
|
|
// Try harder by looking in the DebugMapObject function
|
|
|
|
// ranges map. There are corner cases where this finds a
|
|
|
|
// valid entry. It's unclear if this is right or wrong, but
|
|
|
|
// for now do as dsymutil.
|
|
|
|
// FIXME: Understand exactly what cases this addresses and
|
|
|
|
// potentially remove it along with the Ranges map.
|
|
|
|
auto Range = Ranges.lower_bound(Row.Address);
|
|
|
|
if (Range != Ranges.begin() && Range != Ranges.end())
|
|
|
|
--Range;
|
|
|
|
|
|
|
|
if (Range != Ranges.end() && Range->first <= Row.Address &&
|
|
|
|
Range->second.first >= Row.Address) {
|
|
|
|
StopAddress = Row.Address + Range->second.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (StopAddress != -1ULL && !Seq.empty()) {
|
|
|
|
// Insert end sequence row with the computed end address, but
|
|
|
|
// the same line as the previous one.
|
2015-08-11 00:15:51 +08:00
|
|
|
auto NextLine = Seq.back();
|
2015-08-11 02:27:51 +08:00
|
|
|
NextLine.Address = StopAddress;
|
|
|
|
NextLine.EndSequence = 1;
|
|
|
|
NextLine.PrologueEnd = 0;
|
|
|
|
NextLine.BasicBlock = 0;
|
|
|
|
NextLine.EpilogueBegin = 0;
|
2015-08-11 02:03:35 +08:00
|
|
|
Seq.push_back(NextLine);
|
2015-03-16 04:45:43 +08:00
|
|
|
insertLineSequence(Seq, NewRows);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CurrRangeValid)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore empty sequences.
|
|
|
|
if (Row.EndSequence && Seq.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Relocate row address and add it to the current sequence.
|
|
|
|
Row.Address += CurrRange.value();
|
|
|
|
Seq.emplace_back(Row);
|
|
|
|
|
|
|
|
if (Row.EndSequence)
|
|
|
|
insertLineSequence(Seq, NewRows);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finished extracting, now emit the line tables.
|
2016-12-15 06:38:08 +08:00
|
|
|
uint32_t PrologueEnd = *StmtList + 10 + LineTable.Prologue.PrologueLength;
|
2015-03-16 04:45:43 +08:00
|
|
|
// FIXME: LLVM hardcodes it's prologue values. We just copy the
|
|
|
|
// prologue over and that works because we act as both producer and
|
|
|
|
// consumer. It would be nicer to have a real configurable line
|
|
|
|
// table emitter.
|
2017-06-27 02:43:01 +08:00
|
|
|
if (LineTable.Prologue.getVersion() != 2 ||
|
2015-03-16 04:45:43 +08:00
|
|
|
LineTable.Prologue.DefaultIsStmt != DWARF2_LINE_DEFAULT_IS_STMT ||
|
2015-08-07 23:14:13 +08:00
|
|
|
LineTable.Prologue.OpcodeBase > 13)
|
2017-03-30 20:59:53 +08:00
|
|
|
reportWarning("line table parameters mismatch. Cannot emit.");
|
2015-08-07 23:14:13 +08:00
|
|
|
else {
|
2017-07-20 06:27:28 +08:00
|
|
|
StringRef LineData = OrigDwarf.getDWARFObj().getLineSection().Data;
|
2015-08-07 23:14:13 +08:00
|
|
|
MCDwarfLineTableParams Params;
|
|
|
|
Params.DWARF2LineOpcodeBase = LineTable.Prologue.OpcodeBase;
|
|
|
|
Params.DWARF2LineBase = LineTable.Prologue.LineBase;
|
|
|
|
Params.DWARF2LineRange = LineTable.Prologue.LineRange;
|
|
|
|
Streamer->emitLineTableForUnit(Params,
|
2016-12-15 06:38:08 +08:00
|
|
|
LineData.slice(*StmtList + 4, PrologueEnd),
|
2015-03-16 04:45:43 +08:00
|
|
|
LineTable.Prologue.MinInstLength, NewRows,
|
|
|
|
Unit.getOrigUnit().getAddressByteSize());
|
2015-08-07 23:14:13 +08:00
|
|
|
}
|
2015-03-16 04:45:43 +08:00
|
|
|
}
|
|
|
|
|
2015-03-16 10:05:10 +08:00
|
|
|
void DwarfLinker::emitAcceleratorEntriesForUnit(CompileUnit &Unit) {
|
|
|
|
Streamer->emitPubNamesForUnit(Unit);
|
|
|
|
Streamer->emitPubTypesForUnit(Unit);
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Read the frame info stored in the object, and emit the
|
2015-06-06 07:06:11 +08:00
|
|
|
/// patched frame descriptions for the linked binary.
|
|
|
|
///
|
|
|
|
/// This is actually pretty easy as the data of the CIEs and FDEs can
|
|
|
|
/// be considered as black boxes and moved as is. The only thing to do
|
|
|
|
/// is to patch the addresses in the headers.
|
|
|
|
void DwarfLinker::patchFrameInfoForObject(const DebugMapObject &DMO,
|
|
|
|
DWARFContext &OrigDwarf,
|
|
|
|
unsigned AddrSize) {
|
2017-07-20 06:27:28 +08:00
|
|
|
StringRef FrameData = OrigDwarf.getDWARFObj().getDebugFrameSection();
|
2015-06-06 07:06:11 +08:00
|
|
|
if (FrameData.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
DataExtractor Data(FrameData, OrigDwarf.isLittleEndian(), 0);
|
|
|
|
uint32_t InputOffset = 0;
|
|
|
|
|
|
|
|
// Store the data of the CIEs defined in this object, keyed by their
|
|
|
|
// offsets.
|
|
|
|
DenseMap<uint32_t, StringRef> LocalCIES;
|
|
|
|
|
|
|
|
while (Data.isValidOffset(InputOffset)) {
|
|
|
|
uint32_t EntryOffset = InputOffset;
|
|
|
|
uint32_t InitialLength = Data.getU32(&InputOffset);
|
|
|
|
if (InitialLength == 0xFFFFFFFF)
|
|
|
|
return reportWarning("Dwarf64 bits no supported");
|
|
|
|
|
|
|
|
uint32_t CIEId = Data.getU32(&InputOffset);
|
|
|
|
if (CIEId == 0xFFFFFFFF) {
|
|
|
|
// This is a CIE, store it.
|
|
|
|
StringRef CIEData = FrameData.substr(EntryOffset, InitialLength + 4);
|
|
|
|
LocalCIES[EntryOffset] = CIEData;
|
|
|
|
// The -4 is to account for the CIEId we just read.
|
|
|
|
InputOffset += InitialLength - 4;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Loc = Data.getUnsigned(&InputOffset, AddrSize);
|
|
|
|
|
|
|
|
// Some compilers seem to emit frame info that doesn't start at
|
|
|
|
// the function entry point, thus we can't just lookup the address
|
|
|
|
// in the debug map. Use the linker's range map to see if the FDE
|
|
|
|
// describes something that we can relocate.
|
|
|
|
auto Range = Ranges.upper_bound(Loc);
|
|
|
|
if (Range != Ranges.begin())
|
|
|
|
--Range;
|
|
|
|
if (Range == Ranges.end() || Range->first > Loc ||
|
|
|
|
Range->second.first <= Loc) {
|
|
|
|
// The +4 is to account for the size of the InitialLength field itself.
|
|
|
|
InputOffset = EntryOffset + InitialLength + 4;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is an FDE, and we have a mapping.
|
|
|
|
// Have we already emitted a corresponding CIE?
|
|
|
|
StringRef CIEData = LocalCIES[CIEId];
|
|
|
|
if (CIEData.empty())
|
|
|
|
return reportWarning("Inconsistent debug_frame content. Dropping.");
|
|
|
|
|
|
|
|
// Look if we already emitted a CIE that corresponds to the
|
|
|
|
// referenced one (the CIE data is the key of that lookup).
|
|
|
|
auto IteratorInserted = EmittedCIEs.insert(
|
|
|
|
std::make_pair(CIEData, Streamer->getFrameSectionSize()));
|
|
|
|
// If there is no CIE yet for this ID, emit it.
|
|
|
|
if (IteratorInserted.second ||
|
|
|
|
// FIXME: dsymutil-classic only caches the last used CIE for
|
|
|
|
// reuse. Mimic that behavior for now. Just removing that
|
|
|
|
// second half of the condition and the LastCIEOffset variable
|
|
|
|
// makes the code DTRT.
|
|
|
|
LastCIEOffset != IteratorInserted.first->getValue()) {
|
|
|
|
LastCIEOffset = Streamer->getFrameSectionSize();
|
|
|
|
IteratorInserted.first->getValue() = LastCIEOffset;
|
|
|
|
Streamer->emitCIE(CIEData);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the FDE with updated address and CIE pointer.
|
|
|
|
// (4 + AddrSize) is the size of the CIEId + initial_location
|
|
|
|
// fields that will get reconstructed by emitFDE().
|
|
|
|
unsigned FDERemainingBytes = InitialLength - (4 + AddrSize);
|
|
|
|
Streamer->emitFDE(IteratorInserted.first->getValue(), AddrSize,
|
|
|
|
Loc + Range->second.second,
|
|
|
|
FrameData.substr(InputOffset, FDERemainingBytes));
|
|
|
|
InputOffset += FDERemainingBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:46:10 +08:00
|
|
|
void DwarfLinker::DIECloner::copyAbbrev(
|
|
|
|
const DWARFAbbreviationDeclaration &Abbrev, bool hasODR) {
|
2015-09-11 12:17:30 +08:00
|
|
|
DIEAbbrev Copy(dwarf::Tag(Abbrev.getTag()),
|
|
|
|
dwarf::Form(Abbrev.hasChildren()));
|
|
|
|
|
|
|
|
for (const auto &Attr : Abbrev.attributes()) {
|
|
|
|
uint16_t Form = Attr.Form;
|
|
|
|
if (hasODR && isODRAttribute(Attr.Attr))
|
|
|
|
Form = dwarf::DW_FORM_ref_addr;
|
|
|
|
Copy.AddAttribute(dwarf::Attribute(Attr.Attr), dwarf::Form(Form));
|
|
|
|
}
|
|
|
|
|
2015-09-15 00:46:10 +08:00
|
|
|
Linker.AssignAbbrev(Copy);
|
2015-09-11 12:17:30 +08:00
|
|
|
}
|
|
|
|
|
2016-12-14 02:25:19 +08:00
|
|
|
static uint64_t getDwoId(const DWARFDie &CUDie,
|
2015-09-24 01:11:10 +08:00
|
|
|
const DWARFUnit &Unit) {
|
2017-01-14 06:32:12 +08:00
|
|
|
auto DwoId = dwarf::toUnsigned(CUDie.find({dwarf::DW_AT_dwo_id,
|
|
|
|
dwarf::DW_AT_GNU_dwo_id}));
|
2016-12-15 06:38:08 +08:00
|
|
|
if (DwoId)
|
|
|
|
return *DwoId;
|
|
|
|
return 0;
|
2015-09-24 01:11:10 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 06:20:50 +08:00
|
|
|
bool DwarfLinker::registerModuleReference(
|
2016-12-14 02:25:19 +08:00
|
|
|
const DWARFDie &CUDie, const DWARFUnit &Unit,
|
2015-09-23 06:20:50 +08:00
|
|
|
DebugMap &ModuleMap, unsigned Indent) {
|
2017-01-14 06:32:12 +08:00
|
|
|
std::string PCMfile =
|
|
|
|
dwarf::toString(CUDie.find({dwarf::DW_AT_dwo_name,
|
|
|
|
dwarf::DW_AT_GNU_dwo_name}), "");
|
2015-09-23 06:20:50 +08:00
|
|
|
if (PCMfile.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Clang module DWARF skeleton CUs abuse this for the path to the module.
|
2017-01-14 05:08:18 +08:00
|
|
|
std::string PCMpath = dwarf::toString(CUDie.find(dwarf::DW_AT_comp_dir), "");
|
2015-09-24 01:11:10 +08:00
|
|
|
uint64_t DwoId = getDwoId(CUDie, Unit);
|
2015-09-23 06:20:50 +08:00
|
|
|
|
2017-01-14 05:08:18 +08:00
|
|
|
std::string Name = dwarf::toString(CUDie.find(dwarf::DW_AT_name), "");
|
2015-09-24 01:35:52 +08:00
|
|
|
if (Name.empty()) {
|
|
|
|
reportWarning("Anonymous module skeleton CU for " + PCMfile);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-23 06:20:50 +08:00
|
|
|
if (Options.Verbose) {
|
|
|
|
outs().indent(Indent);
|
|
|
|
outs() << "Found clang module reference " << PCMfile;
|
|
|
|
}
|
|
|
|
|
2015-09-24 01:11:10 +08:00
|
|
|
auto Cached = ClangModules.find(PCMfile);
|
|
|
|
if (Cached != ClangModules.end()) {
|
2016-05-13 08:17:58 +08:00
|
|
|
// FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
|
|
|
|
// fixed in clang, only warn about DWO_id mismatches in verbose mode.
|
|
|
|
// ASTFileSignatures will change randomly when a module is rebuilt.
|
|
|
|
if (Options.Verbose && (Cached->second != DwoId))
|
2015-09-24 01:11:10 +08:00
|
|
|
reportWarning(Twine("hash mismatch: this object file was built against a "
|
|
|
|
"different version of the module ") + PCMfile);
|
2015-09-23 06:20:50 +08:00
|
|
|
if (Options.Verbose)
|
|
|
|
outs() << " [cached].\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Options.Verbose)
|
|
|
|
outs() << " ...\n";
|
|
|
|
|
|
|
|
// Cyclic dependencies are disallowed by Clang, but we still
|
|
|
|
// shouldn't run into an infinite loop, so mark it as processed now.
|
2015-09-24 01:11:10 +08:00
|
|
|
ClangModules.insert({PCMfile, DwoId});
|
2015-09-24 01:35:52 +08:00
|
|
|
loadClangModule(PCMfile, PCMpath, Name, DwoId, ModuleMap, Indent + 2);
|
2015-09-23 06:20:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-24 14:41:11 +08:00
|
|
|
ErrorOr<const object::ObjectFile &>
|
|
|
|
DwarfLinker::loadObject(BinaryHolder &BinaryHolder, DebugMapObject &Obj,
|
|
|
|
const DebugMap &Map) {
|
|
|
|
auto ErrOrObjs =
|
|
|
|
BinaryHolder.GetObjectFiles(Obj.getObjectFilename(), Obj.getTimestamp());
|
2015-08-31 13:16:35 +08:00
|
|
|
if (std::error_code EC = ErrOrObjs.getError()) {
|
2015-07-24 14:41:11 +08:00
|
|
|
reportWarning(Twine(Obj.getObjectFilename()) + ": " + EC.message());
|
2015-08-31 13:16:35 +08:00
|
|
|
return EC;
|
|
|
|
}
|
2015-07-24 14:41:11 +08:00
|
|
|
auto ErrOrObj = BinaryHolder.Get(Map.getTriple());
|
|
|
|
if (std::error_code EC = ErrOrObj.getError())
|
|
|
|
reportWarning(Twine(Obj.getObjectFilename()) + ": " + EC.message());
|
|
|
|
return ErrOrObj;
|
|
|
|
}
|
|
|
|
|
2015-09-23 06:20:50 +08:00
|
|
|
void DwarfLinker::loadClangModule(StringRef Filename, StringRef ModulePath,
|
2015-09-24 01:35:52 +08:00
|
|
|
StringRef ModuleName, uint64_t DwoId,
|
|
|
|
DebugMap &ModuleMap, unsigned Indent) {
|
2015-09-23 06:20:50 +08:00
|
|
|
SmallString<80> Path(Options.PrependPath);
|
|
|
|
if (sys::path::is_relative(Filename))
|
|
|
|
sys::path::append(Path, ModulePath, Filename);
|
|
|
|
else
|
|
|
|
sys::path::append(Path, Filename);
|
|
|
|
BinaryHolder ObjHolder(Options.Verbose);
|
2017-10-06 22:49:20 +08:00
|
|
|
auto &Obj = ModuleMap.addDebugMapObject(
|
|
|
|
Path, sys::TimePoint<std::chrono::seconds>(), MachO::N_OSO);
|
2015-09-23 06:20:50 +08:00
|
|
|
auto ErrOrObj = loadObject(ObjHolder, Obj, ModuleMap);
|
2016-01-15 02:31:07 +08:00
|
|
|
if (!ErrOrObj) {
|
|
|
|
// Try and emit more helpful warnings by applying some heuristics.
|
|
|
|
StringRef ObjFile = CurrentDebugObject->getObjectFilename();
|
|
|
|
bool isClangModule = sys::path::extension(Filename).equals(".pcm");
|
|
|
|
bool isArchive = ObjFile.endswith(")");
|
|
|
|
if (isClangModule) {
|
|
|
|
StringRef ModuleCacheDir = sys::path::parent_path(Path);
|
|
|
|
if (sys::fs::exists(ModuleCacheDir)) {
|
|
|
|
// If the module's parent directory exists, we assume that the module
|
|
|
|
// cache has expired and was pruned by clang. A more adventurous
|
|
|
|
// dsymutil would invoke clang to rebuild the module now.
|
|
|
|
if (!ModuleCacheHintDisplayed) {
|
|
|
|
errs() << "note: The clang module cache may have expired since this "
|
|
|
|
"object file was built. Rebuilding the object file will "
|
|
|
|
"rebuild the module cache.\n";
|
|
|
|
ModuleCacheHintDisplayed = true;
|
|
|
|
}
|
|
|
|
} else if (isArchive) {
|
|
|
|
// If the module cache directory doesn't exist at all and the object
|
|
|
|
// file is inside a static library, we assume that the static library
|
|
|
|
// was built on a different machine. We don't want to discourage module
|
|
|
|
// debugging for convenience libraries within a project though.
|
|
|
|
if (!ArchiveHintDisplayed) {
|
2016-05-21 04:36:06 +08:00
|
|
|
errs() << "note: Linking a static library that was built with "
|
|
|
|
"-gmodules, but the module cache was not found. "
|
|
|
|
"Redistributable static libraries should never be built "
|
|
|
|
"with module debugging enabled. The debug experience will "
|
|
|
|
"be degraded due to incomplete debug information.\n";
|
2016-01-15 02:31:07 +08:00
|
|
|
ArchiveHintDisplayed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-23 06:20:50 +08:00
|
|
|
return;
|
2016-01-15 02:31:07 +08:00
|
|
|
}
|
2015-09-23 06:20:50 +08:00
|
|
|
|
2015-09-23 18:38:59 +08:00
|
|
|
std::unique_ptr<CompileUnit> Unit;
|
2015-09-23 06:20:50 +08:00
|
|
|
|
|
|
|
// Setup access to the debug info.
|
2017-07-20 06:27:28 +08:00
|
|
|
auto DwarfContext = DWARFContext::create(*ErrOrObj);
|
2015-09-23 06:20:50 +08:00
|
|
|
RelocationManager RelocMgr(*this);
|
2017-07-20 06:27:28 +08:00
|
|
|
for (const auto &CU : DwarfContext->compile_units()) {
|
2017-08-09 02:26:12 +08:00
|
|
|
maybeUpdateMaxDwarfVersion(CU->getVersion());
|
|
|
|
|
2015-09-23 06:20:50 +08:00
|
|
|
// Recursively get all modules imported by this one.
|
2017-08-09 02:26:12 +08:00
|
|
|
auto CUDie = CU->getUnitDIE(false);
|
2016-12-14 02:25:19 +08:00
|
|
|
if (!registerModuleReference(CUDie, *CU, ModuleMap, Indent)) {
|
2015-09-23 06:20:50 +08:00
|
|
|
if (Unit) {
|
|
|
|
errs() << Filename << ": Clang modules are expected to have exactly"
|
|
|
|
<< " 1 compile unit.\n";
|
|
|
|
exitDsymutil(1);
|
|
|
|
}
|
2016-04-26 01:04:32 +08:00
|
|
|
// FIXME: Until PR27449 (https://llvm.org/bugs/show_bug.cgi?id=27449) is
|
|
|
|
// fixed in clang, only warn about DWO_id mismatches in verbose mode.
|
|
|
|
// ASTFileSignatures will change randomly when a module is rebuilt.
|
2016-12-14 02:25:19 +08:00
|
|
|
uint64_t PCMDwoId = getDwoId(CUDie, *CU);
|
2016-05-13 08:17:58 +08:00
|
|
|
if (PCMDwoId != DwoId) {
|
|
|
|
if (Options.Verbose)
|
|
|
|
reportWarning(
|
|
|
|
Twine("hash mismatch: this object file was built against a "
|
|
|
|
"different version of the module ") + Filename);
|
|
|
|
// Update the cache entry with the DwoId of the module loaded from disk.
|
|
|
|
ClangModules[Filename] = PCMDwoId;
|
|
|
|
}
|
2015-09-24 01:11:10 +08:00
|
|
|
|
|
|
|
// Add this module.
|
2015-09-24 01:35:52 +08:00
|
|
|
Unit = llvm::make_unique<CompileUnit>(*CU, UnitID++, !Options.NoODR,
|
|
|
|
ModuleName);
|
2015-09-23 06:20:50 +08:00
|
|
|
Unit->setHasInterestingContent();
|
2015-09-24 01:35:52 +08:00
|
|
|
analyzeContextInfo(CUDie, 0, *Unit, &ODRContexts.getRoot(), StringPool,
|
|
|
|
ODRContexts);
|
2015-09-23 06:20:50 +08:00
|
|
|
// Keep everything.
|
|
|
|
Unit->markEverythingAsKept();
|
|
|
|
}
|
|
|
|
}
|
2017-08-22 09:10:48 +08:00
|
|
|
if (!Unit->getOrigUnit().getUnitDIE().hasChildren())
|
|
|
|
return;
|
2015-09-23 06:20:50 +08:00
|
|
|
if (Options.Verbose) {
|
|
|
|
outs().indent(Indent);
|
|
|
|
outs() << "cloning .debug_info from " << Filename << "\n";
|
|
|
|
}
|
|
|
|
|
2016-12-02 02:56:29 +08:00
|
|
|
std::vector<std::unique_ptr<CompileUnit>> CompileUnits;
|
|
|
|
CompileUnits.push_back(std::move(Unit));
|
|
|
|
DIECloner(*this, RelocMgr, DIEAlloc, CompileUnits, Options)
|
2017-07-20 06:27:28 +08:00
|
|
|
.cloneAllCompileUnits(*DwarfContext);
|
2015-09-23 06:20:50 +08:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:27:28 +08:00
|
|
|
void DwarfLinker::DIECloner::cloneAllCompileUnits(DWARFContext &DwarfContext) {
|
2015-09-15 00:46:10 +08:00
|
|
|
if (!Linker.Streamer)
|
2015-09-12 07:45:30 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto &CurrentUnit : CompileUnits) {
|
2016-12-14 02:25:19 +08:00
|
|
|
auto InputDIE = CurrentUnit->getOrigUnit().getUnitDIE();
|
2016-12-02 02:56:29 +08:00
|
|
|
CurrentUnit->setStartOffset(Linker.OutputDebugInfoSize);
|
2017-02-10 03:41:55 +08:00
|
|
|
if (CurrentUnit->getInfo(0).Keep) {
|
|
|
|
// Clone the InputDIE into your Unit DIE in our compile unit since it
|
|
|
|
// already has a DIE inside of it.
|
|
|
|
CurrentUnit->createOutputDIE();
|
|
|
|
cloneDIE(InputDIE, *CurrentUnit, 0 /* PC offset */,
|
|
|
|
11 /* Unit Header size */, 0, CurrentUnit->getOutputUnitDIE());
|
|
|
|
}
|
2016-12-02 02:56:29 +08:00
|
|
|
Linker.OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
|
2015-09-15 00:46:10 +08:00
|
|
|
if (Linker.Options.NoOutput)
|
2015-09-12 07:45:30 +08:00
|
|
|
continue;
|
|
|
|
// FIXME: for compatibility with the classic dsymutil, we emit
|
|
|
|
// an empty line table for the unit, even if the unit doesn't
|
|
|
|
// actually exist in the DIE tree.
|
2016-12-02 02:56:29 +08:00
|
|
|
Linker.patchLineTableForUnit(*CurrentUnit, DwarfContext);
|
|
|
|
Linker.patchRangesForUnit(*CurrentUnit, DwarfContext);
|
|
|
|
Linker.Streamer->emitLocationsForUnit(*CurrentUnit, DwarfContext);
|
|
|
|
Linker.emitAcceleratorEntriesForUnit(*CurrentUnit);
|
2015-09-12 07:45:30 +08:00
|
|
|
}
|
|
|
|
|
2015-09-15 00:46:10 +08:00
|
|
|
if (Linker.Options.NoOutput)
|
2015-09-12 07:45:30 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Emit all the compile unit's debug information.
|
|
|
|
for (auto &CurrentUnit : CompileUnits) {
|
2016-12-02 02:56:29 +08:00
|
|
|
Linker.generateUnitRanges(*CurrentUnit);
|
|
|
|
CurrentUnit->fixupForwardReferences();
|
|
|
|
Linker.Streamer->emitCompileUnitHeader(*CurrentUnit);
|
|
|
|
if (!CurrentUnit->getOutputUnitDIE())
|
2015-09-12 07:45:30 +08:00
|
|
|
continue;
|
2016-12-02 02:56:29 +08:00
|
|
|
Linker.Streamer->emitDIE(*CurrentUnit->getOutputUnitDIE());
|
2015-09-12 07:45:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-29 02:27:01 +08:00
|
|
|
bool DwarfLinker::link(const DebugMap &Map) {
|
|
|
|
|
2015-02-28 08:29:11 +08:00
|
|
|
if (!createStreamer(Map.getTriple(), OutputFilename))
|
|
|
|
return false;
|
|
|
|
|
2015-03-05 06:07:44 +08:00
|
|
|
// Size of the DIEs (and headers) generated for the linked output.
|
2015-09-12 07:45:30 +08:00
|
|
|
OutputDebugInfoSize = 0;
|
2015-03-14 11:46:40 +08:00
|
|
|
// A unique ID that identifies each compile unit.
|
2015-09-23 06:20:50 +08:00
|
|
|
UnitID = 0;
|
|
|
|
DebugMap ModuleMap(Map.getTriple(), Map.getBinaryPath());
|
|
|
|
|
2015-01-29 02:27:01 +08:00
|
|
|
for (const auto &Obj : Map.objects()) {
|
2015-02-14 07:18:29 +08:00
|
|
|
CurrentDebugObject = Obj.get();
|
|
|
|
|
2015-02-28 08:29:07 +08:00
|
|
|
if (Options.Verbose)
|
2015-01-29 02:27:01 +08:00
|
|
|
outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n";
|
2017-10-06 22:49:20 +08:00
|
|
|
|
|
|
|
// N_AST objects (swiftmodule files) should get dumped directly into the
|
|
|
|
// appropriate DWARF section.
|
|
|
|
if (Obj->getType() == MachO::N_AST) {
|
2017-10-11 21:51:30 +08:00
|
|
|
auto ErrOrMemBufferRefs = BinHolder.GetMemoryBuffersForFile(
|
|
|
|
Obj->getObjectFilename(), Obj->getTimestamp());
|
|
|
|
if (ErrOrMemBufferRefs.getError())
|
2017-10-06 22:49:20 +08:00
|
|
|
continue;
|
|
|
|
if (!Options.NoOutput)
|
2017-10-11 21:51:30 +08:00
|
|
|
Streamer->emitSwiftAST(ErrOrMemBufferRefs.get());
|
2017-10-06 22:49:20 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-07-24 14:41:11 +08:00
|
|
|
auto ErrOrObj = loadObject(BinHolder, *Obj, Map);
|
|
|
|
if (!ErrOrObj)
|
2015-01-29 02:27:01 +08:00
|
|
|
continue;
|
|
|
|
|
2015-02-14 07:18:22 +08:00
|
|
|
// Look for relocations that correspond to debug map entries.
|
2015-09-12 07:45:30 +08:00
|
|
|
RelocationManager RelocMgr(*this);
|
|
|
|
if (!RelocMgr.findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) {
|
2015-02-28 08:29:07 +08:00
|
|
|
if (Options.Verbose)
|
2015-02-14 07:18:22 +08:00
|
|
|
outs() << "No valid relocations found. Skipping.\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-29 06:15:14 +08:00
|
|
|
// Setup access to the debug info.
|
2017-07-20 06:27:28 +08:00
|
|
|
auto DwarfContext = DWARFContext::create(*ErrOrObj);
|
|
|
|
startDebugObject(*DwarfContext, *Obj);
|
2015-01-29 02:27:01 +08:00
|
|
|
|
2015-10-06 07:11:20 +08:00
|
|
|
// In a first phase, just read in the debug info and load all clang modules.
|
2017-07-20 06:27:28 +08:00
|
|
|
for (const auto &CU : DwarfContext->compile_units()) {
|
2016-12-14 02:25:19 +08:00
|
|
|
auto CUDie = CU->getUnitDIE(false);
|
2015-02-28 08:29:07 +08:00
|
|
|
if (Options.Verbose) {
|
2015-01-29 02:27:01 +08:00
|
|
|
outs() << "Input compilation unit:";
|
2017-09-13 17:43:05 +08:00
|
|
|
DIDumpOptions DumpOpts;
|
2017-09-21 01:44:00 +08:00
|
|
|
DumpOpts.RecurseDepth = 0;
|
2017-09-13 17:43:05 +08:00
|
|
|
DumpOpts.Verbose = Options.Verbose;
|
2017-09-21 01:44:00 +08:00
|
|
|
CUDie.dump(outs(), 0, DumpOpts);
|
2015-01-29 02:27:01 +08:00
|
|
|
}
|
2015-10-06 07:11:20 +08:00
|
|
|
|
2017-04-21 03:16:51 +08:00
|
|
|
if (!registerModuleReference(CUDie, *CU, ModuleMap)) {
|
2016-12-02 02:56:29 +08:00
|
|
|
Units.push_back(llvm::make_unique<CompileUnit>(*CU, UnitID++,
|
|
|
|
!Options.NoODR, ""));
|
2017-04-21 03:16:51 +08:00
|
|
|
maybeUpdateMaxDwarfVersion(CU->getVersion());
|
|
|
|
}
|
2015-01-29 02:27:01 +08:00
|
|
|
}
|
2015-01-29 06:15:14 +08:00
|
|
|
|
2015-10-06 07:11:20 +08:00
|
|
|
// Now build the DIE parent links that we will use during the next phase.
|
|
|
|
for (auto &CurrentUnit : Units)
|
2016-12-02 02:56:29 +08:00
|
|
|
analyzeContextInfo(CurrentUnit->getOrigUnit().getUnitDIE(), 0, *CurrentUnit,
|
2015-10-06 07:11:20 +08:00
|
|
|
&ODRContexts.getRoot(), StringPool, ODRContexts);
|
|
|
|
|
2015-02-14 07:18:34 +08:00
|
|
|
// Then mark all the DIEs that need to be present in the linked
|
|
|
|
// output and collect some information about them. Note that this
|
|
|
|
// loop can not be merged with the previous one becaue cross-cu
|
|
|
|
// references require the ParentIdx to be setup for every CU in
|
|
|
|
// the object file before calling this.
|
|
|
|
for (auto &CurrentUnit : Units)
|
2016-12-14 02:25:19 +08:00
|
|
|
lookForDIEsToKeep(RelocMgr, CurrentUnit->getOrigUnit().getUnitDIE(), *Obj,
|
2016-12-02 02:56:29 +08:00
|
|
|
*CurrentUnit, 0);
|
2015-02-14 07:18:34 +08:00
|
|
|
|
2015-03-07 09:25:09 +08:00
|
|
|
// The calls to applyValidRelocs inside cloneDIE will walk the
|
|
|
|
// reloc array again (in the same way findValidRelocsInDebugInfo()
|
|
|
|
// did). We need to reset the NextValidReloc index to the beginning.
|
2015-09-12 07:45:30 +08:00
|
|
|
RelocMgr.resetValidRelocs();
|
|
|
|
if (RelocMgr.hasValidRelocs())
|
2015-09-15 00:46:10 +08:00
|
|
|
DIECloner(*this, RelocMgr, DIEAlloc, Units, Options)
|
2017-07-20 06:27:28 +08:00
|
|
|
.cloneAllCompileUnits(*DwarfContext);
|
2015-09-12 07:45:30 +08:00
|
|
|
if (!Options.NoOutput && !Units.empty())
|
2017-07-20 06:27:28 +08:00
|
|
|
patchFrameInfoForObject(*Obj, *DwarfContext,
|
2016-12-02 02:56:29 +08:00
|
|
|
Units[0]->getOrigUnit().getAddressByteSize());
|
2015-06-06 07:06:11 +08:00
|
|
|
|
2015-01-29 06:15:14 +08:00
|
|
|
// Clean-up before starting working on the next object.
|
|
|
|
endDebugObject();
|
2015-01-29 02:27:01 +08:00
|
|
|
}
|
|
|
|
|
2015-03-05 06:07:44 +08:00
|
|
|
// Emit everything that's global.
|
2015-03-07 01:56:30 +08:00
|
|
|
if (!Options.NoOutput) {
|
2017-04-21 03:16:51 +08:00
|
|
|
Streamer->emitAbbrevs(Abbreviations, MaxDwarfVersion);
|
2015-03-07 01:56:30 +08:00
|
|
|
Streamer->emitStrings(StringPool);
|
|
|
|
}
|
2015-03-05 06:07:44 +08:00
|
|
|
|
2015-09-03 00:49:13 +08:00
|
|
|
return Options.NoOutput ? true : Streamer->finish(Map);
|
2014-12-13 01:31:24 +08:00
|
|
|
}
|
|
|
|
}
|
2015-01-29 02:27:01 +08:00
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Get the offset of string \p S in the string table. This
|
2015-08-26 13:09:52 +08:00
|
|
|
/// can insert a new element or return the offset of a preexisitng
|
|
|
|
/// one.
|
|
|
|
uint32_t NonRelocatableStringpool::getStringOffset(StringRef S) {
|
|
|
|
if (S.empty() && !Strings.empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr);
|
|
|
|
MapTy::iterator It;
|
|
|
|
bool Inserted;
|
|
|
|
|
|
|
|
// A non-empty string can't be at offset 0, so if we have an entry
|
|
|
|
// with a 0 offset, it must be a previously interned string.
|
|
|
|
std::tie(It, Inserted) = Strings.insert(std::make_pair(S, Entry));
|
|
|
|
if (Inserted || It->getValue().first == 0) {
|
|
|
|
// Set offset and chain at the end of the entries list.
|
|
|
|
It->getValue().first = CurrentEndOffset;
|
|
|
|
CurrentEndOffset += S.size() + 1; // +1 for the '\0'.
|
|
|
|
Last->getValue().second = &*It;
|
|
|
|
Last = &*It;
|
|
|
|
}
|
|
|
|
return It->getValue().first;
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:51:17 +08:00
|
|
|
/// Put \p S into the StringMap so that it gets permanent
|
2015-08-26 13:09:52 +08:00
|
|
|
/// storage, but do not actually link it in the chain of elements
|
|
|
|
/// that go into the output section. A latter call to
|
|
|
|
/// getStringOffset() with the same string will chain it though.
|
|
|
|
StringRef NonRelocatableStringpool::internString(StringRef S) {
|
|
|
|
std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr);
|
|
|
|
auto InsertResult = Strings.insert(std::make_pair(S, Entry));
|
|
|
|
return InsertResult.first->getKey();
|
|
|
|
}
|
|
|
|
|
2015-08-26 13:09:55 +08:00
|
|
|
void warn(const Twine &Warning, const Twine &Context) {
|
|
|
|
errs() << Twine("while processing ") + Context + ":\n";
|
|
|
|
errs() << Twine("warning: ") + Warning + "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool error(const Twine &Error, const Twine &Context) {
|
|
|
|
errs() << Twine("while processing ") + Context + ":\n";
|
|
|
|
errs() << Twine("error: ") + Error + "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-28 08:29:07 +08:00
|
|
|
bool linkDwarf(StringRef OutputFilename, const DebugMap &DM,
|
|
|
|
const LinkOptions &Options) {
|
|
|
|
DwarfLinker Linker(OutputFilename, Options);
|
2015-01-29 02:27:01 +08:00
|
|
|
return Linker.link(DM);
|
|
|
|
}
|
|
|
|
}
|
2014-12-13 01:31:24 +08:00
|
|
|
}
|