1 //===- lib/MC/MCELFStreamer.cpp - ELF Object Output ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file assembles .s files and emits ELF .o object files.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCCodeEmitter.h"
19 #include "llvm/MC/MCELFSymbolFlags.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCObjectStreamer.h"
23 #include "llvm/MC/MCSection.h"
24 #include "llvm/MC/MCSectionELF.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ELF.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/Target/TargetAsmBackend.h"
36 class MCELFStreamer : public MCObjectStreamer {
37 void EmitInstToFragment(const MCInst &Inst);
38 void EmitInstToData(const MCInst &Inst);
40 MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
41 raw_ostream &OS, MCCodeEmitter *Emitter)
42 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
46 /// @name MCStreamer Interface
49 virtual void InitSections();
50 virtual void EmitLabel(MCSymbol *Symbol);
51 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
52 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
53 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
54 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
55 assert(0 && "ELF doesn't support this directive");
57 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
58 unsigned ByteAlignment);
59 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
60 assert(0 && "ELF doesn't support this directive");
63 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
64 assert(0 && "ELF doesn't support this directive");
67 virtual void EmitCOFFSymbolType(int Type) {
68 assert(0 && "ELF doesn't support this directive");
71 virtual void EndCOFFSymbolDef() {
72 assert(0 && "ELF doesn't support this directive");
75 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
76 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
80 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
81 assert(0 && "ELF doesn't support this directive");
83 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
84 unsigned Size = 0, unsigned ByteAlignment = 0) {
85 assert(0 && "ELF doesn't support this directive");
87 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
88 uint64_t Size, unsigned ByteAlignment = 0) {
89 assert(0 && "ELF doesn't support this directive");
91 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
92 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
93 virtual void EmitGPRel32Value(const MCExpr *Value) {
94 assert(0 && "ELF doesn't support this directive");
96 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
97 unsigned ValueSize = 1,
98 unsigned MaxBytesToEmit = 0);
99 virtual void EmitCodeAlignment(unsigned ByteAlignment,
100 unsigned MaxBytesToEmit = 0);
101 virtual void EmitValueToOffset(const MCExpr *Offset,
102 unsigned char Value = 0);
104 virtual void EmitFileDirective(StringRef Filename);
105 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
106 DEBUG(dbgs() << "FIXME: MCELFStreamer:EmitDwarfFileDirective not implemented\n");
109 virtual void EmitInstruction(const MCInst &Inst);
110 virtual void Finish();
113 void SetSection(StringRef Section, unsigned Type, unsigned Flags,
115 SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
118 void SetSectionData() {
119 SetSection(".data", MCSectionELF::SHT_PROGBITS,
120 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
121 SectionKind::getDataRel());
122 EmitCodeAlignment(4, 0);
124 void SetSectionText() {
125 SetSection(".text", MCSectionELF::SHT_PROGBITS,
126 MCSectionELF::SHF_EXECINSTR |
127 MCSectionELF::SHF_ALLOC, SectionKind::getText());
128 EmitCodeAlignment(4, 0);
130 void SetSectionBss() {
131 SetSection(".bss", MCSectionELF::SHT_NOBITS,
132 MCSectionELF::SHF_WRITE |
133 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
134 EmitCodeAlignment(4, 0);
138 } // end anonymous namespace.
140 void MCELFStreamer::InitSections() {
141 // This emulates the same behavior of GNU as. This makes it easier
142 // to compare the output as the major sections are in the same order.
149 void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
150 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
152 // FIXME: This is wasteful, we don't necessarily need to create a data
153 // fragment. Instead, we should mark the symbol as pointing into the data
154 // fragment if it exists, otherwise we should just queue the label and set its
155 // fragment pointer when we emit the next fragment.
156 MCDataFragment *F = getOrCreateDataFragment();
157 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
158 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
160 SD.setOffset(F->getContents().size());
162 Symbol->setSection(*CurSection);
165 void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
167 case MCAF_SubsectionsViaSymbols:
168 getAssembler().setSubsectionsViaSymbols(true);
172 assert(0 && "invalid assembler flag!");
175 void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
176 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
178 // FIXME: Lift context changes into super class.
179 getAssembler().getOrCreateSymbolData(*Symbol);
180 Symbol->setVariableValue(AddValueSymbols(Value));
183 static void SetBinding(MCSymbolData &SD, unsigned Binding) {
184 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
185 Binding == ELF::STB_WEAK);
186 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
187 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
190 static void SetType(MCSymbolData &SD, unsigned Type) {
191 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
192 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
193 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
194 Type == ELF::STT_TLS);
196 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STT_Shift);
197 SD.setFlags(OtherFlags | (Type << ELF_STT_Shift));
200 static void SetVisibility(MCSymbolData &SD, unsigned Visibility) {
201 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
202 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
204 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STV_Shift);
205 SD.setFlags(OtherFlags | (Visibility << ELF_STV_Shift));
208 void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
209 MCSymbolAttr Attribute) {
210 // Indirect symbols are handled differently, to match how 'as' handles
211 // them. This makes writing matching .o files easier.
212 if (Attribute == MCSA_IndirectSymbol) {
213 // Note that we intentionally cannot use the symbol data here; this is
214 // important for matching the string table that 'as' generates.
215 IndirectSymbolData ISD;
217 ISD.SectionData = getCurrentSectionData();
218 getAssembler().getIndirectSymbols().push_back(ISD);
222 // Adding a symbol attribute always introduces the symbol, note that an
223 // important side effect of calling getOrCreateSymbolData here is to register
224 // the symbol with the assembler.
225 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
227 // The implementation of symbol attributes is designed to match 'as', but it
228 // leaves much to desired. It doesn't really make sense to arbitrarily add and
229 // remove flags, but 'as' allows this (in particular, see .desc).
231 // In the future it might be worth trying to make these operations more well
234 case MCSA_LazyReference:
236 case MCSA_NoDeadStrip:
237 case MCSA_PrivateExtern:
238 case MCSA_WeakDefinition:
239 case MCSA_WeakDefAutoPrivate:
241 case MCSA_ELF_TypeIndFunction:
242 case MCSA_IndirectSymbol:
243 assert(0 && "Invalid symbol attribute for ELF!");
247 SetBinding(SD, ELF::STB_GLOBAL);
248 SD.setExternal(true);
251 case MCSA_WeakReference:
253 SetBinding(SD, ELF::STB_WEAK);
257 SetBinding(SD, ELF::STB_LOCAL);
260 case MCSA_ELF_TypeFunction:
261 SetType(SD, ELF::STT_FUNC);
264 case MCSA_ELF_TypeObject:
265 SetType(SD, ELF::STT_OBJECT);
268 case MCSA_ELF_TypeTLS:
269 SetType(SD, ELF::STT_TLS);
272 case MCSA_ELF_TypeCommon:
273 SetType(SD, ELF::STT_COMMON);
276 case MCSA_ELF_TypeNoType:
277 SetType(SD, ELF::STT_NOTYPE);
281 SetVisibility(SD, ELF::STV_PROTECTED);
285 SetVisibility(SD, ELF::STV_HIDDEN);
289 SetVisibility(SD, ELF::STV_INTERNAL);
294 void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
295 unsigned ByteAlignment) {
296 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
298 if ((SD.getFlags() & (0xf << ELF_STB_Shift)) == ELF_STB_Local) {
299 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
300 MCSectionELF::SHT_NOBITS,
301 MCSectionELF::SHF_WRITE |
302 MCSectionELF::SHF_ALLOC,
303 SectionKind::getBSS());
305 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
306 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
308 Symbol->setSection(*Section);
309 SD.setSize(MCConstantExpr::Create(Size, getContext()));
312 SetBinding(SD, ELF::STB_GLOBAL);
313 SD.setExternal(true);
315 SD.setCommon(Size, ByteAlignment);
318 void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
319 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
321 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
324 void MCELFStreamer::EmitValue(const MCExpr *Value, unsigned Size,
325 unsigned AddrSpace) {
326 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
328 MCDataFragment *DF = getOrCreateDataFragment();
330 // Avoid fixups when possible.
332 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
333 // FIXME: Endianness assumption.
334 for (unsigned i = 0; i != Size; ++i)
335 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
337 DF->addFixup(MCFixup::Create(DF->getContents().size(), AddValueSymbols(Value),
338 MCFixup::getKindForSize(Size)));
339 DF->getContents().resize(DF->getContents().size() + Size, 0);
343 void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
344 int64_t Value, unsigned ValueSize,
345 unsigned MaxBytesToEmit) {
346 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
348 if (MaxBytesToEmit == 0)
349 MaxBytesToEmit = ByteAlignment;
350 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
351 getCurrentSectionData());
353 // Update the maximum alignment on the current section if necessary.
354 if (ByteAlignment > getCurrentSectionData()->getAlignment())
355 getCurrentSectionData()->setAlignment(ByteAlignment);
358 void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
359 unsigned MaxBytesToEmit) {
360 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
362 if (MaxBytesToEmit == 0)
363 MaxBytesToEmit = ByteAlignment;
364 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
365 getCurrentSectionData());
366 F->setEmitNops(true);
368 // Update the maximum alignment on the current section if necessary.
369 if (ByteAlignment > getCurrentSectionData()->getAlignment())
370 getCurrentSectionData()->setAlignment(ByteAlignment);
373 void MCELFStreamer::EmitValueToOffset(const MCExpr *Offset,
374 unsigned char Value) {
375 // TODO: This is exactly the same as MCMachOStreamer. Consider merging into
377 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
380 // Add a symbol for the file name of this module. This is the second
381 // entry in the module's symbol table (the first being the null symbol).
382 void MCELFStreamer::EmitFileDirective(StringRef Filename) {
383 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
384 Symbol->setSection(*CurSection);
385 Symbol->setAbsolute();
387 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
389 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
392 void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
393 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
395 // Add the fixups and data.
397 // FIXME: Revisit this design decision when relaxation is done, we may be
398 // able to get away with not storing any extra data in the MCInst.
399 SmallVector<MCFixup, 4> Fixups;
400 SmallString<256> Code;
401 raw_svector_ostream VecOS(Code);
402 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
405 IF->getCode() = Code;
406 IF->getFixups() = Fixups;
409 void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
410 MCDataFragment *DF = getOrCreateDataFragment();
412 SmallVector<MCFixup, 4> Fixups;
413 SmallString<256> Code;
414 raw_svector_ostream VecOS(Code);
415 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
418 // Add the fixups and data.
419 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
420 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
421 DF->addFixup(Fixups[i]);
423 DF->getContents().append(Code.begin(), Code.end());
426 void MCELFStreamer::EmitInstruction(const MCInst &Inst) {
428 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
429 if (Inst.getOperand(i).isExpr())
430 AddValueSymbols(Inst.getOperand(i).getExpr());
432 getCurrentSectionData()->setHasInstructions(true);
434 // If this instruction doesn't need relaxation, just emit it as data.
435 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
436 EmitInstToData(Inst);
440 // Otherwise, if we are relaxing everything, relax the instruction as much as
441 // possible and emit it as data.
442 if (getAssembler().getRelaxAll()) {
444 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
445 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
446 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
447 EmitInstToData(Relaxed);
451 // Otherwise emit to a separate fragment.
452 EmitInstToFragment(Inst);
455 void MCELFStreamer::Finish() {
456 getAssembler().Finish();
459 MCStreamer *llvm::createELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
460 raw_ostream &OS, MCCodeEmitter *CE,
462 MCELFStreamer *S = new MCELFStreamer(Context, TAB, OS, CE);
464 S->getAssembler().setRelaxAll(true);