blob: 8107005481d7f5261c95afd7e30efae976f80d61 [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
Jan Sjödin2ddfd952011-02-28 21:45:04 +000014#include "MCELF.h"
Rafael Espindolaf340a292012-01-07 23:18:39 +000015#include "llvm/ADT/SmallPtrSet.h"
Jim Grosbach3e965312012-05-18 19:12:01 +000016#include "llvm/ADT/StringExtras.h"
17#include "llvm/ADT/Twine.h"
Rafael Espindolaf340a292012-01-07 23:18:39 +000018#include "llvm/MC/MCAssembler.h"
Matt Fleming3565a062010-08-16 18:57:57 +000019#include "llvm/MC/MCCodeEmitter.h"
Rafael Espindolaf340a292012-01-07 23:18:39 +000020#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCSectionELF.h"
22#include "llvm/MC/MCStreamer.h"
Matt Fleming3565a062010-08-16 18:57:57 +000023#include "llvm/MC/MCELFSymbolFlags.h"
24#include "llvm/MC/MCExpr.h"
25#include "llvm/MC/MCInst.h"
Rafael Espindolaf340a292012-01-07 23:18:39 +000026#include "llvm/MC/MCObjectStreamer.h"
Matt Fleming3565a062010-08-16 18:57:57 +000027#include "llvm/MC/MCSection.h"
Matt Fleming3565a062010-08-16 18:57:57 +000028#include "llvm/MC/MCSymbol.h"
Rafael Espindola484291c2010-11-01 14:28:48 +000029#include "llvm/MC/MCValue.h"
Evan Cheng78c10ee2011-07-25 23:24:55 +000030#include "llvm/MC/MCAsmBackend.h"
Matt Fleming3565a062010-08-16 18:57:57 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/ELF.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/raw_ostream.h"
Matt Fleming3565a062010-08-16 18:57:57 +000035
36using namespace llvm;
37
Rafael Espindolaf340a292012-01-07 23:18:39 +000038namespace {
39class MCELFStreamer : public MCObjectStreamer {
40public:
41 MCELFStreamer(MCContext &Context, MCAsmBackend &TAB,
42 raw_ostream &OS, MCCodeEmitter *Emitter)
43 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
44
45 MCELFStreamer(MCContext &Context, MCAsmBackend &TAB,
46 raw_ostream &OS, MCCodeEmitter *Emitter,
47 MCAssembler *Assembler)
48 : MCObjectStreamer(Context, TAB, OS, Emitter, Assembler) {}
49
50
51 ~MCELFStreamer() {}
52
53 /// @name MCStreamer Interface
54 /// @{
55
56 virtual void InitSections();
57 virtual void ChangeSection(const MCSection *Section);
58 virtual void EmitLabel(MCSymbol *Symbol);
59 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
60 virtual void EmitThumbFunc(MCSymbol *Func);
61 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
62 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
63 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
64 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Craig Topper85814382012-02-07 05:05:23 +000065 llvm_unreachable("ELF doesn't support this directive");
Rafael Espindolaf340a292012-01-07 23:18:39 +000066 }
67 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
68 unsigned ByteAlignment);
69 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
Craig Topper85814382012-02-07 05:05:23 +000070 llvm_unreachable("ELF doesn't support this directive");
Rafael Espindolaf340a292012-01-07 23:18:39 +000071 }
72
73 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
Craig Topper85814382012-02-07 05:05:23 +000074 llvm_unreachable("ELF doesn't support this directive");
Rafael Espindolaf340a292012-01-07 23:18:39 +000075 }
76
77 virtual void EmitCOFFSymbolType(int Type) {
Craig Topper85814382012-02-07 05:05:23 +000078 llvm_unreachable("ELF doesn't support this directive");
Rafael Espindolaf340a292012-01-07 23:18:39 +000079 }
80
81 virtual void EndCOFFSymbolDef() {
Craig Topper85814382012-02-07 05:05:23 +000082 llvm_unreachable("ELF doesn't support this directive");
Rafael Espindolaf340a292012-01-07 23:18:39 +000083 }
84
85 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
86 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
87 SD.setSize(Value);
88 }
89
90 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
91 unsigned ByteAlignment);
92
93 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Evan Chengc90a1fc2012-06-22 20:14:46 +000094 uint64_t Size = 0, unsigned ByteAlignment = 0) {
Craig Topper85814382012-02-07 05:05:23 +000095 llvm_unreachable("ELF doesn't support this directive");
Rafael Espindolaf340a292012-01-07 23:18:39 +000096 }
97 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
98 uint64_t Size, unsigned ByteAlignment = 0) {
Craig Topper85814382012-02-07 05:05:23 +000099 llvm_unreachable("ELF doesn't support this directive");
Rafael Espindolaf340a292012-01-07 23:18:39 +0000100 }
David Meyer5f769262012-02-15 15:09:06 +0000101 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
102 unsigned AddrSpace);
Rafael Espindolaf340a292012-01-07 23:18:39 +0000103
104 virtual void EmitFileDirective(StringRef Filename);
105
106 virtual void FinishImpl();
107
108private:
109 virtual void EmitInstToFragment(const MCInst &Inst);
110 virtual void EmitInstToData(const MCInst &Inst);
111
112 void fixSymbolsInTLSFixups(const MCExpr *expr);
113
114 struct LocalCommon {
115 MCSymbolData *SD;
116 uint64_t Size;
117 unsigned ByteAlignment;
118 };
119 std::vector<LocalCommon> LocalCommons;
120
121 SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
122 /// @}
123 void SetSection(StringRef Section, unsigned Type, unsigned Flags,
124 SectionKind Kind) {
125 SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
126 }
127
128 void SetSectionData() {
129 SetSection(".data", ELF::SHT_PROGBITS,
130 ELF::SHF_WRITE |ELF::SHF_ALLOC,
131 SectionKind::getDataRel());
132 EmitCodeAlignment(4, 0);
133 }
134 void SetSectionText() {
135 SetSection(".text", ELF::SHT_PROGBITS,
136 ELF::SHF_EXECINSTR |
137 ELF::SHF_ALLOC, SectionKind::getText());
138 EmitCodeAlignment(4, 0);
139 }
140 void SetSectionBss() {
141 SetSection(".bss", ELF::SHT_NOBITS,
142 ELF::SHF_WRITE |
143 ELF::SHF_ALLOC, SectionKind::getBSS());
144 EmitCodeAlignment(4, 0);
145 }
146};
147}
148
Rafael Espindolad80781b2010-09-15 21:48:40 +0000149void MCELFStreamer::InitSections() {
150 // This emulates the same behavior of GNU as. This makes it easier
151 // to compare the output as the major sections are in the same order.
152 SetSectionText();
153 SetSectionData();
154 SetSectionBss();
155 SetSectionText();
156}
157
Matt Fleming3565a062010-08-16 18:57:57 +0000158void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindolaba210242010-11-28 16:22:59 +0000159 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
160
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000161 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindola73ffea42010-09-25 05:42:19 +0000162
Rafael Espindolae1a25872010-11-11 19:04:55 +0000163 const MCSectionELF &Section =
164 static_cast<const MCSectionELF&>(Symbol->getSection());
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000165 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Rafael Espindola1c130262011-01-23 04:43:11 +0000166 if (Section.getFlags() & ELF::SHF_TLS)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000167 MCELF::SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000168}
169
170void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
171 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000172 case MCAF_SyntaxUnified: return; // no-op here.
Evan Chengbd27f5a2011-07-27 00:38:12 +0000173 case MCAF_Code16: return; // Change parsing mode; no-op here.
174 case MCAF_Code32: return; // Change parsing mode; no-op here.
175 case MCAF_Code64: return; // Change parsing mode; no-op here.
Matt Fleming3565a062010-08-16 18:57:57 +0000176 case MCAF_SubsectionsViaSymbols:
177 getAssembler().setSubsectionsViaSymbols(true);
178 return;
179 }
180
Craig Topper85814382012-02-07 05:05:23 +0000181 llvm_unreachable("invalid assembler flag!");
Matt Fleming3565a062010-08-16 18:57:57 +0000182}
183
Jim Grosbachce792992010-11-05 22:08:08 +0000184void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
185 // FIXME: Anything needed here to flag the function as thumb?
Rafael Espindola64695402011-05-16 16:17:21 +0000186
187 getAssembler().setIsThumbFunc(Func);
188
189 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
190 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
Jim Grosbachce792992010-11-05 22:08:08 +0000191}
192
Matt Fleming3565a062010-08-16 18:57:57 +0000193void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
194 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
195 // MCObjectStreamer.
196 // FIXME: Lift context changes into super class.
197 getAssembler().getOrCreateSymbolData(*Symbol);
198 Symbol->setVariableValue(AddValueSymbols(Value));
199}
200
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000201void MCELFStreamer::ChangeSection(const MCSection *Section) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000202 const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
203 if (Grp)
204 getAssembler().getOrCreateSymbolData(*Grp);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000205 this->MCObjectStreamer::ChangeSection(Section);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000206}
207
Rafael Espindola484291c2010-11-01 14:28:48 +0000208void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
209 getAssembler().getOrCreateSymbolData(*Symbol);
210 MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
211 AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000212 const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext());
Rafael Espindola484291c2010-11-01 14:28:48 +0000213 Alias->setVariableValue(Value);
214}
215
Matt Fleming3565a062010-08-16 18:57:57 +0000216void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
217 MCSymbolAttr Attribute) {
218 // Indirect symbols are handled differently, to match how 'as' handles
219 // them. This makes writing matching .o files easier.
220 if (Attribute == MCSA_IndirectSymbol) {
221 // Note that we intentionally cannot use the symbol data here; this is
222 // important for matching the string table that 'as' generates.
223 IndirectSymbolData ISD;
224 ISD.Symbol = Symbol;
225 ISD.SectionData = getCurrentSectionData();
226 getAssembler().getIndirectSymbols().push_back(ISD);
227 return;
228 }
229
230 // Adding a symbol attribute always introduces the symbol, note that an
231 // important side effect of calling getOrCreateSymbolData here is to register
232 // the symbol with the assembler.
233 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
234
235 // The implementation of symbol attributes is designed to match 'as', but it
236 // leaves much to desired. It doesn't really make sense to arbitrarily add and
237 // remove flags, but 'as' allows this (in particular, see .desc).
238 //
239 // In the future it might be worth trying to make these operations more well
240 // defined.
241 switch (Attribute) {
242 case MCSA_LazyReference:
243 case MCSA_Reference:
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000244 case MCSA_SymbolResolver:
Matt Fleming3565a062010-08-16 18:57:57 +0000245 case MCSA_PrivateExtern:
Matt Fleming3565a062010-08-16 18:57:57 +0000246 case MCSA_WeakDefinition:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000247 case MCSA_WeakDefAutoPrivate:
Matt Fleming3565a062010-08-16 18:57:57 +0000248 case MCSA_Invalid:
Matt Fleming3565a062010-08-16 18:57:57 +0000249 case MCSA_IndirectSymbol:
Craig Topper85814382012-02-07 05:05:23 +0000250 llvm_unreachable("Invalid symbol attribute for ELF!");
Matt Fleming3565a062010-08-16 18:57:57 +0000251
Jim Grosbach3f90a4c2012-09-13 23:11:31 +0000252 case MCSA_NoDeadStrip:
Rafael Espindola7e528a12010-11-14 01:34:31 +0000253 case MCSA_ELF_TypeGnuUniqueObject:
254 // Ignore for now.
255 break;
256
Matt Fleming3565a062010-08-16 18:57:57 +0000257 case MCSA_Global:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000258 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000259 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000260 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000261 break;
262
Benjamin Kramer230c2742010-09-02 17:18:32 +0000263 case MCSA_WeakReference:
Matt Fleming3565a062010-08-16 18:57:57 +0000264 case MCSA_Weak:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000265 MCELF::SetBinding(SD, ELF::STB_WEAK);
Rafael Espindola3223f192010-10-06 16:47:31 +0000266 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000267 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000268 break;
269
270 case MCSA_Local:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000271 MCELF::SetBinding(SD, ELF::STB_LOCAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000272 SD.setExternal(false);
273 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000274 break;
275
276 case MCSA_ELF_TypeFunction:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000277 MCELF::SetType(SD, ELF::STT_FUNC);
Matt Fleming3565a062010-08-16 18:57:57 +0000278 break;
279
Roman Divackya0c17a42011-12-12 17:34:04 +0000280 case MCSA_ELF_TypeIndFunction:
281 MCELF::SetType(SD, ELF::STT_GNU_IFUNC);
282 break;
283
Matt Fleming3565a062010-08-16 18:57:57 +0000284 case MCSA_ELF_TypeObject:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000285 MCELF::SetType(SD, ELF::STT_OBJECT);
Matt Fleming3565a062010-08-16 18:57:57 +0000286 break;
287
288 case MCSA_ELF_TypeTLS:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000289 MCELF::SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000290 break;
291
292 case MCSA_ELF_TypeCommon:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000293 MCELF::SetType(SD, ELF::STT_COMMON);
Matt Fleming3565a062010-08-16 18:57:57 +0000294 break;
295
296 case MCSA_ELF_TypeNoType:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000297 MCELF::SetType(SD, ELF::STT_NOTYPE);
Matt Fleming3565a062010-08-16 18:57:57 +0000298 break;
299
300 case MCSA_Protected:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000301 MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
Matt Fleming3565a062010-08-16 18:57:57 +0000302 break;
303
304 case MCSA_Hidden:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000305 MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
Matt Fleming3565a062010-08-16 18:57:57 +0000306 break;
307
308 case MCSA_Internal:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000309 MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000310 break;
311 }
312}
313
314void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
315 unsigned ByteAlignment) {
316 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
317
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000318 if (!BindingExplicitlySet.count(Symbol)) {
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000319 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000320 SD.setExternal(true);
321 }
322
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000323 MCELF::SetType(SD, ELF::STT_OBJECT);
Rafael Espindola55d02f32010-11-14 21:11:16 +0000324
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000325 if (MCELF::GetBinding(SD) == ELF_STB_Local) {
Matt Fleming3565a062010-08-16 18:57:57 +0000326 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
Jim Grosbach946227d2011-11-15 16:46:22 +0000327 ELF::SHT_NOBITS,
328 ELF::SHF_WRITE |
329 ELF::SHF_ALLOC,
330 SectionKind::getBSS());
Matt Fleming3565a062010-08-16 18:57:57 +0000331 Symbol->setSection(*Section);
Rafael Espindola19635722010-09-22 17:43:04 +0000332
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000333 struct LocalCommon L = {&SD, Size, ByteAlignment};
334 LocalCommons.push_back(L);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000335 } else {
336 SD.setCommon(Size, ByteAlignment);
Matt Fleming3565a062010-08-16 18:57:57 +0000337 }
338
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000339 SD.setSize(MCConstantExpr::Create(Size, getContext()));
Matt Fleming3565a062010-08-16 18:57:57 +0000340}
341
Benjamin Kramer36a16012011-09-01 23:04:27 +0000342void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
343 unsigned ByteAlignment) {
Jason W Kimf13743b2010-12-16 03:12:17 +0000344 // FIXME: Should this be caught and done earlier?
345 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000346 MCELF::SetBinding(SD, ELF::STB_LOCAL);
Jason W Kimf13743b2010-12-16 03:12:17 +0000347 SD.setExternal(false);
348 BindingExplicitlySet.insert(Symbol);
Benjamin Kramer36a16012011-09-01 23:04:27 +0000349 EmitCommonSymbol(Symbol, Size, ByteAlignment);
Jason W Kimf13743b2010-12-16 03:12:17 +0000350}
351
David Meyer5f769262012-02-15 15:09:06 +0000352void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
353 unsigned AddrSpace) {
354 fixSymbolsInTLSFixups(Value);
355 MCObjectStreamer::EmitValueImpl(Value, Size, AddrSpace);
356}
357
358
Matt Fleming3565a062010-08-16 18:57:57 +0000359// Add a symbol for the file name of this module. This is the second
360// entry in the module's symbol table (the first being the null symbol).
361void MCELFStreamer::EmitFileDirective(StringRef Filename) {
362 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000363 Symbol->setSection(*getCurrentSection());
Matt Fleming3565a062010-08-16 18:57:57 +0000364 Symbol->setAbsolute();
365
366 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
367
368 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
369}
370
Rafael Espindola97551272010-11-24 02:19:40 +0000371void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
372 switch (expr->getKind()) {
373 case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
374 case MCExpr::Constant:
375 break;
376
377 case MCExpr::Binary: {
378 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
379 fixSymbolsInTLSFixups(be->getLHS());
380 fixSymbolsInTLSFixups(be->getRHS());
381 break;
382 }
383
384 case MCExpr::SymbolRef: {
385 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000386 switch (symRef.getKind()) {
387 default:
Rafael Espindola97551272010-11-24 02:19:40 +0000388 return;
Joerg Sonnenbergerd02c8b62011-03-17 00:35:10 +0000389 case MCSymbolRefExpr::VK_GOTTPOFF:
390 case MCSymbolRefExpr::VK_INDNTPOFF:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000391 case MCSymbolRefExpr::VK_NTPOFF:
392 case MCSymbolRefExpr::VK_GOTNTPOFF:
393 case MCSymbolRefExpr::VK_TLSGD:
Joerg Sonnenbergerd02c8b62011-03-17 00:35:10 +0000394 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000395 case MCSymbolRefExpr::VK_TLSLDM:
396 case MCSymbolRefExpr::VK_TPOFF:
397 case MCSymbolRefExpr::VK_DTPOFF:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000398 case MCSymbolRefExpr::VK_ARM_TLSGD:
Joerg Sonnenbergerd02c8b62011-03-17 00:35:10 +0000399 case MCSymbolRefExpr::VK_ARM_TPOFF:
400 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Bruno Cardoso Lopes3507d242011-10-25 18:13:20 +0000401 case MCSymbolRefExpr::VK_Mips_TLSGD:
402 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
403 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
404 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000405 break;
406 }
Rafael Espindola97551272010-11-24 02:19:40 +0000407 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000408 MCELF::SetType(SD, ELF::STT_TLS);
Rafael Espindola97551272010-11-24 02:19:40 +0000409 break;
410 }
411
412 case MCExpr::Unary:
413 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
414 break;
415 }
416}
417
Benjamin Kramer93ded732010-08-27 10:40:51 +0000418void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
Rafael Espindoladedb0452010-12-02 05:44:06 +0000419 this->MCObjectStreamer::EmitInstToFragment(Inst);
420 MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment());
Benjamin Kramer93ded732010-08-27 10:40:51 +0000421
Rafael Espindoladedb0452010-12-02 05:44:06 +0000422 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
423 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
Benjamin Kramer93ded732010-08-27 10:40:51 +0000424}
425
426void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
427 MCDataFragment *DF = getOrCreateDataFragment();
428
429 SmallVector<MCFixup, 4> Fixups;
430 SmallString<256> Code;
431 raw_svector_ostream VecOS(Code);
432 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
433 VecOS.flush();
434
Rafael Espindola97551272010-11-24 02:19:40 +0000435 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
436 fixSymbolsInTLSFixups(Fixups[i].getValue());
437
Benjamin Kramer93ded732010-08-27 10:40:51 +0000438 // Add the fixups and data.
439 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
440 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
441 DF->addFixup(Fixups[i]);
442 }
443 DF->getContents().append(Code.begin(), Code.end());
444}
445
Rafael Espindola99b42372012-01-07 03:13:18 +0000446void MCELFStreamer::FinishImpl() {
Rafael Espindolac25dad82011-05-10 03:14:15 +0000447 EmitFrames(true);
Rafael Espindolac70a1d92010-11-01 17:07:14 +0000448
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000449 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
450 e = LocalCommons.end();
451 i != e; ++i) {
452 MCSymbolData *SD = i->SD;
453 uint64_t Size = i->Size;
454 unsigned ByteAlignment = i->ByteAlignment;
455 const MCSymbol &Symbol = SD->getSymbol();
456 const MCSection &Section = Symbol.getSection();
457
458 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
459 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
460
461 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
462 SD->setFragment(F);
463
464 // Update the maximum alignment of the section if necessary.
465 if (ByteAlignment > SectData.getAlignment())
466 SectData.setAlignment(ByteAlignment);
467 }
468
Rafael Espindola99b42372012-01-07 03:13:18 +0000469 this->MCObjectStreamer::FinishImpl();
Matt Fleming3565a062010-08-16 18:57:57 +0000470}
471
Evan Cheng78c10ee2011-07-25 23:24:55 +0000472MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000473 raw_ostream &OS, MCCodeEmitter *CE,
474 bool RelaxAll, bool NoExecStack) {
Evan Cheng78c10ee2011-07-25 23:24:55 +0000475 MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
Matt Fleming3565a062010-08-16 18:57:57 +0000476 if (RelaxAll)
477 S->getAssembler().setRelaxAll(true);
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000478 if (NoExecStack)
479 S->getAssembler().setNoExecStack(true);
Matt Fleming3565a062010-08-16 18:57:57 +0000480 return S;
481}