* elf-bfd.h (_bfd_elf_rel_vtable_reloc_fn): Declare.
[external/binutils.git] / bfd / elf.c
1 /* ELF executable support for BFD.
2    Copyright 1993, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /*
21
22 SECTION
23         ELF backends
24
25         BFD support for ELF formats is being worked on.
26         Currently, the best supported back ends are for sparc and i386
27         (running svr4 or Solaris 2).
28
29         Documentation of the internals of the support code still needs
30         to be written.  The code is changing quickly enough that we
31         haven't bothered yet.
32  */
33
34 #include "bfd.h"
35 #include "sysdep.h"
36 #include "bfdlink.h"
37 #include "libbfd.h"
38 #define ARCH_SIZE 0
39 #include "elf-bfd.h"
40
41 static INLINE struct elf_segment_map *make_mapping
42   PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
43 static boolean map_sections_to_segments PARAMS ((bfd *));
44 static int elf_sort_sections PARAMS ((const PTR, const PTR));
45 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
46 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
47 static boolean prep_headers PARAMS ((bfd *));
48 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
49 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
50 static char *elf_read PARAMS ((bfd *, long, unsigned int));
51 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
52 static boolean assign_section_numbers PARAMS ((bfd *));
53 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
54 static boolean elf_map_symbols PARAMS ((bfd *));
55 static bfd_size_type get_program_header_size PARAMS ((bfd *));
56
57 /* Swap version information in and out.  The version information is
58    currently size independent.  If that ever changes, this code will
59    need to move into elfcode.h.  */
60
61 /* Swap in a Verdef structure.  */
62
63 void
64 _bfd_elf_swap_verdef_in (abfd, src, dst)
65      bfd *abfd;
66      const Elf_External_Verdef *src;
67      Elf_Internal_Verdef *dst;
68 {
69   dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
70   dst->vd_flags   = bfd_h_get_16 (abfd, src->vd_flags);
71   dst->vd_ndx     = bfd_h_get_16 (abfd, src->vd_ndx);
72   dst->vd_cnt     = bfd_h_get_16 (abfd, src->vd_cnt);
73   dst->vd_hash    = bfd_h_get_32 (abfd, src->vd_hash);
74   dst->vd_aux     = bfd_h_get_32 (abfd, src->vd_aux);
75   dst->vd_next    = bfd_h_get_32 (abfd, src->vd_next);
76 }
77
78 /* Swap out a Verdef structure.  */
79
80 void
81 _bfd_elf_swap_verdef_out (abfd, src, dst)
82      bfd *abfd;
83      const Elf_Internal_Verdef *src;
84      Elf_External_Verdef *dst;
85 {
86   bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
87   bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
88   bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
89   bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
90   bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
91   bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
92   bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
93 }
94
95 /* Swap in a Verdaux structure.  */
96
97 void
98 _bfd_elf_swap_verdaux_in (abfd, src, dst)
99      bfd *abfd;
100      const Elf_External_Verdaux *src;
101      Elf_Internal_Verdaux *dst;
102 {
103   dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
104   dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
105 }
106
107 /* Swap out a Verdaux structure.  */
108
109 void
110 _bfd_elf_swap_verdaux_out (abfd, src, dst)
111      bfd *abfd;
112      const Elf_Internal_Verdaux *src;
113      Elf_External_Verdaux *dst;
114 {
115   bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
116   bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
117 }
118
119 /* Swap in a Verneed structure.  */
120
121 void
122 _bfd_elf_swap_verneed_in (abfd, src, dst)
123      bfd *abfd;
124      const Elf_External_Verneed *src;
125      Elf_Internal_Verneed *dst;
126 {
127   dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
128   dst->vn_cnt     = bfd_h_get_16 (abfd, src->vn_cnt);
129   dst->vn_file    = bfd_h_get_32 (abfd, src->vn_file);
130   dst->vn_aux     = bfd_h_get_32 (abfd, src->vn_aux);
131   dst->vn_next    = bfd_h_get_32 (abfd, src->vn_next);
132 }
133
134 /* Swap out a Verneed structure.  */
135
136 void
137 _bfd_elf_swap_verneed_out (abfd, src, dst)
138      bfd *abfd;
139      const Elf_Internal_Verneed *src;
140      Elf_External_Verneed *dst;
141 {
142   bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
143   bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
144   bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
145   bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
146   bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
147 }
148
149 /* Swap in a Vernaux structure.  */
150
151 void
152 _bfd_elf_swap_vernaux_in (abfd, src, dst)
153      bfd *abfd;
154      const Elf_External_Vernaux *src;
155      Elf_Internal_Vernaux *dst;
156 {
157   dst->vna_hash  = bfd_h_get_32 (abfd, src->vna_hash);
158   dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
159   dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
160   dst->vna_name  = bfd_h_get_32 (abfd, src->vna_name);
161   dst->vna_next  = bfd_h_get_32 (abfd, src->vna_next);
162 }
163
164 /* Swap out a Vernaux structure.  */
165
166 void
167 _bfd_elf_swap_vernaux_out (abfd, src, dst)
168      bfd *abfd;
169      const Elf_Internal_Vernaux *src;
170      Elf_External_Vernaux *dst;
171 {
172   bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
173   bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
174   bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
175   bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
176   bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
177 }
178
179 /* Swap in a Versym structure.  */
180
181 void
182 _bfd_elf_swap_versym_in (abfd, src, dst)
183      bfd *abfd;
184      const Elf_External_Versym *src;
185      Elf_Internal_Versym *dst;
186 {
187   dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
188 }
189
190 /* Swap out a Versym structure.  */
191
192 void
193 _bfd_elf_swap_versym_out (abfd, src, dst)
194      bfd *abfd;
195      const Elf_Internal_Versym *src;
196      Elf_External_Versym *dst;
197 {
198   bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
199 }
200
201 /* Standard ELF hash function.  Do not change this function; you will
202    cause invalid hash tables to be generated.  (Well, you would if this
203    were being used yet.)  */
204 unsigned long
205 bfd_elf_hash (name)
206      CONST unsigned char *name;
207 {
208   unsigned long h = 0;
209   unsigned long g;
210   int ch;
211
212   while ((ch = *name++) != '\0')
213     {
214       h = (h << 4) + ch;
215       if ((g = (h & 0xf0000000)) != 0)
216         {
217           h ^= g >> 24;
218           h &= ~g;
219         }
220     }
221   return h;
222 }
223
224 /* Read a specified number of bytes at a specified offset in an ELF
225    file, into a newly allocated buffer, and return a pointer to the
226    buffer. */
227
228 static char *
229 elf_read (abfd, offset, size)
230      bfd * abfd;
231      long offset;
232      unsigned int size;
233 {
234   char *buf;
235
236   if ((buf = bfd_alloc (abfd, size)) == NULL)
237     return NULL;
238   if (bfd_seek (abfd, offset, SEEK_SET) == -1)
239     return NULL;
240   if (bfd_read ((PTR) buf, size, 1, abfd) != size)
241     {
242       if (bfd_get_error () != bfd_error_system_call)
243         bfd_set_error (bfd_error_file_truncated);
244       return NULL;
245     }
246   return buf;
247 }
248
249 boolean
250 bfd_elf_mkobject (abfd)
251      bfd * abfd;
252 {
253   /* this just does initialization */
254   /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
255   elf_tdata (abfd) = (struct elf_obj_tdata *)
256     bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
257   if (elf_tdata (abfd) == 0)
258     return false;
259   /* since everything is done at close time, do we need any
260      initialization? */
261
262   return true;
263 }
264
265 boolean
266 bfd_elf_mkcorefile (abfd)
267      bfd * abfd;
268 {
269   /* I think this can be done just like an object file. */
270   return bfd_elf_mkobject (abfd);
271 }
272
273 char *
274 bfd_elf_get_str_section (abfd, shindex)
275      bfd * abfd;
276      unsigned int shindex;
277 {
278   Elf_Internal_Shdr **i_shdrp;
279   char *shstrtab = NULL;
280   unsigned int offset;
281   unsigned int shstrtabsize;
282
283   i_shdrp = elf_elfsections (abfd);
284   if (i_shdrp == 0 || i_shdrp[shindex] == 0)
285     return 0;
286
287   shstrtab = (char *) i_shdrp[shindex]->contents;
288   if (shstrtab == NULL)
289     {
290       /* No cached one, attempt to read, and cache what we read. */
291       offset = i_shdrp[shindex]->sh_offset;
292       shstrtabsize = i_shdrp[shindex]->sh_size;
293       shstrtab = elf_read (abfd, offset, shstrtabsize);
294       i_shdrp[shindex]->contents = (PTR) shstrtab;
295     }
296   return shstrtab;
297 }
298
299 char *
300 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
301      bfd * abfd;
302      unsigned int shindex;
303      unsigned int strindex;
304 {
305   Elf_Internal_Shdr *hdr;
306
307   if (strindex == 0)
308     return "";
309
310   hdr = elf_elfsections (abfd)[shindex];
311
312   if (hdr->contents == NULL
313       && bfd_elf_get_str_section (abfd, shindex) == NULL)
314     return NULL;
315
316   if (strindex >= hdr->sh_size)
317     {
318       (*_bfd_error_handler)
319         (_("%s: invalid string offset %u >= %lu for section `%s'"),
320          bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
321          ((shindex == elf_elfheader(abfd)->e_shstrndx
322            && strindex == hdr->sh_name)
323           ? ".shstrtab"
324           : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
325       return "";
326     }
327
328   return ((char *) hdr->contents) + strindex;
329 }
330
331 /* Make a BFD section from an ELF section.  We store a pointer to the
332    BFD section in the bfd_section field of the header.  */
333
334 boolean
335 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
336      bfd *abfd;
337      Elf_Internal_Shdr *hdr;
338      const char *name;
339 {
340   asection *newsect;
341   flagword flags;
342
343   if (hdr->bfd_section != NULL)
344     {
345       BFD_ASSERT (strcmp (name,
346                           bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
347       return true;
348     }
349
350   newsect = bfd_make_section_anyway (abfd, name);
351   if (newsect == NULL)
352     return false;
353
354   newsect->filepos = hdr->sh_offset;
355
356   if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
357       || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
358       || ! bfd_set_section_alignment (abfd, newsect,
359                                       bfd_log2 (hdr->sh_addralign)))
360     return false;
361
362   flags = SEC_NO_FLAGS;
363   if (hdr->sh_type != SHT_NOBITS)
364     flags |= SEC_HAS_CONTENTS;
365   if ((hdr->sh_flags & SHF_ALLOC) != 0)
366     {
367       flags |= SEC_ALLOC;
368       if (hdr->sh_type != SHT_NOBITS)
369         flags |= SEC_LOAD;
370     }
371   if ((hdr->sh_flags & SHF_WRITE) == 0)
372     flags |= SEC_READONLY;
373   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
374     flags |= SEC_CODE;
375   else if ((flags & SEC_LOAD) != 0)
376     flags |= SEC_DATA;
377
378   /* The debugging sections appear to be recognized only by name, not
379      any sort of flag.  */
380   if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
381       || strncmp (name, ".line", sizeof ".line" - 1) == 0
382       || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
383     flags |= SEC_DEBUGGING;
384
385   /* As a GNU extension, if the name begins with .gnu.linkonce, we
386      only link a single copy of the section.  This is used to support
387      g++.  g++ will emit each template expansion in its own section.
388      The symbols will be defined as weak, so that multiple definitions
389      are permitted.  The GNU linker extension is to actually discard
390      all but one of the sections.  */
391   if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
392     flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
393
394   if (! bfd_set_section_flags (abfd, newsect, flags))
395     return false;
396
397   if ((flags & SEC_ALLOC) != 0)
398     {
399       Elf_Internal_Phdr *phdr;
400       unsigned int i;
401
402       /* Look through the phdrs to see if we need to adjust the lma.
403          If all the p_paddr fields are zero, we ignore them, since
404          some ELF linkers produce such output.  */
405       phdr = elf_tdata (abfd)->phdr;
406       for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
407         {
408           if (phdr->p_paddr != 0)
409             break;
410         }
411       if (i < elf_elfheader (abfd)->e_phnum)
412         {
413           phdr = elf_tdata (abfd)->phdr;
414           for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
415             {
416               if (phdr->p_type == PT_LOAD
417                   && phdr->p_vaddr != phdr->p_paddr
418                   && phdr->p_vaddr <= hdr->sh_addr
419                   && (phdr->p_vaddr + phdr->p_memsz
420                       >= hdr->sh_addr + hdr->sh_size)
421                   && ((flags & SEC_LOAD) == 0
422                       || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
423                           && (phdr->p_offset + phdr->p_filesz
424                               >= hdr->sh_offset + hdr->sh_size))))
425                 {
426                   newsect->lma += phdr->p_paddr - phdr->p_vaddr;
427                   break;
428                 }
429             }
430         }
431     }
432
433   hdr->bfd_section = newsect;
434   elf_section_data (newsect)->this_hdr = *hdr;
435
436   return true;
437 }
438
439 /*
440 INTERNAL_FUNCTION
441         bfd_elf_find_section
442
443 SYNOPSIS
444         struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
445
446 DESCRIPTION
447         Helper functions for GDB to locate the string tables.
448         Since BFD hides string tables from callers, GDB needs to use an
449         internal hook to find them.  Sun's .stabstr, in particular,
450         isn't even pointed to by the .stab section, so ordinary
451         mechanisms wouldn't work to find it, even if we had some.
452 */
453
454 struct elf_internal_shdr *
455 bfd_elf_find_section (abfd, name)
456      bfd * abfd;
457      char *name;
458 {
459   Elf_Internal_Shdr **i_shdrp;
460   char *shstrtab;
461   unsigned int max;
462   unsigned int i;
463
464   i_shdrp = elf_elfsections (abfd);
465   if (i_shdrp != NULL)
466     {
467       shstrtab = bfd_elf_get_str_section
468         (abfd, elf_elfheader (abfd)->e_shstrndx);
469       if (shstrtab != NULL)
470         {
471           max = elf_elfheader (abfd)->e_shnum;
472           for (i = 1; i < max; i++)
473             if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
474               return i_shdrp[i];
475         }
476     }
477   return 0;
478 }
479
480 const char *const bfd_elf_section_type_names[] = {
481   "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
482   "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
483   "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
484 };
485
486 /* ELF relocs are against symbols.  If we are producing relocateable
487    output, and the reloc is against an external symbol, and nothing
488    has given us any additional addend, the resulting reloc will also
489    be against the same symbol.  In such a case, we don't want to
490    change anything about the way the reloc is handled, since it will
491    all be done at final link time.  Rather than put special case code
492    into bfd_perform_relocation, all the reloc types use this howto
493    function.  It just short circuits the reloc if producing
494    relocateable output against an external symbol.  */
495
496 /*ARGSUSED*/
497 bfd_reloc_status_type
498 bfd_elf_generic_reloc (abfd,
499                        reloc_entry,
500                        symbol,
501                        data,
502                        input_section,
503                        output_bfd,
504                        error_message)
505      bfd *abfd;
506      arelent *reloc_entry;
507      asymbol *symbol;
508      PTR data;
509      asection *input_section;
510      bfd *output_bfd;
511      char **error_message;
512 {
513   if (output_bfd != (bfd *) NULL
514       && (symbol->flags & BSF_SECTION_SYM) == 0
515       && (! reloc_entry->howto->partial_inplace
516           || reloc_entry->addend == 0))
517     {
518       reloc_entry->address += input_section->output_offset;
519       return bfd_reloc_ok;
520     }
521
522   return bfd_reloc_continue;
523 }
524 \f
525 /* Print out the program headers.  */
526
527 boolean
528 _bfd_elf_print_private_bfd_data (abfd, farg)
529      bfd *abfd;
530      PTR farg;
531 {
532   FILE *f = (FILE *) farg;
533   Elf_Internal_Phdr *p;
534   asection *s;
535   bfd_byte *dynbuf = NULL;
536
537   p = elf_tdata (abfd)->phdr;
538   if (p != NULL)
539     {
540       unsigned int i, c;
541
542       fprintf (f, _("\nProgram Header:\n"));
543       c = elf_elfheader (abfd)->e_phnum;
544       for (i = 0; i < c; i++, p++)
545         {
546           const char *s;
547           char buf[20];
548
549           switch (p->p_type)
550             {
551             case PT_NULL: s = "NULL"; break;
552             case PT_LOAD: s = "LOAD"; break;
553             case PT_DYNAMIC: s = "DYNAMIC"; break;
554             case PT_INTERP: s = "INTERP"; break;
555             case PT_NOTE: s = "NOTE"; break;
556             case PT_SHLIB: s = "SHLIB"; break;
557             case PT_PHDR: s = "PHDR"; break;
558             default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
559             }
560           fprintf (f, "%8s off    0x", s);
561           fprintf_vma (f, p->p_offset);
562           fprintf (f, " vaddr 0x");
563           fprintf_vma (f, p->p_vaddr);
564           fprintf (f, " paddr 0x");
565           fprintf_vma (f, p->p_paddr);
566           fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
567           fprintf (f, "         filesz 0x");
568           fprintf_vma (f, p->p_filesz);
569           fprintf (f, " memsz 0x");
570           fprintf_vma (f, p->p_memsz);
571           fprintf (f, " flags %c%c%c",
572                    (p->p_flags & PF_R) != 0 ? 'r' : '-',
573                    (p->p_flags & PF_W) != 0 ? 'w' : '-',
574                    (p->p_flags & PF_X) != 0 ? 'x' : '-');
575           if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
576             fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
577           fprintf (f, "\n");
578         }
579     }
580
581   s = bfd_get_section_by_name (abfd, ".dynamic");
582   if (s != NULL)
583     {
584       int elfsec;
585       unsigned long link;
586       bfd_byte *extdyn, *extdynend;
587       size_t extdynsize;
588       void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
589
590       fprintf (f, _("\nDynamic Section:\n"));
591
592       dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
593       if (dynbuf == NULL)
594         goto error_return;
595       if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
596                                       s->_raw_size))
597         goto error_return;
598
599       elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
600       if (elfsec == -1)
601         goto error_return;
602       link = elf_elfsections (abfd)[elfsec]->sh_link;
603
604       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
605       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
606
607       extdyn = dynbuf;
608       extdynend = extdyn + s->_raw_size;
609       for (; extdyn < extdynend; extdyn += extdynsize)
610         {
611           Elf_Internal_Dyn dyn;
612           const char *name;
613           char ab[20];
614           boolean stringp;
615
616           (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
617
618           if (dyn.d_tag == DT_NULL)
619             break;
620
621           stringp = false;
622           switch (dyn.d_tag)
623             {
624             default:
625               sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
626               name = ab;
627               break;
628
629             case DT_NEEDED: name = "NEEDED"; stringp = true; break;
630             case DT_PLTRELSZ: name = "PLTRELSZ"; break;
631             case DT_PLTGOT: name = "PLTGOT"; break;
632             case DT_HASH: name = "HASH"; break;
633             case DT_STRTAB: name = "STRTAB"; break;
634             case DT_SYMTAB: name = "SYMTAB"; break;
635             case DT_RELA: name = "RELA"; break;
636             case DT_RELASZ: name = "RELASZ"; break;
637             case DT_RELAENT: name = "RELAENT"; break;
638             case DT_STRSZ: name = "STRSZ"; break;
639             case DT_SYMENT: name = "SYMENT"; break;
640             case DT_INIT: name = "INIT"; break;
641             case DT_FINI: name = "FINI"; break;
642             case DT_SONAME: name = "SONAME"; stringp = true; break;
643             case DT_RPATH: name = "RPATH"; stringp = true; break;
644             case DT_SYMBOLIC: name = "SYMBOLIC"; break;
645             case DT_REL: name = "REL"; break;
646             case DT_RELSZ: name = "RELSZ"; break;
647             case DT_RELENT: name = "RELENT"; break;
648             case DT_PLTREL: name = "PLTREL"; break;
649             case DT_DEBUG: name = "DEBUG"; break;
650             case DT_TEXTREL: name = "TEXTREL"; break;
651             case DT_JMPREL: name = "JMPREL"; break;
652             case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
653             case DT_FILTER: name = "FILTER"; stringp = true; break;
654             case DT_VERSYM: name = "VERSYM"; break;
655             case DT_VERDEF: name = "VERDEF"; break;
656             case DT_VERDEFNUM: name = "VERDEFNUM"; break;
657             case DT_VERNEED: name = "VERNEED"; break;
658             case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
659             }
660
661           fprintf (f, "  %-11s ", name);
662           if (! stringp)
663             fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
664           else
665             {
666               const char *string;
667
668               string = bfd_elf_string_from_elf_section (abfd, link,
669                                                         dyn.d_un.d_val);
670               if (string == NULL)
671                 goto error_return;
672               fprintf (f, "%s", string);
673             }
674           fprintf (f, "\n");
675         }
676
677       free (dynbuf);
678       dynbuf = NULL;
679     }
680
681   if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
682       || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
683     {
684       if (! _bfd_elf_slurp_version_tables (abfd))
685         return false;
686     }
687
688   if (elf_dynverdef (abfd) != 0)
689     {
690       Elf_Internal_Verdef *t;
691
692       fprintf (f, _("\nVersion definitions:\n"));
693       for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
694         {
695           fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
696                    t->vd_flags, t->vd_hash, t->vd_nodename);
697           if (t->vd_auxptr->vda_nextptr != NULL)
698             {
699               Elf_Internal_Verdaux *a;
700
701               fprintf (f, "\t");
702               for (a = t->vd_auxptr->vda_nextptr;
703                    a != NULL;
704                    a = a->vda_nextptr)
705                 fprintf (f, "%s ", a->vda_nodename);
706               fprintf (f, "\n");
707             }
708         }
709     }
710
711   if (elf_dynverref (abfd) != 0)
712     {
713       Elf_Internal_Verneed *t;
714
715       fprintf (f, _("\nVersion References:\n"));
716       for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
717         {
718           Elf_Internal_Vernaux *a;
719
720           fprintf (f, _("  required from %s:\n"), t->vn_filename);
721           for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
722             fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
723                      a->vna_flags, a->vna_other, a->vna_nodename);
724         }
725     }
726
727   return true;
728
729  error_return:
730   if (dynbuf != NULL)
731     free (dynbuf);
732   return false;
733 }
734
735 /* Display ELF-specific fields of a symbol.  */
736
737 void
738 bfd_elf_print_symbol (abfd, filep, symbol, how)
739      bfd *abfd;
740      PTR filep;
741      asymbol *symbol;
742      bfd_print_symbol_type how;
743 {
744   FILE *file = (FILE *) filep;
745   switch (how)
746     {
747     case bfd_print_symbol_name:
748       fprintf (file, "%s", symbol->name);
749       break;
750     case bfd_print_symbol_more:
751       fprintf (file, "elf ");
752       fprintf_vma (file, symbol->value);
753       fprintf (file, " %lx", (long) symbol->flags);
754       break;
755     case bfd_print_symbol_all:
756       {
757         CONST char *section_name;
758         section_name = symbol->section ? symbol->section->name : "(*none*)";
759         bfd_print_symbol_vandf ((PTR) file, symbol);
760         fprintf (file, " %s\t", section_name);
761         /* Print the "other" value for a symbol.  For common symbols,
762            we've already printed the size; now print the alignment.
763            For other symbols, we have no specified alignment, and
764            we've printed the address; now print the size.  */
765         fprintf_vma (file,
766                      (bfd_is_com_section (symbol->section)
767                       ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
768                       : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
769
770         /* If we have version information, print it.  */
771         if (elf_tdata (abfd)->dynversym_section != 0
772             && (elf_tdata (abfd)->dynverdef_section != 0
773                 || elf_tdata (abfd)->dynverref_section != 0))
774           {
775             unsigned int vernum;
776             const char *version_string;
777
778             vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
779
780             if (vernum == 0)
781               version_string = "";
782             else if (vernum == 1)
783               version_string = "Base";
784             else if (vernum <= elf_tdata (abfd)->cverdefs)
785               version_string =
786                 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
787             else
788               {
789                 Elf_Internal_Verneed *t;
790
791                 version_string = "";
792                 for (t = elf_tdata (abfd)->verref;
793                      t != NULL;
794                      t = t->vn_nextref)
795                   {
796                     Elf_Internal_Vernaux *a;
797
798                     for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
799                       {
800                         if (a->vna_other == vernum)
801                           {
802                             version_string = a->vna_nodename;
803                             break;
804                           }
805                       }
806                   }
807               }
808
809             if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
810               fprintf (file, "  %-11s", version_string);
811             else
812               {
813                 int i;
814
815                 fprintf (file, " (%s)", version_string);
816                 for (i = 10 - strlen (version_string); i > 0; --i)
817                   putc (' ', file);
818               }
819           }
820
821         /* If the st_other field is not zero, print it.  */
822         if (((elf_symbol_type *) symbol)->internal_elf_sym.st_other != 0)
823           fprintf (file, " 0x%02x",
824                    ((unsigned int)
825                     ((elf_symbol_type *) symbol)->internal_elf_sym.st_other));
826
827         fprintf (file, " %s", symbol->name);
828       }
829       break;
830     }
831 }
832 \f
833 /* Create an entry in an ELF linker hash table.  */
834
835 struct bfd_hash_entry *
836 _bfd_elf_link_hash_newfunc (entry, table, string)
837      struct bfd_hash_entry *entry;
838      struct bfd_hash_table *table;
839      const char *string;
840 {
841   struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
842
843   /* Allocate the structure if it has not already been allocated by a
844      subclass.  */
845   if (ret == (struct elf_link_hash_entry *) NULL)
846     ret = ((struct elf_link_hash_entry *)
847            bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
848   if (ret == (struct elf_link_hash_entry *) NULL)
849     return (struct bfd_hash_entry *) ret;
850
851   /* Call the allocation method of the superclass.  */
852   ret = ((struct elf_link_hash_entry *)
853          _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
854                                  table, string));
855   if (ret != (struct elf_link_hash_entry *) NULL)
856     {
857       /* Set local fields.  */
858       ret->indx = -1;
859       ret->size = 0;
860       ret->dynindx = -1;
861       ret->dynstr_index = 0;
862       ret->weakdef = NULL;
863       ret->got.offset = (bfd_vma) -1;
864       ret->plt.offset = (bfd_vma) -1;
865       ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
866       ret->verinfo.verdef = NULL;
867       ret->vtable_entries_used = NULL;
868       ret->vtable_parent = NULL;
869       ret->type = STT_NOTYPE;
870       ret->other = 0;
871       /* Assume that we have been called by a non-ELF symbol reader.
872          This flag is then reset by the code which reads an ELF input
873          file.  This ensures that a symbol created by a non-ELF symbol
874          reader will have the flag set correctly.  */
875       ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
876     }
877
878   return (struct bfd_hash_entry *) ret;
879 }
880
881 /* Initialize an ELF linker hash table.  */
882
883 boolean
884 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
885      struct elf_link_hash_table *table;
886      bfd *abfd;
887      struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
888                                                 struct bfd_hash_table *,
889                                                 const char *));
890 {
891   table->dynamic_sections_created = false;
892   table->dynobj = NULL;
893   /* The first dynamic symbol is a dummy.  */
894   table->dynsymcount = 1;
895   table->dynstr = NULL;
896   table->bucketcount = 0;
897   table->needed = NULL;
898   table->hgot = NULL;
899   table->stab_info = NULL;
900   return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
901 }
902
903 /* Create an ELF linker hash table.  */
904
905 struct bfd_link_hash_table *
906 _bfd_elf_link_hash_table_create (abfd)
907      bfd *abfd;
908 {
909   struct elf_link_hash_table *ret;
910
911   ret = ((struct elf_link_hash_table *)
912          bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
913   if (ret == (struct elf_link_hash_table *) NULL)
914     return NULL;
915
916   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
917     {
918       bfd_release (abfd, ret);
919       return NULL;
920     }
921
922   return &ret->root;
923 }
924
925 /* This is a hook for the ELF emulation code in the generic linker to
926    tell the backend linker what file name to use for the DT_NEEDED
927    entry for a dynamic object.  The generic linker passes name as an
928    empty string to indicate that no DT_NEEDED entry should be made.  */
929
930 void
931 bfd_elf_set_dt_needed_name (abfd, name)
932      bfd *abfd;
933      const char *name;
934 {
935   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
936       && bfd_get_format (abfd) == bfd_object)
937     elf_dt_name (abfd) = name;
938 }
939
940 /* Get the list of DT_NEEDED entries for a link.  This is a hook for
941    the linker ELF emulation code.  */
942
943 struct bfd_link_needed_list *
944 bfd_elf_get_needed_list (abfd, info)
945      bfd *abfd;
946      struct bfd_link_info *info;
947 {
948   if (info->hash->creator->flavour != bfd_target_elf_flavour)
949     return NULL;
950   return elf_hash_table (info)->needed;
951 }
952
953 /* Get the name actually used for a dynamic object for a link.  This
954    is the SONAME entry if there is one.  Otherwise, it is the string
955    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
956
957 const char *
958 bfd_elf_get_dt_soname (abfd)
959      bfd *abfd;
960 {
961   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
962       && bfd_get_format (abfd) == bfd_object)
963     return elf_dt_name (abfd);
964   return NULL;
965 }
966
967 /* Get the list of DT_NEEDED entries from a BFD.  This is a hook for
968    the ELF linker emulation code.  */
969
970 boolean
971 bfd_elf_get_bfd_needed_list (abfd, pneeded)
972      bfd *abfd;
973      struct bfd_link_needed_list **pneeded;
974 {
975   asection *s;
976   bfd_byte *dynbuf = NULL;
977   int elfsec;
978   unsigned long link;
979   bfd_byte *extdyn, *extdynend;
980   size_t extdynsize;
981   void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
982
983   *pneeded = NULL;
984
985   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
986       || bfd_get_format (abfd) != bfd_object)
987     return true;
988
989   s = bfd_get_section_by_name (abfd, ".dynamic");
990   if (s == NULL || s->_raw_size == 0)
991     return true;
992
993   dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
994   if (dynbuf == NULL)
995     goto error_return;
996
997   if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
998                                   s->_raw_size))
999     goto error_return;
1000
1001   elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1002   if (elfsec == -1)
1003     goto error_return;
1004
1005   link = elf_elfsections (abfd)[elfsec]->sh_link;
1006
1007   extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1008   swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1009
1010   extdyn = dynbuf;
1011   extdynend = extdyn + s->_raw_size;
1012   for (; extdyn < extdynend; extdyn += extdynsize)
1013     {
1014       Elf_Internal_Dyn dyn;
1015
1016       (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1017
1018       if (dyn.d_tag == DT_NULL)
1019         break;
1020
1021       if (dyn.d_tag == DT_NEEDED)
1022         {
1023           const char *string;
1024           struct bfd_link_needed_list *l;
1025
1026           string = bfd_elf_string_from_elf_section (abfd, link,
1027                                                     dyn.d_un.d_val);
1028           if (string == NULL)
1029             goto error_return;
1030
1031           l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1032           if (l == NULL)
1033             goto error_return;
1034
1035           l->by = abfd;
1036           l->name = string;
1037           l->next = *pneeded;
1038           *pneeded = l;
1039         }
1040     }
1041
1042   free (dynbuf);
1043
1044   return true;
1045
1046  error_return:
1047   if (dynbuf != NULL)
1048     free (dynbuf);
1049   return false;
1050 }
1051 \f
1052 /* Allocate an ELF string table--force the first byte to be zero.  */
1053
1054 struct bfd_strtab_hash *
1055 _bfd_elf_stringtab_init ()
1056 {
1057   struct bfd_strtab_hash *ret;
1058
1059   ret = _bfd_stringtab_init ();
1060   if (ret != NULL)
1061     {
1062       bfd_size_type loc;
1063
1064       loc = _bfd_stringtab_add (ret, "", true, false);
1065       BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1066       if (loc == (bfd_size_type) -1)
1067         {
1068           _bfd_stringtab_free (ret);
1069           ret = NULL;
1070         }
1071     }
1072   return ret;
1073 }
1074 \f
1075 /* ELF .o/exec file reading */
1076
1077 /* Create a new bfd section from an ELF section header. */
1078
1079 boolean
1080 bfd_section_from_shdr (abfd, shindex)
1081      bfd *abfd;
1082      unsigned int shindex;
1083 {
1084   Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1085   Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1086   struct elf_backend_data *bed = get_elf_backend_data (abfd);
1087   char *name;
1088
1089   name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1090
1091   switch (hdr->sh_type)
1092     {
1093     case SHT_NULL:
1094       /* Inactive section. Throw it away.  */
1095       return true;
1096
1097     case SHT_PROGBITS:  /* Normal section with contents.  */
1098     case SHT_DYNAMIC:   /* Dynamic linking information.  */
1099     case SHT_NOBITS:    /* .bss section.  */
1100     case SHT_HASH:      /* .hash section.  */
1101     case SHT_NOTE:      /* .note section.  */
1102       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1103
1104     case SHT_SYMTAB:            /* A symbol table */
1105       if (elf_onesymtab (abfd) == shindex)
1106         return true;
1107
1108       BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1109       BFD_ASSERT (elf_onesymtab (abfd) == 0);
1110       elf_onesymtab (abfd) = shindex;
1111       elf_tdata (abfd)->symtab_hdr = *hdr;
1112       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1113       abfd->flags |= HAS_SYMS;
1114
1115       /* Sometimes a shared object will map in the symbol table.  If
1116          SHF_ALLOC is set, and this is a shared object, then we also
1117          treat this section as a BFD section.  We can not base the
1118          decision purely on SHF_ALLOC, because that flag is sometimes
1119          set in a relocateable object file, which would confuse the
1120          linker.  */
1121       if ((hdr->sh_flags & SHF_ALLOC) != 0
1122           && (abfd->flags & DYNAMIC) != 0
1123           && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1124         return false;
1125
1126       return true;
1127
1128     case SHT_DYNSYM:            /* A dynamic symbol table */
1129       if (elf_dynsymtab (abfd) == shindex)
1130         return true;
1131
1132       BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1133       BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1134       elf_dynsymtab (abfd) = shindex;
1135       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1136       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1137       abfd->flags |= HAS_SYMS;
1138
1139       /* Besides being a symbol table, we also treat this as a regular
1140          section, so that objcopy can handle it.  */
1141       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1142
1143     case SHT_STRTAB:            /* A string table */
1144       if (hdr->bfd_section != NULL)
1145         return true;
1146       if (ehdr->e_shstrndx == shindex)
1147         {
1148           elf_tdata (abfd)->shstrtab_hdr = *hdr;
1149           elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1150           return true;
1151         }
1152       {
1153         unsigned int i;
1154
1155         for (i = 1; i < ehdr->e_shnum; i++)
1156           {
1157             Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1158             if (hdr2->sh_link == shindex)
1159               {
1160                 if (! bfd_section_from_shdr (abfd, i))
1161                   return false;
1162                 if (elf_onesymtab (abfd) == i)
1163                   {
1164                     elf_tdata (abfd)->strtab_hdr = *hdr;
1165                     elf_elfsections (abfd)[shindex] =
1166                       &elf_tdata (abfd)->strtab_hdr;
1167                     return true;
1168                   }
1169                 if (elf_dynsymtab (abfd) == i)
1170                   {
1171                     elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1172                     elf_elfsections (abfd)[shindex] = hdr =
1173                       &elf_tdata (abfd)->dynstrtab_hdr;
1174                     /* We also treat this as a regular section, so
1175                        that objcopy can handle it.  */
1176                     break;
1177                   }
1178 #if 0 /* Not handling other string tables specially right now.  */
1179                 hdr2 = elf_elfsections (abfd)[i];       /* in case it moved */
1180                 /* We have a strtab for some random other section.  */
1181                 newsect = (asection *) hdr2->bfd_section;
1182                 if (!newsect)
1183                   break;
1184                 hdr->bfd_section = newsect;
1185                 hdr2 = &elf_section_data (newsect)->str_hdr;
1186                 *hdr2 = *hdr;
1187                 elf_elfsections (abfd)[shindex] = hdr2;
1188 #endif
1189               }
1190           }
1191       }
1192
1193       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1194
1195     case SHT_REL:
1196     case SHT_RELA:
1197       /* *These* do a lot of work -- but build no sections!  */
1198       {
1199         asection *target_sect;
1200         Elf_Internal_Shdr *hdr2;
1201
1202         /* For some incomprehensible reason Oracle distributes
1203            libraries for Solaris in which some of the objects have
1204            bogus sh_link fields.  It would be nice if we could just
1205            reject them, but, unfortunately, some people need to use
1206            them.  We scan through the section headers; if we find only
1207            one suitable symbol table, we clobber the sh_link to point
1208            to it.  I hope this doesn't break anything.  */
1209         if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1210             && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1211           {
1212             int scan;
1213             int found;
1214
1215             found = 0;
1216             for (scan = 1; scan < ehdr->e_shnum; scan++)
1217               {
1218                 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1219                     || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1220                   {
1221                     if (found != 0)
1222                       {
1223                         found = 0;
1224                         break;
1225                       }
1226                     found = scan;
1227                   }
1228               }
1229             if (found != 0)
1230               hdr->sh_link = found;
1231           }
1232
1233         /* Get the symbol table.  */
1234         if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1235             && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1236           return false;
1237
1238         /* If this reloc section does not use the main symbol table we
1239            don't treat it as a reloc section.  BFD can't adequately
1240            represent such a section, so at least for now, we don't
1241            try.  We just present it as a normal section.  */
1242         if (hdr->sh_link != elf_onesymtab (abfd))
1243           return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1244
1245         if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1246           return false;
1247         target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1248         if (target_sect == NULL)
1249           return false;
1250
1251         if ((target_sect->flags & SEC_RELOC) == 0
1252             || target_sect->reloc_count == 0)
1253           hdr2 = &elf_section_data (target_sect)->rel_hdr;
1254         else
1255           {
1256             BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1257             hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1258             elf_section_data (target_sect)->rel_hdr2 = hdr2;
1259           }
1260         *hdr2 = *hdr;
1261         elf_elfsections (abfd)[shindex] = hdr2;
1262         target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1263         target_sect->flags |= SEC_RELOC;
1264         target_sect->relocation = NULL;
1265         target_sect->rel_filepos = hdr->sh_offset;
1266         abfd->flags |= HAS_RELOC;
1267         return true;
1268       }
1269       break;
1270
1271     case SHT_GNU_verdef:
1272       elf_dynverdef (abfd) = shindex;
1273       elf_tdata (abfd)->dynverdef_hdr = *hdr;
1274       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1275       break;
1276
1277     case SHT_GNU_versym:
1278       elf_dynversym (abfd) = shindex;
1279       elf_tdata (abfd)->dynversym_hdr = *hdr;
1280       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1281       break;
1282
1283     case SHT_GNU_verneed:
1284       elf_dynverref (abfd) = shindex;
1285       elf_tdata (abfd)->dynverref_hdr = *hdr;
1286       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1287       break;
1288
1289     case SHT_SHLIB:
1290       return true;
1291
1292     default:
1293       /* Check for any processor-specific section types.  */
1294       {
1295         if (bed->elf_backend_section_from_shdr)
1296           (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1297       }
1298       break;
1299     }
1300
1301   return true;
1302 }
1303
1304 /* Given an ELF section number, retrieve the corresponding BFD
1305    section.  */
1306
1307 asection *
1308 bfd_section_from_elf_index (abfd, index)
1309      bfd *abfd;
1310      unsigned int index;
1311 {
1312   BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1313   if (index >= elf_elfheader (abfd)->e_shnum)
1314     return NULL;
1315   return elf_elfsections (abfd)[index]->bfd_section;
1316 }
1317
1318 boolean
1319 _bfd_elf_new_section_hook (abfd, sec)
1320      bfd *abfd;
1321      asection *sec;
1322 {
1323   struct bfd_elf_section_data *sdata;
1324
1325   sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
1326   if (!sdata)
1327     return false;
1328   sec->used_by_bfd = (PTR) sdata;
1329   memset (sdata, 0, sizeof (*sdata));
1330   return true;
1331 }
1332
1333 /* Create a new bfd section from an ELF program header.
1334
1335    Since program segments have no names, we generate a synthetic name
1336    of the form segment<NUM>, where NUM is generally the index in the
1337    program header table.  For segments that are split (see below) we
1338    generate the names segment<NUM>a and segment<NUM>b.
1339
1340    Note that some program segments may have a file size that is different than
1341    (less than) the memory size.  All this means is that at execution the
1342    system must allocate the amount of memory specified by the memory size,
1343    but only initialize it with the first "file size" bytes read from the
1344    file.  This would occur for example, with program segments consisting
1345    of combined data+bss.
1346
1347    To handle the above situation, this routine generates TWO bfd sections
1348    for the single program segment.  The first has the length specified by
1349    the file size of the segment, and the second has the length specified
1350    by the difference between the two sizes.  In effect, the segment is split
1351    into it's initialized and uninitialized parts.
1352
1353  */
1354
1355 boolean
1356 bfd_section_from_phdr (abfd, hdr, index)
1357      bfd *abfd;
1358      Elf_Internal_Phdr *hdr;
1359      int index;
1360 {
1361   asection *newsect;
1362   char *name;
1363   char namebuf[64];
1364   int split;
1365
1366   split = ((hdr->p_memsz > 0) &&
1367            (hdr->p_filesz > 0) &&
1368            (hdr->p_memsz > hdr->p_filesz));
1369   sprintf (namebuf, split ? "segment%da" : "segment%d", index);
1370   name = bfd_alloc (abfd, strlen (namebuf) + 1);
1371   if (!name)
1372     return false;
1373   strcpy (name, namebuf);
1374   newsect = bfd_make_section (abfd, name);
1375   if (newsect == NULL)
1376     return false;
1377   newsect->vma = hdr->p_vaddr;
1378   newsect->lma = hdr->p_paddr;
1379   newsect->_raw_size = hdr->p_filesz;
1380   newsect->filepos = hdr->p_offset;
1381   newsect->flags |= SEC_HAS_CONTENTS;
1382   if (hdr->p_type == PT_LOAD)
1383     {
1384       newsect->flags |= SEC_ALLOC;
1385       newsect->flags |= SEC_LOAD;
1386       if (hdr->p_flags & PF_X)
1387         {
1388           /* FIXME: all we known is that it has execute PERMISSION,
1389              may be data. */
1390           newsect->flags |= SEC_CODE;
1391         }
1392     }
1393   if (!(hdr->p_flags & PF_W))
1394     {
1395       newsect->flags |= SEC_READONLY;
1396     }
1397
1398   if (split)
1399     {
1400       sprintf (namebuf, "segment%db", index);
1401       name = bfd_alloc (abfd, strlen (namebuf) + 1);
1402       if (!name)
1403         return false;
1404       strcpy (name, namebuf);
1405       newsect = bfd_make_section (abfd, name);
1406       if (newsect == NULL)
1407         return false;
1408       newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1409       newsect->lma = hdr->p_paddr + hdr->p_filesz;
1410       newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1411       if (hdr->p_type == PT_LOAD)
1412         {
1413           newsect->flags |= SEC_ALLOC;
1414           if (hdr->p_flags & PF_X)
1415             newsect->flags |= SEC_CODE;
1416         }
1417       if (!(hdr->p_flags & PF_W))
1418         newsect->flags |= SEC_READONLY;
1419     }
1420
1421   return true;
1422 }
1423
1424 /* Set up an ELF internal section header for a section.  */
1425
1426 /*ARGSUSED*/
1427 static void
1428 elf_fake_sections (abfd, asect, failedptrarg)
1429      bfd *abfd;
1430      asection *asect;
1431      PTR failedptrarg;
1432 {
1433   struct elf_backend_data *bed = get_elf_backend_data (abfd);
1434   boolean *failedptr = (boolean *) failedptrarg;
1435   Elf_Internal_Shdr *this_hdr;
1436
1437   if (*failedptr)
1438     {
1439       /* We already failed; just get out of the bfd_map_over_sections
1440          loop.  */
1441       return;
1442     }
1443
1444   this_hdr = &elf_section_data (asect)->this_hdr;
1445
1446   this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1447                                                           asect->name,
1448                                                           true, false);
1449   if (this_hdr->sh_name == (unsigned long) -1)
1450     {
1451       *failedptr = true;
1452       return;
1453     }
1454
1455   this_hdr->sh_flags = 0;
1456
1457   if ((asect->flags & SEC_ALLOC) != 0
1458       || asect->user_set_vma)
1459     this_hdr->sh_addr = asect->vma;
1460   else
1461     this_hdr->sh_addr = 0;
1462
1463   this_hdr->sh_offset = 0;
1464   this_hdr->sh_size = asect->_raw_size;
1465   this_hdr->sh_link = 0;
1466   this_hdr->sh_addralign = 1 << asect->alignment_power;
1467   /* The sh_entsize and sh_info fields may have been set already by
1468      copy_private_section_data.  */
1469
1470   this_hdr->bfd_section = asect;
1471   this_hdr->contents = NULL;
1472
1473   /* FIXME: This should not be based on section names.  */
1474   if (strcmp (asect->name, ".dynstr") == 0)
1475     this_hdr->sh_type = SHT_STRTAB;
1476   else if (strcmp (asect->name, ".hash") == 0)
1477     {
1478       this_hdr->sh_type = SHT_HASH;
1479       this_hdr->sh_entsize = bed->s->arch_size / 8;
1480     }
1481   else if (strcmp (asect->name, ".dynsym") == 0)
1482     {
1483       this_hdr->sh_type = SHT_DYNSYM;
1484       this_hdr->sh_entsize = bed->s->sizeof_sym;
1485     }
1486   else if (strcmp (asect->name, ".dynamic") == 0)
1487     {
1488       this_hdr->sh_type = SHT_DYNAMIC;
1489       this_hdr->sh_entsize = bed->s->sizeof_dyn;
1490     }
1491   else if (strncmp (asect->name, ".rela", 5) == 0
1492            && get_elf_backend_data (abfd)->use_rela_p)
1493     {
1494       this_hdr->sh_type = SHT_RELA;
1495       this_hdr->sh_entsize = bed->s->sizeof_rela;
1496     }
1497   else if (strncmp (asect->name, ".rel", 4) == 0
1498            && ! get_elf_backend_data (abfd)->use_rela_p)
1499     {
1500       this_hdr->sh_type = SHT_REL;
1501       this_hdr->sh_entsize = bed->s->sizeof_rel;
1502     }
1503   else if (strncmp (asect->name, ".note", 5) == 0)
1504     this_hdr->sh_type = SHT_NOTE;
1505   else if (strncmp (asect->name, ".stab", 5) == 0
1506            && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1507     this_hdr->sh_type = SHT_STRTAB;
1508   else if (strcmp (asect->name, ".gnu.version") == 0)
1509     {
1510       this_hdr->sh_type = SHT_GNU_versym;
1511       this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1512     }
1513   else if (strcmp (asect->name, ".gnu.version_d") == 0)
1514     {
1515       this_hdr->sh_type = SHT_GNU_verdef;
1516       this_hdr->sh_entsize = 0;
1517       /* objcopy or strip will copy over sh_info, but may not set
1518          cverdefs.  The linker will set cverdefs, but sh_info will be
1519          zero.  */
1520       if (this_hdr->sh_info == 0)
1521         this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1522       else
1523         BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1524                     || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1525     }
1526   else if (strcmp (asect->name, ".gnu.version_r") == 0)
1527     {
1528       this_hdr->sh_type = SHT_GNU_verneed;
1529       this_hdr->sh_entsize = 0;
1530       /* objcopy or strip will copy over sh_info, but may not set
1531          cverrefs.  The linker will set cverrefs, but sh_info will be
1532          zero.  */
1533       if (this_hdr->sh_info == 0)
1534         this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1535       else
1536         BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1537                     || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1538     }
1539   else if ((asect->flags & SEC_ALLOC) != 0
1540            && (asect->flags & SEC_LOAD) != 0)
1541     this_hdr->sh_type = SHT_PROGBITS;
1542   else if ((asect->flags & SEC_ALLOC) != 0
1543            && ((asect->flags & SEC_LOAD) == 0))
1544     this_hdr->sh_type = SHT_NOBITS;
1545   else
1546     {
1547       /* Who knows?  */
1548       this_hdr->sh_type = SHT_PROGBITS;
1549     }
1550
1551   if ((asect->flags & SEC_ALLOC) != 0)
1552     this_hdr->sh_flags |= SHF_ALLOC;
1553   if ((asect->flags & SEC_READONLY) == 0)
1554     this_hdr->sh_flags |= SHF_WRITE;
1555   if ((asect->flags & SEC_CODE) != 0)
1556     this_hdr->sh_flags |= SHF_EXECINSTR;
1557
1558   /* Check for processor-specific section types.  */
1559   {
1560     struct elf_backend_data *bed = get_elf_backend_data (abfd);
1561
1562     if (bed->elf_backend_fake_sections)
1563       (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1564   }
1565
1566   /* If the section has relocs, set up a section header for the
1567      SHT_REL[A] section.  */
1568   if ((asect->flags & SEC_RELOC) != 0)
1569     {
1570       Elf_Internal_Shdr *rela_hdr;
1571       int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1572       char *name;
1573
1574       rela_hdr = &elf_section_data (asect)->rel_hdr;
1575       name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1576       if (name == NULL)
1577         {
1578           *failedptr = true;
1579           return;
1580         }
1581       sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1582       rela_hdr->sh_name =
1583         (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1584                                            true, false);
1585       if (rela_hdr->sh_name == (unsigned int) -1)
1586         {
1587           *failedptr = true;
1588           return;
1589         }
1590       rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1591       rela_hdr->sh_entsize = (use_rela_p
1592                               ? bed->s->sizeof_rela
1593                               : bed->s->sizeof_rel);
1594       rela_hdr->sh_addralign = bed->s->file_align;
1595       rela_hdr->sh_flags = 0;
1596       rela_hdr->sh_addr = 0;
1597       rela_hdr->sh_size = 0;
1598       rela_hdr->sh_offset = 0;
1599     }
1600 }
1601
1602 /* Assign all ELF section numbers.  The dummy first section is handled here
1603    too.  The link/info pointers for the standard section types are filled
1604    in here too, while we're at it.  */
1605
1606 static boolean
1607 assign_section_numbers (abfd)
1608      bfd *abfd;
1609 {
1610   struct elf_obj_tdata *t = elf_tdata (abfd);
1611   asection *sec;
1612   unsigned int section_number;
1613   Elf_Internal_Shdr **i_shdrp;
1614   struct elf_backend_data *bed = get_elf_backend_data (abfd);
1615
1616   section_number = 1;
1617
1618   for (sec = abfd->sections; sec; sec = sec->next)
1619     {
1620       struct bfd_elf_section_data *d = elf_section_data (sec);
1621
1622       d->this_idx = section_number++;
1623       if ((sec->flags & SEC_RELOC) == 0)
1624         d->rel_idx = 0;
1625       else
1626         d->rel_idx = section_number++;
1627     }
1628
1629   t->shstrtab_section = section_number++;
1630   elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1631   t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1632
1633   if (abfd->symcount > 0)
1634     {
1635       t->symtab_section = section_number++;
1636       t->strtab_section = section_number++;
1637     }
1638
1639   elf_elfheader (abfd)->e_shnum = section_number;
1640
1641   /* Set up the list of section header pointers, in agreement with the
1642      indices.  */
1643   i_shdrp = ((Elf_Internal_Shdr **)
1644              bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1645   if (i_shdrp == NULL)
1646     return false;
1647
1648   i_shdrp[0] = ((Elf_Internal_Shdr *)
1649                 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1650   if (i_shdrp[0] == NULL)
1651     {
1652       bfd_release (abfd, i_shdrp);
1653       return false;
1654     }
1655   memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1656
1657   elf_elfsections (abfd) = i_shdrp;
1658
1659   i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1660   if (abfd->symcount > 0)
1661     {
1662       i_shdrp[t->symtab_section] = &t->symtab_hdr;
1663       i_shdrp[t->strtab_section] = &t->strtab_hdr;
1664       t->symtab_hdr.sh_link = t->strtab_section;
1665     }
1666   for (sec = abfd->sections; sec; sec = sec->next)
1667     {
1668       struct bfd_elf_section_data *d = elf_section_data (sec);
1669       asection *s;
1670       const char *name;
1671
1672       i_shdrp[d->this_idx] = &d->this_hdr;
1673       if (d->rel_idx != 0)
1674         i_shdrp[d->rel_idx] = &d->rel_hdr;
1675
1676       /* Fill in the sh_link and sh_info fields while we're at it.  */
1677
1678       /* sh_link of a reloc section is the section index of the symbol
1679          table.  sh_info is the section index of the section to which
1680          the relocation entries apply.  */
1681       if (d->rel_idx != 0)
1682         {
1683           d->rel_hdr.sh_link = t->symtab_section;
1684           d->rel_hdr.sh_info = d->this_idx;
1685         }
1686
1687       switch (d->this_hdr.sh_type)
1688         {
1689         case SHT_REL:
1690         case SHT_RELA:
1691           /* A reloc section which we are treating as a normal BFD
1692              section.  sh_link is the section index of the symbol
1693              table.  sh_info is the section index of the section to
1694              which the relocation entries apply.  We assume that an
1695              allocated reloc section uses the dynamic symbol table.
1696              FIXME: How can we be sure?  */
1697           s = bfd_get_section_by_name (abfd, ".dynsym");
1698           if (s != NULL)
1699             d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1700
1701           /* We look up the section the relocs apply to by name.  */
1702           name = sec->name;
1703           if (d->this_hdr.sh_type == SHT_REL)
1704             name += 4;
1705           else
1706             name += 5;
1707           s = bfd_get_section_by_name (abfd, name);
1708           if (s != NULL)
1709             d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1710           break;
1711
1712         case SHT_STRTAB:
1713           /* We assume that a section named .stab*str is a stabs
1714              string section.  We look for a section with the same name
1715              but without the trailing ``str'', and set its sh_link
1716              field to point to this section.  */
1717           if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1718               && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1719             {
1720               size_t len;
1721               char *alc;
1722
1723               len = strlen (sec->name);
1724               alc = (char *) bfd_malloc (len - 2);
1725               if (alc == NULL)
1726                 return false;
1727               strncpy (alc, sec->name, len - 3);
1728               alc[len - 3] = '\0';
1729               s = bfd_get_section_by_name (abfd, alc);
1730               free (alc);
1731               if (s != NULL)
1732                 {
1733                   elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1734
1735                   /* This is a .stab section.  */
1736                   elf_section_data (s)->this_hdr.sh_entsize =
1737                     4 + 2 * (bed->s->arch_size / 8);
1738                 }
1739             }
1740           break;
1741
1742         case SHT_DYNAMIC:
1743         case SHT_DYNSYM:
1744         case SHT_GNU_verneed:
1745         case SHT_GNU_verdef:
1746           /* sh_link is the section header index of the string table
1747              used for the dynamic entries, or the symbol table, or the
1748              version strings.  */
1749           s = bfd_get_section_by_name (abfd, ".dynstr");
1750           if (s != NULL)
1751             d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1752           break;
1753
1754         case SHT_HASH:
1755         case SHT_GNU_versym:
1756           /* sh_link is the section header index of the symbol table
1757              this hash table or version table is for.  */
1758           s = bfd_get_section_by_name (abfd, ".dynsym");
1759           if (s != NULL)
1760             d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1761           break;
1762         }
1763     }
1764
1765   return true;
1766 }
1767
1768 /* Map symbol from it's internal number to the external number, moving
1769    all local symbols to be at the head of the list.  */
1770
1771 static INLINE int
1772 sym_is_global (abfd, sym)
1773      bfd *abfd;
1774      asymbol *sym;
1775 {
1776   /* If the backend has a special mapping, use it.  */
1777   if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1778     return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1779             (abfd, sym));
1780
1781   return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1782           || bfd_is_und_section (bfd_get_section (sym))
1783           || bfd_is_com_section (bfd_get_section (sym)));
1784 }
1785
1786 static boolean
1787 elf_map_symbols (abfd)
1788      bfd *abfd;
1789 {
1790   int symcount = bfd_get_symcount (abfd);
1791   asymbol **syms = bfd_get_outsymbols (abfd);
1792   asymbol **sect_syms;
1793   int num_locals = 0;
1794   int num_globals = 0;
1795   int num_locals2 = 0;
1796   int num_globals2 = 0;
1797   int max_index = 0;
1798   int num_sections = 0;
1799   int idx;
1800   asection *asect;
1801   asymbol **new_syms;
1802
1803 #ifdef DEBUG
1804   fprintf (stderr, "elf_map_symbols\n");
1805   fflush (stderr);
1806 #endif
1807
1808   /* Add a section symbol for each BFD section.  FIXME: Is this really
1809      necessary?  */
1810   for (asect = abfd->sections; asect; asect = asect->next)
1811     {
1812       if (max_index < asect->index)
1813         max_index = asect->index;
1814     }
1815
1816   max_index++;
1817   sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1818   if (sect_syms == NULL)
1819     return false;
1820   elf_section_syms (abfd) = sect_syms;
1821
1822   for (idx = 0; idx < symcount; idx++)
1823     {
1824       if ((syms[idx]->flags & BSF_SECTION_SYM) != 0
1825           && syms[idx]->value == 0)
1826         {
1827           asection *sec;
1828
1829           sec = syms[idx]->section;
1830           if (sec->owner != NULL)
1831             {
1832               if (sec->owner != abfd)
1833                 {
1834                   if (sec->output_offset != 0)
1835                     continue;
1836                   sec = sec->output_section;
1837                   BFD_ASSERT (sec->owner == abfd);
1838                 }
1839               sect_syms[sec->index] = syms[idx];
1840             }
1841         }
1842     }
1843
1844   for (asect = abfd->sections; asect; asect = asect->next)
1845     {
1846       asymbol *sym;
1847
1848       if (sect_syms[asect->index] != NULL)
1849         continue;
1850
1851       sym = bfd_make_empty_symbol (abfd);
1852       if (sym == NULL)
1853         return false;
1854       sym->the_bfd = abfd;
1855       sym->name = asect->name;
1856       sym->value = 0;
1857       /* Set the flags to 0 to indicate that this one was newly added.  */
1858       sym->flags = 0;
1859       sym->section = asect;
1860       sect_syms[asect->index] = sym;
1861       num_sections++;
1862 #ifdef DEBUG
1863       fprintf (stderr,
1864  _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
1865                asect->name, (long) asect->vma, asect->index, (long) asect);
1866 #endif
1867     }
1868
1869   /* Classify all of the symbols.  */
1870   for (idx = 0; idx < symcount; idx++)
1871     {
1872       if (!sym_is_global (abfd, syms[idx]))
1873         num_locals++;
1874       else
1875         num_globals++;
1876     }
1877   for (asect = abfd->sections; asect; asect = asect->next)
1878     {
1879       if (sect_syms[asect->index] != NULL
1880           && sect_syms[asect->index]->flags == 0)
1881         {
1882           sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1883           if (!sym_is_global (abfd, sect_syms[asect->index]))
1884             num_locals++;
1885           else
1886             num_globals++;
1887           sect_syms[asect->index]->flags = 0;
1888         }
1889     }
1890
1891   /* Now sort the symbols so the local symbols are first.  */
1892   new_syms = ((asymbol **)
1893               bfd_alloc (abfd,
1894                          (num_locals + num_globals) * sizeof (asymbol *)));
1895   if (new_syms == NULL)
1896     return false;
1897
1898   for (idx = 0; idx < symcount; idx++)
1899     {
1900       asymbol *sym = syms[idx];
1901       int i;
1902
1903       if (!sym_is_global (abfd, sym))
1904         i = num_locals2++;
1905       else
1906         i = num_locals + num_globals2++;
1907       new_syms[i] = sym;
1908       sym->udata.i = i + 1;
1909     }
1910   for (asect = abfd->sections; asect; asect = asect->next)
1911     {
1912       if (sect_syms[asect->index] != NULL
1913           && sect_syms[asect->index]->flags == 0)
1914         {
1915           asymbol *sym = sect_syms[asect->index];
1916           int i;
1917
1918           sym->flags = BSF_SECTION_SYM;
1919           if (!sym_is_global (abfd, sym))
1920             i = num_locals2++;
1921           else
1922             i = num_locals + num_globals2++;
1923           new_syms[i] = sym;
1924           sym->udata.i = i + 1;
1925         }
1926     }
1927
1928   bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1929
1930   elf_num_locals (abfd) = num_locals;
1931   elf_num_globals (abfd) = num_globals;
1932   return true;
1933 }
1934
1935 /* Align to the maximum file alignment that could be required for any
1936    ELF data structure.  */
1937
1938 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1939 static INLINE file_ptr
1940 align_file_position (off, align)
1941      file_ptr off;
1942      int align;
1943 {
1944   return (off + align - 1) & ~(align - 1);
1945 }
1946
1947 /* Assign a file position to a section, optionally aligning to the
1948    required section alignment.  */
1949
1950 INLINE file_ptr
1951 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
1952      Elf_Internal_Shdr *i_shdrp;
1953      file_ptr offset;
1954      boolean align;
1955 {
1956   if (align)
1957     {
1958       unsigned int al;
1959
1960       al = i_shdrp->sh_addralign;
1961       if (al > 1)
1962         offset = BFD_ALIGN (offset, al);
1963     }
1964   i_shdrp->sh_offset = offset;
1965   if (i_shdrp->bfd_section != NULL)
1966     i_shdrp->bfd_section->filepos = offset;
1967   if (i_shdrp->sh_type != SHT_NOBITS)
1968     offset += i_shdrp->sh_size;
1969   return offset;
1970 }
1971
1972 /* Compute the file positions we are going to put the sections at, and
1973    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
1974    is not NULL, this is being called by the ELF backend linker.  */
1975
1976 boolean
1977 _bfd_elf_compute_section_file_positions (abfd, link_info)
1978      bfd *abfd;
1979      struct bfd_link_info *link_info;
1980 {
1981   struct elf_backend_data *bed = get_elf_backend_data (abfd);
1982   boolean failed;
1983   struct bfd_strtab_hash *strtab;
1984   Elf_Internal_Shdr *shstrtab_hdr;
1985
1986   if (abfd->output_has_begun)
1987     return true;
1988
1989   /* Do any elf backend specific processing first.  */
1990   if (bed->elf_backend_begin_write_processing)
1991     (*bed->elf_backend_begin_write_processing) (abfd, link_info);
1992
1993   if (! prep_headers (abfd))
1994     return false;
1995
1996   failed = false;
1997   bfd_map_over_sections (abfd, elf_fake_sections, &failed);
1998   if (failed)
1999     return false;
2000
2001   if (!assign_section_numbers (abfd))
2002     return false;
2003
2004   /* The backend linker builds symbol table information itself.  */
2005   if (link_info == NULL && abfd->symcount > 0)
2006     {
2007       /* Non-zero if doing a relocatable link.  */
2008       int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2009
2010       if (! swap_out_syms (abfd, &strtab, relocatable_p))
2011         return false;
2012     }
2013
2014   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2015   /* sh_name was set in prep_headers.  */
2016   shstrtab_hdr->sh_type = SHT_STRTAB;
2017   shstrtab_hdr->sh_flags = 0;
2018   shstrtab_hdr->sh_addr = 0;
2019   shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2020   shstrtab_hdr->sh_entsize = 0;
2021   shstrtab_hdr->sh_link = 0;
2022   shstrtab_hdr->sh_info = 0;
2023   /* sh_offset is set in assign_file_positions_except_relocs.  */
2024   shstrtab_hdr->sh_addralign = 1;
2025
2026   if (!assign_file_positions_except_relocs (abfd))
2027     return false;
2028
2029   if (link_info == NULL && abfd->symcount > 0)
2030     {
2031       file_ptr off;
2032       Elf_Internal_Shdr *hdr;
2033
2034       off = elf_tdata (abfd)->next_file_pos;
2035
2036       hdr = &elf_tdata (abfd)->symtab_hdr;
2037       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2038
2039       hdr = &elf_tdata (abfd)->strtab_hdr;
2040       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2041
2042       elf_tdata (abfd)->next_file_pos = off;
2043
2044       /* Now that we know where the .strtab section goes, write it
2045          out.  */
2046       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2047           || ! _bfd_stringtab_emit (abfd, strtab))
2048         return false;
2049       _bfd_stringtab_free (strtab);
2050     }
2051
2052   abfd->output_has_begun = true;
2053
2054   return true;
2055 }
2056
2057 /* Create a mapping from a set of sections to a program segment.  */
2058
2059 static INLINE struct elf_segment_map *
2060 make_mapping (abfd, sections, from, to, phdr)
2061      bfd *abfd;
2062      asection **sections;
2063      unsigned int from;
2064      unsigned int to;
2065      boolean phdr;
2066 {
2067   struct elf_segment_map *m;
2068   unsigned int i;
2069   asection **hdrpp;
2070
2071   m = ((struct elf_segment_map *)
2072        bfd_zalloc (abfd,
2073                    (sizeof (struct elf_segment_map)
2074                     + (to - from - 1) * sizeof (asection *))));
2075   if (m == NULL)
2076     return NULL;
2077   m->next = NULL;
2078   m->p_type = PT_LOAD;
2079   for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2080     m->sections[i - from] = *hdrpp;
2081   m->count = to - from;
2082
2083   if (from == 0 && phdr)
2084     {
2085       /* Include the headers in the first PT_LOAD segment.  */
2086       m->includes_filehdr = 1;
2087       m->includes_phdrs = 1;
2088     }
2089
2090   return m;
2091 }
2092
2093 /* Set up a mapping from BFD sections to program segments.  */
2094
2095 static boolean
2096 map_sections_to_segments (abfd)
2097      bfd *abfd;
2098 {
2099   asection **sections = NULL;
2100   asection *s;
2101   unsigned int i;
2102   unsigned int count;
2103   struct elf_segment_map *mfirst;
2104   struct elf_segment_map **pm;
2105   struct elf_segment_map *m;
2106   asection *last_hdr;
2107   unsigned int phdr_index;
2108   bfd_vma maxpagesize;
2109   asection **hdrpp;
2110   boolean phdr_in_segment = true;
2111   boolean writable;
2112   asection *dynsec;
2113
2114   if (elf_tdata (abfd)->segment_map != NULL)
2115     return true;
2116
2117   if (bfd_count_sections (abfd) == 0)
2118     return true;
2119
2120   /* Select the allocated sections, and sort them.  */
2121
2122   sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2123                                        * sizeof (asection *));
2124   if (sections == NULL)
2125     goto error_return;
2126
2127   i = 0;
2128   for (s = abfd->sections; s != NULL; s = s->next)
2129     {
2130       if ((s->flags & SEC_ALLOC) != 0)
2131         {
2132           sections[i] = s;
2133           ++i;
2134         }
2135     }
2136   BFD_ASSERT (i <= bfd_count_sections (abfd));
2137   count = i;
2138
2139   qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2140
2141   /* Build the mapping.  */
2142
2143   mfirst = NULL;
2144   pm = &mfirst;
2145
2146   /* If we have a .interp section, then create a PT_PHDR segment for
2147      the program headers and a PT_INTERP segment for the .interp
2148      section.  */
2149   s = bfd_get_section_by_name (abfd, ".interp");
2150   if (s != NULL && (s->flags & SEC_LOAD) != 0)
2151     {
2152       m = ((struct elf_segment_map *)
2153            bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2154       if (m == NULL)
2155         goto error_return;
2156       m->next = NULL;
2157       m->p_type = PT_PHDR;
2158       /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not.  */
2159       m->p_flags = PF_R | PF_X;
2160       m->p_flags_valid = 1;
2161       m->includes_phdrs = 1;
2162
2163       *pm = m;
2164       pm = &m->next;
2165
2166       m = ((struct elf_segment_map *)
2167            bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2168       if (m == NULL)
2169         goto error_return;
2170       m->next = NULL;
2171       m->p_type = PT_INTERP;
2172       m->count = 1;
2173       m->sections[0] = s;
2174
2175       *pm = m;
2176       pm = &m->next;
2177     }
2178
2179   /* Look through the sections.  We put sections in the same program
2180      segment when the start of the second section can be placed within
2181      a few bytes of the end of the first section.  */
2182   last_hdr = NULL;
2183   phdr_index = 0;
2184   maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2185   writable = false;
2186   dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2187   if (dynsec != NULL
2188       && (dynsec->flags & SEC_LOAD) == 0)
2189     dynsec = NULL;
2190
2191   /* Deal with -Ttext or something similar such that the first section
2192      is not adjacent to the program headers.  This is an
2193      approximation, since at this point we don't know exactly how many
2194      program headers we will need.  */
2195   if (count > 0)
2196     {
2197       bfd_size_type phdr_size;
2198
2199       phdr_size = elf_tdata (abfd)->program_header_size;
2200       if (phdr_size == 0)
2201         phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2202       if ((abfd->flags & D_PAGED) == 0
2203           || sections[0]->lma < phdr_size
2204           || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2205         phdr_in_segment = false;
2206     }
2207
2208   for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2209     {
2210       asection *hdr;
2211       boolean new_segment;
2212
2213       hdr = *hdrpp;
2214
2215       /* See if this section and the last one will fit in the same
2216          segment.  */
2217
2218       if (last_hdr == NULL)
2219         {
2220           /* If we don't have a segment yet, then we don't need a new
2221              one (we build the last one after this loop).  */
2222           new_segment = false;
2223         }
2224       else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2225         {
2226           /* If this section has a different relation between the
2227              virtual address and the load address, then we need a new
2228              segment.  */
2229           new_segment = true;
2230         }
2231       else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2232                < BFD_ALIGN (hdr->lma, maxpagesize))
2233         {
2234           /* If putting this section in this segment would force us to
2235              skip a page in the segment, then we need a new segment.  */
2236           new_segment = true;
2237         }
2238       else if ((last_hdr->flags & SEC_LOAD) == 0
2239                && (hdr->flags & SEC_LOAD) != 0)
2240         {
2241           /* We don't want to put a loadable section after a
2242              nonloadable section in the same segment.  */
2243           new_segment = true;
2244         }
2245       else if ((abfd->flags & D_PAGED) == 0)
2246         {
2247           /* If the file is not demand paged, which means that we
2248              don't require the sections to be correctly aligned in the
2249              file, then there is no other reason for a new segment.  */
2250           new_segment = false;
2251         }
2252       else if (! writable
2253                && (hdr->flags & SEC_READONLY) == 0
2254                && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2255                    == hdr->lma))
2256         {
2257           /* We don't want to put a writable section in a read only
2258              segment, unless they are on the same page in memory
2259              anyhow.  We already know that the last section does not
2260              bring us past the current section on the page, so the
2261              only case in which the new section is not on the same
2262              page as the previous section is when the previous section
2263              ends precisely on a page boundary.  */
2264           new_segment = true;
2265         }
2266       else
2267         {
2268           /* Otherwise, we can use the same segment.  */
2269           new_segment = false;
2270         }
2271
2272       if (! new_segment)
2273         {
2274           if ((hdr->flags & SEC_READONLY) == 0)
2275             writable = true;
2276           last_hdr = hdr;
2277           continue;
2278         }
2279
2280       /* We need a new program segment.  We must create a new program
2281          header holding all the sections from phdr_index until hdr.  */
2282
2283       m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2284       if (m == NULL)
2285         goto error_return;
2286
2287       *pm = m;
2288       pm = &m->next;
2289
2290       if ((hdr->flags & SEC_READONLY) == 0)
2291         writable = true;
2292       else
2293         writable = false;
2294
2295       last_hdr = hdr;
2296       phdr_index = i;
2297       phdr_in_segment = false;
2298     }
2299
2300   /* Create a final PT_LOAD program segment.  */
2301   if (last_hdr != NULL)
2302     {
2303       m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2304       if (m == NULL)
2305         goto error_return;
2306
2307       *pm = m;
2308       pm = &m->next;
2309     }
2310
2311   /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
2312   if (dynsec != NULL)
2313     {
2314       m = ((struct elf_segment_map *)
2315            bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2316       if (m == NULL)
2317         goto error_return;
2318       m->next = NULL;
2319       m->p_type = PT_DYNAMIC;
2320       m->count = 1;
2321       m->sections[0] = dynsec;
2322
2323       *pm = m;
2324       pm = &m->next;
2325     }
2326
2327   /* For each loadable .note section, add a PT_NOTE segment.  We don't
2328      use bfd_get_section_by_name, because if we link together
2329      nonloadable .note sections and loadable .note sections, we will
2330      generate two .note sections in the output file.  FIXME: Using
2331      names for section types is bogus anyhow.  */
2332   for (s = abfd->sections; s != NULL; s = s->next)
2333     {
2334       if ((s->flags & SEC_LOAD) != 0
2335           && strncmp (s->name, ".note", 5) == 0)
2336         {
2337           m = ((struct elf_segment_map *)
2338                bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2339           if (m == NULL)
2340             goto error_return;
2341           m->next = NULL;
2342           m->p_type = PT_NOTE;
2343           m->count = 1;
2344           m->sections[0] = s;
2345
2346           *pm = m;
2347           pm = &m->next;
2348         }
2349     }
2350
2351   free (sections);
2352   sections = NULL;
2353
2354   elf_tdata (abfd)->segment_map = mfirst;
2355   return true;
2356
2357  error_return:
2358   if (sections != NULL)
2359     free (sections);
2360   return false;
2361 }
2362
2363 /* Sort sections by address.  */
2364
2365 static int
2366 elf_sort_sections (arg1, arg2)
2367      const PTR arg1;
2368      const PTR arg2;
2369 {
2370   const asection *sec1 = *(const asection **) arg1;
2371   const asection *sec2 = *(const asection **) arg2;
2372
2373   /* Sort by LMA first, since this is the address used to
2374      place the section into a segment.  */
2375   if (sec1->lma < sec2->lma)
2376     return -1;
2377   else if (sec1->lma > sec2->lma)
2378     return 1;
2379
2380   /* Then sort by VMA.  Normally the LMA and the VMA will be
2381      the same, and this will do nothing.  */
2382   if (sec1->vma < sec2->vma)
2383     return -1;
2384   else if (sec1->vma > sec2->vma)
2385     return 1;
2386
2387   /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
2388
2389 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2390
2391   if (TOEND (sec1))
2392     {
2393       if (TOEND (sec2))
2394         return sec1->target_index - sec2->target_index;
2395       else 
2396         return 1;
2397     }
2398
2399   if (TOEND (sec2))
2400     return -1;
2401
2402 #undef TOEND
2403
2404   /* Sort by size, to put zero sized sections before others at the
2405      same address.  */
2406
2407   if (sec1->_raw_size < sec2->_raw_size)
2408     return -1;
2409   if (sec1->_raw_size > sec2->_raw_size)
2410     return 1;
2411
2412   return sec1->target_index - sec2->target_index;
2413 }
2414
2415 /* Assign file positions to the sections based on the mapping from
2416    sections to segments.  This function also sets up some fields in
2417    the file header, and writes out the program headers.  */
2418
2419 static boolean
2420 assign_file_positions_for_segments (abfd)
2421      bfd *abfd;
2422 {
2423   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2424   unsigned int count;
2425   struct elf_segment_map *m;
2426   unsigned int alloc;
2427   Elf_Internal_Phdr *phdrs;
2428   file_ptr off, voff;
2429   bfd_vma filehdr_vaddr, filehdr_paddr;
2430   bfd_vma phdrs_vaddr, phdrs_paddr;
2431   Elf_Internal_Phdr *p;
2432
2433   if (elf_tdata (abfd)->segment_map == NULL)
2434     {
2435       if (! map_sections_to_segments (abfd))
2436         return false;
2437     }
2438
2439   if (bed->elf_backend_modify_segment_map)
2440     {
2441       if (! (*bed->elf_backend_modify_segment_map) (abfd))
2442         return false;
2443     }
2444
2445   count = 0;
2446   for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2447     ++count;
2448
2449   elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2450   elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2451   elf_elfheader (abfd)->e_phnum = count;
2452
2453   if (count == 0)
2454     return true;
2455
2456   /* If we already counted the number of program segments, make sure
2457      that we allocated enough space.  This happens when SIZEOF_HEADERS
2458      is used in a linker script.  */
2459   alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2460   if (alloc != 0 && count > alloc)
2461     {
2462       ((*_bfd_error_handler)
2463        (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2464         bfd_get_filename (abfd), alloc, count));
2465       bfd_set_error (bfd_error_bad_value);
2466       return false;
2467     }
2468
2469   if (alloc == 0)
2470     alloc = count;
2471
2472   phdrs = ((Elf_Internal_Phdr *)
2473            bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2474   if (phdrs == NULL)
2475     return false;
2476
2477   off = bed->s->sizeof_ehdr;
2478   off += alloc * bed->s->sizeof_phdr;
2479
2480   filehdr_vaddr = 0;
2481   filehdr_paddr = 0;
2482   phdrs_vaddr = 0;
2483   phdrs_paddr = 0;
2484   
2485   for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2486        m != NULL;
2487        m = m->next, p++)
2488     {
2489       unsigned int i;
2490       asection **secpp;
2491
2492       /* If elf_segment_map is not from map_sections_to_segments, the
2493          sections may not be correctly ordered.  */
2494       if (m->count > 0)
2495         qsort (m->sections, (size_t) m->count, sizeof (asection *),
2496                elf_sort_sections);
2497
2498       p->p_type = m->p_type;
2499
2500       if (m->p_flags_valid)
2501         p->p_flags = m->p_flags;
2502       else
2503         p->p_flags = 0;
2504
2505       if (p->p_type == PT_LOAD
2506           && m->count > 0
2507           && (m->sections[0]->flags & SEC_ALLOC) != 0)
2508         {
2509           if ((abfd->flags & D_PAGED) != 0)
2510             off += (m->sections[0]->vma - off) % bed->maxpagesize;
2511           else
2512             off += ((m->sections[0]->vma - off)
2513                     % (1 << bfd_get_section_alignment (abfd, m->sections[0])));
2514         }
2515
2516       if (m->count == 0)
2517         p->p_vaddr = 0;
2518       else
2519         p->p_vaddr = m->sections[0]->vma;
2520
2521       if (m->p_paddr_valid)
2522         p->p_paddr = m->p_paddr;
2523       else if (m->count == 0)
2524         p->p_paddr = 0;
2525       else
2526         p->p_paddr = m->sections[0]->lma;
2527
2528       if (p->p_type == PT_LOAD
2529           && (abfd->flags & D_PAGED) != 0)
2530         p->p_align = bed->maxpagesize;
2531       else if (m->count == 0)
2532         p->p_align = bed->s->file_align;
2533       else
2534         p->p_align = 0;
2535
2536       p->p_offset = 0;
2537       p->p_filesz = 0;
2538       p->p_memsz = 0;
2539       
2540       if (m->includes_filehdr)
2541         {
2542           if (! m->p_flags_valid)
2543             p->p_flags |= PF_R;
2544           p->p_offset = 0;
2545           p->p_filesz = bed->s->sizeof_ehdr;
2546           p->p_memsz = bed->s->sizeof_ehdr;
2547           if (m->count > 0)
2548             {
2549               BFD_ASSERT (p->p_type == PT_LOAD);
2550
2551               if (p->p_vaddr < (bfd_vma) off)
2552                 {
2553                   _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2554                                       bfd_get_filename (abfd));
2555                   bfd_set_error (bfd_error_bad_value);
2556                   return false;
2557                 }
2558               
2559               p->p_vaddr -= off;
2560               if (! m->p_paddr_valid)
2561                 p->p_paddr -= off;
2562             }
2563           if (p->p_type == PT_LOAD)
2564             {
2565               filehdr_vaddr = p->p_vaddr;
2566               filehdr_paddr = p->p_paddr;
2567             }
2568         }
2569
2570       if (m->includes_phdrs)
2571         {
2572           if (! m->p_flags_valid)
2573             p->p_flags |= PF_R;
2574           
2575           if (m->includes_filehdr)
2576             {
2577               if (p->p_type == PT_LOAD)
2578                 {
2579                   phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2580                   phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2581                 }
2582             }
2583           else
2584             {
2585               p->p_offset = bed->s->sizeof_ehdr;
2586               
2587               if (m->count > 0)
2588                 {
2589                   BFD_ASSERT (p->p_type == PT_LOAD);
2590                   p->p_vaddr -= off - p->p_offset;
2591                   if (! m->p_paddr_valid)
2592                     p->p_paddr -= off - p->p_offset;
2593                 }
2594               
2595               if (p->p_type == PT_LOAD)
2596                 {
2597                   phdrs_vaddr = p->p_vaddr;
2598                   phdrs_paddr = p->p_paddr;
2599                 }
2600               else
2601                 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2602             }
2603           
2604           p->p_filesz += alloc * bed->s->sizeof_phdr;
2605           p->p_memsz += alloc * bed->s->sizeof_phdr;
2606         }
2607
2608       if (p->p_type == PT_LOAD || p->p_type == PT_NOTE)
2609         {
2610           if (! m->includes_filehdr && ! m->includes_phdrs)
2611             p->p_offset = off;
2612           else
2613             {
2614               file_ptr adjust;
2615
2616               adjust = off - (p->p_offset + p->p_filesz);
2617               p->p_filesz += adjust;
2618               p->p_memsz += adjust;
2619             }
2620         }
2621
2622       voff = off;
2623       
2624       for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2625         {
2626           asection *sec;
2627           flagword flags;
2628           bfd_size_type align;
2629
2630           sec = *secpp;
2631           flags = sec->flags;
2632           align = 1 << bfd_get_section_alignment (abfd, sec);
2633
2634           /* The section may have artificial alignment forced by a 
2635              link script.  Notice this case by the gap between the
2636              cumulative phdr vma and the section's vma.  */
2637           if (p->p_vaddr + p->p_memsz < sec->vma)
2638             {
2639               bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2640
2641               p->p_memsz += adjust;
2642               off += adjust;
2643               voff += adjust;
2644               if ((flags & SEC_LOAD) != 0)
2645                 p->p_filesz += adjust;
2646             }
2647
2648           if (p->p_type == PT_LOAD)
2649             {
2650               bfd_signed_vma adjust;
2651               
2652               if ((flags & SEC_LOAD) != 0)
2653                 {
2654                   adjust = sec->lma - (p->p_paddr + p->p_memsz);
2655                   if (adjust < 0)
2656                     adjust = 0;
2657                 }
2658               else if ((flags & SEC_ALLOC) != 0)
2659                 {
2660                   /* The section VMA must equal the file position
2661                      modulo the page size.  FIXME: I'm not sure if
2662                      this adjustment is really necessary.  We used to
2663                      not have the SEC_LOAD case just above, and then
2664                      this was necessary, but now I'm not sure.  */
2665                   if ((abfd->flags & D_PAGED) != 0)
2666                     adjust = (sec->vma - voff) % bed->maxpagesize;
2667                   else
2668                     adjust = (sec->vma - voff) % align;
2669                 }
2670               else
2671                 adjust = 0;
2672
2673               if (adjust != 0)
2674                 {
2675                   if (i == 0)
2676                     {
2677                       (* _bfd_error_handler)
2678                         (_("Error: First section in segment (%s) starts at 0x%x"),
2679                          bfd_section_name (abfd, sec), sec->lma);
2680                       (* _bfd_error_handler)
2681                         (_("       whereas segment starts at 0x%x"),
2682                          p->p_paddr);
2683                       
2684                       return false;
2685                     }
2686                   p->p_memsz += adjust;
2687                   off += adjust;
2688                   voff += adjust;
2689                   if ((flags & SEC_LOAD) != 0)
2690                     p->p_filesz += adjust;
2691                 }
2692
2693               sec->filepos = off;
2694
2695               /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2696                  used in a linker script we may have a section with
2697                  SEC_LOAD clear but which is supposed to have
2698                  contents.  */
2699               if ((flags & SEC_LOAD) != 0
2700                   || (flags & SEC_HAS_CONTENTS) != 0)
2701                 off += sec->_raw_size;
2702               
2703               if ((flags & SEC_ALLOC) != 0)
2704                 voff += sec->_raw_size;
2705             }
2706
2707           if (p->p_type == PT_NOTE)
2708             {
2709               if (i == 0)       /* the actual "note" segment */
2710                 {               /* this one actually contains everything. */
2711                   sec->filepos = off;
2712                   p->p_filesz = sec->_raw_size;
2713                   off += sec->_raw_size;
2714                   voff = off;
2715                 }
2716               else      /* fake sections -- don't need to be written */
2717                 {
2718                   sec->filepos = 0;
2719                   sec->_raw_size = 0;
2720                   flags = sec->flags = 0;       /* no contents */
2721                 }
2722               p->p_memsz = 0;
2723               p->p_align = 1;
2724             }
2725           else
2726             {
2727               p->p_memsz += sec->_raw_size;
2728
2729               if ((flags & SEC_LOAD) != 0)
2730                 p->p_filesz += sec->_raw_size;
2731
2732               if (align > p->p_align)
2733                 p->p_align = align;
2734             }
2735
2736           if (! m->p_flags_valid)
2737             {
2738               p->p_flags |= PF_R;
2739               if ((flags & SEC_CODE) != 0)
2740                 p->p_flags |= PF_X;
2741               if ((flags & SEC_READONLY) == 0)
2742                 p->p_flags |= PF_W;
2743             }
2744         }
2745     }
2746
2747   /* Now that we have set the section file positions, we can set up
2748      the file positions for the non PT_LOAD segments.  */
2749   for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2750        m != NULL;
2751        m = m->next, p++)
2752     {
2753       if (p->p_type != PT_LOAD && m->count > 0)
2754         {
2755           BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2756           p->p_offset = m->sections[0]->filepos;
2757         }
2758       if (m->count == 0)
2759         {
2760           if (m->includes_filehdr)
2761             {
2762               p->p_vaddr = filehdr_vaddr;
2763               if (! m->p_paddr_valid)
2764                 p->p_paddr = filehdr_paddr;
2765             }
2766           else if (m->includes_phdrs)
2767             {
2768               p->p_vaddr = phdrs_vaddr;
2769               if (! m->p_paddr_valid)
2770                 p->p_paddr = phdrs_paddr;
2771             }
2772         }
2773     }
2774
2775   /* Clear out any program headers we allocated but did not use.  */
2776   for (; count < alloc; count++, p++)
2777     {
2778       memset (p, 0, sizeof *p);
2779       p->p_type = PT_NULL;
2780     }
2781
2782   elf_tdata (abfd)->phdr = phdrs;
2783
2784   elf_tdata (abfd)->next_file_pos = off;
2785
2786   /* Write out the program headers.  */
2787   if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2788       || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2789     return false;
2790
2791   return true;
2792 }
2793
2794 /* Get the size of the program header.
2795
2796    If this is called by the linker before any of the section VMA's are set, it
2797    can't calculate the correct value for a strange memory layout.  This only
2798    happens when SIZEOF_HEADERS is used in a linker script.  In this case,
2799    SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2800    data segment (exclusive of .interp and .dynamic).
2801
2802    ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2803    will be two segments.  */
2804
2805 static bfd_size_type
2806 get_program_header_size (abfd)
2807      bfd *abfd;
2808 {
2809   size_t segs;
2810   asection *s;
2811   struct elf_backend_data *bed = get_elf_backend_data (abfd);
2812
2813   /* We can't return a different result each time we're called.  */
2814   if (elf_tdata (abfd)->program_header_size != 0)
2815     return elf_tdata (abfd)->program_header_size;
2816
2817   if (elf_tdata (abfd)->segment_map != NULL)
2818     {
2819       struct elf_segment_map *m;
2820
2821       segs = 0;
2822       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2823         ++segs;
2824       elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2825       return elf_tdata (abfd)->program_header_size;
2826     }
2827
2828   /* Assume we will need exactly two PT_LOAD segments: one for text
2829      and one for data.  */
2830   segs = 2;
2831
2832   s = bfd_get_section_by_name (abfd, ".interp");
2833   if (s != NULL && (s->flags & SEC_LOAD) != 0)
2834     {
2835       /* If we have a loadable interpreter section, we need a
2836          PT_INTERP segment.  In this case, assume we also need a
2837          PT_PHDR segment, although that may not be true for all
2838          targets.  */
2839       segs += 2;
2840     }
2841
2842   if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
2843     {
2844       /* We need a PT_DYNAMIC segment.  */
2845       ++segs;
2846     }
2847
2848   for (s = abfd->sections; s != NULL; s = s->next)
2849     {
2850       if ((s->flags & SEC_LOAD) != 0
2851           && strncmp (s->name, ".note", 5) == 0)
2852         {
2853           /* We need a PT_NOTE segment.  */
2854           ++segs;
2855         }
2856     }
2857
2858   /* Let the backend count up any program headers it might need.  */
2859   if (bed->elf_backend_additional_program_headers)
2860     {
2861       int a;
2862
2863       a = (*bed->elf_backend_additional_program_headers) (abfd);
2864       if (a == -1)
2865         abort ();
2866       segs += a;
2867     }
2868
2869   elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2870   return elf_tdata (abfd)->program_header_size;
2871 }
2872
2873 /* Work out the file positions of all the sections.  This is called by
2874    _bfd_elf_compute_section_file_positions.  All the section sizes and
2875    VMAs must be known before this is called.
2876
2877    We do not consider reloc sections at this point, unless they form
2878    part of the loadable image.  Reloc sections are assigned file
2879    positions in assign_file_positions_for_relocs, which is called by
2880    write_object_contents and final_link.
2881
2882    We also don't set the positions of the .symtab and .strtab here.  */
2883
2884 static boolean
2885 assign_file_positions_except_relocs (abfd)
2886      bfd *abfd;
2887 {
2888   struct elf_obj_tdata * const tdata = elf_tdata (abfd);
2889   Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
2890   Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
2891   file_ptr off;
2892   struct elf_backend_data *bed = get_elf_backend_data (abfd);
2893
2894   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 &&
2895       abfd->format != bfd_core)
2896     {
2897       Elf_Internal_Shdr **hdrpp;
2898       unsigned int i;
2899
2900       /* Start after the ELF header.  */
2901       off = i_ehdrp->e_ehsize;
2902
2903       /* We are not creating an executable, which means that we are
2904          not creating a program header, and that the actual order of
2905          the sections in the file is unimportant.  */
2906       for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2907         {
2908           Elf_Internal_Shdr *hdr;
2909
2910           hdr = *hdrpp;
2911           if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2912             {
2913               hdr->sh_offset = -1;
2914               continue;
2915             }
2916           if (i == tdata->symtab_section
2917               || i == tdata->strtab_section)
2918             {
2919               hdr->sh_offset = -1;
2920               continue;
2921             }
2922           
2923           off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2924         }
2925     }
2926   else
2927     {
2928       unsigned int i;
2929       Elf_Internal_Shdr **hdrpp;
2930
2931       /* Assign file positions for the loaded sections based on the
2932          assignment of sections to segments.  */
2933       if (! assign_file_positions_for_segments (abfd))
2934         return false;
2935
2936       /* Assign file positions for the other sections.  */
2937
2938       off = elf_tdata (abfd)->next_file_pos;
2939       for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2940         {
2941           Elf_Internal_Shdr *hdr;
2942
2943           hdr = *hdrpp;
2944           if (hdr->bfd_section != NULL
2945               && hdr->bfd_section->filepos != 0)
2946             hdr->sh_offset = hdr->bfd_section->filepos;
2947           else if ((hdr->sh_flags & SHF_ALLOC) != 0)
2948             {
2949               ((*_bfd_error_handler)
2950                (_("%s: warning: allocated section `%s' not in segment"),
2951                 bfd_get_filename (abfd),
2952                 (hdr->bfd_section == NULL
2953                  ? "*unknown*"
2954                  : hdr->bfd_section->name)));
2955               if ((abfd->flags & D_PAGED) != 0)
2956                 off += (hdr->sh_addr - off) % bed->maxpagesize;
2957               else
2958                 off += (hdr->sh_addr - off) % hdr->sh_addralign;
2959               off = _bfd_elf_assign_file_position_for_section (hdr, off,
2960                                                                false);
2961             }
2962           else if (hdr->sh_type == SHT_REL
2963                    || hdr->sh_type == SHT_RELA
2964                    || hdr == i_shdrpp[tdata->symtab_section]
2965                    || hdr == i_shdrpp[tdata->strtab_section])
2966             hdr->sh_offset = -1;
2967           else
2968             off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2969         }                  
2970     }
2971
2972   /* Place the section headers.  */
2973   off = align_file_position (off, bed->s->file_align);
2974   i_ehdrp->e_shoff = off;
2975   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2976
2977   elf_tdata (abfd)->next_file_pos = off;
2978
2979   return true;
2980 }
2981
2982 static boolean
2983 prep_headers (abfd)
2984      bfd *abfd;
2985 {
2986   Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */
2987   Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2988   Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
2989   int count;
2990   struct bfd_strtab_hash *shstrtab;
2991   struct elf_backend_data *bed = get_elf_backend_data (abfd);
2992
2993   i_ehdrp = elf_elfheader (abfd);
2994   i_shdrp = elf_elfsections (abfd);
2995
2996   shstrtab = _bfd_elf_stringtab_init ();
2997   if (shstrtab == NULL)
2998     return false;
2999
3000   elf_shstrtab (abfd) = shstrtab;
3001
3002   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3003   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3004   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3005   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3006
3007   i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3008   i_ehdrp->e_ident[EI_DATA] =
3009     bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3010   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3011
3012   for (count = EI_PAD; count < EI_NIDENT; count++)
3013     i_ehdrp->e_ident[count] = 0;
3014
3015   if ((abfd->flags & DYNAMIC) != 0)
3016     i_ehdrp->e_type = ET_DYN;
3017   else if ((abfd->flags & EXEC_P) != 0)
3018     i_ehdrp->e_type = ET_EXEC;
3019   else if (bfd_get_format (abfd) == bfd_core)
3020     i_ehdrp->e_type = ET_CORE;
3021   else
3022     i_ehdrp->e_type = ET_REL;
3023
3024   switch (bfd_get_arch (abfd))
3025     {
3026     case bfd_arch_unknown:
3027       i_ehdrp->e_machine = EM_NONE;
3028       break;
3029     case bfd_arch_sparc:
3030       if (bed->s->arch_size == 64)
3031         i_ehdrp->e_machine = EM_SPARCV9;
3032       else
3033         i_ehdrp->e_machine = EM_SPARC;
3034       break;
3035     case bfd_arch_i386:
3036       i_ehdrp->e_machine = EM_386;
3037       break;
3038     case bfd_arch_m68k:
3039       i_ehdrp->e_machine = EM_68K;
3040       break;
3041     case bfd_arch_m88k:
3042       i_ehdrp->e_machine = EM_88K;
3043       break;
3044     case bfd_arch_i860:
3045       i_ehdrp->e_machine = EM_860;
3046       break;
3047     case bfd_arch_mips: /* MIPS Rxxxx */
3048       i_ehdrp->e_machine = EM_MIPS;     /* only MIPS R3000 */
3049       break;
3050     case bfd_arch_hppa:
3051       i_ehdrp->e_machine = EM_PARISC;
3052       break;
3053     case bfd_arch_powerpc:
3054       i_ehdrp->e_machine = EM_PPC;
3055       break;
3056     case bfd_arch_alpha:
3057       i_ehdrp->e_machine = EM_ALPHA;
3058       break;
3059     case bfd_arch_sh:
3060       i_ehdrp->e_machine = EM_SH;
3061       break;
3062     case bfd_arch_d10v:
3063       i_ehdrp->e_machine = EM_CYGNUS_D10V;
3064       break;
3065     case bfd_arch_d30v:
3066       i_ehdrp->e_machine = EM_CYGNUS_D30V;
3067       break;
3068     case bfd_arch_v850:
3069       switch (bfd_get_mach (abfd))
3070         {
3071         default:
3072         case 0:               i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3073         }
3074       break;
3075    case bfd_arch_arc:
3076       i_ehdrp->e_machine = EM_CYGNUS_ARC;
3077       break;
3078    case bfd_arch_arm:
3079       i_ehdrp->e_machine = EM_ARM;
3080       break;
3081     case bfd_arch_m32r:
3082       i_ehdrp->e_machine = EM_CYGNUS_M32R;
3083       break;
3084     case bfd_arch_mn10200:
3085       i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3086       break;
3087     case bfd_arch_mn10300:
3088       i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3089       break;
3090       /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3091     default:
3092       i_ehdrp->e_machine = EM_NONE;
3093     }
3094   i_ehdrp->e_version = bed->s->ev_current;
3095   i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3096
3097   /* no program header, for now. */
3098   i_ehdrp->e_phoff = 0;
3099   i_ehdrp->e_phentsize = 0;
3100   i_ehdrp->e_phnum = 0;
3101
3102   /* each bfd section is section header entry */
3103   i_ehdrp->e_entry = bfd_get_start_address (abfd);
3104   i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3105
3106   /* if we're building an executable, we'll need a program header table */
3107   if (abfd->flags & EXEC_P)
3108     {
3109       /* it all happens later */
3110 #if 0
3111       i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3112
3113       /* elf_build_phdrs() returns a (NULL-terminated) array of
3114          Elf_Internal_Phdrs */
3115       i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3116       i_ehdrp->e_phoff = outbase;
3117       outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3118 #endif
3119     }
3120   else
3121     {
3122       i_ehdrp->e_phentsize = 0;
3123       i_phdrp = 0;
3124       i_ehdrp->e_phoff = 0;
3125     }
3126
3127   elf_tdata (abfd)->symtab_hdr.sh_name =
3128     (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3129   elf_tdata (abfd)->strtab_hdr.sh_name =
3130     (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3131   elf_tdata (abfd)->shstrtab_hdr.sh_name =
3132     (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3133   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3134       || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3135       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3136     return false;
3137
3138   return true;
3139 }
3140
3141 /* Assign file positions for all the reloc sections which are not part
3142    of the loadable file image.  */
3143
3144 void
3145 _bfd_elf_assign_file_positions_for_relocs (abfd)
3146      bfd *abfd;
3147 {
3148   file_ptr off;
3149   unsigned int i;
3150   Elf_Internal_Shdr **shdrpp;
3151
3152   off = elf_tdata (abfd)->next_file_pos;
3153
3154   for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3155        i < elf_elfheader (abfd)->e_shnum;
3156        i++, shdrpp++)
3157     {
3158       Elf_Internal_Shdr *shdrp;
3159
3160       shdrp = *shdrpp;
3161       if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3162           && shdrp->sh_offset == -1)
3163         off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3164     }
3165
3166   elf_tdata (abfd)->next_file_pos = off;
3167 }
3168
3169 boolean
3170 _bfd_elf_write_object_contents (abfd)
3171      bfd *abfd;
3172 {
3173   struct elf_backend_data *bed = get_elf_backend_data (abfd);
3174   Elf_Internal_Ehdr *i_ehdrp;
3175   Elf_Internal_Shdr **i_shdrp;
3176   boolean failed;
3177   unsigned int count;
3178
3179   if (! abfd->output_has_begun
3180       && ! _bfd_elf_compute_section_file_positions
3181              (abfd, (struct bfd_link_info *) NULL))
3182     return false;
3183
3184   i_shdrp = elf_elfsections (abfd);
3185   i_ehdrp = elf_elfheader (abfd);
3186
3187   failed = false;
3188   bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3189   if (failed)
3190     return false;
3191
3192   _bfd_elf_assign_file_positions_for_relocs (abfd);
3193
3194   /* After writing the headers, we need to write the sections too... */
3195   for (count = 1; count < i_ehdrp->e_shnum; count++)
3196     {
3197       if (bed->elf_backend_section_processing)
3198         (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3199       if (i_shdrp[count]->contents)
3200         {
3201           if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3202               || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3203                              1, abfd)
3204                   != i_shdrp[count]->sh_size))
3205             return false;
3206         }
3207     }
3208
3209   /* Write out the section header names.  */
3210   if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3211       || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3212     return false;
3213
3214   if (bed->elf_backend_final_write_processing)
3215     (*bed->elf_backend_final_write_processing) (abfd,
3216                                                 elf_tdata (abfd)->linker);
3217
3218   return bed->s->write_shdrs_and_ehdr (abfd);
3219 }
3220
3221 boolean
3222 _bfd_elf_write_corefile_contents (abfd)
3223      bfd *abfd;
3224 {
3225   /* Hopefully this can be done just like an object file. */
3226   return _bfd_elf_write_object_contents (abfd);
3227 }
3228 /* given a section, search the header to find them... */
3229 int
3230 _bfd_elf_section_from_bfd_section (abfd, asect)
3231      bfd *abfd;
3232      struct sec *asect;
3233 {
3234   struct elf_backend_data *bed = get_elf_backend_data (abfd);
3235   Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3236   int index;
3237   Elf_Internal_Shdr *hdr;
3238   int maxindex = elf_elfheader (abfd)->e_shnum;
3239
3240   for (index = 0; index < maxindex; index++)
3241     {
3242       hdr = i_shdrp[index];
3243       if (hdr->bfd_section == asect)
3244         return index;
3245     }
3246
3247   if (bed->elf_backend_section_from_bfd_section)
3248     {
3249       for (index = 0; index < maxindex; index++)
3250         {
3251           int retval;
3252
3253           hdr = i_shdrp[index];
3254           retval = index;
3255           if ((*bed->elf_backend_section_from_bfd_section)
3256               (abfd, hdr, asect, &retval))
3257             return retval;
3258         }
3259     }
3260
3261   if (bfd_is_abs_section (asect))
3262     return SHN_ABS;
3263   if (bfd_is_com_section (asect))
3264     return SHN_COMMON;
3265   if (bfd_is_und_section (asect))
3266     return SHN_UNDEF;
3267
3268   bfd_set_error (bfd_error_nonrepresentable_section);
3269
3270   return -1;
3271 }
3272
3273 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3274    on error.  */
3275
3276 int
3277 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3278      bfd *abfd;
3279      asymbol **asym_ptr_ptr;
3280 {
3281   asymbol *asym_ptr = *asym_ptr_ptr;
3282   int idx;
3283   flagword flags = asym_ptr->flags;
3284
3285   /* When gas creates relocations against local labels, it creates its
3286      own symbol for the section, but does put the symbol into the
3287      symbol chain, so udata is 0.  When the linker is generating
3288      relocatable output, this section symbol may be for one of the
3289      input sections rather than the output section.  */
3290   if (asym_ptr->udata.i == 0
3291       && (flags & BSF_SECTION_SYM)
3292       && asym_ptr->section)
3293     {
3294       int indx;
3295
3296       if (asym_ptr->section->output_section != NULL)
3297         indx = asym_ptr->section->output_section->index;
3298       else
3299         indx = asym_ptr->section->index;
3300       if (elf_section_syms (abfd)[indx])
3301         asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3302     }
3303
3304   idx = asym_ptr->udata.i;
3305
3306   if (idx == 0)
3307     {
3308       /* This case can occur when using --strip-symbol on a symbol
3309          which is used in a relocation entry.  */
3310       (*_bfd_error_handler)
3311         (_("%s: symbol `%s' required but not present"),
3312          bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3313       bfd_set_error (bfd_error_no_symbols);
3314       return -1;
3315     }
3316
3317 #if DEBUG & 4
3318   {
3319     fprintf (stderr,
3320              _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3321              (long) asym_ptr, asym_ptr->name, idx, flags,
3322              elf_symbol_flags (flags));
3323     fflush (stderr);
3324   }
3325 #endif
3326
3327   return idx;
3328 }
3329
3330 /* Copy private BFD data.  This copies any program header information.  */
3331
3332 static boolean
3333 copy_private_bfd_data (ibfd, obfd)
3334      bfd *ibfd;
3335      bfd *obfd;
3336 {
3337   Elf_Internal_Ehdr *iehdr;
3338   struct elf_segment_map *mfirst;
3339   struct elf_segment_map **pm;
3340   struct elf_segment_map *m;
3341   Elf_Internal_Phdr *p;
3342   unsigned int i;
3343   unsigned int num_segments;
3344   boolean phdr_included = false;
3345   
3346   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3347       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3348     return true;
3349
3350   if (elf_tdata (ibfd)->phdr == NULL)
3351     return true;
3352
3353   iehdr = elf_elfheader (ibfd);
3354
3355   mfirst = NULL;
3356   pm = &mfirst;
3357
3358   num_segments = elf_elfheader (ibfd)->e_phnum;
3359
3360 #define IS_CONTAINED_BY(addr, len, bottom, phdr)                        \
3361           ((addr) >= (bottom)                                           \
3362            && (   ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz)      \
3363                || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz)))
3364
3365   /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3366
3367 #define IS_COREFILE_NOTE(p, s)                                               \
3368             (p->p_type == PT_NOTE &&                                         \
3369              s->vma == 0 && s->lma == 0 && s->_cooked_size == 0 &&           \
3370              (bfd_vma) s->filepos >= p->p_offset &&                          \
3371              (bfd_vma) s->filepos + s->_raw_size <= p->p_offset + p->p_filesz)
3372
3373   /* The complicated case when p_vaddr is 0 is to handle the Solaris
3374      linker, which generates a PT_INTERP section with p_vaddr and
3375      p_memsz set to 0.  */
3376
3377 #define IS_SOLARIS_PT_INTERP(p, s)                                      \
3378             (p->p_vaddr == 0                                            \
3379              && p->p_filesz > 0                                         \
3380              && (s->flags & SEC_HAS_CONTENTS) != 0                      \
3381              && s->_raw_size > 0                                        \
3382              && (bfd_vma) s->filepos >= p->p_offset                     \
3383              && ((bfd_vma) s->filepos + s->_raw_size                    \
3384                      <= p->p_offset + p->p_filesz))
3385
3386   /* Scan through the segments specified in the program header
3387      of the input BFD.  */
3388   for (i = 0, p = elf_tdata (ibfd)->phdr; i < num_segments; i++, p++)
3389     {
3390       unsigned int csecs;
3391       asection *s;
3392       asection **sections;
3393       asection *os;
3394       unsigned int isec;
3395       bfd_vma matching_lma;
3396       bfd_vma suggested_lma;
3397       unsigned int j;
3398
3399       /* For each section in the input BFD, decide if it should be
3400          included in the current segment.  A section will be included
3401          if it is within the address space of the segment, and it is
3402          an allocated segment, and there is an output section
3403          associated with it.  */
3404       csecs = 0;
3405       for (s = ibfd->sections; s != NULL; s = s->next)
3406         if (s->output_section != NULL)
3407           {
3408             if ((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p) ||
3409                  IS_SOLARIS_PT_INTERP (p, s)) &&
3410                 (s->flags & SEC_ALLOC) != 0)
3411               ++csecs;
3412             else if (IS_COREFILE_NOTE (p, s))
3413               ++csecs;
3414           }
3415
3416       /* Allocate a segment map big enough to contain all of the
3417          sections we have selected.  */
3418       m = ((struct elf_segment_map *)
3419            bfd_alloc (obfd,
3420                       (sizeof (struct elf_segment_map)
3421                        + ((size_t) csecs - 1) * sizeof (asection *))));
3422       if (m == NULL)
3423         return false;
3424
3425       /* Initialise the fields of the segment map.  Default to
3426          using the physical address of the segment in the input BFD.  */
3427       m->next          = NULL;
3428       m->p_type        = p->p_type;
3429       m->p_flags       = p->p_flags;
3430       m->p_flags_valid = 1;
3431       m->p_paddr       = p->p_paddr;
3432       m->p_paddr_valid = 1;
3433
3434       /* Determine if this segment contains the ELF file header
3435          and if it contains the program headers themselves.  */
3436       m->includes_filehdr = (p->p_offset == 0
3437                              && p->p_filesz >= iehdr->e_ehsize);
3438
3439       if (! phdr_included || p->p_type != PT_LOAD)
3440         {
3441           m->includes_phdrs =
3442             (p->p_offset <= (bfd_vma) iehdr->e_phoff
3443              && (p->p_offset + p->p_filesz
3444                  >= ((bfd_vma) iehdr->e_phoff
3445                      + iehdr->e_phnum * iehdr->e_phentsize)));
3446           if (p->p_type == PT_LOAD && m->includes_phdrs)
3447             phdr_included = true;
3448         }
3449
3450       if (csecs == 0)
3451         {
3452           /* Special segments, such as the PT_PHDR segment, may contain
3453              no sections, but ordinary, loadable segments should contain
3454              something.  */
3455           
3456           if (p->p_type == PT_LOAD)
3457               _bfd_error_handler
3458                 (_("%s: warning: Empty loadable segment detected\n"),
3459                  bfd_get_filename (ibfd));
3460           
3461           m->count = 0;
3462           *pm = m;
3463           pm = &m->next;
3464           
3465           continue;
3466         }
3467       
3468       /* Now scan the sections in the input BFD again and attempt
3469          to add their corresponding output sections to the segment map.
3470          The problem here is how to handle an output section which has
3471          been moved (ie had its LMA changed).  There are four possibilities:
3472
3473          1. None of the sections have been moved.
3474             In this case we can continue to use the segment LMA from the
3475             input BFD.
3476             
3477          2. All of the sections have been moved by the same amount.
3478             In this case we can change the segment's LMA to match the LMA
3479             of the first section.
3480
3481          3. Some of the sections have been moved, others have not.
3482             In this case those sections which have not been moved can be
3483             placed in the current segment which will have to have its size,
3484             and possibly its LMA changed, and a new segment or segments will
3485             have to be created to contain the other sections.
3486
3487          4. The sections have been moved, but not be the same amount.
3488             In this case we can change the segment's LMA to match the LMA
3489             of the first section and we will have to create a new segment
3490             or segments to contain the other sections. 
3491             
3492          In order to save time, we allocate an array to hold the section
3493          pointers that we are interested in.  As these sections get assigned
3494          to a segment, they are removed from this array.  */
3495       
3496       sections = (asection **) bfd_malloc (sizeof (asection *) * csecs);
3497       if (sections == NULL)
3498         return false;
3499       
3500       /* Step One: Scan for segment vs section LMA conflicts.
3501          Also add the sections to the section array allocated above.
3502          Also add the sections to the current segment.  In the common
3503          case, where the sections have not been moved, this means that
3504          we have completely filled the segment, and there is nothing
3505          more to do.  */
3506       
3507       isec = 0;
3508       matching_lma = false;
3509       suggested_lma = 0;
3510
3511       for (j = 0, s = ibfd->sections; s != NULL; s = s->next)
3512         {
3513           os = s->output_section;
3514           
3515           if ((((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3516                  || IS_SOLARIS_PT_INTERP (p, s))
3517                 && (s->flags & SEC_ALLOC) != 0) 
3518                || IS_COREFILE_NOTE (p, s))
3519               && os != NULL)
3520             {
3521               sections[j++] = s;
3522               
3523               /* The Solaris native linker always sets p_paddr to 0.
3524                  We try to catch that case here, and set it to the
3525                  correct value.  */
3526               if (p->p_paddr == 0
3527                   && p->p_vaddr != 0
3528                   && isec == 0
3529                   && os->lma != 0
3530                   && (os->vma == (p->p_vaddr
3531                                   + (m->includes_filehdr
3532                                      ? iehdr->e_ehsize
3533                                      : 0)
3534                                   + (m->includes_phdrs
3535                                      ? iehdr->e_phnum * iehdr->e_phentsize
3536                                      : 0))))
3537                 m->p_paddr = p->p_vaddr;
3538
3539               /* Match up the physical address of the segment with the
3540                  LMA address of the output section.  */
3541               if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p) ||
3542                   IS_COREFILE_NOTE (p, s))
3543                 {
3544                   if (matching_lma == 0)
3545                     matching_lma = os->lma;
3546                   
3547                   /* We assume that if the section fits within the segment
3548                      that it does not overlap any other section within that
3549                      segment.  */
3550                   m->sections[isec++] = os;
3551                 }
3552               else if (suggested_lma == 0)
3553                 suggested_lma = os->lma;
3554             }
3555         }
3556
3557       BFD_ASSERT (j == csecs);
3558
3559       /* Step Two: Adjust the physical address of the current segment,
3560          if necessary.  */
3561       if (isec == csecs)
3562         {
3563           /* All of the sections fitted within the segment as currently
3564              specified.  This is the default case.  Add the segment to
3565              the list of built segments and carry on to process the next
3566              program header in the input BFD.  */
3567           m->count = csecs;
3568           *pm = m;
3569           pm = &m->next;
3570
3571           free (sections);
3572           continue;
3573         }
3574       else if (matching_lma != 0)
3575         {
3576           /* At least one section fits inside the current segment.
3577              Keep it, but modify its physical address to match the
3578              LMA of the first section that fitted.  */
3579
3580           m->p_paddr = matching_lma;
3581         }
3582       else 
3583         {
3584           /* None of the sections fitted inside the current segment.
3585              Change the current segment's physical address to match
3586              the LMA of the first section.  */
3587
3588           m->p_paddr = suggested_lma;
3589         }
3590
3591       /* Step Three: Loop over the sections again, this time assigning
3592          those that fit to the current segment and remvoing them from the
3593          sections array; but making sure not to leave large gaps.  Once all
3594          possible sections have been assigned to the current segment it is
3595          added to the list of built segments and if sections still remain
3596          to be assigned, a new segment is constructed before repeating
3597          the loop.  */
3598       isec = 0;
3599       do
3600         {
3601           m->count = 0;
3602           suggested_lma = 0;
3603           
3604           /* Fill the current segment with sections that fit.  */
3605           for (j = 0; j < csecs; j++)
3606             {
3607               s = sections[j];
3608               
3609               if (s == NULL)
3610                 continue;
3611               
3612               os = s->output_section;
3613               
3614               if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p) ||
3615                   IS_COREFILE_NOTE (p, s))
3616                 {
3617                   if (m->count == 0)
3618                     {
3619                       /* If the first section in a segment does not start at
3620                          the beginning of the segment, then something is wrong.  */
3621                       if (os->lma != m->p_paddr)
3622                         abort ();
3623                     }
3624                   else 
3625                     {
3626                       asection * prev_sec;
3627                       bfd_vma maxpagesize;
3628                       
3629                       prev_sec = m->sections[m->count - 1];
3630                       maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3631
3632                       /* If the gap between the end of the previous section
3633                          and the start of this section is more than maxpagesize
3634                          then we need to start a new segment.  */
3635                       if (BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
3636                           < BFD_ALIGN (os->lma, maxpagesize))
3637                         {
3638                           if (suggested_lma == 0)
3639                             suggested_lma = os->lma;
3640                           
3641                           continue;
3642                         }
3643                     }
3644
3645                   m->sections[m->count++] = os;
3646                   ++isec;
3647                   sections[j] = NULL;
3648                 }
3649               else if (suggested_lma == 0)
3650                 suggested_lma = os->lma;
3651             }
3652
3653           BFD_ASSERT (m->count > 0);
3654           
3655           /* Add the current segment to the list of built segments.  */
3656           *pm = m;
3657           pm = &m->next;
3658
3659           if (isec < csecs)
3660             {
3661               /* We still have not allocated all of the sections to
3662                  segments.  Create a new segment here, initialise it
3663                  and carry on looping.  */
3664
3665               m = ((struct elf_segment_map *)
3666                    bfd_alloc (obfd,
3667                               (sizeof (struct elf_segment_map)
3668                                + ((size_t) csecs - 1) * sizeof (asection *))));
3669               if (m == NULL)
3670                 return false;
3671
3672               /* Initialise the fields of the segment map.  Set the physical
3673                  physical address to the LMA of the first section that has
3674                  not yet been assigned.  */
3675               
3676               m->next             = NULL;
3677               m->p_type           = p->p_type;
3678               m->p_flags          = p->p_flags;
3679               m->p_flags_valid    = 1;
3680               m->p_paddr          = suggested_lma;
3681               m->p_paddr_valid    = 1;
3682               m->includes_filehdr = 0;
3683               m->includes_phdrs   = 0;
3684             }
3685         }
3686       while (isec < csecs);
3687
3688       free (sections);
3689     }
3690
3691   /* The Solaris linker creates program headers in which all the
3692      p_paddr fields are zero.  When we try to objcopy or strip such a
3693      file, we get confused.  Check for this case, and if we find it
3694      reset the p_paddr_valid fields.  */
3695   for (m = mfirst; m != NULL; m = m->next)
3696     if (m->p_paddr != 0)
3697       break;
3698   if (m == NULL)
3699     {
3700       for (m = mfirst; m != NULL; m = m->next)
3701         m->p_paddr_valid = 0;
3702     }
3703
3704   elf_tdata (obfd)->segment_map = mfirst;
3705
3706 #if 0
3707   /* Final Step: Sort the segments into ascending order of physical address. */
3708   if (mfirst != NULL)
3709     {
3710       struct elf_segment_map* prev;
3711       
3712       prev = mfirst;
3713       for (m = mfirst->next; m != NULL; prev = m, m = m->next)
3714         {
3715           /* Yes I know - its a bubble sort....*/
3716           if (m->next != NULL && (m->next->p_paddr < m->p_paddr))
3717             {
3718               /* swap m and m->next */
3719               prev->next = m->next;
3720               m->next = m->next->next;
3721               prev->next->next = m;
3722
3723               /* restart loop. */
3724               m = mfirst;
3725             }
3726         }
3727     }
3728 #endif
3729   
3730 #undef IS_CONTAINED_BY
3731 #undef IS_SOLARIS_PT_INTERP
3732 #undef IS_COREFILE_NOTE
3733   return true;
3734 }
3735
3736 /* Copy private section information.  This copies over the entsize
3737    field, and sometimes the info field.  */
3738
3739 boolean
3740 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
3741      bfd *ibfd;
3742      asection *isec;
3743      bfd *obfd;
3744      asection *osec;
3745 {
3746   Elf_Internal_Shdr *ihdr, *ohdr;
3747
3748   if (ibfd->xvec->flavour != bfd_target_elf_flavour
3749       || obfd->xvec->flavour != bfd_target_elf_flavour)
3750     return true;
3751
3752   /* Copy over private BFD data if it has not already been copied.
3753      This must be done here, rather than in the copy_private_bfd_data
3754      entry point, because the latter is called after the section
3755      contents have been set, which means that the program headers have
3756      already been worked out.  */
3757   if (elf_tdata (obfd)->segment_map == NULL
3758       && elf_tdata (ibfd)->phdr != NULL)
3759     {
3760       asection *s;
3761
3762       /* Only set up the segments if there are no more SEC_ALLOC
3763          sections.  FIXME: This won't do the right thing if objcopy is
3764          used to remove the last SEC_ALLOC section, since objcopy
3765          won't call this routine in that case.  */
3766       for (s = isec->next; s != NULL; s = s->next)
3767         if ((s->flags & SEC_ALLOC) != 0)
3768           break;
3769       if (s == NULL)
3770         {
3771           if (! copy_private_bfd_data (ibfd, obfd))
3772             return false;
3773         }
3774     }
3775
3776   ihdr = &elf_section_data (isec)->this_hdr;
3777   ohdr = &elf_section_data (osec)->this_hdr;
3778
3779   ohdr->sh_entsize = ihdr->sh_entsize;
3780
3781   if (ihdr->sh_type == SHT_SYMTAB
3782       || ihdr->sh_type == SHT_DYNSYM
3783       || ihdr->sh_type == SHT_GNU_verneed
3784       || ihdr->sh_type == SHT_GNU_verdef)
3785     ohdr->sh_info = ihdr->sh_info;
3786
3787   return true;
3788 }
3789
3790 /* Copy private symbol information.  If this symbol is in a section
3791    which we did not map into a BFD section, try to map the section
3792    index correctly.  We use special macro definitions for the mapped
3793    section indices; these definitions are interpreted by the
3794    swap_out_syms function.  */
3795
3796 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
3797 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
3798 #define MAP_STRTAB (SHN_LORESERVE - 3)
3799 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
3800
3801 boolean
3802 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
3803      bfd *ibfd;
3804      asymbol *isymarg;
3805      bfd *obfd;
3806      asymbol *osymarg;
3807 {
3808   elf_symbol_type *isym, *osym;
3809
3810   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3811       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3812     return true;
3813
3814   isym = elf_symbol_from (ibfd, isymarg);
3815   osym = elf_symbol_from (obfd, osymarg);
3816
3817   if (isym != NULL
3818       && osym != NULL
3819       && bfd_is_abs_section (isym->symbol.section))
3820     {
3821       unsigned int shndx;
3822
3823       shndx = isym->internal_elf_sym.st_shndx;
3824       if (shndx == elf_onesymtab (ibfd))
3825         shndx = MAP_ONESYMTAB;
3826       else if (shndx == elf_dynsymtab (ibfd))
3827         shndx = MAP_DYNSYMTAB;
3828       else if (shndx == elf_tdata (ibfd)->strtab_section)
3829         shndx = MAP_STRTAB;
3830       else if (shndx == elf_tdata (ibfd)->shstrtab_section)
3831         shndx = MAP_SHSTRTAB;
3832       osym->internal_elf_sym.st_shndx = shndx;
3833     }
3834
3835   return true;
3836 }
3837
3838 /* Swap out the symbols.  */
3839
3840 static boolean
3841 swap_out_syms (abfd, sttp, relocatable_p)
3842      bfd *abfd;
3843      struct bfd_strtab_hash **sttp;
3844      int relocatable_p;
3845 {
3846   struct elf_backend_data *bed = get_elf_backend_data (abfd);
3847
3848   if (!elf_map_symbols (abfd))
3849     return false;
3850
3851   /* Dump out the symtabs. */
3852   {
3853     int symcount = bfd_get_symcount (abfd);
3854     asymbol **syms = bfd_get_outsymbols (abfd);
3855     struct bfd_strtab_hash *stt;
3856     Elf_Internal_Shdr *symtab_hdr;
3857     Elf_Internal_Shdr *symstrtab_hdr;
3858     char *outbound_syms;
3859     int idx;
3860
3861     stt = _bfd_elf_stringtab_init ();
3862     if (stt == NULL)
3863       return false;
3864
3865     symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3866     symtab_hdr->sh_type = SHT_SYMTAB;
3867     symtab_hdr->sh_entsize = bed->s->sizeof_sym;
3868     symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
3869     symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
3870     symtab_hdr->sh_addralign = bed->s->file_align;
3871
3872     symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3873     symstrtab_hdr->sh_type = SHT_STRTAB;
3874
3875     outbound_syms = bfd_alloc (abfd,
3876                                (1 + symcount) * bed->s->sizeof_sym);
3877     if (outbound_syms == NULL)
3878       return false;
3879     symtab_hdr->contents = (PTR) outbound_syms;
3880
3881     /* now generate the data (for "contents") */
3882     {
3883       /* Fill in zeroth symbol and swap it out.  */
3884       Elf_Internal_Sym sym;
3885       sym.st_name = 0;
3886       sym.st_value = 0;
3887       sym.st_size = 0;
3888       sym.st_info = 0;
3889       sym.st_other = 0;
3890       sym.st_shndx = SHN_UNDEF;
3891       bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
3892       outbound_syms += bed->s->sizeof_sym;
3893     }
3894     for (idx = 0; idx < symcount; idx++)
3895       {
3896         Elf_Internal_Sym sym;
3897         bfd_vma value = syms[idx]->value;
3898         elf_symbol_type *type_ptr;
3899         flagword flags = syms[idx]->flags;
3900         int type;
3901
3902         if (flags & BSF_SECTION_SYM)
3903           /* Section symbols have no names.  */
3904           sym.st_name = 0;
3905         else
3906           {
3907             sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
3908                                                               syms[idx]->name,
3909                                                               true, false);
3910             if (sym.st_name == (unsigned long) -1)
3911               return false;
3912           }
3913
3914         type_ptr = elf_symbol_from (abfd, syms[idx]);
3915
3916         if (bfd_is_com_section (syms[idx]->section))
3917           {
3918             /* ELF common symbols put the alignment into the `value' field,
3919                and the size into the `size' field.  This is backwards from
3920                how BFD handles it, so reverse it here.  */
3921             sym.st_size = value;
3922             if (type_ptr == NULL
3923                 || type_ptr->internal_elf_sym.st_value == 0)
3924               sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
3925             else
3926               sym.st_value = type_ptr->internal_elf_sym.st_value;
3927             sym.st_shndx = _bfd_elf_section_from_bfd_section
3928               (abfd, syms[idx]->section);
3929           }
3930         else
3931           {
3932             asection *sec = syms[idx]->section;
3933             int shndx;
3934
3935             if (sec->output_section)
3936               {
3937                 value += sec->output_offset;
3938                 sec = sec->output_section;
3939               }
3940             /* Don't add in the section vma for relocatable output.  */
3941             if (! relocatable_p)
3942               value += sec->vma;
3943             sym.st_value = value;
3944             sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
3945
3946             if (bfd_is_abs_section (sec)
3947                 && type_ptr != NULL
3948                 && type_ptr->internal_elf_sym.st_shndx != 0)
3949               {
3950                 /* This symbol is in a real ELF section which we did
3951                    not create as a BFD section.  Undo the mapping done
3952                    by copy_private_symbol_data.  */
3953                 shndx = type_ptr->internal_elf_sym.st_shndx;
3954                 switch (shndx)
3955                   {
3956                   case MAP_ONESYMTAB:
3957                     shndx = elf_onesymtab (abfd);
3958                     break;
3959                   case MAP_DYNSYMTAB:
3960                     shndx = elf_dynsymtab (abfd);
3961                     break;
3962                   case MAP_STRTAB:
3963                     shndx = elf_tdata (abfd)->strtab_section;
3964                     break;
3965                   case MAP_SHSTRTAB:
3966                     shndx = elf_tdata (abfd)->shstrtab_section;
3967                     break;
3968                   default:
3969                     break;
3970                   }
3971               }
3972             else
3973               {
3974                 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3975
3976                 if (shndx == -1)
3977                   {
3978                     asection *sec2;
3979
3980                     /* Writing this would be a hell of a lot easier if
3981                        we had some decent documentation on bfd, and
3982                        knew what to expect of the library, and what to
3983                        demand of applications.  For example, it
3984                        appears that `objcopy' might not set the
3985                        section of a symbol to be a section that is
3986                        actually in the output file.  */
3987                     sec2 = bfd_get_section_by_name (abfd, sec->name);
3988                     BFD_ASSERT (sec2 != 0);
3989                     shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
3990                     BFD_ASSERT (shndx != -1);
3991                   }
3992               }
3993
3994             sym.st_shndx = shndx;
3995           }
3996
3997         if ((flags & BSF_FUNCTION) != 0)
3998           type = STT_FUNC;
3999         else if ((flags & BSF_OBJECT) != 0)
4000           type = STT_OBJECT;
4001         else
4002           type = STT_NOTYPE;
4003
4004         if (bfd_is_com_section (syms[idx]->section))
4005           sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4006         else if (bfd_is_und_section (syms[idx]->section))
4007           sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4008                                       ? STB_WEAK
4009                                       : STB_GLOBAL),
4010                                      type);
4011         else if (flags & BSF_SECTION_SYM)
4012           sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4013         else if (flags & BSF_FILE)
4014           sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4015         else
4016           {
4017             int bind = STB_LOCAL;
4018
4019             if (flags & BSF_LOCAL)
4020               bind = STB_LOCAL;
4021             else if (flags & BSF_WEAK)
4022               bind = STB_WEAK;
4023             else if (flags & BSF_GLOBAL)
4024               bind = STB_GLOBAL;
4025
4026             sym.st_info = ELF_ST_INFO (bind, type);
4027           }
4028
4029         if (type_ptr != NULL)
4030           sym.st_other = type_ptr->internal_elf_sym.st_other;
4031         else
4032           sym.st_other = 0;
4033
4034         bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4035         outbound_syms += bed->s->sizeof_sym;
4036       }
4037
4038     *sttp = stt;
4039     symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4040     symstrtab_hdr->sh_type = SHT_STRTAB;
4041
4042     symstrtab_hdr->sh_flags = 0;
4043     symstrtab_hdr->sh_addr = 0;
4044     symstrtab_hdr->sh_entsize = 0;
4045     symstrtab_hdr->sh_link = 0;
4046     symstrtab_hdr->sh_info = 0;
4047     symstrtab_hdr->sh_addralign = 1;
4048   }
4049
4050   return true;
4051 }
4052
4053 /* Return the number of bytes required to hold the symtab vector.
4054
4055    Note that we base it on the count plus 1, since we will null terminate
4056    the vector allocated based on this size.  However, the ELF symbol table
4057    always has a dummy entry as symbol #0, so it ends up even.  */
4058
4059 long
4060 _bfd_elf_get_symtab_upper_bound (abfd)
4061      bfd *abfd;
4062 {
4063   long symcount;
4064   long symtab_size;
4065   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4066
4067   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4068   symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4069
4070   return symtab_size;
4071 }
4072
4073 long
4074 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4075      bfd *abfd;
4076 {
4077   long symcount;
4078   long symtab_size;
4079   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4080
4081   if (elf_dynsymtab (abfd) == 0)
4082     {
4083       bfd_set_error (bfd_error_invalid_operation);
4084       return -1;
4085     }
4086
4087   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4088   symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4089
4090   return symtab_size;
4091 }
4092
4093 long
4094 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4095      bfd *abfd;
4096      sec_ptr asect;
4097 {
4098   return (asect->reloc_count + 1) * sizeof (arelent *);
4099 }
4100
4101 /* Canonicalize the relocs.  */
4102
4103 long
4104 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4105      bfd *abfd;
4106      sec_ptr section;
4107      arelent **relptr;
4108      asymbol **symbols;
4109 {
4110   arelent *tblptr;
4111   unsigned int i;
4112
4113   if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4114                                                            section,
4115                                                            symbols,
4116                                                            false))
4117     return -1;
4118
4119   tblptr = section->relocation;
4120   for (i = 0; i < section->reloc_count; i++)
4121     *relptr++ = tblptr++;
4122
4123   *relptr = NULL;
4124
4125   return section->reloc_count;
4126 }
4127
4128 long
4129 _bfd_elf_get_symtab (abfd, alocation)
4130      bfd *abfd;
4131      asymbol **alocation;
4132 {
4133   long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4134     (abfd, alocation, false);
4135
4136   if (symcount >= 0)
4137     bfd_get_symcount (abfd) = symcount;
4138   return symcount;
4139 }
4140
4141 long
4142 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4143      bfd *abfd;
4144      asymbol **alocation;
4145 {
4146   return get_elf_backend_data (abfd)->s->slurp_symbol_table
4147     (abfd, alocation, true);
4148 }
4149
4150 /* Return the size required for the dynamic reloc entries.  Any
4151    section that was actually installed in the BFD, and has type
4152    SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4153    considered to be a dynamic reloc section.  */
4154
4155 long
4156 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4157      bfd *abfd;
4158 {
4159   long ret;
4160   asection *s;
4161
4162   if (elf_dynsymtab (abfd) == 0)
4163     {
4164       bfd_set_error (bfd_error_invalid_operation);
4165       return -1;
4166     }
4167
4168   ret = sizeof (arelent *);
4169   for (s = abfd->sections; s != NULL; s = s->next)
4170     if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4171         && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4172             || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4173       ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4174               * sizeof (arelent *));
4175
4176   return ret;
4177 }
4178
4179 /* Canonicalize the dynamic relocation entries.  Note that we return
4180    the dynamic relocations as a single block, although they are
4181    actually associated with particular sections; the interface, which
4182    was designed for SunOS style shared libraries, expects that there
4183    is only one set of dynamic relocs.  Any section that was actually
4184    installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4185    the dynamic symbol table, is considered to be a dynamic reloc
4186    section.  */
4187
4188 long
4189 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4190      bfd *abfd;
4191      arelent **storage;
4192      asymbol **syms;
4193 {
4194   boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4195   asection *s;
4196   long ret;
4197
4198   if (elf_dynsymtab (abfd) == 0)
4199     {
4200       bfd_set_error (bfd_error_invalid_operation);
4201       return -1;
4202     }
4203
4204   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4205   ret = 0;
4206   for (s = abfd->sections; s != NULL; s = s->next)
4207     {
4208       if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4209           && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4210               || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4211         {
4212           arelent *p;
4213           long count, i;
4214
4215           if (! (*slurp_relocs) (abfd, s, syms, true))
4216             return -1;
4217           count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4218           p = s->relocation;
4219           for (i = 0; i < count; i++)
4220             *storage++ = p++;
4221           ret += count;
4222         }
4223     }
4224
4225   *storage = NULL;
4226
4227   return ret;
4228 }
4229 \f
4230 /* Read in the version information.  */
4231
4232 boolean
4233 _bfd_elf_slurp_version_tables (abfd)
4234      bfd *abfd;
4235 {
4236   bfd_byte *contents = NULL;
4237
4238   if (elf_dynverdef (abfd) != 0)
4239     {
4240       Elf_Internal_Shdr *hdr;
4241       Elf_External_Verdef *everdef;
4242       Elf_Internal_Verdef *iverdef;
4243       unsigned int i;
4244
4245       hdr = &elf_tdata (abfd)->dynverdef_hdr;
4246
4247       elf_tdata (abfd)->verdef =
4248         ((Elf_Internal_Verdef *)
4249          bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef)));
4250       if (elf_tdata (abfd)->verdef == NULL)
4251         goto error_return;
4252
4253       elf_tdata (abfd)->cverdefs = hdr->sh_info;
4254
4255       contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4256       if (contents == NULL)
4257         goto error_return;
4258       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4259           || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4260         goto error_return;
4261
4262       everdef = (Elf_External_Verdef *) contents;
4263       iverdef = elf_tdata (abfd)->verdef;
4264       for (i = 0; i < hdr->sh_info; i++, iverdef++)
4265         {
4266           Elf_External_Verdaux *everdaux;
4267           Elf_Internal_Verdaux *iverdaux;
4268           unsigned int j;
4269
4270           _bfd_elf_swap_verdef_in (abfd, everdef, iverdef);
4271
4272           iverdef->vd_bfd = abfd;
4273
4274           iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4275                                 bfd_alloc (abfd,
4276                                            (iverdef->vd_cnt
4277                                             * sizeof (Elf_Internal_Verdaux))));
4278           if (iverdef->vd_auxptr == NULL)
4279             goto error_return;
4280
4281           everdaux = ((Elf_External_Verdaux *)
4282                       ((bfd_byte *) everdef + iverdef->vd_aux));
4283           iverdaux = iverdef->vd_auxptr;
4284           for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4285             {
4286               _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4287
4288               iverdaux->vda_nodename =
4289                 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4290                                                  iverdaux->vda_name);
4291               if (iverdaux->vda_nodename == NULL)
4292                 goto error_return;
4293
4294               if (j + 1 < iverdef->vd_cnt)
4295                 iverdaux->vda_nextptr = iverdaux + 1;
4296               else
4297                 iverdaux->vda_nextptr = NULL;
4298
4299               everdaux = ((Elf_External_Verdaux *)
4300                           ((bfd_byte *) everdaux + iverdaux->vda_next));
4301             }
4302
4303           iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4304
4305           if (i + 1 < hdr->sh_info)
4306             iverdef->vd_nextdef = iverdef + 1;
4307           else
4308             iverdef->vd_nextdef = NULL;
4309
4310           everdef = ((Elf_External_Verdef *)
4311                      ((bfd_byte *) everdef + iverdef->vd_next));
4312         }
4313
4314       free (contents);
4315       contents = NULL;
4316     }
4317
4318   if (elf_dynverref (abfd) != 0)
4319     {
4320       Elf_Internal_Shdr *hdr;
4321       Elf_External_Verneed *everneed;
4322       Elf_Internal_Verneed *iverneed;
4323       unsigned int i;
4324
4325       hdr = &elf_tdata (abfd)->dynverref_hdr;
4326
4327       elf_tdata (abfd)->verref =
4328         ((Elf_Internal_Verneed *)
4329          bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4330       if (elf_tdata (abfd)->verref == NULL)
4331         goto error_return;
4332
4333       elf_tdata (abfd)->cverrefs = hdr->sh_info;
4334
4335       contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4336       if (contents == NULL)
4337         goto error_return;
4338       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4339           || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4340         goto error_return;
4341
4342       everneed = (Elf_External_Verneed *) contents;
4343       iverneed = elf_tdata (abfd)->verref;
4344       for (i = 0; i < hdr->sh_info; i++, iverneed++)
4345         {
4346           Elf_External_Vernaux *evernaux;
4347           Elf_Internal_Vernaux *ivernaux;
4348           unsigned int j;
4349
4350           _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4351
4352           iverneed->vn_bfd = abfd;
4353
4354           iverneed->vn_filename =
4355             bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4356                                              iverneed->vn_file);
4357           if (iverneed->vn_filename == NULL)
4358             goto error_return;
4359
4360           iverneed->vn_auxptr =
4361             ((Elf_Internal_Vernaux *)
4362              bfd_alloc (abfd,
4363                         iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4364
4365           evernaux = ((Elf_External_Vernaux *)
4366                       ((bfd_byte *) everneed + iverneed->vn_aux));
4367           ivernaux = iverneed->vn_auxptr;
4368           for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4369             {
4370               _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4371
4372               ivernaux->vna_nodename =
4373                 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4374                                                  ivernaux->vna_name);
4375               if (ivernaux->vna_nodename == NULL)
4376                 goto error_return;
4377
4378               if (j + 1 < iverneed->vn_cnt)
4379                 ivernaux->vna_nextptr = ivernaux + 1;
4380               else
4381                 ivernaux->vna_nextptr = NULL;
4382
4383               evernaux = ((Elf_External_Vernaux *)
4384                           ((bfd_byte *) evernaux + ivernaux->vna_next));
4385             }
4386
4387           if (i + 1 < hdr->sh_info)
4388             iverneed->vn_nextref = iverneed + 1;
4389           else
4390             iverneed->vn_nextref = NULL;
4391
4392           everneed = ((Elf_External_Verneed *)
4393                       ((bfd_byte *) everneed + iverneed->vn_next));
4394         }
4395
4396       free (contents);
4397       contents = NULL;
4398     }
4399
4400   return true;
4401
4402  error_return:
4403   if (contents == NULL)
4404     free (contents);
4405   return false;
4406 }
4407 \f
4408 asymbol *
4409 _bfd_elf_make_empty_symbol (abfd)
4410      bfd *abfd;
4411 {
4412   elf_symbol_type *newsym;
4413
4414   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4415   if (!newsym)
4416     return NULL;
4417   else
4418     {
4419       newsym->symbol.the_bfd = abfd;
4420       return &newsym->symbol;
4421     }
4422 }
4423
4424 void
4425 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4426      bfd *ignore_abfd;
4427      asymbol *symbol;
4428      symbol_info *ret;
4429 {
4430   bfd_symbol_info (symbol, ret);
4431 }
4432
4433 /* Return whether a symbol name implies a local symbol.  Most targets
4434    use this function for the is_local_label_name entry point, but some
4435    override it.  */
4436
4437 boolean
4438 _bfd_elf_is_local_label_name (abfd, name)
4439      bfd *abfd;
4440      const char *name;
4441 {
4442   /* Normal local symbols start with ``.L''.  */
4443   if (name[0] == '.' && name[1] == 'L')
4444     return true;
4445
4446   /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4447      DWARF debugging symbols starting with ``..''.  */
4448   if (name[0] == '.' && name[1] == '.')
4449     return true;
4450
4451   /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4452      emitting DWARF debugging output.  I suspect this is actually a
4453      small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4454      ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4455      underscore to be emitted on some ELF targets).  For ease of use,
4456      we treat such symbols as local.  */
4457   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4458     return true;
4459
4460   return false;
4461 }
4462
4463 alent *
4464 _bfd_elf_get_lineno (ignore_abfd, symbol)
4465      bfd *ignore_abfd;
4466      asymbol *symbol;
4467 {
4468   abort ();
4469   return NULL;
4470 }
4471
4472 boolean
4473 _bfd_elf_set_arch_mach (abfd, arch, machine)
4474      bfd *abfd;
4475      enum bfd_architecture arch;
4476      unsigned long machine;
4477 {
4478   /* If this isn't the right architecture for this backend, and this
4479      isn't the generic backend, fail.  */
4480   if (arch != get_elf_backend_data (abfd)->arch
4481       && arch != bfd_arch_unknown
4482       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4483     return false;
4484
4485   return bfd_default_set_arch_mach (abfd, arch, machine);
4486 }
4487
4488 /* Find the nearest line to a particular section and offset, for error
4489    reporting.  */
4490
4491 boolean
4492 _bfd_elf_find_nearest_line (abfd,
4493                             section,
4494                             symbols,
4495                             offset,
4496                             filename_ptr,
4497                             functionname_ptr,
4498                             line_ptr)
4499      bfd *abfd;
4500      asection *section;
4501      asymbol **symbols;
4502      bfd_vma offset;
4503      CONST char **filename_ptr;
4504      CONST char **functionname_ptr;
4505      unsigned int *line_ptr;
4506 {
4507   boolean found;
4508   const char *filename;
4509   asymbol *func;
4510   bfd_vma low_func;
4511   asymbol **p;
4512
4513   if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4514                                      filename_ptr, functionname_ptr, 
4515                                      line_ptr))
4516     return true;
4517
4518   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4519                                              &found, filename_ptr,
4520                                              functionname_ptr, line_ptr,
4521                                              &elf_tdata (abfd)->line_info))
4522     return false;
4523   if (found)
4524     return true;
4525
4526   if (symbols == NULL)
4527     return false;
4528
4529   filename = NULL;
4530   func = NULL;
4531   low_func = 0;
4532
4533   for (p = symbols; *p != NULL; p++)
4534     {
4535       elf_symbol_type *q;
4536
4537       q = (elf_symbol_type *) *p;
4538
4539       if (bfd_get_section (&q->symbol) != section)
4540         continue;
4541
4542       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4543         {
4544         default:
4545           break;
4546         case STT_FILE:
4547           filename = bfd_asymbol_name (&q->symbol);
4548           break;
4549         case STT_NOTYPE:
4550         case STT_FUNC:
4551           if (q->symbol.section == section
4552               && q->symbol.value >= low_func
4553               && q->symbol.value <= offset)
4554             {
4555               func = (asymbol *) q;
4556               low_func = q->symbol.value;
4557             }
4558           break;
4559         }
4560     }
4561
4562   if (func == NULL)
4563     return false;
4564
4565   *filename_ptr = filename;
4566   *functionname_ptr = bfd_asymbol_name (func);
4567   *line_ptr = 0;
4568   return true;
4569 }
4570
4571 int
4572 _bfd_elf_sizeof_headers (abfd, reloc)
4573      bfd *abfd;
4574      boolean reloc;
4575 {
4576   int ret;
4577
4578   ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
4579   if (! reloc)
4580     ret += get_program_header_size (abfd);
4581   return ret;
4582 }
4583
4584 boolean
4585 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
4586      bfd *abfd;
4587      sec_ptr section;
4588      PTR location;
4589      file_ptr offset;
4590      bfd_size_type count;
4591 {
4592   Elf_Internal_Shdr *hdr;
4593
4594   if (! abfd->output_has_begun
4595       && ! _bfd_elf_compute_section_file_positions
4596       (abfd, (struct bfd_link_info *) NULL))
4597     return false;
4598
4599   hdr = &elf_section_data (section)->this_hdr;
4600
4601   if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
4602     return false;
4603   if (bfd_write (location, 1, count, abfd) != count)
4604     return false;
4605
4606   return true;
4607 }
4608
4609 void
4610 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
4611      bfd *abfd;
4612      arelent *cache_ptr;
4613      Elf_Internal_Rela *dst;
4614 {
4615   abort ();
4616 }
4617
4618 #if 0
4619 void
4620 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
4621      bfd *abfd;
4622      arelent *cache_ptr;
4623      Elf_Internal_Rel *dst;
4624 {
4625   abort ();
4626 }
4627 #endif
4628
4629 /* Try to convert a non-ELF reloc into an ELF one.  */
4630
4631 boolean
4632 _bfd_elf_validate_reloc (abfd, areloc)
4633      bfd *abfd;
4634      arelent *areloc;
4635 {
4636   /* Check whether we really have an ELF howto. */
4637
4638   if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) 
4639     {
4640       bfd_reloc_code_real_type code;
4641       reloc_howto_type *howto;
4642       
4643       /* Alien reloc: Try to determine its type to replace it with an
4644          equivalent ELF reloc. */
4645
4646       if (areloc->howto->pc_relative)
4647         {
4648           switch (areloc->howto->bitsize)
4649             {
4650             case 8:
4651               code = BFD_RELOC_8_PCREL; 
4652               break;
4653             case 12:
4654               code = BFD_RELOC_12_PCREL; 
4655               break;
4656             case 16:
4657               code = BFD_RELOC_16_PCREL; 
4658               break;
4659             case 24:
4660               code = BFD_RELOC_24_PCREL; 
4661               break;
4662             case 32:
4663               code = BFD_RELOC_32_PCREL; 
4664               break;
4665             case 64:
4666               code = BFD_RELOC_64_PCREL; 
4667               break;
4668             default:
4669               goto fail;
4670             }
4671
4672           howto = bfd_reloc_type_lookup (abfd, code);
4673
4674           if (areloc->howto->pcrel_offset != howto->pcrel_offset)
4675             {
4676               if (howto->pcrel_offset)
4677                 areloc->addend += areloc->address;
4678               else
4679                 areloc->addend -= areloc->address; /* addend is unsigned!! */
4680             }
4681         }
4682       else
4683         {
4684           switch (areloc->howto->bitsize)
4685             {
4686             case 8:
4687               code = BFD_RELOC_8; 
4688               break;
4689             case 14:
4690               code = BFD_RELOC_14; 
4691               break;
4692             case 16:
4693               code = BFD_RELOC_16; 
4694               break;
4695             case 26:
4696               code = BFD_RELOC_26; 
4697               break;
4698             case 32:
4699               code = BFD_RELOC_32; 
4700               break;
4701             case 64:
4702               code = BFD_RELOC_64; 
4703               break;
4704             default:
4705               goto fail;
4706             }
4707
4708           howto = bfd_reloc_type_lookup (abfd, code);
4709         }
4710
4711       if (howto)
4712         areloc->howto = howto;
4713       else
4714         goto fail;
4715     }
4716
4717   return true;
4718
4719  fail:
4720   (*_bfd_error_handler)
4721     (_("%s: unsupported relocation type %s"),
4722      bfd_get_filename (abfd), areloc->howto->name);
4723   bfd_set_error (bfd_error_bad_value);
4724   return false;
4725 }
4726
4727 boolean
4728 _bfd_elf_close_and_cleanup (abfd)
4729      bfd *abfd;
4730 {
4731   if (bfd_get_format (abfd) == bfd_object)
4732     {
4733       if (elf_shstrtab (abfd) != NULL)
4734         _bfd_stringtab_free (elf_shstrtab (abfd));
4735     }
4736
4737   return _bfd_generic_close_and_cleanup (abfd);
4738 }
4739
4740 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
4741    in the relocation's offset.  Thus we cannot allow any sort of sanity
4742    range-checking to interfere.  There is nothing else to do in processing
4743    this reloc.  */
4744
4745 bfd_reloc_status_type
4746 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
4747      bfd *abfd;
4748      arelent *re;
4749      struct symbol_cache_entry *symbol;
4750      PTR data;
4751      asection *is;
4752      bfd *obfd;
4753      char **errmsg;
4754 {
4755   return bfd_reloc_ok;
4756 }