xref: /freebsd/contrib/llvm-project/llvm/lib/Object/RecordStreamer.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
49   void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
50   bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
51   void emitZerofill(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
52                     Align ByteAlignment, SMLoc Loc = SMLoc()) override;
53   void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
54                         Align ByteAlignment) override;
55 
56   // Ignore COFF-specific directives; we do not need any information from them,
57   // but the default implementation of these methods crashes, so we override
58   // them with versions that do nothing.
59   void beginCOFFSymbolDef(const MCSymbol *Symbol) override {}
60   void emitCOFFSymbolStorageClass(int StorageClass) override {}
61   void emitCOFFSymbolType(int Type) override {}
62   void endCOFFSymbolDef() override {}
63 
64   /// Record .symver aliases for later processing.
65   void emitELFSymverDirective(const MCSymbol *OriginalSym, StringRef Name,
66                               bool KeepOriginalSym) override;
67 
68   // Emit ELF .symver aliases and ensure they have the same binding as the
69   // defined symbol they alias with.
70   void flushSymverDirectives();
71 
72   // Symbols iterators
73   using const_iterator = StringMap<State>::const_iterator;
74   const_iterator begin();
75   const_iterator end();
76 
77   // SymverAliasMap iterators
78   using const_symver_iterator = decltype(SymverAliasMap)::const_iterator;
79   iterator_range<const_symver_iterator> symverAliases();
80 };
81 
82 } // end namespace llvm
83 
84 #endif // LLVM_LIB_OBJECT_RECORDSTREAMER_H
85