1 //===- lib/MC/MCMachOStreamer.cpp - Mach-O Object Output ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCStreamer.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCCodeEmitter.h"
15 #include "llvm/MC/MCInst.h"
16 #include "llvm/MC/MCSection.h"
17 #include "llvm/MC/MCSymbol.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/raw_ostream.h"
24 class MCMachOStreamer : public MCStreamer {
25 /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest
26 /// 16 bits of the implementation defined flags.
27 enum SymbolFlags { // See <mach-o/nlist.h>.
28 SF_DescFlagsMask = 0xFFFF,
30 // Reference type flags.
31 SF_ReferenceTypeMask = 0x0007,
32 SF_ReferenceTypeUndefinedNonLazy = 0x0000,
33 SF_ReferenceTypeUndefinedLazy = 0x0001,
34 SF_ReferenceTypeDefined = 0x0002,
35 SF_ReferenceTypePrivateDefined = 0x0003,
36 SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004,
37 SF_ReferenceTypePrivateUndefinedLazy = 0x0005,
39 // Other 'desc' flags.
40 SF_NoDeadStrip = 0x0020,
41 SF_WeakReference = 0x0040,
42 SF_WeakDefinition = 0x0080
46 MCAssembler Assembler;
48 MCCodeEmitter *Emitter;
50 MCSectionData *CurSectionData;
52 DenseMap<const MCSection*, MCSectionData*> SectionMap;
54 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
57 MCFragment *getCurrentFragment() const {
58 assert(CurSectionData && "No current section!");
60 if (!CurSectionData->empty())
61 return &CurSectionData->getFragmentList().back();
66 MCSectionData &getSectionData(const MCSection &Section) {
67 MCSectionData *&Entry = SectionMap[&Section];
70 Entry = new MCSectionData(Section, &Assembler);
75 MCSymbolData &getSymbolData(MCSymbol &Symbol) {
76 MCSymbolData *&Entry = SymbolMap[&Symbol];
79 Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
85 MCMachOStreamer(MCContext &Context, raw_ostream &_OS, MCCodeEmitter *_Emitter)
86 : MCStreamer(Context), Assembler(_OS), Emitter(_Emitter),
90 const MCValue &AddValueSymbols(const MCValue &Value) {
92 getSymbolData(*const_cast<MCSymbol*>(Value.getSymA()));
94 getSymbolData(*const_cast<MCSymbol*>(Value.getSymB()));
98 /// @name MCStreamer Interface
101 virtual void SwitchSection(const MCSection *Section);
103 virtual void EmitLabel(MCSymbol *Symbol);
105 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
107 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
108 bool MakeAbsolute = false);
110 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
112 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
114 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
116 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
117 unsigned Pow2Alignment);
119 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
120 unsigned Size = 0, unsigned Pow2Alignment = 0);
122 virtual void EmitBytes(const StringRef &Data);
124 virtual void EmitValue(const MCValue &Value, unsigned Size);
126 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
127 unsigned ValueSize = 1,
128 unsigned MaxBytesToEmit = 0);
130 virtual void EmitValueToOffset(const MCValue &Offset,
131 unsigned char Value = 0);
133 virtual void EmitInstruction(const MCInst &Inst);
135 virtual void Finish();
140 } // end anonymous namespace.
142 void MCMachOStreamer::SwitchSection(const MCSection *Section) {
143 assert(Section && "Cannot switch to a null section!");
145 // If already in this section, then this is a noop.
146 if (Section == CurSection) return;
148 CurSection = Section;
149 CurSectionData = &getSectionData(*Section);
152 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
153 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
155 // FIXME: We should also use offsets into Fill fragments.
156 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
158 F = new MCDataFragment(CurSectionData);
160 MCSymbolData &SD = getSymbolData(*Symbol);
161 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
163 SD.setOffset(F->getContents().size());
165 // This causes the reference type and weak reference flags to be cleared.
166 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
168 Symbol->setSection(*CurSection);
171 void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
174 llvm_unreachable("FIXME: Not yet implemented!");
176 case SubsectionsViaSymbols:
177 Assembler.setSubsectionsViaSymbols(true);
182 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
183 const MCValue &Value,
185 // Only absolute symbols can be redefined.
186 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
187 "Cannot define a symbol twice!");
189 llvm_unreachable("FIXME: Not yet implemented!");
192 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
193 SymbolAttr Attribute) {
194 // Indirect symbols are handled differently, to match how 'as' handles
195 // them. This makes writing matching .o files easier.
196 if (Attribute == MCStreamer::IndirectSymbol) {
197 // Note that we intentionally cannot use the symbol data here; this is
198 // important for matching the string table that 'as' generates.
199 IndirectSymbolData ISD;
201 ISD.SectionData = CurSectionData;
202 Assembler.getIndirectSymbols().push_back(ISD);
206 // Adding a symbol attribute always introduces the symbol, note that an
207 // important side effect of calling getSymbolData here is to register the
208 // symbol with the assembler.
209 MCSymbolData &SD = getSymbolData(*Symbol);
211 // The implementation of symbol attributes is designed to match 'as', but it
212 // leaves much to desired. It doesn't really make sense to arbitrarily add and
213 // remove flags, but 'as' allows this (in particular, see .desc).
215 // In the future it might be worth trying to make these operations more well
218 case MCStreamer::IndirectSymbol:
219 case MCStreamer::Hidden:
220 case MCStreamer::Internal:
221 case MCStreamer::Protected:
222 case MCStreamer::Weak:
223 assert(0 && "Invalid symbol attribute for Mach-O!");
226 case MCStreamer::Global:
227 SD.setExternal(true);
230 case MCStreamer::LazyReference:
231 // FIXME: This requires -dynamic.
232 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
233 if (Symbol->isUndefined())
234 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
237 // Since .reference sets the no dead strip bit, it is equivalent to
238 // .no_dead_strip in practice.
239 case MCStreamer::Reference:
240 case MCStreamer::NoDeadStrip:
241 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
244 case MCStreamer::PrivateExtern:
245 SD.setExternal(true);
246 SD.setPrivateExtern(true);
249 case MCStreamer::WeakReference:
250 // FIXME: This requires -dynamic.
251 if (Symbol->isUndefined())
252 SD.setFlags(SD.getFlags() | SF_WeakReference);
255 case MCStreamer::WeakDefinition:
256 // FIXME: 'as' enforces that this is defined and global. The manual claims
257 // it has to be in a coalesced section, but this isn't enforced.
258 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
263 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
264 // Encode the 'desc' value into the lowest implementation defined bits.
265 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
266 "Invalid .desc value!");
267 getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask);
270 void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
271 llvm_unreachable("FIXME: Not yet implemented!");
274 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
275 unsigned Pow2Alignment) {
276 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
277 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
279 MCSymbolData &SD = getSymbolData(*Symbol);
280 SD.setExternal(true);
281 SD.setCommon(Size, 1 << Pow2Alignment);
284 void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
285 unsigned Size, unsigned Pow2Alignment) {
286 unsigned ByteAlignment = 1 << Pow2Alignment;
287 MCSectionData &SectData = getSectionData(*Section);
289 // The symbol may not be present, which only creates the section.
293 // FIXME: Assert that this section has the zerofill type.
295 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
297 MCSymbolData &SD = getSymbolData(*Symbol);
299 MCFragment *F = new MCZeroFillFragment(Size, 1 << Pow2Alignment, &SectData);
302 Symbol->setSection(*Section);
304 // Update the maximum alignment on the zero fill section if necessary.
305 if (ByteAlignment > SectData.getAlignment())
306 SectData.setAlignment(ByteAlignment);
309 void MCMachOStreamer::EmitBytes(const StringRef &Data) {
310 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
312 DF = new MCDataFragment(CurSectionData);
313 DF->getContents().append(Data.begin(), Data.end());
316 void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
317 new MCFillFragment(AddValueSymbols(Value), Size, 1, CurSectionData);
320 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
321 int64_t Value, unsigned ValueSize,
322 unsigned MaxBytesToEmit) {
323 if (MaxBytesToEmit == 0)
324 MaxBytesToEmit = ByteAlignment;
325 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
328 // Update the maximum alignment on the current section if necessary.
329 if (ByteAlignment > CurSectionData->getAlignment())
330 CurSectionData->setAlignment(ByteAlignment);
333 void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
334 unsigned char Value) {
335 new MCOrgFragment(AddValueSymbols(Offset), Value, CurSectionData);
338 void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
340 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
341 if (Inst.getOperand(i).isMCValue())
342 AddValueSymbols(Inst.getOperand(i).getMCValue());
345 llvm_unreachable("no code emitter available!");
347 // FIXME: Relocations!
348 SmallString<256> Code;
349 raw_svector_ostream VecOS(Code);
350 Emitter->EncodeInstruction(Inst, VecOS);
351 EmitBytes(VecOS.str());
354 void MCMachOStreamer::Finish() {
358 MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS,
360 return new MCMachOStreamer(Context, OS, CE);