forked from Gigallith/gdb-tricore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.cc
3713 lines (3270 loc) · 111 KB
/
object.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
// object.cc -- support for an object file for linking in gold
// Copyright (C) 2006-2020 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <[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 <cerrno>
#include <cstring>
#include <cstdarg>
#include "demangle.h"
#include "libiberty.h"
#include "gc.h"
#include "target-select.h"
#include "dwarf_reader.h"
#include "layout.h"
#include "output.h"
#include "symtab.h"
#include "cref.h"
#include "reloc.h"
#include "object.h"
#include "dynobj.h"
#include "plugin.h"
#include "compressed_output.h"
#include "incremental.h"
#include "merge.h"
namespace gold
{
// Struct Read_symbols_data.
// Destroy any remaining File_view objects and buffers of decompressed
// sections.
Read_symbols_data::~Read_symbols_data()
{
if (this->section_headers != NULL)
delete this->section_headers;
if (this->section_names != NULL)
delete this->section_names;
if (this->symbols != NULL)
delete this->symbols;
if (this->symbol_names != NULL)
delete this->symbol_names;
if (this->versym != NULL)
delete this->versym;
if (this->verdef != NULL)
delete this->verdef;
if (this->verneed != NULL)
delete this->verneed;
}
// Class Xindex.
// Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX
// section and read it in. SYMTAB_SHNDX is the index of the symbol
// table we care about.
template<int size, bool big_endian>
void
Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx)
{
if (!this->symtab_xindex_.empty())
return;
gold_assert(symtab_shndx != 0);
// Look through the sections in reverse order, on the theory that it
// is more likely to be near the end than the beginning.
unsigned int i = object->shnum();
while (i > 0)
{
--i;
if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX
&& this->adjust_shndx(object->section_link(i)) == symtab_shndx)
{
this->read_symtab_xindex<size, big_endian>(object, i, NULL);
return;
}
}
object->error(_("missing SHT_SYMTAB_SHNDX section"));
}
// Read in the symtab_xindex_ array, given the section index of the
// SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the
// section headers.
template<int size, bool big_endian>
void
Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx,
const unsigned char* pshdrs)
{
section_size_type bytecount;
const unsigned char* contents;
if (pshdrs == NULL)
contents = object->section_contents(xindex_shndx, &bytecount, false);
else
{
const unsigned char* p = (pshdrs
+ (xindex_shndx
* elfcpp::Elf_sizes<size>::shdr_size));
typename elfcpp::Shdr<size, big_endian> shdr(p);
bytecount = convert_to_section_size_type(shdr.get_sh_size());
contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false);
}
gold_assert(this->symtab_xindex_.empty());
this->symtab_xindex_.reserve(bytecount / 4);
for (section_size_type i = 0; i < bytecount; i += 4)
{
unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
// We preadjust the section indexes we save.
this->symtab_xindex_.push_back(this->adjust_shndx(shndx));
}
}
// Symbol symndx has a section of SHN_XINDEX; return the real section
// index.
unsigned int
Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx)
{
if (symndx >= this->symtab_xindex_.size())
{
object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
symndx);
return elfcpp::SHN_UNDEF;
}
unsigned int shndx = this->symtab_xindex_[symndx];
if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum())
{
object->error(_("extended index for symbol %u out of range: %u"),
symndx, shndx);
return elfcpp::SHN_UNDEF;
}
return shndx;
}
// Class Object.
// Report an error for this object file. This is used by the
// elfcpp::Elf_file interface, and also called by the Object code
// itself.
void
Object::error(const char* format, ...) const
{
va_list args;
va_start(args, format);
char* buf = NULL;
if (vasprintf(&buf, format, args) < 0)
gold_nomem();
va_end(args);
gold_error(_("%s: %s"), this->name().c_str(), buf);
free(buf);
}
// Return a view of the contents of a section.
const unsigned char*
Object::section_contents(unsigned int shndx, section_size_type* plen,
bool cache)
{ return this->do_section_contents(shndx, plen, cache); }
// Read the section data into SD. This is code common to Sized_relobj_file
// and Sized_dynobj, so we put it into Object.
template<int size, bool big_endian>
void
Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
Read_symbols_data* sd)
{
const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
// Read the section headers.
const off_t shoff = elf_file->shoff();
const unsigned int shnum = this->shnum();
sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size,
true, true);
// Read the section names.
const unsigned char* pshdrs = sd->section_headers->data();
const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
this->error(_("section name section has wrong type: %u"),
static_cast<unsigned int>(shdrnames.get_sh_type()));
sd->section_names_size =
convert_to_section_size_type(shdrnames.get_sh_size());
sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
sd->section_names_size, false,
false);
}
// If NAME is the name of a special .gnu.warning section, arrange for
// the warning to be issued. SHNDX is the section index. Return
// whether it is a warning section.
bool
Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
Symbol_table* symtab)
{
const char warn_prefix[] = ".gnu.warning.";
const int warn_prefix_len = sizeof warn_prefix - 1;
if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
{
// Read the section contents to get the warning text. It would
// be nicer if we only did this if we have to actually issue a
// warning. Unfortunately, warnings are issued as we relocate
// sections. That means that we can not lock the object then,
// as we might try to issue the same warning multiple times
// simultaneously.
section_size_type len;
const unsigned char* contents = this->section_contents(shndx, &len,
false);
if (len == 0)
{
const char* warning = name + warn_prefix_len;
contents = reinterpret_cast<const unsigned char*>(warning);
len = strlen(warning);
}
std::string warning(reinterpret_cast<const char*>(contents), len);
symtab->add_warning(name + warn_prefix_len, this, warning);
return true;
}
return false;
}
// If NAME is the name of the special section which indicates that
// this object was compiled with -fsplit-stack, mark it accordingly.
bool
Object::handle_split_stack_section(const char* name)
{
if (strcmp(name, ".note.GNU-split-stack") == 0)
{
this->uses_split_stack_ = true;
return true;
}
if (strcmp(name, ".note.GNU-no-split-stack") == 0)
{
this->has_no_split_stack_ = true;
return true;
}
return false;
}
// Class Relobj
template<int size>
void
Relobj::initialize_input_to_output_map(unsigned int shndx,
typename elfcpp::Elf_types<size>::Elf_Addr starting_address,
Unordered_map<section_offset_type,
typename elfcpp::Elf_types<size>::Elf_Addr>* output_addresses) const {
Object_merge_map *map = this->object_merge_map_;
map->initialize_input_to_output_map<size>(shndx, starting_address,
output_addresses);
}
void
Relobj::add_merge_mapping(Output_section_data *output_data,
unsigned int shndx, section_offset_type offset,
section_size_type length,
section_offset_type output_offset) {
Object_merge_map* object_merge_map = this->get_or_create_merge_map();
object_merge_map->add_mapping(output_data, shndx, offset, length, output_offset);
}
bool
Relobj::merge_output_offset(unsigned int shndx, section_offset_type offset,
section_offset_type *poutput) const {
Object_merge_map* object_merge_map = this->object_merge_map_;
if (object_merge_map == NULL)
return false;
return object_merge_map->get_output_offset(shndx, offset, poutput);
}
const Output_section_data*
Relobj::find_merge_section(unsigned int shndx) const {
Object_merge_map* object_merge_map = this->object_merge_map_;
if (object_merge_map == NULL)
return NULL;
return object_merge_map->find_merge_section(shndx);
}
// To copy the symbols data read from the file to a local data structure.
// This function is called from do_layout only while doing garbage
// collection.
void
Relobj::copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd,
unsigned int section_header_size)
{
gc_sd->section_headers_data =
new unsigned char[(section_header_size)];
memcpy(gc_sd->section_headers_data, sd->section_headers->data(),
section_header_size);
gc_sd->section_names_data =
new unsigned char[sd->section_names_size];
memcpy(gc_sd->section_names_data, sd->section_names->data(),
sd->section_names_size);
gc_sd->section_names_size = sd->section_names_size;
if (sd->symbols != NULL)
{
gc_sd->symbols_data =
new unsigned char[sd->symbols_size];
memcpy(gc_sd->symbols_data, sd->symbols->data(),
sd->symbols_size);
}
else
{
gc_sd->symbols_data = NULL;
}
gc_sd->symbols_size = sd->symbols_size;
gc_sd->external_symbols_offset = sd->external_symbols_offset;
if (sd->symbol_names != NULL)
{
gc_sd->symbol_names_data =
new unsigned char[sd->symbol_names_size];
memcpy(gc_sd->symbol_names_data, sd->symbol_names->data(),
sd->symbol_names_size);
}
else
{
gc_sd->symbol_names_data = NULL;
}
gc_sd->symbol_names_size = sd->symbol_names_size;
}
// This function determines if a particular section name must be included
// in the link. This is used during garbage collection to determine the
// roots of the worklist.
bool
Relobj::is_section_name_included(const char* name)
{
if (is_prefix_of(".ctors", name)
|| is_prefix_of(".dtors", name)
|| is_prefix_of(".note", name)
|| is_prefix_of(".init", name)
|| is_prefix_of(".fini", name)
|| is_prefix_of(".gcc_except_table", name)
|| is_prefix_of(".jcr", name)
|| is_prefix_of(".preinit_array", name)
|| (is_prefix_of(".text", name)
&& strstr(name, "personality"))
|| (is_prefix_of(".data", name)
&& strstr(name, "personality"))
|| (is_prefix_of(".sdata", name)
&& strstr(name, "personality"))
|| (is_prefix_of(".gnu.linkonce.d", name)
&& strstr(name, "personality"))
|| (is_prefix_of(".rodata", name)
&& strstr(name, "nptl_version")))
{
return true;
}
return false;
}
// Finalize the incremental relocation information. Allocates a block
// of relocation entries for each symbol, and sets the reloc_bases_
// array to point to the first entry in each block. If CLEAR_COUNTS
// is TRUE, also clear the per-symbol relocation counters.
void
Relobj::finalize_incremental_relocs(Layout* layout, bool clear_counts)
{
unsigned int nsyms = this->get_global_symbols()->size();
this->reloc_bases_ = new unsigned int[nsyms];
gold_assert(this->reloc_bases_ != NULL);
gold_assert(layout->incremental_inputs() != NULL);
unsigned int rindex = layout->incremental_inputs()->get_reloc_count();
for (unsigned int i = 0; i < nsyms; ++i)
{
this->reloc_bases_[i] = rindex;
rindex += this->reloc_counts_[i];
if (clear_counts)
this->reloc_counts_[i] = 0;
}
layout->incremental_inputs()->set_reloc_count(rindex);
}
Object_merge_map*
Relobj::get_or_create_merge_map()
{
if (!this->object_merge_map_)
this->object_merge_map_ = new Object_merge_map();
return this->object_merge_map_;
}
// Class Sized_relobj.
// Iterate over local symbols, calling a visitor class V for each GOT offset
// associated with a local symbol.
template<int size, bool big_endian>
void
Sized_relobj<size, big_endian>::do_for_all_local_got_entries(
Got_offset_list::Visitor* v) const
{
unsigned int nsyms = this->local_symbol_count();
for (unsigned int i = 0; i < nsyms; i++)
{
Local_got_entry_key key(i, 0);
Local_got_offsets::const_iterator p = this->local_got_offsets_.find(key);
if (p != this->local_got_offsets_.end())
{
const Got_offset_list* got_offsets = p->second;
got_offsets->for_all_got_offsets(v);
}
}
}
// Get the address of an output section.
template<int size, bool big_endian>
uint64_t
Sized_relobj<size, big_endian>::do_output_section_address(
unsigned int shndx)
{
// If the input file is linked as --just-symbols, the output
// section address is the input section address.
if (this->just_symbols())
return this->section_address(shndx);
const Output_section* os = this->do_output_section(shndx);
gold_assert(os != NULL);
return os->address();
}
// Class Sized_relobj_file.
template<int size, bool big_endian>
Sized_relobj_file<size, big_endian>::Sized_relobj_file(
const std::string& name,
Input_file* input_file,
off_t offset,
const elfcpp::Ehdr<size, big_endian>& ehdr)
: Sized_relobj<size, big_endian>(name, input_file, offset),
elf_file_(this, ehdr),
symtab_shndx_(-1U),
local_symbol_count_(0),
output_local_symbol_count_(0),
output_local_dynsym_count_(0),
symbols_(),
defined_count_(0),
local_symbol_offset_(0),
local_dynsym_offset_(0),
local_values_(),
local_plt_offsets_(),
kept_comdat_sections_(),
has_eh_frame_(false),
is_deferred_layout_(false),
deferred_layout_(),
deferred_layout_relocs_(),
output_views_(NULL)
{
this->e_type_ = ehdr.get_e_type();
}
template<int size, bool big_endian>
Sized_relobj_file<size, big_endian>::~Sized_relobj_file()
{
}
// Set up an object file based on the file header. This sets up the
// section information.
template<int size, bool big_endian>
void
Sized_relobj_file<size, big_endian>::do_setup()
{
const unsigned int shnum = this->elf_file_.shnum();
this->set_shnum(shnum);
}
// Find the SHT_SYMTAB section, given the section headers. The ELF
// standard says that maybe in the future there can be more than one
// SHT_SYMTAB section. Until somebody figures out how that could
// work, we assume there is only one.
template<int size, bool big_endian>
void
Sized_relobj_file<size, big_endian>::find_symtab(const unsigned char* pshdrs)
{
const unsigned int shnum = this->shnum();
this->symtab_shndx_ = 0;
if (shnum > 0)
{
// Look through the sections in reverse order, since gas tends
// to put the symbol table at the end.
const unsigned char* p = pshdrs + shnum * This::shdr_size;
unsigned int i = shnum;
unsigned int xindex_shndx = 0;
unsigned int xindex_link = 0;
while (i > 0)
{
--i;
p -= This::shdr_size;
typename This::Shdr shdr(p);
if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
{
this->symtab_shndx_ = i;
if (xindex_shndx > 0 && xindex_link == i)
{
Xindex* xindex =
new Xindex(this->elf_file_.large_shndx_offset());
xindex->read_symtab_xindex<size, big_endian>(this,
xindex_shndx,
pshdrs);
this->set_xindex(xindex);
}
break;
}
// Try to pick up the SHT_SYMTAB_SHNDX section, if there is
// one. This will work if it follows the SHT_SYMTAB
// section.
if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX)
{
xindex_shndx = i;
xindex_link = this->adjust_shndx(shdr.get_sh_link());
}
}
}
}
// Return the Xindex structure to use for object with lots of
// sections.
template<int size, bool big_endian>
Xindex*
Sized_relobj_file<size, big_endian>::do_initialize_xindex()
{
gold_assert(this->symtab_shndx_ != -1U);
Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_);
return xindex;
}
// Return whether SHDR has the right type and flags to be a GNU
// .eh_frame section.
template<int size, bool big_endian>
bool
Sized_relobj_file<size, big_endian>::check_eh_frame_flags(
const elfcpp::Shdr<size, big_endian>* shdr) const
{
elfcpp::Elf_Word sh_type = shdr->get_sh_type();
return ((sh_type == elfcpp::SHT_PROGBITS
|| sh_type == parameters->target().unwind_section_type())
&& (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
}
// Find the section header with the given name.
template<int size, bool big_endian>
const unsigned char*
Object::find_shdr(
const unsigned char* pshdrs,
const char* name,
const char* names,
section_size_type names_size,
const unsigned char* hdr) const
{
const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
const unsigned int shnum = this->shnum();
const unsigned char* hdr_end = pshdrs + shdr_size * shnum;
size_t sh_name = 0;
while (1)
{
if (hdr)
{
// We found HDR last time we were called, continue looking.
typename elfcpp::Shdr<size, big_endian> shdr(hdr);
sh_name = shdr.get_sh_name();
}
else
{
// Look for the next occurrence of NAME in NAMES.
// The fact that .shstrtab produced by current GNU tools is
// string merged means we shouldn't have both .not.foo and
// .foo in .shstrtab, and multiple .foo sections should all
// have the same sh_name. However, this is not guaranteed
// by the ELF spec and not all ELF object file producers may
// be so clever.
size_t len = strlen(name) + 1;
const char *p = sh_name ? names + sh_name + len : names;
p = reinterpret_cast<const char*>(memmem(p, names_size - (p - names),
name, len));
if (p == NULL)
return NULL;
sh_name = p - names;
hdr = pshdrs;
if (sh_name == 0)
return hdr;
}
hdr += shdr_size;
while (hdr < hdr_end)
{
typename elfcpp::Shdr<size, big_endian> shdr(hdr);
if (shdr.get_sh_name() == sh_name)
return hdr;
hdr += shdr_size;
}
hdr = NULL;
if (sh_name == 0)
return hdr;
}
}
// Return whether there is a GNU .eh_frame section, given the section
// headers and the section names.
template<int size, bool big_endian>
bool
Sized_relobj_file<size, big_endian>::find_eh_frame(
const unsigned char* pshdrs,
const char* names,
section_size_type names_size) const
{
const unsigned char* s = NULL;
while (1)
{
s = this->template find_shdr<size, big_endian>(pshdrs, ".eh_frame",
names, names_size, s);
if (s == NULL)
return false;
typename This::Shdr shdr(s);
if (this->check_eh_frame_flags(&shdr))
return true;
}
}
// Return TRUE if this is a section whose contents will be needed in the
// Add_symbols task. This function is only called for sections that have
// already passed the test in is_compressed_debug_section() and the debug
// section name prefix, ".debug"/".zdebug", has been skipped.
static bool
need_decompressed_section(const char* name)
{
if (*name++ != '_')
return false;
#ifdef ENABLE_THREADS
// Decompressing these sections now will help only if we're
// multithreaded.
if (parameters->options().threads())
{
// We will need .zdebug_str if this is not an incremental link
// (i.e., we are processing string merge sections) or if we need
// to build a gdb index.
if ((!parameters->incremental() || parameters->options().gdb_index())
&& strcmp(name, "str") == 0)
return true;
// We will need these other sections when building a gdb index.
if (parameters->options().gdb_index()
&& (strcmp(name, "info") == 0
|| strcmp(name, "types") == 0
|| strcmp(name, "pubnames") == 0
|| strcmp(name, "pubtypes") == 0
|| strcmp(name, "ranges") == 0
|| strcmp(name, "abbrev") == 0))
return true;
}
#endif
// Even when single-threaded, we will need .zdebug_str if this is
// not an incremental link and we are building a gdb index.
// Otherwise, we would decompress the section twice: once for
// string merge processing, and once for building the gdb index.
if (!parameters->incremental()
&& parameters->options().gdb_index()
&& strcmp(name, "str") == 0)
return true;
return false;
}
// Build a table for any compressed debug sections, mapping each section index
// to the uncompressed size and (if needed) the decompressed contents.
template<int size, bool big_endian>
Compressed_section_map*
build_compressed_section_map(
const unsigned char* pshdrs,
unsigned int shnum,
const char* names,
section_size_type names_size,
Object* obj,
bool decompress_if_needed)
{
Compressed_section_map* uncompressed_map = new Compressed_section_map();
const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
const unsigned char* p = pshdrs + shdr_size;
for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
{
typename elfcpp::Shdr<size, big_endian> shdr(p);
if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
&& (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
{
if (shdr.get_sh_name() >= names_size)
{
obj->error(_("bad section name offset for section %u: %lu"),
i, static_cast<unsigned long>(shdr.get_sh_name()));
continue;
}
const char* name = names + shdr.get_sh_name();
bool is_compressed = ((shdr.get_sh_flags()
& elfcpp::SHF_COMPRESSED) != 0);
bool is_zcompressed = (!is_compressed
&& is_compressed_debug_section(name));
if (is_zcompressed || is_compressed)
{
section_size_type len;
const unsigned char* contents =
obj->section_contents(i, &len, false);
uint64_t uncompressed_size;
Compressed_section_info info;
if (is_zcompressed)
{
// Skip over the ".zdebug" prefix.
name += 7;
uncompressed_size = get_uncompressed_size(contents, len);
info.addralign = shdr.get_sh_addralign();
}
else
{
// Skip over the ".debug" prefix.
name += 6;
elfcpp::Chdr<size, big_endian> chdr(contents);
uncompressed_size = chdr.get_ch_size();
info.addralign = chdr.get_ch_addralign();
}
info.size = convert_to_section_size_type(uncompressed_size);
info.flag = shdr.get_sh_flags();
info.contents = NULL;
if (uncompressed_size != -1ULL)
{
unsigned char* uncompressed_data = NULL;
if (decompress_if_needed && need_decompressed_section(name))
{
uncompressed_data = new unsigned char[uncompressed_size];
if (decompress_input_section(contents, len,
uncompressed_data,
uncompressed_size,
size, big_endian,
shdr.get_sh_flags()))
info.contents = uncompressed_data;
else
delete[] uncompressed_data;
}
(*uncompressed_map)[i] = info;
}
}
}
}
return uncompressed_map;
}
// Stash away info for a number of special sections.
// Return true if any of the sections found require local symbols to be read.
template<int size, bool big_endian>
bool
Sized_relobj_file<size, big_endian>::do_find_special_sections(
Read_symbols_data* sd)
{
const unsigned char* const pshdrs = sd->section_headers->data();
const unsigned char* namesu = sd->section_names->data();
const char* names = reinterpret_cast<const char*>(namesu);
if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
this->has_eh_frame_ = true;
Compressed_section_map* compressed_sections =
build_compressed_section_map<size, big_endian>(
pshdrs, this->shnum(), names, sd->section_names_size, this, true);
if (compressed_sections != NULL)
this->set_compressed_sections(compressed_sections);
return (this->has_eh_frame_
|| (!parameters->options().relocatable()
&& parameters->options().gdb_index()
&& (memmem(names, sd->section_names_size, "debug_info", 11) != NULL
|| memmem(names, sd->section_names_size,
"debug_types", 12) != NULL)));
}
// Read the sections and symbols from an object file.
template<int size, bool big_endian>
void
Sized_relobj_file<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
{
this->base_read_symbols(sd);
}
// Read the sections and symbols from an object file. This is common
// code for all target-specific overrides of do_read_symbols().
template<int size, bool big_endian>
void
Sized_relobj_file<size, big_endian>::base_read_symbols(Read_symbols_data* sd)
{
this->read_section_data(&this->elf_file_, sd);
const unsigned char* const pshdrs = sd->section_headers->data();
this->find_symtab(pshdrs);
bool need_local_symbols = this->do_find_special_sections(sd);
sd->symbols = NULL;
sd->symbols_size = 0;
sd->external_symbols_offset = 0;
sd->symbol_names = NULL;
sd->symbol_names_size = 0;
if (this->symtab_shndx_ == 0)
{
// No symbol table. Weird but legal.
return;
}
// Get the symbol table section header.
typename This::Shdr symtabshdr(pshdrs
+ this->symtab_shndx_ * This::shdr_size);
gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
// If this object has a .eh_frame section, or if building a .gdb_index
// section and there is debug info, we need all the symbols.
// Otherwise we only need the external symbols. While it would be
// simpler to just always read all the symbols, I've seen object
// files with well over 2000 local symbols, which for a 64-bit
// object file format is over 5 pages that we don't need to read
// now.
const int sym_size = This::sym_size;
const unsigned int loccount = symtabshdr.get_sh_info();
this->local_symbol_count_ = loccount;
this->local_values_.resize(loccount);
section_offset_type locsize = loccount * sym_size;
off_t dataoff = symtabshdr.get_sh_offset();
section_size_type datasize =
convert_to_section_size_type(symtabshdr.get_sh_size());
off_t extoff = dataoff + locsize;
section_size_type extsize = datasize - locsize;
off_t readoff = need_local_symbols ? dataoff : extoff;
section_size_type readsize = need_local_symbols ? datasize : extsize;
if (readsize == 0)
{
// No external symbols. Also weird but also legal.
return;
}
File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
// Read the section header for the symbol names.
unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
if (strtab_shndx >= this->shnum())
{
this->error(_("invalid symbol table name index: %u"), strtab_shndx);
return;
}
typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
{
this->error(_("symbol table name section has wrong type: %u"),
static_cast<unsigned int>(strtabshdr.get_sh_type()));
return;
}
// Read the symbol names.
File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
strtabshdr.get_sh_size(),
false, true);
sd->symbols = fvsymtab;
sd->symbols_size = readsize;
sd->external_symbols_offset = need_local_symbols ? locsize : 0;
sd->symbol_names = fvstrtab;
sd->symbol_names_size =
convert_to_section_size_type(strtabshdr.get_sh_size());
}
// Return the section index of symbol SYM. Set *VALUE to its value in
// the object file. Set *IS_ORDINARY if this is an ordinary section
// index, not a special code between SHN_LORESERVE and SHN_HIRESERVE.
// Note that for a symbol which is not defined in this object file,
// this will set *VALUE to 0 and return SHN_UNDEF; it will not return
// the final value of the symbol in the link.
template<int size, bool big_endian>
unsigned int
Sized_relobj_file<size, big_endian>::symbol_section_and_value(unsigned int sym,
Address* value,
bool* is_ordinary)
{
section_size_type symbols_size;
const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
&symbols_size,
false);
const size_t count = symbols_size / This::sym_size;
gold_assert(sym < count);
elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
*value = elfsym.get_st_value();
return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
}
// Return whether to include a section group in the link. LAYOUT is
// used to keep track of which section groups we have already seen.
// INDEX is the index of the section group and SHDR is the section
// header. If we do not want to include this group, we set bits in
// OMIT for each section which should be discarded.
template<int size, bool big_endian>
bool
Sized_relobj_file<size, big_endian>::include_section_group(
Symbol_table* symtab,
Layout* layout,
unsigned int index,
const char* name,
const unsigned char* shdrs,
const char* section_names,
section_size_type section_names_size,
std::vector<bool>* omit)
{
// Read the section contents.
typename This::Shdr shdr(shdrs + index * This::shdr_size);
const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
shdr.get_sh_size(), true, false);
const elfcpp::Elf_Word* pword =
reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
// The first word contains flags. We only care about COMDAT section
// groups. Other section groups are always included in the link
// just like ordinary sections.
elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
// Look up the group signature, which is the name of a symbol. ELF
// uses a symbol name because some group signatures are long, and
// the name is generally already in the symbol table, so it makes
// sense to put the long string just once in .strtab rather than in
// both .strtab and .shstrtab.
// Get the appropriate symbol table header (this will normally be
// the single SHT_SYMTAB section, but in principle it need not be).
const unsigned int link = this->adjust_shndx(shdr.get_sh_link());
typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
// Read the symbol table entry.
unsigned int symndx = shdr.get_sh_info();
if (symndx >= symshdr.get_sh_size() / This::sym_size)
{
this->error(_("section group %u info %u out of range"),
index, symndx);
return false;
}
off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size;