OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [gold/] [gold.cc] - Blame information for rev 299

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 khays
// gold.cc -- main linker functions
2
 
3 159 khays
// Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 27 khays
// 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 159 khays
#include "target-select.h"
34 27 khays
#include "debug.h"
35
#include "workqueue.h"
36
#include "dirsearch.h"
37
#include "readsyms.h"
38
#include "symtab.h"
39
#include "common.h"
40
#include "object.h"
41
#include "layout.h"
42
#include "reloc.h"
43
#include "defstd.h"
44
#include "plugin.h"
45
#include "gc.h"
46
#include "icf.h"
47
#include "incremental.h"
48 163 khays
#include "timer.h"
49 27 khays
 
50
namespace gold
51
{
52
 
53
class Object;
54
 
55
const char* program_name;
56
 
57
static Task*
58
process_incremental_input(Incremental_binary*, unsigned int, Input_objects*,
59
                          Symbol_table*, Layout*, Dirsearch*, Mapfile*,
60
                          Task_token*, Task_token*);
61
 
62
void
63 148 khays
gold_exit(Exit_status status)
64 27 khays
{
65
  if (parameters != NULL
66
      && parameters->options_valid()
67
      && parameters->options().has_plugins())
68
    parameters->options().plugins()->cleanup();
69 148 khays
  if (status != GOLD_OK && parameters != NULL && parameters->options_valid())
70 27 khays
    unlink_if_ordinary(parameters->options().output_file_name());
71 148 khays
  exit(status);
72 27 khays
}
73
 
74
void
75
gold_nomem()
76
{
77
  // We are out of memory, so try hard to print a reasonable message.
78
  // Note that we don't try to translate this message, since the
79
  // translation process itself will require memory.
80
 
81
  // LEN only exists to avoid a pointless warning when write is
82
  // declared with warn_use_result, as when compiling with
83
  // -D_USE_FORTIFY on GNU/Linux.  Casting to void does not appear to
84
  // work, at least not with gcc 4.3.0.
85
 
86
  ssize_t len = write(2, program_name, strlen(program_name));
87
  if (len >= 0)
88
    {
89
      const char* const s = ": out of memory\n";
90
      len = write(2, s, strlen(s));
91
    }
92 148 khays
  gold_exit(GOLD_ERR);
93 27 khays
}
94
 
95
// Handle an unreachable case.
96
 
97
void
98
do_gold_unreachable(const char* filename, int lineno, const char* function)
99
{
100
  fprintf(stderr, _("%s: internal error in %s, at %s:%d\n"),
101
          program_name, function, filename, lineno);
102 148 khays
  gold_exit(GOLD_ERR);
103 27 khays
}
104
 
105
// This class arranges to run the functions done in the middle of the
106
// link.  It is just a closure.
107
 
108
class Middle_runner : public Task_function_runner
109
{
110
 public:
111
  Middle_runner(const General_options& options,
112
                const Input_objects* input_objects,
113
                Symbol_table* symtab,
114
                Layout* layout, Mapfile* mapfile)
115
    : options_(options), input_objects_(input_objects), symtab_(symtab),
116
      layout_(layout), mapfile_(mapfile)
117
  { }
118
 
119
  void
120
  run(Workqueue*, const Task*);
121
 
122
 private:
123
  const General_options& options_;
124
  const Input_objects* input_objects_;
125
  Symbol_table* symtab_;
126
  Layout* layout_;
127
  Mapfile* mapfile_;
128
};
129
 
130
void
131
Middle_runner::run(Workqueue* workqueue, const Task* task)
132
{
133
  queue_middle_tasks(this->options_, task, this->input_objects_, this->symtab_,
134
                     this->layout_, workqueue, this->mapfile_);
135
}
136
 
137
// This class arranges the tasks to process the relocs for garbage collection.
138
 
139
class Gc_runner : public Task_function_runner
140
{
141
  public:
142
   Gc_runner(const General_options& options,
143
             const Input_objects* input_objects,
144
             Symbol_table* symtab,
145
             Layout* layout, Mapfile* mapfile)
146
    : options_(options), input_objects_(input_objects), symtab_(symtab),
147
      layout_(layout), mapfile_(mapfile)
148
   { }
149
 
150
  void
151
  run(Workqueue*, const Task*);
152
 
153
 private:
154
  const General_options& options_;
155
  const Input_objects* input_objects_;
156
  Symbol_table* symtab_;
157
  Layout* layout_;
158
  Mapfile* mapfile_;
159
};
160
 
161
void
162
Gc_runner::run(Workqueue* workqueue, const Task* task)
163
{
164
  queue_middle_gc_tasks(this->options_, task, this->input_objects_,
165
                        this->symtab_, this->layout_, workqueue,
166
                        this->mapfile_);
167
}
168
 
169
// Queue up the initial set of tasks for this link job.
170
 
171
void
172
queue_initial_tasks(const General_options& options,
173
                    Dirsearch& search_path,
174
                    const Command_line& cmdline,
175
                    Workqueue* workqueue, Input_objects* input_objects,
176
                    Symbol_table* symtab, Layout* layout, Mapfile* mapfile)
177
{
178
  if (cmdline.begin() == cmdline.end())
179
    {
180 159 khays
      bool is_ok = false;
181 27 khays
      if (options.printed_version())
182 159 khays
        is_ok = true;
183
      if (options.print_output_format())
184
        {
185
          print_output_format();
186
          is_ok = true;
187
        }
188
      if (is_ok)
189 148 khays
        gold_exit(GOLD_OK);
190 27 khays
      gold_fatal(_("no input files"));
191
    }
192
 
193
  int thread_count = options.thread_count_initial();
194
  if (thread_count == 0)
195
    thread_count = cmdline.number_of_input_files();
196
  workqueue->set_thread_count(thread_count);
197
 
198
  // For incremental links, the base output file.
199
  Incremental_binary* ibase = NULL;
200
 
201 163 khays
  if (parameters->incremental_update())
202 27 khays
    {
203 163 khays
      Output_file* of = new Output_file(options.output_file_name());
204
      if (of->open_base_file(options.incremental_base(), true))
205 27 khays
        {
206 163 khays
          ibase = open_incremental_binary(of);
207
          if (ibase != NULL
208
              && ibase->check_inputs(cmdline, layout->incremental_inputs()))
209
            ibase->init_layout(layout);
210
          else
211 27 khays
            {
212 163 khays
              delete ibase;
213
              ibase = NULL;
214
              of->close();
215 27 khays
            }
216
        }
217 163 khays
      if (ibase == NULL)
218
        {
219
          if (set_parameters_incremental_full())
220
            gold_info(_("linking with --incremental-full"));
221
          else
222
            gold_fallback(_("restart link with --incremental-full"));
223
        }
224 27 khays
    }
225
 
226
  // Read the input files.  We have to add the symbols to the symbol
227
  // table in order.  We do this by creating a separate blocker for
228
  // each input file.  We associate the blocker with the following
229
  // input file, to give us a convenient place to delete it.
230
  Task_token* this_blocker = NULL;
231
  if (ibase == NULL)
232
    {
233
      // Normal link.  Queue a Read_symbols task for each input file
234
      // on the command line.
235
      for (Command_line::const_iterator p = cmdline.begin();
236
           p != cmdline.end();
237
           ++p)
238
        {
239
          Task_token* next_blocker = new Task_token(true);
240
          next_blocker->add_blocker();
241
          workqueue->queue(new Read_symbols(input_objects, symtab, layout,
242
                                            &search_path, 0, mapfile, &*p, NULL,
243
                                            NULL, this_blocker, next_blocker));
244
          this_blocker = next_blocker;
245
        }
246
    }
247
  else
248
    {
249
      // Incremental update link.  Process the list of input files
250
      // stored in the base file, and queue a task for each file:
251
      // a Read_symbols task for a changed file, and an Add_symbols task
252
      // for an unchanged file.  We need to mark all the space used by
253
      // unchanged files before we can start any tasks running.
254
      unsigned int input_file_count = ibase->input_file_count();
255
      std::vector<Task*> tasks;
256
      tasks.reserve(input_file_count);
257
      for (unsigned int i = 0; i < input_file_count; ++i)
258
        {
259
          Task_token* next_blocker = new Task_token(true);
260
          next_blocker->add_blocker();
261
          Task* t = process_incremental_input(ibase, i, input_objects, symtab,
262
                                              layout, &search_path, mapfile,
263
                                              this_blocker, next_blocker);
264
          tasks.push_back(t);
265
          this_blocker = next_blocker;
266
        }
267
      // Now we can queue the tasks.
268
      for (unsigned int i = 0; i < tasks.size(); i++)
269
        workqueue->queue(tasks[i]);
270
    }
271
 
272
  if (options.has_plugins())
273
    {
274
      Task_token* next_blocker = new Task_token(true);
275
      next_blocker->add_blocker();
276
      workqueue->queue(new Plugin_hook(options, input_objects, symtab, layout,
277
                                       &search_path, mapfile, this_blocker,
278
                                       next_blocker));
279
      this_blocker = next_blocker;
280
    }
281
 
282
  if (options.relocatable()
283
      && (options.gc_sections() || options.icf_enabled()))
284
    gold_error(_("cannot mix -r with --gc-sections or --icf"));
285
 
286
  if (options.gc_sections() || options.icf_enabled())
287
    {
288
      workqueue->queue(new Task_function(new Gc_runner(options,
289
                                                       input_objects,
290
                                                       symtab,
291
                                                       layout,
292
                                                       mapfile),
293
                                         this_blocker,
294
                                         "Task_function Gc_runner"));
295
    }
296
  else
297
    {
298
      workqueue->queue(new Task_function(new Middle_runner(options,
299
                                                           input_objects,
300
                                                           symtab,
301
                                                           layout,
302
                                                           mapfile),
303
                                         this_blocker,
304
                                         "Task_function Middle_runner"));
305
    }
306
}
307
 
308
// Process an incremental input file: if it is unchanged from the previous
309
// link, return a task to add its symbols from the base file's incremental
310
// info; if it has changed, return a normal Read_symbols task.  We create a
311
// task for every input file, if only to report the file for rebuilding the
312
// incremental info.
313
 
314
static Task*
315
process_incremental_input(Incremental_binary* ibase,
316
                          unsigned int input_file_index,
317
                          Input_objects* input_objects,
318
                          Symbol_table* symtab,
319
                          Layout* layout,
320
                          Dirsearch* search_path,
321
                          Mapfile* mapfile,
322
                          Task_token* this_blocker,
323
                          Task_token* next_blocker)
324
{
325
  const Incremental_binary::Input_reader* input_reader =
326
      ibase->get_input_reader(input_file_index);
327
  Incremental_input_type input_type = input_reader->type();
328
 
329
  // Get the input argument corresponding to this input file, matching on
330
  // the argument serial number.  If the input file cannot be matched
331
  // to an existing input argument, synthesize a new one.
332
  const Input_argument* input_argument =
333
      ibase->get_input_argument(input_file_index);
334
  if (input_argument == NULL)
335
    {
336
      Input_file_argument file(input_reader->filename(),
337
                               Input_file_argument::INPUT_FILE_TYPE_FILE,
338
                               "", false, parameters->options());
339
      Input_argument* arg = new Input_argument(file);
340
      arg->set_script_info(ibase->get_script_info(input_file_index));
341
      input_argument = arg;
342
    }
343
 
344
  gold_debug(DEBUG_INCREMENTAL, "Incremental object: %s, type %d",
345
             input_reader->filename(), input_type);
346
 
347
  if (input_type == INCREMENTAL_INPUT_SCRIPT)
348
    {
349
      // Incremental_binary::check_inputs should have cancelled the
350
      // incremental update if the script has changed.
351
      gold_assert(!ibase->file_has_changed(input_file_index));
352
      return new Check_script(layout, ibase, input_file_index, input_reader,
353
                              this_blocker, next_blocker);
354
    }
355
 
356
  if (input_type == INCREMENTAL_INPUT_ARCHIVE)
357
    {
358
      Incremental_library* lib = ibase->get_library(input_file_index);
359
      gold_assert(lib != NULL);
360
      if (lib->filename() == "/group/"
361
          || !ibase->file_has_changed(input_file_index))
362
        {
363
          // Queue a task to check that no references have been added to any
364
          // of the library's unused symbols.
365
          return new Check_library(symtab, layout, ibase, input_file_index,
366
                                   input_reader, this_blocker, next_blocker);
367
        }
368
      else
369
        {
370
          // Queue a Read_symbols task to process the archive normally.
371
          return new Read_symbols(input_objects, symtab, layout, search_path,
372
                                  0, mapfile, input_argument, NULL, NULL,
373
                                  this_blocker, next_blocker);
374
        }
375
    }
376
 
377
  if (input_type == INCREMENTAL_INPUT_ARCHIVE_MEMBER)
378
    {
379
      // For archive members, check the timestamp of the containing archive.
380
      Incremental_library* lib = ibase->get_library(input_file_index);
381
      gold_assert(lib != NULL);
382
      // Process members of a --start-lib/--end-lib group as normal objects.
383
      if (lib->filename() != "/group/")
384
        {
385
          if (ibase->file_has_changed(lib->input_file_index()))
386
            {
387
              return new Read_member(input_objects, symtab, layout, mapfile,
388
                                     input_reader, this_blocker, next_blocker);
389
            }
390
          else
391
            {
392
              // The previous contributions from this file will be kept.
393
              // Mark the pieces of output sections contributed by this
394
              // object.
395
              ibase->reserve_layout(input_file_index);
396
              Object* obj = make_sized_incremental_object(ibase,
397
                                                          input_file_index,
398
                                                          input_type,
399
                                                          input_reader);
400
              return new Add_symbols(input_objects, symtab, layout,
401
                                     search_path, 0, mapfile, input_argument,
402
                                     obj, lib, NULL, this_blocker,
403
                                     next_blocker);
404
            }
405
        }
406
    }
407
 
408
  // Normal object file or shared library.  Check if the file has changed
409
  // since the last incremental link.
410
  if (ibase->file_has_changed(input_file_index))
411
    {
412
      return new Read_symbols(input_objects, symtab, layout, search_path, 0,
413
                              mapfile, input_argument, NULL, NULL,
414
                              this_blocker, next_blocker);
415
    }
416
  else
417
    {
418
      // The previous contributions from this file will be kept.
419
      // Mark the pieces of output sections contributed by this object.
420
      ibase->reserve_layout(input_file_index);
421
      Object* obj = make_sized_incremental_object(ibase,
422
                                                  input_file_index,
423
                                                  input_type,
424
                                                  input_reader);
425
      return new Add_symbols(input_objects, symtab, layout, search_path, 0,
426
                             mapfile, input_argument, obj, NULL, NULL,
427
                             this_blocker, next_blocker);
428
    }
429
}
430
 
431
// Queue up a set of tasks to be done before queueing the middle set
432
// of tasks.  This is only necessary when garbage collection
433
// (--gc-sections) of unused sections is desired.  The relocs are read
434
// and processed here early to determine the garbage sections before the
435
// relocs can be scanned in later tasks.
436
 
437
void
438
queue_middle_gc_tasks(const General_options& options,
439
                      const Task* ,
440
                      const Input_objects* input_objects,
441
                      Symbol_table* symtab,
442
                      Layout* layout,
443
                      Workqueue* workqueue,
444
                      Mapfile* mapfile)
445
{
446
  // Read_relocs for all the objects must be done and processed to find
447
  // unused sections before any scanning of the relocs can take place.
448
  Task_token* this_blocker = NULL;
449
  for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
450
       p != input_objects->relobj_end();
451
       ++p)
452
    {
453
      Task_token* next_blocker = new Task_token(true);
454
      next_blocker->add_blocker();
455
      workqueue->queue(new Read_relocs(symtab, layout, *p, this_blocker,
456
                                       next_blocker));
457
      this_blocker = next_blocker;
458
    }
459
 
460
  // If we are given only archives in input, we have no regular
461
  // objects and THIS_BLOCKER is NULL here.  Create a dummy
462
  // blocker here so that we can run the middle tasks immediately.
463
  if (this_blocker == NULL)
464
    {
465
      gold_assert(input_objects->number_of_relobjs() == 0);
466
      this_blocker = new Task_token(true);
467
    }
468
 
469
  workqueue->queue(new Task_function(new Middle_runner(options,
470
                                                       input_objects,
471
                                                       symtab,
472
                                                       layout,
473
                                                       mapfile),
474
                                     this_blocker,
475
                                     "Task_function Middle_runner"));
476
}
477
 
