RecordStreamer.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //===- RecordStreamer.h - Record asm defined and used symbols ---*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_LIB_OBJECT_RECORDSTREAMER_H
  9. #define LLVM_LIB_OBJECT_RECORDSTREAMER_H
  10. #include "llvm/ADT/DenseMap.h"
  11. #include "llvm/ADT/StringMap.h"
  12. #include "llvm/MC/MCDirectives.h"
  13. #include "llvm/MC/MCStreamer.h"
  14. #include "llvm/Support/SMLoc.h"
  15. #include <vector>
  16. namespace llvm {
  17. class MCSymbol;
  18. class Module;
  19. class RecordStreamer : public MCStreamer {
  20. public:
  21. enum State { NeverSeen, Global, Defined, DefinedGlobal, DefinedWeak, Used,
  22. UndefinedWeak};
  23. private:
  24. const Module &M;
  25. StringMap<State> Symbols;
  26. // Map of aliases created by .symver directives, saved so we can update
  27. // their symbol binding after parsing complete. This maps from each
  28. // aliasee to its list of aliases.
  29. DenseMap<const MCSymbol *, std::vector<StringRef>> SymverAliasMap;
  30. /// Get the state recorded for the given symbol.
  31. State getSymbolState(const MCSymbol *Sym);
  32. void markDefined(const MCSymbol &Symbol);
  33. void markGlobal(const MCSymbol &Symbol, MCSymbolAttr Attribute);
  34. void markUsed(const MCSymbol &Symbol);
  35. void visitUsedSymbol(const MCSymbol &Sym) override;
  36. public:
  37. RecordStreamer(MCContext &Context, const Module &M);
  38. void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
  39. void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
  40. void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
  41. bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
  42. void emitZerofill(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
  43. Align ByteAlignment, SMLoc Loc = SMLoc()) override;
  44. void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
  45. Align ByteAlignment) override;
  46. // Ignore COFF-specific directives; we do not need any information from them,
  47. // but the default implementation of these methods crashes, so we override
  48. // them with versions that do nothing.
  49. void beginCOFFSymbolDef(const MCSymbol *Symbol) override {}
  50. void emitCOFFSymbolStorageClass(int StorageClass) override {}
  51. void emitCOFFSymbolType(int Type) override {}
  52. void endCOFFSymbolDef() override {}
  53. /// Record .symver aliases for later processing.
  54. void emitELFSymverDirective(const MCSymbol *OriginalSym, StringRef Name,
  55. bool KeepOriginalSym) override;
  56. // Emit ELF .symver aliases and ensure they have the same binding as the
  57. // defined symbol they alias with.
  58. void flushSymverDirectives();
  59. // Symbols iterators
  60. using const_iterator = StringMap<State>::const_iterator;
  61. const_iterator begin();
  62. const_iterator end();
  63. // SymverAliasMap iterators
  64. using const_symver_iterator = decltype(SymverAliasMap)::const_iterator;
  65. iterator_range<const_symver_iterator> symverAliases();
  66. };
  67. } // end namespace llvm
  68. #endif // LLVM_LIB_OBJECT_RECORDSTREAMER_H