MCObjectFileInfo.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. //===-- MCObjectFileInfo.cpp - Object File Information --------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/MC/MCObjectFileInfo.h"
  9. #include "llvm/ADT/StringExtras.h"
  10. #include "llvm/ADT/Triple.h"
  11. #include "llvm/BinaryFormat/COFF.h"
  12. #include "llvm/BinaryFormat/ELF.h"
  13. #include "llvm/BinaryFormat/Wasm.h"
  14. #include "llvm/MC/MCAsmInfo.h"
  15. #include "llvm/MC/MCContext.h"
  16. #include "llvm/MC/MCSection.h"
  17. #include "llvm/MC/MCSectionCOFF.h"
  18. #include "llvm/MC/MCSectionELF.h"
  19. #include "llvm/MC/MCSectionGOFF.h"
  20. #include "llvm/MC/MCSectionMachO.h"
  21. #include "llvm/MC/MCSectionWasm.h"
  22. #include "llvm/MC/MCSectionXCOFF.h"
  23. using namespace llvm;
  24. static bool useCompactUnwind(const Triple &T) {
  25. // Only on darwin.
  26. if (!T.isOSDarwin())
  27. return false;
  28. // aarch64 always has it.
  29. if (T.getArch() == Triple::aarch64 || T.getArch() == Triple::aarch64_32)
  30. return true;
  31. // armv7k always has it.
  32. if (T.isWatchABI())
  33. return true;
  34. // Use it on newer version of OS X.
  35. if (T.isMacOSX() && !T.isMacOSXVersionLT(10, 6))
  36. return true;
  37. // And the iOS simulator.
  38. if (T.isiOS() && T.isX86())
  39. return true;
  40. return false;
  41. }
  42. void MCObjectFileInfo::initMachOMCObjectFileInfo(const Triple &T) {
  43. // MachO
  44. SupportsWeakOmittedEHFrame = false;
  45. EHFrameSection = Ctx->getMachOSection(
  46. "__TEXT", "__eh_frame",
  47. MachO::S_COALESCED | MachO::S_ATTR_NO_TOC |
  48. MachO::S_ATTR_STRIP_STATIC_SYMS | MachO::S_ATTR_LIVE_SUPPORT,
  49. SectionKind::getReadOnly());
  50. if (T.isOSDarwin() &&
  51. (T.getArch() == Triple::aarch64 || T.getArch() == Triple::aarch64_32))
  52. SupportsCompactUnwindWithoutEHFrame = true;
  53. if (T.isWatchABI())
  54. OmitDwarfIfHaveCompactUnwind = true;
  55. FDECFIEncoding = dwarf::DW_EH_PE_pcrel;
  56. // .comm doesn't support alignment before Leopard.
  57. if (T.isMacOSX() && T.isMacOSXVersionLT(10, 5))
  58. CommDirectiveSupportsAlignment = false;
  59. TextSection // .text
  60. = Ctx->getMachOSection("__TEXT", "__text",
  61. MachO::S_ATTR_PURE_INSTRUCTIONS,
  62. SectionKind::getText());
  63. DataSection // .data
  64. = Ctx->getMachOSection("__DATA", "__data", 0, SectionKind::getData());
  65. // BSSSection might not be expected initialized on msvc.
  66. BSSSection = nullptr;
  67. TLSDataSection // .tdata
  68. = Ctx->getMachOSection("__DATA", "__thread_data",
  69. MachO::S_THREAD_LOCAL_REGULAR,
  70. SectionKind::getData());
  71. TLSBSSSection // .tbss
  72. = Ctx->getMachOSection("__DATA", "__thread_bss",
  73. MachO::S_THREAD_LOCAL_ZEROFILL,
  74. SectionKind::getThreadBSS());
  75. // TODO: Verify datarel below.
  76. TLSTLVSection // .tlv
  77. = Ctx->getMachOSection("__DATA", "__thread_vars",
  78. MachO::S_THREAD_LOCAL_VARIABLES,
  79. SectionKind::getData());
  80. TLSThreadInitSection = Ctx->getMachOSection(
  81. "__DATA", "__thread_init", MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS,
  82. SectionKind::getData());
  83. CStringSection // .cstring
  84. = Ctx->getMachOSection("__TEXT", "__cstring",
  85. MachO::S_CSTRING_LITERALS,
  86. SectionKind::getMergeable1ByteCString());
  87. UStringSection
  88. = Ctx->getMachOSection("__TEXT","__ustring", 0,
  89. SectionKind::getMergeable2ByteCString());
  90. FourByteConstantSection // .literal4
  91. = Ctx->getMachOSection("__TEXT", "__literal4",
  92. MachO::S_4BYTE_LITERALS,
  93. SectionKind::getMergeableConst4());
  94. EightByteConstantSection // .literal8
  95. = Ctx->getMachOSection("__TEXT", "__literal8",
  96. MachO::S_8BYTE_LITERALS,
  97. SectionKind::getMergeableConst8());
  98. SixteenByteConstantSection // .literal16
  99. = Ctx->getMachOSection("__TEXT", "__literal16",
  100. MachO::S_16BYTE_LITERALS,
  101. SectionKind::getMergeableConst16());
  102. ReadOnlySection // .const
  103. = Ctx->getMachOSection("__TEXT", "__const", 0,
  104. SectionKind::getReadOnly());
  105. // If the target is not powerpc, map the coal sections to the non-coal
  106. // sections.
  107. //
  108. // "__TEXT/__textcoal_nt" => section "__TEXT/__text"
  109. // "__TEXT/__const_coal" => section "__TEXT/__const"
  110. // "__DATA/__datacoal_nt" => section "__DATA/__data"
  111. Triple::ArchType ArchTy = T.getArch();
  112. ConstDataSection // .const_data
  113. = Ctx->getMachOSection("__DATA", "__const", 0,
  114. SectionKind::getReadOnlyWithRel());
  115. if (ArchTy == Triple::ppc || ArchTy == Triple::ppc64) {
  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. DataCoalSection = Ctx->getMachOSection(
  126. "__DATA", "__datacoal_nt", MachO::S_COALESCED, SectionKind::getData());
  127. ConstDataCoalSection = DataCoalSection;
  128. } else {
  129. TextCoalSection = TextSection;
  130. ConstTextCoalSection = ReadOnlySection;
  131. DataCoalSection = DataSection;
  132. ConstDataCoalSection = ConstDataSection;
  133. }
  134. DataCommonSection
  135. = Ctx->getMachOSection("__DATA","__common",
  136. MachO::S_ZEROFILL,
  137. SectionKind::getBSS());
  138. DataBSSSection
  139. = Ctx->getMachOSection("__DATA","__bss", MachO::S_ZEROFILL,
  140. SectionKind::getBSS());
  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. ThreadLocalPointerSection
  150. = Ctx->getMachOSection("__DATA", "__thread_ptr",
  151. MachO::S_THREAD_LOCAL_VARIABLE_POINTERS,
  152. SectionKind::getMetadata());
  153. // Exception Handling.
  154. LSDASection = Ctx->getMachOSection("__TEXT", "__gcc_except_tab", 0,
  155. SectionKind::getReadOnlyWithRel());
  156. COFFDebugSymbolsSection = nullptr;
  157. COFFDebugTypesSection = nullptr;
  158. COFFGlobalTypeHashesSection = nullptr;
  159. if (useCompactUnwind(T)) {
  160. CompactUnwindSection =
  161. Ctx->getMachOSection("__LD", "__compact_unwind", MachO::S_ATTR_DEBUG,
  162. SectionKind::getReadOnly());
  163. if (T.isX86())
  164. CompactUnwindDwarfEHFrameOnly = 0x04000000; // UNWIND_X86_64_MODE_DWARF
  165. else if (T.getArch() == Triple::aarch64 || T.getArch() == Triple::aarch64_32)
  166. CompactUnwindDwarfEHFrameOnly = 0x03000000; // UNWIND_ARM64_MODE_DWARF
  167. else if (T.getArch() == Triple::arm || T.getArch() == Triple::thumb)
  168. CompactUnwindDwarfEHFrameOnly = 0x04000000; // UNWIND_ARM_MODE_DWARF
  169. }
  170. // Debug Information.
  171. DwarfDebugNamesSection =
  172. Ctx->getMachOSection("__DWARF", "__debug_names", MachO::S_ATTR_DEBUG,
  173. SectionKind::getMetadata(), "debug_names_begin");
  174. DwarfAccelNamesSection =
  175. Ctx->getMachOSection("__DWARF", "__apple_names", MachO::S_ATTR_DEBUG,
  176. SectionKind::getMetadata(), "names_begin");
  177. DwarfAccelObjCSection =
  178. Ctx->getMachOSection("__DWARF", "__apple_objc", MachO::S_ATTR_DEBUG,
  179. SectionKind::getMetadata(), "objc_begin");
  180. // 16 character section limit...
  181. DwarfAccelNamespaceSection =
  182. Ctx->getMachOSection("__DWARF", "__apple_namespac", MachO::S_ATTR_DEBUG,
  183. SectionKind::getMetadata(), "namespac_begin");
  184. DwarfAccelTypesSection =
  185. Ctx->getMachOSection("__DWARF", "__apple_types", MachO::S_ATTR_DEBUG,
  186. SectionKind::getMetadata(), "types_begin");
  187. DwarfSwiftASTSection =
  188. Ctx->getMachOSection("__DWARF", "__swift_ast", MachO::S_ATTR_DEBUG,
  189. SectionKind::getMetadata());
  190. DwarfAbbrevSection =
  191. Ctx->getMachOSection("__DWARF", "__debug_abbrev", MachO::S_ATTR_DEBUG,
  192. SectionKind::getMetadata(), "section_abbrev");
  193. DwarfInfoSection =
  194. Ctx->getMachOSection("__DWARF", "__debug_info", MachO::S_ATTR_DEBUG,
  195. SectionKind::getMetadata(), "section_info");
  196. DwarfLineSection =
  197. Ctx->getMachOSection("__DWARF", "__debug_line", MachO::S_ATTR_DEBUG,
  198. SectionKind::getMetadata(), "section_line");
  199. DwarfLineStrSection =
  200. Ctx->getMachOSection("__DWARF", "__debug_line_str", MachO::S_ATTR_DEBUG,
  201. SectionKind::getMetadata(), "section_line_str");
  202. DwarfFrameSection =
  203. Ctx->getMachOSection("__DWARF", "__debug_frame", MachO::S_ATTR_DEBUG,
  204. SectionKind::getMetadata());
  205. DwarfPubNamesSection =
  206. Ctx->getMachOSection("__DWARF", "__debug_pubnames", MachO::S_ATTR_DEBUG,
  207. SectionKind::getMetadata());
  208. DwarfPubTypesSection =
  209. Ctx->getMachOSection("__DWARF", "__debug_pubtypes", MachO::S_ATTR_DEBUG,
  210. SectionKind::getMetadata());
  211. DwarfGnuPubNamesSection =
  212. Ctx->getMachOSection("__DWARF", "__debug_gnu_pubn", MachO::S_ATTR_DEBUG,
  213. SectionKind::getMetadata());
  214. DwarfGnuPubTypesSection =
  215. Ctx->getMachOSection("__DWARF", "__debug_gnu_pubt", MachO::S_ATTR_DEBUG,
  216. SectionKind::getMetadata());
  217. DwarfStrSection =
  218. Ctx->getMachOSection("__DWARF", "__debug_str", MachO::S_ATTR_DEBUG,
  219. SectionKind::getMetadata(), "info_string");
  220. DwarfStrOffSection =
  221. Ctx->getMachOSection("__DWARF", "__debug_str_offs", MachO::S_ATTR_DEBUG,
  222. SectionKind::getMetadata(), "section_str_off");
  223. DwarfAddrSection =
  224. Ctx->getMachOSection("__DWARF", "__debug_addr", MachO::S_ATTR_DEBUG,
  225. SectionKind::getMetadata(), "section_info");
  226. DwarfLocSection =
  227. Ctx->getMachOSection("__DWARF", "__debug_loc", MachO::S_ATTR_DEBUG,
  228. SectionKind::getMetadata(), "section_debug_loc");
  229. DwarfLoclistsSection =
  230. Ctx->getMachOSection("__DWARF", "__debug_loclists", MachO::S_ATTR_DEBUG,
  231. SectionKind::getMetadata(), "section_debug_loc");
  232. DwarfARangesSection =
  233. Ctx->getMachOSection("__DWARF", "__debug_aranges", MachO::S_ATTR_DEBUG,
  234. SectionKind::getMetadata());
  235. DwarfRangesSection =
  236. Ctx->getMachOSection("__DWARF", "__debug_ranges", MachO::S_ATTR_DEBUG,
  237. SectionKind::getMetadata(), "debug_range");
  238. DwarfRnglistsSection =
  239. Ctx->getMachOSection("__DWARF", "__debug_rnglists", MachO::S_ATTR_DEBUG,
  240. SectionKind::getMetadata(), "debug_range");
  241. DwarfMacinfoSection =
  242. Ctx->getMachOSection("__DWARF", "__debug_macinfo", MachO::S_ATTR_DEBUG,
  243. SectionKind::getMetadata(), "debug_macinfo");
  244. DwarfMacroSection =
  245. Ctx->getMachOSection("__DWARF", "__debug_macro", MachO::S_ATTR_DEBUG,
  246. SectionKind::getMetadata(), "debug_macro");
  247. DwarfDebugInlineSection =
  248. Ctx->getMachOSection("__DWARF", "__debug_inlined", MachO::S_ATTR_DEBUG,
  249. SectionKind::getMetadata());
  250. DwarfCUIndexSection =
  251. Ctx->getMachOSection("__DWARF", "__debug_cu_index", MachO::S_ATTR_DEBUG,
  252. SectionKind::getMetadata());
  253. DwarfTUIndexSection =
  254. Ctx->getMachOSection("__DWARF", "__debug_tu_index", MachO::S_ATTR_DEBUG,
  255. SectionKind::getMetadata());
  256. StackMapSection = Ctx->getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps",
  257. 0, SectionKind::getMetadata());
  258. FaultMapSection = Ctx->getMachOSection("__LLVM_FAULTMAPS", "__llvm_faultmaps",
  259. 0, SectionKind::getMetadata());
  260. RemarksSection = Ctx->getMachOSection(
  261. "__LLVM", "__remarks", MachO::S_ATTR_DEBUG, SectionKind::getMetadata());
  262. // The architecture of dsymutil makes it very difficult to copy the Swift
  263. // reflection metadata sections into the __TEXT segment, so dsymutil creates
  264. // these sections in the __DWARF segment instead.
  265. if (!Ctx->getSwift5ReflectionSegmentName().empty()) {
  266. #define HANDLE_SWIFT_SECTION(KIND, MACHO, ELF, COFF) \
  267. Swift5ReflectionSections \
  268. [llvm::binaryformat::Swift5ReflectionSectionKind::KIND] = \
  269. Ctx->getMachOSection(Ctx->getSwift5ReflectionSegmentName().data(), \
  270. MACHO, 0, SectionKind::getMetadata());
  271. #include "llvm/BinaryFormat/Swift.def"
  272. }
  273. TLSExtraDataSection = TLSTLVSection;
  274. }
  275. void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) {
  276. switch (T.getArch()) {
  277. case Triple::mips:
  278. case Triple::mipsel:
  279. case Triple::mips64:
  280. case Triple::mips64el:
  281. // We cannot use DW_EH_PE_sdata8 for the large PositionIndependent case
  282. // since there is no R_MIPS_PC64 relocation (only a 32-bit version).
  283. if (PositionIndependent && !Large)
  284. FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
  285. else
  286. FDECFIEncoding = Ctx->getAsmInfo()->getCodePointerSize() == 4
  287. ? dwarf::DW_EH_PE_sdata4
  288. : dwarf::DW_EH_PE_sdata8;
  289. break;
  290. case Triple::ppc64:
  291. case Triple::ppc64le:
  292. case Triple::aarch64:
  293. case Triple::aarch64_be:
  294. case Triple::x86_64:
  295. FDECFIEncoding = dwarf::DW_EH_PE_pcrel |
  296. (Large ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4);
  297. break;
  298. case Triple::bpfel:
  299. case Triple::bpfeb:
  300. FDECFIEncoding = dwarf::DW_EH_PE_sdata8;
  301. break;
  302. case Triple::hexagon:
  303. FDECFIEncoding =
  304. PositionIndependent ? dwarf::DW_EH_PE_pcrel : dwarf::DW_EH_PE_absptr;
  305. break;
  306. default:
  307. FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
  308. break;
  309. }
  310. unsigned EHSectionType = T.getArch() == Triple::x86_64
  311. ? ELF::SHT_X86_64_UNWIND
  312. : ELF::SHT_PROGBITS;
  313. // Solaris requires different flags for .eh_frame to seemingly every other
  314. // platform.
  315. unsigned EHSectionFlags = ELF::SHF_ALLOC;
  316. if (T.isOSSolaris() && T.getArch() != Triple::x86_64)
  317. EHSectionFlags |= ELF::SHF_WRITE;
  318. // ELF
  319. BSSSection = Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
  320. ELF::SHF_WRITE | ELF::SHF_ALLOC);
  321. TextSection = Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
  322. ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);
  323. DataSection = Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
  324. ELF::SHF_WRITE | ELF::SHF_ALLOC);
  325. ReadOnlySection =
  326. Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
  327. TLSDataSection =
  328. Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
  329. ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
  330. TLSBSSSection = Ctx->getELFSection(
  331. ".tbss", ELF::SHT_NOBITS, ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
  332. DataRelROSection = Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
  333. ELF::SHF_ALLOC | ELF::SHF_WRITE);
  334. MergeableConst4Section =
  335. Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
  336. ELF::SHF_ALLOC | ELF::SHF_MERGE, 4);
  337. MergeableConst8Section =
  338. Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
  339. ELF::SHF_ALLOC | ELF::SHF_MERGE, 8);
  340. MergeableConst16Section =
  341. Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
  342. ELF::SHF_ALLOC | ELF::SHF_MERGE, 16);
  343. MergeableConst32Section =
  344. Ctx->getELFSection(".rodata.cst32", ELF::SHT_PROGBITS,
  345. ELF::SHF_ALLOC | ELF::SHF_MERGE, 32);
  346. // Exception Handling Sections.
  347. // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
  348. // it contains relocatable pointers. In PIC mode, this is probably a big
  349. // runtime hit for C++ apps. Either the contents of the LSDA need to be
  350. // adjusted or this should be a data section.
  351. LSDASection = Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
  352. ELF::SHF_ALLOC);
  353. COFFDebugSymbolsSection = nullptr;
  354. COFFDebugTypesSection = nullptr;
  355. unsigned DebugSecType = ELF::SHT_PROGBITS;
  356. // MIPS .debug_* sections should have SHT_MIPS_DWARF section type
  357. // to distinguish among sections contain DWARF and ECOFF debug formats.
  358. // Sections with ECOFF debug format are obsoleted and marked by SHT_PROGBITS.
  359. if (T.isMIPS())
  360. DebugSecType = ELF::SHT_MIPS_DWARF;
  361. // Debug Info Sections.
  362. DwarfAbbrevSection =
  363. Ctx->getELFSection(".debug_abbrev", DebugSecType, 0);
  364. DwarfInfoSection = Ctx->getELFSection(".debug_info", DebugSecType, 0);
  365. DwarfLineSection = Ctx->getELFSection(".debug_line", DebugSecType, 0);
  366. DwarfLineStrSection =
  367. Ctx->getELFSection(".debug_line_str", DebugSecType,
  368. ELF::SHF_MERGE | ELF::SHF_STRINGS, 1);
  369. DwarfFrameSection = Ctx->getELFSection(".debug_frame", DebugSecType, 0);
  370. DwarfPubNamesSection =
  371. Ctx->getELFSection(".debug_pubnames", DebugSecType, 0);
  372. DwarfPubTypesSection =
  373. Ctx->getELFSection(".debug_pubtypes", DebugSecType, 0);
  374. DwarfGnuPubNamesSection =
  375. Ctx->getELFSection(".debug_gnu_pubnames", DebugSecType, 0);
  376. DwarfGnuPubTypesSection =
  377. Ctx->getELFSection(".debug_gnu_pubtypes", DebugSecType, 0);
  378. DwarfStrSection =
  379. Ctx->getELFSection(".debug_str", DebugSecType,
  380. ELF::SHF_MERGE | ELF::SHF_STRINGS, 1);
  381. DwarfLocSection = Ctx->getELFSection(".debug_loc", DebugSecType, 0);
  382. DwarfARangesSection =
  383. Ctx->getELFSection(".debug_aranges", DebugSecType, 0);
  384. DwarfRangesSection =
  385. Ctx->getELFSection(".debug_ranges", DebugSecType, 0);
  386. DwarfMacinfoSection =
  387. Ctx->getELFSection(".debug_macinfo", DebugSecType, 0);
  388. DwarfMacroSection = Ctx->getELFSection(".debug_macro", DebugSecType, 0);
  389. // DWARF5 Experimental Debug Info
  390. // Accelerator Tables
  391. DwarfDebugNamesSection =
  392. Ctx->getELFSection(".debug_names", ELF::SHT_PROGBITS, 0);
  393. DwarfAccelNamesSection =
  394. Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0);
  395. DwarfAccelObjCSection =
  396. Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0);
  397. DwarfAccelNamespaceSection =
  398. Ctx->getELFSection(".apple_namespaces", ELF::SHT_PROGBITS, 0);
  399. DwarfAccelTypesSection =
  400. Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0);
  401. // String Offset and Address Sections
  402. DwarfStrOffSection =
  403. Ctx->getELFSection(".debug_str_offsets", DebugSecType, 0);
  404. DwarfAddrSection = Ctx->getELFSection(".debug_addr", DebugSecType, 0);
  405. DwarfRnglistsSection = Ctx->getELFSection(".debug_rnglists", DebugSecType, 0);
  406. DwarfLoclistsSection = Ctx->getELFSection(".debug_loclists", DebugSecType, 0);
  407. // Fission Sections
  408. DwarfInfoDWOSection =
  409. Ctx->getELFSection(".debug_info.dwo", DebugSecType, ELF::SHF_EXCLUDE);
  410. DwarfTypesDWOSection =
  411. Ctx->getELFSection(".debug_types.dwo", DebugSecType, ELF::SHF_EXCLUDE);
  412. DwarfAbbrevDWOSection =
  413. Ctx->getELFSection(".debug_abbrev.dwo", DebugSecType, ELF::SHF_EXCLUDE);
  414. DwarfStrDWOSection = Ctx->getELFSection(
  415. ".debug_str.dwo", DebugSecType,
  416. ELF::SHF_MERGE | ELF::SHF_STRINGS | ELF::SHF_EXCLUDE, 1);
  417. DwarfLineDWOSection =
  418. Ctx->getELFSection(".debug_line.dwo", DebugSecType, ELF::SHF_EXCLUDE);
  419. DwarfLocDWOSection =
  420. Ctx->getELFSection(".debug_loc.dwo", DebugSecType, ELF::SHF_EXCLUDE);
  421. DwarfStrOffDWOSection = Ctx->getELFSection(".debug_str_offsets.dwo",
  422. DebugSecType, ELF::SHF_EXCLUDE);
  423. DwarfRnglistsDWOSection =
  424. Ctx->getELFSection(".debug_rnglists.dwo", DebugSecType, ELF::SHF_EXCLUDE);
  425. DwarfMacinfoDWOSection =
  426. Ctx->getELFSection(".debug_macinfo.dwo", DebugSecType, ELF::SHF_EXCLUDE);
  427. DwarfMacroDWOSection =
  428. Ctx->getELFSection(".debug_macro.dwo", DebugSecType, ELF::SHF_EXCLUDE);
  429. DwarfLoclistsDWOSection =
  430. Ctx->getELFSection(".debug_loclists.dwo", DebugSecType, ELF::SHF_EXCLUDE);
  431. // DWP Sections
  432. DwarfCUIndexSection =
  433. Ctx->getELFSection(".debug_cu_index", DebugSecType, 0);
  434. DwarfTUIndexSection =
  435. Ctx->getELFSection(".debug_tu_index", DebugSecType, 0);
  436. StackMapSection =
  437. Ctx->getELFSection(".llvm_stackmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
  438. FaultMapSection =
  439. Ctx->getELFSection(".llvm_faultmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
  440. EHFrameSection =
  441. Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags);
  442. StackSizesSection = Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, 0);
  443. PseudoProbeSection = Ctx->getELFSection(".pseudo_probe", DebugSecType, 0);
  444. PseudoProbeDescSection =
  445. Ctx->getELFSection(".pseudo_probe_desc", DebugSecType, 0);
  446. }
  447. void MCObjectFileInfo::initGOFFMCObjectFileInfo(const Triple &T) {
  448. TextSection = Ctx->getGOFFSection(".text", SectionKind::getText());
  449. BSSSection = Ctx->getGOFFSection(".bss", SectionKind::getBSS());
  450. }
  451. void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
  452. EHFrameSection =
  453. Ctx->getCOFFSection(".eh_frame", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  454. COFF::IMAGE_SCN_MEM_READ,
  455. SectionKind::getData());
  456. // Set the `IMAGE_SCN_MEM_16BIT` flag when compiling for thumb mode. This is
  457. // used to indicate to the linker that the text segment contains thumb instructions
  458. // and to set the ISA selection bit for calls accordingly.
  459. const bool IsThumb = T.getArch() == Triple::thumb;
  460. CommDirectiveSupportsAlignment = true;
  461. // COFF
  462. BSSSection = Ctx->getCOFFSection(
  463. ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
  464. COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
  465. SectionKind::getBSS());
  466. TextSection = Ctx->getCOFFSection(
  467. ".text",
  468. (IsThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0) |
  469. COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
  470. COFF::IMAGE_SCN_MEM_READ,
  471. SectionKind::getText());
  472. DataSection = Ctx->getCOFFSection(
  473. ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
  474. COFF::IMAGE_SCN_MEM_WRITE,
  475. SectionKind::getData());
  476. ReadOnlySection = Ctx->getCOFFSection(
  477. ".rdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
  478. SectionKind::getReadOnly());
  479. if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::aarch64) {
  480. // On Windows 64 with SEH, the LSDA is emitted into the .xdata section
  481. LSDASection = nullptr;
  482. } else {
  483. LSDASection = Ctx->getCOFFSection(".gcc_except_table",
  484. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  485. COFF::IMAGE_SCN_MEM_READ,
  486. SectionKind::getReadOnly());
  487. }
  488. // Debug info.
  489. COFFDebugSymbolsSection =
  490. Ctx->getCOFFSection(".debug$S", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
  491. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  492. COFF::IMAGE_SCN_MEM_READ),
  493. SectionKind::getMetadata());
  494. COFFDebugTypesSection =
  495. Ctx->getCOFFSection(".debug$T", (COFF::IMAGE_SCN_MEM_DISCARDABLE |
  496. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  497. COFF::IMAGE_SCN_MEM_READ),
  498. SectionKind::getMetadata());
  499. COFFGlobalTypeHashesSection = Ctx->getCOFFSection(
  500. ".debug$H",
  501. (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  502. COFF::IMAGE_SCN_MEM_READ),
  503. SectionKind::getMetadata());
  504. DwarfAbbrevSection = Ctx->getCOFFSection(
  505. ".debug_abbrev",
  506. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  507. COFF::IMAGE_SCN_MEM_READ,
  508. SectionKind::getMetadata(), "section_abbrev");
  509. DwarfInfoSection = Ctx->getCOFFSection(
  510. ".debug_info",
  511. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  512. COFF::IMAGE_SCN_MEM_READ,
  513. SectionKind::getMetadata(), "section_info");
  514. DwarfLineSection = Ctx->getCOFFSection(
  515. ".debug_line",
  516. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  517. COFF::IMAGE_SCN_MEM_READ,
  518. SectionKind::getMetadata(), "section_line");
  519. DwarfLineStrSection = Ctx->getCOFFSection(
  520. ".debug_line_str",
  521. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  522. COFF::IMAGE_SCN_MEM_READ,
  523. SectionKind::getMetadata(), "section_line_str");
  524. DwarfFrameSection = Ctx->getCOFFSection(
  525. ".debug_frame",
  526. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  527. COFF::IMAGE_SCN_MEM_READ,
  528. SectionKind::getMetadata());
  529. DwarfPubNamesSection = Ctx->getCOFFSection(
  530. ".debug_pubnames",
  531. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  532. COFF::IMAGE_SCN_MEM_READ,
  533. SectionKind::getMetadata());
  534. DwarfPubTypesSection = Ctx->getCOFFSection(
  535. ".debug_pubtypes",
  536. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  537. COFF::IMAGE_SCN_MEM_READ,
  538. SectionKind::getMetadata());
  539. DwarfGnuPubNamesSection = Ctx->getCOFFSection(
  540. ".debug_gnu_pubnames",
  541. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  542. COFF::IMAGE_SCN_MEM_READ,
  543. SectionKind::getMetadata());
  544. DwarfGnuPubTypesSection = Ctx->getCOFFSection(
  545. ".debug_gnu_pubtypes",
  546. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  547. COFF::IMAGE_SCN_MEM_READ,
  548. SectionKind::getMetadata());
  549. DwarfStrSection = Ctx->getCOFFSection(
  550. ".debug_str",
  551. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  552. COFF::IMAGE_SCN_MEM_READ,
  553. SectionKind::getMetadata(), "info_string");
  554. DwarfStrOffSection = Ctx->getCOFFSection(
  555. ".debug_str_offsets",
  556. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  557. COFF::IMAGE_SCN_MEM_READ,
  558. SectionKind::getMetadata(), "section_str_off");
  559. DwarfLocSection = Ctx->getCOFFSection(
  560. ".debug_loc",
  561. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  562. COFF::IMAGE_SCN_MEM_READ,
  563. SectionKind::getMetadata(), "section_debug_loc");
  564. DwarfLoclistsSection = Ctx->getCOFFSection(
  565. ".debug_loclists",
  566. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  567. COFF::IMAGE_SCN_MEM_READ,
  568. SectionKind::getMetadata(), "section_debug_loclists");
  569. DwarfARangesSection = Ctx->getCOFFSection(
  570. ".debug_aranges",
  571. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  572. COFF::IMAGE_SCN_MEM_READ,
  573. SectionKind::getMetadata());
  574. DwarfRangesSection = Ctx->getCOFFSection(
  575. ".debug_ranges",
  576. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  577. COFF::IMAGE_SCN_MEM_READ,
  578. SectionKind::getMetadata(), "debug_range");
  579. DwarfRnglistsSection = Ctx->getCOFFSection(
  580. ".debug_rnglists",
  581. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  582. COFF::IMAGE_SCN_MEM_READ,
  583. SectionKind::getMetadata(), "debug_rnglists");
  584. DwarfMacinfoSection = Ctx->getCOFFSection(
  585. ".debug_macinfo",
  586. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  587. COFF::IMAGE_SCN_MEM_READ,
  588. SectionKind::getMetadata(), "debug_macinfo");
  589. DwarfMacroSection = Ctx->getCOFFSection(
  590. ".debug_macro",
  591. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  592. COFF::IMAGE_SCN_MEM_READ,
  593. SectionKind::getMetadata(), "debug_macro");
  594. DwarfMacinfoDWOSection = Ctx->getCOFFSection(
  595. ".debug_macinfo.dwo",
  596. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  597. COFF::IMAGE_SCN_MEM_READ,
  598. SectionKind::getMetadata(), "debug_macinfo.dwo");
  599. DwarfMacroDWOSection = Ctx->getCOFFSection(
  600. ".debug_macro.dwo",
  601. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  602. COFF::IMAGE_SCN_MEM_READ,
  603. SectionKind::getMetadata(), "debug_macro.dwo");
  604. DwarfInfoDWOSection = Ctx->getCOFFSection(
  605. ".debug_info.dwo",
  606. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  607. COFF::IMAGE_SCN_MEM_READ,
  608. SectionKind::getMetadata(), "section_info_dwo");
  609. DwarfTypesDWOSection = Ctx->getCOFFSection(
  610. ".debug_types.dwo",
  611. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  612. COFF::IMAGE_SCN_MEM_READ,
  613. SectionKind::getMetadata(), "section_types_dwo");
  614. DwarfAbbrevDWOSection = Ctx->getCOFFSection(
  615. ".debug_abbrev.dwo",
  616. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  617. COFF::IMAGE_SCN_MEM_READ,
  618. SectionKind::getMetadata(), "section_abbrev_dwo");
  619. DwarfStrDWOSection = Ctx->getCOFFSection(
  620. ".debug_str.dwo",
  621. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  622. COFF::IMAGE_SCN_MEM_READ,
  623. SectionKind::getMetadata(), "skel_string");
  624. DwarfLineDWOSection = Ctx->getCOFFSection(
  625. ".debug_line.dwo",
  626. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  627. COFF::IMAGE_SCN_MEM_READ,
  628. SectionKind::getMetadata());
  629. DwarfLocDWOSection = Ctx->getCOFFSection(
  630. ".debug_loc.dwo",
  631. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  632. COFF::IMAGE_SCN_MEM_READ,
  633. SectionKind::getMetadata(), "skel_loc");
  634. DwarfStrOffDWOSection = Ctx->getCOFFSection(
  635. ".debug_str_offsets.dwo",
  636. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  637. COFF::IMAGE_SCN_MEM_READ,
  638. SectionKind::getMetadata(), "section_str_off_dwo");
  639. DwarfAddrSection = Ctx->getCOFFSection(
  640. ".debug_addr",
  641. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  642. COFF::IMAGE_SCN_MEM_READ,
  643. SectionKind::getMetadata(), "addr_sec");
  644. DwarfCUIndexSection = Ctx->getCOFFSection(
  645. ".debug_cu_index",
  646. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  647. COFF::IMAGE_SCN_MEM_READ,
  648. SectionKind::getMetadata());
  649. DwarfTUIndexSection = Ctx->getCOFFSection(
  650. ".debug_tu_index",
  651. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  652. COFF::IMAGE_SCN_MEM_READ,
  653. SectionKind::getMetadata());
  654. DwarfDebugNamesSection = Ctx->getCOFFSection(
  655. ".debug_names",
  656. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  657. COFF::IMAGE_SCN_MEM_READ,
  658. SectionKind::getMetadata(), "debug_names_begin");
  659. DwarfAccelNamesSection = Ctx->getCOFFSection(
  660. ".apple_names",
  661. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  662. COFF::IMAGE_SCN_MEM_READ,
  663. SectionKind::getMetadata(), "names_begin");
  664. DwarfAccelNamespaceSection = Ctx->getCOFFSection(
  665. ".apple_namespaces",
  666. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  667. COFF::IMAGE_SCN_MEM_READ,
  668. SectionKind::getMetadata(), "namespac_begin");
  669. DwarfAccelTypesSection = Ctx->getCOFFSection(
  670. ".apple_types",
  671. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  672. COFF::IMAGE_SCN_MEM_READ,
  673. SectionKind::getMetadata(), "types_begin");
  674. DwarfAccelObjCSection = Ctx->getCOFFSection(
  675. ".apple_objc",
  676. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  677. COFF::IMAGE_SCN_MEM_READ,
  678. SectionKind::getMetadata(), "objc_begin");
  679. DrectveSection = Ctx->getCOFFSection(
  680. ".drectve", COFF::IMAGE_SCN_LNK_INFO | COFF::IMAGE_SCN_LNK_REMOVE,
  681. SectionKind::getMetadata());
  682. PDataSection = Ctx->getCOFFSection(
  683. ".pdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
  684. SectionKind::getData());
  685. XDataSection = Ctx->getCOFFSection(
  686. ".xdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
  687. SectionKind::getData());
  688. SXDataSection = Ctx->getCOFFSection(".sxdata", COFF::IMAGE_SCN_LNK_INFO,
  689. SectionKind::getMetadata());
  690. GEHContSection = Ctx->getCOFFSection(".gehcont$y",
  691. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  692. COFF::IMAGE_SCN_MEM_READ,
  693. SectionKind::getMetadata());
  694. GFIDsSection = Ctx->getCOFFSection(".gfids$y",
  695. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  696. COFF::IMAGE_SCN_MEM_READ,
  697. SectionKind::getMetadata());
  698. GIATsSection = Ctx->getCOFFSection(".giats$y",
  699. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  700. COFF::IMAGE_SCN_MEM_READ,
  701. SectionKind::getMetadata());
  702. GLJMPSection = Ctx->getCOFFSection(".gljmp$y",
  703. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  704. COFF::IMAGE_SCN_MEM_READ,
  705. SectionKind::getMetadata());
  706. TLSDataSection = Ctx->getCOFFSection(
  707. ".tls$", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
  708. COFF::IMAGE_SCN_MEM_WRITE,
  709. SectionKind::getData());
  710. StackMapSection = Ctx->getCOFFSection(".llvm_stackmaps",
  711. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  712. COFF::IMAGE_SCN_MEM_READ,
  713. SectionKind::getReadOnly());
  714. }
  715. void MCObjectFileInfo::initWasmMCObjectFileInfo(const Triple &T) {
  716. TextSection = Ctx->getWasmSection(".text", SectionKind::getText());
  717. DataSection = Ctx->getWasmSection(".data", SectionKind::getData());
  718. DwarfLineSection =
  719. Ctx->getWasmSection(".debug_line", SectionKind::getMetadata());
  720. DwarfLineStrSection =
  721. Ctx->getWasmSection(".debug_line_str", SectionKind::getMetadata(),
  722. wasm::WASM_SEG_FLAG_STRINGS);
  723. DwarfStrSection = Ctx->getWasmSection(
  724. ".debug_str", SectionKind::getMetadata(), wasm::WASM_SEG_FLAG_STRINGS);
  725. DwarfLocSection =
  726. Ctx->getWasmSection(".debug_loc", SectionKind::getMetadata());
  727. DwarfAbbrevSection =
  728. Ctx->getWasmSection(".debug_abbrev", SectionKind::getMetadata());
  729. DwarfARangesSection = Ctx->getWasmSection(".debug_aranges", SectionKind::getMetadata());
  730. DwarfRangesSection =
  731. Ctx->getWasmSection(".debug_ranges", SectionKind::getMetadata());
  732. DwarfMacinfoSection =
  733. Ctx->getWasmSection(".debug_macinfo", SectionKind::getMetadata());
  734. DwarfMacroSection =
  735. Ctx->getWasmSection(".debug_macro", SectionKind::getMetadata());
  736. DwarfCUIndexSection = Ctx->getWasmSection(".debug_cu_index", SectionKind::getMetadata());
  737. DwarfTUIndexSection = Ctx->getWasmSection(".debug_tu_index", SectionKind::getMetadata());
  738. DwarfInfoSection =
  739. Ctx->getWasmSection(".debug_info", SectionKind::getMetadata());
  740. DwarfFrameSection = Ctx->getWasmSection(".debug_frame", SectionKind::getMetadata());
  741. DwarfPubNamesSection = Ctx->getWasmSection(".debug_pubnames", SectionKind::getMetadata());
  742. DwarfPubTypesSection = Ctx->getWasmSection(".debug_pubtypes", SectionKind::getMetadata());
  743. DwarfGnuPubNamesSection =
  744. Ctx->getWasmSection(".debug_gnu_pubnames", SectionKind::getMetadata());
  745. DwarfGnuPubTypesSection =
  746. Ctx->getWasmSection(".debug_gnu_pubtypes", SectionKind::getMetadata());
  747. DwarfDebugNamesSection =
  748. Ctx->getWasmSection(".debug_names", SectionKind::getMetadata());
  749. DwarfStrOffSection =
  750. Ctx->getWasmSection(".debug_str_offsets", SectionKind::getMetadata());
  751. DwarfAddrSection =
  752. Ctx->getWasmSection(".debug_addr", SectionKind::getMetadata());
  753. DwarfRnglistsSection =
  754. Ctx->getWasmSection(".debug_rnglists", SectionKind::getMetadata());
  755. DwarfLoclistsSection =
  756. Ctx->getWasmSection(".debug_loclists", SectionKind::getMetadata());
  757. // Fission Sections
  758. DwarfInfoDWOSection =
  759. Ctx->getWasmSection(".debug_info.dwo", SectionKind::getMetadata());
  760. DwarfTypesDWOSection =
  761. Ctx->getWasmSection(".debug_types.dwo", SectionKind::getMetadata());
  762. DwarfAbbrevDWOSection =
  763. Ctx->getWasmSection(".debug_abbrev.dwo", SectionKind::getMetadata());
  764. DwarfStrDWOSection =
  765. Ctx->getWasmSection(".debug_str.dwo", SectionKind::getMetadata(),
  766. wasm::WASM_SEG_FLAG_STRINGS);
  767. DwarfLineDWOSection =
  768. Ctx->getWasmSection(".debug_line.dwo", SectionKind::getMetadata());
  769. DwarfLocDWOSection =
  770. Ctx->getWasmSection(".debug_loc.dwo", SectionKind::getMetadata());
  771. DwarfStrOffDWOSection =
  772. Ctx->getWasmSection(".debug_str_offsets.dwo", SectionKind::getMetadata());
  773. DwarfRnglistsDWOSection =
  774. Ctx->getWasmSection(".debug_rnglists.dwo", SectionKind::getMetadata());
  775. DwarfMacinfoDWOSection =
  776. Ctx->getWasmSection(".debug_macinfo.dwo", SectionKind::getMetadata());
  777. DwarfMacroDWOSection =
  778. Ctx->getWasmSection(".debug_macro.dwo", SectionKind::getMetadata());
  779. DwarfLoclistsDWOSection =
  780. Ctx->getWasmSection(".debug_loclists.dwo", SectionKind::getMetadata());
  781. // DWP Sections
  782. DwarfCUIndexSection =
  783. Ctx->getWasmSection(".debug_cu_index", SectionKind::getMetadata());
  784. DwarfTUIndexSection =
  785. Ctx->getWasmSection(".debug_tu_index", SectionKind::getMetadata());
  786. // Wasm use data section for LSDA.
  787. // TODO Consider putting each function's exception table in a separate
  788. // section, as in -function-sections, to facilitate lld's --gc-section.
  789. LSDASection = Ctx->getWasmSection(".rodata.gcc_except_table",
  790. SectionKind::getReadOnlyWithRel());
  791. // TODO: Define more sections.
  792. }
  793. void MCObjectFileInfo::initXCOFFMCObjectFileInfo(const Triple &T) {
  794. // The default csect for program code. Functions without a specified section
  795. // get placed into this csect. The choice of csect name is not a property of
  796. // the ABI or object file format. For example, the XL compiler uses an unnamed
  797. // csect for program code.
  798. TextSection = Ctx->getXCOFFSection(
  799. ".text", SectionKind::getText(),
  800. XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_PR, XCOFF::XTY_SD),
  801. /* MultiSymbolsAllowed*/ true);
  802. DataSection = Ctx->getXCOFFSection(
  803. ".data", SectionKind::getData(),
  804. XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RW, XCOFF::XTY_SD),
  805. /* MultiSymbolsAllowed*/ true);
  806. ReadOnlySection = Ctx->getXCOFFSection(
  807. ".rodata", SectionKind::getReadOnly(),
  808. XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD),
  809. /* MultiSymbolsAllowed*/ true);
  810. ReadOnlySection->setAlignment(Align(4));
  811. ReadOnly8Section = Ctx->getXCOFFSection(
  812. ".rodata.8", SectionKind::getReadOnly(),
  813. XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD),
  814. /* MultiSymbolsAllowed*/ true);
  815. ReadOnly8Section->setAlignment(Align(8));
  816. ReadOnly16Section = Ctx->getXCOFFSection(
  817. ".rodata.16", SectionKind::getReadOnly(),
  818. XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO, XCOFF::XTY_SD),
  819. /* MultiSymbolsAllowed*/ true);
  820. ReadOnly16Section->setAlignment(Align(16));
  821. TLSDataSection = Ctx->getXCOFFSection(
  822. ".tdata", SectionKind::getThreadData(),
  823. XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_TL, XCOFF::XTY_SD),
  824. /* MultiSymbolsAllowed*/ true);
  825. TOCBaseSection = Ctx->getXCOFFSection(
  826. "TOC", SectionKind::getData(),
  827. XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_TC0,
  828. XCOFF::XTY_SD));
  829. // The TOC-base always has 0 size, but 4 byte alignment.
  830. TOCBaseSection->setAlignment(Align(4));
  831. LSDASection = Ctx->getXCOFFSection(
  832. ".gcc_except_table", SectionKind::getReadOnly(),
  833. XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RO,
  834. XCOFF::XTY_SD));
  835. CompactUnwindSection = Ctx->getXCOFFSection(
  836. ".eh_info_table", SectionKind::getData(),
  837. XCOFF::CsectProperties(XCOFF::StorageMappingClass::XMC_RW,
  838. XCOFF::XTY_SD));
  839. // DWARF sections for XCOFF are not csects. They are special STYP_DWARF
  840. // sections, and the individual DWARF sections are distinguished by their
  841. // section subtype.
  842. DwarfAbbrevSection = Ctx->getXCOFFSection(
  843. ".dwabrev", SectionKind::getMetadata(), /* CsectProperties */ None,
  844. /* MultiSymbolsAllowed */ true, ".dwabrev", XCOFF::SSUBTYP_DWABREV);
  845. DwarfInfoSection = Ctx->getXCOFFSection(
  846. ".dwinfo", SectionKind::getMetadata(), /* CsectProperties */ None,
  847. /* MultiSymbolsAllowed */ true, ".dwinfo", XCOFF::SSUBTYP_DWINFO);
  848. DwarfLineSection = Ctx->getXCOFFSection(
  849. ".dwline", SectionKind::getMetadata(), /* CsectProperties */ None,
  850. /* MultiSymbolsAllowed */ true, ".dwline", XCOFF::SSUBTYP_DWLINE);
  851. DwarfFrameSection = Ctx->getXCOFFSection(
  852. ".dwframe", SectionKind::getMetadata(), /* CsectProperties */ None,
  853. /* MultiSymbolsAllowed */ true, ".dwframe", XCOFF::SSUBTYP_DWFRAME);
  854. DwarfPubNamesSection = Ctx->getXCOFFSection(
  855. ".dwpbnms", SectionKind::getMetadata(), /* CsectProperties */ None,
  856. /* MultiSymbolsAllowed */ true, ".dwpbnms", XCOFF::SSUBTYP_DWPBNMS);
  857. DwarfPubTypesSection = Ctx->getXCOFFSection(
  858. ".dwpbtyp", SectionKind::getMetadata(), /* CsectProperties */ None,
  859. /* MultiSymbolsAllowed */ true, ".dwpbtyp", XCOFF::SSUBTYP_DWPBTYP);
  860. DwarfStrSection = Ctx->getXCOFFSection(
  861. ".dwstr", SectionKind::getMetadata(), /* CsectProperties */ None,
  862. /* MultiSymbolsAllowed */ true, ".dwstr", XCOFF::SSUBTYP_DWSTR);
  863. DwarfLocSection = Ctx->getXCOFFSection(
  864. ".dwloc", SectionKind::getMetadata(), /* CsectProperties */ None,
  865. /* MultiSymbolsAllowed */ true, ".dwloc", XCOFF::SSUBTYP_DWLOC);
  866. DwarfARangesSection = Ctx->getXCOFFSection(
  867. ".dwarnge", SectionKind::getMetadata(), /* CsectProperties */ None,
  868. /* MultiSymbolsAllowed */ true, ".dwarnge", XCOFF::SSUBTYP_DWARNGE);
  869. DwarfRangesSection = Ctx->getXCOFFSection(
  870. ".dwrnges", SectionKind::getMetadata(), /* CsectProperties */ None,
  871. /* MultiSymbolsAllowed */ true, ".dwrnges", XCOFF::SSUBTYP_DWRNGES);
  872. DwarfMacinfoSection = Ctx->getXCOFFSection(
  873. ".dwmac", SectionKind::getMetadata(), /* CsectProperties */ None,
  874. /* MultiSymbolsAllowed */ true, ".dwmac", XCOFF::SSUBTYP_DWMAC);
  875. }
  876. MCObjectFileInfo::~MCObjectFileInfo() {}
  877. void MCObjectFileInfo::initMCObjectFileInfo(MCContext &MCCtx, bool PIC,
  878. bool LargeCodeModel) {
  879. PositionIndependent = PIC;
  880. Ctx = &MCCtx;
  881. // Common.
  882. CommDirectiveSupportsAlignment = true;
  883. SupportsWeakOmittedEHFrame = true;
  884. SupportsCompactUnwindWithoutEHFrame = false;
  885. OmitDwarfIfHaveCompactUnwind = false;
  886. FDECFIEncoding = dwarf::DW_EH_PE_absptr;
  887. CompactUnwindDwarfEHFrameOnly = 0;
  888. EHFrameSection = nullptr; // Created on demand.
  889. CompactUnwindSection = nullptr; // Used only by selected targets.
  890. DwarfAccelNamesSection = nullptr; // Used only by selected targets.
  891. DwarfAccelObjCSection = nullptr; // Used only by selected targets.
  892. DwarfAccelNamespaceSection = nullptr; // Used only by selected targets.
  893. DwarfAccelTypesSection = nullptr; // Used only by selected targets.
  894. Triple TheTriple = Ctx->getTargetTriple();
  895. switch (Ctx->getObjectFileType()) {
  896. case MCContext::IsMachO:
  897. initMachOMCObjectFileInfo(TheTriple);
  898. break;
  899. case MCContext::IsCOFF:
  900. initCOFFMCObjectFileInfo(TheTriple);
  901. break;
  902. case MCContext::IsELF:
  903. initELFMCObjectFileInfo(TheTriple, LargeCodeModel);
  904. break;
  905. case MCContext::IsGOFF:
  906. initGOFFMCObjectFileInfo(TheTriple);
  907. break;
  908. case MCContext::IsWasm:
  909. initWasmMCObjectFileInfo(TheTriple);
  910. break;
  911. case MCContext::IsXCOFF:
  912. initXCOFFMCObjectFileInfo(TheTriple);
  913. break;
  914. }
  915. }
  916. MCSection *MCObjectFileInfo::getDwarfComdatSection(const char *Name,
  917. uint64_t Hash) const {
  918. switch (Ctx->getTargetTriple().getObjectFormat()) {
  919. case Triple::ELF:
  920. return Ctx->getELFSection(Name, ELF::SHT_PROGBITS, ELF::SHF_GROUP, 0,
  921. utostr(Hash), /*IsComdat=*/true);
  922. case Triple::Wasm:
  923. return Ctx->getWasmSection(Name, SectionKind::getMetadata(), 0,
  924. utostr(Hash), MCContext::GenericSectionID);
  925. case Triple::MachO:
  926. case Triple::COFF:
  927. case Triple::GOFF:
  928. case Triple::XCOFF:
  929. case Triple::UnknownObjectFormat:
  930. report_fatal_error("Cannot get DWARF comdat section for this object file "
  931. "format: not implemented.");
  932. break;
  933. }
  934. llvm_unreachable("Unknown ObjectFormatType");
  935. }
  936. MCSection *
  937. MCObjectFileInfo::getStackSizesSection(const MCSection &TextSec) const {
  938. if (Ctx->getObjectFileType() != MCContext::IsELF)
  939. return StackSizesSection;
  940. const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
  941. unsigned Flags = ELF::SHF_LINK_ORDER;
  942. StringRef GroupName;
  943. if (const MCSymbol *Group = ElfSec.getGroup()) {
  944. GroupName = Group->getName();
  945. Flags |= ELF::SHF_GROUP;
  946. }
  947. return Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, Flags, 0,
  948. GroupName, true, ElfSec.getUniqueID(),
  949. cast<MCSymbolELF>(TextSec.getBeginSymbol()));
  950. }
  951. MCSection *
  952. MCObjectFileInfo::getBBAddrMapSection(const MCSection &TextSec) const {
  953. if (Ctx->getObjectFileType() != MCContext::IsELF)
  954. return nullptr;
  955. const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
  956. unsigned Flags = ELF::SHF_LINK_ORDER;
  957. StringRef GroupName;
  958. if (const MCSymbol *Group = ElfSec.getGroup()) {
  959. GroupName = Group->getName();
  960. Flags |= ELF::SHF_GROUP;
  961. }
  962. // Use the text section's begin symbol and unique ID to create a separate
  963. // .llvm_bb_addr_map section associated with every unique text section.
  964. return Ctx->getELFSection(".llvm_bb_addr_map", ELF::SHT_LLVM_BB_ADDR_MAP,
  965. Flags, 0, GroupName, true, ElfSec.getUniqueID(),
  966. cast<MCSymbolELF>(TextSec.getBeginSymbol()));
  967. }
  968. MCSection *
  969. MCObjectFileInfo::getPseudoProbeSection(const MCSection *TextSec) const {
  970. if (Ctx->getObjectFileType() == MCContext::IsELF) {
  971. const auto *ElfSec = static_cast<const MCSectionELF *>(TextSec);
  972. // Create a separate section for probes that comes with a comdat function.
  973. if (const MCSymbol *Group = ElfSec->getGroup()) {
  974. auto *S = static_cast<MCSectionELF *>(PseudoProbeSection);
  975. auto Flags = S->getFlags() | ELF::SHF_GROUP;
  976. return Ctx->getELFSection(S->getName(), S->getType(), Flags,
  977. S->getEntrySize(), Group->getName(),
  978. /*IsComdat=*/true);
  979. }
  980. }
  981. return PseudoProbeSection;
  982. }
  983. MCSection *
  984. MCObjectFileInfo::getPseudoProbeDescSection(StringRef FuncName) const {
  985. if (Ctx->getObjectFileType() == MCContext::IsELF) {
  986. // Create a separate comdat group for each function's descriptor in order
  987. // for the linker to deduplicate. The duplication, must be from different
  988. // tranlation unit, can come from:
  989. // 1. Inline functions defined in header files;
  990. // 2. ThinLTO imported funcions;
  991. // 3. Weak-linkage definitions.
  992. // Use a concatenation of the section name and the function name as the
  993. // group name so that descriptor-only groups won't be folded with groups of
  994. // code.
  995. if (Ctx->getTargetTriple().supportsCOMDAT() && !FuncName.empty()) {
  996. auto *S = static_cast<MCSectionELF *>(PseudoProbeDescSection);
  997. auto Flags = S->getFlags() | ELF::SHF_GROUP;
  998. return Ctx->getELFSection(S->getName(), S->getType(), Flags,
  999. S->getEntrySize(),
  1000. S->getName() + "_" + FuncName,
  1001. /*IsComdat=*/true);
  1002. }
  1003. }
  1004. return PseudoProbeDescSection;
  1005. }