2002-03-27 Michael Snyder <msnyder@redhat.com>
[deliverable/binutils-gdb.git] / bfd / cofflink.c
CommitLineData
252b5132 1/* COFF specific linker code.
7898deda
NC
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
252b5132
RH
4 Written by Ian Lance Taylor, Cygnus Support.
5
6This file is part of BFD, the Binary File Descriptor library.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22/* This file contains the COFF backend linker code. */
23
24#include "bfd.h"
25#include "sysdep.h"
26#include "bfdlink.h"
27#include "libbfd.h"
28#include "coff/internal.h"
29#include "libcoff.h"
30
31static boolean coff_link_add_object_symbols
32 PARAMS ((bfd *, struct bfd_link_info *));
33static boolean coff_link_check_archive_element
34 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
35static boolean coff_link_check_ar_symbols
36 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
37static boolean coff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
38static char *dores_com PARAMS ((char *, bfd *, int));
39static char *get_name PARAMS ((char *, char **));
40static int process_embedded_commands
41 PARAMS ((bfd *, struct bfd_link_info *, bfd *));
42static void mark_relocs PARAMS ((struct coff_final_link_info *, bfd *));
43
e4202681
NC
44/* Return true if SYM is a weak, external symbol. */
45#define IS_WEAK_EXTERNAL(abfd, sym) \
46 ((sym).n_sclass == C_WEAKEXT \
47 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
48
49/* Return true if SYM is an external symbol. */
50#define IS_EXTERNAL(abfd, sym) \
51 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
52
b2d638c7
ILT
53/* Define macros so that the ISFCN, et. al., macros work correctly.
54 These macros are defined in include/coff/internal.h in terms of
55 N_TMASK, etc. These definitions require a user to define local
56 variables with the appropriate names, and with values from the
57 coff_data (abfd) structure. */
58
59#define N_TMASK n_tmask
60#define N_BTSHFT n_btshft
61#define N_BTMASK n_btmask
62
252b5132
RH
63/* Create an entry in a COFF linker hash table. */
64
65struct bfd_hash_entry *
66_bfd_coff_link_hash_newfunc (entry, table, string)
67 struct bfd_hash_entry *entry;
68 struct bfd_hash_table *table;
69 const char *string;
70{
71 struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
72
73 /* Allocate the structure if it has not already been allocated by a
74 subclass. */
75 if (ret == (struct coff_link_hash_entry *) NULL)
76 ret = ((struct coff_link_hash_entry *)
77 bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
78 if (ret == (struct coff_link_hash_entry *) NULL)
79 return (struct bfd_hash_entry *) ret;
80
81 /* Call the allocation method of the superclass. */
82 ret = ((struct coff_link_hash_entry *)
83 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
84 table, string));
85 if (ret != (struct coff_link_hash_entry *) NULL)
86 {
87 /* Set local fields. */
88 ret->indx = -1;
89 ret->type = T_NULL;
90 ret->class = C_NULL;
91 ret->numaux = 0;
92 ret->auxbfd = NULL;
93 ret->aux = NULL;
94 }
95
96 return (struct bfd_hash_entry *) ret;
97}
98
99/* Initialize a COFF linker hash table. */
100
101boolean
102_bfd_coff_link_hash_table_init (table, abfd, newfunc)
103 struct coff_link_hash_table *table;
104 bfd *abfd;
105 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
106 struct bfd_hash_table *,
107 const char *));
108{
109 table->stab_info = NULL;
110 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
111}
112
113/* Create a COFF linker hash table. */
114
115struct bfd_link_hash_table *
116_bfd_coff_link_hash_table_create (abfd)
117 bfd *abfd;
118{
119 struct coff_link_hash_table *ret;
dc810e39 120 bfd_size_type amt = sizeof (struct coff_link_hash_table);
252b5132 121
dc810e39 122 ret = (struct coff_link_hash_table *) bfd_alloc (abfd, amt);
252b5132
RH
123 if (ret == NULL)
124 return NULL;
125 if (! _bfd_coff_link_hash_table_init (ret, abfd,
126 _bfd_coff_link_hash_newfunc))
127 {
128 bfd_release (abfd, ret);
129 return (struct bfd_link_hash_table *) NULL;
130 }
131 return &ret->root;
132}
133
134/* Create an entry in a COFF debug merge hash table. */
135
136struct bfd_hash_entry *
137_bfd_coff_debug_merge_hash_newfunc (entry, table, string)
138 struct bfd_hash_entry *entry;
139 struct bfd_hash_table *table;
140 const char *string;
141{
142 struct coff_debug_merge_hash_entry *ret =
143 (struct coff_debug_merge_hash_entry *) entry;
144
145 /* Allocate the structure if it has not already been allocated by a
146 subclass. */
147 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
148 ret = ((struct coff_debug_merge_hash_entry *)
149 bfd_hash_allocate (table,
150 sizeof (struct coff_debug_merge_hash_entry)));
151 if (ret == (struct coff_debug_merge_hash_entry *) NULL)
152 return (struct bfd_hash_entry *) ret;
153
154 /* Call the allocation method of the superclass. */
155 ret = ((struct coff_debug_merge_hash_entry *)
156 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
157 if (ret != (struct coff_debug_merge_hash_entry *) NULL)
158 {
159 /* Set local fields. */
160 ret->types = NULL;
161 }
162
163 return (struct bfd_hash_entry *) ret;
164}
165
166/* Given a COFF BFD, add symbols to the global hash table as
167 appropriate. */
168
169boolean
170_bfd_coff_link_add_symbols (abfd, info)
171 bfd *abfd;
172 struct bfd_link_info *info;
173{
174 switch (bfd_get_format (abfd))
175 {
176 case bfd_object:
177 return coff_link_add_object_symbols (abfd, info);
178 case bfd_archive:
179 return (_bfd_generic_link_add_archive_symbols
180 (abfd, info, coff_link_check_archive_element));
181 default:
182 bfd_set_error (bfd_error_wrong_format);
183 return false;
184 }
185}
186
187/* Add symbols from a COFF object file. */
188
189static boolean
190coff_link_add_object_symbols (abfd, info)
191 bfd *abfd;
192 struct bfd_link_info *info;
193{
194 if (! _bfd_coff_get_external_symbols (abfd))
195 return false;
196 if (! coff_link_add_symbols (abfd, info))
197 return false;
198
199 if (! info->keep_memory)
200 {
201 if (! _bfd_coff_free_symbols (abfd))
202 return false;
203 }
204 return true;
205}
206
207/* Check a single archive element to see if we need to include it in
208 the link. *PNEEDED is set according to whether this element is
209 needed in the link or not. This is called via
210 _bfd_generic_link_add_archive_symbols. */
211
212static boolean
213coff_link_check_archive_element (abfd, info, pneeded)
214 bfd *abfd;
215 struct bfd_link_info *info;
216 boolean *pneeded;
217{
218 if (! _bfd_coff_get_external_symbols (abfd))
219 return false;
220
221 if (! coff_link_check_ar_symbols (abfd, info, pneeded))
222 return false;
223
224 if (*pneeded)
225 {
226 if (! coff_link_add_symbols (abfd, info))
227 return false;
228 }
229
230 if (! info->keep_memory || ! *pneeded)
231 {
232 if (! _bfd_coff_free_symbols (abfd))
233 return false;
234 }
235
236 return true;
237}
238
239/* Look through the symbols to see if this object file should be
240 included in the link. */
241
242static boolean
243coff_link_check_ar_symbols (abfd, info, pneeded)
244 bfd *abfd;
245 struct bfd_link_info *info;
246 boolean *pneeded;
247{
252b5132
RH
248 bfd_size_type symesz;
249 bfd_byte *esym;
250 bfd_byte *esym_end;
251
252 *pneeded = false;
253
252b5132
RH
254 symesz = bfd_coff_symesz (abfd);
255 esym = (bfd_byte *) obj_coff_external_syms (abfd);
256 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
257 while (esym < esym_end)
258 {
259 struct internal_syment sym;
5d54c628 260 enum coff_symbol_classification classification;
252b5132
RH
261
262 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
263
5d54c628
ILT
264 classification = bfd_coff_classify_symbol (abfd, &sym);
265 if (classification == COFF_SYMBOL_GLOBAL
266 || classification == COFF_SYMBOL_COMMON)
252b5132
RH
267 {
268 const char *name;
269 char buf[SYMNMLEN + 1];
270 struct bfd_link_hash_entry *h;
271
272 /* This symbol is externally visible, and is defined by this
273 object file. */
274
275 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
276 if (name == NULL)
277 return false;
278 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
279
8ceb7a1b
CW
280 /* auto import */
281 if (!h && info->pei386_auto_import)
282 {
283 if (!strncmp (name,"__imp_", 6))
284 {
285 h =
286 bfd_link_hash_lookup (info->hash, name + 6, false, false,
287 true);
288 }
289 }
252b5132
RH
290 /* We are only interested in symbols that are currently
291 undefined. If a symbol is currently known to be common,
292 COFF linkers do not bring in an object file which defines
293 it. */
294 if (h != (struct bfd_link_hash_entry *) NULL
295 && h->type == bfd_link_hash_undefined)
296 {
297 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
298 return false;
299 *pneeded = true;
300 return true;
301 }
302 }
303
304 esym += (sym.n_numaux + 1) * symesz;
305 }
306
307 /* We do not need this object file. */
308 return true;
309}
310
311/* Add all the symbols from an object file to the hash table. */
312
313static boolean
314coff_link_add_symbols (abfd, info)
315 bfd *abfd;
316 struct bfd_link_info *info;
317{
b2d638c7
ILT
318 unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
319 unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
320 unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
252b5132
RH
321 boolean keep_syms;
322 boolean default_copy;
323 bfd_size_type symcount;
324 struct coff_link_hash_entry **sym_hash;
325 bfd_size_type symesz;
326 bfd_byte *esym;
327 bfd_byte *esym_end;
dc810e39 328 bfd_size_type amt;
252b5132
RH
329
330 /* Keep the symbols during this function, in case the linker needs
331 to read the generic symbols in order to report an error message. */
332 keep_syms = obj_coff_keep_syms (abfd);
333 obj_coff_keep_syms (abfd) = true;
334
252b5132
RH
335 if (info->keep_memory)
336 default_copy = false;
337 else
338 default_copy = true;
339
340 symcount = obj_raw_syment_count (abfd);
341
342 /* We keep a list of the linker hash table entries that correspond
343 to particular symbols. */
dc810e39
AM
344 amt = symcount * sizeof (struct coff_link_hash_entry *);
345 sym_hash = (struct coff_link_hash_entry **) bfd_alloc (abfd, amt);
252b5132
RH
346 if (sym_hash == NULL && symcount != 0)
347 goto error_return;
348 obj_coff_sym_hashes (abfd) = sym_hash;
349 memset (sym_hash, 0,
350 (size_t) symcount * sizeof (struct coff_link_hash_entry *));
351
352 symesz = bfd_coff_symesz (abfd);
353 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
354 esym = (bfd_byte *) obj_coff_external_syms (abfd);
355 esym_end = esym + symcount * symesz;
356 while (esym < esym_end)
357 {
358 struct internal_syment sym;
5d54c628 359 enum coff_symbol_classification classification;
252b5132
RH
360 boolean copy;
361
362 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
363
5d54c628
ILT
364 classification = bfd_coff_classify_symbol (abfd, &sym);
365 if (classification != COFF_SYMBOL_LOCAL)
252b5132
RH
366 {
367 const char *name;
368 char buf[SYMNMLEN + 1];
369 flagword flags;
370 asection *section;
371 bfd_vma value;
7fd9c191 372 boolean addit;
252b5132
RH
373
374 /* This symbol is externally visible. */
375
376 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
377 if (name == NULL)
378 goto error_return;
379
380 /* We must copy the name into memory if we got it from the
381 syment itself, rather than the string table. */
382 copy = default_copy;
383 if (sym._n._n_n._n_zeroes != 0
384 || sym._n._n_n._n_offset == 0)
385 copy = true;
386
387 value = sym.n_value;
388
5d54c628 389 switch (classification)
252b5132 390 {
5d54c628
ILT
391 default:
392 abort ();
393
394 case COFF_SYMBOL_GLOBAL:
252b5132
RH
395 flags = BSF_EXPORT | BSF_GLOBAL;
396 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
397 if (! obj_pe (abfd))
398 value -= section->vma;
5d54c628
ILT
399 break;
400
401 case COFF_SYMBOL_UNDEFINED:
402 flags = 0;
403 section = bfd_und_section_ptr;
404 break;
405
406 case COFF_SYMBOL_COMMON:
407 flags = BSF_GLOBAL;
408 section = bfd_com_section_ptr;
409 break;
410
411 case COFF_SYMBOL_PE_SECTION:
412 flags = BSF_SECTION_SYM | BSF_GLOBAL;
413 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
414 break;
252b5132
RH
415 }
416
e4202681 417 if (IS_WEAK_EXTERNAL (abfd, sym))
252b5132
RH
418 flags = BSF_WEAK;
419
7fd9c191
ILT
420 addit = true;
421
422 /* In the PE format, section symbols actually refer to the
423 start of the output section. We handle them specially
424 here. */
425 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
426 {
427 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
428 name, false, copy, false);
429 if (*sym_hash != NULL)
430 {
431 if (((*sym_hash)->coff_link_hash_flags
432 & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
433 && (*sym_hash)->root.type != bfd_link_hash_undefined
434 && (*sym_hash)->root.type != bfd_link_hash_undefweak)
435 (*_bfd_error_handler)
436 ("Warning: symbol `%s' is both section and non-section",
437 name);
438
439 addit = false;
440 }
441 }
442
49147fca
ILT
443 /* The Microsoft Visual C compiler does string pooling by
444 hashing the constants to an internal symbol name, and
08da05b0 445 relying on the linker comdat support to discard
49147fca
ILT
446 duplicate names. However, if one string is a literal and
447 one is a data initializer, one will end up in the .data
448 section and one will end up in the .rdata section. The
449 Microsoft linker will combine them into the .data
450 section, which seems to be wrong since it might cause the
451 literal to change.
452
453 As long as there are no external references to the
454 symbols, which there shouldn't be, we can treat the .data
455 and .rdata instances as separate symbols. The comdat
456 code in the linker will do the appropriate merging. Here
457 we avoid getting a multiple definition error for one of
458 these special symbols.
459
460 FIXME: I don't think this will work in the case where
461 there are two object files which use the constants as a
462 literal and two object files which use it as a data
463 initializer. One or the other of the second object files
464 is going to wind up with an inappropriate reference. */
465 if (obj_pe (abfd)
466 && (classification == COFF_SYMBOL_GLOBAL
467 || classification == COFF_SYMBOL_PE_SECTION)
468 && section->comdat != NULL
469 && strncmp (name, "??_", 3) == 0
470 && strcmp (name, section->comdat->name) == 0)
471 {
472 if (*sym_hash == NULL)
473 *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
474 name, false, copy, false);
475 if (*sym_hash != NULL
476 && (*sym_hash)->root.type == bfd_link_hash_defined
477 && (*sym_hash)->root.u.def.section->comdat != NULL
478 && strcmp ((*sym_hash)->root.u.def.section->comdat->name,
479 section->comdat->name) == 0)
480 addit = false;
481 }
482
7fd9c191
ILT
483 if (addit)
484 {
485 if (! (bfd_coff_link_add_one_symbol
486 (info, abfd, name, flags, section, value,
487 (const char *) NULL, copy, false,
488 (struct bfd_link_hash_entry **) sym_hash)))
489 goto error_return;
490 }
491
492 if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
493 (*sym_hash)->coff_link_hash_flags |=
494 COFF_LINK_HASH_PE_SECTION_SYMBOL;
252b5132 495
2820a0b7
ILT
496 /* Limit the alignment of a common symbol to the possible
497 alignment of a section. There is no point to permitting
498 a higher alignment for a common symbol: we can not
499 guarantee it, and it may cause us to allocate extra space
500 in the common section. */
252b5132
RH
501 if (section == bfd_com_section_ptr
502 && (*sym_hash)->root.type == bfd_link_hash_common
503 && ((*sym_hash)->root.u.c.p->alignment_power
504 > bfd_coff_default_section_alignment_power (abfd)))
505 (*sym_hash)->root.u.c.p->alignment_power
506 = bfd_coff_default_section_alignment_power (abfd);
507
508 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
509 {
5d3aaa74
ILT
510 /* If we don't have any symbol information currently in
511 the hash table, or if we are looking at a symbol
512 definition, then update the symbol class and type in
513 the hash table. */
514 if (((*sym_hash)->class == C_NULL
515 && (*sym_hash)->type == T_NULL)
516 || sym.n_scnum != 0
517 || (sym.n_value != 0
518 && (*sym_hash)->root.type != bfd_link_hash_defined
519 && (*sym_hash)->root.type != bfd_link_hash_defweak))
520 {
521 (*sym_hash)->class = sym.n_sclass;
522 if (sym.n_type != T_NULL)
523 {
524 /* We want to warn if the type changed, but not
525 if it changed from an unspecified type.
526 Testing the whole type byte may work, but the
527 change from (e.g.) a function of unspecified
528 type to function of known type also wants to
529 skip the warning. */
530 if ((*sym_hash)->type != T_NULL
531 && (*sym_hash)->type != sym.n_type
532 && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
533 && (BTYPE ((*sym_hash)->type) == T_NULL
534 || BTYPE (sym.n_type) == T_NULL)))
535 (*_bfd_error_handler)
536 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
537 name, (*sym_hash)->type, sym.n_type,
8f615d07 538 bfd_archive_filename (abfd));
5d3aaa74
ILT
539
540 /* We don't want to change from a meaningful
541 base type to a null one, but if we know
542 nothing, take what little we might now know. */
543 if (BTYPE (sym.n_type) != T_NULL
544 || (*sym_hash)->type == T_NULL)
545 (*sym_hash)->type = sym.n_type;
546 }
547 (*sym_hash)->auxbfd = abfd;
252b5132
RH
548 if (sym.n_numaux != 0)
549 {
550 union internal_auxent *alloc;
551 unsigned int i;
552 bfd_byte *eaux;
553 union internal_auxent *iaux;
554
555 (*sym_hash)->numaux = sym.n_numaux;
556 alloc = ((union internal_auxent *)
557 bfd_hash_allocate (&info->hash->table,
558 (sym.n_numaux
559 * sizeof (*alloc))));
560 if (alloc == NULL)
561 goto error_return;
562 for (i = 0, eaux = esym + symesz, iaux = alloc;
563 i < sym.n_numaux;
564 i++, eaux += symesz, iaux++)
565 bfd_coff_swap_aux_in (abfd, (PTR) eaux, sym.n_type,
dc810e39
AM
566 sym.n_sclass, (int) i,
567 sym.n_numaux, (PTR) iaux);
252b5132
RH
568 (*sym_hash)->aux = alloc;
569 }
570 }
571 }
5d54c628
ILT
572
573 if (classification == COFF_SYMBOL_PE_SECTION
574 && (*sym_hash)->numaux != 0)
575 {
576 /* Some PE sections (such as .bss) have a zero size in
577 the section header, but a non-zero size in the AUX
578 record. Correct that here.
579
580 FIXME: This is not at all the right place to do this.
581 For example, it won't help objdump. This needs to be
582 done when we swap in the section header. */
583
584 BFD_ASSERT ((*sym_hash)->numaux == 1);
585 if (section->_raw_size == 0)
586 section->_raw_size = (*sym_hash)->aux[0].x_scn.x_scnlen;
587
588 /* FIXME: We could test whether the section sizes
589 matches the size in the aux entry, but apparently
590 that sometimes fails unexpectedly. */
591 }
252b5132
RH
592 }
593
594 esym += (sym.n_numaux + 1) * symesz;
595 sym_hash += sym.n_numaux + 1;
596 }
597
598 /* If this is a non-traditional, non-relocateable link, try to
599 optimize the handling of any .stab/.stabstr sections. */
600 if (! info->relocateable
601 && ! info->traditional_format
602 && info->hash->creator->flavour == bfd_get_flavour (abfd)
603 && (info->strip != strip_all && info->strip != strip_debugger))
604 {
605 asection *stab, *stabstr;
606
607 stab = bfd_get_section_by_name (abfd, ".stab");
608 if (stab != NULL)
609 {
610 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
611
612 if (stabstr != NULL)
613 {
614 struct coff_link_hash_table *table;
615 struct coff_section_tdata *secdata;
616
617 secdata = coff_section_data (abfd, stab);
618 if (secdata == NULL)
619 {
dc810e39
AM
620 amt = sizeof (struct coff_section_tdata);
621 stab->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
252b5132
RH
622 if (stab->used_by_bfd == NULL)
623 goto error_return;
624 secdata = coff_section_data (abfd, stab);
625 }
626
627 table = coff_hash_table (info);
628
629 if (! _bfd_link_section_stabs (abfd, &table->stab_info,
630 stab, stabstr,
631 &secdata->stab_info))
632 goto error_return;
633 }
634 }
635 }
636
637 obj_coff_keep_syms (abfd) = keep_syms;
638
639 return true;
640
641 error_return:
642 obj_coff_keep_syms (abfd) = keep_syms;
643 return false;
644}
645\f
646/* Do the final link step. */
647
648boolean
649_bfd_coff_final_link (abfd, info)
650 bfd *abfd;
651 struct bfd_link_info *info;
652{
653 bfd_size_type symesz;
654 struct coff_final_link_info finfo;
655 boolean debug_merge_allocated;
656 boolean long_section_names;
657 asection *o;
658 struct bfd_link_order *p;
dc810e39
AM
659 bfd_size_type max_sym_count;
660 bfd_size_type max_lineno_count;
661 bfd_size_type max_reloc_count;
662 bfd_size_type max_output_reloc_count;
663 bfd_size_type max_contents_size;
252b5132
RH
664 file_ptr rel_filepos;
665 unsigned int relsz;
666 file_ptr line_filepos;
667 unsigned int linesz;
668 bfd *sub;
669 bfd_byte *external_relocs = NULL;
670 char strbuf[STRING_SIZE_SIZE];
dc810e39 671 bfd_size_type amt;
252b5132
RH
672
673 symesz = bfd_coff_symesz (abfd);
674
675 finfo.info = info;
676 finfo.output_bfd = abfd;
677 finfo.strtab = NULL;
678 finfo.section_info = NULL;
679 finfo.last_file_index = -1;
680 finfo.last_bf_index = -1;
681 finfo.internal_syms = NULL;
682 finfo.sec_ptrs = NULL;
683 finfo.sym_indices = NULL;
684 finfo.outsyms = NULL;
685 finfo.linenos = NULL;
686 finfo.contents = NULL;
687 finfo.external_relocs = NULL;
688 finfo.internal_relocs = NULL;
689 finfo.global_to_static = false;
690 debug_merge_allocated = false;
691
692 coff_data (abfd)->link_info = info;
693
694 finfo.strtab = _bfd_stringtab_init ();
695 if (finfo.strtab == NULL)
696 goto error_return;
697
698 if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
699 goto error_return;
700 debug_merge_allocated = true;
701
702 /* Compute the file positions for all the sections. */
703 if (! abfd->output_has_begun)
704 {
705 if (! bfd_coff_compute_section_file_positions (abfd))
706 goto error_return;
707 }
708
709 /* Count the line numbers and relocation entries required for the
710 output file. Set the file positions for the relocs. */
711 rel_filepos = obj_relocbase (abfd);
712 relsz = bfd_coff_relsz (abfd);
713 max_contents_size = 0;
714 max_lineno_count = 0;
715 max_reloc_count = 0;
716
717 long_section_names = false;
718 for (o = abfd->sections; o != NULL; o = o->next)
719 {
720 o->reloc_count = 0;
721 o->lineno_count = 0;
722 for (p = o->link_order_head; p != NULL; p = p->next)
723 {
724 if (p->type == bfd_indirect_link_order)
725 {
726 asection *sec;
727
728 sec = p->u.indirect.section;
729
730 /* Mark all sections which are to be included in the
731 link. This will normally be every section. We need
732 to do this so that we can identify any sections which
733 the linker has decided to not include. */
734 sec->linker_mark = true;
735
736 if (info->strip == strip_none
737 || info->strip == strip_some)
738 o->lineno_count += sec->lineno_count;
739
740 if (info->relocateable)
741 o->reloc_count += sec->reloc_count;
742
743 if (sec->_raw_size > max_contents_size)
744 max_contents_size = sec->_raw_size;
745 if (sec->lineno_count > max_lineno_count)
746 max_lineno_count = sec->lineno_count;
747 if (sec->reloc_count > max_reloc_count)
748 max_reloc_count = sec->reloc_count;
749 }
750 else if (info->relocateable
751 && (p->type == bfd_section_reloc_link_order
752 || p->type == bfd_symbol_reloc_link_order))
753 ++o->reloc_count;
754 }
755 if (o->reloc_count == 0)
756 o->rel_filepos = 0;
757 else
758 {
759 o->flags |= SEC_RELOC;
760 o->rel_filepos = rel_filepos;
761 rel_filepos += o->reloc_count * relsz;
762 }
763
764 if (bfd_coff_long_section_names (abfd)
765 && strlen (o->name) > SCNNMLEN)
766 {
767 /* This section has a long name which must go in the string
768 table. This must correspond to the code in
769 coff_write_object_contents which puts the string index
770 into the s_name field of the section header. That is why
771 we pass hash as false. */
772 if (_bfd_stringtab_add (finfo.strtab, o->name, false, false)
773 == (bfd_size_type) -1)
774 goto error_return;
775 long_section_names = true;
776 }
777 }
778
779 /* If doing a relocateable link, allocate space for the pointers we
780 need to keep. */
781 if (info->relocateable)
782 {
783 unsigned int i;
784
785 /* We use section_count + 1, rather than section_count, because
786 the target_index fields are 1 based. */
dc810e39
AM
787 amt = abfd->section_count + 1;
788 amt *= sizeof (struct coff_link_section_info);
789 finfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
252b5132
RH
790 if (finfo.section_info == NULL)
791 goto error_return;
792 for (i = 0; i <= abfd->section_count; i++)
793 {
794 finfo.section_info[i].relocs = NULL;
795 finfo.section_info[i].rel_hashes = NULL;
796 }
797 }
798
799 /* We now know the size of the relocs, so we can determine the file
800 positions of the line numbers. */
801 line_filepos = rel_filepos;
802 linesz = bfd_coff_linesz (abfd);
803 max_output_reloc_count = 0;
804 for (o = abfd->sections; o != NULL; o = o->next)
805 {
806 if (o->lineno_count == 0)
807 o->line_filepos = 0;
808 else
809 {
810 o->line_filepos = line_filepos;
811 line_filepos += o->lineno_count * linesz;
812 }
813
814 if (o->reloc_count != 0)
815 {
816 /* We don't know the indices of global symbols until we have
817 written out all the local symbols. For each section in
818 the output file, we keep an array of pointers to hash
819 table entries. Each entry in the array corresponds to a
820 reloc. When we find a reloc against a global symbol, we
821 set the corresponding entry in this array so that we can
822 fix up the symbol index after we have written out all the
823 local symbols.
824
825 Because of this problem, we also keep the relocs in
826 memory until the end of the link. This wastes memory,
827 but only when doing a relocateable link, which is not the
828 common case. */
829 BFD_ASSERT (info->relocateable);
dc810e39
AM
830 amt = o->reloc_count;
831 amt *= sizeof (struct internal_reloc);
252b5132 832 finfo.section_info[o->target_index].relocs =
dc810e39
AM
833 (struct internal_reloc *) bfd_malloc (amt);
834 amt = o->reloc_count;
835 amt *= sizeof (struct coff_link_hash_entry *);
252b5132 836 finfo.section_info[o->target_index].rel_hashes =
dc810e39 837 (struct coff_link_hash_entry **) bfd_malloc (amt);
252b5132
RH
838 if (finfo.section_info[o->target_index].relocs == NULL
839 || finfo.section_info[o->target_index].rel_hashes == NULL)
840 goto error_return;
841
842 if (o->reloc_count > max_output_reloc_count)
843 max_output_reloc_count = o->reloc_count;
844 }
845
846 /* Reset the reloc and lineno counts, so that we can use them to
847 count the number of entries we have output so far. */
848 o->reloc_count = 0;
849 o->lineno_count = 0;
850 }
851
852 obj_sym_filepos (abfd) = line_filepos;
853
854 /* Figure out the largest number of symbols in an input BFD. Take
855 the opportunity to clear the output_has_begun fields of all the
856 input BFD's. */
857 max_sym_count = 0;
858 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
859 {
860 size_t sz;
861
862 sub->output_has_begun = false;
863 sz = obj_raw_syment_count (sub);
864 if (sz > max_sym_count)
865 max_sym_count = sz;
866 }
867
868 /* Allocate some buffers used while linking. */
dc810e39
AM
869 amt = max_sym_count * sizeof (struct internal_syment);
870 finfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
871 amt = max_sym_count * sizeof (asection *);
872 finfo.sec_ptrs = (asection **) bfd_malloc (amt);
873 amt = max_sym_count * sizeof (long);
874 finfo.sym_indices = (long *) bfd_malloc (amt);
875 finfo.outsyms = (bfd_byte *) bfd_malloc ((max_sym_count + 1) * symesz);
876 amt = max_lineno_count * bfd_coff_linesz (abfd);
877 finfo.linenos = (bfd_byte *) bfd_malloc (amt);
252b5132 878 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
dc810e39
AM
879 amt = max_reloc_count * relsz;
880 finfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
252b5132 881 if (! info->relocateable)
dc810e39
AM
882 {
883 amt = max_reloc_count * sizeof (struct internal_reloc);
884 finfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
885 }
252b5132
RH
886 if ((finfo.internal_syms == NULL && max_sym_count > 0)
887 || (finfo.sec_ptrs == NULL && max_sym_count > 0)
888 || (finfo.sym_indices == NULL && max_sym_count > 0)
889 || finfo.outsyms == NULL
890 || (finfo.linenos == NULL && max_lineno_count > 0)
891 || (finfo.contents == NULL && max_contents_size > 0)
892 || (finfo.external_relocs == NULL && max_reloc_count > 0)
893 || (! info->relocateable
894 && finfo.internal_relocs == NULL
895 && max_reloc_count > 0))
896 goto error_return;
897
898 /* We now know the position of everything in the file, except that
899 we don't know the size of the symbol table and therefore we don't
900 know where the string table starts. We just build the string
901 table in memory as we go along. We process all the relocations
902 for a single input file at once. */
903 obj_raw_syment_count (abfd) = 0;
904
905 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
906 {
907 if (! bfd_coff_start_final_link (abfd, info))
908 goto error_return;
909 }
910
911 for (o = abfd->sections; o != NULL; o = o->next)
912 {
913 for (p = o->link_order_head; p != NULL; p = p->next)
914 {
915 if (p->type == bfd_indirect_link_order
9bd09e22 916 && bfd_family_coff (p->u.indirect.section->owner))
252b5132
RH
917 {
918 sub = p->u.indirect.section->owner;
919 if (! bfd_coff_link_output_has_begun (sub, & finfo))
920 {
921 if (! _bfd_coff_link_input_bfd (&finfo, sub))
922 goto error_return;
923 sub->output_has_begun = true;
924 }
925 }
926 else if (p->type == bfd_section_reloc_link_order
927 || p->type == bfd_symbol_reloc_link_order)
928 {
929 if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
930 goto error_return;
931 }
932 else
933 {
934 if (! _bfd_default_link_order (abfd, info, o, p))
935 goto error_return;
936 }
937 }
938 }
939
940 if (! bfd_coff_final_link_postscript (abfd, & finfo))
941 goto error_return;
244148ad 942
252b5132
RH
943 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
944
945 coff_debug_merge_hash_table_free (&finfo.debug_merge);
946 debug_merge_allocated = false;
947
948 if (finfo.internal_syms != NULL)
949 {
950 free (finfo.internal_syms);
951 finfo.internal_syms = NULL;
952 }
953 if (finfo.sec_ptrs != NULL)
954 {
955 free (finfo.sec_ptrs);
956 finfo.sec_ptrs = NULL;
957 }
958 if (finfo.sym_indices != NULL)
959 {
960 free (finfo.sym_indices);
961 finfo.sym_indices = NULL;
962 }
963 if (finfo.linenos != NULL)
964 {
965 free (finfo.linenos);
966 finfo.linenos = NULL;
967 }
968 if (finfo.contents != NULL)
969 {
970 free (finfo.contents);
971 finfo.contents = NULL;
972 }
973 if (finfo.external_relocs != NULL)
974 {
975 free (finfo.external_relocs);
976 finfo.external_relocs = NULL;
977 }
978 if (finfo.internal_relocs != NULL)
979 {
980 free (finfo.internal_relocs);
981 finfo.internal_relocs = NULL;
982 }
983
984 /* The value of the last C_FILE symbol is supposed to be the symbol
985 index of the first external symbol. Write it out again if
986 necessary. */
987 if (finfo.last_file_index != -1
988 && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
989 {
dc810e39
AM
990 file_ptr pos;
991
252b5132
RH
992 finfo.last_file.n_value = obj_raw_syment_count (abfd);
993 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
994 (PTR) finfo.outsyms);
dc810e39
AM
995
996 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
997 if (bfd_seek (abfd, pos, SEEK_SET) != 0
998 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
252b5132
RH
999 return false;
1000 }
1001
1002 /* If doing task linking (ld --task-link) then make a pass through the
1003 global symbols, writing out any that are defined, and making them
244148ad 1004 static. */
252b5132
RH
1005 if (info->task_link)
1006 {
1007 finfo.failed = false;
1008 coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_task_globals,
1009 (PTR) &finfo);
1010 if (finfo.failed)
1011 goto error_return;
1012 }
1013
1014 /* Write out the global symbols. */
1015 finfo.failed = false;
1016 coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_global_sym,
1017 (PTR) &finfo);
1018 if (finfo.failed)
1019 goto error_return;
1020
1021 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1022 if (finfo.outsyms != NULL)
1023 {
1024 free (finfo.outsyms);
1025 finfo.outsyms = NULL;
1026 }
1027
1028 if (info->relocateable && max_output_reloc_count > 0)
1029 {
1030 /* Now that we have written out all the global symbols, we know
1031 the symbol indices to use for relocs against them, and we can
1032 finally write out the relocs. */
dc810e39
AM
1033 amt = max_output_reloc_count * relsz;
1034 external_relocs = (bfd_byte *) bfd_malloc (amt);
252b5132
RH
1035 if (external_relocs == NULL)
1036 goto error_return;
1037
1038 for (o = abfd->sections; o != NULL; o = o->next)
1039 {
1040 struct internal_reloc *irel;
1041 struct internal_reloc *irelend;
1042 struct coff_link_hash_entry **rel_hash;
1043 bfd_byte *erel;
1044
1045 if (o->reloc_count == 0)
1046 continue;
1047
1048 irel = finfo.section_info[o->target_index].relocs;
1049 irelend = irel + o->reloc_count;
1050 rel_hash = finfo.section_info[o->target_index].rel_hashes;
1051 erel = external_relocs;
1052 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
1053 {
1054 if (*rel_hash != NULL)
1055 {
1056 BFD_ASSERT ((*rel_hash)->indx >= 0);
1057 irel->r_symndx = (*rel_hash)->indx;
1058 }
1059 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
1060 }
1061
1062 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
dc810e39
AM
1063 || (bfd_bwrite ((PTR) external_relocs,
1064 (bfd_size_type) relsz * o->reloc_count, abfd)
1065 != (bfd_size_type) relsz * o->reloc_count))
252b5132
RH
1066 goto error_return;
1067 }
1068
1069 free (external_relocs);
1070 external_relocs = NULL;
1071 }
1072
1073 /* Free up the section information. */
1074 if (finfo.section_info != NULL)
1075 {
1076 unsigned int i;
1077
1078 for (i = 0; i < abfd->section_count; i++)
1079 {
1080 if (finfo.section_info[i].relocs != NULL)
1081 free (finfo.section_info[i].relocs);
1082 if (finfo.section_info[i].rel_hashes != NULL)
1083 free (finfo.section_info[i].rel_hashes);
1084 }
1085 free (finfo.section_info);
1086 finfo.section_info = NULL;
1087 }
1088
1089 /* If we have optimized stabs strings, output them. */
1090 if (coff_hash_table (info)->stab_info != NULL)
1091 {
1092 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
1093 return false;
1094 }
1095
1096 /* Write out the string table. */
1097 if (obj_raw_syment_count (abfd) != 0 || long_section_names)
1098 {
dc810e39
AM
1099 file_ptr pos;
1100
1101 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
1102 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
252b5132
RH
1103 return false;
1104
1105#if STRING_SIZE_SIZE == 4
dc810e39
AM
1106 H_PUT_32 (abfd,
1107 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
1108 strbuf);
252b5132 1109#else
dc810e39 1110 #error Change H_PUT_32 above
252b5132
RH
1111#endif
1112
dc810e39
AM
1113 if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1114 != STRING_SIZE_SIZE)
252b5132
RH
1115 return false;
1116
1117 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
1118 return false;
d71f672e
PB
1119
1120 obj_coff_strings_written (abfd) = true;
252b5132
RH
1121 }
1122
1123 _bfd_stringtab_free (finfo.strtab);
1124
1125 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1126 not try to write out the symbols. */
1127 bfd_get_symcount (abfd) = 0;
1128
1129 return true;
1130
1131 error_return:
1132 if (debug_merge_allocated)
1133 coff_debug_merge_hash_table_free (&finfo.debug_merge);
1134 if (finfo.strtab != NULL)
1135 _bfd_stringtab_free (finfo.strtab);
1136 if (finfo.section_info != NULL)
1137 {
1138 unsigned int i;
1139
1140 for (i = 0; i < abfd->section_count; i++)
1141 {
1142 if (finfo.section_info[i].relocs != NULL)
1143 free (finfo.section_info[i].relocs);
1144 if (finfo.section_info[i].rel_hashes != NULL)
1145 free (finfo.section_info[i].rel_hashes);
1146 }
1147 free (finfo.section_info);
1148 }
1149 if (finfo.internal_syms != NULL)
1150 free (finfo.internal_syms);
1151 if (finfo.sec_ptrs != NULL)
1152 free (finfo.sec_ptrs);
1153 if (finfo.sym_indices != NULL)
1154 free (finfo.sym_indices);
1155 if (finfo.outsyms != NULL)
1156 free (finfo.outsyms);
1157 if (finfo.linenos != NULL)
1158 free (finfo.linenos);
1159 if (finfo.contents != NULL)
1160 free (finfo.contents);
1161 if (finfo.external_relocs != NULL)
1162 free (finfo.external_relocs);
1163 if (finfo.internal_relocs != NULL)
1164 free (finfo.internal_relocs);
1165 if (external_relocs != NULL)
1166 free (external_relocs);
1167 return false;
1168}
1169
1170/* parse out a -heap <reserved>,<commit> line */
1171
1172static char *
1173dores_com (ptr, output_bfd, heap)
1174 char *ptr;
1175 bfd *output_bfd;
1176 int heap;
1177{
244148ad 1178 if (coff_data(output_bfd)->pe)
252b5132
RH
1179 {
1180 int val = strtoul (ptr, &ptr, 0);
1181 if (heap)
dc810e39 1182 pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val;
252b5132 1183 else
dc810e39 1184 pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val;
252b5132 1185
244148ad 1186 if (ptr[0] == ',')
252b5132 1187 {
dc810e39 1188 val = strtoul (ptr+1, &ptr, 0);
252b5132 1189 if (heap)
dc810e39 1190 pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit = val;
252b5132 1191 else
dc810e39 1192 pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit = val;
252b5132
RH
1193 }
1194 }
1195 return ptr;
1196}
1197
1198static char *get_name(ptr, dst)
1199char *ptr;
1200char **dst;
1201{
1202 while (*ptr == ' ')
1203 ptr++;
1204 *dst = ptr;
1205 while (*ptr && *ptr != ' ')
1206 ptr++;
1207 *ptr = 0;
1208 return ptr+1;
1209}
1210
1211/* Process any magic embedded commands in a section called .drectve */
244148ad 1212
252b5132
RH
1213static int
1214process_embedded_commands (output_bfd, info, abfd)
1215 bfd *output_bfd;
5f771d47 1216 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
1217 bfd *abfd;
1218{
1219 asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1220 char *s;
1221 char *e;
1222 char *copy;
244148ad 1223 if (!sec)
252b5132 1224 return 1;
244148ad 1225
dc810e39 1226 copy = bfd_malloc (sec->_raw_size);
244148ad 1227 if (!copy)
252b5132 1228 return 0;
dc810e39 1229 if (! bfd_get_section_contents(abfd, sec, copy, (bfd_vma) 0, sec->_raw_size))
252b5132
RH
1230 {
1231 free (copy);
1232 return 0;
1233 }
1234 e = copy + sec->_raw_size;
244148ad 1235 for (s = copy; s < e ; )
252b5132
RH
1236 {
1237 if (s[0]!= '-') {
1238 s++;
1239 continue;
1240 }
1241 if (strncmp (s,"-attr", 5) == 0)
1242 {
1243 char *name;
1244 char *attribs;
1245 asection *asec;
1246
1247 int loop = 1;
1248 int had_write = 0;
1249 int had_read = 0;
1250 int had_exec= 0;
1251 int had_shared= 0;
1252 s += 5;
1253 s = get_name(s, &name);
1254 s = get_name(s, &attribs);
1255 while (loop) {
244148ad 1256 switch (*attribs++)
252b5132
RH
1257 {
1258 case 'W':
1259 had_write = 1;
1260 break;
1261 case 'R':
1262 had_read = 1;
1263 break;
1264 case 'S':
1265 had_shared = 1;
1266 break;
1267 case 'X':
1268 had_exec = 1;
1269 break;
1270 default:
1271 loop = 0;
1272 }
1273 }
1274 asec = bfd_get_section_by_name (abfd, name);
1275 if (asec) {
1276 if (had_exec)
1277 asec->flags |= SEC_CODE;
1278 if (!had_write)
1279 asec->flags |= SEC_READONLY;
1280 }
1281 }
1282 else if (strncmp (s,"-heap", 5) == 0)
1283 {
1284 s = dores_com (s+5, output_bfd, 1);
1285 }
1286 else if (strncmp (s,"-stack", 6) == 0)
1287 {
1288 s = dores_com (s+6, output_bfd, 0);
1289 }
244148ad 1290 else
252b5132
RH
1291 s++;
1292 }
1293 free (copy);
1294 return 1;
1295}
1296
1297/* Place a marker against all symbols which are used by relocations.
1298 This marker can be picked up by the 'do we skip this symbol ?'
1299 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
244148ad 1300 that symbol.
252b5132
RH
1301 */
1302
1303static void
1304mark_relocs (finfo, input_bfd)
1305 struct coff_final_link_info * finfo;
1306 bfd * input_bfd;
1307{
1308 asection * a;
1309
1310 if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
1311 return;
244148ad 1312
252b5132
RH
1313 for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
1314 {
1315 struct internal_reloc * internal_relocs;
1316 struct internal_reloc * irel;
1317 struct internal_reloc * irelend;
1318
252b5132
RH
1319 if ((a->flags & SEC_RELOC) == 0 || a->reloc_count < 1)
1320 continue;
1321
1322 /* Read in the relocs. */
1323 internal_relocs = _bfd_coff_read_internal_relocs
1324 (input_bfd, a, false,
1325 finfo->external_relocs,
1326 finfo->info->relocateable,
1327 (finfo->info->relocateable
1328 ? (finfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
1329 : finfo->internal_relocs)
1330 );
244148ad 1331
252b5132
RH
1332 if (internal_relocs == NULL)
1333 continue;
1334
1335 irel = internal_relocs;
1336 irelend = irel + a->reloc_count;
1337
1338 /* Place a mark in the sym_indices array (whose entries have
1339 been initialised to 0) for all of the symbols that are used
1340 in the relocation table. This will then be picked up in the
1341 skip/don't pass */
244148ad 1342
252b5132
RH
1343 for (; irel < irelend; irel++)
1344 {
1345 finfo->sym_indices[ irel->r_symndx ] = -1;
1346 }
1347 }
1348}
1349
1350/* Link an input file into the linker output file. This function
1351 handles all the sections and relocations of the input file at once. */
1352
1353boolean
1354_bfd_coff_link_input_bfd (finfo, input_bfd)
1355 struct coff_final_link_info *finfo;
1356 bfd *input_bfd;
1357{
b2d638c7
ILT
1358 unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
1359 unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
1360#if 0
1361 unsigned int n_btmask = coff_data (input_bfd)->local_n_btmask;
1362#endif
252b5132
RH
1363 boolean (*adjust_symndx) PARAMS ((bfd *, struct bfd_link_info *, bfd *,
1364 asection *, struct internal_reloc *,
1365 boolean *));
1366 bfd *output_bfd;
1367 const char *strings;
1368 bfd_size_type syment_base;
252b5132
RH
1369 boolean copy, hash;
1370 bfd_size_type isymesz;
1371 bfd_size_type osymesz;
1372 bfd_size_type linesz;
1373 bfd_byte *esym;
1374 bfd_byte *esym_end;
1375 struct internal_syment *isymp;
1376 asection **secpp;
1377 long *indexp;
1378 unsigned long output_index;
1379 bfd_byte *outsym;
1380 struct coff_link_hash_entry **sym_hash;
1381 asection *o;
1382
1383 /* Move all the symbols to the output file. */
1384
1385 output_bfd = finfo->output_bfd;
252b5132
RH
1386 strings = NULL;
1387 syment_base = obj_raw_syment_count (output_bfd);
1388 isymesz = bfd_coff_symesz (input_bfd);
1389 osymesz = bfd_coff_symesz (output_bfd);
1390 linesz = bfd_coff_linesz (input_bfd);
1391 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1392
252b5132
RH
1393 copy = false;
1394 if (! finfo->info->keep_memory)
1395 copy = true;
1396 hash = true;
1397 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1398 hash = false;
1399
1400 if (! _bfd_coff_get_external_symbols (input_bfd))
1401 return false;
1402
1403 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1404 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1405 isymp = finfo->internal_syms;
1406 secpp = finfo->sec_ptrs;
1407 indexp = finfo->sym_indices;
1408 output_index = syment_base;
1409 outsym = finfo->outsyms;
1410
1411 if (coff_data (output_bfd)->pe)
1412 {
1413 if (! process_embedded_commands (output_bfd, finfo->info, input_bfd))
1414 return false;
1415 }
1416
1417 /* If we are going to perform relocations and also strip/discard some symbols
1418 then we must make sure that we do not strip/discard those symbols that are
1419 going to be involved in the relocations */
1420 if (( finfo->info->strip != strip_none
1421 || finfo->info->discard != discard_none)
1422 && finfo->info->relocateable)
1423 {
1424 /* mark the symbol array as 'not-used' */
244148ad
KH
1425 memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
1426
252b5132
RH
1427 mark_relocs (finfo, input_bfd);
1428 }
1429
1430 while (esym < esym_end)
1431 {
1432 struct internal_syment isym;
5d54c628 1433 enum coff_symbol_classification classification;
252b5132
RH
1434 boolean skip;
1435 boolean global;
1436 boolean dont_skip_symbol;
1437 int add;
1438
1439 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
1440
1441 /* Make a copy of *isymp so that the relocate_section function
1442 always sees the original values. This is more reliable than
1443 always recomputing the symbol value even if we are stripping
1444 the symbol. */
1445 isym = *isymp;
1446
5d54c628
ILT
1447 classification = bfd_coff_classify_symbol (input_bfd, &isym);
1448 switch (classification)
252b5132 1449 {
5d54c628
ILT
1450 default:
1451 abort ();
1452 case COFF_SYMBOL_GLOBAL:
1453 case COFF_SYMBOL_PE_SECTION:
1454 case COFF_SYMBOL_LOCAL:
1455 *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1456 break;
1457 case COFF_SYMBOL_COMMON:
1458 *secpp = bfd_com_section_ptr;
1459 break;
1460 case COFF_SYMBOL_UNDEFINED:
1461 *secpp = bfd_und_section_ptr;
1462 break;
252b5132
RH
1463 }
1464
1465 /* Extract the flag indicating if this symbol is used by a
1466 relocation. */
1467 if ((finfo->info->strip != strip_none
1468 || finfo->info->discard != discard_none)
1469 && finfo->info->relocateable)
1470 dont_skip_symbol = *indexp;
1471 else
1472 dont_skip_symbol = false;
244148ad 1473
252b5132
RH
1474 *indexp = -1;
1475
1476 skip = false;
1477 global = false;
1478 add = 1 + isym.n_numaux;
1479
1480 /* If we are stripping all symbols, we want to skip this one. */
1481 if (finfo->info->strip == strip_all && ! dont_skip_symbol)
1482 skip = true;
1483
1484 if (! skip)
1485 {
5d54c628 1486 switch (classification)
252b5132 1487 {
5d54c628
ILT
1488 default:
1489 abort ();
1490 case COFF_SYMBOL_GLOBAL:
1491 case COFF_SYMBOL_COMMON:
1492 case COFF_SYMBOL_PE_SECTION:
252b5132
RH
1493 /* This is a global symbol. Global symbols come at the
1494 end of the symbol table, so skip them for now.
1495 Locally defined function symbols, however, are an
1496 exception, and are not moved to the end. */
1497 global = true;
5d54c628 1498 if (! ISFCN (isym.n_type))
252b5132 1499 skip = true;
5d54c628
ILT
1500 break;
1501
1502 case COFF_SYMBOL_UNDEFINED:
1503 /* Undefined symbols are left for the end. */
1504 global = true;
1505 skip = true;
1506 break;
1507
1508 case COFF_SYMBOL_LOCAL:
252b5132
RH
1509 /* This is a local symbol. Skip it if we are discarding
1510 local symbols. */
1511 if (finfo->info->discard == discard_all && ! dont_skip_symbol)
1512 skip = true;
5d54c628 1513 break;
252b5132
RH
1514 }
1515 }
1516
440c4607
NC
1517#ifndef COFF_WITH_PE
1518 /* Skip section symbols for sections which are not going to be
862517b6 1519 emitted. */
440c4607
NC
1520 if (!skip
1521 && isym.n_sclass == C_STAT
1522 && isym.n_type == T_NULL
1523 && isym.n_numaux > 0)
1524 {
862517b6 1525 if ((*secpp)->output_section == bfd_abs_section_ptr)
440c4607
NC
1526 skip = true;
1527 }
1528#endif
1529
252b5132
RH
1530 /* If we stripping debugging symbols, and this is a debugging
1531 symbol, then skip it. FIXME: gas sets the section to N_ABS
1532 for some types of debugging symbols; I don't know if this is
1533 a bug or not. In any case, we handle it here. */
1534 if (! skip
1535 && finfo->info->strip == strip_debugger
1536 && ! dont_skip_symbol
1537 && (isym.n_scnum == N_DEBUG
1538 || (isym.n_scnum == N_ABS
1539 && (isym.n_sclass == C_AUTO
1540 || isym.n_sclass == C_REG
1541 || isym.n_sclass == C_MOS
1542 || isym.n_sclass == C_MOE
1543 || isym.n_sclass == C_MOU
1544 || isym.n_sclass == C_ARG
1545 || isym.n_sclass == C_REGPARM
1546 || isym.n_sclass == C_FIELD
1547 || isym.n_sclass == C_EOS))))
1548 skip = true;
1549
1550 /* If some symbols are stripped based on the name, work out the
1551 name and decide whether to skip this symbol. */
1552 if (! skip
1553 && (finfo->info->strip == strip_some
1554 || finfo->info->discard == discard_l))
1555 {
1556 const char *name;
1557 char buf[SYMNMLEN + 1];
1558
1559 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1560 if (name == NULL)
1561 return false;
1562
1563 if (! dont_skip_symbol
1564 && ((finfo->info->strip == strip_some
1565 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
1566 false) == NULL))
1567 || (! global
1568 && finfo->info->discard == discard_l
1569 && bfd_is_local_label_name (input_bfd, name))))
1570 skip = true;
1571 }
1572
1573 /* If this is an enum, struct, or union tag, see if we have
1574 already output an identical type. */
1575 if (! skip
1576 && (finfo->output_bfd->flags & BFD_TRADITIONAL_FORMAT) == 0
1577 && (isym.n_sclass == C_ENTAG
1578 || isym.n_sclass == C_STRTAG
1579 || isym.n_sclass == C_UNTAG)
1580 && isym.n_numaux == 1)
1581 {
1582 const char *name;
1583 char buf[SYMNMLEN + 1];
1584 struct coff_debug_merge_hash_entry *mh;
1585 struct coff_debug_merge_type *mt;
1586 union internal_auxent aux;
1587 struct coff_debug_merge_element **epp;
1588 bfd_byte *esl, *eslend;
1589 struct internal_syment *islp;
dc810e39 1590 bfd_size_type amt;
252b5132
RH
1591
1592 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1593 if (name == NULL)
1594 return false;
1595
1596 /* Ignore fake names invented by compiler; treat them all as
1597 the same name. */
1598 if (*name == '~' || *name == '.' || *name == '$'
1599 || (*name == bfd_get_symbol_leading_char (input_bfd)
1600 && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
1601 name = "";
1602
1603 mh = coff_debug_merge_hash_lookup (&finfo->debug_merge, name,
1604 true, true);
1605 if (mh == NULL)
1606 return false;
1607
1608 /* Allocate memory to hold type information. If this turns
1609 out to be a duplicate, we pass this address to
1610 bfd_release. */
dc810e39
AM
1611 amt = sizeof (struct coff_debug_merge_type);
1612 mt = (struct coff_debug_merge_type *) bfd_alloc (input_bfd, amt);
252b5132
RH
1613 if (mt == NULL)
1614 return false;
1615 mt->class = isym.n_sclass;
1616
1617 /* Pick up the aux entry, which points to the end of the tag
1618 entries. */
1619 bfd_coff_swap_aux_in (input_bfd, (PTR) (esym + isymesz),
1620 isym.n_type, isym.n_sclass, 0, isym.n_numaux,
1621 (PTR) &aux);
1622
1623 /* Gather the elements. */
1624 epp = &mt->elements;
1625 mt->elements = NULL;
1626 islp = isymp + 2;
1627 esl = esym + 2 * isymesz;
1628 eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
1629 + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
1630 while (esl < eslend)
1631 {
1632 const char *elename;
1633 char elebuf[SYMNMLEN + 1];
00692651 1634 char *name_copy;
252b5132
RH
1635
1636 bfd_coff_swap_sym_in (input_bfd, (PTR) esl, (PTR) islp);
1637
dc810e39 1638 amt = sizeof (struct coff_debug_merge_element);
252b5132 1639 *epp = ((struct coff_debug_merge_element *)
dc810e39 1640 bfd_alloc (input_bfd, amt));
252b5132
RH
1641 if (*epp == NULL)
1642 return false;
1643
1644 elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1645 elebuf);
1646 if (elename == NULL)
1647 return false;
1648
dc810e39
AM
1649 amt = strlen (elename) + 1;
1650 name_copy = (char *) bfd_alloc (input_bfd, amt);
00692651 1651 if (name_copy == NULL)
252b5132 1652 return false;
00692651 1653 strcpy (name_copy, elename);
252b5132 1654
00692651 1655 (*epp)->name = name_copy;
252b5132
RH
1656 (*epp)->type = islp->n_type;
1657 (*epp)->tagndx = 0;
1658 if (islp->n_numaux >= 1
1659 && islp->n_type != T_NULL
1660 && islp->n_sclass != C_EOS)
1661 {
1662 union internal_auxent eleaux;
1663 long indx;
1664
1665 bfd_coff_swap_aux_in (input_bfd, (PTR) (esl + isymesz),
1666 islp->n_type, islp->n_sclass, 0,
1667 islp->n_numaux, (PTR) &eleaux);
1668 indx = eleaux.x_sym.x_tagndx.l;
1669
1670 /* FIXME: If this tagndx entry refers to a symbol
1671 defined later in this file, we just ignore it.
1672 Handling this correctly would be tedious, and may
1673 not be required. */
1674
1675 if (indx > 0
1676 && (indx
1677 < ((esym -
1678 (bfd_byte *) obj_coff_external_syms (input_bfd))
1679 / (long) isymesz)))
1680 {
1681 (*epp)->tagndx = finfo->sym_indices[indx];
1682 if ((*epp)->tagndx < 0)
1683 (*epp)->tagndx = 0;
1684 }
1685 }
1686 epp = &(*epp)->next;
1687 *epp = NULL;
1688
1689 esl += (islp->n_numaux + 1) * isymesz;
1690 islp += islp->n_numaux + 1;
1691 }
1692
1693 /* See if we already have a definition which matches this
1694 type. We always output the type if it has no elements,
1695 for simplicity. */
1696 if (mt->elements == NULL)
1697 bfd_release (input_bfd, (PTR) mt);
1698 else
1699 {
1700 struct coff_debug_merge_type *mtl;
1701
1702 for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1703 {
1704 struct coff_debug_merge_element *me, *mel;
1705
1706 if (mtl->class != mt->class)
1707 continue;
1708
1709 for (me = mt->elements, mel = mtl->elements;
1710 me != NULL && mel != NULL;
1711 me = me->next, mel = mel->next)
1712 {
1713 if (strcmp (me->name, mel->name) != 0
1714 || me->type != mel->type
1715 || me->tagndx != mel->tagndx)
1716 break;
1717 }
1718
1719 if (me == NULL && mel == NULL)
1720 break;
1721 }
1722
1723 if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1724 {
1725 /* This is the first definition of this type. */
1726 mt->indx = output_index;
1727 mt->next = mh->types;
1728 mh->types = mt;
1729 }
1730 else
1731 {
1732 /* This is a redefinition which can be merged. */
1733 bfd_release (input_bfd, (PTR) mt);
1734 *indexp = mtl->indx;
1735 add = (eslend - esym) / isymesz;
1736 skip = true;
1737 }
1738 }
1739 }
1740
1741 /* We now know whether we are to skip this symbol or not. */
1742 if (! skip)
1743 {
1744 /* Adjust the symbol in order to output it. */
1745
1746 if (isym._n._n_n._n_zeroes == 0
1747 && isym._n._n_n._n_offset != 0)
1748 {
1749 const char *name;
1750 bfd_size_type indx;
1751
1752 /* This symbol has a long name. Enter it in the string
1753 table we are building. Note that we do not check
1754 bfd_coff_symname_in_debug. That is only true for
1755 XCOFF, and XCOFF requires different linking code
1756 anyhow. */
1757 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
1758 (char *) NULL);
1759 if (name == NULL)
1760 return false;
1761 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
1762 if (indx == (bfd_size_type) -1)
1763 return false;
1764 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1765 }
1766
8add8e30 1767 switch (isym.n_sclass)
252b5132 1768 {
8add8e30
ILT
1769 case C_AUTO:
1770 case C_MOS:
1771 case C_EOS:
1772 case C_MOE:
1773 case C_MOU:
1774 case C_UNTAG:
1775 case C_STRTAG:
1776 case C_ENTAG:
1777 case C_TPDEF:
1778 case C_ARG:
1779 case C_USTATIC:
1780 case C_REG:
1781 case C_REGPARM:
1782 case C_FIELD:
1783 /* The symbol value should not be modified. */
1784 break;
1785
1786 case C_FCN:
1787 if (obj_pe (input_bfd)
1788 && strcmp (isym.n_name, ".bf") != 0
1789 && isym.n_scnum > 0)
1790 {
1791 /* For PE, .lf and .ef get their value left alone,
1792 while .bf gets relocated. However, they all have
1793 "real" section numbers, and need to be moved into
1794 the new section. */
1795 isym.n_scnum = (*secpp)->output_section->target_index;
1796 break;
1797 }
1798 /* Fall through. */
1799 default:
1800 case C_LABEL: /* Not completely sure about these 2 */
1801 case C_EXTDEF:
1802 case C_BLOCK:
1803 case C_EFCN:
1804 case C_NULL:
1805 case C_EXT:
1806 case C_STAT:
1807 case C_SECTION:
1808 case C_NT_WEAK:
1809 /* Compute new symbol location. */
1810 if (isym.n_scnum > 0)
1811 {
1812 isym.n_scnum = (*secpp)->output_section->target_index;
1813 isym.n_value += (*secpp)->output_offset;
1814 if (! obj_pe (input_bfd))
1815 isym.n_value -= (*secpp)->vma;
1816 if (! obj_pe (finfo->output_bfd))
1817 isym.n_value += (*secpp)->output_section->vma;
1818 }
1819 break;
1820
1821 case C_FILE:
1822 /* The value of a C_FILE symbol is the symbol index of
1823 the next C_FILE symbol. The value of the last C_FILE
1824 symbol is the symbol index to the first external
1825 symbol (actually, coff_renumber_symbols does not get
1826 this right--it just sets the value of the last C_FILE
1827 symbol to zero--and nobody has ever complained about
1828 it). We try to get this right, below, just before we
1829 write the symbols out, but in the general case we may
1830 have to write the symbol out twice. */
252b5132 1831
252b5132 1832 if (finfo->last_file_index != -1
cea4409c 1833 && finfo->last_file.n_value != (bfd_vma) output_index)
252b5132 1834 {
8add8e30
ILT
1835 /* We must correct the value of the last C_FILE
1836 entry. */
252b5132
RH
1837 finfo->last_file.n_value = output_index;
1838 if ((bfd_size_type) finfo->last_file_index >= syment_base)
1839 {
1840 /* The last C_FILE symbol is in this input file. */
1841 bfd_coff_swap_sym_out (output_bfd,
1842 (PTR) &finfo->last_file,
1843 (PTR) (finfo->outsyms
1844 + ((finfo->last_file_index
1845 - syment_base)
1846 * osymesz)));
1847 }
1848 else
1849 {
dc810e39
AM
1850 file_ptr pos;
1851
252b5132
RH
1852 /* We have already written out the last C_FILE
1853 symbol. We need to write it out again. We
1854 borrow *outsym temporarily. */
1855 bfd_coff_swap_sym_out (output_bfd,
1856 (PTR) &finfo->last_file,
1857 (PTR) outsym);
dc810e39
AM
1858 pos = obj_sym_filepos (output_bfd);
1859 pos += finfo->last_file_index * osymesz;
1860 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
1861 || bfd_bwrite (outsym, osymesz, output_bfd) != osymesz)
252b5132
RH
1862 return false;
1863 }
1864 }
1865
1866 finfo->last_file_index = output_index;
1867 finfo->last_file = isym;
8add8e30 1868 break;
252b5132
RH
1869 }
1870
1871 /* If doing task linking, convert normal global function symbols to
e4202681
NC
1872 static functions. */
1873 if (finfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
252b5132
RH
1874 isym.n_sclass = C_STAT;
1875
1876 /* Output the symbol. */
1877
1878 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
1879
1880 *indexp = output_index;
1881
1882 if (global)
1883 {
1884 long indx;
1885 struct coff_link_hash_entry *h;
1886
1887 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1888 / isymesz);
1889 h = obj_coff_sym_hashes (input_bfd)[indx];
1890 if (h == NULL)
1891 {
1892 /* This can happen if there were errors earlier in
1893 the link. */
1894 bfd_set_error (bfd_error_bad_value);
1895 return false;
1896 }
1897 h->indx = output_index;
1898 }
1899
1900 output_index += add;
1901 outsym += add * osymesz;
1902 }
1903
1904 esym += add * isymesz;
1905 isymp += add;
1906 ++secpp;
1907 ++indexp;
1908 for (--add; add > 0; --add)
1909 {
1910 *secpp++ = NULL;
1911 *indexp++ = -1;
1912 }
1913 }
1914
1915 /* Fix up the aux entries. This must be done in a separate pass,
1916 because we don't know the correct symbol indices until we have
1917 already decided which symbols we are going to keep. */
1918
1919 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1920 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1921 isymp = finfo->internal_syms;
1922 indexp = finfo->sym_indices;
1923 sym_hash = obj_coff_sym_hashes (input_bfd);
1924 outsym = finfo->outsyms;
1925 while (esym < esym_end)
1926 {
1927 int add;
1928
1929 add = 1 + isymp->n_numaux;
1930
1931 if ((*indexp < 0
1932 || (bfd_size_type) *indexp < syment_base)
1933 && (*sym_hash == NULL
1934 || (*sym_hash)->auxbfd != input_bfd))
1935 esym += add * isymesz;
1936 else
1937 {
1938 struct coff_link_hash_entry *h;
1939 int i;
1940
1941 h = NULL;
1942 if (*indexp < 0)
1943 {
1944 h = *sym_hash;
1945
1946 /* The m68k-motorola-sysv assembler will sometimes
1947 generate two symbols with the same name, but only one
1948 will have aux entries. */
1949 BFD_ASSERT (isymp->n_numaux == 0
1950 || h->numaux == isymp->n_numaux);
1951 }
1952
1953 esym += isymesz;
1954
1955 if (h == NULL)
1956 outsym += osymesz;
1957
1958 /* Handle the aux entries. This handling is based on
1959 coff_pointerize_aux. I don't know if it always correct. */
1960 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1961 {
1962 union internal_auxent aux;
1963 union internal_auxent *auxp;
1964
1965 if (h != NULL)
1966 auxp = h->aux + i;
1967 else
1968 {
1969 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
1970 isymp->n_sclass, i, isymp->n_numaux,
1971 (PTR) &aux);
1972 auxp = &aux;
1973 }
1974
1975 if (isymp->n_sclass == C_FILE)
1976 {
1977 /* If this is a long filename, we must put it in the
1978 string table. */
1979 if (auxp->x_file.x_n.x_zeroes == 0
1980 && auxp->x_file.x_n.x_offset != 0)
1981 {
1982 const char *filename;
1983 bfd_size_type indx;
1984
1985 BFD_ASSERT (auxp->x_file.x_n.x_offset
1986 >= STRING_SIZE_SIZE);
1987 if (strings == NULL)
1988 {
1989 strings = _bfd_coff_read_string_table (input_bfd);
1990 if (strings == NULL)
1991 return false;
1992 }
1993 filename = strings + auxp->x_file.x_n.x_offset;
1994 indx = _bfd_stringtab_add (finfo->strtab, filename,
1995 hash, copy);
1996 if (indx == (bfd_size_type) -1)
1997 return false;
1998 auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
1999 }
2000 }
2001 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
2002 {
2003 unsigned long indx;
2004
2005 if (ISFCN (isymp->n_type)
2006 || ISTAG (isymp->n_sclass)
2007 || isymp->n_sclass == C_BLOCK
2008 || isymp->n_sclass == C_FCN)
2009 {
2010 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
2011 if (indx > 0
2012 && indx < obj_raw_syment_count (input_bfd))
2013 {
2014 /* We look forward through the symbol for
2015 the index of the next symbol we are going
2016 to include. I don't know if this is
2017 entirely right. */
2018 while ((finfo->sym_indices[indx] < 0
2019 || ((bfd_size_type) finfo->sym_indices[indx]
2020 < syment_base))
2021 && indx < obj_raw_syment_count (input_bfd))
2022 ++indx;
2023 if (indx >= obj_raw_syment_count (input_bfd))
2024 indx = output_index;
2025 else
2026 indx = finfo->sym_indices[indx];
2027 auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
2028 }
2029 }
2030
2031 indx = auxp->x_sym.x_tagndx.l;
2032 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
2033 {
2034 long symindx;
2035
2036 symindx = finfo->sym_indices[indx];
2037 if (symindx < 0)
2038 auxp->x_sym.x_tagndx.l = 0;
2039 else
2040 auxp->x_sym.x_tagndx.l = symindx;
2041 }
2042
2043 /* The .bf symbols are supposed to be linked through
2044 the endndx field. We need to carry this list
2045 across object files. */
2046 if (i == 0
2047 && h == NULL
2048 && isymp->n_sclass == C_FCN
2049 && (isymp->_n._n_n._n_zeroes != 0
2050 || isymp->_n._n_n._n_offset == 0)
2051 && isymp->_n._n_name[0] == '.'
2052 && isymp->_n._n_name[1] == 'b'
2053 && isymp->_n._n_name[2] == 'f'
2054 && isymp->_n._n_name[3] == '\0')
2055 {
2056 if (finfo->last_bf_index != -1)
2057 {
2058 finfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
2059 *indexp;
2060
2061 if ((bfd_size_type) finfo->last_bf_index
2062 >= syment_base)
2063 {
2064 PTR auxout;
2065
2066 /* The last .bf symbol is in this input
2067 file. This will only happen if the
2068 assembler did not set up the .bf
2069 endndx symbols correctly. */
2070 auxout = (PTR) (finfo->outsyms
2071 + ((finfo->last_bf_index
2072 - syment_base)
2073 * osymesz));
2074 bfd_coff_swap_aux_out (output_bfd,
2075 (PTR) &finfo->last_bf,
2076 isymp->n_type,
2077 isymp->n_sclass,
2078 0, isymp->n_numaux,
2079 auxout);
2080 }
2081 else
2082 {
dc810e39
AM
2083 file_ptr pos;
2084
252b5132
RH
2085 /* We have already written out the last
2086 .bf aux entry. We need to write it
2087 out again. We borrow *outsym
2088 temporarily. FIXME: This case should
2089 be made faster. */
2090 bfd_coff_swap_aux_out (output_bfd,
2091 (PTR) &finfo->last_bf,
2092 isymp->n_type,
2093 isymp->n_sclass,
2094 0, isymp->n_numaux,
2095 (PTR) outsym);
dc810e39
AM
2096 pos = obj_sym_filepos (output_bfd);
2097 pos += finfo->last_bf_index * osymesz;
2098 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2099 || (bfd_bwrite (outsym, osymesz, output_bfd)
2100 != osymesz))
252b5132
RH
2101 return false;
2102 }
2103 }
2104
2105 if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
2106 finfo->last_bf_index = -1;
2107 else
2108 {
2109 /* The endndx field of this aux entry must
2110 be updated with the symbol number of the
2111 next .bf symbol. */
2112 finfo->last_bf = *auxp;
2113 finfo->last_bf_index = (((outsym - finfo->outsyms)
2114 / osymesz)
2115 + syment_base);
2116 }
2117 }
2118 }
2119
2120 if (h == NULL)
2121 {
2122 bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isymp->n_type,
2123 isymp->n_sclass, i, isymp->n_numaux,
2124 (PTR) outsym);
2125 outsym += osymesz;
2126 }
2127
2128 esym += isymesz;
2129 }
2130 }
2131
2132 indexp += add;
2133 isymp += add;
2134 sym_hash += add;
2135 }
2136
2137 /* Relocate the line numbers, unless we are stripping them. */
2138 if (finfo->info->strip == strip_none
2139 || finfo->info->strip == strip_some)
2140 {
2141 for (o = input_bfd->sections; o != NULL; o = o->next)
2142 {
2143 bfd_vma offset;
2144 bfd_byte *eline;
2145 bfd_byte *elineend;
b545f675
ILT
2146 bfd_byte *oeline;
2147 boolean skipping;
dc810e39
AM
2148 file_ptr pos;
2149 bfd_size_type amt;
252b5132
RH
2150
2151 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2152 build_link_order in ldwrite.c will not have created a
2153 link order, which means that we will not have seen this
2154 input section in _bfd_coff_final_link, which means that
2155 we will not have allocated space for the line numbers of
2156 this section. I don't think line numbers can be
2157 meaningful for a section which does not have
2158 SEC_HAS_CONTENTS set, but, if they do, this must be
2159 changed. */
2160 if (o->lineno_count == 0
2161 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2162 continue;
2163
2164 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
dc810e39 2165 || bfd_bread (finfo->linenos, linesz * o->lineno_count,
252b5132
RH
2166 input_bfd) != linesz * o->lineno_count)
2167 return false;
2168
2169 offset = o->output_section->vma + o->output_offset - o->vma;
2170 eline = finfo->linenos;
b545f675 2171 oeline = finfo->linenos;
252b5132 2172 elineend = eline + linesz * o->lineno_count;
b545f675 2173 skipping = false;
252b5132
RH
2174 for (; eline < elineend; eline += linesz)
2175 {
2176 struct internal_lineno iline;
2177
2178 bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline);
2179
2180 if (iline.l_lnno != 0)
2181 iline.l_addr.l_paddr += offset;
2182 else if (iline.l_addr.l_symndx >= 0
2183 && ((unsigned long) iline.l_addr.l_symndx
2184 < obj_raw_syment_count (input_bfd)))
2185 {
2186 long indx;
2187
2188 indx = finfo->sym_indices[iline.l_addr.l_symndx];
2189
2190 if (indx < 0)
2191 {
2192 /* These line numbers are attached to a symbol
b545f675
ILT
2193 which we are stripping. We must discard the
2194 line numbers because reading them back with
2195 no associated symbol (or associating them all
2196 with symbol #0) will fail. We can't regain
2197 the space in the output file, but at least
2198 they're dense. */
2199 skipping = true;
252b5132
RH
2200 }
2201 else
2202 {
2203 struct internal_syment is;
2204 union internal_auxent ia;
2205
2206 /* Fix up the lnnoptr field in the aux entry of
2207 the symbol. It turns out that we can't do
2208 this when we modify the symbol aux entries,
2209 because gas sometimes screws up the lnnoptr
2210 field and makes it an offset from the start
2211 of the line numbers rather than an absolute
2212 file index. */
2213 bfd_coff_swap_sym_in (output_bfd,
2214 (PTR) (finfo->outsyms
2215 + ((indx - syment_base)
2216 * osymesz)),
2217 (PTR) &is);
2218 if ((ISFCN (is.n_type)
2219 || is.n_sclass == C_BLOCK)
2220 && is.n_numaux >= 1)
2221 {
2222 PTR auxptr;
2223
2224 auxptr = (PTR) (finfo->outsyms
2225 + ((indx - syment_base + 1)
2226 * osymesz));
2227 bfd_coff_swap_aux_in (output_bfd, auxptr,
2228 is.n_type, is.n_sclass,
2229 0, is.n_numaux, (PTR) &ia);
2230 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2231 (o->output_section->line_filepos
2232 + o->output_section->lineno_count * linesz
2233 + eline - finfo->linenos);
2234 bfd_coff_swap_aux_out (output_bfd, (PTR) &ia,
2235 is.n_type, is.n_sclass, 0,
2236 is.n_numaux, auxptr);
2237 }
b545f675 2238
60bcf0fa 2239 skipping = false;
252b5132
RH
2240 }
2241
2242 iline.l_addr.l_symndx = indx;
2243 }
2244
b545f675
ILT
2245 if (!skipping)
2246 {
2247 bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline,
2248 (PTR) oeline);
2249 oeline += linesz;
2250 }
252b5132
RH
2251 }
2252
dc810e39
AM
2253 pos = o->output_section->line_filepos;
2254 pos += o->output_section->lineno_count * linesz;
2255 amt = oeline - finfo->linenos;
2256 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2257 || bfd_bwrite (finfo->linenos, amt, output_bfd) != amt)
252b5132
RH
2258 return false;
2259
dc810e39 2260 o->output_section->lineno_count += amt / linesz;
252b5132
RH
2261 }
2262 }
2263
2264 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2265 symbol will be the first symbol in the next input file. In the
2266 normal case, this will save us from writing out the C_FILE symbol
2267 again. */
2268 if (finfo->last_file_index != -1
2269 && (bfd_size_type) finfo->last_file_index >= syment_base)
2270 {
2271 finfo->last_file.n_value = output_index;
2272 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
2273 (PTR) (finfo->outsyms
2274 + ((finfo->last_file_index - syment_base)
2275 * osymesz)));
2276 }
2277
2278 /* Write the modified symbols to the output file. */
2279 if (outsym > finfo->outsyms)
2280 {
dc810e39
AM
2281 file_ptr pos;
2282 bfd_size_type amt;
2283
2284 pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
2285 amt = outsym - finfo->outsyms;
2286 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2287 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
252b5132
RH
2288 return false;
2289
2290 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2291 + (outsym - finfo->outsyms) / osymesz)
2292 == output_index);
2293
2294 obj_raw_syment_count (output_bfd) = output_index;
2295 }
2296
2297 /* Relocate the contents of each section. */
2298 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2299 for (o = input_bfd->sections; o != NULL; o = o->next)
2300 {
2301 bfd_byte *contents;
2302 struct coff_section_tdata *secdata;
2303
2304 if (! o->linker_mark)
2305 {
2306 /* This section was omitted from the link. */
2307 continue;
2308 }
2309
2310 if ((o->flags & SEC_HAS_CONTENTS) == 0
2311 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
2312 {
2313 if ((o->flags & SEC_RELOC) != 0
2314 && o->reloc_count != 0)
2315 {
2316 ((*_bfd_error_handler)
2317 (_("%s: relocs in section `%s', but it has no contents"),
8f615d07 2318 bfd_archive_filename (input_bfd),
252b5132
RH
2319 bfd_get_section_name (input_bfd, o)));
2320 bfd_set_error (bfd_error_no_contents);
2321 return false;
2322 }
2323
2324 continue;
2325 }
2326
2327 secdata = coff_section_data (input_bfd, o);
2328 if (secdata != NULL && secdata->contents != NULL)
2329 contents = secdata->contents;
2330 else
2331 {
2332 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
2333 (file_ptr) 0, o->_raw_size))
2334 return false;
2335 contents = finfo->contents;
2336 }
2337
2338 if ((o->flags & SEC_RELOC) != 0)
2339 {
2340 int target_index;
2341 struct internal_reloc *internal_relocs;
2342 struct internal_reloc *irel;
2343
2344 /* Read in the relocs. */
2345 target_index = o->output_section->target_index;
2346 internal_relocs = (_bfd_coff_read_internal_relocs
2347 (input_bfd, o, false, finfo->external_relocs,
2348 finfo->info->relocateable,
2349 (finfo->info->relocateable
2350 ? (finfo->section_info[target_index].relocs
2351 + o->output_section->reloc_count)
2352 : finfo->internal_relocs)));
2353 if (internal_relocs == NULL)
2354 return false;
2355
2356 /* Call processor specific code to relocate the section
2357 contents. */
2358 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
2359 input_bfd, o,
2360 contents,
2361 internal_relocs,
2362 finfo->internal_syms,
2363 finfo->sec_ptrs))
2364 return false;
2365
2366 if (finfo->info->relocateable)
2367 {
2368 bfd_vma offset;
2369 struct internal_reloc *irelend;
2370 struct coff_link_hash_entry **rel_hash;
2371
2372 offset = o->output_section->vma + o->output_offset - o->vma;
2373 irel = internal_relocs;
2374 irelend = irel + o->reloc_count;
2375 rel_hash = (finfo->section_info[target_index].rel_hashes
2376 + o->output_section->reloc_count);
2377 for (; irel < irelend; irel++, rel_hash++)
2378 {
2379 struct coff_link_hash_entry *h;
2380 boolean adjusted;
2381
2382 *rel_hash = NULL;
2383
2384 /* Adjust the reloc address and symbol index. */
2385
2386 irel->r_vaddr += offset;
2387
2388 if (irel->r_symndx == -1)
2389 continue;
2390
2391 if (adjust_symndx)
2392 {
2393 if (! (*adjust_symndx) (output_bfd, finfo->info,
2394 input_bfd, o, irel,
2395 &adjusted))
2396 return false;
2397 if (adjusted)
2398 continue;
2399 }
2400
2401 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2402 if (h != NULL)
2403 {
2404 /* This is a global symbol. */
2405 if (h->indx >= 0)
2406 irel->r_symndx = h->indx;
2407 else
2408 {
2409 /* This symbol is being written at the end
2410 of the file, and we do not yet know the
2411 symbol index. We save the pointer to the
2412 hash table entry in the rel_hash list.
2413 We set the indx field to -2 to indicate
2414 that this symbol must not be stripped. */
2415 *rel_hash = h;
2416 h->indx = -2;
2417 }
2418 }
2419 else
2420 {
2421 long indx;
2422
2423 indx = finfo->sym_indices[irel->r_symndx];
2424 if (indx != -1)
2425 irel->r_symndx = indx;
2426 else
2427 {
2428 struct internal_syment *is;
2429 const char *name;
2430 char buf[SYMNMLEN + 1];
2431
2432 /* This reloc is against a symbol we are
2433 stripping. This should have been handled
2434 by the 'dont_skip_symbol' code in the while
244148ad
KH
2435 loop at the top of this function. */
2436
252b5132
RH
2437 is = finfo->internal_syms + irel->r_symndx;
2438
2439 name = (_bfd_coff_internal_syment_name
2440 (input_bfd, is, buf));
2441 if (name == NULL)
2442 return false;
2443
2444 if (! ((*finfo->info->callbacks->unattached_reloc)
2445 (finfo->info, name, input_bfd, o,
2446 irel->r_vaddr)))
2447 return false;
2448 }
2449 }
2450 }
2451
2452 o->output_section->reloc_count += o->reloc_count;
2453 }
2454 }
2455
2456 /* Write out the modified section contents. */
2457 if (secdata == NULL || secdata->stab_info == NULL)
2458 {
dc810e39
AM
2459 file_ptr loc = o->output_offset * bfd_octets_per_byte (output_bfd);
2460 bfd_size_type amt = (o->_cooked_size != 0
2461 ? o->_cooked_size : o->_raw_size);
252b5132 2462 if (! bfd_set_section_contents (output_bfd, o->output_section,
dc810e39 2463 contents, loc, amt))
252b5132
RH
2464 return false;
2465 }
2466 else
2467 {
2468 if (! (_bfd_write_section_stabs
2469 (output_bfd, &coff_hash_table (finfo->info)->stab_info,
2470 o, &secdata->stab_info, contents)))
2471 return false;
2472 }
2473 }
2474
2475 if (! finfo->info->keep_memory)
2476 {
2477 if (! _bfd_coff_free_symbols (input_bfd))
2478 return false;
2479 }
2480
2481 return true;
2482}
2483
2484/* Write out a global symbol. Called via coff_link_hash_traverse. */
2485
2486boolean
2487_bfd_coff_write_global_sym (h, data)
2488 struct coff_link_hash_entry *h;
2489 PTR data;
2490{
2491 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2492 bfd *output_bfd;
2493 struct internal_syment isym;
2494 bfd_size_type symesz;
2495 unsigned int i;
dc810e39 2496 file_ptr pos;
252b5132
RH
2497
2498 output_bfd = finfo->output_bfd;
2499
2500 if (h->indx >= 0)
2501 return true;
2502
2503 if (h->indx != -2
2504 && (finfo->info->strip == strip_all
2505 || (finfo->info->strip == strip_some
2506 && (bfd_hash_lookup (finfo->info->keep_hash,
2507 h->root.root.string, false, false)
2508 == NULL))))
2509 return true;
2510
2511 switch (h->root.type)
2512 {
2513 default:
2514 case bfd_link_hash_new:
2515 abort ();
2516 return false;
2517
2518 case bfd_link_hash_undefined:
2519 case bfd_link_hash_undefweak:
2520 isym.n_scnum = N_UNDEF;
2521 isym.n_value = 0;
2522 break;
2523
2524 case bfd_link_hash_defined:
2525 case bfd_link_hash_defweak:
2526 {
2527 asection *sec;
2528
2529 sec = h->root.u.def.section->output_section;
2530 if (bfd_is_abs_section (sec))
2531 isym.n_scnum = N_ABS;
2532 else
2533 isym.n_scnum = sec->target_index;
2534 isym.n_value = (h->root.u.def.value
2535 + h->root.u.def.section->output_offset);
2536 if (! obj_pe (finfo->output_bfd))
2537 isym.n_value += sec->vma;
2538 }
2539 break;
2540
2541 case bfd_link_hash_common:
2542 isym.n_scnum = N_UNDEF;
2543 isym.n_value = h->root.u.c.size;
2544 break;
2545
2546 case bfd_link_hash_indirect:
2547 case bfd_link_hash_warning:
2548 /* Just ignore these. They can't be handled anyhow. */
2549 return true;
2550 }
2551
2552 if (strlen (h->root.root.string) <= SYMNMLEN)
2553 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2554 else
2555 {
2556 boolean hash;
2557 bfd_size_type indx;
2558
2559 hash = true;
2560 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
2561 hash = false;
2562 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
2563 false);
2564 if (indx == (bfd_size_type) -1)
2565 {
2566 finfo->failed = true;
2567 return false;
2568 }
2569 isym._n._n_n._n_zeroes = 0;
2570 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2571 }
2572
2573 isym.n_sclass = h->class;
2574 isym.n_type = h->type;
2575
2576 if (isym.n_sclass == C_NULL)
2577 isym.n_sclass = C_EXT;
2578
2579 /* If doing task linking and this is the pass where we convert
2580 defined globals to statics, then do that conversion now. If the
2581 symbol is not being converted, just ignore it and it will be
e4202681 2582 output during a later pass. */
252b5132
RH
2583 if (finfo->global_to_static)
2584 {
e4202681
NC
2585 if (! IS_EXTERNAL (output_bfd, isym))
2586 return true;
2587
252b5132
RH
2588 isym.n_sclass = C_STAT;
2589 }
2590
e4202681
NC
2591 /* When a weak symbol is not overriden by a strong one,
2592 turn it into an external symbol when not building a
2593 shared or relocateable object. */
2594 if (! finfo->info->shared
2595 && ! finfo->info->relocateable
2596 && IS_WEAK_EXTERNAL (finfo->output_bfd, isym))
2597 isym.n_sclass = C_EXT;
2598
252b5132 2599 isym.n_numaux = h->numaux;
244148ad 2600
252b5132
RH
2601 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) finfo->outsyms);
2602
2603 symesz = bfd_coff_symesz (output_bfd);
2604
dc810e39
AM
2605 pos = obj_sym_filepos (output_bfd);
2606 pos += obj_raw_syment_count (output_bfd) * symesz;
2607 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2608 || bfd_bwrite (finfo->outsyms, symesz, output_bfd) != symesz)
252b5132
RH
2609 {
2610 finfo->failed = true;
2611 return false;
2612 }
2613
2614 h->indx = obj_raw_syment_count (output_bfd);
2615
2616 ++obj_raw_syment_count (output_bfd);
2617
2c546cd8
ILT
2618 /* Write out any associated aux entries. Most of the aux entries
2619 will have been modified in _bfd_coff_link_input_bfd. We have to
2620 handle section aux entries here, now that we have the final
2621 relocation and line number counts. */
252b5132
RH
2622 for (i = 0; i < isym.n_numaux; i++)
2623 {
2c546cd8
ILT
2624 union internal_auxent *auxp;
2625
2626 auxp = h->aux + i;
2627
2628 /* Look for a section aux entry here using the same tests that
2629 coff_swap_aux_out uses. */
2630 if (i == 0
2631 && (isym.n_sclass == C_STAT
2632 || isym.n_sclass == C_HIDDEN)
2633 && isym.n_type == T_NULL
2634 && (h->root.type == bfd_link_hash_defined
2635 || h->root.type == bfd_link_hash_defweak))
2636 {
2637 asection *sec;
2638
2639 sec = h->root.u.def.section->output_section;
2640 if (sec != NULL)
2641 {
2642 auxp->x_scn.x_scnlen = (sec->_cooked_size != 0
2643 ? sec->_cooked_size
2644 : sec->_raw_size);
2645
2646 /* For PE, an overflow on the final link reportedly does
2647 not matter. FIXME: Why not? */
2648
2649 if (sec->reloc_count > 0xffff
2650 && (! obj_pe (output_bfd)
2651 || finfo->info->relocateable))
2652 (*_bfd_error_handler)
2653 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2654 bfd_get_filename (output_bfd),
2655 bfd_get_section_name (output_bfd, sec),
2656 sec->reloc_count);
2657
2658 if (sec->lineno_count > 0xffff
2659 && (! obj_pe (output_bfd)
2660 || finfo->info->relocateable))
2661 (*_bfd_error_handler)
2662 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2663 bfd_get_filename (output_bfd),
2664 bfd_get_section_name (output_bfd, sec),
2665 sec->lineno_count);
2666
2667 auxp->x_scn.x_nreloc = sec->reloc_count;
2668 auxp->x_scn.x_nlinno = sec->lineno_count;
2669 auxp->x_scn.x_checksum = 0;
2670 auxp->x_scn.x_associated = 0;
2671 auxp->x_scn.x_comdat = 0;
2672 }
2673 }
2674
2675 bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isym.n_type,
dc810e39 2676 isym.n_sclass, (int) i, isym.n_numaux,
252b5132 2677 (PTR) finfo->outsyms);
dc810e39 2678 if (bfd_bwrite (finfo->outsyms, symesz, output_bfd) != symesz)
252b5132
RH
2679 {
2680 finfo->failed = true;
2681 return false;
2682 }
2683 ++obj_raw_syment_count (output_bfd);
2684 }
2685
2686 return true;
2687}
2688
2689/* Write out task global symbols, converting them to statics. Called
2690 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
244148ad 2691 the dirty work, if the symbol we are processing needs conversion. */
252b5132
RH
2692
2693boolean
2694_bfd_coff_write_task_globals (h, data)
2695 struct coff_link_hash_entry *h;
2696 PTR data;
2697{
2698 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2699 boolean rtnval = true;
2700 boolean save_global_to_static;
2701
2702 if (h->indx < 0)
2703 {
2704 switch (h->root.type)
2705 {
2706 case bfd_link_hash_defined:
2707 case bfd_link_hash_defweak:
2708 save_global_to_static = finfo->global_to_static;
2709 finfo->global_to_static = true;
2710 rtnval = _bfd_coff_write_global_sym (h, data);
2711 finfo->global_to_static = save_global_to_static;
2712 break;
2713 default:
2714 break;
2715 }
2716 }
2717 return (rtnval);
2718}
2719
2720/* Handle a link order which is supposed to generate a reloc. */
2721
2722boolean
2723_bfd_coff_reloc_link_order (output_bfd, finfo, output_section, link_order)
2724 bfd *output_bfd;
2725 struct coff_final_link_info *finfo;
2726 asection *output_section;
2727 struct bfd_link_order *link_order;
2728{
2729 reloc_howto_type *howto;
2730 struct internal_reloc *irel;
2731 struct coff_link_hash_entry **rel_hash_ptr;
2732
2733 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2734 if (howto == NULL)
2735 {
2736 bfd_set_error (bfd_error_bad_value);
2737 return false;
2738 }
2739
2740 if (link_order->u.reloc.p->addend != 0)
2741 {
2742 bfd_size_type size;
2743 bfd_byte *buf;
2744 bfd_reloc_status_type rstat;
2745 boolean ok;
dc810e39 2746 file_ptr loc;
252b5132
RH
2747
2748 size = bfd_get_reloc_size (howto);
2749 buf = (bfd_byte *) bfd_zmalloc (size);
2750 if (buf == NULL)
2751 return false;
2752
2753 rstat = _bfd_relocate_contents (howto, output_bfd,
dc810e39
AM
2754 (bfd_vma) link_order->u.reloc.p->addend,\
2755 buf);
252b5132
RH
2756 switch (rstat)
2757 {
2758 case bfd_reloc_ok:
2759 break;
2760 default:
2761 case bfd_reloc_outofrange:
2762 abort ();
2763 case bfd_reloc_overflow:
2764 if (! ((*finfo->info->callbacks->reloc_overflow)
2765 (finfo->info,
2766 (link_order->type == bfd_section_reloc_link_order
2767 ? bfd_section_name (output_bfd,
2768 link_order->u.reloc.p->u.section)
2769 : link_order->u.reloc.p->u.name),
2770 howto->name, link_order->u.reloc.p->addend,
2771 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2772 {
2773 free (buf);
2774 return false;
2775 }
2776 break;
2777 }
dc810e39 2778 loc = link_order->offset * bfd_octets_per_byte (output_bfd);
252b5132 2779 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
dc810e39 2780 loc, size);
252b5132
RH
2781 free (buf);
2782 if (! ok)
2783 return false;
2784 }
2785
2786 /* Store the reloc information in the right place. It will get
2787 swapped and written out at the end of the final_link routine. */
2788
2789 irel = (finfo->section_info[output_section->target_index].relocs
2790 + output_section->reloc_count);
2791 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
2792 + output_section->reloc_count);
2793
2794 memset (irel, 0, sizeof (struct internal_reloc));
2795 *rel_hash_ptr = NULL;
2796
2797 irel->r_vaddr = output_section->vma + link_order->offset;
2798
2799 if (link_order->type == bfd_section_reloc_link_order)
2800 {
2801 /* We need to somehow locate a symbol in the right section. The
2802 symbol must either have a value of zero, or we must adjust
2803 the addend by the value of the symbol. FIXME: Write this
2804 when we need it. The old linker couldn't handle this anyhow. */
2805 abort ();
2806 *rel_hash_ptr = NULL;
2807 irel->r_symndx = 0;
2808 }
2809 else
2810 {
2811 struct coff_link_hash_entry *h;
2812
2813 h = ((struct coff_link_hash_entry *)
2814 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
2815 link_order->u.reloc.p->u.name,
2816 false, false, true));
2817 if (h != NULL)
2818 {
2819 if (h->indx >= 0)
2820 irel->r_symndx = h->indx;
2821 else
2822 {
2823 /* Set the index to -2 to force this symbol to get
2824 written out. */
2825 h->indx = -2;
2826 *rel_hash_ptr = h;
2827 irel->r_symndx = 0;
2828 }
2829 }
2830 else
2831 {
2832 if (! ((*finfo->info->callbacks->unattached_reloc)
2833 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
2834 (asection *) NULL, (bfd_vma) 0)))
2835 return false;
2836 irel->r_symndx = 0;
2837 }
2838 }
2839
2840 /* FIXME: Is this always right? */
2841 irel->r_type = howto->type;
2842
2843 /* r_size is only used on the RS/6000, which needs its own linker
2844 routines anyhow. r_extern is only used for ECOFF. */
2845
2846 /* FIXME: What is the right value for r_offset? Is zero OK? */
2847
2848 ++output_section->reloc_count;
2849
2850 return true;
2851}
2852
2853/* A basic reloc handling routine which may be used by processors with
2854 simple relocs. */
2855
2856boolean
2857_bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
2858 input_section, contents, relocs, syms,
2859 sections)
2860 bfd *output_bfd;
2861 struct bfd_link_info *info;
2862 bfd *input_bfd;
2863 asection *input_section;
2864 bfd_byte *contents;
2865 struct internal_reloc *relocs;
2866 struct internal_syment *syms;
2867 asection **sections;
2868{
2869 struct internal_reloc *rel;
2870 struct internal_reloc *relend;
2871
2872 rel = relocs;
2873 relend = rel + input_section->reloc_count;
2874 for (; rel < relend; rel++)
2875 {
2876 long symndx;
2877 struct coff_link_hash_entry *h;
2878 struct internal_syment *sym;
2879 bfd_vma addend;
2880 bfd_vma val;
2881 reloc_howto_type *howto;
2882 bfd_reloc_status_type rstat;
2883
2884 symndx = rel->r_symndx;
2885
2886 if (symndx == -1)
2887 {
2888 h = NULL;
2889 sym = NULL;
2890 }
75987f83
ILT
2891 else if (symndx < 0
2892 || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2893 {
2894 (*_bfd_error_handler)
2895 ("%s: illegal symbol index %ld in relocs",
8f615d07 2896 bfd_archive_filename (input_bfd), symndx);
75987f83
ILT
2897 return false;
2898 }
252b5132 2899 else
244148ad 2900 {
252b5132
RH
2901 h = obj_coff_sym_hashes (input_bfd)[symndx];
2902 sym = syms + symndx;
2903 }
2904
2905 /* COFF treats common symbols in one of two ways. Either the
2906 size of the symbol is included in the section contents, or it
2907 is not. We assume that the size is not included, and force
2908 the rtype_to_howto function to adjust the addend as needed. */
2909
2910 if (sym != NULL && sym->n_scnum != 0)
2911 addend = - sym->n_value;
2912 else
2913 addend = 0;
2914
252b5132
RH
2915 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2916 sym, &addend);
2917 if (howto == NULL)
2918 return false;
2919
2920 /* If we are doing a relocateable link, then we can just ignore
2921 a PC relative reloc that is pcrel_offset. It will already
2922 have the correct value. If this is not a relocateable link,
2923 then we should ignore the symbol value. */
2924 if (howto->pc_relative && howto->pcrel_offset)
2925 {
2926 if (info->relocateable)
2927 continue;
2928 if (sym != NULL && sym->n_scnum != 0)
2929 addend += sym->n_value;
2930 }
2931
2932 val = 0;
2933
2934 if (h == NULL)
2935 {
2936 asection *sec;
2937
2938 if (symndx == -1)
2939 {
2940 sec = bfd_abs_section_ptr;
2941 val = 0;
2942 }
2943 else
2944 {
2945 sec = sections[symndx];
2946 val = (sec->output_section->vma
2947 + sec->output_offset
2948 + sym->n_value);
2949 if (! obj_pe (input_bfd))
2950 val -= sec->vma;
2951 }
2952 }
2953 else
2954 {
2955 if (h->root.type == bfd_link_hash_defined
2956 || h->root.type == bfd_link_hash_defweak)
2957 {
2958 asection *sec;
2959
2960 sec = h->root.u.def.section;
2961 val = (h->root.u.def.value
2962 + sec->output_section->vma
2963 + sec->output_offset);
2964 }
2965
bc7a577d
PB
2966 else if (h->root.type == bfd_link_hash_undefweak)
2967 val = 0;
2968
252b5132
RH
2969 else if (! info->relocateable)
2970 {
2971 if (! ((*info->callbacks->undefined_symbol)
2972 (info, h->root.root.string, input_bfd, input_section,
5cc7c785 2973 rel->r_vaddr - input_section->vma, true)))
252b5132
RH
2974 return false;
2975 }
2976 }
2977
2978 if (info->base_file)
2979 {
244148ad 2980 /* Emit a reloc if the backend thinks it needs it. */
252b5132
RH
2981 if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
2982 {
2983 /* Relocation to a symbol in a section which isn't
2984 absolute. We output the address here to a file.
2985 This file is then read by dlltool when generating the
2986 reloc section. Note that the base file is not
2987 portable between systems. We write out a long here,
2988 and dlltool reads in a long. */
244148ad
KH
2989 long addr = (rel->r_vaddr
2990 - input_section->vma
2991 + input_section->output_offset
252b5132
RH
2992 + input_section->output_section->vma);
2993 if (coff_data (output_bfd)->pe)
2994 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
2995 if (fwrite (&addr, 1, sizeof (long), (FILE *) info->base_file)
2996 != sizeof (long))
2997 {
2998 bfd_set_error (bfd_error_system_call);
2999 return false;
3000 }
3001 }
3002 }
244148ad 3003
252b5132
RH
3004 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
3005 contents,
3006 rel->r_vaddr - input_section->vma,
3007 val, addend);
3008
3009 switch (rstat)
3010 {
3011 default:
3012 abort ();
3013 case bfd_reloc_ok:
3014 break;
3015 case bfd_reloc_outofrange:
3016 (*_bfd_error_handler)
3017 (_("%s: bad reloc address 0x%lx in section `%s'"),
8f615d07 3018 bfd_archive_filename (input_bfd),
252b5132
RH
3019 (unsigned long) rel->r_vaddr,
3020 bfd_get_section_name (input_bfd, input_section));
3021 return false;
3022 case bfd_reloc_overflow:
3023 {
3024 const char *name;
3025 char buf[SYMNMLEN + 1];
3026
3027 if (symndx == -1)
3028 name = "*ABS*";
3029 else if (h != NULL)
3030 name = h->root.root.string;
3031 else
3032 {
3033 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3034 if (name == NULL)
3035 return false;
3036 }
3037
3038 if (! ((*info->callbacks->reloc_overflow)
3039 (info, name, howto->name, (bfd_vma) 0, input_bfd,
3040 input_section, rel->r_vaddr - input_section->vma)))
3041 return false;
3042 }
3043 }
3044 }
3045 return true;
3046}
This page took 0.288049 seconds and 4 git commands to generate.