Use STRING_COMMA_LEN to avoid strlen.
[deliverable/binutils-gdb.git] / gold / main.cc
CommitLineData
5a6f7e2d
ILT
1// main.cc -- gold main function.
2
dde3f402 3// Copyright 2006, 2007, 2008, 2009, 2010 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"
ef15dade 47#include "icf.h"
3ce2c28e 48#include "incremental.h"
d675ff46 49#include "timer.h"
5a6f7e2d
ILT
50
51using namespace gold;
52
cbcc4140
ILT
53// This function emits the commandline to a hard-coded file in temp.
54// This is useful for debugging since ld is typically invoked by gcc,
55// so its commandline is not always easy to extract. You should be
56// able to run 'gcc -B... foo.o -o foo' to invoke this linker the
57// first time, and then /tmp/ld-run-foo.sh to invoke it on subsequent
58// runes. "/tmp/ld-run-foo.sh debug" will run the linker inside gdb
59// (or whatever value the environment variable GDB is set to), for
60// even easier debugging. Since this is a debugging-only tool, and
61// creates files, it is only turned on when the user explicitly asks
62// for it, by compiling with -DDEBUG. Do not do this for release
63// versions of the linker!
64
65#ifdef DEBUG
66#include <stdio.h>
67#include <sys/stat.h> // for chmod()
68
69static std::string
70collect_argv(int argc, char** argv)
71{
72 // This is used by write_debug_script(), which wants the unedited argv.
73 std::string args;
74 for (int i = 0; i < argc; ++i)
75 {
76 args.append(" '");
77 // Now append argv[i], but with all single-quotes escaped
78 const char* argpos = argv[i];
79 while (1)
80 {
81 const int len = strcspn(argpos, "'");
82 args.append(argpos, len);
83 if (argpos[len] == '\0')
84 break;
85 args.append("'\"'\"'");
86 argpos += len + 1;
87 }
88 args.append("'");
89 }
90 return args;
91}
92
93static void
94write_debug_script(std::string filename_str,
95 const char* argv_0, const char* args)
96{
97 size_t slash = filename_str.rfind('/');
98 if (slash != std::string::npos)
99 filename_str = filename_str.c_str() + slash + 1;
55a93433 100 filename_str = std::string("/tmp/ld-run-") + filename_str + ".sh";
cbcc4140
ILT
101 const char* filename = filename_str.c_str();
102 FILE* fp = fopen(filename, "w");
103 if (fp)
104 {
fc955173
ILT
105 fprintf(fp, "[ \"$1\" = debug ]"
106 " && PREFIX=\"${GDB-gdb} --annotate=3 --fullname %s --args\""
107 " && shift\n",
108 argv_0);
cbcc4140
ILT
109 fprintf(fp, "$PREFIX%s $*\n", args);
110 fclose(fp);
111 chmod(filename, 0755);
112 }
113 else
114 filename = "[none]";
115 fprintf(stderr, "Welcome to gold! Commandline written to %s.\n", filename);
116 fflush(stderr);
117}
118
119#else // !defined(DEBUG)
120
121static inline std::string
122collect_argv(int, char**)
123{
124 return "";
125}
126
127static inline void
128write_debug_script(std::string, const char*, const char*)
129{
130}
131
132#endif // !defined(DEBUG)
133
134
5a6f7e2d
ILT
135int
136main(int argc, char** argv)
137{
138#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
139 setlocale (LC_MESSAGES, "");
140#endif
141#if defined (HAVE_SETLOCALE)
142 setlocale (LC_CTYPE, "");
143#endif
144 bindtextdomain (PACKAGE, LOCALEDIR);
145 textdomain (PACKAGE);
146
147 program_name = argv[0];
148
fc955173
ILT
149 // In libiberty; expands @filename to the args in "filename".
150 expandargv(&argc, &argv);
151
cbcc4140
ILT
152 // This is used by write_debug_script(), which wants the unedited argv.
153 std::string args = collect_argv(argc, argv);
154
75f2446e
ILT
155 Errors errors(program_name);
156
3c2fafa5
ILT
157 // Initialize the global parameters, to let random code get to the
158 // errors object.
8851ecca 159 set_parameters_errors(&errors);
3c2fafa5 160
5a6f7e2d 161 // Handle the command line options.
a5dc0706 162 Command_line command_line;
ee1fe73e 163 command_line.process(argc - 1, const_cast<const char**>(argv + 1));
e44fcf3b 164
d675ff46 165 Timer timer;
7cc619c3 166 if (command_line.options().stats())
d675ff46 167 timer.start();
e44fcf3b 168
3c2fafa5 169 // Store some options in the globally accessible parameters.
8851ecca 170 set_parameters_options(&command_line.options());
5a6f7e2d 171
cbcc4140
ILT
172 // Do this as early as possible (since it prints a welcome message).
173 write_debug_script(command_line.options().output_file_name(),
174 program_name, args.c_str());
175
7d9e3d98
ILT
176 // If the user asked for a map file, open it.
177 Mapfile* mapfile = NULL;
178 if (command_line.options().user_set_Map())
179 {
180 mapfile = new Mapfile();
181 if (!mapfile->open(command_line.options().Map()))
182 {
183 delete mapfile;
184 mapfile = NULL;
185 }
186 }
187
1ef1f3d3
ILT
188 // The GNU linker ignores version scripts when generating
189 // relocatable output. If we are not compatible, then we break the
190 // Linux kernel build, which uses a linker script with -r which must
191 // not force symbols to be local. It would actually be useful to
192 // permit symbols to be forced local with -r, though, as it would
193 // permit some linker optimizations. Perhaps we need yet another
194 // option to control this. FIXME.
8851ecca 195 if (parameters->options().relocatable())
a5dc0706 196 command_line.script_options().version_script_info()->clear();
1ef1f3d3 197
89fc3421
CC
198 // Load plugin libraries.
199 if (command_line.options().has_plugins())
200 command_line.options().plugins()->load_plugins();
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
6d013333 217 // resizing as well as we could have (perhap 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)
072fe7ce
ILT
232 {
233 layout.incremental_inputs()->report_command_line(argc, argv);
234 layout.incremental_inputs()->report_inputs(command_line.inputs());
235 }
3ce2c28e 236
5a6f7e2d
ILT
237 // Get the search path from the -L options.
238 Dirsearch search_path;
7cc619c3 239 search_path.initialize(&workqueue, &command_line.options().library_path());
5a6f7e2d
ILT
240
241 // Queue up the first set of tasks.
242 queue_initial_tasks(command_line.options(), search_path,
243 command_line, &workqueue, &input_objects,
7d9e3d98 244 &symtab, &layout, mapfile);
5a6f7e2d
ILT
245
246 // Run the main task processing loop.
17a1d0a9 247 workqueue.process(0);
5a6f7e2d 248
7cc619c3 249 if (command_line.options().stats())
e44fcf3b 250 {
d675ff46
RÁE
251 Timer::TimeStats elapsed = timer.get_elapsed_time();
252 fprintf(stderr,
253 _("%s: total run time: " \
254 "(user: %ld.%06ld sys: %ld.%06ld wall: %ld.%06ld)\n"),
255 program_name,
256 elapsed.user / 1000, (elapsed.user % 1000) * 1000,
3e1b9a8a 257 elapsed.sys / 1000, (elapsed.sys % 1000) * 1000,
d675ff46
RÁE
258 elapsed.wall / 1000, (elapsed.wall % 1000) * 1000);
259
e44fcf3b
ILT
260#ifdef HAVE_MALLINFO
261 struct mallinfo m = mallinfo();
262 fprintf(stderr, _("%s: total space allocated by malloc: %d bytes\n"),
263 program_name, m.arena);
264#endif
265 File_read::print_stats();
ac45a351 266 Archive::print_stats();
e44fcf3b
ILT
267 fprintf(stderr, _("%s: output file size: %lld bytes\n"),
268 program_name, static_cast<long long>(layout.output_file_size()));
abaa3995 269 symtab.print_stats();
ad8f37d1 270 layout.print_stats();
e44fcf3b
ILT
271 }
272
92de84a6
ILT
273 // Issue defined symbol report.
274 if (command_line.options().user_set_print_symbol_counts())
275 input_objects.print_symbol_counts(&symtab);
276
dde3f402
ILT
277 // Output cross reference table.
278 if (command_line.options().cref())
279 input_objects.print_cref(&symtab,
280 mapfile == NULL ? stdout : mapfile->file());
281
282 if (mapfile != NULL)
283 mapfile->close();
284
d82a5bcc
ILT
285 if (parameters->options().fatal_warnings()
286 && errors.warning_count() > 0
287 && errors.error_count() == 0)
288 gold_error("treating warnings as errors");
289
cdb0b8f5
ILT
290 // If the user used --noinhibit-exec, we force the exit status to be
291 // successful. This is compatible with GNU ld.
292 gold_exit(errors.error_count() == 0
293 || parameters->options().noinhibit_exec());
5a6f7e2d 294}
This page took 0.18981 seconds and 4 git commands to generate.