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