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