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