]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf.c
* gdbint.texinfo (Releasing GDB): Fix typos in @itemize @bullet
[thirdparty/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
dc810e39
AM
4691 amt = (bfd_size_type) section_count * sizeof (asection *);
4692 sections = (asection **) bfd_malloc (amt);
252b5132
RH
4693 if (sections == NULL)
4694 return false;
4695
4696 /* Step One: Scan for segment vs section LMA conflicts.
4697 Also add the sections to the section array allocated above.
4698 Also add the sections to the current segment. In the common
4699 case, where the sections have not been moved, this means that
4700 we have completely filled the segment, and there is nothing
4701 more to do. */
252b5132 4702 isec = 0;
72730e0c 4703 matching_lma = 0;
252b5132
RH
4704 suggested_lma = 0;
4705
bc67d8a6
NC
4706 for (j = 0, section = ibfd->sections;
4707 section != NULL;
4708 section = section->next)
252b5132 4709 {
caf47ea6 4710 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c0f7859b 4711 {
bc67d8a6
NC
4712 output_section = section->output_section;
4713
4714 sections[j ++] = section;
252b5132
RH
4715
4716 /* The Solaris native linker always sets p_paddr to 0.
4717 We try to catch that case here, and set it to the
4718 correct value. */
bc67d8a6
NC
4719 if (segment->p_paddr == 0
4720 && segment->p_vaddr != 0
252b5132 4721 && isec == 0
bc67d8a6
NC
4722 && output_section->lma != 0
4723 && (output_section->vma == (segment->p_vaddr
4724 + (map->includes_filehdr
4725 ? iehdr->e_ehsize
4726 : 0)
4727 + (map->includes_phdrs
079e9a2f
AM
4728 ? (iehdr->e_phnum
4729 * iehdr->e_phentsize)
bc67d8a6
NC
4730 : 0))))
4731 map->p_paddr = segment->p_vaddr;
252b5132
RH
4732
4733 /* Match up the physical address of the segment with the
4734 LMA address of the output section. */
bc67d8a6 4735 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
caf47ea6 4736 || IS_CONTAINED_BY_FILEPOS (section, segment, bed)
bc67d8a6 4737 || IS_COREFILE_NOTE (segment, section))
252b5132
RH
4738 {
4739 if (matching_lma == 0)
bc67d8a6 4740 matching_lma = output_section->lma;
252b5132
RH
4741
4742 /* We assume that if the section fits within the segment
bc67d8a6 4743 then it does not overlap any other section within that
252b5132 4744 segment. */
bc67d8a6 4745 map->sections[isec ++] = output_section;
252b5132
RH
4746 }
4747 else if (suggested_lma == 0)
bc67d8a6 4748 suggested_lma = output_section->lma;
252b5132
RH
4749 }
4750 }
4751
bc67d8a6 4752 BFD_ASSERT (j == section_count);
252b5132
RH
4753
4754 /* Step Two: Adjust the physical address of the current segment,
4755 if necessary. */
bc67d8a6 4756 if (isec == section_count)
252b5132
RH
4757 {
4758 /* All of the sections fitted within the segment as currently
4759 specified. This is the default case. Add the segment to
4760 the list of built segments and carry on to process the next
4761 program header in the input BFD. */
bc67d8a6 4762 map->count = section_count;
c044fabd
KH
4763 *pointer_to_map = map;
4764 pointer_to_map = &map->next;
252b5132
RH
4765
4766 free (sections);
4767 continue;
4768 }
252b5132
RH
4769 else
4770 {
72730e0c
AM
4771 if (matching_lma != 0)
4772 {
4773 /* At least one section fits inside the current segment.
4774 Keep it, but modify its physical address to match the
4775 LMA of the first section that fitted. */
bc67d8a6 4776 map->p_paddr = matching_lma;
72730e0c
AM
4777 }
4778 else
4779 {
4780 /* None of the sections fitted inside the current segment.
4781 Change the current segment's physical address to match
4782 the LMA of the first section. */
bc67d8a6 4783 map->p_paddr = suggested_lma;
72730e0c
AM
4784 }
4785
bc67d8a6
NC
4786 /* Offset the segment physical address from the lma
4787 to allow for space taken up by elf headers. */
4788 if (map->includes_filehdr)
4789 map->p_paddr -= iehdr->e_ehsize;
252b5132 4790
bc67d8a6
NC
4791 if (map->includes_phdrs)
4792 {
4793 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4794
4795 /* iehdr->e_phnum is just an estimate of the number
4796 of program headers that we will need. Make a note
4797 here of the number we used and the segment we chose
4798 to hold these headers, so that we can adjust the
4799 offset when we know the correct value. */
4800 phdr_adjust_num = iehdr->e_phnum;
4801 phdr_adjust_seg = map;
4802 }
252b5132
RH
4803 }
4804
4805 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 4806 those that fit to the current segment and removing them from the
252b5132
RH
4807 sections array; but making sure not to leave large gaps. Once all
4808 possible sections have been assigned to the current segment it is
4809 added to the list of built segments and if sections still remain
4810 to be assigned, a new segment is constructed before repeating
4811 the loop. */
4812 isec = 0;
4813 do
4814 {
bc67d8a6 4815 map->count = 0;
252b5132
RH
4816 suggested_lma = 0;
4817
4818 /* Fill the current segment with sections that fit. */
bc67d8a6 4819 for (j = 0; j < section_count; j++)
252b5132 4820 {
bc67d8a6 4821 section = sections[j];
252b5132 4822
bc67d8a6 4823 if (section == NULL)
252b5132
RH
4824 continue;
4825
bc67d8a6 4826 output_section = section->output_section;
252b5132 4827
bc67d8a6 4828 BFD_ASSERT (output_section != NULL);
c044fabd 4829
bc67d8a6
NC
4830 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4831 || IS_COREFILE_NOTE (segment, section))
252b5132 4832 {
bc67d8a6 4833 if (map->count == 0)
252b5132
RH
4834 {
4835 /* If the first section in a segment does not start at
bc67d8a6
NC
4836 the beginning of the segment, then something is
4837 wrong. */
4838 if (output_section->lma !=
4839 (map->p_paddr
4840 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4841 + (map->includes_phdrs
4842 ? iehdr->e_phnum * iehdr->e_phentsize
4843 : 0)))
252b5132
RH
4844 abort ();
4845 }
4846 else
4847 {
4848 asection * prev_sec;
252b5132 4849
bc67d8a6 4850 prev_sec = map->sections[map->count - 1];
252b5132
RH
4851
4852 /* If the gap between the end of the previous section
bc67d8a6
NC
4853 and the start of this section is more than
4854 maxpagesize then we need to start a new segment. */
079e9a2f
AM
4855 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
4856 maxpagesize)
caf47ea6 4857 < BFD_ALIGN (output_section->lma, maxpagesize))
079e9a2f
AM
4858 || ((prev_sec->lma + prev_sec->_raw_size)
4859 > output_section->lma))
252b5132
RH
4860 {
4861 if (suggested_lma == 0)
bc67d8a6 4862 suggested_lma = output_section->lma;
252b5132
RH
4863
4864 continue;
4865 }
4866 }
4867
bc67d8a6 4868 map->sections[map->count++] = output_section;
252b5132
RH
4869 ++isec;
4870 sections[j] = NULL;
bc67d8a6 4871 section->segment_mark = true;
252b5132
RH
4872 }
4873 else if (suggested_lma == 0)
bc67d8a6 4874 suggested_lma = output_section->lma;
252b5132
RH
4875 }
4876
bc67d8a6 4877 BFD_ASSERT (map->count > 0);
252b5132
RH
4878
4879 /* Add the current segment to the list of built segments. */
c044fabd
KH
4880 *pointer_to_map = map;
4881 pointer_to_map = &map->next;
252b5132 4882
bc67d8a6 4883 if (isec < section_count)
252b5132
RH
4884 {
4885 /* We still have not allocated all of the sections to
4886 segments. Create a new segment here, initialise it
4887 and carry on looping. */
dc810e39
AM
4888 amt = sizeof (struct elf_segment_map);
4889 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4890 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
bc67d8a6 4891 if (map == NULL)
252b5132
RH
4892 return false;
4893
4894 /* Initialise the fields of the segment map. Set the physical
4895 physical address to the LMA of the first section that has
4896 not yet been assigned. */
bc67d8a6
NC
4897 map->next = NULL;
4898 map->p_type = segment->p_type;
4899 map->p_flags = segment->p_flags;
4900 map->p_flags_valid = 1;
4901 map->p_paddr = suggested_lma;
4902 map->p_paddr_valid = 1;
4903 map->includes_filehdr = 0;
4904 map->includes_phdrs = 0;
252b5132
RH
4905 }
4906 }
bc67d8a6 4907 while (isec < section_count);
252b5132
RH
4908
4909 free (sections);
4910 }
4911
4912 /* The Solaris linker creates program headers in which all the
4913 p_paddr fields are zero. When we try to objcopy or strip such a
4914 file, we get confused. Check for this case, and if we find it
4915 reset the p_paddr_valid fields. */
bc67d8a6
NC
4916 for (map = map_first; map != NULL; map = map->next)
4917 if (map->p_paddr != 0)
252b5132 4918 break;
bc67d8a6 4919 if (map == NULL)
252b5132 4920 {
bc67d8a6
NC
4921 for (map = map_first; map != NULL; map = map->next)
4922 map->p_paddr_valid = 0;
252b5132
RH
4923 }
4924
bc67d8a6
NC
4925 elf_tdata (obfd)->segment_map = map_first;
4926
4927 /* If we had to estimate the number of program headers that were
9ad5cbcf 4928 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
4929 the offset if necessary. */
4930 if (phdr_adjust_seg != NULL)
4931 {
4932 unsigned int count;
c044fabd 4933
bc67d8a6 4934 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 4935 count++;
252b5132 4936
bc67d8a6
NC
4937 if (count > phdr_adjust_num)
4938 phdr_adjust_seg->p_paddr
4939 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4940 }
c044fabd 4941
252b5132 4942#if 0
c044fabd
KH
4943 /* Final Step: Sort the segments into ascending order of physical
4944 address. */
bc67d8a6 4945 if (map_first != NULL)
252b5132 4946 {
c044fabd 4947 struct elf_segment_map *prev;
252b5132 4948
bc67d8a6
NC
4949 prev = map_first;
4950 for (map = map_first->next; map != NULL; prev = map, map = map->next)
252b5132 4951 {
bc67d8a6
NC
4952 /* Yes I know - its a bubble sort.... */
4953 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
252b5132 4954 {
bc67d8a6
NC
4955 /* Swap map and map->next. */
4956 prev->next = map->next;
4957 map->next = map->next->next;
4958 prev->next->next = map;
252b5132 4959
bc67d8a6
NC
4960 /* Restart loop. */
4961 map = map_first;
252b5132
RH
4962 }
4963 }
4964 }
4965#endif
4966
bc67d8a6
NC
4967#undef SEGMENT_END
4968#undef IS_CONTAINED_BY_VMA
4969#undef IS_CONTAINED_BY_LMA
caf47ea6 4970#undef IS_CONTAINED_BY_FILEPOS
252b5132 4971#undef IS_COREFILE_NOTE
bc67d8a6
NC
4972#undef IS_SOLARIS_PT_INTERP
4973#undef INCLUDE_SECTION_IN_SEGMENT
4974#undef SEGMENT_AFTER_SEGMENT
4975#undef SEGMENT_OVERLAPS
252b5132
RH
4976 return true;
4977}
4978
4979/* Copy private section information. This copies over the entsize
4980 field, and sometimes the info field. */
4981
4982boolean
4983_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4984 bfd *ibfd;
4985 asection *isec;
4986 bfd *obfd;
4987 asection *osec;
4988{
4989 Elf_Internal_Shdr *ihdr, *ohdr;
caf47ea6 4990 const struct elf_backend_data *bed = get_elf_backend_data (ibfd);
252b5132
RH
4991
4992 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4993 || obfd->xvec->flavour != bfd_target_elf_flavour)
4994 return true;
4995
4996 /* Copy over private BFD data if it has not already been copied.
4997 This must be done here, rather than in the copy_private_bfd_data
4998 entry point, because the latter is called after the section
4999 contents have been set, which means that the program headers have
caf47ea6
AM
5000 already been worked out. The backend function provides a way to
5001 override the test conditions and code path for the call to
5002 copy_private_bfd_data. */
5003 if (bed->copy_private_bfd_data_p)
252b5132 5004 {
caf47ea6
AM
5005 if ((*bed->copy_private_bfd_data_p) (ibfd, isec, obfd, osec))
5006 if (! copy_private_bfd_data (ibfd, obfd))
5007 return false;
5008 }
5009 else if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5010 {
5011 asection *s;
5012
5013 /* Only set up the segments if there are no more SEC_ALLOC
5014 sections. FIXME: This won't do the right thing if objcopy is
5015 used to remove the last SEC_ALLOC section, since objcopy
5016 won't call this routine in that case. */
5017 for (s = isec->next; s != NULL; s = s->next)
5018 if ((s->flags & SEC_ALLOC) != 0)
5019 break;
5020 if (s == NULL)
5021 {
5022 if (! copy_private_bfd_data (ibfd, obfd))
5023 return false;
5024 }
252b5132
RH
5025 }
5026
5027 ihdr = &elf_section_data (isec)->this_hdr;
5028 ohdr = &elf_section_data (osec)->this_hdr;
5029
5030 ohdr->sh_entsize = ihdr->sh_entsize;
5031
5032 if (ihdr->sh_type == SHT_SYMTAB
5033 || ihdr->sh_type == SHT_DYNSYM
5034 || ihdr->sh_type == SHT_GNU_verneed
5035 || ihdr->sh_type == SHT_GNU_verdef)
5036 ohdr->sh_info = ihdr->sh_info;
5037
9dce4196
AM
5038 /* Set things up for objcopy. The output SHT_GROUP section will
5039 have its elf_next_in_group pointing back to the input group
5040 members. */
5041 elf_next_in_group (osec) = elf_next_in_group (isec);
5042 elf_group_name (osec) = elf_group_name (isec);
5043
bf572ba0
MM
5044 elf_section_data (osec)->use_rela_p
5045 = elf_section_data (isec)->use_rela_p;
5046
252b5132
RH
5047 return true;
5048}
5049
5050/* Copy private symbol information. If this symbol is in a section
5051 which we did not map into a BFD section, try to map the section
5052 index correctly. We use special macro definitions for the mapped
5053 section indices; these definitions are interpreted by the
5054 swap_out_syms function. */
5055
9ad5cbcf
AM
5056#define MAP_ONESYMTAB (SHN_HIOS + 1)
5057#define MAP_DYNSYMTAB (SHN_HIOS + 2)
5058#define MAP_STRTAB (SHN_HIOS + 3)
5059#define MAP_SHSTRTAB (SHN_HIOS + 4)
5060#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132
RH
5061
5062boolean
5063_bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
5064 bfd *ibfd;
5065 asymbol *isymarg;
5066 bfd *obfd;
5067 asymbol *osymarg;
5068{
5069 elf_symbol_type *isym, *osym;
5070
5071 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5072 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5073 return true;
5074
5075 isym = elf_symbol_from (ibfd, isymarg);
5076 osym = elf_symbol_from (obfd, osymarg);
5077
5078 if (isym != NULL
5079 && osym != NULL
5080 && bfd_is_abs_section (isym->symbol.section))
5081 {
5082 unsigned int shndx;
5083
5084 shndx = isym->internal_elf_sym.st_shndx;
5085 if (shndx == elf_onesymtab (ibfd))
5086 shndx = MAP_ONESYMTAB;
5087 else if (shndx == elf_dynsymtab (ibfd))
5088 shndx = MAP_DYNSYMTAB;
5089 else if (shndx == elf_tdata (ibfd)->strtab_section)
5090 shndx = MAP_STRTAB;
5091 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5092 shndx = MAP_SHSTRTAB;
9ad5cbcf
AM
5093 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5094 shndx = MAP_SYM_SHNDX;
252b5132
RH
5095 osym->internal_elf_sym.st_shndx = shndx;
5096 }
5097
5098 return true;
5099}
5100
5101/* Swap out the symbols. */
5102
5103static boolean
5104swap_out_syms (abfd, sttp, relocatable_p)
5105 bfd *abfd;
5106 struct bfd_strtab_hash **sttp;
5107 int relocatable_p;
5108{
079e9a2f
AM
5109 struct elf_backend_data *bed;
5110 int symcount;
5111 asymbol **syms;
5112 struct bfd_strtab_hash *stt;
5113 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 5114 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f
AM
5115 Elf_Internal_Shdr *symstrtab_hdr;
5116 char *outbound_syms;
9ad5cbcf 5117 char *outbound_shndx;
079e9a2f
AM
5118 int idx;
5119 bfd_size_type amt;
252b5132
RH
5120
5121 if (!elf_map_symbols (abfd))
5122 return false;
5123
c044fabd 5124 /* Dump out the symtabs. */
079e9a2f
AM
5125 stt = _bfd_elf_stringtab_init ();
5126 if (stt == NULL)
5127 return false;
252b5132 5128
079e9a2f
AM
5129 bed = get_elf_backend_data (abfd);
5130 symcount = bfd_get_symcount (abfd);
5131 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5132 symtab_hdr->sh_type = SHT_SYMTAB;
5133 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5134 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5135 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5136 symtab_hdr->sh_addralign = bed->s->file_align;
5137
5138 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5139 symstrtab_hdr->sh_type = SHT_STRTAB;
5140
5141 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5142 outbound_syms = bfd_alloc (abfd, amt);
5143 if (outbound_syms == NULL)
5144 return false;
5145 symtab_hdr->contents = (PTR) outbound_syms;
252b5132 5146
9ad5cbcf
AM
5147 outbound_shndx = NULL;
5148 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5149 if (symtab_shndx_hdr->sh_name != 0)
5150 {
5151 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
1126897b 5152 outbound_shndx = bfd_zalloc (abfd, amt);
9ad5cbcf
AM
5153 if (outbound_shndx == NULL)
5154 return false;
9ad5cbcf
AM
5155 symtab_shndx_hdr->contents = outbound_shndx;
5156 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5157 symtab_shndx_hdr->sh_size = amt;
5158 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5159 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5160 }
5161
079e9a2f
AM
5162 /* now generate the data (for "contents") */
5163 {
5164 /* Fill in zeroth symbol and swap it out. */
5165 Elf_Internal_Sym sym;
5166 sym.st_name = 0;
5167 sym.st_value = 0;
5168 sym.st_size = 0;
5169 sym.st_info = 0;
5170 sym.st_other = 0;
5171 sym.st_shndx = SHN_UNDEF;
9ad5cbcf 5172 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 5173 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
5174 if (outbound_shndx != NULL)
5175 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 5176 }
252b5132 5177
079e9a2f
AM
5178 syms = bfd_get_outsymbols (abfd);
5179 for (idx = 0; idx < symcount; idx++)
252b5132 5180 {
252b5132 5181 Elf_Internal_Sym sym;
079e9a2f
AM
5182 bfd_vma value = syms[idx]->value;
5183 elf_symbol_type *type_ptr;
5184 flagword flags = syms[idx]->flags;
5185 int type;
252b5132 5186
079e9a2f
AM
5187 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5188 {
5189 /* Local section symbols have no name. */
5190 sym.st_name = 0;
5191 }
5192 else
5193 {
5194 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5195 syms[idx]->name,
5196 true, false);
5197 if (sym.st_name == (unsigned long) -1)
5198 return false;
5199 }
252b5132 5200
079e9a2f 5201 type_ptr = elf_symbol_from (abfd, syms[idx]);
252b5132 5202
079e9a2f
AM
5203 if ((flags & BSF_SECTION_SYM) == 0
5204 && bfd_is_com_section (syms[idx]->section))
5205 {
5206 /* ELF common symbols put the alignment into the `value' field,
5207 and the size into the `size' field. This is backwards from
5208 how BFD handles it, so reverse it here. */
5209 sym.st_size = value;
5210 if (type_ptr == NULL
5211 || type_ptr->internal_elf_sym.st_value == 0)
5212 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5213 else
5214 sym.st_value = type_ptr->internal_elf_sym.st_value;
5215 sym.st_shndx = _bfd_elf_section_from_bfd_section
5216 (abfd, syms[idx]->section);
5217 }
5218 else
5219 {
5220 asection *sec = syms[idx]->section;
5221 int shndx;
252b5132 5222
079e9a2f
AM
5223 if (sec->output_section)
5224 {
5225 value += sec->output_offset;
5226 sec = sec->output_section;
5227 }
5228 /* Don't add in the section vma for relocatable output. */
5229 if (! relocatable_p)
5230 value += sec->vma;
5231 sym.st_value = value;
5232 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5233
5234 if (bfd_is_abs_section (sec)
5235 && type_ptr != NULL
5236 && type_ptr->internal_elf_sym.st_shndx != 0)
5237 {
5238 /* This symbol is in a real ELF section which we did
5239 not create as a BFD section. Undo the mapping done
5240 by copy_private_symbol_data. */
5241 shndx = type_ptr->internal_elf_sym.st_shndx;
5242 switch (shndx)
5243 {
5244 case MAP_ONESYMTAB:
5245 shndx = elf_onesymtab (abfd);
5246 break;
5247 case MAP_DYNSYMTAB:
5248 shndx = elf_dynsymtab (abfd);
5249 break;
5250 case MAP_STRTAB:
5251 shndx = elf_tdata (abfd)->strtab_section;
5252 break;
5253 case MAP_SHSTRTAB:
5254 shndx = elf_tdata (abfd)->shstrtab_section;
5255 break;
9ad5cbcf
AM
5256 case MAP_SYM_SHNDX:
5257 shndx = elf_tdata (abfd)->symtab_shndx_section;
5258 break;
079e9a2f
AM
5259 default:
5260 break;
5261 }
5262 }
5263 else
5264 {
5265 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 5266
079e9a2f
AM
5267 if (shndx == -1)
5268 {
5269 asection *sec2;
5270
5271 /* Writing this would be a hell of a lot easier if
5272 we had some decent documentation on bfd, and
5273 knew what to expect of the library, and what to
5274 demand of applications. For example, it
5275 appears that `objcopy' might not set the
5276 section of a symbol to be a section that is
5277 actually in the output file. */
5278 sec2 = bfd_get_section_by_name (abfd, sec->name);
5279 BFD_ASSERT (sec2 != 0);
5280 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5281 BFD_ASSERT (shndx != -1);
5282 }
5283 }
252b5132 5284
079e9a2f
AM
5285 sym.st_shndx = shndx;
5286 }
252b5132 5287
13ae64f3
JJ
5288 if ((flags & BSF_THREAD_LOCAL) != 0)
5289 type = STT_TLS;
5290 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
5291 type = STT_FUNC;
5292 else if ((flags & BSF_OBJECT) != 0)
5293 type = STT_OBJECT;
5294 else
5295 type = STT_NOTYPE;
252b5132 5296
13ae64f3
JJ
5297 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5298 type = STT_TLS;
5299
079e9a2f
AM
5300 /* Processor-specific types */
5301 if (type_ptr != NULL
5302 && bed->elf_backend_get_symbol_type)
5303 type = ((*bed->elf_backend_get_symbol_type)
5304 (&type_ptr->internal_elf_sym, type));
252b5132 5305
079e9a2f
AM
5306 if (flags & BSF_SECTION_SYM)
5307 {
5308 if (flags & BSF_GLOBAL)
5309 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5310 else
5311 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5312 }
5313 else if (bfd_is_com_section (syms[idx]->section))
5314 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5315 else if (bfd_is_und_section (syms[idx]->section))
5316 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5317 ? STB_WEAK
5318 : STB_GLOBAL),
5319 type);
5320 else if (flags & BSF_FILE)
5321 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5322 else
5323 {
5324 int bind = STB_LOCAL;
252b5132 5325
079e9a2f
AM
5326 if (flags & BSF_LOCAL)
5327 bind = STB_LOCAL;
5328 else if (flags & BSF_WEAK)
5329 bind = STB_WEAK;
5330 else if (flags & BSF_GLOBAL)
5331 bind = STB_GLOBAL;
252b5132 5332
079e9a2f
AM
5333 sym.st_info = ELF_ST_INFO (bind, type);
5334 }
252b5132 5335
079e9a2f
AM
5336 if (type_ptr != NULL)
5337 sym.st_other = type_ptr->internal_elf_sym.st_other;
5338 else
5339 sym.st_other = 0;
252b5132 5340
9ad5cbcf 5341 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 5342 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
5343 if (outbound_shndx != NULL)
5344 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 5345 }
252b5132 5346
079e9a2f
AM
5347 *sttp = stt;
5348 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5349 symstrtab_hdr->sh_type = SHT_STRTAB;
252b5132 5350
079e9a2f
AM
5351 symstrtab_hdr->sh_flags = 0;
5352 symstrtab_hdr->sh_addr = 0;
5353 symstrtab_hdr->sh_entsize = 0;
5354 symstrtab_hdr->sh_link = 0;
5355 symstrtab_hdr->sh_info = 0;
5356 symstrtab_hdr->sh_addralign = 1;
252b5132
RH
5357
5358 return true;
5359}
5360
5361/* Return the number of bytes required to hold the symtab vector.
5362
5363 Note that we base it on the count plus 1, since we will null terminate
5364 the vector allocated based on this size. However, the ELF symbol table
5365 always has a dummy entry as symbol #0, so it ends up even. */
5366
5367long
5368_bfd_elf_get_symtab_upper_bound (abfd)
5369 bfd *abfd;
5370{
5371 long symcount;
5372 long symtab_size;
5373 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5374
5375 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
5376 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5377 if (symcount > 0)
5378 symtab_size -= sizeof (asymbol *);
252b5132
RH
5379
5380 return symtab_size;
5381}
5382
5383long
5384_bfd_elf_get_dynamic_symtab_upper_bound (abfd)
5385 bfd *abfd;
5386{
5387 long symcount;
5388 long symtab_size;
5389 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5390
5391 if (elf_dynsymtab (abfd) == 0)
5392 {
5393 bfd_set_error (bfd_error_invalid_operation);
5394 return -1;
5395 }
5396
5397 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
5398 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5399 if (symcount > 0)
5400 symtab_size -= sizeof (asymbol *);
252b5132
RH
5401
5402 return symtab_size;
5403}
5404
5405long
5406_bfd_elf_get_reloc_upper_bound (abfd, asect)
7442e600 5407 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
5408 sec_ptr asect;
5409{
5410 return (asect->reloc_count + 1) * sizeof (arelent *);
5411}
5412
5413/* Canonicalize the relocs. */
5414
5415long
5416_bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
5417 bfd *abfd;
5418 sec_ptr section;
5419 arelent **relptr;
5420 asymbol **symbols;
5421{
5422 arelent *tblptr;
5423 unsigned int i;
dbb410c3 5424 struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5425
dbb410c3 5426 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
252b5132
RH
5427 return -1;
5428
5429 tblptr = section->relocation;
5430 for (i = 0; i < section->reloc_count; i++)
5431 *relptr++ = tblptr++;
5432
5433 *relptr = NULL;
5434
5435 return section->reloc_count;
5436}
5437
5438long
5439_bfd_elf_get_symtab (abfd, alocation)
5440 bfd *abfd;
5441 asymbol **alocation;
5442{
dbb410c3
AM
5443 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5444 long symcount = bed->s->slurp_symbol_table (abfd, alocation, false);
252b5132
RH
5445
5446 if (symcount >= 0)
5447 bfd_get_symcount (abfd) = symcount;
5448 return symcount;
5449}
5450
5451long
5452_bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
5453 bfd *abfd;
5454 asymbol **alocation;
5455{
dbb410c3
AM
5456 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5457 return bed->s->slurp_symbol_table (abfd, alocation, true);
252b5132
RH
5458}
5459
5460/* Return the size required for the dynamic reloc entries. Any
5461 section that was actually installed in the BFD, and has type
5462 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5463 considered to be a dynamic reloc section. */
5464
5465long
5466_bfd_elf_get_dynamic_reloc_upper_bound (abfd)
5467 bfd *abfd;
5468{
5469 long ret;
5470 asection *s;
5471
5472 if (elf_dynsymtab (abfd) == 0)
5473 {
5474 bfd_set_error (bfd_error_invalid_operation);
5475 return -1;
5476 }
5477
5478 ret = sizeof (arelent *);
5479 for (s = abfd->sections; s != NULL; s = s->next)
5480 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5481 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5482 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5483 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5484 * sizeof (arelent *));
5485
5486 return ret;
5487}
5488
5489/* Canonicalize the dynamic relocation entries. Note that we return
5490 the dynamic relocations as a single block, although they are
5491 actually associated with particular sections; the interface, which
5492 was designed for SunOS style shared libraries, expects that there
5493 is only one set of dynamic relocs. Any section that was actually
5494 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5495 the dynamic symbol table, is considered to be a dynamic reloc
5496 section. */
5497
5498long
5499_bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5500 bfd *abfd;
5501 arelent **storage;
5502 asymbol **syms;
5503{
5504 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
5505 asection *s;
5506 long ret;
5507
5508 if (elf_dynsymtab (abfd) == 0)
5509 {
5510 bfd_set_error (bfd_error_invalid_operation);
5511 return -1;
5512 }
5513
5514 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5515 ret = 0;
5516 for (s = abfd->sections; s != NULL; s = s->next)
5517 {
5518 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5519 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5520 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5521 {
5522 arelent *p;
5523 long count, i;
5524
5525 if (! (*slurp_relocs) (abfd, s, syms, true))
5526 return -1;
5527 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5528 p = s->relocation;
5529 for (i = 0; i < count; i++)
5530 *storage++ = p++;
5531 ret += count;
5532 }
5533 }
5534
5535 *storage = NULL;
5536
5537 return ret;
5538}
5539\f
5540/* Read in the version information. */
5541
5542boolean
5543_bfd_elf_slurp_version_tables (abfd)
5544 bfd *abfd;
5545{
5546 bfd_byte *contents = NULL;
dc810e39 5547 bfd_size_type amt;
252b5132
RH
5548
5549 if (elf_dynverdef (abfd) != 0)
5550 {
5551 Elf_Internal_Shdr *hdr;
5552 Elf_External_Verdef *everdef;
5553 Elf_Internal_Verdef *iverdef;
f631889e
UD
5554 Elf_Internal_Verdef *iverdefarr;
5555 Elf_Internal_Verdef iverdefmem;
252b5132 5556 unsigned int i;
062e2358 5557 unsigned int maxidx;
252b5132
RH
5558
5559 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5560
252b5132
RH
5561 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5562 if (contents == NULL)
5563 goto error_return;
5564 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
dc810e39 5565 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
252b5132
RH
5566 goto error_return;
5567
f631889e
UD
5568 /* We know the number of entries in the section but not the maximum
5569 index. Therefore we have to run through all entries and find
5570 the maximum. */
252b5132 5571 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
5572 maxidx = 0;
5573 for (i = 0; i < hdr->sh_info; ++i)
5574 {
5575 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5576
062e2358
AM
5577 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5578 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e
UD
5579
5580 everdef = ((Elf_External_Verdef *)
5581 ((bfd_byte *) everdef + iverdefmem.vd_next));
5582 }
5583
dc810e39
AM
5584 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5585 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
f631889e
UD
5586 if (elf_tdata (abfd)->verdef == NULL)
5587 goto error_return;
5588
5589 elf_tdata (abfd)->cverdefs = maxidx;
5590
5591 everdef = (Elf_External_Verdef *) contents;
5592 iverdefarr = elf_tdata (abfd)->verdef;
5593 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
5594 {
5595 Elf_External_Verdaux *everdaux;
5596 Elf_Internal_Verdaux *iverdaux;
5597 unsigned int j;
5598
f631889e
UD
5599 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5600
5601 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5602 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
252b5132
RH
5603
5604 iverdef->vd_bfd = abfd;
5605
dc810e39
AM
5606 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5607 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
252b5132
RH
5608 if (iverdef->vd_auxptr == NULL)
5609 goto error_return;
5610
5611 everdaux = ((Elf_External_Verdaux *)
5612 ((bfd_byte *) everdef + iverdef->vd_aux));
5613 iverdaux = iverdef->vd_auxptr;
5614 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5615 {
5616 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5617
5618 iverdaux->vda_nodename =
5619 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5620 iverdaux->vda_name);
5621 if (iverdaux->vda_nodename == NULL)
5622 goto error_return;
5623
5624 if (j + 1 < iverdef->vd_cnt)
5625 iverdaux->vda_nextptr = iverdaux + 1;
5626 else
5627 iverdaux->vda_nextptr = NULL;
5628
5629 everdaux = ((Elf_External_Verdaux *)
5630 ((bfd_byte *) everdaux + iverdaux->vda_next));
5631 }
5632
5633 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5634
5635 if (i + 1 < hdr->sh_info)
5636 iverdef->vd_nextdef = iverdef + 1;
5637 else
5638 iverdef->vd_nextdef = NULL;
5639
5640 everdef = ((Elf_External_Verdef *)
5641 ((bfd_byte *) everdef + iverdef->vd_next));
5642 }
5643
5644 free (contents);
5645 contents = NULL;
5646 }
5647
5648 if (elf_dynverref (abfd) != 0)
5649 {
5650 Elf_Internal_Shdr *hdr;
5651 Elf_External_Verneed *everneed;
5652 Elf_Internal_Verneed *iverneed;
5653 unsigned int i;
5654
5655 hdr = &elf_tdata (abfd)->dynverref_hdr;
5656
dc810e39 5657 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
252b5132 5658 elf_tdata (abfd)->verref =
dc810e39 5659 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
252b5132
RH
5660 if (elf_tdata (abfd)->verref == NULL)
5661 goto error_return;
5662
5663 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5664
5665 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5666 if (contents == NULL)
5667 goto error_return;
5668 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
dc810e39 5669 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
252b5132
RH
5670 goto error_return;
5671
5672 everneed = (Elf_External_Verneed *) contents;
5673 iverneed = elf_tdata (abfd)->verref;
5674 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5675 {
5676 Elf_External_Vernaux *evernaux;
5677 Elf_Internal_Vernaux *ivernaux;
5678 unsigned int j;
5679
5680 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5681
5682 iverneed->vn_bfd = abfd;
5683
5684 iverneed->vn_filename =
5685 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5686 iverneed->vn_file);
5687 if (iverneed->vn_filename == NULL)
5688 goto error_return;
5689
dc810e39
AM
5690 amt = iverneed->vn_cnt;
5691 amt *= sizeof (Elf_Internal_Vernaux);
5692 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
252b5132
RH
5693
5694 evernaux = ((Elf_External_Vernaux *)
5695 ((bfd_byte *) everneed + iverneed->vn_aux));
5696 ivernaux = iverneed->vn_auxptr;
5697 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5698 {
5699 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5700
5701 ivernaux->vna_nodename =
5702 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5703 ivernaux->vna_name);
5704 if (ivernaux->vna_nodename == NULL)
5705 goto error_return;
5706
5707 if (j + 1 < iverneed->vn_cnt)
5708 ivernaux->vna_nextptr = ivernaux + 1;
5709 else
5710 ivernaux->vna_nextptr = NULL;
5711
5712 evernaux = ((Elf_External_Vernaux *)
5713 ((bfd_byte *) evernaux + ivernaux->vna_next));
5714 }
5715
5716 if (i + 1 < hdr->sh_info)
5717 iverneed->vn_nextref = iverneed + 1;
5718 else
5719 iverneed->vn_nextref = NULL;
5720
5721 everneed = ((Elf_External_Verneed *)
5722 ((bfd_byte *) everneed + iverneed->vn_next));
5723 }
5724
5725 free (contents);
5726 contents = NULL;
5727 }
5728
5729 return true;
5730
5731 error_return:
5732 if (contents == NULL)
5733 free (contents);
5734 return false;
5735}
5736\f
5737asymbol *
5738_bfd_elf_make_empty_symbol (abfd)
5739 bfd *abfd;
5740{
5741 elf_symbol_type *newsym;
dc810e39 5742 bfd_size_type amt = sizeof (elf_symbol_type);
252b5132 5743
dc810e39 5744 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
252b5132
RH
5745 if (!newsym)
5746 return NULL;
5747 else
5748 {
5749 newsym->symbol.the_bfd = abfd;
5750 return &newsym->symbol;
5751 }
5752}
5753
5754void
5755_bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
7442e600 5756 bfd *ignore_abfd ATTRIBUTE_UNUSED;
252b5132
RH
5757 asymbol *symbol;
5758 symbol_info *ret;
5759{
5760 bfd_symbol_info (symbol, ret);
5761}
5762
5763/* Return whether a symbol name implies a local symbol. Most targets
5764 use this function for the is_local_label_name entry point, but some
5765 override it. */
5766
5767boolean
5768_bfd_elf_is_local_label_name (abfd, name)
7442e600 5769 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
5770 const char *name;
5771{
5772 /* Normal local symbols start with ``.L''. */
5773 if (name[0] == '.' && name[1] == 'L')
5774 return true;
5775
5776 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5777 DWARF debugging symbols starting with ``..''. */
5778 if (name[0] == '.' && name[1] == '.')
5779 return true;
5780
5781 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5782 emitting DWARF debugging output. I suspect this is actually a
5783 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5784 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5785 underscore to be emitted on some ELF targets). For ease of use,
5786 we treat such symbols as local. */
5787 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5788 return true;
5789
5790 return false;
5791}
5792
5793alent *
5794_bfd_elf_get_lineno (ignore_abfd, symbol)
7442e600
ILT
5795 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5796 asymbol *symbol ATTRIBUTE_UNUSED;
252b5132
RH
5797{
5798 abort ();
5799 return NULL;
5800}
5801
5802boolean
5803_bfd_elf_set_arch_mach (abfd, arch, machine)
5804 bfd *abfd;
5805 enum bfd_architecture arch;
5806 unsigned long machine;
5807{
5808 /* If this isn't the right architecture for this backend, and this
5809 isn't the generic backend, fail. */
5810 if (arch != get_elf_backend_data (abfd)->arch
5811 && arch != bfd_arch_unknown
5812 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5813 return false;
5814
5815 return bfd_default_set_arch_mach (abfd, arch, machine);
5816}
5817
d1fad7c6
NC
5818/* Find the function to a particular section and offset,
5819 for error reporting. */
252b5132 5820
d1fad7c6
NC
5821static boolean
5822elf_find_function (abfd, section, symbols, offset,
4e8a9624 5823 filename_ptr, functionname_ptr)
d1fad7c6 5824 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
5825 asection *section;
5826 asymbol **symbols;
5827 bfd_vma offset;
4e8a9624
AM
5828 const char **filename_ptr;
5829 const char **functionname_ptr;
252b5132 5830{
252b5132
RH
5831 const char *filename;
5832 asymbol *func;
5833 bfd_vma low_func;
5834 asymbol **p;
5835
252b5132
RH
5836 filename = NULL;
5837 func = NULL;
5838 low_func = 0;
5839
5840 for (p = symbols; *p != NULL; p++)
5841 {
5842 elf_symbol_type *q;
5843
5844 q = (elf_symbol_type *) *p;
5845
5846 if (bfd_get_section (&q->symbol) != section)
5847 continue;
5848
5849 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5850 {
5851 default:
5852 break;
5853 case STT_FILE:
5854 filename = bfd_asymbol_name (&q->symbol);
5855 break;
5856 case STT_NOTYPE:
5857 case STT_FUNC:
5858 if (q->symbol.section == section
5859 && q->symbol.value >= low_func
5860 && q->symbol.value <= offset)
5861 {
5862 func = (asymbol *) q;
5863 low_func = q->symbol.value;
5864 }
5865 break;
5866 }
5867 }
5868
5869 if (func == NULL)
5870 return false;
5871
d1fad7c6
NC
5872 if (filename_ptr)
5873 *filename_ptr = filename;
5874 if (functionname_ptr)
5875 *functionname_ptr = bfd_asymbol_name (func);
5876
5877 return true;
5878}
5879
5880/* Find the nearest line to a particular section and offset,
5881 for error reporting. */
5882
5883boolean
5884_bfd_elf_find_nearest_line (abfd, section, symbols, offset,
4e8a9624 5885 filename_ptr, functionname_ptr, line_ptr)
d1fad7c6
NC
5886 bfd *abfd;
5887 asection *section;
5888 asymbol **symbols;
5889 bfd_vma offset;
4e8a9624
AM
5890 const char **filename_ptr;
5891 const char **functionname_ptr;
d1fad7c6
NC
5892 unsigned int *line_ptr;
5893{
5894 boolean found;
5895
5896 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
5897 filename_ptr, functionname_ptr,
5898 line_ptr))
d1fad7c6
NC
5899 {
5900 if (!*functionname_ptr)
4e8a9624
AM
5901 elf_find_function (abfd, section, symbols, offset,
5902 *filename_ptr ? NULL : filename_ptr,
5903 functionname_ptr);
5904
d1fad7c6
NC
5905 return true;
5906 }
5907
5908 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
5909 filename_ptr, functionname_ptr,
5910 line_ptr, 0,
5911 &elf_tdata (abfd)->dwarf2_find_line_info))
d1fad7c6
NC
5912 {
5913 if (!*functionname_ptr)
4e8a9624
AM
5914 elf_find_function (abfd, section, symbols, offset,
5915 *filename_ptr ? NULL : filename_ptr,
5916 functionname_ptr);
5917
d1fad7c6
NC
5918 return true;
5919 }
5920
5921 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
5922 &found, filename_ptr,
5923 functionname_ptr, line_ptr,
5924 &elf_tdata (abfd)->line_info))
d1fad7c6
NC
5925 return false;
5926 if (found)
5927 return true;
5928
5929 if (symbols == NULL)
5930 return false;
5931
5932 if (! elf_find_function (abfd, section, symbols, offset,
4e8a9624 5933 filename_ptr, functionname_ptr))
d1fad7c6
NC
5934 return false;
5935
252b5132
RH
5936 *line_ptr = 0;
5937 return true;
5938}
5939
5940int
5941_bfd_elf_sizeof_headers (abfd, reloc)
5942 bfd *abfd;
5943 boolean reloc;
5944{
5945 int ret;
5946
5947 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5948 if (! reloc)
5949 ret += get_program_header_size (abfd);
5950 return ret;
5951}
5952
5953boolean
5954_bfd_elf_set_section_contents (abfd, section, location, offset, count)
5955 bfd *abfd;
5956 sec_ptr section;
5957 PTR location;
5958 file_ptr offset;
5959 bfd_size_type count;
5960{
5961 Elf_Internal_Shdr *hdr;
dc810e39 5962 bfd_signed_vma pos;
252b5132
RH
5963
5964 if (! abfd->output_has_begun
5965 && ! _bfd_elf_compute_section_file_positions
5966 (abfd, (struct bfd_link_info *) NULL))
5967 return false;
5968
5969 hdr = &elf_section_data (section)->this_hdr;
dc810e39
AM
5970 pos = hdr->sh_offset + offset;
5971 if (bfd_seek (abfd, pos, SEEK_SET) != 0
5972 || bfd_bwrite (location, count, abfd) != count)
252b5132
RH
5973 return false;
5974
5975 return true;
5976}
5977
5978void
5979_bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
7442e600
ILT
5980 bfd *abfd ATTRIBUTE_UNUSED;
5981 arelent *cache_ptr ATTRIBUTE_UNUSED;
5982 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
252b5132
RH
5983{
5984 abort ();
5985}
5986
5987#if 0
5988void
5989_bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5990 bfd *abfd;
5991 arelent *cache_ptr;
5992 Elf_Internal_Rel *dst;
5993{
5994 abort ();
5995}
5996#endif
5997
5998/* Try to convert a non-ELF reloc into an ELF one. */
5999
6000boolean
6001_bfd_elf_validate_reloc (abfd, areloc)
6002 bfd *abfd;
6003 arelent *areloc;
6004{
c044fabd 6005 /* Check whether we really have an ELF howto. */
252b5132
RH
6006
6007 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6008 {
6009 bfd_reloc_code_real_type code;
6010 reloc_howto_type *howto;
6011
6012 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 6013 equivalent ELF reloc. */
252b5132
RH
6014
6015 if (areloc->howto->pc_relative)
6016 {
6017 switch (areloc->howto->bitsize)
6018 {
6019 case 8:
6020 code = BFD_RELOC_8_PCREL;
6021 break;
6022 case 12:
6023 code = BFD_RELOC_12_PCREL;
6024 break;
6025 case 16:
6026 code = BFD_RELOC_16_PCREL;
6027 break;
6028 case 24:
6029 code = BFD_RELOC_24_PCREL;
6030 break;
6031 case 32:
6032 code = BFD_RELOC_32_PCREL;
6033 break;
6034 case 64:
6035 code = BFD_RELOC_64_PCREL;
6036 break;
6037 default:
6038 goto fail;
6039 }
6040
6041 howto = bfd_reloc_type_lookup (abfd, code);
6042
6043 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6044 {
6045 if (howto->pcrel_offset)
6046 areloc->addend += areloc->address;
6047 else
6048 areloc->addend -= areloc->address; /* addend is unsigned!! */
6049 }
6050 }
6051 else
6052 {
6053 switch (areloc->howto->bitsize)
6054 {
6055 case 8:
6056 code = BFD_RELOC_8;
6057 break;
6058 case 14:
6059 code = BFD_RELOC_14;
6060 break;
6061 case 16:
6062 code = BFD_RELOC_16;
6063 break;
6064 case 26:
6065 code = BFD_RELOC_26;
6066 break;
6067 case 32:
6068 code = BFD_RELOC_32;
6069 break;
6070 case 64:
6071 code = BFD_RELOC_64;
6072 break;
6073 default:
6074 goto fail;
6075 }
6076
6077 howto = bfd_reloc_type_lookup (abfd, code);
6078 }
6079
6080 if (howto)
6081 areloc->howto = howto;
6082 else
6083 goto fail;
6084 }
6085
6086 return true;
6087
6088 fail:
6089 (*_bfd_error_handler)
6090 (_("%s: unsupported relocation type %s"),
8f615d07 6091 bfd_archive_filename (abfd), areloc->howto->name);
252b5132
RH
6092 bfd_set_error (bfd_error_bad_value);
6093 return false;
6094}
6095
6096boolean
6097_bfd_elf_close_and_cleanup (abfd)
6098 bfd *abfd;
6099{
6100 if (bfd_get_format (abfd) == bfd_object)
6101 {
6102 if (elf_shstrtab (abfd) != NULL)
2b0f7ef9 6103 _bfd_elf_strtab_free (elf_shstrtab (abfd));
252b5132
RH
6104 }
6105
6106 return _bfd_generic_close_and_cleanup (abfd);
6107}
6108
6109/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6110 in the relocation's offset. Thus we cannot allow any sort of sanity
6111 range-checking to interfere. There is nothing else to do in processing
6112 this reloc. */
6113
6114bfd_reloc_status_type
6115_bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
7442e600
ILT
6116 bfd *abfd ATTRIBUTE_UNUSED;
6117 arelent *re ATTRIBUTE_UNUSED;
6118 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
6119 PTR data ATTRIBUTE_UNUSED;
6120 asection *is ATTRIBUTE_UNUSED;
6121 bfd *obfd ATTRIBUTE_UNUSED;
6122 char **errmsg ATTRIBUTE_UNUSED;
252b5132
RH
6123{
6124 return bfd_reloc_ok;
6125}
252b5132
RH
6126\f
6127/* Elf core file support. Much of this only works on native
6128 toolchains, since we rely on knowing the
6129 machine-dependent procfs structure in order to pick
c044fabd 6130 out details about the corefile. */
252b5132
RH
6131
6132#ifdef HAVE_SYS_PROCFS_H
6133# include <sys/procfs.h>
6134#endif
6135
c044fabd 6136/* FIXME: this is kinda wrong, but it's what gdb wants. */
252b5132
RH
6137
6138static int
6139elfcore_make_pid (abfd)
c044fabd 6140 bfd *abfd;
252b5132
RH
6141{
6142 return ((elf_tdata (abfd)->core_lwpid << 16)
6143 + (elf_tdata (abfd)->core_pid));
6144}
6145
252b5132
RH
6146/* If there isn't a section called NAME, make one, using
6147 data from SECT. Note, this function will generate a
6148 reference to NAME, so you shouldn't deallocate or
c044fabd 6149 overwrite it. */
252b5132
RH
6150
6151static boolean
6152elfcore_maybe_make_sect (abfd, name, sect)
c044fabd
KH
6153 bfd *abfd;
6154 char *name;
6155 asection *sect;
252b5132 6156{
c044fabd 6157 asection *sect2;
252b5132
RH
6158
6159 if (bfd_get_section_by_name (abfd, name) != NULL)
6160 return true;
6161
6162 sect2 = bfd_make_section (abfd, name);
6163 if (sect2 == NULL)
6164 return false;
6165
6166 sect2->_raw_size = sect->_raw_size;
6167 sect2->filepos = sect->filepos;
6168 sect2->flags = sect->flags;
6169 sect2->alignment_power = sect->alignment_power;
6170 return true;
6171}
6172
bb0082d6
AM
6173/* Create a pseudosection containing SIZE bytes at FILEPOS. This
6174 actually creates up to two pseudosections:
6175 - For the single-threaded case, a section named NAME, unless
6176 such a section already exists.
6177 - For the multi-threaded case, a section named "NAME/PID", where
6178 PID is elfcore_make_pid (abfd).
6179 Both pseudosections have identical contents. */
6180boolean
6181_bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
6182 bfd *abfd;
6183 char *name;
dc810e39
AM
6184 size_t size;
6185 ufile_ptr filepos;
bb0082d6
AM
6186{
6187 char buf[100];
6188 char *threaded_name;
6189 asection *sect;
6190
6191 /* Build the section name. */
6192
6193 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
dc810e39 6194 threaded_name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
bb0082d6
AM
6195 if (threaded_name == NULL)
6196 return false;
6197 strcpy (threaded_name, buf);
6198
6199 sect = bfd_make_section (abfd, threaded_name);
6200 if (sect == NULL)
6201 return false;
6202 sect->_raw_size = size;
6203 sect->filepos = filepos;
6204 sect->flags = SEC_HAS_CONTENTS;
6205 sect->alignment_power = 2;
6206
936e320b 6207 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
6208}
6209
252b5132 6210/* prstatus_t exists on:
4a938328 6211 solaris 2.5+
252b5132
RH
6212 linux 2.[01] + glibc
6213 unixware 4.2
6214*/
6215
6216#if defined (HAVE_PRSTATUS_T)
a7b97311
AM
6217static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
6218
252b5132
RH
6219static boolean
6220elfcore_grok_prstatus (abfd, note)
c044fabd
KH
6221 bfd *abfd;
6222 Elf_Internal_Note *note;
252b5132 6223{
dc810e39 6224 size_t raw_size;
7ee38065 6225 int offset;
252b5132 6226
4a938328
MS
6227 if (note->descsz == sizeof (prstatus_t))
6228 {
6229 prstatus_t prstat;
252b5132 6230
e0ebfc61 6231 raw_size = sizeof (prstat.pr_reg);
7ee38065 6232 offset = offsetof (prstatus_t, pr_reg);
4a938328 6233 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 6234
fa49d224
NC
6235 /* Do not overwrite the core signal if it
6236 has already been set by another thread. */
6237 if (elf_tdata (abfd)->core_signal == 0)
6238 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328 6239 elf_tdata (abfd)->core_pid = prstat.pr_pid;
252b5132 6240
4a938328
MS
6241 /* pr_who exists on:
6242 solaris 2.5+
6243 unixware 4.2
6244 pr_who doesn't exist on:
6245 linux 2.[01]
6246 */
252b5132 6247#if defined (HAVE_PRSTATUS_T_PR_WHO)
4a938328 6248 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
252b5132 6249#endif
4a938328 6250 }
7ee38065 6251#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
6252 else if (note->descsz == sizeof (prstatus32_t))
6253 {
6254 /* 64-bit host, 32-bit corefile */
6255 prstatus32_t prstat;
6256
e0ebfc61 6257 raw_size = sizeof (prstat.pr_reg);
7ee38065 6258 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
6259 memcpy (&prstat, note->descdata, sizeof (prstat));
6260
fa49d224
NC
6261 /* Do not overwrite the core signal if it
6262 has already been set by another thread. */
6263 if (elf_tdata (abfd)->core_signal == 0)
6264 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328
MS
6265 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6266
6267 /* pr_who exists on:
6268 solaris 2.5+
6269 unixware 4.2
6270 pr_who doesn't exist on:
6271 linux 2.[01]
6272 */
7ee38065 6273#if defined (HAVE_PRSTATUS32_T_PR_WHO)
4a938328
MS
6274 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6275#endif
6276 }
7ee38065 6277#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
6278 else
6279 {
6280 /* Fail - we don't know how to handle any other
6281 note size (ie. data object type). */
6282 return true;
6283 }
252b5132 6284
bb0082d6 6285 /* Make a ".reg/999" section and a ".reg" section. */
936e320b
AM
6286 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6287 raw_size, note->descpos + offset);
252b5132
RH
6288}
6289#endif /* defined (HAVE_PRSTATUS_T) */
6290
bb0082d6 6291/* Create a pseudosection containing the exact contents of NOTE. */
252b5132 6292static boolean
ff08c6bb 6293elfcore_make_note_pseudosection (abfd, name, note)
c044fabd 6294 bfd *abfd;
ff08c6bb 6295 char *name;
c044fabd 6296 Elf_Internal_Note *note;
252b5132 6297{
936e320b
AM
6298 return _bfd_elfcore_make_pseudosection (abfd, name,
6299 note->descsz, note->descpos);
252b5132
RH
6300}
6301
ff08c6bb
JB
6302/* There isn't a consistent prfpregset_t across platforms,
6303 but it doesn't matter, because we don't have to pick this
c044fabd
KH
6304 data structure apart. */
6305
ff08c6bb
JB
6306static boolean
6307elfcore_grok_prfpreg (abfd, note)
c044fabd
KH
6308 bfd *abfd;
6309 Elf_Internal_Note *note;
ff08c6bb
JB
6310{
6311 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6312}
6313
ff08c6bb
JB
6314/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6315 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6316 literally. */
c044fabd 6317
ff08c6bb
JB
6318static boolean
6319elfcore_grok_prxfpreg (abfd, note)
c044fabd
KH
6320 bfd *abfd;
6321 Elf_Internal_Note *note;
ff08c6bb
JB
6322{
6323 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6324}
6325
252b5132 6326#if defined (HAVE_PRPSINFO_T)
4a938328 6327typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 6328#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
6329typedef prpsinfo32_t elfcore_psinfo32_t;
6330#endif
252b5132
RH
6331#endif
6332
6333#if defined (HAVE_PSINFO_T)
4a938328 6334typedef psinfo_t elfcore_psinfo_t;
7ee38065 6335#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
6336typedef psinfo32_t elfcore_psinfo32_t;
6337#endif
252b5132
RH
6338#endif
6339
252b5132
RH
6340/* return a malloc'ed copy of a string at START which is at
6341 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 6342 the copy will always have a terminating '\0'. */
252b5132 6343
936e320b 6344char *
bb0082d6 6345_bfd_elfcore_strndup (abfd, start, max)
c044fabd
KH
6346 bfd *abfd;
6347 char *start;
dc810e39 6348 size_t max;
252b5132 6349{
dc810e39 6350 char *dups;
c044fabd 6351 char *end = memchr (start, '\0', max);
dc810e39 6352 size_t len;
252b5132
RH
6353
6354 if (end == NULL)
6355 len = max;
6356 else
6357 len = end - start;
6358
dc810e39
AM
6359 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
6360 if (dups == NULL)
252b5132
RH
6361 return NULL;
6362
dc810e39
AM
6363 memcpy (dups, start, len);
6364 dups[len] = '\0';
252b5132 6365
dc810e39 6366 return dups;
252b5132
RH
6367}
6368
bb0082d6 6369#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
a7b97311 6370static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *));
bb0082d6 6371
252b5132
RH
6372static boolean
6373elfcore_grok_psinfo (abfd, note)
c044fabd
KH
6374 bfd *abfd;
6375 Elf_Internal_Note *note;
252b5132 6376{
4a938328
MS
6377 if (note->descsz == sizeof (elfcore_psinfo_t))
6378 {
6379 elfcore_psinfo_t psinfo;
252b5132 6380
7ee38065 6381 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 6382
4a938328 6383 elf_tdata (abfd)->core_program
936e320b
AM
6384 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6385 sizeof (psinfo.pr_fname));
252b5132 6386
4a938328 6387 elf_tdata (abfd)->core_command
936e320b
AM
6388 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6389 sizeof (psinfo.pr_psargs));
4a938328 6390 }
7ee38065 6391#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
6392 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6393 {
6394 /* 64-bit host, 32-bit corefile */
6395 elfcore_psinfo32_t psinfo;
6396
7ee38065 6397 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 6398
4a938328 6399 elf_tdata (abfd)->core_program
936e320b
AM
6400 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6401 sizeof (psinfo.pr_fname));
4a938328
MS
6402
6403 elf_tdata (abfd)->core_command
936e320b
AM
6404 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6405 sizeof (psinfo.pr_psargs));
4a938328
MS
6406 }
6407#endif
6408
6409 else
6410 {
6411 /* Fail - we don't know how to handle any other
6412 note size (ie. data object type). */
6413 return true;
6414 }
252b5132
RH
6415
6416 /* Note that for some reason, a spurious space is tacked
6417 onto the end of the args in some (at least one anyway)
c044fabd 6418 implementations, so strip it off if it exists. */
252b5132
RH
6419
6420 {
c044fabd 6421 char *command = elf_tdata (abfd)->core_command;
252b5132
RH
6422 int n = strlen (command);
6423
6424 if (0 < n && command[n - 1] == ' ')
6425 command[n - 1] = '\0';
6426 }
6427
6428 return true;
6429}
6430#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6431
252b5132 6432#if defined (HAVE_PSTATUS_T)
51316059
MS
6433static boolean elfcore_grok_pstatus PARAMS ((bfd *, Elf_Internal_Note *));
6434
252b5132
RH
6435static boolean
6436elfcore_grok_pstatus (abfd, note)
c044fabd
KH
6437 bfd *abfd;
6438 Elf_Internal_Note *note;
252b5132 6439{
f572a39d
AM
6440 if (note->descsz == sizeof (pstatus_t)
6441#if defined (HAVE_PXSTATUS_T)
6442 || note->descsz == sizeof (pxstatus_t)
6443#endif
6444 )
4a938328
MS
6445 {
6446 pstatus_t pstat;
252b5132 6447
4a938328 6448 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 6449
4a938328
MS
6450 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6451 }
7ee38065 6452#if defined (HAVE_PSTATUS32_T)
4a938328
MS
6453 else if (note->descsz == sizeof (pstatus32_t))
6454 {
6455 /* 64-bit host, 32-bit corefile */
6456 pstatus32_t pstat;
252b5132 6457
4a938328 6458 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 6459
4a938328
MS
6460 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6461 }
6462#endif
252b5132
RH
6463 /* Could grab some more details from the "representative"
6464 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 6465 NT_LWPSTATUS note, presumably. */
252b5132
RH
6466
6467 return true;
6468}
6469#endif /* defined (HAVE_PSTATUS_T) */
6470
252b5132 6471#if defined (HAVE_LWPSTATUS_T)
51316059
MS
6472static boolean elfcore_grok_lwpstatus PARAMS ((bfd *, Elf_Internal_Note *));
6473
252b5132
RH
6474static boolean
6475elfcore_grok_lwpstatus (abfd, note)
c044fabd
KH
6476 bfd *abfd;
6477 Elf_Internal_Note *note;
252b5132
RH
6478{
6479 lwpstatus_t lwpstat;
6480 char buf[100];
c044fabd
KH
6481 char *name;
6482 asection *sect;
252b5132 6483
f572a39d
AM
6484 if (note->descsz != sizeof (lwpstat)
6485#if defined (HAVE_LWPXSTATUS_T)
6486 && note->descsz != sizeof (lwpxstatus_t)
6487#endif
6488 )
252b5132
RH
6489 return true;
6490
6491 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6492
6493 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6494 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6495
c044fabd 6496 /* Make a ".reg/999" section. */
252b5132
RH
6497
6498 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
dc810e39 6499 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
252b5132
RH
6500 if (name == NULL)
6501 return false;
6502 strcpy (name, buf);
6503
6504 sect = bfd_make_section (abfd, name);
6505 if (sect == NULL)
6506 return false;
6507
6508#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6509 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6510 sect->filepos = note->descpos
6511 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6512#endif
6513
6514#if defined (HAVE_LWPSTATUS_T_PR_REG)
6515 sect->_raw_size = sizeof (lwpstat.pr_reg);
6516 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6517#endif
6518
6519 sect->flags = SEC_HAS_CONTENTS;
6520 sect->alignment_power = 2;
6521
6522 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6523 return false;
6524
6525 /* Make a ".reg2/999" section */
6526
6527 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
dc810e39 6528 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
252b5132
RH
6529 if (name == NULL)
6530 return false;
6531 strcpy (name, buf);
6532
6533 sect = bfd_make_section (abfd, name);
6534 if (sect == NULL)
6535 return false;
6536
6537#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6538 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6539 sect->filepos = note->descpos
6540 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6541#endif
6542
6543#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6544 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6545 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6546#endif
6547
6548 sect->flags = SEC_HAS_CONTENTS;
6549 sect->alignment_power = 2;
6550
936e320b 6551 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
6552}
6553#endif /* defined (HAVE_LWPSTATUS_T) */
6554
16e9c715
NC
6555#if defined (HAVE_WIN32_PSTATUS_T)
6556static boolean
6557elfcore_grok_win32pstatus (abfd, note)
c044fabd
KH
6558 bfd *abfd;
6559 Elf_Internal_Note *note;
16e9c715
NC
6560{
6561 char buf[30];
c044fabd
KH
6562 char *name;
6563 asection *sect;
16e9c715
NC
6564 win32_pstatus_t pstatus;
6565
6566 if (note->descsz < sizeof (pstatus))
6567 return true;
6568
e8eab623 6569 memcpy (&pstatus, note->descdata, sizeof (pstatus));
c044fabd
KH
6570
6571 switch (pstatus.data_type)
16e9c715
NC
6572 {
6573 case NOTE_INFO_PROCESS:
6574 /* FIXME: need to add ->core_command. */
6575 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6576 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
c044fabd 6577 break;
16e9c715
NC
6578
6579 case NOTE_INFO_THREAD:
6580 /* Make a ".reg/999" section. */
6581 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
c044fabd 6582
dc810e39 6583 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
16e9c715 6584 if (name == NULL)
c044fabd
KH
6585 return false;
6586
16e9c715
NC
6587 strcpy (name, buf);
6588
6589 sect = bfd_make_section (abfd, name);
6590 if (sect == NULL)
c044fabd
KH
6591 return false;
6592
16e9c715 6593 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
079e9a2f
AM
6594 sect->filepos = (note->descpos
6595 + offsetof (struct win32_pstatus,
6596 data.thread_info.thread_context));
16e9c715
NC
6597 sect->flags = SEC_HAS_CONTENTS;
6598 sect->alignment_power = 2;
6599
6600 if (pstatus.data.thread_info.is_active_thread)
6601 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6602 return false;
6603 break;
6604
6605 case NOTE_INFO_MODULE:
6606 /* Make a ".module/xxxxxxxx" section. */
c044fabd
KH
6607 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6608
dc810e39 6609 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
16e9c715
NC
6610 if (name == NULL)
6611 return false;
c044fabd 6612
16e9c715 6613 strcpy (name, buf);
252b5132 6614
16e9c715 6615 sect = bfd_make_section (abfd, name);
c044fabd 6616
16e9c715
NC
6617 if (sect == NULL)
6618 return false;
c044fabd 6619
16e9c715
NC
6620 sect->_raw_size = note->descsz;
6621 sect->filepos = note->descpos;
6622 sect->flags = SEC_HAS_CONTENTS;
6623 sect->alignment_power = 2;
6624 break;
6625
6626 default:
6627 return true;
6628 }
6629
6630 return true;
6631}
6632#endif /* HAVE_WIN32_PSTATUS_T */
252b5132
RH
6633
6634static boolean
6635elfcore_grok_note (abfd, note)
c044fabd
KH
6636 bfd *abfd;
6637 Elf_Internal_Note *note;
252b5132 6638{
bb0082d6
AM
6639 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6640
252b5132
RH
6641 switch (note->type)
6642 {
6643 default:
6644 return true;
6645
252b5132 6646 case NT_PRSTATUS:
bb0082d6
AM
6647 if (bed->elf_backend_grok_prstatus)
6648 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6649 return true;
6650#if defined (HAVE_PRSTATUS_T)
252b5132 6651 return elfcore_grok_prstatus (abfd, note);
bb0082d6
AM
6652#else
6653 return true;
252b5132
RH
6654#endif
6655
6656#if defined (HAVE_PSTATUS_T)
6657 case NT_PSTATUS:
6658 return elfcore_grok_pstatus (abfd, note);
6659#endif
6660
6661#if defined (HAVE_LWPSTATUS_T)
6662 case NT_LWPSTATUS:
6663 return elfcore_grok_lwpstatus (abfd, note);
6664#endif
6665
6666 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6667 return elfcore_grok_prfpreg (abfd, note);
6668
16e9c715 6669#if defined (HAVE_WIN32_PSTATUS_T)
c044fabd 6670 case NT_WIN32PSTATUS:
16e9c715
NC
6671 return elfcore_grok_win32pstatus (abfd, note);
6672#endif
6673
c044fabd 6674 case NT_PRXFPREG: /* Linux SSE extension */
ff08c6bb
JB
6675 if (note->namesz == 5
6676 && ! strcmp (note->namedata, "LINUX"))
6677 return elfcore_grok_prxfpreg (abfd, note);
6678 else
6679 return true;
6680
252b5132
RH
6681 case NT_PRPSINFO:
6682 case NT_PSINFO:
bb0082d6
AM
6683 if (bed->elf_backend_grok_psinfo)
6684 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6685 return true;
6686#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 6687 return elfcore_grok_psinfo (abfd, note);
bb0082d6
AM
6688#else
6689 return true;
252b5132
RH
6690#endif
6691 }
6692}
6693
50b2bdb7
AM
6694static boolean
6695elfcore_netbsd_get_lwpid (note, lwpidp)
6696 Elf_Internal_Note *note;
6697 int *lwpidp;
6698{
6699 char *cp;
6700
6701 cp = strchr (note->namedata, '@');
6702 if (cp != NULL)
6703 {
d2b64500 6704 *lwpidp = atoi(cp + 1);
50b2bdb7
AM
6705 return true;
6706 }
6707 return false;
6708}
6709
6710static boolean
6711elfcore_grok_netbsd_procinfo (abfd, note)
6712 bfd *abfd;
6713 Elf_Internal_Note *note;
6714{
6715
6716 /* Signal number at offset 0x08. */
6717 elf_tdata (abfd)->core_signal
6718 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6719
6720 /* Process ID at offset 0x50. */
6721 elf_tdata (abfd)->core_pid
6722 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6723
6724 /* Command name at 0x7c (max 32 bytes, including nul). */
6725 elf_tdata (abfd)->core_command
6726 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6727
6728 return true;
6729}
6730
6731static boolean
6732elfcore_grok_netbsd_note (abfd, note)
6733 bfd *abfd;
6734 Elf_Internal_Note *note;
6735{
6736 int lwp;
6737
6738 if (elfcore_netbsd_get_lwpid (note, &lwp))
6739 elf_tdata (abfd)->core_lwpid = lwp;
6740
b4db1224 6741 if (note->type == NT_NETBSDCORE_PROCINFO)
50b2bdb7
AM
6742 {
6743 /* NetBSD-specific core "procinfo". Note that we expect to
6744 find this note before any of the others, which is fine,
6745 since the kernel writes this note out first when it
6746 creates a core file. */
6747
6748 return elfcore_grok_netbsd_procinfo (abfd, note);
6749 }
6750
b4db1224
JT
6751 /* As of Jan 2002 there are no other machine-independent notes
6752 defined for NetBSD core files. If the note type is less
6753 than the start of the machine-dependent note types, we don't
6754 understand it. */
50b2bdb7 6755
b4db1224 6756 if (note->type < NT_NETBSDCORE_FIRSTMACH)
50b2bdb7
AM
6757 return true;
6758
6759
6760 switch (bfd_get_arch (abfd))
6761 {
6762 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6763 PT_GETFPREGS == mach+2. */
6764
6765 case bfd_arch_alpha:
6766 case bfd_arch_sparc:
6767 switch (note->type)
6768 {
b4db1224 6769 case NT_NETBSDCORE_FIRSTMACH+0:
50b2bdb7
AM
6770 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6771
b4db1224 6772 case NT_NETBSDCORE_FIRSTMACH+2:
50b2bdb7
AM
6773 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6774
6775 default:
6776 return true;
6777 }
6778
6779 /* On all other arch's, PT_GETREGS == mach+1 and
6780 PT_GETFPREGS == mach+3. */
6781
6782 default:
6783 switch (note->type)
6784 {
b4db1224 6785 case NT_NETBSDCORE_FIRSTMACH+1:
50b2bdb7
AM
6786 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6787
b4db1224 6788 case NT_NETBSDCORE_FIRSTMACH+3:
50b2bdb7
AM
6789 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6790
6791 default:
6792 return true;
6793 }
6794 }
6795 /* NOTREACHED */
6796}
6797
7c76fa91
MS
6798/* Function: elfcore_write_note
6799
6800 Inputs:
6801 buffer to hold note
6802 name of note
6803 type of note
6804 data for note
6805 size of data for note
6806
6807 Return:
6808 End of buffer containing note. */
6809
6810char *
6811elfcore_write_note (abfd, buf, bufsiz, name, type, input, size)
6812 bfd *abfd;
6813 char *buf;
6814 int *bufsiz;
6815 char *name;
6816 int type;
6817 void *input;
6818 int size;
6819{
6820 Elf_External_Note *xnp;
6821 int namesz = strlen (name);
6822 int newspace = BFD_ALIGN (sizeof (Elf_External_Note) + size + namesz - 1, 4);
6823 char *p, *dest;
6824
6825 p = realloc (buf, *bufsiz + newspace);
6826 dest = p + *bufsiz;
6827 *bufsiz += newspace;
6828 xnp = (Elf_External_Note *) dest;
6829 H_PUT_32 (abfd, namesz, xnp->namesz);
6830 H_PUT_32 (abfd, size, xnp->descsz);
6831 H_PUT_32 (abfd, type, xnp->type);
6832 strcpy (xnp->name, name);
6833 memcpy (xnp->name + BFD_ALIGN (namesz, 4), input, size);
6834 return p;
6835}
6836
6837#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6838char *
6839elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs)
6840 bfd *abfd;
6841 char *buf;
6842 int *bufsiz;
6843 char *fname;
6844 char *psargs;
6845{
6846 int note_type;
6847 char *note_name = "CORE";
6848
6849#if defined (HAVE_PSINFO_T)
6850 psinfo_t data;
6851 note_type = NT_PSINFO;
6852#else
6853 prpsinfo_t data;
6854 note_type = NT_PRPSINFO;
6855#endif
6856
6857 memset (&data, 0, sizeof (data));
6858 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
6859 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
6860 return elfcore_write_note (abfd, buf, bufsiz,
6861 note_name, note_type, &data, sizeof (data));
6862}
6863#endif /* PSINFO_T or PRPSINFO_T */
6864
6865#if defined (HAVE_PRSTATUS_T)
6866char *
6867elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs)
6868 bfd *abfd;
6869 char *buf;
6870 int *bufsiz;
b87011e9 6871 long pid;
7c76fa91
MS
6872 int cursig;
6873 void *gregs;
6874{
6875 prstatus_t prstat;
6876 char *note_name = "CORE";
6877
6878 memset (&prstat, 0, sizeof (prstat));
6879 prstat.pr_pid = pid;
6880 prstat.pr_cursig = cursig;
c106e334 6881 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
7c76fa91
MS
6882 return elfcore_write_note (abfd, buf, bufsiz,
6883 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
6884}
6885#endif /* HAVE_PRSTATUS_T */
6886
51316059
MS
6887#if defined (HAVE_LWPSTATUS_T)
6888char *
6889elfcore_write_lwpstatus (abfd, buf, bufsiz, pid, cursig, gregs)
6890 bfd *abfd;
6891 char *buf;
6892 int *bufsiz;
6893 long pid;
6894 int cursig;
6895 void *gregs;
6896{
6897 lwpstatus_t lwpstat;
6898 char *note_name = "CORE";
6899
6900 memset (&lwpstat, 0, sizeof (lwpstat));
6901 lwpstat.pr_lwpid = pid >> 16;
6902 lwpstat.pr_cursig = cursig;
6903#if defined (HAVE_LWPSTATUS_T_PR_REG)
6904 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
6905#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6906#if !defined(gregs)
6907 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
6908 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
6909#else
6910 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
6911 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
6912#endif
6913#endif
6914 return elfcore_write_note (abfd, buf, bufsiz, note_name,
6915 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
6916}
6917#endif /* HAVE_LWPSTATUS_T */
6918
7c76fa91
MS
6919#if defined (HAVE_PSTATUS_T)
6920char *
6921elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs)
6922 bfd *abfd;
6923 char *buf;
6924 int *bufsiz;
b87011e9 6925 long pid;
7c76fa91
MS
6926 int cursig;
6927 void *gregs;
6928{
6929 pstatus_t pstat;
6930 char *note_name = "CORE";
6931
51316059
MS
6932 memset (&pstat, 0, sizeof (pstat));
6933 pstat.pr_pid = pid & 0xffff;
6934 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
6935 NT_PSTATUS, &pstat, sizeof (pstat));
6936 return buf;
7c76fa91
MS
6937}
6938#endif /* HAVE_PSTATUS_T */
6939
6940char *
6941elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size)
6942 bfd *abfd;
6943 char *buf;
6944 int *bufsiz;
6945 void *fpregs;
6946 int size;
6947{
6948 char *note_name = "CORE";
6949 return elfcore_write_note (abfd, buf, bufsiz,
6950 note_name, NT_FPREGSET, fpregs, size);
6951}
6952
6953char *
6954elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size)
6955 bfd *abfd;
6956 char *buf;
6957 int *bufsiz;
6958 void *xfpregs;
6959 int size;
6960{
6961 char *note_name = "LINUX";
6962 return elfcore_write_note (abfd, buf, bufsiz,
6963 note_name, NT_PRXFPREG, xfpregs, size);
6964}
6965
252b5132
RH
6966static boolean
6967elfcore_read_notes (abfd, offset, size)
c044fabd 6968 bfd *abfd;
dc810e39
AM
6969 file_ptr offset;
6970 bfd_size_type size;
252b5132 6971{
c044fabd
KH
6972 char *buf;
6973 char *p;
252b5132
RH
6974
6975 if (size <= 0)
6976 return true;
6977
dc810e39 6978 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
252b5132
RH
6979 return false;
6980
dc810e39 6981 buf = bfd_malloc (size);
252b5132
RH
6982 if (buf == NULL)
6983 return false;
6984
dc810e39 6985 if (bfd_bread (buf, size, abfd) != size)
252b5132
RH
6986 {
6987 error:
6988 free (buf);
6989 return false;
6990 }
6991
6992 p = buf;
6993 while (p < buf + size)
6994 {
c044fabd
KH
6995 /* FIXME: bad alignment assumption. */
6996 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
6997 Elf_Internal_Note in;
6998
dc810e39 6999 in.type = H_GET_32 (abfd, xnp->type);
252b5132 7000
dc810e39 7001 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132
RH
7002 in.namedata = xnp->name;
7003
dc810e39 7004 in.descsz = H_GET_32 (abfd, xnp->descsz);
252b5132
RH
7005 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7006 in.descpos = offset + (in.descdata - buf);
7007
50b2bdb7
AM
7008 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7009 {
7010 if (! elfcore_grok_netbsd_note (abfd, &in))
7011 goto error;
7012 }
7013 else
7014 {
7015 if (! elfcore_grok_note (abfd, &in))
7016 goto error;
7017 }
252b5132
RH
7018
7019 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7020 }
7021
7022 free (buf);
7023 return true;
7024}
98d8431c
JB
7025\f
7026/* Providing external access to the ELF program header table. */
7027
7028/* Return an upper bound on the number of bytes required to store a
7029 copy of ABFD's program header table entries. Return -1 if an error
7030 occurs; bfd_get_error will return an appropriate code. */
c044fabd 7031
98d8431c
JB
7032long
7033bfd_get_elf_phdr_upper_bound (abfd)
7034 bfd *abfd;
7035{
7036 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7037 {
7038 bfd_set_error (bfd_error_wrong_format);
7039 return -1;
7040 }
7041
936e320b 7042 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
7043}
7044
98d8431c
JB
7045/* Copy ABFD's program header table entries to *PHDRS. The entries
7046 will be stored as an array of Elf_Internal_Phdr structures, as
7047 defined in include/elf/internal.h. To find out how large the
7048 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7049
7050 Return the number of program header table entries read, or -1 if an
7051 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 7052
98d8431c
JB
7053int
7054bfd_get_elf_phdrs (abfd, phdrs)
7055 bfd *abfd;
7056 void *phdrs;
7057{
7058 int num_phdrs;
7059
7060 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7061 {
7062 bfd_set_error (bfd_error_wrong_format);
7063 return -1;
7064 }
7065
7066 num_phdrs = elf_elfheader (abfd)->e_phnum;
c044fabd 7067 memcpy (phdrs, elf_tdata (abfd)->phdr,
98d8431c
JB
7068 num_phdrs * sizeof (Elf_Internal_Phdr));
7069
7070 return num_phdrs;
7071}
ae4221d7
L
7072
7073void
4e771d61 7074_bfd_elf_sprintf_vma (abfd, buf, value)
cc55aec9 7075 bfd *abfd ATTRIBUTE_UNUSED;
ae4221d7
L
7076 char *buf;
7077 bfd_vma value;
7078{
d3b05f8d 7079#ifdef BFD64
ae4221d7
L
7080 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7081
7082 i_ehdrp = elf_elfheader (abfd);
7083 if (i_ehdrp == NULL)
7084 sprintf_vma (buf, value);
7085 else
7086 {
7087 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
cc55aec9 7088 {
ae4221d7 7089#if BFD_HOST_64BIT_LONG
cc55aec9 7090 sprintf (buf, "%016lx", value);
ae4221d7 7091#else
cc55aec9
AM
7092 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7093 _bfd_int64_low (value));
ae4221d7 7094#endif
cc55aec9 7095 }
ae4221d7
L
7096 else
7097 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7098 }
d3b05f8d
L
7099#else
7100 sprintf_vma (buf, value);
7101#endif
ae4221d7
L
7102}
7103
7104void
4e771d61 7105_bfd_elf_fprintf_vma (abfd, stream, value)
cc55aec9 7106 bfd *abfd ATTRIBUTE_UNUSED;
ae4221d7
L
7107 PTR stream;
7108 bfd_vma value;
7109{
d3b05f8d 7110#ifdef BFD64
ae4221d7
L
7111 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7112
7113 i_ehdrp = elf_elfheader (abfd);
7114 if (i_ehdrp == NULL)
7115 fprintf_vma ((FILE *) stream, value);
7116 else
7117 {
7118 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
cc55aec9 7119 {
ae4221d7 7120#if BFD_HOST_64BIT_LONG
cc55aec9 7121 fprintf ((FILE *) stream, "%016lx", value);
ae4221d7 7122#else
cc55aec9
AM
7123 fprintf ((FILE *) stream, "%08lx%08lx",
7124 _bfd_int64_high (value), _bfd_int64_low (value));
ae4221d7 7125#endif
cc55aec9 7126 }
ae4221d7
L
7127 else
7128 fprintf ((FILE *) stream, "%08lx",
7129 (unsigned long) (value & 0xffffffff));
7130 }
d3b05f8d
L
7131#else
7132 fprintf_vma ((FILE *) stream, value);
7133#endif
ae4221d7 7134}
db6751f2
JJ
7135
7136enum elf_reloc_type_class
f51e552e
AM
7137_bfd_elf_reloc_type_class (rela)
7138 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
db6751f2
JJ
7139{
7140 return reloc_class_normal;
7141}
f8df10f4
JJ
7142
7143/* For RELA architectures, return what the relocation value for
7144 relocation against a local symbol. */
7145
7146bfd_vma
7147_bfd_elf_rela_local_sym (abfd, sym, sec, rel)
7148 bfd *abfd;
7149 Elf_Internal_Sym *sym;
7150 asection *sec;
7151 Elf_Internal_Rela *rel;
7152{
7153 bfd_vma relocation;
7154
7155 relocation = (sec->output_section->vma
7156 + sec->output_offset
7157 + sym->st_value);
7158 if ((sec->flags & SEC_MERGE)
c629eae0 7159 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
65765700 7160 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
f8df10f4
JJ
7161 {
7162 asection *msec;
7163
7164 msec = sec;
7165 rel->r_addend =
7166 _bfd_merged_section_offset (abfd, &msec,
65765700 7167 elf_section_data (sec)->sec_info,
f8df10f4
JJ
7168 sym->st_value + rel->r_addend,
7169 (bfd_vma) 0)
7170 - relocation;
7171 rel->r_addend += msec->output_section->vma + msec->output_offset;
7172 }
7173 return relocation;
7174}
c629eae0
JJ
7175
7176bfd_vma
7177_bfd_elf_rel_local_sym (abfd, sym, psec, addend)
7178 bfd *abfd;
7179 Elf_Internal_Sym *sym;
7180 asection **psec;
7181 bfd_vma addend;
7182{
7183 asection *sec = *psec;
7184
65765700 7185 if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_MERGE)
c629eae0
JJ
7186 return sym->st_value + addend;
7187
7188 return _bfd_merged_section_offset (abfd, psec,
65765700 7189 elf_section_data (sec)->sec_info,
c629eae0
JJ
7190 sym->st_value + addend, (bfd_vma) 0);
7191}
7192
7193bfd_vma
7194_bfd_elf_section_offset (abfd, info, sec, offset)
7195 bfd *abfd;
7196 struct bfd_link_info *info;
7197 asection *sec;
7198 bfd_vma offset;
7199{
7200 struct bfd_elf_section_data *sec_data;
7201
7202 sec_data = elf_section_data (sec);
65765700
JJ
7203 switch (sec_data->sec_info_type)
7204 {
7205 case ELF_INFO_TYPE_STABS:
7206 return _bfd_stab_section_offset
7207 (abfd, &elf_hash_table (info)->merge_info, sec, &sec_data->sec_info,
7208 offset);
7209 case ELF_INFO_TYPE_EH_FRAME:
7210 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
7211 default:
7212 return offset;
7213 }
c629eae0 7214}