blob: 23698212044615dd831a3dc7dd307687f71221d1 [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
Rafael Espindolaf7c10a32010-09-21 00:24:38 +000016#include "llvm/ADT/SmallPtrSet.h"
Matt Fleming3565a062010-08-16 18:57:57 +000017#include "llvm/MC/MCAssembler.h"
18#include "llvm/MC/MCContext.h"
19#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCELFSymbolFlags.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCObjectStreamer.h"
24#include "llvm/MC/MCSection.h"
25#include "llvm/MC/MCSectionELF.h"
26#include "llvm/MC/MCSymbol.h"
Rafael Espindola484291c2010-11-01 14:28:48 +000027#include "llvm/MC/MCValue.h"
Matt Fleming3565a062010-08-16 18:57:57 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/ELF.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/Target/TargetAsmBackend.h"
Rafael Espindola89b93722010-12-10 07:39:47 +000033#include "llvm/Target/TargetAsmInfo.h"
Matt Fleming3565a062010-08-16 18:57:57 +000034
35using namespace llvm;
36
37namespace {
38
Rafael Espindolae1a25872010-11-11 19:04:55 +000039static void SetBinding(MCSymbolData &SD, unsigned Binding) {
40 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
41 Binding == ELF::STB_WEAK);
42 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
43 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
44}
45
46static unsigned GetBinding(const MCSymbolData &SD) {
47 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
48 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
49 Binding == ELF::STB_WEAK);
50 return Binding;
51}
52
53static void SetType(MCSymbolData &SD, unsigned Type) {
54 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
55 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
56 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
57 Type == ELF::STT_TLS);
58
59 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STT_Shift);
60 SD.setFlags(OtherFlags | (Type << ELF_STT_Shift));
61}
62
63static void SetVisibility(MCSymbolData &SD, unsigned Visibility) {
64 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
65 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
66
67 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STV_Shift);
68 SD.setFlags(OtherFlags | (Visibility << ELF_STV_Shift));
69}
70
Matt Fleming3565a062010-08-16 18:57:57 +000071class MCELFStreamer : public MCObjectStreamer {
72public:
73 MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
74 raw_ostream &OS, MCCodeEmitter *Emitter)
Rafael Espindola85f2ecc2010-12-07 00:27:36 +000075 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
Matt Fleming3565a062010-08-16 18:57:57 +000076
77 ~MCELFStreamer() {}
78
79 /// @name MCStreamer Interface
80 /// @{
81
Rafael Espindolad80781b2010-09-15 21:48:40 +000082 virtual void InitSections();
Matt Fleming3565a062010-08-16 18:57:57 +000083 virtual void EmitLabel(MCSymbol *Symbol);
84 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +000085 virtual void EmitThumbFunc(MCSymbol *Func);
Matt Fleming3565a062010-08-16 18:57:57 +000086 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Rafael Espindola484291c2010-11-01 14:28:48 +000087 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +000088 virtual void SwitchSection(const MCSection *Section);
Matt Fleming3565a062010-08-16 18:57:57 +000089 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
90 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
91 assert(0 && "ELF doesn't support this directive");
92 }
93 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
94 unsigned ByteAlignment);
95 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
96 assert(0 && "ELF doesn't support this directive");
97 }
98
99 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
100 assert(0 && "ELF doesn't support this directive");
101 }
102
103 virtual void EmitCOFFSymbolType(int Type) {
104 assert(0 && "ELF doesn't support this directive");
105 }
106
107 virtual void EndCOFFSymbolDef() {
108 assert(0 && "ELF doesn't support this directive");
109 }
110
111 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
112 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
113 SD.setSize(Value);
114 }
115
Jason W Kimf13743b2010-12-16 03:12:17 +0000116 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
117
Matt Fleming3565a062010-08-16 18:57:57 +0000118 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
119 unsigned Size = 0, unsigned ByteAlignment = 0) {
120 assert(0 && "ELF doesn't support this directive");
121 }
122 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
123 uint64_t Size, unsigned ByteAlignment = 0) {
124 assert(0 && "ELF doesn't support this directive");
125 }
126 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Matt Fleming3565a062010-08-16 18:57:57 +0000127 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
128 unsigned ValueSize = 1,
129 unsigned MaxBytesToEmit = 0);
130 virtual void EmitCodeAlignment(unsigned ByteAlignment,
131 unsigned MaxBytesToEmit = 0);
Matt Fleming3565a062010-08-16 18:57:57 +0000132
133 virtual void EmitFileDirective(StringRef Filename);
Matt Fleming3565a062010-08-16 18:57:57 +0000134
Matt Fleming3565a062010-08-16 18:57:57 +0000135 virtual void Finish();
136
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000137private:
Rafael Espindolaf89671d2010-11-01 16:27:31 +0000138 virtual void EmitInstToFragment(const MCInst &Inst);
139 virtual void EmitInstToData(const MCInst &Inst);
140
Rafael Espindola97551272010-11-24 02:19:40 +0000141 void fixSymbolsInTLSFixups(const MCExpr *expr);
142
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000143 struct LocalCommon {
144 MCSymbolData *SD;
145 uint64_t Size;
146 unsigned ByteAlignment;
147 };
148 std::vector<LocalCommon> LocalCommons;
149
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000150 SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
Matt Fleming3565a062010-08-16 18:57:57 +0000151 /// @}
Rafael Espindolad80781b2010-09-15 21:48:40 +0000152 void SetSection(StringRef Section, unsigned Type, unsigned Flags,
153 SectionKind Kind) {
154 SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
155 }
156
157 void SetSectionData() {
158 SetSection(".data", MCSectionELF::SHT_PROGBITS,
159 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
160 SectionKind::getDataRel());
161 EmitCodeAlignment(4, 0);
162 }
163 void SetSectionText() {
164 SetSection(".text", MCSectionELF::SHT_PROGBITS,
165 MCSectionELF::SHF_EXECINSTR |
166 MCSectionELF::SHF_ALLOC, SectionKind::getText());
167 EmitCodeAlignment(4, 0);
168 }
169 void SetSectionBss() {
170 SetSection(".bss", MCSectionELF::SHT_NOBITS,
171 MCSectionELF::SHF_WRITE |
172 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
173 EmitCodeAlignment(4, 0);
174 }
Matt Fleming3565a062010-08-16 18:57:57 +0000175};
176
177} // end anonymous namespace.
178
Rafael Espindolad80781b2010-09-15 21:48:40 +0000179void MCELFStreamer::InitSections() {
180 // This emulates the same behavior of GNU as. This makes it easier
181 // to compare the output as the major sections are in the same order.
182 SetSectionText();
183 SetSectionData();
184 SetSectionBss();
185 SetSectionText();
186}
187
Matt Fleming3565a062010-08-16 18:57:57 +0000188void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindolaba210242010-11-28 16:22:59 +0000189 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
190
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000191 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindola73ffea42010-09-25 05:42:19 +0000192
Rafael Espindolae1a25872010-11-11 19:04:55 +0000193 const MCSectionELF &Section =
194 static_cast<const MCSectionELF&>(Symbol->getSection());
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000195 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Rafael Espindolae1a25872010-11-11 19:04:55 +0000196 if (Section.getFlags() & MCSectionELF::SHF_TLS)
197 SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000198}
199
200void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
201 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000202 case MCAF_SyntaxUnified: return; // no-op here.
203 case MCAF_Code16: return; // no-op here.
Jim Grosbachba219572010-11-05 22:40:09 +0000204 case MCAF_Code32: return; // no-op here.
Matt Fleming3565a062010-08-16 18:57:57 +0000205 case MCAF_SubsectionsViaSymbols:
206 getAssembler().setSubsectionsViaSymbols(true);
207 return;
208 }
209
210 assert(0 && "invalid assembler flag!");
211}
212
Jim Grosbachce792992010-11-05 22:08:08 +0000213void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
214 // FIXME: Anything needed here to flag the function as thumb?
215}
216
Matt Fleming3565a062010-08-16 18:57:57 +0000217void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
218 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
219 // MCObjectStreamer.
220 // FIXME: Lift context changes into super class.
221 getAssembler().getOrCreateSymbolData(*Symbol);
222 Symbol->setVariableValue(AddValueSymbols(Value));
223}
224
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000225void MCELFStreamer::SwitchSection(const MCSection *Section) {
226 const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
227 if (Grp)
228 getAssembler().getOrCreateSymbolData(*Grp);
229 this->MCObjectStreamer::SwitchSection(Section);
230}
231
Rafael Espindola484291c2010-11-01 14:28:48 +0000232void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
233 getAssembler().getOrCreateSymbolData(*Symbol);
234 MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
235 AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000236 const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext());
Rafael Espindola484291c2010-11-01 14:28:48 +0000237 Alias->setVariableValue(Value);
238}
239
Matt Fleming3565a062010-08-16 18:57:57 +0000240void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
241 MCSymbolAttr Attribute) {
242 // Indirect symbols are handled differently, to match how 'as' handles
243 // them. This makes writing matching .o files easier.
244 if (Attribute == MCSA_IndirectSymbol) {
245 // Note that we intentionally cannot use the symbol data here; this is
246 // important for matching the string table that 'as' generates.
247 IndirectSymbolData ISD;
248 ISD.Symbol = Symbol;
249 ISD.SectionData = getCurrentSectionData();
250 getAssembler().getIndirectSymbols().push_back(ISD);
251 return;
252 }
253
254 // Adding a symbol attribute always introduces the symbol, note that an
255 // important side effect of calling getOrCreateSymbolData here is to register
256 // the symbol with the assembler.
257 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
258
259 // The implementation of symbol attributes is designed to match 'as', but it
260 // leaves much to desired. It doesn't really make sense to arbitrarily add and
261 // remove flags, but 'as' allows this (in particular, see .desc).
262 //
263 // In the future it might be worth trying to make these operations more well
264 // defined.
265 switch (Attribute) {
266 case MCSA_LazyReference:
267 case MCSA_Reference:
268 case MCSA_NoDeadStrip:
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000269 case MCSA_SymbolResolver:
Matt Fleming3565a062010-08-16 18:57:57 +0000270 case MCSA_PrivateExtern:
Matt Fleming3565a062010-08-16 18:57:57 +0000271 case MCSA_WeakDefinition:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000272 case MCSA_WeakDefAutoPrivate:
Matt Fleming3565a062010-08-16 18:57:57 +0000273 case MCSA_Invalid:
274 case MCSA_ELF_TypeIndFunction:
275 case MCSA_IndirectSymbol:
276 assert(0 && "Invalid symbol attribute for ELF!");
277 break;
278
Rafael Espindola7e528a12010-11-14 01:34:31 +0000279 case MCSA_ELF_TypeGnuUniqueObject:
280 // Ignore for now.
281 break;
282
Matt Fleming3565a062010-08-16 18:57:57 +0000283 case MCSA_Global:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000284 SetBinding(SD, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000285 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000286 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000287 break;
288
Benjamin Kramer230c2742010-09-02 17:18:32 +0000289 case MCSA_WeakReference:
Matt Fleming3565a062010-08-16 18:57:57 +0000290 case MCSA_Weak:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000291 SetBinding(SD, ELF::STB_WEAK);
Rafael Espindola3223f192010-10-06 16:47:31 +0000292 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000293 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000294 break;
295
296 case MCSA_Local:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000297 SetBinding(SD, ELF::STB_LOCAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000298 SD.setExternal(false);
299 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000300 break;
301
302 case MCSA_ELF_TypeFunction:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000303 SetType(SD, ELF::STT_FUNC);
Matt Fleming3565a062010-08-16 18:57:57 +0000304 break;
305
306 case MCSA_ELF_TypeObject:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000307 SetType(SD, ELF::STT_OBJECT);
Matt Fleming3565a062010-08-16 18:57:57 +0000308 break;
309
310 case MCSA_ELF_TypeTLS:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000311 SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000312 break;
313
314 case MCSA_ELF_TypeCommon:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000315 SetType(SD, ELF::STT_COMMON);
Matt Fleming3565a062010-08-16 18:57:57 +0000316 break;
317
318 case MCSA_ELF_TypeNoType:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000319 SetType(SD, ELF::STT_NOTYPE);
Matt Fleming3565a062010-08-16 18:57:57 +0000320 break;
321
322 case MCSA_Protected:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000323 SetVisibility(SD, ELF::STV_PROTECTED);
Matt Fleming3565a062010-08-16 18:57:57 +0000324 break;
325
326 case MCSA_Hidden:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000327 SetVisibility(SD, ELF::STV_HIDDEN);
Matt Fleming3565a062010-08-16 18:57:57 +0000328 break;
329
330 case MCSA_Internal:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000331 SetVisibility(SD, ELF::STV_INTERNAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000332 break;
333 }
334}
335
336void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
337 unsigned ByteAlignment) {
338 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
339
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000340 if (!BindingExplicitlySet.count(Symbol)) {
341 SetBinding(SD, ELF::STB_GLOBAL);
342 SD.setExternal(true);
343 }
344
Rafael Espindola55d02f32010-11-14 21:11:16 +0000345 SetType(SD, ELF::STT_OBJECT);
346
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000347 if (GetBinding(SD) == ELF_STB_Local) {
Matt Fleming3565a062010-08-16 18:57:57 +0000348 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
349 MCSectionELF::SHT_NOBITS,
350 MCSectionELF::SHF_WRITE |
351 MCSectionELF::SHF_ALLOC,
352 SectionKind::getBSS());
Matt Fleming3565a062010-08-16 18:57:57 +0000353 Symbol->setSection(*Section);
Rafael Espindola19635722010-09-22 17:43:04 +0000354
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000355 struct LocalCommon L = {&SD, Size, ByteAlignment};
356 LocalCommons.push_back(L);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000357 } else {
358 SD.setCommon(Size, ByteAlignment);
Matt Fleming3565a062010-08-16 18:57:57 +0000359 }
360
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000361 SD.setSize(MCConstantExpr::Create(Size, getContext()));
Matt Fleming3565a062010-08-16 18:57:57 +0000362}
363
Jason W Kimf13743b2010-12-16 03:12:17 +0000364void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
365 // FIXME: Should this be caught and done earlier?
366 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
367 SetBinding(SD, ELF::STB_LOCAL);
368 SD.setExternal(false);
369 BindingExplicitlySet.insert(Symbol);
370 // FIXME: ByteAlignment is not needed here, but is required.
371 EmitCommonSymbol(Symbol, Size, 1);
372}
373
Matt Fleming3565a062010-08-16 18:57:57 +0000374void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
375 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
376 // MCObjectStreamer.
377 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
378}
379
Matt Fleming3565a062010-08-16 18:57:57 +0000380void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
381 int64_t Value, unsigned ValueSize,
382 unsigned MaxBytesToEmit) {
383 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
384 // MCObjectStreamer.
385 if (MaxBytesToEmit == 0)
386 MaxBytesToEmit = ByteAlignment;
387 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
388 getCurrentSectionData());
389
390 // Update the maximum alignment on the current section if necessary.
391 if (ByteAlignment > getCurrentSectionData()->getAlignment())
392 getCurrentSectionData()->setAlignment(ByteAlignment);
393}
394
395void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
396 unsigned MaxBytesToEmit) {
397 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
398 // MCObjectStreamer.
399 if (MaxBytesToEmit == 0)
400 MaxBytesToEmit = ByteAlignment;
401 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
402 getCurrentSectionData());
403 F->setEmitNops(true);
404
405 // Update the maximum alignment on the current section if necessary.
406 if (ByteAlignment > getCurrentSectionData()->getAlignment())
407 getCurrentSectionData()->setAlignment(ByteAlignment);
408}
409
Matt Fleming3565a062010-08-16 18:57:57 +0000410// Add a symbol for the file name of this module. This is the second
411// entry in the module's symbol table (the first being the null symbol).
412void MCELFStreamer::EmitFileDirective(StringRef Filename) {
413 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
414 Symbol->setSection(*CurSection);
415 Symbol->setAbsolute();
416
417 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
418
419 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
420}
421
Rafael Espindola97551272010-11-24 02:19:40 +0000422void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
423 switch (expr->getKind()) {
424 case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
425 case MCExpr::Constant:
426 break;
427
428 case MCExpr::Binary: {
429 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
430 fixSymbolsInTLSFixups(be->getLHS());
431 fixSymbolsInTLSFixups(be->getRHS());
432 break;
433 }
434
435 case MCExpr::SymbolRef: {
436 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000437 switch (symRef.getKind()) {
438 default:
Rafael Espindola97551272010-11-24 02:19:40 +0000439 return;
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000440 case MCSymbolRefExpr::VK_NTPOFF:
441 case MCSymbolRefExpr::VK_GOTNTPOFF:
442 case MCSymbolRefExpr::VK_TLSGD:
443 case MCSymbolRefExpr::VK_TLSLDM:
444 case MCSymbolRefExpr::VK_TPOFF:
445 case MCSymbolRefExpr::VK_DTPOFF:
446 case MCSymbolRefExpr::VK_GOTTPOFF:
447 case MCSymbolRefExpr::VK_TLSLD:
448 case MCSymbolRefExpr::VK_ARM_TLSGD:
449 break;
450 }
Rafael Espindola97551272010-11-24 02:19:40 +0000451 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
452 SetType(SD, ELF::STT_TLS);
453 break;
454 }
455
456 case MCExpr::Unary:
457 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
458 break;
459 }
460}
461
Benjamin Kramer93ded732010-08-27 10:40:51 +0000462void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
Rafael Espindoladedb0452010-12-02 05:44:06 +0000463 this->MCObjectStreamer::EmitInstToFragment(Inst);
464 MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment());
Benjamin Kramer93ded732010-08-27 10:40:51 +0000465
Rafael Espindoladedb0452010-12-02 05:44:06 +0000466 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
467 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
Benjamin Kramer93ded732010-08-27 10:40:51 +0000468}
469
470void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
471 MCDataFragment *DF = getOrCreateDataFragment();
472
473 SmallVector<MCFixup, 4> Fixups;
474 SmallString<256> Code;
475 raw_svector_ostream VecOS(Code);
476 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
477 VecOS.flush();
478
Rafael Espindola97551272010-11-24 02:19:40 +0000479 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
480 fixSymbolsInTLSFixups(Fixups[i].getValue());
481
Benjamin Kramer93ded732010-08-27 10:40:51 +0000482 // Add the fixups and data.
483 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
484 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
485 DF->addFixup(Fixups[i]);
486 }
487 DF->getContents().append(Code.begin(), Code.end());
488}
489
Matt Fleming3565a062010-08-16 18:57:57 +0000490void MCELFStreamer::Finish() {
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000491 if (getNumFrameInfos())
Rafael Espindola89b93722010-12-10 07:39:47 +0000492 MCDwarfFrameEmitter::Emit(*this);
Rafael Espindolac70a1d92010-11-01 17:07:14 +0000493
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000494 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
495 e = LocalCommons.end();
496 i != e; ++i) {
497 MCSymbolData *SD = i->SD;
498 uint64_t Size = i->Size;
499 unsigned ByteAlignment = i->ByteAlignment;
500 const MCSymbol &Symbol = SD->getSymbol();
501 const MCSection &Section = Symbol.getSection();
502
503 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
504 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
505
506 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
507 SD->setFragment(F);
508
509 // Update the maximum alignment of the section if necessary.
510 if (ByteAlignment > SectData.getAlignment())
511 SectData.setAlignment(ByteAlignment);
512 }
513
Rafael Espindola73ffea42010-09-25 05:42:19 +0000514 this->MCObjectStreamer::Finish();
Matt Fleming3565a062010-08-16 18:57:57 +0000515}
516
517MCStreamer *llvm::createELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
518 raw_ostream &OS, MCCodeEmitter *CE,
519 bool RelaxAll) {
520 MCELFStreamer *S = new MCELFStreamer(Context, TAB, OS, CE);
521 if (RelaxAll)
522 S->getAssembler().setRelaxAll(true);
523 return S;
524}