-
Notifications
You must be signed in to change notification settings - Fork 544
/
elf.c
14321 lines (12482 loc) · 400 KB
/
elf.c
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
/* ELF executable support for BFD.
Copyright (C) 1993-2024 Free Software Foundation, Inc.
This file is part of BFD, the Binary File Descriptor library.
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. */
/*
SECTION
ELF backends
BFD support for ELF formats is being worked on.
Currently, the best supported back ends are for sparc and i386
(running svr4 or Solaris 2).
Documentation of the internals of the support code still needs
to be written. The code is changing quickly enough that we
haven't bothered yet. */
/* For sparc64-cross-sparc32. */
#define _SYSCALL32
#include "sysdep.h"
#include <limits.h>
#include "bfd.h"
#include "bfdlink.h"
#include "libbfd.h"
#define ARCH_SIZE 0
#include "elf-bfd.h"
#include "libiberty.h"
#include "safe-ctype.h"
#include "elf-linux-core.h"
#ifdef CORE_HEADER
#include CORE_HEADER
#endif
static int elf_sort_sections (const void *, const void *);
static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
struct bfd_link_info *);
static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
file_ptr offset, size_t align);
/* Swap version information in and out. The version information is
currently size independent. If that ever changes, this code will
need to move into elfcode.h. */
/* Swap in a Verdef structure. */
void
_bfd_elf_swap_verdef_in (bfd *abfd,
const Elf_External_Verdef *src,
Elf_Internal_Verdef *dst)
{
dst->vd_version = H_GET_16 (abfd, src->vd_version);
dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
dst->vd_next = H_GET_32 (abfd, src->vd_next);
}
/* Swap out a Verdef structure. */
void
_bfd_elf_swap_verdef_out (bfd *abfd,
const Elf_Internal_Verdef *src,
Elf_External_Verdef *dst)
{
H_PUT_16 (abfd, src->vd_version, dst->vd_version);
H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
H_PUT_32 (abfd, src->vd_next, dst->vd_next);
}
/* Swap in a Verdaux structure. */
void
_bfd_elf_swap_verdaux_in (bfd *abfd,
const Elf_External_Verdaux *src,
Elf_Internal_Verdaux *dst)
{
dst->vda_name = H_GET_32 (abfd, src->vda_name);
dst->vda_next = H_GET_32 (abfd, src->vda_next);
}
/* Swap out a Verdaux structure. */
void
_bfd_elf_swap_verdaux_out (bfd *abfd,
const Elf_Internal_Verdaux *src,
Elf_External_Verdaux *dst)
{
H_PUT_32 (abfd, src->vda_name, dst->vda_name);
H_PUT_32 (abfd, src->vda_next, dst->vda_next);
}
/* Swap in a Verneed structure. */
void
_bfd_elf_swap_verneed_in (bfd *abfd,
const Elf_External_Verneed *src,
Elf_Internal_Verneed *dst)
{
dst->vn_version = H_GET_16 (abfd, src->vn_version);
dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
dst->vn_file = H_GET_32 (abfd, src->vn_file);
dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
dst->vn_next = H_GET_32 (abfd, src->vn_next);
}
/* Swap out a Verneed structure. */
void
_bfd_elf_swap_verneed_out (bfd *abfd,
const Elf_Internal_Verneed *src,
Elf_External_Verneed *dst)
{
H_PUT_16 (abfd, src->vn_version, dst->vn_version);
H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
H_PUT_32 (abfd, src->vn_file, dst->vn_file);
H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
H_PUT_32 (abfd, src->vn_next, dst->vn_next);
}
/* Swap in a Vernaux structure. */
void
_bfd_elf_swap_vernaux_in (bfd *abfd,
const Elf_External_Vernaux *src,
Elf_Internal_Vernaux *dst)
{
dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
dst->vna_other = H_GET_16 (abfd, src->vna_other);
dst->vna_name = H_GET_32 (abfd, src->vna_name);
dst->vna_next = H_GET_32 (abfd, src->vna_next);
}
/* Swap out a Vernaux structure. */
void
_bfd_elf_swap_vernaux_out (bfd *abfd,
const Elf_Internal_Vernaux *src,
Elf_External_Vernaux *dst)
{
H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
H_PUT_16 (abfd, src->vna_other, dst->vna_other);
H_PUT_32 (abfd, src->vna_name, dst->vna_name);
H_PUT_32 (abfd, src->vna_next, dst->vna_next);
}
/* Swap in a Versym structure. */
void
_bfd_elf_swap_versym_in (bfd *abfd,
const Elf_External_Versym *src,
Elf_Internal_Versym *dst)
{
dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
}
/* Swap out a Versym structure. */
void
_bfd_elf_swap_versym_out (bfd *abfd,
const Elf_Internal_Versym *src,
Elf_External_Versym *dst)
{
H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
}
/* Standard ELF hash function. Do not change this function; you will
cause invalid hash tables to be generated. */
unsigned long
bfd_elf_hash (const char *namearg)
{
uint32_t h = 0;
for (const unsigned char *name = (const unsigned char *) namearg;
*name; name++)
{
h = (h << 4) + *name;
h ^= (h >> 24) & 0xf0;
}
return h & 0x0fffffff;
}
/* DT_GNU_HASH hash function. Do not change this function; you will
cause invalid hash tables to be generated. */
unsigned long
bfd_elf_gnu_hash (const char *namearg)
{
uint32_t h = 5381;
for (const unsigned char *name = (const unsigned char *) namearg;
*name; name++)
h = (h << 5) + h + *name;
return h;
}
/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
bool
bfd_elf_allocate_object (bfd *abfd,
size_t object_size,
enum elf_target_id object_id)
{
BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
abfd->tdata.any = bfd_zalloc (abfd, object_size);
if (abfd->tdata.any == NULL)
return false;
elf_object_id (abfd) = object_id;
if (abfd->direction != read_direction)
{
struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
if (o == NULL)
return false;
elf_tdata (abfd)->o = o;
elf_program_header_size (abfd) = (bfd_size_type) -1;
}
return true;
}
bool
bfd_elf_make_object (bfd *abfd)
{
const struct elf_backend_data *bed = get_elf_backend_data (abfd);
return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
bed->target_id);
}
bool
bfd_elf_mkcorefile (bfd *abfd)
{
/* I think this can be done just like an object file. */
if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
return false;
elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
return elf_tdata (abfd)->core != NULL;
}
char *
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
{
Elf_Internal_Shdr **i_shdrp;
bfd_byte *shstrtab = NULL;
file_ptr offset;
bfd_size_type shstrtabsize;
i_shdrp = elf_elfsections (abfd);
if (i_shdrp == 0
|| shindex >= elf_numsections (abfd)
|| i_shdrp[shindex] == 0)
return NULL;
shstrtab = i_shdrp[shindex]->contents;
if (shstrtab == NULL)
{
/* No cached one, attempt to read, and cache what we read. */
offset = i_shdrp[shindex]->sh_offset;
shstrtabsize = i_shdrp[shindex]->sh_size;
if (shstrtabsize == 0
|| bfd_seek (abfd, offset, SEEK_SET) != 0
|| (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL)
{
/* Once we've failed to read it, make sure we don't keep
trying. Otherwise, we'll keep allocating space for
the string table over and over. */
i_shdrp[shindex]->sh_size = 0;
}
else if (shstrtab[shstrtabsize - 1] != 0)
{
/* It is an error if a string table isn't terminated. */
_bfd_error_handler
/* xgettext:c-format */
(_("%pB: string table [%u] is corrupt"), abfd, shindex);
shstrtab[shstrtabsize - 1] = 0;
}
i_shdrp[shindex]->contents = shstrtab;
}
return (char *) shstrtab;
}
char *
bfd_elf_string_from_elf_section (bfd *abfd,
unsigned int shindex,
unsigned int strindex)
{
Elf_Internal_Shdr *hdr;
if (strindex == 0)
return "";
if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
return NULL;
hdr = elf_elfsections (abfd)[shindex];
if (hdr->contents == NULL)
{
if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
{
/* PR 17512: file: f057ec89. */
/* xgettext:c-format */
_bfd_error_handler (_("%pB: attempt to load strings from"
" a non-string section (number %d)"),
abfd, shindex);
return NULL;
}
if (bfd_elf_get_str_section (abfd, shindex) == NULL)
return NULL;
}
else
{
/* PR 24273: The string section's contents may have already
been loaded elsewhere, eg because a corrupt file has the
string section index in the ELF header pointing at a group
section. So be paranoid, and test that the last byte of
the section is zero. */
if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
return NULL;
}
if (strindex >= hdr->sh_size)
{
unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
_bfd_error_handler
/* xgettext:c-format */
(_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
abfd, strindex, (uint64_t) hdr->sh_size,
(shindex == shstrndx && strindex == hdr->sh_name
? ".shstrtab"
: bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
return NULL;
}
return ((char *) hdr->contents) + strindex;
}
/* Read and convert symbols to internal format.
SYMCOUNT specifies the number of symbols to read, starting from
symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
are non-NULL, they are used to store the internal symbols, external
symbols, and symbol section index extensions, respectively.
Returns a pointer to the internal symbol buffer (malloced if necessary)
or NULL if there were no symbols or some kind of problem. */
Elf_Internal_Sym *
bfd_elf_get_elf_syms (bfd *ibfd,
Elf_Internal_Shdr *symtab_hdr,
size_t symcount,
size_t symoffset,
Elf_Internal_Sym *intsym_buf,
void *extsym_buf,
Elf_External_Sym_Shndx *extshndx_buf)
{
Elf_Internal_Shdr *shndx_hdr;
void *alloc_ext;
const bfd_byte *esym;
Elf_External_Sym_Shndx *alloc_extshndx;
Elf_External_Sym_Shndx *shndx;
Elf_Internal_Sym *alloc_intsym;
Elf_Internal_Sym *isym;
Elf_Internal_Sym *isymend;
const struct elf_backend_data *bed;
size_t extsym_size;
size_t amt;
file_ptr pos;
if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
abort ();
if (symcount == 0)
return intsym_buf;
if (elf_use_dt_symtab_p (ibfd))
{
/* Use dynamic symbol table. */
if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
{
bfd_set_error (bfd_error_invalid_operation);
return NULL;
}
return elf_tdata (ibfd)->dt_symtab + symoffset;
}
/* Normal syms might have section extension entries. */
shndx_hdr = NULL;
if (elf_symtab_shndx_list (ibfd) != NULL)
{
elf_section_list * entry;
Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
/* Find an index section that is linked to this symtab section. */
for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
{
/* PR 20063. */
if (entry->hdr.sh_link >= elf_numsections (ibfd))
continue;
if (sections[entry->hdr.sh_link] == symtab_hdr)
{
shndx_hdr = & entry->hdr;
break;
};
}
if (shndx_hdr == NULL)
{
if (symtab_hdr == &elf_symtab_hdr (ibfd))
/* Not really accurate, but this was how the old code used
to work. */
shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
/* Otherwise we do nothing. The assumption is that
the index table will not be needed. */
}
}
/* Read the symbols. */
alloc_ext = NULL;
alloc_extshndx = NULL;
alloc_intsym = NULL;
bed = get_elf_backend_data (ibfd);
extsym_size = bed->s->sizeof_sym;
if (_bfd_mul_overflow (symcount, extsym_size, &amt))
{
bfd_set_error (bfd_error_file_too_big);
return NULL;
}
pos = symtab_hdr->sh_offset + symoffset * extsym_size;
size_t alloc_ext_size = amt;
if (bfd_seek (ibfd, pos, SEEK_SET) != 0
|| !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
&alloc_ext, ibfd, false))
{
intsym_buf = NULL;
goto out2;
}
size_t alloc_extshndx_size = 0;
if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
extshndx_buf = NULL;
else
{
if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
{
bfd_set_error (bfd_error_file_too_big);
intsym_buf = NULL;
goto out1;
}
alloc_extshndx_size = amt;
pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
if (bfd_seek (ibfd, pos, SEEK_SET) != 0
|| !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
&alloc_extshndx_size,
(void **) &alloc_extshndx,
ibfd, false))
{
intsym_buf = NULL;
goto out1;
}
}
if (intsym_buf == NULL)
{
if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
{
bfd_set_error (bfd_error_file_too_big);
goto out1;
}
alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
intsym_buf = alloc_intsym;
if (intsym_buf == NULL)
goto out1;
}
/* Convert the symbols to internal form. */
isymend = intsym_buf + symcount;
for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
shndx = extshndx_buf;
isym < isymend;
esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
{
symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
/* xgettext:c-format */
_bfd_error_handler (_("%pB symbol number %lu references"
" nonexistent SHT_SYMTAB_SHNDX section"),
ibfd, (unsigned long) symoffset);
free (alloc_intsym);
intsym_buf = NULL;
goto out1;
}
out1:
_bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size);
out2:
_bfd_munmap_temporary (alloc_ext, alloc_ext_size);
return intsym_buf;
}
/* Look up a symbol name. */
static const char *
bfd_elf_sym_name_raw (bfd *abfd,
Elf_Internal_Shdr *symtab_hdr,
Elf_Internal_Sym *isym)
{
unsigned int iname = isym->st_name;
unsigned int shindex = symtab_hdr->sh_link;
if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
/* Check for a bogus st_shndx to avoid crashing. */
&& isym->st_shndx < elf_numsections (abfd))
{
iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
shindex = elf_elfheader (abfd)->e_shstrndx;
}
return bfd_elf_string_from_elf_section (abfd, shindex, iname);
}
const char *
bfd_elf_sym_name (bfd *abfd,
Elf_Internal_Shdr *symtab_hdr,
Elf_Internal_Sym *isym,
asection *sym_sec)
{
const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym);
if (name == NULL)
name = bfd_symbol_error_name;
else if (sym_sec && *name == '\0')
name = bfd_section_name (sym_sec);
return name;
}
/* Return the name of the group signature symbol. Why isn't the
signature just a string? */
static const char *
group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
{
Elf_Internal_Shdr *hdr;
unsigned char esym[sizeof (Elf64_External_Sym)];
Elf_External_Sym_Shndx eshndx;
Elf_Internal_Sym isym;
/* First we need to ensure the symbol table is available. Make sure
that it is a symbol table section. */
if (ghdr->sh_link >= elf_numsections (abfd))
return NULL;
hdr = elf_elfsections (abfd) [ghdr->sh_link];
if (hdr->sh_type != SHT_SYMTAB
|| ! bfd_section_from_shdr (abfd, ghdr->sh_link))
return NULL;
/* Go read the symbol. */
hdr = &elf_tdata (abfd)->symtab_hdr;
if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
&isym, esym, &eshndx) == NULL)
return NULL;
return bfd_elf_sym_name_raw (abfd, hdr, &isym);
}
static bool
is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize)
{
return (shdr->sh_size >= minsize
&& shdr->sh_entsize == GRP_ENTRY_SIZE
&& shdr->sh_size % GRP_ENTRY_SIZE == 0
&& shdr->bfd_section != NULL);
}
/* Set next_in_group, sec_group list pointers, and group names. */
static bool
process_sht_group_entries (bfd *abfd,
Elf_Internal_Shdr *ghdr, unsigned int gidx)
{
unsigned char *contents;
/* Read the raw contents. */
if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents))
{
_bfd_error_handler
/* xgettext:c-format */
(_("%pB: could not read contents of group [%u]"), abfd, gidx);
return false;
}
asection *last_elt = NULL;
const char *gname = NULL;
unsigned char *p = contents + ghdr->sh_size;
while (1)
{
unsigned int idx;
Elf_Internal_Shdr *shdr;
asection *elt;
p -= 4;
idx = H_GET_32 (abfd, p);
if (p == contents)
{
if ((idx & GRP_COMDAT) != 0)
ghdr->bfd_section->flags
|= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
break;
}
if (idx == 0
|| idx >= elf_numsections (abfd)
|| (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP
|| ((elt = shdr->bfd_section) != NULL
&& elf_sec_group (elt) != NULL
&& elf_sec_group (elt) != ghdr->bfd_section))
{
_bfd_error_handler
(_("%pB: invalid entry (%#x) in group [%u]"),
abfd, idx, gidx);
continue;
}
/* PR binutils/23199: According to the ELF gABI all sections in
a group must be marked with SHF_GROUP, but some tools
generate broken objects. Fix them up here. */
shdr->sh_flags |= SHF_GROUP;
if (elt == NULL)
{
if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
{
const char *name = bfd_elf_string_from_elf_section
(abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name);
_bfd_error_handler
/* xgettext:c-format */
(_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
abfd, shdr->sh_type, name, gidx);
}
continue;
}
/* Don't try to add a section to elf_next_in_group list twice. */
if (elf_sec_group (elt) != NULL)
continue;
if (last_elt == NULL)
{
/* Start a circular list with one element.
It will be in reverse order to match what gas does. */
elf_next_in_group (elt) = elt;
/* Point the group section to it. */
elf_next_in_group (ghdr->bfd_section) = elt;
gname = group_signature (abfd, ghdr);
if (gname == NULL)
{
free (contents);
return false;
}
}
else
{
elf_next_in_group (elt) = elf_next_in_group (last_elt);
elf_next_in_group (last_elt) = elt;
}
last_elt = elt;
elf_group_name (elt) = gname;
elf_sec_group (elt) = ghdr->bfd_section;
}
free (contents);
return true;
}
bool
_bfd_elf_setup_sections (bfd *abfd)
{
bool result = true;
/* Process SHF_LINK_ORDER. */
for (asection *s = abfd->sections; s != NULL; s = s->next)
{
Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
{
unsigned int elfsec = this_hdr->sh_link;
/* An sh_link value of 0 is now allowed. It indicates that linked
to section has already been discarded, but that the current
section has been retained for some other reason. This linking
section is still a candidate for later garbage collection
however. */
if (elfsec == 0)
{
elf_linked_to_section (s) = NULL;
}
else
{
asection *linksec = NULL;
if (elfsec < elf_numsections (abfd))
{
this_hdr = elf_elfsections (abfd)[elfsec];
linksec = this_hdr->bfd_section;
}
/* PR 1991, 2008:
Some strip/objcopy may leave an incorrect value in
sh_link. We don't want to proceed. */
if (linksec == NULL)
{
_bfd_error_handler
/* xgettext:c-format */
(_("%pB: sh_link [%d] in section `%pA' is incorrect"),
s->owner, elfsec, s);
result = false;
}
elf_linked_to_section (s) = linksec;
}
}
}
/* Process section groups. */
for (unsigned int i = 1; i < elf_numsections (abfd); i++)
{
Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
if (shdr && shdr->sh_type == SHT_GROUP)
{
if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE))
{
if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE
&& !process_sht_group_entries (abfd, shdr, i))
result = false;
}
else
{
/* PR binutils/18758: Beware of corrupt binaries with
invalid group data. */
_bfd_error_handler
/* xgettext:c-format */
(_("%pB: section group entry number %u is corrupt"), abfd, i);
result = false;
}
}
}
return result;
}
bool
bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
{
return elf_next_in_group (sec) != NULL;
}
const char *
bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
{
if (elf_sec_group (sec) != NULL)
return elf_group_name (sec);
return NULL;
}
/* Make a BFD section from an ELF section. We store a pointer to the
BFD section in the bfd_section field of the header. */
bool
_bfd_elf_make_section_from_shdr (bfd *abfd,
Elf_Internal_Shdr *hdr,
const char *name,
int shindex)
{
asection *newsect;
flagword flags;
const struct elf_backend_data *bed;
unsigned int opb = bfd_octets_per_byte (abfd, NULL);
if (hdr->bfd_section != NULL)
return true;
newsect = bfd_make_section_anyway (abfd, name);
if (newsect == NULL)
return false;
hdr->bfd_section = newsect;
elf_section_data (newsect)->this_hdr = *hdr;
elf_section_data (newsect)->this_idx = shindex;
/* Always use the real type/flags. */
elf_section_type (newsect) = hdr->sh_type;
elf_section_flags (newsect) = hdr->sh_flags;
newsect->filepos = hdr->sh_offset;
flags = SEC_NO_FLAGS;
if (hdr->sh_type != SHT_NOBITS)
flags |= SEC_HAS_CONTENTS;
if (hdr->sh_type == SHT_GROUP)
flags |= SEC_GROUP;
if ((hdr->sh_flags & SHF_ALLOC) != 0)
{
flags |= SEC_ALLOC;
if (hdr->sh_type != SHT_NOBITS)
flags |= SEC_LOAD;
}
if ((hdr->sh_flags & SHF_WRITE) == 0)
flags |= SEC_READONLY;
if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
flags |= SEC_CODE;
else if ((flags & SEC_LOAD) != 0)
flags |= SEC_DATA;
if ((hdr->sh_flags & SHF_MERGE) != 0)
{
flags |= SEC_MERGE;
newsect->entsize = hdr->sh_entsize;
}
if ((hdr->sh_flags & SHF_STRINGS) != 0)
flags |= SEC_STRINGS;
if ((hdr->sh_flags & SHF_TLS) != 0)
flags |= SEC_THREAD_LOCAL;
if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
flags |= SEC_EXCLUDE;
switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
{
/* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
but binutils as of 2019-07-23 did not set the EI_OSABI header
byte. */
case ELFOSABI_GNU:
case ELFOSABI_FREEBSD:
if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
/* Fall through */
case ELFOSABI_NONE:
if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
break;
}
if ((flags & SEC_ALLOC) == 0)
{
/* The debugging sections appear to be recognized only by name,
not any sort of flag. Their SEC_ALLOC bits are cleared. */
if (name [0] == '.')
{
if (startswith (name, ".debug")
|| startswith (name, ".gnu.debuglto_.debug_")
|| startswith (name, ".gnu.linkonce.wi.")
|| startswith (name, ".zdebug"))
flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
|| startswith (name, ".note.gnu"))
{
flags |= SEC_ELF_OCTETS;
opb = 1;
}
else if (startswith (name, ".line")
|| startswith (name, ".stab")
|| strcmp (name, ".gdb_index") == 0)
flags |= SEC_DEBUGGING;
}
}
if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
|| !bfd_set_section_size (newsect, hdr->sh_size)
|| !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
& -hdr->sh_addralign)))
return false;
/* As a GNU extension, if the name begins with .gnu.linkonce, we
only link a single copy of the section. This is used to support
g++. g++ will emit each template expansion in its own section.
The symbols will be defined as weak, so that multiple definitions
are permitted. The GNU linker extension is to actually discard
all but one of the sections. */
if (startswith (name, ".gnu.linkonce")
&& elf_next_in_group (newsect) == NULL)
flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
if (!bfd_set_section_flags (newsect, flags))
return false;
bed = get_elf_backend_data (abfd);
if (bed->elf_backend_section_flags)
if (!bed->elf_backend_section_flags (hdr))
return false;
/* We do not parse the PT_NOTE segments as we are interested even in the
separate debug info files which may have the segments offsets corrupted.
PT_NOTEs from the core files are currently not parsed using BFD. */
if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
{
bfd_byte *contents;
if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
return false;
elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
hdr->sh_offset, hdr->sh_addralign);
_bfd_elf_munmap_section_contents (newsect, contents);
}
if ((newsect->flags & SEC_ALLOC) != 0)
{
Elf_Internal_Phdr *phdr;
unsigned int i, nload;
/* Some ELF linkers produce binaries with all the program header
p_paddr fields zero. If we have such a binary with more than
one PT_LOAD header, then leave the section lma equal to vma
so that we don't create sections with overlapping lma. */
phdr = elf_tdata (abfd)->phdr;
for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
if (phdr->p_paddr != 0)
break;
else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
++nload;
if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
return true;
phdr = elf_tdata (abfd)->phdr;
for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
{
if (((phdr->p_type == PT_LOAD
&& (hdr->sh_flags & SHF_TLS) == 0)
|| phdr->p_type == PT_TLS)
&& ELF_SECTION_IN_SEGMENT (hdr, phdr))
{
if ((newsect->flags & SEC_LOAD) == 0)
newsect->lma = (phdr->p_paddr
+ hdr->sh_addr - phdr->p_vaddr) / opb;
else
/* We used to use the same adjustment for SEC_LOAD
sections, but that doesn't work if the segment
is packed with code from multiple VMAs.
Instead we calculate the section LMA based on
the segment LMA. It is assumed that the
segment will contain sections with contiguous
LMAs, even if the VMAs are not. */
newsect->lma = (phdr->p_paddr
+ hdr->sh_offset - phdr->p_offset) / opb;
/* With contiguous segments, we can't tell from file
offsets whether a section with zero size should
be placed at the end of one segment or the
beginning of the next. Decide based on vaddr. */
if (hdr->sh_addr >= phdr->p_vaddr
&& (hdr->sh_addr + hdr->sh_size
<= phdr->p_vaddr + phdr->p_memsz))
break;
}
}
}
/* Compress/decompress DWARF debug sections with names: .debug_*,
.zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */
if ((newsect->flags & SEC_DEBUGGING) != 0
&& (newsect->flags & SEC_HAS_CONTENTS) != 0
&& (newsect->flags & SEC_ELF_OCTETS) != 0)
{
enum { nothing, compress, decompress } action = nothing;
int compression_header_size;
bfd_size_type uncompressed_size;
unsigned int uncompressed_align_power;
enum compression_type ch_type = ch_none;
bool compressed
= bfd_is_section_compressed_info (abfd, newsect,
&compression_header_size,
&uncompressed_size,