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