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