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