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/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCSection.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/MC/MCValue.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
26 class MCMachOStreamer : public MCStreamer {
27 /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest
28 /// 16 bits of the implementation defined flags.
29 enum SymbolFlags { // See <mach-o/nlist.h>.
30 SF_DescFlagsMask = 0xFFFF,
32 // Reference type flags.
33 SF_ReferenceTypeMask = 0x0007,
34 SF_ReferenceTypeUndefinedNonLazy = 0x0000,
35 SF_ReferenceTypeUndefinedLazy = 0x0001,
36 SF_ReferenceTypeDefined = 0x0002,
37 SF_ReferenceTypePrivateDefined = 0x0003,
38 SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004,
39 SF_ReferenceTypePrivateUndefinedLazy = 0x0005,
41 // Other 'desc' flags.
42 SF_NoDeadStrip = 0x0020,
43 SF_WeakReference = 0x0040,
44 SF_WeakDefinition = 0x0080
48 MCAssembler Assembler;
49 MCCodeEmitter *Emitter;
50 MCSectionData *CurSectionData;
51 DenseMap<const MCSection*, MCSectionData*> SectionMap;
52 DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
55 MCFragment *getCurrentFragment() const {
56 assert(CurSectionData && "No current section!");
58 if (!CurSectionData->empty())
59 return &CurSectionData->getFragmentList().back();
64 MCSectionData &getSectionData(const MCSection &Section) {
65 MCSectionData *&Entry = SectionMap[&Section];
68 Entry = new MCSectionData(Section, &Assembler);
73 MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
74 MCSymbolData *&Entry = SymbolMap[&Symbol];
77 Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
83 MCMachOStreamer(MCContext &Context, raw_ostream &_OS, MCCodeEmitter *_Emitter)
84 : MCStreamer(Context), Assembler(Context, _OS), Emitter(_Emitter),
88 const MCExpr *AddValueSymbols(const MCExpr *Value) {
89 switch (Value->getKind()) {
90 case MCExpr::Target: assert(0 && "Can't handle target exprs yet!");
91 case MCExpr::Constant:
94 case MCExpr::Binary: {
95 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
96 AddValueSymbols(BE->getLHS());
97 AddValueSymbols(BE->getRHS());
101 case MCExpr::SymbolRef:
102 getSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
106 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
113 /// @name MCStreamer Interface
116 virtual void SwitchSection(const MCSection *Section);
117 virtual void EmitLabel(MCSymbol *Symbol);
118 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
119 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
120 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
121 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
122 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
123 unsigned ByteAlignment);
124 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
125 assert(0 && "macho doesn't support this directive");
127 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
128 assert(0 && "macho doesn't support this directive");
130 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
131 unsigned Size = 0, unsigned ByteAlignment = 0);
132 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
133 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
134 virtual void EmitGPRel32Value(const MCExpr *Value) {
135 assert(0 && "macho doesn't support this directive");
137 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
138 unsigned ValueSize = 1,
139 unsigned MaxBytesToEmit = 0);
140 virtual void EmitCodeAlignment(unsigned ByteAlignment,
141 unsigned MaxBytesToEmit = 0);
142 virtual void EmitValueToOffset(const MCExpr *Offset,
143 unsigned char Value = 0);
145 virtual void EmitFileDirective(StringRef Filename) {
146 errs() << "FIXME: MCMachoStreamer:EmitFileDirective not implemented\n";
148 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
149 errs() << "FIXME: MCMachoStreamer:EmitDwarfFileDirective not implemented\n";
152 virtual void EmitInstruction(const MCInst &Inst);
153 virtual void Finish();
158 } // end anonymous namespace.
160 void MCMachOStreamer::SwitchSection(const MCSection *Section) {
161 assert(Section && "Cannot switch to a null section!");
163 // If already in this section, then this is a noop.
164 if (Section == CurSection) return;
166 CurSection = Section;
167 CurSectionData = &getSectionData(*Section);
170 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
171 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
173 // FIXME: We should also use offsets into Fill fragments.
174 MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
176 F = new MCDataFragment(CurSectionData);
178 MCSymbolData &SD = getSymbolData(*Symbol);
179 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
181 SD.setOffset(F->getContents().size());
183 // This causes the reference type and weak reference flags to be cleared.
184 SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
186 Symbol->setSection(*CurSection);
189 void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
191 case MCAF_SubsectionsViaSymbols:
192 Assembler.setSubsectionsViaSymbols(true);
196 assert(0 && "invalid assembler flag!");
199 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
200 // Only absolute symbols can be redefined.
201 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
202 "Cannot define a symbol twice!");
204 // FIXME: Lift context changes into super class.
205 // FIXME: Set associated section.
206 Symbol->setValue(Value);
209 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
210 MCSymbolAttr Attribute) {
211 // Indirect symbols are handled differently, to match how 'as' handles
212 // them. This makes writing matching .o files easier.
213 if (Attribute == MCSA_IndirectSymbol) {
214 // Note that we intentionally cannot use the symbol data here; this is
215 // important for matching the string table that 'as' generates.
216 IndirectSymbolData ISD;
218 ISD.SectionData = CurSectionData;
219 Assembler.getIndirectSymbols().push_back(ISD);
223 // Adding a symbol attribute always introduces the symbol, note that an
224 // important side effect of calling getSymbolData here is to register the
225 // symbol with the assembler.
226 MCSymbolData &SD = getSymbolData(*Symbol);
228 // The implementation of symbol attributes is designed to match 'as', but it
229 // leaves much to desired. It doesn't really make sense to arbitrarily add and
230 // remove flags, but 'as' allows this (in particular, see .desc).
232 // In the future it might be worth trying to make these operations more well
236 case MCSA_ELF_TypeFunction:
237 case MCSA_ELF_TypeIndFunction:
238 case MCSA_ELF_TypeObject:
239 case MCSA_ELF_TypeTLS:
240 case MCSA_ELF_TypeCommon:
241 case MCSA_ELF_TypeNoType:
242 case MCSA_IndirectSymbol:
248 assert(0 && "Invalid symbol attribute for Mach-O!");
252 SD.setExternal(true);
255 case MCSA_LazyReference:
256 // FIXME: This requires -dynamic.
257 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
258 if (Symbol->isUndefined())
259 SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
262 // Since .reference sets the no dead strip bit, it is equivalent to
263 // .no_dead_strip in practice.
265 case MCSA_NoDeadStrip:
266 SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
269 case MCSA_PrivateExtern:
270 SD.setExternal(true);
271 SD.setPrivateExtern(true);
274 case MCSA_WeakReference:
275 // FIXME: This requires -dynamic.
276 if (Symbol->isUndefined())
277 SD.setFlags(SD.getFlags() | SF_WeakReference);
280 case MCSA_WeakDefinition:
281 // FIXME: 'as' enforces that this is defined and global. The manual claims
282 // it has to be in a coalesced section, but this isn't enforced.
283 SD.setFlags(SD.getFlags() | SF_WeakDefinition);
288 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
289 // Encode the 'desc' value into the lowest implementation defined bits.
290 assert(DescValue == (DescValue & SF_DescFlagsMask) &&
291 "Invalid .desc value!");
292 getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask);
295 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
296 unsigned ByteAlignment) {
297 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
298 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
300 MCSymbolData &SD = getSymbolData(*Symbol);
301 SD.setExternal(true);
302 SD.setCommon(Size, ByteAlignment);
305 void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
306 unsigned Size, unsigned ByteAlignment) {
307 MCSectionData &SectData = getSectionData(*Section);
309 // The symbol may not be present, which only creates the section.
313 // FIXME: Assert that this section has the zerofill type.
315 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
317 MCSymbolData &SD = getSymbolData(*Symbol);
319 MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData);
322 Symbol->setSection(*Section);
324 // Update the maximum alignment on the zero fill section if necessary.
325 if (ByteAlignment > SectData.getAlignment())
326 SectData.setAlignment(ByteAlignment);
329 void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
330 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
332 DF = new MCDataFragment(CurSectionData);
333 DF->getContents().append(Data.begin(), Data.end());
336 void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
337 unsigned AddrSpace) {
338 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
340 DF = new MCDataFragment(CurSectionData);
342 // Avoid fixups when possible.
344 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
345 // FIXME: Endianness assumption.
346 for (unsigned i = 0; i != Size; ++i)
347 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
349 DF->getFixups().push_back(MCAsmFixup(DF->getContents().size(),
350 *AddValueSymbols(Value),
351 MCFixup::getKindForSize(Size)));
352 DF->getContents().resize(DF->getContents().size() + Size, 0);
356 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
357 int64_t Value, unsigned ValueSize,
358 unsigned MaxBytesToEmit) {
359 if (MaxBytesToEmit == 0)
360 MaxBytesToEmit = ByteAlignment;
361 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
362 false /* EmitNops */, CurSectionData);
364 // Update the maximum alignment on the current section if necessary.
365 if (ByteAlignment > CurSectionData->getAlignment())
366 CurSectionData->setAlignment(ByteAlignment);
369 void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
370 unsigned MaxBytesToEmit) {
371 if (MaxBytesToEmit == 0)
372 MaxBytesToEmit = ByteAlignment;
373 // FIXME the 0x90 is the default x86 1 byte nop opcode.
374 new MCAlignFragment(ByteAlignment, 0x90, 1, MaxBytesToEmit,
375 true /* EmitNops */, CurSectionData);
377 // Update the maximum alignment on the current section if necessary.
378 if (ByteAlignment > CurSectionData->getAlignment())
379 CurSectionData->setAlignment(ByteAlignment);
382 void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
383 unsigned char Value) {
384 new MCOrgFragment(*Offset, Value, CurSectionData);
387 void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
389 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
390 if (Inst.getOperand(i).isExpr())
391 AddValueSymbols(Inst.getOperand(i).getExpr());
394 llvm_unreachable("no code emitter available!");
396 CurSectionData->setHasInstructions(true);
398 SmallVector<MCFixup, 4> Fixups;
399 SmallString<256> Code;
400 raw_svector_ostream VecOS(Code);
401 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
404 // Add the fixups and data.
405 MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
407 DF = new MCDataFragment(CurSectionData);
408 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
409 MCFixup &F = Fixups[i];
410 DF->getFixups().push_back(MCAsmFixup(DF->getContents().size()+F.getOffset(),
414 DF->getContents().append(Code.begin(), Code.end());
417 void MCMachOStreamer::Finish() {
421 MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS,
423 return new MCMachOStreamer(Context, OS, CE);