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