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