2
0

MCObjectFileInfo.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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", MachO::S_ATTR_DEBUG,
  160. SectionKind::getMetadata(), "names_begin");
  161. DwarfAccelObjCSection =
  162. Ctx->getMachOSection("__DWARF", "__apple_objc", MachO::S_ATTR_DEBUG,
  163. SectionKind::getMetadata(), "objc_begin");
  164. // 16 character section limit...
  165. DwarfAccelNamespaceSection =
  166. Ctx->getMachOSection("__DWARF", "__apple_namespac", MachO::S_ATTR_DEBUG,
  167. SectionKind::getMetadata(), "namespac_begin");
  168. DwarfAccelTypesSection =
  169. Ctx->getMachOSection("__DWARF", "__apple_types", MachO::S_ATTR_DEBUG,
  170. SectionKind::getMetadata(), "types_begin");
  171. DwarfAbbrevSection =
  172. Ctx->getMachOSection("__DWARF", "__debug_abbrev", MachO::S_ATTR_DEBUG,
  173. SectionKind::getMetadata(), "section_abbrev");
  174. DwarfInfoSection =
  175. Ctx->getMachOSection("__DWARF", "__debug_info", MachO::S_ATTR_DEBUG,
  176. SectionKind::getMetadata(), "section_info");
  177. DwarfLineSection =
  178. Ctx->getMachOSection("__DWARF", "__debug_line", MachO::S_ATTR_DEBUG,
  179. SectionKind::getMetadata(), "section_line");
  180. DwarfFrameSection =
  181. Ctx->getMachOSection("__DWARF", "__debug_frame", MachO::S_ATTR_DEBUG,
  182. SectionKind::getMetadata());
  183. DwarfPubNamesSection =
  184. Ctx->getMachOSection("__DWARF", "__debug_pubnames", MachO::S_ATTR_DEBUG,
  185. SectionKind::getMetadata());
  186. DwarfPubTypesSection =
  187. Ctx->getMachOSection("__DWARF", "__debug_pubtypes", MachO::S_ATTR_DEBUG,
  188. SectionKind::getMetadata());
  189. DwarfGnuPubNamesSection =
  190. Ctx->getMachOSection("__DWARF", "__debug_gnu_pubn", MachO::S_ATTR_DEBUG,
  191. SectionKind::getMetadata());
  192. DwarfGnuPubTypesSection =
  193. Ctx->getMachOSection("__DWARF", "__debug_gnu_pubt", MachO::S_ATTR_DEBUG,
  194. SectionKind::getMetadata());
  195. DwarfStrSection =
  196. Ctx->getMachOSection("__DWARF", "__debug_str", MachO::S_ATTR_DEBUG,
  197. SectionKind::getMetadata(), "info_string");
  198. DwarfLocSection =
  199. Ctx->getMachOSection("__DWARF", "__debug_loc", MachO::S_ATTR_DEBUG,
  200. SectionKind::getMetadata(), "section_debug_loc");
  201. DwarfARangesSection =
  202. Ctx->getMachOSection("__DWARF", "__debug_aranges", MachO::S_ATTR_DEBUG,
  203. SectionKind::getMetadata());
  204. DwarfRangesSection =
  205. Ctx->getMachOSection("__DWARF", "__debug_ranges", MachO::S_ATTR_DEBUG,
  206. SectionKind::getMetadata(), "debug_range");
  207. DwarfDebugInlineSection =
  208. Ctx->getMachOSection("__DWARF", "__debug_inlined", MachO::S_ATTR_DEBUG,
  209. SectionKind::getMetadata());
  210. StackMapSection = Ctx->getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps",
  211. 0, SectionKind::getMetadata());
  212. FaultMapSection = Ctx->getMachOSection("__LLVM_FAULTMAPS", "__llvm_faultmaps",
  213. 0, SectionKind::getMetadata());
  214. TLSExtraDataSection = TLSTLVSection;
  215. }
  216. void MCObjectFileInfo::initELFMCObjectFileInfo(Triple T) {
  217. switch (T.getArch()) {
  218. case Triple::mips:
  219. case Triple::mipsel:
  220. FDECFIEncoding = dwarf::DW_EH_PE_sdata4;
  221. break;
  222. case Triple::mips64:
  223. case Triple::mips64el:
  224. FDECFIEncoding = dwarf::DW_EH_PE_sdata8;
  225. break;
  226. case Triple::x86_64:
  227. FDECFIEncoding = dwarf::DW_EH_PE_pcrel |
  228. ((CMModel == CodeModel::Large) ? dwarf::DW_EH_PE_sdata8
  229. : dwarf::DW_EH_PE_sdata4);
  230. break;
  231. default:
  232. FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
  233. break;
  234. }
  235. switch (T.getArch()) {
  236. case Triple::arm:
  237. case Triple::armeb:
  238. case Triple::thumb:
  239. case Triple::thumbeb:
  240. if (Ctx->getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM)
  241. break;
  242. // Fallthrough if not using EHABI
  243. case Triple::ppc:
  244. case Triple::x86:
  245. PersonalityEncoding = (RelocM == Reloc::PIC_)
  246. ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
  247. : dwarf::DW_EH_PE_absptr;
  248. LSDAEncoding = (RelocM == Reloc::PIC_)
  249. ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
  250. : dwarf::DW_EH_PE_absptr;
  251. TTypeEncoding = (RelocM == Reloc::PIC_)
  252. ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
  253. : dwarf::DW_EH_PE_absptr;
  254. break;
  255. case Triple::x86_64:
  256. if (RelocM == Reloc::PIC_) {
  257. PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  258. ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
  259. ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
  260. LSDAEncoding = dwarf::DW_EH_PE_pcrel |
  261. (CMModel == CodeModel::Small
  262. ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
  263. TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  264. ((CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
  265. ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
  266. } else {
  267. PersonalityEncoding =
  268. (CMModel == CodeModel::Small || CMModel == CodeModel::Medium)
  269. ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
  270. LSDAEncoding = (CMModel == CodeModel::Small)
  271. ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
  272. TTypeEncoding = (CMModel == CodeModel::Small)
  273. ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
  274. }
  275. break;
  276. case Triple::aarch64:
  277. case Triple::aarch64_be:
  278. // The small model guarantees static code/data size < 4GB, but not where it
  279. // will be in memory. Most of these could end up >2GB away so even a signed
  280. // pc-relative 32-bit address is insufficient, theoretically.
  281. if (RelocM == Reloc::PIC_) {
  282. PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  283. dwarf::DW_EH_PE_sdata8;
  284. LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8;
  285. TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  286. dwarf::DW_EH_PE_sdata8;
  287. } else {
  288. PersonalityEncoding = dwarf::DW_EH_PE_absptr;
  289. LSDAEncoding = dwarf::DW_EH_PE_absptr;
  290. TTypeEncoding = dwarf::DW_EH_PE_absptr;
  291. }
  292. break;
  293. case Triple::mips:
  294. case Triple::mipsel:
  295. case Triple::mips64:
  296. case Triple::mips64el:
  297. // MIPS uses indirect pointer to refer personality functions and types, so
  298. // that the eh_frame section can be read-only. DW.ref.personality will be
  299. // generated for relocation.
  300. PersonalityEncoding = dwarf::DW_EH_PE_indirect;
  301. // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't
  302. // identify N64 from just a triple.
  303. TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  304. dwarf::DW_EH_PE_sdata4;
  305. // We don't support PC-relative LSDA references in GAS so we use the default
  306. // DW_EH_PE_absptr for those.
  307. break;
  308. case Triple::ppc64:
  309. case Triple::ppc64le:
  310. PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  311. dwarf::DW_EH_PE_udata8;
  312. LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
  313. TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  314. dwarf::DW_EH_PE_udata8;
  315. break;
  316. case Triple::sparcel:
  317. case Triple::sparc:
  318. if (RelocM == Reloc::PIC_) {
  319. LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
  320. PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  321. dwarf::DW_EH_PE_sdata4;
  322. TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  323. dwarf::DW_EH_PE_sdata4;
  324. } else {
  325. LSDAEncoding = dwarf::DW_EH_PE_absptr;
  326. PersonalityEncoding = dwarf::DW_EH_PE_absptr;
  327. TTypeEncoding = dwarf::DW_EH_PE_absptr;
  328. }
  329. break;
  330. case Triple::sparcv9:
  331. LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
  332. if (RelocM == Reloc::PIC_) {
  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. PersonalityEncoding = dwarf::DW_EH_PE_absptr;
  339. TTypeEncoding = dwarf::DW_EH_PE_absptr;
  340. }
  341. break;
  342. case Triple::systemz:
  343. // All currently-defined code models guarantee that 4-byte PC-relative
  344. // values will be in range.
  345. if (RelocM == Reloc::PIC_) {
  346. PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  347. dwarf::DW_EH_PE_sdata4;
  348. LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
  349. TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  350. dwarf::DW_EH_PE_sdata4;
  351. } else {
  352. PersonalityEncoding = dwarf::DW_EH_PE_absptr;
  353. LSDAEncoding = dwarf::DW_EH_PE_absptr;
  354. TTypeEncoding = dwarf::DW_EH_PE_absptr;
  355. }
  356. break;
  357. default:
  358. break;
  359. }
  360. // Solaris requires different flags for .eh_frame to seemingly every other
  361. // platform.
  362. EHSectionType = ELF::SHT_PROGBITS;
  363. EHSectionFlags = ELF::SHF_ALLOC;
  364. if (T.isOSSolaris()) {
  365. if (T.getArch() == Triple::x86_64)
  366. EHSectionType = ELF::SHT_X86_64_UNWIND;
  367. else
  368. EHSectionFlags |= ELF::SHF_WRITE;
  369. }
  370. // ELF
  371. BSSSection = Ctx->getELFSection(".bss", ELF::SHT_NOBITS,
  372. ELF::SHF_WRITE | ELF::SHF_ALLOC);
  373. TextSection = Ctx->getELFSection(".text", ELF::SHT_PROGBITS,
  374. ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);
  375. DataSection = Ctx->getELFSection(".data", ELF::SHT_PROGBITS,
  376. ELF::SHF_WRITE | ELF::SHF_ALLOC);
  377. ReadOnlySection =
  378. Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
  379. TLSDataSection =
  380. Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
  381. ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
  382. TLSBSSSection = Ctx->getELFSection(
  383. ".tbss", ELF::SHT_NOBITS, ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
  384. DataRelSection = Ctx->getELFSection(".data.rel", ELF::SHT_PROGBITS,
  385. ELF::SHF_ALLOC | ELF::SHF_WRITE);
  386. DataRelLocalSection = Ctx->getELFSection(".data.rel.local", ELF::SHT_PROGBITS,
  387. ELF::SHF_ALLOC | ELF::SHF_WRITE);
  388. DataRelROSection = Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
  389. ELF::SHF_ALLOC | ELF::SHF_WRITE);
  390. DataRelROLocalSection = Ctx->getELFSection(
  391. ".data.rel.ro.local", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_WRITE);
  392. MergeableConst4Section =
  393. Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS,
  394. ELF::SHF_ALLOC | ELF::SHF_MERGE, 4, "");
  395. MergeableConst8Section =
  396. Ctx->getELFSection(".rodata.cst8", ELF::SHT_PROGBITS,
  397. ELF::SHF_ALLOC | ELF::SHF_MERGE, 8, "");
  398. MergeableConst16Section =
  399. Ctx->getELFSection(".rodata.cst16", ELF::SHT_PROGBITS,
  400. ELF::SHF_ALLOC | ELF::SHF_MERGE, 16, "");
  401. StaticCtorSection = Ctx->getELFSection(".ctors", ELF::SHT_PROGBITS,
  402. ELF::SHF_ALLOC | ELF::SHF_WRITE);
  403. StaticDtorSection = Ctx->getELFSection(".dtors", ELF::SHT_PROGBITS,
  404. ELF::SHF_ALLOC | ELF::SHF_WRITE);
  405. // Exception Handling Sections.
  406. // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
  407. // it contains relocatable pointers. In PIC mode, this is probably a big
  408. // runtime hit for C++ apps. Either the contents of the LSDA need to be
  409. // adjusted or this should be a data section.
  410. LSDASection = Ctx->getELFSection(".gcc_except_table", ELF::SHT_PROGBITS,
  411. ELF::SHF_ALLOC);
  412. COFFDebugSymbolsSection = nullptr;
  413. // Debug Info Sections.
  414. DwarfAbbrevSection = Ctx->getELFSection(".debug_abbrev", ELF::SHT_PROGBITS, 0,
  415. "section_abbrev");
  416. DwarfInfoSection =
  417. Ctx->getELFSection(".debug_info", ELF::SHT_PROGBITS, 0, "section_info");
  418. DwarfLineSection = Ctx->getELFSection(".debug_line", ELF::SHT_PROGBITS, 0);
  419. DwarfFrameSection = Ctx->getELFSection(".debug_frame", ELF::SHT_PROGBITS, 0);
  420. DwarfPubNamesSection =
  421. Ctx->getELFSection(".debug_pubnames", ELF::SHT_PROGBITS, 0);
  422. DwarfPubTypesSection =
  423. Ctx->getELFSection(".debug_pubtypes", ELF::SHT_PROGBITS, 0);
  424. DwarfGnuPubNamesSection =
  425. Ctx->getELFSection(".debug_gnu_pubnames", ELF::SHT_PROGBITS, 0);
  426. DwarfGnuPubTypesSection =
  427. Ctx->getELFSection(".debug_gnu_pubtypes", ELF::SHT_PROGBITS, 0);
  428. DwarfStrSection =
  429. Ctx->getELFSection(".debug_str", ELF::SHT_PROGBITS,
  430. ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
  431. DwarfLocSection = Ctx->getELFSection(".debug_loc", ELF::SHT_PROGBITS, 0);
  432. DwarfARangesSection =
  433. Ctx->getELFSection(".debug_aranges", ELF::SHT_PROGBITS, 0);
  434. DwarfRangesSection =
  435. Ctx->getELFSection(".debug_ranges", ELF::SHT_PROGBITS, 0, "debug_range");
  436. // DWARF5 Experimental Debug Info
  437. // Accelerator Tables
  438. DwarfAccelNamesSection =
  439. Ctx->getELFSection(".apple_names", ELF::SHT_PROGBITS, 0, "names_begin");
  440. DwarfAccelObjCSection =
  441. Ctx->getELFSection(".apple_objc", ELF::SHT_PROGBITS, 0, "objc_begin");
  442. DwarfAccelNamespaceSection = Ctx->getELFSection(
  443. ".apple_namespaces", ELF::SHT_PROGBITS, 0, "namespac_begin");
  444. DwarfAccelTypesSection =
  445. Ctx->getELFSection(".apple_types", ELF::SHT_PROGBITS, 0, "types_begin");
  446. // Fission Sections
  447. DwarfInfoDWOSection =
  448. Ctx->getELFSection(".debug_info.dwo", ELF::SHT_PROGBITS, 0);
  449. DwarfTypesDWOSection =
  450. Ctx->getELFSection(".debug_types.dwo", ELF::SHT_PROGBITS, 0);
  451. DwarfAbbrevDWOSection =
  452. Ctx->getELFSection(".debug_abbrev.dwo", ELF::SHT_PROGBITS, 0);
  453. DwarfStrDWOSection =
  454. Ctx->getELFSection(".debug_str.dwo", ELF::SHT_PROGBITS,
  455. ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
  456. DwarfLineDWOSection =
  457. Ctx->getELFSection(".debug_line.dwo", ELF::SHT_PROGBITS, 0);
  458. DwarfLocDWOSection =
  459. Ctx->getELFSection(".debug_loc.dwo", ELF::SHT_PROGBITS, 0, "skel_loc");
  460. DwarfStrOffDWOSection =
  461. Ctx->getELFSection(".debug_str_offsets.dwo", ELF::SHT_PROGBITS, 0);
  462. DwarfAddrSection =
  463. Ctx->getELFSection(".debug_addr", ELF::SHT_PROGBITS, 0, "addr_sec");
  464. StackMapSection =
  465. Ctx->getELFSection(".llvm_stackmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
  466. FaultMapSection =
  467. Ctx->getELFSection(".llvm_faultmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
  468. }
  469. void MCObjectFileInfo::initCOFFMCObjectFileInfo(Triple T) {
  470. bool IsWoA = T.getArch() == Triple::arm || T.getArch() == Triple::thumb;
  471. CommDirectiveSupportsAlignment = true;
  472. // COFF
  473. BSSSection = Ctx->getCOFFSection(
  474. ".bss", COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
  475. COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
  476. SectionKind::getBSS());
  477. TextSection = Ctx->getCOFFSection(
  478. ".text",
  479. (IsWoA ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0) |
  480. COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
  481. COFF::IMAGE_SCN_MEM_READ,
  482. SectionKind::getText());
  483. DataSection = Ctx->getCOFFSection(
  484. ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
  485. COFF::IMAGE_SCN_MEM_WRITE,
  486. SectionKind::getDataRel());
  487. ReadOnlySection = Ctx->getCOFFSection(
  488. ".rdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
  489. SectionKind::getReadOnly());
  490. if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
  491. StaticCtorSection =
  492. Ctx->getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  493. COFF::IMAGE_SCN_MEM_READ,
  494. SectionKind::getReadOnly());
  495. StaticDtorSection =
  496. Ctx->getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  497. COFF::IMAGE_SCN_MEM_READ,
  498. SectionKind::getReadOnly());
  499. } else {
  500. StaticCtorSection = Ctx->getCOFFSection(
  501. ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  502. COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
  503. SectionKind::getDataRel());
  504. StaticDtorSection = Ctx->getCOFFSection(
  505. ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  506. COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
  507. SectionKind::getDataRel());
  508. }
  509. // FIXME: We're emitting LSDA info into a readonly section on COFF, even
  510. // though it contains relocatable pointers. In PIC mode, this is probably a
  511. // big runtime hit for C++ apps. Either the contents of the LSDA need to be
  512. // adjusted or this should be a data section.
  513. assert(T.isOSWindows() && "Windows is the only supported COFF target");
  514. if (T.getArch() == Triple::x86_64) {
  515. // On Windows 64 with SEH, the LSDA is emitted into the .xdata section
  516. LSDASection = 0;
  517. } else {
  518. LSDASection = Ctx->getCOFFSection(".gcc_except_table",
  519. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  520. COFF::IMAGE_SCN_MEM_READ,
  521. SectionKind::getReadOnly());
  522. }
  523. // Debug info.
  524. COFFDebugSymbolsSection =
  525. Ctx->getCOFFSection(".debug$S", COFF::IMAGE_SCN_MEM_DISCARDABLE |
  526. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  527. COFF::IMAGE_SCN_MEM_READ,
  528. SectionKind::getMetadata());
  529. DwarfAbbrevSection = Ctx->getCOFFSection(
  530. ".debug_abbrev",
  531. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  532. COFF::IMAGE_SCN_MEM_READ,
  533. SectionKind::getMetadata(), "section_abbrev");
  534. DwarfInfoSection = Ctx->getCOFFSection(
  535. ".debug_info",
  536. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  537. COFF::IMAGE_SCN_MEM_READ,
  538. SectionKind::getMetadata(), "section_info");
  539. DwarfLineSection = Ctx->getCOFFSection(
  540. ".debug_line",
  541. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  542. COFF::IMAGE_SCN_MEM_READ,
  543. SectionKind::getMetadata(), "section_line");
  544. DwarfFrameSection = Ctx->getCOFFSection(
  545. ".debug_frame",
  546. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  547. COFF::IMAGE_SCN_MEM_READ,
  548. SectionKind::getMetadata());
  549. DwarfPubNamesSection = Ctx->getCOFFSection(
  550. ".debug_pubnames",
  551. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  552. COFF::IMAGE_SCN_MEM_READ,
  553. SectionKind::getMetadata());
  554. DwarfPubTypesSection = Ctx->getCOFFSection(
  555. ".debug_pubtypes",
  556. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  557. COFF::IMAGE_SCN_MEM_READ,
  558. SectionKind::getMetadata());
  559. DwarfGnuPubNamesSection = Ctx->getCOFFSection(
  560. ".debug_gnu_pubnames",
  561. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  562. COFF::IMAGE_SCN_MEM_READ,
  563. SectionKind::getMetadata());
  564. DwarfGnuPubTypesSection = Ctx->getCOFFSection(
  565. ".debug_gnu_pubtypes",
  566. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  567. COFF::IMAGE_SCN_MEM_READ,
  568. SectionKind::getMetadata());
  569. DwarfStrSection = Ctx->getCOFFSection(
  570. ".debug_str",
  571. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  572. COFF::IMAGE_SCN_MEM_READ,
  573. SectionKind::getMetadata(), "info_string");
  574. DwarfLocSection = Ctx->getCOFFSection(
  575. ".debug_loc",
  576. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  577. COFF::IMAGE_SCN_MEM_READ,
  578. SectionKind::getMetadata(), "section_debug_loc");
  579. DwarfARangesSection = Ctx->getCOFFSection(
  580. ".debug_aranges",
  581. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  582. COFF::IMAGE_SCN_MEM_READ,
  583. SectionKind::getMetadata());
  584. DwarfRangesSection = Ctx->getCOFFSection(
  585. ".debug_ranges",
  586. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  587. COFF::IMAGE_SCN_MEM_READ,
  588. SectionKind::getMetadata(), "debug_range");
  589. DwarfInfoDWOSection = Ctx->getCOFFSection(
  590. ".debug_info.dwo",
  591. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  592. COFF::IMAGE_SCN_MEM_READ,
  593. SectionKind::getMetadata(), "section_info_dwo");
  594. DwarfTypesDWOSection = Ctx->getCOFFSection(
  595. ".debug_types.dwo",
  596. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  597. COFF::IMAGE_SCN_MEM_READ,
  598. SectionKind::getMetadata(), "section_types_dwo");
  599. DwarfAbbrevDWOSection = Ctx->getCOFFSection(
  600. ".debug_abbrev.dwo",
  601. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  602. COFF::IMAGE_SCN_MEM_READ,
  603. SectionKind::getMetadata(), "section_abbrev_dwo");
  604. DwarfStrDWOSection = Ctx->getCOFFSection(
  605. ".debug_str.dwo",
  606. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  607. COFF::IMAGE_SCN_MEM_READ,
  608. SectionKind::getMetadata(), "skel_string");
  609. DwarfLineDWOSection = Ctx->getCOFFSection(
  610. ".debug_line.dwo",
  611. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  612. COFF::IMAGE_SCN_MEM_READ,
  613. SectionKind::getMetadata());
  614. DwarfLocDWOSection = Ctx->getCOFFSection(
  615. ".debug_loc.dwo",
  616. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  617. COFF::IMAGE_SCN_MEM_READ,
  618. SectionKind::getMetadata(), "skel_loc");
  619. DwarfStrOffDWOSection = Ctx->getCOFFSection(
  620. ".debug_str_offsets.dwo",
  621. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  622. COFF::IMAGE_SCN_MEM_READ,
  623. SectionKind::getMetadata());
  624. DwarfAddrSection = Ctx->getCOFFSection(
  625. ".debug_addr",
  626. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  627. COFF::IMAGE_SCN_MEM_READ,
  628. SectionKind::getMetadata(), "addr_sec");
  629. DwarfAccelNamesSection = Ctx->getCOFFSection(
  630. ".apple_names",
  631. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  632. COFF::IMAGE_SCN_MEM_READ,
  633. SectionKind::getMetadata(), "names_begin");
  634. DwarfAccelNamespaceSection = Ctx->getCOFFSection(
  635. ".apple_namespaces",
  636. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  637. COFF::IMAGE_SCN_MEM_READ,
  638. SectionKind::getMetadata(), "namespac_begin");
  639. DwarfAccelTypesSection = Ctx->getCOFFSection(
  640. ".apple_types",
  641. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  642. COFF::IMAGE_SCN_MEM_READ,
  643. SectionKind::getMetadata(), "types_begin");
  644. DwarfAccelObjCSection = Ctx->getCOFFSection(
  645. ".apple_objc",
  646. COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  647. COFF::IMAGE_SCN_MEM_READ,
  648. SectionKind::getMetadata(), "objc_begin");
  649. DrectveSection = Ctx->getCOFFSection(
  650. ".drectve", COFF::IMAGE_SCN_LNK_INFO | COFF::IMAGE_SCN_LNK_REMOVE,
  651. SectionKind::getMetadata());
  652. PDataSection = Ctx->getCOFFSection(
  653. ".pdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
  654. SectionKind::getDataRel());
  655. XDataSection = Ctx->getCOFFSection(
  656. ".xdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
  657. SectionKind::getDataRel());
  658. SXDataSection = Ctx->getCOFFSection(".sxdata", COFF::IMAGE_SCN_LNK_INFO,
  659. SectionKind::getMetadata());
  660. TLSDataSection = Ctx->getCOFFSection(
  661. ".tls$", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ |
  662. COFF::IMAGE_SCN_MEM_WRITE,
  663. SectionKind::getDataRel());
  664. StackMapSection = Ctx->getCOFFSection(".llvm_stackmaps",
  665. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  666. COFF::IMAGE_SCN_MEM_READ,
  667. SectionKind::getReadOnly());
  668. }
  669. void MCObjectFileInfo::InitMCObjectFileInfo(const Triple &TheTriple,
  670. Reloc::Model relocm,
  671. CodeModel::Model cm,
  672. MCContext &ctx) {
  673. RelocM = relocm;
  674. CMModel = cm;
  675. Ctx = &ctx;
  676. // Common.
  677. CommDirectiveSupportsAlignment = true;
  678. SupportsWeakOmittedEHFrame = true;
  679. SupportsCompactUnwindWithoutEHFrame = false;
  680. PersonalityEncoding = LSDAEncoding = FDECFIEncoding = TTypeEncoding =
  681. dwarf::DW_EH_PE_absptr;
  682. CompactUnwindDwarfEHFrameOnly = 0;
  683. EHFrameSection = nullptr; // Created on demand.
  684. CompactUnwindSection = nullptr; // Used only by selected targets.
  685. DwarfAccelNamesSection = nullptr; // Used only by selected targets.
  686. DwarfAccelObjCSection = nullptr; // Used only by selected targets.
  687. DwarfAccelNamespaceSection = nullptr; // Used only by selected targets.
  688. DwarfAccelTypesSection = nullptr; // Used only by selected targets.
  689. TT = TheTriple;
  690. Triple::ArchType Arch = TT.getArch();
  691. // FIXME: Checking for Arch here to filter out bogus triples such as
  692. // cellspu-apple-darwin. Perhaps we should fix in Triple?
  693. if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
  694. Arch == Triple::arm || Arch == Triple::thumb ||
  695. Arch == Triple::aarch64 ||
  696. Arch == Triple::ppc || Arch == Triple::ppc64 ||
  697. Arch == Triple::UnknownArch) &&
  698. TT.isOSBinFormatMachO()) {
  699. Env = IsMachO;
  700. initMachOMCObjectFileInfo(TT);
  701. } else if ((Arch == Triple::x86 || Arch == Triple::x86_64 ||
  702. Arch == Triple::arm || Arch == Triple::thumb) &&
  703. (TT.isOSWindows() && TT.getObjectFormat() == Triple::COFF)) {
  704. Env = IsCOFF;
  705. initCOFFMCObjectFileInfo(TT);
  706. } else {
  707. Env = IsELF;
  708. initELFMCObjectFileInfo(TT);
  709. }
  710. }
  711. void MCObjectFileInfo::InitMCObjectFileInfo(StringRef TT, Reloc::Model RM,
  712. CodeModel::Model CM,
  713. MCContext &ctx) {
  714. InitMCObjectFileInfo(Triple(TT), RM, CM, ctx);
  715. }
  716. MCSection *MCObjectFileInfo::getDwarfTypesSection(uint64_t Hash) const {
  717. return Ctx->getELFSection(".debug_types", ELF::SHT_PROGBITS, ELF::SHF_GROUP,
  718. 0, utostr(Hash));
  719. }
  720. void MCObjectFileInfo::InitEHFrameSection() {
  721. if (Env == IsMachO)
  722. EHFrameSection =
  723. Ctx->getMachOSection("__TEXT", "__eh_frame",
  724. MachO::S_COALESCED |
  725. MachO::S_ATTR_NO_TOC |
  726. MachO::S_ATTR_STRIP_STATIC_SYMS |
  727. MachO::S_ATTR_LIVE_SUPPORT,
  728. SectionKind::getReadOnly());
  729. else if (Env == IsELF)
  730. EHFrameSection =
  731. Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags);
  732. else
  733. EHFrameSection =
  734. Ctx->getCOFFSection(".eh_frame",
  735. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  736. COFF::IMAGE_SCN_MEM_READ |
  737. COFF::IMAGE_SCN_MEM_WRITE,
  738. SectionKind::getDataRel());
  739. }