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