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