TargetLoweringObjectFileImpl.cpp 64 KB

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