Tue Aug 29 13:50:21 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           /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1626              build_link_order in ldwrite.c will not have created a
1627              link order, which means that we will not have seen this
1628              input section in _bfd_coff_final_link, which means that
1629              we will not have allocated space for the line numbers of
1630              this section.  I don't think line numbers can be
1631              meaningful for a section which does not have
1632              SEC_HAS_CONTENTS set, but, if they do, this must be
1633              changed.  */
1634           if (o->lineno_count == 0
1635               || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
1636             continue;
1637
1638           if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
1639               || bfd_read (finfo->linenos, linesz, o->lineno_count,
1640                            input_bfd) != linesz * o->lineno_count)
1641             return false;
1642
1643           offset = o->output_section->vma + o->output_offset - o->vma;
1644           eline = finfo->linenos;
1645           elineend = eline + linesz * o->lineno_count;
1646           for (; eline < elineend; eline += linesz)
1647             {
1648               struct internal_lineno iline;
1649
1650               bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline);
1651
1652               if (iline.l_lnno != 0)
1653                 iline.l_addr.l_paddr += offset;
1654               else if (iline.l_addr.l_symndx >= 0
1655                        && (iline.l_addr.l_symndx
1656                            < obj_raw_syment_count (input_bfd)))
1657                 {
1658                   long indx;
1659
1660                   indx = finfo->sym_indices[iline.l_addr.l_symndx];
1661
1662                   if (indx < 0)
1663                     {
1664                       /* These line numbers are attached to a symbol
1665                          which we are stripping.  We should really
1666                          just discard the line numbers, but that would
1667                          be a pain because we have already counted
1668                          them.  */
1669                       indx = 0;
1670                     }
1671                   else
1672                     {
1673                       struct internal_syment is;
1674                       union internal_auxent ia;
1675
1676                       /* Fix up the lnnoptr field in the aux entry of
1677                          the symbol.  It turns out that we can't do
1678                          this when we modify the symbol aux entries,
1679                          because gas sometimes screws up the lnnoptr
1680                          field and makes it an offset from the start
1681                          of the line numbers rather than an absolute
1682                          file index.  */
1683                       bfd_coff_swap_sym_in (output_bfd,
1684                                             (PTR) (finfo->outsyms
1685                                                    + ((indx - syment_base)
1686                                                       * osymesz)),
1687                                             (PTR) &is);
1688                       if ((ISFCN (is.n_type)
1689                            || is.n_sclass == C_BLOCK)
1690                           && is.n_numaux >= 1)
1691                         {
1692                           PTR auxptr;
1693
1694                           auxptr = (PTR) (finfo->outsyms
1695                                           + ((indx - syment_base + 1)
1696                                              * osymesz));
1697                           bfd_coff_swap_aux_in (output_bfd, auxptr,
1698                                                 is.n_type, is.n_sclass,
1699                                                 0, is.n_numaux, (PTR) &ia);
1700                           ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
1701                             (o->output_section->line_filepos
1702                              + o->output_section->lineno_count * linesz
1703                              + eline - finfo->linenos);
1704                           bfd_coff_swap_aux_out (output_bfd, (PTR) &ia,
1705                                                  is.n_type, is.n_sclass, 0,
1706                                                  is.n_numaux, auxptr);
1707                         }
1708                     }
1709
1710                   iline.l_addr.l_symndx = indx;
1711                 }
1712
1713               bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline, (PTR) eline);
1714             }
1715
1716           if (bfd_seek (output_bfd,
1717                         (o->output_section->line_filepos
1718                          + o->output_section->lineno_count * linesz),
1719                         SEEK_SET) != 0
1720               || bfd_write (finfo->linenos, linesz, o->lineno_count,
1721                             output_bfd) != linesz * o->lineno_count)
1722             return false;
1723
1724           o->output_section->lineno_count += o->lineno_count;
1725         }
1726     }
1727
1728   /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1729      symbol will be the first symbol in the next input file.  In the
1730      normal case, this will save us from writing out the C_FILE symbol
1731      again.  */
1732   if (finfo->last_file_index != -1
1733       && finfo->last_file_index >= syment_base)
1734     {
1735       finfo->last_file.n_value = output_index;
1736       bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
1737                              (PTR) (finfo->outsyms
1738                                     + ((finfo->last_file_index - syment_base)
1739                                        * osymesz)));
1740     }
1741
1742   /* Write the modified symbols to the output file.  */
1743   if (outsym > finfo->outsyms)
1744     {
1745       if (bfd_seek (output_bfd,
1746                     obj_sym_filepos (output_bfd) + syment_base * osymesz,
1747                     SEEK_SET) != 0
1748           || bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
1749                         output_bfd) != outsym - finfo->outsyms)
1750         return false;
1751
1752       BFD_ASSERT ((obj_raw_syment_count (output_bfd)
1753                    + (outsym - finfo->outsyms) / osymesz)
1754                   == output_index);
1755
1756       obj_raw_syment_count (output_bfd) = output_index;
1757     }
1758
1759   /* Relocate the contents of each section.  */
1760   adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
1761   for (o = input_bfd->sections; o != NULL; o = o->next)
1762     {
1763       bfd_byte *contents;
1764
1765       if ((o->flags & SEC_HAS_CONTENTS) == 0)
1766         continue;
1767
1768       if (coff_section_data (input_bfd, o) != NULL
1769           && coff_section_data (input_bfd, o)->contents != NULL)
1770         contents = coff_section_data (input_bfd, o)->contents;
1771       else
1772         {
1773           if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
1774                                           (file_ptr) 0, o->_raw_size))
1775             return false;
1776           contents = finfo->contents;
1777         }
1778
1779       if ((o->flags & SEC_RELOC) != 0)
1780         {
1781           int target_index;
1782           struct internal_reloc *internal_relocs;
1783           struct internal_reloc *irel;
1784
1785           /* Read in the relocs.  */
1786           target_index = o->output_section->target_index;
1787           internal_relocs = (_bfd_coff_read_internal_relocs
1788                              (input_bfd, o, false, finfo->external_relocs,
1789                               finfo->info->relocateable,
1790                               (finfo->info->relocateable
1791                                ? (finfo->section_info[target_index].relocs
1792                                   + o->output_section->reloc_count)
1793                                : finfo->internal_relocs)));
1794           if (internal_relocs == NULL)
1795             return false;
1796
1797           /* Call processor specific code to relocate the section
1798              contents.  */
1799           if (! bfd_coff_relocate_section (output_bfd, finfo->info,
1800                                            input_bfd, o,
1801                                            contents,
1802                                            internal_relocs,
1803                                            finfo->internal_syms,
1804                                            finfo->sec_ptrs))
1805             return false;
1806
1807           if (finfo->info->relocateable)
1808             {
1809               bfd_vma offset;
1810               struct internal_reloc *irelend;
1811               struct coff_link_hash_entry **rel_hash;
1812
1813               offset = o->output_section->vma + o->output_offset - o->vma;
1814               irel = internal_relocs;
1815               irelend = irel + o->reloc_count;
1816               rel_hash = (finfo->section_info[target_index].rel_hashes
1817                           + o->output_section->reloc_count);
1818               for (; irel < irelend; irel++, rel_hash++)
1819                 {
1820                   struct coff_link_hash_entry *h;
1821                   boolean adjusted;
1822
1823                   *rel_hash = NULL;
1824
1825                   /* Adjust the reloc address and symbol index.  */
1826
1827                   irel->r_vaddr += offset;
1828
1829                   if (irel->r_symndx == -1)
1830                     continue;
1831
1832                   if (adjust_symndx)
1833                     {
1834                       if (! (*adjust_symndx) (output_bfd, finfo->info,
1835                                               input_bfd, o, irel,
1836                                               &adjusted))
1837                         return false;
1838                       if (adjusted)
1839                         continue;
1840                     }
1841
1842                   h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
1843                   if (h != NULL)
1844                     {
1845                       /* This is a global symbol.  */
1846                       if (h->indx >= 0)
1847                         irel->r_symndx = h->indx;
1848                       else
1849                         {
1850                           /* This symbol is being written at the end
1851                              of the file, and we do not yet know the
1852                              symbol index.  We save the pointer to the
1853                              hash table entry in the rel_hash list.
1854                              We set the indx field to -2 to indicate
1855                              that this symbol must not be stripped.  */
1856                           *rel_hash = h;
1857                           h->indx = -2;
1858                         }
1859                     }
1860                   else
1861                     {
1862                       long indx;
1863
1864                       indx = finfo->sym_indices[irel->r_symndx];
1865                       if (indx != -1)
1866                         irel->r_symndx = indx;
1867                       else
1868                         {
1869                           struct internal_syment *is;
1870                           const char *name;
1871                           char buf[SYMNMLEN + 1];
1872
1873                           /* This reloc is against a symbol we are
1874                              stripping.  It would be possible to
1875                              handle this case, but I don't think it's
1876                              worth it.  */
1877                           is = finfo->internal_syms + irel->r_symndx;
1878
1879                           name = (_bfd_coff_internal_syment_name
1880                                   (input_bfd, is, buf));
1881                           if (name == NULL)
1882                             return false;
1883
1884                           if (! ((*finfo->info->callbacks->unattached_reloc)
1885                                  (finfo->info, name, input_bfd, o,
1886                                   irel->r_vaddr)))
1887                             return false;
1888                         }
1889                     }
1890                 }
1891
1892               o->output_section->reloc_count += o->reloc_count;
1893             }
1894         }
1895
1896       /* Write out the modified section contents.  */
1897       if (! bfd_set_section_contents (output_bfd, o->output_section,
1898                                       contents, o->output_offset,
1899                                       (o->_cooked_size != 0
1900                                        ? o->_cooked_size
1901                                        : o->_raw_size)))
1902         return false;
1903     }
1904
1905   if (! finfo->info->keep_memory)
1906     {
1907       if (! _bfd_coff_free_symbols (input_bfd))
1908         return false;
1909     }
1910
1911   return true;
1912 }
1913
1914 /* Write out a global symbol.  Called via coff_link_hash_traverse.  */
1915
1916 static boolean
1917 coff_write_global_sym (h, data)
1918      struct coff_link_hash_entry *h;
1919      PTR data;
1920 {
1921   struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
1922   bfd *output_bfd;
1923   struct internal_syment isym;
1924   bfd_size_type symesz;
1925   unsigned int i;
1926
1927   output_bfd = finfo->output_bfd;
1928
1929   if (h->indx >= 0)
1930     return true;
1931
1932   if (h->indx != -2
1933       && (finfo->info->strip == strip_all
1934           || (finfo->info->strip == strip_some
1935               && (bfd_hash_lookup (finfo->info->keep_hash,
1936                                    h->root.root.string, false, false)
1937                   == NULL))))
1938     return true;
1939
1940   switch (h->root.type)
1941     {
1942     default:
1943     case bfd_link_hash_new:
1944       abort ();
1945       return false;
1946
1947     case bfd_link_hash_undefined:
1948     case bfd_link_hash_undefweak:
1949       isym.n_scnum = N_UNDEF;
1950       isym.n_value = 0;
1951       break;
1952
1953     case bfd_link_hash_defined:
1954     case bfd_link_hash_defweak:
1955       {
1956         asection *sec;
1957
1958         sec = h->root.u.def.section->output_section;
1959         if (bfd_is_abs_section (sec))
1960           isym.n_scnum = N_ABS;
1961         else
1962           isym.n_scnum = sec->target_index;
1963         isym.n_value = (h->root.u.def.value
1964                         + sec->vma
1965                         + h->root.u.def.section->output_offset);
1966       }
1967       break;
1968
1969     case bfd_link_hash_common:
1970       isym.n_scnum = N_UNDEF;
1971       isym.n_value = h->root.u.c.size;
1972       break;
1973
1974     case bfd_link_hash_indirect:
1975     case bfd_link_hash_warning:
1976       /* Just ignore these.  They can't be handled anyhow.  */
1977       return true;
1978     }
1979
1980   if (strlen (h->root.root.string) <= SYMNMLEN)
1981     strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
1982   else
1983     {
1984       boolean hash;
1985       bfd_size_type indx;
1986
1987       hash = true;
1988       if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1989         hash = false;
1990       indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
1991                                  false);
1992       if (indx == (bfd_size_type) -1)
1993         {
1994           finfo->failed = true;
1995           return false;
1996         }
1997       isym._n._n_n._n_zeroes = 0;
1998       isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1999     }
2000
2001   isym.n_sclass = h->class;
2002   isym.n_type = h->type;
2003
2004   if (isym.n_sclass == C_NULL)
2005     isym.n_sclass = C_EXT;
2006
2007   isym.n_numaux = h->numaux;
2008   
2009   bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) finfo->outsyms);
2010
2011   symesz = bfd_coff_symesz (output_bfd);
2012
2013   if (bfd_seek (output_bfd,
2014                 (obj_sym_filepos (output_bfd)
2015                  + obj_raw_syment_count (output_bfd) * symesz),
2016                 SEEK_SET) != 0
2017       || bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
2018     {
2019       finfo->failed = true;
2020       return false;
2021     }
2022
2023   h->indx = obj_raw_syment_count (output_bfd);
2024
2025   ++obj_raw_syment_count (output_bfd);
2026
2027   /* Write out any associated aux entries.  There normally will be
2028      none.  If there are any, I have no idea how to modify them.  */
2029   for (i = 0; i < isym.n_numaux; i++)
2030     {
2031       bfd_coff_swap_aux_out (output_bfd, (PTR) (h->aux + i), isym.n_type,
2032                              isym.n_sclass, i, isym.n_numaux,
2033                              (PTR) finfo->outsyms);
2034       if (bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
2035         {
2036           finfo->failed = true;
2037           return false;
2038         }
2039       ++obj_raw_syment_count (output_bfd);
2040     }
2041
2042   return true;
2043 }
2044
2045 /* Handle a link order which is supposed to generate a reloc.  */
2046
2047 static boolean
2048 coff_reloc_link_order (output_bfd, finfo, output_section, link_order)
2049      bfd *output_bfd;
2050      struct coff_final_link_info *finfo;
2051      asection *output_section;
2052      struct bfd_link_order *link_order;
2053 {
2054   reloc_howto_type *howto;
2055   struct internal_reloc *irel;
2056   struct coff_link_hash_entry **rel_hash_ptr;
2057
2058   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2059   if (howto == NULL)
2060     {
2061       bfd_set_error (bfd_error_bad_value);
2062       return false;
2063     }
2064
2065   if (link_order->u.reloc.p->addend != 0)
2066     {
2067       bfd_size_type size;
2068       bfd_byte *buf;
2069       bfd_reloc_status_type rstat;
2070       boolean ok;
2071
2072       size = bfd_get_reloc_size (howto);
2073       buf = (bfd_byte *) bfd_zmalloc (size);
2074       if (buf == NULL)
2075         {
2076           bfd_set_error (bfd_error_no_memory);
2077           return false;
2078         }
2079
2080       rstat = _bfd_relocate_contents (howto, output_bfd,
2081                                       link_order->u.reloc.p->addend, buf);
2082       switch (rstat)
2083         {
2084         case bfd_reloc_ok:
2085           break;
2086         default:
2087         case bfd_reloc_outofrange:
2088           abort ();
2089         case bfd_reloc_overflow:
2090           if (! ((*finfo->info->callbacks->reloc_overflow)
2091                  (finfo->info,
2092                   (link_order->type == bfd_section_reloc_link_order
2093                    ? bfd_section_name (output_bfd,
2094                                        link_order->u.reloc.p->u.section)
2095                    : link_order->u.reloc.p->u.name),
2096                   howto->name, link_order->u.reloc.p->addend,
2097                   (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2098             {
2099               free (buf);
2100               return false;
2101             }
2102           break;
2103         }
2104       ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
2105                                      (file_ptr) link_order->offset, size);
2106       free (buf);
2107       if (! ok)
2108         return false;
2109     }
2110
2111   /* Store the reloc information in the right place.  It will get
2112      swapped and written out at the end of the final_link routine.  */
2113
2114   irel = (finfo->section_info[output_section->target_index].relocs
2115           + output_section->reloc_count);
2116   rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
2117                   + output_section->reloc_count);
2118
2119   memset (irel, 0, sizeof (struct internal_reloc));
2120   *rel_hash_ptr = NULL;
2121
2122   irel->r_vaddr = output_section->vma + link_order->offset;
2123
2124   if (link_order->type == bfd_section_reloc_link_order)
2125     {
2126       /* We need to somehow locate a symbol in the right section.  The
2127          symbol must either have a value of zero, or we must adjust
2128          the addend by the value of the symbol.  FIXME: Write this
2129          when we need it.  The old linker couldn't handle this anyhow.  */
2130       abort ();
2131       *rel_hash_ptr = NULL;
2132       irel->r_symndx = 0;
2133     }
2134   else
2135     {
2136       struct coff_link_hash_entry *h;
2137
2138       h = coff_link_hash_lookup (coff_hash_table (finfo->info),
2139                                  link_order->u.reloc.p->u.name,
2140                                  false, false, true);
2141       if (h != NULL)
2142         {
2143           if (h->indx >= 0)
2144             irel->r_symndx = h->indx;
2145           else
2146             {
2147               /* Set the index to -2 to force this symbol to get
2148                  written out.  */
2149               h->indx = -2;
2150               *rel_hash_ptr = h;
2151               irel->r_symndx = 0;
2152             }
2153         }
2154       else
2155         {
2156           if (! ((*finfo->info->callbacks->unattached_reloc)
2157                  (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
2158                   (asection *) NULL, (bfd_vma) 0)))
2159             return false;
2160           irel->r_symndx = 0;
2161         }
2162     }
2163
2164   /* FIXME: Is this always right?  */
2165   irel->r_type = howto->type;
2166
2167   /* r_size is only used on the RS/6000, which needs its own linker
2168      routines anyhow.  r_extern is only used for ECOFF.  */
2169
2170   /* FIXME: What is the right value for r_offset?  Is zero OK?  */
2171
2172   ++output_section->reloc_count;
2173
2174   return true;
2175 }
2176
2177 /* A basic reloc handling routine which may be used by processors with
2178    simple relocs.  */
2179
2180 boolean
2181 _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
2182                                     input_section, contents, relocs, syms,
2183                                     sections)
2184      bfd *output_bfd;
2185      struct bfd_link_info *info;
2186      bfd *input_bfd;
2187      asection *input_section;
2188      bfd_byte *contents;
2189      struct internal_reloc *relocs;
2190      struct internal_syment *syms;
2191      asection **sections;
2192 {
2193   struct internal_reloc *rel;
2194   struct internal_reloc *relend;
2195
2196
2197   rel = relocs;
2198   relend = rel + input_section->reloc_count;
2199   for (; rel < relend; rel++)
2200     {
2201       long symndx;
2202       struct coff_link_hash_entry *h;
2203       struct internal_syment *sym;
2204       bfd_vma addend;
2205       bfd_vma val;
2206       reloc_howto_type *howto;
2207       bfd_reloc_status_type rstat;
2208
2209       symndx = rel->r_symndx;
2210
2211       if (symndx == -1)
2212         {
2213           h = NULL;
2214           sym = NULL;
2215         }
2216       else
2217         {    
2218           h = obj_coff_sym_hashes (input_bfd)[symndx];
2219           sym = syms + symndx;
2220         }
2221
2222       /* COFF treats common symbols in one of two ways.  Either the
2223          size of the symbol is included in the section contents, or it
2224          is not.  We assume that the size is not included, and force
2225          the rtype_to_howto function to adjust the addend as needed.  */
2226
2227       if (sym != NULL && sym->n_scnum != 0)
2228         addend = - sym->n_value;
2229       else
2230         addend = 0;
2231
2232
2233       howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2234                                        sym, &addend);
2235       if (howto == NULL)
2236         return false;
2237
2238       val = 0;
2239
2240       if (h == NULL)
2241         {
2242           asection *sec;
2243
2244           if (symndx == -1)
2245             {
2246               sec = bfd_abs_section_ptr;
2247               val = 0;
2248             }
2249           else
2250             {
2251               sec = sections[symndx];
2252               val = (sec->output_section->vma
2253                      + sec->output_offset
2254                      + sym->n_value
2255                      - sec->vma);
2256             }
2257         }
2258       else
2259         {
2260           if (h->root.type == bfd_link_hash_defined
2261               || h->root.type == bfd_link_hash_defweak)
2262             {
2263               asection *sec;
2264
2265               sec = h->root.u.def.section;
2266               val = (h->root.u.def.value
2267                      + sec->output_section->vma
2268                      + sec->output_offset);
2269               }
2270
2271           else if (! info->relocateable)
2272             {
2273               if (! ((*info->callbacks->undefined_symbol)
2274                      (info, h->root.root.string, input_bfd, input_section,
2275                       rel->r_vaddr - input_section->vma)))
2276                 return false;
2277             }
2278         }
2279
2280       if (info->base_file)
2281         {
2282           /* So if this is non pcrelative, and is referenced
2283              to a section or a common symbol, then it needs a reloc */
2284           if (!howto->pc_relative
2285               && (sym->n_scnum
2286                   || sym->n_value))
2287             {
2288               /* relocation to a symbol in a section which
2289                  isn't absolute - we output the address here 
2290                  to a file */
2291               bfd_vma addr = rel->r_vaddr 
2292                 + input_section->output_offset 
2293                   + input_section->output_section->vma;
2294               fwrite (&addr, 1,4, (FILE *) info->base_file);
2295             }
2296         }
2297   
2298       rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
2299                                         contents,
2300                                         rel->r_vaddr - input_section->vma,
2301                                         val, addend);
2302
2303       switch (rstat)
2304         {
2305         default:
2306           abort ();
2307         case bfd_reloc_ok:
2308           break;
2309         case bfd_reloc_overflow:
2310           {
2311             const char *name;
2312             char buf[SYMNMLEN + 1];
2313
2314             if (symndx == -1)
2315               name = "*ABS*";
2316             else if (h != NULL)
2317               name = h->root.root.string;
2318             else
2319               {
2320                 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
2321                 if (name == NULL)
2322                   return false;
2323               }
2324
2325             if (! ((*info->callbacks->reloc_overflow)
2326                    (info, name, howto->name, (bfd_vma) 0, input_bfd,
2327                     input_section, rel->r_vaddr - input_section->vma)))
2328               return false;
2329           }
2330         }
2331     }
2332
2333   return true;
2334 }