TargetLoweringObjectFile.h revision 5277b22687d3513dd29d5a9c8510cac740f933f6
1//===-- llvm/Target/TargetLoweringObjectFile.h - Object Info ----*- C++ -*-===//
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 implements classes used to handle lowerings specific to common
11// object file formats.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
16#define LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
17
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringMap.h"
20#include "llvm/MC/SectionKind.h"
21
22namespace llvm {
23  class MCSection;
24  class MCContext;
25  class GlobalValue;
26  class Mangler;
27  class TargetMachine;
28  class TargetAsmInfo;
29
30class TargetLoweringObjectFile {
31  MCContext *Ctx;
32protected:
33
34  TargetLoweringObjectFile();
35
36  /// TextSection - Section directive for standard text.
37  ///
38  const MCSection *TextSection;
39
40  /// DataSection - Section directive for standard data.
41  ///
42  const MCSection *DataSection;
43
44  /// BSSSection - Section that is default initialized to zero.
45  const MCSection *BSSSection;
46
47  /// ReadOnlySection - Section that is readonly and can contain arbitrary
48  /// initialized data.  Targets are not required to have a readonly section.
49  /// If they don't, various bits of code will fall back to using the data
50  /// section for constants.
51  const MCSection *ReadOnlySection;
52
53  /// StaticCtorSection - This section contains the static constructor pointer
54  /// list.
55  const MCSection *StaticCtorSection;
56
57  /// StaticDtorSection - This section contains the static destructor pointer
58  /// list.
59  const MCSection *StaticDtorSection;
60
61  /// LSDASection - If exception handling is supported by the target, this is
62  /// the section the Language Specific Data Area information is emitted to.
63  const MCSection *LSDASection;
64
65  /// EHFrameSection - If exception handling is supported by the target, this is
66  /// the section the EH Frame is emitted to.
67  const MCSection *EHFrameSection;
68
69  // Dwarf sections for debug info.  If a target supports debug info, these must
70  // be set.
71  const MCSection *DwarfAbbrevSection;
72  const MCSection *DwarfInfoSection;
73  const MCSection *DwarfLineSection;
74  const MCSection *DwarfFrameSection;
75  const MCSection *DwarfPubNamesSection;
76  const MCSection *DwarfPubTypesSection;
77  const MCSection *DwarfDebugInlineSection;
78  const MCSection *DwarfStrSection;
79  const MCSection *DwarfLocSection;
80  const MCSection *DwarfARangesSection;
81  const MCSection *DwarfRangesSection;
82  const MCSection *DwarfMacroInfoSection;
83
84public:
85
86  MCContext &getContext() const { return *Ctx; }
87
88
89  virtual ~TargetLoweringObjectFile();
90
91  /// Initialize - this method must be called before any actual lowering is
92  /// done.  This specifies the current context for codegen, and gives the
93  /// lowering implementations a chance to set up their default sections.
94  virtual void Initialize(MCContext &ctx, const TargetMachine &TM) {
95    Ctx = &ctx;
96  }
97
98
99  const MCSection *getTextSection() const { return TextSection; }
100  const MCSection *getDataSection() const { return DataSection; }
101  const MCSection *getStaticCtorSection() const { return StaticCtorSection; }
102  const MCSection *getStaticDtorSection() const { return StaticDtorSection; }
103  const MCSection *getLSDASection() const { return LSDASection; }
104  const MCSection *getEHFrameSection() const { return EHFrameSection; }
105  const MCSection *getDwarfAbbrevSection() const { return DwarfAbbrevSection; }
106  const MCSection *getDwarfInfoSection() const { return DwarfInfoSection; }
107  const MCSection *getDwarfLineSection() const { return DwarfLineSection; }
108  const MCSection *getDwarfFrameSection() const { return DwarfFrameSection; }
109  const MCSection *getDwarfPubNamesSection() const{return DwarfPubNamesSection;}
110  const MCSection *getDwarfPubTypesSection() const{return DwarfPubTypesSection;}
111  const MCSection *getDwarfDebugInlineSection() const {
112    return DwarfDebugInlineSection;
113  }
114  const MCSection *getDwarfStrSection() const { return DwarfStrSection; }
115  const MCSection *getDwarfLocSection() const { return DwarfLocSection; }
116  const MCSection *getDwarfARangesSection() const { return DwarfARangesSection;}
117  const MCSection *getDwarfRangesSection() const { return DwarfRangesSection; }
118  const MCSection *getDwarfMacroInfoSection() const {
119    return DwarfMacroInfoSection;
120  }
121
122  /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively
123  /// decide not to emit the UsedDirective for some symbols in llvm.used.
124  /// FIXME: REMOVE this (rdar://7071300)
125  virtual bool shouldEmitUsedDirectiveFor(const GlobalValue *GV,
126                                          Mangler *) const {
127    return GV != 0;
128  }
129
130  /// getSectionForConstant - Given a constant with the SectionKind, return a
131  /// section that it should be placed in.
132  virtual const MCSection *getSectionForConstant(SectionKind Kind) const;
133
134  /// getKindForGlobal - Classify the specified global variable into a set of
135  /// target independent categories embodied in SectionKind.
136  static SectionKind getKindForGlobal(const GlobalValue *GV,
137                                      const TargetMachine &TM);
138
139  /// SectionForGlobal - This method computes the appropriate section to emit
140  /// the specified global variable or function definition.  This should not
141  /// be passed external (or available externally) globals.
142  const MCSection *SectionForGlobal(const GlobalValue *GV,
143                                    SectionKind Kind, Mangler *Mang,
144                                    const TargetMachine &TM) const;
145
146  /// SectionForGlobal - This method computes the appropriate section to emit
147  /// the specified global variable or function definition.  This should not
148  /// be passed external (or available externally) globals.
149  const MCSection *SectionForGlobal(const GlobalValue *GV,
150                                    Mangler *Mang,
151                                    const TargetMachine &TM) const {
152    return SectionForGlobal(GV, getKindForGlobal(GV, TM), Mang, TM);
153  }
154
155
156
157  /// getExplicitSectionGlobal - Targets should implement this method to assign
158  /// a section to globals with an explicit section specfied.  The
159  /// implementation of this method can assume that GV->hasSection() is true.
160  virtual const MCSection *
161  getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
162                           Mangler *Mang, const TargetMachine &TM) const = 0;
163
164  /// getSpecialCasedSectionGlobals - Allow the target to completely override
165  /// section assignment of a global.
166  virtual const MCSection *
167  getSpecialCasedSectionGlobals(const GlobalValue *GV, Mangler *Mang,
168                                SectionKind Kind) const {
169    return 0;
170  }
171
172  /// getSectionFlagsAsString - Turn the flags in the specified SectionKind
173  /// into a string that can be printed to the assembly file after the
174  /// ".section foo" part of a section directive.
175  virtual void getSectionFlagsAsString(SectionKind Kind,
176                                       SmallVectorImpl<char> &Str,
177                                       const TargetAsmInfo &TAI) const {
178  }
179
180protected:
181  virtual const MCSection *
182  SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
183                         Mangler *Mang, const TargetMachine &TM) const;
184};
185
186
187
188
189class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
190  bool AtIsCommentChar;  // True if @ is the comment character on this target.
191  bool HasCrazyBSS;
192protected:
193  /// TLSDataSection - Section directive for Thread Local data.
194  ///
195  const MCSection *TLSDataSection;        // Defaults to ".tdata".
196
197  /// TLSBSSSection - Section directive for Thread Local uninitialized data.
198  /// Null if this target doesn't support a BSS section.
199  ///
200  const MCSection *TLSBSSSection;         // Defaults to ".tbss".
201
202  const MCSection *DataRelSection;
203  const MCSection *DataRelLocalSection;
204  const MCSection *DataRelROSection;
205  const MCSection *DataRelROLocalSection;
206
207  const MCSection *MergeableConst4Section;
208  const MCSection *MergeableConst8Section;
209  const MCSection *MergeableConst16Section;
210
211protected:
212  const MCSection *getELFSection(const char *Name, bool isDirective,
213                                 SectionKind Kind) const;
214public:
215  /// ELF Constructor - AtIsCommentChar is true if the CommentCharacter from TAI
216  /// is "@".
217  TargetLoweringObjectFileELF(bool atIsCommentChar = false,
218                              // FIXME: REMOVE AFTER UNIQUING IS FIXED.
219                              bool hasCrazyBSS = false)
220    : AtIsCommentChar(atIsCommentChar), HasCrazyBSS(hasCrazyBSS) {}
221
222  virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
223
224  /// getSectionForConstant - Given a constant with the SectionKind, return a
225  /// section that it should be placed in.
226  virtual const MCSection *getSectionForConstant(SectionKind Kind) const;
227
228
229  virtual const MCSection *
230  getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
231                           Mangler *Mang, const TargetMachine &TM) const;
232
233  void getSectionFlagsAsString(SectionKind Kind,
234                               SmallVectorImpl<char> &Str,
235                               const TargetAsmInfo &TAI) const;
236
237  virtual const MCSection *
238  SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
239                         Mangler *Mang, const TargetMachine &TM) const;
240};
241
242
243
244class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
245  const MCSection *CStringSection;
246  const MCSection *UStringSection;
247  const MCSection *TextCoalSection;
248  const MCSection *ConstTextCoalSection;
249  const MCSection *ConstDataCoalSection;
250  const MCSection *ConstDataSection;
251  const MCSection *DataCoalSection;
252  const MCSection *FourByteConstantSection;
253  const MCSection *EightByteConstantSection;
254  const MCSection *SixteenByteConstantSection;
255public:
256
257  virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
258
259  virtual const MCSection *
260  SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
261                         Mangler *Mang, const TargetMachine &TM) const;
262
263  virtual const MCSection *
264  getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
265                           Mangler *Mang, const TargetMachine &TM) const;
266
267  virtual const MCSection *getSectionForConstant(SectionKind Kind) const;
268
269  /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively
270  /// decide not to emit the UsedDirective for some symbols in llvm.used.
271  /// FIXME: REMOVE this (rdar://7071300)
272  virtual bool shouldEmitUsedDirectiveFor(const GlobalValue *GV,
273                                          Mangler *) const;
274
275  /// getMachOSection - Return the MCSection for the specified mach-o section.
276  /// FIXME: Switch this to a semantic view eventually.
277  const MCSection *getMachOSection(const char *Name, bool isDirective,
278                                   SectionKind K) const;
279};
280
281
282
283class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
284public:
285  virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
286
287  virtual const MCSection *
288  getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
289                           Mangler *Mang, const TargetMachine &TM) const;
290
291  virtual void getSectionFlagsAsString(SectionKind Kind,
292                                       SmallVectorImpl<char> &Str,
293                                       const TargetAsmInfo &TAI) const;
294
295  virtual const MCSection *
296  SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
297                         Mangler *Mang, const TargetMachine &TM) const;
298
299  /// getCOFFSection - Return the MCSection for the specified COFF section.
300  /// FIXME: Switch this to a semantic view eventually.
301  const MCSection *getCOFFSection(const char *Name, bool isDirective,
302                                  SectionKind K) const;
303};
304
305} // end namespace llvm
306
307#endif
308