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