478
// Queue up the middle set of tasks.  These are the tasks which run
479
// after all the input objects have been found and all the symbols
480
// have been read, but before we lay out the output file.
481
 
482
void
483
queue_middle_tasks(const General_options& options,
484
                   const Task* task,
485
                   const Input_objects* input_objects,
486
                   Symbol_table* symtab,
487
                   Layout* layout,
488
                   Workqueue* workqueue,
489
                   Mapfile* mapfile)
490
{
491 163 khays
  Timer* timer = parameters->timer();
492
  if (timer != NULL)
493
    timer->stamp(0);
494
 
495 27 khays
  // Add any symbols named with -u options to the symbol table.
496
  symtab->add_undefined_symbols_from_command_line(layout);
497
 
498
  // If garbage collection was chosen, relocs have been read and processed
499
  // at this point by pre_middle_tasks.  Layout can then be done for all 
500
  // objects.
501
  if (parameters->options().gc_sections())
502
    {
503
      // Find the start symbol if any.
504
      Symbol* start_sym = symtab->lookup(parameters->entry());
505
      if (start_sym != NULL)
506
        {
507
          bool is_ordinary;
508
          unsigned int shndx = start_sym->shndx(&is_ordinary);
509
          if (is_ordinary)
510
            {
511
              symtab->gc()->worklist().push(
512
                Section_id(start_sym->object(), shndx));
513
            }
514
        }
515
      // Symbols named with -u should not be considered garbage.
516
      symtab->gc_mark_undef_symbols(layout);
517
      gold_assert(symtab->gc() != NULL);
518
      // Do a transitive closure on all references to determine the worklist.
519
      symtab->gc()->do_transitive_closure();
520
    }
521
 
522
  // If identical code folding (--icf) is chosen it makes sense to do it 
523
  // only after garbage collection (--gc-sections) as we do not want to 
524
  // be folding sections that will be garbage.
525
  if (parameters->options().icf_enabled())
526
    {
527
      symtab->icf()->find_identical_sections(input_objects, symtab);
528
    }
529
 
530
  // Call Object::layout for the second time to determine the 
531
  // output_sections for all referenced input sections.  When 
532
  // --gc-sections or --icf is turned on, Object::layout is 
533
  // called twice.  It is called the first time when the 
534
  // symbols are added.
535
  if (parameters->options().gc_sections()
536
      || parameters->options().icf_enabled())
537
    {
538
      for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
539
           p != input_objects->relobj_end();
540
           ++p)
541
        {
542
          Task_lock_obj<Object> tlo(task, *p);
543
          (*p)->layout(symtab, layout, NULL);
544
        }
545
    }
