Rename bfd_elf_get_arch_size to bfd_get_arch_size
[deliverable/binutils-gdb.git] / bfd / elf.c
1 /* ELF executable support for BFD.
2 Copyright 1993, 94, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 /*
21
22 SECTION
23 ELF backends
24
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
28
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
31 haven't bothered yet.
32 */
33
34 #ifdef __sparcv9
35 #define _SYSCALL32 /* For Sparc64-cross-32 */
36 #endif
37
38 #include "bfd.h"
39 #include "sysdep.h"
40 #include "bfdlink.h"
41 #include "libbfd.h"
42 #define ARCH_SIZE 0
43 #include "elf-bfd.h"
44
45 static INLINE struct elf_segment_map *make_mapping
46 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
47 static boolean map_sections_to_segments PARAMS ((bfd *));
48 static int elf_sort_sections PARAMS ((const PTR, const PTR));
49 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
50 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
51 static boolean prep_headers PARAMS ((bfd *));
52 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
53 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
54 static char *elf_read PARAMS ((bfd *, long, unsigned int));
55 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
56 static boolean assign_section_numbers PARAMS ((bfd *));
57 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
58 static boolean elf_map_symbols PARAMS ((bfd *));
59 static bfd_size_type get_program_header_size PARAMS ((bfd *));
60 static boolean elfcore_read_notes PARAMS ((bfd *, bfd_vma, bfd_vma));
61
62 /* Swap version information in and out. The version information is
63 currently size independent. If that ever changes, this code will
64 need to move into elfcode.h. */
65
66 /* Swap in a Verdef structure. */
67
68 void
69 _bfd_elf_swap_verdef_in (abfd, src, dst)
70 bfd *abfd;
71 const Elf_External_Verdef *src;
72 Elf_Internal_Verdef *dst;
73 {
74 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
75 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags);
76 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx);
77 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt);
78 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash);
79 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux);
80 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next);
81 }
82
83 /* Swap out a Verdef structure. */
84
85 void
86 _bfd_elf_swap_verdef_out (abfd, src, dst)
87 bfd *abfd;
88 const Elf_Internal_Verdef *src;
89 Elf_External_Verdef *dst;
90 {
91 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
92 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
93 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
94 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
95 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
96 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
97 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
98 }
99
100 /* Swap in a Verdaux structure. */
101
102 void
103 _bfd_elf_swap_verdaux_in (abfd, src, dst)
104 bfd *abfd;
105 const Elf_External_Verdaux *src;
106 Elf_Internal_Verdaux *dst;
107 {
108 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
109 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
110 }
111
112 /* Swap out a Verdaux structure. */
113
114 void
115 _bfd_elf_swap_verdaux_out (abfd, src, dst)
116 bfd *abfd;
117 const Elf_Internal_Verdaux *src;
118 Elf_External_Verdaux *dst;
119 {
120 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
121 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
122 }
123
124 /* Swap in a Verneed structure. */
125
126 void
127 _bfd_elf_swap_verneed_in (abfd, src, dst)
128 bfd *abfd;
129 const Elf_External_Verneed *src;
130 Elf_Internal_Verneed *dst;
131 {
132 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
133 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt);
134 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file);
135 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux);
136 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next);
137 }
138
139 /* Swap out a Verneed structure. */
140
141 void
142 _bfd_elf_swap_verneed_out (abfd, src, dst)
143 bfd *abfd;
144 const Elf_Internal_Verneed *src;
145 Elf_External_Verneed *dst;
146 {
147 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
148 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
149 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
150 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
151 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
152 }
153
154 /* Swap in a Vernaux structure. */
155
156 void
157 _bfd_elf_swap_vernaux_in (abfd, src, dst)
158 bfd *abfd;
159 const Elf_External_Vernaux *src;
160 Elf_Internal_Vernaux *dst;
161 {
162 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash);
163 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
164 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
165 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name);
166 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next);
167 }
168
169 /* Swap out a Vernaux structure. */
170
171 void
172 _bfd_elf_swap_vernaux_out (abfd, src, dst)
173 bfd *abfd;
174 const Elf_Internal_Vernaux *src;
175 Elf_External_Vernaux *dst;
176 {
177 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
178 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
179 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
180 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
181 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
182 }
183
184 /* Swap in a Versym structure. */
185
186 void
187 _bfd_elf_swap_versym_in (abfd, src, dst)
188 bfd *abfd;
189 const Elf_External_Versym *src;
190 Elf_Internal_Versym *dst;
191 {
192 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
193 }
194
195 /* Swap out a Versym structure. */
196
197 void
198 _bfd_elf_swap_versym_out (abfd, src, dst)
199 bfd *abfd;
200 const Elf_Internal_Versym *src;
201 Elf_External_Versym *dst;
202 {
203 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
204 }
205
206 /* Standard ELF hash function. Do not change this function; you will
207 cause invalid hash tables to be generated. */
208
209 unsigned long
210 bfd_elf_hash (namearg)
211 const char *namearg;
212 {
213 const unsigned char *name = (const unsigned char *) namearg;
214 unsigned long h = 0;
215 unsigned long g;
216 int ch;
217
218 while ((ch = *name++) != '\0')
219 {
220 h = (h << 4) + ch;
221 if ((g = (h & 0xf0000000)) != 0)
222 {
223 h ^= g >> 24;
224 /* The ELF ABI says `h &= ~g', but this is equivalent in
225 this case and on some machines one insn instead of two. */
226 h ^= g;
227 }
228 }
229 return h;
230 }
231
232 /* Read a specified number of bytes at a specified offset in an ELF
233 file, into a newly allocated buffer, and return a pointer to the
234 buffer. */
235
236 static char *
237 elf_read (abfd, offset, size)
238 bfd * abfd;
239 long offset;
240 unsigned int size;
241 {
242 char *buf;
243
244 if ((buf = bfd_alloc (abfd, size)) == NULL)
245 return NULL;
246 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
247 return NULL;
248 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
249 {
250 if (bfd_get_error () != bfd_error_system_call)
251 bfd_set_error (bfd_error_file_truncated);
252 return NULL;
253 }
254 return buf;
255 }
256
257 boolean
258 bfd_elf_mkobject (abfd)
259 bfd * abfd;
260 {
261 /* this just does initialization */
262 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
263 elf_tdata (abfd) = (struct elf_obj_tdata *)
264 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
265 if (elf_tdata (abfd) == 0)
266 return false;
267 /* since everything is done at close time, do we need any
268 initialization? */
269
270 return true;
271 }
272
273 boolean
274 bfd_elf_mkcorefile (abfd)
275 bfd * abfd;
276 {
277 /* I think this can be done just like an object file. */
278 return bfd_elf_mkobject (abfd);
279 }
280
281 char *
282 bfd_elf_get_str_section (abfd, shindex)
283 bfd * abfd;
284 unsigned int shindex;
285 {
286 Elf_Internal_Shdr **i_shdrp;
287 char *shstrtab = NULL;
288 unsigned int offset;
289 unsigned int shstrtabsize;
290
291 i_shdrp = elf_elfsections (abfd);
292 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
293 return 0;
294
295 shstrtab = (char *) i_shdrp[shindex]->contents;
296 if (shstrtab == NULL)
297 {
298 /* No cached one, attempt to read, and cache what we read. */
299 offset = i_shdrp[shindex]->sh_offset;
300 shstrtabsize = i_shdrp[shindex]->sh_size;
301 shstrtab = elf_read (abfd, offset, shstrtabsize);
302 i_shdrp[shindex]->contents = (PTR) shstrtab;
303 }
304 return shstrtab;
305 }
306
307 char *
308 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
309 bfd * abfd;
310 unsigned int shindex;
311 unsigned int strindex;
312 {
313 Elf_Internal_Shdr *hdr;
314
315 if (strindex == 0)
316 return "";
317
318 hdr = elf_elfsections (abfd)[shindex];
319
320 if (hdr->contents == NULL
321 && bfd_elf_get_str_section (abfd, shindex) == NULL)
322 return NULL;
323
324 if (strindex >= hdr->sh_size)
325 {
326 (*_bfd_error_handler)
327 (_("%s: invalid string offset %u >= %lu for section `%s'"),
328 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
329 ((shindex == elf_elfheader(abfd)->e_shstrndx
330 && strindex == hdr->sh_name)
331 ? ".shstrtab"
332 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
333 return "";
334 }
335
336 return ((char *) hdr->contents) + strindex;
337 }
338
339 /* Make a BFD section from an ELF section. We store a pointer to the
340 BFD section in the bfd_section field of the header. */
341
342 boolean
343 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
344 bfd *abfd;
345 Elf_Internal_Shdr *hdr;
346 const char *name;
347 {
348 asection *newsect;
349 flagword flags;
350
351 if (hdr->bfd_section != NULL)
352 {
353 BFD_ASSERT (strcmp (name,
354 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
355 return true;
356 }
357
358 newsect = bfd_make_section_anyway (abfd, name);
359 if (newsect == NULL)
360 return false;
361
362 newsect->filepos = hdr->sh_offset;
363
364 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
365 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
366 || ! bfd_set_section_alignment (abfd, newsect,
367 bfd_log2 (hdr->sh_addralign)))
368 return false;
369
370 flags = SEC_NO_FLAGS;
371 if (hdr->sh_type != SHT_NOBITS)
372 flags |= SEC_HAS_CONTENTS;
373 if ((hdr->sh_flags & SHF_ALLOC) != 0)
374 {
375 flags |= SEC_ALLOC;
376 if (hdr->sh_type != SHT_NOBITS)
377 flags |= SEC_LOAD;
378 }
379 if ((hdr->sh_flags & SHF_WRITE) == 0)
380 flags |= SEC_READONLY;
381 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
382 flags |= SEC_CODE;
383 else if ((flags & SEC_LOAD) != 0)
384 flags |= SEC_DATA;
385
386 /* The debugging sections appear to be recognized only by name, not
387 any sort of flag. */
388 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
389 || strncmp (name, ".line", sizeof ".line" - 1) == 0
390 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
391 flags |= SEC_DEBUGGING;
392
393 /* As a GNU extension, if the name begins with .gnu.linkonce, we
394 only link a single copy of the section. This is used to support
395 g++. g++ will emit each template expansion in its own section.
396 The symbols will be defined as weak, so that multiple definitions
397 are permitted. The GNU linker extension is to actually discard
398 all but one of the sections. */
399 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
400 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
401
402 if (! bfd_set_section_flags (abfd, newsect, flags))
403 return false;
404
405 if ((flags & SEC_ALLOC) != 0)
406 {
407 Elf_Internal_Phdr *phdr;
408 unsigned int i;
409
410 /* Look through the phdrs to see if we need to adjust the lma.
411 If all the p_paddr fields are zero, we ignore them, since
412 some ELF linkers produce such output. */
413 phdr = elf_tdata (abfd)->phdr;
414 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
415 {
416 if (phdr->p_paddr != 0)
417 break;
418 }
419 if (i < elf_elfheader (abfd)->e_phnum)
420 {
421 phdr = elf_tdata (abfd)->phdr;
422 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
423 {
424 if (phdr->p_type == PT_LOAD
425 && phdr->p_vaddr != phdr->p_paddr
426 && phdr->p_vaddr <= hdr->sh_addr
427 && (phdr->p_vaddr + phdr->p_memsz
428 >= hdr->sh_addr + hdr->sh_size)
429 && ((flags & SEC_LOAD) == 0
430 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
431 && (phdr->p_offset + phdr->p_filesz
432 >= hdr->sh_offset + hdr->sh_size))))
433 {
434 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
435 break;
436 }
437 }
438 }
439 }
440
441 hdr->bfd_section = newsect;
442 elf_section_data (newsect)->this_hdr = *hdr;
443
444 return true;
445 }
446
447 /*
448 INTERNAL_FUNCTION
449 bfd_elf_find_section
450
451 SYNOPSIS
452 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
453
454 DESCRIPTION
455 Helper functions for GDB to locate the string tables.
456 Since BFD hides string tables from callers, GDB needs to use an
457 internal hook to find them. Sun's .stabstr, in particular,
458 isn't even pointed to by the .stab section, so ordinary
459 mechanisms wouldn't work to find it, even if we had some.
460 */
461
462 struct elf_internal_shdr *
463 bfd_elf_find_section (abfd, name)
464 bfd * abfd;
465 char *name;
466 {
467 Elf_Internal_Shdr **i_shdrp;
468 char *shstrtab;
469 unsigned int max;
470 unsigned int i;
471
472 i_shdrp = elf_elfsections (abfd);
473 if (i_shdrp != NULL)
474 {
475 shstrtab = bfd_elf_get_str_section
476 (abfd, elf_elfheader (abfd)->e_shstrndx);
477 if (shstrtab != NULL)
478 {
479 max = elf_elfheader (abfd)->e_shnum;
480 for (i = 1; i < max; i++)
481 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
482 return i_shdrp[i];
483 }
484 }
485 return 0;
486 }
487
488 const char *const bfd_elf_section_type_names[] = {
489 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
490 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
491 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
492 };
493
494 /* ELF relocs are against symbols. If we are producing relocateable
495 output, and the reloc is against an external symbol, and nothing
496 has given us any additional addend, the resulting reloc will also
497 be against the same symbol. In such a case, we don't want to
498 change anything about the way the reloc is handled, since it will
499 all be done at final link time. Rather than put special case code
500 into bfd_perform_relocation, all the reloc types use this howto
501 function. It just short circuits the reloc if producing
502 relocateable output against an external symbol. */
503
504 /*ARGSUSED*/
505 bfd_reloc_status_type
506 bfd_elf_generic_reloc (abfd,
507 reloc_entry,
508 symbol,
509 data,
510 input_section,
511 output_bfd,
512 error_message)
513 bfd *abfd ATTRIBUTE_UNUSED;
514 arelent *reloc_entry;
515 asymbol *symbol;
516 PTR data ATTRIBUTE_UNUSED;
517 asection *input_section;
518 bfd *output_bfd;
519 char **error_message ATTRIBUTE_UNUSED;
520 {
521 if (output_bfd != (bfd *) NULL
522 && (symbol->flags & BSF_SECTION_SYM) == 0
523 && (! reloc_entry->howto->partial_inplace
524 || reloc_entry->addend == 0))
525 {
526 reloc_entry->address += input_section->output_offset;
527 return bfd_reloc_ok;
528 }
529
530 return bfd_reloc_continue;
531 }
532 \f
533 /* Print out the program headers. */
534
535 boolean
536 _bfd_elf_print_private_bfd_data (abfd, farg)
537 bfd *abfd;
538 PTR farg;
539 {
540 FILE *f = (FILE *) farg;
541 Elf_Internal_Phdr *p;
542 asection *s;
543 bfd_byte *dynbuf = NULL;
544
545 p = elf_tdata (abfd)->phdr;
546 if (p != NULL)
547 {
548 unsigned int i, c;
549
550 fprintf (f, _("\nProgram Header:\n"));
551 c = elf_elfheader (abfd)->e_phnum;
552 for (i = 0; i < c; i++, p++)
553 {
554 const char *s;
555 char buf[20];
556
557 switch (p->p_type)
558 {
559 case PT_NULL: s = "NULL"; break;
560 case PT_LOAD: s = "LOAD"; break;
561 case PT_DYNAMIC: s = "DYNAMIC"; break;
562 case PT_INTERP: s = "INTERP"; break;
563 case PT_NOTE: s = "NOTE"; break;
564 case PT_SHLIB: s = "SHLIB"; break;
565 case PT_PHDR: s = "PHDR"; break;
566 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
567 }
568 fprintf (f, "%8s off 0x", s);
569 fprintf_vma (f, p->p_offset);
570 fprintf (f, " vaddr 0x");
571 fprintf_vma (f, p->p_vaddr);
572 fprintf (f, " paddr 0x");
573 fprintf_vma (f, p->p_paddr);
574 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
575 fprintf (f, " filesz 0x");
576 fprintf_vma (f, p->p_filesz);
577 fprintf (f, " memsz 0x");
578 fprintf_vma (f, p->p_memsz);
579 fprintf (f, " flags %c%c%c",
580 (p->p_flags & PF_R) != 0 ? 'r' : '-',
581 (p->p_flags & PF_W) != 0 ? 'w' : '-',
582 (p->p_flags & PF_X) != 0 ? 'x' : '-');
583 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
584 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
585 fprintf (f, "\n");
586 }
587 }
588
589 s = bfd_get_section_by_name (abfd, ".dynamic");
590 if (s != NULL)
591 {
592 int elfsec;
593 unsigned long link;
594 bfd_byte *extdyn, *extdynend;
595 size_t extdynsize;
596 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
597
598 fprintf (f, _("\nDynamic Section:\n"));
599
600 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
601 if (dynbuf == NULL)
602 goto error_return;
603 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
604 s->_raw_size))
605 goto error_return;
606
607 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
608 if (elfsec == -1)
609 goto error_return;
610 link = elf_elfsections (abfd)[elfsec]->sh_link;
611
612 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
613 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
614
615 extdyn = dynbuf;
616 extdynend = extdyn + s->_raw_size;
617 for (; extdyn < extdynend; extdyn += extdynsize)
618 {
619 Elf_Internal_Dyn dyn;
620 const char *name;
621 char ab[20];
622 boolean stringp;
623
624 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
625
626 if (dyn.d_tag == DT_NULL)
627 break;
628
629 stringp = false;
630 switch (dyn.d_tag)
631 {
632 default:
633 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
634 name = ab;
635 break;
636
637 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
638 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
639 case DT_PLTGOT: name = "PLTGOT"; break;
640 case DT_HASH: name = "HASH"; break;
641 case DT_STRTAB: name = "STRTAB"; break;
642 case DT_SYMTAB: name = "SYMTAB"; break;
643 case DT_RELA: name = "RELA"; break;
644 case DT_RELASZ: name = "RELASZ"; break;
645 case DT_RELAENT: name = "RELAENT"; break;
646 case DT_STRSZ: name = "STRSZ"; break;
647 case DT_SYMENT: name = "SYMENT"; break;
648 case DT_INIT: name = "INIT"; break;
649 case DT_FINI: name = "FINI"; break;
650 case DT_SONAME: name = "SONAME"; stringp = true; break;
651 case DT_RPATH: name = "RPATH"; stringp = true; break;
652 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
653 case DT_REL: name = "REL"; break;
654 case DT_RELSZ: name = "RELSZ"; break;
655 case DT_RELENT: name = "RELENT"; break;
656 case DT_PLTREL: name = "PLTREL"; break;
657 case DT_DEBUG: name = "DEBUG"; break;
658 case DT_TEXTREL: name = "TEXTREL"; break;
659 case DT_JMPREL: name = "JMPREL"; break;
660 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
661 case DT_FILTER: name = "FILTER"; stringp = true; break;
662 case DT_VERSYM: name = "VERSYM"; break;
663 case DT_VERDEF: name = "VERDEF"; break;
664 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
665 case DT_VERNEED: name = "VERNEED"; break;
666 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
667 }
668
669 fprintf (f, " %-11s ", name);
670 if (! stringp)
671 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
672 else
673 {
674 const char *string;
675
676 string = bfd_elf_string_from_elf_section (abfd, link,
677 dyn.d_un.d_val);
678 if (string == NULL)
679 goto error_return;
680 fprintf (f, "%s", string);
681 }
682 fprintf (f, "\n");
683 }
684
685 free (dynbuf);
686 dynbuf = NULL;
687 }
688
689 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
690 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
691 {
692 if (! _bfd_elf_slurp_version_tables (abfd))
693 return false;
694 }
695
696 if (elf_dynverdef (abfd) != 0)
697 {
698 Elf_Internal_Verdef *t;
699
700 fprintf (f, _("\nVersion definitions:\n"));
701 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
702 {
703 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
704 t->vd_flags, t->vd_hash, t->vd_nodename);
705 if (t->vd_auxptr->vda_nextptr != NULL)
706 {
707 Elf_Internal_Verdaux *a;
708
709 fprintf (f, "\t");
710 for (a = t->vd_auxptr->vda_nextptr;
711 a != NULL;
712 a = a->vda_nextptr)
713 fprintf (f, "%s ", a->vda_nodename);
714 fprintf (f, "\n");
715 }
716 }
717 }
718
719 if (elf_dynverref (abfd) != 0)
720 {
721 Elf_Internal_Verneed *t;
722
723 fprintf (f, _("\nVersion References:\n"));
724 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
725 {
726 Elf_Internal_Vernaux *a;
727
728 fprintf (f, _(" required from %s:\n"), t->vn_filename);
729 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
730 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
731 a->vna_flags, a->vna_other, a->vna_nodename);
732 }
733 }
734
735 return true;
736
737 error_return:
738 if (dynbuf != NULL)
739 free (dynbuf);
740 return false;
741 }
742
743 /* Display ELF-specific fields of a symbol. */
744
745 void
746 bfd_elf_print_symbol (abfd, filep, symbol, how)
747 bfd *abfd;
748 PTR filep;
749 asymbol *symbol;
750 bfd_print_symbol_type how;
751 {
752 FILE *file = (FILE *) filep;
753 switch (how)
754 {
755 case bfd_print_symbol_name:
756 fprintf (file, "%s", symbol->name);
757 break;
758 case bfd_print_symbol_more:
759 fprintf (file, "elf ");
760 fprintf_vma (file, symbol->value);
761 fprintf (file, " %lx", (long) symbol->flags);
762 break;
763 case bfd_print_symbol_all:
764 {
765 CONST char *section_name;
766 CONST char *name = NULL;
767 struct elf_backend_data *bed;
768 unsigned char st_other;
769
770 section_name = symbol->section ? symbol->section->name : "(*none*)";
771
772 bed = get_elf_backend_data (abfd);
773 if (bed->elf_backend_print_symbol_all)
774 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
775
776 if (name == NULL)
777 {
778 name = symbol->name;
779 bfd_print_symbol_vandf ((PTR) file, symbol);
780 }
781
782 fprintf (file, " %s\t", section_name);
783 /* Print the "other" value for a symbol. For common symbols,
784 we've already printed the size; now print the alignment.
785 For other symbols, we have no specified alignment, and
786 we've printed the address; now print the size. */
787 fprintf_vma (file,
788 (bfd_is_com_section (symbol->section)
789 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
790 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
791
792 /* If we have version information, print it. */
793 if (elf_tdata (abfd)->dynversym_section != 0
794 && (elf_tdata (abfd)->dynverdef_section != 0
795 || elf_tdata (abfd)->dynverref_section != 0))
796 {
797 unsigned int vernum;
798 const char *version_string;
799
800 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
801
802 if (vernum == 0)
803 version_string = "";
804 else if (vernum == 1)
805 version_string = "Base";
806 else if (vernum <= elf_tdata (abfd)->cverdefs)
807 version_string =
808 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
809 else
810 {
811 Elf_Internal_Verneed *t;
812
813 version_string = "";
814 for (t = elf_tdata (abfd)->verref;
815 t != NULL;
816 t = t->vn_nextref)
817 {
818 Elf_Internal_Vernaux *a;
819
820 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
821 {
822 if (a->vna_other == vernum)
823 {
824 version_string = a->vna_nodename;
825 break;
826 }
827 }
828 }
829 }
830
831 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
832 fprintf (file, " %-11s", version_string);
833 else
834 {
835 int i;
836
837 fprintf (file, " (%s)", version_string);
838 for (i = 10 - strlen (version_string); i > 0; --i)
839 putc (' ', file);
840 }
841 }
842
843 /* If the st_other field is not zero, print it. */
844 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
845
846 switch (st_other)
847 {
848 case 0: break;
849 case STV_INTERNAL: fprintf (file, " .internal"); break;
850 case STV_HIDDEN: fprintf (file, " .hidden"); break;
851 case STV_PROTECTED: fprintf (file, " .protected"); break;
852 default:
853 /* Some other non-defined flags are also present, so print
854 everything hex. */
855 fprintf (file, " 0x%02x", (unsigned int) st_other);
856 }
857
858 fprintf (file, " %s", name);
859 }
860 break;
861 }
862 }
863 \f
864 /* Create an entry in an ELF linker hash table. */
865
866 struct bfd_hash_entry *
867 _bfd_elf_link_hash_newfunc (entry, table, string)
868 struct bfd_hash_entry *entry;
869 struct bfd_hash_table *table;
870 const char *string;
871 {
872 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
873
874 /* Allocate the structure if it has not already been allocated by a
875 subclass. */
876 if (ret == (struct elf_link_hash_entry *) NULL)
877 ret = ((struct elf_link_hash_entry *)
878 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
879 if (ret == (struct elf_link_hash_entry *) NULL)
880 return (struct bfd_hash_entry *) ret;
881
882 /* Call the allocation method of the superclass. */
883 ret = ((struct elf_link_hash_entry *)
884 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
885 table, string));
886 if (ret != (struct elf_link_hash_entry *) NULL)
887 {
888 /* Set local fields. */
889 ret->indx = -1;
890 ret->size = 0;
891 ret->dynindx = -1;
892 ret->dynstr_index = 0;
893 ret->weakdef = NULL;
894 ret->got.offset = (bfd_vma) -1;
895 ret->plt.offset = (bfd_vma) -1;
896 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
897 ret->verinfo.verdef = NULL;
898 ret->vtable_entries_used = NULL;
899 ret->vtable_entries_size = 0;
900 ret->vtable_parent = NULL;
901 ret->type = STT_NOTYPE;
902 ret->other = 0;
903 /* Assume that we have been called by a non-ELF symbol reader.
904 This flag is then reset by the code which reads an ELF input
905 file. This ensures that a symbol created by a non-ELF symbol
906 reader will have the flag set correctly. */
907 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
908 }
909
910 return (struct bfd_hash_entry *) ret;
911 }
912
913 /* Copy data from an indirect symbol to its direct symbol, hiding the
914 old indirect symbol. */
915
916 void
917 _bfd_elf_link_hash_copy_indirect (dir, ind)
918 struct elf_link_hash_entry *dir, *ind;
919 {
920 /* Copy down any references that we may have already seen to the
921 symbol which just became indirect. */
922
923 dir->elf_link_hash_flags |=
924 (ind->elf_link_hash_flags
925 & (ELF_LINK_HASH_REF_DYNAMIC
926 | ELF_LINK_HASH_REF_REGULAR
927 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
928 | ELF_LINK_NON_GOT_REF));
929
930 /* Copy over the global and procedure linkage table offset entries.
931 These may have been already set up by a check_relocs routine. */
932 if (dir->got.offset == (bfd_vma) -1)
933 {
934 dir->got.offset = ind->got.offset;
935 ind->got.offset = (bfd_vma) -1;
936 }
937 BFD_ASSERT (ind->got.offset == (bfd_vma) -1);
938
939 if (dir->plt.offset == (bfd_vma) -1)
940 {
941 dir->plt.offset = ind->plt.offset;
942 ind->plt.offset = (bfd_vma) -1;
943 }
944 BFD_ASSERT (ind->plt.offset == (bfd_vma) -1);
945
946 if (dir->dynindx == -1)
947 {
948 dir->dynindx = ind->dynindx;
949 dir->dynstr_index = ind->dynstr_index;
950 ind->dynindx = -1;
951 ind->dynstr_index = 0;
952 }
953 BFD_ASSERT (ind->dynindx == -1);
954 }
955
956 void
957 _bfd_elf_link_hash_hide_symbol(info, h)
958 struct bfd_link_info *info ATTRIBUTE_UNUSED;
959 struct elf_link_hash_entry *h;
960 {
961 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
962 h->dynindx = -1;
963 h->plt.offset = (bfd_vma) -1;
964 }
965
966 /* Initialize an ELF linker hash table. */
967
968 boolean
969 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
970 struct elf_link_hash_table *table;
971 bfd *abfd;
972 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
973 struct bfd_hash_table *,
974 const char *));
975 {
976 table->dynamic_sections_created = false;
977 table->dynobj = NULL;
978 /* The first dynamic symbol is a dummy. */
979 table->dynsymcount = 1;
980 table->dynstr = NULL;
981 table->bucketcount = 0;
982 table->needed = NULL;
983 table->hgot = NULL;
984 table->stab_info = NULL;
985 table->dynlocal = NULL;
986 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
987 }
988
989 /* Create an ELF linker hash table. */
990
991 struct bfd_link_hash_table *
992 _bfd_elf_link_hash_table_create (abfd)
993 bfd *abfd;
994 {
995 struct elf_link_hash_table *ret;
996
997 ret = ((struct elf_link_hash_table *)
998 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
999 if (ret == (struct elf_link_hash_table *) NULL)
1000 return NULL;
1001
1002 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1003 {
1004 bfd_release (abfd, ret);
1005 return NULL;
1006 }
1007
1008 return &ret->root;
1009 }
1010
1011 /* This is a hook for the ELF emulation code in the generic linker to
1012 tell the backend linker what file name to use for the DT_NEEDED
1013 entry for a dynamic object. The generic linker passes name as an
1014 empty string to indicate that no DT_NEEDED entry should be made. */
1015
1016 void
1017 bfd_elf_set_dt_needed_name (abfd, name)
1018 bfd *abfd;
1019 const char *name;
1020 {
1021 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1022 && bfd_get_format (abfd) == bfd_object)
1023 elf_dt_name (abfd) = name;
1024 }
1025
1026 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1027 the linker ELF emulation code. */
1028
1029 struct bfd_link_needed_list *
1030 bfd_elf_get_needed_list (abfd, info)
1031 bfd *abfd ATTRIBUTE_UNUSED;
1032 struct bfd_link_info *info;
1033 {
1034 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1035 return NULL;
1036 return elf_hash_table (info)->needed;
1037 }
1038
1039 /* Get the name actually used for a dynamic object for a link. This
1040 is the SONAME entry if there is one. Otherwise, it is the string
1041 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1042
1043 const char *
1044 bfd_elf_get_dt_soname (abfd)
1045 bfd *abfd;
1046 {
1047 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1048 && bfd_get_format (abfd) == bfd_object)
1049 return elf_dt_name (abfd);
1050 return NULL;
1051 }
1052
1053 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1054 the ELF linker emulation code. */
1055
1056 boolean
1057 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1058 bfd *abfd;
1059 struct bfd_link_needed_list **pneeded;
1060 {
1061 asection *s;
1062 bfd_byte *dynbuf = NULL;
1063 int elfsec;
1064 unsigned long link;
1065 bfd_byte *extdyn, *extdynend;
1066 size_t extdynsize;
1067 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1068
1069 *pneeded = NULL;
1070
1071 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1072 || bfd_get_format (abfd) != bfd_object)
1073 return true;
1074
1075 s = bfd_get_section_by_name (abfd, ".dynamic");
1076 if (s == NULL || s->_raw_size == 0)
1077 return true;
1078
1079 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1080 if (dynbuf == NULL)
1081 goto error_return;
1082
1083 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1084 s->_raw_size))
1085 goto error_return;
1086
1087 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1088 if (elfsec == -1)
1089 goto error_return;
1090
1091 link = elf_elfsections (abfd)[elfsec]->sh_link;
1092
1093 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1094 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1095
1096 extdyn = dynbuf;
1097 extdynend = extdyn + s->_raw_size;
1098 for (; extdyn < extdynend; extdyn += extdynsize)
1099 {
1100 Elf_Internal_Dyn dyn;
1101
1102 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1103
1104 if (dyn.d_tag == DT_NULL)
1105 break;
1106
1107 if (dyn.d_tag == DT_NEEDED)
1108 {
1109 const char *string;
1110 struct bfd_link_needed_list *l;
1111
1112 string = bfd_elf_string_from_elf_section (abfd, link,
1113 dyn.d_un.d_val);
1114 if (string == NULL)
1115 goto error_return;
1116
1117 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1118 if (l == NULL)
1119 goto error_return;
1120
1121 l->by = abfd;
1122 l->name = string;
1123 l->next = *pneeded;
1124 *pneeded = l;
1125 }
1126 }
1127
1128 free (dynbuf);
1129
1130 return true;
1131
1132 error_return:
1133 if (dynbuf != NULL)
1134 free (dynbuf);
1135 return false;
1136 }
1137 \f
1138 /* Allocate an ELF string table--force the first byte to be zero. */
1139
1140 struct bfd_strtab_hash *
1141 _bfd_elf_stringtab_init ()
1142 {
1143 struct bfd_strtab_hash *ret;
1144
1145 ret = _bfd_stringtab_init ();
1146 if (ret != NULL)
1147 {
1148 bfd_size_type loc;
1149
1150 loc = _bfd_stringtab_add (ret, "", true, false);
1151 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1152 if (loc == (bfd_size_type) -1)
1153 {
1154 _bfd_stringtab_free (ret);
1155 ret = NULL;
1156 }
1157 }
1158 return ret;
1159 }
1160 \f
1161 /* ELF .o/exec file reading */
1162
1163 /* Create a new bfd section from an ELF section header. */
1164
1165 boolean
1166 bfd_section_from_shdr (abfd, shindex)
1167 bfd *abfd;
1168 unsigned int shindex;
1169 {
1170 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1171 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1172 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1173 char *name;
1174
1175 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1176
1177 switch (hdr->sh_type)
1178 {
1179 case SHT_NULL:
1180 /* Inactive section. Throw it away. */
1181 return true;
1182
1183 case SHT_PROGBITS: /* Normal section with contents. */
1184 case SHT_DYNAMIC: /* Dynamic linking information. */
1185 case SHT_NOBITS: /* .bss section. */
1186 case SHT_HASH: /* .hash section. */
1187 case SHT_NOTE: /* .note section. */
1188 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1189
1190 case SHT_SYMTAB: /* A symbol table */
1191 if (elf_onesymtab (abfd) == shindex)
1192 return true;
1193
1194 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1195 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1196 elf_onesymtab (abfd) = shindex;
1197 elf_tdata (abfd)->symtab_hdr = *hdr;
1198 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1199 abfd->flags |= HAS_SYMS;
1200
1201 /* Sometimes a shared object will map in the symbol table. If
1202 SHF_ALLOC is set, and this is a shared object, then we also
1203 treat this section as a BFD section. We can not base the
1204 decision purely on SHF_ALLOC, because that flag is sometimes
1205 set in a relocateable object file, which would confuse the
1206 linker. */
1207 if ((hdr->sh_flags & SHF_ALLOC) != 0
1208 && (abfd->flags & DYNAMIC) != 0
1209 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1210 return false;
1211
1212 return true;
1213
1214 case SHT_DYNSYM: /* A dynamic symbol table */
1215 if (elf_dynsymtab (abfd) == shindex)
1216 return true;
1217
1218 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1219 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1220 elf_dynsymtab (abfd) = shindex;
1221 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1222 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1223 abfd->flags |= HAS_SYMS;
1224
1225 /* Besides being a symbol table, we also treat this as a regular
1226 section, so that objcopy can handle it. */
1227 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1228
1229 case SHT_STRTAB: /* A string table */
1230 if (hdr->bfd_section != NULL)
1231 return true;
1232 if (ehdr->e_shstrndx == shindex)
1233 {
1234 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1235 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1236 return true;
1237 }
1238 {
1239 unsigned int i;
1240
1241 for (i = 1; i < ehdr->e_shnum; i++)
1242 {
1243 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1244 if (hdr2->sh_link == shindex)
1245 {
1246 if (! bfd_section_from_shdr (abfd, i))
1247 return false;
1248 if (elf_onesymtab (abfd) == i)
1249 {
1250 elf_tdata (abfd)->strtab_hdr = *hdr;
1251 elf_elfsections (abfd)[shindex] =
1252 &elf_tdata (abfd)->strtab_hdr;
1253 return true;
1254 }
1255 if (elf_dynsymtab (abfd) == i)
1256 {
1257 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1258 elf_elfsections (abfd)[shindex] = hdr =
1259 &elf_tdata (abfd)->dynstrtab_hdr;
1260 /* We also treat this as a regular section, so
1261 that objcopy can handle it. */
1262 break;
1263 }
1264 #if 0 /* Not handling other string tables specially right now. */
1265 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1266 /* We have a strtab for some random other section. */
1267 newsect = (asection *) hdr2->bfd_section;
1268 if (!newsect)
1269 break;
1270 hdr->bfd_section = newsect;
1271 hdr2 = &elf_section_data (newsect)->str_hdr;
1272 *hdr2 = *hdr;
1273 elf_elfsections (abfd)[shindex] = hdr2;
1274 #endif
1275 }
1276 }
1277 }
1278
1279 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1280
1281 case SHT_REL:
1282 case SHT_RELA:
1283 /* *These* do a lot of work -- but build no sections! */
1284 {
1285 asection *target_sect;
1286 Elf_Internal_Shdr *hdr2;
1287
1288 /* Check for a bogus link to avoid crashing. */
1289 if (hdr->sh_link >= ehdr->e_shnum)
1290 {
1291 ((*_bfd_error_handler)
1292 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1293 bfd_get_filename (abfd), hdr->sh_link, name, shindex));
1294 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1295 }
1296
1297 /* For some incomprehensible reason Oracle distributes
1298 libraries for Solaris in which some of the objects have
1299 bogus sh_link fields. It would be nice if we could just
1300 reject them, but, unfortunately, some people need to use
1301 them. We scan through the section headers; if we find only
1302 one suitable symbol table, we clobber the sh_link to point
1303 to it. I hope this doesn't break anything. */
1304 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1305 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1306 {
1307 int scan;
1308 int found;
1309
1310 found = 0;
1311 for (scan = 1; scan < ehdr->e_shnum; scan++)
1312 {
1313 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1314 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1315 {
1316 if (found != 0)
1317 {
1318 found = 0;
1319 break;
1320 }
1321 found = scan;
1322 }
1323 }
1324 if (found != 0)
1325 hdr->sh_link = found;
1326 }
1327
1328 /* Get the symbol table. */
1329 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1330 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1331 return false;
1332
1333 /* If this reloc section does not use the main symbol table we
1334 don't treat it as a reloc section. BFD can't adequately
1335 represent such a section, so at least for now, we don't
1336 try. We just present it as a normal section. We also
1337 can't use it as a reloc section if it points to the null
1338 section. */
1339 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1340 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1341
1342 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1343 return false;
1344 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1345 if (target_sect == NULL)
1346 return false;
1347
1348 if ((target_sect->flags & SEC_RELOC) == 0
1349 || target_sect->reloc_count == 0)
1350 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1351 else
1352 {
1353 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1354 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1355 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1356 }
1357 *hdr2 = *hdr;
1358 elf_elfsections (abfd)[shindex] = hdr2;
1359 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1360 target_sect->flags |= SEC_RELOC;
1361 target_sect->relocation = NULL;
1362 target_sect->rel_filepos = hdr->sh_offset;
1363 /* In the section to which the relocations apply, mark whether
1364 its relocations are of the REL or RELA variety. */
1365 if (hdr->sh_size != 0)
1366 elf_section_data (target_sect)->use_rela_p
1367 = (hdr->sh_type == SHT_RELA);
1368 abfd->flags |= HAS_RELOC;
1369 return true;
1370 }
1371 break;
1372
1373 case SHT_GNU_verdef:
1374 elf_dynverdef (abfd) = shindex;
1375 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1376 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1377 break;
1378
1379 case SHT_GNU_versym:
1380 elf_dynversym (abfd) = shindex;
1381 elf_tdata (abfd)->dynversym_hdr = *hdr;
1382 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1383 break;
1384
1385 case SHT_GNU_verneed:
1386 elf_dynverref (abfd) = shindex;
1387 elf_tdata (abfd)->dynverref_hdr = *hdr;
1388 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1389 break;
1390
1391 case SHT_SHLIB:
1392 return true;
1393
1394 default:
1395 /* Check for any processor-specific section types. */
1396 {
1397 if (bed->elf_backend_section_from_shdr)
1398 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1399 }
1400 break;
1401 }
1402
1403 return true;
1404 }
1405
1406 /* Given an ELF section number, retrieve the corresponding BFD
1407 section. */
1408
1409 asection *
1410 bfd_section_from_elf_index (abfd, index)
1411 bfd *abfd;
1412 unsigned int index;
1413 {
1414 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1415 if (index >= elf_elfheader (abfd)->e_shnum)
1416 return NULL;
1417 return elf_elfsections (abfd)[index]->bfd_section;
1418 }
1419
1420 boolean
1421 _bfd_elf_new_section_hook (abfd, sec)
1422 bfd *abfd;
1423 asection *sec;
1424 {
1425 struct bfd_elf_section_data *sdata;
1426
1427 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata));
1428 if (!sdata)
1429 return false;
1430 sec->used_by_bfd = (PTR) sdata;
1431
1432 /* Indicate whether or not this section should use RELA relocations. */
1433 sdata->use_rela_p
1434 = get_elf_backend_data (abfd)->default_use_rela_p;
1435
1436 return true;
1437 }
1438
1439 /* Create a new bfd section from an ELF program header.
1440
1441 Since program segments have no names, we generate a synthetic name
1442 of the form segment<NUM>, where NUM is generally the index in the
1443 program header table. For segments that are split (see below) we
1444 generate the names segment<NUM>a and segment<NUM>b.
1445
1446 Note that some program segments may have a file size that is different than
1447 (less than) the memory size. All this means is that at execution the
1448 system must allocate the amount of memory specified by the memory size,
1449 but only initialize it with the first "file size" bytes read from the
1450 file. This would occur for example, with program segments consisting
1451 of combined data+bss.
1452
1453 To handle the above situation, this routine generates TWO bfd sections
1454 for the single program segment. The first has the length specified by
1455 the file size of the segment, and the second has the length specified
1456 by the difference between the two sizes. In effect, the segment is split
1457 into it's initialized and uninitialized parts.
1458
1459 */
1460
1461 boolean
1462 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1463 bfd *abfd;
1464 Elf_Internal_Phdr *hdr;
1465 int index;
1466 const char *typename;
1467 {
1468 asection *newsect;
1469 char *name;
1470 char namebuf[64];
1471 int split;
1472
1473 split = ((hdr->p_memsz > 0)
1474 && (hdr->p_filesz > 0)
1475 && (hdr->p_memsz > hdr->p_filesz));
1476 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1477 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1478 if (!name)
1479 return false;
1480 strcpy (name, namebuf);
1481 newsect = bfd_make_section (abfd, name);
1482 if (newsect == NULL)
1483 return false;
1484 newsect->vma = hdr->p_vaddr;
1485 newsect->lma = hdr->p_paddr;
1486 newsect->_raw_size = hdr->p_filesz;
1487 newsect->filepos = hdr->p_offset;
1488 newsect->flags |= SEC_HAS_CONTENTS;
1489 if (hdr->p_type == PT_LOAD)
1490 {
1491 newsect->flags |= SEC_ALLOC;
1492 newsect->flags |= SEC_LOAD;
1493 if (hdr->p_flags & PF_X)
1494 {
1495 /* FIXME: all we known is that it has execute PERMISSION,
1496 may be data. */
1497 newsect->flags |= SEC_CODE;
1498 }
1499 }
1500 if (!(hdr->p_flags & PF_W))
1501 {
1502 newsect->flags |= SEC_READONLY;
1503 }
1504
1505 if (split)
1506 {
1507 sprintf (namebuf, "%s%db", typename, index);
1508 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1509 if (!name)
1510 return false;
1511 strcpy (name, namebuf);
1512 newsect = bfd_make_section (abfd, name);
1513 if (newsect == NULL)
1514 return false;
1515 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1516 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1517 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1518 if (hdr->p_type == PT_LOAD)
1519 {
1520 newsect->flags |= SEC_ALLOC;
1521 if (hdr->p_flags & PF_X)
1522 newsect->flags |= SEC_CODE;
1523 }
1524 if (!(hdr->p_flags & PF_W))
1525 newsect->flags |= SEC_READONLY;
1526 }
1527
1528 return true;
1529 }
1530
1531 boolean
1532 bfd_section_from_phdr (abfd, hdr, index)
1533 bfd *abfd;
1534 Elf_Internal_Phdr *hdr;
1535 int index;
1536 {
1537 struct elf_backend_data *bed;
1538
1539 switch (hdr->p_type)
1540 {
1541 case PT_NULL:
1542 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1543
1544 case PT_LOAD:
1545 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1546
1547 case PT_DYNAMIC:
1548 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1549
1550 case PT_INTERP:
1551 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1552
1553 case PT_NOTE:
1554 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1555 return false;
1556 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
1557 return false;
1558 return true;
1559
1560 case PT_SHLIB:
1561 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1562
1563 case PT_PHDR:
1564 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
1565
1566 default:
1567 /* Check for any processor-specific program segment types.
1568 If no handler for them, default to making "segment" sections. */
1569 bed = get_elf_backend_data (abfd);
1570 if (bed->elf_backend_section_from_phdr)
1571 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
1572 else
1573 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
1574 }
1575 }
1576
1577 /* Initialize REL_HDR, the section-header for new section, containing
1578 relocations against ASECT. If USE_RELA_P is true, we use RELA
1579 relocations; otherwise, we use REL relocations. */
1580
1581 boolean
1582 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1583 bfd *abfd;
1584 Elf_Internal_Shdr *rel_hdr;
1585 asection *asect;
1586 boolean use_rela_p;
1587 {
1588 char *name;
1589 struct elf_backend_data *bed;
1590
1591 bed = get_elf_backend_data (abfd);
1592 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1593 if (name == NULL)
1594 return false;
1595 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1596 rel_hdr->sh_name =
1597 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1598 true, false);
1599 if (rel_hdr->sh_name == (unsigned int) -1)
1600 return false;
1601 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1602 rel_hdr->sh_entsize = (use_rela_p
1603 ? bed->s->sizeof_rela
1604 : bed->s->sizeof_rel);
1605 rel_hdr->sh_addralign = bed->s->file_align;
1606 rel_hdr->sh_flags = 0;
1607 rel_hdr->sh_addr = 0;
1608 rel_hdr->sh_size = 0;
1609 rel_hdr->sh_offset = 0;
1610
1611 return true;
1612 }
1613
1614 /* Set up an ELF internal section header for a section. */
1615
1616 /*ARGSUSED*/
1617 static void
1618 elf_fake_sections (abfd, asect, failedptrarg)
1619 bfd *abfd;
1620 asection *asect;
1621 PTR failedptrarg;
1622 {
1623 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1624 boolean *failedptr = (boolean *) failedptrarg;
1625 Elf_Internal_Shdr *this_hdr;
1626
1627 if (*failedptr)
1628 {
1629 /* We already failed; just get out of the bfd_map_over_sections
1630 loop. */
1631 return;
1632 }
1633
1634 this_hdr = &elf_section_data (asect)->this_hdr;
1635
1636 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1637 asect->name,
1638 true, false);
1639 if (this_hdr->sh_name == (unsigned long) -1)
1640 {
1641 *failedptr = true;
1642 return;
1643 }
1644
1645 this_hdr->sh_flags = 0;
1646
1647 if ((asect->flags & SEC_ALLOC) != 0
1648 || asect->user_set_vma)
1649 this_hdr->sh_addr = asect->vma;
1650 else
1651 this_hdr->sh_addr = 0;
1652
1653 this_hdr->sh_offset = 0;
1654 this_hdr->sh_size = asect->_raw_size;
1655 this_hdr->sh_link = 0;
1656 this_hdr->sh_addralign = 1 << asect->alignment_power;
1657 /* The sh_entsize and sh_info fields may have been set already by
1658 copy_private_section_data. */
1659
1660 this_hdr->bfd_section = asect;
1661 this_hdr->contents = NULL;
1662
1663 /* FIXME: This should not be based on section names. */
1664 if (strcmp (asect->name, ".dynstr") == 0)
1665 this_hdr->sh_type = SHT_STRTAB;
1666 else if (strcmp (asect->name, ".hash") == 0)
1667 {
1668 this_hdr->sh_type = SHT_HASH;
1669 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
1670 }
1671 else if (strcmp (asect->name, ".dynsym") == 0)
1672 {
1673 this_hdr->sh_type = SHT_DYNSYM;
1674 this_hdr->sh_entsize = bed->s->sizeof_sym;
1675 }
1676 else if (strcmp (asect->name, ".dynamic") == 0)
1677 {
1678 this_hdr->sh_type = SHT_DYNAMIC;
1679 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1680 }
1681 else if (strncmp (asect->name, ".rela", 5) == 0
1682 && get_elf_backend_data (abfd)->may_use_rela_p)
1683 {
1684 this_hdr->sh_type = SHT_RELA;
1685 this_hdr->sh_entsize = bed->s->sizeof_rela;
1686 }
1687 else if (strncmp (asect->name, ".rel", 4) == 0
1688 && get_elf_backend_data (abfd)->may_use_rel_p)
1689 {
1690 this_hdr->sh_type = SHT_REL;
1691 this_hdr->sh_entsize = bed->s->sizeof_rel;
1692 }
1693 else if (strncmp (asect->name, ".note", 5) == 0)
1694 this_hdr->sh_type = SHT_NOTE;
1695 else if (strncmp (asect->name, ".stab", 5) == 0
1696 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1697 this_hdr->sh_type = SHT_STRTAB;
1698 else if (strcmp (asect->name, ".gnu.version") == 0)
1699 {
1700 this_hdr->sh_type = SHT_GNU_versym;
1701 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1702 }
1703 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1704 {
1705 this_hdr->sh_type = SHT_GNU_verdef;
1706 this_hdr->sh_entsize = 0;
1707 /* objcopy or strip will copy over sh_info, but may not set
1708 cverdefs. The linker will set cverdefs, but sh_info will be
1709 zero. */
1710 if (this_hdr->sh_info == 0)
1711 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1712 else
1713 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1714 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1715 }
1716 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1717 {
1718 this_hdr->sh_type = SHT_GNU_verneed;
1719 this_hdr->sh_entsize = 0;
1720 /* objcopy or strip will copy over sh_info, but may not set
1721 cverrefs. The linker will set cverrefs, but sh_info will be
1722 zero. */
1723 if (this_hdr->sh_info == 0)
1724 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1725 else
1726 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1727 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1728 }
1729 else if ((asect->flags & SEC_ALLOC) != 0
1730 && (asect->flags & SEC_LOAD) != 0)
1731 this_hdr->sh_type = SHT_PROGBITS;
1732 else if ((asect->flags & SEC_ALLOC) != 0
1733 && ((asect->flags & SEC_LOAD) == 0))
1734 this_hdr->sh_type = SHT_NOBITS;
1735 else
1736 {
1737 /* Who knows? */
1738 this_hdr->sh_type = SHT_PROGBITS;
1739 }
1740
1741 if ((asect->flags & SEC_ALLOC) != 0)
1742 this_hdr->sh_flags |= SHF_ALLOC;
1743 if ((asect->flags & SEC_READONLY) == 0)
1744 this_hdr->sh_flags |= SHF_WRITE;
1745 if ((asect->flags & SEC_CODE) != 0)
1746 this_hdr->sh_flags |= SHF_EXECINSTR;
1747
1748 /* Check for processor-specific section types. */
1749 if (bed->elf_backend_fake_sections)
1750 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1751
1752 /* If the section has relocs, set up a section header for the
1753 SHT_REL[A] section. If two relocation sections are required for
1754 this section, it is up to the processor-specific back-end to
1755 create the other. */
1756 if ((asect->flags & SEC_RELOC) != 0
1757 && !_bfd_elf_init_reloc_shdr (abfd,
1758 &elf_section_data (asect)->rel_hdr,
1759 asect,
1760 elf_section_data (asect)->use_rela_p))
1761 *failedptr = true;
1762 }
1763
1764 /* Assign all ELF section numbers. The dummy first section is handled here
1765 too. The link/info pointers for the standard section types are filled
1766 in here too, while we're at it. */
1767
1768 static boolean
1769 assign_section_numbers (abfd)
1770 bfd *abfd;
1771 {
1772 struct elf_obj_tdata *t = elf_tdata (abfd);
1773 asection *sec;
1774 unsigned int section_number;
1775 Elf_Internal_Shdr **i_shdrp;
1776
1777 section_number = 1;
1778
1779 for (sec = abfd->sections; sec; sec = sec->next)
1780 {
1781 struct bfd_elf_section_data *d = elf_section_data (sec);
1782
1783 d->this_idx = section_number++;
1784 if ((sec->flags & SEC_RELOC) == 0)
1785 d->rel_idx = 0;
1786 else
1787 d->rel_idx = section_number++;
1788
1789 if (d->rel_hdr2)
1790 d->rel_idx2 = section_number++;
1791 else
1792 d->rel_idx2 = 0;
1793 }
1794
1795 t->shstrtab_section = section_number++;
1796 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1797 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1798
1799 if (bfd_get_symcount (abfd) > 0)
1800 {
1801 t->symtab_section = section_number++;
1802 t->strtab_section = section_number++;
1803 }
1804
1805 elf_elfheader (abfd)->e_shnum = section_number;
1806
1807 /* Set up the list of section header pointers, in agreement with the
1808 indices. */
1809 i_shdrp = ((Elf_Internal_Shdr **)
1810 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1811 if (i_shdrp == NULL)
1812 return false;
1813
1814 i_shdrp[0] = ((Elf_Internal_Shdr *)
1815 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1816 if (i_shdrp[0] == NULL)
1817 {
1818 bfd_release (abfd, i_shdrp);
1819 return false;
1820 }
1821 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1822
1823 elf_elfsections (abfd) = i_shdrp;
1824
1825 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1826 if (bfd_get_symcount (abfd) > 0)
1827 {
1828 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1829 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1830 t->symtab_hdr.sh_link = t->strtab_section;
1831 }
1832 for (sec = abfd->sections; sec; sec = sec->next)
1833 {
1834 struct bfd_elf_section_data *d = elf_section_data (sec);
1835 asection *s;
1836 const char *name;
1837
1838 i_shdrp[d->this_idx] = &d->this_hdr;
1839 if (d->rel_idx != 0)
1840 i_shdrp[d->rel_idx] = &d->rel_hdr;
1841 if (d->rel_idx2 != 0)
1842 i_shdrp[d->rel_idx2] = d->rel_hdr2;
1843
1844 /* Fill in the sh_link and sh_info fields while we're at it. */
1845
1846 /* sh_link of a reloc section is the section index of the symbol
1847 table. sh_info is the section index of the section to which
1848 the relocation entries apply. */
1849 if (d->rel_idx != 0)
1850 {
1851 d->rel_hdr.sh_link = t->symtab_section;
1852 d->rel_hdr.sh_info = d->this_idx;
1853 }
1854 if (d->rel_idx2 != 0)
1855 {
1856 d->rel_hdr2->sh_link = t->symtab_section;
1857 d->rel_hdr2->sh_info = d->this_idx;
1858 }
1859
1860 switch (d->this_hdr.sh_type)
1861 {
1862 case SHT_REL:
1863 case SHT_RELA:
1864 /* A reloc section which we are treating as a normal BFD
1865 section. sh_link is the section index of the symbol
1866 table. sh_info is the section index of the section to
1867 which the relocation entries apply. We assume that an
1868 allocated reloc section uses the dynamic symbol table.
1869 FIXME: How can we be sure? */
1870 s = bfd_get_section_by_name (abfd, ".dynsym");
1871 if (s != NULL)
1872 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1873
1874 /* We look up the section the relocs apply to by name. */
1875 name = sec->name;
1876 if (d->this_hdr.sh_type == SHT_REL)
1877 name += 4;
1878 else
1879 name += 5;
1880 s = bfd_get_section_by_name (abfd, name);
1881 if (s != NULL)
1882 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1883 break;
1884
1885 case SHT_STRTAB:
1886 /* We assume that a section named .stab*str is a stabs
1887 string section. We look for a section with the same name
1888 but without the trailing ``str'', and set its sh_link
1889 field to point to this section. */
1890 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1891 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1892 {
1893 size_t len;
1894 char *alc;
1895
1896 len = strlen (sec->name);
1897 alc = (char *) bfd_malloc (len - 2);
1898 if (alc == NULL)
1899 return false;
1900 strncpy (alc, sec->name, len - 3);
1901 alc[len - 3] = '\0';
1902 s = bfd_get_section_by_name (abfd, alc);
1903 free (alc);
1904 if (s != NULL)
1905 {
1906 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1907
1908 /* This is a .stab section. */
1909 elf_section_data (s)->this_hdr.sh_entsize =
1910 4 + 2 * bfd_get_arch_size (abfd) / 8;
1911 }
1912 }
1913 break;
1914
1915 case SHT_DYNAMIC:
1916 case SHT_DYNSYM:
1917 case SHT_GNU_verneed:
1918 case SHT_GNU_verdef:
1919 /* sh_link is the section header index of the string table
1920 used for the dynamic entries, or the symbol table, or the
1921 version strings. */
1922 s = bfd_get_section_by_name (abfd, ".dynstr");
1923 if (s != NULL)
1924 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1925 break;
1926
1927 case SHT_HASH:
1928 case SHT_GNU_versym:
1929 /* sh_link is the section header index of the symbol table
1930 this hash table or version table is for. */
1931 s = bfd_get_section_by_name (abfd, ".dynsym");
1932 if (s != NULL)
1933 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1934 break;
1935 }
1936 }
1937
1938 return true;
1939 }
1940
1941 /* Map symbol from it's internal number to the external number, moving
1942 all local symbols to be at the head of the list. */
1943
1944 static INLINE int
1945 sym_is_global (abfd, sym)
1946 bfd *abfd;
1947 asymbol *sym;
1948 {
1949 /* If the backend has a special mapping, use it. */
1950 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1951 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1952 (abfd, sym));
1953
1954 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1955 || bfd_is_und_section (bfd_get_section (sym))
1956 || bfd_is_com_section (bfd_get_section (sym)));
1957 }
1958
1959 static boolean
1960 elf_map_symbols (abfd)
1961 bfd *abfd;
1962 {
1963 int symcount = bfd_get_symcount (abfd);
1964 asymbol **syms = bfd_get_outsymbols (abfd);
1965 asymbol **sect_syms;
1966 int num_locals = 0;
1967 int num_globals = 0;
1968 int num_locals2 = 0;
1969 int num_globals2 = 0;
1970 int max_index = 0;
1971 int num_sections = 0;
1972 int idx;
1973 asection *asect;
1974 asymbol **new_syms;
1975 asymbol *sym;
1976
1977 #ifdef DEBUG
1978 fprintf (stderr, "elf_map_symbols\n");
1979 fflush (stderr);
1980 #endif
1981
1982 /* Add a section symbol for each BFD section. FIXME: Is this really
1983 necessary? */
1984 for (asect = abfd->sections; asect; asect = asect->next)
1985 {
1986 if (max_index < asect->index)
1987 max_index = asect->index;
1988 }
1989
1990 max_index++;
1991 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1992 if (sect_syms == NULL)
1993 return false;
1994 elf_section_syms (abfd) = sect_syms;
1995
1996 for (idx = 0; idx < symcount; idx++)
1997 {
1998 sym = syms[idx];
1999
2000 if ((sym->flags & BSF_SECTION_SYM) != 0
2001 && sym->value == 0)
2002 {
2003 asection *sec;
2004
2005 sec = sym->section;
2006
2007 if (sec->owner != NULL)
2008 {
2009 if (sec->owner != abfd)
2010 {
2011 if (sec->output_offset != 0)
2012 continue;
2013
2014 sec = sec->output_section;
2015
2016 /* Empty sections in the input files may have had a section
2017 symbol created for them. (See the comment near the end of
2018 _bfd_generic_link_output_symbols in linker.c). If the linker
2019 script discards such sections then we will reach this point.
2020 Since we know that we cannot avoid this case, we detect it
2021 and skip the abort and the assignment to the sect_syms array.
2022 To reproduce this particular case try running the linker
2023 testsuite test ld-scripts/weak.exp for an ELF port that uses
2024 the generic linker. */
2025 if (sec->owner == NULL)
2026 continue;
2027
2028 BFD_ASSERT (sec->owner == abfd);
2029 }
2030 sect_syms[sec->index] = syms[idx];
2031 }
2032 }
2033 }
2034
2035 for (asect = abfd->sections; asect; asect = asect->next)
2036 {
2037 if (sect_syms[asect->index] != NULL)
2038 continue;
2039
2040 sym = bfd_make_empty_symbol (abfd);
2041 if (sym == NULL)
2042 return false;
2043 sym->the_bfd = abfd;
2044 sym->name = asect->name;
2045 sym->value = 0;
2046 /* Set the flags to 0 to indicate that this one was newly added. */
2047 sym->flags = 0;
2048 sym->section = asect;
2049 sect_syms[asect->index] = sym;
2050 num_sections++;
2051 #ifdef DEBUG
2052 fprintf (stderr,
2053 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2054 asect->name, (long) asect->vma, asect->index, (long) asect);
2055 #endif
2056 }
2057
2058 /* Classify all of the symbols. */
2059 for (idx = 0; idx < symcount; idx++)
2060 {
2061 if (!sym_is_global (abfd, syms[idx]))
2062 num_locals++;
2063 else
2064 num_globals++;
2065 }
2066 for (asect = abfd->sections; asect; asect = asect->next)
2067 {
2068 if (sect_syms[asect->index] != NULL
2069 && sect_syms[asect->index]->flags == 0)
2070 {
2071 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
2072 if (!sym_is_global (abfd, sect_syms[asect->index]))
2073 num_locals++;
2074 else
2075 num_globals++;
2076 sect_syms[asect->index]->flags = 0;
2077 }
2078 }
2079
2080 /* Now sort the symbols so the local symbols are first. */
2081 new_syms = ((asymbol **)
2082 bfd_alloc (abfd,
2083 (num_locals + num_globals) * sizeof (asymbol *)));
2084 if (new_syms == NULL)
2085 return false;
2086
2087 for (idx = 0; idx < symcount; idx++)
2088 {
2089 asymbol *sym = syms[idx];
2090 int i;
2091
2092 if (!sym_is_global (abfd, sym))
2093 i = num_locals2++;
2094 else
2095 i = num_locals + num_globals2++;
2096 new_syms[i] = sym;
2097 sym->udata.i = i + 1;
2098 }
2099 for (asect = abfd->sections; asect; asect = asect->next)
2100 {
2101 if (sect_syms[asect->index] != NULL
2102 && sect_syms[asect->index]->flags == 0)
2103 {
2104 asymbol *sym = sect_syms[asect->index];
2105 int i;
2106
2107 sym->flags = BSF_SECTION_SYM;
2108 if (!sym_is_global (abfd, sym))
2109 i = num_locals2++;
2110 else
2111 i = num_locals + num_globals2++;
2112 new_syms[i] = sym;
2113 sym->udata.i = i + 1;
2114 }
2115 }
2116
2117 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2118
2119 elf_num_locals (abfd) = num_locals;
2120 elf_num_globals (abfd) = num_globals;
2121 return true;
2122 }
2123
2124 /* Align to the maximum file alignment that could be required for any
2125 ELF data structure. */
2126
2127 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2128 static INLINE file_ptr
2129 align_file_position (off, align)
2130 file_ptr off;
2131 int align;
2132 {
2133 return (off + align - 1) & ~(align - 1);
2134 }
2135
2136 /* Assign a file position to a section, optionally aligning to the
2137 required section alignment. */
2138
2139 INLINE file_ptr
2140 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2141 Elf_Internal_Shdr *i_shdrp;
2142 file_ptr offset;
2143 boolean align;
2144 {
2145 if (align)
2146 {
2147 unsigned int al;
2148
2149 al = i_shdrp->sh_addralign;
2150 if (al > 1)
2151 offset = BFD_ALIGN (offset, al);
2152 }
2153 i_shdrp->sh_offset = offset;
2154 if (i_shdrp->bfd_section != NULL)
2155 i_shdrp->bfd_section->filepos = offset;
2156 if (i_shdrp->sh_type != SHT_NOBITS)
2157 offset += i_shdrp->sh_size;
2158 return offset;
2159 }
2160
2161 /* Compute the file positions we are going to put the sections at, and
2162 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2163 is not NULL, this is being called by the ELF backend linker. */
2164
2165 boolean
2166 _bfd_elf_compute_section_file_positions (abfd, link_info)
2167 bfd *abfd;
2168 struct bfd_link_info *link_info;
2169 {
2170 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2171 boolean failed;
2172 struct bfd_strtab_hash *strtab;
2173 Elf_Internal_Shdr *shstrtab_hdr;
2174
2175 if (abfd->output_has_begun)
2176 return true;
2177
2178 /* Do any elf backend specific processing first. */
2179 if (bed->elf_backend_begin_write_processing)
2180 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2181
2182 if (! prep_headers (abfd))
2183 return false;
2184
2185 /* Post process the headers if necessary. */
2186 if (bed->elf_backend_post_process_headers)
2187 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2188
2189 failed = false;
2190 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2191 if (failed)
2192 return false;
2193
2194 if (!assign_section_numbers (abfd))
2195 return false;
2196
2197 /* The backend linker builds symbol table information itself. */
2198 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2199 {
2200 /* Non-zero if doing a relocatable link. */
2201 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2202
2203 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2204 return false;
2205 }
2206
2207 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2208 /* sh_name was set in prep_headers. */
2209 shstrtab_hdr->sh_type = SHT_STRTAB;
2210 shstrtab_hdr->sh_flags = 0;
2211 shstrtab_hdr->sh_addr = 0;
2212 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2213 shstrtab_hdr->sh_entsize = 0;
2214 shstrtab_hdr->sh_link = 0;
2215 shstrtab_hdr->sh_info = 0;
2216 /* sh_offset is set in assign_file_positions_except_relocs. */
2217 shstrtab_hdr->sh_addralign = 1;
2218
2219 if (!assign_file_positions_except_relocs (abfd))
2220 return false;
2221
2222 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2223 {
2224 file_ptr off;
2225 Elf_Internal_Shdr *hdr;
2226
2227 off = elf_tdata (abfd)->next_file_pos;
2228
2229 hdr = &elf_tdata (abfd)->symtab_hdr;
2230 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2231
2232 hdr = &elf_tdata (abfd)->strtab_hdr;
2233 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2234
2235 elf_tdata (abfd)->next_file_pos = off;
2236
2237 /* Now that we know where the .strtab section goes, write it
2238 out. */
2239 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2240 || ! _bfd_stringtab_emit (abfd, strtab))
2241 return false;
2242 _bfd_stringtab_free (strtab);
2243 }
2244
2245 abfd->output_has_begun = true;
2246
2247 return true;
2248 }
2249
2250 /* Create a mapping from a set of sections to a program segment. */
2251
2252 static INLINE struct elf_segment_map *
2253 make_mapping (abfd, sections, from, to, phdr)
2254 bfd *abfd;
2255 asection **sections;
2256 unsigned int from;
2257 unsigned int to;
2258 boolean phdr;
2259 {
2260 struct elf_segment_map *m;
2261 unsigned int i;
2262 asection **hdrpp;
2263
2264 m = ((struct elf_segment_map *)
2265 bfd_zalloc (abfd,
2266 (sizeof (struct elf_segment_map)
2267 + (to - from - 1) * sizeof (asection *))));
2268 if (m == NULL)
2269 return NULL;
2270 m->next = NULL;
2271 m->p_type = PT_LOAD;
2272 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2273 m->sections[i - from] = *hdrpp;
2274 m->count = to - from;
2275
2276 if (from == 0 && phdr)
2277 {
2278 /* Include the headers in the first PT_LOAD segment. */
2279 m->includes_filehdr = 1;
2280 m->includes_phdrs = 1;
2281 }
2282
2283 return m;
2284 }
2285
2286 /* Set up a mapping from BFD sections to program segments. */
2287
2288 static boolean
2289 map_sections_to_segments (abfd)
2290 bfd *abfd;
2291 {
2292 asection **sections = NULL;
2293 asection *s;
2294 unsigned int i;
2295 unsigned int count;
2296 struct elf_segment_map *mfirst;
2297 struct elf_segment_map **pm;
2298 struct elf_segment_map *m;
2299 asection *last_hdr;
2300 unsigned int phdr_index;
2301 bfd_vma maxpagesize;
2302 asection **hdrpp;
2303 boolean phdr_in_segment = true;
2304 boolean writable;
2305 asection *dynsec;
2306
2307 if (elf_tdata (abfd)->segment_map != NULL)
2308 return true;
2309
2310 if (bfd_count_sections (abfd) == 0)
2311 return true;
2312
2313 /* Select the allocated sections, and sort them. */
2314
2315 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2316 * sizeof (asection *));
2317 if (sections == NULL)
2318 goto error_return;
2319
2320 i = 0;
2321 for (s = abfd->sections; s != NULL; s = s->next)
2322 {
2323 if ((s->flags & SEC_ALLOC) != 0)
2324 {
2325 sections[i] = s;
2326 ++i;
2327 }
2328 }
2329 BFD_ASSERT (i <= bfd_count_sections (abfd));
2330 count = i;
2331
2332 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2333
2334 /* Build the mapping. */
2335
2336 mfirst = NULL;
2337 pm = &mfirst;
2338
2339 /* If we have a .interp section, then create a PT_PHDR segment for
2340 the program headers and a PT_INTERP segment for the .interp
2341 section. */
2342 s = bfd_get_section_by_name (abfd, ".interp");
2343 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2344 {
2345 m = ((struct elf_segment_map *)
2346 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2347 if (m == NULL)
2348 goto error_return;
2349 m->next = NULL;
2350 m->p_type = PT_PHDR;
2351 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2352 m->p_flags = PF_R | PF_X;
2353 m->p_flags_valid = 1;
2354 m->includes_phdrs = 1;
2355
2356 *pm = m;
2357 pm = &m->next;
2358
2359 m = ((struct elf_segment_map *)
2360 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2361 if (m == NULL)
2362 goto error_return;
2363 m->next = NULL;
2364 m->p_type = PT_INTERP;
2365 m->count = 1;
2366 m->sections[0] = s;
2367
2368 *pm = m;
2369 pm = &m->next;
2370 }
2371
2372 /* Look through the sections. We put sections in the same program
2373 segment when the start of the second section can be placed within
2374 a few bytes of the end of the first section. */
2375 last_hdr = NULL;
2376 phdr_index = 0;
2377 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2378 writable = false;
2379 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2380 if (dynsec != NULL
2381 && (dynsec->flags & SEC_LOAD) == 0)
2382 dynsec = NULL;
2383
2384 /* Deal with -Ttext or something similar such that the first section
2385 is not adjacent to the program headers. This is an
2386 approximation, since at this point we don't know exactly how many
2387 program headers we will need. */
2388 if (count > 0)
2389 {
2390 bfd_size_type phdr_size;
2391
2392 phdr_size = elf_tdata (abfd)->program_header_size;
2393 if (phdr_size == 0)
2394 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2395 if ((abfd->flags & D_PAGED) == 0
2396 || sections[0]->lma < phdr_size
2397 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2398 phdr_in_segment = false;
2399 }
2400
2401 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2402 {
2403 asection *hdr;
2404 boolean new_segment;
2405
2406 hdr = *hdrpp;
2407
2408 /* See if this section and the last one will fit in the same
2409 segment. */
2410
2411 if (last_hdr == NULL)
2412 {
2413 /* If we don't have a segment yet, then we don't need a new
2414 one (we build the last one after this loop). */
2415 new_segment = false;
2416 }
2417 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2418 {
2419 /* If this section has a different relation between the
2420 virtual address and the load address, then we need a new
2421 segment. */
2422 new_segment = true;
2423 }
2424 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2425 < BFD_ALIGN (hdr->lma, maxpagesize))
2426 {
2427 /* If putting this section in this segment would force us to
2428 skip a page in the segment, then we need a new segment. */
2429 new_segment = true;
2430 }
2431 else if ((last_hdr->flags & SEC_LOAD) == 0
2432 && (hdr->flags & SEC_LOAD) != 0)
2433 {
2434 /* We don't want to put a loadable section after a
2435 nonloadable section in the same segment. */
2436 new_segment = true;
2437 }
2438 else if ((abfd->flags & D_PAGED) == 0)
2439 {
2440 /* If the file is not demand paged, which means that we
2441 don't require the sections to be correctly aligned in the
2442 file, then there is no other reason for a new segment. */
2443 new_segment = false;
2444 }
2445 else if (! writable
2446 && (hdr->flags & SEC_READONLY) == 0
2447 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2448 == hdr->lma))
2449 {
2450 /* We don't want to put a writable section in a read only
2451 segment, unless they are on the same page in memory
2452 anyhow. We already know that the last section does not
2453 bring us past the current section on the page, so the
2454 only case in which the new section is not on the same
2455 page as the previous section is when the previous section
2456 ends precisely on a page boundary. */
2457 new_segment = true;
2458 }
2459 else
2460 {
2461 /* Otherwise, we can use the same segment. */
2462 new_segment = false;
2463 }
2464
2465 if (! new_segment)
2466 {
2467 if ((hdr->flags & SEC_READONLY) == 0)
2468 writable = true;
2469 last_hdr = hdr;
2470 continue;
2471 }
2472
2473 /* We need a new program segment. We must create a new program
2474 header holding all the sections from phdr_index until hdr. */
2475
2476 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2477 if (m == NULL)
2478 goto error_return;
2479
2480 *pm = m;
2481 pm = &m->next;
2482
2483 if ((hdr->flags & SEC_READONLY) == 0)
2484 writable = true;
2485 else
2486 writable = false;
2487
2488 last_hdr = hdr;
2489 phdr_index = i;
2490 phdr_in_segment = false;
2491 }
2492
2493 /* Create a final PT_LOAD program segment. */
2494 if (last_hdr != NULL)
2495 {
2496 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2497 if (m == NULL)
2498 goto error_return;
2499
2500 *pm = m;
2501 pm = &m->next;
2502 }
2503
2504 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2505 if (dynsec != NULL)
2506 {
2507 m = ((struct elf_segment_map *)
2508 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2509 if (m == NULL)
2510 goto error_return;
2511 m->next = NULL;
2512 m->p_type = PT_DYNAMIC;
2513 m->count = 1;
2514 m->sections[0] = dynsec;
2515
2516 *pm = m;
2517 pm = &m->next;
2518 }
2519
2520 /* For each loadable .note section, add a PT_NOTE segment. We don't
2521 use bfd_get_section_by_name, because if we link together
2522 nonloadable .note sections and loadable .note sections, we will
2523 generate two .note sections in the output file. FIXME: Using
2524 names for section types is bogus anyhow. */
2525 for (s = abfd->sections; s != NULL; s = s->next)
2526 {
2527 if ((s->flags & SEC_LOAD) != 0
2528 && strncmp (s->name, ".note", 5) == 0)
2529 {
2530 m = ((struct elf_segment_map *)
2531 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2532 if (m == NULL)
2533 goto error_return;
2534 m->next = NULL;
2535 m->p_type = PT_NOTE;
2536 m->count = 1;
2537 m->sections[0] = s;
2538
2539 *pm = m;
2540 pm = &m->next;
2541 }
2542 }
2543
2544 free (sections);
2545 sections = NULL;
2546
2547 elf_tdata (abfd)->segment_map = mfirst;
2548 return true;
2549
2550 error_return:
2551 if (sections != NULL)
2552 free (sections);
2553 return false;
2554 }
2555
2556 /* Sort sections by address. */
2557
2558 static int
2559 elf_sort_sections (arg1, arg2)
2560 const PTR arg1;
2561 const PTR arg2;
2562 {
2563 const asection *sec1 = *(const asection **) arg1;
2564 const asection *sec2 = *(const asection **) arg2;
2565
2566 /* Sort by LMA first, since this is the address used to
2567 place the section into a segment. */
2568 if (sec1->lma < sec2->lma)
2569 return -1;
2570 else if (sec1->lma > sec2->lma)
2571 return 1;
2572
2573 /* Then sort by VMA. Normally the LMA and the VMA will be
2574 the same, and this will do nothing. */
2575 if (sec1->vma < sec2->vma)
2576 return -1;
2577 else if (sec1->vma > sec2->vma)
2578 return 1;
2579
2580 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2581
2582 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2583
2584 if (TOEND (sec1))
2585 {
2586 if (TOEND (sec2))
2587 return sec1->target_index - sec2->target_index;
2588 else
2589 return 1;
2590 }
2591
2592 if (TOEND (sec2))
2593 return -1;
2594
2595 #undef TOEND
2596
2597 /* Sort by size, to put zero sized sections before others at the
2598 same address. */
2599
2600 if (sec1->_raw_size < sec2->_raw_size)
2601 return -1;
2602 if (sec1->_raw_size > sec2->_raw_size)
2603 return 1;
2604
2605 return sec1->target_index - sec2->target_index;
2606 }
2607
2608 /* Assign file positions to the sections based on the mapping from
2609 sections to segments. This function also sets up some fields in
2610 the file header, and writes out the program headers. */
2611
2612 static boolean
2613 assign_file_positions_for_segments (abfd)
2614 bfd *abfd;
2615 {
2616 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2617 unsigned int count;
2618 struct elf_segment_map *m;
2619 unsigned int alloc;
2620 Elf_Internal_Phdr *phdrs;
2621 file_ptr off, voff;
2622 bfd_vma filehdr_vaddr, filehdr_paddr;
2623 bfd_vma phdrs_vaddr, phdrs_paddr;
2624 Elf_Internal_Phdr *p;
2625
2626 if (elf_tdata (abfd)->segment_map == NULL)
2627 {
2628 if (! map_sections_to_segments (abfd))
2629 return false;
2630 }
2631
2632 if (bed->elf_backend_modify_segment_map)
2633 {
2634 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2635 return false;
2636 }
2637
2638 count = 0;
2639 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2640 ++count;
2641
2642 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2643 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2644 elf_elfheader (abfd)->e_phnum = count;
2645
2646 if (count == 0)
2647 return true;
2648
2649 /* If we already counted the number of program segments, make sure
2650 that we allocated enough space. This happens when SIZEOF_HEADERS
2651 is used in a linker script. */
2652 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2653 if (alloc != 0 && count > alloc)
2654 {
2655 ((*_bfd_error_handler)
2656 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2657 bfd_get_filename (abfd), alloc, count));
2658 bfd_set_error (bfd_error_bad_value);
2659 return false;
2660 }
2661
2662 if (alloc == 0)
2663 alloc = count;
2664
2665 phdrs = ((Elf_Internal_Phdr *)
2666 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2667 if (phdrs == NULL)
2668 return false;
2669
2670 off = bed->s->sizeof_ehdr;
2671 off += alloc * bed->s->sizeof_phdr;
2672
2673 filehdr_vaddr = 0;
2674 filehdr_paddr = 0;
2675 phdrs_vaddr = 0;
2676 phdrs_paddr = 0;
2677
2678 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2679 m != NULL;
2680 m = m->next, p++)
2681 {
2682 unsigned int i;
2683 asection **secpp;
2684
2685 /* If elf_segment_map is not from map_sections_to_segments, the
2686 sections may not be correctly ordered. */
2687 if (m->count > 0)
2688 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2689 elf_sort_sections);
2690
2691 p->p_type = m->p_type;
2692 p->p_flags = m->p_flags;
2693
2694 if (p->p_type == PT_LOAD
2695 && m->count > 0
2696 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2697 {
2698 if ((abfd->flags & D_PAGED) != 0)
2699 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2700 else
2701 {
2702 bfd_size_type align;
2703
2704 align = 0;
2705 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2706 {
2707 bfd_size_type secalign;
2708
2709 secalign = bfd_get_section_alignment (abfd, *secpp);
2710 if (secalign > align)
2711 align = secalign;
2712 }
2713
2714 off += (m->sections[0]->vma - off) % (1 << align);
2715 }
2716 }
2717
2718 if (m->count == 0)
2719 p->p_vaddr = 0;
2720 else
2721 p->p_vaddr = m->sections[0]->vma;
2722
2723 if (m->p_paddr_valid)
2724 p->p_paddr = m->p_paddr;
2725 else if (m->count == 0)
2726 p->p_paddr = 0;
2727 else
2728 p->p_paddr = m->sections[0]->lma;
2729
2730 if (p->p_type == PT_LOAD
2731 && (abfd->flags & D_PAGED) != 0)
2732 p->p_align = bed->maxpagesize;
2733 else if (m->count == 0)
2734 p->p_align = bed->s->file_align;
2735 else
2736 p->p_align = 0;
2737
2738 p->p_offset = 0;
2739 p->p_filesz = 0;
2740 p->p_memsz = 0;
2741
2742 if (m->includes_filehdr)
2743 {
2744 if (! m->p_flags_valid)
2745 p->p_flags |= PF_R;
2746 p->p_offset = 0;
2747 p->p_filesz = bed->s->sizeof_ehdr;
2748 p->p_memsz = bed->s->sizeof_ehdr;
2749 if (m->count > 0)
2750 {
2751 BFD_ASSERT (p->p_type == PT_LOAD);
2752
2753 if (p->p_vaddr < (bfd_vma) off)
2754 {
2755 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2756 bfd_get_filename (abfd));
2757 bfd_set_error (bfd_error_bad_value);
2758 return false;
2759 }
2760
2761 p->p_vaddr -= off;
2762 if (! m->p_paddr_valid)
2763 p->p_paddr -= off;
2764 }
2765 if (p->p_type == PT_LOAD)
2766 {
2767 filehdr_vaddr = p->p_vaddr;
2768 filehdr_paddr = p->p_paddr;
2769 }
2770 }
2771
2772 if (m->includes_phdrs)
2773 {
2774 if (! m->p_flags_valid)
2775 p->p_flags |= PF_R;
2776
2777 if (m->includes_filehdr)
2778 {
2779 if (p->p_type == PT_LOAD)
2780 {
2781 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2782 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2783 }
2784 }
2785 else
2786 {
2787 p->p_offset = bed->s->sizeof_ehdr;
2788
2789 if (m->count > 0)
2790 {
2791 BFD_ASSERT (p->p_type == PT_LOAD);
2792 p->p_vaddr -= off - p->p_offset;
2793 if (! m->p_paddr_valid)
2794 p->p_paddr -= off - p->p_offset;
2795 }
2796
2797 if (p->p_type == PT_LOAD)
2798 {
2799 phdrs_vaddr = p->p_vaddr;
2800 phdrs_paddr = p->p_paddr;
2801 }
2802 else
2803 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2804 }
2805
2806 p->p_filesz += alloc * bed->s->sizeof_phdr;
2807 p->p_memsz += alloc * bed->s->sizeof_phdr;
2808 }
2809
2810 if (p->p_type == PT_LOAD
2811 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2812 {
2813 if (! m->includes_filehdr && ! m->includes_phdrs)
2814 p->p_offset = off;
2815 else
2816 {
2817 file_ptr adjust;
2818
2819 adjust = off - (p->p_offset + p->p_filesz);
2820 p->p_filesz += adjust;
2821 p->p_memsz += adjust;
2822 }
2823 }
2824
2825 voff = off;
2826
2827 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2828 {
2829 asection *sec;
2830 flagword flags;
2831 bfd_size_type align;
2832
2833 sec = *secpp;
2834 flags = sec->flags;
2835 align = 1 << bfd_get_section_alignment (abfd, sec);
2836
2837 /* The section may have artificial alignment forced by a
2838 link script. Notice this case by the gap between the
2839 cumulative phdr vma and the section's vma. */
2840 if (p->p_vaddr + p->p_memsz < sec->vma)
2841 {
2842 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2843
2844 p->p_memsz += adjust;
2845 off += adjust;
2846 voff += adjust;
2847 if ((flags & SEC_LOAD) != 0)
2848 p->p_filesz += adjust;
2849 }
2850
2851 if (p->p_type == PT_LOAD)
2852 {
2853 bfd_signed_vma adjust;
2854
2855 if ((flags & SEC_LOAD) != 0)
2856 {
2857 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2858 if (adjust < 0)
2859 adjust = 0;
2860 }
2861 else if ((flags & SEC_ALLOC) != 0)
2862 {
2863 /* The section VMA must equal the file position
2864 modulo the page size. FIXME: I'm not sure if
2865 this adjustment is really necessary. We used to
2866 not have the SEC_LOAD case just above, and then
2867 this was necessary, but now I'm not sure. */
2868 if ((abfd->flags & D_PAGED) != 0)
2869 adjust = (sec->vma - voff) % bed->maxpagesize;
2870 else
2871 adjust = (sec->vma - voff) % align;
2872 }
2873 else
2874 adjust = 0;
2875
2876 if (adjust != 0)
2877 {
2878 if (i == 0)
2879 {
2880 (* _bfd_error_handler)
2881 (_("Error: First section in segment (%s) starts at 0x%x"),
2882 bfd_section_name (abfd, sec), sec->lma);
2883 (* _bfd_error_handler)
2884 (_(" whereas segment starts at 0x%x"),
2885 p->p_paddr);
2886
2887 return false;
2888 }
2889 p->p_memsz += adjust;
2890 off += adjust;
2891 voff += adjust;
2892 if ((flags & SEC_LOAD) != 0)
2893 p->p_filesz += adjust;
2894 }
2895
2896 sec->filepos = off;
2897
2898 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2899 used in a linker script we may have a section with
2900 SEC_LOAD clear but which is supposed to have
2901 contents. */
2902 if ((flags & SEC_LOAD) != 0
2903 || (flags & SEC_HAS_CONTENTS) != 0)
2904 off += sec->_raw_size;
2905
2906 if ((flags & SEC_ALLOC) != 0)
2907 voff += sec->_raw_size;
2908 }
2909
2910 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
2911 {
2912 /* The actual "note" segment has i == 0.
2913 This is the one that actually contains everything. */
2914 if (i == 0)
2915 {
2916 sec->filepos = off;
2917 p->p_filesz = sec->_raw_size;
2918 off += sec->_raw_size;
2919 voff = off;
2920 }
2921 else
2922 {
2923 /* Fake sections -- don't need to be written. */
2924 sec->filepos = 0;
2925 sec->_raw_size = 0;
2926 flags = sec->flags = 0;
2927 }
2928 p->p_memsz = 0;
2929 p->p_align = 1;
2930 }
2931 else
2932 {
2933 p->p_memsz += sec->_raw_size;
2934
2935 if ((flags & SEC_LOAD) != 0)
2936 p->p_filesz += sec->_raw_size;
2937
2938 if (align > p->p_align
2939 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
2940 p->p_align = align;
2941 }
2942
2943 if (! m->p_flags_valid)
2944 {
2945 p->p_flags |= PF_R;
2946 if ((flags & SEC_CODE) != 0)
2947 p->p_flags |= PF_X;
2948 if ((flags & SEC_READONLY) == 0)
2949 p->p_flags |= PF_W;
2950 }
2951 }
2952 }
2953
2954 /* Now that we have set the section file positions, we can set up
2955 the file positions for the non PT_LOAD segments. */
2956 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2957 m != NULL;
2958 m = m->next, p++)
2959 {
2960 if (p->p_type != PT_LOAD && m->count > 0)
2961 {
2962 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2963 p->p_offset = m->sections[0]->filepos;
2964 }
2965 if (m->count == 0)
2966 {
2967 if (m->includes_filehdr)
2968 {
2969 p->p_vaddr = filehdr_vaddr;
2970 if (! m->p_paddr_valid)
2971 p->p_paddr = filehdr_paddr;
2972 }
2973 else if (m->includes_phdrs)
2974 {
2975 p->p_vaddr = phdrs_vaddr;
2976 if (! m->p_paddr_valid)
2977 p->p_paddr = phdrs_paddr;
2978 }
2979 }
2980 }
2981
2982 /* Clear out any program headers we allocated but did not use. */
2983 for (; count < alloc; count++, p++)
2984 {
2985 memset (p, 0, sizeof *p);
2986 p->p_type = PT_NULL;
2987 }
2988
2989 elf_tdata (abfd)->phdr = phdrs;
2990
2991 elf_tdata (abfd)->next_file_pos = off;
2992
2993 /* Write out the program headers. */
2994 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2995 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2996 return false;
2997
2998 return true;
2999 }
3000
3001 /* Get the size of the program header.
3002
3003 If this is called by the linker before any of the section VMA's are set, it
3004 can't calculate the correct value for a strange memory layout. This only
3005 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3006 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3007 data segment (exclusive of .interp and .dynamic).
3008
3009 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3010 will be two segments. */
3011
3012 static bfd_size_type
3013 get_program_header_size (abfd)
3014 bfd *abfd;
3015 {
3016 size_t segs;
3017 asection *s;
3018 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3019
3020 /* We can't return a different result each time we're called. */
3021 if (elf_tdata (abfd)->program_header_size != 0)
3022 return elf_tdata (abfd)->program_header_size;
3023
3024 if (elf_tdata (abfd)->segment_map != NULL)
3025 {
3026 struct elf_segment_map *m;
3027
3028 segs = 0;
3029 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3030 ++segs;
3031 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3032 return elf_tdata (abfd)->program_header_size;
3033 }
3034
3035 /* Assume we will need exactly two PT_LOAD segments: one for text
3036 and one for data. */
3037 segs = 2;
3038
3039 s = bfd_get_section_by_name (abfd, ".interp");
3040 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3041 {
3042 /* If we have a loadable interpreter section, we need a
3043 PT_INTERP segment. In this case, assume we also need a
3044 PT_PHDR segment, although that may not be true for all
3045 targets. */
3046 segs += 2;
3047 }
3048
3049 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3050 {
3051 /* We need a PT_DYNAMIC segment. */
3052 ++segs;
3053 }
3054
3055 for (s = abfd->sections; s != NULL; s = s->next)
3056 {
3057 if ((s->flags & SEC_LOAD) != 0
3058 && strncmp (s->name, ".note", 5) == 0)
3059 {
3060 /* We need a PT_NOTE segment. */
3061 ++segs;
3062 }
3063 }
3064
3065 /* Let the backend count up any program headers it might need. */
3066 if (bed->elf_backend_additional_program_headers)
3067 {
3068 int a;
3069
3070 a = (*bed->elf_backend_additional_program_headers) (abfd);
3071 if (a == -1)
3072 abort ();
3073 segs += a;
3074 }
3075
3076 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3077 return elf_tdata (abfd)->program_header_size;
3078 }
3079
3080 /* Work out the file positions of all the sections. This is called by
3081 _bfd_elf_compute_section_file_positions. All the section sizes and
3082 VMAs must be known before this is called.
3083
3084 We do not consider reloc sections at this point, unless they form
3085 part of the loadable image. Reloc sections are assigned file
3086 positions in assign_file_positions_for_relocs, which is called by
3087 write_object_contents and final_link.
3088
3089 We also don't set the positions of the .symtab and .strtab here. */
3090
3091 static boolean
3092 assign_file_positions_except_relocs (abfd)
3093 bfd *abfd;
3094 {
3095 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3096 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3097 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3098 file_ptr off;
3099 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3100
3101 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3102 && bfd_get_format (abfd) != bfd_core)
3103 {
3104 Elf_Internal_Shdr **hdrpp;
3105 unsigned int i;
3106
3107 /* Start after the ELF header. */
3108 off = i_ehdrp->e_ehsize;
3109
3110 /* We are not creating an executable, which means that we are
3111 not creating a program header, and that the actual order of
3112 the sections in the file is unimportant. */
3113 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3114 {
3115 Elf_Internal_Shdr *hdr;
3116
3117 hdr = *hdrpp;
3118 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3119 {
3120 hdr->sh_offset = -1;
3121 continue;
3122 }
3123 if (i == tdata->symtab_section
3124 || i == tdata->strtab_section)
3125 {
3126 hdr->sh_offset = -1;
3127 continue;
3128 }
3129
3130 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3131 }
3132 }
3133 else
3134 {
3135 unsigned int i;
3136 Elf_Internal_Shdr **hdrpp;
3137
3138 /* Assign file positions for the loaded sections based on the
3139 assignment of sections to segments. */
3140 if (! assign_file_positions_for_segments (abfd))
3141 return false;
3142
3143 /* Assign file positions for the other sections. */
3144
3145 off = elf_tdata (abfd)->next_file_pos;
3146 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3147 {
3148 Elf_Internal_Shdr *hdr;
3149
3150 hdr = *hdrpp;
3151 if (hdr->bfd_section != NULL
3152 && hdr->bfd_section->filepos != 0)
3153 hdr->sh_offset = hdr->bfd_section->filepos;
3154 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3155 {
3156 ((*_bfd_error_handler)
3157 (_("%s: warning: allocated section `%s' not in segment"),
3158 bfd_get_filename (abfd),
3159 (hdr->bfd_section == NULL
3160 ? "*unknown*"
3161 : hdr->bfd_section->name)));
3162 if ((abfd->flags & D_PAGED) != 0)
3163 off += (hdr->sh_addr - off) % bed->maxpagesize;
3164 else
3165 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3166 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3167 false);
3168 }
3169 else if (hdr->sh_type == SHT_REL
3170 || hdr->sh_type == SHT_RELA
3171 || hdr == i_shdrpp[tdata->symtab_section]
3172 || hdr == i_shdrpp[tdata->strtab_section])
3173 hdr->sh_offset = -1;
3174 else
3175 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3176 }
3177 }
3178
3179 /* Place the section headers. */
3180 off = align_file_position (off, bed->s->file_align);
3181 i_ehdrp->e_shoff = off;
3182 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3183
3184 elf_tdata (abfd)->next_file_pos = off;
3185
3186 return true;
3187 }
3188
3189 static boolean
3190 prep_headers (abfd)
3191 bfd *abfd;
3192 {
3193 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3194 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3195 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3196 int count;
3197 struct bfd_strtab_hash *shstrtab;
3198 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3199
3200 i_ehdrp = elf_elfheader (abfd);
3201 i_shdrp = elf_elfsections (abfd);
3202
3203 shstrtab = _bfd_elf_stringtab_init ();
3204 if (shstrtab == NULL)
3205 return false;
3206
3207 elf_shstrtab (abfd) = shstrtab;
3208
3209 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3210 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3211 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3212 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3213
3214 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3215 i_ehdrp->e_ident[EI_DATA] =
3216 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3217 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3218
3219 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3220 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3221
3222 for (count = EI_PAD; count < EI_NIDENT; count++)
3223 i_ehdrp->e_ident[count] = 0;
3224
3225 if ((abfd->flags & DYNAMIC) != 0)
3226 i_ehdrp->e_type = ET_DYN;
3227 else if ((abfd->flags & EXEC_P) != 0)
3228 i_ehdrp->e_type = ET_EXEC;
3229 else if (bfd_get_format (abfd) == bfd_core)
3230 i_ehdrp->e_type = ET_CORE;
3231 else
3232 i_ehdrp->e_type = ET_REL;
3233
3234 switch (bfd_get_arch (abfd))
3235 {
3236 case bfd_arch_unknown:
3237 i_ehdrp->e_machine = EM_NONE;
3238 break;
3239 case bfd_arch_sparc:
3240 if (bfd_get_arch_size (abfd) == 64)
3241 i_ehdrp->e_machine = EM_SPARCV9;
3242 else
3243 i_ehdrp->e_machine = EM_SPARC;
3244 break;
3245 case bfd_arch_i370:
3246 i_ehdrp->e_machine = EM_S370;
3247 break;
3248 case bfd_arch_i386:
3249 i_ehdrp->e_machine = EM_386;
3250 break;
3251 case bfd_arch_ia64:
3252 i_ehdrp->e_machine = EM_IA_64;
3253 break;
3254 case bfd_arch_m68hc11:
3255 i_ehdrp->e_machine = EM_68HC11;
3256 break;
3257 case bfd_arch_m68hc12:
3258 i_ehdrp->e_machine = EM_68HC12;
3259 break;
3260 case bfd_arch_m68k:
3261 i_ehdrp->e_machine = EM_68K;
3262 break;
3263 case bfd_arch_m88k:
3264 i_ehdrp->e_machine = EM_88K;
3265 break;
3266 case bfd_arch_i860:
3267 i_ehdrp->e_machine = EM_860;
3268 break;
3269 case bfd_arch_i960:
3270 i_ehdrp->e_machine = EM_960;
3271 break;
3272 case bfd_arch_mips: /* MIPS Rxxxx */
3273 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3274 break;
3275 case bfd_arch_hppa:
3276 i_ehdrp->e_machine = EM_PARISC;
3277 break;
3278 case bfd_arch_powerpc:
3279 i_ehdrp->e_machine = EM_PPC;
3280 break;
3281 case bfd_arch_alpha:
3282 i_ehdrp->e_machine = EM_ALPHA;
3283 break;
3284 case bfd_arch_sh:
3285 i_ehdrp->e_machine = EM_SH;
3286 break;
3287 case bfd_arch_d10v:
3288 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3289 break;
3290 case bfd_arch_d30v:
3291 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3292 break;
3293 case bfd_arch_fr30:
3294 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3295 break;
3296 case bfd_arch_mcore:
3297 i_ehdrp->e_machine = EM_MCORE;
3298 break;
3299 case bfd_arch_avr:
3300 i_ehdrp->e_machine = EM_AVR;
3301 break;
3302 case bfd_arch_v850:
3303 switch (bfd_get_mach (abfd))
3304 {
3305 default:
3306 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3307 }
3308 break;
3309 case bfd_arch_arc:
3310 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3311 break;
3312 case bfd_arch_arm:
3313 i_ehdrp->e_machine = EM_ARM;
3314 break;
3315 case bfd_arch_m32r:
3316 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3317 break;
3318 case bfd_arch_mn10200:
3319 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3320 break;
3321 case bfd_arch_mn10300:
3322 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3323 break;
3324 case bfd_arch_pj:
3325 i_ehdrp->e_machine = EM_PJ;
3326 break;
3327 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3328 default:
3329 i_ehdrp->e_machine = EM_NONE;
3330 }
3331 i_ehdrp->e_version = bed->s->ev_current;
3332 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3333
3334 /* no program header, for now. */
3335 i_ehdrp->e_phoff = 0;
3336 i_ehdrp->e_phentsize = 0;
3337 i_ehdrp->e_phnum = 0;
3338
3339 /* each bfd section is section header entry */
3340 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3341 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3342
3343 /* if we're building an executable, we'll need a program header table */
3344 if (abfd->flags & EXEC_P)
3345 {
3346 /* it all happens later */
3347 #if 0
3348 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3349
3350 /* elf_build_phdrs() returns a (NULL-terminated) array of
3351 Elf_Internal_Phdrs */
3352 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3353 i_ehdrp->e_phoff = outbase;
3354 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3355 #endif
3356 }
3357 else
3358 {
3359 i_ehdrp->e_phentsize = 0;
3360 i_phdrp = 0;
3361 i_ehdrp->e_phoff = 0;
3362 }
3363
3364 elf_tdata (abfd)->symtab_hdr.sh_name =
3365 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3366 elf_tdata (abfd)->strtab_hdr.sh_name =
3367 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3368 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3369 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3370 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3371 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3372 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3373 return false;
3374
3375 return true;
3376 }
3377
3378 /* Assign file positions for all the reloc sections which are not part
3379 of the loadable file image. */
3380
3381 void
3382 _bfd_elf_assign_file_positions_for_relocs (abfd)
3383 bfd *abfd;
3384 {
3385 file_ptr off;
3386 unsigned int i;
3387 Elf_Internal_Shdr **shdrpp;
3388
3389 off = elf_tdata (abfd)->next_file_pos;
3390
3391 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3392 i < elf_elfheader (abfd)->e_shnum;
3393 i++, shdrpp++)
3394 {
3395 Elf_Internal_Shdr *shdrp;
3396
3397 shdrp = *shdrpp;
3398 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3399 && shdrp->sh_offset == -1)
3400 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3401 }
3402
3403 elf_tdata (abfd)->next_file_pos = off;
3404 }
3405
3406 boolean
3407 _bfd_elf_write_object_contents (abfd)
3408 bfd *abfd;
3409 {
3410 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3411 Elf_Internal_Ehdr *i_ehdrp;
3412 Elf_Internal_Shdr **i_shdrp;
3413 boolean failed;
3414 unsigned int count;
3415
3416 if (! abfd->output_has_begun
3417 && ! _bfd_elf_compute_section_file_positions
3418 (abfd, (struct bfd_link_info *) NULL))
3419 return false;
3420
3421 i_shdrp = elf_elfsections (abfd);
3422 i_ehdrp = elf_elfheader (abfd);
3423
3424 failed = false;
3425 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3426 if (failed)
3427 return false;
3428
3429 _bfd_elf_assign_file_positions_for_relocs (abfd);
3430
3431 /* After writing the headers, we need to write the sections too... */
3432 for (count = 1; count < i_ehdrp->e_shnum; count++)
3433 {
3434 if (bed->elf_backend_section_processing)
3435 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3436 if (i_shdrp[count]->contents)
3437 {
3438 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3439 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3440 1, abfd)
3441 != i_shdrp[count]->sh_size))
3442 return false;
3443 }
3444 }
3445
3446 /* Write out the section header names. */
3447 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3448 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3449 return false;
3450
3451 if (bed->elf_backend_final_write_processing)
3452 (*bed->elf_backend_final_write_processing) (abfd,
3453 elf_tdata (abfd)->linker);
3454
3455 return bed->s->write_shdrs_and_ehdr (abfd);
3456 }
3457
3458 boolean
3459 _bfd_elf_write_corefile_contents (abfd)
3460 bfd *abfd;
3461 {
3462 /* Hopefully this can be done just like an object file. */
3463 return _bfd_elf_write_object_contents (abfd);
3464 }
3465 /* given a section, search the header to find them... */
3466 int
3467 _bfd_elf_section_from_bfd_section (abfd, asect)
3468 bfd *abfd;
3469 struct sec *asect;
3470 {
3471 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3472 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3473 int index;
3474 Elf_Internal_Shdr *hdr;
3475 int maxindex = elf_elfheader (abfd)->e_shnum;
3476
3477 for (index = 0; index < maxindex; index++)
3478 {
3479 hdr = i_shdrp[index];
3480 if (hdr->bfd_section == asect)
3481 return index;
3482 }
3483
3484 if (bed->elf_backend_section_from_bfd_section)
3485 {
3486 for (index = 0; index < maxindex; index++)
3487 {
3488 int retval;
3489
3490 hdr = i_shdrp[index];
3491 retval = index;
3492 if ((*bed->elf_backend_section_from_bfd_section)
3493 (abfd, hdr, asect, &retval))
3494 return retval;
3495 }
3496 }
3497
3498 if (bfd_is_abs_section (asect))
3499 return SHN_ABS;
3500 if (bfd_is_com_section (asect))
3501 return SHN_COMMON;
3502 if (bfd_is_und_section (asect))
3503 return SHN_UNDEF;
3504
3505 bfd_set_error (bfd_error_nonrepresentable_section);
3506
3507 return -1;
3508 }
3509
3510 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3511 on error. */
3512
3513 int
3514 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3515 bfd *abfd;
3516 asymbol **asym_ptr_ptr;
3517 {
3518 asymbol *asym_ptr = *asym_ptr_ptr;
3519 int idx;
3520 flagword flags = asym_ptr->flags;
3521
3522 /* When gas creates relocations against local labels, it creates its
3523 own symbol for the section, but does put the symbol into the
3524 symbol chain, so udata is 0. When the linker is generating
3525 relocatable output, this section symbol may be for one of the
3526 input sections rather than the output section. */
3527 if (asym_ptr->udata.i == 0
3528 && (flags & BSF_SECTION_SYM)
3529 && asym_ptr->section)
3530 {
3531 int indx;
3532
3533 if (asym_ptr->section->output_section != NULL)
3534 indx = asym_ptr->section->output_section->index;
3535 else
3536 indx = asym_ptr->section->index;
3537 if (elf_section_syms (abfd)[indx])
3538 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3539 }
3540
3541 idx = asym_ptr->udata.i;
3542
3543 if (idx == 0)
3544 {
3545 /* This case can occur when using --strip-symbol on a symbol
3546 which is used in a relocation entry. */
3547 (*_bfd_error_handler)
3548 (_("%s: symbol `%s' required but not present"),
3549 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3550 bfd_set_error (bfd_error_no_symbols);
3551 return -1;
3552 }
3553
3554 #if DEBUG & 4
3555 {
3556 fprintf (stderr,
3557 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3558 (long) asym_ptr, asym_ptr->name, idx, flags,
3559 elf_symbol_flags (flags));
3560 fflush (stderr);
3561 }
3562 #endif
3563
3564 return idx;
3565 }
3566
3567 /* Copy private BFD data. This copies any program header information. */
3568
3569 static boolean
3570 copy_private_bfd_data (ibfd, obfd)
3571 bfd *ibfd;
3572 bfd *obfd;
3573 {
3574 Elf_Internal_Ehdr *iehdr;
3575 struct elf_segment_map *mfirst;
3576 struct elf_segment_map **pm;
3577 struct elf_segment_map *m;
3578 Elf_Internal_Phdr *p;
3579 unsigned int i;
3580 unsigned int num_segments;
3581 boolean phdr_included = false;
3582
3583 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3584 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3585 return true;
3586
3587 if (elf_tdata (ibfd)->phdr == NULL)
3588 return true;
3589
3590 iehdr = elf_elfheader (ibfd);
3591
3592 mfirst = NULL;
3593 pm = &mfirst;
3594
3595 num_segments = elf_elfheader (ibfd)->e_phnum;
3596
3597 #define IS_CONTAINED_BY(addr, len, bottom, phdr) \
3598 ((addr) >= (bottom) \
3599 && ( ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz) \
3600 || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz)))
3601
3602 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3603
3604 #define IS_COREFILE_NOTE(p, s) \
3605 (p->p_type == PT_NOTE \
3606 && bfd_get_format (ibfd) == bfd_core \
3607 && s->vma == 0 && s->lma == 0 \
3608 && (bfd_vma) s->filepos >= p->p_offset \
3609 && (bfd_vma) s->filepos + s->_raw_size \
3610 <= p->p_offset + p->p_filesz)
3611
3612 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3613 linker, which generates a PT_INTERP section with p_vaddr and
3614 p_memsz set to 0. */
3615
3616 #define IS_SOLARIS_PT_INTERP(p, s) \
3617 (p->p_vaddr == 0 \
3618 && p->p_filesz > 0 \
3619 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3620 && s->_raw_size > 0 \
3621 && (bfd_vma) s->filepos >= p->p_offset \
3622 && ((bfd_vma) s->filepos + s->_raw_size \
3623 <= p->p_offset + p->p_filesz))
3624
3625 /* Scan through the segments specified in the program header
3626 of the input BFD. */
3627 for (i = 0, p = elf_tdata (ibfd)->phdr; i < num_segments; i++, p++)
3628 {
3629 unsigned int csecs;
3630 asection *s;
3631 asection **sections;
3632 asection *os;
3633 unsigned int isec;
3634 bfd_vma matching_lma;
3635 bfd_vma suggested_lma;
3636 unsigned int j;
3637
3638 /* For each section in the input BFD, decide if it should be
3639 included in the current segment. A section will be included
3640 if it is within the address space of the segment, and it is
3641 an allocated segment, and there is an output section
3642 associated with it. */
3643 csecs = 0;
3644 for (s = ibfd->sections; s != NULL; s = s->next)
3645 if (s->output_section != NULL)
3646 {
3647 if ((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3648 || IS_SOLARIS_PT_INTERP (p, s))
3649 && (s->flags & SEC_ALLOC) != 0)
3650 ++csecs;
3651 else if (IS_COREFILE_NOTE (p, s))
3652 ++csecs;
3653 }
3654
3655 /* Allocate a segment map big enough to contain all of the
3656 sections we have selected. */
3657 m = ((struct elf_segment_map *)
3658 bfd_alloc (obfd,
3659 (sizeof (struct elf_segment_map)
3660 + ((size_t) csecs - 1) * sizeof (asection *))));
3661 if (m == NULL)
3662 return false;
3663
3664 /* Initialise the fields of the segment map. Default to
3665 using the physical address of the segment in the input BFD. */
3666 m->next = NULL;
3667 m->p_type = p->p_type;
3668 m->p_flags = p->p_flags;
3669 m->p_flags_valid = 1;
3670 m->p_paddr = p->p_paddr;
3671 m->p_paddr_valid = 1;
3672
3673 /* Determine if this segment contains the ELF file header
3674 and if it contains the program headers themselves. */
3675 m->includes_filehdr = (p->p_offset == 0
3676 && p->p_filesz >= iehdr->e_ehsize);
3677
3678 m->includes_phdrs = 0;
3679
3680 if (! phdr_included || p->p_type != PT_LOAD)
3681 {
3682 m->includes_phdrs =
3683 (p->p_offset <= (bfd_vma) iehdr->e_phoff
3684 && (p->p_offset + p->p_filesz
3685 >= ((bfd_vma) iehdr->e_phoff
3686 + iehdr->e_phnum * iehdr->e_phentsize)));
3687 if (p->p_type == PT_LOAD && m->includes_phdrs)
3688 phdr_included = true;
3689 }
3690
3691 if (csecs == 0)
3692 {
3693 /* Special segments, such as the PT_PHDR segment, may contain
3694 no sections, but ordinary, loadable segments should contain
3695 something. */
3696
3697 if (p->p_type == PT_LOAD)
3698 _bfd_error_handler
3699 (_("%s: warning: Empty loadable segment detected\n"),
3700 bfd_get_filename (ibfd));
3701
3702 m->count = 0;
3703 *pm = m;
3704 pm = &m->next;
3705
3706 continue;
3707 }
3708
3709 /* Now scan the sections in the input BFD again and attempt
3710 to add their corresponding output sections to the segment map.
3711 The problem here is how to handle an output section which has
3712 been moved (ie had its LMA changed). There are four possibilities:
3713
3714 1. None of the sections have been moved.
3715 In this case we can continue to use the segment LMA from the
3716 input BFD.
3717
3718 2. All of the sections have been moved by the same amount.
3719 In this case we can change the segment's LMA to match the LMA
3720 of the first section.
3721
3722 3. Some of the sections have been moved, others have not.
3723 In this case those sections which have not been moved can be
3724 placed in the current segment which will have to have its size,
3725 and possibly its LMA changed, and a new segment or segments will
3726 have to be created to contain the other sections.
3727
3728 4. The sections have been moved, but not be the same amount.
3729 In this case we can change the segment's LMA to match the LMA
3730 of the first section and we will have to create a new segment
3731 or segments to contain the other sections.
3732
3733 In order to save time, we allocate an array to hold the section
3734 pointers that we are interested in. As these sections get assigned
3735 to a segment, they are removed from this array. */
3736
3737 sections = (asection **) bfd_malloc (sizeof (asection *) * csecs);
3738 if (sections == NULL)
3739 return false;
3740
3741 /* Step One: Scan for segment vs section LMA conflicts.
3742 Also add the sections to the section array allocated above.
3743 Also add the sections to the current segment. In the common
3744 case, where the sections have not been moved, this means that
3745 we have completely filled the segment, and there is nothing
3746 more to do. */
3747
3748 isec = 0;
3749 matching_lma = 0;
3750 suggested_lma = 0;
3751
3752 for (j = 0, s = ibfd->sections; s != NULL; s = s->next)
3753 {
3754 os = s->output_section;
3755
3756 if ((((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3757 || IS_SOLARIS_PT_INTERP (p, s))
3758 && (s->flags & SEC_ALLOC) != 0)
3759 || IS_COREFILE_NOTE (p, s))
3760 && os != NULL)
3761 {
3762 sections[j++] = s;
3763
3764 /* The Solaris native linker always sets p_paddr to 0.
3765 We try to catch that case here, and set it to the
3766 correct value. */
3767 if (p->p_paddr == 0
3768 && p->p_vaddr != 0
3769 && isec == 0
3770 && os->lma != 0
3771 && (os->vma == (p->p_vaddr
3772 + (m->includes_filehdr
3773 ? iehdr->e_ehsize
3774 : 0)
3775 + (m->includes_phdrs
3776 ? iehdr->e_phnum * iehdr->e_phentsize
3777 : 0))))
3778 m->p_paddr = p->p_vaddr;
3779
3780 /* Match up the physical address of the segment with the
3781 LMA address of the output section. */
3782 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3783 || IS_COREFILE_NOTE (p, s))
3784 {
3785 if (matching_lma == 0)
3786 matching_lma = os->lma;
3787
3788 /* We assume that if the section fits within the segment
3789 that it does not overlap any other section within that
3790 segment. */
3791 m->sections[isec++] = os;
3792 }
3793 else if (suggested_lma == 0)
3794 suggested_lma = os->lma;
3795 }
3796 }
3797
3798 BFD_ASSERT (j == csecs);
3799
3800 /* Step Two: Adjust the physical address of the current segment,
3801 if necessary. */
3802 if (isec == csecs)
3803 {
3804 /* All of the sections fitted within the segment as currently
3805 specified. This is the default case. Add the segment to
3806 the list of built segments and carry on to process the next
3807 program header in the input BFD. */
3808 m->count = csecs;
3809 *pm = m;
3810 pm = &m->next;
3811
3812 free (sections);
3813 continue;
3814 }
3815 else
3816 {
3817 if (matching_lma != 0)
3818 {
3819 /* At least one section fits inside the current segment.
3820 Keep it, but modify its physical address to match the
3821 LMA of the first section that fitted. */
3822
3823 m->p_paddr = matching_lma;
3824 }
3825 else
3826 {
3827 /* None of the sections fitted inside the current segment.
3828 Change the current segment's physical address to match
3829 the LMA of the first section. */
3830
3831 m->p_paddr = suggested_lma;
3832 }
3833
3834 /* Offset the segment physical address from the lma to allow
3835 for space taken up by elf headers. */
3836 if (m->includes_filehdr)
3837 m->p_paddr -= iehdr->e_ehsize;
3838
3839 if (m->includes_phdrs)
3840 m->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
3841 }
3842
3843 /* Step Three: Loop over the sections again, this time assigning
3844 those that fit to the current segment and remvoing them from the
3845 sections array; but making sure not to leave large gaps. Once all
3846 possible sections have been assigned to the current segment it is
3847 added to the list of built segments and if sections still remain
3848 to be assigned, a new segment is constructed before repeating
3849 the loop. */
3850 isec = 0;
3851 do
3852 {
3853 m->count = 0;
3854 suggested_lma = 0;
3855
3856 /* Fill the current segment with sections that fit. */
3857 for (j = 0; j < csecs; j++)
3858 {
3859 s = sections[j];
3860
3861 if (s == NULL)
3862 continue;
3863
3864 os = s->output_section;
3865
3866 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3867 || IS_COREFILE_NOTE (p, s))
3868 {
3869 if (m->count == 0)
3870 {
3871 /* If the first section in a segment does not start at
3872 the beginning of the segment, then something is wrong. */
3873 if (os->lma != (m->p_paddr
3874 + (m->includes_filehdr
3875 ? iehdr->e_ehsize : 0)
3876 + (m->includes_phdrs
3877 ? iehdr->e_phnum * iehdr->e_phentsize
3878 : 0)))
3879 abort ();
3880 }
3881 else
3882 {
3883 asection * prev_sec;
3884 bfd_vma maxpagesize;
3885
3886 prev_sec = m->sections[m->count - 1];
3887 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3888
3889 /* If the gap between the end of the previous section
3890 and the start of this section is more than maxpagesize
3891 then we need to start a new segment. */
3892 if (BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
3893 < BFD_ALIGN (os->lma, maxpagesize))
3894 {
3895 if (suggested_lma == 0)
3896 suggested_lma = os->lma;
3897
3898 continue;
3899 }
3900 }
3901
3902 m->sections[m->count++] = os;
3903 ++isec;
3904 sections[j] = NULL;
3905 }
3906 else if (suggested_lma == 0)
3907 suggested_lma = os->lma;
3908 }
3909
3910 BFD_ASSERT (m->count > 0);
3911
3912 /* Add the current segment to the list of built segments. */
3913 *pm = m;
3914 pm = &m->next;
3915
3916 if (isec < csecs)
3917 {
3918 /* We still have not allocated all of the sections to
3919 segments. Create a new segment here, initialise it
3920 and carry on looping. */
3921
3922 m = ((struct elf_segment_map *)
3923 bfd_alloc (obfd,
3924 (sizeof (struct elf_segment_map)
3925 + ((size_t) csecs - 1) * sizeof (asection *))));
3926 if (m == NULL)
3927 return false;
3928
3929 /* Initialise the fields of the segment map. Set the physical
3930 physical address to the LMA of the first section that has
3931 not yet been assigned. */
3932
3933 m->next = NULL;
3934 m->p_type = p->p_type;
3935 m->p_flags = p->p_flags;
3936 m->p_flags_valid = 1;
3937 m->p_paddr = suggested_lma;
3938 m->p_paddr_valid = 1;
3939 m->includes_filehdr = 0;
3940 m->includes_phdrs = 0;
3941 }
3942 }
3943 while (isec < csecs);
3944
3945 free (sections);
3946 }
3947
3948 /* The Solaris linker creates program headers in which all the
3949 p_paddr fields are zero. When we try to objcopy or strip such a
3950 file, we get confused. Check for this case, and if we find it
3951 reset the p_paddr_valid fields. */
3952 for (m = mfirst; m != NULL; m = m->next)
3953 if (m->p_paddr != 0)
3954 break;
3955 if (m == NULL)
3956 {
3957 for (m = mfirst; m != NULL; m = m->next)
3958 m->p_paddr_valid = 0;
3959 }
3960
3961 elf_tdata (obfd)->segment_map = mfirst;
3962
3963 #if 0
3964 /* Final Step: Sort the segments into ascending order of physical address. */
3965 if (mfirst != NULL)
3966 {
3967 struct elf_segment_map* prev;
3968
3969 prev = mfirst;
3970 for (m = mfirst->next; m != NULL; prev = m, m = m->next)
3971 {
3972 /* Yes I know - its a bubble sort....*/
3973 if (m->next != NULL && (m->next->p_paddr < m->p_paddr))
3974 {
3975 /* swap m and m->next */
3976 prev->next = m->next;
3977 m->next = m->next->next;
3978 prev->next->next = m;
3979
3980 /* restart loop. */
3981 m = mfirst;
3982 }
3983 }
3984 }
3985 #endif
3986
3987 #undef IS_CONTAINED_BY
3988 #undef IS_SOLARIS_PT_INTERP
3989 #undef IS_COREFILE_NOTE
3990 return true;
3991 }
3992
3993 /* Copy private section information. This copies over the entsize
3994 field, and sometimes the info field. */
3995
3996 boolean
3997 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
3998 bfd *ibfd;
3999 asection *isec;
4000 bfd *obfd;
4001 asection *osec;
4002 {
4003 Elf_Internal_Shdr *ihdr, *ohdr;
4004
4005 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4006 || obfd->xvec->flavour != bfd_target_elf_flavour)
4007 return true;
4008
4009 /* Copy over private BFD data if it has not already been copied.
4010 This must be done here, rather than in the copy_private_bfd_data
4011 entry point, because the latter is called after the section
4012 contents have been set, which means that the program headers have
4013 already been worked out. */
4014 if (elf_tdata (obfd)->segment_map == NULL
4015 && elf_tdata (ibfd)->phdr != NULL)
4016 {
4017 asection *s;
4018
4019 /* Only set up the segments if there are no more SEC_ALLOC
4020 sections. FIXME: This won't do the right thing if objcopy is
4021 used to remove the last SEC_ALLOC section, since objcopy
4022 won't call this routine in that case. */
4023 for (s = isec->next; s != NULL; s = s->next)
4024 if ((s->flags & SEC_ALLOC) != 0)
4025 break;
4026 if (s == NULL)
4027 {
4028 if (! copy_private_bfd_data (ibfd, obfd))
4029 return false;
4030 }
4031 }
4032
4033 ihdr = &elf_section_data (isec)->this_hdr;
4034 ohdr = &elf_section_data (osec)->this_hdr;
4035
4036 ohdr->sh_entsize = ihdr->sh_entsize;
4037
4038 if (ihdr->sh_type == SHT_SYMTAB
4039 || ihdr->sh_type == SHT_DYNSYM
4040 || ihdr->sh_type == SHT_GNU_verneed
4041 || ihdr->sh_type == SHT_GNU_verdef)
4042 ohdr->sh_info = ihdr->sh_info;
4043
4044 elf_section_data (osec)->use_rela_p
4045 = elf_section_data (isec)->use_rela_p;
4046
4047 return true;
4048 }
4049
4050 /* Copy private symbol information. If this symbol is in a section
4051 which we did not map into a BFD section, try to map the section
4052 index correctly. We use special macro definitions for the mapped
4053 section indices; these definitions are interpreted by the
4054 swap_out_syms function. */
4055
4056 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4057 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4058 #define MAP_STRTAB (SHN_LORESERVE - 3)
4059 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4060
4061 boolean
4062 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4063 bfd *ibfd;
4064 asymbol *isymarg;
4065 bfd *obfd;
4066 asymbol *osymarg;
4067 {
4068 elf_symbol_type *isym, *osym;
4069
4070 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4071 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4072 return true;
4073
4074 isym = elf_symbol_from (ibfd, isymarg);
4075 osym = elf_symbol_from (obfd, osymarg);
4076
4077 if (isym != NULL
4078 && osym != NULL
4079 && bfd_is_abs_section (isym->symbol.section))
4080 {
4081 unsigned int shndx;
4082
4083 shndx = isym->internal_elf_sym.st_shndx;
4084 if (shndx == elf_onesymtab (ibfd))
4085 shndx = MAP_ONESYMTAB;
4086 else if (shndx == elf_dynsymtab (ibfd))
4087 shndx = MAP_DYNSYMTAB;
4088 else if (shndx == elf_tdata (ibfd)->strtab_section)
4089 shndx = MAP_STRTAB;
4090 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4091 shndx = MAP_SHSTRTAB;
4092 osym->internal_elf_sym.st_shndx = shndx;
4093 }
4094
4095 return true;
4096 }
4097
4098 /* Swap out the symbols. */
4099
4100 static boolean
4101 swap_out_syms (abfd, sttp, relocatable_p)
4102 bfd *abfd;
4103 struct bfd_strtab_hash **sttp;
4104 int relocatable_p;
4105 {
4106 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4107
4108 if (!elf_map_symbols (abfd))
4109 return false;
4110
4111 /* Dump out the symtabs. */
4112 {
4113 int symcount = bfd_get_symcount (abfd);
4114 asymbol **syms = bfd_get_outsymbols (abfd);
4115 struct bfd_strtab_hash *stt;
4116 Elf_Internal_Shdr *symtab_hdr;
4117 Elf_Internal_Shdr *symstrtab_hdr;
4118 char *outbound_syms;
4119 int idx;
4120
4121 stt = _bfd_elf_stringtab_init ();
4122 if (stt == NULL)
4123 return false;
4124
4125 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4126 symtab_hdr->sh_type = SHT_SYMTAB;
4127 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4128 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4129 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4130 symtab_hdr->sh_addralign = bed->s->file_align;
4131
4132 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4133 symstrtab_hdr->sh_type = SHT_STRTAB;
4134
4135 outbound_syms = bfd_alloc (abfd,
4136 (1 + symcount) * bed->s->sizeof_sym);
4137 if (outbound_syms == NULL)
4138 return false;
4139 symtab_hdr->contents = (PTR) outbound_syms;
4140
4141 /* now generate the data (for "contents") */
4142 {
4143 /* Fill in zeroth symbol and swap it out. */
4144 Elf_Internal_Sym sym;
4145 sym.st_name = 0;
4146 sym.st_value = 0;
4147 sym.st_size = 0;
4148 sym.st_info = 0;
4149 sym.st_other = 0;
4150 sym.st_shndx = SHN_UNDEF;
4151 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4152 outbound_syms += bed->s->sizeof_sym;
4153 }
4154 for (idx = 0; idx < symcount; idx++)
4155 {
4156 Elf_Internal_Sym sym;
4157 bfd_vma value = syms[idx]->value;
4158 elf_symbol_type *type_ptr;
4159 flagword flags = syms[idx]->flags;
4160 int type;
4161
4162 if (flags & BSF_SECTION_SYM)
4163 /* Section symbols have no names. */
4164 sym.st_name = 0;
4165 else
4166 {
4167 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4168 syms[idx]->name,
4169 true, false);
4170 if (sym.st_name == (unsigned long) -1)
4171 return false;
4172 }
4173
4174 type_ptr = elf_symbol_from (abfd, syms[idx]);
4175
4176 if ((flags & BSF_SECTION_SYM) == 0
4177 && bfd_is_com_section (syms[idx]->section))
4178 {
4179 /* ELF common symbols put the alignment into the `value' field,
4180 and the size into the `size' field. This is backwards from
4181 how BFD handles it, so reverse it here. */
4182 sym.st_size = value;
4183 if (type_ptr == NULL
4184 || type_ptr->internal_elf_sym.st_value == 0)
4185 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4186 else
4187 sym.st_value = type_ptr->internal_elf_sym.st_value;
4188 sym.st_shndx = _bfd_elf_section_from_bfd_section
4189 (abfd, syms[idx]->section);
4190 }
4191 else
4192 {
4193 asection *sec = syms[idx]->section;
4194 int shndx;
4195
4196 if (sec->output_section)
4197 {
4198 value += sec->output_offset;
4199 sec = sec->output_section;
4200 }
4201 /* Don't add in the section vma for relocatable output. */
4202 if (! relocatable_p)
4203 value += sec->vma;
4204 sym.st_value = value;
4205 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4206
4207 if (bfd_is_abs_section (sec)
4208 && type_ptr != NULL
4209 && type_ptr->internal_elf_sym.st_shndx != 0)
4210 {
4211 /* This symbol is in a real ELF section which we did
4212 not create as a BFD section. Undo the mapping done
4213 by copy_private_symbol_data. */
4214 shndx = type_ptr->internal_elf_sym.st_shndx;
4215 switch (shndx)
4216 {
4217 case MAP_ONESYMTAB:
4218 shndx = elf_onesymtab (abfd);
4219 break;
4220 case MAP_DYNSYMTAB:
4221 shndx = elf_dynsymtab (abfd);
4222 break;
4223 case MAP_STRTAB:
4224 shndx = elf_tdata (abfd)->strtab_section;
4225 break;
4226 case MAP_SHSTRTAB:
4227 shndx = elf_tdata (abfd)->shstrtab_section;
4228 break;
4229 default:
4230 break;
4231 }
4232 }
4233 else
4234 {
4235 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4236
4237 if (shndx == -1)
4238 {
4239 asection *sec2;
4240
4241 /* Writing this would be a hell of a lot easier if
4242 we had some decent documentation on bfd, and
4243 knew what to expect of the library, and what to
4244 demand of applications. For example, it
4245 appears that `objcopy' might not set the
4246 section of a symbol to be a section that is
4247 actually in the output file. */
4248 sec2 = bfd_get_section_by_name (abfd, sec->name);
4249 BFD_ASSERT (sec2 != 0);
4250 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4251 BFD_ASSERT (shndx != -1);
4252 }
4253 }
4254
4255 sym.st_shndx = shndx;
4256 }
4257
4258 if ((flags & BSF_FUNCTION) != 0)
4259 type = STT_FUNC;
4260 else if ((flags & BSF_OBJECT) != 0)
4261 type = STT_OBJECT;
4262 else
4263 type = STT_NOTYPE;
4264
4265 /* Processor-specific types */
4266 if (type_ptr != NULL
4267 && bed->elf_backend_get_symbol_type)
4268 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4269
4270 if (flags & BSF_SECTION_SYM)
4271 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4272 else if (bfd_is_com_section (syms[idx]->section))
4273 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4274 else if (bfd_is_und_section (syms[idx]->section))
4275 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4276 ? STB_WEAK
4277 : STB_GLOBAL),
4278 type);
4279 else if (flags & BSF_FILE)
4280 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4281 else
4282 {
4283 int bind = STB_LOCAL;
4284
4285 if (flags & BSF_LOCAL)
4286 bind = STB_LOCAL;
4287 else if (flags & BSF_WEAK)
4288 bind = STB_WEAK;
4289 else if (flags & BSF_GLOBAL)
4290 bind = STB_GLOBAL;
4291
4292 sym.st_info = ELF_ST_INFO (bind, type);
4293 }
4294
4295 if (type_ptr != NULL)
4296 sym.st_other = type_ptr->internal_elf_sym.st_other;
4297 else
4298 sym.st_other = 0;
4299
4300 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4301 outbound_syms += bed->s->sizeof_sym;
4302 }
4303
4304 *sttp = stt;
4305 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4306 symstrtab_hdr->sh_type = SHT_STRTAB;
4307
4308 symstrtab_hdr->sh_flags = 0;
4309 symstrtab_hdr->sh_addr = 0;
4310 symstrtab_hdr->sh_entsize = 0;
4311 symstrtab_hdr->sh_link = 0;
4312 symstrtab_hdr->sh_info = 0;
4313 symstrtab_hdr->sh_addralign = 1;
4314 }
4315
4316 return true;
4317 }
4318
4319 /* Return the number of bytes required to hold the symtab vector.
4320
4321 Note that we base it on the count plus 1, since we will null terminate
4322 the vector allocated based on this size. However, the ELF symbol table
4323 always has a dummy entry as symbol #0, so it ends up even. */
4324
4325 long
4326 _bfd_elf_get_symtab_upper_bound (abfd)
4327 bfd *abfd;
4328 {
4329 long symcount;
4330 long symtab_size;
4331 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4332
4333 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4334 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4335
4336 return symtab_size;
4337 }
4338
4339 long
4340 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4341 bfd *abfd;
4342 {
4343 long symcount;
4344 long symtab_size;
4345 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4346
4347 if (elf_dynsymtab (abfd) == 0)
4348 {
4349 bfd_set_error (bfd_error_invalid_operation);
4350 return -1;
4351 }
4352
4353 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4354 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4355
4356 return symtab_size;
4357 }
4358
4359 long
4360 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4361 bfd *abfd ATTRIBUTE_UNUSED;
4362 sec_ptr asect;
4363 {
4364 return (asect->reloc_count + 1) * sizeof (arelent *);
4365 }
4366
4367 /* Canonicalize the relocs. */
4368
4369 long
4370 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4371 bfd *abfd;
4372 sec_ptr section;
4373 arelent **relptr;
4374 asymbol **symbols;
4375 {
4376 arelent *tblptr;
4377 unsigned int i;
4378
4379 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4380 section,
4381 symbols,
4382 false))
4383 return -1;
4384
4385 tblptr = section->relocation;
4386 for (i = 0; i < section->reloc_count; i++)
4387 *relptr++ = tblptr++;
4388
4389 *relptr = NULL;
4390
4391 return section->reloc_count;
4392 }
4393
4394 long
4395 _bfd_elf_get_symtab (abfd, alocation)
4396 bfd *abfd;
4397 asymbol **alocation;
4398 {
4399 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4400 (abfd, alocation, false);
4401
4402 if (symcount >= 0)
4403 bfd_get_symcount (abfd) = symcount;
4404 return symcount;
4405 }
4406
4407 long
4408 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4409 bfd *abfd;
4410 asymbol **alocation;
4411 {
4412 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4413 (abfd, alocation, true);
4414 }
4415
4416 /* Return the size required for the dynamic reloc entries. Any
4417 section that was actually installed in the BFD, and has type
4418 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4419 considered to be a dynamic reloc section. */
4420
4421 long
4422 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4423 bfd *abfd;
4424 {
4425 long ret;
4426 asection *s;
4427
4428 if (elf_dynsymtab (abfd) == 0)
4429 {
4430 bfd_set_error (bfd_error_invalid_operation);
4431 return -1;
4432 }
4433
4434 ret = sizeof (arelent *);
4435 for (s = abfd->sections; s != NULL; s = s->next)
4436 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4437 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4438 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4439 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4440 * sizeof (arelent *));
4441
4442 return ret;
4443 }
4444
4445 /* Canonicalize the dynamic relocation entries. Note that we return
4446 the dynamic relocations as a single block, although they are
4447 actually associated with particular sections; the interface, which
4448 was designed for SunOS style shared libraries, expects that there
4449 is only one set of dynamic relocs. Any section that was actually
4450 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4451 the dynamic symbol table, is considered to be a dynamic reloc
4452 section. */
4453
4454 long
4455 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4456 bfd *abfd;
4457 arelent **storage;
4458 asymbol **syms;
4459 {
4460 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4461 asection *s;
4462 long ret;
4463
4464 if (elf_dynsymtab (abfd) == 0)
4465 {
4466 bfd_set_error (bfd_error_invalid_operation);
4467 return -1;
4468 }
4469
4470 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4471 ret = 0;
4472 for (s = abfd->sections; s != NULL; s = s->next)
4473 {
4474 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4475 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4476 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4477 {
4478 arelent *p;
4479 long count, i;
4480
4481 if (! (*slurp_relocs) (abfd, s, syms, true))
4482 return -1;
4483 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4484 p = s->relocation;
4485 for (i = 0; i < count; i++)
4486 *storage++ = p++;
4487 ret += count;
4488 }
4489 }
4490
4491 *storage = NULL;
4492
4493 return ret;
4494 }
4495 \f
4496 /* Read in the version information. */
4497
4498 boolean
4499 _bfd_elf_slurp_version_tables (abfd)
4500 bfd *abfd;
4501 {
4502 bfd_byte *contents = NULL;
4503
4504 if (elf_dynverdef (abfd) != 0)
4505 {
4506 Elf_Internal_Shdr *hdr;
4507 Elf_External_Verdef *everdef;
4508 Elf_Internal_Verdef *iverdef;
4509 Elf_Internal_Verdef *iverdefarr;
4510 Elf_Internal_Verdef iverdefmem;
4511 unsigned int i;
4512 unsigned int maxidx;
4513
4514 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4515
4516 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4517 if (contents == NULL)
4518 goto error_return;
4519 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4520 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4521 goto error_return;
4522
4523 /* We know the number of entries in the section but not the maximum
4524 index. Therefore we have to run through all entries and find
4525 the maximum. */
4526 everdef = (Elf_External_Verdef *) contents;
4527 maxidx = 0;
4528 for (i = 0; i < hdr->sh_info; ++i)
4529 {
4530 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4531
4532 if ((iverdefmem.vd_ndx & VERSYM_VERSION) > maxidx)
4533 maxidx = iverdefmem.vd_ndx & VERSYM_VERSION;
4534
4535 everdef = ((Elf_External_Verdef *)
4536 ((bfd_byte *) everdef + iverdefmem.vd_next));
4537 }
4538
4539 elf_tdata (abfd)->verdef =
4540 ((Elf_Internal_Verdef *)
4541 bfd_zalloc (abfd, maxidx * sizeof (Elf_Internal_Verdef)));
4542 if (elf_tdata (abfd)->verdef == NULL)
4543 goto error_return;
4544
4545 elf_tdata (abfd)->cverdefs = maxidx;
4546
4547 everdef = (Elf_External_Verdef *) contents;
4548 iverdefarr = elf_tdata (abfd)->verdef;
4549 for (i = 0; i < hdr->sh_info; i++)
4550 {
4551 Elf_External_Verdaux *everdaux;
4552 Elf_Internal_Verdaux *iverdaux;
4553 unsigned int j;
4554
4555 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4556
4557 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
4558 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
4559
4560 iverdef->vd_bfd = abfd;
4561
4562 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4563 bfd_alloc (abfd,
4564 (iverdef->vd_cnt
4565 * sizeof (Elf_Internal_Verdaux))));
4566 if (iverdef->vd_auxptr == NULL)
4567 goto error_return;
4568
4569 everdaux = ((Elf_External_Verdaux *)
4570 ((bfd_byte *) everdef + iverdef->vd_aux));
4571 iverdaux = iverdef->vd_auxptr;
4572 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4573 {
4574 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4575
4576 iverdaux->vda_nodename =
4577 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4578 iverdaux->vda_name);
4579 if (iverdaux->vda_nodename == NULL)
4580 goto error_return;
4581
4582 if (j + 1 < iverdef->vd_cnt)
4583 iverdaux->vda_nextptr = iverdaux + 1;
4584 else
4585 iverdaux->vda_nextptr = NULL;
4586
4587 everdaux = ((Elf_External_Verdaux *)
4588 ((bfd_byte *) everdaux + iverdaux->vda_next));
4589 }
4590
4591 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4592
4593 if (i + 1 < hdr->sh_info)
4594 iverdef->vd_nextdef = iverdef + 1;
4595 else
4596 iverdef->vd_nextdef = NULL;
4597
4598 everdef = ((Elf_External_Verdef *)
4599 ((bfd_byte *) everdef + iverdef->vd_next));
4600 }
4601
4602 free (contents);
4603 contents = NULL;
4604 }
4605
4606 if (elf_dynverref (abfd) != 0)
4607 {
4608 Elf_Internal_Shdr *hdr;
4609 Elf_External_Verneed *everneed;
4610 Elf_Internal_Verneed *iverneed;
4611 unsigned int i;
4612
4613 hdr = &elf_tdata (abfd)->dynverref_hdr;
4614
4615 elf_tdata (abfd)->verref =
4616 ((Elf_Internal_Verneed *)
4617 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4618 if (elf_tdata (abfd)->verref == NULL)
4619 goto error_return;
4620
4621 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4622
4623 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4624 if (contents == NULL)
4625 goto error_return;
4626 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4627 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4628 goto error_return;
4629
4630 everneed = (Elf_External_Verneed *) contents;
4631 iverneed = elf_tdata (abfd)->verref;
4632 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4633 {
4634 Elf_External_Vernaux *evernaux;
4635 Elf_Internal_Vernaux *ivernaux;
4636 unsigned int j;
4637
4638 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4639
4640 iverneed->vn_bfd = abfd;
4641
4642 iverneed->vn_filename =
4643 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4644 iverneed->vn_file);
4645 if (iverneed->vn_filename == NULL)
4646 goto error_return;
4647
4648 iverneed->vn_auxptr =
4649 ((Elf_Internal_Vernaux *)
4650 bfd_alloc (abfd,
4651 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4652
4653 evernaux = ((Elf_External_Vernaux *)
4654 ((bfd_byte *) everneed + iverneed->vn_aux));
4655 ivernaux = iverneed->vn_auxptr;
4656 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4657 {
4658 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4659
4660 ivernaux->vna_nodename =
4661 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4662 ivernaux->vna_name);
4663 if (ivernaux->vna_nodename == NULL)
4664 goto error_return;
4665
4666 if (j + 1 < iverneed->vn_cnt)
4667 ivernaux->vna_nextptr = ivernaux + 1;
4668 else
4669 ivernaux->vna_nextptr = NULL;
4670
4671 evernaux = ((Elf_External_Vernaux *)
4672 ((bfd_byte *) evernaux + ivernaux->vna_next));
4673 }
4674
4675 if (i + 1 < hdr->sh_info)
4676 iverneed->vn_nextref = iverneed + 1;
4677 else
4678 iverneed->vn_nextref = NULL;
4679
4680 everneed = ((Elf_External_Verneed *)
4681 ((bfd_byte *) everneed + iverneed->vn_next));
4682 }
4683
4684 free (contents);
4685 contents = NULL;
4686 }
4687
4688 return true;
4689
4690 error_return:
4691 if (contents == NULL)
4692 free (contents);
4693 return false;
4694 }
4695 \f
4696 asymbol *
4697 _bfd_elf_make_empty_symbol (abfd)
4698 bfd *abfd;
4699 {
4700 elf_symbol_type *newsym;
4701
4702 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4703 if (!newsym)
4704 return NULL;
4705 else
4706 {
4707 newsym->symbol.the_bfd = abfd;
4708 return &newsym->symbol;
4709 }
4710 }
4711
4712 void
4713 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4714 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4715 asymbol *symbol;
4716 symbol_info *ret;
4717 {
4718 bfd_symbol_info (symbol, ret);
4719 }
4720
4721 /* Return whether a symbol name implies a local symbol. Most targets
4722 use this function for the is_local_label_name entry point, but some
4723 override it. */
4724
4725 boolean
4726 _bfd_elf_is_local_label_name (abfd, name)
4727 bfd *abfd ATTRIBUTE_UNUSED;
4728 const char *name;
4729 {
4730 /* Normal local symbols start with ``.L''. */
4731 if (name[0] == '.' && name[1] == 'L')
4732 return true;
4733
4734 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4735 DWARF debugging symbols starting with ``..''. */
4736 if (name[0] == '.' && name[1] == '.')
4737 return true;
4738
4739 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4740 emitting DWARF debugging output. I suspect this is actually a
4741 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4742 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4743 underscore to be emitted on some ELF targets). For ease of use,
4744 we treat such symbols as local. */
4745 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4746 return true;
4747
4748 return false;
4749 }
4750
4751 alent *
4752 _bfd_elf_get_lineno (ignore_abfd, symbol)
4753 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4754 asymbol *symbol ATTRIBUTE_UNUSED;
4755 {
4756 abort ();
4757 return NULL;
4758 }
4759
4760 boolean
4761 _bfd_elf_set_arch_mach (abfd, arch, machine)
4762 bfd *abfd;
4763 enum bfd_architecture arch;
4764 unsigned long machine;
4765 {
4766 /* If this isn't the right architecture for this backend, and this
4767 isn't the generic backend, fail. */
4768 if (arch != get_elf_backend_data (abfd)->arch
4769 && arch != bfd_arch_unknown
4770 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4771 return false;
4772
4773 return bfd_default_set_arch_mach (abfd, arch, machine);
4774 }
4775
4776 /* Find the nearest line to a particular section and offset, for error
4777 reporting. */
4778
4779 boolean
4780 _bfd_elf_find_nearest_line (abfd,
4781 section,
4782 symbols,
4783 offset,
4784 filename_ptr,
4785 functionname_ptr,
4786 line_ptr)
4787 bfd *abfd;
4788 asection *section;
4789 asymbol **symbols;
4790 bfd_vma offset;
4791 CONST char **filename_ptr;
4792 CONST char **functionname_ptr;
4793 unsigned int *line_ptr;
4794 {
4795 boolean found;
4796 const char *filename;
4797 asymbol *func;
4798 bfd_vma low_func;
4799 asymbol **p;
4800
4801 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4802 filename_ptr, functionname_ptr,
4803 line_ptr))
4804 return true;
4805
4806 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4807 filename_ptr, functionname_ptr,
4808 line_ptr, 0))
4809 return true;
4810
4811 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4812 &found, filename_ptr,
4813 functionname_ptr, line_ptr,
4814 &elf_tdata (abfd)->line_info))
4815 return false;
4816 if (found)
4817 return true;
4818
4819 if (symbols == NULL)
4820 return false;
4821
4822 filename = NULL;
4823 func = NULL;
4824 low_func = 0;
4825
4826 for (p = symbols; *p != NULL; p++)
4827 {
4828 elf_symbol_type *q;
4829
4830 q = (elf_symbol_type *) *p;
4831
4832 if (bfd_get_section (&q->symbol) != section)
4833 continue;
4834
4835 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4836 {
4837 default:
4838 break;
4839 case STT_FILE:
4840 filename = bfd_asymbol_name (&q->symbol);
4841 break;
4842 case STT_NOTYPE:
4843 case STT_FUNC:
4844 if (q->symbol.section == section
4845 && q->symbol.value >= low_func
4846 && q->symbol.value <= offset)
4847 {
4848 func = (asymbol *) q;
4849 low_func = q->symbol.value;
4850 }
4851 break;
4852 }
4853 }
4854
4855 if (func == NULL)
4856 return false;
4857
4858 *filename_ptr = filename;
4859 *functionname_ptr = bfd_asymbol_name (func);
4860 *line_ptr = 0;
4861 return true;
4862 }
4863
4864 int
4865 _bfd_elf_sizeof_headers (abfd, reloc)
4866 bfd *abfd;
4867 boolean reloc;
4868 {
4869 int ret;
4870
4871 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
4872 if (! reloc)
4873 ret += get_program_header_size (abfd);
4874 return ret;
4875 }
4876
4877 boolean
4878 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
4879 bfd *abfd;
4880 sec_ptr section;
4881 PTR location;
4882 file_ptr offset;
4883 bfd_size_type count;
4884 {
4885 Elf_Internal_Shdr *hdr;
4886
4887 if (! abfd->output_has_begun
4888 && ! _bfd_elf_compute_section_file_positions
4889 (abfd, (struct bfd_link_info *) NULL))
4890 return false;
4891
4892 hdr = &elf_section_data (section)->this_hdr;
4893
4894 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
4895 return false;
4896 if (bfd_write (location, 1, count, abfd) != count)
4897 return false;
4898
4899 return true;
4900 }
4901
4902 void
4903 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
4904 bfd *abfd ATTRIBUTE_UNUSED;
4905 arelent *cache_ptr ATTRIBUTE_UNUSED;
4906 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
4907 {
4908 abort ();
4909 }
4910
4911 #if 0
4912 void
4913 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
4914 bfd *abfd;
4915 arelent *cache_ptr;
4916 Elf_Internal_Rel *dst;
4917 {
4918 abort ();
4919 }
4920 #endif
4921
4922 /* Try to convert a non-ELF reloc into an ELF one. */
4923
4924 boolean
4925 _bfd_elf_validate_reloc (abfd, areloc)
4926 bfd *abfd;
4927 arelent *areloc;
4928 {
4929 /* Check whether we really have an ELF howto. */
4930
4931 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
4932 {
4933 bfd_reloc_code_real_type code;
4934 reloc_howto_type *howto;
4935
4936 /* Alien reloc: Try to determine its type to replace it with an
4937 equivalent ELF reloc. */
4938
4939 if (areloc->howto->pc_relative)
4940 {
4941 switch (areloc->howto->bitsize)
4942 {
4943 case 8:
4944 code = BFD_RELOC_8_PCREL;
4945 break;
4946 case 12:
4947 code = BFD_RELOC_12_PCREL;
4948 break;
4949 case 16:
4950 code = BFD_RELOC_16_PCREL;
4951 break;
4952 case 24:
4953 code = BFD_RELOC_24_PCREL;
4954 break;
4955 case 32:
4956 code = BFD_RELOC_32_PCREL;
4957 break;
4958 case 64:
4959 code = BFD_RELOC_64_PCREL;
4960 break;
4961 default:
4962 goto fail;
4963 }
4964
4965 howto = bfd_reloc_type_lookup (abfd, code);
4966
4967 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
4968 {
4969 if (howto->pcrel_offset)
4970 areloc->addend += areloc->address;
4971 else
4972 areloc->addend -= areloc->address; /* addend is unsigned!! */
4973 }
4974 }
4975 else
4976 {
4977 switch (areloc->howto->bitsize)
4978 {
4979 case 8:
4980 code = BFD_RELOC_8;
4981 break;
4982 case 14:
4983 code = BFD_RELOC_14;
4984 break;
4985 case 16:
4986 code = BFD_RELOC_16;
4987 break;
4988 case 26:
4989 code = BFD_RELOC_26;
4990 break;
4991 case 32:
4992 code = BFD_RELOC_32;
4993 break;
4994 case 64:
4995 code = BFD_RELOC_64;
4996 break;
4997 default:
4998 goto fail;
4999 }
5000
5001 howto = bfd_reloc_type_lookup (abfd, code);
5002 }
5003
5004 if (howto)
5005 areloc->howto = howto;
5006 else
5007 goto fail;
5008 }
5009
5010 return true;
5011
5012 fail:
5013 (*_bfd_error_handler)
5014 (_("%s: unsupported relocation type %s"),
5015 bfd_get_filename (abfd), areloc->howto->name);
5016 bfd_set_error (bfd_error_bad_value);
5017 return false;
5018 }
5019
5020 boolean
5021 _bfd_elf_close_and_cleanup (abfd)
5022 bfd *abfd;
5023 {
5024 if (bfd_get_format (abfd) == bfd_object)
5025 {
5026 if (elf_shstrtab (abfd) != NULL)
5027 _bfd_stringtab_free (elf_shstrtab (abfd));
5028 }
5029
5030 return _bfd_generic_close_and_cleanup (abfd);
5031 }
5032
5033 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5034 in the relocation's offset. Thus we cannot allow any sort of sanity
5035 range-checking to interfere. There is nothing else to do in processing
5036 this reloc. */
5037
5038 bfd_reloc_status_type
5039 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5040 bfd *abfd ATTRIBUTE_UNUSED;
5041 arelent *re ATTRIBUTE_UNUSED;
5042 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5043 PTR data ATTRIBUTE_UNUSED;
5044 asection *is ATTRIBUTE_UNUSED;
5045 bfd *obfd ATTRIBUTE_UNUSED;
5046 char **errmsg ATTRIBUTE_UNUSED;
5047 {
5048 return bfd_reloc_ok;
5049 }
5050
5051 \f
5052 /* Elf core file support. Much of this only works on native
5053 toolchains, since we rely on knowing the
5054 machine-dependent procfs structure in order to pick
5055 out details about the corefile. */
5056
5057 #ifdef HAVE_SYS_PROCFS_H
5058 # include <sys/procfs.h>
5059 #endif
5060
5061
5062 /* Define offsetof for those systems which lack it. */
5063
5064 #ifndef offsetof
5065 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5066 #endif
5067
5068
5069 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5070
5071 static int
5072 elfcore_make_pid (abfd)
5073 bfd* abfd;
5074 {
5075 return ((elf_tdata (abfd)->core_lwpid << 16)
5076 + (elf_tdata (abfd)->core_pid));
5077 }
5078
5079
5080 /* If there isn't a section called NAME, make one, using
5081 data from SECT. Note, this function will generate a
5082 reference to NAME, so you shouldn't deallocate or
5083 overwrite it. */
5084
5085 static boolean
5086 elfcore_maybe_make_sect (abfd, name, sect)
5087 bfd* abfd;
5088 char* name;
5089 asection* sect;
5090 {
5091 asection* sect2;
5092
5093 if (bfd_get_section_by_name (abfd, name) != NULL)
5094 return true;
5095
5096 sect2 = bfd_make_section (abfd, name);
5097 if (sect2 == NULL)
5098 return false;
5099
5100 sect2->_raw_size = sect->_raw_size;
5101 sect2->filepos = sect->filepos;
5102 sect2->flags = sect->flags;
5103 sect2->alignment_power = sect->alignment_power;
5104 return true;
5105 }
5106
5107
5108 /* prstatus_t exists on:
5109 solaris 2.5+
5110 linux 2.[01] + glibc
5111 unixware 4.2
5112 */
5113
5114 #if defined (HAVE_PRSTATUS_T)
5115 static boolean
5116 elfcore_grok_prstatus (abfd, note)
5117 bfd* abfd;
5118 Elf_Internal_Note* note;
5119 {
5120 char buf[100];
5121 char* name;
5122 asection* sect;
5123 int raw_size;
5124
5125 if (note->descsz == sizeof (prstatus_t))
5126 {
5127 prstatus_t prstat;
5128
5129 raw_size = sizeof (prstat.pr_reg);
5130 memcpy (&prstat, note->descdata, sizeof (prstat));
5131
5132 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5133 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5134
5135 /* pr_who exists on:
5136 solaris 2.5+
5137 unixware 4.2
5138 pr_who doesn't exist on:
5139 linux 2.[01]
5140 */
5141 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5142 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5143 #endif
5144 }
5145 #if defined (__sparcv9)
5146 else if (note->descsz == sizeof (prstatus32_t))
5147 {
5148 /* 64-bit host, 32-bit corefile */
5149 prstatus32_t prstat;
5150
5151 raw_size = sizeof (prstat.pr_reg);
5152 memcpy (&prstat, note->descdata, sizeof (prstat));
5153
5154 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5155 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5156
5157 /* pr_who exists on:
5158 solaris 2.5+
5159 unixware 4.2
5160 pr_who doesn't exist on:
5161 linux 2.[01]
5162 */
5163 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5164 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5165 #endif
5166 }
5167 #endif /* __sparcv9 */
5168 else
5169 {
5170 /* Fail - we don't know how to handle any other
5171 note size (ie. data object type). */
5172 return true;
5173 }
5174
5175 /* Make a ".reg/999" section. */
5176
5177 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5178 name = bfd_alloc (abfd, strlen (buf) + 1);
5179 if (name == NULL)
5180 return false;
5181 strcpy (name, buf);
5182
5183 sect = bfd_make_section (abfd, name);
5184 if (sect == NULL)
5185 return false;
5186
5187 if (note->descsz == sizeof (prstatus_t))
5188 {
5189 sect->_raw_size = raw_size;
5190 sect->filepos = note->descpos + offsetof (prstatus_t, pr_reg);
5191 }
5192 #if defined (__sparcv9)
5193 else if (note->descsz == sizeof (prstatus32_t))
5194 {
5195 sect->_raw_size = raw_size;
5196 sect->filepos = note->descpos + offsetof (prstatus32_t, pr_reg);
5197 }
5198 #endif
5199
5200 sect->flags = SEC_HAS_CONTENTS;
5201 sect->alignment_power = 2;
5202
5203 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5204 return false;
5205
5206 return true;
5207 }
5208 #endif /* defined (HAVE_PRSTATUS_T) */
5209
5210
5211 /* Create a pseudosection containing the exact contents of NOTE. This
5212 actually creates up to two pseudosections:
5213 - For the single-threaded case, a section named NAME, unless
5214 such a section already exists.
5215 - For the multi-threaded case, a section named "NAME/PID", where
5216 PID is elfcore_make_pid (abfd).
5217 Both pseudosections have identical contents: the contents of NOTE. */
5218
5219 static boolean
5220 elfcore_make_note_pseudosection (abfd, name, note)
5221 bfd* abfd;
5222 char *name;
5223 Elf_Internal_Note* note;
5224 {
5225 char buf[100];
5226 char *threaded_name;
5227 asection* sect;
5228
5229 /* Build the section name. */
5230
5231 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5232 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5233 if (threaded_name == NULL)
5234 return false;
5235 strcpy (threaded_name, buf);
5236
5237 sect = bfd_make_section (abfd, threaded_name);
5238 if (sect == NULL)
5239 return false;
5240 sect->_raw_size = note->descsz;
5241 sect->filepos = note->descpos;
5242 sect->flags = SEC_HAS_CONTENTS;
5243 sect->alignment_power = 2;
5244
5245 if (! elfcore_maybe_make_sect (abfd, name, sect))
5246 return false;
5247
5248 return true;
5249 }
5250
5251
5252 /* There isn't a consistent prfpregset_t across platforms,
5253 but it doesn't matter, because we don't have to pick this
5254 data structure apart. */
5255 static boolean
5256 elfcore_grok_prfpreg (abfd, note)
5257 bfd* abfd;
5258 Elf_Internal_Note* note;
5259 {
5260 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5261 }
5262
5263
5264 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5265 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5266 literally. */
5267 static boolean
5268 elfcore_grok_prxfpreg (abfd, note)
5269 bfd* abfd;
5270 Elf_Internal_Note* note;
5271 {
5272 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5273 }
5274
5275
5276 #if defined (HAVE_PRPSINFO_T)
5277 typedef prpsinfo_t elfcore_psinfo_t;
5278 #if defined (__sparcv9) /* Sparc64 cross Sparc32 */
5279 typedef prpsinfo32_t elfcore_psinfo32_t;
5280 #endif
5281 #endif
5282
5283 #if defined (HAVE_PSINFO_T)
5284 typedef psinfo_t elfcore_psinfo_t;
5285 #if defined (__sparcv9) /* Sparc64 cross Sparc32 */
5286 typedef psinfo32_t elfcore_psinfo32_t;
5287 #endif
5288 #endif
5289
5290
5291 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5292
5293 /* return a malloc'ed copy of a string at START which is at
5294 most MAX bytes long, possibly without a terminating '\0'.
5295 the copy will always have a terminating '\0'. */
5296
5297 static char*
5298 elfcore_strndup (abfd, start, max)
5299 bfd* abfd;
5300 char* start;
5301 int max;
5302 {
5303 char* dup;
5304 char* end = memchr (start, '\0', max);
5305 int len;
5306
5307 if (end == NULL)
5308 len = max;
5309 else
5310 len = end - start;
5311
5312 dup = bfd_alloc (abfd, len + 1);
5313 if (dup == NULL)
5314 return NULL;
5315
5316 memcpy (dup, start, len);
5317 dup[len] = '\0';
5318
5319 return dup;
5320 }
5321
5322 static boolean
5323 elfcore_grok_psinfo (abfd, note)
5324 bfd* abfd;
5325 Elf_Internal_Note* note;
5326 {
5327 if (note->descsz == sizeof (elfcore_psinfo_t))
5328 {
5329 elfcore_psinfo_t psinfo;
5330
5331 memcpy (&psinfo, note->descdata, note->descsz);
5332
5333 elf_tdata (abfd)->core_program
5334 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5335
5336 elf_tdata (abfd)->core_command
5337 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5338 }
5339 #if defined (__sparcv9)
5340 else if (note->descsz == sizeof (elfcore_psinfo32_t))
5341 {
5342 /* 64-bit host, 32-bit corefile */
5343 elfcore_psinfo32_t psinfo;
5344
5345 memcpy (&psinfo, note->descdata, note->descsz);
5346
5347 elf_tdata (abfd)->core_program
5348 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5349
5350 elf_tdata (abfd)->core_command
5351 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5352 }
5353 #endif
5354
5355 else
5356 {
5357 /* Fail - we don't know how to handle any other
5358 note size (ie. data object type). */
5359 return true;
5360 }
5361
5362 /* Note that for some reason, a spurious space is tacked
5363 onto the end of the args in some (at least one anyway)
5364 implementations, so strip it off if it exists. */
5365
5366 {
5367 char* command = elf_tdata (abfd)->core_command;
5368 int n = strlen (command);
5369
5370 if (0 < n && command[n - 1] == ' ')
5371 command[n - 1] = '\0';
5372 }
5373
5374 return true;
5375 }
5376 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5377
5378
5379 #if defined (HAVE_PSTATUS_T)
5380 static boolean
5381 elfcore_grok_pstatus (abfd, note)
5382 bfd* abfd;
5383 Elf_Internal_Note* note;
5384 {
5385 if (note->descsz == sizeof (pstatus_t))
5386 {
5387 pstatus_t pstat;
5388
5389 memcpy (&pstat, note->descdata, sizeof (pstat));
5390
5391 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5392 }
5393 #if defined (__sparcv9)
5394 else if (note->descsz == sizeof (pstatus32_t))
5395 {
5396 /* 64-bit host, 32-bit corefile */
5397 pstatus32_t pstat;
5398
5399 memcpy (&pstat, note->descdata, sizeof (pstat));
5400
5401 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5402 }
5403 #endif
5404 /* Could grab some more details from the "representative"
5405 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5406 NT_LWPSTATUS note, presumably. */
5407
5408 return true;
5409 }
5410 #endif /* defined (HAVE_PSTATUS_T) */
5411
5412
5413 #if defined (HAVE_LWPSTATUS_T)
5414 static boolean
5415 elfcore_grok_lwpstatus (abfd, note)
5416 bfd* abfd;
5417 Elf_Internal_Note* note;
5418 {
5419 lwpstatus_t lwpstat;
5420 char buf[100];
5421 char* name;
5422 asection* sect;
5423
5424 if (note->descsz != sizeof (lwpstat))
5425 return true;
5426
5427 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5428
5429 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5430 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5431
5432 /* Make a ".reg/999" section. */
5433
5434 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5435 name = bfd_alloc (abfd, strlen (buf) + 1);
5436 if (name == NULL)
5437 return false;
5438 strcpy (name, buf);
5439
5440 sect = bfd_make_section (abfd, name);
5441 if (sect == NULL)
5442 return false;
5443
5444 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5445 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5446 sect->filepos = note->descpos
5447 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5448 #endif
5449
5450 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5451 sect->_raw_size = sizeof (lwpstat.pr_reg);
5452 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5453 #endif
5454
5455 sect->flags = SEC_HAS_CONTENTS;
5456 sect->alignment_power = 2;
5457
5458 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5459 return false;
5460
5461 /* Make a ".reg2/999" section */
5462
5463 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5464 name = bfd_alloc (abfd, strlen (buf) + 1);
5465 if (name == NULL)
5466 return false;
5467 strcpy (name, buf);
5468
5469 sect = bfd_make_section (abfd, name);
5470 if (sect == NULL)
5471 return false;
5472
5473 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5474 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5475 sect->filepos = note->descpos
5476 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5477 #endif
5478
5479 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5480 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5481 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5482 #endif
5483
5484 sect->flags = SEC_HAS_CONTENTS;
5485 sect->alignment_power = 2;
5486
5487 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5488 return false;
5489
5490 return true;
5491 }
5492 #endif /* defined (HAVE_LWPSTATUS_T) */
5493
5494 #if defined (HAVE_WIN32_PSTATUS_T)
5495 static boolean
5496 elfcore_grok_win32pstatus (abfd, note)
5497 bfd * abfd;
5498 Elf_Internal_Note * note;
5499 {
5500 char buf[30];
5501 char * name;
5502 asection * sect;
5503 win32_pstatus_t pstatus;
5504
5505 if (note->descsz < sizeof (pstatus))
5506 return true;
5507
5508 memcpy (& pstatus, note->descdata, note->descsz);
5509
5510 switch (pstatus.data_type)
5511 {
5512 case NOTE_INFO_PROCESS:
5513 /* FIXME: need to add ->core_command. */
5514 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
5515 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
5516 break ;
5517
5518 case NOTE_INFO_THREAD:
5519 /* Make a ".reg/999" section. */
5520 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
5521
5522 name = bfd_alloc (abfd, strlen (buf) + 1);
5523 if (name == NULL)
5524 return false;
5525
5526 strcpy (name, buf);
5527
5528 sect = bfd_make_section (abfd, name);
5529 if (sect == NULL)
5530 return false;
5531
5532 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
5533 sect->filepos = note->descpos + offsetof (struct win32_pstatus,
5534 data.thread_info.thread_context);
5535 sect->flags = SEC_HAS_CONTENTS;
5536 sect->alignment_power = 2;
5537
5538 if (pstatus.data.thread_info.is_active_thread)
5539 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5540 return false;
5541 break;
5542
5543 case NOTE_INFO_MODULE:
5544 /* Make a ".module/xxxxxxxx" section. */
5545 sprintf (buf, ".module/%08x" , pstatus.data.module_info.base_address);
5546
5547 name = bfd_alloc (abfd, strlen (buf) + 1);
5548 if (name == NULL)
5549 return false;
5550
5551 strcpy (name, buf);
5552
5553 sect = bfd_make_section (abfd, name);
5554
5555 if (sect == NULL)
5556 return false;
5557
5558 sect->_raw_size = note->descsz;
5559 sect->filepos = note->descpos;
5560 sect->flags = SEC_HAS_CONTENTS;
5561 sect->alignment_power = 2;
5562 break;
5563
5564 default:
5565 return true;
5566 }
5567
5568 return true;
5569 }
5570 #endif /* HAVE_WIN32_PSTATUS_T */
5571
5572 static boolean
5573 elfcore_grok_note (abfd, note)
5574 bfd* abfd;
5575 Elf_Internal_Note* note;
5576 {
5577 switch (note->type)
5578 {
5579 default:
5580 return true;
5581
5582 #if defined (HAVE_PRSTATUS_T)
5583 case NT_PRSTATUS:
5584 return elfcore_grok_prstatus (abfd, note);
5585 #endif
5586
5587 #if defined (HAVE_PSTATUS_T)
5588 case NT_PSTATUS:
5589 return elfcore_grok_pstatus (abfd, note);
5590 #endif
5591
5592 #if defined (HAVE_LWPSTATUS_T)
5593 case NT_LWPSTATUS:
5594 return elfcore_grok_lwpstatus (abfd, note);
5595 #endif
5596
5597 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5598 return elfcore_grok_prfpreg (abfd, note);
5599
5600 #if defined (HAVE_WIN32_PSTATUS_T)
5601 case NT_WIN32PSTATUS:
5602 return elfcore_grok_win32pstatus (abfd, note);
5603 #endif
5604
5605 case NT_PRXFPREG: /* Linux SSE extension */
5606 if (note->namesz == 5
5607 && ! strcmp (note->namedata, "LINUX"))
5608 return elfcore_grok_prxfpreg (abfd, note);
5609 else
5610 return true;
5611
5612 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5613 case NT_PRPSINFO:
5614 case NT_PSINFO:
5615 return elfcore_grok_psinfo (abfd, note);
5616 #endif
5617 }
5618 }
5619
5620
5621 static boolean
5622 elfcore_read_notes (abfd, offset, size)
5623 bfd* abfd;
5624 bfd_vma offset;
5625 bfd_vma size;
5626 {
5627 char* buf;
5628 char* p;
5629
5630 if (size <= 0)
5631 return true;
5632
5633 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5634 return false;
5635
5636 buf = bfd_malloc ((size_t) size);
5637 if (buf == NULL)
5638 return false;
5639
5640 if (bfd_read (buf, size, 1, abfd) != size)
5641 {
5642 error:
5643 free (buf);
5644 return false;
5645 }
5646
5647 p = buf;
5648 while (p < buf + size)
5649 {
5650 /* FIXME: bad alignment assumption. */
5651 Elf_External_Note* xnp = (Elf_External_Note*) p;
5652 Elf_Internal_Note in;
5653
5654 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5655
5656 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5657 in.namedata = xnp->name;
5658
5659 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5660 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5661 in.descpos = offset + (in.descdata - buf);
5662
5663 if (! elfcore_grok_note (abfd, &in))
5664 goto error;
5665
5666 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5667 }
5668
5669 free (buf);
5670 return true;
5671 }
5672
5673
5674 /* FIXME: This function is now unnecessary. Callers can just call
5675 bfd_section_from_phdr directly. */
5676
5677 boolean
5678 _bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5679 bfd* abfd;
5680 Elf_Internal_Phdr* phdr;
5681 int sec_num;
5682 {
5683 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5684 return false;
5685
5686 return true;
5687 }
5688
5689
5690 \f
5691 /* Providing external access to the ELF program header table. */
5692
5693 /* Return an upper bound on the number of bytes required to store a
5694 copy of ABFD's program header table entries. Return -1 if an error
5695 occurs; bfd_get_error will return an appropriate code. */
5696 long
5697 bfd_get_elf_phdr_upper_bound (abfd)
5698 bfd *abfd;
5699 {
5700 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5701 {
5702 bfd_set_error (bfd_error_wrong_format);
5703 return -1;
5704 }
5705
5706 return (elf_elfheader (abfd)->e_phnum
5707 * sizeof (Elf_Internal_Phdr));
5708 }
5709
5710
5711 /* Copy ABFD's program header table entries to *PHDRS. The entries
5712 will be stored as an array of Elf_Internal_Phdr structures, as
5713 defined in include/elf/internal.h. To find out how large the
5714 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5715
5716 Return the number of program header table entries read, or -1 if an
5717 error occurs; bfd_get_error will return an appropriate code. */
5718 int
5719 bfd_get_elf_phdrs (abfd, phdrs)
5720 bfd *abfd;
5721 void *phdrs;
5722 {
5723 int num_phdrs;
5724
5725 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5726 {
5727 bfd_set_error (bfd_error_wrong_format);
5728 return -1;
5729 }
5730
5731 num_phdrs = elf_elfheader (abfd)->e_phnum;
5732 memcpy (phdrs, elf_tdata (abfd)->phdr,
5733 num_phdrs * sizeof (Elf_Internal_Phdr));
5734
5735 return num_phdrs;
5736 }
This page took 0.152149 seconds and 5 git commands to generate.