* elf.c (get_program_header_size): Adjacent loadable .note*
[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_STACK: pt = "STACK"; 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 (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 it's 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 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2246 len = strlen (namebuf) + 1;
2247 name = bfd_alloc (abfd, len);
2248 if (!name)
2249 return FALSE;
2250 memcpy (name, namebuf, len);
2251 newsect = bfd_make_section (abfd, name);
2252 if (newsect == NULL)
2253 return FALSE;
2254 newsect->vma = hdr->p_vaddr;
2255 newsect->lma = hdr->p_paddr;
2256 newsect->size = hdr->p_filesz;
2257 newsect->filepos = hdr->p_offset;
2258 newsect->flags |= SEC_HAS_CONTENTS;
2259 newsect->alignment_power = bfd_log2 (hdr->p_align);
2260 if (hdr->p_type == PT_LOAD)
2261 {
2262 newsect->flags |= SEC_ALLOC;
2263 newsect->flags |= SEC_LOAD;
2264 if (hdr->p_flags & PF_X)
2265 {
2266 /* FIXME: all we known is that it has execute PERMISSION,
2267 may be data. */
2268 newsect->flags |= SEC_CODE;
2269 }
2270 }
2271 if (!(hdr->p_flags & PF_W))
2272 {
2273 newsect->flags |= SEC_READONLY;
2274 }
2275
2276 if (split)
2277 {
2278 sprintf (namebuf, "%s%db", typename, index);
2279 len = strlen (namebuf) + 1;
2280 name = bfd_alloc (abfd, len);
2281 if (!name)
2282 return FALSE;
2283 memcpy (name, namebuf, len);
2284 newsect = bfd_make_section (abfd, name);
2285 if (newsect == NULL)
2286 return FALSE;
2287 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2288 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2289 newsect->size = hdr->p_memsz - hdr->p_filesz;
2290 if (hdr->p_type == PT_LOAD)
2291 {
2292 newsect->flags |= SEC_ALLOC;
2293 if (hdr->p_flags & PF_X)
2294 newsect->flags |= SEC_CODE;
2295 }
2296 if (!(hdr->p_flags & PF_W))
2297 newsect->flags |= SEC_READONLY;
2298 }
2299
2300 return TRUE;
2301 }
2302
2303 bfd_boolean
2304 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2305 {
2306 const struct elf_backend_data *bed;
2307
2308 switch (hdr->p_type)
2309 {
2310 case PT_NULL:
2311 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2312
2313 case PT_LOAD:
2314 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2315
2316 case PT_DYNAMIC:
2317 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2318
2319 case PT_INTERP:
2320 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2321
2322 case PT_NOTE:
2323 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2324 return FALSE;
2325 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2326 return FALSE;
2327 return TRUE;
2328
2329 case PT_SHLIB:
2330 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2331
2332 case PT_PHDR:
2333 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2334
2335 case PT_GNU_EH_FRAME:
2336 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2337 "eh_frame_hdr");
2338
2339 case PT_GNU_STACK:
2340 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2341
2342 case PT_GNU_RELRO:
2343 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2344
2345 default:
2346 /* Check for any processor-specific program segment types. */
2347 bed = get_elf_backend_data (abfd);
2348 return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
2349 }
2350 }
2351
2352 /* Initialize REL_HDR, the section-header for new section, containing
2353 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2354 relocations; otherwise, we use REL relocations. */
2355
2356 bfd_boolean
2357 _bfd_elf_init_reloc_shdr (bfd *abfd,
2358 Elf_Internal_Shdr *rel_hdr,
2359 asection *asect,
2360 bfd_boolean use_rela_p)
2361 {
2362 char *name;
2363 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2364 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2365
2366 name = bfd_alloc (abfd, amt);
2367 if (name == NULL)
2368 return FALSE;
2369 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2370 rel_hdr->sh_name =
2371 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2372 FALSE);
2373 if (rel_hdr->sh_name == (unsigned int) -1)
2374 return FALSE;
2375 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2376 rel_hdr->sh_entsize = (use_rela_p
2377 ? bed->s->sizeof_rela
2378 : bed->s->sizeof_rel);
2379 rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2380 rel_hdr->sh_flags = 0;
2381 rel_hdr->sh_addr = 0;
2382 rel_hdr->sh_size = 0;
2383 rel_hdr->sh_offset = 0;
2384
2385 return TRUE;
2386 }
2387
2388 /* Set up an ELF internal section header for a section. */
2389
2390 static void
2391 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2392 {
2393 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2394 bfd_boolean *failedptr = failedptrarg;
2395 Elf_Internal_Shdr *this_hdr;
2396 unsigned int sh_type;
2397
2398 if (*failedptr)
2399 {
2400 /* We already failed; just get out of the bfd_map_over_sections
2401 loop. */
2402 return;
2403 }
2404
2405 this_hdr = &elf_section_data (asect)->this_hdr;
2406
2407 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2408 asect->name, FALSE);
2409 if (this_hdr->sh_name == (unsigned int) -1)
2410 {
2411 *failedptr = TRUE;
2412 return;
2413 }
2414
2415 /* Don't clear sh_flags. Assembler may set additional bits. */
2416
2417 if ((asect->flags & SEC_ALLOC) != 0
2418 || asect->user_set_vma)
2419 this_hdr->sh_addr = asect->vma;
2420 else
2421 this_hdr->sh_addr = 0;
2422
2423 this_hdr->sh_offset = 0;
2424 this_hdr->sh_size = asect->size;
2425 this_hdr->sh_link = 0;
2426 this_hdr->sh_addralign = 1 << asect->alignment_power;
2427 /* The sh_entsize and sh_info fields may have been set already by
2428 copy_private_section_data. */
2429
2430 this_hdr->bfd_section = asect;
2431 this_hdr->contents = NULL;
2432
2433 /* If the section type is unspecified, we set it based on
2434 asect->flags. */
2435 if (this_hdr->sh_type == SHT_NULL)
2436 {
2437 if ((asect->flags & SEC_GROUP) != 0)
2438 this_hdr->sh_type = SHT_GROUP;
2439 else if ((asect->flags & SEC_ALLOC) != 0
2440 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2441 || (asect->flags & SEC_NEVER_LOAD) != 0))
2442 this_hdr->sh_type = SHT_NOBITS;
2443 else
2444 this_hdr->sh_type = SHT_PROGBITS;
2445 }
2446
2447 switch (this_hdr->sh_type)
2448 {
2449 default:
2450 break;
2451
2452 case SHT_STRTAB:
2453 case SHT_INIT_ARRAY:
2454 case SHT_FINI_ARRAY:
2455 case SHT_PREINIT_ARRAY:
2456 case SHT_NOTE:
2457 case SHT_NOBITS:
2458 case SHT_PROGBITS:
2459 break;
2460
2461 case SHT_HASH:
2462 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2463 break;
2464
2465 case SHT_DYNSYM:
2466 this_hdr->sh_entsize = bed->s->sizeof_sym;
2467 break;
2468
2469 case SHT_DYNAMIC:
2470 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2471 break;
2472
2473 case SHT_RELA:
2474 if (get_elf_backend_data (abfd)->may_use_rela_p)
2475 this_hdr->sh_entsize = bed->s->sizeof_rela;
2476 break;
2477
2478 case SHT_REL:
2479 if (get_elf_backend_data (abfd)->may_use_rel_p)
2480 this_hdr->sh_entsize = bed->s->sizeof_rel;
2481 break;
2482
2483 case SHT_GNU_versym:
2484 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2485 break;
2486
2487 case SHT_GNU_verdef:
2488 this_hdr->sh_entsize = 0;
2489 /* objcopy or strip will copy over sh_info, but may not set
2490 cverdefs. The linker will set cverdefs, but sh_info will be
2491 zero. */
2492 if (this_hdr->sh_info == 0)
2493 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2494 else
2495 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2496 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2497 break;
2498
2499 case SHT_GNU_verneed:
2500 this_hdr->sh_entsize = 0;
2501 /* objcopy or strip will copy over sh_info, but may not set
2502 cverrefs. The linker will set cverrefs, but sh_info will be
2503 zero. */
2504 if (this_hdr->sh_info == 0)
2505 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2506 else
2507 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2508 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2509 break;
2510
2511 case SHT_GROUP:
2512 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2513 break;
2514
2515 case SHT_GNU_HASH:
2516 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2517 break;
2518 }
2519
2520 if ((asect->flags & SEC_ALLOC) != 0)
2521 this_hdr->sh_flags |= SHF_ALLOC;
2522 if ((asect->flags & SEC_READONLY) == 0)
2523 this_hdr->sh_flags |= SHF_WRITE;
2524 if ((asect->flags & SEC_CODE) != 0)
2525 this_hdr->sh_flags |= SHF_EXECINSTR;
2526 if ((asect->flags & SEC_MERGE) != 0)
2527 {
2528 this_hdr->sh_flags |= SHF_MERGE;
2529 this_hdr->sh_entsize = asect->entsize;
2530 if ((asect->flags & SEC_STRINGS) != 0)
2531 this_hdr->sh_flags |= SHF_STRINGS;
2532 }
2533 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2534 this_hdr->sh_flags |= SHF_GROUP;
2535 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2536 {
2537 this_hdr->sh_flags |= SHF_TLS;
2538 if (asect->size == 0
2539 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2540 {
2541 struct bfd_link_order *o = asect->map_tail.link_order;
2542
2543 this_hdr->sh_size = 0;
2544 if (o != NULL)
2545 {
2546 this_hdr->sh_size = o->offset + o->size;
2547 if (this_hdr->sh_size != 0)
2548 this_hdr->sh_type = SHT_NOBITS;
2549 }
2550 }
2551 }
2552
2553 /* Check for processor-specific section types. */
2554 sh_type = this_hdr->sh_type;
2555 if (bed->elf_backend_fake_sections
2556 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2557 *failedptr = TRUE;
2558
2559 if (sh_type == SHT_NOBITS && asect->size != 0)
2560 {
2561 /* Don't change the header type from NOBITS if we are being
2562 called for objcopy --only-keep-debug. */
2563 this_hdr->sh_type = sh_type;
2564 }
2565
2566 /* If the section has relocs, set up a section header for the
2567 SHT_REL[A] section. If two relocation sections are required for
2568 this section, it is up to the processor-specific back-end to
2569 create the other. */
2570 if ((asect->flags & SEC_RELOC) != 0
2571 && !_bfd_elf_init_reloc_shdr (abfd,
2572 &elf_section_data (asect)->rel_hdr,
2573 asect,
2574 asect->use_rela_p))
2575 *failedptr = TRUE;
2576 }
2577
2578 /* Fill in the contents of a SHT_GROUP section. */
2579
2580 void
2581 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2582 {
2583 bfd_boolean *failedptr = failedptrarg;
2584 unsigned long symindx;
2585 asection *elt, *first;
2586 unsigned char *loc;
2587 bfd_boolean gas;
2588
2589 /* Ignore linker created group section. See elfNN_ia64_object_p in
2590 elfxx-ia64.c. */
2591 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2592 || *failedptr)
2593 return;
2594
2595 symindx = 0;
2596 if (elf_group_id (sec) != NULL)
2597 symindx = elf_group_id (sec)->udata.i;
2598
2599 if (symindx == 0)
2600 {
2601 /* If called from the assembler, swap_out_syms will have set up
2602 elf_section_syms; If called for "ld -r", use target_index. */
2603 if (elf_section_syms (abfd) != NULL)
2604 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2605 else
2606 symindx = sec->target_index;
2607 }
2608 elf_section_data (sec)->this_hdr.sh_info = symindx;
2609
2610 /* The contents won't be allocated for "ld -r" or objcopy. */
2611 gas = TRUE;
2612 if (sec->contents == NULL)
2613 {
2614 gas = FALSE;
2615 sec->contents = bfd_alloc (abfd, sec->size);
2616
2617 /* Arrange for the section to be written out. */
2618 elf_section_data (sec)->this_hdr.contents = sec->contents;
2619 if (sec->contents == NULL)
2620 {
2621 *failedptr = TRUE;
2622 return;
2623 }
2624 }
2625
2626 loc = sec->contents + sec->size;
2627
2628 /* Get the pointer to the first section in the group that gas
2629 squirreled away here. objcopy arranges for this to be set to the
2630 start of the input section group. */
2631 first = elt = elf_next_in_group (sec);
2632
2633 /* First element is a flag word. Rest of section is elf section
2634 indices for all the sections of the group. Write them backwards
2635 just to keep the group in the same order as given in .section
2636 directives, not that it matters. */
2637 while (elt != NULL)
2638 {
2639 asection *s;
2640 unsigned int idx;
2641
2642 loc -= 4;
2643 s = elt;
2644 if (!gas)
2645 s = s->output_section;
2646 idx = 0;
2647 if (s != NULL)
2648 idx = elf_section_data (s)->this_idx;
2649 H_PUT_32 (abfd, idx, loc);
2650 elt = elf_next_in_group (elt);
2651 if (elt == first)
2652 break;
2653 }
2654
2655 if ((loc -= 4) != sec->contents)
2656 abort ();
2657
2658 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2659 }
2660
2661 /* Assign all ELF section numbers. The dummy first section is handled here
2662 too. The link/info pointers for the standard section types are filled
2663 in here too, while we're at it. */
2664
2665 static bfd_boolean
2666 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2667 {
2668 struct elf_obj_tdata *t = elf_tdata (abfd);
2669 asection *sec;
2670 unsigned int section_number, secn;
2671 Elf_Internal_Shdr **i_shdrp;
2672 struct bfd_elf_section_data *d;
2673
2674 section_number = 1;
2675
2676 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2677
2678 /* SHT_GROUP sections are in relocatable files only. */
2679 if (link_info == NULL || link_info->relocatable)
2680 {
2681 /* Put SHT_GROUP sections first. */
2682 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2683 {
2684 d = elf_section_data (sec);
2685
2686 if (d->this_hdr.sh_type == SHT_GROUP)
2687 {
2688 if (sec->flags & SEC_LINKER_CREATED)
2689 {
2690 /* Remove the linker created SHT_GROUP sections. */
2691 bfd_section_list_remove (abfd, sec);
2692 abfd->section_count--;
2693 }
2694 else
2695 {
2696 if (section_number == SHN_LORESERVE)
2697 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2698 d->this_idx = section_number++;
2699 }
2700 }
2701 }
2702 }
2703
2704 for (sec = abfd->sections; sec; sec = sec->next)
2705 {
2706 d = elf_section_data (sec);
2707
2708 if (d->this_hdr.sh_type != SHT_GROUP)
2709 {
2710 if (section_number == SHN_LORESERVE)
2711 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2712 d->this_idx = section_number++;
2713 }
2714 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2715 if ((sec->flags & SEC_RELOC) == 0)
2716 d->rel_idx = 0;
2717 else
2718 {
2719 if (section_number == SHN_LORESERVE)
2720 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2721 d->rel_idx = section_number++;
2722 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2723 }
2724
2725 if (d->rel_hdr2)
2726 {
2727 if (section_number == SHN_LORESERVE)
2728 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2729 d->rel_idx2 = section_number++;
2730 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2731 }
2732 else
2733 d->rel_idx2 = 0;
2734 }
2735
2736 if (section_number == SHN_LORESERVE)
2737 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2738 t->shstrtab_section = section_number++;
2739 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2740 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2741
2742 if (bfd_get_symcount (abfd) > 0)
2743 {
2744 if (section_number == SHN_LORESERVE)
2745 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2746 t->symtab_section = section_number++;
2747 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2748 if (section_number > SHN_LORESERVE - 2)
2749 {
2750 if (section_number == SHN_LORESERVE)
2751 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2752 t->symtab_shndx_section = section_number++;
2753 t->symtab_shndx_hdr.sh_name
2754 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2755 ".symtab_shndx", FALSE);
2756 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2757 return FALSE;
2758 }
2759 if (section_number == SHN_LORESERVE)
2760 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2761 t->strtab_section = section_number++;
2762 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2763 }
2764
2765 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2766 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2767
2768 elf_numsections (abfd) = section_number;
2769 elf_elfheader (abfd)->e_shnum = section_number;
2770 if (section_number > SHN_LORESERVE)
2771 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2772
2773 /* Set up the list of section header pointers, in agreement with the
2774 indices. */
2775 i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *));
2776 if (i_shdrp == NULL)
2777 return FALSE;
2778
2779 i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr));
2780 if (i_shdrp[0] == NULL)
2781 {
2782 bfd_release (abfd, i_shdrp);
2783 return FALSE;
2784 }
2785
2786 elf_elfsections (abfd) = i_shdrp;
2787
2788 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2789 if (bfd_get_symcount (abfd) > 0)
2790 {
2791 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2792 if (elf_numsections (abfd) > SHN_LORESERVE)
2793 {
2794 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2795 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2796 }
2797 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2798 t->symtab_hdr.sh_link = t->strtab_section;
2799 }
2800
2801 for (sec = abfd->sections; sec; sec = sec->next)
2802 {
2803 struct bfd_elf_section_data *d = elf_section_data (sec);
2804 asection *s;
2805 const char *name;
2806
2807 i_shdrp[d->this_idx] = &d->this_hdr;
2808 if (d->rel_idx != 0)
2809 i_shdrp[d->rel_idx] = &d->rel_hdr;
2810 if (d->rel_idx2 != 0)
2811 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2812
2813 /* Fill in the sh_link and sh_info fields while we're at it. */
2814
2815 /* sh_link of a reloc section is the section index of the symbol
2816 table. sh_info is the section index of the section to which
2817 the relocation entries apply. */
2818 if (d->rel_idx != 0)
2819 {
2820 d->rel_hdr.sh_link = t->symtab_section;
2821 d->rel_hdr.sh_info = d->this_idx;
2822 }
2823 if (d->rel_idx2 != 0)
2824 {
2825 d->rel_hdr2->sh_link = t->symtab_section;
2826 d->rel_hdr2->sh_info = d->this_idx;
2827 }
2828
2829 /* We need to set up sh_link for SHF_LINK_ORDER. */
2830 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2831 {
2832 s = elf_linked_to_section (sec);
2833 if (s)
2834 {
2835 /* elf_linked_to_section points to the input section. */
2836 if (link_info != NULL)
2837 {
2838 /* Check discarded linkonce section. */
2839 if (elf_discarded_section (s))
2840 {
2841 asection *kept;
2842 (*_bfd_error_handler)
2843 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2844 abfd, d->this_hdr.bfd_section,
2845 s, s->owner);
2846 /* Point to the kept section if it has the same
2847 size as the discarded one. */
2848 kept = _bfd_elf_check_kept_section (s, link_info);
2849 if (kept == NULL)
2850 {
2851 bfd_set_error (bfd_error_bad_value);
2852 return FALSE;
2853 }
2854 s = kept;
2855 }
2856
2857 s = s->output_section;
2858 BFD_ASSERT (s != NULL);
2859 }
2860 else
2861 {
2862 /* Handle objcopy. */
2863 if (s->output_section == NULL)
2864 {
2865 (*_bfd_error_handler)
2866 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2867 abfd, d->this_hdr.bfd_section, s, s->owner);
2868 bfd_set_error (bfd_error_bad_value);
2869 return FALSE;
2870 }
2871 s = s->output_section;
2872 }
2873 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2874 }
2875 else
2876 {
2877 /* PR 290:
2878 The Intel C compiler generates SHT_IA_64_UNWIND with
2879 SHF_LINK_ORDER. But it doesn't set the sh_link or
2880 sh_info fields. Hence we could get the situation
2881 where s is NULL. */
2882 const struct elf_backend_data *bed
2883 = get_elf_backend_data (abfd);
2884 if (bed->link_order_error_handler)
2885 bed->link_order_error_handler
2886 (_("%B: warning: sh_link not set for section `%A'"),
2887 abfd, sec);
2888 }
2889 }
2890
2891 switch (d->this_hdr.sh_type)
2892 {
2893 case SHT_REL:
2894 case SHT_RELA:
2895 /* A reloc section which we are treating as a normal BFD
2896 section. sh_link is the section index of the symbol
2897 table. sh_info is the section index of the section to
2898 which the relocation entries apply. We assume that an
2899 allocated reloc section uses the dynamic symbol table.
2900 FIXME: How can we be sure? */
2901 s = bfd_get_section_by_name (abfd, ".dynsym");
2902 if (s != NULL)
2903 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2904
2905 /* We look up the section the relocs apply to by name. */
2906 name = sec->name;
2907 if (d->this_hdr.sh_type == SHT_REL)
2908 name += 4;
2909 else
2910 name += 5;
2911 s = bfd_get_section_by_name (abfd, name);
2912 if (s != NULL)
2913 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2914 break;
2915
2916 case SHT_STRTAB:
2917 /* We assume that a section named .stab*str is a stabs
2918 string section. We look for a section with the same name
2919 but without the trailing ``str'', and set its sh_link
2920 field to point to this section. */
2921 if (CONST_STRNEQ (sec->name, ".stab")
2922 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2923 {
2924 size_t len;
2925 char *alc;
2926
2927 len = strlen (sec->name);
2928 alc = bfd_malloc (len - 2);
2929 if (alc == NULL)
2930 return FALSE;
2931 memcpy (alc, sec->name, len - 3);
2932 alc[len - 3] = '\0';
2933 s = bfd_get_section_by_name (abfd, alc);
2934 free (alc);
2935 if (s != NULL)
2936 {
2937 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2938
2939 /* This is a .stab section. */
2940 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2941 elf_section_data (s)->this_hdr.sh_entsize
2942 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
2943 }
2944 }
2945 break;
2946
2947 case SHT_DYNAMIC:
2948 case SHT_DYNSYM:
2949 case SHT_GNU_verneed:
2950 case SHT_GNU_verdef:
2951 /* sh_link is the section header index of the string table
2952 used for the dynamic entries, or the symbol table, or the
2953 version strings. */
2954 s = bfd_get_section_by_name (abfd, ".dynstr");
2955 if (s != NULL)
2956 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2957 break;
2958
2959 case SHT_GNU_LIBLIST:
2960 /* sh_link is the section header index of the prelink library
2961 list used for the dynamic entries, or the symbol table, or
2962 the version strings. */
2963 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
2964 ? ".dynstr" : ".gnu.libstr");
2965 if (s != NULL)
2966 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2967 break;
2968
2969 case SHT_HASH:
2970 case SHT_GNU_HASH:
2971 case SHT_GNU_versym:
2972 /* sh_link is the section header index of the symbol table
2973 this hash table or version table is for. */
2974 s = bfd_get_section_by_name (abfd, ".dynsym");
2975 if (s != NULL)
2976 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2977 break;
2978
2979 case SHT_GROUP:
2980 d->this_hdr.sh_link = t->symtab_section;
2981 }
2982 }
2983
2984 for (secn = 1; secn < section_number; ++secn)
2985 if (i_shdrp[secn] == NULL)
2986 i_shdrp[secn] = i_shdrp[0];
2987 else
2988 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2989 i_shdrp[secn]->sh_name);
2990 return TRUE;
2991 }
2992
2993 /* Map symbol from it's internal number to the external number, moving
2994 all local symbols to be at the head of the list. */
2995
2996 static bfd_boolean
2997 sym_is_global (bfd *abfd, asymbol *sym)
2998 {
2999 /* If the backend has a special mapping, use it. */
3000 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3001 if (bed->elf_backend_sym_is_global)
3002 return (*bed->elf_backend_sym_is_global) (abfd, sym);
3003
3004 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
3005 || bfd_is_und_section (bfd_get_section (sym))
3006 || bfd_is_com_section (bfd_get_section (sym)));
3007 }
3008
3009 /* Don't output section symbols for sections that are not going to be
3010 output. Also, don't output section symbols for reloc and other
3011 special sections. */
3012
3013 static bfd_boolean
3014 ignore_section_sym (bfd *abfd, asymbol *sym)
3015 {
3016 return ((sym->flags & BSF_SECTION_SYM) != 0
3017 && (sym->value != 0
3018 || (sym->section->owner != abfd
3019 && (sym->section->output_section->owner != abfd
3020 || sym->section->output_offset != 0))));
3021 }
3022
3023 static bfd_boolean
3024 elf_map_symbols (bfd *abfd)
3025 {
3026 unsigned int symcount = bfd_get_symcount (abfd);
3027 asymbol **syms = bfd_get_outsymbols (abfd);
3028 asymbol **sect_syms;
3029 unsigned int num_locals = 0;
3030 unsigned int num_globals = 0;
3031 unsigned int num_locals2 = 0;
3032 unsigned int num_globals2 = 0;
3033 int max_index = 0;
3034 unsigned int idx;
3035 asection *asect;
3036 asymbol **new_syms;
3037
3038 #ifdef DEBUG
3039 fprintf (stderr, "elf_map_symbols\n");
3040 fflush (stderr);
3041 #endif
3042
3043 for (asect = abfd->sections; asect; asect = asect->next)
3044 {
3045 if (max_index < asect->index)
3046 max_index = asect->index;
3047 }
3048
3049 max_index++;
3050 sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3051 if (sect_syms == NULL)
3052 return FALSE;
3053 elf_section_syms (abfd) = sect_syms;
3054 elf_num_section_syms (abfd) = max_index;
3055
3056 /* Init sect_syms entries for any section symbols we have already
3057 decided to output. */
3058 for (idx = 0; idx < symcount; idx++)
3059 {
3060 asymbol *sym = syms[idx];
3061
3062 if ((sym->flags & BSF_SECTION_SYM) != 0
3063 && !ignore_section_sym (abfd, sym))
3064 {
3065 asection *sec = sym->section;
3066
3067 if (sec->owner != abfd)
3068 sec = sec->output_section;
3069
3070 sect_syms[sec->index] = syms[idx];
3071 }
3072 }
3073
3074 /* Classify all of the symbols. */
3075 for (idx = 0; idx < symcount; idx++)
3076 {
3077 if (ignore_section_sym (abfd, syms[idx]))
3078 continue;
3079 if (!sym_is_global (abfd, syms[idx]))
3080 num_locals++;
3081 else
3082 num_globals++;
3083 }
3084
3085 /* We will be adding a section symbol for each normal BFD section. Most
3086 sections will already have a section symbol in outsymbols, but
3087 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3088 at least in that case. */
3089 for (asect = abfd->sections; asect; asect = asect->next)
3090 {
3091 if (sect_syms[asect->index] == NULL)
3092 {
3093 if (!sym_is_global (abfd, asect->symbol))
3094 num_locals++;
3095 else
3096 num_globals++;
3097 }
3098 }
3099
3100 /* Now sort the symbols so the local symbols are first. */
3101 new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *));
3102
3103 if (new_syms == NULL)
3104 return FALSE;
3105
3106 for (idx = 0; idx < symcount; idx++)
3107 {
3108 asymbol *sym = syms[idx];
3109 unsigned int i;
3110
3111 if (ignore_section_sym (abfd, sym))
3112 continue;
3113 if (!sym_is_global (abfd, sym))
3114 i = num_locals2++;
3115 else
3116 i = num_locals + num_globals2++;
3117 new_syms[i] = sym;
3118 sym->udata.i = i + 1;
3119 }
3120 for (asect = abfd->sections; asect; asect = asect->next)
3121 {
3122 if (sect_syms[asect->index] == NULL)
3123 {
3124 asymbol *sym = asect->symbol;
3125 unsigned int i;
3126
3127 sect_syms[asect->index] = sym;
3128 if (!sym_is_global (abfd, sym))
3129 i = num_locals2++;
3130 else
3131 i = num_locals + num_globals2++;
3132 new_syms[i] = sym;
3133 sym->udata.i = i + 1;
3134 }
3135 }
3136
3137 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3138
3139 elf_num_locals (abfd) = num_locals;
3140 elf_num_globals (abfd) = num_globals;
3141 return TRUE;
3142 }
3143
3144 /* Align to the maximum file alignment that could be required for any
3145 ELF data structure. */
3146
3147 static inline file_ptr
3148 align_file_position (file_ptr off, int align)
3149 {
3150 return (off + align - 1) & ~(align - 1);
3151 }
3152
3153 /* Assign a file position to a section, optionally aligning to the
3154 required section alignment. */
3155
3156 file_ptr
3157 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3158 file_ptr offset,
3159 bfd_boolean align)
3160 {
3161 if (align)
3162 {
3163 unsigned int al;
3164
3165 al = i_shdrp->sh_addralign;
3166 if (al > 1)
3167 offset = BFD_ALIGN (offset, al);
3168 }
3169 i_shdrp->sh_offset = offset;
3170 if (i_shdrp->bfd_section != NULL)
3171 i_shdrp->bfd_section->filepos = offset;
3172 if (i_shdrp->sh_type != SHT_NOBITS)
3173 offset += i_shdrp->sh_size;
3174 return offset;
3175 }
3176
3177 /* Compute the file positions we are going to put the sections at, and
3178 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3179 is not NULL, this is being called by the ELF backend linker. */
3180
3181 bfd_boolean
3182 _bfd_elf_compute_section_file_positions (bfd *abfd,
3183 struct bfd_link_info *link_info)
3184 {
3185 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3186 bfd_boolean failed;
3187 struct bfd_strtab_hash *strtab = NULL;
3188 Elf_Internal_Shdr *shstrtab_hdr;
3189
3190 if (abfd->output_has_begun)
3191 return TRUE;
3192
3193 /* Do any elf backend specific processing first. */
3194 if (bed->elf_backend_begin_write_processing)
3195 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3196
3197 if (! prep_headers (abfd))
3198 return FALSE;
3199
3200 /* Post process the headers if necessary. */
3201 if (bed->elf_backend_post_process_headers)
3202 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3203
3204 failed = FALSE;
3205 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3206 if (failed)
3207 return FALSE;
3208
3209 if (!assign_section_numbers (abfd, link_info))
3210 return FALSE;
3211
3212 /* The backend linker builds symbol table information itself. */
3213 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3214 {
3215 /* Non-zero if doing a relocatable link. */
3216 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3217
3218 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3219 return FALSE;
3220 }
3221
3222 if (link_info == NULL)
3223 {
3224 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3225 if (failed)
3226 return FALSE;
3227 }
3228
3229 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3230 /* sh_name was set in prep_headers. */
3231 shstrtab_hdr->sh_type = SHT_STRTAB;
3232 shstrtab_hdr->sh_flags = 0;
3233 shstrtab_hdr->sh_addr = 0;
3234 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3235 shstrtab_hdr->sh_entsize = 0;
3236 shstrtab_hdr->sh_link = 0;
3237 shstrtab_hdr->sh_info = 0;
3238 /* sh_offset is set in assign_file_positions_except_relocs. */
3239 shstrtab_hdr->sh_addralign = 1;
3240
3241 if (!assign_file_positions_except_relocs (abfd, link_info))
3242 return FALSE;
3243
3244 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3245 {
3246 file_ptr off;
3247 Elf_Internal_Shdr *hdr;
3248
3249 off = elf_tdata (abfd)->next_file_pos;
3250
3251 hdr = &elf_tdata (abfd)->symtab_hdr;
3252 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3253
3254 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3255 if (hdr->sh_size != 0)
3256 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3257
3258 hdr = &elf_tdata (abfd)->strtab_hdr;
3259 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3260
3261 elf_tdata (abfd)->next_file_pos = off;
3262
3263 /* Now that we know where the .strtab section goes, write it
3264 out. */
3265 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3266 || ! _bfd_stringtab_emit (abfd, strtab))
3267 return FALSE;
3268 _bfd_stringtab_free (strtab);
3269 }
3270
3271 abfd->output_has_begun = TRUE;
3272
3273 return TRUE;
3274 }
3275
3276 /* Make an initial estimate of the size of the program header. If we
3277 get the number wrong here, we'll redo section placement. */
3278
3279 static bfd_size_type
3280 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3281 {
3282 size_t segs;
3283 asection *s;
3284 const struct elf_backend_data *bed;
3285
3286 /* Assume we will need exactly two PT_LOAD segments: one for text
3287 and one for data. */
3288 segs = 2;
3289
3290 s = bfd_get_section_by_name (abfd, ".interp");
3291 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3292 {
3293 /* If we have a loadable interpreter section, we need a
3294 PT_INTERP segment. In this case, assume we also need a
3295 PT_PHDR segment, although that may not be true for all
3296 targets. */
3297 segs += 2;
3298 }
3299
3300 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3301 {
3302 /* We need a PT_DYNAMIC segment. */
3303 ++segs;
3304
3305 if (elf_tdata (abfd)->relro)
3306 {
3307 /* We need a PT_GNU_RELRO segment only when there is a
3308 PT_DYNAMIC segment. */
3309 ++segs;
3310 }
3311 }
3312
3313 if (elf_tdata (abfd)->eh_frame_hdr)
3314 {
3315 /* We need a PT_GNU_EH_FRAME segment. */
3316 ++segs;
3317 }
3318
3319 if (elf_tdata (abfd)->stack_flags)
3320 {
3321 /* We need a PT_GNU_STACK segment. */
3322 ++segs;
3323 }
3324
3325 for (s = abfd->sections; s != NULL; s = s->next)
3326 {
3327 if ((s->flags & SEC_LOAD) != 0
3328 && CONST_STRNEQ (s->name, ".note"))
3329 {
3330 /* We need a PT_NOTE segment. */
3331 ++segs;
3332 /* Try to create just one PT_NOTE segment
3333 for all adjacent loadable .note* sections.
3334 gABI requires that within a PT_NOTE segment
3335 (and also inside of each SHT_NOTE section)
3336 each note is padded to a multiple of 4 size,
3337 so we check whether the sections are correctly
3338 aligned. */
3339 if (s->alignment_power == 2)
3340 while (s->next != NULL
3341 && s->next->alignment_power == 2
3342 && (s->next->flags & SEC_LOAD) != 0
3343 && CONST_STRNEQ (s->next->name, ".note"))
3344 s = s->next;
3345 }
3346 }
3347
3348 for (s = abfd->sections; s != NULL; s = s->next)
3349 {
3350 if (s->flags & SEC_THREAD_LOCAL)
3351 {
3352 /* We need a PT_TLS segment. */
3353 ++segs;
3354 break;
3355 }
3356 }
3357
3358 /* Let the backend count up any program headers it might need. */
3359 bed = get_elf_backend_data (abfd);
3360 if (bed->elf_backend_additional_program_headers)
3361 {
3362 int a;
3363
3364 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3365 if (a == -1)
3366 abort ();
3367 segs += a;
3368 }
3369
3370 return segs * bed->s->sizeof_phdr;
3371 }
3372
3373 /* Create a mapping from a set of sections to a program segment. */
3374
3375 static struct elf_segment_map *
3376 make_mapping (bfd *abfd,
3377 asection **sections,
3378 unsigned int from,
3379 unsigned int to,
3380 bfd_boolean phdr)
3381 {
3382 struct elf_segment_map *m;
3383 unsigned int i;
3384 asection **hdrpp;
3385 bfd_size_type amt;
3386
3387 amt = sizeof (struct elf_segment_map);
3388 amt += (to - from - 1) * sizeof (asection *);
3389 m = bfd_zalloc (abfd, amt);
3390 if (m == NULL)
3391 return NULL;
3392 m->next = NULL;
3393 m->p_type = PT_LOAD;
3394 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3395 m->sections[i - from] = *hdrpp;
3396 m->count = to - from;
3397
3398 if (from == 0 && phdr)
3399 {
3400 /* Include the headers in the first PT_LOAD segment. */
3401 m->includes_filehdr = 1;
3402 m->includes_phdrs = 1;
3403 }
3404
3405 return m;
3406 }
3407
3408 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3409 on failure. */
3410
3411 struct elf_segment_map *
3412 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3413 {
3414 struct elf_segment_map *m;
3415
3416 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map));
3417 if (m == NULL)
3418 return NULL;
3419 m->next = NULL;
3420 m->p_type = PT_DYNAMIC;
3421 m->count = 1;
3422 m->sections[0] = dynsec;
3423
3424 return m;
3425 }
3426
3427 /* Possibly add or remove segments from the segment map. */
3428
3429 static bfd_boolean
3430 elf_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
3431 {
3432 struct elf_segment_map **m;
3433 const struct elf_backend_data *bed;
3434
3435 /* The placement algorithm assumes that non allocated sections are
3436 not in PT_LOAD segments. We ensure this here by removing such
3437 sections from the segment map. We also remove excluded
3438 sections. Finally, any PT_LOAD segment without sections is
3439 removed. */
3440 m = &elf_tdata (abfd)->segment_map;
3441 while (*m)
3442 {
3443 unsigned int i, new_count;
3444
3445 for (new_count = 0, i = 0; i < (*m)->count; i++)
3446 {
3447 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3448 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3449 || (*m)->p_type != PT_LOAD))
3450 {
3451 (*m)->sections[new_count] = (*m)->sections[i];
3452 new_count++;
3453 }
3454 }
3455 (*m)->count = new_count;
3456
3457 if ((*m)->p_type == PT_LOAD && (*m)->count == 0)
3458 *m = (*m)->next;
3459 else
3460 m = &(*m)->next;
3461 }
3462
3463 bed = get_elf_backend_data (abfd);
3464 if (bed->elf_backend_modify_segment_map != NULL)
3465 {
3466 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3467 return FALSE;
3468 }
3469
3470 return TRUE;
3471 }
3472
3473 /* Set up a mapping from BFD sections to program segments. */
3474
3475 bfd_boolean
3476 _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3477 {
3478 unsigned int count;
3479 struct elf_segment_map *m;
3480 asection **sections = NULL;
3481 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3482
3483 if (elf_tdata (abfd)->segment_map == NULL
3484 && bfd_count_sections (abfd) != 0)
3485 {
3486 asection *s;
3487 unsigned int i;
3488 struct elf_segment_map *mfirst;
3489 struct elf_segment_map **pm;
3490 asection *last_hdr;
3491 bfd_vma last_size;
3492 unsigned int phdr_index;
3493 bfd_vma maxpagesize;
3494 asection **hdrpp;
3495 bfd_boolean phdr_in_segment = TRUE;
3496 bfd_boolean writable;
3497 int tls_count = 0;
3498 asection *first_tls = NULL;
3499 asection *dynsec, *eh_frame_hdr;
3500 bfd_size_type amt;
3501
3502 /* Select the allocated sections, and sort them. */
3503
3504 sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *));
3505 if (sections == NULL)
3506 goto error_return;
3507
3508 i = 0;
3509 for (s = abfd->sections; s != NULL; s = s->next)
3510 {
3511 if ((s->flags & SEC_ALLOC) != 0)
3512 {
3513 sections[i] = s;
3514 ++i;
3515 }
3516 }
3517 BFD_ASSERT (i <= bfd_count_sections (abfd));
3518 count = i;
3519
3520 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3521
3522 /* Build the mapping. */
3523
3524 mfirst = NULL;
3525 pm = &mfirst;
3526
3527 /* If we have a .interp section, then create a PT_PHDR segment for
3528 the program headers and a PT_INTERP segment for the .interp
3529 section. */
3530 s = bfd_get_section_by_name (abfd, ".interp");
3531 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3532 {
3533 amt = sizeof (struct elf_segment_map);
3534 m = bfd_zalloc (abfd, amt);
3535 if (m == NULL)
3536 goto error_return;
3537 m->next = NULL;
3538 m->p_type = PT_PHDR;
3539 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3540 m->p_flags = PF_R | PF_X;
3541 m->p_flags_valid = 1;
3542 m->includes_phdrs = 1;
3543
3544 *pm = m;
3545 pm = &m->next;
3546
3547 amt = sizeof (struct elf_segment_map);
3548 m = bfd_zalloc (abfd, amt);
3549 if (m == NULL)
3550 goto error_return;
3551 m->next = NULL;
3552 m->p_type = PT_INTERP;
3553 m->count = 1;
3554 m->sections[0] = s;
3555
3556 *pm = m;
3557 pm = &m->next;
3558 }
3559
3560 /* Look through the sections. We put sections in the same program
3561 segment when the start of the second section can be placed within
3562 a few bytes of the end of the first section. */
3563 last_hdr = NULL;
3564 last_size = 0;
3565 phdr_index = 0;
3566 maxpagesize = bed->maxpagesize;
3567 writable = FALSE;
3568 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3569 if (dynsec != NULL
3570 && (dynsec->flags & SEC_LOAD) == 0)
3571 dynsec = NULL;
3572
3573 /* Deal with -Ttext or something similar such that the first section
3574 is not adjacent to the program headers. This is an
3575 approximation, since at this point we don't know exactly how many
3576 program headers we will need. */
3577 if (count > 0)
3578 {
3579 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3580
3581 if (phdr_size == (bfd_size_type) -1)
3582 phdr_size = get_program_header_size (abfd, info);
3583 if ((abfd->flags & D_PAGED) == 0
3584 || sections[0]->lma < phdr_size
3585 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3586 phdr_in_segment = FALSE;
3587 }
3588
3589 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3590 {
3591 asection *hdr;
3592 bfd_boolean new_segment;
3593
3594 hdr = *hdrpp;
3595
3596 /* See if this section and the last one will fit in the same
3597 segment. */
3598
3599 if (last_hdr == NULL)
3600 {
3601 /* If we don't have a segment yet, then we don't need a new
3602 one (we build the last one after this loop). */
3603 new_segment = FALSE;
3604 }
3605 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3606 {
3607 /* If this section has a different relation between the
3608 virtual address and the load address, then we need a new
3609 segment. */
3610 new_segment = TRUE;
3611 }
3612 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3613 < BFD_ALIGN (hdr->lma, maxpagesize))
3614 {
3615 /* If putting this section in this segment would force us to
3616 skip a page in the segment, then we need a new segment. */
3617 new_segment = TRUE;
3618 }
3619 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3620 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3621 {
3622 /* We don't want to put a loadable section after a
3623 nonloadable section in the same segment.
3624 Consider .tbss sections as loadable for this purpose. */
3625 new_segment = TRUE;
3626 }
3627 else if ((abfd->flags & D_PAGED) == 0)
3628 {
3629 /* If the file is not demand paged, which means that we
3630 don't require the sections to be correctly aligned in the
3631 file, then there is no other reason for a new segment. */
3632 new_segment = FALSE;
3633 }
3634 else if (! writable
3635 && (hdr->flags & SEC_READONLY) == 0
3636 && (((last_hdr->lma + last_size - 1)
3637 & ~(maxpagesize - 1))
3638 != (hdr->lma & ~(maxpagesize - 1))))
3639 {
3640 /* We don't want to put a writable section in a read only
3641 segment, unless they are on the same page in memory
3642 anyhow. We already know that the last section does not
3643 bring us past the current section on the page, so the
3644 only case in which the new section is not on the same
3645 page as the previous section is when the previous section
3646 ends precisely on a page boundary. */
3647 new_segment = TRUE;
3648 }
3649 else
3650 {
3651 /* Otherwise, we can use the same segment. */
3652 new_segment = FALSE;
3653 }
3654
3655 /* Allow interested parties a chance to override our decision. */
3656 if (last_hdr && info->callbacks->override_segment_assignment)
3657 new_segment = info->callbacks->override_segment_assignment (info, abfd, hdr, last_hdr, new_segment);
3658
3659 if (! new_segment)
3660 {
3661 if ((hdr->flags & SEC_READONLY) == 0)
3662 writable = TRUE;
3663 last_hdr = hdr;
3664 /* .tbss sections effectively have zero size. */
3665 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3666 != SEC_THREAD_LOCAL)
3667 last_size = hdr->size;
3668 else
3669 last_size = 0;
3670 continue;
3671 }
3672
3673 /* We need a new program segment. We must create a new program
3674 header holding all the sections from phdr_index until hdr. */
3675
3676 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3677 if (m == NULL)
3678 goto error_return;
3679
3680 *pm = m;
3681 pm = &m->next;
3682
3683 if ((hdr->flags & SEC_READONLY) == 0)
3684 writable = TRUE;
3685 else
3686 writable = FALSE;
3687
3688 last_hdr = hdr;
3689 /* .tbss sections effectively have zero size. */
3690 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3691 last_size = hdr->size;
3692 else
3693 last_size = 0;
3694 phdr_index = i;
3695 phdr_in_segment = FALSE;
3696 }
3697
3698 /* Create a final PT_LOAD program segment. */
3699 if (last_hdr != NULL)
3700 {
3701 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3702 if (m == NULL)
3703 goto error_return;
3704
3705 *pm = m;
3706 pm = &m->next;
3707 }
3708
3709 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3710 if (dynsec != NULL)
3711 {
3712 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3713 if (m == NULL)
3714 goto error_return;
3715 *pm = m;
3716 pm = &m->next;
3717 }
3718
3719 /* For each batch of consecutive loadable .note sections,
3720 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3721 because if we link together nonloadable .note sections and
3722 loadable .note sections, we will generate two .note sections
3723 in the output file. FIXME: Using names for section types is
3724 bogus anyhow. */
3725 for (s = abfd->sections; s != NULL; s = s->next)
3726 {
3727 if ((s->flags & SEC_LOAD) != 0
3728 && CONST_STRNEQ (s->name, ".note"))
3729 {
3730 asection *s2;
3731 unsigned count = 1;
3732 amt = sizeof (struct elf_segment_map);
3733 if (s->alignment_power == 2)
3734 for (s2 = s; s2->next != NULL; s2 = s2->next)
3735 if (s2->next->alignment_power == 2
3736 && (s2->next->flags & SEC_LOAD) != 0
3737 && CONST_STRNEQ (s2->next->name, ".note")
3738 && align_power (s2->vma + s2->size, 2) == s2->next->vma)
3739 count++;
3740 else
3741 break;
3742 amt += (count - 1) * sizeof (asection *);
3743 m = bfd_zalloc (abfd, amt);
3744 if (m == NULL)
3745 goto error_return;
3746 m->next = NULL;
3747 m->p_type = PT_NOTE;
3748 m->count = count;
3749 while (count > 1)
3750 {
3751 m->sections[m->count - count--] = s;
3752 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3753 s = s->next;
3754 }
3755 m->sections[m->count - 1] = s;
3756 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3757 *pm = m;
3758 pm = &m->next;
3759 }
3760 if (s->flags & SEC_THREAD_LOCAL)
3761 {
3762 if (! tls_count)
3763 first_tls = s;
3764 tls_count++;
3765 }
3766 }
3767
3768 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3769 if (tls_count > 0)
3770 {
3771 int i;
3772
3773 amt = sizeof (struct elf_segment_map);
3774 amt += (tls_count - 1) * sizeof (asection *);
3775 m = bfd_zalloc (abfd, amt);
3776 if (m == NULL)
3777 goto error_return;
3778 m->next = NULL;
3779 m->p_type = PT_TLS;
3780 m->count = tls_count;
3781 /* Mandated PF_R. */
3782 m->p_flags = PF_R;
3783 m->p_flags_valid = 1;
3784 for (i = 0; i < tls_count; ++i)
3785 {
3786 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3787 m->sections[i] = first_tls;
3788 first_tls = first_tls->next;
3789 }
3790
3791 *pm = m;
3792 pm = &m->next;
3793 }
3794
3795 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3796 segment. */
3797 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3798 if (eh_frame_hdr != NULL
3799 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3800 {
3801 amt = sizeof (struct elf_segment_map);
3802 m = bfd_zalloc (abfd, amt);
3803 if (m == NULL)
3804 goto error_return;
3805 m->next = NULL;
3806 m->p_type = PT_GNU_EH_FRAME;
3807 m->count = 1;
3808 m->sections[0] = eh_frame_hdr->output_section;
3809
3810 *pm = m;
3811 pm = &m->next;
3812 }
3813
3814 if (elf_tdata (abfd)->stack_flags)
3815 {
3816 amt = sizeof (struct elf_segment_map);
3817 m = bfd_zalloc (abfd, amt);
3818 if (m == NULL)
3819 goto error_return;
3820 m->next = NULL;
3821 m->p_type = PT_GNU_STACK;
3822 m->p_flags = elf_tdata (abfd)->stack_flags;
3823 m->p_flags_valid = 1;
3824
3825 *pm = m;
3826 pm = &m->next;
3827 }
3828
3829 if (dynsec != NULL && elf_tdata (abfd)->relro)
3830 {
3831 /* We make a PT_GNU_RELRO segment only when there is a
3832 PT_DYNAMIC segment. */
3833 amt = sizeof (struct elf_segment_map);
3834 m = bfd_zalloc (abfd, amt);
3835 if (m == NULL)
3836 goto error_return;
3837 m->next = NULL;
3838 m->p_type = PT_GNU_RELRO;
3839 m->p_flags = PF_R;
3840 m->p_flags_valid = 1;
3841
3842 *pm = m;
3843 pm = &m->next;
3844 }
3845
3846 free (sections);
3847 elf_tdata (abfd)->segment_map = mfirst;
3848 }
3849
3850 if (!elf_modify_segment_map (abfd, info))
3851 return FALSE;
3852
3853 for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3854 ++count;
3855 elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
3856
3857 return TRUE;
3858
3859 error_return:
3860 if (sections != NULL)
3861 free (sections);
3862 return FALSE;
3863 }
3864
3865 /* Sort sections by address. */
3866
3867 static int
3868 elf_sort_sections (const void *arg1, const void *arg2)
3869 {
3870 const asection *sec1 = *(const asection **) arg1;
3871 const asection *sec2 = *(const asection **) arg2;
3872 bfd_size_type size1, size2;
3873
3874 /* Sort by LMA first, since this is the address used to
3875 place the section into a segment. */
3876 if (sec1->lma < sec2->lma)
3877 return -1;
3878 else if (sec1->lma > sec2->lma)
3879 return 1;
3880
3881 /* Then sort by VMA. Normally the LMA and the VMA will be
3882 the same, and this will do nothing. */
3883 if (sec1->vma < sec2->vma)
3884 return -1;
3885 else if (sec1->vma > sec2->vma)
3886 return 1;
3887
3888 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3889
3890 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3891
3892 if (TOEND (sec1))
3893 {
3894 if (TOEND (sec2))
3895 {
3896 /* If the indicies are the same, do not return 0
3897 here, but continue to try the next comparison. */
3898 if (sec1->target_index - sec2->target_index != 0)
3899 return sec1->target_index - sec2->target_index;
3900 }
3901 else
3902 return 1;
3903 }
3904 else if (TOEND (sec2))
3905 return -1;
3906
3907 #undef TOEND
3908
3909 /* Sort by size, to put zero sized sections
3910 before others at the same address. */
3911
3912 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
3913 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
3914
3915 if (size1 < size2)
3916 return -1;
3917 if (size1 > size2)
3918 return 1;
3919
3920 return sec1->target_index - sec2->target_index;
3921 }
3922
3923 /* Ian Lance Taylor writes:
3924
3925 We shouldn't be using % with a negative signed number. That's just
3926 not good. We have to make sure either that the number is not
3927 negative, or that the number has an unsigned type. When the types
3928 are all the same size they wind up as unsigned. When file_ptr is a
3929 larger signed type, the arithmetic winds up as signed long long,
3930 which is wrong.
3931
3932 What we're trying to say here is something like ``increase OFF by
3933 the least amount that will cause it to be equal to the VMA modulo
3934 the page size.'' */
3935 /* In other words, something like:
3936
3937 vma_offset = m->sections[0]->vma % bed->maxpagesize;
3938 off_offset = off % bed->maxpagesize;
3939 if (vma_offset < off_offset)
3940 adjustment = vma_offset + bed->maxpagesize - off_offset;
3941 else
3942 adjustment = vma_offset - off_offset;
3943
3944 which can can be collapsed into the expression below. */
3945
3946 static file_ptr
3947 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
3948 {
3949 return ((vma - off) % maxpagesize);
3950 }
3951
3952 /* Assign file positions to the sections based on the mapping from
3953 sections to segments. This function also sets up some fields in
3954 the file header. */
3955
3956 static bfd_boolean
3957 assign_file_positions_for_load_sections (bfd *abfd,
3958 struct bfd_link_info *link_info)
3959 {
3960 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3961 struct elf_segment_map *m;
3962 Elf_Internal_Phdr *phdrs;
3963 Elf_Internal_Phdr *p;
3964 file_ptr off;
3965 bfd_size_type maxpagesize;
3966 unsigned int alloc;
3967 unsigned int i, j;
3968
3969 if (link_info == NULL
3970 && !elf_modify_segment_map (abfd, link_info))
3971 return FALSE;
3972
3973 alloc = 0;
3974 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3975 ++alloc;
3976
3977 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3978 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3979 elf_elfheader (abfd)->e_phnum = alloc;
3980
3981 if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
3982 elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
3983 else
3984 BFD_ASSERT (elf_tdata (abfd)->program_header_size
3985 >= alloc * bed->s->sizeof_phdr);
3986
3987 if (alloc == 0)
3988 {
3989 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
3990 return TRUE;
3991 }
3992
3993 phdrs = bfd_alloc2 (abfd, alloc, sizeof (Elf_Internal_Phdr));
3994 elf_tdata (abfd)->phdr = phdrs;
3995 if (phdrs == NULL)
3996 return FALSE;
3997
3998 maxpagesize = 1;
3999 if ((abfd->flags & D_PAGED) != 0)
4000 maxpagesize = bed->maxpagesize;
4001
4002 off = bed->s->sizeof_ehdr;
4003 off += alloc * bed->s->sizeof_phdr;
4004
4005 for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4006 m != NULL;
4007 m = m->next, p++, j++)
4008 {
4009 asection **secpp;
4010 bfd_vma off_adjust;
4011 bfd_boolean no_contents;
4012
4013 /* If elf_segment_map is not from map_sections_to_segments, the
4014 sections may not be correctly ordered. NOTE: sorting should
4015 not be done to the PT_NOTE section of a corefile, which may
4016 contain several pseudo-sections artificially created by bfd.
4017 Sorting these pseudo-sections breaks things badly. */
4018 if (m->count > 1
4019 && !(elf_elfheader (abfd)->e_type == ET_CORE
4020 && m->p_type == PT_NOTE))
4021 qsort (m->sections, (size_t) m->count, sizeof (asection *),
4022 elf_sort_sections);
4023
4024 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4025 number of sections with contents contributing to both p_filesz
4026 and p_memsz, followed by a number of sections with no contents
4027 that just contribute to p_memsz. In this loop, OFF tracks next
4028 available file offset for PT_LOAD and PT_NOTE segments. */
4029 p->p_type = m->p_type;
4030 p->p_flags = m->p_flags;
4031
4032 if (m->count == 0)
4033 p->p_vaddr = 0;
4034 else
4035 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4036
4037 if (m->p_paddr_valid)
4038 p->p_paddr = m->p_paddr;
4039 else if (m->count == 0)
4040 p->p_paddr = 0;
4041 else
4042 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4043
4044 if (p->p_type == PT_LOAD
4045 && (abfd->flags & D_PAGED) != 0)
4046 {
4047 /* p_align in demand paged PT_LOAD segments effectively stores
4048 the maximum page size. When copying an executable with
4049 objcopy, we set m->p_align from the input file. Use this
4050 value for maxpagesize rather than bed->maxpagesize, which
4051 may be different. Note that we use maxpagesize for PT_TLS
4052 segment alignment later in this function, so we are relying
4053 on at least one PT_LOAD segment appearing before a PT_TLS
4054 segment. */
4055 if (m->p_align_valid)
4056 maxpagesize = m->p_align;
4057
4058 p->p_align = maxpagesize;
4059 }
4060 else if (m->count == 0)
4061 p->p_align = 1 << bed->s->log_file_align;
4062 else if (m->p_align_valid)
4063 p->p_align = m->p_align;
4064 else
4065 p->p_align = 0;
4066
4067 no_contents = FALSE;
4068 off_adjust = 0;
4069 if (p->p_type == PT_LOAD
4070 && m->count > 0)
4071 {
4072 bfd_size_type align;
4073 unsigned int align_power = 0;
4074
4075 if (m->p_align_valid)
4076 align = p->p_align;
4077 else
4078 {
4079 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4080 {
4081 unsigned int secalign;
4082
4083 secalign = bfd_get_section_alignment (abfd, *secpp);
4084 if (secalign > align_power)
4085 align_power = secalign;
4086 }
4087 align = (bfd_size_type) 1 << align_power;
4088 if (align < maxpagesize)
4089 align = maxpagesize;
4090 }
4091
4092 for (i = 0; i < m->count; i++)
4093 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4094 /* If we aren't making room for this section, then
4095 it must be SHT_NOBITS regardless of what we've
4096 set via struct bfd_elf_special_section. */
4097 elf_section_type (m->sections[i]) = SHT_NOBITS;
4098
4099 /* Find out whether this segment contains any loadable
4100 sections. If the first section isn't loadable, the same
4101 holds for any other sections. */
4102 i = 0;
4103 while (elf_section_type (m->sections[i]) == SHT_NOBITS)
4104 {
4105 /* If a segment starts with .tbss, we need to look
4106 at the next section to decide whether the segment
4107 has any loadable sections. */
4108 if ((elf_section_flags (m->sections[i]) & SHF_TLS) == 0
4109 || ++i >= m->count)
4110 {
4111 no_contents = TRUE;
4112 break;
4113 }
4114 }
4115
4116 off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4117 off += off_adjust;
4118 if (no_contents)
4119 {
4120 /* We shouldn't need to align the segment on disk since
4121 the segment doesn't need file space, but the gABI
4122 arguably requires the alignment and glibc ld.so
4123 checks it. So to comply with the alignment
4124 requirement but not waste file space, we adjust
4125 p_offset for just this segment. (OFF_ADJUST is
4126 subtracted from OFF later.) This may put p_offset
4127 past the end of file, but that shouldn't matter. */
4128 }
4129 else
4130 off_adjust = 0;
4131 }
4132 /* Make sure the .dynamic section is the first section in the
4133 PT_DYNAMIC segment. */
4134 else if (p->p_type == PT_DYNAMIC
4135 && m->count > 1
4136 && strcmp (m->sections[0]->name, ".dynamic") != 0)
4137 {
4138 _bfd_error_handler
4139 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4140 abfd);
4141 bfd_set_error (bfd_error_bad_value);
4142 return FALSE;
4143 }
4144
4145 p->p_offset = 0;
4146 p->p_filesz = 0;
4147 p->p_memsz = 0;
4148
4149 if (m->includes_filehdr)
4150 {
4151 if (!m->p_flags_valid)
4152 p->p_flags |= PF_R;
4153 p->p_filesz = bed->s->sizeof_ehdr;
4154 p->p_memsz = bed->s->sizeof_ehdr;
4155 if (m->count > 0)
4156 {
4157 BFD_ASSERT (p->p_type == PT_LOAD);
4158
4159 if (p->p_vaddr < (bfd_vma) off)
4160 {
4161 (*_bfd_error_handler)
4162 (_("%B: Not enough room for program headers, try linking with -N"),
4163 abfd);
4164 bfd_set_error (bfd_error_bad_value);
4165 return FALSE;
4166 }
4167
4168 p->p_vaddr -= off;
4169 if (!m->p_paddr_valid)
4170 p->p_paddr -= off;
4171 }
4172 }
4173
4174 if (m->includes_phdrs)
4175 {
4176 if (!m->p_flags_valid)
4177 p->p_flags |= PF_R;
4178
4179 if (!m->includes_filehdr)
4180 {
4181 p->p_offset = bed->s->sizeof_ehdr;
4182
4183 if (m->count > 0)
4184 {
4185 BFD_ASSERT (p->p_type == PT_LOAD);
4186 p->p_vaddr -= off - p->p_offset;
4187 if (!m->p_paddr_valid)
4188 p->p_paddr -= off - p->p_offset;
4189 }
4190 }
4191
4192 p->p_filesz += alloc * bed->s->sizeof_phdr;
4193 p->p_memsz += alloc * bed->s->sizeof_phdr;
4194 }
4195
4196 if (p->p_type == PT_LOAD
4197 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4198 {
4199 if (!m->includes_filehdr && !m->includes_phdrs)
4200 p->p_offset = off;
4201 else
4202 {
4203 file_ptr adjust;
4204
4205 adjust = off - (p->p_offset + p->p_filesz);
4206 if (!no_contents)
4207 p->p_filesz += adjust;
4208 p->p_memsz += adjust;
4209 }
4210 }
4211
4212 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4213 maps. Set filepos for sections in PT_LOAD segments, and in
4214 core files, for sections in PT_NOTE segments.
4215 assign_file_positions_for_non_load_sections will set filepos
4216 for other sections and update p_filesz for other segments. */
4217 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4218 {
4219 asection *sec;
4220 bfd_size_type align;
4221 Elf_Internal_Shdr *this_hdr;
4222
4223 sec = *secpp;
4224 this_hdr = &elf_section_data (sec)->this_hdr;
4225 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4226
4227 if (p->p_type == PT_LOAD
4228 || p->p_type == PT_TLS)
4229 {
4230 bfd_signed_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
4231
4232 if (this_hdr->sh_type != SHT_NOBITS
4233 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4234 && ((this_hdr->sh_flags & SHF_TLS) == 0
4235 || p->p_type == PT_TLS)))
4236 {
4237 if (adjust < 0)
4238 {
4239 (*_bfd_error_handler)
4240 (_("%B: section %A lma 0x%lx overlaps previous sections"),
4241 abfd, sec, (unsigned long) sec->lma);
4242 adjust = 0;
4243 }
4244 p->p_memsz += adjust;
4245
4246 if (this_hdr->sh_type != SHT_NOBITS)
4247 {
4248 off += adjust;
4249 p->p_filesz += adjust;
4250 }
4251 }
4252 }
4253
4254 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4255 {
4256 /* The section at i == 0 is the one that actually contains
4257 everything. */
4258 if (i == 0)
4259 {
4260 this_hdr->sh_offset = sec->filepos = off;
4261 off += this_hdr->sh_size;
4262 p->p_filesz = this_hdr->sh_size;
4263 p->p_memsz = 0;
4264 p->p_align = 1;
4265 }
4266 else
4267 {
4268 /* The rest are fake sections that shouldn't be written. */
4269 sec->filepos = 0;
4270 sec->size = 0;
4271 sec->flags = 0;
4272 continue;
4273 }
4274 }
4275 else
4276 {
4277 if (p->p_type == PT_LOAD)
4278 {
4279 this_hdr->sh_offset = sec->filepos = off;
4280 if (this_hdr->sh_type != SHT_NOBITS)
4281 off += this_hdr->sh_size;
4282 }
4283
4284 if (this_hdr->sh_type != SHT_NOBITS)
4285 {
4286 p->p_filesz += this_hdr->sh_size;
4287 /* A load section without SHF_ALLOC is something like
4288 a note section in a PT_NOTE segment. These take
4289 file space but are not loaded into memory. */
4290 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4291 p->p_memsz += this_hdr->sh_size;
4292 }
4293 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4294 {
4295 if (p->p_type == PT_TLS)
4296 p->p_memsz += this_hdr->sh_size;
4297
4298 /* .tbss is special. It doesn't contribute to p_memsz of
4299 normal segments. */
4300 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4301 p->p_memsz += this_hdr->sh_size;
4302 }
4303
4304 if (p->p_type == PT_GNU_RELRO)
4305 p->p_align = 1;
4306 else if (align > p->p_align
4307 && !m->p_align_valid
4308 && (p->p_type != PT_LOAD
4309 || (abfd->flags & D_PAGED) == 0))
4310 p->p_align = align;
4311 }
4312
4313 if (!m->p_flags_valid)
4314 {
4315 p->p_flags |= PF_R;
4316 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4317 p->p_flags |= PF_X;
4318 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4319 p->p_flags |= PF_W;
4320 }
4321 }
4322 off -= off_adjust;
4323
4324 /* Check that all sections are in a PT_LOAD segment.
4325 Don't check funky gdb generated core files. */
4326 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4327 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4328 {
4329 Elf_Internal_Shdr *this_hdr;
4330 asection *sec;
4331
4332 sec = *secpp;
4333 this_hdr = &(elf_section_data(sec)->this_hdr);
4334 if (this_hdr->sh_size != 0
4335 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4336 {
4337 (*_bfd_error_handler)
4338 (_("%B: section `%A' can't be allocated in segment %d"),
4339 abfd, sec, j);
4340 bfd_set_error (bfd_error_bad_value);
4341 return FALSE;
4342 }
4343 }
4344 }
4345
4346 elf_tdata (abfd)->next_file_pos = off;
4347 return TRUE;
4348 }
4349
4350 /* Assign file positions for the other sections. */
4351
4352 static bfd_boolean
4353 assign_file_positions_for_non_load_sections (bfd *abfd,
4354 struct bfd_link_info *link_info)
4355 {
4356 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4357 Elf_Internal_Shdr **i_shdrpp;
4358 Elf_Internal_Shdr **hdrpp;
4359 Elf_Internal_Phdr *phdrs;
4360 Elf_Internal_Phdr *p;
4361 struct elf_segment_map *m;
4362 bfd_vma filehdr_vaddr, filehdr_paddr;
4363 bfd_vma phdrs_vaddr, phdrs_paddr;
4364 file_ptr off;
4365 unsigned int num_sec;
4366 unsigned int i;
4367 unsigned int count;
4368
4369 i_shdrpp = elf_elfsections (abfd);
4370 num_sec = elf_numsections (abfd);
4371 off = elf_tdata (abfd)->next_file_pos;
4372 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4373 {
4374 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4375 Elf_Internal_Shdr *hdr;
4376
4377 hdr = *hdrpp;
4378 if (hdr->bfd_section != NULL
4379 && (hdr->bfd_section->filepos != 0
4380 || (hdr->sh_type == SHT_NOBITS
4381 && hdr->contents == NULL)))
4382 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4383 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4384 {
4385 if (hdr->sh_size != 0)
4386 ((*_bfd_error_handler)
4387 (_("%B: warning: allocated section `%s' not in segment"),
4388 abfd,
4389 (hdr->bfd_section == NULL
4390 ? "*unknown*"
4391 : hdr->bfd_section->name)));
4392 /* We don't need to page align empty sections. */
4393 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4394 off += vma_page_aligned_bias (hdr->sh_addr, off,
4395 bed->maxpagesize);
4396 else
4397 off += vma_page_aligned_bias (hdr->sh_addr, off,
4398 hdr->sh_addralign);
4399 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4400 FALSE);
4401 }
4402 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4403 && hdr->bfd_section == NULL)
4404 || hdr == i_shdrpp[tdata->symtab_section]
4405 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4406 || hdr == i_shdrpp[tdata->strtab_section])
4407 hdr->sh_offset = -1;
4408 else
4409 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4410
4411 if (i == SHN_LORESERVE - 1)
4412 {
4413 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4414 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4415 }
4416 }
4417
4418 /* Now that we have set the section file positions, we can set up
4419 the file positions for the non PT_LOAD segments. */
4420 count = 0;
4421 filehdr_vaddr = 0;
4422 filehdr_paddr = 0;
4423 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4424 phdrs_paddr = 0;
4425 phdrs = elf_tdata (abfd)->phdr;
4426 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4427 m != NULL;
4428 m = m->next, p++)
4429 {
4430 ++count;
4431 if (p->p_type != PT_LOAD)
4432 continue;
4433
4434 if (m->includes_filehdr)
4435 {
4436 filehdr_vaddr = p->p_vaddr;
4437 filehdr_paddr = p->p_paddr;
4438 }
4439 if (m->includes_phdrs)
4440 {
4441 phdrs_vaddr = p->p_vaddr;
4442 phdrs_paddr = p->p_paddr;
4443 if (m->includes_filehdr)
4444 {
4445 phdrs_vaddr += bed->s->sizeof_ehdr;
4446 phdrs_paddr += bed->s->sizeof_ehdr;
4447 }
4448 }
4449 }
4450
4451 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4452 m != NULL;
4453 m = m->next, p++)
4454 {
4455 if (m->count != 0)
4456 {
4457 if (p->p_type != PT_LOAD
4458 && (p->p_type != PT_NOTE || bfd_get_format (abfd) != bfd_core))
4459 {
4460 Elf_Internal_Shdr *hdr;
4461 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4462
4463 hdr = &elf_section_data (m->sections[m->count - 1])->this_hdr;
4464 p->p_filesz = (m->sections[m->count - 1]->filepos
4465 - m->sections[0]->filepos);
4466 if (hdr->sh_type != SHT_NOBITS)
4467 p->p_filesz += hdr->sh_size;
4468
4469 p->p_offset = m->sections[0]->filepos;
4470 }
4471 }
4472 else
4473 {
4474 if (m->includes_filehdr)
4475 {
4476 p->p_vaddr = filehdr_vaddr;
4477 if (! m->p_paddr_valid)
4478 p->p_paddr = filehdr_paddr;
4479 }
4480 else if (m->includes_phdrs)
4481 {
4482 p->p_vaddr = phdrs_vaddr;
4483 if (! m->p_paddr_valid)
4484 p->p_paddr = phdrs_paddr;
4485 }
4486 else if (p->p_type == PT_GNU_RELRO)
4487 {
4488 Elf_Internal_Phdr *lp;
4489
4490 for (lp = phdrs; lp < phdrs + count; ++lp)
4491 {
4492 if (lp->p_type == PT_LOAD
4493 && lp->p_vaddr <= link_info->relro_end
4494 && lp->p_vaddr >= link_info->relro_start
4495 && (lp->p_vaddr + lp->p_filesz
4496 >= link_info->relro_end))
4497 break;
4498 }
4499
4500 if (lp < phdrs + count
4501 && link_info->relro_end > lp->p_vaddr)
4502 {
4503 p->p_vaddr = lp->p_vaddr;
4504 p->p_paddr = lp->p_paddr;
4505 p->p_offset = lp->p_offset;
4506 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4507 p->p_memsz = p->p_filesz;
4508 p->p_align = 1;
4509 p->p_flags = (lp->p_flags & ~PF_W);
4510 }
4511 else
4512 {
4513 memset (p, 0, sizeof *p);
4514 p->p_type = PT_NULL;
4515 }
4516 }
4517 }
4518 }
4519
4520 elf_tdata (abfd)->next_file_pos = off;
4521
4522 return TRUE;
4523 }
4524
4525 /* Work out the file positions of all the sections. This is called by
4526 _bfd_elf_compute_section_file_positions. All the section sizes and
4527 VMAs must be known before this is called.
4528
4529 Reloc sections come in two flavours: Those processed specially as
4530 "side-channel" data attached to a section to which they apply, and
4531 those that bfd doesn't process as relocations. The latter sort are
4532 stored in a normal bfd section by bfd_section_from_shdr. We don't
4533 consider the former sort here, unless they form part of the loadable
4534 image. Reloc sections not assigned here will be handled later by
4535 assign_file_positions_for_relocs.
4536
4537 We also don't set the positions of the .symtab and .strtab here. */
4538
4539 static bfd_boolean
4540 assign_file_positions_except_relocs (bfd *abfd,
4541 struct bfd_link_info *link_info)
4542 {
4543 struct elf_obj_tdata *tdata = elf_tdata (abfd);
4544 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4545 file_ptr off;
4546 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4547
4548 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4549 && bfd_get_format (abfd) != bfd_core)
4550 {
4551 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4552 unsigned int num_sec = elf_numsections (abfd);
4553 Elf_Internal_Shdr **hdrpp;
4554 unsigned int i;
4555
4556 /* Start after the ELF header. */
4557 off = i_ehdrp->e_ehsize;
4558
4559 /* We are not creating an executable, which means that we are
4560 not creating a program header, and that the actual order of
4561 the sections in the file is unimportant. */
4562 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4563 {
4564 Elf_Internal_Shdr *hdr;
4565
4566 hdr = *hdrpp;
4567 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4568 && hdr->bfd_section == NULL)
4569 || i == tdata->symtab_section
4570 || i == tdata->symtab_shndx_section
4571 || i == tdata->strtab_section)
4572 {
4573 hdr->sh_offset = -1;
4574 }
4575 else
4576 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4577
4578 if (i == SHN_LORESERVE - 1)
4579 {
4580 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4581 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4582 }
4583 }
4584 }
4585 else
4586 {
4587 unsigned int alloc;
4588
4589 /* Assign file positions for the loaded sections based on the
4590 assignment of sections to segments. */
4591 if (!assign_file_positions_for_load_sections (abfd, link_info))
4592 return FALSE;
4593
4594 /* And for non-load sections. */
4595 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4596 return FALSE;
4597
4598 if (bed->elf_backend_modify_program_headers != NULL)
4599 {
4600 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4601 return FALSE;
4602 }
4603
4604 /* Write out the program headers. */
4605 alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4606 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4607 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4608 return FALSE;
4609
4610 off = tdata->next_file_pos;
4611 }
4612
4613 /* Place the section headers. */
4614 off = align_file_position (off, 1 << bed->s->log_file_align);
4615 i_ehdrp->e_shoff = off;
4616 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4617
4618 tdata->next_file_pos = off;
4619
4620 return TRUE;
4621 }
4622
4623 static bfd_boolean
4624 prep_headers (bfd *abfd)
4625 {
4626 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4627 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4628 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4629 struct elf_strtab_hash *shstrtab;
4630 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4631
4632 i_ehdrp = elf_elfheader (abfd);
4633 i_shdrp = elf_elfsections (abfd);
4634
4635 shstrtab = _bfd_elf_strtab_init ();
4636 if (shstrtab == NULL)
4637 return FALSE;
4638
4639 elf_shstrtab (abfd) = shstrtab;
4640
4641 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4642 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4643 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4644 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4645
4646 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4647 i_ehdrp->e_ident[EI_DATA] =
4648 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4649 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4650
4651 if ((abfd->flags & DYNAMIC) != 0)
4652 i_ehdrp->e_type = ET_DYN;
4653 else if ((abfd->flags & EXEC_P) != 0)
4654 i_ehdrp->e_type = ET_EXEC;
4655 else if (bfd_get_format (abfd) == bfd_core)
4656 i_ehdrp->e_type = ET_CORE;
4657 else
4658 i_ehdrp->e_type = ET_REL;
4659
4660 switch (bfd_get_arch (abfd))
4661 {
4662 case bfd_arch_unknown:
4663 i_ehdrp->e_machine = EM_NONE;
4664 break;
4665
4666 /* There used to be a long list of cases here, each one setting
4667 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4668 in the corresponding bfd definition. To avoid duplication,
4669 the switch was removed. Machines that need special handling
4670 can generally do it in elf_backend_final_write_processing(),
4671 unless they need the information earlier than the final write.
4672 Such need can generally be supplied by replacing the tests for
4673 e_machine with the conditions used to determine it. */
4674 default:
4675 i_ehdrp->e_machine = bed->elf_machine_code;
4676 }
4677
4678 i_ehdrp->e_version = bed->s->ev_current;
4679 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4680
4681 /* No program header, for now. */
4682 i_ehdrp->e_phoff = 0;
4683 i_ehdrp->e_phentsize = 0;
4684 i_ehdrp->e_phnum = 0;
4685
4686 /* Each bfd section is section header entry. */
4687 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4688 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4689
4690 /* If we're building an executable, we'll need a program header table. */
4691 if (abfd->flags & EXEC_P)
4692 /* It all happens later. */
4693 ;
4694 else
4695 {
4696 i_ehdrp->e_phentsize = 0;
4697 i_phdrp = 0;
4698 i_ehdrp->e_phoff = 0;
4699 }
4700
4701 elf_tdata (abfd)->symtab_hdr.sh_name =
4702 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4703 elf_tdata (abfd)->strtab_hdr.sh_name =
4704 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4705 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4706 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4707 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4708 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4709 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4710 return FALSE;
4711
4712 return TRUE;
4713 }
4714
4715 /* Assign file positions for all the reloc sections which are not part
4716 of the loadable file image. */
4717
4718 void
4719 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4720 {
4721 file_ptr off;
4722 unsigned int i, num_sec;
4723 Elf_Internal_Shdr **shdrpp;
4724
4725 off = elf_tdata (abfd)->next_file_pos;
4726
4727 num_sec = elf_numsections (abfd);
4728 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4729 {
4730 Elf_Internal_Shdr *shdrp;
4731
4732 shdrp = *shdrpp;
4733 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4734 && shdrp->sh_offset == -1)
4735 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4736 }
4737
4738 elf_tdata (abfd)->next_file_pos = off;
4739 }
4740
4741 bfd_boolean
4742 _bfd_elf_write_object_contents (bfd *abfd)
4743 {
4744 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4745 Elf_Internal_Ehdr *i_ehdrp;
4746 Elf_Internal_Shdr **i_shdrp;
4747 bfd_boolean failed;
4748 unsigned int count, num_sec;
4749
4750 if (! abfd->output_has_begun
4751 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4752 return FALSE;
4753
4754 i_shdrp = elf_elfsections (abfd);
4755 i_ehdrp = elf_elfheader (abfd);
4756
4757 failed = FALSE;
4758 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4759 if (failed)
4760 return FALSE;
4761
4762 _bfd_elf_assign_file_positions_for_relocs (abfd);
4763
4764 /* After writing the headers, we need to write the sections too... */
4765 num_sec = elf_numsections (abfd);
4766 for (count = 1; count < num_sec; count++)
4767 {
4768 if (bed->elf_backend_section_processing)
4769 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4770 if (i_shdrp[count]->contents)
4771 {
4772 bfd_size_type amt = i_shdrp[count]->sh_size;
4773
4774 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4775 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4776 return FALSE;
4777 }
4778 if (count == SHN_LORESERVE - 1)
4779 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4780 }
4781
4782 /* Write out the section header names. */
4783 if (elf_shstrtab (abfd) != NULL
4784 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4785 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
4786 return FALSE;
4787
4788 if (bed->elf_backend_final_write_processing)
4789 (*bed->elf_backend_final_write_processing) (abfd,
4790 elf_tdata (abfd)->linker);
4791
4792 if (!bed->s->write_shdrs_and_ehdr (abfd))
4793 return FALSE;
4794
4795 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
4796 if (elf_tdata (abfd)->after_write_object_contents)
4797 return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
4798
4799 return TRUE;
4800 }
4801
4802 bfd_boolean
4803 _bfd_elf_write_corefile_contents (bfd *abfd)
4804 {
4805 /* Hopefully this can be done just like an object file. */
4806 return _bfd_elf_write_object_contents (abfd);
4807 }
4808
4809 /* Given a section, search the header to find them. */
4810
4811 int
4812 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
4813 {
4814 const struct elf_backend_data *bed;
4815 int index;
4816
4817 if (elf_section_data (asect) != NULL
4818 && elf_section_data (asect)->this_idx != 0)
4819 return elf_section_data (asect)->this_idx;
4820
4821 if (bfd_is_abs_section (asect))
4822 index = SHN_ABS;
4823 else if (bfd_is_com_section (asect))
4824 index = SHN_COMMON;
4825 else if (bfd_is_und_section (asect))
4826 index = SHN_UNDEF;
4827 else
4828 index = -1;
4829
4830 bed = get_elf_backend_data (abfd);
4831 if (bed->elf_backend_section_from_bfd_section)
4832 {
4833 int retval = index;
4834
4835 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4836 return retval;
4837 }
4838
4839 if (index == -1)
4840 bfd_set_error (bfd_error_nonrepresentable_section);
4841
4842 return index;
4843 }
4844
4845 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4846 on error. */
4847
4848 int
4849 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
4850 {
4851 asymbol *asym_ptr = *asym_ptr_ptr;
4852 int idx;
4853 flagword flags = asym_ptr->flags;
4854
4855 /* When gas creates relocations against local labels, it creates its
4856 own symbol for the section, but does put the symbol into the
4857 symbol chain, so udata is 0. When the linker is generating
4858 relocatable output, this section symbol may be for one of the
4859 input sections rather than the output section. */
4860 if (asym_ptr->udata.i == 0
4861 && (flags & BSF_SECTION_SYM)
4862 && asym_ptr->section)
4863 {
4864 asection *sec;
4865 int indx;
4866
4867 sec = asym_ptr->section;
4868 if (sec->owner != abfd && sec->output_section != NULL)
4869 sec = sec->output_section;
4870 if (sec->owner == abfd
4871 && (indx = sec->index) < elf_num_section_syms (abfd)
4872 && elf_section_syms (abfd)[indx] != NULL)
4873 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4874 }
4875
4876 idx = asym_ptr->udata.i;
4877
4878 if (idx == 0)
4879 {
4880 /* This case can occur when using --strip-symbol on a symbol
4881 which is used in a relocation entry. */
4882 (*_bfd_error_handler)
4883 (_("%B: symbol `%s' required but not present"),
4884 abfd, bfd_asymbol_name (asym_ptr));
4885 bfd_set_error (bfd_error_no_symbols);
4886 return -1;
4887 }
4888
4889 #if DEBUG & 4
4890 {
4891 fprintf (stderr,
4892 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4893 (long) asym_ptr, asym_ptr->name, idx, flags,
4894 elf_symbol_flags (flags));
4895 fflush (stderr);
4896 }
4897 #endif
4898
4899 return idx;
4900 }
4901
4902 /* Rewrite program header information. */
4903
4904 static bfd_boolean
4905 rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
4906 {
4907 Elf_Internal_Ehdr *iehdr;
4908 struct elf_segment_map *map;
4909 struct elf_segment_map *map_first;
4910 struct elf_segment_map **pointer_to_map;
4911 Elf_Internal_Phdr *segment;
4912 asection *section;
4913 unsigned int i;
4914 unsigned int num_segments;
4915 bfd_boolean phdr_included = FALSE;
4916 bfd_vma maxpagesize;
4917 struct elf_segment_map *phdr_adjust_seg = NULL;
4918 unsigned int phdr_adjust_num = 0;
4919 const struct elf_backend_data *bed;
4920
4921 bed = get_elf_backend_data (ibfd);
4922 iehdr = elf_elfheader (ibfd);
4923
4924 map_first = NULL;
4925 pointer_to_map = &map_first;
4926
4927 num_segments = elf_elfheader (ibfd)->e_phnum;
4928 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4929
4930 /* Returns the end address of the segment + 1. */
4931 #define SEGMENT_END(segment, start) \
4932 (start + (segment->p_memsz > segment->p_filesz \
4933 ? segment->p_memsz : segment->p_filesz))
4934
4935 #define SECTION_SIZE(section, segment) \
4936 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4937 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4938 ? section->size : 0)
4939
4940 /* Returns TRUE if the given section is contained within
4941 the given segment. VMA addresses are compared. */
4942 #define IS_CONTAINED_BY_VMA(section, segment) \
4943 (section->vma >= segment->p_vaddr \
4944 && (section->vma + SECTION_SIZE (section, segment) \
4945 <= (SEGMENT_END (segment, segment->p_vaddr))))
4946
4947 /* Returns TRUE if the given section is contained within
4948 the given segment. LMA addresses are compared. */
4949 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4950 (section->lma >= base \
4951 && (section->lma + SECTION_SIZE (section, segment) \
4952 <= SEGMENT_END (segment, base)))
4953
4954 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4955 #define IS_COREFILE_NOTE(p, s) \
4956 (p->p_type == PT_NOTE \
4957 && bfd_get_format (ibfd) == bfd_core \
4958 && s->vma == 0 && s->lma == 0 \
4959 && (bfd_vma) s->filepos >= p->p_offset \
4960 && ((bfd_vma) s->filepos + s->size \
4961 <= p->p_offset + p->p_filesz))
4962
4963 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4964 linker, which generates a PT_INTERP section with p_vaddr and
4965 p_memsz set to 0. */
4966 #define IS_SOLARIS_PT_INTERP(p, s) \
4967 (p->p_vaddr == 0 \
4968 && p->p_paddr == 0 \
4969 && p->p_memsz == 0 \
4970 && p->p_filesz > 0 \
4971 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4972 && s->size > 0 \
4973 && (bfd_vma) s->filepos >= p->p_offset \
4974 && ((bfd_vma) s->filepos + s->size \
4975 <= p->p_offset + p->p_filesz))
4976
4977 /* Decide if the given section should be included in the given segment.
4978 A section will be included if:
4979 1. It is within the address space of the segment -- we use the LMA
4980 if that is set for the segment and the VMA otherwise,
4981 2. It is an allocated segment,
4982 3. There is an output section associated with it,
4983 4. The section has not already been allocated to a previous segment.
4984 5. PT_GNU_STACK segments do not include any sections.
4985 6. PT_TLS segment includes only SHF_TLS sections.
4986 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
4987 8. PT_DYNAMIC should not contain empty sections at the beginning
4988 (with the possible exception of .dynamic). */
4989 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
4990 ((((segment->p_paddr \
4991 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4992 : IS_CONTAINED_BY_VMA (section, segment)) \
4993 && (section->flags & SEC_ALLOC) != 0) \
4994 || IS_COREFILE_NOTE (segment, section)) \
4995 && segment->p_type != PT_GNU_STACK \
4996 && (segment->p_type != PT_TLS \
4997 || (section->flags & SEC_THREAD_LOCAL)) \
4998 && (segment->p_type == PT_LOAD \
4999 || segment->p_type == PT_TLS \
5000 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5001 && (segment->p_type != PT_DYNAMIC \
5002 || SECTION_SIZE (section, segment) > 0 \
5003 || (segment->p_paddr \
5004 ? segment->p_paddr != section->lma \
5005 : segment->p_vaddr != section->vma) \
5006 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5007 == 0)) \
5008 && ! section->segment_mark)
5009
5010 /* If the output section of a section in the input segment is NULL,
5011 it is removed from the corresponding output segment. */
5012 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5013 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5014 && section->output_section != NULL)
5015
5016 /* Returns TRUE iff seg1 starts after the end of seg2. */
5017 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5018 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5019
5020 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5021 their VMA address ranges and their LMA address ranges overlap.
5022 It is possible to have overlapping VMA ranges without overlapping LMA
5023 ranges. RedBoot images for example can have both .data and .bss mapped
5024 to the same VMA range, but with the .data section mapped to a different
5025 LMA. */
5026 #define SEGMENT_OVERLAPS(seg1, seg2) \
5027 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5028 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5029 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5030 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5031
5032 /* Initialise the segment mark field. */
5033 for (section = ibfd->sections; section != NULL; section = section->next)
5034 section->segment_mark = FALSE;
5035
5036 /* Scan through the segments specified in the program header
5037 of the input BFD. For this first scan we look for overlaps
5038 in the loadable segments. These can be created by weird
5039 parameters to objcopy. Also, fix some solaris weirdness. */
5040 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5041 i < num_segments;
5042 i++, segment++)
5043 {
5044 unsigned int j;
5045 Elf_Internal_Phdr *segment2;
5046
5047 if (segment->p_type == PT_INTERP)
5048 for (section = ibfd->sections; section; section = section->next)
5049 if (IS_SOLARIS_PT_INTERP (segment, section))
5050 {
5051 /* Mininal change so that the normal section to segment
5052 assignment code will work. */
5053 segment->p_vaddr = section->vma;
5054 break;
5055 }
5056
5057 if (segment->p_type != PT_LOAD)
5058 continue;
5059
5060 /* Determine if this segment overlaps any previous segments. */
5061 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
5062 {
5063 bfd_signed_vma extra_length;
5064
5065 if (segment2->p_type != PT_LOAD
5066 || ! SEGMENT_OVERLAPS (segment, segment2))
5067 continue;
5068
5069 /* Merge the two segments together. */
5070 if (segment2->p_vaddr < segment->p_vaddr)
5071 {
5072 /* Extend SEGMENT2 to include SEGMENT and then delete
5073 SEGMENT. */
5074 extra_length =
5075 SEGMENT_END (segment, segment->p_vaddr)
5076 - SEGMENT_END (segment2, segment2->p_vaddr);
5077
5078 if (extra_length > 0)
5079 {
5080 segment2->p_memsz += extra_length;
5081 segment2->p_filesz += extra_length;
5082 }
5083
5084 segment->p_type = PT_NULL;
5085
5086 /* Since we have deleted P we must restart the outer loop. */
5087 i = 0;
5088 segment = elf_tdata (ibfd)->phdr;
5089 break;
5090 }
5091 else
5092 {
5093 /* Extend SEGMENT to include SEGMENT2 and then delete
5094 SEGMENT2. */
5095 extra_length =
5096 SEGMENT_END (segment2, segment2->p_vaddr)
5097 - SEGMENT_END (segment, segment->p_vaddr);
5098
5099 if (extra_length > 0)
5100 {
5101 segment->p_memsz += extra_length;
5102 segment->p_filesz += extra_length;
5103 }
5104
5105 segment2->p_type = PT_NULL;
5106 }
5107 }
5108 }
5109
5110 /* The second scan attempts to assign sections to segments. */
5111 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5112 i < num_segments;
5113 i ++, segment ++)
5114 {
5115 unsigned int section_count;
5116 asection ** sections;
5117 asection * output_section;
5118 unsigned int isec;
5119 bfd_vma matching_lma;
5120 bfd_vma suggested_lma;
5121 unsigned int j;
5122 bfd_size_type amt;
5123 asection * first_section;
5124
5125 if (segment->p_type == PT_NULL)
5126 continue;
5127
5128 first_section = NULL;
5129 /* Compute how many sections might be placed into this segment. */
5130 for (section = ibfd->sections, section_count = 0;
5131 section != NULL;
5132 section = section->next)
5133 {
5134 /* Find the first section in the input segment, which may be
5135 removed from the corresponding output segment. */
5136 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5137 {
5138 if (first_section == NULL)
5139 first_section = section;
5140 if (section->output_section != NULL)
5141 ++section_count;
5142 }
5143 }
5144
5145 /* Allocate a segment map big enough to contain
5146 all of the sections we have selected. */
5147 amt = sizeof (struct elf_segment_map);
5148 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5149 map = bfd_zalloc (obfd, amt);
5150 if (map == NULL)
5151 return FALSE;
5152
5153 /* Initialise the fields of the segment map. Default to
5154 using the physical address of the segment in the input BFD. */
5155 map->next = NULL;
5156 map->p_type = segment->p_type;
5157 map->p_flags = segment->p_flags;
5158 map->p_flags_valid = 1;
5159
5160 /* If the first section in the input segment is removed, there is
5161 no need to preserve segment physical address in the corresponding
5162 output segment. */
5163 if (!first_section || first_section->output_section != NULL)
5164 {
5165 map->p_paddr = segment->p_paddr;
5166 map->p_paddr_valid = 1;
5167 }
5168
5169 /* Determine if this segment contains the ELF file header
5170 and if it contains the program headers themselves. */
5171 map->includes_filehdr = (segment->p_offset == 0
5172 && segment->p_filesz >= iehdr->e_ehsize);
5173
5174 map->includes_phdrs = 0;
5175
5176 if (! phdr_included || segment->p_type != PT_LOAD)
5177 {
5178 map->includes_phdrs =
5179 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5180 && (segment->p_offset + segment->p_filesz
5181 >= ((bfd_vma) iehdr->e_phoff
5182 + iehdr->e_phnum * iehdr->e_phentsize)));
5183
5184 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5185 phdr_included = TRUE;
5186 }
5187
5188 if (section_count == 0)
5189 {
5190 /* Special segments, such as the PT_PHDR segment, may contain
5191 no sections, but ordinary, loadable segments should contain
5192 something. They are allowed by the ELF spec however, so only
5193 a warning is produced. */
5194 if (segment->p_type == PT_LOAD)
5195 (*_bfd_error_handler)
5196 (_("%B: warning: Empty loadable segment detected, is this intentional ?\n"),
5197 ibfd);
5198
5199 map->count = 0;
5200 *pointer_to_map = map;
5201 pointer_to_map = &map->next;
5202
5203 continue;
5204 }
5205
5206 /* Now scan the sections in the input BFD again and attempt
5207 to add their corresponding output sections to the segment map.
5208 The problem here is how to handle an output section which has
5209 been moved (ie had its LMA changed). There are four possibilities:
5210
5211 1. None of the sections have been moved.
5212 In this case we can continue to use the segment LMA from the
5213 input BFD.
5214
5215 2. All of the sections have been moved by the same amount.
5216 In this case we can change the segment's LMA to match the LMA
5217 of the first section.
5218
5219 3. Some of the sections have been moved, others have not.
5220 In this case those sections which have not been moved can be
5221 placed in the current segment which will have to have its size,
5222 and possibly its LMA changed, and a new segment or segments will
5223 have to be created to contain the other sections.
5224
5225 4. The sections have been moved, but not by the same amount.
5226 In this case we can change the segment's LMA to match the LMA
5227 of the first section and we will have to create a new segment
5228 or segments to contain the other sections.
5229
5230 In order to save time, we allocate an array to hold the section
5231 pointers that we are interested in. As these sections get assigned
5232 to a segment, they are removed from this array. */
5233
5234 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5235 to work around this long long bug. */
5236 sections = bfd_malloc2 (section_count, sizeof (asection *));
5237 if (sections == NULL)
5238 return FALSE;
5239
5240 /* Step One: Scan for segment vs section LMA conflicts.
5241 Also add the sections to the section array allocated above.
5242 Also add the sections to the current segment. In the common
5243 case, where the sections have not been moved, this means that
5244 we have completely filled the segment, and there is nothing
5245 more to do. */
5246 isec = 0;
5247 matching_lma = 0;
5248 suggested_lma = 0;
5249
5250 for (j = 0, section = ibfd->sections;
5251 section != NULL;
5252 section = section->next)
5253 {
5254 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5255 {
5256 output_section = section->output_section;
5257
5258 sections[j ++] = section;
5259
5260 /* The Solaris native linker always sets p_paddr to 0.
5261 We try to catch that case here, and set it to the
5262 correct value. Note - some backends require that
5263 p_paddr be left as zero. */
5264 if (segment->p_paddr == 0
5265 && segment->p_vaddr != 0
5266 && (! bed->want_p_paddr_set_to_zero)
5267 && isec == 0
5268 && output_section->lma != 0
5269 && (output_section->vma == (segment->p_vaddr
5270 + (map->includes_filehdr
5271 ? iehdr->e_ehsize
5272 : 0)
5273 + (map->includes_phdrs
5274 ? (iehdr->e_phnum
5275 * iehdr->e_phentsize)
5276 : 0))))
5277 map->p_paddr = segment->p_vaddr;
5278
5279 /* Match up the physical address of the segment with the
5280 LMA address of the output section. */
5281 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5282 || IS_COREFILE_NOTE (segment, section)
5283 || (bed->want_p_paddr_set_to_zero &&
5284 IS_CONTAINED_BY_VMA (output_section, segment)))
5285 {
5286 if (matching_lma == 0)
5287 matching_lma = output_section->lma;
5288
5289 /* We assume that if the section fits within the segment
5290 then it does not overlap any other section within that
5291 segment. */
5292 map->sections[isec ++] = output_section;
5293 }
5294 else if (suggested_lma == 0)
5295 suggested_lma = output_section->lma;
5296 }
5297 }
5298
5299 BFD_ASSERT (j == section_count);
5300
5301 /* Step Two: Adjust the physical address of the current segment,
5302 if necessary. */
5303 if (isec == section_count)
5304 {
5305 /* All of the sections fitted within the segment as currently
5306 specified. This is the default case. Add the segment to
5307 the list of built segments and carry on to process the next
5308 program header in the input BFD. */
5309 map->count = section_count;
5310 *pointer_to_map = map;
5311 pointer_to_map = &map->next;
5312
5313 if (matching_lma != map->p_paddr
5314 && !map->includes_filehdr && !map->includes_phdrs)
5315 /* There is some padding before the first section in the
5316 segment. So, we must account for that in the output
5317 segment's vma. */
5318 map->p_vaddr_offset = matching_lma - map->p_paddr;
5319
5320 free (sections);
5321 continue;
5322 }
5323 else
5324 {
5325 if (matching_lma != 0)
5326 {
5327 /* At least one section fits inside the current segment.
5328 Keep it, but modify its physical address to match the
5329 LMA of the first section that fitted. */
5330 map->p_paddr = matching_lma;
5331 }
5332 else
5333 {
5334 /* None of the sections fitted inside the current segment.
5335 Change the current segment's physical address to match
5336 the LMA of the first section. */
5337 map->p_paddr = suggested_lma;
5338 }
5339
5340 /* Offset the segment physical address from the lma
5341 to allow for space taken up by elf headers. */
5342 if (map->includes_filehdr)
5343 map->p_paddr -= iehdr->e_ehsize;
5344
5345 if (map->includes_phdrs)
5346 {
5347 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5348
5349 /* iehdr->e_phnum is just an estimate of the number
5350 of program headers that we will need. Make a note
5351 here of the number we used and the segment we chose
5352 to hold these headers, so that we can adjust the
5353 offset when we know the correct value. */
5354 phdr_adjust_num = iehdr->e_phnum;
5355 phdr_adjust_seg = map;
5356 }
5357 }
5358
5359 /* Step Three: Loop over the sections again, this time assigning
5360 those that fit to the current segment and removing them from the
5361 sections array; but making sure not to leave large gaps. Once all
5362 possible sections have been assigned to the current segment it is
5363 added to the list of built segments and if sections still remain
5364 to be assigned, a new segment is constructed before repeating
5365 the loop. */
5366 isec = 0;
5367 do
5368 {
5369 map->count = 0;
5370 suggested_lma = 0;
5371
5372 /* Fill the current segment with sections that fit. */
5373 for (j = 0; j < section_count; j++)
5374 {
5375 section = sections[j];
5376
5377 if (section == NULL)
5378 continue;
5379
5380 output_section = section->output_section;
5381
5382 BFD_ASSERT (output_section != NULL);
5383
5384 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5385 || IS_COREFILE_NOTE (segment, section))
5386 {
5387 if (map->count == 0)
5388 {
5389 /* If the first section in a segment does not start at
5390 the beginning of the segment, then something is
5391 wrong. */
5392 if (output_section->lma !=
5393 (map->p_paddr
5394 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5395 + (map->includes_phdrs
5396 ? iehdr->e_phnum * iehdr->e_phentsize
5397 : 0)))
5398 abort ();
5399 }
5400 else
5401 {
5402 asection * prev_sec;
5403
5404 prev_sec = map->sections[map->count - 1];
5405
5406 /* If the gap between the end of the previous section
5407 and the start of this section is more than
5408 maxpagesize then we need to start a new segment. */
5409 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5410 maxpagesize)
5411 < BFD_ALIGN (output_section->lma, maxpagesize))
5412 || ((prev_sec->lma + prev_sec->size)
5413 > output_section->lma))
5414 {
5415 if (suggested_lma == 0)
5416 suggested_lma = output_section->lma;
5417
5418 continue;
5419 }
5420 }
5421
5422 map->sections[map->count++] = output_section;
5423 ++isec;
5424 sections[j] = NULL;
5425 section->segment_mark = TRUE;
5426 }
5427 else if (suggested_lma == 0)
5428 suggested_lma = output_section->lma;
5429 }
5430
5431 BFD_ASSERT (map->count > 0);
5432
5433 /* Add the current segment to the list of built segments. */
5434 *pointer_to_map = map;
5435 pointer_to_map = &map->next;
5436
5437 if (isec < section_count)
5438 {
5439 /* We still have not allocated all of the sections to
5440 segments. Create a new segment here, initialise it
5441 and carry on looping. */
5442 amt = sizeof (struct elf_segment_map);
5443 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5444 map = bfd_alloc (obfd, amt);
5445 if (map == NULL)
5446 {
5447 free (sections);
5448 return FALSE;
5449 }
5450
5451 /* Initialise the fields of the segment map. Set the physical
5452 physical address to the LMA of the first section that has
5453 not yet been assigned. */
5454 map->next = NULL;
5455 map->p_type = segment->p_type;
5456 map->p_flags = segment->p_flags;
5457 map->p_flags_valid = 1;
5458 map->p_paddr = suggested_lma;
5459 map->p_paddr_valid = 1;
5460 map->includes_filehdr = 0;
5461 map->includes_phdrs = 0;
5462 }
5463 }
5464 while (isec < section_count);
5465
5466 free (sections);
5467 }
5468
5469 /* The Solaris linker creates program headers in which all the
5470 p_paddr fields are zero. When we try to objcopy or strip such a
5471 file, we get confused. Check for this case, and if we find it
5472 reset the p_paddr_valid fields. */
5473 for (map = map_first; map != NULL; map = map->next)
5474 if (map->p_paddr != 0)
5475 break;
5476 if (map == NULL)
5477 for (map = map_first; map != NULL; map = map->next)
5478 map->p_paddr_valid = 0;
5479
5480 elf_tdata (obfd)->segment_map = map_first;
5481
5482 /* If we had to estimate the number of program headers that were
5483 going to be needed, then check our estimate now and adjust
5484 the offset if necessary. */
5485 if (phdr_adjust_seg != NULL)
5486 {
5487 unsigned int count;
5488
5489 for (count = 0, map = map_first; map != NULL; map = map->next)
5490 count++;
5491
5492 if (count > phdr_adjust_num)
5493 phdr_adjust_seg->p_paddr
5494 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5495 }
5496
5497 #undef SEGMENT_END
5498 #undef SECTION_SIZE
5499 #undef IS_CONTAINED_BY_VMA
5500 #undef IS_CONTAINED_BY_LMA
5501 #undef IS_COREFILE_NOTE
5502 #undef IS_SOLARIS_PT_INTERP
5503 #undef IS_SECTION_IN_INPUT_SEGMENT
5504 #undef INCLUDE_SECTION_IN_SEGMENT
5505 #undef SEGMENT_AFTER_SEGMENT
5506 #undef SEGMENT_OVERLAPS
5507 return TRUE;
5508 }
5509
5510 /* Copy ELF program header information. */
5511
5512 static bfd_boolean
5513 copy_elf_program_header (bfd *ibfd, bfd *obfd)
5514 {
5515 Elf_Internal_Ehdr *iehdr;
5516 struct elf_segment_map *map;
5517 struct elf_segment_map *map_first;
5518 struct elf_segment_map **pointer_to_map;
5519 Elf_Internal_Phdr *segment;
5520 unsigned int i;
5521 unsigned int num_segments;
5522 bfd_boolean phdr_included = FALSE;
5523
5524 iehdr = elf_elfheader (ibfd);
5525
5526 map_first = NULL;
5527 pointer_to_map = &map_first;
5528
5529 num_segments = elf_elfheader (ibfd)->e_phnum;
5530 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5531 i < num_segments;
5532 i++, segment++)
5533 {
5534 asection *section;
5535 unsigned int section_count;
5536 bfd_size_type amt;
5537 Elf_Internal_Shdr *this_hdr;
5538 asection *first_section = NULL;
5539
5540 /* FIXME: Do we need to copy PT_NULL segment? */
5541 if (segment->p_type == PT_NULL)
5542 continue;
5543
5544 /* Compute how many sections are in this segment. */
5545 for (section = ibfd->sections, section_count = 0;
5546 section != NULL;
5547 section = section->next)
5548 {
5549 this_hdr = &(elf_section_data(section)->this_hdr);
5550 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5551 {
5552 if (!first_section)
5553 first_section = section;
5554 section_count++;
5555 }
5556 }
5557
5558 /* Allocate a segment map big enough to contain
5559 all of the sections we have selected. */
5560 amt = sizeof (struct elf_segment_map);
5561 if (section_count != 0)
5562 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5563 map = bfd_zalloc (obfd, amt);
5564 if (map == NULL)
5565 return FALSE;
5566
5567 /* Initialize the fields of the output segment map with the
5568 input segment. */
5569 map->next = NULL;
5570 map->p_type = segment->p_type;
5571 map->p_flags = segment->p_flags;
5572 map->p_flags_valid = 1;
5573 map->p_paddr = segment->p_paddr;
5574 map->p_paddr_valid = 1;
5575 map->p_align = segment->p_align;
5576 map->p_align_valid = 1;
5577 map->p_vaddr_offset = 0;
5578
5579 /* Determine if this segment contains the ELF file header
5580 and if it contains the program headers themselves. */
5581 map->includes_filehdr = (segment->p_offset == 0
5582 && segment->p_filesz >= iehdr->e_ehsize);
5583
5584 map->includes_phdrs = 0;
5585 if (! phdr_included || segment->p_type != PT_LOAD)
5586 {
5587 map->includes_phdrs =
5588 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5589 && (segment->p_offset + segment->p_filesz
5590 >= ((bfd_vma) iehdr->e_phoff
5591 + iehdr->e_phnum * iehdr->e_phentsize)));
5592
5593 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5594 phdr_included = TRUE;
5595 }
5596
5597 if (!map->includes_phdrs && !map->includes_filehdr)
5598 /* There is some other padding before the first section. */
5599 map->p_vaddr_offset = ((first_section ? first_section->lma : 0)
5600 - segment->p_paddr);
5601
5602 if (section_count != 0)
5603 {
5604 unsigned int isec = 0;
5605
5606 for (section = first_section;
5607 section != NULL;
5608 section = section->next)
5609 {
5610 this_hdr = &(elf_section_data(section)->this_hdr);
5611 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5612 {
5613 map->sections[isec++] = section->output_section;
5614 if (isec == section_count)
5615 break;
5616 }
5617 }
5618 }
5619
5620 map->count = section_count;
5621 *pointer_to_map = map;
5622 pointer_to_map = &map->next;
5623 }
5624
5625 elf_tdata (obfd)->segment_map = map_first;
5626 return TRUE;
5627 }
5628
5629 /* Copy private BFD data. This copies or rewrites ELF program header
5630 information. */
5631
5632 static bfd_boolean
5633 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5634 {
5635 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5636 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5637 return TRUE;
5638
5639 if (elf_tdata (ibfd)->phdr == NULL)
5640 return TRUE;
5641
5642 if (ibfd->xvec == obfd->xvec)
5643 {
5644 /* Check to see if any sections in the input BFD
5645 covered by ELF program header have changed. */
5646 Elf_Internal_Phdr *segment;
5647 asection *section, *osec;
5648 unsigned int i, num_segments;
5649 Elf_Internal_Shdr *this_hdr;
5650
5651 /* Initialize the segment mark field. */
5652 for (section = obfd->sections; section != NULL;
5653 section = section->next)
5654 section->segment_mark = FALSE;
5655
5656 num_segments = elf_elfheader (ibfd)->e_phnum;
5657 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5658 i < num_segments;
5659 i++, segment++)
5660 {
5661 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5662 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5663 which severly confuses things, so always regenerate the segment
5664 map in this case. */
5665 if (segment->p_paddr == 0
5666 && segment->p_memsz == 0
5667 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
5668 goto rewrite;
5669
5670 for (section = ibfd->sections;
5671 section != NULL; section = section->next)
5672 {
5673 /* We mark the output section so that we know it comes
5674 from the input BFD. */
5675 osec = section->output_section;
5676 if (osec)
5677 osec->segment_mark = TRUE;
5678
5679 /* Check if this section is covered by the segment. */
5680 this_hdr = &(elf_section_data(section)->this_hdr);
5681 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5682 {
5683 /* FIXME: Check if its output section is changed or
5684 removed. What else do we need to check? */
5685 if (osec == NULL
5686 || section->flags != osec->flags
5687 || section->lma != osec->lma
5688 || section->vma != osec->vma
5689 || section->size != osec->size
5690 || section->rawsize != osec->rawsize
5691 || section->alignment_power != osec->alignment_power)
5692 goto rewrite;
5693 }
5694 }
5695 }
5696
5697 /* Check to see if any output section do not come from the
5698 input BFD. */
5699 for (section = obfd->sections; section != NULL;
5700 section = section->next)
5701 {
5702 if (section->segment_mark == FALSE)
5703 goto rewrite;
5704 else
5705 section->segment_mark = FALSE;
5706 }
5707
5708 return copy_elf_program_header (ibfd, obfd);
5709 }
5710
5711 rewrite:
5712 return rewrite_elf_program_header (ibfd, obfd);
5713 }
5714
5715 /* Initialize private output section information from input section. */
5716
5717 bfd_boolean
5718 _bfd_elf_init_private_section_data (bfd *ibfd,
5719 asection *isec,
5720 bfd *obfd,
5721 asection *osec,
5722 struct bfd_link_info *link_info)
5723
5724 {
5725 Elf_Internal_Shdr *ihdr, *ohdr;
5726 bfd_boolean need_group = link_info == NULL || link_info->relocatable;
5727
5728 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5729 || obfd->xvec->flavour != bfd_target_elf_flavour)
5730 return TRUE;
5731
5732 /* Don't copy the output ELF section type from input if the
5733 output BFD section flags have been set to something different.
5734 elf_fake_sections will set ELF section type based on BFD
5735 section flags. */
5736 if (elf_section_type (osec) == SHT_NULL
5737 && (osec->flags == isec->flags || !osec->flags))
5738 elf_section_type (osec) = elf_section_type (isec);
5739
5740 /* FIXME: Is this correct for all OS/PROC specific flags? */
5741 elf_section_flags (osec) |= (elf_section_flags (isec)
5742 & (SHF_MASKOS | SHF_MASKPROC));
5743
5744 /* Set things up for objcopy and relocatable link. The output
5745 SHT_GROUP section will have its elf_next_in_group pointing back
5746 to the input group members. Ignore linker created group section.
5747 See elfNN_ia64_object_p in elfxx-ia64.c. */
5748 if (need_group)
5749 {
5750 if (elf_sec_group (isec) == NULL
5751 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
5752 {
5753 if (elf_section_flags (isec) & SHF_GROUP)
5754 elf_section_flags (osec) |= SHF_GROUP;
5755 elf_next_in_group (osec) = elf_next_in_group (isec);
5756 elf_group_name (osec) = elf_group_name (isec);
5757 }
5758 }
5759
5760 ihdr = &elf_section_data (isec)->this_hdr;
5761
5762 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
5763 don't use the output section of the linked-to section since it
5764 may be NULL at this point. */
5765 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
5766 {
5767 ohdr = &elf_section_data (osec)->this_hdr;
5768 ohdr->sh_flags |= SHF_LINK_ORDER;
5769 elf_linked_to_section (osec) = elf_linked_to_section (isec);
5770 }
5771
5772 osec->use_rela_p = isec->use_rela_p;
5773
5774 return TRUE;
5775 }
5776
5777 /* Copy private section information. This copies over the entsize
5778 field, and sometimes the info field. */
5779
5780 bfd_boolean
5781 _bfd_elf_copy_private_section_data (bfd *ibfd,
5782 asection *isec,
5783 bfd *obfd,
5784 asection *osec)
5785 {
5786 Elf_Internal_Shdr *ihdr, *ohdr;
5787
5788 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5789 || obfd->xvec->flavour != bfd_target_elf_flavour)
5790 return TRUE;
5791
5792 ihdr = &elf_section_data (isec)->this_hdr;
5793 ohdr = &elf_section_data (osec)->this_hdr;
5794
5795 ohdr->sh_entsize = ihdr->sh_entsize;
5796
5797 if (ihdr->sh_type == SHT_SYMTAB
5798 || ihdr->sh_type == SHT_DYNSYM
5799 || ihdr->sh_type == SHT_GNU_verneed
5800 || ihdr->sh_type == SHT_GNU_verdef)
5801 ohdr->sh_info = ihdr->sh_info;
5802
5803 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
5804 NULL);
5805 }
5806
5807 /* Copy private header information. */
5808
5809 bfd_boolean
5810 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
5811 {
5812 asection *isec;
5813
5814 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5815 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5816 return TRUE;
5817
5818 /* Copy over private BFD data if it has not already been copied.
5819 This must be done here, rather than in the copy_private_bfd_data
5820 entry point, because the latter is called after the section
5821 contents have been set, which means that the program headers have
5822 already been worked out. */
5823 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5824 {
5825 if (! copy_private_bfd_data (ibfd, obfd))
5826 return FALSE;
5827 }
5828
5829 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
5830 but this might be wrong if we deleted the group section. */
5831 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
5832 if (elf_section_type (isec) == SHT_GROUP
5833 && isec->output_section == NULL)
5834 {
5835 asection *first = elf_next_in_group (isec);
5836 asection *s = first;
5837 while (s != NULL)
5838 {
5839 if (s->output_section != NULL)
5840 {
5841 elf_section_flags (s->output_section) &= ~SHF_GROUP;
5842 elf_group_name (s->output_section) = NULL;
5843 }
5844 s = elf_next_in_group (s);
5845 if (s == first)
5846 break;
5847 }
5848 }
5849
5850 return TRUE;
5851 }
5852
5853 /* Copy private symbol information. If this symbol is in a section
5854 which we did not map into a BFD section, try to map the section
5855 index correctly. We use special macro definitions for the mapped
5856 section indices; these definitions are interpreted by the
5857 swap_out_syms function. */
5858
5859 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5860 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5861 #define MAP_STRTAB (SHN_HIOS + 3)
5862 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5863 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5864
5865 bfd_boolean
5866 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
5867 asymbol *isymarg,
5868 bfd *obfd,
5869 asymbol *osymarg)
5870 {
5871 elf_symbol_type *isym, *osym;
5872
5873 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5874 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5875 return TRUE;
5876
5877 isym = elf_symbol_from (ibfd, isymarg);
5878 osym = elf_symbol_from (obfd, osymarg);
5879
5880 if (isym != NULL
5881 && osym != NULL
5882 && bfd_is_abs_section (isym->symbol.section))
5883 {
5884 unsigned int shndx;
5885
5886 shndx = isym->internal_elf_sym.st_shndx;
5887 if (shndx == elf_onesymtab (ibfd))
5888 shndx = MAP_ONESYMTAB;
5889 else if (shndx == elf_dynsymtab (ibfd))
5890 shndx = MAP_DYNSYMTAB;
5891 else if (shndx == elf_tdata (ibfd)->strtab_section)
5892 shndx = MAP_STRTAB;
5893 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5894 shndx = MAP_SHSTRTAB;
5895 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5896 shndx = MAP_SYM_SHNDX;
5897 osym->internal_elf_sym.st_shndx = shndx;
5898 }
5899
5900 return TRUE;
5901 }
5902
5903 /* Swap out the symbols. */
5904
5905 static bfd_boolean
5906 swap_out_syms (bfd *abfd,
5907 struct bfd_strtab_hash **sttp,
5908 int relocatable_p)
5909 {
5910 const struct elf_backend_data *bed;
5911 int symcount;
5912 asymbol **syms;
5913 struct bfd_strtab_hash *stt;
5914 Elf_Internal_Shdr *symtab_hdr;
5915 Elf_Internal_Shdr *symtab_shndx_hdr;
5916 Elf_Internal_Shdr *symstrtab_hdr;
5917 bfd_byte *outbound_syms;
5918 bfd_byte *outbound_shndx;
5919 int idx;
5920 bfd_size_type amt;
5921 bfd_boolean name_local_sections;
5922
5923 if (!elf_map_symbols (abfd))
5924 return FALSE;
5925
5926 /* Dump out the symtabs. */
5927 stt = _bfd_elf_stringtab_init ();
5928 if (stt == NULL)
5929 return FALSE;
5930
5931 bed = get_elf_backend_data (abfd);
5932 symcount = bfd_get_symcount (abfd);
5933 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5934 symtab_hdr->sh_type = SHT_SYMTAB;
5935 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5936 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5937 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5938 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
5939
5940 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5941 symstrtab_hdr->sh_type = SHT_STRTAB;
5942
5943 outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym);
5944 if (outbound_syms == NULL)
5945 {
5946 _bfd_stringtab_free (stt);
5947 return FALSE;
5948 }
5949 symtab_hdr->contents = outbound_syms;
5950
5951 outbound_shndx = NULL;
5952 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5953 if (symtab_shndx_hdr->sh_name != 0)
5954 {
5955 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
5956 outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount,
5957 sizeof (Elf_External_Sym_Shndx));
5958 if (outbound_shndx == NULL)
5959 {
5960 _bfd_stringtab_free (stt);
5961 return FALSE;
5962 }
5963
5964 symtab_shndx_hdr->contents = outbound_shndx;
5965 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5966 symtab_shndx_hdr->sh_size = amt;
5967 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5968 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5969 }
5970
5971 /* Now generate the data (for "contents"). */
5972 {
5973 /* Fill in zeroth symbol and swap it out. */
5974 Elf_Internal_Sym sym;
5975 sym.st_name = 0;
5976 sym.st_value = 0;
5977 sym.st_size = 0;
5978 sym.st_info = 0;
5979 sym.st_other = 0;
5980 sym.st_shndx = SHN_UNDEF;
5981 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5982 outbound_syms += bed->s->sizeof_sym;
5983 if (outbound_shndx != NULL)
5984 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5985 }
5986
5987 name_local_sections
5988 = (bed->elf_backend_name_local_section_symbols
5989 && bed->elf_backend_name_local_section_symbols (abfd));
5990
5991 syms = bfd_get_outsymbols (abfd);
5992 for (idx = 0; idx < symcount; idx++)
5993 {
5994 Elf_Internal_Sym sym;
5995 bfd_vma value = syms[idx]->value;
5996 elf_symbol_type *type_ptr;
5997 flagword flags = syms[idx]->flags;
5998 int type;
5999
6000 if (!name_local_sections
6001 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6002 {
6003 /* Local section symbols have no name. */
6004 sym.st_name = 0;
6005 }
6006 else
6007 {
6008 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6009 syms[idx]->name,
6010 TRUE, FALSE);
6011 if (sym.st_name == (unsigned long) -1)
6012 {
6013 _bfd_stringtab_free (stt);
6014 return FALSE;
6015 }
6016 }
6017
6018 type_ptr = elf_symbol_from (abfd, syms[idx]);
6019
6020 if ((flags & BSF_SECTION_SYM) == 0
6021 && bfd_is_com_section (syms[idx]->section))
6022 {
6023 /* ELF common symbols put the alignment into the `value' field,
6024 and the size into the `size' field. This is backwards from
6025 how BFD handles it, so reverse it here. */
6026 sym.st_size = value;
6027 if (type_ptr == NULL
6028 || type_ptr->internal_elf_sym.st_value == 0)
6029 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6030 else
6031 sym.st_value = type_ptr->internal_elf_sym.st_value;
6032 sym.st_shndx = _bfd_elf_section_from_bfd_section
6033 (abfd, syms[idx]->section);
6034 }
6035 else
6036 {
6037 asection *sec = syms[idx]->section;
6038 int shndx;
6039
6040 if (sec->output_section)
6041 {
6042 value += sec->output_offset;
6043 sec = sec->output_section;
6044 }
6045
6046 /* Don't add in the section vma for relocatable output. */
6047 if (! relocatable_p)
6048 value += sec->vma;
6049 sym.st_value = value;
6050 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6051
6052 if (bfd_is_abs_section (sec)
6053 && type_ptr != NULL
6054 && type_ptr->internal_elf_sym.st_shndx != 0)
6055 {
6056 /* This symbol is in a real ELF section which we did
6057 not create as a BFD section. Undo the mapping done
6058 by copy_private_symbol_data. */
6059 shndx = type_ptr->internal_elf_sym.st_shndx;
6060 switch (shndx)
6061 {
6062 case MAP_ONESYMTAB:
6063 shndx = elf_onesymtab (abfd);
6064 break;
6065 case MAP_DYNSYMTAB:
6066 shndx = elf_dynsymtab (abfd);
6067 break;
6068 case MAP_STRTAB:
6069 shndx = elf_tdata (abfd)->strtab_section;
6070 break;
6071 case MAP_SHSTRTAB:
6072 shndx = elf_tdata (abfd)->shstrtab_section;
6073 break;
6074 case MAP_SYM_SHNDX:
6075 shndx = elf_tdata (abfd)->symtab_shndx_section;
6076 break;
6077 default:
6078 break;
6079 }
6080 }
6081 else
6082 {
6083 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6084
6085 if (shndx == -1)
6086 {
6087 asection *sec2;
6088
6089 /* Writing this would be a hell of a lot easier if
6090 we had some decent documentation on bfd, and
6091 knew what to expect of the library, and what to
6092 demand of applications. For example, it
6093 appears that `objcopy' might not set the
6094 section of a symbol to be a section that is
6095 actually in the output file. */
6096 sec2 = bfd_get_section_by_name (abfd, sec->name);
6097 if (sec2 == NULL)
6098 {
6099 _bfd_error_handler (_("\
6100 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6101 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6102 sec->name);
6103 bfd_set_error (bfd_error_invalid_operation);
6104 _bfd_stringtab_free (stt);
6105 return FALSE;
6106 }
6107
6108 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6109 BFD_ASSERT (shndx != -1);
6110 }
6111 }
6112
6113 sym.st_shndx = shndx;
6114 }
6115
6116 if ((flags & BSF_THREAD_LOCAL) != 0)
6117 type = STT_TLS;
6118 else if ((flags & BSF_FUNCTION) != 0)
6119 type = STT_FUNC;
6120 else if ((flags & BSF_OBJECT) != 0)
6121 type = STT_OBJECT;
6122 else if ((flags & BSF_RELC) != 0)
6123 type = STT_RELC;
6124 else if ((flags & BSF_SRELC) != 0)
6125 type = STT_SRELC;
6126 else
6127 type = STT_NOTYPE;
6128
6129 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6130 type = STT_TLS;
6131
6132 /* Processor-specific types. */
6133 if (type_ptr != NULL
6134 && bed->elf_backend_get_symbol_type)
6135 type = ((*bed->elf_backend_get_symbol_type)
6136 (&type_ptr->internal_elf_sym, type));
6137
6138 if (flags & BSF_SECTION_SYM)
6139 {
6140 if (flags & BSF_GLOBAL)
6141 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6142 else
6143 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6144 }
6145 else if (bfd_is_com_section (syms[idx]->section))
6146 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6147 else if (bfd_is_und_section (syms[idx]->section))
6148 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6149 ? STB_WEAK
6150 : STB_GLOBAL),
6151 type);
6152 else if (flags & BSF_FILE)
6153 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6154 else
6155 {
6156 int bind = STB_LOCAL;
6157
6158 if (flags & BSF_LOCAL)
6159 bind = STB_LOCAL;
6160 else if (flags & BSF_WEAK)
6161 bind = STB_WEAK;
6162 else if (flags & BSF_GLOBAL)
6163 bind = STB_GLOBAL;
6164
6165 sym.st_info = ELF_ST_INFO (bind, type);
6166 }
6167
6168 if (type_ptr != NULL)
6169 sym.st_other = type_ptr->internal_elf_sym.st_other;
6170 else
6171 sym.st_other = 0;
6172
6173 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6174 outbound_syms += bed->s->sizeof_sym;
6175 if (outbound_shndx != NULL)
6176 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6177 }
6178
6179 *sttp = stt;
6180 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6181 symstrtab_hdr->sh_type = SHT_STRTAB;
6182
6183 symstrtab_hdr->sh_flags = 0;
6184 symstrtab_hdr->sh_addr = 0;
6185 symstrtab_hdr->sh_entsize = 0;
6186 symstrtab_hdr->sh_link = 0;
6187 symstrtab_hdr->sh_info = 0;
6188 symstrtab_hdr->sh_addralign = 1;
6189
6190 return TRUE;
6191 }
6192
6193 /* Return the number of bytes required to hold the symtab vector.
6194
6195 Note that we base it on the count plus 1, since we will null terminate
6196 the vector allocated based on this size. However, the ELF symbol table
6197 always has a dummy entry as symbol #0, so it ends up even. */
6198
6199 long
6200 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
6201 {
6202 long symcount;
6203 long symtab_size;
6204 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6205
6206 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6207 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6208 if (symcount > 0)
6209 symtab_size -= sizeof (asymbol *);
6210
6211 return symtab_size;
6212 }
6213
6214 long
6215 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6216 {
6217 long symcount;
6218 long symtab_size;
6219 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6220
6221 if (elf_dynsymtab (abfd) == 0)
6222 {
6223 bfd_set_error (bfd_error_invalid_operation);
6224 return -1;
6225 }
6226
6227 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6228 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6229 if (symcount > 0)
6230 symtab_size -= sizeof (asymbol *);
6231
6232 return symtab_size;
6233 }
6234
6235 long
6236 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6237 sec_ptr asect)
6238 {
6239 return (asect->reloc_count + 1) * sizeof (arelent *);
6240 }
6241
6242 /* Canonicalize the relocs. */
6243
6244 long
6245 _bfd_elf_canonicalize_reloc (bfd *abfd,
6246 sec_ptr section,
6247 arelent **relptr,
6248 asymbol **symbols)
6249 {
6250 arelent *tblptr;
6251 unsigned int i;
6252 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6253
6254 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6255 return -1;
6256
6257 tblptr = section->relocation;
6258 for (i = 0; i < section->reloc_count; i++)
6259 *relptr++ = tblptr++;
6260
6261 *relptr = NULL;
6262
6263 return section->reloc_count;
6264 }
6265
6266 long
6267 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6268 {
6269 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6270 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6271
6272 if (symcount >= 0)
6273 bfd_get_symcount (abfd) = symcount;
6274 return symcount;
6275 }
6276
6277 long
6278 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6279 asymbol **allocation)
6280 {
6281 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6282 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6283
6284 if (symcount >= 0)
6285 bfd_get_dynamic_symcount (abfd) = symcount;
6286 return symcount;
6287 }
6288
6289 /* Return the size required for the dynamic reloc entries. Any loadable
6290 section that was actually installed in the BFD, and has type SHT_REL
6291 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6292 dynamic reloc section. */
6293
6294 long
6295 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6296 {
6297 long ret;
6298 asection *s;
6299
6300 if (elf_dynsymtab (abfd) == 0)
6301 {
6302 bfd_set_error (bfd_error_invalid_operation);
6303 return -1;
6304 }
6305
6306 ret = sizeof (arelent *);
6307 for (s = abfd->sections; s != NULL; s = s->next)
6308 if ((s->flags & SEC_LOAD) != 0
6309 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6310 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6311 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6312 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6313 * sizeof (arelent *));
6314
6315 return ret;
6316 }
6317
6318 /* Canonicalize the dynamic relocation entries. Note that we return the
6319 dynamic relocations as a single block, although they are actually
6320 associated with particular sections; the interface, which was
6321 designed for SunOS style shared libraries, expects that there is only
6322 one set of dynamic relocs. Any loadable section that was actually
6323 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6324 dynamic symbol table, is considered to be a dynamic reloc section. */
6325
6326 long
6327 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6328 arelent **storage,
6329 asymbol **syms)
6330 {
6331 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6332 asection *s;
6333 long ret;
6334
6335 if (elf_dynsymtab (abfd) == 0)
6336 {
6337 bfd_set_error (bfd_error_invalid_operation);
6338 return -1;
6339 }
6340
6341 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6342 ret = 0;
6343 for (s = abfd->sections; s != NULL; s = s->next)
6344 {
6345 if ((s->flags & SEC_LOAD) != 0
6346 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6347 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6348 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6349 {
6350 arelent *p;
6351 long count, i;
6352
6353 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6354 return -1;
6355 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6356 p = s->relocation;
6357 for (i = 0; i < count; i++)
6358 *storage++ = p++;
6359 ret += count;
6360 }
6361 }
6362
6363 *storage = NULL;
6364
6365 return ret;
6366 }
6367 \f
6368 /* Read in the version information. */
6369
6370 bfd_boolean
6371 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6372 {
6373 bfd_byte *contents = NULL;
6374 unsigned int freeidx = 0;
6375
6376 if (elf_dynverref (abfd) != 0)
6377 {
6378 Elf_Internal_Shdr *hdr;
6379 Elf_External_Verneed *everneed;
6380 Elf_Internal_Verneed *iverneed;
6381 unsigned int i;
6382 bfd_byte *contents_end;
6383
6384 hdr = &elf_tdata (abfd)->dynverref_hdr;
6385
6386 elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info,
6387 sizeof (Elf_Internal_Verneed));
6388 if (elf_tdata (abfd)->verref == NULL)
6389 goto error_return;
6390
6391 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6392
6393 contents = bfd_malloc (hdr->sh_size);
6394 if (contents == NULL)
6395 {
6396 error_return_verref:
6397 elf_tdata (abfd)->verref = NULL;
6398 elf_tdata (abfd)->cverrefs = 0;
6399 goto error_return;
6400 }
6401 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6402 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6403 goto error_return_verref;
6404
6405 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6406 goto error_return_verref;
6407
6408 BFD_ASSERT (sizeof (Elf_External_Verneed)
6409 == sizeof (Elf_External_Vernaux));
6410 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6411 everneed = (Elf_External_Verneed *) contents;
6412 iverneed = elf_tdata (abfd)->verref;
6413 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6414 {
6415 Elf_External_Vernaux *evernaux;
6416 Elf_Internal_Vernaux *ivernaux;
6417 unsigned int j;
6418
6419 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6420
6421 iverneed->vn_bfd = abfd;
6422
6423 iverneed->vn_filename =
6424 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6425 iverneed->vn_file);
6426 if (iverneed->vn_filename == NULL)
6427 goto error_return_verref;
6428
6429 if (iverneed->vn_cnt == 0)
6430 iverneed->vn_auxptr = NULL;
6431 else
6432 {
6433 iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt,
6434 sizeof (Elf_Internal_Vernaux));
6435 if (iverneed->vn_auxptr == NULL)
6436 goto error_return_verref;
6437 }
6438
6439 if (iverneed->vn_aux
6440 > (size_t) (contents_end - (bfd_byte *) everneed))
6441 goto error_return_verref;
6442
6443 evernaux = ((Elf_External_Vernaux *)
6444 ((bfd_byte *) everneed + iverneed->vn_aux));
6445 ivernaux = iverneed->vn_auxptr;
6446 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6447 {
6448 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6449
6450 ivernaux->vna_nodename =
6451 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6452 ivernaux->vna_name);
6453 if (ivernaux->vna_nodename == NULL)
6454 goto error_return_verref;
6455
6456 if (j + 1 < iverneed->vn_cnt)
6457 ivernaux->vna_nextptr = ivernaux + 1;
6458 else
6459 ivernaux->vna_nextptr = NULL;
6460
6461 if (ivernaux->vna_next
6462 > (size_t) (contents_end - (bfd_byte *) evernaux))
6463 goto error_return_verref;
6464
6465 evernaux = ((Elf_External_Vernaux *)
6466 ((bfd_byte *) evernaux + ivernaux->vna_next));
6467
6468 if (ivernaux->vna_other > freeidx)
6469 freeidx = ivernaux->vna_other;
6470 }
6471
6472 if (i + 1 < hdr->sh_info)
6473 iverneed->vn_nextref = iverneed + 1;
6474 else
6475 iverneed->vn_nextref = NULL;
6476
6477 if (iverneed->vn_next
6478 > (size_t) (contents_end - (bfd_byte *) everneed))
6479 goto error_return_verref;
6480
6481 everneed = ((Elf_External_Verneed *)
6482 ((bfd_byte *) everneed + iverneed->vn_next));
6483 }
6484
6485 free (contents);
6486 contents = NULL;
6487 }
6488
6489 if (elf_dynverdef (abfd) != 0)
6490 {
6491 Elf_Internal_Shdr *hdr;
6492 Elf_External_Verdef *everdef;
6493 Elf_Internal_Verdef *iverdef;
6494 Elf_Internal_Verdef *iverdefarr;
6495 Elf_Internal_Verdef iverdefmem;
6496 unsigned int i;
6497 unsigned int maxidx;
6498 bfd_byte *contents_end_def, *contents_end_aux;
6499
6500 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6501
6502 contents = bfd_malloc (hdr->sh_size);
6503 if (contents == NULL)
6504 goto error_return;
6505 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6506 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6507 goto error_return;
6508
6509 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6510 goto error_return;
6511
6512 BFD_ASSERT (sizeof (Elf_External_Verdef)
6513 >= sizeof (Elf_External_Verdaux));
6514 contents_end_def = contents + hdr->sh_size
6515 - sizeof (Elf_External_Verdef);
6516 contents_end_aux = contents + hdr->sh_size
6517 - sizeof (Elf_External_Verdaux);
6518
6519 /* We know the number of entries in the section but not the maximum
6520 index. Therefore we have to run through all entries and find
6521 the maximum. */
6522 everdef = (Elf_External_Verdef *) contents;
6523 maxidx = 0;
6524 for (i = 0; i < hdr->sh_info; ++i)
6525 {
6526 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6527
6528 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6529 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6530
6531 if (iverdefmem.vd_next
6532 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6533 goto error_return;
6534
6535 everdef = ((Elf_External_Verdef *)
6536 ((bfd_byte *) everdef + iverdefmem.vd_next));
6537 }
6538
6539 if (default_imported_symver)
6540 {
6541 if (freeidx > maxidx)
6542 maxidx = ++freeidx;
6543 else
6544 freeidx = ++maxidx;
6545 }
6546 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx,
6547 sizeof (Elf_Internal_Verdef));
6548 if (elf_tdata (abfd)->verdef == NULL)
6549 goto error_return;
6550
6551 elf_tdata (abfd)->cverdefs = maxidx;
6552
6553 everdef = (Elf_External_Verdef *) contents;
6554 iverdefarr = elf_tdata (abfd)->verdef;
6555 for (i = 0; i < hdr->sh_info; i++)
6556 {
6557 Elf_External_Verdaux *everdaux;
6558 Elf_Internal_Verdaux *iverdaux;
6559 unsigned int j;
6560
6561 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6562
6563 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6564 {
6565 error_return_verdef:
6566 elf_tdata (abfd)->verdef = NULL;
6567 elf_tdata (abfd)->cverdefs = 0;
6568 goto error_return;
6569 }
6570
6571 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6572 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6573
6574 iverdef->vd_bfd = abfd;
6575
6576 if (iverdef->vd_cnt == 0)
6577 iverdef->vd_auxptr = NULL;
6578 else
6579 {
6580 iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt,
6581 sizeof (Elf_Internal_Verdaux));
6582 if (iverdef->vd_auxptr == NULL)
6583 goto error_return_verdef;
6584 }
6585
6586 if (iverdef->vd_aux
6587 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6588 goto error_return_verdef;
6589
6590 everdaux = ((Elf_External_Verdaux *)
6591 ((bfd_byte *) everdef + iverdef->vd_aux));
6592 iverdaux = iverdef->vd_auxptr;
6593 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6594 {
6595 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6596
6597 iverdaux->vda_nodename =
6598 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6599 iverdaux->vda_name);
6600 if (iverdaux->vda_nodename == NULL)
6601 goto error_return_verdef;
6602
6603 if (j + 1 < iverdef->vd_cnt)
6604 iverdaux->vda_nextptr = iverdaux + 1;
6605 else
6606 iverdaux->vda_nextptr = NULL;
6607
6608 if (iverdaux->vda_next
6609 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6610 goto error_return_verdef;
6611
6612 everdaux = ((Elf_External_Verdaux *)
6613 ((bfd_byte *) everdaux + iverdaux->vda_next));
6614 }
6615
6616 if (iverdef->vd_cnt)
6617 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6618
6619 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
6620 iverdef->vd_nextdef = iverdef + 1;
6621 else
6622 iverdef->vd_nextdef = NULL;
6623
6624 everdef = ((Elf_External_Verdef *)
6625 ((bfd_byte *) everdef + iverdef->vd_next));
6626 }
6627
6628 free (contents);
6629 contents = NULL;
6630 }
6631 else if (default_imported_symver)
6632 {
6633 if (freeidx < 3)
6634 freeidx = 3;
6635 else
6636 freeidx++;
6637
6638 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx,
6639 sizeof (Elf_Internal_Verdef));
6640 if (elf_tdata (abfd)->verdef == NULL)
6641 goto error_return;
6642
6643 elf_tdata (abfd)->cverdefs = freeidx;
6644 }
6645
6646 /* Create a default version based on the soname. */
6647 if (default_imported_symver)
6648 {
6649 Elf_Internal_Verdef *iverdef;
6650 Elf_Internal_Verdaux *iverdaux;
6651
6652 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6653
6654 iverdef->vd_version = VER_DEF_CURRENT;
6655 iverdef->vd_flags = 0;
6656 iverdef->vd_ndx = freeidx;
6657 iverdef->vd_cnt = 1;
6658
6659 iverdef->vd_bfd = abfd;
6660
6661 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6662 if (iverdef->vd_nodename == NULL)
6663 goto error_return_verdef;
6664 iverdef->vd_nextdef = NULL;
6665 iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6666 if (iverdef->vd_auxptr == NULL)
6667 goto error_return_verdef;
6668
6669 iverdaux = iverdef->vd_auxptr;
6670 iverdaux->vda_nodename = iverdef->vd_nodename;
6671 iverdaux->vda_nextptr = NULL;
6672 }
6673
6674 return TRUE;
6675
6676 error_return:
6677 if (contents != NULL)
6678 free (contents);
6679 return FALSE;
6680 }
6681 \f
6682 asymbol *
6683 _bfd_elf_make_empty_symbol (bfd *abfd)
6684 {
6685 elf_symbol_type *newsym;
6686 bfd_size_type amt = sizeof (elf_symbol_type);
6687
6688 newsym = bfd_zalloc (abfd, amt);
6689 if (!newsym)
6690 return NULL;
6691 else
6692 {
6693 newsym->symbol.the_bfd = abfd;
6694 return &newsym->symbol;
6695 }
6696 }
6697
6698 void
6699 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6700 asymbol *symbol,
6701 symbol_info *ret)
6702 {
6703 bfd_symbol_info (symbol, ret);
6704 }
6705
6706 /* Return whether a symbol name implies a local symbol. Most targets
6707 use this function for the is_local_label_name entry point, but some
6708 override it. */
6709
6710 bfd_boolean
6711 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6712 const char *name)
6713 {
6714 /* Normal local symbols start with ``.L''. */
6715 if (name[0] == '.' && name[1] == 'L')
6716 return TRUE;
6717
6718 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6719 DWARF debugging symbols starting with ``..''. */
6720 if (name[0] == '.' && name[1] == '.')
6721 return TRUE;
6722
6723 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6724 emitting DWARF debugging output. I suspect this is actually a
6725 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6726 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6727 underscore to be emitted on some ELF targets). For ease of use,
6728 we treat such symbols as local. */
6729 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
6730 return TRUE;
6731
6732 return FALSE;
6733 }
6734
6735 alent *
6736 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6737 asymbol *symbol ATTRIBUTE_UNUSED)
6738 {
6739 abort ();
6740 return NULL;
6741 }
6742
6743 bfd_boolean
6744 _bfd_elf_set_arch_mach (bfd *abfd,
6745 enum bfd_architecture arch,
6746 unsigned long machine)
6747 {
6748 /* If this isn't the right architecture for this backend, and this
6749 isn't the generic backend, fail. */
6750 if (arch != get_elf_backend_data (abfd)->arch
6751 && arch != bfd_arch_unknown
6752 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
6753 return FALSE;
6754
6755 return bfd_default_set_arch_mach (abfd, arch, machine);
6756 }
6757
6758 /* Find the function to a particular section and offset,
6759 for error reporting. */
6760
6761 static bfd_boolean
6762 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
6763 asection *section,
6764 asymbol **symbols,
6765 bfd_vma offset,
6766 const char **filename_ptr,
6767 const char **functionname_ptr)
6768 {
6769 const char *filename;
6770 asymbol *func, *file;
6771 bfd_vma low_func;
6772 asymbol **p;
6773 /* ??? Given multiple file symbols, it is impossible to reliably
6774 choose the right file name for global symbols. File symbols are
6775 local symbols, and thus all file symbols must sort before any
6776 global symbols. The ELF spec may be interpreted to say that a
6777 file symbol must sort before other local symbols, but currently
6778 ld -r doesn't do this. So, for ld -r output, it is possible to
6779 make a better choice of file name for local symbols by ignoring
6780 file symbols appearing after a given local symbol. */
6781 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
6782
6783 filename = NULL;
6784 func = NULL;
6785 file = NULL;
6786 low_func = 0;
6787 state = nothing_seen;
6788
6789 for (p = symbols; *p != NULL; p++)
6790 {
6791 elf_symbol_type *q;
6792
6793 q = (elf_symbol_type *) *p;
6794
6795 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6796 {
6797 default:
6798 break;
6799 case STT_FILE:
6800 file = &q->symbol;
6801 if (state == symbol_seen)
6802 state = file_after_symbol_seen;
6803 continue;
6804 case STT_NOTYPE:
6805 case STT_FUNC:
6806 if (bfd_get_section (&q->symbol) == section
6807 && q->symbol.value >= low_func
6808 && q->symbol.value <= offset)
6809 {
6810 func = (asymbol *) q;
6811 low_func = q->symbol.value;
6812 filename = NULL;
6813 if (file != NULL
6814 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
6815 || state != file_after_symbol_seen))
6816 filename = bfd_asymbol_name (file);
6817 }
6818 break;
6819 }
6820 if (state == nothing_seen)
6821 state = symbol_seen;
6822 }
6823
6824 if (func == NULL)
6825 return FALSE;
6826
6827 if (filename_ptr)
6828 *filename_ptr = filename;
6829 if (functionname_ptr)
6830 *functionname_ptr = bfd_asymbol_name (func);
6831
6832 return TRUE;
6833 }
6834
6835 /* Find the nearest line to a particular section and offset,
6836 for error reporting. */
6837
6838 bfd_boolean
6839 _bfd_elf_find_nearest_line (bfd *abfd,
6840 asection *section,
6841 asymbol **symbols,
6842 bfd_vma offset,
6843 const char **filename_ptr,
6844 const char **functionname_ptr,
6845 unsigned int *line_ptr)
6846 {
6847 bfd_boolean found;
6848
6849 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6850 filename_ptr, functionname_ptr,
6851 line_ptr))
6852 {
6853 if (!*functionname_ptr)
6854 elf_find_function (abfd, section, symbols, offset,
6855 *filename_ptr ? NULL : filename_ptr,
6856 functionname_ptr);
6857
6858 return TRUE;
6859 }
6860
6861 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6862 filename_ptr, functionname_ptr,
6863 line_ptr, 0,
6864 &elf_tdata (abfd)->dwarf2_find_line_info))
6865 {
6866 if (!*functionname_ptr)
6867 elf_find_function (abfd, section, symbols, offset,
6868 *filename_ptr ? NULL : filename_ptr,
6869 functionname_ptr);
6870
6871 return TRUE;
6872 }
6873
6874 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6875 &found, filename_ptr,
6876 functionname_ptr, line_ptr,
6877 &elf_tdata (abfd)->line_info))
6878 return FALSE;
6879 if (found && (*functionname_ptr || *line_ptr))
6880 return TRUE;
6881
6882 if (symbols == NULL)
6883 return FALSE;
6884
6885 if (! elf_find_function (abfd, section, symbols, offset,
6886 filename_ptr, functionname_ptr))
6887 return FALSE;
6888
6889 *line_ptr = 0;
6890 return TRUE;
6891 }
6892
6893 /* Find the line for a symbol. */
6894
6895 bfd_boolean
6896 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
6897 const char **filename_ptr, unsigned int *line_ptr)
6898 {
6899 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
6900 filename_ptr, line_ptr, 0,
6901 &elf_tdata (abfd)->dwarf2_find_line_info);
6902 }
6903
6904 /* After a call to bfd_find_nearest_line, successive calls to
6905 bfd_find_inliner_info can be used to get source information about
6906 each level of function inlining that terminated at the address
6907 passed to bfd_find_nearest_line. Currently this is only supported
6908 for DWARF2 with appropriate DWARF3 extensions. */
6909
6910 bfd_boolean
6911 _bfd_elf_find_inliner_info (bfd *abfd,
6912 const char **filename_ptr,
6913 const char **functionname_ptr,
6914 unsigned int *line_ptr)
6915 {
6916 bfd_boolean found;
6917 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
6918 functionname_ptr, line_ptr,
6919 & elf_tdata (abfd)->dwarf2_find_line_info);
6920 return found;
6921 }
6922
6923 int
6924 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
6925 {
6926 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6927 int ret = bed->s->sizeof_ehdr;
6928
6929 if (!info->relocatable)
6930 {
6931 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
6932
6933 if (phdr_size == (bfd_size_type) -1)
6934 {
6935 struct elf_segment_map *m;
6936
6937 phdr_size = 0;
6938 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
6939 phdr_size += bed->s->sizeof_phdr;
6940
6941 if (phdr_size == 0)
6942 phdr_size = get_program_header_size (abfd, info);
6943 }
6944
6945 elf_tdata (abfd)->program_header_size = phdr_size;
6946 ret += phdr_size;
6947 }
6948
6949 return ret;
6950 }
6951
6952 bfd_boolean
6953 _bfd_elf_set_section_contents (bfd *abfd,
6954 sec_ptr section,
6955 const void *location,
6956 file_ptr offset,
6957 bfd_size_type count)
6958 {
6959 Elf_Internal_Shdr *hdr;
6960 bfd_signed_vma pos;
6961
6962 if (! abfd->output_has_begun
6963 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6964 return FALSE;
6965
6966 hdr = &elf_section_data (section)->this_hdr;
6967 pos = hdr->sh_offset + offset;
6968 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6969 || bfd_bwrite (location, count, abfd) != count)
6970 return FALSE;
6971
6972 return TRUE;
6973 }
6974
6975 void
6976 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
6977 arelent *cache_ptr ATTRIBUTE_UNUSED,
6978 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
6979 {
6980 abort ();
6981 }
6982
6983 /* Try to convert a non-ELF reloc into an ELF one. */
6984
6985 bfd_boolean
6986 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
6987 {
6988 /* Check whether we really have an ELF howto. */
6989
6990 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6991 {
6992 bfd_reloc_code_real_type code;
6993 reloc_howto_type *howto;
6994
6995 /* Alien reloc: Try to determine its type to replace it with an
6996 equivalent ELF reloc. */
6997
6998 if (areloc->howto->pc_relative)
6999 {
7000 switch (areloc->howto->bitsize)
7001 {
7002 case 8:
7003 code = BFD_RELOC_8_PCREL;
7004 break;
7005 case 12:
7006 code = BFD_RELOC_12_PCREL;
7007 break;
7008 case 16:
7009 code = BFD_RELOC_16_PCREL;
7010 break;
7011 case 24:
7012 code = BFD_RELOC_24_PCREL;
7013 break;
7014 case 32:
7015 code = BFD_RELOC_32_PCREL;
7016 break;
7017 case 64:
7018 code = BFD_RELOC_64_PCREL;
7019 break;
7020 default:
7021 goto fail;
7022 }
7023
7024 howto = bfd_reloc_type_lookup (abfd, code);
7025
7026 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7027 {
7028 if (howto->pcrel_offset)
7029 areloc->addend += areloc->address;
7030 else
7031 areloc->addend -= areloc->address; /* addend is unsigned!! */
7032 }
7033 }
7034 else
7035 {
7036 switch (areloc->howto->bitsize)
7037 {
7038 case 8:
7039 code = BFD_RELOC_8;
7040 break;
7041 case 14:
7042 code = BFD_RELOC_14;
7043 break;
7044 case 16:
7045 code = BFD_RELOC_16;
7046 break;
7047 case 26:
7048 code = BFD_RELOC_26;
7049 break;
7050 case 32:
7051 code = BFD_RELOC_32;
7052 break;
7053 case 64:
7054 code = BFD_RELOC_64;
7055 break;
7056 default:
7057 goto fail;
7058 }
7059
7060 howto = bfd_reloc_type_lookup (abfd, code);
7061 }
7062
7063 if (howto)
7064 areloc->howto = howto;
7065 else
7066 goto fail;
7067 }
7068
7069 return TRUE;
7070
7071 fail:
7072 (*_bfd_error_handler)
7073 (_("%B: unsupported relocation type %s"),
7074 abfd, areloc->howto->name);
7075 bfd_set_error (bfd_error_bad_value);
7076 return FALSE;
7077 }
7078
7079 bfd_boolean
7080 _bfd_elf_close_and_cleanup (bfd *abfd)
7081 {
7082 if (bfd_get_format (abfd) == bfd_object)
7083 {
7084 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7085 _bfd_elf_strtab_free (elf_shstrtab (abfd));
7086 _bfd_dwarf2_cleanup_debug_info (abfd);
7087 }
7088
7089 return _bfd_generic_close_and_cleanup (abfd);
7090 }
7091
7092 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7093 in the relocation's offset. Thus we cannot allow any sort of sanity
7094 range-checking to interfere. There is nothing else to do in processing
7095 this reloc. */
7096
7097 bfd_reloc_status_type
7098 _bfd_elf_rel_vtable_reloc_fn
7099 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7100 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7101 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7102 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7103 {
7104 return bfd_reloc_ok;
7105 }
7106 \f
7107 /* Elf core file support. Much of this only works on native
7108 toolchains, since we rely on knowing the
7109 machine-dependent procfs structure in order to pick
7110 out details about the corefile. */
7111
7112 #ifdef HAVE_SYS_PROCFS_H
7113 # include <sys/procfs.h>
7114 #endif
7115
7116 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7117
7118 static int
7119 elfcore_make_pid (bfd *abfd)
7120 {
7121 return ((elf_tdata (abfd)->core_lwpid << 16)
7122 + (elf_tdata (abfd)->core_pid));
7123 }
7124
7125 /* If there isn't a section called NAME, make one, using
7126 data from SECT. Note, this function will generate a
7127 reference to NAME, so you shouldn't deallocate or
7128 overwrite it. */
7129
7130 static bfd_boolean
7131 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7132 {
7133 asection *sect2;
7134
7135 if (bfd_get_section_by_name (abfd, name) != NULL)
7136 return TRUE;
7137
7138 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7139 if (sect2 == NULL)
7140 return FALSE;
7141
7142 sect2->size = sect->size;
7143 sect2->filepos = sect->filepos;
7144 sect2->alignment_power = sect->alignment_power;
7145 return TRUE;
7146 }
7147
7148 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7149 actually creates up to two pseudosections:
7150 - For the single-threaded case, a section named NAME, unless
7151 such a section already exists.
7152 - For the multi-threaded case, a section named "NAME/PID", where
7153 PID is elfcore_make_pid (abfd).
7154 Both pseudosections have identical contents. */
7155 bfd_boolean
7156 _bfd_elfcore_make_pseudosection (bfd *abfd,
7157 char *name,
7158 size_t size,
7159 ufile_ptr filepos)
7160 {
7161 char buf[100];
7162 char *threaded_name;
7163 size_t len;
7164 asection *sect;
7165
7166 /* Build the section name. */
7167
7168 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7169 len = strlen (buf) + 1;
7170 threaded_name = bfd_alloc (abfd, len);
7171 if (threaded_name == NULL)
7172 return FALSE;
7173 memcpy (threaded_name, buf, len);
7174
7175 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7176 SEC_HAS_CONTENTS);
7177 if (sect == NULL)
7178 return FALSE;
7179 sect->size = size;
7180 sect->filepos = filepos;
7181 sect->alignment_power = 2;
7182
7183 return elfcore_maybe_make_sect (abfd, name, sect);
7184 }
7185
7186 /* prstatus_t exists on:
7187 solaris 2.5+
7188 linux 2.[01] + glibc
7189 unixware 4.2
7190 */
7191
7192 #if defined (HAVE_PRSTATUS_T)
7193
7194 static bfd_boolean
7195 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7196 {
7197 size_t size;
7198 int offset;
7199
7200 if (note->descsz == sizeof (prstatus_t))
7201 {
7202 prstatus_t prstat;
7203
7204 size = sizeof (prstat.pr_reg);
7205 offset = offsetof (prstatus_t, pr_reg);
7206 memcpy (&prstat, note->descdata, sizeof (prstat));
7207
7208 /* Do not overwrite the core signal if it
7209 has already been set by another thread. */
7210 if (elf_tdata (abfd)->core_signal == 0)
7211 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7212 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7213
7214 /* pr_who exists on:
7215 solaris 2.5+
7216 unixware 4.2
7217 pr_who doesn't exist on:
7218 linux 2.[01]
7219 */
7220 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7221 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7222 #endif
7223 }
7224 #if defined (HAVE_PRSTATUS32_T)
7225 else if (note->descsz == sizeof (prstatus32_t))
7226 {
7227 /* 64-bit host, 32-bit corefile */
7228 prstatus32_t prstat;
7229
7230 size = sizeof (prstat.pr_reg);
7231 offset = offsetof (prstatus32_t, pr_reg);
7232 memcpy (&prstat, note->descdata, sizeof (prstat));
7233
7234 /* Do not overwrite the core signal if it
7235 has already been set by another thread. */
7236 if (elf_tdata (abfd)->core_signal == 0)
7237 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7238 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7239
7240 /* pr_who exists on:
7241 solaris 2.5+
7242 unixware 4.2
7243 pr_who doesn't exist on:
7244 linux 2.[01]
7245 */
7246 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7247 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7248 #endif
7249 }
7250 #endif /* HAVE_PRSTATUS32_T */
7251 else
7252 {
7253 /* Fail - we don't know how to handle any other
7254 note size (ie. data object type). */
7255 return TRUE;
7256 }
7257
7258 /* Make a ".reg/999" section and a ".reg" section. */
7259 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7260 size, note->descpos + offset);
7261 }
7262 #endif /* defined (HAVE_PRSTATUS_T) */
7263
7264 /* Create a pseudosection containing the exact contents of NOTE. */
7265 static bfd_boolean
7266 elfcore_make_note_pseudosection (bfd *abfd,
7267 char *name,
7268 Elf_Internal_Note *note)
7269 {
7270 return _bfd_elfcore_make_pseudosection (abfd, name,
7271 note->descsz, note->descpos);
7272 }
7273
7274 /* There isn't a consistent prfpregset_t across platforms,
7275 but it doesn't matter, because we don't have to pick this
7276 data structure apart. */
7277
7278 static bfd_boolean
7279 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7280 {
7281 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7282 }
7283
7284 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7285 type of 5 (NT_PRXFPREG). Just include the whole note's contents
7286 literally. */
7287
7288 static bfd_boolean
7289 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7290 {
7291 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7292 }
7293
7294 #if defined (HAVE_PRPSINFO_T)
7295 typedef prpsinfo_t elfcore_psinfo_t;
7296 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7297 typedef prpsinfo32_t elfcore_psinfo32_t;
7298 #endif
7299 #endif
7300
7301 #if defined (HAVE_PSINFO_T)
7302 typedef psinfo_t elfcore_psinfo_t;
7303 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7304 typedef psinfo32_t elfcore_psinfo32_t;
7305 #endif
7306 #endif
7307
7308 /* return a malloc'ed copy of a string at START which is at
7309 most MAX bytes long, possibly without a terminating '\0'.
7310 the copy will always have a terminating '\0'. */
7311
7312 char *
7313 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7314 {
7315 char *dups;
7316 char *end = memchr (start, '\0', max);
7317 size_t len;
7318
7319 if (end == NULL)
7320 len = max;
7321 else
7322 len = end - start;
7323
7324 dups = bfd_alloc (abfd, len + 1);
7325 if (dups == NULL)
7326 return NULL;
7327
7328 memcpy (dups, start, len);
7329 dups[len] = '\0';
7330
7331 return dups;
7332 }
7333
7334 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7335 static bfd_boolean
7336 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7337 {
7338 if (note->descsz == sizeof (elfcore_psinfo_t))
7339 {
7340 elfcore_psinfo_t psinfo;
7341
7342 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7343
7344 elf_tdata (abfd)->core_program
7345 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7346 sizeof (psinfo.pr_fname));
7347
7348 elf_tdata (abfd)->core_command
7349 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7350 sizeof (psinfo.pr_psargs));
7351 }
7352 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7353 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7354 {
7355 /* 64-bit host, 32-bit corefile */
7356 elfcore_psinfo32_t psinfo;
7357
7358 memcpy (&psinfo, note->descdata, sizeof (psinfo));
7359
7360 elf_tdata (abfd)->core_program
7361 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7362 sizeof (psinfo.pr_fname));
7363
7364 elf_tdata (abfd)->core_command
7365 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7366 sizeof (psinfo.pr_psargs));
7367 }
7368 #endif
7369
7370 else
7371 {
7372 /* Fail - we don't know how to handle any other
7373 note size (ie. data object type). */
7374 return TRUE;
7375 }
7376
7377 /* Note that for some reason, a spurious space is tacked
7378 onto the end of the args in some (at least one anyway)
7379 implementations, so strip it off if it exists. */
7380
7381 {
7382 char *command = elf_tdata (abfd)->core_command;
7383 int n = strlen (command);
7384
7385 if (0 < n && command[n - 1] == ' ')
7386 command[n - 1] = '\0';
7387 }
7388
7389 return TRUE;
7390 }
7391 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7392
7393 #if defined (HAVE_PSTATUS_T)
7394 static bfd_boolean
7395 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7396 {
7397 if (note->descsz == sizeof (pstatus_t)
7398 #if defined (HAVE_PXSTATUS_T)
7399 || note->descsz == sizeof (pxstatus_t)
7400 #endif
7401 )
7402 {
7403 pstatus_t pstat;
7404
7405 memcpy (&pstat, note->descdata, sizeof (pstat));
7406
7407 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7408 }
7409 #if defined (HAVE_PSTATUS32_T)
7410 else if (note->descsz == sizeof (pstatus32_t))
7411 {
7412 /* 64-bit host, 32-bit corefile */
7413 pstatus32_t pstat;
7414
7415 memcpy (&pstat, note->descdata, sizeof (pstat));
7416
7417 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7418 }
7419 #endif
7420 /* Could grab some more details from the "representative"
7421 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7422 NT_LWPSTATUS note, presumably. */
7423
7424 return TRUE;
7425 }
7426 #endif /* defined (HAVE_PSTATUS_T) */
7427
7428 #if defined (HAVE_LWPSTATUS_T)
7429 static bfd_boolean
7430 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7431 {
7432 lwpstatus_t lwpstat;
7433 char buf[100];
7434 char *name;
7435 size_t len;
7436 asection *sect;
7437
7438 if (note->descsz != sizeof (lwpstat)
7439 #if defined (HAVE_LWPXSTATUS_T)
7440 && note->descsz != sizeof (lwpxstatus_t)
7441 #endif
7442 )
7443 return TRUE;
7444
7445 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7446
7447 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7448 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7449
7450 /* Make a ".reg/999" section. */
7451
7452 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7453 len = strlen (buf) + 1;
7454 name = bfd_alloc (abfd, len);
7455 if (name == NULL)
7456 return FALSE;
7457 memcpy (name, buf, len);
7458
7459 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7460 if (sect == NULL)
7461 return FALSE;
7462
7463 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7464 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7465 sect->filepos = note->descpos
7466 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7467 #endif
7468
7469 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7470 sect->size = sizeof (lwpstat.pr_reg);
7471 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7472 #endif
7473
7474 sect->alignment_power = 2;
7475
7476 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7477 return FALSE;
7478
7479 /* Make a ".reg2/999" section */
7480
7481 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7482 len = strlen (buf) + 1;
7483 name = bfd_alloc (abfd, len);
7484 if (name == NULL)
7485 return FALSE;
7486 memcpy (name, buf, len);
7487
7488 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7489 if (sect == NULL)
7490 return FALSE;
7491
7492 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7493 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7494 sect->filepos = note->descpos
7495 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7496 #endif
7497
7498 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7499 sect->size = sizeof (lwpstat.pr_fpreg);
7500 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7501 #endif
7502
7503 sect->alignment_power = 2;
7504
7505 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7506 }
7507 #endif /* defined (HAVE_LWPSTATUS_T) */
7508
7509 #if defined (HAVE_WIN32_PSTATUS_T)
7510 static bfd_boolean
7511 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7512 {
7513 char buf[30];
7514 char *name;
7515 size_t len;
7516 asection *sect;
7517 win32_pstatus_t pstatus;
7518
7519 if (note->descsz < sizeof (pstatus))
7520 return TRUE;
7521
7522 memcpy (&pstatus, note->descdata, sizeof (pstatus));
7523
7524 switch (pstatus.data_type)
7525 {
7526 case NOTE_INFO_PROCESS:
7527 /* FIXME: need to add ->core_command. */
7528 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
7529 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
7530 break;
7531
7532 case NOTE_INFO_THREAD:
7533 /* Make a ".reg/999" section. */
7534 sprintf (buf, ".reg/%ld", (long) pstatus.data.thread_info.tid);
7535
7536 len = strlen (buf) + 1;
7537 name = bfd_alloc (abfd, len);
7538 if (name == NULL)
7539 return FALSE;
7540
7541 memcpy (name, buf, len);
7542
7543 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7544 if (sect == NULL)
7545 return FALSE;
7546
7547 sect->size = sizeof (pstatus.data.thread_info.thread_context);
7548 sect->filepos = (note->descpos
7549 + offsetof (struct win32_pstatus,
7550 data.thread_info.thread_context));
7551 sect->alignment_power = 2;
7552
7553 if (pstatus.data.thread_info.is_active_thread)
7554 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7555 return FALSE;
7556 break;
7557
7558 case NOTE_INFO_MODULE:
7559 /* Make a ".module/xxxxxxxx" section. */
7560 sprintf (buf, ".module/%08lx",
7561 (long) pstatus.data.module_info.base_address);
7562
7563 len = strlen (buf) + 1;
7564 name = bfd_alloc (abfd, len);
7565 if (name == NULL)
7566 return FALSE;
7567
7568 memcpy (name, buf, len);
7569
7570 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7571
7572 if (sect == NULL)
7573 return FALSE;
7574
7575 sect->size = note->descsz;
7576 sect->filepos = note->descpos;
7577 sect->alignment_power = 2;
7578 break;
7579
7580 default:
7581 return TRUE;
7582 }
7583
7584 return TRUE;
7585 }
7586 #endif /* HAVE_WIN32_PSTATUS_T */
7587
7588 static bfd_boolean
7589 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7590 {
7591 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7592
7593 switch (note->type)
7594 {
7595 default:
7596 return TRUE;
7597
7598 case NT_PRSTATUS:
7599 if (bed->elf_backend_grok_prstatus)
7600 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7601 return TRUE;
7602 #if defined (HAVE_PRSTATUS_T)
7603 return elfcore_grok_prstatus (abfd, note);
7604 #else
7605 return TRUE;
7606 #endif
7607
7608 #if defined (HAVE_PSTATUS_T)
7609 case NT_PSTATUS:
7610 return elfcore_grok_pstatus (abfd, note);
7611 #endif
7612
7613 #if defined (HAVE_LWPSTATUS_T)
7614 case NT_LWPSTATUS:
7615 return elfcore_grok_lwpstatus (abfd, note);
7616 #endif
7617
7618 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7619 return elfcore_grok_prfpreg (abfd, note);
7620
7621 #if defined (HAVE_WIN32_PSTATUS_T)
7622 case NT_WIN32PSTATUS:
7623 return elfcore_grok_win32pstatus (abfd, note);
7624 #endif
7625
7626 case NT_PRXFPREG: /* Linux SSE extension */
7627 if (note->namesz == 6
7628 && strcmp (note->namedata, "LINUX") == 0)
7629 return elfcore_grok_prxfpreg (abfd, note);
7630 else
7631 return TRUE;
7632
7633 case NT_PRPSINFO:
7634 case NT_PSINFO:
7635 if (bed->elf_backend_grok_psinfo)
7636 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7637 return TRUE;
7638 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7639 return elfcore_grok_psinfo (abfd, note);
7640 #else
7641 return TRUE;
7642 #endif
7643
7644 case NT_AUXV:
7645 {
7646 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
7647 SEC_HAS_CONTENTS);
7648
7649 if (sect == NULL)
7650 return FALSE;
7651 sect->size = note->descsz;
7652 sect->filepos = note->descpos;
7653 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7654
7655 return TRUE;
7656 }
7657 }
7658 }
7659
7660 static bfd_boolean
7661 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
7662 {
7663 char *cp;
7664
7665 cp = strchr (note->namedata, '@');
7666 if (cp != NULL)
7667 {
7668 *lwpidp = atoi(cp + 1);
7669 return TRUE;
7670 }
7671 return FALSE;
7672 }
7673
7674 static bfd_boolean
7675 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
7676 {
7677 /* Signal number at offset 0x08. */
7678 elf_tdata (abfd)->core_signal
7679 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
7680
7681 /* Process ID at offset 0x50. */
7682 elf_tdata (abfd)->core_pid
7683 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
7684
7685 /* Command name at 0x7c (max 32 bytes, including nul). */
7686 elf_tdata (abfd)->core_command
7687 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
7688
7689 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
7690 note);
7691 }
7692
7693 static bfd_boolean
7694 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
7695 {
7696 int lwp;
7697
7698 if (elfcore_netbsd_get_lwpid (note, &lwp))
7699 elf_tdata (abfd)->core_lwpid = lwp;
7700
7701 if (note->type == NT_NETBSDCORE_PROCINFO)
7702 {
7703 /* NetBSD-specific core "procinfo". Note that we expect to
7704 find this note before any of the others, which is fine,
7705 since the kernel writes this note out first when it
7706 creates a core file. */
7707
7708 return elfcore_grok_netbsd_procinfo (abfd, note);
7709 }
7710
7711 /* As of Jan 2002 there are no other machine-independent notes
7712 defined for NetBSD core files. If the note type is less
7713 than the start of the machine-dependent note types, we don't
7714 understand it. */
7715
7716 if (note->type < NT_NETBSDCORE_FIRSTMACH)
7717 return TRUE;
7718
7719
7720 switch (bfd_get_arch (abfd))
7721 {
7722 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7723 PT_GETFPREGS == mach+2. */
7724
7725 case bfd_arch_alpha:
7726 case bfd_arch_sparc:
7727 switch (note->type)
7728 {
7729 case NT_NETBSDCORE_FIRSTMACH+0:
7730 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7731
7732 case NT_NETBSDCORE_FIRSTMACH+2:
7733 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7734
7735 default:
7736 return TRUE;
7737 }
7738
7739 /* On all other arch's, PT_GETREGS == mach+1 and
7740 PT_GETFPREGS == mach+3. */
7741
7742 default:
7743 switch (note->type)
7744 {
7745 case NT_NETBSDCORE_FIRSTMACH+1:
7746 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7747
7748 case NT_NETBSDCORE_FIRSTMACH+3:
7749 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7750
7751 default:
7752 return TRUE;
7753 }
7754 }
7755 /* NOTREACHED */
7756 }
7757
7758 static bfd_boolean
7759 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
7760 {
7761 void *ddata = note->descdata;
7762 char buf[100];
7763 char *name;
7764 asection *sect;
7765 short sig;
7766 unsigned flags;
7767
7768 /* nto_procfs_status 'pid' field is at offset 0. */
7769 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
7770
7771 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
7772 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
7773
7774 /* nto_procfs_status 'flags' field is at offset 8. */
7775 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
7776
7777 /* nto_procfs_status 'what' field is at offset 14. */
7778 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
7779 {
7780 elf_tdata (abfd)->core_signal = sig;
7781 elf_tdata (abfd)->core_lwpid = *tid;
7782 }
7783
7784 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
7785 do not come from signals so we make sure we set the current
7786 thread just in case. */
7787 if (flags & 0x00000080)
7788 elf_tdata (abfd)->core_lwpid = *tid;
7789
7790 /* Make a ".qnx_core_status/%d" section. */
7791 sprintf (buf, ".qnx_core_status/%ld", *tid);
7792
7793 name = bfd_alloc (abfd, strlen (buf) + 1);
7794 if (name == NULL)
7795 return FALSE;
7796 strcpy (name, buf);
7797
7798 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7799 if (sect == NULL)
7800 return FALSE;
7801
7802 sect->size = note->descsz;
7803 sect->filepos = note->descpos;
7804 sect->alignment_power = 2;
7805
7806 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
7807 }
7808
7809 static bfd_boolean
7810 elfcore_grok_nto_regs (bfd *abfd,
7811 Elf_Internal_Note *note,
7812 long tid,
7813 char *base)
7814 {
7815 char buf[100];
7816 char *name;
7817 asection *sect;
7818
7819 /* Make a "(base)/%d" section. */
7820 sprintf (buf, "%s/%ld", base, tid);
7821
7822 name = bfd_alloc (abfd, strlen (buf) + 1);
7823 if (name == NULL)
7824 return FALSE;
7825 strcpy (name, buf);
7826
7827 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7828 if (sect == NULL)
7829 return FALSE;
7830
7831 sect->size = note->descsz;
7832 sect->filepos = note->descpos;
7833 sect->alignment_power = 2;
7834
7835 /* This is the current thread. */
7836 if (elf_tdata (abfd)->core_lwpid == tid)
7837 return elfcore_maybe_make_sect (abfd, base, sect);
7838
7839 return TRUE;
7840 }
7841
7842 #define BFD_QNT_CORE_INFO 7
7843 #define BFD_QNT_CORE_STATUS 8
7844 #define BFD_QNT_CORE_GREG 9
7845 #define BFD_QNT_CORE_FPREG 10
7846
7847 static bfd_boolean
7848 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
7849 {
7850 /* Every GREG section has a STATUS section before it. Store the
7851 tid from the previous call to pass down to the next gregs
7852 function. */
7853 static long tid = 1;
7854
7855 switch (note->type)
7856 {
7857 case BFD_QNT_CORE_INFO:
7858 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
7859 case BFD_QNT_CORE_STATUS:
7860 return elfcore_grok_nto_status (abfd, note, &tid);
7861 case BFD_QNT_CORE_GREG:
7862 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
7863 case BFD_QNT_CORE_FPREG:
7864 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
7865 default:
7866 return TRUE;
7867 }
7868 }
7869
7870 /* Function: elfcore_write_note
7871
7872 Inputs:
7873 buffer to hold note, and current size of buffer
7874 name of note
7875 type of note
7876 data for note
7877 size of data for note
7878
7879 Writes note to end of buffer. ELF64 notes are written exactly as
7880 for ELF32, despite the current (as of 2006) ELF gabi specifying
7881 that they ought to have 8-byte namesz and descsz field, and have
7882 8-byte alignment. Other writers, eg. Linux kernel, do the same.
7883
7884 Return:
7885 Pointer to realloc'd buffer, *BUFSIZ updated. */
7886
7887 char *
7888 elfcore_write_note (bfd *abfd,
7889 char *buf,
7890 int *bufsiz,
7891 const char *name,
7892 int type,
7893 const void *input,
7894 int size)
7895 {
7896 Elf_External_Note *xnp;
7897 size_t namesz;
7898 size_t newspace;
7899 char *dest;
7900
7901 namesz = 0;
7902 if (name != NULL)
7903 namesz = strlen (name) + 1;
7904
7905 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
7906
7907 buf = realloc (buf, *bufsiz + newspace);
7908 dest = buf + *bufsiz;
7909 *bufsiz += newspace;
7910 xnp = (Elf_External_Note *) dest;
7911 H_PUT_32 (abfd, namesz, xnp->namesz);
7912 H_PUT_32 (abfd, size, xnp->descsz);
7913 H_PUT_32 (abfd, type, xnp->type);
7914 dest = xnp->name;
7915 if (name != NULL)
7916 {
7917 memcpy (dest, name, namesz);
7918 dest += namesz;
7919 while (namesz & 3)
7920 {
7921 *dest++ = '\0';
7922 ++namesz;
7923 }
7924 }
7925 memcpy (dest, input, size);
7926 dest += size;
7927 while (size & 3)
7928 {
7929 *dest++ = '\0';
7930 ++size;
7931 }
7932 return buf;
7933 }
7934
7935 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7936 char *
7937 elfcore_write_prpsinfo (bfd *abfd,
7938 char *buf,
7939 int *bufsiz,
7940 const char *fname,
7941 const char *psargs)
7942 {
7943 const char *note_name = "CORE";
7944 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7945
7946 if (bed->elf_backend_write_core_note != NULL)
7947 {
7948 char *ret;
7949 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
7950 NT_PRPSINFO, fname, psargs);
7951 if (ret != NULL)
7952 return ret;
7953 }
7954
7955 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7956 if (bed->s->elfclass == ELFCLASS32)
7957 {
7958 #if defined (HAVE_PSINFO32_T)
7959 psinfo32_t data;
7960 int note_type = NT_PSINFO;
7961 #else
7962 prpsinfo32_t data;
7963 int note_type = NT_PRPSINFO;
7964 #endif
7965
7966 memset (&data, 0, sizeof (data));
7967 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7968 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
7969 return elfcore_write_note (abfd, buf, bufsiz,
7970 note_name, note_type, &data, sizeof (data));
7971 }
7972 else
7973 #endif
7974 {
7975 #if defined (HAVE_PSINFO_T)
7976 psinfo_t data;
7977 int note_type = NT_PSINFO;
7978 #else
7979 prpsinfo_t data;
7980 int note_type = NT_PRPSINFO;
7981 #endif
7982
7983 memset (&data, 0, sizeof (data));
7984 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7985 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
7986 return elfcore_write_note (abfd, buf, bufsiz,
7987 note_name, note_type, &data, sizeof (data));
7988 }
7989 }
7990 #endif /* PSINFO_T or PRPSINFO_T */
7991
7992 #if defined (HAVE_PRSTATUS_T)
7993 char *
7994 elfcore_write_prstatus (bfd *abfd,
7995 char *buf,
7996 int *bufsiz,
7997 long pid,
7998 int cursig,
7999 const void *gregs)
8000 {
8001 const char *note_name = "CORE";
8002 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8003
8004 if (bed->elf_backend_write_core_note != NULL)
8005 {
8006 char *ret;
8007 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8008 NT_PRSTATUS,
8009 pid, cursig, gregs);
8010 if (ret != NULL)
8011 return ret;
8012 }
8013
8014 #if defined (HAVE_PRSTATUS32_T)
8015 if (bed->s->elfclass == ELFCLASS32)
8016 {
8017 prstatus32_t prstat;
8018
8019 memset (&prstat, 0, sizeof (prstat));
8020 prstat.pr_pid = pid;
8021 prstat.pr_cursig = cursig;
8022 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8023 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8024 NT_PRSTATUS, &prstat, sizeof (prstat));
8025 }
8026 else
8027 #endif
8028 {
8029 prstatus_t prstat;
8030
8031 memset (&prstat, 0, sizeof (prstat));
8032 prstat.pr_pid = pid;
8033 prstat.pr_cursig = cursig;
8034 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8035 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8036 NT_PRSTATUS, &prstat, sizeof (prstat));
8037 }
8038 }
8039 #endif /* HAVE_PRSTATUS_T */
8040
8041 #if defined (HAVE_LWPSTATUS_T)
8042 char *
8043 elfcore_write_lwpstatus (bfd *abfd,
8044 char *buf,
8045 int *bufsiz,
8046 long pid,
8047 int cursig,
8048 const void *gregs)
8049 {
8050 lwpstatus_t lwpstat;
8051 const char *note_name = "CORE";
8052
8053 memset (&lwpstat, 0, sizeof (lwpstat));
8054 lwpstat.pr_lwpid = pid >> 16;
8055 lwpstat.pr_cursig = cursig;
8056 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8057 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8058 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8059 #if !defined(gregs)
8060 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8061 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8062 #else
8063 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8064 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8065 #endif
8066 #endif
8067 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8068 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8069 }
8070 #endif /* HAVE_LWPSTATUS_T */
8071
8072 #if defined (HAVE_PSTATUS_T)
8073 char *
8074 elfcore_write_pstatus (bfd *abfd,
8075 char *buf,
8076 int *bufsiz,
8077 long pid,
8078 int cursig ATTRIBUTE_UNUSED,
8079 const void *gregs ATTRIBUTE_UNUSED)
8080 {
8081 const char *note_name = "CORE";
8082 #if defined (HAVE_PSTATUS32_T)
8083 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8084
8085 if (bed->s->elfclass == ELFCLASS32)
8086 {
8087 pstatus32_t pstat;
8088
8089 memset (&pstat, 0, sizeof (pstat));
8090 pstat.pr_pid = pid & 0xffff;
8091 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8092 NT_PSTATUS, &pstat, sizeof (pstat));
8093 return buf;
8094 }
8095 else
8096 #endif
8097 {
8098 pstatus_t pstat;
8099
8100 memset (&pstat, 0, sizeof (pstat));
8101 pstat.pr_pid = pid & 0xffff;
8102 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8103 NT_PSTATUS, &pstat, sizeof (pstat));
8104 return buf;
8105 }
8106 }
8107 #endif /* HAVE_PSTATUS_T */
8108
8109 char *
8110 elfcore_write_prfpreg (bfd *abfd,
8111 char *buf,
8112 int *bufsiz,
8113 const void *fpregs,
8114 int size)
8115 {
8116 const char *note_name = "CORE";
8117 return elfcore_write_note (abfd, buf, bufsiz,
8118 note_name, NT_FPREGSET, fpregs, size);
8119 }
8120
8121 char *
8122 elfcore_write_prxfpreg (bfd *abfd,
8123 char *buf,
8124 int *bufsiz,
8125 const void *xfpregs,
8126 int size)
8127 {
8128 char *note_name = "LINUX";
8129 return elfcore_write_note (abfd, buf, bufsiz,
8130 note_name, NT_PRXFPREG, xfpregs, size);
8131 }
8132
8133 static bfd_boolean
8134 elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8135 {
8136 char *buf;
8137 char *p;
8138
8139 if (size <= 0)
8140 return TRUE;
8141
8142 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8143 return FALSE;
8144
8145 buf = bfd_malloc (size);
8146 if (buf == NULL)
8147 return FALSE;
8148
8149 if (bfd_bread (buf, size, abfd) != size)
8150 {
8151 error:
8152 free (buf);
8153 return FALSE;
8154 }
8155
8156 p = buf;
8157 while (p < buf + size)
8158 {
8159 /* FIXME: bad alignment assumption. */
8160 Elf_External_Note *xnp = (Elf_External_Note *) p;
8161 Elf_Internal_Note in;
8162
8163 in.type = H_GET_32 (abfd, xnp->type);
8164
8165 in.namesz = H_GET_32 (abfd, xnp->namesz);
8166 in.namedata = xnp->name;
8167
8168 in.descsz = H_GET_32 (abfd, xnp->descsz);
8169 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8170 in.descpos = offset + (in.descdata - buf);
8171
8172 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8173 {
8174 if (! elfcore_grok_netbsd_note (abfd, &in))
8175 goto error;
8176 }
8177 else if (CONST_STRNEQ (in.namedata, "QNX"))
8178 {
8179 if (! elfcore_grok_nto_note (abfd, &in))
8180 goto error;
8181 }
8182 else
8183 {
8184 if (! elfcore_grok_note (abfd, &in))
8185 goto error;
8186 }
8187
8188 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8189 }
8190
8191 free (buf);
8192 return TRUE;
8193 }
8194 \f
8195 /* Providing external access to the ELF program header table. */
8196
8197 /* Return an upper bound on the number of bytes required to store a
8198 copy of ABFD's program header table entries. Return -1 if an error
8199 occurs; bfd_get_error will return an appropriate code. */
8200
8201 long
8202 bfd_get_elf_phdr_upper_bound (bfd *abfd)
8203 {
8204 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8205 {
8206 bfd_set_error (bfd_error_wrong_format);
8207 return -1;
8208 }
8209
8210 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
8211 }
8212
8213 /* Copy ABFD's program header table entries to *PHDRS. The entries
8214 will be stored as an array of Elf_Internal_Phdr structures, as
8215 defined in include/elf/internal.h. To find out how large the
8216 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8217
8218 Return the number of program header table entries read, or -1 if an
8219 error occurs; bfd_get_error will return an appropriate code. */
8220
8221 int
8222 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
8223 {
8224 int num_phdrs;
8225
8226 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8227 {
8228 bfd_set_error (bfd_error_wrong_format);
8229 return -1;
8230 }
8231
8232 num_phdrs = elf_elfheader (abfd)->e_phnum;
8233 memcpy (phdrs, elf_tdata (abfd)->phdr,
8234 num_phdrs * sizeof (Elf_Internal_Phdr));
8235
8236 return num_phdrs;
8237 }
8238
8239 void
8240 _bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
8241 {
8242 #ifdef BFD64
8243 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
8244
8245 i_ehdrp = elf_elfheader (abfd);
8246 if (i_ehdrp == NULL)
8247 sprintf_vma (buf, value);
8248 else
8249 {
8250 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
8251 {
8252 #if BFD_HOST_64BIT_LONG
8253 sprintf (buf, "%016lx", value);
8254 #else
8255 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
8256 _bfd_int64_low (value));
8257 #endif
8258 }
8259 else
8260 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
8261 }
8262 #else
8263 sprintf_vma (buf, value);
8264 #endif
8265 }
8266
8267 void
8268 _bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
8269 {
8270 #ifdef BFD64
8271 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
8272
8273 i_ehdrp = elf_elfheader (abfd);
8274 if (i_ehdrp == NULL)
8275 fprintf_vma ((FILE *) stream, value);
8276 else
8277 {
8278 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
8279 {
8280 #if BFD_HOST_64BIT_LONG
8281 fprintf ((FILE *) stream, "%016lx", value);
8282 #else
8283 fprintf ((FILE *) stream, "%08lx%08lx",
8284 _bfd_int64_high (value), _bfd_int64_low (value));
8285 #endif
8286 }
8287 else
8288 fprintf ((FILE *) stream, "%08lx",
8289 (unsigned long) (value & 0xffffffff));
8290 }
8291 #else
8292 fprintf_vma ((FILE *) stream, value);
8293 #endif
8294 }
8295
8296 enum elf_reloc_type_class
8297 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
8298 {
8299 return reloc_class_normal;
8300 }
8301
8302 /* For RELA architectures, return the relocation value for a
8303 relocation against a local symbol. */
8304
8305 bfd_vma
8306 _bfd_elf_rela_local_sym (bfd *abfd,
8307 Elf_Internal_Sym *sym,
8308 asection **psec,
8309 Elf_Internal_Rela *rel)
8310 {
8311 asection *sec = *psec;
8312 bfd_vma relocation;
8313
8314 relocation = (sec->output_section->vma
8315 + sec->output_offset
8316 + sym->st_value);
8317 if ((sec->flags & SEC_MERGE)
8318 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
8319 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
8320 {
8321 rel->r_addend =
8322 _bfd_merged_section_offset (abfd, psec,
8323 elf_section_data (sec)->sec_info,
8324 sym->st_value + rel->r_addend);
8325 if (sec != *psec)
8326 {
8327 /* If we have changed the section, and our original section is
8328 marked with SEC_EXCLUDE, it means that the original
8329 SEC_MERGE section has been completely subsumed in some
8330 other SEC_MERGE section. In this case, we need to leave
8331 some info around for --emit-relocs. */
8332 if ((sec->flags & SEC_EXCLUDE) != 0)
8333 sec->kept_section = *psec;
8334 sec = *psec;
8335 }
8336 rel->r_addend -= relocation;
8337 rel->r_addend += sec->output_section->vma + sec->output_offset;
8338 }
8339 return relocation;
8340 }
8341
8342 bfd_vma
8343 _bfd_elf_rel_local_sym (bfd *abfd,
8344 Elf_Internal_Sym *sym,
8345 asection **psec,
8346 bfd_vma addend)
8347 {
8348 asection *sec = *psec;
8349
8350 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
8351 return sym->st_value + addend;
8352
8353 return _bfd_merged_section_offset (abfd, psec,
8354 elf_section_data (sec)->sec_info,
8355 sym->st_value + addend);
8356 }
8357
8358 bfd_vma
8359 _bfd_elf_section_offset (bfd *abfd,
8360 struct bfd_link_info *info,
8361 asection *sec,
8362 bfd_vma offset)
8363 {
8364 switch (sec->sec_info_type)
8365 {
8366 case ELF_INFO_TYPE_STABS:
8367 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8368 offset);
8369 case ELF_INFO_TYPE_EH_FRAME:
8370 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
8371 default:
8372 return offset;
8373 }
8374 }
8375 \f
8376 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8377 reconstruct an ELF file by reading the segments out of remote memory
8378 based on the ELF file header at EHDR_VMA and the ELF program headers it
8379 points to. If not null, *LOADBASEP is filled in with the difference
8380 between the VMAs from which the segments were read, and the VMAs the
8381 file headers (and hence BFD's idea of each section's VMA) put them at.
8382
8383 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8384 remote memory at target address VMA into the local buffer at MYADDR; it
8385 should return zero on success or an `errno' code on failure. TEMPL must
8386 be a BFD for an ELF target with the word size and byte order found in
8387 the remote memory. */
8388
8389 bfd *
8390 bfd_elf_bfd_from_remote_memory
8391 (bfd *templ,
8392 bfd_vma ehdr_vma,
8393 bfd_vma *loadbasep,
8394 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
8395 {
8396 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8397 (templ, ehdr_vma, loadbasep, target_read_memory);
8398 }
8399 \f
8400 long
8401 _bfd_elf_get_synthetic_symtab (bfd *abfd,
8402 long symcount ATTRIBUTE_UNUSED,
8403 asymbol **syms ATTRIBUTE_UNUSED,
8404 long dynsymcount,
8405 asymbol **dynsyms,
8406 asymbol **ret)
8407 {
8408 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8409 asection *relplt;
8410 asymbol *s;
8411 const char *relplt_name;
8412 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8413 arelent *p;
8414 long count, i, n;
8415 size_t size;
8416 Elf_Internal_Shdr *hdr;
8417 char *names;
8418 asection *plt;
8419
8420 *ret = NULL;
8421
8422 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8423 return 0;
8424
8425 if (dynsymcount <= 0)
8426 return 0;
8427
8428 if (!bed->plt_sym_val)
8429 return 0;
8430
8431 relplt_name = bed->relplt_name;
8432 if (relplt_name == NULL)
8433 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
8434 relplt = bfd_get_section_by_name (abfd, relplt_name);
8435 if (relplt == NULL)
8436 return 0;
8437
8438 hdr = &elf_section_data (relplt)->this_hdr;
8439 if (hdr->sh_link != elf_dynsymtab (abfd)
8440 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8441 return 0;
8442
8443 plt = bfd_get_section_by_name (abfd, ".plt");
8444 if (plt == NULL)
8445 return 0;
8446
8447 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8448 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
8449 return -1;
8450
8451 count = relplt->size / hdr->sh_entsize;
8452 size = count * sizeof (asymbol);
8453 p = relplt->relocation;
8454 for (i = 0; i < count; i++, p++)
8455 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8456
8457 s = *ret = bfd_malloc (size);
8458 if (s == NULL)
8459 return -1;
8460
8461 names = (char *) (s + count);
8462 p = relplt->relocation;
8463 n = 0;
8464 for (i = 0; i < count; i++, s++, p++)
8465 {
8466 size_t len;
8467 bfd_vma addr;
8468
8469 addr = bed->plt_sym_val (i, plt, p);
8470 if (addr == (bfd_vma) -1)
8471 continue;
8472
8473 *s = **p->sym_ptr_ptr;
8474 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8475 we are defining a symbol, ensure one of them is set. */
8476 if ((s->flags & BSF_LOCAL) == 0)
8477 s->flags |= BSF_GLOBAL;
8478 s->section = plt;
8479 s->value = addr - plt->vma;
8480 s->name = names;
8481 len = strlen ((*p->sym_ptr_ptr)->name);
8482 memcpy (names, (*p->sym_ptr_ptr)->name, len);
8483 names += len;
8484 memcpy (names, "@plt", sizeof ("@plt"));
8485 names += sizeof ("@plt");
8486 ++n;
8487 }
8488
8489 return n;
8490 }
8491
8492 /* It is only used by x86-64 so far. */
8493 asection _bfd_elf_large_com_section
8494 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
8495 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
8496
8497 void
8498 _bfd_elf_set_osabi (bfd * abfd,
8499 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
8500 {
8501 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
8502
8503 i_ehdrp = elf_elfheader (abfd);
8504
8505 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
8506 }
8507
8508
8509 /* Return TRUE for ELF symbol types that represent functions.
8510 This is the default version of this function, which is sufficient for
8511 most targets. It returns true if TYPE is STT_FUNC. */
8512
8513 bfd_boolean
8514 _bfd_elf_is_function_type (unsigned int type)
8515 {
8516 return (type == STT_FUNC);
8517 }