546
 
547 163 khays
  /* If plugins have specified a section order, re-arrange input sections
548
     according to a specified section order.  If --section-ordering-file is
549
     also specified, do not do anything here.  */
550
  if (parameters->options().has_plugins()
551
      && layout->is_section_ordering_specified()
552
      && !parameters->options().section_ordering_file ())
553
    {
554
      for (Layout::Section_list::const_iterator p
555
             = layout->section_list().begin();
556
           p != layout->section_list().end();
557
           ++p)
558
        (*p)->update_section_layout(layout->get_section_order_map());
559
    }
560
 
561 27 khays
  // Layout deferred objects due to plugins.
562
  if (parameters->options().has_plugins())
563
    {
564
      Plugin_manager* plugins = parameters->options().plugins();
565
      gold_assert(plugins != NULL);
566
      plugins->layout_deferred_objects();
567
    }
568
 
569
  if (parameters->options().gc_sections()
570
      || parameters->options().icf_enabled())
571
    {
572
      for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
573
           p != input_objects->relobj_end();
574
           ++p)
575
        {
576
          // Update the value of output_section stored in rd.
577
          Read_relocs_data* rd = (*p)->get_relocs_data();
578
          for (Read_relocs_data::Relocs_list::iterator q = rd->relocs.begin();
579
               q != rd->relocs.end();
580
               ++q)
581
            {
582
              q->output_section = (*p)->output_section(q->data_shndx);
583
              q->needs_special_offset_handling =
584
                      (*p)->is_output_section_offset_invalid(q->data_shndx);
585
            }
586
        }
