blob: 583052d0638a5861ecc2bfd4e2868df75be3dcf7 [file] [log] [blame]
Matt Fleming3565a062010-08-16 18:57:57 +00001//===- lib/MC/MCELFStreamer.cpp - ELF Object Output ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file assembles .s files and emits ELF .o object files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/MC/MCStreamer.h"
15
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"
31
32using namespace llvm;
33
34namespace {
35
36class MCELFStreamer : public MCObjectStreamer {
Benjamin Kramer93ded732010-08-27 10:40:51 +000037 void EmitInstToFragment(const MCInst &Inst);
38 void EmitInstToData(const MCInst &Inst);
Matt Fleming3565a062010-08-16 18:57:57 +000039public:
40 MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
41 raw_ostream &OS, MCCodeEmitter *Emitter)
42 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
43
44 ~MCELFStreamer() {}
45
46 /// @name MCStreamer Interface
47 /// @{
48
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");
55 }
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");
60 }
61
62 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
63 assert(0 && "ELF doesn't support this directive");
64 }
65
66 virtual void EmitCOFFSymbolType(int Type) {
67 assert(0 && "ELF doesn't support this directive");
68 }
69
70 virtual void EndCOFFSymbolDef() {
71 assert(0 && "ELF doesn't support this directive");
72 }
73
74 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
75 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
76 SD.setSize(Value);
77 }
78
79 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
80 assert(0 && "ELF doesn't support this directive");
81 }
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");
85 }
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");
89 }
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");
94 }
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);
102
103 virtual void EmitFileDirective(StringRef Filename);
104 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
105 DEBUG(dbgs() << "FIXME: MCELFStreamer:EmitDwarfFileDirective not implemented\n");
106 }
107
108 virtual void EmitInstruction(const MCInst &Inst);
109 virtual void Finish();
110
111 /// @}
112};
113
114} // end anonymous namespace.
115
116void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
117 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
118
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!");
126 SD.setFragment(F);
127 SD.setOffset(F->getContents().size());
128
129 Symbol->setSection(*CurSection);
130}
131
132void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
133 switch (Flag) {
134 case MCAF_SubsectionsViaSymbols:
135 getAssembler().setSubsectionsViaSymbols(true);
136 return;
137 }
138
139 assert(0 && "invalid assembler flag!");
140}
141
142void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
143 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
144 // MCObjectStreamer.
145 // FIXME: Lift context changes into super class.
146 getAssembler().getOrCreateSymbolData(*Symbol);
147 Symbol->setVariableValue(AddValueSymbols(Value));
148}
149
150void 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;
158 ISD.Symbol = Symbol;
159 ISD.SectionData = getCurrentSectionData();
160 getAssembler().getIndirectSymbols().push_back(ISD);
161 return;
162 }
163
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);
168
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).
172 //
173 // In the future it might be worth trying to make these operations more well
174 // defined.
175 switch (Attribute) {
176 case MCSA_LazyReference:
177 case MCSA_Reference:
178 case MCSA_NoDeadStrip:
179 case MCSA_PrivateExtern:
Matt Fleming3565a062010-08-16 18:57:57 +0000180 case MCSA_WeakDefinition:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000181 case MCSA_WeakDefAutoPrivate:
Matt Fleming3565a062010-08-16 18:57:57 +0000182 case MCSA_Invalid:
183 case MCSA_ELF_TypeIndFunction:
184 case MCSA_IndirectSymbol:
185 assert(0 && "Invalid symbol attribute for ELF!");
186 break;
187
188 case MCSA_Global:
189 SD.setFlags(SD.getFlags() | ELF_STB_Global);
190 SD.setExternal(true);
191 break;
192
Benjamin Kramer230c2742010-09-02 17:18:32 +0000193 case MCSA_WeakReference:
Matt Fleming3565a062010-08-16 18:57:57 +0000194 case MCSA_Weak:
195 SD.setFlags(SD.getFlags() | ELF_STB_Weak);
196 break;
197
198 case MCSA_Local:
Roman Divackyb629dcc2010-09-08 18:08:40 +0000199 // 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));
Matt Fleming3565a062010-08-16 18:57:57 +0000202 break;
203
204 case MCSA_ELF_TypeFunction:
205 SD.setFlags(SD.getFlags() | ELF_STT_Func);
206 break;
207
208 case MCSA_ELF_TypeObject:
209 SD.setFlags(SD.getFlags() | ELF_STT_Object);
210 break;
211
212 case MCSA_ELF_TypeTLS:
213 SD.setFlags(SD.getFlags() | ELF_STT_Tls);
214 break;
215
216 case MCSA_ELF_TypeCommon:
217 SD.setFlags(SD.getFlags() | ELF_STT_Common);
218 break;
219
220 case MCSA_ELF_TypeNoType:
221 SD.setFlags(SD.getFlags() | ELF_STT_Notype);
222 break;
223
224 case MCSA_Protected:
225 SD.setFlags(SD.getFlags() | ELF_STV_Protected);
226 break;
227
228 case MCSA_Hidden:
229 SD.setFlags(SD.getFlags() | ELF_STV_Hidden);
230 break;
231
232 case MCSA_Internal:
233 SD.setFlags(SD.getFlags() | ELF_STV_Internal);
234 break;
235 }
236}
237
238void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
239 unsigned ByteAlignment) {
240 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
241
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());
248
249 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
250 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
251 SD.setFragment(F);
252 Symbol->setSection(*Section);
253 SD.setSize(MCConstantExpr::Create(Size, getContext()));
Matt Fleming3565a062010-08-16 18:57:57 +0000254 }
255
Benjamin Kramer82a31712010-08-30 11:56:55 +0000256 SD.setFlags(SD.getFlags() | ELF_STB_Global);
257 SD.setExternal(true);
258
Matt Fleming3565a062010-08-16 18:57:57 +0000259 SD.setCommon(Size, ByteAlignment);
260}
261
262void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
263 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
264 // MCObjectStreamer.
265 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
266}
267
268void MCELFStreamer::EmitValue(const MCExpr *Value, unsigned Size,
269 unsigned AddrSpace) {
270 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
271 // MCObjectStreamer.
272 MCDataFragment *DF = getOrCreateDataFragment();
273
274 // Avoid fixups when possible.
275 int64_t AbsValue;
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)));
280 } else {
281 DF->addFixup(MCFixup::Create(DF->getContents().size(), AddValueSymbols(Value),
282 MCFixup::getKindForSize(Size)));
283 DF->getContents().resize(DF->getContents().size() + Size, 0);
284 }
285}
286
287void 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
291 // MCObjectStreamer.
292 if (MaxBytesToEmit == 0)
293 MaxBytesToEmit = ByteAlignment;
294 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
295 getCurrentSectionData());
296
297 // Update the maximum alignment on the current section if necessary.
298 if (ByteAlignment > getCurrentSectionData()->getAlignment())
299 getCurrentSectionData()->setAlignment(ByteAlignment);
300}
301
302void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
303 unsigned MaxBytesToEmit) {
304 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
305 // MCObjectStreamer.
306 if (MaxBytesToEmit == 0)
307 MaxBytesToEmit = ByteAlignment;
308 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
309 getCurrentSectionData());
310 F->setEmitNops(true);
311
312 // Update the maximum alignment on the current section if necessary.
313 if (ByteAlignment > getCurrentSectionData()->getAlignment())
314 getCurrentSectionData()->setAlignment(ByteAlignment);
315}
316
317void MCELFStreamer::EmitValueToOffset(const MCExpr *Offset,
318 unsigned char Value) {
319 // TODO: This is exactly the same as MCMachOStreamer. Consider merging into
320 // MCObjectStreamer.
321 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
322}
323
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).
326void MCELFStreamer::EmitFileDirective(StringRef Filename) {
327 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
328 Symbol->setSection(*CurSection);
329 Symbol->setAbsolute();
330
331 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
332
333 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
334}
335
Benjamin Kramer93ded732010-08-27 10:40:51 +0000336void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
337 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
338
339 // Add the fixups and data.
340 //
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);
347 VecOS.flush();
348
349 IF->getCode() = Code;
350 IF->getFixups() = Fixups;
351}
352
353void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
354 MCDataFragment *DF = getOrCreateDataFragment();
355
356 SmallVector<MCFixup, 4> Fixups;
357 SmallString<256> Code;
358 raw_svector_ostream VecOS(Code);
359 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
360 VecOS.flush();
361
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]);
366 }
367 DF->getContents().append(Code.begin(), Code.end());
368}
369
Matt Fleming3565a062010-08-16 18:57:57 +0000370void MCELFStreamer::EmitInstruction(const MCInst &Inst) {
371 // Scan for values.
372 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
373 if (Inst.getOperand(i).isExpr())
374 AddValueSymbols(Inst.getOperand(i).getExpr());
375
376 getCurrentSectionData()->setHasInstructions(true);
377
Benjamin Kramer93ded732010-08-27 10:40:51 +0000378 // If this instruction doesn't need relaxation, just emit it as data.
379 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
380 EmitInstToData(Inst);
Matt Fleming3565a062010-08-16 18:57:57 +0000381 return;
382 }
383
Benjamin Kramer93ded732010-08-27 10:40:51 +0000384 // Otherwise, if we are relaxing everything, relax the instruction as much as
385 // possible and emit it as data.
386 if (getAssembler().getRelaxAll()) {
387 MCInst Relaxed;
388 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
389 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
390 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
391 EmitInstToData(Relaxed);
392 return;
Matt Fleming3565a062010-08-16 18:57:57 +0000393 }
Benjamin Kramer93ded732010-08-27 10:40:51 +0000394
395 // Otherwise emit to a separate fragment.
396 EmitInstToFragment(Inst);
Matt Fleming3565a062010-08-16 18:57:57 +0000397}
398
399void MCELFStreamer::Finish() {
400 getAssembler().Finish();
401}
402
403MCStreamer *llvm::createELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
404 raw_ostream &OS, MCCodeEmitter *CE,
405 bool RelaxAll) {
406 MCELFStreamer *S = new MCELFStreamer(Context, TAB, OS, CE);
407 if (RelaxAll)
408 S->getAssembler().setRelaxAll(true);
409 return S;
410}