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