blob: 21d0be99caaa022607bdbb7f6e0d1b0189d6288d [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
Jan Sjödin2ddfd952011-02-28 21:45:04 +000016#include "MCELF.h"
Rafael Espindolaf7c10a32010-09-21 00:24:38 +000017#include "llvm/ADT/SmallPtrSet.h"
Matt Fleming3565a062010-08-16 18:57:57 +000018#include "llvm/MC/MCAssembler.h"
19#include "llvm/MC/MCContext.h"
20#include "llvm/MC/MCCodeEmitter.h"
21#include "llvm/MC/MCELFSymbolFlags.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCObjectStreamer.h"
25#include "llvm/MC/MCSection.h"
26#include "llvm/MC/MCSectionELF.h"
27#include "llvm/MC/MCSymbol.h"
Rafael Espindola484291c2010-11-01 14:28:48 +000028#include "llvm/MC/MCValue.h"
Matt Fleming3565a062010-08-16 18:57:57 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/ELF.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/raw_ostream.h"
33#include "llvm/Target/TargetAsmBackend.h"
Rafael Espindola89b93722010-12-10 07:39:47 +000034#include "llvm/Target/TargetAsmInfo.h"
Matt Fleming3565a062010-08-16 18:57:57 +000035
36using namespace llvm;
37
38namespace {
39
40class MCELFStreamer : public MCObjectStreamer {
41public:
42 MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
43 raw_ostream &OS, MCCodeEmitter *Emitter)
Rafael Espindola85f2ecc2010-12-07 00:27:36 +000044 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
Matt Fleming3565a062010-08-16 18:57:57 +000045
46 ~MCELFStreamer() {}
47
48 /// @name MCStreamer Interface
49 /// @{
50
Rafael Espindolad80781b2010-09-15 21:48:40 +000051 virtual void InitSections();
Rafael Espindola7768a9d2011-02-16 01:08:29 +000052 virtual void ChangeSection(const MCSection *Section);
Matt Fleming3565a062010-08-16 18:57:57 +000053 virtual void EmitLabel(MCSymbol *Symbol);
54 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +000055 virtual void EmitThumbFunc(MCSymbol *Func);
Matt Fleming3565a062010-08-16 18:57:57 +000056 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Rafael Espindola484291c2010-11-01 14:28:48 +000057 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +000058 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
59 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
60 assert(0 && "ELF doesn't support this directive");
61 }
62 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
63 unsigned ByteAlignment);
64 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
65 assert(0 && "ELF doesn't support this directive");
66 }
67
68 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
69 assert(0 && "ELF doesn't support this directive");
70 }
71
72 virtual void EmitCOFFSymbolType(int Type) {
73 assert(0 && "ELF doesn't support this directive");
74 }
75
76 virtual void EndCOFFSymbolDef() {
77 assert(0 && "ELF doesn't support this directive");
78 }
79
80 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
81 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
82 SD.setSize(Value);
83 }
84
Jason W Kimf13743b2010-12-16 03:12:17 +000085 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
86
Matt Fleming3565a062010-08-16 18:57:57 +000087 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
88 unsigned Size = 0, unsigned ByteAlignment = 0) {
89 assert(0 && "ELF doesn't support this directive");
90 }
91 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
92 uint64_t Size, unsigned ByteAlignment = 0) {
93 assert(0 && "ELF doesn't support this directive");
94 }
95 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Matt Fleming3565a062010-08-16 18:57:57 +000096 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);
Matt Fleming3565a062010-08-16 18:57:57 +0000101
102 virtual void EmitFileDirective(StringRef Filename);
Matt Fleming3565a062010-08-16 18:57:57 +0000103
Matt Fleming3565a062010-08-16 18:57:57 +0000104 virtual void Finish();
105
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000106private:
Rafael Espindolaf89671d2010-11-01 16:27:31 +0000107 virtual void EmitInstToFragment(const MCInst &Inst);
108 virtual void EmitInstToData(const MCInst &Inst);
109
Rafael Espindola97551272010-11-24 02:19:40 +0000110 void fixSymbolsInTLSFixups(const MCExpr *expr);
111
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000112 struct LocalCommon {
113 MCSymbolData *SD;
114 uint64_t Size;
115 unsigned ByteAlignment;
116 };
117 std::vector<LocalCommon> LocalCommons;
118
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000119 SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
Matt Fleming3565a062010-08-16 18:57:57 +0000120 /// @}
Rafael Espindolad80781b2010-09-15 21:48:40 +0000121 void SetSection(StringRef Section, unsigned Type, unsigned Flags,
122 SectionKind Kind) {
123 SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
124 }
125
126 void SetSectionData() {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000127 SetSection(".data", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000128 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Rafael Espindolad80781b2010-09-15 21:48:40 +0000129 SectionKind::getDataRel());
130 EmitCodeAlignment(4, 0);
131 }
132 void SetSectionText() {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000133 SetSection(".text", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000134 ELF::SHF_EXECINSTR |
135 ELF::SHF_ALLOC, SectionKind::getText());
Rafael Espindolad80781b2010-09-15 21:48:40 +0000136 EmitCodeAlignment(4, 0);
137 }
138 void SetSectionBss() {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000139 SetSection(".bss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000140 ELF::SHF_WRITE |
141 ELF::SHF_ALLOC, SectionKind::getBSS());
Rafael Espindolad80781b2010-09-15 21:48:40 +0000142 EmitCodeAlignment(4, 0);
143 }
Matt Fleming3565a062010-08-16 18:57:57 +0000144};
145
146} // end anonymous namespace.
147
Rafael Espindolad80781b2010-09-15 21:48:40 +0000148void MCELFStreamer::InitSections() {
149 // This emulates the same behavior of GNU as. This makes it easier
150 // to compare the output as the major sections are in the same order.
151 SetSectionText();
152 SetSectionData();
153 SetSectionBss();
154 SetSectionText();
155}
156
Matt Fleming3565a062010-08-16 18:57:57 +0000157void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindolaba210242010-11-28 16:22:59 +0000158 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
159
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000160 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindola73ffea42010-09-25 05:42:19 +0000161
Rafael Espindolae1a25872010-11-11 19:04:55 +0000162 const MCSectionELF &Section =
163 static_cast<const MCSectionELF&>(Symbol->getSection());
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000164 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Rafael Espindola1c130262011-01-23 04:43:11 +0000165 if (Section.getFlags() & ELF::SHF_TLS)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000166 MCELF::SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000167}
168
169void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
170 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000171 case MCAF_SyntaxUnified: return; // no-op here.
172 case MCAF_Code16: return; // no-op here.
Jim Grosbachba219572010-11-05 22:40:09 +0000173 case MCAF_Code32: return; // no-op here.
Matt Fleming3565a062010-08-16 18:57:57 +0000174 case MCAF_SubsectionsViaSymbols:
175 getAssembler().setSubsectionsViaSymbols(true);
176 return;
177 }
178
179 assert(0 && "invalid assembler flag!");
180}
181
Jim Grosbachce792992010-11-05 22:08:08 +0000182void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
183 // FIXME: Anything needed here to flag the function as thumb?
184}
185
Matt Fleming3565a062010-08-16 18:57:57 +0000186void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
187 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
188 // MCObjectStreamer.
189 // FIXME: Lift context changes into super class.
190 getAssembler().getOrCreateSymbolData(*Symbol);
191 Symbol->setVariableValue(AddValueSymbols(Value));
192}
193
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000194void MCELFStreamer::ChangeSection(const MCSection *Section) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000195 const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
196 if (Grp)
197 getAssembler().getOrCreateSymbolData(*Grp);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000198 this->MCObjectStreamer::ChangeSection(Section);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000199}
200
Rafael Espindola484291c2010-11-01 14:28:48 +0000201void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
202 getAssembler().getOrCreateSymbolData(*Symbol);
203 MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
204 AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000205 const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext());
Rafael Espindola484291c2010-11-01 14:28:48 +0000206 Alias->setVariableValue(Value);
207}
208
Matt Fleming3565a062010-08-16 18:57:57 +0000209void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
210 MCSymbolAttr Attribute) {
211 // Indirect symbols are handled differently, to match how 'as' handles
212 // them. This makes writing matching .o files easier.
213 if (Attribute == MCSA_IndirectSymbol) {
214 // Note that we intentionally cannot use the symbol data here; this is
215 // important for matching the string table that 'as' generates.
216 IndirectSymbolData ISD;
217 ISD.Symbol = Symbol;
218 ISD.SectionData = getCurrentSectionData();
219 getAssembler().getIndirectSymbols().push_back(ISD);
220 return;
221 }
222
223 // Adding a symbol attribute always introduces the symbol, note that an
224 // important side effect of calling getOrCreateSymbolData here is to register
225 // the symbol with the assembler.
226 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
227
228 // The implementation of symbol attributes is designed to match 'as', but it
229 // leaves much to desired. It doesn't really make sense to arbitrarily add and
230 // remove flags, but 'as' allows this (in particular, see .desc).
231 //
232 // In the future it might be worth trying to make these operations more well
233 // defined.
234 switch (Attribute) {
235 case MCSA_LazyReference:
236 case MCSA_Reference:
237 case MCSA_NoDeadStrip:
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000238 case MCSA_SymbolResolver:
Matt Fleming3565a062010-08-16 18:57:57 +0000239 case MCSA_PrivateExtern:
Matt Fleming3565a062010-08-16 18:57:57 +0000240 case MCSA_WeakDefinition:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000241 case MCSA_WeakDefAutoPrivate:
Matt Fleming3565a062010-08-16 18:57:57 +0000242 case MCSA_Invalid:
243 case MCSA_ELF_TypeIndFunction:
244 case MCSA_IndirectSymbol:
245 assert(0 && "Invalid symbol attribute for ELF!");
246 break;
247
Rafael Espindola7e528a12010-11-14 01:34:31 +0000248 case MCSA_ELF_TypeGnuUniqueObject:
249 // Ignore for now.
250 break;
251
Matt Fleming3565a062010-08-16 18:57:57 +0000252 case MCSA_Global:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000253 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000254 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000255 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000256 break;
257
Benjamin Kramer230c2742010-09-02 17:18:32 +0000258 case MCSA_WeakReference:
Matt Fleming3565a062010-08-16 18:57:57 +0000259 case MCSA_Weak:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000260 MCELF::SetBinding(SD, ELF::STB_WEAK);
Rafael Espindola3223f192010-10-06 16:47:31 +0000261 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000262 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000263 break;
264
265 case MCSA_Local:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000266 MCELF::SetBinding(SD, ELF::STB_LOCAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000267 SD.setExternal(false);
268 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000269 break;
270
271 case MCSA_ELF_TypeFunction:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000272 MCELF::SetType(SD, ELF::STT_FUNC);
Matt Fleming3565a062010-08-16 18:57:57 +0000273 break;
274
275 case MCSA_ELF_TypeObject:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000276 MCELF::SetType(SD, ELF::STT_OBJECT);
Matt Fleming3565a062010-08-16 18:57:57 +0000277 break;
278
279 case MCSA_ELF_TypeTLS:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000280 MCELF::SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000281 break;
282
283 case MCSA_ELF_TypeCommon:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000284 MCELF::SetType(SD, ELF::STT_COMMON);
Matt Fleming3565a062010-08-16 18:57:57 +0000285 break;
286
287 case MCSA_ELF_TypeNoType:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000288 MCELF::SetType(SD, ELF::STT_NOTYPE);
Matt Fleming3565a062010-08-16 18:57:57 +0000289 break;
290
291 case MCSA_Protected:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000292 MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
Matt Fleming3565a062010-08-16 18:57:57 +0000293 break;
294
295 case MCSA_Hidden:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000296 MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
Matt Fleming3565a062010-08-16 18:57:57 +0000297 break;
298
299 case MCSA_Internal:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000300 MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000301 break;
302 }
303}
304
305void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
306 unsigned ByteAlignment) {
307 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
308
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000309 if (!BindingExplicitlySet.count(Symbol)) {
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000310 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000311 SD.setExternal(true);
312 }
313
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000314 MCELF::SetType(SD, ELF::STT_OBJECT);
Rafael Espindola55d02f32010-11-14 21:11:16 +0000315
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000316 if (MCELF::GetBinding(SD) == ELF_STB_Local) {
Matt Fleming3565a062010-08-16 18:57:57 +0000317 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
Rafael Espindolac85dca62011-01-23 04:28:49 +0000318 ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000319 ELF::SHF_WRITE |
320 ELF::SHF_ALLOC,
Matt Fleming3565a062010-08-16 18:57:57 +0000321 SectionKind::getBSS());
Matt Fleming3565a062010-08-16 18:57:57 +0000322 Symbol->setSection(*Section);
Rafael Espindola19635722010-09-22 17:43:04 +0000323
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000324 struct LocalCommon L = {&SD, Size, ByteAlignment};
325 LocalCommons.push_back(L);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000326 } else {
327 SD.setCommon(Size, ByteAlignment);
Matt Fleming3565a062010-08-16 18:57:57 +0000328 }
329
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000330 SD.setSize(MCConstantExpr::Create(Size, getContext()));
Matt Fleming3565a062010-08-16 18:57:57 +0000331}
332
Jason W Kimf13743b2010-12-16 03:12:17 +0000333void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
334 // FIXME: Should this be caught and done earlier?
335 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000336 MCELF::SetBinding(SD, ELF::STB_LOCAL);
Jason W Kimf13743b2010-12-16 03:12:17 +0000337 SD.setExternal(false);
338 BindingExplicitlySet.insert(Symbol);
339 // FIXME: ByteAlignment is not needed here, but is required.
340 EmitCommonSymbol(Symbol, Size, 1);
341}
342
Matt Fleming3565a062010-08-16 18:57:57 +0000343void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
344 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
345 // MCObjectStreamer.
346 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
347}
348
Matt Fleming3565a062010-08-16 18:57:57 +0000349void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
350 int64_t Value, unsigned ValueSize,
351 unsigned MaxBytesToEmit) {
352 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
353 // MCObjectStreamer.
354 if (MaxBytesToEmit == 0)
355 MaxBytesToEmit = ByteAlignment;
356 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
357 getCurrentSectionData());
358
359 // Update the maximum alignment on the current section if necessary.
360 if (ByteAlignment > getCurrentSectionData()->getAlignment())
361 getCurrentSectionData()->setAlignment(ByteAlignment);
362}
363
364void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
365 unsigned MaxBytesToEmit) {
366 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
367 // MCObjectStreamer.
368 if (MaxBytesToEmit == 0)
369 MaxBytesToEmit = ByteAlignment;
370 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
371 getCurrentSectionData());
372 F->setEmitNops(true);
373
374 // Update the maximum alignment on the current section if necessary.
375 if (ByteAlignment > getCurrentSectionData()->getAlignment())
376 getCurrentSectionData()->setAlignment(ByteAlignment);
377}
378
Matt Fleming3565a062010-08-16 18:57:57 +0000379// Add a symbol for the file name of this module. This is the second
380// entry in the module's symbol table (the first being the null symbol).
381void MCELFStreamer::EmitFileDirective(StringRef Filename) {
382 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000383 Symbol->setSection(*getCurrentSection());
Matt Fleming3565a062010-08-16 18:57:57 +0000384 Symbol->setAbsolute();
385
386 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
387
388 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
389}
390
Rafael Espindola97551272010-11-24 02:19:40 +0000391void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
392 switch (expr->getKind()) {
393 case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
394 case MCExpr::Constant:
395 break;
396
397 case MCExpr::Binary: {
398 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
399 fixSymbolsInTLSFixups(be->getLHS());
400 fixSymbolsInTLSFixups(be->getRHS());
401 break;
402 }
403
404 case MCExpr::SymbolRef: {
405 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000406 switch (symRef.getKind()) {
407 default:
Rafael Espindola97551272010-11-24 02:19:40 +0000408 return;
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000409 case MCSymbolRefExpr::VK_NTPOFF:
410 case MCSymbolRefExpr::VK_GOTNTPOFF:
411 case MCSymbolRefExpr::VK_TLSGD:
412 case MCSymbolRefExpr::VK_TLSLDM:
413 case MCSymbolRefExpr::VK_TPOFF:
414 case MCSymbolRefExpr::VK_DTPOFF:
415 case MCSymbolRefExpr::VK_GOTTPOFF:
416 case MCSymbolRefExpr::VK_TLSLD:
417 case MCSymbolRefExpr::VK_ARM_TLSGD:
418 break;
419 }
Rafael Espindola97551272010-11-24 02:19:40 +0000420 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000421 MCELF::SetType(SD, ELF::STT_TLS);
Rafael Espindola97551272010-11-24 02:19:40 +0000422 break;
423 }
424
425 case MCExpr::Unary:
426 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
427 break;
428 }
429}
430
Benjamin Kramer93ded732010-08-27 10:40:51 +0000431void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
Rafael Espindoladedb0452010-12-02 05:44:06 +0000432 this->MCObjectStreamer::EmitInstToFragment(Inst);
433 MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment());
Benjamin Kramer93ded732010-08-27 10:40:51 +0000434
Rafael Espindoladedb0452010-12-02 05:44:06 +0000435 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
436 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
Benjamin Kramer93ded732010-08-27 10:40:51 +0000437}
438
439void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
440 MCDataFragment *DF = getOrCreateDataFragment();
441
442 SmallVector<MCFixup, 4> Fixups;
443 SmallString<256> Code;
444 raw_svector_ostream VecOS(Code);
445 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
446 VecOS.flush();
447
Rafael Espindola97551272010-11-24 02:19:40 +0000448 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
449 fixSymbolsInTLSFixups(Fixups[i].getValue());
450
Benjamin Kramer93ded732010-08-27 10:40:51 +0000451 // Add the fixups and data.
452 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
453 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
454 DF->addFixup(Fixups[i]);
455 }
456 DF->getContents().append(Code.begin(), Code.end());
457}
458
Matt Fleming3565a062010-08-16 18:57:57 +0000459void MCELFStreamer::Finish() {
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000460 if (getNumFrameInfos())
Rafael Espindola89b93722010-12-10 07:39:47 +0000461 MCDwarfFrameEmitter::Emit(*this);
Rafael Espindolac70a1d92010-11-01 17:07:14 +0000462
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000463 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
464 e = LocalCommons.end();
465 i != e; ++i) {
466 MCSymbolData *SD = i->SD;
467 uint64_t Size = i->Size;
468 unsigned ByteAlignment = i->ByteAlignment;
469 const MCSymbol &Symbol = SD->getSymbol();
470 const MCSection &Section = Symbol.getSection();
471
472 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
473 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
474
475 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
476 SD->setFragment(F);
477
478 // Update the maximum alignment of the section if necessary.
479 if (ByteAlignment > SectData.getAlignment())
480 SectData.setAlignment(ByteAlignment);
481 }
482
Rafael Espindola73ffea42010-09-25 05:42:19 +0000483 this->MCObjectStreamer::Finish();
Matt Fleming3565a062010-08-16 18:57:57 +0000484}
485
486MCStreamer *llvm::createELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000487 raw_ostream &OS, MCCodeEmitter *CE,
488 bool RelaxAll, bool NoExecStack) {
Matt Fleming3565a062010-08-16 18:57:57 +0000489 MCELFStreamer *S = new MCELFStreamer(Context, TAB, OS, CE);
490 if (RelaxAll)
491 S->getAssembler().setRelaxAll(true);
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000492 if (NoExecStack)
493 S->getAssembler().setNoExecStack(true);
Matt Fleming3565a062010-08-16 18:57:57 +0000494 return S;
495}