This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
elfcode.h
1605 lines (1391 loc) · 49.1 KB
/
elfcode.h
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 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
2001, 2002 Free Software Foundation, Inc.
Written by Fred Fish @ Cygnus Support, from information published
in "UNIX System V Release 4, Programmers Guide: ANSI C and
Programming Support Tools". Sufficient support for gdb.
Rewritten by Mark Eichin @ Cygnus Support, from information
published in "System V Application Binary Interface", chapters 4
and 5, as well as the various "Processor Supplement" documents
derived from it. Added support for assembler and other object file
utilities. Further work done by Ken Raeburn (Cygnus Support), Michael
Meissner (Open Software Foundation), and Peter Hoogenboom (University
of Utah) to finish and extend this.
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* Problems and other issues to resolve.
(1) BFD expects there to be some fixed number of "sections" in
the object file. I.E. there is a "section_count" variable in the
bfd structure which contains the number of sections. However, ELF
supports multiple "views" of a file. In particular, with current
implementations, executable files typically have two tables, a
program header table and a section header table, both of which
partition the executable.
In ELF-speak, the "linking view" of the file uses the section header
table to access "sections" within the file, and the "execution view"
uses the program header table to access "segments" within the file.
"Segments" typically may contain all the data from one or more
"sections".
Note that the section header table is optional in ELF executables,
but it is this information that is most useful to gdb. If the
section header table is missing, then gdb should probably try
to make do with the program header table. (FIXME)
(2) The code in this file is compiled twice, once in 32-bit mode and
once in 64-bit mode. More of it should be made size-independent
and moved into elf.c.
(3) ELF section symbols are handled rather sloppily now. This should
be cleaned up, and ELF section symbols reconciled with BFD section
symbols.
(4) We need a published spec for 64-bit ELF. We've got some stuff here
that we're using for SPARC V9 64-bit chips, but don't assume that
it's cast in stone.
*/
#include "bfd.h"
#include "sysdep.h"
#include "libiberty.h"
#include "bfdlink.h"
#include "libbfd.h"
#include "elf-bfd.h"
/* Renaming structures, typedefs, macros and functions to be size-specific. */
#define Elf_External_Ehdr NAME(Elf,External_Ehdr)
#define Elf_External_Sym NAME(Elf,External_Sym)
#define Elf_External_Shdr NAME(Elf,External_Shdr)
#define Elf_External_Phdr NAME(Elf,External_Phdr)
#define Elf_External_Rel NAME(Elf,External_Rel)
#define Elf_External_Rela NAME(Elf,External_Rela)
#define Elf_External_Dyn NAME(Elf,External_Dyn)
#define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command)
#define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal)
#define elf_core_file_matches_executable_p \
NAME(bfd_elf,core_file_matches_executable_p)
#define elf_object_p NAME(bfd_elf,object_p)
#define elf_core_file_p NAME(bfd_elf,core_file_p)
#define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound)
#define elf_get_dynamic_symtab_upper_bound \
NAME(bfd_elf,get_dynamic_symtab_upper_bound)
#define elf_swap_reloc_in NAME(bfd_elf,swap_reloc_in)
#define elf_swap_reloca_in NAME(bfd_elf,swap_reloca_in)
#define elf_swap_reloc_out NAME(bfd_elf,swap_reloc_out)
#define elf_swap_reloca_out NAME(bfd_elf,swap_reloca_out)
#define elf_swap_symbol_in NAME(bfd_elf,swap_symbol_in)
#define elf_swap_symbol_out NAME(bfd_elf,swap_symbol_out)
#define elf_swap_phdr_in NAME(bfd_elf,swap_phdr_in)
#define elf_swap_phdr_out NAME(bfd_elf,swap_phdr_out)
#define elf_swap_dyn_in NAME(bfd_elf,swap_dyn_in)
#define elf_swap_dyn_out NAME(bfd_elf,swap_dyn_out)
#define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound)
#define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc)
#define elf_slurp_symbol_table NAME(bfd_elf,slurp_symbol_table)
#define elf_get_symtab NAME(bfd_elf,get_symtab)
#define elf_canonicalize_dynamic_symtab \
NAME(bfd_elf,canonicalize_dynamic_symtab)
#define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol)
#define elf_get_symbol_info NAME(bfd_elf,get_symbol_info)
#define elf_get_lineno NAME(bfd_elf,get_lineno)
#define elf_set_arch_mach NAME(bfd_elf,set_arch_mach)
#define elf_find_nearest_line NAME(bfd_elf,find_nearest_line)
#define elf_sizeof_headers NAME(bfd_elf,sizeof_headers)
#define elf_set_section_contents NAME(bfd_elf,set_section_contents)
#define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto)
#define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel)
#define elf_find_section NAME(bfd_elf,find_section)
#define elf_bfd_link_add_symbols NAME(bfd_elf,bfd_link_add_symbols)
#define elf_add_dynamic_entry NAME(bfd_elf,add_dynamic_entry)
#define elf_write_shdrs_and_ehdr NAME(bfd_elf,write_shdrs_and_ehdr)
#define elf_write_out_phdrs NAME(bfd_elf,write_out_phdrs)
#define elf_write_relocs NAME(bfd_elf,write_relocs)
#define elf_slurp_reloc_table NAME(bfd_elf,slurp_reloc_table)
#define elf_link_create_dynamic_sections \
NAME(bfd_elf,link_create_dynamic_sections)
#define elf_bfd_discard_info NAME(bfd_elf,discard_info)
#define elf_reloc_symbol_deleted_p NAME(_bfd_elf,reloc_symbol_deleted_p)
#define elf_link_record_dynamic_symbol _bfd_elf_link_record_dynamic_symbol
#define elf_bfd_final_link NAME(bfd_elf,bfd_final_link)
#define elf_create_pointer_linker_section NAME(bfd_elf,create_pointer_linker_section)
#define elf_finish_pointer_linker_section NAME(bfd_elf,finish_pointer_linker_section)
#define elf_gc_sections NAME(_bfd_elf,gc_sections)
#define elf_gc_common_finalize_got_offsets \
NAME(_bfd_elf,gc_common_finalize_got_offsets)
#define elf_gc_common_final_link NAME(_bfd_elf,gc_common_final_link)
#define elf_gc_record_vtinherit NAME(_bfd_elf,gc_record_vtinherit)
#define elf_gc_record_vtentry NAME(_bfd_elf,gc_record_vtentry)
#define elf_link_record_local_dynamic_symbol \
NAME(_bfd_elf,link_record_local_dynamic_symbol)
#if ARCH_SIZE == 64
#define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
#define ELF_R_SYM(X) ELF64_R_SYM(X)
#define ELF_R_TYPE(X) ELF64_R_TYPE(X)
#define ELFCLASS ELFCLASS64
#define FILE_ALIGN 8
#define LOG_FILE_ALIGN 3
#endif
#if ARCH_SIZE == 32
#define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
#define ELF_R_SYM(X) ELF32_R_SYM(X)
#define ELF_R_TYPE(X) ELF32_R_TYPE(X)
#define ELFCLASS ELFCLASS32
#define FILE_ALIGN 4
#define LOG_FILE_ALIGN 2
#endif
/* Static functions */
static void elf_swap_ehdr_in
PARAMS ((bfd *, const Elf_External_Ehdr *, Elf_Internal_Ehdr *));
static void elf_swap_ehdr_out
PARAMS ((bfd *, const Elf_Internal_Ehdr *, Elf_External_Ehdr *));
static void elf_swap_shdr_in
PARAMS ((bfd *, const Elf_External_Shdr *, Elf_Internal_Shdr *));
static void elf_swap_shdr_out
PARAMS ((bfd *, const Elf_Internal_Shdr *, Elf_External_Shdr *));
#define elf_stringtab_init _bfd_elf_stringtab_init
#define section_from_elf_index bfd_section_from_elf_index
static bfd_boolean elf_slurp_reloc_table_from_section
PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, bfd_size_type,
arelent *, asymbol **, bfd_boolean));
static bfd_boolean elf_file_p PARAMS ((Elf_External_Ehdr *));
#ifdef DEBUG
static void elf_debug_section PARAMS ((int, Elf_Internal_Shdr *));
static void elf_debug_file PARAMS ((Elf_Internal_Ehdr *));
static char *elf_symbol_flags PARAMS ((flagword));
#endif
/* Structure swapping routines */
/* Should perhaps use put_offset, put_word, etc. For now, the two versions
can be handled by explicitly specifying 32 bits or "the long type". */
#if ARCH_SIZE == 64
#define H_PUT_WORD H_PUT_64
#define H_PUT_SIGNED_WORD H_PUT_S64
#define H_GET_WORD H_GET_64
#define H_GET_SIGNED_WORD H_GET_S64
#endif
#if ARCH_SIZE == 32
#define H_PUT_WORD H_PUT_32
#define H_PUT_SIGNED_WORD H_PUT_S32
#define H_GET_WORD H_GET_32
#define H_GET_SIGNED_WORD H_GET_S32
#endif
/* Translate an ELF symbol in external format into an ELF symbol in internal
format. */
void
elf_swap_symbol_in (abfd, psrc, pshn, dst)
bfd *abfd;
const PTR psrc;
const PTR pshn;
Elf_Internal_Sym *dst;
{
const Elf_External_Sym *src = (const Elf_External_Sym *) psrc;
const Elf_External_Sym_Shndx *shndx = (const Elf_External_Sym_Shndx *) pshn;
int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
dst->st_name = H_GET_32 (abfd, src->st_name);
if (signed_vma)
dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value);
else
dst->st_value = H_GET_WORD (abfd, src->st_value);
dst->st_size = H_GET_WORD (abfd, src->st_size);
dst->st_info = H_GET_8 (abfd, src->st_info);
dst->st_other = H_GET_8 (abfd, src->st_other);
dst->st_shndx = H_GET_16 (abfd, src->st_shndx);
if (dst->st_shndx == SHN_XINDEX)
{
if (shndx == NULL)
abort ();
dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx);
}
}
/* Translate an ELF symbol in internal format into an ELF symbol in external
format. */
void
elf_swap_symbol_out (abfd, src, cdst, shndx)
bfd *abfd;
const Elf_Internal_Sym *src;
PTR cdst;
PTR shndx;
{
unsigned int tmp;
Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
H_PUT_32 (abfd, src->st_name, dst->st_name);
H_PUT_WORD (abfd, src->st_value, dst->st_value);
H_PUT_WORD (abfd, src->st_size, dst->st_size);
H_PUT_8 (abfd, src->st_info, dst->st_info);
H_PUT_8 (abfd, src->st_other, dst->st_other);
tmp = src->st_shndx;
if (tmp > SHN_HIRESERVE)
{
if (shndx == NULL)
abort ();
H_PUT_32 (abfd, tmp, shndx);
tmp = SHN_XINDEX;
}
H_PUT_16 (abfd, tmp, dst->st_shndx);
}
/* Translate an ELF file header in external format into an ELF file header in
internal format. */
static void
elf_swap_ehdr_in (abfd, src, dst)
bfd *abfd;
const Elf_External_Ehdr *src;
Elf_Internal_Ehdr *dst;
{
int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
dst->e_type = H_GET_16 (abfd, src->e_type);
dst->e_machine = H_GET_16 (abfd, src->e_machine);
dst->e_version = H_GET_32 (abfd, src->e_version);
if (signed_vma)
dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry);
else
dst->e_entry = H_GET_WORD (abfd, src->e_entry);
dst->e_phoff = H_GET_WORD (abfd, src->e_phoff);
dst->e_shoff = H_GET_WORD (abfd, src->e_shoff);
dst->e_flags = H_GET_32 (abfd, src->e_flags);
dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize);
dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize);
dst->e_phnum = H_GET_16 (abfd, src->e_phnum);
dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize);
dst->e_shnum = H_GET_16 (abfd, src->e_shnum);
dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx);
}
/* Translate an ELF file header in internal format into an ELF file header in
external format. */
static void
elf_swap_ehdr_out (abfd, src, dst)
bfd *abfd;
const Elf_Internal_Ehdr *src;
Elf_External_Ehdr *dst;
{
unsigned int tmp;
int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
/* note that all elements of dst are *arrays of unsigned char* already... */
H_PUT_16 (abfd, src->e_type, dst->e_type);
H_PUT_16 (abfd, src->e_machine, dst->e_machine);
H_PUT_32 (abfd, src->e_version, dst->e_version);
if (signed_vma)
H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
else
H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
H_PUT_32 (abfd, src->e_flags, dst->e_flags);
H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
H_PUT_16 (abfd, src->e_phnum, dst->e_phnum);
H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
tmp = src->e_shnum;
if (tmp >= SHN_LORESERVE)
tmp = SHN_UNDEF;
H_PUT_16 (abfd, tmp, dst->e_shnum);
tmp = src->e_shstrndx;
if (tmp >= SHN_LORESERVE)
tmp = SHN_XINDEX;
H_PUT_16 (abfd, tmp, dst->e_shstrndx);
}
/* Translate an ELF section header table entry in external format into an
ELF section header table entry in internal format. */
static void
elf_swap_shdr_in (abfd, src, dst)
bfd *abfd;
const Elf_External_Shdr *src;
Elf_Internal_Shdr *dst;
{
int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
dst->sh_name = H_GET_32 (abfd, src->sh_name);
dst->sh_type = H_GET_32 (abfd, src->sh_type);
dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
if (signed_vma)
dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
else
dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
dst->sh_size = H_GET_WORD (abfd, src->sh_size);
dst->sh_link = H_GET_32 (abfd, src->sh_link);
dst->sh_info = H_GET_32 (abfd, src->sh_info);
dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
dst->bfd_section = NULL;
dst->contents = NULL;
}
/* Translate an ELF section header table entry in internal format into an
ELF section header table entry in external format. */
static void
elf_swap_shdr_out (abfd, src, dst)
bfd *abfd;
const Elf_Internal_Shdr *src;
Elf_External_Shdr *dst;
{
/* note that all elements of dst are *arrays of unsigned char* already... */
H_PUT_32 (abfd, src->sh_name, dst->sh_name);
H_PUT_32 (abfd, src->sh_type, dst->sh_type);
H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
H_PUT_32 (abfd, src->sh_link, dst->sh_link);
H_PUT_32 (abfd, src->sh_info, dst->sh_info);
H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
}
/* Translate an ELF program header table entry in external format into an
ELF program header table entry in internal format. */
void
elf_swap_phdr_in (abfd, src, dst)
bfd *abfd;
const Elf_External_Phdr *src;
Elf_Internal_Phdr *dst;
{
int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
dst->p_type = H_GET_32 (abfd, src->p_type);
dst->p_flags = H_GET_32 (abfd, src->p_flags);
dst->p_offset = H_GET_WORD (abfd, src->p_offset);
if (signed_vma)
{
dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
}
else
{
dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
}
dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
dst->p_align = H_GET_WORD (abfd, src->p_align);
}
void
elf_swap_phdr_out (abfd, src, dst)
bfd *abfd;
const Elf_Internal_Phdr *src;
Elf_External_Phdr *dst;
{
/* note that all elements of dst are *arrays of unsigned char* already... */
H_PUT_32 (abfd, src->p_type, dst->p_type);
H_PUT_WORD (abfd, src->p_offset, dst->p_offset);
H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr);
H_PUT_WORD (abfd, src->p_paddr, dst->p_paddr);
H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz);
H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz);
H_PUT_32 (abfd, src->p_flags, dst->p_flags);
H_PUT_WORD (abfd, src->p_align, dst->p_align);
}
/* Translate an ELF reloc from external format to internal format. */
void
elf_swap_reloc_in (abfd, s, dst)
bfd *abfd;
const bfd_byte *s;
Elf_Internal_Rela *dst;
{
const Elf_External_Rel *src = (const Elf_External_Rel *) s;
dst->r_offset = H_GET_WORD (abfd, src->r_offset);
dst->r_info = H_GET_WORD (abfd, src->r_info);
dst->r_addend = 0;
}
void
elf_swap_reloca_in (abfd, s, dst)
bfd *abfd;
const bfd_byte *s;
Elf_Internal_Rela *dst;
{
const Elf_External_Rela *src = (const Elf_External_Rela *) s;
dst->r_offset = H_GET_WORD (abfd, src->r_offset);
dst->r_info = H_GET_WORD (abfd, src->r_info);
dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
}
/* Translate an ELF reloc from internal format to external format. */
void
elf_swap_reloc_out (abfd, src, d)
bfd *abfd;
const Elf_Internal_Rela *src;
bfd_byte *d;
{
Elf_External_Rel *dst = (Elf_External_Rel *) d;
H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
H_PUT_WORD (abfd, src->r_info, dst->r_info);
}
void
elf_swap_reloca_out (abfd, src, d)
bfd *abfd;
const Elf_Internal_Rela *src;
bfd_byte *d;
{
Elf_External_Rela *dst = (Elf_External_Rela *) d;
H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
H_PUT_WORD (abfd, src->r_info, dst->r_info);
H_PUT_SIGNED_WORD (abfd, src->r_addend, dst->r_addend);
}
INLINE void
elf_swap_dyn_in (abfd, p, dst)
bfd *abfd;
const PTR p;
Elf_Internal_Dyn *dst;
{
const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
dst->d_tag = H_GET_WORD (abfd, src->d_tag);
dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
}
INLINE void
elf_swap_dyn_out (abfd, src, p)
bfd *abfd;
const Elf_Internal_Dyn *src;
PTR p;
{
Elf_External_Dyn *dst = (Elf_External_Dyn *) p;
H_PUT_WORD (abfd, src->d_tag, dst->d_tag);
H_PUT_WORD (abfd, src->d_un.d_val, dst->d_un.d_val);
}
/* ELF .o/exec file reading */
/* Begin processing a given object.
First we validate the file by reading in the ELF header and checking
the magic number. */
static INLINE bfd_boolean
elf_file_p (x_ehdrp)
Elf_External_Ehdr *x_ehdrp;
{
return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
&& (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
&& (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
&& (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
}
/* Check to see if the file associated with ABFD matches the target vector
that ABFD points to.
Note that we may be called several times with the same ABFD, but different
target vectors, most of which will not match. We have to avoid leaving
any side effects in ABFD, or any data it points to (like tdata), if the
file does not match the target vector. */
const bfd_target *
elf_object_p (abfd)
bfd *abfd;
{
Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
Elf_External_Shdr x_shdr; /* Section header table entry, external form */
Elf_Internal_Shdr i_shdr;
Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
unsigned int shindex;
char *shstrtab; /* Internal copy of section header stringtab */
struct elf_backend_data *ebd;
struct bfd_preserve preserve;
asection *s;
bfd_size_type amt;
preserve.marker = NULL;
/* Read in the ELF header in external format. */
if (bfd_bread ((PTR) & x_ehdr, (bfd_size_type) sizeof (x_ehdr), abfd)
!= sizeof (x_ehdr))
{
if (bfd_get_error () != bfd_error_system_call)
goto got_wrong_format_error;
else
goto got_no_match;
}
/* Now check to see if we have a valid ELF file, and one that BFD can
make use of. The magic number must match, the address size ('class')
and byte-swapping must match our XVEC entry, and it must have a
section header table (FIXME: See comments re sections at top of this
file). */
if (! elf_file_p (&x_ehdr)
|| x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
|| x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
goto got_wrong_format_error;
/* Check that file's byte order matches xvec's */
switch (x_ehdr.e_ident[EI_DATA])
{
case ELFDATA2MSB: /* Big-endian */
if (! bfd_header_big_endian (abfd))
goto got_wrong_format_error;
break;
case ELFDATA2LSB: /* Little-endian */
if (! bfd_header_little_endian (abfd))
goto got_wrong_format_error;
break;
case ELFDATANONE: /* No data encoding specified */
default: /* Unknown data encoding specified */
goto got_wrong_format_error;
}
/* Allocate an instance of the elf_obj_tdata structure and hook it up to
the tdata pointer in the bfd. */
amt = sizeof (struct elf_obj_tdata);
preserve.marker = bfd_zalloc (abfd, amt);
if (preserve.marker == NULL)
goto got_no_match;
if (!bfd_preserve_save (abfd, &preserve))
goto got_no_match;
elf_tdata (abfd) = preserve.marker;
/* Now that we know the byte order, swap in the rest of the header */
i_ehdrp = elf_elfheader (abfd);
elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
#if DEBUG & 1
elf_debug_file (i_ehdrp);
#endif
/* Reject ET_CORE (header indicates core file, not object file) */
if (i_ehdrp->e_type == ET_CORE)
goto got_wrong_format_error;
/* If this is a relocatable file and there is no section header
table, then we're hosed. */
if (i_ehdrp->e_shoff == 0 && i_ehdrp->e_type == ET_REL)
goto got_wrong_format_error;
/* As a simple sanity check, verify that the what BFD thinks is the
size of each section header table entry actually matches the size
recorded in the file, but only if there are any sections. */
if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
goto got_wrong_format_error;
/* Further sanity check. */
if (i_ehdrp->e_shoff == 0 && i_ehdrp->e_shnum != 0)
goto got_wrong_format_error;
ebd = get_elf_backend_data (abfd);
/* Check that the ELF e_machine field matches what this particular
BFD format expects. */
if (ebd->elf_machine_code != i_ehdrp->e_machine
&& (ebd->elf_machine_alt1 == 0
|| i_ehdrp->e_machine != ebd->elf_machine_alt1)
&& (ebd->elf_machine_alt2 == 0
|| i_ehdrp->e_machine != ebd->elf_machine_alt2))
{
const bfd_target * const *target_ptr;
if (ebd->elf_machine_code != EM_NONE)
goto got_wrong_format_error;
/* This is the generic ELF target. Let it match any ELF target
for which we do not have a specific backend. */
for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
{
struct elf_backend_data *back;
if ((*target_ptr)->flavour != bfd_target_elf_flavour)
continue;
back = (struct elf_backend_data *) (*target_ptr)->backend_data;
if (back->elf_machine_code == i_ehdrp->e_machine
|| (back->elf_machine_alt1 != 0
&& back->elf_machine_alt1 == i_ehdrp->e_machine)
|| (back->elf_machine_alt2 != 0
&& back->elf_machine_alt2 == i_ehdrp->e_machine))
{
/* target_ptr is an ELF backend which matches this
object file, so reject the generic ELF target. */
goto got_wrong_format_error;
}
}
}
if (i_ehdrp->e_type == ET_EXEC)
abfd->flags |= EXEC_P;
else if (i_ehdrp->e_type == ET_DYN)
abfd->flags |= DYNAMIC;
if (i_ehdrp->e_phnum > 0)
abfd->flags |= D_PAGED;
if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
{
/* It's OK if this fails for the generic target. */
if (ebd->elf_machine_code != EM_NONE)
goto got_no_match;
}
/* Remember the entry point specified in the ELF file header. */
bfd_set_start_address (abfd, i_ehdrp->e_entry);
if (i_ehdrp->e_shoff != 0)
{
/* Seek to the section header table in the file. */
if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0)
goto got_no_match;
/* Read the first section header at index 0, and convert to internal
form. */
if (bfd_bread ((PTR) & x_shdr, (bfd_size_type) sizeof x_shdr, abfd)
!= sizeof (x_shdr))
goto got_no_match;
elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
/* If the section count is zero, the actual count is in the first
section header. */
if (i_ehdrp->e_shnum == SHN_UNDEF)
i_ehdrp->e_shnum = i_shdr.sh_size;
/* And similarly for the string table index. */
if (i_ehdrp->e_shstrndx == SHN_XINDEX)
i_ehdrp->e_shstrndx = i_shdr.sh_link;
}
/* Allocate space for a copy of the section header table in
internal form. */
if (i_ehdrp->e_shnum != 0)
{
Elf_Internal_Shdr *shdrp;
unsigned int num_sec;
amt = sizeof (*i_shdrp) * i_ehdrp->e_shnum;
i_shdrp = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
if (!i_shdrp)
goto got_no_match;
num_sec = i_ehdrp->e_shnum;
if (num_sec > SHN_LORESERVE)
num_sec += SHN_HIRESERVE + 1 - SHN_LORESERVE;
elf_numsections (abfd) = num_sec;
amt = sizeof (i_shdrp) * num_sec;
elf_elfsections (abfd) = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
if (!elf_elfsections (abfd))
goto got_no_match;
memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
shdrp = i_shdrp;
shindex = 0;
if (num_sec > SHN_LORESERVE)
{
for ( ; shindex < SHN_LORESERVE; shindex++)
elf_elfsections (abfd)[shindex] = shdrp++;
for ( ; shindex < SHN_HIRESERVE + 1; shindex++)
elf_elfsections (abfd)[shindex] = i_shdrp;
}
for ( ; shindex < num_sec; shindex++)
elf_elfsections (abfd)[shindex] = shdrp++;
/* Read in the rest of the section header table and convert it
to internal form. */
for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
{
if (bfd_bread ((PTR) & x_shdr, (bfd_size_type) sizeof x_shdr, abfd)
!= sizeof (x_shdr))
goto got_no_match;
elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
/* If the section is loaded, but not page aligned, clear
D_PAGED. */
if (i_shdrp[shindex].sh_size != 0
&& (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
&& i_shdrp[shindex].sh_type != SHT_NOBITS
&& (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
% ebd->maxpagesize)
!= 0))
abfd->flags &= ~D_PAGED;
}
}
if (i_ehdrp->e_shstrndx && i_ehdrp->e_shoff)
{
if (! bfd_section_from_shdr (abfd, i_ehdrp->e_shstrndx))
goto got_no_match;
}
/* Read in the program headers. */
if (i_ehdrp->e_phnum == 0)
elf_tdata (abfd)->phdr = NULL;
else
{
Elf_Internal_Phdr *i_phdr;
unsigned int i;
amt = i_ehdrp->e_phnum * sizeof (Elf_Internal_Phdr);
elf_tdata (abfd)->phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
if (elf_tdata (abfd)->phdr == NULL)
goto got_no_match;
if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_phoff, SEEK_SET) != 0)
goto got_no_match;
i_phdr = elf_tdata (abfd)->phdr;
for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
{
Elf_External_Phdr x_phdr;
if (bfd_bread ((PTR) &x_phdr, (bfd_size_type) sizeof x_phdr, abfd)
!= sizeof x_phdr)
goto got_no_match;
elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
}
}
/* Read in the string table containing the names of the sections. We
will need the base pointer to this table later. */
/* We read this inline now, so that we don't have to go through
bfd_section_from_shdr with it (since this particular strtab is
used to find all of the ELF section names.) */
if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff)
{
unsigned int num_sec;
shstrtab = bfd_elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
if (!shstrtab)
goto got_no_match;
/* Once all of the section headers have been read and converted, we
can start processing them. Note that the first section header is
a dummy placeholder entry, so we ignore it. */
num_sec = elf_numsections (abfd);
for (shindex = 1; shindex < num_sec; shindex++)
{
if (! bfd_section_from_shdr (abfd, shindex))
goto got_no_match;
if (shindex == SHN_LORESERVE - 1)
shindex += SHN_HIRESERVE + 1 - SHN_LORESERVE;
}
}
/* Let the backend double check the format and override global
information. */
if (ebd->elf_backend_object_p)
{
if (! (*ebd->elf_backend_object_p) (abfd))
goto got_wrong_format_error;
}
/* If we have created any reloc sections that are associated with
debugging sections, mark the reloc sections as debugging as well. */
for (s = abfd->sections; s != NULL; s = s->next)
{
if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL
|| elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
&& elf_section_data (s)->this_hdr.sh_info > 0)
{
unsigned long targ_index;
asection *targ_sec;
targ_index = elf_section_data (s)->this_hdr.sh_info;
targ_sec = bfd_section_from_elf_index (abfd, targ_index);
if (targ_sec != NULL
&& (targ_sec->flags & SEC_DEBUGGING) != 0)
s->flags |= SEC_DEBUGGING;
}
}
bfd_preserve_finish (abfd, &preserve);
return abfd->xvec;
got_wrong_format_error:
/* There is way too much undoing of half-known state here. The caller,
bfd_check_format_matches, really shouldn't iterate on live bfd's to
check match/no-match like it does. We have to rely on that a call to
bfd_default_set_arch_mach with the previously known mach, undoes what
was done by the first bfd_default_set_arch_mach (with mach 0) here.
For this to work, only elf-data and the mach may be changed by the
target-specific elf_backend_object_p function. Note that saving the
whole bfd here and restoring it would be even worse; the first thing
you notice is that the cached bfd file position gets out of sync. */
bfd_set_error (bfd_error_wrong_format);
got_no_match:
if (preserve.marker != NULL)
bfd_preserve_restore (abfd, &preserve);
return NULL;
}
/* ELF .o/exec file writing */
/* Write out the relocs. */
void
elf_write_relocs (abfd, sec, data)
bfd *abfd;
asection *sec;
PTR data;
{
bfd_boolean *failedp = (bfd_boolean *) data;
Elf_Internal_Shdr *rela_hdr;
bfd_vma addr_offset;
void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *));
size_t extsize;
bfd_byte *dst_rela;
unsigned int idx;
asymbol *last_sym;
int last_sym_idx;
/* If we have already failed, don't do anything. */
if (*failedp)
return;
if ((sec->flags & SEC_RELOC) == 0)
return;
/* The linker backend writes the relocs out itself, and sets the
reloc_count field to zero to inhibit writing them here. Also,
sometimes the SEC_RELOC flag gets set even when there aren't any
relocs. */
if (sec->reloc_count == 0)
return;
rela_hdr = &elf_section_data (sec)->rel_hdr;
rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
rela_hdr->contents = (PTR) bfd_alloc (abfd, rela_hdr->sh_size);
if (rela_hdr->contents == NULL)
{
*failedp = TRUE;
return;
}
/* Figure out whether the relocations are RELA or REL relocations. */
if (rela_hdr->sh_type == SHT_RELA)
{
swap_out = elf_swap_reloca_out;
extsize = sizeof (Elf_External_Rela);
}
else if (rela_hdr->sh_type == SHT_REL)
{
swap_out = elf_swap_reloc_out;
extsize = sizeof (Elf_External_Rel);
}
else
/* Every relocation section should be either an SHT_RELA or an
SHT_REL section. */
abort ();
/* The address of an ELF reloc is section relative for an object
file, and absolute for an executable file or shared library.
The address of a BFD reloc is always section relative. */
addr_offset = 0;
if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
addr_offset = sec->vma;
/* orelocation has the data, reloc_count has the count... */
last_sym = 0;
last_sym_idx = 0;
dst_rela = rela_hdr->contents;
for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
{
Elf_Internal_Rela src_rela;
arelent *ptr;
asymbol *sym;
int n;
ptr = sec->orelocation[idx];
sym = *ptr->sym_ptr_ptr;
if (sym == last_sym)
n = last_sym_idx;
else if (bfd_is_abs_section (sym->section) && sym->value == 0)
n = STN_UNDEF;
else
{
last_sym = sym;
n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
if (n < 0)
{
*failedp = TRUE;
return;
}
last_sym_idx = n;
}
if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
&& (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
&& ! _bfd_elf_validate_reloc (abfd, ptr))
{
*failedp = TRUE;
return;
}
src_rela.r_offset = ptr->address + addr_offset;
src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
src_rela.r_addend = ptr->addend;
(*swap_out) (abfd, &src_rela, dst_rela);
}
}
/* Write out the program headers. */
int
elf_write_out_phdrs (abfd, phdr, count)
bfd *abfd;
const Elf_Internal_Phdr *phdr;
unsigned int count;
{
while (count--)
{
Elf_External_Phdr extphdr;
elf_swap_phdr_out (abfd, phdr, &extphdr);
if (bfd_bwrite (&extphdr, (bfd_size_type) sizeof (Elf_External_Phdr),
abfd) != sizeof (Elf_External_Phdr))
return -1;
phdr++;
}
return 0;
}
/* Write out the section headers and the ELF file header. */
bfd_boolean
elf_write_shdrs_and_ehdr (abfd)
bfd *abfd;
{
Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
Elf_External_Shdr *x_shdrp; /* Section header table, external form */
Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
unsigned int count;
bfd_size_type amt;
i_ehdrp = elf_elfheader (abfd);
i_shdrp = elf_elfsections (abfd);