2000-06-18 Ulf Carlsson <ulfc@engr.sgi.com>
[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. */
1337 if (hdr->sh_link != elf_onesymtab (abfd))
1338 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1339
1340 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1341 return false;
1342 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1343 if (target_sect == NULL)
1344 return false;
1345
1346 if ((target_sect->flags & SEC_RELOC) == 0
1347 || target_sect->reloc_count == 0)
1348 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1349 else
1350 {
1351 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1352 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1353 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1354 }
1355 *hdr2 = *hdr;
1356 elf_elfsections (abfd)[shindex] = hdr2;
1357 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1358 target_sect->flags |= SEC_RELOC;
1359 target_sect->relocation = NULL;
1360 target_sect->rel_filepos = hdr->sh_offset;
1361 /* In the section to which the relocations apply, mark whether
1362 its relocations are of the REL or RELA variety. */
1363 if (hdr->sh_size != 0)
1364 elf_section_data (target_sect)->use_rela_p
1365 = (hdr->sh_type == SHT_RELA);
1366 abfd->flags |= HAS_RELOC;
1367 return true;
1368 }
1369 break;
1370
1371 case SHT_GNU_verdef:
1372 elf_dynverdef (abfd) = shindex;
1373 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1374 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1375 break;
1376
1377 case SHT_GNU_versym:
1378 elf_dynversym (abfd) = shindex;
1379 elf_tdata (abfd)->dynversym_hdr = *hdr;
1380 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1381 break;
1382
1383 case SHT_GNU_verneed:
1384 elf_dynverref (abfd) = shindex;
1385 elf_tdata (abfd)->dynverref_hdr = *hdr;
1386 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1387 break;
1388
1389 case SHT_SHLIB:
1390 return true;
1391
1392 default:
1393 /* Check for any processor-specific section types. */
1394 {
1395 if (bed->elf_backend_section_from_shdr)
1396 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1397 }
1398 break;
1399 }
1400
1401 return true;
1402 }
1403
1404 /* Given an ELF section number, retrieve the corresponding BFD
1405 section. */
1406
1407 asection *
1408 bfd_section_from_elf_index (abfd, index)
1409 bfd *abfd;
1410 unsigned int index;
1411 {
1412 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1413 if (index >= elf_elfheader (abfd)->e_shnum)
1414 return NULL;
1415 return elf_elfsections (abfd)[index]->bfd_section;
1416 }
1417
1418 boolean
1419 _bfd_elf_new_section_hook (abfd, sec)
1420 bfd *abfd;
1421 asection *sec;
1422 {
1423 struct bfd_elf_section_data *sdata;
1424
1425 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata));
1426 if (!sdata)
1427 return false;
1428 sec->used_by_bfd = (PTR) sdata;
1429
1430 /* Indicate whether or not this section should use RELA relocations. */
1431 sdata->use_rela_p
1432 = get_elf_backend_data (abfd)->default_use_rela_p;
1433
1434 return true;
1435 }
1436
1437 /* Create a new bfd section from an ELF program header.
1438
1439 Since program segments have no names, we generate a synthetic name
1440 of the form segment<NUM>, where NUM is generally the index in the
1441 program header table. For segments that are split (see below) we
1442 generate the names segment<NUM>a and segment<NUM>b.
1443
1444 Note that some program segments may have a file size that is different than
1445 (less than) the memory size. All this means is that at execution the
1446 system must allocate the amount of memory specified by the memory size,
1447 but only initialize it with the first "file size" bytes read from the
1448 file. This would occur for example, with program segments consisting
1449 of combined data+bss.
1450
1451 To handle the above situation, this routine generates TWO bfd sections
1452 for the single program segment. The first has the length specified by
1453 the file size of the segment, and the second has the length specified
1454 by the difference between the two sizes. In effect, the segment is split
1455 into it's initialized and uninitialized parts.
1456
1457 */
1458
1459 boolean
1460 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1461 bfd *abfd;
1462 Elf_Internal_Phdr *hdr;
1463 int index;
1464 const char *typename;
1465 {
1466 asection *newsect;
1467 char *name;
1468 char namebuf[64];
1469 int split;
1470
1471 split = ((hdr->p_memsz > 0)
1472 && (hdr->p_filesz > 0)
1473 && (hdr->p_memsz > hdr->p_filesz));
1474 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1475 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1476 if (!name)
1477 return false;
1478 strcpy (name, namebuf);
1479 newsect = bfd_make_section (abfd, name);
1480 if (newsect == NULL)
1481 return false;
1482 newsect->vma = hdr->p_vaddr;
1483 newsect->lma = hdr->p_paddr;
1484 newsect->_raw_size = hdr->p_filesz;
1485 newsect->filepos = hdr->p_offset;
1486 newsect->flags |= SEC_HAS_CONTENTS;
1487 if (hdr->p_type == PT_LOAD)
1488 {
1489 newsect->flags |= SEC_ALLOC;
1490 newsect->flags |= SEC_LOAD;
1491 if (hdr->p_flags & PF_X)
1492 {
1493 /* FIXME: all we known is that it has execute PERMISSION,
1494 may be data. */
1495 newsect->flags |= SEC_CODE;
1496 }
1497 }
1498 if (!(hdr->p_flags & PF_W))
1499 {
1500 newsect->flags |= SEC_READONLY;
1501 }
1502
1503 if (split)
1504 {
1505 sprintf (namebuf, "%s%db", typename, index);
1506 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1507 if (!name)
1508 return false;
1509 strcpy (name, namebuf);
1510 newsect = bfd_make_section (abfd, name);
1511 if (newsect == NULL)
1512 return false;
1513 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1514 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1515 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1516 if (hdr->p_type == PT_LOAD)
1517 {
1518 newsect->flags |= SEC_ALLOC;
1519 if (hdr->p_flags & PF_X)
1520 newsect->flags |= SEC_CODE;
1521 }
1522 if (!(hdr->p_flags & PF_W))
1523 newsect->flags |= SEC_READONLY;
1524 }
1525
1526 return true;
1527 }
1528
1529 boolean
1530 bfd_section_from_phdr (abfd, hdr, index)
1531 bfd *abfd;
1532 Elf_Internal_Phdr *hdr;
1533 int index;
1534 {
1535 struct elf_backend_data *bed;
1536
1537 switch (hdr->p_type)
1538 {
1539 case PT_NULL:
1540 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1541
1542 case PT_LOAD:
1543 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1544
1545 case PT_DYNAMIC:
1546 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1547
1548 case PT_INTERP:
1549 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1550
1551 case PT_NOTE:
1552 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1553 return false;
1554 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
1555 return false;
1556 return true;
1557
1558 case PT_SHLIB:
1559 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1560
1561 case PT_PHDR:
1562 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
1563
1564 default:
1565 /* Check for any processor-specific program segment types.
1566 If no handler for them, default to making "segment" sections. */
1567 bed = get_elf_backend_data (abfd);
1568 if (bed->elf_backend_section_from_phdr)
1569 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
1570 else
1571 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
1572 }
1573 }
1574
1575 /* Initialize REL_HDR, the section-header for new section, containing
1576 relocations against ASECT. If USE_RELA_P is true, we use RELA
1577 relocations; otherwise, we use REL relocations. */
1578
1579 boolean
1580 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1581 bfd *abfd;
1582 Elf_Internal_Shdr *rel_hdr;
1583 asection *asect;
1584 boolean use_rela_p;
1585 {
1586 char *name;
1587 struct elf_backend_data *bed;
1588
1589 bed = get_elf_backend_data (abfd);
1590 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1591 if (name == NULL)
1592 return false;
1593 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1594 rel_hdr->sh_name =
1595 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1596 true, false);
1597 if (rel_hdr->sh_name == (unsigned int) -1)
1598 return false;
1599 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1600 rel_hdr->sh_entsize = (use_rela_p
1601 ? bed->s->sizeof_rela
1602 : bed->s->sizeof_rel);
1603 rel_hdr->sh_addralign = bed->s->file_align;
1604 rel_hdr->sh_flags = 0;
1605 rel_hdr->sh_addr = 0;
1606 rel_hdr->sh_size = 0;
1607 rel_hdr->sh_offset = 0;
1608
1609 return true;
1610 }
1611
1612 /* Set up an ELF internal section header for a section. */
1613
1614 /*ARGSUSED*/
1615 static void
1616 elf_fake_sections (abfd, asect, failedptrarg)
1617 bfd *abfd;
1618 asection *asect;
1619 PTR failedptrarg;
1620 {
1621 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1622 boolean *failedptr = (boolean *) failedptrarg;
1623 Elf_Internal_Shdr *this_hdr;
1624
1625 if (*failedptr)
1626 {
1627 /* We already failed; just get out of the bfd_map_over_sections
1628 loop. */
1629 return;
1630 }
1631
1632 this_hdr = &elf_section_data (asect)->this_hdr;
1633
1634 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1635 asect->name,
1636 true, false);
1637 if (this_hdr->sh_name == (unsigned long) -1)
1638 {
1639 *failedptr = true;
1640 return;
1641 }
1642
1643 this_hdr->sh_flags = 0;
1644
1645 if ((asect->flags & SEC_ALLOC) != 0
1646 || asect->user_set_vma)
1647 this_hdr->sh_addr = asect->vma;
1648 else
1649 this_hdr->sh_addr = 0;
1650
1651 this_hdr->sh_offset = 0;
1652 this_hdr->sh_size = asect->_raw_size;
1653 this_hdr->sh_link = 0;
1654 this_hdr->sh_addralign = 1 << asect->alignment_power;
1655 /* The sh_entsize and sh_info fields may have been set already by
1656 copy_private_section_data. */
1657
1658 this_hdr->bfd_section = asect;
1659 this_hdr->contents = NULL;
1660
1661 /* FIXME: This should not be based on section names. */
1662 if (strcmp (asect->name, ".dynstr") == 0)
1663 this_hdr->sh_type = SHT_STRTAB;
1664 else if (strcmp (asect->name, ".hash") == 0)
1665 {
1666 this_hdr->sh_type = SHT_HASH;
1667 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
1668 }
1669 else if (strcmp (asect->name, ".dynsym") == 0)
1670 {
1671 this_hdr->sh_type = SHT_DYNSYM;
1672 this_hdr->sh_entsize = bed->s->sizeof_sym;
1673 }
1674 else if (strcmp (asect->name, ".dynamic") == 0)
1675 {
1676 this_hdr->sh_type = SHT_DYNAMIC;
1677 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1678 }
1679 else if (strncmp (asect->name, ".rela", 5) == 0
1680 && get_elf_backend_data (abfd)->may_use_rela_p)
1681 {
1682 this_hdr->sh_type = SHT_RELA;
1683 this_hdr->sh_entsize = bed->s->sizeof_rela;
1684 }
1685 else if (strncmp (asect->name, ".rel", 4) == 0
1686 && get_elf_backend_data (abfd)->may_use_rel_p)
1687 {
1688 this_hdr->sh_type = SHT_REL;
1689 this_hdr->sh_entsize = bed->s->sizeof_rel;
1690 }
1691 else if (strncmp (asect->name, ".note", 5) == 0)
1692 this_hdr->sh_type = SHT_NOTE;
1693 else if (strncmp (asect->name, ".stab", 5) == 0
1694 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1695 this_hdr->sh_type = SHT_STRTAB;
1696 else if (strcmp (asect->name, ".gnu.version") == 0)
1697 {
1698 this_hdr->sh_type = SHT_GNU_versym;
1699 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1700 }
1701 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1702 {
1703 this_hdr->sh_type = SHT_GNU_verdef;
1704 this_hdr->sh_entsize = 0;
1705 /* objcopy or strip will copy over sh_info, but may not set
1706 cverdefs. The linker will set cverdefs, but sh_info will be
1707 zero. */
1708 if (this_hdr->sh_info == 0)
1709 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1710 else
1711 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1712 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1713 }
1714 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1715 {
1716 this_hdr->sh_type = SHT_GNU_verneed;
1717 this_hdr->sh_entsize = 0;
1718 /* objcopy or strip will copy over sh_info, but may not set
1719 cverrefs. The linker will set cverrefs, but sh_info will be
1720 zero. */
1721 if (this_hdr->sh_info == 0)
1722 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1723 else
1724 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1725 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1726 }
1727 else if ((asect->flags & SEC_ALLOC) != 0
1728 && (asect->flags & SEC_LOAD) != 0)
1729 this_hdr->sh_type = SHT_PROGBITS;
1730 else if ((asect->flags & SEC_ALLOC) != 0
1731 && ((asect->flags & SEC_LOAD) == 0))
1732 this_hdr->sh_type = SHT_NOBITS;
1733 else
1734 {
1735 /* Who knows? */
1736 this_hdr->sh_type = SHT_PROGBITS;
1737 }
1738
1739 if ((asect->flags & SEC_ALLOC) != 0)
1740 this_hdr->sh_flags |= SHF_ALLOC;
1741 if ((asect->flags & SEC_READONLY) == 0)
1742 this_hdr->sh_flags |= SHF_WRITE;
1743 if ((asect->flags & SEC_CODE) != 0)
1744 this_hdr->sh_flags |= SHF_EXECINSTR;
1745
1746 /* Check for processor-specific section types. */
1747 if (bed->elf_backend_fake_sections)
1748 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1749
1750 /* If the section has relocs, set up a section header for the
1751 SHT_REL[A] section. If two relocation sections are required for
1752 this section, it is up to the processor-specific back-end to
1753 create the other. */
1754 if ((asect->flags & SEC_RELOC) != 0
1755 && !_bfd_elf_init_reloc_shdr (abfd,
1756 &elf_section_data (asect)->rel_hdr,
1757 asect,
1758 elf_section_data (asect)->use_rela_p))
1759 *failedptr = true;
1760 }
1761
1762 /* Get elf arch size (32 / 64).
1763 Returns -1 if not elf. */
1764
1765 int
1766 bfd_elf_get_arch_size (abfd)
1767 bfd *abfd;
1768 {
1769 if (abfd->xvec->flavour != bfd_target_elf_flavour)
1770 {
1771 bfd_set_error (bfd_error_wrong_format);
1772 return -1;
1773 }
1774
1775 return (get_elf_backend_data (abfd))->s->arch_size;
1776 }
1777
1778 /* Assign all ELF section numbers. The dummy first section is handled here
1779 too. The link/info pointers for the standard section types are filled
1780 in here too, while we're at it. */
1781
1782 static boolean
1783 assign_section_numbers (abfd)
1784 bfd *abfd;
1785 {
1786 struct elf_obj_tdata *t = elf_tdata (abfd);
1787 asection *sec;
1788 unsigned int section_number;
1789 Elf_Internal_Shdr **i_shdrp;
1790
1791 section_number = 1;
1792
1793 for (sec = abfd->sections; sec; sec = sec->next)
1794 {
1795 struct bfd_elf_section_data *d = elf_section_data (sec);
1796
1797 d->this_idx = section_number++;
1798 if ((sec->flags & SEC_RELOC) == 0)
1799 d->rel_idx = 0;
1800 else
1801 d->rel_idx = section_number++;
1802
1803 if (d->rel_hdr2)
1804 d->rel_idx2 = section_number++;
1805 else
1806 d->rel_idx2 = 0;
1807 }
1808
1809 t->shstrtab_section = section_number++;
1810 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1811 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1812
1813 if (bfd_get_symcount (abfd) > 0)
1814 {
1815 t->symtab_section = section_number++;
1816 t->strtab_section = section_number++;
1817 }
1818
1819 elf_elfheader (abfd)->e_shnum = section_number;
1820
1821 /* Set up the list of section header pointers, in agreement with the
1822 indices. */
1823 i_shdrp = ((Elf_Internal_Shdr **)
1824 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1825 if (i_shdrp == NULL)
1826 return false;
1827
1828 i_shdrp[0] = ((Elf_Internal_Shdr *)
1829 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1830 if (i_shdrp[0] == NULL)
1831 {
1832 bfd_release (abfd, i_shdrp);
1833 return false;
1834 }
1835 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1836
1837 elf_elfsections (abfd) = i_shdrp;
1838
1839 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1840 if (bfd_get_symcount (abfd) > 0)
1841 {
1842 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1843 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1844 t->symtab_hdr.sh_link = t->strtab_section;
1845 }
1846 for (sec = abfd->sections; sec; sec = sec->next)
1847 {
1848 struct bfd_elf_section_data *d = elf_section_data (sec);
1849 asection *s;
1850 const char *name;
1851
1852 i_shdrp[d->this_idx] = &d->this_hdr;
1853 if (d->rel_idx != 0)
1854 i_shdrp[d->rel_idx] = &d->rel_hdr;
1855 if (d->rel_idx2 != 0)
1856 i_shdrp[d->rel_idx2] = d->rel_hdr2;
1857
1858 /* Fill in the sh_link and sh_info fields while we're at it. */
1859
1860 /* sh_link of a reloc section is the section index of the symbol
1861 table. sh_info is the section index of the section to which
1862 the relocation entries apply. */
1863 if (d->rel_idx != 0)
1864 {
1865 d->rel_hdr.sh_link = t->symtab_section;
1866 d->rel_hdr.sh_info = d->this_idx;
1867 }
1868 if (d->rel_idx2 != 0)
1869 {
1870 d->rel_hdr2->sh_link = t->symtab_section;
1871 d->rel_hdr2->sh_info = d->this_idx;
1872 }
1873
1874 switch (d->this_hdr.sh_type)
1875 {
1876 case SHT_REL:
1877 case SHT_RELA:
1878 /* A reloc section which we are treating as a normal BFD
1879 section. sh_link is the section index of the symbol
1880 table. sh_info is the section index of the section to
1881 which the relocation entries apply. We assume that an
1882 allocated reloc section uses the dynamic symbol table.
1883 FIXME: How can we be sure? */
1884 s = bfd_get_section_by_name (abfd, ".dynsym");
1885 if (s != NULL)
1886 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1887
1888 /* We look up the section the relocs apply to by name. */
1889 name = sec->name;
1890 if (d->this_hdr.sh_type == SHT_REL)
1891 name += 4;
1892 else
1893 name += 5;
1894 s = bfd_get_section_by_name (abfd, name);
1895 if (s != NULL)
1896 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1897 break;
1898
1899 case SHT_STRTAB:
1900 /* We assume that a section named .stab*str is a stabs
1901 string section. We look for a section with the same name
1902 but without the trailing ``str'', and set its sh_link
1903 field to point to this section. */
1904 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1905 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1906 {
1907 size_t len;
1908 char *alc;
1909
1910 len = strlen (sec->name);
1911 alc = (char *) bfd_malloc (len - 2);
1912 if (alc == NULL)
1913 return false;
1914 strncpy (alc, sec->name, len - 3);
1915 alc[len - 3] = '\0';
1916 s = bfd_get_section_by_name (abfd, alc);
1917 free (alc);
1918 if (s != NULL)
1919 {
1920 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1921
1922 /* This is a .stab section. */
1923 elf_section_data (s)->this_hdr.sh_entsize =
1924 4 + 2 * bfd_elf_get_arch_size (abfd) / 8;
1925 }
1926 }
1927 break;
1928
1929 case SHT_DYNAMIC:
1930 case SHT_DYNSYM:
1931 case SHT_GNU_verneed:
1932 case SHT_GNU_verdef:
1933 /* sh_link is the section header index of the string table
1934 used for the dynamic entries, or the symbol table, or the
1935 version strings. */
1936 s = bfd_get_section_by_name (abfd, ".dynstr");
1937 if (s != NULL)
1938 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1939 break;
1940
1941 case SHT_HASH:
1942 case SHT_GNU_versym:
1943 /* sh_link is the section header index of the symbol table
1944 this hash table or version table is for. */
1945 s = bfd_get_section_by_name (abfd, ".dynsym");
1946 if (s != NULL)
1947 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1948 break;
1949 }
1950 }
1951
1952 return true;
1953 }
1954
1955 /* Map symbol from it's internal number to the external number, moving
1956 all local symbols to be at the head of the list. */
1957
1958 static INLINE int
1959 sym_is_global (abfd, sym)
1960 bfd *abfd;
1961 asymbol *sym;
1962 {
1963 /* If the backend has a special mapping, use it. */
1964 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1965 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1966 (abfd, sym));
1967
1968 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1969 || bfd_is_und_section (bfd_get_section (sym))
1970 || bfd_is_com_section (bfd_get_section (sym)));
1971 }
1972
1973 static boolean
1974 elf_map_symbols (abfd)
1975 bfd *abfd;
1976 {
1977 int symcount = bfd_get_symcount (abfd);
1978 asymbol **syms = bfd_get_outsymbols (abfd);
1979 asymbol **sect_syms;
1980 int num_locals = 0;
1981 int num_globals = 0;
1982 int num_locals2 = 0;
1983 int num_globals2 = 0;
1984 int max_index = 0;
1985 int num_sections = 0;
1986 int idx;
1987 asection *asect;
1988 asymbol **new_syms;
1989 asymbol *sym;
1990
1991 #ifdef DEBUG
1992 fprintf (stderr, "elf_map_symbols\n");
1993 fflush (stderr);
1994 #endif
1995
1996 /* Add a section symbol for each BFD section. FIXME: Is this really
1997 necessary? */
1998 for (asect = abfd->sections; asect; asect = asect->next)
1999 {
2000 if (max_index < asect->index)
2001 max_index = asect->index;
2002 }
2003
2004 max_index++;
2005 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
2006 if (sect_syms == NULL)
2007 return false;
2008 elf_section_syms (abfd) = sect_syms;
2009
2010 for (idx = 0; idx < symcount; idx++)
2011 {
2012 sym = syms[idx];
2013
2014 if ((sym->flags & BSF_SECTION_SYM) != 0
2015 && sym->value == 0)
2016 {
2017 asection *sec;
2018
2019 sec = sym->section;
2020
2021 if (sec->owner != NULL)
2022 {
2023 if (sec->owner != abfd)
2024 {
2025 if (sec->output_offset != 0)
2026 continue;
2027
2028 sec = sec->output_section;
2029
2030 /* Empty sections in the input files may have had a section
2031 symbol created for them. (See the comment near the end of
2032 _bfd_generic_link_output_symbols in linker.c). If the linker
2033 script discards such sections then we will reach this point.
2034 Since we know that we cannot avoid this case, we detect it
2035 and skip the abort and the assignment to the sect_syms array.
2036 To reproduce this particular case try running the linker
2037 testsuite test ld-scripts/weak.exp for an ELF port that uses
2038 the generic linker. */
2039 if (sec->owner == NULL)
2040 continue;
2041
2042 BFD_ASSERT (sec->owner == abfd);
2043 }
2044 sect_syms[sec->index] = syms[idx];
2045 }
2046 }
2047 }
2048
2049 for (asect = abfd->sections; asect; asect = asect->next)
2050 {
2051 if (sect_syms[asect->index] != NULL)
2052 continue;
2053
2054 sym = bfd_make_empty_symbol (abfd);
2055 if (sym == NULL)
2056 return false;
2057 sym->the_bfd = abfd;
2058 sym->name = asect->name;
2059 sym->value = 0;
2060 /* Set the flags to 0 to indicate that this one was newly added. */
2061 sym->flags = 0;
2062 sym->section = asect;
2063 sect_syms[asect->index] = sym;
2064 num_sections++;
2065 #ifdef DEBUG
2066 fprintf (stderr,
2067 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2068 asect->name, (long) asect->vma, asect->index, (long) asect);
2069 #endif
2070 }
2071
2072 /* Classify all of the symbols. */
2073 for (idx = 0; idx < symcount; idx++)
2074 {
2075 if (!sym_is_global (abfd, syms[idx]))
2076 num_locals++;
2077 else
2078 num_globals++;
2079 }
2080 for (asect = abfd->sections; asect; asect = asect->next)
2081 {
2082 if (sect_syms[asect->index] != NULL
2083 && sect_syms[asect->index]->flags == 0)
2084 {
2085 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
2086 if (!sym_is_global (abfd, sect_syms[asect->index]))
2087 num_locals++;
2088 else
2089 num_globals++;
2090 sect_syms[asect->index]->flags = 0;
2091 }
2092 }
2093
2094 /* Now sort the symbols so the local symbols are first. */
2095 new_syms = ((asymbol **)
2096 bfd_alloc (abfd,
2097 (num_locals + num_globals) * sizeof (asymbol *)));
2098 if (new_syms == NULL)
2099 return false;
2100
2101 for (idx = 0; idx < symcount; idx++)
2102 {
2103 asymbol *sym = syms[idx];
2104 int i;
2105
2106 if (!sym_is_global (abfd, sym))
2107 i = num_locals2++;
2108 else
2109 i = num_locals + num_globals2++;
2110 new_syms[i] = sym;
2111 sym->udata.i = i + 1;
2112 }
2113 for (asect = abfd->sections; asect; asect = asect->next)
2114 {
2115 if (sect_syms[asect->index] != NULL
2116 && sect_syms[asect->index]->flags == 0)
2117 {
2118 asymbol *sym = sect_syms[asect->index];
2119 int i;
2120
2121 sym->flags = BSF_SECTION_SYM;
2122 if (!sym_is_global (abfd, sym))
2123 i = num_locals2++;
2124 else
2125 i = num_locals + num_globals2++;
2126 new_syms[i] = sym;
2127 sym->udata.i = i + 1;
2128 }
2129 }
2130
2131 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2132
2133 elf_num_locals (abfd) = num_locals;
2134 elf_num_globals (abfd) = num_globals;
2135 return true;
2136 }
2137
2138 /* Align to the maximum file alignment that could be required for any
2139 ELF data structure. */
2140
2141 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2142 static INLINE file_ptr
2143 align_file_position (off, align)
2144 file_ptr off;
2145 int align;
2146 {
2147 return (off + align - 1) & ~(align - 1);
2148 }
2149
2150 /* Assign a file position to a section, optionally aligning to the
2151 required section alignment. */
2152
2153 INLINE file_ptr
2154 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2155 Elf_Internal_Shdr *i_shdrp;
2156 file_ptr offset;
2157 boolean align;
2158 {
2159 if (align)
2160 {
2161 unsigned int al;
2162
2163 al = i_shdrp->sh_addralign;
2164 if (al > 1)
2165 offset = BFD_ALIGN (offset, al);
2166 }
2167 i_shdrp->sh_offset = offset;
2168 if (i_shdrp->bfd_section != NULL)
2169 i_shdrp->bfd_section->filepos = offset;
2170 if (i_shdrp->sh_type != SHT_NOBITS)
2171 offset += i_shdrp->sh_size;
2172 return offset;
2173 }
2174
2175 /* Compute the file positions we are going to put the sections at, and
2176 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2177 is not NULL, this is being called by the ELF backend linker. */
2178
2179 boolean
2180 _bfd_elf_compute_section_file_positions (abfd, link_info)
2181 bfd *abfd;
2182 struct bfd_link_info *link_info;
2183 {
2184 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2185 boolean failed;
2186 struct bfd_strtab_hash *strtab;
2187 Elf_Internal_Shdr *shstrtab_hdr;
2188
2189 if (abfd->output_has_begun)
2190 return true;
2191
2192 /* Do any elf backend specific processing first. */
2193 if (bed->elf_backend_begin_write_processing)
2194 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2195
2196 if (! prep_headers (abfd))
2197 return false;
2198
2199 /* Post process the headers if necessary. */
2200 if (bed->elf_backend_post_process_headers)
2201 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2202
2203 failed = false;
2204 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2205 if (failed)
2206 return false;
2207
2208 if (!assign_section_numbers (abfd))
2209 return false;
2210
2211 /* The backend linker builds symbol table information itself. */
2212 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2213 {
2214 /* Non-zero if doing a relocatable link. */
2215 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2216
2217 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2218 return false;
2219 }
2220
2221 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2222 /* sh_name was set in prep_headers. */
2223 shstrtab_hdr->sh_type = SHT_STRTAB;
2224 shstrtab_hdr->sh_flags = 0;
2225 shstrtab_hdr->sh_addr = 0;
2226 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2227 shstrtab_hdr->sh_entsize = 0;
2228 shstrtab_hdr->sh_link = 0;
2229 shstrtab_hdr->sh_info = 0;
2230 /* sh_offset is set in assign_file_positions_except_relocs. */
2231 shstrtab_hdr->sh_addralign = 1;
2232
2233 if (!assign_file_positions_except_relocs (abfd))
2234 return false;
2235
2236 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2237 {
2238 file_ptr off;
2239 Elf_Internal_Shdr *hdr;
2240
2241 off = elf_tdata (abfd)->next_file_pos;
2242
2243 hdr = &elf_tdata (abfd)->symtab_hdr;
2244 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2245
2246 hdr = &elf_tdata (abfd)->strtab_hdr;
2247 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2248
2249 elf_tdata (abfd)->next_file_pos = off;
2250
2251 /* Now that we know where the .strtab section goes, write it
2252 out. */
2253 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2254 || ! _bfd_stringtab_emit (abfd, strtab))
2255 return false;
2256 _bfd_stringtab_free (strtab);
2257 }
2258
2259 abfd->output_has_begun = true;
2260
2261 return true;
2262 }
2263
2264 /* Create a mapping from a set of sections to a program segment. */
2265
2266 static INLINE struct elf_segment_map *
2267 make_mapping (abfd, sections, from, to, phdr)
2268 bfd *abfd;
2269 asection **sections;
2270 unsigned int from;
2271 unsigned int to;
2272 boolean phdr;
2273 {
2274 struct elf_segment_map *m;
2275 unsigned int i;
2276 asection **hdrpp;
2277
2278 m = ((struct elf_segment_map *)
2279 bfd_zalloc (abfd,
2280 (sizeof (struct elf_segment_map)
2281 + (to - from - 1) * sizeof (asection *))));
2282 if (m == NULL)
2283 return NULL;
2284 m->next = NULL;
2285 m->p_type = PT_LOAD;
2286 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2287 m->sections[i - from] = *hdrpp;
2288 m->count = to - from;
2289
2290 if (from == 0 && phdr)
2291 {
2292 /* Include the headers in the first PT_LOAD segment. */
2293 m->includes_filehdr = 1;
2294 m->includes_phdrs = 1;
2295 }
2296
2297 return m;
2298 }
2299
2300 /* Set up a mapping from BFD sections to program segments. */
2301
2302 static boolean
2303 map_sections_to_segments (abfd)
2304 bfd *abfd;
2305 {
2306 asection **sections = NULL;
2307 asection *s;
2308 unsigned int i;
2309 unsigned int count;
2310 struct elf_segment_map *mfirst;
2311 struct elf_segment_map **pm;
2312 struct elf_segment_map *m;
2313 asection *last_hdr;
2314 unsigned int phdr_index;
2315 bfd_vma maxpagesize;
2316 asection **hdrpp;
2317 boolean phdr_in_segment = true;
2318 boolean writable;
2319 asection *dynsec;
2320
2321 if (elf_tdata (abfd)->segment_map != NULL)
2322 return true;
2323
2324 if (bfd_count_sections (abfd) == 0)
2325 return true;
2326
2327 /* Select the allocated sections, and sort them. */
2328
2329 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2330 * sizeof (asection *));
2331 if (sections == NULL)
2332 goto error_return;
2333
2334 i = 0;
2335 for (s = abfd->sections; s != NULL; s = s->next)
2336 {
2337 if ((s->flags & SEC_ALLOC) != 0)
2338 {
2339 sections[i] = s;
2340 ++i;
2341 }
2342 }
2343 BFD_ASSERT (i <= bfd_count_sections (abfd));
2344 count = i;
2345
2346 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2347
2348 /* Build the mapping. */
2349
2350 mfirst = NULL;
2351 pm = &mfirst;
2352
2353 /* If we have a .interp section, then create a PT_PHDR segment for
2354 the program headers and a PT_INTERP segment for the .interp
2355 section. */
2356 s = bfd_get_section_by_name (abfd, ".interp");
2357 if (s != NULL && (s->flags & SEC_LOAD) != 0)
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_PHDR;
2365 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2366 m->p_flags = PF_R | PF_X;
2367 m->p_flags_valid = 1;
2368 m->includes_phdrs = 1;
2369
2370 *pm = m;
2371 pm = &m->next;
2372
2373 m = ((struct elf_segment_map *)
2374 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2375 if (m == NULL)
2376 goto error_return;
2377 m->next = NULL;
2378 m->p_type = PT_INTERP;
2379 m->count = 1;
2380 m->sections[0] = s;
2381
2382 *pm = m;
2383 pm = &m->next;
2384 }
2385
2386 /* Look through the sections. We put sections in the same program
2387 segment when the start of the second section can be placed within
2388 a few bytes of the end of the first section. */
2389 last_hdr = NULL;
2390 phdr_index = 0;
2391 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2392 writable = false;
2393 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2394 if (dynsec != NULL
2395 && (dynsec->flags & SEC_LOAD) == 0)
2396 dynsec = NULL;
2397
2398 /* Deal with -Ttext or something similar such that the first section
2399 is not adjacent to the program headers. This is an
2400 approximation, since at this point we don't know exactly how many
2401 program headers we will need. */
2402 if (count > 0)
2403 {
2404 bfd_size_type phdr_size;
2405
2406 phdr_size = elf_tdata (abfd)->program_header_size;
2407 if (phdr_size == 0)
2408 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2409 if ((abfd->flags & D_PAGED) == 0
2410 || sections[0]->lma < phdr_size
2411 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2412 phdr_in_segment = false;
2413 }
2414
2415 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2416 {
2417 asection *hdr;
2418 boolean new_segment;
2419
2420 hdr = *hdrpp;
2421
2422 /* See if this section and the last one will fit in the same
2423 segment. */
2424
2425 if (last_hdr == NULL)
2426 {
2427 /* If we don't have a segment yet, then we don't need a new
2428 one (we build the last one after this loop). */
2429 new_segment = false;
2430 }
2431 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2432 {
2433 /* If this section has a different relation between the
2434 virtual address and the load address, then we need a new
2435 segment. */
2436 new_segment = true;
2437 }
2438 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2439 < BFD_ALIGN (hdr->lma, maxpagesize))
2440 {
2441 /* If putting this section in this segment would force us to
2442 skip a page in the segment, then we need a new segment. */
2443 new_segment = true;
2444 }
2445 else if ((last_hdr->flags & SEC_LOAD) == 0
2446 && (hdr->flags & SEC_LOAD) != 0)
2447 {
2448 /* We don't want to put a loadable section after a
2449 nonloadable section in the same segment. */
2450 new_segment = true;
2451 }
2452 else if ((abfd->flags & D_PAGED) == 0)
2453 {
2454 /* If the file is not demand paged, which means that we
2455 don't require the sections to be correctly aligned in the
2456 file, then there is no other reason for a new segment. */
2457 new_segment = false;
2458 }
2459 else if (! writable
2460 && (hdr->flags & SEC_READONLY) == 0
2461 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2462 == hdr->lma))
2463 {
2464 /* We don't want to put a writable section in a read only
2465 segment, unless they are on the same page in memory
2466 anyhow. We already know that the last section does not
2467 bring us past the current section on the page, so the
2468 only case in which the new section is not on the same
2469 page as the previous section is when the previous section
2470 ends precisely on a page boundary. */
2471 new_segment = true;
2472 }
2473 else
2474 {
2475 /* Otherwise, we can use the same segment. */
2476 new_segment = false;
2477 }
2478
2479 if (! new_segment)
2480 {
2481 if ((hdr->flags & SEC_READONLY) == 0)
2482 writable = true;
2483 last_hdr = hdr;
2484 continue;
2485 }
2486
2487 /* We need a new program segment. We must create a new program
2488 header holding all the sections from phdr_index until hdr. */
2489
2490 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2491 if (m == NULL)
2492 goto error_return;
2493
2494 *pm = m;
2495 pm = &m->next;
2496
2497 if ((hdr->flags & SEC_READONLY) == 0)
2498 writable = true;
2499 else
2500 writable = false;
2501
2502 last_hdr = hdr;
2503 phdr_index = i;
2504 phdr_in_segment = false;
2505 }
2506
2507 /* Create a final PT_LOAD program segment. */
2508 if (last_hdr != NULL)
2509 {
2510 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2511 if (m == NULL)
2512 goto error_return;
2513
2514 *pm = m;
2515 pm = &m->next;
2516 }
2517
2518 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2519 if (dynsec != NULL)
2520 {
2521 m = ((struct elf_segment_map *)
2522 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2523 if (m == NULL)
2524 goto error_return;
2525 m->next = NULL;
2526 m->p_type = PT_DYNAMIC;
2527 m->count = 1;
2528 m->sections[0] = dynsec;
2529
2530 *pm = m;
2531 pm = &m->next;
2532 }
2533
2534 /* For each loadable .note section, add a PT_NOTE segment. We don't
2535 use bfd_get_section_by_name, because if we link together
2536 nonloadable .note sections and loadable .note sections, we will
2537 generate two .note sections in the output file. FIXME: Using
2538 names for section types is bogus anyhow. */
2539 for (s = abfd->sections; s != NULL; s = s->next)
2540 {
2541 if ((s->flags & SEC_LOAD) != 0
2542 && strncmp (s->name, ".note", 5) == 0)
2543 {
2544 m = ((struct elf_segment_map *)
2545 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2546 if (m == NULL)
2547 goto error_return;
2548 m->next = NULL;
2549 m->p_type = PT_NOTE;
2550 m->count = 1;
2551 m->sections[0] = s;
2552
2553 *pm = m;
2554 pm = &m->next;
2555 }
2556 }
2557
2558 free (sections);
2559 sections = NULL;
2560
2561 elf_tdata (abfd)->segment_map = mfirst;
2562 return true;
2563
2564 error_return:
2565 if (sections != NULL)
2566 free (sections);
2567 return false;
2568 }
2569
2570 /* Sort sections by address. */
2571
2572 static int
2573 elf_sort_sections (arg1, arg2)
2574 const PTR arg1;
2575 const PTR arg2;
2576 {
2577 const asection *sec1 = *(const asection **) arg1;
2578 const asection *sec2 = *(const asection **) arg2;
2579
2580 /* Sort by LMA first, since this is the address used to
2581 place the section into a segment. */
2582 if (sec1->lma < sec2->lma)
2583 return -1;
2584 else if (sec1->lma > sec2->lma)
2585 return 1;
2586
2587 /* Then sort by VMA. Normally the LMA and the VMA will be
2588 the same, and this will do nothing. */
2589 if (sec1->vma < sec2->vma)
2590 return -1;
2591 else if (sec1->vma > sec2->vma)
2592 return 1;
2593
2594 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2595
2596 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2597
2598 if (TOEND (sec1))
2599 {
2600 if (TOEND (sec2))
2601 return sec1->target_index - sec2->target_index;
2602 else
2603 return 1;
2604 }
2605
2606 if (TOEND (sec2))
2607 return -1;
2608
2609 #undef TOEND
2610
2611 /* Sort by size, to put zero sized sections before others at the
2612 same address. */
2613
2614 if (sec1->_raw_size < sec2->_raw_size)
2615 return -1;
2616 if (sec1->_raw_size > sec2->_raw_size)
2617 return 1;
2618
2619 return sec1->target_index - sec2->target_index;
2620 }
2621
2622 /* Assign file positions to the sections based on the mapping from
2623 sections to segments. This function also sets up some fields in
2624 the file header, and writes out the program headers. */
2625
2626 static boolean
2627 assign_file_positions_for_segments (abfd)
2628 bfd *abfd;
2629 {
2630 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2631 unsigned int count;
2632 struct elf_segment_map *m;
2633 unsigned int alloc;
2634 Elf_Internal_Phdr *phdrs;
2635 file_ptr off, voff;
2636 bfd_vma filehdr_vaddr, filehdr_paddr;
2637 bfd_vma phdrs_vaddr, phdrs_paddr;
2638 Elf_Internal_Phdr *p;
2639
2640 if (elf_tdata (abfd)->segment_map == NULL)
2641 {
2642 if (! map_sections_to_segments (abfd))
2643 return false;
2644 }
2645
2646 if (bed->elf_backend_modify_segment_map)
2647 {
2648 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2649 return false;
2650 }
2651
2652 count = 0;
2653 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2654 ++count;
2655
2656 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2657 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2658 elf_elfheader (abfd)->e_phnum = count;
2659
2660 if (count == 0)
2661 return true;
2662
2663 /* If we already counted the number of program segments, make sure
2664 that we allocated enough space. This happens when SIZEOF_HEADERS
2665 is used in a linker script. */
2666 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2667 if (alloc != 0 && count > alloc)
2668 {
2669 ((*_bfd_error_handler)
2670 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2671 bfd_get_filename (abfd), alloc, count));
2672 bfd_set_error (bfd_error_bad_value);
2673 return false;
2674 }
2675
2676 if (alloc == 0)
2677 alloc = count;
2678
2679 phdrs = ((Elf_Internal_Phdr *)
2680 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2681 if (phdrs == NULL)
2682 return false;
2683
2684 off = bed->s->sizeof_ehdr;
2685 off += alloc * bed->s->sizeof_phdr;
2686
2687 filehdr_vaddr = 0;
2688 filehdr_paddr = 0;
2689 phdrs_vaddr = 0;
2690 phdrs_paddr = 0;
2691
2692 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2693 m != NULL;
2694 m = m->next, p++)
2695 {
2696 unsigned int i;
2697 asection **secpp;
2698
2699 /* If elf_segment_map is not from map_sections_to_segments, the
2700 sections may not be correctly ordered. */
2701 if (m->count > 0)
2702 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2703 elf_sort_sections);
2704
2705 p->p_type = m->p_type;
2706 p->p_flags = m->p_flags;
2707
2708 if (p->p_type == PT_LOAD
2709 && m->count > 0
2710 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2711 {
2712 if ((abfd->flags & D_PAGED) != 0)
2713 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2714 else
2715 {
2716 bfd_size_type align;
2717
2718 align = 0;
2719 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2720 {
2721 bfd_size_type secalign;
2722
2723 secalign = bfd_get_section_alignment (abfd, *secpp);
2724 if (secalign > align)
2725 align = secalign;
2726 }
2727
2728 off += (m->sections[0]->vma - off) % (1 << align);
2729 }
2730 }
2731
2732 if (m->count == 0)
2733 p->p_vaddr = 0;
2734 else
2735 p->p_vaddr = m->sections[0]->vma;
2736
2737 if (m->p_paddr_valid)
2738 p->p_paddr = m->p_paddr;
2739 else if (m->count == 0)
2740 p->p_paddr = 0;
2741 else
2742 p->p_paddr = m->sections[0]->lma;
2743
2744 if (p->p_type == PT_LOAD
2745 && (abfd->flags & D_PAGED) != 0)
2746 p->p_align = bed->maxpagesize;
2747 else if (m->count == 0)
2748 p->p_align = bed->s->file_align;
2749 else
2750 p->p_align = 0;
2751
2752 p->p_offset = 0;
2753 p->p_filesz = 0;
2754 p->p_memsz = 0;
2755
2756 if (m->includes_filehdr)
2757 {
2758 if (! m->p_flags_valid)
2759 p->p_flags |= PF_R;
2760 p->p_offset = 0;
2761 p->p_filesz = bed->s->sizeof_ehdr;
2762 p->p_memsz = bed->s->sizeof_ehdr;
2763 if (m->count > 0)
2764 {
2765 BFD_ASSERT (p->p_type == PT_LOAD);
2766
2767 if (p->p_vaddr < (bfd_vma) off)
2768 {
2769 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2770 bfd_get_filename (abfd));
2771 bfd_set_error (bfd_error_bad_value);
2772 return false;
2773 }
2774
2775 p->p_vaddr -= off;
2776 if (! m->p_paddr_valid)
2777 p->p_paddr -= off;
2778 }
2779 if (p->p_type == PT_LOAD)
2780 {
2781 filehdr_vaddr = p->p_vaddr;
2782 filehdr_paddr = p->p_paddr;
2783 }
2784 }
2785
2786 if (m->includes_phdrs)
2787 {
2788 if (! m->p_flags_valid)
2789 p->p_flags |= PF_R;
2790
2791 if (m->includes_filehdr)
2792 {
2793 if (p->p_type == PT_LOAD)
2794 {
2795 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2796 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2797 }
2798 }
2799 else
2800 {
2801 p->p_offset = bed->s->sizeof_ehdr;
2802
2803 if (m->count > 0)
2804 {
2805 BFD_ASSERT (p->p_type == PT_LOAD);
2806 p->p_vaddr -= off - p->p_offset;
2807 if (! m->p_paddr_valid)
2808 p->p_paddr -= off - p->p_offset;
2809 }
2810
2811 if (p->p_type == PT_LOAD)
2812 {
2813 phdrs_vaddr = p->p_vaddr;
2814 phdrs_paddr = p->p_paddr;
2815 }
2816 else
2817 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2818 }
2819
2820 p->p_filesz += alloc * bed->s->sizeof_phdr;
2821 p->p_memsz += alloc * bed->s->sizeof_phdr;
2822 }
2823
2824 if (p->p_type == PT_LOAD
2825 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2826 {
2827 if (! m->includes_filehdr && ! m->includes_phdrs)
2828 p->p_offset = off;
2829 else
2830 {
2831 file_ptr adjust;
2832
2833 adjust = off - (p->p_offset + p->p_filesz);
2834 p->p_filesz += adjust;
2835 p->p_memsz += adjust;
2836 }
2837 }
2838
2839 voff = off;
2840
2841 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2842 {
2843 asection *sec;
2844 flagword flags;
2845 bfd_size_type align;
2846
2847 sec = *secpp;
2848 flags = sec->flags;
2849 align = 1 << bfd_get_section_alignment (abfd, sec);
2850
2851 /* The section may have artificial alignment forced by a
2852 link script. Notice this case by the gap between the
2853 cumulative phdr vma and the section's vma. */
2854 if (p->p_vaddr + p->p_memsz < sec->vma)
2855 {
2856 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2857
2858 p->p_memsz += adjust;
2859 off += adjust;
2860 voff += adjust;
2861 if ((flags & SEC_LOAD) != 0)
2862 p->p_filesz += adjust;
2863 }
2864
2865 if (p->p_type == PT_LOAD)
2866 {
2867 bfd_signed_vma adjust;
2868
2869 if ((flags & SEC_LOAD) != 0)
2870 {
2871 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2872 if (adjust < 0)
2873 adjust = 0;
2874 }
2875 else if ((flags & SEC_ALLOC) != 0)
2876 {
2877 /* The section VMA must equal the file position
2878 modulo the page size. FIXME: I'm not sure if
2879 this adjustment is really necessary. We used to
2880 not have the SEC_LOAD case just above, and then
2881 this was necessary, but now I'm not sure. */
2882 if ((abfd->flags & D_PAGED) != 0)
2883 adjust = (sec->vma - voff) % bed->maxpagesize;
2884 else
2885 adjust = (sec->vma - voff) % align;
2886 }
2887 else
2888 adjust = 0;
2889
2890 if (adjust != 0)
2891 {
2892 if (i == 0)
2893 {
2894 (* _bfd_error_handler)
2895 (_("Error: First section in segment (%s) starts at 0x%x"),
2896 bfd_section_name (abfd, sec), sec->lma);
2897 (* _bfd_error_handler)
2898 (_(" whereas segment starts at 0x%x"),
2899 p->p_paddr);
2900
2901 return false;
2902 }
2903 p->p_memsz += adjust;
2904 off += adjust;
2905 voff += adjust;
2906 if ((flags & SEC_LOAD) != 0)
2907 p->p_filesz += adjust;
2908 }
2909
2910 sec->filepos = off;
2911
2912 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2913 used in a linker script we may have a section with
2914 SEC_LOAD clear but which is supposed to have
2915 contents. */
2916 if ((flags & SEC_LOAD) != 0
2917 || (flags & SEC_HAS_CONTENTS) != 0)
2918 off += sec->_raw_size;
2919
2920 if ((flags & SEC_ALLOC) != 0)
2921 voff += sec->_raw_size;
2922 }
2923
2924 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
2925 {
2926 /* The actual "note" segment has i == 0.
2927 This is the one that actually contains everything. */
2928 if (i == 0)
2929 {
2930 sec->filepos = off;
2931 p->p_filesz = sec->_raw_size;
2932 off += sec->_raw_size;
2933 voff = off;
2934 }
2935 else
2936 {
2937 /* Fake sections -- don't need to be written. */
2938 sec->filepos = 0;
2939 sec->_raw_size = 0;
2940 flags = sec->flags = 0;
2941 }
2942 p->p_memsz = 0;
2943 p->p_align = 1;
2944 }
2945 else
2946 {
2947 p->p_memsz += sec->_raw_size;
2948
2949 if ((flags & SEC_LOAD) != 0)
2950 p->p_filesz += sec->_raw_size;
2951
2952 if (align > p->p_align
2953 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
2954 p->p_align = align;
2955 }
2956
2957 if (! m->p_flags_valid)
2958 {
2959 p->p_flags |= PF_R;
2960 if ((flags & SEC_CODE) != 0)
2961 p->p_flags |= PF_X;
2962 if ((flags & SEC_READONLY) == 0)
2963 p->p_flags |= PF_W;
2964 }
2965 }
2966 }
2967
2968 /* Now that we have set the section file positions, we can set up
2969 the file positions for the non PT_LOAD segments. */
2970 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2971 m != NULL;
2972 m = m->next, p++)
2973 {
2974 if (p->p_type != PT_LOAD && m->count > 0)
2975 {
2976 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2977 p->p_offset = m->sections[0]->filepos;
2978 }
2979 if (m->count == 0)
2980 {
2981 if (m->includes_filehdr)
2982 {
2983 p->p_vaddr = filehdr_vaddr;
2984 if (! m->p_paddr_valid)
2985 p->p_paddr = filehdr_paddr;
2986 }
2987 else if (m->includes_phdrs)
2988 {
2989 p->p_vaddr = phdrs_vaddr;
2990 if (! m->p_paddr_valid)
2991 p->p_paddr = phdrs_paddr;
2992 }
2993 }
2994 }
2995
2996 /* Clear out any program headers we allocated but did not use. */
2997 for (; count < alloc; count++, p++)
2998 {
2999 memset (p, 0, sizeof *p);
3000 p->p_type = PT_NULL;
3001 }
3002
3003 elf_tdata (abfd)->phdr = phdrs;
3004
3005 elf_tdata (abfd)->next_file_pos = off;
3006
3007 /* Write out the program headers. */
3008 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
3009 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3010 return false;
3011
3012 return true;
3013 }
3014
3015 /* Get the size of the program header.
3016
3017 If this is called by the linker before any of the section VMA's are set, it
3018 can't calculate the correct value for a strange memory layout. This only
3019 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3020 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3021 data segment (exclusive of .interp and .dynamic).
3022
3023 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3024 will be two segments. */
3025
3026 static bfd_size_type
3027 get_program_header_size (abfd)
3028 bfd *abfd;
3029 {
3030 size_t segs;
3031 asection *s;
3032 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3033
3034 /* We can't return a different result each time we're called. */
3035 if (elf_tdata (abfd)->program_header_size != 0)
3036 return elf_tdata (abfd)->program_header_size;
3037
3038 if (elf_tdata (abfd)->segment_map != NULL)
3039 {
3040 struct elf_segment_map *m;
3041
3042 segs = 0;
3043 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3044 ++segs;
3045 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3046 return elf_tdata (abfd)->program_header_size;
3047 }
3048
3049 /* Assume we will need exactly two PT_LOAD segments: one for text
3050 and one for data. */
3051 segs = 2;
3052
3053 s = bfd_get_section_by_name (abfd, ".interp");
3054 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3055 {
3056 /* If we have a loadable interpreter section, we need a
3057 PT_INTERP segment. In this case, assume we also need a
3058 PT_PHDR segment, although that may not be true for all
3059 targets. */
3060 segs += 2;
3061 }
3062
3063 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3064 {
3065 /* We need a PT_DYNAMIC segment. */
3066 ++segs;
3067 }
3068
3069 for (s = abfd->sections; s != NULL; s = s->next)
3070 {
3071 if ((s->flags & SEC_LOAD) != 0
3072 && strncmp (s->name, ".note", 5) == 0)
3073 {
3074 /* We need a PT_NOTE segment. */
3075 ++segs;
3076 }
3077 }
3078
3079 /* Let the backend count up any program headers it might need. */
3080 if (bed->elf_backend_additional_program_headers)
3081 {
3082 int a;
3083
3084 a = (*bed->elf_backend_additional_program_headers) (abfd);
3085 if (a == -1)
3086 abort ();
3087 segs += a;
3088 }
3089
3090 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3091 return elf_tdata (abfd)->program_header_size;
3092 }
3093
3094 /* Work out the file positions of all the sections. This is called by
3095 _bfd_elf_compute_section_file_positions. All the section sizes and
3096 VMAs must be known before this is called.
3097
3098 We do not consider reloc sections at this point, unless they form
3099 part of the loadable image. Reloc sections are assigned file
3100 positions in assign_file_positions_for_relocs, which is called by
3101 write_object_contents and final_link.
3102
3103 We also don't set the positions of the .symtab and .strtab here. */
3104
3105 static boolean
3106 assign_file_positions_except_relocs (abfd)
3107 bfd *abfd;
3108 {
3109 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3110 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3111 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3112 file_ptr off;
3113 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3114
3115 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3116 && bfd_get_format (abfd) != bfd_core)
3117 {
3118 Elf_Internal_Shdr **hdrpp;
3119 unsigned int i;
3120
3121 /* Start after the ELF header. */
3122 off = i_ehdrp->e_ehsize;
3123
3124 /* We are not creating an executable, which means that we are
3125 not creating a program header, and that the actual order of
3126 the sections in the file is unimportant. */
3127 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3128 {
3129 Elf_Internal_Shdr *hdr;
3130
3131 hdr = *hdrpp;
3132 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3133 {
3134 hdr->sh_offset = -1;
3135 continue;
3136 }
3137 if (i == tdata->symtab_section
3138 || i == tdata->strtab_section)
3139 {
3140 hdr->sh_offset = -1;
3141 continue;
3142 }
3143
3144 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3145 }
3146 }
3147 else
3148 {
3149 unsigned int i;
3150 Elf_Internal_Shdr **hdrpp;
3151
3152 /* Assign file positions for the loaded sections based on the
3153 assignment of sections to segments. */
3154 if (! assign_file_positions_for_segments (abfd))
3155 return false;
3156
3157 /* Assign file positions for the other sections. */
3158
3159 off = elf_tdata (abfd)->next_file_pos;
3160 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3161 {
3162 Elf_Internal_Shdr *hdr;
3163
3164 hdr = *hdrpp;
3165 if (hdr->bfd_section != NULL
3166 && hdr->bfd_section->filepos != 0)
3167 hdr->sh_offset = hdr->bfd_section->filepos;
3168 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3169 {
3170 ((*_bfd_error_handler)
3171 (_("%s: warning: allocated section `%s' not in segment"),
3172 bfd_get_filename (abfd),
3173 (hdr->bfd_section == NULL
3174 ? "*unknown*"
3175 : hdr->bfd_section->name)));
3176 if ((abfd->flags & D_PAGED) != 0)
3177 off += (hdr->sh_addr - off) % bed->maxpagesize;
3178 else
3179 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3180 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3181 false);
3182 }
3183 else if (hdr->sh_type == SHT_REL
3184 || hdr->sh_type == SHT_RELA
3185 || hdr == i_shdrpp[tdata->symtab_section]
3186 || hdr == i_shdrpp[tdata->strtab_section])
3187 hdr->sh_offset = -1;
3188 else
3189 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3190 }
3191 }
3192
3193 /* Place the section headers. */
3194 off = align_file_position (off, bed->s->file_align);
3195 i_ehdrp->e_shoff = off;
3196 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3197
3198 elf_tdata (abfd)->next_file_pos = off;
3199
3200 return true;
3201 }
3202
3203 static boolean
3204 prep_headers (abfd)
3205 bfd *abfd;
3206 {
3207 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3208 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3209 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3210 int count;
3211 struct bfd_strtab_hash *shstrtab;
3212 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3213
3214 i_ehdrp = elf_elfheader (abfd);
3215 i_shdrp = elf_elfsections (abfd);
3216
3217 shstrtab = _bfd_elf_stringtab_init ();
3218 if (shstrtab == NULL)
3219 return false;
3220
3221 elf_shstrtab (abfd) = shstrtab;
3222
3223 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3224 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3225 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3226 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3227
3228 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3229 i_ehdrp->e_ident[EI_DATA] =
3230 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3231 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3232
3233 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3234 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3235
3236 for (count = EI_PAD; count < EI_NIDENT; count++)
3237 i_ehdrp->e_ident[count] = 0;
3238
3239 if ((abfd->flags & DYNAMIC) != 0)
3240 i_ehdrp->e_type = ET_DYN;
3241 else if ((abfd->flags & EXEC_P) != 0)
3242 i_ehdrp->e_type = ET_EXEC;
3243 else if (bfd_get_format (abfd) == bfd_core)
3244 i_ehdrp->e_type = ET_CORE;
3245 else
3246 i_ehdrp->e_type = ET_REL;
3247
3248 switch (bfd_get_arch (abfd))
3249 {
3250 case bfd_arch_unknown:
3251 i_ehdrp->e_machine = EM_NONE;
3252 break;
3253 case bfd_arch_sparc:
3254 if (bfd_elf_get_arch_size (abfd) == 64)
3255 i_ehdrp->e_machine = EM_SPARCV9;
3256 else
3257 i_ehdrp->e_machine = EM_SPARC;
3258 break;
3259 case bfd_arch_i370:
3260 i_ehdrp->e_machine = EM_S370;
3261 break;
3262 case bfd_arch_i386:
3263 i_ehdrp->e_machine = EM_386;
3264 break;
3265 case bfd_arch_ia64:
3266 i_ehdrp->e_machine = EM_IA_64;
3267 break;
3268 case bfd_arch_m68k:
3269 i_ehdrp->e_machine = EM_68K;
3270 break;
3271 case bfd_arch_m88k:
3272 i_ehdrp->e_machine = EM_88K;
3273 break;
3274 case bfd_arch_i860:
3275 i_ehdrp->e_machine = EM_860;
3276 break;
3277 case bfd_arch_i960:
3278 i_ehdrp->e_machine = EM_960;
3279 break;
3280 case bfd_arch_mips: /* MIPS Rxxxx */
3281 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3282 break;
3283 case bfd_arch_hppa:
3284 i_ehdrp->e_machine = EM_PARISC;
3285 break;
3286 case bfd_arch_powerpc:
3287 i_ehdrp->e_machine = EM_PPC;
3288 break;
3289 case bfd_arch_alpha:
3290 i_ehdrp->e_machine = EM_ALPHA;
3291 break;
3292 case bfd_arch_sh:
3293 i_ehdrp->e_machine = EM_SH;
3294 break;
3295 case bfd_arch_d10v:
3296 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3297 break;
3298 case bfd_arch_d30v:
3299 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3300 break;
3301 case bfd_arch_fr30:
3302 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3303 break;
3304 case bfd_arch_mcore:
3305 i_ehdrp->e_machine = EM_MCORE;
3306 break;
3307 case bfd_arch_avr:
3308 i_ehdrp->e_machine = EM_AVR;
3309 break;
3310 case bfd_arch_v850:
3311 switch (bfd_get_mach (abfd))
3312 {
3313 default:
3314 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3315 }
3316 break;
3317 case bfd_arch_arc:
3318 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3319 break;
3320 case bfd_arch_arm:
3321 i_ehdrp->e_machine = EM_ARM;
3322 break;
3323 case bfd_arch_m32r:
3324 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3325 break;
3326 case bfd_arch_mn10200:
3327 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3328 break;
3329 case bfd_arch_mn10300:
3330 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3331 break;
3332 case bfd_arch_pj:
3333 i_ehdrp->e_machine = EM_PJ;
3334 break;
3335 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3336 default:
3337 i_ehdrp->e_machine = EM_NONE;
3338 }
3339 i_ehdrp->e_version = bed->s->ev_current;
3340 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3341
3342 /* no program header, for now. */
3343 i_ehdrp->e_phoff = 0;
3344 i_ehdrp->e_phentsize = 0;
3345 i_ehdrp->e_phnum = 0;
3346
3347 /* each bfd section is section header entry */
3348 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3349 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3350
3351 /* if we're building an executable, we'll need a program header table */
3352 if (abfd->flags & EXEC_P)
3353 {
3354 /* it all happens later */
3355 #if 0
3356 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3357
3358 /* elf_build_phdrs() returns a (NULL-terminated) array of
3359 Elf_Internal_Phdrs */
3360 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3361 i_ehdrp->e_phoff = outbase;
3362 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3363 #endif
3364 }
3365 else
3366 {
3367 i_ehdrp->e_phentsize = 0;
3368 i_phdrp = 0;
3369 i_ehdrp->e_phoff = 0;
3370 }
3371
3372 elf_tdata (abfd)->symtab_hdr.sh_name =
3373 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3374 elf_tdata (abfd)->strtab_hdr.sh_name =
3375 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3376 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3377 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3378 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3379 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3380 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3381 return false;
3382
3383 return true;
3384 }
3385
3386 /* Assign file positions for all the reloc sections which are not part
3387 of the loadable file image. */
3388
3389 void
3390 _bfd_elf_assign_file_positions_for_relocs (abfd)
3391 bfd *abfd;
3392 {
3393 file_ptr off;
3394 unsigned int i;
3395 Elf_Internal_Shdr **shdrpp;
3396
3397 off = elf_tdata (abfd)->next_file_pos;
3398
3399 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3400 i < elf_elfheader (abfd)->e_shnum;
3401 i++, shdrpp++)
3402 {
3403 Elf_Internal_Shdr *shdrp;
3404
3405 shdrp = *shdrpp;
3406 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3407 && shdrp->sh_offset == -1)
3408 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3409 }
3410
3411 elf_tdata (abfd)->next_file_pos = off;
3412 }
3413
3414 boolean
3415 _bfd_elf_write_object_contents (abfd)
3416 bfd *abfd;
3417 {
3418 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3419 Elf_Internal_Ehdr *i_ehdrp;
3420 Elf_Internal_Shdr **i_shdrp;
3421 boolean failed;
3422 unsigned int count;
3423
3424 if (! abfd->output_has_begun
3425 && ! _bfd_elf_compute_section_file_positions
3426 (abfd, (struct bfd_link_info *) NULL))
3427 return false;
3428
3429 i_shdrp = elf_elfsections (abfd);
3430 i_ehdrp = elf_elfheader (abfd);
3431
3432 failed = false;
3433 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3434 if (failed)
3435 return false;
3436
3437 _bfd_elf_assign_file_positions_for_relocs (abfd);
3438
3439 /* After writing the headers, we need to write the sections too... */
3440 for (count = 1; count < i_ehdrp->e_shnum; count++)
3441 {
3442 if (bed->elf_backend_section_processing)
3443 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3444 if (i_shdrp[count]->contents)
3445 {
3446 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3447 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3448 1, abfd)
3449 != i_shdrp[count]->sh_size))
3450 return false;
3451 }
3452 }
3453
3454 /* Write out the section header names. */
3455 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3456 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3457 return false;
3458
3459 if (bed->elf_backend_final_write_processing)
3460 (*bed->elf_backend_final_write_processing) (abfd,
3461 elf_tdata (abfd)->linker);
3462
3463 return bed->s->write_shdrs_and_ehdr (abfd);
3464 }
3465
3466 boolean
3467 _bfd_elf_write_corefile_contents (abfd)
3468 bfd *abfd;
3469 {
3470 /* Hopefully this can be done just like an object file. */
3471 return _bfd_elf_write_object_contents (abfd);
3472 }
3473 /* given a section, search the header to find them... */
3474 int
3475 _bfd_elf_section_from_bfd_section (abfd, asect)
3476 bfd *abfd;
3477 struct sec *asect;
3478 {
3479 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3480 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3481 int index;
3482 Elf_Internal_Shdr *hdr;
3483 int maxindex = elf_elfheader (abfd)->e_shnum;
3484
3485 for (index = 0; index < maxindex; index++)
3486 {
3487 hdr = i_shdrp[index];
3488 if (hdr->bfd_section == asect)
3489 return index;
3490 }
3491
3492 if (bed->elf_backend_section_from_bfd_section)
3493 {
3494 for (index = 0; index < maxindex; index++)
3495 {
3496 int retval;
3497
3498 hdr = i_shdrp[index];
3499 retval = index;
3500 if ((*bed->elf_backend_section_from_bfd_section)
3501 (abfd, hdr, asect, &retval))
3502 return retval;
3503 }
3504 }
3505
3506 if (bfd_is_abs_section (asect))
3507 return SHN_ABS;
3508 if (bfd_is_com_section (asect))
3509 return SHN_COMMON;
3510 if (bfd_is_und_section (asect))
3511 return SHN_UNDEF;
3512
3513 bfd_set_error (bfd_error_nonrepresentable_section);
3514
3515 return -1;
3516 }
3517
3518 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3519 on error. */
3520
3521 int
3522 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3523 bfd *abfd;
3524 asymbol **asym_ptr_ptr;
3525 {
3526 asymbol *asym_ptr = *asym_ptr_ptr;
3527 int idx;
3528 flagword flags = asym_ptr->flags;
3529
3530 /* When gas creates relocations against local labels, it creates its
3531 own symbol for the section, but does put the symbol into the
3532 symbol chain, so udata is 0. When the linker is generating
3533 relocatable output, this section symbol may be for one of the
3534 input sections rather than the output section. */
3535 if (asym_ptr->udata.i == 0
3536 && (flags & BSF_SECTION_SYM)
3537 && asym_ptr->section)
3538 {
3539 int indx;
3540
3541 if (asym_ptr->section->output_section != NULL)
3542 indx = asym_ptr->section->output_section->index;
3543 else
3544 indx = asym_ptr->section->index;
3545 if (elf_section_syms (abfd)[indx])
3546 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3547 }
3548
3549 idx = asym_ptr->udata.i;
3550
3551 if (idx == 0)
3552 {
3553 /* This case can occur when using --strip-symbol on a symbol
3554 which is used in a relocation entry. */
3555 (*_bfd_error_handler)
3556 (_("%s: symbol `%s' required but not present"),
3557 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3558 bfd_set_error (bfd_error_no_symbols);
3559 return -1;
3560 }
3561
3562 #if DEBUG & 4
3563 {
3564 fprintf (stderr,
3565 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3566 (long) asym_ptr, asym_ptr->name, idx, flags,
3567 elf_symbol_flags (flags));
3568 fflush (stderr);
3569 }
3570 #endif
3571
3572 return idx;
3573 }
3574
3575 /* Copy private BFD data. This copies any program header information. */
3576
3577 static boolean
3578 copy_private_bfd_data (ibfd, obfd)
3579 bfd *ibfd;
3580 bfd *obfd;
3581 {
3582 Elf_Internal_Ehdr *iehdr;
3583 struct elf_segment_map *mfirst;
3584 struct elf_segment_map **pm;
3585 struct elf_segment_map *m;
3586 Elf_Internal_Phdr *p;
3587 unsigned int i;
3588 unsigned int num_segments;
3589 boolean phdr_included = false;
3590
3591 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3592 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3593 return true;
3594
3595 if (elf_tdata (ibfd)->phdr == NULL)
3596 return true;
3597
3598 iehdr = elf_elfheader (ibfd);
3599
3600 mfirst = NULL;
3601 pm = &mfirst;
3602
3603 num_segments = elf_elfheader (ibfd)->e_phnum;
3604
3605 #define IS_CONTAINED_BY(addr, len, bottom, phdr) \
3606 ((addr) >= (bottom) \
3607 && ( ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz) \
3608 || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz)))
3609
3610 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3611
3612 #define IS_COREFILE_NOTE(p, s) \
3613 (p->p_type == PT_NOTE \
3614 && bfd_get_format (ibfd) == bfd_core \
3615 && s->vma == 0 && s->lma == 0 \
3616 && (bfd_vma) s->filepos >= p->p_offset \
3617 && (bfd_vma) s->filepos + s->_raw_size \
3618 <= p->p_offset + p->p_filesz)
3619
3620 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3621 linker, which generates a PT_INTERP section with p_vaddr and
3622 p_memsz set to 0. */
3623
3624 #define IS_SOLARIS_PT_INTERP(p, s) \
3625 (p->p_vaddr == 0 \
3626 && p->p_filesz > 0 \
3627 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3628 && s->_raw_size > 0 \
3629 && (bfd_vma) s->filepos >= p->p_offset \
3630 && ((bfd_vma) s->filepos + s->_raw_size \
3631 <= p->p_offset + p->p_filesz))
3632
3633 /* Scan through the segments specified in the program header
3634 of the input BFD. */
3635 for (i = 0, p = elf_tdata (ibfd)->phdr; i < num_segments; i++, p++)
3636 {
3637 unsigned int csecs;
3638 asection *s;
3639 asection **sections;
3640 asection *os;
3641 unsigned int isec;
3642 bfd_vma matching_lma;
3643 bfd_vma suggested_lma;
3644 unsigned int j;
3645
3646 /* For each section in the input BFD, decide if it should be
3647 included in the current segment. A section will be included
3648 if it is within the address space of the segment, and it is
3649 an allocated segment, and there is an output section
3650 associated with it. */
3651 csecs = 0;
3652 for (s = ibfd->sections; s != NULL; s = s->next)
3653 if (s->output_section != NULL)
3654 {
3655 if ((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3656 || IS_SOLARIS_PT_INTERP (p, s))
3657 && (s->flags & SEC_ALLOC) != 0)
3658 ++csecs;
3659 else if (IS_COREFILE_NOTE (p, s))
3660 ++csecs;
3661 }
3662
3663 /* Allocate a segment map big enough to contain all of the
3664 sections we have selected. */
3665 m = ((struct elf_segment_map *)
3666 bfd_alloc (obfd,
3667 (sizeof (struct elf_segment_map)
3668 + ((size_t) csecs - 1) * sizeof (asection *))));
3669 if (m == NULL)
3670 return false;
3671
3672 /* Initialise the fields of the segment map. Default to
3673 using the physical address of the segment in the input BFD. */
3674 m->next = NULL;
3675 m->p_type = p->p_type;
3676 m->p_flags = p->p_flags;
3677 m->p_flags_valid = 1;
3678 m->p_paddr = p->p_paddr;
3679 m->p_paddr_valid = 1;
3680
3681 /* Determine if this segment contains the ELF file header
3682 and if it contains the program headers themselves. */
3683 m->includes_filehdr = (p->p_offset == 0
3684 && p->p_filesz >= iehdr->e_ehsize);
3685
3686 m->includes_phdrs = 0;
3687
3688 if (! phdr_included || p->p_type != PT_LOAD)
3689 {
3690 m->includes_phdrs =
3691 (p->p_offset <= (bfd_vma) iehdr->e_phoff
3692 && (p->p_offset + p->p_filesz
3693 >= ((bfd_vma) iehdr->e_phoff
3694 + iehdr->e_phnum * iehdr->e_phentsize)));
3695 if (p->p_type == PT_LOAD && m->includes_phdrs)
3696 phdr_included = true;
3697 }
3698
3699 if (csecs == 0)
3700 {
3701 /* Special segments, such as the PT_PHDR segment, may contain
3702 no sections, but ordinary, loadable segments should contain
3703 something. */
3704
3705 if (p->p_type == PT_LOAD)
3706 _bfd_error_handler
3707 (_("%s: warning: Empty loadable segment detected\n"),
3708 bfd_get_filename (ibfd));
3709
3710 m->count = 0;
3711 *pm = m;
3712 pm = &m->next;
3713
3714 continue;
3715 }
3716
3717 /* Now scan the sections in the input BFD again and attempt
3718 to add their corresponding output sections to the segment map.
3719 The problem here is how to handle an output section which has
3720 been moved (ie had its LMA changed). There are four possibilities:
3721
3722 1. None of the sections have been moved.
3723 In this case we can continue to use the segment LMA from the
3724 input BFD.
3725
3726 2. All of the sections have been moved by the same amount.
3727 In this case we can change the segment's LMA to match the LMA
3728 of the first section.
3729
3730 3. Some of the sections have been moved, others have not.
3731 In this case those sections which have not been moved can be
3732 placed in the current segment which will have to have its size,
3733 and possibly its LMA changed, and a new segment or segments will
3734 have to be created to contain the other sections.
3735
3736 4. The sections have been moved, but not be the same amount.
3737 In this case we can change the segment's LMA to match the LMA
3738 of the first section and we will have to create a new segment
3739 or segments to contain the other sections.
3740
3741 In order to save time, we allocate an array to hold the section
3742 pointers that we are interested in. As these sections get assigned
3743 to a segment, they are removed from this array. */
3744
3745 sections = (asection **) bfd_malloc (sizeof (asection *) * csecs);
3746 if (sections == NULL)
3747 return false;
3748
3749 /* Step One: Scan for segment vs section LMA conflicts.
3750 Also add the sections to the section array allocated above.
3751 Also add the sections to the current segment. In the common
3752 case, where the sections have not been moved, this means that
3753 we have completely filled the segment, and there is nothing
3754 more to do. */
3755
3756 isec = 0;
3757 matching_lma = 0;
3758 suggested_lma = 0;
3759
3760 for (j = 0, s = ibfd->sections; s != NULL; s = s->next)
3761 {
3762 os = s->output_section;
3763
3764 if ((((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3765 || IS_SOLARIS_PT_INTERP (p, s))
3766 && (s->flags & SEC_ALLOC) != 0)
3767 || IS_COREFILE_NOTE (p, s))
3768 && os != NULL)
3769 {
3770 sections[j++] = s;
3771
3772 /* The Solaris native linker always sets p_paddr to 0.
3773 We try to catch that case here, and set it to the
3774 correct value. */
3775 if (p->p_paddr == 0
3776 && p->p_vaddr != 0
3777 && isec == 0
3778 && os->lma != 0
3779 && (os->vma == (p->p_vaddr
3780 + (m->includes_filehdr
3781 ? iehdr->e_ehsize
3782 : 0)
3783 + (m->includes_phdrs
3784 ? iehdr->e_phnum * iehdr->e_phentsize
3785 : 0))))
3786 m->p_paddr = p->p_vaddr;
3787
3788 /* Match up the physical address of the segment with the
3789 LMA address of the output section. */
3790 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3791 || IS_COREFILE_NOTE (p, s))
3792 {
3793 if (matching_lma == 0)
3794 matching_lma = os->lma;
3795
3796 /* We assume that if the section fits within the segment
3797 that it does not overlap any other section within that
3798 segment. */
3799 m->sections[isec++] = os;
3800 }
3801 else if (suggested_lma == 0)
3802 suggested_lma = os->lma;
3803 }
3804 }
3805
3806 BFD_ASSERT (j == csecs);
3807
3808 /* Step Two: Adjust the physical address of the current segment,
3809 if necessary. */
3810 if (isec == csecs)
3811 {
3812 /* All of the sections fitted within the segment as currently
3813 specified. This is the default case. Add the segment to
3814 the list of built segments and carry on to process the next
3815 program header in the input BFD. */
3816 m->count = csecs;
3817 *pm = m;
3818 pm = &m->next;
3819
3820 free (sections);
3821 continue;
3822 }
3823 else
3824 {
3825 if (matching_lma != 0)
3826 {
3827 /* At least one section fits inside the current segment.
3828 Keep it, but modify its physical address to match the
3829 LMA of the first section that fitted. */
3830
3831 m->p_paddr = matching_lma;
3832 }
3833 else
3834 {
3835 /* None of the sections fitted inside the current segment.
3836 Change the current segment's physical address to match
3837 the LMA of the first section. */
3838
3839 m->p_paddr = suggested_lma;
3840 }
3841
3842 /* Offset the segment physical address from the lma to allow
3843 for space taken up by elf headers. */
3844 if (m->includes_filehdr)
3845 m->p_paddr -= iehdr->e_ehsize;
3846
3847 if (m->includes_phdrs)
3848 m->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
3849 }
3850
3851 /* Step Three: Loop over the sections again, this time assigning
3852 those that fit to the current segment and remvoing them from the
3853 sections array; but making sure not to leave large gaps. Once all
3854 possible sections have been assigned to the current segment it is
3855 added to the list of built segments and if sections still remain
3856 to be assigned, a new segment is constructed before repeating
3857 the loop. */
3858 isec = 0;
3859 do
3860 {
3861 m->count = 0;
3862 suggested_lma = 0;
3863
3864 /* Fill the current segment with sections that fit. */
3865 for (j = 0; j < csecs; j++)
3866 {
3867 s = sections[j];
3868
3869 if (s == NULL)
3870 continue;
3871
3872 os = s->output_section;
3873
3874 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3875 || IS_COREFILE_NOTE (p, s))
3876 {
3877 if (m->count == 0)
3878 {
3879 /* If the first section in a segment does not start at
3880 the beginning of the segment, then something is wrong. */
3881 if (os->lma != (m->p_paddr
3882 + (m->includes_filehdr
3883 ? iehdr->e_ehsize : 0)
3884 + (m->includes_phdrs
3885 ? iehdr->e_phnum * iehdr->e_phentsize
3886 : 0)))
3887 abort ();
3888 }
3889 else
3890 {
3891 asection * prev_sec;
3892 bfd_vma maxpagesize;
3893
3894 prev_sec = m->sections[m->count - 1];
3895 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3896
3897 /* If the gap between the end of the previous section
3898 and the start of this section is more than maxpagesize
3899 then we need to start a new segment. */
3900 if (BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
3901 < BFD_ALIGN (os->lma, maxpagesize))
3902 {
3903 if (suggested_lma == 0)
3904 suggested_lma = os->lma;
3905
3906 continue;
3907 }
3908 }
3909
3910 m->sections[m->count++] = os;
3911 ++isec;
3912 sections[j] = NULL;
3913 }
3914 else if (suggested_lma == 0)
3915 suggested_lma = os->lma;
3916 }
3917
3918 BFD_ASSERT (m->count > 0);
3919
3920 /* Add the current segment to the list of built segments. */
3921 *pm = m;
3922 pm = &m->next;
3923
3924 if (isec < csecs)
3925 {
3926 /* We still have not allocated all of the sections to
3927 segments. Create a new segment here, initialise it
3928 and carry on looping. */
3929
3930 m = ((struct elf_segment_map *)
3931 bfd_alloc (obfd,
3932 (sizeof (struct elf_segment_map)
3933 + ((size_t) csecs - 1) * sizeof (asection *))));
3934 if (m == NULL)
3935 return false;
3936
3937 /* Initialise the fields of the segment map. Set the physical
3938 physical address to the LMA of the first section that has
3939 not yet been assigned. */
3940
3941 m->next = NULL;
3942 m->p_type = p->p_type;
3943 m->p_flags = p->p_flags;
3944 m->p_flags_valid = 1;
3945 m->p_paddr = suggested_lma;
3946 m->p_paddr_valid = 1;
3947 m->includes_filehdr = 0;
3948 m->includes_phdrs = 0;
3949 }
3950 }
3951 while (isec < csecs);
3952
3953 free (sections);
3954 }
3955
3956 /* The Solaris linker creates program headers in which all the
3957 p_paddr fields are zero. When we try to objcopy or strip such a
3958 file, we get confused. Check for this case, and if we find it
3959 reset the p_paddr_valid fields. */
3960 for (m = mfirst; m != NULL; m = m->next)
3961 if (m->p_paddr != 0)
3962 break;
3963 if (m == NULL)
3964 {
3965 for (m = mfirst; m != NULL; m = m->next)
3966 m->p_paddr_valid = 0;
3967 }
3968
3969 elf_tdata (obfd)->segment_map = mfirst;
3970
3971 #if 0
3972 /* Final Step: Sort the segments into ascending order of physical address. */
3973 if (mfirst != NULL)
3974 {
3975 struct elf_segment_map* prev;
3976
3977 prev = mfirst;
3978 for (m = mfirst->next; m != NULL; prev = m, m = m->next)
3979 {
3980 /* Yes I know - its a bubble sort....*/
3981 if (m->next != NULL && (m->next->p_paddr < m->p_paddr))
3982 {
3983 /* swap m and m->next */
3984 prev->next = m->next;
3985 m->next = m->next->next;
3986 prev->next->next = m;
3987
3988 /* restart loop. */
3989 m = mfirst;
3990 }
3991 }
3992 }
3993 #endif
3994
3995 #undef IS_CONTAINED_BY
3996 #undef IS_SOLARIS_PT_INTERP
3997 #undef IS_COREFILE_NOTE
3998 return true;
3999 }
4000
4001 /* Copy private section information. This copies over the entsize
4002 field, and sometimes the info field. */
4003
4004 boolean
4005 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4006 bfd *ibfd;
4007 asection *isec;
4008 bfd *obfd;
4009 asection *osec;
4010 {
4011 Elf_Internal_Shdr *ihdr, *ohdr;
4012
4013 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4014 || obfd->xvec->flavour != bfd_target_elf_flavour)
4015 return true;
4016
4017 /* Copy over private BFD data if it has not already been copied.
4018 This must be done here, rather than in the copy_private_bfd_data
4019 entry point, because the latter is called after the section
4020 contents have been set, which means that the program headers have
4021 already been worked out. */
4022 if (elf_tdata (obfd)->segment_map == NULL
4023 && elf_tdata (ibfd)->phdr != NULL)
4024 {
4025 asection *s;
4026
4027 /* Only set up the segments if there are no more SEC_ALLOC
4028 sections. FIXME: This won't do the right thing if objcopy is
4029 used to remove the last SEC_ALLOC section, since objcopy
4030 won't call this routine in that case. */
4031 for (s = isec->next; s != NULL; s = s->next)
4032 if ((s->flags & SEC_ALLOC) != 0)
4033 break;
4034 if (s == NULL)
4035 {
4036 if (! copy_private_bfd_data (ibfd, obfd))
4037 return false;
4038 }
4039 }
4040
4041 ihdr = &elf_section_data (isec)->this_hdr;
4042 ohdr = &elf_section_data (osec)->this_hdr;
4043
4044 ohdr->sh_entsize = ihdr->sh_entsize;
4045
4046 if (ihdr->sh_type == SHT_SYMTAB
4047 || ihdr->sh_type == SHT_DYNSYM
4048 || ihdr->sh_type == SHT_GNU_verneed
4049 || ihdr->sh_type == SHT_GNU_verdef)
4050 ohdr->sh_info = ihdr->sh_info;
4051
4052 elf_section_data (osec)->use_rela_p
4053 = elf_section_data (isec)->use_rela_p;
4054
4055 return true;
4056 }
4057
4058 /* Copy private symbol information. If this symbol is in a section
4059 which we did not map into a BFD section, try to map the section
4060 index correctly. We use special macro definitions for the mapped
4061 section indices; these definitions are interpreted by the
4062 swap_out_syms function. */
4063
4064 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4065 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4066 #define MAP_STRTAB (SHN_LORESERVE - 3)
4067 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4068
4069 boolean
4070 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4071 bfd *ibfd;
4072 asymbol *isymarg;
4073 bfd *obfd;
4074 asymbol *osymarg;
4075 {
4076 elf_symbol_type *isym, *osym;
4077
4078 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4079 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4080 return true;
4081
4082 isym = elf_symbol_from (ibfd, isymarg);
4083 osym = elf_symbol_from (obfd, osymarg);
4084
4085 if (isym != NULL
4086 && osym != NULL
4087 && bfd_is_abs_section (isym->symbol.section))
4088 {
4089 unsigned int shndx;
4090
4091 shndx = isym->internal_elf_sym.st_shndx;
4092 if (shndx == elf_onesymtab (ibfd))
4093 shndx = MAP_ONESYMTAB;
4094 else if (shndx == elf_dynsymtab (ibfd))
4095 shndx = MAP_DYNSYMTAB;
4096 else if (shndx == elf_tdata (ibfd)->strtab_section)
4097 shndx = MAP_STRTAB;
4098 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4099 shndx = MAP_SHSTRTAB;
4100 osym->internal_elf_sym.st_shndx = shndx;
4101 }
4102
4103 return true;
4104 }
4105
4106 /* Swap out the symbols. */
4107
4108 static boolean
4109 swap_out_syms (abfd, sttp, relocatable_p)
4110 bfd *abfd;
4111 struct bfd_strtab_hash **sttp;
4112 int relocatable_p;
4113 {
4114 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4115
4116 if (!elf_map_symbols (abfd))
4117 return false;
4118
4119 /* Dump out the symtabs. */
4120 {
4121 int symcount = bfd_get_symcount (abfd);
4122 asymbol **syms = bfd_get_outsymbols (abfd);
4123 struct bfd_strtab_hash *stt;
4124 Elf_Internal_Shdr *symtab_hdr;
4125 Elf_Internal_Shdr *symstrtab_hdr;
4126 char *outbound_syms;
4127 int idx;
4128
4129 stt = _bfd_elf_stringtab_init ();
4130 if (stt == NULL)
4131 return false;
4132
4133 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4134 symtab_hdr->sh_type = SHT_SYMTAB;
4135 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4136 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4137 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4138 symtab_hdr->sh_addralign = bed->s->file_align;
4139
4140 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4141 symstrtab_hdr->sh_type = SHT_STRTAB;
4142
4143 outbound_syms = bfd_alloc (abfd,
4144 (1 + symcount) * bed->s->sizeof_sym);
4145 if (outbound_syms == NULL)
4146 return false;
4147 symtab_hdr->contents = (PTR) outbound_syms;
4148
4149 /* now generate the data (for "contents") */
4150 {
4151 /* Fill in zeroth symbol and swap it out. */
4152 Elf_Internal_Sym sym;
4153 sym.st_name = 0;
4154 sym.st_value = 0;
4155 sym.st_size = 0;
4156 sym.st_info = 0;
4157 sym.st_other = 0;
4158 sym.st_shndx = SHN_UNDEF;
4159 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4160 outbound_syms += bed->s->sizeof_sym;
4161 }
4162 for (idx = 0; idx < symcount; idx++)
4163 {
4164 Elf_Internal_Sym sym;
4165 bfd_vma value = syms[idx]->value;
4166 elf_symbol_type *type_ptr;
4167 flagword flags = syms[idx]->flags;
4168 int type;
4169
4170 if (flags & BSF_SECTION_SYM)
4171 /* Section symbols have no names. */
4172 sym.st_name = 0;
4173 else
4174 {
4175 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4176 syms[idx]->name,
4177 true, false);
4178 if (sym.st_name == (unsigned long) -1)
4179 return false;
4180 }
4181
4182 type_ptr = elf_symbol_from (abfd, syms[idx]);
4183
4184 if ((flags & BSF_SECTION_SYM) == 0
4185 && bfd_is_com_section (syms[idx]->section))
4186 {
4187 /* ELF common symbols put the alignment into the `value' field,
4188 and the size into the `size' field. This is backwards from
4189 how BFD handles it, so reverse it here. */
4190 sym.st_size = value;
4191 if (type_ptr == NULL
4192 || type_ptr->internal_elf_sym.st_value == 0)
4193 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4194 else
4195 sym.st_value = type_ptr->internal_elf_sym.st_value;
4196 sym.st_shndx = _bfd_elf_section_from_bfd_section
4197 (abfd, syms[idx]->section);
4198 }
4199 else
4200 {
4201 asection *sec = syms[idx]->section;
4202 int shndx;
4203
4204 if (sec->output_section)
4205 {
4206 value += sec->output_offset;
4207 sec = sec->output_section;
4208 }
4209 /* Don't add in the section vma for relocatable output. */
4210 if (! relocatable_p)
4211 value += sec->vma;
4212 sym.st_value = value;
4213 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4214
4215 if (bfd_is_abs_section (sec)
4216 && type_ptr != NULL
4217 && type_ptr->internal_elf_sym.st_shndx != 0)
4218 {
4219 /* This symbol is in a real ELF section which we did
4220 not create as a BFD section. Undo the mapping done
4221 by copy_private_symbol_data. */
4222 shndx = type_ptr->internal_elf_sym.st_shndx;
4223 switch (shndx)
4224 {
4225 case MAP_ONESYMTAB:
4226 shndx = elf_onesymtab (abfd);
4227 break;
4228 case MAP_DYNSYMTAB:
4229 shndx = elf_dynsymtab (abfd);
4230 break;
4231 case MAP_STRTAB:
4232 shndx = elf_tdata (abfd)->strtab_section;
4233 break;
4234 case MAP_SHSTRTAB:
4235 shndx = elf_tdata (abfd)->shstrtab_section;
4236 break;
4237 default:
4238 break;
4239 }
4240 }
4241 else
4242 {
4243 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4244
4245 if (shndx == -1)
4246 {
4247 asection *sec2;
4248
4249 /* Writing this would be a hell of a lot easier if
4250 we had some decent documentation on bfd, and
4251 knew what to expect of the library, and what to
4252 demand of applications. For example, it
4253 appears that `objcopy' might not set the
4254 section of a symbol to be a section that is
4255 actually in the output file. */
4256 sec2 = bfd_get_section_by_name (abfd, sec->name);
4257 BFD_ASSERT (sec2 != 0);
4258 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4259 BFD_ASSERT (shndx != -1);
4260 }
4261 }
4262
4263 sym.st_shndx = shndx;
4264 }
4265
4266 if ((flags & BSF_FUNCTION) != 0)
4267 type = STT_FUNC;
4268 else if ((flags & BSF_OBJECT) != 0)
4269 type = STT_OBJECT;
4270 else
4271 type = STT_NOTYPE;
4272
4273 /* Processor-specific types */
4274 if (type_ptr != NULL
4275 && bed->elf_backend_get_symbol_type)
4276 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4277
4278 if (flags & BSF_SECTION_SYM)
4279 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4280 else if (bfd_is_com_section (syms[idx]->section))
4281 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4282 else if (bfd_is_und_section (syms[idx]->section))
4283 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4284 ? STB_WEAK
4285 : STB_GLOBAL),
4286 type);
4287 else if (flags & BSF_FILE)
4288 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4289 else
4290 {
4291 int bind = STB_LOCAL;
4292
4293 if (flags & BSF_LOCAL)
4294 bind = STB_LOCAL;
4295 else if (flags & BSF_WEAK)
4296 bind = STB_WEAK;
4297 else if (flags & BSF_GLOBAL)
4298 bind = STB_GLOBAL;
4299
4300 sym.st_info = ELF_ST_INFO (bind, type);
4301 }
4302
4303 if (type_ptr != NULL)
4304 sym.st_other = type_ptr->internal_elf_sym.st_other;
4305 else
4306 sym.st_other = 0;
4307
4308 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4309 outbound_syms += bed->s->sizeof_sym;
4310 }
4311
4312 *sttp = stt;
4313 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4314 symstrtab_hdr->sh_type = SHT_STRTAB;
4315
4316 symstrtab_hdr->sh_flags = 0;
4317 symstrtab_hdr->sh_addr = 0;
4318 symstrtab_hdr->sh_entsize = 0;
4319 symstrtab_hdr->sh_link = 0;
4320 symstrtab_hdr->sh_info = 0;
4321 symstrtab_hdr->sh_addralign = 1;
4322 }
4323
4324 return true;
4325 }
4326
4327 /* Return the number of bytes required to hold the symtab vector.
4328
4329 Note that we base it on the count plus 1, since we will null terminate
4330 the vector allocated based on this size. However, the ELF symbol table
4331 always has a dummy entry as symbol #0, so it ends up even. */
4332
4333 long
4334 _bfd_elf_get_symtab_upper_bound (abfd)
4335 bfd *abfd;
4336 {
4337 long symcount;
4338 long symtab_size;
4339 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4340
4341 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4342 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4343
4344 return symtab_size;
4345 }
4346
4347 long
4348 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4349 bfd *abfd;
4350 {
4351 long symcount;
4352 long symtab_size;
4353 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4354
4355 if (elf_dynsymtab (abfd) == 0)
4356 {
4357 bfd_set_error (bfd_error_invalid_operation);
4358 return -1;
4359 }
4360
4361 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4362 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4363
4364 return symtab_size;
4365 }
4366
4367 long
4368 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4369 bfd *abfd ATTRIBUTE_UNUSED;
4370 sec_ptr asect;
4371 {
4372 return (asect->reloc_count + 1) * sizeof (arelent *);
4373 }
4374
4375 /* Canonicalize the relocs. */
4376
4377 long
4378 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4379 bfd *abfd;
4380 sec_ptr section;
4381 arelent **relptr;
4382 asymbol **symbols;
4383 {
4384 arelent *tblptr;
4385 unsigned int i;
4386
4387 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4388 section,
4389 symbols,
4390 false))
4391 return -1;
4392
4393 tblptr = section->relocation;
4394 for (i = 0; i < section->reloc_count; i++)
4395 *relptr++ = tblptr++;
4396
4397 *relptr = NULL;
4398
4399 return section->reloc_count;
4400 }
4401
4402 long
4403 _bfd_elf_get_symtab (abfd, alocation)
4404 bfd *abfd;
4405 asymbol **alocation;
4406 {
4407 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4408 (abfd, alocation, false);
4409
4410 if (symcount >= 0)
4411 bfd_get_symcount (abfd) = symcount;
4412 return symcount;
4413 }
4414
4415 long
4416 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4417 bfd *abfd;
4418 asymbol **alocation;
4419 {
4420 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4421 (abfd, alocation, true);
4422 }
4423
4424 /* Return the size required for the dynamic reloc entries. Any
4425 section that was actually installed in the BFD, and has type
4426 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4427 considered to be a dynamic reloc section. */
4428
4429 long
4430 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4431 bfd *abfd;
4432 {
4433 long ret;
4434 asection *s;
4435
4436 if (elf_dynsymtab (abfd) == 0)
4437 {
4438 bfd_set_error (bfd_error_invalid_operation);
4439 return -1;
4440 }
4441
4442 ret = sizeof (arelent *);
4443 for (s = abfd->sections; s != NULL; s = s->next)
4444 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4445 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4446 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4447 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4448 * sizeof (arelent *));
4449
4450 return ret;
4451 }
4452
4453 /* Canonicalize the dynamic relocation entries. Note that we return
4454 the dynamic relocations as a single block, although they are
4455 actually associated with particular sections; the interface, which
4456 was designed for SunOS style shared libraries, expects that there
4457 is only one set of dynamic relocs. Any section that was actually
4458 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4459 the dynamic symbol table, is considered to be a dynamic reloc
4460 section. */
4461
4462 long
4463 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4464 bfd *abfd;
4465 arelent **storage;
4466 asymbol **syms;
4467 {
4468 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4469 asection *s;
4470 long ret;
4471
4472 if (elf_dynsymtab (abfd) == 0)
4473 {
4474 bfd_set_error (bfd_error_invalid_operation);
4475 return -1;
4476 }
4477
4478 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4479 ret = 0;
4480 for (s = abfd->sections; s != NULL; s = s->next)
4481 {
4482 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4483 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4484 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4485 {
4486 arelent *p;
4487 long count, i;
4488
4489 if (! (*slurp_relocs) (abfd, s, syms, true))
4490 return -1;
4491 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4492 p = s->relocation;
4493 for (i = 0; i < count; i++)
4494 *storage++ = p++;
4495 ret += count;
4496 }
4497 }
4498
4499 *storage = NULL;
4500
4501 return ret;
4502 }
4503 \f
4504 /* Read in the version information. */
4505
4506 boolean
4507 _bfd_elf_slurp_version_tables (abfd)
4508 bfd *abfd;
4509 {
4510 bfd_byte *contents = NULL;
4511
4512 if (elf_dynverdef (abfd) != 0)
4513 {
4514 Elf_Internal_Shdr *hdr;
4515 Elf_External_Verdef *everdef;
4516 Elf_Internal_Verdef *iverdef;
4517 Elf_Internal_Verdef *iverdefarr;
4518 Elf_Internal_Verdef iverdefmem;
4519 unsigned int i;
4520 unsigned int maxidx;
4521
4522 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4523
4524 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4525 if (contents == NULL)
4526 goto error_return;
4527 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4528 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4529 goto error_return;
4530
4531 /* We know the number of entries in the section but not the maximum
4532 index. Therefore we have to run through all entries and find
4533 the maximum. */
4534 everdef = (Elf_External_Verdef *) contents;
4535 maxidx = 0;
4536 for (i = 0; i < hdr->sh_info; ++i)
4537 {
4538 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4539
4540 if ((iverdefmem.vd_ndx & VERSYM_VERSION) > maxidx)
4541 maxidx = iverdefmem.vd_ndx & VERSYM_VERSION;
4542
4543 everdef = ((Elf_External_Verdef *)
4544 ((bfd_byte *) everdef + iverdefmem.vd_next));
4545 }
4546
4547 elf_tdata (abfd)->verdef =
4548 ((Elf_Internal_Verdef *)
4549 bfd_zalloc (abfd, maxidx * sizeof (Elf_Internal_Verdef)));
4550 if (elf_tdata (abfd)->verdef == NULL)
4551 goto error_return;
4552
4553 elf_tdata (abfd)->cverdefs = maxidx;
4554
4555 everdef = (Elf_External_Verdef *) contents;
4556 iverdefarr = elf_tdata (abfd)->verdef;
4557 for (i = 0; i < hdr->sh_info; i++)
4558 {
4559 Elf_External_Verdaux *everdaux;
4560 Elf_Internal_Verdaux *iverdaux;
4561 unsigned int j;
4562
4563 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4564
4565 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
4566 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
4567
4568 iverdef->vd_bfd = abfd;
4569
4570 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4571 bfd_alloc (abfd,
4572 (iverdef->vd_cnt
4573 * sizeof (Elf_Internal_Verdaux))));
4574 if (iverdef->vd_auxptr == NULL)
4575 goto error_return;
4576
4577 everdaux = ((Elf_External_Verdaux *)
4578 ((bfd_byte *) everdef + iverdef->vd_aux));
4579 iverdaux = iverdef->vd_auxptr;
4580 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4581 {
4582 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4583
4584 iverdaux->vda_nodename =
4585 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4586 iverdaux->vda_name);
4587 if (iverdaux->vda_nodename == NULL)
4588 goto error_return;
4589
4590 if (j + 1 < iverdef->vd_cnt)
4591 iverdaux->vda_nextptr = iverdaux + 1;
4592 else
4593 iverdaux->vda_nextptr = NULL;
4594
4595 everdaux = ((Elf_External_Verdaux *)
4596 ((bfd_byte *) everdaux + iverdaux->vda_next));
4597 }
4598
4599 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4600
4601 if (i + 1 < hdr->sh_info)
4602 iverdef->vd_nextdef = iverdef + 1;
4603 else
4604 iverdef->vd_nextdef = NULL;
4605
4606 everdef = ((Elf_External_Verdef *)
4607 ((bfd_byte *) everdef + iverdef->vd_next));
4608 }
4609
4610 free (contents);
4611 contents = NULL;
4612 }
4613
4614 if (elf_dynverref (abfd) != 0)
4615 {
4616 Elf_Internal_Shdr *hdr;
4617 Elf_External_Verneed *everneed;
4618 Elf_Internal_Verneed *iverneed;
4619 unsigned int i;
4620
4621 hdr = &elf_tdata (abfd)->dynverref_hdr;
4622
4623 elf_tdata (abfd)->verref =
4624 ((Elf_Internal_Verneed *)
4625 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4626 if (elf_tdata (abfd)->verref == NULL)
4627 goto error_return;
4628
4629 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4630
4631 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4632 if (contents == NULL)
4633 goto error_return;
4634 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4635 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4636 goto error_return;
4637
4638 everneed = (Elf_External_Verneed *) contents;
4639 iverneed = elf_tdata (abfd)->verref;
4640 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4641 {
4642 Elf_External_Vernaux *evernaux;
4643 Elf_Internal_Vernaux *ivernaux;
4644 unsigned int j;
4645
4646 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4647
4648 iverneed->vn_bfd = abfd;
4649
4650 iverneed->vn_filename =
4651 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4652 iverneed->vn_file);
4653 if (iverneed->vn_filename == NULL)
4654 goto error_return;
4655
4656 iverneed->vn_auxptr =
4657 ((Elf_Internal_Vernaux *)
4658 bfd_alloc (abfd,
4659 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4660
4661 evernaux = ((Elf_External_Vernaux *)
4662 ((bfd_byte *) everneed + iverneed->vn_aux));
4663 ivernaux = iverneed->vn_auxptr;
4664 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4665 {
4666 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4667
4668 ivernaux->vna_nodename =
4669 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4670 ivernaux->vna_name);
4671 if (ivernaux->vna_nodename == NULL)
4672 goto error_return;
4673
4674 if (j + 1 < iverneed->vn_cnt)
4675 ivernaux->vna_nextptr = ivernaux + 1;
4676 else
4677 ivernaux->vna_nextptr = NULL;
4678
4679 evernaux = ((Elf_External_Vernaux *)
4680 ((bfd_byte *) evernaux + ivernaux->vna_next));
4681 }
4682
4683 if (i + 1 < hdr->sh_info)
4684 iverneed->vn_nextref = iverneed + 1;
4685 else
4686 iverneed->vn_nextref = NULL;
4687
4688 everneed = ((Elf_External_Verneed *)
4689 ((bfd_byte *) everneed + iverneed->vn_next));
4690 }
4691
4692 free (contents);
4693 contents = NULL;
4694 }
4695
4696 return true;
4697
4698 error_return:
4699 if (contents == NULL)
4700 free (contents);
4701 return false;
4702 }
4703 \f
4704 asymbol *
4705 _bfd_elf_make_empty_symbol (abfd)
4706 bfd *abfd;
4707 {
4708 elf_symbol_type *newsym;
4709
4710 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4711 if (!newsym)
4712 return NULL;
4713 else
4714 {
4715 newsym->symbol.the_bfd = abfd;
4716 return &newsym->symbol;
4717 }
4718 }
4719
4720 void
4721 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4722 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4723 asymbol *symbol;
4724 symbol_info *ret;
4725 {
4726 bfd_symbol_info (symbol, ret);
4727 }
4728
4729 /* Return whether a symbol name implies a local symbol. Most targets
4730 use this function for the is_local_label_name entry point, but some
4731 override it. */
4732
4733 boolean
4734 _bfd_elf_is_local_label_name (abfd, name)
4735 bfd *abfd ATTRIBUTE_UNUSED;
4736 const char *name;
4737 {
4738 /* Normal local symbols start with ``.L''. */
4739 if (name[0] == '.' && name[1] == 'L')
4740 return true;
4741
4742 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4743 DWARF debugging symbols starting with ``..''. */
4744 if (name[0] == '.' && name[1] == '.')
4745 return true;
4746
4747 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4748 emitting DWARF debugging output. I suspect this is actually a
4749 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4750 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4751 underscore to be emitted on some ELF targets). For ease of use,
4752 we treat such symbols as local. */
4753 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4754 return true;
4755
4756 return false;
4757 }
4758
4759 alent *
4760 _bfd_elf_get_lineno (ignore_abfd, symbol)
4761 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4762 asymbol *symbol ATTRIBUTE_UNUSED;
4763 {
4764 abort ();
4765 return NULL;
4766 }
4767
4768 boolean
4769 _bfd_elf_set_arch_mach (abfd, arch, machine)
4770 bfd *abfd;
4771 enum bfd_architecture arch;
4772 unsigned long machine;
4773 {
4774 /* If this isn't the right architecture for this backend, and this
4775 isn't the generic backend, fail. */
4776 if (arch != get_elf_backend_data (abfd)->arch
4777 && arch != bfd_arch_unknown
4778 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4779 return false;
4780
4781 return bfd_default_set_arch_mach (abfd, arch, machine);
4782 }
4783
4784 /* Find the nearest line to a particular section and offset, for error
4785 reporting. */
4786
4787 boolean
4788 _bfd_elf_find_nearest_line (abfd,
4789 section,
4790 symbols,
4791 offset,
4792 filename_ptr,
4793 functionname_ptr,
4794 line_ptr)
4795 bfd *abfd;
4796 asection *section;
4797 asymbol **symbols;
4798 bfd_vma offset;
4799 CONST char **filename_ptr;
4800 CONST char **functionname_ptr;
4801 unsigned int *line_ptr;
4802 {
4803 boolean found;
4804 const char *filename;
4805 asymbol *func;
4806 bfd_vma low_func;
4807 asymbol **p;
4808
4809 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4810 filename_ptr, functionname_ptr,
4811 line_ptr))
4812 return true;
4813
4814 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4815 filename_ptr, functionname_ptr,
4816 line_ptr, 0))
4817 return true;
4818
4819 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4820 &found, filename_ptr,
4821 functionname_ptr, line_ptr,
4822 &elf_tdata (abfd)->line_info))
4823 return false;
4824 if (found)
4825 return true;
4826
4827 if (symbols == NULL)
4828 return false;
4829
4830 filename = NULL;
4831 func = NULL;
4832 low_func = 0;
4833
4834 for (p = symbols; *p != NULL; p++)
4835 {
4836 elf_symbol_type *q;
4837
4838 q = (elf_symbol_type *) *p;
4839
4840 if (bfd_get_section (&q->symbol) != section)
4841 continue;
4842
4843 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4844 {
4845 default:
4846 break;
4847 case STT_FILE:
4848 filename = bfd_asymbol_name (&q->symbol);
4849 break;
4850 case STT_NOTYPE:
4851 case STT_FUNC:
4852 if (q->symbol.section == section
4853 && q->symbol.value >= low_func
4854 && q->symbol.value <= offset)
4855 {
4856 func = (asymbol *) q;
4857 low_func = q->symbol.value;
4858 }
4859 break;
4860 }
4861 }
4862
4863 if (func == NULL)
4864 return false;
4865
4866 *filename_ptr = filename;
4867 *functionname_ptr = bfd_asymbol_name (func);
4868 *line_ptr = 0;
4869 return true;
4870 }
4871
4872 int
4873 _bfd_elf_sizeof_headers (abfd, reloc)
4874 bfd *abfd;
4875 boolean reloc;
4876 {
4877 int ret;
4878
4879 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
4880 if (! reloc)
4881 ret += get_program_header_size (abfd);
4882 return ret;
4883 }
4884
4885 boolean
4886 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
4887 bfd *abfd;
4888 sec_ptr section;
4889 PTR location;
4890 file_ptr offset;
4891 bfd_size_type count;
4892 {
4893 Elf_Internal_Shdr *hdr;
4894
4895 if (! abfd->output_has_begun
4896 && ! _bfd_elf_compute_section_file_positions
4897 (abfd, (struct bfd_link_info *) NULL))
4898 return false;
4899
4900 hdr = &elf_section_data (section)->this_hdr;
4901
4902 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
4903 return false;
4904 if (bfd_write (location, 1, count, abfd) != count)
4905 return false;
4906
4907 return true;
4908 }
4909
4910 void
4911 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
4912 bfd *abfd ATTRIBUTE_UNUSED;
4913 arelent *cache_ptr ATTRIBUTE_UNUSED;
4914 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
4915 {
4916 abort ();
4917 }
4918
4919 #if 0
4920 void
4921 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
4922 bfd *abfd;
4923 arelent *cache_ptr;
4924 Elf_Internal_Rel *dst;
4925 {
4926 abort ();
4927 }
4928 #endif
4929
4930 /* Try to convert a non-ELF reloc into an ELF one. */
4931
4932 boolean
4933 _bfd_elf_validate_reloc (abfd, areloc)
4934 bfd *abfd;
4935 arelent *areloc;
4936 {
4937 /* Check whether we really have an ELF howto. */
4938
4939 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
4940 {
4941 bfd_reloc_code_real_type code;
4942 reloc_howto_type *howto;
4943
4944 /* Alien reloc: Try to determine its type to replace it with an
4945 equivalent ELF reloc. */
4946
4947 if (areloc->howto->pc_relative)
4948 {
4949 switch (areloc->howto->bitsize)
4950 {
4951 case 8:
4952 code = BFD_RELOC_8_PCREL;
4953 break;
4954 case 12:
4955 code = BFD_RELOC_12_PCREL;
4956 break;
4957 case 16:
4958 code = BFD_RELOC_16_PCREL;
4959 break;
4960 case 24:
4961 code = BFD_RELOC_24_PCREL;
4962 break;
4963 case 32:
4964 code = BFD_RELOC_32_PCREL;
4965 break;
4966 case 64:
4967 code = BFD_RELOC_64_PCREL;
4968 break;
4969 default:
4970 goto fail;
4971 }
4972
4973 howto = bfd_reloc_type_lookup (abfd, code);
4974
4975 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
4976 {
4977 if (howto->pcrel_offset)
4978 areloc->addend += areloc->address;
4979 else
4980 areloc->addend -= areloc->address; /* addend is unsigned!! */
4981 }
4982 }
4983 else
4984 {
4985 switch (areloc->howto->bitsize)
4986 {
4987 case 8:
4988 code = BFD_RELOC_8;
4989 break;
4990 case 14:
4991 code = BFD_RELOC_14;
4992 break;
4993 case 16:
4994 code = BFD_RELOC_16;
4995 break;
4996 case 26:
4997 code = BFD_RELOC_26;
4998 break;
4999 case 32:
5000 code = BFD_RELOC_32;
5001 break;
5002 case 64:
5003 code = BFD_RELOC_64;
5004 break;
5005 default:
5006 goto fail;
5007 }
5008
5009 howto = bfd_reloc_type_lookup (abfd, code);
5010 }
5011
5012 if (howto)
5013 areloc->howto = howto;
5014 else
5015 goto fail;
5016 }
5017
5018 return true;
5019
5020 fail:
5021 (*_bfd_error_handler)
5022 (_("%s: unsupported relocation type %s"),
5023 bfd_get_filename (abfd), areloc->howto->name);
5024 bfd_set_error (bfd_error_bad_value);
5025 return false;
5026 }
5027
5028 boolean
5029 _bfd_elf_close_and_cleanup (abfd)
5030 bfd *abfd;
5031 {
5032 if (bfd_get_format (abfd) == bfd_object)
5033 {
5034 if (elf_shstrtab (abfd) != NULL)
5035 _bfd_stringtab_free (elf_shstrtab (abfd));
5036 }
5037
5038 return _bfd_generic_close_and_cleanup (abfd);
5039 }
5040
5041 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5042 in the relocation's offset. Thus we cannot allow any sort of sanity
5043 range-checking to interfere. There is nothing else to do in processing
5044 this reloc. */
5045
5046 bfd_reloc_status_type
5047 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5048 bfd *abfd ATTRIBUTE_UNUSED;
5049 arelent *re ATTRIBUTE_UNUSED;
5050 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5051 PTR data ATTRIBUTE_UNUSED;
5052 asection *is ATTRIBUTE_UNUSED;
5053 bfd *obfd ATTRIBUTE_UNUSED;
5054 char **errmsg ATTRIBUTE_UNUSED;
5055 {
5056 return bfd_reloc_ok;
5057 }
5058
5059 \f
5060 /* Elf core file support. Much of this only works on native
5061 toolchains, since we rely on knowing the
5062 machine-dependent procfs structure in order to pick
5063 out details about the corefile. */
5064
5065 #ifdef HAVE_SYS_PROCFS_H
5066 # include <sys/procfs.h>
5067 #endif
5068
5069
5070 /* Define offsetof for those systems which lack it. */
5071
5072 #ifndef offsetof
5073 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5074 #endif
5075
5076
5077 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5078
5079 static int
5080 elfcore_make_pid (abfd)
5081 bfd* abfd;
5082 {
5083 return ((elf_tdata (abfd)->core_lwpid << 16)
5084 + (elf_tdata (abfd)->core_pid));
5085 }
5086
5087
5088 /* If there isn't a section called NAME, make one, using
5089 data from SECT. Note, this function will generate a
5090 reference to NAME, so you shouldn't deallocate or
5091 overwrite it. */
5092
5093 static boolean
5094 elfcore_maybe_make_sect (abfd, name, sect)
5095 bfd* abfd;
5096 char* name;
5097 asection* sect;
5098 {
5099 asection* sect2;
5100
5101 if (bfd_get_section_by_name (abfd, name) != NULL)
5102 return true;
5103
5104 sect2 = bfd_make_section (abfd, name);
5105 if (sect2 == NULL)
5106 return false;
5107
5108 sect2->_raw_size = sect->_raw_size;
5109 sect2->filepos = sect->filepos;
5110 sect2->flags = sect->flags;
5111 sect2->alignment_power = sect->alignment_power;
5112 return true;
5113 }
5114
5115
5116 /* prstatus_t exists on:
5117 solaris 2.5+
5118 linux 2.[01] + glibc
5119 unixware 4.2
5120 */
5121
5122 #if defined (HAVE_PRSTATUS_T)
5123 static boolean
5124 elfcore_grok_prstatus (abfd, note)
5125 bfd* abfd;
5126 Elf_Internal_Note* note;
5127 {
5128 char buf[100];
5129 char* name;
5130 asection* sect;
5131 int raw_size;
5132
5133 if (note->descsz == sizeof (prstatus_t))
5134 {
5135 prstatus_t prstat;
5136
5137 raw_size = sizeof (prstat.pr_reg);
5138 memcpy (&prstat, note->descdata, sizeof (prstat));
5139
5140 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5141 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5142
5143 /* pr_who exists on:
5144 solaris 2.5+
5145 unixware 4.2
5146 pr_who doesn't exist on:
5147 linux 2.[01]
5148 */
5149 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5150 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5151 #endif
5152 }
5153 #if defined (__sparcv9)
5154 else if (note->descsz == sizeof (prstatus32_t))
5155 {
5156 /* 64-bit host, 32-bit corefile */
5157 prstatus32_t prstat;
5158
5159 raw_size = sizeof (prstat.pr_reg);
5160 memcpy (&prstat, note->descdata, sizeof (prstat));
5161
5162 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5163 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5164
5165 /* pr_who exists on:
5166 solaris 2.5+
5167 unixware 4.2
5168 pr_who doesn't exist on:
5169 linux 2.[01]
5170 */
5171 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5172 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5173 #endif
5174 }
5175 #endif /* __sparcv9 */
5176 else
5177 {
5178 /* Fail - we don't know how to handle any other
5179 note size (ie. data object type). */
5180 return true;
5181 }
5182
5183 /* Make a ".reg/999" section. */
5184
5185 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5186 name = bfd_alloc (abfd, strlen (buf) + 1);
5187 if (name == NULL)
5188 return false;
5189 strcpy (name, buf);
5190
5191 sect = bfd_make_section (abfd, name);
5192 if (sect == NULL)
5193 return false;
5194
5195 if (note->descsz == sizeof (prstatus_t))
5196 {
5197 sect->_raw_size = raw_size;
5198 sect->filepos = note->descpos + offsetof (prstatus_t, pr_reg);
5199 }
5200 #if defined (__sparcv9)
5201 else if (note->descsz == sizeof (prstatus32_t))
5202 {
5203 sect->_raw_size = raw_size;
5204 sect->filepos = note->descpos + offsetof (prstatus32_t, pr_reg);
5205 }
5206 #endif
5207
5208 sect->flags = SEC_HAS_CONTENTS;
5209 sect->alignment_power = 2;
5210
5211 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5212 return false;
5213
5214 return true;
5215 }
5216 #endif /* defined (HAVE_PRSTATUS_T) */
5217
5218
5219 /* Create a pseudosection containing the exact contents of NOTE. This
5220 actually creates up to two pseudosections:
5221 - For the single-threaded case, a section named NAME, unless
5222 such a section already exists.
5223 - For the multi-threaded case, a section named "NAME/PID", where
5224 PID is elfcore_make_pid (abfd).
5225 Both pseudosections have identical contents: the contents of NOTE. */
5226
5227 static boolean
5228 elfcore_make_note_pseudosection (abfd, name, note)
5229 bfd* abfd;
5230 char *name;
5231 Elf_Internal_Note* note;
5232 {
5233 char buf[100];
5234 char *threaded_name;
5235 asection* sect;
5236
5237 /* Build the section name. */
5238
5239 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5240 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5241 if (threaded_name == NULL)
5242 return false;
5243 strcpy (threaded_name, buf);
5244
5245 sect = bfd_make_section (abfd, threaded_name);
5246 if (sect == NULL)
5247 return false;
5248 sect->_raw_size = note->descsz;
5249 sect->filepos = note->descpos;
5250 sect->flags = SEC_HAS_CONTENTS;
5251 sect->alignment_power = 2;
5252
5253 if (! elfcore_maybe_make_sect (abfd, name, sect))
5254 return false;
5255
5256 return true;
5257 }
5258
5259
5260 /* There isn't a consistent prfpregset_t across platforms,
5261 but it doesn't matter, because we don't have to pick this
5262 data structure apart. */
5263 static boolean
5264 elfcore_grok_prfpreg (abfd, note)
5265 bfd* abfd;
5266 Elf_Internal_Note* note;
5267 {
5268 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5269 }
5270
5271
5272 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5273 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5274 literally. */
5275 static boolean
5276 elfcore_grok_prxfpreg (abfd, note)
5277 bfd* abfd;
5278 Elf_Internal_Note* note;
5279 {
5280 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5281 }
5282
5283
5284 #if defined (HAVE_PRPSINFO_T)
5285 typedef prpsinfo_t elfcore_psinfo_t;
5286 #if defined (__sparcv9) /* Sparc64 cross Sparc32 */
5287 typedef prpsinfo32_t elfcore_psinfo32_t;
5288 #endif
5289 #endif
5290
5291 #if defined (HAVE_PSINFO_T)
5292 typedef psinfo_t elfcore_psinfo_t;
5293 #if defined (__sparcv9) /* Sparc64 cross Sparc32 */
5294 typedef psinfo32_t elfcore_psinfo32_t;
5295 #endif
5296 #endif
5297
5298
5299 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5300
5301 /* return a malloc'ed copy of a string at START which is at
5302 most MAX bytes long, possibly without a terminating '\0'.
5303 the copy will always have a terminating '\0'. */
5304
5305 static char*
5306 elfcore_strndup (abfd, start, max)
5307 bfd* abfd;
5308 char* start;
5309 int max;
5310 {
5311 char* dup;
5312 char* end = memchr (start, '\0', max);
5313 int len;
5314
5315 if (end == NULL)
5316 len = max;
5317 else
5318 len = end - start;
5319
5320 dup = bfd_alloc (abfd, len + 1);
5321 if (dup == NULL)
5322 return NULL;
5323
5324 memcpy (dup, start, len);
5325 dup[len] = '\0';
5326
5327 return dup;
5328 }
5329
5330 static boolean
5331 elfcore_grok_psinfo (abfd, note)
5332 bfd* abfd;
5333 Elf_Internal_Note* note;
5334 {
5335 if (note->descsz == sizeof (elfcore_psinfo_t))
5336 {
5337 elfcore_psinfo_t psinfo;
5338
5339 memcpy (&psinfo, note->descdata, note->descsz);
5340
5341 elf_tdata (abfd)->core_program
5342 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5343
5344 elf_tdata (abfd)->core_command
5345 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5346 }
5347 #if defined (__sparcv9)
5348 else if (note->descsz == sizeof (elfcore_psinfo32_t))
5349 {
5350 /* 64-bit host, 32-bit corefile */
5351 elfcore_psinfo32_t psinfo;
5352
5353 memcpy (&psinfo, note->descdata, note->descsz);
5354
5355 elf_tdata (abfd)->core_program
5356 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5357
5358 elf_tdata (abfd)->core_command
5359 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5360 }
5361 #endif
5362
5363 else
5364 {
5365 /* Fail - we don't know how to handle any other
5366 note size (ie. data object type). */
5367 return true;
5368 }
5369
5370 /* Note that for some reason, a spurious space is tacked
5371 onto the end of the args in some (at least one anyway)
5372 implementations, so strip it off if it exists. */
5373
5374 {
5375 char* command = elf_tdata (abfd)->core_command;
5376 int n = strlen (command);
5377
5378 if (0 < n && command[n - 1] == ' ')
5379 command[n - 1] = '\0';
5380 }
5381
5382 return true;
5383 }
5384 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5385
5386
5387 #if defined (HAVE_PSTATUS_T)
5388 static boolean
5389 elfcore_grok_pstatus (abfd, note)
5390 bfd* abfd;
5391 Elf_Internal_Note* note;
5392 {
5393 if (note->descsz == sizeof (pstatus_t))
5394 {
5395 pstatus_t pstat;
5396
5397 memcpy (&pstat, note->descdata, sizeof (pstat));
5398
5399 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5400 }
5401 #if defined (__sparcv9)
5402 else if (note->descsz == sizeof (pstatus32_t))
5403 {
5404 /* 64-bit host, 32-bit corefile */
5405 pstatus32_t pstat;
5406
5407 memcpy (&pstat, note->descdata, sizeof (pstat));
5408
5409 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5410 }
5411 #endif
5412 /* Could grab some more details from the "representative"
5413 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5414 NT_LWPSTATUS note, presumably. */
5415
5416 return true;
5417 }
5418 #endif /* defined (HAVE_PSTATUS_T) */
5419
5420
5421 #if defined (HAVE_LWPSTATUS_T)
5422 static boolean
5423 elfcore_grok_lwpstatus (abfd, note)
5424 bfd* abfd;
5425 Elf_Internal_Note* note;
5426 {
5427 lwpstatus_t lwpstat;
5428 char buf[100];
5429 char* name;
5430 asection* sect;
5431
5432 if (note->descsz != sizeof (lwpstat))
5433 return true;
5434
5435 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5436
5437 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5438 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5439
5440 /* Make a ".reg/999" section. */
5441
5442 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5443 name = bfd_alloc (abfd, strlen (buf) + 1);
5444 if (name == NULL)
5445 return false;
5446 strcpy (name, buf);
5447
5448 sect = bfd_make_section (abfd, name);
5449 if (sect == NULL)
5450 return false;
5451
5452 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5453 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5454 sect->filepos = note->descpos
5455 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5456 #endif
5457
5458 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5459 sect->_raw_size = sizeof (lwpstat.pr_reg);
5460 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5461 #endif
5462
5463 sect->flags = SEC_HAS_CONTENTS;
5464 sect->alignment_power = 2;
5465
5466 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5467 return false;
5468
5469 /* Make a ".reg2/999" section */
5470
5471 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5472 name = bfd_alloc (abfd, strlen (buf) + 1);
5473 if (name == NULL)
5474 return false;
5475 strcpy (name, buf);
5476
5477 sect = bfd_make_section (abfd, name);
5478 if (sect == NULL)
5479 return false;
5480
5481 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5482 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5483 sect->filepos = note->descpos
5484 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5485 #endif
5486
5487 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5488 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5489 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5490 #endif
5491
5492 sect->flags = SEC_HAS_CONTENTS;
5493 sect->alignment_power = 2;
5494
5495 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5496 return false;
5497
5498 return true;
5499 }
5500 #endif /* defined (HAVE_LWPSTATUS_T) */
5501
5502 #if defined (HAVE_WIN32_PSTATUS_T)
5503 static boolean
5504 elfcore_grok_win32pstatus (abfd, note)
5505 bfd * abfd;
5506 Elf_Internal_Note * note;
5507 {
5508 char buf[30];
5509 char * name;
5510 asection * sect;
5511 win32_pstatus_t pstatus;
5512
5513 if (note->descsz < sizeof (pstatus))
5514 return true;
5515
5516 memcpy (& pstatus, note->descdata, note->descsz);
5517
5518 switch (pstatus.data_type)
5519 {
5520 case NOTE_INFO_PROCESS:
5521 /* FIXME: need to add ->core_command. */
5522 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
5523 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
5524 break ;
5525
5526 case NOTE_INFO_THREAD:
5527 /* Make a ".reg/999" section. */
5528 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
5529
5530 name = bfd_alloc (abfd, strlen (buf) + 1);
5531 if (name == NULL)
5532 return false;
5533
5534 strcpy (name, buf);
5535
5536 sect = bfd_make_section (abfd, name);
5537 if (sect == NULL)
5538 return false;
5539
5540 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
5541 sect->filepos = note->descpos + offsetof (struct win32_pstatus,
5542 data.thread_info.thread_context);
5543 sect->flags = SEC_HAS_CONTENTS;
5544 sect->alignment_power = 2;
5545
5546 if (pstatus.data.thread_info.is_active_thread)
5547 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5548 return false;
5549 break;
5550
5551 case NOTE_INFO_MODULE:
5552 /* Make a ".module/xxxxxxxx" section. */
5553 sprintf (buf, ".module/%08x" , pstatus.data.module_info.base_address);
5554
5555 name = bfd_alloc (abfd, strlen (buf) + 1);
5556 if (name == NULL)
5557 return false;
5558
5559 strcpy (name, buf);
5560
5561 sect = bfd_make_section (abfd, name);
5562
5563 if (sect == NULL)
5564 return false;
5565
5566 sect->_raw_size = note->descsz;
5567 sect->filepos = note->descpos;
5568 sect->flags = SEC_HAS_CONTENTS;
5569 sect->alignment_power = 2;
5570 break;
5571
5572 default:
5573 return true;
5574 }
5575
5576 return true;
5577 }
5578 #endif /* HAVE_WIN32_PSTATUS_T */
5579
5580 static boolean
5581 elfcore_grok_note (abfd, note)
5582 bfd* abfd;
5583 Elf_Internal_Note* note;
5584 {
5585 switch (note->type)
5586 {
5587 default:
5588 return true;
5589
5590 #if defined (HAVE_PRSTATUS_T)
5591 case NT_PRSTATUS:
5592 return elfcore_grok_prstatus (abfd, note);
5593 #endif
5594
5595 #if defined (HAVE_PSTATUS_T)
5596 case NT_PSTATUS:
5597 return elfcore_grok_pstatus (abfd, note);
5598 #endif
5599
5600 #if defined (HAVE_LWPSTATUS_T)
5601 case NT_LWPSTATUS:
5602 return elfcore_grok_lwpstatus (abfd, note);
5603 #endif
5604
5605 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5606 return elfcore_grok_prfpreg (abfd, note);
5607
5608 #if defined (HAVE_WIN32_PSTATUS_T)
5609 case NT_WIN32PSTATUS:
5610 return elfcore_grok_win32pstatus (abfd, note);
5611 #endif
5612
5613 case NT_PRXFPREG: /* Linux SSE extension */
5614 if (note->namesz == 5
5615 && ! strcmp (note->namedata, "LINUX"))
5616 return elfcore_grok_prxfpreg (abfd, note);
5617 else
5618 return true;
5619
5620 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5621 case NT_PRPSINFO:
5622 case NT_PSINFO:
5623 return elfcore_grok_psinfo (abfd, note);
5624 #endif
5625 }
5626 }
5627
5628
5629 static boolean
5630 elfcore_read_notes (abfd, offset, size)
5631 bfd* abfd;
5632 bfd_vma offset;
5633 bfd_vma size;
5634 {
5635 char* buf;
5636 char* p;
5637
5638 if (size <= 0)
5639 return true;
5640
5641 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5642 return false;
5643
5644 buf = bfd_malloc ((size_t) size);
5645 if (buf == NULL)
5646 return false;
5647
5648 if (bfd_read (buf, size, 1, abfd) != size)
5649 {
5650 error:
5651 free (buf);
5652 return false;
5653 }
5654
5655 p = buf;
5656 while (p < buf + size)
5657 {
5658 /* FIXME: bad alignment assumption. */
5659 Elf_External_Note* xnp = (Elf_External_Note*) p;
5660 Elf_Internal_Note in;
5661
5662 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5663
5664 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5665 in.namedata = xnp->name;
5666
5667 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5668 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5669 in.descpos = offset + (in.descdata - buf);
5670
5671 if (! elfcore_grok_note (abfd, &in))
5672 goto error;
5673
5674 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5675 }
5676
5677 free (buf);
5678 return true;
5679 }
5680
5681
5682 /* FIXME: This function is now unnecessary. Callers can just call
5683 bfd_section_from_phdr directly. */
5684
5685 boolean
5686 _bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5687 bfd* abfd;
5688 Elf_Internal_Phdr* phdr;
5689 int sec_num;
5690 {
5691 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5692 return false;
5693
5694 return true;
5695 }
5696
5697
5698 \f
5699 /* Providing external access to the ELF program header table. */
5700
5701 /* Return an upper bound on the number of bytes required to store a
5702 copy of ABFD's program header table entries. Return -1 if an error
5703 occurs; bfd_get_error will return an appropriate code. */
5704 long
5705 bfd_get_elf_phdr_upper_bound (abfd)
5706 bfd *abfd;
5707 {
5708 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5709 {
5710 bfd_set_error (bfd_error_wrong_format);
5711 return -1;
5712 }
5713
5714 return (elf_elfheader (abfd)->e_phnum
5715 * sizeof (Elf_Internal_Phdr));
5716 }
5717
5718
5719 /* Copy ABFD's program header table entries to *PHDRS. The entries
5720 will be stored as an array of Elf_Internal_Phdr structures, as
5721 defined in include/elf/internal.h. To find out how large the
5722 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5723
5724 Return the number of program header table entries read, or -1 if an
5725 error occurs; bfd_get_error will return an appropriate code. */
5726 int
5727 bfd_get_elf_phdrs (abfd, phdrs)
5728 bfd *abfd;
5729 void *phdrs;
5730 {
5731 int num_phdrs;
5732
5733 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5734 {
5735 bfd_set_error (bfd_error_wrong_format);
5736 return -1;
5737 }
5738
5739 num_phdrs = elf_elfheader (abfd)->e_phnum;
5740 memcpy (phdrs, elf_tdata (abfd)->phdr,
5741 num_phdrs * sizeof (Elf_Internal_Phdr));
5742
5743 return num_phdrs;
5744 }
This page took 0.172277 seconds and 5 git commands to generate.