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