* config/i386/tm-i386.h (MAX_NUM_REGS): Removed.
[deliverable/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
e1fddb6b 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
7898deda 3 Free Software Foundation, Inc.
252b5132 4
661a3fd4 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
661a3fd4
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
252b5132 11
661a3fd4
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
661a3fd4
NC
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
252b5132 20
661a3fd4
NC
21/* SECTION
22
252b5132
RH
23 ELF backends
24
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
28
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
661a3fd4 31 haven't bothered yet. */
252b5132 32
7ee38065
MS
33/* For sparc64-cross-sparc32. */
34#define _SYSCALL32
252b5132
RH
35#include "bfd.h"
36#include "sysdep.h"
37#include "bfdlink.h"
38#include "libbfd.h"
39#define ARCH_SIZE 0
40#include "elf-bfd.h"
e0e8c97f 41#include "libiberty.h"
252b5132
RH
42
43static INLINE struct elf_segment_map *make_mapping
44 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
45static boolean map_sections_to_segments PARAMS ((bfd *));
46static int elf_sort_sections PARAMS ((const PTR, const PTR));
47static boolean assign_file_positions_for_segments PARAMS ((bfd *));
48static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
49static boolean prep_headers PARAMS ((bfd *));
50static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
51static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
dc810e39 52static char *elf_read PARAMS ((bfd *, file_ptr, bfd_size_type));
b885599b 53static const char *group_signature PARAMS ((bfd *, Elf_Internal_Shdr *));
dbb410c3 54static boolean setup_group PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
d3c456e9 55static void merge_sections_remove_hook PARAMS ((bfd *, asection *));
252b5132
RH
56static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
57static boolean assign_section_numbers PARAMS ((bfd *));
58static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
59static boolean elf_map_symbols PARAMS ((bfd *));
60static bfd_size_type get_program_header_size PARAMS ((bfd *));
dc810e39 61static boolean elfcore_read_notes PARAMS ((bfd *, file_ptr, bfd_size_type));
a7b97311
AM
62static boolean elf_find_function PARAMS ((bfd *, asection *, asymbol **,
63 bfd_vma, const char **,
64 const char **));
65static int elfcore_make_pid PARAMS ((bfd *));
66static boolean elfcore_maybe_make_sect PARAMS ((bfd *, char *, asection *));
67static boolean elfcore_make_note_pseudosection PARAMS ((bfd *, char *,
68 Elf_Internal_Note *));
69static boolean elfcore_grok_prfpreg PARAMS ((bfd *, Elf_Internal_Note *));
70static boolean elfcore_grok_prxfpreg PARAMS ((bfd *, Elf_Internal_Note *));
71static boolean elfcore_grok_note PARAMS ((bfd *, Elf_Internal_Note *));
252b5132 72
50b2bdb7
AM
73static boolean elfcore_netbsd_get_lwpid PARAMS ((Elf_Internal_Note *, int *));
74static boolean elfcore_grok_netbsd_procinfo PARAMS ((bfd *,
75 Elf_Internal_Note *));
76static boolean elfcore_grok_netbsd_note PARAMS ((bfd *, Elf_Internal_Note *));
77
252b5132
RH
78/* Swap version information in and out. The version information is
79 currently size independent. If that ever changes, this code will
80 need to move into elfcode.h. */
81
82/* Swap in a Verdef structure. */
83
84void
85_bfd_elf_swap_verdef_in (abfd, src, dst)
86 bfd *abfd;
87 const Elf_External_Verdef *src;
88 Elf_Internal_Verdef *dst;
89{
dc810e39
AM
90 dst->vd_version = H_GET_16 (abfd, src->vd_version);
91 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
92 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
93 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
94 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
95 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
96 dst->vd_next = H_GET_32 (abfd, src->vd_next);
252b5132
RH
97}
98
99/* Swap out a Verdef structure. */
100
101void
102_bfd_elf_swap_verdef_out (abfd, src, dst)
103 bfd *abfd;
104 const Elf_Internal_Verdef *src;
105 Elf_External_Verdef *dst;
106{
dc810e39
AM
107 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
108 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
109 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
110 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
111 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
112 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
113 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
252b5132
RH
114}
115
116/* Swap in a Verdaux structure. */
117
118void
119_bfd_elf_swap_verdaux_in (abfd, src, dst)
120 bfd *abfd;
121 const Elf_External_Verdaux *src;
122 Elf_Internal_Verdaux *dst;
123{
dc810e39
AM
124 dst->vda_name = H_GET_32 (abfd, src->vda_name);
125 dst->vda_next = H_GET_32 (abfd, src->vda_next);
252b5132
RH
126}
127
128/* Swap out a Verdaux structure. */
129
130void
131_bfd_elf_swap_verdaux_out (abfd, src, dst)
132 bfd *abfd;
133 const Elf_Internal_Verdaux *src;
134 Elf_External_Verdaux *dst;
135{
dc810e39
AM
136 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
137 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
252b5132
RH
138}
139
140/* Swap in a Verneed structure. */
141
142void
143_bfd_elf_swap_verneed_in (abfd, src, dst)
144 bfd *abfd;
145 const Elf_External_Verneed *src;
146 Elf_Internal_Verneed *dst;
147{
dc810e39
AM
148 dst->vn_version = H_GET_16 (abfd, src->vn_version);
149 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
150 dst->vn_file = H_GET_32 (abfd, src->vn_file);
151 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
152 dst->vn_next = H_GET_32 (abfd, src->vn_next);
252b5132
RH
153}
154
155/* Swap out a Verneed structure. */
156
157void
158_bfd_elf_swap_verneed_out (abfd, src, dst)
159 bfd *abfd;
160 const Elf_Internal_Verneed *src;
161 Elf_External_Verneed *dst;
162{
dc810e39
AM
163 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
164 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
165 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
166 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
167 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
252b5132
RH
168}
169
170/* Swap in a Vernaux structure. */
171
172void
173_bfd_elf_swap_vernaux_in (abfd, src, dst)
174 bfd *abfd;
175 const Elf_External_Vernaux *src;
176 Elf_Internal_Vernaux *dst;
177{
dc810e39
AM
178 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
179 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
180 dst->vna_other = H_GET_16 (abfd, src->vna_other);
181 dst->vna_name = H_GET_32 (abfd, src->vna_name);
182 dst->vna_next = H_GET_32 (abfd, src->vna_next);
252b5132
RH
183}
184
185/* Swap out a Vernaux structure. */
186
187void
188_bfd_elf_swap_vernaux_out (abfd, src, dst)
189 bfd *abfd;
190 const Elf_Internal_Vernaux *src;
191 Elf_External_Vernaux *dst;
192{
dc810e39
AM
193 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
194 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
195 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
196 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
197 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
252b5132
RH
198}
199
200/* Swap in a Versym structure. */
201
202void
203_bfd_elf_swap_versym_in (abfd, src, dst)
204 bfd *abfd;
205 const Elf_External_Versym *src;
206 Elf_Internal_Versym *dst;
207{
dc810e39 208 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252b5132
RH
209}
210
211/* Swap out a Versym structure. */
212
213void
214_bfd_elf_swap_versym_out (abfd, src, dst)
215 bfd *abfd;
216 const Elf_Internal_Versym *src;
217 Elf_External_Versym *dst;
218{
dc810e39 219 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
252b5132
RH
220}
221
222/* Standard ELF hash function. Do not change this function; you will
223 cause invalid hash tables to be generated. */
3a99b017 224
252b5132 225unsigned long
3a99b017
ILT
226bfd_elf_hash (namearg)
227 const char *namearg;
252b5132 228{
3a99b017 229 const unsigned char *name = (const unsigned char *) namearg;
252b5132
RH
230 unsigned long h = 0;
231 unsigned long g;
232 int ch;
233
234 while ((ch = *name++) != '\0')
235 {
236 h = (h << 4) + ch;
237 if ((g = (h & 0xf0000000)) != 0)
238 {
239 h ^= g >> 24;
240 /* The ELF ABI says `h &= ~g', but this is equivalent in
241 this case and on some machines one insn instead of two. */
242 h ^= g;
243 }
244 }
245 return h;
246}
247
248/* Read a specified number of bytes at a specified offset in an ELF
249 file, into a newly allocated buffer, and return a pointer to the
c044fabd 250 buffer. */
252b5132
RH
251
252static char *
253elf_read (abfd, offset, size)
c044fabd 254 bfd *abfd;
dc810e39
AM
255 file_ptr offset;
256 bfd_size_type size;
252b5132
RH
257{
258 char *buf;
259
260 if ((buf = bfd_alloc (abfd, size)) == NULL)
261 return NULL;
dc810e39 262 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
252b5132 263 return NULL;
dc810e39 264 if (bfd_bread ((PTR) buf, size, abfd) != size)
252b5132
RH
265 {
266 if (bfd_get_error () != bfd_error_system_call)
267 bfd_set_error (bfd_error_file_truncated);
268 return NULL;
269 }
270 return buf;
271}
272
273boolean
274bfd_elf_mkobject (abfd)
c044fabd 275 bfd *abfd;
252b5132 276{
c044fabd
KH
277 /* This just does initialization. */
278 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
dc810e39
AM
279 bfd_size_type amt = sizeof (struct elf_obj_tdata);
280 elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt);
252b5132
RH
281 if (elf_tdata (abfd) == 0)
282 return false;
c044fabd
KH
283 /* Since everything is done at close time, do we need any
284 initialization? */
252b5132
RH
285
286 return true;
287}
288
289boolean
290bfd_elf_mkcorefile (abfd)
c044fabd 291 bfd *abfd;
252b5132 292{
c044fabd 293 /* I think this can be done just like an object file. */
252b5132
RH
294 return bfd_elf_mkobject (abfd);
295}
296
297char *
298bfd_elf_get_str_section (abfd, shindex)
c044fabd 299 bfd *abfd;
252b5132
RH
300 unsigned int shindex;
301{
302 Elf_Internal_Shdr **i_shdrp;
303 char *shstrtab = NULL;
dc810e39
AM
304 file_ptr offset;
305 bfd_size_type shstrtabsize;
252b5132
RH
306
307 i_shdrp = elf_elfsections (abfd);
308 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
309 return 0;
310
311 shstrtab = (char *) i_shdrp[shindex]->contents;
312 if (shstrtab == NULL)
313 {
c044fabd 314 /* No cached one, attempt to read, and cache what we read. */
252b5132
RH
315 offset = i_shdrp[shindex]->sh_offset;
316 shstrtabsize = i_shdrp[shindex]->sh_size;
317 shstrtab = elf_read (abfd, offset, shstrtabsize);
318 i_shdrp[shindex]->contents = (PTR) shstrtab;
319 }
320 return shstrtab;
321}
322
323char *
324bfd_elf_string_from_elf_section (abfd, shindex, strindex)
c044fabd 325 bfd *abfd;
252b5132
RH
326 unsigned int shindex;
327 unsigned int strindex;
328{
329 Elf_Internal_Shdr *hdr;
330
331 if (strindex == 0)
332 return "";
333
334 hdr = elf_elfsections (abfd)[shindex];
335
336 if (hdr->contents == NULL
337 && bfd_elf_get_str_section (abfd, shindex) == NULL)
338 return NULL;
339
340 if (strindex >= hdr->sh_size)
341 {
342 (*_bfd_error_handler)
343 (_("%s: invalid string offset %u >= %lu for section `%s'"),
8f615d07 344 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
252b5132
RH
345 ((shindex == elf_elfheader(abfd)->e_shstrndx
346 && strindex == hdr->sh_name)
347 ? ".shstrtab"
348 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
349 return "";
350 }
351
352 return ((char *) hdr->contents) + strindex;
353}
354
dbb410c3
AM
355/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
356 sections. The first element is the flags, the rest are section
357 pointers. */
358
359typedef union elf_internal_group {
360 Elf_Internal_Shdr *shdr;
361 unsigned int flags;
362} Elf_Internal_Group;
363
b885599b
AM
364/* Return the name of the group signature symbol. Why isn't the
365 signature just a string? */
366
367static const char *
368group_signature (abfd, ghdr)
369 bfd *abfd;
370 Elf_Internal_Shdr *ghdr;
371{
372 struct elf_backend_data *bed;
373 file_ptr pos;
9dce4196
AM
374 bfd_size_type amt;
375 Elf_Internal_Shdr *hdr;
376 Elf_Internal_Shdr *shndx_hdr;
377 unsigned char esym[sizeof (Elf64_External_Sym)];
378 Elf_External_Sym_Shndx eshndx;
379 Elf_Internal_Sym isym;
380 unsigned int iname;
381 unsigned int shindex;
b885599b
AM
382
383 /* First we need to ensure the symbol table is available. */
384 if (! bfd_section_from_shdr (abfd, ghdr->sh_link))
385 return NULL;
386
9dce4196
AM
387 /* Go read the symbol. */
388 hdr = &elf_tdata (abfd)->symtab_hdr;
b885599b 389 bed = get_elf_backend_data (abfd);
9dce4196
AM
390 amt = bed->s->sizeof_sym;
391 pos = hdr->sh_offset + ghdr->sh_info * amt;
b885599b 392 if (bfd_seek (abfd, pos, SEEK_SET) != 0
9dce4196 393 || bfd_bread (esym, amt, abfd) != amt)
b885599b 394 return NULL;
9dce4196
AM
395
396 /* And possibly the symbol section index extension. */
397 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
398 if (elf_elfsections (abfd) != NULL
399 && elf_elfsections (abfd)[shndx_hdr->sh_link] == hdr)
400 {
401 amt = sizeof (Elf_External_Sym_Shndx);
402 pos = shndx_hdr->sh_offset + ghdr->sh_info * amt;
403 if (bfd_seek (abfd, pos, SEEK_SET) != 0
404 || bfd_bread ((PTR) &eshndx, amt, abfd) != amt)
405 return NULL;
406 }
407
408 /* Convert to internal format. */
409 (*bed->s->swap_symbol_in) (abfd, (const PTR *) &esym, (const PTR *) &eshndx,
410 &isym);
411
412 /* Look up the symbol name. */
413 iname = isym.st_name;
414 shindex = hdr->sh_link;
415 if (iname == 0 && ELF_ST_TYPE (isym.st_info) == STT_SECTION)
416 {
417 iname = elf_elfsections (abfd)[isym.st_shndx]->sh_name;
418 shindex = elf_elfheader (abfd)->e_shstrndx;
419 }
420
421 return bfd_elf_string_from_elf_section (abfd, shindex, iname);
b885599b
AM
422}
423
dbb410c3
AM
424/* Set next_in_group list pointer, and group name for NEWSECT. */
425
426static boolean
427setup_group (abfd, hdr, newsect)
428 bfd *abfd;
429 Elf_Internal_Shdr *hdr;
430 asection *newsect;
431{
432 unsigned int num_group = elf_tdata (abfd)->num_group;
433
434 /* If num_group is zero, read in all SHT_GROUP sections. The count
435 is set to -1 if there are no SHT_GROUP sections. */
436 if (num_group == 0)
437 {
438 unsigned int i, shnum;
439
440 /* First count the number of groups. If we have a SHT_GROUP
441 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 442 shnum = elf_numsections (abfd);
dbb410c3
AM
443 num_group = 0;
444 for (i = 0; i < shnum; i++)
445 {
446 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
447 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
448 num_group += 1;
449 }
450
451 if (num_group == 0)
973ffd63 452 num_group = (unsigned) -1;
dbb410c3
AM
453 elf_tdata (abfd)->num_group = num_group;
454
455 if (num_group > 0)
456 {
457 /* We keep a list of elf section headers for group sections,
458 so we can find them quickly. */
459 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
460 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
461 if (elf_tdata (abfd)->group_sect_ptr == NULL)
462 return false;
463
464 num_group = 0;
465 for (i = 0; i < shnum; i++)
466 {
467 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
468 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
469 {
973ffd63 470 unsigned char *src;
dbb410c3
AM
471 Elf_Internal_Group *dest;
472
473 /* Add to list of sections. */
474 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
475 num_group += 1;
476
477 /* Read the raw contents. */
478 BFD_ASSERT (sizeof (*dest) >= 4);
479 amt = shdr->sh_size * sizeof (*dest) / 4;
480 shdr->contents = bfd_alloc (abfd, amt);
481 if (shdr->contents == NULL
482 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
483 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
484 != shdr->sh_size))
485 return false;
486
487 /* Translate raw contents, a flag word followed by an
488 array of elf section indices all in target byte order,
489 to the flag word followed by an array of elf section
490 pointers. */
491 src = shdr->contents + shdr->sh_size;
492 dest = (Elf_Internal_Group *) (shdr->contents + amt);
493 while (1)
494 {
495 unsigned int idx;
496
497 src -= 4;
498 --dest;
499 idx = H_GET_32 (abfd, src);
500 if (src == shdr->contents)
501 {
502 dest->flags = idx;
b885599b
AM
503 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
504 shdr->bfd_section->flags
505 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
dbb410c3
AM
506 break;
507 }
508 if (idx >= shnum)
509 {
510 ((*_bfd_error_handler)
511 (_("%s: invalid SHT_GROUP entry"),
512 bfd_archive_filename (abfd)));
513 idx = 0;
514 }
515 dest->shdr = elf_elfsections (abfd)[idx];
516 }
517 }
518 }
519 }
520 }
521
522 if (num_group != (unsigned) -1)
523 {
524 unsigned int i;
525
526 for (i = 0; i < num_group; i++)
527 {
528 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
529 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
530 unsigned int n_elt = shdr->sh_size / 4;
531
532 /* Look through this group's sections to see if current
533 section is a member. */
534 while (--n_elt != 0)
535 if ((++idx)->shdr == hdr)
536 {
e0e8c97f 537 asection *s = NULL;
dbb410c3
AM
538
539 /* We are a member of this group. Go looking through
540 other members to see if any others are linked via
541 next_in_group. */
542 idx = (Elf_Internal_Group *) shdr->contents;
543 n_elt = shdr->sh_size / 4;
544 while (--n_elt != 0)
545 if ((s = (++idx)->shdr->bfd_section) != NULL
945906ff 546 && elf_next_in_group (s) != NULL)
dbb410c3
AM
547 break;
548 if (n_elt != 0)
549 {
dbb410c3
AM
550 /* Snarf the group name from other member, and
551 insert current section in circular list. */
945906ff
AM
552 elf_group_name (newsect) = elf_group_name (s);
553 elf_next_in_group (newsect) = elf_next_in_group (s);
554 elf_next_in_group (s) = newsect;
dbb410c3
AM
555 }
556 else
557 {
dbb410c3
AM
558 const char *gname;
559
b885599b
AM
560 gname = group_signature (abfd, shdr);
561 if (gname == NULL)
dbb410c3 562 return false;
945906ff 563 elf_group_name (newsect) = gname;
dbb410c3
AM
564
565 /* Start a circular list with one element. */
945906ff 566 elf_next_in_group (newsect) = newsect;
dbb410c3 567 }
b885599b 568
9dce4196
AM
569 /* If the group section has been created, point to the
570 new member. */
dbb410c3 571 if (shdr->bfd_section != NULL)
945906ff 572 elf_next_in_group (shdr->bfd_section) = newsect;
b885599b 573
dbb410c3
AM
574 i = num_group - 1;
575 break;
576 }
577 }
578 }
579
945906ff 580 if (elf_group_name (newsect) == NULL)
dbb410c3
AM
581 {
582 (*_bfd_error_handler) (_("%s: no group info for section %s"),
583 bfd_archive_filename (abfd), newsect->name);
584 }
585 return true;
586}
587
e61463e1 588boolean
b885599b
AM
589bfd_elf_discard_group (abfd, group)
590 bfd *abfd ATTRIBUTE_UNUSED;
591 asection *group;
592{
593 asection *first = elf_next_in_group (group);
594 asection *s = first;
595
596 while (s != NULL)
597 {
598 s->output_section = bfd_abs_section_ptr;
599 s = elf_next_in_group (s);
600 /* These lists are circular. */
601 if (s == first)
602 break;
603 }
e61463e1 604 return true;
b885599b
AM
605}
606
252b5132
RH
607/* Make a BFD section from an ELF section. We store a pointer to the
608 BFD section in the bfd_section field of the header. */
609
610boolean
611_bfd_elf_make_section_from_shdr (abfd, hdr, name)
612 bfd *abfd;
613 Elf_Internal_Shdr *hdr;
614 const char *name;
615{
616 asection *newsect;
617 flagword flags;
fa152c49 618 struct elf_backend_data *bed;
252b5132
RH
619
620 if (hdr->bfd_section != NULL)
621 {
622 BFD_ASSERT (strcmp (name,
623 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
624 return true;
625 }
626
627 newsect = bfd_make_section_anyway (abfd, name);
628 if (newsect == NULL)
629 return false;
630
631 newsect->filepos = hdr->sh_offset;
632
633 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
634 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
635 || ! bfd_set_section_alignment (abfd, newsect,
dc810e39 636 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
252b5132
RH
637 return false;
638
639 flags = SEC_NO_FLAGS;
640 if (hdr->sh_type != SHT_NOBITS)
641 flags |= SEC_HAS_CONTENTS;
dbb410c3
AM
642 if (hdr->sh_type == SHT_GROUP)
643 flags |= SEC_GROUP | SEC_EXCLUDE;
252b5132
RH
644 if ((hdr->sh_flags & SHF_ALLOC) != 0)
645 {
646 flags |= SEC_ALLOC;
647 if (hdr->sh_type != SHT_NOBITS)
648 flags |= SEC_LOAD;
649 }
650 if ((hdr->sh_flags & SHF_WRITE) == 0)
651 flags |= SEC_READONLY;
652 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
653 flags |= SEC_CODE;
654 else if ((flags & SEC_LOAD) != 0)
655 flags |= SEC_DATA;
f5fa8ca2
JJ
656 if ((hdr->sh_flags & SHF_MERGE) != 0)
657 {
658 flags |= SEC_MERGE;
659 newsect->entsize = hdr->sh_entsize;
660 if ((hdr->sh_flags & SHF_STRINGS) != 0)
661 flags |= SEC_STRINGS;
662 }
dbb410c3
AM
663 if (hdr->sh_flags & SHF_GROUP)
664 if (!setup_group (abfd, hdr, newsect))
665 return false;
13ae64f3
JJ
666 if ((hdr->sh_flags & SHF_TLS) != 0)
667 flags |= SEC_THREAD_LOCAL;
252b5132
RH
668
669 /* The debugging sections appear to be recognized only by name, not
670 any sort of flag. */
7a6cc5fb 671 {
dbf48117 672 static const char *debug_sec_names [] =
7a6cc5fb
NC
673 {
674 ".debug",
675 ".gnu.linkonce.wi.",
676 ".line",
677 ".stab"
678 };
679 int i;
680
e0e8c97f 681 for (i = ARRAY_SIZE (debug_sec_names); i--;)
7a6cc5fb
NC
682 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
683 break;
684
685 if (i >= 0)
686 flags |= SEC_DEBUGGING;
687 }
252b5132
RH
688
689 /* As a GNU extension, if the name begins with .gnu.linkonce, we
690 only link a single copy of the section. This is used to support
691 g++. g++ will emit each template expansion in its own section.
692 The symbols will be defined as weak, so that multiple definitions
693 are permitted. The GNU linker extension is to actually discard
694 all but one of the sections. */
b885599b
AM
695 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
696 && elf_next_in_group (newsect) == NULL)
252b5132
RH
697 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
698
fa152c49
JW
699 bed = get_elf_backend_data (abfd);
700 if (bed->elf_backend_section_flags)
701 if (! bed->elf_backend_section_flags (&flags, hdr))
702 return false;
703
252b5132
RH
704 if (! bfd_set_section_flags (abfd, newsect, flags))
705 return false;
706
707 if ((flags & SEC_ALLOC) != 0)
708 {
709 Elf_Internal_Phdr *phdr;
710 unsigned int i;
711
712 /* Look through the phdrs to see if we need to adjust the lma.
713 If all the p_paddr fields are zero, we ignore them, since
714 some ELF linkers produce such output. */
715 phdr = elf_tdata (abfd)->phdr;
716 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
717 {
718 if (phdr->p_paddr != 0)
719 break;
720 }
721 if (i < elf_elfheader (abfd)->e_phnum)
722 {
723 phdr = elf_tdata (abfd)->phdr;
724 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
725 {
e0e8c97f
NC
726 /* This section is part of this segment if its file
727 offset plus size lies within the segment's memory
728 span and, if the section is loaded, the extent of the
729 loaded data lies within the extent of the segment.
bf36db18
NC
730
731 Note - we used to check the p_paddr field as well, and
732 refuse to set the LMA if it was 0. This is wrong
dba143ef 733 though, as a perfectly valid initialised segment can
bf36db18 734 have a p_paddr of zero. Some architectures, eg ARM,
dba143ef 735 place special significance on the address 0 and
bf36db18
NC
736 executables need to be able to have a segment which
737 covers this address. */
252b5132 738 if (phdr->p_type == PT_LOAD
e0e8c97f
NC
739 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
740 && (hdr->sh_offset + hdr->sh_size
741 <= phdr->p_offset + phdr->p_memsz)
252b5132 742 && ((flags & SEC_LOAD) == 0
d7866f04
AM
743 || (hdr->sh_offset + hdr->sh_size
744 <= phdr->p_offset + phdr->p_filesz)))
252b5132 745 {
dba143ef 746 if ((flags & SEC_LOAD) == 0)
d7866f04
AM
747 newsect->lma = (phdr->p_paddr
748 + hdr->sh_addr - phdr->p_vaddr);
dba143ef
AM
749 else
750 /* We used to use the same adjustment for SEC_LOAD
751 sections, but that doesn't work if the segment
752 is packed with code from multiple VMAs.
753 Instead we calculate the section LMA based on
754 the segment LMA. It is assumed that the
755 segment will contain sections with contiguous
756 LMAs, even if the VMAs are not. */
757 newsect->lma = (phdr->p_paddr
758 + hdr->sh_offset - phdr->p_offset);
d7866f04
AM
759
760 /* With contiguous segments, we can't tell from file
761 offsets whether a section with zero size should
762 be placed at the end of one segment or the
763 beginning of the next. Decide based on vaddr. */
764 if (hdr->sh_addr >= phdr->p_vaddr
765 && (hdr->sh_addr + hdr->sh_size
766 <= phdr->p_vaddr + phdr->p_memsz))
767 break;
252b5132
RH
768 }
769 }
770 }
771 }
772
773 hdr->bfd_section = newsect;
774 elf_section_data (newsect)->this_hdr = *hdr;
775
776 return true;
777}
778
779/*
780INTERNAL_FUNCTION
781 bfd_elf_find_section
782
783SYNOPSIS
784 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
785
786DESCRIPTION
787 Helper functions for GDB to locate the string tables.
788 Since BFD hides string tables from callers, GDB needs to use an
789 internal hook to find them. Sun's .stabstr, in particular,
790 isn't even pointed to by the .stab section, so ordinary
791 mechanisms wouldn't work to find it, even if we had some.
792*/
793
794struct elf_internal_shdr *
795bfd_elf_find_section (abfd, name)
c044fabd 796 bfd *abfd;
252b5132
RH
797 char *name;
798{
799 Elf_Internal_Shdr **i_shdrp;
800 char *shstrtab;
801 unsigned int max;
802 unsigned int i;
803
804 i_shdrp = elf_elfsections (abfd);
805 if (i_shdrp != NULL)
806 {
9ad5cbcf
AM
807 shstrtab = bfd_elf_get_str_section (abfd,
808 elf_elfheader (abfd)->e_shstrndx);
252b5132
RH
809 if (shstrtab != NULL)
810 {
9ad5cbcf 811 max = elf_numsections (abfd);
252b5132
RH
812 for (i = 1; i < max; i++)
813 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
814 return i_shdrp[i];
815 }
816 }
817 return 0;
818}
819
820const char *const bfd_elf_section_type_names[] = {
821 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
822 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
823 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
824};
825
826/* ELF relocs are against symbols. If we are producing relocateable
827 output, and the reloc is against an external symbol, and nothing
828 has given us any additional addend, the resulting reloc will also
829 be against the same symbol. In such a case, we don't want to
830 change anything about the way the reloc is handled, since it will
831 all be done at final link time. Rather than put special case code
832 into bfd_perform_relocation, all the reloc types use this howto
833 function. It just short circuits the reloc if producing
834 relocateable output against an external symbol. */
835
252b5132
RH
836bfd_reloc_status_type
837bfd_elf_generic_reloc (abfd,
838 reloc_entry,
839 symbol,
840 data,
841 input_section,
842 output_bfd,
843 error_message)
7442e600 844 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
845 arelent *reloc_entry;
846 asymbol *symbol;
7442e600 847 PTR data ATTRIBUTE_UNUSED;
252b5132
RH
848 asection *input_section;
849 bfd *output_bfd;
7442e600 850 char **error_message ATTRIBUTE_UNUSED;
252b5132
RH
851{
852 if (output_bfd != (bfd *) NULL
853 && (symbol->flags & BSF_SECTION_SYM) == 0
854 && (! reloc_entry->howto->partial_inplace
855 || reloc_entry->addend == 0))
856 {
857 reloc_entry->address += input_section->output_offset;
858 return bfd_reloc_ok;
859 }
860
861 return bfd_reloc_continue;
862}
863\f
d3c456e9
JJ
864/* Make sure sec_info_type is cleared if sec_info is cleared too. */
865
866static void
867merge_sections_remove_hook (abfd, sec)
868 bfd *abfd ATTRIBUTE_UNUSED;
869 asection *sec;
870{
871 struct bfd_elf_section_data *sec_data;
872
873 sec_data = elf_section_data (sec);
874 BFD_ASSERT (sec_data->sec_info_type == ELF_INFO_TYPE_MERGE);
875 sec_data->sec_info_type = ELF_INFO_TYPE_NONE;
876}
877
8550eb6e
JJ
878/* Finish SHF_MERGE section merging. */
879
880boolean
881_bfd_elf_merge_sections (abfd, info)
882 bfd *abfd;
883 struct bfd_link_info *info;
884{
b0f35f36 885 if (!is_elf_hash_table (info))
8ea2e4bd 886 return false;
b0f35f36 887 if (elf_hash_table (info)->merge_info)
d3c456e9
JJ
888 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info,
889 merge_sections_remove_hook);
8550eb6e
JJ
890 return true;
891}
2d653fc7
AM
892
893void
894_bfd_elf_link_just_syms (sec, info)
895 asection *sec;
896 struct bfd_link_info *info;
897{
898 sec->output_section = bfd_abs_section_ptr;
899 sec->output_offset = sec->vma;
900 if (!is_elf_hash_table (info))
901 return;
902
903 elf_section_data (sec)->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
904}
8550eb6e 905\f
0ac4564e
L
906/* Copy the program header and other data from one object module to
907 another. */
252b5132 908
2d502050
L
909boolean
910_bfd_elf_copy_private_bfd_data (ibfd, obfd)
911 bfd *ibfd;
912 bfd *obfd;
913{
914 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
915 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
916 return true;
917
918 BFD_ASSERT (!elf_flags_init (obfd)
919 || (elf_elfheader (obfd)->e_flags
920 == elf_elfheader (ibfd)->e_flags));
921
0ac4564e 922 elf_gp (obfd) = elf_gp (ibfd);
2d502050
L
923 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
924 elf_flags_init (obfd) = true;
925 return true;
926}
927
f0b79d91
L
928/* Print out the program headers. */
929
252b5132
RH
930boolean
931_bfd_elf_print_private_bfd_data (abfd, farg)
932 bfd *abfd;
933 PTR farg;
934{
935 FILE *f = (FILE *) farg;
936 Elf_Internal_Phdr *p;
937 asection *s;
938 bfd_byte *dynbuf = NULL;
939
940 p = elf_tdata (abfd)->phdr;
941 if (p != NULL)
942 {
943 unsigned int i, c;
944
945 fprintf (f, _("\nProgram Header:\n"));
946 c = elf_elfheader (abfd)->e_phnum;
947 for (i = 0; i < c; i++, p++)
948 {
dc810e39 949 const char *pt;
252b5132
RH
950 char buf[20];
951
952 switch (p->p_type)
953 {
dc810e39
AM
954 case PT_NULL: pt = "NULL"; break;
955 case PT_LOAD: pt = "LOAD"; break;
956 case PT_DYNAMIC: pt = "DYNAMIC"; break;
957 case PT_INTERP: pt = "INTERP"; break;
958 case PT_NOTE: pt = "NOTE"; break;
959 case PT_SHLIB: pt = "SHLIB"; break;
960 case PT_PHDR: pt = "PHDR"; break;
13ae64f3 961 case PT_TLS: pt = "TLS"; break;
65765700 962 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
dc810e39 963 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
252b5132 964 }
dc810e39 965 fprintf (f, "%8s off 0x", pt);
60b89a18 966 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 967 fprintf (f, " vaddr 0x");
60b89a18 968 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 969 fprintf (f, " paddr 0x");
60b89a18 970 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
971 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
972 fprintf (f, " filesz 0x");
60b89a18 973 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 974 fprintf (f, " memsz 0x");
60b89a18 975 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
976 fprintf (f, " flags %c%c%c",
977 (p->p_flags & PF_R) != 0 ? 'r' : '-',
978 (p->p_flags & PF_W) != 0 ? 'w' : '-',
979 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
980 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
981 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
982 fprintf (f, "\n");
983 }
984 }
985
986 s = bfd_get_section_by_name (abfd, ".dynamic");
987 if (s != NULL)
988 {
989 int elfsec;
dc810e39 990 unsigned long shlink;
252b5132
RH
991 bfd_byte *extdyn, *extdynend;
992 size_t extdynsize;
993 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
994
995 fprintf (f, _("\nDynamic Section:\n"));
996
997 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
998 if (dynbuf == NULL)
999 goto error_return;
1000 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1001 s->_raw_size))
1002 goto error_return;
1003
1004 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1005 if (elfsec == -1)
1006 goto error_return;
dc810e39 1007 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1008
1009 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1010 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1011
1012 extdyn = dynbuf;
1013 extdynend = extdyn + s->_raw_size;
1014 for (; extdyn < extdynend; extdyn += extdynsize)
1015 {
1016 Elf_Internal_Dyn dyn;
1017 const char *name;
1018 char ab[20];
1019 boolean stringp;
1020
1021 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1022
1023 if (dyn.d_tag == DT_NULL)
1024 break;
1025
1026 stringp = false;
1027 switch (dyn.d_tag)
1028 {
1029 default:
1030 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1031 name = ab;
1032 break;
1033
1034 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1035 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1036 case DT_PLTGOT: name = "PLTGOT"; break;
1037 case DT_HASH: name = "HASH"; break;
1038 case DT_STRTAB: name = "STRTAB"; break;
1039 case DT_SYMTAB: name = "SYMTAB"; break;
1040 case DT_RELA: name = "RELA"; break;
1041 case DT_RELASZ: name = "RELASZ"; break;
1042 case DT_RELAENT: name = "RELAENT"; break;
1043 case DT_STRSZ: name = "STRSZ"; break;
1044 case DT_SYMENT: name = "SYMENT"; break;
1045 case DT_INIT: name = "INIT"; break;
1046 case DT_FINI: name = "FINI"; break;
1047 case DT_SONAME: name = "SONAME"; stringp = true; break;
1048 case DT_RPATH: name = "RPATH"; stringp = true; break;
1049 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1050 case DT_REL: name = "REL"; break;
1051 case DT_RELSZ: name = "RELSZ"; break;
1052 case DT_RELENT: name = "RELENT"; break;
1053 case DT_PLTREL: name = "PLTREL"; break;
1054 case DT_DEBUG: name = "DEBUG"; break;
1055 case DT_TEXTREL: name = "TEXTREL"; break;
1056 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1057 case DT_BIND_NOW: name = "BIND_NOW"; break;
1058 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1059 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1060 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1061 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1062 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1063 case DT_FLAGS: name = "FLAGS"; break;
1064 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1065 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1066 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1067 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1068 case DT_MOVEENT: name = "MOVEENT"; break;
1069 case DT_MOVESZ: name = "MOVESZ"; break;
1070 case DT_FEATURE: name = "FEATURE"; break;
1071 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1072 case DT_SYMINSZ: name = "SYMINSZ"; break;
1073 case DT_SYMINENT: name = "SYMINENT"; break;
36a30e65
L
1074 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1075 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1076 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
94558834
L
1077 case DT_PLTPAD: name = "PLTPAD"; break;
1078 case DT_MOVETAB: name = "MOVETAB"; break;
1079 case DT_SYMINFO: name = "SYMINFO"; break;
1080 case DT_RELACOUNT: name = "RELACOUNT"; break;
1081 case DT_RELCOUNT: name = "RELCOUNT"; break;
1082 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1083 case DT_VERSYM: name = "VERSYM"; break;
1084 case DT_VERDEF: name = "VERDEF"; break;
1085 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1086 case DT_VERNEED: name = "VERNEED"; break;
1087 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
94558834
L
1088 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1089 case DT_USED: name = "USED"; break;
1090 case DT_FILTER: name = "FILTER"; stringp = true; break;
252b5132
RH
1091 }
1092
1093 fprintf (f, " %-11s ", name);
1094 if (! stringp)
1095 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1096 else
1097 {
1098 const char *string;
dc810e39 1099 unsigned int tagv = dyn.d_un.d_val;
252b5132 1100
dc810e39 1101 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1102 if (string == NULL)
1103 goto error_return;
1104 fprintf (f, "%s", string);
1105 }
1106 fprintf (f, "\n");
1107 }
1108
1109 free (dynbuf);
1110 dynbuf = NULL;
1111 }
1112
1113 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1114 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1115 {
1116 if (! _bfd_elf_slurp_version_tables (abfd))
1117 return false;
1118 }
1119
1120 if (elf_dynverdef (abfd) != 0)
1121 {
1122 Elf_Internal_Verdef *t;
1123
1124 fprintf (f, _("\nVersion definitions:\n"));
1125 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1126 {
1127 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1128 t->vd_flags, t->vd_hash, t->vd_nodename);
1129 if (t->vd_auxptr->vda_nextptr != NULL)
1130 {
1131 Elf_Internal_Verdaux *a;
1132
1133 fprintf (f, "\t");
1134 for (a = t->vd_auxptr->vda_nextptr;
1135 a != NULL;
1136 a = a->vda_nextptr)
1137 fprintf (f, "%s ", a->vda_nodename);
1138 fprintf (f, "\n");
1139 }
1140 }
1141 }
1142
1143 if (elf_dynverref (abfd) != 0)
1144 {
1145 Elf_Internal_Verneed *t;
1146
1147 fprintf (f, _("\nVersion References:\n"));
1148 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1149 {
1150 Elf_Internal_Vernaux *a;
1151
1152 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1153 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1154 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1155 a->vna_flags, a->vna_other, a->vna_nodename);
1156 }
1157 }
1158
1159 return true;
1160
1161 error_return:
1162 if (dynbuf != NULL)
1163 free (dynbuf);
1164 return false;
1165}
1166
1167/* Display ELF-specific fields of a symbol. */
1168
1169void
1170bfd_elf_print_symbol (abfd, filep, symbol, how)
1171 bfd *abfd;
1172 PTR filep;
1173 asymbol *symbol;
1174 bfd_print_symbol_type how;
1175{
1176 FILE *file = (FILE *) filep;
1177 switch (how)
1178 {
1179 case bfd_print_symbol_name:
1180 fprintf (file, "%s", symbol->name);
1181 break;
1182 case bfd_print_symbol_more:
1183 fprintf (file, "elf ");
60b89a18 1184 bfd_fprintf_vma (abfd, file, symbol->value);
252b5132
RH
1185 fprintf (file, " %lx", (long) symbol->flags);
1186 break;
1187 case bfd_print_symbol_all:
1188 {
4e8a9624
AM
1189 const char *section_name;
1190 const char *name = NULL;
587ff49e 1191 struct elf_backend_data *bed;
7a13edea 1192 unsigned char st_other;
dbb410c3 1193 bfd_vma val;
c044fabd 1194
252b5132 1195 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1196
1197 bed = get_elf_backend_data (abfd);
1198 if (bed->elf_backend_print_symbol_all)
c044fabd 1199 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1200
1201 if (name == NULL)
1202 {
7ee38065 1203 name = symbol->name;
60b89a18 1204 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
587ff49e
RH
1205 }
1206
252b5132
RH
1207 fprintf (file, " %s\t", section_name);
1208 /* Print the "other" value for a symbol. For common symbols,
1209 we've already printed the size; now print the alignment.
1210 For other symbols, we have no specified alignment, and
1211 we've printed the address; now print the size. */
dbb410c3
AM
1212 if (bfd_is_com_section (symbol->section))
1213 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1214 else
1215 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1216 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
1217
1218 /* If we have version information, print it. */
1219 if (elf_tdata (abfd)->dynversym_section != 0
1220 && (elf_tdata (abfd)->dynverdef_section != 0
1221 || elf_tdata (abfd)->dynverref_section != 0))
1222 {
1223 unsigned int vernum;
1224 const char *version_string;
1225
1226 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1227
1228 if (vernum == 0)
1229 version_string = "";
1230 else if (vernum == 1)
1231 version_string = "Base";
1232 else if (vernum <= elf_tdata (abfd)->cverdefs)
1233 version_string =
1234 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1235 else
1236 {
1237 Elf_Internal_Verneed *t;
1238
1239 version_string = "";
1240 for (t = elf_tdata (abfd)->verref;
1241 t != NULL;
1242 t = t->vn_nextref)
1243 {
1244 Elf_Internal_Vernaux *a;
1245
1246 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1247 {
1248 if (a->vna_other == vernum)
1249 {
1250 version_string = a->vna_nodename;
1251 break;
1252 }
1253 }
1254 }
1255 }
1256
1257 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1258 fprintf (file, " %-11s", version_string);
1259 else
1260 {
1261 int i;
1262
1263 fprintf (file, " (%s)", version_string);
1264 for (i = 10 - strlen (version_string); i > 0; --i)
1265 putc (' ', file);
1266 }
1267 }
1268
1269 /* If the st_other field is not zero, print it. */
7a13edea 1270 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 1271
7a13edea
NC
1272 switch (st_other)
1273 {
1274 case 0: break;
1275 case STV_INTERNAL: fprintf (file, " .internal"); break;
1276 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1277 case STV_PROTECTED: fprintf (file, " .protected"); break;
1278 default:
1279 /* Some other non-defined flags are also present, so print
1280 everything hex. */
1281 fprintf (file, " 0x%02x", (unsigned int) st_other);
1282 }
252b5132 1283
587ff49e 1284 fprintf (file, " %s", name);
252b5132
RH
1285 }
1286 break;
1287 }
1288}
1289\f
1290/* Create an entry in an ELF linker hash table. */
1291
1292struct bfd_hash_entry *
1293_bfd_elf_link_hash_newfunc (entry, table, string)
1294 struct bfd_hash_entry *entry;
1295 struct bfd_hash_table *table;
1296 const char *string;
1297{
252b5132
RH
1298 /* Allocate the structure if it has not already been allocated by a
1299 subclass. */
51b64d56
AM
1300 if (entry == NULL)
1301 {
1302 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1303 if (entry == NULL)
1304 return entry;
1305 }
252b5132
RH
1306
1307 /* Call the allocation method of the superclass. */
51b64d56
AM
1308 entry = _bfd_link_hash_newfunc (entry, table, string);
1309 if (entry != NULL)
252b5132 1310 {
51b64d56
AM
1311 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1312 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1313
252b5132
RH
1314 /* Set local fields. */
1315 ret->indx = -1;
1316 ret->size = 0;
1317 ret->dynindx = -1;
1318 ret->dynstr_index = 0;
1319 ret->weakdef = NULL;
51b64d56
AM
1320 ret->got.refcount = htab->init_refcount;
1321 ret->plt.refcount = htab->init_refcount;
1322 ret->linker_section_pointer = NULL;
252b5132
RH
1323 ret->verinfo.verdef = NULL;
1324 ret->vtable_entries_used = NULL;
1325 ret->vtable_entries_size = 0;
1326 ret->vtable_parent = NULL;
1327 ret->type = STT_NOTYPE;
1328 ret->other = 0;
1329 /* Assume that we have been called by a non-ELF symbol reader.
1330 This flag is then reset by the code which reads an ELF input
1331 file. This ensures that a symbol created by a non-ELF symbol
1332 reader will have the flag set correctly. */
1333 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1334 }
1335
51b64d56 1336 return entry;
252b5132
RH
1337}
1338
2920b85c 1339/* Copy data from an indirect symbol to its direct symbol, hiding the
0a991dfe 1340 old indirect symbol. Also used for copying flags to a weakdef. */
2920b85c 1341
c61b8717
RH
1342void
1343_bfd_elf_link_hash_copy_indirect (dir, ind)
2920b85c
RH
1344 struct elf_link_hash_entry *dir, *ind;
1345{
3c3e9281
AM
1346 bfd_signed_vma tmp;
1347
2920b85c
RH
1348 /* Copy down any references that we may have already seen to the
1349 symbol which just became indirect. */
1350
1351 dir->elf_link_hash_flags |=
1352 (ind->elf_link_hash_flags
1353 & (ELF_LINK_HASH_REF_DYNAMIC
1354 | ELF_LINK_HASH_REF_REGULAR
1355 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1356 | ELF_LINK_NON_GOT_REF));
1357
1e370bd2 1358 if (ind->root.type != bfd_link_hash_indirect)
0a991dfe
AM
1359 return;
1360
51b64d56 1361 /* Copy over the global and procedure linkage table refcount entries.
2920b85c 1362 These may have been already set up by a check_relocs routine. */
3c3e9281
AM
1363 tmp = dir->got.refcount;
1364 if (tmp <= 0)
2920b85c 1365 {
51b64d56 1366 dir->got.refcount = ind->got.refcount;
3c3e9281 1367 ind->got.refcount = tmp;
2920b85c 1368 }
3c3e9281
AM
1369 else
1370 BFD_ASSERT (ind->got.refcount <= 0);
2920b85c 1371
3c3e9281
AM
1372 tmp = dir->plt.refcount;
1373 if (tmp <= 0)
2920b85c 1374 {
51b64d56 1375 dir->plt.refcount = ind->plt.refcount;
3c3e9281 1376 ind->plt.refcount = tmp;
2920b85c 1377 }
3c3e9281
AM
1378 else
1379 BFD_ASSERT (ind->plt.refcount <= 0);
2920b85c
RH
1380
1381 if (dir->dynindx == -1)
1382 {
1383 dir->dynindx = ind->dynindx;
1384 dir->dynstr_index = ind->dynstr_index;
1385 ind->dynindx = -1;
1386 ind->dynstr_index = 0;
1387 }
3c3e9281
AM
1388 else
1389 BFD_ASSERT (ind->dynindx == -1);
2920b85c
RH
1390}
1391
c61b8717 1392void
e5094212
AM
1393_bfd_elf_link_hash_hide_symbol (info, h, force_local)
1394 struct bfd_link_info *info;
2920b85c 1395 struct elf_link_hash_entry *h;
e5094212 1396 boolean force_local;
2920b85c 1397{
2920b85c 1398 h->plt.offset = (bfd_vma) -1;
e5094212
AM
1399 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1400 if (force_local)
1401 {
1402 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1403 if (h->dynindx != -1)
1404 {
1405 h->dynindx = -1;
1406 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1407 h->dynstr_index);
1408 }
1409 }
2920b85c
RH
1410}
1411
252b5132
RH
1412/* Initialize an ELF linker hash table. */
1413
1414boolean
1415_bfd_elf_link_hash_table_init (table, abfd, newfunc)
1416 struct elf_link_hash_table *table;
1417 bfd *abfd;
1418 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1419 struct bfd_hash_table *,
1420 const char *));
1421{
8ea2e4bd
NC
1422 boolean ret;
1423
252b5132
RH
1424 table->dynamic_sections_created = false;
1425 table->dynobj = NULL;
51b64d56 1426 table->init_refcount = get_elf_backend_data (abfd)->can_refcount - 1;
252b5132
RH
1427 /* The first dynamic symbol is a dummy. */
1428 table->dynsymcount = 1;
1429 table->dynstr = NULL;
1430 table->bucketcount = 0;
1431 table->needed = NULL;
a963dc6a 1432 table->runpath = NULL;
252b5132
RH
1433 table->hgot = NULL;
1434 table->stab_info = NULL;
f5fa8ca2 1435 table->merge_info = NULL;
1ae00f9d 1436 table->dynlocal = NULL;
8ea2e4bd
NC
1437 ret = _bfd_link_hash_table_init (& table->root, abfd, newfunc);
1438 table->root.type = bfd_link_elf_hash_table;
1439
1440 return ret;
252b5132
RH
1441}
1442
1443/* Create an ELF linker hash table. */
1444
1445struct bfd_link_hash_table *
1446_bfd_elf_link_hash_table_create (abfd)
1447 bfd *abfd;
1448{
1449 struct elf_link_hash_table *ret;
dc810e39 1450 bfd_size_type amt = sizeof (struct elf_link_hash_table);
252b5132 1451
e2d34d7d 1452 ret = (struct elf_link_hash_table *) bfd_malloc (amt);
252b5132
RH
1453 if (ret == (struct elf_link_hash_table *) NULL)
1454 return NULL;
1455
1456 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1457 {
e2d34d7d 1458 free (ret);
252b5132
RH
1459 return NULL;
1460 }
1461
1462 return &ret->root;
1463}
1464
1465/* This is a hook for the ELF emulation code in the generic linker to
1466 tell the backend linker what file name to use for the DT_NEEDED
1467 entry for a dynamic object. The generic linker passes name as an
1468 empty string to indicate that no DT_NEEDED entry should be made. */
1469
1470void
1471bfd_elf_set_dt_needed_name (abfd, name)
1472 bfd *abfd;
1473 const char *name;
1474{
1475 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1476 && bfd_get_format (abfd) == bfd_object)
1477 elf_dt_name (abfd) = name;
1478}
1479
74816898
L
1480void
1481bfd_elf_set_dt_needed_soname (abfd, name)
1482 bfd *abfd;
1483 const char *name;
1484{
1485 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1486 && bfd_get_format (abfd) == bfd_object)
1487 elf_dt_soname (abfd) = name;
1488}
1489
252b5132
RH
1490/* Get the list of DT_NEEDED entries for a link. This is a hook for
1491 the linker ELF emulation code. */
1492
1493struct bfd_link_needed_list *
1494bfd_elf_get_needed_list (abfd, info)
7442e600 1495 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
1496 struct bfd_link_info *info;
1497{
1498 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1499 return NULL;
1500 return elf_hash_table (info)->needed;
1501}
1502
a963dc6a
L
1503/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1504 hook for the linker ELF emulation code. */
1505
1506struct bfd_link_needed_list *
1507bfd_elf_get_runpath_list (abfd, info)
1508 bfd *abfd ATTRIBUTE_UNUSED;
1509 struct bfd_link_info *info;
1510{
1511 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1512 return NULL;
1513 return elf_hash_table (info)->runpath;
1514}
1515
252b5132
RH
1516/* Get the name actually used for a dynamic object for a link. This
1517 is the SONAME entry if there is one. Otherwise, it is the string
1518 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1519
1520const char *
1521bfd_elf_get_dt_soname (abfd)
1522 bfd *abfd;
1523{
1524 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1525 && bfd_get_format (abfd) == bfd_object)
1526 return elf_dt_name (abfd);
1527 return NULL;
1528}
1529
1530/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1531 the ELF linker emulation code. */
1532
1533boolean
1534bfd_elf_get_bfd_needed_list (abfd, pneeded)
1535 bfd *abfd;
1536 struct bfd_link_needed_list **pneeded;
1537{
1538 asection *s;
1539 bfd_byte *dynbuf = NULL;
1540 int elfsec;
dc810e39 1541 unsigned long shlink;
252b5132
RH
1542 bfd_byte *extdyn, *extdynend;
1543 size_t extdynsize;
1544 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1545
1546 *pneeded = NULL;
1547
1548 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1549 || bfd_get_format (abfd) != bfd_object)
1550 return true;
1551
1552 s = bfd_get_section_by_name (abfd, ".dynamic");
1553 if (s == NULL || s->_raw_size == 0)
1554 return true;
1555
1556 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1557 if (dynbuf == NULL)
1558 goto error_return;
1559
1560 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1561 s->_raw_size))
1562 goto error_return;
1563
1564 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1565 if (elfsec == -1)
1566 goto error_return;
1567
dc810e39 1568 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1569
1570 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1571 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1572
1573 extdyn = dynbuf;
1574 extdynend = extdyn + s->_raw_size;
1575 for (; extdyn < extdynend; extdyn += extdynsize)
1576 {
1577 Elf_Internal_Dyn dyn;
1578
1579 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1580
1581 if (dyn.d_tag == DT_NULL)
1582 break;
1583
1584 if (dyn.d_tag == DT_NEEDED)
1585 {
1586 const char *string;
1587 struct bfd_link_needed_list *l;
dc810e39
AM
1588 unsigned int tagv = dyn.d_un.d_val;
1589 bfd_size_type amt;
252b5132 1590
dc810e39 1591 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1592 if (string == NULL)
1593 goto error_return;
1594
dc810e39
AM
1595 amt = sizeof *l;
1596 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
252b5132
RH
1597 if (l == NULL)
1598 goto error_return;
1599
1600 l->by = abfd;
1601 l->name = string;
1602 l->next = *pneeded;
1603 *pneeded = l;
1604 }
1605 }
1606
1607 free (dynbuf);
1608
1609 return true;
1610
1611 error_return:
1612 if (dynbuf != NULL)
1613 free (dynbuf);
1614 return false;
1615}
1616\f
1617/* Allocate an ELF string table--force the first byte to be zero. */
1618
1619struct bfd_strtab_hash *
1620_bfd_elf_stringtab_init ()
1621{
1622 struct bfd_strtab_hash *ret;
1623
1624 ret = _bfd_stringtab_init ();
1625 if (ret != NULL)
1626 {
1627 bfd_size_type loc;
1628
1629 loc = _bfd_stringtab_add (ret, "", true, false);
1630 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1631 if (loc == (bfd_size_type) -1)
1632 {
1633 _bfd_stringtab_free (ret);
1634 ret = NULL;
1635 }
1636 }
1637 return ret;
1638}
1639\f
1640/* ELF .o/exec file reading */
1641
c044fabd 1642/* Create a new bfd section from an ELF section header. */
252b5132
RH
1643
1644boolean
1645bfd_section_from_shdr (abfd, shindex)
1646 bfd *abfd;
1647 unsigned int shindex;
1648{
1649 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1650 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1651 struct elf_backend_data *bed = get_elf_backend_data (abfd);
90937f86 1652 const char *name;
252b5132
RH
1653
1654 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1655
1656 switch (hdr->sh_type)
1657 {
1658 case SHT_NULL:
1659 /* Inactive section. Throw it away. */
1660 return true;
1661
1662 case SHT_PROGBITS: /* Normal section with contents. */
1663 case SHT_DYNAMIC: /* Dynamic linking information. */
1664 case SHT_NOBITS: /* .bss section. */
1665 case SHT_HASH: /* .hash section. */
1666 case SHT_NOTE: /* .note section. */
25e27870
L
1667 case SHT_INIT_ARRAY: /* .init_array section. */
1668 case SHT_FINI_ARRAY: /* .fini_array section. */
1669 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
252b5132
RH
1670 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1671
1672 case SHT_SYMTAB: /* A symbol table */
1673 if (elf_onesymtab (abfd) == shindex)
1674 return true;
1675
1676 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1677 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1678 elf_onesymtab (abfd) = shindex;
1679 elf_tdata (abfd)->symtab_hdr = *hdr;
1680 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1681 abfd->flags |= HAS_SYMS;
1682
1683 /* Sometimes a shared object will map in the symbol table. If
1684 SHF_ALLOC is set, and this is a shared object, then we also
1685 treat this section as a BFD section. We can not base the
1686 decision purely on SHF_ALLOC, because that flag is sometimes
1687 set in a relocateable object file, which would confuse the
1688 linker. */
1689 if ((hdr->sh_flags & SHF_ALLOC) != 0
1690 && (abfd->flags & DYNAMIC) != 0
1691 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1692 return false;
1693
1694 return true;
1695
1696 case SHT_DYNSYM: /* A dynamic symbol table */
1697 if (elf_dynsymtab (abfd) == shindex)
1698 return true;
1699
1700 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1701 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1702 elf_dynsymtab (abfd) = shindex;
1703 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1704 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1705 abfd->flags |= HAS_SYMS;
1706
1707 /* Besides being a symbol table, we also treat this as a regular
1708 section, so that objcopy can handle it. */
1709 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1710
9ad5cbcf
AM
1711 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1712 if (elf_symtab_shndx (abfd) == shindex)
1713 return true;
1714
1715 /* Get the associated symbol table. */
1716 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1717 || hdr->sh_link != elf_onesymtab (abfd))
1718 return false;
1719
1720 elf_symtab_shndx (abfd) = shindex;
1721 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1722 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1723 return true;
1724
252b5132
RH
1725 case SHT_STRTAB: /* A string table */
1726 if (hdr->bfd_section != NULL)
1727 return true;
1728 if (ehdr->e_shstrndx == shindex)
1729 {
1730 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1731 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1732 return true;
1733 }
1734 {
9ad5cbcf 1735 unsigned int i, num_sec;
252b5132 1736
9ad5cbcf
AM
1737 num_sec = elf_numsections (abfd);
1738 for (i = 1; i < num_sec; i++)
252b5132
RH
1739 {
1740 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1741 if (hdr2->sh_link == shindex)
1742 {
1743 if (! bfd_section_from_shdr (abfd, i))
1744 return false;
1745 if (elf_onesymtab (abfd) == i)
1746 {
1747 elf_tdata (abfd)->strtab_hdr = *hdr;
1748 elf_elfsections (abfd)[shindex] =
1749 &elf_tdata (abfd)->strtab_hdr;
1750 return true;
1751 }
1752 if (elf_dynsymtab (abfd) == i)
1753 {
1754 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1755 elf_elfsections (abfd)[shindex] = hdr =
1756 &elf_tdata (abfd)->dynstrtab_hdr;
1757 /* We also treat this as a regular section, so
1758 that objcopy can handle it. */
1759 break;
1760 }
1761#if 0 /* Not handling other string tables specially right now. */
1762 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1763 /* We have a strtab for some random other section. */
1764 newsect = (asection *) hdr2->bfd_section;
1765 if (!newsect)
1766 break;
1767 hdr->bfd_section = newsect;
1768 hdr2 = &elf_section_data (newsect)->str_hdr;
1769 *hdr2 = *hdr;
1770 elf_elfsections (abfd)[shindex] = hdr2;
1771#endif
1772 }
1773 }
1774 }
1775
1776 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1777
1778 case SHT_REL:
1779 case SHT_RELA:
1780 /* *These* do a lot of work -- but build no sections! */
1781 {
1782 asection *target_sect;
1783 Elf_Internal_Shdr *hdr2;
9ad5cbcf 1784 unsigned int num_sec = elf_numsections (abfd);
252b5132 1785
03ae5f59 1786 /* Check for a bogus link to avoid crashing. */
9ad5cbcf
AM
1787 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1788 || hdr->sh_link >= num_sec)
03ae5f59
ILT
1789 {
1790 ((*_bfd_error_handler)
1791 (_("%s: invalid link %lu for reloc section %s (index %u)"),
8f615d07 1792 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
03ae5f59
ILT
1793 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1794 }
1795
252b5132
RH
1796 /* For some incomprehensible reason Oracle distributes
1797 libraries for Solaris in which some of the objects have
1798 bogus sh_link fields. It would be nice if we could just
1799 reject them, but, unfortunately, some people need to use
1800 them. We scan through the section headers; if we find only
1801 one suitable symbol table, we clobber the sh_link to point
1802 to it. I hope this doesn't break anything. */
1803 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1804 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1805 {
9ad5cbcf 1806 unsigned int scan;
252b5132
RH
1807 int found;
1808
1809 found = 0;
9ad5cbcf 1810 for (scan = 1; scan < num_sec; scan++)
252b5132
RH
1811 {
1812 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1813 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1814 {
1815 if (found != 0)
1816 {
1817 found = 0;
1818 break;
1819 }
1820 found = scan;
1821 }
1822 }
1823 if (found != 0)
1824 hdr->sh_link = found;
1825 }
1826
1827 /* Get the symbol table. */
1828 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1829 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1830 return false;
1831
1832 /* If this reloc section does not use the main symbol table we
1833 don't treat it as a reloc section. BFD can't adequately
1834 represent such a section, so at least for now, we don't
c044fabd 1835 try. We just present it as a normal section. We also
60bcf0fa 1836 can't use it as a reloc section if it points to the null
c044fabd 1837 section. */
60bcf0fa 1838 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
252b5132
RH
1839 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1840
1841 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1842 return false;
1843 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1844 if (target_sect == NULL)
1845 return false;
1846
1847 if ((target_sect->flags & SEC_RELOC) == 0
1848 || target_sect->reloc_count == 0)
1849 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1850 else
1851 {
dc810e39 1852 bfd_size_type amt;
252b5132 1853 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
dc810e39
AM
1854 amt = sizeof (*hdr2);
1855 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
252b5132
RH
1856 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1857 }
1858 *hdr2 = *hdr;
1859 elf_elfsections (abfd)[shindex] = hdr2;
d9bc7a44 1860 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
252b5132
RH
1861 target_sect->flags |= SEC_RELOC;
1862 target_sect->relocation = NULL;
1863 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
1864 /* In the section to which the relocations apply, mark whether
1865 its relocations are of the REL or RELA variety. */
72730e0c
AM
1866 if (hdr->sh_size != 0)
1867 elf_section_data (target_sect)->use_rela_p
1868 = (hdr->sh_type == SHT_RELA);
252b5132
RH
1869 abfd->flags |= HAS_RELOC;
1870 return true;
1871 }
1872 break;
1873
1874 case SHT_GNU_verdef:
1875 elf_dynverdef (abfd) = shindex;
1876 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1877 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1878 break;
1879
1880 case SHT_GNU_versym:
1881 elf_dynversym (abfd) = shindex;
1882 elf_tdata (abfd)->dynversym_hdr = *hdr;
1883 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1884 break;
1885
1886 case SHT_GNU_verneed:
1887 elf_dynverref (abfd) = shindex;
1888 elf_tdata (abfd)->dynverref_hdr = *hdr;
1889 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1890 break;
1891
1892 case SHT_SHLIB:
1893 return true;
1894
dbb410c3 1895 case SHT_GROUP:
b885599b
AM
1896 /* We need a BFD section for objcopy and relocatable linking,
1897 and it's handy to have the signature available as the section
1898 name. */
1899 name = group_signature (abfd, hdr);
1900 if (name == NULL)
1901 return false;
dbb410c3
AM
1902 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
1903 return false;
1904 if (hdr->contents != NULL)
1905 {
1906 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1907 unsigned int n_elt = hdr->sh_size / 4;
1908 asection *s;
1909
b885599b
AM
1910 if (idx->flags & GRP_COMDAT)
1911 hdr->bfd_section->flags
1912 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1913
dbb410c3
AM
1914 while (--n_elt != 0)
1915 if ((s = (++idx)->shdr->bfd_section) != NULL
945906ff 1916 && elf_next_in_group (s) != NULL)
dbb410c3 1917 {
945906ff 1918 elf_next_in_group (hdr->bfd_section) = s;
dbb410c3
AM
1919 break;
1920 }
1921 }
1922 break;
1923
252b5132
RH
1924 default:
1925 /* Check for any processor-specific section types. */
1926 {
1927 if (bed->elf_backend_section_from_shdr)
1928 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1929 }
1930 break;
1931 }
1932
1933 return true;
1934}
1935
ec338859
AM
1936/* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1937 Return SEC for sections that have no elf section, and NULL on error. */
1938
1939asection *
1940bfd_section_from_r_symndx (abfd, cache, sec, r_symndx)
1941 bfd *abfd;
1942 struct sym_sec_cache *cache;
1943 asection *sec;
1944 unsigned long r_symndx;
1945{
9ad5cbcf 1946 unsigned char esym_shndx[4];
ec338859
AM
1947 unsigned int isym_shndx;
1948 Elf_Internal_Shdr *symtab_hdr;
1949 file_ptr pos;
1950 bfd_size_type amt;
1951 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1952
1953 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
1954 return cache->sec[ent];
1955
1956 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1957 pos = symtab_hdr->sh_offset;
1958 if (get_elf_backend_data (abfd)->s->sizeof_sym
1959 == sizeof (Elf64_External_Sym))
1960 {
1961 pos += r_symndx * sizeof (Elf64_External_Sym);
1962 pos += offsetof (Elf64_External_Sym, st_shndx);
9ad5cbcf 1963 amt = sizeof (((Elf64_External_Sym *) 0)->st_shndx);
ec338859
AM
1964 }
1965 else
1966 {
1967 pos += r_symndx * sizeof (Elf32_External_Sym);
1968 pos += offsetof (Elf32_External_Sym, st_shndx);
9ad5cbcf 1969 amt = sizeof (((Elf32_External_Sym *) 0)->st_shndx);
ec338859 1970 }
ec338859
AM
1971 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1972 || bfd_bread ((PTR) esym_shndx, amt, abfd) != amt)
1973 return NULL;
1974 isym_shndx = H_GET_16 (abfd, esym_shndx);
1975
9ad5cbcf
AM
1976 if (isym_shndx == SHN_XINDEX)
1977 {
1978 Elf_Internal_Shdr *shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1979 if (shndx_hdr->sh_size != 0)
1980 {
1981 pos = shndx_hdr->sh_offset;
1982 pos += r_symndx * sizeof (Elf_External_Sym_Shndx);
1983 amt = sizeof (Elf_External_Sym_Shndx);
1984 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1985 || bfd_bread ((PTR) esym_shndx, amt, abfd) != amt)
1986 return NULL;
1987 isym_shndx = H_GET_32 (abfd, esym_shndx);
1988 }
1989 }
1990
ec338859
AM
1991 if (cache->abfd != abfd)
1992 {
1993 memset (cache->indx, -1, sizeof (cache->indx));
1994 cache->abfd = abfd;
1995 }
1996 cache->indx[ent] = r_symndx;
1997 cache->sec[ent] = sec;
9ad5cbcf 1998 if (isym_shndx < SHN_LORESERVE || isym_shndx > SHN_HIRESERVE)
ec338859
AM
1999 {
2000 asection *s;
2001 s = bfd_section_from_elf_index (abfd, isym_shndx);
2002 if (s != NULL)
2003 cache->sec[ent] = s;
2004 }
2005 return cache->sec[ent];
2006}
2007
252b5132
RH
2008/* Given an ELF section number, retrieve the corresponding BFD
2009 section. */
2010
2011asection *
2012bfd_section_from_elf_index (abfd, index)
2013 bfd *abfd;
2014 unsigned int index;
2015{
9ad5cbcf 2016 if (index >= elf_numsections (abfd))
252b5132
RH
2017 return NULL;
2018 return elf_elfsections (abfd)[index]->bfd_section;
2019}
2020
2021boolean
2022_bfd_elf_new_section_hook (abfd, sec)
2023 bfd *abfd;
2024 asection *sec;
2025{
2026 struct bfd_elf_section_data *sdata;
dc810e39 2027 bfd_size_type amt = sizeof (*sdata);
252b5132 2028
dc810e39 2029 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt);
252b5132
RH
2030 if (!sdata)
2031 return false;
2032 sec->used_by_bfd = (PTR) sdata;
bf572ba0
MM
2033
2034 /* Indicate whether or not this section should use RELA relocations. */
c044fabd 2035 sdata->use_rela_p
bf572ba0
MM
2036 = get_elf_backend_data (abfd)->default_use_rela_p;
2037
252b5132
RH
2038 return true;
2039}
2040
2041/* Create a new bfd section from an ELF program header.
2042
2043 Since program segments have no names, we generate a synthetic name
2044 of the form segment<NUM>, where NUM is generally the index in the
2045 program header table. For segments that are split (see below) we
2046 generate the names segment<NUM>a and segment<NUM>b.
2047
2048 Note that some program segments may have a file size that is different than
2049 (less than) the memory size. All this means is that at execution the
2050 system must allocate the amount of memory specified by the memory size,
2051 but only initialize it with the first "file size" bytes read from the
2052 file. This would occur for example, with program segments consisting
2053 of combined data+bss.
2054
2055 To handle the above situation, this routine generates TWO bfd sections
2056 for the single program segment. The first has the length specified by
2057 the file size of the segment, and the second has the length specified
2058 by the difference between the two sizes. In effect, the segment is split
2059 into it's initialized and uninitialized parts.
2060
2061 */
2062
2063boolean
20cfcaae 2064_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
252b5132
RH
2065 bfd *abfd;
2066 Elf_Internal_Phdr *hdr;
2067 int index;
20cfcaae 2068 const char *typename;
252b5132
RH
2069{
2070 asection *newsect;
2071 char *name;
2072 char namebuf[64];
2073 int split;
2074
2075 split = ((hdr->p_memsz > 0)
2076 && (hdr->p_filesz > 0)
2077 && (hdr->p_memsz > hdr->p_filesz));
27ac83bf 2078 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
dc810e39 2079 name = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
252b5132
RH
2080 if (!name)
2081 return false;
2082 strcpy (name, namebuf);
2083 newsect = bfd_make_section (abfd, name);
2084 if (newsect == NULL)
2085 return false;
2086 newsect->vma = hdr->p_vaddr;
2087 newsect->lma = hdr->p_paddr;
2088 newsect->_raw_size = hdr->p_filesz;
2089 newsect->filepos = hdr->p_offset;
2090 newsect->flags |= SEC_HAS_CONTENTS;
2091 if (hdr->p_type == PT_LOAD)
2092 {
2093 newsect->flags |= SEC_ALLOC;
2094 newsect->flags |= SEC_LOAD;
2095 if (hdr->p_flags & PF_X)
2096 {
2097 /* FIXME: all we known is that it has execute PERMISSION,
c044fabd 2098 may be data. */
252b5132
RH
2099 newsect->flags |= SEC_CODE;
2100 }
2101 }
2102 if (!(hdr->p_flags & PF_W))
2103 {
2104 newsect->flags |= SEC_READONLY;
2105 }
2106
2107 if (split)
2108 {
27ac83bf 2109 sprintf (namebuf, "%s%db", typename, index);
dc810e39 2110 name = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
252b5132
RH
2111 if (!name)
2112 return false;
2113 strcpy (name, namebuf);
2114 newsect = bfd_make_section (abfd, name);
2115 if (newsect == NULL)
2116 return false;
2117 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2118 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2119 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
2120 if (hdr->p_type == PT_LOAD)
2121 {
2122 newsect->flags |= SEC_ALLOC;
2123 if (hdr->p_flags & PF_X)
2124 newsect->flags |= SEC_CODE;
2125 }
2126 if (!(hdr->p_flags & PF_W))
2127 newsect->flags |= SEC_READONLY;
2128 }
2129
2130 return true;
2131}
2132
20cfcaae
NC
2133boolean
2134bfd_section_from_phdr (abfd, hdr, index)
2135 bfd *abfd;
2136 Elf_Internal_Phdr *hdr;
2137 int index;
2138{
2139 struct elf_backend_data *bed;
2140
2141 switch (hdr->p_type)
2142 {
2143 case PT_NULL:
2144 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2145
2146 case PT_LOAD:
2147 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2148
2149 case PT_DYNAMIC:
2150 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2151
2152 case PT_INTERP:
2153 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2154
2155 case PT_NOTE:
2156 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2157 return false;
dc810e39 2158 if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz))
20cfcaae
NC
2159 return false;
2160 return true;
2161
2162 case PT_SHLIB:
2163 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2164
2165 case PT_PHDR:
2166 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2167
2168 default:
2169 /* Check for any processor-specific program segment types.
c044fabd 2170 If no handler for them, default to making "segment" sections. */
20cfcaae
NC
2171 bed = get_elf_backend_data (abfd);
2172 if (bed->elf_backend_section_from_phdr)
2173 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2174 else
2175 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2176 }
2177}
2178
23bc299b
MM
2179/* Initialize REL_HDR, the section-header for new section, containing
2180 relocations against ASECT. If USE_RELA_P is true, we use RELA
2181 relocations; otherwise, we use REL relocations. */
2182
2183boolean
2184_bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
2185 bfd *abfd;
2186 Elf_Internal_Shdr *rel_hdr;
2187 asection *asect;
2188 boolean use_rela_p;
2189{
2190 char *name;
dc810e39
AM
2191 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2192 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
23bc299b 2193
dc810e39 2194 name = bfd_alloc (abfd, amt);
23bc299b
MM
2195 if (name == NULL)
2196 return false;
2197 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2198 rel_hdr->sh_name =
2b0f7ef9
JJ
2199 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2200 false);
23bc299b
MM
2201 if (rel_hdr->sh_name == (unsigned int) -1)
2202 return false;
2203 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2204 rel_hdr->sh_entsize = (use_rela_p
2205 ? bed->s->sizeof_rela
2206 : bed->s->sizeof_rel);
2207 rel_hdr->sh_addralign = bed->s->file_align;
2208 rel_hdr->sh_flags = 0;
2209 rel_hdr->sh_addr = 0;
2210 rel_hdr->sh_size = 0;
2211 rel_hdr->sh_offset = 0;
2212
2213 return true;
2214}
2215
252b5132
RH
2216/* Set up an ELF internal section header for a section. */
2217
252b5132
RH
2218static void
2219elf_fake_sections (abfd, asect, failedptrarg)
2220 bfd *abfd;
2221 asection *asect;
2222 PTR failedptrarg;
2223{
2224 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2225 boolean *failedptr = (boolean *) failedptrarg;
2226 Elf_Internal_Shdr *this_hdr;
2227
2228 if (*failedptr)
2229 {
2230 /* We already failed; just get out of the bfd_map_over_sections
2231 loop. */
2232 return;
2233 }
2234
2235 this_hdr = &elf_section_data (asect)->this_hdr;
2236
2b0f7ef9
JJ
2237 this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2238 asect->name, false);
252b5132
RH
2239 if (this_hdr->sh_name == (unsigned long) -1)
2240 {
2241 *failedptr = true;
2242 return;
2243 }
2244
2245 this_hdr->sh_flags = 0;
2246
2247 if ((asect->flags & SEC_ALLOC) != 0
2248 || asect->user_set_vma)
2249 this_hdr->sh_addr = asect->vma;
2250 else
2251 this_hdr->sh_addr = 0;
2252
2253 this_hdr->sh_offset = 0;
2254 this_hdr->sh_size = asect->_raw_size;
2255 this_hdr->sh_link = 0;
2256 this_hdr->sh_addralign = 1 << asect->alignment_power;
2257 /* The sh_entsize and sh_info fields may have been set already by
2258 copy_private_section_data. */
2259
2260 this_hdr->bfd_section = asect;
2261 this_hdr->contents = NULL;
2262
2263 /* FIXME: This should not be based on section names. */
2264 if (strcmp (asect->name, ".dynstr") == 0)
2265 this_hdr->sh_type = SHT_STRTAB;
2266 else if (strcmp (asect->name, ".hash") == 0)
2267 {
2268 this_hdr->sh_type = SHT_HASH;
c7ac6ff8 2269 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
252b5132
RH
2270 }
2271 else if (strcmp (asect->name, ".dynsym") == 0)
2272 {
2273 this_hdr->sh_type = SHT_DYNSYM;
2274 this_hdr->sh_entsize = bed->s->sizeof_sym;
2275 }
2276 else if (strcmp (asect->name, ".dynamic") == 0)
2277 {
2278 this_hdr->sh_type = SHT_DYNAMIC;
2279 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2280 }
a9d024b8 2281 else if (strncmp (asect->name, ".rela", 5) == 0
bf572ba0 2282 && get_elf_backend_data (abfd)->may_use_rela_p)
252b5132
RH
2283 {
2284 this_hdr->sh_type = SHT_RELA;
2285 this_hdr->sh_entsize = bed->s->sizeof_rela;
2286 }
a9d024b8 2287 else if (strncmp (asect->name, ".rel", 4) == 0
bf572ba0 2288 && get_elf_backend_data (abfd)->may_use_rel_p)
252b5132
RH
2289 {
2290 this_hdr->sh_type = SHT_REL;
2291 this_hdr->sh_entsize = bed->s->sizeof_rel;
2292 }
25e27870
L
2293 else if (strcmp (asect->name, ".init_array") == 0)
2294 this_hdr->sh_type = SHT_INIT_ARRAY;
2295 else if (strcmp (asect->name, ".fini_array") == 0)
2296 this_hdr->sh_type = SHT_FINI_ARRAY;
2297 else if (strcmp (asect->name, ".preinit_array") == 0)
2298 this_hdr->sh_type = SHT_PREINIT_ARRAY;
252b5132
RH
2299 else if (strncmp (asect->name, ".note", 5) == 0)
2300 this_hdr->sh_type = SHT_NOTE;
2301 else if (strncmp (asect->name, ".stab", 5) == 0
2302 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
2303 this_hdr->sh_type = SHT_STRTAB;
2304 else if (strcmp (asect->name, ".gnu.version") == 0)
2305 {
2306 this_hdr->sh_type = SHT_GNU_versym;
2307 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2308 }
2309 else if (strcmp (asect->name, ".gnu.version_d") == 0)
2310 {
2311 this_hdr->sh_type = SHT_GNU_verdef;
2312 this_hdr->sh_entsize = 0;
2313 /* objcopy or strip will copy over sh_info, but may not set
2314 cverdefs. The linker will set cverdefs, but sh_info will be
2315 zero. */
2316 if (this_hdr->sh_info == 0)
2317 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2318 else
2319 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2320 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2321 }
2322 else if (strcmp (asect->name, ".gnu.version_r") == 0)
2323 {
2324 this_hdr->sh_type = SHT_GNU_verneed;
2325 this_hdr->sh_entsize = 0;
2326 /* objcopy or strip will copy over sh_info, but may not set
2327 cverrefs. The linker will set cverrefs, but sh_info will be
2328 zero. */
2329 if (this_hdr->sh_info == 0)
2330 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2331 else
2332 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2333 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2334 }
dbb410c3
AM
2335 else if ((asect->flags & SEC_GROUP) != 0)
2336 {
2337 this_hdr->sh_type = SHT_GROUP;
2338 this_hdr->sh_entsize = 4;
2339 }
252b5132 2340 else if ((asect->flags & SEC_ALLOC) != 0
edd29cf9
AM
2341 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2342 || (asect->flags & SEC_NEVER_LOAD) != 0))
252b5132
RH
2343 this_hdr->sh_type = SHT_NOBITS;
2344 else
6c99a5c3 2345 this_hdr->sh_type = SHT_PROGBITS;
252b5132
RH
2346
2347 if ((asect->flags & SEC_ALLOC) != 0)
2348 this_hdr->sh_flags |= SHF_ALLOC;
2349 if ((asect->flags & SEC_READONLY) == 0)
2350 this_hdr->sh_flags |= SHF_WRITE;
2351 if ((asect->flags & SEC_CODE) != 0)
2352 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
2353 if ((asect->flags & SEC_MERGE) != 0)
2354 {
2355 this_hdr->sh_flags |= SHF_MERGE;
2356 this_hdr->sh_entsize = asect->entsize;
2357 if ((asect->flags & SEC_STRINGS) != 0)
2358 this_hdr->sh_flags |= SHF_STRINGS;
2359 }
1126897b 2360 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 2361 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3
JJ
2362 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2363 this_hdr->sh_flags |= SHF_TLS;
252b5132
RH
2364
2365 /* Check for processor-specific section types. */
e1fddb6b
AO
2366 if (bed->elf_backend_fake_sections
2367 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2368 *failedptr = true;
252b5132
RH
2369
2370 /* If the section has relocs, set up a section header for the
23bc299b
MM
2371 SHT_REL[A] section. If two relocation sections are required for
2372 this section, it is up to the processor-specific back-end to
c044fabd 2373 create the other. */
23bc299b 2374 if ((asect->flags & SEC_RELOC) != 0
c044fabd 2375 && !_bfd_elf_init_reloc_shdr (abfd,
23bc299b 2376 &elf_section_data (asect)->rel_hdr,
c044fabd 2377 asect,
23bc299b
MM
2378 elf_section_data (asect)->use_rela_p))
2379 *failedptr = true;
252b5132
RH
2380}
2381
dbb410c3
AM
2382/* Fill in the contents of a SHT_GROUP section. */
2383
1126897b
AM
2384void
2385bfd_elf_set_group_contents (abfd, sec, failedptrarg)
dbb410c3
AM
2386 bfd *abfd;
2387 asection *sec;
1126897b 2388 PTR failedptrarg;
dbb410c3
AM
2389{
2390 boolean *failedptr = (boolean *) failedptrarg;
2391 unsigned long symindx;
9dce4196 2392 asection *elt, *first;
dbb410c3
AM
2393 unsigned char *loc;
2394 struct bfd_link_order *l;
9dce4196 2395 boolean gas;
dbb410c3
AM
2396
2397 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2398 || *failedptr)
2399 return;
2400
1126897b
AM
2401 symindx = 0;
2402 if (elf_group_id (sec) != NULL)
2403 symindx = elf_group_id (sec)->udata.i;
2404
2405 if (symindx == 0)
2406 {
2407 /* If called from the assembler, swap_out_syms will have set up
2408 elf_section_syms; If called for "ld -r", use target_index. */
2409 if (elf_section_syms (abfd) != NULL)
2410 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2411 else
2412 symindx = sec->target_index;
2413 }
dbb410c3
AM
2414 elf_section_data (sec)->this_hdr.sh_info = symindx;
2415
1126897b 2416 /* The contents won't be allocated for "ld -r" or objcopy. */
9dce4196 2417 gas = true;
dbb410c3
AM
2418 if (sec->contents == NULL)
2419 {
9dce4196 2420 gas = false;
dbb410c3 2421 sec->contents = bfd_alloc (abfd, sec->_raw_size);
9dce4196
AM
2422
2423 /* Arrange for the section to be written out. */
2424 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
2425 if (sec->contents == NULL)
2426 {
2427 *failedptr = true;
2428 return;
2429 }
2430 }
2431
2432 loc = sec->contents + sec->_raw_size;
2433
9dce4196
AM
2434 /* Get the pointer to the first section in the group that gas
2435 squirreled away here. objcopy arranges for this to be set to the
2436 start of the input section group. */
2437 first = elt = elf_next_in_group (sec);
dbb410c3
AM
2438
2439 /* First element is a flag word. Rest of section is elf section
2440 indices for all the sections of the group. Write them backwards
2441 just to keep the group in the same order as given in .section
2442 directives, not that it matters. */
2443 while (elt != NULL)
2444 {
9dce4196
AM
2445 asection *s;
2446 unsigned int idx;
2447
dbb410c3 2448 loc -= 4;
9dce4196
AM
2449 s = elt;
2450 if (!gas)
2451 s = s->output_section;
2452 idx = 0;
2453 if (s != NULL)
2454 idx = elf_section_data (s)->this_idx;
2455 H_PUT_32 (abfd, idx, loc);
945906ff 2456 elt = elf_next_in_group (elt);
9dce4196
AM
2457 if (elt == first)
2458 break;
dbb410c3
AM
2459 }
2460
2461 /* If this is a relocatable link, then the above did nothing because
2462 SEC is the output section. Look through the input sections
2463 instead. */
2464 for (l = sec->link_order_head; l != NULL; l = l->next)
2465 if (l->type == bfd_indirect_link_order
945906ff 2466 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
dbb410c3
AM
2467 do
2468 {
2469 loc -= 4;
2470 H_PUT_32 (abfd,
2471 elf_section_data (elt->output_section)->this_idx, loc);
945906ff 2472 elt = elf_next_in_group (elt);
dbb410c3
AM
2473 /* During a relocatable link, the lists are circular. */
2474 }
945906ff 2475 while (elt != elf_next_in_group (l->u.indirect.section));
dbb410c3 2476
9dce4196
AM
2477 /* With ld -r, merging SHT_GROUP sections results in wasted space
2478 due to allowing for the flag word on each input. We may well
2479 duplicate entries too. */
2480 while ((loc -= 4) > sec->contents)
2481 H_PUT_32 (abfd, 0, loc);
2482
2483 if (loc != sec->contents)
2484 abort ();
dbb410c3 2485
9dce4196 2486 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
2487}
2488
252b5132
RH
2489/* Assign all ELF section numbers. The dummy first section is handled here
2490 too. The link/info pointers for the standard section types are filled
2491 in here too, while we're at it. */
2492
2493static boolean
2494assign_section_numbers (abfd)
2495 bfd *abfd;
2496{
2497 struct elf_obj_tdata *t = elf_tdata (abfd);
2498 asection *sec;
2b0f7ef9 2499 unsigned int section_number, secn;
252b5132 2500 Elf_Internal_Shdr **i_shdrp;
dc810e39 2501 bfd_size_type amt;
252b5132
RH
2502
2503 section_number = 1;
2504
2b0f7ef9
JJ
2505 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2506
252b5132
RH
2507 for (sec = abfd->sections; sec; sec = sec->next)
2508 {
2509 struct bfd_elf_section_data *d = elf_section_data (sec);
2510
9ad5cbcf
AM
2511 if (section_number == SHN_LORESERVE)
2512 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132 2513 d->this_idx = section_number++;
2b0f7ef9 2514 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
252b5132
RH
2515 if ((sec->flags & SEC_RELOC) == 0)
2516 d->rel_idx = 0;
2517 else
2b0f7ef9 2518 {
9ad5cbcf
AM
2519 if (section_number == SHN_LORESERVE)
2520 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2b0f7ef9
JJ
2521 d->rel_idx = section_number++;
2522 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2523 }
23bc299b
MM
2524
2525 if (d->rel_hdr2)
2b0f7ef9 2526 {
9ad5cbcf
AM
2527 if (section_number == SHN_LORESERVE)
2528 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2b0f7ef9
JJ
2529 d->rel_idx2 = section_number++;
2530 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2531 }
23bc299b
MM
2532 else
2533 d->rel_idx2 = 0;
252b5132
RH
2534 }
2535
9ad5cbcf
AM
2536 if (section_number == SHN_LORESERVE)
2537 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132 2538 t->shstrtab_section = section_number++;
2b0f7ef9 2539 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
252b5132 2540 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
252b5132
RH
2541
2542 if (bfd_get_symcount (abfd) > 0)
2543 {
9ad5cbcf
AM
2544 if (section_number == SHN_LORESERVE)
2545 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132 2546 t->symtab_section = section_number++;
2b0f7ef9 2547 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
9ad5cbcf
AM
2548 if (section_number > SHN_LORESERVE - 2)
2549 {
2550 if (section_number == SHN_LORESERVE)
2551 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2552 t->symtab_shndx_section = section_number++;
2553 t->symtab_shndx_hdr.sh_name
2554 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2555 ".symtab_shndx", false);
2556 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2557 return false;
2558 }
2559 if (section_number == SHN_LORESERVE)
2560 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132 2561 t->strtab_section = section_number++;
2b0f7ef9 2562 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
2563 }
2564
2b0f7ef9
JJ
2565 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2566 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
9ad5cbcf
AM
2567
2568 elf_numsections (abfd) = section_number;
252b5132 2569 elf_elfheader (abfd)->e_shnum = section_number;
9ad5cbcf
AM
2570 if (section_number > SHN_LORESERVE)
2571 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132
RH
2572
2573 /* Set up the list of section header pointers, in agreement with the
2574 indices. */
dc810e39
AM
2575 amt = section_number * sizeof (Elf_Internal_Shdr *);
2576 i_shdrp = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
252b5132
RH
2577 if (i_shdrp == NULL)
2578 return false;
2579
dc810e39
AM
2580 amt = sizeof (Elf_Internal_Shdr);
2581 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
252b5132
RH
2582 if (i_shdrp[0] == NULL)
2583 {
2584 bfd_release (abfd, i_shdrp);
2585 return false;
2586 }
2587 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
2588
2589 elf_elfsections (abfd) = i_shdrp;
2590
2591 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2592 if (bfd_get_symcount (abfd) > 0)
2593 {
2594 i_shdrp[t->symtab_section] = &t->symtab_hdr;
9ad5cbcf
AM
2595 if (elf_numsections (abfd) > SHN_LORESERVE)
2596 {
2597 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2598 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2599 }
252b5132
RH
2600 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2601 t->symtab_hdr.sh_link = t->strtab_section;
2602 }
2603 for (sec = abfd->sections; sec; sec = sec->next)
2604 {
2605 struct bfd_elf_section_data *d = elf_section_data (sec);
2606 asection *s;
2607 const char *name;
2608
2609 i_shdrp[d->this_idx] = &d->this_hdr;
2610 if (d->rel_idx != 0)
2611 i_shdrp[d->rel_idx] = &d->rel_hdr;
23bc299b
MM
2612 if (d->rel_idx2 != 0)
2613 i_shdrp[d->rel_idx2] = d->rel_hdr2;
252b5132
RH
2614
2615 /* Fill in the sh_link and sh_info fields while we're at it. */
2616
2617 /* sh_link of a reloc section is the section index of the symbol
2618 table. sh_info is the section index of the section to which
2619 the relocation entries apply. */
2620 if (d->rel_idx != 0)
2621 {
2622 d->rel_hdr.sh_link = t->symtab_section;
2623 d->rel_hdr.sh_info = d->this_idx;
2624 }
23bc299b
MM
2625 if (d->rel_idx2 != 0)
2626 {
2627 d->rel_hdr2->sh_link = t->symtab_section;
2628 d->rel_hdr2->sh_info = d->this_idx;
2629 }
252b5132
RH
2630
2631 switch (d->this_hdr.sh_type)
2632 {
2633 case SHT_REL:
2634 case SHT_RELA:
2635 /* A reloc section which we are treating as a normal BFD
2636 section. sh_link is the section index of the symbol
2637 table. sh_info is the section index of the section to
2638 which the relocation entries apply. We assume that an
2639 allocated reloc section uses the dynamic symbol table.
2640 FIXME: How can we be sure? */
2641 s = bfd_get_section_by_name (abfd, ".dynsym");
2642 if (s != NULL)
2643 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2644
2645 /* We look up the section the relocs apply to by name. */
2646 name = sec->name;
2647 if (d->this_hdr.sh_type == SHT_REL)
2648 name += 4;
2649 else
2650 name += 5;
2651 s = bfd_get_section_by_name (abfd, name);
2652 if (s != NULL)
2653 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2654 break;
2655
2656 case SHT_STRTAB:
2657 /* We assume that a section named .stab*str is a stabs
2658 string section. We look for a section with the same name
2659 but without the trailing ``str'', and set its sh_link
2660 field to point to this section. */
2661 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2662 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2663 {
2664 size_t len;
2665 char *alc;
2666
2667 len = strlen (sec->name);
dc810e39 2668 alc = (char *) bfd_malloc ((bfd_size_type) len - 2);
252b5132
RH
2669 if (alc == NULL)
2670 return false;
2671 strncpy (alc, sec->name, len - 3);
2672 alc[len - 3] = '\0';
2673 s = bfd_get_section_by_name (abfd, alc);
2674 free (alc);
2675 if (s != NULL)
2676 {
2677 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2678
2679 /* This is a .stab section. */
2680 elf_section_data (s)->this_hdr.sh_entsize =
125c4a69 2681 4 + 2 * bfd_get_arch_size (abfd) / 8;
252b5132
RH
2682 }
2683 }
2684 break;
2685
2686 case SHT_DYNAMIC:
2687 case SHT_DYNSYM:
2688 case SHT_GNU_verneed:
2689 case SHT_GNU_verdef:
2690 /* sh_link is the section header index of the string table
2691 used for the dynamic entries, or the symbol table, or the
2692 version strings. */
2693 s = bfd_get_section_by_name (abfd, ".dynstr");
2694 if (s != NULL)
2695 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2696 break;
2697
2698 case SHT_HASH:
2699 case SHT_GNU_versym:
2700 /* sh_link is the section header index of the symbol table
2701 this hash table or version table is for. */
2702 s = bfd_get_section_by_name (abfd, ".dynsym");
2703 if (s != NULL)
2704 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2705 break;
dbb410c3
AM
2706
2707 case SHT_GROUP:
2708 d->this_hdr.sh_link = t->symtab_section;
252b5132
RH
2709 }
2710 }
2711
2b0f7ef9 2712 for (secn = 1; secn < section_number; ++secn)
9ad5cbcf
AM
2713 if (i_shdrp[secn] == NULL)
2714 i_shdrp[secn] = i_shdrp[0];
2715 else
2716 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2717 i_shdrp[secn]->sh_name);
252b5132
RH
2718 return true;
2719}
2720
2721/* Map symbol from it's internal number to the external number, moving
2722 all local symbols to be at the head of the list. */
2723
2724static INLINE int
2725sym_is_global (abfd, sym)
2726 bfd *abfd;
2727 asymbol *sym;
2728{
2729 /* If the backend has a special mapping, use it. */
2730 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2731 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2732 (abfd, sym));
2733
2734 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2735 || bfd_is_und_section (bfd_get_section (sym))
2736 || bfd_is_com_section (bfd_get_section (sym)));
2737}
2738
2739static boolean
2740elf_map_symbols (abfd)
2741 bfd *abfd;
2742{
dc810e39 2743 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
2744 asymbol **syms = bfd_get_outsymbols (abfd);
2745 asymbol **sect_syms;
dc810e39
AM
2746 unsigned int num_locals = 0;
2747 unsigned int num_globals = 0;
2748 unsigned int num_locals2 = 0;
2749 unsigned int num_globals2 = 0;
252b5132 2750 int max_index = 0;
dc810e39 2751 unsigned int idx;
252b5132
RH
2752 asection *asect;
2753 asymbol **new_syms;
dc810e39 2754 bfd_size_type amt;
252b5132
RH
2755
2756#ifdef DEBUG
2757 fprintf (stderr, "elf_map_symbols\n");
2758 fflush (stderr);
2759#endif
2760
252b5132
RH
2761 for (asect = abfd->sections; asect; asect = asect->next)
2762 {
2763 if (max_index < asect->index)
2764 max_index = asect->index;
2765 }
2766
2767 max_index++;
dc810e39
AM
2768 amt = max_index * sizeof (asymbol *);
2769 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
252b5132
RH
2770 if (sect_syms == NULL)
2771 return false;
2772 elf_section_syms (abfd) = sect_syms;
4e89ac30 2773 elf_num_section_syms (abfd) = max_index;
252b5132 2774
079e9a2f
AM
2775 /* Init sect_syms entries for any section symbols we have already
2776 decided to output. */
252b5132
RH
2777 for (idx = 0; idx < symcount; idx++)
2778 {
dc810e39 2779 asymbol *sym = syms[idx];
c044fabd 2780
252b5132
RH
2781 if ((sym->flags & BSF_SECTION_SYM) != 0
2782 && sym->value == 0)
2783 {
2784 asection *sec;
2785
2786 sec = sym->section;
2787
2788 if (sec->owner != NULL)
2789 {
2790 if (sec->owner != abfd)
2791 {
2792 if (sec->output_offset != 0)
2793 continue;
c044fabd 2794
252b5132
RH
2795 sec = sec->output_section;
2796
079e9a2f
AM
2797 /* Empty sections in the input files may have had a
2798 section symbol created for them. (See the comment
2799 near the end of _bfd_generic_link_output_symbols in
2800 linker.c). If the linker script discards such
2801 sections then we will reach this point. Since we know
2802 that we cannot avoid this case, we detect it and skip
2803 the abort and the assignment to the sect_syms array.
2804 To reproduce this particular case try running the
2805 linker testsuite test ld-scripts/weak.exp for an ELF
2806 port that uses the generic linker. */
252b5132
RH
2807 if (sec->owner == NULL)
2808 continue;
2809
2810 BFD_ASSERT (sec->owner == abfd);
2811 }
2812 sect_syms[sec->index] = syms[idx];
2813 }
2814 }
2815 }
2816
252b5132
RH
2817 /* Classify all of the symbols. */
2818 for (idx = 0; idx < symcount; idx++)
2819 {
2820 if (!sym_is_global (abfd, syms[idx]))
2821 num_locals++;
2822 else
2823 num_globals++;
2824 }
079e9a2f
AM
2825
2826 /* We will be adding a section symbol for each BFD section. Most normal
2827 sections will already have a section symbol in outsymbols, but
2828 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2829 at least in that case. */
252b5132
RH
2830 for (asect = abfd->sections; asect; asect = asect->next)
2831 {
079e9a2f 2832 if (sect_syms[asect->index] == NULL)
252b5132 2833 {
079e9a2f 2834 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
2835 num_locals++;
2836 else
2837 num_globals++;
252b5132
RH
2838 }
2839 }
2840
2841 /* Now sort the symbols so the local symbols are first. */
dc810e39
AM
2842 amt = (num_locals + num_globals) * sizeof (asymbol *);
2843 new_syms = (asymbol **) bfd_alloc (abfd, amt);
2844
252b5132
RH
2845 if (new_syms == NULL)
2846 return false;
2847
2848 for (idx = 0; idx < symcount; idx++)
2849 {
2850 asymbol *sym = syms[idx];
dc810e39 2851 unsigned int i;
252b5132
RH
2852
2853 if (!sym_is_global (abfd, sym))
2854 i = num_locals2++;
2855 else
2856 i = num_locals + num_globals2++;
2857 new_syms[i] = sym;
2858 sym->udata.i = i + 1;
2859 }
2860 for (asect = abfd->sections; asect; asect = asect->next)
2861 {
079e9a2f 2862 if (sect_syms[asect->index] == NULL)
252b5132 2863 {
079e9a2f 2864 asymbol *sym = asect->symbol;
dc810e39 2865 unsigned int i;
252b5132 2866
079e9a2f 2867 sect_syms[asect->index] = sym;
252b5132
RH
2868 if (!sym_is_global (abfd, sym))
2869 i = num_locals2++;
2870 else
2871 i = num_locals + num_globals2++;
2872 new_syms[i] = sym;
2873 sym->udata.i = i + 1;
2874 }
2875 }
2876
2877 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2878
2879 elf_num_locals (abfd) = num_locals;
2880 elf_num_globals (abfd) = num_globals;
2881 return true;
2882}
2883
2884/* Align to the maximum file alignment that could be required for any
2885 ELF data structure. */
2886
2887static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2888static INLINE file_ptr
2889align_file_position (off, align)
2890 file_ptr off;
2891 int align;
2892{
2893 return (off + align - 1) & ~(align - 1);
2894}
2895
2896/* Assign a file position to a section, optionally aligning to the
2897 required section alignment. */
2898
2899INLINE file_ptr
2900_bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2901 Elf_Internal_Shdr *i_shdrp;
2902 file_ptr offset;
2903 boolean align;
2904{
2905 if (align)
2906 {
2907 unsigned int al;
2908
2909 al = i_shdrp->sh_addralign;
2910 if (al > 1)
2911 offset = BFD_ALIGN (offset, al);
2912 }
2913 i_shdrp->sh_offset = offset;
2914 if (i_shdrp->bfd_section != NULL)
2915 i_shdrp->bfd_section->filepos = offset;
2916 if (i_shdrp->sh_type != SHT_NOBITS)
2917 offset += i_shdrp->sh_size;
2918 return offset;
2919}
2920
2921/* Compute the file positions we are going to put the sections at, and
2922 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2923 is not NULL, this is being called by the ELF backend linker. */
2924
2925boolean
2926_bfd_elf_compute_section_file_positions (abfd, link_info)
2927 bfd *abfd;
2928 struct bfd_link_info *link_info;
2929{
2930 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2931 boolean failed;
2932 struct bfd_strtab_hash *strtab;
2933 Elf_Internal_Shdr *shstrtab_hdr;
2934
2935 if (abfd->output_has_begun)
2936 return true;
2937
2938 /* Do any elf backend specific processing first. */
2939 if (bed->elf_backend_begin_write_processing)
2940 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2941
2942 if (! prep_headers (abfd))
2943 return false;
2944
e6c51ed4
NC
2945 /* Post process the headers if necessary. */
2946 if (bed->elf_backend_post_process_headers)
2947 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2948
252b5132
RH
2949 failed = false;
2950 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2951 if (failed)
2952 return false;
2953
2954 if (!assign_section_numbers (abfd))
2955 return false;
2956
2957 /* The backend linker builds symbol table information itself. */
2958 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2959 {
2960 /* Non-zero if doing a relocatable link. */
2961 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2962
2963 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2964 return false;
2965 }
2966
1126897b 2967 if (link_info == NULL)
dbb410c3 2968 {
1126897b 2969 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3
AM
2970 if (failed)
2971 return false;
2972 }
2973
252b5132
RH
2974 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2975 /* sh_name was set in prep_headers. */
2976 shstrtab_hdr->sh_type = SHT_STRTAB;
2977 shstrtab_hdr->sh_flags = 0;
2978 shstrtab_hdr->sh_addr = 0;
2b0f7ef9 2979 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
252b5132
RH
2980 shstrtab_hdr->sh_entsize = 0;
2981 shstrtab_hdr->sh_link = 0;
2982 shstrtab_hdr->sh_info = 0;
2983 /* sh_offset is set in assign_file_positions_except_relocs. */
2984 shstrtab_hdr->sh_addralign = 1;
2985
2986 if (!assign_file_positions_except_relocs (abfd))
2987 return false;
2988
2989 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2990 {
2991 file_ptr off;
2992 Elf_Internal_Shdr *hdr;
2993
2994 off = elf_tdata (abfd)->next_file_pos;
2995
2996 hdr = &elf_tdata (abfd)->symtab_hdr;
2997 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2998
9ad5cbcf
AM
2999 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3000 if (hdr->sh_size != 0)
3001 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3002
252b5132
RH
3003 hdr = &elf_tdata (abfd)->strtab_hdr;
3004 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3005
3006 elf_tdata (abfd)->next_file_pos = off;
3007
3008 /* Now that we know where the .strtab section goes, write it
3009 out. */
3010 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3011 || ! _bfd_stringtab_emit (abfd, strtab))
3012 return false;
3013 _bfd_stringtab_free (strtab);
3014 }
3015
3016 abfd->output_has_begun = true;
3017
3018 return true;
3019}
3020
3021/* Create a mapping from a set of sections to a program segment. */
3022
3023static INLINE struct elf_segment_map *
3024make_mapping (abfd, sections, from, to, phdr)
3025 bfd *abfd;
3026 asection **sections;
3027 unsigned int from;
3028 unsigned int to;
3029 boolean phdr;
3030{
3031 struct elf_segment_map *m;
3032 unsigned int i;
3033 asection **hdrpp;
dc810e39 3034 bfd_size_type amt;
252b5132 3035
dc810e39
AM
3036 amt = sizeof (struct elf_segment_map);
3037 amt += (to - from - 1) * sizeof (asection *);
3038 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
3039 if (m == NULL)
3040 return NULL;
3041 m->next = NULL;
3042 m->p_type = PT_LOAD;
3043 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3044 m->sections[i - from] = *hdrpp;
3045 m->count = to - from;
3046
3047 if (from == 0 && phdr)
3048 {
3049 /* Include the headers in the first PT_LOAD segment. */
3050 m->includes_filehdr = 1;
3051 m->includes_phdrs = 1;
3052 }
3053
3054 return m;
3055}
3056
3057/* Set up a mapping from BFD sections to program segments. */
3058
3059static boolean
3060map_sections_to_segments (abfd)
3061 bfd *abfd;
3062{
3063 asection **sections = NULL;
3064 asection *s;
3065 unsigned int i;
3066 unsigned int count;
3067 struct elf_segment_map *mfirst;
3068 struct elf_segment_map **pm;
3069 struct elf_segment_map *m;
3070 asection *last_hdr;
3071 unsigned int phdr_index;
3072 bfd_vma maxpagesize;
3073 asection **hdrpp;
3074 boolean phdr_in_segment = true;
3075 boolean writable;
13ae64f3
JJ
3076 int tls_count = 0;
3077 asection *first_tls = NULL;
65765700 3078 asection *dynsec, *eh_frame_hdr;
dc810e39 3079 bfd_size_type amt;
252b5132
RH
3080
3081 if (elf_tdata (abfd)->segment_map != NULL)
3082 return true;
3083
3084 if (bfd_count_sections (abfd) == 0)
3085 return true;
3086
3087 /* Select the allocated sections, and sort them. */
3088
dc810e39
AM
3089 amt = bfd_count_sections (abfd) * sizeof (asection *);
3090 sections = (asection **) bfd_malloc (amt);
252b5132
RH
3091 if (sections == NULL)
3092 goto error_return;
3093
3094 i = 0;
3095 for (s = abfd->sections; s != NULL; s = s->next)
3096 {
3097 if ((s->flags & SEC_ALLOC) != 0)
3098 {
3099 sections[i] = s;
3100 ++i;
3101 }
3102 }
3103 BFD_ASSERT (i <= bfd_count_sections (abfd));
3104 count = i;
3105
3106 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3107
3108 /* Build the mapping. */
3109
3110 mfirst = NULL;
3111 pm = &mfirst;
3112
3113 /* If we have a .interp section, then create a PT_PHDR segment for
3114 the program headers and a PT_INTERP segment for the .interp
3115 section. */
3116 s = bfd_get_section_by_name (abfd, ".interp");
3117 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3118 {
dc810e39
AM
3119 amt = sizeof (struct elf_segment_map);
3120 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
3121 if (m == NULL)
3122 goto error_return;
3123 m->next = NULL;
3124 m->p_type = PT_PHDR;
3125 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3126 m->p_flags = PF_R | PF_X;
3127 m->p_flags_valid = 1;
3128 m->includes_phdrs = 1;
3129
3130 *pm = m;
3131 pm = &m->next;
3132
dc810e39
AM
3133 amt = sizeof (struct elf_segment_map);
3134 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
3135 if (m == NULL)
3136 goto error_return;
3137 m->next = NULL;
3138 m->p_type = PT_INTERP;
3139 m->count = 1;
3140 m->sections[0] = s;
3141
3142 *pm = m;
3143 pm = &m->next;
3144 }
3145
3146 /* Look through the sections. We put sections in the same program
3147 segment when the start of the second section can be placed within
3148 a few bytes of the end of the first section. */
3149 last_hdr = NULL;
3150 phdr_index = 0;
3151 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3152 writable = false;
3153 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3154 if (dynsec != NULL
3155 && (dynsec->flags & SEC_LOAD) == 0)
3156 dynsec = NULL;
3157
3158 /* Deal with -Ttext or something similar such that the first section
3159 is not adjacent to the program headers. This is an
3160 approximation, since at this point we don't know exactly how many
3161 program headers we will need. */
3162 if (count > 0)
3163 {
3164 bfd_size_type phdr_size;
3165
3166 phdr_size = elf_tdata (abfd)->program_header_size;
3167 if (phdr_size == 0)
3168 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3169 if ((abfd->flags & D_PAGED) == 0
3170 || sections[0]->lma < phdr_size
3171 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3172 phdr_in_segment = false;
3173 }
3174
3175 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3176 {
3177 asection *hdr;
3178 boolean new_segment;
3179
3180 hdr = *hdrpp;
3181
3182 /* See if this section and the last one will fit in the same
3183 segment. */
3184
3185 if (last_hdr == NULL)
3186 {
3187 /* If we don't have a segment yet, then we don't need a new
3188 one (we build the last one after this loop). */
3189 new_segment = false;
3190 }
3191 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3192 {
3193 /* If this section has a different relation between the
3194 virtual address and the load address, then we need a new
3195 segment. */
3196 new_segment = true;
3197 }
3198 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3199 < BFD_ALIGN (hdr->lma, maxpagesize))
3200 {
3201 /* If putting this section in this segment would force us to
3202 skip a page in the segment, then we need a new segment. */
3203 new_segment = true;
3204 }
3205 else if ((last_hdr->flags & SEC_LOAD) == 0
3206 && (hdr->flags & SEC_LOAD) != 0)
3207 {
3208 /* We don't want to put a loadable section after a
3209 nonloadable section in the same segment. */
3210 new_segment = true;
3211 }
3212 else if ((abfd->flags & D_PAGED) == 0)
3213 {
3214 /* If the file is not demand paged, which means that we
3215 don't require the sections to be correctly aligned in the
3216 file, then there is no other reason for a new segment. */
3217 new_segment = false;
3218 }
3219 else if (! writable
3220 && (hdr->flags & SEC_READONLY) == 0
3221 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3222 == hdr->lma))
3223 {
3224 /* We don't want to put a writable section in a read only
3225 segment, unless they are on the same page in memory
3226 anyhow. We already know that the last section does not
3227 bring us past the current section on the page, so the
3228 only case in which the new section is not on the same
3229 page as the previous section is when the previous section
3230 ends precisely on a page boundary. */
3231 new_segment = true;
3232 }
3233 else
3234 {
3235 /* Otherwise, we can use the same segment. */
3236 new_segment = false;
3237 }
3238
3239 if (! new_segment)
3240 {
3241 if ((hdr->flags & SEC_READONLY) == 0)
3242 writable = true;
3243 last_hdr = hdr;
3244 continue;
3245 }
3246
3247 /* We need a new program segment. We must create a new program
3248 header holding all the sections from phdr_index until hdr. */
3249
3250 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3251 if (m == NULL)
3252 goto error_return;
3253
3254 *pm = m;
3255 pm = &m->next;
3256
3257 if ((hdr->flags & SEC_READONLY) == 0)
3258 writable = true;
3259 else
3260 writable = false;
3261
3262 last_hdr = hdr;
3263 phdr_index = i;
3264 phdr_in_segment = false;
3265 }
3266
3267 /* Create a final PT_LOAD program segment. */
3268 if (last_hdr != NULL)
3269 {
3270 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3271 if (m == NULL)
3272 goto error_return;
3273
3274 *pm = m;
3275 pm = &m->next;
3276 }
3277
3278 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3279 if (dynsec != NULL)
3280 {
dc810e39
AM
3281 amt = sizeof (struct elf_segment_map);
3282 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
3283 if (m == NULL)
3284 goto error_return;
3285 m->next = NULL;
3286 m->p_type = PT_DYNAMIC;
3287 m->count = 1;
3288 m->sections[0] = dynsec;
3289
3290 *pm = m;
3291 pm = &m->next;
3292 }
3293
3294 /* For each loadable .note section, add a PT_NOTE segment. We don't
3295 use bfd_get_section_by_name, because if we link together
3296 nonloadable .note sections and loadable .note sections, we will
3297 generate two .note sections in the output file. FIXME: Using
3298 names for section types is bogus anyhow. */
3299 for (s = abfd->sections; s != NULL; s = s->next)
3300 {
3301 if ((s->flags & SEC_LOAD) != 0
3302 && strncmp (s->name, ".note", 5) == 0)
3303 {
dc810e39
AM
3304 amt = sizeof (struct elf_segment_map);
3305 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
3306 if (m == NULL)
3307 goto error_return;
3308 m->next = NULL;
3309 m->p_type = PT_NOTE;
3310 m->count = 1;
3311 m->sections[0] = s;
3312
3313 *pm = m;
3314 pm = &m->next;
3315 }
13ae64f3
JJ
3316 if (s->flags & SEC_THREAD_LOCAL)
3317 {
3318 if (! tls_count)
3319 first_tls = s;
3320 tls_count++;
3321 }
3322 }
3323
3324 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3325 if (tls_count > 0)
3326 {
3327 int i;
3328
3329 amt = sizeof (struct elf_segment_map);
3330 amt += (tls_count - 1) * sizeof (asection *);
3331 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3332 if (m == NULL)
3333 goto error_return;
3334 m->next = NULL;
3335 m->p_type = PT_TLS;
3336 m->count = tls_count;
3337 /* Mandated PF_R. */
3338 m->p_flags = PF_R;
3339 m->p_flags_valid = 1;
3340 for (i = 0; i < tls_count; ++i)
3341 {
3342 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3343 m->sections[i] = first_tls;
3344 first_tls = first_tls->next;
3345 }
3346
3347 *pm = m;
3348 pm = &m->next;
252b5132
RH
3349 }
3350
65765700
JJ
3351 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3352 segment. */
9ad5cbcf
AM
3353 eh_frame_hdr = NULL;
3354 if (elf_tdata (abfd)->eh_frame_hdr)
3355 eh_frame_hdr = bfd_get_section_by_name (abfd, ".eh_frame_hdr");
65765700
JJ
3356 if (eh_frame_hdr != NULL && (eh_frame_hdr->flags & SEC_LOAD))
3357 {
3358 amt = sizeof (struct elf_segment_map);
3359 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3360 if (m == NULL)
3361 goto error_return;
3362 m->next = NULL;
3363 m->p_type = PT_GNU_EH_FRAME;
3364 m->count = 1;
3365 m->sections[0] = eh_frame_hdr;
3366
3367 *pm = m;
3368 pm = &m->next;
3369 }
3370
252b5132
RH
3371 free (sections);
3372 sections = NULL;
3373
3374 elf_tdata (abfd)->segment_map = mfirst;
3375 return true;
3376
3377 error_return:
3378 if (sections != NULL)
3379 free (sections);
3380 return false;
3381}
3382
3383/* Sort sections by address. */
3384
3385static int
3386elf_sort_sections (arg1, arg2)
3387 const PTR arg1;
3388 const PTR arg2;
3389{
3390 const asection *sec1 = *(const asection **) arg1;
3391 const asection *sec2 = *(const asection **) arg2;
3392
3393 /* Sort by LMA first, since this is the address used to
3394 place the section into a segment. */
3395 if (sec1->lma < sec2->lma)
3396 return -1;
3397 else if (sec1->lma > sec2->lma)
3398 return 1;
3399
3400 /* Then sort by VMA. Normally the LMA and the VMA will be
3401 the same, and this will do nothing. */
3402 if (sec1->vma < sec2->vma)
3403 return -1;
3404 else if (sec1->vma > sec2->vma)
3405 return 1;
3406
3407 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3408
3409#define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3410
3411 if (TOEND (sec1))
3412 {
3413 if (TOEND (sec2))
00a7cdc5
NC
3414 {
3415 /* If the indicies are the same, do not return 0
3416 here, but continue to try the next comparison. */
3417 if (sec1->target_index - sec2->target_index != 0)
3418 return sec1->target_index - sec2->target_index;
3419 }
252b5132
RH
3420 else
3421 return 1;
3422 }
00a7cdc5 3423 else if (TOEND (sec2))
252b5132
RH
3424 return -1;
3425
3426#undef TOEND
3427
00a7cdc5
NC
3428 /* Sort by size, to put zero sized sections
3429 before others at the same address. */
252b5132
RH
3430
3431 if (sec1->_raw_size < sec2->_raw_size)
3432 return -1;
3433 if (sec1->_raw_size > sec2->_raw_size)
3434 return 1;
3435
3436 return sec1->target_index - sec2->target_index;
3437}
3438
3439/* Assign file positions to the sections based on the mapping from
3440 sections to segments. This function also sets up some fields in
3441 the file header, and writes out the program headers. */
3442
3443static boolean
3444assign_file_positions_for_segments (abfd)
3445 bfd *abfd;
3446{
3447 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3448 unsigned int count;
3449 struct elf_segment_map *m;
3450 unsigned int alloc;
3451 Elf_Internal_Phdr *phdrs;
3452 file_ptr off, voff;
3453 bfd_vma filehdr_vaddr, filehdr_paddr;
3454 bfd_vma phdrs_vaddr, phdrs_paddr;
3455 Elf_Internal_Phdr *p;
dc810e39 3456 bfd_size_type amt;
252b5132
RH
3457
3458 if (elf_tdata (abfd)->segment_map == NULL)
3459 {
3460 if (! map_sections_to_segments (abfd))
3461 return false;
3462 }
1ed89aa9
NC
3463 else
3464 {
3465 /* The placement algorithm assumes that non allocated sections are
3466 not in PT_LOAD segments. We ensure this here by removing such
3467 sections from the segment map. */
3468 for (m = elf_tdata (abfd)->segment_map;
3469 m != NULL;
3470 m = m->next)
3471 {
3472 unsigned int new_count;
3473 unsigned int i;
3474
3475 if (m->p_type != PT_LOAD)
3476 continue;
3477
3478 new_count = 0;
3479 for (i = 0; i < m->count; i ++)
3480 {
3481 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3482 {
3483 if (i != new_count)
3484 m->sections[new_count] = m->sections[i];
3485
3486 new_count ++;
3487 }
3488 }
3489
3490 if (new_count != m->count)
3491 m->count = new_count;
3492 }
3493 }
252b5132
RH
3494
3495 if (bed->elf_backend_modify_segment_map)
3496 {
3497 if (! (*bed->elf_backend_modify_segment_map) (abfd))
3498 return false;
3499 }
3500
3501 count = 0;
3502 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3503 ++count;
3504
3505 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3506 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3507 elf_elfheader (abfd)->e_phnum = count;
3508
3509 if (count == 0)
3510 return true;
3511
3512 /* If we already counted the number of program segments, make sure
3513 that we allocated enough space. This happens when SIZEOF_HEADERS
3514 is used in a linker script. */
3515 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3516 if (alloc != 0 && count > alloc)
3517 {
3518 ((*_bfd_error_handler)
3519 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3520 bfd_get_filename (abfd), alloc, count));
3521 bfd_set_error (bfd_error_bad_value);
3522 return false;
3523 }
3524
3525 if (alloc == 0)
3526 alloc = count;
3527
dc810e39
AM
3528 amt = alloc * sizeof (Elf_Internal_Phdr);
3529 phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
252b5132
RH
3530 if (phdrs == NULL)
3531 return false;
3532
3533 off = bed->s->sizeof_ehdr;
3534 off += alloc * bed->s->sizeof_phdr;
3535
3536 filehdr_vaddr = 0;
3537 filehdr_paddr = 0;
3538 phdrs_vaddr = 0;
3539 phdrs_paddr = 0;
3540
3541 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3542 m != NULL;
3543 m = m->next, p++)
3544 {
3545 unsigned int i;
3546 asection **secpp;
3547
3548 /* If elf_segment_map is not from map_sections_to_segments, the
52e9b619
MS
3549 sections may not be correctly ordered. NOTE: sorting should
3550 not be done to the PT_NOTE section of a corefile, which may
3551 contain several pseudo-sections artificially created by bfd.
3552 Sorting these pseudo-sections breaks things badly. */
3553 if (m->count > 1
3554 && !(elf_elfheader (abfd)->e_type == ET_CORE
3555 && m->p_type == PT_NOTE))
252b5132
RH
3556 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3557 elf_sort_sections);
3558
3559 p->p_type = m->p_type;
28a7f3e7 3560 p->p_flags = m->p_flags;
252b5132
RH
3561
3562 if (p->p_type == PT_LOAD
3563 && m->count > 0
3564 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3565 {
3566 if ((abfd->flags & D_PAGED) != 0)
3567 off += (m->sections[0]->vma - off) % bed->maxpagesize;
3568 else
3569 {
3570 bfd_size_type align;
3571
3572 align = 0;
3573 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3574 {
3575 bfd_size_type secalign;
3576
3577 secalign = bfd_get_section_alignment (abfd, *secpp);
3578 if (secalign > align)
3579 align = secalign;
3580 }
3581
3582 off += (m->sections[0]->vma - off) % (1 << align);
3583 }
3584 }
3585
3586 if (m->count == 0)
3587 p->p_vaddr = 0;
3588 else
3589 p->p_vaddr = m->sections[0]->vma;
3590
3591 if (m->p_paddr_valid)
3592 p->p_paddr = m->p_paddr;
3593 else if (m->count == 0)
3594 p->p_paddr = 0;
3595 else
3596 p->p_paddr = m->sections[0]->lma;
3597
3598 if (p->p_type == PT_LOAD
3599 && (abfd->flags & D_PAGED) != 0)
3600 p->p_align = bed->maxpagesize;
3601 else if (m->count == 0)
3602 p->p_align = bed->s->file_align;
3603 else
3604 p->p_align = 0;
3605
3606 p->p_offset = 0;
3607 p->p_filesz = 0;
3608 p->p_memsz = 0;
3609
3610 if (m->includes_filehdr)
3611 {
3612 if (! m->p_flags_valid)
3613 p->p_flags |= PF_R;
3614 p->p_offset = 0;
3615 p->p_filesz = bed->s->sizeof_ehdr;
3616 p->p_memsz = bed->s->sizeof_ehdr;
3617 if (m->count > 0)
3618 {
3619 BFD_ASSERT (p->p_type == PT_LOAD);
3620
3621 if (p->p_vaddr < (bfd_vma) off)
3622 {
caf47ea6
AM
3623 (*_bfd_error_handler)
3624 (_("%s: Not enough room for program headers, try linking with -N"),
3625 bfd_get_filename (abfd));
252b5132
RH
3626 bfd_set_error (bfd_error_bad_value);
3627 return false;
3628 }
3629
3630 p->p_vaddr -= off;
3631 if (! m->p_paddr_valid)
3632 p->p_paddr -= off;
3633 }
3634 if (p->p_type == PT_LOAD)
3635 {
3636 filehdr_vaddr = p->p_vaddr;
3637 filehdr_paddr = p->p_paddr;
3638 }
3639 }
3640
3641 if (m->includes_phdrs)
3642 {
3643 if (! m->p_flags_valid)
3644 p->p_flags |= PF_R;
3645
3646 if (m->includes_filehdr)
3647 {
3648 if (p->p_type == PT_LOAD)
3649 {
3650 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3651 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3652 }
3653 }
3654 else
3655 {
3656 p->p_offset = bed->s->sizeof_ehdr;
3657
3658 if (m->count > 0)
3659 {
3660 BFD_ASSERT (p->p_type == PT_LOAD);
3661 p->p_vaddr -= off - p->p_offset;
3662 if (! m->p_paddr_valid)
3663 p->p_paddr -= off - p->p_offset;
3664 }
3665
3666 if (p->p_type == PT_LOAD)
3667 {
3668 phdrs_vaddr = p->p_vaddr;
3669 phdrs_paddr = p->p_paddr;
3670 }
3671 else
3672 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3673 }
3674
3675 p->p_filesz += alloc * bed->s->sizeof_phdr;
3676 p->p_memsz += alloc * bed->s->sizeof_phdr;
3677 }
3678
3679 if (p->p_type == PT_LOAD
3680 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3681 {
3682 if (! m->includes_filehdr && ! m->includes_phdrs)
3683 p->p_offset = off;
3684 else
3685 {
3686 file_ptr adjust;
3687
3688 adjust = off - (p->p_offset + p->p_filesz);
3689 p->p_filesz += adjust;
3690 p->p_memsz += adjust;
3691 }
3692 }
3693
3694 voff = off;
3695
3696 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3697 {
3698 asection *sec;
3699 flagword flags;
3700 bfd_size_type align;
3701
3702 sec = *secpp;
3703 flags = sec->flags;
3704 align = 1 << bfd_get_section_alignment (abfd, sec);
3705
3706 /* The section may have artificial alignment forced by a
3707 link script. Notice this case by the gap between the
f5ffc919
NC
3708 cumulative phdr lma and the section's lma. */
3709 if (p->p_paddr + p->p_memsz < sec->lma)
252b5132 3710 {
f5ffc919 3711 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
252b5132
RH
3712
3713 p->p_memsz += adjust;
3714 off += adjust;
3715 voff += adjust;
3716 if ((flags & SEC_LOAD) != 0)
3717 p->p_filesz += adjust;
3718 }
3719
3720 if (p->p_type == PT_LOAD)
3721 {
3722 bfd_signed_vma adjust;
3723
3724 if ((flags & SEC_LOAD) != 0)
3725 {
3726 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3727 if (adjust < 0)
3728 adjust = 0;
3729 }
3730 else if ((flags & SEC_ALLOC) != 0)
3731 {
3732 /* The section VMA must equal the file position
3733 modulo the page size. FIXME: I'm not sure if
3734 this adjustment is really necessary. We used to
3735 not have the SEC_LOAD case just above, and then
3736 this was necessary, but now I'm not sure. */
3737 if ((abfd->flags & D_PAGED) != 0)
3738 adjust = (sec->vma - voff) % bed->maxpagesize;
3739 else
3740 adjust = (sec->vma - voff) % align;
3741 }
3742 else
3743 adjust = 0;
3744
3745 if (adjust != 0)
3746 {
3747 if (i == 0)
3748 {
cdc7c09f
NC
3749 (* _bfd_error_handler) (_("\
3750Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3751 bfd_section_name (abfd, sec),
3752 sec->lma,
3753 p->p_paddr);
252b5132
RH
3754 return false;
3755 }
3756 p->p_memsz += adjust;
3757 off += adjust;
3758 voff += adjust;
3759 if ((flags & SEC_LOAD) != 0)
3760 p->p_filesz += adjust;
3761 }
3762
3763 sec->filepos = off;
3764
3765 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3766 used in a linker script we may have a section with
3767 SEC_LOAD clear but which is supposed to have
3768 contents. */
3769 if ((flags & SEC_LOAD) != 0
3770 || (flags & SEC_HAS_CONTENTS) != 0)
3771 off += sec->_raw_size;
3772
3773 if ((flags & SEC_ALLOC) != 0)
3774 voff += sec->_raw_size;
3775 }
3776
3777 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3778 {
4a938328
MS
3779 /* The actual "note" segment has i == 0.
3780 This is the one that actually contains everything. */
3781 if (i == 0)
3782 {
252b5132
RH
3783 sec->filepos = off;
3784 p->p_filesz = sec->_raw_size;
3785 off += sec->_raw_size;
3786 voff = off;
3787 }
4a938328 3788 else
252b5132 3789 {
4a938328 3790 /* Fake sections -- don't need to be written. */
252b5132
RH
3791 sec->filepos = 0;
3792 sec->_raw_size = 0;
4a938328 3793 flags = sec->flags = 0;
252b5132
RH
3794 }
3795 p->p_memsz = 0;
3796 p->p_align = 1;
3797 }
3798 else
3799 {
3800 p->p_memsz += sec->_raw_size;
3801
3802 if ((flags & SEC_LOAD) != 0)
3803 p->p_filesz += sec->_raw_size;
3804
13ae64f3
JJ
3805 if (p->p_type == PT_TLS
3806 && sec->_raw_size == 0
3807 && (sec->flags & SEC_HAS_CONTENTS) == 0)
3808 {
3809 struct bfd_link_order *o;
3810 bfd_vma tbss_size = 0;
3811
3812 for (o = sec->link_order_head; o != NULL; o = o->next)
3813 if (tbss_size < o->offset + o->size)
3814 tbss_size = o->offset + o->size;
3815
3816 p->p_memsz += tbss_size;
3817 }
3818
252b5132
RH
3819 if (align > p->p_align
3820 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3821 p->p_align = align;
3822 }
3823
3824 if (! m->p_flags_valid)
3825 {
3826 p->p_flags |= PF_R;
3827 if ((flags & SEC_CODE) != 0)
3828 p->p_flags |= PF_X;
3829 if ((flags & SEC_READONLY) == 0)
3830 p->p_flags |= PF_W;
3831 }
3832 }
3833 }
3834
3835 /* Now that we have set the section file positions, we can set up
3836 the file positions for the non PT_LOAD segments. */
3837 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3838 m != NULL;
3839 m = m->next, p++)
3840 {
3841 if (p->p_type != PT_LOAD && m->count > 0)
3842 {
3843 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3844 p->p_offset = m->sections[0]->filepos;
3845 }
3846 if (m->count == 0)
3847 {
3848 if (m->includes_filehdr)
3849 {
3850 p->p_vaddr = filehdr_vaddr;
3851 if (! m->p_paddr_valid)
3852 p->p_paddr = filehdr_paddr;
3853 }
3854 else if (m->includes_phdrs)
3855 {
3856 p->p_vaddr = phdrs_vaddr;
3857 if (! m->p_paddr_valid)
3858 p->p_paddr = phdrs_paddr;
3859 }
3860 }
3861 }
3862
caf47ea6
AM
3863 /* If additional nonloadable filepos adjustments are required,
3864 do them now. */
3865 if (bed->set_nonloadable_filepos)
3866 (*bed->set_nonloadable_filepos) (abfd, phdrs);
3867
252b5132
RH
3868 /* Clear out any program headers we allocated but did not use. */
3869 for (; count < alloc; count++, p++)
3870 {
3871 memset (p, 0, sizeof *p);
3872 p->p_type = PT_NULL;
3873 }
3874
3875 elf_tdata (abfd)->phdr = phdrs;
3876
3877 elf_tdata (abfd)->next_file_pos = off;
3878
3879 /* Write out the program headers. */
dc810e39 3880 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
252b5132
RH
3881 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3882 return false;
3883
3884 return true;
3885}
3886
3887/* Get the size of the program header.
3888
3889 If this is called by the linker before any of the section VMA's are set, it
3890 can't calculate the correct value for a strange memory layout. This only
3891 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3892 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3893 data segment (exclusive of .interp and .dynamic).
3894
3895 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3896 will be two segments. */
3897
3898static bfd_size_type
3899get_program_header_size (abfd)
3900 bfd *abfd;
3901{
3902 size_t segs;
3903 asection *s;
3904 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3905
3906 /* We can't return a different result each time we're called. */
3907 if (elf_tdata (abfd)->program_header_size != 0)
3908 return elf_tdata (abfd)->program_header_size;
3909
3910 if (elf_tdata (abfd)->segment_map != NULL)
3911 {
3912 struct elf_segment_map *m;
3913
3914 segs = 0;
3915 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3916 ++segs;
3917 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3918 return elf_tdata (abfd)->program_header_size;
3919 }
3920
3921 /* Assume we will need exactly two PT_LOAD segments: one for text
3922 and one for data. */
3923 segs = 2;
3924
3925 s = bfd_get_section_by_name (abfd, ".interp");
3926 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3927 {
3928 /* If we have a loadable interpreter section, we need a
3929 PT_INTERP segment. In this case, assume we also need a
3930 PT_PHDR segment, although that may not be true for all
3931 targets. */
3932 segs += 2;
3933 }
3934
3935 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3936 {
3937 /* We need a PT_DYNAMIC segment. */
3938 ++segs;
3939 }
3940
65765700
JJ
3941 if (elf_tdata (abfd)->eh_frame_hdr
3942 && bfd_get_section_by_name (abfd, ".eh_frame_hdr") != NULL)
3943 {
3944 /* We need a PT_GNU_EH_FRAME segment. */
3945 ++segs;
3946 }
3947
252b5132
RH
3948 for (s = abfd->sections; s != NULL; s = s->next)
3949 {
3950 if ((s->flags & SEC_LOAD) != 0
3951 && strncmp (s->name, ".note", 5) == 0)
3952 {
3953 /* We need a PT_NOTE segment. */
3954 ++segs;
3955 }
3956 }
3957
13ae64f3
JJ
3958 for (s = abfd->sections; s != NULL; s = s->next)
3959 {
3960 if (s->flags & SEC_THREAD_LOCAL)
3961 {
3962 /* We need a PT_TLS segment. */
3963 ++segs;
3964 break;
3965 }
3966 }
3967
252b5132
RH
3968 /* Let the backend count up any program headers it might need. */
3969 if (bed->elf_backend_additional_program_headers)
3970 {
3971 int a;
3972
3973 a = (*bed->elf_backend_additional_program_headers) (abfd);
3974 if (a == -1)
3975 abort ();
3976 segs += a;
3977 }
3978
3979 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3980 return elf_tdata (abfd)->program_header_size;
3981}
3982
3983/* Work out the file positions of all the sections. This is called by
3984 _bfd_elf_compute_section_file_positions. All the section sizes and
3985 VMAs must be known before this is called.
3986
3987 We do not consider reloc sections at this point, unless they form
3988 part of the loadable image. Reloc sections are assigned file
3989 positions in assign_file_positions_for_relocs, which is called by
3990 write_object_contents and final_link.
3991
3992 We also don't set the positions of the .symtab and .strtab here. */
3993
3994static boolean
3995assign_file_positions_except_relocs (abfd)
3996 bfd *abfd;
3997{
3998 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3999 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4000 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
9ad5cbcf 4001 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
4002 file_ptr off;
4003 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4004
4005 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4006 && bfd_get_format (abfd) != bfd_core)
4007 {
4008 Elf_Internal_Shdr **hdrpp;
4009 unsigned int i;
4010
4011 /* Start after the ELF header. */
4012 off = i_ehdrp->e_ehsize;
4013
4014 /* We are not creating an executable, which means that we are
4015 not creating a program header, and that the actual order of
4016 the sections in the file is unimportant. */
9ad5cbcf 4017 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
4018 {
4019 Elf_Internal_Shdr *hdr;
4020
4021 hdr = *hdrpp;
9ad5cbcf
AM
4022 if (hdr->sh_type == SHT_REL
4023 || hdr->sh_type == SHT_RELA
4024 || i == tdata->symtab_section
4025 || i == tdata->symtab_shndx_section
252b5132
RH
4026 || i == tdata->strtab_section)
4027 {
4028 hdr->sh_offset = -1;
252b5132 4029 }
9ad5cbcf
AM
4030 else
4031 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 4032
9ad5cbcf
AM
4033 if (i == SHN_LORESERVE - 1)
4034 {
4035 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4036 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4037 }
252b5132
RH
4038 }
4039 }
4040 else
4041 {
4042 unsigned int i;
4043 Elf_Internal_Shdr **hdrpp;
4044
4045 /* Assign file positions for the loaded sections based on the
4046 assignment of sections to segments. */
4047 if (! assign_file_positions_for_segments (abfd))
4048 return false;
4049
4050 /* Assign file positions for the other sections. */
4051
4052 off = elf_tdata (abfd)->next_file_pos;
9ad5cbcf 4053 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
4054 {
4055 Elf_Internal_Shdr *hdr;
4056
4057 hdr = *hdrpp;
4058 if (hdr->bfd_section != NULL
4059 && hdr->bfd_section->filepos != 0)
4060 hdr->sh_offset = hdr->bfd_section->filepos;
4061 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4062 {
4063 ((*_bfd_error_handler)
4064 (_("%s: warning: allocated section `%s' not in segment"),
4065 bfd_get_filename (abfd),
4066 (hdr->bfd_section == NULL
4067 ? "*unknown*"
4068 : hdr->bfd_section->name)));
4069 if ((abfd->flags & D_PAGED) != 0)
4070 off += (hdr->sh_addr - off) % bed->maxpagesize;
4071 else
4072 off += (hdr->sh_addr - off) % hdr->sh_addralign;
4073 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4074 false);
4075 }
4076 else if (hdr->sh_type == SHT_REL
4077 || hdr->sh_type == SHT_RELA
4078 || hdr == i_shdrpp[tdata->symtab_section]
9ad5cbcf 4079 || hdr == i_shdrpp[tdata->symtab_shndx_section]
252b5132
RH
4080 || hdr == i_shdrpp[tdata->strtab_section])
4081 hdr->sh_offset = -1;
4082 else
4083 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
9ad5cbcf
AM
4084
4085 if (i == SHN_LORESERVE - 1)
4086 {
4087 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4088 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4089 }
252b5132
RH
4090 }
4091 }
4092
4093 /* Place the section headers. */
4094 off = align_file_position (off, bed->s->file_align);
4095 i_ehdrp->e_shoff = off;
4096 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4097
4098 elf_tdata (abfd)->next_file_pos = off;
4099
4100 return true;
4101}
4102
4103static boolean
4104prep_headers (abfd)
4105 bfd *abfd;
4106{
4107 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4108 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4109 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2b0f7ef9 4110 struct elf_strtab_hash *shstrtab;
252b5132
RH
4111 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4112
4113 i_ehdrp = elf_elfheader (abfd);
4114 i_shdrp = elf_elfsections (abfd);
4115
2b0f7ef9 4116 shstrtab = _bfd_elf_strtab_init ();
252b5132
RH
4117 if (shstrtab == NULL)
4118 return false;
4119
4120 elf_shstrtab (abfd) = shstrtab;
4121
4122 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4123 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4124 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4125 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4126
4127 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4128 i_ehdrp->e_ident[EI_DATA] =
4129 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4130 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4131
252b5132
RH
4132 if ((abfd->flags & DYNAMIC) != 0)
4133 i_ehdrp->e_type = ET_DYN;
4134 else if ((abfd->flags & EXEC_P) != 0)
4135 i_ehdrp->e_type = ET_EXEC;
4136 else if (bfd_get_format (abfd) == bfd_core)
4137 i_ehdrp->e_type = ET_CORE;
4138 else
4139 i_ehdrp->e_type = ET_REL;
4140
4141 switch (bfd_get_arch (abfd))
4142 {
4143 case bfd_arch_unknown:
4144 i_ehdrp->e_machine = EM_NONE;
4145 break;
aa4f99bb
AO
4146
4147 /* There used to be a long list of cases here, each one setting
4148 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4149 in the corresponding bfd definition. To avoid duplication,
4150 the switch was removed. Machines that need special handling
4151 can generally do it in elf_backend_final_write_processing(),
4152 unless they need the information earlier than the final write.
4153 Such need can generally be supplied by replacing the tests for
4154 e_machine with the conditions used to determine it. */
252b5132 4155 default:
aa4f99bb
AO
4156 if (get_elf_backend_data (abfd) != NULL)
4157 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;
4158 else
4159 i_ehdrp->e_machine = EM_NONE;
4160 }
4161
252b5132
RH
4162 i_ehdrp->e_version = bed->s->ev_current;
4163 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4164
c044fabd 4165 /* No program header, for now. */
252b5132
RH
4166 i_ehdrp->e_phoff = 0;
4167 i_ehdrp->e_phentsize = 0;
4168 i_ehdrp->e_phnum = 0;
4169
c044fabd 4170 /* Each bfd section is section header entry. */
252b5132
RH
4171 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4172 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4173
c044fabd 4174 /* If we're building an executable, we'll need a program header table. */
252b5132
RH
4175 if (abfd->flags & EXEC_P)
4176 {
c044fabd 4177 /* It all happens later. */
252b5132
RH
4178#if 0
4179 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
4180
4181 /* elf_build_phdrs() returns a (NULL-terminated) array of
c044fabd 4182 Elf_Internal_Phdrs. */
252b5132
RH
4183 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
4184 i_ehdrp->e_phoff = outbase;
4185 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
4186#endif
4187 }
4188 else
4189 {
4190 i_ehdrp->e_phentsize = 0;
4191 i_phdrp = 0;
4192 i_ehdrp->e_phoff = 0;
4193 }
4194
4195 elf_tdata (abfd)->symtab_hdr.sh_name =
2b0f7ef9 4196 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
252b5132 4197 elf_tdata (abfd)->strtab_hdr.sh_name =
2b0f7ef9 4198 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
252b5132 4199 elf_tdata (abfd)->shstrtab_hdr.sh_name =
2b0f7ef9 4200 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
252b5132
RH
4201 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4202 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4203 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4204 return false;
4205
4206 return true;
4207}
4208
4209/* Assign file positions for all the reloc sections which are not part
4210 of the loadable file image. */
4211
4212void
4213_bfd_elf_assign_file_positions_for_relocs (abfd)
4214 bfd *abfd;
4215{
4216 file_ptr off;
9ad5cbcf 4217 unsigned int i, num_sec;
252b5132
RH
4218 Elf_Internal_Shdr **shdrpp;
4219
4220 off = elf_tdata (abfd)->next_file_pos;
4221
9ad5cbcf
AM
4222 num_sec = elf_numsections (abfd);
4223 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
252b5132
RH
4224 {
4225 Elf_Internal_Shdr *shdrp;
4226
4227 shdrp = *shdrpp;
4228 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4229 && shdrp->sh_offset == -1)
4230 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
4231 }
4232
4233 elf_tdata (abfd)->next_file_pos = off;
4234}
4235
4236boolean
4237_bfd_elf_write_object_contents (abfd)
4238 bfd *abfd;
4239{
4240 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4241 Elf_Internal_Ehdr *i_ehdrp;
4242 Elf_Internal_Shdr **i_shdrp;
4243 boolean failed;
9ad5cbcf 4244 unsigned int count, num_sec;
252b5132
RH
4245
4246 if (! abfd->output_has_begun
4247 && ! _bfd_elf_compute_section_file_positions
4248 (abfd, (struct bfd_link_info *) NULL))
4249 return false;
4250
4251 i_shdrp = elf_elfsections (abfd);
4252 i_ehdrp = elf_elfheader (abfd);
4253
4254 failed = false;
4255 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4256 if (failed)
4257 return false;
4258
4259 _bfd_elf_assign_file_positions_for_relocs (abfd);
4260
c044fabd 4261 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
4262 num_sec = elf_numsections (abfd);
4263 for (count = 1; count < num_sec; count++)
252b5132
RH
4264 {
4265 if (bed->elf_backend_section_processing)
4266 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4267 if (i_shdrp[count]->contents)
4268 {
dc810e39
AM
4269 bfd_size_type amt = i_shdrp[count]->sh_size;
4270
252b5132 4271 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 4272 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
252b5132
RH
4273 return false;
4274 }
9ad5cbcf
AM
4275 if (count == SHN_LORESERVE - 1)
4276 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132
RH
4277 }
4278
4279 /* Write out the section header names. */
4280 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
2b0f7ef9 4281 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
252b5132
RH
4282 return false;
4283
4284 if (bed->elf_backend_final_write_processing)
4285 (*bed->elf_backend_final_write_processing) (abfd,
4286 elf_tdata (abfd)->linker);
4287
4288 return bed->s->write_shdrs_and_ehdr (abfd);
4289}
4290
4291boolean
4292_bfd_elf_write_corefile_contents (abfd)
4293 bfd *abfd;
4294{
c044fabd 4295 /* Hopefully this can be done just like an object file. */
252b5132
RH
4296 return _bfd_elf_write_object_contents (abfd);
4297}
c044fabd
KH
4298
4299/* Given a section, search the header to find them. */
4300
252b5132
RH
4301int
4302_bfd_elf_section_from_bfd_section (abfd, asect)
4303 bfd *abfd;
4304 struct sec *asect;
4305{
af746e92 4306 struct elf_backend_data *bed;
252b5132 4307 int index;
252b5132 4308
9ad5cbcf
AM
4309 if (elf_section_data (asect) != NULL
4310 && elf_section_data (asect)->this_idx != 0)
4311 return elf_section_data (asect)->this_idx;
4312
4313 if (bfd_is_abs_section (asect))
af746e92
AM
4314 index = SHN_ABS;
4315 else if (bfd_is_com_section (asect))
4316 index = SHN_COMMON;
4317 else if (bfd_is_und_section (asect))
4318 index = SHN_UNDEF;
4319 else
252b5132 4320 {
af746e92
AM
4321 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4322 int maxindex = elf_numsections (abfd);
4323
4324 for (index = 1; index < maxindex; index++)
4325 {
4326 Elf_Internal_Shdr *hdr = i_shdrp[index];
4327
4328 if (hdr != NULL && hdr->bfd_section == asect)
4329 return index;
4330 }
4331 index = -1;
252b5132
RH
4332 }
4333
af746e92 4334 bed = get_elf_backend_data (abfd);
252b5132
RH
4335 if (bed->elf_backend_section_from_bfd_section)
4336 {
af746e92 4337 int retval = index;
9ad5cbcf 4338
af746e92
AM
4339 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4340 return retval;
252b5132
RH
4341 }
4342
af746e92
AM
4343 if (index == -1)
4344 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 4345
af746e92 4346 return index;
252b5132
RH
4347}
4348
4349/* Given a BFD symbol, return the index in the ELF symbol table, or -1
4350 on error. */
4351
4352int
4353_bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
4354 bfd *abfd;
4355 asymbol **asym_ptr_ptr;
4356{
4357 asymbol *asym_ptr = *asym_ptr_ptr;
4358 int idx;
4359 flagword flags = asym_ptr->flags;
4360
4361 /* When gas creates relocations against local labels, it creates its
4362 own symbol for the section, but does put the symbol into the
4363 symbol chain, so udata is 0. When the linker is generating
4364 relocatable output, this section symbol may be for one of the
4365 input sections rather than the output section. */
4366 if (asym_ptr->udata.i == 0
4367 && (flags & BSF_SECTION_SYM)
4368 && asym_ptr->section)
4369 {
4370 int indx;
4371
4372 if (asym_ptr->section->output_section != NULL)
4373 indx = asym_ptr->section->output_section->index;
4374 else
4375 indx = asym_ptr->section->index;
4e89ac30
L
4376 if (indx < elf_num_section_syms (abfd)
4377 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
4378 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4379 }
4380
4381 idx = asym_ptr->udata.i;
4382
4383 if (idx == 0)
4384 {
4385 /* This case can occur when using --strip-symbol on a symbol
4386 which is used in a relocation entry. */
4387 (*_bfd_error_handler)
4388 (_("%s: symbol `%s' required but not present"),
8f615d07 4389 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
252b5132
RH
4390 bfd_set_error (bfd_error_no_symbols);
4391 return -1;
4392 }
4393
4394#if DEBUG & 4
4395 {
4396 fprintf (stderr,
661a3fd4 4397 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
252b5132
RH
4398 (long) asym_ptr, asym_ptr->name, idx, flags,
4399 elf_symbol_flags (flags));
4400 fflush (stderr);
4401 }
4402#endif
4403
4404 return idx;
4405}
4406
4407/* Copy private BFD data. This copies any program header information. */
4408
4409static boolean
4410copy_private_bfd_data (ibfd, obfd)
4411 bfd *ibfd;
4412 bfd *obfd;
4413{
bc67d8a6
NC
4414 Elf_Internal_Ehdr * iehdr;
4415 struct elf_segment_map * map;
4416 struct elf_segment_map * map_first;
4417 struct elf_segment_map ** pointer_to_map;
4418 Elf_Internal_Phdr * segment;
4419 asection * section;
4420 unsigned int i;
4421 unsigned int num_segments;
4422 boolean phdr_included = false;
4423 bfd_vma maxpagesize;
4424 struct elf_segment_map * phdr_adjust_seg = NULL;
4425 unsigned int phdr_adjust_num = 0;
caf47ea6 4426 struct elf_backend_data * bed;
bc67d8a6 4427
c044fabd 4428 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
252b5132
RH
4429 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4430 return true;
4431
4432 if (elf_tdata (ibfd)->phdr == NULL)
4433 return true;
4434
caf47ea6 4435 bed = get_elf_backend_data (ibfd);
252b5132
RH
4436 iehdr = elf_elfheader (ibfd);
4437
bc67d8a6 4438 map_first = NULL;
c044fabd 4439 pointer_to_map = &map_first;
252b5132
RH
4440
4441 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
4442 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4443
4444 /* Returns the end address of the segment + 1. */
4445#define SEGMENT_END(segment, start) \
4446 (start + (segment->p_memsz > segment->p_filesz \
4447 ? segment->p_memsz : segment->p_filesz))
4448
4449 /* Returns true if the given section is contained within
4450 the given segment. VMA addresses are compared. */
4451#define IS_CONTAINED_BY_VMA(section, segment) \
4452 (section->vma >= segment->p_vaddr \
4453 && (section->vma + section->_raw_size) \
4454 <= (SEGMENT_END (segment, segment->p_vaddr)))
c044fabd 4455
bc67d8a6
NC
4456 /* Returns true if the given section is contained within
4457 the given segment. LMA addresses are compared. */
4458#define IS_CONTAINED_BY_LMA(section, segment, base) \
4459 (section->lma >= base \
4460 && (section->lma + section->_raw_size) \
4461 <= SEGMENT_END (segment, base))
252b5132 4462
caf47ea6
AM
4463 /* Returns true if the given section is contained within the
4464 given segment. Filepos addresses are compared in an elf
4465 backend function. */
4466#define IS_CONTAINED_BY_FILEPOS(sec, seg, bed) \
4467 (bed->is_contained_by_filepos \
4468 && (*bed->is_contained_by_filepos) (sec, seg))
4469
c044fabd 4470 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
bc67d8a6
NC
4471#define IS_COREFILE_NOTE(p, s) \
4472 (p->p_type == PT_NOTE \
4473 && bfd_get_format (ibfd) == bfd_core \
4474 && s->vma == 0 && s->lma == 0 \
4475 && (bfd_vma) s->filepos >= p->p_offset \
4476 && (bfd_vma) s->filepos + s->_raw_size \
252b5132
RH
4477 <= p->p_offset + p->p_filesz)
4478
4479 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4480 linker, which generates a PT_INTERP section with p_vaddr and
4481 p_memsz set to 0. */
bc67d8a6
NC
4482#define IS_SOLARIS_PT_INTERP(p, s) \
4483 ( p->p_vaddr == 0 \
4484 && p->p_filesz > 0 \
4485 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4486 && s->_raw_size > 0 \
4487 && (bfd_vma) s->filepos >= p->p_offset \
4488 && ((bfd_vma) s->filepos + s->_raw_size \
c0f7859b 4489 <= p->p_offset + p->p_filesz))
5c440b1e 4490
bc67d8a6
NC
4491 /* Decide if the given section should be included in the given segment.
4492 A section will be included if:
f5ffc919
NC
4493 1. It is within the address space of the segment -- we use the LMA
4494 if that is set for the segment and the VMA otherwise,
bc67d8a6
NC
4495 2. It is an allocated segment,
4496 3. There is an output section associated with it,
4497 4. The section has not already been allocated to a previous segment. */
caf47ea6 4498#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
f5ffc919
NC
4499 (((((segment->p_paddr \
4500 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4501 : IS_CONTAINED_BY_VMA (section, segment)) \
4502 || IS_SOLARIS_PT_INTERP (segment, section)) \
4503 && (section->flags & SEC_ALLOC) != 0) \
caf47ea6
AM
4504 || IS_COREFILE_NOTE (segment, section) \
4505 || (IS_CONTAINED_BY_FILEPOS (section, segment, bed) \
4506 && (section->flags & SEC_ALLOC) == 0)) \
f5ffc919 4507 && section->output_section != NULL \
bc67d8a6
NC
4508 && section->segment_mark == false)
4509
4510 /* Returns true iff seg1 starts after the end of seg2. */
4511#define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4512 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4513
4514 /* Returns true iff seg1 and seg2 overlap. */
4515#define SEGMENT_OVERLAPS(seg1, seg2) \
4516 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4517
4518 /* Initialise the segment mark field. */
4519 for (section = ibfd->sections; section != NULL; section = section->next)
4520 section->segment_mark = false;
4521
252b5132 4522 /* Scan through the segments specified in the program header
bc67d8a6 4523 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 4524 in the loadable segments. These can be created by weird
bc67d8a6
NC
4525 parameters to objcopy. */
4526 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4527 i < num_segments;
c044fabd 4528 i++, segment++)
252b5132 4529 {
252b5132 4530 unsigned int j;
c044fabd 4531 Elf_Internal_Phdr *segment2;
252b5132 4532
bc67d8a6
NC
4533 if (segment->p_type != PT_LOAD)
4534 continue;
c044fabd 4535
bc67d8a6 4536 /* Determine if this segment overlaps any previous segments. */
c044fabd 4537 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
bc67d8a6
NC
4538 {
4539 bfd_signed_vma extra_length;
c044fabd 4540
bc67d8a6
NC
4541 if (segment2->p_type != PT_LOAD
4542 || ! SEGMENT_OVERLAPS (segment, segment2))
4543 continue;
c044fabd 4544
bc67d8a6
NC
4545 /* Merge the two segments together. */
4546 if (segment2->p_vaddr < segment->p_vaddr)
4547 {
c044fabd
KH
4548 /* Extend SEGMENT2 to include SEGMENT and then delete
4549 SEGMENT. */
bc67d8a6
NC
4550 extra_length =
4551 SEGMENT_END (segment, segment->p_vaddr)
4552 - SEGMENT_END (segment2, segment2->p_vaddr);
c044fabd 4553
bc67d8a6
NC
4554 if (extra_length > 0)
4555 {
4556 segment2->p_memsz += extra_length;
4557 segment2->p_filesz += extra_length;
4558 }
c044fabd 4559
bc67d8a6 4560 segment->p_type = PT_NULL;
c044fabd 4561
bc67d8a6
NC
4562 /* Since we have deleted P we must restart the outer loop. */
4563 i = 0;
4564 segment = elf_tdata (ibfd)->phdr;
4565 break;
4566 }
4567 else
4568 {
c044fabd
KH
4569 /* Extend SEGMENT to include SEGMENT2 and then delete
4570 SEGMENT2. */
bc67d8a6
NC
4571 extra_length =
4572 SEGMENT_END (segment2, segment2->p_vaddr)
4573 - SEGMENT_END (segment, segment->p_vaddr);
c044fabd 4574
bc67d8a6
NC
4575 if (extra_length > 0)
4576 {
4577 segment->p_memsz += extra_length;
4578 segment->p_filesz += extra_length;
4579 }
c044fabd 4580
bc67d8a6
NC
4581 segment2->p_type = PT_NULL;
4582 }
4583 }
4584 }
c044fabd 4585
bc67d8a6
NC
4586 /* The second scan attempts to assign sections to segments. */
4587 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4588 i < num_segments;
4589 i ++, segment ++)
4590 {
4591 unsigned int section_count;
4592 asection ** sections;
4593 asection * output_section;
4594 unsigned int isec;
4595 bfd_vma matching_lma;
4596 bfd_vma suggested_lma;
4597 unsigned int j;
dc810e39 4598 bfd_size_type amt;
bc67d8a6
NC
4599
4600 if (segment->p_type == PT_NULL)
4601 continue;
c044fabd 4602
bc67d8a6
NC
4603 /* Compute how many sections might be placed into this segment. */
4604 section_count = 0;
4605 for (section = ibfd->sections; section != NULL; section = section->next)
caf47ea6 4606 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c044fabd 4607 ++section_count;
252b5132
RH
4608
4609 /* Allocate a segment map big enough to contain all of the
4610 sections we have selected. */
dc810e39
AM
4611 amt = sizeof (struct elf_segment_map);
4612 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4613 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
bc67d8a6 4614 if (map == NULL)
252b5132
RH
4615 return false;
4616
4617 /* Initialise the fields of the segment map. Default to
4618 using the physical address of the segment in the input BFD. */
bc67d8a6
NC
4619 map->next = NULL;
4620 map->p_type = segment->p_type;
4621 map->p_flags = segment->p_flags;
4622 map->p_flags_valid = 1;
4623 map->p_paddr = segment->p_paddr;
4624 map->p_paddr_valid = 1;
252b5132
RH
4625
4626 /* Determine if this segment contains the ELF file header
4627 and if it contains the program headers themselves. */
bc67d8a6
NC
4628 map->includes_filehdr = (segment->p_offset == 0
4629 && segment->p_filesz >= iehdr->e_ehsize);
252b5132 4630
bc67d8a6 4631 map->includes_phdrs = 0;
252b5132 4632
bc67d8a6 4633 if (! phdr_included || segment->p_type != PT_LOAD)
252b5132 4634 {
bc67d8a6
NC
4635 map->includes_phdrs =
4636 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4637 && (segment->p_offset + segment->p_filesz
252b5132
RH
4638 >= ((bfd_vma) iehdr->e_phoff
4639 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 4640
bc67d8a6 4641 if (segment->p_type == PT_LOAD && map->includes_phdrs)
252b5132
RH
4642 phdr_included = true;
4643 }
4644
bc67d8a6 4645 if (section_count == 0)
252b5132
RH
4646 {
4647 /* Special segments, such as the PT_PHDR segment, may contain
4648 no sections, but ordinary, loadable segments should contain
1ed89aa9
NC
4649 something. They are allowed by the ELF spec however, so only
4650 a warning is produced. */
bc67d8a6 4651 if (segment->p_type == PT_LOAD)
caf47ea6 4652 (*_bfd_error_handler)
1ed89aa9 4653 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
caf47ea6 4654 bfd_archive_filename (ibfd));
252b5132 4655
bc67d8a6 4656 map->count = 0;
c044fabd
KH
4657 *pointer_to_map = map;
4658 pointer_to_map = &map->next;
252b5132
RH
4659
4660 continue;
4661 }
4662
4663 /* Now scan the sections in the input BFD again and attempt
4664 to add their corresponding output sections to the segment map.
4665 The problem here is how to handle an output section which has
4666 been moved (ie had its LMA changed). There are four possibilities:
4667
4668 1. None of the sections have been moved.
4669 In this case we can continue to use the segment LMA from the
4670 input BFD.
4671
4672 2. All of the sections have been moved by the same amount.
4673 In this case we can change the segment's LMA to match the LMA
4674 of the first section.
4675
4676 3. Some of the sections have been moved, others have not.
4677 In this case those sections which have not been moved can be
4678 placed in the current segment which will have to have its size,
4679 and possibly its LMA changed, and a new segment or segments will
4680 have to be created to contain the other sections.
4681
4682 4. The sections have been moved, but not be the same amount.
4683 In this case we can change the segment's LMA to match the LMA
4684 of the first section and we will have to create a new segment
4685 or segments to contain the other sections.
4686
4687 In order to save time, we allocate an array to hold the section
4688 pointers that we are interested in. As these sections get assigned
4689 to a segment, they are removed from this array. */
4690
0b14c2aa
L
4691 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4692 to work around this long long bug. */
4693 amt = section_count * sizeof (asection *);
dc810e39 4694 sections = (asection **) bfd_malloc (amt);
252b5132
RH
4695 if (sections == NULL)
4696 return false;
4697
4698 /* Step One: Scan for segment vs section LMA conflicts.
4699 Also add the sections to the section array allocated above.
4700 Also add the sections to the current segment. In the common
4701 case, where the sections have not been moved, this means that
4702 we have completely filled the segment, and there is nothing
4703 more to do. */
252b5132 4704 isec = 0;
72730e0c 4705 matching_lma = 0;
252b5132
RH
4706 suggested_lma = 0;
4707
bc67d8a6
NC
4708 for (j = 0, section = ibfd->sections;
4709 section != NULL;
4710 section = section->next)
252b5132 4711 {
caf47ea6 4712 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c0f7859b 4713 {
bc67d8a6
NC
4714 output_section = section->output_section;
4715
4716 sections[j ++] = section;
252b5132
RH
4717
4718 /* The Solaris native linker always sets p_paddr to 0.
4719 We try to catch that case here, and set it to the
4720 correct value. */
bc67d8a6
NC
4721 if (segment->p_paddr == 0
4722 && segment->p_vaddr != 0
252b5132 4723 && isec == 0
bc67d8a6
NC
4724 && output_section->lma != 0
4725 && (output_section->vma == (segment->p_vaddr
4726 + (map->includes_filehdr
4727 ? iehdr->e_ehsize
4728 : 0)
4729 + (map->includes_phdrs
079e9a2f
AM
4730 ? (iehdr->e_phnum
4731 * iehdr->e_phentsize)
bc67d8a6
NC
4732 : 0))))
4733 map->p_paddr = segment->p_vaddr;
252b5132
RH
4734
4735 /* Match up the physical address of the segment with the
4736 LMA address of the output section. */
bc67d8a6 4737 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
caf47ea6 4738 || IS_CONTAINED_BY_FILEPOS (section, segment, bed)
bc67d8a6 4739 || IS_COREFILE_NOTE (segment, section))
252b5132
RH
4740 {
4741 if (matching_lma == 0)
bc67d8a6 4742 matching_lma = output_section->lma;
252b5132
RH
4743
4744 /* We assume that if the section fits within the segment
bc67d8a6 4745 then it does not overlap any other section within that
252b5132 4746 segment. */
bc67d8a6 4747 map->sections[isec ++] = output_section;
252b5132
RH
4748 }
4749 else if (suggested_lma == 0)
bc67d8a6 4750 suggested_lma = output_section->lma;
252b5132
RH
4751 }
4752 }
4753
bc67d8a6 4754 BFD_ASSERT (j == section_count);
252b5132
RH
4755
4756 /* Step Two: Adjust the physical address of the current segment,
4757 if necessary. */
bc67d8a6 4758 if (isec == section_count)
252b5132
RH
4759 {
4760 /* All of the sections fitted within the segment as currently
4761 specified. This is the default case. Add the segment to
4762 the list of built segments and carry on to process the next
4763 program header in the input BFD. */
bc67d8a6 4764 map->count = section_count;
c044fabd
KH
4765 *pointer_to_map = map;
4766 pointer_to_map = &map->next;
252b5132
RH
4767
4768 free (sections);
4769 continue;
4770 }
252b5132
RH
4771 else
4772 {
72730e0c
AM
4773 if (matching_lma != 0)
4774 {
4775 /* At least one section fits inside the current segment.
4776 Keep it, but modify its physical address to match the
4777 LMA of the first section that fitted. */
bc67d8a6 4778 map->p_paddr = matching_lma;
72730e0c
AM
4779 }
4780 else
4781 {
4782 /* None of the sections fitted inside the current segment.
4783 Change the current segment's physical address to match
4784 the LMA of the first section. */
bc67d8a6 4785 map->p_paddr = suggested_lma;
72730e0c
AM
4786 }
4787
bc67d8a6
NC
4788 /* Offset the segment physical address from the lma
4789 to allow for space taken up by elf headers. */
4790 if (map->includes_filehdr)
4791 map->p_paddr -= iehdr->e_ehsize;
252b5132 4792
bc67d8a6
NC
4793 if (map->includes_phdrs)
4794 {
4795 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4796
4797 /* iehdr->e_phnum is just an estimate of the number
4798 of program headers that we will need. Make a note
4799 here of the number we used and the segment we chose
4800 to hold these headers, so that we can adjust the
4801 offset when we know the correct value. */
4802 phdr_adjust_num = iehdr->e_phnum;
4803 phdr_adjust_seg = map;
4804 }
252b5132
RH
4805 }
4806
4807 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 4808 those that fit to the current segment and removing them from the
252b5132
RH
4809 sections array; but making sure not to leave large gaps. Once all
4810 possible sections have been assigned to the current segment it is
4811 added to the list of built segments and if sections still remain
4812 to be assigned, a new segment is constructed before repeating
4813 the loop. */
4814 isec = 0;
4815 do
4816 {
bc67d8a6 4817 map->count = 0;
252b5132
RH
4818 suggested_lma = 0;
4819
4820 /* Fill the current segment with sections that fit. */
bc67d8a6 4821 for (j = 0; j < section_count; j++)
252b5132 4822 {
bc67d8a6 4823 section = sections[j];
252b5132 4824
bc67d8a6 4825 if (section == NULL)
252b5132
RH
4826 continue;
4827
bc67d8a6 4828 output_section = section->output_section;
252b5132 4829
bc67d8a6 4830 BFD_ASSERT (output_section != NULL);
c044fabd 4831
bc67d8a6
NC
4832 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4833 || IS_COREFILE_NOTE (segment, section))
252b5132 4834 {
bc67d8a6 4835 if (map->count == 0)
252b5132
RH
4836 {
4837 /* If the first section in a segment does not start at
bc67d8a6
NC
4838 the beginning of the segment, then something is
4839 wrong. */
4840 if (output_section->lma !=
4841 (map->p_paddr
4842 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4843 + (map->includes_phdrs
4844 ? iehdr->e_phnum * iehdr->e_phentsize
4845 : 0)))
252b5132
RH
4846 abort ();
4847 }
4848 else
4849 {
4850 asection * prev_sec;
252b5132 4851
bc67d8a6 4852 prev_sec = map->sections[map->count - 1];
252b5132
RH
4853
4854 /* If the gap between the end of the previous section
bc67d8a6
NC
4855 and the start of this section is more than
4856 maxpagesize then we need to start a new segment. */
079e9a2f
AM
4857 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
4858 maxpagesize)
caf47ea6 4859 < BFD_ALIGN (output_section->lma, maxpagesize))
079e9a2f
AM
4860 || ((prev_sec->lma + prev_sec->_raw_size)
4861 > output_section->lma))
252b5132
RH
4862 {
4863 if (suggested_lma == 0)
bc67d8a6 4864 suggested_lma = output_section->lma;
252b5132
RH
4865
4866 continue;
4867 }
4868 }
4869
bc67d8a6 4870 map->sections[map->count++] = output_section;
252b5132
RH
4871 ++isec;
4872 sections[j] = NULL;
bc67d8a6 4873 section->segment_mark = true;
252b5132
RH
4874 }
4875 else if (suggested_lma == 0)
bc67d8a6 4876 suggested_lma = output_section->lma;
252b5132
RH
4877 }
4878
bc67d8a6 4879 BFD_ASSERT (map->count > 0);
252b5132
RH
4880
4881 /* Add the current segment to the list of built segments. */
c044fabd
KH
4882 *pointer_to_map = map;
4883 pointer_to_map = &map->next;
252b5132 4884
bc67d8a6 4885 if (isec < section_count)
252b5132
RH
4886 {
4887 /* We still have not allocated all of the sections to
4888 segments. Create a new segment here, initialise it
4889 and carry on looping. */
dc810e39
AM
4890 amt = sizeof (struct elf_segment_map);
4891 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4892 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
bc67d8a6 4893 if (map == NULL)
252b5132
RH
4894 return false;
4895
4896 /* Initialise the fields of the segment map. Set the physical
4897 physical address to the LMA of the first section that has
4898 not yet been assigned. */
bc67d8a6
NC
4899 map->next = NULL;
4900 map->p_type = segment->p_type;
4901 map->p_flags = segment->p_flags;
4902 map->p_flags_valid = 1;
4903 map->p_paddr = suggested_lma;
4904 map->p_paddr_valid = 1;
4905 map->includes_filehdr = 0;
4906 map->includes_phdrs = 0;
252b5132
RH
4907 }
4908 }
bc67d8a6 4909 while (isec < section_count);
252b5132
RH
4910
4911 free (sections);
4912 }
4913
4914 /* The Solaris linker creates program headers in which all the
4915 p_paddr fields are zero. When we try to objcopy or strip such a
4916 file, we get confused. Check for this case, and if we find it
4917 reset the p_paddr_valid fields. */
bc67d8a6
NC
4918 for (map = map_first; map != NULL; map = map->next)
4919 if (map->p_paddr != 0)
252b5132 4920 break;
bc67d8a6 4921 if (map == NULL)
252b5132 4922 {
bc67d8a6
NC
4923 for (map = map_first; map != NULL; map = map->next)
4924 map->p_paddr_valid = 0;
252b5132
RH
4925 }
4926
bc67d8a6
NC
4927 elf_tdata (obfd)->segment_map = map_first;
4928
4929 /* If we had to estimate the number of program headers that were
9ad5cbcf 4930 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
4931 the offset if necessary. */
4932 if (phdr_adjust_seg != NULL)
4933 {
4934 unsigned int count;
c044fabd 4935
bc67d8a6 4936 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 4937 count++;
252b5132 4938
bc67d8a6
NC
4939 if (count > phdr_adjust_num)
4940 phdr_adjust_seg->p_paddr
4941 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4942 }
c044fabd 4943
252b5132 4944#if 0
c044fabd
KH
4945 /* Final Step: Sort the segments into ascending order of physical
4946 address. */
bc67d8a6 4947 if (map_first != NULL)
252b5132 4948 {
c044fabd 4949 struct elf_segment_map *prev;
252b5132 4950
bc67d8a6
NC
4951 prev = map_first;
4952 for (map = map_first->next; map != NULL; prev = map, map = map->next)
252b5132 4953 {
bc67d8a6
NC
4954 /* Yes I know - its a bubble sort.... */
4955 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
252b5132 4956 {
bc67d8a6
NC
4957 /* Swap map and map->next. */
4958 prev->next = map->next;
4959 map->next = map->next->next;
4960 prev->next->next = map;
252b5132 4961
bc67d8a6
NC
4962 /* Restart loop. */
4963 map = map_first;
252b5132
RH
4964 }
4965 }
4966 }
4967#endif
4968
bc67d8a6
NC
4969#undef SEGMENT_END
4970#undef IS_CONTAINED_BY_VMA
4971#undef IS_CONTAINED_BY_LMA
caf47ea6 4972#undef IS_CONTAINED_BY_FILEPOS
252b5132 4973#undef IS_COREFILE_NOTE
bc67d8a6
NC
4974#undef IS_SOLARIS_PT_INTERP
4975#undef INCLUDE_SECTION_IN_SEGMENT
4976#undef SEGMENT_AFTER_SEGMENT
4977#undef SEGMENT_OVERLAPS
252b5132
RH
4978 return true;
4979}
4980
4981/* Copy private section information. This copies over the entsize
4982 field, and sometimes the info field. */
4983
4984boolean
4985_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4986 bfd *ibfd;
4987 asection *isec;
4988 bfd *obfd;
4989 asection *osec;
4990{
4991 Elf_Internal_Shdr *ihdr, *ohdr;
caf47ea6 4992 const struct elf_backend_data *bed = get_elf_backend_data (ibfd);
252b5132
RH
4993
4994 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4995 || obfd->xvec->flavour != bfd_target_elf_flavour)
4996 return true;
4997
4998 /* Copy over private BFD data if it has not already been copied.
4999 This must be done here, rather than in the copy_private_bfd_data
5000 entry point, because the latter is called after the section
5001 contents have been set, which means that the program headers have
caf47ea6
AM
5002 already been worked out. The backend function provides a way to
5003 override the test conditions and code path for the call to
5004 copy_private_bfd_data. */
5005 if (bed->copy_private_bfd_data_p)
252b5132 5006 {
caf47ea6
AM
5007 if ((*bed->copy_private_bfd_data_p) (ibfd, isec, obfd, osec))
5008 if (! copy_private_bfd_data (ibfd, obfd))
5009 return false;
5010 }
5011 else if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5012 {
5013 asection *s;
5014
5015 /* Only set up the segments if there are no more SEC_ALLOC
5016 sections. FIXME: This won't do the right thing if objcopy is
5017 used to remove the last SEC_ALLOC section, since objcopy
5018 won't call this routine in that case. */
5019 for (s = isec->next; s != NULL; s = s->next)
5020 if ((s->flags & SEC_ALLOC) != 0)
5021 break;
5022 if (s == NULL)
5023 {
5024 if (! copy_private_bfd_data (ibfd, obfd))
5025 return false;
5026 }
252b5132
RH
5027 }
5028
5029 ihdr = &elf_section_data (isec)->this_hdr;
5030 ohdr = &elf_section_data (osec)->this_hdr;
5031
5032 ohdr->sh_entsize = ihdr->sh_entsize;
5033
5034 if (ihdr->sh_type == SHT_SYMTAB
5035 || ihdr->sh_type == SHT_DYNSYM
5036 || ihdr->sh_type == SHT_GNU_verneed
5037 || ihdr->sh_type == SHT_GNU_verdef)
5038 ohdr->sh_info = ihdr->sh_info;
5039
9dce4196
AM
5040 /* Set things up for objcopy. The output SHT_GROUP section will
5041 have its elf_next_in_group pointing back to the input group
5042 members. */
5043 elf_next_in_group (osec) = elf_next_in_group (isec);
5044 elf_group_name (osec) = elf_group_name (isec);
5045
bf572ba0
MM
5046 elf_section_data (osec)->use_rela_p
5047 = elf_section_data (isec)->use_rela_p;
5048
252b5132
RH
5049 return true;
5050}
5051
5052/* Copy private symbol information. If this symbol is in a section
5053 which we did not map into a BFD section, try to map the section
5054 index correctly. We use special macro definitions for the mapped
5055 section indices; these definitions are interpreted by the
5056 swap_out_syms function. */
5057
9ad5cbcf
AM
5058#define MAP_ONESYMTAB (SHN_HIOS + 1)
5059#define MAP_DYNSYMTAB (SHN_HIOS + 2)
5060#define MAP_STRTAB (SHN_HIOS + 3)
5061#define MAP_SHSTRTAB (SHN_HIOS + 4)
5062#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132
RH
5063
5064boolean
5065_bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
5066 bfd *ibfd;
5067 asymbol *isymarg;
5068 bfd *obfd;
5069 asymbol *osymarg;
5070{
5071 elf_symbol_type *isym, *osym;
5072
5073 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5074 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5075 return true;
5076
5077 isym = elf_symbol_from (ibfd, isymarg);
5078 osym = elf_symbol_from (obfd, osymarg);
5079
5080 if (isym != NULL
5081 && osym != NULL
5082 && bfd_is_abs_section (isym->symbol.section))
5083 {
5084 unsigned int shndx;
5085
5086 shndx = isym->internal_elf_sym.st_shndx;
5087 if (shndx == elf_onesymtab (ibfd))
5088 shndx = MAP_ONESYMTAB;
5089 else if (shndx == elf_dynsymtab (ibfd))
5090 shndx = MAP_DYNSYMTAB;
5091 else if (shndx == elf_tdata (ibfd)->strtab_section)
5092 shndx = MAP_STRTAB;
5093 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5094 shndx = MAP_SHSTRTAB;
9ad5cbcf
AM
5095 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5096 shndx = MAP_SYM_SHNDX;
252b5132
RH
5097 osym->internal_elf_sym.st_shndx = shndx;
5098 }
5099
5100 return true;
5101}
5102
5103/* Swap out the symbols. */
5104
5105static boolean
5106swap_out_syms (abfd, sttp, relocatable_p)
5107 bfd *abfd;
5108 struct bfd_strtab_hash **sttp;
5109 int relocatable_p;
5110{
079e9a2f
AM
5111 struct elf_backend_data *bed;
5112 int symcount;
5113 asymbol **syms;
5114 struct bfd_strtab_hash *stt;
5115 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 5116 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f
AM
5117 Elf_Internal_Shdr *symstrtab_hdr;
5118 char *outbound_syms;
9ad5cbcf 5119 char *outbound_shndx;
079e9a2f
AM
5120 int idx;
5121 bfd_size_type amt;
252b5132
RH
5122
5123 if (!elf_map_symbols (abfd))
5124 return false;
5125
c044fabd 5126 /* Dump out the symtabs. */
079e9a2f
AM
5127 stt = _bfd_elf_stringtab_init ();
5128 if (stt == NULL)
5129 return false;
252b5132 5130
079e9a2f
AM
5131 bed = get_elf_backend_data (abfd);
5132 symcount = bfd_get_symcount (abfd);
5133 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5134 symtab_hdr->sh_type = SHT_SYMTAB;
5135 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5136 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5137 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5138 symtab_hdr->sh_addralign = bed->s->file_align;
5139
5140 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5141 symstrtab_hdr->sh_type = SHT_STRTAB;
5142
5143 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5144 outbound_syms = bfd_alloc (abfd, amt);
5145 if (outbound_syms == NULL)
5146 return false;
5147 symtab_hdr->contents = (PTR) outbound_syms;
252b5132 5148
9ad5cbcf
AM
5149 outbound_shndx = NULL;
5150 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5151 if (symtab_shndx_hdr->sh_name != 0)
5152 {
5153 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
1126897b 5154 outbound_shndx = bfd_zalloc (abfd, amt);
9ad5cbcf
AM
5155 if (outbound_shndx == NULL)
5156 return false;
9ad5cbcf
AM
5157 symtab_shndx_hdr->contents = outbound_shndx;
5158 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5159 symtab_shndx_hdr->sh_size = amt;
5160 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5161 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5162 }
5163
079e9a2f
AM
5164 /* now generate the data (for "contents") */
5165 {
5166 /* Fill in zeroth symbol and swap it out. */
5167 Elf_Internal_Sym sym;
5168 sym.st_name = 0;
5169 sym.st_value = 0;
5170 sym.st_size = 0;
5171 sym.st_info = 0;
5172 sym.st_other = 0;
5173 sym.st_shndx = SHN_UNDEF;
9ad5cbcf 5174 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 5175 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
5176 if (outbound_shndx != NULL)
5177 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 5178 }
252b5132 5179
079e9a2f
AM
5180 syms = bfd_get_outsymbols (abfd);
5181 for (idx = 0; idx < symcount; idx++)
252b5132 5182 {
252b5132 5183 Elf_Internal_Sym sym;
079e9a2f
AM
5184 bfd_vma value = syms[idx]->value;
5185 elf_symbol_type *type_ptr;
5186 flagword flags = syms[idx]->flags;
5187 int type;
252b5132 5188
079e9a2f
AM
5189 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5190 {
5191 /* Local section symbols have no name. */
5192 sym.st_name = 0;
5193 }
5194 else
5195 {
5196 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5197 syms[idx]->name,
5198 true, false);
5199 if (sym.st_name == (unsigned long) -1)
5200 return false;
5201 }
252b5132 5202
079e9a2f 5203 type_ptr = elf_symbol_from (abfd, syms[idx]);
252b5132 5204
079e9a2f
AM
5205 if ((flags & BSF_SECTION_SYM) == 0
5206 && bfd_is_com_section (syms[idx]->section))
5207 {
5208 /* ELF common symbols put the alignment into the `value' field,
5209 and the size into the `size' field. This is backwards from
5210 how BFD handles it, so reverse it here. */
5211 sym.st_size = value;
5212 if (type_ptr == NULL
5213 || type_ptr->internal_elf_sym.st_value == 0)
5214 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5215 else
5216 sym.st_value = type_ptr->internal_elf_sym.st_value;
5217 sym.st_shndx = _bfd_elf_section_from_bfd_section
5218 (abfd, syms[idx]->section);
5219 }
5220 else
5221 {
5222 asection *sec = syms[idx]->section;
5223 int shndx;
252b5132 5224
079e9a2f
AM
5225 if (sec->output_section)
5226 {
5227 value += sec->output_offset;
5228 sec = sec->output_section;
5229 }
5230 /* Don't add in the section vma for relocatable output. */
5231 if (! relocatable_p)
5232 value += sec->vma;
5233 sym.st_value = value;
5234 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5235
5236 if (bfd_is_abs_section (sec)
5237 && type_ptr != NULL
5238 && type_ptr->internal_elf_sym.st_shndx != 0)
5239 {
5240 /* This symbol is in a real ELF section which we did
5241 not create as a BFD section. Undo the mapping done
5242 by copy_private_symbol_data. */
5243 shndx = type_ptr->internal_elf_sym.st_shndx;
5244 switch (shndx)
5245 {
5246 case MAP_ONESYMTAB:
5247 shndx = elf_onesymtab (abfd);
5248 break;
5249 case MAP_DYNSYMTAB:
5250 shndx = elf_dynsymtab (abfd);
5251 break;
5252 case MAP_STRTAB:
5253 shndx = elf_tdata (abfd)->strtab_section;
5254 break;
5255 case MAP_SHSTRTAB:
5256 shndx = elf_tdata (abfd)->shstrtab_section;
5257 break;
9ad5cbcf
AM
5258 case MAP_SYM_SHNDX:
5259 shndx = elf_tdata (abfd)->symtab_shndx_section;
5260 break;
079e9a2f
AM
5261 default:
5262 break;
5263 }
5264 }
5265 else
5266 {
5267 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 5268
079e9a2f
AM
5269 if (shndx == -1)
5270 {
5271 asection *sec2;
5272
5273 /* Writing this would be a hell of a lot easier if
5274 we had some decent documentation on bfd, and
5275 knew what to expect of the library, and what to
5276 demand of applications. For example, it
5277 appears that `objcopy' might not set the
5278 section of a symbol to be a section that is
5279 actually in the output file. */
5280 sec2 = bfd_get_section_by_name (abfd, sec->name);
5281 BFD_ASSERT (sec2 != 0);
5282 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5283 BFD_ASSERT (shndx != -1);
5284 }
5285 }
252b5132 5286
079e9a2f
AM
5287 sym.st_shndx = shndx;
5288 }
252b5132 5289
13ae64f3
JJ
5290 if ((flags & BSF_THREAD_LOCAL) != 0)
5291 type = STT_TLS;
5292 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
5293 type = STT_FUNC;
5294 else if ((flags & BSF_OBJECT) != 0)
5295 type = STT_OBJECT;
5296 else
5297 type = STT_NOTYPE;
252b5132 5298
13ae64f3
JJ
5299 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5300 type = STT_TLS;
5301
079e9a2f
AM
5302 /* Processor-specific types */
5303 if (type_ptr != NULL
5304 && bed->elf_backend_get_symbol_type)
5305 type = ((*bed->elf_backend_get_symbol_type)
5306 (&type_ptr->internal_elf_sym, type));
252b5132 5307
079e9a2f
AM
5308 if (flags & BSF_SECTION_SYM)
5309 {
5310 if (flags & BSF_GLOBAL)
5311 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5312 else
5313 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5314 }
5315 else if (bfd_is_com_section (syms[idx]->section))
5316 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5317 else if (bfd_is_und_section (syms[idx]->section))
5318 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5319 ? STB_WEAK
5320 : STB_GLOBAL),
5321 type);
5322 else if (flags & BSF_FILE)
5323 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5324 else
5325 {
5326 int bind = STB_LOCAL;
252b5132 5327
079e9a2f
AM
5328 if (flags & BSF_LOCAL)
5329 bind = STB_LOCAL;
5330 else if (flags & BSF_WEAK)
5331 bind = STB_WEAK;
5332 else if (flags & BSF_GLOBAL)
5333 bind = STB_GLOBAL;
252b5132 5334
079e9a2f
AM
5335 sym.st_info = ELF_ST_INFO (bind, type);
5336 }
252b5132 5337
079e9a2f
AM
5338 if (type_ptr != NULL)
5339 sym.st_other = type_ptr->internal_elf_sym.st_other;
5340 else
5341 sym.st_other = 0;
252b5132 5342
9ad5cbcf 5343 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 5344 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
5345 if (outbound_shndx != NULL)
5346 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 5347 }
252b5132 5348
079e9a2f
AM
5349 *sttp = stt;
5350 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5351 symstrtab_hdr->sh_type = SHT_STRTAB;
252b5132 5352
079e9a2f
AM
5353 symstrtab_hdr->sh_flags = 0;
5354 symstrtab_hdr->sh_addr = 0;
5355 symstrtab_hdr->sh_entsize = 0;
5356 symstrtab_hdr->sh_link = 0;
5357 symstrtab_hdr->sh_info = 0;
5358 symstrtab_hdr->sh_addralign = 1;
252b5132
RH
5359
5360 return true;
5361}
5362
5363/* Return the number of bytes required to hold the symtab vector.
5364
5365 Note that we base it on the count plus 1, since we will null terminate
5366 the vector allocated based on this size. However, the ELF symbol table
5367 always has a dummy entry as symbol #0, so it ends up even. */
5368
5369long
5370_bfd_elf_get_symtab_upper_bound (abfd)
5371 bfd *abfd;
5372{
5373 long symcount;
5374 long symtab_size;
5375 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5376
5377 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
5378 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5379 if (symcount > 0)
5380 symtab_size -= sizeof (asymbol *);
252b5132
RH
5381
5382 return symtab_size;
5383}
5384
5385long
5386_bfd_elf_get_dynamic_symtab_upper_bound (abfd)
5387 bfd *abfd;
5388{
5389 long symcount;
5390 long symtab_size;
5391 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5392
5393 if (elf_dynsymtab (abfd) == 0)
5394 {
5395 bfd_set_error (bfd_error_invalid_operation);
5396 return -1;
5397 }
5398
5399 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
5400 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5401 if (symcount > 0)
5402 symtab_size -= sizeof (asymbol *);
252b5132
RH
5403
5404 return symtab_size;
5405}
5406
5407long
5408_bfd_elf_get_reloc_upper_bound (abfd, asect)
7442e600 5409 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
5410 sec_ptr asect;
5411{
5412 return (asect->reloc_count + 1) * sizeof (arelent *);
5413}
5414
5415/* Canonicalize the relocs. */
5416
5417long
5418_bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
5419 bfd *abfd;
5420 sec_ptr section;
5421 arelent **relptr;
5422 asymbol **symbols;
5423{
5424 arelent *tblptr;
5425 unsigned int i;
dbb410c3 5426 struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5427
dbb410c3 5428 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
252b5132
RH
5429 return -1;
5430
5431 tblptr = section->relocation;
5432 for (i = 0; i < section->reloc_count; i++)
5433 *relptr++ = tblptr++;
5434
5435 *relptr = NULL;
5436
5437 return section->reloc_count;
5438}
5439
5440long
5441_bfd_elf_get_symtab (abfd, alocation)
5442 bfd *abfd;
5443 asymbol **alocation;
5444{
dbb410c3
AM
5445 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5446 long symcount = bed->s->slurp_symbol_table (abfd, alocation, false);
252b5132
RH
5447
5448 if (symcount >= 0)
5449 bfd_get_symcount (abfd) = symcount;
5450 return symcount;
5451}
5452
5453long
5454_bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
5455 bfd *abfd;
5456 asymbol **alocation;
5457{
dbb410c3
AM
5458 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5459 return bed->s->slurp_symbol_table (abfd, alocation, true);
252b5132
RH
5460}
5461
5462/* Return the size required for the dynamic reloc entries. Any
5463 section that was actually installed in the BFD, and has type
5464 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5465 considered to be a dynamic reloc section. */
5466
5467long
5468_bfd_elf_get_dynamic_reloc_upper_bound (abfd)
5469 bfd *abfd;
5470{
5471 long ret;
5472 asection *s;
5473
5474 if (elf_dynsymtab (abfd) == 0)
5475 {
5476 bfd_set_error (bfd_error_invalid_operation);
5477 return -1;
5478 }
5479
5480 ret = sizeof (arelent *);
5481 for (s = abfd->sections; s != NULL; s = s->next)
5482 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5483 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5484 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5485 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5486 * sizeof (arelent *));
5487
5488 return ret;
5489}
5490
5491/* Canonicalize the dynamic relocation entries. Note that we return
5492 the dynamic relocations as a single block, although they are
5493 actually associated with particular sections; the interface, which
5494 was designed for SunOS style shared libraries, expects that there
5495 is only one set of dynamic relocs. Any section that was actually
5496 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5497 the dynamic symbol table, is considered to be a dynamic reloc
5498 section. */
5499
5500long
5501_bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5502 bfd *abfd;
5503 arelent **storage;
5504 asymbol **syms;
5505{
5506 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
5507 asection *s;
5508 long ret;
5509
5510 if (elf_dynsymtab (abfd) == 0)
5511 {
5512 bfd_set_error (bfd_error_invalid_operation);
5513 return -1;
5514 }
5515
5516 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5517 ret = 0;
5518 for (s = abfd->sections; s != NULL; s = s->next)
5519 {
5520 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5521 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5522 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5523 {
5524 arelent *p;
5525 long count, i;
5526
5527 if (! (*slurp_relocs) (abfd, s, syms, true))
5528 return -1;
5529 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5530 p = s->relocation;
5531 for (i = 0; i < count; i++)
5532 *storage++ = p++;
5533 ret += count;
5534 }
5535 }
5536
5537 *storage = NULL;
5538
5539 return ret;
5540}
5541\f
5542/* Read in the version information. */
5543
5544boolean
5545_bfd_elf_slurp_version_tables (abfd)
5546 bfd *abfd;
5547{
5548 bfd_byte *contents = NULL;
dc810e39 5549 bfd_size_type amt;
252b5132
RH
5550
5551 if (elf_dynverdef (abfd) != 0)
5552 {
5553 Elf_Internal_Shdr *hdr;
5554 Elf_External_Verdef *everdef;
5555 Elf_Internal_Verdef *iverdef;
f631889e
UD
5556 Elf_Internal_Verdef *iverdefarr;
5557 Elf_Internal_Verdef iverdefmem;
252b5132 5558 unsigned int i;
062e2358 5559 unsigned int maxidx;
252b5132
RH
5560
5561 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5562
252b5132
RH
5563 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5564 if (contents == NULL)
5565 goto error_return;
5566 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
dc810e39 5567 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
252b5132
RH
5568 goto error_return;
5569
f631889e
UD
5570 /* We know the number of entries in the section but not the maximum
5571 index. Therefore we have to run through all entries and find
5572 the maximum. */
252b5132 5573 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
5574 maxidx = 0;
5575 for (i = 0; i < hdr->sh_info; ++i)
5576 {
5577 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5578
062e2358
AM
5579 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5580 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e
UD
5581
5582 everdef = ((Elf_External_Verdef *)
5583 ((bfd_byte *) everdef + iverdefmem.vd_next));
5584 }
5585
dc810e39
AM
5586 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5587 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
f631889e
UD
5588 if (elf_tdata (abfd)->verdef == NULL)
5589 goto error_return;
5590
5591 elf_tdata (abfd)->cverdefs = maxidx;
5592
5593 everdef = (Elf_External_Verdef *) contents;
5594 iverdefarr = elf_tdata (abfd)->verdef;
5595 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
5596 {
5597 Elf_External_Verdaux *everdaux;
5598 Elf_Internal_Verdaux *iverdaux;
5599 unsigned int j;
5600
f631889e
UD
5601 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5602
5603 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5604 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
252b5132
RH
5605
5606 iverdef->vd_bfd = abfd;
5607
dc810e39
AM
5608 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5609 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
252b5132
RH
5610 if (iverdef->vd_auxptr == NULL)
5611 goto error_return;
5612
5613 everdaux = ((Elf_External_Verdaux *)
5614 ((bfd_byte *) everdef + iverdef->vd_aux));
5615 iverdaux = iverdef->vd_auxptr;
5616 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5617 {
5618 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5619
5620 iverdaux->vda_nodename =
5621 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5622 iverdaux->vda_name);
5623 if (iverdaux->vda_nodename == NULL)
5624 goto error_return;
5625
5626 if (j + 1 < iverdef->vd_cnt)
5627 iverdaux->vda_nextptr = iverdaux + 1;
5628 else
5629 iverdaux->vda_nextptr = NULL;
5630
5631 everdaux = ((Elf_External_Verdaux *)
5632 ((bfd_byte *) everdaux + iverdaux->vda_next));
5633 }
5634
5635 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5636
5637 if (i + 1 < hdr->sh_info)
5638 iverdef->vd_nextdef = iverdef + 1;
5639 else
5640 iverdef->vd_nextdef = NULL;
5641
5642 everdef = ((Elf_External_Verdef *)
5643 ((bfd_byte *) everdef + iverdef->vd_next));
5644 }
5645
5646 free (contents);
5647 contents = NULL;
5648 }
5649
5650 if (elf_dynverref (abfd) != 0)
5651 {
5652 Elf_Internal_Shdr *hdr;
5653 Elf_External_Verneed *everneed;
5654 Elf_Internal_Verneed *iverneed;
5655 unsigned int i;
5656
5657 hdr = &elf_tdata (abfd)->dynverref_hdr;
5658
dc810e39 5659 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
252b5132 5660 elf_tdata (abfd)->verref =
dc810e39 5661 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
252b5132
RH
5662 if (elf_tdata (abfd)->verref == NULL)
5663 goto error_return;
5664
5665 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5666
5667 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5668 if (contents == NULL)
5669 goto error_return;
5670 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
dc810e39 5671 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
252b5132
RH
5672 goto error_return;
5673
5674 everneed = (Elf_External_Verneed *) contents;
5675 iverneed = elf_tdata (abfd)->verref;
5676 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5677 {
5678 Elf_External_Vernaux *evernaux;
5679 Elf_Internal_Vernaux *ivernaux;
5680 unsigned int j;
5681
5682 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5683
5684 iverneed->vn_bfd = abfd;
5685
5686 iverneed->vn_filename =
5687 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5688 iverneed->vn_file);
5689 if (iverneed->vn_filename == NULL)
5690 goto error_return;
5691
dc810e39
AM
5692 amt = iverneed->vn_cnt;
5693 amt *= sizeof (Elf_Internal_Vernaux);
5694 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
252b5132
RH
5695
5696 evernaux = ((Elf_External_Vernaux *)
5697 ((bfd_byte *) everneed + iverneed->vn_aux));
5698 ivernaux = iverneed->vn_auxptr;
5699 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5700 {
5701 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5702
5703 ivernaux->vna_nodename =
5704 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5705 ivernaux->vna_name);
5706 if (ivernaux->vna_nodename == NULL)
5707 goto error_return;
5708
5709 if (j + 1 < iverneed->vn_cnt)
5710 ivernaux->vna_nextptr = ivernaux + 1;
5711 else
5712 ivernaux->vna_nextptr = NULL;
5713
5714 evernaux = ((Elf_External_Vernaux *)
5715 ((bfd_byte *) evernaux + ivernaux->vna_next));
5716 }
5717
5718 if (i + 1 < hdr->sh_info)
5719 iverneed->vn_nextref = iverneed + 1;
5720 else
5721 iverneed->vn_nextref = NULL;
5722
5723 everneed = ((Elf_External_Verneed *)
5724 ((bfd_byte *) everneed + iverneed->vn_next));
5725 }
5726
5727 free (contents);
5728 contents = NULL;
5729 }
5730
5731 return true;
5732
5733 error_return:
5734 if (contents == NULL)
5735 free (contents);
5736 return false;
5737}
5738\f
5739asymbol *
5740_bfd_elf_make_empty_symbol (abfd)
5741 bfd *abfd;
5742{
5743 elf_symbol_type *newsym;
dc810e39 5744 bfd_size_type amt = sizeof (elf_symbol_type);
252b5132 5745
dc810e39 5746 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
252b5132
RH
5747 if (!newsym)
5748 return NULL;
5749 else
5750 {
5751 newsym->symbol.the_bfd = abfd;
5752 return &newsym->symbol;
5753 }
5754}
5755
5756void
5757_bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
7442e600 5758 bfd *ignore_abfd ATTRIBUTE_UNUSED;
252b5132
RH
5759 asymbol *symbol;
5760 symbol_info *ret;
5761{
5762 bfd_symbol_info (symbol, ret);
5763}
5764
5765/* Return whether a symbol name implies a local symbol. Most targets
5766 use this function for the is_local_label_name entry point, but some
5767 override it. */
5768
5769boolean
5770_bfd_elf_is_local_label_name (abfd, name)
7442e600 5771 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
5772 const char *name;
5773{
5774 /* Normal local symbols start with ``.L''. */
5775 if (name[0] == '.' && name[1] == 'L')
5776 return true;
5777
5778 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5779 DWARF debugging symbols starting with ``..''. */
5780 if (name[0] == '.' && name[1] == '.')
5781 return true;
5782
5783 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5784 emitting DWARF debugging output. I suspect this is actually a
5785 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5786 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5787 underscore to be emitted on some ELF targets). For ease of use,
5788 we treat such symbols as local. */
5789 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5790 return true;
5791
5792 return false;
5793}
5794
5795alent *
5796_bfd_elf_get_lineno (ignore_abfd, symbol)
7442e600
ILT
5797 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5798 asymbol *symbol ATTRIBUTE_UNUSED;
252b5132
RH
5799{
5800 abort ();
5801 return NULL;
5802}
5803
5804boolean
5805_bfd_elf_set_arch_mach (abfd, arch, machine)
5806 bfd *abfd;
5807 enum bfd_architecture arch;
5808 unsigned long machine;
5809{
5810 /* If this isn't the right architecture for this backend, and this
5811 isn't the generic backend, fail. */
5812 if (arch != get_elf_backend_data (abfd)->arch
5813 && arch != bfd_arch_unknown
5814 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5815 return false;
5816
5817 return bfd_default_set_arch_mach (abfd, arch, machine);
5818}
5819
d1fad7c6
NC
5820/* Find the function to a particular section and offset,
5821 for error reporting. */
252b5132 5822
d1fad7c6
NC
5823static boolean
5824elf_find_function (abfd, section, symbols, offset,
4e8a9624 5825 filename_ptr, functionname_ptr)
d1fad7c6 5826 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
5827 asection *section;
5828 asymbol **symbols;
5829 bfd_vma offset;
4e8a9624
AM
5830 const char **filename_ptr;
5831 const char **functionname_ptr;
252b5132 5832{
252b5132
RH
5833 const char *filename;
5834 asymbol *func;
5835 bfd_vma low_func;
5836 asymbol **p;
5837
252b5132
RH
5838 filename = NULL;
5839 func = NULL;
5840 low_func = 0;
5841
5842 for (p = symbols; *p != NULL; p++)
5843 {
5844 elf_symbol_type *q;
5845
5846 q = (elf_symbol_type *) *p;
5847
5848 if (bfd_get_section (&q->symbol) != section)
5849 continue;
5850
5851 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5852 {
5853 default:
5854 break;
5855 case STT_FILE:
5856 filename = bfd_asymbol_name (&q->symbol);
5857 break;
5858 case STT_NOTYPE:
5859 case STT_FUNC:
5860 if (q->symbol.section == section
5861 && q->symbol.value >= low_func
5862 && q->symbol.value <= offset)
5863 {
5864 func = (asymbol *) q;
5865 low_func = q->symbol.value;
5866 }
5867 break;
5868 }
5869 }
5870
5871 if (func == NULL)
5872 return false;
5873
d1fad7c6
NC
5874 if (filename_ptr)
5875 *filename_ptr = filename;
5876 if (functionname_ptr)
5877 *functionname_ptr = bfd_asymbol_name (func);
5878
5879 return true;
5880}
5881
5882/* Find the nearest line to a particular section and offset,
5883 for error reporting. */
5884
5885boolean
5886_bfd_elf_find_nearest_line (abfd, section, symbols, offset,
4e8a9624 5887 filename_ptr, functionname_ptr, line_ptr)
d1fad7c6
NC
5888 bfd *abfd;
5889 asection *section;
5890 asymbol **symbols;
5891 bfd_vma offset;
4e8a9624
AM
5892 const char **filename_ptr;
5893 const char **functionname_ptr;
d1fad7c6
NC
5894 unsigned int *line_ptr;
5895{
5896 boolean found;
5897
5898 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
5899 filename_ptr, functionname_ptr,
5900 line_ptr))
d1fad7c6
NC
5901 {
5902 if (!*functionname_ptr)
4e8a9624
AM
5903 elf_find_function (abfd, section, symbols, offset,
5904 *filename_ptr ? NULL : filename_ptr,
5905 functionname_ptr);
5906
d1fad7c6
NC
5907 return true;
5908 }
5909
5910 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
5911 filename_ptr, functionname_ptr,
5912 line_ptr, 0,
5913 &elf_tdata (abfd)->dwarf2_find_line_info))
d1fad7c6
NC
5914 {
5915 if (!*functionname_ptr)
4e8a9624
AM
5916 elf_find_function (abfd, section, symbols, offset,
5917 *filename_ptr ? NULL : filename_ptr,
5918 functionname_ptr);
5919
d1fad7c6
NC
5920 return true;
5921 }
5922
5923 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
5924 &found, filename_ptr,
5925 functionname_ptr, line_ptr,
5926 &elf_tdata (abfd)->line_info))
d1fad7c6
NC
5927 return false;
5928 if (found)
5929 return true;
5930
5931 if (symbols == NULL)
5932 return false;
5933
5934 if (! elf_find_function (abfd, section, symbols, offset,
4e8a9624 5935 filename_ptr, functionname_ptr))
d1fad7c6
NC
5936 return false;
5937
252b5132
RH
5938 *line_ptr = 0;
5939 return true;
5940}
5941
5942int
5943_bfd_elf_sizeof_headers (abfd, reloc)
5944 bfd *abfd;
5945 boolean reloc;
5946{
5947 int ret;
5948
5949 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5950 if (! reloc)
5951 ret += get_program_header_size (abfd);
5952 return ret;
5953}
5954
5955boolean
5956_bfd_elf_set_section_contents (abfd, section, location, offset, count)
5957 bfd *abfd;
5958 sec_ptr section;
5959 PTR location;
5960 file_ptr offset;
5961 bfd_size_type count;
5962{
5963 Elf_Internal_Shdr *hdr;
dc810e39 5964 bfd_signed_vma pos;
252b5132
RH
5965
5966 if (! abfd->output_has_begun
5967 && ! _bfd_elf_compute_section_file_positions
5968 (abfd, (struct bfd_link_info *) NULL))
5969 return false;
5970
5971 hdr = &elf_section_data (section)->this_hdr;
dc810e39
AM
5972 pos = hdr->sh_offset + offset;
5973 if (bfd_seek (abfd, pos, SEEK_SET) != 0
5974 || bfd_bwrite (location, count, abfd) != count)
252b5132
RH
5975 return false;
5976
5977 return true;
5978}
5979
5980void
5981_bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
7442e600
ILT
5982 bfd *abfd ATTRIBUTE_UNUSED;
5983 arelent *cache_ptr ATTRIBUTE_UNUSED;
5984 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
252b5132
RH
5985{
5986 abort ();
5987}
5988
5989#if 0
5990void
5991_bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5992 bfd *abfd;
5993 arelent *cache_ptr;
5994 Elf_Internal_Rel *dst;
5995{
5996 abort ();
5997}
5998#endif
5999
6000/* Try to convert a non-ELF reloc into an ELF one. */
6001
6002boolean
6003_bfd_elf_validate_reloc (abfd, areloc)
6004 bfd *abfd;
6005 arelent *areloc;
6006{
c044fabd 6007 /* Check whether we really have an ELF howto. */
252b5132
RH
6008
6009 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6010 {
6011 bfd_reloc_code_real_type code;
6012 reloc_howto_type *howto;
6013
6014 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 6015 equivalent ELF reloc. */
252b5132
RH
6016
6017 if (areloc->howto->pc_relative)
6018 {
6019 switch (areloc->howto->bitsize)
6020 {
6021 case 8:
6022 code = BFD_RELOC_8_PCREL;
6023 break;
6024 case 12:
6025 code = BFD_RELOC_12_PCREL;
6026 break;
6027 case 16:
6028 code = BFD_RELOC_16_PCREL;
6029 break;
6030 case 24:
6031 code = BFD_RELOC_24_PCREL;
6032 break;
6033 case 32:
6034 code = BFD_RELOC_32_PCREL;
6035 break;
6036 case 64:
6037 code = BFD_RELOC_64_PCREL;
6038 break;
6039 default:
6040 goto fail;
6041 }
6042
6043 howto = bfd_reloc_type_lookup (abfd, code);
6044
6045 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6046 {
6047 if (howto->pcrel_offset)
6048 areloc->addend += areloc->address;
6049 else
6050 areloc->addend -= areloc->address; /* addend is unsigned!! */
6051 }
6052 }
6053 else
6054 {
6055 switch (areloc->howto->bitsize)
6056 {
6057 case 8:
6058 code = BFD_RELOC_8;
6059 break;
6060 case 14:
6061 code = BFD_RELOC_14;
6062 break;
6063 case 16:
6064 code = BFD_RELOC_16;
6065 break;
6066 case 26:
6067 code = BFD_RELOC_26;
6068 break;
6069 case 32:
6070 code = BFD_RELOC_32;
6071 break;
6072 case 64:
6073 code = BFD_RELOC_64;
6074 break;
6075 default:
6076 goto fail;
6077 }
6078
6079 howto = bfd_reloc_type_lookup (abfd, code);
6080 }
6081
6082 if (howto)
6083 areloc->howto = howto;
6084 else
6085 goto fail;
6086 }
6087
6088 return true;
6089
6090 fail:
6091 (*_bfd_error_handler)
6092 (_("%s: unsupported relocation type %s"),
8f615d07 6093 bfd_archive_filename (abfd), areloc->howto->name);
252b5132
RH
6094 bfd_set_error (bfd_error_bad_value);
6095 return false;
6096}
6097
6098boolean
6099_bfd_elf_close_and_cleanup (abfd)
6100 bfd *abfd;
6101{
6102 if (bfd_get_format (abfd) == bfd_object)
6103 {
6104 if (elf_shstrtab (abfd) != NULL)
2b0f7ef9 6105 _bfd_elf_strtab_free (elf_shstrtab (abfd));
252b5132
RH
6106 }
6107
6108 return _bfd_generic_close_and_cleanup (abfd);
6109}
6110
6111/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6112 in the relocation's offset. Thus we cannot allow any sort of sanity
6113 range-checking to interfere. There is nothing else to do in processing
6114 this reloc. */
6115
6116bfd_reloc_status_type
6117_bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
7442e600
ILT
6118 bfd *abfd ATTRIBUTE_UNUSED;
6119 arelent *re ATTRIBUTE_UNUSED;
6120 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
6121 PTR data ATTRIBUTE_UNUSED;
6122 asection *is ATTRIBUTE_UNUSED;
6123 bfd *obfd ATTRIBUTE_UNUSED;
6124 char **errmsg ATTRIBUTE_UNUSED;
252b5132
RH
6125{
6126 return bfd_reloc_ok;
6127}
252b5132
RH
6128\f
6129/* Elf core file support. Much of this only works on native
6130 toolchains, since we rely on knowing the
6131 machine-dependent procfs structure in order to pick
c044fabd 6132 out details about the corefile. */
252b5132
RH
6133
6134#ifdef HAVE_SYS_PROCFS_H
6135# include <sys/procfs.h>
6136#endif
6137
c044fabd 6138/* FIXME: this is kinda wrong, but it's what gdb wants. */
252b5132
RH
6139
6140static int
6141elfcore_make_pid (abfd)
c044fabd 6142 bfd *abfd;
252b5132
RH
6143{
6144 return ((elf_tdata (abfd)->core_lwpid << 16)
6145 + (elf_tdata (abfd)->core_pid));
6146}
6147
252b5132
RH
6148/* If there isn't a section called NAME, make one, using
6149 data from SECT. Note, this function will generate a
6150 reference to NAME, so you shouldn't deallocate or
c044fabd 6151 overwrite it. */
252b5132
RH
6152
6153static boolean
6154elfcore_maybe_make_sect (abfd, name, sect)
c044fabd
KH
6155 bfd *abfd;
6156 char *name;
6157 asection *sect;
252b5132 6158{
c044fabd 6159 asection *sect2;
252b5132
RH
6160
6161 if (bfd_get_section_by_name (abfd, name) != NULL)
6162 return true;
6163
6164 sect2 = bfd_make_section (abfd, name);
6165 if (sect2 == NULL)
6166 return false;
6167
6168 sect2->_raw_size = sect->_raw_size;
6169 sect2->filepos = sect->filepos;
6170 sect2->flags = sect->flags;
6171 sect2->alignment_power = sect->alignment_power;
6172 return true;
6173}
6174
bb0082d6
AM
6175/* Create a pseudosection containing SIZE bytes at FILEPOS. This
6176 actually creates up to two pseudosections:
6177 - For the single-threaded case, a section named NAME, unless
6178 such a section already exists.
6179 - For the multi-threaded case, a section named "NAME/PID", where
6180 PID is elfcore_make_pid (abfd).
6181 Both pseudosections have identical contents. */
6182boolean
6183_bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
6184 bfd *abfd;
6185 char *name;
dc810e39
AM
6186 size_t size;
6187 ufile_ptr filepos;
bb0082d6
AM
6188{
6189 char buf[100];
6190 char *threaded_name;
6191 asection *sect;
6192
6193 /* Build the section name. */
6194
6195 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
dc810e39 6196 threaded_name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
bb0082d6
AM
6197 if (threaded_name == NULL)
6198 return false;
6199 strcpy (threaded_name, buf);
6200
6201 sect = bfd_make_section (abfd, threaded_name);
6202 if (sect == NULL)
6203 return false;
6204 sect->_raw_size = size;
6205 sect->filepos = filepos;
6206 sect->flags = SEC_HAS_CONTENTS;
6207 sect->alignment_power = 2;
6208
936e320b 6209 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
6210}
6211
252b5132 6212/* prstatus_t exists on:
4a938328 6213 solaris 2.5+
252b5132
RH
6214 linux 2.[01] + glibc
6215 unixware 4.2
6216*/
6217
6218#if defined (HAVE_PRSTATUS_T)
a7b97311
AM
6219static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
6220
252b5132
RH
6221static boolean
6222elfcore_grok_prstatus (abfd, note)
c044fabd
KH
6223 bfd *abfd;
6224 Elf_Internal_Note *note;
252b5132 6225{
dc810e39 6226 size_t raw_size;
7ee38065 6227 int offset;
252b5132 6228
4a938328
MS
6229 if (note->descsz == sizeof (prstatus_t))
6230 {
6231 prstatus_t prstat;
252b5132 6232
e0ebfc61 6233 raw_size = sizeof (prstat.pr_reg);
7ee38065 6234 offset = offsetof (prstatus_t, pr_reg);
4a938328 6235 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 6236
fa49d224
NC
6237 /* Do not overwrite the core signal if it
6238 has already been set by another thread. */
6239 if (elf_tdata (abfd)->core_signal == 0)
6240 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328 6241 elf_tdata (abfd)->core_pid = prstat.pr_pid;
252b5132 6242
4a938328
MS
6243 /* pr_who exists on:
6244 solaris 2.5+
6245 unixware 4.2
6246 pr_who doesn't exist on:
6247 linux 2.[01]
6248 */
252b5132 6249#if defined (HAVE_PRSTATUS_T_PR_WHO)
4a938328 6250 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
252b5132 6251#endif
4a938328 6252 }
7ee38065 6253#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
6254 else if (note->descsz == sizeof (prstatus32_t))
6255 {
6256 /* 64-bit host, 32-bit corefile */
6257 prstatus32_t prstat;
6258
e0ebfc61 6259 raw_size = sizeof (prstat.pr_reg);
7ee38065 6260 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
6261 memcpy (&prstat, note->descdata, sizeof (prstat));
6262
fa49d224
NC
6263 /* Do not overwrite the core signal if it
6264 has already been set by another thread. */
6265 if (elf_tdata (abfd)->core_signal == 0)
6266 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328
MS
6267 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6268
6269 /* pr_who exists on:
6270 solaris 2.5+
6271 unixware 4.2
6272 pr_who doesn't exist on:
6273 linux 2.[01]
6274 */
7ee38065 6275#if defined (HAVE_PRSTATUS32_T_PR_WHO)
4a938328
MS
6276 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6277#endif
6278 }
7ee38065 6279#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
6280 else
6281 {
6282 /* Fail - we don't know how to handle any other
6283 note size (ie. data object type). */
6284 return true;
6285 }
252b5132 6286
bb0082d6 6287 /* Make a ".reg/999" section and a ".reg" section. */
936e320b
AM
6288 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6289 raw_size, note->descpos + offset);
252b5132
RH
6290}
6291#endif /* defined (HAVE_PRSTATUS_T) */
6292
bb0082d6 6293/* Create a pseudosection containing the exact contents of NOTE. */
252b5132 6294static boolean
ff08c6bb 6295elfcore_make_note_pseudosection (abfd, name, note)
c044fabd 6296 bfd *abfd;
ff08c6bb 6297 char *name;
c044fabd 6298 Elf_Internal_Note *note;
252b5132 6299{
936e320b
AM
6300 return _bfd_elfcore_make_pseudosection (abfd, name,
6301 note->descsz, note->descpos);
252b5132
RH
6302}
6303
ff08c6bb
JB
6304/* There isn't a consistent prfpregset_t across platforms,
6305 but it doesn't matter, because we don't have to pick this
c044fabd
KH
6306 data structure apart. */
6307
ff08c6bb
JB
6308static boolean
6309elfcore_grok_prfpreg (abfd, note)
c044fabd
KH
6310 bfd *abfd;
6311 Elf_Internal_Note *note;
ff08c6bb
JB
6312{
6313 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6314}
6315
ff08c6bb
JB
6316/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6317 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6318 literally. */
c044fabd 6319
ff08c6bb
JB
6320static boolean
6321elfcore_grok_prxfpreg (abfd, note)
c044fabd
KH
6322 bfd *abfd;
6323 Elf_Internal_Note *note;
ff08c6bb
JB
6324{
6325 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6326}
6327
252b5132 6328#if defined (HAVE_PRPSINFO_T)
4a938328 6329typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 6330#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
6331typedef prpsinfo32_t elfcore_psinfo32_t;
6332#endif
252b5132
RH
6333#endif
6334
6335#if defined (HAVE_PSINFO_T)
4a938328 6336typedef psinfo_t elfcore_psinfo_t;
7ee38065 6337#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
6338typedef psinfo32_t elfcore_psinfo32_t;
6339#endif
252b5132
RH
6340#endif
6341
252b5132
RH
6342/* return a malloc'ed copy of a string at START which is at
6343 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 6344 the copy will always have a terminating '\0'. */
252b5132 6345
936e320b 6346char *
bb0082d6 6347_bfd_elfcore_strndup (abfd, start, max)
c044fabd
KH
6348 bfd *abfd;
6349 char *start;
dc810e39 6350 size_t max;
252b5132 6351{
dc810e39 6352 char *dups;
c044fabd 6353 char *end = memchr (start, '\0', max);
dc810e39 6354 size_t len;
252b5132
RH
6355
6356 if (end == NULL)
6357 len = max;
6358 else
6359 len = end - start;
6360
dc810e39
AM
6361 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
6362 if (dups == NULL)
252b5132
RH
6363 return NULL;
6364
dc810e39
AM
6365 memcpy (dups, start, len);
6366 dups[len] = '\0';
252b5132 6367
dc810e39 6368 return dups;
252b5132
RH
6369}
6370
bb0082d6 6371#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
a7b97311 6372static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *));
bb0082d6 6373
252b5132
RH
6374static boolean
6375elfcore_grok_psinfo (abfd, note)
c044fabd
KH
6376 bfd *abfd;
6377 Elf_Internal_Note *note;
252b5132 6378{
4a938328
MS
6379 if (note->descsz == sizeof (elfcore_psinfo_t))
6380 {
6381 elfcore_psinfo_t psinfo;
252b5132 6382
7ee38065 6383 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 6384
4a938328 6385 elf_tdata (abfd)->core_program
936e320b
AM
6386 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6387 sizeof (psinfo.pr_fname));
252b5132 6388
4a938328 6389 elf_tdata (abfd)->core_command
936e320b
AM
6390 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6391 sizeof (psinfo.pr_psargs));
4a938328 6392 }
7ee38065 6393#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
6394 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6395 {
6396 /* 64-bit host, 32-bit corefile */
6397 elfcore_psinfo32_t psinfo;
6398
7ee38065 6399 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 6400
4a938328 6401 elf_tdata (abfd)->core_program
936e320b
AM
6402 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6403 sizeof (psinfo.pr_fname));
4a938328
MS
6404
6405 elf_tdata (abfd)->core_command
936e320b
AM
6406 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6407 sizeof (psinfo.pr_psargs));
4a938328
MS
6408 }
6409#endif
6410
6411 else
6412 {
6413 /* Fail - we don't know how to handle any other
6414 note size (ie. data object type). */
6415 return true;
6416 }
252b5132
RH
6417
6418 /* Note that for some reason, a spurious space is tacked
6419 onto the end of the args in some (at least one anyway)
c044fabd 6420 implementations, so strip it off if it exists. */
252b5132
RH
6421
6422 {
c044fabd 6423 char *command = elf_tdata (abfd)->core_command;
252b5132
RH
6424 int n = strlen (command);
6425
6426 if (0 < n && command[n - 1] == ' ')
6427 command[n - 1] = '\0';
6428 }
6429
6430 return true;
6431}
6432#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6433
252b5132 6434#if defined (HAVE_PSTATUS_T)
51316059
MS
6435static boolean elfcore_grok_pstatus PARAMS ((bfd *, Elf_Internal_Note *));
6436
252b5132
RH
6437static boolean
6438elfcore_grok_pstatus (abfd, note)
c044fabd
KH
6439 bfd *abfd;
6440 Elf_Internal_Note *note;
252b5132 6441{
f572a39d
AM
6442 if (note->descsz == sizeof (pstatus_t)
6443#if defined (HAVE_PXSTATUS_T)
6444 || note->descsz == sizeof (pxstatus_t)
6445#endif
6446 )
4a938328
MS
6447 {
6448 pstatus_t pstat;
252b5132 6449
4a938328 6450 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 6451
4a938328
MS
6452 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6453 }
7ee38065 6454#if defined (HAVE_PSTATUS32_T)
4a938328
MS
6455 else if (note->descsz == sizeof (pstatus32_t))
6456 {
6457 /* 64-bit host, 32-bit corefile */
6458 pstatus32_t pstat;
252b5132 6459
4a938328 6460 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 6461
4a938328
MS
6462 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6463 }
6464#endif
252b5132
RH
6465 /* Could grab some more details from the "representative"
6466 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 6467 NT_LWPSTATUS note, presumably. */
252b5132
RH
6468
6469 return true;
6470}
6471#endif /* defined (HAVE_PSTATUS_T) */
6472
252b5132 6473#if defined (HAVE_LWPSTATUS_T)
51316059
MS
6474static boolean elfcore_grok_lwpstatus PARAMS ((bfd *, Elf_Internal_Note *));
6475
252b5132
RH
6476static boolean
6477elfcore_grok_lwpstatus (abfd, note)
c044fabd
KH
6478 bfd *abfd;
6479 Elf_Internal_Note *note;
252b5132
RH
6480{
6481 lwpstatus_t lwpstat;
6482 char buf[100];
c044fabd
KH
6483 char *name;
6484 asection *sect;
252b5132 6485
f572a39d
AM
6486 if (note->descsz != sizeof (lwpstat)
6487#if defined (HAVE_LWPXSTATUS_T)
6488 && note->descsz != sizeof (lwpxstatus_t)
6489#endif
6490 )
252b5132
RH
6491 return true;
6492
6493 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6494
6495 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6496 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6497
c044fabd 6498 /* Make a ".reg/999" section. */
252b5132
RH
6499
6500 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
dc810e39 6501 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
252b5132
RH
6502 if (name == NULL)
6503 return false;
6504 strcpy (name, buf);
6505
6506 sect = bfd_make_section (abfd, name);
6507 if (sect == NULL)
6508 return false;
6509
6510#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6511 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6512 sect->filepos = note->descpos
6513 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6514#endif
6515
6516#if defined (HAVE_LWPSTATUS_T_PR_REG)
6517 sect->_raw_size = sizeof (lwpstat.pr_reg);
6518 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6519#endif
6520
6521 sect->flags = SEC_HAS_CONTENTS;
6522 sect->alignment_power = 2;
6523
6524 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6525 return false;
6526
6527 /* Make a ".reg2/999" section */
6528
6529 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
dc810e39 6530 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
252b5132
RH
6531 if (name == NULL)
6532 return false;
6533 strcpy (name, buf);
6534
6535 sect = bfd_make_section (abfd, name);
6536 if (sect == NULL)
6537 return false;
6538
6539#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6540 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6541 sect->filepos = note->descpos
6542 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6543#endif
6544
6545#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6546 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6547 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6548#endif
6549
6550 sect->flags = SEC_HAS_CONTENTS;
6551 sect->alignment_power = 2;
6552
936e320b 6553 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
6554}
6555#endif /* defined (HAVE_LWPSTATUS_T) */
6556
16e9c715
NC
6557#if defined (HAVE_WIN32_PSTATUS_T)
6558static boolean
6559elfcore_grok_win32pstatus (abfd, note)
c044fabd
KH
6560 bfd *abfd;
6561 Elf_Internal_Note *note;
16e9c715
NC
6562{
6563 char buf[30];
c044fabd
KH
6564 char *name;
6565 asection *sect;
16e9c715
NC
6566 win32_pstatus_t pstatus;
6567
6568 if (note->descsz < sizeof (pstatus))
6569 return true;
6570
e8eab623 6571 memcpy (&pstatus, note->descdata, sizeof (pstatus));
c044fabd
KH
6572
6573 switch (pstatus.data_type)
16e9c715
NC
6574 {
6575 case NOTE_INFO_PROCESS:
6576 /* FIXME: need to add ->core_command. */
6577 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6578 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
c044fabd 6579 break;
16e9c715
NC
6580
6581 case NOTE_INFO_THREAD:
6582 /* Make a ".reg/999" section. */
6583 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
c044fabd 6584
dc810e39 6585 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
16e9c715 6586 if (name == NULL)
c044fabd
KH
6587 return false;
6588
16e9c715
NC
6589 strcpy (name, buf);
6590
6591 sect = bfd_make_section (abfd, name);
6592 if (sect == NULL)
c044fabd
KH
6593 return false;
6594
16e9c715 6595 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
079e9a2f
AM
6596 sect->filepos = (note->descpos
6597 + offsetof (struct win32_pstatus,
6598 data.thread_info.thread_context));
16e9c715
NC
6599 sect->flags = SEC_HAS_CONTENTS;
6600 sect->alignment_power = 2;
6601
6602 if (pstatus.data.thread_info.is_active_thread)
6603 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6604 return false;
6605 break;
6606
6607 case NOTE_INFO_MODULE:
6608 /* Make a ".module/xxxxxxxx" section. */
c044fabd
KH
6609 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6610
dc810e39 6611 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
16e9c715
NC
6612 if (name == NULL)
6613 return false;
c044fabd 6614
16e9c715 6615 strcpy (name, buf);
252b5132 6616
16e9c715 6617 sect = bfd_make_section (abfd, name);
c044fabd 6618
16e9c715
NC
6619 if (sect == NULL)
6620 return false;
c044fabd 6621
16e9c715
NC
6622 sect->_raw_size = note->descsz;
6623 sect->filepos = note->descpos;
6624 sect->flags = SEC_HAS_CONTENTS;
6625 sect->alignment_power = 2;
6626 break;
6627
6628 default:
6629 return true;
6630 }
6631
6632 return true;
6633}
6634#endif /* HAVE_WIN32_PSTATUS_T */
252b5132
RH
6635
6636static boolean
6637elfcore_grok_note (abfd, note)
c044fabd
KH
6638 bfd *abfd;
6639 Elf_Internal_Note *note;
252b5132 6640{
bb0082d6
AM
6641 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6642
252b5132
RH
6643 switch (note->type)
6644 {
6645 default:
6646 return true;
6647
252b5132 6648 case NT_PRSTATUS:
bb0082d6
AM
6649 if (bed->elf_backend_grok_prstatus)
6650 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6651 return true;
6652#if defined (HAVE_PRSTATUS_T)
252b5132 6653 return elfcore_grok_prstatus (abfd, note);
bb0082d6
AM
6654#else
6655 return true;
252b5132
RH
6656#endif
6657
6658#if defined (HAVE_PSTATUS_T)
6659 case NT_PSTATUS:
6660 return elfcore_grok_pstatus (abfd, note);
6661#endif
6662
6663#if defined (HAVE_LWPSTATUS_T)
6664 case NT_LWPSTATUS:
6665 return elfcore_grok_lwpstatus (abfd, note);
6666#endif
6667
6668 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6669 return elfcore_grok_prfpreg (abfd, note);
6670
16e9c715 6671#if defined (HAVE_WIN32_PSTATUS_T)
c044fabd 6672 case NT_WIN32PSTATUS:
16e9c715
NC
6673 return elfcore_grok_win32pstatus (abfd, note);
6674#endif
6675
c044fabd 6676 case NT_PRXFPREG: /* Linux SSE extension */
ff08c6bb
JB
6677 if (note->namesz == 5
6678 && ! strcmp (note->namedata, "LINUX"))
6679 return elfcore_grok_prxfpreg (abfd, note);
6680 else
6681 return true;
6682
252b5132
RH
6683 case NT_PRPSINFO:
6684 case NT_PSINFO:
bb0082d6
AM
6685 if (bed->elf_backend_grok_psinfo)
6686 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6687 return true;
6688#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 6689 return elfcore_grok_psinfo (abfd, note);
bb0082d6
AM
6690#else
6691 return true;
252b5132
RH
6692#endif
6693 }
6694}
6695
50b2bdb7
AM
6696static boolean
6697elfcore_netbsd_get_lwpid (note, lwpidp)
6698 Elf_Internal_Note *note;
6699 int *lwpidp;
6700{
6701 char *cp;
6702
6703 cp = strchr (note->namedata, '@');
6704 if (cp != NULL)
6705 {
d2b64500 6706 *lwpidp = atoi(cp + 1);
50b2bdb7
AM
6707 return true;
6708 }
6709 return false;
6710}
6711
6712static boolean
6713elfcore_grok_netbsd_procinfo (abfd, note)
6714 bfd *abfd;
6715 Elf_Internal_Note *note;
6716{
6717
6718 /* Signal number at offset 0x08. */
6719 elf_tdata (abfd)->core_signal
6720 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6721
6722 /* Process ID at offset 0x50. */
6723 elf_tdata (abfd)->core_pid
6724 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6725
6726 /* Command name at 0x7c (max 32 bytes, including nul). */
6727 elf_tdata (abfd)->core_command
6728 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6729
6730 return true;
6731}
6732
6733static boolean
6734elfcore_grok_netbsd_note (abfd, note)
6735 bfd *abfd;
6736 Elf_Internal_Note *note;
6737{
6738 int lwp;
6739
6740 if (elfcore_netbsd_get_lwpid (note, &lwp))
6741 elf_tdata (abfd)->core_lwpid = lwp;
6742
b4db1224 6743 if (note->type == NT_NETBSDCORE_PROCINFO)
50b2bdb7
AM
6744 {
6745 /* NetBSD-specific core "procinfo". Note that we expect to
6746 find this note before any of the others, which is fine,
6747 since the kernel writes this note out first when it
6748 creates a core file. */
6749
6750 return elfcore_grok_netbsd_procinfo (abfd, note);
6751 }
6752
b4db1224
JT
6753 /* As of Jan 2002 there are no other machine-independent notes
6754 defined for NetBSD core files. If the note type is less
6755 than the start of the machine-dependent note types, we don't
6756 understand it. */
50b2bdb7 6757
b4db1224 6758 if (note->type < NT_NETBSDCORE_FIRSTMACH)
50b2bdb7
AM
6759 return true;
6760
6761
6762 switch (bfd_get_arch (abfd))
6763 {
6764 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6765 PT_GETFPREGS == mach+2. */
6766
6767 case bfd_arch_alpha:
6768 case bfd_arch_sparc:
6769 switch (note->type)
6770 {
b4db1224 6771 case NT_NETBSDCORE_FIRSTMACH+0:
50b2bdb7
AM
6772 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6773
b4db1224 6774 case NT_NETBSDCORE_FIRSTMACH+2:
50b2bdb7
AM
6775 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6776
6777 default:
6778 return true;
6779 }
6780
6781 /* On all other arch's, PT_GETREGS == mach+1 and
6782 PT_GETFPREGS == mach+3. */
6783
6784 default:
6785 switch (note->type)
6786 {
b4db1224 6787 case NT_NETBSDCORE_FIRSTMACH+1:
50b2bdb7
AM
6788 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6789
b4db1224 6790 case NT_NETBSDCORE_FIRSTMACH+3:
50b2bdb7
AM
6791 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6792
6793 default:
6794 return true;
6795 }
6796 }
6797 /* NOTREACHED */
6798}
6799
7c76fa91
MS
6800/* Function: elfcore_write_note
6801
6802 Inputs:
6803 buffer to hold note
6804 name of note
6805 type of note
6806 data for note
6807 size of data for note
6808
6809 Return:
6810 End of buffer containing note. */
6811
6812char *
6813elfcore_write_note (abfd, buf, bufsiz, name, type, input, size)
6814 bfd *abfd;
6815 char *buf;
6816 int *bufsiz;
6817 char *name;
6818 int type;
6819 void *input;
6820 int size;
6821{
6822 Elf_External_Note *xnp;
6823 int namesz = strlen (name);
6824 int newspace = BFD_ALIGN (sizeof (Elf_External_Note) + size + namesz - 1, 4);
6825 char *p, *dest;
6826
6827 p = realloc (buf, *bufsiz + newspace);
6828 dest = p + *bufsiz;
6829 *bufsiz += newspace;
6830 xnp = (Elf_External_Note *) dest;
6831 H_PUT_32 (abfd, namesz, xnp->namesz);
6832 H_PUT_32 (abfd, size, xnp->descsz);
6833 H_PUT_32 (abfd, type, xnp->type);
6834 strcpy (xnp->name, name);
6835 memcpy (xnp->name + BFD_ALIGN (namesz, 4), input, size);
6836 return p;
6837}
6838
6839#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6840char *
6841elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs)
6842 bfd *abfd;
6843 char *buf;
6844 int *bufsiz;
6845 char *fname;
6846 char *psargs;
6847{
6848 int note_type;
6849 char *note_name = "CORE";
6850
6851#if defined (HAVE_PSINFO_T)
6852 psinfo_t data;
6853 note_type = NT_PSINFO;
6854#else
6855 prpsinfo_t data;
6856 note_type = NT_PRPSINFO;
6857#endif
6858
6859 memset (&data, 0, sizeof (data));
6860 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
6861 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
6862 return elfcore_write_note (abfd, buf, bufsiz,
6863 note_name, note_type, &data, sizeof (data));
6864}
6865#endif /* PSINFO_T or PRPSINFO_T */
6866
6867#if defined (HAVE_PRSTATUS_T)
6868char *
6869elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs)
6870 bfd *abfd;
6871 char *buf;
6872 int *bufsiz;
b87011e9 6873 long pid;
7c76fa91
MS
6874 int cursig;
6875 void *gregs;
6876{
6877 prstatus_t prstat;
6878 char *note_name = "CORE";
6879
6880 memset (&prstat, 0, sizeof (prstat));
6881 prstat.pr_pid = pid;
6882 prstat.pr_cursig = cursig;
c106e334 6883 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
7c76fa91
MS
6884 return elfcore_write_note (abfd, buf, bufsiz,
6885 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
6886}
6887#endif /* HAVE_PRSTATUS_T */
6888
51316059
MS
6889#if defined (HAVE_LWPSTATUS_T)
6890char *
6891elfcore_write_lwpstatus (abfd, buf, bufsiz, pid, cursig, gregs)
6892 bfd *abfd;
6893 char *buf;
6894 int *bufsiz;
6895 long pid;
6896 int cursig;
6897 void *gregs;
6898{
6899 lwpstatus_t lwpstat;
6900 char *note_name = "CORE";
6901
6902 memset (&lwpstat, 0, sizeof (lwpstat));
6903 lwpstat.pr_lwpid = pid >> 16;
6904 lwpstat.pr_cursig = cursig;
6905#if defined (HAVE_LWPSTATUS_T_PR_REG)
6906 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
6907#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6908#if !defined(gregs)
6909 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
6910 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
6911#else
6912 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
6913 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
6914#endif
6915#endif
6916 return elfcore_write_note (abfd, buf, bufsiz, note_name,
6917 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
6918}
6919#endif /* HAVE_LWPSTATUS_T */
6920
7c76fa91
MS
6921#if defined (HAVE_PSTATUS_T)
6922char *
6923elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs)
6924 bfd *abfd;
6925 char *buf;
6926 int *bufsiz;
b87011e9 6927 long pid;
7c76fa91
MS
6928 int cursig;
6929 void *gregs;
6930{
6931 pstatus_t pstat;
6932 char *note_name = "CORE";
6933
51316059
MS
6934 memset (&pstat, 0, sizeof (pstat));
6935 pstat.pr_pid = pid & 0xffff;
6936 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
6937 NT_PSTATUS, &pstat, sizeof (pstat));
6938 return buf;
7c76fa91
MS
6939}
6940#endif /* HAVE_PSTATUS_T */
6941
6942char *
6943elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size)
6944 bfd *abfd;
6945 char *buf;
6946 int *bufsiz;
6947 void *fpregs;
6948 int size;
6949{
6950 char *note_name = "CORE";
6951 return elfcore_write_note (abfd, buf, bufsiz,
6952 note_name, NT_FPREGSET, fpregs, size);
6953}
6954
6955char *
6956elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size)
6957 bfd *abfd;
6958 char *buf;
6959 int *bufsiz;
6960 void *xfpregs;
6961 int size;
6962{
6963 char *note_name = "LINUX";
6964 return elfcore_write_note (abfd, buf, bufsiz,
6965 note_name, NT_PRXFPREG, xfpregs, size);
6966}
6967
252b5132
RH
6968static boolean
6969elfcore_read_notes (abfd, offset, size)
c044fabd 6970 bfd *abfd;
dc810e39
AM
6971 file_ptr offset;
6972 bfd_size_type size;
252b5132 6973{
c044fabd
KH
6974 char *buf;
6975 char *p;
252b5132
RH
6976
6977 if (size <= 0)
6978 return true;
6979
dc810e39 6980 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
252b5132
RH
6981 return false;
6982
dc810e39 6983 buf = bfd_malloc (size);
252b5132
RH
6984 if (buf == NULL)
6985 return false;
6986
dc810e39 6987 if (bfd_bread (buf, size, abfd) != size)
252b5132
RH
6988 {
6989 error:
6990 free (buf);
6991 return false;
6992 }
6993
6994 p = buf;
6995 while (p < buf + size)
6996 {
c044fabd
KH
6997 /* FIXME: bad alignment assumption. */
6998 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
6999 Elf_Internal_Note in;
7000
dc810e39 7001 in.type = H_GET_32 (abfd, xnp->type);
252b5132 7002
dc810e39 7003 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132
RH
7004 in.namedata = xnp->name;
7005
dc810e39 7006 in.descsz = H_GET_32 (abfd, xnp->descsz);
252b5132
RH
7007 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7008 in.descpos = offset + (in.descdata - buf);
7009
50b2bdb7
AM
7010 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7011 {
7012 if (! elfcore_grok_netbsd_note (abfd, &in))
7013 goto error;
7014 }
7015 else
7016 {
7017 if (! elfcore_grok_note (abfd, &in))
7018 goto error;
7019 }
252b5132
RH
7020
7021 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7022 }
7023
7024 free (buf);
7025 return true;
7026}
98d8431c
JB
7027\f
7028/* Providing external access to the ELF program header table. */
7029
7030/* Return an upper bound on the number of bytes required to store a
7031 copy of ABFD's program header table entries. Return -1 if an error
7032 occurs; bfd_get_error will return an appropriate code. */
c044fabd 7033
98d8431c
JB
7034long
7035bfd_get_elf_phdr_upper_bound (abfd)
7036 bfd *abfd;
7037{
7038 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7039 {
7040 bfd_set_error (bfd_error_wrong_format);
7041 return -1;
7042 }
7043
936e320b 7044 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
7045}
7046
98d8431c
JB
7047/* Copy ABFD's program header table entries to *PHDRS. The entries
7048 will be stored as an array of Elf_Internal_Phdr structures, as
7049 defined in include/elf/internal.h. To find out how large the
7050 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7051
7052 Return the number of program header table entries read, or -1 if an
7053 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 7054
98d8431c
JB
7055int
7056bfd_get_elf_phdrs (abfd, phdrs)
7057 bfd *abfd;
7058 void *phdrs;
7059{
7060 int num_phdrs;
7061
7062 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7063 {
7064 bfd_set_error (bfd_error_wrong_format);
7065 return -1;
7066 }
7067
7068 num_phdrs = elf_elfheader (abfd)->e_phnum;
c044fabd 7069 memcpy (phdrs, elf_tdata (abfd)->phdr,
98d8431c
JB
7070 num_phdrs * sizeof (Elf_Internal_Phdr));
7071
7072 return num_phdrs;
7073}
ae4221d7
L
7074
7075void
4e771d61 7076_bfd_elf_sprintf_vma (abfd, buf, value)
cc55aec9 7077 bfd *abfd ATTRIBUTE_UNUSED;
ae4221d7
L
7078 char *buf;
7079 bfd_vma value;
7080{
d3b05f8d 7081#ifdef BFD64
ae4221d7
L
7082 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7083
7084 i_ehdrp = elf_elfheader (abfd);
7085 if (i_ehdrp == NULL)
7086 sprintf_vma (buf, value);
7087 else
7088 {
7089 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
cc55aec9 7090 {
ae4221d7 7091#if BFD_HOST_64BIT_LONG
cc55aec9 7092 sprintf (buf, "%016lx", value);
ae4221d7 7093#else
cc55aec9
AM
7094 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7095 _bfd_int64_low (value));
ae4221d7 7096#endif
cc55aec9 7097 }
ae4221d7
L
7098 else
7099 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7100 }
d3b05f8d
L
7101#else
7102 sprintf_vma (buf, value);
7103#endif
ae4221d7
L
7104}
7105
7106void
4e771d61 7107_bfd_elf_fprintf_vma (abfd, stream, value)
cc55aec9 7108 bfd *abfd ATTRIBUTE_UNUSED;
ae4221d7
L
7109 PTR stream;
7110 bfd_vma value;
7111{
d3b05f8d 7112#ifdef BFD64
ae4221d7
L
7113 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7114
7115 i_ehdrp = elf_elfheader (abfd);
7116 if (i_ehdrp == NULL)
7117 fprintf_vma ((FILE *) stream, value);
7118 else
7119 {
7120 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
cc55aec9 7121 {
ae4221d7 7122#if BFD_HOST_64BIT_LONG
cc55aec9 7123 fprintf ((FILE *) stream, "%016lx", value);
ae4221d7 7124#else
cc55aec9
AM
7125 fprintf ((FILE *) stream, "%08lx%08lx",
7126 _bfd_int64_high (value), _bfd_int64_low (value));
ae4221d7 7127#endif
cc55aec9 7128 }
ae4221d7
L
7129 else
7130 fprintf ((FILE *) stream, "%08lx",
7131 (unsigned long) (value & 0xffffffff));
7132 }
d3b05f8d
L
7133#else
7134 fprintf_vma ((FILE *) stream, value);
7135#endif
ae4221d7 7136}
db6751f2
JJ
7137
7138enum elf_reloc_type_class
f51e552e
AM
7139_bfd_elf_reloc_type_class (rela)
7140 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
db6751f2
JJ
7141{
7142 return reloc_class_normal;
7143}
f8df10f4
JJ
7144
7145/* For RELA architectures, return what the relocation value for
7146 relocation against a local symbol. */
7147
7148bfd_vma
7149_bfd_elf_rela_local_sym (abfd, sym, sec, rel)
7150 bfd *abfd;
7151 Elf_Internal_Sym *sym;
7152 asection *sec;
7153 Elf_Internal_Rela *rel;
7154{
7155 bfd_vma relocation;
7156
7157 relocation = (sec->output_section->vma
7158 + sec->output_offset
7159 + sym->st_value);
7160 if ((sec->flags & SEC_MERGE)
c629eae0 7161 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
65765700 7162 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
f8df10f4
JJ
7163 {
7164 asection *msec;
7165
7166 msec = sec;
7167 rel->r_addend =
7168 _bfd_merged_section_offset (abfd, &msec,
65765700 7169 elf_section_data (sec)->sec_info,
f8df10f4
JJ
7170 sym->st_value + rel->r_addend,
7171 (bfd_vma) 0)
7172 - relocation;
7173 rel->r_addend += msec->output_section->vma + msec->output_offset;
7174 }
7175 return relocation;
7176}
c629eae0
JJ
7177
7178bfd_vma
7179_bfd_elf_rel_local_sym (abfd, sym, psec, addend)
7180 bfd *abfd;
7181 Elf_Internal_Sym *sym;
7182 asection **psec;
7183 bfd_vma addend;
7184{
7185 asection *sec = *psec;
7186
65765700 7187 if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_MERGE)
c629eae0
JJ
7188 return sym->st_value + addend;
7189
7190 return _bfd_merged_section_offset (abfd, psec,
65765700 7191 elf_section_data (sec)->sec_info,
c629eae0
JJ
7192 sym->st_value + addend, (bfd_vma) 0);
7193}
7194
7195bfd_vma
7196_bfd_elf_section_offset (abfd, info, sec, offset)
7197 bfd *abfd;
7198 struct bfd_link_info *info;
7199 asection *sec;
7200 bfd_vma offset;
7201{
7202 struct bfd_elf_section_data *sec_data;
7203
7204 sec_data = elf_section_data (sec);
65765700
JJ
7205 switch (sec_data->sec_info_type)
7206 {
7207 case ELF_INFO_TYPE_STABS:
7208 return _bfd_stab_section_offset
7209 (abfd, &elf_hash_table (info)->merge_info, sec, &sec_data->sec_info,
7210 offset);
7211 case ELF_INFO_TYPE_EH_FRAME:
7212 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
7213 default:
7214 return offset;
7215 }
c629eae0 7216}
This page took 0.510989 seconds and 4 git commands to generate.