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