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