xref: /freebsd/contrib/llvm-project/llvm/lib/Object/RecordStreamer.h (revision e9e8876a4d6afc1ad5315faaa191b25121a813d7)
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 
9 #ifndef LLVM_LIB_OBJECT_RECORDSTREAMER_H
10 #define LLVM_LIB_OBJECT_RECORDSTREAMER_H
11 
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/StringMap.h"
14 #include "llvm/MC/MCDirectives.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/Support/SMLoc.h"
17 #include <vector>
18 
19 namespace llvm {
20 
21 class MCSymbol;
22 class Module;
23 
24 class RecordStreamer : public MCStreamer {
25 public:
26   enum State { NeverSeen, Global, Defined, DefinedGlobal, DefinedWeak, Used,
27                UndefinedWeak};
28 
29 private:
30   const Module &M;
31   StringMap<State> Symbols;
32   // Map of aliases created by .symver directives, saved so we can update
33   // their symbol binding after parsing complete. This maps from each
34   // aliasee to its list of aliases.
35   DenseMap<const MCSymbol *, std::vector<StringRef>> SymverAliasMap;
36 
37   /// Get the state recorded for the given symbol.
38   State getSymbolState(const MCSymbol *Sym);
39 
40   void markDefined(const MCSymbol &Symbol);
41   void markGlobal(const MCSymbol &Symbol, MCSymbolAttr Attribute);
42   void markUsed(const MCSymbol &Symbol);
43   void visitUsedSymbol(const MCSymbol &Sym) override;
44 
45 public:
46   RecordStreamer(MCContext &Context, const Module &M);
47 
48   void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
49   void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
50   void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
51   bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
52   void emitZerofill(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
53                     unsigned ByteAlignment, SMLoc Loc = SMLoc()) override;
54   void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
55                         unsigned ByteAlignment) override;
56 
57   // Ignore COFF-specific directives; we do not need any information from them,
58   // but the default implementation of these methods crashes, so we override
59   // them with versions that do nothing.
60   void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {}
61   void EmitCOFFSymbolStorageClass(int StorageClass) override {}
62   void EmitCOFFSymbolType(int Type) override {}
63   void EndCOFFSymbolDef() override {}
64 
65   /// Record .symver aliases for later processing.
66   void emitELFSymverDirective(const MCSymbol *OriginalSym, StringRef Name,
67                               bool KeepOriginalSym) override;
68 
69   // Emit ELF .symver aliases and ensure they have the same binding as the
70   // defined symbol they alias with.
71   void flushSymverDirectives();
72 
73   // Symbols iterators
74   using const_iterator = StringMap<State>::const_iterator;
75   const_iterator begin();
76   const_iterator end();
77 
78   // SymverAliasMap iterators
79   using const_symver_iterator = decltype(SymverAliasMap)::const_iterator;
80   iterator_range<const_symver_iterator> symverAliases();
81 };
82 
83 } // end namespace llvm
84 
85 #endif // LLVM_LIB_OBJECT_RECORDSTREAMER_H
86