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