blob: 6c4d0e33a115ecbb93a42a0ccd9a2b35e16d139d [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"
16#include "llvm/MC/MCAssembler.h"
Matt Fleming3565a062010-08-16 18:57:57 +000017#include "llvm/MC/MCCodeEmitter.h"
Rafael Espindolaf340a292012-01-07 23:18:39 +000018#include "llvm/MC/MCContext.h"
19#include "llvm/MC/MCSectionELF.h"
20#include "llvm/MC/MCStreamer.h"
Matt Fleming3565a062010-08-16 18:57:57 +000021#include "llvm/MC/MCELFSymbolFlags.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
Rafael Espindolaf340a292012-01-07 23:18:39 +000024#include "llvm/MC/MCObjectStreamer.h"
Matt Fleming3565a062010-08-16 18:57:57 +000025#include "llvm/MC/MCSection.h"
Matt Fleming3565a062010-08-16 18:57:57 +000026#include "llvm/MC/MCSymbol.h"
Rafael Espindola484291c2010-11-01 14:28:48 +000027#include "llvm/MC/MCValue.h"
Evan Cheng78c10ee2011-07-25 23:24:55 +000028#include "llvm/MC/MCAsmBackend.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"
Matt Fleming3565a062010-08-16 18:57:57 +000033
34using namespace llvm;
35
Rafael Espindolaf340a292012-01-07 23:18:39 +000036namespace {
37class MCELFStreamer : public MCObjectStreamer {
38public:
39 MCELFStreamer(MCContext &Context, MCAsmBackend &TAB,
40 raw_ostream &OS, MCCodeEmitter *Emitter)
41 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
42
43 MCELFStreamer(MCContext &Context, MCAsmBackend &TAB,
44 raw_ostream &OS, MCCodeEmitter *Emitter,
45 MCAssembler *Assembler)
46 : MCObjectStreamer(Context, TAB, OS, Emitter, Assembler) {}
47
48
49 ~MCELFStreamer() {}
50
51 /// @name MCStreamer Interface
52 /// @{
53
54 virtual void InitSections();
55 virtual void ChangeSection(const MCSection *Section);
56 virtual void EmitLabel(MCSymbol *Symbol);
57 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
58 virtual void EmitThumbFunc(MCSymbol *Func);
59 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
60 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
61 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
62 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Craig Topper85814382012-02-07 05:05:23 +000063 llvm_unreachable("ELF doesn't support this directive");
Rafael Espindolaf340a292012-01-07 23:18:39 +000064 }
65 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
66 unsigned ByteAlignment);
67 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
Craig Topper85814382012-02-07 05:05:23 +000068 llvm_unreachable("ELF doesn't support this directive");
Rafael Espindolaf340a292012-01-07 23:18:39 +000069 }
70
71 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
Craig Topper85814382012-02-07 05:05:23 +000072 llvm_unreachable("ELF doesn't support this directive");
Rafael Espindolaf340a292012-01-07 23:18:39 +000073 }
74
75 virtual void EmitCOFFSymbolType(int Type) {
Craig Topper85814382012-02-07 05:05:23 +000076 llvm_unreachable("ELF doesn't support this directive");
Rafael Espindolaf340a292012-01-07 23:18:39 +000077 }
78
79 virtual void EndCOFFSymbolDef() {
Craig Topper85814382012-02-07 05:05:23 +000080 llvm_unreachable("ELF doesn't support this directive");
Rafael Espindolaf340a292012-01-07 23:18:39 +000081 }
82
83 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
84 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
85 SD.setSize(Value);
86 }
87
88 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
89 unsigned ByteAlignment);
90
91 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
92 unsigned Size = 0, unsigned ByteAlignment = 0) {
Craig Topper85814382012-02-07 05:05:23 +000093 llvm_unreachable("ELF doesn't support this directive");
Rafael Espindolaf340a292012-01-07 23:18:39 +000094 }
95 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
96 uint64_t Size, unsigned ByteAlignment = 0) {
Craig Topper85814382012-02-07 05:05:23 +000097 llvm_unreachable("ELF doesn't support this directive");
Rafael Espindolaf340a292012-01-07 23:18:39 +000098 }
99 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
100 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
101 unsigned ValueSize = 1,
102 unsigned MaxBytesToEmit = 0);
103 virtual void EmitCodeAlignment(unsigned ByteAlignment,
104 unsigned MaxBytesToEmit = 0);
David Meyer5f769262012-02-15 15:09:06 +0000105 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
106 unsigned AddrSpace);
Rafael Espindolaf340a292012-01-07 23:18:39 +0000107
108 virtual void EmitFileDirective(StringRef Filename);
109
110 virtual void FinishImpl();
111
112private:
113 virtual void EmitInstToFragment(const MCInst &Inst);
114 virtual void EmitInstToData(const MCInst &Inst);
115
116 void fixSymbolsInTLSFixups(const MCExpr *expr);
117
118 struct LocalCommon {
119 MCSymbolData *SD;
120 uint64_t Size;
121 unsigned ByteAlignment;
122 };
123 std::vector<LocalCommon> LocalCommons;
124
125 SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
126 /// @}
127 void SetSection(StringRef Section, unsigned Type, unsigned Flags,
128 SectionKind Kind) {
129 SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
130 }
131
132 void SetSectionData() {
133 SetSection(".data", ELF::SHT_PROGBITS,
134 ELF::SHF_WRITE |ELF::SHF_ALLOC,
135 SectionKind::getDataRel());
136 EmitCodeAlignment(4, 0);
137 }
138 void SetSectionText() {
139 SetSection(".text", ELF::SHT_PROGBITS,
140 ELF::SHF_EXECINSTR |
141 ELF::SHF_ALLOC, SectionKind::getText());
142 EmitCodeAlignment(4, 0);
143 }
144 void SetSectionBss() {
145 SetSection(".bss", ELF::SHT_NOBITS,
146 ELF::SHF_WRITE |
147 ELF::SHF_ALLOC, SectionKind::getBSS());
148 EmitCodeAlignment(4, 0);
149 }
150};
151}
152
Rafael Espindolad80781b2010-09-15 21:48:40 +0000153void MCELFStreamer::InitSections() {
154 // This emulates the same behavior of GNU as. This makes it easier
155 // to compare the output as the major sections are in the same order.
156 SetSectionText();
157 SetSectionData();
158 SetSectionBss();
159 SetSectionText();
160}
161
Matt Fleming3565a062010-08-16 18:57:57 +0000162void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindolaba210242010-11-28 16:22:59 +0000163 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
164
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000165 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindola73ffea42010-09-25 05:42:19 +0000166
Rafael Espindolae1a25872010-11-11 19:04:55 +0000167 const MCSectionELF &Section =
168 static_cast<const MCSectionELF&>(Symbol->getSection());
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000169 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Rafael Espindola1c130262011-01-23 04:43:11 +0000170 if (Section.getFlags() & ELF::SHF_TLS)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000171 MCELF::SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000172}
173
174void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
175 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000176 case MCAF_SyntaxUnified: return; // no-op here.
Evan Chengbd27f5a2011-07-27 00:38:12 +0000177 case MCAF_Code16: return; // Change parsing mode; no-op here.
178 case MCAF_Code32: return; // Change parsing mode; no-op here.
179 case MCAF_Code64: return; // Change parsing mode; no-op here.
Matt Fleming3565a062010-08-16 18:57:57 +0000180 case MCAF_SubsectionsViaSymbols:
181 getAssembler().setSubsectionsViaSymbols(true);
182 return;
183 }
184
Craig Topper85814382012-02-07 05:05:23 +0000185 llvm_unreachable("invalid assembler flag!");
Matt Fleming3565a062010-08-16 18:57:57 +0000186}
187
Jim Grosbachce792992010-11-05 22:08:08 +0000188void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
189 // FIXME: Anything needed here to flag the function as thumb?
Rafael Espindola64695402011-05-16 16:17:21 +0000190
191 getAssembler().setIsThumbFunc(Func);
192
193 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
194 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
Jim Grosbachce792992010-11-05 22:08:08 +0000195}
196
Matt Fleming3565a062010-08-16 18:57:57 +0000197void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
198 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
199 // MCObjectStreamer.
200 // FIXME: Lift context changes into super class.
201 getAssembler().getOrCreateSymbolData(*Symbol);
202 Symbol->setVariableValue(AddValueSymbols(Value));
203}
204
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000205void MCELFStreamer::ChangeSection(const MCSection *Section) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000206 const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
207 if (Grp)
208 getAssembler().getOrCreateSymbolData(*Grp);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000209 this->MCObjectStreamer::ChangeSection(Section);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000210}
211
Rafael Espindola484291c2010-11-01 14:28:48 +0000212void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
213 getAssembler().getOrCreateSymbolData(*Symbol);
214 MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
215 AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000216 const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext());
Rafael Espindola484291c2010-11-01 14:28:48 +0000217 Alias->setVariableValue(Value);
218}
219
Matt Fleming3565a062010-08-16 18:57:57 +0000220void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
221 MCSymbolAttr Attribute) {
222 // Indirect symbols are handled differently, to match how 'as' handles
223 // them. This makes writing matching .o files easier.
224 if (Attribute == MCSA_IndirectSymbol) {
225 // Note that we intentionally cannot use the symbol data here; this is
226 // important for matching the string table that 'as' generates.
227 IndirectSymbolData ISD;
228 ISD.Symbol = Symbol;
229 ISD.SectionData = getCurrentSectionData();
230 getAssembler().getIndirectSymbols().push_back(ISD);
231 return;
232 }
233
234 // Adding a symbol attribute always introduces the symbol, note that an
235 // important side effect of calling getOrCreateSymbolData here is to register
236 // the symbol with the assembler.
237 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
238
239 // The implementation of symbol attributes is designed to match 'as', but it
240 // leaves much to desired. It doesn't really make sense to arbitrarily add and
241 // remove flags, but 'as' allows this (in particular, see .desc).
242 //
243 // In the future it might be worth trying to make these operations more well
244 // defined.
245 switch (Attribute) {
246 case MCSA_LazyReference:
247 case MCSA_Reference:
248 case MCSA_NoDeadStrip:
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000249 case MCSA_SymbolResolver:
Matt Fleming3565a062010-08-16 18:57:57 +0000250 case MCSA_PrivateExtern:
Matt Fleming3565a062010-08-16 18:57:57 +0000251 case MCSA_WeakDefinition:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000252 case MCSA_WeakDefAutoPrivate:
Matt Fleming3565a062010-08-16 18:57:57 +0000253 case MCSA_Invalid:
Matt Fleming3565a062010-08-16 18:57:57 +0000254 case MCSA_IndirectSymbol:
Craig Topper85814382012-02-07 05:05:23 +0000255 llvm_unreachable("Invalid symbol attribute for ELF!");
Matt Fleming3565a062010-08-16 18:57:57 +0000256
Rafael Espindola7e528a12010-11-14 01:34:31 +0000257 case MCSA_ELF_TypeGnuUniqueObject:
258 // Ignore for now.
259 break;
260
Matt Fleming3565a062010-08-16 18:57:57 +0000261 case MCSA_Global:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000262 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000263 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000264 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000265 break;
266
Benjamin Kramer230c2742010-09-02 17:18:32 +0000267 case MCSA_WeakReference:
Matt Fleming3565a062010-08-16 18:57:57 +0000268 case MCSA_Weak:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000269 MCELF::SetBinding(SD, ELF::STB_WEAK);
Rafael Espindola3223f192010-10-06 16:47:31 +0000270 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000271 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000272 break;
273
274 case MCSA_Local:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000275 MCELF::SetBinding(SD, ELF::STB_LOCAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000276 SD.setExternal(false);
277 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000278 break;
279
280 case MCSA_ELF_TypeFunction:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000281 MCELF::SetType(SD, ELF::STT_FUNC);
Matt Fleming3565a062010-08-16 18:57:57 +0000282 break;
283
Roman Divackya0c17a42011-12-12 17:34:04 +0000284 case MCSA_ELF_TypeIndFunction:
285 MCELF::SetType(SD, ELF::STT_GNU_IFUNC);
286 break;
287
Matt Fleming3565a062010-08-16 18:57:57 +0000288 case MCSA_ELF_TypeObject:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000289 MCELF::SetType(SD, ELF::STT_OBJECT);
Matt Fleming3565a062010-08-16 18:57:57 +0000290 break;
291
292 case MCSA_ELF_TypeTLS:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000293 MCELF::SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000294 break;
295
296 case MCSA_ELF_TypeCommon:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000297 MCELF::SetType(SD, ELF::STT_COMMON);
Matt Fleming3565a062010-08-16 18:57:57 +0000298 break;
299
300 case MCSA_ELF_TypeNoType:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000301 MCELF::SetType(SD, ELF::STT_NOTYPE);
Matt Fleming3565a062010-08-16 18:57:57 +0000302 break;
303
304 case MCSA_Protected:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000305 MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
Matt Fleming3565a062010-08-16 18:57:57 +0000306 break;
307
308 case MCSA_Hidden:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000309 MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
Matt Fleming3565a062010-08-16 18:57:57 +0000310 break;
311
312 case MCSA_Internal:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000313 MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000314 break;
315 }
316}
317
318void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
319 unsigned ByteAlignment) {
320 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
321
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000322 if (!BindingExplicitlySet.count(Symbol)) {
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000323 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000324 SD.setExternal(true);
325 }
326
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000327 MCELF::SetType(SD, ELF::STT_OBJECT);
Rafael Espindola55d02f32010-11-14 21:11:16 +0000328
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000329 if (MCELF::GetBinding(SD) == ELF_STB_Local) {
Matt Fleming3565a062010-08-16 18:57:57 +0000330 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
Jim Grosbach946227d2011-11-15 16:46:22 +0000331 ELF::SHT_NOBITS,
332 ELF::SHF_WRITE |
333 ELF::SHF_ALLOC,
334 SectionKind::getBSS());
Matt Fleming3565a062010-08-16 18:57:57 +0000335 Symbol->setSection(*Section);
Rafael Espindola19635722010-09-22 17:43:04 +0000336
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000337 struct LocalCommon L = {&SD, Size, ByteAlignment};
338 LocalCommons.push_back(L);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000339 } else {
340 SD.setCommon(Size, ByteAlignment);
Matt Fleming3565a062010-08-16 18:57:57 +0000341 }
342
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000343 SD.setSize(MCConstantExpr::Create(Size, getContext()));
Matt Fleming3565a062010-08-16 18:57:57 +0000344}
345
Benjamin Kramer36a16012011-09-01 23:04:27 +0000346void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
347 unsigned ByteAlignment) {
Jason W Kimf13743b2010-12-16 03:12:17 +0000348 // FIXME: Should this be caught and done earlier?
349 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000350 MCELF::SetBinding(SD, ELF::STB_LOCAL);
Jason W Kimf13743b2010-12-16 03:12:17 +0000351 SD.setExternal(false);
352 BindingExplicitlySet.insert(Symbol);
Benjamin Kramer36a16012011-09-01 23:04:27 +0000353 EmitCommonSymbol(Symbol, Size, ByteAlignment);
Jason W Kimf13743b2010-12-16 03:12:17 +0000354}
355
Matt Fleming3565a062010-08-16 18:57:57 +0000356void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
357 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
358 // MCObjectStreamer.
359 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
360}
361
Matt Fleming3565a062010-08-16 18:57:57 +0000362void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
363 int64_t Value, unsigned ValueSize,
364 unsigned MaxBytesToEmit) {
365 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
366 // MCObjectStreamer.
367 if (MaxBytesToEmit == 0)
368 MaxBytesToEmit = ByteAlignment;
369 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
370 getCurrentSectionData());
371
372 // Update the maximum alignment on the current section if necessary.
373 if (ByteAlignment > getCurrentSectionData()->getAlignment())
374 getCurrentSectionData()->setAlignment(ByteAlignment);
375}
376
377void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
378 unsigned MaxBytesToEmit) {
379 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
380 // MCObjectStreamer.
381 if (MaxBytesToEmit == 0)
382 MaxBytesToEmit = ByteAlignment;
383 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
384 getCurrentSectionData());
385 F->setEmitNops(true);
386
387 // Update the maximum alignment on the current section if necessary.
388 if (ByteAlignment > getCurrentSectionData()->getAlignment())
389 getCurrentSectionData()->setAlignment(ByteAlignment);
390}
391
David Meyer5f769262012-02-15 15:09:06 +0000392void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
393 unsigned AddrSpace) {
394 fixSymbolsInTLSFixups(Value);
395 MCObjectStreamer::EmitValueImpl(Value, Size, AddrSpace);
396}
397
398
Matt Fleming3565a062010-08-16 18:57:57 +0000399// Add a symbol for the file name of this module. This is the second
400// entry in the module's symbol table (the first being the null symbol).
401void MCELFStreamer::EmitFileDirective(StringRef Filename) {
402 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000403 Symbol->setSection(*getCurrentSection());
Matt Fleming3565a062010-08-16 18:57:57 +0000404 Symbol->setAbsolute();
405
406 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
407
408 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
409}
410
Rafael Espindola97551272010-11-24 02:19:40 +0000411void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
412 switch (expr->getKind()) {
413 case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
414 case MCExpr::Constant:
415 break;
416
417 case MCExpr::Binary: {
418 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
419 fixSymbolsInTLSFixups(be->getLHS());
420 fixSymbolsInTLSFixups(be->getRHS());
421 break;
422 }
423
424 case MCExpr::SymbolRef: {
425 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000426 switch (symRef.getKind()) {
427 default:
Rafael Espindola97551272010-11-24 02:19:40 +0000428 return;
Joerg Sonnenbergerd02c8b62011-03-17 00:35:10 +0000429 case MCSymbolRefExpr::VK_GOTTPOFF:
430 case MCSymbolRefExpr::VK_INDNTPOFF:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000431 case MCSymbolRefExpr::VK_NTPOFF:
432 case MCSymbolRefExpr::VK_GOTNTPOFF:
433 case MCSymbolRefExpr::VK_TLSGD:
Joerg Sonnenbergerd02c8b62011-03-17 00:35:10 +0000434 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000435 case MCSymbolRefExpr::VK_TLSLDM:
436 case MCSymbolRefExpr::VK_TPOFF:
437 case MCSymbolRefExpr::VK_DTPOFF:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000438 case MCSymbolRefExpr::VK_ARM_TLSGD:
Joerg Sonnenbergerd02c8b62011-03-17 00:35:10 +0000439 case MCSymbolRefExpr::VK_ARM_TPOFF:
440 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Bruno Cardoso Lopes3507d242011-10-25 18:13:20 +0000441 case MCSymbolRefExpr::VK_Mips_TLSGD:
442 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
443 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
444 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000445 break;
446 }
Rafael Espindola97551272010-11-24 02:19:40 +0000447 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000448 MCELF::SetType(SD, ELF::STT_TLS);
Rafael Espindola97551272010-11-24 02:19:40 +0000449 break;
450 }
451
452 case MCExpr::Unary:
453 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
454 break;
455 }
456}
457
Benjamin Kramer93ded732010-08-27 10:40:51 +0000458void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
Rafael Espindoladedb0452010-12-02 05:44:06 +0000459 this->MCObjectStreamer::EmitInstToFragment(Inst);
460 MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment());
Benjamin Kramer93ded732010-08-27 10:40:51 +0000461
Rafael Espindoladedb0452010-12-02 05:44:06 +0000462 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
463 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
Benjamin Kramer93ded732010-08-27 10:40:51 +0000464}
465
466void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
467 MCDataFragment *DF = getOrCreateDataFragment();
468
469 SmallVector<MCFixup, 4> Fixups;
470 SmallString<256> Code;
471 raw_svector_ostream VecOS(Code);
472 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
473 VecOS.flush();
474
Rafael Espindola97551272010-11-24 02:19:40 +0000475 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
476 fixSymbolsInTLSFixups(Fixups[i].getValue());
477
Benjamin Kramer93ded732010-08-27 10:40:51 +0000478 // Add the fixups and data.
479 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
480 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
481 DF->addFixup(Fixups[i]);
482 }
483 DF->getContents().append(Code.begin(), Code.end());
484}
485
Rafael Espindola99b42372012-01-07 03:13:18 +0000486void MCELFStreamer::FinishImpl() {
Rafael Espindolac25dad82011-05-10 03:14:15 +0000487 EmitFrames(true);
Rafael Espindolac70a1d92010-11-01 17:07:14 +0000488
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000489 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
490 e = LocalCommons.end();
491 i != e; ++i) {
492 MCSymbolData *SD = i->SD;
493 uint64_t Size = i->Size;
494 unsigned ByteAlignment = i->ByteAlignment;
495 const MCSymbol &Symbol = SD->getSymbol();
496 const MCSection &Section = Symbol.getSection();
497
498 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
499 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
500
501 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
502 SD->setFragment(F);
503
504 // Update the maximum alignment of the section if necessary.
505 if (ByteAlignment > SectData.getAlignment())
506 SectData.setAlignment(ByteAlignment);
507 }
508
Rafael Espindola99b42372012-01-07 03:13:18 +0000509 this->MCObjectStreamer::FinishImpl();
Matt Fleming3565a062010-08-16 18:57:57 +0000510}
511
Evan Cheng78c10ee2011-07-25 23:24:55 +0000512MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000513 raw_ostream &OS, MCCodeEmitter *CE,
514 bool RelaxAll, bool NoExecStack) {
Evan Cheng78c10ee2011-07-25 23:24:55 +0000515 MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
Matt Fleming3565a062010-08-16 18:57:57 +0000516 if (RelaxAll)
517 S->getAssembler().setRelaxAll(true);
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000518 if (NoExecStack)
519 S->getAssembler().setNoExecStack(true);
Matt Fleming3565a062010-08-16 18:57:57 +0000520 return S;
521}