2008-07-30 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / gold / main.cc
blobe10600b195da3650f7eac2775eff8288e022422e
1 // main.cc -- gold main function.
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #include "gold.h"
25 #include <cstdio>
26 #include <cstring>
28 #ifdef HAVE_MALLINFO
29 #include <malloc.h>
30 #endif
32 #include "libiberty.h"
34 #include "script.h"
35 #include "options.h"
36 #include "parameters.h"
37 #include "errors.h"
38 #include "mapfile.h"
39 #include "dirsearch.h"
40 #include "workqueue.h"
41 #include "object.h"
42 #include "symtab.h"
43 #include "layout.h"
45 using namespace gold;
47 // This function emits the commandline to a hard-coded file in temp.
48 // This is useful for debugging since ld is typically invoked by gcc,
49 // so its commandline is not always easy to extract. You should be
50 // able to run 'gcc -B... foo.o -o foo' to invoke this linker the
51 // first time, and then /tmp/ld-run-foo.sh to invoke it on subsequent
52 // runes. "/tmp/ld-run-foo.sh debug" will run the linker inside gdb
53 // (or whatever value the environment variable GDB is set to), for
54 // even easier debugging. Since this is a debugging-only tool, and
55 // creates files, it is only turned on when the user explicitly asks
56 // for it, by compiling with -DDEBUG. Do not do this for release
57 // versions of the linker!
59 #ifdef DEBUG
60 #include <stdio.h>
61 #include <sys/stat.h> // for chmod()
63 static std::string
64 collect_argv(int argc, char** argv)
66 // This is used by write_debug_script(), which wants the unedited argv.
67 std::string args;
68 for (int i = 0; i < argc; ++i)
70 args.append(" '");
71 // Now append argv[i], but with all single-quotes escaped
72 const char* argpos = argv[i];
73 while (1)
75 const int len = strcspn(argpos, "'");
76 args.append(argpos, len);
77 if (argpos[len] == '\0')
78 break;
79 args.append("'\"'\"'");
80 argpos += len + 1;
82 args.append("'");
84 return args;
87 static void
88 write_debug_script(std::string filename_str,
89 const char* argv_0, const char* args)
91 size_t slash = filename_str.rfind('/');
92 if (slash != std::string::npos)
93 filename_str = filename_str.c_str() + slash + 1;
94 filename_str = std::string("/tmp/ld-run-") + filename_str + ".sh";
95 const char* filename = filename_str.c_str();
96 FILE* fp = fopen(filename, "w");
97 if (fp)
99 fprintf(fp, "[ \"$1\" = debug ]"
100 " && PREFIX=\"${GDB-gdb} --annotate=3 --fullname %s --args\""
101 " && shift\n",
102 argv_0);
103 fprintf(fp, "$PREFIX%s $*\n", args);
104 fclose(fp);
105 chmod(filename, 0755);
107 else
108 filename = "[none]";
109 fprintf(stderr, "Welcome to gold! Commandline written to %s.\n", filename);
110 fflush(stderr);
113 #else // !defined(DEBUG)
115 static inline std::string
116 collect_argv(int, char**)
118 return "";
121 static inline void
122 write_debug_script(std::string, const char*, const char*)
126 #endif // !defined(DEBUG)
130 main(int argc, char** argv)
132 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
133 setlocale (LC_MESSAGES, "");
134 #endif
135 #if defined (HAVE_SETLOCALE)
136 setlocale (LC_CTYPE, "");
137 #endif
138 bindtextdomain (PACKAGE, LOCALEDIR);
139 textdomain (PACKAGE);
141 program_name = argv[0];
143 // In libiberty; expands @filename to the args in "filename".
144 expandargv(&argc, &argv);
146 // This is used by write_debug_script(), which wants the unedited argv.
147 std::string args = collect_argv(argc, argv);
149 Errors errors(program_name);
151 // Initialize the global parameters, to let random code get to the
152 // errors object.
153 set_parameters_errors(&errors);
155 // Handle the command line options.
156 Command_line command_line;
157 command_line.process(argc - 1, const_cast<const char**>(argv + 1));
159 long start_time = 0;
160 if (command_line.options().stats())
161 start_time = get_run_time();
163 // Store some options in the globally accessible parameters.
164 set_parameters_options(&command_line.options());
166 // Do this as early as possible (since it prints a welcome message).
167 write_debug_script(command_line.options().output_file_name(),
168 program_name, args.c_str());
170 // If the user asked for a map file, open it.
171 Mapfile* mapfile = NULL;
172 if (command_line.options().user_set_Map())
174 mapfile = new Mapfile();
175 if (!mapfile->open(command_line.options().Map()))
177 delete mapfile;
178 mapfile = NULL;
182 // The GNU linker ignores version scripts when generating
183 // relocatable output. If we are not compatible, then we break the
184 // Linux kernel build, which uses a linker script with -r which must
185 // not force symbols to be local. It would actually be useful to
186 // permit symbols to be forced local with -r, though, as it would
187 // permit some linker optimizations. Perhaps we need yet another
188 // option to control this. FIXME.
189 if (parameters->options().relocatable())
190 command_line.script_options().version_script_info()->clear();
192 // The work queue.
193 Workqueue workqueue(command_line.options());
195 // The list of input objects.
196 Input_objects input_objects;
198 // The symbol table. We're going to guess here how many symbols
199 // we're going to see based on the number of input files. Even when
200 // this is off, it means at worst we don't quite optimize hashtable
201 // resizing as well as we could have (perhap using more memory).
202 Symbol_table symtab(command_line.number_of_input_files() * 1024,
203 command_line.version_script());
205 // The layout object.
206 Layout layout(command_line.options(), &command_line.script_options());
208 // Get the search path from the -L options.
209 Dirsearch search_path;
210 search_path.initialize(&workqueue, &command_line.options().library_path());
212 // Queue up the first set of tasks.
213 queue_initial_tasks(command_line.options(), search_path,
214 command_line, &workqueue, &input_objects,
215 &symtab, &layout, mapfile);
217 // Run the main task processing loop.
218 workqueue.process(0);
220 if (command_line.options().stats())
222 long run_time = get_run_time() - start_time;
223 fprintf(stderr, _("%s: total run time: %ld.%06ld seconds\n"),
224 program_name, run_time / 1000000, run_time % 1000000);
225 #ifdef HAVE_MALLINFO
226 struct mallinfo m = mallinfo();
227 fprintf(stderr, _("%s: total space allocated by malloc: %d bytes\n"),
228 program_name, m.arena);
229 #endif
230 File_read::print_stats();
231 fprintf(stderr, _("%s: output file size: %lld bytes\n"),
232 program_name, static_cast<long long>(layout.output_file_size()));
233 symtab.print_stats();
234 layout.print_stats();
237 if (mapfile != NULL)
238 mapfile->close();
240 // Issue defined symbol report.
241 if (command_line.options().user_set_print_symbol_counts())
242 input_objects.print_symbol_counts(&symtab);
244 if (parameters->options().fatal_warnings()
245 && errors.warning_count() > 0
246 && errors.error_count() == 0)
247 gold_error("treating warnings as errors");
249 // If the user used --noinhibit-exec, we force the exit status to be
250 // successful. This is compatible with GNU ld.
251 gold_exit(errors.error_count() == 0
252 || parameters->options().noinhibit_exec());