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

Subversion Repositories open8_urisc

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

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

Line No. Rev Author Line
1 27 khays
// inremental.cc -- incremental linking support for gold
2
 
3 159 khays
// Copyright 2009, 2010, 2011 Free Software Foundation, Inc.
4 27 khays
// Written by Mikolaj Zalewski <mikolajz@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 148 khays
#include <set>
26 27 khays
#include <cstdarg>
27
#include "libiberty.h"
28
 
29
#include "elfcpp.h"
30
#include "options.h"
31
#include "output.h"
32
#include "symtab.h"
33
#include "incremental.h"
34
#include "archive.h"
35
#include "object.h"
36
#include "output.h"
37
#include "target-select.h"
38
#include "target.h"
39
#include "fileread.h"
40
#include "script.h"
41
 
42
namespace gold {
43
 
44
// Version information. Will change frequently during the development, later
45
// we could think about backward (and forward?) compatibility.
46
const unsigned int INCREMENTAL_LINK_VERSION = 1;
47
 
48
// This class manages the .gnu_incremental_inputs section, which holds
49
// the header information, a directory of input files, and separate
50
// entries for each input file.
51
 
52
template<int size, bool big_endian>
53
class Output_section_incremental_inputs : public Output_section_data
54
{
55
 public:
56
  Output_section_incremental_inputs(const Incremental_inputs* inputs,
57
                                    const Symbol_table* symtab)
58
    : Output_section_data(size / 8), inputs_(inputs), symtab_(symtab)
59
  { }
60
 
61
 protected:
62
  // This is called to update the section size prior to assigning
63
  // the address and file offset.
64
  void
65
  update_data_size()
66
  { this->set_final_data_size(); }
67
 
68
  // Set the final data size.
69
  void
70
  set_final_data_size();
71
 
72
  // Write the data to the file.
73
  void
74
  do_write(Output_file*);
75
 
76
  // Write to a map file.
77
  void
78
  do_print_to_mapfile(Mapfile* mapfile) const
79
  { mapfile->print_output_data(this, _("** incremental_inputs")); }
80
 
81
 private:
82
  // Write the section header.
83
  unsigned char*
84
  write_header(unsigned char* pov, unsigned int input_file_count,
85
               section_offset_type command_line_offset);
86
 
87
  // Write the input file entries.
88
  unsigned char*
89
  write_input_files(unsigned char* oview, unsigned char* pov,
90
                    Stringpool* strtab);
91
 
92
  // Write the supplemental information blocks.
93
  unsigned char*
94
  write_info_blocks(unsigned char* oview, unsigned char* pov,
95
                    Stringpool* strtab, unsigned int* global_syms,
96
                    unsigned int global_sym_count);
97
 
98
  // Write the contents of the .gnu_incremental_symtab section.
99
  void
100
  write_symtab(unsigned char* pov, unsigned int* global_syms,
101
               unsigned int global_sym_count);
102
 
103
  // Write the contents of the .gnu_incremental_got_plt section.
104
  void
105
  write_got_plt(unsigned char* pov, off_t view_size);
106
 
107
  // Typedefs for writing the data to the output sections.
108
  typedef elfcpp::Swap<size, big_endian> Swap;
109
  typedef elfcpp::Swap<16, big_endian> Swap16;
110
  typedef elfcpp::Swap<32, big_endian> Swap32;
111
  typedef elfcpp::Swap<64, big_endian> Swap64;
112
 
113
  // Sizes of various structures.
114
  static const int sizeof_addr = size / 8;
115
  static const int header_size = 16;
116
  static const int input_entry_size = 24;
117
 
118
  // The Incremental_inputs object.
119
  const Incremental_inputs* inputs_;
120
 
121
  // The symbol table.
122
  const Symbol_table* symtab_;
123
};
124
 
125
// Inform the user why we don't do an incremental link.  Not called in
126
// the obvious case of missing output file.  TODO: Is this helpful?
127
 
128
void
129
vexplain_no_incremental(const char* format, va_list args)
130
{
131
  char* buf = NULL;
132
  if (vasprintf(&buf, format, args) < 0)
133
    gold_nomem();
134
  gold_info(_("the link might take longer: "
135
              "cannot perform incremental link: %s"), buf);
136
  free(buf);
137
}
138
 
139
void
140
explain_no_incremental(const char* format, ...)
141
{
142
  va_list args;
143
  va_start(args, format);
144
  vexplain_no_incremental(format, args);
145
  va_end(args);
146
}
147
 
148
// Report an error.
149
 
150
void
151
Incremental_binary::error(const char* format, ...) const
152
{
153
  va_list args;
154
  va_start(args, format);
155
  // Current code only checks if the file can be used for incremental linking,
156
  // so errors shouldn't fail the build, but only result in a fallback to a
157
  // full build.
158
  // TODO: when we implement incremental editing of the file, we may need a
159
  // flag that will cause errors to be treated seriously.
160
  vexplain_no_incremental(format, args);
161
  va_end(args);
162
}
163
 
164 163 khays
// Return TRUE if a section of type SH_TYPE can be updated in place
165
// during an incremental update.  We can update sections of type PROGBITS,
166
// NOBITS, INIT_ARRAY, FINI_ARRAY, PREINIT_ARRAY, and NOTE.  All others
167
// will be regenerated.
168
 
169
bool
170
can_incremental_update(unsigned int sh_type)
171
{
172
  return (sh_type == elfcpp::SHT_PROGBITS
173
          || sh_type == elfcpp::SHT_NOBITS
174
          || sh_type == elfcpp::SHT_INIT_ARRAY
175
          || sh_type == elfcpp::SHT_FINI_ARRAY
176
          || sh_type == elfcpp::SHT_PREINIT_ARRAY
177
          || sh_type == elfcpp::SHT_NOTE);
178
}
179
 
180 27 khays
// Find the .gnu_incremental_inputs section and related sections.
181
 
182
template<int size, bool big_endian>
183
bool
184
Sized_incremental_binary<size, big_endian>::find_incremental_inputs_sections(
185
    unsigned int* p_inputs_shndx,
186
    unsigned int* p_symtab_shndx,
187
    unsigned int* p_relocs_shndx,
188
    unsigned int* p_got_plt_shndx,
189
    unsigned int* p_strtab_shndx)
190
{
191
  unsigned int inputs_shndx =
192
      this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_INPUTS);
193
  if (inputs_shndx == elfcpp::SHN_UNDEF)  // Not found.
194
    return false;
195
 
196
  unsigned int symtab_shndx =
197
      this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_SYMTAB);
198
  if (symtab_shndx == elfcpp::SHN_UNDEF)  // Not found.
199
    return false;
200
  if (this->elf_file_.section_link(symtab_shndx) != inputs_shndx)
201
    return false;
202
 
203
  unsigned int relocs_shndx =
204
      this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_RELOCS);
205
  if (relocs_shndx == elfcpp::SHN_UNDEF)  // Not found.
206
    return false;
207
  if (this->elf_file_.section_link(relocs_shndx) != inputs_shndx)
208
    return false;
209
 
210
  unsigned int got_plt_shndx =
211
      this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT);
212
  if (got_plt_shndx == elfcpp::SHN_UNDEF)  // Not found.
213
    return false;
214
  if (this->elf_file_.section_link(got_plt_shndx) != inputs_shndx)
215
    return false;
216
 
217
  unsigned int strtab_shndx = this->elf_file_.section_link(inputs_shndx);
218
  if (strtab_shndx == elfcpp::SHN_UNDEF
219
      || strtab_shndx > this->elf_file_.shnum()
220
      || this->elf_file_.section_type(strtab_shndx) != elfcpp::SHT_STRTAB)
221
    return false;
222
 
223
  if (p_inputs_shndx != NULL)
224
    *p_inputs_shndx = inputs_shndx;
225
  if (p_symtab_shndx != NULL)
226
    *p_symtab_shndx = symtab_shndx;
227
  if (p_relocs_shndx != NULL)
228
    *p_relocs_shndx = relocs_shndx;
229
  if (p_got_plt_shndx != NULL)
230
    *p_got_plt_shndx = got_plt_shndx;
231
  if (p_strtab_shndx != NULL)
232
    *p_strtab_shndx = strtab_shndx;
233
  return true;
234
}
235
 
236
// Set up the readers into the incremental info sections.
237
 
238
template<int size, bool big_endian>
239
void
240
Sized_incremental_binary<size, big_endian>::setup_readers()
241
{
242
  unsigned int inputs_shndx;
243
  unsigned int symtab_shndx;
244
  unsigned int relocs_shndx;
245
  unsigned int got_plt_shndx;
246
  unsigned int strtab_shndx;
247
 
248
  if (!this->find_incremental_inputs_sections(&inputs_shndx, &symtab_shndx,
249
                                              &relocs_shndx, &got_plt_shndx,
250
                                              &strtab_shndx))
251
    return;
252
 
253
  Location inputs_location(this->elf_file_.section_contents(inputs_shndx));
254
  Location symtab_location(this->elf_file_.section_contents(symtab_shndx));
255
  Location relocs_location(this->elf_file_.section_contents(relocs_shndx));
256
  Location got_plt_location(this->elf_file_.section_contents(got_plt_shndx));
257
  Location strtab_location(this->elf_file_.section_contents(strtab_shndx));
258
 
259
  View inputs_view = this->view(inputs_location);
260
  View symtab_view = this->view(symtab_location);
261
  View relocs_view = this->view(relocs_location);
262
  View got_plt_view = this->view(got_plt_location);
263
  View strtab_view = this->view(strtab_location);
264
 
265
  elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size);
266
 
267
  this->inputs_reader_ =
268
      Incremental_inputs_reader<size, big_endian>(inputs_view.data(), strtab);
269
  this->symtab_reader_ =
270
      Incremental_symtab_reader<big_endian>(symtab_view.data(),
271
                                            symtab_location.data_size);
272
  this->relocs_reader_ =
273
      Incremental_relocs_reader<size, big_endian>(relocs_view.data(),
274
                                                  relocs_location.data_size);
275
  this->got_plt_reader_ =
276
      Incremental_got_plt_reader<big_endian>(got_plt_view.data());
277
 
278
  // Find the main symbol table.
279
  unsigned int main_symtab_shndx =
280
      this->elf_file_.find_section_by_type(elfcpp::SHT_SYMTAB);
281
  gold_assert(main_symtab_shndx != elfcpp::SHN_UNDEF);
282
  this->main_symtab_loc_ = this->elf_file_.section_contents(main_symtab_shndx);
283
 
284
  // Find the main symbol string table.
285
  unsigned int main_strtab_shndx =
286
      this->elf_file_.section_link(main_symtab_shndx);
287
  gold_assert(main_strtab_shndx != elfcpp::SHN_UNDEF
288
              && main_strtab_shndx < this->elf_file_.shnum());
289
  this->main_strtab_loc_ = this->elf_file_.section_contents(main_strtab_shndx);
290
 
291
  // Walk the list of input files (a) to setup an Input_reader for each
292
  // input file, and (b) to record maps of files added from archive
293
  // libraries and scripts.
294
  Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
295
  unsigned int count = inputs.input_file_count();
296
  this->input_objects_.resize(count);
297
  this->input_entry_readers_.reserve(count);
298
  this->library_map_.resize(count);
299
  this->script_map_.resize(count);
300
  for (unsigned int i = 0; i < count; i++)
301
    {
302
      Input_entry_reader input_file = inputs.input_file(i);
303
      this->input_entry_readers_.push_back(Sized_input_reader(input_file));
304
      switch (input_file.type())
305
        {
306
        case INCREMENTAL_INPUT_OBJECT:
307
        case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
308
        case INCREMENTAL_INPUT_SHARED_LIBRARY:
309
          // No special treatment necessary.
310
          break;
311
        case INCREMENTAL_INPUT_ARCHIVE:
312
          {
313
            Incremental_library* lib =
314
                new Incremental_library(input_file.filename(), i,
315
                                        &this->input_entry_readers_[i]);
316
            this->library_map_[i] = lib;
317
            unsigned int member_count = input_file.get_member_count();
318
            for (unsigned int j = 0; j < member_count; j++)
319
              {
320
                int member_offset = input_file.get_member_offset(j);
321
                int member_index = inputs.input_file_index(member_offset);
322
                this->library_map_[member_index] = lib;
323
              }
324
          }
325
          break;
326
        case INCREMENTAL_INPUT_SCRIPT:
327
          {
328 159 khays
            Script_info* script = new Script_info(input_file.filename(), i);
329 27 khays
            this->script_map_[i] = script;
330
            unsigned int object_count = input_file.get_object_count();
331
            for (unsigned int j = 0; j < object_count; j++)
332
              {
333
                int object_offset = input_file.get_object_offset(j);
334
                int object_index = inputs.input_file_index(object_offset);
335
                this->script_map_[object_index] = script;
336
              }
337
          }
338
          break;
339
        default:
340
          gold_unreachable();
341
        }
342
    }
343
 
344
  // Initialize the map of global symbols.
345
  unsigned int nglobals = this->symtab_reader_.symbol_count();
346
  this->symbol_map_.resize(nglobals);
347
 
348
  this->has_incremental_info_ = true;
349
}
350
 
351
// Walk the list of input files given on the command line, and build
352
// a direct map of file index to the corresponding input argument.
353
 
354
void
355
check_input_args(std::vector<const Input_argument*>& input_args_map,
356
                 Input_arguments::const_iterator begin,
357
                 Input_arguments::const_iterator end)
358
{
359
  for (Input_arguments::const_iterator p = begin;
360
       p != end;
361
       ++p)
362
    {
363
      if (p->is_group())
364
        {
365
          const Input_file_group* group = p->group();
366
          check_input_args(input_args_map, group->begin(), group->end());
367
        }
368
      else if (p->is_lib())
369
        {
370
          const Input_file_lib* lib = p->lib();
371
          check_input_args(input_args_map, lib->begin(), lib->end());
372
        }
373
      else
374
        {
375
          gold_assert(p->is_file());
376
          unsigned int arg_serial = p->file().arg_serial();
377
          if (arg_serial > 0)
378
            {
379
              gold_assert(arg_serial <= input_args_map.size());
380
              gold_assert(input_args_map[arg_serial - 1] == 0);
381
              input_args_map[arg_serial - 1] = &*p;
382
            }
383
        }
384
    }
385
}
386
 
387
// Determine whether an incremental link based on the existing output file
388
// can be done.
389
 
390
template<int size, bool big_endian>
391
bool
392
Sized_incremental_binary<size, big_endian>::do_check_inputs(
393
    const Command_line& cmdline,
394
    Incremental_inputs* incremental_inputs)
