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