TargetLoweringObjectFileImpl.cpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  1. //===- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info ---===//
  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. //
  9. // This file implements classes used to handle lowerings specific to common
  10. // object file formats.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
  14. #include "llvm/ADT/SmallString.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/ADT/Triple.h"
  19. #include "llvm/BinaryFormat/COFF.h"
  20. #include "llvm/BinaryFormat/Dwarf.h"
  21. #include "llvm/BinaryFormat/ELF.h"
  22. #include "llvm/BinaryFormat/MachO.h"
  23. #include "llvm/CodeGen/MachineModuleInfo.h"
  24. #include "llvm/CodeGen/MachineModuleInfoImpls.h"
  25. #include "llvm/IR/Comdat.h"
  26. #include "llvm/IR/Constants.h"
  27. #include "llvm/IR/DataLayout.h"
  28. #include "llvm/IR/DerivedTypes.h"
  29. #include "llvm/IR/Function.h"
  30. #include "llvm/IR/GlobalAlias.h"
  31. #include "llvm/IR/GlobalObject.h"
  32. #include "llvm/IR/GlobalValue.h"
  33. #include "llvm/IR/GlobalVariable.h"
  34. #include "llvm/IR/Mangler.h"
  35. #include "llvm/IR/Metadata.h"
  36. #include "llvm/IR/Module.h"
  37. #include "llvm/IR/Type.h"
  38. #include "llvm/MC/MCAsmInfo.h"
  39. #include "llvm/MC/MCContext.h"
  40. #include "llvm/MC/MCExpr.h"
  41. #include "llvm/MC/MCSectionCOFF.h"
  42. #include "llvm/MC/MCSectionELF.h"
  43. #include "llvm/MC/MCSectionMachO.h"
  44. #include "llvm/MC/MCSectionWasm.h"
  45. #include "llvm/MC/MCStreamer.h"
  46. #include "llvm/MC/MCSymbol.h"
  47. #include "llvm/MC/MCSymbolELF.h"
  48. #include "llvm/MC/MCValue.h"
  49. #include "llvm/MC/SectionKind.h"
  50. #include "llvm/ProfileData/InstrProf.h"
  51. #include "llvm/Support/Casting.h"
  52. #include "llvm/Support/CodeGen.h"
  53. #include "llvm/Support/Format.h"
  54. #include "llvm/Support/ErrorHandling.h"
  55. #include "llvm/Support/raw_ostream.h"
  56. #include "llvm/Target/TargetMachine.h"
  57. #include <cassert>
  58. #include <string>
  59. using namespace llvm;
  60. using namespace dwarf;
  61. static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags,
  62. StringRef &Section) {
  63. SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
  64. M.getModuleFlagsMetadata(ModuleFlags);
  65. for (const auto &MFE: ModuleFlags) {
  66. // Ignore flags with 'Require' behaviour.
  67. if (MFE.Behavior == Module::Require)
  68. continue;
  69. StringRef Key = MFE.Key->getString();
  70. if (Key == "Objective-C Image Info Version") {
  71. Version = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
  72. } else if (Key == "Objective-C Garbage Collection" ||
  73. Key == "Objective-C GC Only" ||
  74. Key == "Objective-C Is Simulated" ||
  75. Key == "Objective-C Class Properties" ||
  76. Key == "Objective-C Image Swift Version") {
  77. Flags |= mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
  78. } else if (Key == "Objective-C Image Info Section") {
  79. Section = cast<MDString>(MFE.Val)->getString();
  80. }
  81. }
  82. }
  83. //===----------------------------------------------------------------------===//
  84. // ELF
  85. //===----------------------------------------------------------------------===//
  86. void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
  87. const TargetMachine &TgtM) {
  88. TargetLoweringObjectFile::Initialize(Ctx, TgtM);
  89. TM = &TgtM;
  90. CodeModel::Model CM = TgtM.getCodeModel();
  91. switch (TgtM.getTargetTriple().getArch()) {
  92. case Triple::arm:
  93. case Triple::armeb:
  94. case Triple::thumb:
  95. case Triple::thumbeb:
  96. if (Ctx.getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM)
  97. break;
  98. // Fallthrough if not using EHABI
  99. LLVM_FALLTHROUGH;
  100. case Triple::ppc:
  101. case Triple::x86:
  102. PersonalityEncoding = isPositionIndependent()
  103. ? dwarf::DW_EH_PE_indirect |
  104. dwarf::DW_EH_PE_pcrel |
  105. dwarf::DW_EH_PE_sdata4
  106. : dwarf::DW_EH_PE_absptr;
  107. LSDAEncoding = isPositionIndependent()
  108. ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
  109. : dwarf::DW_EH_PE_absptr;
  110. TTypeEncoding = isPositionIndependent()
  111. ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  112. dwarf::DW_EH_PE_sdata4
  113. : dwarf::DW_EH_PE_absptr;
  114. break;
  115. case Triple::x86_64:
  116. if (isPositionIndependent()) {
  117. PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  118. ((CM == CodeModel::Small || CM == CodeModel::Medium)
  119. ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
  120. LSDAEncoding = dwarf::DW_EH_PE_pcrel |
  121. (CM == CodeModel::Small
  122. ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8);
  123. TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  124. ((CM == CodeModel::Small || CM == CodeModel::Medium)
  125. ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4);
  126. } else {
  127. PersonalityEncoding =
  128. (CM == CodeModel::Small || CM == CodeModel::Medium)
  129. ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
  130. LSDAEncoding = (CM == CodeModel::Small)
  131. ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
  132. TTypeEncoding = (CM == CodeModel::Small)
  133. ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr;
  134. }
  135. break;
  136. case Triple::hexagon:
  137. PersonalityEncoding = dwarf::DW_EH_PE_absptr;
  138. LSDAEncoding = dwarf::DW_EH_PE_absptr;
  139. TTypeEncoding = dwarf::DW_EH_PE_absptr;
  140. if (isPositionIndependent()) {
  141. PersonalityEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel;
  142. LSDAEncoding |= dwarf::DW_EH_PE_pcrel;
  143. TTypeEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel;
  144. }
  145. break;
  146. case Triple::aarch64:
  147. case Triple::aarch64_be:
  148. // The small model guarantees static code/data size < 4GB, but not where it
  149. // will be in memory. Most of these could end up >2GB away so even a signed
  150. // pc-relative 32-bit address is insufficient, theoretically.
  151. if (isPositionIndependent()) {
  152. PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  153. dwarf::DW_EH_PE_sdata8;
  154. LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8;
  155. TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  156. dwarf::DW_EH_PE_sdata8;
  157. } else {
  158. PersonalityEncoding = dwarf::DW_EH_PE_absptr;
  159. LSDAEncoding = dwarf::DW_EH_PE_absptr;
  160. TTypeEncoding = dwarf::DW_EH_PE_absptr;
  161. }
  162. break;
  163. case Triple::lanai:
  164. LSDAEncoding = dwarf::DW_EH_PE_absptr;
  165. PersonalityEncoding = dwarf::DW_EH_PE_absptr;
  166. TTypeEncoding = dwarf::DW_EH_PE_absptr;
  167. break;
  168. case Triple::mips:
  169. case Triple::mipsel:
  170. case Triple::mips64:
  171. case Triple::mips64el:
  172. // MIPS uses indirect pointer to refer personality functions and types, so
  173. // that the eh_frame section can be read-only. DW.ref.personality will be
  174. // generated for relocation.
  175. PersonalityEncoding = dwarf::DW_EH_PE_indirect;
  176. // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't
  177. // identify N64 from just a triple.
  178. TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  179. dwarf::DW_EH_PE_sdata4;
  180. // We don't support PC-relative LSDA references in GAS so we use the default
  181. // DW_EH_PE_absptr for those.
  182. // FreeBSD must be explicit about the data size and using pcrel since it's
  183. // assembler/linker won't do the automatic conversion that the Linux tools
  184. // do.
  185. if (TgtM.getTargetTriple().isOSFreeBSD()) {
  186. PersonalityEncoding |= dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
  187. LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
  188. }
  189. break;
  190. case Triple::ppc64:
  191. case Triple::ppc64le:
  192. PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  193. dwarf::DW_EH_PE_udata8;
  194. LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8;
  195. TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  196. dwarf::DW_EH_PE_udata8;
  197. break;
  198. case Triple::sparcel:
  199. case Triple::sparc:
  200. if (isPositionIndependent()) {
  201. LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
  202. PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  203. dwarf::DW_EH_PE_sdata4;
  204. TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  205. dwarf::DW_EH_PE_sdata4;
  206. } else {
  207. LSDAEncoding = dwarf::DW_EH_PE_absptr;
  208. PersonalityEncoding = dwarf::DW_EH_PE_absptr;
  209. TTypeEncoding = dwarf::DW_EH_PE_absptr;
  210. }
  211. break;
  212. case Triple::riscv32:
  213. case Triple::riscv64:
  214. LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
  215. PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  216. dwarf::DW_EH_PE_sdata4;
  217. TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  218. dwarf::DW_EH_PE_sdata4;
  219. break;
  220. case Triple::sparcv9:
  221. LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
  222. if (isPositionIndependent()) {
  223. PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  224. dwarf::DW_EH_PE_sdata4;
  225. TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  226. dwarf::DW_EH_PE_sdata4;
  227. } else {
  228. PersonalityEncoding = dwarf::DW_EH_PE_absptr;
  229. TTypeEncoding = dwarf::DW_EH_PE_absptr;
  230. }
  231. break;
  232. case Triple::systemz:
  233. // All currently-defined code models guarantee that 4-byte PC-relative
  234. // values will be in range.
  235. if (isPositionIndependent()) {
  236. PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  237. dwarf::DW_EH_PE_sdata4;
  238. LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
  239. TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
  240. dwarf::DW_EH_PE_sdata4;
  241. } else {
  242. PersonalityEncoding = dwarf::DW_EH_PE_absptr;
  243. LSDAEncoding = dwarf::DW_EH_PE_absptr;
  244. TTypeEncoding = dwarf::DW_EH_PE_absptr;
  245. }
  246. break;
  247. default:
  248. break;
  249. }
  250. }
  251. void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer,
  252. Module &M) const {
  253. auto &C = getContext();
  254. if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
  255. auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS,
  256. ELF::SHF_EXCLUDE);
  257. Streamer.SwitchSection(S);
  258. for (const auto &Operand : LinkerOptions->operands()) {
  259. if (cast<MDNode>(Operand)->getNumOperands() != 2)
  260. report_fatal_error("invalid llvm.linker.options");
  261. for (const auto &Option : cast<MDNode>(Operand)->operands()) {
  262. Streamer.EmitBytes(cast<MDString>(Option)->getString());
  263. Streamer.EmitIntValue(0, 1);
  264. }
  265. }
  266. }
  267. if (NamedMDNode *DependentLibraries = M.getNamedMetadata("llvm.dependent-libraries")) {
  268. auto *S = C.getELFSection(".deplibs", ELF::SHT_LLVM_DEPENDENT_LIBRARIES,
  269. ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
  270. Streamer.SwitchSection(S);
  271. for (const auto &Operand : DependentLibraries->operands()) {
  272. Streamer.EmitBytes(
  273. cast<MDString>(cast<MDNode>(Operand)->getOperand(0))->getString());
  274. Streamer.EmitIntValue(0, 1);
  275. }
  276. }
  277. unsigned Version = 0;
  278. unsigned Flags = 0;
  279. StringRef Section;
  280. GetObjCImageInfo(M, Version, Flags, Section);
  281. if (!Section.empty()) {
  282. auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
  283. Streamer.SwitchSection(S);
  284. Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
  285. Streamer.EmitIntValue(Version, 4);
  286. Streamer.EmitIntValue(Flags, 4);
  287. Streamer.AddBlankLine();
  288. }
  289. SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
  290. M.getModuleFlagsMetadata(ModuleFlags);
  291. MDNode *CFGProfile = nullptr;
  292. for (const auto &MFE : ModuleFlags) {
  293. StringRef Key = MFE.Key->getString();
  294. if (Key == "CG Profile") {
  295. CFGProfile = cast<MDNode>(MFE.Val);
  296. break;
  297. }
  298. }
  299. if (!CFGProfile)
  300. return;
  301. auto GetSym = [this](const MDOperand &MDO) -> MCSymbol * {
  302. if (!MDO)
  303. return nullptr;
  304. auto V = cast<ValueAsMetadata>(MDO);
  305. const Function *F = cast<Function>(V->getValue());
  306. return TM->getSymbol(F);
  307. };
  308. for (const auto &Edge : CFGProfile->operands()) {
  309. MDNode *E = cast<MDNode>(Edge);
  310. const MCSymbol *From = GetSym(E->getOperand(0));
  311. const MCSymbol *To = GetSym(E->getOperand(1));
  312. // Skip null functions. This can happen if functions are dead stripped after
  313. // the CGProfile pass has been run.
  314. if (!From || !To)
  315. continue;
  316. uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2))
  317. ->getValue()
  318. ->getUniqueInteger()
  319. .getZExtValue();
  320. Streamer.emitCGProfileEntry(
  321. MCSymbolRefExpr::create(From, MCSymbolRefExpr::VK_None, C),
  322. MCSymbolRefExpr::create(To, MCSymbolRefExpr::VK_None, C), Count);
  323. }
  324. }
  325. MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol(
  326. const GlobalValue *GV, const TargetMachine &TM,
  327. MachineModuleInfo *MMI) const {
  328. unsigned Encoding = getPersonalityEncoding();
  329. if ((Encoding & 0x80) == DW_EH_PE_indirect)
  330. return getContext().getOrCreateSymbol(StringRef("DW.ref.") +
  331. TM.getSymbol(GV)->getName());
  332. if ((Encoding & 0x70) == DW_EH_PE_absptr)
  333. return TM.getSymbol(GV);
  334. report_fatal_error("We do not support this DWARF encoding yet!");
  335. }
  336. void TargetLoweringObjectFileELF::emitPersonalityValue(
  337. MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const {
  338. SmallString<64> NameData("DW.ref.");
  339. NameData += Sym->getName();
  340. MCSymbolELF *Label =
  341. cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData));
  342. Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
  343. Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
  344. unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP;
  345. MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(),
  346. ELF::SHT_PROGBITS, Flags, 0);
  347. unsigned Size = DL.getPointerSize();
  348. Streamer.SwitchSection(Sec);
  349. Streamer.EmitValueToAlignment(DL.getPointerABIAlignment(0));
  350. Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
  351. const MCExpr *E = MCConstantExpr::create(Size, getContext());
  352. Streamer.emitELFSize(Label, E);
  353. Streamer.EmitLabel(Label);
  354. Streamer.EmitSymbolValue(Sym, Size);
  355. }
  356. const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
  357. const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
  358. MachineModuleInfo *MMI, MCStreamer &Streamer) const {
  359. if (Encoding & DW_EH_PE_indirect) {
  360. MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
  361. MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", TM);
  362. // Add information about the stub reference to ELFMMI so that the stub
  363. // gets emitted by the asmprinter.
  364. MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym);
  365. if (!StubSym.getPointer()) {
  366. MCSymbol *Sym = TM.getSymbol(GV);
  367. StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
  368. }
  369. return TargetLoweringObjectFile::
  370. getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
  371. Encoding & ~DW_EH_PE_indirect, Streamer);
  372. }
  373. return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
  374. MMI, Streamer);
  375. }
  376. static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) {
  377. // N.B.: The defaults used in here are not the same ones used in MC.
  378. // We follow gcc, MC follows gas. For example, given ".section .eh_frame",
  379. // both gas and MC will produce a section with no flags. Given
  380. // section(".eh_frame") gcc will produce:
  381. //
  382. // .section .eh_frame,"a",@progbits
  383. if (Name == getInstrProfSectionName(IPSK_covmap, Triple::ELF,
  384. /*AddSegmentInfo=*/false))
  385. return SectionKind::getMetadata();
  386. if (Name.empty() || Name[0] != '.') return K;
  387. // Default implementation based on some magic section names.
  388. if (Name == ".bss" ||
  389. Name.startswith(".bss.") ||
  390. Name.startswith(".gnu.linkonce.b.") ||
  391. Name.startswith(".llvm.linkonce.b.") ||
  392. Name == ".sbss" ||
  393. Name.startswith(".sbss.") ||
  394. Name.startswith(".gnu.linkonce.sb.") ||
  395. Name.startswith(".llvm.linkonce.sb."))
  396. return SectionKind::getBSS();
  397. if (Name == ".tdata" ||
  398. Name.startswith(".tdata.") ||
  399. Name.startswith(".gnu.linkonce.td.") ||
  400. Name.startswith(".llvm.linkonce.td."))
  401. return SectionKind::getThreadData();
  402. if (Name == ".tbss" ||
  403. Name.startswith(".tbss.") ||
  404. Name.startswith(".gnu.linkonce.tb.") ||
  405. Name.startswith(".llvm.linkonce.tb."))
  406. return SectionKind::getThreadBSS();
  407. return K;
  408. }
  409. static unsigned getELFSectionType(StringRef Name, SectionKind K) {
  410. // Use SHT_NOTE for section whose name starts with ".note" to allow
  411. // emitting ELF notes from C variable declaration.
  412. // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609
  413. if (Name.startswith(".note"))
  414. return ELF::SHT_NOTE;
  415. if (Name == ".init_array")
  416. return ELF::SHT_INIT_ARRAY;
  417. if (Name == ".fini_array")
  418. return ELF::SHT_FINI_ARRAY;
  419. if (Name == ".preinit_array")
  420. return ELF::SHT_PREINIT_ARRAY;
  421. if (K.isBSS() || K.isThreadBSS())
  422. return ELF::SHT_NOBITS;
  423. return ELF::SHT_PROGBITS;
  424. }
  425. static unsigned getELFSectionFlags(SectionKind K) {
  426. unsigned Flags = 0;
  427. if (!K.isMetadata())
  428. Flags |= ELF::SHF_ALLOC;
  429. if (K.isText())
  430. Flags |= ELF::SHF_EXECINSTR;
  431. if (K.isExecuteOnly())
  432. Flags |= ELF::SHF_ARM_PURECODE;
  433. if (K.isWriteable())
  434. Flags |= ELF::SHF_WRITE;
  435. if (K.isThreadLocal())
  436. Flags |= ELF::SHF_TLS;
  437. if (K.isMergeableCString() || K.isMergeableConst())
  438. Flags |= ELF::SHF_MERGE;
  439. if (K.isMergeableCString())
  440. Flags |= ELF::SHF_STRINGS;
  441. return Flags;
  442. }
  443. static const Comdat *getELFComdat(const GlobalValue *GV) {
  444. const Comdat *C = GV->getComdat();
  445. if (!C)
  446. return nullptr;
  447. if (C->getSelectionKind() != Comdat::Any)
  448. report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" +
  449. C->getName() + "' cannot be lowered.");
  450. return C;
  451. }
  452. static const MCSymbolELF *getAssociatedSymbol(const GlobalObject *GO,
  453. const TargetMachine &TM) {
  454. MDNode *MD = GO->getMetadata(LLVMContext::MD_associated);
  455. if (!MD)
  456. return nullptr;
  457. const MDOperand &Op = MD->getOperand(0);
  458. if (!Op.get())
  459. return nullptr;
  460. auto *VM = dyn_cast<ValueAsMetadata>(Op);
  461. if (!VM)
  462. report_fatal_error("MD_associated operand is not ValueAsMetadata");
  463. GlobalObject *OtherGO = dyn_cast<GlobalObject>(VM->getValue());
  464. return OtherGO ? dyn_cast<MCSymbolELF>(TM.getSymbol(OtherGO)) : nullptr;
  465. }
  466. static unsigned getEntrySizeForKind(SectionKind Kind) {
  467. if (Kind.isMergeable1ByteCString())
  468. return 1;
  469. else if (Kind.isMergeable2ByteCString())
  470. return 2;
  471. else if (Kind.isMergeable4ByteCString())
  472. return 4;
  473. else if (Kind.isMergeableConst4())
  474. return 4;
  475. else if (Kind.isMergeableConst8())
  476. return 8;
  477. else if (Kind.isMergeableConst16())
  478. return 16;
  479. else if (Kind.isMergeableConst32())
  480. return 32;
  481. else {
  482. // We shouldn't have mergeable C strings or mergeable constants that we
  483. // didn't handle above.
  484. assert(!Kind.isMergeableCString() && "unknown string width");
  485. assert(!Kind.isMergeableConst() && "unknown data width");
  486. return 0;
  487. }
  488. }
  489. MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
  490. const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
  491. StringRef SectionName = GO->getSection();
  492. // Check if '#pragma clang section' name is applicable.
  493. // Note that pragma directive overrides -ffunction-section, -fdata-section
  494. // and so section name is exactly as user specified and not uniqued.
  495. const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
  496. if (GV && GV->hasImplicitSection()) {
  497. auto Attrs = GV->getAttributes();
  498. if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
  499. SectionName = Attrs.getAttribute("bss-section").getValueAsString();
  500. } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
  501. SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
  502. } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
  503. SectionName = Attrs.getAttribute("data-section").getValueAsString();
  504. }
  505. }
  506. const Function *F = dyn_cast<Function>(GO);
  507. if (F && F->hasFnAttribute("implicit-section-name")) {
  508. SectionName = F->getFnAttribute("implicit-section-name").getValueAsString();
  509. }
  510. // Infer section flags from the section name if we can.
  511. Kind = getELFKindForNamedSection(SectionName, Kind);
  512. StringRef Group = "";
  513. unsigned Flags = getELFSectionFlags(Kind);
  514. if (const Comdat *C = getELFComdat(GO)) {
  515. Group = C->getName();
  516. Flags |= ELF::SHF_GROUP;
  517. }
  518. // A section can have at most one associated section. Put each global with
  519. // MD_associated in a unique section.
  520. unsigned UniqueID = MCContext::GenericSectionID;
  521. const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM);
  522. if (AssociatedSymbol) {
  523. UniqueID = NextUniqueID++;
  524. Flags |= ELF::SHF_LINK_ORDER;
  525. }
  526. MCSectionELF *Section = getContext().getELFSection(
  527. SectionName, getELFSectionType(SectionName, Kind), Flags,
  528. getEntrySizeForKind(Kind), Group, UniqueID, AssociatedSymbol);
  529. // Make sure that we did not get some other section with incompatible sh_link.
  530. // This should not be possible due to UniqueID code above.
  531. assert(Section->getAssociatedSymbol() == AssociatedSymbol &&
  532. "Associated symbol mismatch between sections");
  533. return Section;
  534. }
  535. /// Return the section prefix name used by options FunctionsSections and
  536. /// DataSections.
  537. static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
  538. if (Kind.isText())
  539. return ".text";
  540. if (Kind.isReadOnly())
  541. return ".rodata";
  542. if (Kind.isBSS())
  543. return ".bss";
  544. if (Kind.isThreadData())
  545. return ".tdata";
  546. if (Kind.isThreadBSS())
  547. return ".tbss";
  548. if (Kind.isData())
  549. return ".data";
  550. assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
  551. return ".data.rel.ro";
  552. }
  553. static MCSectionELF *selectELFSectionForGlobal(
  554. MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
  555. const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags,
  556. unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) {
  557. StringRef Group = "";
  558. if (const Comdat *C = getELFComdat(GO)) {
  559. Flags |= ELF::SHF_GROUP;
  560. Group = C->getName();
  561. }
  562. // Get the section entry size based on the kind.
  563. unsigned EntrySize = getEntrySizeForKind(Kind);
  564. SmallString<128> Name;
  565. if (Kind.isMergeableCString()) {
  566. // We also need alignment here.
  567. // FIXME: this is getting the alignment of the character, not the
  568. // alignment of the global!
  569. unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment(
  570. cast<GlobalVariable>(GO));
  571. std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
  572. Name = SizeSpec + utostr(Align);
  573. } else if (Kind.isMergeableConst()) {
  574. Name = ".rodata.cst";
  575. Name += utostr(EntrySize);
  576. } else {
  577. Name = getSectionPrefixForGlobal(Kind);
  578. }
  579. if (const auto *F = dyn_cast<Function>(GO)) {
  580. const auto &OptionalPrefix = F->getSectionPrefix();
  581. if (OptionalPrefix)
  582. Name += *OptionalPrefix;
  583. }
  584. unsigned UniqueID = MCContext::GenericSectionID;
  585. if (EmitUniqueSection) {
  586. if (TM.getUniqueSectionNames()) {
  587. Name.push_back('.');
  588. TM.getNameWithPrefix(Name, GO, Mang, true /*MayAlwaysUsePrivate*/);
  589. } else {
  590. UniqueID = *NextUniqueID;
  591. (*NextUniqueID)++;
  592. }
  593. }
  594. // Use 0 as the unique ID for execute-only text.
  595. if (Kind.isExecuteOnly())
  596. UniqueID = 0;
  597. return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags,
  598. EntrySize, Group, UniqueID, AssociatedSymbol);
  599. }
  600. MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
  601. const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
  602. unsigned Flags = getELFSectionFlags(Kind);
  603. // If we have -ffunction-section or -fdata-section then we should emit the
  604. // global value to a uniqued section specifically for it.
  605. bool EmitUniqueSection = false;
  606. if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) {
  607. if (Kind.isText())
  608. EmitUniqueSection = TM.getFunctionSections();
  609. else
  610. EmitUniqueSection = TM.getDataSections();
  611. }
  612. EmitUniqueSection |= GO->hasComdat();
  613. const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM);
  614. if (AssociatedSymbol) {
  615. EmitUniqueSection = true;
  616. Flags |= ELF::SHF_LINK_ORDER;
  617. }
  618. MCSectionELF *Section = selectELFSectionForGlobal(
  619. getContext(), GO, Kind, getMangler(), TM, EmitUniqueSection, Flags,
  620. &NextUniqueID, AssociatedSymbol);
  621. assert(Section->getAssociatedSymbol() == AssociatedSymbol);
  622. return Section;
  623. }
  624. MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(
  625. const Function &F, const TargetMachine &TM) const {
  626. // If the function can be removed, produce a unique section so that
  627. // the table doesn't prevent the removal.
  628. const Comdat *C = F.getComdat();
  629. bool EmitUniqueSection = TM.getFunctionSections() || C;
  630. if (!EmitUniqueSection)
  631. return ReadOnlySection;
  632. return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(),
  633. getMangler(), TM, EmitUniqueSection,
  634. ELF::SHF_ALLOC, &NextUniqueID,
  635. /* AssociatedSymbol */ nullptr);
  636. }
  637. bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
  638. bool UsesLabelDifference, const Function &F) const {
  639. // We can always create relative relocations, so use another section
  640. // that can be marked non-executable.
  641. return false;
  642. }
  643. /// Given a mergeable constant with the specified size and relocation
  644. /// information, return a section that it should be placed in.
  645. MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
  646. const DataLayout &DL, SectionKind Kind, const Constant *C,
  647. unsigned &Align) const {
  648. if (Kind.isMergeableConst4() && MergeableConst4Section)
  649. return MergeableConst4Section;
  650. if (Kind.isMergeableConst8() && MergeableConst8Section)
  651. return MergeableConst8Section;
  652. if (Kind.isMergeableConst16() && MergeableConst16Section)
  653. return MergeableConst16Section;
  654. if (Kind.isMergeableConst32() && MergeableConst32Section)
  655. return MergeableConst32Section;
  656. if (Kind.isReadOnly())
  657. return ReadOnlySection;
  658. assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
  659. return DataRelROSection;
  660. }
  661. static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray,
  662. bool IsCtor, unsigned Priority,
  663. const MCSymbol *KeySym) {
  664. std::string Name;
  665. unsigned Type;
  666. unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
  667. StringRef COMDAT = KeySym ? KeySym->getName() : "";
  668. if (KeySym)
  669. Flags |= ELF::SHF_GROUP;
  670. if (UseInitArray) {
  671. if (IsCtor) {
  672. Type = ELF::SHT_INIT_ARRAY;
  673. Name = ".init_array";
  674. } else {
  675. Type = ELF::SHT_FINI_ARRAY;
  676. Name = ".fini_array";
  677. }
  678. if (Priority != 65535) {
  679. Name += '.';
  680. Name += utostr(Priority);
  681. }
  682. } else {
  683. // The default scheme is .ctor / .dtor, so we have to invert the priority
  684. // numbering.
  685. if (IsCtor)
  686. Name = ".ctors";
  687. else
  688. Name = ".dtors";
  689. if (Priority != 65535)
  690. raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
  691. Type = ELF::SHT_PROGBITS;
  692. }
  693. return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT);
  694. }
  695. MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
  696. unsigned Priority, const MCSymbol *KeySym) const {
  697. return getStaticStructorSection(getContext(), UseInitArray, true, Priority,
  698. KeySym);
  699. }
  700. MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
  701. unsigned Priority, const MCSymbol *KeySym) const {
  702. return getStaticStructorSection(getContext(), UseInitArray, false, Priority,
  703. KeySym);
  704. }
  705. const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference(
  706. const GlobalValue *LHS, const GlobalValue *RHS,
  707. const TargetMachine &TM) const {
  708. // We may only use a PLT-relative relocation to refer to unnamed_addr
  709. // functions.
  710. if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
  711. return nullptr;
  712. // Basic sanity checks.
  713. if (LHS->getType()->getPointerAddressSpace() != 0 ||
  714. RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
  715. RHS->isThreadLocal())
  716. return nullptr;
  717. return MCBinaryExpr::createSub(
  718. MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind,
  719. getContext()),
  720. MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
  721. }
  722. MCSection *TargetLoweringObjectFileELF::getSectionForCommandLines() const {
  723. // Use ".GCC.command.line" since this feature is to support clang's
  724. // -frecord-gcc-switches which in turn attempts to mimic GCC's switch of the
  725. // same name.
  726. return getContext().getELFSection(".GCC.command.line", ELF::SHT_PROGBITS,
  727. ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
  728. }
  729. void
  730. TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
  731. UseInitArray = UseInitArray_;
  732. MCContext &Ctx = getContext();
  733. if (!UseInitArray) {
  734. StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS,
  735. ELF::SHF_ALLOC | ELF::SHF_WRITE);
  736. StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS,
  737. ELF::SHF_ALLOC | ELF::SHF_WRITE);
  738. return;
  739. }
  740. StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
  741. ELF::SHF_WRITE | ELF::SHF_ALLOC);
  742. StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
  743. ELF::SHF_WRITE | ELF::SHF_ALLOC);
  744. }
  745. //===----------------------------------------------------------------------===//
  746. // MachO
  747. //===----------------------------------------------------------------------===//
  748. TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO()
  749. : TargetLoweringObjectFile() {
  750. SupportIndirectSymViaGOTPCRel = true;
  751. }
  752. void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
  753. const TargetMachine &TM) {
  754. TargetLoweringObjectFile::Initialize(Ctx, TM);
  755. if (TM.getRelocationModel() == Reloc::Static) {
  756. StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0,
  757. SectionKind::getData());
  758. StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0,
  759. SectionKind::getData());
  760. } else {
  761. StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func",
  762. MachO::S_MOD_INIT_FUNC_POINTERS,
  763. SectionKind::getData());
  764. StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func",
  765. MachO::S_MOD_TERM_FUNC_POINTERS,
  766. SectionKind::getData());
  767. }
  768. PersonalityEncoding =
  769. dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
  770. LSDAEncoding = dwarf::DW_EH_PE_pcrel;
  771. TTypeEncoding =
  772. dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
  773. }
  774. void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer,
  775. Module &M) const {
  776. // Emit the linker options if present.
  777. if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
  778. for (const auto &Option : LinkerOptions->operands()) {
  779. SmallVector<std::string, 4> StrOptions;
  780. for (const auto &Piece : cast<MDNode>(Option)->operands())
  781. StrOptions.push_back(cast<MDString>(Piece)->getString());
  782. Streamer.EmitLinkerOptions(StrOptions);
  783. }
  784. }
  785. unsigned VersionVal = 0;
  786. unsigned ImageInfoFlags = 0;
  787. StringRef SectionVal;
  788. GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal);
  789. // The section is mandatory. If we don't have it, then we don't have GC info.
  790. if (SectionVal.empty())
  791. return;
  792. StringRef Segment, Section;
  793. unsigned TAA = 0, StubSize = 0;
  794. bool TAAParsed;
  795. std::string ErrorCode =
  796. MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
  797. TAA, TAAParsed, StubSize);
  798. if (!ErrorCode.empty())
  799. // If invalid, report the error with report_fatal_error.
  800. report_fatal_error("Invalid section specifier '" + Section + "': " +
  801. ErrorCode + ".");
  802. // Get the section.
  803. MCSectionMachO *S = getContext().getMachOSection(
  804. Segment, Section, TAA, StubSize, SectionKind::getData());
  805. Streamer.SwitchSection(S);
  806. Streamer.EmitLabel(getContext().
  807. getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
  808. Streamer.EmitIntValue(VersionVal, 4);
  809. Streamer.EmitIntValue(ImageInfoFlags, 4);
  810. Streamer.AddBlankLine();
  811. }
  812. static void checkMachOComdat(const GlobalValue *GV) {
  813. const Comdat *C = GV->getComdat();
  814. if (!C)
  815. return;
  816. report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() +
  817. "' cannot be lowered.");
  818. }
  819. MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
  820. const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
  821. // Parse the section specifier and create it if valid.
  822. StringRef Segment, Section;
  823. unsigned TAA = 0, StubSize = 0;
  824. bool TAAParsed;
  825. checkMachOComdat(GO);
  826. std::string ErrorCode =
  827. MCSectionMachO::ParseSectionSpecifier(GO->getSection(), Segment, Section,
  828. TAA, TAAParsed, StubSize);
  829. if (!ErrorCode.empty()) {
  830. // If invalid, report the error with report_fatal_error.
  831. report_fatal_error("Global variable '" + GO->getName() +
  832. "' has an invalid section specifier '" +
  833. GO->getSection() + "': " + ErrorCode + ".");
  834. }
  835. // Get the section.
  836. MCSectionMachO *S =
  837. getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind);
  838. // If TAA wasn't set by ParseSectionSpecifier() above,
  839. // use the value returned by getMachOSection() as a default.
  840. if (!TAAParsed)
  841. TAA = S->getTypeAndAttributes();
  842. // Okay, now that we got the section, verify that the TAA & StubSize agree.
  843. // If the user declared multiple globals with different section flags, we need
  844. // to reject it here.
  845. if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
  846. // If invalid, report the error with report_fatal_error.
  847. report_fatal_error("Global variable '" + GO->getName() +
  848. "' section type or attributes does not match previous"
  849. " section specifier");
  850. }
  851. return S;
  852. }
  853. MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
  854. const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
  855. checkMachOComdat(GO);
  856. // Handle thread local data.
  857. if (Kind.isThreadBSS()) return TLSBSSSection;
  858. if (Kind.isThreadData()) return TLSDataSection;
  859. if (Kind.isText())
  860. return GO->isWeakForLinker() ? TextCoalSection : TextSection;
  861. // If this is weak/linkonce, put this in a coalescable section, either in text
  862. // or data depending on if it is writable.
  863. if (GO->isWeakForLinker()) {
  864. if (Kind.isReadOnly())
  865. return ConstTextCoalSection;
  866. if (Kind.isReadOnlyWithRel())
  867. return ConstDataCoalSection;
  868. return DataCoalSection;
  869. }
  870. // FIXME: Alignment check should be handled by section classifier.
  871. if (Kind.isMergeable1ByteCString() &&
  872. GO->getParent()->getDataLayout().getPreferredAlignment(
  873. cast<GlobalVariable>(GO)) < 32)
  874. return CStringSection;
  875. // Do not put 16-bit arrays in the UString section if they have an
  876. // externally visible label, this runs into issues with certain linker
  877. // versions.
  878. if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() &&
  879. GO->getParent()->getDataLayout().getPreferredAlignment(
  880. cast<GlobalVariable>(GO)) < 32)
  881. return UStringSection;
  882. // With MachO only variables whose corresponding symbol starts with 'l' or
  883. // 'L' can be merged, so we only try merging GVs with private linkage.
  884. if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) {
  885. if (Kind.isMergeableConst4())
  886. return FourByteConstantSection;
  887. if (Kind.isMergeableConst8())
  888. return EightByteConstantSection;
  889. if (Kind.isMergeableConst16())
  890. return SixteenByteConstantSection;
  891. }
  892. // Otherwise, if it is readonly, but not something we can specially optimize,
  893. // just drop it in .const.
  894. if (Kind.isReadOnly())
  895. return ReadOnlySection;
  896. // If this is marked const, put it into a const section. But if the dynamic
  897. // linker needs to write to it, put it in the data segment.
  898. if (Kind.isReadOnlyWithRel())
  899. return ConstDataSection;
  900. // Put zero initialized globals with strong external linkage in the
  901. // DATA, __common section with the .zerofill directive.
  902. if (Kind.isBSSExtern())
  903. return DataCommonSection;
  904. // Put zero initialized globals with local linkage in __DATA,__bss directive
  905. // with the .zerofill directive (aka .lcomm).
  906. if (Kind.isBSSLocal())
  907. return DataBSSSection;
  908. // Otherwise, just drop the variable in the normal data section.
  909. return DataSection;
  910. }
  911. MCSection *TargetLoweringObjectFileMachO::getSectionForConstant(
  912. const DataLayout &DL, SectionKind Kind, const Constant *C,
  913. unsigned &Align) const {
  914. // If this constant requires a relocation, we have to put it in the data
  915. // segment, not in the text segment.
  916. if (Kind.isData() || Kind.isReadOnlyWithRel())
  917. return ConstDataSection;
  918. if (Kind.isMergeableConst4())
  919. return FourByteConstantSection;
  920. if (Kind.isMergeableConst8())
  921. return EightByteConstantSection;
  922. if (Kind.isMergeableConst16())
  923. return SixteenByteConstantSection;
  924. return ReadOnlySection; // .const
  925. }
  926. const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference(
  927. const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
  928. MachineModuleInfo *MMI, MCStreamer &Streamer) const {
  929. // The mach-o version of this method defaults to returning a stub reference.
  930. if (Encoding & DW_EH_PE_indirect) {
  931. MachineModuleInfoMachO &MachOMMI =
  932. MMI->getObjFileInfo<MachineModuleInfoMachO>();
  933. MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
  934. // Add information about the stub reference to MachOMMI so that the stub
  935. // gets emitted by the asmprinter.
  936. MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
  937. if (!StubSym.getPointer()) {
  938. MCSymbol *Sym = TM.getSymbol(GV);
  939. StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
  940. }
  941. return TargetLoweringObjectFile::
  942. getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()),
  943. Encoding & ~DW_EH_PE_indirect, Streamer);
  944. }
  945. return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM,
  946. MMI, Streamer);
  947. }
  948. MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol(
  949. const GlobalValue *GV, const TargetMachine &TM,
  950. MachineModuleInfo *MMI) const {
  951. // The mach-o version of this method defaults to returning a stub reference.
  952. MachineModuleInfoMachO &MachOMMI =
  953. MMI->getObjFileInfo<MachineModuleInfoMachO>();
  954. MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM);
  955. // Add information about the stub reference to MachOMMI so that the stub
  956. // gets emitted by the asmprinter.
  957. MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym);
  958. if (!StubSym.getPointer()) {
  959. MCSymbol *Sym = TM.getSymbol(GV);
  960. StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage());
  961. }
  962. return SSym;
  963. }
  964. const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
  965. const MCSymbol *Sym, const MCValue &MV, int64_t Offset,
  966. MachineModuleInfo *MMI, MCStreamer &Streamer) const {
  967. // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation
  968. // as 64-bit do, we replace the GOT equivalent by accessing the final symbol
  969. // through a non_lazy_ptr stub instead. One advantage is that it allows the
  970. // computation of deltas to final external symbols. Example:
  971. //
  972. // _extgotequiv:
  973. // .long _extfoo
  974. //
  975. // _delta:
  976. // .long _extgotequiv-_delta
  977. //
  978. // is transformed to:
  979. //
  980. // _delta:
  981. // .long L_extfoo$non_lazy_ptr-(_delta+0)
  982. //
  983. // .section __IMPORT,__pointers,non_lazy_symbol_pointers
  984. // L_extfoo$non_lazy_ptr:
  985. // .indirect_symbol _extfoo
  986. // .long 0
  987. //
  988. // The indirect symbol table (and sections of non_lazy_symbol_pointers type)
  989. // may point to both local (same translation unit) and global (other
  990. // translation units) symbols. Example:
  991. //
  992. // .section __DATA,__pointers,non_lazy_symbol_pointers
  993. // L1:
  994. // .indirect_symbol _myGlobal
  995. // .long 0
  996. // L2:
  997. // .indirect_symbol _myLocal
  998. // .long _myLocal
  999. //
  1000. // If the symbol is local, instead of the symbol's index, the assembler
  1001. // places the constant INDIRECT_SYMBOL_LOCAL into the indirect symbol table.
  1002. // Then the linker will notice the constant in the table and will look at the
  1003. // content of the symbol.
  1004. MachineModuleInfoMachO &MachOMMI =
  1005. MMI->getObjFileInfo<MachineModuleInfoMachO>();
  1006. MCContext &Ctx = getContext();
  1007. // The offset must consider the original displacement from the base symbol
  1008. // since 32-bit targets don't have a GOTPCREL to fold the PC displacement.
  1009. Offset = -MV.getConstant();
  1010. const MCSymbol *BaseSym = &MV.getSymB()->getSymbol();
  1011. // Access the final symbol via sym$non_lazy_ptr and generate the appropriated
  1012. // non_lazy_ptr stubs.
  1013. SmallString<128> Name;
  1014. StringRef Suffix = "$non_lazy_ptr";
  1015. Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix();
  1016. Name += Sym->getName();
  1017. Name += Suffix;
  1018. MCSymbol *Stub = Ctx.getOrCreateSymbol(Name);
  1019. MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub);
  1020. if (!StubSym.getPointer()) {
  1021. bool IsIndirectLocal = Sym->isDefined() && !Sym->isExternal();
  1022. // With the assumption that IsIndirectLocal == GV->hasLocalLinkage().
  1023. StubSym = MachineModuleInfoImpl::StubValueTy(const_cast<MCSymbol *>(Sym),
  1024. !IsIndirectLocal);
  1025. }
  1026. const MCExpr *BSymExpr =
  1027. MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx);
  1028. const MCExpr *LHS =
  1029. MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx);
  1030. if (!Offset)
  1031. return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx);
  1032. const MCExpr *RHS =
  1033. MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx);
  1034. return MCBinaryExpr::createSub(LHS, RHS, Ctx);
  1035. }
  1036. static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
  1037. const MCSection &Section) {
  1038. if (!AsmInfo.isSectionAtomizableBySymbols(Section))
  1039. return true;
  1040. // If it is not dead stripped, it is safe to use private labels.
  1041. const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
  1042. if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
  1043. return true;
  1044. return false;
  1045. }
  1046. void TargetLoweringObjectFileMachO::getNameWithPrefix(
  1047. SmallVectorImpl<char> &OutName, const GlobalValue *GV,
  1048. const TargetMachine &TM) const {
  1049. bool CannotUsePrivateLabel = true;
  1050. if (auto *GO = GV->getBaseObject()) {
  1051. SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM);
  1052. const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM);
  1053. CannotUsePrivateLabel =
  1054. !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
  1055. }
  1056. getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
  1057. }
  1058. //===----------------------------------------------------------------------===//
  1059. // COFF
  1060. //===----------------------------------------------------------------------===//
  1061. static unsigned
  1062. getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) {
  1063. unsigned Flags = 0;
  1064. bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb;
  1065. if (K.isMetadata())
  1066. Flags |=
  1067. COFF::IMAGE_SCN_MEM_DISCARDABLE;
  1068. else if (K.isText())
  1069. Flags |=
  1070. COFF::IMAGE_SCN_MEM_EXECUTE |
  1071. COFF::IMAGE_SCN_MEM_READ |
  1072. COFF::IMAGE_SCN_CNT_CODE |
  1073. (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0);
  1074. else if (K.isBSS())
  1075. Flags |=
  1076. COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
  1077. COFF::IMAGE_SCN_MEM_READ |
  1078. COFF::IMAGE_SCN_MEM_WRITE;
  1079. else if (K.isThreadLocal())
  1080. Flags |=
  1081. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  1082. COFF::IMAGE_SCN_MEM_READ |
  1083. COFF::IMAGE_SCN_MEM_WRITE;
  1084. else if (K.isReadOnly() || K.isReadOnlyWithRel())
  1085. Flags |=
  1086. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  1087. COFF::IMAGE_SCN_MEM_READ;
  1088. else if (K.isWriteable())
  1089. Flags |=
  1090. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  1091. COFF::IMAGE_SCN_MEM_READ |
  1092. COFF::IMAGE_SCN_MEM_WRITE;
  1093. return Flags;
  1094. }
  1095. static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) {
  1096. const Comdat *C = GV->getComdat();
  1097. assert(C && "expected GV to have a Comdat!");
  1098. StringRef ComdatGVName = C->getName();
  1099. const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName);
  1100. if (!ComdatGV)
  1101. report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
  1102. "' does not exist.");
  1103. if (ComdatGV->getComdat() != C)
  1104. report_fatal_error("Associative COMDAT symbol '" + ComdatGVName +
  1105. "' is not a key for its COMDAT.");
  1106. return ComdatGV;
  1107. }
  1108. static int getSelectionForCOFF(const GlobalValue *GV) {
  1109. if (const Comdat *C = GV->getComdat()) {
  1110. const GlobalValue *ComdatKey = getComdatGVForCOFF(GV);
  1111. if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey))
  1112. ComdatKey = GA->getBaseObject();
  1113. if (ComdatKey == GV) {
  1114. switch (C->getSelectionKind()) {
  1115. case Comdat::Any:
  1116. return COFF::IMAGE_COMDAT_SELECT_ANY;
  1117. case Comdat::ExactMatch:
  1118. return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH;
  1119. case Comdat::Largest:
  1120. return COFF::IMAGE_COMDAT_SELECT_LARGEST;
  1121. case Comdat::NoDuplicates:
  1122. return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
  1123. case Comdat::SameSize:
  1124. return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE;
  1125. }
  1126. } else {
  1127. return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
  1128. }
  1129. }
  1130. return 0;
  1131. }
  1132. MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
  1133. const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
  1134. int Selection = 0;
  1135. unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
  1136. StringRef Name = GO->getSection();
  1137. StringRef COMDATSymName = "";
  1138. if (GO->hasComdat()) {
  1139. Selection = getSelectionForCOFF(GO);
  1140. const GlobalValue *ComdatGV;
  1141. if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
  1142. ComdatGV = getComdatGVForCOFF(GO);
  1143. else
  1144. ComdatGV = GO;
  1145. if (!ComdatGV->hasPrivateLinkage()) {
  1146. MCSymbol *Sym = TM.getSymbol(ComdatGV);
  1147. COMDATSymName = Sym->getName();
  1148. Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
  1149. } else {
  1150. Selection = 0;
  1151. }
  1152. }
  1153. return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName,
  1154. Selection);
  1155. }
  1156. static StringRef getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
  1157. if (Kind.isText())
  1158. return ".text";
  1159. if (Kind.isBSS())
  1160. return ".bss";
  1161. if (Kind.isThreadLocal())
  1162. return ".tls$";
  1163. if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
  1164. return ".rdata";
  1165. return ".data";
  1166. }
  1167. MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
  1168. const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
  1169. // If we have -ffunction-sections then we should emit the global value to a
  1170. // uniqued section specifically for it.
  1171. bool EmitUniquedSection;
  1172. if (Kind.isText())
  1173. EmitUniquedSection = TM.getFunctionSections();
  1174. else
  1175. EmitUniquedSection = TM.getDataSections();
  1176. if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) {
  1177. SmallString<256> Name = getCOFFSectionNameForUniqueGlobal(Kind);
  1178. unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
  1179. Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
  1180. int Selection = getSelectionForCOFF(GO);
  1181. if (!Selection)
  1182. Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
  1183. const GlobalValue *ComdatGV;
  1184. if (GO->hasComdat())
  1185. ComdatGV = getComdatGVForCOFF(GO);
  1186. else
  1187. ComdatGV = GO;
  1188. unsigned UniqueID = MCContext::GenericSectionID;
  1189. if (EmitUniquedSection)
  1190. UniqueID = NextUniqueID++;
  1191. if (!ComdatGV->hasPrivateLinkage()) {
  1192. MCSymbol *Sym = TM.getSymbol(ComdatGV);
  1193. StringRef COMDATSymName = Sym->getName();
  1194. // Append "$symbol" to the section name *before* IR-level mangling is
  1195. // applied when targetting mingw. This is what GCC does, and the ld.bfd
  1196. // COFF linker will not properly handle comdats otherwise.
  1197. if (getTargetTriple().isWindowsGNUEnvironment())
  1198. raw_svector_ostream(Name) << '$' << ComdatGV->getName();
  1199. return getContext().getCOFFSection(Name, Characteristics, Kind,
  1200. COMDATSymName, Selection, UniqueID);
  1201. } else {
  1202. SmallString<256> TmpData;
  1203. getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true);
  1204. return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData,
  1205. Selection, UniqueID);
  1206. }
  1207. }
  1208. if (Kind.isText())
  1209. return TextSection;
  1210. if (Kind.isThreadLocal())
  1211. return TLSDataSection;
  1212. if (Kind.isReadOnly() || Kind.isReadOnlyWithRel())
  1213. return ReadOnlySection;
  1214. // Note: we claim that common symbols are put in BSSSection, but they are
  1215. // really emitted with the magic .comm directive, which creates a symbol table
  1216. // entry but not a section.
  1217. if (Kind.isBSS() || Kind.isCommon())
  1218. return BSSSection;
  1219. return DataSection;
  1220. }
  1221. void TargetLoweringObjectFileCOFF::getNameWithPrefix(
  1222. SmallVectorImpl<char> &OutName, const GlobalValue *GV,
  1223. const TargetMachine &TM) const {
  1224. bool CannotUsePrivateLabel = false;
  1225. if (GV->hasPrivateLinkage() &&
  1226. ((isa<Function>(GV) && TM.getFunctionSections()) ||
  1227. (isa<GlobalVariable>(GV) && TM.getDataSections())))
  1228. CannotUsePrivateLabel = true;
  1229. getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
  1230. }
  1231. MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable(
  1232. const Function &F, const TargetMachine &TM) const {
  1233. // If the function can be removed, produce a unique section so that
  1234. // the table doesn't prevent the removal.
  1235. const Comdat *C = F.getComdat();
  1236. bool EmitUniqueSection = TM.getFunctionSections() || C;
  1237. if (!EmitUniqueSection)
  1238. return ReadOnlySection;
  1239. // FIXME: we should produce a symbol for F instead.
  1240. if (F.hasPrivateLinkage())
  1241. return ReadOnlySection;
  1242. MCSymbol *Sym = TM.getSymbol(&F);
  1243. StringRef COMDATSymName = Sym->getName();
  1244. SectionKind Kind = SectionKind::getReadOnly();
  1245. StringRef SecName = getCOFFSectionNameForUniqueGlobal(Kind);
  1246. unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
  1247. Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
  1248. unsigned UniqueID = NextUniqueID++;
  1249. return getContext().getCOFFSection(
  1250. SecName, Characteristics, Kind, COMDATSymName,
  1251. COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
  1252. }
  1253. void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer,
  1254. Module &M) const {
  1255. if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
  1256. // Emit the linker options to the linker .drectve section. According to the
  1257. // spec, this section is a space-separated string containing flags for
  1258. // linker.
  1259. MCSection *Sec = getDrectveSection();
  1260. Streamer.SwitchSection(Sec);
  1261. for (const auto &Option : LinkerOptions->operands()) {
  1262. for (const auto &Piece : cast<MDNode>(Option)->operands()) {
  1263. // Lead with a space for consistency with our dllexport implementation.
  1264. std::string Directive(" ");
  1265. Directive.append(cast<MDString>(Piece)->getString());
  1266. Streamer.EmitBytes(Directive);
  1267. }
  1268. }
  1269. }
  1270. unsigned Version = 0;
  1271. unsigned Flags = 0;
  1272. StringRef Section;
  1273. GetObjCImageInfo(M, Version, Flags, Section);
  1274. if (Section.empty())
  1275. return;
  1276. auto &C = getContext();
  1277. auto *S = C.getCOFFSection(
  1278. Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
  1279. SectionKind::getReadOnly());
  1280. Streamer.SwitchSection(S);
  1281. Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO")));
  1282. Streamer.EmitIntValue(Version, 4);
  1283. Streamer.EmitIntValue(Flags, 4);
  1284. Streamer.AddBlankLine();
  1285. }
  1286. void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
  1287. const TargetMachine &TM) {
  1288. TargetLoweringObjectFile::Initialize(Ctx, TM);
  1289. const Triple &T = TM.getTargetTriple();
  1290. if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
  1291. StaticCtorSection =
  1292. Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  1293. COFF::IMAGE_SCN_MEM_READ,
  1294. SectionKind::getReadOnly());
  1295. StaticDtorSection =
  1296. Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  1297. COFF::IMAGE_SCN_MEM_READ,
  1298. SectionKind::getReadOnly());
  1299. } else {
  1300. StaticCtorSection = Ctx.getCOFFSection(
  1301. ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  1302. COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
  1303. SectionKind::getData());
  1304. StaticDtorSection = Ctx.getCOFFSection(
  1305. ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  1306. COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE,
  1307. SectionKind::getData());
  1308. }
  1309. }
  1310. static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx,
  1311. const Triple &T, bool IsCtor,
  1312. unsigned Priority,
  1313. const MCSymbol *KeySym,
  1314. MCSectionCOFF *Default) {
  1315. if (T.isWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) {
  1316. // If the priority is the default, use .CRT$XCU, possibly associative.
  1317. if (Priority == 65535)
  1318. return Ctx.getAssociativeCOFFSection(Default, KeySym, 0);
  1319. // Otherwise, we need to compute a new section name. Low priorities should
  1320. // run earlier. The linker will sort sections ASCII-betically, and we need a
  1321. // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we
  1322. // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really
  1323. // low priorities need to sort before 'L', since the CRT uses that
  1324. // internally, so we use ".CRT$XCA00001" for them.
  1325. SmallString<24> Name;
  1326. raw_svector_ostream OS(Name);
  1327. OS << ".CRT$XC" << (Priority < 200 ? 'A' : 'T') << format("%05u", Priority);
  1328. MCSectionCOFF *Sec = Ctx.getCOFFSection(
  1329. Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
  1330. SectionKind::getReadOnly());
  1331. return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0);
  1332. }
  1333. std::string Name = IsCtor ? ".ctors" : ".dtors";
  1334. if (Priority != 65535)
  1335. raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
  1336. return Ctx.getAssociativeCOFFSection(
  1337. Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  1338. COFF::IMAGE_SCN_MEM_READ |
  1339. COFF::IMAGE_SCN_MEM_WRITE,
  1340. SectionKind::getData()),
  1341. KeySym, 0);
  1342. }
  1343. MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
  1344. unsigned Priority, const MCSymbol *KeySym) const {
  1345. return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true,
  1346. Priority, KeySym,
  1347. cast<MCSectionCOFF>(StaticCtorSection));
  1348. }
  1349. MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
  1350. unsigned Priority, const MCSymbol *KeySym) const {
  1351. return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false,
  1352. Priority, KeySym,
  1353. cast<MCSectionCOFF>(StaticDtorSection));
  1354. }
  1355. void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(
  1356. raw_ostream &OS, const GlobalValue *GV) const {
  1357. emitLinkerFlagsForGlobalCOFF(OS, GV, getTargetTriple(), getMangler());
  1358. }
  1359. void TargetLoweringObjectFileCOFF::emitLinkerFlagsForUsed(
  1360. raw_ostream &OS, const GlobalValue *GV) const {
  1361. emitLinkerFlagsForUsedCOFF(OS, GV, getTargetTriple(), getMangler());
  1362. }
  1363. const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference(
  1364. const GlobalValue *LHS, const GlobalValue *RHS,
  1365. const TargetMachine &TM) const {
  1366. const Triple &T = TM.getTargetTriple();
  1367. if (T.isOSCygMing())
  1368. return nullptr;
  1369. // Our symbols should exist in address space zero, cowardly no-op if
  1370. // otherwise.
  1371. if (LHS->getType()->getPointerAddressSpace() != 0 ||
  1372. RHS->getType()->getPointerAddressSpace() != 0)
  1373. return nullptr;
  1374. // Both ptrtoint instructions must wrap global objects:
  1375. // - Only global variables are eligible for image relative relocations.
  1376. // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable.
  1377. // We expect __ImageBase to be a global variable without a section, externally
  1378. // defined.
  1379. //
  1380. // It should look something like this: @__ImageBase = external constant i8
  1381. if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) ||
  1382. LHS->isThreadLocal() || RHS->isThreadLocal() ||
  1383. RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() ||
  1384. cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection())
  1385. return nullptr;
  1386. return MCSymbolRefExpr::create(TM.getSymbol(LHS),
  1387. MCSymbolRefExpr::VK_COFF_IMGREL32,
  1388. getContext());
  1389. }
  1390. static std::string APIntToHexString(const APInt &AI) {
  1391. unsigned Width = (AI.getBitWidth() / 8) * 2;
  1392. std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true);
  1393. unsigned Size = HexString.size();
  1394. assert(Width >= Size && "hex string is too large!");
  1395. HexString.insert(HexString.begin(), Width - Size, '0');
  1396. return HexString;
  1397. }
  1398. static std::string scalarConstantToHexString(const Constant *C) {
  1399. Type *Ty = C->getType();
  1400. if (isa<UndefValue>(C)) {
  1401. return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits()));
  1402. } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
  1403. return APIntToHexString(CFP->getValueAPF().bitcastToAPInt());
  1404. } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
  1405. return APIntToHexString(CI->getValue());
  1406. } else {
  1407. unsigned NumElements;
  1408. if (isa<VectorType>(Ty))
  1409. NumElements = Ty->getVectorNumElements();
  1410. else
  1411. NumElements = Ty->getArrayNumElements();
  1412. std::string HexString;
  1413. for (int I = NumElements - 1, E = -1; I != E; --I)
  1414. HexString += scalarConstantToHexString(C->getAggregateElement(I));
  1415. return HexString;
  1416. }
  1417. }
  1418. MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant(
  1419. const DataLayout &DL, SectionKind Kind, const Constant *C,
  1420. unsigned &Align) const {
  1421. if (Kind.isMergeableConst() && C &&
  1422. getContext().getAsmInfo()->hasCOFFComdatConstants()) {
  1423. // This creates comdat sections with the given symbol name, but unless
  1424. // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol
  1425. // will be created with a null storage class, which makes GNU binutils
  1426. // error out.
  1427. const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  1428. COFF::IMAGE_SCN_MEM_READ |
  1429. COFF::IMAGE_SCN_LNK_COMDAT;
  1430. std::string COMDATSymName;
  1431. if (Kind.isMergeableConst4()) {
  1432. if (Align <= 4) {
  1433. COMDATSymName = "__real@" + scalarConstantToHexString(C);
  1434. Align = 4;
  1435. }
  1436. } else if (Kind.isMergeableConst8()) {
  1437. if (Align <= 8) {
  1438. COMDATSymName = "__real@" + scalarConstantToHexString(C);
  1439. Align = 8;
  1440. }
  1441. } else if (Kind.isMergeableConst16()) {
  1442. // FIXME: These may not be appropriate for non-x86 architectures.
  1443. if (Align <= 16) {
  1444. COMDATSymName = "__xmm@" + scalarConstantToHexString(C);
  1445. Align = 16;
  1446. }
  1447. } else if (Kind.isMergeableConst32()) {
  1448. if (Align <= 32) {
  1449. COMDATSymName = "__ymm@" + scalarConstantToHexString(C);
  1450. Align = 32;
  1451. }
  1452. }
  1453. if (!COMDATSymName.empty())
  1454. return getContext().getCOFFSection(".rdata", Characteristics, Kind,
  1455. COMDATSymName,
  1456. COFF::IMAGE_COMDAT_SELECT_ANY);
  1457. }
  1458. return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Align);
  1459. }
  1460. //===----------------------------------------------------------------------===//
  1461. // Wasm
  1462. //===----------------------------------------------------------------------===//
  1463. static const Comdat *getWasmComdat(const GlobalValue *GV) {
  1464. const Comdat *C = GV->getComdat();
  1465. if (!C)
  1466. return nullptr;
  1467. if (C->getSelectionKind() != Comdat::Any)
  1468. report_fatal_error("WebAssembly COMDATs only support "
  1469. "SelectionKind::Any, '" + C->getName() + "' cannot be "
  1470. "lowered.");
  1471. return C;
  1472. }
  1473. static SectionKind getWasmKindForNamedSection(StringRef Name, SectionKind K) {
  1474. // If we're told we have function data, then use that.
  1475. if (K.isText())
  1476. return SectionKind::getText();
  1477. // Otherwise, ignore whatever section type the generic impl detected and use
  1478. // a plain data section.
  1479. return SectionKind::getData();
  1480. }
  1481. MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal(
  1482. const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
  1483. // We don't support explict section names for functions in the wasm object
  1484. // format. Each function has to be in its own unique section.
  1485. if (isa<Function>(GO)) {
  1486. return SelectSectionForGlobal(GO, Kind, TM);
  1487. }
  1488. StringRef Name = GO->getSection();
  1489. Kind = getWasmKindForNamedSection(Name, Kind);
  1490. StringRef Group = "";
  1491. if (const Comdat *C = getWasmComdat(GO)) {
  1492. Group = C->getName();
  1493. }
  1494. MCSectionWasm* Section =
  1495. getContext().getWasmSection(Name, Kind, Group,
  1496. MCContext::GenericSectionID);
  1497. return Section;
  1498. }
  1499. static MCSectionWasm *selectWasmSectionForGlobal(
  1500. MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang,
  1501. const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) {
  1502. StringRef Group = "";
  1503. if (const Comdat *C = getWasmComdat(GO)) {
  1504. Group = C->getName();
  1505. }
  1506. bool UniqueSectionNames = TM.getUniqueSectionNames();
  1507. SmallString<128> Name = getSectionPrefixForGlobal(Kind);
  1508. if (const auto *F = dyn_cast<Function>(GO)) {
  1509. const auto &OptionalPrefix = F->getSectionPrefix();
  1510. if (OptionalPrefix)
  1511. Name += *OptionalPrefix;
  1512. }
  1513. if (EmitUniqueSection && UniqueSectionNames) {
  1514. Name.push_back('.');
  1515. TM.getNameWithPrefix(Name, GO, Mang, true);
  1516. }
  1517. unsigned UniqueID = MCContext::GenericSectionID;
  1518. if (EmitUniqueSection && !UniqueSectionNames) {
  1519. UniqueID = *NextUniqueID;
  1520. (*NextUniqueID)++;
  1521. }
  1522. return Ctx.getWasmSection(Name, Kind, Group, UniqueID);
  1523. }
  1524. MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal(
  1525. const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
  1526. if (Kind.isCommon())
  1527. report_fatal_error("mergable sections not supported yet on wasm");
  1528. // If we have -ffunction-section or -fdata-section then we should emit the
  1529. // global value to a uniqued section specifically for it.
  1530. bool EmitUniqueSection = false;
  1531. if (Kind.isText())
  1532. EmitUniqueSection = TM.getFunctionSections();
  1533. else
  1534. EmitUniqueSection = TM.getDataSections();
  1535. EmitUniqueSection |= GO->hasComdat();
  1536. return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM,
  1537. EmitUniqueSection, &NextUniqueID);
  1538. }
  1539. bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection(
  1540. bool UsesLabelDifference, const Function &F) const {
  1541. // We can always create relative relocations, so use another section
  1542. // that can be marked non-executable.
  1543. return false;
  1544. }
  1545. const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference(
  1546. const GlobalValue *LHS, const GlobalValue *RHS,
  1547. const TargetMachine &TM) const {
  1548. // We may only use a PLT-relative relocation to refer to unnamed_addr
  1549. // functions.
  1550. if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy())
  1551. return nullptr;
  1552. // Basic sanity checks.
  1553. if (LHS->getType()->getPointerAddressSpace() != 0 ||
  1554. RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() ||
  1555. RHS->isThreadLocal())
  1556. return nullptr;
  1557. return MCBinaryExpr::createSub(
  1558. MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None,
  1559. getContext()),
  1560. MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
  1561. }
  1562. void TargetLoweringObjectFileWasm::InitializeWasm() {
  1563. StaticCtorSection =
  1564. getContext().getWasmSection(".init_array", SectionKind::getData());
  1565. // We don't use PersonalityEncoding and LSDAEncoding because we don't emit
  1566. // .cfi directives. We use TTypeEncoding to encode typeinfo global variables.
  1567. TTypeEncoding = dwarf::DW_EH_PE_absptr;
  1568. }
  1569. MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection(
  1570. unsigned Priority, const MCSymbol *KeySym) const {
  1571. return Priority == UINT16_MAX ?
  1572. StaticCtorSection :
  1573. getContext().getWasmSection(".init_array." + utostr(Priority),
  1574. SectionKind::getData());
  1575. }
  1576. MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection(
  1577. unsigned Priority, const MCSymbol *KeySym) const {
  1578. llvm_unreachable("@llvm.global_dtors should have been lowered already");
  1579. return nullptr;
  1580. }