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