blob: b5a55a75304d7d329b27b7f6bc8ffb01b1a6ae25 [file] [log] [blame]
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001//===- lib/MC/MCMachOStreamer.cpp - Mach-O 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#include "llvm/MC/MCStreamer.h"
11
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000012#include "llvm/MC/MCAssembler.h"
13#include "llvm/MC/MCContext.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000014#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000016#include "llvm/MC/MCInst.h"
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000017#include "llvm/MC/MCObjectStreamer.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000018#include "llvm/MC/MCSection.h"
19#include "llvm/MC/MCSymbol.h"
Kevin Enderbya6eeb6e2010-05-07 21:44:23 +000020#include "llvm/MC/MCMachOSymbolFlags.h"
Kevin Enderbyb07ce602010-08-09 22:52:14 +000021#include "llvm/MC/MCSectionMachO.h"
22#include "llvm/MC/MCDwarf.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000023#include "llvm/Support/Dwarf.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000024#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar4fac7492009-08-27 08:17:51 +000025#include "llvm/Support/raw_ostream.h"
Daniel Dunbard8036fb2010-03-23 05:09:03 +000026#include "llvm/Target/TargetAsmBackend.h"
Rafael Espindola89b93722010-12-10 07:39:47 +000027#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbard8036fb2010-03-23 05:09:03 +000028
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000029using namespace llvm;
30
31namespace {
32
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000033class MCMachOStreamer : public MCObjectStreamer {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +000034private:
Rafael Espindolaf89671d2010-11-01 16:27:31 +000035 virtual void EmitInstToData(const MCInst &Inst);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000036
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000037public:
Daniel Dunbar1f3e4452010-03-11 01:34:27 +000038 MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +000039 raw_ostream &OS, MCCodeEmitter *Emitter)
Rafael Espindola85f2ecc2010-12-07 00:27:36 +000040 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
Daniel Dunbarac2884a2010-03-25 22:49:09 +000041
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000042 /// @name MCStreamer Interface
43 /// @{
44
Rafael Espindolad80781b2010-09-15 21:48:40 +000045 virtual void InitSections();
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000046 virtual void EmitLabel(MCSymbol *Symbol);
Rafael Espindola8bca4102011-04-28 12:50:37 +000047 virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
48 MCSymbol *EHSymbol);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000049 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +000050 virtual void EmitThumbFunc(MCSymbol *Func);
Daniel Dunbar821e3332009-08-31 08:09:28 +000051 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Chris Lattnera5ad93a2010-01-23 06:39:22 +000052 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000053 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Chris Lattner9eb158d2010-01-23 07:47:02 +000054 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000055 unsigned ByteAlignment);
Chris Lattnerb54b9dd2010-05-08 19:54:22 +000056 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
57 assert(0 && "macho doesn't support this directive");
58 }
59 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
60 assert(0 && "macho doesn't support this directive");
61 }
62 virtual void EmitCOFFSymbolType(int Type) {
63 assert(0 && "macho doesn't support this directive");
64 }
65 virtual void EndCOFFSymbolDef() {
66 assert(0 && "macho doesn't support this directive");
67 }
Chris Lattner99328ad2010-01-25 07:52:13 +000068 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
69 assert(0 && "macho doesn't support this directive");
70 }
Chris Lattner9eb158d2010-01-23 07:47:02 +000071 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
72 assert(0 && "macho doesn't support this directive");
73 }
Daniel Dunbar8751b942009-08-28 05:48:22 +000074 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000075 unsigned Size = 0, unsigned ByteAlignment = 0);
Eric Christopher011c3f12010-05-18 21:26:41 +000076 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
77 uint64_t Size, unsigned ByteAlignment = 0);
Chris Lattneraaec2052010-01-19 19:46:13 +000078 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000079 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
80 unsigned ValueSize = 1,
81 unsigned MaxBytesToEmit = 0);
Kevin Enderby6e720482010-02-23 18:26:34 +000082 virtual void EmitCodeAlignment(unsigned ByteAlignment,
83 unsigned MaxBytesToEmit = 0);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000084
Chris Lattnera6594fc2010-01-25 18:58:59 +000085 virtual void EmitFileDirective(StringRef Filename) {
Daniel Dunbar96749672010-07-19 20:44:20 +000086 // FIXME: Just ignore the .file; it isn't important enough to fail the
87 // entire assembly.
88
89 //report_fatal_error("unsupported directive: '.file'");
Chris Lattnera6594fc2010-01-25 18:58:59 +000090 }
Daniel Dunbar2ac0c452010-05-26 20:37:00 +000091
Daniel Dunbara86de102010-06-16 20:04:32 +000092 virtual void Finish();
93
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000094 /// @}
95};
96
97} // end anonymous namespace.
98
Rafael Espindolad80781b2010-09-15 21:48:40 +000099void MCMachOStreamer::InitSections() {
100 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
101 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
102 0, SectionKind::getText()));
103
104}
105
Rafael Espindola8bca4102011-04-28 12:50:37 +0000106void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
107 MCSymbol *EHSymbol) {
108 MCSymbolData &SD =
109 getAssembler().getOrCreateSymbolData(*Symbol);
110 if (SD.isExternal())
111 EmitSymbolAttribute(EHSymbol, MCSA_Global);
112 if (SD.getFlags() & SF_WeakDefinition)
113 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
Rafael Espindola97f7f4e2011-04-30 16:22:46 +0000114 if (SD.isPrivateExtern())
115 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
Rafael Espindola8bca4102011-04-28 12:50:37 +0000116}
117
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000118void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
Rafael Espindolaba210242010-11-28 16:22:59 +0000119 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Rafael Espindolaba210242010-11-28 16:22:59 +0000120
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000121 // isSymbolLinkerVisible uses the section.
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000122 Symbol->setSection(*getCurrentSection());
Daniel Dunbara86de102010-06-16 20:04:32 +0000123 // We have to create a new fragment if this is an atom defining symbol,
124 // fragments cannot span atoms.
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000125 if (getAssembler().isSymbolLinkerVisible(*Symbol))
Daniel Dunbara86de102010-06-16 20:04:32 +0000126 new MCDataFragment(getCurrentSectionData());
Daniel Dunbar071f73d2010-05-10 22:45:09 +0000127
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000128 MCObjectStreamer::EmitLabel(Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000129
Rafael Espindolaea4afa92010-11-28 17:18:55 +0000130 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000131 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
132 // to clear the weak reference and weak definition bits too, but the
133 // implementation was buggy. For now we just try to match 'as', for
134 // diffability.
135 //
136 // FIXME: Cleanup this code, these bits should be emitted based on semantic
137 // properties, not on the order of definition, etc.
138 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000139}
140
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000141void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
Jim Grosbach5be6d2a2010-12-08 01:16:55 +0000142 // Let the target do whatever target specific stuff it needs to do.
143 getAssembler().getBackend().HandleAssemblerFlag(Flag);
144 // Do any generic stuff we need to do.
Daniel Dunbar6009db42009-08-26 21:22:22 +0000145 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000146 case MCAF_SyntaxUnified: return; // no-op here.
147 case MCAF_Code16: return; // no-op here.
Jim Grosbachba219572010-11-05 22:40:09 +0000148 case MCAF_Code32: return; // no-op here.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000149 case MCAF_SubsectionsViaSymbols:
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000150 getAssembler().setSubsectionsViaSymbols(true);
Daniel Dunbar8c3eaf42009-08-28 07:08:47 +0000151 return;
Chandler Carruth5c0d4722010-09-30 10:59:51 +0000152 default:
153 llvm_unreachable("invalid assembler flag!");
Daniel Dunbar6009db42009-08-26 21:22:22 +0000154 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000155}
156
Daniel Dunbarb2624ed2010-12-29 14:14:06 +0000157void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
Jim Grosbachce792992010-11-05 22:08:08 +0000158 // FIXME: Flag the function ISA as thumb with DW_AT_APPLE_isa.
Jim Grosbach47500202010-12-14 18:46:57 +0000159
160 // Remember that the function is a thumb function. Fixup and relocation
161 // values will need adjusted.
Daniel Dunbarb2624ed2010-12-29 14:14:06 +0000162 getAssembler().setIsThumbFunc(Symbol);
163
164 // Mark the thumb bit on the symbol.
165 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
166 SD.setFlags(SD.getFlags() | SF_ThumbFunc);
Jim Grosbachce792992010-11-05 22:08:08 +0000167}
168
Daniel Dunbar821e3332009-08-31 08:09:28 +0000169void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000170 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
171 // MCObjectStreamer.
Daniel Dunbara4d86672009-10-16 01:58:23 +0000172 // FIXME: Lift context changes into super class.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000173 getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar08a408a2010-05-05 17:41:00 +0000174 Symbol->setVariableValue(AddValueSymbols(Value));
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000175}
176
177void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000178 MCSymbolAttr Attribute) {
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000179 // Indirect symbols are handled differently, to match how 'as' handles
180 // them. This makes writing matching .o files easier.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000181 if (Attribute == MCSA_IndirectSymbol) {
Daniel Dunbarad7c3d52009-08-26 00:18:21 +0000182 // Note that we intentionally cannot use the symbol data here; this is
183 // important for matching the string table that 'as' generates.
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000184 IndirectSymbolData ISD;
185 ISD.Symbol = Symbol;
Daniel Dunbar83b46712010-06-16 20:04:25 +0000186 ISD.SectionData = getCurrentSectionData();
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000187 getAssembler().getIndirectSymbols().push_back(ISD);
Daniel Dunbar0c7761b2009-08-24 11:56:58 +0000188 return;
189 }
190
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000191 // Adding a symbol attribute always introduces the symbol, note that an
Daniel Dunbar46836a72010-03-10 20:58:29 +0000192 // important side effect of calling getOrCreateSymbolData here is to register
193 // the symbol with the assembler.
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000194 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000195
196 // The implementation of symbol attributes is designed to match 'as', but it
197 // leaves much to desired. It doesn't really make sense to arbitrarily add and
198 // remove flags, but 'as' allows this (in particular, see .desc).
199 //
200 // In the future it might be worth trying to make these operations more well
201 // defined.
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000202 switch (Attribute) {
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000203 case MCSA_Invalid:
Chris Lattnered0ab152010-01-25 18:30:45 +0000204 case MCSA_ELF_TypeFunction:
205 case MCSA_ELF_TypeIndFunction:
206 case MCSA_ELF_TypeObject:
207 case MCSA_ELF_TypeTLS:
208 case MCSA_ELF_TypeCommon:
209 case MCSA_ELF_TypeNoType:
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000210 case MCSA_ELF_TypeGnuUniqueObject:
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000211 case MCSA_IndirectSymbol:
212 case MCSA_Hidden:
213 case MCSA_Internal:
214 case MCSA_Protected:
215 case MCSA_Weak:
216 case MCSA_Local:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000217 assert(0 && "Invalid symbol attribute for Mach-O!");
218 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000219
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000220 case MCSA_Global:
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000221 SD.setExternal(true);
Daniel Dunbarb18d2dd2010-05-17 20:12:31 +0000222 // This effectively clears the undefined lazy bit, in Darwin 'as', although
223 // it isn't very consistent because it implements this as part of symbol
224 // lookup.
225 //
226 // FIXME: Cleanup this code, these bits should be emitted based on semantic
227 // properties, not on the order of definition, etc.
228 SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000229 break;
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000230
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000231 case MCSA_LazyReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000232 // FIXME: This requires -dynamic.
233 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
234 if (Symbol->isUndefined())
235 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
236 break;
237
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000238 // Since .reference sets the no dead strip bit, it is equivalent to
239 // .no_dead_strip in practice.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000240 case MCSA_Reference:
241 case MCSA_NoDeadStrip:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000242 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
243 break;
244
Kevin Enderbye8e98d72010-11-19 18:39:33 +0000245 case MCSA_SymbolResolver:
246 SD.setFlags(SD.getFlags() | SF_SymbolResolver);
247 break;
248
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000249 case MCSA_PrivateExtern:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000250 SD.setExternal(true);
251 SD.setPrivateExtern(true);
252 break;
253
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000254 case MCSA_WeakReference:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000255 // FIXME: This requires -dynamic.
256 if (Symbol->isUndefined())
257 SD.setFlags(SD.getFlags() | SF_WeakReference);
258 break;
259
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000260 case MCSA_WeakDefinition:
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000261 // FIXME: 'as' enforces that this is defined and global. The manual claims
262 // it has to be in a coalesced section, but this isn't enforced.
263 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
264 break;
Kevin Enderbyf59cac52010-07-08 17:22:42 +0000265
266 case MCSA_WeakDefAutoPrivate:
267 SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
268 break;
Daniel Dunbar3edd9bb2009-08-22 11:41:10 +0000269 }
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000270}
271
272void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000273 // Encode the 'desc' value into the lowest implementation defined bits.
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000274 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
Daniel Dunbar6aff2fb2009-08-24 08:40:12 +0000275 "Invalid .desc value!");
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000276 getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
277 DescValue & SF_DescFlagsMask);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000278}
279
Chris Lattner9eb158d2010-01-23 07:47:02 +0000280void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000281 unsigned ByteAlignment) {
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000282 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
283 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
284
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000285 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000286 SD.setExternal(true);
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000287 SD.setCommon(Size, ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000288}
289
Daniel Dunbar8751b942009-08-28 05:48:22 +0000290void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000291 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000292 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000293
294 // The symbol may not be present, which only creates the section.
295 if (!Symbol)
296 return;
297
298 // FIXME: Assert that this section has the zerofill type.
299
300 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
301
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000302 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000303
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000304 // Emit an align fragment if necessary.
305 if (ByteAlignment != 1)
Daniel Dunbar1c154132010-05-12 22:56:23 +0000306 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000307
Daniel Dunbar4e544872010-05-12 22:51:38 +0000308 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000309 SD.setFragment(F);
310
311 Symbol->setSection(*Section);
312
313 // Update the maximum alignment on the zero fill section if necessary.
314 if (ByteAlignment > SectData.getAlignment())
315 SectData.setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000316}
317
Eric Christopher070c1ab2010-05-21 23:03:53 +0000318// This should always be called with the thread local bss section. Like the
319// .zerofill directive this doesn't actually switch sections on us.
Eric Christopher011c3f12010-05-18 21:26:41 +0000320void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
321 uint64_t Size, unsigned ByteAlignment) {
322 EmitZerofill(Section, Symbol, Size, ByteAlignment);
323 return;
Eric Christopher482eba02010-05-14 01:50:28 +0000324}
325
Chris Lattneraaec2052010-01-19 19:46:13 +0000326void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000327 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
328 // MCObjectStreamer.
Daniel Dunbarf70f4772010-03-22 20:35:46 +0000329 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000330}
331
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000332void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
333 int64_t Value, unsigned ValueSize,
334 unsigned MaxBytesToEmit) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000335 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
336 // MCObjectStreamer.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000337 if (MaxBytesToEmit == 0)
338 MaxBytesToEmit = ByteAlignment;
Daniel Dunbara86de102010-06-16 20:04:32 +0000339 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
340 getCurrentSectionData());
Kevin Enderby6e720482010-02-23 18:26:34 +0000341
342 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar83b46712010-06-16 20:04:25 +0000343 if (ByteAlignment > getCurrentSectionData()->getAlignment())
344 getCurrentSectionData()->setAlignment(ByteAlignment);
Kevin Enderby6e720482010-02-23 18:26:34 +0000345}
346
347void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
348 unsigned MaxBytesToEmit) {
Michael J. Spencer8067adc2010-07-19 06:13:10 +0000349 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
350 // MCObjectStreamer.
Kevin Enderby6e720482010-02-23 18:26:34 +0000351 if (MaxBytesToEmit == 0)
352 MaxBytesToEmit = ByteAlignment;
Daniel Dunbar1c154132010-05-12 22:56:23 +0000353 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
Daniel Dunbar83b46712010-06-16 20:04:25 +0000354 getCurrentSectionData());
Daniel Dunbar1c154132010-05-12 22:56:23 +0000355 F->setEmitNops(true);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000356
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000357 // Update the maximum alignment on the current section if necessary.
Daniel Dunbar83b46712010-06-16 20:04:25 +0000358 if (ByteAlignment > getCurrentSectionData()->getAlignment())
359 getCurrentSectionData()->setAlignment(ByteAlignment);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000360}
361
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000362void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
363 MCDataFragment *DF = getOrCreateDataFragment();
364
365 SmallVector<MCFixup, 4> Fixups;
366 SmallString<256> Code;
367 raw_svector_ostream VecOS(Code);
Daniel Dunbar8dc68ab2010-06-16 20:04:22 +0000368 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
Daniel Dunbar2ac0c452010-05-26 20:37:00 +0000369 VecOS.flush();
370
371 // Add the fixups and data.
372 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
373 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
374 DF->addFixup(Fixups[i]);
375 }
376 DF->getContents().append(Code.begin(), Code.end());
377}
378
Daniel Dunbara86de102010-06-16 20:04:32 +0000379void MCMachOStreamer::Finish() {
380 // We have to set the fragment atom associations so we can relax properly for
381 // Mach-O.
382
383 // First, scan the symbol table to build a lookup table from fragments to
384 // defining symbols.
385 DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
386 for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
387 ie = getAssembler().symbol_end(); it != ie; ++it) {
388 if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
389 it->getFragment()) {
390 // An atom defining symbol should never be internal to a fragment.
391 assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
392 DefiningSymbolMap[it->getFragment()] = it;
393 }
394 }
395
396 // Set the fragment atom associations by tracking the last seen atom defining
397 // symbol.
398 for (MCAssembler::iterator it = getAssembler().begin(),
399 ie = getAssembler().end(); it != ie; ++it) {
400 MCSymbolData *CurrentAtom = 0;
401 for (MCSectionData::iterator it2 = it->begin(),
402 ie2 = it->end(); it2 != ie2; ++it2) {
403 if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
404 CurrentAtom = SD;
405 it2->setAtom(CurrentAtom);
406 }
407 }
408
409 this->MCObjectStreamer::Finish();
410}
411
Daniel Dunbar1f3e4452010-03-11 01:34:27 +0000412MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000413 raw_ostream &OS, MCCodeEmitter *CE,
414 bool RelaxAll) {
415 MCMachOStreamer *S = new MCMachOStreamer(Context, TAB, OS, CE);
416 if (RelaxAll)
417 S->getAssembler().setRelaxAll(true);
418 return S;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000419}