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