1 //===- util.cpp - Utility functions ---------------------------------------===//
3 // This file contains utility functions for gccld. It essentially holds
4 // anything from the original gccld.cpp source that was either incidental
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Module.h"
10 #include "Config/string.h"
17 // Function: PrintAndReturn ()
20 // Prints a message (usually error message) to standard error (stderr) and
21 // returns a value usable for an exit status.
24 // progname - The name of the program (i.e. argv[0]).
25 // Message - The message to print to standard error.
26 // Extra - Extra information to print between the program name and thei
27 // message. It is optional.
33 // Returns a value that can be used as the exit status (i.e. for exit()).
36 PrintAndReturn (const char *progname,
37 const std::string &Message,
38 const std::string &Extra = "")
40 std::cerr << progname << Extra << ": " << Message << "\n";
45 // Function: IsArchive ()
48 // Determine if the specified file is an ar archive. It determines this by
49 // checking the magic string at the beginning of the file.
52 // filename - A C++ string containing the name of the file.
58 // TRUE - The file is an archive.
59 // FALSE - The file is not an archive.
62 IsArchive (const std::string &filename)
64 std::string ArchiveMagic("!<arch>\012");
65 char buf[1 + ArchiveMagic.size()];
67 std::ifstream f(filename.c_str());
68 f.read(buf, ArchiveMagic.size());
69 buf[ArchiveMagic.size()] = '\0';
70 return ArchiveMagic == buf;
74 // Function: GetAllDefinedSymbols ()
77 // Find all of the defined symbols in the specified module.
80 // M - The module in which to find defined symbols.
83 // DefinedSymbols - A set of C++ strings that will contain the name of all
90 GetAllDefinedSymbols (Module *M, std::set<std::string> &DefinedSymbols)
92 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
93 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
94 DefinedSymbols.insert(I->getName());
95 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
96 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
97 DefinedSymbols.insert(I->getName());
101 // Function: GetAllUndefinedSymbols ()
104 // This calculates the set of undefined symbols that still exist in an LLVM
105 // module. This is a bit tricky because there may be two symbols with the
106 // same name but different LLVM types that will be resolved to each other but
107 // aren't currently (thus we need to treat it as resolved).
110 // M - The module in which to find undefined symbols.
113 // UndefinedSymbols - A set of C++ strings containing the name of all
114 // undefined symbols.
120 GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols)
122 std::set<std::string> DefinedSymbols;
123 UndefinedSymbols.clear(); // Start out empty
125 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
128 UndefinedSymbols.insert(I->getName());
129 else if (!I->hasInternalLinkage())
130 DefinedSymbols.insert(I->getName());
132 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
135 UndefinedSymbols.insert(I->getName());
136 else if (!I->hasInternalLinkage())
137 DefinedSymbols.insert(I->getName());
140 // Prune out any defined symbols from the undefined symbols set...
141 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
142 I != UndefinedSymbols.end(); )
143 if (DefinedSymbols.count(*I))
144 UndefinedSymbols.erase(I++); // This symbol really is defined!
146 ++I; // Keep this symbol in the undefined symbols list
151 // Function: copy_env()
154 // This function takes an array of environment variables and makes a
155 // copy of it. This copy can then be manipulated any way the caller likes
156 // without affecting the process's real environment.
159 // envp - An array of C strings containing an environment.
165 // NULL - An error occurred.
167 // Otherwise, a pointer to a new array of C strings is returned. Every string
168 // in the array is a duplicate of the one in the original array (i.e. we do
169 // not copy the char *'s from one array to another).
172 copy_env (char ** const envp)
174 // The new environment list
177 // The number of entries in the old environment list
181 // Count the number of entries in the old list;
183 for (entries = 0; envp[entries] != NULL; entries++)
189 // Add one more entry for the NULL pointer that ends the list.
194 // If there are no entries at all, just return NULL.
202 // Allocate a new environment list.
204 if ((newenv = new (char *) [entries]) == NULL)
210 // Make a copy of the list. Don't forget the NULL that ends the list.
213 while (envp[entries] != NULL)
215 newenv[entries] = new char[strlen (envp[entries]) + 1];
216 strcpy (newenv[entries], envp[entries]);
219 newenv[entries] = NULL;
226 // Function: remove_env()
229 // Remove the specified environment variable from the environment array.
232 // name - The name of the variable to remove. It cannot be NULL.
233 // envp - The array of environment variables. It cannot be NULL.
236 // envp - The pointer to the specified variable name is removed.
242 // This is mainly done because functions to remove items from the environment
243 // are not available across all platforms. In particular, Solaris does not
244 // seem to have an unsetenv() function or a setenv() function (or they are
245 // undocumented if they do exist).
248 remove_env (const char * name, char ** const envp)
250 // Pointer for scanning arrays
253 // Index for selecting elements of the environment array
256 for (index=0; envp[index] != NULL; index++)
259 // Find the first equals sign in the array and make it an EOS character.
261 p = strchr (envp[index], '=');
272 // Compare the two strings. If they are equal, zap this string.
273 // Otherwise, restore it.
275 if (!strcmp (name, envp[index]))