Path: blob/main/contrib/llvm-project/llvm/lib/DWARFLinker/Parallel/DWARFLinkerUnit.h
35292 views
//===- DWARFLinkerUnit.h ----------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIB_DWARFLINKER_PARALLEL_DWARFLINKERUNIT_H9#define LLVM_LIB_DWARFLINKER_PARALLEL_DWARFLINKERUNIT_H1011#include "DWARFLinkerGlobalData.h"12#include "OutputSections.h"13#include "llvm/CodeGen/DIE.h"14#include "llvm/DWARFLinker/IndexedValuesMap.h"15#include "llvm/DWARFLinker/Parallel/DWARFLinker.h"16#include "llvm/DWARFLinker/StringPool.h"17#include "llvm/DebugInfo/DWARF/DWARFUnit.h"18#include "llvm/Support/LEB128.h"1920namespace llvm {21namespace dwarf_linker {22namespace parallel {2324class DwarfUnit;25using MacroOffset2UnitMapTy = DenseMap<uint64_t, DwarfUnit *>;2627/// Base class for all Dwarf units(Compile unit/Type table unit).28class DwarfUnit : public OutputSections {29public:30virtual ~DwarfUnit() {}31DwarfUnit(LinkingGlobalData &GlobalData, unsigned ID,32StringRef ClangModuleName)33: OutputSections(GlobalData), ID(ID), ClangModuleName(ClangModuleName),34OutUnitDIE(nullptr) {}3536/// Unique id of the unit.37unsigned getUniqueID() const { return ID; }3839/// Returns size of this(newly generated) compile unit.40uint64_t getUnitSize() const { return UnitSize; }4142/// Returns this unit name.43StringRef getUnitName() const { return UnitName; }4445/// Return the DW_AT_LLVM_sysroot of the compile unit or an empty StringRef.46StringRef getSysRoot() { return SysRoot; }4748/// Return true if this compile unit is from Clang module.49bool isClangModule() const { return !ClangModuleName.empty(); }5051/// Return Clang module name;52const std::string &getClangModuleName() const { return ClangModuleName; }5354/// Return global data.55LinkingGlobalData &getGlobalData() { return GlobalData; }5657/// Returns true if unit is inter-connected(it references/referenced by other58/// unit).59bool isInterconnectedCU() const { return IsInterconnectedCU; }6061/// Mark this unit as inter-connected(it references/referenced by other unit).62void setInterconnectedCU() { IsInterconnectedCU = true; }6364/// Adds \p Abbrev into unit`s abbreviation table.65void assignAbbrev(DIEAbbrev &Abbrev);6667/// Returns abbreviations for this compile unit.68const std::vector<std::unique_ptr<DIEAbbrev>> &getAbbreviations() const {69return Abbreviations;70}7172/// Returns output unit DIE.73DIE *getOutUnitDIE() { return OutUnitDIE; }7475/// Set output unit DIE.76void setOutUnitDIE(DIE *UnitDie) {77OutUnitDIE = UnitDie;7879if (OutUnitDIE != nullptr) {80UnitSize = getDebugInfoHeaderSize() + OutUnitDIE->getSize();81UnitTag = OutUnitDIE->getTag();82}83}8485/// Returns unit DWARF tag.86dwarf::Tag getTag() const { return UnitTag; }8788/// \defgroup Methods used to emit unit's debug info:89///90/// @{91/// Emit unit's abbreviations.92Error emitAbbreviations();9394/// Emit .debug_info section for unit DIEs.95Error emitDebugInfo(const Triple &TargetTriple);9697/// Emit .debug_line section.98Error emitDebugLine(const Triple &TargetTriple,99const DWARFDebugLine::LineTable &OutLineTable);100101/// Emit the .debug_str_offsets section for current unit.102Error emitDebugStringOffsetSection();103/// @}104105/// \defgroup Methods used for reporting warnings and errors:106///107/// @{108void warn(const Twine &Warning) { GlobalData.warn(Warning, getUnitName()); }109110void error(const Twine &Err) { GlobalData.warn(Err, getUnitName()); }111/// @}112113/// \defgroup Methods and data members used for building accelerator tables:114///115/// @{116117enum class AccelType : uint8_t { None, Name, Namespace, ObjC, Type };118119/// This structure keeps fields which would be used for creating accelerator120/// table.121struct AccelInfo {122AccelInfo() {123AvoidForPubSections = false;124ObjcClassImplementation = false;125}126127/// Name of the entry.128StringEntry *String = nullptr;129130/// Output offset of the DIE this entry describes.131uint64_t OutOffset;132133/// Hash of the fully qualified name.134uint32_t QualifiedNameHash = 0;135136/// Tag of the DIE this entry describes.137dwarf::Tag Tag = dwarf::DW_TAG_null;138139/// Type of this accelerator record.140AccelType Type = AccelType::None;141142/// Avoid emitting this entry for pub sections.143bool AvoidForPubSections : 1;144145/// Is this an ObjC class implementation?146bool ObjcClassImplementation : 1;147};148149/// Emit .debug_pubnames and .debug_pubtypes for \p Unit.150void emitPubAccelerators();151152/// Enumerates accelerator data.153virtual void154forEachAcceleratorRecord(function_ref<void(AccelInfo &)> Handler) = 0;155156/// @}157158/// Returns index(inside .debug_str_offsets) of specified string.159virtual uint64_t getDebugStrIndex(const StringEntry *String) {160return DebugStringIndexMap.getValueIndex(String);161}162163protected:164/// Emit single abbreviation entry.165void emitDwarfAbbrevEntry(const DIEAbbrev &Abbrev,166SectionDescriptor &AbbrevSection);167168/// Emit single pubnames/pubtypes accelerator entry.169std::optional<uint64_t>170emitPubAcceleratorEntry(SectionDescriptor &OutSection, const AccelInfo &Info,171std::optional<uint64_t> LengthOffset);172173/// Unique ID for the unit.174unsigned ID = 0;175176/// The name of this unit.177std::string UnitName;178179/// The DW_AT_LLVM_sysroot of this unit.180std::string SysRoot;181182/// If this is a Clang module, this holds the module's name.183std::string ClangModuleName;184185uint64_t UnitSize = 0;186187/// DWARF unit tag.188dwarf::Tag UnitTag = dwarf::DW_TAG_null;189190/// true if current unit references_to/is_referenced by other unit.191std::atomic<bool> IsInterconnectedCU = {false};192193/// FoldingSet that uniques the abbreviations.194FoldingSet<DIEAbbrev> AbbreviationsSet;195196/// Storage for the unique Abbreviations.197std::vector<std::unique_ptr<DIEAbbrev>> Abbreviations;198199/// Output unit DIE.200DIE *OutUnitDIE = nullptr;201202/// Cache for file names for this unit.203using FileNamesCache =204DenseMap<uint64_t, std::pair<std::string, std::string>>;205FileNamesCache FileNames;206207/// Maps a string into the index inside .debug_str_offsets section.208IndexedValuesMap<const StringEntry *> DebugStringIndexMap;209};210211inline bool isODRLanguage(uint16_t Language) {212switch (Language) {213case dwarf::DW_LANG_C_plus_plus:214case dwarf::DW_LANG_C_plus_plus_03:215case dwarf::DW_LANG_C_plus_plus_11:216case dwarf::DW_LANG_C_plus_plus_14:217case dwarf::DW_LANG_ObjC_plus_plus:218return true;219default:220return false;221};222223return false;224}225226} // end of namespace parallel227} // end of namespace dwarf_linker228} // end of namespace llvm229230#endif // LLVM_LIB_DWARFLINKER_PARALLEL_DWARFLINKERUNIT_H231232233