587
    }
588
 
589
  // We have to support the case of not seeing any input objects, and
590
  // generate an empty file.  Existing builds depend on being able to
591
  // pass an empty archive to the linker and get an empty object file
592
  // out.  In order to do this we need to use a default target.
593
  if (input_objects->number_of_input_objects() == 0
594
      && layout->incremental_base() == NULL)
595
    parameters_force_valid_target();
596
 
597
  int thread_count = options.thread_count_middle();
598
  if (thread_count == 0)
599
    thread_count = std::max(2, input_objects->number_of_input_objects());
600
  workqueue->set_thread_count(thread_count);
601
 
602
  // Now we have seen all the input files.
603
  const bool doing_static_link =
604
    (!input_objects->any_dynamic()
605
     && !parameters->options().output_is_position_independent());
606
  set_parameters_doing_static_link(doing_static_link);
607
  if (!doing_static_link && options.is_static())
608
    {
609
      // We print out just the first .so we see; there may be others.
610
      gold_assert(input_objects->dynobj_begin() != input_objects->dynobj_end());
611
      gold_error(_("cannot mix -static with dynamic object %s"),
612
                 (*input_objects->dynobj_begin())->name().c_str());
613
    }
614
  if (!doing_static_link && parameters->options().relocatable())
615
    gold_fatal(_("cannot mix -r with dynamic object %s"),
616
               (*input_objects->dynobj_begin())->name().c_str());
