]> Git Repo - binutils.git/blob - bfd/elfcode.h
* coff-mips.c (mips_relocate_section): Add MIPS_R_JMPADDR overflow
[binutils.git] / bfd / elfcode.h
1 /* ELF executable support for BFD.
2    Copyright 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
3
4    Written by Fred Fish @ Cygnus Support, from information published
5    in "UNIX System V Release 4, Programmers Guide: ANSI C and
6    Programming Support Tools".  Sufficient support for gdb.
7
8    Rewritten by Mark Eichin @ Cygnus Support, from information
9    published in "System V Application Binary Interface", chapters 4
10    and 5, as well as the various "Processor Supplement" documents
11    derived from it. Added support for assembler and other object file
12    utilities.  Further work done by Ken Raeburn (Cygnus Support), Michael
13    Meissner (Open Software Foundation), and Peter Hoogenboom (University
14    of Utah) to finish and extend this.
15
16 This file is part of BFD, the Binary File Descriptor library.
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
31
32 /* Problems and other issues to resolve.
33
34    (1)  BFD expects there to be some fixed number of "sections" in
35         the object file.  I.E. there is a "section_count" variable in the
36         bfd structure which contains the number of sections.  However, ELF
37         supports multiple "views" of a file.  In particular, with current
38         implementations, executable files typically have two tables, a
39         program header table and a section header table, both of which
40         partition the executable.
41
42         In ELF-speak, the "linking view" of the file uses the section header
43         table to access "sections" within the file, and the "execution view"
44         uses the program header table to access "segments" within the file.
45         "Segments" typically may contain all the data from one or more
46         "sections".
47
48         Note that the section header table is optional in ELF executables,
49         but it is this information that is most useful to gdb.  If the
50         section header table is missing, then gdb should probably try
51         to make do with the program header table.  (FIXME)
52
53    (2)  The code in this file is compiled twice, once in 32-bit mode and
54         once in 64-bit mode.  More of it should be made size-independent
55         and moved into elf.c.
56
57    (3)  ELF section symbols are handled rather sloppily now.  This should
58         be cleaned up, and ELF section symbols reconciled with BFD section
59         symbols.
60  */
61
62 #include <string.h>             /* For strrchr and friends */
63 #include "bfd.h"
64 #include "sysdep.h"
65 #include "bfdlink.h"
66 #include "libbfd.h"
67 #include "libelf.h"
68
69 /* Renaming structures, typedefs, macros and functions to be size-specific.  */
70 #define Elf_External_Ehdr       NAME(Elf,External_Ehdr)
71 #define Elf_External_Sym        NAME(Elf,External_Sym)
72 #define Elf_External_Shdr       NAME(Elf,External_Shdr)
73 #define Elf_External_Phdr       NAME(Elf,External_Phdr)
74 #define Elf_External_Rel        NAME(Elf,External_Rel)
75 #define Elf_External_Rela       NAME(Elf,External_Rela)
76 #define Elf_External_Dyn        NAME(Elf,External_Dyn)
77
78 #define elf_core_file_failing_command   NAME(bfd_elf,core_file_failing_command)
79 #define elf_core_file_failing_signal    NAME(bfd_elf,core_file_failing_signal)
80 #define elf_core_file_matches_executable_p \
81   NAME(bfd_elf,core_file_matches_executable_p)
82 #define elf_object_p                    NAME(bfd_elf,object_p)
83 #define elf_core_file_p                 NAME(bfd_elf,core_file_p)
84 #define elf_get_symtab_upper_bound      NAME(bfd_elf,get_symtab_upper_bound)
85 #define elf_get_dynamic_symtab_upper_bound \
86   NAME(bfd_elf,get_dynamic_symtab_upper_bound)
87 #define elf_swap_reloc_in               NAME(bfd_elf,swap_reloc_in)
88 #define elf_swap_reloca_in              NAME(bfd_elf,swap_reloca_in)
89 #define elf_swap_reloc_out              NAME(bfd_elf,swap_reloc_out)
90 #define elf_swap_reloca_out             NAME(bfd_elf,swap_reloca_out)
91 #define elf_swap_dyn_in                 NAME(bfd_elf,swap_dyn_in)
92 #define elf_swap_dyn_out                NAME(bfd_elf,swap_dyn_out)
93 #define elf_get_reloc_upper_bound       NAME(bfd_elf,get_reloc_upper_bound)
94 #define elf_canonicalize_reloc          NAME(bfd_elf,canonicalize_reloc)
95 #define elf_get_symtab                  NAME(bfd_elf,get_symtab)
96 #define elf_canonicalize_dynamic_symtab \
97   NAME(bfd_elf,canonicalize_dynamic_symtab)
98 #define elf_make_empty_symbol           NAME(bfd_elf,make_empty_symbol)
99 #define elf_get_symbol_info             NAME(bfd_elf,get_symbol_info)
100 #define elf_print_symbol                NAME(bfd_elf,print_symbol)
101 #define elf_get_lineno                  NAME(bfd_elf,get_lineno)
102 #define elf_set_arch_mach               NAME(bfd_elf,set_arch_mach)
103 #define elf_find_nearest_line           NAME(bfd_elf,find_nearest_line)
104 #define elf_sizeof_headers              NAME(bfd_elf,sizeof_headers)
105 #define elf_set_section_contents        NAME(bfd_elf,set_section_contents)
106 #define elf_no_info_to_howto            NAME(bfd_elf,no_info_to_howto)
107 #define elf_no_info_to_howto_rel        NAME(bfd_elf,no_info_to_howto_rel)
108 #define elf_new_section_hook            NAME(bfd_elf,new_section_hook)
109 #define write_relocs                    NAME(bfd_elf,_write_relocs)
110 #define elf_find_section                NAME(bfd_elf,find_section)
111 #define elf_bfd_link_add_symbols        NAME(bfd_elf,bfd_link_add_symbols)
112 #define elf_add_dynamic_entry           NAME(bfd_elf,add_dynamic_entry)
113 #define elf_bfd_final_link              NAME(bfd_elf,bfd_final_link)
114
115 #if ARCH_SIZE == 64
116 #define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
117 #define ELF_R_SYM(X)    ELF64_R_SYM(X)
118 #define ELF_R_TYPE(X)   ELF64_R_TYPE(X)
119 #define ELFCLASS        ELFCLASS64
120 #define FILE_ALIGN      8
121 #define LOG_FILE_ALIGN  3
122 #endif
123 #if ARCH_SIZE == 32
124 #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
125 #define ELF_R_SYM(X)    ELF32_R_SYM(X)
126 #define ELF_R_TYPE(X)   ELF32_R_TYPE(X)
127 #define ELFCLASS        ELFCLASS32
128 #define FILE_ALIGN      4
129 #define LOG_FILE_ALIGN  2
130 #endif
131
132 /* Forward declarations of static functions */
133
134 static unsigned long bfd_add_to_strtab
135   PARAMS ((bfd *, struct strtab *, const char *));
136 static asection *section_from_elf_index PARAMS ((bfd *, unsigned int));
137
138 static int elf_section_from_bfd_section PARAMS ((bfd *, struct sec *));
139
140 static long elf_slurp_symbol_table PARAMS ((bfd *, asymbol **, boolean));
141
142 static int elf_symbol_from_bfd_symbol PARAMS ((bfd *,
143                                              struct symbol_cache_entry **));
144
145 static boolean elf_compute_section_file_positions
146   PARAMS ((bfd *, struct bfd_link_info *));
147 static boolean prep_headers PARAMS ((bfd *));
148 static boolean assign_section_numbers PARAMS ((bfd *));
149 static file_ptr align_file_position PARAMS ((file_ptr));
150 static file_ptr assign_file_position_for_section
151   PARAMS ((Elf_Internal_Shdr *, file_ptr, boolean));
152 static boolean assign_file_positions_except_relocs PARAMS ((bfd *, boolean));
153 static void assign_file_positions_for_relocs PARAMS ((bfd *));
154 static bfd_size_type get_program_header_size PARAMS ((bfd *));
155 static file_ptr map_program_segments
156   PARAMS ((bfd *, file_ptr, Elf_Internal_Shdr *, bfd_size_type));
157
158 static boolean elf_map_symbols PARAMS ((bfd *));
159 static boolean swap_out_syms PARAMS ((bfd *));
160
161 static boolean bfd_section_from_shdr PARAMS ((bfd *, unsigned int shindex));
162
163 #ifdef DEBUG
164 static void elf_debug_section PARAMS ((char *, int, Elf_Internal_Shdr *));
165 static void elf_debug_file PARAMS ((Elf_Internal_Ehdr *));
166 #endif
167
168 #define elf_string_from_elf_strtab(abfd,strindex) \
169      elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex)
170 \f
171
172 /* Structure swapping routines */
173
174 /* Should perhaps use put_offset, put_word, etc.  For now, the two versions
175    can be handled by explicitly specifying 32 bits or "the long type".  */
176 #if ARCH_SIZE == 64
177 #define put_word        bfd_h_put_64
178 #define get_word        bfd_h_get_64
179 #endif
180 #if ARCH_SIZE == 32
181 #define put_word        bfd_h_put_32
182 #define get_word        bfd_h_get_32
183 #endif
184
185 /* Translate an ELF symbol in external format into an ELF symbol in internal
186    format. */
187
188 static void
189 elf_swap_symbol_in (abfd, src, dst)
190      bfd *abfd;
191      Elf_External_Sym *src;
192      Elf_Internal_Sym *dst;
193 {
194   dst->st_name = bfd_h_get_32 (abfd, (bfd_byte *) src->st_name);
195   dst->st_value = get_word (abfd, (bfd_byte *) src->st_value);
196   dst->st_size = get_word (abfd, (bfd_byte *) src->st_size);
197   dst->st_info = bfd_h_get_8 (abfd, (bfd_byte *) src->st_info);
198   dst->st_other = bfd_h_get_8 (abfd, (bfd_byte *) src->st_other);
199   dst->st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src->st_shndx);
200 }
201
202 /* Translate an ELF symbol in internal format into an ELF symbol in external
203    format. */
204
205 static void
206 elf_swap_symbol_out (abfd, src, dst)
207      bfd *abfd;
208      Elf_Internal_Sym *src;
209      Elf_External_Sym *dst;
210 {
211   bfd_h_put_32 (abfd, src->st_name, dst->st_name);
212   put_word (abfd, src->st_value, dst->st_value);
213   put_word (abfd, src->st_size, dst->st_size);
214   bfd_h_put_8 (abfd, src->st_info, dst->st_info);
215   bfd_h_put_8 (abfd, src->st_other, dst->st_other);
216   bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx);
217 }
218
219
220 /* Translate an ELF file header in external format into an ELF file header in
221    internal format. */
222
223 static void
224 elf_swap_ehdr_in (abfd, src, dst)
225      bfd *abfd;
226      Elf_External_Ehdr *src;
227      Elf_Internal_Ehdr *dst;
228 {
229   memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
230   dst->e_type = bfd_h_get_16 (abfd, (bfd_byte *) src->e_type);
231   dst->e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src->e_machine);
232   dst->e_version = bfd_h_get_32 (abfd, (bfd_byte *) src->e_version);
233   dst->e_entry = get_word (abfd, (bfd_byte *) src->e_entry);
234   dst->e_phoff = get_word (abfd, (bfd_byte *) src->e_phoff);
235   dst->e_shoff = get_word (abfd, (bfd_byte *) src->e_shoff);
236   dst->e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->e_flags);
237   dst->e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_ehsize);
238   dst->e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phentsize);
239   dst->e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phnum);
240   dst->e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shentsize);
241   dst->e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shnum);
242   dst->e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shstrndx);
243 }
244
245 /* Translate an ELF file header in internal format into an ELF file header in
246    external format. */
247
248 static void
249 elf_swap_ehdr_out (abfd, src, dst)
250      bfd *abfd;
251      Elf_Internal_Ehdr *src;
252      Elf_External_Ehdr *dst;
253 {
254   memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
255   /* note that all elements of dst are *arrays of unsigned char* already... */
256   bfd_h_put_16 (abfd, src->e_type, dst->e_type);
257   bfd_h_put_16 (abfd, src->e_machine, dst->e_machine);
258   bfd_h_put_32 (abfd, src->e_version, dst->e_version);
259   put_word (abfd, src->e_entry, dst->e_entry);
260   put_word (abfd, src->e_phoff, dst->e_phoff);
261   put_word (abfd, src->e_shoff, dst->e_shoff);
262   bfd_h_put_32 (abfd, src->e_flags, dst->e_flags);
263   bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize);
264   bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize);
265   bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum);
266   bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize);
267   bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum);
268   bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx);
269 }
270
271
272 /* Translate an ELF section header table entry in external format into an
273    ELF section header table entry in internal format. */
274
275 static void
276 elf_swap_shdr_in (abfd, src, dst)
277      bfd *abfd;
278      Elf_External_Shdr *src;
279      Elf_Internal_Shdr *dst;
280 {
281   dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name);
282   dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type);
283   dst->sh_flags = get_word (abfd, (bfd_byte *) src->sh_flags);
284   dst->sh_addr = get_word (abfd, (bfd_byte *) src->sh_addr);
285   dst->sh_offset = get_word (abfd, (bfd_byte *) src->sh_offset);
286   dst->sh_size = get_word (abfd, (bfd_byte *) src->sh_size);
287   dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link);
288   dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info);
289   dst->sh_addralign = get_word (abfd, (bfd_byte *) src->sh_addralign);
290   dst->sh_entsize = get_word (abfd, (bfd_byte *) src->sh_entsize);
291   /* we haven't done any processing on it yet, so... */
292   dst->rawdata = (void *) 0;
293 }
294
295 /* Translate an ELF section header table entry in internal format into an
296    ELF section header table entry in external format. */
297
298 static void
299 elf_swap_shdr_out (abfd, src, dst)
300      bfd *abfd;
301      Elf_Internal_Shdr *src;
302      Elf_External_Shdr *dst;
303 {
304   /* note that all elements of dst are *arrays of unsigned char* already... */
305   bfd_h_put_32 (abfd, src->sh_name, dst->sh_name);
306   bfd_h_put_32 (abfd, src->sh_type, dst->sh_type);
307   put_word (abfd, src->sh_flags, dst->sh_flags);
308   put_word (abfd, src->sh_addr, dst->sh_addr);
309   put_word (abfd, src->sh_offset, dst->sh_offset);
310   put_word (abfd, src->sh_size, dst->sh_size);
311   bfd_h_put_32 (abfd, src->sh_link, dst->sh_link);
312   bfd_h_put_32 (abfd, src->sh_info, dst->sh_info);
313   put_word (abfd, src->sh_addralign, dst->sh_addralign);
314   put_word (abfd, src->sh_entsize, dst->sh_entsize);
315 }
316
317
318 /* Translate an ELF program header table entry in external format into an
319    ELF program header table entry in internal format. */
320
321 static void
322 elf_swap_phdr_in (abfd, src, dst)
323      bfd *abfd;
324      Elf_External_Phdr *src;
325      Elf_Internal_Phdr *dst;
326 {
327   dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type);
328   dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags);
329   dst->p_offset = get_word (abfd, (bfd_byte *) src->p_offset);
330   dst->p_vaddr = get_word (abfd, (bfd_byte *) src->p_vaddr);
331   dst->p_paddr = get_word (abfd, (bfd_byte *) src->p_paddr);
332   dst->p_filesz = get_word (abfd, (bfd_byte *) src->p_filesz);
333   dst->p_memsz = get_word (abfd, (bfd_byte *) src->p_memsz);
334   dst->p_align = get_word (abfd, (bfd_byte *) src->p_align);
335 }
336
337 static void
338 elf_swap_phdr_out (abfd, src, dst)
339      bfd *abfd;
340      Elf_Internal_Phdr *src;
341      Elf_External_Phdr *dst;
342 {
343   /* note that all elements of dst are *arrays of unsigned char* already... */
344   bfd_h_put_32 (abfd, src->p_type, dst->p_type);
345   put_word (abfd, src->p_offset, dst->p_offset);
346   put_word (abfd, src->p_vaddr, dst->p_vaddr);
347   put_word (abfd, src->p_paddr, dst->p_paddr);
348   put_word (abfd, src->p_filesz, dst->p_filesz);
349   put_word (abfd, src->p_memsz, dst->p_memsz);
350   bfd_h_put_32 (abfd, src->p_flags, dst->p_flags);
351   put_word (abfd, src->p_align, dst->p_align);
352 }
353
354 /* Translate an ELF reloc from external format to internal format. */
355 INLINE void
356 elf_swap_reloc_in (abfd, src, dst)
357      bfd *abfd;
358      Elf_External_Rel *src;
359      Elf_Internal_Rel *dst;
360 {
361   dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
362   dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
363 }
364
365 INLINE void
366 elf_swap_reloca_in (abfd, src, dst)
367      bfd *abfd;
368      Elf_External_Rela *src;
369      Elf_Internal_Rela *dst;
370 {
371   dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
372   dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
373   dst->r_addend = get_word (abfd, (bfd_byte *) src->r_addend);
374 }
375
376 /* Translate an ELF reloc from internal format to external format. */
377 INLINE void
378 elf_swap_reloc_out (abfd, src, dst)
379      bfd *abfd;
380      Elf_Internal_Rel *src;
381      Elf_External_Rel *dst;
382 {
383   put_word (abfd, src->r_offset, dst->r_offset);
384   put_word (abfd, src->r_info, dst->r_info);
385 }
386
387 INLINE void
388 elf_swap_reloca_out (abfd, src, dst)
389      bfd *abfd;
390      Elf_Internal_Rela *src;
391      Elf_External_Rela *dst;
392 {
393   put_word (abfd, src->r_offset, dst->r_offset);
394   put_word (abfd, src->r_info, dst->r_info);
395   put_word (abfd, src->r_addend, dst->r_addend);
396 }
397
398 INLINE void
399 elf_swap_dyn_in (abfd, src, dst)
400      bfd *abfd;
401      const Elf_External_Dyn *src;
402      Elf_Internal_Dyn *dst;
403 {
404   dst->d_tag = get_word (abfd, src->d_tag);
405   dst->d_un.d_val = get_word (abfd, src->d_un.d_val);
406 }
407
408 INLINE void
409 elf_swap_dyn_out (abfd, src, dst)
410      bfd *abfd;
411      const Elf_Internal_Dyn *src;
412      Elf_External_Dyn *dst;
413 {
414   put_word (abfd, src->d_tag, dst->d_tag);
415   put_word (abfd, src->d_un.d_val, dst->d_un.d_val);
416 }
417 \f
418 /* String table creation/manipulation routines */
419
420 static struct strtab *
421 bfd_new_strtab (abfd)
422      bfd *abfd;
423 {
424   struct strtab *ss;
425
426   ss = (struct strtab *) malloc (sizeof (struct strtab));
427   if (!ss)
428     {
429       bfd_set_error (bfd_error_no_memory);
430       return NULL;
431     }
432   ss->tab = malloc (1);
433   if (!ss->tab)
434     {
435       bfd_set_error (bfd_error_no_memory);
436       return NULL;
437     }
438   *ss->tab = 0;
439   ss->nentries = 0;
440   ss->length = 1;
441
442   return ss;
443 }
444
445 static unsigned long
446 bfd_add_to_strtab (abfd, ss, str)
447      bfd *abfd;
448      struct strtab *ss;
449      const char *str;
450 {
451   /* should search first, but for now: */
452   /* include the trailing NUL */
453   int ln = strlen (str) + 1;
454
455   /* FIXME: This is slow.  Also, we could combine this with the a.out
456      string table building and use a hash table, although it might not
457      be worth it since ELF symbols don't include debugging information
458      and thus have much less overlap.  */
459   ss->tab = realloc (ss->tab, ss->length + ln);
460   if (ss->tab == NULL)
461     {
462       bfd_set_error (bfd_error_no_memory);
463       return (unsigned long) -1;
464     }
465
466   strcpy (ss->tab + ss->length, str);
467   ss->nentries++;
468   ss->length += ln;
469
470   return ss->length - ln;
471 }
472
473 static int
474 bfd_add_2_to_strtab (abfd, ss, str, str2)
475      bfd *abfd;
476      struct strtab *ss;
477      char *str;
478      CONST char *str2;
479 {
480   /* should search first, but for now: */
481   /* include the trailing NUL */
482   int ln = strlen (str) + strlen (str2) + 1;
483
484   /* should this be using obstacks? */
485   if (ss->length)
486     ss->tab = realloc (ss->tab, ss->length + ln);
487   else
488     ss->tab = malloc (ln);
489
490   BFD_ASSERT (ss->tab != 0);    /* FIXME */
491   strcpy (ss->tab + ss->length, str);
492   strcpy (ss->tab + ss->length + strlen (str), str2);
493   ss->nentries++;
494   ss->length += ln;
495
496   return ss->length - ln;
497 }
498 \f
499 /* ELF .o/exec file reading */
500
501 /* Create a new bfd section from an ELF section header. */
502
503 static boolean
504 bfd_section_from_shdr (abfd, shindex)
505      bfd *abfd;
506      unsigned int shindex;
507 {
508   Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
509   Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
510   char *name;
511
512   name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
513
514   switch (hdr->sh_type)
515     {
516     case SHT_NULL:
517       /* Inactive section. Throw it away.  */
518       return true;
519
520     case SHT_PROGBITS:  /* Normal section with contents.  */
521     case SHT_DYNAMIC:   /* Dynamic linking information.  */
522     case SHT_NOBITS:    /* .bss section.  */
523       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
524
525     case SHT_SYMTAB:            /* A symbol table */
526       if (elf_onesymtab (abfd) == shindex)
527         return true;
528
529       BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym));
530       BFD_ASSERT (elf_onesymtab (abfd) == 0);
531       elf_onesymtab (abfd) = shindex;
532       elf_tdata (abfd)->symtab_hdr = *hdr;
533       elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_hdr;
534       abfd->flags |= HAS_SYMS;
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 == sizeof (Elf_External_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       return true;
548
549     case SHT_STRTAB:            /* A string table */
550       if (hdr->rawdata != NULL)
551         return true;
552       if (ehdr->e_shstrndx == shindex)
553         {
554           elf_tdata (abfd)->shstrtab_hdr = *hdr;
555           elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
556           hdr->rawdata = (PTR) & elf_tdata (abfd)->shstrtab_hdr;
557           return true;
558         }
559       {
560         unsigned int i;
561
562         for (i = 1; i < ehdr->e_shnum; i++)
563           {
564             Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
565             if (hdr2->sh_link == shindex)
566               {
567                 if (! bfd_section_from_shdr (abfd, i))
568                   return false;
569                 if (elf_onesymtab (abfd) == i)
570                   {
571                     elf_tdata (abfd)->strtab_hdr = *hdr;
572                     elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
573                     return true;
574                   }
575                 if (elf_dynsymtab (abfd) == i)
576                   {
577                     elf_tdata (abfd)->dynstrtab_hdr = *hdr;
578                     elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->dynstrtab_hdr;
579                     return true;
580                   }
581 #if 0 /* Not handling other string tables specially right now.  */
582                 hdr2 = elf_elfsections (abfd)[i];       /* in case it moved */
583                 /* We have a strtab for some random other section.  */
584                 newsect = (asection *) hdr2->rawdata;
585                 if (!newsect)
586                   break;
587                 hdr->rawdata = (PTR) newsect;
588                 hdr2 = &elf_section_data (newsect)->str_hdr;
589                 *hdr2 = *hdr;
590                 elf_elfsections (abfd)[shindex] = hdr2;
591 #endif
592               }
593           }
594       }
595
596       return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
597
598     case SHT_REL:
599     case SHT_RELA:
600       /* *These* do a lot of work -- but build no sections!  */
601       {
602         asection *target_sect;
603         Elf_Internal_Shdr *hdr2;
604         int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
605
606         /* Get the symbol table.  */
607         if (! bfd_section_from_shdr (abfd, hdr->sh_link))
608           return false;
609
610         /* If this reloc section does not use the main symbol table,
611            or if it is in the process image, we don't treat it as a
612            reloc section.  BFD can't adequately represent such a
613            section, so at least for now, we don't try.  We just
614            present it as a normal section.  */
615         if ((hdr->sh_flags & SHF_ALLOC) != 0
616             || hdr->sh_link != elf_onesymtab (abfd))
617           return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
618
619         /* Don't allow REL relocations on a machine that uses RELA and
620            vice versa.  */
621         /* @@ Actually, the generic ABI does suggest that both might be
622            used in one file.  But the four ABI Processor Supplements I
623            have access to right now all specify that only one is used on
624            each of those architectures.  It's conceivable that, e.g., a
625            bunch of absolute 32-bit relocs might be more compact in REL
626            form even on a RELA machine...  */
627         BFD_ASSERT (use_rela_p
628                     ? (hdr->sh_type == SHT_RELA
629                        && hdr->sh_entsize == sizeof (Elf_External_Rela))
630                     : (hdr->sh_type == SHT_REL
631                        && hdr->sh_entsize == sizeof (Elf_External_Rel)));
632
633         if (! bfd_section_from_shdr (abfd, hdr->sh_info))
634           return false;
635         target_sect = section_from_elf_index (abfd, hdr->sh_info);
636         if (target_sect == NULL
637             || elf_section_data (target_sect) == NULL)
638           return false;
639
640         hdr2 = &elf_section_data (target_sect)->rel_hdr;
641         *hdr2 = *hdr;
642         elf_elfsections (abfd)[shindex] = hdr2;
643         target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
644         target_sect->flags |= SEC_RELOC;
645         target_sect->relocation = NULL;
646         target_sect->rel_filepos = hdr->sh_offset;
647         abfd->flags |= HAS_RELOC;
648         return true;
649       }
650       break;
651
652     case SHT_HASH:
653 #if 0
654       fprintf (stderr, "Dynamic Linking sections not yet supported.\n");
655       BFD_FAIL ();
656 #endif
657       break;
658
659     case SHT_NOTE:
660 #if 0
661       fprintf (stderr, "Note Sections not yet supported.\n");
662       BFD_FAIL ();
663 #endif
664       break;
665
666     case SHT_SHLIB:
667 #if 0
668       fprintf (stderr, "SHLIB Sections not supported (and non conforming.)\n");
669 #endif
670       return true;
671
672     default:
673       /* Check for any processor-specific section types.  */
674       {
675         struct elf_backend_data *bed = get_elf_backend_data (abfd);
676
677         if (bed->elf_backend_section_from_shdr)
678           (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
679       }
680       break;
681     }
682
683   return true;
684 }
685
686 boolean
687 elf_new_section_hook (abfd, sec)
688      bfd *abfd
689       ;
690      asection *sec;
691 {
692   struct bfd_elf_section_data *sdata;
693
694   sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
695   if (!sdata)
696     {
697       bfd_set_error (bfd_error_no_memory);
698       return false;
699     }
700   sec->used_by_bfd = (PTR) sdata;
701   memset (sdata, 0, sizeof (*sdata));
702   return true;
703 }
704
705 /* Create a new bfd section from an ELF program header.
706
707    Since program segments have no names, we generate a synthetic name
708    of the form segment<NUM>, where NUM is generally the index in the
709    program header table.  For segments that are split (see below) we
710    generate the names segment<NUM>a and segment<NUM>b.
711
712    Note that some program segments may have a file size that is different than
713    (less than) the memory size.  All this means is that at execution the
714    system must allocate the amount of memory specified by the memory size,
715    but only initialize it with the first "file size" bytes read from the
716    file.  This would occur for example, with program segments consisting
717    of combined data+bss.
718
719    To handle the above situation, this routine generates TWO bfd sections
720    for the single program segment.  The first has the length specified by
721    the file size of the segment, and the second has the length specified
722    by the difference between the two sizes.  In effect, the segment is split
723    into it's initialized and uninitialized parts.
724
725  */
726
727 static boolean
728 bfd_section_from_phdr (abfd, hdr, index)
729      bfd *abfd;
730      Elf_Internal_Phdr *hdr;
731      int index;
732 {
733   asection *newsect;
734   char *name;
735   char namebuf[64];
736   int split;
737
738   split = ((hdr->p_memsz > 0) &&
739            (hdr->p_filesz > 0) &&
740            (hdr->p_memsz > hdr->p_filesz));
741   sprintf (namebuf, split ? "segment%da" : "segment%d", index);
742   name = bfd_alloc (abfd, strlen (namebuf) + 1);
743   if (!name)
744     {
745       bfd_set_error (bfd_error_no_memory);
746       return false;
747     }
748   strcpy (name, namebuf);
749   newsect = bfd_make_section (abfd, name);
750   if (newsect == NULL)
751     return false;
752   newsect->vma = hdr->p_vaddr;
753   newsect->_raw_size = hdr->p_filesz;
754   newsect->filepos = hdr->p_offset;
755   newsect->flags |= SEC_HAS_CONTENTS;
756   if (hdr->p_type == PT_LOAD)
757     {
758       newsect->flags |= SEC_ALLOC;
759       newsect->flags |= SEC_LOAD;
760       if (hdr->p_flags & PF_X)
761         {
762           /* FIXME: all we known is that it has execute PERMISSION,
763              may be data. */
764           newsect->flags |= SEC_CODE;
765         }
766     }
767   if (!(hdr->p_flags & PF_W))
768     {
769       newsect->flags |= SEC_READONLY;
770     }
771
772   if (split)
773     {
774       sprintf (namebuf, "segment%db", index);
775       name = bfd_alloc (abfd, strlen (namebuf) + 1);
776       if (!name)
777         {
778           bfd_set_error (bfd_error_no_memory);
779           return false;
780         }
781       strcpy (name, namebuf);
782       newsect = bfd_make_section (abfd, name);
783       if (newsect == NULL)
784         return false;
785       newsect->vma = hdr->p_vaddr + hdr->p_filesz;
786       newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
787       if (hdr->p_type == PT_LOAD)
788         {
789           newsect->flags |= SEC_ALLOC;
790           if (hdr->p_flags & PF_X)
791             newsect->flags |= SEC_CODE;
792         }
793       if (!(hdr->p_flags & PF_W))
794         newsect->flags |= SEC_READONLY;
795     }
796
797   return true;
798 }
799
800 /* Begin processing a given object.
801
802    First we validate the file by reading in the ELF header and checking
803    the magic number.  */
804
805 static INLINE boolean
806 elf_file_p (x_ehdrp)
807      Elf_External_Ehdr *x_ehdrp;
808 {
809   return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
810           && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
811           && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
812           && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
813 }
814
815 /* Check to see if the file associated with ABFD matches the target vector
816    that ABFD points to.
817
818    Note that we may be called several times with the same ABFD, but different
819    target vectors, most of which will not match.  We have to avoid leaving
820    any side effects in ABFD, or any data it points to (like tdata), if the
821    file does not match the target vector.  */
822
823 bfd_target *
824 elf_object_p (abfd)
825      bfd *abfd;
826 {
827   Elf_External_Ehdr x_ehdr;     /* Elf file header, external form */
828   Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */
829   Elf_External_Shdr x_shdr;     /* Section header table entry, external form */
830   Elf_Internal_Shdr *i_shdrp = NULL; /* Section header table, internal form */
831   unsigned int shindex;
832   char *shstrtab;               /* Internal copy of section header stringtab */
833   struct elf_backend_data *ebd;
834   struct elf_obj_tdata *preserved_tdata = elf_tdata (abfd);
835   struct elf_obj_tdata *new_tdata = NULL;
836
837   /* Read in the ELF header in external format.  */
838
839   if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
840     {
841       if (bfd_get_error () != bfd_error_system_call)
842         goto got_wrong_format_error;
843       else
844         goto got_no_match;
845     }
846
847   /* Now check to see if we have a valid ELF file, and one that BFD can
848      make use of.  The magic number must match, the address size ('class')
849      and byte-swapping must match our XVEC entry, and it must have a
850      section header table (FIXME: See comments re sections at top of this
851      file). */
852
853   if ((elf_file_p (&x_ehdr) == false) ||
854       (x_ehdr.e_ident[EI_VERSION] != EV_CURRENT) ||
855       (x_ehdr.e_ident[EI_CLASS] != ELFCLASS))
856     goto got_wrong_format_error;
857
858   /* Check that file's byte order matches xvec's */
859   switch (x_ehdr.e_ident[EI_DATA])
860     {
861     case ELFDATA2MSB:           /* Big-endian */
862       if (!abfd->xvec->header_byteorder_big_p)
863         goto got_wrong_format_error;
864       break;
865     case ELFDATA2LSB:           /* Little-endian */
866       if (abfd->xvec->header_byteorder_big_p)
867         goto got_wrong_format_error;
868       break;
869     case ELFDATANONE:           /* No data encoding specified */
870     default:                    /* Unknown data encoding specified */
871       goto got_wrong_format_error;
872     }
873
874   /* Allocate an instance of the elf_obj_tdata structure and hook it up to
875      the tdata pointer in the bfd.  */
876
877   new_tdata = ((struct elf_obj_tdata *)
878                bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)));
879   if (new_tdata == NULL)
880     goto got_no_memory_error;
881   elf_tdata (abfd) = new_tdata;
882
883   /* Now that we know the byte order, swap in the rest of the header */
884   i_ehdrp = elf_elfheader (abfd);
885   elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
886 #if DEBUG & 1
887   elf_debug_file (i_ehdrp);
888 #endif
889
890   /* If there is no section header table, we're hosed. */
891   if (i_ehdrp->e_shoff == 0)
892     goto got_wrong_format_error;
893
894   /* As a simple sanity check, verify that the what BFD thinks is the
895      size of each section header table entry actually matches the size
896      recorded in the file. */
897   if (i_ehdrp->e_shentsize != sizeof (x_shdr))
898     goto got_wrong_format_error;
899
900   ebd = get_elf_backend_data (abfd);
901
902   /* Check that the ELF e_machine field matches what this particular
903      BFD format expects.  */
904   if (ebd->elf_machine_code != i_ehdrp->e_machine)
905     {
906       bfd_target **target_ptr;
907
908       if (ebd->elf_machine_code != EM_NONE)
909         goto got_wrong_format_error;
910
911       /* This is the generic ELF target.  Let it match any ELF target
912          for which we do not have a specific backend.  */
913       for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
914         {
915           struct elf_backend_data *back;
916
917           if ((*target_ptr)->flavour != bfd_target_elf_flavour)
918             continue;
919           back = (struct elf_backend_data *) (*target_ptr)->backend_data;
920           if (back->elf_machine_code == i_ehdrp->e_machine)
921             {
922               /* target_ptr is an ELF backend which matches this
923                  object file, so reject the generic ELF target.  */
924               goto got_wrong_format_error;
925             }
926         }
927     }
928
929   /* Set the flags and architecture before calling the backend so that
930      it can override them.  */
931   if (i_ehdrp->e_type == ET_EXEC)
932     abfd->flags |= EXEC_P;
933   else if (i_ehdrp->e_type == ET_DYN)
934     abfd->flags |= DYNAMIC;
935
936   if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
937     goto got_no_match;
938
939   /* Remember the entry point specified in the ELF file header. */
940   bfd_get_start_address (abfd) = i_ehdrp->e_entry;
941
942   /* Let the backend double check the format and override global
943      information.  */
944   if (ebd->elf_backend_object_p)
945     {
946       if ((*ebd->elf_backend_object_p) (abfd) == false)
947         goto got_wrong_format_error;
948     }
949
950   /* Allocate space for a copy of the section header table in
951      internal form, seek to the section header table in the file,
952      read it in, and convert it to internal form.  */
953   i_shdrp = ((Elf_Internal_Shdr *)
954              bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum));
955   elf_elfsections (abfd) = ((Elf_Internal_Shdr **)
956                             bfd_alloc (abfd,
957                                        sizeof (i_shdrp) * i_ehdrp->e_shnum));
958   if (!i_shdrp || !elf_elfsections (abfd))
959     goto got_no_memory_error;
960   if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0)
961     goto got_no_match;
962   for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++)
963     {
964       if (bfd_read ((PTR) & x_shdr, sizeof x_shdr, 1, abfd) != sizeof (x_shdr))
965         goto got_no_match;
966       elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
967       elf_elfsections (abfd)[shindex] = i_shdrp + shindex;
968
969       /* If this is a .dynamic section, mark the object file as being
970          dynamically linked.  */
971       if (i_shdrp[shindex].sh_type == SHT_DYNAMIC)
972         abfd->flags |= DYNAMIC;
973     }
974   if (i_ehdrp->e_shstrndx)
975     {
976       if (! bfd_section_from_shdr (abfd, i_ehdrp->e_shstrndx))
977         goto got_no_match;
978     }
979
980   /* Read in the string table containing the names of the sections.  We
981      will need the base pointer to this table later. */
982   /* We read this inline now, so that we don't have to go through
983      bfd_section_from_shdr with it (since this particular strtab is
984      used to find all of the ELF section names.) */
985
986   shstrtab = elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
987   if (!shstrtab)
988     goto got_no_match;
989
990   /* Once all of the section headers have been read and converted, we
991      can start processing them.  Note that the first section header is
992      a dummy placeholder entry, so we ignore it.  */
993
994   for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
995     {
996       if (! bfd_section_from_shdr (abfd, shindex))
997         goto got_no_match;
998     }
999
1000   return (abfd->xvec);
1001
1002 got_wrong_format_error:
1003   bfd_set_error (bfd_error_wrong_format);
1004   goto got_no_match;
1005 got_no_memory_error:
1006   bfd_set_error (bfd_error_no_memory);
1007   goto got_no_match;
1008 got_no_match:
1009   if (new_tdata != NULL
1010       && new_tdata->elf_sect_ptr != NULL)
1011     bfd_release (abfd, new_tdata->elf_sect_ptr);
1012   if (i_shdrp != NULL)
1013     bfd_release (abfd, i_shdrp);
1014   if (new_tdata != NULL)
1015     bfd_release (abfd, new_tdata);
1016   elf_tdata (abfd) = preserved_tdata;
1017   return (NULL);
1018 }
1019 \f
1020
1021 /* ELF .o/exec file writing */
1022
1023 /* Takes a bfd and a symbol, returns a pointer to the elf specific area
1024    of the symbol if there is one.  */
1025 static INLINE elf_symbol_type *
1026 elf_symbol_from (ignore_abfd, symbol)
1027      bfd *ignore_abfd;
1028      asymbol *symbol;
1029 {
1030   if (symbol->the_bfd->xvec->flavour != bfd_target_elf_flavour)
1031     return 0;
1032
1033   if (symbol->the_bfd->tdata.elf_obj_data == (struct elf_obj_tdata *) NULL)
1034     return 0;
1035
1036   return (elf_symbol_type *) symbol;
1037 }
1038
1039 /* Create ELF output from BFD sections.
1040
1041    Essentially, just create the section header and forget about the program
1042    header for now.  */
1043
1044 static void
1045 elf_make_sections (abfd, asect, obj)
1046      bfd *abfd;
1047      asection *asect;
1048      PTR obj;
1049 {
1050   /* most of what is in bfd_shdr_from_section goes in here... */
1051   /* and all of these sections generate at *least* one ELF section. */
1052   Elf_Internal_Shdr *this_hdr;
1053   this_hdr = &elf_section_data (asect)->this_hdr;
1054
1055   this_hdr->sh_addr = asect->vma;
1056   this_hdr->sh_size = asect->_raw_size;
1057   /* contents already set by elf_set_section_contents */
1058
1059   if (asect->flags & SEC_RELOC)
1060     {
1061       /* emit a reloc section, and thus strtab and symtab... */
1062       Elf_Internal_Shdr *rela_hdr;
1063       int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1064
1065       rela_hdr = &elf_section_data (asect)->rel_hdr;
1066
1067       /* orelocation has the data, reloc_count has the count... */
1068       if (use_rela_p)
1069         {
1070           rela_hdr->sh_type = SHT_RELA;
1071           rela_hdr->sh_entsize = sizeof (Elf_External_Rela);
1072         }
1073       else
1074         /* REL relocations */
1075         {
1076           rela_hdr->sh_type = SHT_REL;
1077           rela_hdr->sh_entsize = sizeof (Elf_External_Rel);
1078         }
1079       rela_hdr->sh_flags = 0;
1080       rela_hdr->sh_addr = 0;
1081       rela_hdr->sh_offset = 0;
1082
1083       /* FIXME: Systems I've checked use an alignment of 4, but it is
1084          possible that some systems use a different alignment.  */
1085       rela_hdr->sh_addralign = 4;
1086
1087       rela_hdr->size = 0;
1088     }
1089   if (asect->flags & SEC_ALLOC)
1090     {
1091       this_hdr->sh_flags |= SHF_ALLOC;
1092       if (asect->flags & SEC_LOAD)
1093         {
1094           /* @@ Do something with sh_type? */
1095         }
1096     }
1097   else
1098     {
1099       /* If this section is not part of the program image during
1100          execution, leave the address fields at 0.  */
1101       this_hdr->sh_addr = 0;
1102       asect->vma = 0;
1103     }
1104   if (!(asect->flags & SEC_READONLY))
1105     this_hdr->sh_flags |= SHF_WRITE;
1106
1107   if (asect->flags & SEC_CODE)
1108     this_hdr->sh_flags |= SHF_EXECINSTR;
1109 }
1110
1111 void
1112 write_relocs (abfd, sec, xxx)
1113      bfd *abfd;
1114      asection *sec;
1115      PTR xxx;
1116 {
1117   Elf_Internal_Shdr *rela_hdr;
1118   Elf_External_Rela *outbound_relocas;
1119   Elf_External_Rel *outbound_relocs;
1120   int idx;
1121   int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1122   asymbol *last_sym = 0;
1123   int last_sym_idx = 9999999;   /* should always be written before use */
1124
1125   if ((sec->flags & SEC_RELOC) == 0)
1126     return;
1127
1128   /* The linker backend writes the relocs out itself, and sets the
1129      reloc_count field to zero to inhibit writing them here.  Also,
1130      sometimes the SEC_RELOC flag gets set even when there aren't any
1131      relocs.  */
1132   if (sec->reloc_count == 0)
1133     return;
1134
1135   rela_hdr = &elf_section_data (sec)->rel_hdr;
1136
1137   rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
1138   rela_hdr->contents = (void *) bfd_alloc (abfd, rela_hdr->sh_size);
1139   if (!rela_hdr->contents)
1140     {
1141       bfd_set_error (bfd_error_no_memory);
1142       abort ();                 /* FIXME */
1143     }
1144
1145   /* orelocation has the data, reloc_count has the count... */
1146   if (use_rela_p)
1147     {
1148       outbound_relocas = (Elf_External_Rela *) rela_hdr->contents;
1149
1150       for (idx = 0; idx < sec->reloc_count; idx++)
1151         {
1152           Elf_Internal_Rela dst_rela;
1153           Elf_External_Rela *src_rela;
1154           arelent *ptr;
1155           asymbol *sym;
1156           int n;
1157
1158           ptr = sec->orelocation[idx];
1159           src_rela = outbound_relocas + idx;
1160           if (!(abfd->flags & EXEC_P))
1161             dst_rela.r_offset = ptr->address - sec->vma;
1162           else
1163             dst_rela.r_offset = ptr->address;
1164
1165           sym = *ptr->sym_ptr_ptr;
1166           if (sym == last_sym)
1167             n = last_sym_idx;
1168           else
1169             {
1170               last_sym = sym;
1171               last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
1172             }
1173           dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
1174
1175           dst_rela.r_addend = ptr->addend;
1176           elf_swap_reloca_out (abfd, &dst_rela, src_rela);
1177         }
1178     }
1179   else
1180     /* REL relocations */
1181     {
1182       outbound_relocs = (Elf_External_Rel *) rela_hdr->contents;
1183
1184       for (idx = 0; idx < sec->reloc_count; idx++)
1185         {
1186           Elf_Internal_Rel dst_rel;
1187           Elf_External_Rel *src_rel;
1188           arelent *ptr;
1189           int n;
1190           asymbol *sym;
1191
1192           ptr = sec->orelocation[idx];
1193           sym = *ptr->sym_ptr_ptr;
1194           src_rel = outbound_relocs + idx;
1195           if (!(abfd->flags & EXEC_P))
1196             dst_rel.r_offset = ptr->address - sec->vma;
1197           else
1198             dst_rel.r_offset = ptr->address;
1199
1200           if (sym == last_sym)
1201             n = last_sym_idx;
1202           else
1203             {
1204               last_sym = sym;
1205               last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
1206             }
1207           dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type);
1208
1209           elf_swap_reloc_out (abfd, &dst_rel, src_rel);
1210         }
1211     }
1212 }
1213
1214 static void
1215 fix_up_strtabs (abfd, asect, obj)
1216      bfd *abfd;
1217      asection *asect;
1218      PTR obj;
1219 {
1220   Elf_Internal_Shdr *this_hdr = &elf_section_data (asect)->this_hdr;
1221   int this_idx = elf_section_data (asect)->this_idx;
1222
1223   /* @@ Check flags!  */
1224   if (!strncmp (asect->name, ".stab", 5)
1225       && !strcmp ("str", asect->name + strlen (asect->name) - 3))
1226     {
1227       size_t len = strlen (asect->name) + 1;
1228       char *s = (char *) malloc (len);
1229       if (s == NULL)
1230         /* FIXME: Should deal more gracefully with errors.  */
1231         abort ();
1232       strcpy (s, asect->name);
1233       s[len - 4] = 0;
1234       asect = bfd_get_section_by_name (abfd, s);
1235       free (s);
1236       if (!asect)
1237         abort ();
1238       elf_section_data (asect)->this_hdr.sh_link = this_idx;
1239       /* @@ Assuming 32 bits!  */
1240       elf_section_data (asect)->this_hdr.sh_entsize = 0xc;
1241
1242       this_hdr->sh_type = SHT_STRTAB;
1243     }
1244 }
1245
1246 static void
1247 elf_fake_sections (abfd, asect, obj)
1248      bfd *abfd;
1249      asection *asect;
1250      PTR obj;
1251 {
1252   /* most of what is in bfd_shdr_from_section goes in here... */
1253   /* and all of these sections generate at *least* one ELF section. */
1254
1255   Elf_Internal_Shdr *this_hdr;
1256   this_hdr = &elf_section_data (asect)->this_hdr;
1257   this_hdr->sh_name =
1258     bfd_add_to_strtab (abfd, elf_shstrtab (abfd), asect->name);
1259   if (this_hdr->sh_name == (unsigned long) -1)
1260     abort (); /* FIXME */
1261   /* We need to log the type *now* so that elf_section_from_bfd_section
1262      can find us... have to set rawdata too. */
1263   this_hdr->rawdata = (void *) asect;
1264   this_hdr->sh_addralign = 1 << asect->alignment_power;
1265
1266   /* FIXME: This should not be based on section names.  */
1267   if (strcmp (asect->name, ".dynstr") == 0)
1268     this_hdr->sh_type = SHT_STRTAB;
1269   else if (strcmp (asect->name, ".hash") == 0)
1270     this_hdr->sh_type = SHT_HASH;
1271   else if (strcmp (asect->name, ".dynsym") == 0)
1272     this_hdr->sh_type = SHT_DYNSYM;
1273   else if (strcmp (asect->name, ".dynamic") == 0)
1274     this_hdr->sh_type = SHT_DYNAMIC;
1275   else if (strncmp (asect->name, ".rel.", 5) == 0)
1276     this_hdr->sh_type = SHT_REL;
1277   else if (strncmp (asect->name, ".rela.", 6) == 0)
1278     this_hdr->sh_type = SHT_RELA;
1279   else if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD))
1280     this_hdr->sh_type = SHT_PROGBITS;
1281   else if ((asect->flags & SEC_ALLOC) && ((asect->flags & SEC_LOAD) == 0))
1282     {
1283       BFD_ASSERT (strcmp (asect->name, ".bss") == 0
1284                   || strcmp (asect->name, ".sbss") == 0);
1285       this_hdr->sh_type = SHT_NOBITS;
1286     }
1287   /* FIXME I am not sure how to detect a .note section from the flags
1288      word of an `asection'.  */
1289   else if (!strcmp (asect->name, ".note"))
1290     this_hdr->sh_type = SHT_NOTE;
1291   else
1292     this_hdr->sh_type = SHT_PROGBITS;
1293
1294   this_hdr->sh_flags = 0;
1295   this_hdr->sh_addr = 0;
1296   this_hdr->sh_size = 0;
1297   this_hdr->sh_entsize = 0;
1298   this_hdr->sh_info = 0;
1299   this_hdr->sh_link = 0;
1300   this_hdr->sh_offset = 0;
1301   this_hdr->size = 0;
1302
1303   /* Now, check for processor-specific section types.  */
1304   {
1305     struct elf_backend_data *bed = get_elf_backend_data (abfd);
1306
1307     if (bed->elf_backend_fake_sections)
1308       (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1309   }
1310
1311   {
1312     /* Emit a strtab and symtab, and possibly a reloc section.  */
1313     Elf_Internal_Shdr *rela_hdr;
1314
1315     /* Note that only one symtab is used, so just remember it
1316        for now.  */
1317
1318     if (asect->flags & SEC_RELOC)
1319       {
1320         int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1321
1322         rela_hdr = &elf_section_data (asect)->rel_hdr;
1323         rela_hdr->sh_name =
1324           bfd_add_2_to_strtab (abfd, elf_shstrtab (abfd),
1325                                use_rela_p ? ".rela" : ".rel",
1326                                asect->name);
1327         rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1328         rela_hdr->sh_entsize = (use_rela_p
1329                                 ? sizeof (Elf_External_Rela)
1330                                 : sizeof (Elf_External_Rel));
1331
1332         rela_hdr->sh_flags = 0;
1333         rela_hdr->sh_addr = 0;
1334         rela_hdr->sh_size = 0;
1335         rela_hdr->sh_offset = 0;
1336
1337         /* FIXME: Systems I've checked use an alignment of 4, but some
1338            systems may use a different alignment.  */
1339         rela_hdr->sh_addralign = 4;
1340
1341         rela_hdr->size = 0;
1342       }
1343   }
1344   if (asect->flags & SEC_ALLOC)
1345     {
1346       this_hdr->sh_flags |= SHF_ALLOC;
1347       if (asect->flags & SEC_LOAD)
1348         {
1349           /* @@ Do something with sh_type?  */
1350         }
1351     }
1352   if (!(asect->flags & SEC_READONLY))
1353     this_hdr->sh_flags |= SHF_WRITE;
1354   if (asect->flags & SEC_CODE)
1355     this_hdr->sh_flags |= SHF_EXECINSTR;
1356 }
1357
1358 /* Map symbol from it's internal number to the external number, moving
1359    all local symbols to be at the head of the list.  */
1360
1361 static INLINE int
1362 sym_is_global (abfd, sym)
1363      bfd *abfd;
1364      asymbol *sym;
1365 {
1366   /* If the backend has a special mapping, use it.  */
1367   if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1368     return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1369             (abfd, sym));
1370
1371   if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
1372     {
1373       if (sym->flags & BSF_LOCAL)
1374         abort ();
1375       return 1;
1376     }
1377   if (sym->section == 0)
1378     {
1379       /* Is this valid?  */
1380       abort ();
1381
1382       return 1;
1383     }
1384   if (sym->section == &bfd_und_section)
1385     return 1;
1386   if (bfd_is_com_section (sym->section))
1387     return 1;
1388   if (sym->flags & (BSF_LOCAL | BSF_SECTION_SYM | BSF_FILE))
1389     return 0;
1390   return 0;
1391 }
1392
1393 static boolean
1394 elf_map_symbols (abfd)
1395      bfd *abfd;
1396 {
1397   int symcount = bfd_get_symcount (abfd);
1398   asymbol **syms = bfd_get_outsymbols (abfd);
1399   asymbol **sect_syms;
1400   int num_locals = 0;
1401   int num_globals = 0;
1402   int num_locals2 = 0;
1403   int num_globals2 = 0;
1404   int max_index = 0;
1405   int num_sections = 0;
1406   Elf_Sym_Extra *sym_extra;
1407   int idx;
1408   asection *asect;
1409
1410 #ifdef DEBUG
1411   fprintf (stderr, "elf_map_symbols\n");
1412   fflush (stderr);
1413 #endif
1414
1415   /* Add local symbols for each section for which there are relocs.
1416      FIXME: How can we tell which sections have relocs at this point?
1417      Will reloc_count always be accurate?  Actually, I think most ELF
1418      targets create section symbols for all sections anyhow.  */
1419   for (asect = abfd->sections; asect; asect = asect->next)
1420     {
1421       if (max_index < asect->index)
1422         max_index = asect->index;
1423     }
1424
1425   max_index++;
1426   elf_num_section_syms (abfd) = max_index;
1427   sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1428   elf_section_syms (abfd) = sect_syms;
1429
1430   if (sect_syms == 0)
1431     {
1432       bfd_set_error (bfd_error_no_memory);
1433       return false;
1434     }
1435
1436   for (asect = abfd->sections; asect; asect = asect->next)
1437     {
1438       asymbol *sym = bfd_make_empty_symbol (abfd);
1439       if (!sym)
1440         {
1441           bfd_set_error (bfd_error_no_memory);
1442           return false;
1443         }
1444       sym->the_bfd = abfd;
1445       sym->name = asect->name;
1446       sym->value = asect->vma;
1447       sym->flags = BSF_SECTION_SYM;
1448       sym->section = asect;
1449       sect_syms[asect->index] = sym;
1450       num_sections++;
1451 #ifdef DEBUG
1452       fprintf (stderr,
1453                "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1454                asect->name, (long) asect->vma, asect->index, (long) asect);
1455 #endif
1456     }
1457
1458   if (num_sections)
1459     {
1460       if (syms)
1461         syms = (asymbol **) bfd_realloc (abfd, syms,
1462                                          ((symcount + num_sections + 1)
1463                                           * sizeof (asymbol *)));
1464       else
1465         syms = (asymbol **) bfd_alloc (abfd,
1466                                    (num_sections + 1) * sizeof (asymbol *));
1467       if (!syms)
1468         {
1469           bfd_set_error (bfd_error_no_memory);
1470           return false;
1471         }
1472
1473       for (asect = abfd->sections; asect; asect = asect->next)
1474         {
1475           if (sect_syms[asect->index])
1476             syms[symcount++] = sect_syms[asect->index];
1477         }
1478
1479       syms[symcount] = (asymbol *) 0;
1480       bfd_set_symtab (abfd, syms, symcount);
1481     }
1482
1483   elf_sym_extra (abfd) = sym_extra
1484     = (Elf_Sym_Extra *) bfd_alloc (abfd, symcount * sizeof (Elf_Sym_Extra));
1485   if (!sym_extra)
1486     {
1487       bfd_set_error (bfd_error_no_memory);
1488       return false;
1489     }
1490
1491   /* Identify and classify all of the symbols.  */
1492   for (idx = 0; idx < symcount; idx++)
1493     {
1494       if (!sym_is_global (abfd, syms[idx]))
1495         num_locals++;
1496       else
1497         num_globals++;
1498     }
1499
1500   /* Now provide mapping information.  Add +1 for skipping over the
1501      dummy symbol.  */
1502   for (idx = 0; idx < symcount; idx++)
1503     {
1504       syms[idx]->udata = (PTR) & sym_extra[idx];
1505       if (!sym_is_global (abfd, syms[idx]))
1506         sym_extra[idx].elf_sym_num = 1 + num_locals2++;
1507       else
1508         sym_extra[idx].elf_sym_num = 1 + num_locals + num_globals2++;
1509     }
1510
1511   elf_num_locals (abfd) = num_locals;
1512   elf_num_globals (abfd) = num_globals;
1513   return true;
1514 }
1515
1516 /* Compute the file positions we are going to put the sections at, and
1517    otherwise prepare to begin writing out the ELF file.  If LINK_INFO
1518    is not NULL, this is being called by the ELF backend linker.  */
1519
1520 static boolean
1521 elf_compute_section_file_positions (abfd, link_info)
1522      bfd *abfd;
1523      struct bfd_link_info *link_info;
1524 {
1525   struct elf_backend_data *bed = get_elf_backend_data (abfd);
1526   Elf_Internal_Shdr *shstrtab_hdr;
1527
1528   if (abfd->output_has_begun)
1529     return true;
1530
1531   /* Do any elf backend specific processing first.  */
1532   if (bed->elf_backend_begin_write_processing)
1533     (*bed->elf_backend_begin_write_processing) (abfd);
1534
1535   if (! prep_headers (abfd))
1536     return false;
1537
1538   bfd_map_over_sections (abfd, elf_fake_sections, 0);
1539
1540   if (!assign_section_numbers (abfd))
1541     return false;
1542
1543   bfd_map_over_sections (abfd, elf_make_sections, 0);
1544
1545   bfd_map_over_sections (abfd, fix_up_strtabs, 0);      /* .stab/.stabstr &c */
1546
1547   /* The backend linker builds symbol table information itself.  */
1548   if (link_info == NULL)
1549     {
1550       if (! swap_out_syms (abfd))
1551         return false;
1552     }
1553
1554   shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
1555   /* sh_name was set in prep_headers.  */
1556   shstrtab_hdr->sh_type = SHT_STRTAB;
1557   shstrtab_hdr->sh_flags = 0;
1558   shstrtab_hdr->sh_addr = 0;
1559   shstrtab_hdr->sh_size = elf_shstrtab (abfd)->length;
1560   shstrtab_hdr->sh_entsize = 0;
1561   shstrtab_hdr->sh_link = 0;
1562   shstrtab_hdr->sh_info = 0;
1563   /* sh_offset is set in assign_file_positions_for_symtabs_and_strtabs.  */
1564   shstrtab_hdr->sh_addralign = 1;
1565   shstrtab_hdr->contents = (PTR) elf_shstrtab (abfd)->tab;
1566
1567   if (!assign_file_positions_except_relocs (abfd,
1568                                             link_info == NULL ? true : false))
1569     return false;
1570
1571   abfd->output_has_begun = true;
1572
1573   return true;
1574 }
1575
1576
1577 /* Assign all ELF section numbers.  The dummy first section is handled here
1578    too.  The link/info pointers for the standard section types are filled
1579    in here too, while we're at it.  (Link pointers for .stab sections are
1580    not filled in here.)  */
1581 static boolean
1582 assign_section_numbers (abfd)
1583      bfd *abfd;
1584 {
1585   struct elf_obj_tdata *t = elf_tdata (abfd);
1586   asection *sec;
1587   int section_number = 1;
1588   int i;
1589   Elf_Internal_Shdr **i_shdrp;
1590
1591   t->shstrtab_hdr.sh_size = elf_shstrtab (abfd)->length;
1592   t->shstrtab_hdr.contents = (void *) elf_shstrtab (abfd)->tab;
1593
1594   t->shstrtab_section = section_number++;
1595   elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1596   if (abfd->symcount)
1597     {
1598       t->symtab_section = section_number++;
1599       t->strtab_section = section_number++;
1600       t->symtab_hdr.sh_link = t->strtab_section;
1601     }
1602   for (sec = abfd->sections; sec; sec = sec->next)
1603     {
1604       struct bfd_elf_section_data *d = elf_section_data (sec);
1605       d->this_idx = section_number++;
1606       if (sec->flags & SEC_RELOC)
1607         {
1608           d->rel_idx = section_number++;
1609           d->rel_hdr.sh_link = t->symtab_section;
1610           d->rel_hdr.sh_info = d->this_idx;
1611         }
1612       else
1613         d->rel_idx = 0;
1614       /* No handling for per-section string tables currently.  */
1615     }
1616   elf_elfheader (abfd)->e_shnum = section_number;
1617
1618   /* Set up the list of section header pointers, in agreement with the
1619      indices.  */
1620   i_shdrp = ((Elf_Internal_Shdr **)
1621              bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1622   if (i_shdrp == NULL)
1623     {
1624       bfd_set_error (bfd_error_no_memory);
1625       return false;
1626     }
1627   i_shdrp[0] = ((Elf_Internal_Shdr *)
1628                 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1629   if (i_shdrp[0] == NULL)
1630     {
1631       bfd_release (abfd, i_shdrp);
1632       bfd_set_error (bfd_error_no_memory);
1633       return false;
1634     }
1635   elf_elfsections (abfd) = i_shdrp;
1636   for (i = 1; i < section_number; i++)
1637     i_shdrp[i] = NULL;
1638   memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1639
1640   i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1641   if (abfd->symcount)
1642     {
1643       i_shdrp[t->symtab_section] = &t->symtab_hdr;
1644       i_shdrp[t->strtab_section] = &t->strtab_hdr;
1645     }
1646   for (sec = abfd->sections; sec; sec = sec->next)
1647     {
1648       struct bfd_elf_section_data *d = elf_section_data (sec);
1649       i_shdrp[d->this_idx] = &d->this_hdr;
1650       if (d->rel_idx)
1651         i_shdrp[d->rel_idx] = &d->rel_hdr;
1652     }
1653   /* Make sure we got everything.... */
1654   for (i = 0; i < section_number; i++)
1655     if (i_shdrp[i] == NULL)
1656       abort ();
1657   return true;
1658 }
1659
1660 /* Align to the maximum file alignment that could be required for any
1661    ELF data structure.  */
1662
1663 static INLINE file_ptr
1664 align_file_position (off)
1665      file_ptr off;
1666 {
1667   return (off + FILE_ALIGN - 1) & ~(FILE_ALIGN - 1);
1668 }
1669
1670 /* Assign a file position to a section, optionally aligning to the
1671    required section alignment.  */
1672
1673 static INLINE file_ptr
1674 assign_file_position_for_section (i_shdrp, offset, align)
1675      Elf_Internal_Shdr *i_shdrp;
1676      file_ptr offset;
1677      boolean align;
1678 {
1679   if (align)
1680     {
1681       unsigned int al;
1682
1683       al = i_shdrp->sh_addralign;
1684       if (al > 1)
1685         offset = BFD_ALIGN (offset, al);
1686     }
1687   i_shdrp->sh_offset = offset;
1688   if (i_shdrp->rawdata != NULL)
1689     ((asection *) i_shdrp->rawdata)->filepos = offset;
1690   if (i_shdrp->sh_type != SHT_NOBITS)
1691     offset += i_shdrp->sh_size;
1692   return offset;
1693 }
1694
1695 /* Get the size of the program header.  This is called by the linker
1696    before any of the section VMA's are set, so it can't calculate the
1697    correct value for a strange memory layout.  */
1698
1699 static bfd_size_type
1700 get_program_header_size (abfd)
1701      bfd *abfd;
1702 {
1703   size_t segs;
1704   asection *s;
1705
1706   /* Assume we will need exactly two PT_LOAD segments: one for text
1707      and one for data.  */
1708   segs = 2;
1709
1710   s = bfd_get_section_by_name (abfd, ".interp");
1711   if (s != NULL && (s->flags & SEC_LOAD) != 0)
1712     {
1713       /* If we have a loadable interpreter section, we need a
1714          PT_INTERP segment.  In this case, assume we also need a
1715          PT_PHDR segment, although that may not be true for all
1716          targets.  */
1717       segs += 2;
1718     }
1719
1720   if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
1721     {
1722       /* We need a PT_DYNAMIC segment.  */
1723       ++segs;
1724     }
1725
1726   return segs * sizeof (Elf_External_Phdr);
1727 }
1728
1729 /* Create the program header.  OFF is the file offset where the
1730    program header should be written.  FIRST is the first loadable ELF
1731    section.  PHDR_SIZE is the size of the program header as returned
1732    by get_program_header_size.  */
1733
1734 static file_ptr
1735 map_program_segments (abfd, off, first, phdr_size)
1736      bfd *abfd;
1737      file_ptr off;
1738      Elf_Internal_Shdr *first;
1739      bfd_size_type phdr_size;
1740 {
1741   Elf_Internal_Phdr phdrs[10];
1742   unsigned int phdr_count;
1743   Elf_Internal_Phdr *phdr;
1744   int phdr_size_adjust;
1745   unsigned int i;
1746   Elf_Internal_Shdr **hdrpp;
1747   asection *sinterp, *sdyn;
1748   unsigned int last_type;
1749   Elf_Internal_Ehdr *i_ehdrp;
1750
1751   BFD_ASSERT ((abfd->flags & EXEC_P) != 0);
1752   BFD_ASSERT (phdr_size / sizeof (Elf_Internal_Phdr)
1753               <= sizeof phdrs / sizeof (phdrs[0]));
1754
1755   phdr_count = 0;
1756   phdr = phdrs;
1757
1758   phdr_size_adjust = 0;
1759
1760   /* If we have a loadable .interp section, we must create a PT_INTERP
1761      segment which must precede all PT_LOAD segments.  We assume that
1762      we must also create a PT_PHDR segment, although that may not be
1763      true for all targets.  */
1764   sinterp = bfd_get_section_by_name (abfd, ".interp");
1765   if (sinterp != NULL && (sinterp->flags & SEC_LOAD) != 0)
1766     {
1767       BFD_ASSERT (first != NULL);
1768
1769       phdr->p_type = PT_PHDR;
1770
1771       phdr->p_offset = off;
1772
1773       /* Account for any adjustment made because of the alignment of
1774          the first loadable section.  */
1775       phdr_size_adjust = (first->sh_offset - phdr_size) - off;
1776       BFD_ASSERT (phdr_size_adjust >= 0 && phdr_size_adjust < 128);
1777
1778       /* The program header precedes all loadable sections.  This lets
1779          us compute its loadable address.  This depends on the linker
1780          script.  */
1781       phdr->p_vaddr = first->sh_addr - (phdr_size + phdr_size_adjust);
1782
1783       phdr->p_paddr = 0;
1784       phdr->p_filesz = phdr_size;
1785       phdr->p_memsz = phdr_size;
1786
1787       /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not.  */
1788       phdr->p_flags = PF_R | PF_X;
1789
1790       phdr->p_align = FILE_ALIGN;
1791       BFD_ASSERT ((phdr->p_vaddr - phdr->p_offset) % FILE_ALIGN == 0);
1792
1793       /* Include the ELF header in the first loadable segment.  */
1794       phdr_size_adjust += off;
1795
1796       ++phdr_count;
1797       ++phdr;
1798
1799       phdr->p_type = PT_INTERP;
1800       phdr->p_offset = sinterp->filepos;
1801       phdr->p_vaddr = sinterp->vma;
1802       phdr->p_paddr = 0;
1803       phdr->p_filesz = sinterp->_raw_size;
1804       phdr->p_memsz = sinterp->_raw_size;
1805       phdr->p_flags = PF_R;
1806       phdr->p_align = 1 << bfd_get_section_alignment (abfd, sinterp);
1807
1808       ++phdr_count;
1809       ++phdr;
1810     }
1811
1812   /* Look through the sections to see how they will be divided into
1813      program segments.  The sections must be arranged in order by
1814      sh_addr for this to work correctly.  */
1815   phdr->p_type = PT_NULL;
1816   last_type = SHT_PROGBITS;
1817   for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
1818        i < elf_elfheader (abfd)->e_shnum;
1819        i++, hdrpp++)
1820     {
1821       Elf_Internal_Shdr *hdr;
1822
1823       hdr = *hdrpp;
1824
1825       /* Ignore any section which will not be part of the process
1826          image.  */
1827       if ((hdr->sh_flags & SHF_ALLOC) == 0)
1828         continue;
1829
1830       /* If this section fits in the segment we are constructing, add
1831          it in.  */
1832       if (phdr->p_type != PT_NULL
1833           && (hdr->sh_offset - (phdr->p_offset + phdr->p_memsz)
1834               == hdr->sh_addr - (phdr->p_vaddr + phdr->p_memsz))
1835           && (last_type != SHT_NOBITS || hdr->sh_type == SHT_NOBITS))
1836         {
1837           bfd_size_type adjust;
1838
1839           adjust = hdr->sh_addr - (phdr->p_vaddr + phdr->p_memsz);
1840           phdr->p_memsz += hdr->sh_size + adjust;
1841           if (hdr->sh_type != SHT_NOBITS)
1842             phdr->p_filesz += hdr->sh_size + adjust;
1843           if ((hdr->sh_flags & SHF_WRITE) != 0)
1844             phdr->p_flags |= PF_W;
1845           if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1846             phdr->p_flags |= PF_X;
1847           last_type = hdr->sh_type;
1848           continue;
1849         }
1850
1851       /* If we have a segment, move to the next one.  */
1852       if (phdr->p_type != PT_NULL)
1853         {
1854           ++phdr;
1855           ++phdr_count;
1856         }
1857
1858       /* Start a new segment.  */
1859       phdr->p_type = PT_LOAD;
1860       phdr->p_offset = hdr->sh_offset;
1861       phdr->p_vaddr = hdr->sh_addr;
1862       phdr->p_paddr = 0;
1863       if (hdr->sh_type == SHT_NOBITS)
1864         phdr->p_filesz = 0;
1865       else
1866         phdr->p_filesz = hdr->sh_size;
1867       phdr->p_memsz = hdr->sh_size;
1868       phdr->p_flags = PF_R;
1869       if ((hdr->sh_flags & SHF_WRITE) != 0)
1870         phdr->p_flags |= PF_W;
1871       if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1872         phdr->p_flags |= PF_X;
1873       phdr->p_align = get_elf_backend_data (abfd)->maxpagesize;
1874
1875       if (hdr == first
1876           && sinterp != NULL
1877           && (sinterp->flags & SEC_LOAD) != 0)
1878         {
1879           phdr->p_offset -= phdr_size + phdr_size_adjust;
1880           phdr->p_vaddr -= phdr_size + phdr_size_adjust;
1881           phdr->p_filesz += phdr_size + phdr_size_adjust;
1882           phdr->p_memsz += phdr_size + phdr_size_adjust;
1883         }
1884
1885       last_type = hdr->sh_type;
1886     }
1887
1888   if (phdr->p_type != PT_NULL)
1889     {
1890       ++phdr;
1891       ++phdr_count;
1892     }
1893
1894   /* If we have a .dynamic section, create a PT_DYNAMIC segment.  */
1895   sdyn = bfd_get_section_by_name (abfd, ".dynamic");
1896   if (sdyn != NULL && (sdyn->flags & SEC_LOAD) != 0)
1897     {
1898       phdr->p_type = PT_DYNAMIC;
1899       phdr->p_offset = sdyn->filepos;
1900       phdr->p_vaddr = sdyn->vma;
1901       phdr->p_paddr = 0;
1902       phdr->p_filesz = sdyn->_raw_size;
1903       phdr->p_memsz = sdyn->_raw_size;
1904       phdr->p_flags = PF_R;
1905       if ((sdyn->flags & SEC_READONLY) == 0)
1906         phdr->p_flags |= PF_W;
1907       if ((sdyn->flags & SEC_CODE) != 0)
1908         phdr->p_flags |= PF_X;
1909       phdr->p_align = 1 << bfd_get_section_alignment (abfd, sdyn);
1910
1911       ++phdr;
1912       ++phdr_count;
1913     }
1914
1915   /* Make sure the return value from get_program_header_size matches
1916      what we computed here.  */
1917   if (phdr_count != phdr_size / sizeof (Elf_Internal_Phdr))
1918     abort ();
1919
1920   /* Set up program header information.  */
1921   i_ehdrp = elf_elfheader (abfd);
1922   i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
1923   i_ehdrp->e_phoff = off;
1924   i_ehdrp->e_phnum = phdr_count;
1925
1926   /* Save the program headers away.  I don't think anybody uses this
1927      information right now.  */
1928   elf_tdata (abfd)->phdr = ((Elf_Internal_Phdr *)
1929                             bfd_alloc (abfd,
1930                                        (phdr_count
1931                                         * sizeof (Elf_Internal_Phdr))));
1932   if (elf_tdata (abfd)->phdr == NULL && phdr_count != 0)
1933     {
1934       bfd_set_error (bfd_error_no_memory);
1935       return (file_ptr) -1;
1936     }
1937   memcpy (elf_tdata (abfd)->phdr, phdrs,
1938           phdr_count * sizeof (Elf_Internal_Phdr));
1939
1940   /* Write out the program headers.  */
1941   if (bfd_seek (abfd, off, SEEK_SET) != 0)
1942     return (file_ptr) -1;
1943
1944   for (i = 0, phdr = phdrs; i < phdr_count; i++, phdr++)
1945     {
1946       Elf_External_Phdr extphdr;
1947
1948       elf_swap_phdr_out (abfd, phdr, &extphdr);
1949       if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), 1, abfd)
1950           != sizeof (Elf_External_Phdr))
1951         return (file_ptr) -1;
1952     }
1953
1954   return off + phdr_count * sizeof (Elf_External_Phdr);
1955 }
1956
1957 /* Work out the file positions of all the sections.  This is called by
1958    elf_compute_section_file_positions.  All the section sizes and VMAs
1959    must be known before this is called.
1960
1961    We do not consider reloc sections at this point, unless they form
1962    part of the loadable image.  Reloc sections are assigned file
1963    positions in assign_file_positions_for_relocs, which is called by
1964    write_object_contents and final_link.
1965
1966    If DOSYMS is false, we do not assign file positions for the symbol
1967    table or the string table.  */
1968
1969 static boolean
1970 assign_file_positions_except_relocs (abfd, dosyms)
1971      bfd *abfd;
1972      boolean dosyms;
1973 {
1974   struct elf_obj_tdata * const tdata = elf_tdata (abfd);
1975   Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
1976   Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
1977   file_ptr off;
1978
1979   /* Start after the ELF header.  */
1980   off = i_ehdrp->e_ehsize;
1981
1982   if ((abfd->flags & EXEC_P) == 0)
1983     {
1984       Elf_Internal_Shdr **hdrpp;
1985       unsigned int i;
1986
1987       /* We are not creating an executable, which means that we are
1988          not creating a program header, and that the actual order of
1989          the sections in the file is unimportant.  */
1990       for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
1991         {
1992           Elf_Internal_Shdr *hdr;
1993
1994           hdr = *hdrpp;
1995           if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
1996             {
1997               hdr->sh_offset = -1;
1998               continue;
1999             }
2000           if (! dosyms
2001               && (i == tdata->symtab_section
2002                   || i == tdata->strtab_section))
2003             {
2004               hdr->sh_offset = -1;
2005               continue;
2006             }
2007           
2008           off = assign_file_position_for_section (hdr, off, true);
2009         }
2010     }
2011   else
2012     {
2013       file_ptr phdr_off;
2014       bfd_size_type phdr_size;
2015       bfd_vma maxpagesize;
2016       Elf_Internal_Shdr **hdrpp;
2017       unsigned int i;
2018       Elf_Internal_Shdr *first;
2019       file_ptr phdr_map;
2020
2021       /* We are creating an executable.  We must create a program
2022          header.  We can't actually create the program header until we
2023          have set the file positions for the sections, but we can
2024          figure out how big it is going to be.  */
2025       off = align_file_position (off);
2026       phdr_size = get_program_header_size (abfd);
2027       if (phdr_size == (file_ptr) -1)
2028         return false;
2029       phdr_off = off;
2030       off += phdr_size;
2031
2032       maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2033       if (maxpagesize == 0)
2034         maxpagesize = 1;
2035
2036       /* FIXME: We might want to sort the sections on the sh_addr
2037          field here.  For now, we just assume that the linker will
2038          create the sections in an appropriate order.  */
2039
2040       /* Assign file positions in two passes.  In the first pass, we
2041          assign a file position to every section which forms part of
2042          the executable image.  */
2043       first = NULL;
2044       for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2045         {
2046           Elf_Internal_Shdr *hdr;
2047
2048           hdr = *hdrpp;
2049           if ((hdr->sh_flags & SHF_ALLOC) == 0)
2050             continue;
2051
2052           if (first == NULL)
2053             first = hdr;
2054
2055           if ((abfd->flags & D_PAGED) != 0)
2056             {
2057               /* The section VMA must equal the file position modulo
2058                  the page size.  This is required by the program
2059                  header.  */
2060               off += (hdr->sh_addr - off) % maxpagesize;
2061             }
2062
2063           off = assign_file_position_for_section (hdr, off, false);
2064         }
2065
2066       /* Assign file positions to all the sections which do not form
2067          part of the loadable image, except for the relocs.  */
2068       for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2069         {
2070           Elf_Internal_Shdr *hdr;
2071
2072           hdr = *hdrpp;
2073           if ((hdr->sh_flags & SHF_ALLOC) != 0)
2074             continue;
2075           if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2076             {
2077               hdr->sh_offset = -1;
2078               continue;
2079             }
2080           if (! dosyms
2081               && (i == tdata->symtab_section
2082                   || i == tdata->strtab_section))
2083             {
2084               hdr->sh_offset = -1;
2085               continue;
2086             }
2087
2088           off = assign_file_position_for_section (hdr, off, true);
2089         }
2090
2091       phdr_map = map_program_segments (abfd, phdr_off, first, phdr_size);
2092       if (phdr_map == (file_ptr) -1)
2093         return false;
2094       BFD_ASSERT (phdr_map == phdr_off + phdr_size);
2095     }
2096
2097   /* Place the section headers.  */
2098   off = align_file_position (off);
2099   i_ehdrp->e_shoff = off;
2100   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2101
2102   elf_tdata (abfd)->next_file_pos = off;
2103
2104   return true;
2105 }
2106
2107 static boolean
2108 prep_headers (abfd)
2109      bfd *abfd;
2110 {
2111   Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */
2112   Elf_Internal_Phdr *i_phdrp = 0;       /* Program header table, internal form */
2113   Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
2114   int count;
2115   struct strtab *shstrtab;
2116
2117   i_ehdrp = elf_elfheader (abfd);
2118   i_shdrp = elf_elfsections (abfd);
2119
2120   shstrtab = bfd_new_strtab (abfd);
2121   if (!shstrtab)
2122     return false;
2123
2124   elf_shstrtab (abfd) = shstrtab;
2125
2126   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2127   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2128   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2129   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2130
2131   i_ehdrp->e_ident[EI_CLASS] = ELFCLASS;
2132   i_ehdrp->e_ident[EI_DATA] =
2133     abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB;
2134   i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT;
2135
2136   for (count = EI_PAD; count < EI_NIDENT; count++)
2137     i_ehdrp->e_ident[count] = 0;
2138
2139   i_ehdrp->e_type = (abfd->flags & EXEC_P) ? ET_EXEC : ET_REL;
2140   switch (bfd_get_arch (abfd))
2141     {
2142     case bfd_arch_unknown:
2143       i_ehdrp->e_machine = EM_NONE;
2144       break;
2145     case bfd_arch_sparc:
2146       i_ehdrp->e_machine = EM_SPARC;
2147       /* start-sanitize-v9 */
2148 #if ARCH_SIZE == 64
2149       i_ehdrp->e_machine = EM_SPARC64;
2150 #endif
2151       /* end-sanitize-v9 */
2152       break;
2153     case bfd_arch_i386:
2154       i_ehdrp->e_machine = EM_386;
2155       break;
2156     case bfd_arch_m68k:
2157       i_ehdrp->e_machine = EM_68K;
2158       break;
2159     case bfd_arch_m88k:
2160       i_ehdrp->e_machine = EM_88K;
2161       break;
2162     case bfd_arch_i860:
2163       i_ehdrp->e_machine = EM_860;
2164       break;
2165     case bfd_arch_mips: /* MIPS Rxxxx */
2166       i_ehdrp->e_machine = EM_MIPS;     /* only MIPS R3000 */
2167       break;
2168     case bfd_arch_hppa:
2169       i_ehdrp->e_machine = EM_PARISC;
2170       break;
2171     case bfd_arch_powerpc:
2172       i_ehdrp->e_machine = EM_CYGNUS_POWERPC;
2173       break;
2174       /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2175     default:
2176       i_ehdrp->e_machine = EM_NONE;
2177     }
2178   i_ehdrp->e_version = EV_CURRENT;
2179   i_ehdrp->e_ehsize = sizeof (Elf_External_Ehdr);
2180
2181   /* no program header, for now. */
2182   i_ehdrp->e_phoff = 0;
2183   i_ehdrp->e_phentsize = 0;
2184   i_ehdrp->e_phnum = 0;
2185
2186   /* each bfd section is section header entry */
2187   i_ehdrp->e_entry = bfd_get_start_address (abfd);
2188   i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr);
2189
2190   /* if we're building an executable, we'll need a program header table */
2191   if (abfd->flags & EXEC_P)
2192     {
2193       /* it all happens later */
2194 #if 0
2195       i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2196
2197       /* elf_build_phdrs() returns a (NULL-terminated) array of
2198          Elf_Internal_Phdrs */
2199       i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2200       i_ehdrp->e_phoff = outbase;
2201       outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2202 #endif
2203     }
2204   else
2205     {
2206       i_ehdrp->e_phentsize = 0;
2207       i_phdrp = 0;
2208       i_ehdrp->e_phoff = 0;
2209     }
2210
2211   elf_tdata (abfd)->symtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2212                                                             ".symtab");
2213   elf_tdata (abfd)->strtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2214                                                             ".strtab");
2215   elf_tdata (abfd)->shstrtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2216                                                               ".shstrtab");
2217   if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2218       || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2219       || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
2220     return false;
2221
2222   return true;
2223 }
2224
2225 static boolean
2226 swap_out_syms (abfd)
2227      bfd *abfd;
2228 {
2229   if (!elf_map_symbols (abfd))
2230     return false;
2231
2232   /* Dump out the symtabs. */
2233   {
2234     int symcount = bfd_get_symcount (abfd);
2235     asymbol **syms = bfd_get_outsymbols (abfd);
2236     struct strtab *stt = bfd_new_strtab (abfd);
2237     Elf_Internal_Shdr *symtab_hdr;
2238     Elf_Internal_Shdr *symstrtab_hdr;
2239     Elf_External_Sym *outbound_syms;
2240     int idx;
2241
2242     if (!stt)
2243       return false;
2244     symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2245     symtab_hdr->sh_type = SHT_SYMTAB;
2246     symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
2247     symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
2248     symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
2249
2250     /* FIXME: Systems I've checked use 4 byte alignment for .symtab,
2251        but it is possible that there are systems which use a different
2252        alignment.  */
2253     symtab_hdr->sh_addralign = 4;
2254
2255     /* see assert in elf_fake_sections that supports this: */
2256     symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
2257     symstrtab_hdr->sh_type = SHT_STRTAB;
2258
2259     outbound_syms = (Elf_External_Sym *)
2260       bfd_alloc (abfd, (1 + symcount) * sizeof (Elf_External_Sym));
2261     if (!outbound_syms)
2262       {
2263         bfd_set_error (bfd_error_no_memory);
2264         return false;
2265       }
2266     /* now generate the data (for "contents") */
2267     {
2268       /* Fill in zeroth symbol and swap it out.  */
2269       Elf_Internal_Sym sym;
2270       sym.st_name = 0;
2271       sym.st_value = 0;
2272       sym.st_size = 0;
2273       sym.st_info = 0;
2274       sym.st_other = 0;
2275       sym.st_shndx = SHN_UNDEF;
2276       elf_swap_symbol_out (abfd, &sym, outbound_syms);
2277     }
2278     for (idx = 0; idx < symcount; idx++)
2279       {
2280         Elf_Internal_Sym sym;
2281         bfd_vma value = syms[idx]->value;
2282
2283         if (syms[idx]->flags & BSF_SECTION_SYM)
2284           /* Section symbols have no names.  */
2285           sym.st_name = 0;
2286         else
2287           {
2288             sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name);
2289             if (sym.st_name == (unsigned long) -1)
2290               return false;
2291           }
2292
2293         if (bfd_is_com_section (syms[idx]->section))
2294           {
2295             /* ELF common symbols put the alignment into the `value' field,
2296                and the size into the `size' field.  This is backwards from
2297                how BFD handles it, so reverse it here.  */
2298             sym.st_size = value;
2299             /* Should retrieve this from somewhere... */
2300             sym.st_value = 16;
2301             sym.st_shndx = elf_section_from_bfd_section (abfd,
2302                                                          syms[idx]->section);
2303           }
2304         else
2305           {
2306             asection *sec = syms[idx]->section;
2307             elf_symbol_type *type_ptr;
2308             int shndx;
2309
2310             if (sec->output_section)
2311               {
2312                 value += sec->output_offset;
2313                 sec = sec->output_section;
2314               }
2315             value += sec->vma;
2316             sym.st_value = value;
2317             type_ptr = elf_symbol_from (abfd, syms[idx]);
2318             sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
2319             sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec);
2320             if (shndx == -1)
2321               {
2322                 asection *sec2;
2323                 /* Writing this would be a hell of a lot easier if we had
2324                    some decent documentation on bfd, and knew what to expect
2325                    of the library, and what to demand of applications.  For
2326                    example, it appears that `objcopy' might not set the
2327                    section of a symbol to be a section that is actually in
2328                    the output file.  */
2329                 sec2 = bfd_get_section_by_name (abfd, sec->name);
2330                 BFD_ASSERT (sec2 != 0);
2331                 sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec2);
2332                 BFD_ASSERT (shndx != -1);
2333               }
2334           }
2335
2336         if (bfd_is_com_section (syms[idx]->section))
2337           sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT);
2338         else if (syms[idx]->section == &bfd_und_section)
2339           sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_NOTYPE);
2340         else if (syms[idx]->flags & BSF_SECTION_SYM)
2341           sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
2342         else if (syms[idx]->flags & BSF_FILE)
2343           sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
2344         else
2345           {
2346             int bind = STB_LOCAL;
2347             int type = STT_OBJECT;
2348             unsigned int flags = syms[idx]->flags;
2349
2350             if (flags & BSF_LOCAL)
2351               bind = STB_LOCAL;
2352             else if (flags & BSF_WEAK)
2353               bind = STB_WEAK;
2354             else if (flags & BSF_GLOBAL)
2355               bind = STB_GLOBAL;
2356
2357             if (flags & BSF_FUNCTION)
2358               type = STT_FUNC;
2359
2360             sym.st_info = ELF_ST_INFO (bind, type);
2361           }
2362
2363         sym.st_other = 0;
2364         elf_swap_symbol_out (abfd, &sym,
2365                              (outbound_syms
2366                               + elf_sym_extra (abfd)[idx].elf_sym_num));
2367       }
2368
2369     symtab_hdr->contents = (PTR) outbound_syms;
2370     symstrtab_hdr->contents = (PTR) stt->tab;
2371     symstrtab_hdr->sh_size = stt->length;
2372     symstrtab_hdr->sh_type = SHT_STRTAB;
2373
2374     symstrtab_hdr->sh_flags = 0;
2375     symstrtab_hdr->sh_addr = 0;
2376     symstrtab_hdr->sh_entsize = 0;
2377     symstrtab_hdr->sh_link = 0;
2378     symstrtab_hdr->sh_info = 0;
2379     symstrtab_hdr->sh_addralign = 1;
2380     symstrtab_hdr->size = 0;
2381   }
2382
2383   return true;
2384 }
2385
2386 static boolean
2387 write_shdrs_and_ehdr (abfd)
2388      bfd *abfd;
2389 {
2390   Elf_External_Ehdr x_ehdr;     /* Elf file header, external form */
2391   Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */
2392   Elf_External_Shdr *x_shdrp;   /* Section header table, external form */
2393   Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
2394   unsigned int count;
2395   struct strtab *shstrtab;
2396
2397   i_ehdrp = elf_elfheader (abfd);
2398   i_shdrp = elf_elfsections (abfd);
2399   shstrtab = elf_shstrtab (abfd);
2400
2401   /* swap the header before spitting it out... */
2402
2403 #if DEBUG & 1
2404   elf_debug_file (i_ehdrp);
2405 #endif
2406   elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
2407   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2408       || (bfd_write ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd)
2409           != sizeof (x_ehdr)))
2410     return false;
2411
2412   /* at this point we've concocted all the ELF sections... */
2413   x_shdrp = (Elf_External_Shdr *)
2414     bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
2415   if (!x_shdrp)
2416     {
2417       bfd_set_error (bfd_error_no_memory);
2418       return false;
2419     }
2420
2421   for (count = 0; count < i_ehdrp->e_shnum; count++)
2422     {
2423 #if DEBUG & 2
2424       elf_debug_section (shstrtab->tab + i_shdrp[count]->sh_name, count,
2425                          i_shdrp[count]);
2426 #endif
2427       elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count);
2428     }
2429   if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0
2430       || (bfd_write ((PTR) x_shdrp, sizeof (*x_shdrp), i_ehdrp->e_shnum, abfd)
2431           != sizeof (*x_shdrp) * i_ehdrp->e_shnum))
2432     return false;
2433
2434   /* need to dump the string table too... */
2435
2436   return true;
2437 }
2438
2439 /* Assign file positions for all the reloc sections which are not part
2440    of the loadable file image.  */
2441
2442 static void
2443 assign_file_positions_for_relocs (abfd)
2444      bfd *abfd;
2445 {
2446   file_ptr off;
2447   unsigned int i;
2448   Elf_Internal_Shdr **shdrpp;
2449
2450   off = elf_tdata (abfd)->next_file_pos;
2451
2452   for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
2453        i < elf_elfheader (abfd)->e_shnum;
2454        i++, shdrpp++)
2455     {
2456       Elf_Internal_Shdr *shdrp;
2457
2458       shdrp = *shdrpp;
2459       if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
2460           && shdrp->sh_offset == -1)
2461         off = assign_file_position_for_section (shdrp, off, true);
2462     }
2463
2464   elf_tdata (abfd)->next_file_pos = off;
2465 }
2466
2467 boolean
2468 NAME(bfd_elf,write_object_contents) (abfd)
2469      bfd *abfd;
2470 {
2471   struct elf_backend_data *bed = get_elf_backend_data (abfd);
2472   Elf_Internal_Ehdr *i_ehdrp;
2473   Elf_Internal_Shdr **i_shdrp;
2474   unsigned int count;
2475
2476   /* We don't know how to write dynamic objects.  Specifically, we
2477      don't know how to construct the program header.  */
2478   if ((abfd->flags & DYNAMIC) != 0)
2479     {
2480       fprintf (stderr, "Writing ELF dynamic objects is not supported\n");
2481       bfd_set_error (bfd_error_wrong_format);
2482       return false;
2483     }
2484
2485   if (! abfd->output_has_begun
2486       && ! elf_compute_section_file_positions (abfd,
2487                                                (struct bfd_link_info *) NULL))
2488     return false;
2489
2490   i_shdrp = elf_elfsections (abfd);
2491   i_ehdrp = elf_elfheader (abfd);
2492
2493   bfd_map_over_sections (abfd, write_relocs, (PTR) 0);
2494   assign_file_positions_for_relocs (abfd);
2495
2496   /* After writing the headers, we need to write the sections too... */
2497   for (count = 1; count < i_ehdrp->e_shnum; count++)
2498     {
2499       if (bed->elf_backend_section_processing)
2500         (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
2501       if (i_shdrp[count]->contents)
2502         {
2503           if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
2504               || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
2505                              1, abfd)
2506                   != i_shdrp[count]->sh_size))
2507             return false;
2508         }
2509     }
2510
2511   if (bed->elf_backend_final_write_processing)
2512     (*bed->elf_backend_final_write_processing) (abfd);
2513
2514   return write_shdrs_and_ehdr (abfd);
2515 }
2516
2517 /* Given an index of a section, retrieve a pointer to it.  Note
2518    that for our purposes, sections are indexed by {1, 2, ...} with
2519    0 being an illegal index. */
2520
2521 /* In the original, each ELF section went into exactly one BFD
2522    section. This doesn't really make sense, so we need a real mapping.
2523    The mapping has to hide in the Elf_Internal_Shdr since asection
2524    doesn't have anything like a tdata field... */
2525
2526 static asection *
2527 section_from_elf_index (abfd, index)
2528      bfd *abfd;
2529      unsigned int index;
2530 {
2531   /* @@ Is bfd_com_section really correct in all the places it could
2532      be returned from this routine?  */
2533
2534   if (index == SHN_ABS)
2535     return &bfd_com_section;    /* not abs? */
2536   if (index == SHN_COMMON)
2537     return &bfd_com_section;
2538
2539   if (index > elf_elfheader (abfd)->e_shnum)
2540     return NULL;
2541
2542   {
2543     Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[index];
2544
2545     switch (hdr->sh_type)
2546       {
2547         /* ELF sections that map to BFD sections */
2548       case SHT_PROGBITS:
2549       case SHT_NOBITS:
2550       case SHT_HASH:
2551       case SHT_DYNAMIC:
2552         if (hdr->rawdata == NULL)
2553           {
2554             if (! bfd_section_from_shdr (abfd, index))
2555               return NULL;
2556           }
2557         return (struct sec *) hdr->rawdata;
2558
2559       default:
2560         return (struct sec *) &bfd_abs_section;
2561       }
2562   }
2563 }
2564
2565 /* given a section, search the header to find them... */
2566 static int
2567 elf_section_from_bfd_section (abfd, asect)
2568      bfd *abfd;
2569      struct sec *asect;
2570 {
2571   Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
2572   int index;
2573   Elf_Internal_Shdr *hdr;
2574   int maxindex = elf_elfheader (abfd)->e_shnum;
2575
2576   if (asect == &bfd_abs_section)
2577     return SHN_ABS;
2578   if (asect == &bfd_com_section)
2579     return SHN_COMMON;
2580   if (asect == &bfd_und_section)
2581     return SHN_UNDEF;
2582
2583   for (index = 0; index < maxindex; index++)
2584     {
2585       hdr = i_shdrp[index];
2586       switch (hdr->sh_type)
2587         {
2588           /* ELF sections that map to BFD sections */
2589         case SHT_PROGBITS:
2590         case SHT_NOBITS:
2591         case SHT_NOTE:
2592         case SHT_HASH:
2593         case SHT_DYNAMIC:
2594           if (hdr->rawdata)
2595             {
2596               if (((struct sec *) (hdr->rawdata)) == asect)
2597                 return index;
2598             }
2599           break;
2600
2601         case SHT_STRTAB:
2602           /* fix_up_strtabs will generate STRTAB sections with names
2603              of .stab*str.  */
2604           if (!strncmp (asect->name, ".stab", 5)
2605               && !strcmp ("str", asect->name + strlen (asect->name) - 3))
2606             {
2607               if (hdr->rawdata)
2608                 {
2609                   if (((struct sec *) (hdr->rawdata)) == asect)
2610                     return index;
2611                 }
2612               break;
2613             }
2614           /* FALL THROUGH */
2615         default:
2616           {
2617             struct elf_backend_data *bed = get_elf_backend_data (abfd);
2618
2619             if (bed->elf_backend_section_from_bfd_section)
2620               {
2621                 int retval;
2622
2623                 retval = index;
2624                 if ((*bed->elf_backend_section_from_bfd_section)
2625                     (abfd, hdr, asect, &retval))
2626                   return retval;
2627               }
2628           }
2629           break;
2630         }
2631     }
2632   return -1;
2633 }
2634
2635 /* given a symbol, return the bfd index for that symbol.  */
2636 static int
2637 elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
2638      bfd *abfd;
2639      struct symbol_cache_entry **asym_ptr_ptr;
2640 {
2641   struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr;
2642   int idx;
2643   flagword flags = asym_ptr->flags;
2644
2645   /* When gas creates relocations against local labels, it creates its
2646      own symbol for the section, but does put the symbol into the
2647      symbol chain, so udata is 0.  When the linker is generating
2648      relocatable output, this section symbol may be for one of the
2649      input sections rather than the output section.  */
2650   if (asym_ptr->udata == (PTR) 0
2651       && (flags & BSF_SECTION_SYM)
2652       && asym_ptr->section)
2653     {
2654       int indx;
2655
2656       if (asym_ptr->section->output_section != NULL)
2657         indx = asym_ptr->section->output_section->index;
2658       else
2659         indx = asym_ptr->section->index;
2660       if (elf_section_syms (abfd)[indx])
2661         asym_ptr->udata = elf_section_syms (abfd)[indx]->udata;
2662     }
2663
2664   if (asym_ptr->udata)
2665     idx = ((Elf_Sym_Extra *) asym_ptr->udata)->elf_sym_num;
2666   else
2667     {
2668       abort ();
2669     }
2670
2671 #if DEBUG & 4
2672   {
2673
2674     fprintf (stderr,
2675              "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n",
2676      (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags));
2677     fflush (stderr);
2678   }
2679 #endif
2680
2681   return idx;
2682 }
2683
2684 static long
2685 elf_slurp_symbol_table (abfd, symptrs, dynamic)
2686      bfd *abfd;
2687      asymbol **symptrs;         /* Buffer for generated bfd symbols */
2688      boolean dynamic;
2689 {
2690   Elf_Internal_Shdr *hdr;
2691   long symcount;                /* Number of external ELF symbols */
2692   elf_symbol_type *sym;         /* Pointer to current bfd symbol */
2693   elf_symbol_type *symbase;     /* Buffer for generated bfd symbols */
2694   Elf_Internal_Sym i_sym;
2695   Elf_External_Sym *x_symp = NULL;
2696
2697   /* Read each raw ELF symbol, converting from external ELF form to
2698      internal ELF form, and then using the information to create a
2699      canonical bfd symbol table entry.
2700
2701      Note that we allocate the initial bfd canonical symbol buffer
2702      based on a one-to-one mapping of the ELF symbols to canonical
2703      symbols.  We actually use all the ELF symbols, so there will be no
2704      space left over at the end.  When we have all the symbols, we
2705      build the caller's pointer vector. */
2706
2707   if (dynamic)
2708     hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2709   else
2710     hdr = &elf_tdata (abfd)->symtab_hdr;
2711   if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
2712     return -1;
2713
2714   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
2715
2716   if (symcount == 0)
2717     sym = symbase = NULL;
2718   else
2719     {
2720       long i;
2721
2722       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
2723         return -1;
2724
2725       symbase = ((elf_symbol_type *)
2726                  bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type)));
2727       if (symbase == (elf_symbol_type *) NULL)
2728         {
2729           bfd_set_error (bfd_error_no_memory);
2730           return -1;
2731         }
2732       sym = symbase;
2733
2734       /* Temporarily allocate room for the raw ELF symbols.  */
2735       x_symp = ((Elf_External_Sym *)
2736                 malloc (symcount * sizeof (Elf_External_Sym)));
2737       if (x_symp == NULL && symcount != 0)
2738         {
2739           bfd_set_error (bfd_error_no_memory);
2740           goto error_return;
2741         }
2742
2743       if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd)
2744           != symcount * sizeof (Elf_External_Sym))
2745         goto error_return;
2746       /* Skip first symbol, which is a null dummy.  */
2747       for (i = 1; i < symcount; i++)
2748         {
2749           elf_swap_symbol_in (abfd, x_symp + i, &i_sym);
2750           memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym));
2751 #ifdef ELF_KEEP_EXTSYM
2752           memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym));
2753 #endif
2754           sym->symbol.the_bfd = abfd;
2755
2756           sym->symbol.name = elf_string_from_elf_section (abfd, hdr->sh_link,
2757                                                           i_sym.st_name);
2758
2759           sym->symbol.value = i_sym.st_value;
2760
2761           if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERVE)
2762             {
2763               sym->symbol.section = section_from_elf_index (abfd,
2764                                                             i_sym.st_shndx);
2765               if (sym->symbol.section == NULL)
2766                 {
2767                   /* This symbol is in a section for which we did not
2768                      create a BFD section.  Just use bfd_abs_section,
2769                      although it is wrong.  FIXME.  */
2770                   sym->symbol.section = &bfd_abs_section;
2771                 }
2772             }
2773           else if (i_sym.st_shndx == SHN_ABS)
2774             {
2775               sym->symbol.section = &bfd_abs_section;
2776             }
2777           else if (i_sym.st_shndx == SHN_COMMON)
2778             {
2779               sym->symbol.section = &bfd_com_section;
2780               /* Elf puts the alignment into the `value' field, and
2781                  the size into the `size' field.  BFD wants to see the
2782                  size in the value field, and doesn't care (at the
2783                  moment) about the alignment.  */
2784               sym->symbol.value = i_sym.st_size;
2785             }
2786           else if (i_sym.st_shndx == SHN_UNDEF)
2787             {
2788               sym->symbol.section = &bfd_und_section;
2789             }
2790           else
2791             sym->symbol.section = &bfd_abs_section;
2792
2793           sym->symbol.value -= sym->symbol.section->vma;
2794
2795           switch (ELF_ST_BIND (i_sym.st_info))
2796             {
2797             case STB_LOCAL:
2798               sym->symbol.flags |= BSF_LOCAL;
2799               break;
2800             case STB_GLOBAL:
2801               sym->symbol.flags |= BSF_GLOBAL;
2802               break;
2803             case STB_WEAK:
2804               sym->symbol.flags |= BSF_WEAK;
2805               break;
2806             }
2807
2808           switch (ELF_ST_TYPE (i_sym.st_info))
2809             {
2810             case STT_SECTION:
2811               sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING;
2812               break;
2813             case STT_FILE:
2814               sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
2815               break;
2816             case STT_FUNC:
2817               sym->symbol.flags |= BSF_FUNCTION;
2818               break;
2819             }
2820
2821           if (dynamic)
2822             sym->symbol.flags |= BSF_DYNAMIC;
2823
2824           /* Do some backend-specific processing on this symbol.  */
2825           {
2826             struct elf_backend_data *ebd = get_elf_backend_data (abfd);
2827             if (ebd->elf_backend_symbol_processing)
2828               (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
2829           }
2830
2831           sym++;
2832         }
2833     }
2834
2835   /* Do some backend-specific processing on this symbol table.  */
2836   {
2837     struct elf_backend_data *ebd = get_elf_backend_data (abfd);
2838     if (ebd->elf_backend_symbol_table_processing)
2839       (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
2840   }
2841
2842   /* We rely on the zalloc to clear out the final symbol entry.  */
2843
2844   symcount = sym - symbase;
2845
2846   /* Fill in the user's symbol pointer vector if needed.  */
2847   if (symptrs)
2848     {
2849       long l = symcount;
2850
2851       sym = symbase;
2852       while (l-- > 0)
2853         {
2854           *symptrs++ = &sym->symbol;
2855           sym++;
2856         }
2857       *symptrs = 0;             /* Final null pointer */
2858     }
2859
2860   if (x_symp != NULL)
2861     free (x_symp);
2862   return symcount;
2863 error_return:
2864   if (x_symp != NULL)
2865     free (x_symp);
2866   return -1;
2867 }
2868
2869 /* Return the number of bytes required to hold the symtab vector.
2870
2871    Note that we base it on the count plus 1, since we will null terminate
2872    the vector allocated based on this size.  However, the ELF symbol table
2873    always has a dummy entry as symbol #0, so it ends up even.  */
2874
2875 long
2876 elf_get_symtab_upper_bound (abfd)
2877      bfd *abfd;
2878 {
2879   long symcount;
2880   long symtab_size;
2881   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
2882
2883   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
2884   symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
2885
2886   return symtab_size;
2887 }
2888
2889 long
2890 elf_get_dynamic_symtab_upper_bound (abfd)
2891      bfd *abfd;
2892 {
2893   long symcount;
2894   long symtab_size;
2895   Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2896
2897   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
2898   symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
2899
2900   return symtab_size;
2901 }
2902
2903 /*
2904         This function return the number of bytes required to store the
2905         relocation information associated with section <<sect>>
2906         attached to bfd <<abfd>>
2907
2908 */
2909 long
2910 elf_get_reloc_upper_bound (abfd, asect)
2911      bfd *abfd;
2912      sec_ptr asect;
2913 {
2914   if (asect->flags & SEC_RELOC)
2915     {
2916       /* either rel or rela */
2917       return elf_section_data (asect)->rel_hdr.sh_size;
2918     }
2919   else
2920     return 0;
2921 }
2922
2923 static boolean
2924 elf_slurp_reloca_table (abfd, asect, symbols)
2925      bfd *abfd;
2926      sec_ptr asect;
2927      asymbol **symbols;
2928 {
2929   Elf_External_Rela *native_relocs;
2930   arelent *reloc_cache;
2931   arelent *cache_ptr;
2932
2933   unsigned int idx;
2934
2935   if (asect->relocation)
2936     return true;
2937   if (asect->reloc_count == 0)
2938     return true;
2939   if (asect->flags & SEC_CONSTRUCTOR)
2940     return true;
2941
2942   if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
2943     return false;
2944   native_relocs = (Elf_External_Rela *)
2945     bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rela));
2946   if (!native_relocs)
2947     {
2948       bfd_set_error (bfd_error_no_memory);
2949       return false;
2950     }
2951   if (bfd_read ((PTR) native_relocs,
2952                 sizeof (Elf_External_Rela), asect->reloc_count, abfd)
2953       != sizeof (Elf_External_Rela) * asect->reloc_count)
2954     return false;
2955
2956   reloc_cache = (arelent *)
2957     bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
2958
2959   if (!reloc_cache)
2960     {
2961       bfd_set_error (bfd_error_no_memory);
2962       return false;
2963     }
2964
2965   for (idx = 0; idx < asect->reloc_count; idx++)
2966     {
2967       Elf_Internal_Rela dst;
2968       Elf_External_Rela *src;
2969
2970       cache_ptr = reloc_cache + idx;
2971       src = native_relocs + idx;
2972       elf_swap_reloca_in (abfd, src, &dst);
2973
2974 #ifdef RELOC_PROCESSING
2975       RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
2976 #else
2977       if (asect->flags & SEC_RELOC)
2978         {
2979           /* relocatable, so the offset is off of the section */
2980           cache_ptr->address = dst.r_offset + asect->vma;
2981         }
2982       else
2983         {
2984           /* non-relocatable, so the offset a virtual address */
2985           cache_ptr->address = dst.r_offset;
2986         }
2987
2988       /* ELF_R_SYM(dst.r_info) is the symbol table offset.  An offset
2989          of zero points to the dummy symbol, which was not read into
2990          the symbol table SYMBOLS.  */
2991       if (ELF_R_SYM (dst.r_info) == 0)
2992         cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
2993       else
2994         {
2995           asymbol *s;
2996
2997           cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1;
2998
2999           /* Translate any ELF section symbol into a BFD section
3000              symbol.  */
3001           s = *(cache_ptr->sym_ptr_ptr);
3002           if (s->flags & BSF_SECTION_SYM)
3003             {
3004               cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr;
3005               s = *cache_ptr->sym_ptr_ptr;
3006               if (s->name == 0 || s->name[0] == 0)
3007                 abort ();
3008             }
3009         }
3010       cache_ptr->addend = dst.r_addend;
3011
3012       /* Fill in the cache_ptr->howto field from dst.r_type */
3013       {
3014         struct elf_backend_data *ebd = get_elf_backend_data (abfd);
3015         (*ebd->elf_info_to_howto) (abfd, cache_ptr, &dst);
3016       }
3017 #endif
3018     }
3019
3020   asect->relocation = reloc_cache;
3021   return true;
3022 }
3023
3024 #ifdef DEBUG
3025 static void
3026 elf_debug_section (str, num, hdr)
3027      char *str;
3028      int num;
3029      Elf_Internal_Shdr *hdr;
3030 {
3031   fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num, str, (long) hdr);
3032   fprintf (stderr,
3033            "sh_name      = %ld\tsh_type      = %ld\tsh_flags     = %ld\n",
3034            (long) hdr->sh_name,
3035            (long) hdr->sh_type,
3036            (long) hdr->sh_flags);
3037   fprintf (stderr,
3038            "sh_addr      = %ld\tsh_offset    = %ld\tsh_size      = %ld\n",
3039            (long) hdr->sh_addr,
3040            (long) hdr->sh_offset,
3041            (long) hdr->sh_size);
3042   fprintf (stderr,
3043            "sh_link      = %ld\tsh_info      = %ld\tsh_addralign = %ld\n",
3044            (long) hdr->sh_link,
3045            (long) hdr->sh_info,
3046            (long) hdr->sh_addralign);
3047   fprintf (stderr, "sh_entsize   = %ld\n",
3048            (long) hdr->sh_entsize);
3049   fprintf (stderr, "rawdata      = 0x%.8lx\n", (long) hdr->rawdata);
3050   fprintf (stderr, "contents     = 0x%.8lx\n", (long) hdr->contents);
3051   fprintf (stderr, "size         = %ld\n", (long) hdr->size);
3052   fflush (stderr);
3053 }
3054
3055 static void
3056 elf_debug_file (ehdrp)
3057      Elf_Internal_Ehdr *ehdrp;
3058 {
3059   fprintf (stderr, "e_entry      = 0x%.8lx\n", (long) ehdrp->e_entry);
3060   fprintf (stderr, "e_phoff      = %ld\n", (long) ehdrp->e_phoff);
3061   fprintf (stderr, "e_phnum      = %ld\n", (long) ehdrp->e_phnum);
3062   fprintf (stderr, "e_phentsize  = %ld\n", (long) ehdrp->e_phentsize);
3063   fprintf (stderr, "e_shoff      = %ld\n", (long) ehdrp->e_shoff);
3064   fprintf (stderr, "e_shnum      = %ld\n", (long) ehdrp->e_shnum);
3065   fprintf (stderr, "e_shentsize  = %ld\n", (long) ehdrp->e_shentsize);
3066 }
3067 #endif
3068
3069 static boolean
3070 elf_slurp_reloc_table (abfd, asect, symbols)
3071      bfd *abfd;
3072      sec_ptr asect;
3073      asymbol **symbols;
3074 {
3075   Elf_External_Rel *native_relocs;
3076   arelent *reloc_cache;
3077   arelent *cache_ptr;
3078   Elf_Internal_Shdr *data_hdr;
3079   bfd_vma data_off;
3080   unsigned long data_max;
3081   char buf[4];                  /* FIXME -- might be elf64 */
3082
3083   unsigned int idx;
3084
3085   if (asect->relocation)
3086     return true;
3087   if (asect->reloc_count == 0)
3088     return true;
3089   if (asect->flags & SEC_CONSTRUCTOR)
3090     return true;
3091
3092   if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
3093     return false;
3094   native_relocs = (Elf_External_Rel *)
3095     bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rel));
3096   if (!native_relocs)
3097     {
3098       bfd_set_error (bfd_error_no_memory);
3099       return false;
3100     }
3101   if (bfd_read ((PTR) native_relocs,
3102                 sizeof (Elf_External_Rel), asect->reloc_count, abfd)
3103       != sizeof (Elf_External_Rel) * asect->reloc_count)
3104     return false;
3105
3106   reloc_cache = (arelent *)
3107     bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
3108
3109   if (!reloc_cache)
3110     {
3111       bfd_set_error (bfd_error_no_memory);
3112       return false;
3113     }
3114
3115   /* Get the offset of the start of the segment we are relocating to read in
3116      the implicit addend.  */
3117   data_hdr = &elf_section_data (asect)->this_hdr;
3118   data_off = data_hdr->sh_offset;
3119   data_max = data_hdr->sh_size - sizeof (buf) + 1;
3120
3121 #if DEBUG & 2
3122   elf_debug_section ("data section", -1, data_hdr);
3123 #endif
3124
3125   for (idx = 0; idx < asect->reloc_count; idx++)
3126     {
3127 #ifdef RELOC_PROCESSING
3128       Elf_Internal_Rel dst;
3129       Elf_External_Rel *src;
3130
3131       cache_ptr = reloc_cache + idx;
3132       src = native_relocs + idx;
3133       elf_swap_reloc_in (abfd, src, &dst);
3134
3135       RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
3136 #else
3137       Elf_Internal_Rel dst;
3138       Elf_External_Rel *src;
3139
3140       cache_ptr = reloc_cache + idx;
3141       src = native_relocs + idx;
3142
3143       elf_swap_reloc_in (abfd, src, &dst);
3144
3145       if (asect->flags & SEC_RELOC)
3146         {
3147           /* relocatable, so the offset is off of the section */
3148           cache_ptr->address = dst.r_offset + asect->vma;
3149         }
3150       else
3151         {
3152           /* non-relocatable, so the offset a virtual address */
3153           cache_ptr->address = dst.r_offset;
3154         }
3155
3156       /* ELF_R_SYM(dst.r_info) is the symbol table offset.  An offset
3157          of zero points to the dummy symbol, which was not read into
3158          the symbol table SYMBOLS.  */
3159       if (ELF_R_SYM (dst.r_info) == 0)
3160         cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
3161       else
3162         {
3163           asymbol *s;
3164
3165           cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1;
3166
3167           /* Translate any ELF section symbol into a BFD section
3168              symbol.  */
3169           s = *(cache_ptr->sym_ptr_ptr);
3170           if (s->flags & BSF_SECTION_SYM)
3171             {
3172               cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr;
3173               s = *cache_ptr->sym_ptr_ptr;
3174               if (s->name == 0 || s->name[0] == 0)
3175                 abort ();
3176             }
3177         }
3178       BFD_ASSERT (dst.r_offset <= data_max);
3179       cache_ptr->addend = 0;
3180
3181       /* Fill in the cache_ptr->howto field from dst.r_type */
3182       {
3183         struct elf_backend_data *ebd = get_elf_backend_data (abfd);
3184         (*ebd->elf_info_to_howto_rel) (abfd, cache_ptr, &dst);
3185       }
3186 #endif
3187     }
3188
3189   asect->relocation = reloc_cache;
3190   return true;
3191 }
3192
3193 long
3194 elf_canonicalize_reloc (abfd, section, relptr, symbols)
3195      bfd *abfd;
3196      sec_ptr section;
3197      arelent **relptr;
3198      asymbol **symbols;
3199 {
3200   arelent *tblptr = section->relocation;
3201   unsigned int count = 0;
3202   int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
3203
3204   /* snarfed from coffcode.h */
3205   if (use_rela_p)
3206     {
3207       if (! elf_slurp_reloca_table (abfd, section, symbols))
3208         return -1;
3209     }
3210   else
3211     {
3212       if (! elf_slurp_reloc_table (abfd, section, symbols))
3213         return -1;
3214     }
3215
3216   tblptr = section->relocation;
3217
3218   for (; count++ < section->reloc_count;)
3219     *relptr++ = tblptr++;
3220
3221   *relptr = 0;
3222   return section->reloc_count;
3223 }
3224
3225 long
3226 elf_get_symtab (abfd, alocation)
3227      bfd *abfd;
3228      asymbol **alocation;
3229 {
3230   long symcount = elf_slurp_symbol_table (abfd, alocation, false);
3231
3232   if (symcount >= 0)
3233     bfd_get_symcount (abfd) = symcount;
3234   return symcount;
3235 }
3236
3237 long
3238 elf_canonicalize_dynamic_symtab (abfd, alocation)
3239      bfd *abfd;
3240      asymbol **alocation;
3241 {
3242   return elf_slurp_symbol_table (abfd, alocation, true);
3243 }
3244
3245 asymbol *
3246 elf_make_empty_symbol (abfd)
3247      bfd *abfd;
3248 {
3249   elf_symbol_type *newsym;
3250
3251   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
3252   if (!newsym)
3253     {
3254       bfd_set_error (bfd_error_no_memory);
3255       return NULL;
3256     }
3257   else
3258     {
3259       newsym->symbol.the_bfd = abfd;
3260       return &newsym->symbol;
3261     }
3262 }
3263
3264 void
3265 elf_get_symbol_info (ignore_abfd, symbol, ret)
3266      bfd *ignore_abfd;
3267      asymbol *symbol;
3268      symbol_info *ret;
3269 {
3270   bfd_symbol_info (symbol, ret);
3271 }
3272
3273 void
3274 elf_print_symbol (ignore_abfd, filep, symbol, how)
3275      bfd *ignore_abfd;
3276      PTR filep;
3277      asymbol *symbol;
3278      bfd_print_symbol_type how;
3279 {
3280   FILE *file = (FILE *) filep;
3281   switch (how)
3282     {
3283     case bfd_print_symbol_name:
3284       fprintf (file, "%s", symbol->name);
3285       break;
3286     case bfd_print_symbol_more:
3287       fprintf (file, "elf ");
3288       fprintf_vma (file, symbol->value);
3289       fprintf (file, " %lx", (long) symbol->flags);
3290       break;
3291     case bfd_print_symbol_all:
3292       {
3293         CONST char *section_name;
3294         section_name = symbol->section ? symbol->section->name : "(*none*)";
3295         bfd_print_symbol_vandf ((PTR) file, symbol);
3296         fprintf (file, " %s\t%s",
3297                  section_name,
3298                  symbol->name);
3299       }
3300       break;
3301     }
3302
3303 }
3304
3305 alent *
3306 elf_get_lineno (ignore_abfd, symbol)
3307      bfd *ignore_abfd;
3308      asymbol *symbol;
3309 {
3310   fprintf (stderr, "elf_get_lineno unimplemented\n");
3311   fflush (stderr);
3312   BFD_FAIL ();
3313   return NULL;
3314 }
3315
3316 boolean
3317 elf_set_arch_mach (abfd, arch, machine)
3318      bfd *abfd;
3319      enum bfd_architecture arch;
3320      unsigned long machine;
3321 {
3322   /* If this isn't the right architecture for this backend, and this
3323      isn't the generic backend, fail.  */
3324   if (arch != get_elf_backend_data (abfd)->arch
3325       && arch != bfd_arch_unknown
3326       && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
3327     return false;
3328
3329   return bfd_default_set_arch_mach (abfd, arch, machine);
3330 }
3331
3332 boolean
3333 elf_find_nearest_line (abfd,
3334                        section,
3335                        symbols,
3336                        offset,
3337                        filename_ptr,
3338                        functionname_ptr,
3339                        line_ptr)
3340      bfd *abfd;
3341      asection *section;
3342      asymbol **symbols;
3343      bfd_vma offset;
3344      CONST char **filename_ptr;
3345      CONST char **functionname_ptr;
3346      unsigned int *line_ptr;
3347 {
3348   return false;
3349 }
3350
3351 int
3352 elf_sizeof_headers (abfd, reloc)
3353      bfd *abfd;
3354      boolean reloc;
3355 {
3356   int ret;
3357
3358   ret = sizeof (Elf_External_Ehdr);
3359   if (! reloc)
3360     ret += get_program_header_size (abfd);
3361   return ret;
3362 }
3363
3364 boolean
3365 elf_set_section_contents (abfd, section, location, offset, count)
3366      bfd *abfd;
3367      sec_ptr section;
3368      PTR location;
3369      file_ptr offset;
3370      bfd_size_type count;
3371 {
3372   Elf_Internal_Shdr *hdr;
3373
3374   if (! abfd->output_has_begun
3375       && ! elf_compute_section_file_positions (abfd,
3376                                                (struct bfd_link_info *) NULL))
3377     return false;
3378
3379   hdr = &elf_section_data (section)->this_hdr;
3380
3381   if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
3382     return false;
3383   if (bfd_write (location, 1, count, abfd) != count)
3384     return false;
3385
3386   return true;
3387 }
3388
3389 void
3390 elf_no_info_to_howto (abfd, cache_ptr, dst)
3391      bfd *abfd;
3392      arelent *cache_ptr;
3393      Elf_Internal_Rela *dst;
3394 {
3395   fprintf (stderr, "elf RELA relocation support for target machine unimplemented\n");
3396   fflush (stderr);
3397   BFD_FAIL ();
3398 }
3399
3400 void
3401 elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
3402      bfd *abfd;
3403      arelent *cache_ptr;
3404      Elf_Internal_Rel *dst;
3405 {
3406   fprintf (stderr, "elf REL relocation support for target machine unimplemented\n");
3407   fflush (stderr);
3408   BFD_FAIL ();
3409 }
3410 \f
3411
3412 /* Core file support */
3413
3414 #ifdef HAVE_PROCFS              /* Some core file support requires host /proc files */
3415 #include <sys/procfs.h>
3416 #else
3417 #define bfd_prstatus(abfd, descdata, descsz, filepos) true
3418 #define bfd_fpregset(abfd, descdata, descsz, filepos) true
3419 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) true
3420 #endif
3421
3422 #ifdef HAVE_PROCFS
3423
3424 static boolean
3425 bfd_prstatus (abfd, descdata, descsz, filepos)
3426      bfd *abfd;
3427      char *descdata;
3428      int descsz;
3429      long filepos;
3430 {
3431   asection *newsect;
3432   prstatus_t *status = (prstatus_t *) 0;
3433
3434   if (descsz == sizeof (prstatus_t))
3435     {
3436       newsect = bfd_make_section (abfd, ".reg");
3437       if (newsect == NULL)
3438         return false;
3439       newsect->_raw_size = sizeof (status->pr_reg);
3440       newsect->filepos = filepos + (long) &status->pr_reg;
3441       newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3442       newsect->alignment_power = 2;
3443       if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
3444         {
3445           memcpy (core_prstatus (abfd), descdata, descsz);
3446         }
3447     }
3448   return true;
3449 }
3450
3451 /* Stash a copy of the prpsinfo structure away for future use. */
3452
3453 static boolean
3454 bfd_prpsinfo (abfd, descdata, descsz, filepos)
3455      bfd *abfd;
3456      char *descdata;
3457      int descsz;
3458      long filepos;
3459 {
3460   if (descsz == sizeof (prpsinfo_t))
3461     {
3462       if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) == NULL)
3463         {
3464           bfd_set_error (bfd_error_no_memory);
3465           return false;
3466         }
3467       memcpy (core_prpsinfo (abfd), descdata, descsz);
3468     }
3469   return true;
3470 }
3471
3472 static boolean
3473 bfd_fpregset (abfd, descdata, descsz, filepos)
3474      bfd *abfd;
3475      char *descdata;
3476      int descsz;
3477      long filepos;
3478 {
3479   asection *newsect;
3480
3481   newsect = bfd_make_section (abfd, ".reg2");
3482   if (newsect == NULL)
3483     return false;
3484   newsect->_raw_size = descsz;
3485   newsect->filepos = filepos;
3486   newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3487   newsect->alignment_power = 2;
3488   return true;
3489 }
3490
3491 #endif /* HAVE_PROCFS */
3492
3493 /* Return a pointer to the args (including the command name) that were
3494    seen by the program that generated the core dump.  Note that for
3495    some reason, a spurious space is tacked onto the end of the args
3496    in some (at least one anyway) implementations, so strip it off if
3497    it exists. */
3498
3499 char *
3500 elf_core_file_failing_command (abfd)
3501      bfd *abfd;
3502 {
3503 #ifdef HAVE_PROCFS
3504   if (core_prpsinfo (abfd))
3505     {
3506       prpsinfo_t *p = core_prpsinfo (abfd);
3507       char *scan = p->pr_psargs;
3508       while (*scan++)
3509         {;
3510         }
3511       scan -= 2;
3512       if ((scan > p->pr_psargs) && (*scan == ' '))
3513         {
3514           *scan = '\000';
3515         }
3516       return p->pr_psargs;
3517     }
3518 #endif
3519   return NULL;
3520 }
3521
3522 /* Return the number of the signal that caused the core dump.  Presumably,
3523    since we have a core file, we got a signal of some kind, so don't bother
3524    checking the other process status fields, just return the signal number.
3525    */
3526
3527 int
3528 elf_core_file_failing_signal (abfd)
3529      bfd *abfd;
3530 {
3531 #ifdef HAVE_PROCFS
3532   if (core_prstatus (abfd))
3533     {
3534       return ((prstatus_t *) (core_prstatus (abfd)))->pr_cursig;
3535     }
3536 #endif
3537   return -1;
3538 }
3539
3540 /* Check to see if the core file could reasonably be expected to have
3541    come for the current executable file.  Note that by default we return
3542    true unless we find something that indicates that there might be a
3543    problem.
3544    */
3545
3546 boolean
3547 elf_core_file_matches_executable_p (core_bfd, exec_bfd)
3548      bfd *core_bfd;
3549      bfd *exec_bfd;
3550 {
3551 #ifdef HAVE_PROCFS
3552   char *corename;
3553   char *execname;
3554 #endif
3555
3556   /* First, xvecs must match since both are ELF files for the same target. */
3557
3558   if (core_bfd->xvec != exec_bfd->xvec)
3559     {
3560       bfd_set_error (bfd_error_system_call);
3561       return false;
3562     }
3563
3564 #ifdef HAVE_PROCFS
3565
3566   /* If no prpsinfo, just return true.  Otherwise, grab the last component
3567      of the exec'd pathname from the prpsinfo. */
3568
3569   if (core_prpsinfo (core_bfd))
3570     {
3571       corename = (((struct prpsinfo *) core_prpsinfo (core_bfd))->pr_fname);
3572     }
3573   else
3574     {
3575       return true;
3576     }
3577
3578   /* Find the last component of the executable pathname. */
3579
3580   if ((execname = strrchr (exec_bfd->filename, '/')) != NULL)
3581     {
3582       execname++;
3583     }
3584   else
3585     {
3586       execname = (char *) exec_bfd->filename;
3587     }
3588
3589   /* See if they match */
3590
3591   return strcmp (execname, corename) ? false : true;
3592
3593 #else
3594
3595   return true;
3596
3597 #endif /* HAVE_PROCFS */
3598 }
3599
3600 /* ELF core files contain a segment of type PT_NOTE, that holds much of
3601    the information that would normally be available from the /proc interface
3602    for the process, at the time the process dumped core.  Currently this
3603    includes copies of the prstatus, prpsinfo, and fpregset structures.
3604
3605    Since these structures are potentially machine dependent in size and
3606    ordering, bfd provides two levels of support for them.  The first level,
3607    available on all machines since it does not require that the host
3608    have /proc support or the relevant include files, is to create a bfd
3609    section for each of the prstatus, prpsinfo, and fpregset structures,
3610    without any interpretation of their contents.  With just this support,
3611    the bfd client will have to interpret the structures itself.  Even with
3612    /proc support, it might want these full structures for it's own reasons.
3613
3614    In the second level of support, where HAVE_PROCFS is defined, bfd will
3615    pick apart the structures to gather some additional information that
3616    clients may want, such as the general register set, the name of the
3617    exec'ed file and its arguments, the signal (if any) that caused the
3618    core dump, etc.
3619
3620    */
3621
3622 static boolean
3623 elf_corefile_note (abfd, hdr)
3624      bfd *abfd;
3625      Elf_Internal_Phdr *hdr;
3626 {
3627   Elf_External_Note *x_note_p;  /* Elf note, external form */
3628   Elf_Internal_Note i_note;     /* Elf note, internal form */
3629   char *buf = NULL;             /* Entire note segment contents */
3630   char *namedata;               /* Name portion of the note */
3631   char *descdata;               /* Descriptor portion of the note */
3632   char *sectname;               /* Name to use for new section */
3633   long filepos;                 /* File offset to descriptor data */
3634   asection *newsect;
3635
3636   if (hdr->p_filesz > 0
3637       && (buf = (char *) malloc (hdr->p_filesz)) != NULL
3638       && bfd_seek (abfd, hdr->p_offset, SEEK_SET) != -1
3639       && bfd_read ((PTR) buf, hdr->p_filesz, 1, abfd) == hdr->p_filesz)
3640     {
3641       x_note_p = (Elf_External_Note *) buf;
3642       while ((char *) x_note_p < (buf + hdr->p_filesz))
3643         {
3644           i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->namesz);
3645           i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->descsz);
3646           i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->type);
3647           namedata = x_note_p->name;
3648           descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
3649           filepos = hdr->p_offset + (descdata - buf);
3650           switch (i_note.type)
3651             {
3652             case NT_PRSTATUS:
3653               /* process descdata as prstatus info */
3654               if (! bfd_prstatus (abfd, descdata, i_note.descsz, filepos))
3655                 return false;
3656               sectname = ".prstatus";
3657               break;
3658             case NT_FPREGSET:
3659               /* process descdata as fpregset info */
3660               if (! bfd_fpregset (abfd, descdata, i_note.descsz, filepos))
3661                 return false;
3662               sectname = ".fpregset";
3663               break;
3664             case NT_PRPSINFO:
3665               /* process descdata as prpsinfo */
3666               if (! bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos))
3667                 return false;
3668               sectname = ".prpsinfo";
3669               break;
3670             default:
3671               /* Unknown descriptor, just ignore it. */
3672               sectname = NULL;
3673               break;
3674             }
3675           if (sectname != NULL)
3676             {
3677               newsect = bfd_make_section (abfd, sectname);
3678               if (newsect == NULL)
3679                 return false;
3680               newsect->_raw_size = i_note.descsz;
3681               newsect->filepos = filepos;
3682               newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3683               newsect->alignment_power = 2;
3684             }
3685           x_note_p = (Elf_External_Note *)
3686             (descdata + BFD_ALIGN (i_note.descsz, 4));
3687         }
3688     }
3689   if (buf != NULL)
3690     {
3691       free (buf);
3692     }
3693   else if (hdr->p_filesz > 0)
3694     {
3695       bfd_set_error (bfd_error_no_memory);
3696       return false;
3697     }
3698   return true;
3699
3700 }
3701
3702 /*  Core files are simply standard ELF formatted files that partition
3703     the file using the execution view of the file (program header table)
3704     rather than the linking view.  In fact, there is no section header
3705     table in a core file.
3706
3707     The process status information (including the contents of the general
3708     register set) and the floating point register set are stored in a
3709     segment of type PT_NOTE.  We handcraft a couple of extra bfd sections
3710     that allow standard bfd access to the general registers (.reg) and the
3711     floating point registers (.reg2).
3712
3713  */
3714
3715 bfd_target *
3716 elf_core_file_p (abfd)
3717      bfd *abfd;
3718 {
3719   Elf_External_Ehdr x_ehdr;     /* Elf file header, external form */
3720   Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */
3721   Elf_External_Phdr x_phdr;     /* Program header table entry, external form */
3722   Elf_Internal_Phdr *i_phdrp;   /* Program header table, internal form */
3723   unsigned int phindex;
3724   struct elf_backend_data *ebd;
3725
3726   /* Read in the ELF header in external format.  */
3727
3728   if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
3729     {
3730       if (bfd_get_error () != bfd_error_system_call)
3731         bfd_set_error (bfd_error_wrong_format);
3732       return NULL;
3733     }
3734
3735   /* Now check to see if we have a valid ELF file, and one that BFD can
3736      make use of.  The magic number must match, the address size ('class')
3737      and byte-swapping must match our XVEC entry, and it must have a
3738      program header table (FIXME: See comments re segments at top of this
3739      file). */
3740
3741   if (elf_file_p (&x_ehdr) == false)
3742     {
3743     wrong:
3744       bfd_set_error (bfd_error_wrong_format);
3745       return NULL;
3746     }
3747
3748   /* FIXME, Check EI_VERSION here !  */
3749
3750   {
3751 #if ARCH_SIZE == 32
3752     int desired_address_size = ELFCLASS32;
3753 #endif
3754 #if ARCH_SIZE == 64
3755     int desired_address_size = ELFCLASS64;
3756 #endif
3757
3758     if (x_ehdr.e_ident[EI_CLASS] != desired_address_size)
3759       goto wrong;
3760   }
3761
3762   /* Switch xvec to match the specified byte order.  */
3763   switch (x_ehdr.e_ident[EI_DATA])
3764     {
3765     case ELFDATA2MSB:           /* Big-endian */
3766       if (abfd->xvec->byteorder_big_p == false)
3767         goto wrong;
3768       break;
3769     case ELFDATA2LSB:           /* Little-endian */
3770       if (abfd->xvec->byteorder_big_p == true)
3771         goto wrong;
3772       break;
3773     case ELFDATANONE:           /* No data encoding specified */
3774     default:                    /* Unknown data encoding specified */
3775       goto wrong;
3776     }
3777
3778   /* Allocate an instance of the elf_obj_tdata structure and hook it up to
3779      the tdata pointer in the bfd. */
3780
3781   elf_tdata (abfd) =
3782     (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
3783   if (elf_tdata (abfd) == NULL)
3784     {
3785       bfd_set_error (bfd_error_no_memory);
3786       return NULL;
3787     }
3788
3789   /* FIXME, `wrong' returns from this point onward, leak memory.  */
3790
3791   /* Now that we know the byte order, swap in the rest of the header */
3792   i_ehdrp = elf_elfheader (abfd);
3793   elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
3794 #if DEBUG & 1
3795   elf_debug_file (i_ehdrp);
3796 #endif
3797
3798   ebd = get_elf_backend_data (abfd);
3799
3800   /* Check that the ELF e_machine field matches what this particular
3801      BFD format expects.  */
3802   if (ebd->elf_machine_code != i_ehdrp->e_machine)
3803     {
3804       bfd_target **target_ptr;
3805
3806       if (ebd->elf_machine_code != EM_NONE)
3807         goto wrong;
3808
3809       /* This is the generic ELF target.  Let it match any ELF target
3810          for which we do not have a specific backend.  */
3811       for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
3812         {
3813           struct elf_backend_data *back;
3814
3815           if ((*target_ptr)->flavour != bfd_target_elf_flavour)
3816             continue;
3817           back = (struct elf_backend_data *) (*target_ptr)->backend_data;
3818           if (back->elf_machine_code == i_ehdrp->e_machine)
3819             {
3820               /* target_ptr is an ELF backend which matches this
3821                  object file, so reject the generic ELF target.  */
3822               goto wrong;
3823             }
3824         }
3825     }
3826
3827   /* If there is no program header, or the type is not a core file, then
3828      we are hosed. */
3829   if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
3830     goto wrong;
3831
3832   /* Allocate space for a copy of the program header table in
3833      internal form, seek to the program header table in the file,
3834      read it in, and convert it to internal form.  As a simple sanity
3835      check, verify that the what BFD thinks is the size of each program
3836      header table entry actually matches the size recorded in the file. */
3837
3838   if (i_ehdrp->e_phentsize != sizeof (x_phdr))
3839     goto wrong;
3840   i_phdrp = (Elf_Internal_Phdr *)
3841     bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum);
3842   if (!i_phdrp)
3843     {
3844       bfd_set_error (bfd_error_no_memory);
3845       return NULL;
3846     }
3847   if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1)
3848     return NULL;
3849   for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
3850     {
3851       if (bfd_read ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd)
3852           != sizeof (x_phdr))
3853         return NULL;
3854       elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
3855     }
3856
3857   /* Once all of the program headers have been read and converted, we
3858      can start processing them. */
3859
3860   for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
3861     {
3862       bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
3863       if ((i_phdrp + phindex)->p_type == PT_NOTE)
3864         {
3865           if (! elf_corefile_note (abfd, i_phdrp + phindex))
3866             return NULL;
3867         }
3868     }
3869
3870   /* Remember the entry point specified in the ELF file header. */
3871
3872   bfd_get_start_address (abfd) = i_ehdrp->e_entry;
3873
3874   return abfd->xvec;
3875 }
3876 \f
3877 /* ELF linker code.  */
3878
3879 static boolean elf_link_add_object_symbols
3880   PARAMS ((bfd *, struct bfd_link_info *));
3881 static boolean elf_link_add_archive_symbols
3882   PARAMS ((bfd *, struct bfd_link_info *));
3883 static INLINE boolean elf_link_record_dynamic_symbol
3884   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
3885 static boolean elf_link_create_dynamic_sections
3886   PARAMS ((bfd *, struct bfd_link_info *));
3887 static boolean elf_adjust_dynamic_symbol
3888   PARAMS ((struct elf_link_hash_entry *, PTR));
3889
3890 /* Given an ELF BFD, add symbols to the global hash table as
3891    appropriate.  */
3892
3893 boolean
3894 elf_bfd_link_add_symbols (abfd, info)
3895      bfd *abfd;
3896      struct bfd_link_info *info;
3897 {
3898   switch (bfd_get_format (abfd))
3899     {
3900     case bfd_object:
3901       return elf_link_add_object_symbols (abfd, info);
3902     case bfd_archive:
3903       return elf_link_add_archive_symbols (abfd, info);
3904     default:
3905       bfd_set_error (bfd_error_wrong_format);
3906       return false;
3907     }
3908 }
3909
3910 /* Add symbols from an ELF archive file to the linker hash table.  We
3911    don't use _bfd_generic_link_add_archive_symbols because of a
3912    problem which arises on UnixWare.  The UnixWare libc.so is an
3913    archive which includes an entry libc.so.1 which defines a bunch of
3914    symbols.  The libc.so archive also includes a number of other
3915    object files, which also define symbols, some of which are the same
3916    as those defined in libc.so.1.  Correct linking requires that we
3917    consider each object file in turn, and include it if it defines any
3918    symbols we need.  _bfd_generic_link_add_archive_symbols does not do
3919    this; it looks through the list of undefined symbols, and includes
3920    any object file which defines them.  When this algorithm is used on
3921    UnixWare, it winds up pulling in libc.so.1 early and defining a
3922    bunch of symbols.  This means that some of the other objects in the
3923    archive are not included in the link, which is incorrect since they
3924    precede libc.so.1 in the archive.
3925
3926    Fortunately, ELF archive handling is simpler than that done by
3927    _bfd_generic_link_add_archive_symbols, which has to allow for a.out
3928    oddities.  In ELF, if we find a symbol in the archive map, and the
3929    symbol is currently undefined, we know that we must pull in that
3930    object file.
3931
3932    Unfortunately, we do have to make multiple passes over the symbol
3933    table until nothing further is resolved.  */
3934
3935 static boolean
3936 elf_link_add_archive_symbols (abfd, info)
3937      bfd *abfd;
3938      struct bfd_link_info *info;
3939 {
3940   symindex c;
3941   boolean *defined = NULL;
3942   boolean *included = NULL;
3943   carsym *symdefs;
3944   boolean loop;
3945
3946   if (! bfd_has_map (abfd))
3947     {
3948       bfd_set_error (bfd_error_no_symbols);
3949       return false;
3950     }
3951
3952   /* Keep track of all symbols we know to be already defined, and all
3953      files we know to be already included.  This is to speed up the
3954      second and subsequent passes.  */
3955   c = bfd_ardata (abfd)->symdef_count;
3956   if (c == 0)
3957     return true;
3958   defined = (boolean *) malloc (c * sizeof (boolean));
3959   included = (boolean *) malloc (c * sizeof (boolean));
3960   if (defined == (boolean *) NULL || included == (boolean *) NULL)
3961     {
3962       bfd_set_error (bfd_error_no_memory);
3963       goto error_return;
3964     }
3965   memset (defined, 0, c * sizeof (boolean));
3966   memset (included, 0, c * sizeof (boolean));
3967
3968   symdefs = bfd_ardata (abfd)->symdefs;
3969
3970   do
3971     {
3972       file_ptr last;
3973       symindex i;
3974       carsym *symdef;
3975       carsym *symdefend;
3976
3977       loop = false;
3978       last = -1;
3979
3980       symdef = symdefs;
3981       symdefend = symdef + c;
3982       for (i = 0; symdef < symdefend; symdef++, i++)
3983         {
3984           struct elf_link_hash_entry *h;
3985           bfd *element;
3986           struct bfd_link_hash_entry *undefs_tail;
3987           symindex mark;
3988
3989           if (defined[i] || included[i])
3990             continue;
3991           if (symdef->file_offset == last)
3992             {
3993               included[i] = true;
3994               continue;
3995             }
3996
3997           h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
3998                                     false, false, false);
3999           if (h == (struct elf_link_hash_entry *) NULL)
4000             continue;
4001           if (h->root.type != bfd_link_hash_undefined)
4002             {
4003               defined[i] = true;
4004               continue;
4005             }
4006
4007           /* We need to include this archive member.  */
4008
4009           element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4010           if (element == (bfd *) NULL)
4011             goto error_return;
4012
4013           if (! bfd_check_format (element, bfd_object))
4014             goto error_return;
4015
4016           /* Doublecheck that we have not included this object
4017              already--it should be impossible, but there may be
4018              something wrong with the archive.  */
4019           if (element->archive_pass != 0)
4020             {
4021               bfd_set_error (bfd_error_bad_value);
4022               goto error_return;
4023             }
4024           element->archive_pass = 1;
4025
4026           undefs_tail = info->hash->undefs_tail;
4027
4028           if (! (*info->callbacks->add_archive_element) (info, element,
4029                                                          symdef->name))
4030             goto error_return;
4031           if (! elf_link_add_object_symbols (element, info))
4032             goto error_return;
4033
4034           /* If there are any new undefined symbols, we need to make
4035              another pass through the archive in order to see whether
4036              they can be defined.  FIXME: This isn't perfect, because
4037              common symbols wind up on undefs_tail and because an
4038              undefined symbol which is defined later on in this pass
4039              does not require another pass.  This isn't a bug, but it
4040              does make the code less efficient than it could be.  */
4041           if (undefs_tail != info->hash->undefs_tail)
4042             loop = true;
4043
4044           /* Look backward to mark all symbols from this object file
4045              which we have already seen in this pass.  */
4046           mark = i;
4047           do
4048             {
4049               included[mark] = true;
4050               if (mark == 0)
4051                 break;
4052               --mark;
4053             }
4054           while (symdefs[mark].file_offset == symdef->file_offset);
4055
4056           /* We mark subsequent symbols from this object file as we go
4057              on through the loop.  */
4058           last = symdef->file_offset;
4059         }
4060     }
4061   while (loop);
4062
4063   free (defined);
4064   free (included);
4065
4066   return true;
4067
4068  error_return:
4069   if (defined != (boolean *) NULL)
4070     free (defined);
4071   if (included != (boolean *) NULL)
4072     free (included);
4073   return false;
4074 }
4075
4076 /* Record a new dynamic symbol.  We record the dynamic symbols as we
4077    read the input files, since we need to have a list of all of them
4078    before we can determine the final sizes of the output sections.  */
4079
4080 static INLINE boolean
4081 elf_link_record_dynamic_symbol (info, h)
4082      struct bfd_link_info *info;
4083      struct elf_link_hash_entry *h;
4084 {
4085   if (h->dynindx == -1)
4086     {
4087       h->dynindx = elf_hash_table (info)->dynsymcount;
4088       ++elf_hash_table (info)->dynsymcount;
4089       h->dynstr_index = bfd_add_to_strtab (elf_hash_table (info)->dynobj,
4090                                            elf_hash_table (info)->dynstr,
4091                                            h->root.root.string);
4092       if (h->dynstr_index == (unsigned long) -1)
4093         return false;
4094     }
4095
4096   return true;
4097 }
4098
4099 /* Add symbols from an ELF object file to the linker hash table.  */
4100
4101 static boolean
4102 elf_link_add_object_symbols (abfd, info)
4103      bfd *abfd;
4104      struct bfd_link_info *info;
4105 {
4106   boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
4107                                       const Elf_Internal_Sym *,
4108                                       const char **, flagword *,
4109                                       asection **, bfd_vma *));
4110   boolean collect;
4111   Elf_Internal_Shdr *hdr;
4112   size_t symcount;
4113   size_t extsymcount;
4114   Elf_External_Sym *buf = NULL;
4115   struct elf_link_hash_entry **sym_hash;
4116   boolean dynamic;
4117   Elf_External_Dyn *dynbuf = NULL;
4118   struct elf_link_hash_entry *weaks;
4119   Elf_External_Sym *esym;
4120   Elf_External_Sym *esymend;
4121
4122   add_symbol_hook = get_elf_backend_data (abfd)->elf_add_symbol_hook;
4123   collect = get_elf_backend_data (abfd)->collect;
4124
4125   hdr = &elf_tdata (abfd)->symtab_hdr;
4126   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
4127
4128   /* The sh_info field of the symtab header tells us where the
4129      external symbols start.  We don't care about the local symbols at
4130      this point.  */
4131   extsymcount = symcount - hdr->sh_info;
4132
4133   buf = (Elf_External_Sym *) malloc (extsymcount * sizeof (Elf_External_Sym));
4134   if (buf == NULL && extsymcount != 0)
4135     {
4136       bfd_set_error (bfd_error_no_memory);
4137       goto error_return;
4138     }
4139
4140   /* We store a pointer to the hash table entry for each external
4141      symbol.  */
4142   sym_hash = ((struct elf_link_hash_entry **)
4143               bfd_alloc (abfd,
4144                          extsymcount * sizeof (struct elf_link_hash_entry *)));
4145   if (sym_hash == NULL)
4146     {
4147       bfd_set_error (bfd_error_no_memory);
4148       goto error_return;
4149     }
4150   elf_sym_hashes (abfd) = sym_hash;
4151
4152   if (elf_elfheader (abfd)->e_type != ET_DYN)
4153     dynamic = false;
4154   else
4155     {
4156       asection *s;
4157       const char *name;
4158       unsigned long strindex;
4159
4160       dynamic = true;
4161
4162       /* You can't use -r against a dynamic object.  There's no hope
4163          of using a dynamic object which does not exactly match the
4164          format of the output file.  */
4165       if (info->relocateable
4166           || info->hash->creator != abfd->xvec)
4167         {
4168           bfd_set_error (bfd_error_invalid_operation);
4169           goto error_return;
4170         }
4171
4172       /* Find the name to use in a DT_NEEDED entry that refers to this
4173          object.  If the object has a DT_SONAME entry, we use it.
4174          Otherwise, we use the file name.  */
4175       name = bfd_get_filename (abfd);
4176       s = bfd_get_section_by_name (abfd, ".dynamic");
4177       if (s != NULL)
4178         {
4179           Elf_External_Dyn *extdyn;
4180           Elf_External_Dyn *extdynend;
4181
4182           dynbuf = (Elf_External_Dyn *) malloc (s->_raw_size);
4183           if (dynbuf == NULL)
4184             {
4185               bfd_set_error (bfd_error_no_memory);
4186               goto error_return;
4187             }
4188
4189           if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
4190                                           (file_ptr) 0, s->_raw_size))
4191             goto error_return;
4192
4193           extdyn = dynbuf;
4194           extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
4195           for (; extdyn < extdynend; extdyn++)
4196             {
4197               Elf_Internal_Dyn dyn;
4198
4199               elf_swap_dyn_in (abfd, extdyn, &dyn);
4200               if (dyn.d_tag == DT_SONAME)
4201                 {
4202                   int elfsec;
4203                   unsigned long link;
4204
4205                   elfsec = elf_section_from_bfd_section (abfd, s);
4206                   if (elfsec == -1)
4207                     goto error_return;
4208                   link = elf_elfsections (abfd)[elfsec]->sh_link;
4209                   name = elf_string_from_elf_section (abfd, link,
4210                                                       dyn.d_un.d_val);
4211                   if (name == NULL)
4212                     goto error_return;
4213
4214                   break;
4215                 }
4216             }
4217
4218           free (dynbuf);
4219           dynbuf = NULL;
4220         }
4221
4222       /* We do not want to include any of the sections in a dynamic
4223          object in the output file.  We hack by simply clobbering the
4224          list of sections in the BFD.  This could be handled more
4225          cleanly by, say, a new section flag; the existing
4226          SEC_NEVER_LOAD flag is not the one we want, because that one
4227          still implies that the section takes up space in the output
4228          file.  */
4229       abfd->sections = NULL;
4230
4231       /* If this is the first dynamic object found in the link, create
4232          the special sections required for dynamic linking.  We need
4233          to put them somewhere, and attaching them to the first
4234          dynamic object is as good place as any.  */
4235       if (elf_hash_table (info)->dynobj == NULL)
4236         {
4237           if (! elf_link_create_dynamic_sections (abfd, info))
4238             goto error_return;
4239           elf_hash_table (info)->dynobj = abfd;
4240         }
4241
4242       /* Add a DT_NEEDED entry for this dynamic object.  */
4243       strindex = bfd_add_to_strtab (abfd,
4244                                     elf_hash_table (info)->dynstr,
4245                                     name);
4246       
4247       if (strindex == (unsigned long) -1)
4248         goto error_return;
4249       if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
4250         goto error_return;
4251     }
4252
4253   if (bfd_seek (abfd,
4254                 hdr->sh_offset + hdr->sh_info * sizeof (Elf_External_Sym),
4255                 SEEK_SET) != 0
4256       || (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd)
4257           != extsymcount * sizeof (Elf_External_Sym)))
4258     goto error_return;
4259
4260   weaks = NULL;
4261
4262   esymend = buf + extsymcount;
4263   for (esym = buf; esym < esymend; esym++, sym_hash++)
4264     {
4265       Elf_Internal_Sym sym;
4266       int bind;
4267       bfd_vma value;
4268       asection *sec;
4269       flagword flags;
4270       const char *name;
4271       struct elf_link_hash_entry *h = NULL;
4272       boolean definition;
4273
4274       elf_swap_symbol_in (abfd, esym, &sym);
4275
4276       flags = BSF_NO_FLAGS;
4277       sec = NULL;
4278       value = sym.st_value;
4279       *sym_hash = NULL;
4280
4281       bind = ELF_ST_BIND (sym.st_info);
4282       if (bind == STB_LOCAL)
4283         {
4284           /* This should be impossible, since ELF requires that all
4285              global symbols follow all local symbols, and that sh_info
4286              point to the first global symbol.  */
4287           bfd_set_error (bfd_error_bad_value);
4288           goto error_return;
4289         }
4290       else if (bind == STB_GLOBAL)
4291         flags = BSF_GLOBAL;
4292       else if (bind == STB_WEAK)
4293         flags = BSF_WEAK;
4294       else
4295         {
4296           /* Leave it up to the processor backend.  */
4297         }
4298
4299       if (sym.st_shndx == SHN_UNDEF)
4300         sec = &bfd_und_section;
4301       else if (sym.st_shndx > 0 && sym.st_shndx < SHN_LORESERVE)
4302         {
4303           sec = section_from_elf_index (abfd, sym.st_shndx);
4304           if (sec == NULL)
4305             goto error_return;
4306           value -= sec->vma;
4307         }
4308       else if (sym.st_shndx == SHN_ABS)
4309         sec = &bfd_abs_section;
4310       else if (sym.st_shndx == SHN_COMMON)
4311         {
4312           sec = &bfd_com_section;
4313           /* What ELF calls the size we call the value.  What ELF
4314              calls the value we call the alignment.  */
4315           value = sym.st_size;
4316         }
4317       else
4318         {
4319           /* Leave it up to the processor backend.  */
4320         }
4321
4322       name = elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
4323       if (name == (const char *) NULL)
4324         goto error_return;
4325
4326       if (add_symbol_hook)
4327         {
4328           if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
4329                                     &value))
4330             goto error_return;
4331
4332           /* The hook function sets the name to NULL if this symbol
4333              should be skipped for some reason.  */
4334           if (name == (const char *) NULL)
4335             continue;
4336         }
4337
4338       /* Sanity check that all possibilities were handled.  */
4339       if (flags == BSF_NO_FLAGS || sec == (asection *) NULL)
4340         {
4341           bfd_set_error (bfd_error_bad_value);
4342           goto error_return;
4343         }
4344
4345       if (sec == &bfd_und_section
4346           || bfd_is_com_section (sec))
4347         definition = false;
4348       else
4349         definition = true;
4350
4351       if (info->hash->creator->flavour == bfd_target_elf_flavour)
4352         {
4353           /* We need to look up the symbol now in order to get some of
4354              the dynamic object handling right.  We pass the hash
4355              table entry in to _bfd_generic_link_add_one_symbol so
4356              that it does not have to look it up again.  */
4357           h = elf_link_hash_lookup (elf_hash_table (info), name,
4358                                     true, false, false);
4359           if (h == NULL)
4360             goto error_return;
4361           *sym_hash = h;
4362
4363           /* If we are looking at a dynamic object, and this is a
4364              definition, we need to see if it has already been defined
4365              by some other object.  If it has, we want to use the
4366              existing definition, and we do not want to report a
4367              multiple symbol definition error; we do this by
4368              clobbering sec to be bfd_und_section.  */
4369           if (dynamic && definition)
4370             {
4371               if (h->root.type == bfd_link_hash_defined)
4372                 sec = &bfd_und_section;
4373             }
4374
4375           /* Similarly, if we are not looking at a dynamic object, and
4376              we have a definition, we want to override any definition
4377              we may have from a dynamic object.  Symbols from regular
4378              files always take precedence over symbols from dynamic
4379              objects, even if they are defined after the dynamic
4380              object in the link.  */
4381           if (! dynamic
4382               && definition
4383               && h->root.type == bfd_link_hash_defined
4384               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4385               && (bfd_get_flavour (h->root.u.def.section->owner)
4386                   == bfd_target_elf_flavour)
4387               && (elf_elfheader (h->root.u.def.section->owner)->e_type
4388                   == ET_DYN))
4389             {
4390               /* Change the hash table entry to undefined, and let
4391                  _bfd_generic_link_add_one_symbol do the right thing
4392                  with the new definition.  */
4393               h->root.type = bfd_link_hash_undefined;
4394               h->root.u.undef.abfd = h->root.u.def.section->owner;
4395             }
4396         }
4397
4398       if (! (_bfd_generic_link_add_one_symbol
4399              (info, abfd, name, flags, sec, value, (const char *) NULL,
4400               false, collect, (struct bfd_link_hash_entry **) sym_hash)))
4401         goto error_return;
4402
4403       if (dynamic
4404           && definition
4405           && (flags & BSF_WEAK) != 0
4406           && ELF_ST_TYPE (sym.st_info) != STT_FUNC
4407           && (*sym_hash)->weakdef == NULL)
4408         {
4409           /* Keep a list of all weak defined non function symbols from
4410              a dynamic object, using the weakdef field.  Later in this
4411              function we will set the weakdef field to the correct
4412              value.  We only put non-function symbols from dynamic
4413              objects on this list, because that happens to be the only
4414              time we need to know the normal symbol corresponding to a
4415              weak symbol, and the information is time consuming to
4416              figure out.  If the weakdef field is not already NULL,
4417              then this symbol was already defined by some previous
4418              dynamic object, and we will be using that previous
4419              definition anyhow.  */
4420
4421           (*sym_hash)->weakdef = weaks;
4422           weaks = *sym_hash;
4423         }
4424
4425       if (info->hash->creator->flavour == bfd_target_elf_flavour)
4426         {
4427           int old_flags;
4428           boolean dynsym;
4429           int new_flag;
4430
4431           /* Remember the symbol size, type and alignment.  */
4432           if (sym.st_size != 0)
4433             {
4434               /* FIXME: We should probably somehow give a warning if
4435                  the symbol size changes.  */
4436               h->size = sym.st_size;
4437             }
4438           if (sym.st_shndx == SHN_COMMON
4439               && sym.st_value > h->align)
4440             h->align = sym.st_value;
4441           if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE)
4442             {
4443               /* FIXME: We should probably somehow give a warning if
4444                  the symbol type changes.  */
4445               h->type = ELF_ST_TYPE (sym.st_info);
4446             }
4447
4448           /* Set a flag in the hash table entry indicating the type of
4449              reference or definition we just found.  Keep a count of
4450              the number of dynamic symbols we find.  A dynamic symbol
4451              is one which is referenced or defined by both a regular
4452              object and a shared object, or one which is referenced or
4453              defined by more than one shared object.  */
4454           old_flags = h->elf_link_hash_flags;
4455           dynsym = false;
4456           if (! dynamic)
4457             {
4458               if (! definition)
4459                 new_flag = ELF_LINK_HASH_REF_REGULAR;
4460               else
4461                 new_flag = ELF_LINK_HASH_DEF_REGULAR;
4462               if ((old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
4463                                 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
4464                 dynsym = true;
4465             }
4466           else
4467             {
4468               if (! definition)
4469                 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
4470               else
4471                 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
4472               if ((old_flags & new_flag) != 0)
4473                 {
4474                   if (! definition)
4475                     new_flag = ELF_LINK_HASH_REF_DYNAMIC_MULTIPLE;
4476                   else
4477                     new_flag = ELF_LINK_HASH_DEF_DYNAMIC_MULTIPLE;
4478                   dynsym = true;
4479                 }
4480               else
4481                 {
4482                   if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
4483                                     | ELF_LINK_HASH_REF_REGULAR)) != 0)
4484                     dynsym = true;
4485                 }
4486             }
4487
4488           h->elf_link_hash_flags |= new_flag;
4489           if (dynsym && h->dynindx == -1)
4490             {
4491               if (! elf_link_record_dynamic_symbol (info, h))
4492                 goto error_return;
4493             }
4494         }
4495     }
4496
4497   /* Now set the weakdefs field correctly for all the weak defined
4498      symbols we found.  The only way to do this is to search all the
4499      symbols.  Since we only need the information for non functions in
4500      dynamic objects, that's the only time we actually put anything on
4501      the list WEAKS.  We need this information so that if a regular
4502      object refers to a symbol defined weakly in a dynamic object, the
4503      real symbol in the dynamic object is also put in the dynamic
4504      symbols; we also must arrange for both symbols to point to the
4505      same memory location.  We could handle the general case of symbol
4506      aliasing, but a general symbol alias can only be generated in
4507      assembler code, handling it correctly would be very time
4508      consuming, and other ELF linkers don't handle general aliasing
4509      either.  */
4510   while (weaks != NULL)
4511     {
4512       struct elf_link_hash_entry *hlook;
4513       asection *slook;
4514       bfd_vma vlook;
4515       struct elf_link_hash_entry **hpp;
4516       struct elf_link_hash_entry **hppend;
4517
4518       hlook = weaks;
4519       weaks = hlook->weakdef;
4520       hlook->weakdef = NULL;
4521
4522       BFD_ASSERT (hlook->root.type == bfd_link_hash_defined);
4523       slook = hlook->root.u.def.section;
4524       vlook = hlook->root.u.def.value;
4525
4526       hpp = elf_sym_hashes (abfd);
4527       hppend = hpp + extsymcount;
4528       for (; hpp < hppend; hpp++)
4529         {
4530           struct elf_link_hash_entry *h;
4531
4532           h = *hpp;
4533           if (h != hlook
4534               && h->root.type == bfd_link_hash_defined
4535               && h->root.u.def.section == slook
4536               && h->root.u.def.value == vlook)
4537             {
4538               hlook->weakdef = h;
4539
4540               /* If the weak definition is in the list of dynamic
4541                  symbols, make sure the real definition is put there
4542                  as well.  */
4543               if (hlook->dynindx != -1
4544                   && h->dynindx == -1)
4545                 {
4546                   if (! elf_link_record_dynamic_symbol (info, h))
4547                     goto error_return;
4548                 }
4549
4550               break;
4551             }
4552         }
4553     }
4554
4555   if (buf != NULL)
4556     free (buf);
4557
4558   return true;
4559
4560  error_return:
4561   if (buf != NULL)
4562     free (buf);
4563   if (dynbuf != NULL)
4564     free (dynbuf);
4565   return false;
4566 }
4567
4568 /* Create some sections which will be filled in with dynamic linking
4569    information.  The ABFD argument is an input file which is a dynamic
4570    object.  The dynamic sections take up virtual memory space when the
4571    final executable is run, so we need to create them before addresses
4572    are assigned to the output sections.  We work out the actual
4573    contents and size of these sections later.  */
4574
4575 static boolean
4576 elf_link_create_dynamic_sections (abfd, info)
4577      bfd *abfd;
4578      struct bfd_link_info *info;
4579 {
4580   flagword flags;
4581   register asection *s;
4582   struct elf_link_hash_entry *h;
4583   struct elf_backend_data *bed;
4584
4585   /* Note that we set the SEC_IN_MEMORY flag for all of these
4586      sections.  */
4587   flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
4588
4589   s = bfd_make_section (abfd, ".interp");
4590   if (s == NULL
4591       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
4592     return false;
4593
4594   s = bfd_make_section (abfd, ".dynamic");
4595   if (s == NULL
4596       || ! bfd_set_section_flags (abfd, s, flags)
4597       || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
4598     return false;
4599
4600   /* The special symbol _DYNAMIC is always set to the start of the
4601      .dynamic section.  This call occurs before we have processed the
4602      symbols for any dynamic object, so we don't have to worry about
4603      overriding a dynamic definition.  We could set _DYNAMIC in a
4604      linker script, but we only want to define it if we are, in fact,
4605      creating a .dynamic section.  We don't want to define it if there
4606      is no .dynamic section, since on some ELF platforms the start up
4607      code examines it to decide how to initialize the process.  */
4608   h = NULL;
4609   if (! (_bfd_generic_link_add_one_symbol
4610          (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
4611           (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
4612           (struct bfd_link_hash_entry **) &h)))
4613     return false;
4614   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4615
4616   s = bfd_make_section (abfd, ".dynsym");
4617   if (s == NULL
4618       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
4619       || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
4620     return false;
4621
4622   /* The first .dynsym symbol is a dummy.  */
4623   elf_hash_table (info)->dynsymcount = 1;
4624
4625   s = bfd_make_section (abfd, ".dynstr");
4626   if (s == NULL
4627       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
4628     return false;
4629
4630   /* Create a strtab to hold the dynamic symbol names.  */
4631   elf_hash_table (info)->dynstr = bfd_new_strtab (abfd);
4632   if (elf_hash_table (info)->dynstr == NULL)
4633     return false;
4634
4635   s = bfd_make_section (abfd, ".hash");
4636   if (s == NULL
4637       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
4638       || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
4639     return false;
4640
4641   /* Let the backend create the rest of the sections.  This lets the
4642      backend set the right flags.  The backend will normally create
4643      the .got and .plt sections.  */
4644   bed = get_elf_backend_data (abfd);
4645   return (*bed->elf_backend_create_dynamic_sections) (abfd, info);
4646 }
4647
4648 /* Add an entry to the .dynamic table.  */
4649
4650 boolean
4651 elf_add_dynamic_entry (info, tag, val)
4652      struct bfd_link_info *info;
4653      bfd_vma tag;
4654      bfd_vma val;
4655 {
4656   Elf_Internal_Dyn dyn;
4657   bfd *dynobj;
4658   asection *s;
4659   size_t newsize;
4660   bfd_byte *newcontents;
4661
4662   dynobj = elf_hash_table (info)->dynobj;
4663
4664   s = bfd_get_section_by_name (dynobj, ".dynamic");
4665   BFD_ASSERT (s != NULL);
4666
4667   newsize = s->_raw_size + sizeof (Elf_External_Dyn);
4668   if (s->contents == NULL)
4669     newcontents = (bfd_byte *) malloc (newsize);
4670   else
4671     newcontents = (bfd_byte *) realloc (s->contents, newsize);
4672   if (newcontents == NULL)
4673     {
4674       bfd_set_error (bfd_error_no_memory);
4675       return false;
4676     }
4677
4678   dyn.d_tag = tag;
4679   dyn.d_un.d_val = val;
4680   elf_swap_dyn_out (dynobj, &dyn,
4681                     (Elf_External_Dyn *) (newcontents + s->_raw_size));
4682
4683   s->_raw_size = newsize;
4684   s->contents = newcontents;
4685
4686   return true;
4687 }
4688
4689 /* Record an assignment to a symbol made by a linker script.  We need
4690    this in case some dynamic object refers to this symbol.  */
4691
4692 /*ARGSUSED*/
4693 boolean
4694 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name)
4695      bfd *output_bfd;
4696      struct bfd_link_info *info;
4697      const char *name;
4698 {
4699   struct elf_link_hash_entry *h;
4700
4701   /* This is called after we have examined all the input objects.  If
4702      the symbol does not exist, it merely means that no object refers
4703      to it, and we can just ignore it at this point.  */
4704   h = elf_link_hash_lookup (elf_hash_table (info), name,
4705                             false, false, false);
4706   if (h == NULL)
4707     return true;
4708
4709   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4710
4711   if ((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
4712                                  | ELF_LINK_HASH_REF_DYNAMIC)) != 0
4713       && h->dynindx == -1)
4714     {
4715       if (! elf_link_record_dynamic_symbol (info, h))
4716         return false;
4717
4718       /* If this is a weak defined symbol, and we know a corresponding
4719          real symbol from the same dynamic object, make sure the real
4720          symbol is also made into a dynamic symbol.  */
4721       if (h->weakdef != NULL
4722           && h->weakdef->dynindx == -1)
4723         {
4724           if (! elf_link_record_dynamic_symbol (info, h->weakdef))
4725             return false;
4726         }
4727     }
4728
4729   return true;
4730 }
4731
4732 /* Array used to determine the number of hash table buckets to use
4733    based on the number of symbols there are.  If there are fewer than
4734    3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4735    fewer than 37 we use 17 buckets, and so forth.  We never use more
4736    than 521 buckets.  */
4737
4738 static const size_t elf_buckets[] =
4739 {
4740   1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 0
4741 };
4742
4743 /* Set up the sizes and contents of the ELF dynamic sections.  This is
4744    called by the ELF linker emulation before_allocation routine.  We
4745    must set the sizes of the sections before the linker sets the
4746    addresses of the various sections.  */
4747
4748 boolean
4749 NAME(bfd_elf,size_dynamic_sections) (output_bfd, info)
4750      bfd *output_bfd;
4751      struct bfd_link_info *info;
4752 {
4753   bfd *dynobj;
4754   size_t dynsymcount;
4755   asection *s;
4756   Elf_Internal_Sym isym;
4757   size_t i;
4758   size_t bucketcount;
4759   struct elf_backend_data *bed;
4760
4761   dynobj = elf_hash_table (info)->dynobj;
4762   dynsymcount = elf_hash_table (info)->dynsymcount;
4763
4764   /* If there were no dynamic objects in the link, there is nothing to
4765      do here.  */
4766   if (dynobj == NULL)
4767     return true;
4768
4769   /* Set the size of the .dynsym and .hash sections.  We counted the
4770      number of dynamic symbols in elf_link_add_object_symbols.  We
4771      will build the contents of .dynsym and .hash when we build the
4772      final symbol table, because until then we do not know the correct
4773      value to give the symbols.  We built the .dynstr section as we
4774      went along in elf_link_add_object_symbols.  */
4775   s = bfd_get_section_by_name (dynobj, ".dynsym");
4776   BFD_ASSERT (s != NULL);
4777   s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
4778   s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
4779   if (s->contents == NULL && s->_raw_size != 0)
4780     {
4781       bfd_set_error (bfd_error_no_memory);
4782       return false;
4783     }
4784
4785   /* The first entry in .dynsym is a dummy symbol.  */
4786   isym.st_value = 0;
4787   isym.st_size = 0;
4788   isym.st_name = 0;
4789   isym.st_info = 0;
4790   isym.st_other = 0;
4791   isym.st_shndx = 0;
4792   elf_swap_symbol_out (output_bfd, &isym,
4793                        (Elf_External_Sym *) s->contents);
4794
4795   for (i = 0; elf_buckets[i] != 0; i++)
4796     {
4797       bucketcount = elf_buckets[i];
4798       if (dynsymcount < elf_buckets[i + 1])
4799         break;
4800     }
4801
4802   s = bfd_get_section_by_name (dynobj, ".hash");
4803   BFD_ASSERT (s != NULL);
4804   s->_raw_size = (2 + bucketcount + dynsymcount) * (ARCH_SIZE / 8);
4805   s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
4806   if (s->contents == NULL)
4807     {
4808       bfd_set_error (bfd_error_no_memory);
4809       return false;
4810     }
4811   memset (s->contents, 0, s->_raw_size);
4812
4813   put_word (output_bfd, bucketcount, s->contents);
4814   put_word (output_bfd, dynsymcount, s->contents + (ARCH_SIZE / 8));
4815
4816   elf_hash_table (info)->bucketcount = bucketcount;
4817
4818   s = bfd_get_section_by_name (dynobj, ".dynstr");
4819   BFD_ASSERT (s != NULL);
4820   s->_raw_size = elf_hash_table (info)->dynstr->length;
4821   s->contents = (unsigned char *) elf_hash_table (info)->dynstr->tab;
4822
4823   /* Find all symbols which were defined in a dynamic object and make
4824      the backend pick a reasonable value for them.  */
4825   elf_link_hash_traverse (elf_hash_table (info),
4826                           elf_adjust_dynamic_symbol,
4827                           (PTR) info);
4828
4829   /* Add some entries to the .dynamic section.  We fill in some of the
4830      values later, in elf_bfd_final_link, but we must add the entries
4831      now so that we know the final size of the .dynamic section.  */
4832   if (bfd_get_section_by_name (output_bfd, ".init") != NULL)
4833     {
4834       if (! elf_add_dynamic_entry (info, DT_INIT, 0))
4835         return false;
4836     }
4837   if (bfd_get_section_by_name (output_bfd, ".fini") != NULL)
4838     {
4839       if (! elf_add_dynamic_entry (info, DT_FINI, 0))
4840         return false;
4841     }
4842   if (! elf_add_dynamic_entry (info, DT_HASH, 0)
4843       || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
4844       || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
4845       || ! elf_add_dynamic_entry (info, DT_STRSZ,
4846                                   elf_hash_table (info)->dynstr->length)
4847       || ! elf_add_dynamic_entry (info, DT_SYMENT,
4848                                   sizeof (Elf_External_Sym)))
4849     return false;
4850
4851   /* The backend must work out the sizes of all the other dynamic
4852      sections.  */
4853   bed = get_elf_backend_data (output_bfd);
4854   if (! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
4855     return false;
4856
4857   return elf_add_dynamic_entry (info, DT_NULL, 0);
4858 }
4859
4860 /* Make the backend pick a good value for a dynamic symbol.  This is
4861    called via elf_link_hash_traverse, and also calls itself
4862    recursively.  */
4863
4864 static boolean
4865 elf_adjust_dynamic_symbol (h, data)
4866      struct elf_link_hash_entry *h;
4867      PTR data;
4868 {
4869   struct bfd_link_info *info = (struct bfd_link_info *) data;
4870   bfd *dynobj;
4871   struct elf_backend_data *bed;
4872
4873   /* If this symbol is not defined by a dynamic object, or is not
4874      referenced by a regular object, ignore it.  FIXME: Do we need to
4875      worry about symbols which are defined by one dynamic object and
4876      referenced by another one?  */
4877   if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4878       || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4879       || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4880     return true;
4881
4882   /* If we've already adjusted this symbol, don't do it again.  This
4883      can happen via a recursive call.  */
4884   if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
4885     return true;
4886
4887   /* Don't look at this symbol again.  Note that we must set this
4888      after checking the above conditions, because we may look at a
4889      symbol once, decide not to do anything, and then get called
4890      recursively later after REF_REGULAR is set below.  */
4891   h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
4892
4893   /* If this is a weak definition, and we know a real definition, and
4894      the real symbol is not itself defined by a regular object file,
4895      then get a good value for the real definition.  We handle the
4896      real symbol first, for the convenience of the backend routine.
4897
4898      Note that there is a confusing case here.  If the real definition
4899      is defined by a regular object file, we don't get the real symbol
4900      from the dynamic object, but we do get the weak symbol.  If the
4901      processor backend uses a COPY reloc, then if some routine in the
4902      dynamic object changes the real symbol, we will not see that
4903      change in the corresponding weak symbol.  This is the way other
4904      ELF linkers work as well, and seems to be a result of the shared
4905      library model.
4906
4907      I will clarify this issue.  Most SVR4 shared libraries define the
4908      variable _timezone and define timezone as a weak synonym.  The
4909      tzset call changes _timezone.  If you write
4910        extern int timezone;
4911        int _timezone = 5;
4912        int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4913      you might expect that, since timezone is a synonym for _timezone,
4914      the same number will print both times.  However, if the processor
4915      backend uses a COPY reloc, then actually timezone will be copied
4916      into your process image, and, since you define _timezone
4917      yourself, _timezone will not.  Thus timezone and _timezone will
4918      wind up at different memory locations.  The tzset call will set
4919      _timezone, leaving timezone unchanged.  */
4920
4921   if (h->weakdef != NULL)
4922     {
4923       struct elf_link_hash_entry *weakdef;
4924
4925       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
4926       weakdef = h->weakdef;
4927       BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined);
4928       BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
4929       if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4930           || (weakdef->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4931         {
4932           /* This symbol is defined or referenced by a regular object
4933              file, so we will not do anything special.  Clear weakdef
4934              for the convenience of the processor backend.  */
4935           h->weakdef = NULL;
4936         }
4937       else
4938         {
4939           /* There is an implicit reference by a regular object file
4940              via the weak symbol.  */
4941           weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
4942           if (! elf_adjust_dynamic_symbol (weakdef, (PTR) info))
4943             return false;
4944         }
4945     }
4946
4947   dynobj = elf_hash_table (info)->dynobj;
4948   bed = get_elf_backend_data (dynobj);
4949   if (! (*bed->elf_backend_adjust_dynamic_symbol) (info, h))
4950     {
4951       /* FIXME: No way to return error.  */
4952       abort ();
4953     }
4954
4955   return true;
4956 }
4957 \f
4958 /* Final phase of ELF linker.  */
4959
4960 /* A structure we use to avoid passing large numbers of arguments.  */
4961
4962 struct elf_final_link_info
4963 {
4964   /* General link information.  */
4965   struct bfd_link_info *info;
4966   /* Output BFD.  */
4967   bfd *output_bfd;
4968   /* Symbol string table.  */
4969   struct strtab *symstrtab;
4970   /* .dynsym section.  */
4971   asection *dynsym_sec;
4972   /* .hash section.  */
4973   asection *hash_sec;
4974   /* Buffer large enough to hold contents of any section.  */
4975   bfd_byte *contents;
4976   /* Buffer large enough to hold external relocs of any section.  */
4977   PTR external_relocs;
4978   /* Buffer large enough to hold internal relocs of any section.  */
4979   Elf_Internal_Rela *internal_relocs;
4980   /* Buffer large enough to hold external local symbols of any input
4981      BFD.  */
4982   Elf_External_Sym *external_syms;
4983   /* Buffer large enough to hold internal local symbols of any input
4984      BFD.  */
4985   Elf_Internal_Sym *internal_syms;
4986   /* Array large enough to hold a symbol index for each local symbol
4987      of any input BFD.  */
4988   long *indices;
4989   /* Array large enough to hold a section pointer for each local
4990      symbol of any input BFD.  */
4991   asection **sections;
4992   /* Buffer to hold swapped out symbols.  */
4993   Elf_External_Sym *symbuf;
4994   /* Number of swapped out symbols in buffer.  */
4995   size_t symbuf_count;
4996   /* Number of symbols which fit in symbuf.  */
4997   size_t symbuf_size;
4998 };
4999
5000 static boolean elf_link_output_sym
5001   PARAMS ((struct elf_final_link_info *, const char *, Elf_Internal_Sym *));
5002 static boolean elf_link_flush_output_syms
5003   PARAMS ((struct elf_final_link_info *));
5004 static boolean elf_link_output_extsym
5005   PARAMS ((struct elf_link_hash_entry *, PTR));
5006 static boolean elf_link_input_bfd
5007   PARAMS ((struct elf_final_link_info *, bfd *));
5008 static boolean elf_reloc_link_order
5009   PARAMS ((bfd *, struct bfd_link_info *, asection *,
5010            struct bfd_link_order *));
5011
5012 /* Do the final step of an ELF link.  */
5013
5014 boolean
5015 elf_bfd_final_link (abfd, info)
5016      bfd *abfd;
5017      struct bfd_link_info *info;
5018 {
5019   bfd *dynobj;
5020   struct elf_final_link_info finfo;
5021   register asection *o;
5022   register struct bfd_link_order *p;
5023   register bfd *sub;
5024   size_t max_contents_size;
5025   size_t max_external_reloc_size;
5026   size_t max_internal_reloc_count;
5027   size_t max_sym_count;
5028   file_ptr off;
5029   Elf_Internal_Sym elfsym;
5030   unsigned int i;
5031   Elf_Internal_Shdr *symtab_hdr;
5032   Elf_Internal_Shdr *symstrtab_hdr;
5033
5034   dynobj = elf_hash_table (info)->dynobj;
5035
5036   finfo.info = info;
5037   finfo.output_bfd = abfd;
5038   finfo.symstrtab = bfd_new_strtab (abfd);
5039   if (finfo.symstrtab == NULL)
5040     return false;
5041   if (dynobj == NULL)
5042     {
5043       finfo.dynsym_sec = NULL;
5044       finfo.hash_sec = NULL;
5045     }
5046   else
5047     {
5048       finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
5049       finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
5050       if (finfo.dynsym_sec == NULL
5051           || finfo.hash_sec == NULL)
5052         abort ();
5053     }
5054   finfo.contents = NULL;
5055   finfo.external_relocs = NULL;
5056   finfo.internal_relocs = NULL;
5057   finfo.external_syms = NULL;
5058   finfo.internal_syms = NULL;
5059   finfo.indices = NULL;
5060   finfo.sections = NULL;
5061   finfo.symbuf = NULL;
5062   finfo.symbuf_count = 0;
5063
5064   /* Count up the number of relocations we will output for each output
5065      section, so that we know the sizes of the reloc sections.  We
5066      also figure out some maximum sizes.  */
5067   max_contents_size = 0;
5068   max_external_reloc_size = 0;
5069   max_internal_reloc_count = 0;
5070   max_sym_count = 0;
5071   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
5072     {
5073       o->reloc_count = 0;
5074
5075       for (p = o->link_order_head; p != NULL; p = p->next)
5076         {
5077           if (p->type == bfd_section_reloc_link_order
5078               || p->type == bfd_symbol_reloc_link_order)
5079             ++o->reloc_count;
5080           else if (p->type == bfd_indirect_link_order)
5081             {
5082               asection *sec;
5083
5084               sec = p->u.indirect.section;
5085
5086               if (info->relocateable)
5087                 o->reloc_count += sec->reloc_count;
5088
5089               if (sec->_raw_size > max_contents_size)
5090                 max_contents_size = sec->_raw_size;
5091               if (sec->_cooked_size > max_contents_size)
5092                 max_contents_size = sec->_cooked_size;
5093
5094               /* We are interested in just local symbols, not all
5095                  symbols.  */
5096               if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
5097                   && (elf_tdata (sec->owner)->symtab_hdr.sh_info
5098                       > max_sym_count))
5099                 max_sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
5100
5101               if ((sec->flags & SEC_RELOC) != 0)
5102                 {
5103                   size_t ext_size;
5104
5105                   ext_size = elf_section_data (sec)->rel_hdr.sh_size;
5106                   if (ext_size > max_external_reloc_size)
5107                     max_external_reloc_size = ext_size;
5108                   if (sec->reloc_count > max_internal_reloc_count)
5109                     max_internal_reloc_count = sec->reloc_count;
5110                 }
5111             }
5112         }
5113
5114       if (o->reloc_count > 0)
5115         o->flags |= SEC_RELOC;
5116       else
5117         {
5118           /* Explicitly clear the SEC_RELOC flag.  The linker tends to
5119              set it (this is probably a bug) and if it is set
5120              assign_section_numbers will create a reloc section.  */
5121           o->flags &=~ SEC_RELOC;
5122         }
5123     }
5124
5125   /* Figure out the file positions for everything but the symbol table
5126      and the relocs.  We set symcount to force assign_section_numbers
5127      to create a symbol table.  */
5128   abfd->symcount = info->strip == strip_all ? 0 : 1;
5129   BFD_ASSERT (! abfd->output_has_begun);
5130   if (! elf_compute_section_file_positions (abfd, info))
5131     goto error_return;
5132
5133   /* That created the reloc sections.  Set their sizes, and assign
5134      them file positions, and allocate some buffers.  */
5135   for (o = abfd->sections; o != NULL; o = o->next)
5136     {
5137       if ((o->flags & SEC_RELOC) != 0)
5138         {
5139           Elf_Internal_Shdr *rel_hdr;
5140           register struct elf_link_hash_entry **p, **pend;
5141
5142           rel_hdr = &elf_section_data (o)->rel_hdr;
5143
5144           rel_hdr->sh_size = rel_hdr->sh_entsize * o->reloc_count;
5145
5146           /* The contents field must last into write_object_contents,
5147              so we allocate it with bfd_alloc rather than malloc.  */
5148           rel_hdr->contents = (PTR) bfd_alloc (abfd, rel_hdr->sh_size);
5149           if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
5150             {
5151               bfd_set_error (bfd_error_no_memory);
5152               goto error_return;
5153             }
5154
5155           p = ((struct elf_link_hash_entry **)
5156                malloc (o->reloc_count
5157                        * sizeof (struct elf_link_hash_entry *)));
5158           if (p == NULL && o->reloc_count != 0)
5159             {
5160               bfd_set_error (bfd_error_no_memory);
5161               goto error_return;
5162             }
5163           elf_section_data (o)->rel_hashes = p;
5164           pend = p + o->reloc_count;
5165           for (; p < pend; p++)
5166             *p = NULL;
5167
5168           /* Use the reloc_count field as an index when outputting the
5169              relocs.  */
5170           o->reloc_count = 0;
5171         }
5172     }
5173
5174   assign_file_positions_for_relocs (abfd);
5175
5176   /* We have now assigned file positions for all the sections except
5177      .symtab and .strtab.  We start the .symtab section at the current
5178      file position, and write directly to it.  We build the .strtab
5179      section in memory.  When we add .dynsym support, we will build
5180      that in memory as well (.dynsym is smaller than .symtab).  */
5181   abfd->symcount = 0;
5182   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5183   /* sh_name is set in prep_headers.  */
5184   symtab_hdr->sh_type = SHT_SYMTAB;
5185   symtab_hdr->sh_flags = 0;
5186   symtab_hdr->sh_addr = 0;
5187   symtab_hdr->sh_size = 0;
5188   symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
5189   /* sh_link is set in assign_section_numbers.  */
5190   /* sh_info is set below.  */
5191   /* sh_offset is set just below.  */
5192   symtab_hdr->sh_addralign = 4;  /* FIXME: system dependent?  */
5193
5194   off = elf_tdata (abfd)->next_file_pos;
5195   off = assign_file_position_for_section (symtab_hdr, off, true);
5196
5197   /* Note that at this point elf_tdata (abfd)->next_file_pos is
5198      incorrect.  We do not yet know the size of the .symtab section.
5199      We correct next_file_pos below, after we do know the size.  */
5200
5201   /* Allocate a buffer to hold swapped out symbols.  This is to avoid
5202      continuously seeking to the right position in the file.  */
5203   if (! info->keep_memory || max_sym_count < 20)
5204     finfo.symbuf_size = 20;
5205   else
5206     finfo.symbuf_size = max_sym_count;
5207   finfo.symbuf = ((Elf_External_Sym *)
5208                   malloc (finfo.symbuf_size * sizeof (Elf_External_Sym)));
5209   if (finfo.symbuf == NULL)
5210     {
5211       bfd_set_error (bfd_error_no_memory);
5212       goto error_return;
5213     }
5214
5215   /* Start writing out the symbol table.  The first symbol is always a
5216      dummy symbol.  */
5217   elfsym.st_value = 0;
5218   elfsym.st_size = 0;
5219   elfsym.st_info = 0;
5220   elfsym.st_other = 0;
5221   elfsym.st_shndx = SHN_UNDEF;
5222   if (! elf_link_output_sym (&finfo, (const char *) NULL, &elfsym))
5223     goto error_return;
5224
5225 #if 0
5226   /* Some standard ELF linkers do this, but we don't because it causes
5227      bootstrap comparison failures.  */
5228   /* Output a file symbol for the output file as the second symbol.
5229      We output this even if we are discarding local symbols, although
5230      I'm not sure if this is correct.  */
5231   elfsym.st_value = 0;
5232   elfsym.st_size = 0;
5233   elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5234   elfsym.st_other = 0;
5235   elfsym.st_shndx = SHN_ABS;
5236   if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd), &elfsym))
5237     goto error_return;
5238 #endif
5239
5240   /* Output a symbol for each section.  We output these even if we are
5241      discarding local symbols, since they are used for relocs.  These
5242      symbols have no names.  We store the index of each one in the
5243      index field of the section, so that we can find it again when
5244      outputting relocs.  */
5245   elfsym.st_value = 0;
5246   elfsym.st_size = 0;
5247   elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5248   elfsym.st_other = 0;
5249   for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
5250     {
5251       o = section_from_elf_index (abfd, i);
5252       if (o != &bfd_abs_section)
5253         o->target_index = abfd->symcount;
5254       elfsym.st_shndx = i;
5255       if (! elf_link_output_sym (&finfo, (const char *) NULL, &elfsym))
5256         goto error_return;
5257     }
5258
5259   /* Allocate some memory to hold information read in from the input
5260      files.  */
5261   finfo.contents = (bfd_byte *) malloc (max_contents_size);
5262   finfo.external_relocs = (PTR) malloc (max_external_reloc_size);
5263   finfo.internal_relocs = ((Elf_Internal_Rela *)
5264                            malloc (max_internal_reloc_count
5265                                    * sizeof (Elf_Internal_Rela)));
5266   finfo.external_syms = ((Elf_External_Sym *)
5267                          malloc (max_sym_count * sizeof (Elf_External_Sym)));
5268   finfo.internal_syms = ((Elf_Internal_Sym *)
5269                          malloc (max_sym_count * sizeof (Elf_Internal_Sym)));
5270   finfo.indices = (long *) malloc (max_sym_count * sizeof (long));
5271   finfo.sections = (asection **) malloc (max_sym_count * sizeof (asection *));
5272   if ((finfo.contents == NULL && max_contents_size != 0)
5273       || (finfo.external_relocs == NULL && max_external_reloc_size != 0)
5274       || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0)
5275       || (finfo.external_syms == NULL && max_sym_count != 0)
5276       || (finfo.internal_syms == NULL && max_sym_count != 0)
5277       || (finfo.indices == NULL && max_sym_count != 0)
5278       || (finfo.sections == NULL && max_sym_count != 0))
5279     {
5280       bfd_set_error (bfd_error_no_memory);
5281       goto error_return;
5282     }
5283
5284   /* Since ELF permits relocations to be against local symbols, we
5285      must have the local symbols available when we do the relocations.
5286      Since we would rather only read the local symbols once, and we
5287      would rather not keep them in memory, we handle all the
5288      relocations for a single input file at the same time.
5289
5290      Unfortunately, there is no way to know the total number of local
5291      symbols until we have seen all of them, and the local symbol
5292      indices precede the global symbol indices.  This means that when
5293      we are generating relocateable output, and we see a reloc against
5294      a global symbol, we can not know the symbol index until we have
5295      finished examining all the local symbols to see which ones we are
5296      going to output.  To deal with this, we keep the relocations in
5297      memory, and don't output them until the end of the link.  This is
5298      an unfortunate waste of memory, but I don't see a good way around
5299      it.  Fortunately, it only happens when performing a relocateable
5300      link, which is not the common case.  FIXME: If keep_memory is set
5301      we could write the relocs out and then read them again; I don't
5302      know how bad the memory loss will be.  */
5303
5304   for (sub = info->input_bfds; sub != NULL; sub = sub->next)
5305     sub->output_has_begun = false;
5306   for (o = abfd->sections; o != NULL; o = o->next)
5307     {
5308       for (p = o->link_order_head; p != NULL; p = p->next)
5309         {
5310           if (p->type == bfd_indirect_link_order
5311               && (bfd_get_flavour (p->u.indirect.section->owner)
5312                   == bfd_target_elf_flavour))
5313             {
5314               sub = p->u.indirect.section->owner;
5315               if (! sub->output_has_begun)
5316                 {
5317                   if (! elf_link_input_bfd (&finfo, sub))
5318                     goto error_return;
5319                   sub->output_has_begun = true;
5320                 }
5321             }
5322           else if (p->type == bfd_section_reloc_link_order
5323                    || p->type == bfd_symbol_reloc_link_order)
5324             {
5325               if (! elf_reloc_link_order (abfd, info, o, p))
5326                 goto error_return;
5327             }
5328           else
5329             {
5330               if (! _bfd_default_link_order (abfd, info, o, p))
5331                 goto error_return;
5332             }
5333         }
5334     }
5335
5336   /* That wrote out all the local symbols.  Finish up the symbol table
5337      with the global symbols.  */
5338
5339   /* The sh_info field records the index of the first non local
5340      symbol.  */
5341   symtab_hdr->sh_info = abfd->symcount;
5342   if (dynobj != NULL)
5343     elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = 1;
5344
5345   /* We get the global symbols from the hash table.  */
5346   elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5347                           (PTR) &finfo);
5348
5349   /* Flush all symbols to the file.  */
5350   if (! elf_link_flush_output_syms (&finfo))
5351     return false;
5352
5353   /* Now we know the size of the symtab section.  */
5354   off += symtab_hdr->sh_size;
5355
5356   /* Finish up the symbol string table (.strtab) section.  */
5357   symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5358   /* sh_name was set in prep_headers.  */
5359   symstrtab_hdr->sh_type = SHT_STRTAB;
5360   symstrtab_hdr->sh_flags = 0;
5361   symstrtab_hdr->sh_addr = 0;
5362   symstrtab_hdr->sh_size = finfo.symstrtab->length;
5363   symstrtab_hdr->sh_entsize = 0;
5364   symstrtab_hdr->sh_link = 0;
5365   symstrtab_hdr->sh_info = 0;
5366   /* sh_offset is set just below.  */
5367   symstrtab_hdr->sh_addralign = 1;
5368   symstrtab_hdr->contents = (PTR) finfo.symstrtab->tab;
5369
5370   off = assign_file_position_for_section (symstrtab_hdr, off, true);
5371   elf_tdata (abfd)->next_file_pos = off;
5372
5373   /* Adjust the relocs to have the correct symbol indices.  */
5374   for (o = abfd->sections; o != NULL; o = o->next)
5375     {
5376       struct elf_link_hash_entry **rel_hash;
5377       Elf_Internal_Shdr *rel_hdr;
5378
5379       if ((o->flags & SEC_RELOC) == 0)
5380         continue;
5381
5382       rel_hash = elf_section_data (o)->rel_hashes;
5383       rel_hdr = &elf_section_data (o)->rel_hdr;
5384       for (i = 0; i < o->reloc_count; i++, rel_hash++)
5385         {
5386           if (*rel_hash == NULL)
5387             continue;
5388               
5389           BFD_ASSERT ((*rel_hash)->indx >= 0);
5390
5391           if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
5392             {
5393               Elf_External_Rel *erel;
5394               Elf_Internal_Rel irel;
5395
5396               erel = (Elf_External_Rel *) rel_hdr->contents + i;
5397               elf_swap_reloc_in (abfd, erel, &irel);
5398               irel.r_info = ELF_R_INFO ((*rel_hash)->indx,
5399                                         ELF_R_TYPE (irel.r_info));
5400               elf_swap_reloc_out (abfd, &irel, erel);
5401             }
5402           else
5403             {
5404               Elf_External_Rela *erela;
5405               Elf_Internal_Rela irela;
5406
5407               BFD_ASSERT (rel_hdr->sh_entsize
5408                           == sizeof (Elf_External_Rela));
5409
5410               erela = (Elf_External_Rela *) rel_hdr->contents + i;
5411               elf_swap_reloca_in (abfd, erela, &irela);
5412               irela.r_info = ELF_R_INFO ((*rel_hash)->indx,
5413                                          ELF_R_TYPE (irela.r_info));
5414               elf_swap_reloca_out (abfd, &irela, erela);
5415             }
5416         }
5417
5418       /* Set the reloc_count field to 0 to prevent write_relocs from
5419          trying to swap the relocs out itself.  */
5420       o->reloc_count = 0;
5421     }
5422
5423   /* If we are linking against a dynamic object, finish up the dynamic
5424      linking information.  */
5425   if (dynobj != NULL)
5426     {
5427       Elf_External_Dyn *dyncon, *dynconend;
5428       struct elf_backend_data *bed;
5429
5430       /* Fix up .dynamic entries.  */
5431       o = bfd_get_section_by_name (dynobj, ".dynamic");
5432       BFD_ASSERT (o != NULL);
5433
5434       dyncon = (Elf_External_Dyn *) o->contents;
5435       dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5436       for (; dyncon < dynconend; dyncon++)
5437         {
5438           Elf_Internal_Dyn dyn;
5439           const char *name;
5440           unsigned int type;
5441
5442           elf_swap_dyn_in (dynobj, dyncon, &dyn);
5443
5444           switch (dyn.d_tag)
5445             {
5446             default:
5447               break;
5448
5449             case DT_INIT:
5450               name = ".init";
5451               goto get_vma;
5452             case DT_FINI:
5453               name = ".fini";
5454               goto get_vma;
5455             case DT_HASH:
5456               name = ".hash";
5457               goto get_vma;
5458             case DT_STRTAB:
5459               name = ".dynstr";
5460               goto get_vma;
5461             case DT_SYMTAB:
5462               name = ".dynsym";
5463             get_vma:
5464               o = bfd_get_section_by_name (abfd, name);
5465               BFD_ASSERT (o != NULL);
5466               dyn.d_un.d_ptr = o->vma;
5467               elf_swap_dyn_out (dynobj, &dyn, dyncon);
5468               break;
5469
5470             case DT_REL:
5471             case DT_RELA:
5472             case DT_RELSZ:
5473             case DT_RELASZ:
5474               if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5475                 type = SHT_REL;
5476               else
5477                 type = SHT_RELA;
5478               dyn.d_un.d_val = 0;
5479               for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
5480                 {
5481                   Elf_Internal_Shdr *hdr;
5482
5483                   hdr = elf_elfsections (abfd)[i];
5484                   if (hdr->sh_type == type
5485                       && (hdr->sh_flags & SHF_ALLOC) != 0)
5486                     {
5487                       if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5488                         dyn.d_un.d_val += hdr->sh_size;
5489                       else
5490                         {
5491                           if (dyn.d_un.d_val == 0
5492                               || hdr->sh_addr < dyn.d_un.d_val)
5493                             dyn.d_un.d_val = hdr->sh_addr;
5494                         }
5495                     }
5496                 }
5497               elf_swap_dyn_out (dynobj, &dyn, dyncon);
5498               break;
5499             }
5500         }
5501
5502       bed = get_elf_backend_data (abfd);
5503       if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5504         goto error_return;
5505
5506       for (o = dynobj->sections; o != NULL; o = o->next)
5507         {
5508           Elf_Internal_Shdr *hdr;
5509           asection *s;
5510           const char *name;
5511
5512           if ((o->flags & SEC_HAS_CONTENTS) == 0)
5513             continue;
5514           BFD_ASSERT ((o->flags & SEC_IN_MEMORY) != 0);
5515           if (! bfd_set_section_contents (abfd, o->output_section,
5516                                           o->contents, o->output_offset,
5517                                           o->_raw_size))
5518             goto error_return;
5519
5520           hdr = &elf_section_data (o->output_section)->this_hdr;
5521           switch (hdr->sh_type)
5522             {
5523             case SHT_DYNAMIC:
5524               s = bfd_get_section_by_name (abfd, ".dynstr");
5525               hdr->sh_link = elf_section_data (s)->this_idx;
5526               hdr->sh_entsize = sizeof (Elf_External_Dyn);
5527               break;
5528
5529             case SHT_HASH:
5530               s = finfo.dynsym_sec->output_section;
5531               hdr->sh_link = elf_section_data (s)->this_idx;
5532               hdr->sh_entsize = ARCH_SIZE / 8;
5533               break;
5534
5535             case SHT_DYNSYM:
5536               s = bfd_get_section_by_name (abfd, ".dynstr");
5537               hdr->sh_link = elf_section_data (s)->this_idx;
5538               hdr->sh_entsize = sizeof (Elf_External_Sym);
5539               break;
5540
5541             case SHT_REL:
5542             case SHT_RELA:
5543               name = bfd_get_section_name (abfd, o->output_section);
5544               s = finfo.dynsym_sec->output_section;
5545               hdr->sh_link = elf_section_data (s)->this_idx;
5546               if (hdr->sh_type == SHT_REL)
5547                 {
5548                   name += 4;
5549                   hdr->sh_entsize = sizeof (Elf_External_Rel);
5550                 }
5551               else
5552                 {
5553                   name += 5;
5554                   hdr->sh_entsize = sizeof (Elf_External_Rela);
5555                 }
5556               s = bfd_get_section_by_name (abfd, name);
5557               if (s != NULL)
5558                 hdr->sh_info = elf_section_data (s)->this_idx;
5559               break;
5560             }
5561         }
5562     }
5563
5564   if (finfo.contents != NULL)
5565     free (finfo.contents);
5566   if (finfo.external_relocs != NULL)
5567     free (finfo.external_relocs);
5568   if (finfo.internal_relocs != NULL)
5569     free (finfo.internal_relocs);
5570   if (finfo.external_syms != NULL)
5571     free (finfo.external_syms);
5572   if (finfo.internal_syms != NULL)
5573     free (finfo.internal_syms);
5574   if (finfo.indices != NULL)
5575     free (finfo.indices);
5576   if (finfo.sections != NULL)
5577     free (finfo.sections);
5578   if (finfo.symbuf != NULL)
5579     free (finfo.symbuf);
5580   for (o = abfd->sections; o != NULL; o = o->next)
5581     {
5582       if ((o->flags & SEC_RELOC) != 0
5583           && elf_section_data (o)->rel_hashes != NULL)
5584         free (elf_section_data (o)->rel_hashes);
5585     }
5586
5587   return true;
5588
5589  error_return:
5590   if (finfo.contents != NULL)
5591     free (finfo.contents);
5592   if (finfo.external_relocs != NULL)
5593     free (finfo.external_relocs);
5594   if (finfo.internal_relocs != NULL)
5595     free (finfo.internal_relocs);
5596   if (finfo.external_syms != NULL)
5597     free (finfo.external_syms);
5598   if (finfo.internal_syms != NULL)
5599     free (finfo.internal_syms);
5600   if (finfo.indices != NULL)
5601     free (finfo.indices);
5602   if (finfo.sections != NULL)
5603     free (finfo.sections);
5604   if (finfo.symbuf != NULL)
5605     free (finfo.symbuf);
5606   for (o = abfd->sections; o != NULL; o = o->next)
5607     {
5608       if ((o->flags & SEC_RELOC) != 0
5609           && elf_section_data (o)->rel_hashes != NULL)
5610         free (elf_section_data (o)->rel_hashes);
5611     }
5612
5613   return false;
5614 }
5615
5616 /* Add a symbol to the output symbol table.  */
5617
5618 static boolean
5619 elf_link_output_sym (finfo, name, elfsym)
5620      struct elf_final_link_info *finfo;
5621      const char *name;
5622      Elf_Internal_Sym *elfsym;
5623 {
5624   if (name == (const char *) NULL || *name == '\0')
5625     elfsym->st_name = 0;
5626   else
5627     {
5628       elfsym->st_name = bfd_add_to_strtab (finfo->output_bfd,
5629                                            finfo->symstrtab, name);
5630       if (elfsym->st_name == (unsigned long) -1)
5631         return false;
5632     }
5633
5634   if (finfo->symbuf_count >= finfo->symbuf_size)
5635     {
5636       if (! elf_link_flush_output_syms (finfo))
5637         return false;
5638     }
5639
5640   elf_swap_symbol_out (finfo->output_bfd, elfsym,
5641                        finfo->symbuf + finfo->symbuf_count);
5642   ++finfo->symbuf_count;
5643
5644   ++finfo->output_bfd->symcount;
5645
5646   return true;
5647 }
5648
5649 /* Flush the output symbols to the file.  */
5650
5651 static boolean
5652 elf_link_flush_output_syms (finfo)
5653      struct elf_final_link_info *finfo;
5654 {
5655   Elf_Internal_Shdr *symtab;
5656
5657   symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5658
5659   if (bfd_seek (finfo->output_bfd, symtab->sh_offset + symtab->sh_size,
5660                 SEEK_SET) != 0
5661       || (bfd_write ((PTR) finfo->symbuf, finfo->symbuf_count,
5662                      sizeof (Elf_External_Sym), finfo->output_bfd)
5663           != finfo->symbuf_count * sizeof (Elf_External_Sym)))
5664     return false;
5665
5666   symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym);
5667
5668   finfo->symbuf_count = 0;
5669
5670   return true;
5671 }
5672
5673 /* Add an external symbol to the symbol table.  This is called from
5674    the hash table traversal routine.  */
5675
5676 static boolean
5677 elf_link_output_extsym (h, data)
5678      struct elf_link_hash_entry *h;
5679      PTR data;
5680 {
5681   struct elf_final_link_info *finfo = (struct elf_final_link_info *) data;
5682   boolean strip;
5683   Elf_Internal_Sym sym;
5684
5685   /* We don't want to output symbols that have never been mentioned by
5686      a regular file, or that we have been told to strip.  However, if
5687      h->indx is set to -2, the symbol is used by a reloc and we must
5688      output it.  */
5689   if (h->indx == -2)
5690     strip = false;
5691   else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
5692            && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5693     strip = true;
5694   else if (finfo->info->strip == strip_all
5695            || (finfo->info->strip == strip_some
5696                && bfd_hash_lookup (finfo->info->keep_hash,
5697                                    h->root.root.string,
5698                                    false, false) == NULL))
5699     strip = true;
5700   else
5701     strip = false;
5702
5703   /* If we're stripping it, and it's not a dynamic symbol, there's
5704      nothing else to do.  */
5705   if (strip && h->dynindx == -1)
5706     return true;
5707
5708   sym.st_value = 0;
5709   sym.st_size = h->size;
5710   sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
5711   sym.st_other = 0;
5712
5713   switch (h->root.type)
5714     {
5715     default:
5716     case bfd_link_hash_new:
5717       abort ();
5718       return false;
5719
5720     case bfd_link_hash_undefined:
5721       sym.st_shndx = SHN_UNDEF;
5722       break;
5723
5724     case bfd_link_hash_weak:
5725       sym.st_shndx = SHN_UNDEF;
5726       sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
5727       break;
5728
5729     case bfd_link_hash_defined:
5730       {
5731         asection *sec;
5732
5733         sec = h->root.u.def.section;
5734         if (sec->output_section != NULL)
5735           {
5736             sym.st_shndx = elf_section_from_bfd_section (finfo->output_bfd,
5737                                                          sec->output_section);
5738             if (sym.st_shndx == (unsigned short) -1)
5739               {
5740                 /* FIXME: No way to handle errors.  */
5741                 abort ();
5742               }
5743
5744             /* ELF symbols in relocateable files are section relative,
5745                but in nonrelocateable files they are virtual
5746                addresses.  */
5747             sym.st_value = h->root.u.def.value + sec->output_offset;
5748             if (! finfo->info->relocateable)
5749               sym.st_value += sec->output_section->vma;
5750           }
5751         else
5752           {
5753             BFD_ASSERT (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
5754                         && elf_elfheader (sec->owner)->e_type == ET_DYN);
5755             sym.st_shndx = SHN_UNDEF;
5756           }
5757       }
5758       break;
5759
5760     case bfd_link_hash_common:
5761       sym.st_shndx = SHN_COMMON;
5762       if (h->align == 0)
5763         sym.st_value = 1;
5764       else
5765         sym.st_value = h->align;
5766       break;
5767
5768     case bfd_link_hash_indirect:
5769     case bfd_link_hash_warning:
5770       /* I have no idea how these should be handled.  */
5771       return true;
5772     }
5773
5774   /* If this symbol should be put in the .dynsym section, then put it
5775      there now.  We have already know the symbol index.  We also fill
5776      in the entry in the .hash section.  */
5777   if (h->dynindx != -1)
5778     {
5779       struct elf_backend_data *bed;
5780       size_t bucketcount;
5781       size_t bucket;
5782       bfd_byte *bucketpos;
5783       bfd_vma chain;
5784
5785       sym.st_name = h->dynstr_index;
5786
5787       /* Give the processor backend a chance to tweak the symbol
5788          value, and also to finish up anything that needs to be done
5789          for this symbol.  */
5790       bed = get_elf_backend_data (finfo->output_bfd);
5791       if (! ((*bed->elf_backend_finish_dynamic_symbol)
5792              (finfo->output_bfd, finfo->info, h, &sym)))
5793         {
5794           /* FIXME: No way to return error.  */
5795           abort ();
5796         }
5797
5798       elf_swap_symbol_out (finfo->output_bfd, &sym,
5799                            ((Elf_External_Sym *) finfo->dynsym_sec->contents
5800                             + h->dynindx));
5801
5802       bucketcount = elf_hash_table (finfo->info)->bucketcount;
5803       bucket = bfd_elf_hash (h->root.root.string) % bucketcount;
5804       bucketpos = ((bfd_byte *) finfo->hash_sec->contents
5805                    + (bucket + 2) * (ARCH_SIZE / 8));
5806       chain = get_word (finfo->output_bfd, bucketpos);
5807       put_word (finfo->output_bfd, h->dynindx, bucketpos);
5808       put_word (finfo->output_bfd, chain,
5809                 ((bfd_byte *) finfo->hash_sec->contents
5810                  + (bucketcount + 2 + h->dynindx) * (ARCH_SIZE / 8)));
5811     }
5812
5813   /* If we're stripping it, then it was just a dynamic symbol, and
5814      there's nothing else to do.  */
5815   if (strip)
5816     return true;
5817
5818   h->indx = finfo->output_bfd->symcount;
5819
5820   if (! elf_link_output_sym (finfo, h->root.root.string, &sym))
5821     {
5822       /* FIXME: No way to return error.  */
5823       abort ();
5824     }
5825
5826   return true;
5827 }
5828
5829 /* Link an input file into the linker output file.  This function
5830    handles all the sections and relocations of the input file at once.
5831    This is so that we only have to read the local symbols once, and
5832    don't have to keep them in memory.  */
5833
5834 static boolean
5835 elf_link_input_bfd (finfo, input_bfd)
5836      struct elf_final_link_info *finfo;
5837      bfd *input_bfd;
5838 {
5839   boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
5840                                        bfd *, asection *, bfd_byte *,
5841                                        Elf_Internal_Rela *,
5842                                        Elf_Internal_Sym *,
5843                                        asection **));
5844   bfd *output_bfd;
5845   Elf_Internal_Shdr *symtab_hdr;
5846   Elf_External_Sym *esym;
5847   Elf_External_Sym *esymend;
5848   Elf_Internal_Sym *isym;
5849   long *pindex;
5850   asection **ppsection;
5851   asection *o;
5852
5853   output_bfd = finfo->output_bfd;
5854   relocate_section =
5855     get_elf_backend_data (output_bfd)->elf_backend_relocate_section;
5856
5857   /* If this is a dynamic object, we don't want to do anything here:
5858      we don't want the local symbols, and we don't want the section
5859      contents.  */
5860   if (elf_elfheader (input_bfd)->e_type == ET_DYN)
5861     return true;
5862
5863   /* Read the local symbols.  */
5864   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5865   if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
5866       || (bfd_read (finfo->external_syms, sizeof (Elf_External_Sym),
5867                     symtab_hdr->sh_info, input_bfd)
5868           != symtab_hdr->sh_info * sizeof (Elf_External_Sym)))
5869     return false;
5870
5871   /* Swap in the local symbols and write out the ones which we know
5872      are going into the output file.  */
5873   esym = finfo->external_syms;
5874   esymend = esym + symtab_hdr->sh_info;
5875   isym = finfo->internal_syms;
5876   pindex = finfo->indices;
5877   ppsection = finfo->sections;
5878   for (; esym < esymend; esym++, isym++, pindex++, ppsection++)
5879     {
5880       asection *isec;
5881       const char *name;
5882       bfd_vma oldval;
5883
5884       elf_swap_symbol_in (input_bfd, esym, isym);
5885       *pindex = -1;
5886
5887       if (isym->st_shndx == SHN_UNDEF)
5888         isec = &bfd_und_section;
5889       else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE)
5890         {
5891           isec = section_from_elf_index (input_bfd, isym->st_shndx);
5892           if (isec == NULL)
5893             return false;
5894         }
5895       else if (isym->st_shndx == SHN_ABS)
5896         isec = &bfd_abs_section;
5897       else if (isym->st_shndx == SHN_COMMON)
5898         isec = &bfd_com_section;
5899       else
5900         {
5901           /* Who knows?  */
5902           isec = NULL;
5903         }
5904
5905       *ppsection = isec;
5906
5907       /* Don't output the first, undefined, symbol.  */
5908       if (esym == finfo->external_syms)
5909         continue;
5910
5911       /* If we are stripping all symbols, we don't want to output this
5912          one.  */
5913       if (finfo->info->strip == strip_all)
5914         continue;
5915
5916       /* We never output section symbols.  Instead, we use the section
5917          symbol of the corresponding section in the output file.  */
5918       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
5919         continue;
5920
5921       /* If we are discarding all local symbols, we don't want to
5922          output this one.  If we are generating a relocateable output
5923          file, then some of the local symbols may be required by
5924          relocs; we output them below as we discover that they are
5925          needed.  */
5926       if (finfo->info->discard == discard_all)
5927         continue;
5928
5929       /* Get the name of the symbol.  */
5930       name = elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
5931                                           isym->st_name);
5932       if (name == NULL)
5933         return false;
5934
5935       /* See if we are discarding symbols with this name.  */
5936       if ((finfo->info->strip == strip_some
5937            && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
5938                == NULL))
5939           || (finfo->info->discard == discard_l
5940               && strncmp (name, finfo->info->lprefix,
5941                           finfo->info->lprefix_len) == 0))
5942         continue;
5943
5944       /* If we get here, we are going to output this symbol.  */
5945
5946       /* Adjust the section index for the output file.  */
5947       isym->st_shndx = elf_section_from_bfd_section (output_bfd,
5948                                                      isec->output_section);
5949       if (isym->st_shndx == (unsigned short) -1)
5950         return false;
5951
5952       *pindex = output_bfd->symcount;
5953
5954       /* ELF symbols in relocateable files are section relative, but
5955          in executable files they are virtual addresses.  Note that
5956          this code assumes that all ELF sections have an associated
5957          BFD section with a reasonable value for output_offset; below
5958          we assume that they also have a reasonable value for
5959          output_section.  Any special sections must be set up to meet
5960          these requirements.  */
5961       oldval = isym->st_value;
5962       isym->st_value += isec->output_offset;
5963       if (! finfo->info->relocateable)
5964         isym->st_value += isec->output_section->vma;
5965
5966       if (! elf_link_output_sym (finfo, name, isym))
5967         return false;
5968
5969       /* Restore the old value for reloc handling.  */
5970       isym->st_value = oldval;
5971     }
5972
5973   /* Relocate the contents of each section.  */
5974   for (o = input_bfd->sections; o != NULL; o = o->next)
5975     {
5976       Elf_Internal_Shdr *input_rel_hdr;
5977
5978       if ((o->flags & SEC_HAS_CONTENTS) == 0)
5979         continue;
5980
5981       /* Read the contents of the section.  */
5982       if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
5983                                       (file_ptr) 0, o->_raw_size))
5984         return false;
5985
5986       if ((o->flags & SEC_RELOC) != 0)
5987         {
5988           /* Read in the relocs.  */
5989           input_rel_hdr = &elf_section_data (o)->rel_hdr;
5990           if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
5991               || bfd_read (finfo->external_relocs, 1, input_rel_hdr->sh_size,
5992                            input_bfd) != input_rel_hdr->sh_size)
5993             return false;
5994
5995           /* Swap in the relocs.  For convenience, we always produce
5996              an Elf_Internal_Rela array; if the relocs are Rel, we set
5997              the addend to 0.  */
5998           if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
5999             {
6000               Elf_External_Rel *erel;
6001               Elf_External_Rel *erelend;
6002               Elf_Internal_Rela *irela;
6003
6004               erel = (Elf_External_Rel *) finfo->external_relocs;
6005               erelend = erel + o->reloc_count;
6006               irela = finfo->internal_relocs;
6007               for (; erel < erelend; erel++, irela++)
6008                 {
6009                   Elf_Internal_Rel irel;
6010
6011                   elf_swap_reloc_in (input_bfd, erel, &irel);
6012                   irela->r_offset = irel.r_offset;
6013                   irela->r_info = irel.r_info;
6014                   irela->r_addend = 0;
6015                 }
6016             }
6017           else
6018             {
6019               Elf_External_Rela *erela;
6020               Elf_External_Rela *erelaend;
6021               Elf_Internal_Rela *irela;
6022
6023               BFD_ASSERT (input_rel_hdr->sh_entsize
6024                           == sizeof (Elf_External_Rela));
6025
6026               erela = (Elf_External_Rela *) finfo->external_relocs;
6027               erelaend = erela + o->reloc_count;
6028               irela = finfo->internal_relocs;
6029               for (; erela < erelaend; erela++, irela++)
6030                 elf_swap_reloca_in (input_bfd, erela, irela);
6031             }
6032
6033           /* Relocate the section by invoking a back end routine.
6034
6035              The back end routine is responsible for adjusting the
6036              section contents as necessary, and (if using Rela relocs
6037              and generating a relocateable output file) adjusting the
6038              reloc addend as necessary.
6039
6040              The back end routine does not have to worry about setting
6041              the reloc address or the reloc symbol index.
6042
6043              The back end routine is given a pointer to the swapped in
6044              internal symbols, and can access the hash table entries
6045              for the external symbols via elf_sym_hashes (input_bfd).
6046
6047              When generating relocateable output, the back end routine
6048              must handle STB_LOCAL/STT_SECTION symbols specially.  The
6049              output symbol is going to be a section symbol
6050              corresponding to the output section, which will require
6051              the addend to be adjusted.  */
6052
6053           if (! (*relocate_section) (output_bfd, finfo->info,
6054                                      input_bfd, o,
6055                                      finfo->contents,
6056                                      finfo->internal_relocs,
6057                                      finfo->internal_syms,
6058                                      finfo->sections))
6059             return false;
6060
6061           if (finfo->info->relocateable)
6062             {
6063               Elf_Internal_Rela *irela;
6064               Elf_Internal_Rela *irelaend;
6065               struct elf_link_hash_entry **rel_hash;
6066               Elf_Internal_Shdr *output_rel_hdr;
6067
6068               /* Adjust the reloc addresses and symbol indices.  */
6069
6070               irela = finfo->internal_relocs;
6071               irelaend = irela + o->reloc_count;
6072               rel_hash = (elf_section_data (o->output_section)->rel_hashes
6073                           + o->output_section->reloc_count);
6074               for (; irela < irelaend; irela++, rel_hash++)
6075                 {
6076                   long r_symndx;
6077                   Elf_Internal_Sym *isym;
6078                   asection *sec;
6079
6080                   irela->r_offset += o->output_offset;
6081
6082                   r_symndx = ELF_R_SYM (irela->r_info);
6083
6084                   if (r_symndx == 0)
6085                     continue;
6086
6087                   if (r_symndx >= symtab_hdr->sh_info)
6088                     {
6089                       long indx;
6090
6091                       /* This is a reloc against a global symbol.  We
6092                          have not yet output all the local symbols, so
6093                          we do not know the symbol index of any global
6094                          symbol.  We set the rel_hash entry for this
6095                          reloc to point to the global hash table entry
6096                          for this symbol.  The symbol index is then
6097                          set at the end of elf_bfd_final_link.  */
6098                       indx = r_symndx - symtab_hdr->sh_info;
6099                       *rel_hash = elf_sym_hashes (input_bfd)[indx];
6100
6101                       /* Setting the index to -2 tells
6102                          elf_link_output_extsym that this symbol is
6103                          used by a reloc.  */
6104                       BFD_ASSERT ((*rel_hash)->indx < 0);
6105                       (*rel_hash)->indx = -2;
6106
6107                       continue;
6108                     }
6109
6110                   /* This is a reloc against a local symbol. */
6111
6112                   *rel_hash = NULL;
6113                   isym = finfo->internal_syms + r_symndx;
6114                   sec = finfo->sections[r_symndx];
6115                   if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6116                     {
6117                       /* I suppose the backend ought to fill in the
6118                          section of any STT_SECTION symbol against a
6119                          processor specific section.  */
6120                       if (sec == NULL
6121                           || sec == &bfd_und_section
6122                           || sec == &bfd_com_section)
6123                         {
6124                           bfd_set_error (bfd_error_bad_value);
6125                           return false;
6126                         }
6127                       else if (sec == &bfd_abs_section)
6128                         r_symndx = 0;
6129                       else
6130                         {
6131                           r_symndx = sec->output_section->target_index;
6132                           if (r_symndx == 0)
6133                             abort ();
6134                         }
6135                     }
6136                   else
6137                     {
6138                       if (finfo->indices[r_symndx] == -1)
6139                         {
6140                           unsigned long link;
6141                           const char *name;
6142                           asection *osec;
6143
6144                           if (finfo->info->strip == strip_all)
6145                             {
6146                               /* You can't do ld -r -s.  */
6147                               bfd_set_error (bfd_error_invalid_operation);
6148                               return false;
6149                             }
6150
6151                           /* This symbol was skipped earlier, but
6152                              since it is needed by a reloc, we
6153                              must output it now.  */
6154                           link = symtab_hdr->sh_link;
6155                           name = elf_string_from_elf_section (input_bfd,
6156                                                               link,
6157                                                               isym->st_name);
6158                           if (name == NULL)
6159                             return false;
6160
6161                           osec = sec->output_section;
6162                           isym->st_shndx =
6163                             elf_section_from_bfd_section (output_bfd,
6164                                                           osec);
6165                           if (isym->st_shndx == (unsigned short) -1)
6166                             return false;
6167
6168                           isym->st_value += sec->output_offset;
6169                           if (! finfo->info->relocateable)
6170                             isym->st_value += osec->vma;
6171
6172                           finfo->indices[r_symndx] = output_bfd->symcount;
6173
6174                           if (! elf_link_output_sym (finfo, name, isym))
6175                             return false;
6176                         }
6177
6178                       r_symndx = finfo->indices[r_symndx];
6179                     }
6180
6181                   irela->r_info = ELF_R_INFO (r_symndx,
6182                                               ELF_R_TYPE (irela->r_info));
6183                 }
6184
6185               /* Swap out the relocs.  */
6186               output_rel_hdr = &elf_section_data (o->output_section)->rel_hdr;
6187               BFD_ASSERT (output_rel_hdr->sh_entsize
6188                           == input_rel_hdr->sh_entsize);
6189               irela = finfo->internal_relocs;
6190               irelaend = irela + o->reloc_count;
6191               if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
6192                 {
6193                   Elf_External_Rel *erel;
6194
6195                   erel = ((Elf_External_Rel *) output_rel_hdr->contents
6196                           + o->output_section->reloc_count);
6197                   for (; irela < irelaend; irela++, erel++)
6198                     {
6199                       Elf_Internal_Rel irel;
6200
6201                       irel.r_offset = irela->r_offset;
6202                       irel.r_info = irela->r_info;
6203                       BFD_ASSERT (irela->r_addend == 0);
6204                       elf_swap_reloc_out (output_bfd, &irel, erel);
6205                     }
6206                 }
6207               else
6208                 {
6209                   Elf_External_Rela *erela;
6210
6211                   BFD_ASSERT (input_rel_hdr->sh_entsize
6212                               == sizeof (Elf_External_Rela));
6213                   erela = ((Elf_External_Rela *) output_rel_hdr->contents
6214                            + o->output_section->reloc_count);
6215                   for (; irela < irelaend; irela++, erela++)
6216                     elf_swap_reloca_out (output_bfd, irela, erela);
6217                 }
6218
6219               o->output_section->reloc_count += o->reloc_count;
6220             }
6221         }
6222
6223       /* Write out the modified section contents.  */
6224       if (! bfd_set_section_contents (output_bfd, o->output_section,
6225                                       finfo->contents, o->output_offset,
6226                                       (o->_cooked_size != 0
6227                                        ? o->_cooked_size
6228                                        : o->_raw_size)))
6229         return false;
6230     }
6231
6232   return true;
6233 }
6234
6235 /* Generate a reloc when linking an ELF file.  This is a reloc
6236    requested by the linker, and does come from any input file.  This
6237    is used to build constructor and destructor tables when linking
6238    with -Ur.  */
6239
6240 static boolean
6241 elf_reloc_link_order (output_bfd, info, output_section, link_order)
6242      bfd *output_bfd;
6243      struct bfd_link_info *info;
6244      asection *output_section;
6245      struct bfd_link_order *link_order;
6246 {
6247   const reloc_howto_type *howto;
6248   long indx;
6249   bfd_vma offset;
6250   struct elf_link_hash_entry **rel_hash_ptr;
6251   Elf_Internal_Shdr *rel_hdr;
6252
6253   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6254   if (howto == NULL)
6255     {
6256       bfd_set_error (bfd_error_bad_value);
6257       return false;
6258     }
6259
6260   /* If this is an inplace reloc, we must write the addend into the
6261      object file.  */
6262   if (howto->partial_inplace
6263       && link_order->u.reloc.p->addend != 0)
6264     {
6265       bfd_size_type size;
6266       bfd_reloc_status_type rstat;
6267       bfd_byte *buf;
6268       boolean ok;
6269
6270       size = bfd_get_reloc_size (howto);
6271       buf = (bfd_byte *) bfd_zmalloc (size);
6272       if (buf == (bfd_byte *) NULL)
6273         {
6274           bfd_set_error (bfd_error_no_memory);
6275           return false;
6276         }
6277       rstat = _bfd_relocate_contents (howto, output_bfd,
6278                                       link_order->u.reloc.p->addend, buf);
6279       switch (rstat)
6280         {
6281         case bfd_reloc_ok:
6282           break;
6283         default:
6284         case bfd_reloc_outofrange:
6285           abort ();
6286         case bfd_reloc_overflow:
6287           if (! ((*info->callbacks->reloc_overflow)
6288                  (info,
6289                   (link_order->type == bfd_section_reloc_link_order
6290                    ? bfd_section_name (output_bfd,
6291                                        link_order->u.reloc.p->u.section)
6292                    : link_order->u.reloc.p->u.name),
6293                   howto->name, link_order->u.reloc.p->addend,
6294                   (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
6295             {
6296               free (buf);
6297               return false;
6298             }
6299           break;
6300         }
6301       ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6302                                      (file_ptr) link_order->offset, size);
6303       free (buf);
6304       if (! ok)
6305         return false;
6306     }
6307
6308   /* Figure out the symbol index.  */
6309   rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
6310                   + output_section->reloc_count);
6311   if (link_order->type == bfd_section_reloc_link_order)
6312     {
6313       indx = link_order->u.reloc.p->u.section->target_index;
6314       if (indx == 0)
6315         abort ();
6316       *rel_hash_ptr = NULL;
6317     }
6318   else
6319     {
6320       struct elf_link_hash_entry *h;
6321
6322       h = elf_link_hash_lookup (elf_hash_table (info),
6323                                 link_order->u.reloc.p->u.name,
6324                                 false, false, true);
6325       if (h != NULL)
6326         {
6327           /* Setting the index to -2 tells elf_link_output_extsym that
6328              this symbol is used by a reloc.  */
6329           h->indx = -2;
6330           *rel_hash_ptr = h;
6331           indx = 0;
6332         }
6333       else
6334         {
6335           if (! ((*info->callbacks->unattached_reloc)
6336                  (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6337                   (asection *) NULL, (bfd_vma) 0)))
6338             return false;
6339           indx = 0;
6340         }
6341     }
6342
6343   /* The address of a reloc is relative to the section in a
6344      relocateable file, and is a virtual address in an executable
6345      file.  */
6346   offset = link_order->offset;
6347   if (! info->relocateable)
6348     offset += output_section->vma;
6349
6350   rel_hdr = &elf_section_data (output_section)->rel_hdr;
6351
6352   if (rel_hdr->sh_type == SHT_REL)
6353     {
6354       Elf_Internal_Rel irel;
6355       Elf_External_Rel *erel;
6356
6357       irel.r_offset = offset;
6358       irel.r_info = ELF_R_INFO (indx, howto->type);
6359       erel = ((Elf_External_Rel *) rel_hdr->contents
6360               + output_section->reloc_count);
6361       elf_swap_reloc_out (output_bfd, &irel, erel);
6362     }
6363   else
6364     {
6365       Elf_Internal_Rela irela;
6366       Elf_External_Rela *erela;
6367
6368       irela.r_offset = offset;
6369       irela.r_info = ELF_R_INFO (indx, howto->type);
6370       irela.r_addend = link_order->u.reloc.p->addend;
6371       erela = ((Elf_External_Rela *) rel_hdr->contents
6372                + output_section->reloc_count);
6373       elf_swap_reloca_out (output_bfd, &irela, erela);
6374     }
6375
6376   ++output_section->reloc_count;
6377
6378   return true;
6379 }
This page took 0.406412 seconds and 4 git commands to generate.