be0025d0a4d0a489f377fdd0e9cc39443a826faf
[deliverable/binutils-gdb.git] / bfd / elf.c
1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 /* SECTION
22
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 /* For sparc64-cross-sparc32. */
34 #define _SYSCALL32
35 #include "bfd.h"
36 #include "sysdep.h"
37 #include "bfdlink.h"
38 #include "libbfd.h"
39 #define ARCH_SIZE 0
40 #include "elf-bfd.h"
41 #include "libiberty.h"
42
43 static INLINE struct elf_segment_map *make_mapping
44 PARAMS ((bfd *, asection **, unsigned int, unsigned int, bfd_boolean));
45 static bfd_boolean map_sections_to_segments
46 PARAMS ((bfd *));
47 static int elf_sort_sections
48 PARAMS ((const PTR, const PTR));
49 static bfd_boolean assign_file_positions_for_segments
50 PARAMS ((bfd *));
51 static bfd_boolean assign_file_positions_except_relocs
52 PARAMS ((bfd *));
53 static bfd_boolean prep_headers
54 PARAMS ((bfd *));
55 static bfd_boolean swap_out_syms
56 PARAMS ((bfd *, struct bfd_strtab_hash **, int));
57 static bfd_boolean copy_private_bfd_data
58 PARAMS ((bfd *, bfd *));
59 static char *elf_read
60 PARAMS ((bfd *, file_ptr, bfd_size_type));
61 static const char *group_signature
62 PARAMS ((bfd *, Elf_Internal_Shdr *));
63 static bfd_boolean setup_group
64 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
65 static void merge_sections_remove_hook
66 PARAMS ((bfd *, asection *));
67 static void elf_fake_sections
68 PARAMS ((bfd *, asection *, PTR));
69 static bfd_boolean assign_section_numbers
70 PARAMS ((bfd *));
71 static INLINE int sym_is_global
72 PARAMS ((bfd *, asymbol *));
73 static bfd_boolean elf_map_symbols
74 PARAMS ((bfd *));
75 static bfd_size_type get_program_header_size
76 PARAMS ((bfd *));
77 static bfd_boolean elfcore_read_notes
78 PARAMS ((bfd *, file_ptr, bfd_size_type));
79 static bfd_boolean elf_find_function
80 PARAMS ((bfd *, asection *, asymbol **, bfd_vma, const char **,
81 const char **));
82 static int elfcore_make_pid
83 PARAMS ((bfd *));
84 static bfd_boolean elfcore_maybe_make_sect
85 PARAMS ((bfd *, char *, asection *));
86 static bfd_boolean elfcore_make_note_pseudosection
87 PARAMS ((bfd *, char *, Elf_Internal_Note *));
88 static bfd_boolean elfcore_grok_prfpreg
89 PARAMS ((bfd *, Elf_Internal_Note *));
90 static bfd_boolean elfcore_grok_prxfpreg
91 PARAMS ((bfd *, Elf_Internal_Note *));
92 static bfd_boolean elfcore_grok_note
93 PARAMS ((bfd *, Elf_Internal_Note *));
94 static bfd_boolean elfcore_netbsd_get_lwpid
95 PARAMS ((Elf_Internal_Note *, int *));
96 static bfd_boolean elfcore_grok_netbsd_procinfo
97 PARAMS ((bfd *, Elf_Internal_Note *));
98 static bfd_boolean elfcore_grok_netbsd_note
99 PARAMS ((bfd *, Elf_Internal_Note *));
100
101 /* Swap version information in and out. The version information is
102 currently size independent. If that ever changes, this code will
103 need to move into elfcode.h. */
104
105 /* Swap in a Verdef structure. */
106
107 void
108 _bfd_elf_swap_verdef_in (abfd, src, dst)
109 bfd *abfd;
110 const Elf_External_Verdef *src;
111 Elf_Internal_Verdef *dst;
112 {
113 dst->vd_version = H_GET_16 (abfd, src->vd_version);
114 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
115 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
116 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
117 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
118 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
119 dst->vd_next = H_GET_32 (abfd, src->vd_next);
120 }
121
122 /* Swap out a Verdef structure. */
123
124 void
125 _bfd_elf_swap_verdef_out (abfd, src, dst)
126 bfd *abfd;
127 const Elf_Internal_Verdef *src;
128 Elf_External_Verdef *dst;
129 {
130 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
131 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
132 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
133 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
134 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
135 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
136 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
137 }
138
139 /* Swap in a Verdaux structure. */
140
141 void
142 _bfd_elf_swap_verdaux_in (abfd, src, dst)
143 bfd *abfd;
144 const Elf_External_Verdaux *src;
145 Elf_Internal_Verdaux *dst;
146 {
147 dst->vda_name = H_GET_32 (abfd, src->vda_name);
148 dst->vda_next = H_GET_32 (abfd, src->vda_next);
149 }
150
151 /* Swap out a Verdaux structure. */
152
153 void
154 _bfd_elf_swap_verdaux_out (abfd, src, dst)
155 bfd *abfd;
156 const Elf_Internal_Verdaux *src;
157 Elf_External_Verdaux *dst;
158 {
159 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
160 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
161 }
162
163 /* Swap in a Verneed structure. */
164
165 void
166 _bfd_elf_swap_verneed_in (abfd, src, dst)
167 bfd *abfd;
168 const Elf_External_Verneed *src;
169 Elf_Internal_Verneed *dst;
170 {
171 dst->vn_version = H_GET_16 (abfd, src->vn_version);
172 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
173 dst->vn_file = H_GET_32 (abfd, src->vn_file);
174 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
175 dst->vn_next = H_GET_32 (abfd, src->vn_next);
176 }
177
178 /* Swap out a Verneed structure. */
179
180 void
181 _bfd_elf_swap_verneed_out (abfd, src, dst)
182 bfd *abfd;
183 const Elf_Internal_Verneed *src;
184 Elf_External_Verneed *dst;
185 {
186 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
187 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
188 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
189 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
190 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
191 }
192
193 /* Swap in a Vernaux structure. */
194
195 void
196 _bfd_elf_swap_vernaux_in (abfd, src, dst)
197 bfd *abfd;
198 const Elf_External_Vernaux *src;
199 Elf_Internal_Vernaux *dst;
200 {
201 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
202 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
203 dst->vna_other = H_GET_16 (abfd, src->vna_other);
204 dst->vna_name = H_GET_32 (abfd, src->vna_name);
205 dst->vna_next = H_GET_32 (abfd, src->vna_next);
206 }
207
208 /* Swap out a Vernaux structure. */
209
210 void
211 _bfd_elf_swap_vernaux_out (abfd, src, dst)
212 bfd *abfd;
213 const Elf_Internal_Vernaux *src;
214 Elf_External_Vernaux *dst;
215 {
216 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
217 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
218 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
219 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
220 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
221 }
222
223 /* Swap in a Versym structure. */
224
225 void
226 _bfd_elf_swap_versym_in (abfd, src, dst)
227 bfd *abfd;
228 const Elf_External_Versym *src;
229 Elf_Internal_Versym *dst;
230 {
231 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
232 }
233
234 /* Swap out a Versym structure. */
235
236 void
237 _bfd_elf_swap_versym_out (abfd, src, dst)
238 bfd *abfd;
239 const Elf_Internal_Versym *src;
240 Elf_External_Versym *dst;
241 {
242 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
243 }
244
245 /* Standard ELF hash function. Do not change this function; you will
246 cause invalid hash tables to be generated. */
247
248 unsigned long
249 bfd_elf_hash (namearg)
250 const char *namearg;
251 {
252 const unsigned char *name = (const unsigned char *) namearg;
253 unsigned long h = 0;
254 unsigned long g;
255 int ch;
256
257 while ((ch = *name++) != '\0')
258 {
259 h = (h << 4) + ch;
260 if ((g = (h & 0xf0000000)) != 0)
261 {
262 h ^= g >> 24;
263 /* The ELF ABI says `h &= ~g', but this is equivalent in
264 this case and on some machines one insn instead of two. */
265 h ^= g;
266 }
267 }
268 return h;
269 }
270
271 /* Read a specified number of bytes at a specified offset in an ELF
272 file, into a newly allocated buffer, and return a pointer to the
273 buffer. */
274
275 static char *
276 elf_read (abfd, offset, size)
277 bfd *abfd;
278 file_ptr offset;
279 bfd_size_type size;
280 {
281 char *buf;
282
283 if ((buf = bfd_alloc (abfd, size)) == NULL)
284 return NULL;
285 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
286 return NULL;
287 if (bfd_bread ((PTR) buf, size, abfd) != size)
288 {
289 if (bfd_get_error () != bfd_error_system_call)
290 bfd_set_error (bfd_error_file_truncated);
291 return NULL;
292 }
293 return buf;
294 }
295
296 bfd_boolean
297 bfd_elf_mkobject (abfd)
298 bfd *abfd;
299 {
300 /* This just does initialization. */
301 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
302 bfd_size_type amt = sizeof (struct elf_obj_tdata);
303 elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt);
304 if (elf_tdata (abfd) == 0)
305 return FALSE;
306 /* Since everything is done at close time, do we need any
307 initialization? */
308
309 return TRUE;
310 }
311
312 bfd_boolean
313 bfd_elf_mkcorefile (abfd)
314 bfd *abfd;
315 {
316 /* I think this can be done just like an object file. */
317 return bfd_elf_mkobject (abfd);
318 }
319
320 char *
321 bfd_elf_get_str_section (abfd, shindex)
322 bfd *abfd;
323 unsigned int shindex;
324 {
325 Elf_Internal_Shdr **i_shdrp;
326 char *shstrtab = NULL;
327 file_ptr offset;
328 bfd_size_type shstrtabsize;
329
330 i_shdrp = elf_elfsections (abfd);
331 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
332 return 0;
333
334 shstrtab = (char *) i_shdrp[shindex]->contents;
335 if (shstrtab == NULL)
336 {
337 /* No cached one, attempt to read, and cache what we read. */
338 offset = i_shdrp[shindex]->sh_offset;
339 shstrtabsize = i_shdrp[shindex]->sh_size;
340 shstrtab = elf_read (abfd, offset, shstrtabsize);
341 i_shdrp[shindex]->contents = (PTR) shstrtab;
342 }
343 return shstrtab;
344 }
345
346 char *
347 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
348 bfd *abfd;
349 unsigned int shindex;
350 unsigned int strindex;
351 {
352 Elf_Internal_Shdr *hdr;
353
354 if (strindex == 0)
355 return "";
356
357 hdr = elf_elfsections (abfd)[shindex];
358
359 if (hdr->contents == NULL
360 && bfd_elf_get_str_section (abfd, shindex) == NULL)
361 return NULL;
362
363 if (strindex >= hdr->sh_size)
364 {
365 (*_bfd_error_handler)
366 (_("%s: invalid string offset %u >= %lu for section `%s'"),
367 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
368 ((shindex == elf_elfheader(abfd)->e_shstrndx
369 && strindex == hdr->sh_name)
370 ? ".shstrtab"
371 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
372 return "";
373 }
374
375 return ((char *) hdr->contents) + strindex;
376 }
377
378 /* Read and convert symbols to internal format.
379 SYMCOUNT specifies the number of symbols to read, starting from
380 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
381 are non-NULL, they are used to store the internal symbols, external
382 symbols, and symbol section index extensions, respectively. */
383
384 Elf_Internal_Sym *
385 bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, symoffset,
386 intsym_buf, extsym_buf, extshndx_buf)
387 bfd *ibfd;
388 Elf_Internal_Shdr *symtab_hdr;
389 size_t symcount;
390 size_t symoffset;
391 Elf_Internal_Sym *intsym_buf;
392 PTR extsym_buf;
393 Elf_External_Sym_Shndx *extshndx_buf;
394 {
395 Elf_Internal_Shdr *shndx_hdr;
396 PTR alloc_ext;
397 const bfd_byte *esym;
398 Elf_External_Sym_Shndx *alloc_extshndx;
399 Elf_External_Sym_Shndx *shndx;
400 Elf_Internal_Sym *isym;
401 Elf_Internal_Sym *isymend;
402 struct elf_backend_data *bed;
403 size_t extsym_size;
404 bfd_size_type amt;
405 file_ptr pos;
406
407 if (symcount == 0)
408 return intsym_buf;
409
410 /* Normal syms might have section extension entries. */
411 shndx_hdr = NULL;
412 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
413 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
414
415 /* Read the symbols. */
416 alloc_ext = NULL;
417 alloc_extshndx = NULL;
418 bed = get_elf_backend_data (ibfd);
419 extsym_size = bed->s->sizeof_sym;
420 amt = symcount * extsym_size;
421 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
422 if (extsym_buf == NULL)
423 {
424 alloc_ext = bfd_malloc (amt);
425 extsym_buf = alloc_ext;
426 }
427 if (extsym_buf == NULL
428 || bfd_seek (ibfd, pos, SEEK_SET) != 0
429 || bfd_bread (extsym_buf, amt, ibfd) != amt)
430 {
431 intsym_buf = NULL;
432 goto out;
433 }
434
435 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
436 extshndx_buf = NULL;
437 else
438 {
439 amt = symcount * sizeof (Elf_External_Sym_Shndx);
440 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
441 if (extshndx_buf == NULL)
442 {
443 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
444 extshndx_buf = alloc_extshndx;
445 }
446 if (extshndx_buf == NULL
447 || bfd_seek (ibfd, pos, SEEK_SET) != 0
448 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
449 {
450 intsym_buf = NULL;
451 goto out;
452 }
453 }
454
455 if (intsym_buf == NULL)
456 {
457 bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym);
458 intsym_buf = (Elf_Internal_Sym *) bfd_malloc (amt);
459 if (intsym_buf == NULL)
460 goto out;
461 }
462
463 /* Convert the symbols to internal form. */
464 isymend = intsym_buf + symcount;
465 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
466 isym < isymend;
467 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
468 (*bed->s->swap_symbol_in) (ibfd, esym, (const PTR) shndx, isym);
469
470 out:
471 if (alloc_ext != NULL)
472 free (alloc_ext);
473 if (alloc_extshndx != NULL)
474 free (alloc_extshndx);
475
476 return intsym_buf;
477 }
478
479 /* Look up a symbol name. */
480 const char *
481 bfd_elf_local_sym_name (abfd, isym)
482 bfd *abfd;
483 Elf_Internal_Sym *isym;
484 {
485 unsigned int iname = isym->st_name;
486 unsigned int shindex = elf_tdata (abfd)->symtab_hdr.sh_link;
487 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION)
488 {
489 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
490 shindex = elf_elfheader (abfd)->e_shstrndx;
491 }
492
493 return bfd_elf_string_from_elf_section (abfd, shindex, iname);
494 }
495
496 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
497 sections. The first element is the flags, the rest are section
498 pointers. */
499
500 typedef union elf_internal_group {
501 Elf_Internal_Shdr *shdr;
502 unsigned int flags;
503 } Elf_Internal_Group;
504
505 /* Return the name of the group signature symbol. Why isn't the
506 signature just a string? */
507
508 static const char *
509 group_signature (abfd, ghdr)
510 bfd *abfd;
511 Elf_Internal_Shdr *ghdr;
512 {
513 Elf_Internal_Shdr *hdr;
514 unsigned char esym[sizeof (Elf64_External_Sym)];
515 Elf_External_Sym_Shndx eshndx;
516 Elf_Internal_Sym isym;
517
518 /* First we need to ensure the symbol table is available. */
519 if (! bfd_section_from_shdr (abfd, ghdr->sh_link))
520 return NULL;
521
522 /* Go read the symbol. */
523 hdr = &elf_tdata (abfd)->symtab_hdr;
524 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
525 &isym, esym, &eshndx) == NULL)
526 return NULL;
527
528 return bfd_elf_local_sym_name (abfd, &isym);
529 }
530
531 /* Set next_in_group list pointer, and group name for NEWSECT. */
532
533 static bfd_boolean
534 setup_group (abfd, hdr, newsect)
535 bfd *abfd;
536 Elf_Internal_Shdr *hdr;
537 asection *newsect;
538 {
539 unsigned int num_group = elf_tdata (abfd)->num_group;
540
541 /* If num_group is zero, read in all SHT_GROUP sections. The count
542 is set to -1 if there are no SHT_GROUP sections. */
543 if (num_group == 0)
544 {
545 unsigned int i, shnum;
546
547 /* First count the number of groups. If we have a SHT_GROUP
548 section with just a flag word (ie. sh_size is 4), ignore it. */
549 shnum = elf_numsections (abfd);
550 num_group = 0;
551 for (i = 0; i < shnum; i++)
552 {
553 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
554 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
555 num_group += 1;
556 }
557
558 if (num_group == 0)
559 num_group = (unsigned) -1;
560 elf_tdata (abfd)->num_group = num_group;
561
562 if (num_group > 0)
563 {
564 /* We keep a list of elf section headers for group sections,
565 so we can find them quickly. */
566 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
567 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
568 if (elf_tdata (abfd)->group_sect_ptr == NULL)
569 return FALSE;
570
571 num_group = 0;
572 for (i = 0; i < shnum; i++)
573 {
574 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
575 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
576 {
577 unsigned char *src;
578 Elf_Internal_Group *dest;
579
580 /* Add to list of sections. */
581 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
582 num_group += 1;
583
584 /* Read the raw contents. */
585 BFD_ASSERT (sizeof (*dest) >= 4);
586 amt = shdr->sh_size * sizeof (*dest) / 4;
587 shdr->contents = bfd_alloc (abfd, amt);
588 if (shdr->contents == NULL
589 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
590 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
591 != shdr->sh_size))
592 return FALSE;
593
594 /* Translate raw contents, a flag word followed by an
595 array of elf section indices all in target byte order,
596 to the flag word followed by an array of elf section
597 pointers. */
598 src = shdr->contents + shdr->sh_size;
599 dest = (Elf_Internal_Group *) (shdr->contents + amt);
600 while (1)
601 {
602 unsigned int idx;
603
604 src -= 4;
605 --dest;
606 idx = H_GET_32 (abfd, src);
607 if (src == shdr->contents)
608 {
609 dest->flags = idx;
610 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
611 shdr->bfd_section->flags
612 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
613 break;
614 }
615 if (idx >= shnum)
616 {
617 ((*_bfd_error_handler)
618 (_("%s: invalid SHT_GROUP entry"),
619 bfd_archive_filename (abfd)));
620 idx = 0;
621 }
622 dest->shdr = elf_elfsections (abfd)[idx];
623 }
624 }
625 }
626 }
627 }
628
629 if (num_group != (unsigned) -1)
630 {
631 unsigned int i;
632
633 for (i = 0; i < num_group; i++)
634 {
635 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
636 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
637 unsigned int n_elt = shdr->sh_size / 4;
638
639 /* Look through this group's sections to see if current
640 section is a member. */
641 while (--n_elt != 0)
642 if ((++idx)->shdr == hdr)
643 {
644 asection *s = NULL;
645
646 /* We are a member of this group. Go looking through
647 other members to see if any others are linked via
648 next_in_group. */
649 idx = (Elf_Internal_Group *) shdr->contents;
650 n_elt = shdr->sh_size / 4;
651 while (--n_elt != 0)
652 if ((s = (++idx)->shdr->bfd_section) != NULL
653 && elf_next_in_group (s) != NULL)
654 break;
655 if (n_elt != 0)
656 {
657 /* Snarf the group name from other member, and
658 insert current section in circular list. */
659 elf_group_name (newsect) = elf_group_name (s);
660 elf_next_in_group (newsect) = elf_next_in_group (s);
661 elf_next_in_group (s) = newsect;
662 }
663 else
664 {
665 const char *gname;
666
667 gname = group_signature (abfd, shdr);
668 if (gname == NULL)
669 return FALSE;
670 elf_group_name (newsect) = gname;
671
672 /* Start a circular list with one element. */
673 elf_next_in_group (newsect) = newsect;
674 }
675
676 /* If the group section has been created, point to the
677 new member. */
678 if (shdr->bfd_section != NULL)
679 elf_next_in_group (shdr->bfd_section) = newsect;
680
681 i = num_group - 1;
682 break;
683 }
684 }
685 }
686
687 if (elf_group_name (newsect) == NULL)
688 {
689 (*_bfd_error_handler) (_("%s: no group info for section %s"),
690 bfd_archive_filename (abfd), newsect->name);
691 }
692 return TRUE;
693 }
694
695 bfd_boolean
696 bfd_elf_discard_group (abfd, group)
697 bfd *abfd ATTRIBUTE_UNUSED;
698 asection *group;
699 {
700 asection *first = elf_next_in_group (group);
701 asection *s = first;
702
703 while (s != NULL)
704 {
705 s->output_section = bfd_abs_section_ptr;
706 s = elf_next_in_group (s);
707 /* These lists are circular. */
708 if (s == first)
709 break;
710 }
711 return TRUE;
712 }
713
714 /* Make a BFD section from an ELF section. We store a pointer to the
715 BFD section in the bfd_section field of the header. */
716
717 bfd_boolean
718 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
719 bfd *abfd;
720 Elf_Internal_Shdr *hdr;
721 const char *name;
722 {
723 asection *newsect;
724 flagword flags;
725 struct elf_backend_data *bed;
726
727 if (hdr->bfd_section != NULL)
728 {
729 BFD_ASSERT (strcmp (name,
730 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
731 return TRUE;
732 }
733
734 newsect = bfd_make_section_anyway (abfd, name);
735 if (newsect == NULL)
736 return FALSE;
737
738 newsect->filepos = hdr->sh_offset;
739
740 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
741 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
742 || ! bfd_set_section_alignment (abfd, newsect,
743 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
744 return FALSE;
745
746 flags = SEC_NO_FLAGS;
747 if (hdr->sh_type != SHT_NOBITS)
748 flags |= SEC_HAS_CONTENTS;
749 if (hdr->sh_type == SHT_GROUP)
750 flags |= SEC_GROUP | SEC_EXCLUDE;
751 if ((hdr->sh_flags & SHF_ALLOC) != 0)
752 {
753 flags |= SEC_ALLOC;
754 if (hdr->sh_type != SHT_NOBITS)
755 flags |= SEC_LOAD;
756 }
757 if ((hdr->sh_flags & SHF_WRITE) == 0)
758 flags |= SEC_READONLY;
759 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
760 flags |= SEC_CODE;
761 else if ((flags & SEC_LOAD) != 0)
762 flags |= SEC_DATA;
763 if ((hdr->sh_flags & SHF_MERGE) != 0)
764 {
765 flags |= SEC_MERGE;
766 newsect->entsize = hdr->sh_entsize;
767 if ((hdr->sh_flags & SHF_STRINGS) != 0)
768 flags |= SEC_STRINGS;
769 }
770 if (hdr->sh_flags & SHF_GROUP)
771 if (!setup_group (abfd, hdr, newsect))
772 return FALSE;
773 if ((hdr->sh_flags & SHF_TLS) != 0)
774 flags |= SEC_THREAD_LOCAL;
775
776 /* The debugging sections appear to be recognized only by name, not
777 any sort of flag. */
778 {
779 static const char *debug_sec_names [] =
780 {
781 ".debug",
782 ".gnu.linkonce.wi.",
783 ".line",
784 ".stab"
785 };
786 int i;
787
788 for (i = ARRAY_SIZE (debug_sec_names); i--;)
789 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
790 break;
791
792 if (i >= 0)
793 flags |= SEC_DEBUGGING;
794 }
795
796 /* As a GNU extension, if the name begins with .gnu.linkonce, we
797 only link a single copy of the section. This is used to support
798 g++. g++ will emit each template expansion in its own section.
799 The symbols will be defined as weak, so that multiple definitions
800 are permitted. The GNU linker extension is to actually discard
801 all but one of the sections. */
802 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
803 && elf_next_in_group (newsect) == NULL)
804 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
805
806 bed = get_elf_backend_data (abfd);
807 if (bed->elf_backend_section_flags)
808 if (! bed->elf_backend_section_flags (&flags, hdr))
809 return FALSE;
810
811 if (! bfd_set_section_flags (abfd, newsect, flags))
812 return FALSE;
813
814 if ((flags & SEC_ALLOC) != 0)
815 {
816 Elf_Internal_Phdr *phdr;
817 unsigned int i;
818
819 /* Look through the phdrs to see if we need to adjust the lma.
820 If all the p_paddr fields are zero, we ignore them, since
821 some ELF linkers produce such output. */
822 phdr = elf_tdata (abfd)->phdr;
823 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
824 {
825 if (phdr->p_paddr != 0)
826 break;
827 }
828 if (i < elf_elfheader (abfd)->e_phnum)
829 {
830 phdr = elf_tdata (abfd)->phdr;
831 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
832 {
833 /* This section is part of this segment if its file
834 offset plus size lies within the segment's memory
835 span and, if the section is loaded, the extent of the
836 loaded data lies within the extent of the segment.
837
838 Note - we used to check the p_paddr field as well, and
839 refuse to set the LMA if it was 0. This is wrong
840 though, as a perfectly valid initialised segment can
841 have a p_paddr of zero. Some architectures, eg ARM,
842 place special significance on the address 0 and
843 executables need to be able to have a segment which
844 covers this address. */
845 if (phdr->p_type == PT_LOAD
846 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
847 && (hdr->sh_offset + hdr->sh_size
848 <= phdr->p_offset + phdr->p_memsz)
849 && ((flags & SEC_LOAD) == 0
850 || (hdr->sh_offset + hdr->sh_size
851 <= phdr->p_offset + phdr->p_filesz)))
852 {
853 if ((flags & SEC_LOAD) == 0)
854 newsect->lma = (phdr->p_paddr
855 + hdr->sh_addr - phdr->p_vaddr);
856 else
857 /* We used to use the same adjustment for SEC_LOAD
858 sections, but that doesn't work if the segment
859 is packed with code from multiple VMAs.
860 Instead we calculate the section LMA based on
861 the segment LMA. It is assumed that the
862 segment will contain sections with contiguous
863 LMAs, even if the VMAs are not. */
864 newsect->lma = (phdr->p_paddr
865 + hdr->sh_offset - phdr->p_offset);
866
867 /* With contiguous segments, we can't tell from file
868 offsets whether a section with zero size should
869 be placed at the end of one segment or the
870 beginning of the next. Decide based on vaddr. */
871 if (hdr->sh_addr >= phdr->p_vaddr
872 && (hdr->sh_addr + hdr->sh_size
873 <= phdr->p_vaddr + phdr->p_memsz))
874 break;
875 }
876 }
877 }
878 }
879
880 hdr->bfd_section = newsect;
881 elf_section_data (newsect)->this_hdr = *hdr;
882
883 return TRUE;
884 }
885
886 /*
887 INTERNAL_FUNCTION
888 bfd_elf_find_section
889
890 SYNOPSIS
891 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
892
893 DESCRIPTION
894 Helper functions for GDB to locate the string tables.
895 Since BFD hides string tables from callers, GDB needs to use an
896 internal hook to find them. Sun's .stabstr, in particular,
897 isn't even pointed to by the .stab section, so ordinary
898 mechanisms wouldn't work to find it, even if we had some.
899 */
900
901 struct elf_internal_shdr *
902 bfd_elf_find_section (abfd, name)
903 bfd *abfd;
904 char *name;
905 {
906 Elf_Internal_Shdr **i_shdrp;
907 char *shstrtab;
908 unsigned int max;
909 unsigned int i;
910
911 i_shdrp = elf_elfsections (abfd);
912 if (i_shdrp != NULL)
913 {
914 shstrtab = bfd_elf_get_str_section (abfd,
915 elf_elfheader (abfd)->e_shstrndx);
916 if (shstrtab != NULL)
917 {
918 max = elf_numsections (abfd);
919 for (i = 1; i < max; i++)
920 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
921 return i_shdrp[i];
922 }
923 }
924 return 0;
925 }
926
927 const char *const bfd_elf_section_type_names[] = {
928 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
929 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
930 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
931 };
932
933 /* ELF relocs are against symbols. If we are producing relocateable
934 output, and the reloc is against an external symbol, and nothing
935 has given us any additional addend, the resulting reloc will also
936 be against the same symbol. In such a case, we don't want to
937 change anything about the way the reloc is handled, since it will
938 all be done at final link time. Rather than put special case code
939 into bfd_perform_relocation, all the reloc types use this howto
940 function. It just short circuits the reloc if producing
941 relocateable output against an external symbol. */
942
943 bfd_reloc_status_type
944 bfd_elf_generic_reloc (abfd,
945 reloc_entry,
946 symbol,
947 data,
948 input_section,
949 output_bfd,
950 error_message)
951 bfd *abfd ATTRIBUTE_UNUSED;
952 arelent *reloc_entry;
953 asymbol *symbol;
954 PTR data ATTRIBUTE_UNUSED;
955 asection *input_section;
956 bfd *output_bfd;
957 char **error_message ATTRIBUTE_UNUSED;
958 {
959 if (output_bfd != (bfd *) NULL
960 && (symbol->flags & BSF_SECTION_SYM) == 0
961 && (! reloc_entry->howto->partial_inplace
962 || reloc_entry->addend == 0))
963 {
964 reloc_entry->address += input_section->output_offset;
965 return bfd_reloc_ok;
966 }
967
968 return bfd_reloc_continue;
969 }
970 \f
971 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
972
973 static void
974 merge_sections_remove_hook (abfd, sec)
975 bfd *abfd ATTRIBUTE_UNUSED;
976 asection *sec;
977 {
978 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
979 sec->sec_info_type = ELF_INFO_TYPE_NONE;
980 }
981
982 /* Finish SHF_MERGE section merging. */
983
984 bfd_boolean
985 _bfd_elf_merge_sections (abfd, info)
986 bfd *abfd;
987 struct bfd_link_info *info;
988 {
989 if (!is_elf_hash_table (info))
990 return FALSE;
991 if (elf_hash_table (info)->merge_info)
992 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info,
993 merge_sections_remove_hook);
994 return TRUE;
995 }
996
997 void
998 _bfd_elf_link_just_syms (sec, info)
999 asection *sec;
1000 struct bfd_link_info *info;
1001 {
1002 sec->output_section = bfd_abs_section_ptr;
1003 sec->output_offset = sec->vma;
1004 if (!is_elf_hash_table (info))
1005 return;
1006
1007 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
1008 }
1009 \f
1010 /* Copy the program header and other data from one object module to
1011 another. */
1012
1013 bfd_boolean
1014 _bfd_elf_copy_private_bfd_data (ibfd, obfd)
1015 bfd *ibfd;
1016 bfd *obfd;
1017 {
1018 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1019 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1020 return TRUE;
1021
1022 BFD_ASSERT (!elf_flags_init (obfd)
1023 || (elf_elfheader (obfd)->e_flags
1024 == elf_elfheader (ibfd)->e_flags));
1025
1026 elf_gp (obfd) = elf_gp (ibfd);
1027 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1028 elf_flags_init (obfd) = TRUE;
1029 return TRUE;
1030 }
1031
1032 /* Print out the program headers. */
1033
1034 bfd_boolean
1035 _bfd_elf_print_private_bfd_data (abfd, farg)
1036 bfd *abfd;
1037 PTR farg;
1038 {
1039 FILE *f = (FILE *) farg;
1040 Elf_Internal_Phdr *p;
1041 asection *s;
1042 bfd_byte *dynbuf = NULL;
1043
1044 p = elf_tdata (abfd)->phdr;
1045 if (p != NULL)
1046 {
1047 unsigned int i, c;
1048
1049 fprintf (f, _("\nProgram Header:\n"));
1050 c = elf_elfheader (abfd)->e_phnum;
1051 for (i = 0; i < c; i++, p++)
1052 {
1053 const char *pt;
1054 char buf[20];
1055
1056 switch (p->p_type)
1057 {
1058 case PT_NULL: pt = "NULL"; break;
1059 case PT_LOAD: pt = "LOAD"; break;
1060 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1061 case PT_INTERP: pt = "INTERP"; break;
1062 case PT_NOTE: pt = "NOTE"; break;
1063 case PT_SHLIB: pt = "SHLIB"; break;
1064 case PT_PHDR: pt = "PHDR"; break;
1065 case PT_TLS: pt = "TLS"; break;
1066 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1067 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
1068 }
1069 fprintf (f, "%8s off 0x", pt);
1070 bfd_fprintf_vma (abfd, f, p->p_offset);
1071 fprintf (f, " vaddr 0x");
1072 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1073 fprintf (f, " paddr 0x");
1074 bfd_fprintf_vma (abfd, f, p->p_paddr);
1075 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1076 fprintf (f, " filesz 0x");
1077 bfd_fprintf_vma (abfd, f, p->p_filesz);
1078 fprintf (f, " memsz 0x");
1079 bfd_fprintf_vma (abfd, f, p->p_memsz);
1080 fprintf (f, " flags %c%c%c",
1081 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1082 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1083 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1084 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1085 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1086 fprintf (f, "\n");
1087 }
1088 }
1089
1090 s = bfd_get_section_by_name (abfd, ".dynamic");
1091 if (s != NULL)
1092 {
1093 int elfsec;
1094 unsigned long shlink;
1095 bfd_byte *extdyn, *extdynend;
1096 size_t extdynsize;
1097 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1098
1099 fprintf (f, _("\nDynamic Section:\n"));
1100
1101 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1102 if (dynbuf == NULL)
1103 goto error_return;
1104 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1105 s->_raw_size))
1106 goto error_return;
1107
1108 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1109 if (elfsec == -1)
1110 goto error_return;
1111 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1112
1113 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1114 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1115
1116 extdyn = dynbuf;
1117 extdynend = extdyn + s->_raw_size;
1118 for (; extdyn < extdynend; extdyn += extdynsize)
1119 {
1120 Elf_Internal_Dyn dyn;
1121 const char *name;
1122 char ab[20];
1123 bfd_boolean stringp;
1124
1125 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1126
1127 if (dyn.d_tag == DT_NULL)
1128 break;
1129
1130 stringp = FALSE;
1131 switch (dyn.d_tag)
1132 {
1133 default:
1134 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1135 name = ab;
1136 break;
1137
1138 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1139 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1140 case DT_PLTGOT: name = "PLTGOT"; break;
1141 case DT_HASH: name = "HASH"; break;
1142 case DT_STRTAB: name = "STRTAB"; break;
1143 case DT_SYMTAB: name = "SYMTAB"; break;
1144 case DT_RELA: name = "RELA"; break;
1145 case DT_RELASZ: name = "RELASZ"; break;
1146 case DT_RELAENT: name = "RELAENT"; break;
1147 case DT_STRSZ: name = "STRSZ"; break;
1148 case DT_SYMENT: name = "SYMENT"; break;
1149 case DT_INIT: name = "INIT"; break;
1150 case DT_FINI: name = "FINI"; break;
1151 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1152 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1153 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1154 case DT_REL: name = "REL"; break;
1155 case DT_RELSZ: name = "RELSZ"; break;
1156 case DT_RELENT: name = "RELENT"; break;
1157 case DT_PLTREL: name = "PLTREL"; break;
1158 case DT_DEBUG: name = "DEBUG"; break;
1159 case DT_TEXTREL: name = "TEXTREL"; break;
1160 case DT_JMPREL: name = "JMPREL"; break;
1161 case DT_BIND_NOW: name = "BIND_NOW"; break;
1162 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1163 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1164 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1165 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1166 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1167 case DT_FLAGS: name = "FLAGS"; break;
1168 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1169 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1170 case DT_CHECKSUM: name = "CHECKSUM"; break;
1171 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1172 case DT_MOVEENT: name = "MOVEENT"; break;
1173 case DT_MOVESZ: name = "MOVESZ"; break;
1174 case DT_FEATURE: name = "FEATURE"; break;
1175 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1176 case DT_SYMINSZ: name = "SYMINSZ"; break;
1177 case DT_SYMINENT: name = "SYMINENT"; break;
1178 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1179 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1180 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1181 case DT_PLTPAD: name = "PLTPAD"; break;
1182 case DT_MOVETAB: name = "MOVETAB"; break;
1183 case DT_SYMINFO: name = "SYMINFO"; break;
1184 case DT_RELACOUNT: name = "RELACOUNT"; break;
1185 case DT_RELCOUNT: name = "RELCOUNT"; break;
1186 case DT_FLAGS_1: name = "FLAGS_1"; break;
1187 case DT_VERSYM: name = "VERSYM"; break;
1188 case DT_VERDEF: name = "VERDEF"; break;
1189 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1190 case DT_VERNEED: name = "VERNEED"; break;
1191 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1192 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1193 case DT_USED: name = "USED"; break;
1194 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1195 }
1196
1197 fprintf (f, " %-11s ", name);
1198 if (! stringp)
1199 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1200 else
1201 {
1202 const char *string;
1203 unsigned int tagv = dyn.d_un.d_val;
1204
1205 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1206 if (string == NULL)
1207 goto error_return;
1208 fprintf (f, "%s", string);
1209 }
1210 fprintf (f, "\n");
1211 }
1212
1213 free (dynbuf);
1214 dynbuf = NULL;
1215 }
1216
1217 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1218 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1219 {
1220 if (! _bfd_elf_slurp_version_tables (abfd))
1221 return FALSE;
1222 }
1223
1224 if (elf_dynverdef (abfd) != 0)
1225 {
1226 Elf_Internal_Verdef *t;
1227
1228 fprintf (f, _("\nVersion definitions:\n"));
1229 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1230 {
1231 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1232 t->vd_flags, t->vd_hash, t->vd_nodename);
1233 if (t->vd_auxptr->vda_nextptr != NULL)
1234 {
1235 Elf_Internal_Verdaux *a;
1236
1237 fprintf (f, "\t");
1238 for (a = t->vd_auxptr->vda_nextptr;
1239 a != NULL;
1240 a = a->vda_nextptr)
1241 fprintf (f, "%s ", a->vda_nodename);
1242 fprintf (f, "\n");
1243 }
1244 }
1245 }
1246
1247 if (elf_dynverref (abfd) != 0)
1248 {
1249 Elf_Internal_Verneed *t;
1250
1251 fprintf (f, _("\nVersion References:\n"));
1252 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1253 {
1254 Elf_Internal_Vernaux *a;
1255
1256 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1257 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1258 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1259 a->vna_flags, a->vna_other, a->vna_nodename);
1260 }
1261 }
1262
1263 return TRUE;
1264
1265 error_return:
1266 if (dynbuf != NULL)
1267 free (dynbuf);
1268 return FALSE;
1269 }
1270
1271 /* Display ELF-specific fields of a symbol. */
1272
1273 void
1274 bfd_elf_print_symbol (abfd, filep, symbol, how)
1275 bfd *abfd;
1276 PTR filep;
1277 asymbol *symbol;
1278 bfd_print_symbol_type how;
1279 {
1280 FILE *file = (FILE *) filep;
1281 switch (how)
1282 {
1283 case bfd_print_symbol_name:
1284 fprintf (file, "%s", symbol->name);
1285 break;
1286 case bfd_print_symbol_more:
1287 fprintf (file, "elf ");
1288 bfd_fprintf_vma (abfd, file, symbol->value);
1289 fprintf (file, " %lx", (long) symbol->flags);
1290 break;
1291 case bfd_print_symbol_all:
1292 {
1293 const char *section_name;
1294 const char *name = NULL;
1295 struct elf_backend_data *bed;
1296 unsigned char st_other;
1297 bfd_vma val;
1298
1299 section_name = symbol->section ? symbol->section->name : "(*none*)";
1300
1301 bed = get_elf_backend_data (abfd);
1302 if (bed->elf_backend_print_symbol_all)
1303 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1304
1305 if (name == NULL)
1306 {
1307 name = symbol->name;
1308 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
1309 }
1310
1311 fprintf (file, " %s\t", section_name);
1312 /* Print the "other" value for a symbol. For common symbols,
1313 we've already printed the size; now print the alignment.
1314 For other symbols, we have no specified alignment, and
1315 we've printed the address; now print the size. */
1316 if (bfd_is_com_section (symbol->section))
1317 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1318 else
1319 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1320 bfd_fprintf_vma (abfd, file, val);
1321
1322 /* If we have version information, print it. */
1323 if (elf_tdata (abfd)->dynversym_section != 0
1324 && (elf_tdata (abfd)->dynverdef_section != 0
1325 || elf_tdata (abfd)->dynverref_section != 0))
1326 {
1327 unsigned int vernum;
1328 const char *version_string;
1329
1330 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1331
1332 if (vernum == 0)
1333 version_string = "";
1334 else if (vernum == 1)
1335 version_string = "Base";
1336 else if (vernum <= elf_tdata (abfd)->cverdefs)
1337 version_string =
1338 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1339 else
1340 {
1341 Elf_Internal_Verneed *t;
1342
1343 version_string = "";
1344 for (t = elf_tdata (abfd)->verref;
1345 t != NULL;
1346 t = t->vn_nextref)
1347 {
1348 Elf_Internal_Vernaux *a;
1349
1350 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1351 {
1352 if (a->vna_other == vernum)
1353 {
1354 version_string = a->vna_nodename;
1355 break;
1356 }
1357 }
1358 }
1359 }
1360
1361 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1362 fprintf (file, " %-11s", version_string);
1363 else
1364 {
1365 int i;
1366
1367 fprintf (file, " (%s)", version_string);
1368 for (i = 10 - strlen (version_string); i > 0; --i)
1369 putc (' ', file);
1370 }
1371 }
1372
1373 /* If the st_other field is not zero, print it. */
1374 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1375
1376 switch (st_other)
1377 {
1378 case 0: break;
1379 case STV_INTERNAL: fprintf (file, " .internal"); break;
1380 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1381 case STV_PROTECTED: fprintf (file, " .protected"); break;
1382 default:
1383 /* Some other non-defined flags are also present, so print
1384 everything hex. */
1385 fprintf (file, " 0x%02x", (unsigned int) st_other);
1386 }
1387
1388 fprintf (file, " %s", name);
1389 }
1390 break;
1391 }
1392 }
1393 \f
1394 /* Create an entry in an ELF linker hash table. */
1395
1396 struct bfd_hash_entry *
1397 _bfd_elf_link_hash_newfunc (entry, table, string)
1398 struct bfd_hash_entry *entry;
1399 struct bfd_hash_table *table;
1400 const char *string;
1401 {
1402 /* Allocate the structure if it has not already been allocated by a
1403 subclass. */
1404 if (entry == NULL)
1405 {
1406 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1407 if (entry == NULL)
1408 return entry;
1409 }
1410
1411 /* Call the allocation method of the superclass. */
1412 entry = _bfd_link_hash_newfunc (entry, table, string);
1413 if (entry != NULL)
1414 {
1415 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1416 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1417
1418 /* Set local fields. */
1419 ret->indx = -1;
1420 ret->dynindx = -1;
1421 ret->dynstr_index = 0;
1422 ret->elf_hash_value = 0;
1423 ret->weakdef = NULL;
1424 ret->linker_section_pointer = NULL;
1425 ret->verinfo.verdef = NULL;
1426 ret->vtable_entries_size = 0;
1427 ret->vtable_entries_used = NULL;
1428 ret->vtable_parent = NULL;
1429 ret->got = htab->init_refcount;
1430 ret->plt = htab->init_refcount;
1431 ret->size = 0;
1432 ret->type = STT_NOTYPE;
1433 ret->other = 0;
1434 /* Assume that we have been called by a non-ELF symbol reader.
1435 This flag is then reset by the code which reads an ELF input
1436 file. This ensures that a symbol created by a non-ELF symbol
1437 reader will have the flag set correctly. */
1438 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1439 }
1440
1441 return entry;
1442 }
1443
1444 /* Copy data from an indirect symbol to its direct symbol, hiding the
1445 old indirect symbol. Also used for copying flags to a weakdef. */
1446
1447 void
1448 _bfd_elf_link_hash_copy_indirect (bed, dir, ind)
1449 struct elf_backend_data *bed;
1450 struct elf_link_hash_entry *dir, *ind;
1451 {
1452 bfd_signed_vma tmp;
1453 bfd_signed_vma lowest_valid = bed->can_refcount;
1454
1455 /* Copy down any references that we may have already seen to the
1456 symbol which just became indirect. */
1457
1458 dir->elf_link_hash_flags |=
1459 (ind->elf_link_hash_flags
1460 & (ELF_LINK_HASH_REF_DYNAMIC
1461 | ELF_LINK_HASH_REF_REGULAR
1462 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1463 | ELF_LINK_NON_GOT_REF));
1464
1465 if (ind->root.type != bfd_link_hash_indirect)
1466 return;
1467
1468 /* Copy over the global and procedure linkage table refcount entries.
1469 These may have been already set up by a check_relocs routine. */
1470 tmp = dir->got.refcount;
1471 if (tmp < lowest_valid)
1472 {
1473 dir->got.refcount = ind->got.refcount;
1474 ind->got.refcount = tmp;
1475 }
1476 else
1477 BFD_ASSERT (ind->got.refcount < lowest_valid);
1478
1479 tmp = dir->plt.refcount;
1480 if (tmp < lowest_valid)
1481 {
1482 dir->plt.refcount = ind->plt.refcount;
1483 ind->plt.refcount = tmp;
1484 }
1485 else
1486 BFD_ASSERT (ind->plt.refcount < lowest_valid);
1487
1488 if (dir->dynindx == -1)
1489 {
1490 dir->dynindx = ind->dynindx;
1491 dir->dynstr_index = ind->dynstr_index;
1492 ind->dynindx = -1;
1493 ind->dynstr_index = 0;
1494 }
1495 else
1496 BFD_ASSERT (ind->dynindx == -1);
1497 }
1498
1499 void
1500 _bfd_elf_link_hash_hide_symbol (info, h, force_local)
1501 struct bfd_link_info *info;
1502 struct elf_link_hash_entry *h;
1503 bfd_boolean force_local;
1504 {
1505 h->plt = elf_hash_table (info)->init_offset;
1506 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1507 if (force_local)
1508 {
1509 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1510 if (h->dynindx != -1)
1511 {
1512 h->dynindx = -1;
1513 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1514 h->dynstr_index);
1515 }
1516 }
1517 }
1518
1519 /* Initialize an ELF linker hash table. */
1520
1521 bfd_boolean
1522 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1523 struct elf_link_hash_table *table;
1524 bfd *abfd;
1525 struct bfd_hash_entry *(*newfunc)
1526 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
1527 const char *));
1528 {
1529 bfd_boolean ret;
1530
1531 table->dynamic_sections_created = FALSE;
1532 table->dynobj = NULL;
1533 /* Make sure can_refcount is extended to the width and signedness of
1534 init_refcount before we subtract one from it. */
1535 table->init_refcount.refcount = get_elf_backend_data (abfd)->can_refcount;
1536 table->init_refcount.refcount -= 1;
1537 table->init_offset.offset = -(bfd_vma) 1;
1538 /* The first dynamic symbol is a dummy. */
1539 table->dynsymcount = 1;
1540 table->dynstr = NULL;
1541 table->bucketcount = 0;
1542 table->needed = NULL;
1543 table->hgot = NULL;
1544 table->stab_info = NULL;
1545 table->merge_info = NULL;
1546 memset (&table->eh_info, 0, sizeof (table->eh_info));
1547 table->dynlocal = NULL;
1548 table->runpath = NULL;
1549 table->tls_segment = NULL;
1550 table->loaded = NULL;
1551
1552 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1553 table->root.type = bfd_link_elf_hash_table;
1554
1555 return ret;
1556 }
1557
1558 /* Create an ELF linker hash table. */
1559
1560 struct bfd_link_hash_table *
1561 _bfd_elf_link_hash_table_create (abfd)
1562 bfd *abfd;
1563 {
1564 struct elf_link_hash_table *ret;
1565 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1566
1567 ret = (struct elf_link_hash_table *) bfd_malloc (amt);
1568 if (ret == (struct elf_link_hash_table *) NULL)
1569 return NULL;
1570
1571 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1572 {
1573 free (ret);
1574 return NULL;
1575 }
1576
1577 return &ret->root;
1578 }
1579
1580 /* This is a hook for the ELF emulation code in the generic linker to
1581 tell the backend linker what file name to use for the DT_NEEDED
1582 entry for a dynamic object. The generic linker passes name as an
1583 empty string to indicate that no DT_NEEDED entry should be made. */
1584
1585 void
1586 bfd_elf_set_dt_needed_name (abfd, name)
1587 bfd *abfd;
1588 const char *name;
1589 {
1590 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1591 && bfd_get_format (abfd) == bfd_object)
1592 elf_dt_name (abfd) = name;
1593 }
1594
1595 void
1596 bfd_elf_set_dt_needed_soname (abfd, name)
1597 bfd *abfd;
1598 const char *name;
1599 {
1600 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1601 && bfd_get_format (abfd) == bfd_object)
1602 elf_dt_soname (abfd) = name;
1603 }
1604
1605 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1606 the linker ELF emulation code. */
1607
1608 struct bfd_link_needed_list *
1609 bfd_elf_get_needed_list (abfd, info)
1610 bfd *abfd ATTRIBUTE_UNUSED;
1611 struct bfd_link_info *info;
1612 {
1613 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1614 return NULL;
1615 return elf_hash_table (info)->needed;
1616 }
1617
1618 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1619 hook for the linker ELF emulation code. */
1620
1621 struct bfd_link_needed_list *
1622 bfd_elf_get_runpath_list (abfd, info)
1623 bfd *abfd ATTRIBUTE_UNUSED;
1624 struct bfd_link_info *info;
1625 {
1626 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1627 return NULL;
1628 return elf_hash_table (info)->runpath;
1629 }
1630
1631 /* Get the name actually used for a dynamic object for a link. This
1632 is the SONAME entry if there is one. Otherwise, it is the string
1633 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1634
1635 const char *
1636 bfd_elf_get_dt_soname (abfd)
1637 bfd *abfd;
1638 {
1639 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1640 && bfd_get_format (abfd) == bfd_object)
1641 return elf_dt_name (abfd);
1642 return NULL;
1643 }
1644
1645 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1646 the ELF linker emulation code. */
1647
1648 bfd_boolean
1649 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1650 bfd *abfd;
1651 struct bfd_link_needed_list **pneeded;
1652 {
1653 asection *s;
1654 bfd_byte *dynbuf = NULL;
1655 int elfsec;
1656 unsigned long shlink;
1657 bfd_byte *extdyn, *extdynend;
1658 size_t extdynsize;
1659 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1660
1661 *pneeded = NULL;
1662
1663 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1664 || bfd_get_format (abfd) != bfd_object)
1665 return TRUE;
1666
1667 s = bfd_get_section_by_name (abfd, ".dynamic");
1668 if (s == NULL || s->_raw_size == 0)
1669 return TRUE;
1670
1671 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1672 if (dynbuf == NULL)
1673 goto error_return;
1674
1675 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1676 s->_raw_size))
1677 goto error_return;
1678
1679 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1680 if (elfsec == -1)
1681 goto error_return;
1682
1683 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1684
1685 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1686 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1687
1688 extdyn = dynbuf;
1689 extdynend = extdyn + s->_raw_size;
1690 for (; extdyn < extdynend; extdyn += extdynsize)
1691 {
1692 Elf_Internal_Dyn dyn;
1693
1694 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1695
1696 if (dyn.d_tag == DT_NULL)
1697 break;
1698
1699 if (dyn.d_tag == DT_NEEDED)
1700 {
1701 const char *string;
1702 struct bfd_link_needed_list *l;
1703 unsigned int tagv = dyn.d_un.d_val;
1704 bfd_size_type amt;
1705
1706 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1707 if (string == NULL)
1708 goto error_return;
1709
1710 amt = sizeof *l;
1711 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1712 if (l == NULL)
1713 goto error_return;
1714
1715 l->by = abfd;
1716 l->name = string;
1717 l->next = *pneeded;
1718 *pneeded = l;
1719 }
1720 }
1721
1722 free (dynbuf);
1723
1724 return TRUE;
1725
1726 error_return:
1727 if (dynbuf != NULL)
1728 free (dynbuf);
1729 return FALSE;
1730 }
1731 \f
1732 /* Allocate an ELF string table--force the first byte to be zero. */
1733
1734 struct bfd_strtab_hash *
1735 _bfd_elf_stringtab_init ()
1736 {
1737 struct bfd_strtab_hash *ret;
1738
1739 ret = _bfd_stringtab_init ();
1740 if (ret != NULL)
1741 {
1742 bfd_size_type loc;
1743
1744 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1745 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1746 if (loc == (bfd_size_type) -1)
1747 {
1748 _bfd_stringtab_free (ret);
1749 ret = NULL;
1750 }
1751 }
1752 return ret;
1753 }
1754 \f
1755 /* ELF .o/exec file reading */
1756
1757 /* Create a new bfd section from an ELF section header. */
1758
1759 bfd_boolean
1760 bfd_section_from_shdr (abfd, shindex)
1761 bfd *abfd;
1762 unsigned int shindex;
1763 {
1764 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1765 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1766 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1767 const char *name;
1768
1769 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1770
1771 switch (hdr->sh_type)
1772 {
1773 case SHT_NULL:
1774 /* Inactive section. Throw it away. */
1775 return TRUE;
1776
1777 case SHT_PROGBITS: /* Normal section with contents. */
1778 case SHT_NOBITS: /* .bss section. */
1779 case SHT_HASH: /* .hash section. */
1780 case SHT_NOTE: /* .note section. */
1781 case SHT_INIT_ARRAY: /* .init_array section. */
1782 case SHT_FINI_ARRAY: /* .fini_array section. */
1783 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1784 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1785
1786 case SHT_DYNAMIC: /* Dynamic linking information. */
1787 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1788 return FALSE;
1789 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1790 {
1791 Elf_Internal_Shdr *dynsymhdr;
1792
1793 /* The shared libraries distributed with hpux11 have a bogus
1794 sh_link field for the ".dynamic" section. Find the
1795 string table for the ".dynsym" section instead. */
1796 if (elf_dynsymtab (abfd) != 0)
1797 {
1798 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1799 hdr->sh_link = dynsymhdr->sh_link;
1800 }
1801 else
1802 {
1803 unsigned int i, num_sec;
1804
1805 num_sec = elf_numsections (abfd);
1806 for (i = 1; i < num_sec; i++)
1807 {
1808 dynsymhdr = elf_elfsections (abfd)[i];
1809 if (dynsymhdr->sh_type == SHT_DYNSYM)
1810 {
1811 hdr->sh_link = dynsymhdr->sh_link;
1812 break;
1813 }
1814 }
1815 }
1816 }
1817 break;
1818
1819 case SHT_SYMTAB: /* A symbol table */
1820 if (elf_onesymtab (abfd) == shindex)
1821 return TRUE;
1822
1823 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1824 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1825 elf_onesymtab (abfd) = shindex;
1826 elf_tdata (abfd)->symtab_hdr = *hdr;
1827 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1828 abfd->flags |= HAS_SYMS;
1829
1830 /* Sometimes a shared object will map in the symbol table. If
1831 SHF_ALLOC is set, and this is a shared object, then we also
1832 treat this section as a BFD section. We can not base the
1833 decision purely on SHF_ALLOC, because that flag is sometimes
1834 set in a relocateable object file, which would confuse the
1835 linker. */
1836 if ((hdr->sh_flags & SHF_ALLOC) != 0
1837 && (abfd->flags & DYNAMIC) != 0
1838 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1839 return FALSE;
1840
1841 return TRUE;
1842
1843 case SHT_DYNSYM: /* A dynamic symbol table */
1844 if (elf_dynsymtab (abfd) == shindex)
1845 return TRUE;
1846
1847 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1848 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1849 elf_dynsymtab (abfd) = shindex;
1850 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1851 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1852 abfd->flags |= HAS_SYMS;
1853
1854 /* Besides being a symbol table, we also treat this as a regular
1855 section, so that objcopy can handle it. */
1856 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1857
1858 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1859 if (elf_symtab_shndx (abfd) == shindex)
1860 return TRUE;
1861
1862 /* Get the associated symbol table. */
1863 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1864 || hdr->sh_link != elf_onesymtab (abfd))
1865 return FALSE;
1866
1867 elf_symtab_shndx (abfd) = shindex;
1868 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1869 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1870 return TRUE;
1871
1872 case SHT_STRTAB: /* A string table */
1873 if (hdr->bfd_section != NULL)
1874 return TRUE;
1875 if (ehdr->e_shstrndx == shindex)
1876 {
1877 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1878 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1879 return TRUE;
1880 }
1881 {
1882 unsigned int i, num_sec;
1883
1884 num_sec = elf_numsections (abfd);
1885 for (i = 1; i < num_sec; i++)
1886 {
1887 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1888 if (hdr2->sh_link == shindex)
1889 {
1890 if (! bfd_section_from_shdr (abfd, i))
1891 return FALSE;
1892 if (elf_onesymtab (abfd) == i)
1893 {
1894 elf_tdata (abfd)->strtab_hdr = *hdr;
1895 elf_elfsections (abfd)[shindex] =
1896 &elf_tdata (abfd)->strtab_hdr;
1897 return TRUE;
1898 }
1899 if (elf_dynsymtab (abfd) == i)
1900 {
1901 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1902 elf_elfsections (abfd)[shindex] = hdr =
1903 &elf_tdata (abfd)->dynstrtab_hdr;
1904 /* We also treat this as a regular section, so
1905 that objcopy can handle it. */
1906 break;
1907 }
1908 #if 0 /* Not handling other string tables specially right now. */
1909 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1910 /* We have a strtab for some random other section. */
1911 newsect = (asection *) hdr2->bfd_section;
1912 if (!newsect)
1913 break;
1914 hdr->bfd_section = newsect;
1915 hdr2 = &elf_section_data (newsect)->str_hdr;
1916 *hdr2 = *hdr;
1917 elf_elfsections (abfd)[shindex] = hdr2;
1918 #endif
1919 }
1920 }
1921 }
1922
1923 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1924
1925 case SHT_REL:
1926 case SHT_RELA:
1927 /* *These* do a lot of work -- but build no sections! */
1928 {
1929 asection *target_sect;
1930 Elf_Internal_Shdr *hdr2;
1931 unsigned int num_sec = elf_numsections (abfd);
1932
1933 /* Check for a bogus link to avoid crashing. */
1934 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1935 || hdr->sh_link >= num_sec)
1936 {
1937 ((*_bfd_error_handler)
1938 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1939 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1940 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1941 }
1942
1943 /* For some incomprehensible reason Oracle distributes
1944 libraries for Solaris in which some of the objects have
1945 bogus sh_link fields. It would be nice if we could just
1946 reject them, but, unfortunately, some people need to use
1947 them. We scan through the section headers; if we find only
1948 one suitable symbol table, we clobber the sh_link to point
1949 to it. I hope this doesn't break anything. */
1950 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1951 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1952 {
1953 unsigned int scan;
1954 int found;
1955
1956 found = 0;
1957 for (scan = 1; scan < num_sec; scan++)
1958 {
1959 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1960 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1961 {
1962 if (found != 0)
1963 {
1964 found = 0;
1965 break;
1966 }
1967 found = scan;
1968 }
1969 }
1970 if (found != 0)
1971 hdr->sh_link = found;
1972 }
1973
1974 /* Get the symbol table. */
1975 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1976 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1977 return FALSE;
1978
1979 /* If this reloc section does not use the main symbol table we
1980 don't treat it as a reloc section. BFD can't adequately
1981 represent such a section, so at least for now, we don't
1982 try. We just present it as a normal section. We also
1983 can't use it as a reloc section if it points to the null
1984 section. */
1985 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1986 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1987
1988 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1989 return FALSE;
1990 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1991 if (target_sect == NULL)
1992 return FALSE;
1993
1994 if ((target_sect->flags & SEC_RELOC) == 0
1995 || target_sect->reloc_count == 0)
1996 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1997 else
1998 {
1999 bfd_size_type amt;
2000 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
2001 amt = sizeof (*hdr2);
2002 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
2003 elf_section_data (target_sect)->rel_hdr2 = hdr2;
2004 }
2005 *hdr2 = *hdr;
2006 elf_elfsections (abfd)[shindex] = hdr2;
2007 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
2008 target_sect->flags |= SEC_RELOC;
2009 target_sect->relocation = NULL;
2010 target_sect->rel_filepos = hdr->sh_offset;
2011 /* In the section to which the relocations apply, mark whether
2012 its relocations are of the REL or RELA variety. */
2013 if (hdr->sh_size != 0)
2014 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
2015 abfd->flags |= HAS_RELOC;
2016 return TRUE;
2017 }
2018 break;
2019
2020 case SHT_GNU_verdef:
2021 elf_dynverdef (abfd) = shindex;
2022 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2023 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2024 break;
2025
2026 case SHT_GNU_versym:
2027 elf_dynversym (abfd) = shindex;
2028 elf_tdata (abfd)->dynversym_hdr = *hdr;
2029 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2030 break;
2031
2032 case SHT_GNU_verneed:
2033 elf_dynverref (abfd) = shindex;
2034 elf_tdata (abfd)->dynverref_hdr = *hdr;
2035 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2036 break;
2037
2038 case SHT_SHLIB:
2039 return TRUE;
2040
2041 case SHT_GROUP:
2042 /* We need a BFD section for objcopy and relocatable linking,
2043 and it's handy to have the signature available as the section
2044 name. */
2045 name = group_signature (abfd, hdr);
2046 if (name == NULL)
2047 return FALSE;
2048 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
2049 return FALSE;
2050 if (hdr->contents != NULL)
2051 {
2052 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2053 unsigned int n_elt = hdr->sh_size / 4;
2054 asection *s;
2055
2056 if (idx->flags & GRP_COMDAT)
2057 hdr->bfd_section->flags
2058 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2059
2060 while (--n_elt != 0)
2061 if ((s = (++idx)->shdr->bfd_section) != NULL
2062 && elf_next_in_group (s) != NULL)
2063 {
2064 elf_next_in_group (hdr->bfd_section) = s;
2065 break;
2066 }
2067 }
2068 break;
2069
2070 default:
2071 /* Check for any processor-specific section types. */
2072 {
2073 if (bed->elf_backend_section_from_shdr)
2074 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
2075 }
2076 break;
2077 }
2078
2079 return TRUE;
2080 }
2081
2082 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2083 Return SEC for sections that have no elf section, and NULL on error. */
2084
2085 asection *
2086 bfd_section_from_r_symndx (abfd, cache, sec, r_symndx)
2087 bfd *abfd;
2088 struct sym_sec_cache *cache;
2089 asection *sec;
2090 unsigned long r_symndx;
2091 {
2092 Elf_Internal_Shdr *symtab_hdr;
2093 unsigned char esym[sizeof (Elf64_External_Sym)];
2094 Elf_External_Sym_Shndx eshndx;
2095 Elf_Internal_Sym isym;
2096 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2097
2098 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
2099 return cache->sec[ent];
2100
2101 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2102 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2103 &isym, esym, &eshndx) == NULL)
2104 return NULL;
2105
2106 if (cache->abfd != abfd)
2107 {
2108 memset (cache->indx, -1, sizeof (cache->indx));
2109 cache->abfd = abfd;
2110 }
2111 cache->indx[ent] = r_symndx;
2112 cache->sec[ent] = sec;
2113 if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE)
2114 {
2115 asection *s;
2116 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
2117 if (s != NULL)
2118 cache->sec[ent] = s;
2119 }
2120 return cache->sec[ent];
2121 }
2122
2123 /* Given an ELF section number, retrieve the corresponding BFD
2124 section. */
2125
2126 asection *
2127 bfd_section_from_elf_index (abfd, index)
2128 bfd *abfd;
2129 unsigned int index;
2130 {
2131 if (index >= elf_numsections (abfd))
2132 return NULL;
2133 return elf_elfsections (abfd)[index]->bfd_section;
2134 }
2135
2136 bfd_boolean
2137 _bfd_elf_new_section_hook (abfd, sec)
2138 bfd *abfd;
2139 asection *sec;
2140 {
2141 struct bfd_elf_section_data *sdata;
2142
2143 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2144 if (sdata == NULL)
2145 {
2146 bfd_size_type amt = sizeof (*sdata);
2147 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt);
2148 if (sdata == NULL)
2149 return FALSE;
2150 sec->used_by_bfd = (PTR) sdata;
2151 }
2152
2153 /* Indicate whether or not this section should use RELA relocations. */
2154 sec->use_rela_p = get_elf_backend_data (abfd)->default_use_rela_p;
2155
2156 return TRUE;
2157 }
2158
2159 /* Create a new bfd section from an ELF program header.
2160
2161 Since program segments have no names, we generate a synthetic name
2162 of the form segment<NUM>, where NUM is generally the index in the
2163 program header table. For segments that are split (see below) we
2164 generate the names segment<NUM>a and segment<NUM>b.
2165
2166 Note that some program segments may have a file size that is different than
2167 (less than) the memory size. All this means is that at execution the
2168 system must allocate the amount of memory specified by the memory size,
2169 but only initialize it with the first "file size" bytes read from the
2170 file. This would occur for example, with program segments consisting
2171 of combined data+bss.
2172
2173 To handle the above situation, this routine generates TWO bfd sections
2174 for the single program segment. The first has the length specified by
2175 the file size of the segment, and the second has the length specified
2176 by the difference between the two sizes. In effect, the segment is split
2177 into it's initialized and uninitialized parts.
2178
2179 */
2180
2181 bfd_boolean
2182 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
2183 bfd *abfd;
2184 Elf_Internal_Phdr *hdr;
2185 int index;
2186 const char *typename;
2187 {
2188 asection *newsect;
2189 char *name;
2190 char namebuf[64];
2191 size_t len;
2192 int split;
2193
2194 split = ((hdr->p_memsz > 0)
2195 && (hdr->p_filesz > 0)
2196 && (hdr->p_memsz > hdr->p_filesz));
2197 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2198 len = strlen (namebuf) + 1;
2199 name = bfd_alloc (abfd, (bfd_size_type) len);
2200 if (!name)
2201 return FALSE;
2202 memcpy (name, namebuf, len);
2203 newsect = bfd_make_section (abfd, name);
2204 if (newsect == NULL)
2205 return FALSE;
2206 newsect->vma = hdr->p_vaddr;
2207 newsect->lma = hdr->p_paddr;
2208 newsect->_raw_size = hdr->p_filesz;
2209 newsect->filepos = hdr->p_offset;
2210 newsect->flags |= SEC_HAS_CONTENTS;
2211 if (hdr->p_type == PT_LOAD)
2212 {
2213 newsect->flags |= SEC_ALLOC;
2214 newsect->flags |= SEC_LOAD;
2215 if (hdr->p_flags & PF_X)
2216 {
2217 /* FIXME: all we known is that it has execute PERMISSION,
2218 may be data. */
2219 newsect->flags |= SEC_CODE;
2220 }
2221 }
2222 if (!(hdr->p_flags & PF_W))
2223 {
2224 newsect->flags |= SEC_READONLY;
2225 }
2226
2227 if (split)
2228 {
2229 sprintf (namebuf, "%s%db", typename, index);
2230 len = strlen (namebuf) + 1;
2231 name = bfd_alloc (abfd, (bfd_size_type) len);
2232 if (!name)
2233 return FALSE;
2234 memcpy (name, namebuf, len);
2235 newsect = bfd_make_section (abfd, name);
2236 if (newsect == NULL)
2237 return FALSE;
2238 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2239 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2240 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
2241 if (hdr->p_type == PT_LOAD)
2242 {
2243 newsect->flags |= SEC_ALLOC;
2244 if (hdr->p_flags & PF_X)
2245 newsect->flags |= SEC_CODE;
2246 }
2247 if (!(hdr->p_flags & PF_W))
2248 newsect->flags |= SEC_READONLY;
2249 }
2250
2251 return TRUE;
2252 }
2253
2254 bfd_boolean
2255 bfd_section_from_phdr (abfd, hdr, index)
2256 bfd *abfd;
2257 Elf_Internal_Phdr *hdr;
2258 int index;
2259 {
2260 struct elf_backend_data *bed;
2261
2262 switch (hdr->p_type)
2263 {
2264 case PT_NULL:
2265 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2266
2267 case PT_LOAD:
2268 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2269
2270 case PT_DYNAMIC:
2271 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2272
2273 case PT_INTERP:
2274 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2275
2276 case PT_NOTE:
2277 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2278 return FALSE;
2279 if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz))
2280 return FALSE;
2281 return TRUE;
2282
2283 case PT_SHLIB:
2284 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2285
2286 case PT_PHDR:
2287 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2288
2289 default:
2290 /* Check for any processor-specific program segment types.
2291 If no handler for them, default to making "segment" sections. */
2292 bed = get_elf_backend_data (abfd);
2293 if (bed->elf_backend_section_from_phdr)
2294 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2295 else
2296 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2297 }
2298 }
2299
2300 /* Initialize REL_HDR, the section-header for new section, containing
2301 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2302 relocations; otherwise, we use REL relocations. */
2303
2304 bfd_boolean
2305 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
2306 bfd *abfd;
2307 Elf_Internal_Shdr *rel_hdr;
2308 asection *asect;
2309 bfd_boolean use_rela_p;
2310 {
2311 char *name;
2312 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2313 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2314
2315 name = bfd_alloc (abfd, amt);
2316 if (name == NULL)
2317 return FALSE;
2318 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2319 rel_hdr->sh_name =
2320 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2321 FALSE);
2322 if (rel_hdr->sh_name == (unsigned int) -1)
2323 return FALSE;
2324 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2325 rel_hdr->sh_entsize = (use_rela_p
2326 ? bed->s->sizeof_rela
2327 : bed->s->sizeof_rel);
2328 rel_hdr->sh_addralign = bed->s->file_align;
2329 rel_hdr->sh_flags = 0;
2330 rel_hdr->sh_addr = 0;
2331 rel_hdr->sh_size = 0;
2332 rel_hdr->sh_offset = 0;
2333
2334 return TRUE;
2335 }
2336
2337 /* Set up an ELF internal section header for a section. */
2338
2339 static void
2340 elf_fake_sections (abfd, asect, failedptrarg)
2341 bfd *abfd;
2342 asection *asect;
2343 PTR failedptrarg;
2344 {
2345 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2346 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2347 Elf_Internal_Shdr *this_hdr;
2348
2349 if (*failedptr)
2350 {
2351 /* We already failed; just get out of the bfd_map_over_sections
2352 loop. */
2353 return;
2354 }
2355
2356 this_hdr = &elf_section_data (asect)->this_hdr;
2357
2358 this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2359 asect->name, FALSE);
2360 if (this_hdr->sh_name == (unsigned long) -1)
2361 {
2362 *failedptr = TRUE;
2363 return;
2364 }
2365
2366 this_hdr->sh_flags = 0;
2367
2368 if ((asect->flags & SEC_ALLOC) != 0
2369 || asect->user_set_vma)
2370 this_hdr->sh_addr = asect->vma;
2371 else
2372 this_hdr->sh_addr = 0;
2373
2374 this_hdr->sh_offset = 0;
2375 this_hdr->sh_size = asect->_raw_size;
2376 this_hdr->sh_link = 0;
2377 this_hdr->sh_addralign = 1 << asect->alignment_power;
2378 /* The sh_entsize and sh_info fields may have been set already by
2379 copy_private_section_data. */
2380
2381 this_hdr->bfd_section = asect;
2382 this_hdr->contents = NULL;
2383
2384 /* FIXME: This should not be based on section names. */
2385 if (strcmp (asect->name, ".dynstr") == 0)
2386 this_hdr->sh_type = SHT_STRTAB;
2387 else if (strcmp (asect->name, ".hash") == 0)
2388 {
2389 this_hdr->sh_type = SHT_HASH;
2390 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2391 }
2392 else if (strcmp (asect->name, ".dynsym") == 0)
2393 {
2394 this_hdr->sh_type = SHT_DYNSYM;
2395 this_hdr->sh_entsize = bed->s->sizeof_sym;
2396 }
2397 else if (strcmp (asect->name, ".dynamic") == 0)
2398 {
2399 this_hdr->sh_type = SHT_DYNAMIC;
2400 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2401 }
2402 else if (strncmp (asect->name, ".rela", 5) == 0
2403 && get_elf_backend_data (abfd)->may_use_rela_p)
2404 {
2405 this_hdr->sh_type = SHT_RELA;
2406 this_hdr->sh_entsize = bed->s->sizeof_rela;
2407 }
2408 else if (strncmp (asect->name, ".rel", 4) == 0
2409 && get_elf_backend_data (abfd)->may_use_rel_p)
2410 {
2411 this_hdr->sh_type = SHT_REL;
2412 this_hdr->sh_entsize = bed->s->sizeof_rel;
2413 }
2414 else if (strcmp (asect->name, ".init_array") == 0)
2415 this_hdr->sh_type = SHT_INIT_ARRAY;
2416 else if (strcmp (asect->name, ".fini_array") == 0)
2417 this_hdr->sh_type = SHT_FINI_ARRAY;
2418 else if (strcmp (asect->name, ".preinit_array") == 0)
2419 this_hdr->sh_type = SHT_PREINIT_ARRAY;
2420 else if (strncmp (asect->name, ".note", 5) == 0)
2421 this_hdr->sh_type = SHT_NOTE;
2422 else if (strncmp (asect->name, ".stab", 5) == 0
2423 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
2424 this_hdr->sh_type = SHT_STRTAB;
2425 else if (strcmp (asect->name, ".gnu.version") == 0)
2426 {
2427 this_hdr->sh_type = SHT_GNU_versym;
2428 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2429 }
2430 else if (strcmp (asect->name, ".gnu.version_d") == 0)
2431 {
2432 this_hdr->sh_type = SHT_GNU_verdef;
2433 this_hdr->sh_entsize = 0;
2434 /* objcopy or strip will copy over sh_info, but may not set
2435 cverdefs. The linker will set cverdefs, but sh_info will be
2436 zero. */
2437 if (this_hdr->sh_info == 0)
2438 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2439 else
2440 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2441 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2442 }
2443 else if (strcmp (asect->name, ".gnu.version_r") == 0)
2444 {
2445 this_hdr->sh_type = SHT_GNU_verneed;
2446 this_hdr->sh_entsize = 0;
2447 /* objcopy or strip will copy over sh_info, but may not set
2448 cverrefs. The linker will set cverrefs, but sh_info will be
2449 zero. */
2450 if (this_hdr->sh_info == 0)
2451 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2452 else
2453 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2454 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2455 }
2456 else if ((asect->flags & SEC_GROUP) != 0)
2457 {
2458 this_hdr->sh_type = SHT_GROUP;
2459 this_hdr->sh_entsize = 4;
2460 }
2461 else if ((asect->flags & SEC_ALLOC) != 0
2462 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2463 || (asect->flags & SEC_NEVER_LOAD) != 0))
2464 this_hdr->sh_type = SHT_NOBITS;
2465 else
2466 this_hdr->sh_type = SHT_PROGBITS;
2467
2468 if ((asect->flags & SEC_ALLOC) != 0)
2469 this_hdr->sh_flags |= SHF_ALLOC;
2470 if ((asect->flags & SEC_READONLY) == 0)
2471 this_hdr->sh_flags |= SHF_WRITE;
2472 if ((asect->flags & SEC_CODE) != 0)
2473 this_hdr->sh_flags |= SHF_EXECINSTR;
2474 if ((asect->flags & SEC_MERGE) != 0)
2475 {
2476 this_hdr->sh_flags |= SHF_MERGE;
2477 this_hdr->sh_entsize = asect->entsize;
2478 if ((asect->flags & SEC_STRINGS) != 0)
2479 this_hdr->sh_flags |= SHF_STRINGS;
2480 }
2481 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2482 this_hdr->sh_flags |= SHF_GROUP;
2483 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2484 {
2485 this_hdr->sh_flags |= SHF_TLS;
2486 if (asect->_raw_size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2487 {
2488 struct bfd_link_order *o;
2489
2490 this_hdr->sh_size = 0;
2491 for (o = asect->link_order_head; o != NULL; o = o->next)
2492 if (this_hdr->sh_size < o->offset + o->size)
2493 this_hdr->sh_size = o->offset + o->size;
2494 if (this_hdr->sh_size)
2495 this_hdr->sh_type = SHT_NOBITS;
2496 }
2497 }
2498
2499 /* Check for processor-specific section types. */
2500 if (bed->elf_backend_fake_sections
2501 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2502 *failedptr = TRUE;
2503
2504 /* If the section has relocs, set up a section header for the
2505 SHT_REL[A] section. If two relocation sections are required for
2506 this section, it is up to the processor-specific back-end to
2507 create the other. */
2508 if ((asect->flags & SEC_RELOC) != 0
2509 && !_bfd_elf_init_reloc_shdr (abfd,
2510 &elf_section_data (asect)->rel_hdr,
2511 asect,
2512 asect->use_rela_p))
2513 *failedptr = TRUE;
2514 }
2515
2516 /* Fill in the contents of a SHT_GROUP section. */
2517
2518 void
2519 bfd_elf_set_group_contents (abfd, sec, failedptrarg)
2520 bfd *abfd;
2521 asection *sec;
2522 PTR failedptrarg;
2523 {
2524 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2525 unsigned long symindx;
2526 asection *elt, *first;
2527 unsigned char *loc;
2528 struct bfd_link_order *l;
2529 bfd_boolean gas;
2530
2531 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2532 || *failedptr)
2533 return;
2534
2535 symindx = 0;
2536 if (elf_group_id (sec) != NULL)
2537 symindx = elf_group_id (sec)->udata.i;
2538
2539 if (symindx == 0)
2540 {
2541 /* If called from the assembler, swap_out_syms will have set up
2542 elf_section_syms; If called for "ld -r", use target_index. */
2543 if (elf_section_syms (abfd) != NULL)
2544 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2545 else
2546 symindx = sec->target_index;
2547 }
2548 elf_section_data (sec)->this_hdr.sh_info = symindx;
2549
2550 /* The contents won't be allocated for "ld -r" or objcopy. */
2551 gas = TRUE;
2552 if (sec->contents == NULL)
2553 {
2554 gas = FALSE;
2555 sec->contents = bfd_alloc (abfd, sec->_raw_size);
2556
2557 /* Arrange for the section to be written out. */
2558 elf_section_data (sec)->this_hdr.contents = sec->contents;
2559 if (sec->contents == NULL)
2560 {
2561 *failedptr = TRUE;
2562 return;
2563 }
2564 }
2565
2566 loc = sec->contents + sec->_raw_size;
2567
2568 /* Get the pointer to the first section in the group that gas
2569 squirreled away here. objcopy arranges for this to be set to the
2570 start of the input section group. */
2571 first = elt = elf_next_in_group (sec);
2572
2573 /* First element is a flag word. Rest of section is elf section
2574 indices for all the sections of the group. Write them backwards
2575 just to keep the group in the same order as given in .section
2576 directives, not that it matters. */
2577 while (elt != NULL)
2578 {
2579 asection *s;
2580 unsigned int idx;
2581
2582 loc -= 4;
2583 s = elt;
2584 if (!gas)
2585 s = s->output_section;
2586 idx = 0;
2587 if (s != NULL)
2588 idx = elf_section_data (s)->this_idx;
2589 H_PUT_32 (abfd, idx, loc);
2590 elt = elf_next_in_group (elt);
2591 if (elt == first)
2592 break;
2593 }
2594
2595 /* If this is a relocatable link, then the above did nothing because
2596 SEC is the output section. Look through the input sections
2597 instead. */
2598 for (l = sec->link_order_head; l != NULL; l = l->next)
2599 if (l->type == bfd_indirect_link_order
2600 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2601 do
2602 {
2603 loc -= 4;
2604 H_PUT_32 (abfd,
2605 elf_section_data (elt->output_section)->this_idx, loc);
2606 elt = elf_next_in_group (elt);
2607 /* During a relocatable link, the lists are circular. */
2608 }
2609 while (elt != elf_next_in_group (l->u.indirect.section));
2610
2611 /* With ld -r, merging SHT_GROUP sections results in wasted space
2612 due to allowing for the flag word on each input. We may well
2613 duplicate entries too. */
2614 while ((loc -= 4) > sec->contents)
2615 H_PUT_32 (abfd, 0, loc);
2616
2617 if (loc != sec->contents)
2618 abort ();
2619
2620 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2621 }
2622
2623 /* Assign all ELF section numbers. The dummy first section is handled here
2624 too. The link/info pointers for the standard section types are filled
2625 in here too, while we're at it. */
2626
2627 static bfd_boolean
2628 assign_section_numbers (abfd)
2629 bfd *abfd;
2630 {
2631 struct elf_obj_tdata *t = elf_tdata (abfd);
2632 asection *sec;
2633 unsigned int section_number, secn;
2634 Elf_Internal_Shdr **i_shdrp;
2635 bfd_size_type amt;
2636
2637 section_number = 1;
2638
2639 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2640
2641 for (sec = abfd->sections; sec; sec = sec->next)
2642 {
2643 struct bfd_elf_section_data *d = elf_section_data (sec);
2644
2645 if (section_number == SHN_LORESERVE)
2646 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2647 d->this_idx = section_number++;
2648 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2649 if ((sec->flags & SEC_RELOC) == 0)
2650 d->rel_idx = 0;
2651 else
2652 {
2653 if (section_number == SHN_LORESERVE)
2654 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2655 d->rel_idx = section_number++;
2656 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2657 }
2658
2659 if (d->rel_hdr2)
2660 {
2661 if (section_number == SHN_LORESERVE)
2662 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2663 d->rel_idx2 = section_number++;
2664 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2665 }
2666 else
2667 d->rel_idx2 = 0;
2668 }
2669
2670 if (section_number == SHN_LORESERVE)
2671 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2672 t->shstrtab_section = section_number++;
2673 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2674 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2675
2676 if (bfd_get_symcount (abfd) > 0)
2677 {
2678 if (section_number == SHN_LORESERVE)
2679 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2680 t->symtab_section = section_number++;
2681 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2682 if (section_number > SHN_LORESERVE - 2)
2683 {
2684 if (section_number == SHN_LORESERVE)
2685 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2686 t->symtab_shndx_section = section_number++;
2687 t->symtab_shndx_hdr.sh_name
2688 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2689 ".symtab_shndx", FALSE);
2690 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2691 return FALSE;
2692 }
2693 if (section_number == SHN_LORESERVE)
2694 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2695 t->strtab_section = section_number++;
2696 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2697 }
2698
2699 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2700 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2701
2702 elf_numsections (abfd) = section_number;
2703 elf_elfheader (abfd)->e_shnum = section_number;
2704 if (section_number > SHN_LORESERVE)
2705 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2706
2707 /* Set up the list of section header pointers, in agreement with the
2708 indices. */
2709 amt = section_number * sizeof (Elf_Internal_Shdr *);
2710 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
2711 if (i_shdrp == NULL)
2712 return FALSE;
2713
2714 amt = sizeof (Elf_Internal_Shdr);
2715 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
2716 if (i_shdrp[0] == NULL)
2717 {
2718 bfd_release (abfd, i_shdrp);
2719 return FALSE;
2720 }
2721
2722 elf_elfsections (abfd) = i_shdrp;
2723
2724 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2725 if (bfd_get_symcount (abfd) > 0)
2726 {
2727 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2728 if (elf_numsections (abfd) > SHN_LORESERVE)
2729 {
2730 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2731 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2732 }
2733 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2734 t->symtab_hdr.sh_link = t->strtab_section;
2735 }
2736 for (sec = abfd->sections; sec; sec = sec->next)
2737 {
2738 struct bfd_elf_section_data *d = elf_section_data (sec);
2739 asection *s;
2740 const char *name;
2741
2742 i_shdrp[d->this_idx] = &d->this_hdr;
2743 if (d->rel_idx != 0)
2744 i_shdrp[d->rel_idx] = &d->rel_hdr;
2745 if (d->rel_idx2 != 0)
2746 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2747
2748 /* Fill in the sh_link and sh_info fields while we're at it. */
2749
2750 /* sh_link of a reloc section is the section index of the symbol
2751 table. sh_info is the section index of the section to which
2752 the relocation entries apply. */
2753 if (d->rel_idx != 0)
2754 {
2755 d->rel_hdr.sh_link = t->symtab_section;
2756 d->rel_hdr.sh_info = d->this_idx;
2757 }
2758 if (d->rel_idx2 != 0)
2759 {
2760 d->rel_hdr2->sh_link = t->symtab_section;
2761 d->rel_hdr2->sh_info = d->this_idx;
2762 }
2763
2764 switch (d->this_hdr.sh_type)
2765 {
2766 case SHT_REL:
2767 case SHT_RELA:
2768 /* A reloc section which we are treating as a normal BFD
2769 section. sh_link is the section index of the symbol
2770 table. sh_info is the section index of the section to
2771 which the relocation entries apply. We assume that an
2772 allocated reloc section uses the dynamic symbol table.
2773 FIXME: How can we be sure? */
2774 s = bfd_get_section_by_name (abfd, ".dynsym");
2775 if (s != NULL)
2776 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2777
2778 /* We look up the section the relocs apply to by name. */
2779 name = sec->name;
2780 if (d->this_hdr.sh_type == SHT_REL)
2781 name += 4;
2782 else
2783 name += 5;
2784 s = bfd_get_section_by_name (abfd, name);
2785 if (s != NULL)
2786 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2787 break;
2788
2789 case SHT_STRTAB:
2790 /* We assume that a section named .stab*str is a stabs
2791 string section. We look for a section with the same name
2792 but without the trailing ``str'', and set its sh_link
2793 field to point to this section. */
2794 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2795 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2796 {
2797 size_t len;
2798 char *alc;
2799
2800 len = strlen (sec->name);
2801 alc = (char *) bfd_malloc ((bfd_size_type) (len - 2));
2802 if (alc == NULL)
2803 return FALSE;
2804 memcpy (alc, sec->name, len - 3);
2805 alc[len - 3] = '\0';
2806 s = bfd_get_section_by_name (abfd, alc);
2807 free (alc);
2808 if (s != NULL)
2809 {
2810 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2811
2812 /* This is a .stab section. */
2813 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2814 elf_section_data (s)->this_hdr.sh_entsize
2815 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
2816 }
2817 }
2818 break;
2819
2820 case SHT_DYNAMIC:
2821 case SHT_DYNSYM:
2822 case SHT_GNU_verneed:
2823 case SHT_GNU_verdef:
2824 /* sh_link is the section header index of the string table
2825 used for the dynamic entries, or the symbol table, or the
2826 version strings. */
2827 s = bfd_get_section_by_name (abfd, ".dynstr");
2828 if (s != NULL)
2829 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2830 break;
2831
2832 case SHT_HASH:
2833 case SHT_GNU_versym:
2834 /* sh_link is the section header index of the symbol table
2835 this hash table or version table is for. */
2836 s = bfd_get_section_by_name (abfd, ".dynsym");
2837 if (s != NULL)
2838 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2839 break;
2840
2841 case SHT_GROUP:
2842 d->this_hdr.sh_link = t->symtab_section;
2843 }
2844 }
2845
2846 for (secn = 1; secn < section_number; ++secn)
2847 if (i_shdrp[secn] == NULL)
2848 i_shdrp[secn] = i_shdrp[0];
2849 else
2850 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2851 i_shdrp[secn]->sh_name);
2852 return TRUE;
2853 }
2854
2855 /* Map symbol from it's internal number to the external number, moving
2856 all local symbols to be at the head of the list. */
2857
2858 static INLINE int
2859 sym_is_global (abfd, sym)
2860 bfd *abfd;
2861 asymbol *sym;
2862 {
2863 /* If the backend has a special mapping, use it. */
2864 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2865 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2866 (abfd, sym));
2867
2868 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2869 || bfd_is_und_section (bfd_get_section (sym))
2870 || bfd_is_com_section (bfd_get_section (sym)));
2871 }
2872
2873 static bfd_boolean
2874 elf_map_symbols (abfd)
2875 bfd *abfd;
2876 {
2877 unsigned int symcount = bfd_get_symcount (abfd);
2878 asymbol **syms = bfd_get_outsymbols (abfd);
2879 asymbol **sect_syms;
2880 unsigned int num_locals = 0;
2881 unsigned int num_globals = 0;
2882 unsigned int num_locals2 = 0;
2883 unsigned int num_globals2 = 0;
2884 int max_index = 0;
2885 unsigned int idx;
2886 asection *asect;
2887 asymbol **new_syms;
2888 bfd_size_type amt;
2889
2890 #ifdef DEBUG
2891 fprintf (stderr, "elf_map_symbols\n");
2892 fflush (stderr);
2893 #endif
2894
2895 for (asect = abfd->sections; asect; asect = asect->next)
2896 {
2897 if (max_index < asect->index)
2898 max_index = asect->index;
2899 }
2900
2901 max_index++;
2902 amt = max_index * sizeof (asymbol *);
2903 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
2904 if (sect_syms == NULL)
2905 return FALSE;
2906 elf_section_syms (abfd) = sect_syms;
2907 elf_num_section_syms (abfd) = max_index;
2908
2909 /* Init sect_syms entries for any section symbols we have already
2910 decided to output. */
2911 for (idx = 0; idx < symcount; idx++)
2912 {
2913 asymbol *sym = syms[idx];
2914
2915 if ((sym->flags & BSF_SECTION_SYM) != 0
2916 && sym->value == 0)
2917 {
2918 asection *sec;
2919
2920 sec = sym->section;
2921
2922 if (sec->owner != NULL)
2923 {
2924 if (sec->owner != abfd)
2925 {
2926 if (sec->output_offset != 0)
2927 continue;
2928
2929 sec = sec->output_section;
2930
2931 /* Empty sections in the input files may have had a
2932 section symbol created for them. (See the comment
2933 near the end of _bfd_generic_link_output_symbols in
2934 linker.c). If the linker script discards such
2935 sections then we will reach this point. Since we know
2936 that we cannot avoid this case, we detect it and skip
2937 the abort and the assignment to the sect_syms array.
2938 To reproduce this particular case try running the
2939 linker testsuite test ld-scripts/weak.exp for an ELF
2940 port that uses the generic linker. */
2941 if (sec->owner == NULL)
2942 continue;
2943
2944 BFD_ASSERT (sec->owner == abfd);
2945 }
2946 sect_syms[sec->index] = syms[idx];
2947 }
2948 }
2949 }
2950
2951 /* Classify all of the symbols. */
2952 for (idx = 0; idx < symcount; idx++)
2953 {
2954 if (!sym_is_global (abfd, syms[idx]))
2955 num_locals++;
2956 else
2957 num_globals++;
2958 }
2959
2960 /* We will be adding a section symbol for each BFD section. Most normal
2961 sections will already have a section symbol in outsymbols, but
2962 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2963 at least in that case. */
2964 for (asect = abfd->sections; asect; asect = asect->next)
2965 {
2966 if (sect_syms[asect->index] == NULL)
2967 {
2968 if (!sym_is_global (abfd, asect->symbol))
2969 num_locals++;
2970 else
2971 num_globals++;
2972 }
2973 }
2974
2975 /* Now sort the symbols so the local symbols are first. */
2976 amt = (num_locals + num_globals) * sizeof (asymbol *);
2977 new_syms = (asymbol **) bfd_alloc (abfd, amt);
2978
2979 if (new_syms == NULL)
2980 return FALSE;
2981
2982 for (idx = 0; idx < symcount; idx++)
2983 {
2984 asymbol *sym = syms[idx];
2985 unsigned int i;
2986
2987 if (!sym_is_global (abfd, sym))
2988 i = num_locals2++;
2989 else
2990 i = num_locals + num_globals2++;
2991 new_syms[i] = sym;
2992 sym->udata.i = i + 1;
2993 }
2994 for (asect = abfd->sections; asect; asect = asect->next)
2995 {
2996 if (sect_syms[asect->index] == NULL)
2997 {
2998 asymbol *sym = asect->symbol;
2999 unsigned int i;
3000
3001 sect_syms[asect->index] = sym;
3002 if (!sym_is_global (abfd, sym))
3003 i = num_locals2++;
3004 else
3005 i = num_locals + num_globals2++;
3006 new_syms[i] = sym;
3007 sym->udata.i = i + 1;
3008 }
3009 }
3010
3011 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3012
3013 elf_num_locals (abfd) = num_locals;
3014 elf_num_globals (abfd) = num_globals;
3015 return TRUE;
3016 }
3017
3018 /* Align to the maximum file alignment that could be required for any
3019 ELF data structure. */
3020
3021 static INLINE file_ptr align_file_position
3022 PARAMS ((file_ptr, int));
3023 static INLINE file_ptr
3024 align_file_position (off, align)
3025 file_ptr off;
3026 int align;
3027 {
3028 return (off + align - 1) & ~(align - 1);
3029 }
3030
3031 /* Assign a file position to a section, optionally aligning to the
3032 required section alignment. */
3033
3034 INLINE file_ptr
3035 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
3036 Elf_Internal_Shdr *i_shdrp;
3037 file_ptr offset;
3038 bfd_boolean align;
3039 {
3040 if (align)
3041 {
3042 unsigned int al;
3043
3044 al = i_shdrp->sh_addralign;
3045 if (al > 1)
3046 offset = BFD_ALIGN (offset, al);
3047 }
3048 i_shdrp->sh_offset = offset;
3049 if (i_shdrp->bfd_section != NULL)
3050 i_shdrp->bfd_section->filepos = offset;
3051 if (i_shdrp->sh_type != SHT_NOBITS)
3052 offset += i_shdrp->sh_size;
3053 return offset;
3054 }
3055
3056 /* Compute the file positions we are going to put the sections at, and
3057 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3058 is not NULL, this is being called by the ELF backend linker. */
3059
3060 bfd_boolean
3061 _bfd_elf_compute_section_file_positions (abfd, link_info)
3062 bfd *abfd;
3063 struct bfd_link_info *link_info;
3064 {
3065 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3066 bfd_boolean failed;
3067 struct bfd_strtab_hash *strtab;
3068 Elf_Internal_Shdr *shstrtab_hdr;
3069
3070 if (abfd->output_has_begun)
3071 return TRUE;
3072
3073 /* Do any elf backend specific processing first. */
3074 if (bed->elf_backend_begin_write_processing)
3075 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3076
3077 if (! prep_headers (abfd))
3078 return FALSE;
3079
3080 /* Post process the headers if necessary. */
3081 if (bed->elf_backend_post_process_headers)
3082 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3083
3084 failed = FALSE;
3085 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3086 if (failed)
3087 return FALSE;
3088
3089 if (!assign_section_numbers (abfd))
3090 return FALSE;
3091
3092 /* The backend linker builds symbol table information itself. */
3093 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3094 {
3095 /* Non-zero if doing a relocatable link. */
3096 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3097
3098 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3099 return FALSE;
3100 }
3101
3102 if (link_info == NULL)
3103 {
3104 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3105 if (failed)
3106 return FALSE;
3107 }
3108
3109 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3110 /* sh_name was set in prep_headers. */
3111 shstrtab_hdr->sh_type = SHT_STRTAB;
3112 shstrtab_hdr->sh_flags = 0;
3113 shstrtab_hdr->sh_addr = 0;
3114 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3115 shstrtab_hdr->sh_entsize = 0;
3116 shstrtab_hdr->sh_link = 0;
3117 shstrtab_hdr->sh_info = 0;
3118 /* sh_offset is set in assign_file_positions_except_relocs. */
3119 shstrtab_hdr->sh_addralign = 1;
3120
3121 if (!assign_file_positions_except_relocs (abfd))
3122 return FALSE;
3123
3124 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3125 {
3126 file_ptr off;
3127 Elf_Internal_Shdr *hdr;
3128
3129 off = elf_tdata (abfd)->next_file_pos;
3130
3131 hdr = &elf_tdata (abfd)->symtab_hdr;
3132 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3133
3134 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3135 if (hdr->sh_size != 0)
3136 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3137
3138 hdr = &elf_tdata (abfd)->strtab_hdr;
3139 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3140
3141 elf_tdata (abfd)->next_file_pos = off;
3142
3143 /* Now that we know where the .strtab section goes, write it
3144 out. */
3145 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3146 || ! _bfd_stringtab_emit (abfd, strtab))
3147 return FALSE;
3148 _bfd_stringtab_free (strtab);
3149 }
3150
3151 abfd->output_has_begun = TRUE;
3152
3153 return TRUE;
3154 }
3155
3156 /* Create a mapping from a set of sections to a program segment. */
3157
3158 static INLINE struct elf_segment_map *
3159 make_mapping (abfd, sections, from, to, phdr)
3160 bfd *abfd;
3161 asection **sections;
3162 unsigned int from;
3163 unsigned int to;
3164 bfd_boolean phdr;
3165 {
3166 struct elf_segment_map *m;
3167 unsigned int i;
3168 asection **hdrpp;
3169 bfd_size_type amt;
3170
3171 amt = sizeof (struct elf_segment_map);
3172 amt += (to - from - 1) * sizeof (asection *);
3173 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3174 if (m == NULL)
3175 return NULL;
3176 m->next = NULL;
3177 m->p_type = PT_LOAD;
3178 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3179 m->sections[i - from] = *hdrpp;
3180 m->count = to - from;
3181
3182 if (from == 0 && phdr)
3183 {
3184 /* Include the headers in the first PT_LOAD segment. */
3185 m->includes_filehdr = 1;
3186 m->includes_phdrs = 1;
3187 }
3188
3189 return m;
3190 }
3191
3192 /* Set up a mapping from BFD sections to program segments. */
3193
3194 static bfd_boolean
3195 map_sections_to_segments (abfd)
3196 bfd *abfd;
3197 {
3198 asection **sections = NULL;
3199 asection *s;
3200 unsigned int i;
3201 unsigned int count;
3202 struct elf_segment_map *mfirst;
3203 struct elf_segment_map **pm;
3204 struct elf_segment_map *m;
3205 asection *last_hdr;
3206 unsigned int phdr_index;
3207 bfd_vma maxpagesize;
3208 asection **hdrpp;
3209 bfd_boolean phdr_in_segment = TRUE;
3210 bfd_boolean writable;
3211 int tls_count = 0;
3212 asection *first_tls = NULL;
3213 asection *dynsec, *eh_frame_hdr;
3214 bfd_size_type amt;
3215
3216 if (elf_tdata (abfd)->segment_map != NULL)
3217 return TRUE;
3218
3219 if (bfd_count_sections (abfd) == 0)
3220 return TRUE;
3221
3222 /* Select the allocated sections, and sort them. */
3223
3224 amt = bfd_count_sections (abfd) * sizeof (asection *);
3225 sections = (asection **) bfd_malloc (amt);
3226 if (sections == NULL)
3227 goto error_return;
3228
3229 i = 0;
3230 for (s = abfd->sections; s != NULL; s = s->next)
3231 {
3232 if ((s->flags & SEC_ALLOC) != 0)
3233 {
3234 sections[i] = s;
3235 ++i;
3236 }
3237 }
3238 BFD_ASSERT (i <= bfd_count_sections (abfd));
3239 count = i;
3240
3241 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3242
3243 /* Build the mapping. */
3244
3245 mfirst = NULL;
3246 pm = &mfirst;
3247
3248 /* If we have a .interp section, then create a PT_PHDR segment for
3249 the program headers and a PT_INTERP segment for the .interp
3250 section. */
3251 s = bfd_get_section_by_name (abfd, ".interp");
3252 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3253 {
3254 amt = sizeof (struct elf_segment_map);
3255 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3256 if (m == NULL)
3257 goto error_return;
3258 m->next = NULL;
3259 m->p_type = PT_PHDR;
3260 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3261 m->p_flags = PF_R | PF_X;
3262 m->p_flags_valid = 1;
3263 m->includes_phdrs = 1;
3264
3265 *pm = m;
3266 pm = &m->next;
3267
3268 amt = sizeof (struct elf_segment_map);
3269 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3270 if (m == NULL)
3271 goto error_return;
3272 m->next = NULL;
3273 m->p_type = PT_INTERP;
3274 m->count = 1;
3275 m->sections[0] = s;
3276
3277 *pm = m;
3278 pm = &m->next;
3279 }
3280
3281 /* Look through the sections. We put sections in the same program
3282 segment when the start of the second section can be placed within
3283 a few bytes of the end of the first section. */
3284 last_hdr = NULL;
3285 phdr_index = 0;
3286 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3287 writable = FALSE;
3288 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3289 if (dynsec != NULL
3290 && (dynsec->flags & SEC_LOAD) == 0)
3291 dynsec = NULL;
3292
3293 /* Deal with -Ttext or something similar such that the first section
3294 is not adjacent to the program headers. This is an
3295 approximation, since at this point we don't know exactly how many
3296 program headers we will need. */
3297 if (count > 0)
3298 {
3299 bfd_size_type phdr_size;
3300
3301 phdr_size = elf_tdata (abfd)->program_header_size;
3302 if (phdr_size == 0)
3303 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3304 if ((abfd->flags & D_PAGED) == 0
3305 || sections[0]->lma < phdr_size
3306 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3307 phdr_in_segment = FALSE;
3308 }
3309
3310 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3311 {
3312 asection *hdr;
3313 bfd_boolean new_segment;
3314
3315 hdr = *hdrpp;
3316
3317 /* See if this section and the last one will fit in the same
3318 segment. */
3319
3320 if (last_hdr == NULL)
3321 {
3322 /* If we don't have a segment yet, then we don't need a new
3323 one (we build the last one after this loop). */
3324 new_segment = FALSE;
3325 }
3326 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3327 {
3328 /* If this section has a different relation between the
3329 virtual address and the load address, then we need a new
3330 segment. */
3331 new_segment = TRUE;
3332 }
3333 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3334 < BFD_ALIGN (hdr->lma, maxpagesize))
3335 {
3336 /* If putting this section in this segment would force us to
3337 skip a page in the segment, then we need a new segment. */
3338 new_segment = TRUE;
3339 }
3340 else if ((last_hdr->flags & SEC_LOAD) == 0
3341 && (hdr->flags & SEC_LOAD) != 0)
3342 {
3343 /* We don't want to put a loadable section after a
3344 nonloadable section in the same segment. */
3345 new_segment = TRUE;
3346 }
3347 else if ((abfd->flags & D_PAGED) == 0)
3348 {
3349 /* If the file is not demand paged, which means that we
3350 don't require the sections to be correctly aligned in the
3351 file, then there is no other reason for a new segment. */
3352 new_segment = FALSE;
3353 }
3354 else if (! writable
3355 && (hdr->flags & SEC_READONLY) == 0
3356 && (((last_hdr->lma + last_hdr->_raw_size - 1)
3357 & ~(maxpagesize - 1))
3358 != (hdr->lma & ~(maxpagesize - 1))))
3359 {
3360 /* We don't want to put a writable section in a read only
3361 segment, unless they are on the same page in memory
3362 anyhow. We already know that the last section does not
3363 bring us past the current section on the page, so the
3364 only case in which the new section is not on the same
3365 page as the previous section is when the previous section
3366 ends precisely on a page boundary. */
3367 new_segment = TRUE;
3368 }
3369 else
3370 {
3371 /* Otherwise, we can use the same segment. */
3372 new_segment = FALSE;
3373 }
3374
3375 if (! new_segment)
3376 {
3377 if ((hdr->flags & SEC_READONLY) == 0)
3378 writable = TRUE;
3379 last_hdr = hdr;
3380 continue;
3381 }
3382
3383 /* We need a new program segment. We must create a new program
3384 header holding all the sections from phdr_index until hdr. */
3385
3386 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3387 if (m == NULL)
3388 goto error_return;
3389
3390 *pm = m;
3391 pm = &m->next;
3392
3393 if ((hdr->flags & SEC_READONLY) == 0)
3394 writable = TRUE;
3395 else
3396 writable = FALSE;
3397
3398 last_hdr = hdr;
3399 phdr_index = i;
3400 phdr_in_segment = FALSE;
3401 }
3402
3403 /* Create a final PT_LOAD program segment. */
3404 if (last_hdr != NULL)
3405 {
3406 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3407 if (m == NULL)
3408 goto error_return;
3409
3410 *pm = m;
3411 pm = &m->next;
3412 }
3413
3414 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3415 if (dynsec != NULL)
3416 {
3417 amt = sizeof (struct elf_segment_map);
3418 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3419 if (m == NULL)
3420 goto error_return;
3421 m->next = NULL;
3422 m->p_type = PT_DYNAMIC;
3423 m->count = 1;
3424 m->sections[0] = dynsec;
3425
3426 *pm = m;
3427 pm = &m->next;
3428 }
3429
3430 /* For each loadable .note section, add a PT_NOTE segment. We don't
3431 use bfd_get_section_by_name, because if we link together
3432 nonloadable .note sections and loadable .note sections, we will
3433 generate two .note sections in the output file. FIXME: Using
3434 names for section types is bogus anyhow. */
3435 for (s = abfd->sections; s != NULL; s = s->next)
3436 {
3437 if ((s->flags & SEC_LOAD) != 0
3438 && strncmp (s->name, ".note", 5) == 0)
3439 {
3440 amt = sizeof (struct elf_segment_map);
3441 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3442 if (m == NULL)
3443 goto error_return;
3444 m->next = NULL;
3445 m->p_type = PT_NOTE;
3446 m->count = 1;
3447 m->sections[0] = s;
3448
3449 *pm = m;
3450 pm = &m->next;
3451 }
3452 if (s->flags & SEC_THREAD_LOCAL)
3453 {
3454 if (! tls_count)
3455 first_tls = s;
3456 tls_count++;
3457 }
3458 }
3459
3460 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3461 if (tls_count > 0)
3462 {
3463 int i;
3464
3465 amt = sizeof (struct elf_segment_map);
3466 amt += (tls_count - 1) * sizeof (asection *);
3467 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3468 if (m == NULL)
3469 goto error_return;
3470 m->next = NULL;
3471 m->p_type = PT_TLS;
3472 m->count = tls_count;
3473 /* Mandated PF_R. */
3474 m->p_flags = PF_R;
3475 m->p_flags_valid = 1;
3476 for (i = 0; i < tls_count; ++i)
3477 {
3478 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3479 m->sections[i] = first_tls;
3480 first_tls = first_tls->next;
3481 }
3482
3483 *pm = m;
3484 pm = &m->next;
3485 }
3486
3487 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3488 segment. */
3489 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3490 if (eh_frame_hdr != NULL
3491 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3492 {
3493 amt = sizeof (struct elf_segment_map);
3494 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3495 if (m == NULL)
3496 goto error_return;
3497 m->next = NULL;
3498 m->p_type = PT_GNU_EH_FRAME;
3499 m->count = 1;
3500 m->sections[0] = eh_frame_hdr->output_section;
3501
3502 *pm = m;
3503 pm = &m->next;
3504 }
3505
3506 free (sections);
3507 sections = NULL;
3508
3509 elf_tdata (abfd)->segment_map = mfirst;
3510 return TRUE;
3511
3512 error_return:
3513 if (sections != NULL)
3514 free (sections);
3515 return FALSE;
3516 }
3517
3518 /* Sort sections by address. */
3519
3520 static int
3521 elf_sort_sections (arg1, arg2)
3522 const PTR arg1;
3523 const PTR arg2;
3524 {
3525 const asection *sec1 = *(const asection **) arg1;
3526 const asection *sec2 = *(const asection **) arg2;
3527 bfd_size_type size1, size2;
3528
3529 /* Sort by LMA first, since this is the address used to
3530 place the section into a segment. */
3531 if (sec1->lma < sec2->lma)
3532 return -1;
3533 else if (sec1->lma > sec2->lma)
3534 return 1;
3535
3536 /* Then sort by VMA. Normally the LMA and the VMA will be
3537 the same, and this will do nothing. */
3538 if (sec1->vma < sec2->vma)
3539 return -1;
3540 else if (sec1->vma > sec2->vma)
3541 return 1;
3542
3543 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3544
3545 #define TOEND(x) (((x)->flags & (SEC_LOAD|SEC_THREAD_LOCAL)) == 0)
3546
3547 if (TOEND (sec1))
3548 {
3549 if (TOEND (sec2))
3550 {
3551 /* If the indicies are the same, do not return 0
3552 here, but continue to try the next comparison. */
3553 if (sec1->target_index - sec2->target_index != 0)
3554 return sec1->target_index - sec2->target_index;
3555 }
3556 else
3557 return 1;
3558 }
3559 else if (TOEND (sec2))
3560 return -1;
3561
3562 #undef TOEND
3563
3564 /* Sort by size, to put zero sized sections
3565 before others at the same address. */
3566
3567 size1 = (sec1->flags & SEC_LOAD) ? sec1->_raw_size : 0;
3568 size2 = (sec2->flags & SEC_LOAD) ? sec2->_raw_size : 0;
3569
3570 if (size1 < size2)
3571 return -1;
3572 if (size1 > size2)
3573 return 1;
3574
3575 return sec1->target_index - sec2->target_index;
3576 }
3577
3578 /* Assign file positions to the sections based on the mapping from
3579 sections to segments. This function also sets up some fields in
3580 the file header, and writes out the program headers. */
3581
3582 static bfd_boolean
3583 assign_file_positions_for_segments (abfd)
3584 bfd *abfd;
3585 {
3586 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3587 unsigned int count;
3588 struct elf_segment_map *m;
3589 unsigned int alloc;
3590 Elf_Internal_Phdr *phdrs;
3591 file_ptr off, voff;
3592 bfd_vma filehdr_vaddr, filehdr_paddr;
3593 bfd_vma phdrs_vaddr, phdrs_paddr;
3594 Elf_Internal_Phdr *p;
3595 bfd_size_type amt;
3596
3597 if (elf_tdata (abfd)->segment_map == NULL)
3598 {
3599 if (! map_sections_to_segments (abfd))
3600 return FALSE;
3601 }
3602 else
3603 {
3604 /* The placement algorithm assumes that non allocated sections are
3605 not in PT_LOAD segments. We ensure this here by removing such
3606 sections from the segment map. */
3607 for (m = elf_tdata (abfd)->segment_map;
3608 m != NULL;
3609 m = m->next)
3610 {
3611 unsigned int new_count;
3612 unsigned int i;
3613
3614 if (m->p_type != PT_LOAD)
3615 continue;
3616
3617 new_count = 0;
3618 for (i = 0; i < m->count; i ++)
3619 {
3620 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3621 {
3622 if (i != new_count)
3623 m->sections[new_count] = m->sections[i];
3624
3625 new_count ++;
3626 }
3627 }
3628
3629 if (new_count != m->count)
3630 m->count = new_count;
3631 }
3632 }
3633
3634 if (bed->elf_backend_modify_segment_map)
3635 {
3636 if (! (*bed->elf_backend_modify_segment_map) (abfd))
3637 return FALSE;
3638 }
3639
3640 count = 0;
3641 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3642 ++count;
3643
3644 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3645 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3646 elf_elfheader (abfd)->e_phnum = count;
3647
3648 if (count == 0)
3649 return TRUE;
3650
3651 /* If we already counted the number of program segments, make sure
3652 that we allocated enough space. This happens when SIZEOF_HEADERS
3653 is used in a linker script. */
3654 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3655 if (alloc != 0 && count > alloc)
3656 {
3657 ((*_bfd_error_handler)
3658 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3659 bfd_get_filename (abfd), alloc, count));
3660 bfd_set_error (bfd_error_bad_value);
3661 return FALSE;
3662 }
3663
3664 if (alloc == 0)
3665 alloc = count;
3666
3667 amt = alloc * sizeof (Elf_Internal_Phdr);
3668 phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
3669 if (phdrs == NULL)
3670 return FALSE;
3671
3672 off = bed->s->sizeof_ehdr;
3673 off += alloc * bed->s->sizeof_phdr;
3674
3675 filehdr_vaddr = 0;
3676 filehdr_paddr = 0;
3677 phdrs_vaddr = 0;
3678 phdrs_paddr = 0;
3679
3680 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3681 m != NULL;
3682 m = m->next, p++)
3683 {
3684 unsigned int i;
3685 asection **secpp;
3686
3687 /* If elf_segment_map is not from map_sections_to_segments, the
3688 sections may not be correctly ordered. NOTE: sorting should
3689 not be done to the PT_NOTE section of a corefile, which may
3690 contain several pseudo-sections artificially created by bfd.
3691 Sorting these pseudo-sections breaks things badly. */
3692 if (m->count > 1
3693 && !(elf_elfheader (abfd)->e_type == ET_CORE
3694 && m->p_type == PT_NOTE))
3695 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3696 elf_sort_sections);
3697
3698 p->p_type = m->p_type;
3699 p->p_flags = m->p_flags;
3700
3701 if (p->p_type == PT_LOAD
3702 && m->count > 0
3703 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3704 {
3705 if ((abfd->flags & D_PAGED) != 0)
3706 off += (m->sections[0]->vma - off) % bed->maxpagesize;
3707 else
3708 {
3709 bfd_size_type align;
3710
3711 align = 0;
3712 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3713 {
3714 bfd_size_type secalign;
3715
3716 secalign = bfd_get_section_alignment (abfd, *secpp);
3717 if (secalign > align)
3718 align = secalign;
3719 }
3720
3721 off += (m->sections[0]->vma - off) % (1 << align);
3722 }
3723 }
3724
3725 if (m->count == 0)
3726 p->p_vaddr = 0;
3727 else
3728 p->p_vaddr = m->sections[0]->vma;
3729
3730 if (m->p_paddr_valid)
3731 p->p_paddr = m->p_paddr;
3732 else if (m->count == 0)
3733 p->p_paddr = 0;
3734 else
3735 p->p_paddr = m->sections[0]->lma;
3736
3737 if (p->p_type == PT_LOAD
3738 && (abfd->flags & D_PAGED) != 0)
3739 p->p_align = bed->maxpagesize;
3740 else if (m->count == 0)
3741 p->p_align = bed->s->file_align;
3742 else
3743 p->p_align = 0;
3744
3745 p->p_offset = 0;
3746 p->p_filesz = 0;
3747 p->p_memsz = 0;
3748
3749 if (m->includes_filehdr)
3750 {
3751 if (! m->p_flags_valid)
3752 p->p_flags |= PF_R;
3753 p->p_offset = 0;
3754 p->p_filesz = bed->s->sizeof_ehdr;
3755 p->p_memsz = bed->s->sizeof_ehdr;
3756 if (m->count > 0)
3757 {
3758 BFD_ASSERT (p->p_type == PT_LOAD);
3759
3760 if (p->p_vaddr < (bfd_vma) off)
3761 {
3762 (*_bfd_error_handler)
3763 (_("%s: Not enough room for program headers, try linking with -N"),
3764 bfd_get_filename (abfd));
3765 bfd_set_error (bfd_error_bad_value);
3766 return FALSE;
3767 }
3768
3769 p->p_vaddr -= off;
3770 if (! m->p_paddr_valid)
3771 p->p_paddr -= off;
3772 }
3773 if (p->p_type == PT_LOAD)
3774 {
3775 filehdr_vaddr = p->p_vaddr;
3776 filehdr_paddr = p->p_paddr;
3777 }
3778 }
3779
3780 if (m->includes_phdrs)
3781 {
3782 if (! m->p_flags_valid)
3783 p->p_flags |= PF_R;
3784
3785 if (m->includes_filehdr)
3786 {
3787 if (p->p_type == PT_LOAD)
3788 {
3789 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3790 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3791 }
3792 }
3793 else
3794 {
3795 p->p_offset = bed->s->sizeof_ehdr;
3796
3797 if (m->count > 0)
3798 {
3799 BFD_ASSERT (p->p_type == PT_LOAD);
3800 p->p_vaddr -= off - p->p_offset;
3801 if (! m->p_paddr_valid)
3802 p->p_paddr -= off - p->p_offset;
3803 }
3804
3805 if (p->p_type == PT_LOAD)
3806 {
3807 phdrs_vaddr = p->p_vaddr;
3808 phdrs_paddr = p->p_paddr;
3809 }
3810 else
3811 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3812 }
3813
3814 p->p_filesz += alloc * bed->s->sizeof_phdr;
3815 p->p_memsz += alloc * bed->s->sizeof_phdr;
3816 }
3817
3818 if (p->p_type == PT_LOAD
3819 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3820 {
3821 if (! m->includes_filehdr && ! m->includes_phdrs)
3822 p->p_offset = off;
3823 else
3824 {
3825 file_ptr adjust;
3826
3827 adjust = off - (p->p_offset + p->p_filesz);
3828 p->p_filesz += adjust;
3829 p->p_memsz += adjust;
3830 }
3831 }
3832
3833 voff = off;
3834
3835 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3836 {
3837 asection *sec;
3838 flagword flags;
3839 bfd_size_type align;
3840
3841 sec = *secpp;
3842 flags = sec->flags;
3843 align = 1 << bfd_get_section_alignment (abfd, sec);
3844
3845 /* The section may have artificial alignment forced by a
3846 link script. Notice this case by the gap between the
3847 cumulative phdr lma and the section's lma. */
3848 if (p->p_paddr + p->p_memsz < sec->lma)
3849 {
3850 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3851
3852 p->p_memsz += adjust;
3853 if (p->p_type == PT_LOAD
3854 || (p->p_type == PT_NOTE
3855 && bfd_get_format (abfd) == bfd_core))
3856 {
3857 off += adjust;
3858 voff += adjust;
3859 }
3860 if ((flags & SEC_LOAD) != 0
3861 || (flags & SEC_THREAD_LOCAL) != 0)
3862 p->p_filesz += adjust;
3863 }
3864
3865 if (p->p_type == PT_LOAD)
3866 {
3867 bfd_signed_vma adjust;
3868
3869 if ((flags & SEC_LOAD) != 0)
3870 {
3871 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3872 if (adjust < 0)
3873 adjust = 0;
3874 }
3875 else if ((flags & SEC_ALLOC) != 0)
3876 {
3877 /* The section VMA must equal the file position
3878 modulo the page size. FIXME: I'm not sure if
3879 this adjustment is really necessary. We used to
3880 not have the SEC_LOAD case just above, and then
3881 this was necessary, but now I'm not sure. */
3882 if ((abfd->flags & D_PAGED) != 0)
3883 adjust = (sec->vma - voff) % bed->maxpagesize;
3884 else
3885 adjust = (sec->vma - voff) % align;
3886 }
3887 else
3888 adjust = 0;
3889
3890 if (adjust != 0)
3891 {
3892 if (i == 0)
3893 {
3894 (* _bfd_error_handler) (_("\
3895 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3896 bfd_section_name (abfd, sec),
3897 sec->lma,
3898 p->p_paddr);
3899 return FALSE;
3900 }
3901 p->p_memsz += adjust;
3902 off += adjust;
3903 voff += adjust;
3904 if ((flags & SEC_LOAD) != 0)
3905 p->p_filesz += adjust;
3906 }
3907
3908 sec->filepos = off;
3909
3910 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3911 used in a linker script we may have a section with
3912 SEC_LOAD clear but which is supposed to have
3913 contents. */
3914 if ((flags & SEC_LOAD) != 0
3915 || (flags & SEC_HAS_CONTENTS) != 0)
3916 off += sec->_raw_size;
3917
3918 if ((flags & SEC_ALLOC) != 0
3919 && ((flags & SEC_LOAD) != 0
3920 || (flags & SEC_THREAD_LOCAL) == 0))
3921 voff += sec->_raw_size;
3922 }
3923
3924 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3925 {
3926 /* The actual "note" segment has i == 0.
3927 This is the one that actually contains everything. */
3928 if (i == 0)
3929 {
3930 sec->filepos = off;
3931 p->p_filesz = sec->_raw_size;
3932 off += sec->_raw_size;
3933 voff = off;
3934 }
3935 else
3936 {
3937 /* Fake sections -- don't need to be written. */
3938 sec->filepos = 0;
3939 sec->_raw_size = 0;
3940 flags = sec->flags = 0;
3941 }
3942 p->p_memsz = 0;
3943 p->p_align = 1;
3944 }
3945 else
3946 {
3947 if ((sec->flags & SEC_LOAD) != 0
3948 || (sec->flags & SEC_THREAD_LOCAL) == 0
3949 || p->p_type == PT_TLS)
3950 p->p_memsz += sec->_raw_size;
3951
3952 if ((flags & SEC_LOAD) != 0)
3953 p->p_filesz += sec->_raw_size;
3954
3955 if (p->p_type == PT_TLS
3956 && sec->_raw_size == 0
3957 && (sec->flags & SEC_HAS_CONTENTS) == 0)
3958 {
3959 struct bfd_link_order *o;
3960 bfd_vma tbss_size = 0;
3961
3962 for (o = sec->link_order_head; o != NULL; o = o->next)
3963 if (tbss_size < o->offset + o->size)
3964 tbss_size = o->offset + o->size;
3965
3966 p->p_memsz += tbss_size;
3967 }
3968
3969 if (align > p->p_align
3970 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3971 p->p_align = align;
3972 }
3973
3974 if (! m->p_flags_valid)
3975 {
3976 p->p_flags |= PF_R;
3977 if ((flags & SEC_CODE) != 0)
3978 p->p_flags |= PF_X;
3979 if ((flags & SEC_READONLY) == 0)
3980 p->p_flags |= PF_W;
3981 }
3982 }
3983 }
3984
3985 /* Now that we have set the section file positions, we can set up
3986 the file positions for the non PT_LOAD segments. */
3987 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3988 m != NULL;
3989 m = m->next, p++)
3990 {
3991 if (p->p_type != PT_LOAD && m->count > 0)
3992 {
3993 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3994 p->p_offset = m->sections[0]->filepos;
3995 }
3996 if (m->count == 0)
3997 {
3998 if (m->includes_filehdr)
3999 {
4000 p->p_vaddr = filehdr_vaddr;
4001 if (! m->p_paddr_valid)
4002 p->p_paddr = filehdr_paddr;
4003 }
4004 else if (m->includes_phdrs)
4005 {
4006 p->p_vaddr = phdrs_vaddr;
4007 if (! m->p_paddr_valid)
4008 p->p_paddr = phdrs_paddr;
4009 }
4010 }
4011 }
4012
4013 /* Clear out any program headers we allocated but did not use. */
4014 for (; count < alloc; count++, p++)
4015 {
4016 memset (p, 0, sizeof *p);
4017 p->p_type = PT_NULL;
4018 }
4019
4020 elf_tdata (abfd)->phdr = phdrs;
4021
4022 elf_tdata (abfd)->next_file_pos = off;
4023
4024 /* Write out the program headers. */
4025 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4026 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
4027 return FALSE;
4028
4029 return TRUE;
4030 }
4031
4032 /* Get the size of the program header.
4033
4034 If this is called by the linker before any of the section VMA's are set, it
4035 can't calculate the correct value for a strange memory layout. This only
4036 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4037 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4038 data segment (exclusive of .interp and .dynamic).
4039
4040 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4041 will be two segments. */
4042
4043 static bfd_size_type
4044 get_program_header_size (abfd)
4045 bfd *abfd;
4046 {
4047 size_t segs;
4048 asection *s;
4049 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4050
4051 /* We can't return a different result each time we're called. */
4052 if (elf_tdata (abfd)->program_header_size != 0)
4053 return elf_tdata (abfd)->program_header_size;
4054
4055 if (elf_tdata (abfd)->segment_map != NULL)
4056 {
4057 struct elf_segment_map *m;
4058
4059 segs = 0;
4060 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4061 ++segs;
4062 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4063 return elf_tdata (abfd)->program_header_size;
4064 }
4065
4066 /* Assume we will need exactly two PT_LOAD segments: one for text
4067 and one for data. */
4068 segs = 2;
4069
4070 s = bfd_get_section_by_name (abfd, ".interp");
4071 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4072 {
4073 /* If we have a loadable interpreter section, we need a
4074 PT_INTERP segment. In this case, assume we also need a
4075 PT_PHDR segment, although that may not be true for all
4076 targets. */
4077 segs += 2;
4078 }
4079
4080 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4081 {
4082 /* We need a PT_DYNAMIC segment. */
4083 ++segs;
4084 }
4085
4086 if (elf_tdata (abfd)->eh_frame_hdr)
4087 {
4088 /* We need a PT_GNU_EH_FRAME segment. */
4089 ++segs;
4090 }
4091
4092 for (s = abfd->sections; s != NULL; s = s->next)
4093 {
4094 if ((s->flags & SEC_LOAD) != 0
4095 && strncmp (s->name, ".note", 5) == 0)
4096 {
4097 /* We need a PT_NOTE segment. */
4098 ++segs;
4099 }
4100 }
4101
4102 for (s = abfd->sections; s != NULL; s = s->next)
4103 {
4104 if (s->flags & SEC_THREAD_LOCAL)
4105 {
4106 /* We need a PT_TLS segment. */
4107 ++segs;
4108 break;
4109 }
4110 }
4111
4112 /* Let the backend count up any program headers it might need. */
4113 if (bed->elf_backend_additional_program_headers)
4114 {
4115 int a;
4116
4117 a = (*bed->elf_backend_additional_program_headers) (abfd);
4118 if (a == -1)
4119 abort ();
4120 segs += a;
4121 }
4122
4123 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4124 return elf_tdata (abfd)->program_header_size;
4125 }
4126
4127 /* Work out the file positions of all the sections. This is called by
4128 _bfd_elf_compute_section_file_positions. All the section sizes and
4129 VMAs must be known before this is called.
4130
4131 We do not consider reloc sections at this point, unless they form
4132 part of the loadable image. Reloc sections are assigned file
4133 positions in assign_file_positions_for_relocs, which is called by
4134 write_object_contents and final_link.
4135
4136 We also don't set the positions of the .symtab and .strtab here. */
4137
4138 static bfd_boolean
4139 assign_file_positions_except_relocs (abfd)
4140 bfd *abfd;
4141 {
4142 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4143 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4144 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4145 unsigned int num_sec = elf_numsections (abfd);
4146 file_ptr off;
4147 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4148
4149 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4150 && bfd_get_format (abfd) != bfd_core)
4151 {
4152 Elf_Internal_Shdr **hdrpp;
4153 unsigned int i;
4154
4155 /* Start after the ELF header. */
4156 off = i_ehdrp->e_ehsize;
4157
4158 /* We are not creating an executable, which means that we are
4159 not creating a program header, and that the actual order of
4160 the sections in the file is unimportant. */
4161 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4162 {
4163 Elf_Internal_Shdr *hdr;
4164
4165 hdr = *hdrpp;
4166 if (hdr->sh_type == SHT_REL
4167 || hdr->sh_type == SHT_RELA
4168 || i == tdata->symtab_section
4169 || i == tdata->symtab_shndx_section
4170 || i == tdata->strtab_section)
4171 {
4172 hdr->sh_offset = -1;
4173 }
4174 else
4175 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4176
4177 if (i == SHN_LORESERVE - 1)
4178 {
4179 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4180 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4181 }
4182 }
4183 }
4184 else
4185 {
4186 unsigned int i;
4187 Elf_Internal_Shdr **hdrpp;
4188
4189 /* Assign file positions for the loaded sections based on the
4190 assignment of sections to segments. */
4191 if (! assign_file_positions_for_segments (abfd))
4192 return FALSE;
4193
4194 /* Assign file positions for the other sections. */
4195
4196 off = elf_tdata (abfd)->next_file_pos;
4197 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4198 {
4199 Elf_Internal_Shdr *hdr;
4200
4201 hdr = *hdrpp;
4202 if (hdr->bfd_section != NULL
4203 && hdr->bfd_section->filepos != 0)
4204 hdr->sh_offset = hdr->bfd_section->filepos;
4205 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4206 {
4207 ((*_bfd_error_handler)
4208 (_("%s: warning: allocated section `%s' not in segment"),
4209 bfd_get_filename (abfd),
4210 (hdr->bfd_section == NULL
4211 ? "*unknown*"
4212 : hdr->bfd_section->name)));
4213 if ((abfd->flags & D_PAGED) != 0)
4214 off += (hdr->sh_addr - off) % bed->maxpagesize;
4215 else
4216 off += (hdr->sh_addr - off) % hdr->sh_addralign;
4217 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4218 FALSE);
4219 }
4220 else if (hdr->sh_type == SHT_REL
4221 || hdr->sh_type == SHT_RELA
4222 || hdr == i_shdrpp[tdata->symtab_section]
4223 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4224 || hdr == i_shdrpp[tdata->strtab_section])
4225 hdr->sh_offset = -1;
4226 else
4227 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4228
4229 if (i == SHN_LORESERVE - 1)
4230 {
4231 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4232 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4233 }
4234 }
4235 }
4236
4237 /* Place the section headers. */
4238 off = align_file_position (off, bed->s->file_align);
4239 i_ehdrp->e_shoff = off;
4240 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4241
4242 elf_tdata (abfd)->next_file_pos = off;
4243
4244 return TRUE;
4245 }
4246
4247 static bfd_boolean
4248 prep_headers (abfd)
4249 bfd *abfd;
4250 {
4251 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4252 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4253 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4254 struct elf_strtab_hash *shstrtab;
4255 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4256
4257 i_ehdrp = elf_elfheader (abfd);
4258 i_shdrp = elf_elfsections (abfd);
4259
4260 shstrtab = _bfd_elf_strtab_init ();
4261 if (shstrtab == NULL)
4262 return FALSE;
4263
4264 elf_shstrtab (abfd) = shstrtab;
4265
4266 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4267 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4268 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4269 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4270
4271 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4272 i_ehdrp->e_ident[EI_DATA] =
4273 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4274 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4275
4276 if ((abfd->flags & DYNAMIC) != 0)
4277 i_ehdrp->e_type = ET_DYN;
4278 else if ((abfd->flags & EXEC_P) != 0)
4279 i_ehdrp->e_type = ET_EXEC;
4280 else if (bfd_get_format (abfd) == bfd_core)
4281 i_ehdrp->e_type = ET_CORE;
4282 else
4283 i_ehdrp->e_type = ET_REL;
4284
4285 switch (bfd_get_arch (abfd))
4286 {
4287 case bfd_arch_unknown:
4288 i_ehdrp->e_machine = EM_NONE;
4289 break;
4290
4291 /* There used to be a long list of cases here, each one setting
4292 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4293 in the corresponding bfd definition. To avoid duplication,
4294 the switch was removed. Machines that need special handling
4295 can generally do it in elf_backend_final_write_processing(),
4296 unless they need the information earlier than the final write.
4297 Such need can generally be supplied by replacing the tests for
4298 e_machine with the conditions used to determine it. */
4299 default:
4300 if (get_elf_backend_data (abfd) != NULL)
4301 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;
4302 else
4303 i_ehdrp->e_machine = EM_NONE;
4304 }
4305
4306 i_ehdrp->e_version = bed->s->ev_current;
4307 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4308
4309 /* No program header, for now. */
4310 i_ehdrp->e_phoff = 0;
4311 i_ehdrp->e_phentsize = 0;
4312 i_ehdrp->e_phnum = 0;
4313
4314 /* Each bfd section is section header entry. */
4315 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4316 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4317
4318 /* If we're building an executable, we'll need a program header table. */
4319 if (abfd->flags & EXEC_P)
4320 {
4321 /* It all happens later. */
4322 #if 0
4323 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
4324
4325 /* elf_build_phdrs() returns a (NULL-terminated) array of
4326 Elf_Internal_Phdrs. */
4327 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
4328 i_ehdrp->e_phoff = outbase;
4329 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
4330 #endif
4331 }
4332 else
4333 {
4334 i_ehdrp->e_phentsize = 0;
4335 i_phdrp = 0;
4336 i_ehdrp->e_phoff = 0;
4337 }
4338
4339 elf_tdata (abfd)->symtab_hdr.sh_name =
4340 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4341 elf_tdata (abfd)->strtab_hdr.sh_name =
4342 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4343 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4344 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4345 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4346 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4347 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4348 return FALSE;
4349
4350 return TRUE;
4351 }
4352
4353 /* Assign file positions for all the reloc sections which are not part
4354 of the loadable file image. */
4355
4356 void
4357 _bfd_elf_assign_file_positions_for_relocs (abfd)
4358 bfd *abfd;
4359 {
4360 file_ptr off;
4361 unsigned int i, num_sec;
4362 Elf_Internal_Shdr **shdrpp;
4363
4364 off = elf_tdata (abfd)->next_file_pos;
4365
4366 num_sec = elf_numsections (abfd);
4367 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4368 {
4369 Elf_Internal_Shdr *shdrp;
4370
4371 shdrp = *shdrpp;
4372 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4373 && shdrp->sh_offset == -1)
4374 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4375 }
4376
4377 elf_tdata (abfd)->next_file_pos = off;
4378 }
4379
4380 bfd_boolean
4381 _bfd_elf_write_object_contents (abfd)
4382 bfd *abfd;
4383 {
4384 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4385 Elf_Internal_Ehdr *i_ehdrp;
4386 Elf_Internal_Shdr **i_shdrp;
4387 bfd_boolean failed;
4388 unsigned int count, num_sec;
4389
4390 if (! abfd->output_has_begun
4391 && ! _bfd_elf_compute_section_file_positions
4392 (abfd, (struct bfd_link_info *) NULL))
4393 return FALSE;
4394
4395 i_shdrp = elf_elfsections (abfd);
4396 i_ehdrp = elf_elfheader (abfd);
4397
4398 failed = FALSE;
4399 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4400 if (failed)
4401 return FALSE;
4402
4403 _bfd_elf_assign_file_positions_for_relocs (abfd);
4404
4405 /* After writing the headers, we need to write the sections too... */
4406 num_sec = elf_numsections (abfd);
4407 for (count = 1; count < num_sec; count++)
4408 {
4409 if (bed->elf_backend_section_processing)
4410 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4411 if (i_shdrp[count]->contents)
4412 {
4413 bfd_size_type amt = i_shdrp[count]->sh_size;
4414
4415 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4416 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4417 return FALSE;
4418 }
4419 if (count == SHN_LORESERVE - 1)
4420 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4421 }
4422
4423 /* Write out the section header names. */
4424 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4425 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4426 return FALSE;
4427
4428 if (bed->elf_backend_final_write_processing)
4429 (*bed->elf_backend_final_write_processing) (abfd,
4430 elf_tdata (abfd)->linker);
4431
4432 return bed->s->write_shdrs_and_ehdr (abfd);
4433 }
4434
4435 bfd_boolean
4436 _bfd_elf_write_corefile_contents (abfd)
4437 bfd *abfd;
4438 {
4439 /* Hopefully this can be done just like an object file. */
4440 return _bfd_elf_write_object_contents (abfd);
4441 }
4442
4443 /* Given a section, search the header to find them. */
4444
4445 int
4446 _bfd_elf_section_from_bfd_section (abfd, asect)
4447 bfd *abfd;
4448 struct sec *asect;
4449 {
4450 struct elf_backend_data *bed;
4451 int index;
4452
4453 if (elf_section_data (asect) != NULL
4454 && elf_section_data (asect)->this_idx != 0)
4455 return elf_section_data (asect)->this_idx;
4456
4457 if (bfd_is_abs_section (asect))
4458 index = SHN_ABS;
4459 else if (bfd_is_com_section (asect))
4460 index = SHN_COMMON;
4461 else if (bfd_is_und_section (asect))
4462 index = SHN_UNDEF;
4463 else
4464 {
4465 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4466 int maxindex = elf_numsections (abfd);
4467
4468 for (index = 1; index < maxindex; index++)
4469 {
4470 Elf_Internal_Shdr *hdr = i_shdrp[index];
4471
4472 if (hdr != NULL && hdr->bfd_section == asect)
4473 return index;
4474 }
4475 index = -1;
4476 }
4477
4478 bed = get_elf_backend_data (abfd);
4479 if (bed->elf_backend_section_from_bfd_section)
4480 {
4481 int retval = index;
4482
4483 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4484 return retval;
4485 }
4486
4487 if (index == -1)
4488 bfd_set_error (bfd_error_nonrepresentable_section);
4489
4490 return index;
4491 }
4492
4493 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4494 on error. */
4495
4496 int
4497 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
4498 bfd *abfd;
4499 asymbol **asym_ptr_ptr;
4500 {
4501 asymbol *asym_ptr = *asym_ptr_ptr;
4502 int idx;
4503 flagword flags = asym_ptr->flags;
4504
4505 /* When gas creates relocations against local labels, it creates its
4506 own symbol for the section, but does put the symbol into the
4507 symbol chain, so udata is 0. When the linker is generating
4508 relocatable output, this section symbol may be for one of the
4509 input sections rather than the output section. */
4510 if (asym_ptr->udata.i == 0
4511 && (flags & BSF_SECTION_SYM)
4512 && asym_ptr->section)
4513 {
4514 int indx;
4515
4516 if (asym_ptr->section->output_section != NULL)
4517 indx = asym_ptr->section->output_section->index;
4518 else
4519 indx = asym_ptr->section->index;
4520 if (indx < elf_num_section_syms (abfd)
4521 && elf_section_syms (abfd)[indx] != NULL)
4522 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4523 }
4524
4525 idx = asym_ptr->udata.i;
4526
4527 if (idx == 0)
4528 {
4529 /* This case can occur when using --strip-symbol on a symbol
4530 which is used in a relocation entry. */
4531 (*_bfd_error_handler)
4532 (_("%s: symbol `%s' required but not present"),
4533 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4534 bfd_set_error (bfd_error_no_symbols);
4535 return -1;
4536 }
4537
4538 #if DEBUG & 4
4539 {
4540 fprintf (stderr,
4541 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4542 (long) asym_ptr, asym_ptr->name, idx, flags,
4543 elf_symbol_flags (flags));
4544 fflush (stderr);
4545 }
4546 #endif
4547
4548 return idx;
4549 }
4550
4551 /* Copy private BFD data. This copies any program header information. */
4552
4553 static bfd_boolean
4554 copy_private_bfd_data (ibfd, obfd)
4555 bfd *ibfd;
4556 bfd *obfd;
4557 {
4558 Elf_Internal_Ehdr *iehdr;
4559 struct elf_segment_map *map;
4560 struct elf_segment_map *map_first;
4561 struct elf_segment_map **pointer_to_map;
4562 Elf_Internal_Phdr *segment;
4563 asection *section;
4564 unsigned int i;
4565 unsigned int num_segments;
4566 bfd_boolean phdr_included = FALSE;
4567 bfd_vma maxpagesize;
4568 struct elf_segment_map *phdr_adjust_seg = NULL;
4569 unsigned int phdr_adjust_num = 0;
4570 struct elf_backend_data *bed;
4571
4572 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4573 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4574 return TRUE;
4575
4576 if (elf_tdata (ibfd)->phdr == NULL)
4577 return TRUE;
4578
4579 bed = get_elf_backend_data (ibfd);
4580 iehdr = elf_elfheader (ibfd);
4581
4582 map_first = NULL;
4583 pointer_to_map = &map_first;
4584
4585 num_segments = elf_elfheader (ibfd)->e_phnum;
4586 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4587
4588 /* Returns the end address of the segment + 1. */
4589 #define SEGMENT_END(segment, start) \
4590 (start + (segment->p_memsz > segment->p_filesz \
4591 ? segment->p_memsz : segment->p_filesz))
4592
4593 #define SECTION_SIZE(section, segment) \
4594 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4595 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4596 ? section->_raw_size : 0)
4597
4598 /* Returns TRUE if the given section is contained within
4599 the given segment. VMA addresses are compared. */
4600 #define IS_CONTAINED_BY_VMA(section, segment) \
4601 (section->vma >= segment->p_vaddr \
4602 && (section->vma + SECTION_SIZE (section, segment) \
4603 <= (SEGMENT_END (segment, segment->p_vaddr))))
4604
4605 /* Returns TRUE if the given section is contained within
4606 the given segment. LMA addresses are compared. */
4607 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4608 (section->lma >= base \
4609 && (section->lma + SECTION_SIZE (section, segment) \
4610 <= SEGMENT_END (segment, base)))
4611
4612 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4613 #define IS_COREFILE_NOTE(p, s) \
4614 (p->p_type == PT_NOTE \
4615 && bfd_get_format (ibfd) == bfd_core \
4616 && s->vma == 0 && s->lma == 0 \
4617 && (bfd_vma) s->filepos >= p->p_offset \
4618 && ((bfd_vma) s->filepos + s->_raw_size \
4619 <= p->p_offset + p->p_filesz))
4620
4621 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4622 linker, which generates a PT_INTERP section with p_vaddr and
4623 p_memsz set to 0. */
4624 #define IS_SOLARIS_PT_INTERP(p, s) \
4625 (p->p_vaddr == 0 \
4626 && p->p_paddr == 0 \
4627 && p->p_memsz == 0 \
4628 && p->p_filesz > 0 \
4629 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4630 && s->_raw_size > 0 \
4631 && (bfd_vma) s->filepos >= p->p_offset \
4632 && ((bfd_vma) s->filepos + s->_raw_size \
4633 <= p->p_offset + p->p_filesz))
4634
4635 /* Decide if the given section should be included in the given segment.
4636 A section will be included if:
4637 1. It is within the address space of the segment -- we use the LMA
4638 if that is set for the segment and the VMA otherwise,
4639 2. It is an allocated segment,
4640 3. There is an output section associated with it,
4641 4. The section has not already been allocated to a previous segment.
4642 5. PT_TLS segment includes only SHF_TLS sections.
4643 6. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. */
4644 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4645 ((((segment->p_paddr \
4646 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4647 : IS_CONTAINED_BY_VMA (section, segment)) \
4648 && (section->flags & SEC_ALLOC) != 0) \
4649 || IS_COREFILE_NOTE (segment, section)) \
4650 && section->output_section != NULL \
4651 && (segment->p_type != PT_TLS \
4652 || (section->flags & SEC_THREAD_LOCAL)) \
4653 && (segment->p_type == PT_LOAD \
4654 || segment->p_type == PT_TLS \
4655 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4656 && ! section->segment_mark)
4657
4658 /* Returns TRUE iff seg1 starts after the end of seg2. */
4659 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4660 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4661
4662 /* Returns TRUE iff seg1 and seg2 overlap. */
4663 #define SEGMENT_OVERLAPS(seg1, seg2) \
4664 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) \
4665 || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4666
4667 /* Initialise the segment mark field. */
4668 for (section = ibfd->sections; section != NULL; section = section->next)
4669 section->segment_mark = FALSE;
4670
4671 /* Scan through the segments specified in the program header
4672 of the input BFD. For this first scan we look for overlaps
4673 in the loadable segments. These can be created by weird
4674 parameters to objcopy. Also, fix some solaris weirdness. */
4675 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4676 i < num_segments;
4677 i++, segment++)
4678 {
4679 unsigned int j;
4680 Elf_Internal_Phdr *segment2;
4681
4682 if (segment->p_type == PT_INTERP)
4683 for (section = ibfd->sections; section; section = section->next)
4684 if (IS_SOLARIS_PT_INTERP (segment, section))
4685 {
4686 /* Mininal change so that the normal section to segment
4687 assigment code will work. */
4688 segment->p_vaddr = section->vma;
4689 break;
4690 }
4691
4692 if (segment->p_type != PT_LOAD)
4693 continue;
4694
4695 /* Determine if this segment overlaps any previous segments. */
4696 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4697 {
4698 bfd_signed_vma extra_length;
4699
4700 if (segment2->p_type != PT_LOAD
4701 || ! SEGMENT_OVERLAPS (segment, segment2))
4702 continue;
4703
4704 /* Merge the two segments together. */
4705 if (segment2->p_vaddr < segment->p_vaddr)
4706 {
4707 /* Extend SEGMENT2 to include SEGMENT and then delete
4708 SEGMENT. */
4709 extra_length =
4710 SEGMENT_END (segment, segment->p_vaddr)
4711 - SEGMENT_END (segment2, segment2->p_vaddr);
4712
4713 if (extra_length > 0)
4714 {
4715 segment2->p_memsz += extra_length;
4716 segment2->p_filesz += extra_length;
4717 }
4718
4719 segment->p_type = PT_NULL;
4720
4721 /* Since we have deleted P we must restart the outer loop. */
4722 i = 0;
4723 segment = elf_tdata (ibfd)->phdr;
4724 break;
4725 }
4726 else
4727 {
4728 /* Extend SEGMENT to include SEGMENT2 and then delete
4729 SEGMENT2. */
4730 extra_length =
4731 SEGMENT_END (segment2, segment2->p_vaddr)
4732 - SEGMENT_END (segment, segment->p_vaddr);
4733
4734 if (extra_length > 0)
4735 {
4736 segment->p_memsz += extra_length;
4737 segment->p_filesz += extra_length;
4738 }
4739
4740 segment2->p_type = PT_NULL;
4741 }
4742 }
4743 }
4744
4745 /* The second scan attempts to assign sections to segments. */
4746 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4747 i < num_segments;
4748 i ++, segment ++)
4749 {
4750 unsigned int section_count;
4751 asection ** sections;
4752 asection * output_section;
4753 unsigned int isec;
4754 bfd_vma matching_lma;
4755 bfd_vma suggested_lma;
4756 unsigned int j;
4757 bfd_size_type amt;
4758
4759 if (segment->p_type == PT_NULL)
4760 continue;
4761
4762 /* Compute how many sections might be placed into this segment. */
4763 section_count = 0;
4764 for (section = ibfd->sections; section != NULL; section = section->next)
4765 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4766 ++section_count;
4767
4768 /* Allocate a segment map big enough to contain all of the
4769 sections we have selected. */
4770 amt = sizeof (struct elf_segment_map);
4771 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4772 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4773 if (map == NULL)
4774 return FALSE;
4775
4776 /* Initialise the fields of the segment map. Default to
4777 using the physical address of the segment in the input BFD. */
4778 map->next = NULL;
4779 map->p_type = segment->p_type;
4780 map->p_flags = segment->p_flags;
4781 map->p_flags_valid = 1;
4782 map->p_paddr = segment->p_paddr;
4783 map->p_paddr_valid = 1;
4784
4785 /* Determine if this segment contains the ELF file header
4786 and if it contains the program headers themselves. */
4787 map->includes_filehdr = (segment->p_offset == 0
4788 && segment->p_filesz >= iehdr->e_ehsize);
4789
4790 map->includes_phdrs = 0;
4791
4792 if (! phdr_included || segment->p_type != PT_LOAD)
4793 {
4794 map->includes_phdrs =
4795 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4796 && (segment->p_offset + segment->p_filesz
4797 >= ((bfd_vma) iehdr->e_phoff
4798 + iehdr->e_phnum * iehdr->e_phentsize)));
4799
4800 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4801 phdr_included = TRUE;
4802 }
4803
4804 if (section_count == 0)
4805 {
4806 /* Special segments, such as the PT_PHDR segment, may contain
4807 no sections, but ordinary, loadable segments should contain
4808 something. They are allowed by the ELF spec however, so only
4809 a warning is produced. */
4810 if (segment->p_type == PT_LOAD)
4811 (*_bfd_error_handler)
4812 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4813 bfd_archive_filename (ibfd));
4814
4815 map->count = 0;
4816 *pointer_to_map = map;
4817 pointer_to_map = &map->next;
4818
4819 continue;
4820 }
4821
4822 /* Now scan the sections in the input BFD again and attempt
4823 to add their corresponding output sections to the segment map.
4824 The problem here is how to handle an output section which has
4825 been moved (ie had its LMA changed). There are four possibilities:
4826
4827 1. None of the sections have been moved.
4828 In this case we can continue to use the segment LMA from the
4829 input BFD.
4830
4831 2. All of the sections have been moved by the same amount.
4832 In this case we can change the segment's LMA to match the LMA
4833 of the first section.
4834
4835 3. Some of the sections have been moved, others have not.
4836 In this case those sections which have not been moved can be
4837 placed in the current segment which will have to have its size,
4838 and possibly its LMA changed, and a new segment or segments will
4839 have to be created to contain the other sections.
4840
4841 4. The sections have been moved, but not be the same amount.
4842 In this case we can change the segment's LMA to match the LMA
4843 of the first section and we will have to create a new segment
4844 or segments to contain the other sections.
4845
4846 In order to save time, we allocate an array to hold the section
4847 pointers that we are interested in. As these sections get assigned
4848 to a segment, they are removed from this array. */
4849
4850 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4851 to work around this long long bug. */
4852 amt = section_count * sizeof (asection *);
4853 sections = (asection **) bfd_malloc (amt);
4854 if (sections == NULL)
4855 return FALSE;
4856
4857 /* Step One: Scan for segment vs section LMA conflicts.
4858 Also add the sections to the section array allocated above.
4859 Also add the sections to the current segment. In the common
4860 case, where the sections have not been moved, this means that
4861 we have completely filled the segment, and there is nothing
4862 more to do. */
4863 isec = 0;
4864 matching_lma = 0;
4865 suggested_lma = 0;
4866
4867 for (j = 0, section = ibfd->sections;
4868 section != NULL;
4869 section = section->next)
4870 {
4871 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4872 {
4873 output_section = section->output_section;
4874
4875 sections[j ++] = section;
4876
4877 /* The Solaris native linker always sets p_paddr to 0.
4878 We try to catch that case here, and set it to the
4879 correct value. Note - some backends require that
4880 p_paddr be left as zero. */
4881 if (segment->p_paddr == 0
4882 && segment->p_vaddr != 0
4883 && (! bed->want_p_paddr_set_to_zero)
4884 && isec == 0
4885 && output_section->lma != 0
4886 && (output_section->vma == (segment->p_vaddr
4887 + (map->includes_filehdr
4888 ? iehdr->e_ehsize
4889 : 0)
4890 + (map->includes_phdrs
4891 ? (iehdr->e_phnum
4892 * iehdr->e_phentsize)
4893 : 0))))
4894 map->p_paddr = segment->p_vaddr;
4895
4896 /* Match up the physical address of the segment with the
4897 LMA address of the output section. */
4898 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4899 || IS_COREFILE_NOTE (segment, section)
4900 || (bed->want_p_paddr_set_to_zero &&
4901 IS_CONTAINED_BY_VMA (output_section, segment))
4902 )
4903 {
4904 if (matching_lma == 0)
4905 matching_lma = output_section->lma;
4906
4907 /* We assume that if the section fits within the segment
4908 then it does not overlap any other section within that
4909 segment. */
4910 map->sections[isec ++] = output_section;
4911 }
4912 else if (suggested_lma == 0)
4913 suggested_lma = output_section->lma;
4914 }
4915 }
4916
4917 BFD_ASSERT (j == section_count);
4918
4919 /* Step Two: Adjust the physical address of the current segment,
4920 if necessary. */
4921 if (isec == section_count)
4922 {
4923 /* All of the sections fitted within the segment as currently
4924 specified. This is the default case. Add the segment to
4925 the list of built segments and carry on to process the next
4926 program header in the input BFD. */
4927 map->count = section_count;
4928 *pointer_to_map = map;
4929 pointer_to_map = &map->next;
4930
4931 free (sections);
4932 continue;
4933 }
4934 else
4935 {
4936 if (matching_lma != 0)
4937 {
4938 /* At least one section fits inside the current segment.
4939 Keep it, but modify its physical address to match the
4940 LMA of the first section that fitted. */
4941 map->p_paddr = matching_lma;
4942 }
4943 else
4944 {
4945 /* None of the sections fitted inside the current segment.
4946 Change the current segment's physical address to match
4947 the LMA of the first section. */
4948 map->p_paddr = suggested_lma;
4949 }
4950
4951 /* Offset the segment physical address from the lma
4952 to allow for space taken up by elf headers. */
4953 if (map->includes_filehdr)
4954 map->p_paddr -= iehdr->e_ehsize;
4955
4956 if (map->includes_phdrs)
4957 {
4958 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4959
4960 /* iehdr->e_phnum is just an estimate of the number
4961 of program headers that we will need. Make a note
4962 here of the number we used and the segment we chose
4963 to hold these headers, so that we can adjust the
4964 offset when we know the correct value. */
4965 phdr_adjust_num = iehdr->e_phnum;
4966 phdr_adjust_seg = map;
4967 }
4968 }
4969
4970 /* Step Three: Loop over the sections again, this time assigning
4971 those that fit to the current segment and removing them from the
4972 sections array; but making sure not to leave large gaps. Once all
4973 possible sections have been assigned to the current segment it is
4974 added to the list of built segments and if sections still remain
4975 to be assigned, a new segment is constructed before repeating
4976 the loop. */
4977 isec = 0;
4978 do
4979 {
4980 map->count = 0;
4981 suggested_lma = 0;
4982
4983 /* Fill the current segment with sections that fit. */
4984 for (j = 0; j < section_count; j++)
4985 {
4986 section = sections[j];
4987
4988 if (section == NULL)
4989 continue;
4990
4991 output_section = section->output_section;
4992
4993 BFD_ASSERT (output_section != NULL);
4994
4995 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4996 || IS_COREFILE_NOTE (segment, section))
4997 {
4998 if (map->count == 0)
4999 {
5000 /* If the first section in a segment does not start at
5001 the beginning of the segment, then something is
5002 wrong. */
5003 if (output_section->lma !=
5004 (map->p_paddr
5005 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5006 + (map->includes_phdrs
5007 ? iehdr->e_phnum * iehdr->e_phentsize
5008 : 0)))
5009 abort ();
5010 }
5011 else
5012 {
5013 asection * prev_sec;
5014
5015 prev_sec = map->sections[map->count - 1];
5016
5017 /* If the gap between the end of the previous section
5018 and the start of this section is more than
5019 maxpagesize then we need to start a new segment. */
5020 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
5021 maxpagesize)
5022 < BFD_ALIGN (output_section->lma, maxpagesize))
5023 || ((prev_sec->lma + prev_sec->_raw_size)
5024 > output_section->lma))
5025 {
5026 if (suggested_lma == 0)
5027 suggested_lma = output_section->lma;
5028
5029 continue;
5030 }
5031 }
5032
5033 map->sections[map->count++] = output_section;
5034 ++isec;
5035 sections[j] = NULL;
5036 section->segment_mark = TRUE;
5037 }
5038 else if (suggested_lma == 0)
5039 suggested_lma = output_section->lma;
5040 }
5041
5042 BFD_ASSERT (map->count > 0);
5043
5044 /* Add the current segment to the list of built segments. */
5045 *pointer_to_map = map;
5046 pointer_to_map = &map->next;
5047
5048 if (isec < section_count)
5049 {
5050 /* We still have not allocated all of the sections to
5051 segments. Create a new segment here, initialise it
5052 and carry on looping. */
5053 amt = sizeof (struct elf_segment_map);
5054 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5055 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
5056 if (map == NULL)
5057 return FALSE;
5058
5059 /* Initialise the fields of the segment map. Set the physical
5060 physical address to the LMA of the first section that has
5061 not yet been assigned. */
5062 map->next = NULL;
5063 map->p_type = segment->p_type;
5064 map->p_flags = segment->p_flags;
5065 map->p_flags_valid = 1;
5066 map->p_paddr = suggested_lma;
5067 map->p_paddr_valid = 1;
5068 map->includes_filehdr = 0;
5069 map->includes_phdrs = 0;
5070 }
5071 }
5072 while (isec < section_count);
5073
5074 free (sections);
5075 }
5076
5077 /* The Solaris linker creates program headers in which all the
5078 p_paddr fields are zero. When we try to objcopy or strip such a
5079 file, we get confused. Check for this case, and if we find it
5080 reset the p_paddr_valid fields. */
5081 for (map = map_first; map != NULL; map = map->next)
5082 if (map->p_paddr != 0)
5083 break;
5084 if (map == NULL)
5085 {
5086 for (map = map_first; map != NULL; map = map->next)
5087 map->p_paddr_valid = 0;
5088 }
5089
5090 elf_tdata (obfd)->segment_map = map_first;
5091
5092 /* If we had to estimate the number of program headers that were
5093 going to be needed, then check our estimate now and adjust
5094 the offset if necessary. */
5095 if (phdr_adjust_seg != NULL)
5096 {
5097 unsigned int count;
5098
5099 for (count = 0, map = map_first; map != NULL; map = map->next)
5100 count++;
5101
5102 if (count > phdr_adjust_num)
5103 phdr_adjust_seg->p_paddr
5104 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5105 }
5106
5107 #if 0
5108 /* Final Step: Sort the segments into ascending order of physical
5109 address. */
5110 if (map_first != NULL)
5111 {
5112 struct elf_segment_map *prev;
5113
5114 prev = map_first;
5115 for (map = map_first->next; map != NULL; prev = map, map = map->next)
5116 {
5117 /* Yes I know - its a bubble sort.... */
5118 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
5119 {
5120 /* Swap map and map->next. */
5121 prev->next = map->next;
5122 map->next = map->next->next;
5123 prev->next->next = map;
5124
5125 /* Restart loop. */
5126 map = map_first;
5127 }
5128 }
5129 }
5130 #endif
5131
5132 #undef SEGMENT_END
5133 #undef SECTION_SIZE
5134 #undef IS_CONTAINED_BY_VMA
5135 #undef IS_CONTAINED_BY_LMA
5136 #undef IS_COREFILE_NOTE
5137 #undef IS_SOLARIS_PT_INTERP
5138 #undef INCLUDE_SECTION_IN_SEGMENT
5139 #undef SEGMENT_AFTER_SEGMENT
5140 #undef SEGMENT_OVERLAPS
5141 return TRUE;
5142 }
5143
5144 /* Copy private section information. This copies over the entsize
5145 field, and sometimes the info field. */
5146
5147 bfd_boolean
5148 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
5149 bfd *ibfd;
5150 asection *isec;
5151 bfd *obfd;
5152 asection *osec;
5153 {
5154 Elf_Internal_Shdr *ihdr, *ohdr;
5155
5156 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5157 || obfd->xvec->flavour != bfd_target_elf_flavour)
5158 return TRUE;
5159
5160 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5161 {
5162 asection *s;
5163
5164 /* Only set up the segments if there are no more SEC_ALLOC
5165 sections. FIXME: This won't do the right thing if objcopy is
5166 used to remove the last SEC_ALLOC section, since objcopy
5167 won't call this routine in that case. */
5168 for (s = isec->next; s != NULL; s = s->next)
5169 if ((s->flags & SEC_ALLOC) != 0)
5170 break;
5171 if (s == NULL)
5172 {
5173 if (! copy_private_bfd_data (ibfd, obfd))
5174 return FALSE;
5175 }
5176 }
5177
5178 ihdr = &elf_section_data (isec)->this_hdr;
5179 ohdr = &elf_section_data (osec)->this_hdr;
5180
5181 ohdr->sh_entsize = ihdr->sh_entsize;
5182
5183 if (ihdr->sh_type == SHT_SYMTAB
5184 || ihdr->sh_type == SHT_DYNSYM
5185 || ihdr->sh_type == SHT_GNU_verneed
5186 || ihdr->sh_type == SHT_GNU_verdef)
5187 ohdr->sh_info = ihdr->sh_info;
5188
5189 /* Set things up for objcopy. The output SHT_GROUP section will
5190 have its elf_next_in_group pointing back to the input group
5191 members. */
5192 elf_next_in_group (osec) = elf_next_in_group (isec);
5193 elf_group_name (osec) = elf_group_name (isec);
5194
5195 osec->use_rela_p = isec->use_rela_p;
5196
5197 return TRUE;
5198 }
5199
5200 /* Copy private symbol information. If this symbol is in a section
5201 which we did not map into a BFD section, try to map the section
5202 index correctly. We use special macro definitions for the mapped
5203 section indices; these definitions are interpreted by the
5204 swap_out_syms function. */
5205
5206 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5207 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5208 #define MAP_STRTAB (SHN_HIOS + 3)
5209 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5210 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5211
5212 bfd_boolean
5213 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
5214 bfd *ibfd;
5215 asymbol *isymarg;
5216 bfd *obfd;
5217 asymbol *osymarg;
5218 {
5219 elf_symbol_type *isym, *osym;
5220
5221 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5222 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5223 return TRUE;
5224
5225 isym = elf_symbol_from (ibfd, isymarg);
5226 osym = elf_symbol_from (obfd, osymarg);
5227
5228 if (isym != NULL
5229 && osym != NULL
5230 && bfd_is_abs_section (isym->symbol.section))
5231 {
5232 unsigned int shndx;
5233
5234 shndx = isym->internal_elf_sym.st_shndx;
5235 if (shndx == elf_onesymtab (ibfd))
5236 shndx = MAP_ONESYMTAB;
5237 else if (shndx == elf_dynsymtab (ibfd))
5238 shndx = MAP_DYNSYMTAB;
5239 else if (shndx == elf_tdata (ibfd)->strtab_section)
5240 shndx = MAP_STRTAB;
5241 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5242 shndx = MAP_SHSTRTAB;
5243 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5244 shndx = MAP_SYM_SHNDX;
5245 osym->internal_elf_sym.st_shndx = shndx;
5246 }
5247
5248 return TRUE;
5249 }
5250
5251 /* Swap out the symbols. */
5252
5253 static bfd_boolean
5254 swap_out_syms (abfd, sttp, relocatable_p)
5255 bfd *abfd;
5256 struct bfd_strtab_hash **sttp;
5257 int relocatable_p;
5258 {
5259 struct elf_backend_data *bed;
5260 int symcount;
5261 asymbol **syms;
5262 struct bfd_strtab_hash *stt;
5263 Elf_Internal_Shdr *symtab_hdr;
5264 Elf_Internal_Shdr *symtab_shndx_hdr;
5265 Elf_Internal_Shdr *symstrtab_hdr;
5266 char *outbound_syms;
5267 char *outbound_shndx;
5268 int idx;
5269 bfd_size_type amt;
5270
5271 if (!elf_map_symbols (abfd))
5272 return FALSE;
5273
5274 /* Dump out the symtabs. */
5275 stt = _bfd_elf_stringtab_init ();
5276 if (stt == NULL)
5277 return FALSE;
5278
5279 bed = get_elf_backend_data (abfd);
5280 symcount = bfd_get_symcount (abfd);
5281 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5282 symtab_hdr->sh_type = SHT_SYMTAB;
5283 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5284 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5285 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5286 symtab_hdr->sh_addralign = bed->s->file_align;
5287
5288 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5289 symstrtab_hdr->sh_type = SHT_STRTAB;
5290
5291 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5292 outbound_syms = bfd_alloc (abfd, amt);
5293 if (outbound_syms == NULL)
5294 return FALSE;
5295 symtab_hdr->contents = (PTR) outbound_syms;
5296
5297 outbound_shndx = NULL;
5298 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5299 if (symtab_shndx_hdr->sh_name != 0)
5300 {
5301 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
5302 outbound_shndx = bfd_zalloc (abfd, amt);
5303 if (outbound_shndx == NULL)
5304 return FALSE;
5305 symtab_shndx_hdr->contents = outbound_shndx;
5306 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5307 symtab_shndx_hdr->sh_size = amt;
5308 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5309 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5310 }
5311
5312 /* now generate the data (for "contents") */
5313 {
5314 /* Fill in zeroth symbol and swap it out. */
5315 Elf_Internal_Sym sym;
5316 sym.st_name = 0;
5317 sym.st_value = 0;
5318 sym.st_size = 0;
5319 sym.st_info = 0;
5320 sym.st_other = 0;
5321 sym.st_shndx = SHN_UNDEF;
5322 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5323 outbound_syms += bed->s->sizeof_sym;
5324 if (outbound_shndx != NULL)
5325 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5326 }
5327
5328 syms = bfd_get_outsymbols (abfd);
5329 for (idx = 0; idx < symcount; idx++)
5330 {
5331 Elf_Internal_Sym sym;
5332 bfd_vma value = syms[idx]->value;
5333 elf_symbol_type *type_ptr;
5334 flagword flags = syms[idx]->flags;
5335 int type;
5336
5337 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5338 {
5339 /* Local section symbols have no name. */
5340 sym.st_name = 0;
5341 }
5342 else
5343 {
5344 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5345 syms[idx]->name,
5346 TRUE, FALSE);
5347 if (sym.st_name == (unsigned long) -1)
5348 return FALSE;
5349 }
5350
5351 type_ptr = elf_symbol_from (abfd, syms[idx]);
5352
5353 if ((flags & BSF_SECTION_SYM) == 0
5354 && bfd_is_com_section (syms[idx]->section))
5355 {
5356 /* ELF common symbols put the alignment into the `value' field,
5357 and the size into the `size' field. This is backwards from
5358 how BFD handles it, so reverse it here. */
5359 sym.st_size = value;
5360 if (type_ptr == NULL
5361 || type_ptr->internal_elf_sym.st_value == 0)
5362 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5363 else
5364 sym.st_value = type_ptr->internal_elf_sym.st_value;
5365 sym.st_shndx = _bfd_elf_section_from_bfd_section
5366 (abfd, syms[idx]->section);
5367 }
5368 else
5369 {
5370 asection *sec = syms[idx]->section;
5371 int shndx;
5372
5373 if (sec->output_section)
5374 {
5375 value += sec->output_offset;
5376 sec = sec->output_section;
5377 }
5378 /* Don't add in the section vma for relocatable output. */
5379 if (! relocatable_p)
5380 value += sec->vma;
5381 sym.st_value = value;
5382 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5383
5384 if (bfd_is_abs_section (sec)
5385 && type_ptr != NULL
5386 && type_ptr->internal_elf_sym.st_shndx != 0)
5387 {
5388 /* This symbol is in a real ELF section which we did
5389 not create as a BFD section. Undo the mapping done
5390 by copy_private_symbol_data. */
5391 shndx = type_ptr->internal_elf_sym.st_shndx;
5392 switch (shndx)
5393 {
5394 case MAP_ONESYMTAB:
5395 shndx = elf_onesymtab (abfd);
5396 break;
5397 case MAP_DYNSYMTAB:
5398 shndx = elf_dynsymtab (abfd);
5399 break;
5400 case MAP_STRTAB:
5401 shndx = elf_tdata (abfd)->strtab_section;
5402 break;
5403 case MAP_SHSTRTAB:
5404 shndx = elf_tdata (abfd)->shstrtab_section;
5405 break;
5406 case MAP_SYM_SHNDX:
5407 shndx = elf_tdata (abfd)->symtab_shndx_section;
5408 break;
5409 default:
5410 break;
5411 }
5412 }
5413 else
5414 {
5415 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5416
5417 if (shndx == -1)
5418 {
5419 asection *sec2;
5420
5421 /* Writing this would be a hell of a lot easier if
5422 we had some decent documentation on bfd, and
5423 knew what to expect of the library, and what to
5424 demand of applications. For example, it
5425 appears that `objcopy' might not set the
5426 section of a symbol to be a section that is
5427 actually in the output file. */
5428 sec2 = bfd_get_section_by_name (abfd, sec->name);
5429 BFD_ASSERT (sec2 != 0);
5430 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5431 BFD_ASSERT (shndx != -1);
5432 }
5433 }
5434
5435 sym.st_shndx = shndx;
5436 }
5437
5438 if ((flags & BSF_THREAD_LOCAL) != 0)
5439 type = STT_TLS;
5440 else if ((flags & BSF_FUNCTION) != 0)
5441 type = STT_FUNC;
5442 else if ((flags & BSF_OBJECT) != 0)
5443 type = STT_OBJECT;
5444 else
5445 type = STT_NOTYPE;
5446
5447 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5448 type = STT_TLS;
5449
5450 /* Processor-specific types */
5451 if (type_ptr != NULL
5452 && bed->elf_backend_get_symbol_type)
5453 type = ((*bed->elf_backend_get_symbol_type)
5454 (&type_ptr->internal_elf_sym, type));
5455
5456 if (flags & BSF_SECTION_SYM)
5457 {
5458 if (flags & BSF_GLOBAL)
5459 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5460 else
5461 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5462 }
5463 else if (bfd_is_com_section (syms[idx]->section))
5464 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5465 else if (bfd_is_und_section (syms[idx]->section))
5466 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5467 ? STB_WEAK
5468 : STB_GLOBAL),
5469 type);
5470 else if (flags & BSF_FILE)
5471 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5472 else
5473 {
5474 int bind = STB_LOCAL;
5475
5476 if (flags & BSF_LOCAL)
5477 bind = STB_LOCAL;
5478 else if (flags & BSF_WEAK)
5479 bind = STB_WEAK;
5480 else if (flags & BSF_GLOBAL)
5481 bind = STB_GLOBAL;
5482
5483 sym.st_info = ELF_ST_INFO (bind, type);
5484 }
5485
5486 if (type_ptr != NULL)
5487 sym.st_other = type_ptr->internal_elf_sym.st_other;
5488 else
5489 sym.st_other = 0;
5490
5491 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5492 outbound_syms += bed->s->sizeof_sym;
5493 if (outbound_shndx != NULL)
5494 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5495 }
5496
5497 *sttp = stt;
5498 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5499 symstrtab_hdr->sh_type = SHT_STRTAB;
5500
5501 symstrtab_hdr->sh_flags = 0;
5502 symstrtab_hdr->sh_addr = 0;
5503 symstrtab_hdr->sh_entsize = 0;
5504 symstrtab_hdr->sh_link = 0;
5505 symstrtab_hdr->sh_info = 0;
5506 symstrtab_hdr->sh_addralign = 1;
5507
5508 return TRUE;
5509 }
5510
5511 /* Return the number of bytes required to hold the symtab vector.
5512
5513 Note that we base it on the count plus 1, since we will null terminate
5514 the vector allocated based on this size. However, the ELF symbol table
5515 always has a dummy entry as symbol #0, so it ends up even. */
5516
5517 long
5518 _bfd_elf_get_symtab_upper_bound (abfd)
5519 bfd *abfd;
5520 {
5521 long symcount;
5522 long symtab_size;
5523 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5524
5525 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5526 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5527 if (symcount > 0)
5528 symtab_size -= sizeof (asymbol *);
5529
5530 return symtab_size;
5531 }
5532
5533 long
5534 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
5535 bfd *abfd;
5536 {
5537 long symcount;
5538 long symtab_size;
5539 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5540
5541 if (elf_dynsymtab (abfd) == 0)
5542 {
5543 bfd_set_error (bfd_error_invalid_operation);
5544 return -1;
5545 }
5546
5547 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5548 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5549 if (symcount > 0)
5550 symtab_size -= sizeof (asymbol *);
5551
5552 return symtab_size;
5553 }
5554
5555 long
5556 _bfd_elf_get_reloc_upper_bound (abfd, asect)
5557 bfd *abfd ATTRIBUTE_UNUSED;
5558 sec_ptr asect;
5559 {
5560 return (asect->reloc_count + 1) * sizeof (arelent *);
5561 }
5562
5563 /* Canonicalize the relocs. */
5564
5565 long
5566 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
5567 bfd *abfd;
5568 sec_ptr section;
5569 arelent **relptr;
5570 asymbol **symbols;
5571 {
5572 arelent *tblptr;
5573 unsigned int i;
5574 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5575
5576 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
5577 return -1;
5578
5579 tblptr = section->relocation;
5580 for (i = 0; i < section->reloc_count; i++)
5581 *relptr++ = tblptr++;
5582
5583 *relptr = NULL;
5584
5585 return section->reloc_count;
5586 }
5587
5588 long
5589 _bfd_elf_get_symtab (abfd, alocation)
5590 bfd *abfd;
5591 asymbol **alocation;
5592 {
5593 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5594 long symcount = bed->s->slurp_symbol_table (abfd, alocation, FALSE);
5595
5596 if (symcount >= 0)
5597 bfd_get_symcount (abfd) = symcount;
5598 return symcount;
5599 }
5600
5601 long
5602 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
5603 bfd *abfd;
5604 asymbol **alocation;
5605 {
5606 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5607 long symcount = bed->s->slurp_symbol_table (abfd, alocation, TRUE);
5608
5609 if (symcount >= 0)
5610 bfd_get_dynamic_symcount (abfd) = symcount;
5611 return symcount;
5612 }
5613
5614 /* Return the size required for the dynamic reloc entries. Any
5615 section that was actually installed in the BFD, and has type
5616 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5617 considered to be a dynamic reloc section. */
5618
5619 long
5620 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
5621 bfd *abfd;
5622 {
5623 long ret;
5624 asection *s;
5625
5626 if (elf_dynsymtab (abfd) == 0)
5627 {
5628 bfd_set_error (bfd_error_invalid_operation);
5629 return -1;
5630 }
5631
5632 ret = sizeof (arelent *);
5633 for (s = abfd->sections; s != NULL; s = s->next)
5634 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5635 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5636 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5637 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5638 * sizeof (arelent *));
5639
5640 return ret;
5641 }
5642
5643 /* Canonicalize the dynamic relocation entries. Note that we return
5644 the dynamic relocations as a single block, although they are
5645 actually associated with particular sections; the interface, which
5646 was designed for SunOS style shared libraries, expects that there
5647 is only one set of dynamic relocs. Any section that was actually
5648 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5649 the dynamic symbol table, is considered to be a dynamic reloc
5650 section. */
5651
5652 long
5653 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5654 bfd *abfd;
5655 arelent **storage;
5656 asymbol **syms;
5657 {
5658 bfd_boolean (*slurp_relocs)
5659 PARAMS ((bfd *, asection *, asymbol **, bfd_boolean));
5660 asection *s;
5661 long ret;
5662
5663 if (elf_dynsymtab (abfd) == 0)
5664 {
5665 bfd_set_error (bfd_error_invalid_operation);
5666 return -1;
5667 }
5668
5669 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5670 ret = 0;
5671 for (s = abfd->sections; s != NULL; s = s->next)
5672 {
5673 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5674 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5675 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5676 {
5677 arelent *p;
5678 long count, i;
5679
5680 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
5681 return -1;
5682 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5683 p = s->relocation;
5684 for (i = 0; i < count; i++)
5685 *storage++ = p++;
5686 ret += count;
5687 }
5688 }
5689
5690 *storage = NULL;
5691
5692 return ret;
5693 }
5694 \f
5695 /* Read in the version information. */
5696
5697 bfd_boolean
5698 _bfd_elf_slurp_version_tables (abfd)
5699 bfd *abfd;
5700 {
5701 bfd_byte *contents = NULL;
5702 bfd_size_type amt;
5703
5704 if (elf_dynverdef (abfd) != 0)
5705 {
5706 Elf_Internal_Shdr *hdr;
5707 Elf_External_Verdef *everdef;
5708 Elf_Internal_Verdef *iverdef;
5709 Elf_Internal_Verdef *iverdefarr;
5710 Elf_Internal_Verdef iverdefmem;
5711 unsigned int i;
5712 unsigned int maxidx;
5713
5714 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5715
5716 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5717 if (contents == NULL)
5718 goto error_return;
5719 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5720 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5721 goto error_return;
5722
5723 /* We know the number of entries in the section but not the maximum
5724 index. Therefore we have to run through all entries and find
5725 the maximum. */
5726 everdef = (Elf_External_Verdef *) contents;
5727 maxidx = 0;
5728 for (i = 0; i < hdr->sh_info; ++i)
5729 {
5730 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5731
5732 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5733 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5734
5735 everdef = ((Elf_External_Verdef *)
5736 ((bfd_byte *) everdef + iverdefmem.vd_next));
5737 }
5738
5739 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5740 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
5741 if (elf_tdata (abfd)->verdef == NULL)
5742 goto error_return;
5743
5744 elf_tdata (abfd)->cverdefs = maxidx;
5745
5746 everdef = (Elf_External_Verdef *) contents;
5747 iverdefarr = elf_tdata (abfd)->verdef;
5748 for (i = 0; i < hdr->sh_info; i++)
5749 {
5750 Elf_External_Verdaux *everdaux;
5751 Elf_Internal_Verdaux *iverdaux;
5752 unsigned int j;
5753
5754 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5755
5756 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5757 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5758
5759 iverdef->vd_bfd = abfd;
5760
5761 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5762 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
5763 if (iverdef->vd_auxptr == NULL)
5764 goto error_return;
5765
5766 everdaux = ((Elf_External_Verdaux *)
5767 ((bfd_byte *) everdef + iverdef->vd_aux));
5768 iverdaux = iverdef->vd_auxptr;
5769 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5770 {
5771 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5772
5773 iverdaux->vda_nodename =
5774 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5775 iverdaux->vda_name);
5776 if (iverdaux->vda_nodename == NULL)
5777 goto error_return;
5778
5779 if (j + 1 < iverdef->vd_cnt)
5780 iverdaux->vda_nextptr = iverdaux + 1;
5781 else
5782 iverdaux->vda_nextptr = NULL;
5783
5784 everdaux = ((Elf_External_Verdaux *)
5785 ((bfd_byte *) everdaux + iverdaux->vda_next));
5786 }
5787
5788 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5789
5790 if (i + 1 < hdr->sh_info)
5791 iverdef->vd_nextdef = iverdef + 1;
5792 else
5793 iverdef->vd_nextdef = NULL;
5794
5795 everdef = ((Elf_External_Verdef *)
5796 ((bfd_byte *) everdef + iverdef->vd_next));
5797 }
5798
5799 free (contents);
5800 contents = NULL;
5801 }
5802
5803 if (elf_dynverref (abfd) != 0)
5804 {
5805 Elf_Internal_Shdr *hdr;
5806 Elf_External_Verneed *everneed;
5807 Elf_Internal_Verneed *iverneed;
5808 unsigned int i;
5809
5810 hdr = &elf_tdata (abfd)->dynverref_hdr;
5811
5812 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5813 elf_tdata (abfd)->verref =
5814 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
5815 if (elf_tdata (abfd)->verref == NULL)
5816 goto error_return;
5817
5818 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5819
5820 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5821 if (contents == NULL)
5822 goto error_return;
5823 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5824 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5825 goto error_return;
5826
5827 everneed = (Elf_External_Verneed *) contents;
5828 iverneed = elf_tdata (abfd)->verref;
5829 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5830 {
5831 Elf_External_Vernaux *evernaux;
5832 Elf_Internal_Vernaux *ivernaux;
5833 unsigned int j;
5834
5835 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5836
5837 iverneed->vn_bfd = abfd;
5838
5839 iverneed->vn_filename =
5840 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5841 iverneed->vn_file);
5842 if (iverneed->vn_filename == NULL)
5843 goto error_return;
5844
5845 amt = iverneed->vn_cnt;
5846 amt *= sizeof (Elf_Internal_Vernaux);
5847 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
5848
5849 evernaux = ((Elf_External_Vernaux *)
5850 ((bfd_byte *) everneed + iverneed->vn_aux));
5851 ivernaux = iverneed->vn_auxptr;
5852 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5853 {
5854 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5855
5856 ivernaux->vna_nodename =
5857 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5858 ivernaux->vna_name);
5859 if (ivernaux->vna_nodename == NULL)
5860 goto error_return;
5861
5862 if (j + 1 < iverneed->vn_cnt)
5863 ivernaux->vna_nextptr = ivernaux + 1;
5864 else
5865 ivernaux->vna_nextptr = NULL;
5866
5867 evernaux = ((Elf_External_Vernaux *)
5868 ((bfd_byte *) evernaux + ivernaux->vna_next));
5869 }
5870
5871 if (i + 1 < hdr->sh_info)
5872 iverneed->vn_nextref = iverneed + 1;
5873 else
5874 iverneed->vn_nextref = NULL;
5875
5876 everneed = ((Elf_External_Verneed *)
5877 ((bfd_byte *) everneed + iverneed->vn_next));
5878 }
5879
5880 free (contents);
5881 contents = NULL;
5882 }
5883
5884 return TRUE;
5885
5886 error_return:
5887 if (contents == NULL)
5888 free (contents);
5889 return FALSE;
5890 }
5891 \f
5892 asymbol *
5893 _bfd_elf_make_empty_symbol (abfd)
5894 bfd *abfd;
5895 {
5896 elf_symbol_type *newsym;
5897 bfd_size_type amt = sizeof (elf_symbol_type);
5898
5899 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
5900 if (!newsym)
5901 return NULL;
5902 else
5903 {
5904 newsym->symbol.the_bfd = abfd;
5905 return &newsym->symbol;
5906 }
5907 }
5908
5909 void
5910 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
5911 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5912 asymbol *symbol;
5913 symbol_info *ret;
5914 {
5915 bfd_symbol_info (symbol, ret);
5916 }
5917
5918 /* Return whether a symbol name implies a local symbol. Most targets
5919 use this function for the is_local_label_name entry point, but some
5920 override it. */
5921
5922 bfd_boolean
5923 _bfd_elf_is_local_label_name (abfd, name)
5924 bfd *abfd ATTRIBUTE_UNUSED;
5925 const char *name;
5926 {
5927 /* Normal local symbols start with ``.L''. */
5928 if (name[0] == '.' && name[1] == 'L')
5929 return TRUE;
5930
5931 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5932 DWARF debugging symbols starting with ``..''. */
5933 if (name[0] == '.' && name[1] == '.')
5934 return TRUE;
5935
5936 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5937 emitting DWARF debugging output. I suspect this is actually a
5938 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5939 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5940 underscore to be emitted on some ELF targets). For ease of use,
5941 we treat such symbols as local. */
5942 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5943 return TRUE;
5944
5945 return FALSE;
5946 }
5947
5948 alent *
5949 _bfd_elf_get_lineno (ignore_abfd, symbol)
5950 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5951 asymbol *symbol ATTRIBUTE_UNUSED;
5952 {
5953 abort ();
5954 return NULL;
5955 }
5956
5957 bfd_boolean
5958 _bfd_elf_set_arch_mach (abfd, arch, machine)
5959 bfd *abfd;
5960 enum bfd_architecture arch;
5961 unsigned long machine;
5962 {
5963 /* If this isn't the right architecture for this backend, and this
5964 isn't the generic backend, fail. */
5965 if (arch != get_elf_backend_data (abfd)->arch
5966 && arch != bfd_arch_unknown
5967 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5968 return FALSE;
5969
5970 return bfd_default_set_arch_mach (abfd, arch, machine);
5971 }
5972
5973 /* Find the function to a particular section and offset,
5974 for error reporting. */
5975
5976 static bfd_boolean
5977 elf_find_function (abfd, section, symbols, offset,
5978 filename_ptr, functionname_ptr)
5979 bfd *abfd ATTRIBUTE_UNUSED;
5980 asection *section;
5981 asymbol **symbols;
5982 bfd_vma offset;
5983 const char **filename_ptr;
5984 const char **functionname_ptr;
5985 {
5986 const char *filename;
5987 asymbol *func;
5988 bfd_vma low_func;
5989 asymbol **p;
5990
5991 filename = NULL;
5992 func = NULL;
5993 low_func = 0;
5994
5995 for (p = symbols; *p != NULL; p++)
5996 {
5997 elf_symbol_type *q;
5998
5999 q = (elf_symbol_type *) *p;
6000
6001 if (bfd_get_section (&q->symbol) != section)
6002 continue;
6003
6004 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6005 {
6006 default:
6007 break;
6008 case STT_FILE:
6009 filename = bfd_asymbol_name (&q->symbol);
6010 break;
6011 case STT_NOTYPE:
6012 case STT_FUNC:
6013 if (q->symbol.section == section
6014 && q->symbol.value >= low_func
6015 && q->symbol.value <= offset)
6016 {
6017 func = (asymbol *) q;
6018 low_func = q->symbol.value;
6019 }
6020 break;
6021 }
6022 }
6023
6024 if (func == NULL)
6025 return FALSE;
6026
6027 if (filename_ptr)
6028 *filename_ptr = filename;
6029 if (functionname_ptr)
6030 *functionname_ptr = bfd_asymbol_name (func);
6031
6032 return TRUE;
6033 }
6034
6035 /* Find the nearest line to a particular section and offset,
6036 for error reporting. */
6037
6038 bfd_boolean
6039 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
6040 filename_ptr, functionname_ptr, line_ptr)
6041 bfd *abfd;
6042 asection *section;
6043 asymbol **symbols;
6044 bfd_vma offset;
6045 const char **filename_ptr;
6046 const char **functionname_ptr;
6047 unsigned int *line_ptr;
6048 {
6049 bfd_boolean found;
6050
6051 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6052 filename_ptr, functionname_ptr,
6053 line_ptr))
6054 {
6055 if (!*functionname_ptr)
6056 elf_find_function (abfd, section, symbols, offset,
6057 *filename_ptr ? NULL : filename_ptr,
6058 functionname_ptr);
6059
6060 return TRUE;
6061 }
6062
6063 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6064 filename_ptr, functionname_ptr,
6065 line_ptr, 0,
6066 &elf_tdata (abfd)->dwarf2_find_line_info))
6067 {
6068 if (!*functionname_ptr)
6069 elf_find_function (abfd, section, symbols, offset,
6070 *filename_ptr ? NULL : filename_ptr,
6071 functionname_ptr);
6072
6073 return TRUE;
6074 }
6075
6076 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6077 &found, filename_ptr,
6078 functionname_ptr, line_ptr,
6079 &elf_tdata (abfd)->line_info))
6080 return FALSE;
6081 if (found && (*functionname_ptr || *line_ptr))
6082 return TRUE;
6083
6084 if (symbols == NULL)
6085 return FALSE;
6086
6087 if (! elf_find_function (abfd, section, symbols, offset,
6088 filename_ptr, functionname_ptr))
6089 return FALSE;
6090
6091 *line_ptr = 0;
6092 return TRUE;
6093 }
6094
6095 int
6096 _bfd_elf_sizeof_headers (abfd, reloc)
6097 bfd *abfd;
6098 bfd_boolean reloc;
6099 {
6100 int ret;
6101
6102 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6103 if (! reloc)
6104 ret += get_program_header_size (abfd);
6105 return ret;
6106 }
6107
6108 bfd_boolean
6109 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
6110 bfd *abfd;
6111 sec_ptr section;
6112 PTR location;
6113 file_ptr offset;
6114 bfd_size_type count;
6115 {
6116 Elf_Internal_Shdr *hdr;
6117 bfd_signed_vma pos;
6118
6119 if (! abfd->output_has_begun
6120 && ! (_bfd_elf_compute_section_file_positions
6121 (abfd, (struct bfd_link_info *) NULL)))
6122 return FALSE;
6123
6124 hdr = &elf_section_data (section)->this_hdr;
6125 pos = hdr->sh_offset + offset;
6126 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6127 || bfd_bwrite (location, count, abfd) != count)
6128 return FALSE;
6129
6130 return TRUE;
6131 }
6132
6133 void
6134 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
6135 bfd *abfd ATTRIBUTE_UNUSED;
6136 arelent *cache_ptr ATTRIBUTE_UNUSED;
6137 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
6138 {
6139 abort ();
6140 }
6141
6142 /* Try to convert a non-ELF reloc into an ELF one. */
6143
6144 bfd_boolean
6145 _bfd_elf_validate_reloc (abfd, areloc)
6146 bfd *abfd;
6147 arelent *areloc;
6148 {
6149 /* Check whether we really have an ELF howto. */
6150
6151 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6152 {
6153 bfd_reloc_code_real_type code;
6154 reloc_howto_type *howto;
6155
6156 /* Alien reloc: Try to determine its type to replace it with an
6157 equivalent ELF reloc. */
6158
6159 if (areloc->howto->pc_relative)
6160 {
6161 switch (areloc->howto->bitsize)
6162 {
6163 case 8:
6164 code = BFD_RELOC_8_PCREL;
6165 break;
6166 case 12:
6167 code = BFD_RELOC_12_PCREL;
6168 break;
6169 case 16:
6170 code = BFD_RELOC_16_PCREL;
6171 break;
6172 case 24:
6173 code = BFD_RELOC_24_PCREL;
6174 break;
6175 case 32:
6176 code = BFD_RELOC_32_PCREL;
6177 break;
6178 case 64:
6179 code = BFD_RELOC_64_PCREL;
6180 break;
6181 default:
6182 goto fail;
6183 }
6184
6185 howto = bfd_reloc_type_lookup (abfd, code);
6186
6187 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6188 {
6189 if (howto->pcrel_offset)
6190 areloc->addend += areloc->address;
6191 else
6192 areloc->addend -= areloc->address; /* addend is unsigned!! */
6193 }
6194 }
6195 else
6196 {
6197 switch (areloc->howto->bitsize)
6198 {
6199 case 8:
6200 code = BFD_RELOC_8;
6201 break;
6202 case 14:
6203 code = BFD_RELOC_14;
6204 break;
6205 case 16:
6206 code = BFD_RELOC_16;
6207 break;
6208 case 26:
6209 code = BFD_RELOC_26;
6210 break;
6211 case 32:
6212 code = BFD_RELOC_32;
6213 break;
6214 case 64:
6215 code = BFD_RELOC_64;
6216 break;
6217 default:
6218 goto fail;
6219 }
6220
6221 howto = bfd_reloc_type_lookup (abfd, code);
6222 }
6223
6224 if (howto)
6225 areloc->howto = howto;
6226 else
6227 goto fail;
6228 }
6229
6230 return TRUE;
6231
6232 fail:
6233 (*_bfd_error_handler)
6234 (_("%s: unsupported relocation type %s"),
6235 bfd_archive_filename (abfd), areloc->howto->name);
6236 bfd_set_error (bfd_error_bad_value);
6237 return FALSE;
6238 }
6239
6240 bfd_boolean
6241 _bfd_elf_close_and_cleanup (abfd)
6242 bfd *abfd;
6243 {
6244 if (bfd_get_format (abfd) == bfd_object)
6245 {
6246 if (elf_shstrtab (abfd) != NULL)
6247 _bfd_elf_strtab_free (elf_shstrtab (abfd));
6248 }
6249
6250 return _bfd_generic_close_and_cleanup (abfd);
6251 }
6252
6253 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6254 in the relocation's offset. Thus we cannot allow any sort of sanity
6255 range-checking to interfere. There is nothing else to do in processing
6256 this reloc. */
6257
6258 bfd_reloc_status_type
6259 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
6260 bfd *abfd ATTRIBUTE_UNUSED;
6261 arelent *re ATTRIBUTE_UNUSED;
6262 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
6263 PTR data ATTRIBUTE_UNUSED;
6264 asection *is ATTRIBUTE_UNUSED;
6265 bfd *obfd ATTRIBUTE_UNUSED;
6266 char **errmsg ATTRIBUTE_UNUSED;
6267 {
6268 return bfd_reloc_ok;
6269 }
6270 \f
6271 /* Elf core file support. Much of this only works on native
6272 toolchains, since we rely on knowing the
6273 machine-dependent procfs structure in order to pick
6274 out details about the corefile. */
6275
6276 #ifdef HAVE_SYS_PROCFS_H
6277 # include <sys/procfs.h>
6278 #endif
6279
6280 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6281
6282 static int
6283 elfcore_make_pid (abfd)
6284 bfd *abfd;
6285 {
6286 return ((elf_tdata (abfd)->core_lwpid << 16)
6287 + (elf_tdata (abfd)->core_pid));
6288 }
6289
6290 /* If there isn't a section called NAME, make one, using
6291 data from SECT. Note, this function will generate a
6292 reference to NAME, so you shouldn't deallocate or
6293 overwrite it. */
6294
6295 static bfd_boolean
6296 elfcore_maybe_make_sect (abfd, name, sect)
6297 bfd *abfd;
6298 char *name;
6299 asection *sect;
6300 {
6301 asection *sect2;
6302
6303 if (bfd_get_section_by_name (abfd, name) != NULL)
6304 return TRUE;
6305
6306 sect2 = bfd_make_section (abfd, name);
6307 if (sect2 == NULL)
6308 return FALSE;
6309
6310 sect2->_raw_size = sect->_raw_size;
6311 sect2->filepos = sect->filepos;
6312 sect2->flags = sect->flags;
6313 sect2->alignment_power = sect->alignment_power;
6314 return TRUE;
6315 }
6316
6317 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6318 actually creates up to two pseudosections:
6319 - For the single-threaded case, a section named NAME, unless
6320 such a section already exists.
6321 - For the multi-threaded case, a section named "NAME/PID", where
6322 PID is elfcore_make_pid (abfd).
6323 Both pseudosections have identical contents. */
6324 bfd_boolean
6325 _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
6326 bfd *abfd;
6327 char *name;
6328 size_t size;
6329 ufile_ptr filepos;
6330 {
6331 char buf[100];
6332 char *threaded_name;
6333 size_t len;
6334 asection *sect;
6335
6336 /* Build the section name. */
6337
6338 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
6339 len = strlen (buf) + 1;
6340 threaded_name = bfd_alloc (abfd, (bfd_size_type) len);
6341 if (threaded_name == NULL)
6342 return FALSE;
6343 memcpy (threaded_name, buf, len);
6344
6345 sect = bfd_make_section (abfd, threaded_name);
6346 if (sect == NULL)
6347 return FALSE;
6348 sect->_raw_size = size;
6349 sect->filepos = filepos;
6350 sect->flags = SEC_HAS_CONTENTS;
6351 sect->alignment_power = 2;
6352
6353 return elfcore_maybe_make_sect (abfd, name, sect);
6354 }
6355
6356 /* prstatus_t exists on:
6357 solaris 2.5+
6358 linux 2.[01] + glibc
6359 unixware 4.2
6360 */
6361
6362 #if defined (HAVE_PRSTATUS_T)
6363 static bfd_boolean elfcore_grok_prstatus
6364 PARAMS ((bfd *, Elf_Internal_Note *));
6365
6366 static bfd_boolean
6367 elfcore_grok_prstatus (abfd, note)
6368 bfd *abfd;
6369 Elf_Internal_Note *note;
6370 {
6371 size_t raw_size;
6372 int offset;
6373
6374 if (note->descsz == sizeof (prstatus_t))
6375 {
6376 prstatus_t prstat;
6377
6378 raw_size = sizeof (prstat.pr_reg);
6379 offset = offsetof (prstatus_t, pr_reg);
6380 memcpy (&prstat, note->descdata, sizeof (prstat));
6381
6382 /* Do not overwrite the core signal if it
6383 has already been set by another thread. */
6384 if (elf_tdata (abfd)->core_signal == 0)
6385 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6386 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6387
6388 /* pr_who exists on:
6389 solaris 2.5+
6390 unixware 4.2
6391 pr_who doesn't exist on:
6392 linux 2.[01]
6393 */
6394 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6395 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6396 #endif
6397 }
6398 #if defined (HAVE_PRSTATUS32_T)
6399 else if (note->descsz == sizeof (prstatus32_t))
6400 {
6401 /* 64-bit host, 32-bit corefile */
6402 prstatus32_t prstat;
6403
6404 raw_size = sizeof (prstat.pr_reg);
6405 offset = offsetof (prstatus32_t, pr_reg);
6406 memcpy (&prstat, note->descdata, sizeof (prstat));
6407
6408 /* Do not overwrite the core signal if it
6409 has already been set by another thread. */
6410 if (elf_tdata (abfd)->core_signal == 0)
6411 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6412 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6413
6414 /* pr_who exists on:
6415 solaris 2.5+
6416 unixware 4.2
6417 pr_who doesn't exist on:
6418 linux 2.[01]
6419 */
6420 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6421 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6422 #endif
6423 }
6424 #endif /* HAVE_PRSTATUS32_T */
6425 else
6426 {
6427 /* Fail - we don't know how to handle any other
6428 note size (ie. data object type). */
6429 return TRUE;
6430 }
6431
6432 /* Make a ".reg/999" section and a ".reg" section. */
6433 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6434 raw_size, note->descpos + offset);
6435 }
6436 #endif /* defined (HAVE_PRSTATUS_T) */
6437
6438 /* Create a pseudosection containing the exact contents of NOTE. */
6439 static bfd_boolean
6440 elfcore_make_note_pseudosection (abfd, name, note)
6441 bfd *abfd;
6442 char *name;
6443 Elf_Internal_Note *note;
6444 {
6445 return _bfd_elfcore_make_pseudosection (abfd, name,
6446 note->descsz, note->descpos);
6447 }
6448
6449 /* There isn't a consistent prfpregset_t across platforms,
6450 but it doesn't matter, because we don't have to pick this
6451 data structure apart. */
6452
6453 static bfd_boolean
6454 elfcore_grok_prfpreg (abfd, note)
6455 bfd *abfd;
6456 Elf_Internal_Note *note;
6457 {
6458 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6459 }
6460
6461 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6462 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6463 literally. */
6464
6465 static bfd_boolean
6466 elfcore_grok_prxfpreg (abfd, note)
6467 bfd *abfd;
6468 Elf_Internal_Note *note;
6469 {
6470 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6471 }
6472
6473 #if defined (HAVE_PRPSINFO_T)
6474 typedef prpsinfo_t elfcore_psinfo_t;
6475 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6476 typedef prpsinfo32_t elfcore_psinfo32_t;
6477 #endif
6478 #endif
6479
6480 #if defined (HAVE_PSINFO_T)
6481 typedef psinfo_t elfcore_psinfo_t;
6482 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6483 typedef psinfo32_t elfcore_psinfo32_t;
6484 #endif
6485 #endif
6486
6487 /* return a malloc'ed copy of a string at START which is at
6488 most MAX bytes long, possibly without a terminating '\0'.
6489 the copy will always have a terminating '\0'. */
6490
6491 char *
6492 _bfd_elfcore_strndup (abfd, start, max)
6493 bfd *abfd;
6494 char *start;
6495 size_t max;
6496 {
6497 char *dups;
6498 char *end = memchr (start, '\0', max);
6499 size_t len;
6500
6501 if (end == NULL)
6502 len = max;
6503 else
6504 len = end - start;
6505
6506 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
6507 if (dups == NULL)
6508 return NULL;
6509
6510 memcpy (dups, start, len);
6511 dups[len] = '\0';
6512
6513 return dups;
6514 }
6515
6516 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6517 static bfd_boolean elfcore_grok_psinfo
6518 PARAMS ((bfd *, Elf_Internal_Note *));
6519
6520 static bfd_boolean
6521 elfcore_grok_psinfo (abfd, note)
6522 bfd *abfd;
6523 Elf_Internal_Note *note;
6524 {
6525 if (note->descsz == sizeof (elfcore_psinfo_t))
6526 {
6527 elfcore_psinfo_t psinfo;
6528
6529 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6530
6531 elf_tdata (abfd)->core_program
6532 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6533 sizeof (psinfo.pr_fname));
6534
6535 elf_tdata (abfd)->core_command
6536 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6537 sizeof (psinfo.pr_psargs));
6538 }
6539 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6540 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6541 {
6542 /* 64-bit host, 32-bit corefile */
6543 elfcore_psinfo32_t psinfo;
6544
6545 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6546
6547 elf_tdata (abfd)->core_program
6548 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6549 sizeof (psinfo.pr_fname));
6550
6551 elf_tdata (abfd)->core_command
6552 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6553 sizeof (psinfo.pr_psargs));
6554 }
6555 #endif
6556
6557 else
6558 {
6559 /* Fail - we don't know how to handle any other
6560 note size (ie. data object type). */
6561 return TRUE;
6562 }
6563
6564 /* Note that for some reason, a spurious space is tacked
6565 onto the end of the args in some (at least one anyway)
6566 implementations, so strip it off if it exists. */
6567
6568 {
6569 char *command = elf_tdata (abfd)->core_command;
6570 int n = strlen (command);
6571
6572 if (0 < n && command[n - 1] == ' ')
6573 command[n - 1] = '\0';
6574 }
6575
6576 return TRUE;
6577 }
6578 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6579
6580 #if defined (HAVE_PSTATUS_T)
6581 static bfd_boolean elfcore_grok_pstatus
6582 PARAMS ((bfd *, Elf_Internal_Note *));
6583
6584 static bfd_boolean
6585 elfcore_grok_pstatus (abfd, note)
6586 bfd *abfd;
6587 Elf_Internal_Note *note;
6588 {
6589 if (note->descsz == sizeof (pstatus_t)
6590 #if defined (HAVE_PXSTATUS_T)
6591 || note->descsz == sizeof (pxstatus_t)
6592 #endif
6593 )
6594 {
6595 pstatus_t pstat;
6596
6597 memcpy (&pstat, note->descdata, sizeof (pstat));
6598
6599 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6600 }
6601 #if defined (HAVE_PSTATUS32_T)
6602 else if (note->descsz == sizeof (pstatus32_t))
6603 {
6604 /* 64-bit host, 32-bit corefile */
6605 pstatus32_t pstat;
6606
6607 memcpy (&pstat, note->descdata, sizeof (pstat));
6608
6609 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6610 }
6611 #endif
6612 /* Could grab some more details from the "representative"
6613 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6614 NT_LWPSTATUS note, presumably. */
6615
6616 return TRUE;
6617 }
6618 #endif /* defined (HAVE_PSTATUS_T) */
6619
6620 #if defined (HAVE_LWPSTATUS_T)
6621 static bfd_boolean elfcore_grok_lwpstatus
6622 PARAMS ((bfd *, Elf_Internal_Note *));
6623
6624 static bfd_boolean
6625 elfcore_grok_lwpstatus (abfd, note)
6626 bfd *abfd;
6627 Elf_Internal_Note *note;
6628 {
6629 lwpstatus_t lwpstat;
6630 char buf[100];
6631 char *name;
6632 size_t len;
6633 asection *sect;
6634
6635 if (note->descsz != sizeof (lwpstat)
6636 #if defined (HAVE_LWPXSTATUS_T)
6637 && note->descsz != sizeof (lwpxstatus_t)
6638 #endif
6639 )
6640 return TRUE;
6641
6642 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6643
6644 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6645 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6646
6647 /* Make a ".reg/999" section. */
6648
6649 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6650 len = strlen (buf) + 1;
6651 name = bfd_alloc (abfd, (bfd_size_type) len);
6652 if (name == NULL)
6653 return FALSE;
6654 memcpy (name, buf, len);
6655
6656 sect = bfd_make_section (abfd, name);
6657 if (sect == NULL)
6658 return FALSE;
6659
6660 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6661 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6662 sect->filepos = note->descpos
6663 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6664 #endif
6665
6666 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6667 sect->_raw_size = sizeof (lwpstat.pr_reg);
6668 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6669 #endif
6670
6671 sect->flags = SEC_HAS_CONTENTS;
6672 sect->alignment_power = 2;
6673
6674 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6675 return FALSE;
6676
6677 /* Make a ".reg2/999" section */
6678
6679 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6680 len = strlen (buf) + 1;
6681 name = bfd_alloc (abfd, (bfd_size_type) len);
6682 if (name == NULL)
6683 return FALSE;
6684 memcpy (name, buf, len);
6685
6686 sect = bfd_make_section (abfd, name);
6687 if (sect == NULL)
6688 return FALSE;
6689
6690 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6691 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6692 sect->filepos = note->descpos
6693 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6694 #endif
6695
6696 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6697 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6698 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6699 #endif
6700
6701 sect->flags = SEC_HAS_CONTENTS;
6702 sect->alignment_power = 2;
6703
6704 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6705 }
6706 #endif /* defined (HAVE_LWPSTATUS_T) */
6707
6708 #if defined (HAVE_WIN32_PSTATUS_T)
6709 static bfd_boolean
6710 elfcore_grok_win32pstatus (abfd, note)
6711 bfd *abfd;
6712 Elf_Internal_Note *note;
6713 {
6714 char buf[30];
6715 char *name;
6716 size_t len;
6717 asection *sect;
6718 win32_pstatus_t pstatus;
6719
6720 if (note->descsz < sizeof (pstatus))
6721 return TRUE;
6722
6723 memcpy (&pstatus, note->descdata, sizeof (pstatus));
6724
6725 switch (pstatus.data_type)
6726 {
6727 case NOTE_INFO_PROCESS:
6728 /* FIXME: need to add ->core_command. */
6729 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6730 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6731 break;
6732
6733 case NOTE_INFO_THREAD:
6734 /* Make a ".reg/999" section. */
6735 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6736
6737 len = strlen (buf) + 1;
6738 name = bfd_alloc (abfd, (bfd_size_type) len);
6739 if (name == NULL)
6740 return FALSE;
6741
6742 memcpy (name, buf, len);
6743
6744 sect = bfd_make_section (abfd, name);
6745 if (sect == NULL)
6746 return FALSE;
6747
6748 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
6749 sect->filepos = (note->descpos
6750 + offsetof (struct win32_pstatus,
6751 data.thread_info.thread_context));
6752 sect->flags = SEC_HAS_CONTENTS;
6753 sect->alignment_power = 2;
6754
6755 if (pstatus.data.thread_info.is_active_thread)
6756 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6757 return FALSE;
6758 break;
6759
6760 case NOTE_INFO_MODULE:
6761 /* Make a ".module/xxxxxxxx" section. */
6762 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6763
6764 len = strlen (buf) + 1;
6765 name = bfd_alloc (abfd, (bfd_size_type) len);
6766 if (name == NULL)
6767 return FALSE;
6768
6769 memcpy (name, buf, len);
6770
6771 sect = bfd_make_section (abfd, name);
6772
6773 if (sect == NULL)
6774 return FALSE;
6775
6776 sect->_raw_size = note->descsz;
6777 sect->filepos = note->descpos;
6778 sect->flags = SEC_HAS_CONTENTS;
6779 sect->alignment_power = 2;
6780 break;
6781
6782 default:
6783 return TRUE;
6784 }
6785
6786 return TRUE;
6787 }
6788 #endif /* HAVE_WIN32_PSTATUS_T */
6789
6790 static bfd_boolean
6791 elfcore_grok_note (abfd, note)
6792 bfd *abfd;
6793 Elf_Internal_Note *note;
6794 {
6795 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6796
6797 switch (note->type)
6798 {
6799 default:
6800 return TRUE;
6801
6802 case NT_PRSTATUS:
6803 if (bed->elf_backend_grok_prstatus)
6804 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6805 return TRUE;
6806 #if defined (HAVE_PRSTATUS_T)
6807 return elfcore_grok_prstatus (abfd, note);
6808 #else
6809 return TRUE;
6810 #endif
6811
6812 #if defined (HAVE_PSTATUS_T)
6813 case NT_PSTATUS:
6814 return elfcore_grok_pstatus (abfd, note);
6815 #endif
6816
6817 #if defined (HAVE_LWPSTATUS_T)
6818 case NT_LWPSTATUS:
6819 return elfcore_grok_lwpstatus (abfd, note);
6820 #endif
6821
6822 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6823 return elfcore_grok_prfpreg (abfd, note);
6824
6825 #if defined (HAVE_WIN32_PSTATUS_T)
6826 case NT_WIN32PSTATUS:
6827 return elfcore_grok_win32pstatus (abfd, note);
6828 #endif
6829
6830 case NT_PRXFPREG: /* Linux SSE extension */
6831 if (note->namesz == 6
6832 && strcmp (note->namedata, "LINUX") == 0)
6833 return elfcore_grok_prxfpreg (abfd, note);
6834 else
6835 return TRUE;
6836
6837 case NT_PRPSINFO:
6838 case NT_PSINFO:
6839 if (bed->elf_backend_grok_psinfo)
6840 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6841 return TRUE;
6842 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6843 return elfcore_grok_psinfo (abfd, note);
6844 #else
6845 return TRUE;
6846 #endif
6847 }
6848 }
6849
6850 static bfd_boolean
6851 elfcore_netbsd_get_lwpid (note, lwpidp)
6852 Elf_Internal_Note *note;
6853 int *lwpidp;
6854 {
6855 char *cp;
6856
6857 cp = strchr (note->namedata, '@');
6858 if (cp != NULL)
6859 {
6860 *lwpidp = atoi(cp + 1);
6861 return TRUE;
6862 }
6863 return FALSE;
6864 }
6865
6866 static bfd_boolean
6867 elfcore_grok_netbsd_procinfo (abfd, note)
6868 bfd *abfd;
6869 Elf_Internal_Note *note;
6870 {
6871
6872 /* Signal number at offset 0x08. */
6873 elf_tdata (abfd)->core_signal
6874 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6875
6876 /* Process ID at offset 0x50. */
6877 elf_tdata (abfd)->core_pid
6878 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6879
6880 /* Command name at 0x7c (max 32 bytes, including nul). */
6881 elf_tdata (abfd)->core_command
6882 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6883
6884 return TRUE;
6885 }
6886
6887 static bfd_boolean
6888 elfcore_grok_netbsd_note (abfd, note)
6889 bfd *abfd;
6890 Elf_Internal_Note *note;
6891 {
6892 int lwp;
6893
6894 if (elfcore_netbsd_get_lwpid (note, &lwp))
6895 elf_tdata (abfd)->core_lwpid = lwp;
6896
6897 if (note->type == NT_NETBSDCORE_PROCINFO)
6898 {
6899 /* NetBSD-specific core "procinfo". Note that we expect to
6900 find this note before any of the others, which is fine,
6901 since the kernel writes this note out first when it
6902 creates a core file. */
6903
6904 return elfcore_grok_netbsd_procinfo (abfd, note);
6905 }
6906
6907 /* As of Jan 2002 there are no other machine-independent notes
6908 defined for NetBSD core files. If the note type is less
6909 than the start of the machine-dependent note types, we don't
6910 understand it. */
6911
6912 if (note->type < NT_NETBSDCORE_FIRSTMACH)
6913 return TRUE;
6914
6915
6916 switch (bfd_get_arch (abfd))
6917 {
6918 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6919 PT_GETFPREGS == mach+2. */
6920
6921 case bfd_arch_alpha:
6922 case bfd_arch_sparc:
6923 switch (note->type)
6924 {
6925 case NT_NETBSDCORE_FIRSTMACH+0:
6926 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6927
6928 case NT_NETBSDCORE_FIRSTMACH+2:
6929 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6930
6931 default:
6932 return TRUE;
6933 }
6934
6935 /* On all other arch's, PT_GETREGS == mach+1 and
6936 PT_GETFPREGS == mach+3. */
6937
6938 default:
6939 switch (note->type)
6940 {
6941 case NT_NETBSDCORE_FIRSTMACH+1:
6942 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6943
6944 case NT_NETBSDCORE_FIRSTMACH+3:
6945 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6946
6947 default:
6948 return TRUE;
6949 }
6950 }
6951 /* NOTREACHED */
6952 }
6953
6954 /* Function: elfcore_write_note
6955
6956 Inputs:
6957 buffer to hold note
6958 name of note
6959 type of note
6960 data for note
6961 size of data for note
6962
6963 Return:
6964 End of buffer containing note. */
6965
6966 char *
6967 elfcore_write_note (abfd, buf, bufsiz, name, type, input, size)
6968 bfd *abfd;
6969 char *buf;
6970 int *bufsiz;
6971 const char *name;
6972 int type;
6973 const PTR input;
6974 int size;
6975 {
6976 Elf_External_Note *xnp;
6977 size_t namesz;
6978 size_t pad;
6979 size_t newspace;
6980 char *p, *dest;
6981
6982 namesz = 0;
6983 pad = 0;
6984 if (name != NULL)
6985 {
6986 struct elf_backend_data *bed;
6987
6988 namesz = strlen (name) + 1;
6989 bed = get_elf_backend_data (abfd);
6990 pad = -namesz & (bed->s->file_align - 1);
6991 }
6992
6993 newspace = sizeof (Elf_External_Note) - 1 + namesz + pad + size;
6994
6995 p = realloc (buf, *bufsiz + newspace);
6996 dest = p + *bufsiz;
6997 *bufsiz += newspace;
6998 xnp = (Elf_External_Note *) dest;
6999 H_PUT_32 (abfd, namesz, xnp->namesz);
7000 H_PUT_32 (abfd, size, xnp->descsz);
7001 H_PUT_32 (abfd, type, xnp->type);
7002 dest = xnp->name;
7003 if (name != NULL)
7004 {
7005 memcpy (dest, name, namesz);
7006 dest += namesz;
7007 while (pad != 0)
7008 {
7009 *dest++ = '\0';
7010 --pad;
7011 }
7012 }
7013 memcpy (dest, input, size);
7014 return p;
7015 }
7016
7017 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7018 char *
7019 elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs)
7020 bfd *abfd;
7021 char *buf;
7022 int *bufsiz;
7023 const char *fname;
7024 const char *psargs;
7025 {
7026 int note_type;
7027 char *note_name = "CORE";
7028
7029 #if defined (HAVE_PSINFO_T)
7030 psinfo_t data;
7031 note_type = NT_PSINFO;
7032 #else
7033 prpsinfo_t data;
7034 note_type = NT_PRPSINFO;
7035 #endif
7036
7037 memset (&data, 0, sizeof (data));
7038 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7039 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
7040 return elfcore_write_note (abfd, buf, bufsiz,
7041 note_name, note_type, &data, sizeof (data));
7042 }
7043 #endif /* PSINFO_T or PRPSINFO_T */
7044
7045 #if defined (HAVE_PRSTATUS_T)
7046 char *
7047 elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7048 bfd *abfd;
7049 char *buf;
7050 int *bufsiz;
7051 long pid;
7052 int cursig;
7053 const PTR gregs;
7054 {
7055 prstatus_t prstat;
7056 char *note_name = "CORE";
7057
7058 memset (&prstat, 0, sizeof (prstat));
7059 prstat.pr_pid = pid;
7060 prstat.pr_cursig = cursig;
7061 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
7062 return elfcore_write_note (abfd, buf, bufsiz,
7063 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7064 }
7065 #endif /* HAVE_PRSTATUS_T */
7066
7067 #if defined (HAVE_LWPSTATUS_T)
7068 char *
7069 elfcore_write_lwpstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7070 bfd *abfd;
7071 char *buf;
7072 int *bufsiz;
7073 long pid;
7074 int cursig;
7075 const PTR gregs;
7076 {
7077 lwpstatus_t lwpstat;
7078 char *note_name = "CORE";
7079
7080 memset (&lwpstat, 0, sizeof (lwpstat));
7081 lwpstat.pr_lwpid = pid >> 16;
7082 lwpstat.pr_cursig = cursig;
7083 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7084 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7085 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7086 #if !defined(gregs)
7087 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7088 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7089 #else
7090 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7091 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7092 #endif
7093 #endif
7094 return elfcore_write_note (abfd, buf, bufsiz, note_name,
7095 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7096 }
7097 #endif /* HAVE_LWPSTATUS_T */
7098
7099 #if defined (HAVE_PSTATUS_T)
7100 char *
7101 elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7102 bfd *abfd;
7103 char *buf;
7104 int *bufsiz;
7105 long pid;
7106 int cursig;
7107 const PTR gregs;
7108 {
7109 pstatus_t pstat;
7110 char *note_name = "CORE";
7111
7112 memset (&pstat, 0, sizeof (pstat));
7113 pstat.pr_pid = pid & 0xffff;
7114 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
7115 NT_PSTATUS, &pstat, sizeof (pstat));
7116 return buf;
7117 }
7118 #endif /* HAVE_PSTATUS_T */
7119
7120 char *
7121 elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size)
7122 bfd *abfd;
7123 char *buf;
7124 int *bufsiz;
7125 const PTR fpregs;
7126 int size;
7127 {
7128 char *note_name = "CORE";
7129 return elfcore_write_note (abfd, buf, bufsiz,
7130 note_name, NT_FPREGSET, fpregs, size);
7131 }
7132
7133 char *
7134 elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size)
7135 bfd *abfd;
7136 char *buf;
7137 int *bufsiz;
7138 const PTR xfpregs;
7139 int size;
7140 {
7141 char *note_name = "LINUX";
7142 return elfcore_write_note (abfd, buf, bufsiz,
7143 note_name, NT_PRXFPREG, xfpregs, size);
7144 }
7145
7146 static bfd_boolean
7147 elfcore_read_notes (abfd, offset, size)
7148 bfd *abfd;
7149 file_ptr offset;
7150 bfd_size_type size;
7151 {
7152 char *buf;
7153 char *p;
7154
7155 if (size <= 0)
7156 return TRUE;
7157
7158 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
7159 return FALSE;
7160
7161 buf = bfd_malloc (size);
7162 if (buf == NULL)
7163 return FALSE;
7164
7165 if (bfd_bread (buf, size, abfd) != size)
7166 {
7167 error:
7168 free (buf);
7169 return FALSE;
7170 }
7171
7172 p = buf;
7173 while (p < buf + size)
7174 {
7175 /* FIXME: bad alignment assumption. */
7176 Elf_External_Note *xnp = (Elf_External_Note *) p;
7177 Elf_Internal_Note in;
7178
7179 in.type = H_GET_32 (abfd, xnp->type);
7180
7181 in.namesz = H_GET_32 (abfd, xnp->namesz);
7182 in.namedata = xnp->name;
7183
7184 in.descsz = H_GET_32 (abfd, xnp->descsz);
7185 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7186 in.descpos = offset + (in.descdata - buf);
7187
7188 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7189 {
7190 if (! elfcore_grok_netbsd_note (abfd, &in))
7191 goto error;
7192 }
7193 else
7194 {
7195 if (! elfcore_grok_note (abfd, &in))
7196 goto error;
7197 }
7198
7199 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7200 }
7201
7202 free (buf);
7203 return TRUE;
7204 }
7205 \f
7206 /* Providing external access to the ELF program header table. */
7207
7208 /* Return an upper bound on the number of bytes required to store a
7209 copy of ABFD's program header table entries. Return -1 if an error
7210 occurs; bfd_get_error will return an appropriate code. */
7211
7212 long
7213 bfd_get_elf_phdr_upper_bound (abfd)
7214 bfd *abfd;
7215 {
7216 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7217 {
7218 bfd_set_error (bfd_error_wrong_format);
7219 return -1;
7220 }
7221
7222 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
7223 }
7224
7225 /* Copy ABFD's program header table entries to *PHDRS. The entries
7226 will be stored as an array of Elf_Internal_Phdr structures, as
7227 defined in include/elf/internal.h. To find out how large the
7228 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7229
7230 Return the number of program header table entries read, or -1 if an
7231 error occurs; bfd_get_error will return an appropriate code. */
7232
7233 int
7234 bfd_get_elf_phdrs (abfd, phdrs)
7235 bfd *abfd;
7236 void *phdrs;
7237 {
7238 int num_phdrs;
7239
7240 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7241 {
7242 bfd_set_error (bfd_error_wrong_format);
7243 return -1;
7244 }
7245
7246 num_phdrs = elf_elfheader (abfd)->e_phnum;
7247 memcpy (phdrs, elf_tdata (abfd)->phdr,
7248 num_phdrs * sizeof (Elf_Internal_Phdr));
7249
7250 return num_phdrs;
7251 }
7252
7253 void
7254 _bfd_elf_sprintf_vma (abfd, buf, value)
7255 bfd *abfd ATTRIBUTE_UNUSED;
7256 char *buf;
7257 bfd_vma value;
7258 {
7259 #ifdef BFD64
7260 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7261
7262 i_ehdrp = elf_elfheader (abfd);
7263 if (i_ehdrp == NULL)
7264 sprintf_vma (buf, value);
7265 else
7266 {
7267 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7268 {
7269 #if BFD_HOST_64BIT_LONG
7270 sprintf (buf, "%016lx", value);
7271 #else
7272 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7273 _bfd_int64_low (value));
7274 #endif
7275 }
7276 else
7277 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7278 }
7279 #else
7280 sprintf_vma (buf, value);
7281 #endif
7282 }
7283
7284 void
7285 _bfd_elf_fprintf_vma (abfd, stream, value)
7286 bfd *abfd ATTRIBUTE_UNUSED;
7287 PTR stream;
7288 bfd_vma value;
7289 {
7290 #ifdef BFD64
7291 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7292
7293 i_ehdrp = elf_elfheader (abfd);
7294 if (i_ehdrp == NULL)
7295 fprintf_vma ((FILE *) stream, value);
7296 else
7297 {
7298 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7299 {
7300 #if BFD_HOST_64BIT_LONG
7301 fprintf ((FILE *) stream, "%016lx", value);
7302 #else
7303 fprintf ((FILE *) stream, "%08lx%08lx",
7304 _bfd_int64_high (value), _bfd_int64_low (value));
7305 #endif
7306 }
7307 else
7308 fprintf ((FILE *) stream, "%08lx",
7309 (unsigned long) (value & 0xffffffff));
7310 }
7311 #else
7312 fprintf_vma ((FILE *) stream, value);
7313 #endif
7314 }
7315
7316 enum elf_reloc_type_class
7317 _bfd_elf_reloc_type_class (rela)
7318 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
7319 {
7320 return reloc_class_normal;
7321 }
7322
7323 /* For RELA architectures, return the relocation value for a
7324 relocation against a local symbol. */
7325
7326 bfd_vma
7327 _bfd_elf_rela_local_sym (abfd, sym, sec, rel)
7328 bfd *abfd;
7329 Elf_Internal_Sym *sym;
7330 asection *sec;
7331 Elf_Internal_Rela *rel;
7332 {
7333 bfd_vma relocation;
7334
7335 relocation = (sec->output_section->vma
7336 + sec->output_offset
7337 + sym->st_value);
7338 if ((sec->flags & SEC_MERGE)
7339 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
7340 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
7341 {
7342 asection *msec;
7343
7344 msec = sec;
7345 rel->r_addend =
7346 _bfd_merged_section_offset (abfd, &msec,
7347 elf_section_data (sec)->sec_info,
7348 sym->st_value + rel->r_addend,
7349 (bfd_vma) 0)
7350 - relocation;
7351 rel->r_addend += msec->output_section->vma + msec->output_offset;
7352 }
7353 return relocation;
7354 }
7355
7356 bfd_vma
7357 _bfd_elf_rel_local_sym (abfd, sym, psec, addend)
7358 bfd *abfd;
7359 Elf_Internal_Sym *sym;
7360 asection **psec;
7361 bfd_vma addend;
7362 {
7363 asection *sec = *psec;
7364
7365 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
7366 return sym->st_value + addend;
7367
7368 return _bfd_merged_section_offset (abfd, psec,
7369 elf_section_data (sec)->sec_info,
7370 sym->st_value + addend, (bfd_vma) 0);
7371 }
7372
7373 bfd_vma
7374 _bfd_elf_section_offset (abfd, info, sec, offset)
7375 bfd *abfd;
7376 struct bfd_link_info *info;
7377 asection *sec;
7378 bfd_vma offset;
7379 {
7380 struct bfd_elf_section_data *sec_data;
7381
7382 sec_data = elf_section_data (sec);
7383 switch (sec->sec_info_type)
7384 {
7385 case ELF_INFO_TYPE_STABS:
7386 return _bfd_stab_section_offset (abfd,
7387 &elf_hash_table (info)->merge_info,
7388 sec, &sec_data->sec_info, offset);
7389 case ELF_INFO_TYPE_EH_FRAME:
7390 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
7391 default:
7392 return offset;
7393 }
7394 }
This page took 0.312509 seconds and 4 git commands to generate.