only emit -mthumb-interowrk directive if really necessary.
[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];
00692651 1526 char *name_copy;
252b5132
RH
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
00692651
ILT
1541 name_copy = (char *) bfd_alloc (input_bfd,
1542 strlen (elename) + 1);
1543 if (name_copy == NULL)
252b5132 1544 return false;
00692651 1545 strcpy (name_copy, elename);
252b5132 1546
00692651 1547 (*epp)->name = name_copy;
252b5132
RH
1548 (*epp)->type = islp->n_type;
1549 (*epp)->tagndx = 0;
1550 if (islp->n_numaux >= 1
1551 && islp->n_type != T_NULL
1552 && islp->n_sclass != C_EOS)
1553 {
1554 union internal_auxent eleaux;
1555 long indx;
1556
1557 bfd_coff_swap_aux_in (input_bfd, (PTR) (esl + isymesz),
1558 islp->n_type, islp->n_sclass, 0,
1559 islp->n_numaux, (PTR) &eleaux);
1560 indx = eleaux.x_sym.x_tagndx.l;
1561
1562 /* FIXME: If this tagndx entry refers to a symbol
1563 defined later in this file, we just ignore it.
1564 Handling this correctly would be tedious, and may
1565 not be required. */
1566
1567 if (indx > 0
1568 && (indx
1569 < ((esym -
1570 (bfd_byte *) obj_coff_external_syms (input_bfd))
1571 / (long) isymesz)))
1572 {
1573 (*epp)->tagndx = finfo->sym_indices[indx];
1574 if ((*epp)->tagndx < 0)
1575 (*epp)->tagndx = 0;
1576 }
1577 }
1578 epp = &(*epp)->next;
1579 *epp = NULL;
1580
1581 esl += (islp->n_numaux + 1) * isymesz;
1582 islp += islp->n_numaux + 1;
1583 }
1584
1585 /* See if we already have a definition which matches this
1586 type. We always output the type if it has no elements,
1587 for simplicity. */
1588 if (mt->elements == NULL)
1589 bfd_release (input_bfd, (PTR) mt);
1590 else
1591 {
1592 struct coff_debug_merge_type *mtl;
1593
1594 for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1595 {
1596 struct coff_debug_merge_element *me, *mel;
1597
1598 if (mtl->class != mt->class)
1599 continue;
1600
1601 for (me = mt->elements, mel = mtl->elements;
1602 me != NULL && mel != NULL;
1603 me = me->next, mel = mel->next)
1604 {
1605 if (strcmp (me->name, mel->name) != 0
1606 || me->type != mel->type
1607 || me->tagndx != mel->tagndx)
1608 break;
1609 }
1610
1611 if (me == NULL && mel == NULL)
1612 break;
1613 }
1614
1615 if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1616 {
1617 /* This is the first definition of this type. */
1618 mt->indx = output_index;
1619 mt->next = mh->types;
1620 mh->types = mt;
1621 }
1622 else
1623 {
1624 /* This is a redefinition which can be merged. */
1625 bfd_release (input_bfd, (PTR) mt);
1626 *indexp = mtl->indx;
1627 add = (eslend - esym) / isymesz;
1628 skip = true;
1629 }
1630 }
1631 }
1632
1633 /* We now know whether we are to skip this symbol or not. */
1634 if (! skip)
1635 {
1636 /* Adjust the symbol in order to output it. */
1637
1638 if (isym._n._n_n._n_zeroes == 0
1639 && isym._n._n_n._n_offset != 0)
1640 {
1641 const char *name;
1642 bfd_size_type indx;
1643
1644 /* This symbol has a long name. Enter it in the string
1645 table we are building. Note that we do not check
1646 bfd_coff_symname_in_debug. That is only true for
1647 XCOFF, and XCOFF requires different linking code
1648 anyhow. */
1649 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
1650 (char *) NULL);
1651 if (name == NULL)
1652 return false;
1653 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
1654 if (indx == (bfd_size_type) -1)
1655 return false;
1656 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1657 }
1658
1659 if (isym.n_scnum > 0)
1660 {
1661 isym.n_scnum = (*secpp)->output_section->target_index;
1662 isym.n_value += (*secpp)->output_offset;
1663 if (! obj_pe (input_bfd))
1664 isym.n_value -= (*secpp)->vma;
1665 if (! obj_pe (finfo->output_bfd))
1666 isym.n_value += (*secpp)->output_section->vma;
1667 }
1668
1669 /* The value of a C_FILE symbol is the symbol index of the
1670 next C_FILE symbol. The value of the last C_FILE symbol
1671 is the symbol index to the first external symbol
1672 (actually, coff_renumber_symbols does not get this
1673 right--it just sets the value of the last C_FILE symbol
1674 to zero--and nobody has ever complained about it). We
1675 try to get this right, below, just before we write the
1676 symbols out, but in the general case we may have to write
1677 the symbol out twice. */
1678 if (isym.n_sclass == C_FILE)
1679 {
1680 if (finfo->last_file_index != -1
1681 && finfo->last_file.n_value != (long) output_index)
1682 {
1683 /* We must correct the value of the last C_FILE entry. */
1684 finfo->last_file.n_value = output_index;
1685 if ((bfd_size_type) finfo->last_file_index >= syment_base)
1686 {
1687 /* The last C_FILE symbol is in this input file. */
1688 bfd_coff_swap_sym_out (output_bfd,
1689 (PTR) &finfo->last_file,
1690 (PTR) (finfo->outsyms
1691 + ((finfo->last_file_index
1692 - syment_base)
1693 * osymesz)));
1694 }
1695 else
1696 {
1697 /* We have already written out the last C_FILE
1698 symbol. We need to write it out again. We
1699 borrow *outsym temporarily. */
1700 bfd_coff_swap_sym_out (output_bfd,
1701 (PTR) &finfo->last_file,
1702 (PTR) outsym);
1703 if (bfd_seek (output_bfd,
1704 (obj_sym_filepos (output_bfd)
1705 + finfo->last_file_index * osymesz),
1706 SEEK_SET) != 0
1707 || (bfd_write (outsym, osymesz, 1, output_bfd)
1708 != osymesz))
1709 return false;
1710 }
1711 }
1712
1713 finfo->last_file_index = output_index;
1714 finfo->last_file = isym;
1715 }
1716
1717 /* If doing task linking, convert normal global function symbols to
1718 static functions. */
1719
1720 if (finfo->info->task_link
1721 && (isym.n_sclass == C_EXT
1722 || isym.n_sclass == C_WEAKEXT
1723 || (obj_pe (input_bfd) && isym.n_sclass == C_NT_WEAK)))
1724 isym.n_sclass = C_STAT;
1725
1726 /* Output the symbol. */
1727
1728 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
1729
1730 *indexp = output_index;
1731
1732 if (global)
1733 {
1734 long indx;
1735 struct coff_link_hash_entry *h;
1736
1737 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1738 / isymesz);
1739 h = obj_coff_sym_hashes (input_bfd)[indx];
1740 if (h == NULL)
1741 {
1742 /* This can happen if there were errors earlier in
1743 the link. */
1744 bfd_set_error (bfd_error_bad_value);
1745 return false;
1746 }
1747 h->indx = output_index;
1748 }
1749
1750 output_index += add;
1751 outsym += add * osymesz;
1752 }
1753
1754 esym += add * isymesz;
1755 isymp += add;
1756 ++secpp;
1757 ++indexp;
1758 for (--add; add > 0; --add)
1759 {
1760 *secpp++ = NULL;
1761 *indexp++ = -1;
1762 }
1763 }
1764
1765 /* Fix up the aux entries. This must be done in a separate pass,
1766 because we don't know the correct symbol indices until we have
1767 already decided which symbols we are going to keep. */
1768
1769 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1770 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1771 isymp = finfo->internal_syms;
1772 indexp = finfo->sym_indices;
1773 sym_hash = obj_coff_sym_hashes (input_bfd);
1774 outsym = finfo->outsyms;
1775 while (esym < esym_end)
1776 {
1777 int add;
1778
1779 add = 1 + isymp->n_numaux;
1780
1781 if ((*indexp < 0
1782 || (bfd_size_type) *indexp < syment_base)
1783 && (*sym_hash == NULL
1784 || (*sym_hash)->auxbfd != input_bfd))
1785 esym += add * isymesz;
1786 else
1787 {
1788 struct coff_link_hash_entry *h;
1789 int i;
1790
1791 h = NULL;
1792 if (*indexp < 0)
1793 {
1794 h = *sym_hash;
1795
1796 /* The m68k-motorola-sysv assembler will sometimes
1797 generate two symbols with the same name, but only one
1798 will have aux entries. */
1799 BFD_ASSERT (isymp->n_numaux == 0
1800 || h->numaux == isymp->n_numaux);
1801 }
1802
1803 esym += isymesz;
1804
1805 if (h == NULL)
1806 outsym += osymesz;
1807
1808 /* Handle the aux entries. This handling is based on
1809 coff_pointerize_aux. I don't know if it always correct. */
1810 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1811 {
1812 union internal_auxent aux;
1813 union internal_auxent *auxp;
1814
1815 if (h != NULL)
1816 auxp = h->aux + i;
1817 else
1818 {
1819 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
1820 isymp->n_sclass, i, isymp->n_numaux,
1821 (PTR) &aux);
1822 auxp = &aux;
1823 }
1824
1825 if (isymp->n_sclass == C_FILE)
1826 {
1827 /* If this is a long filename, we must put it in the
1828 string table. */
1829 if (auxp->x_file.x_n.x_zeroes == 0
1830 && auxp->x_file.x_n.x_offset != 0)
1831 {
1832 const char *filename;
1833 bfd_size_type indx;
1834
1835 BFD_ASSERT (auxp->x_file.x_n.x_offset
1836 >= STRING_SIZE_SIZE);
1837 if (strings == NULL)
1838 {
1839 strings = _bfd_coff_read_string_table (input_bfd);
1840 if (strings == NULL)
1841 return false;
1842 }
1843 filename = strings + auxp->x_file.x_n.x_offset;
1844 indx = _bfd_stringtab_add (finfo->strtab, filename,
1845 hash, copy);
1846 if (indx == (bfd_size_type) -1)
1847 return false;
1848 auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
1849 }
1850 }
1851 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
1852 {
1853 unsigned long indx;
1854
1855 if (ISFCN (isymp->n_type)
1856 || ISTAG (isymp->n_sclass)
1857 || isymp->n_sclass == C_BLOCK
1858 || isymp->n_sclass == C_FCN)
1859 {
1860 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
1861 if (indx > 0
1862 && indx < obj_raw_syment_count (input_bfd))
1863 {
1864 /* We look forward through the symbol for
1865 the index of the next symbol we are going
1866 to include. I don't know if this is
1867 entirely right. */
1868 while ((finfo->sym_indices[indx] < 0
1869 || ((bfd_size_type) finfo->sym_indices[indx]
1870 < syment_base))
1871 && indx < obj_raw_syment_count (input_bfd))
1872 ++indx;
1873 if (indx >= obj_raw_syment_count (input_bfd))
1874 indx = output_index;
1875 else
1876 indx = finfo->sym_indices[indx];
1877 auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
1878 }
1879 }
1880
1881 indx = auxp->x_sym.x_tagndx.l;
1882 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
1883 {
1884 long symindx;
1885
1886 symindx = finfo->sym_indices[indx];
1887 if (symindx < 0)
1888 auxp->x_sym.x_tagndx.l = 0;
1889 else
1890 auxp->x_sym.x_tagndx.l = symindx;
1891 }
1892
1893 /* The .bf symbols are supposed to be linked through
1894 the endndx field. We need to carry this list
1895 across object files. */
1896 if (i == 0
1897 && h == NULL
1898 && isymp->n_sclass == C_FCN
1899 && (isymp->_n._n_n._n_zeroes != 0
1900 || isymp->_n._n_n._n_offset == 0)
1901 && isymp->_n._n_name[0] == '.'
1902 && isymp->_n._n_name[1] == 'b'
1903 && isymp->_n._n_name[2] == 'f'
1904 && isymp->_n._n_name[3] == '\0')
1905 {
1906 if (finfo->last_bf_index != -1)
1907 {
1908 finfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
1909 *indexp;
1910
1911 if ((bfd_size_type) finfo->last_bf_index
1912 >= syment_base)
1913 {
1914 PTR auxout;
1915
1916 /* The last .bf symbol is in this input
1917 file. This will only happen if the
1918 assembler did not set up the .bf
1919 endndx symbols correctly. */
1920 auxout = (PTR) (finfo->outsyms
1921 + ((finfo->last_bf_index
1922 - syment_base)
1923 * osymesz));
1924 bfd_coff_swap_aux_out (output_bfd,
1925 (PTR) &finfo->last_bf,
1926 isymp->n_type,
1927 isymp->n_sclass,
1928 0, isymp->n_numaux,
1929 auxout);
1930 }
1931 else
1932 {
1933 /* We have already written out the last
1934 .bf aux entry. We need to write it
1935 out again. We borrow *outsym
1936 temporarily. FIXME: This case should
1937 be made faster. */
1938 bfd_coff_swap_aux_out (output_bfd,
1939 (PTR) &finfo->last_bf,
1940 isymp->n_type,
1941 isymp->n_sclass,
1942 0, isymp->n_numaux,
1943 (PTR) outsym);
1944 if (bfd_seek (output_bfd,
1945 (obj_sym_filepos (output_bfd)
1946 + finfo->last_bf_index * osymesz),
1947 SEEK_SET) != 0
1948 || bfd_write (outsym, osymesz, 1,
1949 output_bfd) != osymesz)
1950 return false;
1951 }
1952 }
1953
1954 if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
1955 finfo->last_bf_index = -1;
1956 else
1957 {
1958 /* The endndx field of this aux entry must
1959 be updated with the symbol number of the
1960 next .bf symbol. */
1961 finfo->last_bf = *auxp;
1962 finfo->last_bf_index = (((outsym - finfo->outsyms)
1963 / osymesz)
1964 + syment_base);
1965 }
1966 }
1967 }
1968
1969 if (h == NULL)
1970 {
1971 bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isymp->n_type,
1972 isymp->n_sclass, i, isymp->n_numaux,
1973 (PTR) outsym);
1974 outsym += osymesz;
1975 }
1976
1977 esym += isymesz;
1978 }
1979 }
1980
1981 indexp += add;
1982 isymp += add;
1983 sym_hash += add;
1984 }
1985
1986 /* Relocate the line numbers, unless we are stripping them. */
1987 if (finfo->info->strip == strip_none
1988 || finfo->info->strip == strip_some)
1989 {
1990 for (o = input_bfd->sections; o != NULL; o = o->next)
1991 {
1992 bfd_vma offset;
1993 bfd_byte *eline;
1994 bfd_byte *elineend;
1995
1996 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1997 build_link_order in ldwrite.c will not have created a
1998 link order, which means that we will not have seen this
1999 input section in _bfd_coff_final_link, which means that
2000 we will not have allocated space for the line numbers of
2001 this section. I don't think line numbers can be
2002 meaningful for a section which does not have
2003 SEC_HAS_CONTENTS set, but, if they do, this must be
2004 changed. */
2005 if (o->lineno_count == 0
2006 || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2007 continue;
2008
2009 if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
2010 || bfd_read (finfo->linenos, linesz, o->lineno_count,
2011 input_bfd) != linesz * o->lineno_count)
2012 return false;
2013
2014 offset = o->output_section->vma + o->output_offset - o->vma;
2015 eline = finfo->linenos;
2016 elineend = eline + linesz * o->lineno_count;
2017 for (; eline < elineend; eline += linesz)
2018 {
2019 struct internal_lineno iline;
2020
2021 bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline);
2022
2023 if (iline.l_lnno != 0)
2024 iline.l_addr.l_paddr += offset;
2025 else if (iline.l_addr.l_symndx >= 0
2026 && ((unsigned long) iline.l_addr.l_symndx
2027 < obj_raw_syment_count (input_bfd)))
2028 {
2029 long indx;
2030
2031 indx = finfo->sym_indices[iline.l_addr.l_symndx];
2032
2033 if (indx < 0)
2034 {
2035 /* These line numbers are attached to a symbol
2036 which we are stripping. We should really
2037 just discard the line numbers, but that would
2038 be a pain because we have already counted
2039 them. */
2040 indx = 0;
2041 }
2042 else
2043 {
2044 struct internal_syment is;
2045 union internal_auxent ia;
2046
2047 /* Fix up the lnnoptr field in the aux entry of
2048 the symbol. It turns out that we can't do
2049 this when we modify the symbol aux entries,
2050 because gas sometimes screws up the lnnoptr
2051 field and makes it an offset from the start
2052 of the line numbers rather than an absolute
2053 file index. */
2054 bfd_coff_swap_sym_in (output_bfd,
2055 (PTR) (finfo->outsyms
2056 + ((indx - syment_base)
2057 * osymesz)),
2058 (PTR) &is);
2059 if ((ISFCN (is.n_type)
2060 || is.n_sclass == C_BLOCK)
2061 && is.n_numaux >= 1)
2062 {
2063 PTR auxptr;
2064
2065 auxptr = (PTR) (finfo->outsyms
2066 + ((indx - syment_base + 1)
2067 * osymesz));
2068 bfd_coff_swap_aux_in (output_bfd, auxptr,
2069 is.n_type, is.n_sclass,
2070 0, is.n_numaux, (PTR) &ia);
2071 ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2072 (o->output_section->line_filepos
2073 + o->output_section->lineno_count * linesz
2074 + eline - finfo->linenos);
2075 bfd_coff_swap_aux_out (output_bfd, (PTR) &ia,
2076 is.n_type, is.n_sclass, 0,
2077 is.n_numaux, auxptr);
2078 }
2079 }
2080
2081 iline.l_addr.l_symndx = indx;
2082 }
2083
2084 bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline, (PTR) eline);
2085 }
2086
2087 if (bfd_seek (output_bfd,
2088 (o->output_section->line_filepos
2089 + o->output_section->lineno_count * linesz),
2090 SEEK_SET) != 0
2091 || bfd_write (finfo->linenos, linesz, o->lineno_count,
2092 output_bfd) != linesz * o->lineno_count)
2093 return false;
2094
2095 o->output_section->lineno_count += o->lineno_count;
2096 }
2097 }
2098
2099 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2100 symbol will be the first symbol in the next input file. In the
2101 normal case, this will save us from writing out the C_FILE symbol
2102 again. */
2103 if (finfo->last_file_index != -1
2104 && (bfd_size_type) finfo->last_file_index >= syment_base)
2105 {
2106 finfo->last_file.n_value = output_index;
2107 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
2108 (PTR) (finfo->outsyms
2109 + ((finfo->last_file_index - syment_base)
2110 * osymesz)));
2111 }
2112
2113 /* Write the modified symbols to the output file. */
2114 if (outsym > finfo->outsyms)
2115 {
2116 if (bfd_seek (output_bfd,
2117 obj_sym_filepos (output_bfd) + syment_base * osymesz,
2118 SEEK_SET) != 0
2119 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
2120 output_bfd)
2121 != (bfd_size_type) (outsym - finfo->outsyms)))
2122 return false;
2123
2124 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2125 + (outsym - finfo->outsyms) / osymesz)
2126 == output_index);
2127
2128 obj_raw_syment_count (output_bfd) = output_index;
2129 }
2130
2131 /* Relocate the contents of each section. */
2132 adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2133 for (o = input_bfd->sections; o != NULL; o = o->next)
2134 {
2135 bfd_byte *contents;
2136 struct coff_section_tdata *secdata;
2137
2138 if (! o->linker_mark)
2139 {
2140 /* This section was omitted from the link. */
2141 continue;
2142 }
2143
2144 if ((o->flags & SEC_HAS_CONTENTS) == 0
2145 || (o->_raw_size == 0 && (o->flags & SEC_RELOC) == 0))
2146 {
2147 if ((o->flags & SEC_RELOC) != 0
2148 && o->reloc_count != 0)
2149 {
2150 ((*_bfd_error_handler)
2151 (_("%s: relocs in section `%s', but it has no contents"),
2152 bfd_get_filename (input_bfd),
2153 bfd_get_section_name (input_bfd, o)));
2154 bfd_set_error (bfd_error_no_contents);
2155 return false;
2156 }
2157
2158 continue;
2159 }
2160
2161 secdata = coff_section_data (input_bfd, o);
2162 if (secdata != NULL && secdata->contents != NULL)
2163 contents = secdata->contents;
2164 else
2165 {
2166 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
2167 (file_ptr) 0, o->_raw_size))
2168 return false;
2169 contents = finfo->contents;
2170 }
2171
2172 if ((o->flags & SEC_RELOC) != 0)
2173 {
2174 int target_index;
2175 struct internal_reloc *internal_relocs;
2176 struct internal_reloc *irel;
2177
2178 /* Read in the relocs. */
2179 target_index = o->output_section->target_index;
2180 internal_relocs = (_bfd_coff_read_internal_relocs
2181 (input_bfd, o, false, finfo->external_relocs,
2182 finfo->info->relocateable,
2183 (finfo->info->relocateable
2184 ? (finfo->section_info[target_index].relocs
2185 + o->output_section->reloc_count)
2186 : finfo->internal_relocs)));
2187 if (internal_relocs == NULL)
2188 return false;
2189
2190 /* Call processor specific code to relocate the section
2191 contents. */
2192 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
2193 input_bfd, o,
2194 contents,
2195 internal_relocs,
2196 finfo->internal_syms,
2197 finfo->sec_ptrs))
2198 return false;
2199
2200 if (finfo->info->relocateable)
2201 {
2202 bfd_vma offset;
2203 struct internal_reloc *irelend;
2204 struct coff_link_hash_entry **rel_hash;
2205
2206 offset = o->output_section->vma + o->output_offset - o->vma;
2207 irel = internal_relocs;
2208 irelend = irel + o->reloc_count;
2209 rel_hash = (finfo->section_info[target_index].rel_hashes
2210 + o->output_section->reloc_count);
2211 for (; irel < irelend; irel++, rel_hash++)
2212 {
2213 struct coff_link_hash_entry *h;
2214 boolean adjusted;
2215
2216 *rel_hash = NULL;
2217
2218 /* Adjust the reloc address and symbol index. */
2219
2220 irel->r_vaddr += offset;
2221
2222 if (irel->r_symndx == -1)
2223 continue;
2224
2225 if (adjust_symndx)
2226 {
2227 if (! (*adjust_symndx) (output_bfd, finfo->info,
2228 input_bfd, o, irel,
2229 &adjusted))
2230 return false;
2231 if (adjusted)
2232 continue;
2233 }
2234
2235 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2236 if (h != NULL)
2237 {
2238 /* This is a global symbol. */
2239 if (h->indx >= 0)
2240 irel->r_symndx = h->indx;
2241 else
2242 {
2243 /* This symbol is being written at the end
2244 of the file, and we do not yet know the
2245 symbol index. We save the pointer to the
2246 hash table entry in the rel_hash list.
2247 We set the indx field to -2 to indicate
2248 that this symbol must not be stripped. */
2249 *rel_hash = h;
2250 h->indx = -2;
2251 }
2252 }
2253 else
2254 {
2255 long indx;
2256
2257 indx = finfo->sym_indices[irel->r_symndx];
2258 if (indx != -1)
2259 irel->r_symndx = indx;
2260 else
2261 {
2262 struct internal_syment *is;
2263 const char *name;
2264 char buf[SYMNMLEN + 1];
2265
2266 /* This reloc is against a symbol we are
2267 stripping. This should have been handled
2268 by the 'dont_skip_symbol' code in the while
2269 loop at the top of this function. */
2270
2271 is = finfo->internal_syms + irel->r_symndx;
2272
2273 name = (_bfd_coff_internal_syment_name
2274 (input_bfd, is, buf));
2275 if (name == NULL)
2276 return false;
2277
2278 if (! ((*finfo->info->callbacks->unattached_reloc)
2279 (finfo->info, name, input_bfd, o,
2280 irel->r_vaddr)))
2281 return false;
2282 }
2283 }
2284 }
2285
2286 o->output_section->reloc_count += o->reloc_count;
2287 }
2288 }
2289
2290 /* Write out the modified section contents. */
2291 if (secdata == NULL || secdata->stab_info == NULL)
2292 {
2293 if (! bfd_set_section_contents (output_bfd, o->output_section,
2294 contents, o->output_offset,
2295 (o->_cooked_size != 0
2296 ? o->_cooked_size
2297 : o->_raw_size)))
2298 return false;
2299 }
2300 else
2301 {
2302 if (! (_bfd_write_section_stabs
2303 (output_bfd, &coff_hash_table (finfo->info)->stab_info,
2304 o, &secdata->stab_info, contents)))
2305 return false;
2306 }
2307 }
2308
2309 if (! finfo->info->keep_memory)
2310 {
2311 if (! _bfd_coff_free_symbols (input_bfd))
2312 return false;
2313 }
2314
2315 return true;
2316}
2317
2318/* Write out a global symbol. Called via coff_link_hash_traverse. */
2319
2320boolean
2321_bfd_coff_write_global_sym (h, data)
2322 struct coff_link_hash_entry *h;
2323 PTR data;
2324{
2325 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2326 bfd *output_bfd;
2327 struct internal_syment isym;
2328 bfd_size_type symesz;
2329 unsigned int i;
2330
2331 output_bfd = finfo->output_bfd;
2332
2333 if (h->indx >= 0)
2334 return true;
2335
2336 if (h->indx != -2
2337 && (finfo->info->strip == strip_all
2338 || (finfo->info->strip == strip_some
2339 && (bfd_hash_lookup (finfo->info->keep_hash,
2340 h->root.root.string, false, false)
2341 == NULL))))
2342 return true;
2343
2344 switch (h->root.type)
2345 {
2346 default:
2347 case bfd_link_hash_new:
2348 abort ();
2349 return false;
2350
2351 case bfd_link_hash_undefined:
2352 case bfd_link_hash_undefweak:
2353 isym.n_scnum = N_UNDEF;
2354 isym.n_value = 0;
2355 break;
2356
2357 case bfd_link_hash_defined:
2358 case bfd_link_hash_defweak:
2359 {
2360 asection *sec;
2361
2362 sec = h->root.u.def.section->output_section;
2363 if (bfd_is_abs_section (sec))
2364 isym.n_scnum = N_ABS;
2365 else
2366 isym.n_scnum = sec->target_index;
2367 isym.n_value = (h->root.u.def.value
2368 + h->root.u.def.section->output_offset);
2369 if (! obj_pe (finfo->output_bfd))
2370 isym.n_value += sec->vma;
2371 }
2372 break;
2373
2374 case bfd_link_hash_common:
2375 isym.n_scnum = N_UNDEF;
2376 isym.n_value = h->root.u.c.size;
2377 break;
2378
2379 case bfd_link_hash_indirect:
2380 case bfd_link_hash_warning:
2381 /* Just ignore these. They can't be handled anyhow. */
2382 return true;
2383 }
2384
2385 if (strlen (h->root.root.string) <= SYMNMLEN)
2386 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2387 else
2388 {
2389 boolean hash;
2390 bfd_size_type indx;
2391
2392 hash = true;
2393 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
2394 hash = false;
2395 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
2396 false);
2397 if (indx == (bfd_size_type) -1)
2398 {
2399 finfo->failed = true;
2400 return false;
2401 }
2402 isym._n._n_n._n_zeroes = 0;
2403 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2404 }
2405
2406 isym.n_sclass = h->class;
2407 isym.n_type = h->type;
2408
2409 if (isym.n_sclass == C_NULL)
2410 isym.n_sclass = C_EXT;
2411
2412 /* If doing task linking and this is the pass where we convert
2413 defined globals to statics, then do that conversion now. If the
2414 symbol is not being converted, just ignore it and it will be
2415 output during a later pass. */
2416 if (finfo->global_to_static)
2417 {
2418 if (isym.n_sclass != C_EXT
2419 && isym.n_sclass != C_WEAKEXT
2420 && (! obj_pe (output_bfd) || isym.n_sclass != C_NT_WEAK))
2421 {
2422 return true;
2423 }
2424 isym.n_sclass = C_STAT;
2425 }
2426
2427 isym.n_numaux = h->numaux;
2428
2429 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) finfo->outsyms);
2430
2431 symesz = bfd_coff_symesz (output_bfd);
2432
2433 if (bfd_seek (output_bfd,
2434 (obj_sym_filepos (output_bfd)
2435 + obj_raw_syment_count (output_bfd) * symesz),
2436 SEEK_SET) != 0
2437 || bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
2438 {
2439 finfo->failed = true;
2440 return false;
2441 }
2442
2443 h->indx = obj_raw_syment_count (output_bfd);
2444
2445 ++obj_raw_syment_count (output_bfd);
2446
2447 /* Write out any associated aux entries. There normally will be
2448 none. If there are any, I have no idea how to modify them. */
2449 for (i = 0; i < isym.n_numaux; i++)
2450 {
2451 bfd_coff_swap_aux_out (output_bfd, (PTR) (h->aux + i), isym.n_type,
2452 isym.n_sclass, i, isym.n_numaux,
2453 (PTR) finfo->outsyms);
2454 if (bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
2455 {
2456 finfo->failed = true;
2457 return false;
2458 }
2459 ++obj_raw_syment_count (output_bfd);
2460 }
2461
2462 return true;
2463}
2464
2465/* Write out task global symbols, converting them to statics. Called
2466 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2467 the dirty work, if the symbol we are processing needs conversion. */
2468
2469boolean
2470_bfd_coff_write_task_globals (h, data)
2471 struct coff_link_hash_entry *h;
2472 PTR data;
2473{
2474 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
2475 boolean rtnval = true;
2476 boolean save_global_to_static;
2477
2478 if (h->indx < 0)
2479 {
2480 switch (h->root.type)
2481 {
2482 case bfd_link_hash_defined:
2483 case bfd_link_hash_defweak:
2484 save_global_to_static = finfo->global_to_static;
2485 finfo->global_to_static = true;
2486 rtnval = _bfd_coff_write_global_sym (h, data);
2487 finfo->global_to_static = save_global_to_static;
2488 break;
2489 default:
2490 break;
2491 }
2492 }
2493 return (rtnval);
2494}
2495
2496/* Handle a link order which is supposed to generate a reloc. */
2497
2498boolean
2499_bfd_coff_reloc_link_order (output_bfd, finfo, output_section, link_order)
2500 bfd *output_bfd;
2501 struct coff_final_link_info *finfo;
2502 asection *output_section;
2503 struct bfd_link_order *link_order;
2504{
2505 reloc_howto_type *howto;
2506 struct internal_reloc *irel;
2507 struct coff_link_hash_entry **rel_hash_ptr;
2508
2509 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2510 if (howto == NULL)
2511 {
2512 bfd_set_error (bfd_error_bad_value);
2513 return false;
2514 }
2515
2516 if (link_order->u.reloc.p->addend != 0)
2517 {
2518 bfd_size_type size;
2519 bfd_byte *buf;
2520 bfd_reloc_status_type rstat;
2521 boolean ok;
2522
2523 size = bfd_get_reloc_size (howto);
2524 buf = (bfd_byte *) bfd_zmalloc (size);
2525 if (buf == NULL)
2526 return false;
2527
2528 rstat = _bfd_relocate_contents (howto, output_bfd,
2529 link_order->u.reloc.p->addend, buf);
2530 switch (rstat)
2531 {
2532 case bfd_reloc_ok:
2533 break;
2534 default:
2535 case bfd_reloc_outofrange:
2536 abort ();
2537 case bfd_reloc_overflow:
2538 if (! ((*finfo->info->callbacks->reloc_overflow)
2539 (finfo->info,
2540 (link_order->type == bfd_section_reloc_link_order
2541 ? bfd_section_name (output_bfd,
2542 link_order->u.reloc.p->u.section)
2543 : link_order->u.reloc.p->u.name),
2544 howto->name, link_order->u.reloc.p->addend,
2545 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2546 {
2547 free (buf);
2548 return false;
2549 }
2550 break;
2551 }
2552 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
2553 (file_ptr) link_order->offset, size);
2554 free (buf);
2555 if (! ok)
2556 return false;
2557 }
2558
2559 /* Store the reloc information in the right place. It will get
2560 swapped and written out at the end of the final_link routine. */
2561
2562 irel = (finfo->section_info[output_section->target_index].relocs
2563 + output_section->reloc_count);
2564 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
2565 + output_section->reloc_count);
2566
2567 memset (irel, 0, sizeof (struct internal_reloc));
2568 *rel_hash_ptr = NULL;
2569
2570 irel->r_vaddr = output_section->vma + link_order->offset;
2571
2572 if (link_order->type == bfd_section_reloc_link_order)
2573 {
2574 /* We need to somehow locate a symbol in the right section. The
2575 symbol must either have a value of zero, or we must adjust
2576 the addend by the value of the symbol. FIXME: Write this
2577 when we need it. The old linker couldn't handle this anyhow. */
2578 abort ();
2579 *rel_hash_ptr = NULL;
2580 irel->r_symndx = 0;
2581 }
2582 else
2583 {
2584 struct coff_link_hash_entry *h;
2585
2586 h = ((struct coff_link_hash_entry *)
2587 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
2588 link_order->u.reloc.p->u.name,
2589 false, false, true));
2590 if (h != NULL)
2591 {
2592 if (h->indx >= 0)
2593 irel->r_symndx = h->indx;
2594 else
2595 {
2596 /* Set the index to -2 to force this symbol to get
2597 written out. */
2598 h->indx = -2;
2599 *rel_hash_ptr = h;
2600 irel->r_symndx = 0;
2601 }
2602 }
2603 else
2604 {
2605 if (! ((*finfo->info->callbacks->unattached_reloc)
2606 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
2607 (asection *) NULL, (bfd_vma) 0)))
2608 return false;
2609 irel->r_symndx = 0;
2610 }
2611 }
2612
2613 /* FIXME: Is this always right? */
2614 irel->r_type = howto->type;
2615
2616 /* r_size is only used on the RS/6000, which needs its own linker
2617 routines anyhow. r_extern is only used for ECOFF. */
2618
2619 /* FIXME: What is the right value for r_offset? Is zero OK? */
2620
2621 ++output_section->reloc_count;
2622
2623 return true;
2624}
2625
2626/* A basic reloc handling routine which may be used by processors with
2627 simple relocs. */
2628
2629boolean
2630_bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
2631 input_section, contents, relocs, syms,
2632 sections)
2633 bfd *output_bfd;
2634 struct bfd_link_info *info;
2635 bfd *input_bfd;
2636 asection *input_section;
2637 bfd_byte *contents;
2638 struct internal_reloc *relocs;
2639 struct internal_syment *syms;
2640 asection **sections;
2641{
2642 struct internal_reloc *rel;
2643 struct internal_reloc *relend;
2644
2645 rel = relocs;
2646 relend = rel + input_section->reloc_count;
2647 for (; rel < relend; rel++)
2648 {
2649 long symndx;
2650 struct coff_link_hash_entry *h;
2651 struct internal_syment *sym;
2652 bfd_vma addend;
2653 bfd_vma val;
2654 reloc_howto_type *howto;
2655 bfd_reloc_status_type rstat;
2656
2657 symndx = rel->r_symndx;
2658
2659 if (symndx == -1)
2660 {
2661 h = NULL;
2662 sym = NULL;
2663 }
75987f83
ILT
2664 else if (symndx < 0
2665 || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2666 {
2667 (*_bfd_error_handler)
2668 ("%s: illegal symbol index %ld in relocs",
2669 bfd_get_filename (input_bfd), symndx);
2670 return false;
2671 }
252b5132
RH
2672 else
2673 {
2674 h = obj_coff_sym_hashes (input_bfd)[symndx];
2675 sym = syms + symndx;
2676 }
2677
2678 /* COFF treats common symbols in one of two ways. Either the
2679 size of the symbol is included in the section contents, or it
2680 is not. We assume that the size is not included, and force
2681 the rtype_to_howto function to adjust the addend as needed. */
2682
2683 if (sym != NULL && sym->n_scnum != 0)
2684 addend = - sym->n_value;
2685 else
2686 addend = 0;
2687
2688
2689 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2690 sym, &addend);
2691 if (howto == NULL)
2692 return false;
2693
2694 /* If we are doing a relocateable link, then we can just ignore
2695 a PC relative reloc that is pcrel_offset. It will already
2696 have the correct value. If this is not a relocateable link,
2697 then we should ignore the symbol value. */
2698 if (howto->pc_relative && howto->pcrel_offset)
2699 {
2700 if (info->relocateable)
2701 continue;
2702 if (sym != NULL && sym->n_scnum != 0)
2703 addend += sym->n_value;
2704 }
2705
2706 val = 0;
2707
2708 if (h == NULL)
2709 {
2710 asection *sec;
2711
2712 if (symndx == -1)
2713 {
2714 sec = bfd_abs_section_ptr;
2715 val = 0;
2716 }
2717 else
2718 {
2719 sec = sections[symndx];
2720 val = (sec->output_section->vma
2721 + sec->output_offset
2722 + sym->n_value);
2723 if (! obj_pe (input_bfd))
2724 val -= sec->vma;
2725 }
2726 }
2727 else
2728 {
2729 if (h->root.type == bfd_link_hash_defined
2730 || h->root.type == bfd_link_hash_defweak)
2731 {
2732 asection *sec;
2733
2734 sec = h->root.u.def.section;
2735 val = (h->root.u.def.value
2736 + sec->output_section->vma
2737 + sec->output_offset);
2738 }
2739
2740 else if (! info->relocateable)
2741 {
2742 if (! ((*info->callbacks->undefined_symbol)
2743 (info, h->root.root.string, input_bfd, input_section,
2744 rel->r_vaddr - input_section->vma)))
2745 return false;
2746 }
2747 }
2748
2749 if (info->base_file)
2750 {
2751 /* Emit a reloc if the backend thinks it needs it. */
2752 if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
2753 {
2754 /* Relocation to a symbol in a section which isn't
2755 absolute. We output the address here to a file.
2756 This file is then read by dlltool when generating the
2757 reloc section. Note that the base file is not
2758 portable between systems. We write out a long here,
2759 and dlltool reads in a long. */
2760 long addr = (rel->r_vaddr
2761 - input_section->vma
2762 + input_section->output_offset
2763 + input_section->output_section->vma);
2764 if (coff_data (output_bfd)->pe)
2765 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
2766 if (fwrite (&addr, 1, sizeof (long), (FILE *) info->base_file)
2767 != sizeof (long))
2768 {
2769 bfd_set_error (bfd_error_system_call);
2770 return false;
2771 }
2772 }
2773 }
2774
2775 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
2776 contents,
2777 rel->r_vaddr - input_section->vma,
2778 val, addend);
2779
2780 switch (rstat)
2781 {
2782 default:
2783 abort ();
2784 case bfd_reloc_ok:
2785 break;
2786 case bfd_reloc_outofrange:
2787 (*_bfd_error_handler)
2788 (_("%s: bad reloc address 0x%lx in section `%s'"),
2789 bfd_get_filename (input_bfd),
2790 (unsigned long) rel->r_vaddr,
2791 bfd_get_section_name (input_bfd, input_section));
2792 return false;
2793 case bfd_reloc_overflow:
2794 {
2795 const char *name;
2796 char buf[SYMNMLEN + 1];
2797
2798 if (symndx == -1)
2799 name = "*ABS*";
2800 else if (h != NULL)
2801 name = h->root.root.string;
2802 else
2803 {
2804 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
2805 if (name == NULL)
2806 return false;
2807 }
2808
2809 if (! ((*info->callbacks->reloc_overflow)
2810 (info, name, howto->name, (bfd_vma) 0, input_bfd,
2811 input_section, rel->r_vaddr - input_section->vma)))
2812 return false;
2813 }
2814 }
2815 }
2816 return true;
2817}
2818
This page took 0.155012 seconds and 4 git commands to generate.