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   MCSection *DwarfMacroSection = nullptr;
93   // The pubnames section is no longer generated by default.  The generation
94   // can be enabled by a compiler flag.
95   MCSection *DwarfPubNamesSection = nullptr;
96 
97   /// Accelerator table sections. DwarfDebugNamesSection is the DWARF v5
98   /// accelerator table, while DwarfAccelNamesSection, DwarfAccelObjCSection,
99   /// DwarfAccelNamespaceSection, DwarfAccelTypesSection are pre-DWARF v5
100   /// extensions.
101   MCSection *DwarfDebugNamesSection = nullptr;
102   MCSection *DwarfAccelNamesSection = nullptr;
103   MCSection *DwarfAccelObjCSection = nullptr;
104   MCSection *DwarfAccelNamespaceSection = nullptr;
105   MCSection *DwarfAccelTypesSection = nullptr;
106 
107   // These are used for the Fission separate debug information files.
108   MCSection *DwarfInfoDWOSection = nullptr;
109   MCSection *DwarfTypesDWOSection = nullptr;
110   MCSection *DwarfAbbrevDWOSection = nullptr;
111   MCSection *DwarfStrDWOSection = nullptr;
112   MCSection *DwarfLineDWOSection = nullptr;
113   MCSection *DwarfLocDWOSection = nullptr;
114   MCSection *DwarfStrOffDWOSection = nullptr;
115   MCSection *DwarfMacinfoDWOSection = nullptr;
116   MCSection *DwarfMacroDWOSection = nullptr;
117 
118   /// The DWARF v5 string offset and address table sections.
119   MCSection *DwarfStrOffSection = nullptr;
120   MCSection *DwarfAddrSection = nullptr;
121   /// The DWARF v5 range list section.
122   MCSection *DwarfRnglistsSection = nullptr;
123   /// The DWARF v5 locations list section.
124   MCSection *DwarfLoclistsSection = nullptr;
125 
126   /// The DWARF v5 range and location list sections for fission.
127   MCSection *DwarfRnglistsDWOSection = nullptr;
128   MCSection *DwarfLoclistsDWOSection = nullptr;
129 
130   // These are for Fission DWP files.
131   MCSection *DwarfCUIndexSection = nullptr;
132   MCSection *DwarfTUIndexSection = nullptr;
133 
134   /// Section for newer gnu pubnames.
135   MCSection *DwarfGnuPubNamesSection = nullptr;
136   /// Section for newer gnu pubtypes.
137   MCSection *DwarfGnuPubTypesSection = nullptr;
138 
139   // Section for Swift AST
140   MCSection *DwarfSwiftASTSection = nullptr;
141 
142   MCSection *COFFDebugSymbolsSection = nullptr;
143   MCSection *COFFDebugTypesSection = nullptr;
144   MCSection *COFFGlobalTypeHashesSection = nullptr;
145 
146   /// Extra TLS Variable Data section.
147   ///
148   /// If the target needs to put additional information for a TLS variable,
149   /// it'll go here.
150   MCSection *TLSExtraDataSection = nullptr;
151 
152   /// Section directive for Thread Local data. ELF, MachO, COFF, and Wasm.
153   MCSection *TLSDataSection = nullptr; // Defaults to ".tdata".
154 
155   /// Section directive for Thread Local uninitialized data.
156   ///
157   /// Null if this target doesn't support a BSS section. ELF and MachO only.
158   MCSection *TLSBSSSection = nullptr; // Defaults to ".tbss".
159 
160   /// StackMap section.
161   MCSection *StackMapSection = nullptr;
162 
163   /// FaultMap section.
164   MCSection *FaultMapSection = nullptr;
165 
166   /// Remarks section.
167   MCSection *RemarksSection = nullptr;
168 
169   /// EH frame section.
170   ///
171   /// It is initialized on demand so it can be overwritten (with uniquing).
172   MCSection *EHFrameSection = nullptr;
173 
174   /// Section containing metadata on function stack sizes.
175   MCSection *StackSizesSection = nullptr;
176 
177   /// Section for pseudo probe information used by AutoFDO
178   MCSection *PseudoProbeSection = nullptr;
179   MCSection *PseudoProbeDescSection = nullptr;
180 
181   // ELF specific sections.
182   MCSection *DataRelROSection = nullptr;
183   MCSection *MergeableConst4Section = nullptr;
184   MCSection *MergeableConst8Section = nullptr;
185   MCSection *MergeableConst16Section = nullptr;
186   MCSection *MergeableConst32Section = nullptr;
187 
188   // MachO specific sections.
189 
190   /// Section for thread local structure information.
191   ///
192   /// Contains the source code name of the variable, visibility and a pointer to
193   /// the initial value (.tdata or .tbss).
194   MCSection *TLSTLVSection = nullptr; // Defaults to ".tlv".
195 
196   /// Section for thread local data initialization functions.
197    // Defaults to ".thread_init_func".
198   const MCSection *TLSThreadInitSection = nullptr;
199 
200   MCSection *CStringSection = nullptr;
201   MCSection *UStringSection = nullptr;
202   MCSection *TextCoalSection = nullptr;
203   MCSection *ConstTextCoalSection = nullptr;
204   MCSection *ConstDataSection = nullptr;
205   MCSection *DataCoalSection = nullptr;
206   MCSection *ConstDataCoalSection = nullptr;
207   MCSection *DataCommonSection = nullptr;
208   MCSection *DataBSSSection = nullptr;
209   MCSection *FourByteConstantSection = nullptr;
210   MCSection *EightByteConstantSection = nullptr;
211   MCSection *SixteenByteConstantSection = nullptr;
212   MCSection *LazySymbolPointerSection = nullptr;
213   MCSection *NonLazySymbolPointerSection = nullptr;
214   MCSection *ThreadLocalPointerSection = nullptr;
215 
216   /// COFF specific sections.
217   MCSection *DrectveSection = nullptr;
218   MCSection *PDataSection = nullptr;
219   MCSection *XDataSection = nullptr;
220   MCSection *SXDataSection = nullptr;
221   MCSection *GEHContSection = nullptr;
222   MCSection *GFIDsSection = nullptr;
223   MCSection *GIATsSection = nullptr;
224   MCSection *GLJMPSection = nullptr;
225 
226   // XCOFF specific sections
227   MCSection *TOCBaseSection = nullptr;
228 
229 public:
230   void initMCObjectFileInfo(MCContext &MCCtx, bool PIC,
231                             bool LargeCodeModel = false);
getContext()232   MCContext &getContext() const { return *Ctx; }
233 
getSupportsWeakOmittedEHFrame()234   bool getSupportsWeakOmittedEHFrame() const {
235     return SupportsWeakOmittedEHFrame;
236   }
getSupportsCompactUnwindWithoutEHFrame()237   bool getSupportsCompactUnwindWithoutEHFrame() const {
238     return SupportsCompactUnwindWithoutEHFrame;
239   }
getOmitDwarfIfHaveCompactUnwind()240   bool getOmitDwarfIfHaveCompactUnwind() const {
241     return OmitDwarfIfHaveCompactUnwind;
242   }
243 
getCommDirectiveSupportsAlignment()244   bool getCommDirectiveSupportsAlignment() const {
245     return CommDirectiveSupportsAlignment;
246   }
247 
getFDEEncoding()248   unsigned getFDEEncoding() const { return FDECFIEncoding; }
249 
getCompactUnwindDwarfEHFrameOnly()250   unsigned getCompactUnwindDwarfEHFrameOnly() const {
251     return CompactUnwindDwarfEHFrameOnly;
252   }
253 
getTextSection()254   MCSection *getTextSection() const { return TextSection; }
getDataSection()255   MCSection *getDataSection() const { return DataSection; }
getBSSSection()256   MCSection *getBSSSection() const { return BSSSection; }
getReadOnlySection()257   MCSection *getReadOnlySection() const { return ReadOnlySection; }
getCompactUnwindSection()258   MCSection *getCompactUnwindSection() const { return CompactUnwindSection; }
getDwarfAbbrevSection()259   MCSection *getDwarfAbbrevSection() const { return DwarfAbbrevSection; }
getDwarfInfoSection()260   MCSection *getDwarfInfoSection() const { return DwarfInfoSection; }
getDwarfInfoSection(uint64_t Hash)261   MCSection *getDwarfInfoSection(uint64_t Hash) const {
262     return getDwarfComdatSection(".debug_info", Hash);
263   }
getDwarfLineSection()264   MCSection *getDwarfLineSection() const { return DwarfLineSection; }
getDwarfLineStrSection()265   MCSection *getDwarfLineStrSection() const { return DwarfLineStrSection; }
getDwarfFrameSection()266   MCSection *getDwarfFrameSection() const { return DwarfFrameSection; }
getDwarfPubNamesSection()267   MCSection *getDwarfPubNamesSection() const { return DwarfPubNamesSection; }
getDwarfPubTypesSection()268   MCSection *getDwarfPubTypesSection() const { return DwarfPubTypesSection; }
getDwarfGnuPubNamesSection()269   MCSection *getDwarfGnuPubNamesSection() const {
270     return DwarfGnuPubNamesSection;
271   }
getDwarfGnuPubTypesSection()272   MCSection *getDwarfGnuPubTypesSection() const {
273     return DwarfGnuPubTypesSection;
274   }
getDwarfDebugInlineSection()275   const MCSection *getDwarfDebugInlineSection() const {
276     return DwarfDebugInlineSection;
277   }
getDwarfStrSection()278   MCSection *getDwarfStrSection() const { return DwarfStrSection; }
getDwarfLocSection()279   MCSection *getDwarfLocSection() const { return DwarfLocSection; }
getDwarfARangesSection()280   MCSection *getDwarfARangesSection() const { return DwarfARangesSection; }
getDwarfRangesSection()281   MCSection *getDwarfRangesSection() const { return DwarfRangesSection; }
getDwarfRnglistsSection()282   MCSection *getDwarfRnglistsSection() const { return DwarfRnglistsSection; }
getDwarfLoclistsSection()283   MCSection *getDwarfLoclistsSection() const { return DwarfLoclistsSection; }
getDwarfMacinfoSection()284   MCSection *getDwarfMacinfoSection() const { return DwarfMacinfoSection; }
getDwarfMacroSection()285   MCSection *getDwarfMacroSection() const { return DwarfMacroSection; }
286 
getDwarfDebugNamesSection()287   MCSection *getDwarfDebugNamesSection() const {
288     return DwarfDebugNamesSection;
289   }
getDwarfAccelNamesSection()290   MCSection *getDwarfAccelNamesSection() const {
291     return DwarfAccelNamesSection;
292   }
getDwarfAccelObjCSection()293   MCSection *getDwarfAccelObjCSection() const { return DwarfAccelObjCSection; }
getDwarfAccelNamespaceSection()294   MCSection *getDwarfAccelNamespaceSection() const {
295     return DwarfAccelNamespaceSection;
296   }
getDwarfAccelTypesSection()297   MCSection *getDwarfAccelTypesSection() const {
298     return DwarfAccelTypesSection;
299   }
getDwarfInfoDWOSection()300   MCSection *getDwarfInfoDWOSection() const { return DwarfInfoDWOSection; }
getDwarfTypesSection(uint64_t Hash)301   MCSection *getDwarfTypesSection(uint64_t Hash) const {
302     return getDwarfComdatSection(".debug_types", Hash);
303   }
getDwarfTypesDWOSection()304   MCSection *getDwarfTypesDWOSection() const { return DwarfTypesDWOSection; }
getDwarfAbbrevDWOSection()305   MCSection *getDwarfAbbrevDWOSection() const { return DwarfAbbrevDWOSection; }
getDwarfStrDWOSection()306   MCSection *getDwarfStrDWOSection() const { return DwarfStrDWOSection; }
getDwarfLineDWOSection()307   MCSection *getDwarfLineDWOSection() const { return DwarfLineDWOSection; }
getDwarfLocDWOSection()308   MCSection *getDwarfLocDWOSection() const { return DwarfLocDWOSection; }
getDwarfStrOffDWOSection()309   MCSection *getDwarfStrOffDWOSection() const { return DwarfStrOffDWOSection; }
getDwarfStrOffSection()310   MCSection *getDwarfStrOffSection() const { return DwarfStrOffSection; }
getDwarfAddrSection()311   MCSection *getDwarfAddrSection() const { return DwarfAddrSection; }
getDwarfRnglistsDWOSection()312   MCSection *getDwarfRnglistsDWOSection() const {
313     return DwarfRnglistsDWOSection;
314   }
getDwarfLoclistsDWOSection()315   MCSection *getDwarfLoclistsDWOSection() const {
316     return DwarfLoclistsDWOSection;
317   }
getDwarfMacroDWOSection()318   MCSection *getDwarfMacroDWOSection() const { return DwarfMacroDWOSection; }
getDwarfMacinfoDWOSection()319   MCSection *getDwarfMacinfoDWOSection() const {
320     return DwarfMacinfoDWOSection;
321   }
getDwarfCUIndexSection()322   MCSection *getDwarfCUIndexSection() const { return DwarfCUIndexSection; }
getDwarfTUIndexSection()323   MCSection *getDwarfTUIndexSection() const { return DwarfTUIndexSection; }
getDwarfSwiftASTSection()324   MCSection *getDwarfSwiftASTSection() const { return DwarfSwiftASTSection; }
325 
getCOFFDebugSymbolsSection()326   MCSection *getCOFFDebugSymbolsSection() const {
327     return COFFDebugSymbolsSection;
328   }
getCOFFDebugTypesSection()329   MCSection *getCOFFDebugTypesSection() const {
330     return COFFDebugTypesSection;
331   }
getCOFFGlobalTypeHashesSection()332   MCSection *getCOFFGlobalTypeHashesSection() const {
333     return COFFGlobalTypeHashesSection;
334   }
335 
getTLSExtraDataSection()336   MCSection *getTLSExtraDataSection() const { return TLSExtraDataSection; }
getTLSDataSection()337   const MCSection *getTLSDataSection() const { return TLSDataSection; }
getTLSBSSSection()338   MCSection *getTLSBSSSection() const { return TLSBSSSection; }
339 
getStackMapSection()340   MCSection *getStackMapSection() const { return StackMapSection; }
getFaultMapSection()341   MCSection *getFaultMapSection() const { return FaultMapSection; }
getRemarksSection()342   MCSection *getRemarksSection() const { return RemarksSection; }
343 
344   MCSection *getStackSizesSection(const MCSection &TextSec) const;
345 
346   MCSection *getBBAddrMapSection(const MCSection &TextSec) const;
347 
348   MCSection *getPseudoProbeSection(const MCSection *TextSec) const;
349 
350   MCSection *getPseudoProbeDescSection(StringRef FuncName) const;
351 
352   // ELF specific sections.
getDataRelROSection()353   MCSection *getDataRelROSection() const { return DataRelROSection; }
getMergeableConst4Section()354   const MCSection *getMergeableConst4Section() const {
355     return MergeableConst4Section;
356   }
getMergeableConst8Section()357   const MCSection *getMergeableConst8Section() const {
358     return MergeableConst8Section;
359   }
getMergeableConst16Section()360   const MCSection *getMergeableConst16Section() const {
361     return MergeableConst16Section;
362   }
getMergeableConst32Section()363   const MCSection *getMergeableConst32Section() const {
364     return MergeableConst32Section;
365   }
366 
367   // MachO specific sections.
getTLSTLVSection()368   const MCSection *getTLSTLVSection() const { return TLSTLVSection; }
getTLSThreadInitSection()369   const MCSection *getTLSThreadInitSection() const {
370     return TLSThreadInitSection;
371   }
getCStringSection()372   const MCSection *getCStringSection() const { return CStringSection; }
getUStringSection()373   const MCSection *getUStringSection() const { return UStringSection; }
getTextCoalSection()374   MCSection *getTextCoalSection() const { return TextCoalSection; }
getConstTextCoalSection()375   const MCSection *getConstTextCoalSection() const {
376     return ConstTextCoalSection;
377   }
getConstDataSection()378   const MCSection *getConstDataSection() const { return ConstDataSection; }
getDataCoalSection()379   const MCSection *getDataCoalSection() const { return DataCoalSection; }
getConstDataCoalSection()380   const MCSection *getConstDataCoalSection() const {
381     return ConstDataCoalSection;
382   }
getDataCommonSection()383   const MCSection *getDataCommonSection() const { return DataCommonSection; }
getDataBSSSection()384   MCSection *getDataBSSSection() const { return DataBSSSection; }
getFourByteConstantSection()385   const MCSection *getFourByteConstantSection() const {
386     return FourByteConstantSection;
387   }
getEightByteConstantSection()388   const MCSection *getEightByteConstantSection() const {
389     return EightByteConstantSection;
390   }
getSixteenByteConstantSection()391   const MCSection *getSixteenByteConstantSection() const {
392     return SixteenByteConstantSection;
393   }
getLazySymbolPointerSection()394   MCSection *getLazySymbolPointerSection() const {
395     return LazySymbolPointerSection;
396   }
getNonLazySymbolPointerSection()397   MCSection *getNonLazySymbolPointerSection() const {
398     return NonLazySymbolPointerSection;
399   }
getThreadLocalPointerSection()400   MCSection *getThreadLocalPointerSection() const {
401     return ThreadLocalPointerSection;
402   }
403 
404   // COFF specific sections.
getDrectveSection()405   MCSection *getDrectveSection() const { return DrectveSection; }
getPDataSection()406   MCSection *getPDataSection() const { return PDataSection; }
getXDataSection()407   MCSection *getXDataSection() const { return XDataSection; }
getSXDataSection()408   MCSection *getSXDataSection() const { return SXDataSection; }
getGEHContSection()409   MCSection *getGEHContSection() const { return GEHContSection; }
getGFIDsSection()410   MCSection *getGFIDsSection() const { return GFIDsSection; }
getGIATsSection()411   MCSection *getGIATsSection() const { return GIATsSection; }
getGLJMPSection()412   MCSection *getGLJMPSection() const { return GLJMPSection; }
413 
414   // XCOFF specific sections
getTOCBaseSection()415   MCSection *getTOCBaseSection() const { return TOCBaseSection; }
416 
getEHFrameSection()417   MCSection *getEHFrameSection() const { return EHFrameSection; }
418 
isPositionIndependent()419   bool isPositionIndependent() const { return PositionIndependent; }
420 
421 private:
422   bool PositionIndependent = false;
423   MCContext *Ctx = nullptr;
424   VersionTuple SDKVersion;
425 
426   void initMachOMCObjectFileInfo(const Triple &T);
427   void initELFMCObjectFileInfo(const Triple &T, bool Large);
428   void initCOFFMCObjectFileInfo(const Triple &T);
429   void initWasmMCObjectFileInfo(const Triple &T);
430   void initXCOFFMCObjectFileInfo(const Triple &T);
431   MCSection *getDwarfComdatSection(const char *Name, uint64_t Hash) const;
432 
433 public:
setSDKVersion(const VersionTuple & TheSDKVersion)434   void setSDKVersion(const VersionTuple &TheSDKVersion) {
435     SDKVersion = TheSDKVersion;
436   }
437 
getSDKVersion()438   const VersionTuple &getSDKVersion() const { return SDKVersion; }
439 };
440 
441 } // end namespace llvm
442 
443 #endif
444