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