395
{
396
  Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
397
 
398
  if (!this->has_incremental_info_)
399
    {
400
      explain_no_incremental(_("no incremental data from previous build"));
401
      return false;
402
    }
403
 
404
  if (inputs.version() != INCREMENTAL_LINK_VERSION)
405
    {
406
      explain_no_incremental(_("different version of incremental build data"));
407
      return false;
408
    }
409
 
410
  if (incremental_inputs->command_line() != inputs.command_line())
411
    {
412 159 khays
      gold_debug(DEBUG_INCREMENTAL,
413
                 "old command line: %s",
414
                 inputs.command_line());
415
      gold_debug(DEBUG_INCREMENTAL,
416
                 "new command line: %s",
417
                 incremental_inputs->command_line().c_str());
418 27 khays
      explain_no_incremental(_("command line changed"));
419
      return false;
420
    }
421
 
422
  // Walk the list of input files given on the command line, and build
423
  // a direct map of argument serial numbers to the corresponding input
424
  // arguments.
425
  this->input_args_map_.resize(cmdline.number_of_input_files());
426
  check_input_args(this->input_args_map_, cmdline.begin(), cmdline.end());
427
 
428
  // Walk the list of input files to check for conditions that prevent
429
  // an incremental update link.
430
  unsigned int count = inputs.input_file_count();
431
  for (unsigned int i = 0; i < count; i++)
432
    {
433
      Input_entry_reader input_file = inputs.input_file(i);
434
      switch (input_file.type())
435
        {
436
        case INCREMENTAL_INPUT_OBJECT:
437
        case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
438
        case INCREMENTAL_INPUT_SHARED_LIBRARY:
439
        case INCREMENTAL_INPUT_ARCHIVE:
440
          // No special treatment necessary.
441
          break;
442
        case INCREMENTAL_INPUT_SCRIPT:
443
          if (this->do_file_has_changed(i))
444
            {
445
              explain_no_incremental(_("%s: script file changed"),
446
                                     input_file.filename());
447
              return false;
448
            }
449
          break;
450
        default:
451
          gold_unreachable();
452
        }
453
    }
454
 
455
  return true;
456
}
457
 
458
// Return TRUE if input file N has changed since the last incremental link.
459
 
460
template<int size, bool big_endian>
461
bool
462
Sized_incremental_binary<size, big_endian>::do_file_has_changed(
463
    unsigned int n) const
464
{
465
  Input_entry_reader input_file = this->inputs_reader_.input_file(n);
466
  Incremental_disposition disp = INCREMENTAL_CHECK;
467 159 khays
 
468
  // For files named in scripts, find the file that was actually named
469
  // on the command line, so that we can get the incremental disposition
470
  // flag.
471
  Script_info* script = this->get_script_info(n);
472
  if (script != NULL)
473
    n = script->input_file_index();
474
 
475 27 khays
  const Input_argument* input_argument = this->get_input_argument(n);
476
  if (input_argument != NULL)
477
    disp = input_argument->file().options().incremental_disposition();
478
 
479 159 khays
  // For files at the beginning of the command line (i.e., those added
480
  // implicitly by gcc), check whether the --incremental-startup-unchanged
481
  // option was used.
482
  if (disp == INCREMENTAL_STARTUP)
483
    disp = parameters->options().incremental_startup_disposition();
484
 
485 27 khays
  if (disp != INCREMENTAL_CHECK)
486
    return disp == INCREMENTAL_CHANGED;
487
 
488
  const char* filename = input_file.filename();
489
  Timespec old_mtime = input_file.get_mtime();
490
  Timespec new_mtime;
491
  if (!get_mtime(filename, &new_mtime))
492
    {
493
      // If we can't open get the current modification time, assume it has
494
      // changed.  If the file doesn't exist, we'll issue an error when we
495
      // try to open it later.
496
      return true;
497
    }
498
 
499
  if (new_mtime.seconds > old_mtime.seconds)
500
    return true;
501
  if (new_mtime.seconds == old_mtime.seconds
502
      && new_mtime.nanoseconds > old_mtime.nanoseconds)
503
    return true;
504
  return false;
505
}
506
 
507
// Initialize the layout of the output file based on the existing
508
// output file.
509
 
510
template<int size, bool big_endian>
511
void
512
Sized_incremental_binary<size, big_endian>::do_init_layout(Layout* layout)
513
{
514
  typedef elfcpp::Shdr<size, big_endian> Shdr;
515
  const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
516
 
517
  // Get views of the section headers and the section string table.
518
  const off_t shoff = this->elf_file_.shoff();
519
  const unsigned int shnum = this->elf_file_.shnum();
520
  const unsigned int shstrndx = this->elf_file_.shstrndx();
521
  Location shdrs_location(shoff, shnum * shdr_size);
522
  Location shstrndx_location(this->elf_file_.section_contents(shstrndx));
523
  View shdrs_view = this->view(shdrs_location);
524
  View shstrndx_view = this->view(shstrndx_location);
525
  elfcpp::Elf_strtab shstrtab(shstrndx_view.data(),
526
                              shstrndx_location.data_size);
527
 
528
  layout->set_incremental_base(this);
529
 
530
  // Initialize the layout.
531
  this->section_map_.resize(shnum);
532
  const unsigned char* pshdr = shdrs_view.data() + shdr_size;
533
  for (unsigned int i = 1; i < shnum; i++)
534
    {
535
      Shdr shdr(pshdr);
536
      const char* name;
537
      if (!shstrtab.get_c_string(shdr.get_sh_name(), &name))
538
        name = NULL;
539
      gold_debug(DEBUG_INCREMENTAL,
540
                 "Output section: %2d %08lx %08lx %08lx %3d %s",
541
                 i,
542
                 static_cast<long>(shdr.get_sh_addr()),
543
                 static_cast<long>(shdr.get_sh_offset()),
544
                 static_cast<long>(shdr.get_sh_size()),
545
                 shdr.get_sh_type(), name ? name : "<null>");
546
      this->section_map_[i] = layout->init_fixed_output_section(name, shdr);
547
      pshdr += shdr_size;
548
    }
549
}
550
 
551
// Mark regions of the input file that must be kept unchanged.
552
 
553
template<int size, bool big_endian>
554
void
555
Sized_incremental_binary<size, big_endian>::do_reserve_layout(
556
    unsigned int input_file_index)
557
{
558 148 khays
  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
559
 
560 27 khays
  Input_entry_reader input_file =
561
      this->inputs_reader_.input_file(input_file_index);
562
 
563
  if (input_file.type() == INCREMENTAL_INPUT_SHARED_LIBRARY)
564 148 khays
    {
565
      // Reserve the BSS space used for COPY relocations.
566
      unsigned int nsyms = input_file.get_global_symbol_count();
567
      Incremental_binary::View symtab_view(NULL);
568
      unsigned int symtab_count;
569
      elfcpp::Elf_strtab strtab(NULL, 0);
570
      this->get_symtab_view(&symtab_view, &symtab_count, &strtab);
571
      for (unsigned int i = 0; i < nsyms; ++i)
572
        {
573
          bool is_def;
574
          bool is_copy;
575
          unsigned int output_symndx =
576
              input_file.get_output_symbol_index(i, &is_def, &is_copy);
577
          if (is_copy)
578
            {
579
              const unsigned char* sym_p = (symtab_view.data()
580
                                            + output_symndx * sym_size);
581
              elfcpp::Sym<size, big_endian> gsym(sym_p);
582
              unsigned int shndx = gsym.get_st_shndx();
583
              if (shndx < 1 || shndx >= this->section_map_.size())
584
                continue;
585
              Output_section* os = this->section_map_[shndx];
586
              off_t offset = gsym.get_st_value() - os->address();
587
              os->reserve(offset, gsym.get_st_size());
588
              gold_debug(DEBUG_INCREMENTAL,
589
                         "Reserve for COPY reloc: %s, off %d, size %d",
590
                         os->name(),
591
                         static_cast<int>(offset),
592
                         static_cast<int>(gsym.get_st_size()));
593
            }
594
        }
595
      return;
596
    }
597 27 khays
 
598
  unsigned int shnum = input_file.get_input_section_count();
599
  for (unsigned int i = 0; i < shnum; i++)
600
    {
601
      typename Input_entry_reader::Input_section_info sect =
602
          input_file.get_input_section(i);
603
      if (sect.output_shndx == 0 || sect.sh_offset == -1)
604
        continue;
605
      Output_section* os = this->section_map_[sect.output_shndx];
606
      gold_assert(os != NULL);
607
      os->reserve(sect.sh_offset, sect.sh_size);
608
    }
609
}
610
 
611
// Process the GOT and PLT entries from the existing output file.
612
 
613
template<int size, bool big_endian>
614
void
615
Sized_incremental_binary<size, big_endian>::do_process_got_plt(
616
    Symbol_table* symtab,
617
    Layout* layout)
618
{
619
  Incremental_got_plt_reader<big_endian> got_plt_reader(this->got_plt_reader());
620
  Sized_target<size, big_endian>* target =
621
      parameters->sized_target<size, big_endian>();
622
 
623
  // Get the number of symbols in the main symbol table and in the
624
  // incremental symbol table.  The difference between the two counts
625
  // is the index of the first forced-local or global symbol in the
626
  // main symbol table.
627
  unsigned int symtab_count =
628
      this->main_symtab_loc_.data_size / elfcpp::Elf_sizes<size>::sym_size;
629
  unsigned int isym_count = this->symtab_reader_.symbol_count();
630
  unsigned int first_global = symtab_count - isym_count;
631
 
632
  // Tell the target how big the GOT and PLT sections are.
633
  unsigned int got_count = got_plt_reader.get_got_entry_count();
634
  unsigned int plt_count = got_plt_reader.get_plt_entry_count();
635 166 khays
  Output_data_got_base* got =
636 27 khays
      target->init_got_plt_for_update(symtab, layout, got_count, plt_count);
637
 
638
  // Read the GOT entries from the base file and build the outgoing GOT.
639
  for (unsigned int i = 0; i < got_count; ++i)
640
    {
641
      unsigned int got_type = got_plt_reader.get_got_type(i);
642
      if ((got_type & 0x7f) == 0x7f)
643
        {
644
          // This is the second entry of a pair.
645
          got->reserve_slot(i);
646
          continue;
647
        }
648
      unsigned int symndx = got_plt_reader.get_got_symndx(i);
649
      if (got_type & 0x80)
650
        {
651
          // This is an entry for a local symbol.  Ignore this entry if
652
          // the object file was replaced.
653
          unsigned int input_index = got_plt_reader.get_got_input_index(i);
654
          gold_debug(DEBUG_INCREMENTAL,
655
                     "GOT entry %d, type %02x: (local symbol)",
656
                     i, got_type & 0x7f);
657
          Sized_relobj_incr<size, big_endian>* obj =
658
              this->input_object(input_index);
659
          if (obj != NULL)
660
            target->reserve_local_got_entry(i, obj, symndx, got_type & 0x7f);
661
        }
662
      else
663
        {
664
          // This is an entry for a global symbol.  GOT_DESC is the symbol
665
          // table index.
666
          // FIXME: This should really be a fatal error (corrupt input).
667
          gold_assert(symndx >= first_global && symndx < symtab_count);
668
          Symbol* sym = this->global_symbol(symndx - first_global);
669 148 khays
          // Add the GOT entry only if the symbol is still referenced.
670
          if (sym != NULL && sym->in_reg())
671
            {
672
              gold_debug(DEBUG_INCREMENTAL,
673
                         "GOT entry %d, type %02x: %s",
674
                         i, got_type, sym->name());
675
              target->reserve_global_got_entry(i, sym, got_type);
676
            }
677 27 khays
        }
678
    }
679
 
680
  // Read the PLT entries from the base file and pass each to the target.
681
  for (unsigned int i = 0; i < plt_count; ++i)
682
    {
683
      unsigned int plt_desc = got_plt_reader.get_plt_desc(i);
684
      // FIXME: This should really be a fatal error (corrupt input).
685
      gold_assert(plt_desc >= first_global && plt_desc < symtab_count);
686
      Symbol* sym = this->global_symbol(plt_desc - first_global);
687 148 khays
      // Add the PLT entry only if the symbol is still referenced.
688 163 khays
      if (sym != NULL && sym->in_reg())
689 148 khays
        {
690
          gold_debug(DEBUG_INCREMENTAL,
691
                     "PLT entry %d: %s",
692
                     i, sym->name());
693 159 khays
          target->register_global_plt_entry(symtab, layout, i, sym);
694 148 khays
        }
695 27 khays
    }
696
}
697
 
698 148 khays
// Emit COPY relocations from the existing output file.
699
 
700
template<int size, bool big_endian>
701
void
702
Sized_incremental_binary<size, big_endian>::do_emit_copy_relocs(
703
    Symbol_table* symtab)
704
{
705
  Sized_target<size, big_endian>* target =
706
      parameters->sized_target<size, big_endian>();
707
 
708
  for (typename Copy_relocs::iterator p = this->copy_relocs_.begin();
709
       p != this->copy_relocs_.end();
710
       ++p)
711
    {
712
      if (!(*p).symbol->is_copied_from_dynobj())
713
        target->emit_copy_reloc(symtab, (*p).symbol, (*p).output_section,
714
                                (*p).offset);
715
    }
716
}
717
 
718 27 khays
// Apply incremental relocations for symbols whose values have changed.
719
 
720
template<int size, bool big_endian>
721
void
722
Sized_incremental_binary<size, big_endian>::do_apply_incremental_relocs(
723
    const Symbol_table* symtab,
724
    Layout* layout,
725
    Output_file* of)
726
{
727
  typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
728
  typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addend;
729
  Incremental_symtab_reader<big_endian> isymtab(this->symtab_reader());
730
  Incremental_relocs_reader<size, big_endian> irelocs(this->relocs_reader());
731
  unsigned int nglobals = isymtab.symbol_count();
732
  const unsigned int incr_reloc_size = irelocs.reloc_size;
733
 
734
  Relocate_info<size, big_endian> relinfo;
735
  relinfo.symtab = symtab;
736
  relinfo.layout = layout;
737
  relinfo.object = NULL;
738
  relinfo.reloc_shndx = 0;
739
  relinfo.reloc_shdr = NULL;
740
  relinfo.data_shndx = 0;
741
  relinfo.data_shdr = NULL;
742
 
743
  Sized_target<size, big_endian>* target =
744
      parameters->sized_target<size, big_endian>();
745
 
746
  for (unsigned int i = 0; i < nglobals; i++)
747
    {
748
      const Symbol* gsym = this->global_symbol(i);
749
 
750
      // If the symbol is not referenced from any unchanged input files,
751
      // we do not need to reapply any of its relocations.
752
      if (gsym == NULL)
753
        continue;
754
 
755
      // If the symbol is defined in an unchanged file, we do not need to
756
      // reapply any of its relocations.
757
      if (gsym->source() == Symbol::FROM_OBJECT
758
          && gsym->object()->is_incremental())
759
        continue;
760
 
761
      gold_debug(DEBUG_INCREMENTAL,
762
                 "Applying incremental relocations for global symbol %s [%d]",
763
                 gsym->name(), i);
764
 
765
      // Follow the linked list of input symbol table entries for this symbol.
766
      // We don't bother to figure out whether the symbol table entry belongs
767
      // to a changed or unchanged file because it's easier just to apply all
768
      // the relocations -- although we might scribble over an area that has
769
      // been reallocated, we do this before copying any new data into the
770
      // output file.
771
      unsigned int offset = isymtab.get_list_head(i);
772
      while (offset > 0)
773
        {
774
          Incremental_global_symbol_reader<big_endian> sym_info =
775
              this->inputs_reader().global_symbol_reader_at_offset(offset);
776
          unsigned int r_base = sym_info.reloc_offset();
777
          unsigned int r_count = sym_info.reloc_count();
778
 
779
          // Apply each relocation for this symbol table entry.
780
          for (unsigned int j = 0; j < r_count;
781
               ++j, r_base += incr_reloc_size)
782
            {
783
              unsigned int r_type = irelocs.get_r_type(r_base);
784
              unsigned int r_shndx = irelocs.get_r_shndx(r_base);
785
              Address r_offset = irelocs.get_r_offset(r_base);
786
              Addend r_addend = irelocs.get_r_addend(r_base);
787
              Output_section* os = this->output_section(r_shndx);
788
              Address address = os->address();
789
              off_t section_offset = os->offset();
790
              size_t view_size = os->data_size();
791
              unsigned char* const view = of->get_output_view(section_offset,
792
                                                              view_size);
793
 
794
              gold_debug(DEBUG_INCREMENTAL,
795
                         "  %08lx: %s + %d: type %d addend %ld",
796
                         (long)(section_offset + r_offset),
797
                         os->name(),
798
                         (int)r_offset,
799
                         r_type,
800
                         (long)r_addend);
801
 
802
              target->apply_relocation(&relinfo, r_offset, r_type, r_addend,
803
                                       gsym, view, address, view_size);
804
 
805
              // FIXME: Do something more efficient if write_output_view
806
              // ever becomes more than a no-op.
807
              of->write_output_view(section_offset, view_size, view);
808
            }
809
          offset = sym_info.next_offset();
810
        }
811
    }
812
}
813
 
