Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 1 | //===- gccld.cpp - LLVM 'ld' compatible linker ----------------------------===// |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // This utility is intended to be compatible with GCC, and follows standard |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 4 | // system 'ld' conventions. As such, the default output file is ./a.out. |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 5 | // Additionally, this program outputs a shell script that is used to invoke LLI |
| 6 | // to execute the program. In this manner, the generated executable (a.out for |
| 7 | // example), is directly executable, whereas the bytecode file actually lives in |
| 8 | // the a.out.bc file generated by this program. Also, Force is on by default. |
| 9 | // |
| 10 | // Note that if someone (or a script) deletes the executable program generated, |
| 11 | // the .bc file will be left around. Considering that this is a temporary hack, |
| 12 | // I'm not to worried about this. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | c8cc4cb | 2002-05-07 18:36:35 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Utils/Linker.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 18 | #include "llvm/PassManager.h" |
| 19 | #include "llvm/Bytecode/Reader.h" |
| 20 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Chris Lattner | 9c3b55e | 2003-04-24 19:13:02 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
Chris Lattner | d9d8c07 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | d9d8c07 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 24 | #include "Support/CommandLine.h" |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 25 | #include "Support/Signals.h" |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 26 | #include <fstream> |
| 27 | #include <memory> |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 28 | #include <set> |
Chris Lattner | 41c3465 | 2002-03-11 17:49:53 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 30 | #include <sys/types.h> // For FileExists |
| 31 | #include <sys/stat.h> |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 32 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | cl::list<std::string> |
| 35 | InputFilenames(cl::Positional, cl::desc("<input bytecode files>"), |
| 36 | cl::OneOrMore); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 37 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 38 | cl::opt<std::string> |
| 39 | OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"), |
| 40 | cl::value_desc("filename")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 41 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 42 | cl::opt<bool> |
| 43 | Verbose("v", cl::desc("Print information about actions taken")); |
| 44 | |
| 45 | cl::list<std::string> |
| 46 | LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix, |
| 47 | cl::value_desc("directory")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 48 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 49 | cl::list<std::string> |
| 50 | Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix, |
| 51 | cl::value_desc("library prefix")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 52 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 53 | cl::opt<bool> |
| 54 | Strip("s", cl::desc("Strip symbol info from executable")); |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 55 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 56 | cl::opt<bool> |
| 57 | NoInternalize("disable-internalize", |
| 58 | cl::desc("Do not mark all symbols as internal")); |
Chris Lattner | a856db2 | 2003-04-18 23:38:22 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 60 | cl::opt<bool> |
| 61 | LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a" |
| 62 | " library, not an executable")); |
| 63 | |
Chris Lattner | a856db2 | 2003-04-18 23:38:22 +0000 | [diff] [blame] | 64 | // Compatibility options that are ignored, but support by LD |
| 65 | cl::opt<std::string> |
| 66 | CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored")); |
| 67 | cl::opt<std::string> |
| 68 | CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored")); |
| 69 | cl::opt<bool> |
| 70 | CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored")); |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 71 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 72 | |
| 73 | // FileExists - Return true if the specified string is an openable file... |
| 74 | static inline bool FileExists(const std::string &FN) { |
| 75 | struct stat StatBuf; |
| 76 | return stat(FN.c_str(), &StatBuf) != -1; |
| 77 | } |
| 78 | |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 80 | // LoadObject - Read the specified "object file", which should not search the |
| 81 | // library path to find it. |
Chris Lattner | 7cb77e1 | 2003-05-13 22:14:13 +0000 | [diff] [blame^] | 82 | static inline std::auto_ptr<Module> LoadObject(std::string FN, |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 83 | std::string &OutErrorMessage) { |
| 84 | if (Verbose) std::cerr << "Loading '" << FN << "'\n"; |
| 85 | if (!FileExists(FN)) { |
Chris Lattner | 7cb77e1 | 2003-05-13 22:14:13 +0000 | [diff] [blame^] | 86 | // Attempt to load from the LLVM_LIB_SEARCH_PATH directory... if we would |
| 87 | // otherwise fail. This is used to locate objects like crtend.o. |
| 88 | // |
| 89 | char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"); |
| 90 | if (SearchPath && FileExists(std::string(SearchPath)+"/"+FN)) |
| 91 | FN = std::string(SearchPath)+"/"+FN; |
| 92 | else { |
| 93 | OutErrorMessage = "could not find input file '" + FN + "'!"; |
| 94 | return std::auto_ptr<Module>(); |
| 95 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 98 | std::string ErrorMessage; |
| 99 | Module *Result = ParseBytecodeFile(FN, &ErrorMessage); |
| 100 | if (Result) return std::auto_ptr<Module>(Result); |
| 101 | |
| 102 | OutErrorMessage = "Bytecode file '" + FN + "' corrupt!"; |
| 103 | if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 104 | return std::auto_ptr<Module>(); |
| 105 | } |
| 106 | |
| 107 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 108 | static Module *LoadSingleLibraryObject(const std::string &Filename) { |
| 109 | std::string ErrorMessage; |
| 110 | std::auto_ptr<Module> M = LoadObject(Filename, ErrorMessage); |
| 111 | if (M.get() == 0 && Verbose) { |
| 112 | std::cerr << "Error loading '" + Filename + "'"; |
| 113 | if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage; |
| 114 | std::cerr << "\n"; |
| 115 | } |
| 116 | |
| 117 | return M.release(); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | // LoadLibraryFromDirectory - This looks for a .a, .so, or .bc file in a |
| 122 | // particular directory. It returns true if no library is found, otherwise it |
| 123 | // puts the loaded modules into the Objects list, and sets isArchive to true if |
| 124 | // a .a file was loaded. |
| 125 | // |
| 126 | static inline bool LoadLibraryFromDirectory(const std::string &LibName, |
| 127 | const std::string &Directory, |
| 128 | std::vector<Module*> &Objects, |
| 129 | bool &isArchive) { |
| 130 | if (FileExists(Directory + "lib" + LibName + ".a")) { |
| 131 | std::string ErrorMessage; |
Chris Lattner | c65b104 | 2003-04-19 23:07:33 +0000 | [diff] [blame] | 132 | if (Verbose) std::cerr << " Loading '" << Directory << "lib" |
| 133 | << LibName << ".a'\n"; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 134 | if (!ReadArchiveFile(Directory + "lib" + LibName + ".a", Objects, |
| 135 | &ErrorMessage)) { // Read the archive file |
| 136 | isArchive = true; |
| 137 | return false; // Success! |
| 138 | } |
| 139 | |
| 140 | if (Verbose) { |
Chris Lattner | c65b104 | 2003-04-19 23:07:33 +0000 | [diff] [blame] | 141 | std::cerr << " Error loading archive '" + Directory +"lib"+LibName+".a'"; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 142 | if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage; |
| 143 | std::cerr << "\n"; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (FileExists(Directory + "lib" + LibName + ".so")) |
| 148 | if (Module *M = LoadSingleLibraryObject(Directory + "lib" + LibName+".so")){ |
| 149 | isArchive = false; |
| 150 | Objects.push_back(M); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | if (FileExists(Directory + "lib" + LibName + ".bc")) |
| 155 | if (Module *M = LoadSingleLibraryObject(Directory + "lib" + LibName+".bc")){ |
| 156 | isArchive = false; |
| 157 | Objects.push_back(M); |
| 158 | return false; |
| 159 | } |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | // LoadLibrary - This searches for a .a, .so, or .bc file which provides the |
| 164 | // LLVM bytecode for the library. It returns true if no library is found, |
| 165 | // otherwise it puts the loaded modules into the Objects list, and sets |
| 166 | // isArchive to true if a .a file was loaded. |
| 167 | // |
| 168 | static inline bool LoadLibrary(const std::string &LibName, |
| 169 | std::vector<Module*> &Objects, bool &isArchive, |
| 170 | std::string &ErrorMessage) { |
| 171 | std::string Directory; |
| 172 | unsigned NextLibPathIdx = 0; |
| 173 | |
| 174 | while (1) { |
| 175 | // Try loading from the current directory... |
| 176 | if (Verbose) std::cerr << " Looking in directory '" << Directory << "'\n"; |
| 177 | if (!LoadLibraryFromDirectory(LibName, Directory, Objects, isArchive)) |
| 178 | return false; |
| 179 | |
| 180 | if (NextLibPathIdx == LibPaths.size()) break; |
| 181 | Directory = LibPaths[NextLibPathIdx++]+"/"; |
| 182 | } |
| 183 | |
| 184 | ErrorMessage = "error linking library '-l" + LibName+ "': library not found!"; |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | static void GetAllDefinedSymbols(Module *M, |
| 189 | std::set<std::string> &DefinedSymbols) { |
| 190 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 191 | if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage()) |
| 192 | DefinedSymbols.insert(I->getName()); |
| 193 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 194 | if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage()) |
| 195 | DefinedSymbols.insert(I->getName()); |
| 196 | } |
| 197 | |
| 198 | // GetAllUndefinedSymbols - This calculates the set of undefined symbols that |
| 199 | // still exist in an LLVM module. This is a bit tricky because there may be two |
| 200 | // symbols with the same name, but different LLVM types that will be resolved to |
| 201 | // each other, but aren't currently (thus we need to treat it as resolved). |
| 202 | // |
| 203 | static void GetAllUndefinedSymbols(Module *M, |
| 204 | std::set<std::string> &UndefinedSymbols) { |
| 205 | std::set<std::string> DefinedSymbols; |
| 206 | UndefinedSymbols.clear(); // Start out empty |
| 207 | |
| 208 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 209 | if (I->hasName()) { |
| 210 | if (I->isExternal()) |
| 211 | UndefinedSymbols.insert(I->getName()); |
| 212 | else if (!I->hasInternalLinkage()) |
| 213 | DefinedSymbols.insert(I->getName()); |
| 214 | } |
| 215 | for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) |
| 216 | if (I->hasName()) { |
| 217 | if (I->isExternal()) |
| 218 | UndefinedSymbols.insert(I->getName()); |
| 219 | else if (!I->hasInternalLinkage()) |
| 220 | DefinedSymbols.insert(I->getName()); |
| 221 | } |
| 222 | |
| 223 | // Prune out any defined symbols from the undefined symbols set... |
| 224 | for (std::set<std::string>::iterator I = UndefinedSymbols.begin(); |
| 225 | I != UndefinedSymbols.end(); ) |
| 226 | if (DefinedSymbols.count(*I)) |
| 227 | UndefinedSymbols.erase(I++); // This symbol really is defined! |
| 228 | else |
| 229 | ++I; // Keep this symbol in the undefined symbols list |
| 230 | } |
| 231 | |
| 232 | |
| 233 | static bool LinkLibrary(Module *M, const std::string &LibName, |
| 234 | std::string &ErrorMessage) { |
Chris Lattner | 7cb77e1 | 2003-05-13 22:14:13 +0000 | [diff] [blame^] | 235 | std::set<std::string> UndefinedSymbols; |
| 236 | GetAllUndefinedSymbols(M, UndefinedSymbols); |
| 237 | if (UndefinedSymbols.empty()) { |
| 238 | if (Verbose) std::cerr << " No symbols undefined, don't link library!\n"; |
| 239 | return false; // No need to link anything in! |
| 240 | } |
| 241 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 242 | std::vector<Module*> Objects; |
| 243 | bool isArchive; |
| 244 | if (LoadLibrary(LibName, Objects, isArchive, ErrorMessage)) return true; |
| 245 | |
| 246 | // Figure out which symbols are defined by all of the modules in the .a file |
| 247 | std::vector<std::set<std::string> > DefinedSymbols; |
| 248 | DefinedSymbols.resize(Objects.size()); |
| 249 | for (unsigned i = 0; i != Objects.size(); ++i) |
| 250 | GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]); |
| 251 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 252 | bool Linked = true; |
| 253 | while (Linked) { // While we are linking in object files, loop. |
| 254 | Linked = false; |
| 255 | |
| 256 | for (unsigned i = 0; i != Objects.size(); ++i) { |
| 257 | // Consider whether we need to link in this module... we only need to |
| 258 | // link it in if it defines some symbol which is so far undefined. |
| 259 | // |
| 260 | const std::set<std::string> &DefSymbols = DefinedSymbols[i]; |
| 261 | |
| 262 | bool ObjectRequired = false; |
| 263 | for (std::set<std::string>::iterator I = UndefinedSymbols.begin(), |
| 264 | E = UndefinedSymbols.end(); I != E; ++I) |
| 265 | if (DefSymbols.count(*I)) { |
| 266 | if (Verbose) |
| 267 | std::cerr << " Found object providing symbol '" << *I << "'...\n"; |
| 268 | ObjectRequired = true; |
| 269 | break; |
| 270 | } |
| 271 | |
| 272 | // We DO need to link this object into the program... |
| 273 | if (ObjectRequired) { |
| 274 | if (LinkModules(M, Objects[i], &ErrorMessage)) |
| 275 | return true; // Couldn't link in the right object file... |
| 276 | |
| 277 | // Since we have linked in this object, delete it from the list of |
| 278 | // objects to consider in this archive file. |
| 279 | std::swap(Objects[i], Objects.back()); |
| 280 | std::swap(DefinedSymbols[i], DefinedSymbols.back()); |
| 281 | Objects.pop_back(); |
| 282 | DefinedSymbols.pop_back(); |
| 283 | --i; // Do not skip an entry |
| 284 | |
| 285 | // The undefined symbols set should have shrunk. |
| 286 | GetAllUndefinedSymbols(M, UndefinedSymbols); |
| 287 | Linked = true; // We have linked something in! |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | static int PrintAndReturn(const char *progname, const std::string &Message, |
| 296 | const std::string &Extra = "") { |
| 297 | std::cerr << progname << Extra << ": " << Message << "\n"; |
| 298 | return 1; |
| 299 | } |
| 300 | |
| 301 | |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 302 | int main(int argc, char **argv) { |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 303 | cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n"); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 304 | |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 305 | std::string ErrorMessage; |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 306 | std::auto_ptr<Module> Composite(LoadObject(InputFilenames[0], ErrorMessage)); |
| 307 | if (Composite.get() == 0) |
| 308 | return PrintAndReturn(argv[0], ErrorMessage); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 309 | |
Chris Lattner | d34a51d | 2003-04-21 19:53:24 +0000 | [diff] [blame] | 310 | // If the user specied an extra search path in their environment, respect it. |
| 311 | if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH")) |
| 312 | LibPaths.push_back(SearchPath); |
| 313 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 314 | for (unsigned i = 1; i < InputFilenames.size(); ++i) { |
| 315 | std::auto_ptr<Module> M(LoadObject(InputFilenames[i], ErrorMessage)); |
| 316 | if (M.get() == 0) |
| 317 | return PrintAndReturn(argv[0], ErrorMessage); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 318 | |
Chris Lattner | f3d4f17 | 2003-04-18 23:01:25 +0000 | [diff] [blame] | 319 | if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n"; |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 320 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 321 | if (LinkModules(Composite.get(), M.get(), &ErrorMessage)) |
| 322 | return PrintAndReturn(argv[0], ErrorMessage, |
| 323 | ": error linking in '" + InputFilenames[i] + "'"); |
| 324 | } |
| 325 | |
Chris Lattner | c65b104 | 2003-04-19 23:07:33 +0000 | [diff] [blame] | 326 | // Remove any consecutive duplicates of the same library... |
| 327 | Libraries.erase(std::unique(Libraries.begin(), Libraries.end()), |
| 328 | Libraries.end()); |
| 329 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 330 | // Link in all of the libraries next... |
| 331 | for (unsigned i = 0; i != Libraries.size(); ++i) { |
| 332 | if (Verbose) std::cerr << "Linking in library: -l" << Libraries[i] << "\n"; |
| 333 | if (LinkLibrary(Composite.get(), Libraries[i], ErrorMessage)) |
| 334 | return PrintAndReturn(argv[0], ErrorMessage); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Chris Lattner | f8b90ee | 2002-04-10 20:37:47 +0000 | [diff] [blame] | 337 | // In addition to just linking the input from GCC, we also want to spiff it up |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 338 | // a little bit. Do this now. |
| 339 | // |
| 340 | PassManager Passes; |
| 341 | |
Chris Lattner | 9c3b55e | 2003-04-24 19:13:02 +0000 | [diff] [blame] | 342 | // Add an appropriate TargetData instance for this module... |
| 343 | Passes.add(new TargetData("gccas", Composite.get())); |
| 344 | |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 345 | // Linking modules together can lead to duplicated global constants, only keep |
| 346 | // one copy of each constant... |
| 347 | // |
| 348 | Passes.add(createConstantMergePass()); |
| 349 | |
Chris Lattner | 2b59837 | 2002-04-08 05:18:12 +0000 | [diff] [blame] | 350 | // If the -s command line option was specified, strip the symbols out of the |
| 351 | // resulting program to make it smaller. -s is a GCC option that we are |
| 352 | // supporting. |
| 353 | // |
| 354 | if (Strip) |
| 355 | Passes.add(createSymbolStrippingPass()); |
| 356 | |
Chris Lattner | f8b90ee | 2002-04-10 20:37:47 +0000 | [diff] [blame] | 357 | // Often if the programmer does not specify proper prototypes for the |
| 358 | // functions they are calling, they end up calling a vararg version of the |
| 359 | // function that does not get a body filled in (the real function has typed |
| 360 | // arguments). This pass merges the two functions. |
| 361 | // |
| 362 | Passes.add(createFunctionResolvingPass()); |
| 363 | |
Chris Lattner | dabaa46 | 2003-04-16 21:43:22 +0000 | [diff] [blame] | 364 | if (!NoInternalize) { |
| 365 | // Now that composite has been compiled, scan through the module, looking |
| 366 | // for a main function. If main is defined, mark all other functions |
| 367 | // internal. |
| 368 | // |
| 369 | Passes.add(createInternalizePass()); |
| 370 | } |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 371 | |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 372 | // Now that we have optimized the program, discard unreachable functions... |
| 373 | // |
| 374 | Passes.add(createGlobalDCEPass()); |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 375 | |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 376 | // Add the pass that writes bytecode to the output file... |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 377 | std::string RealBytecodeOutput = OutputFilename; |
| 378 | if (!LinkAsLibrary) RealBytecodeOutput += ".bc"; |
| 379 | std::ofstream Out(RealBytecodeOutput.c_str()); |
| 380 | if (!Out.good()) |
| 381 | return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput + |
| 382 | "' for writing!"); |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 383 | Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file... |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 385 | // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 386 | RemoveFileOnSignal(RealBytecodeOutput); |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 387 | |
Chris Lattner | ad202a0 | 2002-04-08 00:14:58 +0000 | [diff] [blame] | 388 | // Run our queue of passes all at once now, efficiently. |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 389 | Passes.run(*Composite.get()); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 390 | Out.close(); |
| 391 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 392 | if (!LinkAsLibrary) { |
| 393 | // Output the script to start the program... |
| 394 | std::ofstream Out2(OutputFilename.c_str()); |
| 395 | if (!Out2.good()) |
| 396 | return PrintAndReturn(argv[0], "error opening '" + OutputFilename + |
| 397 | "' for writing!"); |
| 398 | Out2 << "#!/bin/sh\nlli -q -abort-on-exception $0.bc $*\n"; |
| 399 | Out2.close(); |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 400 | |
Chris Lattner | 10970eb | 2003-04-19 22:44:38 +0000 | [diff] [blame] | 401 | // Make the script executable... |
| 402 | chmod(OutputFilename.c_str(), 0755); |
| 403 | } |
Chris Lattner | e7fca51 | 2002-01-24 19:12:12 +0000 | [diff] [blame] | 404 | |
| 405 | return 0; |
| 406 | } |