TargetLoweringObjectFile.h revision 968ff1196768c0b6dbcc5508025a2923bfa73fab
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
21namespace llvm {
22  class MCSection;
23  class MCContext;
24  class GlobalValue;
25  class Mangler;
26  class TargetMachine;
27
28/// SectionKind - This is a simple POD value that classifies the properties of
29/// a section.  A global variable is classified into the deepest possible
30/// classification, and then the target maps them onto their sections based on
31/// what capabilities they have.
32///
33/// The comments below describe these as if they were an inheritance hierarchy
34/// in order to explain the predicates below.
35class SectionKind {
36public:
37  enum Kind {
38    /// Metadata - Debug info sections or other metadata.
39    Metadata,
40
41    /// Text - Text section, used for functions and other executable code.
42    Text,
43
44    /// ReadOnly - Data that is never written to at program runtime by the
45    /// program or the dynamic linker.  Things in the top-level readonly
46    /// SectionKind are not mergeable.
47    ReadOnly,
48
49        /// MergeableCString - This is a special section for nul-terminated
50        /// strings.  The linker can unique the C strings, knowing their
51        /// semantics.  Because it uniques based on the nul terminators, the
52        /// compiler can't put strings in this section that have embeded nuls
53        /// in them.
54        MergeableCString,
55
56        /// MergeableConst - These are sections for merging fixed-length
57        /// constants together.  For example, this can be used to unique
58        /// constant pool entries etc.
59        MergeableConst,
60
61            /// MergeableConst4 - This is a section used by 4-byte constants,
62            /// for example, floats.
63            MergeableConst4,
64
65            /// MergeableConst8 - This is a section used by 8-byte constants,
66            /// for example, doubles.
67            MergeableConst8,
68
69            /// MergeableConst16 - This is a section used by 16-byte constants,
70            /// for example, vectors.
71            MergeableConst16,
72
73    /// Writeable - This is the base of all segments that need to be written
74    /// to during program runtime.
75
76       /// ThreadLocal - This is the base of all TLS segments.  All TLS
77       /// objects must be writeable, otherwise there is no reason for them to
78       /// be thread local!
79
80           /// ThreadBSS - Zero-initialized TLS data objects.
81           ThreadBSS,
82
83           /// ThreadData - Initialized TLS data objects.
84           ThreadData,
85
86       /// GlobalWriteableData - Writeable data that is global (not thread
87       /// local).
88
89           /// BSS - Zero initialized writeable data.
90           BSS,
91
92           /// DataRel - This is the most general form of data that is written
93           /// to by the program, it can have random relocations to arbitrary
94           /// globals.
95           DataRel,
96
97               /// DataRelLocal - This is writeable data that has a non-zero
98               /// initializer and has relocations in it, but all of the
99               /// relocations are known to be within the final linked image
100               /// the global is linked into.
101               DataRelLocal,
102
103                   /// DataNoRel - This is writeable data that has a non-zero
104                   /// initializer, but whose initializer is known to have no
105                   /// relocations.
106                   DataNoRel,
107
108           /// ReadOnlyWithRel - These are global variables that are never
109           /// written to by the program, but that have relocations, so they
110           /// must be stuck in a writeable section so that the dynamic linker
111           /// can write to them.  If it chooses to, the dynamic linker can
112           /// mark the pages these globals end up on as read-only after it is
113           /// done with its relocation phase.
114           ReadOnlyWithRel,
115
116               /// ReadOnlyWithRelLocal - This is data that is readonly by the
117               /// program, but must be writeable so that the dynamic linker
118               /// can perform relocations in it.  This is used when we know
119               /// that all the relocations are to globals in this final
120               /// linked image.
121               ReadOnlyWithRelLocal
122
123  };
124
125protected:
126  Kind K : 6;
127
128public:
129
130  // FIXME: REMOVE.
131  Kind getKind() const { return K; }
132
133  bool isMetadata() const { return K == Metadata; }
134  bool isText() const { return K == Text; }
135
136  bool isReadOnly() const {
137    return K == ReadOnly || K == MergeableCString || isMergeableConst();
138  }
139
140  bool isMergeableCString() const { return K == MergeableCString; }
141  bool isMergeableConst() const {
142    return K == MergeableConst || K == MergeableConst4 ||
143           K == MergeableConst8 || K == MergeableConst16;
144  }
145
146  bool isMergeableConst4() const { return K == MergeableConst4; }
147  bool isMergeableConst8() const { return K == MergeableConst8; }
148  bool isMergeableConst16() const { return K == MergeableConst16; }
149
150  bool isWriteable() const {
151    return isThreadLocal() || isGlobalWriteableData();
152  }
153
154  bool isThreadLocal() const {
155    return K == ThreadData || K == ThreadBSS;
156  }
157
158  bool isThreadBSS() const { return K == ThreadBSS; }
159  bool isThreadData() const { return K == ThreadData; }
160
161  bool isGlobalWriteableData() const {
162    return isBSS() || isDataRel() || isReadOnlyWithRel();
163  }
164
165  bool isBSS() const { return K == BSS; }
166
167  bool isDataRel() const {
168    return K == DataRel || K == DataRelLocal || K == DataNoRel;
169  }
170
171  bool isDataRelLocal() const {
172    return K == DataRelLocal || K == DataNoRel;
173  }
174
175  bool isDataNoRel() const { return K == DataNoRel; }
176
177  bool isReadOnlyWithRel() const {
178    return K == ReadOnlyWithRel || K == ReadOnlyWithRelLocal;
179  }
180
181  bool isReadOnlyWithRelLocal() const {
182    return K == ReadOnlyWithRelLocal;
183  }
184
185  static SectionKind get(Kind K) {
186    SectionKind Res;
187    Res.K = K;
188    return Res;
189  }
190};
191
192
193/// SectionInfo - This class is a target-independent classification of a global
194/// which is used to simplify target-specific code by exposing common
195/// predicates.
196class SectionInfo : public SectionKind {
197  /// Weak - This is true if the referenced symbol is weak (i.e. linkonce,
198  /// weak, weak_odr, etc).  This is orthogonal from the categorization.
199  bool Weak : 1;
200
201public:
202
203  /// Weak - This is true if the referenced symbol is weak (i.e. linkonce,
204  /// weak, weak_odr, etc).  This is orthogonal from the categorization.
205  bool isWeak() const { return Weak; }
206
207  static SectionInfo get(Kind K, bool isWeak = false) {
208    SectionInfo Res;
209    Res.K = K;
210    Res.Weak = isWeak;
211    return Res;
212  }
213  static SectionInfo get(SectionKind K, bool isWeak = false) {
214    SectionInfo Res;
215    *(SectionKind*)&Res = K;
216    Res.Weak = isWeak;
217    return Res;
218  }
219};
220
221class TargetLoweringObjectFile {
222  MCContext *Ctx;
223protected:
224
225  TargetLoweringObjectFile();
226
227  /// TextSection - Section directive for standard text.
228  ///
229  const MCSection *TextSection;           // Defaults to ".text".
230
231  /// DataSection - Section directive for standard data.
232  ///
233  const MCSection *DataSection;           // Defaults to ".data".
234
235
236
237  // FIXME: SINK THESE.
238  const MCSection *BSSSection_;
239
240  /// ReadOnlySection - This is the directive that is emitted to switch to a
241  /// read-only section for constant data (e.g. data declared const,
242  /// jump tables).
243  const MCSection *ReadOnlySection;       // Defaults to NULL
244
245  /// TLSDataSection - Section directive for Thread Local data.
246  ///
247  const MCSection *TLSDataSection;        // Defaults to ".tdata".
248
249  /// TLSBSSSection - Section directive for Thread Local uninitialized data.
250  /// Null if this target doesn't support a BSS section.
251  ///
252  const MCSection *TLSBSSSection;         // Defaults to ".tbss".
253
254  const MCSection *CStringSection_;
255
256public:
257  // FIXME: NONPUB.
258  const MCSection *getOrCreateSection(const char *Name,
259                                      bool isDirective,
260                                      SectionKind K) const;
261public:
262
263  virtual ~TargetLoweringObjectFile();
264
265  /// Initialize - this method must be called before any actual lowering is
266  /// done.  This specifies the current context for codegen, and gives the
267  /// lowering implementations a chance to set up their default sections.
268  virtual void Initialize(MCContext &ctx, const TargetMachine &TM) {
269    Ctx = &ctx;
270  }
271
272
273  const MCSection *getTextSection() const { return TextSection; }
274  const MCSection *getDataSection() const { return DataSection; }
275
276  /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively
277  /// decide not to emit the UsedDirective for some symbols in llvm.used.
278  /// FIXME: REMOVE this (rdar://7071300)
279  virtual bool shouldEmitUsedDirectiveFor(const GlobalValue *GV,
280                                          Mangler *) const {
281    return (GV!=0);
282  }
283
284  /// getSectionForMergeableConstant - Given a mergeable constant with the
285  /// specified size and relocation information, return a section that it
286  /// should be placed in.
287  virtual const MCSection *
288  getSectionForMergeableConstant(SectionKind Kind) const;
289
290  /// getKindForNamedSection - If this target wants to be able to override
291  /// section flags based on the name of the section specified for a global
292  /// variable, it can implement this.  This is used on ELF systems so that
293  /// ".tbss" gets the TLS bit set etc.
294  virtual SectionKind getKindForNamedSection(const char *Section,
295                                             SectionKind K) const {
296    return K;
297  }
298
299  /// SectionForGlobal - This method computes the appropriate section to emit
300  /// the specified global variable or function definition.  This should not
301  /// be passed external (or available externally) globals.
302  const MCSection *SectionForGlobal(const GlobalValue *GV,
303                                    Mangler *Mang,
304                                    const TargetMachine &TM) const;
305
306  /// getSpecialCasedSectionGlobals - Allow the target to completely override
307  /// section assignment of a global.
308  /// FIXME: ELIMINATE this by making PIC16 implement ADDRESS with
309  /// getFlagsForNamedSection.
310  virtual const MCSection *
311  getSpecialCasedSectionGlobals(const GlobalValue *GV, Mangler *Mang,
312                                SectionInfo Kind) const {
313    return 0;
314  }
315
316  /// getSectionFlagsAsString - Turn the flags in the specified SectionKind
317  /// into a string that can be printed to the assembly file after the
318  /// ".section foo" part of a section directive.
319  virtual void getSectionFlagsAsString(SectionKind Kind,
320                                       SmallVectorImpl<char> &Str) const {
321  }
322
323protected:
324  virtual const MCSection *
325  SelectSectionForGlobal(const GlobalValue *GV, SectionInfo Kind,
326                         Mangler *Mang, const TargetMachine &TM) const;
327};
328
329
330
331
332class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
333  bool AtIsCommentChar;  // True if @ is the comment character on this target.
334  bool HasCrazyBSS;
335public:
336  /// ELF Constructor - AtIsCommentChar is true if the CommentCharacter from TAI
337  /// is "@".
338  TargetLoweringObjectFileELF(bool atIsCommentChar = false,
339                              // FIXME: REMOVE AFTER UNIQUING IS FIXED.
340                              bool hasCrazyBSS = false)
341    : AtIsCommentChar(atIsCommentChar), HasCrazyBSS(hasCrazyBSS) {}
342
343  virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
344
345
346  /// getSectionForMergeableConstant - Given a mergeable constant with the
347  /// specified size and relocation information, return a section that it
348  /// should be placed in.
349  virtual const MCSection *
350  getSectionForMergeableConstant(SectionKind Kind) const;
351
352  virtual SectionKind getKindForNamedSection(const char *Section,
353                                             SectionKind K) const;
354  void getSectionFlagsAsString(SectionKind Kind,
355                               SmallVectorImpl<char> &Str) const;
356
357  virtual const MCSection *
358  SelectSectionForGlobal(const GlobalValue *GV, SectionInfo Kind,
359                         Mangler *Mang, const TargetMachine &TM) const;
360protected:
361  const MCSection *DataRelSection;
362  const MCSection *DataRelLocalSection;
363  const MCSection *DataRelROSection;
364  const MCSection *DataRelROLocalSection;
365
366  const MCSection *MergeableConst4Section;
367  const MCSection *MergeableConst8Section;
368  const MCSection *MergeableConst16Section;
369};
370
371
372
373class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
374  const MCSection *TextCoalSection;
375  const MCSection *ConstTextCoalSection;
376  const MCSection *ConstDataCoalSection;
377  const MCSection *ConstDataSection;
378  const MCSection *DataCoalSection;
379  const MCSection *FourByteConstantSection;
380  const MCSection *EightByteConstantSection;
381  const MCSection *SixteenByteConstantSection;
382public:
383
384  virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
385
386  virtual const MCSection *
387  SelectSectionForGlobal(const GlobalValue *GV, SectionInfo Kind,
388                         Mangler *Mang, const TargetMachine &TM) const;
389
390  virtual const MCSection *
391  getSectionForMergeableConstant(SectionKind Kind) const;
392
393  /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively
394  /// decide not to emit the UsedDirective for some symbols in llvm.used.
395  /// FIXME: REMOVE this (rdar://7071300)
396  virtual bool shouldEmitUsedDirectiveFor(const GlobalValue *GV,
397                                          Mangler *) const;
398};
399
400
401
402class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
403public:
404  virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
405
406  virtual void getSectionFlagsAsString(SectionKind Kind,
407                                       SmallVectorImpl<char> &Str) const;
408
409  virtual const MCSection *
410  SelectSectionForGlobal(const GlobalValue *GV, SectionInfo Kind,
411                         Mangler *Mang, const TargetMachine &TM) const;
412};
413
414} // end namespace llvm
415
416#endif
417