617
  if (!doing_static_link
618
      && options.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
619
    gold_fatal(_("cannot use non-ELF output format with dynamic object %s"),
620
               (*input_objects->dynobj_begin())->name().c_str());
621
 
622
  if (parameters->options().relocatable())
623
    {
624
      Input_objects::Relobj_iterator p = input_objects->relobj_begin();
625
      if (p != input_objects->relobj_end())
626
        {
627
          bool uses_split_stack = (*p)->uses_split_stack();
628
          for (++p; p != input_objects->relobj_end(); ++p)
629
            {
630
              if ((*p)->uses_split_stack() != uses_split_stack)
631
                gold_fatal(_("cannot mix split-stack '%s' and "
632
                             "non-split-stack '%s' when using -r"),
633
                           (*input_objects->relobj_begin())->name().c_str(),
634
                           (*p)->name().c_str());
635
            }
636
        }
637
    }
638
 
639 148 khays
  // For incremental updates, record the existing GOT and PLT entries,
640
  // and the COPY relocations.
641 27 khays
  if (parameters->incremental_update())
642
    {
643
      Incremental_binary* ibase = layout->incremental_base();
644
      ibase->process_got_plt(symtab, layout);
645 148 khays
      ibase->emit_copy_relocs(symtab);
646 27 khays
    }
647
 
648
  if (is_debugging_enabled(DEBUG_SCRIPT))
649
    layout->script_options()->print(stderr);
650
 
651
  // For each dynamic object, record whether we've seen all the
652
  // dynamic objects that it depends upon.
653
  input_objects->check_dynamic_dependencies();
654
 
655
  // See if any of the input definitions violate the One Definition Rule.
656
  // TODO: if this is too slow, do this as a task, rather than inline.
657
  symtab->detect_odr_violations(task, options.output_file_name());
658
 
659
  // Do the --no-undefined-version check.
660
  if (!parameters->options().undefined_version())
661
    {
662
      Script_options* so = layout->script_options();
663
      so->version_script_info()->check_unmatched_names(symtab);
664
    }
665
 
666
  // Create any automatic note sections.
667
  layout->create_notes();
668
 
669
  // Create any output sections required by any linker script.
670
  layout->create_script_sections();
671
 
672
  // Define some sections and symbols needed for a dynamic link.  This
673
  // handles some cases we want to see before we read the relocs.
674
  layout->create_initial_dynamic_sections(symtab);
675
 
676
  // Define symbols from any linker scripts.
677
  layout->define_script_symbols(symtab);
678
 
679
  // Attach sections to segments.
680
  layout->attach_sections_to_segments();
681
 
682
  if (!parameters->options().relocatable())
683
    {
684
      // Predefine standard symbols.
685
      define_standard_symbols(symtab, layout);
686
 
687
      // Define __start and __stop symbols for output sections where
688
      // appropriate.
689
      layout->define_section_symbols(symtab);
690
    }
691
 
692
  // Make sure we have symbols for any required group signatures.
693
  layout->define_group_signatures(symtab);
694
 
695
  Task_token* this_blocker = NULL;
696
 
697
  // Allocate common symbols.  We use a blocker to run this before the
698
  // Scan_relocs tasks, because it writes to the symbol table just as
699
  // they do.
700
  if (parameters->options().define_common())
701
    {
702
      this_blocker = new Task_token(true);
703
      this_blocker->add_blocker();
704
      workqueue->queue(new Allocate_commons_task(symtab, layout, mapfile,
705
                                                 this_blocker));
706
    }
707
 
708
  // If doing garbage collection, the relocations have already been read.
709
  // Otherwise, read and scan the relocations.
710
  if (parameters->options().gc_sections()
711
      || parameters->options().icf_enabled())
712
    {
713
      for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
714
           p != input_objects->relobj_end();
715
           ++p)
716
        {
717
          Task_token* next_blocker = new Task_token(true);
718
          next_blocker->add_blocker();
719
          workqueue->queue(new Scan_relocs(symtab, layout, *p,
720
                                           (*p)->get_relocs_data(),
721
                                           this_blocker, next_blocker));
722
          this_blocker = next_blocker;
723
        }
724
    }
