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