blob: c8b75bcc6d1dfad126cd0404306dbc2459bc184e [file] [log] [blame]
Eugene Zelenko1df42fa2017-04-24 23:21:38 +00001//===- RecordStreamer.h - Record asm defined and used symbols ---*- C++ -*-===//
Rafael Espindola13b69d62014-07-03 18:59:23 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Rafael Espindola13b69d62014-07-03 18:59:23 +00006//
7//===----------------------------------------------------------------------===//
8
Benjamin Kramera7c40ef2014-08-13 16:26:38 +00009#ifndef LLVM_LIB_OBJECT_RECORDSTREAMER_H
10#define LLVM_LIB_OBJECT_RECORDSTREAMER_H
Rafael Espindola13b69d62014-07-03 18:59:23 +000011
Eugene Zelenko1df42fa2017-04-24 23:21:38 +000012#include "llvm/ADT/DenseMap.h"
13#include "llvm/ADT/StringMap.h"
14#include "llvm/MC/MCDirectives.h"
Rafael Espindola13b69d62014-07-03 18:59:23 +000015#include "llvm/MC/MCStreamer.h"
Eugene Zelenko1df42fa2017-04-24 23:21:38 +000016#include "llvm/MC/MCSymbol.h"
17#include "llvm/Support/SMLoc.h"
18#include <vector>
Rafael Espindola13b69d62014-07-03 18:59:23 +000019
20namespace llvm {
Eugene Zelenko1df42fa2017-04-24 23:21:38 +000021
Vitaly Buka0d038812018-03-20 00:38:33 +000022class GlobalValue;
23class Module;
24
Rafael Espindola13b69d62014-07-03 18:59:23 +000025class RecordStreamer : public MCStreamer {
26public:
Davide Italianof7518492016-09-15 17:54:22 +000027 enum State { NeverSeen, Global, Defined, DefinedGlobal, DefinedWeak, Used,
28 UndefinedWeak};
Rafael Espindola13b69d62014-07-03 18:59:23 +000029
30private:
Vitaly Buka0d038812018-03-20 00:38:33 +000031 const Module &M;
Rafael Espindola13b69d62014-07-03 18:59:23 +000032 StringMap<State> Symbols;
Teresa Johnsond8204472017-03-09 00:19:49 +000033 // Map of aliases created by .symver directives, saved so we can update
34 // their symbol binding after parsing complete. This maps from each
35 // aliasee to its list of aliases.
Vitaly Buka0d038812018-03-20 00:38:33 +000036 DenseMap<const MCSymbol *, std::vector<StringRef>> SymverAliasMap;
37
38 /// Get the state recorded for the given symbol.
39 State getSymbolState(const MCSymbol *Sym);
Eugene Zelenko1df42fa2017-04-24 23:21:38 +000040
Rafael Espindola13b69d62014-07-03 18:59:23 +000041 void markDefined(const MCSymbol &Symbol);
Davide Italianoec7e29e2016-06-22 20:48:15 +000042 void markGlobal(const MCSymbol &Symbol, MCSymbolAttr Attribute);
Rafael Espindola13b69d62014-07-03 18:59:23 +000043 void markUsed(const MCSymbol &Symbol);
44 void visitUsedSymbol(const MCSymbol &Sym) override;
45
46public:
Vitaly Buka0d038812018-03-20 00:38:33 +000047 RecordStreamer(MCContext &Context, const Module &M);
Eugene Zelenko1df42fa2017-04-24 23:21:38 +000048
Andrea Di Biagioedbf06a2019-02-04 12:51:26 +000049 void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
Rafael Espindolabe991572017-02-10 15:13:12 +000050 void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
Rafael Espindola13b69d62014-07-03 18:59:23 +000051 void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
52 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
Rafael Espindola0709a7b2015-05-21 19:20:38 +000053 void EmitZerofill(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
Francis Visoiu Mistrih4d5b1072018-07-02 17:29:43 +000054 unsigned ByteAlignment, SMLoc Loc = SMLoc()) override;
Rafael Espindola13b69d62014-07-03 18:59:23 +000055 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
56 unsigned ByteAlignment) override;
Bob Haarman38ebaf72019-01-24 21:41:03 +000057
58 // Ignore COFF-specific directives; we do not need any information from them,
59 // but the default implementation of these methods crashes, so we override
60 // them with versions that do nothing.
61 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {}
62 void EmitCOFFSymbolStorageClass(int StorageClass) override {}
63 void EmitCOFFSymbolType(int Type) override {}
64 void EndCOFFSymbolDef() override {}
65
Teresa Johnsond8204472017-03-09 00:19:49 +000066 /// Record .symver aliases for later processing.
Rafael Espindola47b4d6b2018-03-09 18:42:25 +000067 void emitELFSymverDirective(StringRef AliasName,
Teresa Johnsond8204472017-03-09 00:19:49 +000068 const MCSymbol *Aliasee) override;
Vlad Tsyrklevich230b2562018-04-20 01:36:48 +000069
Vitaly Buka0d038812018-03-20 00:38:33 +000070 // Emit ELF .symver aliases and ensure they have the same binding as the
71 // defined symbol they alias with.
72 void flushSymverDirectives();
Vlad Tsyrklevich230b2562018-04-20 01:36:48 +000073
74 // Symbols iterators
75 using const_iterator = StringMap<State>::const_iterator;
76 const_iterator begin();
77 const_iterator end();
78
79 // SymverAliasMap iterators
80 using const_symver_iterator = decltype(SymverAliasMap)::const_iterator;
81 iterator_range<const_symver_iterator> symverAliases();
Rafael Espindola13b69d62014-07-03 18:59:23 +000082};
Eugene Zelenko1df42fa2017-04-24 23:21:38 +000083
84} // end namespace llvm
85
86#endif // LLVM_LIB_OBJECT_RECORDSTREAMER_H