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