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