725
  else
726
    {
727
      // Read the relocations of the input files.  We do this to find
728
      // which symbols are used by relocations which require a GOT and/or
729
      // a PLT entry, or a COPY reloc.  When we implement garbage
730
      // collection we will do it here by reading the relocations in a
731
      // breadth first search by references.
732
      //
733
      // We could also read the relocations during the first pass, and
734
      // mark symbols at that time.  That is how the old GNU linker works.
735
      // Doing that is more complex, since we may later decide to discard
736
      // some of the sections, and thus change our minds about the types
737
      // of references made to the symbols.
738
      for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
739
           p != input_objects->relobj_end();
740
           ++p)
741
        {
742
          Task_token* next_blocker = new Task_token(true);
743
          next_blocker->add_blocker();
744
          workqueue->queue(new Read_relocs(symtab, layout, *p, this_blocker,
745
                                           next_blocker));
746
          this_blocker = next_blocker;
747
        }
748
    }
749
 
750
  if (this_blocker == NULL)
751
    {
752
      if (input_objects->number_of_relobjs() == 0)
753
        {
754
          // If we are given only archives in input, we have no regular
755
          // objects and THIS_BLOCKER is NULL here.  Create a dummy
756
          // blocker here so that we can run the layout task immediately.
757
          this_blocker = new Task_token(true);
758
        }
759
      else
760
        {
761
          // If we failed to open any input files, it's possible for
762
          // THIS_BLOCKER to be NULL here.  There's no real point in
763
          // continuing if that happens.
764
          gold_assert(parameters->errors()->error_count() > 0);
765 148 khays
          gold_exit(GOLD_ERR);
766 27 khays
        }
767
    }
