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