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   // ELF specific sections.
178   MCSection *DataRelROSection = nullptr;
179   MCSection *MergeableConst4Section = nullptr;
180   MCSection *MergeableConst8Section = nullptr;
181   MCSection *MergeableConst16Section = nullptr;
182   MCSection *MergeableConst32Section = nullptr;
183 
184   // MachO specific sections.
185 
186   /// Section for thread local structure information.
187   ///
188   /// Contains the source code name of the variable, visibility and a pointer to
189   /// the initial value (.tdata or .tbss).
190   MCSection *TLSTLVSection = nullptr; // Defaults to ".tlv".
191 
192   /// Section for thread local data initialization functions.
193    // Defaults to ".thread_init_func".
194   const MCSection *TLSThreadInitSection = nullptr;
195 
196   MCSection *CStringSection = nullptr;
197   MCSection *UStringSection = nullptr;
198   MCSection *TextCoalSection = nullptr;
199   MCSection *ConstTextCoalSection = nullptr;
200   MCSection *ConstDataSection = nullptr;
201   MCSection *DataCoalSection = nullptr;
202   MCSection *ConstDataCoalSection = nullptr;
203   MCSection *DataCommonSection = nullptr;
204   MCSection *DataBSSSection = nullptr;
205   MCSection *FourByteConstantSection = nullptr;
206   MCSection *EightByteConstantSection = nullptr;
207   MCSection *SixteenByteConstantSection = nullptr;
208   MCSection *LazySymbolPointerSection = nullptr;
209   MCSection *NonLazySymbolPointerSection = nullptr;
210   MCSection *ThreadLocalPointerSection = nullptr;
211 
212   /// COFF specific sections.
213   MCSection *DrectveSection = nullptr;
214   MCSection *PDataSection = nullptr;
215   MCSection *XDataSection = nullptr;
216   MCSection *SXDataSection = nullptr;
217   MCSection *GFIDsSection = nullptr;
218   MCSection *GIATsSection = nullptr;
219   MCSection *GLJMPSection = nullptr;
220 
221   // XCOFF specific sections
222   MCSection *TOCBaseSection = nullptr;
223 
224 public:
225   void InitMCObjectFileInfo(const Triple &TT, bool PIC, MCContext &ctx,
226                             bool LargeCodeModel = false);
getContext()227   MCContext &getContext() const { return *Ctx; }
228 
getSupportsWeakOmittedEHFrame()229   bool getSupportsWeakOmittedEHFrame() const {
230     return SupportsWeakOmittedEHFrame;
231   }
getSupportsCompactUnwindWithoutEHFrame()232   bool getSupportsCompactUnwindWithoutEHFrame() const {
233     return SupportsCompactUnwindWithoutEHFrame;
234   }
getOmitDwarfIfHaveCompactUnwind()235   bool getOmitDwarfIfHaveCompactUnwind() const {
236     return OmitDwarfIfHaveCompactUnwind;
237   }
238 
getCommDirectiveSupportsAlignment()239   bool getCommDirectiveSupportsAlignment() const {
240     return CommDirectiveSupportsAlignment;
241   }
242 
getFDEEncoding()243   unsigned getFDEEncoding() const { return FDECFIEncoding; }
244 
getCompactUnwindDwarfEHFrameOnly()245   unsigned getCompactUnwindDwarfEHFrameOnly() const {
246     return CompactUnwindDwarfEHFrameOnly;
247   }
248 
getTextSection()249   MCSection *getTextSection() const { return TextSection; }
getDataSection()250   MCSection *getDataSection() const { return DataSection; }
getBSSSection()251   MCSection *getBSSSection() const { return BSSSection; }
getReadOnlySection()252   MCSection *getReadOnlySection() const { return ReadOnlySection; }
getCompactUnwindSection()253   MCSection *getCompactUnwindSection() const { return CompactUnwindSection; }
getDwarfAbbrevSection()254   MCSection *getDwarfAbbrevSection() const { return DwarfAbbrevSection; }
getDwarfInfoSection()255   MCSection *getDwarfInfoSection() const { return DwarfInfoSection; }
getDwarfInfoSection(uint64_t Hash)256   MCSection *getDwarfInfoSection(uint64_t Hash) const {
257     return getDwarfComdatSection(".debug_info", Hash);
258   }
getDwarfLineSection()259   MCSection *getDwarfLineSection() const { return DwarfLineSection; }
getDwarfLineStrSection()260   MCSection *getDwarfLineStrSection() const { return DwarfLineStrSection; }
getDwarfFrameSection()261   MCSection *getDwarfFrameSection() const { return DwarfFrameSection; }
getDwarfPubNamesSection()262   MCSection *getDwarfPubNamesSection() const { return DwarfPubNamesSection; }
getDwarfPubTypesSection()263   MCSection *getDwarfPubTypesSection() const { return DwarfPubTypesSection; }
getDwarfGnuPubNamesSection()264   MCSection *getDwarfGnuPubNamesSection() const {
265     return DwarfGnuPubNamesSection;
266   }
getDwarfGnuPubTypesSection()267   MCSection *getDwarfGnuPubTypesSection() const {
268     return DwarfGnuPubTypesSection;
269   }
getDwarfDebugInlineSection()270   const MCSection *getDwarfDebugInlineSection() const {
271     return DwarfDebugInlineSection;
272   }
getDwarfStrSection()273   MCSection *getDwarfStrSection() const { return DwarfStrSection; }
getDwarfLocSection()274   MCSection *getDwarfLocSection() const { return DwarfLocSection; }
getDwarfARangesSection()275   MCSection *getDwarfARangesSection() const { return DwarfARangesSection; }
getDwarfRangesSection()276   MCSection *getDwarfRangesSection() const { return DwarfRangesSection; }
getDwarfRnglistsSection()277   MCSection *getDwarfRnglistsSection() const { return DwarfRnglistsSection; }
getDwarfLoclistsSection()278   MCSection *getDwarfLoclistsSection() const { return DwarfLoclistsSection; }
getDwarfMacinfoSection()279   MCSection *getDwarfMacinfoSection() const { return DwarfMacinfoSection; }
getDwarfMacroSection()280   MCSection *getDwarfMacroSection() const { return DwarfMacroSection; }
281 
getDwarfDebugNamesSection()282   MCSection *getDwarfDebugNamesSection() const {
283     return DwarfDebugNamesSection;
284   }
getDwarfAccelNamesSection()285   MCSection *getDwarfAccelNamesSection() const {
286     return DwarfAccelNamesSection;
287   }
getDwarfAccelObjCSection()288   MCSection *getDwarfAccelObjCSection() const { return DwarfAccelObjCSection; }
getDwarfAccelNamespaceSection()289   MCSection *getDwarfAccelNamespaceSection() const {
290     return DwarfAccelNamespaceSection;
291   }
getDwarfAccelTypesSection()292   MCSection *getDwarfAccelTypesSection() const {
293     return DwarfAccelTypesSection;
294   }
getDwarfInfoDWOSection()295   MCSection *getDwarfInfoDWOSection() const { return DwarfInfoDWOSection; }
getDwarfTypesSection(uint64_t Hash)296   MCSection *getDwarfTypesSection(uint64_t Hash) const {
297     return getDwarfComdatSection(".debug_types", Hash);
298   }
getDwarfTypesDWOSection()299   MCSection *getDwarfTypesDWOSection() const { return DwarfTypesDWOSection; }
getDwarfAbbrevDWOSection()300   MCSection *getDwarfAbbrevDWOSection() const { return DwarfAbbrevDWOSection; }
getDwarfStrDWOSection()301   MCSection *getDwarfStrDWOSection() const { return DwarfStrDWOSection; }
getDwarfLineDWOSection()302   MCSection *getDwarfLineDWOSection() const { return DwarfLineDWOSection; }
getDwarfLocDWOSection()303   MCSection *getDwarfLocDWOSection() const { return DwarfLocDWOSection; }
getDwarfStrOffDWOSection()304   MCSection *getDwarfStrOffDWOSection() const { return DwarfStrOffDWOSection; }
getDwarfStrOffSection()305   MCSection *getDwarfStrOffSection() const { return DwarfStrOffSection; }
getDwarfAddrSection()306   MCSection *getDwarfAddrSection() const { return DwarfAddrSection; }
getDwarfRnglistsDWOSection()307   MCSection *getDwarfRnglistsDWOSection() const {
308     return DwarfRnglistsDWOSection;
309   }
getDwarfLoclistsDWOSection()310   MCSection *getDwarfLoclistsDWOSection() const {
311     return DwarfLoclistsDWOSection;
312   }
getDwarfMacroDWOSection()313   MCSection *getDwarfMacroDWOSection() const { return DwarfMacroDWOSection; }
getDwarfMacinfoDWOSection()314   MCSection *getDwarfMacinfoDWOSection() const {
315     return DwarfMacinfoDWOSection;
316   }
getDwarfCUIndexSection()317   MCSection *getDwarfCUIndexSection() const { return DwarfCUIndexSection; }
getDwarfTUIndexSection()318   MCSection *getDwarfTUIndexSection() const { return DwarfTUIndexSection; }
getDwarfSwiftASTSection()319   MCSection *getDwarfSwiftASTSection() const { return DwarfSwiftASTSection; }
320 
getCOFFDebugSymbolsSection()321   MCSection *getCOFFDebugSymbolsSection() const {
322     return COFFDebugSymbolsSection;
323   }
getCOFFDebugTypesSection()324   MCSection *getCOFFDebugTypesSection() const {
325     return COFFDebugTypesSection;
326   }
getCOFFGlobalTypeHashesSection()327   MCSection *getCOFFGlobalTypeHashesSection() const {
328     return COFFGlobalTypeHashesSection;
329   }
330 
getTLSExtraDataSection()331   MCSection *getTLSExtraDataSection() const { return TLSExtraDataSection; }
getTLSDataSection()332   const MCSection *getTLSDataSection() const { return TLSDataSection; }
getTLSBSSSection()333   MCSection *getTLSBSSSection() const { return TLSBSSSection; }
334 
getStackMapSection()335   MCSection *getStackMapSection() const { return StackMapSection; }
getFaultMapSection()336   MCSection *getFaultMapSection() const { return FaultMapSection; }
getRemarksSection()337   MCSection *getRemarksSection() const { return RemarksSection; }
338 
339   MCSection *getStackSizesSection(const MCSection &TextSec) const;
340 
341   MCSection *getBBAddrMapSection(const MCSection &TextSec) const;
342 
343   // ELF specific sections.
getDataRelROSection()344   MCSection *getDataRelROSection() const { return DataRelROSection; }
getMergeableConst4Section()345   const MCSection *getMergeableConst4Section() const {
346     return MergeableConst4Section;
347   }
getMergeableConst8Section()348   const MCSection *getMergeableConst8Section() const {
349     return MergeableConst8Section;
350   }
getMergeableConst16Section()351   const MCSection *getMergeableConst16Section() const {
352     return MergeableConst16Section;
353   }
getMergeableConst32Section()354   const MCSection *getMergeableConst32Section() const {
355     return MergeableConst32Section;
356   }
357 
358   // MachO specific sections.
getTLSTLVSection()359   const MCSection *getTLSTLVSection() const { return TLSTLVSection; }
getTLSThreadInitSection()360   const MCSection *getTLSThreadInitSection() const {
361     return TLSThreadInitSection;
362   }
getCStringSection()363   const MCSection *getCStringSection() const { return CStringSection; }
getUStringSection()364   const MCSection *getUStringSection() const { return UStringSection; }
getTextCoalSection()365   MCSection *getTextCoalSection() const { return TextCoalSection; }
getConstTextCoalSection()366   const MCSection *getConstTextCoalSection() const {
367     return ConstTextCoalSection;
368   }
getConstDataSection()369   const MCSection *getConstDataSection() const { return ConstDataSection; }
getDataCoalSection()370   const MCSection *getDataCoalSection() const { return DataCoalSection; }
getConstDataCoalSection()371   const MCSection *getConstDataCoalSection() const {
372     return ConstDataCoalSection;
373   }
getDataCommonSection()374   const MCSection *getDataCommonSection() const { return DataCommonSection; }
getDataBSSSection()375   MCSection *getDataBSSSection() const { return DataBSSSection; }
getFourByteConstantSection()376   const MCSection *getFourByteConstantSection() const {
377     return FourByteConstantSection;
378   }
getEightByteConstantSection()379   const MCSection *getEightByteConstantSection() const {
380     return EightByteConstantSection;
381   }
getSixteenByteConstantSection()382   const MCSection *getSixteenByteConstantSection() const {
383     return SixteenByteConstantSection;
384   }
getLazySymbolPointerSection()385   MCSection *getLazySymbolPointerSection() const {
386     return LazySymbolPointerSection;
387   }
getNonLazySymbolPointerSection()388   MCSection *getNonLazySymbolPointerSection() const {
389     return NonLazySymbolPointerSection;
390   }
getThreadLocalPointerSection()391   MCSection *getThreadLocalPointerSection() const {
392     return ThreadLocalPointerSection;
393   }
394 
395   // COFF specific sections.
getDrectveSection()396   MCSection *getDrectveSection() const { return DrectveSection; }
getPDataSection()397   MCSection *getPDataSection() const { return PDataSection; }
getXDataSection()398   MCSection *getXDataSection() const { return XDataSection; }
getSXDataSection()399   MCSection *getSXDataSection() const { return SXDataSection; }
getGFIDsSection()400   MCSection *getGFIDsSection() const { return GFIDsSection; }
getGIATsSection()401   MCSection *getGIATsSection() const { return GIATsSection; }
getGLJMPSection()402   MCSection *getGLJMPSection() const { return GLJMPSection; }
403 
404   // XCOFF specific sections
getTOCBaseSection()405   MCSection *getTOCBaseSection() const { return TOCBaseSection; }
406 
getEHFrameSection()407   MCSection *getEHFrameSection() {
408     return EHFrameSection;
409   }
410 
411   enum Environment { IsMachO, IsELF, IsCOFF, IsWasm, IsXCOFF };
getObjectFileType()412   Environment getObjectFileType() const { return Env; }
413 
isPositionIndependent()414   bool isPositionIndependent() const { return PositionIndependent; }
415 
416 private:
417   Environment Env;
418   bool PositionIndependent = false;
419   MCContext *Ctx = nullptr;
420   Triple TT;
421   VersionTuple SDKVersion;
422 
423   void initMachOMCObjectFileInfo(const Triple &T);
424   void initELFMCObjectFileInfo(const Triple &T, bool Large);
425   void initCOFFMCObjectFileInfo(const Triple &T);
426   void initWasmMCObjectFileInfo(const Triple &T);
427   void initXCOFFMCObjectFileInfo(const Triple &T);
428   MCSection *getDwarfComdatSection(const char *Name, uint64_t Hash) const;
429 
430 public:
getTargetTriple()431   const Triple &getTargetTriple() const { return TT; }
432 
setSDKVersion(const VersionTuple & TheSDKVersion)433   void setSDKVersion(const VersionTuple &TheSDKVersion) {
434     SDKVersion = TheSDKVersion;
435   }
436 
getSDKVersion()437   const VersionTuple &getSDKVersion() const { return SDKVersion; }
438 };
439 
440 } // end namespace llvm
441 
442 #endif
443