814
// Get a view of the main symbol table and the symbol string table.
815
 
816
template<int size, bool big_endian>
817
void
818
Sized_incremental_binary<size, big_endian>::get_symtab_view(
819
    View* symtab_view,
820
    unsigned int* nsyms,
821
    elfcpp::Elf_strtab* strtab)
822
{
823
  *symtab_view = this->view(this->main_symtab_loc_);
824
  *nsyms = this->main_symtab_loc_.data_size / elfcpp::Elf_sizes<size>::sym_size;
825
 
826
  View strtab_view(this->view(this->main_strtab_loc_));
827
  *strtab = elfcpp::Elf_strtab(strtab_view.data(),
828
                               this->main_strtab_loc_.data_size);
829
}
830
 
831
namespace
832
{
833
 
834
// Create a Sized_incremental_binary object of the specified size and
835
// endianness. Fails if the target architecture is not supported.
836
 
837
template<int size, bool big_endian>
838
Incremental_binary*
839
make_sized_incremental_binary(Output_file* file,
840
                              const elfcpp::Ehdr<size, big_endian>& ehdr)
841
{
842
  Target* target = select_target(ehdr.get_e_machine(), size, big_endian,
843
                                 ehdr.get_e_ident()[elfcpp::EI_OSABI],
844
                                 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
845
  if (target == NULL)
846
    {
847
      explain_no_incremental(_("unsupported ELF machine number %d"),
848
               ehdr.get_e_machine());
849
      return NULL;
850
    }
851
 
852
  if (!parameters->target_valid())
853
    set_parameters_target(target);
854
  else if (target != &parameters->target())
855
    gold_error(_("%s: incompatible target"), file->filename());
856
 
857
  return new Sized_incremental_binary<size, big_endian>(file, ehdr, target);
858
}
859
 
860
}  // End of anonymous namespace.
861
 
862
// Create an Incremental_binary object for FILE.  Returns NULL is this is not
863
// possible, e.g. FILE is not an ELF file or has an unsupported target.  FILE
864
// should be opened.
865
 
866
Incremental_binary*
867
open_incremental_binary(Output_file* file)
868
{
869
  off_t filesize = file->filesize();
870
  int want = elfcpp::Elf_recognizer::max_header_size;
871
  if (filesize < want)
872
    want = filesize;
873
 
874
  const unsigned char* p = file->get_input_view(0, want);
875
  if (!elfcpp::Elf_recognizer::is_elf_file(p, want))
876
    {
877
      explain_no_incremental(_("output is not an ELF file."));
878
      return NULL;
879
    }
880
 
881
  int size = 0;
882
  bool big_endian = false;
883
  std::string error;
884
  if (!elfcpp::Elf_recognizer::is_valid_header(p, want, &size, &big_endian,
885
                                               &error))
886
    {
887
      explain_no_incremental(error.c_str());
888
      return NULL;
889
    }
890
 
891
  Incremental_binary* result = NULL;
892
  if (size == 32)
893
    {
894
      if (big_endian)
895
        {
896
#ifdef HAVE_TARGET_32_BIG
897
          result = make_sized_incremental_binary<32, true>(
898
              file, elfcpp::Ehdr<32, true>(p));
899
#else
900
          explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
901
#endif
902
        }
903
      else
904
        {
905
#ifdef HAVE_TARGET_32_LITTLE
906
          result = make_sized_incremental_binary<32, false>(
907
              file, elfcpp::Ehdr<32, false>(p));
908
#else
909
          explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
910
#endif
911
        }
912
    }
913
  else if (size == 64)
914
    {
915
      if (big_endian)
916
        {
917
#ifdef HAVE_TARGET_64_BIG
918
          result = make_sized_incremental_binary<64, true>(
919
              file, elfcpp::Ehdr<64, true>(p));
920
#else
921
          explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
922
#endif
923
        }
924
      else
925
        {
926
#ifdef HAVE_TARGET_64_LITTLE
927
          result = make_sized_incremental_binary<64, false>(
928
              file, elfcpp::Ehdr<64, false>(p));
929
#else
930
          explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
931
#endif
932
        }
933
    }
934
  else
935
    gold_unreachable();
936
 
937
  return result;
938
}
939
 
940
// Class Incremental_inputs.
941
 
942
// Add the command line to the string table, setting
943
// command_line_key_.  In incremental builds, the command line is
944
// stored in .gnu_incremental_inputs so that the next linker run can
945
// check if the command line options didn't change.
946
 
947
void
948
Incremental_inputs::report_command_line(int argc, const char* const* argv)
949
{
950
  // Always store 'gold' as argv[0] to avoid a full relink if the user used a
951
  // different path to the linker.
952
  std::string args("gold");
953
  // Copied from collect_argv in main.cc.
954
  for (int i = 1; i < argc; ++i)
955
    {
956
      // Adding/removing these options should not result in a full relink.
957
      if (strcmp(argv[i], "--incremental") == 0
958
          || strcmp(argv[i], "--incremental-full") == 0
959
          || strcmp(argv[i], "--incremental-update") == 0
960
          || strcmp(argv[i], "--incremental-changed") == 0
961
          || strcmp(argv[i], "--incremental-unchanged") == 0
962
          || strcmp(argv[i], "--incremental-unknown") == 0
963 159 khays
          || strcmp(argv[i], "--incremental-startup-unchanged") == 0
964 27 khays
          || is_prefix_of("--incremental-base=", argv[i])
965 159 khays
          || is_prefix_of("--incremental-patch=", argv[i])
966 27 khays
          || is_prefix_of("--debug=", argv[i]))
967
        continue;
968
      if (strcmp(argv[i], "--incremental-base") == 0
969 159 khays
          || strcmp(argv[i], "--incremental-patch") == 0
970 27 khays
          || strcmp(argv[i], "--debug") == 0)
971
        {
972
          // When these options are used without the '=', skip the
973
          // following parameter as well.
974
          ++i;
975
          continue;
976
        }
977
 
978
      args.append(" '");
979
      // Now append argv[i], but with all single-quotes escaped
980
      const char* argpos = argv[i];
981
      while (1)
982
        {
983
          const int len = strcspn(argpos, "'");
984
          args.append(argpos, len);
985
          if (argpos[len] == '\0')
986
            break;
987
          args.append("'\"'\"'");
988
          argpos += len + 1;
989
        }
990
      args.append("'");
991
    }
992
 
993
  this->command_line_ = args;
994
  this->strtab_->add(this->command_line_.c_str(), false,
995
                     &this->command_line_key_);
996
}
997
 
998
// Record the input archive file ARCHIVE.  This is called by the
999
// Add_archive_symbols task before determining which archive members
1000
// to include.  We create the Incremental_archive_entry here and
1001
// attach it to the Archive, but we do not add it to the list of
1002
// input objects until report_archive_end is called.
1003
 
1004
void
1005
Incremental_inputs::report_archive_begin(Library_base* arch,
1006
                                         unsigned int arg_serial,
1007
                                         Script_info* script_info)
1008
{
1009
  Stringpool::Key filename_key;
1010
  Timespec mtime = arch->get_mtime();
1011
 
1012
  // For a file loaded from a script, don't record its argument serial number.
1013
  if (script_info != NULL)
1014
    arg_serial = 0;
1015
 
1016
  this->strtab_->add(arch->filename().c_str(), false, &filename_key);
1017
  Incremental_archive_entry* entry =
1018
      new Incremental_archive_entry(filename_key, arg_serial, mtime);
1019
  arch->set_incremental_info(entry);
1020
 
1021
  if (script_info != NULL)
1022
    {
1023
      Incremental_script_entry* script_entry = script_info->incremental_info();
1024
      gold_assert(script_entry != NULL);
1025
      script_entry->add_object(entry);
1026
    }
1027
}
1028
 
1029
// Visitor class for processing the unused global symbols in a library.
1030
// An instance of this class is passed to the library's
1031
// for_all_unused_symbols() iterator, which will call the visit()
1032
// function for each global symbol defined in each unused library
1033
// member.  We add those symbol names to the incremental info for the
1034
// library.
1035
 
1036
class Unused_symbol_visitor : public Library_base::Symbol_visitor_base
1037
{
1038
 public:
1039
  Unused_symbol_visitor(Incremental_archive_entry* entry, Stringpool* strtab)
1040
    : entry_(entry), strtab_(strtab)
1041
  { }
1042
 
1043
  void
1044
  visit(const char* sym)
1045
  {
1046
    Stringpool::Key symbol_key;
1047
    this->strtab_->add(sym, true, &symbol_key);
1048
    this->entry_->add_unused_global_symbol(symbol_key);
1049
  }
1050
 
1051
 private:
1052
  Incremental_archive_entry* entry_;
1053
  Stringpool* strtab_;
1054
};
1055
 
1056
// Finish recording the input archive file ARCHIVE.  This is called by the
1057
// Add_archive_symbols task after determining which archive members
1058
// to include.
1059
 
1060
void
1061
Incremental_inputs::report_archive_end(Library_base* arch)
1062
{
1063
  Incremental_archive_entry* entry = arch->incremental_info();
1064
 
1065
  gold_assert(entry != NULL);
1066
  this->inputs_.push_back(entry);
1067
 
1068
  // Collect unused global symbols.
1069
  Unused_symbol_visitor v(entry, this->strtab_);
1070
  arch->for_all_unused_symbols(&v);
1071
}
1072
 
1073
// Record the input object file OBJ.  If ARCH is not NULL, attach
1074
// the object file to the archive.  This is called by the
1075
// Add_symbols task after finding out the type of the file.
1076
 
1077
void
1078
Incremental_inputs::report_object(Object* obj, unsigned int arg_serial,
1079
                                  Library_base* arch, Script_info* script_info)
1080
{
1081
  Stringpool::Key filename_key;
1082
  Timespec mtime = obj->get_mtime();
1083
 
1084
  // For a file loaded from a script, don't record its argument serial number.
1085
  if (script_info != NULL)
1086
    arg_serial = 0;
1087
 
1088
  this->strtab_->add(obj->name().c_str(), false, &filename_key);
1089
 
1090
  Incremental_input_entry* input_entry;
1091
 
1092
  this->current_object_ = obj;
1093
 
1094
  if (!obj->is_dynamic())
1095
    {
1096
      this->current_object_entry_ =
1097
          new Incremental_object_entry(filename_key, obj, arg_serial, mtime);
1098
      input_entry = this->current_object_entry_;
1099
      if (arch != NULL)
1100
        {
1101
          Incremental_archive_entry* arch_entry = arch->incremental_info();
1102
          gold_assert(arch_entry != NULL);
1103
          arch_entry->add_object(this->current_object_entry_);
1104
        }
1105
    }
1106
  else
1107
    {
1108
      this->current_object_entry_ = NULL;
1109
      Stringpool::Key soname_key;
1110
      Dynobj* dynobj = obj->dynobj();
1111
      gold_assert(dynobj != NULL);
1112
      this->strtab_->add(dynobj->soname(), false, &soname_key);
1113
      input_entry = new Incremental_dynobj_entry(filename_key, soname_key, obj,
1114
                                                 arg_serial, mtime);
1115
    }
1116
 
1117
  if (obj->is_in_system_directory())
1118
    input_entry->set_is_in_system_directory();
1119
 
1120
  if (obj->as_needed())
1121
    input_entry->set_as_needed();
1122
 
1123
  this->inputs_.push_back(input_entry);
1124
 
1125
  if (script_info != NULL)
1126
    {
1127
      Incremental_script_entry* script_entry = script_info->incremental_info();
1128
      gold_assert(script_entry != NULL);
1129
      script_entry->add_object(input_entry);
1130
    }
1131
}
1132
 
1133
// Record an input section SHNDX from object file OBJ.
1134
 
1135
void
1136
Incremental_inputs::report_input_section(Object* obj, unsigned int shndx,
1137
                                         const char* name, off_t sh_size)
1138
{
1139
  Stringpool::Key key = 0;
1140
 
1141
  if (name != NULL)
1142
    this->strtab_->add(name, true, &key);
1143
 
1144
  gold_assert(obj == this->current_object_);
1145
  gold_assert(this->current_object_entry_ != NULL);
1146
  this->current_object_entry_->add_input_section(shndx, key, sh_size);
1147
}
1148
 
1149
// Record a kept COMDAT group belonging to object file OBJ.
1150
 
1151
void
1152
Incremental_inputs::report_comdat_group(Object* obj, const char* name)
1153
{
1154
  Stringpool::Key key = 0;
1155
 
1156
  if (name != NULL)
1157
    this->strtab_->add(name, true, &key);
1158
  gold_assert(obj == this->current_object_);
1159
  gold_assert(this->current_object_entry_ != NULL);
1160
  this->current_object_entry_->add_comdat_group(key);
1161
}
1162
 
1163
// Record that the input argument INPUT is a script SCRIPT.  This is
1164
// called by read_script after parsing the script and reading the list
1165
// of inputs added by this script.
1166
 
1167
void
1168
Incremental_inputs::report_script(Script_info* script,
1169
                                  unsigned int arg_serial,
1170
                                  Timespec mtime)
1171
{
1172
  Stringpool::Key filename_key;
1173
 
1174
  this->strtab_->add(script->filename().c_str(), false, &filename_key);
1175
  Incremental_script_entry* entry =
1176
      new Incremental_script_entry(filename_key, arg_serial, script, mtime);
1177
  this->inputs_.push_back(entry);
1178
  script->set_incremental_info(entry);
1179
}
1180
 
1181
// Finalize the incremental link information.  Called from
1182
// Layout::finalize.
1183
 
1184
void
1185
Incremental_inputs::finalize()
1186
{
1187
  // Finalize the string table.
1188
  this->strtab_->set_string_offsets();
1189
}
1190
 
1191
// Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
1192
 
1193
void
1194
Incremental_inputs::create_data_sections(Symbol_table* symtab)
1195
{
1196
  switch (parameters->size_and_endianness())
1197
    {
1198
#ifdef HAVE_TARGET_32_LITTLE
1199
    case Parameters::TARGET_32_LITTLE:
1200
      this->inputs_section_ =
1201
          new Output_section_incremental_inputs<32, false>(this, symtab);
1202
      break;
1203
#endif
1204
#ifdef HAVE_TARGET_32_BIG
1205
    case Parameters::TARGET_32_BIG:
1206
      this->inputs_section_ =
1207
          new Output_section_incremental_inputs<32, true>(this, symtab);
1208
      break;
1209
#endif
1210
#ifdef HAVE_TARGET_64_LITTLE
1211
    case Parameters::TARGET_64_LITTLE:
1212
      this->inputs_section_ =
1213
          new Output_section_incremental_inputs<64, false>(this, symtab);
1214
      break;
1215
#endif
1216
#ifdef HAVE_TARGET_64_BIG
1217
    case Parameters::TARGET_64_BIG:
1218
      this->inputs_section_ =
1219
          new Output_section_incremental_inputs<64, true>(this, symtab);
1220
      break;
1221
#endif
1222
    default:
1223
      gold_unreachable();
1224
    }
1225
  this->symtab_section_ = new Output_data_space(4, "** incremental_symtab");
1226
  this->relocs_section_ = new Output_data_space(4, "** incremental_relocs");
1227
  this->got_plt_section_ = new Output_data_space(4, "** incremental_got_plt");
1228
}
1229
 
1230
// Return the sh_entsize value for the .gnu_incremental_relocs section.
1231
unsigned int
1232
Incremental_inputs::relocs_entsize() const
1233
{
1234
  return 8 + 2 * parameters->target().get_size() / 8;
1235
}
1236
 
1237
// Class Output_section_incremental_inputs.
1238
 
1239
// Finalize the offsets for each input section and supplemental info block,
1240
// and set the final data size of the incremental output sections.
1241
 
1242
template<int size, bool big_endian>
1243
void
1244
Output_section_incremental_inputs<size, big_endian>::set_final_data_size()
1245
{
1246
  const Incremental_inputs* inputs = this->inputs_;
1247
  const unsigned int sizeof_addr = size / 8;
1248
  const unsigned int rel_size = 8 + 2 * sizeof_addr;
1249
 
1250
  // Offset of each input entry.
1251
  unsigned int input_offset = this->header_size;
1252
 
1253
  // Offset of each supplemental info block.
1254
  unsigned int file_index = 0;
1255
  unsigned int info_offset = this->header_size;
1256
  info_offset += this->input_entry_size * inputs->input_file_count();
1257
 
1258
  // Count each input file and its supplemental information block.
1259
  for (Incremental_inputs::Input_list::const_iterator p =
1260
           inputs->input_files().begin();
1261
       p != inputs->input_files().end();
1262
       ++p)
1263
    {
1264
      // Set the index and offset of the input file entry.
1265
      (*p)->set_offset(file_index, input_offset);
1266
      ++file_index;
1267
      input_offset += this->input_entry_size;
1268
 
1269
      // Set the offset of the supplemental info block.
1270
      switch ((*p)->type())
1271
        {
1272
        case INCREMENTAL_INPUT_SCRIPT:
1273
          {
1274
            Incremental_script_entry *entry = (*p)->script_entry();
1275
            gold_assert(entry != NULL);
1276
            (*p)->set_info_offset(info_offset);
1277
            // Object count.
1278
            info_offset += 4;
1279
            // Each member.
1280
            info_offset += (entry->get_object_count() * 4);
1281
          }
1282
          break;
1283
        case INCREMENTAL_INPUT_OBJECT:
1284
        case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
1285
          {
1286
            Incremental_object_entry* entry = (*p)->object_entry();
1287
            gold_assert(entry != NULL);
1288
            (*p)->set_info_offset(info_offset);
1289
            // Input section count, global symbol count, local symbol offset,
1290
            // local symbol count, first dynamic reloc, dynamic reloc count,
1291
            // comdat group count.
1292
            info_offset += 28;
1293
            // Each input section.
1294
            info_offset += (entry->get_input_section_count()
1295
                            * (8 + 2 * sizeof_addr));
1296
            // Each global symbol.
1297
            const Object::Symbols* syms = entry->object()->get_global_symbols();
1298
            info_offset += syms->size() * 20;
1299
            // Each comdat group.
1300
            info_offset += entry->get_comdat_group_count() * 4;
1301
          }
1302
          break;
1303
        case INCREMENTAL_INPUT_SHARED_LIBRARY:
1304
          {
1305
            Incremental_dynobj_entry* entry = (*p)->dynobj_entry();
1306
            gold_assert(entry != NULL);
1307
            (*p)->set_info_offset(info_offset);
1308
            // Global symbol count, soname index.
1309
            info_offset += 8;
1310
            // Each global symbol.
1311
            const Object::Symbols* syms = entry->object()->get_global_symbols();
1312
            gold_assert(syms != NULL);
1313
            unsigned int nsyms = syms->size();
1314
            unsigned int nsyms_out = 0;
1315
            for (unsigned int i = 0; i < nsyms; ++i)
1316
              {
1317
                const Symbol* sym = (*syms)[i];
1318
                if (sym == NULL)
1319
                  continue;
1320
                if (sym->is_forwarder())
1321
                  sym = this->symtab_->resolve_forwards(sym);
1322
                if (sym->symtab_index() != -1U)
1323
                  ++nsyms_out;
1324
              }
1325
            info_offset += nsyms_out * 4;
1326
          }
1327
          break;
1328
        case INCREMENTAL_INPUT_ARCHIVE:
1329
          {
1330
            Incremental_archive_entry* entry = (*p)->archive_entry();
1331
            gold_assert(entry != NULL);
1332
            (*p)->set_info_offset(info_offset);
1333
            // Member count + unused global symbol count.
1334
            info_offset += 8;
1335
            // Each member.
1336
            info_offset += (entry->get_member_count() * 4);
1337
            // Each global symbol.
1338
            info_offset += (entry->get_unused_global_symbol_count() * 4);
1339
          }
1340
          break;
1341
        default:
1342
          gold_unreachable();
1343
        }
1344
    }
1345
 
1346
  this->set_data_size(info_offset);
1347
 
1348
  // Set the size of the .gnu_incremental_symtab section.
1349
  inputs->symtab_section()->set_current_data_size(this->symtab_->output_count()
1350
                                                  * sizeof(unsigned int));
1351
 
1352
  // Set the size of the .gnu_incremental_relocs section.
1353
  inputs->relocs_section()->set_current_data_size(inputs->get_reloc_count()
1354
                                                  * rel_size);
1355
 
1356
  // Set the size of the .gnu_incremental_got_plt section.
1357
  Sized_target<size, big_endian>* target =
1358
    parameters->sized_target<size, big_endian>();
1359
  unsigned int got_count = target->got_entry_count();
1360
  unsigned int plt_count = target->plt_entry_count();
1361
  unsigned int got_plt_size = 8;  // GOT entry count, PLT entry count.
1362
  got_plt_size = (got_plt_size + got_count + 3) & ~3;  // GOT type array.
1363
  got_plt_size += got_count * 8 + plt_count * 4;  // GOT array, PLT array.
1364
  inputs->got_plt_section()->set_current_data_size(got_plt_size);
1365
}
1366
 
1367
// Write the contents of the .gnu_incremental_inputs and
1368
// .gnu_incremental_symtab sections.
1369
 
1370
template<int size, bool big_endian>
1371
void
1372
Output_section_incremental_inputs<size, big_endian>::do_write(Output_file* of)
1373
{
1374
  const Incremental_inputs* inputs = this->inputs_;
1375
  Stringpool* strtab = inputs->get_stringpool();
1376
 
1377
  // Get a view into the .gnu_incremental_inputs section.
1378
  const off_t off = this->offset();
1379
  const off_t oview_size = this->data_size();
1380
  unsigned char* const oview = of->get_output_view(off, oview_size);
1381
  unsigned char* pov = oview;
1382
 
1383
  // Get a view into the .gnu_incremental_symtab section.
1384
  const off_t symtab_off = inputs->symtab_section()->offset();
1385
  const off_t symtab_size = inputs->symtab_section()->data_size();
1386
  unsigned char* const symtab_view = of->get_output_view(symtab_off,
1387
                                                         symtab_size);
1388
 
1389
  // Allocate an array of linked list heads for the .gnu_incremental_symtab
1390
  // section.  Each element corresponds to a global symbol in the output
1391
  // symbol table, and points to the head of the linked list that threads
1392
  // through the object file input entries.  The value of each element
1393
  // is the section-relative offset to a global symbol entry in a
1394
  // supplemental information block.
1395
  unsigned int global_sym_count = this->symtab_->output_count();
1396
  unsigned int* global_syms = new unsigned int[global_sym_count];
1397
  memset(global_syms, 0, global_sym_count * sizeof(unsigned int));
1398
 
1399
  // Write the section header.
1400
  Stringpool::Key command_line_key = inputs->command_line_key();
1401
  pov = this->write_header(pov, inputs->input_file_count(),
1402
                           strtab->get_offset_from_key(command_line_key));
1403
 
1404
  // Write the list of input files.
1405
  pov = this->write_input_files(oview, pov, strtab);
1406
 
1407
  // Write the supplemental information blocks for each input file.
1408
  pov = this->write_info_blocks(oview, pov, strtab, global_syms,
1409
                                global_sym_count);
1410
 
1411
  gold_assert(pov - oview == oview_size);
1412
 
1413
  // Write the .gnu_incremental_symtab section.
1414
  gold_assert(global_sym_count * 4 == symtab_size);
1415
  this->write_symtab(symtab_view, global_syms, global_sym_count);
1416
 
1417
  delete[] global_syms;
1418
 
1419
  // Write the .gnu_incremental_got_plt section.
1420
  const off_t got_plt_off = inputs->got_plt_section()->offset();
1421
  const off_t got_plt_size = inputs->got_plt_section()->data_size();
1422
  unsigned char* const got_plt_view = of->get_output_view(got_plt_off,
1423
                                                          got_plt_size);
1424
  this->write_got_plt(got_plt_view, got_plt_size);
1425
 
1426
  of->write_output_view(off, oview_size, oview);
1427
  of->write_output_view(symtab_off, symtab_size, symtab_view);
1428
  of->write_output_view(got_plt_off, got_plt_size, got_plt_view);
1429
}
1430
 
1431
// Write the section header: version, input file count, offset of command line
1432
// in the string table, and 4 bytes of padding.
1433
 
1434
template<int size, bool big_endian>
1435
unsigned char*
1436
Output_section_incremental_inputs<size, big_endian>::write_header(
1437
    unsigned char* pov,
1438
    unsigned int input_file_count,
1439
    section_offset_type command_line_offset)
1440
{
1441
  Swap32::writeval(pov, INCREMENTAL_LINK_VERSION);
1442
  Swap32::writeval(pov + 4, input_file_count);
1443
  Swap32::writeval(pov + 8, command_line_offset);
1444
  Swap32::writeval(pov + 12, 0);
1445
  return pov + this->header_size;
1446
}
1447
 
1448
// Write the input file entries.
1449
 
1450
template<int size, bool big_endian>
1451
unsigned char*
1452
Output_section_incremental_inputs<size, big_endian>::write_input_files(
1453
    unsigned char* oview,
1454
    unsigned char* pov,
1455
    Stringpool* strtab)
1456
{
1457
  const Incremental_inputs* inputs = this->inputs_;
1458
 
1459
  for (Incremental_inputs::Input_list::const_iterator p =
1460
           inputs->input_files().begin();
1461
       p != inputs->input_files().end();
1462
       ++p)
1463
    {
1464
      gold_assert(static_cast<unsigned int>(pov - oview) == (*p)->get_offset());
1465
      section_offset_type filename_offset =
1466
          strtab->get_offset_from_key((*p)->get_filename_key());
1467
      const Timespec& mtime = (*p)->get_mtime();
1468
      unsigned int flags = (*p)->type();
1469
      if ((*p)->is_in_system_directory())
1470
        flags |= INCREMENTAL_INPUT_IN_SYSTEM_DIR;
1471
      if ((*p)->as_needed())
1472
        flags |= INCREMENTAL_INPUT_AS_NEEDED;
1473
      Swap32::writeval(pov, filename_offset);
1474
      Swap32::writeval(pov + 4, (*p)->get_info_offset());
1475
      Swap64::writeval(pov + 8, mtime.seconds);
1476
      Swap32::writeval(pov + 16, mtime.nanoseconds);
1477
      Swap16::writeval(pov + 20, flags);
1478
      Swap16::writeval(pov + 22, (*p)->arg_serial());
1479
      pov += this->input_entry_size;
1480
    }
1481
  return pov;
1482
}
1483
 
1484
// Write the supplemental information blocks.
1485
 
1486
template<int size, bool big_endian>
1487
unsigned char*
1488
Output_section_incremental_inputs<size, big_endian>::write_info_blocks(
1489
    unsigned char* oview,
1490
    unsigned char* pov,
1491
    Stringpool* strtab,
1492
    unsigned int* global_syms,
1493
    unsigned int global_sym_count)
1494
{
1495
  const Incremental_inputs* inputs = this->inputs_;
1496
  unsigned int first_global_index = this->symtab_->first_global_index();
1497
 
1498
  for (Incremental_inputs::Input_list::const_iterator p =
1499
           inputs->input_files().begin();
1500
       p != inputs->input_files().end();
1501
       ++p)
1502
    {
1503
      switch ((*p)->type())
1504
        {
1505
        case INCREMENTAL_INPUT_SCRIPT:
1506
          {
1507
            gold_assert(static_cast<unsigned int>(pov - oview)
1508
                        == (*p)->get_info_offset());
1509
            Incremental_script_entry* entry = (*p)->script_entry();
1510
            gold_assert(entry != NULL);
1511
 
1512
            // Write the object count.
1513
            unsigned int nobjects = entry->get_object_count();
1514
            Swap32::writeval(pov, nobjects);
1515
            pov += 4;
1516
 
1517
            // For each object, write the offset to its input file entry.
1518
            for (unsigned int i = 0; i < nobjects; ++i)
1519
              {
1520
                Incremental_input_entry* obj = entry->get_object(i);
1521
                Swap32::writeval(pov, obj->get_offset());
1522
                pov += 4;
1523
              }
1524
          }
1525
          break;
1526
 
1527
        case INCREMENTAL_INPUT_OBJECT:
1528
        case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
1529
          {
1530
            gold_assert(static_cast<unsigned int>(pov - oview)
1531
                        == (*p)->get_info_offset());
1532
            Incremental_object_entry* entry = (*p)->object_entry();
1533
            gold_assert(entry != NULL);
1534
            const Object* obj = entry->object();
1535
            const Relobj* relobj = static_cast<const Relobj*>(obj);
1536
            const Object::Symbols* syms = obj->get_global_symbols();
1537
            // Write the input section count and global symbol count.
1538
            unsigned int nsections = entry->get_input_section_count();
1539
            unsigned int nsyms = syms->size();
1540
            off_t locals_offset = relobj->local_symbol_offset();
1541
            unsigned int nlocals = relobj->output_local_symbol_count();
1542
            unsigned int first_dynrel = relobj->first_dyn_reloc();
1543
            unsigned int ndynrel = relobj->dyn_reloc_count();
1544
            unsigned int ncomdat = entry->get_comdat_group_count();
1545
            Swap32::writeval(pov, nsections);
1546
            Swap32::writeval(pov + 4, nsyms);
1547
            Swap32::writeval(pov + 8, static_cast<unsigned int>(locals_offset));
1548
            Swap32::writeval(pov + 12, nlocals);
1549
            Swap32::writeval(pov + 16, first_dynrel);
1550
            Swap32::writeval(pov + 20, ndynrel);
1551
            Swap32::writeval(pov + 24, ncomdat);
1552
            pov += 28;
1553
 
1554
            // Build a temporary array to map input section indexes
1555
            // from the original object file index to the index in the
1556
            // incremental info table.
1557
            unsigned int* index_map = new unsigned int[obj->shnum()];
1558
            memset(index_map, 0, obj->shnum() * sizeof(unsigned int));
1559
 
1560
            // For each input section, write the name, output section index,
1561
            // offset within output section, and input section size.
1562
            for (unsigned int i = 0; i < nsections; i++)
1563
              {
1564
                unsigned int shndx = entry->get_input_section_index(i);
1565
                index_map[shndx] = i + 1;
1566
                Stringpool::Key key = entry->get_input_section_name_key(i);
1567
                off_t name_offset = 0;
1568
                if (key != 0)
1569
                  name_offset = strtab->get_offset_from_key(key);
1570
                int out_shndx = 0;
1571
                off_t out_offset = 0;
1572
                off_t sh_size = 0;
1573
                Output_section* os = obj->output_section(shndx);
1574
                if (os != NULL)
1575
                  {
1576
                    out_shndx = os->out_shndx();
1577
                    out_offset = obj->output_section_offset(shndx);
1578
                    sh_size = entry->get_input_section_size(i);
1579
                  }
1580
                Swap32::writeval(pov, name_offset);
1581
                Swap32::writeval(pov + 4, out_shndx);
1582
                Swap::writeval(pov + 8, out_offset);
1583
                Swap::writeval(pov + 8 + sizeof_addr, sh_size);
1584
                pov += 8 + 2 * sizeof_addr;
1585
              }
1586
 
1587
            // For each global symbol, write its associated relocations,
1588
            // add it to the linked list of globals, then write the
1589
            // supplemental information:  global symbol table index,
1590
            // input section index, linked list chain pointer, relocation
1591
            // count, and offset to the relocations.
1592
            for (unsigned int i = 0; i < nsyms; i++)
1593
              {
1594
                const Symbol* sym = (*syms)[i];
1595
                if (sym->is_forwarder())
1596
                  sym = this->symtab_->resolve_forwards(sym);
1597
                unsigned int shndx = 0;
1598 148 khays
                if (sym->source() != Symbol::FROM_OBJECT)
1599 27 khays
                  {
1600 148 khays
                    // The symbol was defined by the linker (e.g., common).
1601
                    // We mark these symbols with a special SHNDX of -1,
1602
                    // but exclude linker-predefined symbols and symbols
1603
                    // copied from shared objects.
1604
                    if (!sym->is_predefined()
1605
                        && !sym->is_copied_from_dynobj())
1606
                      shndx = -1U;
1607
                  }
1608
                else if (sym->object() == obj && sym->is_defined())
1609
                  {
1610 27 khays
                    bool is_ordinary;
1611
                    unsigned int orig_shndx = sym->shndx(&is_ordinary);
1612
                    if (is_ordinary)
1613
                      shndx = index_map[orig_shndx];
1614 148 khays
                    else
1615
                      shndx = 1;
1616 27 khays
                  }
1617
                unsigned int symtab_index = sym->symtab_index();
1618
                unsigned int chain = 0;
1619
                unsigned int first_reloc = 0;
1620
                unsigned int nrelocs = obj->get_incremental_reloc_count(i);
1621
                if (nrelocs > 0)
1622
                  {
1623
                    gold_assert(symtab_index != -1U
1624
                                && (symtab_index - first_global_index
1625
                                    < global_sym_count));
1626
                    first_reloc = obj->get_incremental_reloc_base(i);
1627
                    chain = global_syms[symtab_index - first_global_index];
1628
                    global_syms[symtab_index - first_global_index] =
1629
                        pov - oview;
1630
                  }
1631
                Swap32::writeval(pov, symtab_index);
1632
                Swap32::writeval(pov + 4, shndx);
1633
                Swap32::writeval(pov + 8, chain);
1634
                Swap32::writeval(pov + 12, nrelocs);
1635 166 khays
                Swap32::writeval(pov + 16,
1636
                                 first_reloc * (8 + 2 * sizeof_addr));
1637 27 khays
                pov += 20;
1638
              }
1639
 
1640
            // For each kept COMDAT group, write the group signature.
1641
            for (unsigned int i = 0; i < ncomdat; i++)
1642
              {
1643
                Stringpool::Key key = entry->get_comdat_signature_key(i);
1644
                off_t name_offset = 0;
1645
                if (key != 0)
1646
                  name_offset = strtab->get_offset_from_key(key);
1647
                Swap32::writeval(pov, name_offset);
1648
                pov += 4;
1649
              }
1650
 
1651
            delete[] index_map;
1652
          }
1653
          break;
1654
 
1655
        case INCREMENTAL_INPUT_SHARED_LIBRARY:
1656
          {
1657
            gold_assert(static_cast<unsigned int>(pov - oview)
1658
                        == (*p)->get_info_offset());
1659
            Incremental_dynobj_entry* entry = (*p)->dynobj_entry();
1660
            gold_assert(entry != NULL);
1661 148 khays
            Object* obj = entry->object();
1662
            Dynobj* dynobj = obj->dynobj();
1663
            gold_assert(dynobj != NULL);
1664 27 khays
            const Object::Symbols* syms = obj->get_global_symbols();
1665
 
1666
            // Write the soname string table index.
1667
            section_offset_type soname_offset =
1668
                strtab->get_offset_from_key(entry->get_soname_key());
1669
            Swap32::writeval(pov, soname_offset);
1670
            pov += 4;
1671
 
1672
            // Skip the global symbol count for now.
1673
            unsigned char* orig_pov = pov;
1674
            pov += 4;
1675
 
1676
            // For each global symbol, write the global symbol table index.
1677
            unsigned int nsyms = syms->size();
1678
            unsigned int nsyms_out = 0;
1679
            for (unsigned int i = 0; i < nsyms; i++)
1680
              {
1681
                const Symbol* sym = (*syms)[i];
1682
                if (sym == NULL)
1683
                  continue;
1684
                if (sym->is_forwarder())
1685
                  sym = this->symtab_->resolve_forwards(sym);
1686
                if (sym->symtab_index() == -1U)
1687
                  continue;
1688 148 khays
                unsigned int flags = 0;
1689 159 khays
                // If the symbol has hidden or internal visibility, we
1690
                // mark it as defined in the shared object so we don't
1691
                // try to resolve it during an incremental update.
1692
                if (sym->visibility() == elfcpp::STV_HIDDEN
1693
                    || sym->visibility() == elfcpp::STV_INTERNAL)
1694 148 khays
                  flags = INCREMENTAL_SHLIB_SYM_DEF;
1695 159 khays
                else if (sym->source() == Symbol::FROM_OBJECT
1696
                         && sym->object() == obj
1697
                         && sym->is_defined())
1698
                  flags = INCREMENTAL_SHLIB_SYM_DEF;
1699 148 khays
                else if (sym->is_copied_from_dynobj()
1700
                         && this->symtab_->get_copy_source(sym) == dynobj)
1701
                  flags = INCREMENTAL_SHLIB_SYM_COPY;
1702
                flags <<= INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT;
1703
                Swap32::writeval(pov, sym->symtab_index() | flags);
1704 27 khays
                pov += 4;
1705
                ++nsyms_out;
1706
              }
1707
 
1708
            // Now write the global symbol count.
1709
            Swap32::writeval(orig_pov, nsyms_out);
1710
          }
1711
          break;
1712
 
1713
        case INCREMENTAL_INPUT_ARCHIVE:
1714
          {
1715
            gold_assert(static_cast<unsigned int>(pov - oview)
1716
                        == (*p)->get_info_offset());
1717
            Incremental_archive_entry* entry = (*p)->archive_entry();
1718
            gold_assert(entry != NULL);
1719
 
1720
            // Write the member count and unused global symbol count.
1721
            unsigned int nmembers = entry->get_member_count();
1722
            unsigned int nsyms = entry->get_unused_global_symbol_count();
1723
            Swap32::writeval(pov, nmembers);
1724
            Swap32::writeval(pov + 4, nsyms);
1725
            pov += 8;
1726
 
1727
            // For each member, write the offset to its input file entry.
1728
            for (unsigned int i = 0; i < nmembers; ++i)
1729
              {
1730
                Incremental_object_entry* member = entry->get_member(i);
1731
                Swap32::writeval(pov, member->get_offset());
1732
                pov += 4;
1733
              }
1734
 
1735
            // For each global symbol, write the name offset.
1736
            for (unsigned int i = 0; i < nsyms; ++i)
1737
              {
1738
                Stringpool::Key key = entry->get_unused_global_symbol(i);
1739
                Swap32::writeval(pov, strtab->get_offset_from_key(key));
1740
                pov += 4;
1741
              }
1742
          }
1743
          break;
1744
 
1745
        default:
1746
          gold_unreachable();
1747
        }
1748
    }
1749
  return pov;
1750
}
1751
 
1752
// Write the contents of the .gnu_incremental_symtab section.
1753
 
1754
template<int size, bool big_endian>
1755
void
1756
Output_section_incremental_inputs<size, big_endian>::write_symtab(
1757
    unsigned char* pov,
1758
    unsigned int* global_syms,
1759
    unsigned int global_sym_count)
1760
{
1761
  for (unsigned int i = 0; i < global_sym_count; ++i)
1762
    {
1763
      Swap32::writeval(pov, global_syms[i]);
1764
      pov += 4;
1765
    }
1766
}
1767
 
1768
// This struct holds the view information needed to write the
1769
// .gnu_incremental_got_plt section.
1770
 
1771
struct Got_plt_view_info
1772
{
1773
  // Start of the GOT type array in the output view.
1774
  unsigned char* got_type_p;
1775
  // Start of the GOT descriptor array in the output view.
1776
  unsigned char* got_desc_p;
1777
  // Start of the PLT descriptor array in the output view.
1778
  unsigned char* plt_desc_p;
1779
  // Number of GOT entries.
1780
  unsigned int got_count;
1781
  // Number of PLT entries.
1782
  unsigned int plt_count;
1783
  // Offset of the first non-reserved PLT entry (this is a target-dependent value).
1784
  unsigned int first_plt_entry_offset;
1785
  // Size of a PLT entry (this is a target-dependent value).
1786
  unsigned int plt_entry_size;
1787
  // Symbol index to write in the GOT descriptor array.  For global symbols,
1788
  // this is the global symbol table index; for local symbols, it is the
1789
  // local symbol table index.
1790
  unsigned int sym_index;
1791
  // Input file index to write in the GOT descriptor array.  For global
1792
  // symbols, this is 0; for local symbols, it is the index of the input
1793
  // file entry in the .gnu_incremental_inputs section.
1794
  unsigned int input_index;
1795
};
1796
 
1797
// Functor class for processing a GOT offset list for local symbols.
1798
// Writes the GOT type and symbol index into the GOT type and descriptor
1799
// arrays in the output section.
1800
 
1801
template<int size, bool big_endian>
1802
class Local_got_offset_visitor : public Got_offset_list::Visitor
1803
{
1804
 public:
1805
  Local_got_offset_visitor(struct Got_plt_view_info& info)
1806
    : info_(info)
1807
  { }
1808
 
1809
  void
1810
  visit(unsigned int got_type, unsigned int got_offset)
1811
  {
1812
    unsigned int got_index = got_offset / this->got_entry_size_;
1813
    gold_assert(got_index < this->info_.got_count);
1814
    // We can only handle GOT entry types in the range 0..0x7e
1815
    // because we use a byte array to store them, and we use the
1816
    // high bit to flag a local symbol.
1817
    gold_assert(got_type < 0x7f);
1818
    this->info_.got_type_p[got_index] = got_type | 0x80;
1819
    unsigned char* pov = this->info_.got_desc_p + got_index * 8;
1820
    elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.sym_index);
1821
    elfcpp::Swap<32, big_endian>::writeval(pov + 4, this->info_.input_index);
1822
  }
1823
 
1824
 private:
1825
  static const unsigned int got_entry_size_ = size / 8;
1826
  struct Got_plt_view_info& info_;
1827
};
1828
 
