forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincremental.cc
3134 lines (2738 loc) · 96.6 KB
/
incremental.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// inremental.cc -- incremental linking support for gold
// Copyright (C) 2009-2025 Free Software Foundation, Inc.
// Written by Mikolaj Zalewski <[email protected]>.
// This file is part of gold.
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
// MA 02110-1301, USA.
#include "gold.h"
#include <set>
#include <cstdarg>
#include "libiberty.h"
#include "elfcpp.h"
#include "options.h"
#include "output.h"
#include "symtab.h"
#include "incremental.h"
#include "archive.h"
#include "object.h"
#include "target-select.h"
#include "target.h"
#include "fileread.h"
#include "script.h"
namespace gold {
// Version number for the .gnu_incremental_inputs section.
// Version 1 was the initial checkin.
// Version 2 adds some padding to ensure 8-byte alignment where necessary.
const unsigned int INCREMENTAL_LINK_VERSION = 2;
// This class manages the .gnu_incremental_inputs section, which holds
// the header information, a directory of input files, and separate
// entries for each input file.
template<int size, bool big_endian>
class Output_section_incremental_inputs : public Output_section_data
{
public:
Output_section_incremental_inputs(const Incremental_inputs* inputs,
const Symbol_table* symtab)
: Output_section_data(size / 8), inputs_(inputs), symtab_(symtab)
{ }
protected:
// This is called to update the section size prior to assigning
// the address and file offset.
void
update_data_size()
{ this->set_final_data_size(); }
// Set the final data size.
void
set_final_data_size();
// Write the data to the file.
void
do_write(Output_file*);
// Write to a map file.
void
do_print_to_mapfile(Mapfile* mapfile) const
{ mapfile->print_output_data(this, _("** incremental_inputs")); }
private:
// Write the section header.
unsigned char*
write_header(unsigned char* pov, unsigned int input_file_count,
section_offset_type command_line_offset);
// Write the input file entries.
unsigned char*
write_input_files(unsigned char* oview, unsigned char* pov,
Stringpool* strtab);
// Write the supplemental information blocks.
unsigned char*
write_info_blocks(unsigned char* oview, unsigned char* pov,
Stringpool* strtab, unsigned int* global_syms,
unsigned int global_sym_count);
// Write the contents of the .gnu_incremental_symtab section.
void
write_symtab(unsigned char* pov, unsigned int* global_syms,
unsigned int global_sym_count);
// Write the contents of the .gnu_incremental_got_plt section.
void
write_got_plt(unsigned char* pov, off_t view_size);
// Typedefs for writing the data to the output sections.
typedef elfcpp::Swap<size, big_endian> Swap;
typedef elfcpp::Swap<16, big_endian> Swap16;
typedef elfcpp::Swap<32, big_endian> Swap32;
typedef elfcpp::Swap<64, big_endian> Swap64;
// Sizes of various structures.
static const int sizeof_addr = size / 8;
static const int header_size =
Incremental_inputs_reader<size, big_endian>::header_size;
static const int input_entry_size =
Incremental_inputs_reader<size, big_endian>::input_entry_size;
static const unsigned int object_info_size =
Incremental_inputs_reader<size, big_endian>::object_info_size;
static const unsigned int input_section_entry_size =
Incremental_inputs_reader<size, big_endian>::input_section_entry_size;
static const unsigned int global_sym_entry_size =
Incremental_inputs_reader<size, big_endian>::global_sym_entry_size;
static const unsigned int incr_reloc_size =
Incremental_relocs_reader<size, big_endian>::reloc_size;
// The Incremental_inputs object.
const Incremental_inputs* inputs_;
// The symbol table.
const Symbol_table* symtab_;
};
// Inform the user why we don't do an incremental link. Not called in
// the obvious case of missing output file. TODO: Is this helpful?
void
vexplain_no_incremental(const char* format, va_list args)
{
char* buf = NULL;
if (vasprintf(&buf, format, args) < 0)
gold_nomem();
gold_info(_("the link might take longer: "
"cannot perform incremental link: %s"), buf);
free(buf);
}
void
explain_no_incremental(const char* format, ...)
{
va_list args;
va_start(args, format);
vexplain_no_incremental(format, args);
va_end(args);
}
// Report an error.
void
Incremental_binary::error(const char* format, ...) const
{
va_list args;
va_start(args, format);
// Current code only checks if the file can be used for incremental linking,
// so errors shouldn't fail the build, but only result in a fallback to a
// full build.
// TODO: when we implement incremental editing of the file, we may need a
// flag that will cause errors to be treated seriously.
vexplain_no_incremental(format, args);
va_end(args);
}
// Return TRUE if a section of type SH_TYPE can be updated in place
// during an incremental update. We can update sections of type PROGBITS,
// NOBITS, INIT_ARRAY, FINI_ARRAY, PREINIT_ARRAY, NOTE, and
// (processor-specific) unwind sections. All others will be regenerated.
bool
can_incremental_update(unsigned int sh_type)
{
return (sh_type == elfcpp::SHT_PROGBITS
|| sh_type == elfcpp::SHT_NOBITS
|| sh_type == elfcpp::SHT_INIT_ARRAY
|| sh_type == elfcpp::SHT_FINI_ARRAY
|| sh_type == elfcpp::SHT_PREINIT_ARRAY
|| sh_type == elfcpp::SHT_NOTE
|| sh_type == parameters->target().unwind_section_type());
}
// Find the .gnu_incremental_inputs section and related sections.
template<int size, bool big_endian>
bool
Sized_incremental_binary<size, big_endian>::find_incremental_inputs_sections(
unsigned int* p_inputs_shndx,
unsigned int* p_symtab_shndx,
unsigned int* p_relocs_shndx,
unsigned int* p_got_plt_shndx,
unsigned int* p_strtab_shndx)
{
unsigned int inputs_shndx =
this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_INPUTS);
if (inputs_shndx == elfcpp::SHN_UNDEF) // Not found.
return false;
unsigned int symtab_shndx =
this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_SYMTAB);
if (symtab_shndx == elfcpp::SHN_UNDEF) // Not found.
return false;
if (this->elf_file_.section_link(symtab_shndx) != inputs_shndx)
return false;
unsigned int relocs_shndx =
this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_RELOCS);
if (relocs_shndx == elfcpp::SHN_UNDEF) // Not found.
return false;
if (this->elf_file_.section_link(relocs_shndx) != inputs_shndx)
return false;
unsigned int got_plt_shndx =
this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT);
if (got_plt_shndx == elfcpp::SHN_UNDEF) // Not found.
return false;
if (this->elf_file_.section_link(got_plt_shndx) != inputs_shndx)
return false;
unsigned int strtab_shndx = this->elf_file_.section_link(inputs_shndx);
if (strtab_shndx == elfcpp::SHN_UNDEF
|| strtab_shndx > this->elf_file_.shnum()
|| this->elf_file_.section_type(strtab_shndx) != elfcpp::SHT_STRTAB)
return false;
if (p_inputs_shndx != NULL)
*p_inputs_shndx = inputs_shndx;
if (p_symtab_shndx != NULL)
*p_symtab_shndx = symtab_shndx;
if (p_relocs_shndx != NULL)
*p_relocs_shndx = relocs_shndx;
if (p_got_plt_shndx != NULL)
*p_got_plt_shndx = got_plt_shndx;
if (p_strtab_shndx != NULL)
*p_strtab_shndx = strtab_shndx;
return true;
}
// Set up the readers into the incremental info sections.
template<int size, bool big_endian>
void
Sized_incremental_binary<size, big_endian>::setup_readers()
{
unsigned int inputs_shndx;
unsigned int symtab_shndx;
unsigned int relocs_shndx;
unsigned int got_plt_shndx;
unsigned int strtab_shndx;
if (!this->find_incremental_inputs_sections(&inputs_shndx, &symtab_shndx,
&relocs_shndx, &got_plt_shndx,
&strtab_shndx))
return;
Location inputs_location(this->elf_file_.section_contents(inputs_shndx));
Location symtab_location(this->elf_file_.section_contents(symtab_shndx));
Location relocs_location(this->elf_file_.section_contents(relocs_shndx));
Location got_plt_location(this->elf_file_.section_contents(got_plt_shndx));
Location strtab_location(this->elf_file_.section_contents(strtab_shndx));
View inputs_view = this->view(inputs_location);
View symtab_view = this->view(symtab_location);
View relocs_view = this->view(relocs_location);
View got_plt_view = this->view(got_plt_location);
View strtab_view = this->view(strtab_location);
elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size);
this->inputs_reader_ =
Incremental_inputs_reader<size, big_endian>(inputs_view.data(), strtab);
this->symtab_reader_ =
Incremental_symtab_reader<big_endian>(symtab_view.data(),
symtab_location.data_size);
this->relocs_reader_ =
Incremental_relocs_reader<size, big_endian>(relocs_view.data(),
relocs_location.data_size);
this->got_plt_reader_ =
Incremental_got_plt_reader<big_endian>(got_plt_view.data());
// Find the main symbol table.
unsigned int main_symtab_shndx =
this->elf_file_.find_section_by_type(elfcpp::SHT_SYMTAB);
gold_assert(main_symtab_shndx != elfcpp::SHN_UNDEF);
this->main_symtab_loc_ = this->elf_file_.section_contents(main_symtab_shndx);
// Find the main symbol string table.
unsigned int main_strtab_shndx =
this->elf_file_.section_link(main_symtab_shndx);
gold_assert(main_strtab_shndx != elfcpp::SHN_UNDEF
&& main_strtab_shndx < this->elf_file_.shnum());
this->main_strtab_loc_ = this->elf_file_.section_contents(main_strtab_shndx);
// Walk the list of input files (a) to setup an Input_reader for each
// input file, and (b) to record maps of files added from archive
// libraries and scripts.
Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
unsigned int count = inputs.input_file_count();
this->input_objects_.resize(count);
this->input_entry_readers_.reserve(count);
this->library_map_.resize(count);
this->script_map_.resize(count);
for (unsigned int i = 0; i < count; i++)
{
Input_entry_reader input_file = inputs.input_file(i);
#if __cplusplus >= 2001103L
this->input_entry_readers_.emplace_back(input_file);
#else
this->input_entry_readers_.push_back(Sized_input_reader(input_file));
#endif
switch (input_file.type())
{
case INCREMENTAL_INPUT_OBJECT:
case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
case INCREMENTAL_INPUT_SHARED_LIBRARY:
// No special treatment necessary.
break;
case INCREMENTAL_INPUT_ARCHIVE:
{
Incremental_library* lib =
new Incremental_library(input_file.filename(), i,
&this->input_entry_readers_[i]);
this->library_map_[i] = lib;
unsigned int member_count = input_file.get_member_count();
for (unsigned int j = 0; j < member_count; j++)
{
int member_offset = input_file.get_member_offset(j);
int member_index = inputs.input_file_index(member_offset);
this->library_map_[member_index] = lib;
}
}
break;
case INCREMENTAL_INPUT_SCRIPT:
{
Script_info* script = new Script_info(input_file.filename(), i);
this->script_map_[i] = script;
unsigned int object_count = input_file.get_object_count();
for (unsigned int j = 0; j < object_count; j++)
{
int object_offset = input_file.get_object_offset(j);
int object_index = inputs.input_file_index(object_offset);
this->script_map_[object_index] = script;
}
}
break;
default:
gold_unreachable();
}
}
// Initialize the map of global symbols.
unsigned int nglobals = this->symtab_reader_.symbol_count();
this->symbol_map_.resize(nglobals);
this->has_incremental_info_ = true;
}
// Walk the list of input files given on the command line, and build
// a direct map of file index to the corresponding input argument.
void
check_input_args(std::vector<const Input_argument*>& input_args_map,
Input_arguments::const_iterator begin,
Input_arguments::const_iterator end)
{
for (Input_arguments::const_iterator p = begin;
p != end;
++p)
{
if (p->is_group())
{
const Input_file_group* group = p->group();
check_input_args(input_args_map, group->begin(), group->end());
}
else if (p->is_lib())
{
const Input_file_lib* lib = p->lib();
check_input_args(input_args_map, lib->begin(), lib->end());
}
else
{
gold_assert(p->is_file());
unsigned int arg_serial = p->file().arg_serial();
if (arg_serial > 0)
{
gold_assert(arg_serial <= input_args_map.size());
gold_assert(input_args_map[arg_serial - 1] == 0);
input_args_map[arg_serial - 1] = &*p;
}
}
}
}
// Determine whether an incremental link based on the existing output file
// can be done.
template<int size, bool big_endian>
bool
Sized_incremental_binary<size, big_endian>::do_check_inputs(
const Command_line& cmdline,
Incremental_inputs* incremental_inputs)
{
Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
if (!this->has_incremental_info_)
{
explain_no_incremental(_("no incremental data from previous build"));
return false;
}
if (inputs.version() != INCREMENTAL_LINK_VERSION)
{
explain_no_incremental(_("different version of incremental build data"));
return false;
}
if (incremental_inputs->command_line() != inputs.command_line())
{
gold_debug(DEBUG_INCREMENTAL,
"old command line: %s",
inputs.command_line());
gold_debug(DEBUG_INCREMENTAL,
"new command line: %s",
incremental_inputs->command_line().c_str());
explain_no_incremental(_("command line changed"));
return false;
}
// Walk the list of input files given on the command line, and build
// a direct map of argument serial numbers to the corresponding input
// arguments.
this->input_args_map_.resize(cmdline.number_of_input_files());
check_input_args(this->input_args_map_, cmdline.begin(), cmdline.end());
// Walk the list of input files to check for conditions that prevent
// an incremental update link.
unsigned int count = inputs.input_file_count();
for (unsigned int i = 0; i < count; i++)
{
Input_entry_reader input_file = inputs.input_file(i);
switch (input_file.type())
{
case INCREMENTAL_INPUT_OBJECT:
case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
case INCREMENTAL_INPUT_SHARED_LIBRARY:
case INCREMENTAL_INPUT_ARCHIVE:
// No special treatment necessary.
break;
case INCREMENTAL_INPUT_SCRIPT:
if (this->do_file_has_changed(i))
{
explain_no_incremental(_("%s: script file changed"),
input_file.filename());
return false;
}
break;
default:
gold_unreachable();
}
}
return true;
}
// Return TRUE if input file N has changed since the last incremental link.
template<int size, bool big_endian>
bool
Sized_incremental_binary<size, big_endian>::do_file_has_changed(
unsigned int n) const
{
Input_entry_reader input_file = this->inputs_reader_.input_file(n);
Incremental_disposition disp = INCREMENTAL_CHECK;
// For files named in scripts, find the file that was actually named
// on the command line, so that we can get the incremental disposition
// flag.
Script_info* script = this->get_script_info(n);
if (script != NULL)
n = script->input_file_index();
const Input_argument* input_argument = this->get_input_argument(n);
if (input_argument != NULL)
disp = input_argument->file().options().incremental_disposition();
// For files at the beginning of the command line (i.e., those added
// implicitly by gcc), check whether the --incremental-startup-unchanged
// option was used.
if (disp == INCREMENTAL_STARTUP)
disp = parameters->options().incremental_startup_disposition();
if (disp != INCREMENTAL_CHECK)
return disp == INCREMENTAL_CHANGED;
const char* filename = input_file.filename();
Timespec old_mtime = input_file.get_mtime();
Timespec new_mtime;
if (!get_mtime(filename, &new_mtime))
{
// If we can't open get the current modification time, assume it has
// changed. If the file doesn't exist, we'll issue an error when we
// try to open it later.
return true;
}
if (new_mtime.seconds > old_mtime.seconds)
return true;
if (new_mtime.seconds == old_mtime.seconds
&& new_mtime.nanoseconds > old_mtime.nanoseconds)
return true;
return false;
}
// Initialize the layout of the output file based on the existing
// output file.
template<int size, bool big_endian>
void
Sized_incremental_binary<size, big_endian>::do_init_layout(Layout* layout)
{
typedef elfcpp::Shdr<size, big_endian> Shdr;
const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
// Get views of the section headers and the section string table.
const off_t shoff = this->elf_file_.shoff();
const unsigned int shnum = this->elf_file_.shnum();
const unsigned int shstrndx = this->elf_file_.shstrndx();
Location shdrs_location(shoff, shnum * shdr_size);
Location shstrndx_location(this->elf_file_.section_contents(shstrndx));
View shdrs_view = this->view(shdrs_location);
View shstrndx_view = this->view(shstrndx_location);
elfcpp::Elf_strtab shstrtab(shstrndx_view.data(),
shstrndx_location.data_size);
layout->set_incremental_base(this);
// Initialize the layout.
this->section_map_.resize(shnum);
const unsigned char* pshdr = shdrs_view.data() + shdr_size;
for (unsigned int i = 1; i < shnum; i++)
{
Shdr shdr(pshdr);
const char* name;
if (!shstrtab.get_c_string(shdr.get_sh_name(), &name))
name = NULL;
gold_debug(DEBUG_INCREMENTAL,
"Output section: %2d %08lx %08lx %08lx %3d %s",
i,
static_cast<long>(shdr.get_sh_addr()),
static_cast<long>(shdr.get_sh_offset()),
static_cast<long>(shdr.get_sh_size()),
shdr.get_sh_type(), name ? name : "<null>");
this->section_map_[i] = layout->init_fixed_output_section(name, shdr);
pshdr += shdr_size;
}
}
// Mark regions of the input file that must be kept unchanged.
template<int size, bool big_endian>
void
Sized_incremental_binary<size, big_endian>::do_reserve_layout(
unsigned int input_file_index)
{
const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
Input_entry_reader input_file =
this->inputs_reader_.input_file(input_file_index);
if (input_file.type() == INCREMENTAL_INPUT_SHARED_LIBRARY)
{
// Reserve the BSS space used for COPY relocations.
unsigned int nsyms = input_file.get_global_symbol_count();
Incremental_binary::View symtab_view(NULL);
unsigned int symtab_count;
elfcpp::Elf_strtab strtab(NULL, 0);
this->get_symtab_view(&symtab_view, &symtab_count, &strtab);
for (unsigned int i = 0; i < nsyms; ++i)
{
bool is_def;
bool is_copy;
unsigned int output_symndx =
input_file.get_output_symbol_index(i, &is_def, &is_copy);
if (is_copy)
{
const unsigned char* sym_p = (symtab_view.data()
+ output_symndx * sym_size);
elfcpp::Sym<size, big_endian> gsym(sym_p);
unsigned int shndx = gsym.get_st_shndx();
if (shndx < 1 || shndx >= this->section_map_.size())
continue;
Output_section* os = this->section_map_[shndx];
off_t offset = gsym.get_st_value() - os->address();
os->reserve(offset, gsym.get_st_size());
gold_debug(DEBUG_INCREMENTAL,
"Reserve for COPY reloc: %s, off %d, size %d",
os->name(),
static_cast<int>(offset),
static_cast<int>(gsym.get_st_size()));
}
}
return;
}
unsigned int shnum = input_file.get_input_section_count();
for (unsigned int i = 0; i < shnum; i++)
{
typename Input_entry_reader::Input_section_info sect =
input_file.get_input_section(i);
if (sect.output_shndx == 0 || sect.sh_offset == -1)
continue;
Output_section* os = this->section_map_[sect.output_shndx];
gold_assert(os != NULL);
os->reserve(sect.sh_offset, sect.sh_size);
}
}
// Process the GOT and PLT entries from the existing output file.
template<int size, bool big_endian>
void
Sized_incremental_binary<size, big_endian>::do_process_got_plt(
Symbol_table* symtab,
Layout* layout)
{
Incremental_got_plt_reader<big_endian> got_plt_reader(this->got_plt_reader());
Sized_target<size, big_endian>* target =
parameters->sized_target<size, big_endian>();
// Get the number of symbols in the main symbol table and in the
// incremental symbol table. The difference between the two counts
// is the index of the first forced-local or global symbol in the
// main symbol table.
unsigned int symtab_count =
this->main_symtab_loc_.data_size / elfcpp::Elf_sizes<size>::sym_size;
unsigned int isym_count = this->symtab_reader_.symbol_count();
unsigned int first_global = symtab_count - isym_count;
// Tell the target how big the GOT and PLT sections are.
unsigned int got_count = got_plt_reader.get_got_entry_count();
unsigned int plt_count = got_plt_reader.get_plt_entry_count();
Output_data_got_base* got =
target->init_got_plt_for_update(symtab, layout, got_count, plt_count);
// Read the GOT entries from the base file and build the outgoing GOT.
for (unsigned int i = 0; i < got_count; ++i)
{
unsigned int got_type = got_plt_reader.get_got_type(i);
if ((got_type & 0x7f) == 0x7f)
{
// This is the second entry of a pair.
got->reserve_slot(i);
continue;
}
unsigned int symndx = got_plt_reader.get_got_symndx(i);
if (got_type & 0x80)
{
// This is an entry for a local symbol. Ignore this entry if
// the object file was replaced.
unsigned int input_index = got_plt_reader.get_got_input_index(i);
gold_debug(DEBUG_INCREMENTAL,
"GOT entry %d, type %02x: (local symbol)",
i, got_type & 0x7f);
Sized_relobj_incr<size, big_endian>* obj =
this->input_object(input_index);
if (obj != NULL)
target->reserve_local_got_entry(i, obj, symndx, got_type & 0x7f);
}
else
{
// This is an entry for a global symbol. GOT_DESC is the symbol
// table index.
// FIXME: This should really be a fatal error (corrupt input).
gold_assert(symndx >= first_global && symndx < symtab_count);
Symbol* sym = this->global_symbol(symndx - first_global);
// Add the GOT entry only if the symbol is still referenced.
if (sym != NULL && sym->in_reg())
{
gold_debug(DEBUG_INCREMENTAL,
"GOT entry %d, type %02x: %s",
i, got_type, sym->name());
target->reserve_global_got_entry(i, sym, got_type);
}
}
}
// Read the PLT entries from the base file and pass each to the target.
for (unsigned int i = 0; i < plt_count; ++i)
{
unsigned int plt_desc = got_plt_reader.get_plt_desc(i);
// FIXME: This should really be a fatal error (corrupt input).
gold_assert(plt_desc >= first_global && plt_desc < symtab_count);
Symbol* sym = this->global_symbol(plt_desc - first_global);
// Add the PLT entry only if the symbol is still referenced.
if (sym != NULL && sym->in_reg())
{
gold_debug(DEBUG_INCREMENTAL,
"PLT entry %d: %s",
i, sym->name());
target->register_global_plt_entry(symtab, layout, i, sym);
}
}
}
// Emit COPY relocations from the existing output file.
template<int size, bool big_endian>
void
Sized_incremental_binary<size, big_endian>::do_emit_copy_relocs(
Symbol_table* symtab)
{
Sized_target<size, big_endian>* target =
parameters->sized_target<size, big_endian>();
for (typename Copy_relocs::iterator p = this->copy_relocs_.begin();
p != this->copy_relocs_.end();
++p)
{
if (!(*p).symbol->is_copied_from_dynobj())
target->emit_copy_reloc(symtab, (*p).symbol, (*p).output_section,
(*p).offset);
}
}
// Apply incremental relocations for symbols whose values have changed.
template<int size, bool big_endian>
void
Sized_incremental_binary<size, big_endian>::do_apply_incremental_relocs(
const Symbol_table* symtab,
Layout* layout,
Output_file* of)
{
typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addend;
Incremental_symtab_reader<big_endian> isymtab(this->symtab_reader());
Incremental_relocs_reader<size, big_endian> irelocs(this->relocs_reader());
unsigned int nglobals = isymtab.symbol_count();
const unsigned int incr_reloc_size = irelocs.reloc_size;
Relocate_info<size, big_endian> relinfo;
relinfo.symtab = symtab;
relinfo.layout = layout;
relinfo.object = NULL;
relinfo.reloc_shndx = 0;
relinfo.reloc_shdr = NULL;
relinfo.data_shndx = 0;
relinfo.data_shdr = NULL;
Sized_target<size, big_endian>* target =
parameters->sized_target<size, big_endian>();
for (unsigned int i = 0; i < nglobals; i++)
{
const Symbol* gsym = this->global_symbol(i);
// If the symbol is not referenced from any unchanged input files,
// we do not need to reapply any of its relocations.
if (gsym == NULL)
continue;
// If the symbol is defined in an unchanged file, we do not need to
// reapply any of its relocations.
if (gsym->source() == Symbol::FROM_OBJECT
&& gsym->object()->is_incremental())
continue;
gold_debug(DEBUG_INCREMENTAL,
"Applying incremental relocations for global symbol %s [%d]",
gsym->name(), i);
// Follow the linked list of input symbol table entries for this symbol.
// We don't bother to figure out whether the symbol table entry belongs
// to a changed or unchanged file because it's easier just to apply all
// the relocations -- although we might scribble over an area that has
// been reallocated, we do this before copying any new data into the
// output file.
unsigned int offset = isymtab.get_list_head(i);
while (offset > 0)
{
Incremental_global_symbol_reader<big_endian> sym_info =
this->inputs_reader().global_symbol_reader_at_offset(offset);
unsigned int r_base = sym_info.reloc_offset();
unsigned int r_count = sym_info.reloc_count();
// Apply each relocation for this symbol table entry.
for (unsigned int j = 0; j < r_count;
++j, r_base += incr_reloc_size)
{
unsigned int r_type = irelocs.get_r_type(r_base);
unsigned int r_shndx = irelocs.get_r_shndx(r_base);
Address r_offset = irelocs.get_r_offset(r_base);
Addend r_addend = irelocs.get_r_addend(r_base);
Output_section* os = this->output_section(r_shndx);
Address address = os->address();
off_t section_offset = os->offset();
size_t view_size = os->data_size();
unsigned char* const view = of->get_output_view(section_offset,
view_size);
gold_debug(DEBUG_INCREMENTAL,
" %08lx: %s + %d: type %d addend %ld",
(long)(section_offset + r_offset),
os->name(),
(int)r_offset,
r_type,
(long)r_addend);
target->apply_relocation(&relinfo, r_offset, r_type, r_addend,
gsym, view, address, view_size);
// FIXME: Do something more efficient if write_output_view
// ever becomes more than a no-op.
of->write_output_view(section_offset, view_size, view);
}
offset = sym_info.next_offset();
}
}
}
// Get a view of the main symbol table and the symbol string table.
template<int size, bool big_endian>
void
Sized_incremental_binary<size, big_endian>::get_symtab_view(
View* symtab_view,
unsigned int* nsyms,
elfcpp::Elf_strtab* strtab)
{
*symtab_view = this->view(this->main_symtab_loc_);
*nsyms = this->main_symtab_loc_.data_size / elfcpp::Elf_sizes<size>::sym_size;
View strtab_view(this->view(this->main_strtab_loc_));
*strtab = elfcpp::Elf_strtab(strtab_view.data(),
this->main_strtab_loc_.data_size);
}
namespace
{
// Create a Sized_incremental_binary object of the specified size and
// endianness. Fails if the target architecture is not supported.
template<int size, bool big_endian>
Incremental_binary*
make_sized_incremental_binary(Output_file* file,
const elfcpp::Ehdr<size, big_endian>& ehdr)
{
Target* target = select_target(NULL, 0, // XXX
ehdr.get_e_machine(), size, big_endian,
ehdr.get_ei_osabi(),
ehdr.get_ei_abiversion());
if (target == NULL)
{
explain_no_incremental(_("unsupported ELF machine number %d"),
ehdr.get_e_machine());
return NULL;
}
if (!parameters->target_valid())
set_parameters_target(target);
else if (target != ¶meters->target())
gold_error(_("%s: incompatible target"), file->filename());
return new Sized_incremental_binary<size, big_endian>(file, ehdr, target);
}
} // End of anonymous namespace.
// Create an Incremental_binary object for FILE. Returns NULL is this is not
// possible, e.g. FILE is not an ELF file or has an unsupported target. FILE
// should be opened.
Incremental_binary*
open_incremental_binary(Output_file* file)
{
off_t filesize = file->filesize();
int want = elfcpp::Elf_recognizer::max_header_size;
if (filesize < want)
want = filesize;
const unsigned char* p = file->get_input_view(0, want);
if (!elfcpp::Elf_recognizer::is_elf_file(p, want))
{
explain_no_incremental(_("output is not an ELF file."));
return NULL;
}
int size = 0;
bool big_endian = false;
std::string error;
if (!elfcpp::Elf_recognizer::is_valid_header(p, want, &size, &big_endian,
&error))
{
explain_no_incremental(error.c_str());
return NULL;
}
Incremental_binary* result = NULL;
if (size == 32)
{
if (big_endian)
{
#ifdef HAVE_TARGET_32_BIG
result = make_sized_incremental_binary<32, true>(
file, elfcpp::Ehdr<32, true>(p));
#else
explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
#endif
}
else
{
#ifdef HAVE_TARGET_32_LITTLE
result = make_sized_incremental_binary<32, false>(
file, elfcpp::Ehdr<32, false>(p));
#else
explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
#endif
}
}
else if (size == 64)
{
if (big_endian)
{
#ifdef HAVE_TARGET_64_BIG
result = make_sized_incremental_binary<64, true>(
file, elfcpp::Ehdr<64, true>(p));
#else
explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
#endif
}
else
{
#ifdef HAVE_TARGET_64_LITTLE
result = make_sized_incremental_binary<64, false>(
file, elfcpp::Ehdr<64, false>(p));
#else
explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
#endif
}
}
else
gold_unreachable();
return result;
}
// Class Incremental_inputs.
// Add the command line to the string table, setting
// command_line_key_. In incremental builds, the command line is
// stored in .gnu_incremental_inputs so that the next linker run can
// check if the command line options didn't change.
void
Incremental_inputs::report_command_line(int argc, const char* const* argv)
{
// Always store 'gold' as argv[0] to avoid a full relink if the user used a
// different path to the linker.
std::string args("gold");
// Copied from collect_argv in main.cc.
for (int i = 1; i < argc; ++i)
{
// Adding/removing these options should not result in a full relink.
if (strcmp(argv[i], "--incremental") == 0
|| strcmp(argv[i], "--incremental-full") == 0
|| strcmp(argv[i], "--incremental-update") == 0
|| strcmp(argv[i], "--incremental-changed") == 0
|| strcmp(argv[i], "--incremental-unchanged") == 0
|| strcmp(argv[i], "--incremental-unknown") == 0
|| strcmp(argv[i], "--incremental-startup-unchanged") == 0
|| is_prefix_of("--incremental-base=", argv[i])
|| is_prefix_of("--incremental-patch=", argv[i])
|| is_prefix_of("--debug=", argv[i]))
continue;
if (strcmp(argv[i], "--incremental-base") == 0
|| strcmp(argv[i], "--incremental-patch") == 0
|| strcmp(argv[i], "--debug") == 0)
{
// When these options are used without the '=', skip the
// following parameter as well.
++i;
continue;
}
args.append(" '");
// Now append argv[i], but with all single-quotes escaped
const char* argpos = argv[i];
while (1)
{
const int len = strcspn(argpos, "'");
args.append(argpos, len);