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