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