]> Git Repo - binutils.git/blob - bfd/elf.c
* cpu-mn10x00.c, elf32-mn10x00.c: New files.
[binutils.git] / bfd / elf.c
1 /* ELF executable support for BFD.
2    Copyright 1993, 1994, 1995, 1996 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 **));
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 /* Standard ELF hash function.  Do not change this function; you will
58    cause invalid hash tables to be generated.  (Well, you would if this
59    were being used yet.)  */
60 unsigned long
61 bfd_elf_hash (name)
62      CONST unsigned char *name;
63 {
64   unsigned long h = 0;
65   unsigned long g;
66   int ch;
67
68   while ((ch = *name++) != '\0')
69     {
70       h = (h << 4) + ch;
71       if ((g = (h & 0xf0000000)) != 0)
72         {
73           h ^= g >> 24;
74           h &= ~g;
75         }
76     }
77   return h;
78 }
79
80 /* Read a specified number of bytes at a specified offset in an ELF
81    file, into a newly allocated buffer, and return a pointer to the
82    buffer. */
83
84 static char *
85 elf_read (abfd, offset, size)
86      bfd * abfd;
87      long offset;
88      unsigned int size;
89 {
90   char *buf;
91
92   if ((buf = bfd_alloc (abfd, size)) == NULL)
93     return NULL;
94   if (bfd_seek (abfd, offset, SEEK_SET) == -1)
95     return NULL;
96   if (bfd_read ((PTR) buf, size, 1, abfd) != size)
97     {
98       if (bfd_get_error () != bfd_error_system_call)
99         bfd_set_error (bfd_error_file_truncated);
100       return NULL;
101     }
102   return buf;
103 }
104
105 boolean
106 elf_mkobject (abfd)
107      bfd * abfd;
108 {
109   /* this just does initialization */
110   /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
111   elf_tdata (abfd) = (struct elf_obj_tdata *)
112     bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
113   if (elf_tdata (abfd) == 0)
114     return false;
115   /* since everything is done at close time, do we need any
116      initialization? */
117
118   return true;
119 }
120
121 char *
122 bfd_elf_get_str_section (abfd, shindex)
123      bfd * abfd;
124      unsigned int shindex;
125 {
126   Elf_Internal_Shdr **i_shdrp;
127   char *shstrtab = NULL;
128   unsigned int offset;
129   unsigned int shstrtabsize;
130
131   i_shdrp = elf_elfsections (abfd);
132   if (i_shdrp == 0 || i_shdrp[shindex] == 0)
133     return 0;
134
135   shstrtab = (char *) i_shdrp[shindex]->contents;
136   if (shstrtab == NULL)
137     {
138       /* No cached one, attempt to read, and cache what we read. */
139       offset = i_shdrp[shindex]->sh_offset;
140       shstrtabsize = i_shdrp[shindex]->sh_size;
141       shstrtab = elf_read (abfd, offset, shstrtabsize);
142       i_shdrp[shindex]->contents = (PTR) shstrtab;
143     }
144   return shstrtab;
145 }
146
147 char *
148 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
149      bfd * abfd;
150      unsigned int shindex;
151      unsigned int strindex;
152 {
153   Elf_Internal_Shdr *hdr;
154
155   if (strindex == 0)
156     return "";
157
158   hdr = elf_elfsections (abfd)[shindex];
159
160   if (hdr->contents == NULL
161       && bfd_elf_get_str_section (abfd, shindex) == NULL)
162     return NULL;
163
164   return ((char *) hdr->contents) + strindex;
165 }
166
167 /* Make a BFD section from an ELF section.  We store a pointer to the
168    BFD section in the bfd_section field of the header.  */
169
170 boolean
171 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
172      bfd *abfd;
173      Elf_Internal_Shdr *hdr;
174      const char *name;
175 {
176   asection *newsect;
177   flagword flags;
178
179   if (hdr->bfd_section != NULL)
180     {
181       BFD_ASSERT (strcmp (name,
182                           bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
183       return true;
184     }
185
186   newsect = bfd_make_section_anyway (abfd, name);
187   if (newsect == NULL)
188     return false;
189
190   newsect->filepos = hdr->sh_offset;
191
192   if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
193       || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
194       || ! bfd_set_section_alignment (abfd, newsect,
195                                       bfd_log2 (hdr->sh_addralign)))
196     return false;
197
198   flags = SEC_NO_FLAGS;
199   if (hdr->sh_type != SHT_NOBITS)
200     flags |= SEC_HAS_CONTENTS;
201   if ((hdr->sh_flags & SHF_ALLOC) != 0)
202     {
203       flags |= SEC_ALLOC;
204       if (hdr->sh_type != SHT_NOBITS)
205         flags |= SEC_LOAD;
206     }
207   if ((hdr->sh_flags & SHF_WRITE) == 0)
208     flags |= SEC_READONLY;
209   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
210     flags |= SEC_CODE;
211   else if ((flags & SEC_LOAD) != 0)
212     flags |= SEC_DATA;
213
214   /* The debugging sections appear to be recognized only by name, not
215      any sort of flag.  */
216   if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
217       || strncmp (name, ".line", sizeof ".line" - 1) == 0
218       || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
219     flags |= SEC_DEBUGGING;
220
221   /* As a GNU extension, if the name begins with .gnu.linkonce, we
222      only link a single copy of the section.  This is used to support
223      g++.  g++ will emit each template expansion in its own section.
224      The symbols will be defined as weak, so that multiple definitions
225      are permitted.  The GNU linker extension is to actually discard
226      all but one of the sections.  */
227   if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
228     flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
229
230   if (! bfd_set_section_flags (abfd, newsect, flags))
231     return false;
232
233   if ((flags & SEC_ALLOC) != 0)
234     {
235       Elf_Internal_Phdr *phdr;
236       unsigned int i;
237
238       /* Look through the phdrs to see if we need to adjust the lma.  */
239       phdr = elf_tdata (abfd)->phdr;
240       for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
241         {
242           if (phdr->p_type == PT_LOAD
243               && phdr->p_paddr != 0
244               && phdr->p_vaddr != phdr->p_paddr
245               && phdr->p_vaddr <= hdr->sh_addr
246               && phdr->p_vaddr + phdr->p_memsz >= hdr->sh_addr + hdr->sh_size
247               && ((flags & SEC_LOAD) == 0
248                   || (phdr->p_offset <= hdr->sh_offset
249                       && (phdr->p_offset + phdr->p_filesz
250                           >= hdr->sh_offset + hdr->sh_size))))
251             {
252               newsect->lma += phdr->p_paddr - phdr->p_vaddr;
253               break;
254             }
255         }
256     }
257
258   hdr->bfd_section = newsect;
259   elf_section_data (newsect)->this_hdr = *hdr;
260
261   return true;
262 }
263
264 /*
265 INTERNAL_FUNCTION
266         bfd_elf_find_section
267
268 SYNOPSIS
269         struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
270
271 DESCRIPTION
272         Helper functions for GDB to locate the string tables.
273         Since BFD hides string tables from callers, GDB needs to use an
274         internal hook to find them.  Sun's .stabstr, in particular,
275         isn't even pointed to by the .stab section, so ordinary
276         mechanisms wouldn't work to find it, even if we had some.
277 */
278
279 struct elf_internal_shdr *
280 bfd_elf_find_section (abfd, name)
281      bfd * abfd;
282      char *name;
283 {
284   Elf_Internal_Shdr **i_shdrp;
285   char *shstrtab;
286   unsigned int max;
287   unsigned int i;
288
289   i_shdrp = elf_elfsections (abfd);
290   if (i_shdrp != NULL)
291     {
292       shstrtab = bfd_elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
293       if (shstrtab != NULL)
294         {
295           max = elf_elfheader (abfd)->e_shnum;
296           for (i = 1; i < max; i++)
297             if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
298               return i_shdrp[i];
299         }
300     }
301   return 0;
302 }
303
304 const char *const bfd_elf_section_type_names[] = {
305   "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
306   "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
307   "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
308 };
309
310 /* ELF relocs are against symbols.  If we are producing relocateable
311    output, and the reloc is against an external symbol, and nothing
312    has given us any additional addend, the resulting reloc will also
313    be against the same symbol.  In such a case, we don't want to
314    change anything about the way the reloc is handled, since it will
315    all be done at final link time.  Rather than put special case code
316    into bfd_perform_relocation, all the reloc types use this howto
317    function.  It just short circuits the reloc if producing
318    relocateable output against an external symbol.  */
319
320 /*ARGSUSED*/
321 bfd_reloc_status_type
322 bfd_elf_generic_reloc (abfd,
323                        reloc_entry,
324                        symbol,
325                        data,
326                        input_section,
327                        output_bfd,
328                        error_message)
329      bfd *abfd;
330      arelent *reloc_entry;
331      asymbol *symbol;
332      PTR data;
333      asection *input_section;
334      bfd *output_bfd;
335      char **error_message;
336 {
337   if (output_bfd != (bfd *) NULL
338       && (symbol->flags & BSF_SECTION_SYM) == 0
339       && (! reloc_entry->howto->partial_inplace
340           || reloc_entry->addend == 0))
341     {
342       reloc_entry->address += input_section->output_offset;
343       return bfd_reloc_ok;
344     }
345
346   return bfd_reloc_continue;
347 }
348 \f
349 /* Print out the program headers.  */
350
351 boolean
352 _bfd_elf_print_private_bfd_data (abfd, farg)
353      bfd *abfd;
354      PTR farg;
355 {
356   FILE *f = (FILE *) farg;
357   Elf_Internal_Phdr *p;
358   asection *s;
359   bfd_byte *dynbuf = NULL;
360
361   p = elf_tdata (abfd)->phdr;
362   if (p != NULL)
363     {
364       unsigned int i, c;
365
366       fprintf (f, "\nProgram Header:\n");
367       c = elf_elfheader (abfd)->e_phnum;
368       for (i = 0; i < c; i++, p++)
369         {
370           const char *s;
371           char buf[20];
372
373           switch (p->p_type)
374             {
375             case PT_NULL: s = "NULL"; break;
376             case PT_LOAD: s = "LOAD"; break;
377             case PT_DYNAMIC: s = "DYNAMIC"; break;
378             case PT_INTERP: s = "INTERP"; break;
379             case PT_NOTE: s = "NOTE"; break;
380             case PT_SHLIB: s = "SHLIB"; break;
381             case PT_PHDR: s = "PHDR"; break;
382             default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
383             }
384           fprintf (f, "%8s off    0x", s);
385           fprintf_vma (f, p->p_offset);
386           fprintf (f, " vaddr 0x");
387           fprintf_vma (f, p->p_vaddr);
388           fprintf (f, " paddr 0x");
389           fprintf_vma (f, p->p_paddr);
390           fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
391           fprintf (f, "         filesz 0x");
392           fprintf_vma (f, p->p_filesz);
393           fprintf (f, " memsz 0x");
394           fprintf_vma (f, p->p_memsz);
395           fprintf (f, " flags %c%c%c",
396                    (p->p_flags & PF_R) != 0 ? 'r' : '-',
397                    (p->p_flags & PF_W) != 0 ? 'w' : '-',
398                    (p->p_flags & PF_X) != 0 ? 'x' : '-');
399           if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
400             fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
401           fprintf (f, "\n");
402         }
403     }
404
405   s = bfd_get_section_by_name (abfd, ".dynamic");
406   if (s != NULL)
407     {
408       int elfsec;
409       unsigned long link;
410       bfd_byte *extdyn, *extdynend;
411       size_t extdynsize;
412       void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
413
414       fprintf (f, "\nDynamic Section:\n");
415
416       dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
417       if (dynbuf == NULL)
418         goto error_return;
419       if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
420                                       s->_raw_size))
421         goto error_return;
422
423       elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
424       if (elfsec == -1)
425         goto error_return;
426       link = elf_elfsections (abfd)[elfsec]->sh_link;
427
428       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
429       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
430
431       extdyn = dynbuf;
432       extdynend = extdyn + s->_raw_size;
433       for (; extdyn < extdynend; extdyn += extdynsize)
434         {
435           Elf_Internal_Dyn dyn;
436           const char *name;
437           char ab[20];
438           boolean stringp;
439
440           (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
441
442           if (dyn.d_tag == DT_NULL)
443             break;
444
445           stringp = false;
446           switch (dyn.d_tag)
447             {
448             default:
449               sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
450               name = ab;
451               break;
452
453             case DT_NEEDED: name = "NEEDED"; stringp = true; break;
454             case DT_PLTRELSZ: name = "PLTRELSZ"; break;
455             case DT_PLTGOT: name = "PLTGOT"; break;
456             case DT_HASH: name = "HASH"; break;
457             case DT_STRTAB: name = "STRTAB"; break;
458             case DT_SYMTAB: name = "SYMTAB"; break;
459             case DT_RELA: name = "RELA"; break;
460             case DT_RELASZ: name = "RELASZ"; break;
461             case DT_RELAENT: name = "RELAENT"; break;
462             case DT_STRSZ: name = "STRSZ"; break;
463             case DT_SYMENT: name = "SYMENT"; break;
464             case DT_INIT: name = "INIT"; break;
465             case DT_FINI: name = "FINI"; break;
466             case DT_SONAME: name = "SONAME"; stringp = true; break;
467             case DT_RPATH: name = "RPATH"; stringp = true; break;
468             case DT_SYMBOLIC: name = "SYMBOLIC"; break;
469             case DT_REL: name = "REL"; break;
470             case DT_RELSZ: name = "RELSZ"; break;
471             case DT_RELENT: name = "RELENT"; break;
472             case DT_PLTREL: name = "PLTREL"; break;
473             case DT_DEBUG: name = "DEBUG"; break;
474             case DT_TEXTREL: name = "TEXTREL"; break;
475             case DT_JMPREL: name = "JMPREL"; break;
476             }
477
478           fprintf (f, "  %-11s ", name);
479           if (! stringp)
480             fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
481           else
482             {
483               const char *string;
484
485               string = bfd_elf_string_from_elf_section (abfd, link,
486                                                         dyn.d_un.d_val);
487               if (string == NULL)
488                 goto error_return;
489               fprintf (f, "%s", string);
490             }
491           fprintf (f, "\n");
492         }
493
494       free (dynbuf);
495       dynbuf = NULL;
496     }
497
498   return true;
499
500  error_return:
501   if (dynbuf != NULL)
502     free (dynbuf);
503   return false;
504 }
505
506 /* Display ELF-specific fields of a symbol.  */
507 void
508 bfd_elf_print_symbol (ignore_abfd, filep, symbol, how)
509      bfd *ignore_abfd;
510      PTR filep;
511      asymbol *symbol;
512      bfd_print_symbol_type how;
513 {
514   FILE *file = (FILE *) filep;
515   switch (how)
516     {
517     case bfd_print_symbol_name:
518       fprintf (file, "%s", symbol->name);
519       break;
520     case bfd_print_symbol_more:
521       fprintf (file, "elf ");
522       fprintf_vma (file, symbol->value);
523       fprintf (file, " %lx", (long) symbol->flags);
524       break;
525     case bfd_print_symbol_all:
526       {
527         CONST char *section_name;
528         section_name = symbol->section ? symbol->section->name : "(*none*)";
529         bfd_print_symbol_vandf ((PTR) file, symbol);
530         fprintf (file, " %s\t", section_name);
531         /* Print the "other" value for a symbol.  For common symbols,
532            we've already printed the size; now print the alignment.
533            For other symbols, we have no specified alignment, and
534            we've printed the address; now print the size.  */
535         fprintf_vma (file,
536                      (bfd_is_com_section (symbol->section)
537                       ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
538                       : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
539         fprintf (file, " %s", symbol->name);
540       }
541       break;
542     }
543 }
544 \f
545 /* Create an entry in an ELF linker hash table.  */
546
547 struct bfd_hash_entry *
548 _bfd_elf_link_hash_newfunc (entry, table, string)
549      struct bfd_hash_entry *entry;
550      struct bfd_hash_table *table;
551      const char *string;
552 {
553   struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
554
555   /* Allocate the structure if it has not already been allocated by a
556      subclass.  */
557   if (ret == (struct elf_link_hash_entry *) NULL)
558     ret = ((struct elf_link_hash_entry *)
559            bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
560   if (ret == (struct elf_link_hash_entry *) NULL)
561     return (struct bfd_hash_entry *) ret;
562
563   /* Call the allocation method of the superclass.  */
564   ret = ((struct elf_link_hash_entry *)
565          _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
566                                  table, string));
567   if (ret != (struct elf_link_hash_entry *) NULL)
568     {
569       /* Set local fields.  */
570       ret->indx = -1;
571       ret->size = 0;
572       ret->dynindx = -1;
573       ret->dynstr_index = 0;
574       ret->weakdef = NULL;
575       ret->got_offset = (bfd_vma) -1;
576       ret->plt_offset = (bfd_vma) -1;
577       ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
578       ret->type = STT_NOTYPE;
579       /* Assume that we have been called by a non-ELF symbol reader.
580          This flag is then reset by the code which reads an ELF input
581          file.  This ensures that a symbol created by a non-ELF symbol
582          reader will have the flag set correctly.  */
583       ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
584     }
585
586   return (struct bfd_hash_entry *) ret;
587 }
588
589 /* Initialize an ELF linker hash table.  */
590
591 boolean
592 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
593      struct elf_link_hash_table *table;
594      bfd *abfd;
595      struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
596                                                 struct bfd_hash_table *,
597                                                 const char *));
598 {
599   table->dynamic_sections_created = false;
600   table->dynobj = NULL;
601   /* The first dynamic symbol is a dummy.  */
602   table->dynsymcount = 1;
603   table->dynstr = NULL;
604   table->bucketcount = 0;
605   table->needed = NULL;
606   table->hgot = NULL;
607   table->stab_info = NULL;
608   return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
609 }
610
611 /* Create an ELF linker hash table.  */
612
613 struct bfd_link_hash_table *
614 _bfd_elf_link_hash_table_create (abfd)
615      bfd *abfd;
616 {
617   struct elf_link_hash_table *ret;
618
619   ret = ((struct elf_link_hash_table *)
620          bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
621   if (ret == (struct elf_link_hash_table *) NULL)
622     return NULL;
623
624   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
625     {
626       bfd_release (abfd, ret);
627       return NULL;
628     }
629
630   return &ret->root;
631 }
632
633 /* This is a hook for the ELF emulation code in the generic linker to
634    tell the backend linker what file name to use for the DT_NEEDED
635    entry for a dynamic object.  The generic linker passes name as an
636    empty string to indicate that no DT_NEEDED entry should be made.  */
637
638 void
639 bfd_elf_set_dt_needed_name (abfd, name)
640      bfd *abfd;
641      const char *name;
642 {
643   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
644       && bfd_get_format (abfd) == bfd_object)
645     elf_dt_name (abfd) = name;
646 }
647
648 /* Get the list of DT_NEEDED entries for a link.  This is a hook for
649    the ELF emulation code.  */
650
651 struct bfd_link_needed_list *
652 bfd_elf_get_needed_list (abfd, info)
653      bfd *abfd;
654      struct bfd_link_info *info;
655 {
656   if (info->hash->creator->flavour != bfd_target_elf_flavour)
657     return NULL;
658   return elf_hash_table (info)->needed;
659 }
660
661 /* Get the name actually used for a dynamic object for a link.  This
662    is the SONAME entry if there is one.  Otherwise, it is the string
663    passed to bfd_elf_set_dt_needed_name, or it is the filename.  */
664
665 const char *
666 bfd_elf_get_dt_soname (abfd)
667      bfd *abfd;
668 {
669   if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
670       && bfd_get_format (abfd) == bfd_object)
671     return elf_dt_name (abfd);
672   return NULL;
673 }
674 \f
675 /* Allocate an ELF string table--force the first byte to be zero.  */
676
677 struct bfd_strtab_hash *
678 _bfd_elf_stringtab_init ()
679 {
680   struct bfd_strtab_hash *ret;
681
682   ret = _bfd_stringtab_init ();
683   if (ret != NULL)
684     {
685       bfd_size_type loc;
686
687       loc = _bfd_stringtab_add (ret, "", true, false);
688       BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
689       if (loc == (bfd_size_type) -1)
690         {
691           _bfd_stringtab_free (ret);
692           ret = NULL;
693         }
694     }
695   return ret;
696 }
697 \f
698 /* ELF .o/exec file reading */
699
700 /* Create a new bfd section from an ELF section header. */
701
702 boolean
703 bfd_section_from_shdr (abfd, shindex)
704      bfd *abfd;
705      unsigned int shindex;
706 {
707   Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
708   Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
709   struct elf_backend_data *bed = get_elf_backend_data (abfd);
710   char *name;
711
712   name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
713
714   switch (hdr->sh_type)
715     {
716     case SHT_NULL:
717       /* Inactive section. Throw it away.  */
718       return true;
719
720     case SHT_PROGBITS:  /* Normal section with contents.  */
721     case SHT_DYNAMIC:   /* Dynamic linking information.  */
722     case SHT_NOBITS:    /* .bss section.  */
723     case SHT_HASH:      /* .hash section.  */
724     case SHT_NOTE:      /* .note section.  */
725       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
726
727     case SHT_SYMTAB:            /* A symbol table */
728       if (elf_onesymtab (abfd) == shindex)
729         return true;
730
731       BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
732       BFD_ASSERT (elf_onesymtab (abfd) == 0);
733       elf_onesymtab (abfd) = shindex;
734       elf_tdata (abfd)->symtab_hdr = *hdr;
735       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
736       abfd->flags |= HAS_SYMS;
737
738       /* Sometimes a shared object will map in the symbol table.  If
739          SHF_ALLOC is set, and this is a shared object, then we also
740          treat this section as a BFD section.  We can not base the
741          decision purely on SHF_ALLOC, because that flag is sometimes
742          set in a relocateable object file, which would confuse the
743          linker.  */
744       if ((hdr->sh_flags & SHF_ALLOC) != 0
745           && (abfd->flags & DYNAMIC) != 0
746           && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
747         return false;
748
749       return true;
750
751     case SHT_DYNSYM:            /* A dynamic symbol table */
752       if (elf_dynsymtab (abfd) == shindex)
753         return true;
754
755       BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
756       BFD_ASSERT (elf_dynsymtab (abfd) == 0);
757       elf_dynsymtab (abfd) = shindex;
758       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
759       elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
760       abfd->flags |= HAS_SYMS;
761
762       /* Besides being a symbol table, we also treat this as a regular
763          section, so that objcopy can handle it.  */
764       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
765
766     case SHT_STRTAB:            /* A string table */
767       if (hdr->bfd_section != NULL)
768         return true;
769       if (ehdr->e_shstrndx == shindex)
770         {
771           elf_tdata (abfd)->shstrtab_hdr = *hdr;
772           elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
773           return true;
774         }
775       {
776         unsigned int i;
777
778         for (i = 1; i < ehdr->e_shnum; i++)
779           {
780             Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
781             if (hdr2->sh_link == shindex)
782               {
783                 if (! bfd_section_from_shdr (abfd, i))
784                   return false;
785                 if (elf_onesymtab (abfd) == i)
786                   {
787                     elf_tdata (abfd)->strtab_hdr = *hdr;
788                     elf_elfsections (abfd)[shindex] =
789                       &elf_tdata (abfd)->strtab_hdr;
790                     return true;
791                   }
792                 if (elf_dynsymtab (abfd) == i)
793                   {
794                     elf_tdata (abfd)->dynstrtab_hdr = *hdr;
795                     elf_elfsections (abfd)[shindex] = hdr =
796                       &elf_tdata (abfd)->dynstrtab_hdr;
797                     /* We also treat this as a regular section, so
798                        that objcopy can handle it.  */
799                     break;
800                   }
801 #if 0 /* Not handling other string tables specially right now.  */
802                 hdr2 = elf_elfsections (abfd)[i];       /* in case it moved */
803                 /* We have a strtab for some random other section.  */
804                 newsect = (asection *) hdr2->bfd_section;
805                 if (!newsect)
806                   break;
807                 hdr->bfd_section = newsect;
808                 hdr2 = &elf_section_data (newsect)->str_hdr;
809                 *hdr2 = *hdr;
810                 elf_elfsections (abfd)[shindex] = hdr2;
811 #endif
812               }
813           }
814       }
815
816       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
817
818     case SHT_REL:
819     case SHT_RELA:
820       /* *These* do a lot of work -- but build no sections!  */
821       {
822         asection *target_sect;
823         Elf_Internal_Shdr *hdr2;
824
825         /* For some incomprehensible reason Oracle distributes
826            libraries for Solaris in which some of the objects have
827            bogus sh_link fields.  It would be nice if we could just
828            reject them, but, unfortunately, some people need to use
829            them.  We scan through the section headers; if we find only
830            one suitable symbol table, we clobber the sh_link to point
831            to it.  I hope this doesn't break anything.  */
832         if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
833             && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
834           {
835             int scan;
836             int found;
837
838             found = 0;
839             for (scan = 1; scan < ehdr->e_shnum; scan++)
840               {
841                 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
842                     || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
843                   {
844                     if (found != 0)
845                       {
846                         found = 0;
847                         break;
848                       }
849                     found = scan;
850                   }
851               }
852             if (found != 0)
853               hdr->sh_link = found;
854           }
855
856         /* Get the symbol table.  */
857         if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
858             && ! bfd_section_from_shdr (abfd, hdr->sh_link))
859           return false;
860
861         /* If this reloc section does not use the main symbol table we
862            don't treat it as a reloc section.  BFD can't adequately
863            represent such a section, so at least for now, we don't
864            try.  We just present it as a normal section.  */
865         if (hdr->sh_link != elf_onesymtab (abfd))
866           return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
867
868         if (! bfd_section_from_shdr (abfd, hdr->sh_info))
869           return false;
870         target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
871         if (target_sect == NULL)
872           return false;
873
874         if ((target_sect->flags & SEC_RELOC) == 0
875             || target_sect->reloc_count == 0)
876           hdr2 = &elf_section_data (target_sect)->rel_hdr;
877         else
878           {
879             BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
880             hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
881             elf_section_data (target_sect)->rel_hdr2 = hdr2;
882           }
883         *hdr2 = *hdr;
884         elf_elfsections (abfd)[shindex] = hdr2;
885         target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
886         target_sect->flags |= SEC_RELOC;
887         target_sect->relocation = NULL;
888         target_sect->rel_filepos = hdr->sh_offset;
889         abfd->flags |= HAS_RELOC;
890         return true;
891       }
892       break;
893
894     case SHT_SHLIB:
895       return true;
896
897     default:
898       /* Check for any processor-specific section types.  */
899       {
900         if (bed->elf_backend_section_from_shdr)
901           (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
902       }
903       break;
904     }
905
906   return true;
907 }
908
909 /* Given an ELF section number, retrieve the corresponding BFD
910    section.  */
911
912 asection *
913 bfd_section_from_elf_index (abfd, index)
914      bfd *abfd;
915      unsigned int index;
916 {
917   BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
918   if (index >= elf_elfheader (abfd)->e_shnum)
919     return NULL;
920   return elf_elfsections (abfd)[index]->bfd_section;
921 }
922
923 boolean
924 _bfd_elf_new_section_hook (abfd, sec)
925      bfd *abfd;
926      asection *sec;
927 {
928   struct bfd_elf_section_data *sdata;
929
930   sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
931   if (!sdata)
932     return false;
933   sec->used_by_bfd = (PTR) sdata;
934   memset (sdata, 0, sizeof (*sdata));
935   return true;
936 }
937
938 /* Create a new bfd section from an ELF program header.
939
940    Since program segments have no names, we generate a synthetic name
941    of the form segment<NUM>, where NUM is generally the index in the
942    program header table.  For segments that are split (see below) we
943    generate the names segment<NUM>a and segment<NUM>b.
944
945    Note that some program segments may have a file size that is different than
946    (less than) the memory size.  All this means is that at execution the
947    system must allocate the amount of memory specified by the memory size,
948    but only initialize it with the first "file size" bytes read from the
949    file.  This would occur for example, with program segments consisting
950    of combined data+bss.
951
952    To handle the above situation, this routine generates TWO bfd sections
953    for the single program segment.  The first has the length specified by
954    the file size of the segment, and the second has the length specified
955    by the difference between the two sizes.  In effect, the segment is split
956    into it's initialized and uninitialized parts.
957
958  */
959
960 boolean
961 bfd_section_from_phdr (abfd, hdr, index)
962      bfd *abfd;
963      Elf_Internal_Phdr *hdr;
964      int index;
965 {
966   asection *newsect;
967   char *name;
968   char namebuf[64];
969   int split;
970
971   split = ((hdr->p_memsz > 0) &&
972            (hdr->p_filesz > 0) &&
973            (hdr->p_memsz > hdr->p_filesz));
974   sprintf (namebuf, split ? "segment%da" : "segment%d", index);
975   name = bfd_alloc (abfd, strlen (namebuf) + 1);
976   if (!name)
977     return false;
978   strcpy (name, namebuf);
979   newsect = bfd_make_section (abfd, name);
980   if (newsect == NULL)
981     return false;
982   newsect->vma = hdr->p_vaddr;
983   newsect->lma = hdr->p_paddr;
984   newsect->_raw_size = hdr->p_filesz;
985   newsect->filepos = hdr->p_offset;
986   newsect->flags |= SEC_HAS_CONTENTS;
987   if (hdr->p_type == PT_LOAD)
988     {
989       newsect->flags |= SEC_ALLOC;
990       newsect->flags |= SEC_LOAD;
991       if (hdr->p_flags & PF_X)
992         {
993           /* FIXME: all we known is that it has execute PERMISSION,
994              may be data. */
995           newsect->flags |= SEC_CODE;
996         }
997     }
998   if (!(hdr->p_flags & PF_W))
999     {
1000       newsect->flags |= SEC_READONLY;
1001     }
1002
1003   if (split)
1004     {
1005       sprintf (namebuf, "segment%db", index);
1006       name = bfd_alloc (abfd, strlen (namebuf) + 1);
1007       if (!name)
1008         return false;
1009       strcpy (name, namebuf);
1010       newsect = bfd_make_section (abfd, name);
1011       if (newsect == NULL)
1012         return false;
1013       newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1014       newsect->lma = hdr->p_paddr + hdr->p_filesz;
1015       newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1016       if (hdr->p_type == PT_LOAD)
1017         {
1018           newsect->flags |= SEC_ALLOC;
1019           if (hdr->p_flags & PF_X)
1020             newsect->flags |= SEC_CODE;
1021         }
1022       if (!(hdr->p_flags & PF_W))
1023         newsect->flags |= SEC_READONLY;
1024     }
1025
1026   return true;
1027 }
1028
1029 /* Set up an ELF internal section header for a section.  */
1030
1031 /*ARGSUSED*/
1032 static void
1033 elf_fake_sections (abfd, asect, failedptrarg)
1034      bfd *abfd;
1035      asection *asect;
1036      PTR failedptrarg;
1037 {
1038   struct elf_backend_data *bed = get_elf_backend_data (abfd);
1039   boolean *failedptr = (boolean *) failedptrarg;
1040   Elf_Internal_Shdr *this_hdr;
1041
1042   if (*failedptr)
1043     {
1044       /* We already failed; just get out of the bfd_map_over_sections
1045          loop.  */
1046       return;
1047     }
1048
1049   this_hdr = &elf_section_data (asect)->this_hdr;
1050
1051   this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1052                                                           asect->name,
1053                                                           true, false);
1054   if (this_hdr->sh_name == (unsigned long) -1)
1055     {
1056       *failedptr = true;
1057       return;
1058     }
1059
1060   this_hdr->sh_flags = 0;
1061
1062   if ((asect->flags & SEC_ALLOC) != 0
1063       || asect->user_set_vma)
1064     this_hdr->sh_addr = asect->vma;
1065   else
1066     this_hdr->sh_addr = 0;
1067
1068   this_hdr->sh_offset = 0;
1069   this_hdr->sh_size = asect->_raw_size;
1070   this_hdr->sh_link = 0;
1071   this_hdr->sh_addralign = 1 << asect->alignment_power;
1072   /* The sh_entsize and sh_info fields may have been set already by
1073      copy_private_section_data.  */
1074
1075   this_hdr->bfd_section = asect;
1076   this_hdr->contents = NULL;
1077
1078   /* FIXME: This should not be based on section names.  */
1079   if (strcmp (asect->name, ".dynstr") == 0)
1080     this_hdr->sh_type = SHT_STRTAB;
1081   else if (strcmp (asect->name, ".hash") == 0)
1082     {
1083       this_hdr->sh_type = SHT_HASH;
1084       this_hdr->sh_entsize = bed->s->arch_size / 8;
1085     }
1086   else if (strcmp (asect->name, ".dynsym") == 0)
1087     {
1088       this_hdr->sh_type = SHT_DYNSYM;
1089       this_hdr->sh_entsize = bed->s->sizeof_sym;
1090     }
1091   else if (strcmp (asect->name, ".dynamic") == 0)
1092     {
1093       this_hdr->sh_type = SHT_DYNAMIC;
1094       this_hdr->sh_entsize = bed->s->sizeof_dyn;
1095     }
1096   else if (strncmp (asect->name, ".rela", 5) == 0
1097            && get_elf_backend_data (abfd)->use_rela_p)
1098     {
1099       this_hdr->sh_type = SHT_RELA;
1100       this_hdr->sh_entsize = bed->s->sizeof_rela;
1101     }
1102   else if (strncmp (asect->name, ".rel", 4) == 0
1103            && ! get_elf_backend_data (abfd)->use_rela_p)
1104     {
1105       this_hdr->sh_type = SHT_REL;
1106       this_hdr->sh_entsize = bed->s->sizeof_rel;
1107     }
1108   else if (strcmp (asect->name, ".note") == 0)
1109     this_hdr->sh_type = SHT_NOTE;
1110   else if (strncmp (asect->name, ".stab", 5) == 0
1111            && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1112     this_hdr->sh_type = SHT_STRTAB;
1113   else if ((asect->flags & SEC_ALLOC) != 0
1114            && (asect->flags & SEC_LOAD) != 0)
1115     this_hdr->sh_type = SHT_PROGBITS;
1116   else if ((asect->flags & SEC_ALLOC) != 0
1117            && ((asect->flags & SEC_LOAD) == 0))
1118     this_hdr->sh_type = SHT_NOBITS;
1119   else
1120     {
1121       /* Who knows?  */
1122       this_hdr->sh_type = SHT_PROGBITS;
1123     }
1124
1125   if ((asect->flags & SEC_ALLOC) != 0)
1126     this_hdr->sh_flags |= SHF_ALLOC;
1127   if ((asect->flags & SEC_READONLY) == 0)
1128     this_hdr->sh_flags |= SHF_WRITE;
1129   if ((asect->flags & SEC_CODE) != 0)
1130     this_hdr->sh_flags |= SHF_EXECINSTR;
1131
1132   /* Check for processor-specific section types.  */
1133   {
1134     struct elf_backend_data *bed = get_elf_backend_data (abfd);
1135
1136     if (bed->elf_backend_fake_sections)
1137       (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1138   }
1139
1140   /* If the section has relocs, set up a section header for the
1141      SHT_REL[A] section.  */
1142   if ((asect->flags & SEC_RELOC) != 0)
1143     {
1144       Elf_Internal_Shdr *rela_hdr;
1145       int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1146       char *name;
1147
1148       rela_hdr = &elf_section_data (asect)->rel_hdr;
1149       name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1150       if (name == NULL)
1151         {
1152           *failedptr = true;
1153           return;
1154         }
1155       sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1156       rela_hdr->sh_name =
1157         (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1158                                            true, false);
1159       if (rela_hdr->sh_name == (unsigned int) -1)
1160         {
1161           *failedptr = true;
1162           return;
1163         }
1164       rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1165       rela_hdr->sh_entsize = (use_rela_p
1166                               ? bed->s->sizeof_rela
1167                               : bed->s->sizeof_rel);
1168       rela_hdr->sh_addralign = bed->s->file_align;
1169       rela_hdr->sh_flags = 0;
1170       rela_hdr->sh_addr = 0;
1171       rela_hdr->sh_size = 0;
1172       rela_hdr->sh_offset = 0;
1173     }
1174 }
1175
1176 /* Assign all ELF section numbers.  The dummy first section is handled here
1177    too.  The link/info pointers for the standard section types are filled
1178    in here too, while we're at it.  */
1179
1180 static boolean
1181 assign_section_numbers (abfd)
1182      bfd *abfd;
1183 {
1184   struct elf_obj_tdata *t = elf_tdata (abfd);
1185   asection *sec;
1186   unsigned int section_number;
1187   Elf_Internal_Shdr **i_shdrp;
1188   struct elf_backend_data *bed = get_elf_backend_data (abfd);
1189
1190   section_number = 1;
1191
1192   for (sec = abfd->sections; sec; sec = sec->next)
1193     {
1194       struct bfd_elf_section_data *d = elf_section_data (sec);
1195
1196       d->this_idx = section_number++;
1197       if ((sec->flags & SEC_RELOC) == 0)
1198         d->rel_idx = 0;
1199       else
1200         d->rel_idx = section_number++;
1201     }
1202
1203   t->shstrtab_section = section_number++;
1204   elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1205   t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1206
1207   if (abfd->symcount > 0)
1208     {
1209       t->symtab_section = section_number++;
1210       t->strtab_section = section_number++;
1211     }
1212
1213   elf_elfheader (abfd)->e_shnum = section_number;
1214
1215   /* Set up the list of section header pointers, in agreement with the
1216      indices.  */
1217   i_shdrp = ((Elf_Internal_Shdr **)
1218              bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1219   if (i_shdrp == NULL)
1220     return false;
1221
1222   i_shdrp[0] = ((Elf_Internal_Shdr *)
1223                 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1224   if (i_shdrp[0] == NULL)
1225     {
1226       bfd_release (abfd, i_shdrp);
1227       return false;
1228     }
1229   memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1230
1231   elf_elfsections (abfd) = i_shdrp;
1232
1233   i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1234   if (abfd->symcount > 0)
1235     {
1236       i_shdrp[t->symtab_section] = &t->symtab_hdr;
1237       i_shdrp[t->strtab_section] = &t->strtab_hdr;
1238       t->symtab_hdr.sh_link = t->strtab_section;
1239     }
1240   for (sec = abfd->sections; sec; sec = sec->next)
1241     {
1242       struct bfd_elf_section_data *d = elf_section_data (sec);
1243       asection *s;
1244       const char *name;
1245
1246       i_shdrp[d->this_idx] = &d->this_hdr;
1247       if (d->rel_idx != 0)
1248         i_shdrp[d->rel_idx] = &d->rel_hdr;
1249
1250       /* Fill in the sh_link and sh_info fields while we're at it.  */
1251
1252       /* sh_link of a reloc section is the section index of the symbol
1253          table.  sh_info is the section index of the section to which
1254          the relocation entries apply.  */
1255       if (d->rel_idx != 0)
1256         {
1257           d->rel_hdr.sh_link = t->symtab_section;
1258           d->rel_hdr.sh_info = d->this_idx;
1259         }
1260
1261       switch (d->this_hdr.sh_type)
1262         {
1263         case SHT_REL:
1264         case SHT_RELA:
1265           /* A reloc section which we are treating as a normal BFD
1266              section.  sh_link is the section index of the symbol
1267              table.  sh_info is the section index of the section to
1268              which the relocation entries apply.  We assume that an
1269              allocated reloc section uses the dynamic symbol table.
1270              FIXME: How can we be sure?  */
1271           s = bfd_get_section_by_name (abfd, ".dynsym");
1272           if (s != NULL)
1273             d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1274
1275           /* We look up the section the relocs apply to by name.  */
1276           name = sec->name;
1277           if (d->this_hdr.sh_type == SHT_REL)
1278             name += 4;
1279           else
1280             name += 5;
1281           s = bfd_get_section_by_name (abfd, name);
1282           if (s != NULL)
1283             d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1284           break;
1285
1286         case SHT_STRTAB:
1287           /* We assume that a section named .stab*str is a stabs
1288              string section.  We look for a section with the same name
1289              but without the trailing ``str'', and set its sh_link
1290              field to point to this section.  */
1291           if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1292               && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1293             {
1294               size_t len;
1295               char *alc;
1296
1297               len = strlen (sec->name);
1298               alc = (char *) bfd_malloc (len - 2);
1299               if (alc == NULL)
1300                 return false;
1301               strncpy (alc, sec->name, len - 3);
1302               alc[len - 3] = '\0';
1303               s = bfd_get_section_by_name (abfd, alc);
1304               free (alc);
1305               if (s != NULL)
1306                 {
1307                   elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1308
1309                   /* This is a .stab section.  */
1310                   elf_section_data (s)->this_hdr.sh_entsize =
1311                     4 + 2 * (bed->s->arch_size / 8);
1312                 }
1313             }
1314           break;
1315
1316         case SHT_DYNAMIC:
1317         case SHT_DYNSYM:
1318           /* sh_link is the section header index of the string table
1319              used for the dynamic entries or symbol table.  */
1320           s = bfd_get_section_by_name (abfd, ".dynstr");
1321           if (s != NULL)
1322             d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1323           break;
1324
1325         case SHT_HASH:
1326           /* sh_link is the section header index of the symbol table
1327              this hash table is for.  */
1328           s = bfd_get_section_by_name (abfd, ".dynsym");
1329           if (s != NULL)
1330             d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1331           break;
1332         }
1333     }
1334
1335   return true;
1336 }
1337
1338 /* Map symbol from it's internal number to the external number, moving
1339    all local symbols to be at the head of the list.  */
1340
1341 static INLINE int
1342 sym_is_global (abfd, sym)
1343      bfd *abfd;
1344      asymbol *sym;
1345 {
1346   /* If the backend has a special mapping, use it.  */
1347   if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1348     return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1349             (abfd, sym));
1350
1351   return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1352           || bfd_is_und_section (bfd_get_section (sym))
1353           || bfd_is_com_section (bfd_get_section (sym)));
1354 }
1355
1356 static boolean
1357 elf_map_symbols (abfd)
1358      bfd *abfd;
1359 {
1360   int symcount = bfd_get_symcount (abfd);
1361   asymbol **syms = bfd_get_outsymbols (abfd);
1362   asymbol **sect_syms;
1363   int num_locals = 0;
1364   int num_globals = 0;
1365   int num_locals2 = 0;
1366   int num_globals2 = 0;
1367   int max_index = 0;
1368   int num_sections = 0;
1369   int idx;
1370   asection *asect;
1371   asymbol **new_syms;
1372
1373 #ifdef DEBUG
1374   fprintf (stderr, "elf_map_symbols\n");
1375   fflush (stderr);
1376 #endif
1377
1378   /* Add a section symbol for each BFD section.  FIXME: Is this really
1379      necessary?  */
1380   for (asect = abfd->sections; asect; asect = asect->next)
1381     {
1382       if (max_index < asect->index)
1383         max_index = asect->index;
1384     }
1385
1386   max_index++;
1387   sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1388   if (sect_syms == NULL)
1389     return false;
1390   elf_section_syms (abfd) = sect_syms;
1391
1392   for (idx = 0; idx < symcount; idx++)
1393     {
1394       if ((syms[idx]->flags & BSF_SECTION_SYM) != 0
1395           && (syms[idx]->value + syms[idx]->section->vma) == 0)
1396         {
1397           asection *sec;
1398
1399           sec = syms[idx]->section;
1400           if (sec->owner != NULL)
1401             {
1402               if (sec->owner != abfd)
1403                 {
1404                   if (sec->output_offset != 0)
1405                     continue;
1406                   sec = sec->output_section;
1407                   BFD_ASSERT (sec->owner == abfd);
1408                 }
1409               sect_syms[sec->index] = syms[idx];
1410             }
1411         }
1412     }
1413
1414   for (asect = abfd->sections; asect; asect = asect->next)
1415     {
1416       asymbol *sym;
1417
1418       if (sect_syms[asect->index] != NULL)
1419         continue;
1420
1421       sym = bfd_make_empty_symbol (abfd);
1422       if (sym == NULL)
1423         return false;
1424       sym->the_bfd = abfd;
1425       sym->name = asect->name;
1426       sym->value = 0;
1427       /* Set the flags to 0 to indicate that this one was newly added.  */
1428       sym->flags = 0;
1429       sym->section = asect;
1430       sect_syms[asect->index] = sym;
1431       num_sections++;
1432 #ifdef DEBUG
1433       fprintf (stderr,
1434                "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1435                asect->name, (long) asect->vma, asect->index, (long) asect);
1436 #endif
1437     }
1438
1439   /* Classify all of the symbols.  */
1440   for (idx = 0; idx < symcount; idx++)
1441     {
1442       if (!sym_is_global (abfd, syms[idx]))
1443         num_locals++;
1444       else
1445         num_globals++;
1446     }
1447   for (asect = abfd->sections; asect; asect = asect->next)
1448     {
1449       if (sect_syms[asect->index] != NULL
1450           && sect_syms[asect->index]->flags == 0)
1451         {
1452           sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1453           if (!sym_is_global (abfd, sect_syms[asect->index]))
1454             num_locals++;
1455           else
1456             num_globals++;
1457           sect_syms[asect->index]->flags = 0;
1458         }
1459     }
1460
1461   /* Now sort the symbols so the local symbols are first.  */
1462   new_syms = ((asymbol **)
1463               bfd_alloc (abfd,
1464                          (num_locals + num_globals) * sizeof (asymbol *)));
1465   if (new_syms == NULL)
1466     return false;
1467
1468   for (idx = 0; idx < symcount; idx++)
1469     {
1470       asymbol *sym = syms[idx];
1471       int i;
1472
1473       if (!sym_is_global (abfd, sym))
1474         i = num_locals2++;
1475       else
1476         i = num_locals + num_globals2++;
1477       new_syms[i] = sym;
1478       sym->udata.i = i + 1;
1479     }
1480   for (asect = abfd->sections; asect; asect = asect->next)
1481     {
1482       if (sect_syms[asect->index] != NULL
1483           && sect_syms[asect->index]->flags == 0)
1484         {
1485           asymbol *sym = sect_syms[asect->index];
1486           int i;
1487
1488           sym->flags = BSF_SECTION_SYM;
1489           if (!sym_is_global (abfd, sym))
1490             i = num_locals2++;
1491           else
1492             i = num_locals + num_globals2++;
1493           new_syms[i] = sym;
1494           sym->udata.i = i + 1;
1495         }
1496     }
1497
1498   bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1499
1500   elf_num_locals (abfd) = num_locals;
1501   elf_num_globals (abfd) = num_globals;
1502   return true;
1503 }
1504
1505 /* Align to the maximum file alignment that could be required for any
1506    ELF data structure.  */
1507
1508 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1509 static INLINE file_ptr
1510 align_file_position (off, align)
1511      file_ptr off;
1512      int align;
1513 {
1514   return (off + align - 1) & ~(align - 1);
1515 }
1516
1517 /* Assign a file position to a section, optionally aligning to the
1518    required section alignment.  */
1519
1520 INLINE file_ptr
1521 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
1522      Elf_Internal_Shdr *i_shdrp;
1523      file_ptr offset;
1524      boolean align;
1525 {
1526   if (align)
1527     {
1528       unsigned int al;
1529
1530       al = i_shdrp->sh_addralign;
1531       if (al > 1)
1532         offset = BFD_ALIGN (offset, al);
1533     }
1534   i_shdrp->sh_offset = offset;
1535   if (i_shdrp->bfd_section != NULL)
1536     i_shdrp->bfd_section->filepos = offset;
1537   if (i_shdrp->sh_type != SHT_NOBITS)
1538     offset += i_shdrp->sh_size;
1539   return offset;
1540 }
1541
1542 /* Compute the file positions we are going to put the sections at, and
1543    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
1544    is not NULL, this is being called by the ELF backend linker.  */
1545
1546 boolean
1547 _bfd_elf_compute_section_file_positions (abfd, link_info)
1548      bfd *abfd;
1549      struct bfd_link_info *link_info;
1550 {
1551   struct elf_backend_data *bed = get_elf_backend_data (abfd);
1552   boolean failed;
1553   struct bfd_strtab_hash *strtab;
1554   Elf_Internal_Shdr *shstrtab_hdr;
1555
1556   if (abfd->output_has_begun)
1557     return true;
1558
1559   /* Do any elf backend specific processing first.  */
1560   if (bed->elf_backend_begin_write_processing)
1561     (*bed->elf_backend_begin_write_processing) (abfd, link_info);
1562
1563   if (! prep_headers (abfd))
1564     return false;
1565
1566   failed = false;
1567   bfd_map_over_sections (abfd, elf_fake_sections, &failed);
1568   if (failed)
1569     return false;
1570
1571   if (!assign_section_numbers (abfd))
1572     return false;
1573
1574   /* The backend linker builds symbol table information itself.  */
1575   if (link_info == NULL && abfd->symcount > 0)
1576     {
1577       if (! swap_out_syms (abfd, &strtab))
1578         return false;
1579     }
1580
1581   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
1582   /* sh_name was set in prep_headers.  */
1583   shstrtab_hdr->sh_type = SHT_STRTAB;
1584   shstrtab_hdr->sh_flags = 0;
1585   shstrtab_hdr->sh_addr = 0;
1586   shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1587   shstrtab_hdr->sh_entsize = 0;
1588   shstrtab_hdr->sh_link = 0;
1589   shstrtab_hdr->sh_info = 0;
1590   /* sh_offset is set in assign_file_positions_except_relocs.  */
1591   shstrtab_hdr->sh_addralign = 1;
1592
1593   if (!assign_file_positions_except_relocs (abfd))
1594     return false;
1595
1596   if (link_info == NULL && abfd->symcount > 0)
1597     {
1598       file_ptr off;
1599       Elf_Internal_Shdr *hdr;
1600
1601       off = elf_tdata (abfd)->next_file_pos;
1602
1603       hdr = &elf_tdata (abfd)->symtab_hdr;
1604       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1605
1606       hdr = &elf_tdata (abfd)->strtab_hdr;
1607       off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1608
1609       elf_tdata (abfd)->next_file_pos = off;
1610
1611       /* Now that we know where the .strtab section goes, write it
1612          out.  */
1613       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
1614           || ! _bfd_stringtab_emit (abfd, strtab))
1615         return false;
1616       _bfd_stringtab_free (strtab);
1617     }
1618
1619   abfd->output_has_begun = true;
1620
1621   return true;
1622 }
1623
1624 /* Create a mapping from a set of sections to a program segment.  */
1625
1626 static INLINE struct elf_segment_map *
1627 make_mapping (abfd, sections, from, to, phdr)
1628      bfd *abfd;
1629      asection **sections;
1630      unsigned int from;
1631      unsigned int to;
1632      boolean phdr;
1633 {
1634   struct elf_segment_map *m;
1635   unsigned int i;
1636   asection **hdrpp;
1637
1638   m = ((struct elf_segment_map *)
1639        bfd_zalloc (abfd,
1640                    (sizeof (struct elf_segment_map)
1641                     + (to - from - 1) * sizeof (asection *))));
1642   if (m == NULL)
1643     return NULL;
1644   m->next = NULL;
1645   m->p_type = PT_LOAD;
1646   for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
1647     m->sections[i - from] = *hdrpp;
1648   m->count = to - from;
1649
1650   if (from == 0 && phdr)
1651     {
1652       /* Include the headers in the first PT_LOAD segment.  */
1653       m->includes_filehdr = 1;
1654       m->includes_phdrs = 1;
1655     }
1656
1657   return m;
1658 }
1659
1660 /* Set up a mapping from BFD sections to program segments.  */
1661
1662 static boolean
1663 map_sections_to_segments (abfd)
1664      bfd *abfd;
1665 {
1666   asection **sections = NULL;
1667   asection *s;
1668   unsigned int i;
1669   unsigned int count;
1670   struct elf_segment_map *mfirst;
1671   struct elf_segment_map **pm;
1672   struct elf_segment_map *m;
1673   asection *last_hdr;
1674   unsigned int phdr_index;
1675   bfd_vma maxpagesize;
1676   asection **hdrpp;
1677   boolean phdr_in_section = true;
1678   boolean writable;
1679   asection *dynsec;
1680
1681   if (elf_tdata (abfd)->segment_map != NULL)
1682     return true;
1683
1684   if (bfd_count_sections (abfd) == 0)
1685     return true;
1686
1687   /* Select the allocated sections, and sort them.  */
1688
1689   sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
1690                                        * sizeof (asection *));
1691   if (sections == NULL)
1692     goto error_return;
1693
1694   i = 0;
1695   for (s = abfd->sections; s != NULL; s = s->next)
1696     {
1697       if ((s->flags & SEC_ALLOC) != 0)
1698         {
1699           sections[i] = s;
1700           ++i;
1701         }
1702     }
1703   BFD_ASSERT (i <= bfd_count_sections (abfd));
1704   count = i;
1705
1706   qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
1707
1708   /* Build the mapping.  */
1709
1710   mfirst = NULL;
1711   pm = &mfirst;
1712
1713   /* If we have a .interp section, then create a PT_PHDR segment for
1714      the program headers and a PT_INTERP segment for the .interp
1715      section.  */
1716   s = bfd_get_section_by_name (abfd, ".interp");
1717   if (s != NULL && (s->flags & SEC_LOAD) != 0)
1718     {
1719       m = ((struct elf_segment_map *)
1720            bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1721       if (m == NULL)
1722         goto error_return;
1723       m->next = NULL;
1724       m->p_type = PT_PHDR;
1725       /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not.  */
1726       m->p_flags = PF_R | PF_X;
1727       m->p_flags_valid = 1;
1728       m->includes_phdrs = 1;
1729
1730       *pm = m;
1731       pm = &m->next;
1732
1733       m = ((struct elf_segment_map *)
1734            bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1735       if (m == NULL)
1736         goto error_return;
1737       m->next = NULL;
1738       m->p_type = PT_INTERP;
1739       m->count = 1;
1740       m->sections[0] = s;
1741
1742       *pm = m;
1743       pm = &m->next;
1744     }
1745
1746   /* Look through the sections.  We put sections in the same program
1747      segment when the start of the second section can be placed within
1748      a few bytes of the end of the first section.  */
1749   last_hdr = NULL;
1750   phdr_index = 0;
1751   maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1752   writable = false;
1753   dynsec = bfd_get_section_by_name (abfd, ".dynamic");
1754   if (dynsec != NULL
1755       && (dynsec->flags & SEC_LOAD) == 0)
1756     dynsec = NULL;
1757
1758   /* Deal with -Ttext or something similar such that the first section
1759      is not adjacent to the program headers.  This is an
1760      approximation, since at this point we don't know exactly how many
1761      program headers we will need.  */
1762   if (count > 0)
1763     {
1764       bfd_size_type phdr_size;
1765
1766       phdr_size = elf_tdata (abfd)->program_header_size;
1767       if (phdr_size == 0)
1768         phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
1769       if ((abfd->flags & D_PAGED) == 0
1770           || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
1771         phdr_in_section = false;
1772     }
1773
1774   for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
1775     {
1776       asection *hdr;
1777       boolean new_segment;
1778
1779       hdr = *hdrpp;
1780
1781       /* See if this section and the last one will fit in the same
1782          segment.  */
1783
1784       if (last_hdr == NULL)
1785         {
1786           /* If we don't have a segment yet, then we don't need a new
1787              one (we build the last one after this loop).  */
1788           new_segment = false;
1789         }
1790       else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
1791         {
1792           /* If this section has a different relation between the
1793              virtual address and the load address, then we need a new
1794              segment.  */
1795           new_segment = true;
1796         }
1797       else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
1798                < hdr->lma)
1799         {
1800           /* If putting this section in this segment would force us to
1801              skip a page in the segment, then we need a new segment.  */
1802           new_segment = true;
1803         }
1804       else if ((abfd->flags & D_PAGED) == 0)
1805         {
1806           /* If the file is not demand paged, which means that we
1807              don't require the sections to be correctly aligned in the
1808              file, then there is no other reason for a new segment.  */
1809           new_segment = false;
1810         }
1811       else if ((last_hdr->flags & SEC_LOAD) == 0
1812                && (hdr->flags & SEC_LOAD) != 0)
1813         {
1814           /* We don't want to put a loadable section after a
1815              nonloadable section in the same segment.  */
1816           new_segment = true;
1817         }
1818       else if (! writable
1819                && (hdr->flags & SEC_READONLY) == 0
1820                && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
1821                    == hdr->lma))
1822         {
1823           /* We don't want to put a writable section in a read only
1824              segment, unless they are on the same page in memory
1825              anyhow.  We already know that the last section does not
1826              bring us past the current section on the page, so the
1827              only case in which the new section is not on the same
1828              page as the previous section is when the previous section
1829              ends precisely on a page boundary.  */
1830           new_segment = true;
1831         }
1832       else
1833         {
1834           /* Otherwise, we can use the same segment.  */
1835           new_segment = false;
1836         }
1837
1838       if (! new_segment)
1839         {
1840           if ((hdr->flags & SEC_READONLY) == 0)
1841             writable = true;
1842           last_hdr = hdr;
1843           continue;
1844         }
1845
1846       /* We need a new program segment.  We must create a new program
1847          header holding all the sections from phdr_index until hdr.  */
1848
1849       m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
1850       if (m == NULL)
1851         goto error_return;
1852
1853       *pm = m;
1854       pm = &m->next;
1855
1856       if ((hdr->flags & SEC_READONLY) == 0)
1857         writable = true;
1858       else
1859         writable = false;
1860
1861       last_hdr = hdr;
1862       phdr_index = i;
1863       phdr_in_section = false;
1864     }
1865
1866   /* Create a final PT_LOAD program segment.  */
1867   if (last_hdr != NULL)
1868     {
1869       m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
1870       if (m == NULL)
1871         goto error_return;
1872
1873       *pm = m;
1874       pm = &m->next;
1875     }
1876
1877   /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
1878   if (dynsec != NULL)
1879     {
1880       m = ((struct elf_segment_map *)
1881            bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1882       if (m == NULL)
1883         goto error_return;
1884       m->next = NULL;
1885       m->p_type = PT_DYNAMIC;
1886       m->count = 1;
1887       m->sections[0] = dynsec;
1888
1889       *pm = m;
1890       pm = &m->next;
1891     }
1892
1893   free (sections);
1894   sections = NULL;
1895
1896   elf_tdata (abfd)->segment_map = mfirst;
1897   return true;
1898
1899  error_return:
1900   if (sections != NULL)
1901     free (sections);
1902   return false;
1903 }
1904
1905 /* Sort sections by VMA.  */
1906
1907 static int
1908 elf_sort_sections (arg1, arg2)
1909      const PTR arg1;
1910      const PTR arg2;
1911 {
1912   const asection *sec1 = *(const asection **) arg1;
1913   const asection *sec2 = *(const asection **) arg2;
1914
1915   if (sec1->vma < sec2->vma)
1916     return -1;
1917   else if (sec1->vma > sec2->vma)
1918     return 1;
1919
1920   /* Sort by LMA.  Normally the LMA and the VMA will be the same, and
1921      this will do nothing.  */
1922   if (sec1->lma < sec2->lma)
1923     return -1;
1924   else if (sec1->lma > sec2->lma)
1925     return 1;
1926
1927   /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
1928
1929 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
1930
1931   if (TOEND (sec1))
1932     if (TOEND (sec2))
1933       return sec1->target_index - sec2->target_index;
1934     else 
1935       return 1;
1936
1937   if (TOEND (sec2))
1938     return -1;
1939
1940 #undef TOEND
1941
1942   /* Sort by size, to put zero sized sections before others at the
1943      same address.  */
1944
1945   if (sec1->_raw_size < sec2->_raw_size)
1946     return -1;
1947   if (sec1->_raw_size > sec2->_raw_size)
1948     return 1;
1949
1950   return sec1->target_index - sec2->target_index;
1951 }
1952
1953 /* Assign file positions to the sections based on the mapping from
1954    sections to segments.  This function also sets up some fields in
1955    the file header, and writes out the program headers.  */
1956
1957 static boolean
1958 assign_file_positions_for_segments (abfd)
1959      bfd *abfd;
1960 {
1961   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1962   unsigned int count;
1963   struct elf_segment_map *m;
1964   unsigned int alloc;
1965   Elf_Internal_Phdr *phdrs;
1966   file_ptr off, voff;
1967   bfd_vma filehdr_vaddr, filehdr_paddr;
1968   bfd_vma phdrs_vaddr, phdrs_paddr;
1969   Elf_Internal_Phdr *p;
1970
1971   if (elf_tdata (abfd)->segment_map == NULL)
1972     {
1973       if (! map_sections_to_segments (abfd))
1974         return false;
1975     }
1976
1977   if (bed->elf_backend_modify_segment_map)
1978     {
1979       if (! (*bed->elf_backend_modify_segment_map) (abfd))
1980         return false;
1981     }
1982
1983   count = 0;
1984   for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1985     ++count;
1986
1987   elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
1988   elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
1989   elf_elfheader (abfd)->e_phnum = count;
1990
1991   if (count == 0)
1992     return true;
1993
1994   /* If we already counted the number of program segments, make sure
1995      that we allocated enough space.  This happens when SIZEOF_HEADERS
1996      is used in a linker script.  */
1997   alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
1998   if (alloc != 0 && count > alloc)
1999     {
2000       ((*_bfd_error_handler)
2001        ("%s: Not enough room for program headers (allocated %u, need %u)",
2002         bfd_get_filename (abfd), alloc, count));
2003       bfd_set_error (bfd_error_bad_value);
2004       return false;
2005     }
2006
2007   if (alloc == 0)
2008     alloc = count;
2009
2010   phdrs = ((Elf_Internal_Phdr *)
2011            bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2012   if (phdrs == NULL)
2013     return false;
2014
2015   off = bed->s->sizeof_ehdr;
2016   off += alloc * bed->s->sizeof_phdr;
2017
2018   filehdr_vaddr = 0;
2019   filehdr_paddr = 0;
2020   phdrs_vaddr = 0;
2021   phdrs_paddr = 0;
2022   for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2023        m != NULL;
2024        m = m->next, p++)
2025     {
2026       unsigned int i;
2027       asection **secpp;
2028
2029       /* If elf_segment_map is not from map_sections_to_segments, the
2030          sections may not be correctly ordered.  */
2031       if (m->count > 0)
2032         qsort (m->sections, (size_t) m->count, sizeof (asection *),
2033                elf_sort_sections);
2034
2035       p->p_type = m->p_type;
2036
2037       if (m->p_flags_valid)
2038         p->p_flags = m->p_flags;
2039       else
2040         p->p_flags = 0;
2041
2042       if (p->p_type == PT_LOAD
2043           && m->count > 0
2044           && (m->sections[0]->flags & SEC_ALLOC) != 0)
2045         {
2046           if ((abfd->flags & D_PAGED) != 0)
2047             off += (m->sections[0]->vma - off) % bed->maxpagesize;
2048           else
2049             off += ((m->sections[0]->vma - off)
2050                     % (1 << bfd_get_section_alignment (abfd, m->sections[0])));
2051         }
2052
2053       if (m->count == 0)
2054         p->p_vaddr = 0;
2055       else
2056         p->p_vaddr = m->sections[0]->vma;
2057
2058       if (m->p_paddr_valid)
2059         p->p_paddr = m->p_paddr;
2060       else if (m->count == 0)
2061         p->p_paddr = 0;
2062       else
2063         p->p_paddr = m->sections[0]->lma;
2064
2065       if (p->p_type == PT_LOAD
2066           && (abfd->flags & D_PAGED) != 0)
2067         p->p_align = bed->maxpagesize;
2068       else if (m->count == 0)
2069         p->p_align = bed->s->file_align;
2070       else
2071         p->p_align = 0;
2072
2073       p->p_offset = 0;
2074       p->p_filesz = 0;
2075       p->p_memsz = 0;
2076
2077       if (m->includes_filehdr)
2078         {
2079           if (! m->p_flags_valid)
2080             p->p_flags |= PF_R;
2081           p->p_offset = 0;
2082           p->p_filesz = bed->s->sizeof_ehdr;
2083           p->p_memsz = bed->s->sizeof_ehdr;
2084           if (m->count > 0)
2085             {
2086               BFD_ASSERT (p->p_type == PT_LOAD);
2087               p->p_vaddr -= off;
2088               if (! m->p_paddr_valid)
2089                 p->p_paddr -= off;
2090             }
2091           if (p->p_type == PT_LOAD)
2092             {
2093               filehdr_vaddr = p->p_vaddr;
2094               filehdr_paddr = p->p_paddr;
2095             }
2096         }
2097
2098       if (m->includes_phdrs)
2099         {
2100           if (! m->p_flags_valid)
2101             p->p_flags |= PF_R;
2102           if (m->includes_filehdr)
2103             {
2104               if (p->p_type == PT_LOAD)
2105                 {
2106                   phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2107                   phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2108                 }
2109             }
2110           else
2111             {
2112               p->p_offset = bed->s->sizeof_ehdr;
2113               if (m->count > 0)
2114                 {
2115                   BFD_ASSERT (p->p_type == PT_LOAD);
2116                   p->p_vaddr -= off - p->p_offset;
2117                   if (! m->p_paddr_valid)
2118                     p->p_paddr -= off - p->p_offset;
2119                 }
2120               if (p->p_type == PT_LOAD)
2121                 {
2122                   phdrs_vaddr = p->p_vaddr;
2123                   phdrs_paddr = p->p_paddr;
2124                 }
2125             }
2126           p->p_filesz += alloc * bed->s->sizeof_phdr;
2127           p->p_memsz += alloc * bed->s->sizeof_phdr;
2128         }
2129
2130       if (p->p_type == PT_LOAD)
2131         {
2132           if (! m->includes_filehdr && ! m->includes_phdrs)
2133             p->p_offset = off;
2134           else
2135             {
2136               file_ptr adjust;
2137
2138               adjust = off - (p->p_offset + p->p_filesz);
2139               p->p_filesz += adjust;
2140               p->p_memsz += adjust;
2141             }
2142         }
2143
2144       voff = off;
2145       for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2146         {
2147           asection *sec;
2148           flagword flags;
2149           bfd_size_type align;
2150
2151           sec = *secpp;
2152           flags = sec->flags;
2153           align = 1 << bfd_get_section_alignment (abfd, sec);
2154
2155           if (p->p_type == PT_LOAD)
2156             {
2157               bfd_vma adjust;
2158
2159               /* The section VMA must equal the file position modulo
2160                  the page size.  */
2161               if ((flags & SEC_ALLOC) != 0)
2162                 {
2163                   if ((abfd->flags & D_PAGED) != 0)
2164                     adjust = (sec->vma - voff) % bed->maxpagesize;
2165                   else
2166                     adjust = (sec->vma - voff) % align;
2167                   if (adjust != 0)
2168                     {
2169                       if (i == 0)
2170                         abort ();
2171                       p->p_memsz += adjust;
2172                       off += adjust;
2173                       voff += adjust;
2174                       if ((flags & SEC_LOAD) != 0)
2175                         p->p_filesz += adjust;
2176                     }
2177                 }
2178
2179               sec->filepos = off;
2180
2181               if ((flags & SEC_LOAD) != 0)
2182                 off += sec->_raw_size;
2183               if ((flags & SEC_ALLOC) != 0)
2184                 voff += sec->_raw_size;
2185             }
2186
2187           p->p_memsz += sec->_raw_size;
2188
2189           if ((flags & SEC_LOAD) != 0)
2190             p->p_filesz += sec->_raw_size;
2191
2192           if (align > p->p_align)
2193             p->p_align = align;
2194
2195           if (! m->p_flags_valid)
2196             {
2197               p->p_flags |= PF_R;
2198               if ((flags & SEC_CODE) != 0)
2199                 p->p_flags |= PF_X;
2200               if ((flags & SEC_READONLY) == 0)
2201                 p->p_flags |= PF_W;
2202             }
2203         }
2204     }
2205
2206   /* Now that we have set the section file positions, we can set up
2207      the file positions for the non PT_LOAD segments.  */
2208   for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2209        m != NULL;
2210        m = m->next, p++)
2211     {
2212       if (p->p_type != PT_LOAD && m->count > 0)
2213         {
2214           BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2215           p->p_offset = m->sections[0]->filepos;
2216         }
2217       if (m->count == 0)
2218         {
2219           if (m->includes_filehdr)
2220             {
2221               p->p_vaddr = filehdr_vaddr;
2222               if (! m->p_paddr_valid)
2223                 p->p_paddr = filehdr_paddr;
2224             }
2225           else if (m->includes_phdrs)
2226             {
2227               p->p_vaddr = phdrs_vaddr;
2228               if (! m->p_paddr_valid)
2229                 p->p_paddr = phdrs_paddr;
2230             }
2231         }
2232     }
2233
2234   /* Clear out any program headers we allocated but did not use.  */
2235   for (; count < alloc; count++, p++)
2236     {
2237       memset (p, 0, sizeof *p);
2238       p->p_type = PT_NULL;
2239     }
2240
2241   elf_tdata (abfd)->phdr = phdrs;
2242
2243   elf_tdata (abfd)->next_file_pos = off;
2244
2245   /* Write out the program headers.  */
2246   if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2247       || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2248     return false;
2249
2250   return true;
2251 }
2252
2253 /* Get the size of the program header.
2254
2255    If this is called by the linker before any of the section VMA's are set, it
2256    can't calculate the correct value for a strange memory layout.  This only
2257    happens when SIZEOF_HEADERS is used in a linker script.  In this case,
2258    SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2259    data segment (exclusive of .interp and .dynamic).
2260
2261    ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2262    will be two segments.  */
2263
2264 static bfd_size_type
2265 get_program_header_size (abfd)
2266      bfd *abfd;
2267 {
2268   size_t segs;
2269   asection *s;
2270   struct elf_backend_data *bed = get_elf_backend_data (abfd);
2271
2272   /* We can't return a different result each time we're called.  */
2273   if (elf_tdata (abfd)->program_header_size != 0)
2274     return elf_tdata (abfd)->program_header_size;
2275
2276   if (elf_tdata (abfd)->segment_map != NULL)
2277     {
2278       struct elf_segment_map *m;
2279
2280       segs = 0;
2281       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2282         ++segs;
2283       elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2284       return elf_tdata (abfd)->program_header_size;
2285     }
2286
2287   /* Assume we will need exactly two PT_LOAD segments: one for text
2288      and one for data.  */
2289   segs = 2;
2290
2291   s = bfd_get_section_by_name (abfd, ".interp");
2292   if (s != NULL && (s->flags & SEC_LOAD) != 0)
2293     {
2294       /* If we have a loadable interpreter section, we need a
2295          PT_INTERP segment.  In this case, assume we also need a
2296          PT_PHDR segment, although that may not be true for all
2297          targets.  */
2298       segs += 2;
2299     }
2300
2301   if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
2302     {
2303       /* We need a PT_DYNAMIC segment.  */
2304       ++segs;
2305     }
2306
2307   /* Let the backend count up any program headers it might need.  */
2308   if (bed->elf_backend_additional_program_headers)
2309     {
2310       int a;
2311
2312       a = (*bed->elf_backend_additional_program_headers) (abfd);
2313       if (a == -1)
2314         abort ();
2315       segs += a;
2316     }
2317
2318   elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2319   return elf_tdata (abfd)->program_header_size;
2320 }
2321
2322 /* Work out the file positions of all the sections.  This is called by
2323    _bfd_elf_compute_section_file_positions.  All the section sizes and
2324    VMAs must be known before this is called.
2325
2326    We do not consider reloc sections at this point, unless they form
2327    part of the loadable image.  Reloc sections are assigned file
2328    positions in assign_file_positions_for_relocs, which is called by
2329    write_object_contents and final_link.
2330
2331    We also don't set the positions of the .symtab and .strtab here.  */
2332
2333 static boolean
2334 assign_file_positions_except_relocs (abfd)
2335      bfd *abfd;
2336 {
2337   struct elf_obj_tdata * const tdata = elf_tdata (abfd);
2338   Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
2339   Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
2340   file_ptr off;
2341   struct elf_backend_data *bed = get_elf_backend_data (abfd);
2342
2343   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2344     {
2345       Elf_Internal_Shdr **hdrpp;
2346       unsigned int i;
2347
2348       /* Start after the ELF header.  */
2349       off = i_ehdrp->e_ehsize;
2350
2351       /* We are not creating an executable, which means that we are
2352          not creating a program header, and that the actual order of
2353          the sections in the file is unimportant.  */
2354       for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2355         {
2356           Elf_Internal_Shdr *hdr;
2357
2358           hdr = *hdrpp;
2359           if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2360             {
2361               hdr->sh_offset = -1;
2362               continue;
2363             }
2364           if (i == tdata->symtab_section
2365               || i == tdata->strtab_section)
2366             {
2367               hdr->sh_offset = -1;
2368               continue;
2369             }
2370           
2371           off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2372         }
2373     }
2374   else
2375     {
2376       unsigned int i;
2377       Elf_Internal_Shdr **hdrpp;
2378
2379       /* Assign file positions for the loaded sections based on the
2380          assignment of sections to segments.  */
2381       if (! assign_file_positions_for_segments (abfd))
2382         return false;
2383
2384       /* Assign file positions for the other sections.  */
2385
2386       off = elf_tdata (abfd)->next_file_pos;
2387       for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2388         {
2389           Elf_Internal_Shdr *hdr;
2390
2391           hdr = *hdrpp;
2392           if (hdr->bfd_section != NULL
2393               && hdr->bfd_section->filepos != 0)
2394             hdr->sh_offset = hdr->bfd_section->filepos;
2395           else if ((hdr->sh_flags & SHF_ALLOC) != 0)
2396             {
2397               ((*_bfd_error_handler)
2398                ("%s: warning: allocated section `%s' not in segment",
2399                 bfd_get_filename (abfd),
2400                 (hdr->bfd_section == NULL
2401                  ? "*unknown*"
2402                  : hdr->bfd_section->name)));
2403               if ((abfd->flags & D_PAGED) != 0)
2404                 off += (hdr->sh_addr - off) % bed->maxpagesize;
2405               else
2406                 off += (hdr->sh_addr - off) % hdr->sh_addralign;
2407               off = _bfd_elf_assign_file_position_for_section (hdr, off,
2408                                                                false);
2409             }
2410           else if (hdr->sh_type == SHT_REL
2411                    || hdr->sh_type == SHT_RELA
2412                    || hdr == i_shdrpp[tdata->symtab_section]
2413                    || hdr == i_shdrpp[tdata->strtab_section])
2414             hdr->sh_offset = -1;
2415           else
2416             off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2417         }                  
2418     }
2419
2420   /* Place the section headers.  */
2421   off = align_file_position (off, bed->s->file_align);
2422   i_ehdrp->e_shoff = off;
2423   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2424
2425   elf_tdata (abfd)->next_file_pos = off;
2426
2427   return true;
2428 }
2429
2430 static boolean
2431 prep_headers (abfd)
2432      bfd *abfd;
2433 {
2434   Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */
2435   Elf_Internal_Phdr *i_phdrp = 0;       /* Program header table, internal form */
2436   Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
2437   int count;
2438   struct bfd_strtab_hash *shstrtab;
2439   struct elf_backend_data *bed = get_elf_backend_data (abfd);
2440
2441   i_ehdrp = elf_elfheader (abfd);
2442   i_shdrp = elf_elfsections (abfd);
2443
2444   shstrtab = _bfd_elf_stringtab_init ();
2445   if (shstrtab == NULL)
2446     return false;
2447
2448   elf_shstrtab (abfd) = shstrtab;
2449
2450   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2451   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2452   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2453   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2454
2455   i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
2456   i_ehdrp->e_ident[EI_DATA] =
2457     bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
2458   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
2459
2460   for (count = EI_PAD; count < EI_NIDENT; count++)
2461     i_ehdrp->e_ident[count] = 0;
2462
2463   if ((abfd->flags & DYNAMIC) != 0)
2464     i_ehdrp->e_type = ET_DYN;
2465   else if ((abfd->flags & EXEC_P) != 0)
2466     i_ehdrp->e_type = ET_EXEC;
2467   else
2468     i_ehdrp->e_type = ET_REL;
2469
2470   switch (bfd_get_arch (abfd))
2471     {
2472     case bfd_arch_unknown:
2473       i_ehdrp->e_machine = EM_NONE;
2474       break;
2475     case bfd_arch_sparc:
2476       if (bed->s->arch_size == 64)
2477         i_ehdrp->e_machine = EM_SPARC64;
2478       else
2479         i_ehdrp->e_machine = EM_SPARC;
2480       break;
2481     case bfd_arch_i386:
2482       i_ehdrp->e_machine = EM_386;
2483       break;
2484     case bfd_arch_m68k:
2485       i_ehdrp->e_machine = EM_68K;
2486       break;
2487     case bfd_arch_m88k:
2488       i_ehdrp->e_machine = EM_88K;
2489       break;
2490     case bfd_arch_i860:
2491       i_ehdrp->e_machine = EM_860;
2492       break;
2493     case bfd_arch_mips: /* MIPS Rxxxx */
2494       i_ehdrp->e_machine = EM_MIPS;     /* only MIPS R3000 */
2495       break;
2496     case bfd_arch_hppa:
2497       i_ehdrp->e_machine = EM_PARISC;
2498       break;
2499     case bfd_arch_powerpc:
2500       i_ehdrp->e_machine = EM_PPC;
2501       break;
2502     case bfd_arch_alpha:
2503       i_ehdrp->e_machine = EM_ALPHA;
2504       break;
2505     case bfd_arch_sh:
2506       i_ehdrp->e_machine = EM_SH;
2507       break;
2508 /* start-sanitize-d10v */
2509     case bfd_arch_d10v:
2510       i_ehdrp->e_machine = EM_CYGNUS_D10V;
2511       break;
2512 /* end-sanitize-d10v */
2513 /* start-sanitize-v850 */
2514     case bfd_arch_v850:
2515       i_ehdrp->e_machine = EM_CYGNUS_V850;
2516       break;
2517 /* end-sanitize-v850 */
2518 /* start-sanitize-arc */
2519     case bfd_arch_arc:
2520       i_ehdrp->e_machine = EM_CYGNUS_ARC;
2521       break;
2522 /* end-sanitize-arc */
2523 /* start-sanitize-m32r */
2524     case bfd_arch_m32r:
2525       i_ehdrp->e_machine = EM_CYGNUS_M32R;
2526       break;
2527 /* end-sanitize-m32r */
2528     case bfd_arch_mn10x00:
2529       i_ehdrp->e_machine = EM_CYGNUS_MN10x00;
2530       break;
2531       /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2532     default:
2533       i_ehdrp->e_machine = EM_NONE;
2534     }
2535   i_ehdrp->e_version = bed->s->ev_current;
2536   i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
2537
2538   /* no program header, for now. */
2539   i_ehdrp->e_phoff = 0;
2540   i_ehdrp->e_phentsize = 0;
2541   i_ehdrp->e_phnum = 0;
2542
2543   /* each bfd section is section header entry */
2544   i_ehdrp->e_entry = bfd_get_start_address (abfd);
2545   i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
2546
2547   /* if we're building an executable, we'll need a program header table */
2548   if (abfd->flags & EXEC_P)
2549     {
2550       /* it all happens later */
2551 #if 0
2552       i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2553
2554       /* elf_build_phdrs() returns a (NULL-terminated) array of
2555          Elf_Internal_Phdrs */
2556       i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2557       i_ehdrp->e_phoff = outbase;
2558       outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2559 #endif
2560     }
2561   else
2562     {
2563       i_ehdrp->e_phentsize = 0;
2564       i_phdrp = 0;
2565       i_ehdrp->e_phoff = 0;
2566     }
2567
2568   elf_tdata (abfd)->symtab_hdr.sh_name =
2569     (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
2570   elf_tdata (abfd)->strtab_hdr.sh_name =
2571     (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
2572   elf_tdata (abfd)->shstrtab_hdr.sh_name =
2573     (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
2574   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2575       || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2576       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
2577     return false;
2578
2579   return true;
2580 }
2581
2582 /* Assign file positions for all the reloc sections which are not part
2583    of the loadable file image.  */
2584
2585 void
2586 _bfd_elf_assign_file_positions_for_relocs (abfd)
2587      bfd *abfd;
2588 {
2589   file_ptr off;
2590   unsigned int i;
2591   Elf_Internal_Shdr **shdrpp;
2592
2593   off = elf_tdata (abfd)->next_file_pos;
2594
2595   for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
2596        i < elf_elfheader (abfd)->e_shnum;
2597        i++, shdrpp++)
2598     {
2599       Elf_Internal_Shdr *shdrp;
2600
2601       shdrp = *shdrpp;
2602       if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
2603           && shdrp->sh_offset == -1)
2604         off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
2605     }
2606
2607   elf_tdata (abfd)->next_file_pos = off;
2608 }
2609
2610 boolean
2611 _bfd_elf_write_object_contents (abfd)
2612      bfd *abfd;
2613 {
2614   struct elf_backend_data *bed = get_elf_backend_data (abfd);
2615   Elf_Internal_Ehdr *i_ehdrp;
2616   Elf_Internal_Shdr **i_shdrp;
2617   boolean failed;
2618   unsigned int count;
2619
2620   if (! abfd->output_has_begun
2621       && ! _bfd_elf_compute_section_file_positions (abfd,
2622                                                     (struct bfd_link_info *) NULL))
2623     return false;
2624
2625   i_shdrp = elf_elfsections (abfd);
2626   i_ehdrp = elf_elfheader (abfd);
2627
2628   failed = false;
2629   bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
2630   if (failed)
2631     return false;
2632   _bfd_elf_assign_file_positions_for_relocs (abfd);
2633
2634   /* After writing the headers, we need to write the sections too... */
2635   for (count = 1; count < i_ehdrp->e_shnum; count++)
2636     {
2637       if (bed->elf_backend_section_processing)
2638         (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
2639       if (i_shdrp[count]->contents)
2640         {
2641           if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
2642               || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
2643                              1, abfd)
2644                   != i_shdrp[count]->sh_size))
2645             return false;
2646         }
2647     }
2648
2649   /* Write out the section header names.  */
2650   if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
2651       || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
2652     return false;
2653
2654   if (bed->elf_backend_final_write_processing)
2655     (*bed->elf_backend_final_write_processing) (abfd,
2656                                                 elf_tdata (abfd)->linker);
2657
2658   return bed->s->write_shdrs_and_ehdr (abfd);
2659 }
2660
2661 /* given a section, search the header to find them... */
2662 int
2663 _bfd_elf_section_from_bfd_section (abfd, asect)
2664      bfd *abfd;
2665      struct sec *asect;
2666 {
2667   struct elf_backend_data *bed = get_elf_backend_data (abfd);
2668   Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
2669   int index;
2670   Elf_Internal_Shdr *hdr;
2671   int maxindex = elf_elfheader (abfd)->e_shnum;
2672
2673   for (index = 0; index < maxindex; index++)
2674     {
2675       hdr = i_shdrp[index];
2676       if (hdr->bfd_section == asect)
2677         return index;
2678     }
2679
2680   if (bed->elf_backend_section_from_bfd_section)
2681     {
2682       for (index = 0; index < maxindex; index++)
2683         {
2684           int retval;
2685
2686           hdr = i_shdrp[index];
2687           retval = index;
2688           if ((*bed->elf_backend_section_from_bfd_section)
2689               (abfd, hdr, asect, &retval))
2690             return retval;
2691         }
2692     }
2693
2694   if (bfd_is_abs_section (asect))
2695     return SHN_ABS;
2696   if (bfd_is_com_section (asect))
2697     return SHN_COMMON;
2698   if (bfd_is_und_section (asect))
2699     return SHN_UNDEF;
2700
2701   return -1;
2702 }
2703
2704 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
2705    on error.  */
2706
2707 int
2708 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
2709      bfd *abfd;
2710      asymbol **asym_ptr_ptr;
2711 {
2712   asymbol *asym_ptr = *asym_ptr_ptr;
2713   int idx;
2714   flagword flags = asym_ptr->flags;
2715
2716   /* When gas creates relocations against local labels, it creates its
2717      own symbol for the section, but does put the symbol into the
2718      symbol chain, so udata is 0.  When the linker is generating
2719      relocatable output, this section symbol may be for one of the
2720      input sections rather than the output section.  */
2721   if (asym_ptr->udata.i == 0
2722       && (flags & BSF_SECTION_SYM)
2723       && asym_ptr->section)
2724     {
2725       int indx;
2726
2727       if (asym_ptr->section->output_section != NULL)
2728         indx = asym_ptr->section->output_section->index;
2729       else
2730         indx = asym_ptr->section->index;
2731       if (elf_section_syms (abfd)[indx])
2732         asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
2733     }
2734
2735   idx = asym_ptr->udata.i;
2736
2737   if (idx == 0)
2738     {
2739       /* This case can occur when using --strip-symbol on a symbol
2740          which is used in a relocation entry.  */
2741       (*_bfd_error_handler)
2742         ("%s: symbol `%s' required but not present",
2743          bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
2744       bfd_set_error (bfd_error_no_symbols);
2745       return -1;
2746     }
2747
2748 #if DEBUG & 4
2749   {
2750     fprintf (stderr,
2751              "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
2752              (long) asym_ptr, asym_ptr->name, idx, flags,
2753              elf_symbol_flags (flags));
2754     fflush (stderr);
2755   }
2756 #endif
2757
2758   return idx;
2759 }
2760
2761 /* Copy private BFD data.  This copies any program header information.  */
2762
2763 static boolean
2764 copy_private_bfd_data (ibfd, obfd)
2765      bfd *ibfd;
2766      bfd *obfd;
2767 {
2768   Elf_Internal_Ehdr *iehdr;
2769   struct elf_segment_map *mfirst;
2770   struct elf_segment_map **pm;
2771   Elf_Internal_Phdr *p;
2772   unsigned int i, c;
2773
2774   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2775       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2776     return true;
2777
2778   if (elf_tdata (ibfd)->phdr == NULL)
2779     return true;
2780
2781   iehdr = elf_elfheader (ibfd);
2782
2783   mfirst = NULL;
2784   pm = &mfirst;
2785
2786   c = elf_elfheader (ibfd)->e_phnum;
2787   for (i = 0, p = elf_tdata (ibfd)->phdr; i < c; i++, p++)
2788     {
2789       unsigned int csecs;
2790       asection *s;
2791       struct elf_segment_map *m;
2792       unsigned int isec;
2793
2794       csecs = 0;
2795
2796       /* The complicated case when p_vaddr is 0 is to handle the
2797          Solaris linker, which generates a PT_INTERP section with
2798          p_vaddr and p_memsz set to 0.  */
2799       for (s = ibfd->sections; s != NULL; s = s->next)
2800         if (((s->vma >= p->p_vaddr
2801               && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
2802                   || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
2803              || (p->p_vaddr == 0
2804                  && p->p_filesz > 0
2805                  && (s->flags & SEC_HAS_CONTENTS) != 0
2806                  && (bfd_vma) s->filepos >= p->p_offset
2807                  && ((bfd_vma) s->filepos + s->_raw_size
2808                      <= p->p_offset + p->p_filesz)))
2809             && (s->flags & SEC_ALLOC) != 0
2810             && s->output_section != NULL)
2811           ++csecs;
2812
2813       m = ((struct elf_segment_map *)
2814            bfd_alloc (obfd,
2815                       (sizeof (struct elf_segment_map)
2816                        + (csecs - 1) * sizeof (asection *))));
2817       if (m == NULL)
2818         return false;
2819
2820       m->next = NULL;
2821       m->p_type = p->p_type;
2822       m->p_flags = p->p_flags;
2823       m->p_flags_valid = 1;
2824       m->p_paddr = p->p_paddr;
2825       m->p_paddr_valid = 1;
2826
2827       m->includes_filehdr = (p->p_offset == 0
2828                              && p->p_filesz >= iehdr->e_ehsize);
2829
2830       m->includes_phdrs = (p->p_offset <= (bfd_vma) iehdr->e_phoff
2831                            && (p->p_offset + p->p_filesz
2832                                >= ((bfd_vma) iehdr->e_phoff
2833                                    + iehdr->e_phnum * iehdr->e_phentsize)));
2834
2835       isec = 0;
2836       for (s = ibfd->sections; s != NULL; s = s->next)
2837         {
2838           if (((s->vma >= p->p_vaddr
2839                 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
2840                     || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
2841                || (p->p_vaddr == 0
2842                    && p->p_filesz > 0
2843                    && (s->flags & SEC_HAS_CONTENTS) != 0
2844                    && (bfd_vma) s->filepos >= p->p_offset
2845                    && ((bfd_vma) s->filepos + s->_raw_size
2846                        <= p->p_offset + p->p_filesz)))
2847               && (s->flags & SEC_ALLOC) != 0
2848               && s->output_section != NULL)
2849             {
2850               m->sections[isec] = s->output_section;
2851               ++isec;
2852             }
2853         }
2854       BFD_ASSERT (isec == csecs);
2855       m->count = csecs;
2856
2857       *pm = m;
2858       pm = &m->next;
2859     }
2860
2861   elf_tdata (obfd)->segment_map = mfirst;
2862
2863   return true;
2864 }
2865
2866 /* Copy private section information.  This copies over the entsize
2867    field, and sometimes the info field.  */
2868
2869 boolean
2870 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
2871      bfd *ibfd;
2872      asection *isec;
2873      bfd *obfd;
2874      asection *osec;
2875 {
2876   Elf_Internal_Shdr *ihdr, *ohdr;
2877
2878   if (ibfd->xvec->flavour != bfd_target_elf_flavour
2879       || obfd->xvec->flavour != bfd_target_elf_flavour)
2880     return true;
2881
2882   /* Copy over private BFD data if it has not already been copied.
2883      This must be done here, rather than in the copy_private_bfd_data
2884      entry point, because the latter is called after the section
2885      contents have been set, which means that the program headers have
2886      already been worked out.  */
2887   if (elf_tdata (obfd)->segment_map == NULL
2888       && elf_tdata (ibfd)->phdr != NULL)
2889     {
2890       asection *s;
2891
2892       /* Only set up the segments when all the sections have been set
2893          up.  */
2894       for (s = ibfd->sections; s != NULL; s = s->next)
2895         if (s->output_section == NULL)
2896           break;
2897       if (s == NULL)
2898         {
2899           if (! copy_private_bfd_data (ibfd, obfd))
2900             return false;
2901         }
2902     }
2903
2904   ihdr = &elf_section_data (isec)->this_hdr;
2905   ohdr = &elf_section_data (osec)->this_hdr;
2906
2907   ohdr->sh_entsize = ihdr->sh_entsize;
2908
2909   if (ihdr->sh_type == SHT_SYMTAB
2910       || ihdr->sh_type == SHT_DYNSYM)
2911     ohdr->sh_info = ihdr->sh_info;
2912
2913   return true;
2914 }
2915
2916 /* Copy private symbol information.  If this symbol is in a section
2917    which we did not map into a BFD section, try to map the section
2918    index correctly.  We use special macro definitions for the mapped
2919    section indices; these definitions are interpreted by the
2920    swap_out_syms function.  */
2921
2922 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
2923 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
2924 #define MAP_STRTAB (SHN_LORESERVE - 3)
2925 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
2926
2927 boolean
2928 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
2929      bfd *ibfd;
2930      asymbol *isymarg;
2931      bfd *obfd;
2932      asymbol *osymarg;
2933 {
2934   elf_symbol_type *isym, *osym;
2935
2936   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2937       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2938     return true;
2939
2940   isym = elf_symbol_from (ibfd, isymarg);
2941   osym = elf_symbol_from (obfd, osymarg);
2942
2943   if (isym != NULL
2944       && osym != NULL
2945       && bfd_is_abs_section (isym->symbol.section))
2946     {
2947       unsigned int shndx;
2948
2949       shndx = isym->internal_elf_sym.st_shndx;
2950       if (shndx == elf_onesymtab (ibfd))
2951         shndx = MAP_ONESYMTAB;
2952       else if (shndx == elf_dynsymtab (ibfd))
2953         shndx = MAP_DYNSYMTAB;
2954       else if (shndx == elf_tdata (ibfd)->strtab_section)
2955         shndx = MAP_STRTAB;
2956       else if (shndx == elf_tdata (ibfd)->shstrtab_section)
2957         shndx = MAP_SHSTRTAB;
2958       osym->internal_elf_sym.st_shndx = shndx;
2959     }
2960
2961   return true;
2962 }
2963
2964 /* Swap out the symbols.  */
2965
2966 static boolean
2967 swap_out_syms (abfd, sttp)
2968      bfd *abfd;
2969      struct bfd_strtab_hash **sttp;
2970 {
2971   struct elf_backend_data *bed = get_elf_backend_data (abfd);
2972
2973   if (!elf_map_symbols (abfd))
2974     return false;
2975
2976   /* Dump out the symtabs. */
2977   {
2978     int symcount = bfd_get_symcount (abfd);
2979     asymbol **syms = bfd_get_outsymbols (abfd);
2980     struct bfd_strtab_hash *stt;
2981     Elf_Internal_Shdr *symtab_hdr;
2982     Elf_Internal_Shdr *symstrtab_hdr;
2983     char *outbound_syms;
2984     int idx;
2985
2986     stt = _bfd_elf_stringtab_init ();
2987     if (stt == NULL)
2988       return false;
2989
2990     symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2991     symtab_hdr->sh_type = SHT_SYMTAB;
2992     symtab_hdr->sh_entsize = bed->s->sizeof_sym;
2993     symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
2994     symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
2995     symtab_hdr->sh_addralign = bed->s->file_align;
2996
2997     symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
2998     symstrtab_hdr->sh_type = SHT_STRTAB;
2999
3000     outbound_syms = bfd_alloc (abfd,
3001                                (1 + symcount) * bed->s->sizeof_sym);
3002     if (outbound_syms == NULL)
3003       return false;
3004     symtab_hdr->contents = (PTR) outbound_syms;
3005
3006     /* now generate the data (for "contents") */
3007     {
3008       /* Fill in zeroth symbol and swap it out.  */
3009       Elf_Internal_Sym sym;
3010       sym.st_name = 0;
3011       sym.st_value = 0;
3012       sym.st_size = 0;
3013       sym.st_info = 0;
3014       sym.st_other = 0;
3015       sym.st_shndx = SHN_UNDEF;
3016       bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
3017       outbound_syms += bed->s->sizeof_sym;
3018     }
3019     for (idx = 0; idx < symcount; idx++)
3020       {
3021         Elf_Internal_Sym sym;
3022         bfd_vma value = syms[idx]->value;
3023         elf_symbol_type *type_ptr;
3024         flagword flags = syms[idx]->flags;
3025         int type;
3026
3027         if (flags & BSF_SECTION_SYM)
3028           /* Section symbols have no names.  */
3029           sym.st_name = 0;
3030         else
3031           {
3032             sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
3033                                                               syms[idx]->name,
3034                                                               true, false);
3035             if (sym.st_name == (unsigned long) -1)
3036               return false;
3037           }
3038
3039         type_ptr = elf_symbol_from (abfd, syms[idx]);
3040
3041         if (bfd_is_com_section (syms[idx]->section))
3042           {
3043             /* ELF common symbols put the alignment into the `value' field,
3044                and the size into the `size' field.  This is backwards from
3045                how BFD handles it, so reverse it here.  */
3046             sym.st_size = value;
3047             if (type_ptr == NULL
3048                 || type_ptr->internal_elf_sym.st_value == 0)
3049               sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
3050             else
3051               sym.st_value = type_ptr->internal_elf_sym.st_value;
3052             sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd,
3053                                                               syms[idx]->section);
3054           }
3055         else
3056           {
3057             asection *sec = syms[idx]->section;
3058             int shndx;
3059
3060             if (sec->output_section)
3061               {
3062                 value += sec->output_offset;
3063                 sec = sec->output_section;
3064               }
3065             value += sec->vma;
3066             sym.st_value = value;
3067             sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
3068
3069             if (bfd_is_abs_section (sec)
3070                 && type_ptr != NULL
3071                 && type_ptr->internal_elf_sym.st_shndx != 0)
3072               {
3073                 /* This symbol is in a real ELF section which we did
3074                    not create as a BFD section.  Undo the mapping done
3075                    by copy_private_symbol_data.  */
3076                 shndx = type_ptr->internal_elf_sym.st_shndx;
3077                 switch (shndx)
3078                   {
3079                   case MAP_ONESYMTAB:
3080                     shndx = elf_onesymtab (abfd);
3081                     break;
3082                   case MAP_DYNSYMTAB:
3083                     shndx = elf_dynsymtab (abfd);
3084                     break;
3085                   case MAP_STRTAB:
3086                     shndx = elf_tdata (abfd)->strtab_section;
3087                     break;
3088                   case MAP_SHSTRTAB:
3089                     shndx = elf_tdata (abfd)->shstrtab_section;
3090                     break;
3091                   default:
3092                     break;
3093                   }
3094               }
3095             else
3096               {
3097                 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3098
3099                 if (shndx == -1)
3100                   {
3101                     asection *sec2;
3102
3103                     /* Writing this would be a hell of a lot easier if
3104                        we had some decent documentation on bfd, and
3105                        knew what to expect of the library, and what to
3106                        demand of applications.  For example, it
3107                        appears that `objcopy' might not set the
3108                        section of a symbol to be a section that is
3109                        actually in the output file.  */
3110                     sec2 = bfd_get_section_by_name (abfd, sec->name);
3111                     BFD_ASSERT (sec2 != 0);
3112                     shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
3113                     BFD_ASSERT (shndx != -1);
3114                   }
3115               }
3116
3117             sym.st_shndx = shndx;
3118           }
3119
3120         if ((flags & BSF_FUNCTION) != 0)
3121           type = STT_FUNC;
3122         else if ((flags & BSF_OBJECT) != 0)
3123           type = STT_OBJECT;
3124         else
3125           type = STT_NOTYPE;
3126
3127         if (bfd_is_com_section (syms[idx]->section))
3128           sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
3129         else if (bfd_is_und_section (syms[idx]->section))
3130           sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
3131                                       ? STB_WEAK
3132                                       : STB_GLOBAL),
3133                                      type);
3134         else if (flags & BSF_SECTION_SYM)
3135           sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3136         else if (flags & BSF_FILE)
3137           sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3138         else
3139           {
3140             int bind = STB_LOCAL;
3141
3142             if (flags & BSF_LOCAL)
3143               bind = STB_LOCAL;
3144             else if (flags & BSF_WEAK)
3145               bind = STB_WEAK;
3146             else if (flags & BSF_GLOBAL)
3147               bind = STB_GLOBAL;
3148
3149             sym.st_info = ELF_ST_INFO (bind, type);
3150           }
3151
3152         sym.st_other = 0;
3153         bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
3154         outbound_syms += bed->s->sizeof_sym;
3155       }
3156
3157     *sttp = stt;
3158     symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
3159     symstrtab_hdr->sh_type = SHT_STRTAB;
3160
3161     symstrtab_hdr->sh_flags = 0;
3162     symstrtab_hdr->sh_addr = 0;
3163     symstrtab_hdr->sh_entsize = 0;
3164     symstrtab_hdr->sh_link = 0;
3165     symstrtab_hdr->sh_info = 0;
3166     symstrtab_hdr->sh_addralign = 1;
3167   }
3168
3169   return true;
3170 }
3171
3172 /* Return the number of bytes required to hold the symtab vector.
3173
3174    Note that we base it on the count plus 1, since we will null terminate
3175    the vector allocated based on this size.  However, the ELF symbol table
3176    always has a dummy entry as symbol #0, so it ends up even.  */
3177
3178 long
3179 _bfd_elf_get_symtab_upper_bound (abfd)
3180      bfd *abfd;
3181 {
3182   long symcount;
3183   long symtab_size;
3184   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
3185
3186   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3187   symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3188
3189   return symtab_size;
3190 }
3191
3192 long
3193 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
3194      bfd *abfd;
3195 {
3196   long symcount;
3197   long symtab_size;
3198   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3199
3200   if (elf_dynsymtab (abfd) == 0)
3201     {
3202       bfd_set_error (bfd_error_invalid_operation);
3203       return -1;
3204     }
3205
3206   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3207   symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3208
3209   return symtab_size;
3210 }
3211
3212 long
3213 _bfd_elf_get_reloc_upper_bound (abfd, asect)
3214      bfd *abfd;
3215      sec_ptr asect;
3216 {
3217   return (asect->reloc_count + 1) * sizeof (arelent *);
3218 }
3219
3220 /* Canonicalize the relocs.  */
3221
3222 long
3223 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
3224      bfd *abfd;
3225      sec_ptr section;
3226      arelent **relptr;
3227      asymbol **symbols;
3228 {
3229   arelent *tblptr;
3230   unsigned int i;
3231
3232   if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd, section, symbols))
3233     return -1;
3234
3235   tblptr = section->relocation;
3236   for (i = 0; i < section->reloc_count; i++)
3237     *relptr++ = tblptr++;
3238
3239   *relptr = NULL;
3240
3241   return section->reloc_count;
3242 }
3243
3244 long
3245 _bfd_elf_get_symtab (abfd, alocation)
3246      bfd *abfd;
3247      asymbol **alocation;
3248 {
3249   long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false);
3250
3251   if (symcount >= 0)
3252     bfd_get_symcount (abfd) = symcount;
3253   return symcount;
3254 }
3255
3256 long
3257 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
3258      bfd *abfd;
3259      asymbol **alocation;
3260 {
3261   return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true);
3262 }
3263
3264 asymbol *
3265 _bfd_elf_make_empty_symbol (abfd)
3266      bfd *abfd;
3267 {
3268   elf_symbol_type *newsym;
3269
3270   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
3271   if (!newsym)
3272     return NULL;
3273   else
3274     {
3275       newsym->symbol.the_bfd = abfd;
3276       return &newsym->symbol;
3277     }
3278 }
3279
3280 void
3281 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
3282      bfd *ignore_abfd;
3283      asymbol *symbol;
3284      symbol_info *ret;
3285 {
3286   bfd_symbol_info (symbol, ret);
3287 }
3288
3289 alent *
3290 _bfd_elf_get_lineno (ignore_abfd, symbol)
3291      bfd *ignore_abfd;
3292      asymbol *symbol;
3293 {
3294   abort ();
3295   return NULL;
3296 }
3297
3298 boolean
3299 _bfd_elf_set_arch_mach (abfd, arch, machine)
3300      bfd *abfd;
3301      enum bfd_architecture arch;
3302      unsigned long machine;
3303 {
3304   /* If this isn't the right architecture for this backend, and this
3305      isn't the generic backend, fail.  */
3306   if (arch != get_elf_backend_data (abfd)->arch
3307       && arch != bfd_arch_unknown
3308       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
3309     return false;
3310
3311   return bfd_default_set_arch_mach (abfd, arch, machine);
3312 }
3313
3314 /* Find the nearest line to a particular section and offset, for error
3315    reporting.  */
3316
3317 boolean
3318 _bfd_elf_find_nearest_line (abfd,
3319                             section,
3320                             symbols,
3321                             offset,
3322                             filename_ptr,
3323                             functionname_ptr,
3324                             line_ptr)
3325      bfd *abfd;
3326      asection *section;
3327      asymbol **symbols;
3328      bfd_vma offset;
3329      CONST char **filename_ptr;
3330      CONST char **functionname_ptr;
3331      unsigned int *line_ptr;
3332 {
3333   boolean found;
3334   const char *filename;
3335   asymbol *func;
3336   bfd_vma low_func;
3337   asymbol **p;
3338
3339   if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
3340                                              &found, filename_ptr,
3341                                              functionname_ptr, line_ptr,
3342                                              &elf_tdata (abfd)->line_info))
3343     return false;
3344   if (found)
3345     return true;
3346
3347   if (symbols == NULL)
3348     return false;
3349
3350   filename = NULL;
3351   func = NULL;
3352   low_func = 0;
3353
3354   for (p = symbols; *p != NULL; p++)
3355     {
3356       elf_symbol_type *q;
3357
3358       q = (elf_symbol_type *) *p;
3359
3360       if (bfd_get_section (&q->symbol) != section)
3361         continue;
3362
3363       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
3364         {
3365         default:
3366           break;
3367         case STT_FILE:
3368           filename = bfd_asymbol_name (&q->symbol);
3369           break;
3370         case STT_FUNC:
3371           if (q->symbol.section == section
3372               && q->symbol.value >= low_func
3373               && q->symbol.value <= offset)
3374             {
3375               func = (asymbol *) q;
3376               low_func = q->symbol.value;
3377             }
3378           break;
3379         }
3380     }
3381
3382   if (func == NULL)
3383     return false;
3384
3385   *filename_ptr = filename;
3386   *functionname_ptr = bfd_asymbol_name (func);
3387   *line_ptr = 0;
3388   return true;
3389 }
3390
3391 int
3392 _bfd_elf_sizeof_headers (abfd, reloc)
3393      bfd *abfd;
3394      boolean reloc;
3395 {
3396   int ret;
3397
3398   ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
3399   if (! reloc)
3400     ret += get_program_header_size (abfd);
3401   return ret;
3402 }
3403
3404 boolean
3405 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
3406      bfd *abfd;
3407      sec_ptr section;
3408      PTR location;
3409      file_ptr offset;
3410      bfd_size_type count;
3411 {
3412   Elf_Internal_Shdr *hdr;
3413
3414   if (! abfd->output_has_begun
3415       && ! _bfd_elf_compute_section_file_positions (abfd,
3416                                                     (struct bfd_link_info *) NULL))
3417     return false;
3418
3419   hdr = &elf_section_data (section)->this_hdr;
3420
3421   if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
3422     return false;
3423   if (bfd_write (location, 1, count, abfd) != count)
3424     return false;
3425
3426   return true;
3427 }
3428
3429 void
3430 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
3431      bfd *abfd;
3432      arelent *cache_ptr;
3433      Elf_Internal_Rela *dst;
3434 {
3435   abort ();
3436 }
3437
3438 #if 0
3439 void
3440 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
3441      bfd *abfd;
3442      arelent *cache_ptr;
3443      Elf_Internal_Rel *dst;
3444 {
3445   abort ();
3446 }
3447 #endif
3448
3449 /* Try to convert a non-ELF reloc into an ELF one.  */
3450
3451 boolean
3452 _bfd_elf_validate_reloc (abfd, areloc)
3453      bfd *abfd;
3454      arelent *areloc;
3455 {
3456   /* Check whether we really have an ELF howto. */
3457
3458   if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) 
3459     {
3460       bfd_reloc_code_real_type code;
3461       reloc_howto_type *howto;
3462       
3463       /* Alien reloc: Try to determine its type to replace it with an
3464          equivalent ELF reloc. */
3465
3466       if (areloc->howto->pc_relative)
3467         {
3468           switch (areloc->howto->bitsize)
3469             {
3470             case 8:
3471               code = BFD_RELOC_8_PCREL; 
3472               break;
3473             case 12:
3474               code = BFD_RELOC_12_PCREL; 
3475               break;
3476             case 16:
3477               code = BFD_RELOC_16_PCREL; 
3478               break;
3479             case 24:
3480               code = BFD_RELOC_24_PCREL; 
3481               break;
3482             case 32:
3483               code = BFD_RELOC_32_PCREL; 
3484               break;
3485             case 64:
3486               code = BFD_RELOC_64_PCREL; 
3487               break;
3488             default:
3489               goto fail;
3490             }
3491
3492           howto = bfd_reloc_type_lookup (abfd, code);
3493
3494           if (areloc->howto->pcrel_offset != howto->pcrel_offset)
3495             {
3496               if (howto->pcrel_offset)
3497                 areloc->addend += areloc->address;
3498               else
3499                 areloc->addend -= areloc->address; /* addend is unsigned!! */
3500             }
3501         }
3502       else
3503         {
3504           switch (areloc->howto->bitsize)
3505             {
3506             case 8:
3507               code = BFD_RELOC_8; 
3508               break;
3509             case 14:
3510               code = BFD_RELOC_14; 
3511               break;
3512             case 16:
3513               code = BFD_RELOC_16; 
3514               break;
3515             case 26:
3516               code = BFD_RELOC_26; 
3517               break;
3518             case 32:
3519               code = BFD_RELOC_32; 
3520               break;
3521             case 64:
3522               code = BFD_RELOC_64; 
3523               break;
3524             default:
3525               goto fail;
3526             }
3527
3528           howto = bfd_reloc_type_lookup (abfd, code);
3529         }
3530
3531       if (howto)
3532         areloc->howto = howto;
3533       else
3534         goto fail;
3535     }
3536
3537   return true;
3538
3539  fail:
3540   (*_bfd_error_handler)
3541     ("%s: unsupported relocation type %s",
3542      bfd_get_filename (abfd), areloc->howto->name);
3543   bfd_set_error (bfd_error_bad_value);
3544   return false;
3545 }
This page took 0.220026 seconds and 4 git commands to generate.