LLVM  14.0.0git
MCELFStreamer.h
Go to the documentation of this file.
1 //===- MCELFStreamer.h - MCStreamer ELF Object File Interface ---*- 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_MC_MCELFSTREAMER_H
10 #define LLVM_MC_MCELFSTREAMER_H
11 
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/BinaryFormat/ELF.h"
14 #include "llvm/MC/MCDirectives.h"
16 
17 namespace llvm {
18 
19 class MCAsmBackend;
20 class MCCodeEmitter;
21 class MCExpr;
22 class MCInst;
23 
25 public:
26  MCELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
27  std::unique_ptr<MCObjectWriter> OW,
28  std::unique_ptr<MCCodeEmitter> Emitter);
29 
30  ~MCELFStreamer() override = default;
31 
32  /// state management
33  void reset() override {
34  SeenIdent = false;
35  BundleGroups.clear();
37  }
38 
39  /// \name MCStreamer Interface
40  /// @{
41 
42  void initSections(bool NoExecStack, const MCSubtargetInfo &STI) override;
43  void changeSection(MCSection *Section, const MCExpr *Subsection) override;
44  void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
46  uint64_t Offset) override;
47  void emitAssemblerFlag(MCAssemblerFlag Flag) override;
48  void emitThumbFunc(MCSymbol *Func) override;
49  void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
51  void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
53  unsigned ByteAlignment) override;
54 
55  void emitELFSize(MCSymbol *Symbol, const MCExpr *Value) override;
56  void emitELFSymverDirective(const MCSymbol *OriginalSym, StringRef Name,
57  bool KeepOriginalSym) override;
58 
60  unsigned ByteAlignment) override;
61 
62  void emitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
63  uint64_t Size = 0, unsigned ByteAlignment = 0,
64  SMLoc L = SMLoc()) override;
66  unsigned ByteAlignment = 0) override;
67  void emitValueImpl(const MCExpr *Value, unsigned Size,
68  SMLoc Loc = SMLoc()) override;
69 
70  void emitIdent(StringRef IdentString) override;
71 
72  void emitValueToAlignment(unsigned, int64_t, unsigned, unsigned) override;
73 
75  const MCSymbolRefExpr *To, uint64_t Count) override;
76 
77  void finishImpl() override;
78 
79  void emitBundleAlignMode(unsigned AlignPow2) override;
80  void emitBundleLock(bool AlignToEnd) override;
81  void emitBundleUnlock() override;
82 
83  /// ELF object attributes section emission support
84  struct AttributeItem {
85  // This structure holds all attributes, accounting for their string /
86  // numeric value, so we can later emit them in declaration order, keeping
87  // all in the same vector.
88  enum {
93  } Type;
94  unsigned Tag;
95  unsigned IntValue;
96  std::string StringValue;
97  };
98 
99  // Attributes that are added and managed entirely by target.
101  void setAttributeItem(unsigned Attribute, unsigned Value,
102  bool OverwriteExisting);
103  void setAttributeItem(unsigned Attribute, StringRef Value,
104  bool OverwriteExisting);
105  void setAttributeItems(unsigned Attribute, unsigned IntValue,
106  StringRef StringValue, bool OverwriteExisting);
108  unsigned Type, MCSection *&AttributeSection) {
109  createAttributesSection(Vendor, Section, Type, AttributeSection, Contents);
110  }
111 
112 private:
113  AttributeItem *getAttributeItem(unsigned Attribute);
114  size_t calculateContentSize(SmallVector<AttributeItem, 64> &AttrsVec);
115  void createAttributesSection(StringRef Vendor, const Twine &Section,
116  unsigned Type, MCSection *&AttributeSection,
118 
119  // GNU attributes that will get emitted at the end of the asm file.
120  SmallVector<AttributeItem, 64> GNUAttributes;
121 
122 public:
123  void emitGNUAttribute(unsigned Tag, unsigned Value) override {
125  std::string(StringRef(""))};
126  GNUAttributes.push_back(Item);
127  }
128 
129 private:
130  bool isBundleLocked() const;
131  void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &) override;
132  void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &) override;
133 
134  void fixSymbolsInTLSFixups(const MCExpr *expr);
135  void finalizeCGProfileEntry(const MCSymbolRefExpr *&S, uint64_t Offset);
136  void finalizeCGProfile();
137 
138  /// Merge the content of the fragment \p EF into the fragment \p DF.
139  void mergeFragment(MCDataFragment *, MCDataFragment *);
140 
141  bool SeenIdent = false;
142 
143  /// BundleGroups - The stack of fragments holding the bundle-locked
144  /// instructions.
146 };
147 
148 MCELFStreamer *createARMELFStreamer(MCContext &Context,
149  std::unique_ptr<MCAsmBackend> TAB,
150  std::unique_ptr<MCObjectWriter> OW,
151  std::unique_ptr<MCCodeEmitter> Emitter,
152  bool RelaxAll, bool IsThumb, bool IsAndroid);
153 
154 } // end namespace llvm
155 
156 #endif // LLVM_MC_MCELFSTREAMER_H
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::MCELFStreamer::emitLabel
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
Definition: MCELFStreamer.cpp:100
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
llvm::Attribute
Definition: Attributes.h:52
MCDirectives.h
llvm::MCELFStreamer::emitZerofill
void emitZerofill(MCSection *Section, MCSymbol *Symbol=nullptr, uint64_t Size=0, unsigned ByteAlignment=0, SMLoc L=SMLoc()) override
Emit the zerofill section and an optional symbol.
Definition: MCELFStreamer.cpp:727
llvm::SmallVector< AttributeItem, 64 >
llvm::MCELFStreamer::changeSection
void changeSection(MCSection *Section, const MCExpr *Subsection) override
Update streamer for a new active section.
Definition: MCELFStreamer.cpp:147
llvm::MCELFStreamer::MCELFStreamer
MCELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
Definition: MCELFStreamer.cpp:42
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::MCELFStreamer::emitTBSSSymbol
void emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment=0) override
Emit a thread local bss (.tbss) symbol.
Definition: MCELFStreamer.cpp:733
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::MCAssemblerFlag
MCAssemblerFlag
Definition: MCDirectives.h:50
llvm::MCELFStreamer::reset
void reset() override
state management
Definition: MCELFStreamer.h:33
llvm::MCObjectStreamer::reset
void reset() override
state management
Definition: MCObjectStreamer.cpp:157
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::MCSymbolAttr
MCSymbolAttr
Definition: MCDirectives.h:18
llvm::MCELFStreamer::AttributeItem::TextAttribute
@ TextAttribute
Definition: MCELFStreamer.h:91
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::ARMBuildAttrs::Section
@ Section
Legacy Tags.
Definition: ARMBuildAttributes.h:78
MCObjectStreamer.h
llvm::MCFragment
Definition: MCFragment.h:31
ELF.h
llvm::MCELFStreamer
Definition: MCELFStreamer.h:24
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
llvm::MCELFStreamer::~MCELFStreamer
~MCELFStreamer() override=default
llvm::MCELFStreamer::emitCGProfileEntry
void emitCGProfileEntry(const MCSymbolRefExpr *From, const MCSymbolRefExpr *To, uint64_t Count) override
Definition: MCELFStreamer.cpp:375
llvm::MCObjectStreamer
Streaming object file generation interface.
Definition: MCObjectStreamer.h:36
llvm::MCELFStreamer::emitBundleLock
void emitBundleLock(bool AlignToEnd) override
The following instructions are a bundle-locked group.
Definition: MCELFStreamer.cpp:646
llvm::MCID::Flag
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:146
llvm::MCELFStreamer::emitWeakReference
void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override
Emit an weak reference from Alias to Symbol.
Definition: MCELFStreamer.cpp:167
llvm::MCELFStreamer::emitCommonSymbol
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment) override
Emit a common symbol.
Definition: MCELFStreamer.cpp:306
llvm::MCELFStreamer::Contents
SmallVector< AttributeItem, 64 > Contents
Definition: MCELFStreamer.h:100
llvm::MCELFStreamer::emitLabelAtPos
void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment *F, uint64_t Offset) override
Definition: MCELFStreamer.cpp:110
llvm::MCELFStreamer::emitValueToAlignment
void emitValueToAlignment(unsigned, int64_t, unsigned, unsigned) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
Definition: MCELFStreamer.cpp:365
llvm::MCELFStreamer::emitThumbFunc
void emitThumbFunc(MCSymbol *Func) override
Note in the output that the specified Func is a Thumb mode function (ARM target only).
Definition: MCELFStreamer.cpp:719
llvm::MCELFStreamer::finishImpl
void finishImpl() override
Streamer specific finalization.
Definition: MCELFStreamer.cpp:701
llvm::MCELFStreamer::emitELFSymverDirective
void emitELFSymverDirective(const MCSymbol *OriginalSym, StringRef Name, bool KeepOriginalSym) override
Emit an ELF .symver directive.
Definition: MCELFStreamer.cpp:341
llvm::MCELFStreamer::emitLocalCommonSymbol
void emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment) override
Emit a local common (.lcomm) symbol.
Definition: MCELFStreamer.cpp:348
uint64_t
llvm::MCELFStreamer::emitIdent
void emitIdent(StringRef IdentString) override
Emit the "identifiers" directive.
Definition: MCELFStreamer.cpp:381
llvm::MCELFStreamer::emitSymbolDesc
void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override
Set the DescValue for the Symbol.
Definition: MCELFStreamer.cpp:723
llvm::MCELFStreamer::AttributeItem::NumericAttribute
@ NumericAttribute
Definition: MCELFStreamer.h:90
llvm::MCELFStreamer::AttributeItem::IntValue
unsigned IntValue
Definition: MCELFStreamer.h:95
llvm::MCSymbolRefExpr
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
llvm::createARMELFStreamer
MCELFStreamer * createARMELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter, bool RelaxAll, bool IsThumb, bool IsAndroid)
Definition: ARMELFStreamer.cpp:1350
llvm::MCELFStreamer::setAttributeItem
void setAttributeItem(unsigned Attribute, unsigned Value, bool OverwriteExisting)
Definition: MCELFStreamer.cpp:738
llvm::MCELFStreamer::emitValueImpl
void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
Definition: MCELFStreamer.cpp:357
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::MCELFStreamer::setAttributeItems
void setAttributeItems(unsigned Attribute, unsigned IntValue, StringRef StringValue, bool OverwriteExisting)
Definition: MCELFStreamer.cpp:772
llvm::MCELFStreamer::emitGNUAttribute
void emitGNUAttribute(unsigned Tag, unsigned Value) override
Emit a .gnu_attribute directive.
Definition: MCELFStreamer.h:123
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::MCELFStreamer::AttributeItem::NumericAndTextAttributes
@ NumericAndTextAttributes
Definition: MCELFStreamer.h:92
llvm::MCELFStreamer::AttributeItem
ELF object attributes section emission support.
Definition: MCELFStreamer.h:84
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::MCELFStreamer::emitBundleAlignMode
void emitBundleAlignMode(unsigned AlignPow2) override
Set the bundle alignment mode from now on in the section.
Definition: MCELFStreamer.cpp:636
llvm::MCELFStreamer::AttributeItem::Type
enum llvm::MCELFStreamer::AttributeItem::@298 Type
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::MCELFStreamer::emitSymbolAttribute
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override
Add the given Attribute to Symbol.
Definition: MCELFStreamer.cpp:193
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::MCELFStreamer::emitAssemblerFlag
void emitAssemblerFlag(MCAssemblerFlag Flag) override
Note in the output the specified Flag.
Definition: MCELFStreamer.cpp:121
llvm::LCOMM::ByteAlignment
@ ByteAlignment
Definition: MCAsmInfo.h:50
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:79
SmallVector.h
llvm::MCELFStreamer::emitELFSize
void emitELFSize(MCSymbol *Symbol, const MCExpr *Value) override
Emit an ELF .size directive.
Definition: MCELFStreamer.cpp:337
llvm::MCELFStreamer::emitBundleUnlock
void emitBundleUnlock() override
Ends a bundle-locked group.
Definition: MCELFStreamer.cpp:667
llvm::MCELFStreamer::AttributeItem::Tag
unsigned Tag
Definition: MCELFStreamer.h:94
llvm::MCELFStreamer::initSections
void initSections(bool NoExecStack, const MCSubtargetInfo &STI) override
Create the default sections and set the initial one.
Definition: MCELFStreamer.cpp:91
llvm::MCELFStreamer::AttributeItem::HiddenAttribute
@ HiddenAttribute
Definition: MCELFStreamer.h:89
From
BlockVerifier::State From
Definition: BlockVerifier.cpp:55
llvm::MCDataFragment
Fragment for data and encoded instructions.
Definition: MCFragment.h:242
llvm::MCELFStreamer::AttributeItem::StringValue
std::string StringValue
Definition: MCELFStreamer.h:96
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:75
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::MCELFStreamer::emitAttributesSection
void emitAttributesSection(StringRef Vendor, const Twine &Section, unsigned Type, MCSection *&AttributeSection)
Definition: MCELFStreamer.h:107