+namespace {
+class LocalValueMaterializer : public ValueMaterializer {
+ DenseSet<GlobalValue *> &Dropped;
+
+public:
+ LocalValueMaterializer(DenseSet<GlobalValue *> &Dropped) : Dropped(Dropped) {}
+ Value *materializeValueFor(Value *V) override;
+};
+}
+
+Value *LocalValueMaterializer::materializeValueFor(Value *V) {
+ auto *GV = dyn_cast<GlobalValue>(V);
+ if (!GV)
+ return nullptr;
+ if (!Dropped.count(GV))
+ return nullptr;
+ assert(!isa<GlobalAlias>(GV) && "Found alias point to weak alias.");
+ return makeInternalReplacement(cast<GlobalObject>(GV));
+}
+
+static Constant *mapConstantToLocalCopy(Constant *C, ValueToValueMapTy &VM,
+ LocalValueMaterializer *Materializer) {
+ return MapValue(C, VM, RF_IgnoreMissingEntries, nullptr, Materializer);
+}
+
+static std::unique_ptr<Module>
+getModuleForFile(LLVMContext &Context, claimed_file &F, raw_fd_ostream *ApiFile,
+ StringSet<> &Internalize, StringSet<> &Maybe) {
+ ld_plugin_input_file File;
+ if (get_input_file(F.handle, &File) != LDPS_OK)
+ message(LDPL_FATAL, "Failed to get file information");
+
+ if (get_symbols(F.handle, F.syms.size(), &F.syms[0]) != LDPS_OK)
+ message(LDPL_FATAL, "Failed to get symbol information");
+
+ const void *View;
+ if (get_view(F.handle, &View) != LDPS_OK)
+ message(LDPL_FATAL, "Failed to get a view of file");
+
+ llvm::ErrorOr<MemoryBufferRef> MBOrErr =
+ object::IRObjectFile::findBitcodeInMemBuffer(
+ MemoryBufferRef(StringRef((const char *)View, File.filesize), ""));
+ if (std::error_code EC = MBOrErr.getError())
+ message(LDPL_FATAL, "Could not read bitcode from file : %s",
+ EC.message().c_str());
+
+ std::unique_ptr<MemoryBuffer> Buffer =
+ MemoryBuffer::getMemBuffer(MBOrErr->getBuffer(), "", false);
+
+ if (release_input_file(F.handle) != LDPS_OK)
+ message(LDPL_FATAL, "Failed to release file information");
+
+ ErrorOr<Module *> MOrErr = getLazyBitcodeModule(std::move(Buffer), Context);
+
+ if (std::error_code EC = MOrErr.getError())
+ message(LDPL_FATAL, "Could not read bitcode from file : %s",
+ EC.message().c_str());
+
+ std::unique_ptr<Module> M(MOrErr.get());
+
+ SmallPtrSet<GlobalValue *, 8> Used;
+ collectUsedGlobalVariables(*M, Used, /*CompilerUsed*/ false);
+
+ DenseSet<GlobalValue *> Drop;
+ std::vector<GlobalAlias *> KeptAliases;
+ for (ld_plugin_symbol &Sym : F.syms) {
+ ld_plugin_symbol_resolution Resolution =
+ (ld_plugin_symbol_resolution)Sym.resolution;
+
+ if (options::generate_api_file)
+ *ApiFile << Sym.name << ' ' << getResolutionName(Resolution) << '\n';
+
+ GlobalValue *GV = M->getNamedValue(Sym.name);
+ if (!GV)
+ continue; // Asm symbol.
+
+ if (Resolution != LDPR_PREVAILING_DEF_IRONLY && GV->hasCommonLinkage()) {
+ // Common linkage is special. There is no single symbol that wins the
+ // resolution. Instead we have to collect the maximum alignment and size.
+ // The IR linker does that for us if we just pass it every common GV.
+ // We still have to keep track of LDPR_PREVAILING_DEF_IRONLY so we
+ // internalize once the IR linker has done its job.