LinkModules.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  1. //===- lib/Linker/LinkModules.cpp - Module Linker Implementation ----------===//
  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 the LLVM module linker.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Linker/Linker.h"
  14. #include "llvm-c/Linker.h"
  15. #include "llvm/ADT/Hashing.h"
  16. #include "llvm/ADT/Optional.h"
  17. #include "llvm/ADT/SetVector.h"
  18. #include "llvm/ADT/SmallString.h"
  19. #include "llvm/ADT/Statistic.h"
  20. #include "llvm/ADT/Triple.h"
  21. #include "llvm/IR/Constants.h"
  22. #include "llvm/IR/DebugInfo.h"
  23. #include "llvm/IR/DiagnosticInfo.h"
  24. #include "llvm/IR/DiagnosticPrinter.h"
  25. #include "llvm/IR/LLVMContext.h"
  26. #include "llvm/IR/Module.h"
  27. #include "llvm/IR/TypeFinder.h"
  28. #include "llvm/Support/CommandLine.h"
  29. #include "llvm/Support/Debug.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. #include "llvm/Transforms/Utils/Cloning.h"
  32. #include <cctype>
  33. #include <tuple>
  34. using namespace llvm;
  35. //===----------------------------------------------------------------------===//
  36. // TypeMap implementation.
  37. //===----------------------------------------------------------------------===//
  38. namespace {
  39. class TypeMapTy : public ValueMapTypeRemapper {
  40. /// This is a mapping from a source type to a destination type to use.
  41. DenseMap<Type*, Type*> MappedTypes;
  42. /// When checking to see if two subgraphs are isomorphic, we speculatively
  43. /// add types to MappedTypes, but keep track of them here in case we need to
  44. /// roll back.
  45. SmallVector<Type*, 16> SpeculativeTypes;
  46. SmallVector<StructType*, 16> SpeculativeDstOpaqueTypes;
  47. /// This is a list of non-opaque structs in the source module that are mapped
  48. /// to an opaque struct in the destination module.
  49. SmallVector<StructType*, 16> SrcDefinitionsToResolve;
  50. /// This is the set of opaque types in the destination modules who are
  51. /// getting a body from the source module.
  52. SmallPtrSet<StructType*, 16> DstResolvedOpaqueTypes;
  53. public:
  54. TypeMapTy(Linker::IdentifiedStructTypeSet &DstStructTypesSet)
  55. : DstStructTypesSet(DstStructTypesSet) {}
  56. Linker::IdentifiedStructTypeSet &DstStructTypesSet;
  57. /// Indicate that the specified type in the destination module is conceptually
  58. /// equivalent to the specified type in the source module.
  59. void addTypeMapping(Type *DstTy, Type *SrcTy);
  60. /// Produce a body for an opaque type in the dest module from a type
  61. /// definition in the source module.
  62. void linkDefinedTypeBodies();
  63. /// Return the mapped type to use for the specified input type from the
  64. /// source module.
  65. Type *get(Type *SrcTy);
  66. Type *get(Type *SrcTy, SmallPtrSet<StructType *, 8> &Visited);
  67. void finishType(StructType *DTy, StructType *STy, ArrayRef<Type *> ETypes);
  68. FunctionType *get(FunctionType *T) {
  69. return cast<FunctionType>(get((Type *)T));
  70. }
  71. /// Dump out the type map for debugging purposes.
  72. void dump() const {
  73. for (auto &Pair : MappedTypes) {
  74. dbgs() << "TypeMap: ";
  75. Pair.first->print(dbgs());
  76. dbgs() << " => ";
  77. Pair.second->print(dbgs());
  78. dbgs() << '\n';
  79. }
  80. }
  81. private:
  82. Type *remapType(Type *SrcTy) override { return get(SrcTy); }
  83. bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
  84. };
  85. }
  86. void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
  87. assert(SpeculativeTypes.empty());
  88. assert(SpeculativeDstOpaqueTypes.empty());
  89. // Check to see if these types are recursively isomorphic and establish a
  90. // mapping between them if so.
  91. if (!areTypesIsomorphic(DstTy, SrcTy)) {
  92. // Oops, they aren't isomorphic. Just discard this request by rolling out
  93. // any speculative mappings we've established.
  94. for (Type *Ty : SpeculativeTypes)
  95. MappedTypes.erase(Ty);
  96. SrcDefinitionsToResolve.resize(SrcDefinitionsToResolve.size() -
  97. SpeculativeDstOpaqueTypes.size());
  98. for (StructType *Ty : SpeculativeDstOpaqueTypes)
  99. DstResolvedOpaqueTypes.erase(Ty);
  100. } else {
  101. for (Type *Ty : SpeculativeTypes)
  102. if (auto *STy = dyn_cast<StructType>(Ty))
  103. if (STy->hasName())
  104. STy->setName("");
  105. }
  106. SpeculativeTypes.clear();
  107. SpeculativeDstOpaqueTypes.clear();
  108. }
  109. /// Recursively walk this pair of types, returning true if they are isomorphic,
  110. /// false if they are not.
  111. bool TypeMapTy::areTypesIsomorphic(Type *DstTy, Type *SrcTy) {
  112. // Two types with differing kinds are clearly not isomorphic.
  113. if (DstTy->getTypeID() != SrcTy->getTypeID())
  114. return false;
  115. // If we have an entry in the MappedTypes table, then we have our answer.
  116. Type *&Entry = MappedTypes[SrcTy];
  117. if (Entry)
  118. return Entry == DstTy;
  119. // Two identical types are clearly isomorphic. Remember this
  120. // non-speculatively.
  121. if (DstTy == SrcTy) {
  122. Entry = DstTy;
  123. return true;
  124. }
  125. // Okay, we have two types with identical kinds that we haven't seen before.
  126. // If this is an opaque struct type, special case it.
  127. if (StructType *SSTy = dyn_cast<StructType>(SrcTy)) {
  128. // Mapping an opaque type to any struct, just keep the dest struct.
  129. if (SSTy->isOpaque()) {
  130. Entry = DstTy;
  131. SpeculativeTypes.push_back(SrcTy);
  132. return true;
  133. }
  134. // Mapping a non-opaque source type to an opaque dest. If this is the first
  135. // type that we're mapping onto this destination type then we succeed. Keep
  136. // the dest, but fill it in later. If this is the second (different) type
  137. // that we're trying to map onto the same opaque type then we fail.
  138. if (cast<StructType>(DstTy)->isOpaque()) {
  139. // We can only map one source type onto the opaque destination type.
  140. if (!DstResolvedOpaqueTypes.insert(cast<StructType>(DstTy)).second)
  141. return false;
  142. SrcDefinitionsToResolve.push_back(SSTy);
  143. SpeculativeTypes.push_back(SrcTy);
  144. SpeculativeDstOpaqueTypes.push_back(cast<StructType>(DstTy));
  145. Entry = DstTy;
  146. return true;
  147. }
  148. }
  149. // If the number of subtypes disagree between the two types, then we fail.
  150. if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes())
  151. return false;
  152. // Fail if any of the extra properties (e.g. array size) of the type disagree.
  153. if (isa<IntegerType>(DstTy))
  154. return false; // bitwidth disagrees.
  155. if (PointerType *PT = dyn_cast<PointerType>(DstTy)) {
  156. if (PT->getAddressSpace() != cast<PointerType>(SrcTy)->getAddressSpace())
  157. return false;
  158. } else if (FunctionType *FT = dyn_cast<FunctionType>(DstTy)) {
  159. if (FT->isVarArg() != cast<FunctionType>(SrcTy)->isVarArg())
  160. return false;
  161. } else if (StructType *DSTy = dyn_cast<StructType>(DstTy)) {
  162. StructType *SSTy = cast<StructType>(SrcTy);
  163. if (DSTy->isLiteral() != SSTy->isLiteral() ||
  164. DSTy->isPacked() != SSTy->isPacked())
  165. return false;
  166. } else if (ArrayType *DATy = dyn_cast<ArrayType>(DstTy)) {
  167. if (DATy->getNumElements() != cast<ArrayType>(SrcTy)->getNumElements())
  168. return false;
  169. } else if (VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
  170. if (DVTy->getNumElements() != cast<VectorType>(SrcTy)->getNumElements())
  171. return false;
  172. }
  173. // Otherwise, we speculate that these two types will line up and recursively
  174. // check the subelements.
  175. Entry = DstTy;
  176. SpeculativeTypes.push_back(SrcTy);
  177. for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I)
  178. if (!areTypesIsomorphic(DstTy->getContainedType(I),
  179. SrcTy->getContainedType(I)))
  180. return false;
  181. // If everything seems to have lined up, then everything is great.
  182. return true;
  183. }
  184. void TypeMapTy::linkDefinedTypeBodies() {
  185. SmallVector<Type*, 16> Elements;
  186. for (StructType *SrcSTy : SrcDefinitionsToResolve) {
  187. StructType *DstSTy = cast<StructType>(MappedTypes[SrcSTy]);
  188. assert(DstSTy->isOpaque());
  189. // Map the body of the source type over to a new body for the dest type.
  190. Elements.resize(SrcSTy->getNumElements());
  191. for (unsigned I = 0, E = Elements.size(); I != E; ++I)
  192. Elements[I] = get(SrcSTy->getElementType(I));
  193. DstSTy->setBody(Elements, SrcSTy->isPacked());
  194. DstStructTypesSet.switchToNonOpaque(DstSTy);
  195. }
  196. SrcDefinitionsToResolve.clear();
  197. DstResolvedOpaqueTypes.clear();
  198. }
  199. void TypeMapTy::finishType(StructType *DTy, StructType *STy,
  200. ArrayRef<Type *> ETypes) {
  201. DTy->setBody(ETypes, STy->isPacked());
  202. // Steal STy's name.
  203. if (STy->hasName()) {
  204. SmallString<16> TmpName = STy->getName();
  205. STy->setName("");
  206. DTy->setName(TmpName);
  207. }
  208. DstStructTypesSet.addNonOpaque(DTy);
  209. }
  210. Type *TypeMapTy::get(Type *Ty) {
  211. SmallPtrSet<StructType *, 8> Visited;
  212. return get(Ty, Visited);
  213. }
  214. Type *TypeMapTy::get(Type *Ty, SmallPtrSet<StructType *, 8> &Visited) {
  215. // If we already have an entry for this type, return it.
  216. Type **Entry = &MappedTypes[Ty];
  217. if (*Entry)
  218. return *Entry;
  219. // These are types that LLVM itself will unique.
  220. bool IsUniqued = !isa<StructType>(Ty) || cast<StructType>(Ty)->isLiteral();
  221. #ifndef NDEBUG
  222. if (!IsUniqued) {
  223. for (auto &Pair : MappedTypes) {
  224. assert(!(Pair.first != Ty && Pair.second == Ty) &&
  225. "mapping to a source type");
  226. }
  227. }
  228. #endif
  229. if (!IsUniqued && !Visited.insert(cast<StructType>(Ty)).second) {
  230. StructType *DTy = StructType::create(Ty->getContext());
  231. return *Entry = DTy;
  232. }
  233. // If this is not a recursive type, then just map all of the elements and
  234. // then rebuild the type from inside out.
  235. SmallVector<Type *, 4> ElementTypes;
  236. // If there are no element types to map, then the type is itself. This is
  237. // true for the anonymous {} struct, things like 'float', integers, etc.
  238. if (Ty->getNumContainedTypes() == 0 && IsUniqued)
  239. return *Entry = Ty;
  240. // Remap all of the elements, keeping track of whether any of them change.
  241. bool AnyChange = false;
  242. ElementTypes.resize(Ty->getNumContainedTypes());
  243. for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) {
  244. ElementTypes[I] = get(Ty->getContainedType(I), Visited);
  245. AnyChange |= ElementTypes[I] != Ty->getContainedType(I);
  246. }
  247. // If we found our type while recursively processing stuff, just use it.
  248. Entry = &MappedTypes[Ty];
  249. if (*Entry) {
  250. if (auto *DTy = dyn_cast<StructType>(*Entry)) {
  251. if (DTy->isOpaque()) {
  252. auto *STy = cast<StructType>(Ty);
  253. finishType(DTy, STy, ElementTypes);
  254. }
  255. }
  256. return *Entry;
  257. }
  258. // If all of the element types mapped directly over and the type is not
  259. // a nomed struct, then the type is usable as-is.
  260. if (!AnyChange && IsUniqued)
  261. return *Entry = Ty;
  262. // Otherwise, rebuild a modified type.
  263. switch (Ty->getTypeID()) {
  264. default:
  265. llvm_unreachable("unknown derived type to remap");
  266. case Type::ArrayTyID:
  267. return *Entry = ArrayType::get(ElementTypes[0],
  268. cast<ArrayType>(Ty)->getNumElements());
  269. case Type::VectorTyID:
  270. return *Entry = VectorType::get(ElementTypes[0],
  271. cast<VectorType>(Ty)->getNumElements());
  272. case Type::PointerTyID:
  273. return *Entry = PointerType::get(ElementTypes[0],
  274. cast<PointerType>(Ty)->getAddressSpace());
  275. case Type::FunctionTyID:
  276. return *Entry = FunctionType::get(ElementTypes[0],
  277. makeArrayRef(ElementTypes).slice(1),
  278. cast<FunctionType>(Ty)->isVarArg());
  279. case Type::StructTyID: {
  280. auto *STy = cast<StructType>(Ty);
  281. bool IsPacked = STy->isPacked();
  282. if (IsUniqued)
  283. return *Entry = StructType::get(Ty->getContext(), ElementTypes, IsPacked);
  284. // If the type is opaque, we can just use it directly.
  285. if (STy->isOpaque()) {
  286. DstStructTypesSet.addOpaque(STy);
  287. return *Entry = Ty;
  288. }
  289. if (StructType *OldT =
  290. DstStructTypesSet.findNonOpaque(ElementTypes, IsPacked)) {
  291. STy->setName("");
  292. return *Entry = OldT;
  293. }
  294. if (!AnyChange) {
  295. DstStructTypesSet.addNonOpaque(STy);
  296. return *Entry = Ty;
  297. }
  298. StructType *DTy = StructType::create(Ty->getContext());
  299. finishType(DTy, STy, ElementTypes);
  300. return *Entry = DTy;
  301. }
  302. }
  303. }
  304. //===----------------------------------------------------------------------===//
  305. // ModuleLinker implementation.
  306. //===----------------------------------------------------------------------===//
  307. namespace {
  308. class ModuleLinker;
  309. /// Creates prototypes for functions that are lazily linked on the fly. This
  310. /// speeds up linking for modules with many/ lazily linked functions of which
  311. /// few get used.
  312. class ValueMaterializerTy : public ValueMaterializer {
  313. TypeMapTy &TypeMap;
  314. Module *DstM;
  315. std::vector<GlobalValue *> &LazilyLinkGlobalValues;
  316. public:
  317. ValueMaterializerTy(TypeMapTy &TypeMap, Module *DstM,
  318. std::vector<GlobalValue *> &LazilyLinkGlobalValues)
  319. : ValueMaterializer(), TypeMap(TypeMap), DstM(DstM),
  320. LazilyLinkGlobalValues(LazilyLinkGlobalValues) {}
  321. Value *materializeValueFor(Value *V) override;
  322. };
  323. class LinkDiagnosticInfo : public DiagnosticInfo {
  324. const Twine &Msg;
  325. public:
  326. LinkDiagnosticInfo(DiagnosticSeverity Severity, const Twine &Msg);
  327. void print(DiagnosticPrinter &DP) const override;
  328. };
  329. LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity,
  330. const Twine &Msg)
  331. : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {}
  332. void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; }
  333. /// This is an implementation class for the LinkModules function, which is the
  334. /// entrypoint for this file.
  335. class ModuleLinker {
  336. Module *DstM, *SrcM;
  337. TypeMapTy TypeMap;
  338. ValueMaterializerTy ValMaterializer;
  339. /// Mapping of values from what they used to be in Src, to what they are now
  340. /// in DstM. ValueToValueMapTy is a ValueMap, which involves some overhead
  341. /// due to the use of Value handles which the Linker doesn't actually need,
  342. /// but this allows us to reuse the ValueMapper code.
  343. ValueToValueMapTy ValueMap;
  344. struct AppendingVarInfo {
  345. GlobalVariable *NewGV; // New aggregate global in dest module.
  346. const Constant *DstInit; // Old initializer from dest module.
  347. const Constant *SrcInit; // Old initializer from src module.
  348. };
  349. std::vector<AppendingVarInfo> AppendingVars;
  350. // Set of items not to link in from source.
  351. SmallPtrSet<const Value *, 16> DoNotLinkFromSource;
  352. // Vector of GlobalValues to lazily link in.
  353. std::vector<GlobalValue *> LazilyLinkGlobalValues;
  354. /// Functions that have replaced other functions.
  355. SmallPtrSet<const Function *, 16> OverridingFunctions;
  356. DiagnosticHandlerFunction DiagnosticHandler;
  357. /// For symbol clashes, prefer those from Src.
  358. bool OverrideFromSrc;
  359. public:
  360. ModuleLinker(Module *dstM, Linker::IdentifiedStructTypeSet &Set, Module *srcM,
  361. DiagnosticHandlerFunction DiagnosticHandler,
  362. bool OverrideFromSrc)
  363. : DstM(dstM), SrcM(srcM), TypeMap(Set),
  364. ValMaterializer(TypeMap, DstM, LazilyLinkGlobalValues),
  365. DiagnosticHandler(DiagnosticHandler), OverrideFromSrc(OverrideFromSrc) {
  366. }
  367. bool run();
  368. private:
  369. bool shouldLinkFromSource(bool &LinkFromSrc, const GlobalValue &Dest,
  370. const GlobalValue &Src);
  371. /// Helper method for setting a message and returning an error code.
  372. bool emitError(const Twine &Message) {
  373. DiagnosticHandler(LinkDiagnosticInfo(DS_Error, Message));
  374. return true;
  375. }
  376. void emitWarning(const Twine &Message) {
  377. DiagnosticHandler(LinkDiagnosticInfo(DS_Warning, Message));
  378. }
  379. bool getComdatLeader(Module *M, StringRef ComdatName,
  380. const GlobalVariable *&GVar);
  381. bool computeResultingSelectionKind(StringRef ComdatName,
  382. Comdat::SelectionKind Src,
  383. Comdat::SelectionKind Dst,
  384. Comdat::SelectionKind &Result,
  385. bool &LinkFromSrc);
  386. std::map<const Comdat *, std::pair<Comdat::SelectionKind, bool>>
  387. ComdatsChosen;
  388. bool getComdatResult(const Comdat *SrcC, Comdat::SelectionKind &SK,
  389. bool &LinkFromSrc);
  390. /// Given a global in the source module, return the global in the
  391. /// destination module that is being linked to, if any.
  392. GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) {
  393. // If the source has no name it can't link. If it has local linkage,
  394. // there is no name match-up going on.
  395. if (!SrcGV->hasName() || SrcGV->hasLocalLinkage())
  396. return nullptr;
  397. // Otherwise see if we have a match in the destination module's symtab.
  398. GlobalValue *DGV = DstM->getNamedValue(SrcGV->getName());
  399. if (!DGV)
  400. return nullptr;
  401. // If we found a global with the same name in the dest module, but it has
  402. // internal linkage, we are really not doing any linkage here.
  403. if (DGV->hasLocalLinkage())
  404. return nullptr;
  405. // Otherwise, we do in fact link to the destination global.
  406. return DGV;
  407. }
  408. void computeTypeMapping();
  409. void upgradeMismatchedGlobalArray(StringRef Name);
  410. void upgradeMismatchedGlobals();
  411. bool linkAppendingVarProto(GlobalVariable *DstGV,
  412. const GlobalVariable *SrcGV);
  413. bool linkGlobalValueProto(GlobalValue *GV);
  414. bool linkModuleFlagsMetadata();
  415. void linkAppendingVarInit(const AppendingVarInfo &AVI);
  416. void linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src);
  417. bool linkFunctionBody(Function &Dst, Function &Src);
  418. void linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src);
  419. bool linkGlobalValueBody(GlobalValue &Src);
  420. void linkNamedMDNodes();
  421. void stripReplacedSubprograms();
  422. };
  423. }
  424. /// The LLVM SymbolTable class autorenames globals that conflict in the symbol
  425. /// table. This is good for all clients except for us. Go through the trouble
  426. /// to force this back.
  427. static void forceRenaming(GlobalValue *GV, StringRef Name) {
  428. // If the global doesn't force its name or if it already has the right name,
  429. // there is nothing for us to do.
  430. if (GV->hasLocalLinkage() || GV->getName() == Name)
  431. return;
  432. Module *M = GV->getParent();
  433. // If there is a conflict, rename the conflict.
  434. if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
  435. GV->takeName(ConflictGV);
  436. ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
  437. assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
  438. } else {
  439. GV->setName(Name); // Force the name back
  440. }
  441. }
  442. /// copy additional attributes (those not needed to construct a GlobalValue)
  443. /// from the SrcGV to the DestGV.
  444. static void copyGVAttributes(GlobalValue *DestGV, const GlobalValue *SrcGV) {
  445. DestGV->copyAttributesFrom(SrcGV);
  446. forceRenaming(DestGV, SrcGV->getName());
  447. }
  448. static bool isLessConstraining(GlobalValue::VisibilityTypes a,
  449. GlobalValue::VisibilityTypes b) {
  450. if (a == GlobalValue::HiddenVisibility)
  451. return false;
  452. if (b == GlobalValue::HiddenVisibility)
  453. return true;
  454. if (a == GlobalValue::ProtectedVisibility)
  455. return false;
  456. if (b == GlobalValue::ProtectedVisibility)
  457. return true;
  458. return false;
  459. }
  460. /// Loop through the global variables in the src module and merge them into the
  461. /// dest module.
  462. static GlobalVariable *copyGlobalVariableProto(TypeMapTy &TypeMap, Module &DstM,
  463. const GlobalVariable *SGVar) {
  464. // No linking to be performed or linking from the source: simply create an
  465. // identical version of the symbol over in the dest module... the
  466. // initializer will be filled in later by LinkGlobalInits.
  467. GlobalVariable *NewDGV = new GlobalVariable(
  468. DstM, TypeMap.get(SGVar->getType()->getElementType()),
  469. SGVar->isConstant(), SGVar->getLinkage(), /*init*/ nullptr,
  470. SGVar->getName(), /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
  471. SGVar->getType()->getAddressSpace());
  472. return NewDGV;
  473. }
  474. /// Link the function in the source module into the destination module if
  475. /// needed, setting up mapping information.
  476. static Function *copyFunctionProto(TypeMapTy &TypeMap, Module &DstM,
  477. const Function *SF) {
  478. // If there is no linkage to be performed or we are linking from the source,
  479. // bring SF over.
  480. return Function::Create(TypeMap.get(SF->getFunctionType()), SF->getLinkage(),
  481. SF->getName(), &DstM);
  482. }
  483. /// Set up prototypes for any aliases that come over from the source module.
  484. static GlobalAlias *copyGlobalAliasProto(TypeMapTy &TypeMap, Module &DstM,
  485. const GlobalAlias *SGA) {
  486. // If there is no linkage to be performed or we're linking from the source,
  487. // bring over SGA.
  488. auto *PTy = cast<PointerType>(TypeMap.get(SGA->getType()));
  489. return GlobalAlias::create(PTy, SGA->getLinkage(), SGA->getName(), &DstM);
  490. }
  491. static GlobalValue *copyGlobalValueProto(TypeMapTy &TypeMap, Module &DstM,
  492. const GlobalValue *SGV) {
  493. GlobalValue *NewGV;
  494. if (auto *SGVar = dyn_cast<GlobalVariable>(SGV))
  495. NewGV = copyGlobalVariableProto(TypeMap, DstM, SGVar);
  496. else if (auto *SF = dyn_cast<Function>(SGV))
  497. NewGV = copyFunctionProto(TypeMap, DstM, SF);
  498. else
  499. NewGV = copyGlobalAliasProto(TypeMap, DstM, cast<GlobalAlias>(SGV));
  500. copyGVAttributes(NewGV, SGV);
  501. return NewGV;
  502. }
  503. Value *ValueMaterializerTy::materializeValueFor(Value *V) {
  504. auto *SGV = dyn_cast<GlobalValue>(V);
  505. if (!SGV)
  506. return nullptr;
  507. GlobalValue *DGV = copyGlobalValueProto(TypeMap, *DstM, SGV);
  508. if (Comdat *SC = SGV->getComdat()) {
  509. if (auto *DGO = dyn_cast<GlobalObject>(DGV)) {
  510. Comdat *DC = DstM->getOrInsertComdat(SC->getName());
  511. DGO->setComdat(DC);
  512. }
  513. }
  514. LazilyLinkGlobalValues.push_back(SGV);
  515. return DGV;
  516. }
  517. bool ModuleLinker::getComdatLeader(Module *M, StringRef ComdatName,
  518. const GlobalVariable *&GVar) {
  519. const GlobalValue *GVal = M->getNamedValue(ComdatName);
  520. if (const auto *GA = dyn_cast_or_null<GlobalAlias>(GVal)) {
  521. GVal = GA->getBaseObject();
  522. if (!GVal)
  523. // We cannot resolve the size of the aliasee yet.
  524. return emitError("Linking COMDATs named '" + ComdatName +
  525. "': COMDAT key involves incomputable alias size.");
  526. }
  527. GVar = dyn_cast_or_null<GlobalVariable>(GVal);
  528. if (!GVar)
  529. return emitError(
  530. "Linking COMDATs named '" + ComdatName +
  531. "': GlobalVariable required for data dependent selection!");
  532. return false;
  533. }
  534. bool ModuleLinker::computeResultingSelectionKind(StringRef ComdatName,
  535. Comdat::SelectionKind Src,
  536. Comdat::SelectionKind Dst,
  537. Comdat::SelectionKind &Result,
  538. bool &LinkFromSrc) {
  539. // The ability to mix Comdat::SelectionKind::Any with
  540. // Comdat::SelectionKind::Largest is a behavior that comes from COFF.
  541. bool DstAnyOrLargest = Dst == Comdat::SelectionKind::Any ||
  542. Dst == Comdat::SelectionKind::Largest;
  543. bool SrcAnyOrLargest = Src == Comdat::SelectionKind::Any ||
  544. Src == Comdat::SelectionKind::Largest;
  545. if (DstAnyOrLargest && SrcAnyOrLargest) {
  546. if (Dst == Comdat::SelectionKind::Largest ||
  547. Src == Comdat::SelectionKind::Largest)
  548. Result = Comdat::SelectionKind::Largest;
  549. else
  550. Result = Comdat::SelectionKind::Any;
  551. } else if (Src == Dst) {
  552. Result = Dst;
  553. } else {
  554. return emitError("Linking COMDATs named '" + ComdatName +
  555. "': invalid selection kinds!");
  556. }
  557. switch (Result) {
  558. case Comdat::SelectionKind::Any:
  559. // Go with Dst.
  560. LinkFromSrc = false;
  561. break;
  562. case Comdat::SelectionKind::NoDuplicates:
  563. return emitError("Linking COMDATs named '" + ComdatName +
  564. "': noduplicates has been violated!");
  565. case Comdat::SelectionKind::ExactMatch:
  566. case Comdat::SelectionKind::Largest:
  567. case Comdat::SelectionKind::SameSize: {
  568. const GlobalVariable *DstGV;
  569. const GlobalVariable *SrcGV;
  570. if (getComdatLeader(DstM, ComdatName, DstGV) ||
  571. getComdatLeader(SrcM, ComdatName, SrcGV))
  572. return true;
  573. const DataLayout &DstDL = DstM->getDataLayout();
  574. const DataLayout &SrcDL = SrcM->getDataLayout();
  575. uint64_t DstSize =
  576. DstDL.getTypeAllocSize(DstGV->getType()->getPointerElementType());
  577. uint64_t SrcSize =
  578. SrcDL.getTypeAllocSize(SrcGV->getType()->getPointerElementType());
  579. if (Result == Comdat::SelectionKind::ExactMatch) {
  580. if (SrcGV->getInitializer() != DstGV->getInitializer())
  581. return emitError("Linking COMDATs named '" + ComdatName +
  582. "': ExactMatch violated!");
  583. LinkFromSrc = false;
  584. } else if (Result == Comdat::SelectionKind::Largest) {
  585. LinkFromSrc = SrcSize > DstSize;
  586. } else if (Result == Comdat::SelectionKind::SameSize) {
  587. if (SrcSize != DstSize)
  588. return emitError("Linking COMDATs named '" + ComdatName +
  589. "': SameSize violated!");
  590. LinkFromSrc = false;
  591. } else {
  592. llvm_unreachable("unknown selection kind");
  593. }
  594. break;
  595. }
  596. }
  597. return false;
  598. }
  599. bool ModuleLinker::getComdatResult(const Comdat *SrcC,
  600. Comdat::SelectionKind &Result,
  601. bool &LinkFromSrc) {
  602. Comdat::SelectionKind SSK = SrcC->getSelectionKind();
  603. StringRef ComdatName = SrcC->getName();
  604. Module::ComdatSymTabType &ComdatSymTab = DstM->getComdatSymbolTable();
  605. Module::ComdatSymTabType::iterator DstCI = ComdatSymTab.find(ComdatName);
  606. if (DstCI == ComdatSymTab.end()) {
  607. // Use the comdat if it is only available in one of the modules.
  608. LinkFromSrc = true;
  609. Result = SSK;
  610. return false;
  611. }
  612. const Comdat *DstC = &DstCI->second;
  613. Comdat::SelectionKind DSK = DstC->getSelectionKind();
  614. return computeResultingSelectionKind(ComdatName, SSK, DSK, Result,
  615. LinkFromSrc);
  616. }
  617. bool ModuleLinker::shouldLinkFromSource(bool &LinkFromSrc,
  618. const GlobalValue &Dest,
  619. const GlobalValue &Src) {
  620. // Should we unconditionally use the Src?
  621. if (OverrideFromSrc) {
  622. LinkFromSrc = true;
  623. return false;
  624. }
  625. // We always have to add Src if it has appending linkage.
  626. if (Src.hasAppendingLinkage()) {
  627. LinkFromSrc = true;
  628. return false;
  629. }
  630. bool SrcIsDeclaration = Src.isDeclarationForLinker();
  631. bool DestIsDeclaration = Dest.isDeclarationForLinker();
  632. if (SrcIsDeclaration) {
  633. // If Src is external or if both Src & Dest are external.. Just link the
  634. // external globals, we aren't adding anything.
  635. if (Src.hasDLLImportStorageClass()) {
  636. // If one of GVs is marked as DLLImport, result should be dllimport'ed.
  637. LinkFromSrc = DestIsDeclaration;
  638. return false;
  639. }
  640. // If the Dest is weak, use the source linkage.
  641. LinkFromSrc = Dest.hasExternalWeakLinkage();
  642. return false;
  643. }
  644. if (DestIsDeclaration) {
  645. // If Dest is external but Src is not:
  646. LinkFromSrc = true;
  647. return false;
  648. }
  649. if (Src.hasCommonLinkage()) {
  650. if (Dest.hasLinkOnceLinkage() || Dest.hasWeakLinkage()) {
  651. LinkFromSrc = true;
  652. return false;
  653. }
  654. if (!Dest.hasCommonLinkage()) {
  655. LinkFromSrc = false;
  656. return false;
  657. }
  658. const DataLayout &DL = Dest.getParent()->getDataLayout();
  659. uint64_t DestSize = DL.getTypeAllocSize(Dest.getType()->getElementType());
  660. uint64_t SrcSize = DL.getTypeAllocSize(Src.getType()->getElementType());
  661. LinkFromSrc = SrcSize > DestSize;
  662. return false;
  663. }
  664. if (Src.isWeakForLinker()) {
  665. assert(!Dest.hasExternalWeakLinkage());
  666. assert(!Dest.hasAvailableExternallyLinkage());
  667. if (Dest.hasLinkOnceLinkage() && Src.hasWeakLinkage()) {
  668. LinkFromSrc = true;
  669. return false;
  670. }
  671. LinkFromSrc = false;
  672. return false;
  673. }
  674. if (Dest.isWeakForLinker()) {
  675. assert(Src.hasExternalLinkage());
  676. LinkFromSrc = true;
  677. return false;
  678. }
  679. assert(!Src.hasExternalWeakLinkage());
  680. assert(!Dest.hasExternalWeakLinkage());
  681. assert(Dest.hasExternalLinkage() && Src.hasExternalLinkage() &&
  682. "Unexpected linkage type!");
  683. return emitError("Linking globals named '" + Src.getName() +
  684. "': symbol multiply defined!");
  685. }
  686. /// Loop over all of the linked values to compute type mappings. For example,
  687. /// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct
  688. /// types 'Foo' but one got renamed when the module was loaded into the same
  689. /// LLVMContext.
  690. void ModuleLinker::computeTypeMapping() {
  691. for (GlobalValue &SGV : SrcM->globals()) {
  692. GlobalValue *DGV = getLinkedToGlobal(&SGV);
  693. if (!DGV)
  694. continue;
  695. if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {
  696. TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
  697. continue;
  698. }
  699. // Unify the element type of appending arrays.
  700. ArrayType *DAT = cast<ArrayType>(DGV->getType()->getElementType());
  701. ArrayType *SAT = cast<ArrayType>(SGV.getType()->getElementType());
  702. TypeMap.addTypeMapping(DAT->getElementType(), SAT->getElementType());
  703. }
  704. for (GlobalValue &SGV : *SrcM) {
  705. if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
  706. TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
  707. }
  708. for (GlobalValue &SGV : SrcM->aliases()) {
  709. if (GlobalValue *DGV = getLinkedToGlobal(&SGV))
  710. TypeMap.addTypeMapping(DGV->getType(), SGV.getType());
  711. }
  712. // Incorporate types by name, scanning all the types in the source module.
  713. // At this point, the destination module may have a type "%foo = { i32 }" for
  714. // example. When the source module got loaded into the same LLVMContext, if
  715. // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
  716. std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes();
  717. for (StructType *ST : Types) {
  718. if (!ST->hasName())
  719. continue;
  720. // Check to see if there is a dot in the name followed by a digit.
  721. size_t DotPos = ST->getName().rfind('.');
  722. if (DotPos == 0 || DotPos == StringRef::npos ||
  723. ST->getName().back() == '.' ||
  724. !isdigit(static_cast<unsigned char>(ST->getName()[DotPos + 1])))
  725. continue;
  726. // Check to see if the destination module has a struct with the prefix name.
  727. StructType *DST = DstM->getTypeByName(ST->getName().substr(0, DotPos));
  728. if (!DST)
  729. continue;
  730. // Don't use it if this actually came from the source module. They're in
  731. // the same LLVMContext after all. Also don't use it unless the type is
  732. // actually used in the destination module. This can happen in situations
  733. // like this:
  734. //
  735. // Module A Module B
  736. // -------- --------
  737. // %Z = type { %A } %B = type { %C.1 }
  738. // %A = type { %B.1, [7 x i8] } %C.1 = type { i8* }
  739. // %B.1 = type { %C } %A.2 = type { %B.3, [5 x i8] }
  740. // %C = type { i8* } %B.3 = type { %C.1 }
  741. //
  742. // When we link Module B with Module A, the '%B' in Module B is
  743. // used. However, that would then use '%C.1'. But when we process '%C.1',
  744. // we prefer to take the '%C' version. So we are then left with both
  745. // '%C.1' and '%C' being used for the same types. This leads to some
  746. // variables using one type and some using the other.
  747. if (TypeMap.DstStructTypesSet.hasType(DST))
  748. TypeMap.addTypeMapping(DST, ST);
  749. }
  750. // Now that we have discovered all of the type equivalences, get a body for
  751. // any 'opaque' types in the dest module that are now resolved.
  752. TypeMap.linkDefinedTypeBodies();
  753. }
  754. static void upgradeGlobalArray(GlobalVariable *GV) {
  755. ArrayType *ATy = cast<ArrayType>(GV->getType()->getElementType());
  756. StructType *OldTy = cast<StructType>(ATy->getElementType());
  757. assert(OldTy->getNumElements() == 2 && "Expected to upgrade from 2 elements");
  758. // Get the upgraded 3 element type.
  759. PointerType *VoidPtrTy = Type::getInt8Ty(GV->getContext())->getPointerTo();
  760. Type *Tys[3] = {OldTy->getElementType(0), OldTy->getElementType(1),
  761. VoidPtrTy};
  762. StructType *NewTy = StructType::get(GV->getContext(), Tys, false);
  763. // Build new constants with a null third field filled in.
  764. Constant *OldInitC = GV->getInitializer();
  765. ConstantArray *OldInit = dyn_cast<ConstantArray>(OldInitC);
  766. if (!OldInit && !isa<ConstantAggregateZero>(OldInitC))
  767. // Invalid initializer; give up.
  768. return;
  769. std::vector<Constant *> Initializers;
  770. if (OldInit && OldInit->getNumOperands()) {
  771. Value *Null = Constant::getNullValue(VoidPtrTy);
  772. for (Use &U : OldInit->operands()) {
  773. ConstantStruct *Init = cast<ConstantStruct>(U.get());
  774. Initializers.push_back(ConstantStruct::get(
  775. NewTy, Init->getOperand(0), Init->getOperand(1), Null, nullptr));
  776. }
  777. }
  778. assert(Initializers.size() == ATy->getNumElements() &&
  779. "Failed to copy all array elements");
  780. // Replace the old GV with a new one.
  781. ATy = ArrayType::get(NewTy, Initializers.size());
  782. Constant *NewInit = ConstantArray::get(ATy, Initializers);
  783. GlobalVariable *NewGV = new GlobalVariable(
  784. *GV->getParent(), ATy, GV->isConstant(), GV->getLinkage(), NewInit, "",
  785. GV, GV->getThreadLocalMode(), GV->getType()->getAddressSpace(),
  786. GV->isExternallyInitialized());
  787. NewGV->copyAttributesFrom(GV);
  788. NewGV->takeName(GV);
  789. assert(GV->use_empty() && "program cannot use initializer list");
  790. GV->eraseFromParent();
  791. }
  792. void ModuleLinker::upgradeMismatchedGlobalArray(StringRef Name) {
  793. // Look for the global arrays.
  794. auto *DstGV = dyn_cast_or_null<GlobalVariable>(DstM->getNamedValue(Name));
  795. if (!DstGV)
  796. return;
  797. auto *SrcGV = dyn_cast_or_null<GlobalVariable>(SrcM->getNamedValue(Name));
  798. if (!SrcGV)
  799. return;
  800. // Check if the types already match.
  801. auto *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
  802. auto *SrcTy =
  803. cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()));
  804. if (DstTy == SrcTy)
  805. return;
  806. // Grab the element types. We can only upgrade an array of a two-field
  807. // struct. Only bother if the other one has three-fields.
  808. auto *DstEltTy = cast<StructType>(DstTy->getElementType());
  809. auto *SrcEltTy = cast<StructType>(SrcTy->getElementType());
  810. if (DstEltTy->getNumElements() == 2 && SrcEltTy->getNumElements() == 3) {
  811. upgradeGlobalArray(DstGV);
  812. return;
  813. }
  814. if (DstEltTy->getNumElements() == 3 && SrcEltTy->getNumElements() == 2)
  815. upgradeGlobalArray(SrcGV);
  816. // We can't upgrade any other differences.
  817. }
  818. void ModuleLinker::upgradeMismatchedGlobals() {
  819. upgradeMismatchedGlobalArray("llvm.global_ctors");
  820. upgradeMismatchedGlobalArray("llvm.global_dtors");
  821. }
  822. /// If there were any appending global variables, link them together now.
  823. /// Return true on error.
  824. bool ModuleLinker::linkAppendingVarProto(GlobalVariable *DstGV,
  825. const GlobalVariable *SrcGV) {
  826. if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
  827. return emitError("Linking globals named '" + SrcGV->getName() +
  828. "': can only link appending global with another appending global!");
  829. ArrayType *DstTy = cast<ArrayType>(DstGV->getType()->getElementType());
  830. ArrayType *SrcTy =
  831. cast<ArrayType>(TypeMap.get(SrcGV->getType()->getElementType()));
  832. Type *EltTy = DstTy->getElementType();
  833. // Check to see that they two arrays agree on type.
  834. if (EltTy != SrcTy->getElementType())
  835. return emitError("Appending variables with different element types!");
  836. if (DstGV->isConstant() != SrcGV->isConstant())
  837. return emitError("Appending variables linked with different const'ness!");
  838. if (DstGV->getAlignment() != SrcGV->getAlignment())
  839. return emitError(
  840. "Appending variables with different alignment need to be linked!");
  841. if (DstGV->getVisibility() != SrcGV->getVisibility())
  842. return emitError(
  843. "Appending variables with different visibility need to be linked!");
  844. if (DstGV->hasUnnamedAddr() != SrcGV->hasUnnamedAddr())
  845. return emitError(
  846. "Appending variables with different unnamed_addr need to be linked!");
  847. if (StringRef(DstGV->getSection()) != SrcGV->getSection())
  848. return emitError(
  849. "Appending variables with different section name need to be linked!");
  850. uint64_t NewSize = DstTy->getNumElements() + SrcTy->getNumElements();
  851. ArrayType *NewType = ArrayType::get(EltTy, NewSize);
  852. // Create the new global variable.
  853. GlobalVariable *NG =
  854. new GlobalVariable(*DstGV->getParent(), NewType, SrcGV->isConstant(),
  855. DstGV->getLinkage(), /*init*/nullptr, /*name*/"", DstGV,
  856. DstGV->getThreadLocalMode(),
  857. DstGV->getType()->getAddressSpace());
  858. // Propagate alignment, visibility and section info.
  859. copyGVAttributes(NG, DstGV);
  860. AppendingVarInfo AVI;
  861. AVI.NewGV = NG;
  862. AVI.DstInit = DstGV->getInitializer();
  863. AVI.SrcInit = SrcGV->getInitializer();
  864. AppendingVars.push_back(AVI);
  865. // Replace any uses of the two global variables with uses of the new
  866. // global.
  867. ValueMap[SrcGV] = ConstantExpr::getBitCast(NG, TypeMap.get(SrcGV->getType()));
  868. DstGV->replaceAllUsesWith(ConstantExpr::getBitCast(NG, DstGV->getType()));
  869. DstGV->eraseFromParent();
  870. // Track the source variable so we don't try to link it.
  871. DoNotLinkFromSource.insert(SrcGV);
  872. return false;
  873. }
  874. bool ModuleLinker::linkGlobalValueProto(GlobalValue *SGV) {
  875. GlobalValue *DGV = getLinkedToGlobal(SGV);
  876. // Handle the ultra special appending linkage case first.
  877. if (DGV && DGV->hasAppendingLinkage())
  878. return linkAppendingVarProto(cast<GlobalVariable>(DGV),
  879. cast<GlobalVariable>(SGV));
  880. bool LinkFromSrc = true;
  881. Comdat *C = nullptr;
  882. GlobalValue::VisibilityTypes Visibility = SGV->getVisibility();
  883. bool HasUnnamedAddr = SGV->hasUnnamedAddr();
  884. if (const Comdat *SC = SGV->getComdat()) {
  885. Comdat::SelectionKind SK;
  886. std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
  887. C = DstM->getOrInsertComdat(SC->getName());
  888. C->setSelectionKind(SK);
  889. } else if (DGV) {
  890. if (shouldLinkFromSource(LinkFromSrc, *DGV, *SGV))
  891. return true;
  892. }
  893. if (!LinkFromSrc) {
  894. // Track the source global so that we don't attempt to copy it over when
  895. // processing global initializers.
  896. DoNotLinkFromSource.insert(SGV);
  897. if (DGV)
  898. // Make sure to remember this mapping.
  899. ValueMap[SGV] =
  900. ConstantExpr::getBitCast(DGV, TypeMap.get(SGV->getType()));
  901. }
  902. if (DGV) {
  903. Visibility = isLessConstraining(Visibility, DGV->getVisibility())
  904. ? DGV->getVisibility()
  905. : Visibility;
  906. HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
  907. }
  908. if (!LinkFromSrc && !DGV)
  909. return false;
  910. GlobalValue *NewGV;
  911. if (!LinkFromSrc) {
  912. NewGV = DGV;
  913. } else {
  914. // If the GV is to be lazily linked, don't create it just yet.
  915. // The ValueMaterializerTy will deal with creating it if it's used.
  916. if (!DGV && !OverrideFromSrc &&
  917. (SGV->hasLocalLinkage() || SGV->hasLinkOnceLinkage() ||
  918. SGV->hasAvailableExternallyLinkage())) {
  919. DoNotLinkFromSource.insert(SGV);
  920. return false;
  921. }
  922. NewGV = copyGlobalValueProto(TypeMap, *DstM, SGV);
  923. if (DGV && isa<Function>(DGV))
  924. if (auto *NewF = dyn_cast<Function>(NewGV))
  925. OverridingFunctions.insert(NewF);
  926. }
  927. NewGV->setUnnamedAddr(HasUnnamedAddr);
  928. NewGV->setVisibility(Visibility);
  929. if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {
  930. if (C)
  931. NewGO->setComdat(C);
  932. if (DGV && DGV->hasCommonLinkage() && SGV->hasCommonLinkage())
  933. NewGO->setAlignment(std::max(DGV->getAlignment(), SGV->getAlignment()));
  934. }
  935. if (auto *NewGVar = dyn_cast<GlobalVariable>(NewGV)) {
  936. auto *DGVar = dyn_cast_or_null<GlobalVariable>(DGV);
  937. auto *SGVar = dyn_cast<GlobalVariable>(SGV);
  938. if (DGVar && SGVar && DGVar->isDeclaration() && SGVar->isDeclaration() &&
  939. (!DGVar->isConstant() || !SGVar->isConstant()))
  940. NewGVar->setConstant(false);
  941. }
  942. // Make sure to remember this mapping.
  943. if (NewGV != DGV) {
  944. if (DGV) {
  945. DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewGV, DGV->getType()));
  946. DGV->eraseFromParent();
  947. }
  948. ValueMap[SGV] = NewGV;
  949. }
  950. return false;
  951. }
  952. static void getArrayElements(const Constant *C,
  953. SmallVectorImpl<Constant *> &Dest) {
  954. unsigned NumElements = cast<ArrayType>(C->getType())->getNumElements();
  955. for (unsigned i = 0; i != NumElements; ++i)
  956. Dest.push_back(C->getAggregateElement(i));
  957. }
  958. void ModuleLinker::linkAppendingVarInit(const AppendingVarInfo &AVI) {
  959. // Merge the initializer.
  960. SmallVector<Constant *, 16> DstElements;
  961. getArrayElements(AVI.DstInit, DstElements);
  962. SmallVector<Constant *, 16> SrcElements;
  963. getArrayElements(AVI.SrcInit, SrcElements);
  964. ArrayType *NewType = cast<ArrayType>(AVI.NewGV->getType()->getElementType());
  965. StringRef Name = AVI.NewGV->getName();
  966. bool IsNewStructor =
  967. (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") &&
  968. cast<StructType>(NewType->getElementType())->getNumElements() == 3;
  969. for (auto *V : SrcElements) {
  970. if (IsNewStructor) {
  971. Constant *Key = V->getAggregateElement(2);
  972. if (DoNotLinkFromSource.count(Key))
  973. continue;
  974. }
  975. DstElements.push_back(
  976. MapValue(V, ValueMap, RF_None, &TypeMap, &ValMaterializer));
  977. }
  978. if (IsNewStructor) {
  979. NewType = ArrayType::get(NewType->getElementType(), DstElements.size());
  980. AVI.NewGV->mutateType(PointerType::get(NewType, 0));
  981. }
  982. AVI.NewGV->setInitializer(ConstantArray::get(NewType, DstElements));
  983. }
  984. /// Update the initializers in the Dest module now that all globals that may be
  985. /// referenced are in Dest.
  986. void ModuleLinker::linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src) {
  987. // Figure out what the initializer looks like in the dest module.
  988. Dst.setInitializer(MapValue(Src.getInitializer(), ValueMap, RF_None, &TypeMap,
  989. &ValMaterializer));
  990. }
  991. /// Copy the source function over into the dest function and fix up references
  992. /// to values. At this point we know that Dest is an external function, and
  993. /// that Src is not.
  994. bool ModuleLinker::linkFunctionBody(Function &Dst, Function &Src) {
  995. assert(Dst.isDeclaration() && !Src.isDeclaration());
  996. // Materialize if needed.
  997. if (std::error_code EC = Src.materialize())
  998. return emitError(EC.message());
  999. // Link in the prefix data.
  1000. if (Src.hasPrefixData())
  1001. Dst.setPrefixData(MapValue(Src.getPrefixData(), ValueMap, RF_None, &TypeMap,
  1002. &ValMaterializer));
  1003. // Link in the prologue data.
  1004. if (Src.hasPrologueData())
  1005. Dst.setPrologueData(MapValue(Src.getPrologueData(), ValueMap, RF_None,
  1006. &TypeMap, &ValMaterializer));
  1007. // Link in the personality function.
  1008. if (Src.hasPersonalityFn())
  1009. Dst.setPersonalityFn(MapValue(Src.getPersonalityFn(), ValueMap, RF_None,
  1010. &TypeMap, &ValMaterializer));
  1011. // Go through and convert function arguments over, remembering the mapping.
  1012. Function::arg_iterator DI = Dst.arg_begin();
  1013. for (Argument &Arg : Src.args()) {
  1014. DI->setName(Arg.getName()); // Copy the name over.
  1015. // Add a mapping to our mapping.
  1016. ValueMap[&Arg] = DI;
  1017. ++DI;
  1018. }
  1019. // Copy over the metadata attachments.
  1020. SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
  1021. Src.getAllMetadata(MDs);
  1022. for (const auto &I : MDs)
  1023. Dst.setMetadata(I.first, MapMetadata(I.second, ValueMap, RF_None, &TypeMap,
  1024. &ValMaterializer));
  1025. // Splice the body of the source function into the dest function.
  1026. Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());
  1027. // At this point, all of the instructions and values of the function are now
  1028. // copied over. The only problem is that they are still referencing values in
  1029. // the Source function as operands. Loop through all of the operands of the
  1030. // functions and patch them up to point to the local versions.
  1031. for (BasicBlock &BB : Dst)
  1032. for (Instruction &I : BB)
  1033. RemapInstruction(&I, ValueMap, RF_IgnoreMissingEntries, &TypeMap,
  1034. &ValMaterializer);
  1035. // There is no need to map the arguments anymore.
  1036. for (Argument &Arg : Src.args())
  1037. ValueMap.erase(&Arg);
  1038. Src.dematerialize();
  1039. return false;
  1040. }
  1041. void ModuleLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) {
  1042. Constant *Aliasee = Src.getAliasee();
  1043. Constant *Val =
  1044. MapValue(Aliasee, ValueMap, RF_None, &TypeMap, &ValMaterializer);
  1045. Dst.setAliasee(Val);
  1046. }
  1047. bool ModuleLinker::linkGlobalValueBody(GlobalValue &Src) {
  1048. Value *Dst = ValueMap[&Src];
  1049. assert(Dst);
  1050. if (auto *F = dyn_cast<Function>(&Src))
  1051. return linkFunctionBody(cast<Function>(*Dst), *F);
  1052. if (auto *GVar = dyn_cast<GlobalVariable>(&Src)) {
  1053. linkGlobalInit(cast<GlobalVariable>(*Dst), *GVar);
  1054. return false;
  1055. }
  1056. linkAliasBody(cast<GlobalAlias>(*Dst), cast<GlobalAlias>(Src));
  1057. return false;
  1058. }
  1059. /// Insert all of the named MDNodes in Src into the Dest module.
  1060. void ModuleLinker::linkNamedMDNodes() {
  1061. const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
  1062. for (const NamedMDNode &NMD : SrcM->named_metadata()) {
  1063. // Don't link module flags here. Do them separately.
  1064. if (&NMD == SrcModFlags)
  1065. continue;
  1066. NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(NMD.getName());
  1067. // Add Src elements into Dest node.
  1068. for (const MDNode *op : NMD.operands())
  1069. DestNMD->addOperand(
  1070. MapMetadata(op, ValueMap, RF_None, &TypeMap, &ValMaterializer));
  1071. }
  1072. }
  1073. /// Drop DISubprograms that have been superseded.
  1074. ///
  1075. /// FIXME: this creates an asymmetric result: we strip functions from losing
  1076. /// subprograms in DstM, but leave losing subprograms in SrcM.
  1077. /// TODO: Remove this logic once the backend can correctly determine canonical
  1078. /// subprograms.
  1079. void ModuleLinker::stripReplacedSubprograms() {
  1080. // Avoid quadratic runtime by returning early when there's nothing to do.
  1081. if (OverridingFunctions.empty())
  1082. return;
  1083. // Move the functions now, so the set gets cleared even on early returns.
  1084. auto Functions = std::move(OverridingFunctions);
  1085. OverridingFunctions.clear();
  1086. // Drop functions from subprograms if they've been overridden by the new
  1087. // compile unit.
  1088. NamedMDNode *CompileUnits = DstM->getNamedMetadata("llvm.dbg.cu");
  1089. if (!CompileUnits)
  1090. return;
  1091. for (unsigned I = 0, E = CompileUnits->getNumOperands(); I != E; ++I) {
  1092. auto *CU = cast<DICompileUnit>(CompileUnits->getOperand(I));
  1093. assert(CU && "Expected valid compile unit");
  1094. for (DISubprogram *SP : CU->getSubprograms()) {
  1095. if (!SP || !SP->getFunction() || !Functions.count(SP->getFunction()))
  1096. continue;
  1097. // Prevent DebugInfoFinder from tagging this as the canonical subprogram,
  1098. // since the canonical one is in the incoming module.
  1099. SP->replaceFunction(nullptr);
  1100. }
  1101. }
  1102. }
  1103. /// Merge the linker flags in Src into the Dest module.
  1104. bool ModuleLinker::linkModuleFlagsMetadata() {
  1105. // If the source module has no module flags, we are done.
  1106. const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
  1107. if (!SrcModFlags) return false;
  1108. // If the destination module doesn't have module flags yet, then just copy
  1109. // over the source module's flags.
  1110. NamedMDNode *DstModFlags = DstM->getOrInsertModuleFlagsMetadata();
  1111. if (DstModFlags->getNumOperands() == 0) {
  1112. for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
  1113. DstModFlags->addOperand(SrcModFlags->getOperand(I));
  1114. return false;
  1115. }
  1116. // First build a map of the existing module flags and requirements.
  1117. DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags;
  1118. SmallSetVector<MDNode*, 16> Requirements;
  1119. for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
  1120. MDNode *Op = DstModFlags->getOperand(I);
  1121. ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0));
  1122. MDString *ID = cast<MDString>(Op->getOperand(1));
  1123. if (Behavior->getZExtValue() == Module::Require) {
  1124. Requirements.insert(cast<MDNode>(Op->getOperand(2)));
  1125. } else {
  1126. Flags[ID] = std::make_pair(Op, I);
  1127. }
  1128. }
  1129. // Merge in the flags from the source module, and also collect its set of
  1130. // requirements.
  1131. bool HasErr = false;
  1132. for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
  1133. MDNode *SrcOp = SrcModFlags->getOperand(I);
  1134. ConstantInt *SrcBehavior =
  1135. mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
  1136. MDString *ID = cast<MDString>(SrcOp->getOperand(1));
  1137. MDNode *DstOp;
  1138. unsigned DstIndex;
  1139. std::tie(DstOp, DstIndex) = Flags.lookup(ID);
  1140. unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
  1141. // If this is a requirement, add it and continue.
  1142. if (SrcBehaviorValue == Module::Require) {
  1143. // If the destination module does not already have this requirement, add
  1144. // it.
  1145. if (Requirements.insert(cast<MDNode>(SrcOp->getOperand(2)))) {
  1146. DstModFlags->addOperand(SrcOp);
  1147. }
  1148. continue;
  1149. }
  1150. // If there is no existing flag with this ID, just add it.
  1151. if (!DstOp) {
  1152. Flags[ID] = std::make_pair(SrcOp, DstModFlags->getNumOperands());
  1153. DstModFlags->addOperand(SrcOp);
  1154. continue;
  1155. }
  1156. // Otherwise, perform a merge.
  1157. ConstantInt *DstBehavior =
  1158. mdconst::extract<ConstantInt>(DstOp->getOperand(0));
  1159. unsigned DstBehaviorValue = DstBehavior->getZExtValue();
  1160. // If either flag has override behavior, handle it first.
  1161. if (DstBehaviorValue == Module::Override) {
  1162. // Diagnose inconsistent flags which both have override behavior.
  1163. if (SrcBehaviorValue == Module::Override &&
  1164. SrcOp->getOperand(2) != DstOp->getOperand(2)) {
  1165. HasErr |= emitError("linking module flags '" + ID->getString() +
  1166. "': IDs have conflicting override values");
  1167. }
  1168. continue;
  1169. } else if (SrcBehaviorValue == Module::Override) {
  1170. // Update the destination flag to that of the source.
  1171. DstModFlags->setOperand(DstIndex, SrcOp);
  1172. Flags[ID].first = SrcOp;
  1173. continue;
  1174. }
  1175. // Diagnose inconsistent merge behavior types.
  1176. if (SrcBehaviorValue != DstBehaviorValue) {
  1177. HasErr |= emitError("linking module flags '" + ID->getString() +
  1178. "': IDs have conflicting behaviors");
  1179. continue;
  1180. }
  1181. auto replaceDstValue = [&](MDNode *New) {
  1182. Metadata *FlagOps[] = {DstOp->getOperand(0), ID, New};
  1183. MDNode *Flag = MDNode::get(DstM->getContext(), FlagOps);
  1184. DstModFlags->setOperand(DstIndex, Flag);
  1185. Flags[ID].first = Flag;
  1186. };
  1187. // Perform the merge for standard behavior types.
  1188. switch (SrcBehaviorValue) {
  1189. case Module::Require:
  1190. case Module::Override: llvm_unreachable("not possible");
  1191. case Module::Error: {
  1192. // Emit an error if the values differ.
  1193. if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
  1194. HasErr |= emitError("linking module flags '" + ID->getString() +
  1195. "': IDs have conflicting values");
  1196. }
  1197. continue;
  1198. }
  1199. case Module::Warning: {
  1200. // Emit a warning if the values differ.
  1201. if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
  1202. emitWarning("linking module flags '" + ID->getString() +
  1203. "': IDs have conflicting values");
  1204. }
  1205. continue;
  1206. }
  1207. case Module::Append: {
  1208. MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
  1209. MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
  1210. SmallVector<Metadata *, 8> MDs;
  1211. MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands());
  1212. MDs.append(DstValue->op_begin(), DstValue->op_end());
  1213. MDs.append(SrcValue->op_begin(), SrcValue->op_end());
  1214. replaceDstValue(MDNode::get(DstM->getContext(), MDs));
  1215. break;
  1216. }
  1217. case Module::AppendUnique: {
  1218. SmallSetVector<Metadata *, 16> Elts;
  1219. MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
  1220. MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
  1221. Elts.insert(DstValue->op_begin(), DstValue->op_end());
  1222. Elts.insert(SrcValue->op_begin(), SrcValue->op_end());
  1223. replaceDstValue(MDNode::get(DstM->getContext(),
  1224. makeArrayRef(Elts.begin(), Elts.end())));
  1225. break;
  1226. }
  1227. }
  1228. }
  1229. // Check all of the requirements.
  1230. for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
  1231. MDNode *Requirement = Requirements[I];
  1232. MDString *Flag = cast<MDString>(Requirement->getOperand(0));
  1233. Metadata *ReqValue = Requirement->getOperand(1);
  1234. MDNode *Op = Flags[Flag].first;
  1235. if (!Op || Op->getOperand(2) != ReqValue) {
  1236. HasErr |= emitError("linking module flags '" + Flag->getString() +
  1237. "': does not have the required value");
  1238. continue;
  1239. }
  1240. }
  1241. return HasErr;
  1242. }
  1243. // This function returns true if the triples match.
  1244. static bool triplesMatch(const Triple &T0, const Triple &T1) {
  1245. // If vendor is apple, ignore the version number.
  1246. if (T0.getVendor() == Triple::Apple)
  1247. return T0.getArch() == T1.getArch() &&
  1248. T0.getSubArch() == T1.getSubArch() &&
  1249. T0.getVendor() == T1.getVendor() &&
  1250. T0.getOS() == T1.getOS();
  1251. return T0 == T1;
  1252. }
  1253. // This function returns the merged triple.
  1254. static std::string mergeTriples(const Triple &SrcTriple, const Triple &DstTriple) {
  1255. // If vendor is apple, pick the triple with the larger version number.
  1256. if (SrcTriple.getVendor() == Triple::Apple)
  1257. if (DstTriple.isOSVersionLT(SrcTriple))
  1258. return SrcTriple.str();
  1259. return DstTriple.str();
  1260. }
  1261. bool ModuleLinker::run() {
  1262. assert(DstM && "Null destination module");
  1263. assert(SrcM && "Null source module");
  1264. // Inherit the target data from the source module if the destination module
  1265. // doesn't have one already.
  1266. if (DstM->getDataLayout().isDefault())
  1267. DstM->setDataLayout(SrcM->getDataLayout());
  1268. if (SrcM->getDataLayout() != DstM->getDataLayout()) {
  1269. emitWarning("Linking two modules of different data layouts: '" +
  1270. SrcM->getModuleIdentifier() + "' is '" +
  1271. SrcM->getDataLayoutStr() + "' whereas '" +
  1272. DstM->getModuleIdentifier() + "' is '" +
  1273. DstM->getDataLayoutStr() + "'\n");
  1274. }
  1275. // Copy the target triple from the source to dest if the dest's is empty.
  1276. if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
  1277. DstM->setTargetTriple(SrcM->getTargetTriple());
  1278. Triple SrcTriple(SrcM->getTargetTriple()), DstTriple(DstM->getTargetTriple());
  1279. if (!SrcM->getTargetTriple().empty() && !triplesMatch(SrcTriple, DstTriple))
  1280. emitWarning("Linking two modules of different target triples: " +
  1281. SrcM->getModuleIdentifier() + "' is '" +
  1282. SrcM->getTargetTriple() + "' whereas '" +
  1283. DstM->getModuleIdentifier() + "' is '" +
  1284. DstM->getTargetTriple() + "'\n");
  1285. DstM->setTargetTriple(mergeTriples(SrcTriple, DstTriple));
  1286. // Append the module inline asm string.
  1287. if (!SrcM->getModuleInlineAsm().empty()) {
  1288. if (DstM->getModuleInlineAsm().empty())
  1289. DstM->setModuleInlineAsm(SrcM->getModuleInlineAsm());
  1290. else
  1291. DstM->setModuleInlineAsm(DstM->getModuleInlineAsm()+"\n"+
  1292. SrcM->getModuleInlineAsm());
  1293. }
  1294. // Loop over all of the linked values to compute type mappings.
  1295. computeTypeMapping();
  1296. ComdatsChosen.clear();
  1297. for (const auto &SMEC : SrcM->getComdatSymbolTable()) {
  1298. const Comdat &C = SMEC.getValue();
  1299. if (ComdatsChosen.count(&C))
  1300. continue;
  1301. Comdat::SelectionKind SK;
  1302. bool LinkFromSrc;
  1303. if (getComdatResult(&C, SK, LinkFromSrc))
  1304. return true;
  1305. ComdatsChosen[&C] = std::make_pair(SK, LinkFromSrc);
  1306. }
  1307. // Upgrade mismatched global arrays.
  1308. upgradeMismatchedGlobals();
  1309. // Insert all of the globals in src into the DstM module... without linking
  1310. // initializers (which could refer to functions not yet mapped over).
  1311. for (GlobalVariable &GV : SrcM->globals())
  1312. if (linkGlobalValueProto(&GV))
  1313. return true;
  1314. // Link the functions together between the two modules, without doing function
  1315. // bodies... this just adds external function prototypes to the DstM
  1316. // function... We do this so that when we begin processing function bodies,
  1317. // all of the global values that may be referenced are available in our
  1318. // ValueMap.
  1319. for (Function &F :*SrcM)
  1320. if (linkGlobalValueProto(&F))
  1321. return true;
  1322. // If there were any aliases, link them now.
  1323. for (GlobalAlias &GA : SrcM->aliases())
  1324. if (linkGlobalValueProto(&GA))
  1325. return true;
  1326. for (const AppendingVarInfo &AppendingVar : AppendingVars)
  1327. linkAppendingVarInit(AppendingVar);
  1328. for (const auto &Entry : DstM->getComdatSymbolTable()) {
  1329. const Comdat &C = Entry.getValue();
  1330. if (C.getSelectionKind() == Comdat::Any)
  1331. continue;
  1332. const GlobalValue *GV = SrcM->getNamedValue(C.getName());
  1333. if (GV)
  1334. MapValue(GV, ValueMap, RF_None, &TypeMap, &ValMaterializer);
  1335. }
  1336. // Strip replaced subprograms before mapping any metadata -- so that we're
  1337. // not changing metadata from the source module (note that
  1338. // linkGlobalValueBody() eventually calls RemapInstruction() and therefore
  1339. // MapMetadata()) -- but after linking global value protocols -- so that
  1340. // OverridingFunctions has been built.
  1341. stripReplacedSubprograms();
  1342. // Link in the function bodies that are defined in the source module into
  1343. // DstM.
  1344. for (Function &SF : *SrcM) {
  1345. // Skip if no body (function is external).
  1346. if (SF.isDeclaration())
  1347. continue;
  1348. // Skip if not linking from source.
  1349. if (DoNotLinkFromSource.count(&SF))
  1350. continue;
  1351. if (linkGlobalValueBody(SF))
  1352. return true;
  1353. }
  1354. // Resolve all uses of aliases with aliasees.
  1355. for (GlobalAlias &Src : SrcM->aliases()) {
  1356. if (DoNotLinkFromSource.count(&Src))
  1357. continue;
  1358. linkGlobalValueBody(Src);
  1359. }
  1360. // Remap all of the named MDNodes in Src into the DstM module. We do this
  1361. // after linking GlobalValues so that MDNodes that reference GlobalValues
  1362. // are properly remapped.
  1363. linkNamedMDNodes();
  1364. // Merge the module flags into the DstM module.
  1365. if (linkModuleFlagsMetadata())
  1366. return true;
  1367. // Update the initializers in the DstM module now that all globals that may
  1368. // be referenced are in DstM.
  1369. for (GlobalVariable &Src : SrcM->globals()) {
  1370. // Only process initialized GV's or ones not already in dest.
  1371. if (!Src.hasInitializer() || DoNotLinkFromSource.count(&Src))
  1372. continue;
  1373. linkGlobalValueBody(Src);
  1374. }
  1375. // Process vector of lazily linked in functions.
  1376. while (!LazilyLinkGlobalValues.empty()) {
  1377. GlobalValue *SGV = LazilyLinkGlobalValues.back();
  1378. LazilyLinkGlobalValues.pop_back();
  1379. assert(!SGV->isDeclaration() && "users should not pass down decls");
  1380. if (linkGlobalValueBody(*SGV))
  1381. return true;
  1382. }
  1383. return false;
  1384. }
  1385. Linker::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P)
  1386. : ETypes(E), IsPacked(P) {}
  1387. Linker::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST)
  1388. : ETypes(ST->elements()), IsPacked(ST->isPacked()) {}
  1389. bool Linker::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const {
  1390. if (IsPacked != That.IsPacked)
  1391. return false;
  1392. if (ETypes != That.ETypes)
  1393. return false;
  1394. return true;
  1395. }
  1396. bool Linker::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const {
  1397. return !this->operator==(That);
  1398. }
  1399. StructType *Linker::StructTypeKeyInfo::getEmptyKey() {
  1400. return DenseMapInfo<StructType *>::getEmptyKey();
  1401. }
  1402. StructType *Linker::StructTypeKeyInfo::getTombstoneKey() {
  1403. return DenseMapInfo<StructType *>::getTombstoneKey();
  1404. }
  1405. unsigned Linker::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {
  1406. return hash_combine(hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()),
  1407. Key.IsPacked);
  1408. }
  1409. unsigned Linker::StructTypeKeyInfo::getHashValue(const StructType *ST) {
  1410. return getHashValue(KeyTy(ST));
  1411. }
  1412. bool Linker::StructTypeKeyInfo::isEqual(const KeyTy &LHS,
  1413. const StructType *RHS) {
  1414. if (RHS == getEmptyKey() || RHS == getTombstoneKey())
  1415. return false;
  1416. return LHS == KeyTy(RHS);
  1417. }
  1418. bool Linker::StructTypeKeyInfo::isEqual(const StructType *LHS,
  1419. const StructType *RHS) {
  1420. if (RHS == getEmptyKey())
  1421. return LHS == getEmptyKey();
  1422. if (RHS == getTombstoneKey())
  1423. return LHS == getTombstoneKey();
  1424. return KeyTy(LHS) == KeyTy(RHS);
  1425. }
  1426. void Linker::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) {
  1427. assert(!Ty->isOpaque());
  1428. NonOpaqueStructTypes.insert(Ty);
  1429. }
  1430. void Linker::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) {
  1431. assert(!Ty->isOpaque());
  1432. NonOpaqueStructTypes.insert(Ty);
  1433. bool Removed = OpaqueStructTypes.erase(Ty);
  1434. (void)Removed;
  1435. assert(Removed);
  1436. }
  1437. void Linker::IdentifiedStructTypeSet::addOpaque(StructType *Ty) {
  1438. assert(Ty->isOpaque());
  1439. OpaqueStructTypes.insert(Ty);
  1440. }
  1441. StructType *
  1442. Linker::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes,
  1443. bool IsPacked) {
  1444. Linker::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked);
  1445. auto I = NonOpaqueStructTypes.find_as(Key);
  1446. if (I == NonOpaqueStructTypes.end())
  1447. return nullptr;
  1448. return *I;
  1449. }
  1450. bool Linker::IdentifiedStructTypeSet::hasType(StructType *Ty) {
  1451. if (Ty->isOpaque())
  1452. return OpaqueStructTypes.count(Ty);
  1453. auto I = NonOpaqueStructTypes.find(Ty);
  1454. if (I == NonOpaqueStructTypes.end())
  1455. return false;
  1456. return *I == Ty;
  1457. }
  1458. void Linker::init(Module *M, DiagnosticHandlerFunction DiagnosticHandler) {
  1459. this->Composite = M;
  1460. this->DiagnosticHandler = DiagnosticHandler;
  1461. TypeFinder StructTypes;
  1462. StructTypes.run(*M, true);
  1463. for (StructType *Ty : StructTypes) {
  1464. if (Ty->isOpaque())
  1465. IdentifiedStructTypes.addOpaque(Ty);
  1466. else
  1467. IdentifiedStructTypes.addNonOpaque(Ty);
  1468. }
  1469. }
  1470. Linker::Linker(Module *M, DiagnosticHandlerFunction DiagnosticHandler) {
  1471. init(M, DiagnosticHandler);
  1472. }
  1473. Linker::Linker(Module *M) {
  1474. init(M, [this](const DiagnosticInfo &DI) {
  1475. Composite->getContext().diagnose(DI);
  1476. });
  1477. }
  1478. Linker::~Linker() {
  1479. }
  1480. void Linker::deleteModule() {
  1481. delete Composite;
  1482. Composite = nullptr;
  1483. }
  1484. bool Linker::linkInModule(Module *Src, bool OverrideSymbols) {
  1485. ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src,
  1486. DiagnosticHandler, OverrideSymbols);
  1487. bool RetCode = TheLinker.run();
  1488. Composite->dropTriviallyDeadConstantArrays();
  1489. return RetCode;
  1490. }
  1491. void Linker::setModule(Module *Dst) {
  1492. init(Dst, DiagnosticHandler);
  1493. }
  1494. //===----------------------------------------------------------------------===//
  1495. // LinkModules entrypoint.
  1496. //===----------------------------------------------------------------------===//
  1497. /// This function links two modules together, with the resulting Dest module
  1498. /// modified to be the composite of the two input modules. If an error occurs,
  1499. /// true is returned and ErrorMsg (if not null) is set to indicate the problem.
  1500. /// Upon failure, the Dest module could be in a modified state, and shouldn't be
  1501. /// relied on to be consistent.
  1502. bool Linker::LinkModules(Module *Dest, Module *Src,
  1503. DiagnosticHandlerFunction DiagnosticHandler) {
  1504. Linker L(Dest, DiagnosticHandler);
  1505. return L.linkInModule(Src);
  1506. }
  1507. bool Linker::LinkModules(Module *Dest, Module *Src) {
  1508. Linker L(Dest);
  1509. return L.linkInModule(Src);
  1510. }
  1511. //===----------------------------------------------------------------------===//
  1512. // C API.
  1513. //===----------------------------------------------------------------------===//
  1514. _Use_decl_annotations_
  1515. LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
  1516. LLVMLinkerMode Unused, char **OutMessages) {
  1517. Module *D = unwrap(Dest);
  1518. std::string Message;
  1519. raw_string_ostream Stream(Message);
  1520. DiagnosticPrinterRawOStream DP(Stream);
  1521. LLVMBool Result = Linker::LinkModules(
  1522. D, unwrap(Src), [&](const DiagnosticInfo &DI) { DI.print(DP); });
  1523. if (OutMessages && Result) {
  1524. Stream.flush();
  1525. #ifdef _WIN32
  1526. *OutMessages = _strdup(Message.c_str()); // HLSL Change for strdup
  1527. #else
  1528. *OutMessages = strdup(Message.c_str());
  1529. #endif
  1530. }
  1531. return Result;
  1532. }