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;
499 /* ELF .o/exec file reading */
501 /* Create a new bfd section from an ELF section header. */
504 bfd_section_from_shdr (abfd, shindex)
506 unsigned int shindex;
508 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
509 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
512 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
514 switch (hdr->sh_type)
517 /* Inactive section. Throw it away. */
520 case SHT_PROGBITS: /* Normal section with contents. */
521 case SHT_DYNAMIC: /* Dynamic linking information. */
522 case SHT_NOBITS: /* .bss section. */
523 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
525 case SHT_SYMTAB: /* A symbol table */
526 if (elf_onesymtab (abfd) == shindex)
529 BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym));
530 BFD_ASSERT (elf_onesymtab (abfd) == 0);
531 elf_onesymtab (abfd) = shindex;
532 elf_tdata (abfd)->symtab_hdr = *hdr;
533 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_hdr;
534 abfd->flags |= HAS_SYMS;
537 case SHT_DYNSYM: /* A dynamic symbol table */
538 if (elf_dynsymtab (abfd) == shindex)
541 BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym));
542 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
543 elf_dynsymtab (abfd) = shindex;
544 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
545 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->dynsymtab_hdr;
546 abfd->flags |= HAS_SYMS;
549 case SHT_STRTAB: /* A string table */
550 if (hdr->rawdata != NULL)
552 if (ehdr->e_shstrndx == shindex)
554 elf_tdata (abfd)->shstrtab_hdr = *hdr;
555 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
556 hdr->rawdata = (PTR) & elf_tdata (abfd)->shstrtab_hdr;
562 for (i = 1; i < ehdr->e_shnum; i++)
564 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
565 if (hdr2->sh_link == shindex)
567 if (! bfd_section_from_shdr (abfd, i))
569 if (elf_onesymtab (abfd) == i)
571 elf_tdata (abfd)->strtab_hdr = *hdr;
572 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
575 if (elf_dynsymtab (abfd) == i)
577 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
578 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->dynstrtab_hdr;
581 #if 0 /* Not handling other string tables specially right now. */
582 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
583 /* We have a strtab for some random other section. */
584 newsect = (asection *) hdr2->rawdata;
587 hdr->rawdata = (PTR) newsect;
588 hdr2 = &elf_section_data (newsect)->str_hdr;
590 elf_elfsections (abfd)[shindex] = hdr2;
596 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
600 /* *These* do a lot of work -- but build no sections! */
602 asection *target_sect;
603 Elf_Internal_Shdr *hdr2;
604 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
606 /* Get the symbol table. */
607 if (! bfd_section_from_shdr (abfd, hdr->sh_link))
610 /* If this reloc section does not use the main symbol table,
611 or if it is in the process image, we don't treat it as a
612 reloc section. BFD can't adequately represent such a
613 section, so at least for now, we don't try. We just
614 present it as a normal section. */
615 if ((hdr->sh_flags & SHF_ALLOC) != 0
616 || hdr->sh_link != elf_onesymtab (abfd))
617 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
619 /* Don't allow REL relocations on a machine that uses RELA and
621 /* @@ Actually, the generic ABI does suggest that both might be
622 used in one file. But the four ABI Processor Supplements I
623 have access to right now all specify that only one is used on
624 each of those architectures. It's conceivable that, e.g., a
625 bunch of absolute 32-bit relocs might be more compact in REL
626 form even on a RELA machine... */
627 BFD_ASSERT (use_rela_p
628 ? (hdr->sh_type == SHT_RELA
629 && hdr->sh_entsize == sizeof (Elf_External_Rela))
630 : (hdr->sh_type == SHT_REL
631 && hdr->sh_entsize == sizeof (Elf_External_Rel)));
633 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
635 target_sect = section_from_elf_index (abfd, hdr->sh_info);
636 if (target_sect == NULL
637 || elf_section_data (target_sect) == NULL)
640 hdr2 = &elf_section_data (target_sect)->rel_hdr;
642 elf_elfsections (abfd)[shindex] = hdr2;
643 target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
644 target_sect->flags |= SEC_RELOC;
645 target_sect->relocation = NULL;
646 target_sect->rel_filepos = hdr->sh_offset;
647 abfd->flags |= HAS_RELOC;
654 fprintf (stderr, "Dynamic Linking sections not yet supported.\n");
661 fprintf (stderr, "Note Sections not yet supported.\n");
668 fprintf (stderr, "SHLIB Sections not supported (and non conforming.)\n");
673 /* Check for any processor-specific section types. */
675 struct elf_backend_data *bed = get_elf_backend_data (abfd);
677 if (bed->elf_backend_section_from_shdr)
678 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
687 elf_new_section_hook (abfd, sec)
692 struct bfd_elf_section_data *sdata;
694 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
697 bfd_set_error (bfd_error_no_memory);
700 sec->used_by_bfd = (PTR) sdata;
701 memset (sdata, 0, sizeof (*sdata));
705 /* Create a new bfd section from an ELF program header.
707 Since program segments have no names, we generate a synthetic name
708 of the form segment<NUM>, where NUM is generally the index in the
709 program header table. For segments that are split (see below) we
710 generate the names segment<NUM>a and segment<NUM>b.
712 Note that some program segments may have a file size that is different than
713 (less than) the memory size. All this means is that at execution the
714 system must allocate the amount of memory specified by the memory size,
715 but only initialize it with the first "file size" bytes read from the
716 file. This would occur for example, with program segments consisting
717 of combined data+bss.
719 To handle the above situation, this routine generates TWO bfd sections
720 for the single program segment. The first has the length specified by
721 the file size of the segment, and the second has the length specified
722 by the difference between the two sizes. In effect, the segment is split
723 into it's initialized and uninitialized parts.
728 bfd_section_from_phdr (abfd, hdr, index)
730 Elf_Internal_Phdr *hdr;
738 split = ((hdr->p_memsz > 0) &&
739 (hdr->p_filesz > 0) &&
740 (hdr->p_memsz > hdr->p_filesz));
741 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
742 name = bfd_alloc (abfd, strlen (namebuf) + 1);
745 bfd_set_error (bfd_error_no_memory);
748 strcpy (name, namebuf);
749 newsect = bfd_make_section (abfd, name);
752 newsect->vma = hdr->p_vaddr;
753 newsect->_raw_size = hdr->p_filesz;
754 newsect->filepos = hdr->p_offset;
755 newsect->flags |= SEC_HAS_CONTENTS;
756 if (hdr->p_type == PT_LOAD)
758 newsect->flags |= SEC_ALLOC;
759 newsect->flags |= SEC_LOAD;
760 if (hdr->p_flags & PF_X)
762 /* FIXME: all we known is that it has execute PERMISSION,
764 newsect->flags |= SEC_CODE;
767 if (!(hdr->p_flags & PF_W))
769 newsect->flags |= SEC_READONLY;
774 sprintf (namebuf, "segment%db", index);
775 name = bfd_alloc (abfd, strlen (namebuf) + 1);
778 bfd_set_error (bfd_error_no_memory);
781 strcpy (name, namebuf);
782 newsect = bfd_make_section (abfd, name);
785 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
786 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
787 if (hdr->p_type == PT_LOAD)
789 newsect->flags |= SEC_ALLOC;
790 if (hdr->p_flags & PF_X)
791 newsect->flags |= SEC_CODE;
793 if (!(hdr->p_flags & PF_W))
794 newsect->flags |= SEC_READONLY;
800 /* Begin processing a given object.
802 First we validate the file by reading in the ELF header and checking
805 static INLINE boolean
807 Elf_External_Ehdr *x_ehdrp;
809 return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
810 && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
811 && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
812 && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
815 /* Check to see if the file associated with ABFD matches the target vector
818 Note that we may be called several times with the same ABFD, but different
819 target vectors, most of which will not match. We have to avoid leaving
820 any side effects in ABFD, or any data it points to (like tdata), if the
821 file does not match the target vector. */
827 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
828 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
829 Elf_External_Shdr x_shdr; /* Section header table entry, external form */
830 Elf_Internal_Shdr *i_shdrp = NULL; /* Section header table, internal form */
831 unsigned int shindex;
832 char *shstrtab; /* Internal copy of section header stringtab */
833 struct elf_backend_data *ebd;
834 struct elf_obj_tdata *preserved_tdata = elf_tdata (abfd);
835 struct elf_obj_tdata *new_tdata = NULL;
837 /* Read in the ELF header in external format. */
839 if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
841 if (bfd_get_error () != bfd_error_system_call)
842 goto got_wrong_format_error;
847 /* Now check to see if we have a valid ELF file, and one that BFD can
848 make use of. The magic number must match, the address size ('class')
849 and byte-swapping must match our XVEC entry, and it must have a
850 section header table (FIXME: See comments re sections at top of this
853 if ((elf_file_p (&x_ehdr) == false) ||
854 (x_ehdr.e_ident[EI_VERSION] != EV_CURRENT) ||
855 (x_ehdr.e_ident[EI_CLASS] != ELFCLASS))
856 goto got_wrong_format_error;
858 /* Check that file's byte order matches xvec's */
859 switch (x_ehdr.e_ident[EI_DATA])
861 case ELFDATA2MSB: /* Big-endian */
862 if (!abfd->xvec->header_byteorder_big_p)
863 goto got_wrong_format_error;
865 case ELFDATA2LSB: /* Little-endian */
866 if (abfd->xvec->header_byteorder_big_p)
867 goto got_wrong_format_error;
869 case ELFDATANONE: /* No data encoding specified */
870 default: /* Unknown data encoding specified */
871 goto got_wrong_format_error;
874 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
875 the tdata pointer in the bfd. */
877 new_tdata = ((struct elf_obj_tdata *)
878 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)));
879 if (new_tdata == NULL)
880 goto got_no_memory_error;
881 elf_tdata (abfd) = new_tdata;
883 /* Now that we know the byte order, swap in the rest of the header */
884 i_ehdrp = elf_elfheader (abfd);
885 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
887 elf_debug_file (i_ehdrp);
890 /* If there is no section header table, we're hosed. */
891 if (i_ehdrp->e_shoff == 0)
892 goto got_wrong_format_error;
894 /* As a simple sanity check, verify that the what BFD thinks is the
895 size of each section header table entry actually matches the size
896 recorded in the file. */
897 if (i_ehdrp->e_shentsize != sizeof (x_shdr))
898 goto got_wrong_format_error;
900 ebd = get_elf_backend_data (abfd);
902 /* Check that the ELF e_machine field matches what this particular
903 BFD format expects. */
904 if (ebd->elf_machine_code != i_ehdrp->e_machine)
906 bfd_target **target_ptr;
908 if (ebd->elf_machine_code != EM_NONE)
909 goto got_wrong_format_error;
911 /* This is the generic ELF target. Let it match any ELF target
912 for which we do not have a specific backend. */
913 for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
915 struct elf_backend_data *back;
917 if ((*target_ptr)->flavour != bfd_target_elf_flavour)
919 back = (struct elf_backend_data *) (*target_ptr)->backend_data;
920 if (back->elf_machine_code == i_ehdrp->e_machine)
922 /* target_ptr is an ELF backend which matches this
923 object file, so reject the generic ELF target. */
924 goto got_wrong_format_error;
929 /* Set the flags and architecture before calling the backend so that
930 it can override them. */
931 if (i_ehdrp->e_type == ET_EXEC)
932 abfd->flags |= EXEC_P;
933 else if (i_ehdrp->e_type == ET_DYN)
934 abfd->flags |= DYNAMIC;
936 if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
939 /* Remember the entry point specified in the ELF file header. */
940 bfd_get_start_address (abfd) = i_ehdrp->e_entry;
942 /* Let the backend double check the format and override global
944 if (ebd->elf_backend_object_p)
946 if ((*ebd->elf_backend_object_p) (abfd) == false)
947 goto got_wrong_format_error;
950 /* Allocate space for a copy of the section header table in
951 internal form, seek to the section header table in the file,
952 read it in, and convert it to internal form. */
953 i_shdrp = ((Elf_Internal_Shdr *)
954 bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum));
955 elf_elfsections (abfd) = ((Elf_Internal_Shdr **)
957 sizeof (i_shdrp) * i_ehdrp->e_shnum));
958 if (!i_shdrp || !elf_elfsections (abfd))
959 goto got_no_memory_error;
960 if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0)
962 for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++)
964 if (bfd_read ((PTR) & x_shdr, sizeof x_shdr, 1, abfd) != sizeof (x_shdr))
966 elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
967 elf_elfsections (abfd)[shindex] = i_shdrp + shindex;
969 /* If this is a .dynamic section, mark the object file as being
970 dynamically linked. */
971 if (i_shdrp[shindex].sh_type == SHT_DYNAMIC)
972 abfd->flags |= DYNAMIC;
974 if (i_ehdrp->e_shstrndx)
976 if (! bfd_section_from_shdr (abfd, i_ehdrp->e_shstrndx))
980 /* Read in the string table containing the names of the sections. We
981 will need the base pointer to this table later. */
982 /* We read this inline now, so that we don't have to go through
983 bfd_section_from_shdr with it (since this particular strtab is
984 used to find all of the ELF section names.) */
986 shstrtab = elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
990 /* Once all of the section headers have been read and converted, we
991 can start processing them. Note that the first section header is
992 a dummy placeholder entry, so we ignore it. */
994 for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
996 if (! bfd_section_from_shdr (abfd, shindex))
1000 return (abfd->xvec);
1002 got_wrong_format_error:
1003 bfd_set_error (bfd_error_wrong_format);
1005 got_no_memory_error:
1006 bfd_set_error (bfd_error_no_memory);
1009 if (new_tdata != NULL
1010 && new_tdata->elf_sect_ptr != NULL)
1011 bfd_release (abfd, new_tdata->elf_sect_ptr);
1012 if (i_shdrp != NULL)
1013 bfd_release (abfd, i_shdrp);
1014 if (new_tdata != NULL)
1015 bfd_release (abfd, new_tdata);
1016 elf_tdata (abfd) = preserved_tdata;
1021 /* ELF .o/exec file writing */
1023 /* Takes a bfd and a symbol, returns a pointer to the elf specific area
1024 of the symbol if there is one. */
1025 static INLINE elf_symbol_type *
1026 elf_symbol_from (ignore_abfd, symbol)
1030 if (symbol->the_bfd->xvec->flavour != bfd_target_elf_flavour)
1033 if (symbol->the_bfd->tdata.elf_obj_data == (struct elf_obj_tdata *) NULL)
1036 return (elf_symbol_type *) symbol;
1039 /* Create ELF output from BFD sections.
1041 Essentially, just create the section header and forget about the program
1045 elf_make_sections (abfd, asect, obj)
1050 /* most of what is in bfd_shdr_from_section goes in here... */
1051 /* and all of these sections generate at *least* one ELF section. */
1052 Elf_Internal_Shdr *this_hdr;
1053 this_hdr = &elf_section_data (asect)->this_hdr;
1055 this_hdr->sh_addr = asect->vma;
1056 this_hdr->sh_size = asect->_raw_size;
1057 /* contents already set by elf_set_section_contents */
1059 if (asect->flags & SEC_RELOC)
1061 /* emit a reloc section, and thus strtab and symtab... */
1062 Elf_Internal_Shdr *rela_hdr;
1063 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1065 rela_hdr = &elf_section_data (asect)->rel_hdr;
1067 /* orelocation has the data, reloc_count has the count... */
1070 rela_hdr->sh_type = SHT_RELA;
1071 rela_hdr->sh_entsize = sizeof (Elf_External_Rela);
1074 /* REL relocations */
1076 rela_hdr->sh_type = SHT_REL;
1077 rela_hdr->sh_entsize = sizeof (Elf_External_Rel);
1079 rela_hdr->sh_flags = 0;
1080 rela_hdr->sh_addr = 0;
1081 rela_hdr->sh_offset = 0;
1083 /* FIXME: Systems I've checked use an alignment of 4, but it is
1084 possible that some systems use a different alignment. */
1085 rela_hdr->sh_addralign = 4;
1089 if (asect->flags & SEC_ALLOC)
1091 this_hdr->sh_flags |= SHF_ALLOC;
1092 if (asect->flags & SEC_LOAD)
1094 /* @@ Do something with sh_type? */
1099 /* If this section is not part of the program image during
1100 execution, leave the address fields at 0. */
1101 this_hdr->sh_addr = 0;
1104 if (!(asect->flags & SEC_READONLY))
1105 this_hdr->sh_flags |= SHF_WRITE;
1107 if (asect->flags & SEC_CODE)
1108 this_hdr->sh_flags |= SHF_EXECINSTR;
1112 write_relocs (abfd, sec, xxx)
1117 Elf_Internal_Shdr *rela_hdr;
1118 Elf_External_Rela *outbound_relocas;
1119 Elf_External_Rel *outbound_relocs;
1121 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1122 asymbol *last_sym = 0;
1123 int last_sym_idx = 9999999; /* should always be written before use */
1125 if ((sec->flags & SEC_RELOC) == 0)
1128 /* The linker backend writes the relocs out itself, and sets the
1129 reloc_count field to zero to inhibit writing them here. Also,
1130 sometimes the SEC_RELOC flag gets set even when there aren't any
1132 if (sec->reloc_count == 0)
1135 rela_hdr = &elf_section_data (sec)->rel_hdr;
1137 rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
1138 rela_hdr->contents = (void *) bfd_alloc (abfd, rela_hdr->sh_size);
1139 if (!rela_hdr->contents)
1141 bfd_set_error (bfd_error_no_memory);
1142 abort (); /* FIXME */
1145 /* orelocation has the data, reloc_count has the count... */
1148 outbound_relocas = (Elf_External_Rela *) rela_hdr->contents;
1150 for (idx = 0; idx < sec->reloc_count; idx++)
1152 Elf_Internal_Rela dst_rela;
1153 Elf_External_Rela *src_rela;
1158 ptr = sec->orelocation[idx];
1159 src_rela = outbound_relocas + idx;
1160 if (!(abfd->flags & EXEC_P))
1161 dst_rela.r_offset = ptr->address - sec->vma;
1163 dst_rela.r_offset = ptr->address;
1165 sym = *ptr->sym_ptr_ptr;
1166 if (sym == last_sym)
1171 last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
1173 dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
1175 dst_rela.r_addend = ptr->addend;
1176 elf_swap_reloca_out (abfd, &dst_rela, src_rela);
1180 /* REL relocations */
1182 outbound_relocs = (Elf_External_Rel *) rela_hdr->contents;
1184 for (idx = 0; idx < sec->reloc_count; idx++)
1186 Elf_Internal_Rel dst_rel;
1187 Elf_External_Rel *src_rel;
1192 ptr = sec->orelocation[idx];
1193 sym = *ptr->sym_ptr_ptr;
1194 src_rel = outbound_relocs + idx;
1195 if (!(abfd->flags & EXEC_P))
1196 dst_rel.r_offset = ptr->address - sec->vma;
1198 dst_rel.r_offset = ptr->address;
1200 if (sym == last_sym)
1205 last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
1207 dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type);
1209 elf_swap_reloc_out (abfd, &dst_rel, src_rel);
1215 fix_up_strtabs (abfd, asect, obj)
1220 Elf_Internal_Shdr *this_hdr = &elf_section_data (asect)->this_hdr;
1221 int this_idx = elf_section_data (asect)->this_idx;
1223 /* @@ Check flags! */
1224 if (!strncmp (asect->name, ".stab", 5)
1225 && !strcmp ("str", asect->name + strlen (asect->name) - 3))
1227 size_t len = strlen (asect->name) + 1;
1228 char *s = (char *) malloc (len);
1230 /* FIXME: Should deal more gracefully with errors. */
1232 strcpy (s, asect->name);
1234 asect = bfd_get_section_by_name (abfd, s);
1238 elf_section_data (asect)->this_hdr.sh_link = this_idx;
1239 /* @@ Assuming 32 bits! */
1240 elf_section_data (asect)->this_hdr.sh_entsize = 0xc;
1242 this_hdr->sh_type = SHT_STRTAB;
1247 elf_fake_sections (abfd, asect, obj)
1252 /* most of what is in bfd_shdr_from_section goes in here... */
1253 /* and all of these sections generate at *least* one ELF section. */
1255 Elf_Internal_Shdr *this_hdr;
1256 this_hdr = &elf_section_data (asect)->this_hdr;
1258 bfd_add_to_strtab (abfd, elf_shstrtab (abfd), asect->name);
1259 if (this_hdr->sh_name == (unsigned long) -1)
1260 abort (); /* FIXME */
1261 /* We need to log the type *now* so that elf_section_from_bfd_section
1262 can find us... have to set rawdata too. */
1263 this_hdr->rawdata = (void *) asect;
1264 this_hdr->sh_addralign = 1 << asect->alignment_power;
1266 /* FIXME: This should not be based on section names. */
1267 if (strcmp (asect->name, ".dynstr") == 0)
1268 this_hdr->sh_type = SHT_STRTAB;
1269 else if (strcmp (asect->name, ".hash") == 0)
1270 this_hdr->sh_type = SHT_HASH;
1271 else if (strcmp (asect->name, ".dynsym") == 0)
1272 this_hdr->sh_type = SHT_DYNSYM;
1273 else if (strcmp (asect->name, ".dynamic") == 0)
1274 this_hdr->sh_type = SHT_DYNAMIC;
1275 else if (strncmp (asect->name, ".rel.", 5) == 0)
1276 this_hdr->sh_type = SHT_REL;
1277 else if (strncmp (asect->name, ".rela.", 6) == 0)
1278 this_hdr->sh_type = SHT_RELA;
1279 else if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD))
1280 this_hdr->sh_type = SHT_PROGBITS;
1281 else if ((asect->flags & SEC_ALLOC) && ((asect->flags & SEC_LOAD) == 0))
1283 BFD_ASSERT (strcmp (asect->name, ".bss") == 0
1284 || strcmp (asect->name, ".sbss") == 0);
1285 this_hdr->sh_type = SHT_NOBITS;
1287 /* FIXME I am not sure how to detect a .note section from the flags
1288 word of an `asection'. */
1289 else if (!strcmp (asect->name, ".note"))
1290 this_hdr->sh_type = SHT_NOTE;
1292 this_hdr->sh_type = SHT_PROGBITS;
1294 this_hdr->sh_flags = 0;
1295 this_hdr->sh_addr = 0;
1296 this_hdr->sh_size = 0;
1297 this_hdr->sh_entsize = 0;
1298 this_hdr->sh_info = 0;
1299 this_hdr->sh_link = 0;
1300 this_hdr->sh_offset = 0;
1303 /* Now, check for processor-specific section types. */
1305 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1307 if (bed->elf_backend_fake_sections)
1308 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1312 /* Emit a strtab and symtab, and possibly a reloc section. */
1313 Elf_Internal_Shdr *rela_hdr;
1315 /* Note that only one symtab is used, so just remember it
1318 if (asect->flags & SEC_RELOC)
1320 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1322 rela_hdr = &elf_section_data (asect)->rel_hdr;
1324 bfd_add_2_to_strtab (abfd, elf_shstrtab (abfd),
1325 use_rela_p ? ".rela" : ".rel",
1327 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1328 rela_hdr->sh_entsize = (use_rela_p
1329 ? sizeof (Elf_External_Rela)
1330 : sizeof (Elf_External_Rel));
1332 rela_hdr->sh_flags = 0;
1333 rela_hdr->sh_addr = 0;
1334 rela_hdr->sh_size = 0;
1335 rela_hdr->sh_offset = 0;
1337 /* FIXME: Systems I've checked use an alignment of 4, but some
1338 systems may use a different alignment. */
1339 rela_hdr->sh_addralign = 4;
1344 if (asect->flags & SEC_ALLOC)
1346 this_hdr->sh_flags |= SHF_ALLOC;
1347 if (asect->flags & SEC_LOAD)
1349 /* @@ Do something with sh_type? */
1352 if (!(asect->flags & SEC_READONLY))
1353 this_hdr->sh_flags |= SHF_WRITE;
1354 if (asect->flags & SEC_CODE)
1355 this_hdr->sh_flags |= SHF_EXECINSTR;
1358 /* Map symbol from it's internal number to the external number, moving
1359 all local symbols to be at the head of the list. */
1362 sym_is_global (abfd, sym)
1366 /* If the backend has a special mapping, use it. */
1367 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1368 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1371 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
1373 if (sym->flags & BSF_LOCAL)
1377 if (sym->section == 0)
1379 /* Is this valid? */
1384 if (sym->section == &bfd_und_section)
1386 if (bfd_is_com_section (sym->section))
1388 if (sym->flags & (BSF_LOCAL | BSF_SECTION_SYM | BSF_FILE))
1394 elf_map_symbols (abfd)
1397 int symcount = bfd_get_symcount (abfd);
1398 asymbol **syms = bfd_get_outsymbols (abfd);
1399 asymbol **sect_syms;
1401 int num_globals = 0;
1402 int num_locals2 = 0;
1403 int num_globals2 = 0;
1405 int num_sections = 0;
1406 Elf_Sym_Extra *sym_extra;
1411 fprintf (stderr, "elf_map_symbols\n");
1415 /* Add local symbols for each section for which there are relocs.
1416 FIXME: How can we tell which sections have relocs at this point?
1417 Will reloc_count always be accurate? Actually, I think most ELF
1418 targets create section symbols for all sections anyhow. */
1419 for (asect = abfd->sections; asect; asect = asect->next)
1421 if (max_index < asect->index)
1422 max_index = asect->index;
1426 elf_num_section_syms (abfd) = max_index;
1427 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1428 elf_section_syms (abfd) = sect_syms;
1432 bfd_set_error (bfd_error_no_memory);
1436 for (asect = abfd->sections; asect; asect = asect->next)
1438 asymbol *sym = bfd_make_empty_symbol (abfd);
1441 bfd_set_error (bfd_error_no_memory);
1444 sym->the_bfd = abfd;
1445 sym->name = asect->name;
1446 sym->value = asect->vma;
1447 sym->flags = BSF_SECTION_SYM;
1448 sym->section = asect;
1449 sect_syms[asect->index] = sym;
1453 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1454 asect->name, (long) asect->vma, asect->index, (long) asect);
1461 syms = (asymbol **) bfd_realloc (abfd, syms,
1462 ((symcount + num_sections + 1)
1463 * sizeof (asymbol *)));
1465 syms = (asymbol **) bfd_alloc (abfd,
1466 (num_sections + 1) * sizeof (asymbol *));
1469 bfd_set_error (bfd_error_no_memory);
1473 for (asect = abfd->sections; asect; asect = asect->next)
1475 if (sect_syms[asect->index])
1476 syms[symcount++] = sect_syms[asect->index];
1479 syms[symcount] = (asymbol *) 0;
1480 bfd_set_symtab (abfd, syms, symcount);
1483 elf_sym_extra (abfd) = sym_extra
1484 = (Elf_Sym_Extra *) bfd_alloc (abfd, symcount * sizeof (Elf_Sym_Extra));
1487 bfd_set_error (bfd_error_no_memory);
1491 /* Identify and classify all of the symbols. */
1492 for (idx = 0; idx < symcount; idx++)
1494 if (!sym_is_global (abfd, syms[idx]))
1500 /* Now provide mapping information. Add +1 for skipping over the
1502 for (idx = 0; idx < symcount; idx++)
1504 syms[idx]->udata = (PTR) & sym_extra[idx];
1505 if (!sym_is_global (abfd, syms[idx]))
1506 sym_extra[idx].elf_sym_num = 1 + num_locals2++;
1508 sym_extra[idx].elf_sym_num = 1 + num_locals + num_globals2++;
1511 elf_num_locals (abfd) = num_locals;
1512 elf_num_globals (abfd) = num_globals;
1516 /* Compute the file positions we are going to put the sections at, and
1517 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1518 is not NULL, this is being called by the ELF backend linker. */
1521 elf_compute_section_file_positions (abfd, link_info)
1523 struct bfd_link_info *link_info;
1525 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1526 Elf_Internal_Shdr *shstrtab_hdr;
1528 if (abfd->output_has_begun)
1531 /* Do any elf backend specific processing first. */
1532 if (bed->elf_backend_begin_write_processing)
1533 (*bed->elf_backend_begin_write_processing) (abfd);
1535 if (! prep_headers (abfd))
1538 bfd_map_over_sections (abfd, elf_fake_sections, 0);
1540 if (!assign_section_numbers (abfd))
1543 bfd_map_over_sections (abfd, elf_make_sections, 0);
1545 bfd_map_over_sections (abfd, fix_up_strtabs, 0); /* .stab/.stabstr &c */
1547 /* The backend linker builds symbol table information itself. */
1548 if (link_info == NULL)
1550 if (! swap_out_syms (abfd))
1554 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
1555 /* sh_name was set in prep_headers. */
1556 shstrtab_hdr->sh_type = SHT_STRTAB;
1557 shstrtab_hdr->sh_flags = 0;
1558 shstrtab_hdr->sh_addr = 0;
1559 shstrtab_hdr->sh_size = elf_shstrtab (abfd)->length;
1560 shstrtab_hdr->sh_entsize = 0;
1561 shstrtab_hdr->sh_link = 0;
1562 shstrtab_hdr->sh_info = 0;
1563 /* sh_offset is set in assign_file_positions_for_symtabs_and_strtabs. */
1564 shstrtab_hdr->sh_addralign = 1;
1565 shstrtab_hdr->contents = (PTR) elf_shstrtab (abfd)->tab;
1567 if (!assign_file_positions_except_relocs (abfd,
1568 link_info == NULL ? true : false))
1571 abfd->output_has_begun = true;
1577 /* Assign all ELF section numbers. The dummy first section is handled here
1578 too. The link/info pointers for the standard section types are filled
1579 in here too, while we're at it. (Link pointers for .stab sections are
1580 not filled in here.) */
1582 assign_section_numbers (abfd)
1585 struct elf_obj_tdata *t = elf_tdata (abfd);
1587 int section_number = 1;
1589 Elf_Internal_Shdr **i_shdrp;
1591 t->shstrtab_hdr.sh_size = elf_shstrtab (abfd)->length;
1592 t->shstrtab_hdr.contents = (void *) elf_shstrtab (abfd)->tab;
1594 t->shstrtab_section = section_number++;
1595 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1598 t->symtab_section = section_number++;
1599 t->strtab_section = section_number++;
1600 t->symtab_hdr.sh_link = t->strtab_section;
1602 for (sec = abfd->sections; sec; sec = sec->next)
1604 struct bfd_elf_section_data *d = elf_section_data (sec);
1605 d->this_idx = section_number++;
1606 if (sec->flags & SEC_RELOC)
1608 d->rel_idx = section_number++;
1609 d->rel_hdr.sh_link = t->symtab_section;
1610 d->rel_hdr.sh_info = d->this_idx;
1614 /* No handling for per-section string tables currently. */
1616 elf_elfheader (abfd)->e_shnum = section_number;
1618 /* Set up the list of section header pointers, in agreement with the
1620 i_shdrp = ((Elf_Internal_Shdr **)
1621 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1622 if (i_shdrp == NULL)
1624 bfd_set_error (bfd_error_no_memory);
1627 i_shdrp[0] = ((Elf_Internal_Shdr *)
1628 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1629 if (i_shdrp[0] == NULL)
1631 bfd_release (abfd, i_shdrp);
1632 bfd_set_error (bfd_error_no_memory);
1635 elf_elfsections (abfd) = i_shdrp;
1636 for (i = 1; i < section_number; i++)
1638 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1640 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1643 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1644 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1646 for (sec = abfd->sections; sec; sec = sec->next)
1648 struct bfd_elf_section_data *d = elf_section_data (sec);
1649 i_shdrp[d->this_idx] = &d->this_hdr;
1651 i_shdrp[d->rel_idx] = &d->rel_hdr;
1653 /* Make sure we got everything.... */
1654 for (i = 0; i < section_number; i++)
1655 if (i_shdrp[i] == NULL)
1660 /* Align to the maximum file alignment that could be required for any
1661 ELF data structure. */
1663 static INLINE file_ptr
1664 align_file_position (off)
1667 return (off + FILE_ALIGN - 1) & ~(FILE_ALIGN - 1);
1670 /* Assign a file position to a section, optionally aligning to the
1671 required section alignment. */
1673 static INLINE file_ptr
1674 assign_file_position_for_section (i_shdrp, offset, align)
1675 Elf_Internal_Shdr *i_shdrp;
1683 al = i_shdrp->sh_addralign;
1685 offset = BFD_ALIGN (offset, al);
1687 i_shdrp->sh_offset = offset;
1688 if (i_shdrp->rawdata != NULL)
1689 ((asection *) i_shdrp->rawdata)->filepos = offset;
1690 if (i_shdrp->sh_type != SHT_NOBITS)
1691 offset += i_shdrp->sh_size;
1695 /* Get the size of the program header. This is called by the linker
1696 before any of the section VMA's are set, so it can't calculate the
1697 correct value for a strange memory layout. */
1699 static bfd_size_type
1700 get_program_header_size (abfd)
1706 /* Assume we will need exactly two PT_LOAD segments: one for text
1707 and one for data. */
1710 s = bfd_get_section_by_name (abfd, ".interp");
1711 if (s != NULL && (s->flags & SEC_LOAD) != 0)
1713 /* If we have a loadable interpreter section, we need a
1714 PT_INTERP segment. In this case, assume we also need a
1715 PT_PHDR segment, although that may not be true for all
1720 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
1722 /* We need a PT_DYNAMIC segment. */
1726 return segs * sizeof (Elf_External_Phdr);
1729 /* Create the program header. OFF is the file offset where the
1730 program header should be written. FIRST is the first loadable ELF
1731 section. PHDR_SIZE is the size of the program header as returned
1732 by get_program_header_size. */
1735 map_program_segments (abfd, off, first, phdr_size)
1738 Elf_Internal_Shdr *first;
1739 bfd_size_type phdr_size;
1741 Elf_Internal_Phdr phdrs[10];
1742 unsigned int phdr_count;
1743 Elf_Internal_Phdr *phdr;
1744 int phdr_size_adjust;
1746 Elf_Internal_Shdr **hdrpp;
1747 asection *sinterp, *sdyn;
1748 unsigned int last_type;
1749 Elf_Internal_Ehdr *i_ehdrp;
1751 BFD_ASSERT ((abfd->flags & EXEC_P) != 0);
1752 BFD_ASSERT (phdr_size / sizeof (Elf_Internal_Phdr)
1753 <= sizeof phdrs / sizeof (phdrs[0]));
1758 phdr_size_adjust = 0;
1760 /* If we have a loadable .interp section, we must create a PT_INTERP
1761 segment which must precede all PT_LOAD segments. We assume that
1762 we must also create a PT_PHDR segment, although that may not be
1763 true for all targets. */
1764 sinterp = bfd_get_section_by_name (abfd, ".interp");
1765 if (sinterp != NULL && (sinterp->flags & SEC_LOAD) != 0)
1767 BFD_ASSERT (first != NULL);
1769 phdr->p_type = PT_PHDR;
1771 phdr->p_offset = off;
1773 /* Account for any adjustment made because of the alignment of
1774 the first loadable section. */
1775 phdr_size_adjust = (first->sh_offset - phdr_size) - off;
1776 BFD_ASSERT (phdr_size_adjust >= 0 && phdr_size_adjust < 128);
1778 /* The program header precedes all loadable sections. This lets
1779 us compute its loadable address. This depends on the linker
1781 phdr->p_vaddr = first->sh_addr - (phdr_size + phdr_size_adjust);
1784 phdr->p_filesz = phdr_size;
1785 phdr->p_memsz = phdr_size;
1787 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
1788 phdr->p_flags = PF_R | PF_X;
1790 phdr->p_align = FILE_ALIGN;
1791 BFD_ASSERT ((phdr->p_vaddr - phdr->p_offset) % FILE_ALIGN == 0);
1793 /* Include the ELF header in the first loadable segment. */
1794 phdr_size_adjust += off;
1799 phdr->p_type = PT_INTERP;
1800 phdr->p_offset = sinterp->filepos;
1801 phdr->p_vaddr = sinterp->vma;
1803 phdr->p_filesz = sinterp->_raw_size;
1804 phdr->p_memsz = sinterp->_raw_size;
1805 phdr->p_flags = PF_R;
1806 phdr->p_align = 1 << bfd_get_section_alignment (abfd, sinterp);
1812 /* Look through the sections to see how they will be divided into
1813 program segments. The sections must be arranged in order by
1814 sh_addr for this to work correctly. */
1815 phdr->p_type = PT_NULL;
1816 last_type = SHT_PROGBITS;
1817 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
1818 i < elf_elfheader (abfd)->e_shnum;
1821 Elf_Internal_Shdr *hdr;
1825 /* Ignore any section which will not be part of the process
1827 if ((hdr->sh_flags & SHF_ALLOC) == 0)
1830 /* If this section fits in the segment we are constructing, add
1832 if (phdr->p_type != PT_NULL
1833 && (hdr->sh_offset - (phdr->p_offset + phdr->p_memsz)
1834 == hdr->sh_addr - (phdr->p_vaddr + phdr->p_memsz))
1835 && (last_type != SHT_NOBITS || hdr->sh_type == SHT_NOBITS))
1837 bfd_size_type adjust;
1839 adjust = hdr->sh_addr - (phdr->p_vaddr + phdr->p_memsz);
1840 phdr->p_memsz += hdr->sh_size + adjust;
1841 if (hdr->sh_type != SHT_NOBITS)
1842 phdr->p_filesz += hdr->sh_size + adjust;
1843 if ((hdr->sh_flags & SHF_WRITE) != 0)
1844 phdr->p_flags |= PF_W;
1845 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1846 phdr->p_flags |= PF_X;
1847 last_type = hdr->sh_type;
1851 /* If we have a segment, move to the next one. */
1852 if (phdr->p_type != PT_NULL)
1858 /* Start a new segment. */
1859 phdr->p_type = PT_LOAD;
1860 phdr->p_offset = hdr->sh_offset;
1861 phdr->p_vaddr = hdr->sh_addr;
1863 if (hdr->sh_type == SHT_NOBITS)
1866 phdr->p_filesz = hdr->sh_size;
1867 phdr->p_memsz = hdr->sh_size;
1868 phdr->p_flags = PF_R;
1869 if ((hdr->sh_flags & SHF_WRITE) != 0)
1870 phdr->p_flags |= PF_W;
1871 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1872 phdr->p_flags |= PF_X;
1873 phdr->p_align = get_elf_backend_data (abfd)->maxpagesize;
1877 && (sinterp->flags & SEC_LOAD) != 0)
1879 phdr->p_offset -= phdr_size + phdr_size_adjust;
1880 phdr->p_vaddr -= phdr_size + phdr_size_adjust;
1881 phdr->p_filesz += phdr_size + phdr_size_adjust;
1882 phdr->p_memsz += phdr_size + phdr_size_adjust;
1885 last_type = hdr->sh_type;
1888 if (phdr->p_type != PT_NULL)
1894 /* If we have a .dynamic section, create a PT_DYNAMIC segment. */
1895 sdyn = bfd_get_section_by_name (abfd, ".dynamic");
1896 if (sdyn != NULL && (sdyn->flags & SEC_LOAD) != 0)
1898 phdr->p_type = PT_DYNAMIC;
1899 phdr->p_offset = sdyn->filepos;
1900 phdr->p_vaddr = sdyn->vma;
1902 phdr->p_filesz = sdyn->_raw_size;
1903 phdr->p_memsz = sdyn->_raw_size;
1904 phdr->p_flags = PF_R;
1905 if ((sdyn->flags & SEC_READONLY) == 0)
1906 phdr->p_flags |= PF_W;
1907 if ((sdyn->flags & SEC_CODE) != 0)
1908 phdr->p_flags |= PF_X;
1909 phdr->p_align = 1 << bfd_get_section_alignment (abfd, sdyn);
1915 /* Make sure the return value from get_program_header_size matches
1916 what we computed here. */
1917 if (phdr_count != phdr_size / sizeof (Elf_Internal_Phdr))
1920 /* Set up program header information. */
1921 i_ehdrp = elf_elfheader (abfd);
1922 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
1923 i_ehdrp->e_phoff = off;
1924 i_ehdrp->e_phnum = phdr_count;
1926 /* Save the program headers away. I don't think anybody uses this
1927 information right now. */
1928 elf_tdata (abfd)->phdr = ((Elf_Internal_Phdr *)
1931 * sizeof (Elf_Internal_Phdr))));
1932 if (elf_tdata (abfd)->phdr == NULL && phdr_count != 0)
1934 bfd_set_error (bfd_error_no_memory);
1935 return (file_ptr) -1;
1937 memcpy (elf_tdata (abfd)->phdr, phdrs,
1938 phdr_count * sizeof (Elf_Internal_Phdr));
1940 /* Write out the program headers. */
1941 if (bfd_seek (abfd, off, SEEK_SET) != 0)
1942 return (file_ptr) -1;
1944 for (i = 0, phdr = phdrs; i < phdr_count; i++, phdr++)
1946 Elf_External_Phdr extphdr;
1948 elf_swap_phdr_out (abfd, phdr, &extphdr);
1949 if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), 1, abfd)
1950 != sizeof (Elf_External_Phdr))
1951 return (file_ptr) -1;
1954 return off + phdr_count * sizeof (Elf_External_Phdr);
1957 /* Work out the file positions of all the sections. This is called by
1958 elf_compute_section_file_positions. All the section sizes and VMAs
1959 must be known before this is called.
1961 We do not consider reloc sections at this point, unless they form
1962 part of the loadable image. Reloc sections are assigned file
1963 positions in assign_file_positions_for_relocs, which is called by
1964 write_object_contents and final_link.
1966 If DOSYMS is false, we do not assign file positions for the symbol
1967 table or the string table. */
1970 assign_file_positions_except_relocs (abfd, dosyms)
1974 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
1975 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
1976 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
1979 /* Start after the ELF header. */
1980 off = i_ehdrp->e_ehsize;
1982 if ((abfd->flags & EXEC_P) == 0)
1984 Elf_Internal_Shdr **hdrpp;
1987 /* We are not creating an executable, which means that we are
1988 not creating a program header, and that the actual order of
1989 the sections in the file is unimportant. */
1990 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
1992 Elf_Internal_Shdr *hdr;
1995 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
1997 hdr->sh_offset = -1;
2001 && (i == tdata->symtab_section
2002 || i == tdata->strtab_section))
2004 hdr->sh_offset = -1;
2008 off = assign_file_position_for_section (hdr, off, true);
2014 bfd_size_type phdr_size;
2015 bfd_vma maxpagesize;
2016 Elf_Internal_Shdr **hdrpp;
2018 Elf_Internal_Shdr *first;
2021 /* We are creating an executable. We must create a program
2022 header. We can't actually create the program header until we
2023 have set the file positions for the sections, but we can
2024 figure out how big it is going to be. */
2025 off = align_file_position (off);
2026 phdr_size = get_program_header_size (abfd);
2027 if (phdr_size == (file_ptr) -1)
2032 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2033 if (maxpagesize == 0)
2036 /* FIXME: We might want to sort the sections on the sh_addr
2037 field here. For now, we just assume that the linker will
2038 create the sections in an appropriate order. */
2040 /* Assign file positions in two passes. In the first pass, we
2041 assign a file position to every section which forms part of
2042 the executable image. */
2044 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2046 Elf_Internal_Shdr *hdr;
2049 if ((hdr->sh_flags & SHF_ALLOC) == 0)
2055 if ((abfd->flags & D_PAGED) != 0)
2057 /* The section VMA must equal the file position modulo
2058 the page size. This is required by the program
2060 off += (hdr->sh_addr - off) % maxpagesize;
2063 off = assign_file_position_for_section (hdr, off, false);
2066 /* Assign file positions to all the sections which do not form
2067 part of the loadable image, except for the relocs. */
2068 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2070 Elf_Internal_Shdr *hdr;
2073 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2075 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2077 hdr->sh_offset = -1;
2081 && (i == tdata->symtab_section
2082 || i == tdata->strtab_section))
2084 hdr->sh_offset = -1;
2088 off = assign_file_position_for_section (hdr, off, true);
2091 phdr_map = map_program_segments (abfd, phdr_off, first, phdr_size);
2092 if (phdr_map == (file_ptr) -1)
2094 BFD_ASSERT (phdr_map == phdr_off + phdr_size);
2097 /* Place the section headers. */
2098 off = align_file_position (off);
2099 i_ehdrp->e_shoff = off;
2100 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2102 elf_tdata (abfd)->next_file_pos = off;
2111 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2112 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2113 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2115 struct strtab *shstrtab;
2117 i_ehdrp = elf_elfheader (abfd);
2118 i_shdrp = elf_elfsections (abfd);
2120 shstrtab = bfd_new_strtab (abfd);
2124 elf_shstrtab (abfd) = shstrtab;
2126 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2127 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2128 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2129 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2131 i_ehdrp->e_ident[EI_CLASS] = ELFCLASS;
2132 i_ehdrp->e_ident[EI_DATA] =
2133 abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB;
2134 i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT;
2136 for (count = EI_PAD; count < EI_NIDENT; count++)
2137 i_ehdrp->e_ident[count] = 0;
2139 i_ehdrp->e_type = (abfd->flags & EXEC_P) ? ET_EXEC : ET_REL;
2140 switch (bfd_get_arch (abfd))
2142 case bfd_arch_unknown:
2143 i_ehdrp->e_machine = EM_NONE;
2145 case bfd_arch_sparc:
2146 i_ehdrp->e_machine = EM_SPARC;
2147 /* start-sanitize-v9 */
2149 i_ehdrp->e_machine = EM_SPARC64;
2151 /* end-sanitize-v9 */
2154 i_ehdrp->e_machine = EM_386;
2157 i_ehdrp->e_machine = EM_68K;
2160 i_ehdrp->e_machine = EM_88K;
2163 i_ehdrp->e_machine = EM_860;
2165 case bfd_arch_mips: /* MIPS Rxxxx */
2166 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
2169 i_ehdrp->e_machine = EM_PARISC;
2171 case bfd_arch_powerpc:
2172 i_ehdrp->e_machine = EM_CYGNUS_POWERPC;
2174 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2176 i_ehdrp->e_machine = EM_NONE;
2178 i_ehdrp->e_version = EV_CURRENT;
2179 i_ehdrp->e_ehsize = sizeof (Elf_External_Ehdr);
2181 /* no program header, for now. */
2182 i_ehdrp->e_phoff = 0;
2183 i_ehdrp->e_phentsize = 0;
2184 i_ehdrp->e_phnum = 0;
2186 /* each bfd section is section header entry */
2187 i_ehdrp->e_entry = bfd_get_start_address (abfd);
2188 i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr);
2190 /* if we're building an executable, we'll need a program header table */
2191 if (abfd->flags & EXEC_P)
2193 /* it all happens later */
2195 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2197 /* elf_build_phdrs() returns a (NULL-terminated) array of
2198 Elf_Internal_Phdrs */
2199 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2200 i_ehdrp->e_phoff = outbase;
2201 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2206 i_ehdrp->e_phentsize = 0;
2208 i_ehdrp->e_phoff = 0;
2211 elf_tdata (abfd)->symtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2213 elf_tdata (abfd)->strtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2215 elf_tdata (abfd)->shstrtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2217 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2218 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2219 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
2226 swap_out_syms (abfd)
2229 if (!elf_map_symbols (abfd))
2232 /* Dump out the symtabs. */
2234 int symcount = bfd_get_symcount (abfd);
2235 asymbol **syms = bfd_get_outsymbols (abfd);
2236 struct strtab *stt = bfd_new_strtab (abfd);
2237 Elf_Internal_Shdr *symtab_hdr;
2238 Elf_Internal_Shdr *symstrtab_hdr;
2239 Elf_External_Sym *outbound_syms;
2244 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2245 symtab_hdr->sh_type = SHT_SYMTAB;
2246 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
2247 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
2248 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
2250 /* FIXME: Systems I've checked use 4 byte alignment for .symtab,
2251 but it is possible that there are systems which use a different
2253 symtab_hdr->sh_addralign = 4;
2255 /* see assert in elf_fake_sections that supports this: */
2256 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
2257 symstrtab_hdr->sh_type = SHT_STRTAB;
2259 outbound_syms = (Elf_External_Sym *)
2260 bfd_alloc (abfd, (1 + symcount) * sizeof (Elf_External_Sym));
2263 bfd_set_error (bfd_error_no_memory);
2266 /* now generate the data (for "contents") */
2268 /* Fill in zeroth symbol and swap it out. */
2269 Elf_Internal_Sym sym;
2275 sym.st_shndx = SHN_UNDEF;
2276 elf_swap_symbol_out (abfd, &sym, outbound_syms);
2278 for (idx = 0; idx < symcount; idx++)
2280 Elf_Internal_Sym sym;
2281 bfd_vma value = syms[idx]->value;
2283 if (syms[idx]->flags & BSF_SECTION_SYM)
2284 /* Section symbols have no names. */
2288 sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name);
2289 if (sym.st_name == (unsigned long) -1)
2293 if (bfd_is_com_section (syms[idx]->section))
2295 /* ELF common symbols put the alignment into the `value' field,
2296 and the size into the `size' field. This is backwards from
2297 how BFD handles it, so reverse it here. */
2298 sym.st_size = value;
2299 /* Should retrieve this from somewhere... */
2301 sym.st_shndx = elf_section_from_bfd_section (abfd,
2302 syms[idx]->section);
2306 asection *sec = syms[idx]->section;
2307 elf_symbol_type *type_ptr;
2310 if (sec->output_section)
2312 value += sec->output_offset;
2313 sec = sec->output_section;
2316 sym.st_value = value;
2317 type_ptr = elf_symbol_from (abfd, syms[idx]);
2318 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
2319 sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec);
2323 /* Writing this would be a hell of a lot easier if we had
2324 some decent documentation on bfd, and knew what to expect
2325 of the library, and what to demand of applications. For
2326 example, it appears that `objcopy' might not set the
2327 section of a symbol to be a section that is actually in
2329 sec2 = bfd_get_section_by_name (abfd, sec->name);
2330 BFD_ASSERT (sec2 != 0);
2331 sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec2);
2332 BFD_ASSERT (shndx != -1);
2336 if (bfd_is_com_section (syms[idx]->section))
2337 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT);
2338 else if (syms[idx]->section == &bfd_und_section)
2339 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_NOTYPE);
2340 else if (syms[idx]->flags & BSF_SECTION_SYM)
2341 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
2342 else if (syms[idx]->flags & BSF_FILE)
2343 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
2346 int bind = STB_LOCAL;
2347 int type = STT_OBJECT;
2348 unsigned int flags = syms[idx]->flags;
2350 if (flags & BSF_LOCAL)
2352 else if (flags & BSF_WEAK)
2354 else if (flags & BSF_GLOBAL)
2357 if (flags & BSF_FUNCTION)
2360 sym.st_info = ELF_ST_INFO (bind, type);
2364 elf_swap_symbol_out (abfd, &sym,
2366 + elf_sym_extra (abfd)[idx].elf_sym_num));
2369 symtab_hdr->contents = (PTR) outbound_syms;
2370 symstrtab_hdr->contents = (PTR) stt->tab;
2371 symstrtab_hdr->sh_size = stt->length;
2372 symstrtab_hdr->sh_type = SHT_STRTAB;
2374 symstrtab_hdr->sh_flags = 0;
2375 symstrtab_hdr->sh_addr = 0;
2376 symstrtab_hdr->sh_entsize = 0;
2377 symstrtab_hdr->sh_link = 0;
2378 symstrtab_hdr->sh_info = 0;
2379 symstrtab_hdr->sh_addralign = 1;
2380 symstrtab_hdr->size = 0;
2387 write_shdrs_and_ehdr (abfd)
2390 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
2391 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2392 Elf_External_Shdr *x_shdrp; /* Section header table, external form */
2393 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2395 struct strtab *shstrtab;
2397 i_ehdrp = elf_elfheader (abfd);
2398 i_shdrp = elf_elfsections (abfd);
2399 shstrtab = elf_shstrtab (abfd);
2401 /* swap the header before spitting it out... */
2404 elf_debug_file (i_ehdrp);
2406 elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
2407 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2408 || (bfd_write ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd)
2409 != sizeof (x_ehdr)))
2412 /* at this point we've concocted all the ELF sections... */
2413 x_shdrp = (Elf_External_Shdr *)
2414 bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
2417 bfd_set_error (bfd_error_no_memory);
2421 for (count = 0; count < i_ehdrp->e_shnum; count++)
2424 elf_debug_section (shstrtab->tab + i_shdrp[count]->sh_name, count,
2427 elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count);
2429 if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0
2430 || (bfd_write ((PTR) x_shdrp, sizeof (*x_shdrp), i_ehdrp->e_shnum, abfd)
2431 != sizeof (*x_shdrp) * i_ehdrp->e_shnum))
2434 /* need to dump the string table too... */
2439 /* Assign file positions for all the reloc sections which are not part
2440 of the loadable file image. */
2443 assign_file_positions_for_relocs (abfd)
2448 Elf_Internal_Shdr **shdrpp;
2450 off = elf_tdata (abfd)->next_file_pos;
2452 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
2453 i < elf_elfheader (abfd)->e_shnum;
2456 Elf_Internal_Shdr *shdrp;
2459 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
2460 && shdrp->sh_offset == -1)
2461 off = assign_file_position_for_section (shdrp, off, true);
2464 elf_tdata (abfd)->next_file_pos = off;
2468 NAME(bfd_elf,write_object_contents) (abfd)
2471 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2472 Elf_Internal_Ehdr *i_ehdrp;
2473 Elf_Internal_Shdr **i_shdrp;
2476 /* We don't know how to write dynamic objects. Specifically, we
2477 don't know how to construct the program header. */
2478 if ((abfd->flags & DYNAMIC) != 0)
2480 fprintf (stderr, "Writing ELF dynamic objects is not supported\n");
2481 bfd_set_error (bfd_error_wrong_format);
2485 if (! abfd->output_has_begun
2486 && ! elf_compute_section_file_positions (abfd,
2487 (struct bfd_link_info *) NULL))
2490 i_shdrp = elf_elfsections (abfd);
2491 i_ehdrp = elf_elfheader (abfd);
2493 bfd_map_over_sections (abfd, write_relocs, (PTR) 0);
2494 assign_file_positions_for_relocs (abfd);
2496 /* After writing the headers, we need to write the sections too... */
2497 for (count = 1; count < i_ehdrp->e_shnum; count++)
2499 if (bed->elf_backend_section_processing)
2500 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
2501 if (i_shdrp[count]->contents)
2503 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
2504 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
2506 != i_shdrp[count]->sh_size))
2511 if (bed->elf_backend_final_write_processing)
2512 (*bed->elf_backend_final_write_processing) (abfd);
2514 return write_shdrs_and_ehdr (abfd);
2517 /* Given an index of a section, retrieve a pointer to it. Note
2518 that for our purposes, sections are indexed by {1, 2, ...} with
2519 0 being an illegal index. */
2521 /* In the original, each ELF section went into exactly one BFD
2522 section. This doesn't really make sense, so we need a real mapping.
2523 The mapping has to hide in the Elf_Internal_Shdr since asection
2524 doesn't have anything like a tdata field... */
2527 section_from_elf_index (abfd, index)
2531 /* @@ Is bfd_com_section really correct in all the places it could
2532 be returned from this routine? */
2534 if (index == SHN_ABS)
2535 return &bfd_com_section; /* not abs? */
2536 if (index == SHN_COMMON)
2537 return &bfd_com_section;
2539 if (index > elf_elfheader (abfd)->e_shnum)
2543 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[index];
2545 switch (hdr->sh_type)
2547 /* ELF sections that map to BFD sections */
2552 if (hdr->rawdata == NULL)
2554 if (! bfd_section_from_shdr (abfd, index))
2557 return (struct sec *) hdr->rawdata;
2560 return (struct sec *) &bfd_abs_section;
2565 /* given a section, search the header to find them... */
2567 elf_section_from_bfd_section (abfd, asect)
2571 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
2573 Elf_Internal_Shdr *hdr;
2574 int maxindex = elf_elfheader (abfd)->e_shnum;
2576 if (asect == &bfd_abs_section)
2578 if (asect == &bfd_com_section)
2580 if (asect == &bfd_und_section)
2583 for (index = 0; index < maxindex; index++)
2585 hdr = i_shdrp[index];
2586 switch (hdr->sh_type)
2588 /* ELF sections that map to BFD sections */
2596 if (((struct sec *) (hdr->rawdata)) == asect)
2602 /* fix_up_strtabs will generate STRTAB sections with names
2604 if (!strncmp (asect->name, ".stab", 5)
2605 && !strcmp ("str", asect->name + strlen (asect->name) - 3))
2609 if (((struct sec *) (hdr->rawdata)) == asect)
2617 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2619 if (bed->elf_backend_section_from_bfd_section)
2624 if ((*bed->elf_backend_section_from_bfd_section)
2625 (abfd, hdr, asect, &retval))
2635 /* given a symbol, return the bfd index for that symbol. */
2637 elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
2639 struct symbol_cache_entry **asym_ptr_ptr;
2641 struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr;
2643 flagword flags = asym_ptr->flags;
2645 /* When gas creates relocations against local labels, it creates its
2646 own symbol for the section, but does put the symbol into the
2647 symbol chain, so udata is 0. When the linker is generating
2648 relocatable output, this section symbol may be for one of the
2649 input sections rather than the output section. */
2650 if (asym_ptr->udata == (PTR) 0
2651 && (flags & BSF_SECTION_SYM)
2652 && asym_ptr->section)
2656 if (asym_ptr->section->output_section != NULL)
2657 indx = asym_ptr->section->output_section->index;
2659 indx = asym_ptr->section->index;
2660 if (elf_section_syms (abfd)[indx])
2661 asym_ptr->udata = elf_section_syms (abfd)[indx]->udata;
2664 if (asym_ptr->udata)
2665 idx = ((Elf_Sym_Extra *) asym_ptr->udata)->elf_sym_num;
2675 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n",
2676 (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags));
2685 elf_slurp_symbol_table (abfd, symptrs, dynamic)
2687 asymbol **symptrs; /* Buffer for generated bfd symbols */
2690 Elf_Internal_Shdr *hdr;
2691 long symcount; /* Number of external ELF symbols */
2692 elf_symbol_type *sym; /* Pointer to current bfd symbol */
2693 elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
2694 Elf_Internal_Sym i_sym;
2695 Elf_External_Sym *x_symp = NULL;
2697 /* Read each raw ELF symbol, converting from external ELF form to
2698 internal ELF form, and then using the information to create a
2699 canonical bfd symbol table entry.
2701 Note that we allocate the initial bfd canonical symbol buffer
2702 based on a one-to-one mapping of the ELF symbols to canonical
2703 symbols. We actually use all the ELF symbols, so there will be no
2704 space left over at the end. When we have all the symbols, we
2705 build the caller's pointer vector. */
2708 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2710 hdr = &elf_tdata (abfd)->symtab_hdr;
2711 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
2714 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
2717 sym = symbase = NULL;
2722 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
2725 symbase = ((elf_symbol_type *)
2726 bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type)));
2727 if (symbase == (elf_symbol_type *) NULL)
2729 bfd_set_error (bfd_error_no_memory);
2734 /* Temporarily allocate room for the raw ELF symbols. */
2735 x_symp = ((Elf_External_Sym *)
2736 malloc (symcount * sizeof (Elf_External_Sym)));
2737 if (x_symp == NULL && symcount != 0)
2739 bfd_set_error (bfd_error_no_memory);
2743 if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd)
2744 != symcount * sizeof (Elf_External_Sym))
2746 /* Skip first symbol, which is a null dummy. */
2747 for (i = 1; i < symcount; i++)
2749 elf_swap_symbol_in (abfd, x_symp + i, &i_sym);
2750 memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym));
2751 #ifdef ELF_KEEP_EXTSYM
2752 memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym));
2754 sym->symbol.the_bfd = abfd;
2756 sym->symbol.name = elf_string_from_elf_section (abfd, hdr->sh_link,
2759 sym->symbol.value = i_sym.st_value;
2761 if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERVE)
2763 sym->symbol.section = section_from_elf_index (abfd,
2765 if (sym->symbol.section == NULL)
2767 /* This symbol is in a section for which we did not
2768 create a BFD section. Just use bfd_abs_section,
2769 although it is wrong. FIXME. */
2770 sym->symbol.section = &bfd_abs_section;
2773 else if (i_sym.st_shndx == SHN_ABS)
2775 sym->symbol.section = &bfd_abs_section;
2777 else if (i_sym.st_shndx == SHN_COMMON)
2779 sym->symbol.section = &bfd_com_section;
2780 /* Elf puts the alignment into the `value' field, and
2781 the size into the `size' field. BFD wants to see the
2782 size in the value field, and doesn't care (at the
2783 moment) about the alignment. */
2784 sym->symbol.value = i_sym.st_size;
2786 else if (i_sym.st_shndx == SHN_UNDEF)
2788 sym->symbol.section = &bfd_und_section;
2791 sym->symbol.section = &bfd_abs_section;
2793 sym->symbol.value -= sym->symbol.section->vma;
2795 switch (ELF_ST_BIND (i_sym.st_info))
2798 sym->symbol.flags |= BSF_LOCAL;
2801 sym->symbol.flags |= BSF_GLOBAL;
2804 sym->symbol.flags |= BSF_WEAK;
2808 switch (ELF_ST_TYPE (i_sym.st_info))
2811 sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING;
2814 sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
2817 sym->symbol.flags |= BSF_FUNCTION;
2822 sym->symbol.flags |= BSF_DYNAMIC;
2824 /* Do some backend-specific processing on this symbol. */
2826 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
2827 if (ebd->elf_backend_symbol_processing)
2828 (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
2835 /* Do some backend-specific processing on this symbol table. */
2837 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
2838 if (ebd->elf_backend_symbol_table_processing)
2839 (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
2842 /* We rely on the zalloc to clear out the final symbol entry. */
2844 symcount = sym - symbase;
2846 /* Fill in the user's symbol pointer vector if needed. */
2854 *symptrs++ = &sym->symbol;
2857 *symptrs = 0; /* Final null pointer */
2869 /* Return the number of bytes required to hold the symtab vector.
2871 Note that we base it on the count plus 1, since we will null terminate
2872 the vector allocated based on this size. However, the ELF symbol table
2873 always has a dummy entry as symbol #0, so it ends up even. */
2876 elf_get_symtab_upper_bound (abfd)
2881 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
2883 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
2884 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
2890 elf_get_dynamic_symtab_upper_bound (abfd)
2895 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2897 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
2898 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
2904 This function return the number of bytes required to store the
2905 relocation information associated with section <<sect>>
2906 attached to bfd <<abfd>>
2910 elf_get_reloc_upper_bound (abfd, asect)
2914 if (asect->flags & SEC_RELOC)
2916 /* either rel or rela */
2917 return elf_section_data (asect)->rel_hdr.sh_size;
2924 elf_slurp_reloca_table (abfd, asect, symbols)
2929 Elf_External_Rela *native_relocs;
2930 arelent *reloc_cache;
2935 if (asect->relocation)
2937 if (asect->reloc_count == 0)
2939 if (asect->flags & SEC_CONSTRUCTOR)
2942 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
2944 native_relocs = (Elf_External_Rela *)
2945 bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rela));
2948 bfd_set_error (bfd_error_no_memory);
2951 if (bfd_read ((PTR) native_relocs,
2952 sizeof (Elf_External_Rela), asect->reloc_count, abfd)
2953 != sizeof (Elf_External_Rela) * asect->reloc_count)
2956 reloc_cache = (arelent *)
2957 bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
2961 bfd_set_error (bfd_error_no_memory);
2965 for (idx = 0; idx < asect->reloc_count; idx++)
2967 Elf_Internal_Rela dst;
2968 Elf_External_Rela *src;
2970 cache_ptr = reloc_cache + idx;
2971 src = native_relocs + idx;
2972 elf_swap_reloca_in (abfd, src, &dst);
2974 #ifdef RELOC_PROCESSING
2975 RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
2977 if (asect->flags & SEC_RELOC)
2979 /* relocatable, so the offset is off of the section */
2980 cache_ptr->address = dst.r_offset + asect->vma;
2984 /* non-relocatable, so the offset a virtual address */
2985 cache_ptr->address = dst.r_offset;
2988 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
2989 of zero points to the dummy symbol, which was not read into
2990 the symbol table SYMBOLS. */
2991 if (ELF_R_SYM (dst.r_info) == 0)
2992 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
2997 cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1;
2999 /* Translate any ELF section symbol into a BFD section
3001 s = *(cache_ptr->sym_ptr_ptr);
3002 if (s->flags & BSF_SECTION_SYM)
3004 cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr;
3005 s = *cache_ptr->sym_ptr_ptr;
3006 if (s->name == 0 || s->name[0] == 0)
3010 cache_ptr->addend = dst.r_addend;
3012 /* Fill in the cache_ptr->howto field from dst.r_type */
3014 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
3015 (*ebd->elf_info_to_howto) (abfd, cache_ptr, &dst);
3020 asect->relocation = reloc_cache;
3026 elf_debug_section (str, num, hdr)
3029 Elf_Internal_Shdr *hdr;
3031 fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num, str, (long) hdr);
3033 "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n",
3034 (long) hdr->sh_name,
3035 (long) hdr->sh_type,
3036 (long) hdr->sh_flags);
3038 "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n",
3039 (long) hdr->sh_addr,
3040 (long) hdr->sh_offset,
3041 (long) hdr->sh_size);
3043 "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n",
3044 (long) hdr->sh_link,
3045 (long) hdr->sh_info,
3046 (long) hdr->sh_addralign);
3047 fprintf (stderr, "sh_entsize = %ld\n",
3048 (long) hdr->sh_entsize);
3049 fprintf (stderr, "rawdata = 0x%.8lx\n", (long) hdr->rawdata);
3050 fprintf (stderr, "contents = 0x%.8lx\n", (long) hdr->contents);
3051 fprintf (stderr, "size = %ld\n", (long) hdr->size);
3056 elf_debug_file (ehdrp)
3057 Elf_Internal_Ehdr *ehdrp;
3059 fprintf (stderr, "e_entry = 0x%.8lx\n", (long) ehdrp->e_entry);
3060 fprintf (stderr, "e_phoff = %ld\n", (long) ehdrp->e_phoff);
3061 fprintf (stderr, "e_phnum = %ld\n", (long) ehdrp->e_phnum);
3062 fprintf (stderr, "e_phentsize = %ld\n", (long) ehdrp->e_phentsize);
3063 fprintf (stderr, "e_shoff = %ld\n", (long) ehdrp->e_shoff);
3064 fprintf (stderr, "e_shnum = %ld\n", (long) ehdrp->e_shnum);
3065 fprintf (stderr, "e_shentsize = %ld\n", (long) ehdrp->e_shentsize);
3070 elf_slurp_reloc_table (abfd, asect, symbols)
3075 Elf_External_Rel *native_relocs;
3076 arelent *reloc_cache;
3078 Elf_Internal_Shdr *data_hdr;
3080 unsigned long data_max;
3081 char buf[4]; /* FIXME -- might be elf64 */
3085 if (asect->relocation)
3087 if (asect->reloc_count == 0)
3089 if (asect->flags & SEC_CONSTRUCTOR)
3092 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
3094 native_relocs = (Elf_External_Rel *)
3095 bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rel));
3098 bfd_set_error (bfd_error_no_memory);
3101 if (bfd_read ((PTR) native_relocs,
3102 sizeof (Elf_External_Rel), asect->reloc_count, abfd)
3103 != sizeof (Elf_External_Rel) * asect->reloc_count)
3106 reloc_cache = (arelent *)
3107 bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
3111 bfd_set_error (bfd_error_no_memory);
3115 /* Get the offset of the start of the segment we are relocating to read in
3116 the implicit addend. */
3117 data_hdr = &elf_section_data (asect)->this_hdr;
3118 data_off = data_hdr->sh_offset;
3119 data_max = data_hdr->sh_size - sizeof (buf) + 1;
3122 elf_debug_section ("data section", -1, data_hdr);
3125 for (idx = 0; idx < asect->reloc_count; idx++)
3127 #ifdef RELOC_PROCESSING
3128 Elf_Internal_Rel dst;
3129 Elf_External_Rel *src;
3131 cache_ptr = reloc_cache + idx;
3132 src = native_relocs + idx;
3133 elf_swap_reloc_in (abfd, src, &dst);
3135 RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
3137 Elf_Internal_Rel dst;
3138 Elf_External_Rel *src;
3140 cache_ptr = reloc_cache + idx;
3141 src = native_relocs + idx;
3143 elf_swap_reloc_in (abfd, src, &dst);
3145 if (asect->flags & SEC_RELOC)
3147 /* relocatable, so the offset is off of the section */
3148 cache_ptr->address = dst.r_offset + asect->vma;
3152 /* non-relocatable, so the offset a virtual address */
3153 cache_ptr->address = dst.r_offset;
3156 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
3157 of zero points to the dummy symbol, which was not read into
3158 the symbol table SYMBOLS. */
3159 if (ELF_R_SYM (dst.r_info) == 0)
3160 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
3165 cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1;
3167 /* Translate any ELF section symbol into a BFD section
3169 s = *(cache_ptr->sym_ptr_ptr);
3170 if (s->flags & BSF_SECTION_SYM)
3172 cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr;
3173 s = *cache_ptr->sym_ptr_ptr;
3174 if (s->name == 0 || s->name[0] == 0)
3178 BFD_ASSERT (dst.r_offset <= data_max);
3179 cache_ptr->addend = 0;
3181 /* Fill in the cache_ptr->howto field from dst.r_type */
3183 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
3184 (*ebd->elf_info_to_howto_rel) (abfd, cache_ptr, &dst);
3189 asect->relocation = reloc_cache;
3194 elf_canonicalize_reloc (abfd, section, relptr, symbols)
3200 arelent *tblptr = section->relocation;
3201 unsigned int count = 0;
3202 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
3204 /* snarfed from coffcode.h */
3207 if (! elf_slurp_reloca_table (abfd, section, symbols))
3212 if (! elf_slurp_reloc_table (abfd, section, symbols))
3216 tblptr = section->relocation;
3218 for (; count++ < section->reloc_count;)
3219 *relptr++ = tblptr++;
3222 return section->reloc_count;
3226 elf_get_symtab (abfd, alocation)
3228 asymbol **alocation;
3230 long symcount = elf_slurp_symbol_table (abfd, alocation, false);
3233 bfd_get_symcount (abfd) = symcount;
3238 elf_canonicalize_dynamic_symtab (abfd, alocation)
3240 asymbol **alocation;
3242 return elf_slurp_symbol_table (abfd, alocation, true);
3246 elf_make_empty_symbol (abfd)
3249 elf_symbol_type *newsym;
3251 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
3254 bfd_set_error (bfd_error_no_memory);
3259 newsym->symbol.the_bfd = abfd;
3260 return &newsym->symbol;
3265 elf_get_symbol_info (ignore_abfd, symbol, ret)
3270 bfd_symbol_info (symbol, ret);
3274 elf_print_symbol (ignore_abfd, filep, symbol, how)
3278 bfd_print_symbol_type how;
3280 FILE *file = (FILE *) filep;
3283 case bfd_print_symbol_name:
3284 fprintf (file, "%s", symbol->name);
3286 case bfd_print_symbol_more:
3287 fprintf (file, "elf ");
3288 fprintf_vma (file, symbol->value);
3289 fprintf (file, " %lx", (long) symbol->flags);
3291 case bfd_print_symbol_all:
3293 CONST char *section_name;
3294 section_name = symbol->section ? symbol->section->name : "(*none*)";
3295 bfd_print_symbol_vandf ((PTR) file, symbol);
3296 fprintf (file, " %s\t%s",
3306 elf_get_lineno (ignore_abfd, symbol)
3310 fprintf (stderr, "elf_get_lineno unimplemented\n");
3317 elf_set_arch_mach (abfd, arch, machine)
3319 enum bfd_architecture arch;
3320 unsigned long machine;
3322 /* If this isn't the right architecture for this backend, and this
3323 isn't the generic backend, fail. */
3324 if (arch != get_elf_backend_data (abfd)->arch
3325 && arch != bfd_arch_unknown
3326 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
3329 return bfd_default_set_arch_mach (abfd, arch, machine);
3333 elf_find_nearest_line (abfd,
3344 CONST char **filename_ptr;
3345 CONST char **functionname_ptr;
3346 unsigned int *line_ptr;
3352 elf_sizeof_headers (abfd, reloc)
3358 ret = sizeof (Elf_External_Ehdr);
3360 ret += get_program_header_size (abfd);
3365 elf_set_section_contents (abfd, section, location, offset, count)
3370 bfd_size_type count;
3372 Elf_Internal_Shdr *hdr;
3374 if (! abfd->output_has_begun
3375 && ! elf_compute_section_file_positions (abfd,
3376 (struct bfd_link_info *) NULL))
3379 hdr = &elf_section_data (section)->this_hdr;
3381 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
3383 if (bfd_write (location, 1, count, abfd) != count)
3390 elf_no_info_to_howto (abfd, cache_ptr, dst)
3393 Elf_Internal_Rela *dst;
3395 fprintf (stderr, "elf RELA relocation support for target machine unimplemented\n");
3401 elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
3404 Elf_Internal_Rel *dst;
3406 fprintf (stderr, "elf REL relocation support for target machine unimplemented\n");
3412 /* Core file support */
3414 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
3415 #include <sys/procfs.h>
3417 #define bfd_prstatus(abfd, descdata, descsz, filepos) true
3418 #define bfd_fpregset(abfd, descdata, descsz, filepos) true
3419 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) true
3425 bfd_prstatus (abfd, descdata, descsz, filepos)
3432 prstatus_t *status = (prstatus_t *) 0;
3434 if (descsz == sizeof (prstatus_t))
3436 newsect = bfd_make_section (abfd, ".reg");
3437 if (newsect == NULL)
3439 newsect->_raw_size = sizeof (status->pr_reg);
3440 newsect->filepos = filepos + (long) &status->pr_reg;
3441 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3442 newsect->alignment_power = 2;
3443 if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
3445 memcpy (core_prstatus (abfd), descdata, descsz);
3451 /* Stash a copy of the prpsinfo structure away for future use. */
3454 bfd_prpsinfo (abfd, descdata, descsz, filepos)
3460 if (descsz == sizeof (prpsinfo_t))
3462 if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) == NULL)
3464 bfd_set_error (bfd_error_no_memory);
3467 memcpy (core_prpsinfo (abfd), descdata, descsz);
3473 bfd_fpregset (abfd, descdata, descsz, filepos)
3481 newsect = bfd_make_section (abfd, ".reg2");
3482 if (newsect == NULL)
3484 newsect->_raw_size = descsz;
3485 newsect->filepos = filepos;
3486 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3487 newsect->alignment_power = 2;
3491 #endif /* HAVE_PROCFS */
3493 /* Return a pointer to the args (including the command name) that were
3494 seen by the program that generated the core dump. Note that for
3495 some reason, a spurious space is tacked onto the end of the args
3496 in some (at least one anyway) implementations, so strip it off if
3500 elf_core_file_failing_command (abfd)
3504 if (core_prpsinfo (abfd))
3506 prpsinfo_t *p = core_prpsinfo (abfd);
3507 char *scan = p->pr_psargs;
3512 if ((scan > p->pr_psargs) && (*scan == ' '))
3516 return p->pr_psargs;
3522 /* Return the number of the signal that caused the core dump. Presumably,
3523 since we have a core file, we got a signal of some kind, so don't bother
3524 checking the other process status fields, just return the signal number.
3528 elf_core_file_failing_signal (abfd)
3532 if (core_prstatus (abfd))
3534 return ((prstatus_t *) (core_prstatus (abfd)))->pr_cursig;
3540 /* Check to see if the core file could reasonably be expected to have
3541 come for the current executable file. Note that by default we return
3542 true unless we find something that indicates that there might be a
3547 elf_core_file_matches_executable_p (core_bfd, exec_bfd)
3556 /* First, xvecs must match since both are ELF files for the same target. */
3558 if (core_bfd->xvec != exec_bfd->xvec)
3560 bfd_set_error (bfd_error_system_call);
3566 /* If no prpsinfo, just return true. Otherwise, grab the last component
3567 of the exec'd pathname from the prpsinfo. */
3569 if (core_prpsinfo (core_bfd))
3571 corename = (((struct prpsinfo *) core_prpsinfo (core_bfd))->pr_fname);
3578 /* Find the last component of the executable pathname. */
3580 if ((execname = strrchr (exec_bfd->filename, '/')) != NULL)
3586 execname = (char *) exec_bfd->filename;
3589 /* See if they match */
3591 return strcmp (execname, corename) ? false : true;
3597 #endif /* HAVE_PROCFS */
3600 /* ELF core files contain a segment of type PT_NOTE, that holds much of
3601 the information that would normally be available from the /proc interface
3602 for the process, at the time the process dumped core. Currently this
3603 includes copies of the prstatus, prpsinfo, and fpregset structures.
3605 Since these structures are potentially machine dependent in size and
3606 ordering, bfd provides two levels of support for them. The first level,
3607 available on all machines since it does not require that the host
3608 have /proc support or the relevant include files, is to create a bfd
3609 section for each of the prstatus, prpsinfo, and fpregset structures,
3610 without any interpretation of their contents. With just this support,
3611 the bfd client will have to interpret the structures itself. Even with
3612 /proc support, it might want these full structures for it's own reasons.
3614 In the second level of support, where HAVE_PROCFS is defined, bfd will
3615 pick apart the structures to gather some additional information that
3616 clients may want, such as the general register set, the name of the
3617 exec'ed file and its arguments, the signal (if any) that caused the
3623 elf_corefile_note (abfd, hdr)
3625 Elf_Internal_Phdr *hdr;
3627 Elf_External_Note *x_note_p; /* Elf note, external form */
3628 Elf_Internal_Note i_note; /* Elf note, internal form */
3629 char *buf = NULL; /* Entire note segment contents */
3630 char *namedata; /* Name portion of the note */
3631 char *descdata; /* Descriptor portion of the note */
3632 char *sectname; /* Name to use for new section */
3633 long filepos; /* File offset to descriptor data */
3636 if (hdr->p_filesz > 0
3637 && (buf = (char *) malloc (hdr->p_filesz)) != NULL
3638 && bfd_seek (abfd, hdr->p_offset, SEEK_SET) != -1
3639 && bfd_read ((PTR) buf, hdr->p_filesz, 1, abfd) == hdr->p_filesz)
3641 x_note_p = (Elf_External_Note *) buf;
3642 while ((char *) x_note_p < (buf + hdr->p_filesz))
3644 i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->namesz);
3645 i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->descsz);
3646 i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->type);
3647 namedata = x_note_p->name;
3648 descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
3649 filepos = hdr->p_offset + (descdata - buf);
3650 switch (i_note.type)
3653 /* process descdata as prstatus info */
3654 if (! bfd_prstatus (abfd, descdata, i_note.descsz, filepos))
3656 sectname = ".prstatus";
3659 /* process descdata as fpregset info */
3660 if (! bfd_fpregset (abfd, descdata, i_note.descsz, filepos))
3662 sectname = ".fpregset";
3665 /* process descdata as prpsinfo */
3666 if (! bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos))
3668 sectname = ".prpsinfo";
3671 /* Unknown descriptor, just ignore it. */
3675 if (sectname != NULL)
3677 newsect = bfd_make_section (abfd, sectname);
3678 if (newsect == NULL)
3680 newsect->_raw_size = i_note.descsz;
3681 newsect->filepos = filepos;
3682 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3683 newsect->alignment_power = 2;
3685 x_note_p = (Elf_External_Note *)
3686 (descdata + BFD_ALIGN (i_note.descsz, 4));
3693 else if (hdr->p_filesz > 0)
3695 bfd_set_error (bfd_error_no_memory);
3702 /* Core files are simply standard ELF formatted files that partition
3703 the file using the execution view of the file (program header table)
3704 rather than the linking view. In fact, there is no section header
3705 table in a core file.
3707 The process status information (including the contents of the general
3708 register set) and the floating point register set are stored in a
3709 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
3710 that allow standard bfd access to the general registers (.reg) and the
3711 floating point registers (.reg2).
3716 elf_core_file_p (abfd)
3719 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
3720 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3721 Elf_External_Phdr x_phdr; /* Program header table entry, external form */
3722 Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */
3723 unsigned int phindex;
3724 struct elf_backend_data *ebd;
3726 /* Read in the ELF header in external format. */
3728 if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
3730 if (bfd_get_error () != bfd_error_system_call)
3731 bfd_set_error (bfd_error_wrong_format);
3735 /* Now check to see if we have a valid ELF file, and one that BFD can
3736 make use of. The magic number must match, the address size ('class')
3737 and byte-swapping must match our XVEC entry, and it must have a
3738 program header table (FIXME: See comments re segments at top of this
3741 if (elf_file_p (&x_ehdr) == false)
3744 bfd_set_error (bfd_error_wrong_format);
3748 /* FIXME, Check EI_VERSION here ! */
3752 int desired_address_size = ELFCLASS32;
3755 int desired_address_size = ELFCLASS64;
3758 if (x_ehdr.e_ident[EI_CLASS] != desired_address_size)
3762 /* Switch xvec to match the specified byte order. */
3763 switch (x_ehdr.e_ident[EI_DATA])
3765 case ELFDATA2MSB: /* Big-endian */
3766 if (abfd->xvec->byteorder_big_p == false)
3769 case ELFDATA2LSB: /* Little-endian */
3770 if (abfd->xvec->byteorder_big_p == true)
3773 case ELFDATANONE: /* No data encoding specified */
3774 default: /* Unknown data encoding specified */
3778 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
3779 the tdata pointer in the bfd. */
3782 (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
3783 if (elf_tdata (abfd) == NULL)
3785 bfd_set_error (bfd_error_no_memory);
3789 /* FIXME, `wrong' returns from this point onward, leak memory. */
3791 /* Now that we know the byte order, swap in the rest of the header */
3792 i_ehdrp = elf_elfheader (abfd);
3793 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
3795 elf_debug_file (i_ehdrp);
3798 ebd = get_elf_backend_data (abfd);
3800 /* Check that the ELF e_machine field matches what this particular
3801 BFD format expects. */
3802 if (ebd->elf_machine_code != i_ehdrp->e_machine)
3804 bfd_target **target_ptr;
3806 if (ebd->elf_machine_code != EM_NONE)
3809 /* This is the generic ELF target. Let it match any ELF target
3810 for which we do not have a specific backend. */
3811 for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
3813 struct elf_backend_data *back;
3815 if ((*target_ptr)->flavour != bfd_target_elf_flavour)
3817 back = (struct elf_backend_data *) (*target_ptr)->backend_data;
3818 if (back->elf_machine_code == i_ehdrp->e_machine)
3820 /* target_ptr is an ELF backend which matches this
3821 object file, so reject the generic ELF target. */
3827 /* If there is no program header, or the type is not a core file, then
3829 if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
3832 /* Allocate space for a copy of the program header table in
3833 internal form, seek to the program header table in the file,
3834 read it in, and convert it to internal form. As a simple sanity
3835 check, verify that the what BFD thinks is the size of each program
3836 header table entry actually matches the size recorded in the file. */
3838 if (i_ehdrp->e_phentsize != sizeof (x_phdr))
3840 i_phdrp = (Elf_Internal_Phdr *)
3841 bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum);
3844 bfd_set_error (bfd_error_no_memory);
3847 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1)
3849 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
3851 if (bfd_read ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd)
3854 elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
3857 /* Once all of the program headers have been read and converted, we
3858 can start processing them. */
3860 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
3862 bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
3863 if ((i_phdrp + phindex)->p_type == PT_NOTE)
3865 if (! elf_corefile_note (abfd, i_phdrp + phindex))
3870 /* Remember the entry point specified in the ELF file header. */
3872 bfd_get_start_address (abfd) = i_ehdrp->e_entry;
3877 /* ELF linker code. */
3879 static boolean elf_link_add_object_symbols
3880 PARAMS ((bfd *, struct bfd_link_info *));
3881 static boolean elf_link_add_archive_symbols
3882 PARAMS ((bfd *, struct bfd_link_info *));
3883 static INLINE boolean elf_link_record_dynamic_symbol
3884 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
3885 static boolean elf_link_create_dynamic_sections
3886 PARAMS ((bfd *, struct bfd_link_info *));
3887 static boolean elf_adjust_dynamic_symbol
3888 PARAMS ((struct elf_link_hash_entry *, PTR));
3890 /* Given an ELF BFD, add symbols to the global hash table as
3894 elf_bfd_link_add_symbols (abfd, info)
3896 struct bfd_link_info *info;
3898 switch (bfd_get_format (abfd))
3901 return elf_link_add_object_symbols (abfd, info);
3903 return elf_link_add_archive_symbols (abfd, info);
3905 bfd_set_error (bfd_error_wrong_format);
3910 /* Add symbols from an ELF archive file to the linker hash table. We
3911 don't use _bfd_generic_link_add_archive_symbols because of a
3912 problem which arises on UnixWare. The UnixWare libc.so is an
3913 archive which includes an entry libc.so.1 which defines a bunch of
3914 symbols. The libc.so archive also includes a number of other
3915 object files, which also define symbols, some of which are the same
3916 as those defined in libc.so.1. Correct linking requires that we
3917 consider each object file in turn, and include it if it defines any
3918 symbols we need. _bfd_generic_link_add_archive_symbols does not do
3919 this; it looks through the list of undefined symbols, and includes
3920 any object file which defines them. When this algorithm is used on
3921 UnixWare, it winds up pulling in libc.so.1 early and defining a
3922 bunch of symbols. This means that some of the other objects in the
3923 archive are not included in the link, which is incorrect since they
3924 precede libc.so.1 in the archive.
3926 Fortunately, ELF archive handling is simpler than that done by
3927 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
3928 oddities. In ELF, if we find a symbol in the archive map, and the
3929 symbol is currently undefined, we know that we must pull in that
3932 Unfortunately, we do have to make multiple passes over the symbol
3933 table until nothing further is resolved. */
3936 elf_link_add_archive_symbols (abfd, info)
3938 struct bfd_link_info *info;
3941 boolean *defined = NULL;
3942 boolean *included = NULL;
3946 if (! bfd_has_map (abfd))
3948 bfd_set_error (bfd_error_no_symbols);
3952 /* Keep track of all symbols we know to be already defined, and all
3953 files we know to be already included. This is to speed up the
3954 second and subsequent passes. */
3955 c = bfd_ardata (abfd)->symdef_count;
3958 defined = (boolean *) malloc (c * sizeof (boolean));
3959 included = (boolean *) malloc (c * sizeof (boolean));
3960 if (defined == (boolean *) NULL || included == (boolean *) NULL)
3962 bfd_set_error (bfd_error_no_memory);
3965 memset (defined, 0, c * sizeof (boolean));
3966 memset (included, 0, c * sizeof (boolean));
3968 symdefs = bfd_ardata (abfd)->symdefs;
3981 symdefend = symdef + c;
3982 for (i = 0; symdef < symdefend; symdef++, i++)
3984 struct elf_link_hash_entry *h;
3986 struct bfd_link_hash_entry *undefs_tail;
3989 if (defined[i] || included[i])
3991 if (symdef->file_offset == last)
3997 h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
3998 false, false, false);
3999 if (h == (struct elf_link_hash_entry *) NULL)
4001 if (h->root.type != bfd_link_hash_undefined)
4007 /* We need to include this archive member. */
4009 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4010 if (element == (bfd *) NULL)
4013 if (! bfd_check_format (element, bfd_object))
4016 /* Doublecheck that we have not included this object
4017 already--it should be impossible, but there may be
4018 something wrong with the archive. */
4019 if (element->archive_pass != 0)
4021 bfd_set_error (bfd_error_bad_value);
4024 element->archive_pass = 1;
4026 undefs_tail = info->hash->undefs_tail;
4028 if (! (*info->callbacks->add_archive_element) (info, element,
4031 if (! elf_link_add_object_symbols (element, info))
4034 /* If there are any new undefined symbols, we need to make
4035 another pass through the archive in order to see whether
4036 they can be defined. FIXME: This isn't perfect, because
4037 common symbols wind up on undefs_tail and because an
4038 undefined symbol which is defined later on in this pass
4039 does not require another pass. This isn't a bug, but it
4040 does make the code less efficient than it could be. */
4041 if (undefs_tail != info->hash->undefs_tail)
4044 /* Look backward to mark all symbols from this object file
4045 which we have already seen in this pass. */
4049 included[mark] = true;
4054 while (symdefs[mark].file_offset == symdef->file_offset);
4056 /* We mark subsequent symbols from this object file as we go
4057 on through the loop. */
4058 last = symdef->file_offset;
4069 if (defined != (boolean *) NULL)
4071 if (included != (boolean *) NULL)
4076 /* Record a new dynamic symbol. We record the dynamic symbols as we
4077 read the input files, since we need to have a list of all of them
4078 before we can determine the final sizes of the output sections. */
4080 static INLINE boolean
4081 elf_link_record_dynamic_symbol (info, h)
4082 struct bfd_link_info *info;
4083 struct elf_link_hash_entry *h;
4085 if (h->dynindx == -1)
4087 h->dynindx = elf_hash_table (info)->dynsymcount;
4088 ++elf_hash_table (info)->dynsymcount;
4089 h->dynstr_index = bfd_add_to_strtab (elf_hash_table (info)->dynobj,
4090 elf_hash_table (info)->dynstr,
4091 h->root.root.string);
4092 if (h->dynstr_index == (unsigned long) -1)
4099 /* Add symbols from an ELF object file to the linker hash table. */
4102 elf_link_add_object_symbols (abfd, info)
4104 struct bfd_link_info *info;
4106 boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
4107 const Elf_Internal_Sym *,
4108 const char **, flagword *,
4109 asection **, bfd_vma *));
4111 Elf_Internal_Shdr *hdr;
4114 Elf_External_Sym *buf = NULL;
4115 struct elf_link_hash_entry **sym_hash;
4117 Elf_External_Dyn *dynbuf = NULL;
4118 struct elf_link_hash_entry *weaks;
4119 Elf_External_Sym *esym;
4120 Elf_External_Sym *esymend;
4122 add_symbol_hook = get_elf_backend_data (abfd)->elf_add_symbol_hook;
4123 collect = get_elf_backend_data (abfd)->collect;
4125 hdr = &elf_tdata (abfd)->symtab_hdr;
4126 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
4128 /* The sh_info field of the symtab header tells us where the
4129 external symbols start. We don't care about the local symbols at
4131 extsymcount = symcount - hdr->sh_info;
4133 buf = (Elf_External_Sym *) malloc (extsymcount * sizeof (Elf_External_Sym));
4134 if (buf == NULL && extsymcount != 0)
4136 bfd_set_error (bfd_error_no_memory);
4140 /* We store a pointer to the hash table entry for each external
4142 sym_hash = ((struct elf_link_hash_entry **)
4144 extsymcount * sizeof (struct elf_link_hash_entry *)));
4145 if (sym_hash == NULL)
4147 bfd_set_error (bfd_error_no_memory);
4150 elf_sym_hashes (abfd) = sym_hash;
4152 if (elf_elfheader (abfd)->e_type != ET_DYN)
4158 unsigned long strindex;
4162 /* You can't use -r against a dynamic object. There's no hope
4163 of using a dynamic object which does not exactly match the
4164 format of the output file. */
4165 if (info->relocateable
4166 || info->hash->creator != abfd->xvec)
4168 bfd_set_error (bfd_error_invalid_operation);
4172 /* Find the name to use in a DT_NEEDED entry that refers to this
4173 object. If the object has a DT_SONAME entry, we use it.
4174 Otherwise, we use the file name. */
4175 name = bfd_get_filename (abfd);
4176 s = bfd_get_section_by_name (abfd, ".dynamic");
4179 Elf_External_Dyn *extdyn;
4180 Elf_External_Dyn *extdynend;
4182 dynbuf = (Elf_External_Dyn *) malloc (s->_raw_size);
4185 bfd_set_error (bfd_error_no_memory);
4189 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
4190 (file_ptr) 0, s->_raw_size))
4194 extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
4195 for (; extdyn < extdynend; extdyn++)
4197 Elf_Internal_Dyn dyn;
4199 elf_swap_dyn_in (abfd, extdyn, &dyn);
4200 if (dyn.d_tag == DT_SONAME)
4205 elfsec = elf_section_from_bfd_section (abfd, s);
4208 link = elf_elfsections (abfd)[elfsec]->sh_link;
4209 name = elf_string_from_elf_section (abfd, link,
4222 /* We do not want to include any of the sections in a dynamic
4223 object in the output file. We hack by simply clobbering the
4224 list of sections in the BFD. This could be handled more
4225 cleanly by, say, a new section flag; the existing
4226 SEC_NEVER_LOAD flag is not the one we want, because that one
4227 still implies that the section takes up space in the output
4229 abfd->sections = NULL;
4231 /* If this is the first dynamic object found in the link, create
4232 the special sections required for dynamic linking. We need
4233 to put them somewhere, and attaching them to the first
4234 dynamic object is as good place as any. */
4235 if (elf_hash_table (info)->dynobj == NULL)
4237 if (! elf_link_create_dynamic_sections (abfd, info))
4239 elf_hash_table (info)->dynobj = abfd;
4242 /* Add a DT_NEEDED entry for this dynamic object. */
4243 strindex = bfd_add_to_strtab (abfd,
4244 elf_hash_table (info)->dynstr,
4247 if (strindex == (unsigned long) -1)
4249 if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
4254 hdr->sh_offset + hdr->sh_info * sizeof (Elf_External_Sym),
4256 || (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd)
4257 != extsymcount * sizeof (Elf_External_Sym)))
4262 esymend = buf + extsymcount;
4263 for (esym = buf; esym < esymend; esym++, sym_hash++)
4265 Elf_Internal_Sym sym;
4271 struct elf_link_hash_entry *h = NULL;
4274 elf_swap_symbol_in (abfd, esym, &sym);
4276 flags = BSF_NO_FLAGS;
4278 value = sym.st_value;
4281 bind = ELF_ST_BIND (sym.st_info);
4282 if (bind == STB_LOCAL)
4284 /* This should be impossible, since ELF requires that all
4285 global symbols follow all local symbols, and that sh_info
4286 point to the first global symbol. */
4287 bfd_set_error (bfd_error_bad_value);
4290 else if (bind == STB_GLOBAL)
4292 else if (bind == STB_WEAK)
4296 /* Leave it up to the processor backend. */
4299 if (sym.st_shndx == SHN_UNDEF)
4300 sec = &bfd_und_section;
4301 else if (sym.st_shndx > 0 && sym.st_shndx < SHN_LORESERVE)
4303 sec = section_from_elf_index (abfd, sym.st_shndx);
4308 else if (sym.st_shndx == SHN_ABS)
4309 sec = &bfd_abs_section;
4310 else if (sym.st_shndx == SHN_COMMON)
4312 sec = &bfd_com_section;
4313 /* What ELF calls the size we call the value. What ELF
4314 calls the value we call the alignment. */
4315 value = sym.st_size;
4319 /* Leave it up to the processor backend. */
4322 name = elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
4323 if (name == (const char *) NULL)
4326 if (add_symbol_hook)
4328 if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
4332 /* The hook function sets the name to NULL if this symbol
4333 should be skipped for some reason. */
4334 if (name == (const char *) NULL)
4338 /* Sanity check that all possibilities were handled. */
4339 if (flags == BSF_NO_FLAGS || sec == (asection *) NULL)
4341 bfd_set_error (bfd_error_bad_value);
4345 if (sec == &bfd_und_section
4346 || bfd_is_com_section (sec))
4351 if (info->hash->creator->flavour == bfd_target_elf_flavour)
4353 /* We need to look up the symbol now in order to get some of
4354 the dynamic object handling right. We pass the hash
4355 table entry in to _bfd_generic_link_add_one_symbol so
4356 that it does not have to look it up again. */
4357 h = elf_link_hash_lookup (elf_hash_table (info), name,
4358 true, false, false);
4363 /* If we are looking at a dynamic object, and this is a
4364 definition, we need to see if it has already been defined
4365 by some other object. If it has, we want to use the
4366 existing definition, and we do not want to report a
4367 multiple symbol definition error; we do this by
4368 clobbering sec to be bfd_und_section. */
4369 if (dynamic && definition)
4371 if (h->root.type == bfd_link_hash_defined)
4372 sec = &bfd_und_section;
4375 /* Similarly, if we are not looking at a dynamic object, and
4376 we have a definition, we want to override any definition
4377 we may have from a dynamic object. Symbols from regular
4378 files always take precedence over symbols from dynamic
4379 objects, even if they are defined after the dynamic
4380 object in the link. */
4383 && h->root.type == bfd_link_hash_defined
4384 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4385 && (bfd_get_flavour (h->root.u.def.section->owner)
4386 == bfd_target_elf_flavour)
4387 && (elf_elfheader (h->root.u.def.section->owner)->e_type
4390 /* Change the hash table entry to undefined, and let
4391 _bfd_generic_link_add_one_symbol do the right thing
4392 with the new definition. */
4393 h->root.type = bfd_link_hash_undefined;
4394 h->root.u.undef.abfd = h->root.u.def.section->owner;
4398 if (! (_bfd_generic_link_add_one_symbol
4399 (info, abfd, name, flags, sec, value, (const char *) NULL,
4400 false, collect, (struct bfd_link_hash_entry **) sym_hash)))
4405 && (flags & BSF_WEAK) != 0
4406 && ELF_ST_TYPE (sym.st_info) != STT_FUNC
4407 && (*sym_hash)->weakdef == NULL)
4409 /* Keep a list of all weak defined non function symbols from
4410 a dynamic object, using the weakdef field. Later in this
4411 function we will set the weakdef field to the correct
4412 value. We only put non-function symbols from dynamic
4413 objects on this list, because that happens to be the only
4414 time we need to know the normal symbol corresponding to a
4415 weak symbol, and the information is time consuming to
4416 figure out. If the weakdef field is not already NULL,
4417 then this symbol was already defined by some previous
4418 dynamic object, and we will be using that previous
4419 definition anyhow. */
4421 (*sym_hash)->weakdef = weaks;
4425 if (info->hash->creator->flavour == bfd_target_elf_flavour)
4431 /* Remember the symbol size, type and alignment. */
4432 if (sym.st_size != 0)
4434 /* FIXME: We should probably somehow give a warning if
4435 the symbol size changes. */
4436 h->size = sym.st_size;
4438 if (sym.st_shndx == SHN_COMMON
4439 && sym.st_value > h->align)
4440 h->align = sym.st_value;
4441 if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE)
4443 /* FIXME: We should probably somehow give a warning if
4444 the symbol type changes. */
4445 h->type = ELF_ST_TYPE (sym.st_info);
4448 /* Set a flag in the hash table entry indicating the type of
4449 reference or definition we just found. Keep a count of
4450 the number of dynamic symbols we find. A dynamic symbol
4451 is one which is referenced or defined by both a regular
4452 object and a shared object, or one which is referenced or
4453 defined by more than one shared object. */
4454 old_flags = h->elf_link_hash_flags;
4459 new_flag = ELF_LINK_HASH_REF_REGULAR;
4461 new_flag = ELF_LINK_HASH_DEF_REGULAR;
4462 if ((old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
4463 | ELF_LINK_HASH_REF_DYNAMIC)) != 0)
4469 new_flag = ELF_LINK_HASH_REF_DYNAMIC;
4471 new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
4472 if ((old_flags & new_flag) != 0)
4475 new_flag = ELF_LINK_HASH_REF_DYNAMIC_MULTIPLE;
4477 new_flag = ELF_LINK_HASH_DEF_DYNAMIC_MULTIPLE;
4482 if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
4483 | ELF_LINK_HASH_REF_REGULAR)) != 0)
4488 h->elf_link_hash_flags |= new_flag;
4489 if (dynsym && h->dynindx == -1)
4491 if (! elf_link_record_dynamic_symbol (info, h))
4497 /* Now set the weakdefs field correctly for all the weak defined
4498 symbols we found. The only way to do this is to search all the
4499 symbols. Since we only need the information for non functions in
4500 dynamic objects, that's the only time we actually put anything on
4501 the list WEAKS. We need this information so that if a regular
4502 object refers to a symbol defined weakly in a dynamic object, the
4503 real symbol in the dynamic object is also put in the dynamic
4504 symbols; we also must arrange for both symbols to point to the
4505 same memory location. We could handle the general case of symbol
4506 aliasing, but a general symbol alias can only be generated in
4507 assembler code, handling it correctly would be very time
4508 consuming, and other ELF linkers don't handle general aliasing
4510 while (weaks != NULL)
4512 struct elf_link_hash_entry *hlook;
4515 struct elf_link_hash_entry **hpp;
4516 struct elf_link_hash_entry **hppend;
4519 weaks = hlook->weakdef;
4520 hlook->weakdef = NULL;
4522 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined);
4523 slook = hlook->root.u.def.section;
4524 vlook = hlook->root.u.def.value;
4526 hpp = elf_sym_hashes (abfd);
4527 hppend = hpp + extsymcount;
4528 for (; hpp < hppend; hpp++)
4530 struct elf_link_hash_entry *h;
4534 && h->root.type == bfd_link_hash_defined
4535 && h->root.u.def.section == slook
4536 && h->root.u.def.value == vlook)
4540 /* If the weak definition is in the list of dynamic
4541 symbols, make sure the real definition is put there
4543 if (hlook->dynindx != -1
4544 && h->dynindx == -1)
4546 if (! elf_link_record_dynamic_symbol (info, h))
4568 /* Create some sections which will be filled in with dynamic linking
4569 information. The ABFD argument is an input file which is a dynamic
4570 object. The dynamic sections take up virtual memory space when the
4571 final executable is run, so we need to create them before addresses
4572 are assigned to the output sections. We work out the actual
4573 contents and size of these sections later. */
4576 elf_link_create_dynamic_sections (abfd, info)
4578 struct bfd_link_info *info;
4581 register asection *s;
4582 struct elf_link_hash_entry *h;
4583 struct elf_backend_data *bed;
4585 /* Note that we set the SEC_IN_MEMORY flag for all of these
4587 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
4589 s = bfd_make_section (abfd, ".interp");
4591 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
4594 s = bfd_make_section (abfd, ".dynamic");
4596 || ! bfd_set_section_flags (abfd, s, flags)
4597 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
4600 /* The special symbol _DYNAMIC is always set to the start of the
4601 .dynamic section. This call occurs before we have processed the
4602 symbols for any dynamic object, so we don't have to worry about
4603 overriding a dynamic definition. We could set _DYNAMIC in a
4604 linker script, but we only want to define it if we are, in fact,
4605 creating a .dynamic section. We don't want to define it if there
4606 is no .dynamic section, since on some ELF platforms the start up
4607 code examines it to decide how to initialize the process. */
4609 if (! (_bfd_generic_link_add_one_symbol
4610 (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
4611 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
4612 (struct bfd_link_hash_entry **) &h)))
4614 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4616 s = bfd_make_section (abfd, ".dynsym");
4618 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
4619 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
4622 /* The first .dynsym symbol is a dummy. */
4623 elf_hash_table (info)->dynsymcount = 1;
4625 s = bfd_make_section (abfd, ".dynstr");
4627 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
4630 /* Create a strtab to hold the dynamic symbol names. */
4631 elf_hash_table (info)->dynstr = bfd_new_strtab (abfd);
4632 if (elf_hash_table (info)->dynstr == NULL)
4635 s = bfd_make_section (abfd, ".hash");
4637 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
4638 || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
4641 /* Let the backend create the rest of the sections. This lets the
4642 backend set the right flags. The backend will normally create
4643 the .got and .plt sections. */
4644 bed = get_elf_backend_data (abfd);
4645 return (*bed->elf_backend_create_dynamic_sections) (abfd, info);
4648 /* Add an entry to the .dynamic table. */
4651 elf_add_dynamic_entry (info, tag, val)
4652 struct bfd_link_info *info;
4656 Elf_Internal_Dyn dyn;
4660 bfd_byte *newcontents;
4662 dynobj = elf_hash_table (info)->dynobj;
4664 s = bfd_get_section_by_name (dynobj, ".dynamic");
4665 BFD_ASSERT (s != NULL);
4667 newsize = s->_raw_size + sizeof (Elf_External_Dyn);
4668 if (s->contents == NULL)
4669 newcontents = (bfd_byte *) malloc (newsize);
4671 newcontents = (bfd_byte *) realloc (s->contents, newsize);
4672 if (newcontents == NULL)
4674 bfd_set_error (bfd_error_no_memory);
4679 dyn.d_un.d_val = val;
4680 elf_swap_dyn_out (dynobj, &dyn,
4681 (Elf_External_Dyn *) (newcontents + s->_raw_size));
4683 s->_raw_size = newsize;
4684 s->contents = newcontents;
4689 /* Record an assignment to a symbol made by a linker script. We need
4690 this in case some dynamic object refers to this symbol. */
4694 NAME(bfd_elf,record_link_assignment) (output_bfd, info, name)
4696 struct bfd_link_info *info;
4699 struct elf_link_hash_entry *h;
4701 /* This is called after we have examined all the input objects. If
4702 the symbol does not exist, it merely means that no object refers
4703 to it, and we can just ignore it at this point. */
4704 h = elf_link_hash_lookup (elf_hash_table (info), name,
4705 false, false, false);
4709 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4711 if ((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
4712 | ELF_LINK_HASH_REF_DYNAMIC)) != 0
4713 && h->dynindx == -1)
4715 if (! elf_link_record_dynamic_symbol (info, h))
4718 /* If this is a weak defined symbol, and we know a corresponding
4719 real symbol from the same dynamic object, make sure the real
4720 symbol is also made into a dynamic symbol. */
4721 if (h->weakdef != NULL
4722 && h->weakdef->dynindx == -1)
4724 if (! elf_link_record_dynamic_symbol (info, h->weakdef))
4732 /* Array used to determine the number of hash table buckets to use
4733 based on the number of symbols there are. If there are fewer than
4734 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4735 fewer than 37 we use 17 buckets, and so forth. We never use more
4736 than 521 buckets. */
4738 static const size_t elf_buckets[] =
4740 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 0
4743 /* Set up the sizes and contents of the ELF dynamic sections. This is
4744 called by the ELF linker emulation before_allocation routine. We
4745 must set the sizes of the sections before the linker sets the
4746 addresses of the various sections. */
4749 NAME(bfd_elf,size_dynamic_sections) (output_bfd, info)
4751 struct bfd_link_info *info;
4756 Elf_Internal_Sym isym;
4759 struct elf_backend_data *bed;
4761 dynobj = elf_hash_table (info)->dynobj;
4762 dynsymcount = elf_hash_table (info)->dynsymcount;
4764 /* If there were no dynamic objects in the link, there is nothing to
4769 /* Set the size of the .dynsym and .hash sections. We counted the
4770 number of dynamic symbols in elf_link_add_object_symbols. We
4771 will build the contents of .dynsym and .hash when we build the
4772 final symbol table, because until then we do not know the correct
4773 value to give the symbols. We built the .dynstr section as we
4774 went along in elf_link_add_object_symbols. */
4775 s = bfd_get_section_by_name (dynobj, ".dynsym");
4776 BFD_ASSERT (s != NULL);
4777 s->_raw_size = dynsymcount * sizeof (Elf_External_Sym);
4778 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
4779 if (s->contents == NULL && s->_raw_size != 0)
4781 bfd_set_error (bfd_error_no_memory);
4785 /* The first entry in .dynsym is a dummy symbol. */
4792 elf_swap_symbol_out (output_bfd, &isym,
4793 (Elf_External_Sym *) s->contents);
4795 for (i = 0; elf_buckets[i] != 0; i++)
4797 bucketcount = elf_buckets[i];
4798 if (dynsymcount < elf_buckets[i + 1])
4802 s = bfd_get_section_by_name (dynobj, ".hash");
4803 BFD_ASSERT (s != NULL);
4804 s->_raw_size = (2 + bucketcount + dynsymcount) * (ARCH_SIZE / 8);
4805 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
4806 if (s->contents == NULL)
4808 bfd_set_error (bfd_error_no_memory);
4811 memset (s->contents, 0, s->_raw_size);
4813 put_word (output_bfd, bucketcount, s->contents);
4814 put_word (output_bfd, dynsymcount, s->contents + (ARCH_SIZE / 8));
4816 elf_hash_table (info)->bucketcount = bucketcount;
4818 s = bfd_get_section_by_name (dynobj, ".dynstr");
4819 BFD_ASSERT (s != NULL);
4820 s->_raw_size = elf_hash_table (info)->dynstr->length;
4821 s->contents = (unsigned char *) elf_hash_table (info)->dynstr->tab;
4823 /* Find all symbols which were defined in a dynamic object and make
4824 the backend pick a reasonable value for them. */
4825 elf_link_hash_traverse (elf_hash_table (info),
4826 elf_adjust_dynamic_symbol,
4829 /* Add some entries to the .dynamic section. We fill in some of the
4830 values later, in elf_bfd_final_link, but we must add the entries
4831 now so that we know the final size of the .dynamic section. */
4832 if (bfd_get_section_by_name (output_bfd, ".init") != NULL)
4834 if (! elf_add_dynamic_entry (info, DT_INIT, 0))
4837 if (bfd_get_section_by_name (output_bfd, ".fini") != NULL)
4839 if (! elf_add_dynamic_entry (info, DT_FINI, 0))
4842 if (! elf_add_dynamic_entry (info, DT_HASH, 0)
4843 || ! elf_add_dynamic_entry (info, DT_STRTAB, 0)
4844 || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0)
4845 || ! elf_add_dynamic_entry (info, DT_STRSZ,
4846 elf_hash_table (info)->dynstr->length)
4847 || ! elf_add_dynamic_entry (info, DT_SYMENT,
4848 sizeof (Elf_External_Sym)))
4851 /* The backend must work out the sizes of all the other dynamic
4853 bed = get_elf_backend_data (output_bfd);
4854 if (! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
4857 return elf_add_dynamic_entry (info, DT_NULL, 0);
4860 /* Make the backend pick a good value for a dynamic symbol. This is
4861 called via elf_link_hash_traverse, and also calls itself
4865 elf_adjust_dynamic_symbol (h, data)
4866 struct elf_link_hash_entry *h;
4869 struct bfd_link_info *info = (struct bfd_link_info *) data;
4871 struct elf_backend_data *bed;
4873 /* If this symbol is not defined by a dynamic object, or is not
4874 referenced by a regular object, ignore it. FIXME: Do we need to
4875 worry about symbols which are defined by one dynamic object and
4876 referenced by another one? */
4877 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4878 || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4879 || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4882 /* If we've already adjusted this symbol, don't do it again. This
4883 can happen via a recursive call. */
4884 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
4887 /* Don't look at this symbol again. Note that we must set this
4888 after checking the above conditions, because we may look at a
4889 symbol once, decide not to do anything, and then get called
4890 recursively later after REF_REGULAR is set below. */
4891 h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED;
4893 /* If this is a weak definition, and we know a real definition, and
4894 the real symbol is not itself defined by a regular object file,
4895 then get a good value for the real definition. We handle the
4896 real symbol first, for the convenience of the backend routine.
4898 Note that there is a confusing case here. If the real definition
4899 is defined by a regular object file, we don't get the real symbol
4900 from the dynamic object, but we do get the weak symbol. If the
4901 processor backend uses a COPY reloc, then if some routine in the
4902 dynamic object changes the real symbol, we will not see that
4903 change in the corresponding weak symbol. This is the way other
4904 ELF linkers work as well, and seems to be a result of the shared
4907 I will clarify this issue. Most SVR4 shared libraries define the
4908 variable _timezone and define timezone as a weak synonym. The
4909 tzset call changes _timezone. If you write
4910 extern int timezone;
4912 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
4913 you might expect that, since timezone is a synonym for _timezone,
4914 the same number will print both times. However, if the processor
4915 backend uses a COPY reloc, then actually timezone will be copied
4916 into your process image, and, since you define _timezone
4917 yourself, _timezone will not. Thus timezone and _timezone will
4918 wind up at different memory locations. The tzset call will set
4919 _timezone, leaving timezone unchanged. */
4921 if (h->weakdef != NULL)
4923 struct elf_link_hash_entry *weakdef;
4925 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
4926 weakdef = h->weakdef;
4927 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined);
4928 BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC);
4929 if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4930 || (weakdef->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
4932 /* This symbol is defined or referenced by a regular object
4933 file, so we will not do anything special. Clear weakdef
4934 for the convenience of the processor backend. */
4939 /* There is an implicit reference by a regular object file
4940 via the weak symbol. */
4941 weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
4942 if (! elf_adjust_dynamic_symbol (weakdef, (PTR) info))
4947 dynobj = elf_hash_table (info)->dynobj;
4948 bed = get_elf_backend_data (dynobj);
4949 if (! (*bed->elf_backend_adjust_dynamic_symbol) (info, h))
4951 /* FIXME: No way to return error. */
4958 /* Final phase of ELF linker. */
4960 /* A structure we use to avoid passing large numbers of arguments. */
4962 struct elf_final_link_info
4964 /* General link information. */
4965 struct bfd_link_info *info;
4968 /* Symbol string table. */
4969 struct strtab *symstrtab;
4970 /* .dynsym section. */
4971 asection *dynsym_sec;
4972 /* .hash section. */
4974 /* Buffer large enough to hold contents of any section. */
4976 /* Buffer large enough to hold external relocs of any section. */
4977 PTR external_relocs;
4978 /* Buffer large enough to hold internal relocs of any section. */
4979 Elf_Internal_Rela *internal_relocs;
4980 /* Buffer large enough to hold external local symbols of any input
4982 Elf_External_Sym *external_syms;
4983 /* Buffer large enough to hold internal local symbols of any input
4985 Elf_Internal_Sym *internal_syms;
4986 /* Array large enough to hold a symbol index for each local symbol
4987 of any input BFD. */
4989 /* Array large enough to hold a section pointer for each local
4990 symbol of any input BFD. */
4991 asection **sections;
4992 /* Buffer to hold swapped out symbols. */
4993 Elf_External_Sym *symbuf;
4994 /* Number of swapped out symbols in buffer. */
4995 size_t symbuf_count;
4996 /* Number of symbols which fit in symbuf. */
5000 static boolean elf_link_output_sym
5001 PARAMS ((struct elf_final_link_info *, const char *, Elf_Internal_Sym *));
5002 static boolean elf_link_flush_output_syms
5003 PARAMS ((struct elf_final_link_info *));
5004 static boolean elf_link_output_extsym
5005 PARAMS ((struct elf_link_hash_entry *, PTR));
5006 static boolean elf_link_input_bfd
5007 PARAMS ((struct elf_final_link_info *, bfd *));
5008 static boolean elf_reloc_link_order
5009 PARAMS ((bfd *, struct bfd_link_info *, asection *,
5010 struct bfd_link_order *));
5012 /* Do the final step of an ELF link. */
5015 elf_bfd_final_link (abfd, info)
5017 struct bfd_link_info *info;
5020 struct elf_final_link_info finfo;
5021 register asection *o;
5022 register struct bfd_link_order *p;
5024 size_t max_contents_size;
5025 size_t max_external_reloc_size;
5026 size_t max_internal_reloc_count;
5027 size_t max_sym_count;
5029 Elf_Internal_Sym elfsym;
5031 Elf_Internal_Shdr *symtab_hdr;
5032 Elf_Internal_Shdr *symstrtab_hdr;
5034 dynobj = elf_hash_table (info)->dynobj;
5037 finfo.output_bfd = abfd;
5038 finfo.symstrtab = bfd_new_strtab (abfd);
5039 if (finfo.symstrtab == NULL)
5043 finfo.dynsym_sec = NULL;
5044 finfo.hash_sec = NULL;
5048 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
5049 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
5050 if (finfo.dynsym_sec == NULL
5051 || finfo.hash_sec == NULL)
5054 finfo.contents = NULL;
5055 finfo.external_relocs = NULL;
5056 finfo.internal_relocs = NULL;
5057 finfo.external_syms = NULL;
5058 finfo.internal_syms = NULL;
5059 finfo.indices = NULL;
5060 finfo.sections = NULL;
5061 finfo.symbuf = NULL;
5062 finfo.symbuf_count = 0;
5064 /* Count up the number of relocations we will output for each output
5065 section, so that we know the sizes of the reloc sections. We
5066 also figure out some maximum sizes. */
5067 max_contents_size = 0;
5068 max_external_reloc_size = 0;
5069 max_internal_reloc_count = 0;
5071 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
5075 for (p = o->link_order_head; p != NULL; p = p->next)
5077 if (p->type == bfd_section_reloc_link_order
5078 || p->type == bfd_symbol_reloc_link_order)
5080 else if (p->type == bfd_indirect_link_order)
5084 sec = p->u.indirect.section;
5086 if (info->relocateable)
5087 o->reloc_count += sec->reloc_count;
5089 if (sec->_raw_size > max_contents_size)
5090 max_contents_size = sec->_raw_size;
5091 if (sec->_cooked_size > max_contents_size)
5092 max_contents_size = sec->_cooked_size;
5094 /* We are interested in just local symbols, not all
5096 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
5097 && (elf_tdata (sec->owner)->symtab_hdr.sh_info
5099 max_sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
5101 if ((sec->flags & SEC_RELOC) != 0)
5105 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
5106 if (ext_size > max_external_reloc_size)
5107 max_external_reloc_size = ext_size;
5108 if (sec->reloc_count > max_internal_reloc_count)
5109 max_internal_reloc_count = sec->reloc_count;
5114 if (o->reloc_count > 0)
5115 o->flags |= SEC_RELOC;
5118 /* Explicitly clear the SEC_RELOC flag. The linker tends to
5119 set it (this is probably a bug) and if it is set
5120 assign_section_numbers will create a reloc section. */
5121 o->flags &=~ SEC_RELOC;
5125 /* Figure out the file positions for everything but the symbol table
5126 and the relocs. We set symcount to force assign_section_numbers
5127 to create a symbol table. */
5128 abfd->symcount = info->strip == strip_all ? 0 : 1;
5129 BFD_ASSERT (! abfd->output_has_begun);
5130 if (! elf_compute_section_file_positions (abfd, info))
5133 /* That created the reloc sections. Set their sizes, and assign
5134 them file positions, and allocate some buffers. */
5135 for (o = abfd->sections; o != NULL; o = o->next)
5137 if ((o->flags & SEC_RELOC) != 0)
5139 Elf_Internal_Shdr *rel_hdr;
5140 register struct elf_link_hash_entry **p, **pend;
5142 rel_hdr = &elf_section_data (o)->rel_hdr;
5144 rel_hdr->sh_size = rel_hdr->sh_entsize * o->reloc_count;
5146 /* The contents field must last into write_object_contents,
5147 so we allocate it with bfd_alloc rather than malloc. */
5148 rel_hdr->contents = (PTR) bfd_alloc (abfd, rel_hdr->sh_size);
5149 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
5151 bfd_set_error (bfd_error_no_memory);
5155 p = ((struct elf_link_hash_entry **)
5156 malloc (o->reloc_count
5157 * sizeof (struct elf_link_hash_entry *)));
5158 if (p == NULL && o->reloc_count != 0)
5160 bfd_set_error (bfd_error_no_memory);
5163 elf_section_data (o)->rel_hashes = p;
5164 pend = p + o->reloc_count;
5165 for (; p < pend; p++)
5168 /* Use the reloc_count field as an index when outputting the
5174 assign_file_positions_for_relocs (abfd);
5176 /* We have now assigned file positions for all the sections except
5177 .symtab and .strtab. We start the .symtab section at the current
5178 file position, and write directly to it. We build the .strtab
5179 section in memory. When we add .dynsym support, we will build
5180 that in memory as well (.dynsym is smaller than .symtab). */
5182 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5183 /* sh_name is set in prep_headers. */
5184 symtab_hdr->sh_type = SHT_SYMTAB;
5185 symtab_hdr->sh_flags = 0;
5186 symtab_hdr->sh_addr = 0;
5187 symtab_hdr->sh_size = 0;
5188 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
5189 /* sh_link is set in assign_section_numbers. */
5190 /* sh_info is set below. */
5191 /* sh_offset is set just below. */
5192 symtab_hdr->sh_addralign = 4; /* FIXME: system dependent? */
5194 off = elf_tdata (abfd)->next_file_pos;
5195 off = assign_file_position_for_section (symtab_hdr, off, true);
5197 /* Note that at this point elf_tdata (abfd)->next_file_pos is
5198 incorrect. We do not yet know the size of the .symtab section.
5199 We correct next_file_pos below, after we do know the size. */
5201 /* Allocate a buffer to hold swapped out symbols. This is to avoid
5202 continuously seeking to the right position in the file. */
5203 if (! info->keep_memory || max_sym_count < 20)
5204 finfo.symbuf_size = 20;
5206 finfo.symbuf_size = max_sym_count;
5207 finfo.symbuf = ((Elf_External_Sym *)
5208 malloc (finfo.symbuf_size * sizeof (Elf_External_Sym)));
5209 if (finfo.symbuf == NULL)
5211 bfd_set_error (bfd_error_no_memory);
5215 /* Start writing out the symbol table. The first symbol is always a
5217 elfsym.st_value = 0;
5220 elfsym.st_other = 0;
5221 elfsym.st_shndx = SHN_UNDEF;
5222 if (! elf_link_output_sym (&finfo, (const char *) NULL, &elfsym))
5226 /* Some standard ELF linkers do this, but we don't because it causes
5227 bootstrap comparison failures. */
5228 /* Output a file symbol for the output file as the second symbol.
5229 We output this even if we are discarding local symbols, although
5230 I'm not sure if this is correct. */
5231 elfsym.st_value = 0;
5233 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5234 elfsym.st_other = 0;
5235 elfsym.st_shndx = SHN_ABS;
5236 if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd), &elfsym))
5240 /* Output a symbol for each section. We output these even if we are
5241 discarding local symbols, since they are used for relocs. These
5242 symbols have no names. We store the index of each one in the
5243 index field of the section, so that we can find it again when
5244 outputting relocs. */
5245 elfsym.st_value = 0;
5247 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5248 elfsym.st_other = 0;
5249 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
5251 o = section_from_elf_index (abfd, i);
5252 if (o != &bfd_abs_section)
5253 o->target_index = abfd->symcount;
5254 elfsym.st_shndx = i;
5255 if (! elf_link_output_sym (&finfo, (const char *) NULL, &elfsym))
5259 /* Allocate some memory to hold information read in from the input
5261 finfo.contents = (bfd_byte *) malloc (max_contents_size);
5262 finfo.external_relocs = (PTR) malloc (max_external_reloc_size);
5263 finfo.internal_relocs = ((Elf_Internal_Rela *)
5264 malloc (max_internal_reloc_count
5265 * sizeof (Elf_Internal_Rela)));
5266 finfo.external_syms = ((Elf_External_Sym *)
5267 malloc (max_sym_count * sizeof (Elf_External_Sym)));
5268 finfo.internal_syms = ((Elf_Internal_Sym *)
5269 malloc (max_sym_count * sizeof (Elf_Internal_Sym)));
5270 finfo.indices = (long *) malloc (max_sym_count * sizeof (long));
5271 finfo.sections = (asection **) malloc (max_sym_count * sizeof (asection *));
5272 if ((finfo.contents == NULL && max_contents_size != 0)
5273 || (finfo.external_relocs == NULL && max_external_reloc_size != 0)
5274 || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0)
5275 || (finfo.external_syms == NULL && max_sym_count != 0)
5276 || (finfo.internal_syms == NULL && max_sym_count != 0)
5277 || (finfo.indices == NULL && max_sym_count != 0)
5278 || (finfo.sections == NULL && max_sym_count != 0))
5280 bfd_set_error (bfd_error_no_memory);
5284 /* Since ELF permits relocations to be against local symbols, we
5285 must have the local symbols available when we do the relocations.
5286 Since we would rather only read the local symbols once, and we
5287 would rather not keep them in memory, we handle all the
5288 relocations for a single input file at the same time.
5290 Unfortunately, there is no way to know the total number of local
5291 symbols until we have seen all of them, and the local symbol
5292 indices precede the global symbol indices. This means that when
5293 we are generating relocateable output, and we see a reloc against
5294 a global symbol, we can not know the symbol index until we have
5295 finished examining all the local symbols to see which ones we are
5296 going to output. To deal with this, we keep the relocations in
5297 memory, and don't output them until the end of the link. This is
5298 an unfortunate waste of memory, but I don't see a good way around
5299 it. Fortunately, it only happens when performing a relocateable
5300 link, which is not the common case. FIXME: If keep_memory is set
5301 we could write the relocs out and then read them again; I don't
5302 know how bad the memory loss will be. */
5304 for (sub = info->input_bfds; sub != NULL; sub = sub->next)
5305 sub->output_has_begun = false;
5306 for (o = abfd->sections; o != NULL; o = o->next)
5308 for (p = o->link_order_head; p != NULL; p = p->next)
5310 if (p->type == bfd_indirect_link_order
5311 && (bfd_get_flavour (p->u.indirect.section->owner)
5312 == bfd_target_elf_flavour))
5314 sub = p->u.indirect.section->owner;
5315 if (! sub->output_has_begun)
5317 if (! elf_link_input_bfd (&finfo, sub))
5319 sub->output_has_begun = true;
5322 else if (p->type == bfd_section_reloc_link_order
5323 || p->type == bfd_symbol_reloc_link_order)
5325 if (! elf_reloc_link_order (abfd, info, o, p))
5330 if (! _bfd_default_link_order (abfd, info, o, p))
5336 /* That wrote out all the local symbols. Finish up the symbol table
5337 with the global symbols. */
5339 /* The sh_info field records the index of the first non local
5341 symtab_hdr->sh_info = abfd->symcount;
5343 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = 1;
5345 /* We get the global symbols from the hash table. */
5346 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
5349 /* Flush all symbols to the file. */
5350 if (! elf_link_flush_output_syms (&finfo))
5353 /* Now we know the size of the symtab section. */
5354 off += symtab_hdr->sh_size;
5356 /* Finish up the symbol string table (.strtab) section. */
5357 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5358 /* sh_name was set in prep_headers. */
5359 symstrtab_hdr->sh_type = SHT_STRTAB;
5360 symstrtab_hdr->sh_flags = 0;
5361 symstrtab_hdr->sh_addr = 0;
5362 symstrtab_hdr->sh_size = finfo.symstrtab->length;
5363 symstrtab_hdr->sh_entsize = 0;
5364 symstrtab_hdr->sh_link = 0;
5365 symstrtab_hdr->sh_info = 0;
5366 /* sh_offset is set just below. */
5367 symstrtab_hdr->sh_addralign = 1;
5368 symstrtab_hdr->contents = (PTR) finfo.symstrtab->tab;
5370 off = assign_file_position_for_section (symstrtab_hdr, off, true);
5371 elf_tdata (abfd)->next_file_pos = off;
5373 /* Adjust the relocs to have the correct symbol indices. */
5374 for (o = abfd->sections; o != NULL; o = o->next)
5376 struct elf_link_hash_entry **rel_hash;
5377 Elf_Internal_Shdr *rel_hdr;
5379 if ((o->flags & SEC_RELOC) == 0)
5382 rel_hash = elf_section_data (o)->rel_hashes;
5383 rel_hdr = &elf_section_data (o)->rel_hdr;
5384 for (i = 0; i < o->reloc_count; i++, rel_hash++)
5386 if (*rel_hash == NULL)
5389 BFD_ASSERT ((*rel_hash)->indx >= 0);
5391 if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
5393 Elf_External_Rel *erel;
5394 Elf_Internal_Rel irel;
5396 erel = (Elf_External_Rel *) rel_hdr->contents + i;
5397 elf_swap_reloc_in (abfd, erel, &irel);
5398 irel.r_info = ELF_R_INFO ((*rel_hash)->indx,
5399 ELF_R_TYPE (irel.r_info));
5400 elf_swap_reloc_out (abfd, &irel, erel);
5404 Elf_External_Rela *erela;
5405 Elf_Internal_Rela irela;
5407 BFD_ASSERT (rel_hdr->sh_entsize
5408 == sizeof (Elf_External_Rela));
5410 erela = (Elf_External_Rela *) rel_hdr->contents + i;
5411 elf_swap_reloca_in (abfd, erela, &irela);
5412 irela.r_info = ELF_R_INFO ((*rel_hash)->indx,
5413 ELF_R_TYPE (irela.r_info));
5414 elf_swap_reloca_out (abfd, &irela, erela);
5418 /* Set the reloc_count field to 0 to prevent write_relocs from
5419 trying to swap the relocs out itself. */
5423 /* If we are linking against a dynamic object, finish up the dynamic
5424 linking information. */
5427 Elf_External_Dyn *dyncon, *dynconend;
5428 struct elf_backend_data *bed;
5430 /* Fix up .dynamic entries. */
5431 o = bfd_get_section_by_name (dynobj, ".dynamic");
5432 BFD_ASSERT (o != NULL);
5434 dyncon = (Elf_External_Dyn *) o->contents;
5435 dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size);
5436 for (; dyncon < dynconend; dyncon++)
5438 Elf_Internal_Dyn dyn;
5442 elf_swap_dyn_in (dynobj, dyncon, &dyn);
5464 o = bfd_get_section_by_name (abfd, name);
5465 BFD_ASSERT (o != NULL);
5466 dyn.d_un.d_ptr = o->vma;
5467 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5474 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
5479 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
5481 Elf_Internal_Shdr *hdr;
5483 hdr = elf_elfsections (abfd)[i];
5484 if (hdr->sh_type == type
5485 && (hdr->sh_flags & SHF_ALLOC) != 0)
5487 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
5488 dyn.d_un.d_val += hdr->sh_size;
5491 if (dyn.d_un.d_val == 0
5492 || hdr->sh_addr < dyn.d_un.d_val)
5493 dyn.d_un.d_val = hdr->sh_addr;
5497 elf_swap_dyn_out (dynobj, &dyn, dyncon);
5502 bed = get_elf_backend_data (abfd);
5503 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
5506 for (o = dynobj->sections; o != NULL; o = o->next)
5508 Elf_Internal_Shdr *hdr;
5512 if ((o->flags & SEC_HAS_CONTENTS) == 0)
5514 BFD_ASSERT ((o->flags & SEC_IN_MEMORY) != 0);
5515 if (! bfd_set_section_contents (abfd, o->output_section,
5516 o->contents, o->output_offset,
5520 hdr = &elf_section_data (o->output_section)->this_hdr;
5521 switch (hdr->sh_type)
5524 s = bfd_get_section_by_name (abfd, ".dynstr");
5525 hdr->sh_link = elf_section_data (s)->this_idx;
5526 hdr->sh_entsize = sizeof (Elf_External_Dyn);
5530 s = finfo.dynsym_sec->output_section;
5531 hdr->sh_link = elf_section_data (s)->this_idx;
5532 hdr->sh_entsize = ARCH_SIZE / 8;
5536 s = bfd_get_section_by_name (abfd, ".dynstr");
5537 hdr->sh_link = elf_section_data (s)->this_idx;
5538 hdr->sh_entsize = sizeof (Elf_External_Sym);
5543 name = bfd_get_section_name (abfd, o->output_section);
5544 s = finfo.dynsym_sec->output_section;
5545 hdr->sh_link = elf_section_data (s)->this_idx;
5546 if (hdr->sh_type == SHT_REL)
5549 hdr->sh_entsize = sizeof (Elf_External_Rel);
5554 hdr->sh_entsize = sizeof (Elf_External_Rela);
5556 s = bfd_get_section_by_name (abfd, name);
5558 hdr->sh_info = elf_section_data (s)->this_idx;
5564 if (finfo.contents != NULL)
5565 free (finfo.contents);
5566 if (finfo.external_relocs != NULL)
5567 free (finfo.external_relocs);
5568 if (finfo.internal_relocs != NULL)
5569 free (finfo.internal_relocs);
5570 if (finfo.external_syms != NULL)
5571 free (finfo.external_syms);
5572 if (finfo.internal_syms != NULL)
5573 free (finfo.internal_syms);
5574 if (finfo.indices != NULL)
5575 free (finfo.indices);
5576 if (finfo.sections != NULL)
5577 free (finfo.sections);
5578 if (finfo.symbuf != NULL)
5579 free (finfo.symbuf);
5580 for (o = abfd->sections; o != NULL; o = o->next)
5582 if ((o->flags & SEC_RELOC) != 0
5583 && elf_section_data (o)->rel_hashes != NULL)
5584 free (elf_section_data (o)->rel_hashes);
5590 if (finfo.contents != NULL)
5591 free (finfo.contents);
5592 if (finfo.external_relocs != NULL)
5593 free (finfo.external_relocs);
5594 if (finfo.internal_relocs != NULL)
5595 free (finfo.internal_relocs);
5596 if (finfo.external_syms != NULL)
5597 free (finfo.external_syms);
5598 if (finfo.internal_syms != NULL)
5599 free (finfo.internal_syms);
5600 if (finfo.indices != NULL)
5601 free (finfo.indices);
5602 if (finfo.sections != NULL)
5603 free (finfo.sections);
5604 if (finfo.symbuf != NULL)
5605 free (finfo.symbuf);
5606 for (o = abfd->sections; o != NULL; o = o->next)
5608 if ((o->flags & SEC_RELOC) != 0
5609 && elf_section_data (o)->rel_hashes != NULL)
5610 free (elf_section_data (o)->rel_hashes);
5616 /* Add a symbol to the output symbol table. */
5619 elf_link_output_sym (finfo, name, elfsym)
5620 struct elf_final_link_info *finfo;
5622 Elf_Internal_Sym *elfsym;
5624 if (name == (const char *) NULL || *name == '\0')
5625 elfsym->st_name = 0;
5628 elfsym->st_name = bfd_add_to_strtab (finfo->output_bfd,
5629 finfo->symstrtab, name);
5630 if (elfsym->st_name == (unsigned long) -1)
5634 if (finfo->symbuf_count >= finfo->symbuf_size)
5636 if (! elf_link_flush_output_syms (finfo))
5640 elf_swap_symbol_out (finfo->output_bfd, elfsym,
5641 finfo->symbuf + finfo->symbuf_count);
5642 ++finfo->symbuf_count;
5644 ++finfo->output_bfd->symcount;
5649 /* Flush the output symbols to the file. */
5652 elf_link_flush_output_syms (finfo)
5653 struct elf_final_link_info *finfo;
5655 Elf_Internal_Shdr *symtab;
5657 symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr;
5659 if (bfd_seek (finfo->output_bfd, symtab->sh_offset + symtab->sh_size,
5661 || (bfd_write ((PTR) finfo->symbuf, finfo->symbuf_count,
5662 sizeof (Elf_External_Sym), finfo->output_bfd)
5663 != finfo->symbuf_count * sizeof (Elf_External_Sym)))
5666 symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym);
5668 finfo->symbuf_count = 0;
5673 /* Add an external symbol to the symbol table. This is called from
5674 the hash table traversal routine. */
5677 elf_link_output_extsym (h, data)
5678 struct elf_link_hash_entry *h;
5681 struct elf_final_link_info *finfo = (struct elf_final_link_info *) data;
5683 Elf_Internal_Sym sym;
5685 /* We don't want to output symbols that have never been mentioned by
5686 a regular file, or that we have been told to strip. However, if
5687 h->indx is set to -2, the symbol is used by a reloc and we must
5691 else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
5692 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
5694 else if (finfo->info->strip == strip_all
5695 || (finfo->info->strip == strip_some
5696 && bfd_hash_lookup (finfo->info->keep_hash,
5697 h->root.root.string,
5698 false, false) == NULL))
5703 /* If we're stripping it, and it's not a dynamic symbol, there's
5704 nothing else to do. */
5705 if (strip && h->dynindx == -1)
5709 sym.st_size = h->size;
5710 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
5713 switch (h->root.type)
5716 case bfd_link_hash_new:
5720 case bfd_link_hash_undefined:
5721 sym.st_shndx = SHN_UNDEF;
5724 case bfd_link_hash_weak:
5725 sym.st_shndx = SHN_UNDEF;
5726 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
5729 case bfd_link_hash_defined:
5733 sec = h->root.u.def.section;
5734 if (sec->output_section != NULL)
5736 sym.st_shndx = elf_section_from_bfd_section (finfo->output_bfd,
5737 sec->output_section);
5738 if (sym.st_shndx == (unsigned short) -1)
5740 /* FIXME: No way to handle errors. */
5744 /* ELF symbols in relocateable files are section relative,
5745 but in nonrelocateable files they are virtual
5747 sym.st_value = h->root.u.def.value + sec->output_offset;
5748 if (! finfo->info->relocateable)
5749 sym.st_value += sec->output_section->vma;
5753 BFD_ASSERT (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
5754 && elf_elfheader (sec->owner)->e_type == ET_DYN);
5755 sym.st_shndx = SHN_UNDEF;
5760 case bfd_link_hash_common:
5761 sym.st_shndx = SHN_COMMON;
5765 sym.st_value = h->align;
5768 case bfd_link_hash_indirect:
5769 case bfd_link_hash_warning:
5770 /* I have no idea how these should be handled. */
5774 /* If this symbol should be put in the .dynsym section, then put it
5775 there now. We have already know the symbol index. We also fill
5776 in the entry in the .hash section. */
5777 if (h->dynindx != -1)
5779 struct elf_backend_data *bed;
5782 bfd_byte *bucketpos;
5785 sym.st_name = h->dynstr_index;
5787 /* Give the processor backend a chance to tweak the symbol
5788 value, and also to finish up anything that needs to be done
5790 bed = get_elf_backend_data (finfo->output_bfd);
5791 if (! ((*bed->elf_backend_finish_dynamic_symbol)
5792 (finfo->output_bfd, finfo->info, h, &sym)))
5794 /* FIXME: No way to return error. */
5798 elf_swap_symbol_out (finfo->output_bfd, &sym,
5799 ((Elf_External_Sym *) finfo->dynsym_sec->contents
5802 bucketcount = elf_hash_table (finfo->info)->bucketcount;
5803 bucket = bfd_elf_hash (h->root.root.string) % bucketcount;
5804 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
5805 + (bucket + 2) * (ARCH_SIZE / 8));
5806 chain = get_word (finfo->output_bfd, bucketpos);
5807 put_word (finfo->output_bfd, h->dynindx, bucketpos);
5808 put_word (finfo->output_bfd, chain,
5809 ((bfd_byte *) finfo->hash_sec->contents
5810 + (bucketcount + 2 + h->dynindx) * (ARCH_SIZE / 8)));
5813 /* If we're stripping it, then it was just a dynamic symbol, and
5814 there's nothing else to do. */
5818 h->indx = finfo->output_bfd->symcount;
5820 if (! elf_link_output_sym (finfo, h->root.root.string, &sym))
5822 /* FIXME: No way to return error. */
5829 /* Link an input file into the linker output file. This function
5830 handles all the sections and relocations of the input file at once.
5831 This is so that we only have to read the local symbols once, and
5832 don't have to keep them in memory. */
5835 elf_link_input_bfd (finfo, input_bfd)
5836 struct elf_final_link_info *finfo;
5839 boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *,
5840 bfd *, asection *, bfd_byte *,
5841 Elf_Internal_Rela *,
5845 Elf_Internal_Shdr *symtab_hdr;
5846 Elf_External_Sym *esym;
5847 Elf_External_Sym *esymend;
5848 Elf_Internal_Sym *isym;
5850 asection **ppsection;
5853 output_bfd = finfo->output_bfd;
5855 get_elf_backend_data (output_bfd)->elf_backend_relocate_section;
5857 /* If this is a dynamic object, we don't want to do anything here:
5858 we don't want the local symbols, and we don't want the section
5860 if (elf_elfheader (input_bfd)->e_type == ET_DYN)
5863 /* Read the local symbols. */
5864 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5865 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
5866 || (bfd_read (finfo->external_syms, sizeof (Elf_External_Sym),
5867 symtab_hdr->sh_info, input_bfd)
5868 != symtab_hdr->sh_info * sizeof (Elf_External_Sym)))
5871 /* Swap in the local symbols and write out the ones which we know
5872 are going into the output file. */
5873 esym = finfo->external_syms;
5874 esymend = esym + symtab_hdr->sh_info;
5875 isym = finfo->internal_syms;
5876 pindex = finfo->indices;
5877 ppsection = finfo->sections;
5878 for (; esym < esymend; esym++, isym++, pindex++, ppsection++)
5884 elf_swap_symbol_in (input_bfd, esym, isym);
5887 if (isym->st_shndx == SHN_UNDEF)
5888 isec = &bfd_und_section;
5889 else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE)
5891 isec = section_from_elf_index (input_bfd, isym->st_shndx);
5895 else if (isym->st_shndx == SHN_ABS)
5896 isec = &bfd_abs_section;
5897 else if (isym->st_shndx == SHN_COMMON)
5898 isec = &bfd_com_section;
5907 /* Don't output the first, undefined, symbol. */
5908 if (esym == finfo->external_syms)
5911 /* If we are stripping all symbols, we don't want to output this
5913 if (finfo->info->strip == strip_all)
5916 /* We never output section symbols. Instead, we use the section
5917 symbol of the corresponding section in the output file. */
5918 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
5921 /* If we are discarding all local symbols, we don't want to
5922 output this one. If we are generating a relocateable output
5923 file, then some of the local symbols may be required by
5924 relocs; we output them below as we discover that they are
5926 if (finfo->info->discard == discard_all)
5929 /* Get the name of the symbol. */
5930 name = elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
5935 /* See if we are discarding symbols with this name. */
5936 if ((finfo->info->strip == strip_some
5937 && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
5939 || (finfo->info->discard == discard_l
5940 && strncmp (name, finfo->info->lprefix,
5941 finfo->info->lprefix_len) == 0))
5944 /* If we get here, we are going to output this symbol. */
5946 /* Adjust the section index for the output file. */
5947 isym->st_shndx = elf_section_from_bfd_section (output_bfd,
5948 isec->output_section);
5949 if (isym->st_shndx == (unsigned short) -1)
5952 *pindex = output_bfd->symcount;
5954 /* ELF symbols in relocateable files are section relative, but
5955 in executable files they are virtual addresses. Note that
5956 this code assumes that all ELF sections have an associated
5957 BFD section with a reasonable value for output_offset; below
5958 we assume that they also have a reasonable value for
5959 output_section. Any special sections must be set up to meet
5960 these requirements. */
5961 oldval = isym->st_value;
5962 isym->st_value += isec->output_offset;
5963 if (! finfo->info->relocateable)
5964 isym->st_value += isec->output_section->vma;
5966 if (! elf_link_output_sym (finfo, name, isym))
5969 /* Restore the old value for reloc handling. */
5970 isym->st_value = oldval;
5973 /* Relocate the contents of each section. */
5974 for (o = input_bfd->sections; o != NULL; o = o->next)
5976 Elf_Internal_Shdr *input_rel_hdr;
5978 if ((o->flags & SEC_HAS_CONTENTS) == 0)
5981 /* Read the contents of the section. */
5982 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
5983 (file_ptr) 0, o->_raw_size))
5986 if ((o->flags & SEC_RELOC) != 0)
5988 /* Read in the relocs. */
5989 input_rel_hdr = &elf_section_data (o)->rel_hdr;
5990 if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
5991 || bfd_read (finfo->external_relocs, 1, input_rel_hdr->sh_size,
5992 input_bfd) != input_rel_hdr->sh_size)
5995 /* Swap in the relocs. For convenience, we always produce
5996 an Elf_Internal_Rela array; if the relocs are Rel, we set
5998 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
6000 Elf_External_Rel *erel;
6001 Elf_External_Rel *erelend;
6002 Elf_Internal_Rela *irela;
6004 erel = (Elf_External_Rel *) finfo->external_relocs;
6005 erelend = erel + o->reloc_count;
6006 irela = finfo->internal_relocs;
6007 for (; erel < erelend; erel++, irela++)
6009 Elf_Internal_Rel irel;
6011 elf_swap_reloc_in (input_bfd, erel, &irel);
6012 irela->r_offset = irel.r_offset;
6013 irela->r_info = irel.r_info;
6014 irela->r_addend = 0;
6019 Elf_External_Rela *erela;
6020 Elf_External_Rela *erelaend;
6021 Elf_Internal_Rela *irela;
6023 BFD_ASSERT (input_rel_hdr->sh_entsize
6024 == sizeof (Elf_External_Rela));
6026 erela = (Elf_External_Rela *) finfo->external_relocs;
6027 erelaend = erela + o->reloc_count;
6028 irela = finfo->internal_relocs;
6029 for (; erela < erelaend; erela++, irela++)
6030 elf_swap_reloca_in (input_bfd, erela, irela);
6033 /* Relocate the section by invoking a back end routine.
6035 The back end routine is responsible for adjusting the
6036 section contents as necessary, and (if using Rela relocs
6037 and generating a relocateable output file) adjusting the
6038 reloc addend as necessary.
6040 The back end routine does not have to worry about setting
6041 the reloc address or the reloc symbol index.
6043 The back end routine is given a pointer to the swapped in
6044 internal symbols, and can access the hash table entries
6045 for the external symbols via elf_sym_hashes (input_bfd).
6047 When generating relocateable output, the back end routine
6048 must handle STB_LOCAL/STT_SECTION symbols specially. The
6049 output symbol is going to be a section symbol
6050 corresponding to the output section, which will require
6051 the addend to be adjusted. */
6053 if (! (*relocate_section) (output_bfd, finfo->info,
6056 finfo->internal_relocs,
6057 finfo->internal_syms,
6061 if (finfo->info->relocateable)
6063 Elf_Internal_Rela *irela;
6064 Elf_Internal_Rela *irelaend;
6065 struct elf_link_hash_entry **rel_hash;
6066 Elf_Internal_Shdr *output_rel_hdr;
6068 /* Adjust the reloc addresses and symbol indices. */
6070 irela = finfo->internal_relocs;
6071 irelaend = irela + o->reloc_count;
6072 rel_hash = (elf_section_data (o->output_section)->rel_hashes
6073 + o->output_section->reloc_count);
6074 for (; irela < irelaend; irela++, rel_hash++)
6077 Elf_Internal_Sym *isym;
6080 irela->r_offset += o->output_offset;
6082 r_symndx = ELF_R_SYM (irela->r_info);
6087 if (r_symndx >= symtab_hdr->sh_info)
6091 /* This is a reloc against a global symbol. We
6092 have not yet output all the local symbols, so
6093 we do not know the symbol index of any global
6094 symbol. We set the rel_hash entry for this
6095 reloc to point to the global hash table entry
6096 for this symbol. The symbol index is then
6097 set at the end of elf_bfd_final_link. */
6098 indx = r_symndx - symtab_hdr->sh_info;
6099 *rel_hash = elf_sym_hashes (input_bfd)[indx];
6101 /* Setting the index to -2 tells
6102 elf_link_output_extsym that this symbol is
6104 BFD_ASSERT ((*rel_hash)->indx < 0);
6105 (*rel_hash)->indx = -2;
6110 /* This is a reloc against a local symbol. */
6113 isym = finfo->internal_syms + r_symndx;
6114 sec = finfo->sections[r_symndx];
6115 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6117 /* I suppose the backend ought to fill in the
6118 section of any STT_SECTION symbol against a
6119 processor specific section. */
6121 || sec == &bfd_und_section
6122 || sec == &bfd_com_section)
6124 bfd_set_error (bfd_error_bad_value);
6127 else if (sec == &bfd_abs_section)
6131 r_symndx = sec->output_section->target_index;
6138 if (finfo->indices[r_symndx] == -1)
6144 if (finfo->info->strip == strip_all)
6146 /* You can't do ld -r -s. */
6147 bfd_set_error (bfd_error_invalid_operation);
6151 /* This symbol was skipped earlier, but
6152 since it is needed by a reloc, we
6153 must output it now. */
6154 link = symtab_hdr->sh_link;
6155 name = elf_string_from_elf_section (input_bfd,
6161 osec = sec->output_section;
6163 elf_section_from_bfd_section (output_bfd,
6165 if (isym->st_shndx == (unsigned short) -1)
6168 isym->st_value += sec->output_offset;
6169 if (! finfo->info->relocateable)
6170 isym->st_value += osec->vma;
6172 finfo->indices[r_symndx] = output_bfd->symcount;
6174 if (! elf_link_output_sym (finfo, name, isym))
6178 r_symndx = finfo->indices[r_symndx];
6181 irela->r_info = ELF_R_INFO (r_symndx,
6182 ELF_R_TYPE (irela->r_info));
6185 /* Swap out the relocs. */
6186 output_rel_hdr = &elf_section_data (o->output_section)->rel_hdr;
6187 BFD_ASSERT (output_rel_hdr->sh_entsize
6188 == input_rel_hdr->sh_entsize);
6189 irela = finfo->internal_relocs;
6190 irelaend = irela + o->reloc_count;
6191 if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
6193 Elf_External_Rel *erel;
6195 erel = ((Elf_External_Rel *) output_rel_hdr->contents
6196 + o->output_section->reloc_count);
6197 for (; irela < irelaend; irela++, erel++)
6199 Elf_Internal_Rel irel;
6201 irel.r_offset = irela->r_offset;
6202 irel.r_info = irela->r_info;
6203 BFD_ASSERT (irela->r_addend == 0);
6204 elf_swap_reloc_out (output_bfd, &irel, erel);
6209 Elf_External_Rela *erela;
6211 BFD_ASSERT (input_rel_hdr->sh_entsize
6212 == sizeof (Elf_External_Rela));
6213 erela = ((Elf_External_Rela *) output_rel_hdr->contents
6214 + o->output_section->reloc_count);
6215 for (; irela < irelaend; irela++, erela++)
6216 elf_swap_reloca_out (output_bfd, irela, erela);
6219 o->output_section->reloc_count += o->reloc_count;
6223 /* Write out the modified section contents. */
6224 if (! bfd_set_section_contents (output_bfd, o->output_section,
6225 finfo->contents, o->output_offset,
6226 (o->_cooked_size != 0
6235 /* Generate a reloc when linking an ELF file. This is a reloc
6236 requested by the linker, and does come from any input file. This
6237 is used to build constructor and destructor tables when linking
6241 elf_reloc_link_order (output_bfd, info, output_section, link_order)
6243 struct bfd_link_info *info;
6244 asection *output_section;
6245 struct bfd_link_order *link_order;
6247 const reloc_howto_type *howto;
6250 struct elf_link_hash_entry **rel_hash_ptr;
6251 Elf_Internal_Shdr *rel_hdr;
6253 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6256 bfd_set_error (bfd_error_bad_value);
6260 /* If this is an inplace reloc, we must write the addend into the
6262 if (howto->partial_inplace
6263 && link_order->u.reloc.p->addend != 0)
6266 bfd_reloc_status_type rstat;
6270 size = bfd_get_reloc_size (howto);
6271 buf = (bfd_byte *) bfd_zmalloc (size);
6272 if (buf == (bfd_byte *) NULL)
6274 bfd_set_error (bfd_error_no_memory);
6277 rstat = _bfd_relocate_contents (howto, output_bfd,
6278 link_order->u.reloc.p->addend, buf);
6284 case bfd_reloc_outofrange:
6286 case bfd_reloc_overflow:
6287 if (! ((*info->callbacks->reloc_overflow)
6289 (link_order->type == bfd_section_reloc_link_order
6290 ? bfd_section_name (output_bfd,
6291 link_order->u.reloc.p->u.section)
6292 : link_order->u.reloc.p->u.name),
6293 howto->name, link_order->u.reloc.p->addend,
6294 (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
6301 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6302 (file_ptr) link_order->offset, size);
6308 /* Figure out the symbol index. */
6309 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
6310 + output_section->reloc_count);
6311 if (link_order->type == bfd_section_reloc_link_order)
6313 indx = link_order->u.reloc.p->u.section->target_index;
6316 *rel_hash_ptr = NULL;
6320 struct elf_link_hash_entry *h;
6322 h = elf_link_hash_lookup (elf_hash_table (info),
6323 link_order->u.reloc.p->u.name,
6324 false, false, true);
6327 /* Setting the index to -2 tells elf_link_output_extsym that
6328 this symbol is used by a reloc. */
6335 if (! ((*info->callbacks->unattached_reloc)
6336 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6337 (asection *) NULL, (bfd_vma) 0)))
6343 /* The address of a reloc is relative to the section in a
6344 relocateable file, and is a virtual address in an executable
6346 offset = link_order->offset;
6347 if (! info->relocateable)
6348 offset += output_section->vma;
6350 rel_hdr = &elf_section_data (output_section)->rel_hdr;
6352 if (rel_hdr->sh_type == SHT_REL)
6354 Elf_Internal_Rel irel;
6355 Elf_External_Rel *erel;
6357 irel.r_offset = offset;
6358 irel.r_info = ELF_R_INFO (indx, howto->type);
6359 erel = ((Elf_External_Rel *) rel_hdr->contents
6360 + output_section->reloc_count);
6361 elf_swap_reloc_out (output_bfd, &irel, erel);
6365 Elf_Internal_Rela irela;
6366 Elf_External_Rela *erela;
6368 irela.r_offset = offset;
6369 irela.r_info = ELF_R_INFO (indx, howto->type);
6370 irela.r_addend = link_order->u.reloc.p->addend;
6371 erela = ((Elf_External_Rela *) rel_hdr->contents
6372 + output_section->reloc_count);
6373 elf_swap_reloca_out (output_bfd, &irela, erela);
6376 ++output_section->reloc_count;