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