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