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.
32 #include "libiberty.h"
36 #include "parameters.h"
39 #include "dirsearch.h"
40 #include "workqueue.h"
48 // This function emits the commandline to a hard-coded file in temp.
49 // This is useful for debugging since ld is typically invoked by gcc,
50 // so its commandline is not always easy to extract. You should be
51 // able to run 'gcc -B... foo.o -o foo' to invoke this linker the
52 // first time, and then /tmp/ld-run-foo.sh to invoke it on subsequent
53 // runes. "/tmp/ld-run-foo.sh debug" will run the linker inside gdb
54 // (or whatever value the environment variable GDB is set to), for
55 // even easier debugging. Since this is a debugging-only tool, and
56 // creates files, it is only turned on when the user explicitly asks
57 // for it, by compiling with -DDEBUG. Do not do this for release
58 // versions of the linker!
62 #include <sys/stat.h> // for chmod()
65 collect_argv(int argc
, char** argv
)
67 // This is used by write_debug_script(), which wants the unedited argv.
69 for (int i
= 0; i
< argc
; ++i
)
72 // Now append argv[i], but with all single-quotes escaped
73 const char* argpos
= argv
[i
];
76 const int len
= strcspn(argpos
, "'");
77 args
.append(argpos
, len
);
78 if (argpos
[len
] == '\0')
80 args
.append("'\"'\"'");
89 write_debug_script(std::string filename_str
,
90 const char* argv_0
, const char* args
)
92 size_t slash
= filename_str
.rfind('/');
93 if (slash
!= std::string::npos
)
94 filename_str
= filename_str
.c_str() + slash
+ 1;
95 filename_str
= std::string("/tmp/ld-run-") + filename_str
+ ".sh";
96 const char* filename
= filename_str
.c_str();
97 FILE* fp
= fopen(filename
, "w");
100 fprintf(fp
, "[ \"$1\" = debug ]"
101 " && PREFIX=\"${GDB-gdb} --annotate=3 --fullname %s --args\""
104 fprintf(fp
, "$PREFIX%s $*\n", args
);
106 chmod(filename
, 0755);
110 fprintf(stderr
, "Welcome to gold! Commandline written to %s.\n", filename
);
114 #else // !defined(DEBUG)
116 static inline std::string
117 collect_argv(int, char**)
123 write_debug_script(std::string
, const char*, const char*)
127 #endif // !defined(DEBUG)
131 main(int argc
, char** argv
)
133 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
134 setlocale (LC_MESSAGES
, "");
136 #if defined (HAVE_SETLOCALE)
137 setlocale (LC_CTYPE
, "");
139 bindtextdomain (PACKAGE
, LOCALEDIR
);
140 textdomain (PACKAGE
);
142 program_name
= argv
[0];
144 // In libiberty; expands @filename to the args in "filename".
145 expandargv(&argc
, &argv
);
147 // This is used by write_debug_script(), which wants the unedited argv.
148 std::string args
= collect_argv(argc
, argv
);
150 Errors
errors(program_name
);
152 // Initialize the global parameters, to let random code get to the
154 set_parameters_errors(&errors
);
156 // Handle the command line options.
157 Command_line command_line
;
158 command_line
.process(argc
- 1, const_cast<const char**>(argv
+ 1));
161 if (command_line
.options().stats())
162 start_time
= get_run_time();
164 // Store some options in the globally accessible parameters.
165 set_parameters_options(&command_line
.options());
167 // Do this as early as possible (since it prints a welcome message).
168 write_debug_script(command_line
.options().output_file_name(),
169 program_name
, args
.c_str());
171 // If the user asked for a map file, open it.
172 Mapfile
* mapfile
= NULL
;
173 if (command_line
.options().user_set_Map())
175 mapfile
= new Mapfile();
176 if (!mapfile
->open(command_line
.options().Map()))
183 // The GNU linker ignores version scripts when generating
184 // relocatable output. If we are not compatible, then we break the
185 // Linux kernel build, which uses a linker script with -r which must
186 // not force symbols to be local. It would actually be useful to
187 // permit symbols to be forced local with -r, though, as it would
188 // permit some linker optimizations. Perhaps we need yet another
189 // option to control this. FIXME.
190 if (parameters
->options().relocatable())
191 command_line
.script_options().version_script_info()->clear();
194 Workqueue
workqueue(command_line
.options());
196 // The list of input objects.
197 Input_objects input_objects
;
199 // The symbol table. We're going to guess here how many symbols
200 // we're going to see based on the number of input files. Even when
201 // this is off, it means at worst we don't quite optimize hashtable
202 // resizing as well as we could have (perhap using more memory).
203 Symbol_table
symtab(command_line
.number_of_input_files() * 1024,
204 command_line
.version_script());
206 // The layout object.
207 Layout
layout(command_line
.options(), &command_line
.script_options());
209 // Get the search path from the -L options.
210 Dirsearch search_path
;
211 search_path
.initialize(&workqueue
, &command_line
.options().library_path());
213 // Queue up the first set of tasks.
214 queue_initial_tasks(command_line
.options(), search_path
,
215 command_line
, &workqueue
, &input_objects
,
216 &symtab
, &layout
, mapfile
);
218 // Run the main task processing loop.
219 workqueue
.process(0);
221 if (command_line
.options().stats())
223 long run_time
= get_run_time() - start_time
;
224 fprintf(stderr
, _("%s: total run time: %ld.%06ld seconds\n"),
225 program_name
, run_time
/ 1000000, run_time
% 1000000);
227 struct mallinfo m
= mallinfo();
228 fprintf(stderr
, _("%s: total space allocated by malloc: %d bytes\n"),
229 program_name
, m
.arena
);
231 File_read::print_stats();
232 Archive::print_stats();
233 fprintf(stderr
, _("%s: output file size: %lld bytes\n"),
234 program_name
, static_cast<long long>(layout
.output_file_size()));
235 symtab
.print_stats();
236 layout
.print_stats();
242 // Issue defined symbol report.
243 if (command_line
.options().user_set_print_symbol_counts())
244 input_objects
.print_symbol_counts(&symtab
);
246 if (parameters
->options().fatal_warnings()
247 && errors
.warning_count() > 0
248 && errors
.error_count() == 0)
249 gold_error("treating warnings as errors");
251 // If the user used --noinhibit-exec, we force the exit status to be
252 // successful. This is compatible with GNU ld.
253 gold_exit(errors
.error_count() == 0
254 || parameters
->options().noinhibit_exec());