Merge branch 'vendor/LIBARCHIVE'
[dragonfly.git] / contrib / binutils-2.21 / gold / gold.cc
1 // gold.cc -- main linker functions
2
3 // Copyright 2006, 2007, 2008, 2009, 2010 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 #include <cstdlib>
26 #include <cstdio>
27 #include <cstring>
28 #include <unistd.h>
29 #include <algorithm>
30 #include "libiberty.h"
31
32 #include "options.h"
33 #include "debug.h"
34 #include "workqueue.h"
35 #include "dirsearch.h"
36 #include "readsyms.h"
37 #include "symtab.h"
38 #include "common.h"
39 #include "object.h"
40 #include "layout.h"
41 #include "reloc.h"
42 #include "defstd.h"
43 #include "plugin.h"
44 #include "gc.h"
45 #include "icf.h"
46 #include "incremental.h"
47
48 namespace gold
49 {
50
51 const char* program_name;
52
53 void
54 gold_exit(bool status)
55 {
56   if (parameters != NULL
57       && parameters->options_valid()
58       && parameters->options().has_plugins())
59     parameters->options().plugins()->cleanup();
60   if (!status && parameters != NULL && parameters->options_valid())
61     unlink_if_ordinary(parameters->options().output_file_name());
62   exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
63 }
64
65 void
66 gold_nomem()
67 {
68   // We are out of memory, so try hard to print a reasonable message.
69   // Note that we don't try to translate this message, since the
70   // translation process itself will require memory.
71
72   // LEN only exists to avoid a pointless warning when write is
73   // declared with warn_use_result, as when compiling with
74   // -D_USE_FORTIFY on GNU/Linux.  Casting to void does not appear to
75   // work, at least not with gcc 4.3.0.
76
77   ssize_t len = write(2, program_name, strlen(program_name));
78   if (len >= 0)
79     {
80       const char* const s = ": out of memory\n";
81       len = write(2, s, strlen(s));
82     }
83   gold_exit(false);
84 }
85
86 // Handle an unreachable case.
87
88 void
89 do_gold_unreachable(const char* filename, int lineno, const char* function)
90 {
91   fprintf(stderr, _("%s: internal error in %s, at %s:%d\n"),
92           program_name, function, filename, lineno);
93   gold_exit(false);
94 }
95
96 // This class arranges to run the functions done in the middle of the
97 // link.  It is just a closure.
98
99 class Middle_runner : public Task_function_runner
100 {
101  public:
102   Middle_runner(const General_options& options,
103                 const Input_objects* input_objects,
104                 Symbol_table* symtab,
105                 Layout* layout, Mapfile* mapfile)
106     : options_(options), input_objects_(input_objects), symtab_(symtab),
107       layout_(layout), mapfile_(mapfile)
108   { }
109
110   void
111   run(Workqueue*, const Task*);
112
113  private:
114   const General_options& options_;
115   const Input_objects* input_objects_;
116   Symbol_table* symtab_;
117   Layout* layout_;
118   Mapfile* mapfile_;
119 };
120
121 void
122 Middle_runner::run(Workqueue* workqueue, const Task* task)
123 {
124   queue_middle_tasks(this->options_, task, this->input_objects_, this->symtab_,
125                      this->layout_, workqueue, this->mapfile_);
126 }
127
128 // This class arranges the tasks to process the relocs for garbage collection.
129
130 class Gc_runner : public Task_function_runner 
131 {
132   public:
133    Gc_runner(const General_options& options,
134              const Input_objects* input_objects,
135              Symbol_table* symtab,
136              Layout* layout, Mapfile* mapfile)
137     : options_(options), input_objects_(input_objects), symtab_(symtab),
138       layout_(layout), mapfile_(mapfile)
139    { }
140
141   void
142   run(Workqueue*, const Task*);
143
144  private:
145   const General_options& options_;
146   const Input_objects* input_objects_;
147   Symbol_table* symtab_;
148   Layout* layout_;
149   Mapfile* mapfile_;
150 };
151
152 void
153 Gc_runner::run(Workqueue* workqueue, const Task* task)
154 {
155   queue_middle_gc_tasks(this->options_, task, this->input_objects_, 
156                         this->symtab_, this->layout_, workqueue, 
157                         this->mapfile_);
158 }
159
160 // Queue up the initial set of tasks for this link job.
161
162 void
163 queue_initial_tasks(const General_options& options,
164                     Dirsearch& search_path,
165                     const Command_line& cmdline,
166                     Workqueue* workqueue, Input_objects* input_objects,
167                     Symbol_table* symtab, Layout* layout, Mapfile* mapfile)
168 {
169   if (cmdline.begin() == cmdline.end())
170     {
171       if (options.printed_version())
172         gold_exit(true);
173       gold_fatal(_("no input files"));
174     }
175
176   int thread_count = options.thread_count_initial();
177   if (thread_count == 0)
178     thread_count = cmdline.number_of_input_files();
179   workqueue->set_thread_count(thread_count);
180
181   if (parameters->incremental())
182     {
183       Incremental_checker incremental_checker(
184           parameters->options().output_file_name(),
185           layout->incremental_inputs());
186       if (incremental_checker.can_incrementally_link_output_file())
187         {
188           // TODO: remove when incremental linking implemented.
189           printf("Incremental linking might be possible "
190               "(not implemented yet)\n");
191         }
192       // TODO: If we decide on an incremental build, fewer tasks
193       // should be scheduled.
194     }
195
196   // Read the input files.  We have to add the symbols to the symbol
197   // table in order.  We do this by creating a separate blocker for
198   // each input file.  We associate the blocker with the following
199   // input file, to give us a convenient place to delete it.
200   Task_token* this_blocker = NULL;
201   for (Command_line::const_iterator p = cmdline.begin();
202        p != cmdline.end();
203        ++p)
204     {
205       Task_token* next_blocker = new Task_token(true);
206       next_blocker->add_blocker();
207       workqueue->queue(new Read_symbols(input_objects, symtab, layout,
208                                         &search_path, 0, mapfile, &*p, NULL,
209                                         NULL, this_blocker, next_blocker));
210       this_blocker = next_blocker;
211     }
212
213   if (options.has_plugins())
214     {
215       Task_token* next_blocker = new Task_token(true);
216       next_blocker->add_blocker();
217       workqueue->queue(new Plugin_hook(options, input_objects, symtab, layout,
218                                        &search_path, mapfile, this_blocker,
219                                        next_blocker));
220       this_blocker = next_blocker;
221     }
222
223   if (parameters->options().relocatable()
224       && (parameters->options().gc_sections()
225           || parameters->options().icf_enabled()))
226     gold_error(_("cannot mix -r with --gc-sections or --icf"));
227
228   if (parameters->options().gc_sections()
229       || parameters->options().icf_enabled())
230     {
231       workqueue->queue(new Task_function(new Gc_runner(options,
232                                                        input_objects,
233                                                        symtab,
234                                                        layout,
235                                                        mapfile),
236                                          this_blocker,
237                                          "Task_function Gc_runner"));
238     }
239   else
240     {
241       workqueue->queue(new Task_function(new Middle_runner(options,
242                                                            input_objects,
243                                                            symtab,
244                                                            layout,
245                                                            mapfile),
246                                          this_blocker,
247                                          "Task_function Middle_runner"));
248     }
249 }
250
251 // Queue up a set of tasks to be done before queueing the middle set
252 // of tasks.  This is only necessary when garbage collection
253 // (--gc-sections) of unused sections is desired.  The relocs are read
254 // and processed here early to determine the garbage sections before the
255 // relocs can be scanned in later tasks.
256
257 void
258 queue_middle_gc_tasks(const General_options& options,
259                       const Task* ,
260                       const Input_objects* input_objects,
261                       Symbol_table* symtab,
262                       Layout* layout,
263                       Workqueue* workqueue,
264                       Mapfile* mapfile)
265 {
266   // Read_relocs for all the objects must be done and processed to find
267   // unused sections before any scanning of the relocs can take place.
268   Task_token* this_blocker = NULL;
269   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
270        p != input_objects->relobj_end();
271        ++p)
272     {
273       Task_token* next_blocker = new Task_token(true);
274       next_blocker->add_blocker();
275       workqueue->queue(new Read_relocs(symtab, layout, *p, this_blocker,
276                                        next_blocker));
277       this_blocker = next_blocker;
278     }
279
280   // If we are given only archives in input, we have no regular
281   // objects and THIS_BLOCKER is NULL here.  Create a dummy
282   // blocker here so that we can run the middle tasks immediately.
283   if (this_blocker == NULL)
284     {
285       gold_assert(input_objects->number_of_relobjs() == 0);
286       this_blocker = new Task_token(true);
287     }
288
289   workqueue->queue(new Task_function(new Middle_runner(options,
290                                                        input_objects,
291                                                        symtab,
292                                                        layout,
293                                                        mapfile),
294                                      this_blocker,
295                                      "Task_function Middle_runner"));
296 }
297
298 // Queue up the middle set of tasks.  These are the tasks which run
299 // after all the input objects have been found and all the symbols
300 // have been read, but before we lay out the output file.
301
302 void
303 queue_middle_tasks(const General_options& options,
304                    const Task* task,
305                    const Input_objects* input_objects,
306                    Symbol_table* symtab,
307                    Layout* layout,
308                    Workqueue* workqueue,
309                    Mapfile* mapfile)
310 {
311   // Add any symbols named with -u options to the symbol table.
312   symtab->add_undefined_symbols_from_command_line(layout);
313
314   // If garbage collection was chosen, relocs have been read and processed
315   // at this point by pre_middle_tasks.  Layout can then be done for all 
316   // objects.
317   if (parameters->options().gc_sections())
318     {
319       // Find the start symbol if any.
320       Symbol* start_sym;
321       if (parameters->options().entry())
322         start_sym = symtab->lookup(parameters->options().entry());
323       else
324         start_sym = symtab->lookup("_start");
325       if (start_sym != NULL)
326         {
327           bool is_ordinary;
328           unsigned int shndx = start_sym->shndx(&is_ordinary);
329           if (is_ordinary) 
330             {
331               symtab->gc()->worklist().push(
332                 Section_id(start_sym->object(), shndx));
333             }
334         }
335       // Symbols named with -u should not be considered garbage.
336       symtab->gc_mark_undef_symbols(layout);
337       gold_assert(symtab->gc() != NULL);
338       // Do a transitive closure on all references to determine the worklist.
339       symtab->gc()->do_transitive_closure();
340     }
341
342   // If identical code folding (--icf) is chosen it makes sense to do it 
343   // only after garbage collection (--gc-sections) as we do not want to 
344   // be folding sections that will be garbage.
345   if (parameters->options().icf_enabled())
346     {
347       symtab->icf()->find_identical_sections(input_objects, symtab);
348     }
349
350   // Call Object::layout for the second time to determine the 
351   // output_sections for all referenced input sections.  When 
352   // --gc-sections or --icf is turned on, Object::layout is 
353   // called twice.  It is called the first time when the 
354   // symbols are added.
355   if (parameters->options().gc_sections()
356       || parameters->options().icf_enabled())
357     {
358       for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
359            p != input_objects->relobj_end();
360            ++p)
361         {
362           Task_lock_obj<Object> tlo(task, *p);
363           (*p)->layout(symtab, layout, NULL);
364         }
365     }
366
367   // Layout deferred objects due to plugins.
368   if (parameters->options().has_plugins())
369     {
370       Plugin_manager* plugins = parameters->options().plugins();
371       gold_assert(plugins != NULL);
372       plugins->layout_deferred_objects();
373     }     
374
375   if (parameters->options().gc_sections()
376       || parameters->options().icf_enabled())
377     {
378       for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
379            p != input_objects->relobj_end();
380            ++p)
381         {
382           // Update the value of output_section stored in rd.
383           Read_relocs_data* rd = (*p)->get_relocs_data();
384           for (Read_relocs_data::Relocs_list::iterator q = rd->relocs.begin();
385                q != rd->relocs.end();
386                ++q)
387             {
388               q->output_section = (*p)->output_section(q->data_shndx);
389               q->needs_special_offset_handling = 
390                       (*p)->is_output_section_offset_invalid(q->data_shndx);
391             }
392         }
393     }
394
395   // We have to support the case of not seeing any input objects, and
396   // generate an empty file.  Existing builds depend on being able to
397   // pass an empty archive to the linker and get an empty object file
398   // out.  In order to do this we need to use a default target.
399   if (input_objects->number_of_input_objects() == 0)
400     parameters_force_valid_target();
401
402   int thread_count = options.thread_count_middle();
403   if (thread_count == 0)
404     thread_count = std::max(2, input_objects->number_of_input_objects());
405   workqueue->set_thread_count(thread_count);
406
407   // Now we have seen all the input files.
408   const bool doing_static_link =
409     (!input_objects->any_dynamic()
410      && !parameters->options().output_is_position_independent());
411   set_parameters_doing_static_link(doing_static_link);
412   if (!doing_static_link && options.is_static())
413     {
414       // We print out just the first .so we see; there may be others.
415       gold_assert(input_objects->dynobj_begin() != input_objects->dynobj_end());
416       gold_error(_("cannot mix -static with dynamic object %s"),
417                  (*input_objects->dynobj_begin())->name().c_str());
418     }
419   if (!doing_static_link && parameters->options().relocatable())
420     gold_fatal(_("cannot mix -r with dynamic object %s"),
421                (*input_objects->dynobj_begin())->name().c_str());
422   if (!doing_static_link
423       && options.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
424     gold_fatal(_("cannot use non-ELF output format with dynamic object %s"),
425                (*input_objects->dynobj_begin())->name().c_str());
426
427   if (parameters->options().relocatable())
428     {
429       Input_objects::Relobj_iterator p = input_objects->relobj_begin();
430       if (p != input_objects->relobj_end())
431         {
432           bool uses_split_stack = (*p)->uses_split_stack();
433           for (++p; p != input_objects->relobj_end(); ++p)
434             {
435               if ((*p)->uses_split_stack() != uses_split_stack)
436                 gold_fatal(_("cannot mix split-stack '%s' and "
437                              "non-split-stack '%s' when using -r"),
438                            (*input_objects->relobj_begin())->name().c_str(),
439                            (*p)->name().c_str());
440             }
441         }
442     }
443
444   if (is_debugging_enabled(DEBUG_SCRIPT))
445     layout->script_options()->print(stderr);
446
447   // For each dynamic object, record whether we've seen all the
448   // dynamic objects that it depends upon.
449   input_objects->check_dynamic_dependencies();
450
451   // See if any of the input definitions violate the One Definition Rule.
452   // TODO: if this is too slow, do this as a task, rather than inline.
453   symtab->detect_odr_violations(task, options.output_file_name());
454
455   // Do the --no-undefined-version check.
456   if (!parameters->options().undefined_version())
457     {
458       Script_options* so = layout->script_options();
459       so->version_script_info()->check_unmatched_names(symtab);
460     }
461
462   // Create any automatic note sections.
463   layout->create_notes();
464
465   // Create any output sections required by any linker script.
466   layout->create_script_sections();
467
468   // Define some sections and symbols needed for a dynamic link.  This
469   // handles some cases we want to see before we read the relocs.
470   layout->create_initial_dynamic_sections(symtab);
471
472   // Define symbols from any linker scripts.
473   layout->define_script_symbols(symtab);
474
475   // Attach sections to segments.
476   layout->attach_sections_to_segments();
477
478   if (!parameters->options().relocatable())
479     {
480       // Predefine standard symbols.
481       define_standard_symbols(symtab, layout);
482
483       // Define __start and __stop symbols for output sections where
484       // appropriate.
485       layout->define_section_symbols(symtab);
486     }
487
488   // Make sure we have symbols for any required group signatures.
489   layout->define_group_signatures(symtab);
490
491   Task_token* this_blocker = NULL;
492
493   // Allocate common symbols.  We use a blocker to run this before the
494   // Scan_relocs tasks, because it writes to the symbol table just as
495   // they do.
496   if (parameters->options().define_common())
497     {
498       this_blocker = new Task_token(true);
499       this_blocker->add_blocker();
500       workqueue->queue(new Allocate_commons_task(symtab, layout, mapfile,
501                                                  this_blocker));
502     }
503
504   // If doing garbage collection, the relocations have already been read.
505   // Otherwise, read and scan the relocations.
506   if (parameters->options().gc_sections()
507       || parameters->options().icf_enabled())
508     {
509       for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
510            p != input_objects->relobj_end();
511            ++p)
512         {
513           Task_token* next_blocker = new Task_token(true);
514           next_blocker->add_blocker();
515           workqueue->queue(new Scan_relocs(symtab, layout, *p, 
516                                            (*p)->get_relocs_data(),
517                                            this_blocker, next_blocker));
518           this_blocker = next_blocker;
519         }
520     }
521   else
522     {
523       // Read the relocations of the input files.  We do this to find
524       // which symbols are used by relocations which require a GOT and/or
525       // a PLT entry, or a COPY reloc.  When we implement garbage
526       // collection we will do it here by reading the relocations in a
527       // breadth first search by references.
528       //
529       // We could also read the relocations during the first pass, and
530       // mark symbols at that time.  That is how the old GNU linker works.
531       // Doing that is more complex, since we may later decide to discard
532       // some of the sections, and thus change our minds about the types
533       // of references made to the symbols.
534       for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
535            p != input_objects->relobj_end();
536            ++p)
537         {
538           Task_token* next_blocker = new Task_token(true);
539           next_blocker->add_blocker();
540           workqueue->queue(new Read_relocs(symtab, layout, *p, this_blocker,
541                                            next_blocker));
542           this_blocker = next_blocker;
543         }
544     }
545
546   if (this_blocker == NULL)
547     {
548       if (input_objects->number_of_relobjs() == 0)
549         {
550           // If we are given only archives in input, we have no regular
551           // objects and THIS_BLOCKER is NULL here.  Create a dummy
552           // blocker here so that we can run the layout task immediately.
553           this_blocker = new Task_token(true);
554         }
555       else 
556         {
557           // If we failed to open any input files, it's possible for
558           // THIS_BLOCKER to be NULL here.  There's no real point in
559           // continuing if that happens.
560           gold_assert(parameters->errors()->error_count() > 0);
561           gold_exit(false);
562         }
563     }
564
565   // When all those tasks are complete, we can start laying out the
566   // output file.
567   // TODO(csilvers): figure out a more principled way to get the target
568   Target* target = const_cast<Target*>(&parameters->target());
569   workqueue->queue(new Task_function(new Layout_task_runner(options,
570                                                             input_objects,
571                                                             symtab,
572                                                             target,
573                                                             layout,
574                                                             mapfile),
575                                      this_blocker,
576                                      "Task_function Layout_task_runner"));
577 }
578
579 // Queue up the final set of tasks.  This is called at the end of
580 // Layout_task.
581
582 void
583 queue_final_tasks(const General_options& options,
584                   const Input_objects* input_objects,
585                   const Symbol_table* symtab,
586                   Layout* layout,
587                   Workqueue* workqueue,
588                   Output_file* of)
589 {
590   int thread_count = options.thread_count_final();
591   if (thread_count == 0)
592     thread_count = std::max(2, input_objects->number_of_input_objects());
593   workqueue->set_thread_count(thread_count);
594
595   bool any_postprocessing_sections = layout->any_postprocessing_sections();
596
597   // Use a blocker to wait until all the input sections have been
598   // written out.
599   Task_token* input_sections_blocker = NULL;
600   if (!any_postprocessing_sections)
601     {
602       input_sections_blocker = new Task_token(true);
603       input_sections_blocker->add_blockers(input_objects->number_of_relobjs());
604     }
605
606   // Use a blocker to block any objects which have to wait for the
607   // output sections to complete before they can apply relocations.
608   Task_token* output_sections_blocker = new Task_token(true);
609   output_sections_blocker->add_blocker();
610
611   // Use a blocker to block the final cleanup task.
612   Task_token* final_blocker = new Task_token(true);
613   // Write_symbols_task, Write_sections_task, Write_data_task,
614   // Relocate_tasks.
615   final_blocker->add_blockers(3);
616   final_blocker->add_blockers(input_objects->number_of_relobjs());
617   if (!any_postprocessing_sections)
618     final_blocker->add_blocker();
619
620   // Queue a task to write out the symbol table.
621   workqueue->queue(new Write_symbols_task(layout,
622                                           symtab,
623                                           input_objects,
624                                           layout->sympool(),
625                                           layout->dynpool(),
626                                           of,
627                                           final_blocker));
628
629   // Queue a task to write out the output sections.
630   workqueue->queue(new Write_sections_task(layout, of, output_sections_blocker,
631                                            final_blocker));
632
633   // Queue a task to write out everything else.
634   workqueue->queue(new Write_data_task(layout, symtab, of, final_blocker));
635
636   // Queue a task for each input object to relocate the sections and
637   // write out the local symbols.
638   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
639        p != input_objects->relobj_end();
640        ++p)
641     workqueue->queue(new Relocate_task(symtab, layout, *p, of,
642                                        input_sections_blocker,
643                                        output_sections_blocker,
644                                        final_blocker));
645
646   // Queue a task to write out the output sections which depend on
647   // input sections.  If there are any sections which require
648   // postprocessing, then we need to do this last, since it may resize
649   // the output file.
650   if (!any_postprocessing_sections)
651     {
652       Task* t = new Write_after_input_sections_task(layout, of,
653                                                     input_sections_blocker,
654                                                     final_blocker);
655       workqueue->queue(t);
656     }
657   else
658     {
659       Task_token* new_final_blocker = new Task_token(true);
660       new_final_blocker->add_blocker();
661       Task* t = new Write_after_input_sections_task(layout, of,
662                                                     final_blocker,
663                                                     new_final_blocker);
664       workqueue->queue(t);
665       final_blocker = new_final_blocker;
666     }
667
668   // Queue a task to close the output file.  This will be blocked by
669   // FINAL_BLOCKER.
670   workqueue->queue(new Task_function(new Close_task_runner(&options, layout,
671                                                            of),
672                                      final_blocker,
673                                      "Task_function Close_task_runner"));
674 }
675
676 } // End namespace gold.