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