768
 
769
  // When all those tasks are complete, we can start laying out the
770
  // output file.
771
  // TODO(csilvers): figure out a more principled way to get the target
772
  Target* target = const_cast<Target*>(&parameters->target());
773
  workqueue->queue(new Task_function(new Layout_task_runner(options,
774
                                                            input_objects,
775
                                                            symtab,
776
                                                            target,
777
                                                            layout,
778
                                                            mapfile),
779
                                     this_blocker,
780
                                     "Task_function Layout_task_runner"));
781
}
782
 
783
// Queue up the final set of tasks.  This is called at the end of
784
// Layout_task.
785
 
786
void
787
queue_final_tasks(const General_options& options,
788
                  const Input_objects* input_objects,
789
                  const Symbol_table* symtab,
790
                  Layout* layout,
791
                  Workqueue* workqueue,
792
                  Output_file* of)
793
{
794 163 khays
  Timer* timer = parameters->timer();
795
  if (timer != NULL)
796
    timer->stamp(1);
797
 
798 27 khays
  int thread_count = options.thread_count_final();
799
  if (thread_count == 0)
800
    thread_count = std::max(2, input_objects->number_of_input_objects());
801
  workqueue->set_thread_count(thread_count);
802
 
803
  bool any_postprocessing_sections = layout->any_postprocessing_sections();
804
 
805
  // Use a blocker to wait until all the input sections have been
806
  // written out.
807
  Task_token* input_sections_blocker = NULL;
808
  if (!any_postprocessing_sections)
809
    {
810
      input_sections_blocker = new Task_token(true);
811
      input_sections_blocker->add_blockers(input_objects->number_of_relobjs());
812
    }
813
 
814
  // Use a blocker to block any objects which have to wait for the
815
  // output sections to complete before they can apply relocations.
816
  Task_token* output_sections_blocker = new Task_token(true);
817
  output_sections_blocker->add_blocker();
818
 
819
  // Use a blocker to block the final cleanup task.
820
  Task_token* final_blocker = new Task_token(true);
821
  // Write_symbols_task, Write_sections_task, Write_data_task,
822
  // Relocate_tasks.
823
  final_blocker->add_blockers(3);
824
  final_blocker->add_blockers(input_objects->number_of_relobjs());
825
  if (!any_postprocessing_sections)
826
    final_blocker->add_blocker();
827
 
828
  // Queue a task to write out the symbol table.
829
  workqueue->queue(new Write_symbols_task(layout,
830
                                          symtab,
831
                                          input_objects,
832
                                          layout->sympool(),
833
                                          layout->dynpool(),
834
                                          of,
835
                                          final_blocker));
836
 
837
  // Queue a task to write out the output sections.
838
  workqueue->queue(new Write_sections_task(layout, of, output_sections_blocker,
839
                                           final_blocker));
840
 
841
  // Queue a task to write out everything else.
842
  workqueue->queue(new Write_data_task(layout, symtab, of, final_blocker));
843
 
844
  // Queue a task for each input object to relocate the sections and
845
  // write out the local symbols.
846
  for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
847
       p != input_objects->relobj_end();
848
       ++p)
