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