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