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