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 EmitLabel(MCSymbol *Symbol);
50 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
51 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
52 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
53 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
54 assert(0 && "ELF doesn't support this directive");
56 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
57 unsigned ByteAlignment);
58 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
59 assert(0 && "ELF doesn't support this directive");
62 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
63 assert(0 && "ELF doesn't support this directive");
66 virtual void EmitCOFFSymbolType(int Type) {
67 assert(0 && "ELF doesn't support this directive");
70 virtual void EndCOFFSymbolDef() {
71 assert(0 && "ELF doesn't support this directive");
74 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
75 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
79 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
80 assert(0 && "ELF doesn't support this directive");
82 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
83 unsigned Size = 0, unsigned ByteAlignment = 0) {
84 assert(0 && "ELF doesn't support this directive");
86 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
87 uint64_t Size, unsigned ByteAlignment = 0) {
88 assert(0 && "ELF doesn't support this directive");
90 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
91 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
92 virtual void EmitGPRel32Value(const MCExpr *Value) {
93 assert(0 && "ELF doesn't support this directive");
95 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
96 unsigned ValueSize = 1,
97 unsigned MaxBytesToEmit = 0);
98 virtual void EmitCodeAlignment(unsigned ByteAlignment,
99 unsigned MaxBytesToEmit = 0);
100 virtual void EmitValueToOffset(const MCExpr *Offset,
101 unsigned char Value = 0);
103 virtual void EmitFileDirective(StringRef Filename);
104 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
105 DEBUG(dbgs() << "FIXME: MCELFStreamer:EmitDwarfFileDirective not implemented\n");
108 virtual void EmitInstruction(const MCInst &Inst);
109 virtual void Finish();
114 } // end anonymous namespace.
116 void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
117 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
119 // FIXME: This is wasteful, we don't necessarily need to create a data
120 // fragment. Instead, we should mark the symbol as pointing into the data
121 // fragment if it exists, otherwise we should just queue the label and set its
122 // fragment pointer when we emit the next fragment.
123 MCDataFragment *F = getOrCreateDataFragment();
124 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
125 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
127 SD.setOffset(F->getContents().size());
129 Symbol->setSection(*CurSection);
132 void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
134 case MCAF_SubsectionsViaSymbols:
135 getAssembler().setSubsectionsViaSymbols(true);
139 assert(0 && "invalid assembler flag!");
142 void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
143 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
145 // FIXME: Lift context changes into super class.
146 getAssembler().getOrCreateSymbolData(*Symbol);
147 Symbol->setVariableValue(AddValueSymbols(Value));
150 void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
151 MCSymbolAttr Attribute) {
152 // Indirect symbols are handled differently, to match how 'as' handles
153 // them. This makes writing matching .o files easier.
154 if (Attribute == MCSA_IndirectSymbol) {
155 // Note that we intentionally cannot use the symbol data here; this is
156 // important for matching the string table that 'as' generates.
157 IndirectSymbolData ISD;
159 ISD.SectionData = getCurrentSectionData();
160 getAssembler().getIndirectSymbols().push_back(ISD);
164 // Adding a symbol attribute always introduces the symbol, note that an
165 // important side effect of calling getOrCreateSymbolData here is to register
166 // the symbol with the assembler.
167 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
169 // The implementation of symbol attributes is designed to match 'as', but it
170 // leaves much to desired. It doesn't really make sense to arbitrarily add and
171 // remove flags, but 'as' allows this (in particular, see .desc).
173 // In the future it might be worth trying to make these operations more well
176 case MCSA_LazyReference:
178 case MCSA_NoDeadStrip:
179 case MCSA_PrivateExtern:
180 case MCSA_WeakDefinition:
181 case MCSA_WeakDefAutoPrivate:
183 case MCSA_ELF_TypeIndFunction:
184 case MCSA_IndirectSymbol:
185 assert(0 && "Invalid symbol attribute for ELF!");
189 SD.setFlags(SD.getFlags() | ELF_STB_Global);
190 SD.setExternal(true);
193 case MCSA_WeakReference:
195 SD.setFlags(SD.getFlags() | ELF_STB_Weak);
199 // ELF_STB_Local is 0, so zero the ELF_STB area
200 // SD.getFlags() | ELF_STB_Local is a NOP
201 SD.setFlags(SD.getFlags() & ~(0xf << ELF_STB_Shift));
204 case MCSA_ELF_TypeFunction:
205 SD.setFlags(SD.getFlags() | ELF_STT_Func);
208 case MCSA_ELF_TypeObject:
209 SD.setFlags(SD.getFlags() | ELF_STT_Object);
212 case MCSA_ELF_TypeTLS:
213 SD.setFlags(SD.getFlags() | ELF_STT_Tls);
216 case MCSA_ELF_TypeCommon:
217 SD.setFlags(SD.getFlags() | ELF_STT_Common);
220 case MCSA_ELF_TypeNoType:
221 SD.setFlags(SD.getFlags() | ELF_STT_Notype);
225 SD.setFlags(SD.getFlags() | ELF_STV_Protected);
229 SD.setFlags(SD.getFlags() | ELF_STV_Hidden);
233 SD.setFlags(SD.getFlags() | ELF_STV_Internal);
238 void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
239 unsigned ByteAlignment) {
240 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
242 if ((SD.getFlags() & (0xf << ELF_STB_Shift)) == ELF_STB_Local) {
243 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
244 MCSectionELF::SHT_NOBITS,
245 MCSectionELF::SHF_WRITE |
246 MCSectionELF::SHF_ALLOC,
247 SectionKind::getBSS());
249 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
250 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
252 Symbol->setSection(*Section);
253 SD.setSize(MCConstantExpr::Create(Size, getContext()));
256 SD.setFlags(SD.getFlags() | ELF_STB_Global);
257 SD.setExternal(true);
259 SD.setCommon(Size, ByteAlignment);
262 void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
263 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
265 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
268 void MCELFStreamer::EmitValue(const MCExpr *Value, unsigned Size,
269 unsigned AddrSpace) {
270 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
272 MCDataFragment *DF = getOrCreateDataFragment();
274 // Avoid fixups when possible.
276 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
277 // FIXME: Endianness assumption.
278 for (unsigned i = 0; i != Size; ++i)
279 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
281 DF->addFixup(MCFixup::Create(DF->getContents().size(), AddValueSymbols(Value),
282 MCFixup::getKindForSize(Size)));
283 DF->getContents().resize(DF->getContents().size() + Size, 0);
287 void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
288 int64_t Value, unsigned ValueSize,
289 unsigned MaxBytesToEmit) {
290 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
292 if (MaxBytesToEmit == 0)
293 MaxBytesToEmit = ByteAlignment;
294 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
295 getCurrentSectionData());
297 // Update the maximum alignment on the current section if necessary.
298 if (ByteAlignment > getCurrentSectionData()->getAlignment())
299 getCurrentSectionData()->setAlignment(ByteAlignment);
302 void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
303 unsigned MaxBytesToEmit) {
304 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
306 if (MaxBytesToEmit == 0)
307 MaxBytesToEmit = ByteAlignment;
308 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
309 getCurrentSectionData());
310 F->setEmitNops(true);
312 // Update the maximum alignment on the current section if necessary.
313 if (ByteAlignment > getCurrentSectionData()->getAlignment())
314 getCurrentSectionData()->setAlignment(ByteAlignment);
317 void MCELFStreamer::EmitValueToOffset(const MCExpr *Offset,
318 unsigned char Value) {
319 // TODO: This is exactly the same as MCMachOStreamer. Consider merging into
321 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
324 // Add a symbol for the file name of this module. This is the second
325 // entry in the module's symbol table (the first being the null symbol).
326 void MCELFStreamer::EmitFileDirective(StringRef Filename) {
327 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
328 Symbol->setSection(*CurSection);
329 Symbol->setAbsolute();
331 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
333 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
336 void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
337 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
339 // Add the fixups and data.
341 // FIXME: Revisit this design decision when relaxation is done, we may be
342 // able to get away with not storing any extra data in the MCInst.
343 SmallVector<MCFixup, 4> Fixups;
344 SmallString<256> Code;
345 raw_svector_ostream VecOS(Code);
346 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
349 IF->getCode() = Code;
350 IF->getFixups() = Fixups;
353 void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
354 MCDataFragment *DF = getOrCreateDataFragment();
356 SmallVector<MCFixup, 4> Fixups;
357 SmallString<256> Code;
358 raw_svector_ostream VecOS(Code);
359 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
362 // Add the fixups and data.
363 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
364 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
365 DF->addFixup(Fixups[i]);
367 DF->getContents().append(Code.begin(), Code.end());
370 void MCELFStreamer::EmitInstruction(const MCInst &Inst) {
372 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
373 if (Inst.getOperand(i).isExpr())
374 AddValueSymbols(Inst.getOperand(i).getExpr());
376 getCurrentSectionData()->setHasInstructions(true);
378 // If this instruction doesn't need relaxation, just emit it as data.
379 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
380 EmitInstToData(Inst);
384 // Otherwise, if we are relaxing everything, relax the instruction as much as
385 // possible and emit it as data.
386 if (getAssembler().getRelaxAll()) {
388 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
389 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
390 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
391 EmitInstToData(Relaxed);
395 // Otherwise emit to a separate fragment.
396 EmitInstToFragment(Inst);
399 void MCELFStreamer::Finish() {
400 getAssembler().Finish();
403 MCStreamer *llvm::createELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
404 raw_ostream &OS, MCCodeEmitter *CE,
406 MCELFStreamer *S = new MCELFStreamer(Context, TAB, OS, CE);
408 S->getAssembler().setRelaxAll(true);