-//
-// Function: LinkLibraries ()
-//
-// Description:
-// This function takes the specified library files and links them into the
-// main bytecode object file.
-//
-// Inputs:
-// progname - The name of the program (infamous argv[0]).
-// HeadModule - The module into which all necessary libraries will be linked.
-// Libraries - The list of libraries to link into the module.
-// LibPaths - The list of library paths in which to find libraries.
-// Verbose - Flags whether verbose messages should be printed.
-// Native - Flags whether native code is being generated.
-//
-// Outputs:
-// HeadModule - The module will have all necessary libraries linked in.
-//
-// Return value:
-// FALSE - No error.
-// TRUE - Error.
-//
-bool LinkLibraries(const char *progname,
- Module *HeadModule,
- const std::vector<std::string> &Libraries,
- const std::vector<std::string> &LibPaths,
- bool Verbose,
- bool Native)
-{
- // String in which to receive error messages.
- std::string ErrorMessage;
-
- for (unsigned i = 1; i < Libraries.size(); ++i) {
- // Determine where this library lives.
- std::string Pathname = FindLib(Libraries[i], LibPaths);
- if (Pathname.empty()) {
- // If the pathname does not exist, then continue to the next one if
- // we're doing a native link and give an error if we're doing a bytecode
- // link.
- if (!Native) {
- PrintAndReturn(progname, "Cannot find " + Libraries[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 '" << Libraries[i] << "'\n";
-
- if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) {
- PrintAndReturn(progname, ErrorMessage,
- ": Error linking in '" + Libraries[i] + "'");
- return true;
- }
- } else {
- if (Verbose)
- std::cerr << "Linking file '" << Libraries[i] << "'\n";
-
- if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
- PrintAndReturn(progname, ErrorMessage,
- ": error linking in '" + Libraries[i] + "'");
- return true;
- }
- }
+ // Get the undefined symbols from the aggregate module. This recomputes the
+ // symbols we still need after the new modules have been linked in.
+ GetAllUndefinedSymbols(Composite, UndefinedSymbols);
+
+ // At this point we have two sets of undefined symbols: UndefinedSymbols
+ // which holds the undefined symbols from all the modules, and
+ // NotDefinedByArchive which holds symbols we know the archive doesn't
+ // define. There's no point searching for symbols that we won't find in the
+ // archive so we subtract these sets.
+ set_subtract(UndefinedSymbols, NotDefinedByArchive);
+
+ // If there's no symbols left, no point in continuing to search the
+ // archive.
+ if (UndefinedSymbols.empty())
+ break;