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