From 30235dad4b77ed83ca985030aff4fb4767551e5d Mon Sep 17 00:00:00 2001 From: Devang Patel Date: Mon, 14 Aug 2006 22:36:16 +0000 Subject: [PATCH] Use mangler, instead of addUnderscore(), to get mangled name. Now, LLVMSymbol keeps symbol original name and mangled name. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29679 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/LinkTimeOptimizer.h | 10 ++++++++-- tools/lto/lto.cpp | 34 +++++++++++++++++++------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/include/llvm/LinkTimeOptimizer.h b/include/llvm/LinkTimeOptimizer.h index c567a04ac8c..d4de787154a 100644 --- a/include/llvm/LinkTimeOptimizer.h +++ b/include/llvm/LinkTimeOptimizer.h @@ -55,11 +55,17 @@ namespace llvm { LTOLinkageTypes getLinkage() const { return linkage; } void mayBeNotUsed(); - LLVMSymbol (enum LTOLinkageTypes lt, GlobalValue *g) : linkage(lt), gv(g) {} + LLVMSymbol (enum LTOLinkageTypes lt, GlobalValue *g, std::string n, + std::string m) : linkage(lt), gv(g), name(n), mangledName(m) {} + + const char *getName() { return name.c_str(); } + const char *getMangledName() { return mangledName.c_str(); } private: enum LTOLinkageTypes linkage; GlobalValue *gv; + std::string name; + std::string mangledName; }; class string_compare { @@ -79,7 +85,7 @@ namespace llvm { enum LTOStatus readLLVMObjectFile(const std::string &InputFilename, NameToSymbolMap &symbols, - std::set &references); + std::set &references); enum LTOStatus optimizeModules(const std::string &OutputFilename, std::vector &exportList); diff --git a/tools/lto/lto.cpp b/tools/lto/lto.cpp index 4a8e25cf981..a274d98a78c 100644 --- a/tools/lto/lto.cpp +++ b/tools/lto/lto.cpp @@ -23,6 +23,7 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileUtilities.h" #include "llvm/Support/SystemUtils.h" +#include "llvm/Support/Mangler.h" #include "llvm/System/Program.h" #include "llvm/System/Signals.h" #include "llvm/Analysis/Passes.h" @@ -84,17 +85,18 @@ getLTOLinkageType(GlobalValue *v) // Find exeternal symbols referenced by VALUE. This is a recursive function. static void -findExternalRefs(Value *value, std::set &references) { +findExternalRefs(Value *value, std::set &references, + Mangler &mangler) { if (GlobalValue *gv = dyn_cast(value)) { LTOLinkageTypes lt = getLTOLinkageType(gv); if (lt != LTOInternalLinkage && strncmp (gv->getName().c_str(), "llvm.", 5)) - references.insert(addUnderscore(gv->getName().c_str())); + references.insert(mangler.getValueName(gv)); } else if (Constant *c = dyn_cast(value)) // Handle ConstantExpr, ConstantStruct, ConstantArry etc.. for (unsigned i = 0, e = c->getNumOperands(); i != e; ++i) - findExternalRefs(c->getOperand(i), references); + findExternalRefs(c->getOperand(i), references, mangler); } /// InputFilename is a LLVM bytecode file. Read it using bytecode reader. @@ -104,11 +106,15 @@ findExternalRefs(Value *value, std::set &references) { enum LTOStatus LinkTimeOptimizer::readLLVMObjectFile(const std::string &InputFilename, NameToSymbolMap &symbols, - std::set &references) + std::set &references) { Module *m = ParseBytecodeFile(InputFilename); if (!m) return LTO_READ_FAILURE; + + // Use mangler to add GlobalPrefix to names to match linker names. + // FIXME : Instead of hard coding "-" use GlobalPrefix. + Mangler mangler(*m, "_"); modules.push_back(m); @@ -118,19 +124,19 @@ LinkTimeOptimizer::readLLVMObjectFile(const std::string &InputFilename, if (!f->isExternal() && lt != LTOInternalLinkage && strncmp (f->getName().c_str(), "llvm.", 5)) { - const char *name = addUnderscore(f->getName().c_str()); - LLVMSymbol *newSymbol = new LLVMSymbol(lt, f); - symbols[name] = newSymbol; - allSymbols[name] = newSymbol; + LLVMSymbol *newSymbol = new LLVMSymbol(lt, f, f->getName(), + mangler.getValueName(f)); + symbols[newSymbol->getMangledName()] = newSymbol; + allSymbols[newSymbol->getMangledName()] = newSymbol; } - + // Collect external symbols referenced by this function. for (Function::iterator b = f->begin(), fe = f->end(); b != fe; ++b) for (BasicBlock::iterator i = b->begin(), be = b->end(); i != be; ++i) for (unsigned count = 0, total = i->getNumOperands(); count != total; ++count) - findExternalRefs(i->getOperand(count), references); + findExternalRefs(i->getOperand(count), references, mangler); } for (Module::global_iterator v = m->global_begin(), e = m->global_end(); @@ -138,13 +144,13 @@ LinkTimeOptimizer::readLLVMObjectFile(const std::string &InputFilename, LTOLinkageTypes lt = getLTOLinkageType(v); if (!v->isExternal() && lt != LTOInternalLinkage && strncmp (v->getName().c_str(), "llvm.", 5)) { - const char *name = addUnderscore(v->getName().c_str()); - LLVMSymbol *newSymbol = new LLVMSymbol(lt,v); - symbols[name] = newSymbol; + LLVMSymbol *newSymbol = new LLVMSymbol(lt, v, v->getName(), + mangler.getValueName(v)); + symbols[newSymbol->getMangledName()] = newSymbol; for (unsigned count = 0, total = v->getNumOperands(); count != total; ++count) - findExternalRefs(v->getOperand(count), references); + findExternalRefs(v->getOperand(count), references, mangler); } }