1 //===-- MObjectFileInfo.cpp - Object File Information ---------------------===//
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 #include "llvm/MC/MCObjectFileInfo.h"
11 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/ADT/Triple.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCSection.h"
16 #include "llvm/MC/MCSectionCOFF.h"
17 #include "llvm/MC/MCSectionELF.h"
18 #include "llvm/MC/MCSectionMachO.h"
19 using namespace llvm;
20 
21 static bool useCompactUnwind(const Triple &T) {
22   // Only on darwin.
23   if (!T.isOSDarwin())
24     return false;
25 
26   // aarch64 always has it.
27   if (T.getArch() == Triple::arm64 || T.getArch() == Triple::aarch64)
28     return true;
29 
30   // Use it on newer version of OS X.
31   if (T.isMacOSX() && !T.isMacOSXVersionLT(10, 6))
32     return true;
33 
34   // And the iOS simulator.
35   if (T.isiOS() &&
36       (T.getArch() == Triple::x86_64 || T.getArch() == Triple::x86))
37     return true;
38 
39   return false;
40 }
41 
42 void MCObjectFileInfo::InitMachOMCObjectFileInfo(Triple T) {
43   // MachO
44   SupportsWeakOmittedEHFrame = false;
45 
46   if (T.isOSDarwin() &&
47       (T.getArch() == Triple::arm64 || T.getArch() == Triple::aarch64))
48     SupportsCompactUnwindWithoutEHFrame = true;
49 
50   PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel
51     | dwarf::DW_EH_PE_sdata4;
52   LSDAEncoding = FDECFIEncoding = dwarf::DW_EH_PE_pcrel;
53   TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
54     dwarf::DW_EH_PE_sdata4;
55 
56   // .comm doesn't support alignment before Leopard.
57   if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
58     CommDirectiveSupportsAlignment = false;
59 
60   TextSection // .text
61     = Ctx->getMachOSection("__TEXT", "__text",
62                            MachO::S_ATTR_PURE_INSTRUCTIONS,
63                            SectionKind::getText());
64   DataSection // .data
65     = Ctx->getMachOSection("__DATA", "__data", 0,
66                            SectionKind::getDataRel());
67 
68   // BSSSection might not be expected initialized on msvc.
69   BSSSection = nullptr;
70 
71   TLSDataSection // .tdata
72     = Ctx->getMachOSection("__DATA", "__thread_data",
73                            MachO::S_THREAD_LOCAL_REGULAR,
74                            SectionKind::getDataRel());
75   TLSBSSSection // .tbss
76     = Ctx->getMachOSection("__DATA", "__thread_bss",
77                            MachO::S_THREAD_LOCAL_ZEROFILL,
78                            SectionKind::getThreadBSS());
79 
80   // TODO: Verify datarel below.
81   TLSTLVSection // .tlv
82     = Ctx->getMachOSection("__DATA", "__thread_vars",
83                            MachO::S_THREAD_LOCAL_VARIABLES,
84                            SectionKind::getDataRel());
85 
86   TLSThreadInitSection
87     = Ctx->getMachOSection("__DATA", "__thread_init",
88                           MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
89                           SectionKind::getDataRel());
90 
91   CStringSection // .cstring
92     = Ctx->getMachOSection("__TEXT", "__cstring",
93                            MachO::S_CSTRING_LITERALS,
94                            SectionKind::getMergeable1ByteCString());
95   UStringSection
96     = Ctx->getMachOSection("__TEXT","__ustring", 0,
97                            SectionKind::getMergeable2ByteCString());
98   FourByteConstantSection // .literal4
99     = Ctx->getMachOSection("__TEXT", "__literal4",
100                            MachO::S_4BYTE_LITERALS,
101                            SectionKind::getMergeableConst4());
102   EightByteConstantSection // .literal8
103     = Ctx->getMachOSection("__TEXT", "__literal8",
104                            MachO::S_8BYTE_LITERALS,
105                            SectionKind::getMergeableConst8());
106 
107   SixteenByteConstantSection // .literal16
108       = Ctx->getMachOSection("__TEXT", "__literal16",
109                              MachO::S_16BYTE_LITERALS,
110                              SectionKind::getMergeableConst16());
111 
112   ReadOnlySection  // .const
113     = Ctx->getMachOSection("__TEXT", "__const", 0,
114                            SectionKind::getReadOnly());
115 
116   TextCoalSection
117     = Ctx->getMachOSection("__TEXT", "__textcoal_nt",
118                            MachO::S_COALESCED |
119                            MachO::S_ATTR_PURE_INSTRUCTIONS,
120                            SectionKind::getText());
121   ConstTextCoalSection
122     = Ctx->getMachOSection("__TEXT", "__const_coal",
123                            MachO::S_COALESCED,
124                            SectionKind::getReadOnly());
125   ConstDataSection  // .const_data
126     = Ctx->getMachOSection("__DATA", "__const", 0,
127                            SectionKind::getReadOnlyWithRel());
128   DataCoalSection
129     = Ctx->getMachOSection("__DATA","__datacoal_nt",
130                            MachO::S_COALESCED,
131                            SectionKind::getDataRel());
132   DataCommonSection
133     = Ctx->getMachOSection("__DATA","__common",
134                            MachO::S_ZEROFILL,
135                            SectionKind::getBSS());
136   DataBSSSection
137     = Ctx->getMachOSection("__DATA","__bss", MachO::S_ZEROFILL,
138                            SectionKind::getBSS());
139 
140 
141   LazySymbolPointerSection
142     = Ctx->getMachOSection("__DATA", "__la_symbol_ptr",
143                            MachO::S_LAZY_SYMBOL_POINTERS,
144                            SectionKind::getMetadata());
145   NonLazySymbolPointerSection
146     = Ctx->getMachOSection("__DATA", "__nl_symbol_ptr",
147                            MachO::S_NON_LAZY_SYMBOL_POINTERS,
148                            SectionKind::getMetadata());
149 
150   if (RelocM == Reloc::Static) {
151     StaticCtorSection
152       = Ctx->getMachOSection("__TEXT", "__constructor", 0,
153                              SectionKind::getDataRel());
154     StaticDtorSection
155       = Ctx->getMachOSection("__TEXT", "__destructor", 0,
156                              SectionKind::getDataRel());
157   } else {
158     StaticCtorSection
159       = Ctx->getMachOSection("__DATA", "__mod_init_func",
160                              MachO::S_MOD_INIT_FUNC_POINTERS,
161                              SectionKind::getDataRel());
162     StaticDtorSection
163       = Ctx->getMachOSection("__DATA", "__mod_term_func",
164                              MachO::S_MOD_TERM_FUNC_POINTERS,
165                              SectionKind::getDataRel());
166   }
167 
168   // Exception Handling.
169   LSDASection = Ctx->getMachOSection("__TEXT", "__gcc_except_tab", 0,
170                                      SectionKind::getReadOnlyWithRel());
171 
172   COFFDebugSymbolsSection = nullptr;
173 
174   if (useCompactUnwind(T)) {
175     CompactUnwindSection =
176         Ctx->getMachOSection("__LD", "__compact_unwind", MachO::S_ATTR_DEBUG,
177                              SectionKind::getReadOnly());
178 
179     if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::x86)
180       CompactUnwindDwarfEHFrameOnly = 0x04000000;
181     else if (T.getArch() == Triple::arm64 || T.getArch() == Triple::aarch64)
182       CompactUnwindDwarfEHFrameOnly = 0x03000000;
183   }
184 
185   // Debug Information.
186   DwarfAccelNamesSection =
187     Ctx->getMachOSection("__DWARF", "__apple_names",
188                          MachO::S_ATTR_DEBUG,
189                          SectionKind::getMetadata());
190   DwarfAccelObjCSection =
191     Ctx->getMachOSection("__DWARF", "__apple_objc",
192                          MachO::S_ATTR_DEBUG,
193                          SectionKind::getMetadata());
194   // 16 character section limit...
195   DwarfAccelNamespaceSection =
196     Ctx->getMachOSection("__DWARF", "__apple_namespac",
197                          MachO::S_ATTR_DEBUG,
198                          SectionKind::getMetadata());
199   DwarfAccelTypesSection =
200     Ctx->getMachOSection("__DWARF", "__apple_types",
201                          MachO::S_ATTR_DEBUG,
202                          SectionKind::getMetadata());
203 
204   DwarfAbbrevSection =
205     Ctx->getMachOSection("__DWARF", "__debug_abbrev",
206                          MachO::S_ATTR_DEBUG,
207                          SectionKind::getMetadata());
208   DwarfInfoSection =
209     Ctx->getMachOSection("__DWARF", "__debug_info",
210                          MachO::S_ATTR_DEBUG,
211                          SectionKind::getMetadata());
212   DwarfLineSection =
213     Ctx->getMachOSection("__DWARF", "__debug_line",
214                          MachO::S_ATTR_DEBUG,
215                          SectionKind::getMetadata());
216   DwarfFrameSection =
217     Ctx->getMachOSection("__DWARF", "__debug_frame",
218                          MachO::S_ATTR_DEBUG,
219                          SectionKind::getMetadata());
220   DwarfPubNamesSection =
221     Ctx->getMachOSection("__DWARF", "__debug_pubnames",
222                          MachO::S_ATTR_DEBUG,
223                          SectionKind::getMetadata());
224   DwarfPubTypesSection =
225     Ctx->getMachOSection("__DWARF", "__debug_pubtypes",
226                          MachO::S_ATTR_DEBUG,
227                          SectionKind::getMetadata());
228   DwarfGnuPubNamesSection =
229     Ctx->getMachOSection("__DWARF", "__debug_gnu_pubn",
230                          MachO::S_ATTR_DEBUG,
231                          SectionKind::getMetadata());
232   DwarfGnuPubTypesSection =
233     Ctx->getMachOSection("__DWARF", "__debug_gnu_pubt",
234                          MachO::S_ATTR_DEBUG,
235                          SectionKind::getMetadata());
236   DwarfStrSection =
237     Ctx->getMachOSection("__DWARF", "__debug_str",
238                          MachO::S_ATTR_DEBUG,
239                          SectionKind::getMetadata());
240   DwarfLocSection =
241     Ctx->getMachOSection("__DWARF", "__debug_loc",
242                          MachO::S_ATTR_DEBUG,
243                          SectionKind::getMetadata());
244   DwarfARangesSection =
245     Ctx->getMachOSection("__DWARF", "__debug_aranges",
246                          MachO::S_ATTR_DEBUG,
247                          SectionKind::getMetadata());
248   DwarfRangesSection =
249     Ctx->getMachOSection("__DWARF", "__debug_ranges",
250                          MachO::S_ATTR_DEBUG,
251                          SectionKind::getMetadata());
252   DwarfMacroInfoSection =
253     Ctx->getMachOSection("__DWARF", "__debug_macinfo",
254                          MachO::S_ATTR_DEBUG,
255                          SectionKind::getMetadata());
256   DwarfDebugInlineSection =
257     Ctx->getMachOSection("__DWARF", "__debug_inlined",
258                          MachO::S_ATTR_DEBUG,
259                          SectionKind::getMetadata());
260   StackMapSection =
261     Ctx->getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps", 0,
262                          SectionKind::getMetadata());
263 
264   TLSExtraDataSection = TLSTLVSection;
265 }
266 
267 void MCObjectFileInfo::InitELFMCObjectFileInfo(Triple T) {
268   switch (T.getArch()) {
269   case Triple::mips:
270   case Triple::mipsel:
271     FDECFIEncoding = dwarf::DW_EH_PE_sdata4;
272     break;
273   case Triple::mips64:
274   case Triple::mips64el:
275     FDECFIEncoding = dwarf::DW_EH_PE_sdata8;
276     break;
277   default:
278     FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
279     break;
280   }
281 
282   switch (T.getArch()) {
283   case Triple::arm:
284   case Triple::armeb:
285   case Triple::thumb:
286   case Triple::thumbeb:
287     if (Ctx->getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM)
288       break;
289     // Fallthrough if not using EHABI
290   case Triple::ppc:
291   case Triple::x86:
292     PersonalityEncoding = (RelocM == Reloc::PIC_)
293      ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
294      : dwarf::DW_EH_PE_absptr;
295     LSDAEncoding = (RelocM == Reloc::PIC_)
296       ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
297       : dwarf::DW_EH_PE_absptr;
298     TTypeEncoding = (RelocM == Reloc::PIC_)
299      ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
300      : dwarf::DW_EH_PE_absptr;
301     break;
302   case Triple::x86_64:
303     if (RelocM == Reloc::PIC_) {
304       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
305         ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
306          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
307       LSDAEncoding = dwarf::DW_EH_PE_pcrel |
308         (CMModel == CodeModel::Small
309          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
310       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
311         ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
312          ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
313     } else {
314       PersonalityEncoding =
315         (CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
316         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
317       LSDAEncoding = (CMModel == CodeModel::Small)
318         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
319       TTypeEncoding = (CMModel == CodeModel::Small)
320         ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
321     }
322     break;
323   case Triple::aarch64:
324   case Triple::aarch64_be:
325   case Triple::arm64:
326   case Triple::arm64_be:
327     // The small model guarantees static code/data size < 4GB, but not where it
328     // will be in memory. Most of these could end up >2GB away so even a signed
329     // pc-relative 32-bit address is insufficient, theoretically.
330     if (RelocM == Reloc::PIC_) {
331       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
332         dwarf::DW_EH_PE_sdata8;
333       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8;
334       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
335         dwarf::DW_EH_PE_sdata8;
336     } else {
337       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
338       LSDAEncoding = dwarf::DW_EH_PE_absptr;
339       TTypeEncoding = dwarf::DW_EH_PE_absptr;
340     }
341     break;
342   case Triple::mips:
343   case Triple::mipsel:
344     // MIPS uses indirect pointer to refer personality functions, so that the
345     // eh_frame section can be read-only.  DW.ref.personality will be generated
346     // for relocation.
347     PersonalityEncoding = dwarf::DW_EH_PE_indirect;
348     break;
349   case Triple::ppc64:
350   case Triple::ppc64le:
351     PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
352       dwarf::DW_EH_PE_udata8;
353     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
354     TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
355       dwarf::DW_EH_PE_udata8;
356     break;
357   case Triple::sparc:
358     if (RelocM == Reloc::PIC_) {
359       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
360       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
361         dwarf::DW_EH_PE_sdata4;
362       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
363         dwarf::DW_EH_PE_sdata4;
364     } else {
365       LSDAEncoding = dwarf::DW_EH_PE_absptr;
366       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
367       TTypeEncoding = dwarf::DW_EH_PE_absptr;
368     }
369     break;
370   case Triple::sparcv9:
371     LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
372     if (RelocM == Reloc::PIC_) {
373       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
374         dwarf::DW_EH_PE_sdata4;
375       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
376         dwarf::DW_EH_PE_sdata4;
377     } else {
378       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
379       TTypeEncoding = dwarf::DW_EH_PE_absptr;
380     }
381     break;
382   case Triple::systemz:
383     // All currently-defined code models guarantee that 4-byte PC-relative
384     // values will be in range.
385     if (RelocM == Reloc::PIC_) {
386       PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
387         dwarf::DW_EH_PE_sdata4;
388       LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
389       TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
390         dwarf::DW_EH_PE_sdata4;
391     } else {
392       PersonalityEncoding = dwarf::DW_EH_PE_absptr;
393       LSDAEncoding = dwarf::DW_EH_PE_absptr;
394       TTypeEncoding = dwarf::DW_EH_PE_absptr;
395     }
396     break;
397   default:
398     break;
399   }
400 
401   // Solaris requires different flags for .eh_frame to seemingly every other
402   // platform.
403   EHSectionType = ELF::SHT_PROGBITS;
404   EHSectionFlags = ELF::SHF_ALLOC;
405   if (T.getOS() == Triple::Solaris) {
406     if (T.getArch() == Triple::x86_64)
407       EHSectionType = ELF::SHT_X86_64_UNWIND;
408     else
409       EHSectionFlags |= ELF::SHF_WRITE;
410   }
411 
412 
413   // ELF
414   BSSSection =
415     Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
416                        ELF::SHF_WRITE | ELF::SHF_ALLOC,
417                        SectionKind::getBSS());
418 
419   TextSection =
420     Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
421                        ELF::SHF_EXECINSTR |
422                        ELF::SHF_ALLOC,
423                        SectionKind::getText());
424 
425   DataSection =
426     Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
427                        ELF::SHF_WRITE |ELF::SHF_ALLOC,
428                        SectionKind::getDataRel());
429 
430   ReadOnlySection =
431     Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS,
432                        ELF::SHF_ALLOC,
433                        SectionKind::getReadOnly());
434 
435   TLSDataSection =
436     Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
437                        ELF::SHF_ALLOC | ELF::SHF_TLS |
438                        ELF::SHF_WRITE,
439                        SectionKind::getThreadData());
440 
441   TLSBSSSection =
442     Ctx->getELFSection(".tbss", ELF::SHT_NOBITS,
443                        ELF::SHF_ALLOC | ELF::SHF_TLS |
444                        ELF::SHF_WRITE,
445                        SectionKind::getThreadBSS());
446 
447   DataRelSection =
448     Ctx->getELFSection(".data.rel", ELF::SHT_PROGBITS,
449                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
450                        SectionKind::getDataRel());
451 
452   DataRelLocalSection =
453     Ctx->getELFSection(".data.rel.local", ELF::SHT_PROGBITS,
454                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
455                        SectionKind::getDataRelLocal());
456 
457   DataRelROSection =
458     Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
459                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
460                        SectionKind::getReadOnlyWithRel());
461 
462   DataRelROLocalSection =
463     Ctx->getELFSection(".data.rel.ro.local", ELF::SHT_PROGBITS,
464                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
465                        SectionKind::getReadOnlyWithRelLocal());
466 
467   MergeableConst4Section =
468     Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
469                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
470                        SectionKind::getMergeableConst4());
471 
472   MergeableConst8Section =
473     Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
474                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
475                        SectionKind::getMergeableConst8());
476 
477   MergeableConst16Section =
478     Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
479                        ELF::SHF_ALLOC |ELF::SHF_MERGE,
480                        SectionKind::getMergeableConst16());
481 
482   StaticCtorSection =
483     Ctx->getELFSection(".ctors", ELF::SHT_PROGBITS,
484                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
485                        SectionKind::getDataRel());
486 
487   StaticDtorSection =
488     Ctx->getELFSection(".dtors", ELF::SHT_PROGBITS,
489                        ELF::SHF_ALLOC |ELF::SHF_WRITE,
490                        SectionKind::getDataRel());
491 
492   // Exception Handling Sections.
493 
494   // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
495   // it contains relocatable pointers.  In PIC mode, this is probably a big
496   // runtime hit for C++ apps.  Either the contents of the LSDA need to be
497   // adjusted or this should be a data section.
498   LSDASection =
499     Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
500                        ELF::SHF_ALLOC,
501                        SectionKind::getReadOnly());
502 
503   COFFDebugSymbolsSection = nullptr;
504 
505   // Debug Info Sections.
506   DwarfAbbrevSection =
507     Ctx->getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
508                        SectionKind::getMetadata());
509   DwarfInfoSection =
510     Ctx->getELFSection(".debug_info", ELF::SHT_PROGBITS, 0,
511                        SectionKind::getMetadata());
512   DwarfLineSection =
513     Ctx->getELFSection(".debug_line", ELF::SHT_PROGBITS, 0,
514                        SectionKind::getMetadata());
515   DwarfFrameSection =
516     Ctx->getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0,
517                        SectionKind::getMetadata());
518   DwarfPubNamesSection =
519     Ctx->getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0,
520                        SectionKind::getMetadata());
521   DwarfPubTypesSection =
522     Ctx->getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0,
523                        SectionKind::getMetadata());
524   DwarfGnuPubNamesSection =
525     Ctx->getELFSection(".debug_gnu_pubnames", ELF::SHT_PROGBITS, 0,
526                        SectionKind::getMetadata());
527   DwarfGnuPubTypesSection =
528     Ctx->getELFSection(".debug_gnu_pubtypes", ELF::SHT_PROGBITS, 0,
529                        SectionKind::getMetadata());
530   DwarfStrSection =
531     Ctx->getELFSection(".debug_str", ELF::SHT_PROGBITS,
532                        ELF::SHF_MERGE | ELF::SHF_STRINGS,
533                        SectionKind::getMergeable1ByteCString());
534   DwarfLocSection =
535     Ctx->getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0,
536                        SectionKind::getMetadata());
537   DwarfARangesSection =
538     Ctx->getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0,
539                        SectionKind::getMetadata());
540   DwarfRangesSection =
541     Ctx->getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0,
542                        SectionKind::getMetadata());
543   DwarfMacroInfoSection =
544     Ctx->getELFSection(".debug_macinfo", ELF::SHT_PROGBITS, 0,
545                        SectionKind::getMetadata());
546 
547   // DWARF5 Experimental Debug Info
548 
549   // Accelerator Tables
550   DwarfAccelNamesSection =
551     Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0,
552                        SectionKind::getMetadata());
553   DwarfAccelObjCSection =
554     Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0,
555                        SectionKind::getMetadata());
556   DwarfAccelNamespaceSection =
557     Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0,
558                        SectionKind::getMetadata());
559   DwarfAccelTypesSection =
560     Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0,
561                        SectionKind::getMetadata());
562 
563   // Fission Sections
564   DwarfInfoDWOSection =
565     Ctx->getELFSection(".debug_info.dwo", ELF::SHT_PROGBITS, 0,
566                        SectionKind::getMetadata());
567   DwarfAbbrevDWOSection =
568     Ctx->getELFSection(".debug_abbrev.dwo", ELF::SHT_PROGBITS, 0,
569                        SectionKind::getMetadata());
570   DwarfStrDWOSection =
571     Ctx->getELFSection(".debug_str.dwo", ELF::SHT_PROGBITS,
572                        ELF::SHF_MERGE | ELF::SHF_STRINGS,
573                        SectionKind::getMergeable1ByteCString());
574   DwarfLineDWOSection =
575     Ctx->getELFSection(".debug_line.dwo", ELF::SHT_PROGBITS, 0,
576                        SectionKind::getMetadata());
577   DwarfLocDWOSection =
578     Ctx->getELFSection(".debug_loc.dwo", ELF::SHT_PROGBITS, 0,
579                        SectionKind::getMetadata());
580   DwarfStrOffDWOSection =
581     Ctx->getELFSection(".debug_str_offsets.dwo", ELF::SHT_PROGBITS, 0,
582                        SectionKind::getMetadata());
583   DwarfAddrSection =
584     Ctx->getELFSection(".debug_addr", ELF::SHT_PROGBITS, 0,
585                        SectionKind::getMetadata());
586 }
587 
588 
589 void MCObjectFileInfo::InitCOFFMCObjectFileInfo(Triple T) {
590   bool IsWoA = T.getArch() == Triple::arm || T.getArch() == Triple::thumb;
591 
592   // The object file format cannot represent common symbols with explicit
593   // alignments.
594   CommDirectiveSupportsAlignment = false;
595 
596   // COFF
597   BSSSection =
598     Ctx->getCOFFSection(".bss",
599                         COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
600                         COFF::IMAGE_SCN_MEM_READ |
601                         COFF::IMAGE_SCN_MEM_WRITE,
602                         SectionKind::getBSS());
603   TextSection =
604     Ctx->getCOFFSection(".text",
605                         (IsWoA ? COFF::IMAGE_SCN_MEM_16BIT
606                                : (COFF::SectionCharacteristics)0) |
607                         COFF::IMAGE_SCN_CNT_CODE |
608                         COFF::IMAGE_SCN_MEM_EXECUTE |
609                         COFF::IMAGE_SCN_MEM_READ,
610                         SectionKind::getText());
611   DataSection =
612     Ctx->getCOFFSection(".data",
613                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
614                         COFF::IMAGE_SCN_MEM_READ |
615                         COFF::IMAGE_SCN_MEM_WRITE,
616                         SectionKind::getDataRel());
617   ReadOnlySection =
618     Ctx->getCOFFSection(".rdata",
619                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
620                         COFF::IMAGE_SCN_MEM_READ,
621                         SectionKind::getReadOnly());
622 
623   if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
624     StaticCtorSection =
625       Ctx->getCOFFSection(".CRT$XCU",
626                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
627                           COFF::IMAGE_SCN_MEM_READ,
628                           SectionKind::getReadOnly());
629     StaticDtorSection =
630       Ctx->getCOFFSection(".CRT$XTX",
631                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
632                           COFF::IMAGE_SCN_MEM_READ,
633                           SectionKind::getReadOnly());
634   } else {
635     StaticCtorSection =
636       Ctx->getCOFFSection(".ctors",
637                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
638                           COFF::IMAGE_SCN_MEM_READ |
639                           COFF::IMAGE_SCN_MEM_WRITE,
640                           SectionKind::getDataRel());
641     StaticDtorSection =
642       Ctx->getCOFFSection(".dtors",
643                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
644                           COFF::IMAGE_SCN_MEM_READ |
645                           COFF::IMAGE_SCN_MEM_WRITE,
646                           SectionKind::getDataRel());
647   }
648 
649   // FIXME: We're emitting LSDA info into a readonly section on COFF, even
650   // though it contains relocatable pointers.  In PIC mode, this is probably a
651   // big runtime hit for C++ apps.  Either the contents of the LSDA need to be
652   // adjusted or this should be a data section.
653   assert(T.isOSWindows() && "Windows is the only supported COFF target");
654   if (T.getArch() == Triple::x86_64) {
655     // On Windows 64 with SEH, the LSDA is emitted into the .xdata section
656     LSDASection = 0;
657   } else {
658     LSDASection = Ctx->getCOFFSection(".gcc_except_table",
659                                       COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
660                                           COFF::IMAGE_SCN_MEM_READ,
661                                       SectionKind::getReadOnly());
662   }
663 
664   // Debug info.
665   COFFDebugSymbolsSection =
666     Ctx->getCOFFSection(".debug$S",
667                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
668                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
669                         COFF::IMAGE_SCN_MEM_READ,
670                         SectionKind::getMetadata());
671 
672   DwarfAbbrevSection =
673     Ctx->getCOFFSection(".debug_abbrev",
674                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
675                         COFF::IMAGE_SCN_MEM_READ,
676                         SectionKind::getMetadata());
677   DwarfInfoSection =
678     Ctx->getCOFFSection(".debug_info",
679                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
680                         COFF::IMAGE_SCN_MEM_READ,
681                         SectionKind::getMetadata());
682   DwarfLineSection =
683     Ctx->getCOFFSection(".debug_line",
684                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
685                         COFF::IMAGE_SCN_MEM_READ,
686                         SectionKind::getMetadata());
687   DwarfFrameSection =
688     Ctx->getCOFFSection(".debug_frame",
689                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
690                         COFF::IMAGE_SCN_MEM_READ,
691                         SectionKind::getMetadata());
692   DwarfPubNamesSection =
693     Ctx->getCOFFSection(".debug_pubnames",
694                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
695                         COFF::IMAGE_SCN_MEM_READ,
696                         SectionKind::getMetadata());
697   DwarfPubTypesSection =
698     Ctx->getCOFFSection(".debug_pubtypes",
699                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
700                         COFF::IMAGE_SCN_MEM_READ,
701                         SectionKind::getMetadata());
702   DwarfGnuPubNamesSection =
703     Ctx->getCOFFSection(".debug_gnu_pubnames",
704                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
705                         COFF::IMAGE_SCN_MEM_READ,
706                         SectionKind::getMetadata());
707   DwarfGnuPubTypesSection =
708     Ctx->getCOFFSection(".debug_gnu_pubtypes",
709                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
710                         COFF::IMAGE_SCN_MEM_READ,
711                         SectionKind::getMetadata());
712   DwarfStrSection =
713     Ctx->getCOFFSection(".debug_str",
714                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
715                         COFF::IMAGE_SCN_MEM_READ,
716                         SectionKind::getMetadata());
717   DwarfLocSection =
718     Ctx->getCOFFSection(".debug_loc",
719                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
720                         COFF::IMAGE_SCN_MEM_READ,
721                         SectionKind::getMetadata());
722   DwarfARangesSection =
723     Ctx->getCOFFSection(".debug_aranges",
724                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
725                         COFF::IMAGE_SCN_MEM_READ,
726                         SectionKind::getMetadata());
727   DwarfRangesSection =
728     Ctx->getCOFFSection(".debug_ranges",
729                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
730                         COFF::IMAGE_SCN_MEM_READ,
731                         SectionKind::getMetadata());
732   DwarfMacroInfoSection =
733     Ctx->getCOFFSection(".debug_macinfo",
734                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
735                         COFF::IMAGE_SCN_MEM_READ,
736                         SectionKind::getMetadata());
737   DwarfInfoDWOSection =
738       Ctx->getCOFFSection(".debug_info.dwo",
739                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
740                           COFF::IMAGE_SCN_MEM_READ,
741                           SectionKind::getMetadata());
742   DwarfAbbrevDWOSection =
743       Ctx->getCOFFSection(".debug_abbrev.dwo",
744                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
745                           COFF::IMAGE_SCN_MEM_READ,
746                           SectionKind::getMetadata());
747   DwarfStrDWOSection =
748       Ctx->getCOFFSection(".debug_str.dwo",
749                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
750                           COFF::IMAGE_SCN_MEM_READ,
751                           SectionKind::getMetadata());
752   DwarfLineDWOSection =
753       Ctx->getCOFFSection(".debug_line.dwo",
754                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
755                           COFF::IMAGE_SCN_MEM_READ,
756                           SectionKind::getMetadata());
757   DwarfLocDWOSection =
758       Ctx->getCOFFSection(".debug_loc.dwo",
759                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
760                           COFF::IMAGE_SCN_MEM_READ,
761                           SectionKind::getMetadata());
762   DwarfStrOffDWOSection =
763       Ctx->getCOFFSection(".debug_str_offsets.dwo",
764                           COFF::IMAGE_SCN_MEM_DISCARDABLE |
765                           COFF::IMAGE_SCN_MEM_READ,
766                           SectionKind::getMetadata());
767 
768   DwarfAddrSection =
769     Ctx->getCOFFSection(".debug_addr",
770                         COFF::IMAGE_SCN_MEM_DISCARDABLE |
771                         COFF::IMAGE_SCN_MEM_READ,
772                         SectionKind::getMetadata());
773 
774   DrectveSection =
775     Ctx->getCOFFSection(".drectve",
776                         COFF::IMAGE_SCN_LNK_INFO |
777                         COFF::IMAGE_SCN_LNK_REMOVE,
778                         SectionKind::getMetadata());
779 
780   PDataSection =
781     Ctx->getCOFFSection(".pdata",
782                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
783                         COFF::IMAGE_SCN_MEM_READ,
784                         SectionKind::getDataRel());
785 
786   XDataSection =
787     Ctx->getCOFFSection(".xdata",
788                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
789                         COFF::IMAGE_SCN_MEM_READ,
790                         SectionKind::getDataRel());
791 
792   TLSDataSection =
793     Ctx->getCOFFSection(".tls$",
794                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
795                         COFF::IMAGE_SCN_MEM_READ |
796                         COFF::IMAGE_SCN_MEM_WRITE,
797                         SectionKind::getDataRel());
798 }
799 
800 void MCObjectFileInfo::InitMCObjectFileInfo(StringRef T, Reloc::Model relocm,
801                                             CodeModel::Model cm,
802                                             MCContext &ctx) {
803   RelocM = relocm;
804   CMModel = cm;
805   Ctx = &ctx;
806 
807   // Common.
808   CommDirectiveSupportsAlignment = true;
809   SupportsWeakOmittedEHFrame = true;
810   SupportsCompactUnwindWithoutEHFrame = false;
811 
812   PersonalityEncoding = LSDAEncoding = FDECFIEncoding = TTypeEncoding =
813       dwarf::DW_EH_PE_absptr;
814 
815   CompactUnwindDwarfEHFrameOnly = 0;
816 
817   EHFrameSection = nullptr;             // Created on demand.
818   CompactUnwindSection = nullptr;       // Used only by selected targets.
819   DwarfAccelNamesSection = nullptr;     // Used only by selected targets.
820   DwarfAccelObjCSection = nullptr;      // Used only by selected targets.
821   DwarfAccelNamespaceSection = nullptr; // Used only by selected targets.
822   DwarfAccelTypesSection = nullptr;     // Used only by selected targets.
823 
824   TT = Triple(T);
825 
826   Triple::ArchType Arch = TT.getArch();
827   // FIXME: Checking for Arch here to filter out bogus triples such as
828   // cellspu-apple-darwin. Perhaps we should fix in Triple?
829   if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
830        Arch == Triple::arm || Arch == Triple::thumb ||
831        Arch == Triple::arm64 || Arch == Triple::aarch64 ||
832        Arch == Triple::ppc || Arch == Triple::ppc64 ||
833        Arch == Triple::UnknownArch) &&
834       (TT.isOSDarwin() || TT.isOSBinFormatMachO())) {
835     Env = IsMachO;
836     InitMachOMCObjectFileInfo(TT);
837   } else if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
838               Arch == Triple::arm || Arch == Triple::thumb) &&
839              (TT.isOSWindows() && TT.getObjectFormat() == Triple::COFF)) {
840     Env = IsCOFF;
841     InitCOFFMCObjectFileInfo(TT);
842   } else {
843     Env = IsELF;
844     InitELFMCObjectFileInfo(TT);
845   }
846 }
847 
848 const MCSection *MCObjectFileInfo::getDwarfTypesSection(uint64_t Hash) const {
849   return Ctx->getELFSection(".debug_types", ELF::SHT_PROGBITS, ELF::SHF_GROUP,
850                             SectionKind::getMetadata(), 0, utostr(Hash));
851 }
852 
853 const MCSection *
854 MCObjectFileInfo::getDwarfTypesDWOSection(uint64_t Hash) const {
855   return Ctx->getELFSection(".debug_types.dwo", ELF::SHT_PROGBITS,
856                             ELF::SHF_GROUP, SectionKind::getMetadata(), 0,
857                             utostr(Hash));
858 }
859 
860 void MCObjectFileInfo::InitEHFrameSection() {
861   if (Env == IsMachO)
862     EHFrameSection =
863       Ctx->getMachOSection("__TEXT", "__eh_frame",
864                            MachO::S_COALESCED |
865                            MachO::S_ATTR_NO_TOC |
866                            MachO::S_ATTR_STRIP_STATIC_SYMS |
867                            MachO::S_ATTR_LIVE_SUPPORT,
868                            SectionKind::getReadOnly());
869   else if (Env == IsELF)
870     EHFrameSection =
871       Ctx->getELFSection(".eh_frame", EHSectionType,
872                          EHSectionFlags,
873                          SectionKind::getDataRel());
874   else
875     EHFrameSection =
876       Ctx->getCOFFSection(".eh_frame",
877                           COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
878                           COFF::IMAGE_SCN_MEM_READ |
879                           COFF::IMAGE_SCN_MEM_WRITE,
880                           SectionKind::getDataRel());
881 }
882