1 /* COFF specific linker code.
2 Copyright 1994, 1995 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This file contains the COFF backend linker code. */
27 #include "coff/internal.h"
30 #define STRING_SIZE_SIZE (4)
32 /* Information we keep for each section in the output file when doing
33 a relocateable link. */
35 struct coff_link_section_info
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;
44 /* Information that we pass around while doing the final link step. */
46 struct coff_final_link_info
48 /* General link information. */
49 struct bfd_link_info *info;
52 /* Used to indicate failure in traversal routine. */
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). */
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. */
67 /* Buffer large enough to hold output indices of symbols of any
70 /* Buffer large enough to hold output symbols for any input file. */
72 /* Buffer large enough to hold external line numbers for any input
75 /* Buffer large enough to hold any input section. */
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;
82 enum bfd_link_subsystem subsystem;
83 bfd_link_stack_heap stack_heap_parameters;
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 *));
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;
135 const char * DLL_name;
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;
145 /* Create an entry in a COFF linker hash table. */
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;
153 struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
155 /* Allocate the structure if it has not already been allocated by a
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)
162 bfd_set_error (bfd_error_no_memory);
163 return (struct bfd_hash_entry *) ret;
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,
170 if (ret != (struct coff_link_hash_entry *) NULL)
172 /* Set local fields. */
181 return (struct bfd_hash_entry *) ret;
184 /* Create a COFF linker hash table. */
186 struct bfd_link_hash_table *
187 _bfd_coff_link_hash_table_create (abfd)
190 struct coff_link_hash_table *ret;
192 ret = ((struct coff_link_hash_table *)
193 malloc (sizeof (struct coff_link_hash_table)));
196 bfd_set_error (bfd_error_no_memory);
199 if (! _bfd_link_hash_table_init (&ret->root, abfd,
200 coff_link_hash_newfunc))
203 return (struct bfd_link_hash_table *) NULL;
208 /* Given a COFF BFD, add symbols to the global hash table as
212 _bfd_coff_link_add_symbols (abfd, info)
214 struct bfd_link_info *info;
216 switch (bfd_get_format (abfd))
219 return coff_link_add_object_symbols (abfd, info);
221 return (_bfd_generic_link_add_archive_symbols
222 (abfd, info, coff_link_check_archive_element));
224 bfd_set_error (bfd_error_wrong_format);
229 /* Add symbols from a COFF object file. */
232 coff_link_add_object_symbols (abfd, info)
234 struct bfd_link_info *info;
236 if (! coff_link_get_symbols (abfd))
238 if (! coff_link_add_symbols (abfd, info))
240 if (! info->keep_memory)
242 if (! coff_link_free_symbols (abfd))
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. */
254 coff_link_check_archive_element (abfd, info, pneeded)
256 struct bfd_link_info *info;
259 if (! coff_link_get_symbols (abfd))
262 if (! coff_link_check_ar_symbols (abfd, info, pneeded))
267 if (! coff_link_add_symbols (abfd, info))
271 if (! info->keep_memory || ! *pneeded)
273 if (! coff_link_free_symbols (abfd))
280 /* Read in the external symbols. */
283 coff_link_get_symbols (abfd)
286 bfd_size_type symesz;
290 if (obj_coff_external_syms (abfd) != NULL)
293 symesz = bfd_coff_symesz (abfd);
295 size = obj_raw_syment_count (abfd) * symesz;
297 syms = malloc (size);
298 if (syms == NULL && size != 0)
300 bfd_set_error (bfd_error_no_memory);
304 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
305 || bfd_read (syms, size, 1, abfd) != size)
312 obj_coff_external_syms (abfd) = syms;
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. */
322 coff_read_string_table (abfd)
325 char extstrsize[STRING_SIZE_SIZE];
329 if (obj_coff_strings (abfd) != NULL)
330 return obj_coff_strings (abfd);
333 (obj_sym_filepos (abfd)
334 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
338 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
340 if (bfd_get_error () != bfd_error_file_truncated)
343 /* There is no string table. */
344 strsize = STRING_SIZE_SIZE;
348 #if STRING_SIZE_SIZE == 4
349 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
351 #error Change bfd_h_get_32
355 strings = malloc (strsize);
358 bfd_set_error (bfd_error_no_memory);
362 if (bfd_read (strings + STRING_SIZE_SIZE,
363 strsize - STRING_SIZE_SIZE, 1, abfd)
364 != strsize - STRING_SIZE_SIZE)
370 obj_coff_strings (abfd) = strings;
375 /* Free up the external symbols and strings read from a COFF file. */
378 coff_link_free_symbols (abfd)
381 if (obj_coff_external_syms (abfd) != NULL)
383 free (obj_coff_external_syms (abfd));
384 obj_coff_external_syms (abfd) = NULL;
386 if (obj_coff_strings (abfd) != NULL)
388 free (obj_coff_strings (abfd));
389 obj_coff_strings (abfd) = NULL;
394 /* Look through the symbols to see if this object file should be
395 included in the link. */
398 coff_link_check_ar_symbols (abfd, info, pneeded)
400 struct bfd_link_info *info;
403 boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
405 bfd_size_type symesz;
411 sym_is_global = coff_backend_info (abfd)->_bfd_coff_sym_is_global;
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)
419 struct internal_syment sym;
421 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
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))
428 char buf[SYMNMLEN + 1];
429 struct bfd_link_hash_entry *h;
431 /* This symbol is externally visible, and is defined by this
434 /* FIXME: It's not clear this will work correctly if sizeof
436 if (sym._n._n_n._n_zeroes != 0
437 || sym._n._n_n._n_offset == 0)
439 memcpy (buf, sym._n._n_name, SYMNMLEN);
440 buf[SYMNMLEN] = '\0';
445 BFD_ASSERT (sym._n._n_n._n_offset >= STRING_SIZE_SIZE);
448 strings = coff_read_string_table (abfd);
452 name = strings + sym._n._n_n._n_offset;
455 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
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
461 if (h != (struct bfd_link_hash_entry *) NULL
462 && h->type == bfd_link_hash_undefined)
464 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
471 esym += (sym.n_numaux + 1) * symesz;
474 /* We do not need this object file. */
478 /* Add all the symbols from an object file to the hash table. */
481 coff_link_add_symbols (abfd, info)
483 struct bfd_link_info *info;
485 boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
487 boolean default_copy;
488 bfd_size_type symcount;
489 struct coff_link_hash_entry **sym_hash;
490 bfd_size_type symesz;
494 sym_is_global = coff_backend_info (abfd)->_bfd_coff_sym_is_global;
497 if (info->keep_memory)
498 default_copy = false;
502 symcount = obj_raw_syment_count (abfd);
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 **)
509 * sizeof (struct coff_link_hash_entry *))));
510 if (sym_hash == NULL && symcount != 0)
512 bfd_set_error (bfd_error_no_memory);
515 obj_coff_sym_hashes (abfd) = sym_hash;
517 (size_t) symcount * sizeof (struct coff_link_hash_entry *));
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)
525 struct internal_syment sym;
528 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
530 if (sym.n_sclass == C_EXT
531 || (sym_is_global && (*sym_is_global) (abfd, &sym)))
534 char buf[SYMNMLEN + 1];
539 /* This symbol is externally visible. */
541 /* FIXME: It's not clear this will work correctly if sizeof
544 if (sym._n._n_n._n_zeroes == 0
545 && sym._n._n_n._n_offset != 0)
547 BFD_ASSERT (sym._n._n_n._n_offset >= STRING_SIZE_SIZE);
550 strings = coff_read_string_table (abfd);
554 name = strings + sym._n._n_n._n_offset;
558 memcpy (buf, sym._n._n_name, SYMNMLEN);
559 buf[SYMNMLEN] = '\0';
566 if (sym.n_scnum == 0)
571 section = bfd_und_section_ptr;
576 section = bfd_com_section_ptr;
581 flags = BSF_EXPORT | BSF_GLOBAL;
582 section = coff_section_from_bfd_index (abfd, sym.n_scnum);
583 value -= section->vma;
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)))
592 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
594 if (((*sym_hash)->class == C_NULL
595 && (*sym_hash)->type == T_NULL)
598 && (*sym_hash)->root.type != bfd_link_hash_defined))
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)
606 union internal_auxent *alloc;
609 union internal_auxent *iaux;
611 alloc = ((union internal_auxent *)
612 bfd_hash_allocate (&info->hash->table,
614 * sizeof (*alloc))));
617 bfd_set_error (bfd_error_no_memory);
620 for (i = 0, eaux = esym + symesz, iaux = alloc;
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,
626 (*sym_hash)->aux = alloc;
632 esym += (sym.n_numaux + 1) * symesz;
633 sym_hash += sym.n_numaux + 1;
639 /* parse out a -heap <reserved>,<commit> line */
642 dores_com (ptr, def,res, com)
649 *res = strtoul (ptr, &ptr, 0);
651 *com = strtoul (ptr+1, &ptr, 0);
655 static char *get_name(ptr, dst)
662 while (*ptr && *ptr != ' ')
667 /* Process any magic embedded commands in a section called .drectve */
670 process_embedded_commands (abfd)
673 asection *sec = bfd_get_section_by_name (abfd, ".drectve");
680 copy = malloc (sec->_raw_size);
683 bfd_set_error (bfd_error_no_memory);
686 if (! bfd_get_section_contents(abfd, sec, copy, 0, sec->_raw_size))
691 e = copy + sec->_raw_size;
692 for (s = copy; s < e ; )
698 if (strncmp (s,"-attr", 5) == 0)
710 s = get_name(s, &name);
711 s = get_name(s, &attribs);
731 asec = bfd_get_section_by_name (abfd, name);
734 asec->flags |= SEC_CODE;
736 asec->flags |= SEC_READONLY;
739 else if (strncmp (s,"-heap", 5) == 0)
742 &NT_stack_heap.heap_defined,
743 &NT_stack_heap.heap_reserve,
744 &NT_stack_heap.heap_commit);
746 else if (strncmp (s,"-stack", 6) == 0)
749 &NT_stack_heap.heap_defined,
750 &NT_stack_heap.heap_reserve,
751 &NT_stack_heap.heap_commit);
759 /* Do the final link step. */
762 _bfd_coff_final_link (abfd, info)
764 struct bfd_link_info *info;
766 bfd_size_type symesz;
767 struct coff_final_link_info finfo;
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;
777 file_ptr line_filepos;
780 bfd_byte *external_relocs = NULL;
781 char strbuf[STRING_SIZE_SIZE];
783 symesz = bfd_coff_symesz (abfd);
786 finfo.output_bfd = abfd;
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;
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;
808 finfo.strtab = _bfd_stringtab_init ();
809 if (finfo.strtab == NULL)
812 /* Compute the file positions for all the sections. */
813 if (! abfd->output_has_begun)
814 bfd_coff_compute_section_file_positions (abfd);
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;
823 for (o = abfd->sections; o != NULL; o = o->next)
827 for (p = o->link_order_head; p != NULL; p = p->next)
829 if (p->type == bfd_indirect_link_order)
833 sec = p->u.indirect.section;
835 if (info->strip == strip_none
836 || info->strip == strip_some)
837 o->lineno_count += sec->lineno_count;
839 if (info->relocateable)
840 o->reloc_count += sec->reloc_count;
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;
849 else if (info->relocateable
850 && (p->type == bfd_section_reloc_link_order
851 || p->type == bfd_symbol_reloc_link_order))
854 if (o->reloc_count == 0)
858 o->flags |= SEC_RELOC;
859 o->rel_filepos = rel_filepos;
860 rel_filepos += o->reloc_count * relsz;
864 /* If doing a relocateable link, allocate space for the pointers we
866 if (info->relocateable)
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)
877 bfd_set_error (bfd_error_no_memory);
880 for (i = 0; i <= abfd->section_count; i++)
882 finfo.section_info[i].relocs = NULL;
883 finfo.section_info[i].rel_hashes = NULL;
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)
894 if (o->lineno_count == 0)
898 o->line_filepos = line_filepos;
899 line_filepos += o->lineno_count * linesz;
902 if (o->reloc_count != 0)
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
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
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)
928 bfd_set_error (bfd_error_no_memory);
932 if (o->reloc_count > max_output_reloc_count)
933 max_output_reloc_count = o->reloc_count;
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. */
942 obj_sym_filepos (abfd) = line_filepos;
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
948 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
952 sub->output_has_begun = false;
953 sz = obj_raw_syment_count (sub);
954 if (sz > max_sym_count)
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))
984 bfd_set_error (bfd_error_no_memory);
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)
996 for (p = o->link_order_head; p != NULL; p = p->next)
998 if (p->type == bfd_indirect_link_order
999 && (bfd_get_flavour (p->u.indirect.section->owner)
1000 == bfd_target_coff_flavour))
1002 sub = p->u.indirect.section->owner;
1003 if (! sub->output_has_begun)
1005 if (! coff_link_input_bfd (&finfo, sub))
1007 sub->output_has_begun = true;
1010 else if (p->type == bfd_section_reloc_link_order
1011 || p->type == bfd_symbol_reloc_link_order)
1013 if (! coff_reloc_link_order (abfd, &finfo, o, p))
1018 if (! _bfd_default_link_order (abfd, info, o, p))
1024 /* Free up the buffers used by coff_link_input_bfd. */
1025 if (finfo.internal_syms != NULL)
1027 free (finfo.internal_syms);
1028 finfo.internal_syms = NULL;
1030 if (finfo.sec_ptrs != NULL)
1032 free (finfo.sec_ptrs);
1033 finfo.sec_ptrs = NULL;
1035 if (finfo.sym_indices != NULL)
1037 free (finfo.sym_indices);
1038 finfo.sym_indices = NULL;
1040 if (finfo.linenos != NULL)
1042 free (finfo.linenos);
1043 finfo.linenos = NULL;
1045 if (finfo.contents != NULL)
1047 free (finfo.contents);
1048 finfo.contents = NULL;
1050 if (finfo.external_relocs != NULL)
1052 free (finfo.external_relocs);
1053 finfo.external_relocs = NULL;
1055 if (finfo.internal_relocs != NULL)
1057 free (finfo.internal_relocs);
1058 finfo.internal_relocs = NULL;
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
1064 if (finfo.last_file_index != -1
1065 && finfo.last_file.n_value != obj_raw_syment_count (abfd))
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);
1071 (obj_sym_filepos (abfd)
1072 + finfo.last_file_index * symesz),
1074 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
1078 /* Write out the global symbols. */
1079 finfo.failed = false;
1080 coff_link_hash_traverse (coff_hash_table (info), coff_write_global_sym,
1085 /* The outsyms buffer is used by coff_write_global_sym. */
1086 if (finfo.outsyms != NULL)
1088 free (finfo.outsyms);
1089 finfo.outsyms = NULL;
1092 if (info->relocateable)
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)
1100 bfd_set_error (bfd_error_no_memory);
1104 for (o = abfd->sections; o != NULL; o = o->next)
1106 struct internal_reloc *irel;
1107 struct internal_reloc *irelend;
1108 struct coff_link_hash_entry **rel_hash;
1111 if (o->reloc_count == 0)
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)
1120 if (*rel_hash != NULL)
1122 BFD_ASSERT ((*rel_hash)->indx >= 0);
1123 irel->r_symndx = (*rel_hash)->indx;
1125 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
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)
1134 free (external_relocs);
1135 external_relocs = NULL;
1138 /* Free up the section information. */
1139 if (finfo.section_info != NULL)
1143 for (i = 0; i < abfd->section_count; i++)
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);
1150 free (finfo.section_info);
1151 finfo.section_info = NULL;
1154 /* Write out the string table. */
1156 (obj_sym_filepos (abfd)
1157 + obj_raw_syment_count (abfd) * symesz),
1161 #if STRING_SIZE_SIZE == 4
1163 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
1164 (bfd_byte *) strbuf);
1166 #error Change bfd_h_put_32
1169 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
1172 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
1175 _bfd_stringtab_free (finfo.strtab);
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;
1184 if (finfo.strtab != NULL)
1185 _bfd_stringtab_free (finfo.strtab);
1186 if (finfo.section_info != NULL)
1190 for (i = 0; i < abfd->section_count; i++)
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);
1197 free (finfo.section_info);
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);
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. */
1224 coff_link_input_bfd (finfo, input_bfd)
1225 struct coff_final_link_info *finfo;
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 *,
1233 const char *strings;
1234 bfd_size_type syment_base;
1235 unsigned int n_tmask;
1236 unsigned int n_btshft;
1238 bfd_size_type isymesz;
1239 bfd_size_type osymesz;
1240 bfd_size_type linesz;
1243 struct internal_syment *isymp;
1248 struct coff_link_hash_entry **sym_hash;
1249 bfd_size_type relsz;
1252 /* Move all the symbols to the output file. */
1254 output_bfd = finfo->output_bfd;
1255 sym_is_global = coff_backend_info (input_bfd)->_bfd_coff_sym_is_global;
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));
1263 n_tmask = coff_data (input_bfd)->local_n_tmask;
1264 n_btshft = coff_data (input_bfd)->local_n_btshft;
1266 /* Define macros so that ISFCN, et. al., macros work correctly. */
1267 #define N_TMASK n_tmask
1268 #define N_BTSHFT n_btshft
1271 if (! finfo->info->keep_memory)
1274 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1277 if (! coff_link_get_symbols (input_bfd))
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;
1288 if (obj_pe (output_bfd))
1290 if (!process_embedded_commands (input_bfd))
1294 while (esym < esym_end)
1296 struct internal_syment isym;
1301 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
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
1309 if (isym.n_scnum != 0)
1310 *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1313 if (isym.n_value == 0)
1314 *secpp = bfd_und_section_ptr;
1316 *secpp = bfd_com_section_ptr;
1323 add = 1 + isym.n_numaux;
1325 /* If we are stripping all symbols, we want to skip this one. */
1326 if (finfo->info->strip == strip_all)
1331 if (isym.n_sclass == C_EXT
1332 || (sym_is_global && (*sym_is_global) (input_bfd, &isym)))
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. */
1339 if (! ISFCN (isym.n_type))
1344 /* This is a local symbol. Skip it if we are discarding
1346 if (finfo->info->discard == discard_all)
1351 /* If we stripping debugging symbols, and this is a debugging
1352 symbol, then skip it. */
1354 && finfo->info->strip == strip_debugger
1355 && isym.n_scnum == N_DEBUG)
1358 /* If some symbols are stripped based on the name, work out the
1359 name and decide whether to skip this symbol. */
1361 && (finfo->info->strip == strip_some
1362 || finfo->info->discard == discard_l))
1365 char buf[SYMNMLEN + 1];
1367 if (isym._n._n_n._n_zeroes == 0
1368 && isym._n._n_n._n_offset != 0)
1370 if (strings == NULL)
1372 strings = coff_read_string_table (input_bfd);
1373 if (strings == NULL)
1376 name = strings + isym._n._n_n._n_offset;
1380 memcpy (buf, isym._n._n_name, SYMNMLEN);
1381 buf[SYMNMLEN] = '\0';
1385 if ((finfo->info->strip == strip_some
1386 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
1389 && finfo->info->discard == discard_l
1390 && strncmp (name, finfo->info->lprefix,
1391 finfo->info->lprefix_len) == 0))
1395 /* We now know whether we are to skip this symbol or not. */
1398 /* Adjust the symbol in order to output it. */
1400 if (isym._n._n_n._n_zeroes == 0
1401 && isym._n._n_n._n_offset != 0)
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
1411 BFD_ASSERT (isym._n._n_n._n_offset >= STRING_SIZE_SIZE);
1412 if (strings == NULL)
1414 strings = coff_read_string_table (input_bfd);
1415 if (strings == NULL)
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)
1422 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1425 if (isym.n_scnum > 0)
1427 isym.n_scnum = (*secpp)->output_section->target_index;
1428 isym.n_value += ((*secpp)->output_section->vma
1429 + (*secpp)->output_offset
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)
1444 if (finfo->last_file_index != -1
1445 && finfo->last_file.n_value != output_index)
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)
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
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,
1467 if (bfd_seek (output_bfd,
1468 (obj_sym_filepos (output_bfd)
1469 + finfo->last_file_index * osymesz),
1471 || (bfd_write (outsym, osymesz, 1, output_bfd)
1477 finfo->last_file_index = output_index;
1478 finfo->last_file = isym;
1481 /* Output the symbol. */
1483 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
1485 *indexp = output_index;
1490 struct coff_link_hash_entry *h;
1492 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1494 h = obj_coff_sym_hashes (input_bfd)[indx];
1495 BFD_ASSERT (h != NULL);
1496 h->indx = output_index;
1499 output_index += add;
1500 outsym += add * osymesz;
1503 esym += add * isymesz;
1507 for (--add; add > 0; --add)
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. */
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)
1528 add = 1 + isymp->n_numaux;
1531 && (*sym_hash == NULL
1532 || (*sym_hash)->auxbfd != input_bfd))
1533 esym += add * isymesz;
1536 struct coff_link_hash_entry *h;
1543 BFD_ASSERT (h->numaux == isymp->n_numaux);
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++)
1555 union internal_auxent aux;
1556 union internal_auxent *auxp;
1562 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
1563 isymp->n_sclass, i, isymp->n_numaux,
1568 if (isymp->n_sclass == C_FILE)
1570 /* If this is a long filename, we must put it in the
1572 if (auxp->x_file.x_n.x_zeroes == 0
1573 && auxp->x_file.x_n.x_offset != 0)
1575 const char *filename;
1578 BFD_ASSERT (auxp->x_file.x_n.x_offset
1579 >= STRING_SIZE_SIZE);
1580 if (strings == NULL)
1582 strings = coff_read_string_table (input_bfd);
1583 if (strings == NULL)
1586 filename = strings + auxp->x_file.x_n.x_offset;
1587 indx = _bfd_stringtab_add (finfo->strtab, filename,
1589 if (indx == (bfd_size_type) -1)
1591 auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
1594 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
1598 if (ISFCN (isymp->n_type)
1599 || ISTAG (isymp->n_sclass)
1600 || isymp->n_sclass == C_BLOCK)
1602 indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
1604 && indx < obj_raw_syment_count (input_bfd))
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
1610 while (finfo->sym_indices[indx] < 0
1611 && indx < obj_raw_syment_count (input_bfd))
1613 if (indx >= obj_raw_syment_count (input_bfd))
1614 indx = output_index;
1616 indx = finfo->sym_indices[indx];
1617 auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
1621 indx = auxp->x_sym.x_tagndx.l;
1622 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
1624 indx = finfo->sym_indices[indx];
1626 auxp->x_sym.x_tagndx.l = 0;
1628 auxp->x_sym.x_tagndx.l = indx;
1634 bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isymp->n_type,
1635 isymp->n_sclass, i, isymp->n_numaux,
1649 /* Relocate the line numbers, unless we are stripping them. */
1650 if (finfo->info->strip == strip_none
1651 || finfo->info->strip == strip_some)
1653 for (o = input_bfd->sections; o != NULL; o = o->next)
1659 if (o->lineno_count == 0)
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)
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)
1672 struct internal_lineno iline;
1674 bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline);
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)))
1684 indx = finfo->sym_indices[iline.l_addr.l_symndx];
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
1697 struct internal_syment is;
1698 union internal_auxent ia;
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
1707 bfd_coff_swap_sym_in (output_bfd,
1708 (PTR) (finfo->outsyms
1709 + ((indx - syment_base)
1712 if ((ISFCN (is.n_type)
1713 || is.n_sclass == C_BLOCK)
1714 && is.n_numaux >= 1)
1718 auxptr = (PTR) (finfo->outsyms
1719 + ((indx - syment_base + 1)
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);
1734 iline.l_addr.l_symndx = indx;
1737 bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline, (PTR) eline);
1740 if (bfd_seek (output_bfd,
1741 (o->output_section->line_filepos
1742 + o->output_section->lineno_count * linesz),
1744 || bfd_write (finfo->linenos, linesz, o->lineno_count,
1745 output_bfd) != linesz * o->lineno_count)
1748 o->output_section->lineno_count += o->lineno_count;
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
1756 if (finfo->last_file_index != -1
1757 && finfo->last_file_index >= syment_base)
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)
1766 /* Write the modified symbols to the output file. */
1767 if (outsym > finfo->outsyms)
1769 if (bfd_seek (output_bfd,
1770 obj_sym_filepos (output_bfd) + syment_base * osymesz,
1772 || bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
1773 output_bfd) != outsym - finfo->outsyms)
1776 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
1777 + (outsym - finfo->outsyms) / osymesz)
1780 obj_raw_syment_count (output_bfd) = output_index;
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)
1788 if ((o->flags & SEC_HAS_CONTENTS) == 0)
1791 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
1792 (file_ptr) 0, o->_raw_size))
1795 if ((o->flags & SEC_RELOC) != 0)
1798 struct internal_reloc *internal_relocs;
1801 struct internal_reloc *irel;
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))
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
1812 target_index = o->output_section->target_index;
1813 if (! finfo->info->relocateable)
1814 internal_relocs = finfo->internal_relocs;
1816 internal_relocs = (finfo->section_info[target_index].relocs
1817 + o->output_section->reloc_count);
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);
1826 /* Call processor specific code to relocate the section
1828 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
1832 finfo->internal_syms,
1836 if (finfo->info->relocateable)
1839 struct internal_reloc *irelend;
1840 struct coff_link_hash_entry **rel_hash;
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++)
1849 struct coff_link_hash_entry *h;
1854 /* Adjust the reloc address and symbol index. */
1856 irel->r_vaddr += offset;
1858 if (irel->r_symndx == -1)
1863 if (! (*adjust_symndx) (output_bfd, finfo->info,
1871 h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
1874 /* This is a global symbol. */
1876 irel->r_symndx = h->indx;
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. */
1893 indx = finfo->sym_indices[irel->r_symndx];
1895 irel->r_symndx = indx;
1898 struct internal_syment *is;
1900 char buf[SYMNMLEN + 1];
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
1906 is = finfo->internal_syms + irel->r_symndx;
1908 if (is->_n._n_n._n_zeroes == 0
1909 && is->_n._n_n._n_offset != 0)
1911 if (strings == NULL)
1913 strings = coff_read_string_table (input_bfd);
1914 if (strings == NULL)
1917 name = strings + is->_n._n_n._n_offset;
1921 memcpy (buf, is->_n._n_name, SYMNMLEN);
1922 buf[SYMNMLEN] = '\0';
1926 if (! ((*finfo->info->callbacks->unattached_reloc)
1927 (finfo->info, name, input_bfd, o,
1934 o->output_section->reloc_count += o->reloc_count;
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
1947 if (! finfo->info->keep_memory)
1949 if (! coff_link_free_symbols (input_bfd))
1956 /* Write out a global symbol. Called via coff_link_hash_traverse. */
1959 coff_write_global_sym (h, data)
1960 struct coff_link_hash_entry *h;
1963 struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
1965 struct internal_syment isym;
1966 bfd_size_type symesz;
1969 output_bfd = finfo->output_bfd;
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)
1982 switch (h->root.type)
1985 case bfd_link_hash_new:
1989 case bfd_link_hash_undefined:
1990 case bfd_link_hash_undefweak:
1991 isym.n_scnum = N_UNDEF;
1995 case bfd_link_hash_defined:
1996 case bfd_link_hash_defweak:
2000 sec = h->root.u.def.section->output_section;
2001 if (bfd_is_abs_section (sec))
2002 isym.n_scnum = N_ABS;
2004 isym.n_scnum = sec->target_index;
2005 isym.n_value = (h->root.u.def.value
2007 + h->root.u.def.section->output_offset);
2011 case bfd_link_hash_common:
2012 isym.n_scnum = N_UNDEF;
2013 isym.n_value = h->root.u.c.size;
2016 case bfd_link_hash_indirect:
2017 case bfd_link_hash_warning:
2018 /* Just ignore these. They can't be handled anyhow. */
2022 if (strlen (h->root.root.string) <= SYMNMLEN)
2023 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2030 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
2032 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
2034 if (indx == (bfd_size_type) -1)
2036 finfo->failed = true;
2039 isym._n._n_n._n_zeroes = 0;
2040 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2043 isym.n_sclass = h->class;
2044 isym.n_type = h->type;
2046 if (isym.n_sclass == C_NULL)
2047 isym.n_sclass = C_EXT;
2049 isym.n_numaux = h->numaux;
2051 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) finfo->outsyms);
2053 symesz = bfd_coff_symesz (output_bfd);
2055 if (bfd_seek (output_bfd,
2056 (obj_sym_filepos (output_bfd)
2057 + obj_raw_syment_count (output_bfd) * symesz),
2059 || bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
2061 finfo->failed = true;
2065 h->indx = obj_raw_syment_count (output_bfd);
2067 ++obj_raw_syment_count (output_bfd);
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++)
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)
2078 finfo->failed = true;
2081 ++obj_raw_syment_count (output_bfd);
2087 /* Handle a link order which is supposed to generate a reloc. */
2090 coff_reloc_link_order (output_bfd, finfo, output_section, link_order)
2092 struct coff_final_link_info *finfo;
2093 asection *output_section;
2094 struct bfd_link_order *link_order;
2096 reloc_howto_type *howto;
2097 struct internal_reloc *irel;
2098 struct coff_link_hash_entry **rel_hash_ptr;
2100 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2103 bfd_set_error (bfd_error_bad_value);
2107 if (link_order->u.reloc.p->addend != 0)
2111 bfd_reloc_status_type rstat;
2114 size = bfd_get_reloc_size (howto);
2115 buf = (bfd_byte *) bfd_zmalloc (size);
2118 bfd_set_error (bfd_error_no_memory);
2122 rstat = _bfd_relocate_contents (howto, output_bfd,
2123 link_order->u.reloc.p->addend, buf);
2129 case bfd_reloc_outofrange:
2131 case bfd_reloc_overflow:
2132 if (! ((*finfo->info->callbacks->reloc_overflow)
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)))
2146 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
2147 (file_ptr) link_order->offset, size);
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. */
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);
2161 memset (irel, 0, sizeof (struct internal_reloc));
2162 *rel_hash_ptr = NULL;
2164 irel->r_vaddr = output_section->vma + link_order->offset;
2166 if (link_order->type == bfd_section_reloc_link_order)
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. */
2173 *rel_hash_ptr = NULL;
2178 struct coff_link_hash_entry *h;
2180 h = coff_link_hash_lookup (coff_hash_table (finfo->info),
2181 link_order->u.reloc.p->u.name,
2182 false, false, true);
2186 irel->r_symndx = h->indx;
2189 /* Set the index to -2 to force this symbol to get
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)))
2206 /* FIXME: Is this always right? */
2207 irel->r_type = howto->type;
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. */
2212 /* FIXME: What is the right value for r_offset? Is zero OK? */
2214 ++output_section->reloc_count;
2219 /* A basic reloc handling routine which may be used by processors with
2223 _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
2224 input_section, contents, relocs, syms,
2227 struct bfd_link_info *info;
2229 asection *input_section;
2231 struct internal_reloc *relocs;
2232 struct internal_syment *syms;
2233 asection **sections;
2235 struct internal_reloc *rel;
2236 struct internal_reloc *relend;
2240 relend = rel + input_section->reloc_count;
2241 for (; rel < relend; rel++)
2244 struct coff_link_hash_entry *h;
2245 struct internal_syment *sym;
2248 reloc_howto_type *howto;
2249 bfd_reloc_status_type rstat;
2251 symndx = rel->r_symndx;
2260 h = obj_coff_sym_hashes (input_bfd)[symndx];
2261 sym = syms + symndx;
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. */
2269 if (sym != NULL && sym->n_scnum != 0)
2270 addend = - sym->n_value;
2275 howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
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. */
2298 sec = bfd_abs_section_ptr;
2303 sec = sections[symndx];
2304 val = (sec->output_section->vma
2305 + sec->output_offset
2308 if (obj_pe (output_bfd))
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)
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)
2320 val -= NT_IMAGE_BASE;
2329 if (h->root.type == bfd_link_hash_defined
2330 || h->root.type == bfd_link_hash_defweak)
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)
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)
2349 val -= NT_IMAGE_BASE;
2355 else if (! info->relocateable)
2357 if (! ((*info->callbacks->undefined_symbol)
2358 (info, h->root.root.string, input_bfd, input_section,
2359 rel->r_vaddr - input_section->vma)))
2364 if (obj_pe (output_bfd)) {
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
2375 if (strncmp (input_section->name, ".idata$5", 8) == 0)
2377 if (num_DLLs == 0) /* this is the first one */
2380 MS_DLL[num_DLLs].DLL_name = input_bfd->filename;
2381 MS_DLL[num_DLLs].num_entries += 1;
2383 else if (!strcmp (input_bfd->filename, MS_DLL[num_DLLs].DLL_name))
2385 /* this is just another entry */
2386 MS_DLL[num_DLLs].num_entries += 1;
2390 /* This is a new DLL */
2392 MS_DLL[num_DLLs].DLL_name = input_bfd->filename;
2393 MS_DLL[num_DLLs].num_entries += 1;
2398 else if (strncmp (input_section->name, ".idata$2", 8) == 0)
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) */
2409 char buf[SYMNMLEN + 1];
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;
2415 strncpy (buf, sym->_n._n_name, SYMNMLEN);
2416 buf[SYMNMLEN] = '\0';
2422 /* we have done at least one. The val fixups are based on the
2424 if (strncmp (name, ".idata$4", 8) == 0)
2426 add_to_val = idata_4_prev +
2427 ((MS_DLL[num_DLLs_done].num_entries + 1) * 4);
2428 idata_4_prev = add_to_val;
2430 else if (strncmp (name, ".idata$5", 8) == 0)
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*/
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)
2444 add_to_val = ((num_DLLs - 1) * 0x14) + 0x28;
2445 idata_4_prev = add_to_val;
2447 else if (strncmp (name, ".idata$5", 8) == 0)
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*/
2456 val = val + add_to_val;
2460 if (info->base_file)
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
2468 /* relocation to a symbol in a section which
2469 isn't absolute - we output the address here
2471 bfd_vma addr = rel->r_vaddr
2472 + input_section->output_offset
2473 + input_section->output_section->vma;
2474 fwrite (&addr, 1,4, info->base_file);
2478 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
2480 rel->r_vaddr - input_section->vma,
2489 case bfd_reloc_overflow:
2492 char buf[SYMNMLEN + 1];
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;
2503 strncpy (buf, sym->_n._n_name, SYMNMLEN);
2504 buf[SYMNMLEN] = '\0';
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)))