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