#ifndef LLVM_TOOLS_DSYMUTIL_DWARFLINKER_H
#define LLVM_TOOLS_DSYMUTIL_DWARFLINKER_H
#include "BinaryHolder.h"
#include "DebugMap.h"
#include "LinkUtils.h"
#include "MachOUtils.h"
#include "llvm/DWARFLinker/DWARFLinker.h"
#include "llvm/DWARFLinker/DWARFLinkerCompileUnit.h"
#include "llvm/DWARFLinker/DWARFLinkerDeclContext.h"
#include "llvm/DWARFLinker/DWARFStreamer.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
#include "llvm/Remarks/RemarkFormat.h"
#include "llvm/Remarks/RemarkLinker.h"
namespace llvm {
namespace dsymutil {
class DwarfLinkerForBinary {
public:
DwarfLinkerForBinary(raw_fd_ostream &OutFile, BinaryHolder &BinHolder,
LinkOptions Options)
: OutFile(OutFile), BinHolder(BinHolder), Options(std::move(Options)) {}
bool link(const DebugMap &);
void reportWarning(const Twine &Warning, StringRef Context,
const DWARFDie *DIE = nullptr) const;
enum TraversalFlags {
TF_Keep = 1 << 0, TF_InFunctionScope = 1 << 1, TF_DependencyWalk = 1 << 2, TF_ParentWalk = 1 << 3, TF_ODR = 1 << 4, TF_SkipPC = 1 << 5, };
private:
class AddressManager : public AddressesMap {
struct ValidReloc {
uint64_t Offset;
uint32_t Size;
uint64_t Addend;
const DebugMapObject::DebugMapEntry *Mapping;
ValidReloc(uint64_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;
}
bool operator<(uint64_t RHS) const { return Offset < RHS; }
};
const DwarfLinkerForBinary &Linker;
std::vector<ValidReloc> ValidDebugInfoRelocs;
std::vector<ValidReloc> ValidDebugAddrRelocs;
RangesTy AddressRanges;
StringRef SrcFileName;
std::vector<ValidReloc>
getRelocations(const std::vector<ValidReloc> &Relocs, uint64_t StartPos,
uint64_t EndPos);
uint64_t relocate(const ValidReloc &Reloc) const;
void fillDieInfo(const ValidReloc &Reloc, CompileUnit::DIEInfo &Info);
void printReloc(const ValidReloc &Reloc);
public:
AddressManager(DwarfLinkerForBinary &Linker, const object::ObjectFile &Obj,
const DebugMapObject &DMO)
: Linker(Linker), SrcFileName(DMO.getObjectFilename()) {
findValidRelocsInDebugSections(Obj, DMO);
for (const auto &Entry : DMO.symbols()) {
const auto &Mapping = Entry.getValue();
if (Mapping.Size && Mapping.ObjectAddress)
AddressRanges.insert(
{*Mapping.ObjectAddress, *Mapping.ObjectAddress + Mapping.Size},
int64_t(Mapping.BinaryAddress) - *Mapping.ObjectAddress);
}
}
~AddressManager() override { clear(); }
bool hasValidRelocs() override {
return !ValidDebugInfoRelocs.empty() || !ValidDebugAddrRelocs.empty();
}
bool findValidRelocsInDebugSections(const object::ObjectFile &Obj,
const DebugMapObject &DMO);
bool findValidRelocs(const object::SectionRef &Section,
const object::ObjectFile &Obj,
const DebugMapObject &DMO,
std::vector<ValidReloc> &ValidRelocs);
void findValidRelocsMachO(const object::SectionRef &Section,
const object::MachOObjectFile &Obj,
const DebugMapObject &DMO,
std::vector<ValidReloc> &ValidRelocs);
bool hasValidRelocationAt(const std::vector<ValidReloc> &Relocs,
uint64_t StartOffset, uint64_t EndOffset,
CompileUnit::DIEInfo &Info);
bool isLiveVariable(const DWARFDie &DIE,
CompileUnit::DIEInfo &Info) override;
bool isLiveSubprogram(const DWARFDie &DIE,
CompileUnit::DIEInfo &Info) override;
bool applyValidRelocs(MutableArrayRef<char> Data, uint64_t BaseOffset,
bool IsLittleEndian) override;
llvm::Expected<uint64_t> relocateIndexedAddr(uint64_t StartOffset,
uint64_t EndOffset) override;
RangesTy &getValidAddressRanges() override { return AddressRanges; }
void clear() override {
AddressRanges.clear();
ValidDebugInfoRelocs.clear();
ValidDebugAddrRelocs.clear();
}
};
private:
bool createStreamer(const Triple &TheTriple, raw_fd_ostream &OutFile);
ErrorOr<const object::ObjectFile &> loadObject(const DebugMapObject &Obj,
const Triple &triple);
ErrorOr<DWARFFile &> loadObject(const DebugMapObject &Obj,
const DebugMap &DebugMap,
remarks::RemarkLinker &RL);
void collectRelocationsToApplyToSwiftReflectionSections(
const object::SectionRef &Section, StringRef &Contents,
const llvm::object::MachOObjectFile *MO,
const std::vector<uint64_t> &SectionToOffsetInDwarf,
const llvm::dsymutil::DebugMapObject *Obj,
std::vector<MachOUtils::DwarfRelocationApplicationInfo>
&RelocationsToApply) const;
void copySwiftReflectionMetadata(
const llvm::dsymutil::DebugMapObject *Obj, DwarfStreamer *Streamer,
std::vector<uint64_t> &SectionToOffsetInDwarf,
std::vector<MachOUtils::DwarfRelocationApplicationInfo>
&RelocationsToApply);
raw_fd_ostream &OutFile;
BinaryHolder &BinHolder;
LinkOptions Options;
std::unique_ptr<DwarfStreamer> Streamer;
std::vector<std::unique_ptr<DWARFFile>> ObjectsForLinking;
std::vector<std::unique_ptr<DWARFContext>> ContextForLinking;
std::vector<std::unique_ptr<AddressManager>> AddressMapForLinking;
std::vector<std::string> EmptyWarnings;
std::map<std::string, std::string> ParseableSwiftInterfaces;
bool ModuleCacheHintDisplayed = false;
bool ArchiveHintDisplayed = false;
};
} }
#endif