MCObjectFileInfo.cpp 34 KB

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