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