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