MCObjectFileInfo.cpp 51 KB

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