1829
// Functor class for processing a GOT offset list.  Writes the GOT type
1830
// and symbol index into the GOT type and descriptor arrays in the output
1831
// section.
1832
 
1833
template<int size, bool big_endian>
1834
class Global_got_offset_visitor : public Got_offset_list::Visitor
1835
{
1836
 public:
1837
  Global_got_offset_visitor(struct Got_plt_view_info& info)
1838
    : info_(info)
1839
  { }
1840
 
1841
  void
1842
  visit(unsigned int got_type, unsigned int got_offset)
1843
  {
1844
    unsigned int got_index = got_offset / this->got_entry_size_;
1845
    gold_assert(got_index < this->info_.got_count);
1846
    // We can only handle GOT entry types in the range 0..0x7e
1847
    // because we use a byte array to store them, and we use the
1848
    // high bit to flag a local symbol.
1849
    gold_assert(got_type < 0x7f);
1850
    this->info_.got_type_p[got_index] = got_type;
1851
    unsigned char* pov = this->info_.got_desc_p + got_index * 8;
1852
    elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.sym_index);
1853
    elfcpp::Swap<32, big_endian>::writeval(pov + 4, 0);
1854
  }
1855
 
1856
 private:
1857
  static const unsigned int got_entry_size_ = size / 8;
1858
  struct Got_plt_view_info& info_;
