2009-01-20 Sriraman Tallam <tmsriram@google.com>
[deliverable/binutils-gdb.git] / gold / main.cc
1 // main.cc -- gold main function.
2
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #include "gold.h"
24
25 #include <cstdio>
26 #include <cstring>
27
28 #ifdef HAVE_MALLINFO
29 #include <malloc.h>
30 #endif
31
32 #include "libiberty.h"
33
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 "archive.h"
43 #include "symtab.h"
44 #include "layout.h"
45 #include "plugin.h"
46 #include "gc.h"
47
48 using namespace gold;
49
50 // This function emits the commandline to a hard-coded file in temp.
51 // This is useful for debugging since ld is typically invoked by gcc,
52 // so its commandline is not always easy to extract. You should be
53 // able to run 'gcc -B... foo.o -o foo' to invoke this linker the
54 // first time, and then /tmp/ld-run-foo.sh to invoke it on subsequent
55 // runes. "/tmp/ld-run-foo.sh debug" will run the linker inside gdb
56 // (or whatever value the environment variable GDB is set to), for
57 // even easier debugging. Since this is a debugging-only tool, and
58 // creates files, it is only turned on when the user explicitly asks
59 // for it, by compiling with -DDEBUG. Do not do this for release
60 // versions of the linker!
61
62 #ifdef DEBUG
63 #include <stdio.h>
64 #include <sys/stat.h> // for chmod()
65
66 static std::string
67 collect_argv(int argc, char** argv)
68 {
69 // This is used by write_debug_script(), which wants the unedited argv.
70 std::string args;
71 for (int i = 0; i < argc; ++i)
72 {
73 args.append(" '");
74 // Now append argv[i], but with all single-quotes escaped
75 const char* argpos = argv[i];
76 while (1)
77 {
78 const int len = strcspn(argpos, "'");
79 args.append(argpos, len);
80 if (argpos[len] == '\0')
81 break;
82 args.append("'\"'\"'");
83 argpos += len + 1;
84 }
85 args.append("'");
86 }
87 return args;
88 }
89
90 static void
91 write_debug_script(std::string filename_str,
92 const char* argv_0, const char* args)
93 {
94 size_t slash = filename_str.rfind('/');
95 if (slash != std::string::npos)
96 filename_str = filename_str.c_str() + slash + 1;
97 filename_str = std::string("/tmp/ld-run-") + filename_str + ".sh";
98 const char* filename = filename_str.c_str();
99 FILE* fp = fopen(filename, "w");
100 if (fp)
101 {
102 fprintf(fp, "[ \"$1\" = debug ]"
103 " && PREFIX=\"${GDB-gdb} --annotate=3 --fullname %s --args\""
104 " && shift\n",
105 argv_0);
106 fprintf(fp, "$PREFIX%s $*\n", args);
107 fclose(fp);
108 chmod(filename, 0755);
109 }
110 else
111 filename = "[none]";
112 fprintf(stderr, "Welcome to gold! Commandline written to %s.\n", filename);
113 fflush(stderr);
114 }
115
116 #else // !defined(DEBUG)
117
118 static inline std::string
119 collect_argv(int, char**)
120 {
121 return "";
122 }
123
124 static inline void
125 write_debug_script(std::string, const char*, const char*)
126 {
127 }
128
129 #endif // !defined(DEBUG)
130
131
132 int
133 main(int argc, char** argv)
134 {
135 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
136 setlocale (LC_MESSAGES, "");
137 #endif
138 #if defined (HAVE_SETLOCALE)
139 setlocale (LC_CTYPE, "");
140 #endif
141 bindtextdomain (PACKAGE, LOCALEDIR);
142 textdomain (PACKAGE);
143
144 program_name = argv[0];
145
146 // In libiberty; expands @filename to the args in "filename".
147 expandargv(&argc, &argv);
148
149 // This is used by write_debug_script(), which wants the unedited argv.
150 std::string args = collect_argv(argc, argv);
151
152 Errors errors(program_name);
153
154 // Initialize the global parameters, to let random code get to the
155 // errors object.
156 set_parameters_errors(&errors);
157
158 // Handle the command line options.
159 Command_line command_line;
160 command_line.process(argc - 1, const_cast<const char**>(argv + 1));
161
162 long start_time = 0;
163 if (command_line.options().stats())
164 start_time = get_run_time();
165
166 // Store some options in the globally accessible parameters.
167 set_parameters_options(&command_line.options());
168
169 // Do this as early as possible (since it prints a welcome message).
170 write_debug_script(command_line.options().output_file_name(),
171 program_name, args.c_str());
172
173 // If the user asked for a map file, open it.
174 Mapfile* mapfile = NULL;
175 if (command_line.options().user_set_Map())
176 {
177 mapfile = new Mapfile();
178 if (!mapfile->open(command_line.options().Map()))
179 {
180 delete mapfile;
181 mapfile = NULL;
182 }
183 }
184
185 // The GNU linker ignores version scripts when generating
186 // relocatable output. If we are not compatible, then we break the
187 // Linux kernel build, which uses a linker script with -r which must
188 // not force symbols to be local. It would actually be useful to
189 // permit symbols to be forced local with -r, though, as it would
190 // permit some linker optimizations. Perhaps we need yet another
191 // option to control this. FIXME.
192 if (parameters->options().relocatable())
193 command_line.script_options().version_script_info()->clear();
194
195 // Load plugin libraries.
196 if (command_line.options().has_plugins())
197 command_line.options().plugins()->load_plugins();
198
199 // The work queue.
200 Workqueue workqueue(command_line.options());
201
202 // The list of input objects.
203 Input_objects input_objects;
204
205 // The Garbage Collection Object.
206 Garbage_collection gc;
207
208 // The symbol table. We're going to guess here how many symbols
209 // we're going to see based on the number of input files. Even when
210 // this is off, it means at worst we don't quite optimize hashtable
211 // resizing as well as we could have (perhap using more memory).
212 Symbol_table symtab(command_line.number_of_input_files() * 1024,
213 command_line.version_script());
214
215 if (parameters->options().gc_sections())
216 symtab.set_gc(&gc);
217
218 // The layout object.
219 Layout layout(command_line.options(), &command_line.script_options());
220
221 // Get the search path from the -L options.
222 Dirsearch search_path;
223 search_path.initialize(&workqueue, &command_line.options().library_path());
224
225 // Queue up the first set of tasks.
226 queue_initial_tasks(command_line.options(), search_path,
227 command_line, &workqueue, &input_objects,
228 &symtab, &layout, mapfile);
229
230 // Run the main task processing loop.
231 workqueue.process(0);
232
233 if (command_line.options().stats())
234 {
235 long run_time = get_run_time() - start_time;
236 fprintf(stderr, _("%s: total run time: %ld.%06ld seconds\n"),
237 program_name, run_time / 1000000, run_time % 1000000);
238 #ifdef HAVE_MALLINFO
239 struct mallinfo m = mallinfo();
240 fprintf(stderr, _("%s: total space allocated by malloc: %d bytes\n"),
241 program_name, m.arena);
242 #endif
243 File_read::print_stats();
244 Archive::print_stats();
245 fprintf(stderr, _("%s: output file size: %lld bytes\n"),
246 program_name, static_cast<long long>(layout.output_file_size()));
247 symtab.print_stats();
248 layout.print_stats();
249 }
250
251 if (mapfile != NULL)
252 mapfile->close();
253
254 // Issue defined symbol report.
255 if (command_line.options().user_set_print_symbol_counts())
256 input_objects.print_symbol_counts(&symtab);
257
258 if (parameters->options().fatal_warnings()
259 && errors.warning_count() > 0
260 && errors.error_count() == 0)
261 gold_error("treating warnings as errors");
262
263 // If the user used --noinhibit-exec, we force the exit status to be
264 // successful. This is compatible with GNU ld.
265 gold_exit(errors.error_count() == 0
266 || parameters->options().noinhibit_exec());
267 }
This page took 0.036366 seconds and 5 git commands to generate.