* cofflink.c (_bfd_coff_link_input_bfd): When merging, skip names
[deliverable/binutils-gdb.git] / bfd / linker.c
CommitLineData
da6b2d99 1/* linker.c -- BFD linker routines
6c97aedf 2 Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
da6b2d99
ILT
3 Written by Steve Chamberlain and Ian Lance Taylor, Cygnus Support
4
9783e04a 5This file is part of BFD, the Binary File Descriptor library.
da6b2d99 6
9783e04a 7This program is free software; you can redistribute it and/or modify
da6b2d99 8it under the terms of the GNU General Public License as published by
9783e04a
DM
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
da6b2d99 11
9783e04a 12This program is distributed in the hope that it will be useful,
da6b2d99
ILT
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9783e04a 18along with this program; if not, write to the Free Software
943fbd5b 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
da6b2d99
ILT
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "libbfd.h"
24#include "bfdlink.h"
25#include "genlink.h"
26
d4366f97
ILT
27/*
28SECTION
29 Linker Functions
30
31@cindex Linker
32 The linker uses three special entry points in the BFD target
33 vector. It is not necessary to write special routines for
34 these entry points when creating a new BFD back end, since
35 generic versions are provided. However, writing them can
36 speed up linking and make it use significantly less runtime
37 memory.
38
39 The first routine creates a hash table used by the other
40 routines. The second routine adds the symbols from an object
41 file to the hash table. The third routine takes all the
42 object files and links them together to create the output
43 file. These routines are designed so that the linker proper
44 does not need to know anything about the symbols in the object
45 files that it is linking. The linker merely arranges the
46 sections as directed by the linker script and lets BFD handle
47 the details of symbols and relocs.
48
49 The second routine and third routines are passed a pointer to
50 a <<struct bfd_link_info>> structure (defined in
51 <<bfdlink.h>>) which holds information relevant to the link,
52 including the linker hash table (which was created by the
53 first routine) and a set of callback functions to the linker
54 proper.
55
56 The generic linker routines are in <<linker.c>>, and use the
57 header file <<genlink.h>>. As of this writing, the only back
58 ends which have implemented versions of these routines are
59 a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>). The a.out
60 routines are used as examples throughout this section.
61
62@menu
63@* Creating a Linker Hash Table::
64@* Adding Symbols to the Hash Table::
65@* Performing the Final Link::
66@end menu
67
68INODE
69Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
70SUBSECTION
71 Creating a linker hash table
72
73@cindex _bfd_link_hash_table_create in target vector
74@cindex target vector (_bfd_link_hash_table_create)
75 The linker routines must create a hash table, which must be
76 derived from <<struct bfd_link_hash_table>> described in
77 <<bfdlink.c>>. @xref{Hash Tables} for information on how to
78 create a derived hash table. This entry point is called using
79 the target vector of the linker output file.
80
81 The <<_bfd_link_hash_table_create>> entry point must allocate
82 and initialize an instance of the desired hash table. If the
83 back end does not require any additional information to be
84 stored with the entries in the hash table, the entry point may
85 simply create a <<struct bfd_link_hash_table>>. Most likely,
86 however, some additional information will be needed.
87
88 For example, with each entry in the hash table the a.out
89 linker keeps the index the symbol has in the final output file
90 (this index number is used so that when doing a relocateable
91 link the symbol index used in the output file can be quickly
92 filled in when copying over a reloc). The a.out linker code
93 defines the required structures and functions for a hash table
94 derived from <<struct bfd_link_hash_table>>. The a.out linker
95 hash table is created by the function
96 <<NAME(aout,link_hash_table_create)>>; it simply allocates
97 space for the hash table, initializes it, and returns a
98 pointer to it.
99
100 When writing the linker routines for a new back end, you will
101 generally not know exactly which fields will be required until
102 you have finished. You should simply create a new hash table
103 which defines no additional fields, and then simply add fields
104 as they become necessary.
105
106INODE
107Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
108SUBSECTION
109 Adding symbols to the hash table
110
111@cindex _bfd_link_add_symbols in target vector
112@cindex target vector (_bfd_link_add_symbols)
113 The linker proper will call the <<_bfd_link_add_symbols>>
114 entry point for each object file or archive which is to be
115 linked (typically these are the files named on the command
116 line, but some may also come from the linker script). The
117 entry point is responsible for examining the file. For an
118 object file, BFD must add any relevant symbol information to
119 the hash table. For an archive, BFD must determine which
120 elements of the archive should be used and adding them to the
121 link.
122
123 The a.out version of this entry point is
124 <<NAME(aout,link_add_symbols)>>.
125
126@menu
127@* Differing file formats::
128@* Adding symbols from an object file::
129@* Adding symbols from an archive::
130@end menu
131
132INODE
133Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
134SUBSUBSECTION
135 Differing file formats
136
137 Normally all the files involved in a link will be of the same
138 format, but it is also possible to link together different
139 format object files, and the back end must support that. The
140 <<_bfd_link_add_symbols>> entry point is called via the target
141 vector of the file to be added. This has an important
142 consequence: the function may not assume that the hash table
143 is the type created by the corresponding
144 <<_bfd_link_hash_table_create>> vector. All the
145 <<_bfd_link_add_symbols>> function can assume about the hash
146 table is that it is derived from <<struct
147 bfd_link_hash_table>>.
148
149 Sometimes the <<_bfd_link_add_symbols>> function must store
150 some information in the hash table entry to be used by the
151 <<_bfd_final_link>> function. In such a case the <<creator>>
152 field of the hash table must be checked to make sure that the
153 hash table was created by an object file of the same format.
154
155 The <<_bfd_final_link>> routine must be prepared to handle a
156 hash entry without any extra information added by the
157 <<_bfd_link_add_symbols>> function. A hash entry without
158 extra information will also occur when the linker script
159 directs the linker to create a symbol. Note that, regardless
160 of how a hash table entry is added, all the fields will be
161 initialized to some sort of null value by the hash table entry
162 initialization function.
163
164 See <<ecoff_link_add_externals>> for an example of how to
165 check the <<creator>> field before saving information (in this
166 case, the ECOFF external symbol debugging information) in a
167 hash table entry.
168
169INODE
170Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
171SUBSUBSECTION
172 Adding symbols from an object file
173
174 When the <<_bfd_link_add_symbols>> routine is passed an object
175 file, it must add all externally visible symbols in that
176 object file to the hash table. The actual work of adding the
177 symbol to the hash table is normally handled by the function
178 <<_bfd_generic_link_add_one_symbol>>. The
179 <<_bfd_link_add_symbols>> routine is responsible for reading
180 all the symbols from the object file and passing the correct
181 information to <<_bfd_generic_link_add_one_symbol>>.
182
183 The <<_bfd_link_add_symbols>> routine should not use
184 <<bfd_canonicalize_symtab>> to read the symbols. The point of
185 providing this routine is to avoid the overhead of converting
186 the symbols into generic <<asymbol>> structures.
187
188@findex _bfd_generic_link_add_one_symbol
189 <<_bfd_generic_link_add_one_symbol>> handles the details of
190 combining common symbols, warning about multiple definitions,
191 and so forth. It takes arguments which describe the symbol to
192 add, notably symbol flags, a section, and an offset. The
193 symbol flags include such things as <<BSF_WEAK>> or
194 <<BSF_INDIRECT>>. The section is a section in the object
a537cb21
ILT
195 file, or something like <<bfd_und_section_ptr>> for an undefined
196 symbol or <<bfd_com_section_ptr>> for a common symbol.
d4366f97
ILT
197
198 If the <<_bfd_final_link>> routine is also going to need to
199 read the symbol information, the <<_bfd_link_add_symbols>>
200 routine should save it somewhere attached to the object file
201 BFD. However, the information should only be saved if the
202 <<keep_memory>> field of the <<info>> argument is true, so
203 that the <<-no-keep-memory>> linker switch is effective.
204
205 The a.out function which adds symbols from an object file is
206 <<aout_link_add_object_symbols>>, and most of the interesting
207 work is in <<aout_link_add_symbols>>. The latter saves
208 pointers to the hash tables entries created by
209 <<_bfd_generic_link_add_one_symbol>> indexed by symbol number,
210 so that the <<_bfd_final_link>> routine does not have to call
211 the hash table lookup routine to locate the entry.
212
213INODE
214Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
215SUBSUBSECTION
216 Adding symbols from an archive
217
218 When the <<_bfd_link_add_symbols>> routine is passed an
219 archive, it must look through the symbols defined by the
220 archive and decide which elements of the archive should be
221 included in the link. For each such element it must call the
222 <<add_archive_element>> linker callback, and it must add the
223 symbols from the object file to the linker hash table.
224
225@findex _bfd_generic_link_add_archive_symbols
226 In most cases the work of looking through the symbols in the
227 archive should be done by the
228 <<_bfd_generic_link_add_archive_symbols>> function. This
229 function builds a hash table from the archive symbol table and
230 looks through the list of undefined symbols to see which
231 elements should be included.
232 <<_bfd_generic_link_add_archive_symbols>> is passed a function
233 to call to make the final decision about adding an archive
234 element to the link and to do the actual work of adding the
235 symbols to the linker hash table.
236
237 The function passed to
238 <<_bfd_generic_link_add_archive_symbols>> must read the
239 symbols of the archive element and decide whether the archive
240 element should be included in the link. If the element is to
241 be included, the <<add_archive_element>> linker callback
242 routine must be called with the element as an argument, and
243 the elements symbols must be added to the linker hash table
244 just as though the element had itself been passed to the
245 <<_bfd_link_add_symbols>> function.
246
247 When the a.out <<_bfd_link_add_symbols>> function receives an
248 archive, it calls <<_bfd_generic_link_add_archive_symbols>>
249 passing <<aout_link_check_archive_element>> as the function
250 argument. <<aout_link_check_archive_element>> calls
251 <<aout_link_check_ar_symbols>>. If the latter decides to add
252 the element (an element is only added if it provides a real,
253 non-common, definition for a previously undefined or common
254 symbol) it calls the <<add_archive_element>> callback and then
255 <<aout_link_check_archive_element>> calls
256 <<aout_link_add_symbols>> to actually add the symbols to the
257 linker hash table.
258
259 The ECOFF back end is unusual in that it does not normally
260 call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
261 archives already contain a hash table of symbols. The ECOFF
262 back end searches the archive itself to avoid the overhead of
263 creating a new hash table.
264
265INODE
266Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions
267SUBSECTION
268 Performing the final link
269
270@cindex _bfd_link_final_link in target vector
271@cindex target vector (_bfd_final_link)
272 When all the input files have been processed, the linker calls
273 the <<_bfd_final_link>> entry point of the output BFD. This
274 routine is responsible for producing the final output file,
275 which has several aspects. It must relocate the contents of
276 the input sections and copy the data into the output sections.
277 It must build an output symbol table including any local
278 symbols from the input files and the global symbols from the
279 hash table. When producing relocateable output, it must
280 modify the input relocs and write them into the output file.
281 There may also be object format dependent work to be done.
282
283 The linker will also call the <<write_object_contents>> entry
284 point when the BFD is closed. The two entry points must work
285 together in order to produce the correct output file.
286
287 The details of how this works are inevitably dependent upon
288 the specific object file format. The a.out
289 <<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
290
291@menu
292@* Information provided by the linker::
293@* Relocating the section contents::
294@* Writing the symbol table::
295@end menu
296
297INODE
298Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
299SUBSUBSECTION
300 Information provided by the linker
301
302 Before the linker calls the <<_bfd_final_link>> entry point,
303 it sets up some data structures for the function to use.
304
305 The <<input_bfds>> field of the <<bfd_link_info>> structure
306 will point to a list of all the input files included in the
307 link. These files are linked through the <<link_next>> field
308 of the <<bfd>> structure.
309
310 Each section in the output file will have a list of
311 <<link_order>> structures attached to the <<link_order_head>>
312 field (the <<link_order>> structure is defined in
313 <<bfdlink.h>>). These structures describe how to create the
314 contents of the output section in terms of the contents of
315 various input sections, fill constants, and, eventually, other
4335ce64
ILT
316 types of information. They also describe relocs that must be
317 created by the BFD backend, but do not correspond to any input
318 file; this is used to support -Ur, which builds constructors
319 while generating a relocateable object file.
d4366f97
ILT
320
321INODE
322Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
323SUBSUBSECTION
324 Relocating the section contents
325
326 The <<_bfd_final_link>> function should look through the
327 <<link_order>> structures attached to each section of the
328 output file. Each <<link_order>> structure should either be
329 handled specially, or it should be passed to the function
330 <<_bfd_default_link_order>> which will do the right thing
331 (<<_bfd_default_link_order>> is defined in <<linker.c>>).
332
333 For efficiency, a <<link_order>> of type
334 <<bfd_indirect_link_order>> whose associated section belongs
335 to a BFD of the same format as the output BFD must be handled
336 specially. This type of <<link_order>> describes part of an
337 output section in terms of a section belonging to one of the
338 input files. The <<_bfd_final_link>> function should read the
339 contents of the section and any associated relocs, apply the
340 relocs to the section contents, and write out the modified
341 section contents. If performing a relocateable link, the
342 relocs themselves must also be modified and written out.
343
344@findex _bfd_relocate_contents
345@findex _bfd_final_link_relocate
346 The functions <<_bfd_relocate_contents>> and
347 <<_bfd_final_link_relocate>> provide some general support for
348 performing the actual relocations, notably overflow checking.
349 Their arguments include information about the symbol the
350 relocation is against and a <<reloc_howto_type>> argument
351 which describes the relocation to perform. These functions
352 are defined in <<reloc.c>>.
353
354 The a.out function which handles reading, relocating, and
355 writing section contents is <<aout_link_input_section>>. The
356 actual relocation is done in <<aout_link_input_section_std>>
357 and <<aout_link_input_section_ext>>.
358
359INODE
360Writing the symbol table, , Relocating the section contents, Performing the Final Link
361SUBSUBSECTION
362 Writing the symbol table
363
364 The <<_bfd_final_link>> function must gather all the symbols
365 in the input files and write them out. It must also write out
366 all the symbols in the global hash table. This must be
367 controlled by the <<strip>> and <<discard>> fields of the
368 <<bfd_link_info>> structure.
369
370 The local symbols of the input files will not have been
371 entered into the linker hash table. The <<_bfd_final_link>>
372 routine must consider each input file and include the symbols
373 in the output file. It may be convenient to do this when
374 looking through the <<link_order>> structures, or it may be
375 done by stepping through the <<input_bfds>> list.
376
377 The <<_bfd_final_link>> routine must also traverse the global
378 hash table to gather all the externally visible symbols. It
379 is possible that most of the externally visible symbols may be
380 written out when considering the symbols of each input file,
381 but it is still necessary to traverse the hash table since the
382 linker script may have defined some symbols that are not in
6c97aedf 383 any of the input files.
d4366f97
ILT
384
385 The <<strip>> field of the <<bfd_link_info>> structure
386 controls which symbols are written out. The possible values
387 are listed in <<bfdlink.h>>. If the value is <<strip_some>>,
388 then the <<keep_hash>> field of the <<bfd_link_info>>
389 structure is a hash table of symbols to keep; each symbol
390 should be looked up in this hash table, and only symbols which
391 are present should be included in the output file.
392
393 If the <<strip>> field of the <<bfd_link_info>> structure
394 permits local symbols to be written out, the <<discard>> field
395 is used to further controls which local symbols are included
396 in the output file. If the value is <<discard_l>>, then all
397 local symbols which begin with a certain prefix are discarded;
398 this prefix is described by the <<lprefix>> and
399 <<lprefix_len>> fields of the <<bfd_link_info>> structure.
400
401 The a.out backend handles symbols by calling
402 <<aout_link_write_symbols>> on each input BFD and then
403 traversing the global hash table with the function
404 <<aout_link_write_other_symbol>>. It builds a string table
405 while writing out the symbols, which is written to the output
406 file at the end of <<NAME(aout,final_link)>>.
407*/
408
da6b2d99
ILT
409static struct bfd_hash_entry *generic_link_hash_newfunc
410 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
411 const char *));
8e5090ce
ILT
412static boolean generic_link_read_symbols
413 PARAMS ((bfd *));
4335ce64
ILT
414static boolean generic_link_add_symbols
415 PARAMS ((bfd *, struct bfd_link_info *, boolean collect));
da6b2d99 416static boolean generic_link_add_object_symbols
4335ce64
ILT
417 PARAMS ((bfd *, struct bfd_link_info *, boolean collect));
418static boolean generic_link_check_archive_element_no_collect
419 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
420static boolean generic_link_check_archive_element_collect
da6b2d99 421 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
4335ce64
ILT
422static boolean generic_link_check_archive_element
423 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded, boolean collect));
da6b2d99 424static boolean generic_link_add_symbol_list
4335ce64
ILT
425 PARAMS ((bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **,
426 boolean collect));
ae115e51 427static bfd *hash_entry_bfd PARAMS ((struct bfd_link_hash_entry *));
9c26be63
ILT
428static void set_symbol_from_hash
429 PARAMS ((asymbol *, struct bfd_link_hash_entry *));
da6b2d99
ILT
430static boolean generic_add_output_symbol
431 PARAMS ((bfd *, size_t *psymalloc, asymbol *));
432static boolean default_fill_link_order
433 PARAMS ((bfd *, struct bfd_link_info *, asection *,
434 struct bfd_link_order *));
6e07e54f
ILT
435static boolean default_indirect_link_order
436 PARAMS ((bfd *, struct bfd_link_info *, asection *,
9c26be63 437 struct bfd_link_order *, boolean));
da6b2d99
ILT
438
439/* The link hash table structure is defined in bfdlink.h. It provides
440 a base hash table which the backend specific hash tables are built
441 upon. */
442
443/* Routine to create an entry in the link hash table. */
444
445struct bfd_hash_entry *
446_bfd_link_hash_newfunc (entry, table, string)
447 struct bfd_hash_entry *entry;
448 struct bfd_hash_table *table;
449 const char *string;
450{
451 struct bfd_link_hash_entry *ret = (struct bfd_link_hash_entry *) entry;
452
453 /* Allocate the structure if it has not already been allocated by a
454 subclass. */
455 if (ret == (struct bfd_link_hash_entry *) NULL)
456 ret = ((struct bfd_link_hash_entry *)
457 bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry)));
9783e04a 458 if (ret == (struct bfd_link_hash_entry *) NULL)
ae115e51 459 return NULL;
da6b2d99
ILT
460
461 /* Call the allocation method of the superclass. */
462 ret = ((struct bfd_link_hash_entry *)
463 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
464
9783e04a
DM
465 if (ret)
466 {
467 /* Initialize the local fields. */
468 ret->type = bfd_link_hash_new;
9783e04a
DM
469 ret->next = NULL;
470 }
da6b2d99
ILT
471
472 return (struct bfd_hash_entry *) ret;
473}
474
475/* Initialize a link hash table. The BFD argument is the one
476 responsible for creating this table. */
477
478boolean
479_bfd_link_hash_table_init (table, abfd, newfunc)
480 struct bfd_link_hash_table *table;
481 bfd *abfd;
482 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
483 struct bfd_hash_table *,
484 const char *));
485{
486 table->creator = abfd->xvec;
487 table->undefs = NULL;
488 table->undefs_tail = NULL;
489 return bfd_hash_table_init (&table->table, newfunc);
490}
491
492/* Look up a symbol in a link hash table. If follow is true, we
493 follow bfd_link_hash_indirect and bfd_link_hash_warning links to
494 the real symbol. */
495
496struct bfd_link_hash_entry *
497bfd_link_hash_lookup (table, string, create, copy, follow)
498 struct bfd_link_hash_table *table;
499 const char *string;
500 boolean create;
501 boolean copy;
502 boolean follow;
503{
504 struct bfd_link_hash_entry *ret;
505
506 ret = ((struct bfd_link_hash_entry *)
507 bfd_hash_lookup (&table->table, string, create, copy));
508
509 if (follow && ret != (struct bfd_link_hash_entry *) NULL)
510 {
511 while (ret->type == bfd_link_hash_indirect
512 || ret->type == bfd_link_hash_warning)
513 ret = ret->u.i.link;
514 }
515
516 return ret;
517}
518
519/* Traverse a generic link hash table. The only reason this is not a
520 macro is to do better type checking. This code presumes that an
d4366f97 521 argument passed as a struct bfd_hash_entry * may be caught as a
da6b2d99
ILT
522 struct bfd_link_hash_entry * with no explicit cast required on the
523 call. */
524
525void
526bfd_link_hash_traverse (table, func, info)
527 struct bfd_link_hash_table *table;
528 boolean (*func) PARAMS ((struct bfd_link_hash_entry *, PTR));
529 PTR info;
530{
531 bfd_hash_traverse (&table->table,
532 ((boolean (*) PARAMS ((struct bfd_hash_entry *, PTR)))
533 func),
534 info);
535}
536
537/* Add a symbol to the linker hash table undefs list. */
538
539INLINE void
540bfd_link_add_undef (table, h)
541 struct bfd_link_hash_table *table;
542 struct bfd_link_hash_entry *h;
543{
544 BFD_ASSERT (h->next == NULL);
545 if (table->undefs_tail != (struct bfd_link_hash_entry *) NULL)
546 table->undefs_tail->next = h;
547 if (table->undefs == (struct bfd_link_hash_entry *) NULL)
548 table->undefs = h;
549 table->undefs_tail = h;
550}
551\f
552/* Routine to create an entry in an generic link hash table. */
553
554static struct bfd_hash_entry *
555generic_link_hash_newfunc (entry, table, string)
556 struct bfd_hash_entry *entry;
557 struct bfd_hash_table *table;
558 const char *string;
559{
560 struct generic_link_hash_entry *ret =
561 (struct generic_link_hash_entry *) entry;
562
563 /* Allocate the structure if it has not already been allocated by a
564 subclass. */
565 if (ret == (struct generic_link_hash_entry *) NULL)
566 ret = ((struct generic_link_hash_entry *)
567 bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry)));
9783e04a 568 if (ret == (struct generic_link_hash_entry *) NULL)
ae115e51 569 return NULL;
da6b2d99
ILT
570
571 /* Call the allocation method of the superclass. */
572 ret = ((struct generic_link_hash_entry *)
573 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
574 table, string));
575
9783e04a
DM
576 if (ret)
577 {
578 /* Set local fields. */
35fee729 579 ret->written = false;
9783e04a
DM
580 ret->sym = NULL;
581 }
da6b2d99
ILT
582
583 return (struct bfd_hash_entry *) ret;
584}
585
586/* Create an generic link hash table. */
587
588struct bfd_link_hash_table *
589_bfd_generic_link_hash_table_create (abfd)
590 bfd *abfd;
591{
592 struct generic_link_hash_table *ret;
593
594 ret = ((struct generic_link_hash_table *)
e3364701
ILT
595 bfd_alloc (abfd, sizeof (struct generic_link_hash_table)));
596 if (ret == NULL)
a9713b91 597 return (struct bfd_link_hash_table *) NULL;
da6b2d99
ILT
598 if (! _bfd_link_hash_table_init (&ret->root, abfd,
599 generic_link_hash_newfunc))
600 {
601 free (ret);
602 return (struct bfd_link_hash_table *) NULL;
603 }
604 return &ret->root;
605}
8e5090ce
ILT
606
607/* Grab the symbols for an object file when doing a generic link. We
608 store the symbols in the outsymbols field. We need to keep them
609 around for the entire link to ensure that we only read them once.
610 If we read them multiple times, we might wind up with relocs and
611 the hash table pointing to different instances of the symbol
612 structure. */
613
614static boolean
615generic_link_read_symbols (abfd)
616 bfd *abfd;
617{
618 if (abfd->outsymbols == (asymbol **) NULL)
619 {
620 long symsize;
621 long symcount;
622
623 symsize = bfd_get_symtab_upper_bound (abfd);
624 if (symsize < 0)
625 return false;
626 abfd->outsymbols = (asymbol **) bfd_alloc (abfd, symsize);
627 if (abfd->outsymbols == NULL && symsize != 0)
a9713b91 628 return false;
8e5090ce
ILT
629 symcount = bfd_canonicalize_symtab (abfd, abfd->outsymbols);
630 if (symcount < 0)
631 return false;
632 abfd->symcount = symcount;
633 }
634
635 return true;
636}
da6b2d99 637\f
4335ce64
ILT
638/* Generic function to add symbols to from an object file to the
639 global hash table. This version does not automatically collect
640 constructors by name. */
da6b2d99
ILT
641
642boolean
643_bfd_generic_link_add_symbols (abfd, info)
644 bfd *abfd;
645 struct bfd_link_info *info;
4335ce64
ILT
646{
647 return generic_link_add_symbols (abfd, info, false);
648}
649
650/* Generic function to add symbols from an object file to the global
651 hash table. This version automatically collects constructors by
652 name, as the collect2 program does. It should be used for any
653 target which does not provide some other mechanism for setting up
654 constructors and destructors; these are approximately those targets
655 for which gcc uses collect2 and do not support stabs. */
656
657boolean
658_bfd_generic_link_add_symbols_collect (abfd, info)
659 bfd *abfd;
660 struct bfd_link_info *info;
661{
662 return generic_link_add_symbols (abfd, info, true);
663}
664
665/* Add symbols from an object file to the global hash table. */
666
667static boolean
668generic_link_add_symbols (abfd, info, collect)
669 bfd *abfd;
670 struct bfd_link_info *info;
671 boolean collect;
da6b2d99
ILT
672{
673 boolean ret;
674
675 switch (bfd_get_format (abfd))
676 {
677 case bfd_object:
4335ce64 678 ret = generic_link_add_object_symbols (abfd, info, collect);
da6b2d99
ILT
679 break;
680 case bfd_archive:
4335ce64
ILT
681 ret = (_bfd_generic_link_add_archive_symbols
682 (abfd, info,
683 (collect
684 ? generic_link_check_archive_element_collect
685 : generic_link_check_archive_element_no_collect)));
da6b2d99
ILT
686 break;
687 default:
d1ad85a6 688 bfd_set_error (bfd_error_wrong_format);
da6b2d99
ILT
689 ret = false;
690 }
691
da6b2d99
ILT
692 return ret;
693}
694
695/* Add symbols from an object file to the global hash table. */
696
697static boolean
4335ce64 698generic_link_add_object_symbols (abfd, info, collect)
da6b2d99
ILT
699 bfd *abfd;
700 struct bfd_link_info *info;
4335ce64 701 boolean collect;
da6b2d99 702{
8e5090ce
ILT
703 if (! generic_link_read_symbols (abfd))
704 return false;
705 return generic_link_add_symbol_list (abfd, info,
706 _bfd_generic_link_get_symcount (abfd),
707 _bfd_generic_link_get_symbols (abfd),
708 collect);
da6b2d99
ILT
709}
710\f
711/* We build a hash table of all symbols defined in an archive. */
712
713/* An archive symbol may be defined by multiple archive elements.
714 This linked list is used to hold the elements. */
715
716struct archive_list
717{
718 struct archive_list *next;
719 int indx;
720};
721
722/* An entry in an archive hash table. */
723
724struct archive_hash_entry
725{
726 struct bfd_hash_entry root;
727 /* Where the symbol is defined. */
728 struct archive_list *defs;
729};
730
731/* An archive hash table itself. */
732
733struct archive_hash_table
734{
735 struct bfd_hash_table table;
736};
737
738static struct bfd_hash_entry *archive_hash_newfunc
739 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
740static boolean archive_hash_table_init
741 PARAMS ((struct archive_hash_table *,
742 struct bfd_hash_entry *(*) (struct bfd_hash_entry *,
743 struct bfd_hash_table *,
744 const char *)));
745
746/* Create a new entry for an archive hash table. */
747
748static struct bfd_hash_entry *
749archive_hash_newfunc (entry, table, string)
750 struct bfd_hash_entry *entry;
751 struct bfd_hash_table *table;
752 const char *string;
753{
754 struct archive_hash_entry *ret = (struct archive_hash_entry *) entry;
755
756 /* Allocate the structure if it has not already been allocated by a
757 subclass. */
758 if (ret == (struct archive_hash_entry *) NULL)
759 ret = ((struct archive_hash_entry *)
760 bfd_hash_allocate (table, sizeof (struct archive_hash_entry)));
9783e04a 761 if (ret == (struct archive_hash_entry *) NULL)
ae115e51 762 return NULL;
da6b2d99
ILT
763
764 /* Call the allocation method of the superclass. */
765 ret = ((struct archive_hash_entry *)
766 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
767
9783e04a
DM
768 if (ret)
769 {
770 /* Initialize the local fields. */
771 ret->defs = (struct archive_list *) NULL;
772 }
da6b2d99
ILT
773
774 return (struct bfd_hash_entry *) ret;
775}
776
777/* Initialize an archive hash table. */
778
779static boolean
780archive_hash_table_init (table, newfunc)
781 struct archive_hash_table *table;
782 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
783 struct bfd_hash_table *,
784 const char *));
785{
786 return bfd_hash_table_init (&table->table, newfunc);
787}
788
789/* Look up an entry in an archive hash table. */
790
791#define archive_hash_lookup(t, string, create, copy) \
792 ((struct archive_hash_entry *) \
793 bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
794
a537cb21
ILT
795/* Allocate space in an archive hash table. */
796
797#define archive_hash_allocate(t, size) bfd_hash_allocate (&(t)->table, (size))
798
da6b2d99
ILT
799/* Free an archive hash table. */
800
801#define archive_hash_table_free(t) bfd_hash_table_free (&(t)->table)
802
803/* Generic function to add symbols from an archive file to the global
804 hash file. This function presumes that the archive symbol table
805 has already been read in (this is normally done by the
806 bfd_check_format entry point). It looks through the undefined and
807 common symbols and searches the archive symbol table for them. If
808 it finds an entry, it includes the associated object file in the
809 link.
810
811 The old linker looked through the archive symbol table for
812 undefined symbols. We do it the other way around, looking through
813 undefined symbols for symbols defined in the archive. The
814 advantage of the newer scheme is that we only have to look through
815 the list of undefined symbols once, whereas the old method had to
816 re-search the symbol table each time a new object file was added.
817
818 The CHECKFN argument is used to see if an object file should be
819 included. CHECKFN should set *PNEEDED to true if the object file
820 should be included, and must also call the bfd_link_info
821 add_archive_element callback function and handle adding the symbols
822 to the global hash table. CHECKFN should only return false if some
823 sort of error occurs.
824
825 For some formats, such as a.out, it is possible to look through an
826 object file but not actually include it in the link. The
827 archive_pass field in a BFD is used to avoid checking the symbols
828 of an object files too many times. When an object is included in
829 the link, archive_pass is set to -1. If an object is scanned but
830 not included, archive_pass is set to the pass number. The pass
831 number is incremented each time a new object file is included. The
832 pass number is used because when a new object file is included it
833 may create new undefined symbols which cause a previously examined
834 object file to be included. */
835
836boolean
837_bfd_generic_link_add_archive_symbols (abfd, info, checkfn)
838 bfd *abfd;
839 struct bfd_link_info *info;
840 boolean (*checkfn) PARAMS ((bfd *, struct bfd_link_info *,
841 boolean *pneeded));
842{
843 carsym *arsyms;
844 carsym *arsym_end;
845 register carsym *arsym;
846 int pass;
847 struct archive_hash_table arsym_hash;
848 int indx;
849 struct bfd_link_hash_entry **pundef;
850
851 if (! bfd_has_map (abfd))
852 {
040c913e
ILT
853 /* An empty archive is a special case. */
854 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
855 return true;
9675c281 856 bfd_set_error (bfd_error_no_armap);
da6b2d99
ILT
857 return false;
858 }
859
860 arsyms = bfd_ardata (abfd)->symdefs;
861 arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
862
863 /* In order to quickly determine whether an symbol is defined in
864 this archive, we build a hash table of the symbols. */
865 if (! archive_hash_table_init (&arsym_hash, archive_hash_newfunc))
866 return false;
867 for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
868 {
869 struct archive_hash_entry *arh;
ecff0ffe 870 struct archive_list *l, **pp;
da6b2d99
ILT
871
872 arh = archive_hash_lookup (&arsym_hash, arsym->name, true, false);
873 if (arh == (struct archive_hash_entry *) NULL)
f1cca647 874 goto error_return;
a537cb21
ILT
875 l = ((struct archive_list *)
876 archive_hash_allocate (&arsym_hash, sizeof (struct archive_list)));
f1cca647 877 if (l == NULL)
a537cb21 878 goto error_return;
da6b2d99 879 l->indx = indx;
ecff0ffe
ILT
880 for (pp = &arh->defs;
881 *pp != (struct archive_list *) NULL;
882 pp = &(*pp)->next)
883 ;
884 *pp = l;
885 l->next = NULL;
da6b2d99
ILT
886 }
887
9c26be63
ILT
888 /* The archive_pass field in the archive itself is used to
889 initialize PASS, sine we may search the same archive multiple
890 times. */
891 pass = abfd->archive_pass + 1;
da6b2d99
ILT
892
893 /* New undefined symbols are added to the end of the list, so we
894 only need to look through it once. */
895 pundef = &info->hash->undefs;
896 while (*pundef != (struct bfd_link_hash_entry *) NULL)
897 {
898 struct bfd_link_hash_entry *h;
899 struct archive_hash_entry *arh;
900 struct archive_list *l;
901
902 h = *pundef;
903
904 /* When a symbol is defined, it is not necessarily removed from
905 the list. */
906 if (h->type != bfd_link_hash_undefined
907 && h->type != bfd_link_hash_common)
908 {
909 /* Remove this entry from the list, for general cleanliness
910 and because we are going to look through the list again
911 if we search any more libraries. We can't remove the
912 entry if it is the tail, because that would lose any
22aabad5
ILT
913 entries we add to the list later on (it would also cause
914 us to lose track of whether the symbol has been
915 referenced). */
da6b2d99
ILT
916 if (*pundef != info->hash->undefs_tail)
917 *pundef = (*pundef)->next;
918 else
919 pundef = &(*pundef)->next;
920 continue;
921 }
922
923 /* Look for this symbol in the archive symbol map. */
924 arh = archive_hash_lookup (&arsym_hash, h->root.string, false, false);
925 if (arh == (struct archive_hash_entry *) NULL)
926 {
927 pundef = &(*pundef)->next;
928 continue;
929 }
930
931 /* Look at all the objects which define this symbol. */
932 for (l = arh->defs; l != (struct archive_list *) NULL; l = l->next)
933 {
934 bfd *element;
935 boolean needed;
936
937 /* If the symbol has gotten defined along the way, quit. */
938 if (h->type != bfd_link_hash_undefined
939 && h->type != bfd_link_hash_common)
940 break;
941
942 element = bfd_get_elt_at_index (abfd, l->indx);
943 if (element == (bfd *) NULL)
f1cca647 944 goto error_return;
da6b2d99
ILT
945
946 /* If we've already included this element, or if we've
947 already checked it on this pass, continue. */
948 if (element->archive_pass == -1
949 || element->archive_pass == pass)
950 continue;
951
952 /* If we can't figure this element out, just ignore it. */
953 if (! bfd_check_format (element, bfd_object))
954 {
955 element->archive_pass = -1;
956 continue;
957 }
958
959 /* CHECKFN will see if this element should be included, and
960 go ahead and include it if appropriate. */
961 if (! (*checkfn) (element, info, &needed))
f1cca647 962 goto error_return;
da6b2d99
ILT
963
964 if (! needed)
965 element->archive_pass = pass;
966 else
967 {
968 element->archive_pass = -1;
969
970 /* Increment the pass count to show that we may need to
971 recheck object files which were already checked. */
972 ++pass;
973 }
974 }
975
976 pundef = &(*pundef)->next;
977 }
978
979 archive_hash_table_free (&arsym_hash);
980
9c26be63
ILT
981 /* Save PASS in case we are called again. */
982 abfd->archive_pass = pass;
983
da6b2d99 984 return true;
f1cca647
ILT
985
986 error_return:
987 archive_hash_table_free (&arsym_hash);
988 return false;
da6b2d99
ILT
989}
990\f
4335ce64
ILT
991/* See if we should include an archive element. This version is used
992 when we do not want to automatically collect constructors based on
993 the symbol name, presumably because we have some other mechanism
994 for finding them. */
995
996static boolean
997generic_link_check_archive_element_no_collect (abfd, info, pneeded)
998 bfd *abfd;
999 struct bfd_link_info *info;
1000 boolean *pneeded;
1001{
1002 return generic_link_check_archive_element (abfd, info, pneeded, false);
1003}
1004
1005/* See if we should include an archive element. This version is used
1006 when we want to automatically collect constructors based on the
1007 symbol name, as collect2 does. */
1008
1009static boolean
1010generic_link_check_archive_element_collect (abfd, info, pneeded)
1011 bfd *abfd;
1012 struct bfd_link_info *info;
1013 boolean *pneeded;
1014{
1015 return generic_link_check_archive_element (abfd, info, pneeded, true);
1016}
1017
1018/* See if we should include an archive element. Optionally collect
1019 constructors. */
da6b2d99
ILT
1020
1021static boolean
4335ce64 1022generic_link_check_archive_element (abfd, info, pneeded, collect)
da6b2d99
ILT
1023 bfd *abfd;
1024 struct bfd_link_info *info;
1025 boolean *pneeded;
4335ce64 1026 boolean collect;
da6b2d99 1027{
da6b2d99
ILT
1028 asymbol **pp, **ppend;
1029
1030 *pneeded = false;
1031
8e5090ce
ILT
1032 if (! generic_link_read_symbols (abfd))
1033 return false;
da6b2d99 1034
8e5090ce
ILT
1035 pp = _bfd_generic_link_get_symbols (abfd);
1036 ppend = pp + _bfd_generic_link_get_symcount (abfd);
da6b2d99
ILT
1037 for (; pp < ppend; pp++)
1038 {
1039 asymbol *p;
1040 struct bfd_link_hash_entry *h;
1041
1042 p = *pp;
1043
1044 /* We are only interested in globally visible symbols. */
1045 if (! bfd_is_com_section (p->section)
1046 && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0)
1047 continue;
1048
1049 /* We are only interested if we know something about this
1050 symbol, and it is undefined or common. An undefined weak
6c97aedf
ILT
1051 symbol (type bfd_link_hash_undefweak) is not considered to be
1052 a reference when pulling files out of an archive. See the
1053 SVR4 ABI, p. 4-27. */
da6b2d99
ILT
1054 h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), false,
1055 false, true);
1056 if (h == (struct bfd_link_hash_entry *) NULL
1057 || (h->type != bfd_link_hash_undefined
1058 && h->type != bfd_link_hash_common))
1059 continue;
1060
1061 /* P is a symbol we are looking for. */
1062
1063 if (! bfd_is_com_section (p->section))
1064 {
8e5090ce
ILT
1065 bfd_size_type symcount;
1066 asymbol **symbols;
1067
da6b2d99
ILT
1068 /* This object file defines this symbol, so pull it in. */
1069 if (! (*info->callbacks->add_archive_element) (info, abfd,
1070 bfd_asymbol_name (p)))
8e5090ce
ILT
1071 return false;
1072 symcount = _bfd_generic_link_get_symcount (abfd);
1073 symbols = _bfd_generic_link_get_symbols (abfd);
1074 if (! generic_link_add_symbol_list (abfd, info, symcount,
4335ce64 1075 symbols, collect))
8e5090ce 1076 return false;
da6b2d99 1077 *pneeded = true;
8e5090ce 1078 return true;
da6b2d99
ILT
1079 }
1080
1081 /* P is a common symbol. */
1082
1083 if (h->type == bfd_link_hash_undefined)
1084 {
1085 bfd *symbfd;
9c26be63
ILT
1086 bfd_vma size;
1087 unsigned int power;
da6b2d99
ILT
1088
1089 symbfd = h->u.undef.abfd;
1090 if (symbfd == (bfd *) NULL)
1091 {
1092 /* This symbol was created as undefined from outside
1093 BFD. We assume that we should link in the object
1094 file. This is for the -u option in the linker. */
1095 if (! (*info->callbacks->add_archive_element)
1096 (info, abfd, bfd_asymbol_name (p)))
8e5090ce 1097 return false;
da6b2d99 1098 *pneeded = true;
8e5090ce 1099 return true;
da6b2d99
ILT
1100 }
1101
1102 /* Turn the symbol into a common symbol but do not link in
1103 the object file. This is how a.out works. Object
1104 formats that require different semantics must implement
1105 this function differently. This symbol is already on the
6e07e54f
ILT
1106 undefs list. We add the section to a common section
1107 attached to symbfd to ensure that it is in a BFD which
1108 will be linked in. */
da6b2d99 1109 h->type = bfd_link_hash_common;
6ff9c051
ILT
1110 h->u.c.p =
1111 ((struct bfd_link_hash_common_entry *)
1112 bfd_hash_allocate (&info->hash->table,
1113 sizeof (struct bfd_link_hash_common_entry)));
1114 if (h->u.c.p == NULL)
1115 return false;
9c26be63
ILT
1116
1117 size = bfd_asymbol_value (p);
1118 h->u.c.size = size;
9c26be63
ILT
1119
1120 power = bfd_log2 (size);
1121 if (power > 4)
1122 power = 4;
6ff9c051 1123 h->u.c.p->alignment_power = power;
9c26be63 1124
a537cb21 1125 if (p->section == bfd_com_section_ptr)
6ff9c051 1126 h->u.c.p->section = bfd_make_section_old_way (symbfd, "COMMON");
6e07e54f 1127 else
6ff9c051
ILT
1128 h->u.c.p->section = bfd_make_section_old_way (symbfd,
1129 p->section->name);
1130 h->u.c.p->section->flags = SEC_ALLOC;
da6b2d99
ILT
1131 }
1132 else
1133 {
1134 /* Adjust the size of the common symbol if necessary. This
1135 is how a.out works. Object formats that require
1136 different semantics must implement this function
1137 differently. */
1138 if (bfd_asymbol_value (p) > h->u.c.size)
1139 h->u.c.size = bfd_asymbol_value (p);
1140 }
1141 }
1142
1143 /* This archive element is not needed. */
1144 return true;
1145}
1146
4335ce64
ILT
1147/* Add the symbols from an object file to the global hash table. ABFD
1148 is the object file. INFO is the linker information. SYMBOL_COUNT
1149 is the number of symbols. SYMBOLS is the list of symbols. COLLECT
1150 is true if constructors should be automatically collected by name
1151 as is done by collect2. */
da6b2d99
ILT
1152
1153static boolean
4335ce64 1154generic_link_add_symbol_list (abfd, info, symbol_count, symbols, collect)
da6b2d99
ILT
1155 bfd *abfd;
1156 struct bfd_link_info *info;
1157 bfd_size_type symbol_count;
1158 asymbol **symbols;
4335ce64 1159 boolean collect;
da6b2d99
ILT
1160{
1161 asymbol **pp, **ppend;
1162
1163 pp = symbols;
1164 ppend = symbols + symbol_count;
1165 for (; pp < ppend; pp++)
1166 {
1167 asymbol *p;
1168
1169 p = *pp;
1170
1171 if ((p->flags & (BSF_INDIRECT
1172 | BSF_WARNING
1173 | BSF_GLOBAL
1174 | BSF_CONSTRUCTOR
1175 | BSF_WEAK)) != 0
a537cb21 1176 || bfd_is_und_section (bfd_get_section (p))
da6b2d99 1177 || bfd_is_com_section (bfd_get_section (p))
a537cb21 1178 || bfd_is_ind_section (bfd_get_section (p)))
da6b2d99
ILT
1179 {
1180 const char *name;
1181 const char *string;
1182 struct generic_link_hash_entry *h;
1183
1184 name = bfd_asymbol_name (p);
a9713b91
ILT
1185 if (((p->flags & BSF_INDIRECT) != 0
1186 || bfd_is_ind_section (p->section))
1187 && pp + 1 < ppend)
1188 {
1189 pp++;
1190 string = bfd_asymbol_name (*pp);
1191 }
1192 else if ((p->flags & BSF_WARNING) != 0
1193 && pp + 1 < ppend)
da6b2d99
ILT
1194 {
1195 /* The name of P is actually the warning string, and the
a9713b91 1196 next symbol is the one to warn about. */
da6b2d99 1197 string = name;
a9713b91
ILT
1198 pp++;
1199 name = bfd_asymbol_name (*pp);
da6b2d99
ILT
1200 }
1201 else
1202 string = NULL;
6e07e54f 1203
8e5090ce 1204 h = NULL;
da6b2d99
ILT
1205 if (! (_bfd_generic_link_add_one_symbol
1206 (info, abfd, name, p->flags, bfd_get_section (p),
4335ce64 1207 p->value, string, false, collect,
da6b2d99
ILT
1208 (struct bfd_link_hash_entry **) &h)))
1209 return false;
1210
6ff9c051
ILT
1211 /* If this is a constructor symbol, and the linker didn't do
1212 anything with it, then we want to just pass the symbol
1213 through to the output file. This will happen when
1214 linking with -r. */
1215 if ((p->flags & BSF_CONSTRUCTOR) != 0
1216 && (h == NULL || h->root.type == bfd_link_hash_new))
1217 {
1218 p->udata.p = NULL;
1219 continue;
1220 }
1221
da6b2d99
ILT
1222 /* Save the BFD symbol so that we don't lose any backend
1223 specific information that may be attached to it. We only
1224 want this one if it gives more information than the
1225 existing one; we don't want to replace a defined symbol
1226 with an undefined one. This routine may be called with a
1227 hash table other than the generic hash table, so we only
1228 do this if we are certain that the hash table is a
1229 generic one. */
1230 if (info->hash->creator == abfd->xvec)
1231 {
1232 if (h->sym == (asymbol *) NULL
a537cb21 1233 || (! bfd_is_und_section (bfd_get_section (p))
da6b2d99 1234 && (! bfd_is_com_section (bfd_get_section (p))
a537cb21 1235 || bfd_is_und_section (bfd_get_section (h->sym)))))
c3156966
ILT
1236 {
1237 h->sym = p;
1238 /* BSF_OLD_COMMON is a hack to support COFF reloc
1239 reading, and it should go away when the COFF
1240 linker is switched to the new version. */
1241 if (bfd_is_com_section (bfd_get_section (p)))
1242 p->flags |= BSF_OLD_COMMON;
1243 }
8e5090ce
ILT
1244
1245 /* Store a back pointer from the symbol to the hash
1246 table entry for the benefit of relaxation code until
9c26be63
ILT
1247 it gets rewritten to not use asymbol structures.
1248 Setting this is also used to check whether these
1249 symbols were set up by the generic linker. */
1250 p->udata.p = (PTR) h;
da6b2d99
ILT
1251 }
1252 }
1253 }
1254
1255 return true;
1256}
1257\f
1258/* We use a state table to deal with adding symbols from an object
1259 file. The first index into the state table describes the symbol
1260 from the object file. The second index into the state table is the
1261 type of the symbol in the hash table. */
1262
1263/* The symbol from the object file is turned into one of these row
1264 values. */
1265
1266enum link_row
1267{
1268 UNDEF_ROW, /* Undefined. */
1269 UNDEFW_ROW, /* Weak undefined. */
1270 DEF_ROW, /* Defined. */
1271 DEFW_ROW, /* Weak defined. */
1272 COMMON_ROW, /* Common. */
1273 INDR_ROW, /* Indirect. */
1274 WARN_ROW, /* Warning. */
1275 SET_ROW /* Member of set. */
1276};
1277
2e66a627
KR
1278/* apparently needed for Hitachi 3050R(HI-UX/WE2)? */
1279#undef FAIL
1280
da6b2d99
ILT
1281/* The actions to take in the state table. */
1282
1283enum link_action
1284{
1285 FAIL, /* Abort. */
1286 UND, /* Mark symbol undefined. */
1287 WEAK, /* Mark symbol weak undefined. */
1288 DEF, /* Mark symbol defined. */
6c97aedf 1289 DEFW, /* Mark symbol weak defined. */
da6b2d99 1290 COM, /* Mark symbol common. */
22aabad5 1291 REF, /* Mark defined symbol referenced. */
da6b2d99
ILT
1292 CREF, /* Possibly warn about common reference to defined symbol. */
1293 CDEF, /* Define existing common symbol. */
1294 NOACT, /* No action. */
1295 BIG, /* Mark symbol common using largest size. */
1296 MDEF, /* Multiple definition error. */
22aabad5 1297 MIND, /* Multiple indirect symbols. */
da6b2d99 1298 IND, /* Make indirect symbol. */
9c26be63 1299 CIND, /* Make indirect symbol from existing common symbol. */
da6b2d99
ILT
1300 SET, /* Add value to set. */
1301 MWARN, /* Make warning symbol. */
1302 WARN, /* Issue warning. */
22aabad5 1303 CWARN, /* Warn if referenced, else MWARN. */
da6b2d99 1304 CYCLE, /* Repeat with symbol pointed to. */
22aabad5 1305 REFC, /* Mark indirect symbol referenced and then CYCLE. */
da6b2d99
ILT
1306 WARNC /* Issue warning and then CYCLE. */
1307};
1308
1309/* The state table itself. The first index is a link_row and the
1310 second index is a bfd_link_hash_type. */
1311
6c97aedf 1312static const enum link_action link_action[8][8] =
da6b2d99 1313{
6c97aedf 1314 /* current\prev new undef undefw def defw com indr warn */
ae115e51 1315 /* UNDEF_ROW */ {UND, NOACT, UND, REF, REF, NOACT, REFC, WARNC },
6ff9c051 1316 /* UNDEFW_ROW */ {WEAK, NOACT, NOACT, REF, REF, NOACT, REFC, WARNC },
6c97aedf
ILT
1317 /* DEF_ROW */ {DEF, DEF, DEF, MDEF, DEF, CDEF, MDEF, CYCLE },
1318 /* DEFW_ROW */ {DEFW, DEFW, DEFW, NOACT, NOACT, NOACT, NOACT, CYCLE },
1319 /* COMMON_ROW */ {COM, COM, COM, CREF, CREF, BIG, CREF, WARNC },
1320 /* INDR_ROW */ {IND, IND, IND, MDEF, IND, CIND, MIND, CYCLE },
1321 /* WARN_ROW */ {MWARN, WARN, WARN, CWARN, CWARN, WARN, CWARN, CYCLE },
1322 /* SET_ROW */ {SET, SET, SET, SET, SET, SET, CYCLE, CYCLE }
da6b2d99
ILT
1323};
1324
22aabad5
ILT
1325/* Most of the entries in the LINK_ACTION table are straightforward,
1326 but a few are somewhat subtle.
1327
1328 A reference to an indirect symbol (UNDEF_ROW/indr or
1329 UNDEFW_ROW/indr) is counted as a reference both to the indirect
1330 symbol and to the symbol the indirect symbol points to.
1331
1332 A reference to a warning symbol (UNDEF_ROW/warn or UNDEFW_ROW/warn)
1333 causes the warning to be issued.
1334
1335 A common definition of an indirect symbol (COMMON_ROW/indr) is
1336 treated as a multiple definition error. Likewise for an indirect
1337 definition of a common symbol (INDR_ROW/com).
1338
1339 An indirect definition of a warning (INDR_ROW/warn) does not cause
1340 the warning to be issued.
1341
1342 If a warning is created for an indirect symbol (WARN_ROW/indr) no
1343 warning is created for the symbol the indirect symbol points to.
1344
1345 Adding an entry to a set does not count as a reference to a set,
1346 and no warning is issued (SET_ROW/warn). */
1347
ae115e51
ILT
1348/* Return the BFD in which a hash entry has been defined, if known. */
1349
1350static bfd *
1351hash_entry_bfd (h)
1352 struct bfd_link_hash_entry *h;
1353{
1354 while (h->type == bfd_link_hash_warning)
1355 h = h->u.i.link;
1356 switch (h->type)
1357 {
1358 default:
1359 return NULL;
1360 case bfd_link_hash_undefined:
1361 case bfd_link_hash_undefweak:
1362 return h->u.undef.abfd;
1363 case bfd_link_hash_defined:
1364 case bfd_link_hash_defweak:
1365 return h->u.def.section->owner;
1366 case bfd_link_hash_common:
1367 return h->u.c.p->section->owner;
1368 }
1369 /*NOTREACHED*/
1370}
1371
da6b2d99
ILT
1372/* Add a symbol to the global hash table.
1373 ABFD is the BFD the symbol comes from.
1374 NAME is the name of the symbol.
1375 FLAGS is the BSF_* bits associated with the symbol.
1376 SECTION is the section in which the symbol is defined; this may be
a537cb21 1377 bfd_und_section_ptr or bfd_com_section_ptr.
da6b2d99
ILT
1378 VALUE is the value of the symbol, relative to the section.
1379 STRING is used for either an indirect symbol, in which case it is
1380 the name of the symbol to indirect to, or a warning symbol, in
1381 which case it is the warning string.
1382 COPY is true if NAME or STRING must be copied into locally
1383 allocated memory if they need to be saved.
4335ce64
ILT
1384 COLLECT is true if we should automatically collect gcc constructor
1385 or destructor names as collect2 does.
da6b2d99 1386 HASHP, if not NULL, is a place to store the created hash table
8e5090ce 1387 entry; if *HASHP is not NULL, the caller has already looked up
eeb8c187 1388 the hash table entry, and stored it in *HASHP. */
da6b2d99
ILT
1389
1390boolean
1391_bfd_generic_link_add_one_symbol (info, abfd, name, flags, section, value,
4335ce64 1392 string, copy, collect, hashp)
da6b2d99
ILT
1393 struct bfd_link_info *info;
1394 bfd *abfd;
1395 const char *name;
1396 flagword flags;
1397 asection *section;
1398 bfd_vma value;
1399 const char *string;
1400 boolean copy;
4335ce64 1401 boolean collect;
da6b2d99
ILT
1402 struct bfd_link_hash_entry **hashp;
1403{
1404 enum link_row row;
1405 struct bfd_link_hash_entry *h;
1406 boolean cycle;
1407
a537cb21 1408 if (bfd_is_ind_section (section)
da6b2d99
ILT
1409 || (flags & BSF_INDIRECT) != 0)
1410 row = INDR_ROW;
1411 else if ((flags & BSF_WARNING) != 0)
1412 row = WARN_ROW;
1413 else if ((flags & BSF_CONSTRUCTOR) != 0)
1414 row = SET_ROW;
a537cb21 1415 else if (bfd_is_und_section (section))
da6b2d99
ILT
1416 {
1417 if ((flags & BSF_WEAK) != 0)
1418 row = UNDEFW_ROW;
1419 else
1420 row = UNDEF_ROW;
1421 }
1422 else if ((flags & BSF_WEAK) != 0)
1423 row = DEFW_ROW;
1424 else if (bfd_is_com_section (section))
1425 row = COMMON_ROW;
1426 else
1427 row = DEF_ROW;
1428
8e5090ce 1429 if (hashp != NULL && *hashp != NULL)
da6b2d99 1430 {
8e5090ce 1431 h = *hashp;
420e63c6 1432 BFD_ASSERT (strcmp (h->root.string, name) == 0);
8e5090ce
ILT
1433 }
1434 else
1435 {
1436 h = bfd_link_hash_lookup (info->hash, name, true, copy, false);
1437 if (h == NULL)
1438 {
1439 if (hashp != NULL)
1440 *hashp = NULL;
1441 return false;
1442 }
da6b2d99
ILT
1443 }
1444
1445 if (info->notice_hash != (struct bfd_hash_table *) NULL
1446 && (bfd_hash_lookup (info->notice_hash, name, false, false)
1447 != (struct bfd_hash_entry *) NULL))
1448 {
1449 if (! (*info->callbacks->notice) (info, name, abfd, section, value))
1450 return false;
1451 }
1452
1453 if (hashp != (struct bfd_link_hash_entry **) NULL)
1454 *hashp = h;
1455
1456 do
1457 {
1458 enum link_action action;
1459
1460 cycle = false;
1461 action = link_action[(int) row][(int) h->type];
1462 switch (action)
1463 {
1464 case FAIL:
1465 abort ();
22aabad5
ILT
1466
1467 case NOACT:
1468 /* Do nothing. */
1469 break;
1470
da6b2d99 1471 case UND:
22aabad5 1472 /* Make a new undefined symbol. */
da6b2d99
ILT
1473 h->type = bfd_link_hash_undefined;
1474 h->u.undef.abfd = abfd;
1475 bfd_link_add_undef (info->hash, h);
1476 break;
22aabad5 1477
da6b2d99 1478 case WEAK:
22aabad5 1479 /* Make a new weak undefined symbol. */
6c97aedf 1480 h->type = bfd_link_hash_undefweak;
da6b2d99
ILT
1481 h->u.undef.abfd = abfd;
1482 break;
22aabad5 1483
da6b2d99 1484 case CDEF:
22aabad5
ILT
1485 /* We have found a definition for a symbol which was
1486 previously common. */
da6b2d99
ILT
1487 BFD_ASSERT (h->type == bfd_link_hash_common);
1488 if (! ((*info->callbacks->multiple_common)
1489 (info, name,
6ff9c051 1490 h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
da6b2d99
ILT
1491 abfd, bfd_link_hash_defined, (bfd_vma) 0)))
1492 return false;
1493 /* Fall through. */
1494 case DEF:
6c97aedf
ILT
1495 case DEFW:
1496 {
a9713b91 1497 enum bfd_link_hash_type oldtype;
6e07e54f 1498
6c97aedf
ILT
1499 /* Define a symbol. */
1500 oldtype = h->type;
1501 if (action == DEFW)
1502 h->type = bfd_link_hash_defweak;
1503 else
1504 h->type = bfd_link_hash_defined;
1505 h->u.def.section = section;
1506 h->u.def.value = value;
1507
1508 /* If we have been asked to, we act like collect2 and
1509 identify all functions that might be global
1510 constructors and destructors and pass them up in a
1511 callback. We only do this for certain object file
1512 types, since many object file types can handle this
1513 automatically. */
1514 if (collect && name[0] == '_')
1515 {
1516 const char *s;
1517
1518 /* A constructor or destructor name starts like this:
1519 _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
1520 the second are the same character (we accept any
1521 character there, in case a new object file format
1522 comes along with even worse naming restrictions). */
6e07e54f
ILT
1523
1524#define CONS_PREFIX "GLOBAL_"
1525#define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
1526
6c97aedf
ILT
1527 s = name + 1;
1528 while (*s == '_')
1529 ++s;
1530 if (s[0] == 'G'
1531 && strncmp (s, CONS_PREFIX, CONS_PREFIX_LEN - 1) == 0)
1532 {
1533 char c;
1534
1535 c = s[CONS_PREFIX_LEN + 1];
1536 if ((c == 'I' || c == 'D')
1537 && s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
1538 {
1539 /* If this is a definition of a symbol which
1540 was previously weakly defined, we are in
1541 trouble. We have already added a
1542 constructor entry for the weak defined
1543 symbol, and now we are trying to add one
1544 for the new symbol. Fortunately, this case
1545 should never arise in practice. */
1546 if (oldtype == bfd_link_hash_defweak)
1547 abort ();
1548
1549 if (! ((*info->callbacks->constructor)
1550 (info,
1551 c == 'I' ? true : false,
1552 name, abfd, section, value)))
1553 return false;
1554 }
1555 }
1556 }
1557 }
6e07e54f 1558
da6b2d99 1559 break;
22aabad5 1560
da6b2d99 1561 case COM:
22aabad5 1562 /* We have found a common definition for a symbol. */
da6b2d99
ILT
1563 if (h->type == bfd_link_hash_new)
1564 bfd_link_add_undef (info->hash, h);
1565 h->type = bfd_link_hash_common;
6ff9c051
ILT
1566 h->u.c.p =
1567 ((struct bfd_link_hash_common_entry *)
1568 bfd_hash_allocate (&info->hash->table,
1569 sizeof (struct bfd_link_hash_common_entry)));
1570 if (h->u.c.p == NULL)
1571 return false;
1572
da6b2d99 1573 h->u.c.size = value;
9c26be63
ILT
1574
1575 /* Select a default alignment based on the size. This may
1576 be overridden by the caller. */
1577 {
1578 unsigned int power;
1579
1580 power = bfd_log2 (value);
1581 if (power > 4)
1582 power = 4;
6ff9c051 1583 h->u.c.p->alignment_power = power;
9c26be63
ILT
1584 }
1585
1586 /* The section of a common symbol is only used if the common
1587 symbol is actually allocated. It basically provides a
1588 hook for the linker script to decide which output section
1589 the common symbols should be put in. In most cases, the
1590 section of a common symbol will be bfd_com_section_ptr,
1591 the code here will choose a common symbol section named
1592 "COMMON", and the linker script will contain *(COMMON) in
1593 the appropriate place. A few targets use separate common
1594 sections for small symbols, and they require special
1595 handling. */
a537cb21 1596 if (section == bfd_com_section_ptr)
a20bdb43 1597 {
6ff9c051
ILT
1598 h->u.c.p->section = bfd_make_section_old_way (abfd, "COMMON");
1599 h->u.c.p->section->flags = SEC_ALLOC;
a20bdb43 1600 }
da6b2d99 1601 else if (section->owner != abfd)
a20bdb43 1602 {
6ff9c051
ILT
1603 h->u.c.p->section = bfd_make_section_old_way (abfd,
1604 section->name);
1605 h->u.c.p->section->flags = SEC_ALLOC;
a20bdb43 1606 }
da6b2d99 1607 else
6ff9c051 1608 h->u.c.p->section = section;
da6b2d99 1609 break;
22aabad5
ILT
1610
1611 case REF:
1612 /* A reference to a defined symbol. */
1613 if (h->next == NULL && info->hash->undefs_tail != h)
1614 h->next = h;
da6b2d99 1615 break;
22aabad5 1616
da6b2d99 1617 case BIG:
22aabad5
ILT
1618 /* We have found a common definition for a symbol which
1619 already had a common definition. Use the maximum of the
1620 two sizes. */
da6b2d99
ILT
1621 BFD_ASSERT (h->type == bfd_link_hash_common);
1622 if (! ((*info->callbacks->multiple_common)
1623 (info, name,
6ff9c051 1624 h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
da6b2d99
ILT
1625 abfd, bfd_link_hash_common, value)))
1626 return false;
1627 if (value > h->u.c.size)
9c26be63
ILT
1628 {
1629 unsigned int power;
1630
1631 h->u.c.size = value;
1632
1633 /* Select a default alignment based on the size. This may
1634 be overridden by the caller. */
1635 power = bfd_log2 (value);
1636 if (power > 4)
1637 power = 4;
6ff9c051 1638 h->u.c.p->alignment_power = power;
9c26be63 1639 }
da6b2d99 1640 break;
22aabad5 1641
da6b2d99 1642 case CREF:
9c26be63
ILT
1643 {
1644 bfd *obfd;
1645
1646 /* We have found a common definition for a symbol which
1647 was already defined. FIXME: It would nice if we could
1648 report the BFD which defined an indirect symbol, but we
1649 don't have anywhere to store the information. */
6c97aedf
ILT
1650 if (h->type == bfd_link_hash_defined
1651 || h->type == bfd_link_hash_defweak)
9c26be63
ILT
1652 obfd = h->u.def.section->owner;
1653 else
1654 obfd = NULL;
1655 if (! ((*info->callbacks->multiple_common)
1656 (info, name, obfd, h->type, (bfd_vma) 0,
1657 abfd, bfd_link_hash_common, value)))
1658 return false;
1659 }
da6b2d99 1660 break;
22aabad5
ILT
1661
1662 case MIND:
1663 /* Multiple indirect symbols. This is OK if they both point
1664 to the same symbol. */
1665 if (strcmp (h->u.i.link->root.string, string) == 0)
1666 break;
1667 /* Fall through. */
da6b2d99 1668 case MDEF:
22aabad5 1669 /* Handle a multiple definition. */
da6b2d99
ILT
1670 {
1671 asection *msec;
1672 bfd_vma mval;
1673
1674 switch (h->type)
1675 {
1676 case bfd_link_hash_defined:
1677 msec = h->u.def.section;
1678 mval = h->u.def.value;
1679 break;
da6b2d99 1680 case bfd_link_hash_indirect:
a537cb21 1681 msec = bfd_ind_section_ptr;
da6b2d99
ILT
1682 mval = 0;
1683 break;
1684 default:
1685 abort ();
1686 }
040c913e
ILT
1687
1688 /* Ignore a redefinition of an absolute symbol to the same
1689 value; it's harmless. */
1690 if (h->type == bfd_link_hash_defined
1691 && bfd_is_abs_section (msec)
1692 && bfd_is_abs_section (section)
1693 && value == mval)
1694 break;
1695
da6b2d99
ILT
1696 if (! ((*info->callbacks->multiple_definition)
1697 (info, name, msec->owner, msec, mval, abfd, section,
1698 value)))
1699 return false;
1700 }
1701 break;
22aabad5 1702
9c26be63
ILT
1703 case CIND:
1704 /* Create an indirect symbol from an existing common symbol. */
1705 BFD_ASSERT (h->type == bfd_link_hash_common);
1706 if (! ((*info->callbacks->multiple_common)
1707 (info, name,
6ff9c051 1708 h->u.c.p->section->owner, bfd_link_hash_common, h->u.c.size,
9c26be63
ILT
1709 abfd, bfd_link_hash_indirect, (bfd_vma) 0)))
1710 return false;
1711 /* Fall through. */
da6b2d99 1712 case IND:
22aabad5 1713 /* Create an indirect symbol. */
da6b2d99
ILT
1714 {
1715 struct bfd_link_hash_entry *inh;
1716
1717 /* STRING is the name of the symbol we want to indirect
1718 to. */
1719 inh = bfd_link_hash_lookup (info->hash, string, true, copy,
1720 false);
1721 if (inh == (struct bfd_link_hash_entry *) NULL)
1722 return false;
1723 if (inh->type == bfd_link_hash_new)
1724 {
1725 inh->type = bfd_link_hash_undefined;
1726 inh->u.undef.abfd = abfd;
1727 bfd_link_add_undef (info->hash, inh);
1728 }
22aabad5
ILT
1729
1730 /* If the indirect symbol has been referenced, we need to
1731 push the reference down to the symbol we are
1732 referencing. */
1733 if (h->type != bfd_link_hash_new)
1734 {
1735 row = UNDEF_ROW;
1736 cycle = true;
1737 }
1738
da6b2d99
ILT
1739 h->type = bfd_link_hash_indirect;
1740 h->u.i.link = inh;
1741 }
1742 break;
22aabad5 1743
da6b2d99 1744 case SET:
22aabad5 1745 /* Add an entry to a set. */
f1cca647
ILT
1746 if (! (*info->callbacks->add_to_set) (info, h, BFD_RELOC_CTOR,
1747 abfd, section, value))
da6b2d99
ILT
1748 return false;
1749 break;
22aabad5 1750
da6b2d99 1751 case WARNC:
22aabad5 1752 /* Issue a warning and cycle. */
da6b2d99
ILT
1753 if (h->u.i.warning != NULL)
1754 {
4ca63811 1755 if (! (*info->callbacks->warning) (info, h->u.i.warning, name,
ae115e51
ILT
1756 abfd, (asection *) NULL,
1757 (bfd_vma) 0))
da6b2d99
ILT
1758 return false;
1759 /* Only issue a warning once. */
1760 h->u.i.warning = NULL;
1761 }
da6b2d99
ILT
1762 /* Fall through. */
1763 case CYCLE:
22aabad5
ILT
1764 /* Try again with the referenced symbol. */
1765 h = h->u.i.link;
1766 cycle = true;
1767 break;
1768
1769 case REFC:
1770 /* A reference to an indirect symbol. */
1771 if (h->next == NULL && info->hash->undefs_tail != h)
1772 h->next = h;
da6b2d99
ILT
1773 h = h->u.i.link;
1774 cycle = true;
1775 break;
22aabad5
ILT
1776
1777 case WARN:
1778 /* Issue a warning. */
4ca63811 1779 if (! (*info->callbacks->warning) (info, string, name,
ae115e51
ILT
1780 hash_entry_bfd (h),
1781 (asection *) NULL, (bfd_vma) 0))
22aabad5
ILT
1782 return false;
1783 break;
1784
1785 case CWARN:
1786 /* Warn if this symbol has been referenced already,
6c97aedf
ILT
1787 otherwise add a warning. A symbol has been referenced if
1788 the next field is not NULL, or it is the tail of the
1789 undefined symbol list. The REF case above helps to
1790 ensure this. */
22aabad5
ILT
1791 if (h->next != NULL || info->hash->undefs_tail == h)
1792 {
4ca63811 1793 if (! (*info->callbacks->warning) (info, string, name,
ae115e51
ILT
1794 hash_entry_bfd (h),
1795 (asection *) NULL,
1796 (bfd_vma) 0))
22aabad5
ILT
1797 return false;
1798 break;
1799 }
1800 /* Fall through. */
da6b2d99 1801 case MWARN:
22aabad5 1802 /* Make a warning symbol. */
da6b2d99
ILT
1803 {
1804 struct bfd_link_hash_entry *sub;
1805
1806 /* STRING is the warning to give. */
1807 sub = ((struct bfd_link_hash_entry *)
ae115e51
ILT
1808 ((*info->hash->table.newfunc)
1809 ((struct bfd_hash_entry *) NULL, &info->hash->table,
1810 h->root.string)));
1811 if (sub == NULL)
1812 return false;
da6b2d99 1813 *sub = *h;
ae115e51
ILT
1814 sub->type = bfd_link_hash_warning;
1815 sub->u.i.link = h;
da6b2d99 1816 if (! copy)
ae115e51 1817 sub->u.i.warning = string;
da6b2d99
ILT
1818 else
1819 {
1820 char *w;
1821
1822 w = bfd_hash_allocate (&info->hash->table,
1823 strlen (string) + 1);
ae115e51
ILT
1824 if (w == NULL)
1825 return false;
da6b2d99 1826 strcpy (w, string);
ae115e51 1827 sub->u.i.warning = w;
da6b2d99 1828 }
ae115e51
ILT
1829
1830 bfd_hash_replace (&info->hash->table,
1831 (struct bfd_hash_entry *) h,
1832 (struct bfd_hash_entry *) sub);
1833 if (hashp != NULL)
1834 *hashp = sub;
da6b2d99
ILT
1835 }
1836 break;
1837 }
1838 }
1839 while (cycle);
1840
1841 return true;
1842}
1843\f
1844/* Generic final link routine. */
1845
1846boolean
1847_bfd_generic_final_link (abfd, info)
1848 bfd *abfd;
1849 struct bfd_link_info *info;
1850{
1851 bfd *sub;
1852 asection *o;
1853 struct bfd_link_order *p;
1854 size_t outsymalloc;
1855 struct generic_write_global_symbol_info wginfo;
1856
1857 abfd->outsymbols = (asymbol **) NULL;
1858 abfd->symcount = 0;
1859 outsymalloc = 0;
1860
8e5090ce 1861 /* Build the output symbol table. */
da6b2d99
ILT
1862 for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
1863 if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
1864 return false;
1865
1866 /* Accumulate the global symbols. */
9783e04a 1867 wginfo.info = info;
da6b2d99
ILT
1868 wginfo.output_bfd = abfd;
1869 wginfo.psymalloc = &outsymalloc;
1870 _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
1871 _bfd_generic_link_write_global_symbol,
1872 (PTR) &wginfo);
1873
1874 if (info->relocateable)
1875 {
1876 /* Allocate space for the output relocs for each section. */
1877 for (o = abfd->sections;
1878 o != (asection *) NULL;
1879 o = o->next)
1880 {
4335ce64 1881 o->reloc_count = 0;
da6b2d99
ILT
1882 for (p = o->link_order_head;
1883 p != (struct bfd_link_order *) NULL;
1884 p = p->next)
1885 {
f1cca647
ILT
1886 if (p->type == bfd_section_reloc_link_order
1887 || p->type == bfd_symbol_reloc_link_order)
1888 ++o->reloc_count;
1889 else if (p->type == bfd_indirect_link_order)
da6b2d99
ILT
1890 {
1891 asection *input_section;
1892 bfd *input_bfd;
8e5090ce 1893 long relsize;
da6b2d99 1894 arelent **relocs;
8e5090ce
ILT
1895 asymbol **symbols;
1896 long reloc_count;
da6b2d99
ILT
1897
1898 input_section = p->u.indirect.section;
1899 input_bfd = input_section->owner;
1900 relsize = bfd_get_reloc_upper_bound (input_bfd,
1901 input_section);
8e5090ce
ILT
1902 if (relsize < 0)
1903 return false;
9783e04a 1904 relocs = (arelent **) malloc ((size_t) relsize);
f1cca647 1905 if (!relocs && relsize != 0)
9783e04a 1906 {
d1ad85a6 1907 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
1908 return false;
1909 }
8e5090ce
ILT
1910 symbols = _bfd_generic_link_get_symbols (input_bfd);
1911 reloc_count = bfd_canonicalize_reloc (input_bfd,
1912 input_section,
1913 relocs,
1914 symbols);
1915 if (reloc_count < 0)
1916 return false;
ae115e51
ILT
1917 BFD_ASSERT ((unsigned long) reloc_count
1918 == input_section->reloc_count);
da6b2d99
ILT
1919 o->reloc_count += reloc_count;
1920 free (relocs);
1921 }
1922 }
1923 if (o->reloc_count > 0)
1924 {
1925 o->orelocation = ((arelent **)
1926 bfd_alloc (abfd,
1927 (o->reloc_count
1928 * sizeof (arelent *))));
9783e04a 1929 if (!o->orelocation)
a9713b91 1930 return false;
4335ce64 1931 o->flags |= SEC_RELOC;
da6b2d99
ILT
1932 /* Reset the count so that it can be used as an index
1933 when putting in the output relocs. */
1934 o->reloc_count = 0;
1935 }
1936 }
1937 }
1938
1939 /* Handle all the link order information for the sections. */
1940 for (o = abfd->sections;
1941 o != (asection *) NULL;
1942 o = o->next)
1943 {
1944 for (p = o->link_order_head;
1945 p != (struct bfd_link_order *) NULL;
1946 p = p->next)
1947 {
f1cca647
ILT
1948 switch (p->type)
1949 {
1950 case bfd_section_reloc_link_order:
1951 case bfd_symbol_reloc_link_order:
1952 if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
1953 return false;
1954 break;
9c26be63
ILT
1955 case bfd_indirect_link_order:
1956 if (! default_indirect_link_order (abfd, info, o, p, true))
1957 return false;
1958 break;
f1cca647
ILT
1959 default:
1960 if (! _bfd_default_link_order (abfd, info, o, p))
1961 return false;
1962 break;
1963 }
da6b2d99
ILT
1964 }
1965 }
1966
1967 return true;
1968}
1969
1970/* Add an output symbol to the output BFD. */
1971
1972static boolean
1973generic_add_output_symbol (output_bfd, psymalloc, sym)
1974 bfd *output_bfd;
1975 size_t *psymalloc;
1976 asymbol *sym;
1977{
1978 if (output_bfd->symcount >= *psymalloc)
1979 {
1980 asymbol **newsyms;
1981
1982 if (*psymalloc == 0)
1983 *psymalloc = 124;
1984 else
1985 *psymalloc *= 2;
1986 if (output_bfd->outsymbols == (asymbol **) NULL)
1987 newsyms = (asymbol **) malloc (*psymalloc * sizeof (asymbol *));
1988 else
1989 newsyms = (asymbol **) realloc (output_bfd->outsymbols,
1990 *psymalloc * sizeof (asymbol *));
1991 if (newsyms == (asymbol **) NULL)
1992 {
d1ad85a6 1993 bfd_set_error (bfd_error_no_memory);
da6b2d99
ILT
1994 return false;
1995 }
1996 output_bfd->outsymbols = newsyms;
1997 }
1998
1999 output_bfd->outsymbols[output_bfd->symcount] = sym;
2000 ++output_bfd->symcount;
2001
2002 return true;
2003}
2004
2005/* Handle the symbols for an input BFD. */
2006
2007boolean
2008_bfd_generic_link_output_symbols (output_bfd, input_bfd, info, psymalloc)
2009 bfd *output_bfd;
2010 bfd *input_bfd;
2011 struct bfd_link_info *info;
2012 size_t *psymalloc;
2013{
da6b2d99
ILT
2014 asymbol **sym_ptr;
2015 asymbol **sym_end;
2016
8e5090ce
ILT
2017 if (! generic_link_read_symbols (input_bfd))
2018 return false;
da6b2d99
ILT
2019
2020 /* Create a filename symbol if we are supposed to. */
2021 if (info->create_object_symbols_section != (asection *) NULL)
2022 {
2023 asection *sec;
2024
2025 for (sec = input_bfd->sections;
2026 sec != (asection *) NULL;
2027 sec = sec->next)
2028 {
2029 if (sec->output_section == info->create_object_symbols_section)
2030 {
2031 asymbol *newsym;
2032
2033 newsym = bfd_make_empty_symbol (input_bfd);
9783e04a
DM
2034 if (!newsym)
2035 return false;
da6b2d99
ILT
2036 newsym->name = input_bfd->filename;
2037 newsym->value = 0;
2038 newsym->flags = BSF_LOCAL | BSF_FILE;
2039 newsym->section = sec;
2040
2041 if (! generic_add_output_symbol (output_bfd, psymalloc,
2042 newsym))
2043 return false;
2044
2045 break;
2046 }
2047 }
2048 }
2049
2050 /* Adjust the values of the globally visible symbols, and write out
2051 local symbols. */
8e5090ce
ILT
2052 sym_ptr = _bfd_generic_link_get_symbols (input_bfd);
2053 sym_end = sym_ptr + _bfd_generic_link_get_symcount (input_bfd);
da6b2d99
ILT
2054 for (; sym_ptr < sym_end; sym_ptr++)
2055 {
2056 asymbol *sym;
2057 struct generic_link_hash_entry *h;
2058 boolean output;
2059
2060 h = (struct generic_link_hash_entry *) NULL;
2061 sym = *sym_ptr;
2062 if ((sym->flags & (BSF_INDIRECT
2063 | BSF_WARNING
2064 | BSF_GLOBAL
2065 | BSF_CONSTRUCTOR
2066 | BSF_WEAK)) != 0
a537cb21 2067 || bfd_is_und_section (bfd_get_section (sym))
da6b2d99 2068 || bfd_is_com_section (bfd_get_section (sym))
a537cb21 2069 || bfd_is_ind_section (bfd_get_section (sym)))
da6b2d99 2070 {
6ff9c051
ILT
2071 if (sym->udata.p != NULL)
2072 h = (struct generic_link_hash_entry *) sym->udata.p;
2073 else if ((sym->flags & BSF_CONSTRUCTOR) != 0)
2074 {
2075 /* This case normally means that the main linker code
2076 deliberately ignored this constructor symbol. We
2077 should just pass it through. This will screw up if
2078 the constructor symbol is from a different,
2079 non-generic, object file format, but the case will
2080 only arise when linking with -r, which will probably
2081 fail anyhow, since there will be no way to represent
2082 the relocs in the output format being used. */
2083 h = NULL;
2084 }
2085 else
2086 h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
2087 bfd_asymbol_name (sym),
2088 false, false, true);
2089
da6b2d99
ILT
2090 if (h != (struct generic_link_hash_entry *) NULL)
2091 {
2092 /* Force all references to this symbol to point to
2093 the same area in memory. It is possible that
2094 this routine will be called with a hash table
2095 other than a generic hash table, so we double
2096 check that. */
2097 if (info->hash->creator == input_bfd->xvec)
2098 {
2099 if (h->sym != (asymbol *) NULL)
2100 *sym_ptr = sym = h->sym;
2101 }
2102
2103 switch (h->root.type)
2104 {
2105 default:
2106 case bfd_link_hash_new:
2107 abort ();
2108 case bfd_link_hash_undefined:
6c97aedf
ILT
2109 break;
2110 case bfd_link_hash_undefweak:
2111 sym->flags |= BSF_WEAK;
da6b2d99 2112 break;
6ff9c051
ILT
2113 case bfd_link_hash_indirect:
2114 h = (struct generic_link_hash_entry *) h->root.u.i.link;
2115 /* fall through */
da6b2d99 2116 case bfd_link_hash_defined:
6c97aedf 2117 sym->flags |= BSF_GLOBAL;
6ff9c051 2118 sym->flags &=~ BSF_CONSTRUCTOR;
6c97aedf
ILT
2119 sym->value = h->root.u.def.value;
2120 sym->section = h->root.u.def.section;
2121 break;
2122 case bfd_link_hash_defweak:
2123 sym->flags |= BSF_WEAK;
6ff9c051 2124 sym->flags &=~ BSF_CONSTRUCTOR;
da6b2d99
ILT
2125 sym->value = h->root.u.def.value;
2126 sym->section = h->root.u.def.section;
da6b2d99
ILT
2127 break;
2128 case bfd_link_hash_common:
2129 sym->value = h->root.u.c.size;
2130 sym->flags |= BSF_GLOBAL;
5072b8e5
ILT
2131 if (! bfd_is_com_section (sym->section))
2132 {
a537cb21
ILT
2133 BFD_ASSERT (bfd_is_und_section (sym->section));
2134 sym->section = bfd_com_section_ptr;
5072b8e5 2135 }
da6b2d99 2136 /* We do not set the section of the symbol to
6ff9c051 2137 h->root.u.c.p->section. That value was saved so
5072b8e5
ILT
2138 that we would know where to allocate the symbol
2139 if it was defined. In this case the type is
2140 still bfd_link_hash_common, so we did not define
2141 it, so we do not want to use that section. */
da6b2d99
ILT
2142 break;
2143 }
2144 }
2145 }
2146
2147 /* This switch is straight from the old code in
2148 write_file_locals in ldsym.c. */
2149 if (info->strip == strip_some
2150 && (bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym),
2151 false, false)
2152 == (struct bfd_hash_entry *) NULL))
2153 output = false;
2154 else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
2155 {
2156 /* If this symbol is marked as occurring now, rather
2157 than at the end, output it now. This is used for
2158 COFF C_EXT FCN symbols. FIXME: There must be a
2159 better way. */
2160 if (bfd_asymbol_bfd (sym) == input_bfd
2161 && (sym->flags & BSF_NOT_AT_END) != 0)
2162 output = true;
2163 else
2164 output = false;
2165 }
a537cb21 2166 else if (bfd_is_ind_section (sym->section))
da6b2d99
ILT
2167 output = false;
2168 else if ((sym->flags & BSF_DEBUGGING) != 0)
2169 {
2170 if (info->strip == strip_none)
2171 output = true;
2172 else
2173 output = false;
2174 }
a537cb21 2175 else if (bfd_is_und_section (sym->section)
da6b2d99
ILT
2176 || bfd_is_com_section (sym->section))
2177 output = false;
2178 else if ((sym->flags & BSF_LOCAL) != 0)
2179 {
2180 if ((sym->flags & BSF_WARNING) != 0)
2181 output = false;
2182 else
2183 {
2184 switch (info->discard)
2185 {
2186 default:
2187 case discard_all:
2188 output = false;
2189 break;
2190 case discard_l:
2191 if (bfd_asymbol_name (sym)[0] == info->lprefix[0]
2192 && (info->lprefix_len == 1
2193 || strncmp (bfd_asymbol_name (sym), info->lprefix,
2194 info->lprefix_len) == 0))
2195 output = false;
2196 else
2197 output = true;
2198 break;
2199 case discard_none:
2200 output = true;
2201 break;
2202 }
2203 }
2204 }
2205 else if ((sym->flags & BSF_CONSTRUCTOR))
2206 {
2207 if (info->strip != strip_all)
2208 output = true;
2209 else
2210 output = false;
2211 }
2212 else
2213 abort ();
2214
2215 if (output)
2216 {
2217 if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
2218 return false;
2219 if (h != (struct generic_link_hash_entry *) NULL)
35fee729 2220 h->written = true;
da6b2d99
ILT
2221 }
2222 }
2223
2224 return true;
2225}
2226
9c26be63
ILT
2227/* Set the section and value of a generic BFD symbol based on a linker
2228 hash table entry. */
2229
2230static void
2231set_symbol_from_hash (sym, h)
2232 asymbol *sym;
2233 struct bfd_link_hash_entry *h;
2234{
2235 switch (h->type)
2236 {
2237 default:
9c26be63 2238 abort ();
ae115e51
ILT
2239 break;
2240 case bfd_link_hash_new:
2241 /* This can happen when a constructor symbol is seen but we are
2242 not building constructors. */
2243 if (sym->section != NULL)
2244 {
2245 BFD_ASSERT ((sym->flags & BSF_CONSTRUCTOR) != 0);
2246 }
2247 else
2248 {
2249 sym->flags |= BSF_CONSTRUCTOR;
2250 sym->section = bfd_abs_section_ptr;
2251 sym->value = 0;
2252 }
2253 break;
9c26be63
ILT
2254 case bfd_link_hash_undefined:
2255 sym->section = bfd_und_section_ptr;
2256 sym->value = 0;
2257 break;
6c97aedf 2258 case bfd_link_hash_undefweak:
9c26be63
ILT
2259 sym->section = bfd_und_section_ptr;
2260 sym->value = 0;
2261 sym->flags |= BSF_WEAK;
2262 break;
2263 case bfd_link_hash_defined:
2264 sym->section = h->u.def.section;
2265 sym->value = h->u.def.value;
2266 break;
6c97aedf
ILT
2267 case bfd_link_hash_defweak:
2268 sym->flags |= BSF_WEAK;
2269 sym->section = h->u.def.section;
2270 sym->value = h->u.def.value;
2271 break;
9c26be63
ILT
2272 case bfd_link_hash_common:
2273 sym->value = h->u.c.size;
2274 if (sym->section == NULL)
2275 sym->section = bfd_com_section_ptr;
2276 else if (! bfd_is_com_section (sym->section))
2277 {
2278 BFD_ASSERT (bfd_is_und_section (sym->section));
2279 sym->section = bfd_com_section_ptr;
2280 }
2281 /* Do not set the section; see _bfd_generic_link_output_symbols. */
2282 break;
2283 case bfd_link_hash_indirect:
2284 case bfd_link_hash_warning:
2285 /* FIXME: What should we do here? */
2286 break;
2287 }
2288}
2289
da6b2d99
ILT
2290/* Write out a global symbol, if it hasn't already been written out.
2291 This is called for each symbol in the hash table. */
2292
2293boolean
2294_bfd_generic_link_write_global_symbol (h, data)
2295 struct generic_link_hash_entry *h;
2296 PTR data;
2297{
2298 struct generic_write_global_symbol_info *wginfo =
2299 (struct generic_write_global_symbol_info *) data;
2300 asymbol *sym;
2301
35fee729 2302 if (h->written)
da6b2d99
ILT
2303 return true;
2304
35fee729 2305 h->written = true;
9783e04a
DM
2306
2307 if (wginfo->info->strip == strip_all
2308 || (wginfo->info->strip == strip_some
2309 && bfd_hash_lookup (wginfo->info->keep_hash, h->root.root.string,
2310 false, false) == NULL))
2311 return true;
2312
da6b2d99
ILT
2313 if (h->sym != (asymbol *) NULL)
2314 {
2315 sym = h->sym;
2316 BFD_ASSERT (strcmp (bfd_asymbol_name (sym), h->root.root.string) == 0);
2317 }
2318 else
2319 {
2320 sym = bfd_make_empty_symbol (wginfo->output_bfd);
9783e04a
DM
2321 if (!sym)
2322 return false;
da6b2d99
ILT
2323 sym->name = h->root.root.string;
2324 sym->flags = 0;
2325 }
2326
9c26be63 2327 set_symbol_from_hash (sym, &h->root);
da6b2d99
ILT
2328
2329 sym->flags |= BSF_GLOBAL;
2330
2331 if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
2332 sym))
2333 {
2334 /* FIXME: No way to return failure. */
2335 abort ();
2336 }
2337
da6b2d99
ILT
2338 return true;
2339}
f1cca647
ILT
2340
2341/* Create a relocation. */
2342
2343boolean
2344_bfd_generic_reloc_link_order (abfd, info, sec, link_order)
2345 bfd *abfd;
2346 struct bfd_link_info *info;
2347 asection *sec;
2348 struct bfd_link_order *link_order;
2349{
2350 arelent *r;
2351
2352 if (! info->relocateable)
2353 abort ();
2354 if (sec->orelocation == (arelent **) NULL)
2355 abort ();
2356
2357 r = (arelent *) bfd_alloc (abfd, sizeof (arelent));
2358 if (r == (arelent *) NULL)
a9713b91 2359 return false;
f1cca647
ILT
2360
2361 r->address = link_order->offset;
2362 r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
6c97aedf 2363 if (r->howto == 0)
f1cca647
ILT
2364 {
2365 bfd_set_error (bfd_error_bad_value);
2366 return false;
2367 }
2368
2369 /* Get the symbol to use for the relocation. */
2370 if (link_order->type == bfd_section_reloc_link_order)
2371 r->sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
2372 else
2373 {
2374 struct generic_link_hash_entry *h;
2375
2376 h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
2377 link_order->u.reloc.p->u.name,
2378 false, false, true);
2379 if (h == (struct generic_link_hash_entry *) NULL
35fee729 2380 || ! h->written)
f1cca647
ILT
2381 {
2382 if (! ((*info->callbacks->unattached_reloc)
2383 (info, link_order->u.reloc.p->u.name,
2384 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2385 return false;
2386 bfd_set_error (bfd_error_bad_value);
2387 return false;
2388 }
2389 r->sym_ptr_ptr = &h->sym;
2390 }
2391
2392 /* If this is an inplace reloc, write the addend to the object file.
2393 Otherwise, store it in the reloc addend. */
2394 if (! r->howto->partial_inplace)
2395 r->addend = link_order->u.reloc.p->addend;
2396 else
2397 {
2398 bfd_size_type size;
2399 bfd_reloc_status_type rstat;
2400 bfd_byte *buf;
2401 boolean ok;
2402
2403 size = bfd_get_reloc_size (r->howto);
2404 buf = (bfd_byte *) bfd_zmalloc (size);
2405 if (buf == (bfd_byte *) NULL)
a9713b91 2406 return false;
f1cca647
ILT
2407 rstat = _bfd_relocate_contents (r->howto, abfd,
2408 link_order->u.reloc.p->addend, buf);
2409 switch (rstat)
2410 {
2411 case bfd_reloc_ok:
2412 break;
2413 default:
2414 case bfd_reloc_outofrange:
2415 abort ();
2416 case bfd_reloc_overflow:
2417 if (! ((*info->callbacks->reloc_overflow)
2418 (info,
2419 (link_order->type == bfd_section_reloc_link_order
2420 ? bfd_section_name (abfd, link_order->u.reloc.p->u.section)
2421 : link_order->u.reloc.p->u.name),
2422 r->howto->name, link_order->u.reloc.p->addend,
2423 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2424 {
2425 free (buf);
2426 return false;
2427 }
2428 break;
2429 }
2430 ok = bfd_set_section_contents (abfd, sec, (PTR) buf,
2431 (file_ptr) link_order->offset, size);
2432 free (buf);
2433 if (! ok)
2434 return false;
2435
2436 r->addend = 0;
2437 }
2438
2439 sec->orelocation[sec->reloc_count] = r;
2440 ++sec->reloc_count;
2441
2442 return true;
2443}
da6b2d99
ILT
2444\f
2445/* Allocate a new link_order for a section. */
2446
2447struct bfd_link_order *
2448bfd_new_link_order (abfd, section)
2449 bfd *abfd;
2450 asection *section;
2451{
2452 struct bfd_link_order *new;
2453
2454 new = ((struct bfd_link_order *)
2455 bfd_alloc_by_size_t (abfd, sizeof (struct bfd_link_order)));
9783e04a 2456 if (!new)
a9713b91 2457 return NULL;
da6b2d99
ILT
2458
2459 new->type = bfd_undefined_link_order;
2460 new->offset = 0;
2461 new->size = 0;
2462 new->next = (struct bfd_link_order *) NULL;
2463
2464 if (section->link_order_tail != (struct bfd_link_order *) NULL)
2465 section->link_order_tail->next = new;
2466 else
2467 section->link_order_head = new;
2468 section->link_order_tail = new;
2469
2470 return new;
2471}
2472
f1cca647
ILT
2473/* Default link order processing routine. Note that we can not handle
2474 the reloc_link_order types here, since they depend upon the details
2475 of how the particular backends generates relocs. */
da6b2d99
ILT
2476
2477boolean
2478_bfd_default_link_order (abfd, info, sec, link_order)
2479 bfd *abfd;
2480 struct bfd_link_info *info;
2481 asection *sec;
2482 struct bfd_link_order *link_order;
2483{
2484 switch (link_order->type)
2485 {
2486 case bfd_undefined_link_order:
f1cca647
ILT
2487 case bfd_section_reloc_link_order:
2488 case bfd_symbol_reloc_link_order:
da6b2d99
ILT
2489 default:
2490 abort ();
2491 case bfd_indirect_link_order:
9c26be63
ILT
2492 return default_indirect_link_order (abfd, info, sec, link_order,
2493 false);
da6b2d99
ILT
2494 case bfd_fill_link_order:
2495 return default_fill_link_order (abfd, info, sec, link_order);
4335ce64
ILT
2496 case bfd_data_link_order:
2497 return bfd_set_section_contents (abfd, sec,
2498 (PTR) link_order->u.data.contents,
2499 (file_ptr) link_order->offset,
2500 link_order->size);
da6b2d99
ILT
2501 }
2502}
2503
2504/* Default routine to handle a bfd_fill_link_order. */
2505
6e07e54f 2506/*ARGSUSED*/
da6b2d99
ILT
2507static boolean
2508default_fill_link_order (abfd, info, sec, link_order)
2509 bfd *abfd;
2510 struct bfd_link_info *info;
2511 asection *sec;
2512 struct bfd_link_order *link_order;
2513{
2514 size_t size;
2515 char *space;
2516 size_t i;
2517 int fill;
f1cca647 2518 boolean result;
da6b2d99
ILT
2519
2520 BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
2521
2522 size = (size_t) link_order->size;
f1cca647
ILT
2523 space = (char *) malloc (size);
2524 if (space == NULL && size != 0)
2525 {
2526 bfd_set_error (bfd_error_no_memory);
2527 return false;
2528 }
2529
da6b2d99
ILT
2530 fill = link_order->u.fill.value;
2531 for (i = 0; i < size; i += 2)
2532 space[i] = fill >> 8;
2533 for (i = 1; i < size; i += 2)
2534 space[i] = fill;
f1cca647
ILT
2535 result = bfd_set_section_contents (abfd, sec, space,
2536 (file_ptr) link_order->offset,
2537 link_order->size);
2538 free (space);
2539 return result;
da6b2d99 2540}
6e07e54f
ILT
2541
2542/* Default routine to handle a bfd_indirect_link_order. */
2543
2544static boolean
9c26be63
ILT
2545default_indirect_link_order (output_bfd, info, output_section, link_order,
2546 generic_linker)
6e07e54f
ILT
2547 bfd *output_bfd;
2548 struct bfd_link_info *info;
2549 asection *output_section;
2550 struct bfd_link_order *link_order;
9c26be63 2551 boolean generic_linker;
6e07e54f
ILT
2552{
2553 asection *input_section;
2554 bfd *input_bfd;
f1cca647
ILT
2555 bfd_byte *contents = NULL;
2556 bfd_byte *new_contents;
6e07e54f
ILT
2557
2558 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
2559
2560 if (link_order->size == 0)
2561 return true;
2562
2563 input_section = link_order->u.indirect.section;
2564 input_bfd = input_section->owner;
2565
2566 BFD_ASSERT (input_section->output_section == output_section);
2567 BFD_ASSERT (input_section->output_offset == link_order->offset);
66d9f06f 2568 BFD_ASSERT (input_section->_cooked_size == link_order->size);
6e07e54f
ILT
2569
2570 if (info->relocateable
92f345b9 2571 && input_section->reloc_count > 0
6e07e54f
ILT
2572 && output_section->orelocation == (arelent **) NULL)
2573 {
2574 /* Space has not been allocated for the output relocations.
2575 This can happen when we are called by a specific backend
2576 because somebody is attempting to link together different
2577 types of object files. Handling this case correctly is
2578 difficult, and sometimes impossible. */
2579 abort ();
2580 }
2581
9c26be63
ILT
2582 if (! generic_linker)
2583 {
2584 asymbol **sympp;
2585 asymbol **symppend;
2586
2587 /* Get the canonical symbols. The generic linker will always
2588 have retrieved them by this point, but we are being called by
2589 a specific linker, presumably because we are linking
2590 different types of object files together. */
2591 if (! generic_link_read_symbols (input_bfd))
2592 return false;
2593
2594 /* Since we have been called by a specific linker, rather than
2595 the generic linker, the values of the symbols will not be
2596 right. They will be the values as seen in the input file,
2597 not the values of the final link. We need to fix them up
2598 before we can relocate the section. */
2599 sympp = _bfd_generic_link_get_symbols (input_bfd);
2600 symppend = sympp + _bfd_generic_link_get_symcount (input_bfd);
2601 for (; sympp < symppend; sympp++)
2602 {
2603 asymbol *sym;
2604 struct bfd_link_hash_entry *h;
2605
2606 sym = *sympp;
2607
2608 if ((sym->flags & (BSF_INDIRECT
2609 | BSF_WARNING
2610 | BSF_GLOBAL
2611 | BSF_CONSTRUCTOR
2612 | BSF_WEAK)) != 0
2613 || bfd_is_und_section (bfd_get_section (sym))
2614 || bfd_is_com_section (bfd_get_section (sym))
2615 || bfd_is_ind_section (bfd_get_section (sym)))
2616 {
2617 /* sym->udata may have been set by
2618 generic_link_add_symbol_list. */
2619 if (sym->udata.p != NULL)
2620 h = (struct bfd_link_hash_entry *) sym->udata.p;
2621 else
2622 h = bfd_link_hash_lookup (info->hash,
2623 bfd_asymbol_name (sym),
2624 false, false, true);
2625 if (h != NULL)
2626 set_symbol_from_hash (sym, h);
2627 }
2628 }
2629 }
6e07e54f
ILT
2630
2631 /* Get and relocate the section contents. */
ae115e51
ILT
2632 contents = ((bfd_byte *)
2633 malloc ((size_t) bfd_section_size (input_bfd, input_section)));
f1cca647
ILT
2634 if (contents == NULL && bfd_section_size (input_bfd, input_section) != 0)
2635 {
2636 bfd_set_error (bfd_error_no_memory);
2637 goto error_return;
2638 }
2639 new_contents = (bfd_get_relocated_section_contents
2640 (output_bfd, info, link_order, contents, info->relocateable,
8e5090ce 2641 _bfd_generic_link_get_symbols (input_bfd)));
f1cca647
ILT
2642 if (!new_contents)
2643 goto error_return;
6e07e54f
ILT
2644
2645 /* Output the section contents. */
f1cca647
ILT
2646 if (! bfd_set_section_contents (output_bfd, output_section,
2647 (PTR) new_contents,
6e07e54f 2648 link_order->offset, link_order->size))
f1cca647 2649 goto error_return;
6e07e54f 2650
f1cca647
ILT
2651 if (contents != NULL)
2652 free (contents);
6e07e54f 2653 return true;
f1cca647
ILT
2654
2655 error_return:
2656 if (contents != NULL)
2657 free (contents);
2658 return false;
2659}
2660
2661/* A little routine to count the number of relocs in a link_order
2662 list. */
2663
2664unsigned int
2665_bfd_count_link_order_relocs (link_order)
2666 struct bfd_link_order *link_order;
2667{
2668 register unsigned int c;
2669 register struct bfd_link_order *l;
2670
2671 c = 0;
2672 for (l = link_order; l != (struct bfd_link_order *) NULL; l = l->next)
2673 {
2674 if (l->type == bfd_section_reloc_link_order
2675 || l->type == bfd_symbol_reloc_link_order)
2676 ++c;
2677 }
2678
2679 return c;
6e07e54f 2680}
6ff9c051
ILT
2681
2682/*
2683FUNCTION
2684 bfd_link_split_section
2685
2686SYNOPSIS
2687 boolean bfd_link_split_section(bfd *abfd, asection *sec);
2688
2689DESCRIPTION
2690 Return nonzero if @var{sec} should be split during a
2691 reloceatable or final link.
2692
2693.#define bfd_link_split_section(abfd, sec) \
2694. BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
2695.
2696
2697*/
2698
2699
2700
2701boolean
2702_bfd_generic_link_split_section (abfd, sec)
2703 bfd *abfd;
2704 asection *sec;
2705{
2706 return false;
2707}
This page took 0.212651 seconds and 4 git commands to generate.