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