blob: 464c136029894c72ca59b0aac1d0cc4b95e4f046 [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ödin24b17c62011-03-03 14:52:12 +000014#include "MCELFStreamer.h"
Jan Sjödin2ddfd952011-02-28 21:45:04 +000015#include "MCELF.h"
Jan Sjödin24b17c62011-03-03 14:52:12 +000016#include "llvm/MC/MCStreamer.h"
Matt Fleming3565a062010-08-16 18:57:57 +000017#include "llvm/MC/MCCodeEmitter.h"
18#include "llvm/MC/MCELFSymbolFlags.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCInst.h"
Matt Fleming3565a062010-08-16 18:57:57 +000021#include "llvm/MC/MCSection.h"
Matt Fleming3565a062010-08-16 18:57:57 +000022#include "llvm/MC/MCSymbol.h"
Rafael Espindola484291c2010-11-01 14:28:48 +000023#include "llvm/MC/MCValue.h"
Matt Fleming3565a062010-08-16 18:57:57 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/ELF.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/raw_ostream.h"
28#include "llvm/Target/TargetAsmBackend.h"
Rafael Espindola89b93722010-12-10 07:39:47 +000029#include "llvm/Target/TargetAsmInfo.h"
Matt Fleming3565a062010-08-16 18:57:57 +000030
31using namespace llvm;
32
Rafael Espindolad80781b2010-09-15 21:48:40 +000033void MCELFStreamer::InitSections() {
34 // This emulates the same behavior of GNU as. This makes it easier
35 // to compare the output as the major sections are in the same order.
36 SetSectionText();
37 SetSectionData();
38 SetSectionBss();
39 SetSectionText();
40}
41
Matt Fleming3565a062010-08-16 18:57:57 +000042void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindolaba210242010-11-28 16:22:59 +000043 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
44
Rafael Espindolaea4afa92010-11-28 17:18:55 +000045 MCObjectStreamer::EmitLabel(Symbol);
Rafael Espindola73ffea42010-09-25 05:42:19 +000046
Rafael Espindolae1a25872010-11-11 19:04:55 +000047 const MCSectionELF &Section =
48 static_cast<const MCSectionELF&>(Symbol->getSection());
Rafael Espindolaea4afa92010-11-28 17:18:55 +000049 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Rafael Espindola1c130262011-01-23 04:43:11 +000050 if (Section.getFlags() & ELF::SHF_TLS)
Jan Sjödin2ddfd952011-02-28 21:45:04 +000051 MCELF::SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +000052}
53
54void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
55 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +000056 case MCAF_SyntaxUnified: return; // no-op here.
57 case MCAF_Code16: return; // no-op here.
Jim Grosbachba219572010-11-05 22:40:09 +000058 case MCAF_Code32: return; // no-op here.
Matt Fleming3565a062010-08-16 18:57:57 +000059 case MCAF_SubsectionsViaSymbols:
60 getAssembler().setSubsectionsViaSymbols(true);
61 return;
62 }
63
64 assert(0 && "invalid assembler flag!");
65}
66
Jim Grosbachce792992010-11-05 22:08:08 +000067void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
68 // FIXME: Anything needed here to flag the function as thumb?
69}
70
Matt Fleming3565a062010-08-16 18:57:57 +000071void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
72 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
73 // MCObjectStreamer.
74 // FIXME: Lift context changes into super class.
75 getAssembler().getOrCreateSymbolData(*Symbol);
76 Symbol->setVariableValue(AddValueSymbols(Value));
77}
78
Rafael Espindola7768a9d2011-02-16 01:08:29 +000079void MCELFStreamer::ChangeSection(const MCSection *Section) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +000080 const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
81 if (Grp)
82 getAssembler().getOrCreateSymbolData(*Grp);
Rafael Espindola7768a9d2011-02-16 01:08:29 +000083 this->MCObjectStreamer::ChangeSection(Section);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +000084}
85
Rafael Espindola484291c2010-11-01 14:28:48 +000086void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
87 getAssembler().getOrCreateSymbolData(*Symbol);
88 MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
89 AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +000090 const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext());
Rafael Espindola484291c2010-11-01 14:28:48 +000091 Alias->setVariableValue(Value);
92}
93
Matt Fleming3565a062010-08-16 18:57:57 +000094void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
95 MCSymbolAttr Attribute) {
96 // Indirect symbols are handled differently, to match how 'as' handles
97 // them. This makes writing matching .o files easier.
98 if (Attribute == MCSA_IndirectSymbol) {
99 // Note that we intentionally cannot use the symbol data here; this is
100 // important for matching the string table that 'as' generates.
101 IndirectSymbolData ISD;
102 ISD.Symbol = Symbol;
103 ISD.SectionData = getCurrentSectionData();
104 getAssembler().getIndirectSymbols().push_back(ISD);
105 return;
106 }
107
108 // Adding a symbol attribute always introduces the symbol, note that an
109 // important side effect of calling getOrCreateSymbolData here is to register
110 // the symbol with the assembler.
111 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
112
113 // The implementation of symbol attributes is designed to match 'as', but it
114 // leaves much to desired. It doesn't really make sense to arbitrarily add and
115 // remove flags, but 'as' allows this (in particular, see .desc).
116 //
117 // In the future it might be worth trying to make these operations more well
118 // defined.
119 switch (Attribute) {
120 case MCSA_LazyReference:
121 case MCSA_Reference:
122 case MCSA_NoDeadStrip:
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000123 case MCSA_SymbolResolver:
Matt Fleming3565a062010-08-16 18:57:57 +0000124 case MCSA_PrivateExtern:
Matt Fleming3565a062010-08-16 18:57:57 +0000125 case MCSA_WeakDefinition:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000126 case MCSA_WeakDefAutoPrivate:
Matt Fleming3565a062010-08-16 18:57:57 +0000127 case MCSA_Invalid:
128 case MCSA_ELF_TypeIndFunction:
129 case MCSA_IndirectSymbol:
130 assert(0 && "Invalid symbol attribute for ELF!");
131 break;
132
Rafael Espindola7e528a12010-11-14 01:34:31 +0000133 case MCSA_ELF_TypeGnuUniqueObject:
134 // Ignore for now.
135 break;
136
Matt Fleming3565a062010-08-16 18:57:57 +0000137 case MCSA_Global:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000138 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000139 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000140 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000141 break;
142
Benjamin Kramer230c2742010-09-02 17:18:32 +0000143 case MCSA_WeakReference:
Matt Fleming3565a062010-08-16 18:57:57 +0000144 case MCSA_Weak:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000145 MCELF::SetBinding(SD, ELF::STB_WEAK);
Rafael Espindola3223f192010-10-06 16:47:31 +0000146 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000147 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000148 break;
149
150 case MCSA_Local:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000151 MCELF::SetBinding(SD, ELF::STB_LOCAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000152 SD.setExternal(false);
153 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000154 break;
155
156 case MCSA_ELF_TypeFunction:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000157 MCELF::SetType(SD, ELF::STT_FUNC);
Matt Fleming3565a062010-08-16 18:57:57 +0000158 break;
159
160 case MCSA_ELF_TypeObject:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000161 MCELF::SetType(SD, ELF::STT_OBJECT);
Matt Fleming3565a062010-08-16 18:57:57 +0000162 break;
163
164 case MCSA_ELF_TypeTLS:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000165 MCELF::SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000166 break;
167
168 case MCSA_ELF_TypeCommon:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000169 MCELF::SetType(SD, ELF::STT_COMMON);
Matt Fleming3565a062010-08-16 18:57:57 +0000170 break;
171
172 case MCSA_ELF_TypeNoType:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000173 MCELF::SetType(SD, ELF::STT_NOTYPE);
Matt Fleming3565a062010-08-16 18:57:57 +0000174 break;
175
176 case MCSA_Protected:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000177 MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
Matt Fleming3565a062010-08-16 18:57:57 +0000178 break;
179
180 case MCSA_Hidden:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000181 MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
Matt Fleming3565a062010-08-16 18:57:57 +0000182 break;
183
184 case MCSA_Internal:
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000185 MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000186 break;
187 }
188}
189
190void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
191 unsigned ByteAlignment) {
192 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
193
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000194 if (!BindingExplicitlySet.count(Symbol)) {
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000195 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000196 SD.setExternal(true);
197 }
198
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000199 MCELF::SetType(SD, ELF::STT_OBJECT);
Rafael Espindola55d02f32010-11-14 21:11:16 +0000200
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000201 if (MCELF::GetBinding(SD) == ELF_STB_Local) {
Matt Fleming3565a062010-08-16 18:57:57 +0000202 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
Rafael Espindolac85dca62011-01-23 04:28:49 +0000203 ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000204 ELF::SHF_WRITE |
205 ELF::SHF_ALLOC,
Matt Fleming3565a062010-08-16 18:57:57 +0000206 SectionKind::getBSS());
Matt Fleming3565a062010-08-16 18:57:57 +0000207 Symbol->setSection(*Section);
Rafael Espindola19635722010-09-22 17:43:04 +0000208
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000209 struct LocalCommon L = {&SD, Size, ByteAlignment};
210 LocalCommons.push_back(L);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000211 } else {
212 SD.setCommon(Size, ByteAlignment);
Matt Fleming3565a062010-08-16 18:57:57 +0000213 }
214
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000215 SD.setSize(MCConstantExpr::Create(Size, getContext()));
Matt Fleming3565a062010-08-16 18:57:57 +0000216}
217
Jason W Kimf13743b2010-12-16 03:12:17 +0000218void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
219 // FIXME: Should this be caught and done earlier?
220 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000221 MCELF::SetBinding(SD, ELF::STB_LOCAL);
Jason W Kimf13743b2010-12-16 03:12:17 +0000222 SD.setExternal(false);
223 BindingExplicitlySet.insert(Symbol);
224 // FIXME: ByteAlignment is not needed here, but is required.
225 EmitCommonSymbol(Symbol, Size, 1);
226}
227
Matt Fleming3565a062010-08-16 18:57:57 +0000228void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
229 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
230 // MCObjectStreamer.
231 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
232}
233
Matt Fleming3565a062010-08-16 18:57:57 +0000234void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
235 int64_t Value, unsigned ValueSize,
236 unsigned MaxBytesToEmit) {
237 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
238 // MCObjectStreamer.
239 if (MaxBytesToEmit == 0)
240 MaxBytesToEmit = ByteAlignment;
241 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
242 getCurrentSectionData());
243
244 // Update the maximum alignment on the current section if necessary.
245 if (ByteAlignment > getCurrentSectionData()->getAlignment())
246 getCurrentSectionData()->setAlignment(ByteAlignment);
247}
248
249void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
250 unsigned MaxBytesToEmit) {
251 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
252 // MCObjectStreamer.
253 if (MaxBytesToEmit == 0)
254 MaxBytesToEmit = ByteAlignment;
255 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
256 getCurrentSectionData());
257 F->setEmitNops(true);
258
259 // Update the maximum alignment on the current section if necessary.
260 if (ByteAlignment > getCurrentSectionData()->getAlignment())
261 getCurrentSectionData()->setAlignment(ByteAlignment);
262}
263
Matt Fleming3565a062010-08-16 18:57:57 +0000264// Add a symbol for the file name of this module. This is the second
265// entry in the module's symbol table (the first being the null symbol).
266void MCELFStreamer::EmitFileDirective(StringRef Filename) {
267 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000268 Symbol->setSection(*getCurrentSection());
Matt Fleming3565a062010-08-16 18:57:57 +0000269 Symbol->setAbsolute();
270
271 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
272
273 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
274}
275
Rafael Espindola97551272010-11-24 02:19:40 +0000276void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
277 switch (expr->getKind()) {
278 case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
279 case MCExpr::Constant:
280 break;
281
282 case MCExpr::Binary: {
283 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
284 fixSymbolsInTLSFixups(be->getLHS());
285 fixSymbolsInTLSFixups(be->getRHS());
286 break;
287 }
288
289 case MCExpr::SymbolRef: {
290 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000291 switch (symRef.getKind()) {
292 default:
Rafael Espindola97551272010-11-24 02:19:40 +0000293 return;
Rafael Espindolabf8209d2010-11-24 18:51:21 +0000294 case MCSymbolRefExpr::VK_NTPOFF:
295 case MCSymbolRefExpr::VK_GOTNTPOFF:
296 case MCSymbolRefExpr::VK_TLSGD:
297 case MCSymbolRefExpr::VK_TLSLDM:
298 case MCSymbolRefExpr::VK_TPOFF:
299 case MCSymbolRefExpr::VK_DTPOFF:
300 case MCSymbolRefExpr::VK_GOTTPOFF:
301 case MCSymbolRefExpr::VK_TLSLD:
302 case MCSymbolRefExpr::VK_ARM_TLSGD:
303 break;
304 }
Rafael Espindola97551272010-11-24 02:19:40 +0000305 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000306 MCELF::SetType(SD, ELF::STT_TLS);
Rafael Espindola97551272010-11-24 02:19:40 +0000307 break;
308 }
309
310 case MCExpr::Unary:
311 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
312 break;
313 }
314}
315
Benjamin Kramer93ded732010-08-27 10:40:51 +0000316void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
Rafael Espindoladedb0452010-12-02 05:44:06 +0000317 this->MCObjectStreamer::EmitInstToFragment(Inst);
318 MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment());
Benjamin Kramer93ded732010-08-27 10:40:51 +0000319
Rafael Espindoladedb0452010-12-02 05:44:06 +0000320 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
321 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
Benjamin Kramer93ded732010-08-27 10:40:51 +0000322}
323
324void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
325 MCDataFragment *DF = getOrCreateDataFragment();
326
327 SmallVector<MCFixup, 4> Fixups;
328 SmallString<256> Code;
329 raw_svector_ostream VecOS(Code);
330 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
331 VecOS.flush();
332
Rafael Espindola97551272010-11-24 02:19:40 +0000333 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
334 fixSymbolsInTLSFixups(Fixups[i].getValue());
335
Benjamin Kramer93ded732010-08-27 10:40:51 +0000336 // Add the fixups and data.
337 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
338 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
339 DF->addFixup(Fixups[i]);
340 }
341 DF->getContents().append(Code.begin(), Code.end());
342}
343
Matt Fleming3565a062010-08-16 18:57:57 +0000344void MCELFStreamer::Finish() {
Rafael Espindolad7c8cca2010-12-26 20:20:31 +0000345 if (getNumFrameInfos())
Rafael Espindola89b93722010-12-10 07:39:47 +0000346 MCDwarfFrameEmitter::Emit(*this);
Rafael Espindolac70a1d92010-11-01 17:07:14 +0000347
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000348 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
349 e = LocalCommons.end();
350 i != e; ++i) {
351 MCSymbolData *SD = i->SD;
352 uint64_t Size = i->Size;
353 unsigned ByteAlignment = i->ByteAlignment;
354 const MCSymbol &Symbol = SD->getSymbol();
355 const MCSection &Section = Symbol.getSection();
356
357 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
358 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
359
360 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
361 SD->setFragment(F);
362
363 // Update the maximum alignment of the section if necessary.
364 if (ByteAlignment > SectData.getAlignment())
365 SectData.setAlignment(ByteAlignment);
366 }
367
Rafael Espindola73ffea42010-09-25 05:42:19 +0000368 this->MCObjectStreamer::Finish();
Matt Fleming3565a062010-08-16 18:57:57 +0000369}
370
371MCStreamer *llvm::createELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000372 raw_ostream &OS, MCCodeEmitter *CE,
373 bool RelaxAll, bool NoExecStack) {
Matt Fleming3565a062010-08-16 18:57:57 +0000374 MCELFStreamer *S = new MCELFStreamer(Context, TAB, OS, CE);
375 if (RelaxAll)
376 S->getAssembler().setRelaxAll(true);
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000377 if (NoExecStack)
378 S->getAssembler().setNoExecStack(true);
Matt Fleming3565a062010-08-16 18:57:57 +0000379 return S;
380}