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