1 /* ELF executable support for BFD.
2 Copyright 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
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.
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.
16 This file is part of BFD, the Binary File Descriptor library.
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
32 /* Problems and other issues to resolve.
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.
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
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)
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
57 (3) ELF section symbols are handled rather sloppily now. This should
58 be cleaned up, and ELF section symbols reconciled with BFD section
62 #include <string.h> /* For strrchr and friends */
69 /* Renaming structures, typedefs, macros and functions to be size-specific. */
70 #define Elf_External_Ehdr NAME(Elf,External_Ehdr)
71 #define Elf_External_Sym NAME(Elf,External_Sym)
72 #define Elf_External_Shdr NAME(Elf,External_Shdr)
73 #define Elf_External_Phdr NAME(Elf,External_Phdr)
74 #define Elf_External_Rel NAME(Elf,External_Rel)
75 #define Elf_External_Rela NAME(Elf,External_Rela)
76 #define Elf_External_Dyn NAME(Elf,External_Dyn)
78 #define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command)
79 #define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal)
80 #define elf_core_file_matches_executable_p \
81 NAME(bfd_elf,core_file_matches_executable_p)
82 #define elf_object_p NAME(bfd_elf,object_p)
83 #define elf_core_file_p NAME(bfd_elf,core_file_p)
84 #define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound)
85 #define elf_get_dynamic_symtab_upper_bound \
86 NAME(bfd_elf,get_dynamic_symtab_upper_bound)
87 #define elf_swap_reloc_in NAME(bfd_elf,swap_reloc_in)
88 #define elf_swap_reloca_in NAME(bfd_elf,swap_reloca_in)
89 #define elf_swap_reloc_out NAME(bfd_elf,swap_reloc_out)
90 #define elf_swap_reloca_out NAME(bfd_elf,swap_reloca_out)
91 #define elf_swap_dyn_in NAME(bfd_elf,swap_dyn_in)
92 #define elf_swap_dyn_out NAME(bfd_elf,swap_dyn_out)
93 #define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound)
94 #define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc)
95 #define elf_get_symtab NAME(bfd_elf,get_symtab)
96 #define elf_canonicalize_dynamic_symtab \
97 NAME(bfd_elf,canonicalize_dynamic_symtab)
98 #define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol)
99 #define elf_get_symbol_info NAME(bfd_elf,get_symbol_info)
100 #define elf_print_symbol NAME(bfd_elf,print_symbol)
101 #define elf_get_lineno NAME(bfd_elf,get_lineno)
102 #define elf_set_arch_mach NAME(bfd_elf,set_arch_mach)
103 #define elf_find_nearest_line NAME(bfd_elf,find_nearest_line)
104 #define elf_sizeof_headers NAME(bfd_elf,sizeof_headers)
105 #define elf_set_section_contents NAME(bfd_elf,set_section_contents)
106 #define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto)
107 #define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel)
108 #define elf_new_section_hook NAME(bfd_elf,new_section_hook)
109 #define write_relocs NAME(bfd_elf,_write_relocs)
110 #define elf_find_section NAME(bfd_elf,find_section)
111 #define elf_bfd_link_add_symbols NAME(bfd_elf,bfd_link_add_symbols)
112 #define elf_add_dynamic_entry NAME(bfd_elf,add_dynamic_entry)
113 #define elf_bfd_final_link NAME(bfd_elf,bfd_final_link)
116 #define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
117 #define ELF_R_SYM(X) ELF64_R_SYM(X)
118 #define ELF_R_TYPE(X) ELF64_R_TYPE(X)
119 #define ELFCLASS ELFCLASS64
121 #define LOG_FILE_ALIGN 3
124 #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
125 #define ELF_R_SYM(X) ELF32_R_SYM(X)
126 #define ELF_R_TYPE(X) ELF32_R_TYPE(X)
127 #define ELFCLASS ELFCLASS32
129 #define LOG_FILE_ALIGN 2
132 /* Forward declarations of static functions */
134 static unsigned long bfd_add_to_strtab
135 PARAMS ((bfd
*, struct strtab
*, const char *));
136 static asection
*section_from_elf_index
PARAMS ((bfd
*, unsigned int));
138 static int elf_section_from_bfd_section
PARAMS ((bfd
*, struct sec
*));
140 static long elf_slurp_symbol_table
PARAMS ((bfd
*, asymbol
**, boolean
));
142 static int elf_symbol_from_bfd_symbol
PARAMS ((bfd
*,
143 struct symbol_cache_entry
**));
145 static boolean elf_compute_section_file_positions
146 PARAMS ((bfd
*, struct bfd_link_info
*));
147 static boolean prep_headers
PARAMS ((bfd
*));
148 static boolean assign_section_numbers
PARAMS ((bfd
*));
149 static file_ptr align_file_position
PARAMS ((file_ptr
));
150 static file_ptr assign_file_position_for_section
151 PARAMS ((Elf_Internal_Shdr
*, file_ptr
, boolean
));
152 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*, boolean
));
153 static void assign_file_positions_for_relocs
PARAMS ((bfd
*));
154 static bfd_size_type get_program_header_size
PARAMS ((bfd
*));
155 static file_ptr map_program_segments
156 PARAMS ((bfd
*, file_ptr
, Elf_Internal_Shdr
*, bfd_size_type
));
158 static boolean elf_map_symbols
PARAMS ((bfd
*));
159 static boolean swap_out_syms
PARAMS ((bfd
*));
161 static boolean bfd_section_from_shdr
PARAMS ((bfd
*, unsigned int shindex
));
164 static void elf_debug_section
PARAMS ((char *, int, Elf_Internal_Shdr
*));
165 static void elf_debug_file
PARAMS ((Elf_Internal_Ehdr
*));
168 #define elf_string_from_elf_strtab(abfd,strindex) \
169 elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex)
172 /* Structure swapping routines */
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". */
177 #define put_word bfd_h_put_64
178 #define get_word bfd_h_get_64
181 #define put_word bfd_h_put_32
182 #define get_word bfd_h_get_32
185 /* Translate an ELF symbol in external format into an ELF symbol in internal
189 elf_swap_symbol_in (abfd
, src
, dst
)
191 Elf_External_Sym
*src
;
192 Elf_Internal_Sym
*dst
;
194 dst
->st_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->st_name
);
195 dst
->st_value
= get_word (abfd
, (bfd_byte
*) src
->st_value
);
196 dst
->st_size
= get_word (abfd
, (bfd_byte
*) src
->st_size
);
197 dst
->st_info
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_info
);
198 dst
->st_other
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
->st_other
);
199 dst
->st_shndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->st_shndx
);
202 /* Translate an ELF symbol in internal format into an ELF symbol in external
206 elf_swap_symbol_out (abfd
, src
, dst
)
208 Elf_Internal_Sym
*src
;
209 Elf_External_Sym
*dst
;
211 bfd_h_put_32 (abfd
, src
->st_name
, dst
->st_name
);
212 put_word (abfd
, src
->st_value
, dst
->st_value
);
213 put_word (abfd
, src
->st_size
, dst
->st_size
);
214 bfd_h_put_8 (abfd
, src
->st_info
, dst
->st_info
);
215 bfd_h_put_8 (abfd
, src
->st_other
, dst
->st_other
);
216 bfd_h_put_16 (abfd
, src
->st_shndx
, dst
->st_shndx
);
220 /* Translate an ELF file header in external format into an ELF file header in
224 elf_swap_ehdr_in (abfd
, src
, dst
)
226 Elf_External_Ehdr
*src
;
227 Elf_Internal_Ehdr
*dst
;
229 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
230 dst
->e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_type
);
231 dst
->e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_machine
);
232 dst
->e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_version
);
233 dst
->e_entry
= get_word (abfd
, (bfd_byte
*) src
->e_entry
);
234 dst
->e_phoff
= get_word (abfd
, (bfd_byte
*) src
->e_phoff
);
235 dst
->e_shoff
= get_word (abfd
, (bfd_byte
*) src
->e_shoff
);
236 dst
->e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->e_flags
);
237 dst
->e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_ehsize
);
238 dst
->e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phentsize
);
239 dst
->e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_phnum
);
240 dst
->e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shentsize
);
241 dst
->e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shnum
);
242 dst
->e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
->e_shstrndx
);
245 /* Translate an ELF file header in internal format into an ELF file header in
249 elf_swap_ehdr_out (abfd
, src
, dst
)
251 Elf_Internal_Ehdr
*src
;
252 Elf_External_Ehdr
*dst
;
254 memcpy (dst
->e_ident
, src
->e_ident
, EI_NIDENT
);
255 /* note that all elements of dst are *arrays of unsigned char* already... */
256 bfd_h_put_16 (abfd
, src
->e_type
, dst
->e_type
);
257 bfd_h_put_16 (abfd
, src
->e_machine
, dst
->e_machine
);
258 bfd_h_put_32 (abfd
, src
->e_version
, dst
->e_version
);
259 put_word (abfd
, src
->e_entry
, dst
->e_entry
);
260 put_word (abfd
, src
->e_phoff
, dst
->e_phoff
);
261 put_word (abfd
, src
->e_shoff
, dst
->e_shoff
);
262 bfd_h_put_32 (abfd
, src
->e_flags
, dst
->e_flags
);
263 bfd_h_put_16 (abfd
, src
->e_ehsize
, dst
->e_ehsize
);
264 bfd_h_put_16 (abfd
, src
->e_phentsize
, dst
->e_phentsize
);
265 bfd_h_put_16 (abfd
, src
->e_phnum
, dst
->e_phnum
);
266 bfd_h_put_16 (abfd
, src
->e_shentsize
, dst
->e_shentsize
);
267 bfd_h_put_16 (abfd
, src
->e_shnum
, dst
->e_shnum
);
268 bfd_h_put_16 (abfd
, src
->e_shstrndx
, dst
->e_shstrndx
);
272 /* Translate an ELF section header table entry in external format into an
273 ELF section header table entry in internal format. */
276 elf_swap_shdr_in (abfd
, src
, dst
)
278 Elf_External_Shdr
*src
;
279 Elf_Internal_Shdr
*dst
;
281 dst
->sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_name
);
282 dst
->sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_type
);
283 dst
->sh_flags
= get_word (abfd
, (bfd_byte
*) src
->sh_flags
);
284 dst
->sh_addr
= get_word (abfd
, (bfd_byte
*) src
->sh_addr
);
285 dst
->sh_offset
= get_word (abfd
, (bfd_byte
*) src
->sh_offset
);
286 dst
->sh_size
= get_word (abfd
, (bfd_byte
*) src
->sh_size
);
287 dst
->sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_link
);
288 dst
->sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_info
);
289 dst
->sh_addralign
= get_word (abfd
, (bfd_byte
*) src
->sh_addralign
);
290 dst
->sh_entsize
= get_word (abfd
, (bfd_byte
*) src
->sh_entsize
);
291 /* we haven't done any processing on it yet, so... */
292 dst
->rawdata
= (void *) 0;
295 /* Translate an ELF section header table entry in internal format into an
296 ELF section header table entry in external format. */
299 elf_swap_shdr_out (abfd
, src
, dst
)
301 Elf_Internal_Shdr
*src
;
302 Elf_External_Shdr
*dst
;
304 /* note that all elements of dst are *arrays of unsigned char* already... */
305 bfd_h_put_32 (abfd
, src
->sh_name
, dst
->sh_name
);
306 bfd_h_put_32 (abfd
, src
->sh_type
, dst
->sh_type
);
307 put_word (abfd
, src
->sh_flags
, dst
->sh_flags
);
308 put_word (abfd
, src
->sh_addr
, dst
->sh_addr
);
309 put_word (abfd
, src
->sh_offset
, dst
->sh_offset
);
310 put_word (abfd
, src
->sh_size
, dst
->sh_size
);
311 bfd_h_put_32 (abfd
, src
->sh_link
, dst
->sh_link
);
312 bfd_h_put_32 (abfd
, src
->sh_info
, dst
->sh_info
);
313 put_word (abfd
, src
->sh_addralign
, dst
->sh_addralign
);
314 put_word (abfd
, src
->sh_entsize
, dst
->sh_entsize
);
318 /* Translate an ELF program header table entry in external format into an
319 ELF program header table entry in internal format. */
322 elf_swap_phdr_in (abfd
, src
, dst
)
324 Elf_External_Phdr
*src
;
325 Elf_Internal_Phdr
*dst
;
327 dst
->p_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_type
);
328 dst
->p_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_flags
);
329 dst
->p_offset
= get_word (abfd
, (bfd_byte
*) src
->p_offset
);
330 dst
->p_vaddr
= get_word (abfd
, (bfd_byte
*) src
->p_vaddr
);
331 dst
->p_paddr
= get_word (abfd
, (bfd_byte
*) src
->p_paddr
);
332 dst
->p_filesz
= get_word (abfd
, (bfd_byte
*) src
->p_filesz
);
333 dst
->p_memsz
= get_word (abfd
, (bfd_byte
*) src
->p_memsz
);
334 dst
->p_align
= get_word (abfd
, (bfd_byte
*) src
->p_align
);
338 elf_swap_phdr_out (abfd
, src
, dst
)
340 Elf_Internal_Phdr
*src
;
341 Elf_External_Phdr
*dst
;
343 /* note that all elements of dst are *arrays of unsigned char* already... */
344 bfd_h_put_32 (abfd
, src
->p_type
, dst
->p_type
);
345 put_word (abfd
, src
->p_offset
, dst
->p_offset
);
346 put_word (abfd
, src
->p_vaddr
, dst
->p_vaddr
);
347 put_word (abfd
, src
->p_paddr
, dst
->p_paddr
);
348 put_word (abfd
, src
->p_filesz
, dst
->p_filesz
);
349 put_word (abfd
, src
->p_memsz
, dst
->p_memsz
);
350 bfd_h_put_32 (abfd
, src
->p_flags
, dst
->p_flags
);
351 put_word (abfd
, src
->p_align
, dst
->p_align
);
354 /* Translate an ELF reloc from external format to internal format. */
356 elf_swap_reloc_in (abfd
, src
, dst
)
358 Elf_External_Rel
*src
;
359 Elf_Internal_Rel
*dst
;
361 dst
->r_offset
= get_word (abfd
, (bfd_byte
*) src
->r_offset
);
362 dst
->r_info
= get_word (abfd
, (bfd_byte
*) src
->r_info
);
366 elf_swap_reloca_in (abfd
, src
, dst
)
368 Elf_External_Rela
*src
;
369 Elf_Internal_Rela
*dst
;
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
);
376 /* Translate an ELF reloc from internal format to external format. */
378 elf_swap_reloc_out (abfd
, src
, dst
)
380 Elf_Internal_Rel
*src
;
381 Elf_External_Rel
*dst
;
383 put_word (abfd
, src
->r_offset
, dst
->r_offset
);
384 put_word (abfd
, src
->r_info
, dst
->r_info
);
388 elf_swap_reloca_out (abfd
, src
, dst
)
390 Elf_Internal_Rela
*src
;
391 Elf_External_Rela
*dst
;
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
);
399 elf_swap_dyn_in (abfd
, src
, dst
)
401 const Elf_External_Dyn
*src
;
402 Elf_Internal_Dyn
*dst
;
404 dst
->d_tag
= get_word (abfd
, src
->d_tag
);
405 dst
->d_un
.d_val
= get_word (abfd
, src
->d_un
.d_val
);
409 elf_swap_dyn_out (abfd
, src
, dst
)
411 const Elf_Internal_Dyn
*src
;
412 Elf_External_Dyn
*dst
;
414 put_word (abfd
, src
->d_tag
, dst
->d_tag
);
415 put_word (abfd
, src
->d_un
.d_val
, dst
->d_un
.d_val
);
418 /* String table creation/manipulation routines */
420 static struct strtab
*
421 bfd_new_strtab (abfd
)
426 ss
= (struct strtab
*) malloc (sizeof (struct strtab
));
429 bfd_set_error (bfd_error_no_memory
);
432 ss
->tab
= malloc (1);
435 bfd_set_error (bfd_error_no_memory
);
446 bfd_add_to_strtab (abfd
, ss
, str
)
451 /* should search first, but for now: */
452 /* include the trailing NUL */
453 int ln
= strlen (str
) + 1;
455 /* FIXME: This is slow. Also, we could combine this with the a.out
456 string table building and use a hash table, although it might not
457 be worth it since ELF symbols don't include debugging information
458 and thus have much less overlap. */
459 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
462 bfd_set_error (bfd_error_no_memory
);
463 return (unsigned long) -1;
466 strcpy (ss
->tab
+ ss
->length
, str
);
470 return ss
->length
- ln
;
474 bfd_add_2_to_strtab (abfd
, ss
, str
, str2
)
480 /* should search first, but for now: */
481 /* include the trailing NUL */
482 int ln
= strlen (str
) + strlen (str2
) + 1;
484 /* should this be using obstacks? */
486 ss
->tab
= realloc (ss
->tab
, ss
->length
+ ln
);
488 ss
->tab
= malloc (ln
);
490 BFD_ASSERT (ss
->tab
!= 0); /* FIXME */
491 strcpy (ss
->tab
+ ss
->length
, str
);
492 strcpy (ss
->tab
+ ss
->length
+ strlen (str
), str2
);
496 return ss
->length
- ln
;
500 /* ELF .o/exec file reading */
502 /* Create a new bfd section from an ELF section header. */
505 bfd_section_from_shdr (abfd
, shindex
)
507 unsigned int shindex
;
509 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
510 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
514 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
516 switch (hdr
->sh_type
)
520 /* inactive section. Throw it away. */
525 /* Bits that get saved. This one is real. */
526 if (hdr
->rawdata
== NULL
)
528 newsect
= bfd_make_section_anyway (abfd
, name
);
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
);
538 if (hdr
->sh_flags
& SHF_ALLOC
)
540 newsect
->flags
|= SEC_ALLOC
;
541 newsect
->flags
|= SEC_LOAD
;
544 if (!(hdr
->sh_flags
& SHF_WRITE
))
545 newsect
->flags
|= SEC_READONLY
;
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
;
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
;
559 hdr
->rawdata
= (PTR
) newsect
;
564 /* Bits that get saved. This one is real. */
565 if (hdr
->rawdata
== NULL
)
567 newsect
= bfd_make_section_anyway (abfd
, name
);
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
;
578 if (!(hdr
->sh_flags
& SHF_WRITE
))
579 newsect
->flags
|= SEC_READONLY
;
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 */
586 hdr
->rawdata
= (PTR
) newsect
;
590 case SHT_SYMTAB
: /* A symbol table */
591 if (elf_onesymtab (abfd
) == shindex
)
594 BFD_ASSERT (hdr
->sh_entsize
== sizeof (Elf_External_Sym
));
595 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
596 elf_onesymtab (abfd
) = shindex
;
597 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
598 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_hdr
;
599 abfd
->flags
|= HAS_SYMS
;
602 case SHT_DYNSYM
: /* A dynamic symbol table */
603 if (elf_dynsymtab (abfd
) == shindex
)
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
;
614 case SHT_STRTAB
: /* A string table */
617 if (ehdr
->e_shstrndx
== shindex
)
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
;
627 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
629 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
630 if (hdr2
->sh_link
== shindex
)
632 if (! bfd_section_from_shdr (abfd
, i
))
634 if (elf_onesymtab (abfd
) == i
)
636 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
637 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
640 if (elf_dynsymtab (abfd
) == i
)
642 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
643 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->dynstrtab_hdr
;
646 #if 0 /* Not handling other string tables specially right now. */
647 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
648 /* We have a strtab for some random other section. */
649 newsect
= (asection
*) hdr2
->rawdata
;
652 hdr
->rawdata
= (PTR
) newsect
;
653 hdr2
= &elf_section_data (newsect
)->str_hdr
;
655 elf_elfsections (abfd
)[shindex
] = hdr2
;
661 newsect
= bfd_make_section_anyway (abfd
, name
);
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
;
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
;
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
;
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. */
693 /* FIXME: The above statement is wrong! There are typically at least
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 */
699 asection
*target_sect
;
700 Elf_Internal_Shdr
*hdr2
;
701 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
703 /* Don't allow REL relocations on a machine that uses RELA and
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
==
715 ? sizeof (Elf_External_Rela
)
716 : sizeof (Elf_External_Rel
)));
718 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
) /* target */
719 || ! bfd_section_from_shdr (abfd
, hdr
->sh_link
)) /* symbol table */
721 target_sect
= section_from_elf_index (abfd
, hdr
->sh_info
);
722 if (target_sect
== NULL
723 || elf_section_data (target_sect
) == NULL
)
726 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
728 elf_elfsections (abfd
)[shindex
] = hdr2
;
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
;
733 abfd
->flags
|= HAS_RELOC
;
740 fprintf (stderr
, "Dynamic Linking sections not yet supported.\n");
747 fprintf (stderr
, "Note Sections not yet supported.\n");
754 fprintf (stderr
, "SHLIB Sections not supported (and non conforming.)\n");
759 /* Check for any processor-specific section types. */
761 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
763 if (bed
->elf_backend_section_from_shdr
)
764 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
773 elf_new_section_hook (abfd
, sec
)
778 struct bfd_elf_section_data
*sdata
;
780 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
783 bfd_set_error (bfd_error_no_memory
);
786 sec
->used_by_bfd
= (PTR
) sdata
;
787 memset (sdata
, 0, sizeof (*sdata
));
791 /* Create a new bfd section from an ELF program header.
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.
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.
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.
814 bfd_section_from_phdr (abfd
, hdr
, index
)
816 Elf_Internal_Phdr
*hdr
;
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);
831 bfd_set_error (bfd_error_no_memory
);
834 strcpy (name
, namebuf
);
835 newsect
= bfd_make_section (abfd
, name
);
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
)
844 newsect
->flags
|= SEC_ALLOC
;
845 newsect
->flags
|= SEC_LOAD
;
846 if (hdr
->p_flags
& PF_X
)
848 /* FIXME: all we known is that it has execute PERMISSION,
850 newsect
->flags
|= SEC_CODE
;
853 if (!(hdr
->p_flags
& PF_W
))
855 newsect
->flags
|= SEC_READONLY
;
860 sprintf (namebuf
, "segment%db", index
);
861 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
864 bfd_set_error (bfd_error_no_memory
);
867 strcpy (name
, namebuf
);
868 newsect
= bfd_make_section (abfd
, name
);
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
)
875 newsect
->flags
|= SEC_ALLOC
;
876 if (hdr
->p_flags
& PF_X
)
877 newsect
->flags
|= SEC_CODE
;
879 if (!(hdr
->p_flags
& PF_W
))
880 newsect
->flags
|= SEC_READONLY
;
886 /* Begin processing a given object.
888 First we validate the file by reading in the ELF header and checking
891 static INLINE boolean
893 Elf_External_Ehdr
*x_ehdrp
;
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
));
901 /* Check to see if the file associated with ABFD matches the target vector
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
907 file does not match the target vector. */
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 */
916 Elf_Internal_Shdr
*i_shdrp
= NULL
; /* Section header table, internal form */
917 unsigned int shindex
;
918 char *shstrtab
; /* Internal copy of section header stringtab */
919 struct elf_backend_data
*ebd
;
920 struct elf_obj_tdata
*preserved_tdata
= elf_tdata (abfd
);
921 struct elf_obj_tdata
*new_tdata
= NULL
;
923 /* Read in the ELF header in external format. */
925 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
927 if (bfd_get_error () != bfd_error_system_call
)
928 goto got_wrong_format_error
;
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
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
;
944 /* Check that file's byte order matches xvec's */
945 switch (x_ehdr
.e_ident
[EI_DATA
])
947 case ELFDATA2MSB
: /* Big-endian */
948 if (!abfd
->xvec
->header_byteorder_big_p
)
949 goto got_wrong_format_error
;
951 case ELFDATA2LSB
: /* Little-endian */
952 if (abfd
->xvec
->header_byteorder_big_p
)
953 goto got_wrong_format_error
;
955 case ELFDATANONE
: /* No data encoding specified */
956 default: /* Unknown data encoding specified */
957 goto got_wrong_format_error
;
960 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
961 the tdata pointer in the bfd. */
963 new_tdata
= ((struct elf_obj_tdata
*)
964 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
)));
965 if (new_tdata
== NULL
)
966 goto got_no_memory_error
;
967 elf_tdata (abfd
) = new_tdata
;
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
);
973 elf_debug_file (i_ehdrp
);
976 /* If there is no section header table, we're hosed. */
977 if (i_ehdrp
->e_shoff
== 0)
978 goto got_wrong_format_error
;
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
;
986 ebd
= get_elf_backend_data (abfd
);
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
)
992 bfd_target
**target_ptr
;
994 if (ebd
->elf_machine_code
!= EM_NONE
)
995 goto got_wrong_format_error
;
997 /* This is the generic ELF target. Let it match any ELF target
998 for which we do not have a specific backend. */
999 for (target_ptr
= bfd_target_vector
; *target_ptr
!= NULL
; target_ptr
++)
1001 struct elf_backend_data
*back
;
1003 if ((*target_ptr
)->flavour
!= bfd_target_elf_flavour
)
1005 back
= (struct elf_backend_data
*) (*target_ptr
)->backend_data
;
1006 if (back
->elf_machine_code
== i_ehdrp
->e_machine
)
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
;
1015 /* Set the flags and architecture before calling the backend so that
1016 it can override them. */
1017 if (i_ehdrp
->e_type
== ET_EXEC
)
1018 abfd
->flags
|= EXEC_P
;
1019 else if (i_ehdrp
->e_type
== ET_DYN
)
1020 abfd
->flags
|= DYNAMIC
;
1022 if (! bfd_default_set_arch_mach (abfd
, ebd
->arch
, 0))
1025 /* Remember the entry point specified in the ELF file header. */
1026 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
1028 /* Let the backend double check the format and override global
1030 if (ebd
->elf_backend_object_p
)
1032 if ((*ebd
->elf_backend_object_p
) (abfd
) == false)
1033 goto got_wrong_format_error
;
1036 /* Allocate space for a copy of the section header table in
1037 internal form, seek to the section header table in the file,
1038 read it in, and convert it to internal form. */
1039 i_shdrp
= ((Elf_Internal_Shdr
*)
1040 bfd_alloc (abfd
, sizeof (*i_shdrp
) * i_ehdrp
->e_shnum
));
1041 elf_elfsections (abfd
) = ((Elf_Internal_Shdr
**)
1043 sizeof (i_shdrp
) * i_ehdrp
->e_shnum
));
1044 if (!i_shdrp
|| !elf_elfsections (abfd
))
1045 goto got_no_memory_error
;
1046 if (bfd_seek (abfd
, i_ehdrp
->e_shoff
, SEEK_SET
) != 0)
1048 for (shindex
= 0; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1050 if (bfd_read ((PTR
) & x_shdr
, sizeof x_shdr
, 1, abfd
) != sizeof (x_shdr
))
1052 elf_swap_shdr_in (abfd
, &x_shdr
, i_shdrp
+ shindex
);
1053 elf_elfsections (abfd
)[shindex
] = i_shdrp
+ shindex
;
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
;
1060 if (i_ehdrp
->e_shstrndx
)
1062 if (! bfd_section_from_shdr (abfd
, i_ehdrp
->e_shstrndx
))
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.) */
1072 shstrtab
= elf_get_str_section (abfd
, i_ehdrp
->e_shstrndx
);
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
1078 a dummy placeholder entry, so we ignore it. */
1080 for (shindex
= 1; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1082 if (! bfd_section_from_shdr (abfd
, shindex
))
1086 return (abfd
->xvec
);
1088 got_wrong_format_error
:
1089 bfd_set_error (bfd_error_wrong_format
);
1091 got_no_memory_error
:
1092 bfd_set_error (bfd_error_no_memory
);
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
);
1102 elf_tdata (abfd
) = preserved_tdata
;
1107 /* ELF .o/exec file writing */
1109 /* Takes a bfd and a symbol, returns a pointer to the elf specific area
1110 of the symbol if there is one. */
1111 static INLINE elf_symbol_type
*
1112 elf_symbol_from (ignore_abfd
, symbol
)
1116 if (symbol
->the_bfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
1119 if (symbol
->the_bfd
->tdata
.elf_obj_data
== (struct elf_obj_tdata
*) NULL
)
1122 return (elf_symbol_type
*) symbol
;
1125 /* Create ELF output from BFD sections.
1127 Essentially, just create the section header and forget about the program
1131 elf_make_sections (abfd
, asect
, obj
)
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. */
1138 Elf_Internal_Shdr
*this_hdr
;
1139 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1141 this_hdr
->sh_addr
= asect
->vma
;
1142 this_hdr
->sh_size
= asect
->_raw_size
;
1143 /* contents already set by elf_set_section_contents */
1145 if (asect
->flags
& SEC_RELOC
)
1147 /* emit a reloc section, and thus strtab and symtab... */
1148 Elf_Internal_Shdr
*rela_hdr
;
1149 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1151 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1153 /* orelocation has the data, reloc_count has the count... */
1156 rela_hdr
->sh_type
= SHT_RELA
;
1157 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1160 /* REL relocations */
1162 rela_hdr
->sh_type
= SHT_REL
;
1163 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rel
);
1165 rela_hdr
->sh_flags
= 0;
1166 rela_hdr
->sh_addr
= 0;
1167 rela_hdr
->sh_offset
= 0;
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;
1175 if (asect
->flags
& SEC_ALLOC
)
1177 this_hdr
->sh_flags
|= SHF_ALLOC
;
1178 if (asect
->flags
& SEC_LOAD
)
1180 /* @@ Do something with sh_type? */
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;
1190 if (!(asect
->flags
& SEC_READONLY
))
1191 this_hdr
->sh_flags
|= SHF_WRITE
;
1193 if (asect
->flags
& SEC_CODE
)
1194 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1198 write_relocs (abfd
, sec
, xxx
)
1203 Elf_Internal_Shdr
*rela_hdr
;
1204 Elf_External_Rela
*outbound_relocas
;
1205 Elf_External_Rel
*outbound_relocs
;
1207 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1208 asymbol
*last_sym
= 0;
1209 int last_sym_idx
= 9999999; /* should always be written before use */
1211 if ((sec
->flags
& SEC_RELOC
) == 0)
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
1218 if (sec
->reloc_count
== 0)
1221 rela_hdr
= &elf_section_data (sec
)->rel_hdr
;
1223 rela_hdr
->sh_size
= rela_hdr
->sh_entsize
* sec
->reloc_count
;
1224 rela_hdr
->contents
= (void *) bfd_alloc (abfd
, rela_hdr
->sh_size
);
1225 if (!rela_hdr
->contents
)
1227 bfd_set_error (bfd_error_no_memory
);
1228 abort (); /* FIXME */
1231 /* orelocation has the data, reloc_count has the count... */
1234 outbound_relocas
= (Elf_External_Rela
*) rela_hdr
->contents
;
1236 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1238 Elf_Internal_Rela dst_rela
;
1239 Elf_External_Rela
*src_rela
;
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
;
1249 dst_rela
.r_offset
= ptr
->address
;
1251 sym
= *ptr
->sym_ptr_ptr
;
1252 if (sym
== last_sym
)
1257 last_sym_idx
= n
= elf_symbol_from_bfd_symbol (abfd
, &sym
);
1259 dst_rela
.r_info
= ELF_R_INFO (n
, ptr
->howto
->type
);
1261 dst_rela
.r_addend
= ptr
->addend
;
1262 elf_swap_reloca_out (abfd
, &dst_rela
, src_rela
);
1266 /* REL relocations */
1268 outbound_relocs
= (Elf_External_Rel
*) rela_hdr
->contents
;
1270 for (idx
= 0; idx
< sec
->reloc_count
; idx
++)
1272 Elf_Internal_Rel dst_rel
;
1273 Elf_External_Rel
*src_rel
;
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
;
1284 dst_rel
.r_offset
= ptr
->address
;
1286 if (sym
== last_sym
)
1291 last_sym_idx
= n
= elf_symbol_from_bfd_symbol (abfd
, &sym
);
1293 dst_rel
.r_info
= ELF_R_INFO (n
, ptr
->howto
->type
);
1295 elf_swap_reloc_out (abfd
, &dst_rel
, src_rel
);
1301 fix_up_strtabs (abfd
, asect
, obj
)
1306 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (asect
)->this_hdr
;
1307 int this_idx
= elf_section_data (asect
)->this_idx
;
1309 /* @@ Check flags! */
1310 if (!strncmp (asect
->name
, ".stab", 5)
1311 && !strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
1313 size_t len
= strlen (asect
->name
) + 1;
1314 char *s
= (char *) malloc (len
);
1316 /* FIXME: Should deal more gracefully with errors. */
1318 strcpy (s
, asect
->name
);
1320 asect
= bfd_get_section_by_name (abfd
, s
);
1324 elf_section_data (asect
)->this_hdr
.sh_link
= this_idx
;
1325 /* @@ Assuming 32 bits! */
1326 elf_section_data (asect
)->this_hdr
.sh_entsize
= 0xc;
1328 this_hdr
->sh_type
= SHT_STRTAB
;
1333 elf_fake_sections (abfd
, asect
, obj
)
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. */
1341 Elf_Internal_Shdr
*this_hdr
;
1342 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1344 bfd_add_to_strtab (abfd
, elf_shstrtab (abfd
), asect
->name
);
1345 if (this_hdr
->sh_name
== (unsigned long) -1)
1346 abort (); /* FIXME */
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
;
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
))
1366 this_hdr
->sh_type
= SHT_PROGBITS
;
1367 else if ((asect
->flags
& SEC_ALLOC
) && ((asect
->flags
& SEC_LOAD
) == 0))
1369 BFD_ASSERT (strcmp (asect
->name
, ".bss") == 0
1370 || strcmp (asect
->name
, ".sbss") == 0);
1371 this_hdr
->sh_type
= SHT_NOBITS
;
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
;
1378 this_hdr
->sh_type
= SHT_PROGBITS
;
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;
1389 /* Now, check for processor-specific section types. */
1391 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1393 if (bed
->elf_backend_fake_sections
)
1394 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1398 /* Emit a strtab and symtab, and possibly a reloc section. */
1399 Elf_Internal_Shdr
*rela_hdr
;
1401 /* Note that only one symtab is used, so just remember it
1404 if (asect
->flags
& SEC_RELOC
)
1406 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1408 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1410 bfd_add_2_to_strtab (abfd
, elf_shstrtab (abfd
),
1411 use_rela_p
? ".rela" : ".rel",
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
));
1418 rela_hdr
->sh_flags
= 0;
1419 rela_hdr
->sh_addr
= 0;
1420 rela_hdr
->sh_size
= 0;
1421 rela_hdr
->sh_offset
= 0;
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;
1430 if (asect
->flags
& SEC_ALLOC
)
1432 this_hdr
->sh_flags
|= SHF_ALLOC
;
1433 if (asect
->flags
& SEC_LOAD
)
1435 /* @@ Do something with sh_type? */
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
;
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. */
1448 sym_is_global (abfd
, sym
)
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
)
1457 if (sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
))
1459 if (sym
->flags
& BSF_LOCAL
)
1463 if (sym
->section
== 0)
1465 /* Is this valid? */
1470 if (sym
->section
== &bfd_und_section
)
1472 if (bfd_is_com_section (sym
->section
))
1474 if (sym
->flags
& (BSF_LOCAL
| BSF_SECTION_SYM
| BSF_FILE
))
1480 elf_map_symbols (abfd
)
1483 int symcount
= bfd_get_symcount (abfd
);
1484 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1485 asymbol
**sect_syms
;
1487 int num_globals
= 0;
1488 int num_locals2
= 0;
1489 int num_globals2
= 0;
1491 int num_sections
= 0;
1492 Elf_Sym_Extra
*sym_extra
;
1497 fprintf (stderr
, "elf_map_symbols\n");
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. */
1505 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1507 if (max_index
< asect
->index
)
1508 max_index
= asect
->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
;
1518 bfd_set_error (bfd_error_no_memory
);
1522 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1524 asymbol
*sym
= bfd_make_empty_symbol (abfd
);
1527 bfd_set_error (bfd_error_no_memory
);
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
;
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
);
1547 syms
= (asymbol
**) bfd_realloc (abfd
, syms
,
1548 ((symcount
+ num_sections
+ 1)
1549 * sizeof (asymbol
*)));
1551 syms
= (asymbol
**) bfd_alloc (abfd
,
1552 (num_sections
+ 1) * sizeof (asymbol
*));
1555 bfd_set_error (bfd_error_no_memory
);
1559 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1561 if (sect_syms
[asect
->index
])
1562 syms
[symcount
++] = sect_syms
[asect
->index
];
1565 syms
[symcount
] = (asymbol
*) 0;
1566 bfd_set_symtab (abfd
, syms
, symcount
);
1569 elf_sym_extra (abfd
) = sym_extra
1570 = (Elf_Sym_Extra
*) bfd_alloc (abfd
, symcount
* sizeof (Elf_Sym_Extra
));
1573 bfd_set_error (bfd_error_no_memory
);
1577 /* Identify and classify all of the symbols. */
1578 for (idx
= 0; idx
< symcount
; idx
++)
1580 if (!sym_is_global (abfd
, syms
[idx
]))
1586 /* Now provide mapping information. Add +1 for skipping over the
1588 for (idx
= 0; idx
< symcount
; idx
++)
1590 syms
[idx
]->udata
= (PTR
) & sym_extra
[idx
];
1591 if (!sym_is_global (abfd
, syms
[idx
]))
1592 sym_extra
[idx
].elf_sym_num
= 1 + num_locals2
++;
1594 sym_extra
[idx
].elf_sym_num
= 1 + num_locals
+ num_globals2
++;
1597 elf_num_locals (abfd
) = num_locals
;
1598 elf_num_globals (abfd
) = num_globals
;
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. */
1607 elf_compute_section_file_positions (abfd
, link_info
)
1609 struct bfd_link_info
*link_info
;
1611 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1612 Elf_Internal_Shdr
*shstrtab_hdr
;
1614 if (abfd
->output_has_begun
)
1617 /* Do any elf backend specific processing first. */
1618 if (bed
->elf_backend_begin_write_processing
)
1619 (*bed
->elf_backend_begin_write_processing
) (abfd
);
1621 if (! prep_headers (abfd
))
1624 bfd_map_over_sections (abfd
, elf_fake_sections
, 0);
1626 if (!assign_section_numbers (abfd
))
1629 bfd_map_over_sections (abfd
, elf_make_sections
, 0);
1631 bfd_map_over_sections (abfd
, fix_up_strtabs
, 0); /* .stab/.stabstr &c */
1633 /* The backend linker builds symbol table information itself. */
1634 if (link_info
== NULL
)
1636 if (! swap_out_syms (abfd
))
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
;
1653 if (!assign_file_positions_except_relocs (abfd
,
1654 link_info
== NULL
? true : false))
1657 abfd
->output_has_begun
= true;
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.) */
1668 assign_section_numbers (abfd
)
1671 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1673 int section_number
= 1;
1675 Elf_Internal_Shdr
**i_shdrp
;
1677 t
->shstrtab_hdr
.sh_size
= elf_shstrtab (abfd
)->length
;
1678 t
->shstrtab_hdr
.contents
= (void *) elf_shstrtab (abfd
)->tab
;
1680 t
->shstrtab_section
= section_number
++;
1681 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1684 t
->symtab_section
= section_number
++;
1685 t
->strtab_section
= section_number
++;
1686 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1688 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1690 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1691 d
->this_idx
= section_number
++;
1692 if (sec
->flags
& SEC_RELOC
)
1694 d
->rel_idx
= section_number
++;
1695 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1696 d
->rel_hdr
.sh_info
= d
->this_idx
;
1700 /* No handling for per-section string tables currently. */
1702 elf_elfheader (abfd
)->e_shnum
= section_number
;
1704 /* Set up the list of section header pointers, in agreement with the
1706 i_shdrp
= ((Elf_Internal_Shdr
**)
1707 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1708 if (i_shdrp
== NULL
)
1710 bfd_set_error (bfd_error_no_memory
);
1713 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1714 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1715 if (i_shdrp
[0] == NULL
)
1717 bfd_release (abfd
, i_shdrp
);
1718 bfd_set_error (bfd_error_no_memory
);
1721 elf_elfsections (abfd
) = i_shdrp
;
1722 for (i
= 1; i
< section_number
; i
++)
1724 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1726 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1729 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1730 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1732 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1734 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1735 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1737 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1739 /* Make sure we got everything.... */
1740 for (i
= 0; i
< section_number
; i
++)
1741 if (i_shdrp
[i
] == NULL
)
1746 /* Align to the maximum file alignment that could be required for any
1747 ELF data structure. */
1749 static INLINE file_ptr
1750 align_file_position (off
)
1753 return (off
+ FILE_ALIGN
- 1) & ~(FILE_ALIGN
- 1);
1756 /* Assign a file position to a section, optionally aligning to the
1757 required section alignment. */
1759 static INLINE file_ptr
1760 assign_file_position_for_section (i_shdrp
, offset
, align
)
1761 Elf_Internal_Shdr
*i_shdrp
;
1769 al
= i_shdrp
->sh_addralign
;
1771 offset
= BFD_ALIGN (offset
, al
);
1773 i_shdrp
->sh_offset
= offset
;
1774 if (i_shdrp
->rawdata
!= NULL
)
1775 ((asection
*) i_shdrp
->rawdata
)->filepos
= offset
;
1776 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1777 offset
+= i_shdrp
->sh_size
;
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. */
1785 static bfd_size_type
1786 get_program_header_size (abfd
)
1792 /* Assume we will need exactly two PT_LOAD segments: one for text
1793 and one for data. */
1796 s
= bfd_get_section_by_name (abfd
, ".interp");
1797 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
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
1806 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
1808 /* We need a PT_DYNAMIC segment. */
1812 return segs
* sizeof (Elf_External_Phdr
);
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. */
1821 map_program_segments (abfd
, off
, first
, phdr_size
)
1824 Elf_Internal_Shdr
*first
;
1825 bfd_size_type phdr_size
;
1827 Elf_Internal_Phdr phdrs
[10];
1828 unsigned int phdr_count
;
1829 Elf_Internal_Phdr
*phdr
;
1830 int phdr_size_adjust
;
1832 Elf_Internal_Shdr
**hdrpp
;
1833 asection
*sinterp
, *sdyn
;
1834 unsigned int last_type
;
1835 Elf_Internal_Ehdr
*i_ehdrp
;
1837 BFD_ASSERT ((abfd
->flags
& EXEC_P
) != 0);
1838 BFD_ASSERT (phdr_size
/ sizeof (Elf_Internal_Phdr
)
1839 <= sizeof phdrs
/ sizeof (phdrs
[0]));
1844 phdr_size_adjust
= 0;
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)
1853 BFD_ASSERT (first
!= NULL
);
1855 phdr
->p_type
= PT_PHDR
;
1857 phdr
->p_offset
= off
;
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);
1864 /* The program header precedes all loadable sections. This lets
1865 us compute its loadable address. This depends on the linker
1867 phdr
->p_vaddr
= first
->sh_addr
- (phdr_size
+ phdr_size_adjust
);
1870 phdr
->p_filesz
= phdr_size
;
1871 phdr
->p_memsz
= phdr_size
;
1873 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
1874 phdr
->p_flags
= PF_R
| PF_X
;
1876 phdr
->p_align
= FILE_ALIGN
;
1877 BFD_ASSERT ((phdr
->p_vaddr
- phdr
->p_offset
) % FILE_ALIGN
== 0);
1879 /* Include the ELF header in the first loadable segment. */
1880 phdr_size_adjust
+= off
;
1885 phdr
->p_type
= PT_INTERP
;
1886 phdr
->p_offset
= sinterp
->filepos
;
1887 phdr
->p_vaddr
= sinterp
->vma
;
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
);
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
;
1907 Elf_Internal_Shdr
*hdr
;
1911 /* Ignore any section which will not be part of the process
1913 if ((hdr
->sh_flags
& SHF_ALLOC
) == 0)
1916 /* If this section fits in the segment we are constructing, add
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
))
1923 bfd_size_type adjust
;
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
;
1937 /* If we have a segment, move to the next one. */
1938 if (phdr
->p_type
!= PT_NULL
)
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
;
1949 if (hdr
->sh_type
== SHT_NOBITS
)
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
;
1963 && (sinterp
->flags
& SEC_LOAD
) != 0)
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
;
1971 last_type
= hdr
->sh_type
;
1974 if (phdr
->p_type
!= PT_NULL
)
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)
1984 phdr
->p_type
= PT_DYNAMIC
;
1985 phdr
->p_offset
= sdyn
->filepos
;
1986 phdr
->p_vaddr
= sdyn
->vma
;
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
);
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
))
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
;
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
*)
2017 * sizeof (Elf_Internal_Phdr
))));
2018 if (elf_tdata (abfd
)->phdr
== NULL
&& phdr_count
!= 0)
2020 bfd_set_error (bfd_error_no_memory
);
2021 return (file_ptr
) -1;
2023 memcpy (elf_tdata (abfd
)->phdr
, phdrs
,
2024 phdr_count
* sizeof (Elf_Internal_Phdr
));
2026 /* Write out the program headers. */
2027 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0)
2028 return (file_ptr
) -1;
2030 for (i
= 0, phdr
= phdrs
; i
< phdr_count
; i
++, phdr
++)
2032 Elf_External_Phdr extphdr
;
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;
2040 return off
+ phdr_count
* sizeof (Elf_External_Phdr
);
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.
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.
2052 If DOSYMS is false, we do not assign file positions for the symbol
2053 table or the string table. */
2056 assign_file_positions_except_relocs (abfd
, dosyms
)
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
);
2065 /* Start after the ELF header. */
2066 off
= i_ehdrp
->e_ehsize
;
2068 if ((abfd
->flags
& EXEC_P
) == 0)
2070 Elf_Internal_Shdr
**hdrpp
;
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
++)
2078 Elf_Internal_Shdr
*hdr
;
2081 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
2083 hdr
->sh_offset
= -1;
2087 && (i
== tdata
->symtab_section
2088 || i
== tdata
->strtab_section
))
2090 hdr
->sh_offset
= -1;
2094 off
= assign_file_position_for_section (hdr
, off
, true);
2100 bfd_size_type phdr_size
;
2101 bfd_vma maxpagesize
;
2102 Elf_Internal_Shdr
**hdrpp
;
2104 Elf_Internal_Shdr
*first
;
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)
2118 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2119 if (maxpagesize
== 0)
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. */
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. */
2130 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
2132 Elf_Internal_Shdr
*hdr
;
2135 if ((hdr
->sh_flags
& SHF_ALLOC
) == 0)
2141 if ((abfd
->flags
& D_PAGED
) != 0)
2143 /* The section VMA must equal the file position modulo
2144 the page size. This is required by the program
2146 off
+= (hdr
->sh_addr
- off
) % maxpagesize
;
2149 off
= assign_file_position_for_section (hdr
, off
, false);
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
++)
2156 Elf_Internal_Shdr
*hdr
;
2159 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2161 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
2163 hdr
->sh_offset
= -1;
2167 && (i
== tdata
->symtab_section
2168 || i
== tdata
->strtab_section
))
2170 hdr
->sh_offset
= -1;
2174 off
= assign_file_position_for_section (hdr
, off
, true);
2177 phdr_map
= map_program_segments (abfd
, phdr_off
, first
, phdr_size
);
2178 if (phdr_map
== (file_ptr
) -1)
2180 BFD_ASSERT (phdr_map
== phdr_off
+ phdr_size
);
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
;
2188 elf_tdata (abfd
)->next_file_pos
= off
;
2197 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2198 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2199 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2201 struct strtab
*shstrtab
;
2203 i_ehdrp
= elf_elfheader (abfd
);
2204 i_shdrp
= elf_elfsections (abfd
);
2206 shstrtab
= bfd_new_strtab (abfd
);
2210 elf_shstrtab (abfd
) = shstrtab
;
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
;
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
;
2222 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
2223 i_ehdrp
->e_ident
[count
] = 0;
2225 i_ehdrp
->e_type
= (abfd
->flags
& EXEC_P
) ? ET_EXEC
: ET_REL
;
2226 switch (bfd_get_arch (abfd
))
2228 case bfd_arch_unknown
:
2229 i_ehdrp
->e_machine
= EM_NONE
;
2231 case bfd_arch_sparc
:
2232 i_ehdrp
->e_machine
= EM_SPARC
;
2233 /* start-sanitize-v9 */
2235 i_ehdrp
->e_machine
= EM_SPARC64
;
2237 /* end-sanitize-v9 */
2240 i_ehdrp
->e_machine
= EM_386
;
2243 i_ehdrp
->e_machine
= EM_68K
;
2246 i_ehdrp
->e_machine
= EM_88K
;
2249 i_ehdrp
->e_machine
= EM_860
;
2251 case bfd_arch_mips
: /* MIPS Rxxxx */
2252 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
2255 i_ehdrp
->e_machine
= EM_PARISC
;
2257 case bfd_arch_powerpc
:
2258 i_ehdrp
->e_machine
= EM_CYGNUS_POWERPC
;
2260 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2262 i_ehdrp
->e_machine
= EM_NONE
;
2264 i_ehdrp
->e_version
= EV_CURRENT
;
2265 i_ehdrp
->e_ehsize
= sizeof (Elf_External_Ehdr
);
2267 /* no program header, for now. */
2268 i_ehdrp
->e_phoff
= 0;
2269 i_ehdrp
->e_phentsize
= 0;
2270 i_ehdrp
->e_phnum
= 0;
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
);
2276 /* if we're building an executable, we'll need a program header table */
2277 if (abfd
->flags
& EXEC_P
)
2279 /* it all happens later */
2281 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
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
;
2292 i_ehdrp
->e_phentsize
= 0;
2294 i_ehdrp
->e_phoff
= 0;
2297 elf_tdata (abfd
)->symtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
2299 elf_tdata (abfd
)->strtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
2301 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
= bfd_add_to_strtab (abfd
, shstrtab
,
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)
2312 swap_out_syms (abfd
)
2315 if (!elf_map_symbols (abfd
))
2318 /* Dump out the symtabs. */
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
;
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;
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
2339 symtab_hdr
->sh_addralign
= 4;
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
;
2345 outbound_syms
= (Elf_External_Sym
*)
2346 bfd_alloc (abfd
, (1 + symcount
) * sizeof (Elf_External_Sym
));
2349 bfd_set_error (bfd_error_no_memory
);
2352 /* now generate the data (for "contents") */
2354 /* Fill in zeroth symbol and swap it out. */
2355 Elf_Internal_Sym sym
;
2361 sym
.st_shndx
= SHN_UNDEF
;
2362 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
);
2364 for (idx
= 0; idx
< symcount
; idx
++)
2366 Elf_Internal_Sym sym
;
2367 bfd_vma value
= syms
[idx
]->value
;
2369 if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2370 /* Section symbols have no names. */
2374 sym
.st_name
= bfd_add_to_strtab (abfd
, stt
, syms
[idx
]->name
);
2375 if (sym
.st_name
== (unsigned long) -1)
2379 if (bfd_is_com_section (syms
[idx
]->section
))
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... */
2387 sym
.st_shndx
= elf_section_from_bfd_section (abfd
,
2388 syms
[idx
]->section
);
2392 asection
*sec
= syms
[idx
]->section
;
2393 elf_symbol_type
*type_ptr
;
2396 if (sec
->output_section
)
2398 value
+= sec
->output_offset
;
2399 sec
= sec
->output_section
;
2402 sym
.st_value
= value
;
2403 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
2404 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
2405 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec
);
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
2415 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
2416 BFD_ASSERT (sec2
!= 0);
2417 sym
.st_shndx
= shndx
= elf_section_from_bfd_section (abfd
, sec2
);
2418 BFD_ASSERT (shndx
!= -1);
2422 if (bfd_is_com_section (syms
[idx
]->section
))
2423 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_OBJECT
);
2424 else if (syms
[idx
]->section
== &bfd_und_section
)
2425 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_NOTYPE
);
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
);
2432 int bind
= STB_LOCAL
;
2433 int type
= STT_OBJECT
;
2434 unsigned int flags
= syms
[idx
]->flags
;
2436 if (flags
& BSF_LOCAL
)
2438 else if (flags
& BSF_WEAK
)
2440 else if (flags
& BSF_GLOBAL
)
2443 if (flags
& BSF_FUNCTION
)
2446 sym
.st_info
= ELF_ST_INFO (bind
, type
);
2450 elf_swap_symbol_out (abfd
, &sym
,
2452 + elf_sym_extra (abfd
)[idx
].elf_sym_num
));
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
;
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;
2465 symstrtab_hdr
->sh_addralign
= 1;
2466 symstrtab_hdr
->size
= 0;
2473 write_shdrs_and_ehdr (abfd
)
2476 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
2477 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2478 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
2479 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2481 struct strtab
*shstrtab
;
2483 i_ehdrp
= elf_elfheader (abfd
);
2484 i_shdrp
= elf_elfsections (abfd
);
2485 shstrtab
= elf_shstrtab (abfd
);
2487 /* swap the header before spitting it out... */
2490 elf_debug_file (i_ehdrp
);
2492 elf_swap_ehdr_out (abfd
, i_ehdrp
, &x_ehdr
);
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
)))
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
));
2503 bfd_set_error (bfd_error_no_memory
);
2507 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2510 elf_debug_section (shstrtab
->tab
+ i_shdrp
[count
]->sh_name
, count
,
2513 elf_swap_shdr_out (abfd
, i_shdrp
[count
], x_shdrp
+ count
);
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
)
2517 != sizeof (*x_shdrp
) * i_ehdrp
->e_shnum
))
2520 /* need to dump the string table too... */
2525 /* Assign file positions for all the reloc sections which are not part
2526 of the loadable file image. */
2529 assign_file_positions_for_relocs (abfd
)
2534 Elf_Internal_Shdr
**shdrpp
;
2536 off
= elf_tdata (abfd
)->next_file_pos
;
2538 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
2539 i
< elf_elfheader (abfd
)->e_shnum
;
2542 Elf_Internal_Shdr
*shdrp
;
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);
2550 elf_tdata (abfd
)->next_file_pos
= off
;
2554 NAME(bfd_elf
,write_object_contents
) (abfd
)
2557 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2558 Elf_Internal_Ehdr
*i_ehdrp
;
2559 Elf_Internal_Shdr
**i_shdrp
;
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)
2566 fprintf (stderr
, "Writing ELF dynamic objects is not supported\n");
2567 bfd_set_error (bfd_error_wrong_format
);
2571 if (! abfd
->output_has_begun
2572 && ! elf_compute_section_file_positions (abfd
,
2573 (struct bfd_link_info
*) NULL
))
2576 i_shdrp
= elf_elfsections (abfd
);
2577 i_ehdrp
= elf_elfheader (abfd
);
2579 bfd_map_over_sections (abfd
, write_relocs
, (PTR
) 0);
2580 assign_file_positions_for_relocs (abfd
);
2582 /* After writing the headers, we need to write the sections too... */
2583 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
2585 if (bed
->elf_backend_section_processing
)
2586 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
2587 if (i_shdrp
[count
]->contents
)
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
,
2592 != i_shdrp
[count
]->sh_size
))
2597 if (bed
->elf_backend_final_write_processing
)
2598 (*bed
->elf_backend_final_write_processing
) (abfd
);
2600 return write_shdrs_and_ehdr (abfd
);
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. */
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... */
2613 section_from_elf_index (abfd
, index
)
2617 /* @@ Is bfd_com_section really correct in all the places it could
2618 be returned from this routine? */
2620 if (index
== SHN_ABS
)
2621 return &bfd_com_section
; /* not abs? */
2622 if (index
== SHN_COMMON
)
2623 return &bfd_com_section
;
2625 if (index
> elf_elfheader (abfd
)->e_shnum
)
2629 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[index
];
2631 switch (hdr
->sh_type
)
2633 /* ELF sections that map to BFD sections */
2638 if (hdr
->rawdata
== NULL
)
2640 if (! bfd_section_from_shdr (abfd
, index
))
2643 return (struct sec
*) hdr
->rawdata
;
2646 return (struct sec
*) &bfd_abs_section
;
2651 /* given a section, search the header to find them... */
2653 elf_section_from_bfd_section (abfd
, asect
)
2657 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
2659 Elf_Internal_Shdr
*hdr
;
2660 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2662 if (asect
== &bfd_abs_section
)
2664 if (asect
== &bfd_com_section
)
2666 if (asect
== &bfd_und_section
)
2669 for (index
= 0; index
< maxindex
; index
++)
2671 hdr
= i_shdrp
[index
];
2672 switch (hdr
->sh_type
)
2674 /* ELF sections that map to BFD sections */
2682 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2688 /* fix_up_strtabs will generate STRTAB sections with names
2690 if (!strncmp (asect
->name
, ".stab", 5)
2691 && !strcmp ("str", asect
->name
+ strlen (asect
->name
) - 3))
2695 if (((struct sec
*) (hdr
->rawdata
)) == asect
)
2703 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2705 if (bed
->elf_backend_section_from_bfd_section
)
2710 if ((*bed
->elf_backend_section_from_bfd_section
)
2711 (abfd
, hdr
, asect
, &retval
))
2721 /* given a symbol, return the bfd index for that symbol. */
2723 elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
2725 struct symbol_cache_entry
**asym_ptr_ptr
;
2727 struct symbol_cache_entry
*asym_ptr
= *asym_ptr_ptr
;
2729 flagword flags
= asym_ptr
->flags
;
2731 /* When gas creates relocations against local labels, it creates its
2732 own symbol for the section, but does put the symbol into the
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. */
2736 if (asym_ptr
->udata
== (PTR
) 0
2737 && (flags
& BSF_SECTION_SYM
)
2738 && asym_ptr
->section
)
2742 if (asym_ptr
->section
->output_section
!= NULL
)
2743 indx
= asym_ptr
->section
->output_section
->index
;
2745 indx
= asym_ptr
->section
->index
;
2746 if (elf_section_syms (abfd
)[indx
])
2747 asym_ptr
->udata
= elf_section_syms (abfd
)[indx
]->udata
;
2750 if (asym_ptr
->udata
)
2751 idx
= ((Elf_Sym_Extra
*) asym_ptr
->udata
)->elf_sym_num
;
2761 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n",
2762 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
, elf_symbol_flags (flags
));
2771 elf_slurp_symbol_table (abfd
, symptrs
, dynamic
)
2773 asymbol
**symptrs
; /* Buffer for generated bfd symbols */
2776 Elf_Internal_Shdr
*hdr
;
2777 long symcount
; /* Number of external ELF symbols */
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
;
2781 Elf_External_Sym
*x_symp
= NULL
;
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.
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. */
2794 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2796 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2797 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2800 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2803 sym
= symbase
= NULL
;
2808 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2811 symbase
= ((elf_symbol_type
*)
2812 bfd_zalloc (abfd
, symcount
* sizeof (elf_symbol_type
)));
2813 if (symbase
== (elf_symbol_type
*) NULL
)
2815 bfd_set_error (bfd_error_no_memory
);
2820 /* Temporarily allocate room for the raw ELF symbols. */
2821 x_symp
= ((Elf_External_Sym
*)
2822 malloc (symcount
* sizeof (Elf_External_Sym
)));
2823 if (x_symp
== NULL
&& symcount
!= 0)
2825 bfd_set_error (bfd_error_no_memory
);
2829 if (bfd_read ((PTR
) x_symp
, sizeof (Elf_External_Sym
), symcount
, abfd
)
2830 != symcount
* sizeof (Elf_External_Sym
))
2832 /* Skip first symbol, which is a null dummy. */
2833 for (i
= 1; i
< symcount
; i
++)
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
));
2840 sym
->symbol
.the_bfd
= abfd
;
2842 sym
->symbol
.name
= elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2845 sym
->symbol
.value
= i_sym
.st_value
;
2847 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERVE
)
2849 sym
->symbol
.section
= section_from_elf_index (abfd
,
2851 if (sym
->symbol
.section
== NULL
)
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
;
2859 else if (i_sym
.st_shndx
== SHN_ABS
)
2861 sym
->symbol
.section
= &bfd_abs_section
;
2863 else if (i_sym
.st_shndx
== SHN_COMMON
)
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
;
2872 else if (i_sym
.st_shndx
== SHN_UNDEF
)
2874 sym
->symbol
.section
= &bfd_und_section
;
2877 sym
->symbol
.section
= &bfd_abs_section
;
2879 sym
->symbol
.value
-= sym
->symbol
.section
->vma
;
2881 switch (ELF_ST_BIND (i_sym
.st_info
))
2884 sym
->symbol
.flags
|= BSF_LOCAL
;
2887 sym
->symbol
.flags
|= BSF_GLOBAL
;
2890 sym
->symbol
.flags
|= BSF_WEAK
;
2894 switch (ELF_ST_TYPE (i_sym
.st_info
))
2897 sym
->symbol
.flags
|= BSF_SECTION_SYM
| BSF_DEBUGGING
;
2900 sym
->symbol
.flags
|= BSF_FILE
| BSF_DEBUGGING
;
2903 sym
->symbol
.flags
|= BSF_FUNCTION
;
2908 sym
->symbol
.flags
|= BSF_DYNAMIC
;
2910 /* Do some backend-specific processing on this symbol. */
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
);
2921 /* Do some backend-specific processing on this symbol table. */
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
);
2928 /* We rely on the zalloc to clear out the final symbol entry. */
2930 symcount
= sym
- symbase
;
2932 /* Fill in the user's symbol pointer vector if needed. */
2940 *symptrs
++ = &sym
->symbol
;
2943 *symptrs
= 0; /* Final null pointer */
2955 /* Return the number of bytes required to hold the symtab vector.
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. */
2962 elf_get_symtab_upper_bound (abfd
)
2967 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2969 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2970 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2976 elf_get_dynamic_symtab_upper_bound (abfd
)
2981 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2983 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2984 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2990 This function return the number of bytes required to store the
2991 relocation information associated with section <<sect>>
2992 attached to bfd <<abfd>>
2996 elf_get_reloc_upper_bound (abfd
, asect
)
3000 if (asect
->flags
& SEC_RELOC
)
3002 /* either rel or rela */
3003 return elf_section_data (asect
)->rel_hdr
.sh_size
;
3010 elf_slurp_reloca_table (abfd
, asect
, symbols
)
3015 Elf_External_Rela
*native_relocs
;
3016 arelent
*reloc_cache
;
3021 if (asect
->relocation
)
3023 if (asect
->reloc_count
== 0)
3025 if (asect
->flags
& SEC_CONSTRUCTOR
)
3028 if (bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
) != 0)
3030 native_relocs
= (Elf_External_Rela
*)
3031 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rela
));
3034 bfd_set_error (bfd_error_no_memory
);
3037 if (bfd_read ((PTR
) native_relocs
,
3038 sizeof (Elf_External_Rela
), asect
->reloc_count
, abfd
)
3039 != sizeof (Elf_External_Rela
) * asect
->reloc_count
)
3042 reloc_cache
= (arelent
*)
3043 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
3047 bfd_set_error (bfd_error_no_memory
);
3051 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
3053 Elf_Internal_Rela dst
;
3054 Elf_External_Rela
*src
;
3056 cache_ptr
= reloc_cache
+ idx
;
3057 src
= native_relocs
+ idx
;
3058 elf_swap_reloca_in (abfd
, src
, &dst
);
3060 #ifdef RELOC_PROCESSING
3061 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
3063 if (asect
->flags
& SEC_RELOC
)
3065 /* relocatable, so the offset is off of the section */
3066 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
3070 /* non-relocatable, so the offset a virtual address */
3071 cache_ptr
->address
= dst
.r_offset
;
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
;
3083 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
3085 /* Translate any ELF section symbol into a BFD section
3087 s
= *(cache_ptr
->sym_ptr_ptr
);
3088 if (s
->flags
& BSF_SECTION_SYM
)
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)
3096 cache_ptr
->addend
= dst
.r_addend
;
3098 /* Fill in the cache_ptr->howto field from dst.r_type */
3100 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
3101 (*ebd
->elf_info_to_howto
) (abfd
, cache_ptr
, &dst
);
3106 asect
->relocation
= reloc_cache
;
3112 elf_debug_section (str
, num
, hdr
)
3115 Elf_Internal_Shdr
*hdr
;
3117 fprintf (stderr
, "\nSection#%d '%s' 0x%.8lx\n", num
, str
, (long) hdr
);
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
);
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
);
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
);
3142 elf_debug_file (ehdrp
)
3143 Elf_Internal_Ehdr
*ehdrp
;
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
);
3156 elf_slurp_reloc_table (abfd
, asect
, symbols
)
3161 Elf_External_Rel
*native_relocs
;
3162 arelent
*reloc_cache
;
3164 Elf_Internal_Shdr
*data_hdr
;
3166 unsigned long data_max
;
3167 char buf
[4]; /* FIXME -- might be elf64 */
3171 if (asect
->relocation
)
3173 if (asect
->reloc_count
== 0)
3175 if (asect
->flags
& SEC_CONSTRUCTOR
)
3178 if (bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
) != 0)
3180 native_relocs
= (Elf_External_Rel
*)
3181 bfd_alloc (abfd
, asect
->reloc_count
* sizeof (Elf_External_Rel
));
3184 bfd_set_error (bfd_error_no_memory
);
3187 if (bfd_read ((PTR
) native_relocs
,
3188 sizeof (Elf_External_Rel
), asect
->reloc_count
, abfd
)
3189 != sizeof (Elf_External_Rel
) * asect
->reloc_count
)
3192 reloc_cache
= (arelent
*)
3193 bfd_alloc (abfd
, (size_t) (asect
->reloc_count
* sizeof (arelent
)));
3197 bfd_set_error (bfd_error_no_memory
);
3201 /* Get the offset of the start of the segment we are relocating to read in
3202 the implicit addend. */
3203 data_hdr
= &elf_section_data (asect
)->this_hdr
;
3204 data_off
= data_hdr
->sh_offset
;
3205 data_max
= data_hdr
->sh_size
- sizeof (buf
) + 1;
3208 elf_debug_section ("data section", -1, data_hdr
);
3211 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
3213 #ifdef RELOC_PROCESSING
3214 Elf_Internal_Rel dst
;
3215 Elf_External_Rel
*src
;
3217 cache_ptr
= reloc_cache
+ idx
;
3218 src
= native_relocs
+ idx
;
3219 elf_swap_reloc_in (abfd
, src
, &dst
);
3221 RELOC_PROCESSING (cache_ptr
, &dst
, symbols
, abfd
, asect
);
3223 Elf_Internal_Rel dst
;
3224 Elf_External_Rel
*src
;
3226 cache_ptr
= reloc_cache
+ idx
;
3227 src
= native_relocs
+ idx
;
3229 elf_swap_reloc_in (abfd
, src
, &dst
);
3231 if (asect
->flags
& SEC_RELOC
)
3233 /* relocatable, so the offset is off of the section */
3234 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
3238 /* non-relocatable, so the offset a virtual address */
3239 cache_ptr
->address
= dst
.r_offset
;
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
;
3251 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM (dst
.r_info
) - 1;
3253 /* Translate any ELF section symbol into a BFD section
3255 s
= *(cache_ptr
->sym_ptr_ptr
);
3256 if (s
->flags
& BSF_SECTION_SYM
)
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)
3264 BFD_ASSERT (dst
.r_offset
<= data_max
);
3265 cache_ptr
->addend
= 0;
3267 /* Fill in the cache_ptr->howto field from dst.r_type */
3269 struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
3270 (*ebd
->elf_info_to_howto_rel
) (abfd
, cache_ptr
, &dst
);
3275 asect
->relocation
= reloc_cache
;
3280 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
3286 arelent
*tblptr
= section
->relocation
;
3287 unsigned int count
= 0;
3288 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
3290 /* snarfed from coffcode.h */
3293 if (! elf_slurp_reloca_table (abfd
, section
, symbols
))
3298 if (! elf_slurp_reloc_table (abfd
, section
, symbols
))
3302 tblptr
= section
->relocation
;
3304 for (; count
++ < section
->reloc_count
;)
3305 *relptr
++ = tblptr
++;
3308 return section
->reloc_count
;
3312 elf_get_symtab (abfd
, alocation
)
3314 asymbol
**alocation
;
3316 long symcount
= elf_slurp_symbol_table (abfd
, alocation
, false);
3319 bfd_get_symcount (abfd
) = symcount
;
3324 elf_canonicalize_dynamic_symtab (abfd
, alocation
)
3326 asymbol
**alocation
;
3328 return elf_slurp_symbol_table (abfd
, alocation
, true);
3332 elf_make_empty_symbol (abfd
)
3335 elf_symbol_type
*newsym
;
3337 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
3340 bfd_set_error (bfd_error_no_memory
);
3345 newsym
->symbol
.the_bfd
= abfd
;
3346 return &newsym
->symbol
;
3351 elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
3356 bfd_symbol_info (symbol
, ret
);
3360 elf_print_symbol (ignore_abfd
, filep
, symbol
, how
)
3364 bfd_print_symbol_type how
;
3366 FILE *file
= (FILE *) filep
;
3369 case bfd_print_symbol_name
:
3370 fprintf (file
, "%s", symbol
->name
);
3372 case bfd_print_symbol_more
:
3373 fprintf (file
, "elf ");
3374 fprintf_vma (file
, symbol
->value
);
3375 fprintf (file
, " %lx", (long) symbol
->flags
);
3377 case bfd_print_symbol_all
:
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",
3392 elf_get_lineno (ignore_abfd
, symbol
)
3396 fprintf (stderr
, "elf_get_lineno unimplemented\n");
3403 elf_set_arch_mach (abfd
, arch
, machine
)
3405 enum bfd_architecture arch
;
3406 unsigned long machine
;
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
)
3415 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
3419 elf_find_nearest_line (abfd
,
3430 CONST
char **filename_ptr
;
3431 CONST
char **functionname_ptr
;
3432 unsigned int *line_ptr
;
3438 elf_sizeof_headers (abfd
, reloc
)
3444 ret
= sizeof (Elf_External_Ehdr
);
3446 ret
+= get_program_header_size (abfd
);
3451 elf_set_section_contents (abfd
, section
, location
, offset
, count
)
3456 bfd_size_type count
;
3458 Elf_Internal_Shdr
*hdr
;
3460 if (! abfd
->output_has_begun
3461 && ! elf_compute_section_file_positions (abfd
,
3462 (struct bfd_link_info
*) NULL
))
3465 hdr
= &elf_section_data (section
)->this_hdr
;
3467 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
3469 if (bfd_write (location
, 1, count
, abfd
) != count
)
3476 elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
3479 Elf_Internal_Rela
*dst
;
3481 fprintf (stderr
, "elf RELA relocation support for target machine unimplemented\n");
3487 elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
3490 Elf_Internal_Rel
*dst
;
3492 fprintf (stderr
, "elf REL relocation support for target machine unimplemented\n");
3498 /* Core file support */
3500 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
3501 #include <sys/procfs.h>
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
3511 bfd_prstatus (abfd
, descdata
, descsz
, filepos
)
3518 prstatus_t
*status
= (prstatus_t
*) 0;
3520 if (descsz
== sizeof (prstatus_t
))
3522 newsect
= bfd_make_section (abfd
, ".reg");
3523 if (newsect
== NULL
)
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
)
3531 memcpy (core_prstatus (abfd
), descdata
, descsz
);
3537 /* Stash a copy of the prpsinfo structure away for future use. */
3540 bfd_prpsinfo (abfd
, descdata
, descsz
, filepos
)
3546 if (descsz
== sizeof (prpsinfo_t
))
3548 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) == NULL
)
3550 bfd_set_error (bfd_error_no_memory
);
3553 memcpy (core_prpsinfo (abfd
), descdata
, descsz
);
3559 bfd_fpregset (abfd
, descdata
, descsz
, filepos
)
3567 newsect
= bfd_make_section (abfd
, ".reg2");
3568 if (newsect
== NULL
)
3570 newsect
->_raw_size
= descsz
;
3571 newsect
->filepos
= filepos
;
3572 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3573 newsect
->alignment_power
= 2;
3577 #endif /* HAVE_PROCFS */
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
3586 elf_core_file_failing_command (abfd
)
3590 if (core_prpsinfo (abfd
))
3592 prpsinfo_t
*p
= core_prpsinfo (abfd
);
3593 char *scan
= p
->pr_psargs
;
3598 if ((scan
> p
->pr_psargs
) && (*scan
== ' '))
3602 return p
->pr_psargs
;
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.
3614 elf_core_file_failing_signal (abfd
)
3618 if (core_prstatus (abfd
))
3620 return ((prstatus_t
*) (core_prstatus (abfd
)))->pr_cursig
;
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
3633 elf_core_file_matches_executable_p (core_bfd
, exec_bfd
)
3642 /* First, xvecs must match since both are ELF files for the same target. */
3644 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
3646 bfd_set_error (bfd_error_system_call
);
3652 /* If no prpsinfo, just return true. Otherwise, grab the last component
3653 of the exec'd pathname from the prpsinfo. */
3655 if (core_prpsinfo (core_bfd
))
3657 corename
= (((struct prpsinfo
*) core_prpsinfo (core_bfd
))->pr_fname
);
3664 /* Find the last component of the executable pathname. */
3666 if ((execname
= strrchr (exec_bfd
->filename
, '/')) != NULL
)
3672 execname
= (char *) exec_bfd
->filename
;
3675 /* See if they match */
3677 return strcmp (execname
, corename
) ? false : true;
3683 #endif /* HAVE_PROCFS */
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.
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.
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
3709 elf_corefile_note (abfd
, hdr
)
3711 Elf_Internal_Phdr
*hdr
;
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 */
3722 if (hdr
->p_filesz
> 0
3723 && (buf
= (char *) malloc (hdr
->p_filesz
)) != NULL
3724 && bfd_seek (abfd
, hdr
->p_offset
, SEEK_SET
) != -1
3725 && bfd_read ((PTR
) buf
, hdr
->p_filesz
, 1, abfd
) == hdr
->p_filesz
)
3727 x_note_p
= (Elf_External_Note
*) buf
;
3728 while ((char *) x_note_p
< (buf
+ hdr
->p_filesz
))
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
)
3739 /* process descdata as prstatus info */
3740 if (! bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
))
3742 sectname
= ".prstatus";
3745 /* process descdata as fpregset info */
3746 if (! bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
))
3748 sectname
= ".fpregset";
3751 /* process descdata as prpsinfo */
3752 if (! bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
))
3754 sectname
= ".prpsinfo";
3757 /* Unknown descriptor, just ignore it. */
3761 if (sectname
!= NULL
)
3763 newsect
= bfd_make_section (abfd
, sectname
);
3764 if (newsect
== NULL
)
3766 newsect
->_raw_size
= i_note
.descsz
;
3767 newsect
->filepos
= filepos
;
3768 newsect
->flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
3769 newsect
->alignment_power
= 2;
3771 x_note_p
= (Elf_External_Note
*)
3772 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
3779 else if (hdr
->p_filesz
> 0)
3781 bfd_set_error (bfd_error_no_memory
);
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.
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).
3802 elf_core_file_p (abfd
)
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
;
3810 struct elf_backend_data
*ebd
;
3812 /* Read in the ELF header in external format. */
3814 if (bfd_read ((PTR
) & x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
3816 if (bfd_get_error () != bfd_error_system_call
)
3817 bfd_set_error (bfd_error_wrong_format
);
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
3827 if (elf_file_p (&x_ehdr
) == false)
3830 bfd_set_error (bfd_error_wrong_format
);
3834 /* FIXME, Check EI_VERSION here ! */
3838 int desired_address_size
= ELFCLASS32
;
3841 int desired_address_size
= ELFCLASS64
;
3844 if (x_ehdr
.e_ident
[EI_CLASS
] != desired_address_size
)
3848 /* Switch xvec to match the specified byte order. */
3849 switch (x_ehdr
.e_ident
[EI_DATA
])
3851 case ELFDATA2MSB
: /* Big-endian */
3852 if (abfd
->xvec
->byteorder_big_p
== false)
3855 case ELFDATA2LSB
: /* Little-endian */
3856 if (abfd
->xvec
->byteorder_big_p
== true)
3859 case ELFDATANONE
: /* No data encoding specified */
3860 default: /* Unknown data encoding specified */
3864 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
3865 the tdata pointer in the bfd. */
3868 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
3869 if (elf_tdata (abfd
) == NULL
)
3871 bfd_set_error (bfd_error_no_memory
);
3875 /* FIXME, `wrong' returns from this point onward, leak memory. */
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
);
3881 elf_debug_file (i_ehdrp
);
3884 ebd
= get_elf_backend_data (abfd
);
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
)
3890 bfd_target
**target_ptr
;
3892 if (ebd
->elf_machine_code
!= EM_NONE
)
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
++)
3899 struct elf_backend_data
*back
;
3901 if ((*target_ptr
)->flavour
!= bfd_target_elf_flavour
)
3903 back
= (struct elf_backend_data
*) (*target_ptr
)->backend_data
;
3904 if (back
->elf_machine_code
== i_ehdrp
->e_machine
)
3906 /* target_ptr is an ELF backend which matches this
3907 object file, so reject the generic ELF target. */
3913 /* If there is no program header, or the type is not a core file, then
3915 if (i_ehdrp
->e_phoff
== 0 || i_ehdrp
->e_type
!= ET_CORE
)
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. */
3924 if (i_ehdrp
->e_phentsize
!= sizeof (x_phdr
))
3926 i_phdrp
= (Elf_Internal_Phdr
*)
3927 bfd_alloc (abfd
, sizeof (*i_phdrp
) * i_ehdrp
->e_phnum
);
3930 bfd_set_error (bfd_error_no_memory
);
3933 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) == -1)
3935 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3937 if (bfd_read ((PTR
) & x_phdr
, sizeof (x_phdr
), 1, abfd
)
3940 elf_swap_phdr_in (abfd
, &x_phdr
, i_phdrp
+ phindex
);
3943 /* Once all of the program headers have been read and converted, we
3944 can start processing them. */
3946 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
3948 bfd_section_from_phdr (abfd
, i_phdrp
+ phindex
, phindex
);
3949 if ((i_phdrp
+ phindex
)->p_type
== PT_NOTE
)
3951 if (! elf_corefile_note (abfd
, i_phdrp
+ phindex
))
3956 /* Remember the entry point specified in the ELF file header. */
3958 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
3963 /* ELF linker code. */
3965 static boolean elf_link_add_object_symbols
3966 PARAMS ((bfd
*, struct bfd_link_info
*));
3967 static boolean elf_link_add_archive_symbols
3968 PARAMS ((bfd
*, struct bfd_link_info
*));
3969 static INLINE boolean elf_link_record_dynamic_symbol
3970 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
3971 static boolean elf_link_create_dynamic_sections
3972 PARAMS ((bfd
*, struct bfd_link_info
*));
3973 static boolean elf_adjust_dynamic_symbol
3974 PARAMS ((struct elf_link_hash_entry
*, PTR
));
3976 /* Given an ELF BFD, add symbols to the global hash table as
3980 elf_bfd_link_add_symbols (abfd
, info
)
3982 struct bfd_link_info
*info
;
3984 switch (bfd_get_format (abfd
))
3987 return elf_link_add_object_symbols (abfd
, info
);
3989 return elf_link_add_archive_symbols (abfd
, info
);
3991 bfd_set_error (bfd_error_wrong_format
);
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.
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
4018 Unfortunately, we do have to make multiple passes over the symbol
4019 table until nothing further is resolved. */
4022 elf_link_add_archive_symbols (abfd
, info
)
4024 struct bfd_link_info
*info
;
4027 boolean
*defined
= NULL
;
4028 boolean
*included
= NULL
;
4032 if (! bfd_has_map (abfd
))
4034 bfd_set_error (bfd_error_no_symbols
);
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
;
4044 defined
= (boolean
*) malloc (c
* sizeof (boolean
));
4045 included
= (boolean
*) malloc (c
* sizeof (boolean
));
4046 if (defined
== (boolean
*) NULL
|| included
== (boolean
*) NULL
)
4048 bfd_set_error (bfd_error_no_memory
);
4051 memset (defined
, 0, c
* sizeof (boolean
));
4052 memset (included
, 0, c
* sizeof (boolean
));
4054 symdefs
= bfd_ardata (abfd
)->symdefs
;
4067 symdefend
= symdef
+ c
;
4068 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4070 struct elf_link_hash_entry
*h
;
4072 struct bfd_link_hash_entry
*undefs_tail
;
4075 if (defined
[i
] || included
[i
])
4077 if (symdef
->file_offset
== last
)
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
)
4087 if (h
->root
.type
!= bfd_link_hash_undefined
)
4093 /* We need to include this archive member. */
4095 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4096 if (element
== (bfd
*) NULL
)
4099 if (! bfd_check_format (element
, bfd_object
))
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)
4107 bfd_set_error (bfd_error_bad_value
);
4110 element
->archive_pass
= 1;
4112 undefs_tail
= info
->hash
->undefs_tail
;
4114 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4117 if (! elf_link_add_object_symbols (element
, info
))
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
)
4130 /* Look backward to mark all symbols from this object file
4131 which we have already seen in this pass. */
4135 included
[mark
] = true;
4140 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4142 /* We mark subsequent symbols from this object file as we go
4143 on through the loop. */
4144 last
= symdef
->file_offset
;
4155 if (defined
!= (boolean
*) NULL
)
4157 if (included
!= (boolean
*) NULL
)
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. */
4166 static INLINE boolean
4167 elf_link_record_dynamic_symbol (info
, h
)
4168 struct bfd_link_info
*info
;
4169 struct elf_link_hash_entry
*h
;
4171 if (h
->dynindx
== -1)
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)
4185 /* Add symbols from an ELF object file to the linker hash table. */
4188 elf_link_add_object_symbols (abfd
, info
)
4190 struct bfd_link_info
*info
;
4192 boolean (*add_symbol_hook
) PARAMS ((bfd
*, struct bfd_link_info
*,
4193 const Elf_Internal_Sym
*,
4194 const char **, flagword
*,
4195 asection
**, bfd_vma
*));
4197 Elf_Internal_Shdr
*hdr
;
4200 Elf_External_Sym
*buf
= NULL
;
4201 struct elf_link_hash_entry
**sym_hash
;
4203 Elf_External_Dyn
*dynbuf
= NULL
;
4204 struct elf_link_hash_entry
*weaks
;
4205 Elf_External_Sym
*esym
;
4206 Elf_External_Sym
*esymend
;
4208 add_symbol_hook
= get_elf_backend_data (abfd
)->elf_add_symbol_hook
;
4209 collect
= get_elf_backend_data (abfd
)->collect
;
4211 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4212 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
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
4217 extsymcount
= symcount
- hdr
->sh_info
;
4219 buf
= (Elf_External_Sym
*) malloc (extsymcount
* sizeof (Elf_External_Sym
));
4220 if (buf
== NULL
&& extsymcount
!= 0)
4222 bfd_set_error (bfd_error_no_memory
);
4226 /* We store a pointer to the hash table entry for each external
4228 sym_hash
= ((struct elf_link_hash_entry
**)
4230 extsymcount
* sizeof (struct elf_link_hash_entry
*)));
4231 if (sym_hash
== NULL
)
4233 bfd_set_error (bfd_error_no_memory
);
4236 elf_sym_hashes (abfd
) = sym_hash
;
4238 if (elf_elfheader (abfd
)->e_type
!= ET_DYN
)
4244 unsigned long strindex
;
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
)
4254 bfd_set_error (bfd_error_invalid_operation
);
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");
4265 Elf_External_Dyn
*extdyn
;
4266 Elf_External_Dyn
*extdynend
;
4268 dynbuf
= (Elf_External_Dyn
*) malloc (s
->_raw_size
);
4271 bfd_set_error (bfd_error_no_memory
);
4275 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
,
4276 (file_ptr
) 0, s
->_raw_size
))
4280 extdynend
= extdyn
+ s
->_raw_size
/ sizeof (Elf_External_Dyn
);
4281 for (; extdyn
< extdynend
; extdyn
++)
4283 Elf_Internal_Dyn dyn
;
4285 elf_swap_dyn_in (abfd
, extdyn
, &dyn
);
4286 if (dyn
.d_tag
== DT_SONAME
)
4291 elfsec
= elf_section_from_bfd_section (abfd
, s
);
4294 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
4295 name
= elf_string_from_elf_section (abfd
, link
,
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
4315 abfd
->sections
= NULL
;
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
)
4323 if (! elf_link_create_dynamic_sections (abfd
, info
))
4325 elf_hash_table (info
)->dynobj
= abfd
;
4328 /* Add a DT_NEEDED entry for this dynamic object. */
4329 strindex
= bfd_add_to_strtab (abfd
,
4330 elf_hash_table (info
)->dynstr
,
4333 if (strindex
== (unsigned long) -1)
4335 if (! elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
4340 hdr
->sh_offset
+ hdr
->sh_info
* sizeof (Elf_External_Sym
),
4342 || (bfd_read ((PTR
) buf
, sizeof (Elf_External_Sym
), extsymcount
, abfd
)
4343 != extsymcount
* sizeof (Elf_External_Sym
)))
4348 esymend
= buf
+ extsymcount
;
4349 for (esym
= buf
; esym
< esymend
; esym
++, sym_hash
++)
4351 Elf_Internal_Sym sym
;
4357 struct elf_link_hash_entry
*h
= NULL
;
4360 elf_swap_symbol_in (abfd
, esym
, &sym
);
4362 flags
= BSF_NO_FLAGS
;
4364 value
= sym
.st_value
;
4367 bind
= ELF_ST_BIND (sym
.st_info
);
4368 if (bind
== STB_LOCAL
)
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
);
4376 else if (bind
== STB_GLOBAL
)
4378 else if (bind
== STB_WEAK
)
4382 /* Leave it up to the processor backend. */
4385 if (sym
.st_shndx
== SHN_UNDEF
)
4386 sec
= &bfd_und_section
;
4387 else if (sym
.st_shndx
> 0 && sym
.st_shndx
< SHN_LORESERVE
)
4389 sec
= section_from_elf_index (abfd
, sym
.st_shndx
);
4394 else if (sym
.st_shndx
== SHN_ABS
)
4395 sec
= &bfd_abs_section
;
4396 else if (sym
.st_shndx
== SHN_COMMON
)
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
;
4405 /* Leave it up to the processor backend. */
4408 name
= elf_string_from_elf_section (abfd
, hdr
->sh_link
, sym
.st_name
);
4409 if (name
== (const char *) NULL
)
4412 if (add_symbol_hook
)
4414 if (! (*add_symbol_hook
) (abfd
, info
, &sym
, &name
, &flags
, &sec
,
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
)
4424 /* Sanity check that all possibilities were handled. */
4425 if (flags
== BSF_NO_FLAGS
|| sec
== (asection
*) NULL
)
4427 bfd_set_error (bfd_error_bad_value
);
4431 if (sec
== &bfd_und_section
4432 || bfd_is_com_section (sec
))
4437 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
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);
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
)
4457 if (h
->root
.type
== bfd_link_hash_defined
)
4458 sec
= &bfd_und_section
;
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. */
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
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
;
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
)))
4491 && (flags
& BSF_WEAK
) != 0
4492 && ELF_ST_TYPE (sym
.st_info
) != STT_FUNC
4493 && (*sym_hash
)->weakdef
== NULL
)
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. */
4507 (*sym_hash
)->weakdef
= weaks
;
4511 if (info
->hash
->creator
->flavour
== bfd_target_elf_flavour
)
4517 /* Remember the symbol size, type and alignment. */
4518 if (sym
.st_size
!= 0)
4520 /* FIXME: We should probably somehow give a warning if
4521 the symbol size changes. */
4522 h
->size
= sym
.st_size
;
4524 if (sym
.st_shndx
== SHN_COMMON
4525 && sym
.st_value
> h
->align
)
4526 h
->align
= sym
.st_value
;
4527 if (ELF_ST_TYPE (sym
.st_info
) != STT_NOTYPE
)
4529 /* FIXME: We should probably somehow give a warning if
4530 the symbol type changes. */
4531 h
->type
= ELF_ST_TYPE (sym
.st_info
);
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
;
4545 new_flag
= ELF_LINK_HASH_REF_REGULAR
;
4547 new_flag
= ELF_LINK_HASH_DEF_REGULAR
;
4548 if ((old_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
4549 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0)
4555 new_flag
= ELF_LINK_HASH_REF_DYNAMIC
;
4557 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC
;
4558 if ((old_flags
& new_flag
) != 0)
4561 new_flag
= ELF_LINK_HASH_REF_DYNAMIC_MULTIPLE
;
4563 new_flag
= ELF_LINK_HASH_DEF_DYNAMIC_MULTIPLE
;
4568 if ((old_flags
& (ELF_LINK_HASH_DEF_REGULAR
4569 | ELF_LINK_HASH_REF_REGULAR
)) != 0)
4574 h
->elf_link_hash_flags
|= new_flag
;
4575 if (dynsym
&& h
->dynindx
== -1)
4577 if (! elf_link_record_dynamic_symbol (info
, h
))
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
4596 while (weaks
!= NULL
)
4598 struct elf_link_hash_entry
*hlook
;
4601 struct elf_link_hash_entry
**hpp
;
4602 struct elf_link_hash_entry
**hppend
;
4605 weaks
= hlook
->weakdef
;
4606 hlook
->weakdef
= NULL
;
4608 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
);
4609 slook
= hlook
->root
.u
.def
.section
;
4610 vlook
= hlook
->root
.u
.def
.value
;
4612 hpp
= elf_sym_hashes (abfd
);
4613 hppend
= hpp
+ extsymcount
;
4614 for (; hpp
< hppend
; hpp
++)
4616 struct elf_link_hash_entry
*h
;
4620 && h
->root
.type
== bfd_link_hash_defined
4621 && h
->root
.u
.def
.section
== slook
4622 && h
->root
.u
.def
.value
== vlook
)
4626 /* If the weak definition is in the list of dynamic
4627 symbols, make sure the real definition is put there
4629 if (hlook
->dynindx
!= -1
4630 && h
->dynindx
== -1)
4632 if (! elf_link_record_dynamic_symbol (info
, h
))
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. */
4662 elf_link_create_dynamic_sections (abfd
, info
)
4664 struct bfd_link_info
*info
;
4667 register asection
*s
;
4668 struct elf_link_hash_entry
*h
;
4669 struct elf_backend_data
*bed
;
4671 /* Note that we set the SEC_IN_MEMORY flag for all of these
4673 flags
= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
4675 s
= bfd_make_section (abfd
, ".interp");
4677 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
4680 s
= bfd_make_section (abfd
, ".dynamic");
4682 || ! bfd_set_section_flags (abfd
, s
, flags
)
4683 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
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. */
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
)))
4700 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4702 s
= bfd_make_section (abfd
, ".dynsym");
4704 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
4705 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
4708 /* The first .dynsym symbol is a dummy. */
4709 elf_hash_table (info
)->dynsymcount
= 1;
4711 s
= bfd_make_section (abfd
, ".dynstr");
4713 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
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
)
4721 s
= bfd_make_section (abfd
, ".hash");
4723 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
4724 || ! bfd_set_section_alignment (abfd
, s
, LOG_FILE_ALIGN
))
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
);
4734 /* Add an entry to the .dynamic table. */
4737 elf_add_dynamic_entry (info
, tag
, val
)
4738 struct bfd_link_info
*info
;
4742 Elf_Internal_Dyn dyn
;
4746 bfd_byte
*newcontents
;
4748 dynobj
= elf_hash_table (info
)->dynobj
;
4750 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
4751 BFD_ASSERT (s
!= NULL
);
4753 newsize
= s
->_raw_size
+ sizeof (Elf_External_Dyn
);
4754 if (s
->contents
== NULL
)
4755 newcontents
= (bfd_byte
*) malloc (newsize
);
4757 newcontents
= (bfd_byte
*) realloc (s
->contents
, newsize
);
4758 if (newcontents
== NULL
)
4760 bfd_set_error (bfd_error_no_memory
);
4765 dyn
.d_un
.d_val
= val
;
4766 elf_swap_dyn_out (dynobj
, &dyn
,
4767 (Elf_External_Dyn
*) (newcontents
+ s
->_raw_size
));
4769 s
->_raw_size
= newsize
;
4770 s
->contents
= newcontents
;
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. */
4780 NAME(bfd_elf
,record_link_assignment
) (output_bfd
, info
, name
)
4782 struct bfd_link_info
*info
;
4785 struct elf_link_hash_entry
*h
;
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);
4795 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
4797 if ((h
->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
4798 | ELF_LINK_HASH_REF_DYNAMIC
)) != 0
4799 && h
->dynindx
== -1)
4801 if (! elf_link_record_dynamic_symbol (info
, h
))
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)
4810 if (! elf_link_record_dynamic_symbol (info
, h
->weakdef
))
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. */
4824 static const size_t elf_buckets
[] =
4826 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 0
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. */
4835 NAME(bfd_elf
,size_dynamic_sections
) (output_bfd
, info
)
4837 struct bfd_link_info
*info
;
4842 Elf_Internal_Sym isym
;
4845 struct elf_backend_data
*bed
;
4847 dynobj
= elf_hash_table (info
)->dynobj
;
4848 dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4850 /* If there were no dynamic objects in the link, there is nothing to
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)
4867 bfd_set_error (bfd_error_no_memory
);
4871 /* The first entry in .dynsym is a dummy symbol. */
4878 elf_swap_symbol_out (output_bfd
, &isym
,
4879 (Elf_External_Sym
*) s
->contents
);
4881 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4883 bucketcount
= elf_buckets
[i
];
4884 if (dynsymcount
< elf_buckets
[i
+ 1])
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
)
4894 bfd_set_error (bfd_error_no_memory
);
4897 memset (s
->contents
, 0, s
->_raw_size
);
4899 put_word (output_bfd
, bucketcount
, s
->contents
);
4900 put_word (output_bfd
, dynsymcount
, s
->contents
+ (ARCH_SIZE
/ 8));
4902 elf_hash_table (info
)->bucketcount
= bucketcount
;
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
;
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
,
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
)
4920 if (! elf_add_dynamic_entry (info
, DT_INIT
, 0))
4923 if (bfd_get_section_by_name (output_bfd
, ".fini") != NULL
)
4925 if (! elf_add_dynamic_entry (info
, DT_FINI
, 0))
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
)))
4937 /* The backend must work out the sizes of all the other dynamic
4939 bed
= get_elf_backend_data (output_bfd
);
4940 if (! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
4943 return elf_add_dynamic_entry (info
, DT_NULL
, 0);
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
4951 elf_adjust_dynamic_symbol (h
, data
)
4952 struct elf_link_hash_entry
*h
;
4955 struct bfd_link_info
*info
= (struct bfd_link_info
*) data
;
4957 struct elf_backend_data
*bed
;
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)
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)
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
;
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.
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
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;
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. */
5007 if (h
->weakdef
!= NULL
)
5009 struct elf_link_hash_entry
*weakdef
;
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)
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. */
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
))
5033 dynobj
= elf_hash_table (info
)->dynobj
;
5034 bed
= get_elf_backend_data (dynobj
);
5035 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (info
, h
))
5037 /* FIXME: No way to return error. */
5044 /* Final phase of ELF linker. */
5046 /* A structure we use to avoid passing large numbers of arguments. */
5048 struct elf_final_link_info
5050 /* General link information. */
5051 struct bfd_link_info
*info
;
5054 /* Symbol string table. */
5055 struct strtab
*symstrtab
;
5056 /* .dynsym section. */
5057 asection
*dynsym_sec
;
5058 /* .hash section. */
5060 /* Buffer large enough to hold contents of any section. */
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
5068 Elf_External_Sym
*external_syms
;
5069 /* Buffer large enough to hold internal local symbols of any input
5071 Elf_Internal_Sym
*internal_syms
;
5072 /* Array large enough to hold a symbol index for each local symbol
5073 of any input BFD. */
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. */
5086 static boolean elf_link_output_sym
5087 PARAMS ((struct elf_final_link_info
*, const char *, Elf_Internal_Sym
*));
5088 static boolean elf_link_flush_output_syms
5089 PARAMS ((struct elf_final_link_info
*));
5090 static boolean elf_link_output_extsym
5091 PARAMS ((struct elf_link_hash_entry
*, PTR
));
5092 static boolean elf_link_input_bfd
5093 PARAMS ((struct elf_final_link_info
*, bfd
*));
5094 static boolean elf_reloc_link_order
5095 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
5096 struct bfd_link_order
*));
5098 /* Do the final step of an ELF link. */
5101 elf_bfd_final_link (abfd
, info
)
5103 struct bfd_link_info
*info
;
5106 struct elf_final_link_info finfo
;
5107 register asection
*o
;
5108 register struct bfd_link_order
*p
;
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
;
5115 Elf_Internal_Sym elfsym
;
5117 Elf_Internal_Shdr
*symtab_hdr
;
5118 Elf_Internal_Shdr
*symstrtab_hdr
;
5120 dynobj
= elf_hash_table (info
)->dynobj
;
5123 finfo
.output_bfd
= abfd
;
5124 finfo
.symstrtab
= bfd_new_strtab (abfd
);
5125 if (finfo
.symstrtab
== NULL
)
5129 finfo
.dynsym_sec
= NULL
;
5130 finfo
.hash_sec
= NULL
;
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
)
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;
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;
5157 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
5161 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5163 if (p
->type
== bfd_section_reloc_link_order
5164 || p
->type
== bfd_symbol_reloc_link_order
)
5166 else if (p
->type
== bfd_indirect_link_order
)
5170 sec
= p
->u
.indirect
.section
;
5172 if (info
->relocateable
)
5173 o
->reloc_count
+= sec
->reloc_count
;
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
;
5180 /* We are interested in just local symbols, not all
5182 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
5183 && (elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
5185 max_sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
5187 if ((sec
->flags
& SEC_RELOC
) != 0)
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
;
5200 if (o
->reloc_count
> 0)
5201 o
->flags
|= SEC_RELOC
;
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
;
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
))
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
)
5223 if ((o
->flags
& SEC_RELOC
) != 0)
5225 Elf_Internal_Shdr
*rel_hdr
;
5226 register struct elf_link_hash_entry
**p
, **pend
;
5228 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
5230 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* o
->reloc_count
;
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)
5237 bfd_set_error (bfd_error_no_memory
);
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)
5246 bfd_set_error (bfd_error_no_memory
);
5249 elf_section_data (o
)->rel_hashes
= p
;
5250 pend
= p
+ o
->reloc_count
;
5251 for (; p
< pend
; p
++)
5254 /* Use the reloc_count field as an index when outputting the
5260 assign_file_positions_for_relocs (abfd
);
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). */
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? */
5280 off
= elf_tdata (abfd
)->next_file_pos
;
5281 off
= assign_file_position_for_section (symtab_hdr
, off
, true);
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. */
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;
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
)
5297 bfd_set_error (bfd_error_no_memory
);
5301 /* Start writing out the symbol table. The first symbol is always a
5303 elfsym
.st_value
= 0;
5306 elfsym
.st_other
= 0;
5307 elfsym
.st_shndx
= SHN_UNDEF
;
5308 if (! elf_link_output_sym (&finfo
, (const char *) NULL
, &elfsym
))
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;
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
))
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;
5333 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5334 elfsym
.st_other
= 0;
5335 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
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
;
5341 if (! elf_link_output_sym (&finfo
, (const char *) NULL
, &elfsym
))
5345 /* Allocate some memory to hold information read in from the input
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))
5366 bfd_set_error (bfd_error_no_memory
);
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.
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. */
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
)
5394 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
5396 if (p
->type
== bfd_indirect_link_order
5397 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
5398 == bfd_target_elf_flavour
))
5400 sub
= p
->u
.indirect
.section
->owner
;
5401 if (! sub
->output_has_begun
)
5403 if (! elf_link_input_bfd (&finfo
, sub
))
5405 sub
->output_has_begun
= true;
5408 else if (p
->type
== bfd_section_reloc_link_order
5409 || p
->type
== bfd_symbol_reloc_link_order
)
5411 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
5416 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
5422 /* That wrote out all the local symbols. Finish up the symbol table
5423 with the global symbols. */
5425 /* The sh_info field records the index of the first non local
5427 symtab_hdr
->sh_info
= abfd
->symcount
;
5429 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
= 1;
5431 /* We get the global symbols from the hash table. */
5432 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
5435 /* Flush all symbols to the file. */
5436 if (! elf_link_flush_output_syms (&finfo
))
5439 /* Now we know the size of the symtab section. */
5440 off
+= symtab_hdr
->sh_size
;
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
;
5456 off
= assign_file_position_for_section (symstrtab_hdr
, off
, true);
5457 elf_tdata (abfd
)->next_file_pos
= off
;
5459 /* Adjust the relocs to have the correct symbol indices. */
5460 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5462 struct elf_link_hash_entry
**rel_hash
;
5463 Elf_Internal_Shdr
*rel_hdr
;
5465 if ((o
->flags
& SEC_RELOC
) == 0)
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
++)
5472 if (*rel_hash
== NULL
)
5475 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5477 if (rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
5479 Elf_External_Rel
*erel
;
5480 Elf_Internal_Rel irel
;
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
);
5490 Elf_External_Rela
*erela
;
5491 Elf_Internal_Rela irela
;
5493 BFD_ASSERT (rel_hdr
->sh_entsize
5494 == sizeof (Elf_External_Rela
));
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
);
5504 /* Set the reloc_count field to 0 to prevent write_relocs from
5505 trying to swap the relocs out itself. */
5509 /* If we are linking against a dynamic object, finish up the dynamic
5510 linking information. */
5513 Elf_External_Dyn
*dyncon
, *dynconend
;
5514 struct elf_backend_data
*bed
;
5516 /* Fix up .dynamic entries. */
5517 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
5518 BFD_ASSERT (o
!= NULL
);
5520 dyncon
= (Elf_External_Dyn
*) o
->contents
;
5521 dynconend
= (Elf_External_Dyn
*) (o
->contents
+ o
->_raw_size
);
5522 for (; dyncon
< dynconend
; dyncon
++)
5524 Elf_Internal_Dyn dyn
;
5528 elf_swap_dyn_in (dynobj
, dyncon
, &dyn
);
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
);
5560 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
5565 for (i
= 1; i
< elf_elfheader (abfd
)->e_shnum
; i
++)
5567 Elf_Internal_Shdr
*hdr
;
5569 hdr
= elf_elfsections (abfd
)[i
];
5570 if (hdr
->sh_type
== type
5571 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
5573 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
5574 dyn
.d_un
.d_val
+= hdr
->sh_size
;
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
;
5583 elf_swap_dyn_out (dynobj
, &dyn
, dyncon
);
5588 bed
= get_elf_backend_data (abfd
);
5589 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
5592 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
5594 Elf_Internal_Shdr
*hdr
;
5598 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0)
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
,
5606 hdr
= &elf_section_data (o
->output_section
)->this_hdr
;
5607 switch (hdr
->sh_type
)
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
);
5616 s
= finfo
.dynsym_sec
->output_section
;
5617 hdr
->sh_link
= elf_section_data (s
)->this_idx
;
5618 hdr
->sh_entsize
= ARCH_SIZE
/ 8;
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
);
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
)
5635 hdr
->sh_entsize
= sizeof (Elf_External_Rel
);
5640 hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
5642 s
= bfd_get_section_by_name (abfd
, name
);
5644 hdr
->sh_info
= elf_section_data (s
)->this_idx
;
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
)
5668 if ((o
->flags
& SEC_RELOC
) != 0
5669 && elf_section_data (o
)->rel_hashes
!= NULL
)
5670 free (elf_section_data (o
)->rel_hashes
);
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
)
5694 if ((o
->flags
& SEC_RELOC
) != 0
5695 && elf_section_data (o
)->rel_hashes
!= NULL
)
5696 free (elf_section_data (o
)->rel_hashes
);
5702 /* Add a symbol to the output symbol table. */
5705 elf_link_output_sym (finfo
, name
, elfsym
)
5706 struct elf_final_link_info
*finfo
;
5708 Elf_Internal_Sym
*elfsym
;
5710 if (name
== (const char *) NULL
|| *name
== '\0')
5711 elfsym
->st_name
= 0;
5714 elfsym
->st_name
= bfd_add_to_strtab (finfo
->output_bfd
,
5715 finfo
->symstrtab
, name
);
5716 if (elfsym
->st_name
== (unsigned long) -1)
5720 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5722 if (! elf_link_flush_output_syms (finfo
))
5726 elf_swap_symbol_out (finfo
->output_bfd
, elfsym
,
5727 finfo
->symbuf
+ finfo
->symbuf_count
);
5728 ++finfo
->symbuf_count
;
5730 ++finfo
->output_bfd
->symcount
;
5735 /* Flush the output symbols to the file. */
5738 elf_link_flush_output_syms (finfo
)
5739 struct elf_final_link_info
*finfo
;
5741 Elf_Internal_Shdr
*symtab
;
5743 symtab
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5745 if (bfd_seek (finfo
->output_bfd
, symtab
->sh_offset
+ symtab
->sh_size
,
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
)))
5752 symtab
->sh_size
+= finfo
->symbuf_count
* sizeof (Elf_External_Sym
);
5754 finfo
->symbuf_count
= 0;
5759 /* Add an external symbol to the symbol table. This is called from
5760 the hash table traversal routine. */
5763 elf_link_output_extsym (h
, data
)
5764 struct elf_link_hash_entry
*h
;
5767 struct elf_final_link_info
*finfo
= (struct elf_final_link_info
*) data
;
5769 Elf_Internal_Sym sym
;
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
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)
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
))
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)
5795 sym
.st_size
= h
->size
;
5796 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
5799 switch (h
->root
.type
)
5802 case bfd_link_hash_new
:
5806 case bfd_link_hash_undefined
:
5807 sym
.st_shndx
= SHN_UNDEF
;
5810 case bfd_link_hash_weak
:
5811 sym
.st_shndx
= SHN_UNDEF
;
5812 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
5815 case bfd_link_hash_defined
:
5819 sec
= h
->root
.u
.def
.section
;
5820 if (sec
->output_section
!= NULL
)
5822 sym
.st_shndx
= elf_section_from_bfd_section (finfo
->output_bfd
,
5823 sec
->output_section
);
5824 if (sym
.st_shndx
== (unsigned short) -1)
5826 /* FIXME: No way to handle errors. */
5830 /* ELF symbols in relocateable files are section relative,
5831 but in nonrelocateable files they are virtual
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
;
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
;
5846 case bfd_link_hash_common
:
5847 sym
.st_shndx
= SHN_COMMON
;
5851 sym
.st_value
= h
->align
;
5854 case bfd_link_hash_indirect
:
5855 case bfd_link_hash_warning
:
5856 /* I have no idea how these should be handled. */
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)
5865 struct elf_backend_data
*bed
;
5868 bfd_byte
*bucketpos
;
5871 sym
.st_name
= h
->dynstr_index
;
5873 /* Give the processor backend a chance to tweak the symbol
5874 value, and also to finish up anything that needs to be done
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
)))
5880 /* FIXME: No way to return error. */
5884 elf_swap_symbol_out (finfo
->output_bfd
, &sym
,
5885 ((Elf_External_Sym
*) finfo
->dynsym_sec
->contents
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)));
5899 /* If we're stripping it, then it was just a dynamic symbol, and
5900 there's nothing else to do. */
5904 h
->indx
= finfo
->output_bfd
->symcount
;
5906 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
))
5908 /* FIXME: No way to return error. */
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. */
5921 elf_link_input_bfd (finfo
, input_bfd
)
5922 struct elf_final_link_info
*finfo
;
5925 boolean (*relocate_section
) PARAMS ((bfd
*, struct bfd_link_info
*,
5926 bfd
*, asection
*, bfd_byte
*,
5927 Elf_Internal_Rela
*,
5931 Elf_Internal_Shdr
*symtab_hdr
;
5932 Elf_External_Sym
*esym
;
5933 Elf_External_Sym
*esymend
;
5934 Elf_Internal_Sym
*isym
;
5936 asection
**ppsection
;
5939 output_bfd
= finfo
->output_bfd
;
5941 get_elf_backend_data (output_bfd
)->elf_backend_relocate_section
;
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
5946 if (elf_elfheader (input_bfd
)->e_type
== ET_DYN
)
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
)))
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
++)
5970 elf_swap_symbol_in (input_bfd
, esym
, isym
);
5973 if (isym
->st_shndx
== SHN_UNDEF
)
5974 isec
= &bfd_und_section
;
5975 else if (isym
->st_shndx
> 0 && isym
->st_shndx
< SHN_LORESERVE
)
5977 isec
= section_from_elf_index (input_bfd
, isym
->st_shndx
);
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
;
5993 /* Don't output the first, undefined, symbol. */
5994 if (esym
== finfo
->external_syms
)
5997 /* If we are stripping all symbols, we don't want to output this
5999 if (finfo
->info
->strip
== strip_all
)
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
)
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
6012 if (finfo
->info
->discard
== discard_all
)
6015 /* Get the name of the symbol. */
6016 name
= elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
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)
6025 || (finfo
->info
->discard
== discard_l
6026 && strncmp (name
, finfo
->info
->lprefix
,
6027 finfo
->info
->lprefix_len
) == 0))
6030 /* If we get here, we are going to output this symbol. */
6032 /* Adjust the section index for the output file. */
6033 isym
->st_shndx
= elf_section_from_bfd_section (output_bfd
,
6034 isec
->output_section
);
6035 if (isym
->st_shndx
== (unsigned short) -1)
6038 *pindex
= output_bfd
->symcount
;
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
;
6052 if (! elf_link_output_sym (finfo
, name
, isym
))
6055 /* Restore the old value for reloc handling. */
6056 isym
->st_value
= oldval
;
6059 /* Relocate the contents of each section. */
6060 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6062 Elf_Internal_Shdr
*input_rel_hdr
;
6064 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0)
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
))
6072 if ((o
->flags
& SEC_RELOC
) != 0)
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
)
6081 /* Swap in the relocs. For convenience, we always produce
6082 an Elf_Internal_Rela array; if the relocs are Rel, we set
6084 if (input_rel_hdr
->sh_entsize
== sizeof (Elf_External_Rel
))
6086 Elf_External_Rel
*erel
;
6087 Elf_External_Rel
*erelend
;
6088 Elf_Internal_Rela
*irela
;
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
++)
6095 Elf_Internal_Rel irel
;
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;
6105 Elf_External_Rela
*erela
;
6106 Elf_External_Rela
*erelaend
;
6107 Elf_Internal_Rela
*irela
;
6109 BFD_ASSERT (input_rel_hdr
->sh_entsize
6110 == sizeof (Elf_External_Rela
));
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
);
6119 /* Relocate the section by invoking a back end routine.
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.
6126 The back end routine does not have to worry about setting
6127 the reloc address or the reloc symbol index.
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).
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. */
6139 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6142 finfo
->internal_relocs
,
6143 finfo
->internal_syms
,
6147 if (finfo
->info
->relocateable
)
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
;
6154 /* Adjust the reloc addresses and symbol indices. */
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
++)
6163 Elf_Internal_Sym
*isym
;
6166 irela
->r_offset
+= o
->output_offset
;
6168 r_symndx
= ELF_R_SYM (irela
->r_info
);
6173 if (r_symndx
>= symtab_hdr
->sh_info
)
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
];
6187 /* Setting the index to -2 tells
6188 elf_link_output_extsym that this symbol is
6190 BFD_ASSERT ((*rel_hash
)->indx
< 0);
6191 (*rel_hash
)->indx
= -2;
6196 /* This is a reloc against a local symbol. */
6199 isym
= finfo
->internal_syms
+ r_symndx
;
6200 sec
= finfo
->sections
[r_symndx
];
6201 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6203 /* I suppose the backend ought to fill in the
6204 section of any STT_SECTION symbol against a
6205 processor specific section. */
6207 || sec
== &bfd_und_section
6208 || sec
== &bfd_com_section
)
6210 bfd_set_error (bfd_error_bad_value
);
6213 else if (sec
== &bfd_abs_section
)
6217 r_symndx
= sec
->output_section
->target_index
;
6224 if (finfo
->indices
[r_symndx
] == -1)
6230 if (finfo
->info
->strip
== strip_all
)
6232 /* You can't do ld -r -s. */
6233 bfd_set_error (bfd_error_invalid_operation
);
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
,
6247 osec
= sec
->output_section
;
6249 elf_section_from_bfd_section (output_bfd
,
6251 if (isym
->st_shndx
== (unsigned short) -1)
6254 isym
->st_value
+= sec
->output_offset
;
6255 if (! finfo
->info
->relocateable
)
6256 isym
->st_value
+= osec
->vma
;
6258 finfo
->indices
[r_symndx
] = output_bfd
->symcount
;
6260 if (! elf_link_output_sym (finfo
, name
, isym
))
6264 r_symndx
= finfo
->indices
[r_symndx
];
6267 irela
->r_info
= ELF_R_INFO (r_symndx
,
6268 ELF_R_TYPE (irela
->r_info
));
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
))
6279 Elf_External_Rel
*erel
;
6281 erel
= ((Elf_External_Rel
*) output_rel_hdr
->contents
6282 + o
->output_section
->reloc_count
);
6283 for (; irela
< irelaend
; irela
++, erel
++)
6285 Elf_Internal_Rel irel
;
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
);
6295 Elf_External_Rela
*erela
;
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
);
6305 o
->output_section
->reloc_count
+= o
->reloc_count
;
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
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
6327 elf_reloc_link_order (output_bfd
, info
, output_section
, link_order
)
6329 struct bfd_link_info
*info
;
6330 asection
*output_section
;
6331 struct bfd_link_order
*link_order
;
6333 const reloc_howto_type
*howto
;
6336 struct elf_link_hash_entry
**rel_hash_ptr
;
6337 Elf_Internal_Shdr
*rel_hdr
;
6339 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
6342 bfd_set_error (bfd_error_bad_value
);
6346 /* If this is an inplace reloc, we must write the addend into the
6348 if (howto
->partial_inplace
6349 && link_order
->u
.reloc
.p
->addend
!= 0)
6352 bfd_reloc_status_type rstat
;
6356 size
= bfd_get_reloc_size (howto
);
6357 buf
= (bfd_byte
*) bfd_zmalloc (size
);
6358 if (buf
== (bfd_byte
*) NULL
)
6360 bfd_set_error (bfd_error_no_memory
);
6363 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
6364 link_order
->u
.reloc
.p
->addend
, buf
);
6370 case bfd_reloc_outofrange
:
6372 case bfd_reloc_overflow
:
6373 if (! ((*info
->callbacks
->reloc_overflow
)
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)))
6387 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
6388 (file_ptr
) link_order
->offset
, size
);
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
)
6399 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
6402 *rel_hash_ptr
= NULL
;
6406 struct elf_link_hash_entry
*h
;
6408 h
= elf_link_hash_lookup (elf_hash_table (info
),
6409 link_order
->u
.reloc
.p
->u
.name
,
6410 false, false, true);
6413 /* Setting the index to -2 tells elf_link_output_extsym that
6414 this symbol is used by a reloc. */
6421 if (! ((*info
->callbacks
->unattached_reloc
)
6422 (info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
6423 (asection
*) NULL
, (bfd_vma
) 0)))
6429 /* The address of a reloc is relative to the section in a
6430 relocateable file, and is a virtual address in an executable
6432 offset
= link_order
->offset
;
6433 if (! info
->relocateable
)
6434 offset
+= output_section
->vma
;
6436 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
6438 if (rel_hdr
->sh_type
== SHT_REL
)
6440 Elf_Internal_Rel irel
;
6441 Elf_External_Rel
*erel
;
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
);
6451 Elf_Internal_Rela irela
;
6452 Elf_External_Rela
*erela
;
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
);
6462 ++output_section
->reloc_count
;