- if (Named.size() == 0) {
- return false; // Nothing to extract
- }
-
-
- if (deleteStuff)
- return deleteGV();
- M.setModuleInlineAsm("");
- return isolateGV(M);
- }
-
- bool deleteGV() {
- for (std::vector<GlobalValue*>::iterator GI = Named.begin(),
- GE = Named.end(); GI != GE; ++GI) {
- if (Function* NamedFunc = dyn_cast<Function>(*GI)) {
- // If we're in relinking mode, set linkage of all internal callees to
- // external. This will allow us extract function, and then - link
- // everything together
- if (reLink) {
- for (Function::iterator B = NamedFunc->begin(), BE = NamedFunc->end();
- B != BE; ++B) {
- for (BasicBlock::iterator I = B->begin(), E = B->end();
- I != E; ++I) {
- if (CallInst* callInst = dyn_cast<CallInst>(&*I)) {
- Function* Callee = callInst->getCalledFunction();
- if (Callee && Callee->hasLocalLinkage())
- Callee->setLinkage(GlobalValue::ExternalLinkage);
- }
- }
- }
- }
-
- NamedFunc->setLinkage(GlobalValue::ExternalLinkage);
- NamedFunc->deleteBody();
- assert(NamedFunc->isDeclaration() && "This didn't make the function external!");
- } else {
- if (!(*GI)->isDeclaration()) {
- cast<GlobalVariable>(*GI)->setInitializer(0); //clear the initializer
- (*GI)->setLinkage(GlobalValue::ExternalLinkage);
- }
+ // Visit the global inline asm.
+ if (!deleteStuff)
+ M.setModuleInlineAsm("");
+
+ // For simplicity, just give all GlobalValues ExternalLinkage. A trickier
+ // implementation could figure out which GlobalValues are actually
+ // referenced by the Named set, and which GlobalValues in the rest of
+ // the module are referenced by the NamedSet, and get away with leaving
+ // more internal and private things internal and private. But for now,
+ // be conservative and simple.
+
+ // Visit the GlobalVariables.
+ for (Module::global_iterator I = M.global_begin(), E = M.global_end();
+ I != E; ++I) {
+ bool Delete =
+ deleteStuff == (bool)Named.count(I) && !I->isDeclaration();
+ if (!Delete) {
+ if (I->hasAvailableExternallyLinkage())
+ continue;
+ if (I->getName() == "llvm.global_ctors")
+ continue;