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