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