]> Git Repo - binutils.git/blob - bfd/elf.c
* xcofflink.c: More improvements, mostly to fix handling of
[binutils.git] / bfd / elf.c
1 /* ELF executable support for BFD.
2    Copyright 1993 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 "libelf.h"
40
41 static file_ptr map_program_segments PARAMS ((bfd *, file_ptr,
42                                               Elf_Internal_Shdr *,
43                                               Elf_Internal_Shdr **,
44                                               bfd_size_type));
45 static boolean assign_file_positions_except_relocs PARAMS ((bfd *, boolean));
46 static boolean prep_headers PARAMS ((bfd *));
47 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **));
48
49 /* Standard ELF hash function.  Do not change this function; you will
50    cause invalid hash tables to be generated.  (Well, you would if this
51    were being used yet.)  */
52 unsigned long
53 bfd_elf_hash (name)
54      CONST unsigned char *name;
55 {
56   unsigned long h = 0;
57   unsigned long g;
58   int ch;
59
60   while ((ch = *name++) != '\0')
61     {
62       h = (h << 4) + ch;
63       if ((g = (h & 0xf0000000)) != 0)
64         {
65           h ^= g >> 24;
66           h &= ~g;
67         }
68     }
69   return h;
70 }
71
72 /* Read a specified number of bytes at a specified offset in an ELF
73    file, into a newly allocated buffer, and return a pointer to the
74    buffer. */
75
76 static char *
77 elf_read (abfd, offset, size)
78      bfd * abfd;
79      long offset;
80      unsigned int size;
81 {
82   char *buf;
83
84   if ((buf = bfd_alloc (abfd, size)) == NULL)
85     {
86       bfd_set_error (bfd_error_no_memory);
87       return NULL;
88     }
89   if (bfd_seek (abfd, offset, SEEK_SET) == -1)
90     return NULL;
91   if (bfd_read ((PTR) buf, size, 1, abfd) != size)
92     {
93       if (bfd_get_error () != bfd_error_system_call)
94         bfd_set_error (bfd_error_file_truncated);
95       return NULL;
96     }
97   return buf;
98 }
99
100 boolean
101 elf_mkobject (abfd)
102      bfd * abfd;
103 {
104   /* this just does initialization */
105   /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
106   elf_tdata (abfd) = (struct elf_obj_tdata *)
107     bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
108   if (elf_tdata (abfd) == 0)
109     {
110       bfd_set_error (bfd_error_no_memory);
111       return false;
112     }
113   /* since everything is done at close time, do we need any
114      initialization? */
115
116   return true;
117 }
118
119 char *
120 bfd_elf_get_str_section (abfd, shindex)
121      bfd * abfd;
122      unsigned int shindex;
123 {
124   Elf_Internal_Shdr **i_shdrp;
125   char *shstrtab = NULL;
126   unsigned int offset;
127   unsigned int shstrtabsize;
128
129   i_shdrp = elf_elfsections (abfd);
130   if (i_shdrp == 0 || i_shdrp[shindex] == 0)
131     return 0;
132
133   shstrtab = (char *) i_shdrp[shindex]->contents;
134   if (shstrtab == NULL)
135     {
136       /* No cached one, attempt to read, and cache what we read. */
137       offset = i_shdrp[shindex]->sh_offset;
138       shstrtabsize = i_shdrp[shindex]->sh_size;
139       shstrtab = elf_read (abfd, offset, shstrtabsize);
140       i_shdrp[shindex]->contents = (PTR) shstrtab;
141     }
142   return shstrtab;
143 }
144
145 char *
146 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
147      bfd * abfd;
148      unsigned int shindex;
149      unsigned int strindex;
150 {
151   Elf_Internal_Shdr *hdr;
152
153   if (strindex == 0)
154     return "";
155
156   hdr = elf_elfsections (abfd)[shindex];
157
158   if (hdr->contents == NULL
159       && bfd_elf_get_str_section (abfd, shindex) == NULL)
160     return NULL;
161
162   return ((char *) hdr->contents) + strindex;
163 }
164
165 /* Make a BFD section from an ELF section.  We store a pointer to the
166    BFD section in the bfd_section field of the header.  */
167
168 boolean
169 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
170      bfd *abfd;
171      Elf_Internal_Shdr *hdr;
172      const char *name;
173 {
174   asection *newsect;
175   flagword flags;
176
177   if (hdr->bfd_section != NULL)
178     {
179       BFD_ASSERT (strcmp (name,
180                           bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
181       return true;
182     }
183
184   newsect = bfd_make_section_anyway (abfd, name);
185   if (newsect == NULL)
186     return false;
187
188   newsect->filepos = hdr->sh_offset;
189
190   if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
191       || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
192       || ! bfd_set_section_alignment (abfd, newsect,
193                                       bfd_log2 (hdr->sh_addralign)))
194     return false;
195
196   flags = SEC_NO_FLAGS;
197   if (hdr->sh_type != SHT_NOBITS)
198     flags |= SEC_HAS_CONTENTS;
199   if ((hdr->sh_flags & SHF_ALLOC) != 0)
200     {
201       flags |= SEC_ALLOC;
202       if (hdr->sh_type != SHT_NOBITS)
203         flags |= SEC_LOAD;
204     }
205   if ((hdr->sh_flags & SHF_WRITE) == 0)
206     flags |= SEC_READONLY;
207   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
208     flags |= SEC_CODE;
209   else if ((flags & SEC_LOAD) != 0)
210     flags |= SEC_DATA;
211
212   /* The debugging sections appear to be recognized only by name, not
213      any sort of flag.  */
214   if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
215       || strncmp (name, ".line", sizeof ".line" - 1) == 0
216       || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
217     flags |= SEC_DEBUGGING;
218
219   if (! bfd_set_section_flags (abfd, newsect, flags))
220     return false;
221
222   hdr->bfd_section = newsect;
223   elf_section_data (newsect)->this_hdr = *hdr;
224
225   return true;
226 }
227
228 /*
229 INTERNAL_FUNCTION
230         bfd_elf_find_section
231
232 SYNOPSIS
233         struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
234
235 DESCRIPTION
236         Helper functions for GDB to locate the string tables.
237         Since BFD hides string tables from callers, GDB needs to use an
238         internal hook to find them.  Sun's .stabstr, in particular,
239         isn't even pointed to by the .stab section, so ordinary
240         mechanisms wouldn't work to find it, even if we had some.
241 */
242
243 struct elf_internal_shdr *
244 bfd_elf_find_section (abfd, name)
245      bfd * abfd;
246      char *name;
247 {
248   Elf_Internal_Shdr **i_shdrp;
249   char *shstrtab;
250   unsigned int max;
251   unsigned int i;
252
253   i_shdrp = elf_elfsections (abfd);
254   if (i_shdrp != NULL)
255     {
256       shstrtab = bfd_elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
257       if (shstrtab != NULL)
258         {
259           max = elf_elfheader (abfd)->e_shnum;
260           for (i = 1; i < max; i++)
261             if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
262               return i_shdrp[i];
263         }
264     }
265   return 0;
266 }
267
268 const char *const bfd_elf_section_type_names[] = {
269   "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
270   "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
271   "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
272 };
273
274 /* ELF relocs are against symbols.  If we are producing relocateable
275    output, and the reloc is against an external symbol, and nothing
276    has given us any additional addend, the resulting reloc will also
277    be against the same symbol.  In such a case, we don't want to
278    change anything about the way the reloc is handled, since it will
279    all be done at final link time.  Rather than put special case code
280    into bfd_perform_relocation, all the reloc types use this howto
281    function.  It just short circuits the reloc if producing
282    relocateable output against an external symbol.  */
283
284 /*ARGSUSED*/
285 bfd_reloc_status_type
286 bfd_elf_generic_reloc (abfd,
287                        reloc_entry,
288                        symbol,
289                        data,
290                        input_section,
291                        output_bfd,
292                        error_message)
293      bfd *abfd;
294      arelent *reloc_entry;
295      asymbol *symbol;
296      PTR data;
297      asection *input_section;
298      bfd *output_bfd;
299      char **error_message;
300 {
301   if (output_bfd != (bfd *) NULL
302       && (symbol->flags & BSF_SECTION_SYM) == 0
303       && (! reloc_entry->howto->partial_inplace
304           || reloc_entry->addend == 0))
305     {
306       reloc_entry->address += input_section->output_offset;
307       return bfd_reloc_ok;
308     }
309
310   return bfd_reloc_continue;
311 }
312 \f
313 /* Display ELF-specific fields of a symbol.  */
314 void
315 bfd_elf_print_symbol (ignore_abfd, filep, symbol, how)
316      bfd *ignore_abfd;
317      PTR filep;
318      asymbol *symbol;
319      bfd_print_symbol_type how;
320 {
321   FILE *file = (FILE *) filep;
322   switch (how)
323     {
324     case bfd_print_symbol_name:
325       fprintf (file, "%s", symbol->name);
326       break;
327     case bfd_print_symbol_more:
328       fprintf (file, "elf ");
329       fprintf_vma (file, symbol->value);
330       fprintf (file, " %lx", (long) symbol->flags);
331       break;
332     case bfd_print_symbol_all:
333       {
334         CONST char *section_name;
335         section_name = symbol->section ? symbol->section->name : "(*none*)";
336         bfd_print_symbol_vandf ((PTR) file, symbol);
337         fprintf (file, " %s\t", section_name);
338         /* Print the "other" value for a symbol.  For common symbols,
339            we've already printed the size; now print the alignment.
340            For other symbols, we have no specified alignment, and
341            we've printed the address; now print the size.  */
342         fprintf_vma (file,
343                      (bfd_is_com_section (symbol->section)
344                       ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
345                       : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
346         fprintf (file, " %s", symbol->name);
347       }
348       break;
349     }
350 }
351 \f
352 /* Create an entry in an ELF linker hash table.  */
353
354 struct bfd_hash_entry *
355 _bfd_elf_link_hash_newfunc (entry, table, string)
356      struct bfd_hash_entry *entry;
357      struct bfd_hash_table *table;
358      const char *string;
359 {
360   struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
361
362   /* Allocate the structure if it has not already been allocated by a
363      subclass.  */
364   if (ret == (struct elf_link_hash_entry *) NULL)
365     ret = ((struct elf_link_hash_entry *)
366            bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
367   if (ret == (struct elf_link_hash_entry *) NULL)
368     {
369       bfd_set_error (bfd_error_no_memory);
370       return (struct bfd_hash_entry *) ret;
371     }
372
373   /* Call the allocation method of the superclass.  */
374   ret = ((struct elf_link_hash_entry *)
375          _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
376                                  table, string));
377   if (ret != (struct elf_link_hash_entry *) NULL)
378     {
379       /* Set local fields.  */
380       ret->indx = -1;
381       ret->size = 0;
382       ret->dynindx = -1;
383       ret->dynstr_index = 0;
384       ret->weakdef = NULL;
385       ret->got_offset = (bfd_vma) -1;
386       ret->plt_offset = (bfd_vma) -1;
387       ret->type = STT_NOTYPE;
388       ret->elf_link_hash_flags = 0;
389     }
390
391   return (struct bfd_hash_entry *) ret;
392 }
393
394 /* Initialize an ELF linker hash table.  */
395
396 boolean
397 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
398      struct elf_link_hash_table *table;
399      bfd *abfd;
400      struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
401                                                 struct bfd_hash_table *,
402                                                 const char *));
403 {
404   table->dynamic_sections_created = false;
405   table->dynobj = NULL;
406   /* The first dynamic symbol is a dummy.  */
407   table->dynsymcount = 1;
408   table->dynstr = NULL;
409   table->bucketcount = 0;
410   table->needed = NULL;
411   return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
412 }
413
414 /* Create an ELF linker hash table.  */
415
416 struct bfd_link_hash_table *
417 _bfd_elf_link_hash_table_create (abfd)
418      bfd *abfd;
419 {
420   struct elf_link_hash_table *ret;
421
422   ret = ((struct elf_link_hash_table *)
423          bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
424   if (ret == (struct elf_link_hash_table *) NULL)
425     {
426       bfd_set_error (bfd_error_no_memory);
427       return NULL;
428     }
429
430   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
431     {
432       bfd_release (abfd, ret);
433       return NULL;
434     }
435
436   return &ret->root;
437 }
438
439 /* This is a hook for the ELF emulation code in the generic linker to
440    tell the backend linker what file name to use for the DT_NEEDED
441    entry for a dynamic object.  The generic linker passes name as an
442    empty string to indicate that no DT_NEEDED entry should be made.  */
443
444 void
445 bfd_elf_set_dt_needed_name (abfd, name)
446      bfd *abfd;
447      const char *name;
448 {
449   elf_dt_needed_name (abfd) = name;
450 }
451
452 /* Get the list of DT_NEEDED entries for a link.  */
453
454 struct bfd_elf_link_needed_list *
455 bfd_elf_get_needed_list (abfd, info)
456      bfd *abfd;
457      struct bfd_link_info *info;
458 {
459   return elf_hash_table (info)->needed;
460 }
461 \f
462 /* Allocate an ELF string table--force the first byte to be zero.  */
463
464 struct bfd_strtab_hash *
465 _bfd_elf_stringtab_init ()
466 {
467   struct bfd_strtab_hash *ret;
468
469   ret = _bfd_stringtab_init ();
470   if (ret != NULL)
471     {
472       bfd_size_type loc;
473
474       loc = _bfd_stringtab_add (ret, "", true, false);
475       BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
476       if (loc == (bfd_size_type) -1)
477         {
478           _bfd_stringtab_free (ret);
479           ret = NULL;
480         }
481     }
482   return ret;
483 }
484 \f
485 /* ELF .o/exec file reading */
486
487 /* Create a new bfd section from an ELF section header. */
488
489 boolean
490 bfd_section_from_shdr (abfd, shindex)
491      bfd *abfd;
492      unsigned int shindex;
493 {
494   Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
495   Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
496   struct elf_backend_data *bed = get_elf_backend_data (abfd);
497   char *name;
498
499   name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
500
501   switch (hdr->sh_type)
502     {
503     case SHT_NULL:
504       /* Inactive section. Throw it away.  */
505       return true;
506
507     case SHT_PROGBITS:  /* Normal section with contents.  */
508     case SHT_DYNAMIC:   /* Dynamic linking information.  */
509     case SHT_NOBITS:    /* .bss section.  */
510     case SHT_HASH:      /* .hash section.  */
511       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
512
513     case SHT_SYMTAB:            /* A symbol table */
514       if (elf_onesymtab (abfd) == shindex)
515         return true;
516
517       BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
518       BFD_ASSERT (elf_onesymtab (abfd) == 0);
519       elf_onesymtab (abfd) = shindex;
520       elf_tdata (abfd)->symtab_hdr = *hdr;
521       elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_hdr;
522       abfd->flags |= HAS_SYMS;
523
524       /* Sometimes a shared object will map in the symbol table.  If
525          SHF_ALLOC is set, and this is a shared object, then we also
526          treat this section as a BFD section.  We can not base the
527          decision purely on SHF_ALLOC, because that flag is sometimes
528          set in a relocateable object file, which would confuse the
529          linker.  */
530       if ((hdr->sh_flags & SHF_ALLOC) != 0
531           && (abfd->flags & DYNAMIC) != 0
532           && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
533         return false;
534
535       return true;
536
537     case SHT_DYNSYM:            /* A dynamic symbol table */
538       if (elf_dynsymtab (abfd) == shindex)
539         return true;
540
541       BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
542       BFD_ASSERT (elf_dynsymtab (abfd) == 0);
543       elf_dynsymtab (abfd) = shindex;
544       elf_tdata (abfd)->dynsymtab_hdr = *hdr;
545       elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->dynsymtab_hdr;
546       abfd->flags |= HAS_SYMS;
547
548       /* Besides being a symbol table, we also treat this as a regular
549          section, so that objcopy can handle it.  */
550       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
551
552     case SHT_STRTAB:            /* A string table */
553       if (hdr->bfd_section != NULL)
554         return true;
555       if (ehdr->e_shstrndx == shindex)
556         {
557           elf_tdata (abfd)->shstrtab_hdr = *hdr;
558           elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
559           return true;
560         }
561       {
562         unsigned int i;
563
564         for (i = 1; i < ehdr->e_shnum; i++)
565           {
566             Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
567             if (hdr2->sh_link == shindex)
568               {
569                 if (! bfd_section_from_shdr (abfd, i))
570                   return false;
571                 if (elf_onesymtab (abfd) == i)
572                   {
573                     elf_tdata (abfd)->strtab_hdr = *hdr;
574                     elf_elfsections (abfd)[shindex] =
575                       &elf_tdata (abfd)->strtab_hdr;
576                     return true;
577                   }
578                 if (elf_dynsymtab (abfd) == i)
579                   {
580                     elf_tdata (abfd)->dynstrtab_hdr = *hdr;
581                     elf_elfsections (abfd)[shindex] =
582                       &elf_tdata (abfd)->dynstrtab_hdr;
583                     /* We also treat this as a regular section, so
584                        that objcopy can handle it.  */
585                     break;
586                   }
587 #if 0 /* Not handling other string tables specially right now.  */
588                 hdr2 = elf_elfsections (abfd)[i];       /* in case it moved */
589                 /* We have a strtab for some random other section.  */
590                 newsect = (asection *) hdr2->bfd_section;
591                 if (!newsect)
592                   break;
593                 hdr->bfd_section = newsect;
594                 hdr2 = &elf_section_data (newsect)->str_hdr;
595                 *hdr2 = *hdr;
596                 elf_elfsections (abfd)[shindex] = hdr2;
597 #endif
598               }
599           }
600       }
601
602       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
603
604     case SHT_REL:
605     case SHT_RELA:
606       /* *These* do a lot of work -- but build no sections!  */
607       {
608         asection *target_sect;
609         Elf_Internal_Shdr *hdr2;
610         int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
611
612         /* For some incomprehensible reason Oracle distributes
613            libraries for Solaris in which some of the objects have
614            bogus sh_link fields.  It would be nice if we could just
615            reject them, but, unfortunately, some people need to use
616            them.  We scan through the section headers; if we find only
617            one suitable symbol table, we clobber the sh_link to point
618            to it.  I hope this doesn't break anything.  */
619         if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
620             && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
621           {
622             int scan;
623             int found;
624
625             found = 0;
626             for (scan = 1; scan < ehdr->e_shnum; scan++)
627               {
628                 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
629                     || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
630                   {
631                     if (found != 0)
632                       {
633                         found = 0;
634                         break;
635                       }
636                     found = scan;
637                   }
638               }
639             if (found != 0)
640               hdr->sh_link = found;
641           }
642
643         /* Get the symbol table.  */
644         if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
645             && ! bfd_section_from_shdr (abfd, hdr->sh_link))
646           return false;
647
648         /* If this reloc section does not use the main symbol table we
649            don't treat it as a reloc section.  BFD can't adequately
650            represent such a section, so at least for now, we don't
651            try.  We just present it as a normal section.  */
652         if (hdr->sh_link != elf_onesymtab (abfd))
653           return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
654
655         /* Don't allow REL relocations on a machine that uses RELA and
656            vice versa.  */
657         /* @@ Actually, the generic ABI does suggest that both might be
658            used in one file.  But the four ABI Processor Supplements I
659            have access to right now all specify that only one is used on
660            each of those architectures.  It's conceivable that, e.g., a
661            bunch of absolute 32-bit relocs might be more compact in REL
662            form even on a RELA machine...  */
663         BFD_ASSERT (use_rela_p
664                     ? (hdr->sh_type == SHT_RELA
665                        && hdr->sh_entsize == bed->s->sizeof_rela)
666                     : (hdr->sh_type == SHT_REL
667                        && hdr->sh_entsize == bed->s->sizeof_rel));
668
669         if (! bfd_section_from_shdr (abfd, hdr->sh_info))
670           return false;
671         target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
672         if (target_sect == NULL)
673           return false;
674
675         hdr2 = &elf_section_data (target_sect)->rel_hdr;
676         *hdr2 = *hdr;
677         elf_elfsections (abfd)[shindex] = hdr2;
678         target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
679         target_sect->flags |= SEC_RELOC;
680         target_sect->relocation = NULL;
681         target_sect->rel_filepos = hdr->sh_offset;
682         abfd->flags |= HAS_RELOC;
683         return true;
684       }
685       break;
686
687     case SHT_NOTE:
688       break;
689
690     case SHT_SHLIB:
691       return true;
692
693     default:
694       /* Check for any processor-specific section types.  */
695       {
696         if (bed->elf_backend_section_from_shdr)
697           (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
698       }
699       break;
700     }
701
702   return true;
703 }
704
705 /* Given an ELF section number, retrieve the corresponding BFD
706    section.  */
707
708 asection *
709 bfd_section_from_elf_index (abfd, index)
710      bfd *abfd;
711      unsigned int index;
712 {
713   BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
714   if (index >= elf_elfheader (abfd)->e_shnum)
715     return NULL;
716   return elf_elfsections (abfd)[index]->bfd_section;
717 }
718
719 boolean
720 _bfd_elf_new_section_hook (abfd, sec)
721      bfd *abfd;
722      asection *sec;
723 {
724   struct bfd_elf_section_data *sdata;
725
726   sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
727   if (!sdata)
728     {
729       bfd_set_error (bfd_error_no_memory);
730       return false;
731     }
732   sec->used_by_bfd = (PTR) sdata;
733   memset (sdata, 0, sizeof (*sdata));
734   return true;
735 }
736
737 /* Create a new bfd section from an ELF program header.
738
739    Since program segments have no names, we generate a synthetic name
740    of the form segment<NUM>, where NUM is generally the index in the
741    program header table.  For segments that are split (see below) we
742    generate the names segment<NUM>a and segment<NUM>b.
743
744    Note that some program segments may have a file size that is different than
745    (less than) the memory size.  All this means is that at execution the
746    system must allocate the amount of memory specified by the memory size,
747    but only initialize it with the first "file size" bytes read from the
748    file.  This would occur for example, with program segments consisting
749    of combined data+bss.
750
751    To handle the above situation, this routine generates TWO bfd sections
752    for the single program segment.  The first has the length specified by
753    the file size of the segment, and the second has the length specified
754    by the difference between the two sizes.  In effect, the segment is split
755    into it's initialized and uninitialized parts.
756
757  */
758
759 boolean
760 bfd_section_from_phdr (abfd, hdr, index)
761      bfd *abfd;
762      Elf_Internal_Phdr *hdr;
763      int index;
764 {
765   asection *newsect;
766   char *name;
767   char namebuf[64];
768   int split;
769
770   split = ((hdr->p_memsz > 0) &&
771            (hdr->p_filesz > 0) &&
772            (hdr->p_memsz > hdr->p_filesz));
773   sprintf (namebuf, split ? "segment%da" : "segment%d", index);
774   name = bfd_alloc (abfd, strlen (namebuf) + 1);
775   if (!name)
776     {
777       bfd_set_error (bfd_error_no_memory);
778       return false;
779     }
780   strcpy (name, namebuf);
781   newsect = bfd_make_section (abfd, name);
782   if (newsect == NULL)
783     return false;
784   newsect->vma = hdr->p_vaddr;
785   newsect->lma = hdr->p_paddr;
786   newsect->_raw_size = hdr->p_filesz;
787   newsect->filepos = hdr->p_offset;
788   newsect->flags |= SEC_HAS_CONTENTS;
789   if (hdr->p_type == PT_LOAD)
790     {
791       newsect->flags |= SEC_ALLOC;
792       newsect->flags |= SEC_LOAD;
793       if (hdr->p_flags & PF_X)
794         {
795           /* FIXME: all we known is that it has execute PERMISSION,
796              may be data. */
797           newsect->flags |= SEC_CODE;
798         }
799     }
800   if (!(hdr->p_flags & PF_W))
801     {
802       newsect->flags |= SEC_READONLY;
803     }
804
805   if (split)
806     {
807       sprintf (namebuf, "segment%db", index);
808       name = bfd_alloc (abfd, strlen (namebuf) + 1);
809       if (!name)
810         {
811           bfd_set_error (bfd_error_no_memory);
812           return false;
813         }
814       strcpy (name, namebuf);
815       newsect = bfd_make_section (abfd, name);
816       if (newsect == NULL)
817         return false;
818       newsect->vma = hdr->p_vaddr + hdr->p_filesz;
819       newsect->lma = hdr->p_paddr + hdr->p_filesz;
820       newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
821       if (hdr->p_type == PT_LOAD)
822         {
823           newsect->flags |= SEC_ALLOC;
824           if (hdr->p_flags & PF_X)
825             newsect->flags |= SEC_CODE;
826         }
827       if (!(hdr->p_flags & PF_W))
828         newsect->flags |= SEC_READONLY;
829     }
830
831   return true;
832 }
833
834 /* Set up an ELF internal section header for a section.  */
835
836 /*ARGSUSED*/
837 static void
838 elf_fake_sections (abfd, asect, failedptrarg)
839      bfd *abfd;
840      asection *asect;
841      PTR failedptrarg;
842 {
843   struct elf_backend_data *bed = get_elf_backend_data (abfd);
844   boolean *failedptr = (boolean *) failedptrarg;
845   Elf_Internal_Shdr *this_hdr;
846
847   if (*failedptr)
848     {
849       /* We already failed; just get out of the bfd_map_over_sections
850          loop.  */
851       return;
852     }
853
854   this_hdr = &elf_section_data (asect)->this_hdr;
855
856   this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
857                                                           asect->name,
858                                                           true, false);
859   if (this_hdr->sh_name == (unsigned long) -1)
860     {
861       *failedptr = true;
862       return;
863     }
864
865   this_hdr->sh_flags = 0;
866
867   /* FIXME: This should really use vma, rather than lma.  However,
868      that would mean that the lma information was lost, which would
869      mean that the AT keyword in linker scripts would not work.
870      Fortunately, native scripts do not use the AT keyword, so we can
871      get away with using lma here.  The right way to handle this is to
872      1) read the program headers as well as the section headers, and
873      set the lma fields of the BFD sections based on the p_paddr
874      fields of the program headers, and 2) set the p_paddr fields of
875      the program headers based on the section lma fields when writing
876      them out.  */
877   if ((asect->flags & SEC_ALLOC) != 0)
878     this_hdr->sh_addr = asect->lma;
879   else
880     this_hdr->sh_addr = 0;
881
882   this_hdr->sh_offset = 0;
883   this_hdr->sh_size = asect->_raw_size;
884   this_hdr->sh_link = 0;
885   this_hdr->sh_info = 0;
886   this_hdr->sh_addralign = 1 << asect->alignment_power;
887   this_hdr->sh_entsize = 0;
888
889   this_hdr->bfd_section = asect;
890   this_hdr->contents = NULL;
891
892   /* FIXME: This should not be based on section names.  */
893   if (strcmp (asect->name, ".dynstr") == 0)
894     this_hdr->sh_type = SHT_STRTAB;
895   else if (strcmp (asect->name, ".hash") == 0)
896     {
897       this_hdr->sh_type = SHT_HASH;
898       this_hdr->sh_entsize = bed->s->arch_size / 8;
899     }
900   else if (strcmp (asect->name, ".dynsym") == 0)
901     {
902       this_hdr->sh_type = SHT_DYNSYM;
903       this_hdr->sh_entsize = bed->s->sizeof_sym;
904     }
905   else if (strcmp (asect->name, ".dynamic") == 0)
906     {
907       this_hdr->sh_type = SHT_DYNAMIC;
908       this_hdr->sh_entsize = bed->s->sizeof_dyn;
909     }
910   else if (strncmp (asect->name, ".rela", 5) == 0
911            && get_elf_backend_data (abfd)->use_rela_p)
912     {
913       this_hdr->sh_type = SHT_RELA;
914       this_hdr->sh_entsize = bed->s->sizeof_rela;
915     }
916   else if (strncmp (asect->name, ".rel", 4) == 0
917            && ! get_elf_backend_data (abfd)->use_rela_p)
918     {
919       this_hdr->sh_type = SHT_REL;
920       this_hdr->sh_entsize = bed->s->sizeof_rel;
921     }
922   else if (strcmp (asect->name, ".note") == 0)
923     this_hdr->sh_type = SHT_NOTE;
924   else if (strncmp (asect->name, ".stab", 5) == 0
925            && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
926     this_hdr->sh_type = SHT_STRTAB;
927   else if ((asect->flags & SEC_ALLOC) != 0
928            && (asect->flags & SEC_LOAD) != 0)
929     this_hdr->sh_type = SHT_PROGBITS;
930   else if ((asect->flags & SEC_ALLOC) != 0
931            && ((asect->flags & SEC_LOAD) == 0))
932     {
933       BFD_ASSERT (strcmp (asect->name, ".bss") == 0
934                   || strcmp (asect->name, ".sbss") == 0
935                   || strcmp (asect->name, ".scommon") == 0
936                   || strcmp (asect->name, "COMMON") == 0);
937       this_hdr->sh_type = SHT_NOBITS;
938     }
939   else
940     {
941       /* Who knows?  */
942       this_hdr->sh_type = SHT_PROGBITS;
943     }
944
945   if ((asect->flags & SEC_ALLOC) != 0)
946     this_hdr->sh_flags |= SHF_ALLOC;
947   if ((asect->flags & SEC_READONLY) == 0)
948     this_hdr->sh_flags |= SHF_WRITE;
949   if ((asect->flags & SEC_CODE) != 0)
950     this_hdr->sh_flags |= SHF_EXECINSTR;
951
952   /* Check for processor-specific section types.  */
953   {
954     struct elf_backend_data *bed = get_elf_backend_data (abfd);
955
956     if (bed->elf_backend_fake_sections)
957       (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
958   }
959
960   /* If the section has relocs, set up a section header for the
961      SHT_REL[A] section.  */
962   if ((asect->flags & SEC_RELOC) != 0)
963     {
964       Elf_Internal_Shdr *rela_hdr;
965       int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
966       char *name;
967
968       rela_hdr = &elf_section_data (asect)->rel_hdr;
969       name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
970       if (name == NULL)
971         {
972           bfd_set_error (bfd_error_no_memory);
973           *failedptr = true;
974           return;
975         }
976       sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
977       rela_hdr->sh_name =
978         (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
979                                            true, false);
980       if (rela_hdr->sh_name == (unsigned int) -1)
981         {
982           *failedptr = true;
983           return;
984         }
985       rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
986       rela_hdr->sh_entsize = (use_rela_p
987                               ? bed->s->sizeof_rela
988                               : bed->s->sizeof_rel);
989       rela_hdr->sh_addralign = bed->s->file_align;
990       rela_hdr->sh_flags = 0;
991       rela_hdr->sh_addr = 0;
992       rela_hdr->sh_size = 0;
993       rela_hdr->sh_offset = 0;
994     }
995 }
996
997 /* Assign all ELF section numbers.  The dummy first section is handled here
998    too.  The link/info pointers for the standard section types are filled
999    in here too, while we're at it.  */
1000
1001 static boolean
1002 assign_section_numbers (abfd)
1003      bfd *abfd;
1004 {
1005   struct elf_obj_tdata *t = elf_tdata (abfd);
1006   asection *sec;
1007   unsigned int section_number;
1008   Elf_Internal_Shdr **i_shdrp;
1009   struct elf_backend_data *bed = get_elf_backend_data (abfd);
1010
1011   section_number = 1;
1012
1013   for (sec = abfd->sections; sec; sec = sec->next)
1014     {
1015       struct bfd_elf_section_data *d = elf_section_data (sec);
1016
1017       d->this_idx = section_number++;
1018       if ((sec->flags & SEC_RELOC) == 0)
1019         d->rel_idx = 0;
1020       else
1021         d->rel_idx = section_number++;
1022     }
1023
1024   t->shstrtab_section = section_number++;
1025   elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1026   t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1027
1028   if (abfd->symcount > 0)
1029     {
1030       t->symtab_section = section_number++;
1031       t->strtab_section = section_number++;
1032     }
1033
1034   elf_elfheader (abfd)->e_shnum = section_number;
1035
1036   /* Set up the list of section header pointers, in agreement with the
1037      indices.  */
1038   i_shdrp = ((Elf_Internal_Shdr **)
1039              bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1040   if (i_shdrp == NULL)
1041     {
1042       bfd_set_error (bfd_error_no_memory);
1043       return false;
1044     }
1045
1046   i_shdrp[0] = ((Elf_Internal_Shdr *)
1047                 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1048   if (i_shdrp[0] == NULL)
1049     {
1050       bfd_release (abfd, i_shdrp);
1051       bfd_set_error (bfd_error_no_memory);
1052       return false;
1053     }
1054   memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1055
1056   elf_elfsections (abfd) = i_shdrp;
1057
1058   i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1059   if (abfd->symcount > 0)
1060     {
1061       i_shdrp[t->symtab_section] = &t->symtab_hdr;
1062       i_shdrp[t->strtab_section] = &t->strtab_hdr;
1063       t->symtab_hdr.sh_link = t->strtab_section;
1064     }
1065   for (sec = abfd->sections; sec; sec = sec->next)
1066     {
1067       struct bfd_elf_section_data *d = elf_section_data (sec);
1068       asection *s;
1069       const char *name;
1070
1071       i_shdrp[d->this_idx] = &d->this_hdr;
1072       if (d->rel_idx != 0)
1073         i_shdrp[d->rel_idx] = &d->rel_hdr;
1074
1075       /* Fill in the sh_link and sh_info fields while we're at it.  */
1076
1077       /* sh_link of a reloc section is the section index of the symbol
1078          table.  sh_info is the section index of the section to which
1079          the relocation entries apply.  */
1080       if (d->rel_idx != 0)
1081         {
1082           d->rel_hdr.sh_link = t->symtab_section;
1083           d->rel_hdr.sh_info = d->this_idx;
1084         }
1085
1086       switch (d->this_hdr.sh_type)
1087         {
1088         case SHT_REL:
1089         case SHT_RELA:
1090           /* A reloc section which we are treating as a normal BFD
1091              section.  sh_link is the section index of the symbol
1092              table.  sh_info is the section index of the section to
1093              which the relocation entries apply.  We assume that an
1094              allocated reloc section uses the dynamic symbol table.
1095              FIXME: How can we be sure?  */
1096           s = bfd_get_section_by_name (abfd, ".dynsym");
1097           if (s != NULL)
1098             d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1099
1100           /* We look up the section the relocs apply to by name.  */
1101           name = sec->name;
1102           if (d->this_hdr.sh_type == SHT_REL)
1103             name += 4;
1104           else
1105             name += 5;
1106           s = bfd_get_section_by_name (abfd, name);
1107           if (s != NULL)
1108             d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1109           break;
1110
1111         case SHT_STRTAB:
1112           /* We assume that a section named .stab*str is a stabs
1113              string section.  We look for a section with the same name
1114              but without the trailing ``str'', and set its sh_link
1115              field to point to this section.  */
1116           if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1117               && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1118             {
1119               size_t len;
1120               char *alc;
1121
1122               len = strlen (sec->name);
1123               alc = (char *) malloc (len - 2);
1124               if (alc == NULL)
1125                 {
1126                   bfd_set_error (bfd_error_no_memory);
1127                   return false;
1128                 }
1129               strncpy (alc, sec->name, len - 3);
1130               alc[len - 3] = '\0';
1131               s = bfd_get_section_by_name (abfd, alc);
1132               free (alc);
1133               if (s != NULL)
1134                 {
1135                   elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1136
1137                   /* This is a .stab section.  */
1138                   elf_section_data (s)->this_hdr.sh_entsize =
1139                     4 + 2 * (bed->s->arch_size / 8);
1140                 }
1141             }
1142           break;
1143
1144         case SHT_DYNAMIC:
1145         case SHT_DYNSYM:
1146           /* sh_link is the section header index of the string table
1147              used for the dynamic entries or symbol table.  */
1148           s = bfd_get_section_by_name (abfd, ".dynstr");
1149           if (s != NULL)
1150             d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1151           break;
1152
1153         case SHT_HASH:
1154           /* sh_link is the section header index of the symbol table
1155              this hash table is for.  */
1156           s = bfd_get_section_by_name (abfd, ".dynsym");
1157           if (s != NULL)
1158             d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1159           break;
1160         }
1161     }
1162
1163   return true;
1164 }
1165
1166 /* Map symbol from it's internal number to the external number, moving
1167    all local symbols to be at the head of the list.  */
1168
1169 static INLINE int
1170 sym_is_global (abfd, sym)
1171      bfd *abfd;
1172      asymbol *sym;
1173 {
1174   /* If the backend has a special mapping, use it.  */
1175   if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1176     return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1177             (abfd, sym));
1178
1179   return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1180           || bfd_is_und_section (bfd_get_section (sym))
1181           || bfd_is_com_section (bfd_get_section (sym)));
1182 }
1183
1184 static boolean
1185 elf_map_symbols (abfd)
1186      bfd *abfd;
1187 {
1188   int symcount = bfd_get_symcount (abfd);
1189   asymbol **syms = bfd_get_outsymbols (abfd);
1190   asymbol **sect_syms;
1191   int num_locals = 0;
1192   int num_globals = 0;
1193   int num_locals2 = 0;
1194   int num_globals2 = 0;
1195   int max_index = 0;
1196   int num_sections = 0;
1197   int idx;
1198   asection *asect;
1199   asymbol **new_syms;
1200
1201 #ifdef DEBUG
1202   fprintf (stderr, "elf_map_symbols\n");
1203   fflush (stderr);
1204 #endif
1205
1206   /* Add a section symbol for each BFD section.  FIXME: Is this really
1207      necessary?  */
1208   for (asect = abfd->sections; asect; asect = asect->next)
1209     {
1210       if (max_index < asect->index)
1211         max_index = asect->index;
1212     }
1213
1214   max_index++;
1215   sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1216   if (sect_syms == NULL)
1217     {
1218       bfd_set_error (bfd_error_no_memory);
1219       return false;
1220     }
1221   elf_section_syms (abfd) = sect_syms;
1222
1223   for (idx = 0; idx < symcount; idx++)
1224     {
1225       if ((syms[idx]->flags & BSF_SECTION_SYM) != 0
1226           && syms[idx]->value == 0)
1227         {
1228           asection *sec;
1229
1230           sec = syms[idx]->section;
1231           if (sec->owner != NULL)
1232             {
1233               if (sec->owner != abfd)
1234                 {
1235                   if (sec->output_offset != 0)
1236                     continue;
1237                   sec = sec->output_section;
1238                   BFD_ASSERT (sec->owner == abfd);
1239                 }
1240               sect_syms[sec->index] = syms[idx];
1241             }
1242         }
1243     }
1244
1245   for (asect = abfd->sections; asect; asect = asect->next)
1246     {
1247       asymbol *sym;
1248
1249       if (sect_syms[asect->index] != NULL)
1250         continue;
1251
1252       sym = bfd_make_empty_symbol (abfd);
1253       if (sym == NULL)
1254         return false;
1255       sym->the_bfd = abfd;
1256       sym->name = asect->name;
1257       sym->value = 0;
1258       /* Set the flags to 0 to indicate that this one was newly added.  */
1259       sym->flags = 0;
1260       sym->section = asect;
1261       sect_syms[asect->index] = sym;
1262       num_sections++;
1263 #ifdef DEBUG
1264       fprintf (stderr,
1265                "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1266                asect->name, (long) asect->vma, asect->index, (long) asect);
1267 #endif
1268     }
1269
1270   /* Classify all of the symbols.  */
1271   for (idx = 0; idx < symcount; idx++)
1272     {
1273       if (!sym_is_global (abfd, syms[idx]))
1274         num_locals++;
1275       else
1276         num_globals++;
1277     }
1278   for (asect = abfd->sections; asect; asect = asect->next)
1279     {
1280       if (sect_syms[asect->index] != NULL
1281           && sect_syms[asect->index]->flags == 0)
1282         {
1283           sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1284           if (!sym_is_global (abfd, sect_syms[asect->index]))
1285             num_locals++;
1286           else
1287             num_globals++;
1288           sect_syms[asect->index]->flags = 0;
1289         }
1290     }
1291
1292   /* Now sort the symbols so the local symbols are first.  */
1293   new_syms = ((asymbol **)
1294               bfd_alloc (abfd,
1295                          (num_locals + num_globals) * sizeof (asymbol *)));
1296   if (new_syms == NULL)
1297     {
1298       bfd_set_error (bfd_error_no_memory);
1299       return false;
1300     }
1301
1302   for (idx = 0; idx < symcount; idx++)
1303     {
1304       asymbol *sym = syms[idx];
1305       int i;
1306
1307       if (!sym_is_global (abfd, sym))
1308         i = num_locals2++;
1309       else
1310         i = num_locals + num_globals2++;
1311       new_syms[i] = sym;
1312       sym->udata.i = i + 1;
1313     }
1314   for (asect = abfd->sections; asect; asect = asect->next)
1315     {
1316       if (sect_syms[asect->index] != NULL
1317           && sect_syms[asect->index]->flags == 0)
1318         {
1319           asymbol *sym = sect_syms[asect->index];
1320           int i;
1321
1322           sym->flags = BSF_SECTION_SYM;
1323           if (!sym_is_global (abfd, sym))
1324             i = num_locals2++;
1325           else
1326             i = num_locals + num_globals2++;
1327           new_syms[i] = sym;
1328           sym->udata.i = i + 1;
1329         }
1330     }
1331
1332   bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1333
1334   elf_num_locals (abfd) = num_locals;
1335   elf_num_globals (abfd) = num_globals;
1336   return true;
1337 }
1338
1339 /* Compute the file positions we are going to put the sections at, and
1340    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
1341    is not NULL, this is being called by the ELF backend linker.  */
1342
1343 boolean
1344 _bfd_elf_compute_section_file_positions (abfd, link_info)
1345      bfd *abfd;
1346      struct bfd_link_info *link_info;
1347 {
1348   struct elf_backend_data *bed = get_elf_backend_data (abfd);
1349   boolean failed;
1350   struct bfd_strtab_hash *strtab;
1351   Elf_Internal_Shdr *shstrtab_hdr;
1352
1353   if (abfd->output_has_begun)
1354     return true;
1355
1356   /* Do any elf backend specific processing first.  */
1357   if (bed->elf_backend_begin_write_processing)
1358     (*bed->elf_backend_begin_write_processing) (abfd, link_info);
1359
1360   if (! prep_headers (abfd))
1361     return false;
1362
1363   failed = false;
1364   bfd_map_over_sections (abfd, elf_fake_sections, &failed);
1365   if (failed)
1366     return false;
1367
1368   if (!assign_section_numbers (abfd))
1369     return false;
1370
1371   /* The backend linker builds symbol table information itself.  */
1372   if (link_info == NULL)
1373     {
1374       if (! swap_out_syms (abfd, &strtab))
1375         return false;
1376     }
1377
1378   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
1379   /* sh_name was set in prep_headers.  */
1380   shstrtab_hdr->sh_type = SHT_STRTAB;
1381   shstrtab_hdr->sh_flags = 0;
1382   shstrtab_hdr->sh_addr = 0;
1383   shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1384   shstrtab_hdr->sh_entsize = 0;
1385   shstrtab_hdr->sh_link = 0;
1386   shstrtab_hdr->sh_info = 0;
1387   /* sh_offset is set in assign_file_positions_for_symtabs_and_strtabs.  */
1388   shstrtab_hdr->sh_addralign = 1;
1389
1390   if (!assign_file_positions_except_relocs (abfd,
1391                                             link_info == NULL ? true : false))
1392     return false;
1393
1394   if (link_info == NULL)
1395     {
1396       /* Now that we know where the .strtab section goes, write it
1397          out.  */
1398       if ((bfd_seek (abfd, elf_tdata (abfd)->strtab_hdr.sh_offset, SEEK_SET)
1399            != 0)
1400           || ! _bfd_stringtab_emit (abfd, strtab))
1401         return false;
1402       _bfd_stringtab_free (strtab);
1403     }
1404
1405   abfd->output_has_begun = true;
1406
1407   return true;
1408 }
1409
1410
1411 /* Align to the maximum file alignment that could be required for any
1412    ELF data structure.  */
1413
1414 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1415 static INLINE file_ptr
1416 align_file_position (off, align)
1417      file_ptr off;
1418      int align;
1419 {
1420   return (off + align - 1) & ~(align - 1);
1421 }
1422
1423 /* Assign a file position to a section, aligning to the required
1424    section alignment.  */
1425
1426 INLINE file_ptr
1427 _bfd_elf_assign_file_position_for_section (i_shdrp, offset)
1428      Elf_Internal_Shdr *i_shdrp;
1429      file_ptr offset;
1430 {
1431   unsigned int al;
1432
1433   /* Align the offst.  */
1434   al = i_shdrp->sh_addralign;
1435   if (al > 1)
1436     offset = BFD_ALIGN (offset, al);
1437
1438   i_shdrp->sh_offset = offset;
1439   if (i_shdrp->bfd_section != NULL)
1440     i_shdrp->bfd_section->filepos = offset;
1441   if (i_shdrp->sh_type != SHT_NOBITS)
1442     offset += i_shdrp->sh_size;
1443   return offset;
1444 }
1445
1446 /* Get the size of the program header.
1447
1448    SORTED_HDRS, if non-NULL, is an array of COUNT pointers to headers sorted
1449    by VMA.  Non-allocated sections (!SHF_ALLOC) must appear last.  All
1450    section VMAs and sizes are known so we can compute the correct value.
1451    (??? This may not be perfectly true.  What cases do we miss?)
1452
1453    If SORTED_HDRS is NULL we assume there are two segments: text and data
1454    (exclusive of .interp and .dynamic).
1455
1456    If this is called by the linker before any of the section VMA's are set, it
1457    can't calculate the correct value for a strange memory layout.  This only
1458    happens when SIZEOF_HEADERS is used in a linker script.  In this case,
1459    SORTED_HDRS is NULL and we assume the normal scenario of one text and one
1460    data segment (exclusive of .interp and .dynamic).
1461
1462    ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
1463    will be two segments.  */
1464
1465 static bfd_size_type
1466 get_program_header_size (abfd, sorted_hdrs, count, maxpagesize)
1467      bfd *abfd;
1468      Elf_Internal_Shdr **sorted_hdrs;
1469      unsigned int count;
1470      bfd_vma maxpagesize;
1471 {
1472   size_t segs;
1473   asection *s;
1474   struct elf_backend_data *bed = get_elf_backend_data (abfd);
1475
1476   /* We can't return a different result each time we're called.  */
1477   if (elf_tdata (abfd)->program_header_size != 0)
1478     return elf_tdata (abfd)->program_header_size;
1479
1480   if (sorted_hdrs != NULL)
1481     {
1482       unsigned int i;
1483       unsigned int last_type;
1484       Elf_Internal_Shdr **hdrpp;
1485       /* What we think the current segment's offset is.  */
1486       bfd_vma p_offset;
1487       /* What we think the current segment's address is.  */
1488       bfd_vma p_vaddr;
1489       /* How big we think the current segment is.  */
1490       bfd_vma p_memsz;
1491       /* What we think the current file offset is.  */
1492       bfd_vma file_offset;
1493       bfd_vma next_offset;
1494
1495       /* Scan the headers and compute the number of segments required.  This
1496          code is intentionally similar to the code in map_program_segments.
1497
1498          The `sh_offset' field isn't valid at this point, so we keep our own
1499          running total in `file_offset'.
1500
1501          This works because section VMAs are already known.  */
1502
1503       segs = 1;
1504       /* Make sure the first section goes in the first segment.  */
1505       file_offset = p_offset = sorted_hdrs[0]->sh_addr % maxpagesize;
1506       p_vaddr = sorted_hdrs[0]->sh_addr;
1507       p_memsz = 0;
1508       last_type = SHT_PROGBITS;
1509
1510       for (i = 0, hdrpp = sorted_hdrs; i < count; i++, hdrpp++)
1511         {
1512           Elf_Internal_Shdr *hdr;
1513
1514           hdr = *hdrpp;
1515
1516           /* Ignore any section which will not be part of the process
1517              image.  */
1518           if ((hdr->sh_flags & SHF_ALLOC) == 0)
1519             continue;
1520
1521           /* Keep track of where this and the next sections go.
1522              The section VMA must equal the file position modulo
1523              the page size.  */
1524           file_offset += (hdr->sh_addr - file_offset) % maxpagesize;
1525           next_offset = file_offset;
1526           if (hdr->sh_type != SHT_NOBITS)
1527             next_offset = file_offset + hdr->sh_size;
1528
1529           /* If this section fits in the segment we are constructing, add
1530              it in.  */
1531           if ((file_offset - (p_offset + p_memsz)
1532                == hdr->sh_addr - (p_vaddr + p_memsz))
1533               && (last_type != SHT_NOBITS || hdr->sh_type == SHT_NOBITS))
1534             {
1535               bfd_size_type adjust;
1536
1537               adjust = hdr->sh_addr - (p_vaddr + p_memsz);
1538               p_memsz += hdr->sh_size + adjust;
1539               file_offset = next_offset;
1540               last_type = hdr->sh_type;
1541               continue;
1542             }
1543
1544           /* The section won't fit, start a new segment.  */
1545           ++segs;
1546
1547           /* Initialize the segment.  */
1548           p_vaddr = hdr->sh_addr;
1549           p_memsz = hdr->sh_size;
1550           p_offset = file_offset;
1551           file_offset = next_offset;
1552
1553           last_type = hdr->sh_type;
1554         }
1555     }
1556   else
1557     {
1558       /* Assume we will need exactly two PT_LOAD segments: one for text
1559          and one for data.  */
1560       segs = 2;
1561     }
1562
1563   s = bfd_get_section_by_name (abfd, ".interp");
1564   if (s != NULL && (s->flags & SEC_LOAD) != 0)
1565     {
1566       /* If we have a loadable interpreter section, we need a
1567          PT_INTERP segment.  In this case, assume we also need a
1568          PT_PHDR segment, although that may not be true for all
1569          targets.  */
1570       segs += 2;
1571     }
1572
1573   if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
1574     {
1575       /* We need a PT_DYNAMIC segment.  */
1576       ++segs;
1577     }
1578
1579   /* Let the backend count up any program headers it might need.  */
1580   if (bed->elf_backend_create_program_headers)
1581     segs = ((*bed->elf_backend_create_program_headers)
1582             (abfd, (Elf_Internal_Phdr *) NULL, segs));
1583
1584   elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
1585   return elf_tdata (abfd)->program_header_size;
1586 }
1587
1588 /* Create the program header.  OFF is the file offset where the
1589    program header should be written.  FIRST is the first loadable ELF
1590    section.  SORTED_HDRS is the ELF sections sorted by section
1591    address.  PHDR_SIZE is the size of the program header as returned
1592    by get_program_header_size.  */
1593
1594 static file_ptr
1595 map_program_segments (abfd, off, first, sorted_hdrs, phdr_size)
1596      bfd *abfd;
1597      file_ptr off;
1598      Elf_Internal_Shdr *first;
1599      Elf_Internal_Shdr **sorted_hdrs;
1600      bfd_size_type phdr_size;
1601 {
1602   Elf_Internal_Phdr phdrs[10];
1603   unsigned int phdr_count;
1604   Elf_Internal_Phdr *phdr;
1605   int phdr_size_adjust;
1606   unsigned int i;
1607   Elf_Internal_Shdr **hdrpp;
1608   asection *sinterp, *sdyn;
1609   unsigned int last_type;
1610   Elf_Internal_Ehdr *i_ehdrp;
1611   struct elf_backend_data *bed = get_elf_backend_data (abfd);
1612
1613   BFD_ASSERT ((abfd->flags & (EXEC_P | DYNAMIC)) != 0);
1614   BFD_ASSERT (phdr_size / sizeof (Elf_Internal_Phdr)
1615               <= sizeof phdrs / sizeof (phdrs[0]));
1616
1617   phdr_count = 0;
1618   phdr = phdrs;
1619
1620   if (bed->want_hdr_in_seg)
1621     phdr_size_adjust = first->sh_offset - phdr_size;
1622   else
1623     phdr_size_adjust = 0;
1624
1625   /* If we have a loadable .interp section, we must create a PT_INTERP
1626      segment which must precede all PT_LOAD segments.  We assume that
1627      we must also create a PT_PHDR segment, although that may not be
1628      true for all targets.  */
1629   sinterp = bfd_get_section_by_name (abfd, ".interp");
1630   if (sinterp != NULL && (sinterp->flags & SEC_LOAD) != 0)
1631     {
1632       BFD_ASSERT (first != NULL);
1633
1634       phdr->p_type = PT_PHDR;
1635
1636       phdr->p_offset = off;
1637
1638       /* Account for any adjustment made because of the alignment of
1639          the first loadable section.  */
1640       phdr_size_adjust = (first->sh_offset - phdr_size) - off;
1641       BFD_ASSERT (phdr_size_adjust >= 0 && phdr_size_adjust < 128);
1642
1643       /* The program header precedes all loadable sections.  This lets
1644          us compute its loadable address.  This depends on the linker
1645          script.  */
1646       phdr->p_vaddr = first->sh_addr - (phdr_size + phdr_size_adjust);
1647
1648       phdr->p_paddr = 0;
1649       phdr->p_filesz = phdr_size;
1650       phdr->p_memsz = phdr_size;
1651
1652       /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not.  */
1653       phdr->p_flags = PF_R | PF_X;
1654
1655       phdr->p_align = bed->s->file_align;
1656       BFD_ASSERT ((phdr->p_vaddr - phdr->p_offset) % bed->s->file_align == 0);
1657
1658       /* Include the ELF header in the first loadable segment.  */
1659       phdr_size_adjust += off;
1660
1661       ++phdr_count;
1662       ++phdr;
1663
1664       phdr->p_type = PT_INTERP;
1665       phdr->p_offset = sinterp->filepos;
1666       phdr->p_vaddr = sinterp->vma;
1667       phdr->p_paddr = 0;
1668       phdr->p_filesz = sinterp->_raw_size;
1669       phdr->p_memsz = sinterp->_raw_size;
1670       phdr->p_flags = PF_R;
1671       phdr->p_align = 1 << bfd_get_section_alignment (abfd, sinterp);
1672
1673       ++phdr_count;
1674       ++phdr;
1675     }
1676
1677   /* Look through the sections to see how they will be divided into
1678      program segments.  The sections must be arranged in order by
1679      sh_addr for this to work correctly.  */
1680   phdr->p_type = PT_NULL;
1681   last_type = SHT_PROGBITS;
1682   for (i = 1, hdrpp = sorted_hdrs;
1683        i < elf_elfheader (abfd)->e_shnum;
1684        i++, hdrpp++)
1685     {
1686       Elf_Internal_Shdr *hdr;
1687
1688       hdr = *hdrpp;
1689
1690       /* Ignore any section which will not be part of the process
1691          image.  */
1692       if ((hdr->sh_flags & SHF_ALLOC) == 0)
1693         continue;
1694
1695       /* If this section fits in the segment we are constructing, add
1696          it in.  */
1697       if (phdr->p_type != PT_NULL
1698           && (hdr->sh_offset - (phdr->p_offset + phdr->p_memsz)
1699               == hdr->sh_addr - (phdr->p_vaddr + phdr->p_memsz))
1700           && (last_type != SHT_NOBITS || hdr->sh_type == SHT_NOBITS))
1701         {
1702           bfd_size_type adjust;
1703
1704           adjust = hdr->sh_addr - (phdr->p_vaddr + phdr->p_memsz);
1705           phdr->p_memsz += hdr->sh_size + adjust;
1706           if (hdr->sh_type != SHT_NOBITS)
1707             phdr->p_filesz += hdr->sh_size + adjust;
1708           if ((hdr->sh_flags & SHF_WRITE) != 0)
1709             phdr->p_flags |= PF_W;
1710           if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1711             phdr->p_flags |= PF_X;
1712           last_type = hdr->sh_type;
1713           continue;
1714         }
1715
1716       /* The section won't fit, start a new segment.  If we're already in one,
1717          move to the next one.  */
1718       if (phdr->p_type != PT_NULL)
1719         {
1720           ++phdr;
1721           ++phdr_count;
1722         }
1723
1724       /* Initialize the segment.  */
1725       phdr->p_type = PT_LOAD;
1726       phdr->p_offset = hdr->sh_offset;
1727       phdr->p_vaddr = hdr->sh_addr;
1728       phdr->p_paddr = 0;
1729       if (hdr->sh_type == SHT_NOBITS)
1730         phdr->p_filesz = 0;
1731       else
1732         phdr->p_filesz = hdr->sh_size;
1733       phdr->p_memsz = hdr->sh_size;
1734       phdr->p_flags = PF_R;
1735       if ((hdr->sh_flags & SHF_WRITE) != 0)
1736         phdr->p_flags |= PF_W;
1737       if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1738         phdr->p_flags |= PF_X;
1739       phdr->p_align = bed->maxpagesize;
1740
1741       if (hdr == first
1742           && (bed->want_hdr_in_seg
1743               || (sinterp != NULL
1744                   && (sinterp->flags & SEC_LOAD) != 0)))
1745         {
1746           phdr->p_offset -= phdr_size + phdr_size_adjust;
1747           phdr->p_vaddr -= phdr_size + phdr_size_adjust;
1748           phdr->p_filesz += phdr_size + phdr_size_adjust;
1749           phdr->p_memsz += phdr_size + phdr_size_adjust;
1750         }
1751
1752       last_type = hdr->sh_type;
1753     }
1754
1755   if (phdr->p_type != PT_NULL)
1756     {
1757       ++phdr;
1758       ++phdr_count;
1759     }
1760
1761   /* If we have a .dynamic section, create a PT_DYNAMIC segment.  */
1762   sdyn = bfd_get_section_by_name (abfd, ".dynamic");
1763   if (sdyn != NULL && (sdyn->flags & SEC_LOAD) != 0)
1764     {
1765       phdr->p_type = PT_DYNAMIC;
1766       phdr->p_offset = sdyn->filepos;
1767       phdr->p_vaddr = sdyn->vma;
1768       phdr->p_paddr = 0;
1769       phdr->p_filesz = sdyn->_raw_size;
1770       phdr->p_memsz = sdyn->_raw_size;
1771       phdr->p_flags = PF_R;
1772       if ((sdyn->flags & SEC_READONLY) == 0)
1773         phdr->p_flags |= PF_W;
1774       if ((sdyn->flags & SEC_CODE) != 0)
1775         phdr->p_flags |= PF_X;
1776       phdr->p_align = 1 << bfd_get_section_alignment (abfd, sdyn);
1777
1778       ++phdr;
1779       ++phdr_count;
1780     }
1781
1782   /* Let the backend create additional program headers.  */
1783   if (bed->elf_backend_create_program_headers)
1784     phdr_count = (*bed->elf_backend_create_program_headers) (abfd,
1785                                                              phdrs,
1786                                                              phdr_count);
1787
1788   /* Make sure the return value from get_program_header_size matches
1789      what we computed here.  Actually, it's OK if we allocated too
1790      much space in the program header.  */
1791   if (phdr_count > phdr_size / bed->s->sizeof_phdr)
1792     {
1793       ((*_bfd_error_handler)
1794        ("%s: Not enough room for program headers (allocated %lu, need %u)",
1795         bfd_get_filename (abfd),
1796         (unsigned long) (phdr_size / bed->s->sizeof_phdr),
1797         phdr_count));
1798       bfd_set_error (bfd_error_bad_value);
1799       return (file_ptr) -1;
1800     }
1801
1802   /* Set up program header information.  */
1803   i_ehdrp = elf_elfheader (abfd);
1804   i_ehdrp->e_phentsize = bed->s->sizeof_phdr;
1805   i_ehdrp->e_phoff = off;
1806   i_ehdrp->e_phnum = phdr_count;
1807
1808   /* Save the program headers away.  I don't think anybody uses this
1809      information right now.  */
1810   elf_tdata (abfd)->phdr = ((Elf_Internal_Phdr *)
1811                             bfd_alloc (abfd,
1812                                        (phdr_count
1813                                         * sizeof (Elf_Internal_Phdr))));
1814   if (elf_tdata (abfd)->phdr == NULL && phdr_count != 0)
1815     {
1816       bfd_set_error (bfd_error_no_memory);
1817       return (file_ptr) -1;
1818     }
1819   memcpy (elf_tdata (abfd)->phdr, phdrs,
1820           phdr_count * sizeof (Elf_Internal_Phdr));
1821
1822   /* Write out the program headers.  */
1823   if (bfd_seek (abfd, off, SEEK_SET) != 0)
1824     return (file_ptr) -1;
1825
1826   if (bed->s->write_out_phdrs (abfd, phdrs, phdr_count) != 0)
1827     return (file_ptr) -1;
1828
1829   return off + phdr_count * bed->s->sizeof_phdr;
1830 }
1831
1832 /* Work out the file positions of all the sections.  This is called by
1833    _bfd_elf_compute_section_file_positions.  All the section sizes and
1834    VMAs must be known before this is called.
1835
1836    We do not consider reloc sections at this point, unless they form
1837    part of the loadable image.  Reloc sections are assigned file
1838    positions in assign_file_positions_for_relocs, which is called by
1839    write_object_contents and final_link.
1840
1841    If DOSYMS is false, we do not assign file positions for the symbol
1842    table or the string table.  */
1843
1844 static int elf_sort_hdrs PARAMS ((const PTR, const PTR));
1845
1846 static boolean
1847 assign_file_positions_except_relocs (abfd, dosyms)
1848      bfd *abfd;
1849      boolean dosyms;
1850 {
1851   struct elf_obj_tdata * const tdata = elf_tdata (abfd);
1852   Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
1853   Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
1854   file_ptr off;
1855   struct elf_backend_data *bed = get_elf_backend_data (abfd);
1856
1857   /* Start after the ELF header.  */
1858   off = i_ehdrp->e_ehsize;
1859
1860   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
1861     {
1862       Elf_Internal_Shdr **hdrpp;
1863       unsigned int i;
1864
1865       /* We are not creating an executable, which means that we are
1866          not creating a program header, and that the actual order of
1867          the sections in the file is unimportant.  */
1868       for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
1869         {
1870           Elf_Internal_Shdr *hdr;
1871
1872           hdr = *hdrpp;
1873           if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
1874             {
1875               hdr->sh_offset = -1;
1876               continue;
1877             }
1878           if (! dosyms
1879               && (i == tdata->symtab_section
1880                   || i == tdata->strtab_section))
1881             {
1882               hdr->sh_offset = -1;
1883               continue;
1884             }
1885           
1886           off = _bfd_elf_assign_file_position_for_section (hdr, off);
1887         }
1888     }
1889   else
1890     {
1891       file_ptr phdr_off;
1892       bfd_size_type phdr_size;
1893       bfd_vma maxpagesize;
1894       size_t hdrppsize;
1895       Elf_Internal_Shdr **sorted_hdrs;
1896       Elf_Internal_Shdr **hdrpp;
1897       unsigned int i;
1898       Elf_Internal_Shdr *first;
1899       file_ptr phdr_map;
1900
1901       /* We are creating an executable.  */
1902
1903       maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1904       if (maxpagesize == 0)
1905         maxpagesize = 1;
1906
1907       /* We must sort the sections.  The GNU linker will always create
1908          the sections in an appropriate order, but the Irix 5 linker
1909          will not.  We don't include the dummy first section in the
1910          sort.  We sort sections which are not SHF_ALLOC to the end.  */
1911       hdrppsize = (i_ehdrp->e_shnum - 1) * sizeof (Elf_Internal_Shdr *);
1912       sorted_hdrs = (Elf_Internal_Shdr **) malloc (hdrppsize);
1913       if (sorted_hdrs == NULL)
1914         {
1915           bfd_set_error (bfd_error_no_memory);
1916           return false;
1917         }
1918
1919       memcpy (sorted_hdrs, i_shdrpp + 1, hdrppsize);
1920       qsort (sorted_hdrs, (size_t) i_ehdrp->e_shnum - 1,
1921              sizeof (Elf_Internal_Shdr *), elf_sort_hdrs);
1922
1923       /* We can't actually create the program header until we have set the
1924          file positions for the sections, and we can't do that until we know
1925          how big the header is going to be.  */
1926       off = align_file_position (off, bed->s->file_align);
1927       phdr_size = get_program_header_size (abfd,
1928                                            sorted_hdrs, i_ehdrp->e_shnum - 1,
1929                                            maxpagesize);
1930       if (phdr_size == (bfd_size_type) -1)
1931         return false;
1932
1933       /* Compute the file offsets of each section.  */
1934       phdr_off = off;
1935       off += phdr_size;
1936       first = NULL;
1937       for (i = 1, hdrpp = sorted_hdrs; i < i_ehdrp->e_shnum; i++, hdrpp++)
1938         {
1939           Elf_Internal_Shdr *hdr;
1940
1941           hdr = *hdrpp;
1942           if ((hdr->sh_flags & SHF_ALLOC) == 0)
1943             {
1944               if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
1945                 {
1946                   hdr->sh_offset = -1;
1947                   continue;
1948                 }
1949               if (! dosyms
1950                   && (hdr == i_shdrpp[tdata->symtab_section]
1951                       || hdr == i_shdrpp[tdata->strtab_section]))
1952                 {
1953                   hdr->sh_offset = -1;
1954                   continue;
1955                 }
1956               off = _bfd_elf_assign_file_position_for_section (hdr, off);
1957             }
1958           else
1959             {
1960               if (first == NULL)
1961                 first = hdr;
1962
1963               /* The section VMA must equal the file position modulo
1964                  the page size.  This is required by the program
1965                  header.  */
1966               off += (hdr->sh_addr - off) % maxpagesize;
1967               off = _bfd_elf_assign_file_position_for_section (hdr, off);
1968             }
1969         }
1970
1971       /* Create the program header.  */
1972       phdr_map = map_program_segments (abfd, phdr_off, first, sorted_hdrs,
1973                                        phdr_size);
1974       if (phdr_map == (file_ptr) -1)
1975         return false;
1976       BFD_ASSERT ((bfd_size_type) phdr_map
1977                   <= (bfd_size_type) phdr_off + phdr_size);
1978
1979       free (sorted_hdrs);
1980     }
1981
1982   /* Place the section headers.  */
1983   off = align_file_position (off, bed->s->file_align);
1984   i_ehdrp->e_shoff = off;
1985   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
1986
1987   elf_tdata (abfd)->next_file_pos = off;
1988
1989   return true;
1990 }
1991
1992 /* Sort the ELF headers by VMA.  We sort headers which are not
1993    SHF_ALLOC to the end.  */
1994 static int
1995 elf_sort_hdrs (arg1, arg2)
1996      const PTR arg1;
1997      const PTR arg2;
1998 {
1999   int ret;
2000   const Elf_Internal_Shdr *hdr1 = *(const Elf_Internal_Shdr **) arg1;
2001   const Elf_Internal_Shdr *hdr2 = *(const Elf_Internal_Shdr **) arg2;
2002
2003 #define TOEND(x) (((x)->sh_flags & SHF_ALLOC)==0)
2004
2005   if (TOEND (hdr1))
2006     if (TOEND (hdr2))
2007       return 0;
2008     else 
2009       return 1;
2010
2011   if (TOEND (hdr2))
2012     return -1;
2013
2014   if (hdr1->sh_addr < hdr2->sh_addr)
2015     return -1;
2016   else if (hdr1->sh_addr > hdr2->sh_addr)
2017     return 1;
2018
2019   /* Put !SHT_NOBITS sections before SHT_NOBITS ones.
2020      The main loop in map_program_segments requires this. */
2021
2022   ret = (hdr1->sh_type == SHT_NOBITS) - (hdr2->sh_type == SHT_NOBITS);
2023
2024   if (ret != 0)
2025     return ret;
2026   if (hdr1->sh_size < hdr2->sh_size)
2027     return -1;
2028   if (hdr1->sh_size > hdr2->sh_size)
2029     return 1;
2030   return 0;
2031 }
2032
2033 static boolean
2034 prep_headers (abfd)
2035      bfd *abfd;
2036 {
2037   Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */
2038   Elf_Internal_Phdr *i_phdrp = 0;       /* Program header table, internal form */
2039   Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
2040   int count;
2041   struct bfd_strtab_hash *shstrtab;
2042   struct elf_backend_data *bed = get_elf_backend_data (abfd);
2043
2044   i_ehdrp = elf_elfheader (abfd);
2045   i_shdrp = elf_elfsections (abfd);
2046
2047   shstrtab = _bfd_elf_stringtab_init ();
2048   if (shstrtab == NULL)
2049     return false;
2050
2051   elf_shstrtab (abfd) = shstrtab;
2052
2053   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2054   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2055   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2056   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2057
2058   i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
2059   i_ehdrp->e_ident[EI_DATA] =
2060     abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB;
2061   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
2062
2063   for (count = EI_PAD; count < EI_NIDENT; count++)
2064     i_ehdrp->e_ident[count] = 0;
2065
2066   if ((abfd->flags & DYNAMIC) != 0)
2067     i_ehdrp->e_type = ET_DYN;
2068   else if ((abfd->flags & EXEC_P) != 0)
2069     i_ehdrp->e_type = ET_EXEC;
2070   else
2071     i_ehdrp->e_type = ET_REL;
2072
2073   switch (bfd_get_arch (abfd))
2074     {
2075     case bfd_arch_unknown:
2076       i_ehdrp->e_machine = EM_NONE;
2077       break;
2078     case bfd_arch_sparc:
2079       if (bed->s->arch_size == 64)
2080         i_ehdrp->e_machine = EM_SPARC64;
2081       else
2082         i_ehdrp->e_machine = EM_SPARC;
2083       break;
2084     case bfd_arch_i386:
2085       i_ehdrp->e_machine = EM_386;
2086       break;
2087     case bfd_arch_m68k:
2088       i_ehdrp->e_machine = EM_68K;
2089       break;
2090     case bfd_arch_m88k:
2091       i_ehdrp->e_machine = EM_88K;
2092       break;
2093     case bfd_arch_i860:
2094       i_ehdrp->e_machine = EM_860;
2095       break;
2096     case bfd_arch_mips: /* MIPS Rxxxx */
2097       i_ehdrp->e_machine = EM_MIPS;     /* only MIPS R3000 */
2098       break;
2099     case bfd_arch_hppa:
2100       i_ehdrp->e_machine = EM_PARISC;
2101       break;
2102     case bfd_arch_powerpc:
2103       i_ehdrp->e_machine = EM_PPC;
2104       break;
2105 /* start-sanitize-arc */
2106     case bfd_arch_arc:
2107       i_ehdrp->e_machine = EM_CYGNUS_ARC;
2108       break;
2109 /* end-sanitize-arc */
2110       /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2111     default:
2112       i_ehdrp->e_machine = EM_NONE;
2113     }
2114   i_ehdrp->e_version = bed->s->ev_current;
2115   i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
2116
2117   /* no program header, for now. */
2118   i_ehdrp->e_phoff = 0;
2119   i_ehdrp->e_phentsize = 0;
2120   i_ehdrp->e_phnum = 0;
2121
2122   /* each bfd section is section header entry */
2123   i_ehdrp->e_entry = bfd_get_start_address (abfd);
2124   i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
2125
2126   /* if we're building an executable, we'll need a program header table */
2127   if (abfd->flags & EXEC_P)
2128     {
2129       /* it all happens later */
2130 #if 0
2131       i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2132
2133       /* elf_build_phdrs() returns a (NULL-terminated) array of
2134          Elf_Internal_Phdrs */
2135       i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2136       i_ehdrp->e_phoff = outbase;
2137       outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2138 #endif
2139     }
2140   else
2141     {
2142       i_ehdrp->e_phentsize = 0;
2143       i_phdrp = 0;
2144       i_ehdrp->e_phoff = 0;
2145     }
2146
2147   elf_tdata (abfd)->symtab_hdr.sh_name =
2148     (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
2149   elf_tdata (abfd)->strtab_hdr.sh_name =
2150     (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
2151   elf_tdata (abfd)->shstrtab_hdr.sh_name =
2152     (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
2153   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2154       || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2155       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
2156     return false;
2157
2158   return true;
2159 }
2160
2161 /* Assign file positions for all the reloc sections which are not part
2162    of the loadable file image.  */
2163
2164 void
2165 _bfd_elf_assign_file_positions_for_relocs (abfd)
2166      bfd *abfd;
2167 {
2168   file_ptr off;
2169   unsigned int i;
2170   Elf_Internal_Shdr **shdrpp;
2171
2172   off = elf_tdata (abfd)->next_file_pos;
2173
2174   for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
2175        i < elf_elfheader (abfd)->e_shnum;
2176        i++, shdrpp++)
2177     {
2178       Elf_Internal_Shdr *shdrp;
2179
2180       shdrp = *shdrpp;
2181       if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
2182           && shdrp->sh_offset == -1)
2183         off = _bfd_elf_assign_file_position_for_section (shdrp, off);
2184     }
2185
2186   elf_tdata (abfd)->next_file_pos = off;
2187 }
2188
2189 boolean
2190 _bfd_elf_write_object_contents (abfd)
2191      bfd *abfd;
2192 {
2193   struct elf_backend_data *bed = get_elf_backend_data (abfd);
2194   Elf_Internal_Ehdr *i_ehdrp;
2195   Elf_Internal_Shdr **i_shdrp;
2196   boolean failed;
2197   unsigned int count;
2198
2199   if (! abfd->output_has_begun
2200       && ! _bfd_elf_compute_section_file_positions (abfd,
2201                                                     (struct bfd_link_info *) NULL))
2202     return false;
2203
2204   i_shdrp = elf_elfsections (abfd);
2205   i_ehdrp = elf_elfheader (abfd);
2206
2207   failed = false;
2208   bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
2209   if (failed)
2210     return false;
2211   _bfd_elf_assign_file_positions_for_relocs (abfd);
2212
2213   /* After writing the headers, we need to write the sections too... */
2214   for (count = 1; count < i_ehdrp->e_shnum; count++)
2215     {
2216       if (bed->elf_backend_section_processing)
2217         (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
2218       if (i_shdrp[count]->contents)
2219         {
2220           if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
2221               || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
2222                              1, abfd)
2223                   != i_shdrp[count]->sh_size))
2224             return false;
2225         }
2226     }
2227
2228   /* Write out the section header names.  */
2229   if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
2230       || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
2231     return false;
2232
2233   if (bed->elf_backend_final_write_processing)
2234     (*bed->elf_backend_final_write_processing) (abfd,
2235                                                 elf_tdata (abfd)->linker);
2236
2237   return bed->s->write_shdrs_and_ehdr (abfd);
2238 }
2239
2240 /* given a section, search the header to find them... */
2241 int
2242 _bfd_elf_section_from_bfd_section (abfd, asect)
2243      bfd *abfd;
2244      struct sec *asect;
2245 {
2246   struct elf_backend_data *bed = get_elf_backend_data (abfd);
2247   Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
2248   int index;
2249   Elf_Internal_Shdr *hdr;
2250   int maxindex = elf_elfheader (abfd)->e_shnum;
2251
2252   for (index = 0; index < maxindex; index++)
2253     {
2254       hdr = i_shdrp[index];
2255       if (hdr->bfd_section == asect)
2256         return index;
2257     }
2258
2259   if (bed->elf_backend_section_from_bfd_section)
2260     {
2261       for (index = 0; index < maxindex; index++)
2262         {
2263           int retval;
2264
2265           hdr = i_shdrp[index];
2266           retval = index;
2267           if ((*bed->elf_backend_section_from_bfd_section)
2268               (abfd, hdr, asect, &retval))
2269             return retval;
2270         }
2271     }
2272
2273   if (bfd_is_abs_section (asect))
2274     return SHN_ABS;
2275   if (bfd_is_com_section (asect))
2276     return SHN_COMMON;
2277   if (bfd_is_und_section (asect))
2278     return SHN_UNDEF;
2279
2280   return -1;
2281 }
2282
2283 /* given a symbol, return the bfd index for that symbol.  */
2284  int
2285 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
2286      bfd *abfd;
2287      struct symbol_cache_entry **asym_ptr_ptr;
2288 {
2289   struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr;
2290   int idx;
2291   flagword flags = asym_ptr->flags;
2292
2293   /* When gas creates relocations against local labels, it creates its
2294      own symbol for the section, but does put the symbol into the
2295      symbol chain, so udata is 0.  When the linker is generating
2296      relocatable output, this section symbol may be for one of the
2297      input sections rather than the output section.  */
2298   if (asym_ptr->udata.i == 0
2299       && (flags & BSF_SECTION_SYM)
2300       && asym_ptr->section)
2301     {
2302       int indx;
2303
2304       if (asym_ptr->section->output_section != NULL)
2305         indx = asym_ptr->section->output_section->index;
2306       else
2307         indx = asym_ptr->section->index;
2308       if (elf_section_syms (abfd)[indx])
2309         asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
2310     }
2311
2312   idx = asym_ptr->udata.i;
2313   BFD_ASSERT (idx != 0);
2314
2315 #if DEBUG & 4
2316   {
2317     fprintf (stderr,
2318              "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
2319      (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags));
2320     fflush (stderr);
2321   }
2322 #endif
2323
2324   return idx;
2325 }
2326
2327 static boolean
2328 swap_out_syms (abfd, sttp)
2329      bfd *abfd;
2330      struct bfd_strtab_hash **sttp;
2331 {
2332   struct elf_backend_data *bed = get_elf_backend_data (abfd);
2333
2334   if (!elf_map_symbols (abfd))
2335     return false;
2336
2337   /* Dump out the symtabs. */
2338   {
2339     int symcount = bfd_get_symcount (abfd);
2340     asymbol **syms = bfd_get_outsymbols (abfd);
2341     struct bfd_strtab_hash *stt;
2342     Elf_Internal_Shdr *symtab_hdr;
2343     Elf_Internal_Shdr *symstrtab_hdr;
2344     char *outbound_syms;
2345     int idx;
2346
2347     stt = _bfd_elf_stringtab_init ();
2348     if (stt == NULL)
2349       return false;
2350
2351     symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2352     symtab_hdr->sh_type = SHT_SYMTAB;
2353     symtab_hdr->sh_entsize = bed->s->sizeof_sym;
2354     symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
2355     symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
2356     symtab_hdr->sh_addralign = bed->s->file_align;
2357
2358     symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
2359     symstrtab_hdr->sh_type = SHT_STRTAB;
2360
2361     outbound_syms = bfd_alloc (abfd,
2362                                (1 + symcount) * bed->s->sizeof_sym);
2363     if (outbound_syms == NULL)
2364       {
2365         bfd_set_error (bfd_error_no_memory);
2366         return false;
2367       }
2368     symtab_hdr->contents = (PTR) outbound_syms;
2369
2370     /* now generate the data (for "contents") */
2371     {
2372       /* Fill in zeroth symbol and swap it out.  */
2373       Elf_Internal_Sym sym;
2374       sym.st_name = 0;
2375       sym.st_value = 0;
2376       sym.st_size = 0;
2377       sym.st_info = 0;
2378       sym.st_other = 0;
2379       sym.st_shndx = SHN_UNDEF;
2380       bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
2381       outbound_syms += bed->s->sizeof_sym;
2382     }
2383     for (idx = 0; idx < symcount; idx++)
2384       {
2385         Elf_Internal_Sym sym;
2386         bfd_vma value = syms[idx]->value;
2387         elf_symbol_type *type_ptr;
2388         flagword flags = syms[idx]->flags;
2389
2390         if (flags & BSF_SECTION_SYM)
2391           /* Section symbols have no names.  */
2392           sym.st_name = 0;
2393         else
2394           {
2395             sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
2396                                                               syms[idx]->name,
2397                                                               true, false);
2398             if (sym.st_name == (unsigned long) -1)
2399               return false;
2400           }
2401
2402         type_ptr = elf_symbol_from (abfd, syms[idx]);
2403
2404         if (bfd_is_com_section (syms[idx]->section))
2405           {
2406             /* ELF common symbols put the alignment into the `value' field,
2407                and the size into the `size' field.  This is backwards from
2408                how BFD handles it, so reverse it here.  */
2409             sym.st_size = value;
2410             if (type_ptr == NULL
2411                 || type_ptr->internal_elf_sym.st_value == 0)
2412               sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
2413             else
2414               sym.st_value = type_ptr->internal_elf_sym.st_value;
2415             sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd,
2416                                                               syms[idx]->section);
2417           }
2418         else
2419           {
2420             asection *sec = syms[idx]->section;
2421             int shndx;
2422
2423             if (sec->output_section)
2424               {
2425                 value += sec->output_offset;
2426                 sec = sec->output_section;
2427               }
2428             value += sec->vma;
2429             sym.st_value = value;
2430             sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
2431             sym.st_shndx = shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2432             if (shndx == -1)
2433               {
2434                 asection *sec2;
2435                 /* Writing this would be a hell of a lot easier if we had
2436                    some decent documentation on bfd, and knew what to expect
2437                    of the library, and what to demand of applications.  For
2438                    example, it appears that `objcopy' might not set the
2439                    section of a symbol to be a section that is actually in
2440                    the output file.  */
2441                 sec2 = bfd_get_section_by_name (abfd, sec->name);
2442                 BFD_ASSERT (sec2 != 0);
2443                 sym.st_shndx = shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
2444                 BFD_ASSERT (shndx != -1);
2445               }
2446           }
2447
2448         if (bfd_is_com_section (syms[idx]->section))
2449           sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT);
2450         else if (bfd_is_und_section (syms[idx]->section))
2451           sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
2452                                       ? STB_WEAK
2453                                       : STB_GLOBAL),
2454                                      ((flags & BSF_FUNCTION)
2455                                       ? STT_FUNC
2456                                       : STT_NOTYPE));
2457         else if (flags & BSF_SECTION_SYM)
2458           sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
2459         else if (flags & BSF_FILE)
2460           sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
2461         else
2462           {
2463             int bind = STB_LOCAL;
2464             int type = STT_OBJECT;
2465
2466             if (flags & BSF_LOCAL)
2467               bind = STB_LOCAL;
2468             else if (flags & BSF_WEAK)
2469               bind = STB_WEAK;
2470             else if (flags & BSF_GLOBAL)
2471               bind = STB_GLOBAL;
2472
2473             if (flags & BSF_FUNCTION)
2474               type = STT_FUNC;
2475
2476             sym.st_info = ELF_ST_INFO (bind, type);
2477           }
2478
2479         sym.st_other = 0;
2480         bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
2481         outbound_syms += bed->s->sizeof_sym;
2482       }
2483
2484     *sttp = stt;
2485     symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
2486     symstrtab_hdr->sh_type = SHT_STRTAB;
2487
2488     symstrtab_hdr->sh_flags = 0;
2489     symstrtab_hdr->sh_addr = 0;
2490     symstrtab_hdr->sh_entsize = 0;
2491     symstrtab_hdr->sh_link = 0;
2492     symstrtab_hdr->sh_info = 0;
2493     symstrtab_hdr->sh_addralign = 1;
2494   }
2495
2496   return true;
2497 }
2498
2499 /* Return the number of bytes required to hold the symtab vector.
2500
2501    Note that we base it on the count plus 1, since we will null terminate
2502    the vector allocated based on this size.  However, the ELF symbol table
2503    always has a dummy entry as symbol #0, so it ends up even.  */
2504
2505 long
2506 _bfd_elf_get_symtab_upper_bound (abfd)
2507      bfd *abfd;
2508 {
2509   long symcount;
2510   long symtab_size;
2511   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
2512
2513   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2514   symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
2515
2516   return symtab_size;
2517 }
2518
2519 long
2520 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
2521      bfd *abfd;
2522 {
2523   long symcount;
2524   long symtab_size;
2525   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2526
2527   if (elf_dynsymtab (abfd) == 0)
2528     {
2529       bfd_set_error (bfd_error_invalid_operation);
2530       return -1;
2531     }
2532
2533   symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2534   symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
2535
2536   return symtab_size;
2537 }
2538
2539 long
2540 _bfd_elf_get_reloc_upper_bound (abfd, asect)
2541      bfd *abfd;
2542      sec_ptr asect;
2543 {
2544   return (asect->reloc_count + 1) * sizeof (arelent *);
2545 }
2546
2547 /* Canonicalize the relocs.  */
2548
2549 long
2550 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
2551      bfd *abfd;
2552      sec_ptr section;
2553      arelent **relptr;
2554      asymbol **symbols;
2555 {
2556   arelent *tblptr;
2557   unsigned int i;
2558
2559   if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd, section, symbols))
2560     return -1;
2561
2562   tblptr = section->relocation;
2563   for (i = 0; i < section->reloc_count; i++)
2564     *relptr++ = tblptr++;
2565
2566   *relptr = NULL;
2567
2568   return section->reloc_count;
2569 }
2570
2571 long
2572 _bfd_elf_get_symtab (abfd, alocation)
2573      bfd *abfd;
2574      asymbol **alocation;
2575 {
2576   long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false);
2577
2578   if (symcount >= 0)
2579     bfd_get_symcount (abfd) = symcount;
2580   return symcount;
2581 }
2582
2583 long
2584 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
2585      bfd *abfd;
2586      asymbol **alocation;
2587 {
2588   return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true);
2589 }
2590
2591 asymbol *
2592 _bfd_elf_make_empty_symbol (abfd)
2593      bfd *abfd;
2594 {
2595   elf_symbol_type *newsym;
2596
2597   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
2598   if (!newsym)
2599     {
2600       bfd_set_error (bfd_error_no_memory);
2601       return NULL;
2602     }
2603   else
2604     {
2605       newsym->symbol.the_bfd = abfd;
2606       return &newsym->symbol;
2607     }
2608 }
2609
2610 void
2611 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
2612      bfd *ignore_abfd;
2613      asymbol *symbol;
2614      symbol_info *ret;
2615 {
2616   bfd_symbol_info (symbol, ret);
2617 }
2618
2619 alent *
2620 _bfd_elf_get_lineno (ignore_abfd, symbol)
2621      bfd *ignore_abfd;
2622      asymbol *symbol;
2623 {
2624   abort ();
2625   return NULL;
2626 }
2627
2628 boolean
2629 _bfd_elf_set_arch_mach (abfd, arch, machine)
2630      bfd *abfd;
2631      enum bfd_architecture arch;
2632      unsigned long machine;
2633 {
2634   /* If this isn't the right architecture for this backend, and this
2635      isn't the generic backend, fail.  */
2636   if (arch != get_elf_backend_data (abfd)->arch
2637       && arch != bfd_arch_unknown
2638       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
2639     return false;
2640
2641   return bfd_default_set_arch_mach (abfd, arch, machine);
2642 }
2643
2644 /* Find the nearest line to a particular section and offset, for error
2645    reporting.  */
2646
2647 boolean
2648 _bfd_elf_find_nearest_line (abfd,
2649                             section,
2650                             symbols,
2651                             offset,
2652                             filename_ptr,
2653                             functionname_ptr,
2654                             line_ptr)
2655      bfd *abfd;
2656      asection *section;
2657      asymbol **symbols;
2658      bfd_vma offset;
2659      CONST char **filename_ptr;
2660      CONST char **functionname_ptr;
2661      unsigned int *line_ptr;
2662 {
2663   const char *filename;
2664   asymbol *func;
2665   asymbol **p;
2666
2667   if (symbols == NULL)
2668     return false;
2669
2670   filename = NULL;
2671   func = NULL;
2672
2673   for (p = symbols; *p != NULL; p++)
2674     {
2675       elf_symbol_type *q;
2676
2677       q = (elf_symbol_type *) *p;
2678
2679       if (bfd_get_section (&q->symbol) != section)
2680         continue;
2681
2682       switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
2683         {
2684         default:
2685           break;
2686         case STT_FILE:
2687           filename = bfd_asymbol_name (&q->symbol);
2688           break;
2689         case STT_FUNC:
2690           if (func == NULL
2691               || q->symbol.value <= offset)
2692             func = (asymbol *) q;
2693           break;
2694         }
2695     }
2696
2697   if (func == NULL)
2698     return false;
2699
2700   *filename_ptr = filename;
2701   *functionname_ptr = bfd_asymbol_name (func);
2702   *line_ptr = 0;
2703   return true;
2704 }
2705
2706 int
2707 _bfd_elf_sizeof_headers (abfd, reloc)
2708      bfd *abfd;
2709      boolean reloc;
2710 {
2711   int ret;
2712
2713   ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
2714   if (! reloc)
2715     ret += get_program_header_size (abfd, (Elf_Internal_Shdr **) NULL, 0,
2716                                     (bfd_vma) 0);
2717   return ret;
2718 }
2719
2720 boolean
2721 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
2722      bfd *abfd;
2723      sec_ptr section;
2724      PTR location;
2725      file_ptr offset;
2726      bfd_size_type count;
2727 {
2728   Elf_Internal_Shdr *hdr;
2729
2730   if (! abfd->output_has_begun
2731       && ! _bfd_elf_compute_section_file_positions (abfd,
2732                                                     (struct bfd_link_info *) NULL))
2733     return false;
2734
2735   hdr = &elf_section_data (section)->this_hdr;
2736
2737   if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
2738     return false;
2739   if (bfd_write (location, 1, count, abfd) != count)
2740     return false;
2741
2742   return true;
2743 }
2744
2745 void
2746 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
2747      bfd *abfd;
2748      arelent *cache_ptr;
2749      Elf_Internal_Rela *dst;
2750 {
2751   abort ();
2752 }
2753
2754 #if 0
2755 void
2756 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
2757      bfd *abfd;
2758      arelent *cache_ptr;
2759      Elf_Internal_Rel *dst;
2760 {
2761   abort ();
2762 }
2763 #endif
This page took 0.18655 seconds and 4 git commands to generate.