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