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