LLVM: lib/MC/MCELFStreamer.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
34#include
35#include
36
37using namespace llvm;
38
40 std::unique_ptr TAB,
41 std::unique_ptr OW,
42 std::unique_ptr Emitter)
45
49
52 switchSection(Ctx.getObjectFileInfo()->getTextSection());
54 &STI);
55
56 if (NoExecStack)
57 switchSection(Ctx.getAsmInfo()->getStackSection(Ctx, false));
58}
59
61 auto *Symbol = static_cast<MCSymbolELF *>(S);
63
68}
69
72 auto *Symbol = static_cast<MCSymbolELF *>(S);
74
79}
80
83 auto *SectionELF = static_cast<const MCSectionELF *>(Section);
84 const MCSymbol *Grp = SectionELF->getGroup();
85 if (Grp)
86 Asm.registerSymbol(*Grp);
89
91 auto *Sym = static_cast<MCSymbolELF *>(Section->getBeginSymbol());
94}
95
98 if (A->isDefined()) {
100 "' is already defined");
101 return;
102 }
104 A->setIsWeakref();
106}
107
108
109
110
111
112
113
114
119 return T2;
120 if (T2 == Type)
121 return T1;
122 }
123
124 return T2;
125}
126
128 auto *Symbol = static_cast<MCSymbolELF *>(S);
129
130
131
132
134
135
136
137
138
139
140
154 return false;
155
157
158 break;
159
164 break;
165
167
168
169
170 if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_GLOBAL)
172 Symbol->getName() +
173 " changed binding to STB_GLOBAL");
175 break;
176
179
180
181 if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_WEAK)
183 getStartTokLoc(), Symbol->getName() + " changed binding to STB_WEAK");
185 break;
186
188 if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_LOCAL)
190 Symbol->getName() +
191 " changed binding to STB_LOCAL");
193 break;
194
197 break;
198
202 break;
203
206 break;
207
210 break;
211
213
215 break;
216
219 break;
220
223 break;
224
226 Symbol->setMemtag(true);
227 break;
228
231 break;
232
235 break;
236
238 llvm_unreachable("ELF doesn't support the .alt_entry attribute");
239
241 llvm_unreachable("ELF doesn't support the .lglobl attribute");
242 }
243
244 return true;
245}
246
248 Align ByteAlignment) {
249 auto *Symbol = static_cast<MCSymbolELF *>(S);
251
252 if (!Symbol->isBindingSet())
254
256
262
266
268 } else {
269 if (Symbol->declareCommon(Size, ByteAlignment))
271 " redeclared as different type");
272 }
273
275}
276
280
283 bool KeepOriginalSym) {
285 getStartTokLoc(), OriginalSym, Name, KeepOriginalSym});
286}
287
289 Align ByteAlignment) {
290 auto *Symbol = static_cast<MCSymbolELF *>(S);
291
295}
296
302
308 if (!SeenIdent) {
310 SeenIdent = true;
311 }
315}
316
317void MCELFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *Sym,
324 SRE->getLoc(), Twine("Reference to undefined temporary symbol ") +
325 "`" + S->getName() + "`");
326 return;
327 }
331 }
335}
336
337void MCELFStreamer::finalizeCGProfile() {
339 if (W.getCGProfile().empty())
340 return;
347 auto *Sym =
349 for (auto &E : W.getCGProfile()) {
350 finalizeCGProfileEntry(Sym, Offset, E.From);
351 finalizeCGProfileEntry(Sym, Offset, E.To);
353 Offset += sizeof(uint64_t);
354 }
356}
357
359
360 if (!GNUAttributes.empty()) {
361 MCSection *DummyAttributeSection = nullptr;
363 DummyAttributeSection, GNUAttributes);
364 }
365
366 finalizeCGProfile();
368
370}
371
373 bool OverwriteExisting) {
374
376 if (!OverwriteExisting)
377 return;
379 Item->IntValue = Value;
380 return;
381 }
382
383
387}
388
390 bool OverwriteExisting) {
391
393 if (!OverwriteExisting)
394 return;
396 Item->StringValue = std::string(Value);
397 return;
398 }
399
400
402 std::string(Value)};
404}
405
408 bool OverwriteExisting) {
409
411 if (!OverwriteExisting)
412 return;
414 Item->IntValue = IntValue;
415 Item->StringValue = std::string(StringValue);
416 return;
417 }
418
419
421 IntValue, std::string(StringValue)};
423}
424
426MCELFStreamer::getAttributeItem(unsigned Attribute) {
427 for (AttributeItem &Item : Contents)
429 return &Item;
430 return nullptr;
431}
432
433size_t MCELFStreamer::calculateContentSize(
435 size_t Result = 0;
436 for (const AttributeItem &Item : AttrsVec) {
437 switch (Item.Type) {
439 break;
443 break;
446 Result += Item.StringValue.size() + 1;
447 break;
451 Result += Item.StringValue.size() + 1;
452 break;
453 }
454 }
456}
457
458void MCELFStreamer::createAttributesSection(
461
462
463
464
465
466
467
468
469
470 if (AttributeSection) {
472 } else {
475
476
478 }
479
480
481 const size_t VendorHeaderSize = 4 + Vendor.size() + 1;
482
483
484 const size_t TagHeaderSize = 1 + 4;
485
486 const size_t ContentsSize = calculateContentSize(AttrsVec);
487
488 emitInt32(VendorHeaderSize + TagHeaderSize + ContentsSize);
491
493 emitInt32(TagHeaderSize + ContentsSize);
494
495
496
499 switch (Item.Type) {
500 default:
504 break;
508 break;
513 break;
514 }
515 }
516
517 AttrsVec.clear();
518}
519
520void MCELFStreamer::createAttributesWithSubsection(
523
524
525
526
527
528
529 if (0 == SubSectionVec.size()) {
530 return;
531 }
532
533
534 if (AttributeSection) {
536 } else {
539
540
542 }
543
545
546 const size_t VendorHeaderSize = 4 + SubSection.VendorName.size() + 1;
547
548 const size_t VendorParameters = 1 + 1;
549 const size_t ContentsSize = calculateContentSize(SubSection.Content);
550
551 emitInt32(VendorHeaderSize + VendorParameters + ContentsSize);
552 emitBytes(SubSection.VendorName);
554 emitInt8(SubSection.IsOptional);
555 emitInt8(SubSection.ParameterType);
556
559 switch (Item.Type) {
560 default:
561 assert(0 && "Invalid attribute type");
562 break;
565 break;
569 break;
574 break;
575 }
576 }
577 }
578 SubSectionVec.clear();
579}
580
582 std::unique_ptr &&MAB,
583 std::unique_ptr &&OW,
584 std::unique_ptr &&CE) {
586 new MCELFStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
587 return S;
588}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
dxil DXContainer Global Emitter
static unsigned CombineSymbolTypes(unsigned T1, unsigned T2)
Definition MCELFStreamer.cpp:115
This file defines the SmallVector class.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
SmallVector< const MCSymbolELF *, 0 > Weakrefs
SmallVector< Symver, 0 > Symvers
MCContext & getContext() const
MCObjectWriter & getWriter() const
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol)
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Context object for machine code objects.
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
LLVM_ABI void reportWarning(SMLoc L, const Twine &Msg)
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
SmallVector< AttributeItem, 64 > Contents
void changeSection(MCSection *Section, uint32_t Subsection=0) override
This is called by popSection and switchSection, if the current section changes.
Definition MCELFStreamer.cpp:81
void emitIdent(StringRef IdentString) override
Emit the "identifiers" directive.
Definition MCELFStreamer.cpp:303
void setAttributeItems(unsigned Attribute, unsigned IntValue, StringRef StringValue, bool OverwriteExisting)
Definition MCELFStreamer.cpp:406
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, Align ByteAlignment) override
Emit a common symbol.
Definition MCELFStreamer.cpp:247
void emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, Align ByteAlignment) override
Emit a local common (.lcomm) symbol.
Definition MCELFStreamer.cpp:288
void emitELFSize(MCSymbol *Symbol, const MCExpr *Value) override
Emit an ELF .size directive.
Definition MCELFStreamer.cpp:277
void emitWeakReference(MCSymbol *Alias, const MCSymbol *Target) override
Emit an weak reference from Alias to Symbol.
Definition MCELFStreamer.cpp:96
void emitELFSymverDirective(const MCSymbol *OriginalSym, StringRef Name, bool KeepOriginalSym) override
Emit an ELF .symver directive.
Definition MCELFStreamer.cpp:281
void emitCGProfileEntry(const MCSymbolRefExpr *From, const MCSymbolRefExpr *To, uint64_t Count) override
Definition MCELFStreamer.cpp:297
void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F, uint64_t Offset) override
Definition MCELFStreamer.cpp:70
ELFObjectWriter & getWriter()
Definition MCELFStreamer.cpp:46
void setAttributeItem(unsigned Attribute, unsigned Value, bool OverwriteExisting)
Definition MCELFStreamer.cpp:372
void finishImpl() final
Streamer specific finalization.
Definition MCELFStreamer.cpp:358
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
Definition MCELFStreamer.cpp:60
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override
Add the given Attribute to Symbol.
Definition MCELFStreamer.cpp:127
void initSections(bool NoExecStack, const MCSubtargetInfo &STI) override
Create the default sections and set the initial one.
Definition MCELFStreamer.cpp:50
MCELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
Definition MCELFStreamer.cpp:39
Base class for the full range of assembler expressions which are needed for parsing.
void emitValueToAlignment(Align Alignment, int64_t Fill=0, uint8_t FillLen=1, unsigned MaxBytesToEmit=0) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
MCAssembler & getAssembler()
void emitRelocDirective(const MCExpr &Offset, StringRef Name, const MCExpr *Expr, SMLoc Loc={}) override
Record a relocation described by the .reloc directive.
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F, uint64_t Offset)
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
MCObjectStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
void finishImpl() override
Streamer specific finalization.
void changeSection(MCSection *Section, uint32_t Subsection=0) override
This is called by popSection and switchSection, if the current section changes.
void emitCodeAlignment(Align ByteAlignment, const MCSubtargetInfo *STI, unsigned MaxBytesToEmit=0) override
Emit nops until the byte alignment ByteAlignment is reached.
SmallVector< CGProfileEntry, 0 > & getCGProfile()
This represents a section on linux, lots of unix variants and some bare metal systems.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
MCSymbol * getBeginSymbol()
Streaming machine code generation interface.
virtual bool popSection()
Restore the current and previous section from the section stack.
MCContext & getContext() const
SMLoc getStartTokLoc() const
virtual void emitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers.
void pushSection()
Save the current and previous section on the section stack.
unsigned emitULEB128IntValue(uint64_t Value, unsigned PadTo=0)
Special case of EmitULEB128Value that avoids the client having to pass in a MCExpr for constant integ...
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
void emitInt32(uint64_t Value)
MCSectionSubPair getCurrentSection() const
Return the current section that the streamer is emitting code to.
MCSection * getCurrentSectionOnly() const
void emitZeros(uint64_t NumBytes)
Emit NumBytes worth of zeros.
void emitInt8(uint64_t Value)
Generic base class for all target subtargets.
void setBinding(unsigned Binding) const
Represent a reference to a symbol from inside an expression.
const MCSymbol & getSymbol() const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute).
StringRef getName() const
getName - Get the symbol name.
void setUsedInReloc() const
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
Represents a location in source code.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
constexpr size_t size() const
size - Get the string size.
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ SHT_LLVM_CALL_GRAPH_PROFILE
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI MCStreamer * createELFStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE)
Definition MCELFStreamer.cpp:581
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
FunctionAddr VTableAddr Count
LLVM_ABI unsigned getULEB128Size(uint64_t Value)
Utility function to get the size of the ULEB128-encoded value.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
std::pair< MCSection *, uint32_t > MCSectionSubPair
@ MCSA_WeakDefAutoPrivate
.weak_def_can_be_hidden (MachO)
@ MCSA_Memtag
.memtag (ELF)
@ MCSA_Protected
.protected (ELF)
@ MCSA_Exported
.globl _foo, exported (XCOFF)
@ MCSA_PrivateExtern
.private_extern (MachO)
@ MCSA_Internal
.internal (ELF)
@ MCSA_WeakReference
.weak_reference (MachO)
@ MCSA_AltEntry
.alt_entry (MachO)
@ MCSA_ELF_TypeIndFunction
.type _foo, STT_GNU_IFUNC
@ MCSA_LazyReference
.lazy_reference (MachO)
@ MCSA_ELF_TypeNoType
.type _foo, STT_NOTYPE # aka @notype
@ MCSA_Reference
.reference (MachO)
@ MCSA_SymbolResolver
.symbol_resolver (MachO)
@ MCSA_ELF_TypeTLS
.type _foo, STT_TLS # aka @tls_object
@ MCSA_IndirectSymbol
.indirect_symbol (MachO)
@ MCSA_WeakDefinition
.weak_definition (MachO)
@ MCSA_ELF_TypeCommon
.type _foo, STT_COMMON # aka @common
@ MCSA_Global
.type _foo, @gnu_unique_object
@ MCSA_WeakAntiDep
.weak_anti_dep (COFF)
@ MCSA_Extern
.extern (XCOFF)
@ MCSA_ELF_TypeObject
.type _foo, STT_OBJECT # aka @object
@ MCSA_ELF_TypeGnuUniqueObject
@ MCSA_ELF_TypeFunction
.type _foo, STT_FUNC # aka @function
@ MCSA_Hidden
.hidden (ELF)
@ MCSA_LGlobal
.lglobl (XCOFF)
@ MCSA_Invalid
Not a valid directive.
@ MCSA_NoDeadStrip
.no_dead_strip (MachO)
Implement std::hash so that hash_code can be used in STL containers.
This struct is a compact representation of a valid (non-zero power of two) alignment.
ELF object attributes section emission support.
@ NumericAndTextAttributes
ELF object attributes subsection support.