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