849
    workqueue->queue(new Relocate_task(symtab, layout, *p, of,
850
                                       input_sections_blocker,
851
                                       output_sections_blocker,
852
                                       final_blocker));
853
 
854
  // Queue a task to write out the output sections which depend on
855
  // input sections.  If there are any sections which require
856
  // postprocessing, then we need to do this last, since it may resize
857
  // the output file.
858
  if (!any_postprocessing_sections)
859
    {
860
      Task* t = new Write_after_input_sections_task(layout, of,
861
                                                    input_sections_blocker,
862
                                                    final_blocker);
863
      workqueue->queue(t);
864
    }
865
  else
866
    {
867
      Task_token* new_final_blocker = new Task_token(true);
868
      new_final_blocker->add_blocker();
869
      Task* t = new Write_after_input_sections_task(layout, of,
870
                                                    final_blocker,
871
                                                    new_final_blocker);
872
      workqueue->queue(t);
873
      final_blocker = new_final_blocker;
874
    }
875
 
876
  // Queue a task to close the output file.  This will be blocked by
877
  // FINAL_BLOCKER.
878
  workqueue->queue(new Task_function(new Close_task_runner(&options, layout,
879
                                                           of),
880
                                     final_blocker,
881
                                     "Task_function Close_task_runner"));
882
}
883
 
884
} // End namespace gold.

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.