* aout-adobe.c (aout_adobe_set_arch_mach): Check return value of
[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 "libbfd.h"
66 #include "libelf.h"
67
68 /* Renaming structures, typedefs, macros and functions to be size-specific. */
69 #define Elf_External_Ehdr NAME(Elf,External_Ehdr)
70 #define Elf_External_Sym NAME(Elf,External_Sym)
71 #define Elf_External_Shdr NAME(Elf,External_Shdr)
72 #define Elf_External_Phdr NAME(Elf,External_Phdr)
73 #define Elf_External_Rel NAME(Elf,External_Rel)
74 #define Elf_External_Rela NAME(Elf,External_Rela)
75
76 #define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command)
77 #define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal)
78 #define elf_core_file_matches_executable_p \
79 NAME(bfd_elf,core_file_matches_executable_p)
80 #define elf_object_p NAME(bfd_elf,object_p)
81 #define elf_core_file_p NAME(bfd_elf,core_file_p)
82 #define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound)
83 #define elf_get_dynamic_symtab_upper_bound \
84 NAME(bfd_elf,get_dynamic_symtab_upper_bound)
85 #define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound)
86 #define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc)
87 #define elf_get_symtab NAME(bfd_elf,get_symtab)
88 #define elf_canonicalize_dynamic_symtab \
89 NAME(bfd_elf,canonicalize_dynamic_symtab)
90 #define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol)
91 #define elf_get_symbol_info NAME(bfd_elf,get_symbol_info)
92 #define elf_print_symbol NAME(bfd_elf,print_symbol)
93 #define elf_get_lineno NAME(bfd_elf,get_lineno)
94 #define elf_set_arch_mach NAME(bfd_elf,set_arch_mach)
95 #define elf_find_nearest_line NAME(bfd_elf,find_nearest_line)
96 #define elf_sizeof_headers NAME(bfd_elf,sizeof_headers)
97 #define elf_set_section_contents NAME(bfd_elf,set_section_contents)
98 #define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto)
99 #define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel)
100 #define elf_new_section_hook NAME(bfd_elf,new_section_hook)
101 #define write_relocs NAME(bfd_elf,_write_relocs)
102 #define elf_find_section NAME(bfd_elf,find_section)
103
104 #if ARCH_SIZE == 64
105 #define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
106 #define ELF_R_SYM(X) ELF64_R_SYM(X)
107 #define ELFCLASS ELFCLASS64
108 #define FILE_ALIGN 8
109 #endif
110 #if ARCH_SIZE == 32
111 #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
112 #define ELF_R_SYM(X) ELF32_R_SYM(X)
113 #define ELFCLASS ELFCLASS32
114 #define FILE_ALIGN 4
115 #endif
116
117 static int shstrtab_length_fixed;
118
119 struct elf_sect_data
120 {
121 int reloc_sec;
122 /* more? */
123 };
124
125 /* Forward declarations of static functions */
126
127 static struct sec *section_from_elf_index PARAMS ((bfd *, unsigned int));
128
129 static int elf_section_from_bfd_section PARAMS ((bfd *, struct sec *));
130
131 static long elf_slurp_symbol_table PARAMS ((bfd *, asymbol **, boolean));
132
133 static int elf_symbol_from_bfd_symbol PARAMS ((bfd *,
134 struct symbol_cache_entry **));
135
136 static boolean elf_map_symbols PARAMS ((bfd *));
137 static boolean swap_out_syms PARAMS ((bfd *));
138
139 #ifdef DEBUG
140 static void elf_debug_section PARAMS ((char *, int, Elf_Internal_Shdr *));
141 static void elf_debug_file PARAMS ((Elf_Internal_Ehdr *));
142 #endif
143
144 #define elf_string_from_elf_strtab(abfd,strindex) \
145 elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex)
146 \f
147
148 /* Structure swapping routines */
149
150 /* Should perhaps use put_offset, put_word, etc. For now, the two versions
151 can be handled by explicitly specifying 32 bits or "the long type". */
152 #if ARCH_SIZE == 64
153 #define put_word bfd_h_put_64
154 #define get_word bfd_h_get_64
155 #endif
156 #if ARCH_SIZE == 32
157 #define put_word bfd_h_put_32
158 #define get_word bfd_h_get_32
159 #endif
160
161 /* Translate an ELF symbol in external format into an ELF symbol in internal
162 format. */
163
164 static void
165 elf_swap_symbol_in (abfd, src, dst)
166 bfd *abfd;
167 Elf_External_Sym *src;
168 Elf_Internal_Sym *dst;
169 {
170 dst->st_name = bfd_h_get_32 (abfd, (bfd_byte *) src->st_name);
171 dst->st_value = get_word (abfd, (bfd_byte *) src->st_value);
172 dst->st_size = get_word (abfd, (bfd_byte *) src->st_size);
173 dst->st_info = bfd_h_get_8 (abfd, (bfd_byte *) src->st_info);
174 dst->st_other = bfd_h_get_8 (abfd, (bfd_byte *) src->st_other);
175 dst->st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src->st_shndx);
176 }
177
178 /* Translate an ELF symbol in internal format into an ELF symbol in external
179 format. */
180
181 static void
182 elf_swap_symbol_out (abfd, src, dst)
183 bfd *abfd;
184 Elf_Internal_Sym *src;
185 Elf_External_Sym *dst;
186 {
187 bfd_h_put_32 (abfd, src->st_name, dst->st_name);
188 put_word (abfd, src->st_value, dst->st_value);
189 put_word (abfd, src->st_size, dst->st_size);
190 bfd_h_put_8 (abfd, src->st_info, dst->st_info);
191 bfd_h_put_8 (abfd, src->st_other, dst->st_other);
192 bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx);
193 }
194
195
196 /* Translate an ELF file header in external format into an ELF file header in
197 internal format. */
198
199 static void
200 elf_swap_ehdr_in (abfd, src, dst)
201 bfd *abfd;
202 Elf_External_Ehdr *src;
203 Elf_Internal_Ehdr *dst;
204 {
205 memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
206 dst->e_type = bfd_h_get_16 (abfd, (bfd_byte *) src->e_type);
207 dst->e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src->e_machine);
208 dst->e_version = bfd_h_get_32 (abfd, (bfd_byte *) src->e_version);
209 dst->e_entry = get_word (abfd, (bfd_byte *) src->e_entry);
210 dst->e_phoff = get_word (abfd, (bfd_byte *) src->e_phoff);
211 dst->e_shoff = get_word (abfd, (bfd_byte *) src->e_shoff);
212 dst->e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->e_flags);
213 dst->e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_ehsize);
214 dst->e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phentsize);
215 dst->e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phnum);
216 dst->e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shentsize);
217 dst->e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shnum);
218 dst->e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shstrndx);
219 }
220
221 /* Translate an ELF file header in internal format into an ELF file header in
222 external format. */
223
224 static void
225 elf_swap_ehdr_out (abfd, src, dst)
226 bfd *abfd;
227 Elf_Internal_Ehdr *src;
228 Elf_External_Ehdr *dst;
229 {
230 memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
231 /* note that all elements of dst are *arrays of unsigned char* already... */
232 bfd_h_put_16 (abfd, src->e_type, dst->e_type);
233 bfd_h_put_16 (abfd, src->e_machine, dst->e_machine);
234 bfd_h_put_32 (abfd, src->e_version, dst->e_version);
235 put_word (abfd, src->e_entry, dst->e_entry);
236 put_word (abfd, src->e_phoff, dst->e_phoff);
237 put_word (abfd, src->e_shoff, dst->e_shoff);
238 bfd_h_put_32 (abfd, src->e_flags, dst->e_flags);
239 bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize);
240 bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize);
241 bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum);
242 bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize);
243 bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum);
244 bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx);
245 }
246
247
248 /* Translate an ELF section header table entry in external format into an
249 ELF section header table entry in internal format. */
250
251 static void
252 elf_swap_shdr_in (abfd, src, dst)
253 bfd *abfd;
254 Elf_External_Shdr *src;
255 Elf_Internal_Shdr *dst;
256 {
257 dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name);
258 dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type);
259 dst->sh_flags = get_word (abfd, (bfd_byte *) src->sh_flags);
260 dst->sh_addr = get_word (abfd, (bfd_byte *) src->sh_addr);
261 dst->sh_offset = get_word (abfd, (bfd_byte *) src->sh_offset);
262 dst->sh_size = get_word (abfd, (bfd_byte *) src->sh_size);
263 dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link);
264 dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info);
265 dst->sh_addralign = get_word (abfd, (bfd_byte *) src->sh_addralign);
266 dst->sh_entsize = get_word (abfd, (bfd_byte *) src->sh_entsize);
267 /* we haven't done any processing on it yet, so... */
268 dst->rawdata = (void *) 0;
269 }
270
271 /* Translate an ELF section header table entry in internal format into an
272 ELF section header table entry in external format. */
273
274 static void
275 elf_swap_shdr_out (abfd, src, dst)
276 bfd *abfd;
277 Elf_Internal_Shdr *src;
278 Elf_External_Shdr *dst;
279 {
280 /* note that all elements of dst are *arrays of unsigned char* already... */
281 bfd_h_put_32 (abfd, src->sh_name, dst->sh_name);
282 bfd_h_put_32 (abfd, src->sh_type, dst->sh_type);
283 put_word (abfd, src->sh_flags, dst->sh_flags);
284 put_word (abfd, src->sh_addr, dst->sh_addr);
285 put_word (abfd, src->sh_offset, dst->sh_offset);
286 put_word (abfd, src->sh_size, dst->sh_size);
287 bfd_h_put_32 (abfd, src->sh_link, dst->sh_link);
288 bfd_h_put_32 (abfd, src->sh_info, dst->sh_info);
289 put_word (abfd, src->sh_addralign, dst->sh_addralign);
290 put_word (abfd, src->sh_entsize, dst->sh_entsize);
291 }
292
293
294 /* Translate an ELF program header table entry in external format into an
295 ELF program header table entry in internal format. */
296
297 static void
298 elf_swap_phdr_in (abfd, src, dst)
299 bfd *abfd;
300 Elf_External_Phdr *src;
301 Elf_Internal_Phdr *dst;
302 {
303 dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type);
304 dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags);
305 dst->p_offset = get_word (abfd, (bfd_byte *) src->p_offset);
306 dst->p_vaddr = get_word (abfd, (bfd_byte *) src->p_vaddr);
307 dst->p_paddr = get_word (abfd, (bfd_byte *) src->p_paddr);
308 dst->p_filesz = get_word (abfd, (bfd_byte *) src->p_filesz);
309 dst->p_memsz = get_word (abfd, (bfd_byte *) src->p_memsz);
310 dst->p_align = get_word (abfd, (bfd_byte *) src->p_align);
311 }
312
313 static void
314 elf_swap_phdr_out (abfd, src, dst)
315 bfd *abfd;
316 Elf_Internal_Phdr *src;
317 Elf_External_Phdr *dst;
318 {
319 /* note that all elements of dst are *arrays of unsigned char* already... */
320 bfd_h_put_32 (abfd, src->p_type, dst->p_type);
321 put_word (abfd, src->p_offset, dst->p_offset);
322 put_word (abfd, src->p_vaddr, dst->p_vaddr);
323 put_word (abfd, src->p_paddr, dst->p_paddr);
324 put_word (abfd, src->p_filesz, dst->p_filesz);
325 put_word (abfd, src->p_memsz, dst->p_memsz);
326 bfd_h_put_32 (abfd, src->p_flags, dst->p_flags);
327 put_word (abfd, src->p_align, dst->p_align);
328 }
329
330 /* Translate an ELF reloc from external format to internal format. */
331 static INLINE void
332 elf_swap_reloc_in (abfd, src, dst)
333 bfd *abfd;
334 Elf_External_Rel *src;
335 Elf_Internal_Rel *dst;
336 {
337 dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
338 dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
339 }
340
341 static INLINE void
342 elf_swap_reloca_in (abfd, src, dst)
343 bfd *abfd;
344 Elf_External_Rela *src;
345 Elf_Internal_Rela *dst;
346 {
347 dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
348 dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
349 dst->r_addend = get_word (abfd, (bfd_byte *) src->r_addend);
350 }
351
352 /* Translate an ELF reloc from internal format to external format. */
353 static INLINE void
354 elf_swap_reloc_out (abfd, src, dst)
355 bfd *abfd;
356 Elf_Internal_Rel *src;
357 Elf_External_Rel *dst;
358 {
359 put_word (abfd, src->r_offset, dst->r_offset);
360 put_word (abfd, src->r_info, dst->r_info);
361 }
362
363 static INLINE void
364 elf_swap_reloca_out (abfd, src, dst)
365 bfd *abfd;
366 Elf_Internal_Rela *src;
367 Elf_External_Rela *dst;
368 {
369 put_word (abfd, src->r_offset, dst->r_offset);
370 put_word (abfd, src->r_info, dst->r_info);
371 put_word (abfd, src->r_addend, dst->r_addend);
372 }
373 \f
374
375
376 /* String table creation/manipulation routines */
377
378 static struct strtab *
379 bfd_new_strtab (abfd)
380 bfd *abfd;
381 {
382 struct strtab *ss;
383
384 ss = (struct strtab *) malloc (sizeof (struct strtab));
385 if (!ss)
386 {
387 bfd_set_error (bfd_error_no_memory);
388 return NULL;
389 }
390 ss->tab = malloc (1);
391 if (!ss->tab)
392 {
393 bfd_set_error (bfd_error_no_memory);
394 return NULL;
395 }
396 *ss->tab = 0;
397 ss->nentries = 0;
398 ss->length = 1;
399
400 return ss;
401 }
402
403 static int
404 bfd_add_to_strtab (abfd, ss, str)
405 bfd *abfd;
406 struct strtab *ss;
407 CONST char *str;
408 {
409 /* should search first, but for now: */
410 /* include the trailing NUL */
411 int ln = strlen (str) + 1;
412
413 /* should this be using obstacks? */
414 ss->tab = realloc (ss->tab, ss->length + ln);
415
416 BFD_ASSERT (ss->tab != 0); /* FIXME */
417 strcpy (ss->tab + ss->length, str);
418 ss->nentries++;
419 ss->length += ln;
420
421 return ss->length - ln;
422 }
423
424 static int
425 bfd_add_2_to_strtab (abfd, ss, str, str2)
426 bfd *abfd;
427 struct strtab *ss;
428 char *str;
429 CONST char *str2;
430 {
431 /* should search first, but for now: */
432 /* include the trailing NUL */
433 int ln = strlen (str) + strlen (str2) + 1;
434
435 /* should this be using obstacks? */
436 if (ss->length)
437 ss->tab = realloc (ss->tab, ss->length + ln);
438 else
439 ss->tab = malloc (ln);
440
441 BFD_ASSERT (ss->tab != 0); /* FIXME */
442 strcpy (ss->tab + ss->length, str);
443 strcpy (ss->tab + ss->length + strlen (str), str2);
444 ss->nentries++;
445 ss->length += ln;
446
447 return ss->length - ln;
448 }
449 \f
450
451 /* ELF .o/exec file reading */
452
453 /* Create a new bfd section from an ELF section header. */
454
455 static boolean
456 bfd_section_from_shdr (abfd, shindex)
457 bfd *abfd;
458 unsigned int shindex;
459 {
460 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
461 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
462 asection *newsect;
463 char *name;
464
465 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
466
467 switch (hdr->sh_type)
468 {
469
470 case SHT_NULL:
471 /* inactive section. Throw it away. */
472 return true;
473
474 case SHT_PROGBITS:
475 case SHT_DYNAMIC:
476 /* Bits that get saved. This one is real. */
477 if (!hdr->rawdata)
478 {
479 newsect = bfd_make_section (abfd, name);
480 if (newsect != NULL)
481 {
482 newsect->filepos = hdr->sh_offset; /* so we can read back the bits */
483 newsect->flags |= SEC_HAS_CONTENTS;
484 newsect->vma = hdr->sh_addr;
485 newsect->_raw_size = hdr->sh_size;
486 newsect->alignment_power = bfd_log2 (hdr->sh_addralign);
487
488 if (hdr->sh_flags & SHF_ALLOC)
489 {
490 newsect->flags |= SEC_ALLOC;
491 newsect->flags |= SEC_LOAD;
492 }
493
494 if (!(hdr->sh_flags & SHF_WRITE))
495 newsect->flags |= SEC_READONLY;
496
497 if (hdr->sh_flags & SHF_EXECINSTR)
498 newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */
499 else if (newsect->flags & SEC_ALLOC)
500 newsect->flags |= SEC_DATA;
501
502 /* The debugging sections appear to recognized only by
503 name. */
504 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
505 || strncmp (name, ".line", sizeof ".line" - 1) == 0
506 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
507 newsect->flags |= SEC_DEBUGGING;
508
509 hdr->rawdata = (void *) newsect;
510 }
511 else
512 hdr->rawdata = (void *) bfd_get_section_by_name (abfd, name);
513 }
514 return true;
515
516 case SHT_NOBITS:
517 /* Bits that get saved. This one is real. */
518 if (!hdr->rawdata)
519 {
520 newsect = bfd_make_section (abfd, name);
521 if (newsect != NULL)
522 {
523 newsect->vma = hdr->sh_addr;
524 newsect->_raw_size = hdr->sh_size;
525 newsect->filepos = hdr->sh_offset; /* fake */
526 newsect->alignment_power = bfd_log2 (hdr->sh_addralign);
527 if (hdr->sh_flags & SHF_ALLOC)
528 newsect->flags |= SEC_ALLOC;
529
530 if (!(hdr->sh_flags & SHF_WRITE))
531 newsect->flags |= SEC_READONLY;
532
533 /* FIXME: This section is empty. Does it really make
534 sense to set SEC_CODE for it? */
535 if (hdr->sh_flags & SHF_EXECINSTR)
536 newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */
537
538 hdr->rawdata = (void *) newsect;
539 }
540 }
541 return true;
542
543 case SHT_SYMTAB: /* A symbol table */
544 if (elf_onesymtab (abfd) == shindex)
545 return true;
546
547 BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym));
548 BFD_ASSERT (elf_onesymtab (abfd) == 0);
549 elf_onesymtab (abfd) = shindex;
550 elf_tdata (abfd)->symtab_hdr = *hdr;
551 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_hdr;
552 abfd->flags |= HAS_SYMS;
553 return true;
554
555 case SHT_DYNSYM: /* A dynamic symbol table */
556 if (elf_dynsymtab (abfd) == shindex)
557 return true;
558
559 BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym));
560 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
561 elf_dynsymtab (abfd) = shindex;
562 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
563 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->dynsymtab_hdr;
564 abfd->flags |= HAS_SYMS;
565 return true;
566
567 case SHT_STRTAB: /* A string table */
568 if (hdr->rawdata)
569 return true;
570 if (ehdr->e_shstrndx == shindex)
571 {
572 elf_tdata (abfd)->shstrtab_hdr = *hdr;
573 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
574 hdr->rawdata = (PTR) & elf_tdata (abfd)->shstrtab_hdr;
575 return true;
576 }
577 {
578 unsigned int i;
579
580 for (i = 1; i < ehdr->e_shnum; i++)
581 {
582 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
583 if (hdr2->sh_link == shindex)
584 {
585 bfd_section_from_shdr (abfd, i);
586 if (elf_onesymtab (abfd) == i)
587 {
588 elf_tdata (abfd)->strtab_hdr = *hdr;
589 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
590 return true;
591 }
592 if (elf_dynsymtab (abfd) == i)
593 {
594 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
595 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->dynstrtab_hdr;
596 return true;
597 }
598 #if 0 /* Not handling other string tables specially right now. */
599 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
600 /* We have a strtab for some random other section. */
601 newsect = (asection *) hdr2->rawdata;
602 if (!newsect)
603 break;
604 hdr->rawdata = (PTR) newsect;
605 hdr2 = &elf_section_data (newsect)->str_hdr;
606 *hdr2 = *hdr;
607 elf_elfsections (abfd)[shindex] = hdr2;
608 #endif
609 }
610 }
611 }
612
613 newsect = bfd_make_section (abfd, name);
614 if (newsect)
615 {
616 newsect->flags = SEC_HAS_CONTENTS;
617 hdr->rawdata = (PTR) newsect;
618 newsect->_raw_size = hdr->sh_size;
619 newsect->alignment_power = bfd_log2 (hdr->sh_addralign);
620 newsect->vma = hdr->sh_addr;
621 newsect->filepos = hdr->sh_offset;
622
623 if (hdr->sh_flags & SHF_ALLOC)
624 newsect->flags |= SEC_ALLOC | SEC_LOAD;
625 if (!(hdr->sh_flags & SHF_WRITE))
626 newsect->flags |= SEC_READONLY;
627 if (hdr->sh_flags & SHF_EXECINSTR)
628 newsect->flags |= SEC_CODE;
629 else if (newsect->flags & SEC_ALLOC)
630 newsect->flags |= SEC_DATA;
631
632 /* Check for debugging string tables. */
633 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
634 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
635 newsect->flags |= SEC_DEBUGGING;
636 }
637
638 return true;
639
640 case SHT_REL:
641 case SHT_RELA:
642 /* *These* do a lot of work -- but build no sections!
643 The spec says there can be multiple strtabs, but only one symtab,
644 but there can be lots of REL* sections. */
645 /* FIXME: The above statement is wrong! There are typically at least
646 two symbol tables in a dynamically linked executable, ".dynsym"
647 which is the dynamic linkage symbol table and ".symtab", which is
648 the "traditional" symbol table. -fnf */
649
650 {
651 asection *target_sect;
652 Elf_Internal_Shdr *hdr2;
653 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
654
655 /* Don't allow REL relocations on a machine that uses RELA and
656 vice versa. */
657 /* @@ Actually, the generic ABI does suggest that both might be
658 used in one file. But the four ABI Processor Supplements I
659 have access to right now all specify that only one is used on
660 each of those architectures. It's conceivable that, e.g., a
661 bunch of absolute 32-bit relocs might be more compact in REL
662 form even on a RELA machine... */
663 BFD_ASSERT (!(use_rela_p && (hdr->sh_type == SHT_REL)));
664 BFD_ASSERT (!(!use_rela_p && (hdr->sh_type == SHT_RELA)));
665 BFD_ASSERT (hdr->sh_entsize ==
666 (use_rela_p
667 ? sizeof (Elf_External_Rela)
668 : sizeof (Elf_External_Rel)));
669
670 bfd_section_from_shdr (abfd, hdr->sh_info); /* target */
671 bfd_section_from_shdr (abfd, hdr->sh_link); /* symbol table */
672 target_sect = section_from_elf_index (abfd, hdr->sh_info);
673 if (target_sect == NULL
674 || elf_section_data (target_sect) == NULL)
675 return false;
676
677 hdr2 = &elf_section_data (target_sect)->rel_hdr;
678 *hdr2 = *hdr;
679 elf_elfsections (abfd)[shindex] = hdr2;
680 target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
681 target_sect->flags |= SEC_RELOC;
682 target_sect->relocation = 0;
683 target_sect->rel_filepos = hdr->sh_offset;
684 abfd->flags |= HAS_RELOC;
685 return true;
686 }
687 break;
688
689 case SHT_HASH:
690 #if 0
691 fprintf (stderr, "Dynamic Linking sections not yet supported.\n");
692 BFD_FAIL ();
693 #endif
694 break;
695
696 case SHT_NOTE:
697 #if 0
698 fprintf (stderr, "Note Sections not yet supported.\n");
699 BFD_FAIL ();
700 #endif
701 break;
702
703 case SHT_SHLIB:
704 #if 0
705 fprintf (stderr, "SHLIB Sections not supported (and non conforming.)\n");
706 #endif
707 return true;
708
709 default:
710 /* Check for any processor-specific section types. */
711 {
712 struct elf_backend_data *bed = get_elf_backend_data (abfd);
713
714 if (bed->elf_backend_section_from_shdr)
715 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
716 }
717 break;
718 }
719
720 return true;
721 }
722
723 boolean
724 elf_new_section_hook (abfd, sec)
725 bfd *abfd
726 ;
727 asection *sec;
728 {
729 struct bfd_elf_section_data *sdata;
730
731 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
732 if (!sdata)
733 {
734 bfd_set_error (bfd_error_no_memory);
735 return false;
736 }
737 sec->used_by_bfd = (PTR) sdata;
738 memset (sdata, 0, sizeof (*sdata));
739 return true;
740 }
741
742 /* Create a new bfd section from an ELF program header.
743
744 Since program segments have no names, we generate a synthetic name
745 of the form segment<NUM>, where NUM is generally the index in the
746 program header table. For segments that are split (see below) we
747 generate the names segment<NUM>a and segment<NUM>b.
748
749 Note that some program segments may have a file size that is different than
750 (less than) the memory size. All this means is that at execution the
751 system must allocate the amount of memory specified by the memory size,
752 but only initialize it with the first "file size" bytes read from the
753 file. This would occur for example, with program segments consisting
754 of combined data+bss.
755
756 To handle the above situation, this routine generates TWO bfd sections
757 for the single program segment. The first has the length specified by
758 the file size of the segment, and the second has the length specified
759 by the difference between the two sizes. In effect, the segment is split
760 into it's initialized and uninitialized parts.
761
762 */
763
764 static boolean
765 bfd_section_from_phdr (abfd, hdr, index)
766 bfd *abfd;
767 Elf_Internal_Phdr *hdr;
768 int index;
769 {
770 asection *newsect;
771 char *name;
772 char namebuf[64];
773 int split;
774
775 split = ((hdr->p_memsz > 0) &&
776 (hdr->p_filesz > 0) &&
777 (hdr->p_memsz > hdr->p_filesz));
778 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
779 name = bfd_alloc (abfd, strlen (namebuf) + 1);
780 if (!name)
781 {
782 bfd_set_error (bfd_error_no_memory);
783 return false;
784 }
785 strcpy (name, namebuf);
786 newsect = bfd_make_section (abfd, name);
787 newsect->vma = hdr->p_vaddr;
788 newsect->_raw_size = hdr->p_filesz;
789 newsect->filepos = hdr->p_offset;
790 newsect->flags |= SEC_HAS_CONTENTS;
791 if (hdr->p_type == PT_LOAD)
792 {
793 newsect->flags |= SEC_ALLOC;
794 newsect->flags |= SEC_LOAD;
795 if (hdr->p_flags & PF_X)
796 {
797 /* FIXME: all we known is that it has execute PERMISSION,
798 may be data. */
799 newsect->flags |= SEC_CODE;
800 }
801 }
802 if (!(hdr->p_flags & PF_W))
803 {
804 newsect->flags |= SEC_READONLY;
805 }
806
807 if (split)
808 {
809 sprintf (namebuf, "segment%db", index);
810 name = bfd_alloc (abfd, strlen (namebuf) + 1);
811 if (!name)
812 {
813 bfd_set_error (bfd_error_no_memory);
814 return false;
815 }
816 strcpy (name, namebuf);
817 newsect = bfd_make_section (abfd, name);
818 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
819 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
820 if (hdr->p_type == PT_LOAD)
821 {
822 newsect->flags |= SEC_ALLOC;
823 if (hdr->p_flags & PF_X)
824 newsect->flags |= SEC_CODE;
825 }
826 if (!(hdr->p_flags & PF_W))
827 newsect->flags |= SEC_READONLY;
828 }
829
830 return true;
831 }
832
833 /* Begin processing a given object.
834
835 First we validate the file by reading in the ELF header and checking
836 the magic number. */
837
838 static INLINE boolean
839 elf_file_p (x_ehdrp)
840 Elf_External_Ehdr *x_ehdrp;
841 {
842 return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
843 && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
844 && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
845 && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
846 }
847
848 /* Check to see if the file associated with ABFD matches the target vector
849 that ABFD points to.
850
851 Note that we may be called several times with the same ABFD, but different
852 target vectors, most of which will not match. We have to avoid leaving
853 any side effects in ABFD, or any data it points to (like tdata), if the
854 file does not match the target vector.
855
856 FIXME: There is memory leak if we are called more than once with the same
857 ABFD, and that bfd already has tdata allocated, since we allocate more tdata
858 and the old tdata is orphaned. Since it's in the bfd obstack, there isn't
859 much we can do about this except possibly rewrite the code. There are
860 also other bfd_allocs that may be the source of memory leaks as well. */
861
862 bfd_target *
863 elf_object_p (abfd)
864 bfd *abfd;
865 {
866 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
867 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
868 Elf_External_Shdr x_shdr; /* Section header table entry, external form */
869 Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
870 unsigned int shindex;
871 char *shstrtab; /* Internal copy of section header stringtab */
872 struct elf_backend_data *ebd;
873 struct elf_obj_tdata *preserved_tdata = elf_tdata (abfd);
874
875 /* Read in the ELF header in external format. */
876
877 if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
878 {
879 if (bfd_get_error () != bfd_error_system_call)
880 goto got_wrong_format_error;
881 else
882 goto got_no_match;
883 }
884
885 /* Now check to see if we have a valid ELF file, and one that BFD can
886 make use of. The magic number must match, the address size ('class')
887 and byte-swapping must match our XVEC entry, and it must have a
888 section header table (FIXME: See comments re sections at top of this
889 file). */
890
891 if ((elf_file_p (&x_ehdr) == false) ||
892 (x_ehdr.e_ident[EI_VERSION] != EV_CURRENT) ||
893 (x_ehdr.e_ident[EI_CLASS] != ELFCLASS))
894 goto got_wrong_format_error;
895
896 /* Check that file's byte order matches xvec's */
897 switch (x_ehdr.e_ident[EI_DATA])
898 {
899 case ELFDATA2MSB: /* Big-endian */
900 if (!abfd->xvec->header_byteorder_big_p)
901 goto got_wrong_format_error;
902 break;
903 case ELFDATA2LSB: /* Little-endian */
904 if (abfd->xvec->header_byteorder_big_p)
905 goto got_wrong_format_error;
906 break;
907 case ELFDATANONE: /* No data encoding specified */
908 default: /* Unknown data encoding specified */
909 goto got_wrong_format_error;
910 }
911
912 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
913 the tdata pointer in the bfd. FIXME: memory leak, see above. */
914
915 elf_tdata (abfd) =
916 (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
917 if (elf_tdata (abfd) == NULL)
918 goto got_no_memory_error;
919
920 /* Now that we know the byte order, swap in the rest of the header */
921 i_ehdrp = elf_elfheader (abfd);
922 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
923 #if DEBUG & 1
924 elf_debug_file (i_ehdrp);
925 #endif
926
927 /* If there is no section header table, we're hosed. */
928 if (i_ehdrp->e_shoff == 0)
929 goto got_wrong_format_error;
930
931 /* As a simple sanity check, verify that the what BFD thinks is the
932 size of each section header table entry actually matches the size
933 recorded in the file. */
934 if (i_ehdrp->e_shentsize != sizeof (x_shdr))
935 goto got_wrong_format_error;
936
937 ebd = get_elf_backend_data (abfd);
938
939 /* Check that the ELF e_machine field matches what this particular
940 BFD format expects. */
941 if (ebd->elf_machine_code != i_ehdrp->e_machine)
942 {
943 bfd_target **target_ptr;
944
945 if (ebd->elf_machine_code != EM_NONE)
946 goto got_wrong_format_error;
947
948 /* This is the generic ELF target. Let it match any ELF target
949 for which we do not have a specific backend. */
950 for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
951 {
952 struct elf_backend_data *back;
953
954 if ((*target_ptr)->flavour != bfd_target_elf_flavour)
955 continue;
956 back = (struct elf_backend_data *) (*target_ptr)->backend_data;
957 if (back->elf_machine_code == i_ehdrp->e_machine)
958 {
959 /* target_ptr is an ELF backend which matches this
960 object file, so reject the generic ELF target. */
961 goto got_wrong_format_error;
962 }
963 }
964 }
965
966
967 /* Set the flags and architecture before calling the backend so that
968 it can override them. */
969 if (i_ehdrp->e_type == ET_EXEC)
970 abfd->flags |= EXEC_P;
971 else if (i_ehdrp->e_type == ET_DYN)
972 abfd->flags |= DYNAMIC;
973
974 bfd_default_set_arch_mach (abfd, ebd->arch, 0);
975
976 /* Remember the entry point specified in the ELF file header. */
977 bfd_get_start_address (abfd) = i_ehdrp->e_entry;
978
979 /* Let the backend double check the format and override global
980 information. */
981 if (ebd->elf_backend_object_p)
982 {
983 if ((*ebd->elf_backend_object_p) (abfd) == false)
984 goto got_wrong_format_error;
985 }
986
987 /* Allocate space for a copy of the section header table in
988 internal form, seek to the section header table in the file,
989 read it in, and convert it to internal form. */
990 i_shdrp = (Elf_Internal_Shdr *)
991 bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum);
992 elf_elfsections (abfd) =
993 (Elf_Internal_Shdr **) bfd_alloc (abfd, sizeof (i_shdrp) * i_ehdrp->e_shnum);
994 if (!i_shdrp || !elf_elfsections (abfd))
995 goto got_no_memory_error;
996 if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) == -1)
997 goto got_no_match;
998 for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++)
999 {
1000 if (bfd_read ((PTR) & x_shdr, sizeof x_shdr, 1, abfd) != sizeof (x_shdr))
1001 goto got_no_match;
1002 elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
1003 elf_elfsections (abfd)[shindex] = i_shdrp + shindex;
1004
1005 /* If this is a .dynamic section, mark the object file as being
1006 dynamically linked. */
1007 if (i_shdrp[shindex].sh_type == SHT_DYNAMIC)
1008 abfd->flags |= DYNAMIC;
1009 }
1010 if (i_ehdrp->e_shstrndx)
1011 {
1012 bfd_section_from_shdr (abfd, i_ehdrp->e_shstrndx);
1013 }
1014
1015 /* Read in the string table containing the names of the sections. We
1016 will need the base pointer to this table later. */
1017 /* We read this inline now, so that we don't have to go through
1018 bfd_section_from_shdr with it (since this particular strtab is
1019 used to find all of the ELF section names.) */
1020
1021 shstrtab = elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
1022 if (!shstrtab)
1023 goto got_wrong_format_error;
1024
1025 /* Once all of the section headers have been read and converted, we
1026 can start processing them. Note that the first section header is
1027 a dummy placeholder entry, so we ignore it.
1028
1029 We also watch for the symbol table section and remember the file
1030 offset and section size for both the symbol table section and the
1031 associated string table section. */
1032
1033 for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
1034 {
1035 bfd_section_from_shdr (abfd, shindex);
1036 }
1037
1038 return (abfd->xvec);
1039
1040 /* If we are going to use goto's to avoid duplicating error setting
1041 and return(NULL) code, then this at least makes it more maintainable. */
1042
1043 got_wrong_format_error:
1044 bfd_set_error (bfd_error_wrong_format);
1045 goto got_no_match;
1046 got_no_memory_error:
1047 bfd_set_error (bfd_error_no_memory);
1048 goto got_no_match;
1049 got_no_match:
1050 elf_tdata (abfd) = preserved_tdata;
1051 return (NULL);
1052 }
1053 \f
1054
1055 /* ELF .o/exec file writing */
1056
1057 /* Takes a bfd and a symbol, returns a pointer to the elf specific area
1058 of the symbol if there is one. */
1059 static INLINE elf_symbol_type *
1060 elf_symbol_from (ignore_abfd, symbol)
1061 bfd *ignore_abfd;
1062 asymbol *symbol;
1063 {
1064 if (symbol->the_bfd->xvec->flavour != bfd_target_elf_flavour)
1065 return 0;
1066
1067 if (symbol->the_bfd->tdata.elf_obj_data == (struct elf_obj_tdata *) NULL)
1068 return 0;
1069
1070 return (elf_symbol_type *) symbol;
1071 }
1072
1073 /* Create ELF output from BFD sections.
1074
1075 Essentially, just create the section header and forget about the program
1076 header for now. */
1077
1078 static void
1079 elf_make_sections (abfd, asect, obj)
1080 bfd *abfd;
1081 asection *asect;
1082 PTR obj;
1083 {
1084 /* most of what is in bfd_shdr_from_section goes in here... */
1085 /* and all of these sections generate at *least* one ELF section. */
1086 Elf_Internal_Shdr *this_hdr;
1087 this_hdr = &elf_section_data (asect)->this_hdr;
1088
1089 this_hdr->sh_addr = asect->vma;
1090 this_hdr->sh_size = asect->_raw_size;
1091 /* contents already set by elf_set_section_contents */
1092
1093 if (asect->flags & SEC_RELOC)
1094 {
1095 /* emit a reloc section, and thus strtab and symtab... */
1096 Elf_Internal_Shdr *rela_hdr;
1097 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1098
1099 rela_hdr = &elf_section_data (asect)->rel_hdr;
1100
1101 /* orelocation has the data, reloc_count has the count... */
1102 if (use_rela_p)
1103 {
1104 rela_hdr->sh_type = SHT_RELA;
1105 rela_hdr->sh_entsize = sizeof (Elf_External_Rela);
1106 }
1107 else
1108 /* REL relocations */
1109 {
1110 rela_hdr->sh_type = SHT_REL;
1111 rela_hdr->sh_entsize = sizeof (Elf_External_Rel);
1112 }
1113 rela_hdr->sh_flags = 0;
1114 rela_hdr->sh_addr = 0;
1115 rela_hdr->sh_offset = 0;
1116
1117 /* FIXME: Systems I've checked use an alignment of 4, but it is
1118 possible that some systems use a different alignment. */
1119 rela_hdr->sh_addralign = 4;
1120
1121 rela_hdr->size = 0;
1122 }
1123 if (asect->flags & SEC_ALLOC)
1124 {
1125 this_hdr->sh_flags |= SHF_ALLOC;
1126 if (asect->flags & SEC_LOAD)
1127 {
1128 /* @@ Do something with sh_type? */
1129 }
1130 }
1131 else
1132 {
1133 /* If this section is not part of the program image during
1134 execution, leave the address fields at 0. */
1135 this_hdr->sh_addr = 0;
1136 asect->vma = 0;
1137 }
1138 if (!(asect->flags & SEC_READONLY))
1139 this_hdr->sh_flags |= SHF_WRITE;
1140
1141 if (asect->flags & SEC_CODE)
1142 this_hdr->sh_flags |= SHF_EXECINSTR;
1143 }
1144
1145 void
1146 write_relocs (abfd, sec, xxx)
1147 bfd *abfd;
1148 asection *sec;
1149 PTR xxx;
1150 {
1151 Elf_Internal_Shdr *rela_hdr;
1152 Elf_External_Rela *outbound_relocas;
1153 Elf_External_Rel *outbound_relocs;
1154 int idx;
1155 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1156 asymbol *last_sym = 0;
1157 int last_sym_idx = 9999999; /* should always be written before use */
1158
1159 if ((sec->flags & SEC_RELOC) == 0)
1160 return;
1161 /* Flags are sometimes inconsistent. */
1162 if (sec->reloc_count == 0)
1163 return;
1164
1165 rela_hdr = &elf_section_data (sec)->rel_hdr;
1166
1167 rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
1168 rela_hdr->contents = (void *) bfd_alloc (abfd, rela_hdr->sh_size);
1169 if (!rela_hdr->contents)
1170 {
1171 bfd_set_error (bfd_error_no_memory);
1172 abort (); /* FIXME */
1173 }
1174
1175 /* orelocation has the data, reloc_count has the count... */
1176 if (use_rela_p)
1177 {
1178 outbound_relocas = (Elf_External_Rela *) rela_hdr->contents;
1179
1180 for (idx = 0; idx < sec->reloc_count; idx++)
1181 {
1182 Elf_Internal_Rela dst_rela;
1183 Elf_External_Rela *src_rela;
1184 arelent *ptr;
1185 asymbol *sym;
1186 int n;
1187
1188 ptr = sec->orelocation[idx];
1189 src_rela = outbound_relocas + idx;
1190 if (!(abfd->flags & EXEC_P))
1191 dst_rela.r_offset = ptr->address - sec->vma;
1192 else
1193 dst_rela.r_offset = ptr->address;
1194
1195 sym = *ptr->sym_ptr_ptr;
1196
1197 /* If SYM is a section symbol for an input section, which
1198 has been combined with other similar input sections (ld -r),
1199 then adjust the addend by the output_offset of sym->section.
1200
1201 Apparently elf_symbol_from_bfd_symbol doesn't provide a 1:1
1202 mapping from bfd symbol to elf symbols in this case. */
1203 if ((sym->flags & BSF_SECTION_SYM)
1204 && sym->section)
1205 ptr->addend += sym->section->output_offset;
1206
1207 if (sym == last_sym)
1208 n = last_sym_idx;
1209 else
1210 {
1211 last_sym = sym;
1212 last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
1213 }
1214 dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
1215
1216 dst_rela.r_addend = ptr->addend;
1217 elf_swap_reloca_out (abfd, &dst_rela, src_rela);
1218 }
1219 }
1220 else
1221 /* REL relocations */
1222 {
1223 outbound_relocs = (Elf_External_Rel *) rela_hdr->contents;
1224
1225 for (idx = 0; idx < sec->reloc_count; idx++)
1226 {
1227 Elf_Internal_Rel dst_rel;
1228 Elf_External_Rel *src_rel;
1229 arelent *ptr;
1230 int n;
1231 asymbol *sym;
1232
1233 ptr = sec->orelocation[idx];
1234 sym = *ptr->sym_ptr_ptr;
1235 src_rel = outbound_relocs + idx;
1236 if (!(abfd->flags & EXEC_P))
1237 dst_rel.r_offset = ptr->address - sec->vma;
1238 else
1239 dst_rel.r_offset = ptr->address;
1240
1241 if (sym == last_sym)
1242 n = last_sym_idx;
1243 else
1244 {
1245 last_sym = sym;
1246 last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
1247 }
1248 dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type);
1249
1250 elf_swap_reloc_out (abfd, &dst_rel, src_rel);
1251 }
1252 }
1253 }
1254
1255 static void
1256 fix_up_strtabs (abfd, asect, obj)
1257 bfd *abfd;
1258 asection *asect;
1259 PTR obj;
1260 {
1261 Elf_Internal_Shdr *this_hdr = &elf_section_data (asect)->this_hdr;
1262 int this_idx = elf_section_data (asect)->this_idx;
1263
1264 /* @@ Check flags! */
1265 if (!strncmp (asect->name, ".stab", 5)
1266 && !strcmp ("str", asect->name + strlen (asect->name) - 3))
1267 {
1268 size_t len = strlen (asect->name) + 1;
1269 char *s = (char *) malloc (len);
1270 if (s == NULL)
1271 /* FIXME: Should deal more gracefully with errors. */
1272 abort ();
1273 strcpy (s, asect->name);
1274 s[len - 4] = 0;
1275 asect = bfd_get_section_by_name (abfd, s);
1276 free (s);
1277 if (!asect)
1278 abort ();
1279 elf_section_data (asect)->this_hdr.sh_link = this_idx;
1280 /* @@ Assuming 32 bits! */
1281 elf_section_data (asect)->this_hdr.sh_entsize = 0xc;
1282
1283 this_hdr->sh_type = SHT_STRTAB;
1284 }
1285 }
1286
1287 static void
1288 elf_fake_sections (abfd, asect, obj)
1289 bfd *abfd;
1290 asection *asect;
1291 PTR obj;
1292 {
1293 /* most of what is in bfd_shdr_from_section goes in here... */
1294 /* and all of these sections generate at *least* one ELF section. */
1295
1296 Elf_Internal_Shdr *this_hdr;
1297 this_hdr = &elf_section_data (asect)->this_hdr;
1298 this_hdr->sh_name =
1299 bfd_add_to_strtab (abfd, elf_shstrtab (abfd), asect->name);
1300 /* We need to log the type *now* so that elf_section_from_bfd_section
1301 can find us... have to set rawdata too. */
1302 this_hdr->rawdata = (void *) asect;
1303 this_hdr->sh_addralign = 1 << asect->alignment_power;
1304 if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD))
1305 this_hdr->sh_type = SHT_PROGBITS;
1306 else if ((asect->flags & SEC_ALLOC) && ((asect->flags & SEC_LOAD) == 0))
1307 {
1308 BFD_ASSERT (strcmp (asect->name, ".bss") == 0
1309 || strcmp (asect->name, ".sbss") == 0);
1310 this_hdr->sh_type = SHT_NOBITS;
1311 }
1312 /* FIXME I am not sure how to detect a .note section from the flags
1313 word of an `asection'. */
1314 else if (!strcmp (asect->name, ".note"))
1315 this_hdr->sh_type = SHT_NOTE;
1316 else
1317 this_hdr->sh_type = SHT_PROGBITS;
1318
1319 this_hdr->sh_flags = 0;
1320 this_hdr->sh_addr = 0;
1321 this_hdr->sh_size = 0;
1322 this_hdr->sh_entsize = 0;
1323 this_hdr->sh_info = 0;
1324 this_hdr->sh_link = 0;
1325 this_hdr->sh_offset = 0;
1326 this_hdr->size = 0;
1327
1328 /* Now, check for processor-specific section types. */
1329 {
1330 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1331
1332 if (bed->elf_backend_fake_sections)
1333 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1334 }
1335
1336 {
1337 /* Emit a strtab and symtab, and possibly a reloc section. */
1338 Elf_Internal_Shdr *rela_hdr;
1339
1340 /* Note that only one symtab is used, so just remember it
1341 for now. */
1342
1343 if (asect->flags & SEC_RELOC)
1344 {
1345 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1346
1347 rela_hdr = &elf_section_data (asect)->rel_hdr;
1348 rela_hdr->sh_name =
1349 bfd_add_2_to_strtab (abfd, elf_shstrtab (abfd),
1350 use_rela_p ? ".rela" : ".rel",
1351 asect->name);
1352 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1353 rela_hdr->sh_entsize = (use_rela_p
1354 ? sizeof (Elf_External_Rela)
1355 : sizeof (Elf_External_Rel));
1356
1357 rela_hdr->sh_flags = 0;
1358 rela_hdr->sh_addr = 0;
1359 rela_hdr->sh_size = 0;
1360 rela_hdr->sh_offset = 0;
1361
1362 /* FIXME: Systems I've checked use an alignment of 4, but some
1363 systems may use a different alignment. */
1364 rela_hdr->sh_addralign = 4;
1365
1366 rela_hdr->size = 0;
1367 }
1368 }
1369 if (asect->flags & SEC_ALLOC)
1370 {
1371 this_hdr->sh_flags |= SHF_ALLOC;
1372 if (asect->flags & SEC_LOAD)
1373 {
1374 /* @@ Do something with sh_type? */
1375 }
1376 }
1377 if (!(asect->flags & SEC_READONLY))
1378 this_hdr->sh_flags |= SHF_WRITE;
1379 if (asect->flags & SEC_CODE)
1380 this_hdr->sh_flags |= SHF_EXECINSTR;
1381 }
1382
1383 /* Map symbol from it's internal number to the external number, moving
1384 all local symbols to be at the head of the list. */
1385
1386 static INLINE int
1387 sym_is_global (abfd, sym)
1388 bfd *abfd;
1389 asymbol *sym;
1390 {
1391 /* If the backend has a special mapping, use it. */
1392 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1393 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1394 (abfd, sym));
1395
1396 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
1397 {
1398 if (sym->flags & BSF_LOCAL)
1399 abort ();
1400 return 1;
1401 }
1402 if (sym->section == 0)
1403 {
1404 /* Is this valid? */
1405 abort ();
1406
1407 return 1;
1408 }
1409 if (sym->section == &bfd_und_section)
1410 return 1;
1411 if (bfd_is_com_section (sym->section))
1412 return 1;
1413 if (sym->flags & (BSF_LOCAL | BSF_SECTION_SYM | BSF_FILE))
1414 return 0;
1415 return 0;
1416 }
1417
1418 static boolean
1419 elf_map_symbols (abfd)
1420 bfd *abfd;
1421 {
1422 int symcount = bfd_get_symcount (abfd);
1423 asymbol **syms = bfd_get_outsymbols (abfd);
1424 asymbol **sect_syms;
1425 int num_locals = 0;
1426 int num_globals = 0;
1427 int num_locals2 = 0;
1428 int num_globals2 = 0;
1429 int max_index = 0;
1430 int num_sections = 0;
1431 Elf_Sym_Extra *sym_extra;
1432 int idx;
1433 asection *asect;
1434
1435 #ifdef DEBUG
1436 fprintf (stderr, "elf_map_symbols\n");
1437 fflush (stderr);
1438 #endif
1439
1440 /* Add local symbols for each section for which there are relocs.
1441 FIXME: How can we tell which sections have relocs at this point?
1442 Will reloc_count always be accurate? Actually, I think most ELF
1443 targets create section symbols for all sections anyhow. */
1444 for (asect = abfd->sections; asect; asect = asect->next)
1445 {
1446 if (max_index < asect->index)
1447 max_index = asect->index;
1448 }
1449
1450 max_index++;
1451 elf_num_section_syms (abfd) = max_index;
1452 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1453 elf_section_syms (abfd) = sect_syms;
1454
1455 if (sect_syms == 0)
1456 {
1457 bfd_set_error (bfd_error_no_memory);
1458 return false;
1459 }
1460
1461 for (asect = abfd->sections; asect; asect = asect->next)
1462 {
1463 asymbol *sym = bfd_make_empty_symbol (abfd);
1464 if (!sym)
1465 {
1466 bfd_set_error (bfd_error_no_memory);
1467 return false;
1468 }
1469 sym->the_bfd = abfd;
1470 sym->name = asect->name;
1471 sym->value = asect->vma;
1472 sym->flags = BSF_SECTION_SYM;
1473 sym->section = asect;
1474 sect_syms[asect->index] = sym;
1475 num_sections++;
1476 #ifdef DEBUG
1477 fprintf (stderr,
1478 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1479 asect->name, (long) asect->vma, asect->index, (long) asect);
1480 #endif
1481 }
1482
1483 if (num_sections)
1484 {
1485 if (syms)
1486 syms = (asymbol **) bfd_realloc (abfd, syms,
1487 ((symcount + num_sections + 1)
1488 * sizeof (asymbol *)));
1489 else
1490 syms = (asymbol **) bfd_alloc (abfd,
1491 (num_sections + 1) * sizeof (asymbol *));
1492 if (!syms)
1493 {
1494 bfd_set_error (bfd_error_no_memory);
1495 return false;
1496 }
1497
1498 for (asect = abfd->sections; asect; asect = asect->next)
1499 {
1500 if (sect_syms[asect->index])
1501 syms[symcount++] = sect_syms[asect->index];
1502 }
1503
1504 syms[symcount] = (asymbol *) 0;
1505 bfd_set_symtab (abfd, syms, symcount);
1506 }
1507
1508 elf_sym_extra (abfd) = sym_extra
1509 = (Elf_Sym_Extra *) bfd_alloc (abfd, symcount * sizeof (Elf_Sym_Extra));
1510 if (!sym_extra)
1511 {
1512 bfd_set_error (bfd_error_no_memory);
1513 return false;
1514 }
1515
1516 /* Identify and classify all of the symbols. */
1517 for (idx = 0; idx < symcount; idx++)
1518 {
1519 if (!sym_is_global (abfd, syms[idx]))
1520 num_locals++;
1521 else
1522 num_globals++;
1523 }
1524
1525 /* Now provide mapping information. Add +1 for skipping over the
1526 dummy symbol. */
1527 for (idx = 0; idx < symcount; idx++)
1528 {
1529 syms[idx]->udata = (PTR) & sym_extra[idx];
1530 if (!sym_is_global (abfd, syms[idx]))
1531 sym_extra[idx].elf_sym_num = 1 + num_locals2++;
1532 else
1533 sym_extra[idx].elf_sym_num = 1 + num_locals + num_globals2++;
1534 }
1535
1536 elf_num_locals (abfd) = num_locals;
1537 elf_num_globals (abfd) = num_globals;
1538 return true;
1539 }
1540
1541 static boolean assign_section_numbers ();
1542 static boolean assign_file_positions_except_relocs ();
1543
1544 static boolean
1545 elf_compute_section_file_positions (abfd)
1546 bfd *abfd;
1547 {
1548 bfd_map_over_sections (abfd, elf_fake_sections, 0);
1549
1550 if (!assign_section_numbers (abfd))
1551 return false;
1552
1553 bfd_map_over_sections (abfd, elf_make_sections, 0);
1554
1555 bfd_map_over_sections (abfd, fix_up_strtabs, 0); /* .stab/.stabstr &c */
1556
1557 if (swap_out_syms (abfd) == false)
1558 return false;
1559
1560 if (!assign_file_positions_except_relocs (abfd))
1561 return false;
1562
1563 return true;
1564 }
1565
1566 static boolean
1567 elf_write_phdrs (abfd, i_ehdrp, i_phdrp, phdr_cnt)
1568 bfd *abfd;
1569 Elf_Internal_Ehdr *i_ehdrp;
1570 Elf_Internal_Phdr *i_phdrp;
1571 unsigned short phdr_cnt;
1572 {
1573 /* first program header entry goes after the file header */
1574 int outbase = i_ehdrp->e_phoff;
1575 unsigned int i;
1576 Elf_External_Phdr x_phdr;
1577
1578 for (i = 0; i < phdr_cnt; i++)
1579 {
1580 elf_swap_phdr_out (abfd, i_phdrp + i, &x_phdr);
1581 if (bfd_seek (abfd, outbase, SEEK_SET) != 0
1582 || (bfd_write ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd)
1583 != sizeof (x_phdr)))
1584 return false;
1585 outbase += sizeof (x_phdr);
1586 }
1587
1588 return true;
1589 }
1590
1591 static const Elf_Internal_Shdr null_shdr;
1592
1593 /* Assign all ELF section numbers. The dummy first section is handled here
1594 too. The link/info pointers for the standard section types are filled
1595 in here too, while we're at it. (Link pointers for .stab sections are
1596 not filled in here.) */
1597 static boolean
1598 assign_section_numbers (abfd)
1599 bfd *abfd;
1600 {
1601 struct elf_obj_tdata *t = elf_tdata (abfd);
1602 asection *sec;
1603 int section_number = 1;
1604 int i;
1605 Elf_Internal_Shdr **i_shdrp;
1606
1607 t->shstrtab_hdr.sh_size = elf_shstrtab (abfd)->length;
1608 t->shstrtab_hdr.contents = (void *) elf_shstrtab (abfd)->tab;
1609 shstrtab_length_fixed = 1;
1610
1611 t->shstrtab_section = section_number++;
1612 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1613 if (abfd->symcount)
1614 {
1615 t->symtab_section = section_number++;
1616 t->strtab_section = section_number++;
1617 t->symtab_hdr.sh_link = t->strtab_section;
1618 }
1619 for (sec = abfd->sections; sec; sec = sec->next)
1620 {
1621 struct bfd_elf_section_data *d = elf_section_data (sec);
1622 d->this_idx = section_number++;
1623 if (sec->flags & SEC_RELOC)
1624 {
1625 d->rel_idx = section_number++;
1626 d->rel_hdr.sh_link = t->symtab_section;
1627 d->rel_hdr.sh_info = d->this_idx;
1628 }
1629 else
1630 d->rel_idx = 0;
1631 /* No handling for per-section string tables currently. */
1632 }
1633 elf_elfheader (abfd)->e_shnum = section_number;
1634
1635 /* Set up the list of section header pointers, in agreement with the
1636 indices. */
1637 i_shdrp = (Elf_Internal_Shdr **)
1638 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *));
1639 if (!i_shdrp)
1640 {
1641 bfd_set_error (bfd_error_no_memory);
1642 return false;
1643 }
1644 elf_elfsections (abfd) = i_shdrp;
1645 for (i = 0; i < section_number; i++)
1646 i_shdrp[i] = 0;
1647
1648 i_shdrp[0] = (Elf_Internal_Shdr *) & null_shdr;
1649 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1650 if (abfd->symcount)
1651 {
1652 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1653 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1654 }
1655 for (sec = abfd->sections; sec; sec = sec->next)
1656 {
1657 struct bfd_elf_section_data *d = elf_section_data (sec);
1658 i_shdrp[d->this_idx] = &d->this_hdr;
1659 if (d->rel_idx)
1660 i_shdrp[d->rel_idx] = &d->rel_hdr;
1661 }
1662 /* Make sure we got everything.... */
1663 for (i = 0; i < section_number; i++)
1664 if (i_shdrp[i] == 0)
1665 abort ();
1666 return true;
1667 }
1668
1669 static INLINE file_ptr
1670 assign_file_position_for_section (i_shdrp, offset)
1671 Elf_Internal_Shdr *i_shdrp;
1672 file_ptr offset;
1673 {
1674 int align;
1675
1676 if (i_shdrp->sh_addralign != 0)
1677 align = i_shdrp->sh_addralign;
1678 else
1679 align = 1;
1680 i_shdrp->sh_offset = offset = BFD_ALIGN (offset, align);
1681 if (i_shdrp->rawdata != NULL)
1682 ((asection *) i_shdrp->rawdata)->filepos = offset;
1683 if (i_shdrp->sh_type != SHT_NOBITS)
1684 offset += i_shdrp->sh_size;
1685 return offset;
1686 }
1687
1688 static INLINE file_ptr
1689 align_file_position (off)
1690 file_ptr off;
1691 {
1692 return (off + FILE_ALIGN - 1) & ~(FILE_ALIGN - 1);
1693 }
1694
1695 static INLINE file_ptr
1696 assign_file_positions_for_symtab_and_strtabs (abfd, off)
1697 bfd *abfd;
1698 file_ptr off;
1699 {
1700 struct elf_obj_tdata *t = elf_tdata (abfd);
1701
1702 off = align_file_position (off);
1703 off = assign_file_position_for_section (&t->symtab_hdr, off);
1704 off = assign_file_position_for_section (&t->shstrtab_hdr, off);
1705 off = assign_file_position_for_section (&t->strtab_hdr, off);
1706 return off;
1707 }
1708
1709 struct seg_info
1710 {
1711 bfd_vma low, mem_size;
1712 file_ptr file_size;
1713 int start_pos;
1714 int sh_flags;
1715 struct seg_info *next;
1716 };
1717
1718 static boolean
1719 map_program_segments (abfd)
1720 bfd *abfd;
1721 {
1722 Elf_Internal_Shdr **i_shdrpp = elf_elfsections (abfd);
1723 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
1724 Elf_Internal_Shdr *i_shdrp;
1725 Elf_Internal_Phdr *phdr;
1726 char *done = NULL;
1727 unsigned int i, n_left = 0;
1728 file_ptr lowest_offset = 0;
1729 struct seg_info *seg = NULL;
1730
1731 done = (char *) malloc (i_ehdrp->e_shnum);
1732 if (done == NULL && i_ehdrp->e_shnum != 0)
1733 {
1734 bfd_set_error (bfd_error_no_memory);
1735 goto error_return;
1736 }
1737 memset (done, 0, i_ehdrp->e_shnum);
1738 for (i = 1; i < i_ehdrp->e_shnum; i++)
1739 {
1740 i_shdrp = i_shdrpp[i];
1741 /* If it's going to be mapped in, it's been assigned a position. */
1742 if (i_shdrp->sh_offset + 1 == 0)
1743 {
1744 /* Well, not really, but we won't process it here. */
1745 done[i] = 1;
1746 continue;
1747 }
1748 if (i_shdrp->sh_offset < lowest_offset
1749 || lowest_offset == 0)
1750 lowest_offset = i_shdrp->sh_offset;
1751 /* Only interested in PROGBITS or NOBITS for generating segments. */
1752 switch (i_shdrp->sh_type)
1753 {
1754 case SHT_PROGBITS:
1755 case SHT_NOBITS:
1756 break;
1757 default:
1758 done[i] = 1;
1759 }
1760 if (!done[i])
1761 n_left++;
1762 }
1763 while (n_left)
1764 {
1765 bfd_vma lowest_vma = -1, high;
1766 int low_sec = 0;
1767 int mem_size;
1768 int file_size = 0;
1769 struct seg_info *snew;
1770 struct seg_info **s_ptr;
1771
1772 for (i = 1; i < i_ehdrp->e_shnum; i++)
1773 {
1774 i_shdrp = i_shdrpp[i];
1775 if (!done[i] && i_shdrp->sh_addr < lowest_vma)
1776 {
1777 lowest_vma = i_shdrp->sh_addr;
1778 low_sec = i;
1779 }
1780 }
1781 if (low_sec == 0)
1782 abort ();
1783 /* So now we know the lowest vma of any unassigned sections; start
1784 a segment there. */
1785 snew = (struct seg_info *) bfd_alloc (abfd, sizeof (struct seg_info));
1786 if (!snew)
1787 {
1788 bfd_set_error (bfd_error_no_memory);
1789 goto error_return;
1790 }
1791 s_ptr = &seg;
1792 while (*s_ptr != (struct seg_info *) NULL)
1793 s_ptr = &(*s_ptr)->next;
1794 *s_ptr = snew;
1795 snew->next = NULL;
1796 snew->low = lowest_vma;
1797 i_shdrp = i_shdrpp[low_sec];
1798 snew->start_pos = i_shdrp->sh_offset;
1799 snew->sh_flags = i_shdrp->sh_flags;
1800 done[low_sec] = 1, n_left--;
1801 mem_size = i_shdrp->sh_size;
1802 high = lowest_vma + i_shdrp->sh_size;
1803
1804 if (i_shdrp->sh_type == SHT_PROGBITS)
1805 file_size = i_shdrp->sh_size;
1806
1807 for (i = 1; i < i_ehdrp->e_shnum; i++)
1808 {
1809 file_ptr f1;
1810
1811 if (done[i])
1812 continue;
1813 i_shdrp = i_shdrpp[i];
1814 /* position of next byte on disk */
1815 f1 = snew->start_pos + file_size;
1816 if (i_shdrp->sh_type == SHT_PROGBITS)
1817 {
1818 if (i_shdrp->sh_offset - f1 != i_shdrp->sh_addr - high)
1819 continue;
1820 if (file_size != mem_size)
1821 break;
1822 }
1823 else
1824 /* sh_type == NOBITS */
1825 {
1826 /* If the section in question has no contents in the disk
1827 file, we really don't care where it supposedly starts.
1828 But we don't want to bother merging it into this segment
1829 if it doesn't start on this memory page. */
1830 bfd_vma page1, page2;
1831 bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1832
1833 /* page number in address space of current end of snew */
1834 page1 = (high - 1 + maxpagesize - 1) / maxpagesize;
1835 /* page number in address space of start of this section */
1836 page2 = (i_shdrp->sh_addr + maxpagesize - 1) / maxpagesize;
1837
1838 if (page1 != page2)
1839 continue;
1840 }
1841 done[i] = 1, n_left--;
1842 if (i_shdrp->sh_type == SHT_PROGBITS)
1843 file_size = i_shdrp->sh_offset + i_shdrp->sh_size - snew->start_pos;
1844 mem_size = i_shdrp->sh_addr + i_shdrp->sh_size - snew->low;
1845 high = i_shdrp->sh_addr + i_shdrp->sh_size;
1846 i = 0;
1847 }
1848 snew->file_size = file_size;
1849 snew->mem_size = mem_size;
1850 }
1851 /* Now do something with the list of segments we've built up. */
1852 {
1853 bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1854 struct seg_info *s;
1855 int n_segs = 0;
1856 int sz;
1857
1858 for (s = seg; s; s = s->next)
1859 {
1860 n_segs++;
1861 }
1862 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
1863 sz = sizeof (Elf_External_Phdr) * n_segs;
1864 if (align_file_position (i_ehdrp->e_ehsize) + sz <= lowest_offset)
1865 i_ehdrp->e_phoff = align_file_position (i_ehdrp->e_ehsize);
1866 else
1867 {
1868 i_ehdrp->e_phoff = align_file_position (elf_tdata (abfd)->next_file_pos);
1869 elf_tdata (abfd)->next_file_pos = i_ehdrp->e_phoff + sz;
1870 }
1871 phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd,
1872 n_segs * sizeof (Elf_Internal_Phdr));
1873 if (!phdr)
1874 {
1875 bfd_set_error (bfd_error_no_memory);
1876 abort (); /* FIXME */
1877 }
1878 elf_tdata (abfd)->phdr = phdr;
1879 while (seg)
1880 {
1881 phdr->p_type = PT_LOAD; /* only type we really support so far */
1882 phdr->p_offset = seg->start_pos;
1883 phdr->p_vaddr = seg->low;
1884 phdr->p_paddr = 0;
1885 phdr->p_filesz = seg->file_size;
1886 phdr->p_memsz = seg->mem_size;
1887 phdr->p_flags = PF_R;
1888 phdr->p_align = maxpagesize; /* ? */
1889 if (seg->sh_flags & SHF_WRITE)
1890 /* SysVr4 ELF docs say "data segments normally have read, write,
1891 and execute permissions." */
1892 phdr->p_flags |= (PF_W | PF_X);
1893 if (seg->sh_flags & SHF_EXECINSTR)
1894 phdr->p_flags |= PF_X;
1895 phdr++;
1896 seg = seg->next;
1897 }
1898 i_ehdrp->e_phnum = n_segs;
1899 }
1900 elf_write_phdrs (abfd, i_ehdrp, elf_tdata (abfd)->phdr, i_ehdrp->e_phnum);
1901 if (done != NULL)
1902 free (done);
1903 return true;
1904 error_return:
1905 if (done != NULL)
1906 free (done);
1907 return false;
1908 }
1909
1910 static boolean
1911 assign_file_positions_except_relocs (abfd)
1912 bfd *abfd;
1913 {
1914 /* For now, we ignore the possibility of having program segments, which
1915 may require some alignment in the file. That'll require padding, and
1916 some interesting calculations to optimize file space usage.
1917
1918 Also, since the application may change the list of relocations for
1919 a given section, we don't figure them in here. We'll put them at the
1920 end of the file, at positions computed during bfd_close.
1921
1922 The order, for now: <ehdr> <shdr> <sec1> <sec2> <sec3> ... <rel1> ...
1923 or: <ehdr> <phdr> <sec1> <sec2> ... <shdr> <rel1> ... */
1924
1925 struct elf_obj_tdata *t = elf_tdata (abfd);
1926 file_ptr off;
1927 unsigned int i;
1928 Elf_Internal_Shdr **i_shdrpp = elf_elfsections (abfd);
1929 Elf_Internal_Shdr *i_shdrp;
1930 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
1931 int exec_p = (abfd->flags & EXEC_P) != 0;
1932 bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1933
1934 /* Everything starts after the ELF file header. */
1935 off = i_ehdrp->e_ehsize;
1936
1937 if (!exec_p)
1938 {
1939 /* Section headers. */
1940 off = align_file_position (off);
1941 i_ehdrp->e_shoff = off;
1942 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
1943 off = assign_file_positions_for_symtab_and_strtabs (abfd, off);
1944 }
1945 for (i = 1; i < i_ehdrp->e_shnum; i++)
1946 {
1947 /* The symtab and strtab sections are placed by
1948 assign_file_positions_for_symtab_and_strtabs. */
1949 if (i == t->symtab_section
1950 || i == t->strtab_section
1951 || i == t->shstrtab_section)
1952 continue;
1953
1954 i_shdrp = i_shdrpp[i];
1955 if (i_shdrp->sh_type == SHT_REL || i_shdrp->sh_type == SHT_RELA)
1956 {
1957 i_shdrp->sh_offset = -1;
1958 continue;
1959 }
1960 if (exec_p)
1961 {
1962 if (maxpagesize == 0)
1963 maxpagesize = 1; /* make the arithmetic work */
1964 /* This isn't necessarily going to give the best packing, if the
1965 segments require padding between them, but since that isn't
1966 usually the case, this'll do. */
1967 if ((i_shdrp->sh_flags & SHF_ALLOC) == 0)
1968 {
1969 i_shdrp->sh_offset = -1;
1970 continue;
1971 }
1972 /* Blindly assume that the segments are ordered optimally. With
1973 the default LD script, they will be. */
1974 if (i_shdrp->sh_type != SHT_NOBITS)
1975 {
1976 /* need big unsigned type */
1977 bfd_vma addtl_off;
1978 addtl_off = i_shdrp->sh_addr - off;
1979 addtl_off = addtl_off % maxpagesize;
1980 if (addtl_off)
1981 {
1982 off += addtl_off;
1983 }
1984 }
1985 }
1986 off = assign_file_position_for_section (i_shdrp, off);
1987
1988 if (exec_p
1989 && i_shdrp->sh_type == SHT_NOBITS
1990 && (i == i_ehdrp->e_shnum
1991 || i_shdrpp[i + 1]->sh_type != SHT_NOBITS))
1992 {
1993 /* Skip to the next page to ensure that when the file is
1994 loaded the bss section is loaded with zeroes. I don't
1995 know if this is required on all platforms, but it
1996 shouldn't really hurt. */
1997 off = BFD_ALIGN (off, maxpagesize);
1998 }
1999
2000 if (exec_p
2001 && (abfd->flags & D_PAGED) != 0
2002 && get_elf_backend_data (abfd)->maxpagesize > 1
2003 && i_shdrp->sh_type == SHT_PROGBITS
2004 && (i_shdrp->sh_flags & SHF_ALLOC)
2005 && (i_shdrp->sh_offset - i_shdrp->sh_addr) % get_elf_backend_data (abfd)->maxpagesize != 0)
2006 abort ();
2007 }
2008 if (exec_p)
2009 {
2010 elf_tdata (abfd)->next_file_pos = off;
2011 if (!map_program_segments (abfd))
2012 return false;
2013 off = elf_tdata (abfd)->next_file_pos;
2014
2015 /* Section headers. */
2016 off = align_file_position (off);
2017 i_ehdrp->e_shoff = off;
2018 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2019
2020 off = assign_file_positions_for_symtab_and_strtabs (abfd, off);
2021
2022 for (i = 1; i < i_ehdrp->e_shnum; i++)
2023 {
2024 i_shdrp = i_shdrpp[i];
2025 if (i_shdrp->sh_offset + 1 == 0
2026 && i_shdrp->sh_type != SHT_REL
2027 && i_shdrp->sh_type != SHT_RELA)
2028 off = assign_file_position_for_section (i_shdrp, off);
2029 }
2030 }
2031 elf_tdata (abfd)->next_file_pos = off;
2032 return true;
2033 }
2034
2035 static boolean
2036 prep_headers (abfd)
2037 bfd *abfd;
2038 {
2039 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2040 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2041 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2042 int count;
2043 struct strtab *shstrtab;
2044
2045 i_ehdrp = elf_elfheader (abfd);
2046 i_shdrp = elf_elfsections (abfd);
2047
2048 shstrtab = bfd_new_strtab (abfd);
2049 if (!shstrtab)
2050 return false;
2051
2052 elf_shstrtab (abfd) = shstrtab;
2053
2054 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2055 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2056 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2057 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2058
2059 i_ehdrp->e_ident[EI_CLASS] = ELFCLASS;
2060 i_ehdrp->e_ident[EI_DATA] =
2061 abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB;
2062 i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT;
2063
2064 for (count = EI_PAD; count < EI_NIDENT; count++)
2065 i_ehdrp->e_ident[count] = 0;
2066
2067 i_ehdrp->e_type = (abfd->flags & EXEC_P) ? ET_EXEC : ET_REL;
2068 switch (bfd_get_arch (abfd))
2069 {
2070 case bfd_arch_unknown:
2071 i_ehdrp->e_machine = EM_NONE;
2072 break;
2073 case bfd_arch_sparc:
2074 i_ehdrp->e_machine = EM_SPARC;
2075 /* start-sanitize-v9 */
2076 #if ARCH_SIZE == 64
2077 i_ehdrp->e_machine = EM_SPARC64;
2078 #endif
2079 /* end-sanitize-v9 */
2080 break;
2081 case bfd_arch_i386:
2082 i_ehdrp->e_machine = EM_386;
2083 break;
2084 case bfd_arch_m68k:
2085 i_ehdrp->e_machine = EM_68K;
2086 break;
2087 case bfd_arch_m88k:
2088 i_ehdrp->e_machine = EM_88K;
2089 break;
2090 case bfd_arch_i860:
2091 i_ehdrp->e_machine = EM_860;
2092 break;
2093 case bfd_arch_mips: /* MIPS Rxxxx */
2094 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
2095 break;
2096 case bfd_arch_hppa:
2097 i_ehdrp->e_machine = EM_HPPA;
2098 break;
2099 case bfd_arch_powerpc:
2100 i_ehdrp->e_machine = EM_CYGNUS_POWERPC;
2101 break;
2102 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2103 default:
2104 i_ehdrp->e_machine = EM_NONE;
2105 }
2106 i_ehdrp->e_version = EV_CURRENT;
2107 i_ehdrp->e_ehsize = sizeof (Elf_External_Ehdr);
2108
2109 /* no program header, for now. */
2110 i_ehdrp->e_phoff = 0;
2111 i_ehdrp->e_phentsize = 0;
2112 i_ehdrp->e_phnum = 0;
2113
2114 /* each bfd section is section header entry */
2115 i_ehdrp->e_entry = bfd_get_start_address (abfd);
2116 i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr);
2117
2118 /* if we're building an executable, we'll need a program header table */
2119 if (abfd->flags & EXEC_P)
2120 {
2121 /* it all happens later */
2122 #if 0
2123 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2124
2125 /* elf_build_phdrs() returns a (NULL-terminated) array of
2126 Elf_Internal_Phdrs */
2127 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2128 i_ehdrp->e_phoff = outbase;
2129 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2130 #endif
2131 }
2132 else
2133 {
2134 i_ehdrp->e_phentsize = 0;
2135 i_phdrp = 0;
2136 i_ehdrp->e_phoff = 0;
2137 }
2138
2139 elf_tdata (abfd)->symtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2140 ".symtab");
2141 elf_tdata (abfd)->strtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2142 ".strtab");
2143 elf_tdata (abfd)->shstrtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2144 ".shstrtab");
2145 return true;
2146 }
2147
2148 static boolean
2149 swap_out_syms (abfd)
2150 bfd *abfd;
2151 {
2152 if (!elf_map_symbols (abfd))
2153 return false;
2154
2155 /* Dump out the symtabs. */
2156 {
2157 int symcount = bfd_get_symcount (abfd);
2158 asymbol **syms = bfd_get_outsymbols (abfd);
2159 struct strtab *stt = bfd_new_strtab (abfd);
2160 Elf_Internal_Shdr *symtab_hdr;
2161 Elf_Internal_Shdr *symstrtab_hdr;
2162 Elf_External_Sym *outbound_syms;
2163 int idx;
2164
2165 if (!stt)
2166 return false;
2167 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2168 symtab_hdr->sh_type = SHT_SYMTAB;
2169 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
2170 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
2171 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
2172
2173 /* FIXME: Systems I've checked use 4 byte alignment for .symtab,
2174 but it is possible that there are systems which use a different
2175 alignment. */
2176 symtab_hdr->sh_addralign = 4;
2177
2178 /* see assert in elf_fake_sections that supports this: */
2179 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
2180 symstrtab_hdr->sh_type = SHT_STRTAB;
2181
2182 outbound_syms = (Elf_External_Sym *)
2183 bfd_alloc (abfd, (1 + symcount) * sizeof (Elf_External_Sym));
2184 if (!outbound_syms)
2185 {
2186 bfd_set_error (bfd_error_no_memory);
2187 return false;
2188 }
2189 /* now generate the data (for "contents") */
2190 {
2191 /* Fill in zeroth symbol and swap it out. */
2192 Elf_Internal_Sym sym;
2193 sym.st_name = 0;
2194 sym.st_value = 0;
2195 sym.st_size = 0;
2196 sym.st_info = 0;
2197 sym.st_other = 0;
2198 sym.st_shndx = SHN_UNDEF;
2199 elf_swap_symbol_out (abfd, &sym, outbound_syms);
2200 }
2201 for (idx = 0; idx < symcount; idx++)
2202 {
2203 Elf_Internal_Sym sym;
2204 bfd_vma value = syms[idx]->value;
2205
2206 if (syms[idx]->flags & BSF_SECTION_SYM)
2207 /* Section symbols have no names. */
2208 sym.st_name = 0;
2209 else
2210 sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name);
2211
2212 if (bfd_is_com_section (syms[idx]->section))
2213 {
2214 /* ELF common symbols put the alignment into the `value' field,
2215 and the size into the `size' field. This is backwards from
2216 how BFD handles it, so reverse it here. */
2217 sym.st_size = value;
2218 /* Should retrieve this from somewhere... */
2219 sym.st_value = 16;
2220 sym.st_shndx = elf_section_from_bfd_section (abfd,
2221 syms[idx]->section);
2222 }
2223 else
2224 {
2225 asection *sec = syms[idx]->section;
2226 elf_symbol_type *type_ptr;
2227 int shndx;
2228
2229 if (sec->output_section)
2230 {
2231 value += sec->output_offset;
2232 sec = sec->output_section;
2233 }
2234 value += sec->vma;
2235 sym.st_value = value;
2236 type_ptr = elf_symbol_from (abfd, syms[idx]);
2237 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
2238 sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec);
2239 if (shndx == -1)
2240 {
2241 asection *sec2;
2242 /* Writing this would be a hell of a lot easier if we had
2243 some decent documentation on bfd, and knew what to expect
2244 of the library, and what to demand of applications. For
2245 example, it appears that `objcopy' might not set the
2246 section of a symbol to be a section that is actually in
2247 the output file. */
2248 sec2 = bfd_get_section_by_name (abfd, sec->name);
2249 BFD_ASSERT (sec2 != 0);
2250 sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec2);
2251 BFD_ASSERT (shndx != -1);
2252 }
2253 }
2254
2255 if (bfd_is_com_section (syms[idx]->section))
2256 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT);
2257 else if (syms[idx]->section == &bfd_und_section)
2258 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_NOTYPE);
2259 else if (syms[idx]->flags & BSF_SECTION_SYM)
2260 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
2261 else if (syms[idx]->flags & BSF_FILE)
2262 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
2263 else
2264 {
2265 int bind = STB_LOCAL;
2266 int type = STT_OBJECT;
2267 unsigned int flags = syms[idx]->flags;
2268
2269 if (flags & BSF_LOCAL)
2270 bind = STB_LOCAL;
2271 else if (flags & BSF_WEAK)
2272 bind = STB_WEAK;
2273 else if (flags & BSF_GLOBAL)
2274 bind = STB_GLOBAL;
2275
2276 if (flags & BSF_FUNCTION)
2277 type = STT_FUNC;
2278
2279 sym.st_info = ELF_ST_INFO (bind, type);
2280 }
2281
2282 sym.st_other = 0;
2283 elf_swap_symbol_out (abfd, &sym,
2284 (outbound_syms
2285 + elf_sym_extra (abfd)[idx].elf_sym_num));
2286 }
2287
2288 symtab_hdr->contents = (PTR) outbound_syms;
2289 symstrtab_hdr->contents = (PTR) stt->tab;
2290 symstrtab_hdr->sh_size = stt->length;
2291 symstrtab_hdr->sh_type = SHT_STRTAB;
2292
2293 symstrtab_hdr->sh_flags = 0;
2294 symstrtab_hdr->sh_addr = 0;
2295 symstrtab_hdr->sh_entsize = 0;
2296 symstrtab_hdr->sh_link = 0;
2297 symstrtab_hdr->sh_info = 0;
2298 symstrtab_hdr->sh_addralign = 1;
2299 symstrtab_hdr->size = 0;
2300 }
2301
2302 /* put the strtab out too... */
2303 {
2304 Elf_Internal_Shdr *this_hdr;
2305
2306 this_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2307 this_hdr->contents = (PTR) elf_shstrtab (abfd)->tab;
2308 this_hdr->sh_size = elf_shstrtab (abfd)->length;
2309 this_hdr->sh_type = SHT_STRTAB;
2310 this_hdr->sh_flags = 0;
2311 this_hdr->sh_addr = 0;
2312 this_hdr->sh_entsize = 0;
2313 this_hdr->sh_addralign = 1;
2314 this_hdr->size = 0;
2315 }
2316 return true;
2317 }
2318
2319 static boolean
2320 write_shdrs_and_ehdr (abfd)
2321 bfd *abfd;
2322 {
2323 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
2324 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2325 Elf_External_Shdr *x_shdrp; /* Section header table, external form */
2326 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2327 unsigned int count;
2328 struct strtab *shstrtab;
2329
2330 i_ehdrp = elf_elfheader (abfd);
2331 i_shdrp = elf_elfsections (abfd);
2332 shstrtab = elf_shstrtab (abfd);
2333
2334 /* swap the header before spitting it out... */
2335
2336 #if DEBUG & 1
2337 elf_debug_file (i_ehdrp);
2338 #endif
2339 elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
2340 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2341 || (bfd_write ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd)
2342 != sizeof (x_ehdr)))
2343 return false;
2344
2345 /* at this point we've concocted all the ELF sections... */
2346 x_shdrp = (Elf_External_Shdr *)
2347 bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
2348 if (!x_shdrp)
2349 {
2350 bfd_set_error (bfd_error_no_memory);
2351 return false;
2352 }
2353
2354 for (count = 0; count < i_ehdrp->e_shnum; count++)
2355 {
2356 #if DEBUG & 2
2357 elf_debug_section (shstrtab->tab + i_shdrp[count]->sh_name, count,
2358 i_shdrp[count]);
2359 #endif
2360 elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count);
2361 }
2362 if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0
2363 || (bfd_write ((PTR) x_shdrp, sizeof (*x_shdrp), i_ehdrp->e_shnum, abfd)
2364 != sizeof (*x_shdrp) * i_ehdrp->e_shnum))
2365 return false;
2366
2367 /* need to dump the string table too... */
2368
2369 return true;
2370 }
2371
2372 static void
2373 assign_file_positions_for_relocs (abfd)
2374 bfd *abfd;
2375 {
2376 file_ptr off = elf_tdata (abfd)->next_file_pos;
2377 unsigned int i;
2378 Elf_Internal_Shdr **shdrpp = elf_elfsections (abfd);
2379 Elf_Internal_Shdr *shdrp;
2380 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
2381 {
2382 shdrp = shdrpp[i];
2383 if (shdrp->sh_type != SHT_REL && shdrp->sh_type != SHT_RELA)
2384 continue;
2385 off = align_file_position (off);
2386 off = assign_file_position_for_section (shdrp, off);
2387 }
2388 elf_tdata (abfd)->next_file_pos = off;
2389 }
2390
2391 boolean
2392 NAME(bfd_elf,write_object_contents) (abfd)
2393 bfd *abfd;
2394 {
2395 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2396 Elf_Internal_Ehdr *i_ehdrp;
2397 Elf_Internal_Shdr **i_shdrp;
2398 unsigned int count;
2399
2400 /* We don't know how to write dynamic objects. Specifically, we
2401 don't know how to construct the program header. */
2402 if ((abfd->flags & DYNAMIC) != 0)
2403 {
2404 fprintf (stderr, "Writing ELF dynamic objects is not supported\n");
2405 bfd_set_error (bfd_error_wrong_format);
2406 return false;
2407 }
2408
2409 if (abfd->output_has_begun == false)
2410 {
2411 /* Do any elf backend specific processing first. */
2412 if (bed->elf_backend_begin_write_processing)
2413 (*bed->elf_backend_begin_write_processing) (abfd);
2414
2415 if (prep_headers (abfd) == false)
2416 return false;
2417 if (elf_compute_section_file_positions (abfd) == false)
2418 return false;
2419 abfd->output_has_begun = true;
2420 }
2421
2422 i_shdrp = elf_elfsections (abfd);
2423 i_ehdrp = elf_elfheader (abfd);
2424
2425 bfd_map_over_sections (abfd, write_relocs, (PTR) 0);
2426 assign_file_positions_for_relocs (abfd);
2427
2428 /* After writing the headers, we need to write the sections too... */
2429 for (count = 1; count < i_ehdrp->e_shnum; count++)
2430 {
2431 if (bed->elf_backend_section_processing)
2432 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
2433 if (i_shdrp[count]->contents)
2434 {
2435 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
2436 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
2437 1, abfd)
2438 != i_shdrp[count]->sh_size))
2439 return false;
2440 }
2441 }
2442
2443 if (bed->elf_backend_final_write_processing)
2444 (*bed->elf_backend_final_write_processing) (abfd);
2445
2446 return write_shdrs_and_ehdr (abfd);
2447 }
2448
2449 /* Given an index of a section, retrieve a pointer to it. Note
2450 that for our purposes, sections are indexed by {1, 2, ...} with
2451 0 being an illegal index. */
2452
2453 /* In the original, each ELF section went into exactly one BFD
2454 section. This doesn't really make sense, so we need a real mapping.
2455 The mapping has to hide in the Elf_Internal_Shdr since asection
2456 doesn't have anything like a tdata field... */
2457
2458 static struct sec *
2459 section_from_elf_index (abfd, index)
2460 bfd *abfd;
2461 unsigned int index;
2462 {
2463 /* @@ Is bfd_com_section really correct in all the places it could
2464 be returned from this routine? */
2465
2466 if (index == SHN_ABS)
2467 return &bfd_com_section; /* not abs? */
2468 if (index == SHN_COMMON)
2469 return &bfd_com_section;
2470
2471 if (index > elf_elfheader (abfd)->e_shnum)
2472 return 0;
2473
2474 {
2475 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[index];
2476
2477 switch (hdr->sh_type)
2478 {
2479 /* ELF sections that map to BFD sections */
2480 case SHT_PROGBITS:
2481 case SHT_NOBITS:
2482 if (!hdr->rawdata)
2483 bfd_section_from_shdr (abfd, index);
2484 return (struct sec *) hdr->rawdata;
2485
2486 default:
2487 return (struct sec *) &bfd_abs_section;
2488 }
2489 }
2490 }
2491
2492 /* given a section, search the header to find them... */
2493 static int
2494 elf_section_from_bfd_section (abfd, asect)
2495 bfd *abfd;
2496 struct sec *asect;
2497 {
2498 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
2499 int index;
2500 Elf_Internal_Shdr *hdr;
2501 int maxindex = elf_elfheader (abfd)->e_shnum;
2502
2503 if (asect == &bfd_abs_section)
2504 return SHN_ABS;
2505 if (asect == &bfd_com_section)
2506 return SHN_COMMON;
2507 if (asect == &bfd_und_section)
2508 return SHN_UNDEF;
2509
2510 for (index = 0; index < maxindex; index++)
2511 {
2512 hdr = i_shdrp[index];
2513 switch (hdr->sh_type)
2514 {
2515 /* ELF sections that map to BFD sections */
2516 case SHT_PROGBITS:
2517 case SHT_NOBITS:
2518 case SHT_NOTE:
2519 if (hdr->rawdata)
2520 {
2521 if (((struct sec *) (hdr->rawdata)) == asect)
2522 return index;
2523 }
2524 break;
2525
2526 case SHT_STRTAB:
2527 /* fix_up_strtabs will generate STRTAB sections with names
2528 of .stab*str. */
2529 if (!strncmp (asect->name, ".stab", 5)
2530 && !strcmp ("str", asect->name + strlen (asect->name) - 3))
2531 {
2532 if (hdr->rawdata)
2533 {
2534 if (((struct sec *) (hdr->rawdata)) == asect)
2535 return index;
2536 }
2537 break;
2538 }
2539 /* FALL THROUGH */
2540 default:
2541 {
2542 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2543
2544 if (bed->elf_backend_section_from_bfd_section)
2545 {
2546 int retval;
2547
2548 retval = index;
2549 if ((*bed->elf_backend_section_from_bfd_section)
2550 (abfd, hdr, asect, &retval))
2551 return retval;
2552 }
2553 }
2554 break;
2555 }
2556 }
2557 return -1;
2558 }
2559
2560 /* given a symbol, return the bfd index for that symbol. */
2561 static int
2562 elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
2563 bfd *abfd;
2564 struct symbol_cache_entry **asym_ptr_ptr;
2565 {
2566 struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr;
2567 int idx;
2568 flagword flags = asym_ptr->flags;
2569
2570 /* When gas creates relocations against local labels, it creates its
2571 own symbol for the section, but does put the symbol into the
2572 symbol chain, so udata is 0. When the linker is generating
2573 relocatable output, this section symbol may be for one of the
2574 input sections rather than the output section. */
2575 if (asym_ptr->udata == (PTR) 0
2576 && (flags & BSF_SECTION_SYM)
2577 && asym_ptr->section)
2578 {
2579 int indx;
2580
2581 if (asym_ptr->section->output_section != NULL)
2582 indx = asym_ptr->section->output_section->index;
2583 else
2584 indx = asym_ptr->section->index;
2585 if (elf_section_syms (abfd)[indx])
2586 asym_ptr->udata = elf_section_syms (abfd)[indx]->udata;
2587 }
2588
2589 if (asym_ptr->udata)
2590 idx = ((Elf_Sym_Extra *) asym_ptr->udata)->elf_sym_num;
2591 else
2592 {
2593 abort ();
2594 }
2595
2596 #if DEBUG & 4
2597 {
2598
2599 fprintf (stderr,
2600 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n",
2601 (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags));
2602 fflush (stderr);
2603 }
2604 #endif
2605
2606 return idx;
2607 }
2608
2609 static long
2610 elf_slurp_symbol_table (abfd, symptrs, dynamic)
2611 bfd *abfd;
2612 asymbol **symptrs; /* Buffer for generated bfd symbols */
2613 boolean dynamic;
2614 {
2615 Elf_Internal_Shdr *hdr;
2616 long symcount; /* Number of external ELF symbols */
2617 elf_symbol_type *sym; /* Pointer to current bfd symbol */
2618 elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
2619 Elf_Internal_Sym i_sym;
2620 Elf_External_Sym *x_symp = NULL;
2621
2622 /* Read each raw ELF symbol, converting from external ELF form to
2623 internal ELF form, and then using the information to create a
2624 canonical bfd symbol table entry.
2625
2626 Note that we allocate the initial bfd canonical symbol buffer
2627 based on a one-to-one mapping of the ELF symbols to canonical
2628 symbols. We actually use all the ELF symbols, so there will be no
2629 space left over at the end. When we have all the symbols, we
2630 build the caller's pointer vector. */
2631
2632 if (dynamic)
2633 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2634 else
2635 hdr = &elf_tdata (abfd)->symtab_hdr;
2636 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
2637 return -1;
2638
2639 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
2640
2641 if (symcount == 0)
2642 sym = symbase = NULL;
2643 else
2644 {
2645 long i;
2646
2647 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
2648 return -1;
2649
2650 symbase = ((elf_symbol_type *)
2651 bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type)));
2652 if (symbase == (elf_symbol_type *) NULL)
2653 {
2654 bfd_set_error (bfd_error_no_memory);
2655 return -1;
2656 }
2657 sym = symbase;
2658
2659 /* Temporarily allocate room for the raw ELF symbols. */
2660 x_symp = ((Elf_External_Sym *)
2661 malloc (symcount * sizeof (Elf_External_Sym)));
2662 if (x_symp == NULL && symcount != 0)
2663 {
2664 bfd_set_error (bfd_error_no_memory);
2665 goto error_return;
2666 }
2667
2668 if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd)
2669 != symcount * sizeof (Elf_External_Sym))
2670 goto error_return;
2671 /* Skip first symbol, which is a null dummy. */
2672 for (i = 1; i < symcount; i++)
2673 {
2674 elf_swap_symbol_in (abfd, x_symp + i, &i_sym);
2675 memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym));
2676 #ifdef ELF_KEEP_EXTSYM
2677 memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym));
2678 #endif
2679 sym->symbol.the_bfd = abfd;
2680
2681 sym->symbol.name = elf_string_from_elf_section (abfd, hdr->sh_link,
2682 i_sym.st_name);
2683
2684 sym->symbol.value = i_sym.st_value;
2685
2686 if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERV)
2687 {
2688 sym->symbol.section = section_from_elf_index (abfd,
2689 i_sym.st_shndx);
2690 }
2691 else if (i_sym.st_shndx == SHN_ABS)
2692 {
2693 sym->symbol.section = &bfd_abs_section;
2694 }
2695 else if (i_sym.st_shndx == SHN_COMMON)
2696 {
2697 sym->symbol.section = &bfd_com_section;
2698 /* Elf puts the alignment into the `value' field, and
2699 the size into the `size' field. BFD wants to see the
2700 size in the value field, and doesn't care (at the
2701 moment) about the alignment. */
2702 sym->symbol.value = i_sym.st_size;
2703 }
2704 else if (i_sym.st_shndx == SHN_UNDEF)
2705 {
2706 sym->symbol.section = &bfd_und_section;
2707 }
2708 else
2709 sym->symbol.section = &bfd_abs_section;
2710
2711 sym->symbol.value -= sym->symbol.section->vma;
2712
2713 switch (ELF_ST_BIND (i_sym.st_info))
2714 {
2715 case STB_LOCAL:
2716 sym->symbol.flags |= BSF_LOCAL;
2717 break;
2718 case STB_GLOBAL:
2719 sym->symbol.flags |= BSF_GLOBAL;
2720 break;
2721 case STB_WEAK:
2722 sym->symbol.flags |= BSF_WEAK;
2723 break;
2724 }
2725
2726 switch (ELF_ST_TYPE (i_sym.st_info))
2727 {
2728 case STT_SECTION:
2729 sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING;
2730 break;
2731 case STT_FILE:
2732 sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
2733 break;
2734 case STT_FUNC:
2735 sym->symbol.flags |= BSF_FUNCTION;
2736 break;
2737 }
2738
2739 if (dynamic)
2740 sym->symbol.flags |= BSF_DYNAMIC;
2741
2742 /* Do some backend-specific processing on this symbol. */
2743 {
2744 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
2745 if (ebd->elf_backend_symbol_processing)
2746 (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
2747 }
2748
2749 sym++;
2750 }
2751 }
2752
2753 /* Do some backend-specific processing on this symbol table. */
2754 {
2755 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
2756 if (ebd->elf_backend_symbol_table_processing)
2757 (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
2758 }
2759
2760 /* We rely on the zalloc to clear out the final symbol entry. */
2761
2762 symcount = sym - symbase;
2763
2764 /* Fill in the user's symbol pointer vector if needed. */
2765 if (symptrs)
2766 {
2767 long l = symcount;
2768
2769 sym = symbase;
2770 while (l-- > 0)
2771 {
2772 *symptrs++ = &sym->symbol;
2773 sym++;
2774 }
2775 *symptrs = 0; /* Final null pointer */
2776 }
2777
2778 if (x_symp != NULL)
2779 free (x_symp);
2780 return symcount;
2781 error_return:
2782 if (x_symp != NULL)
2783 free (x_symp);
2784 return -1;
2785 }
2786
2787 /* Return the number of bytes required to hold the symtab vector.
2788
2789 Note that we base it on the count plus 1, since we will null terminate
2790 the vector allocated based on this size. However, the ELF symbol table
2791 always has a dummy entry as symbol #0, so it ends up even. */
2792
2793 long
2794 elf_get_symtab_upper_bound (abfd)
2795 bfd *abfd;
2796 {
2797 long symcount;
2798 long symtab_size;
2799 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
2800
2801 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
2802 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
2803
2804 return symtab_size;
2805 }
2806
2807 long
2808 elf_get_dynamic_symtab_upper_bound (abfd)
2809 bfd *abfd;
2810 {
2811 long symcount;
2812 long symtab_size;
2813 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2814
2815 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
2816 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
2817
2818 return symtab_size;
2819 }
2820
2821 /*
2822 This function return the number of bytes required to store the
2823 relocation information associated with section <<sect>>
2824 attached to bfd <<abfd>>
2825
2826 */
2827 long
2828 elf_get_reloc_upper_bound (abfd, asect)
2829 bfd *abfd;
2830 sec_ptr asect;
2831 {
2832 if (asect->flags & SEC_RELOC)
2833 {
2834 /* either rel or rela */
2835 return elf_section_data (asect)->rel_hdr.sh_size;
2836 }
2837 else
2838 return 0;
2839 }
2840
2841 static boolean
2842 elf_slurp_reloca_table (abfd, asect, symbols)
2843 bfd *abfd;
2844 sec_ptr asect;
2845 asymbol **symbols;
2846 {
2847 Elf_External_Rela *native_relocs;
2848 arelent *reloc_cache;
2849 arelent *cache_ptr;
2850
2851 unsigned int idx;
2852
2853 if (asect->relocation)
2854 return true;
2855 if (asect->reloc_count == 0)
2856 return true;
2857 if (asect->flags & SEC_CONSTRUCTOR)
2858 return true;
2859
2860 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
2861 return false;
2862 native_relocs = (Elf_External_Rela *)
2863 bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rela));
2864 if (!native_relocs)
2865 {
2866 bfd_set_error (bfd_error_no_memory);
2867 return false;
2868 }
2869 if (bfd_read ((PTR) native_relocs,
2870 sizeof (Elf_External_Rela), asect->reloc_count, abfd)
2871 != sizeof (Elf_External_Rela) * asect->reloc_count)
2872 return false;
2873
2874 reloc_cache = (arelent *)
2875 bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
2876
2877 if (!reloc_cache)
2878 {
2879 bfd_set_error (bfd_error_no_memory);
2880 return false;
2881 }
2882
2883 for (idx = 0; idx < asect->reloc_count; idx++)
2884 {
2885 Elf_Internal_Rela dst;
2886 Elf_External_Rela *src;
2887
2888 cache_ptr = reloc_cache + idx;
2889 src = native_relocs + idx;
2890 elf_swap_reloca_in (abfd, src, &dst);
2891
2892 #ifdef RELOC_PROCESSING
2893 RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
2894 #else
2895 if (asect->flags & SEC_RELOC)
2896 {
2897 /* relocatable, so the offset is off of the section */
2898 cache_ptr->address = dst.r_offset + asect->vma;
2899 }
2900 else
2901 {
2902 /* non-relocatable, so the offset a virtual address */
2903 cache_ptr->address = dst.r_offset;
2904 }
2905
2906 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
2907 of zero points to the dummy symbol, which was not read into
2908 the symbol table SYMBOLS. */
2909 if (ELF_R_SYM (dst.r_info) == 0)
2910 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
2911 else
2912 {
2913 asymbol *s;
2914
2915 cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1;
2916
2917 /* Translate any ELF section symbol into a BFD section
2918 symbol. */
2919 s = *(cache_ptr->sym_ptr_ptr);
2920 if (s->flags & BSF_SECTION_SYM)
2921 {
2922 cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr;
2923 s = *cache_ptr->sym_ptr_ptr;
2924 if (s->name == 0 || s->name[0] == 0)
2925 abort ();
2926 }
2927 }
2928 cache_ptr->addend = dst.r_addend;
2929
2930 /* Fill in the cache_ptr->howto field from dst.r_type */
2931 {
2932 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
2933 (*ebd->elf_info_to_howto) (abfd, cache_ptr, &dst);
2934 }
2935 #endif
2936 }
2937
2938 asect->relocation = reloc_cache;
2939 return true;
2940 }
2941
2942 #ifdef DEBUG
2943 static void
2944 elf_debug_section (str, num, hdr)
2945 char *str;
2946 int num;
2947 Elf_Internal_Shdr *hdr;
2948 {
2949 fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num, str, (long) hdr);
2950 fprintf (stderr,
2951 "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n",
2952 (long) hdr->sh_name,
2953 (long) hdr->sh_type,
2954 (long) hdr->sh_flags);
2955 fprintf (stderr,
2956 "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n",
2957 (long) hdr->sh_addr,
2958 (long) hdr->sh_offset,
2959 (long) hdr->sh_size);
2960 fprintf (stderr,
2961 "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n",
2962 (long) hdr->sh_link,
2963 (long) hdr->sh_info,
2964 (long) hdr->sh_addralign);
2965 fprintf (stderr, "sh_entsize = %ld\n",
2966 (long) hdr->sh_entsize);
2967 fprintf (stderr, "rawdata = 0x%.8lx\n", (long) hdr->rawdata);
2968 fprintf (stderr, "contents = 0x%.8lx\n", (long) hdr->contents);
2969 fprintf (stderr, "size = %ld\n", (long) hdr->size);
2970 fflush (stderr);
2971 }
2972
2973 static void
2974 elf_debug_file (ehdrp)
2975 Elf_Internal_Ehdr *ehdrp;
2976 {
2977 fprintf (stderr, "e_entry = 0x%.8lx\n", (long) ehdrp->e_entry);
2978 fprintf (stderr, "e_phoff = %ld\n", (long) ehdrp->e_phoff);
2979 fprintf (stderr, "e_phnum = %ld\n", (long) ehdrp->e_phnum);
2980 fprintf (stderr, "e_phentsize = %ld\n", (long) ehdrp->e_phentsize);
2981 fprintf (stderr, "e_shoff = %ld\n", (long) ehdrp->e_shoff);
2982 fprintf (stderr, "e_shnum = %ld\n", (long) ehdrp->e_shnum);
2983 fprintf (stderr, "e_shentsize = %ld\n", (long) ehdrp->e_shentsize);
2984 }
2985 #endif
2986
2987 static boolean
2988 elf_slurp_reloc_table (abfd, asect, symbols)
2989 bfd *abfd;
2990 sec_ptr asect;
2991 asymbol **symbols;
2992 {
2993 Elf_External_Rel *native_relocs;
2994 arelent *reloc_cache;
2995 arelent *cache_ptr;
2996 Elf_Internal_Shdr *data_hdr;
2997 bfd_vma data_off;
2998 unsigned long data_max;
2999 char buf[4]; /* FIXME -- might be elf64 */
3000
3001 unsigned int idx;
3002
3003 if (asect->relocation)
3004 return true;
3005 if (asect->reloc_count == 0)
3006 return true;
3007 if (asect->flags & SEC_CONSTRUCTOR)
3008 return true;
3009
3010 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
3011 return false;
3012 native_relocs = (Elf_External_Rel *)
3013 bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rel));
3014 if (!native_relocs)
3015 {
3016 bfd_set_error (bfd_error_no_memory);
3017 return false;
3018 }
3019 if (bfd_read ((PTR) native_relocs,
3020 sizeof (Elf_External_Rel), asect->reloc_count, abfd)
3021 != sizeof (Elf_External_Rel) * asect->reloc_count)
3022 return false;
3023
3024 reloc_cache = (arelent *)
3025 bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
3026
3027 if (!reloc_cache)
3028 {
3029 bfd_set_error (bfd_error_no_memory);
3030 return false;
3031 }
3032
3033 /* Get the offset of the start of the segment we are relocating to read in
3034 the implicit addend. */
3035 data_hdr = &elf_section_data (asect)->this_hdr;
3036 data_off = data_hdr->sh_offset;
3037 data_max = data_hdr->sh_size - sizeof (buf) + 1;
3038
3039 #if DEBUG & 2
3040 elf_debug_section ("data section", -1, data_hdr);
3041 #endif
3042
3043 for (idx = 0; idx < asect->reloc_count; idx++)
3044 {
3045 #ifdef RELOC_PROCESSING
3046 Elf_Internal_Rel dst;
3047 Elf_External_Rel *src;
3048
3049 cache_ptr = reloc_cache + idx;
3050 src = native_relocs + idx;
3051 elf_swap_reloc_in (abfd, src, &dst);
3052
3053 RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
3054 #else
3055 Elf_Internal_Rel dst;
3056 Elf_External_Rel *src;
3057
3058 cache_ptr = reloc_cache + idx;
3059 src = native_relocs + idx;
3060
3061 elf_swap_reloc_in (abfd, src, &dst);
3062
3063 if (asect->flags & SEC_RELOC)
3064 {
3065 /* relocatable, so the offset is off of the section */
3066 cache_ptr->address = dst.r_offset + asect->vma;
3067 }
3068 else
3069 {
3070 /* non-relocatable, so the offset a virtual address */
3071 cache_ptr->address = dst.r_offset;
3072 }
3073
3074 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
3075 of zero points to the dummy symbol, which was not read into
3076 the symbol table SYMBOLS. */
3077 if (ELF_R_SYM (dst.r_info) == 0)
3078 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
3079 else
3080 {
3081 asymbol *s;
3082
3083 cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1;
3084
3085 /* Translate any ELF section symbol into a BFD section
3086 symbol. */
3087 s = *(cache_ptr->sym_ptr_ptr);
3088 if (s->flags & BSF_SECTION_SYM)
3089 {
3090 cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr;
3091 s = *cache_ptr->sym_ptr_ptr;
3092 if (s->name == 0 || s->name[0] == 0)
3093 abort ();
3094 }
3095 }
3096 BFD_ASSERT (dst.r_offset <= data_max);
3097 cache_ptr->addend = 0;
3098
3099 /* Fill in the cache_ptr->howto field from dst.r_type */
3100 {
3101 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
3102 (*ebd->elf_info_to_howto_rel) (abfd, cache_ptr, &dst);
3103 }
3104 #endif
3105 }
3106
3107 asect->relocation = reloc_cache;
3108 return true;
3109 }
3110
3111 long
3112 elf_canonicalize_reloc (abfd, section, relptr, symbols)
3113 bfd *abfd;
3114 sec_ptr section;
3115 arelent **relptr;
3116 asymbol **symbols;
3117 {
3118 arelent *tblptr = section->relocation;
3119 unsigned int count = 0;
3120 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
3121
3122 /* snarfed from coffcode.h */
3123 if (use_rela_p)
3124 {
3125 if (! elf_slurp_reloca_table (abfd, section, symbols))
3126 return -1;
3127 }
3128 else
3129 {
3130 if (! elf_slurp_reloc_table (abfd, section, symbols))
3131 return -1;
3132 }
3133
3134 tblptr = section->relocation;
3135
3136 for (; count++ < section->reloc_count;)
3137 *relptr++ = tblptr++;
3138
3139 *relptr = 0;
3140 return section->reloc_count;
3141 }
3142
3143 long
3144 elf_get_symtab (abfd, alocation)
3145 bfd *abfd;
3146 asymbol **alocation;
3147 {
3148 long symcount = elf_slurp_symbol_table (abfd, alocation, false);
3149
3150 if (symcount >= 0)
3151 bfd_get_symcount (abfd) = symcount;
3152 return symcount;
3153 }
3154
3155 long
3156 elf_canonicalize_dynamic_symtab (abfd, alocation)
3157 bfd *abfd;
3158 asymbol **alocation;
3159 {
3160 return elf_slurp_symbol_table (abfd, alocation, true);
3161 }
3162
3163 asymbol *
3164 elf_make_empty_symbol (abfd)
3165 bfd *abfd;
3166 {
3167 elf_symbol_type *newsym;
3168
3169 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
3170 if (!newsym)
3171 {
3172 bfd_set_error (bfd_error_no_memory);
3173 return NULL;
3174 }
3175 else
3176 {
3177 newsym->symbol.the_bfd = abfd;
3178 return &newsym->symbol;
3179 }
3180 }
3181
3182 void
3183 elf_get_symbol_info (ignore_abfd, symbol, ret)
3184 bfd *ignore_abfd;
3185 asymbol *symbol;
3186 symbol_info *ret;
3187 {
3188 bfd_symbol_info (symbol, ret);
3189 }
3190
3191 void
3192 elf_print_symbol (ignore_abfd, filep, symbol, how)
3193 bfd *ignore_abfd;
3194 PTR filep;
3195 asymbol *symbol;
3196 bfd_print_symbol_type how;
3197 {
3198 FILE *file = (FILE *) filep;
3199 switch (how)
3200 {
3201 case bfd_print_symbol_name:
3202 fprintf (file, "%s", symbol->name);
3203 break;
3204 case bfd_print_symbol_more:
3205 fprintf (file, "elf ");
3206 fprintf_vma (file, symbol->value);
3207 fprintf (file, " %lx", (long) symbol->flags);
3208 break;
3209 case bfd_print_symbol_all:
3210 {
3211 CONST char *section_name;
3212 section_name = symbol->section ? symbol->section->name : "(*none*)";
3213 bfd_print_symbol_vandf ((PTR) file, symbol);
3214 fprintf (file, " %s\t%s",
3215 section_name,
3216 symbol->name);
3217 }
3218 break;
3219 }
3220
3221 }
3222
3223 alent *
3224 elf_get_lineno (ignore_abfd, symbol)
3225 bfd *ignore_abfd;
3226 asymbol *symbol;
3227 {
3228 fprintf (stderr, "elf_get_lineno unimplemented\n");
3229 fflush (stderr);
3230 BFD_FAIL ();
3231 return NULL;
3232 }
3233
3234 boolean
3235 elf_set_arch_mach (abfd, arch, machine)
3236 bfd *abfd;
3237 enum bfd_architecture arch;
3238 unsigned long machine;
3239 {
3240 /* If this isn't the right architecture for this backend, and this
3241 isn't the generic backend, fail. */
3242 if (arch != get_elf_backend_data (abfd)->arch
3243 && arch != bfd_arch_unknown
3244 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
3245 return false;
3246
3247 return bfd_default_set_arch_mach (abfd, arch, machine);
3248 }
3249
3250 boolean
3251 elf_find_nearest_line (abfd,
3252 section,
3253 symbols,
3254 offset,
3255 filename_ptr,
3256 functionname_ptr,
3257 line_ptr)
3258 bfd *abfd;
3259 asection *section;
3260 asymbol **symbols;
3261 bfd_vma offset;
3262 CONST char **filename_ptr;
3263 CONST char **functionname_ptr;
3264 unsigned int *line_ptr;
3265 {
3266 return false;
3267 }
3268
3269 int
3270 elf_sizeof_headers (abfd, reloc)
3271 bfd *abfd;
3272 boolean reloc;
3273 {
3274 fprintf (stderr, "elf_sizeof_headers unimplemented\n");
3275 fflush (stderr);
3276 BFD_FAIL ();
3277 return 0;
3278 }
3279
3280 boolean
3281 elf_set_section_contents (abfd, section, location, offset, count)
3282 bfd *abfd;
3283 sec_ptr section;
3284 PTR location;
3285 file_ptr offset;
3286 bfd_size_type count;
3287 {
3288 Elf_Internal_Shdr *hdr;
3289
3290 if (abfd->output_has_begun == false) /* set by bfd.c handler? */
3291 {
3292 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3293
3294 /* Do any elf backend specific processing first. */
3295 if (bed->elf_backend_begin_write_processing)
3296 (*bed->elf_backend_begin_write_processing) (abfd);
3297
3298 /* do setup calculations (FIXME) */
3299 if (prep_headers (abfd) == false)
3300 return false;
3301 if (elf_compute_section_file_positions (abfd) == false)
3302 return false;
3303 abfd->output_has_begun = true;
3304 }
3305
3306 hdr = &elf_section_data (section)->this_hdr;
3307
3308 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
3309 return false;
3310 if (bfd_write (location, 1, count, abfd) != count)
3311 return false;
3312
3313 return true;
3314 }
3315
3316 void
3317 elf_no_info_to_howto (abfd, cache_ptr, dst)
3318 bfd *abfd;
3319 arelent *cache_ptr;
3320 Elf_Internal_Rela *dst;
3321 {
3322 fprintf (stderr, "elf RELA relocation support for target machine unimplemented\n");
3323 fflush (stderr);
3324 BFD_FAIL ();
3325 }
3326
3327 void
3328 elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
3329 bfd *abfd;
3330 arelent *cache_ptr;
3331 Elf_Internal_Rel *dst;
3332 {
3333 fprintf (stderr, "elf REL relocation support for target machine unimplemented\n");
3334 fflush (stderr);
3335 BFD_FAIL ();
3336 }
3337 \f
3338
3339 /* Core file support */
3340
3341 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
3342 #include <sys/procfs.h>
3343 #else
3344 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
3345 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
3346 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
3347 #endif
3348
3349 #ifdef HAVE_PROCFS
3350
3351 static void
3352 bfd_prstatus (abfd, descdata, descsz, filepos)
3353 bfd *abfd;
3354 char *descdata;
3355 int descsz;
3356 long filepos;
3357 {
3358 asection *newsect;
3359 prstatus_t *status = (prstatus_t *) 0;
3360
3361 if (descsz == sizeof (prstatus_t))
3362 {
3363 newsect = bfd_make_section (abfd, ".reg");
3364 newsect->_raw_size = sizeof (status->pr_reg);
3365 newsect->filepos = filepos + (long) &status->pr_reg;
3366 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3367 newsect->alignment_power = 2;
3368 if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
3369 {
3370 memcpy (core_prstatus (abfd), descdata, descsz);
3371 }
3372 }
3373 }
3374
3375 /* Stash a copy of the prpsinfo structure away for future use. */
3376
3377 static void
3378 bfd_prpsinfo (abfd, descdata, descsz, filepos)
3379 bfd *abfd;
3380 char *descdata;
3381 int descsz;
3382 long filepos;
3383 {
3384 asection *newsect;
3385
3386 if (descsz == sizeof (prpsinfo_t))
3387 {
3388 if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) != NULL)
3389 {
3390 memcpy (core_prpsinfo (abfd), descdata, descsz);
3391 }
3392 }
3393 }
3394
3395 static void
3396 bfd_fpregset (abfd, descdata, descsz, filepos)
3397 bfd *abfd;
3398 char *descdata;
3399 int descsz;
3400 long filepos;
3401 {
3402 asection *newsect;
3403
3404 newsect = bfd_make_section (abfd, ".reg2");
3405 newsect->_raw_size = descsz;
3406 newsect->filepos = filepos;
3407 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3408 newsect->alignment_power = 2;
3409 }
3410
3411 #endif /* HAVE_PROCFS */
3412
3413 /* Return a pointer to the args (including the command name) that were
3414 seen by the program that generated the core dump. Note that for
3415 some reason, a spurious space is tacked onto the end of the args
3416 in some (at least one anyway) implementations, so strip it off if
3417 it exists. */
3418
3419 char *
3420 elf_core_file_failing_command (abfd)
3421 bfd *abfd;
3422 {
3423 #ifdef HAVE_PROCFS
3424 if (core_prpsinfo (abfd))
3425 {
3426 prpsinfo_t *p = core_prpsinfo (abfd);
3427 char *scan = p->pr_psargs;
3428 while (*scan++)
3429 {;
3430 }
3431 scan -= 2;
3432 if ((scan > p->pr_psargs) && (*scan == ' '))
3433 {
3434 *scan = '\000';
3435 }
3436 return p->pr_psargs;
3437 }
3438 #endif
3439 return NULL;
3440 }
3441
3442 /* Return the number of the signal that caused the core dump. Presumably,
3443 since we have a core file, we got a signal of some kind, so don't bother
3444 checking the other process status fields, just return the signal number.
3445 */
3446
3447 int
3448 elf_core_file_failing_signal (abfd)
3449 bfd *abfd;
3450 {
3451 #ifdef HAVE_PROCFS
3452 if (core_prstatus (abfd))
3453 {
3454 return ((prstatus_t *) (core_prstatus (abfd)))->pr_cursig;
3455 }
3456 #endif
3457 return -1;
3458 }
3459
3460 /* Check to see if the core file could reasonably be expected to have
3461 come for the current executable file. Note that by default we return
3462 true unless we find something that indicates that there might be a
3463 problem.
3464 */
3465
3466 boolean
3467 elf_core_file_matches_executable_p (core_bfd, exec_bfd)
3468 bfd *core_bfd;
3469 bfd *exec_bfd;
3470 {
3471 #ifdef HAVE_PROCFS
3472 char *corename;
3473 char *execname;
3474 #endif
3475
3476 /* First, xvecs must match since both are ELF files for the same target. */
3477
3478 if (core_bfd->xvec != exec_bfd->xvec)
3479 {
3480 bfd_set_error (bfd_error_system_call);
3481 return false;
3482 }
3483
3484 #ifdef HAVE_PROCFS
3485
3486 /* If no prpsinfo, just return true. Otherwise, grab the last component
3487 of the exec'd pathname from the prpsinfo. */
3488
3489 if (core_prpsinfo (core_bfd))
3490 {
3491 corename = (((struct prpsinfo *) core_prpsinfo (core_bfd))->pr_fname);
3492 }
3493 else
3494 {
3495 return true;
3496 }
3497
3498 /* Find the last component of the executable pathname. */
3499
3500 if ((execname = strrchr (exec_bfd->filename, '/')) != NULL)
3501 {
3502 execname++;
3503 }
3504 else
3505 {
3506 execname = (char *) exec_bfd->filename;
3507 }
3508
3509 /* See if they match */
3510
3511 return strcmp (execname, corename) ? false : true;
3512
3513 #else
3514
3515 return true;
3516
3517 #endif /* HAVE_PROCFS */
3518 }
3519
3520 /* ELF core files contain a segment of type PT_NOTE, that holds much of
3521 the information that would normally be available from the /proc interface
3522 for the process, at the time the process dumped core. Currently this
3523 includes copies of the prstatus, prpsinfo, and fpregset structures.
3524
3525 Since these structures are potentially machine dependent in size and
3526 ordering, bfd provides two levels of support for them. The first level,
3527 available on all machines since it does not require that the host
3528 have /proc support or the relevant include files, is to create a bfd
3529 section for each of the prstatus, prpsinfo, and fpregset structures,
3530 without any interpretation of their contents. With just this support,
3531 the bfd client will have to interpret the structures itself. Even with
3532 /proc support, it might want these full structures for it's own reasons.
3533
3534 In the second level of support, where HAVE_PROCFS is defined, bfd will
3535 pick apart the structures to gather some additional information that
3536 clients may want, such as the general register set, the name of the
3537 exec'ed file and its arguments, the signal (if any) that caused the
3538 core dump, etc.
3539
3540 */
3541
3542 static boolean
3543 elf_corefile_note (abfd, hdr)
3544 bfd *abfd;
3545 Elf_Internal_Phdr *hdr;
3546 {
3547 Elf_External_Note *x_note_p; /* Elf note, external form */
3548 Elf_Internal_Note i_note; /* Elf note, internal form */
3549 char *buf = NULL; /* Entire note segment contents */
3550 char *namedata; /* Name portion of the note */
3551 char *descdata; /* Descriptor portion of the note */
3552 char *sectname; /* Name to use for new section */
3553 long filepos; /* File offset to descriptor data */
3554 asection *newsect;
3555
3556 if (hdr->p_filesz > 0
3557 && (buf = (char *) malloc (hdr->p_filesz)) != NULL
3558 && bfd_seek (abfd, hdr->p_offset, SEEK_SET) != -1
3559 && bfd_read ((PTR) buf, hdr->p_filesz, 1, abfd) == hdr->p_filesz)
3560 {
3561 x_note_p = (Elf_External_Note *) buf;
3562 while ((char *) x_note_p < (buf + hdr->p_filesz))
3563 {
3564 i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->namesz);
3565 i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->descsz);
3566 i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->type);
3567 namedata = x_note_p->name;
3568 descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
3569 filepos = hdr->p_offset + (descdata - buf);
3570 switch (i_note.type)
3571 {
3572 case NT_PRSTATUS:
3573 /* process descdata as prstatus info */
3574 bfd_prstatus (abfd, descdata, i_note.descsz, filepos);
3575 sectname = ".prstatus";
3576 break;
3577 case NT_FPREGSET:
3578 /* process descdata as fpregset info */
3579 bfd_fpregset (abfd, descdata, i_note.descsz, filepos);
3580 sectname = ".fpregset";
3581 break;
3582 case NT_PRPSINFO:
3583 /* process descdata as prpsinfo */
3584 bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos);
3585 sectname = ".prpsinfo";
3586 break;
3587 default:
3588 /* Unknown descriptor, just ignore it. */
3589 sectname = NULL;
3590 break;
3591 }
3592 if (sectname != NULL)
3593 {
3594 newsect = bfd_make_section (abfd, sectname);
3595 newsect->_raw_size = i_note.descsz;
3596 newsect->filepos = filepos;
3597 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3598 newsect->alignment_power = 2;
3599 }
3600 x_note_p = (Elf_External_Note *)
3601 (descdata + BFD_ALIGN (i_note.descsz, 4));
3602 }
3603 }
3604 if (buf != NULL)
3605 {
3606 free (buf);
3607 }
3608 else if (hdr->p_filesz > 0)
3609 {
3610 bfd_set_error (bfd_error_no_memory);
3611 return false;
3612 }
3613 return true;
3614
3615 }
3616
3617 /* Core files are simply standard ELF formatted files that partition
3618 the file using the execution view of the file (program header table)
3619 rather than the linking view. In fact, there is no section header
3620 table in a core file.
3621
3622 The process status information (including the contents of the general
3623 register set) and the floating point register set are stored in a
3624 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
3625 that allow standard bfd access to the general registers (.reg) and the
3626 floating point registers (.reg2).
3627
3628 */
3629
3630 bfd_target *
3631 elf_core_file_p (abfd)
3632 bfd *abfd;
3633 {
3634 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
3635 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3636 Elf_External_Phdr x_phdr; /* Program header table entry, external form */
3637 Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */
3638 unsigned int phindex;
3639 struct elf_backend_data *ebd;
3640
3641 /* Read in the ELF header in external format. */
3642
3643 if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
3644 {
3645 if (bfd_get_error () != bfd_error_system_call)
3646 bfd_set_error (bfd_error_wrong_format);
3647 return NULL;
3648 }
3649
3650 /* Now check to see if we have a valid ELF file, and one that BFD can
3651 make use of. The magic number must match, the address size ('class')
3652 and byte-swapping must match our XVEC entry, and it must have a
3653 program header table (FIXME: See comments re segments at top of this
3654 file). */
3655
3656 if (elf_file_p (&x_ehdr) == false)
3657 {
3658 wrong:
3659 bfd_set_error (bfd_error_wrong_format);
3660 return NULL;
3661 }
3662
3663 /* FIXME, Check EI_VERSION here ! */
3664
3665 {
3666 #if ARCH_SIZE == 32
3667 int desired_address_size = ELFCLASS32;
3668 #endif
3669 #if ARCH_SIZE == 64
3670 int desired_address_size = ELFCLASS64;
3671 #endif
3672
3673 if (x_ehdr.e_ident[EI_CLASS] != desired_address_size)
3674 goto wrong;
3675 }
3676
3677 /* Switch xvec to match the specified byte order. */
3678 switch (x_ehdr.e_ident[EI_DATA])
3679 {
3680 case ELFDATA2MSB: /* Big-endian */
3681 if (abfd->xvec->byteorder_big_p == false)
3682 goto wrong;
3683 break;
3684 case ELFDATA2LSB: /* Little-endian */
3685 if (abfd->xvec->byteorder_big_p == true)
3686 goto wrong;
3687 break;
3688 case ELFDATANONE: /* No data encoding specified */
3689 default: /* Unknown data encoding specified */
3690 goto wrong;
3691 }
3692
3693 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
3694 the tdata pointer in the bfd. */
3695
3696 elf_tdata (abfd) =
3697 (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
3698 if (elf_tdata (abfd) == NULL)
3699 {
3700 bfd_set_error (bfd_error_no_memory);
3701 return NULL;
3702 }
3703
3704 /* FIXME, `wrong' returns from this point onward, leak memory. */
3705
3706 /* Now that we know the byte order, swap in the rest of the header */
3707 i_ehdrp = elf_elfheader (abfd);
3708 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
3709 #if DEBUG & 1
3710 elf_debug_file (i_ehdrp);
3711 #endif
3712
3713 ebd = get_elf_backend_data (abfd);
3714
3715 /* Check that the ELF e_machine field matches what this particular
3716 BFD format expects. */
3717 if (ebd->elf_machine_code != i_ehdrp->e_machine)
3718 {
3719 bfd_target **target_ptr;
3720
3721 if (ebd->elf_machine_code != EM_NONE)
3722 goto wrong;
3723
3724 /* This is the generic ELF target. Let it match any ELF target
3725 for which we do not have a specific backend. */
3726 for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
3727 {
3728 struct elf_backend_data *back;
3729
3730 if ((*target_ptr)->flavour != bfd_target_elf_flavour)
3731 continue;
3732 back = (struct elf_backend_data *) (*target_ptr)->backend_data;
3733 if (back->elf_machine_code == i_ehdrp->e_machine)
3734 {
3735 /* target_ptr is an ELF backend which matches this
3736 object file, so reject the generic ELF target. */
3737 goto wrong;
3738 }
3739 }
3740 }
3741
3742 /* If there is no program header, or the type is not a core file, then
3743 we are hosed. */
3744 if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
3745 goto wrong;
3746
3747 /* Allocate space for a copy of the program header table in
3748 internal form, seek to the program header table in the file,
3749 read it in, and convert it to internal form. As a simple sanity
3750 check, verify that the what BFD thinks is the size of each program
3751 header table entry actually matches the size recorded in the file. */
3752
3753 if (i_ehdrp->e_phentsize != sizeof (x_phdr))
3754 goto wrong;
3755 i_phdrp = (Elf_Internal_Phdr *)
3756 bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum);
3757 if (!i_phdrp)
3758 {
3759 bfd_set_error (bfd_error_no_memory);
3760 return NULL;
3761 }
3762 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1)
3763 return NULL;
3764 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
3765 {
3766 if (bfd_read ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd)
3767 != sizeof (x_phdr))
3768 return NULL;
3769 elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
3770 }
3771
3772 /* Once all of the program headers have been read and converted, we
3773 can start processing them. */
3774
3775 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
3776 {
3777 bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
3778 if ((i_phdrp + phindex)->p_type == PT_NOTE)
3779 {
3780 elf_corefile_note (abfd, i_phdrp + phindex);
3781 }
3782 }
3783
3784 /* Remember the entry point specified in the ELF file header. */
3785
3786 bfd_get_start_address (abfd) = i_ehdrp->e_entry;
3787
3788 return abfd->xvec;
3789 }