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