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