source: branches/libc-0.6/src/binutils/bfd/linker.c

Last change on this file was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

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