1859
};
1860
 
1861
// Functor class for processing the global symbol table.  Processes the
1862
// GOT offset list for the symbol, and writes the symbol table index
1863
// into the PLT descriptor array in the output section.
1864
 
1865
template<int size, bool big_endian>
1866
class Global_symbol_visitor_got_plt
1867
{
1868
 public:
1869
  Global_symbol_visitor_got_plt(struct Got_plt_view_info& info)
1870
    : info_(info)
1871
  { }
1872
 
1873
  void
1874
  operator()(const Sized_symbol<size>* sym)
1875
  {
1876
    typedef Global_got_offset_visitor<size, big_endian> Got_visitor;
1877
    const Got_offset_list* got_offsets = sym->got_offset_list();
1878
    if (got_offsets != NULL)
1879
      {
1880
        this->info_.sym_index = sym->symtab_index();
1881
        this->info_.input_index = 0;
1882
        Got_visitor v(this->info_);
1883
        got_offsets->for_all_got_offsets(&v);
1884
      }
1885
    if (sym->has_plt_offset())
1886
      {
1887
        unsigned int plt_index =
1888
            ((sym->plt_offset() - this->info_.first_plt_entry_offset)
1889
             / this->info_.plt_entry_size);
1890
        gold_assert(plt_index < this->info_.plt_count);
1891
        unsigned char* pov = this->info_.plt_desc_p + plt_index * 4;
1892
        elfcpp::Swap<32, big_endian>::writeval(pov, sym->symtab_index());
1893
      }
1894
  }
1895
 
1896
 private:
1897
  struct Got_plt_view_info& info_;
1898
};
1899
 
