Fix PE-COFF archiving long name tables
[binutils/dougsmingw.git] / gold / main.cc
blobe3df9c084841926370a798c2929b8f7f1b0cddbc
1 // main.cc -- gold main function.
3 // Copyright 2006, 2007, 2008, 2009, 2010 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.
23 #include "gold.h"
25 #include <cstdio>
26 #include <cstring>
28 #ifdef HAVE_MALLINFO
29 #include <malloc.h>
30 #endif
32 #include "libiberty.h"
34 #include "script.h"
35 #include "options.h"
36 #include "parameters.h"
37 #include "errors.h"
38 #include "mapfile.h"
39 #include "dirsearch.h"
40 #include "workqueue.h"
41 #include "object.h"
42 #include "archive.h"
43 #include "symtab.h"
44 #include "layout.h"
45 #include "plugin.h"
46 #include "gc.h"
47 #include "icf.h"
48 #include "incremental.h"
49 #include "timer.h"
51 using namespace gold;
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!
65 #ifdef DEBUG
66 #include <stdio.h>
67 #include <sys/stat.h> // for chmod()
69 static std::string
70 collect_argv(int argc, char** argv)
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)
76 args.append(" '");
77 // Now append argv[i], but with all single-quotes escaped
78 const char* argpos = argv[i];
79 while (1)
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;
88 args.append("'");
90 return args;
93 static void
94 write_debug_script(std::string filename_str,
95 const char* argv_0, const char* args)
97 size_t slash = filename_str.rfind('/');
98 if (slash != std::string::npos)
99 filename_str = filename_str.c_str() + slash + 1;
100 filename_str = std::string("/tmp/ld-run-") + filename_str + ".sh";
101 const char* filename = filename_str.c_str();
102 FILE* fp = fopen(filename, "w");
103 if (fp)
105 fprintf(fp, "[ \"$1\" = debug ]"
106 " && PREFIX=\"${GDB-gdb} --annotate=3 --fullname %s --args\""
107 " && shift\n",
108 argv_0);
109 fprintf(fp, "$PREFIX%s $*\n", args);
110 fclose(fp);
111 chmod(filename, 0755);
113 else
114 filename = "[none]";
115 fprintf(stderr, "Welcome to gold! Commandline written to %s.\n", filename);
116 fflush(stderr);
119 #else // !defined(DEBUG)
121 static inline std::string
122 collect_argv(int, char**)
124 return "";
127 static inline void
128 write_debug_script(std::string, const char*, const char*)
132 #endif // !defined(DEBUG)
136 main(int argc, char** argv)
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);
147 program_name = argv[0];
149 // In libiberty; expands @filename to the args in "filename".
150 expandargv(&argc, &argv);
152 // This is used by write_debug_script(), which wants the unedited argv.
153 std::string args = collect_argv(argc, argv);
155 Errors errors(program_name);
157 // Initialize the global parameters, to let random code get to the
158 // errors object.
159 set_parameters_errors(&errors);
161 // Handle the command line options.
162 Command_line command_line;
163 command_line.process(argc - 1, const_cast<const char**>(argv + 1));
165 Timer timer;
166 if (command_line.options().stats())
167 timer.start();
169 // Store some options in the globally accessible parameters.
170 set_parameters_options(&command_line.options());
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());
176 // If the user asked for a map file, open it.
177 Mapfile* mapfile = NULL;
178 if (command_line.options().user_set_Map())
180 mapfile = new Mapfile();
181 if (!mapfile->open(command_line.options().Map()))
183 delete mapfile;
184 mapfile = NULL;
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.
195 if (parameters->options().relocatable())
196 command_line.script_options().version_script_info()->clear();
198 // Load plugin libraries.
199 if (command_line.options().has_plugins())
200 command_line.options().plugins()->load_plugins();
202 // The work queue.
203 Workqueue workqueue(command_line.options());
205 // The list of input objects.
206 Input_objects input_objects;
208 // The Garbage Collection (GC, --gc-sections) Object.
209 Garbage_collection gc;
211 // The Identical Code Folding (ICF, --icf) Object.
212 Icf icf;
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
216 // this is off, it means at worst we don't quite optimize hashtable
217 // resizing as well as we could have (perhap using more memory).
218 Symbol_table symtab(command_line.number_of_input_files() * 1024,
219 command_line.version_script());
221 if (parameters->options().gc_sections())
222 symtab.set_gc(&gc);
224 if (parameters->options().icf_enabled())
225 symtab.set_icf(&icf);
227 // The layout object.
228 Layout layout(command_line.number_of_input_files(),
229 &command_line.script_options());
231 if (layout.incremental_inputs() != NULL)
233 layout.incremental_inputs()->report_command_line(argc, argv);
234 layout.incremental_inputs()->report_inputs(command_line.inputs());
237 // Get the search path from the -L options.
238 Dirsearch search_path;
239 search_path.initialize(&workqueue, &command_line.options().library_path());
241 // Queue up the first set of tasks.
242 queue_initial_tasks(command_line.options(), search_path,
243 command_line, &workqueue, &input_objects,
244 &symtab, &layout, mapfile);
246 // Run the main task processing loop.
247 workqueue.process(0);
249 if (command_line.options().stats())
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,
257 elapsed.sys / 1000, (elapsed.sys % 1000) * 1000,
258 elapsed.wall / 1000, (elapsed.wall % 1000) * 1000);
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();
266 Archive::print_stats();
267 Lib_group::print_stats();
268 fprintf(stderr, _("%s: output file size: %lld bytes\n"),
269 program_name, static_cast<long long>(layout.output_file_size()));
270 symtab.print_stats();
271 layout.print_stats();
274 // Issue defined symbol report.
275 if (command_line.options().user_set_print_symbol_counts())
276 input_objects.print_symbol_counts(&symtab);
278 // Output cross reference table.
279 if (command_line.options().cref())
280 input_objects.print_cref(&symtab,
281 mapfile == NULL ? stdout : mapfile->file());
283 if (mapfile != NULL)
284 mapfile->close();
286 if (parameters->options().fatal_warnings()
287 && errors.warning_count() > 0
288 && errors.error_count() == 0)
289 gold_error("treating warnings as errors");
291 // If the user used --noinhibit-exec, we force the exit status to be
292 // successful. This is compatible with GNU ld.
293 gold_exit(errors.error_count() == 0
294 || parameters->options().noinhibit_exec());