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