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