b76aaa47239857b2419fd4236a5c1447bdc83429
[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   // 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   IsFunctionEHFrameSymbolPrivate = false;
45   SupportsWeakOmittedEHFrame = false;
46
47   if (T.isOSDarwin() &&
48       (T.getArch() == Triple::arm64 || T.getArch() == Triple::aarch64))
49     SupportsCompactUnwindWithoutEHFrame = true;
50
51   PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel
52     | dwarf::DW_EH_PE_sdata4;
53   LSDAEncoding = FDECFIEncoding = dwarf::DW_EH_PE_pcrel;
54   TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
55     dwarf::DW_EH_PE_sdata4;
56
57   // .comm doesn't support alignment before Leopard.
58   if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
59     CommDirectiveSupportsAlignment = false;
60
61   TextSection // .text
62     = Ctx->getMachOSection("__TEXT", "__text",
63                            MachO::S_ATTR_PURE_INSTRUCTIONS,
64                            SectionKind::getText());
65   DataSection // .data
66     = Ctx->getMachOSection("__DATA", "__data", 0,
67                            SectionKind::getDataRel());
68
69   // BSSSection might not be expected initialized on msvc.
70   BSSSection = nullptr;
71
72   TLSDataSection // .tdata
73     = Ctx->getMachOSection("__DATA", "__thread_data",
74                            MachO::S_THREAD_LOCAL_REGULAR,
75                            SectionKind::getDataRel());
76   TLSBSSSection // .tbss
77     = Ctx->getMachOSection("__DATA", "__thread_bss",
78                            MachO::S_THREAD_LOCAL_ZEROFILL,
79                            SectionKind::getThreadBSS());
80
81   // TODO: Verify datarel below.
82   TLSTLVSection // .tlv
83     = Ctx->getMachOSection("__DATA", "__thread_vars",
84                            MachO::S_THREAD_LOCAL_VARIABLES,
85                            SectionKind::getDataRel());
86
87   TLSThreadInitSection
88     = Ctx->getMachOSection("__DATA", "__thread_init",
89                           MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
90                           SectionKind::getDataRel());
91
92   CStringSection // .cstring
93     = Ctx->getMachOSection("__TEXT", "__cstring",
94                            MachO::S_CSTRING_LITERALS,
95                            SectionKind::getMergeable1ByteCString());
96   UStringSection
97     = Ctx->getMachOSection("__TEXT","__ustring", 0,
98                            SectionKind::getMergeable2ByteCString());
99   FourByteConstantSection // .literal4
100     = Ctx->getMachOSection("__TEXT", "__literal4",
101                            MachO::S_4BYTE_LITERALS,
102                            SectionKind::getMergeableConst4());
103   EightByteConstantSection // .literal8
104     = Ctx->getMachOSection("__TEXT", "__literal8",
105                            MachO::S_8BYTE_LITERALS,
106                            SectionKind::getMergeableConst8());
107
108   SixteenByteConstantSection // .literal16
109       = Ctx->getMachOSection("__TEXT", "__literal16",
110                              MachO::S_16BYTE_LITERALS,
111                              SectionKind::getMergeableConst16());
112
113   ReadOnlySection  // .const
114     = Ctx->getMachOSection("__TEXT", "__const", 0,
115                            SectionKind::getReadOnly());
116
117   TextCoalSection
118     = Ctx->getMachOSection("__TEXT", "__textcoal_nt",
119                            MachO::S_COALESCED |
120                            MachO::S_ATTR_PURE_INSTRUCTIONS,
121                            SectionKind::getText());
122   ConstTextCoalSection
123     = Ctx->getMachOSection("__TEXT", "__const_coal",
124                            MachO::S_COALESCED,
125                            SectionKind::getReadOnly());
126   ConstDataSection  // .const_data
127     = Ctx->getMachOSection("__DATA", "__const", 0,
128                            SectionKind::getReadOnlyWithRel());
129   DataCoalSection
130     = Ctx->getMachOSection("__DATA","__datacoal_nt",
131                            MachO::S_COALESCED,
132                            SectionKind::getDataRel());
133   DataCommonSection
134     = Ctx->getMachOSection("__DATA","__common",
135                            MachO::S_ZEROFILL,
136                            SectionKind::getBSS());
137   DataBSSSection
138     = Ctx->getMachOSection("__DATA","__bss", MachO::S_ZEROFILL,
139                            SectionKind::getBSS());
140
141
142   LazySymbolPointerSection
143     = Ctx->getMachOSection("__DATA", "__la_symbol_ptr",
144                            MachO::S_LAZY_SYMBOL_POINTERS,
145                            SectionKind::getMetadata());
146   NonLazySymbolPointerSection
147     = Ctx->getMachOSection("__DATA", "__nl_symbol_ptr",
148                            MachO::S_NON_LAZY_SYMBOL_POINTERS,
149                            SectionKind::getMetadata());
150
151   if (RelocM == Reloc::Static) {
152     StaticCtorSection
153       = Ctx->getMachOSection("__TEXT", "__constructor", 0,
154                              SectionKind::getDataRel());
155     StaticDtorSection
156       = Ctx->getMachOSection("__TEXT", "__destructor", 0,
157                              SectionKind::getDataRel());
158   } else {
159     StaticCtorSection
160       = Ctx->getMachOSection("__DATA", "__mod_init_func",
161                              MachO::S_MOD_INIT_FUNC_POINTERS,
162                              SectionKind::getDataRel());
163     StaticDtorSection
164       = Ctx->getMachOSection("__DATA", "__mod_term_func",
165                              MachO::S_MOD_TERM_FUNC_POINTERS,
166                              SectionKind::getDataRel());
167   }
168
169   // Exception Handling.
170   LSDASection = Ctx->getMachOSection("__TEXT", "__gcc_except_tab", 0,
171                                      SectionKind::getReadOnlyWithRel());
172
173   COFFDebugSymbolsSection = nullptr;
174
175   if (useCompactUnwind(T)) {
176     CompactUnwindSection =
177         Ctx->getMachOSection("__LD", "__compact_unwind", MachO::S_ATTR_DEBUG,
178                              SectionKind::getReadOnly());
179
180     if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::x86)
181       CompactUnwindDwarfEHFrameOnly = 0x04000000;
182     else if (T.getArch() == Triple::arm64 || T.getArch() == Triple::aarch64)
183       CompactUnwindDwarfEHFrameOnly = 0x03000000;
184   }
185
186   // Debug Information.
187   DwarfAccelNamesSection =
188     Ctx->getMachOSection("__DWARF", "__apple_names",
189                          MachO::S_ATTR_DEBUG,
190                          SectionKind::getMetadata());
191   DwarfAccelObjCSection =
192     Ctx->getMachOSection("__DWARF", "__apple_objc",
193                          MachO::S_ATTR_DEBUG,
194                          SectionKind::getMetadata());
195   // 16 character section limit...
196   DwarfAccelNamespaceSection =
197     Ctx->getMachOSection("__DWARF", "__apple_namespac",
198                          MachO::S_ATTR_DEBUG,
199                          SectionKind::getMetadata());
200   DwarfAccelTypesSection =
201     Ctx->getMachOSection("__DWARF", "__apple_types",
202                          MachO::S_ATTR_DEBUG,
203                          SectionKind::getMetadata());
204
205   DwarfAbbrevSection =
206     Ctx->getMachOSection("__DWARF", "__debug_abbrev",
207                          MachO::S_ATTR_DEBUG,
208                          SectionKind::getMetadata());
209   DwarfInfoSection =
210     Ctx->getMachOSection("__DWARF", "__debug_info",
211                          MachO::S_ATTR_DEBUG,
212                          SectionKind::getMetadata());
213   DwarfLineSection =
214     Ctx->getMachOSection("__DWARF", "__debug_line",
215                          MachO::S_ATTR_DEBUG,
216                          SectionKind::getMetadata());
217   DwarfFrameSection =
218     Ctx->getMachOSection("__DWARF", "__debug_frame",
219                          MachO::S_ATTR_DEBUG,
220                          SectionKind::getMetadata());
221   DwarfPubNamesSection =
222     Ctx->getMachOSection("__DWARF", "__debug_pubnames",
223                          MachO::S_ATTR_DEBUG,
224                          SectionKind::getMetadata());
225   DwarfPubTypesSection =
226     Ctx->getMachOSection("__DWARF", "__debug_pubtypes",
227                          MachO::S_ATTR_DEBUG,
228                          SectionKind::getMetadata());
229   DwarfGnuPubNamesSection =
230     Ctx->getMachOSection("__DWARF", "__debug_gnu_pubn",
231                          MachO::S_ATTR_DEBUG,
232                          SectionKind::getMetadata());
233   DwarfGnuPubTypesSection =
234     Ctx->getMachOSection("__DWARF", "__debug_gnu_pubt",
235                          MachO::S_ATTR_DEBUG,
236                          SectionKind::getMetadata());
237   DwarfStrSection =
238     Ctx->getMachOSection("__DWARF", "__debug_str",
239                          MachO::S_ATTR_DEBUG,
240                          SectionKind::getMetadata());
241   DwarfLocSection =
242     Ctx->getMachOSection("__DWARF", "__debug_loc",
243                          MachO::S_ATTR_DEBUG,
244                          SectionKind::getMetadata());
245   DwarfARangesSection =
246     Ctx->getMachOSection("__DWARF", "__debug_aranges",
247                          MachO::S_ATTR_DEBUG,
248                          SectionKind::getMetadata());
249   DwarfRangesSection =
250     Ctx->getMachOSection("__DWARF", "__debug_ranges",
251                          MachO::S_ATTR_DEBUG,
252                          SectionKind::getMetadata());
253   DwarfMacroInfoSection =
254     Ctx->getMachOSection("__DWARF", "__debug_macinfo",
255                          MachO::S_ATTR_DEBUG,
256                          SectionKind::getMetadata());
257   DwarfDebugInlineSection =
258     Ctx->getMachOSection("__DWARF", "__debug_inlined",
259                          MachO::S_ATTR_DEBUG,
260                          SectionKind::getMetadata());
261   StackMapSection =
262     Ctx->getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps", 0,
263                          SectionKind::getMetadata());
264
265   TLSExtraDataSection = TLSTLVSection;
266 }
267
268 void MCObjectFileInfo::InitELFMCObjectFileInfo(Triple T) {
269   switch (T.getArch()) {
270   case Triple::mips:
271   case Triple::mipsel:
272     FDECFIEncoding = dwarf::DW_EH_PE_sdata4;
273     break;
274   case Triple::mips64:
275   case Triple::mips64el:
276     FDECFIEncoding = dwarf::DW_EH_PE_sdata8;
277     break;
278   default:
279     FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
280     break;
281   }
282
283   switch (T.getArch()) {
284   case Triple::arm:
285   case Triple::armeb:
286   case Triple::thumb:
287   case Triple::thumbeb:
288     if (Ctx->getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM)
289       break;
290     // Fallthrough if not using EHABI
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   LSDASection =
654     Ctx->getCOFFSection(".gcc_except_table",
655                         COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
656                         COFF::IMAGE_SCN_MEM_READ,
657                         SectionKind::getReadOnly());
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 }