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