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