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