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 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringMap.h"
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.
33 /// The comments below describe these as if they were an inheritance hierarchy
34 /// in order to explain the predicates below.
38 /// Metadata - Debug info sections or other metadata.
41 /// Text - Text section, used for functions and other executable code.
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.
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
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.
61 /// MergeableConst4 - This is a section used by 4-byte constants,
62 /// for example, floats.
65 /// MergeableConst8 - This is a section used by 8-byte constants,
66 /// for example, doubles.
69 /// MergeableConst16 - This is a section used by 16-byte constants,
70 /// for example, vectors.
73 /// Writeable - This is the base of all segments that need to be written
74 /// to during program runtime.
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
80 /// ThreadBSS - Zero-initialized TLS data objects.
83 /// ThreadData - Initialized TLS data objects.
86 /// GlobalWriteableData - Writeable data that is global (not thread
89 /// BSS - Zero initialized writeable data.
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
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.
103 /// DataNoRel - This is writeable data that has a non-zero
104 /// initializer, but whose initializer is known to have no
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.
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
128 /// Weak - This is true if the referenced symbol is weak (i.e. linkonce,
129 /// weak, weak_odr, etc). This is orthogonal from the categorization.
132 /// ExplicitSection - This is true if the global had a section explicitly
134 bool ExplicitSection : 1;
138 Kind getKind() const { return K; }
140 bool isWeak() const { return Weak; }
141 bool hasExplicitSection() const { return ExplicitSection; }
144 bool isMetadata() const { return K == Metadata; }
145 bool isText() const { return K == Text; }
147 bool isReadOnly() const {
148 return K == ReadOnly || K == MergeableCString || isMergeableConst();
151 bool isMergeableCString() const { return K == MergeableCString; }
152 bool isMergeableConst() const {
153 return K == MergeableConst || K == MergeableConst4 ||
154 K == MergeableConst8 || K == MergeableConst16;
157 bool isMergeableConst4() const { return K == MergeableConst4; }
158 bool isMergeableConst8() const { return K == MergeableConst8; }
159 bool isMergeableConst16() const { return K == MergeableConst16; }
161 bool isWriteable() const {
162 return isThreadLocal() || isGlobalWriteableData();
165 bool isThreadLocal() const {
166 return K == ThreadData || K == ThreadBSS;
169 bool isThreadBSS() const { return K == ThreadBSS; }
170 bool isThreadData() const { return K == ThreadData; }
172 bool isGlobalWriteableData() const {
173 return isBSS() || isDataRel() || isReadOnlyWithRel();
176 bool isBSS() const { return K == BSS; }
178 bool isDataRel() const {
179 return K == DataRel || K == DataRelLocal || K == DataNoRel;
182 bool isDataRelLocal() const {
183 return K == DataRelLocal || K == DataNoRel;
186 bool isDataNoRel() const { return K == DataNoRel; }
188 bool isReadOnlyWithRel() const {
189 return K == ReadOnlyWithRel || K == ReadOnlyWithRelLocal;
192 bool isReadOnlyWithRelLocal() const {
193 return K == ReadOnlyWithRelLocal;
196 static SectionKind get(Kind K, bool isWeak = false,
197 bool hasExplicitSection = false) {
201 Res.ExplicitSection = hasExplicitSection;
207 class TargetLoweringObjectFile {
211 TargetLoweringObjectFile();
213 /// TextSection - Section directive for standard text.
215 const MCSection *TextSection; // Defaults to ".text".
217 /// DataSection - Section directive for standard data.
219 const MCSection *DataSection; // Defaults to ".data".
223 // FIXME: SINK THESE.
224 const MCSection *BSSSection_;
226 /// ReadOnlySection - This is the directive that is emitted to switch to a
227 /// read-only section for constant data (e.g. data declared const,
229 const MCSection *ReadOnlySection; // Defaults to NULL
231 /// TLSDataSection - Section directive for Thread Local data.
233 const MCSection *TLSDataSection; // Defaults to ".tdata".
235 /// TLSBSSSection - Section directive for Thread Local uninitialized data.
236 /// Null if this target doesn't support a BSS section.
238 const MCSection *TLSBSSSection; // Defaults to ".tbss".
240 const MCSection *CStringSection_;
244 const MCSection *getOrCreateSection(const char *Name,
246 SectionKind::Kind K) const;
249 virtual ~TargetLoweringObjectFile();
251 /// Initialize - this method must be called before any actual lowering is
252 /// done. This specifies the current context for codegen, and gives the
253 /// lowering implementations a chance to set up their default sections.
254 virtual void Initialize(MCContext &ctx, const TargetMachine &TM) {
259 const MCSection *getTextSection() const { return TextSection; }
260 const MCSection *getDataSection() const { return DataSection; }
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 MCSection *
267 getSectionForMergeableConstant(SectionKind Kind) const;
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{
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 MCSection *SectionForGlobal(const GlobalValue *GV,
283 const TargetMachine &TM) const;
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 MCSection *
290 getSpecialCasedSectionGlobals(const GlobalValue *GV, Mangler *Mang,
291 SectionKind Kind) const {
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 {
303 virtual const MCSection *
304 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
305 Mangler *Mang, const TargetMachine &TM) const;
311 class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
312 bool AtIsCommentChar; // True if @ is the comment character on this target.
315 /// ELF Constructor - AtIsCommentChar is true if the CommentCharacter from TAI
317 TargetLoweringObjectFileELF(bool atIsCommentChar = false,
318 // FIXME: REMOVE AFTER UNIQUING IS FIXED.
319 bool hasCrazyBSS = false)
320 : AtIsCommentChar(atIsCommentChar), HasCrazyBSS(hasCrazyBSS) {}
322 virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
325 /// getSectionForMergeableConstant - Given a mergeable constant with the
326 /// specified size and relocation information, return a section that it
327 /// should be placed in.
328 virtual const MCSection *
329 getSectionForMergeableConstant(SectionKind Kind) const;
331 virtual SectionKind::Kind getKindForNamedSection(const char *Section,
332 SectionKind::Kind K) const;
333 void getSectionFlagsAsString(SectionKind Kind,
334 SmallVectorImpl<char> &Str) const;
336 virtual const MCSection *
337 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
338 Mangler *Mang, const TargetMachine &TM) const;
340 const MCSection *DataRelSection;
341 const MCSection *DataRelLocalSection;
342 const MCSection *DataRelROSection;
343 const MCSection *DataRelROLocalSection;
345 const MCSection *MergeableConst4Section;
346 const MCSection *MergeableConst8Section;
347 const MCSection *MergeableConst16Section;
352 class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
353 const MCSection *TextCoalSection;
354 const MCSection *ConstTextCoalSection;
355 const MCSection *ConstDataCoalSection;
356 const MCSection *ConstDataSection;
357 const MCSection *DataCoalSection;
358 const MCSection *FourByteConstantSection;
359 const MCSection *EightByteConstantSection;
360 const MCSection *SixteenByteConstantSection;
363 virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
365 virtual const MCSection *
366 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
367 Mangler *Mang, const TargetMachine &TM) const;
369 virtual const MCSection *
370 getSectionForMergeableConstant(SectionKind Kind) const;
375 class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
377 virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
379 virtual void getSectionFlagsAsString(SectionKind Kind,
380 SmallVectorImpl<char> &Str) const;
382 virtual const MCSection *
383 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
384 Mangler *Mang, const TargetMachine &TM) const;
387 } // end namespace llvm