Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // ld.c -- linker main function |
2 | ||
3 | #include "gold.h" | |
4 | ||
5 | #include <cstdlib> | |
6 | #include <cstdio> | |
7 | #include <cstring> | |
8 | #include <unistd.h> | |
9 | ||
10 | #include "options.h" | |
11 | #include "workqueue.h" | |
12 | #include "dirsearch.h" | |
13 | #include "readsyms.h" | |
14bfc3f5 | 14 | #include "symtab.h" |
ead1e424 | 15 | #include "common.h" |
54dc6425 | 16 | #include "object.h" |
a2fb1b05 | 17 | #include "layout.h" |
61ba1cf9 | 18 | #include "reloc.h" |
ead1e424 | 19 | #include "defstd.h" |
bae7f79e ILT |
20 | |
21 | namespace gold | |
22 | { | |
23 | ||
24 | const char* program_name; | |
25 | ||
26 | void | |
27 | gold_exit(bool status) | |
28 | { | |
29 | exit(status ? EXIT_SUCCESS : EXIT_FAILURE); | |
30 | } | |
31 | ||
32 | void | |
33 | gold_fatal(const char* msg, bool perrno) | |
34 | { | |
35 | fprintf(stderr, "%s: ", program_name); | |
36 | if (perrno) | |
37 | perror(msg); | |
38 | else | |
39 | fprintf(stderr, "%s\n", msg); | |
40 | gold_exit(false); | |
41 | } | |
42 | ||
43 | void | |
44 | gold_nomem() | |
45 | { | |
46 | // We are out of memory, so try hard to print a reasonable message. | |
47 | // Note that we don't try to translate this message, since the | |
48 | // translation process itself will require memory. | |
49 | write(2, program_name, strlen(program_name)); | |
50 | const char* const s = ": out of memory\n"; | |
51 | write(2, s, strlen(s)); | |
52 | gold_exit(false); | |
53 | } | |
54 | ||
a3ad94ed ILT |
55 | // Handle an unreachable case. |
56 | ||
bae7f79e | 57 | void |
a3ad94ed | 58 | do_gold_unreachable(const char* filename, int lineno, const char* function) |
bae7f79e | 59 | { |
a3ad94ed ILT |
60 | fprintf(stderr, "%s: internal error in %s, at %s:%d\n", |
61 | program_name, function, filename, lineno); | |
62 | gold_exit(false); | |
bae7f79e ILT |
63 | } |
64 | ||
92e059d8 ILT |
65 | // This class arranges to run the functions done in the middle of the |
66 | // link. It is just a closure. | |
bae7f79e | 67 | |
92e059d8 | 68 | class Middle_runner : public Task_function_runner |
bae7f79e | 69 | { |
92e059d8 ILT |
70 | public: |
71 | Middle_runner(const General_options& options, | |
72 | const Input_objects* input_objects, | |
73 | Symbol_table* symtab, | |
74 | Layout* layout) | |
75 | : options_(options), input_objects_(input_objects), symtab_(symtab), | |
76 | layout_(layout) | |
77 | { } | |
78 | ||
79 | void | |
80 | run(Workqueue*); | |
81 | ||
82 | private: | |
83 | const General_options& options_; | |
84 | const Input_objects* input_objects_; | |
85 | Symbol_table* symtab_; | |
86 | Layout* layout_; | |
87 | }; | |
bae7f79e | 88 | |
92e059d8 ILT |
89 | void |
90 | Middle_runner::run(Workqueue* workqueue) | |
91 | { | |
92 | queue_middle_tasks(this->options_, this->input_objects_, this->symtab_, | |
93 | this->layout_, workqueue); | |
94 | } | |
bae7f79e ILT |
95 | |
96 | // Queue up the initial set of tasks for this link job. | |
97 | ||
98 | void | |
99 | queue_initial_tasks(const General_options& options, | |
100 | const Dirsearch& search_path, | |
ead1e424 | 101 | const Command_line& cmdline, |
54dc6425 | 102 | Workqueue* workqueue, Input_objects* input_objects, |
12e14209 | 103 | Symbol_table* symtab, Layout* layout) |
bae7f79e | 104 | { |
ead1e424 | 105 | if (cmdline.begin() == cmdline.end()) |
bae7f79e ILT |
106 | gold_fatal(_("no input files"), false); |
107 | ||
108 | // Read the input files. We have to add the symbols to the symbol | |
109 | // table in order. We do this by creating a separate blocker for | |
110 | // each input file. We associate the blocker with the following | |
111 | // input file, to give us a convenient place to delete it. | |
112 | Task_token* this_blocker = NULL; | |
ead1e424 ILT |
113 | for (Command_line::const_iterator p = cmdline.begin(); |
114 | p != cmdline.end(); | |
bae7f79e ILT |
115 | ++p) |
116 | { | |
117 | Task_token* next_blocker = new Task_token(); | |
118 | next_blocker->add_blocker(); | |
12e14209 | 119 | workqueue->queue(new Read_symbols(options, input_objects, symtab, layout, |
dbe717ef | 120 | search_path, &*p, NULL, this_blocker, |
a2fb1b05 | 121 | next_blocker)); |
bae7f79e ILT |
122 | this_blocker = next_blocker; |
123 | } | |
124 | ||
92e059d8 ILT |
125 | workqueue->queue(new Task_function(new Middle_runner(options, |
126 | input_objects, | |
127 | symtab, | |
128 | layout), | |
129 | this_blocker)); | |
bae7f79e ILT |
130 | } |
131 | ||
92e059d8 ILT |
132 | // Queue up the middle set of tasks. These are the tasks which run |
133 | // after all the input objects have been found and all the symbols | |
134 | // have been read, but before we lay out the output file. | |
bae7f79e | 135 | |
92e059d8 ILT |
136 | void |
137 | queue_middle_tasks(const General_options& options, | |
138 | const Input_objects* input_objects, | |
139 | Symbol_table* symtab, | |
140 | Layout* layout, | |
141 | Workqueue* workqueue) | |
61ba1cf9 | 142 | { |
a3ad94ed ILT |
143 | // Define some sections and symbols needed for a dynamic link. This |
144 | // handles some cases we want to see before we read the relocs. | |
145 | layout->create_initial_dynamic_sections(input_objects, symtab); | |
146 | ||
ead1e424 ILT |
147 | // Predefine standard symbols. This should be fast, so we don't |
148 | // bother to create a task for it. | |
149 | define_standard_symbols(symtab, layout, input_objects->target()); | |
150 | ||
92e059d8 ILT |
151 | // Read the relocations of the input files. We do this to find |
152 | // which symbols are used by relocations which require a GOT and/or | |
153 | // a PLT entry, or a COPY reloc. When we implement garbage | |
154 | // collection we will do it here by reading the relocations in a | |
155 | // breadth first search by references. | |
156 | // | |
157 | // We could also read the relocations during the first pass, and | |
158 | // mark symbols at that time. That is how the old GNU linker works. | |
159 | // Doing that is more complex, since we may later decide to discard | |
160 | // some of the sections, and thus change our minds about the types | |
161 | // of references made to the symbols. | |
162 | Task_token* blocker = new Task_token(); | |
163 | Task_token* symtab_lock = new Task_token(); | |
f6ce93d6 ILT |
164 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
165 | p != input_objects->relobj_end(); | |
92e059d8 ILT |
166 | ++p) |
167 | { | |
168 | // We can read and process the relocations in any order. But we | |
169 | // only want one task to write to the symbol table at a time. | |
170 | // So we queue up a task for each object to read the | |
171 | // relocations. That task will in turn queue a task to wait | |
172 | // until it can write to the symbol table. | |
173 | blocker->add_blocker(); | |
ead1e424 ILT |
174 | workqueue->queue(new Read_relocs(options, symtab, layout, *p, |
175 | symtab_lock, blocker)); | |
92e059d8 ILT |
176 | } |
177 | ||
178 | // Allocate common symbols. This requires write access to the | |
179 | // symbol table, but is independent of the relocation processing. | |
ead1e424 ILT |
180 | blocker->add_blocker(); |
181 | workqueue->queue(new Allocate_commons_task(options, symtab, layout, | |
182 | symtab_lock, blocker)); | |
92e059d8 ILT |
183 | |
184 | // When all those tasks are complete, we can start laying out the | |
185 | // output file. | |
186 | workqueue->queue(new Task_function(new Layout_task_runner(options, | |
187 | input_objects, | |
188 | symtab, | |
189 | layout), | |
190 | blocker)); | |
191 | } | |
61ba1cf9 ILT |
192 | |
193 | // Queue up the final set of tasks. This is called at the end of | |
194 | // Layout_task. | |
195 | ||
196 | void | |
197 | queue_final_tasks(const General_options& options, | |
198 | const Input_objects* input_objects, | |
199 | const Symbol_table* symtab, | |
200 | const Layout* layout, | |
201 | Workqueue* workqueue, | |
202 | Output_file* of) | |
203 | { | |
204 | // Use a blocker to block the final cleanup task. | |
205 | Task_token* final_blocker = new Task_token(); | |
206 | ||
207 | // Queue a task for each input object to relocate the sections and | |
208 | // write out the local symbols. | |
f6ce93d6 ILT |
209 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
210 | p != input_objects->relobj_end(); | |
61ba1cf9 ILT |
211 | ++p) |
212 | { | |
213 | final_blocker->add_blocker(); | |
92e059d8 ILT |
214 | workqueue->queue(new Relocate_task(options, symtab, layout, *p, of, |
215 | final_blocker)); | |
61ba1cf9 ILT |
216 | } |
217 | ||
218 | // Queue a task to write out the symbol table. | |
219 | final_blocker->add_blocker(); | |
220 | workqueue->queue(new Write_symbols_task(symtab, input_objects->target(), | |
221 | layout->sympool(), of, | |
222 | final_blocker)); | |
223 | ||
224 | // Queue a task to write out everything else. | |
225 | final_blocker->add_blocker(); | |
a3ad94ed ILT |
226 | workqueue->queue(new Write_data_task(layout, symtab, |
227 | input_objects->target(), | |
228 | of, final_blocker)); | |
61ba1cf9 ILT |
229 | |
230 | // Queue a task to close the output file. This will be blocked by | |
231 | // FINAL_BLOCKER. | |
92e059d8 ILT |
232 | workqueue->queue(new Task_function(new Close_task_runner(of), |
233 | final_blocker)); | |
61ba1cf9 ILT |
234 | } |
235 | ||
236 | } // End namespace gold. | |
237 | ||
92e059d8 ILT |
238 | using namespace gold; |
239 | ||
bae7f79e ILT |
240 | int |
241 | main(int argc, char** argv) | |
242 | { | |
243 | #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES) | |
244 | setlocale (LC_MESSAGES, ""); | |
245 | #endif | |
246 | #if defined (HAVE_SETLOCALE) | |
247 | setlocale (LC_CTYPE, ""); | |
248 | #endif | |
249 | bindtextdomain (PACKAGE, LOCALEDIR); | |
250 | textdomain (PACKAGE); | |
251 | ||
252 | gold::program_name = argv[0]; | |
253 | ||
254 | // Handle the command line options. | |
255 | gold::Command_line command_line; | |
256 | command_line.process(argc - 1, argv + 1); | |
257 | ||
258 | // The work queue. | |
259 | gold::Workqueue workqueue(command_line.options()); | |
260 | ||
a2fb1b05 | 261 | // The list of input objects. |
54dc6425 | 262 | Input_objects input_objects; |
a2fb1b05 | 263 | |
bae7f79e | 264 | // The symbol table. |
14bfc3f5 | 265 | Symbol_table symtab; |
bae7f79e | 266 | |
12e14209 ILT |
267 | // The layout object. |
268 | Layout layout(command_line.options()); | |
269 | ||
bae7f79e ILT |
270 | // Get the search path from the -L options. |
271 | Dirsearch search_path; | |
272 | search_path.add(&workqueue, command_line.options().search_path()); | |
273 | ||
274 | // Queue up the first set of tasks. | |
275 | queue_initial_tasks(command_line.options(), search_path, | |
ead1e424 | 276 | command_line, &workqueue, &input_objects, |
12e14209 | 277 | &symtab, &layout); |
bae7f79e ILT |
278 | |
279 | // Run the main task processing loop. | |
280 | workqueue.process(); | |
281 | ||
282 | gold::gold_exit(true); | |
283 | } |