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