SEGV during AX eval of OP_DOUBLE (unsupported)
[deliverable/binutils-gdb.git] / gold / main.cc
CommitLineData
5a6f7e2d
ILT
1// main.cc -- gold main function.
2
f1ddb600 3// Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6cb15b7f
ILT
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
5a6f7e2d
ILT
23#include "gold.h"
24
7d9e3d98 25#include <cstdio>
04bf7072
ILT
26#include <cstring>
27
e44fcf3b
ILT
28#ifdef HAVE_MALLINFO
29#include <malloc.h>
30#endif
04bf7072 31
e44fcf3b
ILT
32#include "libiberty.h"
33
e5756efb 34#include "script.h"
5a6f7e2d 35#include "options.h"
f1ddb600 36#include "target-select.h"
7e1edb90 37#include "parameters.h"
75f2446e 38#include "errors.h"
7d9e3d98 39#include "mapfile.h"
5a6f7e2d
ILT
40#include "dirsearch.h"
41#include "workqueue.h"
42#include "object.h"
ac45a351 43#include "archive.h"
5a6f7e2d
ILT
44#include "symtab.h"
45#include "layout.h"
89fc3421 46#include "plugin.h"
6d03d481 47#include "gc.h"
ef15dade 48#include "icf.h"
3ce2c28e 49#include "incremental.h"
d675ff46 50#include "timer.h"
5a6f7e2d
ILT
51
52using namespace gold;
53
cbcc4140
ILT
54// This function emits the commandline to a hard-coded file in temp.
55// This is useful for debugging since ld is typically invoked by gcc,
56// so its commandline is not always easy to extract. You should be
57// able to run 'gcc -B... foo.o -o foo' to invoke this linker the
58// first time, and then /tmp/ld-run-foo.sh to invoke it on subsequent
59// runes. "/tmp/ld-run-foo.sh debug" will run the linker inside gdb
60// (or whatever value the environment variable GDB is set to), for
61// even easier debugging. Since this is a debugging-only tool, and
62// creates files, it is only turned on when the user explicitly asks
63// for it, by compiling with -DDEBUG. Do not do this for release
64// versions of the linker!
65
66#ifdef DEBUG
67#include <stdio.h>
68#include <sys/stat.h> // for chmod()
69
70static std::string
71collect_argv(int argc, char** argv)
72{
73 // This is used by write_debug_script(), which wants the unedited argv.
74 std::string args;
75 for (int i = 0; i < argc; ++i)
76 {
77 args.append(" '");
78 // Now append argv[i], but with all single-quotes escaped
79 const char* argpos = argv[i];
80 while (1)
81 {
82 const int len = strcspn(argpos, "'");
83 args.append(argpos, len);
84 if (argpos[len] == '\0')
85 break;
86 args.append("'\"'\"'");
87 argpos += len + 1;
88 }
89 args.append("'");
90 }
91 return args;
92}
93
94static void
95write_debug_script(std::string filename_str,
96 const char* argv_0, const char* args)
97{
98 size_t slash = filename_str.rfind('/');
99 if (slash != std::string::npos)
100 filename_str = filename_str.c_str() + slash + 1;
55a93433 101 filename_str = std::string("/tmp/ld-run-") + filename_str + ".sh";
cbcc4140
ILT
102 const char* filename = filename_str.c_str();
103 FILE* fp = fopen(filename, "w");
104 if (fp)
105 {
fc955173
ILT
106 fprintf(fp, "[ \"$1\" = debug ]"
107 " && PREFIX=\"${GDB-gdb} --annotate=3 --fullname %s --args\""
108 " && shift\n",
109 argv_0);
cbcc4140
ILT
110 fprintf(fp, "$PREFIX%s $*\n", args);
111 fclose(fp);
112 chmod(filename, 0755);
113 }
114 else
115 filename = "[none]";
116 fprintf(stderr, "Welcome to gold! Commandline written to %s.\n", filename);
117 fflush(stderr);
118}
119
120#else // !defined(DEBUG)
121
122static inline std::string
123collect_argv(int, char**)
124{
125 return "";
126}
127
128static inline void
129write_debug_script(std::string, const char*, const char*)
130{
131}
132
133#endif // !defined(DEBUG)
134
135
5a6f7e2d
ILT
136int
137main(int argc, char** argv)
138{
139#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
ca09d69a 140 setlocale(LC_MESSAGES, "");
5a6f7e2d
ILT
141#endif
142#if defined (HAVE_SETLOCALE)
ca09d69a 143 setlocale(LC_CTYPE, "");
5a6f7e2d 144#endif
ca09d69a
NC
145 bindtextdomain(PACKAGE, LOCALEDIR);
146 textdomain(PACKAGE);
5a6f7e2d
ILT
147
148 program_name = argv[0];
149
fc955173
ILT
150 // In libiberty; expands @filename to the args in "filename".
151 expandargv(&argc, &argv);
152
cbcc4140
ILT
153 // This is used by write_debug_script(), which wants the unedited argv.
154 std::string args = collect_argv(argc, argv);
155
75f2446e
ILT
156 Errors errors(program_name);
157
3c2fafa5
ILT
158 // Initialize the global parameters, to let random code get to the
159 // errors object.
8851ecca 160 set_parameters_errors(&errors);
3c2fafa5 161
5a6f7e2d 162 // Handle the command line options.
a5dc0706 163 Command_line command_line;
ee1fe73e 164 command_line.process(argc - 1, const_cast<const char**>(argv + 1));
e44fcf3b 165
d675ff46 166 Timer timer;
7cc619c3 167 if (command_line.options().stats())
b490c0bb
CC
168 {
169 timer.start();
170 set_parameters_timer(&timer);
171 }
e44fcf3b 172
3c2fafa5 173 // Store some options in the globally accessible parameters.
8851ecca 174 set_parameters_options(&command_line.options());
5a6f7e2d 175
cbcc4140
ILT
176 // Do this as early as possible (since it prints a welcome message).
177 write_debug_script(command_line.options().output_file_name(),
178 program_name, args.c_str());
179
7d9e3d98
ILT
180 // If the user asked for a map file, open it.
181 Mapfile* mapfile = NULL;
182 if (command_line.options().user_set_Map())
183 {
184 mapfile = new Mapfile();
185 if (!mapfile->open(command_line.options().Map()))
186 {
187 delete mapfile;
188 mapfile = NULL;
189 }
190 }
191
1ef1f3d3
ILT
192 // The GNU linker ignores version scripts when generating
193 // relocatable output. If we are not compatible, then we break the
194 // Linux kernel build, which uses a linker script with -r which must
195 // not force symbols to be local. It would actually be useful to
196 // permit symbols to be forced local with -r, though, as it would
197 // permit some linker optimizations. Perhaps we need yet another
198 // option to control this. FIXME.
8851ecca 199 if (parameters->options().relocatable())
a5dc0706 200 command_line.script_options().version_script_info()->clear();
1ef1f3d3 201
5a6f7e2d
ILT
202 // The work queue.
203 Workqueue workqueue(command_line.options());
204
205 // The list of input objects.
206 Input_objects input_objects;
207
ef15dade 208 // The Garbage Collection (GC, --gc-sections) Object.
6d03d481
ST
209 Garbage_collection gc;
210
ef15dade
ST
211 // The Identical Code Folding (ICF, --icf) Object.
212 Icf icf;
213
6d013333
ILT
214 // The symbol table. We're going to guess here how many symbols
215 // we're going to see based on the number of input files. Even when
cbcc4140 216 // this is off, it means at worst we don't quite optimize hashtable
9b547ce6 217 // resizing as well as we could have (perhaps using more memory).
09124467 218 Symbol_table symtab(command_line.number_of_input_files() * 1024,
a5dc0706 219 command_line.version_script());
5a6f7e2d 220
6d03d481
ST
221 if (parameters->options().gc_sections())
222 symtab.set_gc(&gc);
223
032ce4e9 224 if (parameters->options().icf_enabled())
ef15dade
ST
225 symtab.set_icf(&icf);
226
5a6f7e2d 227 // The layout object.
e55bde5e
ILT
228 Layout layout(command_line.number_of_input_files(),
229 &command_line.script_options());
5a6f7e2d 230
3ce2c28e 231 if (layout.incremental_inputs() != NULL)
09ec0418 232 layout.incremental_inputs()->report_command_line(argc, argv);
3ce2c28e 233
6e9ba2ca
ST
234 if (parameters->options().section_ordering_file())
235 layout.read_layout_from_file();
236
e9552f7e
ST
237 // Load plugin libraries.
238 if (command_line.options().has_plugins())
239 command_line.options().plugins()->load_plugins(&layout);
240
5a6f7e2d
ILT
241 // Get the search path from the -L options.
242 Dirsearch search_path;
7cc619c3 243 search_path.initialize(&workqueue, &command_line.options().library_path());
5a6f7e2d
ILT
244
245 // Queue up the first set of tasks.
246 queue_initial_tasks(command_line.options(), search_path,
247 command_line, &workqueue, &input_objects,
7d9e3d98 248 &symtab, &layout, mapfile);
5a6f7e2d
ILT
249
250 // Run the main task processing loop.
17a1d0a9 251 workqueue.process(0);
5a6f7e2d 252
f1ddb600
ILT
253 if (command_line.options().print_output_format())
254 print_output_format();
255
7cc619c3 256 if (command_line.options().stats())
e44fcf3b 257 {
b490c0bb
CC
258 timer.stamp(2);
259 Timer::TimeStats elapsed = timer.get_pass_time(0);
260 fprintf(stderr,
261 _("%s: initial tasks run time: " \
262 "(user: %ld.%06ld sys: %ld.%06ld wall: %ld.%06ld)\n"),
263 program_name,
264 elapsed.user / 1000, (elapsed.user % 1000) * 1000,
265 elapsed.sys / 1000, (elapsed.sys % 1000) * 1000,
266 elapsed.wall / 1000, (elapsed.wall % 1000) * 1000);
267 elapsed = timer.get_pass_time(1);
268 fprintf(stderr,
269 _("%s: middle tasks run time: " \
270 "(user: %ld.%06ld sys: %ld.%06ld wall: %ld.%06ld)\n"),
271 program_name,
272 elapsed.user / 1000, (elapsed.user % 1000) * 1000,
273 elapsed.sys / 1000, (elapsed.sys % 1000) * 1000,
274 elapsed.wall / 1000, (elapsed.wall % 1000) * 1000);
275 elapsed = timer.get_pass_time(2);
276 fprintf(stderr,
277 _("%s: final tasks run time: " \
278 "(user: %ld.%06ld sys: %ld.%06ld wall: %ld.%06ld)\n"),
279 program_name,
280 elapsed.user / 1000, (elapsed.user % 1000) * 1000,
281 elapsed.sys / 1000, (elapsed.sys % 1000) * 1000,
282 elapsed.wall / 1000, (elapsed.wall % 1000) * 1000);
283 elapsed = timer.get_elapsed_time();
d675ff46
RÁE
284 fprintf(stderr,
285 _("%s: total run time: " \
286 "(user: %ld.%06ld sys: %ld.%06ld wall: %ld.%06ld)\n"),
287 program_name,
288 elapsed.user / 1000, (elapsed.user % 1000) * 1000,
3e1b9a8a 289 elapsed.sys / 1000, (elapsed.sys % 1000) * 1000,
d675ff46
RÁE
290 elapsed.wall / 1000, (elapsed.wall % 1000) * 1000);
291
e44fcf3b
ILT
292#ifdef HAVE_MALLINFO
293 struct mallinfo m = mallinfo();
294 fprintf(stderr, _("%s: total space allocated by malloc: %d bytes\n"),
295 program_name, m.arena);
296#endif
297 File_read::print_stats();
ac45a351 298 Archive::print_stats();
b0193076 299 Lib_group::print_stats();
e44fcf3b
ILT
300 fprintf(stderr, _("%s: output file size: %lld bytes\n"),
301 program_name, static_cast<long long>(layout.output_file_size()));
abaa3995 302 symtab.print_stats();
ad8f37d1 303 layout.print_stats();
cdc29364 304 Free_list::print_stats();
e44fcf3b
ILT
305 }
306
92de84a6
ILT
307 // Issue defined symbol report.
308 if (command_line.options().user_set_print_symbol_counts())
309 input_objects.print_symbol_counts(&symtab);
310
dde3f402
ILT
311 // Output cross reference table.
312 if (command_line.options().cref())
313 input_objects.print_cref(&symtab,
314 mapfile == NULL ? stdout : mapfile->file());
315
316 if (mapfile != NULL)
317 mapfile->close();
318
d82a5bcc
ILT
319 if (parameters->options().fatal_warnings()
320 && errors.warning_count() > 0
321 && errors.error_count() == 0)
322 gold_error("treating warnings as errors");
323
cdb0b8f5
ILT
324 // If the user used --noinhibit-exec, we force the exit status to be
325 // successful. This is compatible with GNU ld.
e6455dfb
CC
326 gold_exit((errors.error_count() == 0
327 || parameters->options().noinhibit_exec())
328 ? GOLD_OK
329 : GOLD_ERR);
5a6f7e2d 330}
This page took 0.250784 seconds and 4 git commands to generate.