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