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