1900
// Write the contents of the .gnu_incremental_got_plt section.
1901
 
1902
template<int size, bool big_endian>
1903
void
1904
Output_section_incremental_inputs<size, big_endian>::write_got_plt(
1905
    unsigned char* pov,
1906
    off_t view_size)
1907
{
1908
  Sized_target<size, big_endian>* target =
1909
    parameters->sized_target<size, big_endian>();
1910
 
1911
  // Set up the view information for the functors.
1912
  struct Got_plt_view_info view_info;
1913
  view_info.got_count = target->got_entry_count();
1914
  view_info.plt_count = target->plt_entry_count();
1915
  view_info.first_plt_entry_offset = target->first_plt_entry_offset();
1916
  view_info.plt_entry_size = target->plt_entry_size();
1917
  view_info.got_type_p = pov + 8;
1918
  view_info.got_desc_p = (view_info.got_type_p
1919
                          + ((view_info.got_count + 3) & ~3));
1920
  view_info.plt_desc_p = view_info.got_desc_p + view_info.got_count * 8;
1921
 
1922
  gold_assert(pov + view_size ==
1923
              view_info.plt_desc_p + view_info.plt_count * 4);
1924
 
1925
  // Write the section header.
1926
  Swap32::writeval(pov, view_info.got_count);
1927
  Swap32::writeval(pov + 4, view_info.plt_count);
1928
 
1929
  // Initialize the GOT type array to 0xff (reserved).
1930
  memset(view_info.got_type_p, 0xff, view_info.got_count);
1931
 
1932
  // Write the incremental GOT descriptors for local symbols.
1933
  typedef Local_got_offset_visitor<size, big_endian> Got_visitor;
1934
  for (Incremental_inputs::Input_list::const_iterator p =
1935
           this->inputs_->input_files().begin();
1936
       p != this->inputs_->input_files().end();
1937
       ++p)
1938
    {
1939
      if ((*p)->type() != INCREMENTAL_INPUT_OBJECT
1940
          && (*p)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
1941
        continue;
1942
      Incremental_object_entry* entry = (*p)->object_entry();
1943
      gold_assert(entry != NULL);
1944
      const Object* obj = entry->object();
1945
      gold_assert(obj != NULL);
1946
      view_info.input_index = (*p)->get_file_index();
1947
      Got_visitor v(view_info);
1948
      obj->for_all_local_got_entries(&v);
1949
    }
1950
 
1951
  // Write the incremental GOT and PLT descriptors for global symbols.
1952
  typedef Global_symbol_visitor_got_plt<size, big_endian> Symbol_visitor;
1953
  symtab_->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(view_info));
1954
}
1955
 
1956
// Class Sized_relobj_incr.  Most of these methods are not used for
1957
// Incremental objects, but are required to be implemented by the
1958
// base class Object.
1959
 
1960
template<int size, bool big_endian>
1961
Sized_relobj_incr<size, big_endian>::Sized_relobj_incr(
1962
    const std::string& name,
1963
    Sized_incremental_binary<size, big_endian>* ibase,
1964
    unsigned int input_file_index)
1965
  : Sized_relobj<size, big_endian>(name, NULL), ibase_(ibase),
1966
    input_file_index_(input_file_index),
1967
    input_reader_(ibase->inputs_reader().input_file(input_file_index)),
1968
    local_symbol_count_(0), output_local_dynsym_count_(0),
1969
    local_symbol_index_(0), local_symbol_offset_(0), local_dynsym_offset_(0),
1970 163 khays
    symbols_(), defined_count_(0), incr_reloc_offset_(-1U),
1971
    incr_reloc_count_(0), incr_reloc_output_index_(0), incr_relocs_(NULL),
1972
    local_symbols_()
1973 27 khays
{
1974
  if (this->input_reader_.is_in_system_directory())
1975
    this->set_is_in_system_directory();
1976
  const unsigned int shnum = this->input_reader_.get_input_section_count() + 1;
1977
  this->set_shnum(shnum);
1978
  ibase->set_input_object(input_file_index, this);
1979
}
1980
 
1981
// Read the symbols.
1982
 
1983
template<int size, bool big_endian>
1984
void
1985
Sized_relobj_incr<size, big_endian>::do_read_symbols(Read_symbols_data*)
1986
{
1987
  gold_unreachable();
1988
}
1989
 
1990
// Lay out the input sections.
1991
 
1992
template<int size, bool big_endian>
1993
void
1994
Sized_relobj_incr<size, big_endian>::do_layout(
1995
    Symbol_table*,
1996
    Layout* layout,
1997
    Read_symbols_data*)
1998
{
1999
  const unsigned int shnum = this->shnum();
2000
  Incremental_inputs* incremental_inputs = layout->incremental_inputs();
2001
  gold_assert(incremental_inputs != NULL);
2002
  Output_sections& out_sections(this->output_sections());
2003
  out_sections.resize(shnum);
2004
  this->section_offsets().resize(shnum);
2005
  for (unsigned int i = 1; i < shnum; i++)
2006
    {
2007
      typename Input_entry_reader::Input_section_info sect =
2008
          this->input_reader_.get_input_section(i - 1);
2009
      // Add the section to the incremental inputs layout.
2010
      incremental_inputs->report_input_section(this, i, sect.name,
2011
                                               sect.sh_size);
2012
      if (sect.output_shndx == 0 || sect.sh_offset == -1)
2013
        continue;
2014
      Output_section* os = this->ibase_->output_section(sect.output_shndx);
2015
      gold_assert(os != NULL);
2016
      out_sections[i] = os;
2017
      this->section_offsets()[i] = static_cast<Address>(sect.sh_offset);
2018
    }
2019
 
2020
  // Process the COMDAT groups.
2021
  unsigned int ncomdat = this->input_reader_.get_comdat_group_count();
2022
  for (unsigned int i = 0; i < ncomdat; i++)
2023
    {
2024
      const char* signature = this->input_reader_.get_comdat_group_signature(i);
2025
      if (signature == NULL || signature[0] == '\0')
2026
        this->error(_("COMDAT group has no signature"));
2027
      bool keep = layout->find_or_add_kept_section(signature, this, i, true,
2028
                                                   true, NULL);
2029 163 khays
      if (keep)
2030
        incremental_inputs->report_comdat_group(this, signature);
2031
      else
2032 27 khays
        this->error(_("COMDAT group %s included twice in incremental link"),
2033
                    signature);
2034
    }
2035
}
2036
 
2037
// Layout sections whose layout was deferred while waiting for
2038
// input files from a plugin.
2039
template<int size, bool big_endian>
2040
void
2041
Sized_relobj_incr<size, big_endian>::do_layout_deferred_sections(Layout*)
2042
{
2043
}
2044
 
2045
// Add the symbols to the symbol table.
2046
 
2047
template<int size, bool big_endian>
2048
void
2049
Sized_relobj_incr<size, big_endian>::do_add_symbols(
2050
    Symbol_table* symtab,
2051
    Read_symbols_data*,
2052
    Layout*)
2053
{
2054
  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2055
  unsigned char symbuf[sym_size];
2056
  elfcpp::Sym<size, big_endian> sym(symbuf);
2057
  elfcpp::Sym_write<size, big_endian> osym(symbuf);
2058
 
2059
  typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
2060
 
2061
  unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2062
  this->symbols_.resize(nsyms);
2063
 
2064
  Incremental_binary::View symtab_view(NULL);
2065
  unsigned int symtab_count;
2066
  elfcpp::Elf_strtab strtab(NULL, 0);
2067
  this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2068
 
2069
  Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
2070
  unsigned int isym_count = isymtab.symbol_count();
2071
  unsigned int first_global = symtab_count - isym_count;
2072
 
2073
  const unsigned char* sym_p;
2074
  for (unsigned int i = 0; i < nsyms; ++i)
2075
    {
2076
      Incremental_global_symbol_reader<big_endian> info =
2077
          this->input_reader_.get_global_symbol_reader(i);
2078
      unsigned int output_symndx = info.output_symndx();
2079
      sym_p = symtab_view.data() + output_symndx * sym_size;
2080
      elfcpp::Sym<size, big_endian> gsym(sym_p);
2081
      const char* name;
2082
      if (!strtab.get_c_string(gsym.get_st_name(), &name))
2083
        name = "";
2084
 
2085
      typename elfcpp::Elf_types<size>::Elf_Addr v = gsym.get_st_value();
2086
      unsigned int shndx = gsym.get_st_shndx();
2087
      elfcpp::STB st_bind = gsym.get_st_bind();
2088
      elfcpp::STT st_type = gsym.get_st_type();
2089
 
2090
      // Local hidden symbols start out as globals, but get converted to
2091
      // to local during output.
2092
      if (st_bind == elfcpp::STB_LOCAL)
2093
        st_bind = elfcpp::STB_GLOBAL;
2094
 
2095
      unsigned int input_shndx = info.shndx();
2096 148 khays
      if (input_shndx == 0 || input_shndx == -1U)
2097 27 khays
        {
2098
          shndx = elfcpp::SHN_UNDEF;
2099
          v = 0;
2100
        }
2101
      else if (shndx != elfcpp::SHN_ABS)
2102
        {
2103
          // Find the input section and calculate the section-relative value.
2104
          gold_assert(shndx != elfcpp::SHN_UNDEF);
2105
          Output_section* os = this->ibase_->output_section(shndx);
2106
          gold_assert(os != NULL && os->has_fixed_layout());
2107
          typename Input_entry_reader::Input_section_info sect =
2108
              this->input_reader_.get_input_section(input_shndx - 1);
2109
          gold_assert(sect.output_shndx == shndx);
2110
          if (st_type != elfcpp::STT_TLS)
2111
            v -= os->address();
2112
          v -= sect.sh_offset;
2113
          shndx = input_shndx;
2114
        }
2115
 
2116
      osym.put_st_name(0);
2117
      osym.put_st_value(v);
2118
      osym.put_st_size(gsym.get_st_size());
2119
      osym.put_st_info(st_bind, st_type);
2120
      osym.put_st_other(gsym.get_st_other());
2121
      osym.put_st_shndx(shndx);
2122
 
2123 148 khays
      Symbol* res = symtab->add_from_incrobj(this, name, NULL, &sym);
2124
 
2125 163 khays
      if (shndx != elfcpp::SHN_UNDEF)
2126
        ++this->defined_count_;
2127
 
2128 148 khays
      // If this is a linker-defined symbol that hasn't yet been defined,
2129
      // define it now.
2130
      if (input_shndx == -1U && !res->is_defined())
2131
        {
2132
          shndx = gsym.get_st_shndx();
2133
          v = gsym.get_st_value();
2134
          Elf_size_type symsize = gsym.get_st_size();
2135
          if (shndx == elfcpp::SHN_ABS)
2136
            {
2137
              symtab->define_as_constant(name, NULL,
2138
                                         Symbol_table::INCREMENTAL_BASE,
2139
                                         v, symsize, st_type, st_bind,
2140
                                         gsym.get_st_visibility(), 0,
2141
                                         false, false);
2142
            }
2143
          else
2144
            {
2145
              Output_section* os = this->ibase_->output_section(shndx);
2146
              gold_assert(os != NULL && os->has_fixed_layout());
2147
              v -= os->address();
2148
              if (symsize > 0)
2149
                os->reserve(v, symsize);
2150
              symtab->define_in_output_data(name, NULL,
2151
                                            Symbol_table::INCREMENTAL_BASE,
2152
                                            os, v, symsize, st_type, st_bind,
2153
                                            gsym.get_st_visibility(), 0,
2154
                                            false, false);
2155
            }
2156
        }
2157
 
2158
      this->symbols_[i] = res;
2159
      this->ibase_->add_global_symbol(output_symndx - first_global, res);
2160 27 khays
    }
2161
}
2162
 
2163
// Return TRUE if we should include this object from an archive library.
2164
 
2165
template<int size, bool big_endian>
2166
Archive::Should_include
2167
Sized_relobj_incr<size, big_endian>::do_should_include_member(
2168
    Symbol_table*,
2169
    Layout*,
2170
    Read_symbols_data*,
2171
    std::string*)
2172
{
2173
  gold_unreachable();
2174
}
2175
 
2176
// Iterate over global symbols, calling a visitor class V for each.
2177
 
2178
template<int size, bool big_endian>
2179
void
2180
Sized_relobj_incr<size, big_endian>::do_for_all_global_symbols(
2181
    Read_symbols_data*,
2182
    Library_base::Symbol_visitor_base*)
2183
{
2184
  // This routine is not used for incremental objects.
2185
}
2186
 
2187
// Get the size of a section.
2188
 
2189
template<int size, bool big_endian>
2190
uint64_t
2191
Sized_relobj_incr<size, big_endian>::do_section_size(unsigned int)
2192
{
2193
  gold_unreachable();
2194
}
2195
 
2196
// Get the name of a section.
2197
 
2198
template<int size, bool big_endian>
2199
std::string
2200
Sized_relobj_incr<size, big_endian>::do_section_name(unsigned int)
2201
{
2202
  gold_unreachable();
2203
}
2204
 
2205
// Return a view of the contents of a section.
2206
 
2207
template<int size, bool big_endian>
2208
Object::Location
2209
Sized_relobj_incr<size, big_endian>::do_section_contents(unsigned int)
2210
{
2211
  gold_unreachable();
2212
}
2213
 
2214
// Return section flags.
2215
 
2216
template<int size, bool big_endian>
2217
uint64_t
2218
Sized_relobj_incr<size, big_endian>::do_section_flags(unsigned int)
2219
{
2220
  gold_unreachable();
2221
}
2222
 
2223
// Return section entsize.
2224
 
