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