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