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