2225
template<int size, bool big_endian>
2226
uint64_t
2227
Sized_relobj_incr<size, big_endian>::do_section_entsize(unsigned int)
2228
{
2229
  gold_unreachable();
2230
}
2231
 
2232
// Return section address.
2233
 
2234
template<int size, bool big_endian>
2235
uint64_t
2236
Sized_relobj_incr<size, big_endian>::do_section_address(unsigned int)
2237
{
2238
  gold_unreachable();
2239
}
2240
 
2241
// Return section type.
2242
 
2243
template<int size, bool big_endian>
2244
unsigned int
2245
Sized_relobj_incr<size, big_endian>::do_section_type(unsigned int)
2246
{
2247
  gold_unreachable();
2248
}
2249
 
2250
// Return the section link field.
2251
 
2252
template<int size, bool big_endian>
2253
unsigned int
2254
Sized_relobj_incr<size, big_endian>::do_section_link(unsigned int)
2255
{
2256
  gold_unreachable();
2257
}
2258
 
2259
// Return the section link field.
2260
 
2261
template<int size, bool big_endian>
2262
unsigned int
2263
Sized_relobj_incr<size, big_endian>::do_section_info(unsigned int)
2264
{
2265
  gold_unreachable();
2266
}
2267
 
2268
// Return the section alignment.
2269
 
2270
template<int size, bool big_endian>
2271
uint64_t
2272
Sized_relobj_incr<size, big_endian>::do_section_addralign(unsigned int)
2273
{
2274
  gold_unreachable();
2275
}
2276
 
2277
// Return the Xindex structure to use.
2278
 
2279
template<int size, bool big_endian>
2280
Xindex*
2281
Sized_relobj_incr<size, big_endian>::do_initialize_xindex()
2282
{
2283
  gold_unreachable();
2284
}
2285
 
2286
// Get symbol counts.
2287
 
2288
template<int size, bool big_endian>
2289
void
2290
Sized_relobj_incr<size, big_endian>::do_get_global_symbol_counts(
2291 163 khays
    const Symbol_table*,
2292
    size_t* defined,
2293
    size_t* used) const
2294 27 khays
{
2295 163 khays
  *defined = this->defined_count_;
2296
  size_t count = 0;
2297
  for (typename Symbols::const_iterator p = this->symbols_.begin();
2298
       p != this->symbols_.end();
2299
       ++p)
2300
    if (*p != NULL
2301
        && (*p)->source() == Symbol::FROM_OBJECT
2302
        && (*p)->object() == this
2303
        && (*p)->is_defined())
2304
      ++count;
2305
  *used = count;
2306 27 khays
}
2307
 
2308
// Read the relocs.
2309
 
2310
template<int size, bool big_endian>
2311
void
2312
Sized_relobj_incr<size, big_endian>::do_read_relocs(Read_relocs_data*)
2313
{
2314
}
2315
 
2316
// Process the relocs to find list of referenced sections. Used only
2317
// during garbage collection.
2318
 
2319
template<int size, bool big_endian>
2320
void
2321
Sized_relobj_incr<size, big_endian>::do_gc_process_relocs(Symbol_table*,
2322
                                                          Layout*,
2323
                                                          Read_relocs_data*)
2324
{
2325
  gold_unreachable();
2326
}
2327
 
2328
// Scan the relocs and adjust the symbol table.
2329
 
2330
template<int size, bool big_endian>
2331
void
2332
Sized_relobj_incr<size, big_endian>::do_scan_relocs(Symbol_table*,
2333
                                                    Layout* layout,
2334
                                                    Read_relocs_data*)
2335
{
2336
  // Count the incremental relocations for this object.
2337
  unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2338
  this->allocate_incremental_reloc_counts();
2339
  for (unsigned int i = 0; i < nsyms; i++)
2340
    {
2341
      Incremental_global_symbol_reader<big_endian> sym =
2342
          this->input_reader_.get_global_symbol_reader(i);
2343
      unsigned int reloc_count = sym.reloc_count();
2344
      if (reloc_count > 0 && this->incr_reloc_offset_ == -1U)
2345
        this->incr_reloc_offset_ = sym.reloc_offset();
2346
      this->incr_reloc_count_ += reloc_count;
2347
      for (unsigned int j = 0; j < reloc_count; j++)
2348
        this->count_incremental_reloc(i);
2349
    }
2350
  this->incr_reloc_output_index_ =
2351
      layout->incremental_inputs()->get_reloc_count();
2352
  this->finalize_incremental_relocs(layout, false);
2353
 
2354
  // The incoming incremental relocations may not end up in the same
2355
  // location after the incremental update, because the incremental info
2356
  // is regenerated in each link.  Because the new location may overlap
2357
  // with other data in the updated output file, we need to copy the
2358
  // relocations into a buffer so that we can still read them safely
2359
  // after we start writing updates to the output file.
2360
  if (this->incr_reloc_count_ > 0)
2361
    {
2362
      const Incremental_relocs_reader<size, big_endian>& relocs_reader =
2363
          this->ibase_->relocs_reader();
2364
      const unsigned int incr_reloc_size = relocs_reader.reloc_size;
2365
      unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
2366
      this->incr_relocs_ = new unsigned char[len];
2367
      memcpy(this->incr_relocs_,
2368
             relocs_reader.data(this->incr_reloc_offset_),
2369
             len);
2370
    }
2371
}
2372
 
2373
// Count the local symbols.
2374
 
2375
template<int size, bool big_endian>
2376
void
2377
Sized_relobj_incr<size, big_endian>::do_count_local_symbols(
2378
    Stringpool_template<char>* pool,
2379
    Stringpool_template<char>*)
2380
{
2381
  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2382
 
2383
  // Set the count of local symbols based on the incremental info.
2384
  unsigned int nlocals = this->input_reader_.get_local_symbol_count();
2385
  this->local_symbol_count_ = nlocals;
2386
  this->local_symbols_.reserve(nlocals);
2387
 
2388
  // Get views of the base file's symbol table and string table.
2389
  Incremental_binary::View symtab_view(NULL);
2390
  unsigned int symtab_count;
2391
  elfcpp::Elf_strtab strtab(NULL, 0);
2392
  this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2393
 
2394
  // Read the local symbols from the base file's symbol table.
2395
  off_t off = this->input_reader_.get_local_symbol_offset();
2396
  const unsigned char* symp = symtab_view.data() + off;
2397
  for (unsigned int i = 0; i < nlocals; ++i, symp += sym_size)
2398
    {
2399
      elfcpp::Sym<size, big_endian> sym(symp);
2400
      const char* name;
2401
      if (!strtab.get_c_string(sym.get_st_name(), &name))
2402
        name = "";
2403
      gold_debug(DEBUG_INCREMENTAL, "Local symbol %d: %s", i, name);
2404
      name = pool->add(name, true, NULL);
2405
      this->local_symbols_.push_back(Local_symbol(name,
2406
                                                  sym.get_st_value(),
2407
                                                  sym.get_st_size(),
2408
                                                  sym.get_st_shndx(),
2409
                                                  sym.get_st_type(),
2410
                                                  false));
2411
    }
2412
}
2413
 
2414
// Finalize the local symbols.
2415
 
2416
template<int size, bool big_endian>
2417
unsigned int
2418
Sized_relobj_incr<size, big_endian>::do_finalize_local_symbols(
2419
    unsigned int index,
2420
    off_t off,
2421
    Symbol_table*)
2422
{
2423
  this->local_symbol_index_ = index;
2424
  this->local_symbol_offset_ = off;
2425
  return index + this->local_symbol_count_;
2426
}
2427
 
2428
// Set the offset where local dynamic symbol information will be stored.
2429
 
2430
template<int size, bool big_endian>
2431
unsigned int
2432
Sized_relobj_incr<size, big_endian>::do_set_local_dynsym_indexes(
2433
    unsigned int index)
2434
{
2435
  // FIXME: set local dynsym indexes.
2436
  return index;
2437
}
2438
 
2439
// Set the offset where local dynamic symbol information will be stored.
2440
 
2441
template<int size, bool big_endian>
2442
unsigned int
2443
Sized_relobj_incr<size, big_endian>::do_set_local_dynsym_offset(off_t)
2444
{
2445
  return 0;
2446
}
2447
 
2448
// Relocate the input sections and write out the local symbols.
2449
// We don't actually do any relocation here.  For unchanged input files,
2450
// we reapply relocations only for symbols that have changed; that happens
2451
// in queue_final_tasks.  We do need to rewrite the incremental relocations
2452
// for this object.
2453
 
2454
template<int size, bool big_endian>
2455
void
2456
Sized_relobj_incr<size, big_endian>::do_relocate(const Symbol_table*,
2457
                                                 const Layout* layout,
2458
                                                 Output_file* of)
2459
{
2460
  if (this->incr_reloc_count_ == 0)
2461
    return;
2462
 
2463
  const unsigned int incr_reloc_size =
2464
      Incremental_relocs_reader<size, big_endian>::reloc_size;
2465
 
2466
  // Get a view for the .gnu_incremental_relocs section.
2467
  Incremental_inputs* inputs = layout->incremental_inputs();
2468
  gold_assert(inputs != NULL);
2469
  const off_t relocs_off = inputs->relocs_section()->offset();
2470
  const off_t relocs_size = inputs->relocs_section()->data_size();
2471
  unsigned char* const view = of->get_output_view(relocs_off, relocs_size);
2472
 
2473
  // Copy the relocations from the buffer.
2474
  off_t off = this->incr_reloc_output_index_ * incr_reloc_size;
2475
  unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
2476
  memcpy(view + off, this->incr_relocs_, len);
2477
 
2478
  // The output section table may have changed, so we need to map
2479
  // the old section index to the new section index for each relocation.
2480
  for (unsigned int i = 0; i < this->incr_reloc_count_; ++i)
2481
    {
2482
      unsigned char* pov = view + off + i * incr_reloc_size;
2483
      unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(pov + 4);
2484
      Output_section* os = this->ibase_->output_section(shndx);
2485
      gold_assert(os != NULL);
2486
      shndx = os->out_shndx();
2487
      elfcpp::Swap<32, big_endian>::writeval(pov + 4, shndx);
2488
    }
2489
 
2490
  of->write_output_view(off, len, view);
2491
 
2492
  // Get views into the output file for the portions of the symbol table
2493
  // and the dynamic symbol table that we will be writing.
2494
  off_t symtab_off = layout->symtab_section()->offset();
2495
  off_t output_size = this->local_symbol_count_ * This::sym_size;
2496
  unsigned char* oview = NULL;
2497
  if (output_size > 0)
2498
    oview = of->get_output_view(symtab_off + this->local_symbol_offset_,
2499
                                output_size);
2500
 
2501
  off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
2502
  unsigned char* dyn_oview = NULL;
2503
  if (dyn_output_size > 0)
2504
    dyn_oview = of->get_output_view(this->local_dynsym_offset_,
2505
                                    dyn_output_size);
2506
 
2507
  // Write the local symbols.
2508
  unsigned char* ov = oview;
2509
  unsigned char* dyn_ov = dyn_oview;
2510
  const Stringpool* sympool = layout->sympool();
2511
  const Stringpool* dynpool = layout->dynpool();
2512
  Output_symtab_xindex* symtab_xindex = layout->symtab_xindex();
2513
  Output_symtab_xindex* dynsym_xindex = layout->dynsym_xindex();
2514
  for (unsigned int i = 0; i < this->local_symbol_count_; ++i)
2515
    {
2516
      Local_symbol& lsym(this->local_symbols_[i]);
2517
 
2518
      bool is_ordinary;
2519
      unsigned int st_shndx = this->adjust_sym_shndx(i, lsym.st_shndx,
2520
                                                     &is_ordinary);
2521
      if (is_ordinary)
2522
        {
2523
          Output_section* os = this->ibase_->output_section(st_shndx);
2524
          st_shndx = os->out_shndx();
2525
          if (st_shndx >= elfcpp::SHN_LORESERVE)
2526
            {
2527
              symtab_xindex->add(this->local_symbol_index_ + i, st_shndx);
2528
              if (lsym.needs_dynsym_entry)
2529
                dynsym_xindex->add(lsym.output_dynsym_index, st_shndx);
2530
              st_shndx = elfcpp::SHN_XINDEX;
2531
            }
2532
        }
2533
 
2534
      // Write the symbol to the output symbol table.
2535
      {
2536
        elfcpp::Sym_write<size, big_endian> osym(ov);
2537
        osym.put_st_name(sympool->get_offset(lsym.name));
2538
        osym.put_st_value(lsym.st_value);
2539
        osym.put_st_size(lsym.st_size);
2540
        osym.put_st_info(elfcpp::STB_LOCAL,
2541
                         static_cast<elfcpp::STT>(lsym.st_type));
2542
        osym.put_st_other(0);
2543
        osym.put_st_shndx(st_shndx);
2544
        ov += sym_size;
2545
      }
2546
 
2547
      // Write the symbol to the output dynamic symbol table.
2548
      if (lsym.needs_dynsym_entry)
2549
        {
2550
          gold_assert(dyn_ov < dyn_oview + dyn_output_size);
2551
          elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
2552
          osym.put_st_name(dynpool->get_offset(lsym.name));
2553
          osym.put_st_value(lsym.st_value);
2554
          osym.put_st_size(lsym.st_size);
2555
          osym.put_st_info(elfcpp::STB_LOCAL,
2556
                           static_cast<elfcpp::STT>(lsym.st_type));
2557
          osym.put_st_other(0);
2558
          osym.put_st_shndx(st_shndx);
2559
          dyn_ov += sym_size;
2560
        }
2561
    }
2562
 
2563
  if (output_size > 0)
2564
    {
2565
      gold_assert(ov - oview == output_size);
2566
      of->write_output_view(symtab_off + this->local_symbol_offset_,
2567
                            output_size, oview);
2568
    }
2569
 
2570
  if (dyn_output_size > 0)
2571
    {
2572
      gold_assert(dyn_ov - dyn_oview == dyn_output_size);
2573
      of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
2574
                            dyn_oview);
2575
    }
2576
}
2577
 
2578
// Set the offset of a section.
2579
 
2580
template<int size, bool big_endian>
2581
void
2582
Sized_relobj_incr<size, big_endian>::do_set_section_offset(unsigned int,
2583
                                                           uint64_t)
2584
{
2585
}
2586
 
2587
// Class Sized_incr_dynobj.  Most of these methods are not used for
2588
// Incremental objects, but are required to be implemented by the
2589
// base class Object.
2590
 
2591
template<int size, bool big_endian>
2592
Sized_incr_dynobj<size, big_endian>::Sized_incr_dynobj(
2593
    const std::string& name,
2594
    Sized_incremental_binary<size, big_endian>* ibase,
2595
    unsigned int input_file_index)
2596
  : Dynobj(name, NULL), ibase_(ibase),
2597
    input_file_index_(input_file_index),
2598
    input_reader_(ibase->inputs_reader().input_file(input_file_index)),
2599 163 khays
    symbols_(), defined_count_(0)
