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