LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MCMachOStreamer.cpp
Go to the documentation of this file.
1 //===-- MCMachOStreamer.cpp - MachO Streamer ------------------------------===//
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 #include "llvm/MC/MCAsmBackend.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCCodeEmitter.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCDwarf.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCSection.h"
21 #include "llvm/MC/MCSectionMachO.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/Support/Dwarf.h"
26 
27 using namespace llvm;
28 
29 namespace {
30 
31 class MCMachOStreamer : public MCObjectStreamer {
32 private:
33  virtual void EmitInstToData(const MCInst &Inst);
34 
35  void EmitDataRegion(DataRegionData::KindTy Kind);
36  void EmitDataRegionEnd();
37 public:
38  MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, raw_ostream &OS,
39  MCCodeEmitter *Emitter)
40  : MCObjectStreamer(Context, 0, MAB, OS, Emitter) {}
41 
42  /// @name MCStreamer Interface
43  /// @{
44 
45  virtual void InitSections();
46  virtual void InitToTextSection();
47  virtual void EmitLabel(MCSymbol *Symbol);
48  virtual void EmitDebugLabel(MCSymbol *Symbol);
49  virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
50  MCSymbol *EHSymbol);
51  virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
52  virtual void EmitLinkerOptions(ArrayRef<std::string> Options);
53  virtual void EmitDataRegion(MCDataRegionType Kind);
54  virtual void EmitThumbFunc(MCSymbol *Func);
55  virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
56  virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
57  virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
58  unsigned ByteAlignment);
59  virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
60  llvm_unreachable("macho doesn't support this directive");
61  }
62  virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
63  llvm_unreachable("macho doesn't support this directive");
64  }
65  virtual void EmitCOFFSymbolType(int Type) {
66  llvm_unreachable("macho doesn't support this directive");
67  }
68  virtual void EndCOFFSymbolDef() {
69  llvm_unreachable("macho doesn't support this directive");
70  }
71  virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
72  llvm_unreachable("macho doesn't support this directive");
73  }
74  virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
75  unsigned ByteAlignment);
76  virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
77  uint64_t Size = 0, unsigned ByteAlignment = 0);
78  virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
79  uint64_t Size, unsigned ByteAlignment = 0);
80 
81  virtual void EmitFileDirective(StringRef Filename) {
82  // FIXME: Just ignore the .file; it isn't important enough to fail the
83  // entire assembly.
84 
85  // report_fatal_error("unsupported directive: '.file'");
86  }
87 
88  virtual void EmitIdent(StringRef IdentString) {
89  llvm_unreachable("macho doesn't support this directive");
90  }
91 
92  virtual void FinishImpl();
93 };
94 
95 } // end anonymous namespace.
96 
97 void MCMachOStreamer::InitSections() {
98  InitToTextSection();
99 }
100 
101 void MCMachOStreamer::InitToTextSection() {
102  SwitchSection(getContext().getMachOSection(
103  "__TEXT", "__text",
104  MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, 0,
106 }
107 
108 void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
109  MCSymbol *EHSymbol) {
110  MCSymbolData &SD =
111  getAssembler().getOrCreateSymbolData(*Symbol);
112  if (SD.isExternal())
113  EmitSymbolAttribute(EHSymbol, MCSA_Global);
114  if (SD.getFlags() & SF_WeakDefinition)
115  EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
116  if (SD.isPrivateExtern())
117  EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
118 }
119 
120 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
121  assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
122 
123  // isSymbolLinkerVisible uses the section.
124  AssignSection(Symbol, getCurrentSection().first);
125  // We have to create a new fragment if this is an atom defining symbol,
126  // fragments cannot span atoms.
127  if (getAssembler().isSymbolLinkerVisible(*Symbol))
128  insert(new MCDataFragment());
129 
131 
132  MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
133  // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
134  // to clear the weak reference and weak definition bits too, but the
135  // implementation was buggy. For now we just try to match 'as', for
136  // diffability.
137  //
138  // FIXME: Cleanup this code, these bits should be emitted based on semantic
139  // properties, not on the order of definition, etc.
141 }
142 
143 void MCMachOStreamer::EmitDebugLabel(MCSymbol *Symbol) {
144  EmitLabel(Symbol);
145 }
146 void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
147  if (!getAssembler().getBackend().hasDataInCodeSupport())
148  return;
149  // Create a temporary label to mark the start of the data region.
150  MCSymbol *Start = getContext().CreateTempSymbol();
151  EmitLabel(Start);
152  // Record the region for the object writer to use.
153  DataRegionData Data = { Kind, Start, NULL };
154  std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
155  Regions.push_back(Data);
156 }
157 
158 void MCMachOStreamer::EmitDataRegionEnd() {
159  if (!getAssembler().getBackend().hasDataInCodeSupport())
160  return;
161  std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
162  assert(Regions.size() && "Mismatched .end_data_region!");
163  DataRegionData &Data = Regions.back();
164  assert(Data.End == NULL && "Mismatched .end_data_region!");
165  // Create a temporary label to mark the end of the data region.
166  Data.End = getContext().CreateTempSymbol();
167  EmitLabel(Data.End);
168 }
169 
170 void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
171  // Let the target do whatever target specific stuff it needs to do.
172  getAssembler().getBackend().handleAssemblerFlag(Flag);
173  // Do any generic stuff we need to do.
174  switch (Flag) {
175  case MCAF_SyntaxUnified: return; // no-op here.
176  case MCAF_Code16: return; // Change parsing mode; no-op here.
177  case MCAF_Code32: return; // Change parsing mode; no-op here.
178  case MCAF_Code64: return; // Change parsing mode; no-op here.
180  getAssembler().setSubsectionsViaSymbols(true);
181  return;
182  }
183 }
184 
185 void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
186  getAssembler().getLinkerOptions().push_back(Options);
187 }
188 
189 void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
190  switch (Kind) {
191  case MCDR_DataRegion:
192  EmitDataRegion(DataRegionData::Data);
193  return;
194  case MCDR_DataRegionJT8:
195  EmitDataRegion(DataRegionData::JumpTable8);
196  return;
197  case MCDR_DataRegionJT16:
198  EmitDataRegion(DataRegionData::JumpTable16);
199  return;
200  case MCDR_DataRegionJT32:
201  EmitDataRegion(DataRegionData::JumpTable32);
202  return;
203  case MCDR_DataRegionEnd:
204  EmitDataRegionEnd();
205  return;
206  }
207 }
208 
209 void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
210  // Remember that the function is a thumb function. Fixup and relocation
211  // values will need adjusted.
212  getAssembler().setIsThumbFunc(Symbol);
213 
214  // Mark the thumb bit on the symbol.
215  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
216  SD.setFlags(SD.getFlags() | SF_ThumbFunc);
217 }
218 
219 bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
221  // Indirect symbols are handled differently, to match how 'as' handles
222  // them. This makes writing matching .o files easier.
223  if (Attribute == MCSA_IndirectSymbol) {
224  // Note that we intentionally cannot use the symbol data here; this is
225  // important for matching the string table that 'as' generates.
226  IndirectSymbolData ISD;
227  ISD.Symbol = Symbol;
228  ISD.SectionData = getCurrentSectionData();
229  getAssembler().getIndirectSymbols().push_back(ISD);
230  return true;
231  }
232 
233  // Adding a symbol attribute always introduces the symbol, note that an
234  // important side effect of calling getOrCreateSymbolData here is to register
235  // the symbol with the assembler.
236  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
237 
238  // The implementation of symbol attributes is designed to match 'as', but it
239  // leaves much to desired. It doesn't really make sense to arbitrarily add and
240  // remove flags, but 'as' allows this (in particular, see .desc).
241  //
242  // In the future it might be worth trying to make these operations more well
243  // defined.
244  switch (Attribute) {
245  case MCSA_Invalid:
248  case MCSA_ELF_TypeObject:
249  case MCSA_ELF_TypeTLS:
250  case MCSA_ELF_TypeCommon:
251  case MCSA_ELF_TypeNoType:
253  case MCSA_Hidden:
254  case MCSA_IndirectSymbol:
255  case MCSA_Internal:
256  case MCSA_Protected:
257  case MCSA_Weak:
258  case MCSA_Local:
259  return false;
260 
261  case MCSA_Global:
262  SD.setExternal(true);
263  // This effectively clears the undefined lazy bit, in Darwin 'as', although
264  // it isn't very consistent because it implements this as part of symbol
265  // lookup.
266  //
267  // FIXME: Cleanup this code, these bits should be emitted based on semantic
268  // properties, not on the order of definition, etc.
270  break;
271 
272  case MCSA_LazyReference:
273  // FIXME: This requires -dynamic.
274  SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
275  if (Symbol->isUndefined())
277  break;
278 
279  // Since .reference sets the no dead strip bit, it is equivalent to
280  // .no_dead_strip in practice.
281  case MCSA_Reference:
282  case MCSA_NoDeadStrip:
283  SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
284  break;
285 
286  case MCSA_SymbolResolver:
288  break;
289 
290  case MCSA_PrivateExtern:
291  SD.setExternal(true);
292  SD.setPrivateExtern(true);
293  break;
294 
295  case MCSA_WeakReference:
296  // FIXME: This requires -dynamic.
297  if (Symbol->isUndefined())
299  break;
300 
301  case MCSA_WeakDefinition:
302  // FIXME: 'as' enforces that this is defined and global. The manual claims
303  // it has to be in a coalesced section, but this isn't enforced.
305  break;
306 
309  break;
310  }
311 
312  return true;
313 }
314 
315 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
316  // Encode the 'desc' value into the lowest implementation defined bits.
317  assert(DescValue == (DescValue & SF_DescFlagsMask) &&
318  "Invalid .desc value!");
319  getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
320  DescValue & SF_DescFlagsMask);
321 }
322 
323 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
324  unsigned ByteAlignment) {
325  // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
326  assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
327 
328  AssignSection(Symbol, NULL);
329 
330  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
331  SD.setExternal(true);
332  SD.setCommon(Size, ByteAlignment);
333 }
334 
335 void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
336  unsigned ByteAlignment) {
337  // '.lcomm' is equivalent to '.zerofill'.
338  return EmitZerofill(getContext().getMachOSection("__DATA", "__bss",
340  0, SectionKind::getBSS()),
341  Symbol, Size, ByteAlignment);
342 }
343 
344 void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
345  uint64_t Size, unsigned ByteAlignment) {
346  MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
347 
348  // The symbol may not be present, which only creates the section.
349  if (!Symbol)
350  return;
351 
352  // On darwin all virtual sections have zerofill type.
353  assert(Section->isVirtualSection() && "Section does not have zerofill type!");
354 
355  assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
356 
357  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
358 
359  // Emit an align fragment if necessary.
360  if (ByteAlignment != 1)
361  new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
362 
363  MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
364  SD.setFragment(F);
365 
366  AssignSection(Symbol, Section);
367 
368  // Update the maximum alignment on the zero fill section if necessary.
369  if (ByteAlignment > SectData.getAlignment())
370  SectData.setAlignment(ByteAlignment);
371 }
372 
373 // This should always be called with the thread local bss section. Like the
374 // .zerofill directive this doesn't actually switch sections on us.
375 void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
376  uint64_t Size, unsigned ByteAlignment) {
377  EmitZerofill(Section, Symbol, Size, ByteAlignment);
378  return;
379 }
380 
381 void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
382  MCDataFragment *DF = getOrCreateDataFragment();
383 
386  raw_svector_ostream VecOS(Code);
387  getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
388  VecOS.flush();
389 
390  // Add the fixups and data.
391  for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
392  Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
393  DF->getFixups().push_back(Fixups[i]);
394  }
395  DF->getContents().append(Code.begin(), Code.end());
396 }
397 
398 void MCMachOStreamer::FinishImpl() {
399  EmitFrames(&getAssembler().getBackend(), true);
400 
401  // We have to set the fragment atom associations so we can relax properly for
402  // Mach-O.
403 
404  // First, scan the symbol table to build a lookup table from fragments to
405  // defining symbols.
407  for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
408  ie = getAssembler().symbol_end(); it != ie; ++it) {
409  if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
410  it->getFragment()) {
411  // An atom defining symbol should never be internal to a fragment.
412  assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
413  DefiningSymbolMap[it->getFragment()] = it;
414  }
415  }
416 
417  // Set the fragment atom associations by tracking the last seen atom defining
418  // symbol.
419  for (MCAssembler::iterator it = getAssembler().begin(),
420  ie = getAssembler().end(); it != ie; ++it) {
421  MCSymbolData *CurrentAtom = 0;
422  for (MCSectionData::iterator it2 = it->begin(),
423  ie2 = it->end(); it2 != ie2; ++it2) {
424  if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
425  CurrentAtom = SD;
426  it2->setAtom(CurrentAtom);
427  }
428  }
429 
431 }
432 
434  raw_ostream &OS, MCCodeEmitter *CE,
435  bool RelaxAll) {
436  MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE);
437  if (RelaxAll)
438  S->getAssembler().setRelaxAll(true);
439  return S;
440 }
void setExternal(bool Value)
Definition: MCAssembler.h:731
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:181
void setFlags(uint32_t Value)
setFlags - Set the (implementation defined) symbol flags.
Definition: MCAssembler.h:773
.type _foo, STT_OBJECT # aka
Definition: MCDirectives.h:25
Not a valid directive.
Definition: MCDirectives.h:20
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:173
F(f)
virtual SmallVectorImpl< char > & getContents()
Definition: MCAssembler.h:221
MCDataRegionType
Definition: MCDirectives.h:55
.type _foo, STT_NOTYPE # aka
Definition: MCDirectives.h:28
COFF::SymbolStorageClass StorageClass
Definition: COFFYAML.cpp:204
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCAssembler.h:224
static SectionKind getBSS()
Definition: SectionKind.h:225
MCStreamer * createMachOStreamer(MCContext &Ctx, MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *CE, bool RelaxAll=false)
#define llvm_unreachable(msg)
.data_region jt16
Definition: MCDirectives.h:58
.local (ELF)
Definition: MCDirectives.h:35
.no_dead_strip (MachO)
Definition: MCDirectives.h:36
.code16 (X86) / .code 16 (ARM)
Definition: MCDirectives.h:50
Streaming object file generation interface.
.type _foo, STT_GNU_IFUNC
Definition: MCDirectives.h:24
bool isExternal() const
Definition: MCAssembler.h:730
.protected (ELF)
Definition: MCDirectives.h:39
.lazy_reference (MachO)
Definition: MCDirectives.h:34
.reference (MachO)
Definition: MCDirectives.h:40
.hidden (ELF)
Definition: MCDirectives.h:31
.data_region jt32
Definition: MCDirectives.h:59
.weak_def_can_be_hidden (MachO)
Definition: MCDirectives.h:44
void setPrivateExtern(bool Value)
Definition: MCAssembler.h:734
uint32_t getFlags() const
getFlags - Get the (implementation defined) symbol flags.
Definition: MCAssembler.h:770
void setCommon(uint64_t Size, unsigned Align)
Definition: MCAssembler.h:743
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:22
.subsections_via_symbols (MachO)
Definition: MCDirectives.h:49
.weak_reference (MachO)
Definition: MCDirectives.h:43
void append(in_iter in_start, in_iter in_end)
Definition: SmallVector.h:445
virtual bool isVirtualSection() const =0
unsigned getAlignment() const
Definition: MCAssembler.h:607
.indirect_symbol (MachO)
Definition: MCDirectives.h:32
.type _foo, STT_TLS # aka
Definition: MCDirectives.h:26
virtual void EmitLabel(MCSymbol *Symbol)
MCSymbolAttr
Definition: MCDirectives.h:19
.syntax (ARM/ELF)
Definition: MCDirectives.h:48
.internal (ELF)
Definition: MCDirectives.h:33
.code32 (X86) / .code 32 (ARM)
Definition: MCDirectives.h:51
.type _foo, STT_COMMON # aka
Definition: MCDirectives.h:27
.code64 (X86)
Definition: MCDirectives.h:52
MCSectionData * SectionData
Definition: MCAssembler.h:794
.symbol_resolver (MachO)
Definition: MCDirectives.h:37
.type _foo,
Definition: MCDirectives.h:30
virtual void FinishImpl()
FinishImpl - Streamer specific finalization.
MCAssemblerFlag
Definition: MCDirectives.h:47
.type _foo, STT_FUNC # aka
Definition: MCDirectives.h:23
void setAlignment(unsigned Value)
Definition: MCAssembler.h:608
.weak_definition (MachO)
Definition: MCDirectives.h:42
.private_extern (MachO)
Definition: MCDirectives.h:38
.data_region jt8
Definition: MCDirectives.h:57
LLVM Value Representation.
Definition: Value.h:66
MCAsmBackend - Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:34
bool isPrivateExtern() const
Definition: MCAssembler.h:733
cl::opt< bool > RelaxAll("mc-relax-all", cl::desc("When used with filetype=obj, ""relax all fixups in the emitted object file"))
bool isUndefined() const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
Definition: MCSymbol.h:100
.end_data_region
Definition: MCDirectives.h:60
static SectionKind getText()
Definition: SectionKind.h:208