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