Commit | Line | Data |
---|---|---|
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 | ||
e44fcf3b ILT |
25 | #ifdef HAVE_MALLINFO |
26 | #include <malloc.h> | |
27 | #endif | |
28 | #include "libiberty.h" | |
29 | ||
e5756efb | 30 | #include "script.h" |
5a6f7e2d | 31 | #include "options.h" |
7e1edb90 | 32 | #include "parameters.h" |
75f2446e | 33 | #include "errors.h" |
5a6f7e2d ILT |
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 | ||
cbcc4140 ILT |
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; | |
55a93433 | 89 | filename_str = std::string("/tmp/ld-run-") + filename_str + ".sh"; |
cbcc4140 ILT |
90 | const char* filename = filename_str.c_str(); |
91 | FILE* fp = fopen(filename, "w"); | |
92 | if (fp) | |
93 | { | |
fc955173 ILT |
94 | fprintf(fp, "[ \"$1\" = debug ]" |
95 | " && PREFIX=\"${GDB-gdb} --annotate=3 --fullname %s --args\"" | |
96 | " && shift\n", | |
97 | argv_0); | |
cbcc4140 ILT |
98 | fprintf(fp, "$PREFIX%s $*\n", args); |
99 | fclose(fp); | |
100 | chmod(filename, 0755); | |
101 | } | |
102 | else | |
103 | filename = "[none]"; | |
104 | fprintf(stderr, "Welcome to gold! Commandline written to %s.\n", filename); | |
105 | fflush(stderr); | |
106 | } | |
107 | ||
108 | #else // !defined(DEBUG) | |
109 | ||
110 | static inline std::string | |
111 | collect_argv(int, char**) | |
112 | { | |
113 | return ""; | |
114 | } | |
115 | ||
116 | static inline void | |
117 | write_debug_script(std::string, const char*, const char*) | |
118 | { | |
119 | } | |
120 | ||
121 | #endif // !defined(DEBUG) | |
122 | ||
123 | ||
5a6f7e2d ILT |
124 | int |
125 | main(int argc, char** argv) | |
126 | { | |
127 | #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) | |
128 | setlocale (LC_MESSAGES, ""); | |
129 | #endif | |
130 | #if defined (HAVE_SETLOCALE) | |
131 | setlocale (LC_CTYPE, ""); | |
132 | #endif | |
133 | bindtextdomain (PACKAGE, LOCALEDIR); | |
134 | textdomain (PACKAGE); | |
135 | ||
136 | program_name = argv[0]; | |
137 | ||
fc955173 ILT |
138 | // In libiberty; expands @filename to the args in "filename". |
139 | expandargv(&argc, &argv); | |
140 | ||
cbcc4140 ILT |
141 | // This is used by write_debug_script(), which wants the unedited argv. |
142 | std::string args = collect_argv(argc, argv); | |
143 | ||
75f2446e ILT |
144 | Errors errors(program_name); |
145 | ||
3c2fafa5 ILT |
146 | // Initialize the global parameters, to let random code get to the |
147 | // errors object. | |
148 | initialize_parameters(&errors); | |
149 | ||
e5756efb ILT |
150 | // Options which may be set by the command line or by linker |
151 | // scripts. | |
152 | Script_options script_options; | |
153 | ||
5a6f7e2d | 154 | // Handle the command line options. |
e5756efb | 155 | Command_line command_line(&script_options); |
5a6f7e2d | 156 | command_line.process(argc - 1, argv + 1); |
e44fcf3b ILT |
157 | |
158 | long start_time = 0; | |
159 | if (command_line.options().print_stats()) | |
160 | start_time = get_run_time(); | |
161 | ||
3c2fafa5 ILT |
162 | // Store some options in the globally accessible parameters. |
163 | set_parameters_from_options(&command_line.options()); | |
5a6f7e2d | 164 | |
cbcc4140 ILT |
165 | // Do this as early as possible (since it prints a welcome message). |
166 | write_debug_script(command_line.options().output_file_name(), | |
167 | program_name, args.c_str()); | |
168 | ||
1ef1f3d3 ILT |
169 | // The GNU linker ignores version scripts when generating |
170 | // relocatable output. If we are not compatible, then we break the | |
171 | // Linux kernel build, which uses a linker script with -r which must | |
172 | // not force symbols to be local. It would actually be useful to | |
173 | // permit symbols to be forced local with -r, though, as it would | |
174 | // permit some linker optimizations. Perhaps we need yet another | |
175 | // option to control this. FIXME. | |
176 | if (parameters->output_is_object()) | |
177 | command_line.script_options()->version_script_info()->clear(); | |
178 | ||
5a6f7e2d ILT |
179 | // The work queue. |
180 | Workqueue workqueue(command_line.options()); | |
181 | ||
182 | // The list of input objects. | |
183 | Input_objects input_objects; | |
184 | ||
6d013333 ILT |
185 | // The symbol table. We're going to guess here how many symbols |
186 | // we're going to see based on the number of input files. Even when | |
cbcc4140 | 187 | // this is off, it means at worst we don't quite optimize hashtable |
6d013333 | 188 | // resizing as well as we could have (perhap using more memory). |
09124467 ILT |
189 | Symbol_table symtab(command_line.number_of_input_files() * 1024, |
190 | command_line.options().version_script()); | |
5a6f7e2d ILT |
191 | |
192 | // The layout object. | |
e5756efb | 193 | Layout layout(command_line.options(), &script_options); |
5a6f7e2d ILT |
194 | |
195 | // Get the search path from the -L options. | |
196 | Dirsearch search_path; | |
ad2d6943 | 197 | search_path.initialize(&workqueue, &command_line.options().search_path()); |
5a6f7e2d ILT |
198 | |
199 | // Queue up the first set of tasks. | |
200 | queue_initial_tasks(command_line.options(), search_path, | |
201 | command_line, &workqueue, &input_objects, | |
202 | &symtab, &layout); | |
203 | ||
204 | // Run the main task processing loop. | |
17a1d0a9 | 205 | workqueue.process(0); |
5a6f7e2d | 206 | |
e44fcf3b ILT |
207 | if (command_line.options().print_stats()) |
208 | { | |
209 | long run_time = get_run_time() - start_time; | |
210 | fprintf(stderr, _("%s: total run time: %ld.%06ld seconds\n"), | |
211 | program_name, run_time / 1000000, run_time % 1000000); | |
212 | #ifdef HAVE_MALLINFO | |
213 | struct mallinfo m = mallinfo(); | |
214 | fprintf(stderr, _("%s: total space allocated by malloc: %d bytes\n"), | |
215 | program_name, m.arena); | |
216 | #endif | |
217 | File_read::print_stats(); | |
218 | fprintf(stderr, _("%s: output file size: %lld bytes\n"), | |
219 | program_name, static_cast<long long>(layout.output_file_size())); | |
abaa3995 | 220 | symtab.print_stats(); |
ad8f37d1 | 221 | layout.print_stats(); |
e44fcf3b ILT |
222 | } |
223 | ||
75f2446e | 224 | gold_exit(errors.error_count() == 0); |
5a6f7e2d | 225 | } |