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