TargetLoweringObjectFileImpl.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769
  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. static unsigned getEntrySizeForKind(SectionKind Kind) {
  450. if (Kind.isMergeable1ByteCString())
  451. return 1;
  452. else if (Kind.isMergeable2ByteCString())
  453. return 2;
  454. else if (Kind.isMergeable4ByteCString())
  455. return 4;
  456. else if (Kind.isMergeableConst4())
  457. return 4;
  458. else if (Kind.isMergeableConst8())
  459. return 8;
  460. else if (Kind.isMergeableConst16())
  461. return 16;
  462. else if (Kind.isMergeableConst32())
  463. return 32;
  464. else {
  465. // We shouldn't have mergeable C strings or mergeable constants that we
  466. // didn't handle above.
  467. assert(!Kind.isMergeableCString() && "unknown string width");
  468. assert(!Kind.isMergeableConst() && "unknown data width");
  469. return 0;
  470. }
  471. }
  472. MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
  473. const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
  474. StringRef SectionName = GO->getSection();
  475. // Check if '#pragma clang section' name is applicable.
  476. // Note that pragma directive overrides -ffunction-section, -fdata-section
  477. // and so section name is exactly as user specified and not uniqued.
  478. const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO);
  479. if (GV && GV->hasImplicitSection()) {
  480. auto Attrs = GV->getAttributes();
  481. if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) {
  482. SectionName = Attrs.getAttribute("bss-section").getValueAsString();
  483. } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) {
  484. SectionName = Attrs.getAttribute("rodata-section").getValueAsString();
  485. } else if (Attrs.hasAttribute("data-section") && Kind.isData()) {
  486. SectionName = Attrs.getAttribute("data-section").getValueAsString();
  487. }
  488. }
  489. const Function *F = dyn_cast<Function>(GO);
  490. if (F && F->hasFnAttribute("implicit-section-name")) {
  491. SectionName = F->getFnAttribute("implicit-section-name").getValueAsString();
  492. }
  493. // Infer section flags from the section name if we can.
  494. Kind = getELFKindForNamedSection(SectionName, Kind);
  495. StringRef Group = "";
  496. unsigned Flags = getELFSectionFlags(Kind);
  497. if (const Comdat *C = getELFComdat(GO)) {
  498. Group = C->getName();
  499. Flags |= ELF::SHF_GROUP;
  500. }
  501. // A section can have at most one associated section. Put each global with
  502. // MD_associated in a unique section.
  503. unsigned UniqueID = MCContext::GenericSectionID;
  504. const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM);
  505. if (AssociatedSymbol) {
  506. UniqueID = NextUniqueID++;
  507. Flags |= ELF::SHF_LINK_ORDER;
  508. }
  509. MCSectionELF *Section = getContext().getELFSection(
  510. SectionName, getELFSectionType(SectionName, Kind), Flags,
  511. getEntrySizeForKind(Kind), Group, UniqueID, AssociatedSymbol);
  512. // Make sure that we did not get some other section with incompatible sh_link.
  513. // This should not be possible due to UniqueID code above.
  514. assert(Section->getAssociatedSymbol() == AssociatedSymbol &&
  515. "Associated symbol mismatch between sections");
  516. return Section;
  517. }
  518. /// Return the section prefix name used by options FunctionsSections and
  519. /// DataSections.
  520. static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
  521. if (Kind.isText())
  522. return ".text";
  523. if (Kind.isReadOnly())
  524. return ".rodata";
  525. if (Kind.isBSS())
  526. return ".bss";
  527. if (Kind.isThreadData())
  528. return ".tdata";
  529. if (Kind.isThreadBSS())
  530. return ".tbss";
  531. if (Kind.isData())
  532. return ".data";
  533. assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
  534. return ".data.rel.ro";
  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. // We don't use PersonalityEncoding and LSDAEncoding because we don't emit
  1522. // .cfi directives. We use TTypeEncoding to encode typeinfo global variables.
  1523. TTypeEncoding = dwarf::DW_EH_PE_absptr;
  1524. }
  1525. MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection(
  1526. unsigned Priority, const MCSymbol *KeySym) const {
  1527. return Priority == UINT16_MAX ?
  1528. StaticCtorSection :
  1529. getContext().getWasmSection(".init_array." + utostr(Priority),
  1530. SectionKind::getData());
  1531. }
  1532. MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection(
  1533. unsigned Priority, const MCSymbol *KeySym) const {
  1534. llvm_unreachable("@llvm.global_dtors should have been lowered already");
  1535. return nullptr;
  1536. }