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