2600 27 khays
{
2601
  if (this->input_reader_.is_in_system_directory())
2602
    this->set_is_in_system_directory();
2603
  if (this->input_reader_.as_needed())
2604
    this->set_as_needed();
2605
  this->set_soname_string(this->input_reader_.get_soname());
2606
  this->set_shnum(0);
2607
}
2608
 
2609
// Read the symbols.
2610
 
2611
template<int size, bool big_endian>
2612
void
2613
Sized_incr_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
2614
{
2615
  gold_unreachable();
2616
}
2617
 
2618
// Lay out the input sections.
2619
 
2620
template<int size, bool big_endian>
2621
void
2622
Sized_incr_dynobj<size, big_endian>::do_layout(
2623
    Symbol_table*,
2624
    Layout*,
2625
    Read_symbols_data*)
2626
{
2627
}
2628
 
2629
// Add the symbols to the symbol table.
2630
 
2631
template<int size, bool big_endian>
2632
void
2633
Sized_incr_dynobj<size, big_endian>::do_add_symbols(
2634
    Symbol_table* symtab,
2635
    Read_symbols_data*,
2636
    Layout*)
2637
{
2638
  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2639
  unsigned char symbuf[sym_size];
2640
  elfcpp::Sym<size, big_endian> sym(symbuf);
2641
  elfcpp::Sym_write<size, big_endian> osym(symbuf);
2642
 
2643
  typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
2644
 
2645
  unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2646
  this->symbols_.resize(nsyms);
2647
 
2648
  Incremental_binary::View symtab_view(NULL);
2649
  unsigned int symtab_count;
2650
  elfcpp::Elf_strtab strtab(NULL, 0);
2651
  this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2652
 
2653
  Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
2654
  unsigned int isym_count = isymtab.symbol_count();
2655
  unsigned int first_global = symtab_count - isym_count;
2656
 
2657 148 khays
  // We keep a set of symbols that we have generated COPY relocations
2658
  // for, indexed by the symbol value. We do not need more than one
2659
  // COPY relocation per address.
2660
  typedef typename std::set<Address> Copied_symbols;
2661
  Copied_symbols copied_symbols;
2662
 
2663 27 khays
  const unsigned char* sym_p;
2664
  for (unsigned int i = 0; i < nsyms; ++i)
2665
    {
2666
      bool is_def;
2667 148 khays
      bool is_copy;
2668 27 khays
      unsigned int output_symndx =
2669 148 khays
          this->input_reader_.get_output_symbol_index(i, &is_def, &is_copy);
2670 27 khays
      sym_p = symtab_view.data() + output_symndx * sym_size;
2671
      elfcpp::Sym<size, big_endian> gsym(sym_p);
2672
      const char* name;
2673
      if (!strtab.get_c_string(gsym.get_st_name(), &name))
2674
        name = "";
2675
 
2676 148 khays
      Address v;
2677 27 khays
      unsigned int shndx;
2678
      elfcpp::STB st_bind = gsym.get_st_bind();
2679
      elfcpp::STT st_type = gsym.get_st_type();
2680
 
2681
      // Local hidden symbols start out as globals, but get converted to
2682
      // to local during output.
2683
      if (st_bind == elfcpp::STB_LOCAL)
2684
        st_bind = elfcpp::STB_GLOBAL;
2685
 
2686
      if (!is_def)
2687
        {
2688
          shndx = elfcpp::SHN_UNDEF;
2689
          v = 0;
2690
        }
2691
      else
2692
        {
2693
          // For a symbol defined in a shared object, the section index
2694
          // is meaningless, as long as it's not SHN_UNDEF.
2695
          shndx = 1;
2696
          v = gsym.get_st_value();
2697 163 khays
          ++this->defined_count_;
2698 27 khays
        }
2699
 
2700
      osym.put_st_name(0);
2701
      osym.put_st_value(v);
2702
      osym.put_st_size(gsym.get_st_size());
2703
      osym.put_st_info(st_bind, st_type);
2704
      osym.put_st_other(gsym.get_st_other());
2705
      osym.put_st_shndx(shndx);
2706
 
2707 148 khays
      Sized_symbol<size>* res =
2708
          symtab->add_from_incrobj<size, big_endian>(this, name, NULL, &sym);
2709
      this->symbols_[i] = res;
2710 27 khays
      this->ibase_->add_global_symbol(output_symndx - first_global,
2711
                                      this->symbols_[i]);
2712 148 khays
 
2713
      if (is_copy)
2714
        {
2715
          std::pair<typename Copied_symbols::iterator, bool> ins =
2716
              copied_symbols.insert(v);
2717
          if (ins.second)
2718
            {
2719
              unsigned int shndx = gsym.get_st_shndx();
2720
              Output_section* os = this->ibase_->output_section(shndx);
2721
              off_t offset = v - os->address();
2722
              this->ibase_->add_copy_reloc(this->symbols_[i], os, offset);
2723
            }
2724
        }
2725 27 khays
    }
2726
}
2727
 
2728
// Return TRUE if we should include this object from an archive library.
2729
 
2730
template<int size, bool big_endian>
2731
Archive::Should_include
2732
Sized_incr_dynobj<size, big_endian>::do_should_include_member(
2733
    Symbol_table*,
2734
    Layout*,
2735
    Read_symbols_data*,
2736
    std::string*)
2737
{
2738
  gold_unreachable();
2739
}
2740
 
2741
// Iterate over global symbols, calling a visitor class V for each.
2742
 
2743
template<int size, bool big_endian>
2744
void
2745
Sized_incr_dynobj<size, big_endian>::do_for_all_global_symbols(
2746
    Read_symbols_data*,
2747
    Library_base::Symbol_visitor_base*)
2748
{
2749
  // This routine is not used for dynamic libraries.
2750
}
2751
 
2752
// Iterate over local symbols, calling a visitor class V for each GOT offset
2753
// associated with a local symbol.
2754
 
2755
template<int size, bool big_endian>
2756
void
2757
Sized_incr_dynobj<size, big_endian>::do_for_all_local_got_entries(
2758
    Got_offset_list::Visitor*) const
2759
{
2760
}
2761
 
2762
// Get the size of a section.
2763
 
2764
template<int size, bool big_endian>
2765
uint64_t
2766
Sized_incr_dynobj<size, big_endian>::do_section_size(unsigned int)
2767
{
2768
  gold_unreachable();
2769
}
2770
 
2771
// Get the name of a section.
2772
 
2773
template<int size, bool big_endian>
2774
std::string
2775
Sized_incr_dynobj<size, big_endian>::do_section_name(unsigned int)
2776
{
2777
  gold_unreachable();
2778
}
2779
 
2780
// Return a view of the contents of a section.
2781
 
2782
template<int size, bool big_endian>
2783
Object::Location
2784
Sized_incr_dynobj<size, big_endian>::do_section_contents(unsigned int)
2785
{
2786
  gold_unreachable();
2787
}
2788
 
2789
// Return section flags.
2790
 
2791
template<int size, bool big_endian>
2792
uint64_t
2793
Sized_incr_dynobj<size, big_endian>::do_section_flags(unsigned int)
2794
{
2795
  gold_unreachable();
2796
}
2797
 
2798
// Return section entsize.
2799
 
2800
template<int size, bool big_endian>
2801
uint64_t
2802
Sized_incr_dynobj<size, big_endian>::do_section_entsize(unsigned int)
2803
{
2804
  gold_unreachable();
2805
}
2806
 
2807
// Return section address.
2808
 
2809
template<int size, bool big_endian>
2810
uint64_t
2811
Sized_incr_dynobj<size, big_endian>::do_section_address(unsigned int)
2812
{
2813
  gold_unreachable();
2814
}
2815
 
2816
// Return section type.
2817
 
2818
template<int size, bool big_endian>
2819
unsigned int
2820
Sized_incr_dynobj<size, big_endian>::do_section_type(unsigned int)
2821
{
2822
  gold_unreachable();
2823
}
2824
 
2825
// Return the section link field.
2826
 
2827
template<int size, bool big_endian>
2828
unsigned int
2829
Sized_incr_dynobj<size, big_endian>::do_section_link(unsigned int)
2830
{
2831
  gold_unreachable();
2832
}
2833
 
2834
// Return the section link field.
2835
 
2836
template<int size, bool big_endian>
2837
unsigned int
2838
Sized_incr_dynobj<size, big_endian>::do_section_info(unsigned int)
2839
{
2840
  gold_unreachable();
2841
}
2842
 
2843
// Return the section alignment.
2844
 
2845
template<int size, bool big_endian>
2846
uint64_t
2847
Sized_incr_dynobj<size, big_endian>::do_section_addralign(unsigned int)
2848
{
2849
  gold_unreachable();
2850
}
2851
 
2852
// Return the Xindex structure to use.
2853
 
2854
template<int size, bool big_endian>
2855
Xindex*
2856
Sized_incr_dynobj<size, big_endian>::do_initialize_xindex()
2857
{
2858
  gold_unreachable();
2859
}
2860
 
2861
// Get symbol counts.
2862
 
2863
template<int size, bool big_endian>
2864
void
2865
Sized_incr_dynobj<size, big_endian>::do_get_global_symbol_counts(
2866 163 khays
    const Symbol_table*,
2867
    size_t* defined,
2868
    size_t* used) const
2869 27 khays
{
2870 163 khays
  *defined = this->defined_count_;
2871
  size_t count = 0;
2872
  for (typename Symbols::const_iterator p = this->symbols_.begin();
2873
       p != this->symbols_.end();
2874
       ++p)
2875
    if (*p != NULL
2876
        && (*p)->source() == Symbol::FROM_OBJECT
2877
        && (*p)->object() == this
2878
        && (*p)->is_defined()
2879
        && (*p)->dynsym_index() != -1U)
2880
      ++count;
2881
  *used = count;
2882 27 khays
}
2883
 
2884
// Allocate an incremental object of the appropriate size and endianness.
2885
 
2886
Object*
2887
make_sized_incremental_object(
2888
    Incremental_binary* ibase,
2889
    unsigned int input_file_index,
2890
    Incremental_input_type input_type,
2891
    const Incremental_binary::Input_reader* input_reader)
2892
{
2893
  Object* obj = NULL;
2894
  std::string name(input_reader->filename());
2895
 
2896
  switch (parameters->size_and_endianness())
2897
    {
2898
#ifdef HAVE_TARGET_32_LITTLE
2899
    case Parameters::TARGET_32_LITTLE:
2900
      {
2901
        Sized_incremental_binary<32, false>* sized_ibase =
2902
            static_cast<Sized_incremental_binary<32, false>*>(ibase);
2903
        if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2904
          obj = new Sized_incr_dynobj<32, false>(name, sized_ibase,
2905
                                                 input_file_index);
2906
        else
2907
          obj = new Sized_relobj_incr<32, false>(name, sized_ibase,
2908
                                                 input_file_index);
2909
      }
2910
      break;
2911
#endif
2912
#ifdef HAVE_TARGET_32_BIG
2913
    case Parameters::TARGET_32_BIG:
2914
      {
2915
        Sized_incremental_binary<32, true>* sized_ibase =
2916
            static_cast<Sized_incremental_binary<32, true>*>(ibase);
2917
        if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2918
          obj = new Sized_incr_dynobj<32, true>(name, sized_ibase,
2919
                                                input_file_index);
2920
        else
2921
          obj = new Sized_relobj_incr<32, true>(name, sized_ibase,
2922
                                                input_file_index);
2923
      }
2924
      break;
2925
#endif
2926
#ifdef HAVE_TARGET_64_LITTLE
2927
    case Parameters::TARGET_64_LITTLE:
2928
      {
2929
        Sized_incremental_binary<64, false>* sized_ibase =
2930
            static_cast<Sized_incremental_binary<64, false>*>(ibase);
2931
        if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2932
          obj = new Sized_incr_dynobj<64, false>(name, sized_ibase,
2933
                                                 input_file_index);
2934
        else
2935
          obj = new Sized_relobj_incr<64, false>(name, sized_ibase,
2936
                                                 input_file_index);
2937
     }
2938
      break;
2939
#endif
2940
#ifdef HAVE_TARGET_64_BIG
2941
    case Parameters::TARGET_64_BIG:
2942
      {
2943
        Sized_incremental_binary<64, true>* sized_ibase =
2944
            static_cast<Sized_incremental_binary<64, true>*>(ibase);
2945
        if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2946
          obj = new Sized_incr_dynobj<64, true>(name, sized_ibase,
2947
                                                input_file_index);
2948
        else
2949
          obj = new Sized_relobj_incr<64, true>(name, sized_ibase,
2950
                                                input_file_index);
2951
      }
2952
      break;
2953
#endif
2954
    default:
2955
      gold_unreachable();
2956
    }
2957
 
2958
  gold_assert(obj != NULL);
2959
  return obj;
2960
}
2961
 
2962
// Copy the unused symbols from the incremental input info.
2963
// We need to do this because we may be overwriting the incremental
2964
// input info in the base file before we write the new incremental
2965
// info.
2966
void
2967
Incremental_library::copy_unused_symbols()
2968
{
2969
  unsigned int symcount = this->input_reader_->get_unused_symbol_count();
2970
  this->unused_symbols_.reserve(symcount);
2971
  for (unsigned int i = 0; i < symcount; ++i)
2972
    {
2973
      std::string name(this->input_reader_->get_unused_symbol(i));
2974
      this->unused_symbols_.push_back(name);
2975
    }
2976
}
2977
 
2978
// Iterator for unused global symbols in the library.
2979
void
2980
Incremental_library::do_for_all_unused_symbols(Symbol_visitor_base* v) const
2981
{
2982
  for (Symbol_list::const_iterator p = this->unused_symbols_.begin();
2983
       p != this->unused_symbols_.end();
2984
       ++p)
2985
  v->visit(p->c_str());
2986
}
2987
 
2988
// Instantiate the templates we need.
2989
 
2990
#ifdef HAVE_TARGET_32_LITTLE
2991
template
2992
class Sized_incremental_binary<32, false>;
2993
 
2994
template
2995
class Sized_relobj_incr<32, false>;
2996
 
2997
template
2998
class Sized_incr_dynobj<32, false>;
2999
#endif
3000
 
3001
#ifdef HAVE_TARGET_32_BIG
3002
template
3003
class Sized_incremental_binary<32, true>;
3004
 
3005
template
3006
class Sized_relobj_incr<32, true>;
3007
 
3008
template
3009
class Sized_incr_dynobj<32, true>;
3010
#endif
3011
 
3012
#ifdef HAVE_TARGET_64_LITTLE
3013
template
3014
class Sized_incremental_binary<64, false>;
3015
 
3016
template
3017
class Sized_relobj_incr<64, false>;
3018
 
3019
template
3020
class Sized_incr_dynobj<64, false>;
3021
#endif
3022
 
3023
#ifdef HAVE_TARGET_64_BIG
3024
template
3025
class Sized_incremental_binary<64, true>;
3026
 
3027
template
3028
class Sized_relobj_incr<64, true>;
3029
 
3030
template
3031
class Sized_incr_dynobj<64, true>;
3032
#endif
3033
 
3034
} // End namespace gold.

powered by: WebSVN 2.1.0

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