1 //===-- llvm/Target/TargetLoweringObjectFile.h - Object Info ----*- C++ -*-===//
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 // This file implements classes used to handle lowerings specific to common
11 // object file formats.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
16 #define LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
18 // FIXME: Switch to MC.
19 #include "llvm/Target/TargetAsmInfo.h"
24 /// SectionKind - This is a simple POD value that classifies the properties of
25 /// a section. A global variable is classified into the deepest possible
26 /// classification, and then the target maps them onto their sections based on
27 /// what capabilities they have.
29 /// The comments below describe these as if they were an inheritance hierarchy
30 /// in order to explain the predicates below.
34 /// Metadata - Debug info sections or other metadata.
37 /// Text - Text section, used for functions and other executable code.
40 /// ReadOnly - Data that is never written to at program runtime by the
41 /// program or the dynamic linker. Things in the top-level readonly
42 /// SectionKind are not mergeable.
45 /// MergeableCString - This is a special section for nul-terminated
46 /// strings. The linker can unique the C strings, knowing their
47 /// semantics. Because it uniques based on the nul terminators, the
48 /// compiler can't put strings in this section that have embeded nuls
52 /// MergeableConst - These are sections for merging fixed-length
53 /// constants together. For example, this can be used to unique
54 /// constant pool entries etc.
57 /// MergeableConst4 - This is a section used by 4-byte constants,
58 /// for example, floats.
61 /// MergeableConst8 - This is a section used by 8-byte constants,
62 /// for example, doubles.
65 /// MergeableConst16 - This is a section used by 16-byte constants,
66 /// for example, vectors.
69 /// Writeable - This is the base of all segments that need to be written
70 /// to during program runtime.
72 /// ThreadLocal - This is the base of all TLS segments. All TLS
73 /// objects must be writeable, otherwise there is no reason for them to
76 /// ThreadBSS - Zero-initialized TLS data objects.
79 /// ThreadData - Initialized TLS data objects.
82 /// GlobalWriteableData - Writeable data that is global (not thread
85 /// BSS - Zero initialized writeable data.
88 /// DataRel - This is the most general form of data that is written
89 /// to by the program, it can have random relocations to arbitrary
93 /// DataRelLocal - This is writeable data that has a non-zero
94 /// initializer and has relocations in it, but all of the
95 /// relocations are known to be within the final linked image
96 /// the global is linked into.
99 /// DataNoRel - This is writeable data that has a non-zero
100 /// initializer, but whose initializer is known to have no
104 /// ReadOnlyWithRel - These are global variables that are never
105 /// written to by the program, but that have relocations, so they
106 /// must be stuck in a writeable section so that the dynamic linker
107 /// can write to them. If it chooses to, the dynamic linker can
108 /// mark the pages these globals end up on as read-only after it is
109 /// done with its relocation phase.
112 /// ReadOnlyWithRelLocal - This is data that is readonly by the
113 /// program, but must be writeable so that the dynamic linker
114 /// can perform relocations in it. This is used when we know
115 /// that all the relocations are to globals in this final
124 /// Weak - This is true if the referenced symbol is weak (i.e. linkonce,
125 /// weak, weak_odr, etc). This is orthogonal from the categorization.
128 /// ExplicitSection - This is true if the global had a section explicitly
130 bool ExplicitSection : 1;
134 Kind getKind() const { return K; }
136 bool isWeak() const { return Weak; }
137 bool hasExplicitSection() const { return ExplicitSection; }
140 bool isMetadata() const { return K == Metadata; }
141 bool isText() const { return K == Text; }
143 bool isReadOnly() const {
144 return K == ReadOnly || K == MergeableCString || isMergeableConst();
147 bool isMergeableCString() const { return K == MergeableCString; }
148 bool isMergeableConst() const {
149 return K == MergeableConst || K == MergeableConst4 ||
150 K == MergeableConst8 || K == MergeableConst16;
153 bool isMergeableConst4() const { return K == MergeableConst4; }
154 bool isMergeableConst8() const { return K == MergeableConst8; }
155 bool isMergeableConst16() const { return K == MergeableConst16; }
157 bool isWriteable() const {
158 return isThreadLocal() || isGlobalWriteableData();
161 bool isThreadLocal() const {
162 return K == ThreadData || K == ThreadBSS;
165 bool isThreadBSS() const { return K == ThreadBSS; }
166 bool isThreadData() const { return K == ThreadData; }
168 bool isGlobalWriteableData() const {
169 return isBSS() || isDataRel() || isReadOnlyWithRel();
172 bool isBSS() const { return K == BSS; }
174 bool isDataRel() const {
175 return K == DataRel || K == DataRelLocal || K == DataNoRel;
178 bool isDataRelLocal() const {
179 return K == DataRelLocal || K == DataNoRel;
182 bool isDataNoRel() const { return K == DataNoRel; }
184 bool isReadOnlyWithRel() const {
185 return K == ReadOnlyWithRel || K == ReadOnlyWithRelLocal;
188 bool isReadOnlyWithRelLocal() const {
189 return K == ReadOnlyWithRelLocal;
192 static SectionKind get(Kind K, bool isWeak = false,
193 bool hasExplicitSection = false) {
197 Res.ExplicitSection = hasExplicitSection;
208 explicit Section() { }
209 Section(const std::string &N, SectionKind K) : Name(N), Kind(K) {}
210 const std::string &getName() const { return Name; }
211 SectionKind getKind() const { return Kind; }
215 class TargetLoweringObjectFile {
217 mutable StringMap<Section> Sections;
220 TargetLoweringObjectFile();
222 /// TextSection - Section directive for standard text.
224 const Section *TextSection; // Defaults to ".text".
226 /// DataSection - Section directive for standard data.
228 const Section *DataSection; // Defaults to ".data".
232 // FIXME: SINK THESE.
233 const Section *BSSSection_;
235 /// ReadOnlySection - This is the directive that is emitted to switch to a
236 /// read-only section for constant data (e.g. data declared const,
238 const Section *ReadOnlySection; // Defaults to NULL
240 /// TLSDataSection - Section directive for Thread Local data.
242 const Section *TLSDataSection; // Defaults to ".tdata".
244 /// TLSBSSSection - Section directive for Thread Local uninitialized data.
245 /// Null if this target doesn't support a BSS section.
247 const Section *TLSBSSSection; // Defaults to ".tbss".
249 const Section *CStringSection_;
253 const Section *getOrCreateSection(const char *Name,
255 SectionKind::Kind K) const;
258 virtual ~TargetLoweringObjectFile();
260 /// Initialize - this method must be called before any actual lowering is
261 /// done. This specifies the current context for codegen, and gives the
262 /// lowering implementations a chance to set up their default sections.
263 virtual void Initialize(MCContext &Ctx, const TargetMachine &TM) {}
266 const Section *getTextSection() const { return TextSection; }
267 const Section *getDataSection() const { return DataSection; }
270 /// getSectionForMergeableConstant - Given a mergeable constant with the
271 /// specified size and relocation information, return a section that it
272 /// should be placed in.
273 virtual const Section *
274 getSectionForMergeableConstant(SectionKind Kind) const;
276 /// getKindForNamedSection - If this target wants to be able to override
277 /// section flags based on the name of the section specified for a global
278 /// variable, it can implement this. This is used on ELF systems so that
279 /// ".tbss" gets the TLS bit set etc.
280 virtual SectionKind::Kind getKindForNamedSection(const char *Section,
281 SectionKind::Kind K) const{
285 /// SectionForGlobal - This method computes the appropriate section to emit
286 /// the specified global variable or function definition. This should not
287 /// be passed external (or available externally) globals.
288 const Section *SectionForGlobal(const GlobalValue *GV,
290 const TargetMachine &TM) const;
292 /// getSpecialCasedSectionGlobals - Allow the target to completely override
293 /// section assignment of a global.
294 /// FIXME: ELIMINATE this by making PIC16 implement ADDRESS with
295 /// getFlagsForNamedSection.
296 virtual const Section *
297 getSpecialCasedSectionGlobals(const GlobalValue *GV, Mangler *Mang,
298 SectionKind Kind) const {
302 /// getSectionFlagsAsString - Turn the flags in the specified SectionKind
303 /// into a string that can be printed to the assembly file after the
304 /// ".section foo" part of a section directive.
305 virtual void getSectionFlagsAsString(SectionKind Kind,
306 SmallVectorImpl<char> &Str) const {
310 virtual const Section *
311 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
312 Mangler *Mang, const TargetMachine &TM) const;
318 class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
319 bool AtIsCommentChar; // True if @ is the comment character on this target.
322 /// ELF Constructor - AtIsCommentChar is true if the CommentCharacter from TAI
324 TargetLoweringObjectFileELF(bool atIsCommentChar = false,
325 // FIXME: REMOVE AFTER UNIQUING IS FIXED.
326 bool hasCrazyBSS = false)
327 : AtIsCommentChar(atIsCommentChar), HasCrazyBSS(hasCrazyBSS) {}
329 virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
332 /// getSectionForMergeableConstant - Given a mergeable constant with the
333 /// specified size and relocation information, return a section that it
334 /// should be placed in.
335 virtual const Section *
336 getSectionForMergeableConstant(SectionKind Kind) const;
338 virtual SectionKind::Kind getKindForNamedSection(const char *Section,
339 SectionKind::Kind K) const;
340 void getSectionFlagsAsString(SectionKind Kind,
341 SmallVectorImpl<char> &Str) const;
343 virtual const Section *
344 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
345 Mangler *Mang, const TargetMachine &TM) const;
347 const Section *DataRelSection;
348 const Section *DataRelLocalSection;
349 const Section *DataRelROSection;
350 const Section *DataRelROLocalSection;
352 const Section *MergeableConst4Section;
353 const Section *MergeableConst8Section;
354 const Section *MergeableConst16Section;
359 class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
360 const Section *TextCoalSection;
361 const Section *ConstTextCoalSection;
362 const Section *ConstDataCoalSection;
363 const Section *ConstDataSection;
364 const Section *DataCoalSection;
365 const Section *FourByteConstantSection;
366 const Section *EightByteConstantSection;
367 const Section *SixteenByteConstantSection;
370 virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
372 virtual const Section *
373 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
374 Mangler *Mang, const TargetMachine &TM) const;
376 virtual const Section *
377 getSectionForMergeableConstant(SectionKind Kind) const;
382 class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
384 virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
386 virtual void getSectionFlagsAsString(SectionKind Kind,
387 SmallVectorImpl<char> &Str) const;
389 virtual const Section *
390 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
391 Mangler *Mang, const TargetMachine &TM) const;
394 } // end namespace llvm