- // Load in the archive objects.
- if (Verbose) std::cerr << " Loading '" << Filename << "'\n";
- std::vector<Module*> Objects;
- if (ReadArchiveFile(Filename, Objects, &ErrorMessage))
- return true;
-
- // Figure out which symbols are defined by all of the modules in the archive.
- std::vector<std::set<std::string> > DefinedSymbols;
- DefinedSymbols.resize(Objects.size());
- for (unsigned i = 0; i != Objects.size(); ++i) {
- GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]);
- }
-
- // While we are linking in object files, loop.
- bool Linked = true;
- while (Linked) {
- Linked = false;
-
- for (unsigned i = 0; i != Objects.size(); ++i) {
- // Consider whether we need to link in this module... we only need to
- // link it in if it defines some symbol which is so far undefined.
- //
- const std::set<std::string> &DefSymbols = DefinedSymbols[i];
-
- bool ObjectRequired = false;
- for (std::set<std::string>::iterator I = UndefinedSymbols.begin(),
- E = UndefinedSymbols.end(); I != E; ++I)
- if (DefSymbols.count(*I)) {
- if (Verbose)
- std::cerr << " Found object providing symbol '" << *I << "'...\n";
- ObjectRequired = true;
- break;
- }
-
- // We DO need to link this object into the program...
- if (ObjectRequired) {
- if (LinkModules(M, Objects[i], &ErrorMessage))
- return true; // Couldn't link in the right object file...
-
- // Since we have linked in this object, delete it from the list of
- // objects to consider in this archive file.
- std::swap(Objects[i], Objects.back());
- std::swap(DefinedSymbols[i], DefinedSymbols.back());
- Objects.pop_back();
- DefinedSymbols.pop_back();
- --i; // Do not skip an entry
-
- // The undefined symbols set should have shrunk.
- GetAllUndefinedSymbols(M, UndefinedSymbols);
- Linked = true; // We have linked something in!
- }
- }
- }
-
- return false;
-}
-
-//
-// Function: LinkInFile ()
-//
-// Description:
-// This function will open an archive library and link in all objects which
-// provide symbols that are currently undefined.
-//
-// Inputs:
-// HeadModule - The module in which to link the archives.
-// Filename - The pathname of the archive.
-// Verbose - Flags whether verbose messages should be printed.
-//
-// Outputs:
-// ErrorMessage - A C++ string detailing what error occurred, if any.
-//
-// Return Value:
-// TRUE - An error occurred.
-// FALSE - No errors.
-//
-static bool LinkInFile(Module *HeadModule,
- const std::string &Filename,
- std::string &ErrorMessage,
- bool Verbose)
-{
- std::auto_ptr<Module> M(LoadObject(Filename, ErrorMessage));
- if (M.get() == 0) return true;
- if (Verbose) std::cerr << "Linking in '" << Filename << "'\n";
- return LinkModules(HeadModule, M.get(), &ErrorMessage);
-}
-
-//
-// Function: LinkFiles ()
-//
-// Description:
-// This function takes a module and a list of files and links them all
-// together. It locates the file either in the current directory, as it's
-// absolute or relative pathname, or as a file somewhere in
-// LLVM_LIB_SEARCH_PATH.
-//
-// Inputs:
-// progname - The name of the program (infamous argv[0]).
-// HeadModule - The module under which all files will be linked.
-// Files - A vector of C++ strings indicating the LLVM bytecode filenames
-// to be linked. The names can refer to a mixture of pure LLVM
-// bytecode files and archive (ar) formatted files.
-// Verbose - Flags whether verbose output should be printed while linking.
-//
-// Outputs:
-// HeadModule - The module will have the specified LLVM bytecode files linked
-// in.
-//
-// Return value:
-// FALSE - No errors.
-// TRUE - Some error occurred.
-//
-bool LinkFiles(const char *progname,
- Module *HeadModule,
- const std::vector<std::string> &Files,
- bool Verbose)
-{
- // String in which to receive error messages.
- std::string ErrorMessage;
-
- // Full pathname of the file
- std::string Pathname;
-
- // Get the library search path from the environment
- char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
-
- for (unsigned i = 1; i < Files.size(); ++i) {
- // Determine where this file lives.
- if (FileExists(Files[i])) {
- Pathname = Files[i];
- } else {
- if (SearchPath == NULL) {
- std::cerr << "Cannot find linker input file '" << Files[i] << "'";
- return true;
- }
-
- Pathname = std::string(SearchPath)+"/"+Files[i];
- if (!FileExists(Pathname)) {
- std::cerr << "Cannot find linker input file '" << Files[i] << "'";
- return true;
- }
- }
-
- // A user may specify an ar archive without -l, perhaps because it
- // is not installed as a library. Detect that and link the library.
- if (IsArchive(Pathname)) {
- if (Verbose)
- std::cerr << "Linking archive '" << Files[i] << "'\n";