blob: 465f9ce79cbd2815d7e3aa9662145e0758b99020 [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"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/ELF.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
31#include "llvm/Target/TargetAsmBackend.h"
32
33using namespace llvm;
34
35namespace {
36
37class MCELFStreamer : public MCObjectStreamer {
Benjamin Kramer93ded732010-08-27 10:40:51 +000038 void EmitInstToFragment(const MCInst &Inst);
39 void EmitInstToData(const MCInst &Inst);
Matt Fleming3565a062010-08-16 18:57:57 +000040public:
41 MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
42 raw_ostream &OS, MCCodeEmitter *Emitter)
Rafael Espindola59ff3c92010-09-22 22:27:05 +000043 : MCObjectStreamer(Context, TAB, OS, Emitter, false) {}
Matt Fleming3565a062010-08-16 18:57:57 +000044
45 ~MCELFStreamer() {}
46
47 /// @name MCStreamer Interface
48 /// @{
49
Rafael Espindolad80781b2010-09-15 21:48:40 +000050 virtual void InitSections();
Matt Fleming3565a062010-08-16 18:57:57 +000051 virtual void EmitLabel(MCSymbol *Symbol);
52 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
53 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
54 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
55 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
56 assert(0 && "ELF doesn't support this directive");
57 }
58 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
59 unsigned ByteAlignment);
60 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
61 assert(0 && "ELF doesn't support this directive");
62 }
63
64 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
65 assert(0 && "ELF doesn't support this directive");
66 }
67
68 virtual void EmitCOFFSymbolType(int Type) {
69 assert(0 && "ELF doesn't support this directive");
70 }
71
72 virtual void EndCOFFSymbolDef() {
73 assert(0 && "ELF doesn't support this directive");
74 }
75
76 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
77 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
78 SD.setSize(Value);
79 }
80
81 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
82 assert(0 && "ELF doesn't support this directive");
83 }
84 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
85 unsigned Size = 0, unsigned ByteAlignment = 0) {
86 assert(0 && "ELF doesn't support this directive");
87 }
88 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
89 uint64_t Size, unsigned ByteAlignment = 0) {
90 assert(0 && "ELF doesn't support this directive");
91 }
92 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
93 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
94 virtual void EmitGPRel32Value(const MCExpr *Value) {
95 assert(0 && "ELF doesn't support this directive");
96 }
97 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
98 unsigned ValueSize = 1,
99 unsigned MaxBytesToEmit = 0);
100 virtual void EmitCodeAlignment(unsigned ByteAlignment,
101 unsigned MaxBytesToEmit = 0);
102 virtual void EmitValueToOffset(const MCExpr *Offset,
103 unsigned char Value = 0);
104
105 virtual void EmitFileDirective(StringRef Filename);
106 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
107 DEBUG(dbgs() << "FIXME: MCELFStreamer:EmitDwarfFileDirective not implemented\n");
108 }
109
110 virtual void EmitInstruction(const MCInst &Inst);
111 virtual void Finish();
112
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000113private:
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000114 struct LocalCommon {
115 MCSymbolData *SD;
116 uint64_t Size;
117 unsigned ByteAlignment;
118 };
119 std::vector<LocalCommon> LocalCommons;
120
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000121 SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
Matt Fleming3565a062010-08-16 18:57:57 +0000122 /// @}
Rafael Espindolad80781b2010-09-15 21:48:40 +0000123 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", MCSectionELF::SHT_PROGBITS,
130 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
131 SectionKind::getDataRel());
132 EmitCodeAlignment(4, 0);
133 }
134 void SetSectionText() {
135 SetSection(".text", MCSectionELF::SHT_PROGBITS,
136 MCSectionELF::SHF_EXECINSTR |
137 MCSectionELF::SHF_ALLOC, SectionKind::getText());
138 EmitCodeAlignment(4, 0);
139 }
140 void SetSectionBss() {
141 SetSection(".bss", MCSectionELF::SHT_NOBITS,
142 MCSectionELF::SHF_WRITE |
143 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
144 EmitCodeAlignment(4, 0);
145 }
Matt Fleming3565a062010-08-16 18:57:57 +0000146};
147
148} // end anonymous namespace.
149
Rafael Espindolad80781b2010-09-15 21:48:40 +0000150void MCELFStreamer::InitSections() {
151 // This emulates the same behavior of GNU as. This makes it easier
152 // to compare the output as the major sections are in the same order.
153 SetSectionText();
154 SetSectionData();
155 SetSectionBss();
156 SetSectionText();
157}
158
Rafael Espindola73ffea42010-09-25 05:42:19 +0000159static bool isSymbolLinkerVisible(const MCAssembler &Asm,
160 const MCSymbolData &Data) {
161 const MCSymbol &Symbol = Data.getSymbol();
162 // Absolute temporary labels are never visible.
163 if (!Symbol.isInSection())
164 return false;
165
166 if (Asm.getBackend().doesSectionRequireSymbols(Symbol.getSection()))
167 return true;
168
169 if (!Data.isExternal())
170 return false;
171
172 return Asm.isSymbolLinkerVisible(Symbol);
173}
174
Matt Fleming3565a062010-08-16 18:57:57 +0000175void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
176 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
177
Rafael Espindola73ffea42010-09-25 05:42:19 +0000178 Symbol->setSection(*CurSection);
179
180 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
181
182 // We have to create a new fragment if this is an atom defining symbol,
183 // fragments cannot span atoms.
184 if (isSymbolLinkerVisible(getAssembler(), SD))
185 new MCDataFragment(getCurrentSectionData());
186
Matt Fleming3565a062010-08-16 18:57:57 +0000187 // FIXME: This is wasteful, we don't necessarily need to create a data
188 // fragment. Instead, we should mark the symbol as pointing into the data
189 // fragment if it exists, otherwise we should just queue the label and set its
190 // fragment pointer when we emit the next fragment.
191 MCDataFragment *F = getOrCreateDataFragment();
Rafael Espindola73ffea42010-09-25 05:42:19 +0000192
Matt Fleming3565a062010-08-16 18:57:57 +0000193 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
194 SD.setFragment(F);
195 SD.setOffset(F->getContents().size());
Matt Fleming3565a062010-08-16 18:57:57 +0000196}
197
198void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
199 switch (Flag) {
200 case MCAF_SubsectionsViaSymbols:
201 getAssembler().setSubsectionsViaSymbols(true);
202 return;
203 }
204
205 assert(0 && "invalid assembler flag!");
206}
207
208void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
209 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
210 // MCObjectStreamer.
211 // FIXME: Lift context changes into super class.
212 getAssembler().getOrCreateSymbolData(*Symbol);
213 Symbol->setVariableValue(AddValueSymbols(Value));
214}
215
Rafael Espindolaf7133842010-09-13 17:39:45 +0000216static void SetBinding(MCSymbolData &SD, unsigned Binding) {
217 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
218 Binding == ELF::STB_WEAK);
219 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
220 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
221}
222
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000223static unsigned GetBinding(const MCSymbolData &SD) {
224 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
225 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
226 Binding == ELF::STB_WEAK);
227 return Binding;
228}
229
Rafael Espindolaf7133842010-09-13 17:39:45 +0000230static void SetType(MCSymbolData &SD, unsigned Type) {
231 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
232 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
233 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
234 Type == ELF::STT_TLS);
235
236 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STT_Shift);
237 SD.setFlags(OtherFlags | (Type << ELF_STT_Shift));
238}
239
240static void SetVisibility(MCSymbolData &SD, unsigned Visibility) {
241 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
242 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
243
244 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STV_Shift);
245 SD.setFlags(OtherFlags | (Visibility << ELF_STV_Shift));
246}
247
Matt Fleming3565a062010-08-16 18:57:57 +0000248void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
249 MCSymbolAttr Attribute) {
250 // Indirect symbols are handled differently, to match how 'as' handles
251 // them. This makes writing matching .o files easier.
252 if (Attribute == MCSA_IndirectSymbol) {
253 // Note that we intentionally cannot use the symbol data here; this is
254 // important for matching the string table that 'as' generates.
255 IndirectSymbolData ISD;
256 ISD.Symbol = Symbol;
257 ISD.SectionData = getCurrentSectionData();
258 getAssembler().getIndirectSymbols().push_back(ISD);
259 return;
260 }
261
262 // Adding a symbol attribute always introduces the symbol, note that an
263 // important side effect of calling getOrCreateSymbolData here is to register
264 // the symbol with the assembler.
265 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
266
267 // The implementation of symbol attributes is designed to match 'as', but it
268 // leaves much to desired. It doesn't really make sense to arbitrarily add and
269 // remove flags, but 'as' allows this (in particular, see .desc).
270 //
271 // In the future it might be worth trying to make these operations more well
272 // defined.
273 switch (Attribute) {
274 case MCSA_LazyReference:
275 case MCSA_Reference:
276 case MCSA_NoDeadStrip:
277 case MCSA_PrivateExtern:
Matt Fleming3565a062010-08-16 18:57:57 +0000278 case MCSA_WeakDefinition:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000279 case MCSA_WeakDefAutoPrivate:
Matt Fleming3565a062010-08-16 18:57:57 +0000280 case MCSA_Invalid:
281 case MCSA_ELF_TypeIndFunction:
282 case MCSA_IndirectSymbol:
283 assert(0 && "Invalid symbol attribute for ELF!");
284 break;
285
286 case MCSA_Global:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000287 SetBinding(SD, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000288 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000289 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000290 break;
291
Benjamin Kramer230c2742010-09-02 17:18:32 +0000292 case MCSA_WeakReference:
Matt Fleming3565a062010-08-16 18:57:57 +0000293 case MCSA_Weak:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000294 SetBinding(SD, ELF::STB_WEAK);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000295 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000296 break;
297
298 case MCSA_Local:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000299 SetBinding(SD, ELF::STB_LOCAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000300 SD.setExternal(false);
301 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000302 break;
303
304 case MCSA_ELF_TypeFunction:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000305 SetType(SD, ELF::STT_FUNC);
Matt Fleming3565a062010-08-16 18:57:57 +0000306 break;
307
308 case MCSA_ELF_TypeObject:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000309 SetType(SD, ELF::STT_OBJECT);
Matt Fleming3565a062010-08-16 18:57:57 +0000310 break;
311
312 case MCSA_ELF_TypeTLS:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000313 SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000314 break;
315
316 case MCSA_ELF_TypeCommon:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000317 SetType(SD, ELF::STT_COMMON);
Matt Fleming3565a062010-08-16 18:57:57 +0000318 break;
319
320 case MCSA_ELF_TypeNoType:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000321 SetType(SD, ELF::STT_NOTYPE);
Matt Fleming3565a062010-08-16 18:57:57 +0000322 break;
323
324 case MCSA_Protected:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000325 SetVisibility(SD, ELF::STV_PROTECTED);
Matt Fleming3565a062010-08-16 18:57:57 +0000326 break;
327
328 case MCSA_Hidden:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000329 SetVisibility(SD, ELF::STV_HIDDEN);
Matt Fleming3565a062010-08-16 18:57:57 +0000330 break;
331
332 case MCSA_Internal:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000333 SetVisibility(SD, ELF::STV_INTERNAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000334 break;
335 }
336}
337
338void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
339 unsigned ByteAlignment) {
340 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
341
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000342 if (!BindingExplicitlySet.count(Symbol)) {
343 SetBinding(SD, ELF::STB_GLOBAL);
344 SD.setExternal(true);
345 }
346
347 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
364void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
365 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
366 // MCObjectStreamer.
367 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
368}
369
370void MCELFStreamer::EmitValue(const MCExpr *Value, unsigned Size,
371 unsigned AddrSpace) {
372 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
373 // MCObjectStreamer.
374 MCDataFragment *DF = getOrCreateDataFragment();
375
376 // Avoid fixups when possible.
377 int64_t AbsValue;
378 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
379 // FIXME: Endianness assumption.
380 for (unsigned i = 0; i != Size; ++i)
381 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
382 } else {
383 DF->addFixup(MCFixup::Create(DF->getContents().size(), AddValueSymbols(Value),
384 MCFixup::getKindForSize(Size)));
385 DF->getContents().resize(DF->getContents().size() + Size, 0);
386 }
387}
388
389void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
390 int64_t Value, unsigned ValueSize,
391 unsigned MaxBytesToEmit) {
392 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
393 // MCObjectStreamer.
394 if (MaxBytesToEmit == 0)
395 MaxBytesToEmit = ByteAlignment;
396 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
397 getCurrentSectionData());
398
399 // Update the maximum alignment on the current section if necessary.
400 if (ByteAlignment > getCurrentSectionData()->getAlignment())
401 getCurrentSectionData()->setAlignment(ByteAlignment);
402}
403
404void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
405 unsigned MaxBytesToEmit) {
406 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
407 // MCObjectStreamer.
408 if (MaxBytesToEmit == 0)
409 MaxBytesToEmit = ByteAlignment;
410 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
411 getCurrentSectionData());
412 F->setEmitNops(true);
413
414 // Update the maximum alignment on the current section if necessary.
415 if (ByteAlignment > getCurrentSectionData()->getAlignment())
416 getCurrentSectionData()->setAlignment(ByteAlignment);
417}
418
419void MCELFStreamer::EmitValueToOffset(const MCExpr *Offset,
420 unsigned char Value) {
421 // TODO: This is exactly the same as MCMachOStreamer. Consider merging into
422 // MCObjectStreamer.
423 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
424}
425
426// Add a symbol for the file name of this module. This is the second
427// entry in the module's symbol table (the first being the null symbol).
428void MCELFStreamer::EmitFileDirective(StringRef Filename) {
429 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
430 Symbol->setSection(*CurSection);
431 Symbol->setAbsolute();
432
433 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
434
435 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
436}
437
Benjamin Kramer93ded732010-08-27 10:40:51 +0000438void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
439 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
440
441 // Add the fixups and data.
442 //
443 // FIXME: Revisit this design decision when relaxation is done, we may be
444 // able to get away with not storing any extra data in the MCInst.
445 SmallVector<MCFixup, 4> Fixups;
446 SmallString<256> Code;
447 raw_svector_ostream VecOS(Code);
448 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
449 VecOS.flush();
450
451 IF->getCode() = Code;
452 IF->getFixups() = Fixups;
453}
454
455void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
456 MCDataFragment *DF = getOrCreateDataFragment();
457
458 SmallVector<MCFixup, 4> Fixups;
459 SmallString<256> Code;
460 raw_svector_ostream VecOS(Code);
461 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
462 VecOS.flush();
463
464 // Add the fixups and data.
465 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
466 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
467 DF->addFixup(Fixups[i]);
468 }
469 DF->getContents().append(Code.begin(), Code.end());
470}
471
Matt Fleming3565a062010-08-16 18:57:57 +0000472void MCELFStreamer::EmitInstruction(const MCInst &Inst) {
473 // Scan for values.
474 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
475 if (Inst.getOperand(i).isExpr())
476 AddValueSymbols(Inst.getOperand(i).getExpr());
477
478 getCurrentSectionData()->setHasInstructions(true);
479
Benjamin Kramer93ded732010-08-27 10:40:51 +0000480 // If this instruction doesn't need relaxation, just emit it as data.
481 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
482 EmitInstToData(Inst);
Matt Fleming3565a062010-08-16 18:57:57 +0000483 return;
484 }
485
Benjamin Kramer93ded732010-08-27 10:40:51 +0000486 // Otherwise, if we are relaxing everything, relax the instruction as much as
487 // possible and emit it as data.
488 if (getAssembler().getRelaxAll()) {
489 MCInst Relaxed;
490 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
491 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
492 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
493 EmitInstToData(Relaxed);
494 return;
Matt Fleming3565a062010-08-16 18:57:57 +0000495 }
Benjamin Kramer93ded732010-08-27 10:40:51 +0000496
497 // Otherwise emit to a separate fragment.
498 EmitInstToFragment(Inst);
Matt Fleming3565a062010-08-16 18:57:57 +0000499}
500
501void MCELFStreamer::Finish() {
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000502 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
503 e = LocalCommons.end();
504 i != e; ++i) {
505 MCSymbolData *SD = i->SD;
506 uint64_t Size = i->Size;
507 unsigned ByteAlignment = i->ByteAlignment;
508 const MCSymbol &Symbol = SD->getSymbol();
509 const MCSection &Section = Symbol.getSection();
510
511 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
512 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
513
514 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
515 SD->setFragment(F);
516
517 // Update the maximum alignment of the section if necessary.
518 if (ByteAlignment > SectData.getAlignment())
519 SectData.setAlignment(ByteAlignment);
520 }
521
Rafael Espindola73ffea42010-09-25 05:42:19 +0000522 // FIXME: We create more atoms than it is necessary. Some relocations to
523 // merge sections can be implemented with section address + offset,
524 // figure out which ones and why.
525
526 // First, scan the symbol table to build a lookup table from fragments to
527 // defining symbols.
528 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
529 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
530 ie = getAssembler().symbol_end(); it != ie; ++it) {
531 if (isSymbolLinkerVisible(getAssembler(), *it) &&
532 it->getFragment()) {
533 // An atom defining symbol should never be internal to a fragment.
534 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
535 DefiningSymbolMap[it->getFragment()] = it;
536 }
537 }
538
539 // Set the fragment atom associations by tracking the last seen atom defining
540 // symbol.
541 for (MCAssembler::iterator it = getAssembler().begin(),
542 ie = getAssembler().end(); it != ie; ++it) {
543 MCSymbolData *CurrentAtom = 0;
544 for (MCSectionData::iterator it2 = it->begin(),
545 ie2 = it->end(); it2 != ie2; ++it2) {
546 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
547 CurrentAtom = SD;
548 it2->setAtom(CurrentAtom);
549 }
550 }
551
552 this->MCObjectStreamer::Finish();
Matt Fleming3565a062010-08-16 18:57:57 +0000553}
554
555MCStreamer *llvm::createELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
556 raw_ostream &OS, MCCodeEmitter *CE,
557 bool RelaxAll) {
558 MCELFStreamer *S = new MCELFStreamer(Context, TAB, OS, CE);
559 if (RelaxAll)
560 S->getAssembler().setRelaxAll(true);
561 return S;
562}