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