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