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