blob: 868b274b30dbc0e3540e65706bbca3b1059bccb7 [file] [log] [blame]
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001//===-- 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
Chris Lattnere6ad12f2009-07-31 18:48:30 +000018#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/StringMap.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000020
21namespace llvm {
Chris Lattnere6ad12f2009-07-31 18:48:30 +000022 class MCSection;
Chris Lattner01316272009-07-31 17:42:42 +000023 class MCContext;
Chris Lattnere6ad12f2009-07-31 18:48:30 +000024 class GlobalValue;
25 class Mangler;
26 class TargetMachine;
27
Chris Lattnerc5c3e942009-07-31 16:47:16 +000028/// 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
125private:
126 Kind K : 6;
127
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.
130 bool Weak : 1;
131
132 /// ExplicitSection - This is true if the global had a section explicitly
133 /// specified on it.
134 bool ExplicitSection : 1;
135public:
136
137 // FIXME: REMOVE.
138 Kind getKind() const { return K; }
139
140 bool isWeak() const { return Weak; }
141 bool hasExplicitSection() const { return ExplicitSection; }
142
143
144 bool isMetadata() const { return K == Metadata; }
145 bool isText() const { return K == Text; }
146
147 bool isReadOnly() const {
148 return K == ReadOnly || K == MergeableCString || isMergeableConst();
149 }
150
151 bool isMergeableCString() const { return K == MergeableCString; }
152 bool isMergeableConst() const {
153 return K == MergeableConst || K == MergeableConst4 ||
154 K == MergeableConst8 || K == MergeableConst16;
155 }
156
157 bool isMergeableConst4() const { return K == MergeableConst4; }
158 bool isMergeableConst8() const { return K == MergeableConst8; }
159 bool isMergeableConst16() const { return K == MergeableConst16; }
160
161 bool isWriteable() const {
162 return isThreadLocal() || isGlobalWriteableData();
163 }
164
165 bool isThreadLocal() const {
166 return K == ThreadData || K == ThreadBSS;
167 }
168
169 bool isThreadBSS() const { return K == ThreadBSS; }
170 bool isThreadData() const { return K == ThreadData; }
171
172 bool isGlobalWriteableData() const {
173 return isBSS() || isDataRel() || isReadOnlyWithRel();
174 }
175
176 bool isBSS() const { return K == BSS; }
177
178 bool isDataRel() const {
179 return K == DataRel || K == DataRelLocal || K == DataNoRel;
180 }
181
182 bool isDataRelLocal() const {
183 return K == DataRelLocal || K == DataNoRel;
184 }
185
186 bool isDataNoRel() const { return K == DataNoRel; }
187
188 bool isReadOnlyWithRel() const {
189 return K == ReadOnlyWithRel || K == ReadOnlyWithRelLocal;
190 }
191
192 bool isReadOnlyWithRelLocal() const {
193 return K == ReadOnlyWithRelLocal;
194 }
195
196 static SectionKind get(Kind K, bool isWeak = false,
197 bool hasExplicitSection = false) {
198 SectionKind Res;
199 Res.K = K;
200 Res.Weak = isWeak;
201 Res.ExplicitSection = hasExplicitSection;
202 return Res;
203 }
204};
205
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000206
207class TargetLoweringObjectFile {
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000208 MCContext *Ctx;
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000209protected:
210
211 TargetLoweringObjectFile();
212
213 /// TextSection - Section directive for standard text.
214 ///
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000215 const MCSection *TextSection; // Defaults to ".text".
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000216
217 /// DataSection - Section directive for standard data.
218 ///
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000219 const MCSection *DataSection; // Defaults to ".data".
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000220
221
222
223 // FIXME: SINK THESE.
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000224 const MCSection *BSSSection_;
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000225
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,
228 /// jump tables).
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000229 const MCSection *ReadOnlySection; // Defaults to NULL
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000230
231 /// TLSDataSection - Section directive for Thread Local data.
232 ///
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000233 const MCSection *TLSDataSection; // Defaults to ".tdata".
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000234
235 /// TLSBSSSection - Section directive for Thread Local uninitialized data.
236 /// Null if this target doesn't support a BSS section.
237 ///
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000238 const MCSection *TLSBSSSection; // Defaults to ".tbss".
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000239
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000240 const MCSection *CStringSection_;
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000241
242public:
243 // FIXME: NONPUB.
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000244 const MCSection *getOrCreateSection(const char *Name,
245 bool isDirective,
246 SectionKind::Kind K) const;
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000247public:
248
249 virtual ~TargetLoweringObjectFile();
250
Chris Lattner01316272009-07-31 17:42:42 +0000251 /// 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.
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000254 virtual void Initialize(MCContext &ctx, const TargetMachine &TM) {
255 Ctx = &ctx;
256 }
Chris Lattner01316272009-07-31 17:42:42 +0000257
258
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000259 const MCSection *getTextSection() const { return TextSection; }
260 const MCSection *getDataSection() const { return DataSection; }
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000261
Chris Lattner84362ac2009-07-31 20:52:39 +0000262 /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively
263 /// decide not to emit the UsedDirective for some symbols in llvm.used.
264 /// FIXME: REMOVE this (rdar://7071300)
265 virtual bool shouldEmitUsedDirectiveFor(const GlobalValue *GV,
266 Mangler *) const {
267 return (GV!=0);
268 }
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000269
270 /// getSectionForMergeableConstant - Given a mergeable constant with the
271 /// specified size and relocation information, return a section that it
272 /// should be placed in.
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000273 virtual const MCSection *
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000274 getSectionForMergeableConstant(SectionKind Kind) const;
275
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{
282 return K;
283 }
284
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.
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000288 const MCSection *SectionForGlobal(const GlobalValue *GV,
289 Mangler *Mang,
290 const TargetMachine &TM) const;
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000291
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.
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000296 virtual const MCSection *
Chris Lattner2931fe42009-07-29 05:09:30 +0000297 getSpecialCasedSectionGlobals(const GlobalValue *GV, Mangler *Mang,
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000298 SectionKind Kind) const {
299 return 0;
300 }
301
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 {
307 }
308
309protected:
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000310 virtual const MCSection *
Chris Lattner2931fe42009-07-29 05:09:30 +0000311 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
312 Mangler *Mang, const TargetMachine &TM) const;
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000313};
314
315
316
317
318class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
319 bool AtIsCommentChar; // True if @ is the comment character on this target.
Chris Lattner01316272009-07-31 17:42:42 +0000320 bool HasCrazyBSS;
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000321public:
322 /// ELF Constructor - AtIsCommentChar is true if the CommentCharacter from TAI
323 /// is "@".
Chris Lattner01316272009-07-31 17:42:42 +0000324 TargetLoweringObjectFileELF(bool atIsCommentChar = false,
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000325 // FIXME: REMOVE AFTER UNIQUING IS FIXED.
Chris Lattner01316272009-07-31 17:42:42 +0000326 bool hasCrazyBSS = false)
327 : AtIsCommentChar(atIsCommentChar), HasCrazyBSS(hasCrazyBSS) {}
328
329 virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
330
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000331
332 /// getSectionForMergeableConstant - Given a mergeable constant with the
333 /// specified size and relocation information, return a section that it
334 /// should be placed in.
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000335 virtual const MCSection *
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000336 getSectionForMergeableConstant(SectionKind Kind) const;
337
338 virtual SectionKind::Kind getKindForNamedSection(const char *Section,
339 SectionKind::Kind K) const;
340 void getSectionFlagsAsString(SectionKind Kind,
341 SmallVectorImpl<char> &Str) const;
342
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000343 virtual const MCSection *
Chris Lattner2931fe42009-07-29 05:09:30 +0000344 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
345 Mangler *Mang, const TargetMachine &TM) const;
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000346protected:
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000347 const MCSection *DataRelSection;
348 const MCSection *DataRelLocalSection;
349 const MCSection *DataRelROSection;
350 const MCSection *DataRelROLocalSection;
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000351
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000352 const MCSection *MergeableConst4Section;
353 const MCSection *MergeableConst8Section;
354 const MCSection *MergeableConst16Section;
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000355};
356
Chris Lattner01316272009-07-31 17:42:42 +0000357
358
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000359class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000360 const MCSection *TextCoalSection;
361 const MCSection *ConstTextCoalSection;
362 const MCSection *ConstDataCoalSection;
363 const MCSection *ConstDataSection;
364 const MCSection *DataCoalSection;
365 const MCSection *FourByteConstantSection;
366 const MCSection *EightByteConstantSection;
367 const MCSection *SixteenByteConstantSection;
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000368public:
Chris Lattner01316272009-07-31 17:42:42 +0000369
370 virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
371
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000372 virtual const MCSection *
Chris Lattner2931fe42009-07-29 05:09:30 +0000373 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
374 Mangler *Mang, const TargetMachine &TM) const;
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000375
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000376 virtual const MCSection *
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000377 getSectionForMergeableConstant(SectionKind Kind) const;
Chris Lattner84362ac2009-07-31 20:52:39 +0000378
379 /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively
380 /// decide not to emit the UsedDirective for some symbols in llvm.used.
381 /// FIXME: REMOVE this (rdar://7071300)
382 virtual bool shouldEmitUsedDirectiveFor(const GlobalValue *GV,
383 Mangler *) const;
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000384};
385
386
387
388class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
389public:
Chris Lattner01316272009-07-31 17:42:42 +0000390 virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
391
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000392 virtual void getSectionFlagsAsString(SectionKind Kind,
393 SmallVectorImpl<char> &Str) const;
394
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000395 virtual const MCSection *
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000396 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner2931fe42009-07-29 05:09:30 +0000397 Mangler *Mang, const TargetMachine &TM) const;
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000398};
399
400} // end namespace llvm
401
402#endif