forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinker.c
3617 lines (3150 loc) · 105 KB
/
linker.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
/* linker.c -- BFD linker routines
Copyright (C) 1993-2025 Free Software Foundation, Inc.
Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
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. */
#include "sysdep.h"
#include "bfd.h"
#include "libbfd.h"
#include "bfdlink.h"
#include "genlink.h"
/*
SECTION
Linker Functions
@cindex Linker
The linker uses three special entry points in the BFD target
vector. It is not necessary to write special routines for
these entry points when creating a new BFD back end, since
generic versions are provided. However, writing them can
speed up linking and make it use significantly less runtime
memory.
The first routine creates a hash table used by the other
routines. The second routine adds the symbols from an object
file to the hash table. The third routine takes all the
object files and links them together to create the output
file. These routines are designed so that the linker proper
does not need to know anything about the symbols in the object
files that it is linking. The linker merely arranges the
sections as directed by the linker script and lets BFD handle
the details of symbols and relocs.
The second routine and third routines are passed a pointer to
a <<struct bfd_link_info>> structure (defined in
<<bfdlink.h>>) which holds information relevant to the link,
including the linker hash table (which was created by the
first routine) and a set of callback functions to the linker
proper.
The generic linker routines are in <<linker.c>>, and use the
header file <<genlink.h>>. As of this writing, the only back
ends which have implemented versions of these routines are
a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>). The a.out
routines are used as examples throughout this section.
@menu
@* Creating a Linker Hash Table::
@* Adding Symbols to the Hash Table::
@* Performing the Final Link::
@end menu
INODE
Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
SUBSECTION
Creating a linker hash table
@cindex _bfd_link_hash_table_create in target vector
@cindex target vector (_bfd_link_hash_table_create)
The linker routines must create a hash table, which must be
derived from <<struct bfd_link_hash_table>> described in
<<bfdlink.c>>. @xref{Hash Tables}, for information on how to
create a derived hash table. This entry point is called using
the target vector of the linker output file.
The <<_bfd_link_hash_table_create>> entry point must allocate
and initialize an instance of the desired hash table. If the
back end does not require any additional information to be
stored with the entries in the hash table, the entry point may
simply create a <<struct bfd_link_hash_table>>. Most likely,
however, some additional information will be needed.
For example, with each entry in the hash table the a.out
linker keeps the index the symbol has in the final output file
(this index number is used so that when doing a relocatable
link the symbol index used in the output file can be quickly
filled in when copying over a reloc). The a.out linker code
defines the required structures and functions for a hash table
derived from <<struct bfd_link_hash_table>>. The a.out linker
hash table is created by the function
<<NAME(aout,link_hash_table_create)>>; it simply allocates
space for the hash table, initializes it, and returns a
pointer to it.
When writing the linker routines for a new back end, you will
generally not know exactly which fields will be required until
you have finished. You should simply create a new hash table
which defines no additional fields, and then simply add fields
as they become necessary.
INODE
Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
SUBSECTION
Adding symbols to the hash table
@cindex _bfd_link_add_symbols in target vector
@cindex target vector (_bfd_link_add_symbols)
The linker proper will call the <<_bfd_link_add_symbols>>
entry point for each object file or archive which is to be
linked (typically these are the files named on the command
line, but some may also come from the linker script). The
entry point is responsible for examining the file. For an
object file, BFD must add any relevant symbol information to
the hash table. For an archive, BFD must determine which
elements of the archive should be used and adding them to the
link.
The a.out version of this entry point is
<<NAME(aout,link_add_symbols)>>.
@menu
@* Differing file formats::
@* Adding symbols from an object file::
@* Adding symbols from an archive::
@end menu
INODE
Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
SUBSUBSECTION
Differing file formats
Normally all the files involved in a link will be of the same
format, but it is also possible to link together different
format object files, and the back end must support that. The
<<_bfd_link_add_symbols>> entry point is called via the target
vector of the file to be added. This has an important
consequence: the function may not assume that the hash table
is the type created by the corresponding
<<_bfd_link_hash_table_create>> vector. All the
<<_bfd_link_add_symbols>> function can assume about the hash
table is that it is derived from <<struct
bfd_link_hash_table>>.
Sometimes the <<_bfd_link_add_symbols>> function must store
some information in the hash table entry to be used by the
<<_bfd_final_link>> function. In such a case the output bfd
xvec must be checked to make sure that the hash table was
created by an object file of the same format.
The <<_bfd_final_link>> routine must be prepared to handle a
hash entry without any extra information added by the
<<_bfd_link_add_symbols>> function. A hash entry without
extra information will also occur when the linker script
directs the linker to create a symbol. Note that, regardless
of how a hash table entry is added, all the fields will be
initialized to some sort of null value by the hash table entry
initialization function.
See <<ecoff_link_add_externals>> for an example of how to
check the output bfd before saving information (in this
case, the ECOFF external symbol debugging information) in a
hash table entry.
INODE
Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
SUBSUBSECTION
Adding symbols from an object file
When the <<_bfd_link_add_symbols>> routine is passed an object
file, it must add all externally visible symbols in that
object file to the hash table. The actual work of adding the
symbol to the hash table is normally handled by the function
<<_bfd_generic_link_add_one_symbol>>. The
<<_bfd_link_add_symbols>> routine is responsible for reading
all the symbols from the object file and passing the correct
information to <<_bfd_generic_link_add_one_symbol>>.
The <<_bfd_link_add_symbols>> routine should not use
<<bfd_canonicalize_symtab>> to read the symbols. The point of
providing this routine is to avoid the overhead of converting
the symbols into generic <<asymbol>> structures.
@findex _bfd_generic_link_add_one_symbol
<<_bfd_generic_link_add_one_symbol>> handles the details of
combining common symbols, warning about multiple definitions,
and so forth. It takes arguments which describe the symbol to
add, notably symbol flags, a section, and an offset. The
symbol flags include such things as <<BSF_WEAK>> or
<<BSF_INDIRECT>>. The section is a section in the object
file, or something like <<bfd_und_section_ptr>> for an undefined
symbol or <<bfd_com_section_ptr>> for a common symbol.
If the <<_bfd_final_link>> routine is also going to need to
read the symbol information, the <<_bfd_link_add_symbols>>
routine should save it somewhere attached to the object file
BFD. However, the information should only be saved if the
<<keep_memory>> field of the <<info>> argument is TRUE, so
that the <<-no-keep-memory>> linker switch is effective.
The a.out function which adds symbols from an object file is
<<aout_link_add_object_symbols>>, and most of the interesting
work is in <<aout_link_add_symbols>>. The latter saves
pointers to the hash tables entries created by
<<_bfd_generic_link_add_one_symbol>> indexed by symbol number,
so that the <<_bfd_final_link>> routine does not have to call
the hash table lookup routine to locate the entry.
INODE
Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
SUBSUBSECTION
Adding symbols from an archive
When the <<_bfd_link_add_symbols>> routine is passed an
archive, it must look through the symbols defined by the
archive and decide which elements of the archive should be
included in the link. For each such element it must call the
<<add_archive_element>> linker callback, and it must add the
symbols from the object file to the linker hash table. (The
callback may in fact indicate that a replacement BFD should be
used, in which case the symbols from that BFD should be added
to the linker hash table instead.)
@findex _bfd_generic_link_add_archive_symbols
In most cases the work of looking through the symbols in the
archive should be done by the
<<_bfd_generic_link_add_archive_symbols>> function.
<<_bfd_generic_link_add_archive_symbols>> is passed a function
to call to make the final decision about adding an archive
element to the link and to do the actual work of adding the
symbols to the linker hash table. If the element is to
be included, the <<add_archive_element>> linker callback
routine must be called with the element as an argument, and
the element's symbols must be added to the linker hash table
just as though the element had itself been passed to the
<<_bfd_link_add_symbols>> function.
When the a.out <<_bfd_link_add_symbols>> function receives an
archive, it calls <<_bfd_generic_link_add_archive_symbols>>
passing <<aout_link_check_archive_element>> as the function
argument. <<aout_link_check_archive_element>> calls
<<aout_link_check_ar_symbols>>. If the latter decides to add
the element (an element is only added if it provides a real,
non-common, definition for a previously undefined or common
symbol) it calls the <<add_archive_element>> callback and then
<<aout_link_check_archive_element>> calls
<<aout_link_add_symbols>> to actually add the symbols to the
linker hash table - possibly those of a substitute BFD, if the
<<add_archive_element>> callback avails itself of that option.
The ECOFF back end is unusual in that it does not normally
call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
archives already contain a hash table of symbols. The ECOFF
back end searches the archive itself to avoid the overhead of
creating a new hash table.
INODE
Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions
SUBSECTION
Performing the final link
@cindex _bfd_link_final_link in target vector
@cindex target vector (_bfd_final_link)
When all the input files have been processed, the linker calls
the <<_bfd_final_link>> entry point of the output BFD. This
routine is responsible for producing the final output file,
which has several aspects. It must relocate the contents of
the input sections and copy the data into the output sections.
It must build an output symbol table including any local
symbols from the input files and the global symbols from the
hash table. When producing relocatable output, it must
modify the input relocs and write them into the output file.
There may also be object format dependent work to be done.
The linker will also call the <<write_object_contents>> entry
point when the BFD is closed. The two entry points must work
together in order to produce the correct output file.
The details of how this works are inevitably dependent upon
the specific object file format. The a.out
<<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
@menu
@* Information provided by the linker::
@* Relocating the section contents::
@* Writing the symbol table::
@end menu
INODE
Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
SUBSUBSECTION
Information provided by the linker
Before the linker calls the <<_bfd_final_link>> entry point,
it sets up some data structures for the function to use.
The <<input_bfds>> field of the <<bfd_link_info>> structure
will point to a list of all the input files included in the
link. These files are linked through the <<link.next>> field
of the <<bfd>> structure.
Each section in the output file will have a list of
<<link_order>> structures attached to the <<map_head.link_order>>
field (the <<link_order>> structure is defined in
<<bfdlink.h>>). These structures describe how to create the
contents of the output section in terms of the contents of
various input sections, fill constants, and, eventually, other
types of information. They also describe relocs that must be
created by the BFD backend, but do not correspond to any input
file; this is used to support -Ur, which builds constructors
while generating a relocatable object file.
INODE
Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
SUBSUBSECTION
Relocating the section contents
The <<_bfd_final_link>> function should look through the
<<link_order>> structures attached to each section of the
output file. Each <<link_order>> structure should either be
handled specially, or it should be passed to the function
<<_bfd_default_link_order>> which will do the right thing
(<<_bfd_default_link_order>> is defined in <<linker.c>>).
For efficiency, a <<link_order>> of type
<<bfd_indirect_link_order>> whose associated section belongs
to a BFD of the same format as the output BFD must be handled
specially. This type of <<link_order>> describes part of an
output section in terms of a section belonging to one of the
input files. The <<_bfd_final_link>> function should read the
contents of the section and any associated relocs, apply the
relocs to the section contents, and write out the modified
section contents. If performing a relocatable link, the
relocs themselves must also be modified and written out.
@findex _bfd_relocate_contents
@findex _bfd_final_link_relocate
The functions <<_bfd_relocate_contents>> and
<<_bfd_final_link_relocate>> provide some general support for
performing the actual relocations, notably overflow checking.
Their arguments include information about the symbol the
relocation is against and a <<reloc_howto_type>> argument
which describes the relocation to perform. These functions
are defined in <<reloc.c>>.
The a.out function which handles reading, relocating, and
writing section contents is <<aout_link_input_section>>. The
actual relocation is done in <<aout_link_input_section_std>>
and <<aout_link_input_section_ext>>.
INODE
Writing the symbol table, , Relocating the section contents, Performing the Final Link
SUBSUBSECTION
Writing the symbol table
The <<_bfd_final_link>> function must gather all the symbols
in the input files and write them out. It must also write out
all the symbols in the global hash table. This must be
controlled by the <<strip>> and <<discard>> fields of the
<<bfd_link_info>> structure.
The local symbols of the input files will not have been
entered into the linker hash table. The <<_bfd_final_link>>
routine must consider each input file and include the symbols
in the output file. It may be convenient to do this when
looking through the <<link_order>> structures, or it may be
done by stepping through the <<input_bfds>> list.
The <<_bfd_final_link>> routine must also traverse the global
hash table to gather all the externally visible symbols. It
is possible that most of the externally visible symbols may be
written out when considering the symbols of each input file,
but it is still necessary to traverse the hash table since the
linker script may have defined some symbols that are not in
any of the input files.
The <<strip>> field of the <<bfd_link_info>> structure
controls which symbols are written out. The possible values
are listed in <<bfdlink.h>>. If the value is <<strip_some>>,
then the <<keep_hash>> field of the <<bfd_link_info>>
structure is a hash table of symbols to keep; each symbol
should be looked up in this hash table, and only symbols which
are present should be included in the output file.
If the <<strip>> field of the <<bfd_link_info>> structure
permits local symbols to be written out, the <<discard>> field
is used to further controls which local symbols are included
in the output file. If the value is <<discard_l>>, then all
local symbols which begin with a certain prefix are discarded;
this is controlled by the <<bfd_is_local_label_name>> entry point.
The a.out backend handles symbols by calling
<<aout_link_write_symbols>> on each input BFD and then
traversing the global hash table with the function
<<aout_link_write_other_symbol>>. It builds a string table
while writing out the symbols, which is written to the output
file at the end of <<NAME(aout,final_link)>>.
*/
/* This structure is used to pass information to
_bfd_generic_link_write_global_symbol, which may be called via
_bfd_generic_link_hash_traverse. */
struct generic_write_global_symbol_info
{
struct bfd_link_info *info;
bfd *output_bfd;
size_t *psymalloc;
bool failed;
};
static bool generic_link_add_object_symbols
(bfd *, struct bfd_link_info *);
static bool generic_link_check_archive_element
(bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
bool *);
static bool generic_link_add_symbol_list
(bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **);
static bool generic_add_output_symbol
(bfd *, size_t *psymalloc, asymbol *);
static bool default_data_link_order
(bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
static bool default_indirect_link_order
(bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *,
bool);
static bool _bfd_generic_link_output_symbols
(bfd *, bfd *, struct bfd_link_info *, size_t *);
static bool _bfd_generic_link_write_global_symbol
(struct generic_link_hash_entry *, void *);
/* The link hash table structure is defined in bfdlink.h. It provides
a base hash table which the backend specific hash tables are built
upon. */
/* Routine to create an entry in the link hash table. */
struct bfd_hash_entry *
_bfd_link_hash_newfunc (struct bfd_hash_entry *entry,
struct bfd_hash_table *table,
const char *string)
{
/* Allocate the structure if it has not already been allocated by a
subclass. */
if (entry == NULL)
{
entry = (struct bfd_hash_entry *)
bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry));
if (entry == NULL)
return entry;
}
/* Call the allocation method of the superclass. */
entry = bfd_hash_newfunc (entry, table, string);
if (entry)
{
struct bfd_link_hash_entry *h = (struct bfd_link_hash_entry *) entry;
/* Initialize the local fields. */
memset ((char *) &h->root + sizeof (h->root), 0,
sizeof (*h) - sizeof (h->root));
}
return entry;
}
/* Initialize a link hash table. The BFD argument is the one
responsible for creating this table. */
bool
_bfd_link_hash_table_init
(struct bfd_link_hash_table *table,
bfd *abfd ATTRIBUTE_UNUSED,
struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
struct bfd_hash_table *,
const char *),
unsigned int entsize)
{
bool ret;
BFD_ASSERT (!abfd->is_linker_output && !abfd->link.hash);
table->undefs = NULL;
table->undefs_tail = NULL;
table->type = bfd_link_generic_hash_table;
ret = bfd_hash_table_init (&table->table, newfunc, entsize);
if (ret)
{
/* Arrange for destruction of this hash table on closing ABFD. */
table->hash_table_free = _bfd_generic_link_hash_table_free;
abfd->link.hash = table;
abfd->is_linker_output = true;
}
return ret;
}
/* Look up a symbol in a link hash table. If follow is TRUE, we
follow bfd_link_hash_indirect and bfd_link_hash_warning links to
the real symbol.
.{* Return TRUE if the symbol described by a linker hash entry H
. is going to be absolute. Linker-script defined symbols can be
. converted from absolute to section-relative ones late in the
. link. Use this macro to correctly determine whether the symbol
. will actually end up absolute in output. *}
.#define bfd_is_abs_symbol(H) \
. (((H)->type == bfd_link_hash_defined \
. || (H)->type == bfd_link_hash_defweak) \
. && bfd_is_abs_section ((H)->u.def.section) \
. && !(H)->rel_from_abs)
.
*/
struct bfd_link_hash_entry *
bfd_link_hash_lookup (struct bfd_link_hash_table *table,
const char *string,
bool create,
bool copy,
bool follow)
{
struct bfd_link_hash_entry *ret;
if (table == NULL || string == NULL)
return NULL;
ret = ((struct bfd_link_hash_entry *)
bfd_hash_lookup (&table->table, string, create, copy));
if (follow && ret != NULL)
{
while (ret->type == bfd_link_hash_indirect
|| ret->type == bfd_link_hash_warning)
ret = ret->u.i.link;
}
return ret;
}
/* Look up a symbol in the main linker hash table if the symbol might
be wrapped. This should only be used for references to an
undefined symbol, not for definitions of a symbol. */
struct bfd_link_hash_entry *
bfd_wrapped_link_hash_lookup (bfd *abfd,
struct bfd_link_info *info,
const char *string,
bool create,
bool copy,
bool follow)
{
size_t amt;
if (info->wrap_hash != NULL)
{
const char *l;
char prefix = '\0';
l = string;
if (*l
&& (*l == bfd_get_symbol_leading_char (abfd)
|| *l == info->wrap_char))
{
prefix = *l;
++l;
}
#undef WRAP
#define WRAP "__wrap_"
if (bfd_hash_lookup (info->wrap_hash, l, false, false) != NULL)
{
char *n;
struct bfd_link_hash_entry *h;
/* This symbol is being wrapped. We want to replace all
references to SYM with references to __wrap_SYM. */
amt = strlen (l) + sizeof WRAP + 1;
n = (char *) bfd_malloc (amt);
if (n == NULL)
return NULL;
n[0] = prefix;
n[1] = '\0';
strcat (n, WRAP);
strcat (n, l);
h = bfd_link_hash_lookup (info->hash, n, create, true, follow);
if (h != NULL)
h->wrapper_symbol = true;
free (n);
return h;
}
#undef REAL
#define REAL "__real_"
if (*l == '_'
&& startswith (l, REAL)
&& bfd_hash_lookup (info->wrap_hash, l + sizeof REAL - 1,
false, false) != NULL)
{
char *n;
struct bfd_link_hash_entry *h;
/* This is a reference to __real_SYM, where SYM is being
wrapped. We want to replace all references to __real_SYM
with references to SYM. */
amt = strlen (l + sizeof REAL - 1) + 2;
n = (char *) bfd_malloc (amt);
if (n == NULL)
return NULL;
n[0] = prefix;
n[1] = '\0';
strcat (n, l + sizeof REAL - 1);
h = bfd_link_hash_lookup (info->hash, n, create, true, follow);
if (h != NULL)
h->ref_real = 1;
free (n);
return h;
}
#undef REAL
}
return bfd_link_hash_lookup (info->hash, string, create, copy, follow);
}
/* If H is a wrapped symbol, ie. the symbol name starts with "__wrap_"
and the remainder is found in wrap_hash, return the real symbol. */
struct bfd_link_hash_entry *
unwrap_hash_lookup (struct bfd_link_info *info,
bfd *input_bfd,
struct bfd_link_hash_entry *h)
{
const char *l = h->root.string;
if (*l
&& (*l == bfd_get_symbol_leading_char (input_bfd)
|| *l == info->wrap_char))
++l;
if (startswith (l, WRAP))
{
l += sizeof WRAP - 1;
if (bfd_hash_lookup (info->wrap_hash, l, false, false) != NULL)
{
char save = 0;
if (l - (sizeof WRAP - 1) != h->root.string)
{
--l;
save = *l;
*(char *) l = *h->root.string;
}
h = bfd_link_hash_lookup (info->hash, l, false, false, false);
if (save)
*(char *) l = save;
}
}
return h;
}
#undef WRAP
/* Traverse a generic link hash table. Differs from bfd_hash_traverse
in the treatment of warning symbols. When warning symbols are
created they replace the real symbol, so you don't get to see the
real symbol in a bfd_hash_traverse. This traversal calls func with
the real symbol. */
void
bfd_link_hash_traverse
(struct bfd_link_hash_table *htab,
bool (*func) (struct bfd_link_hash_entry *, void *),
void *info)
{
unsigned int i;
htab->table.frozen = 1;
for (i = 0; i < htab->table.size; i++)
{
struct bfd_link_hash_entry *p;
p = (struct bfd_link_hash_entry *) htab->table.table[i];
for (; p != NULL; p = (struct bfd_link_hash_entry *) p->root.next)
if (!(*func) (p->type == bfd_link_hash_warning ? p->u.i.link : p, info))
goto out;
}
out:
htab->table.frozen = 0;
}
/* Add a symbol to the linker hash table undefs list. */
void
bfd_link_add_undef (struct bfd_link_hash_table *table,
struct bfd_link_hash_entry *h)
{
BFD_ASSERT (h->u.undef.next == NULL);
if (table->undefs_tail != NULL)
table->undefs_tail->u.undef.next = h;
if (table->undefs == NULL)
table->undefs = h;
table->undefs_tail = h;
}
/* The undefs list was designed so that in normal use we don't need to
remove entries. However, if symbols on the list are changed from
bfd_link_hash_undefined to either bfd_link_hash_undefweak or
bfd_link_hash_new for some reason, then they must be removed from the
list. Failure to do so might result in the linker attempting to add
the symbol to the list again at a later stage. */
void
bfd_link_repair_undef_list (struct bfd_link_hash_table *table)
{
struct bfd_link_hash_entry **pun;
pun = &table->undefs;
while (*pun != NULL)
{
struct bfd_link_hash_entry *h = *pun;
if (h->type == bfd_link_hash_new
|| h->type == bfd_link_hash_undefweak)
{
*pun = h->u.undef.next;
h->u.undef.next = NULL;
if (h == table->undefs_tail)
{
if (pun == &table->undefs)
table->undefs_tail = NULL;
else
/* pun points at an u.undef.next field. Go back to
the start of the link_hash_entry. */
table->undefs_tail = (struct bfd_link_hash_entry *)
((char *) pun - ((char *) &h->u.undef.next - (char *) h));
break;
}
}
else
pun = &h->u.undef.next;
}
}
/* Routine to create an entry in a generic link hash table. */
static struct bfd_hash_entry *
_bfd_generic_link_hash_newfunc (struct bfd_hash_entry *entry,
struct bfd_hash_table *table,
const char *string)
{
/* Allocate the structure if it has not already been allocated by a
subclass. */
if (entry == NULL)
{
entry = (struct bfd_hash_entry *)
bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry));
if (entry == NULL)
return entry;
}
/* Call the allocation method of the superclass. */
entry = _bfd_link_hash_newfunc (entry, table, string);
if (entry)
{
struct generic_link_hash_entry *ret;
/* Set local fields. */
ret = (struct generic_link_hash_entry *) entry;
ret->written = false;
ret->sym = NULL;
}
return entry;
}
/* Create a generic link hash table. */
struct bfd_link_hash_table *
_bfd_generic_link_hash_table_create (bfd *abfd)
{
struct generic_link_hash_table *ret;
size_t amt = sizeof (struct generic_link_hash_table);
ret = (struct generic_link_hash_table *) bfd_malloc (amt);
if (ret == NULL)
return NULL;
if (! _bfd_link_hash_table_init (&ret->root, abfd,
_bfd_generic_link_hash_newfunc,
sizeof (struct generic_link_hash_entry)))
{
free (ret);
return NULL;
}
return &ret->root;
}
void
_bfd_generic_link_hash_table_free (bfd *obfd)
{
struct generic_link_hash_table *ret;
BFD_ASSERT (obfd->is_linker_output && obfd->link.hash);
ret = (struct generic_link_hash_table *) obfd->link.hash;
bfd_hash_table_free (&ret->root.table);
free (ret);
obfd->link.hash = NULL;
obfd->is_linker_output = false;
}
/* Grab the symbols for an object file when doing a generic link. We
store the symbols in the outsymbols field. We need to keep them
around for the entire link to ensure that we only read them once.
If we read them multiple times, we might wind up with relocs and
the hash table pointing to different instances of the symbol
structure. */
bool
bfd_generic_link_read_symbols (bfd *abfd)
{
if (bfd_get_outsymbols (abfd) == NULL)
{
long symsize;
long symcount;
symsize = bfd_get_symtab_upper_bound (abfd);
if (symsize < 0)
return false;
abfd->outsymbols = bfd_alloc (abfd, symsize);
if (bfd_get_outsymbols (abfd) == NULL && symsize != 0)
return false;
symcount = bfd_canonicalize_symtab (abfd, bfd_get_outsymbols (abfd));
if (symcount < 0)
return false;
abfd->symcount = symcount;
}
return true;
}
/* Indicate that we are only retrieving symbol values from this
section. We want the symbols to act as though the values in the
file are absolute. */
void
_bfd_generic_link_just_syms (asection *sec,
struct bfd_link_info *info ATTRIBUTE_UNUSED)
{
sec->sec_info_type = SEC_INFO_TYPE_JUST_SYMS;
sec->output_section = bfd_abs_section_ptr;
sec->output_offset = sec->vma;
}
/* Copy the symbol type and other attributes for a linker script
assignment from HSRC to HDEST.
The default implementation does nothing. */
void
_bfd_generic_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
struct bfd_link_hash_entry *hdest ATTRIBUTE_UNUSED,
struct bfd_link_hash_entry *hsrc ATTRIBUTE_UNUSED)
{
}
/* Generic function to add symbols from an object file to the
global hash table. */
bool
_bfd_generic_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
{
bool ret;
switch (bfd_get_format (abfd))
{
case bfd_object:
ret = generic_link_add_object_symbols (abfd, info);
break;
case bfd_archive:
ret = (_bfd_generic_link_add_archive_symbols
(abfd, info, generic_link_check_archive_element));
break;
default:
bfd_set_error (bfd_error_wrong_format);
ret = false;
}
return ret;
}
/* Add symbols from an object file to the global hash table. */
static bool
generic_link_add_object_symbols (bfd *abfd,
struct bfd_link_info *info)
{
bfd_size_type symcount;
struct bfd_symbol **outsyms;
if (!bfd_generic_link_read_symbols (abfd))
return false;
symcount = _bfd_generic_link_get_symcount (abfd);
outsyms = _bfd_generic_link_get_symbols (abfd);
return generic_link_add_symbol_list (abfd, info, symcount, outsyms);
}
/* Generic function to add symbols from an archive file to the global
hash file. This function presumes that the archive symbol table
has already been read in (this is normally done by the
bfd_check_format entry point). It looks through the archive symbol
table for symbols that are undefined or common in the linker global
symbol hash table. When one is found, the CHECKFN argument is used
to see if an object file should be included. This allows targets
to customize common symbol behaviour. CHECKFN should set *PNEEDED
to TRUE if the object file should be included, and must also call
the bfd_link_info add_archive_element callback function and handle
adding the symbols to the global hash table. CHECKFN must notice
if the callback indicates a substitute BFD, and arrange to add
those symbols instead if it does so. CHECKFN should only return
FALSE if some sort of error occurs. */
bool
_bfd_generic_link_add_archive_symbols
(bfd *abfd,
struct bfd_link_info *info,
bool (*checkfn) (bfd *, struct bfd_link_info *,
struct bfd_link_hash_entry *, const char *, bool *))
{
bool loop;
bfd_size_type amt;
unsigned char *included;
if (! bfd_has_map (abfd))
{
/* An empty archive is a special case. */
if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
return true;
bfd_set_error (bfd_error_no_armap);
return false;
}
amt = bfd_ardata (abfd)->symdef_count;
if (amt == 0)
return true;
amt *= sizeof (*included);
included = (unsigned char *) bfd_zmalloc (amt);
if (included == NULL)
return false;
do
{
carsym *arsyms;
carsym *arsym_end;
carsym *arsym;
unsigned int indx;
file_ptr last_ar_offset = -1;
bool needed = false;
bfd *element = NULL;
loop = false;
arsyms = bfd_ardata (abfd)->symdefs;
arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
{
struct bfd_link_hash_entry *h;
struct bfd_link_hash_entry *undefs_tail;
if (included[indx])
continue;
if (needed && arsym->file_offset == last_ar_offset)
{
included[indx] = 1;
continue;
}
if (arsym->name == NULL)
goto error_return;
h = bfd_link_hash_lookup (info->hash, arsym->name,
false, false, true);
if (h == NULL
&& info->pei386_auto_import
&& startswith (arsym->name, "__imp_"))
h = bfd_link_hash_lookup (info->hash, arsym->name + 6,
false, false, true);
if (h == NULL)
continue;
if (h->type != bfd_link_hash_undefined
&& h->type != bfd_link_hash_common)
{
if (h->type != bfd_link_hash_undefweak)
/* Symbol must be defined. Don't check it again. */
included[indx] = 1;