1 //===-- llvm/MC/MCObjectFileInfo.h - Object File Info -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file describes common object file formats.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_MC_MCOBJECTFILEINFO_H
14 #define LLVM_MC_MCOBJECTFILEINFO_H
15 
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Support/CodeGen.h"
20 #include "llvm/Support/VersionTuple.h"
21 
22 namespace llvm {
23 class MCContext;
24 class MCSection;
25 
26 class MCObjectFileInfo {
27 protected:
28   /// True if .comm supports alignment.  This is a hack for as long as we
29   /// support 10.4 Tiger, whose assembler doesn't support alignment on comm.
30   bool CommDirectiveSupportsAlignment = false;
31 
32   /// True if target object file supports a weak_definition of constant 0 for an
33   /// omitted EH frame.
34   bool SupportsWeakOmittedEHFrame = false;
35 
36   /// True if the target object file supports emitting a compact unwind section
37   /// without an associated EH frame section.
38   bool SupportsCompactUnwindWithoutEHFrame = false;
39 
40   /// OmitDwarfIfHaveCompactUnwind - True if the target object file
41   /// supports having some functions with compact unwind and other with
42   /// dwarf unwind.
43   bool OmitDwarfIfHaveCompactUnwind = false;
44 
45   /// FDE CFI encoding. Controls the encoding of the begin label in the
46   /// .eh_frame section. Unlike the LSDA encoding, personality encoding, and
47   /// type encodings, this is something that the assembler just "knows" about
48   /// its target
49   unsigned FDECFIEncoding = 0;
50 
51   /// Compact unwind encoding indicating that we should emit only an EH frame.
52   unsigned CompactUnwindDwarfEHFrameOnly = 0;
53 
54   /// Section directive for standard text.
55   MCSection *TextSection = nullptr;
56 
57   /// Section directive for standard data.
58   MCSection *DataSection = nullptr;
59 
60   /// Section that is default initialized to zero.
61   MCSection *BSSSection = nullptr;
62 
63   /// Section that is readonly and can contain arbitrary initialized data.
64   /// Targets are not required to have a readonly section. If they don't,
65   /// various bits of code will fall back to using the data section for
66   /// constants.
67   MCSection *ReadOnlySection = nullptr;
68 
69   /// If exception handling is supported by the target, this is the section the
70   /// Language Specific Data Area information is emitted to.
71   MCSection *LSDASection = nullptr;
72 
73   /// If exception handling is supported by the target and the target can
74   /// support a compact representation of the CIE and FDE, this is the section
75   /// to emit them into.
76   MCSection *CompactUnwindSection = nullptr;
77 
78   // Dwarf sections for debug info.  If a target supports debug info, these must
79   // be set.
80   MCSection *DwarfAbbrevSection = nullptr;
81   MCSection *DwarfInfoSection = nullptr;
82   MCSection *DwarfLineSection = nullptr;
83   MCSection *DwarfLineStrSection = nullptr;
84   MCSection *DwarfFrameSection = nullptr;
85   MCSection *DwarfPubTypesSection = nullptr;
86   const MCSection *DwarfDebugInlineSection = nullptr;
87   MCSection *DwarfStrSection = nullptr;
88   MCSection *DwarfLocSection = nullptr;
89   MCSection *DwarfARangesSection = nullptr;
90   MCSection *DwarfRangesSection = nullptr;
91   MCSection *DwarfMacinfoSection = nullptr;
92   // The pubnames section is no longer generated by default.  The generation
93   // can be enabled by a compiler flag.
94   MCSection *DwarfPubNamesSection = nullptr;
95 
96   /// Accelerator table sections. DwarfDebugNamesSection is the DWARF v5
97   /// accelerator table, while DwarfAccelNamesSection, DwarfAccelObjCSection,
98   /// DwarfAccelNamespaceSection, DwarfAccelTypesSection are pre-DWARF v5
99   /// extensions.
100   MCSection *DwarfDebugNamesSection = nullptr;
101   MCSection *DwarfAccelNamesSection = nullptr;
102   MCSection *DwarfAccelObjCSection = nullptr;
103   MCSection *DwarfAccelNamespaceSection = nullptr;
104   MCSection *DwarfAccelTypesSection = nullptr;
105 
106   // These are used for the Fission separate debug information files.
107   MCSection *DwarfInfoDWOSection = nullptr;
108   MCSection *DwarfTypesDWOSection = nullptr;
109   MCSection *DwarfAbbrevDWOSection = nullptr;
110   MCSection *DwarfStrDWOSection = nullptr;
111   MCSection *DwarfLineDWOSection = nullptr;
112   MCSection *DwarfLocDWOSection = nullptr;
113   MCSection *DwarfStrOffDWOSection = nullptr;
114   MCSection *DwarfMacinfoDWOSection = nullptr;
115 
116   /// The DWARF v5 string offset and address table sections.
117   MCSection *DwarfStrOffSection = nullptr;
118   MCSection *DwarfAddrSection = nullptr;
119   /// The DWARF v5 range list section.
120   MCSection *DwarfRnglistsSection = nullptr;
121   /// The DWARF v5 locations list section.
122   MCSection *DwarfLoclistsSection = nullptr;
123 
124   /// The DWARF v5 range and location list sections for fission.
125   MCSection *DwarfRnglistsDWOSection = nullptr;
126   MCSection *DwarfLoclistsDWOSection = nullptr;
127 
128   // These are for Fission DWP files.
129   MCSection *DwarfCUIndexSection = nullptr;
130   MCSection *DwarfTUIndexSection = nullptr;
131 
132   /// Section for newer gnu pubnames.
133   MCSection *DwarfGnuPubNamesSection = nullptr;
134   /// Section for newer gnu pubtypes.
135   MCSection *DwarfGnuPubTypesSection = nullptr;
136 
137   // Section for Swift AST
138   MCSection *DwarfSwiftASTSection = nullptr;
139 
140   MCSection *COFFDebugSymbolsSection = nullptr;
141   MCSection *COFFDebugTypesSection = nullptr;
142   MCSection *COFFGlobalTypeHashesSection = nullptr;
143 
144   /// Extra TLS Variable Data section.
145   ///
146   /// If the target needs to put additional information for a TLS variable,
147   /// it'll go here.
148   MCSection *TLSExtraDataSection = nullptr;
149 
150   /// Section directive for Thread Local data. ELF, MachO, COFF, and Wasm.
151   MCSection *TLSDataSection = nullptr; // Defaults to ".tdata".
152 
153   /// Section directive for Thread Local uninitialized data.
154   ///
155   /// Null if this target doesn't support a BSS section. ELF and MachO only.
156   MCSection *TLSBSSSection = nullptr; // Defaults to ".tbss".
157 
158   /// StackMap section.
159   MCSection *StackMapSection = nullptr;
160 
161   /// FaultMap section.
162   MCSection *FaultMapSection = nullptr;
163 
164   /// Remarks section.
165   MCSection *RemarksSection = nullptr;
166 
167   /// EH frame section.
168   ///
169   /// It is initialized on demand so it can be overwritten (with uniquing).
170   MCSection *EHFrameSection = nullptr;
171 
172   /// Section containing metadata on function stack sizes.
173   MCSection *StackSizesSection = nullptr;
174   mutable DenseMap<const MCSymbol *, unsigned> StackSizesUniquing;
175 
176   // ELF specific sections.
177   MCSection *DataRelROSection = nullptr;
178   MCSection *MergeableConst4Section = nullptr;
179   MCSection *MergeableConst8Section = nullptr;
180   MCSection *MergeableConst16Section = nullptr;
181   MCSection *MergeableConst32Section = nullptr;
182 
183   // MachO specific sections.
184 
185   /// Section for thread local structure information.
186   ///
187   /// Contains the source code name of the variable, visibility and a pointer to
188   /// the initial value (.tdata or .tbss).
189   MCSection *TLSTLVSection = nullptr; // Defaults to ".tlv".
190 
191   /// Section for thread local data initialization functions.
192    // Defaults to ".thread_init_func".
193   const MCSection *TLSThreadInitSection = nullptr;
194 
195   MCSection *CStringSection = nullptr;
196   MCSection *UStringSection = nullptr;
197   MCSection *TextCoalSection = nullptr;
198   MCSection *ConstTextCoalSection = nullptr;
199   MCSection *ConstDataSection = nullptr;
200   MCSection *DataCoalSection = nullptr;
201   MCSection *ConstDataCoalSection = nullptr;
202   MCSection *DataCommonSection = nullptr;
203   MCSection *DataBSSSection = nullptr;
204   MCSection *FourByteConstantSection = nullptr;
205   MCSection *EightByteConstantSection = nullptr;
206   MCSection *SixteenByteConstantSection = nullptr;
207   MCSection *LazySymbolPointerSection = nullptr;
208   MCSection *NonLazySymbolPointerSection = nullptr;
209   MCSection *ThreadLocalPointerSection = nullptr;
210 
211   /// COFF specific sections.
212   MCSection *DrectveSection = nullptr;
213   MCSection *PDataSection = nullptr;
214   MCSection *XDataSection = nullptr;
215   MCSection *SXDataSection = nullptr;
216   MCSection *GFIDsSection = nullptr;
217   MCSection *GLJMPSection = nullptr;
218 
219 public:
220   void InitMCObjectFileInfo(const Triple &TT, bool PIC, MCContext &ctx,
221                             bool LargeCodeModel = false);
222 
223   bool getSupportsWeakOmittedEHFrame() const {
224     return SupportsWeakOmittedEHFrame;
225   }
226   bool getSupportsCompactUnwindWithoutEHFrame() const {
227     return SupportsCompactUnwindWithoutEHFrame;
228   }
229   bool getOmitDwarfIfHaveCompactUnwind() const {
230     return OmitDwarfIfHaveCompactUnwind;
231   }
232 
233   bool getCommDirectiveSupportsAlignment() const {
234     return CommDirectiveSupportsAlignment;
235   }
236 
237   unsigned getFDEEncoding() const { return FDECFIEncoding; }
238 
239   unsigned getCompactUnwindDwarfEHFrameOnly() const {
240     return CompactUnwindDwarfEHFrameOnly;
241   }
242 
243   MCSection *getTextSection() const { return TextSection; }
244   MCSection *getDataSection() const { return DataSection; }
245   MCSection *getBSSSection() const { return BSSSection; }
246   MCSection *getReadOnlySection() const { return ReadOnlySection; }
247   MCSection *getLSDASection() const { return LSDASection; }
248   MCSection *getCompactUnwindSection() const { return CompactUnwindSection; }
249   MCSection *getDwarfAbbrevSection() const { return DwarfAbbrevSection; }
250   MCSection *getDwarfInfoSection() const { return DwarfInfoSection; }
251   MCSection *getDwarfInfoSection(uint64_t Hash) const {
252     return getDwarfComdatSection(".debug_info", Hash);
253   }
254   MCSection *getDwarfLineSection() const { return DwarfLineSection; }
255   MCSection *getDwarfLineStrSection() const { return DwarfLineStrSection; }
256   MCSection *getDwarfFrameSection() const { return DwarfFrameSection; }
257   MCSection *getDwarfPubNamesSection() const { return DwarfPubNamesSection; }
258   MCSection *getDwarfPubTypesSection() const { return DwarfPubTypesSection; }
259   MCSection *getDwarfGnuPubNamesSection() const {
260     return DwarfGnuPubNamesSection;
261   }
262   MCSection *getDwarfGnuPubTypesSection() const {
263     return DwarfGnuPubTypesSection;
264   }
265   const MCSection *getDwarfDebugInlineSection() const {
266     return DwarfDebugInlineSection;
267   }
268   MCSection *getDwarfStrSection() const { return DwarfStrSection; }
269   MCSection *getDwarfLocSection() const { return DwarfLocSection; }
270   MCSection *getDwarfARangesSection() const { return DwarfARangesSection; }
271   MCSection *getDwarfRangesSection() const { return DwarfRangesSection; }
272   MCSection *getDwarfRnglistsSection() const { return DwarfRnglistsSection; }
273   MCSection *getDwarfLoclistsSection() const { return DwarfLoclistsSection; }
274   MCSection *getDwarfMacinfoSection() const { return DwarfMacinfoSection; }
275 
276   MCSection *getDwarfDebugNamesSection() const {
277     return DwarfDebugNamesSection;
278   }
279   MCSection *getDwarfAccelNamesSection() const {
280     return DwarfAccelNamesSection;
281   }
282   MCSection *getDwarfAccelObjCSection() const { return DwarfAccelObjCSection; }
283   MCSection *getDwarfAccelNamespaceSection() const {
284     return DwarfAccelNamespaceSection;
285   }
286   MCSection *getDwarfAccelTypesSection() const {
287     return DwarfAccelTypesSection;
288   }
289   MCSection *getDwarfInfoDWOSection() const { return DwarfInfoDWOSection; }
290   MCSection *getDwarfTypesSection(uint64_t Hash) const {
291     return getDwarfComdatSection(".debug_types", Hash);
292   }
293   MCSection *getDwarfTypesDWOSection() const { return DwarfTypesDWOSection; }
294   MCSection *getDwarfAbbrevDWOSection() const { return DwarfAbbrevDWOSection; }
295   MCSection *getDwarfStrDWOSection() const { return DwarfStrDWOSection; }
296   MCSection *getDwarfLineDWOSection() const { return DwarfLineDWOSection; }
297   MCSection *getDwarfLocDWOSection() const { return DwarfLocDWOSection; }
298   MCSection *getDwarfStrOffDWOSection() const { return DwarfStrOffDWOSection; }
299   MCSection *getDwarfStrOffSection() const { return DwarfStrOffSection; }
300   MCSection *getDwarfAddrSection() const { return DwarfAddrSection; }
301   MCSection *getDwarfRnglistsDWOSection() const {
302     return DwarfRnglistsDWOSection;
303   }
304   MCSection *getDwarfLoclistsDWOSection() const {
305     return DwarfLoclistsDWOSection;
306   }
307   MCSection *getDwarfMacinfoDWOSection() const {
308     return DwarfMacinfoDWOSection;
309   }
310   MCSection *getDwarfCUIndexSection() const { return DwarfCUIndexSection; }
311   MCSection *getDwarfTUIndexSection() const { return DwarfTUIndexSection; }
312   MCSection *getDwarfSwiftASTSection() const { return DwarfSwiftASTSection; }
313 
314   MCSection *getCOFFDebugSymbolsSection() const {
315     return COFFDebugSymbolsSection;
316   }
317   MCSection *getCOFFDebugTypesSection() const {
318     return COFFDebugTypesSection;
319   }
320   MCSection *getCOFFGlobalTypeHashesSection() const {
321     return COFFGlobalTypeHashesSection;
322   }
323 
324   MCSection *getTLSExtraDataSection() const { return TLSExtraDataSection; }
325   const MCSection *getTLSDataSection() const { return TLSDataSection; }
326   MCSection *getTLSBSSSection() const { return TLSBSSSection; }
327 
328   MCSection *getStackMapSection() const { return StackMapSection; }
329   MCSection *getFaultMapSection() const { return FaultMapSection; }
330   MCSection *getRemarksSection() const { return RemarksSection; }
331 
332   MCSection *getStackSizesSection(const MCSection &TextSec) const;
333 
334   // ELF specific sections.
335   MCSection *getDataRelROSection() const { return DataRelROSection; }
336   const MCSection *getMergeableConst4Section() const {
337     return MergeableConst4Section;
338   }
339   const MCSection *getMergeableConst8Section() const {
340     return MergeableConst8Section;
341   }
342   const MCSection *getMergeableConst16Section() const {
343     return MergeableConst16Section;
344   }
345   const MCSection *getMergeableConst32Section() const {
346     return MergeableConst32Section;
347   }
348 
349   // MachO specific sections.
350   const MCSection *getTLSTLVSection() const { return TLSTLVSection; }
351   const MCSection *getTLSThreadInitSection() const {
352     return TLSThreadInitSection;
353   }
354   const MCSection *getCStringSection() const { return CStringSection; }
355   const MCSection *getUStringSection() const { return UStringSection; }
356   MCSection *getTextCoalSection() const { return TextCoalSection; }
357   const MCSection *getConstTextCoalSection() const {
358     return ConstTextCoalSection;
359   }
360   const MCSection *getConstDataSection() const { return ConstDataSection; }
361   const MCSection *getDataCoalSection() const { return DataCoalSection; }
362   const MCSection *getConstDataCoalSection() const {
363     return ConstDataCoalSection;
364   }
365   const MCSection *getDataCommonSection() const { return DataCommonSection; }
366   MCSection *getDataBSSSection() const { return DataBSSSection; }
367   const MCSection *getFourByteConstantSection() const {
368     return FourByteConstantSection;
369   }
370   const MCSection *getEightByteConstantSection() const {
371     return EightByteConstantSection;
372   }
373   const MCSection *getSixteenByteConstantSection() const {
374     return SixteenByteConstantSection;
375   }
376   MCSection *getLazySymbolPointerSection() const {
377     return LazySymbolPointerSection;
378   }
379   MCSection *getNonLazySymbolPointerSection() const {
380     return NonLazySymbolPointerSection;
381   }
382   MCSection *getThreadLocalPointerSection() const {
383     return ThreadLocalPointerSection;
384   }
385 
386   // COFF specific sections.
387   MCSection *getDrectveSection() const { return DrectveSection; }
388   MCSection *getPDataSection() const { return PDataSection; }
389   MCSection *getXDataSection() const { return XDataSection; }
390   MCSection *getSXDataSection() const { return SXDataSection; }
391   MCSection *getGFIDsSection() const { return GFIDsSection; }
392   MCSection *getGLJMPSection() const { return GLJMPSection; }
393 
394   MCSection *getEHFrameSection() {
395     return EHFrameSection;
396   }
397 
398   enum Environment { IsMachO, IsELF, IsCOFF, IsWasm, IsXCOFF };
399   Environment getObjectFileType() const { return Env; }
400 
401   bool isPositionIndependent() const { return PositionIndependent; }
402 
403 private:
404   Environment Env;
405   bool PositionIndependent = false;
406   MCContext *Ctx = nullptr;
407   Triple TT;
408   VersionTuple SDKVersion;
409 
410   void initMachOMCObjectFileInfo(const Triple &T);
411   void initELFMCObjectFileInfo(const Triple &T, bool Large);
412   void initCOFFMCObjectFileInfo(const Triple &T);
413   void initWasmMCObjectFileInfo(const Triple &T);
414   void initXCOFFMCObjectFileInfo(const Triple &T);
415   MCSection *getDwarfComdatSection(const char *Name, uint64_t Hash) const;
416 
417 public:
418   const Triple &getTargetTriple() const { return TT; }
419 
420   void setSDKVersion(const VersionTuple &TheSDKVersion) {
421     SDKVersion = TheSDKVersion;
422   }
423 
424   const VersionTuple &getSDKVersion() const { return SDKVersion; }
425 };
426 
427 } // end namespace llvm
428 
429 #endif
430