1 /* ELF executable support for BFD.
2
3 Copyright (C) 1993-2022 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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 3 of the License, or
10 (at your option) any later version.
11
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.
16
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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22
23 /*
24 SECTION
25 ELF backends
26
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
30
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
34
35 /* For sparc64-cross-sparc32. */
36 #define _SYSCALL32
37 #include "sysdep.h"
38 #include <limits.h>
39 #include "bfd.h"
40 #include "bfdlink.h"
41 #include "libbfd.h"
42 #define ARCH_SIZE 0
43 #include "elf-bfd.h"
44 #include "libiberty.h"
45 #include "safe-ctype.h"
46 #include "elf-linux-core.h"
47
48 #ifdef CORE_HEADER
49 #include CORE_HEADER
50 #endif
51
52 static int elf_sort_sections (const void *, const void *);
53 static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
54 static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
55 struct bfd_link_info *);
56 static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
57 file_ptr offset, size_t align);
58
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
62
63 /* Swap in a Verdef structure. */
64
65 void
_bfd_elf_swap_verdef_in(bfd * abfd,const Elf_External_Verdef * src,Elf_Internal_Verdef * dst)66 _bfd_elf_swap_verdef_in (bfd *abfd,
67 const Elf_External_Verdef *src,
68 Elf_Internal_Verdef *dst)
69 {
70 dst->vd_version = H_GET_16 (abfd, src->vd_version);
71 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
72 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
73 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
74 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
75 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
76 dst->vd_next = H_GET_32 (abfd, src->vd_next);
77 }
78
79 /* Swap out a Verdef structure. */
80
81 void
_bfd_elf_swap_verdef_out(bfd * abfd,const Elf_Internal_Verdef * src,Elf_External_Verdef * dst)82 _bfd_elf_swap_verdef_out (bfd *abfd,
83 const Elf_Internal_Verdef *src,
84 Elf_External_Verdef *dst)
85 {
86 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
87 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
88 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
91 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
92 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
93 }
94
95 /* Swap in a Verdaux structure. */
96
97 void
_bfd_elf_swap_verdaux_in(bfd * abfd,const Elf_External_Verdaux * src,Elf_Internal_Verdaux * dst)98 _bfd_elf_swap_verdaux_in (bfd *abfd,
99 const Elf_External_Verdaux *src,
100 Elf_Internal_Verdaux *dst)
101 {
102 dst->vda_name = H_GET_32 (abfd, src->vda_name);
103 dst->vda_next = H_GET_32 (abfd, src->vda_next);
104 }
105
106 /* Swap out a Verdaux structure. */
107
108 void
_bfd_elf_swap_verdaux_out(bfd * abfd,const Elf_Internal_Verdaux * src,Elf_External_Verdaux * dst)109 _bfd_elf_swap_verdaux_out (bfd *abfd,
110 const Elf_Internal_Verdaux *src,
111 Elf_External_Verdaux *dst)
112 {
113 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
115 }
116
117 /* Swap in a Verneed structure. */
118
119 void
_bfd_elf_swap_verneed_in(bfd * abfd,const Elf_External_Verneed * src,Elf_Internal_Verneed * dst)120 _bfd_elf_swap_verneed_in (bfd *abfd,
121 const Elf_External_Verneed *src,
122 Elf_Internal_Verneed *dst)
123 {
124 dst->vn_version = H_GET_16 (abfd, src->vn_version);
125 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
126 dst->vn_file = H_GET_32 (abfd, src->vn_file);
127 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
128 dst->vn_next = H_GET_32 (abfd, src->vn_next);
129 }
130
131 /* Swap out a Verneed structure. */
132
133 void
_bfd_elf_swap_verneed_out(bfd * abfd,const Elf_Internal_Verneed * src,Elf_External_Verneed * dst)134 _bfd_elf_swap_verneed_out (bfd *abfd,
135 const Elf_Internal_Verneed *src,
136 Elf_External_Verneed *dst)
137 {
138 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
139 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
140 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
141 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
142 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
143 }
144
145 /* Swap in a Vernaux structure. */
146
147 void
_bfd_elf_swap_vernaux_in(bfd * abfd,const Elf_External_Vernaux * src,Elf_Internal_Vernaux * dst)148 _bfd_elf_swap_vernaux_in (bfd *abfd,
149 const Elf_External_Vernaux *src,
150 Elf_Internal_Vernaux *dst)
151 {
152 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
153 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154 dst->vna_other = H_GET_16 (abfd, src->vna_other);
155 dst->vna_name = H_GET_32 (abfd, src->vna_name);
156 dst->vna_next = H_GET_32 (abfd, src->vna_next);
157 }
158
159 /* Swap out a Vernaux structure. */
160
161 void
_bfd_elf_swap_vernaux_out(bfd * abfd,const Elf_Internal_Vernaux * src,Elf_External_Vernaux * dst)162 _bfd_elf_swap_vernaux_out (bfd *abfd,
163 const Elf_Internal_Vernaux *src,
164 Elf_External_Vernaux *dst)
165 {
166 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
167 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
168 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
169 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
170 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
171 }
172
173 /* Swap in a Versym structure. */
174
175 void
_bfd_elf_swap_versym_in(bfd * abfd,const Elf_External_Versym * src,Elf_Internal_Versym * dst)176 _bfd_elf_swap_versym_in (bfd *abfd,
177 const Elf_External_Versym *src,
178 Elf_Internal_Versym *dst)
179 {
180 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
181 }
182
183 /* Swap out a Versym structure. */
184
185 void
_bfd_elf_swap_versym_out(bfd * abfd,const Elf_Internal_Versym * src,Elf_External_Versym * dst)186 _bfd_elf_swap_versym_out (bfd *abfd,
187 const Elf_Internal_Versym *src,
188 Elf_External_Versym *dst)
189 {
190 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
191 }
192
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
195
196 unsigned long
bfd_elf_hash(const char * namearg)197 bfd_elf_hash (const char *namearg)
198 {
199 const unsigned char *name = (const unsigned char *) namearg;
200 unsigned long h = 0;
201 unsigned long g;
202 int ch;
203
204 while ((ch = *name++) != '\0')
205 {
206 h = (h << 4) + ch;
207 if ((g = (h & 0xf0000000)) != 0)
208 {
209 h ^= g >> 24;
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
212 h ^= g;
213 }
214 }
215 return h & 0xffffffff;
216 }
217
218 /* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
220
221 unsigned long
bfd_elf_gnu_hash(const char * namearg)222 bfd_elf_gnu_hash (const char *namearg)
223 {
224 const unsigned char *name = (const unsigned char *) namearg;
225 unsigned long h = 5381;
226 unsigned char ch;
227
228 while ((ch = *name++) != '\0')
229 h = (h << 5) + h + ch;
230 return h & 0xffffffff;
231 }
232
233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
235 bool
bfd_elf_allocate_object(bfd * abfd,size_t object_size,enum elf_target_id object_id)236 bfd_elf_allocate_object (bfd *abfd,
237 size_t object_size,
238 enum elf_target_id object_id)
239 {
240 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
241 abfd->tdata.any = bfd_zalloc (abfd, object_size);
242 if (abfd->tdata.any == NULL)
243 return false;
244
245 elf_object_id (abfd) = object_id;
246 if (abfd->direction != read_direction)
247 {
248 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
249 if (o == NULL)
250 return false;
251 elf_tdata (abfd)->o = o;
252 elf_program_header_size (abfd) = (bfd_size_type) -1;
253 }
254 return true;
255 }
256
257
258 bool
bfd_elf_make_object(bfd * abfd)259 bfd_elf_make_object (bfd *abfd)
260 {
261 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
262 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
263 bed->target_id);
264 }
265
266 bool
bfd_elf_mkcorefile(bfd * abfd)267 bfd_elf_mkcorefile (bfd *abfd)
268 {
269 /* I think this can be done just like an object file. */
270 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
271 return false;
272 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
273 return elf_tdata (abfd)->core != NULL;
274 }
275
276 char *
bfd_elf_get_str_section(bfd * abfd,unsigned int shindex)277 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
278 {
279 Elf_Internal_Shdr **i_shdrp;
280 bfd_byte *shstrtab = NULL;
281 file_ptr offset;
282 bfd_size_type shstrtabsize;
283
284 i_shdrp = elf_elfsections (abfd);
285 if (i_shdrp == 0
286 || shindex >= elf_numsections (abfd)
287 || i_shdrp[shindex] == 0)
288 return NULL;
289
290 shstrtab = i_shdrp[shindex]->contents;
291 if (shstrtab == NULL)
292 {
293 /* No cached one, attempt to read, and cache what we read. */
294 offset = i_shdrp[shindex]->sh_offset;
295 shstrtabsize = i_shdrp[shindex]->sh_size;
296
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
299 if (shstrtabsize + 1 <= 1
300 || (bfd_get_file_size (abfd) > 0 /* not a character device */
301 && shstrtabsize > bfd_get_file_size (abfd))
302 || bfd_seek (abfd, offset, SEEK_SET) != 0
303 || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1,
304 shstrtabsize)) == NULL)
305 {
306 /* Once we've failed to read it, make sure we don't keep
307 trying. Otherwise, we'll keep allocating space for
308 the string table over and over. */
309 i_shdrp[shindex]->sh_size = 0;
310 }
311 else
312 shstrtab[shstrtabsize] = '\0';
313 i_shdrp[shindex]->contents = shstrtab;
314 }
315 return (char *) shstrtab;
316 }
317
318 char *
bfd_elf_string_from_elf_section(bfd * abfd,unsigned int shindex,unsigned int strindex)319 bfd_elf_string_from_elf_section (bfd *abfd,
320 unsigned int shindex,
321 unsigned int strindex)
322 {
323 Elf_Internal_Shdr *hdr;
324
325 if (strindex == 0)
326 return "";
327
328 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
329 return NULL;
330
331 hdr = elf_elfsections (abfd)[shindex];
332
333 if (hdr->contents == NULL)
334 {
335 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
336 {
337 /* PR 17512: file: f057ec89. */
338 /* xgettext:c-format */
339 _bfd_error_handler (_("%pB: attempt to load strings from"
340 " a non-string section (number %d)"),
341 abfd, shindex);
342 return NULL;
343 }
344
345 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
346 return NULL;
347 }
348 else
349 {
350 /* PR 24273: The string section's contents may have already
351 been loaded elsewhere, eg because a corrupt file has the
352 string section index in the ELF header pointing at a group
353 section. So be paranoid, and test that the last byte of
354 the section is zero. */
355 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
356 return NULL;
357 }
358
359 if (strindex >= hdr->sh_size)
360 {
361 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
362 _bfd_error_handler
363 /* xgettext:c-format */
364 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
365 abfd, strindex, (uint64_t) hdr->sh_size,
366 (shindex == shstrndx && strindex == hdr->sh_name
367 ? ".shstrtab"
368 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
369 return NULL;
370 }
371
372 return ((char *) hdr->contents) + strindex;
373 }
374
375 /* Read and convert symbols to internal format.
376 SYMCOUNT specifies the number of symbols to read, starting from
377 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
378 are non-NULL, they are used to store the internal symbols, external
379 symbols, and symbol section index extensions, respectively.
380 Returns a pointer to the internal symbol buffer (malloced if necessary)
381 or NULL if there were no symbols or some kind of problem. */
382
383 Elf_Internal_Sym *
bfd_elf_get_elf_syms(bfd * ibfd,Elf_Internal_Shdr * symtab_hdr,size_t symcount,size_t symoffset,Elf_Internal_Sym * intsym_buf,void * extsym_buf,Elf_External_Sym_Shndx * extshndx_buf)384 bfd_elf_get_elf_syms (bfd *ibfd,
385 Elf_Internal_Shdr *symtab_hdr,
386 size_t symcount,
387 size_t symoffset,
388 Elf_Internal_Sym *intsym_buf,
389 void *extsym_buf,
390 Elf_External_Sym_Shndx *extshndx_buf)
391 {
392 Elf_Internal_Shdr *shndx_hdr;
393 void *alloc_ext;
394 const bfd_byte *esym;
395 Elf_External_Sym_Shndx *alloc_extshndx;
396 Elf_External_Sym_Shndx *shndx;
397 Elf_Internal_Sym *alloc_intsym;
398 Elf_Internal_Sym *isym;
399 Elf_Internal_Sym *isymend;
400 const struct elf_backend_data *bed;
401 size_t extsym_size;
402 size_t amt;
403 file_ptr pos;
404
405 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
406 abort ();
407
408 if (symcount == 0)
409 return intsym_buf;
410
411 /* Normal syms might have section extension entries. */
412 shndx_hdr = NULL;
413 if (elf_symtab_shndx_list (ibfd) != NULL)
414 {
415 elf_section_list * entry;
416 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
417
418 /* Find an index section that is linked to this symtab section. */
419 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
420 {
421 /* PR 20063. */
422 if (entry->hdr.sh_link >= elf_numsections (ibfd))
423 continue;
424
425 if (sections[entry->hdr.sh_link] == symtab_hdr)
426 {
427 shndx_hdr = & entry->hdr;
428 break;
429 };
430 }
431
432 if (shndx_hdr == NULL)
433 {
434 if (symtab_hdr == & elf_symtab_hdr (ibfd))
435 /* Not really accurate, but this was how the old code used to work. */
436 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
437 /* Otherwise we do nothing. The assumption is that
438 the index table will not be needed. */
439 }
440 }
441
442 /* Read the symbols. */
443 alloc_ext = NULL;
444 alloc_extshndx = NULL;
445 alloc_intsym = NULL;
446 bed = get_elf_backend_data (ibfd);
447 extsym_size = bed->s->sizeof_sym;
448 if (_bfd_mul_overflow (symcount, extsym_size, &amt))
449 {
450 bfd_set_error (bfd_error_file_too_big);
451 intsym_buf = NULL;
452 goto out;
453 }
454 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
455 if (extsym_buf == NULL)
456 {
457 alloc_ext = bfd_malloc (amt);
458 extsym_buf = alloc_ext;
459 }
460 if (extsym_buf == NULL
461 || bfd_seek (ibfd, pos, SEEK_SET) != 0
462 || bfd_bread (extsym_buf, amt, ibfd) != amt)
463 {
464 intsym_buf = NULL;
465 goto out;
466 }
467
468 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
469 extshndx_buf = NULL;
470 else
471 {
472 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
473 {
474 bfd_set_error (bfd_error_file_too_big);
475 intsym_buf = NULL;
476 goto out;
477 }
478 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
479 if (extshndx_buf == NULL)
480 {
481 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
482 extshndx_buf = alloc_extshndx;
483 }
484 if (extshndx_buf == NULL
485 || bfd_seek (ibfd, pos, SEEK_SET) != 0
486 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
487 {
488 intsym_buf = NULL;
489 goto out;
490 }
491 }
492
493 if (intsym_buf == NULL)
494 {
495 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
496 {
497 bfd_set_error (bfd_error_file_too_big);
498 goto out;
499 }
500 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
501 intsym_buf = alloc_intsym;
502 if (intsym_buf == NULL)
503 goto out;
504 }
505
506 /* Convert the symbols to internal form. */
507 isymend = intsym_buf + symcount;
508 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
509 shndx = extshndx_buf;
510 isym < isymend;
511 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
512 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
513 {
514 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
515 /* xgettext:c-format */
516 _bfd_error_handler (_("%pB symbol number %lu references"
517 " nonexistent SHT_SYMTAB_SHNDX section"),
518 ibfd, (unsigned long) symoffset);
519 free (alloc_intsym);
520 intsym_buf = NULL;
521 goto out;
522 }
523
524 out:
525 free (alloc_ext);
526 free (alloc_extshndx);
527
528 return intsym_buf;
529 }
530
531 /* Look up a symbol name. */
532 const char *
bfd_elf_sym_name(bfd * abfd,Elf_Internal_Shdr * symtab_hdr,Elf_Internal_Sym * isym,asection * sym_sec)533 bfd_elf_sym_name (bfd *abfd,
534 Elf_Internal_Shdr *symtab_hdr,
535 Elf_Internal_Sym *isym,
536 asection *sym_sec)
537 {
538 const char *name;
539 unsigned int iname = isym->st_name;
540 unsigned int shindex = symtab_hdr->sh_link;
541
542 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
543 /* Check for a bogus st_shndx to avoid crashing. */
544 && isym->st_shndx < elf_numsections (abfd))
545 {
546 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
547 shindex = elf_elfheader (abfd)->e_shstrndx;
548 }
549
550 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
551 if (name == NULL)
552 name = "(null)";
553 else if (sym_sec && *name == '\0')
554 name = bfd_section_name (sym_sec);
555
556 return name;
557 }
558
559 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
560 sections. The first element is the flags, the rest are section
561 pointers. */
562
563 typedef union elf_internal_group {
564 Elf_Internal_Shdr *shdr;
565 unsigned int flags;
566 } Elf_Internal_Group;
567
568 /* Return the name of the group signature symbol. Why isn't the
569 signature just a string? */
570
571 static const char *
group_signature(bfd * abfd,Elf_Internal_Shdr * ghdr)572 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
573 {
574 Elf_Internal_Shdr *hdr;
575 unsigned char esym[sizeof (Elf64_External_Sym)];
576 Elf_External_Sym_Shndx eshndx;
577 Elf_Internal_Sym isym;
578
579 /* First we need to ensure the symbol table is available. Make sure
580 that it is a symbol table section. */
581 if (ghdr->sh_link >= elf_numsections (abfd))
582 return NULL;
583 hdr = elf_elfsections (abfd) [ghdr->sh_link];
584 if (hdr->sh_type != SHT_SYMTAB
585 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
586 return NULL;
587
588 /* Go read the symbol. */
589 hdr = &elf_tdata (abfd)->symtab_hdr;
590 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
591 &isym, esym, &eshndx) == NULL)
592 return NULL;
593
594 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
595 }
596
597 /* Set next_in_group list pointer, and group name for NEWSECT. */
598
599 static bool
setup_group(bfd * abfd,Elf_Internal_Shdr * hdr,asection * newsect)600 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
601 {
602 unsigned int num_group = elf_tdata (abfd)->num_group;
603
604 /* If num_group is zero, read in all SHT_GROUP sections. The count
605 is set to -1 if there are no SHT_GROUP sections. */
606 if (num_group == 0)
607 {
608 unsigned int i, shnum;
609
610 /* First count the number of groups. If we have a SHT_GROUP
611 section with just a flag word (ie. sh_size is 4), ignore it. */
612 shnum = elf_numsections (abfd);
613 num_group = 0;
614
615 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
616 ( (shdr)->sh_type == SHT_GROUP \
617 && (shdr)->sh_size >= minsize \
618 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
619 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
620
621 for (i = 0; i < shnum; i++)
622 {
623 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
624
625 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
626 num_group += 1;
627 }
628
629 if (num_group == 0)
630 {
631 num_group = (unsigned) -1;
632 elf_tdata (abfd)->num_group = num_group;
633 elf_tdata (abfd)->group_sect_ptr = NULL;
634 }
635 else
636 {
637 /* We keep a list of elf section headers for group sections,
638 so we can find them quickly. */
639 size_t amt;
640
641 elf_tdata (abfd)->num_group = num_group;
642 amt = num_group * sizeof (Elf_Internal_Shdr *);
643 elf_tdata (abfd)->group_sect_ptr
644 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
645 if (elf_tdata (abfd)->group_sect_ptr == NULL)
646 return false;
647 num_group = 0;
648
649 for (i = 0; i < shnum; i++)
650 {
651 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
652
653 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
654 {
655 unsigned char *src;
656 Elf_Internal_Group *dest;
657
658 /* Make sure the group section has a BFD section
659 attached to it. */
660 if (!bfd_section_from_shdr (abfd, i))
661 return false;
662
663 /* Add to list of sections. */
664 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
665 num_group += 1;
666
667 /* Read the raw contents. */
668 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0);
669 shdr->contents = NULL;
670 if (_bfd_mul_overflow (shdr->sh_size,
671 sizeof (*dest) / 4, &amt)
672 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
673 || !(shdr->contents
674 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size)))
675 {
676 _bfd_error_handler
677 /* xgettext:c-format */
678 (_("%pB: invalid size field in group section"
679 " header: %#" PRIx64 ""),
680 abfd, (uint64_t) shdr->sh_size);
681 bfd_set_error (bfd_error_bad_value);
682 -- num_group;
683 continue;
684 }
685
686 /* Translate raw contents, a flag word followed by an
687 array of elf section indices all in target byte order,
688 to the flag word followed by an array of elf section
689 pointers. */
690 src = shdr->contents + shdr->sh_size;
691 dest = (Elf_Internal_Group *) (shdr->contents + amt);
692
693 while (1)
694 {
695 unsigned int idx;
696
697 src -= 4;
698 --dest;
699 idx = H_GET_32 (abfd, src);
700 if (src == shdr->contents)
701 {
702 dest->shdr = NULL;
703 dest->flags = idx;
704 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
705 shdr->bfd_section->flags
706 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
707 break;
708 }
709 if (idx < shnum)
710 {
711 dest->shdr = elf_elfsections (abfd)[idx];
712 /* PR binutils/23199: All sections in a
713 section group should be marked with
714 SHF_GROUP. But some tools generate
715 broken objects without SHF_GROUP. Fix
716 them up here. */
717 dest->shdr->sh_flags |= SHF_GROUP;
718 }
719 if (idx >= shnum
720 || dest->shdr->sh_type == SHT_GROUP)
721 {
722 _bfd_error_handler
723 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
724 abfd, i);
725 dest->shdr = NULL;
726 }
727 }
728 }
729 }
730
731 /* PR 17510: Corrupt binaries might contain invalid groups. */
732 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
733 {
734 elf_tdata (abfd)->num_group = num_group;
735
736 /* If all groups are invalid then fail. */
737 if (num_group == 0)
738 {
739 elf_tdata (abfd)->group_sect_ptr = NULL;
740 elf_tdata (abfd)->num_group = num_group = -1;
741 _bfd_error_handler
742 (_("%pB: no valid group sections found"), abfd);
743 bfd_set_error (bfd_error_bad_value);
744 }
745 }
746 }
747 }
748
749 if (num_group != (unsigned) -1)
750 {
751 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
752 unsigned int j;
753
754 for (j = 0; j < num_group; j++)
755 {
756 /* Begin search from previous found group. */
757 unsigned i = (j + search_offset) % num_group;
758
759 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
760 Elf_Internal_Group *idx;
761 bfd_size_type n_elt;
762
763 if (shdr == NULL)
764 continue;
765
766 idx = (Elf_Internal_Group *) shdr->contents;
767 if (idx == NULL || shdr->sh_size < 4)
768 {
769 /* See PR 21957 for a reproducer. */
770 /* xgettext:c-format */
771 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
772 abfd, shdr->bfd_section);
773 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
774 bfd_set_error (bfd_error_bad_value);
775 return false;
776 }
777 n_elt = shdr->sh_size / 4;
778
779 /* Look through this group's sections to see if current
780 section is a member. */
781 while (--n_elt != 0)
782 if ((++idx)->shdr == hdr)
783 {
784 asection *s = NULL;
785
786 /* We are a member of this group. Go looking through
787 other members to see if any others are linked via
788 next_in_group. */
789 idx = (Elf_Internal_Group *) shdr->contents;
790 n_elt = shdr->sh_size / 4;
791 while (--n_elt != 0)
792 if ((++idx)->shdr != NULL
793 && (s = idx->shdr->bfd_section) != NULL
794 && elf_next_in_group (s) != NULL)
795 break;
796 if (n_elt != 0)
797 {
798 /* Snarf the group name from other member, and
799 insert current section in circular list. */
800 elf_group_name (newsect) = elf_group_name (s);
801 elf_next_in_group (newsect) = elf_next_in_group (s);
802 elf_next_in_group (s) = newsect;
803 }
804 else
805 {
806 const char *gname;
807
808 gname = group_signature (abfd, shdr);
809 if (gname == NULL)
810 return false;
811 elf_group_name (newsect) = gname;
812
813 /* Start a circular list with one element. */
814 elf_next_in_group (newsect) = newsect;
815 }
816
817 /* If the group section has been created, point to the
818 new member. */
819 if (shdr->bfd_section != NULL)
820 elf_next_in_group (shdr->bfd_section) = newsect;
821
822 elf_tdata (abfd)->group_search_offset = i;
823 j = num_group - 1;
824 break;
825 }
826 }
827 }
828
829 if (elf_group_name (newsect) == NULL)
830 {
831 /* xgettext:c-format */
832 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
833 abfd, newsect);
834 return false;
835 }
836 return true;
837 }
838
839 bool
_bfd_elf_setup_sections(bfd * abfd)840 _bfd_elf_setup_sections (bfd *abfd)
841 {
842 unsigned int i;
843 unsigned int num_group = elf_tdata (abfd)->num_group;
844 bool result = true;
845 asection *s;
846
847 /* Process SHF_LINK_ORDER. */
848 for (s = abfd->sections; s != NULL; s = s->next)
849 {
850 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
851 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
852 {
853 unsigned int elfsec = this_hdr->sh_link;
854 /* An sh_link value of 0 is now allowed. It indicates that linked
855 to section has already been discarded, but that the current
856 section has been retained for some other reason. This linking
857 section is still a candidate for later garbage collection
858 however. */
859 if (elfsec == 0)
860 {
861 elf_linked_to_section (s) = NULL;
862 }
863 else
864 {
865 asection *linksec = NULL;
866
867 if (elfsec < elf_numsections (abfd))
868 {
869 this_hdr = elf_elfsections (abfd)[elfsec];
870 linksec = this_hdr->bfd_section;
871 }
872
873 /* PR 1991, 2008:
874 Some strip/objcopy may leave an incorrect value in
875 sh_link. We don't want to proceed. */
876 if (linksec == NULL)
877 {
878 _bfd_error_handler
879 /* xgettext:c-format */
880 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
881 s->owner, elfsec, s);
882 result = false;
883 }
884
885 elf_linked_to_section (s) = linksec;
886 }
887 }
888 else if (this_hdr->sh_type == SHT_GROUP
889 && elf_next_in_group (s) == NULL)
890 {
891 _bfd_error_handler
892 /* xgettext:c-format */
893 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
894 abfd, elf_section_data (s)->this_idx);
895 result = false;
896 }
897 }
898
899 /* Process section groups. */
900 if (num_group == (unsigned) -1)
901 return result;
902
903 for (i = 0; i < num_group; i++)
904 {
905 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
906 Elf_Internal_Group *idx;
907 unsigned int n_elt;
908
909 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
910 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
911 {
912 _bfd_error_handler
913 /* xgettext:c-format */
914 (_("%pB: section group entry number %u is corrupt"),
915 abfd, i);
916 result = false;
917 continue;
918 }
919
920 idx = (Elf_Internal_Group *) shdr->contents;
921 n_elt = shdr->sh_size / 4;
922
923 while (--n_elt != 0)
924 {
925 ++ idx;
926
927 if (idx->shdr == NULL)
928 continue;
929 else if (idx->shdr->bfd_section)
930 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
931 else if (idx->shdr->sh_type != SHT_RELA
932 && idx->shdr->sh_type != SHT_REL)
933 {
934 /* There are some unknown sections in the group. */
935 _bfd_error_handler
936 /* xgettext:c-format */
937 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
938 abfd,
939 idx->shdr->sh_type,
940 bfd_elf_string_from_elf_section (abfd,
941 (elf_elfheader (abfd)
942 ->e_shstrndx),
943 idx->shdr->sh_name),
944 shdr->bfd_section);
945 result = false;
946 }
947 }
948 }
949
950 return result;
951 }
952
953 bool
bfd_elf_is_group_section(bfd * abfd ATTRIBUTE_UNUSED,const asection * sec)954 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
955 {
956 return elf_next_in_group (sec) != NULL;
957 }
958
959 const char *
bfd_elf_group_name(bfd * abfd ATTRIBUTE_UNUSED,const asection * sec)960 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
961 {
962 if (elf_sec_group (sec) != NULL)
963 return elf_group_name (sec);
964 return NULL;
965 }
966
967 static char *
convert_debug_to_zdebug(bfd * abfd,const char * name)968 convert_debug_to_zdebug (bfd *abfd, const char *name)
969 {
970 unsigned int len = strlen (name);
971 char *new_name = bfd_alloc (abfd, len + 2);
972 if (new_name == NULL)
973 return NULL;
974 new_name[0] = '.';
975 new_name[1] = 'z';
976 memcpy (new_name + 2, name + 1, len);
977 return new_name;
978 }
979
980 static char *
convert_zdebug_to_debug(bfd * abfd,const char * name)981 convert_zdebug_to_debug (bfd *abfd, const char *name)
982 {
983 unsigned int len = strlen (name);
984 char *new_name = bfd_alloc (abfd, len);
985 if (new_name == NULL)
986 return NULL;
987 new_name[0] = '.';
988 memcpy (new_name + 1, name + 2, len - 1);
989 return new_name;
990 }
991
992 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
993
994 struct lto_section
995 {
996 int16_t major_version;
997 int16_t minor_version;
998 unsigned char slim_object;
999
1000 /* Flags is a private field that is not defined publicly. */
1001 uint16_t flags;
1002 };
1003
1004 /* Make a BFD section from an ELF section. We store a pointer to the
1005 BFD section in the bfd_section field of the header. */
1006
1007 bool
_bfd_elf_make_section_from_shdr(bfd * abfd,Elf_Internal_Shdr * hdr,const char * name,int shindex)1008 _bfd_elf_make_section_from_shdr (bfd *abfd,
1009 Elf_Internal_Shdr *hdr,
1010 const char *name,
1011 int shindex)
1012 {
1013 asection *newsect;
1014 flagword flags;
1015 const struct elf_backend_data *bed;
1016 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
1017
1018 if (hdr->bfd_section != NULL)
1019 return true;
1020
1021 newsect = bfd_make_section_anyway (abfd, name);
1022 if (newsect == NULL)
1023 return false;
1024
1025 hdr->bfd_section = newsect;
1026 elf_section_data (newsect)->this_hdr = *hdr;
1027 elf_section_data (newsect)->this_idx = shindex;
1028
1029 /* Always use the real type/flags. */
1030 elf_section_type (newsect) = hdr->sh_type;
1031 elf_section_flags (newsect) = hdr->sh_flags;
1032
1033 newsect->filepos = hdr->sh_offset;
1034
1035 flags = SEC_NO_FLAGS;
1036 if (hdr->sh_type != SHT_NOBITS)
1037 flags |= SEC_HAS_CONTENTS;
1038 if (hdr->sh_type == SHT_GROUP)
1039 flags |= SEC_GROUP;
1040 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1041 {
1042 flags |= SEC_ALLOC;
1043 if (hdr->sh_type != SHT_NOBITS)
1044 flags |= SEC_LOAD;
1045 }
1046 if ((hdr->sh_flags & SHF_WRITE) == 0)
1047 flags |= SEC_READONLY;
1048 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1049 flags |= SEC_CODE;
1050 else if ((flags & SEC_LOAD) != 0)
1051 flags |= SEC_DATA;
1052 if ((hdr->sh_flags & SHF_MERGE) != 0)
1053 {
1054 flags |= SEC_MERGE;
1055 newsect->entsize = hdr->sh_entsize;
1056 }
1057 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1058 flags |= SEC_STRINGS;
1059 if (hdr->sh_flags & SHF_GROUP)
1060 if (!setup_group (abfd, hdr, newsect))
1061 return false;
1062 if ((hdr->sh_flags & SHF_TLS) != 0)
1063 flags |= SEC_THREAD_LOCAL;
1064 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1065 flags |= SEC_EXCLUDE;
1066
1067 switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
1068 {
1069 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1070 but binutils as of 2019-07-23 did not set the EI_OSABI header
1071 byte. */
1072 case ELFOSABI_GNU:
1073 case ELFOSABI_FREEBSD:
1074 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
1075 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
1076 /* Fall through */
1077 case ELFOSABI_NONE:
1078 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
1079 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1080 break;
1081 }
1082
1083 if ((flags & SEC_ALLOC) == 0)
1084 {
1085 /* The debugging sections appear to be recognized only by name,
1086 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1087 if (name [0] == '.')
1088 {
1089 if (startswith (name, ".debug")
1090 || startswith (name, ".gnu.debuglto_.debug_")
1091 || startswith (name, ".gnu.linkonce.wi.")
1092 || startswith (name, ".zdebug"))
1093 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
1094 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
1095 || startswith (name, ".note.gnu"))
1096 {
1097 flags |= SEC_ELF_OCTETS;
1098 opb = 1;
1099 }
1100 else if (startswith (name, ".line")
1101 || startswith (name, ".stab")
1102 || strcmp (name, ".gdb_index") == 0)
1103 flags |= SEC_DEBUGGING;
1104 }
1105 }
1106
1107 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
1108 || !bfd_set_section_size (newsect, hdr->sh_size)
1109 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
1110 & -hdr->sh_addralign)))
1111 return false;
1112
1113 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1114 only link a single copy of the section. This is used to support
1115 g++. g++ will emit each template expansion in its own section.
1116 The symbols will be defined as weak, so that multiple definitions
1117 are permitted. The GNU linker extension is to actually discard
1118 all but one of the sections. */
1119 if (startswith (name, ".gnu.linkonce")
1120 && elf_next_in_group (newsect) == NULL)
1121 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1122
1123 if (!bfd_set_section_flags (newsect, flags))
1124 return false;
1125
1126 bed = get_elf_backend_data (abfd);
1127 if (bed->elf_backend_section_flags)
1128 if (!bed->elf_backend_section_flags (hdr))
1129 return false;
1130
1131 /* We do not parse the PT_NOTE segments as we are interested even in the
1132 separate debug info files which may have the segments offsets corrupted.
1133 PT_NOTEs from the core files are currently not parsed using BFD. */
1134 if (hdr->sh_type == SHT_NOTE)
1135 {
1136 bfd_byte *contents;
1137
1138 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1139 return false;
1140
1141 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1142 hdr->sh_offset, hdr->sh_addralign);
1143 free (contents);
1144 }
1145
1146 if ((newsect->flags & SEC_ALLOC) != 0)
1147 {
1148 Elf_Internal_Phdr *phdr;
1149 unsigned int i, nload;
1150
1151 /* Some ELF linkers produce binaries with all the program header
1152 p_paddr fields zero. If we have such a binary with more than
1153 one PT_LOAD header, then leave the section lma equal to vma
1154 so that we don't create sections with overlapping lma. */
1155 phdr = elf_tdata (abfd)->phdr;
1156 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1157 if (phdr->p_paddr != 0)
1158 break;
1159 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1160 ++nload;
1161 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1162 return true;
1163
1164 phdr = elf_tdata (abfd)->phdr;
1165 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1166 {
1167 if (((phdr->p_type == PT_LOAD
1168 && (hdr->sh_flags & SHF_TLS) == 0)
1169 || phdr->p_type == PT_TLS)
1170 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1171 {
1172 if ((newsect->flags & SEC_LOAD) == 0)
1173 newsect->lma = (phdr->p_paddr
1174 + hdr->sh_addr - phdr->p_vaddr) / opb;
1175 else
1176 /* We used to use the same adjustment for SEC_LOAD
1177 sections, but that doesn't work if the segment
1178 is packed with code from multiple VMAs.
1179 Instead we calculate the section LMA based on
1180 the segment LMA. It is assumed that the
1181 segment will contain sections with contiguous
1182 LMAs, even if the VMAs are not. */
1183 newsect->lma = (phdr->p_paddr
1184 + hdr->sh_offset - phdr->p_offset) / opb;
1185
1186 /* With contiguous segments, we can't tell from file
1187 offsets whether a section with zero size should
1188 be placed at the end of one segment or the
1189 beginning of the next. Decide based on vaddr. */
1190 if (hdr->sh_addr >= phdr->p_vaddr
1191 && (hdr->sh_addr + hdr->sh_size
1192 <= phdr->p_vaddr + phdr->p_memsz))
1193 break;
1194 }
1195 }
1196 }
1197
1198 /* Compress/decompress DWARF debug sections with names: .debug_* and
1199 .zdebug_*, after the section flags is set. */
1200 if ((newsect->flags & SEC_DEBUGGING)
1201 && ((name[1] == 'd' && name[6] == '_')
1202 || (name[1] == 'z' && name[7] == '_')))
1203 {
1204 enum { nothing, compress, decompress } action = nothing;
1205 int compression_header_size;
1206 bfd_size_type uncompressed_size;
1207 unsigned int uncompressed_align_power;
1208 bool compressed
1209 = bfd_is_section_compressed_with_header (abfd, newsect,
1210 &compression_header_size,
1211 &uncompressed_size,
1212 &uncompressed_align_power);
1213 if (compressed)
1214 {
1215 /* Compressed section. Check if we should decompress. */
1216 if ((abfd->flags & BFD_DECOMPRESS))
1217 action = decompress;
1218 }
1219
1220 /* Compress the uncompressed section or convert from/to .zdebug*
1221 section. Check if we should compress. */
1222 if (action == nothing)
1223 {
1224 if (newsect->size != 0
1225 && (abfd->flags & BFD_COMPRESS)
1226 && compression_header_size >= 0
1227 && uncompressed_size > 0
1228 && (!compressed
1229 || ((compression_header_size > 0)
1230 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
1231 action = compress;
1232 else
1233 return true;
1234 }
1235
1236 if (action == compress)
1237 {
1238 if (!bfd_init_section_compress_status (abfd, newsect))
1239 {
1240 _bfd_error_handler
1241 /* xgettext:c-format */
1242 (_("%pB: unable to initialize compress status for section %s"),
1243 abfd, name);
1244 return false;
1245 }
1246 }
1247 else
1248 {
1249 if (!bfd_init_section_decompress_status (abfd, newsect))
1250 {
1251 _bfd_error_handler
1252 /* xgettext:c-format */
1253 (_("%pB: unable to initialize decompress status for section %s"),
1254 abfd, name);
1255 return false;
1256 }
1257 }
1258
1259 if (abfd->is_linker_input)
1260 {
1261 if (name[1] == 'z'
1262 && (action == decompress
1263 || (action == compress
1264 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
1265 {
1266 /* Convert section name from .zdebug_* to .debug_* so
1267 that linker will consider this section as a debug
1268 section. */
1269 char *new_name = convert_zdebug_to_debug (abfd, name);
1270 if (new_name == NULL)
1271 return false;
1272 bfd_rename_section (newsect, new_name);
1273 }
1274 }
1275 else
1276 /* For objdump, don't rename the section. For objcopy, delay
1277 section rename to elf_fake_sections. */
1278 newsect->flags |= SEC_ELF_RENAME;
1279 }
1280
1281 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1282 section. */
1283 if (startswith (name, ".gnu.lto_.lto."))
1284 {
1285 struct lto_section lsection;
1286 if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
1287 sizeof (struct lto_section)))
1288 abfd->lto_slim_object = lsection.slim_object;
1289 }
1290
1291 return true;
1292 }
1293
1294 const char *const bfd_elf_section_type_names[] =
1295 {
1296 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1297 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1298 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1299 };
1300
1301 /* ELF relocs are against symbols. If we are producing relocatable
1302 output, and the reloc is against an external symbol, and nothing
1303 has given us any additional addend, the resulting reloc will also
1304 be against the same symbol. In such a case, we don't want to
1305 change anything about the way the reloc is handled, since it will
1306 all be done at final link time. Rather than put special case code
1307 into bfd_perform_relocation, all the reloc types use this howto
1308 function, or should call this function for relocatable output. */
1309
1310 bfd_reloc_status_type
bfd_elf_generic_reloc(bfd * abfd ATTRIBUTE_UNUSED,arelent * reloc_entry,asymbol * symbol,void * data ATTRIBUTE_UNUSED,asection * input_section,bfd * output_bfd,char ** error_message ATTRIBUTE_UNUSED)1311 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1312 arelent *reloc_entry,
1313 asymbol *symbol,
1314 void *data ATTRIBUTE_UNUSED,
1315 asection *input_section,
1316 bfd *output_bfd,
1317 char **error_message ATTRIBUTE_UNUSED)
1318 {
1319 if (output_bfd != NULL
1320 && (symbol->flags & BSF_SECTION_SYM) == 0
1321 && (! reloc_entry->howto->partial_inplace
1322 || reloc_entry->addend == 0))
1323 {
1324 reloc_entry->address += input_section->output_offset;
1325 return bfd_reloc_ok;
1326 }
1327
1328 /* In some cases the relocation should be treated as output section
1329 relative, as when linking ELF DWARF into PE COFF. Many ELF
1330 targets lack section relative relocations and instead use
1331 ordinary absolute relocations for references between DWARF
1332 sections. That is arguably a bug in those targets but it happens
1333 to work for the usual case of linking to non-loaded ELF debug
1334 sections with VMAs forced to zero. PE COFF on the other hand
1335 doesn't allow a section VMA of zero. */
1336 if (output_bfd == NULL
1337 && !reloc_entry->howto->pc_relative
1338 && (symbol->section->flags & SEC_DEBUGGING) != 0
1339 && (input_section->flags & SEC_DEBUGGING) != 0)
1340 reloc_entry->addend -= symbol->section->output_section->vma;
1341
1342 return bfd_reloc_continue;
1343 }
1344
1345 /* Returns TRUE if section A matches section B.
1346 Names, addresses and links may be different, but everything else
1347 should be the same. */
1348
1349 static bool
section_match(const Elf_Internal_Shdr * a,const Elf_Internal_Shdr * b)1350 section_match (const Elf_Internal_Shdr * a,
1351 const Elf_Internal_Shdr * b)
1352 {
1353 if (a->sh_type != b->sh_type
1354 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1355 || a->sh_addralign != b->sh_addralign
1356 || a->sh_entsize != b->sh_entsize)
1357 return false;
1358 if (a->sh_type == SHT_SYMTAB
1359 || a->sh_type == SHT_STRTAB)
1360 return true;
1361 return a->sh_size == b->sh_size;
1362 }
1363
1364 /* Find a section in OBFD that has the same characteristics
1365 as IHEADER. Return the index of this section or SHN_UNDEF if
1366 none can be found. Check's section HINT first, as this is likely
1367 to be the correct section. */
1368
1369 static unsigned int
find_link(const bfd * obfd,const Elf_Internal_Shdr * iheader,const unsigned int hint)1370 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1371 const unsigned int hint)
1372 {
1373 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1374 unsigned int i;
1375
1376 BFD_ASSERT (iheader != NULL);
1377
1378 /* See PR 20922 for a reproducer of the NULL test. */
1379 if (hint < elf_numsections (obfd)
1380 && oheaders[hint] != NULL
1381 && section_match (oheaders[hint], iheader))
1382 return hint;
1383
1384 for (i = 1; i < elf_numsections (obfd); i++)
1385 {
1386 Elf_Internal_Shdr * oheader = oheaders[i];
1387
1388 if (oheader == NULL)
1389 continue;
1390 if (section_match (oheader, iheader))
1391 /* FIXME: Do we care if there is a potential for
1392 multiple matches ? */
1393 return i;
1394 }
1395
1396 return SHN_UNDEF;
1397 }
1398
1399 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1400 Processor specific section, based upon a matching input section.
1401 Returns TRUE upon success, FALSE otherwise. */
1402
1403 static bool
copy_special_section_fields(const bfd * ibfd,bfd * obfd,const Elf_Internal_Shdr * iheader,Elf_Internal_Shdr * oheader,const unsigned int secnum)1404 copy_special_section_fields (const bfd *ibfd,
1405 bfd *obfd,
1406 const Elf_Internal_Shdr *iheader,
1407 Elf_Internal_Shdr *oheader,
1408 const unsigned int secnum)
1409 {
1410 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1411 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1412 bool changed = false;
1413 unsigned int sh_link;
1414
1415 if (oheader->sh_type == SHT_NOBITS)
1416 {
1417 /* This is a feature for objcopy --only-keep-debug:
1418 When a section's type is changed to NOBITS, we preserve
1419 the sh_link and sh_info fields so that they can be
1420 matched up with the original.
1421
1422 Note: Strictly speaking these assignments are wrong.
1423 The sh_link and sh_info fields should point to the
1424 relevent sections in the output BFD, which may not be in
1425 the same location as they were in the input BFD. But
1426 the whole point of this action is to preserve the
1427 original values of the sh_link and sh_info fields, so
1428 that they can be matched up with the section headers in
1429 the original file. So strictly speaking we may be
1430 creating an invalid ELF file, but it is only for a file
1431 that just contains debug info and only for sections
1432 without any contents. */
1433 if (oheader->sh_link == 0)
1434 oheader->sh_link = iheader->sh_link;
1435 if (oheader->sh_info == 0)
1436 oheader->sh_info = iheader->sh_info;
1437 return true;
1438 }
1439
1440 /* Allow the target a chance to decide how these fields should be set. */
1441 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1442 iheader, oheader))
1443 return true;
1444
1445 /* We have an iheader which might match oheader, and which has non-zero
1446 sh_info and/or sh_link fields. Attempt to follow those links and find
1447 the section in the output bfd which corresponds to the linked section
1448 in the input bfd. */
1449 if (iheader->sh_link != SHN_UNDEF)
1450 {
1451 /* See PR 20931 for a reproducer. */
1452 if (iheader->sh_link >= elf_numsections (ibfd))
1453 {
1454 _bfd_error_handler
1455 /* xgettext:c-format */
1456 (_("%pB: invalid sh_link field (%d) in section number %d"),
1457 ibfd, iheader->sh_link, secnum);
1458 return false;
1459 }
1460
1461 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1462 if (sh_link != SHN_UNDEF)
1463 {
1464 oheader->sh_link = sh_link;
1465 changed = true;
1466 }
1467 else
1468 /* FIXME: Should we install iheader->sh_link
1469 if we could not find a match ? */
1470 _bfd_error_handler
1471 /* xgettext:c-format */
1472 (_("%pB: failed to find link section for section %d"), obfd, secnum);
1473 }
1474
1475 if (iheader->sh_info)
1476 {
1477 /* The sh_info field can hold arbitrary information, but if the
1478 SHF_LINK_INFO flag is set then it should be interpreted as a
1479 section index. */
1480 if (iheader->sh_flags & SHF_INFO_LINK)
1481 {
1482 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1483 iheader->sh_info);
1484 if (sh_link != SHN_UNDEF)
1485 oheader->sh_flags |= SHF_INFO_LINK;
1486 }
1487 else
1488 /* No idea what it means - just copy it. */
1489 sh_link = iheader->sh_info;
1490
1491 if (sh_link != SHN_UNDEF)
1492 {
1493 oheader->sh_info = sh_link;
1494 changed = true;
1495 }
1496 else
1497 _bfd_error_handler
1498 /* xgettext:c-format */
1499 (_("%pB: failed to find info section for section %d"), obfd, secnum);
1500 }
1501
1502 return changed;
1503 }
1504
1505 /* Copy the program header and other data from one object module to
1506 another. */
1507
1508 bool
_bfd_elf_copy_private_bfd_data(bfd * ibfd,bfd * obfd)1509 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1510 {
1511 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1512 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1513 const struct elf_backend_data *bed;
1514 unsigned int i;
1515
1516 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1517 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1518 return true;
1519
1520 if (!elf_flags_init (obfd))
1521 {
1522 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1523 elf_flags_init (obfd) = true;
1524 }
1525
1526 elf_gp (obfd) = elf_gp (ibfd);
1527
1528 /* Also copy the EI_OSABI field. */
1529 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1530 elf_elfheader (ibfd)->e_ident[EI_OSABI];
1531
1532 /* If set, copy the EI_ABIVERSION field. */
1533 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1534 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1535 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1536
1537 /* Copy object attributes. */
1538 _bfd_elf_copy_obj_attributes (ibfd, obfd);
1539
1540 if (iheaders == NULL || oheaders == NULL)
1541 return true;
1542
1543 bed = get_elf_backend_data (obfd);
1544
1545 /* Possibly copy other fields in the section header. */
1546 for (i = 1; i < elf_numsections (obfd); i++)
1547 {
1548 unsigned int j;
1549 Elf_Internal_Shdr * oheader = oheaders[i];
1550
1551 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1552 because of a special case need for generating separate debug info
1553 files. See below for more details. */
1554 if (oheader == NULL
1555 || (oheader->sh_type != SHT_NOBITS
1556 && oheader->sh_type < SHT_LOOS))
1557 continue;
1558
1559 /* Ignore empty sections, and sections whose
1560 fields have already been initialised. */
1561 if (oheader->sh_size == 0
1562 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1563 continue;
1564
1565 /* Scan for the matching section in the input bfd.
1566 First we try for a direct mapping between the input and output sections. */
1567 for (j = 1; j < elf_numsections (ibfd); j++)
1568 {
1569 const Elf_Internal_Shdr * iheader = iheaders[j];
1570
1571 if (iheader == NULL)
1572 continue;
1573
1574 if (oheader->bfd_section != NULL
1575 && iheader->bfd_section != NULL
1576 && iheader->bfd_section->output_section != NULL
1577 && iheader->bfd_section->output_section == oheader->bfd_section)
1578 {
1579 /* We have found a connection from the input section to the
1580 output section. Attempt to copy the header fields. If
1581 this fails then do not try any further sections - there
1582 should only be a one-to-one mapping between input and output. */
1583 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1584 j = elf_numsections (ibfd);
1585 break;
1586 }
1587 }
1588
1589 if (j < elf_numsections (ibfd))
1590 continue;
1591
1592 /* That failed. So try to deduce the corresponding input section.
1593 Unfortunately we cannot compare names as the output string table
1594 is empty, so instead we check size, address and type. */
1595 for (j = 1; j < elf_numsections (ibfd); j++)
1596 {
1597 const Elf_Internal_Shdr * iheader = iheaders[j];
1598
1599 if (iheader == NULL)
1600 continue;
1601
1602 /* Try matching fields in the input section's header.
1603 Since --only-keep-debug turns all non-debug sections into
1604 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1605 input type. */
1606 if ((oheader->sh_type == SHT_NOBITS
1607 || iheader->sh_type == oheader->sh_type)
1608 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1609 == (oheader->sh_flags & ~ SHF_INFO_LINK)
1610 && iheader->sh_addralign == oheader->sh_addralign
1611 && iheader->sh_entsize == oheader->sh_entsize
1612 && iheader->sh_size == oheader->sh_size
1613 && iheader->sh_addr == oheader->sh_addr
1614 && (iheader->sh_info != oheader->sh_info
1615 || iheader->sh_link != oheader->sh_link))
1616 {
1617 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1618 break;
1619 }
1620 }
1621
1622 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1623 {
1624 /* Final attempt. Call the backend copy function
1625 with a NULL input section. */
1626 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1627 NULL, oheader);
1628 }
1629 }
1630
1631 return true;
1632 }
1633
1634 static const char *
get_segment_type(unsigned int p_type)1635 get_segment_type (unsigned int p_type)
1636 {
1637 const char *pt;
1638 switch (p_type)
1639 {
1640 case PT_NULL: pt = "NULL"; break;
1641 case PT_LOAD: pt = "LOAD"; break;
1642 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1643 case PT_INTERP: pt = "INTERP"; break;
1644 case PT_NOTE: pt = "NOTE"; break;
1645 case PT_SHLIB: pt = "SHLIB"; break;
1646 case PT_PHDR: pt = "PHDR"; break;
1647 case PT_TLS: pt = "TLS"; break;
1648 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1649 case PT_GNU_STACK: pt = "STACK"; break;
1650 case PT_GNU_RELRO: pt = "RELRO"; break;
1651 default: pt = NULL; break;
1652 }
1653 return pt;
1654 }
1655
1656 /* Print out the program headers. */
1657
1658 bool
_bfd_elf_print_private_bfd_data(bfd * abfd,void * farg)1659 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1660 {
1661 FILE *f = (FILE *) farg;
1662 Elf_Internal_Phdr *p;
1663 asection *s;
1664 bfd_byte *dynbuf = NULL;
1665
1666 p = elf_tdata (abfd)->phdr;
1667 if (p != NULL)
1668 {
1669 unsigned int i, c;
1670
1671 fprintf (f, _("\nProgram Header:\n"));
1672 c = elf_elfheader (abfd)->e_phnum;
1673 for (i = 0; i < c; i++, p++)
1674 {
1675 const char *pt = get_segment_type (p->p_type);
1676 char buf[20];
1677
1678 if (pt == NULL)
1679 {
1680 sprintf (buf, "0x%lx", p->p_type);
1681 pt = buf;
1682 }
1683 fprintf (f, "%8s off 0x", pt);
1684 bfd_fprintf_vma (abfd, f, p->p_offset);
1685 fprintf (f, " vaddr 0x");
1686 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1687 fprintf (f, " paddr 0x");
1688 bfd_fprintf_vma (abfd, f, p->p_paddr);
1689 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1690 fprintf (f, " filesz 0x");
1691 bfd_fprintf_vma (abfd, f, p->p_filesz);
1692 fprintf (f, " memsz 0x");
1693 bfd_fprintf_vma (abfd, f, p->p_memsz);
1694 fprintf (f, " flags %c%c%c",
1695 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1696 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1697 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1698 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1699 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1700 fprintf (f, "\n");
1701 }
1702 }
1703
1704 s = bfd_get_section_by_name (abfd, ".dynamic");
1705 if (s != NULL)
1706 {
1707 unsigned int elfsec;
1708 unsigned long shlink;
1709 bfd_byte *extdyn, *extdynend;
1710 size_t extdynsize;
1711 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1712
1713 fprintf (f, _("\nDynamic Section:\n"));
1714
1715 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1716 goto error_return;
1717
1718 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1719 if (elfsec == SHN_BAD)
1720 goto error_return;
1721 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1722
1723 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1724 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1725
1726 extdyn = dynbuf;
1727 /* PR 17512: file: 6f427532. */
1728 if (s->size < extdynsize)
1729 goto error_return;
1730 extdynend = extdyn + s->size;
1731 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1732 Fix range check. */
1733 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1734 {
1735 Elf_Internal_Dyn dyn;
1736 const char *name = "";
1737 char ab[20];
1738 bool stringp;
1739 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1740
1741 (*swap_dyn_in) (abfd, extdyn, &dyn);
1742
1743 if (dyn.d_tag == DT_NULL)
1744 break;
1745
1746 stringp = false;
1747 switch (dyn.d_tag)
1748 {
1749 default:
1750 if (bed->elf_backend_get_target_dtag)
1751 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1752
1753 if (!strcmp (name, ""))
1754 {
1755 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
1756 name = ab;
1757 }
1758 break;
1759
1760 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1761 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1762 case DT_PLTGOT: name = "PLTGOT"; break;
1763 case DT_HASH: name = "HASH"; break;
1764 case DT_STRTAB: name = "STRTAB"; break;
1765 case DT_SYMTAB: name = "SYMTAB"; break;
1766 case DT_RELA: name = "RELA"; break;
1767 case DT_RELASZ: name = "RELASZ"; break;
1768 case DT_RELAENT: name = "RELAENT"; break;
1769 case DT_STRSZ: name = "STRSZ"; break;
1770 case DT_SYMENT: name = "SYMENT"; break;
1771 case DT_INIT: name = "INIT"; break;
1772 case DT_FINI: name = "FINI"; break;
1773 case DT_SONAME: name = "SONAME"; stringp = true; break;
1774 case DT_RPATH: name = "RPATH"; stringp = true; break;
1775 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1776 case DT_REL: name = "REL"; break;
1777 case DT_RELSZ: name = "RELSZ"; break;
1778 case DT_RELENT: name = "RELENT"; break;
1779 case DT_RELR: name = "RELR"; break;
1780 case DT_RELRSZ: name = "RELRSZ"; break;
1781 case DT_RELRENT: name = "RELRENT"; break;
1782 case DT_PLTREL: name = "PLTREL"; break;
1783 case DT_DEBUG: name = "DEBUG"; break;
1784 case DT_TEXTREL: name = "TEXTREL"; break;
1785 case DT_JMPREL: name = "JMPREL"; break;
1786 case DT_BIND_NOW: name = "BIND_NOW"; break;
1787 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1788 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1789 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1790 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1791 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1792 case DT_FLAGS: name = "FLAGS"; break;
1793 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1794 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1795 case DT_CHECKSUM: name = "CHECKSUM"; break;
1796 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1797 case DT_MOVEENT: name = "MOVEENT"; break;
1798 case DT_MOVESZ: name = "MOVESZ"; break;
1799 case DT_FEATURE: name = "FEATURE"; break;
1800 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1801 case DT_SYMINSZ: name = "SYMINSZ"; break;
1802 case DT_SYMINENT: name = "SYMINENT"; break;
1803 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1804 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1805 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1806 case DT_PLTPAD: name = "PLTPAD"; break;
1807 case DT_MOVETAB: name = "MOVETAB"; break;
1808 case DT_SYMINFO: name = "SYMINFO"; break;
1809 case DT_RELACOUNT: name = "RELACOUNT"; break;
1810 case DT_RELCOUNT: name = "RELCOUNT"; break;
1811 case DT_FLAGS_1: name = "FLAGS_1"; break;
1812 case DT_VERSYM: name = "VERSYM"; break;
1813 case DT_VERDEF: name = "VERDEF"; break;
1814 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1815 case DT_VERNEED: name = "VERNEED"; break;
1816 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1817 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1818 case DT_USED: name = "USED"; break;
1819 case DT_FILTER: name = "FILTER"; stringp = true; break;
1820 case DT_GNU_HASH: name = "GNU_HASH"; break;
1821 }
1822
1823 fprintf (f, " %-20s ", name);
1824 if (! stringp)
1825 {
1826 fprintf (f, "0x");
1827 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1828 }
1829 else
1830 {
1831 const char *string;
1832 unsigned int tagv = dyn.d_un.d_val;
1833
1834 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1835 if (string == NULL)
1836 goto error_return;
1837 fprintf (f, "%s", string);
1838 }
1839 fprintf (f, "\n");
1840 }
1841
1842 free (dynbuf);
1843 dynbuf = NULL;
1844 }
1845
1846 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1847 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1848 {
1849 if (! _bfd_elf_slurp_version_tables (abfd, false))
1850 return false;
1851 }
1852
1853 if (elf_dynverdef (abfd) != 0)
1854 {
1855 Elf_Internal_Verdef *t;
1856
1857 fprintf (f, _("\nVersion definitions:\n"));
1858 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1859 {
1860 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1861 t->vd_flags, t->vd_hash,
1862 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1863 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1864 {
1865 Elf_Internal_Verdaux *a;
1866
1867 fprintf (f, "\t");
1868 for (a = t->vd_auxptr->vda_nextptr;
1869 a != NULL;
1870 a = a->vda_nextptr)
1871 fprintf (f, "%s ",
1872 a->vda_nodename ? a->vda_nodename : "<corrupt>");
1873 fprintf (f, "\n");
1874 }
1875 }
1876 }
1877
1878 if (elf_dynverref (abfd) != 0)
1879 {
1880 Elf_Internal_Verneed *t;
1881
1882 fprintf (f, _("\nVersion References:\n"));
1883 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1884 {
1885 Elf_Internal_Vernaux *a;
1886
1887 fprintf (f, _(" required from %s:\n"),
1888 t->vn_filename ? t->vn_filename : "<corrupt>");
1889 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1890 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1891 a->vna_flags, a->vna_other,
1892 a->vna_nodename ? a->vna_nodename : "<corrupt>");
1893 }
1894 }
1895
1896 return true;
1897
1898 error_return:
1899 free (dynbuf);
1900 return false;
1901 }
1902
1903 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1904 and return symbol version for symbol version itself. */
1905
1906 const char *
_bfd_elf_get_symbol_version_string(bfd * abfd,asymbol * symbol,bool base_p,bool * hidden)1907 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1908 bool base_p,
1909 bool *hidden)
1910 {
1911 const char *version_string = NULL;
1912 if (elf_dynversym (abfd) != 0
1913 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1914 {
1915 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1916
1917 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1918 vernum &= VERSYM_VERSION;
1919
1920 if (vernum == 0)
1921 version_string = "";
1922 else if (vernum == 1
1923 && (vernum > elf_tdata (abfd)->cverdefs
1924 || (elf_tdata (abfd)->verdef[0].vd_flags
1925 == VER_FLG_BASE)))
1926 version_string = base_p ? "Base" : "";
1927 else if (vernum <= elf_tdata (abfd)->cverdefs)
1928 {
1929 const char *nodename
1930 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1931 version_string = "";
1932 if (base_p
1933 || nodename == NULL
1934 || symbol->name == NULL
1935 || strcmp (symbol->name, nodename) != 0)
1936 version_string = nodename;
1937 }
1938 else
1939 {
1940 Elf_Internal_Verneed *t;
1941
1942 version_string = _("<corrupt>");
1943 for (t = elf_tdata (abfd)->verref;
1944 t != NULL;
1945 t = t->vn_nextref)
1946 {
1947 Elf_Internal_Vernaux *a;
1948
1949 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1950 {
1951 if (a->vna_other == vernum)
1952 {
1953 *hidden = true;
1954 version_string = a->vna_nodename;
1955 break;
1956 }
1957 }
1958 }
1959 }
1960 }
1961 return version_string;
1962 }
1963
1964 /* Display ELF-specific fields of a symbol. */
1965
1966 void
bfd_elf_print_symbol(bfd * abfd,void * filep,asymbol * symbol,bfd_print_symbol_type how)1967 bfd_elf_print_symbol (bfd *abfd,
1968 void *filep,
1969 asymbol *symbol,
1970 bfd_print_symbol_type how)
1971 {
1972 FILE *file = (FILE *) filep;
1973 switch (how)
1974 {
1975 case bfd_print_symbol_name:
1976 fprintf (file, "%s", symbol->name);
1977 break;
1978 case bfd_print_symbol_more:
1979 fprintf (file, "elf ");
1980 bfd_fprintf_vma (abfd, file, symbol->value);
1981 fprintf (file, " %x", symbol->flags);
1982 break;
1983 case bfd_print_symbol_all:
1984 {
1985 const char *section_name;
1986 const char *name = NULL;
1987 const struct elf_backend_data *bed;
1988 unsigned char st_other;
1989 bfd_vma val;
1990 const char *version_string;
1991 bool hidden;
1992
1993 section_name = symbol->section ? symbol->section->name : "(*none*)";
1994
1995 bed = get_elf_backend_data (abfd);
1996 if (bed->elf_backend_print_symbol_all)
1997 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1998
1999 if (name == NULL)
2000 {
2001 name = symbol->name;
2002 bfd_print_symbol_vandf (abfd, file, symbol);
2003 }
2004
2005 fprintf (file, " %s\t", section_name);
2006 /* Print the "other" value for a symbol. For common symbols,
2007 we've already printed the size; now print the alignment.
2008 For other symbols, we have no specified alignment, and
2009 we've printed the address; now print the size. */
2010 if (symbol->section && bfd_is_com_section (symbol->section))
2011 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2012 else
2013 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2014 bfd_fprintf_vma (abfd, file, val);
2015
2016 /* If we have version information, print it. */
2017 version_string = _bfd_elf_get_symbol_version_string (abfd,
2018 symbol,
2019 true,
2020 &hidden);
2021 if (version_string)
2022 {
2023 if (!hidden)
2024 fprintf (file, " %-11s", version_string);
2025 else
2026 {
2027 int i;
2028
2029 fprintf (file, " (%s)", version_string);
2030 for (i = 10 - strlen (version_string); i > 0; --i)
2031 putc (' ', file);
2032 }
2033 }
2034
2035 /* If the st_other field is not zero, print it. */
2036 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2037
2038 switch (st_other)
2039 {
2040 case 0: break;
2041 case STV_INTERNAL: fprintf (file, " .internal"); break;
2042 case STV_HIDDEN: fprintf (file, " .hidden"); break;
2043 case STV_PROTECTED: fprintf (file, " .protected"); break;
2044 default:
2045 /* Some other non-defined flags are also present, so print
2046 everything hex. */
2047 fprintf (file, " 0x%02x", (unsigned int) st_other);
2048 }
2049
2050 fprintf (file, " %s", name);
2051 }
2052 break;
2053 }
2054 }
2055
2056 /* ELF .o/exec file reading */
2057
2058 /* Create a new bfd section from an ELF section header. */
2059
2060 bool
bfd_section_from_shdr(bfd * abfd,unsigned int shindex)2061 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2062 {
2063 Elf_Internal_Shdr *hdr;
2064 Elf_Internal_Ehdr *ehdr;
2065 const struct elf_backend_data *bed;
2066 const char *name;
2067 bool ret = true;
2068
2069 if (shindex >= elf_numsections (abfd))
2070 return false;
2071
2072 /* PR17512: A corrupt ELF binary might contain a loop of sections via
2073 sh_link or sh_info. Detect this here, by refusing to load a
2074 section that we are already in the process of loading. */
2075 if (elf_tdata (abfd)->being_created[shindex])
2076 {
2077 _bfd_error_handler
2078 (_("%pB: warning: loop in section dependencies detected"), abfd);
2079 return false;
2080 }
2081 elf_tdata (abfd)->being_created[shindex] = true;
2082
2083 hdr = elf_elfsections (abfd)[shindex];
2084 ehdr = elf_elfheader (abfd);
2085 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2086 hdr->sh_name);
2087 if (name == NULL)
2088 goto fail;
2089
2090 bed = get_elf_backend_data (abfd);
2091 switch (hdr->sh_type)
2092 {
2093 case SHT_NULL:
2094 /* Inactive section. Throw it away. */
2095 goto success;
2096
2097 case SHT_PROGBITS: /* Normal section with contents. */
2098 case SHT_NOBITS: /* .bss section. */
2099 case SHT_HASH: /* .hash section. */
2100 case SHT_NOTE: /* .note section. */
2101 case SHT_INIT_ARRAY: /* .init_array section. */
2102 case SHT_FINI_ARRAY: /* .fini_array section. */
2103 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
2104 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
2105 case SHT_GNU_HASH: /* .gnu.hash section. */
2106 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2107 goto success;
2108
2109 case SHT_DYNAMIC: /* Dynamic linking information. */
2110 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2111 goto fail;
2112
2113 if (hdr->sh_link > elf_numsections (abfd))
2114 {
2115 /* PR 10478: Accept Solaris binaries with a sh_link
2116 field set to SHN_BEFORE or SHN_AFTER. */
2117 switch (bfd_get_arch (abfd))
2118 {
2119 case bfd_arch_i386:
2120 case bfd_arch_sparc:
2121 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2122 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2123 break;
2124 /* Otherwise fall through. */
2125 default:
2126 goto fail;
2127 }
2128 }
2129 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2130 goto fail;
2131 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2132 {
2133 Elf_Internal_Shdr *dynsymhdr;
2134
2135 /* The shared libraries distributed with hpux11 have a bogus
2136 sh_link field for the ".dynamic" section. Find the
2137 string table for the ".dynsym" section instead. */
2138 if (elf_dynsymtab (abfd) != 0)
2139 {
2140 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2141 hdr->sh_link = dynsymhdr->sh_link;
2142 }
2143 else
2144 {
2145 unsigned int i, num_sec;
2146
2147 num_sec = elf_numsections (abfd);
2148 for (i = 1; i < num_sec; i++)
2149 {
2150 dynsymhdr = elf_elfsections (abfd)[i];
2151 if (dynsymhdr->sh_type == SHT_DYNSYM)
2152 {
2153 hdr->sh_link = dynsymhdr->sh_link;
2154 break;
2155 }
2156 }
2157 }
2158 }
2159 goto success;
2160
2161 case SHT_SYMTAB: /* A symbol table. */
2162 if (elf_onesymtab (abfd) == shindex)
2163 goto success;
2164
2165 if (hdr->sh_entsize != bed->s->sizeof_sym)
2166 goto fail;
2167
2168 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2169 {
2170 if (hdr->sh_size != 0)
2171 goto fail;
2172 /* Some assemblers erroneously set sh_info to one with a
2173 zero sh_size. ld sees this as a global symbol count
2174 of (unsigned) -1. Fix it here. */
2175 hdr->sh_info = 0;
2176 goto success;
2177 }
2178
2179 /* PR 18854: A binary might contain more than one symbol table.
2180 Unusual, but possible. Warn, but continue. */
2181 if (elf_onesymtab (abfd) != 0)
2182 {
2183 _bfd_error_handler
2184 /* xgettext:c-format */
2185 (_("%pB: warning: multiple symbol tables detected"
2186 " - ignoring the table in section %u"),
2187 abfd, shindex);
2188 goto success;
2189 }
2190 elf_onesymtab (abfd) = shindex;
2191 elf_symtab_hdr (abfd) = *hdr;
2192 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2193 abfd->flags |= HAS_SYMS;
2194
2195 /* Sometimes a shared object will map in the symbol table. If
2196 SHF_ALLOC is set, and this is a shared object, then we also
2197 treat this section as a BFD section. We can not base the
2198 decision purely on SHF_ALLOC, because that flag is sometimes
2199 set in a relocatable object file, which would confuse the
2200 linker. */
2201 if ((hdr->sh_flags & SHF_ALLOC) != 0
2202 && (abfd->flags & DYNAMIC) != 0
2203 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2204 shindex))
2205 goto fail;
2206
2207 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2208 can't read symbols without that section loaded as well. It
2209 is most likely specified by the next section header. */
2210 {
2211 elf_section_list * entry;
2212 unsigned int i, num_sec;
2213
2214 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2215 if (entry->hdr.sh_link == shindex)
2216 goto success;
2217
2218 num_sec = elf_numsections (abfd);
2219 for (i = shindex + 1; i < num_sec; i++)
2220 {
2221 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2222
2223 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2224 && hdr2->sh_link == shindex)
2225 break;
2226 }
2227
2228 if (i == num_sec)
2229 for (i = 1; i < shindex; i++)
2230 {
2231 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2232
2233 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2234 && hdr2->sh_link == shindex)
2235 break;
2236 }
2237
2238 if (i != shindex)
2239 ret = bfd_section_from_shdr (abfd, i);
2240 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2241 goto success;
2242 }
2243
2244 case SHT_DYNSYM: /* A dynamic symbol table. */
2245 if (elf_dynsymtab (abfd) == shindex)
2246 goto success;
2247
2248 if (hdr->sh_entsize != bed->s->sizeof_sym)
2249 goto fail;
2250
2251 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2252 {
2253 if (hdr->sh_size != 0)
2254 goto fail;
2255
2256 /* Some linkers erroneously set sh_info to one with a
2257 zero sh_size. ld sees this as a global symbol count
2258 of (unsigned) -1. Fix it here. */
2259 hdr->sh_info = 0;
2260 goto success;
2261 }
2262
2263 /* PR 18854: A binary might contain more than one dynamic symbol table.
2264 Unusual, but possible. Warn, but continue. */
2265 if (elf_dynsymtab (abfd) != 0)
2266 {
2267 _bfd_error_handler
2268 /* xgettext:c-format */
2269 (_("%pB: warning: multiple dynamic symbol tables detected"
2270 " - ignoring the table in section %u"),
2271 abfd, shindex);
2272 goto success;
2273 }
2274 elf_dynsymtab (abfd) = shindex;
2275 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2276 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2277 abfd->flags |= HAS_SYMS;
2278
2279 /* Besides being a symbol table, we also treat this as a regular
2280 section, so that objcopy can handle it. */
2281 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2282 goto success;
2283
2284 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
2285 {
2286 elf_section_list * entry;
2287
2288 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2289 if (entry->ndx == shindex)
2290 goto success;
2291
2292 entry = bfd_alloc (abfd, sizeof (*entry));
2293 if (entry == NULL)
2294 goto fail;
2295 entry->ndx = shindex;
2296 entry->hdr = * hdr;
2297 entry->next = elf_symtab_shndx_list (abfd);
2298 elf_symtab_shndx_list (abfd) = entry;
2299 elf_elfsections (abfd)[shindex] = & entry->hdr;
2300 goto success;
2301 }
2302
2303 case SHT_STRTAB: /* A string table. */
2304 if (hdr->bfd_section != NULL)
2305 goto success;
2306
2307 if (ehdr->e_shstrndx == shindex)
2308 {
2309 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2310 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2311 goto success;
2312 }
2313
2314 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2315 {
2316 symtab_strtab:
2317 elf_tdata (abfd)->strtab_hdr = *hdr;
2318 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2319 goto success;
2320 }
2321
2322 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2323 {
2324 dynsymtab_strtab:
2325 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2326 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2327 elf_elfsections (abfd)[shindex] = hdr;
2328 /* We also treat this as a regular section, so that objcopy
2329 can handle it. */
2330 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2331 shindex);
2332 goto success;
2333 }
2334
2335 /* If the string table isn't one of the above, then treat it as a
2336 regular section. We need to scan all the headers to be sure,
2337 just in case this strtab section appeared before the above. */
2338 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2339 {
2340 unsigned int i, num_sec;
2341
2342 num_sec = elf_numsections (abfd);
2343 for (i = 1; i < num_sec; i++)
2344 {
2345 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2346 if (hdr2->sh_link == shindex)
2347 {
2348 /* Prevent endless recursion on broken objects. */
2349 if (i == shindex)
2350 goto fail;
2351 if (! bfd_section_from_shdr (abfd, i))
2352 goto fail;
2353 if (elf_onesymtab (abfd) == i)
2354 goto symtab_strtab;
2355 if (elf_dynsymtab (abfd) == i)
2356 goto dynsymtab_strtab;
2357 }
2358 }
2359 }
2360 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2361 goto success;
2362
2363 case SHT_REL:
2364 case SHT_RELA:
2365 case SHT_RELR:
2366 /* *These* do a lot of work -- but build no sections! */
2367 {
2368 asection *target_sect;
2369 Elf_Internal_Shdr *hdr2, **p_hdr;
2370 unsigned int num_sec = elf_numsections (abfd);
2371 struct bfd_elf_section_data *esdt;
2372 bfd_size_type size;
2373
2374 if (hdr->sh_type == SHT_REL)
2375 size = bed->s->sizeof_rel;
2376 else if (hdr->sh_type == SHT_RELA)
2377 size = bed->s->sizeof_rela;
2378 else
2379 size = bed->s->arch_size / 8;
2380 if (hdr->sh_entsize != size)
2381 goto fail;
2382
2383 /* Check for a bogus link to avoid crashing. */
2384 if (hdr->sh_link >= num_sec)
2385 {
2386 _bfd_error_handler
2387 /* xgettext:c-format */
2388 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2389 abfd, hdr->sh_link, name, shindex);
2390 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2391 shindex);
2392 goto success;
2393 }
2394
2395 /* Get the symbol table. */
2396 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2397 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2398 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2399 goto fail;
2400
2401 /* If this is an alloc section in an executable or shared
2402 library, or the reloc section does not use the main symbol
2403 table we don't treat it as a reloc section. BFD can't
2404 adequately represent such a section, so at least for now,
2405 we don't try. We just present it as a normal section. We
2406 also can't use it as a reloc section if it points to the
2407 null section, an invalid section, another reloc section, or
2408 its sh_link points to the null section. */
2409 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2410 && (hdr->sh_flags & SHF_ALLOC) != 0)
2411 || hdr->sh_link == SHN_UNDEF
2412 || hdr->sh_link != elf_onesymtab (abfd)
2413 || hdr->sh_info == SHN_UNDEF
2414 || hdr->sh_info >= num_sec
2415 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2416 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2417 {
2418 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2419 shindex);
2420 goto success;
2421 }
2422
2423 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2424 goto fail;
2425
2426 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2427 if (target_sect == NULL)
2428 goto fail;
2429
2430 esdt = elf_section_data (target_sect);
2431 if (hdr->sh_type == SHT_RELA)
2432 p_hdr = &esdt->rela.hdr;
2433 else
2434 p_hdr = &esdt->rel.hdr;
2435
2436 /* PR 17512: file: 0b4f81b7.
2437 Also see PR 24456, for a file which deliberately has two reloc
2438 sections. */
2439 if (*p_hdr != NULL)
2440 {
2441 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2442 {
2443 _bfd_error_handler
2444 /* xgettext:c-format */
2445 (_("%pB: warning: secondary relocation section '%s' "
2446 "for section %pA found - ignoring"),
2447 abfd, name, target_sect);
2448 }
2449 else
2450 esdt->has_secondary_relocs = true;
2451 goto success;
2452 }
2453
2454 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2455 if (hdr2 == NULL)
2456 goto fail;
2457 *hdr2 = *hdr;
2458 *p_hdr = hdr2;
2459 elf_elfsections (abfd)[shindex] = hdr2;
2460 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2461 * bed->s->int_rels_per_ext_rel);
2462 target_sect->flags |= SEC_RELOC;
2463 target_sect->relocation = NULL;
2464 target_sect->rel_filepos = hdr->sh_offset;
2465 /* In the section to which the relocations apply, mark whether
2466 its relocations are of the REL or RELA variety. */
2467 if (hdr->sh_size != 0)
2468 {
2469 if (hdr->sh_type == SHT_RELA)
2470 target_sect->use_rela_p = 1;
2471 }
2472 abfd->flags |= HAS_RELOC;
2473 goto success;
2474 }
2475
2476 case SHT_GNU_verdef:
2477 elf_dynverdef (abfd) = shindex;
2478 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2479 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2480 goto success;
2481
2482 case SHT_GNU_versym:
2483 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2484 goto fail;
2485
2486 elf_dynversym (abfd) = shindex;
2487 elf_tdata (abfd)->dynversym_hdr = *hdr;
2488 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2489 goto success;
2490
2491 case SHT_GNU_verneed:
2492 elf_dynverref (abfd) = shindex;
2493 elf_tdata (abfd)->dynverref_hdr = *hdr;
2494 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2495 goto success;
2496
2497 case SHT_SHLIB:
2498 goto success;
2499
2500 case SHT_GROUP:
2501 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2502 goto fail;
2503
2504 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2505 goto fail;
2506
2507 goto success;
2508
2509 default:
2510 /* Possibly an attributes section. */
2511 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2512 || hdr->sh_type == bed->obj_attrs_section_type)
2513 {
2514 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2515 goto fail;
2516 _bfd_elf_parse_attributes (abfd, hdr);
2517 goto success;
2518 }
2519
2520 /* Check for any processor-specific section types. */
2521 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2522 goto success;
2523
2524 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2525 {
2526 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2527 /* FIXME: How to properly handle allocated section reserved
2528 for applications? */
2529 _bfd_error_handler
2530 /* xgettext:c-format */
2531 (_("%pB: unknown type [%#x] section `%s'"),
2532 abfd, hdr->sh_type, name);
2533 else
2534 {
2535 /* Allow sections reserved for applications. */
2536 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2537 shindex);
2538 goto success;
2539 }
2540 }
2541 else if (hdr->sh_type >= SHT_LOPROC
2542 && hdr->sh_type <= SHT_HIPROC)
2543 /* FIXME: We should handle this section. */
2544 _bfd_error_handler
2545 /* xgettext:c-format */
2546 (_("%pB: unknown type [%#x] section `%s'"),
2547 abfd, hdr->sh_type, name);
2548 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2549 {
2550 /* Unrecognised OS-specific sections. */
2551 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2552 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2553 required to correctly process the section and the file should
2554 be rejected with an error message. */
2555 _bfd_error_handler
2556 /* xgettext:c-format */
2557 (_("%pB: unknown type [%#x] section `%s'"),
2558 abfd, hdr->sh_type, name);
2559 else
2560 {
2561 /* Otherwise it should be processed. */
2562 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2563 goto success;
2564 }
2565 }
2566 else
2567 /* FIXME: We should handle this section. */
2568 _bfd_error_handler
2569 /* xgettext:c-format */
2570 (_("%pB: unknown type [%#x] section `%s'"),
2571 abfd, hdr->sh_type, name);
2572
2573 goto fail;
2574 }
2575
2576 fail:
2577 ret = false;
2578 success:
2579 elf_tdata (abfd)->being_created[shindex] = false;
2580 return ret;
2581 }
2582
2583 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2584
2585 Elf_Internal_Sym *
bfd_sym_from_r_symndx(struct sym_cache * cache,bfd * abfd,unsigned long r_symndx)2586 bfd_sym_from_r_symndx (struct sym_cache *cache,
2587 bfd *abfd,
2588 unsigned long r_symndx)
2589 {
2590 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2591
2592 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2593 {
2594 Elf_Internal_Shdr *symtab_hdr;
2595 unsigned char esym[sizeof (Elf64_External_Sym)];
2596 Elf_External_Sym_Shndx eshndx;
2597
2598 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2599 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2600 &cache->sym[ent], esym, &eshndx) == NULL)
2601 return NULL;
2602
2603 if (cache->abfd != abfd)
2604 {
2605 memset (cache->indx, -1, sizeof (cache->indx));
2606 cache->abfd = abfd;
2607 }
2608 cache->indx[ent] = r_symndx;
2609 }
2610
2611 return &cache->sym[ent];
2612 }
2613
2614 /* Given an ELF section number, retrieve the corresponding BFD
2615 section. */
2616
2617 asection *
bfd_section_from_elf_index(bfd * abfd,unsigned int sec_index)2618 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2619 {
2620 if (sec_index >= elf_numsections (abfd))
2621 return NULL;
2622 return elf_elfsections (abfd)[sec_index]->bfd_section;
2623 }
2624
2625 static const struct bfd_elf_special_section special_sections_b[] =
2626 {
2627 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2628 { NULL, 0, 0, 0, 0 }
2629 };
2630
2631 static const struct bfd_elf_special_section special_sections_c[] =
2632 {
2633 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2634 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 },
2635 { NULL, 0, 0, 0, 0 }
2636 };
2637
2638 static const struct bfd_elf_special_section special_sections_d[] =
2639 {
2640 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2641 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2642 /* There are more DWARF sections than these, but they needn't be added here
2643 unless you have to cope with broken compilers that don't emit section
2644 attributes or you want to help the user writing assembler. */
2645 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2646 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2647 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2648 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
2649 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2650 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2651 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2652 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2653 { NULL, 0, 0, 0, 0 }
2654 };
2655
2656 static const struct bfd_elf_special_section special_sections_f[] =
2657 {
2658 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2659 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2660 { NULL, 0 , 0, 0, 0 }
2661 };
2662
2663 static const struct bfd_elf_special_section special_sections_g[] =
2664 {
2665 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2666 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2667 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2668 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2669 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2670 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
2671 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2672 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
2673 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2674 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2675 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2676 { NULL, 0, 0, 0, 0 }
2677 };
2678
2679 static const struct bfd_elf_special_section special_sections_h[] =
2680 {
2681 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2682 { NULL, 0, 0, 0, 0 }
2683 };
2684
2685 static const struct bfd_elf_special_section special_sections_i[] =
2686 {
2687 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2688 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2689 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2690 { NULL, 0, 0, 0, 0 }
2691 };
2692
2693 static const struct bfd_elf_special_section special_sections_l[] =
2694 {
2695 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2696 { NULL, 0, 0, 0, 0 }
2697 };
2698
2699 static const struct bfd_elf_special_section special_sections_n[] =
2700 {
2701 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2702 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2703 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2704 { NULL, 0, 0, 0, 0 }
2705 };
2706
2707 static const struct bfd_elf_special_section special_sections_p[] =
2708 {
2709 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2710 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2711 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2712 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2713 { NULL, 0, 0, 0, 0 }
2714 };
2715
2716 static const struct bfd_elf_special_section special_sections_r[] =
2717 {
2718 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2719 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2720 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
2721 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2722 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2723 { NULL, 0, 0, 0, 0 }
2724 };
2725
2726 static const struct bfd_elf_special_section special_sections_s[] =
2727 {
2728 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2729 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2730 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
2731 /* See struct bfd_elf_special_section declaration for the semantics of
2732 this special case where .prefix_length != strlen (.prefix). */
2733 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2734 { NULL, 0, 0, 0, 0 }
2735 };
2736
2737 static const struct bfd_elf_special_section special_sections_t[] =
2738 {
2739 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2740 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2741 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2742 { NULL, 0, 0, 0, 0 }
2743 };
2744
2745 static const struct bfd_elf_special_section special_sections_z[] =
2746 {
2747 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2748 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
2749 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2750 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2751 { NULL, 0, 0, 0, 0 }
2752 };
2753
2754 static const struct bfd_elf_special_section * const special_sections[] =
2755 {
2756 special_sections_b, /* 'b' */
2757 special_sections_c, /* 'c' */
2758 special_sections_d, /* 'd' */
2759 NULL, /* 'e' */
2760 special_sections_f, /* 'f' */
2761 special_sections_g, /* 'g' */
2762 special_sections_h, /* 'h' */
2763 special_sections_i, /* 'i' */
2764 NULL, /* 'j' */
2765 NULL, /* 'k' */
2766 special_sections_l, /* 'l' */
2767 NULL, /* 'm' */
2768 special_sections_n, /* 'n' */
2769 NULL, /* 'o' */
2770 special_sections_p, /* 'p' */
2771 NULL, /* 'q' */
2772 special_sections_r, /* 'r' */
2773 special_sections_s, /* 's' */
2774 special_sections_t, /* 't' */
2775 NULL, /* 'u' */
2776 NULL, /* 'v' */
2777 NULL, /* 'w' */
2778 NULL, /* 'x' */
2779 NULL, /* 'y' */
2780 special_sections_z /* 'z' */
2781 };
2782
2783 const struct bfd_elf_special_section *
_bfd_elf_get_special_section(const char * name,const struct bfd_elf_special_section * spec,unsigned int rela)2784 _bfd_elf_get_special_section (const char *name,
2785 const struct bfd_elf_special_section *spec,
2786 unsigned int rela)
2787 {
2788 int i;
2789 int len;
2790
2791 len = strlen (name);
2792
2793 for (i = 0; spec[i].prefix != NULL; i++)
2794 {
2795 int suffix_len;
2796 int prefix_len = spec[i].prefix_length;
2797
2798 if (len < prefix_len)
2799 continue;
2800 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2801 continue;
2802
2803 suffix_len = spec[i].suffix_length;
2804 if (suffix_len <= 0)
2805 {
2806 if (name[prefix_len] != 0)
2807 {
2808 if (suffix_len == 0)
2809 continue;
2810 if (name[prefix_len] != '.'
2811 && (suffix_len == -2
2812 || (rela && spec[i].type == SHT_REL)))
2813 continue;
2814 }
2815 }
2816 else
2817 {
2818 if (len < prefix_len + suffix_len)
2819 continue;
2820 if (memcmp (name + len - suffix_len,
2821 spec[i].prefix + prefix_len,
2822 suffix_len) != 0)
2823 continue;
2824 }
2825 return &spec[i];
2826 }
2827
2828 return NULL;
2829 }
2830
2831 const struct bfd_elf_special_section *
_bfd_elf_get_sec_type_attr(bfd * abfd,asection * sec)2832 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2833 {
2834 int i;
2835 const struct bfd_elf_special_section *spec;
2836 const struct elf_backend_data *bed;
2837
2838 /* See if this is one of the special sections. */
2839 if (sec->name == NULL)
2840 return NULL;
2841
2842 bed = get_elf_backend_data (abfd);
2843 spec = bed->special_sections;
2844 if (spec)
2845 {
2846 spec = _bfd_elf_get_special_section (sec->name,
2847 bed->special_sections,
2848 sec->use_rela_p);
2849 if (spec != NULL)
2850 return spec;
2851 }
2852
2853 if (sec->name[0] != '.')
2854 return NULL;
2855
2856 i = sec->name[1] - 'b';
2857 if (i < 0 || i > 'z' - 'b')
2858 return NULL;
2859
2860 spec = special_sections[i];
2861
2862 if (spec == NULL)
2863 return NULL;
2864
2865 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2866 }
2867
2868 bool
_bfd_elf_new_section_hook(bfd * abfd,asection * sec)2869 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2870 {
2871 struct bfd_elf_section_data *sdata;
2872 const struct elf_backend_data *bed;
2873 const struct bfd_elf_special_section *ssect;
2874
2875 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2876 if (sdata == NULL)
2877 {
2878 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2879 sizeof (*sdata));
2880 if (sdata == NULL)
2881 return false;
2882 sec->used_by_bfd = sdata;
2883 }
2884
2885 /* Indicate whether or not this section should use RELA relocations. */
2886 bed = get_elf_backend_data (abfd);
2887 sec->use_rela_p = bed->default_use_rela_p;
2888
2889 /* Set up ELF section type and flags for newly created sections, if
2890 there is an ABI mandated section. */
2891 ssect = (*bed->get_sec_type_attr) (abfd, sec);
2892 if (ssect != NULL)
2893 {
2894 elf_section_type (sec) = ssect->type;
2895 elf_section_flags (sec) = ssect->attr;
2896 }
2897
2898 return _bfd_generic_new_section_hook (abfd, sec);
2899 }
2900
2901 /* Create a new bfd section from an ELF program header.
2902
2903 Since program segments have no names, we generate a synthetic name
2904 of the form segment<NUM>, where NUM is generally the index in the
2905 program header table. For segments that are split (see below) we
2906 generate the names segment<NUM>a and segment<NUM>b.
2907
2908 Note that some program segments may have a file size that is different than
2909 (less than) the memory size. All this means is that at execution the
2910 system must allocate the amount of memory specified by the memory size,
2911 but only initialize it with the first "file size" bytes read from the
2912 file. This would occur for example, with program segments consisting
2913 of combined data+bss.
2914
2915 To handle the above situation, this routine generates TWO bfd sections
2916 for the single program segment. The first has the length specified by
2917 the file size of the segment, and the second has the length specified
2918 by the difference between the two sizes. In effect, the segment is split
2919 into its initialized and uninitialized parts.
2920
2921 */
2922
2923 bool
_bfd_elf_make_section_from_phdr(bfd * abfd,Elf_Internal_Phdr * hdr,int hdr_index,const char * type_name)2924 _bfd_elf_make_section_from_phdr (bfd *abfd,
2925 Elf_Internal_Phdr *hdr,
2926 int hdr_index,
2927 const char *type_name)
2928 {
2929 asection *newsect;
2930 char *name;
2931 char namebuf[64];
2932 size_t len;
2933 int split;
2934 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
2935
2936 split = ((hdr->p_memsz > 0)
2937 && (hdr->p_filesz > 0)
2938 && (hdr->p_memsz > hdr->p_filesz));
2939
2940 if (hdr->p_filesz > 0)
2941 {
2942 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2943 len = strlen (namebuf) + 1;
2944 name = (char *) bfd_alloc (abfd, len);
2945 if (!name)
2946 return false;
2947 memcpy (name, namebuf, len);
2948 newsect = bfd_make_section (abfd, name);
2949 if (newsect == NULL)
2950 return false;
2951 newsect->vma = hdr->p_vaddr / opb;
2952 newsect->lma = hdr->p_paddr / opb;
2953 newsect->size = hdr->p_filesz;
2954 newsect->filepos = hdr->p_offset;
2955 newsect->flags |= SEC_HAS_CONTENTS;
2956 newsect->alignment_power = bfd_log2 (hdr->p_align);
2957 if (hdr->p_type == PT_LOAD)
2958 {
2959 newsect->flags |= SEC_ALLOC;
2960 newsect->flags |= SEC_LOAD;
2961 if (hdr->p_flags & PF_X)
2962 {
2963 /* FIXME: all we known is that it has execute PERMISSION,
2964 may be data. */
2965 newsect->flags |= SEC_CODE;
2966 }
2967 }
2968 if (!(hdr->p_flags & PF_W))
2969 {
2970 newsect->flags |= SEC_READONLY;
2971 }
2972 }
2973
2974 if (hdr->p_memsz > hdr->p_filesz)
2975 {
2976 bfd_vma align;
2977
2978 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2979 len = strlen (namebuf) + 1;
2980 name = (char *) bfd_alloc (abfd, len);
2981 if (!name)
2982 return false;
2983 memcpy (name, namebuf, len);
2984 newsect = bfd_make_section (abfd, name);
2985 if (newsect == NULL)
2986 return false;
2987 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
2988 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
2989 newsect->size = hdr->p_memsz - hdr->p_filesz;
2990 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2991 align = newsect->vma & -newsect->vma;
2992 if (align == 0 || align > hdr->p_align)
2993 align = hdr->p_align;
2994 newsect->alignment_power = bfd_log2 (align);
2995 if (hdr->p_type == PT_LOAD)
2996 {
2997 newsect->flags |= SEC_ALLOC;
2998 if (hdr->p_flags & PF_X)
2999 newsect->flags |= SEC_CODE;
3000 }
3001 if (!(hdr->p_flags & PF_W))
3002 newsect->flags |= SEC_READONLY;
3003 }
3004
3005 return true;
3006 }
3007
3008 static bool
_bfd_elf_core_find_build_id(bfd * templ,bfd_vma offset)3009 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3010 {
3011 /* The return value is ignored. Build-ids are considered optional. */
3012 if (templ->xvec->flavour == bfd_target_elf_flavour)
3013 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3014 (templ, offset);
3015 return false;
3016 }
3017
3018 bool
bfd_section_from_phdr(bfd * abfd,Elf_Internal_Phdr * hdr,int hdr_index)3019 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3020 {
3021 const struct elf_backend_data *bed;
3022
3023 switch (hdr->p_type)
3024 {
3025 case PT_NULL:
3026 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3027
3028 case PT_LOAD:
3029 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3030 return false;
3031 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3032 _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3033 return true;
3034
3035 case PT_DYNAMIC:
3036 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3037
3038 case PT_INTERP:
3039 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3040
3041 case PT_NOTE:
3042 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3043 return false;
3044 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3045 hdr->p_align))
3046 return false;
3047 return true;
3048
3049 case PT_SHLIB:
3050 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3051
3052 case PT_PHDR:
3053 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3054
3055 case PT_GNU_EH_FRAME:
3056 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3057 "eh_frame_hdr");
3058
3059 case PT_GNU_STACK:
3060 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3061
3062 case PT_GNU_RELRO:
3063 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3064
3065 default:
3066 /* Check for any processor-specific program segment types. */
3067 bed = get_elf_backend_data (abfd);
3068 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3069 }
3070 }
3071
3072 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3073 REL or RELA. */
3074
3075 Elf_Internal_Shdr *
_bfd_elf_single_rel_hdr(asection * sec)3076 _bfd_elf_single_rel_hdr (asection *sec)
3077 {
3078 if (elf_section_data (sec)->rel.hdr)
3079 {
3080 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3081 return elf_section_data (sec)->rel.hdr;
3082 }
3083 else
3084 return elf_section_data (sec)->rela.hdr;
3085 }
3086
3087 static bool
_bfd_elf_set_reloc_sh_name(bfd * abfd,Elf_Internal_Shdr * rel_hdr,const char * sec_name,bool use_rela_p)3088 _bfd_elf_set_reloc_sh_name (bfd *abfd,
3089 Elf_Internal_Shdr *rel_hdr,
3090 const char *sec_name,
3091 bool use_rela_p)
3092 {
3093 char *name = (char *) bfd_alloc (abfd,
3094 sizeof ".rela" + strlen (sec_name));
3095 if (name == NULL)
3096 return false;
3097
3098 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3099 rel_hdr->sh_name =
3100 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3101 false);
3102 if (rel_hdr->sh_name == (unsigned int) -1)
3103 return false;
3104
3105 return true;
3106 }
3107
3108 /* Allocate and initialize a section-header for a new reloc section,
3109 containing relocations against ASECT. It is stored in RELDATA. If
3110 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3111 relocations. */
3112
3113 static bool
_bfd_elf_init_reloc_shdr(bfd * abfd,struct bfd_elf_section_reloc_data * reldata,const char * sec_name,bool use_rela_p,bool delay_st_name_p)3114 _bfd_elf_init_reloc_shdr (bfd *abfd,
3115 struct bfd_elf_section_reloc_data *reldata,
3116 const char *sec_name,
3117 bool use_rela_p,
3118 bool delay_st_name_p)
3119 {
3120 Elf_Internal_Shdr *rel_hdr;
3121 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3122
3123 BFD_ASSERT (reldata->hdr == NULL);
3124 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3125 reldata->hdr = rel_hdr;
3126
3127 if (delay_st_name_p)
3128 rel_hdr->sh_name = (unsigned int) -1;
3129 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3130 use_rela_p))
3131 return false;
3132 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3133 rel_hdr->sh_entsize = (use_rela_p
3134 ? bed->s->sizeof_rela
3135 : bed->s->sizeof_rel);
3136 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3137 rel_hdr->sh_flags = 0;
3138 rel_hdr->sh_addr = 0;
3139 rel_hdr->sh_size = 0;
3140 rel_hdr->sh_offset = 0;
3141
3142 return true;
3143 }
3144
3145 /* Return the default section type based on the passed in section flags. */
3146
3147 int
bfd_elf_get_default_section_type(flagword flags)3148 bfd_elf_get_default_section_type (flagword flags)
3149 {
3150 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3151 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3152 return SHT_NOBITS;
3153 return SHT_PROGBITS;
3154 }
3155
3156 struct fake_section_arg
3157 {
3158 struct bfd_link_info *link_info;
3159 bool failed;
3160 };
3161
3162 /* Set up an ELF internal section header for a section. */
3163
3164 static void
elf_fake_sections(bfd * abfd,asection * asect,void * fsarg)3165 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3166 {
3167 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3168 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3169 struct bfd_elf_section_data *esd = elf_section_data (asect);
3170 Elf_Internal_Shdr *this_hdr;
3171 unsigned int sh_type;
3172 const char *name = asect->name;
3173 bool delay_st_name_p = false;
3174 bfd_vma mask;
3175
3176 if (arg->failed)
3177 {
3178 /* We already failed; just get out of the bfd_map_over_sections
3179 loop. */
3180 return;
3181 }
3182
3183 this_hdr = &esd->this_hdr;
3184
3185 if (arg->link_info)
3186 {
3187 /* ld: compress DWARF debug sections with names: .debug_*. */
3188 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3189 && (asect->flags & SEC_DEBUGGING)
3190 && name[1] == 'd'
3191 && name[6] == '_')
3192 {
3193 /* Set SEC_ELF_COMPRESS to indicate this section should be
3194 compressed. */
3195 asect->flags |= SEC_ELF_COMPRESS;
3196 /* If this section will be compressed, delay adding section
3197 name to section name section after it is compressed in
3198 _bfd_elf_assign_file_positions_for_non_load. */
3199 delay_st_name_p = true;
3200 }
3201 }
3202 else if ((asect->flags & SEC_ELF_RENAME))
3203 {
3204 /* objcopy: rename output DWARF debug section. */
3205 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3206 {
3207 /* When we decompress or compress with SHF_COMPRESSED,
3208 convert section name from .zdebug_* to .debug_* if
3209 needed. */
3210 if (name[1] == 'z')
3211 {
3212 char *new_name = convert_zdebug_to_debug (abfd, name);
3213 if (new_name == NULL)
3214 {
3215 arg->failed = true;
3216 return;
3217 }
3218 name = new_name;
3219 }
3220 }
3221 else if (asect->compress_status == COMPRESS_SECTION_DONE)
3222 {
3223 /* PR binutils/18087: Compression does not always make a
3224 section smaller. So only rename the section when
3225 compression has actually taken place. If input section
3226 name is .zdebug_*, we should never compress it again. */
3227 char *new_name = convert_debug_to_zdebug (abfd, name);
3228 if (new_name == NULL)
3229 {
3230 arg->failed = true;
3231 return;
3232 }
3233 BFD_ASSERT (name[1] != 'z');
3234 name = new_name;
3235 }
3236 }
3237
3238 if (delay_st_name_p)
3239 this_hdr->sh_name = (unsigned int) -1;
3240 else
3241 {
3242 this_hdr->sh_name
3243 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3244 name, false);
3245 if (this_hdr->sh_name == (unsigned int) -1)
3246 {
3247 arg->failed = true;
3248 return;
3249 }
3250 }
3251
3252 /* Don't clear sh_flags. Assembler may set additional bits. */
3253
3254 if ((asect->flags & SEC_ALLOC) != 0
3255 || asect->user_set_vma)
3256 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3257 else
3258 this_hdr->sh_addr = 0;
3259
3260 this_hdr->sh_offset = 0;
3261 this_hdr->sh_size = asect->size;
3262 this_hdr->sh_link = 0;
3263 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3264 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3265 {
3266 _bfd_error_handler
3267 /* xgettext:c-format */
3268 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3269 abfd, asect->alignment_power, asect);
3270 arg->failed = true;
3271 return;
3272 }
3273 /* Set sh_addralign to the highest power of two given by alignment
3274 consistent with the section VMA. Linker scripts can force VMA. */
3275 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3276 this_hdr->sh_addralign = mask & -mask;
3277 /* The sh_entsize and sh_info fields may have been set already by
3278 copy_private_section_data. */
3279
3280 this_hdr->bfd_section = asect;
3281 this_hdr->contents = NULL;
3282
3283 /* If the section type is unspecified, we set it based on
3284 asect->flags. */
3285 if (asect->type != 0)
3286 sh_type = asect->type;
3287 else if ((asect->flags & SEC_GROUP) != 0)
3288 sh_type = SHT_GROUP;
3289 else
3290 sh_type = bfd_elf_get_default_section_type (asect->flags);
3291
3292 if (this_hdr->sh_type == SHT_NULL)
3293 this_hdr->sh_type = sh_type;
3294 else if (this_hdr->sh_type == SHT_NOBITS
3295 && sh_type == SHT_PROGBITS
3296 && (asect->flags & SEC_ALLOC) != 0)
3297 {
3298 /* Warn if we are changing a NOBITS section to PROGBITS, but
3299 allow the link to proceed. This can happen when users link
3300 non-bss input sections to bss output sections, or emit data
3301 to a bss output section via a linker script. */
3302 _bfd_error_handler
3303 (_("warning: section `%pA' type changed to PROGBITS"), asect);
3304 this_hdr->sh_type = sh_type;
3305 }
3306
3307 switch (this_hdr->sh_type)
3308 {
3309 default:
3310 break;
3311
3312 case SHT_STRTAB:
3313 case SHT_NOTE:
3314 case SHT_NOBITS:
3315 case SHT_PROGBITS:
3316 break;
3317
3318 case SHT_INIT_ARRAY:
3319 case SHT_FINI_ARRAY:
3320 case SHT_PREINIT_ARRAY:
3321 this_hdr->sh_entsize = bed->s->arch_size / 8;
3322 break;
3323
3324 case SHT_HASH:
3325 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3326 break;
3327
3328 case SHT_DYNSYM:
3329 this_hdr->sh_entsize = bed->s->sizeof_sym;
3330 break;
3331
3332 case SHT_DYNAMIC:
3333 this_hdr->sh_entsize = bed->s->sizeof_dyn;
3334 break;
3335
3336 case SHT_RELA:
3337 if (get_elf_backend_data (abfd)->may_use_rela_p)
3338 this_hdr->sh_entsize = bed->s->sizeof_rela;
3339 break;
3340
3341 case SHT_REL:
3342 if (get_elf_backend_data (abfd)->may_use_rel_p)
3343 this_hdr->sh_entsize = bed->s->sizeof_rel;
3344 break;
3345
3346 case SHT_GNU_versym:
3347 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3348 break;
3349
3350 case SHT_GNU_verdef:
3351 this_hdr->sh_entsize = 0;
3352 /* objcopy or strip will copy over sh_info, but may not set
3353 cverdefs. The linker will set cverdefs, but sh_info will be
3354 zero. */
3355 if (this_hdr->sh_info == 0)
3356 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3357 else
3358 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3359 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3360 break;
3361
3362 case SHT_GNU_verneed:
3363 this_hdr->sh_entsize = 0;
3364 /* objcopy or strip will copy over sh_info, but may not set
3365 cverrefs. The linker will set cverrefs, but sh_info will be
3366 zero. */
3367 if (this_hdr->sh_info == 0)
3368 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3369 else
3370 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3371 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3372 break;
3373
3374 case SHT_GROUP:
3375 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3376 break;
3377
3378 case SHT_GNU_HASH:
3379 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3380 break;
3381 }
3382
3383 if ((asect->flags & SEC_ALLOC) != 0)
3384 this_hdr->sh_flags |= SHF_ALLOC;
3385 if ((asect->flags & SEC_READONLY) == 0)
3386 this_hdr->sh_flags |= SHF_WRITE;
3387 if ((asect->flags & SEC_CODE) != 0)
3388 this_hdr->sh_flags |= SHF_EXECINSTR;
3389 if ((asect->flags & SEC_MERGE) != 0)
3390 {
3391 this_hdr->sh_flags |= SHF_MERGE;
3392 this_hdr->sh_entsize = asect->entsize;
3393 }
3394 if ((asect->flags & SEC_STRINGS) != 0)
3395 this_hdr->sh_flags |= SHF_STRINGS;
3396 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3397 this_hdr->sh_flags |= SHF_GROUP;
3398 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3399 {
3400 this_hdr->sh_flags |= SHF_TLS;
3401 if (asect->size == 0
3402 && (asect->flags & SEC_HAS_CONTENTS) == 0)
3403 {
3404 struct bfd_link_order *o = asect->map_tail.link_order;
3405
3406 this_hdr->sh_size = 0;
3407 if (o != NULL)
3408 {
3409 this_hdr->sh_size = o->offset + o->size;
3410 if (this_hdr->sh_size != 0)
3411 this_hdr->sh_type = SHT_NOBITS;
3412 }
3413 }
3414 }
3415 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3416 this_hdr->sh_flags |= SHF_EXCLUDE;
3417
3418 /* If the section has relocs, set up a section header for the
3419 SHT_REL[A] section. If two relocation sections are required for
3420 this section, it is up to the processor-specific back-end to
3421 create the other. */
3422 if ((asect->flags & SEC_RELOC) != 0)
3423 {
3424 /* When doing a relocatable link, create both REL and RELA sections if
3425 needed. */
3426 if (arg->link_info
3427 /* Do the normal setup if we wouldn't create any sections here. */
3428 && esd->rel.count + esd->rela.count > 0
3429 && (bfd_link_relocatable (arg->link_info)
3430 || arg->link_info->emitrelocations))
3431 {
3432 if (esd->rel.count && esd->rel.hdr == NULL
3433 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3434 false, delay_st_name_p))
3435 {
3436 arg->failed = true;
3437 return;
3438 }
3439 if (esd->rela.count && esd->rela.hdr == NULL
3440 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3441 true, delay_st_name_p))
3442 {
3443 arg->failed = true;
3444 return;
3445 }
3446 }
3447 else if (!_bfd_elf_init_reloc_shdr (abfd,
3448 (asect->use_rela_p
3449 ? &esd->rela : &esd->rel),
3450 name,
3451 asect->use_rela_p,
3452 delay_st_name_p))
3453 {
3454 arg->failed = true;
3455 return;
3456 }
3457 }
3458
3459 /* Check for processor-specific section types. */
3460 sh_type = this_hdr->sh_type;
3461 if (bed->elf_backend_fake_sections
3462 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3463 {
3464 arg->failed = true;
3465 return;
3466 }
3467
3468 if (sh_type == SHT_NOBITS && asect->size != 0)
3469 {
3470 /* Don't change the header type from NOBITS if we are being
3471 called for objcopy --only-keep-debug. */
3472 this_hdr->sh_type = sh_type;
3473 }
3474 }
3475
3476 /* Fill in the contents of a SHT_GROUP section. Called from
3477 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3478 when ELF targets use the generic linker, ld. Called for ld -r
3479 from bfd_elf_final_link. */
3480
3481 void
bfd_elf_set_group_contents(bfd * abfd,asection * sec,void * failedptrarg)3482 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3483 {
3484 bool *failedptr = (bool *) failedptrarg;
3485 asection *elt, *first;
3486 unsigned char *loc;
3487 bool gas;
3488
3489 /* Ignore linker created group section. See elfNN_ia64_object_p in
3490 elfxx-ia64.c. */
3491 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3492 || sec->size == 0
3493 || *failedptr)
3494 return;
3495
3496 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3497 {
3498 unsigned long symindx = 0;
3499
3500 /* elf_group_id will have been set up by objcopy and the
3501 generic linker. */
3502 if (elf_group_id (sec) != NULL)
3503 symindx = elf_group_id (sec)->udata.i;
3504
3505 if (symindx == 0)
3506 {
3507 /* If called from the assembler, swap_out_syms will have set up
3508 elf_section_syms.
3509 PR 25699: A corrupt input file could contain bogus group info. */
3510 if (sec->index >= elf_num_section_syms (abfd)
3511 || elf_section_syms (abfd)[sec->index] == NULL)
3512 {
3513 *failedptr = true;
3514 return;
3515 }
3516 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3517 }
3518 elf_section_data (sec)->this_hdr.sh_info = symindx;
3519 }
3520 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3521 {
3522 /* The ELF backend linker sets sh_info to -2 when the group
3523 signature symbol is global, and thus the index can't be
3524 set until all local symbols are output. */
3525 asection *igroup;
3526 struct bfd_elf_section_data *sec_data;
3527 unsigned long symndx;
3528 unsigned long extsymoff;
3529 struct elf_link_hash_entry *h;
3530
3531 /* The point of this little dance to the first SHF_GROUP section
3532 then back to the SHT_GROUP section is that this gets us to
3533 the SHT_GROUP in the input object. */
3534 igroup = elf_sec_group (elf_next_in_group (sec));
3535 sec_data = elf_section_data (igroup);
3536 symndx = sec_data->this_hdr.sh_info;
3537 extsymoff = 0;
3538 if (!elf_bad_symtab (igroup->owner))
3539 {
3540 Elf_Internal_Shdr *symtab_hdr;
3541
3542 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3543 extsymoff = symtab_hdr->sh_info;
3544 }
3545 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3546 while (h->root.type == bfd_link_hash_indirect
3547 || h->root.type == bfd_link_hash_warning)
3548 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3549
3550 elf_section_data (sec)->this_hdr.sh_info = h->indx;
3551 }
3552
3553 /* The contents won't be allocated for "ld -r" or objcopy. */
3554 gas = true;
3555 if (sec->contents == NULL)
3556 {
3557 gas = false;
3558 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3559
3560 /* Arrange for the section to be written out. */
3561 elf_section_data (sec)->this_hdr.contents = sec->contents;
3562 if (sec->contents == NULL)
3563 {
3564 *failedptr = true;
3565 return;
3566 }
3567 }
3568
3569 loc = sec->contents + sec->size;
3570
3571 /* Get the pointer to the first section in the group that gas
3572 squirreled away here. objcopy arranges for this to be set to the
3573 start of the input section group. */
3574 first = elt = elf_next_in_group (sec);
3575
3576 /* First element is a flag word. Rest of section is elf section
3577 indices for all the sections of the group. Write them backwards
3578 just to keep the group in the same order as given in .section
3579 directives, not that it matters. */
3580 while (elt != NULL)
3581 {
3582 asection *s;
3583
3584 s = elt;
3585 if (!gas)
3586 s = s->output_section;
3587 if (s != NULL
3588 && !bfd_is_abs_section (s))
3589 {
3590 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
3591 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3592
3593 if (elf_sec->rel.hdr != NULL
3594 && (gas
3595 || (input_elf_sec->rel.hdr != NULL
3596 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
3597 {
3598 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
3599 loc -= 4;
3600 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3601 }
3602 if (elf_sec->rela.hdr != NULL
3603 && (gas
3604 || (input_elf_sec->rela.hdr != NULL
3605 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
3606 {
3607 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
3608 loc -= 4;
3609 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3610 }
3611 loc -= 4;
3612 H_PUT_32 (abfd, elf_sec->this_idx, loc);
3613 }
3614 elt = elf_next_in_group (elt);
3615 if (elt == first)
3616 break;
3617 }
3618
3619 loc -= 4;
3620 BFD_ASSERT (loc == sec->contents);
3621
3622 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3623 }
3624
3625 /* Given NAME, the name of a relocation section stripped of its
3626 .rel/.rela prefix, return the section in ABFD to which the
3627 relocations apply. */
3628
3629 asection *
_bfd_elf_plt_get_reloc_section(bfd * abfd,const char * name)3630 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3631 {
3632 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3633 section likely apply to .got.plt or .got section. */
3634 if (get_elf_backend_data (abfd)->want_got_plt
3635 && strcmp (name, ".plt") == 0)
3636 {
3637 asection *sec;
3638
3639 name = ".got.plt";
3640 sec = bfd_get_section_by_name (abfd, name);
3641 if (sec != NULL)
3642 return sec;
3643 name = ".got";
3644 }
3645
3646 return bfd_get_section_by_name (abfd, name);
3647 }
3648
3649 /* Return the section to which RELOC_SEC applies. */
3650
3651 static asection *
elf_get_reloc_section(asection * reloc_sec)3652 elf_get_reloc_section (asection *reloc_sec)
3653 {
3654 const char *name;
3655 unsigned int type;
3656 bfd *abfd;
3657 const struct elf_backend_data *bed;
3658
3659 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3660 if (type != SHT_REL && type != SHT_RELA)
3661 return NULL;
3662
3663 /* We look up the section the relocs apply to by name. */
3664 name = reloc_sec->name;
3665 if (!startswith (name, ".rel"))
3666 return NULL;
3667 name += 4;
3668 if (type == SHT_RELA && *name++ != 'a')
3669 return NULL;
3670
3671 abfd = reloc_sec->owner;
3672 bed = get_elf_backend_data (abfd);
3673 return bed->get_reloc_section (abfd, name);
3674 }
3675
3676 /* Assign all ELF section numbers. The dummy first section is handled here
3677 too. The link/info pointers for the standard section types are filled
3678 in here too, while we're at it. LINK_INFO will be 0 when arriving
3679 here for objcopy, and when using the generic ELF linker. */
3680
3681 static bool
assign_section_numbers(bfd * abfd,struct bfd_link_info * link_info)3682 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3683 {
3684 struct elf_obj_tdata *t = elf_tdata (abfd);
3685 asection *sec;
3686 unsigned int section_number;
3687 Elf_Internal_Shdr **i_shdrp;
3688 struct bfd_elf_section_data *d;
3689 bool need_symtab;
3690 size_t amt;
3691
3692 section_number = 1;
3693
3694 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3695
3696 /* SHT_GROUP sections are in relocatable files only. */
3697 if (link_info == NULL || !link_info->resolve_section_groups)
3698 {
3699 size_t reloc_count = 0;
3700
3701 /* Put SHT_GROUP sections first. */
3702 for (sec = abfd->sections; sec != NULL; sec = sec->next)
3703 {
3704 d = elf_section_data (sec);
3705
3706 if (d->this_hdr.sh_type == SHT_GROUP)
3707 {
3708 if (sec->flags & SEC_LINKER_CREATED)
3709 {
3710 /* Remove the linker created SHT_GROUP sections. */
3711 bfd_section_list_remove (abfd, sec);
3712 abfd->section_count--;
3713 }
3714 else
3715 d->this_idx = section_number++;
3716 }
3717
3718 /* Count relocations. */
3719 reloc_count += sec->reloc_count;
3720 }
3721
3722 /* Clear HAS_RELOC if there are no relocations. */
3723 if (reloc_count == 0)
3724 abfd->flags &= ~HAS_RELOC;
3725 }
3726
3727 for (sec = abfd->sections; sec; sec = sec->next)
3728 {
3729 d = elf_section_data (sec);
3730
3731 if (d->this_hdr.sh_type != SHT_GROUP)
3732 d->this_idx = section_number++;
3733 if (d->this_hdr.sh_name != (unsigned int) -1)
3734 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3735 if (d->rel.hdr)
3736 {
3737 d->rel.idx = section_number++;
3738 if (d->rel.hdr->sh_name != (unsigned int) -1)
3739 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3740 }
3741 else
3742 d->rel.idx = 0;
3743
3744 if (d->rela.hdr)
3745 {
3746 d->rela.idx = section_number++;
3747 if (d->rela.hdr->sh_name != (unsigned int) -1)
3748 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3749 }
3750 else
3751 d->rela.idx = 0;
3752 }
3753
3754 need_symtab = (bfd_get_symcount (abfd) > 0
3755 || (link_info == NULL
3756 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3757 == HAS_RELOC)));
3758 if (need_symtab)
3759 {
3760 elf_onesymtab (abfd) = section_number++;
3761 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3762 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3763 {
3764 elf_section_list *entry;
3765
3766 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3767
3768 entry = bfd_zalloc (abfd, sizeof (*entry));
3769 entry->ndx = section_number++;
3770 elf_symtab_shndx_list (abfd) = entry;
3771 entry->hdr.sh_name
3772 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3773 ".symtab_shndx", false);
3774 if (entry->hdr.sh_name == (unsigned int) -1)
3775 return false;
3776 }
3777 elf_strtab_sec (abfd) = section_number++;
3778 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3779 }
3780
3781 elf_shstrtab_sec (abfd) = section_number++;
3782 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3783 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3784
3785 if (section_number >= SHN_LORESERVE)
3786 {
3787 /* xgettext:c-format */
3788 _bfd_error_handler (_("%pB: too many sections: %u"),
3789 abfd, section_number);
3790 return false;
3791 }
3792
3793 elf_numsections (abfd) = section_number;
3794 elf_elfheader (abfd)->e_shnum = section_number;
3795
3796 /* Set up the list of section header pointers, in agreement with the
3797 indices. */
3798 amt = section_number * sizeof (Elf_Internal_Shdr *);
3799 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
3800 if (i_shdrp == NULL)
3801 return false;
3802
3803 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3804 sizeof (Elf_Internal_Shdr));
3805 if (i_shdrp[0] == NULL)
3806 {
3807 bfd_release (abfd, i_shdrp);
3808 return false;
3809 }
3810
3811 elf_elfsections (abfd) = i_shdrp;
3812
3813 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3814 if (need_symtab)
3815 {
3816 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3817 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3818 {
3819 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3820 BFD_ASSERT (entry != NULL);
3821 i_shdrp[entry->ndx] = & entry->hdr;
3822 entry->hdr.sh_link = elf_onesymtab (abfd);
3823 }
3824 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3825 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3826 }
3827
3828 for (sec = abfd->sections; sec; sec = sec->next)
3829 {
3830 asection *s;
3831
3832 d = elf_section_data (sec);
3833
3834 i_shdrp[d->this_idx] = &d->this_hdr;
3835 if (d->rel.idx != 0)
3836 i_shdrp[d->rel.idx] = d->rel.hdr;
3837 if (d->rela.idx != 0)
3838 i_shdrp[d->rela.idx] = d->rela.hdr;
3839
3840 /* Fill in the sh_link and sh_info fields while we're at it. */
3841
3842 /* sh_link of a reloc section is the section index of the symbol
3843 table. sh_info is the section index of the section to which
3844 the relocation entries apply. */
3845 if (d->rel.idx != 0)
3846 {
3847 d->rel.hdr->sh_link = elf_onesymtab (abfd);
3848 d->rel.hdr->sh_info = d->this_idx;
3849 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3850 }
3851 if (d->rela.idx != 0)
3852 {
3853 d->rela.hdr->sh_link = elf_onesymtab (abfd);
3854 d->rela.hdr->sh_info = d->this_idx;
3855 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3856 }
3857
3858 /* We need to set up sh_link for SHF_LINK_ORDER. */
3859 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3860 {
3861 s = elf_linked_to_section (sec);
3862 /* We can now have a NULL linked section pointer.
3863 This happens when the sh_link field is 0, which is done
3864 when a linked to section is discarded but the linking
3865 section has been retained for some reason. */
3866 if (s)
3867 {
3868 /* Check discarded linkonce section. */
3869 if (discarded_section (s))
3870 {
3871 asection *kept;
3872 _bfd_error_handler
3873 /* xgettext:c-format */
3874 (_("%pB: sh_link of section `%pA' points to"
3875 " discarded section `%pA' of `%pB'"),
3876 abfd, d->this_hdr.bfd_section, s, s->owner);
3877 /* Point to the kept section if it has the same
3878 size as the discarded one. */
3879 kept = _bfd_elf_check_kept_section (s, link_info);
3880 if (kept == NULL)
3881 {
3882 bfd_set_error (bfd_error_bad_value);
3883 return false;
3884 }
3885 s = kept;
3886 }
3887 /* Handle objcopy. */
3888 else if (s->output_section == NULL)
3889 {
3890 _bfd_error_handler
3891 /* xgettext:c-format */
3892 (_("%pB: sh_link of section `%pA' points to"
3893 " removed section `%pA' of `%pB'"),
3894 abfd, d->this_hdr.bfd_section, s, s->owner);
3895 bfd_set_error (bfd_error_bad_value);
3896 return false;
3897 }
3898 s = s->output_section;
3899 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3900 }
3901 }
3902
3903 switch (d->this_hdr.sh_type)
3904 {
3905 case SHT_REL:
3906 case SHT_RELA:
3907 /* A reloc section which we are treating as a normal BFD
3908 section. sh_link is the section index of the symbol
3909 table. sh_info is the section index of the section to
3910 which the relocation entries apply. We assume that an
3911 allocated reloc section uses the dynamic symbol table
3912 if there is one. Otherwise we guess the normal symbol
3913 table. FIXME: How can we be sure? */
3914 if (d->this_hdr.sh_link == 0 && (sec->flags & SEC_ALLOC) != 0)
3915 {
3916 s = bfd_get_section_by_name (abfd, ".dynsym");
3917 if (s != NULL)
3918 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3919 }
3920 if (d->this_hdr.sh_link == 0)
3921 d->this_hdr.sh_link = elf_onesymtab (abfd);
3922
3923 s = elf_get_reloc_section (sec);
3924 if (s != NULL)
3925 {
3926 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3927 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3928 }
3929 break;
3930
3931 case SHT_STRTAB:
3932 /* We assume that a section named .stab*str is a stabs
3933 string section. We look for a section with the same name
3934 but without the trailing ``str'', and set its sh_link
3935 field to point to this section. */
3936 if (startswith (sec->name, ".stab")
3937 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3938 {
3939 size_t len;
3940 char *alc;
3941
3942 len = strlen (sec->name);
3943 alc = (char *) bfd_malloc (len - 2);
3944 if (alc == NULL)
3945 return false;
3946 memcpy (alc, sec->name, len - 3);
3947 alc[len - 3] = '\0';
3948 s = bfd_get_section_by_name (abfd, alc);
3949 free (alc);
3950 if (s != NULL)
3951 {
3952 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3953
3954 /* This is a .stab section. */
3955 elf_section_data (s)->this_hdr.sh_entsize = 12;
3956 }
3957 }
3958 break;
3959
3960 case SHT_DYNAMIC:
3961 case SHT_DYNSYM:
3962 case SHT_GNU_verneed:
3963 case SHT_GNU_verdef:
3964 /* sh_link is the section header index of the string table
3965 used for the dynamic entries, or the symbol table, or the
3966 version strings. */
3967 s = bfd_get_section_by_name (abfd, ".dynstr");
3968 if (s != NULL)
3969 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3970 break;
3971
3972 case SHT_GNU_LIBLIST:
3973 /* sh_link is the section header index of the prelink library
3974 list used for the dynamic entries, or the symbol table, or
3975 the version strings. */
3976 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
3977 ? ".dynstr" : ".gnu.libstr"));
3978 if (s != NULL)
3979 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3980 break;
3981
3982 case SHT_HASH:
3983 case SHT_GNU_HASH:
3984 case SHT_GNU_versym:
3985 /* sh_link is the section header index of the symbol table
3986 this hash table or version table is for. */
3987 s = bfd_get_section_by_name (abfd, ".dynsym");
3988 if (s != NULL)
3989 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3990 break;
3991
3992 case SHT_GROUP:
3993 d->this_hdr.sh_link = elf_onesymtab (abfd);
3994 }
3995 }
3996
3997 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3998 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3999 debug section name from .debug_* to .zdebug_* if needed. */
4000
4001 return true;
4002 }
4003
4004 static bool
sym_is_global(bfd * abfd,asymbol * sym)4005 sym_is_global (bfd *abfd, asymbol *sym)
4006 {
4007 /* If the backend has a special mapping, use it. */
4008 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4009 if (bed->elf_backend_sym_is_global)
4010 return (*bed->elf_backend_sym_is_global) (abfd, sym);
4011
4012 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4013 || bfd_is_und_section (bfd_asymbol_section (sym))
4014 || bfd_is_com_section (bfd_asymbol_section (sym)));
4015 }
4016
4017 /* Filter global symbols of ABFD to include in the import library. All
4018 SYMCOUNT symbols of ABFD can be examined from their pointers in
4019 SYMS. Pointers of symbols to keep should be stored contiguously at
4020 the beginning of that array.
4021
4022 Returns the number of symbols to keep. */
4023
4024 unsigned int
_bfd_elf_filter_global_symbols(bfd * abfd,struct bfd_link_info * info,asymbol ** syms,long symcount)4025 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4026 asymbol **syms, long symcount)
4027 {
4028 long src_count, dst_count = 0;
4029
4030 for (src_count = 0; src_count < symcount; src_count++)
4031 {
4032 asymbol *sym = syms[src_count];
4033 char *name = (char *) bfd_asymbol_name (sym);
4034 struct bfd_link_hash_entry *h;
4035
4036 if (!sym_is_global (abfd, sym))
4037 continue;
4038
4039 h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4040 if (h == NULL)
4041 continue;
4042 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4043 continue;
4044 if (h->linker_def || h->ldscript_def)
4045 continue;
4046
4047 syms[dst_count++] = sym;
4048 }
4049
4050 syms[dst_count] = NULL;
4051
4052 return dst_count;
4053 }
4054
4055 /* Don't output section symbols for sections that are not going to be
4056 output, that are duplicates or there is no BFD section. */
4057
4058 static bool
ignore_section_sym(bfd * abfd,asymbol * sym)4059 ignore_section_sym (bfd *abfd, asymbol *sym)
4060 {
4061 elf_symbol_type *type_ptr;
4062
4063 if (sym == NULL)
4064 return false;
4065
4066 if ((sym->flags & BSF_SECTION_SYM) == 0)
4067 return false;
4068
4069 /* Ignore the section symbol if it isn't used. */
4070 if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4071 return true;
4072
4073 if (sym->section == NULL)
4074 return true;
4075
4076 type_ptr = elf_symbol_from (sym);
4077 return ((type_ptr != NULL
4078 && type_ptr->internal_elf_sym.st_shndx != 0
4079 && bfd_is_abs_section (sym->section))
4080 || !(sym->section->owner == abfd
4081 || (sym->section->output_section != NULL
4082 && sym->section->output_section->owner == abfd
4083 && sym->section->output_offset == 0)
4084 || bfd_is_abs_section (sym->section)));
4085 }
4086
4087 /* Map symbol from it's internal number to the external number, moving
4088 all local symbols to be at the head of the list. */
4089
4090 static bool
elf_map_symbols(bfd * abfd,unsigned int * pnum_locals)4091 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4092 {
4093 unsigned int symcount = bfd_get_symcount (abfd);
4094 asymbol **syms = bfd_get_outsymbols (abfd);
4095 asymbol **sect_syms;
4096 unsigned int num_locals = 0;
4097 unsigned int num_globals = 0;
4098 unsigned int num_locals2 = 0;
4099 unsigned int num_globals2 = 0;
4100 unsigned int max_index = 0;
4101 unsigned int idx;
4102 asection *asect;
4103 asymbol **new_syms;
4104 size_t amt;
4105
4106 #ifdef DEBUG
4107 fprintf (stderr, "elf_map_symbols\n");
4108 fflush (stderr);
4109 #endif
4110
4111 for (asect = abfd->sections; asect; asect = asect->next)
4112 {
4113 if (max_index < asect->index)
4114 max_index = asect->index;
4115 }
4116
4117 max_index++;
4118 amt = max_index * sizeof (asymbol *);
4119 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4120 if (sect_syms == NULL)
4121 return false;
4122 elf_section_syms (abfd) = sect_syms;
4123 elf_num_section_syms (abfd) = max_index;
4124
4125 /* Init sect_syms entries for any section symbols we have already
4126 decided to output. */
4127 for (idx = 0; idx < symcount; idx++)
4128 {
4129 asymbol *sym = syms[idx];
4130
4131 if ((sym->flags & BSF_SECTION_SYM) != 0
4132 && sym->value == 0
4133 && !ignore_section_sym (abfd, sym)
4134 && !bfd_is_abs_section (sym->section))
4135 {
4136 asection *sec = sym->section;
4137
4138 if (sec->owner != abfd)
4139 sec = sec->output_section;
4140
4141 sect_syms[sec->index] = syms[idx];
4142 }
4143 }
4144
4145 /* Classify all of the symbols. */
4146 for (idx = 0; idx < symcount; idx++)
4147 {
4148 if (sym_is_global (abfd, syms[idx]))
4149 num_globals++;
4150 else if (!ignore_section_sym (abfd, syms[idx]))
4151 num_locals++;
4152 }
4153
4154 /* We will be adding a section symbol for each normal BFD section. Most
4155 sections will already have a section symbol in outsymbols, but
4156 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4157 at least in that case. */
4158 for (asect = abfd->sections; asect; asect = asect->next)
4159 {
4160 asymbol *sym = asect->symbol;
4161 /* Don't include ignored section symbols. */
4162 if (!ignore_section_sym (abfd, sym)
4163 && sect_syms[asect->index] == NULL)
4164 {
4165 if (!sym_is_global (abfd, asect->symbol))
4166 num_locals++;
4167 else
4168 num_globals++;
4169 }
4170 }
4171
4172 /* Now sort the symbols so the local symbols are first. */
4173 amt = (num_locals + num_globals) * sizeof (asymbol *);
4174 new_syms = (asymbol **) bfd_alloc (abfd, amt);
4175 if (new_syms == NULL)
4176 return false;
4177
4178 for (idx = 0; idx < symcount; idx++)
4179 {
4180 asymbol *sym = syms[idx];
4181 unsigned int i;
4182
4183 if (sym_is_global (abfd, sym))
4184 i = num_locals + num_globals2++;
4185 /* Don't include ignored section symbols. */
4186 else if (!ignore_section_sym (abfd, sym))
4187 i = num_locals2++;
4188 else
4189 continue;
4190 new_syms[i] = sym;
4191 sym->udata.i = i + 1;
4192 }
4193 for (asect = abfd->sections; asect; asect = asect->next)
4194 {
4195 asymbol *sym = asect->symbol;
4196 if (!ignore_section_sym (abfd, sym)
4197 && sect_syms[asect->index] == NULL)
4198 {
4199 unsigned int i;
4200
4201 sect_syms[asect->index] = sym;
4202 if (!sym_is_global (abfd, sym))
4203 i = num_locals2++;
4204 else
4205 i = num_locals + num_globals2++;
4206 new_syms[i] = sym;
4207 sym->udata.i = i + 1;
4208 }
4209 }
4210
4211 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4212
4213 *pnum_locals = num_locals;
4214 return true;
4215 }
4216
4217 /* Align to the maximum file alignment that could be required for any
4218 ELF data structure. */
4219
4220 static inline file_ptr
align_file_position(file_ptr off,int align)4221 align_file_position (file_ptr off, int align)
4222 {
4223 return (off + align - 1) & ~(align - 1);
4224 }
4225
4226 /* Assign a file position to a section, optionally aligning to the
4227 required section alignment. */
4228
4229 file_ptr
_bfd_elf_assign_file_position_for_section(Elf_Internal_Shdr * i_shdrp,file_ptr offset,bool align)4230 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4231 file_ptr offset,
4232 bool align)
4233 {
4234 if (align && i_shdrp->sh_addralign > 1)
4235 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
4236 i_shdrp->sh_offset = offset;
4237 if (i_shdrp->bfd_section != NULL)
4238 i_shdrp->bfd_section->filepos = offset;
4239 if (i_shdrp->sh_type != SHT_NOBITS)
4240 offset += i_shdrp->sh_size;
4241 return offset;
4242 }
4243
4244 /* Compute the file positions we are going to put the sections at, and
4245 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4246 is not NULL, this is being called by the ELF backend linker. */
4247
4248 bool
_bfd_elf_compute_section_file_positions(bfd * abfd,struct bfd_link_info * link_info)4249 _bfd_elf_compute_section_file_positions (bfd *abfd,
4250 struct bfd_link_info *link_info)
4251 {
4252 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4253 struct fake_section_arg fsargs;
4254 bool failed;
4255 struct elf_strtab_hash *strtab = NULL;
4256 Elf_Internal_Shdr *shstrtab_hdr;
4257 bool need_symtab;
4258
4259 if (abfd->output_has_begun)
4260 return true;
4261
4262 /* Do any elf backend specific processing first. */
4263 if (bed->elf_backend_begin_write_processing)
4264 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4265
4266 if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4267 return false;
4268
4269 fsargs.failed = false;
4270 fsargs.link_info = link_info;
4271 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4272 if (fsargs.failed)
4273 return false;
4274
4275 if (!assign_section_numbers (abfd, link_info))
4276 return false;
4277
4278 /* The backend linker builds symbol table information itself. */
4279 need_symtab = (link_info == NULL
4280 && (bfd_get_symcount (abfd) > 0
4281 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4282 == HAS_RELOC)));
4283 if (need_symtab)
4284 {
4285 /* Non-zero if doing a relocatable link. */
4286 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4287
4288 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4289 return false;
4290 }
4291
4292 failed = false;
4293 if (link_info == NULL)
4294 {
4295 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4296 if (failed)
4297 return false;
4298 }
4299
4300 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4301 /* sh_name was set in init_file_header. */
4302 shstrtab_hdr->sh_type = SHT_STRTAB;
4303 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
4304 shstrtab_hdr->sh_addr = 0;
4305 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4306 shstrtab_hdr->sh_entsize = 0;
4307 shstrtab_hdr->sh_link = 0;
4308 shstrtab_hdr->sh_info = 0;
4309 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4310 shstrtab_hdr->sh_addralign = 1;
4311
4312 if (!assign_file_positions_except_relocs (abfd, link_info))
4313 return false;
4314
4315 if (need_symtab)
4316 {
4317 file_ptr off;
4318 Elf_Internal_Shdr *hdr;
4319
4320 off = elf_next_file_pos (abfd);
4321
4322 hdr = & elf_symtab_hdr (abfd);
4323 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4324
4325 if (elf_symtab_shndx_list (abfd) != NULL)
4326 {
4327 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4328 if (hdr->sh_size != 0)
4329 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4330 /* FIXME: What about other symtab_shndx sections in the list ? */
4331 }
4332
4333 hdr = &elf_tdata (abfd)->strtab_hdr;
4334 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
4335
4336 elf_next_file_pos (abfd) = off;
4337
4338 /* Now that we know where the .strtab section goes, write it
4339 out. */
4340 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4341 || ! _bfd_elf_strtab_emit (abfd, strtab))
4342 return false;
4343 _bfd_elf_strtab_free (strtab);
4344 }
4345
4346 abfd->output_has_begun = true;
4347
4348 return true;
4349 }
4350
4351 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the
4352 function effectively returns whether --eh-frame-hdr is given on the
4353 command line. After size_dynamic_sections the result reflects
4354 whether .eh_frame_hdr will actually be output (sizing isn't done
4355 until ldemul_after_allocation). */
4356
4357 static asection *
elf_eh_frame_hdr(const struct bfd_link_info * info)4358 elf_eh_frame_hdr (const struct bfd_link_info *info)
4359 {
4360 if (info != NULL && is_elf_hash_table (info->hash))
4361 return elf_hash_table (info)->eh_info.hdr_sec;
4362 return NULL;
4363 }
4364
4365 /* Make an initial estimate of the size of the program header. If we
4366 get the number wrong here, we'll redo section placement. */
4367
4368 static bfd_size_type
get_program_header_size(bfd * abfd,struct bfd_link_info * info)4369 get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4370 {
4371 size_t segs;
4372 asection *s, *s2;
4373 const struct elf_backend_data *bed;
4374
4375 /* Assume we will need exactly two PT_LOAD segments: one for text
4376 and one for data. */
4377 segs = 2;
4378
4379 s = bfd_get_section_by_name (abfd, ".interp");
4380 s2 = bfd_get_section_by_name (abfd, ".dynamic");
4381 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4382 {
4383 ++segs;
4384 }
4385
4386 if (s2 != NULL && (s2->flags & SEC_LOAD) != 0)
4387 {
4388 /* We need a PT_DYNAMIC segment. */
4389 ++segs;
4390 }
4391
4392 if ((s != NULL && (s->flags & SEC_LOAD) != 0) ||
4393 (s2 != NULL && (s2->flags & SEC_LOAD) != 0))
4394 {
4395 /*
4396 * If either a PT_INTERP or PT_DYNAMIC segment is created,
4397 * also create a PT_PHDR segment.
4398 */
4399 ++segs;
4400 }
4401
4402 if (info != NULL && info->relro)
4403 {
4404 /* We need a PT_GNU_RELRO segment. */
4405 ++segs;
4406 }
4407
4408 if (elf_eh_frame_hdr (info))
4409 {
4410 /* We need a PT_GNU_EH_FRAME segment. */
4411 ++segs;
4412 }
4413
4414 if (elf_stack_flags (abfd))
4415 {
4416 /* We need a PT_GNU_STACK segment. */
4417 ++segs;
4418 }
4419
4420 s = bfd_get_section_by_name (abfd,
4421 NOTE_GNU_PROPERTY_SECTION_NAME);
4422 if (s != NULL && s->size != 0)
4423 {
4424 /* We need a PT_GNU_PROPERTY segment. */
4425 ++segs;
4426 }
4427
4428 for (s = abfd->sections; s != NULL; s = s->next)
4429 {
4430 if ((s->flags & SEC_LOAD) != 0
4431 && elf_section_type (s) == SHT_NOTE)
4432 {
4433 unsigned int alignment_power;
4434 /* We need a PT_NOTE segment. */
4435 ++segs;
4436 /* Try to create just one PT_NOTE segment for all adjacent
4437 loadable SHT_NOTE sections. gABI requires that within a
4438 PT_NOTE segment (and also inside of each SHT_NOTE section)
4439 each note should have the same alignment. So we check
4440 whether the sections are correctly aligned. */
4441 alignment_power = s->alignment_power;
4442 while (s->next != NULL
4443 && s->next->alignment_power == alignment_power
4444 && (s->next->flags & SEC_LOAD) != 0
4445 && elf_section_type (s->next) == SHT_NOTE)
4446 s = s->next;
4447 }
4448 }
4449
4450 for (s = abfd->sections; s != NULL; s = s->next)
4451 {
4452 if (s->flags & SEC_THREAD_LOCAL)
4453 {
4454 /* We need a PT_TLS segment. */
4455 ++segs;
4456 break;
4457 }
4458 }
4459
4460 bed = get_elf_backend_data (abfd);
4461
4462 if ((abfd->flags & D_PAGED) != 0
4463 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4464 {
4465 /* Add a PT_GNU_MBIND segment for each mbind section. */
4466 bfd_vma commonpagesize;
4467 unsigned int page_align_power;
4468
4469 if (info != NULL)
4470 commonpagesize = info->commonpagesize;
4471 else
4472 commonpagesize = bed->commonpagesize;
4473 page_align_power = bfd_log2 (commonpagesize);
4474 for (s = abfd->sections; s != NULL; s = s->next)
4475 if (elf_section_flags (s) & SHF_GNU_MBIND)
4476 {
4477 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4478 {
4479 _bfd_error_handler
4480 /* xgettext:c-format */
4481 (_("%pB: GNU_MBIND section `%pA' has invalid "
4482 "sh_info field: %d"),
4483 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4484 continue;
4485 }
4486 /* Align mbind section to page size. */
4487 if (s->alignment_power < page_align_power)
4488 s->alignment_power = page_align_power;
4489 segs ++;
4490 }
4491 }
4492
4493 /* Let the backend count up any program headers it might need. */
4494 if (bed->elf_backend_additional_program_headers)
4495 {
4496 int a;
4497
4498 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4499 if (a == -1)
4500 abort ();
4501 segs += a;
4502 }
4503
4504 return segs * bed->s->sizeof_phdr;
4505 }
4506
4507 /* Find the segment that contains the output_section of section. */
4508
4509 Elf_Internal_Phdr *
_bfd_elf_find_segment_containing_section(bfd * abfd,asection * section)4510 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4511 {
4512 struct elf_segment_map *m;
4513 Elf_Internal_Phdr *p;
4514
4515 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4516 m != NULL;
4517 m = m->next, p++)
4518 {
4519 int i;
4520
4521 for (i = m->count - 1; i >= 0; i--)
4522 if (m->sections[i] == section)
4523 return p;
4524 }
4525
4526 return NULL;
4527 }
4528
4529 /* Create a mapping from a set of sections to a program segment. */
4530
4531 static struct elf_segment_map *
make_mapping(bfd * abfd,asection ** sections,unsigned int from,unsigned int to,bool phdr)4532 make_mapping (bfd *abfd,
4533 asection **sections,
4534 unsigned int from,
4535 unsigned int to,
4536 bool phdr)
4537 {
4538 struct elf_segment_map *m;
4539 unsigned int i;
4540 asection **hdrpp;
4541 size_t amt;
4542
4543 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4544 amt += (to - from) * sizeof (asection *);
4545 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4546 if (m == NULL)
4547 return NULL;
4548 m->next = NULL;
4549 m->p_type = PT_LOAD;
4550 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4551 m->sections[i - from] = *hdrpp;
4552 m->count = to - from;
4553
4554 if (from == 0 && phdr)
4555 {
4556 /* Include the headers in the first PT_LOAD segment. */
4557 m->includes_filehdr = 1;
4558 m->includes_phdrs = 1;
4559 }
4560
4561 return m;
4562 }
4563
4564 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4565 on failure. */
4566
4567 struct elf_segment_map *
_bfd_elf_make_dynamic_segment(bfd * abfd,asection * dynsec)4568 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4569 {
4570 struct elf_segment_map *m;
4571
4572 m = (struct elf_segment_map *) bfd_zalloc (abfd,
4573 sizeof (struct elf_segment_map));
4574 if (m == NULL)
4575 return NULL;
4576 m->next = NULL;
4577 m->p_type = PT_DYNAMIC;
4578 m->count = 1;
4579 m->sections[0] = dynsec;
4580
4581 return m;
4582 }
4583
4584 /* Possibly add or remove segments from the segment map. */
4585
4586 static bool
elf_modify_segment_map(bfd * abfd,struct bfd_link_info * info,bool remove_empty_load)4587 elf_modify_segment_map (bfd *abfd,
4588 struct bfd_link_info *info,
4589 bool remove_empty_load)
4590 {
4591 struct elf_segment_map **m;
4592 const struct elf_backend_data *bed;
4593
4594 /* The placement algorithm assumes that non allocated sections are
4595 not in PT_LOAD segments. We ensure this here by removing such
4596 sections from the segment map. We also remove excluded
4597 sections. Finally, any PT_LOAD segment without sections is
4598 removed. */
4599 m = &elf_seg_map (abfd);
4600 while (*m)
4601 {
4602 unsigned int i, new_count;
4603
4604 for (new_count = 0, i = 0; i < (*m)->count; i++)
4605 {
4606 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4607 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4608 || (*m)->p_type != PT_LOAD))
4609 {
4610 (*m)->sections[new_count] = (*m)->sections[i];
4611 new_count++;
4612 }
4613 }
4614 (*m)->count = new_count;
4615
4616 if (remove_empty_load
4617 && (*m)->p_type == PT_LOAD
4618 && (*m)->count == 0
4619 && !(*m)->includes_phdrs)
4620 *m = (*m)->next;
4621 else
4622 m = &(*m)->next;
4623 }
4624
4625 bed = get_elf_backend_data (abfd);
4626 if (bed->elf_backend_modify_segment_map != NULL)
4627 {
4628 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4629 return false;
4630 }
4631
4632 return true;
4633 }
4634
4635 #define IS_TBSS(s) \
4636 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4637
4638 /* Set up a mapping from BFD sections to program segments. Update
4639 NEED_LAYOUT if the section layout is changed. */
4640
4641 bool
_bfd_elf_map_sections_to_segments(bfd * abfd,struct bfd_link_info * info,bool * need_layout)4642 _bfd_elf_map_sections_to_segments (bfd *abfd,
4643 struct bfd_link_info *info,
4644 bool *need_layout)
4645 {
4646 unsigned int count;
4647 struct elf_segment_map *m;
4648 asection **sections = NULL;
4649 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4650 bool no_user_phdrs;
4651
4652 no_user_phdrs = elf_seg_map (abfd) == NULL;
4653
4654 if (info != NULL)
4655 {
4656 info->user_phdrs = !no_user_phdrs;
4657
4658 /* Size the relative relocations if DT_RELR is enabled. */
4659 if (info->enable_dt_relr
4660 && need_layout != NULL
4661 && bed->size_relative_relocs
4662 && !bed->size_relative_relocs (info, need_layout))
4663 info->callbacks->einfo
4664 (_("%F%P: failed to size relative relocations\n"));
4665 }
4666
4667 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4668 {
4669 asection *s;
4670 unsigned int i;
4671 struct elf_segment_map *mfirst;
4672 struct elf_segment_map **pm;
4673 asection *last_hdr;
4674 bfd_vma last_size;
4675 unsigned int hdr_index;
4676 bfd_vma maxpagesize;
4677 asection **hdrpp;
4678 bool phdr_in_segment;
4679 bool writable;
4680 bool executable;
4681 unsigned int tls_count = 0;
4682 asection *first_tls = NULL;
4683 asection *first_mbind = NULL;
4684 asection *dynsec, *eh_frame_hdr;
4685 size_t amt;
4686 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */
4687 bfd_size_type phdr_size; /* Octets/bytes. */
4688 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
4689
4690 /* Select the allocated sections, and sort them. */
4691
4692 amt = bfd_count_sections (abfd) * sizeof (asection *);
4693 sections = (asection **) bfd_malloc (amt);
4694 if (sections == NULL)
4695 goto error_return;
4696
4697 /* Calculate top address, avoiding undefined behaviour of shift
4698 left operator when shift count is equal to size of type
4699 being shifted. */
4700 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4701 addr_mask = (addr_mask << 1) + 1;
4702
4703 i = 0;
4704 for (s = abfd->sections; s != NULL; s = s->next)
4705 {
4706 if ((s->flags & SEC_ALLOC) != 0)
4707 {
4708 /* target_index is unused until bfd_elf_final_link
4709 starts output of section symbols. Use it to make
4710 qsort stable. */
4711 s->target_index = i;
4712 sections[i] = s;
4713 ++i;
4714 /* A wrapping section potentially clashes with header. */
4715 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
4716 wrap_to = (s->lma + s->size / opb) & addr_mask;
4717 }
4718 }
4719 BFD_ASSERT (i <= bfd_count_sections (abfd));
4720 count = i;
4721
4722 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4723
4724 phdr_size = elf_program_header_size (abfd);
4725 if (phdr_size == (bfd_size_type) -1)
4726 phdr_size = get_program_header_size (abfd, info);
4727 phdr_size += bed->s->sizeof_ehdr;
4728 /* phdr_size is compared to LMA values which are in bytes. */
4729 phdr_size /= opb;
4730 if (info != NULL)
4731 maxpagesize = info->maxpagesize;
4732 else
4733 maxpagesize = bed->maxpagesize;
4734 if (maxpagesize == 0)
4735 maxpagesize = 1;
4736 phdr_in_segment = info != NULL && info->load_phdrs;
4737 if (count != 0
4738 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
4739 >= (phdr_size & (maxpagesize - 1))))
4740 /* For compatibility with old scripts that may not be using
4741 SIZEOF_HEADERS, add headers when it looks like space has
4742 been left for them. */
4743 phdr_in_segment = true;
4744
4745 /* Build the mapping. */
4746 mfirst = NULL;
4747 pm = &mfirst;
4748
4749 /* If we have a .interp section, then create a PT_PHDR segment for
4750 the program headers and a PT_INTERP segment for the .interp
4751 section. */
4752 s = bfd_get_section_by_name (abfd, ".interp");
4753 if (s != NULL && (s->flags & SEC_LOAD) == 0)
4754 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4755 s = NULL;
4756 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4757 if (dynsec != NULL && (dynsec->flags & SEC_LOAD) == 0)
4758 dynsec = NULL;
4759
4760 if (s != NULL || dynsec != NULL)
4761 {
4762 amt = sizeof (struct elf_segment_map);
4763 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4764 if (m == NULL)
4765 goto error_return;
4766 m->next = NULL;
4767 m->p_type = PT_PHDR;
4768 m->p_flags = PF_R;
4769 m->p_flags_valid = 1;
4770 m->includes_phdrs = 1;
4771 phdr_in_segment = true;
4772 *pm = m;
4773 pm = &m->next;
4774 }
4775
4776 if (s != NULL)
4777 {
4778 amt = sizeof (struct elf_segment_map);
4779 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4780 if (m == NULL)
4781 goto error_return;
4782 m->next = NULL;
4783 m->p_type = PT_INTERP;
4784 m->count = 1;
4785 m->sections[0] = s;
4786
4787 *pm = m;
4788 pm = &m->next;
4789 }
4790
4791 /* Look through the sections. We put sections in the same program
4792 segment when the start of the second section can be placed within
4793 a few bytes of the end of the first section. */
4794 last_hdr = NULL;
4795 last_size = 0;
4796 hdr_index = 0;
4797 writable = false;
4798 executable = false;
4799
4800 if ((abfd->flags & D_PAGED) == 0)
4801 phdr_in_segment = false;
4802
4803 /* Deal with -Ttext or something similar such that the first section
4804 is not adjacent to the program headers. This is an
4805 approximation, since at this point we don't know exactly how many
4806 program headers we will need. */
4807 if (phdr_in_segment && count > 0)
4808 {
4809 bfd_vma phdr_lma; /* Bytes. */
4810 bool separate_phdr = false;
4811
4812 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
4813 if (info != NULL
4814 && info->separate_code
4815 && (sections[0]->flags & SEC_CODE) != 0)
4816 {
4817 /* If data sections should be separate from code and
4818 thus not executable, and the first section is
4819 executable then put the file and program headers in
4820 their own PT_LOAD. */
4821 separate_phdr = true;
4822 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
4823 == (sections[0]->lma & addr_mask & -maxpagesize)))
4824 {
4825 /* The file and program headers are currently on the
4826 same page as the first section. Put them on the
4827 previous page if we can. */
4828 if (phdr_lma >= maxpagesize)
4829 phdr_lma -= maxpagesize;
4830 else
4831 separate_phdr = false;
4832 }
4833 }
4834 if ((sections[0]->lma & addr_mask) < phdr_lma
4835 || (sections[0]->lma & addr_mask) < phdr_size)
4836 /* If file and program headers would be placed at the end
4837 of memory then it's probably better to omit them. */
4838 phdr_in_segment = false;
4839 else if (phdr_lma < wrap_to)
4840 /* If a section wraps around to where we'll be placing
4841 file and program headers, then the headers will be
4842 overwritten. */
4843 phdr_in_segment = false;
4844 else if (separate_phdr)
4845 {
4846 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
4847 if (m == NULL)
4848 goto error_return;
4849 m->p_paddr = phdr_lma * opb;
4850 m->p_vaddr_offset
4851 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
4852 m->p_paddr_valid = 1;
4853 *pm = m;
4854 pm = &m->next;
4855 phdr_in_segment = false;
4856 }
4857 }
4858
4859 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4860 {
4861 asection *hdr;
4862 bool new_segment;
4863
4864 hdr = *hdrpp;
4865
4866 /* See if this section and the last one will fit in the same
4867 segment. */
4868
4869 if (last_hdr == NULL)
4870 {
4871 /* If we don't have a segment yet, then we don't need a new
4872 one (we build the last one after this loop). */
4873 new_segment = false;
4874 }
4875 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4876 {
4877 /* If this section has a different relation between the
4878 virtual address and the load address, then we need a new
4879 segment. */
4880 new_segment = true;
4881 }
4882 else if (hdr->lma < last_hdr->lma + last_size
4883 || last_hdr->lma + last_size < last_hdr->lma)
4884 {
4885 /* If this section has a load address that makes it overlap
4886 the previous section, then we need a new segment. */
4887 new_segment = true;
4888 }
4889 else if ((abfd->flags & D_PAGED) != 0
4890 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4891 == (hdr->lma & -maxpagesize)))
4892 {
4893 /* If we are demand paged then we can't map two disk
4894 pages onto the same memory page. */
4895 new_segment = false;
4896 }
4897 /* In the next test we have to be careful when last_hdr->lma is close
4898 to the end of the address space. If the aligned address wraps
4899 around to the start of the address space, then there are no more
4900 pages left in memory and it is OK to assume that the current
4901 section can be included in the current segment. */
4902 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4903 + maxpagesize > last_hdr->lma)
4904 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4905 + maxpagesize <= hdr->lma))
4906 {
4907 /* If putting this section in this segment would force us to
4908 skip a page in the segment, then we need a new segment. */
4909 new_segment = true;
4910 }
4911 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4912 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4913 {
4914 /* We don't want to put a loaded section after a
4915 nonloaded (ie. bss style) section in the same segment
4916 as that will force the non-loaded section to be loaded.
4917 Consider .tbss sections as loaded for this purpose. */
4918 new_segment = true;
4919 }
4920 else if ((abfd->flags & D_PAGED) == 0)
4921 {
4922 /* If the file is not demand paged, which means that we
4923 don't require the sections to be correctly aligned in the
4924 file, then there is no other reason for a new segment. */
4925 new_segment = false;
4926 }
4927 else if (info != NULL
4928 && info->separate_code
4929 && executable != ((hdr->flags & SEC_CODE) != 0))
4930 {
4931 new_segment = true;
4932 }
4933 else if (! writable
4934 && (hdr->flags & SEC_READONLY) == 0)
4935 {
4936 /* We don't want to put a writable section in a read only
4937 segment. */
4938 new_segment = true;
4939 }
4940 else
4941 {
4942 /* Otherwise, we can use the same segment. */
4943 new_segment = false;
4944 }
4945
4946 /* Allow interested parties a chance to override our decision. */
4947 if (last_hdr != NULL
4948 && info != NULL
4949 && info->callbacks->override_segment_assignment != NULL)
4950 new_segment
4951 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4952 last_hdr,
4953 new_segment);
4954
4955 if (! new_segment)
4956 {
4957 if ((hdr->flags & SEC_READONLY) == 0)
4958 writable = true;
4959 if ((hdr->flags & SEC_CODE) != 0)
4960 executable = true;
4961 last_hdr = hdr;
4962 /* .tbss sections effectively have zero size. */
4963 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
4964 continue;
4965 }
4966
4967 /* We need a new program segment. We must create a new program
4968 header holding all the sections from hdr_index until hdr. */
4969
4970 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
4971 if (m == NULL)
4972 goto error_return;
4973
4974 *pm = m;
4975 pm = &m->next;
4976
4977 if ((hdr->flags & SEC_READONLY) == 0)
4978 writable = true;
4979 else
4980 writable = false;
4981
4982 if ((hdr->flags & SEC_CODE) == 0)
4983 executable = false;
4984 else
4985 executable = true;
4986
4987 last_hdr = hdr;
4988 /* .tbss sections effectively have zero size. */
4989 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
4990 hdr_index = i;
4991 phdr_in_segment = false;
4992 }
4993
4994 /* Create a final PT_LOAD program segment, but not if it's just
4995 for .tbss. */
4996 if (last_hdr != NULL
4997 && (i - hdr_index != 1
4998 || !IS_TBSS (last_hdr)))
4999 {
5000 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5001 if (m == NULL)
5002 goto error_return;
5003
5004 *pm = m;
5005 pm = &m->next;
5006 }
5007
5008 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5009 if (dynsec != NULL)
5010 {
5011 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5012 if (m == NULL)
5013 goto error_return;
5014 *pm = m;
5015 pm = &m->next;
5016 }
5017
5018 /* For each batch of consecutive loadable SHT_NOTE sections,
5019 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5020 because if we link together nonloadable .note sections and
5021 loadable .note sections, we will generate two .note sections
5022 in the output file. */
5023 for (s = abfd->sections; s != NULL; s = s->next)
5024 {
5025 if ((s->flags & SEC_LOAD) != 0
5026 && elf_section_type (s) == SHT_NOTE)
5027 {
5028 asection *s2;
5029 unsigned int alignment_power = s->alignment_power;
5030
5031 count = 1;
5032 for (s2 = s; s2->next != NULL; s2 = s2->next)
5033 {
5034 if (s2->next->alignment_power == alignment_power
5035 && (s2->next->flags & SEC_LOAD) != 0
5036 && elf_section_type (s2->next) == SHT_NOTE
5037 && align_power (s2->lma + s2->size / opb,
5038 alignment_power)
5039 == s2->next->lma)
5040 count++;
5041 else
5042 break;
5043 }
5044 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5045 amt += count * sizeof (asection *);
5046 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5047 if (m == NULL)
5048 goto error_return;
5049 m->next = NULL;
5050 m->p_type = PT_NOTE;
5051 m->count = count;
5052 while (count > 1)
5053 {
5054 m->sections[m->count - count--] = s;
5055 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5056 s = s->next;
5057 }
5058 m->sections[m->count - 1] = s;
5059 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5060 *pm = m;
5061 pm = &m->next;
5062 }
5063 if (s->flags & SEC_THREAD_LOCAL)
5064 {
5065 if (! tls_count)
5066 first_tls = s;
5067 tls_count++;
5068 }
5069 if (first_mbind == NULL
5070 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5071 first_mbind = s;
5072 }
5073
5074 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5075 if (tls_count > 0)
5076 {
5077 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5078 amt += tls_count * sizeof (asection *);
5079 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5080 if (m == NULL)
5081 goto error_return;
5082 m->next = NULL;
5083 m->p_type = PT_TLS;
5084 m->count = tls_count;
5085 /* Mandated PF_R. */
5086 m->p_flags = PF_R;
5087 m->p_flags_valid = 1;
5088 s = first_tls;
5089 for (i = 0; i < tls_count; ++i)
5090 {
5091 if ((s->flags & SEC_THREAD_LOCAL) == 0)
5092 {
5093 _bfd_error_handler
5094 (_("%pB: TLS sections are not adjacent:"), abfd);
5095 s = first_tls;
5096 i = 0;
5097 while (i < tls_count)
5098 {
5099 if ((s->flags & SEC_THREAD_LOCAL) != 0)
5100 {
5101 _bfd_error_handler (_(" TLS: %pA"), s);
5102 i++;
5103 }
5104 else
5105 _bfd_error_handler (_(" non-TLS: %pA"), s);
5106 s = s->next;
5107 }
5108 bfd_set_error (bfd_error_bad_value);
5109 goto error_return;
5110 }
5111 m->sections[i] = s;
5112 s = s->next;
5113 }
5114
5115 *pm = m;
5116 pm = &m->next;
5117 }
5118
5119 if (first_mbind
5120 && (abfd->flags & D_PAGED) != 0
5121 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5122 for (s = first_mbind; s != NULL; s = s->next)
5123 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5124 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5125 {
5126 /* Mandated PF_R. */
5127 unsigned long p_flags = PF_R;
5128 if ((s->flags & SEC_READONLY) == 0)
5129 p_flags |= PF_W;
5130 if ((s->flags & SEC_CODE) != 0)
5131 p_flags |= PF_X;
5132
5133 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5134 m = bfd_zalloc (abfd, amt);
5135 if (m == NULL)
5136 goto error_return;
5137 m->next = NULL;
5138 m->p_type = (PT_GNU_MBIND_LO
5139 + elf_section_data (s)->this_hdr.sh_info);
5140 m->count = 1;
5141 m->p_flags_valid = 1;
5142 m->sections[0] = s;
5143 m->p_flags = p_flags;
5144
5145 *pm = m;
5146 pm = &m->next;
5147 }
5148
5149 s = bfd_get_section_by_name (abfd,
5150 NOTE_GNU_PROPERTY_SECTION_NAME);
5151 if (s != NULL && s->size != 0)
5152 {
5153 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5154 m = bfd_zalloc (abfd, amt);
5155 if (m == NULL)
5156 goto error_return;
5157 m->next = NULL;
5158 m->p_type = PT_GNU_PROPERTY;
5159 m->count = 1;
5160 m->p_flags_valid = 1;
5161 m->sections[0] = s;
5162 m->p_flags = PF_R;
5163 *pm = m;
5164 pm = &m->next;
5165 }
5166
5167 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5168 segment. */
5169 eh_frame_hdr = elf_eh_frame_hdr (info);
5170 if (eh_frame_hdr != NULL
5171 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5172 {
5173 amt = sizeof (struct elf_segment_map);
5174 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5175 if (m == NULL)
5176 goto error_return;
5177 m->next = NULL;
5178 m->p_type = PT_GNU_EH_FRAME;
5179 m->count = 1;
5180 m->sections[0] = eh_frame_hdr->output_section;
5181
5182 *pm = m;
5183 pm = &m->next;
5184 }
5185
5186 if (elf_stack_flags (abfd))
5187 {
5188 amt = sizeof (struct elf_segment_map);
5189 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5190 if (m == NULL)
5191 goto error_return;
5192 m->next = NULL;
5193 m->p_type = PT_GNU_STACK;
5194 m->p_flags = elf_stack_flags (abfd);
5195 m->p_align = bed->stack_align;
5196 m->p_flags_valid = 1;
5197 m->p_align_valid = m->p_align != 0;
5198 if (info->stacksize > 0)
5199 {
5200 m->p_size = info->stacksize;
5201 m->p_size_valid = 1;
5202 }
5203
5204 *pm = m;
5205 pm = &m->next;
5206 }
5207
5208 if (info != NULL && info->relro)
5209 {
5210 for (m = mfirst; m != NULL; m = m->next)
5211 {
5212 if (m->p_type == PT_LOAD
5213 && m->count != 0
5214 && m->sections[0]->vma >= info->relro_start
5215 && m->sections[0]->vma < info->relro_end)
5216 {
5217 i = m->count;
5218 while (--i != (unsigned) -1)
5219 {
5220 if (m->sections[i]->size > 0
5221 && (m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5222 == (SEC_LOAD | SEC_HAS_CONTENTS))
5223 break;
5224 }
5225
5226 if (i != (unsigned) -1)
5227 break;
5228 }
5229 }
5230
5231 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5232 if (m != NULL)
5233 {
5234 amt = sizeof (struct elf_segment_map);
5235 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5236 if (m == NULL)
5237 goto error_return;
5238 m->next = NULL;
5239 m->p_type = PT_GNU_RELRO;
5240 *pm = m;
5241 pm = &m->next;
5242 }
5243 }
5244
5245 free (sections);
5246 elf_seg_map (abfd) = mfirst;
5247 }
5248
5249 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
5250 return false;
5251
5252 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5253 ++count;
5254 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5255
5256 return true;
5257
5258 error_return:
5259 free (sections);
5260 return false;
5261 }
5262
5263 /* Sort sections by address. */
5264
5265 static int
elf_sort_sections(const void * arg1,const void * arg2)5266 elf_sort_sections (const void *arg1, const void *arg2)
5267 {
5268 const asection *sec1 = *(const asection **) arg1;
5269 const asection *sec2 = *(const asection **) arg2;
5270 bfd_size_type size1, size2;
5271
5272 /* Sort by LMA first, since this is the address used to
5273 place the section into a segment. */
5274 if (sec1->lma < sec2->lma)
5275 return -1;
5276 else if (sec1->lma > sec2->lma)
5277 return 1;
5278
5279 /* Then sort by VMA. Normally the LMA and the VMA will be
5280 the same, and this will do nothing. */
5281 if (sec1->vma < sec2->vma)
5282 return -1;
5283 else if (sec1->vma > sec2->vma)
5284 return 1;
5285
5286 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5287
5288 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5289 && (x)->size != 0)
5290
5291 if (TOEND (sec1))
5292 {
5293 if (!TOEND (sec2))
5294 return 1;
5295 }
5296 else if (TOEND (sec2))
5297 return -1;
5298
5299 #undef TOEND
5300
5301 /* Sort by size, to put zero sized sections
5302 before others at the same address. */
5303
5304 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5305 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5306
5307 if (size1 < size2)
5308 return -1;
5309 if (size1 > size2)
5310 return 1;
5311
5312 return sec1->target_index - sec2->target_index;
5313 }
5314
5315 /* This qsort comparison functions sorts PT_LOAD segments first and
5316 by p_paddr, for assign_file_positions_for_load_sections. */
5317
5318 static int
elf_sort_segments(const void * arg1,const void * arg2)5319 elf_sort_segments (const void *arg1, const void *arg2)
5320 {
5321 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5322 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5323
5324 if (m1->p_type != m2->p_type)
5325 {
5326 if (m1->p_type == PT_NULL)
5327 return 1;
5328 if (m2->p_type == PT_NULL)
5329 return -1;
5330 return m1->p_type < m2->p_type ? -1 : 1;
5331 }
5332 if (m1->includes_filehdr != m2->includes_filehdr)
5333 return m1->includes_filehdr ? -1 : 1;
5334 if (m1->no_sort_lma != m2->no_sort_lma)
5335 return m1->no_sort_lma ? -1 : 1;
5336 if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5337 {
5338 bfd_vma lma1, lma2; /* Octets. */
5339 lma1 = 0;
5340 if (m1->p_paddr_valid)
5341 lma1 = m1->p_paddr;
5342 else if (m1->count != 0)
5343 {
5344 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5345 m1->sections[0]);
5346 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5347 }
5348 lma2 = 0;
5349 if (m2->p_paddr_valid)
5350 lma2 = m2->p_paddr;
5351 else if (m2->count != 0)
5352 {
5353 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5354 m2->sections[0]);
5355 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5356 }
5357 if (lma1 != lma2)
5358 return lma1 < lma2 ? -1 : 1;
5359 }
5360 if (m1->idx != m2->idx)
5361 return m1->idx < m2->idx ? -1 : 1;
5362 return 0;
5363 }
5364
5365 /* Ian Lance Taylor writes:
5366
5367 We shouldn't be using % with a negative signed number. That's just
5368 not good. We have to make sure either that the number is not
5369 negative, or that the number has an unsigned type. When the types
5370 are all the same size they wind up as unsigned. When file_ptr is a
5371 larger signed type, the arithmetic winds up as signed long long,
5372 which is wrong.
5373
5374 What we're trying to say here is something like ``increase OFF by
5375 the least amount that will cause it to be equal to the VMA modulo
5376 the page size.'' */
5377 /* In other words, something like:
5378
5379 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5380 off_offset = off % bed->maxpagesize;
5381 if (vma_offset < off_offset)
5382 adjustment = vma_offset + bed->maxpagesize - off_offset;
5383 else
5384 adjustment = vma_offset - off_offset;
5385
5386 which can be collapsed into the expression below. */
5387
5388 static file_ptr
vma_page_aligned_bias(bfd_vma vma,ufile_ptr off,bfd_vma maxpagesize)5389 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5390 {
5391 /* PR binutils/16199: Handle an alignment of zero. */
5392 if (maxpagesize == 0)
5393 maxpagesize = 1;
5394 return ((vma - off) % maxpagesize);
5395 }
5396
5397 static void
print_segment_map(const struct elf_segment_map * m)5398 print_segment_map (const struct elf_segment_map *m)
5399 {
5400 unsigned int j;
5401 const char *pt = get_segment_type (m->p_type);
5402 char buf[32];
5403
5404 if (pt == NULL)
5405 {
5406 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5407 sprintf (buf, "LOPROC+%7.7x",
5408 (unsigned int) (m->p_type - PT_LOPROC));
5409 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5410 sprintf (buf, "LOOS+%7.7x",
5411 (unsigned int) (m->p_type - PT_LOOS));
5412 else
5413 snprintf (buf, sizeof (buf), "%8.8x",
5414 (unsigned int) m->p_type);
5415 pt = buf;
5416 }
5417 fflush (stdout);
5418 fprintf (stderr, "%s:", pt);
5419 for (j = 0; j < m->count; j++)
5420 fprintf (stderr, " %s", m->sections [j]->name);
5421 putc ('\n',stderr);
5422 fflush (stderr);
5423 }
5424
5425 static bool
write_zeros(bfd * abfd,file_ptr pos,bfd_size_type len)5426 write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5427 {
5428 void *buf;
5429 bool ret;
5430
5431 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5432 return false;
5433 buf = bfd_zmalloc (len);
5434 if (buf == NULL)
5435 return false;
5436 ret = bfd_bwrite (buf, len, abfd) == len;
5437 free (buf);
5438 return ret;
5439 }
5440
5441 /* Assign file positions to the sections based on the mapping from
5442 sections to segments. This function also sets up some fields in
5443 the file header. */
5444
5445 static bool
assign_file_positions_for_load_sections(bfd * abfd,struct bfd_link_info * link_info)5446 assign_file_positions_for_load_sections (bfd *abfd,
5447 struct bfd_link_info *link_info)
5448 {
5449 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5450 struct elf_segment_map *m;
5451 struct elf_segment_map *phdr_load_seg;
5452 Elf_Internal_Phdr *phdrs;
5453 Elf_Internal_Phdr *p;
5454 file_ptr off; /* Octets. */
5455 bfd_size_type maxpagesize;
5456 bfd_size_type p_align;
5457 bool p_align_p = false;
5458 unsigned int alloc, actual;
5459 unsigned int i, j;
5460 struct elf_segment_map **sorted_seg_map;
5461 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5462
5463 if (link_info == NULL
5464 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5465 return false;
5466
5467 alloc = 0;
5468 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5469 m->idx = alloc++;
5470
5471 if (alloc)
5472 {
5473 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5474 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5475 }
5476 else
5477 {
5478 /* PR binutils/12467. */
5479 elf_elfheader (abfd)->e_phoff = 0;
5480 elf_elfheader (abfd)->e_phentsize = 0;
5481 }
5482
5483 elf_elfheader (abfd)->e_phnum = alloc;
5484
5485 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5486 {
5487 actual = alloc;
5488 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5489 }
5490 else
5491 {
5492 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5493 BFD_ASSERT (elf_program_header_size (abfd)
5494 == actual * bed->s->sizeof_phdr);
5495 BFD_ASSERT (actual >= alloc);
5496 }
5497
5498 if (alloc == 0)
5499 {
5500 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5501 return true;
5502 }
5503
5504 /* We're writing the size in elf_program_header_size (abfd),
5505 see assign_file_positions_except_relocs, so make sure we have
5506 that amount allocated, with trailing space cleared.
5507 The variable alloc contains the computed need, while
5508 elf_program_header_size (abfd) contains the size used for the
5509 layout.
5510 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5511 where the layout is forced to according to a larger size in the
5512 last iterations for the testcase ld-elf/header. */
5513 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5514 + alloc * sizeof (*sorted_seg_map)));
5515 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5516 elf_tdata (abfd)->phdr = phdrs;
5517 if (phdrs == NULL)
5518 return false;
5519
5520 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5521 {
5522 sorted_seg_map[j] = m;
5523 /* If elf_segment_map is not from map_sections_to_segments, the
5524 sections may not be correctly ordered. NOTE: sorting should
5525 not be done to the PT_NOTE section of a corefile, which may
5526 contain several pseudo-sections artificially created by bfd.
5527 Sorting these pseudo-sections breaks things badly. */
5528 if (m->count > 1
5529 && !(elf_elfheader (abfd)->e_type == ET_CORE
5530 && m->p_type == PT_NOTE))
5531 {
5532 for (i = 0; i < m->count; i++)
5533 m->sections[i]->target_index = i;
5534 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5535 elf_sort_sections);
5536 }
5537 }
5538 if (alloc > 1)
5539 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5540 elf_sort_segments);
5541
5542 p_align = bed->p_align;
5543 maxpagesize = 1;
5544 if ((abfd->flags & D_PAGED) != 0)
5545 {
5546 if (link_info != NULL)
5547 maxpagesize = link_info->maxpagesize;
5548 else
5549 maxpagesize = bed->maxpagesize;
5550 }
5551
5552 /* Sections must map to file offsets past the ELF file header. */
5553 off = bed->s->sizeof_ehdr;
5554 /* And if one of the PT_LOAD headers doesn't include the program
5555 headers then we'll be mapping program headers in the usual
5556 position after the ELF file header. */
5557 phdr_load_seg = NULL;
5558 for (j = 0; j < alloc; j++)
5559 {
5560 m = sorted_seg_map[j];
5561 if (m->p_type != PT_LOAD)
5562 break;
5563 if (m->includes_phdrs)
5564 {
5565 phdr_load_seg = m;
5566 break;
5567 }
5568 }
5569 if (phdr_load_seg == NULL)
5570 off += actual * bed->s->sizeof_phdr;
5571
5572 for (j = 0; j < alloc; j++)
5573 {
5574 asection **secpp;
5575 bfd_vma off_adjust; /* Octets. */
5576 bool no_contents;
5577
5578 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5579 number of sections with contents contributing to both p_filesz
5580 and p_memsz, followed by a number of sections with no contents
5581 that just contribute to p_memsz. In this loop, OFF tracks next
5582 available file offset for PT_LOAD and PT_NOTE segments. */
5583 m = sorted_seg_map[j];
5584 p = phdrs + m->idx;
5585 p->p_type = m->p_type;
5586 p->p_flags = m->p_flags;
5587
5588 if (m->count == 0)
5589 p->p_vaddr = m->p_vaddr_offset * opb;
5590 else
5591 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
5592
5593 if (m->p_paddr_valid)
5594 p->p_paddr = m->p_paddr;
5595 else if (m->count == 0)
5596 p->p_paddr = 0;
5597 else
5598 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
5599
5600 if (p->p_type == PT_LOAD
5601 && (abfd->flags & D_PAGED) != 0)
5602 {
5603 /* p_align in demand paged PT_LOAD segments effectively stores
5604 the maximum page size. When copying an executable with
5605 objcopy, we set m->p_align from the input file. Use this
5606 value for maxpagesize rather than bed->maxpagesize, which
5607 may be different. Note that we use maxpagesize for PT_TLS
5608 segment alignment later in this function, so we are relying
5609 on at least one PT_LOAD segment appearing before a PT_TLS
5610 segment. */
5611 if (m->p_align_valid)
5612 maxpagesize = m->p_align;
5613 else if (p_align != 0
5614 && (link_info == NULL
5615 || !link_info->maxpagesize_is_set))
5616 /* Set p_align to the default p_align value while laying
5617 out segments aligning to the maximum page size or the
5618 largest section alignment. The run-time loader can
5619 align segments to the default p_align value or the
5620 maximum page size, depending on system page size. */
5621 p_align_p = true;
5622
5623 p->p_align = maxpagesize;
5624 }
5625 else if (m->p_align_valid)
5626 p->p_align = m->p_align;
5627 else if (m->count == 0)
5628 p->p_align = 1 << bed->s->log_file_align;
5629
5630 if (m == phdr_load_seg)
5631 {
5632 if (!m->includes_filehdr)
5633 p->p_offset = off;
5634 off += actual * bed->s->sizeof_phdr;
5635 }
5636
5637 no_contents = false;
5638 off_adjust = 0;
5639 if (p->p_type == PT_LOAD
5640 && m->count > 0)
5641 {
5642 bfd_size_type align; /* Bytes. */
5643 unsigned int align_power = 0;
5644
5645 if (m->p_align_valid)
5646 align = p->p_align;
5647 else
5648 {
5649 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5650 {
5651 unsigned int secalign;
5652
5653 secalign = bfd_section_alignment (*secpp);
5654 if (secalign > align_power)
5655 align_power = secalign;
5656 }
5657 align = (bfd_size_type) 1 << align_power;
5658 if (align < maxpagesize)
5659 {
5660 /* If a section requires alignment higher than the
5661 default p_align value, don't set p_align to the
5662 default p_align value. */
5663 if (align > p_align)
5664 p_align_p = false;
5665 align = maxpagesize;
5666 }
5667 else
5668 {
5669 /* If a section requires alignment higher than the
5670 maximum page size, set p_align to the section
5671 alignment. */
5672 p_align_p = true;
5673 p_align = align;
5674 }
5675 }
5676
5677 for (i = 0; i < m->count; i++)
5678 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5679 /* If we aren't making room for this section, then
5680 it must be SHT_NOBITS regardless of what we've
5681 set via struct bfd_elf_special_section. */
5682 elf_section_type (m->sections[i]) = SHT_NOBITS;
5683
5684 /* Find out whether this segment contains any loadable
5685 sections. */
5686 no_contents = true;
5687 for (i = 0; i < m->count; i++)
5688 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5689 {
5690 no_contents = false;
5691 break;
5692 }
5693
5694 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
5695
5696 /* Broken hardware and/or kernel require that files do not
5697 map the same page with different permissions on some hppa
5698 processors. */
5699 if (j != 0
5700 && (abfd->flags & D_PAGED) != 0
5701 && bed->no_page_alias
5702 && (off & (maxpagesize - 1)) != 0
5703 && ((off & -maxpagesize)
5704 == ((off + off_adjust) & -maxpagesize)))
5705 off_adjust += maxpagesize;
5706 off += off_adjust;
5707 if (no_contents)
5708 {
5709 /* We shouldn't need to align the segment on disk since
5710 the segment doesn't need file space, but the gABI
5711 arguably requires the alignment and glibc ld.so
5712 checks it. So to comply with the alignment
5713 requirement but not waste file space, we adjust
5714 p_offset for just this segment. (OFF_ADJUST is
5715 subtracted from OFF later.) This may put p_offset
5716 past the end of file, but that shouldn't matter. */
5717 }
5718 else
5719 off_adjust = 0;
5720 }
5721 /* Make sure the .dynamic section is the first section in the
5722 PT_DYNAMIC segment. */
5723 else if (p->p_type == PT_DYNAMIC
5724 && m->count > 1
5725 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5726 {
5727 _bfd_error_handler
5728 (_("%pB: The first section in the PT_DYNAMIC segment"
5729 " is not the .dynamic section"),
5730 abfd);
5731 bfd_set_error (bfd_error_bad_value);
5732 return false;
5733 }
5734 /* Set the note section type to SHT_NOTE. */
5735 else if (p->p_type == PT_NOTE)
5736 for (i = 0; i < m->count; i++)
5737 elf_section_type (m->sections[i]) = SHT_NOTE;
5738
5739 if (m->includes_filehdr)
5740 {
5741 if (!m->p_flags_valid)
5742 p->p_flags |= PF_R;
5743 p->p_filesz = bed->s->sizeof_ehdr;
5744 p->p_memsz = bed->s->sizeof_ehdr;
5745 if (p->p_type == PT_LOAD)
5746 {
5747 if (m->count > 0)
5748 {
5749 if (p->p_vaddr < (bfd_vma) off
5750 || (!m->p_paddr_valid
5751 && p->p_paddr < (bfd_vma) off))
5752 {
5753 _bfd_error_handler
5754 (_("%pB: not enough room for program headers,"
5755 " try linking with -N"),
5756 abfd);
5757 bfd_set_error (bfd_error_bad_value);
5758 return false;
5759 }
5760 p->p_vaddr -= off;
5761 if (!m->p_paddr_valid)
5762 p->p_paddr -= off;
5763 }
5764 }
5765 else if (sorted_seg_map[0]->includes_filehdr)
5766 {
5767 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
5768 p->p_vaddr = filehdr->p_vaddr;
5769 if (!m->p_paddr_valid)
5770 p->p_paddr = filehdr->p_paddr;
5771 }
5772 }
5773
5774 if (m->includes_phdrs)
5775 {
5776 if (!m->p_flags_valid)
5777 p->p_flags |= PF_R;
5778 p->p_filesz += actual * bed->s->sizeof_phdr;
5779 p->p_memsz += actual * bed->s->sizeof_phdr;
5780 if (!m->includes_filehdr)
5781 {
5782 if (p->p_type == PT_LOAD)
5783 {
5784 elf_elfheader (abfd)->e_phoff = p->p_offset;
5785 if (m->count > 0)
5786 {
5787 p->p_vaddr -= off - p->p_offset;
5788 if (!m->p_paddr_valid)
5789 p->p_paddr -= off - p->p_offset;
5790 }
5791 }
5792 else if (phdr_load_seg != NULL)
5793 {
5794 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
5795 bfd_vma phdr_off = 0; /* Octets. */
5796 if (phdr_load_seg->includes_filehdr)
5797 phdr_off = bed->s->sizeof_ehdr;
5798 p->p_vaddr = phdr->p_vaddr + phdr_off;
5799 if (!m->p_paddr_valid)
5800 p->p_paddr = phdr->p_paddr + phdr_off;
5801 p->p_offset = phdr->p_offset + phdr_off;
5802 }
5803 else
5804 p->p_offset = bed->s->sizeof_ehdr;
5805 }
5806 }
5807
5808 if (p->p_type == PT_LOAD
5809 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5810 {
5811 if (!m->includes_filehdr && !m->includes_phdrs)
5812 {
5813 p->p_offset = off;
5814 if (no_contents)
5815 {
5816 /* Put meaningless p_offset for PT_LOAD segments
5817 without file contents somewhere within the first
5818 page, in an attempt to not point past EOF. */
5819 bfd_size_type align = maxpagesize;
5820 if (align < p->p_align)
5821 align = p->p_align;
5822 if (align < 1)
5823 align = 1;
5824 p->p_offset = off % align;
5825 }
5826 }
5827 else
5828 {
5829 file_ptr adjust; /* Octets. */
5830
5831 adjust = off - (p->p_offset + p->p_filesz);
5832 if (!no_contents)
5833 p->p_filesz += adjust;
5834 p->p_memsz += adjust;
5835 }
5836 }
5837
5838 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5839 maps. Set filepos for sections in PT_LOAD segments, and in
5840 core files, for sections in PT_NOTE segments.
5841 assign_file_positions_for_non_load_sections will set filepos
5842 for other sections and update p_filesz for other segments. */
5843 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5844 {
5845 asection *sec;
5846 bfd_size_type align;
5847 Elf_Internal_Shdr *this_hdr;
5848
5849 sec = *secpp;
5850 this_hdr = &elf_section_data (sec)->this_hdr;
5851 align = (bfd_size_type) 1 << bfd_section_alignment (sec);
5852
5853 if ((p->p_type == PT_LOAD
5854 || p->p_type == PT_TLS)
5855 && (this_hdr->sh_type != SHT_NOBITS
5856 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5857 && ((this_hdr->sh_flags & SHF_TLS) == 0
5858 || p->p_type == PT_TLS))))
5859 {
5860 bfd_vma p_start = p->p_paddr; /* Octets. */
5861 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */
5862 bfd_vma s_start = sec->lma * opb; /* Octets. */
5863 bfd_vma adjust = s_start - p_end; /* Octets. */
5864
5865 if (adjust != 0
5866 && (s_start < p_end
5867 || p_end < p_start))
5868 {
5869 _bfd_error_handler
5870 /* xgettext:c-format */
5871 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
5872 abfd, sec, (uint64_t) s_start / opb,
5873 (uint64_t) p_end / opb);
5874 adjust = 0;
5875 sec->lma = p_end / opb;
5876 }
5877 p->p_memsz += adjust;
5878
5879 if (p->p_type == PT_LOAD)
5880 {
5881 if (this_hdr->sh_type != SHT_NOBITS)
5882 {
5883 off_adjust = 0;
5884 if (p->p_filesz + adjust < p->p_memsz)
5885 {
5886 /* We have a PROGBITS section following NOBITS ones.
5887 Allocate file space for the NOBITS section(s) and
5888 zero it. */
5889 adjust = p->p_memsz - p->p_filesz;
5890 if (!write_zeros (abfd, off, adjust))
5891 return false;
5892 }
5893 }
5894 /* We only adjust sh_offset in SHT_NOBITS sections
5895 as would seem proper for their address when the
5896 section is first in the segment. sh_offset
5897 doesn't really have any significance for
5898 SHT_NOBITS anyway, apart from a notional position
5899 relative to other sections. Historically we
5900 didn't bother with adjusting sh_offset and some
5901 programs depend on it not being adjusted. See
5902 pr12921 and pr25662. */
5903 if (this_hdr->sh_type != SHT_NOBITS || i == 0)
5904 {
5905 off += adjust;
5906 if (this_hdr->sh_type == SHT_NOBITS)
5907 off_adjust += adjust;
5908 }
5909 }
5910 if (this_hdr->sh_type != SHT_NOBITS)
5911 p->p_filesz += adjust;
5912 }
5913
5914 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5915 {
5916 /* The section at i == 0 is the one that actually contains
5917 everything. */
5918 if (i == 0)
5919 {
5920 this_hdr->sh_offset = sec->filepos = off;
5921 off += this_hdr->sh_size;
5922 p->p_filesz = this_hdr->sh_size;
5923 p->p_memsz = 0;
5924 p->p_align = 1;
5925 }
5926 else
5927 {
5928 /* The rest are fake sections that shouldn't be written. */
5929 sec->filepos = 0;
5930 sec->size = 0;
5931 sec->flags = 0;
5932 continue;
5933 }
5934 }
5935 else
5936 {
5937 if (p->p_type == PT_LOAD)
5938 {
5939 this_hdr->sh_offset = sec->filepos = off;
5940 if (this_hdr->sh_type != SHT_NOBITS)
5941 off += this_hdr->sh_size;
5942 }
5943 else if (this_hdr->sh_type == SHT_NOBITS
5944 && (this_hdr->sh_flags & SHF_TLS) != 0
5945 && this_hdr->sh_offset == 0)
5946 {
5947 /* This is a .tbss section that didn't get a PT_LOAD.
5948 (See _bfd_elf_map_sections_to_segments "Create a
5949 final PT_LOAD".) Set sh_offset to the value it
5950 would have if we had created a zero p_filesz and
5951 p_memsz PT_LOAD header for the section. This
5952 also makes the PT_TLS header have the same
5953 p_offset value. */
5954 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5955 off, align);
5956 this_hdr->sh_offset = sec->filepos = off + adjust;
5957 }
5958
5959 if (this_hdr->sh_type != SHT_NOBITS)
5960 {
5961 p->p_filesz += this_hdr->sh_size;
5962 /* A load section without SHF_ALLOC is something like
5963 a note section in a PT_NOTE segment. These take
5964 file space but are not loaded into memory. */
5965 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5966 p->p_memsz += this_hdr->sh_size;
5967 }
5968 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5969 {
5970 if (p->p_type == PT_TLS)
5971 p->p_memsz += this_hdr->sh_size;
5972
5973 /* .tbss is special. It doesn't contribute to p_memsz of
5974 normal segments. */
5975 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5976 p->p_memsz += this_hdr->sh_size;
5977 }
5978
5979 if (align > p->p_align
5980 && !m->p_align_valid
5981 && (p->p_type != PT_LOAD
5982 || (abfd->flags & D_PAGED) == 0))
5983 p->p_align = align;
5984 }
5985
5986 if (!m->p_flags_valid)
5987 {
5988 p->p_flags |= PF_R;
5989 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5990 p->p_flags |= PF_X;
5991 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5992 p->p_flags |= PF_W;
5993 }
5994 }
5995
5996 off -= off_adjust;
5997
5998 /* PR ld/20815 - Check that the program header segment, if
5999 present, will be loaded into memory. */
6000 if (p->p_type == PT_PHDR
6001 && phdr_load_seg == NULL
6002 && !(bed->elf_backend_allow_non_load_phdr != NULL
6003 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6004 {
6005 /* The fix for this error is usually to edit the linker script being
6006 used and set up the program headers manually. Either that or
6007 leave room for the headers at the start of the SECTIONS. */
6008 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6009 " by LOAD segment"),
6010 abfd);
6011 if (link_info == NULL)
6012 return false;
6013 /* Arrange for the linker to exit with an error, deleting
6014 the output file unless --noinhibit-exec is given. */
6015 link_info->callbacks->info ("%X");
6016 }
6017
6018 /* Check that all sections are in a PT_LOAD segment.
6019 Don't check funky gdb generated core files. */
6020 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6021 {
6022 bool check_vma = true;
6023
6024 for (i = 1; i < m->count; i++)
6025 if (m->sections[i]->vma == m->sections[i - 1]->vma
6026 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6027 ->this_hdr), p) != 0
6028 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6029 ->this_hdr), p) != 0)
6030 {
6031 /* Looks like we have overlays packed into the segment. */
6032 check_vma = false;
6033 break;
6034 }
6035
6036 for (i = 0; i < m->count; i++)
6037 {
6038 Elf_Internal_Shdr *this_hdr;
6039 asection *sec;
6040
6041 sec = m->sections[i];
6042 this_hdr = &(elf_section_data(sec)->this_hdr);
6043 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6044 && !ELF_TBSS_SPECIAL (this_hdr, p))
6045 {
6046 _bfd_error_handler
6047 /* xgettext:c-format */
6048 (_("%pB: section `%pA' can't be allocated in segment %d"),
6049 abfd, sec, j);
6050 print_segment_map (m);
6051 }
6052 }
6053
6054 if (p_align_p)
6055 p->p_align = p_align;
6056 }
6057 }
6058
6059 elf_next_file_pos (abfd) = off;
6060
6061 if (link_info != NULL
6062 && phdr_load_seg != NULL
6063 && phdr_load_seg->includes_filehdr)
6064 {
6065 /* There is a segment that contains both the file headers and the
6066 program headers, so provide a symbol __ehdr_start pointing there.
6067 A program can use this to examine itself robustly. */
6068
6069 struct elf_link_hash_entry *hash
6070 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
6071 false, false, true);
6072 /* If the symbol was referenced and not defined, define it. */
6073 if (hash != NULL
6074 && (hash->root.type == bfd_link_hash_new
6075 || hash->root.type == bfd_link_hash_undefined
6076 || hash->root.type == bfd_link_hash_undefweak
6077 || hash->root.type == bfd_link_hash_common))
6078 {
6079 asection *s = NULL;
6080 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6081
6082 if (phdr_load_seg->count != 0)
6083 /* The segment contains sections, so use the first one. */
6084 s = phdr_load_seg->sections[0];
6085 else
6086 /* Use the first (i.e. lowest-addressed) section in any segment. */
6087 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6088 if (m->p_type == PT_LOAD && m->count != 0)
6089 {
6090 s = m->sections[0];
6091 break;
6092 }
6093
6094 if (s != NULL)
6095 {
6096 hash->root.u.def.value = filehdr_vaddr - s->vma;
6097 hash->root.u.def.section = s;
6098 }
6099 else
6100 {
6101 hash->root.u.def.value = filehdr_vaddr;
6102 hash->root.u.def.section = bfd_abs_section_ptr;
6103 }
6104
6105 hash->root.type = bfd_link_hash_defined;
6106 hash->def_regular = 1;
6107 hash->non_elf = 0;
6108 }
6109 }
6110
6111 return true;
6112 }
6113
6114 /* Determine if a bfd is a debuginfo file. Unfortunately there
6115 is no defined method for detecting such files, so we have to
6116 use heuristics instead. */
6117
6118 bool
is_debuginfo_file(bfd * abfd)6119 is_debuginfo_file (bfd *abfd)
6120 {
6121 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6122 return false;
6123
6124 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6125 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6126 Elf_Internal_Shdr **headerp;
6127
6128 for (headerp = start_headers; headerp < end_headers; headerp ++)
6129 {
6130 Elf_Internal_Shdr *header = * headerp;
6131
6132 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6133 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6134 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6135 && header->sh_type != SHT_NOBITS
6136 && header->sh_type != SHT_NOTE)
6137 return false;
6138 }
6139
6140 return true;
6141 }
6142
6143 /* Assign file positions for the other sections, except for compressed debugging
6144 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
6145
6146 static bool
assign_file_positions_for_non_load_sections(bfd * abfd,struct bfd_link_info * link_info)6147 assign_file_positions_for_non_load_sections (bfd *abfd,
6148 struct bfd_link_info *link_info)
6149 {
6150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6151 Elf_Internal_Shdr **i_shdrpp;
6152 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6153 Elf_Internal_Phdr *phdrs;
6154 Elf_Internal_Phdr *p;
6155 struct elf_segment_map *m;
6156 file_ptr off;
6157 unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6158 bfd_vma maxpagesize;
6159
6160 if (link_info != NULL)
6161 maxpagesize = link_info->maxpagesize;
6162 else
6163 maxpagesize = bed->maxpagesize;
6164 i_shdrpp = elf_elfsections (abfd);
6165 end_hdrpp = i_shdrpp + elf_numsections (abfd);
6166 off = elf_next_file_pos (abfd);
6167 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6168 {
6169 Elf_Internal_Shdr *hdr;
6170 bfd_vma align;
6171
6172 hdr = *hdrpp;
6173 if (hdr->bfd_section != NULL
6174 && (hdr->bfd_section->filepos != 0
6175 || (hdr->sh_type == SHT_NOBITS
6176 && hdr->contents == NULL)))
6177 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6178 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6179 {
6180 if (hdr->sh_size != 0
6181 /* PR 24717 - debuginfo files are known to be not strictly
6182 compliant with the ELF standard. In particular they often
6183 have .note.gnu.property sections that are outside of any
6184 loadable segment. This is not a problem for such files,
6185 so do not warn about them. */
6186 && ! is_debuginfo_file (abfd))
6187 _bfd_error_handler
6188 /* xgettext:c-format */
6189 (_("%pB: warning: allocated section `%s' not in segment"),
6190 abfd,
6191 (hdr->bfd_section == NULL
6192 ? "*unknown*"
6193 : hdr->bfd_section->name));
6194 /* We don't need to page align empty sections. */
6195 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6196 align = maxpagesize;
6197 else
6198 align = hdr->sh_addralign & -hdr->sh_addralign;
6199 off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6200 off = _bfd_elf_assign_file_position_for_section (hdr, off,
6201 false);
6202 }
6203 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6204 && hdr->bfd_section == NULL)
6205 /* We don't know the offset of these sections yet: their size has
6206 not been decided. */
6207 || (hdr->bfd_section != NULL
6208 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6209 || (bfd_section_is_ctf (hdr->bfd_section)
6210 && abfd->is_linker_output)))
6211 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6212 || (elf_symtab_shndx_list (abfd) != NULL
6213 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6214 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6215 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6216 hdr->sh_offset = -1;
6217 else
6218 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6219 }
6220 elf_next_file_pos (abfd) = off;
6221
6222 /* Now that we have set the section file positions, we can set up
6223 the file positions for the non PT_LOAD segments. */
6224 phdrs = elf_tdata (abfd)->phdr;
6225 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6226 {
6227 if (p->p_type == PT_GNU_RELRO)
6228 {
6229 bfd_vma start, end; /* Bytes. */
6230 bool ok;
6231
6232 if (link_info != NULL)
6233 {
6234 /* During linking the range of the RELRO segment is passed
6235 in link_info. Note that there may be padding between
6236 relro_start and the first RELRO section. */
6237 start = link_info->relro_start;
6238 end = link_info->relro_end;
6239 }
6240 else if (m->count != 0)
6241 {
6242 if (!m->p_size_valid)
6243 abort ();
6244 start = m->sections[0]->vma;
6245 end = start + m->p_size / opb;
6246 }
6247 else
6248 {
6249 start = 0;
6250 end = 0;
6251 }
6252
6253 ok = false;
6254 if (start < end)
6255 {
6256 struct elf_segment_map *lm;
6257 const Elf_Internal_Phdr *lp;
6258 unsigned int i;
6259
6260 /* Find a LOAD segment containing a section in the RELRO
6261 segment. */
6262 for (lm = elf_seg_map (abfd), lp = phdrs;
6263 lm != NULL;
6264 lm = lm->next, lp++)
6265 {
6266 if (lp->p_type == PT_LOAD
6267 && lm->count != 0
6268 && (lm->sections[lm->count - 1]->vma
6269 + (!IS_TBSS (lm->sections[lm->count - 1])
6270 ? lm->sections[lm->count - 1]->size / opb
6271 : 0)) > start
6272 && lm->sections[0]->vma < end)
6273 break;
6274 }
6275
6276 if (lm != NULL)
6277 {
6278 /* Find the section starting the RELRO segment. */
6279 for (i = 0; i < lm->count; i++)
6280 {
6281 asection *s = lm->sections[i];
6282 if (s->vma >= start
6283 && s->vma < end
6284 && s->size != 0)
6285 break;
6286 }
6287
6288 if (i < lm->count)
6289 {
6290 p->p_vaddr = lm->sections[i]->vma * opb;
6291 p->p_paddr = lm->sections[i]->lma * opb;
6292 p->p_offset = lm->sections[i]->filepos;
6293 p->p_memsz = end * opb - p->p_vaddr;
6294 p->p_filesz = p->p_memsz;
6295
6296 /* The RELRO segment typically ends a few bytes
6297 into .got.plt but other layouts are possible.
6298 In cases where the end does not match any
6299 loaded section (for instance is in file
6300 padding), trim p_filesz back to correspond to
6301 the end of loaded section contents. */
6302 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6303 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6304
6305 /* Preserve the alignment and flags if they are
6306 valid. The gold linker generates RW/4 for
6307 the PT_GNU_RELRO section. It is better for
6308 objcopy/strip to honor these attributes
6309 otherwise gdb will choke when using separate
6310 debug files. */
6311 if (!m->p_align_valid)
6312 p->p_align = 1;
6313 if (!m->p_flags_valid)
6314 p->p_flags = PF_R;
6315 ok = true;
6316 }
6317 }
6318 }
6319
6320 if (!ok)
6321 {
6322 if (link_info != NULL)
6323 _bfd_error_handler
6324 (_("%pB: warning: unable to allocate any sections to PT_GNU_RELRO segment"),
6325 abfd);
6326 memset (p, 0, sizeof *p);
6327 }
6328 }
6329 else if (p->p_type == PT_GNU_STACK)
6330 {
6331 if (m->p_size_valid)
6332 p->p_memsz = m->p_size;
6333 }
6334 else if (m->count != 0)
6335 {
6336 unsigned int i;
6337
6338 if (p->p_type != PT_LOAD
6339 && (p->p_type != PT_NOTE
6340 || bfd_get_format (abfd) != bfd_core))
6341 {
6342 /* A user specified segment layout may include a PHDR
6343 segment that overlaps with a LOAD segment... */
6344 if (p->p_type == PT_PHDR)
6345 {
6346 m->count = 0;
6347 continue;
6348 }
6349
6350 if (m->includes_filehdr || m->includes_phdrs)
6351 {
6352 /* PR 17512: file: 2195325e. */
6353 _bfd_error_handler
6354 (_("%pB: error: non-load segment %d includes file header "
6355 "and/or program header"),
6356 abfd, (int) (p - phdrs));
6357 return false;
6358 }
6359
6360 p->p_filesz = 0;
6361 p->p_offset = m->sections[0]->filepos;
6362 for (i = m->count; i-- != 0;)
6363 {
6364 asection *sect = m->sections[i];
6365 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6366 if (hdr->sh_type != SHT_NOBITS)
6367 {
6368 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6369 /* NB: p_memsz of the loadable PT_NOTE segment
6370 should be the same as p_filesz. */
6371 if (p->p_type == PT_NOTE
6372 && (hdr->sh_flags & SHF_ALLOC) != 0)
6373 p->p_memsz = p->p_filesz;
6374 break;
6375 }
6376 }
6377 }
6378 }
6379 }
6380
6381 return true;
6382 }
6383
6384 static elf_section_list *
find_section_in_list(unsigned int i,elf_section_list * list)6385 find_section_in_list (unsigned int i, elf_section_list * list)
6386 {
6387 for (;list != NULL; list = list->next)
6388 if (list->ndx == i)
6389 break;
6390 return list;
6391 }
6392
6393 /* Work out the file positions of all the sections. This is called by
6394 _bfd_elf_compute_section_file_positions. All the section sizes and
6395 VMAs must be known before this is called.
6396
6397 Reloc sections come in two flavours: Those processed specially as
6398 "side-channel" data attached to a section to which they apply, and those that
6399 bfd doesn't process as relocations. The latter sort are stored in a normal
6400 bfd section by bfd_section_from_shdr. We don't consider the former sort
6401 here, unless they form part of the loadable image. Reloc sections not
6402 assigned here (and compressed debugging sections and CTF sections which
6403 nothing else in the file can rely upon) will be handled later by
6404 assign_file_positions_for_relocs.
6405
6406 We also don't set the positions of the .symtab and .strtab here. */
6407
6408 static bool
assign_file_positions_except_relocs(bfd * abfd,struct bfd_link_info * link_info)6409 assign_file_positions_except_relocs (bfd *abfd,
6410 struct bfd_link_info *link_info)
6411 {
6412 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6413 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6414 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6415 unsigned int alloc;
6416
6417 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6418 && bfd_get_format (abfd) != bfd_core)
6419 {
6420 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6421 unsigned int num_sec = elf_numsections (abfd);
6422 Elf_Internal_Shdr **hdrpp;
6423 unsigned int i;
6424 file_ptr off;
6425
6426 /* Start after the ELF header. */
6427 off = i_ehdrp->e_ehsize;
6428
6429 /* We are not creating an executable, which means that we are
6430 not creating a program header, and that the actual order of
6431 the sections in the file is unimportant. */
6432 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6433 {
6434 Elf_Internal_Shdr *hdr;
6435
6436 hdr = *hdrpp;
6437 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6438 && hdr->bfd_section == NULL)
6439 /* Do not assign offsets for these sections yet: we don't know
6440 their sizes. */
6441 || (hdr->bfd_section != NULL
6442 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS
6443 || (bfd_section_is_ctf (hdr->bfd_section)
6444 && abfd->is_linker_output)))
6445 || i == elf_onesymtab (abfd)
6446 || (elf_symtab_shndx_list (abfd) != NULL
6447 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6448 || i == elf_strtab_sec (abfd)
6449 || i == elf_shstrtab_sec (abfd))
6450 {
6451 hdr->sh_offset = -1;
6452 }
6453 else
6454 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
6455 }
6456
6457 elf_next_file_pos (abfd) = off;
6458 elf_program_header_size (abfd) = 0;
6459 }
6460 else
6461 {
6462 /* Assign file positions for the loaded sections based on the
6463 assignment of sections to segments. */
6464 if (!assign_file_positions_for_load_sections (abfd, link_info))
6465 return false;
6466
6467 /* And for non-load sections. */
6468 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6469 return false;
6470 }
6471
6472 if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6473 return false;
6474
6475 /* Write out the program headers. */
6476 alloc = i_ehdrp->e_phnum;
6477 if (alloc != 0)
6478 {
6479 if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6480 {
6481 /* Memory resident segments with non-zero size and RWX permissions are a
6482 security risk, so we generate a warning here if we are creating any. */
6483 unsigned int i;
6484
6485 for (i = 0; i < alloc; i++)
6486 {
6487 const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6488
6489 if (phdr->p_memsz == 0)
6490 continue;
6491
6492 if (phdr->p_type == PT_TLS && (phdr->p_flags & PF_X))
6493 _bfd_error_handler (_("warning: %pB has a TLS segment with execute permission"),
6494 abfd);
6495 else if (phdr->p_type == PT_LOAD
6496 && (phdr->p_flags & (PF_R | PF_W | PF_X)) == (PF_R | PF_W | PF_X))
6497 _bfd_error_handler (_("warning: %pB has a LOAD segment with RWX permissions"),
6498 abfd);
6499 }
6500 }
6501
6502 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6503 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6504 return false;
6505 }
6506
6507 return true;
6508 }
6509
6510 bool
_bfd_elf_init_file_header(bfd * abfd,struct bfd_link_info * info ATTRIBUTE_UNUSED)6511 _bfd_elf_init_file_header (bfd *abfd,
6512 struct bfd_link_info *info ATTRIBUTE_UNUSED)
6513 {
6514 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
6515 struct elf_strtab_hash *shstrtab;
6516 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6517
6518 i_ehdrp = elf_elfheader (abfd);
6519
6520 shstrtab = _bfd_elf_strtab_init ();
6521 if (shstrtab == NULL)
6522 return false;
6523
6524 elf_shstrtab (abfd) = shstrtab;
6525
6526 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6527 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6528 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6529 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6530
6531 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6532 i_ehdrp->e_ident[EI_DATA] =
6533 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6534 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6535
6536 if ((abfd->flags & DYNAMIC) != 0)
6537 i_ehdrp->e_type = ET_DYN;
6538 else if ((abfd->flags & EXEC_P) != 0)
6539 i_ehdrp->e_type = ET_EXEC;
6540 else if (bfd_get_format (abfd) == bfd_core)
6541 i_ehdrp->e_type = ET_CORE;
6542 else
6543 i_ehdrp->e_type = ET_REL;
6544
6545 switch (bfd_get_arch (abfd))
6546 {
6547 case bfd_arch_unknown:
6548 i_ehdrp->e_machine = EM_NONE;
6549 break;
6550
6551 /* There used to be a long list of cases here, each one setting
6552 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6553 in the corresponding bfd definition. To avoid duplication,
6554 the switch was removed. Machines that need special handling
6555 can generally do it in elf_backend_final_write_processing(),
6556 unless they need the information earlier than the final write.
6557 Such need can generally be supplied by replacing the tests for
6558 e_machine with the conditions used to determine it. */
6559 default:
6560 i_ehdrp->e_machine = bed->elf_machine_code;
6561 }
6562
6563 i_ehdrp->e_version = bed->s->ev_current;
6564 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6565
6566 /* No program header, for now. */
6567 i_ehdrp->e_phoff = 0;
6568 i_ehdrp->e_phentsize = 0;
6569 i_ehdrp->e_phnum = 0;
6570
6571 /* Each bfd section is section header entry. */
6572 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6573 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6574
6575 elf_tdata (abfd)->symtab_hdr.sh_name =
6576 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
6577 elf_tdata (abfd)->strtab_hdr.sh_name =
6578 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
6579 elf_tdata (abfd)->shstrtab_hdr.sh_name =
6580 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
6581 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
6582 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
6583 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
6584 return false;
6585
6586 return true;
6587 }
6588
6589 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6590
6591 FIXME: We used to have code here to sort the PT_LOAD segments into
6592 ascending order, as per the ELF spec. But this breaks some programs,
6593 including the Linux kernel. But really either the spec should be
6594 changed or the programs updated. */
6595
6596 bool
_bfd_elf_modify_headers(bfd * obfd,struct bfd_link_info * link_info)6597 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
6598 {
6599 if (link_info != NULL && bfd_link_pie (link_info))
6600 {
6601 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
6602 unsigned int num_segments = i_ehdrp->e_phnum;
6603 struct elf_obj_tdata *tdata = elf_tdata (obfd);
6604 Elf_Internal_Phdr *segment = tdata->phdr;
6605 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6606
6607 /* Find the lowest p_vaddr in PT_LOAD segments. */
6608 bfd_vma p_vaddr = (bfd_vma) -1;
6609 for (; segment < end_segment; segment++)
6610 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6611 p_vaddr = segment->p_vaddr;
6612
6613 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6614 segments is non-zero. */
6615 if (p_vaddr)
6616 i_ehdrp->e_type = ET_EXEC;
6617 }
6618 return true;
6619 }
6620
6621 /* Assign file positions for all the reloc sections which are not part
6622 of the loadable file image, and the file position of section headers. */
6623
6624 static bool
_bfd_elf_assign_file_positions_for_non_load(bfd * abfd)6625 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
6626 {
6627 file_ptr off;
6628 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
6629 Elf_Internal_Shdr *shdrp;
6630 Elf_Internal_Ehdr *i_ehdrp;
6631 const struct elf_backend_data *bed;
6632
6633 off = elf_next_file_pos (abfd);
6634
6635 shdrpp = elf_elfsections (abfd);
6636 end_shdrpp = shdrpp + elf_numsections (abfd);
6637 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
6638 {
6639 shdrp = *shdrpp;
6640 if (shdrp->sh_offset == -1)
6641 {
6642 asection *sec = shdrp->bfd_section;
6643 bool is_rel = (shdrp->sh_type == SHT_REL
6644 || shdrp->sh_type == SHT_RELA);
6645 bool is_ctf = sec && bfd_section_is_ctf (sec);
6646 if (is_rel
6647 || is_ctf
6648 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
6649 {
6650 if (!is_rel && !is_ctf)
6651 {
6652 const char *name = sec->name;
6653 struct bfd_elf_section_data *d;
6654
6655 /* Compress DWARF debug sections. */
6656 if (!bfd_compress_section (abfd, sec,
6657 shdrp->contents))
6658 return false;
6659
6660 if (sec->compress_status == COMPRESS_SECTION_DONE
6661 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6662 {
6663 /* If section is compressed with zlib-gnu, convert
6664 section name from .debug_* to .zdebug_*. */
6665 char *new_name
6666 = convert_debug_to_zdebug (abfd, name);
6667 if (new_name == NULL)
6668 return false;
6669 name = new_name;
6670 }
6671 /* Add section name to section name section. */
6672 if (shdrp->sh_name != (unsigned int) -1)
6673 abort ();
6674 shdrp->sh_name
6675 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6676 name, false);
6677 d = elf_section_data (sec);
6678
6679 /* Add reloc section name to section name section. */
6680 if (d->rel.hdr
6681 && !_bfd_elf_set_reloc_sh_name (abfd,
6682 d->rel.hdr,
6683 name, false))
6684 return false;
6685 if (d->rela.hdr
6686 && !_bfd_elf_set_reloc_sh_name (abfd,
6687 d->rela.hdr,
6688 name, true))
6689 return false;
6690
6691 /* Update section size and contents. */
6692 shdrp->sh_size = sec->size;
6693 shdrp->contents = sec->contents;
6694 shdrp->bfd_section->contents = NULL;
6695 }
6696 else if (is_ctf)
6697 {
6698 /* Update section size and contents. */
6699 shdrp->sh_size = sec->size;
6700 shdrp->contents = sec->contents;
6701 }
6702
6703 off = _bfd_elf_assign_file_position_for_section (shdrp,
6704 off,
6705 true);
6706 }
6707 }
6708 }
6709
6710 /* Place section name section after DWARF debug sections have been
6711 compressed. */
6712 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6713 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6714 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6715 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
6716
6717 /* Place the section headers. */
6718 i_ehdrp = elf_elfheader (abfd);
6719 bed = get_elf_backend_data (abfd);
6720 off = align_file_position (off, 1 << bed->s->log_file_align);
6721 i_ehdrp->e_shoff = off;
6722 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
6723 elf_next_file_pos (abfd) = off;
6724
6725 return true;
6726 }
6727
6728 bool
_bfd_elf_write_object_contents(bfd * abfd)6729 _bfd_elf_write_object_contents (bfd *abfd)
6730 {
6731 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6732 Elf_Internal_Shdr **i_shdrp;
6733 bool failed;
6734 unsigned int count, num_sec;
6735 struct elf_obj_tdata *t;
6736
6737 if (! abfd->output_has_begun
6738 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6739 return false;
6740 /* Do not rewrite ELF data when the BFD has been opened for update.
6741 abfd->output_has_begun was set to TRUE on opening, so creation of new
6742 sections, and modification of existing section sizes was restricted.
6743 This means the ELF header, program headers and section headers can't have
6744 changed.
6745 If the contents of any sections has been modified, then those changes have
6746 already been written to the BFD. */
6747 else if (abfd->direction == both_direction)
6748 {
6749 BFD_ASSERT (abfd->output_has_begun);
6750 return true;
6751 }
6752
6753 i_shdrp = elf_elfsections (abfd);
6754
6755 failed = false;
6756 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6757 if (failed)
6758 return false;
6759
6760 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6761 return false;
6762
6763 /* After writing the headers, we need to write the sections too... */
6764 num_sec = elf_numsections (abfd);
6765 for (count = 1; count < num_sec; count++)
6766 {
6767 i_shdrp[count]->sh_name
6768 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6769 i_shdrp[count]->sh_name);
6770 if (bed->elf_backend_section_processing)
6771 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6772 return false;
6773 if (i_shdrp[count]->contents)
6774 {
6775 bfd_size_type amt = i_shdrp[count]->sh_size;
6776
6777 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
6778 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
6779 return false;
6780 }
6781 }
6782
6783 /* Write out the section header names. */
6784 t = elf_tdata (abfd);
6785 if (elf_shstrtab (abfd) != NULL
6786 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
6787 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
6788 return false;
6789
6790 if (!(*bed->elf_backend_final_write_processing) (abfd))
6791 return false;
6792
6793 if (!bed->s->write_shdrs_and_ehdr (abfd))
6794 return false;
6795
6796 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6797 if (t->o->build_id.after_write_object_contents != NULL
6798 && !(*t->o->build_id.after_write_object_contents) (abfd))
6799 return false;
6800 if (t->o->package_metadata.after_write_object_contents != NULL
6801 && !(*t->o->package_metadata.after_write_object_contents) (abfd))
6802 return false;
6803
6804 return true;
6805 }
6806
6807 bool
_bfd_elf_write_corefile_contents(bfd * abfd)6808 _bfd_elf_write_corefile_contents (bfd *abfd)
6809 {
6810 /* Hopefully this can be done just like an object file. */
6811 return _bfd_elf_write_object_contents (abfd);
6812 }
6813
6814 /* Given a section, search the header to find them. */
6815
6816 unsigned int
_bfd_elf_section_from_bfd_section(bfd * abfd,struct bfd_section * asect)6817 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
6818 {
6819 const struct elf_backend_data *bed;
6820 unsigned int sec_index;
6821
6822 if (elf_section_data (asect) != NULL
6823 && elf_section_data (asect)->this_idx != 0)
6824 return elf_section_data (asect)->this_idx;
6825
6826 if (bfd_is_abs_section (asect))
6827 sec_index = SHN_ABS;
6828 else if (bfd_is_com_section (asect))
6829 sec_index = SHN_COMMON;
6830 else if (bfd_is_und_section (asect))
6831 sec_index = SHN_UNDEF;
6832 else
6833 sec_index = SHN_BAD;
6834
6835 bed = get_elf_backend_data (abfd);
6836 if (bed->elf_backend_section_from_bfd_section)
6837 {
6838 int retval = sec_index;
6839
6840 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6841 return retval;
6842 }
6843
6844 if (sec_index == SHN_BAD)
6845 bfd_set_error (bfd_error_nonrepresentable_section);
6846
6847 return sec_index;
6848 }
6849
6850 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6851 on error. */
6852
6853 int
_bfd_elf_symbol_from_bfd_symbol(bfd * abfd,asymbol ** asym_ptr_ptr)6854 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
6855 {
6856 asymbol *asym_ptr = *asym_ptr_ptr;
6857 int idx;
6858 flagword flags = asym_ptr->flags;
6859
6860 /* When gas creates relocations against local labels, it creates its
6861 own symbol for the section, but does put the symbol into the
6862 symbol chain, so udata is 0. When the linker is generating
6863 relocatable output, this section symbol may be for one of the
6864 input sections rather than the output section. */
6865 if (asym_ptr->udata.i == 0
6866 && (flags & BSF_SECTION_SYM)
6867 && asym_ptr->section)
6868 {
6869 asection *sec;
6870
6871 sec = asym_ptr->section;
6872 if (sec->owner != abfd && sec->output_section != NULL)
6873 sec = sec->output_section;
6874 if (sec->owner == abfd
6875 && sec->index < elf_num_section_syms (abfd)
6876 && elf_section_syms (abfd)[sec->index] != NULL)
6877 asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
6878 }
6879
6880 idx = asym_ptr->udata.i;
6881
6882 if (idx == 0)
6883 {
6884 /* This case can occur when using --strip-symbol on a symbol
6885 which is used in a relocation entry. */
6886 _bfd_error_handler
6887 /* xgettext:c-format */
6888 (_("%pB: symbol `%s' required but not present"),
6889 abfd, bfd_asymbol_name (asym_ptr));
6890 bfd_set_error (bfd_error_no_symbols);
6891 return -1;
6892 }
6893
6894 #if DEBUG & 4
6895 {
6896 fprintf (stderr,
6897 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6898 (long) asym_ptr, asym_ptr->name, idx, flags);
6899 fflush (stderr);
6900 }
6901 #endif
6902
6903 return idx;
6904 }
6905
6906 /* Rewrite program header information. */
6907
6908 static bool
rewrite_elf_program_header(bfd * ibfd,bfd * obfd,bfd_vma maxpagesize)6909 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
6910 {
6911 Elf_Internal_Ehdr *iehdr;
6912 struct elf_segment_map *map;
6913 struct elf_segment_map *map_first;
6914 struct elf_segment_map **pointer_to_map;
6915 Elf_Internal_Phdr *segment;
6916 asection *section;
6917 unsigned int i;
6918 unsigned int num_segments;
6919 bool phdr_included = false;
6920 bool p_paddr_valid;
6921 struct elf_segment_map *phdr_adjust_seg = NULL;
6922 unsigned int phdr_adjust_num = 0;
6923 const struct elf_backend_data *bed;
6924 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
6925
6926 bed = get_elf_backend_data (ibfd);
6927 iehdr = elf_elfheader (ibfd);
6928
6929 map_first = NULL;
6930 pointer_to_map = &map_first;
6931
6932 num_segments = elf_elfheader (ibfd)->e_phnum;
6933
6934 /* Returns the end address of the segment + 1. */
6935 #define SEGMENT_END(segment, start) \
6936 (start + (segment->p_memsz > segment->p_filesz \
6937 ? segment->p_memsz : segment->p_filesz))
6938
6939 #define SECTION_SIZE(section, segment) \
6940 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6941 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6942 ? section->size : 0)
6943
6944 /* Returns TRUE if the given section is contained within
6945 the given segment. VMA addresses are compared. */
6946 #define IS_CONTAINED_BY_VMA(section, segment, opb) \
6947 (section->vma * (opb) >= segment->p_vaddr \
6948 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
6949 <= (SEGMENT_END (segment, segment->p_vaddr))))
6950
6951 /* Returns TRUE if the given section is contained within
6952 the given segment. LMA addresses are compared. */
6953 #define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6954 (section->lma * (opb) >= base \
6955 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6956 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
6957 <= SEGMENT_END (segment, base)))
6958
6959 /* Handle PT_NOTE segment. */
6960 #define IS_NOTE(p, s) \
6961 (p->p_type == PT_NOTE \
6962 && elf_section_type (s) == SHT_NOTE \
6963 && (bfd_vma) s->filepos >= p->p_offset \
6964 && ((bfd_vma) s->filepos + s->size \
6965 <= p->p_offset + p->p_filesz))
6966
6967 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6968 etc. */
6969 #define IS_COREFILE_NOTE(p, s) \
6970 (IS_NOTE (p, s) \
6971 && bfd_get_format (ibfd) == bfd_core \
6972 && s->vma == 0 \
6973 && s->lma == 0)
6974
6975 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6976 linker, which generates a PT_INTERP section with p_vaddr and
6977 p_memsz set to 0. */
6978 #define IS_SOLARIS_PT_INTERP(p, s) \
6979 (p->p_vaddr == 0 \
6980 && p->p_paddr == 0 \
6981 && p->p_memsz == 0 \
6982 && p->p_filesz > 0 \
6983 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6984 && s->size > 0 \
6985 && (bfd_vma) s->filepos >= p->p_offset \
6986 && ((bfd_vma) s->filepos + s->size \
6987 <= p->p_offset + p->p_filesz))
6988
6989 /* Decide if the given section should be included in the given segment.
6990 A section will be included if:
6991 1. It is within the address space of the segment -- we use the LMA
6992 if that is set for the segment and the VMA otherwise,
6993 2. It is an allocated section or a NOTE section in a PT_NOTE
6994 segment.
6995 3. There is an output section associated with it,
6996 4. The section has not already been allocated to a previous segment.
6997 5. PT_GNU_STACK segments do not include any sections.
6998 6. PT_TLS segment includes only SHF_TLS sections.
6999 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7000 8. PT_DYNAMIC should not contain empty sections at the beginning
7001 (with the possible exception of .dynamic). */
7002 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
7003 ((((segment->p_paddr \
7004 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
7005 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
7006 && (section->flags & SEC_ALLOC) != 0) \
7007 || IS_NOTE (segment, section)) \
7008 && segment->p_type != PT_GNU_STACK \
7009 && (segment->p_type != PT_TLS \
7010 || (section->flags & SEC_THREAD_LOCAL)) \
7011 && (segment->p_type == PT_LOAD \
7012 || segment->p_type == PT_TLS \
7013 || (section->flags & SEC_THREAD_LOCAL) == 0) \
7014 && (segment->p_type != PT_DYNAMIC \
7015 || SECTION_SIZE (section, segment) > 0 \
7016 || (segment->p_paddr \
7017 ? segment->p_paddr != section->lma * (opb) \
7018 : segment->p_vaddr != section->vma * (opb)) \
7019 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
7020 && (segment->p_type != PT_LOAD || !section->segment_mark))
7021
7022 /* If the output section of a section in the input segment is NULL,
7023 it is removed from the corresponding output segment. */
7024 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
7025 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
7026 && section->output_section != NULL)
7027
7028 /* Returns TRUE iff seg1 starts after the end of seg2. */
7029 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
7030 (seg1->field >= SEGMENT_END (seg2, seg2->field))
7031
7032 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7033 their VMA address ranges and their LMA address ranges overlap.
7034 It is possible to have overlapping VMA ranges without overlapping LMA
7035 ranges. RedBoot images for example can have both .data and .bss mapped
7036 to the same VMA range, but with the .data section mapped to a different
7037 LMA. */
7038 #define SEGMENT_OVERLAPS(seg1, seg2) \
7039 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
7040 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
7041 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
7042 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7043
7044 /* Initialise the segment mark field, and discard stupid alignment. */
7045 for (section = ibfd->sections; section != NULL; section = section->next)
7046 {
7047 asection *o = section->output_section;
7048 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7049 o->alignment_power = 0;
7050 section->segment_mark = false;
7051 }
7052
7053 /* The Solaris linker creates program headers in which all the
7054 p_paddr fields are zero. When we try to objcopy or strip such a
7055 file, we get confused. Check for this case, and if we find it
7056 don't set the p_paddr_valid fields. */
7057 p_paddr_valid = false;
7058 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7059 i < num_segments;
7060 i++, segment++)
7061 if (segment->p_paddr != 0)
7062 {
7063 p_paddr_valid = true;
7064 break;
7065 }
7066
7067 /* Scan through the segments specified in the program header
7068 of the input BFD. For this first scan we look for overlaps
7069 in the loadable segments. These can be created by weird
7070 parameters to objcopy. Also, fix some solaris weirdness. */
7071 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7072 i < num_segments;
7073 i++, segment++)
7074 {
7075 unsigned int j;
7076 Elf_Internal_Phdr *segment2;
7077
7078 if (segment->p_type == PT_INTERP)
7079 for (section = ibfd->sections; section; section = section->next)
7080 if (IS_SOLARIS_PT_INTERP (segment, section))
7081 {
7082 /* Mininal change so that the normal section to segment
7083 assignment code will work. */
7084 segment->p_vaddr = section->vma * opb;
7085 break;
7086 }
7087
7088 if (segment->p_type != PT_LOAD)
7089 {
7090 /* Remove PT_GNU_RELRO segment. */
7091 if (segment->p_type == PT_GNU_RELRO)
7092 segment->p_type = PT_NULL;
7093 continue;
7094 }
7095
7096 /* Determine if this segment overlaps any previous segments. */
7097 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7098 {
7099 bfd_signed_vma extra_length;
7100
7101 if (segment2->p_type != PT_LOAD
7102 || !SEGMENT_OVERLAPS (segment, segment2))
7103 continue;
7104
7105 /* Merge the two segments together. */
7106 if (segment2->p_vaddr < segment->p_vaddr)
7107 {
7108 /* Extend SEGMENT2 to include SEGMENT and then delete
7109 SEGMENT. */
7110 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
7111 - SEGMENT_END (segment2, segment2->p_vaddr));
7112
7113 if (extra_length > 0)
7114 {
7115 segment2->p_memsz += extra_length;
7116 segment2->p_filesz += extra_length;
7117 }
7118
7119 segment->p_type = PT_NULL;
7120
7121 /* Since we have deleted P we must restart the outer loop. */
7122 i = 0;
7123 segment = elf_tdata (ibfd)->phdr;
7124 break;
7125 }
7126 else
7127 {
7128 /* Extend SEGMENT to include SEGMENT2 and then delete
7129 SEGMENT2. */
7130 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
7131 - SEGMENT_END (segment, segment->p_vaddr));
7132
7133 if (extra_length > 0)
7134 {
7135 segment->p_memsz += extra_length;
7136 segment->p_filesz += extra_length;
7137 }
7138
7139 segment2->p_type = PT_NULL;
7140 }
7141 }
7142 }
7143
7144 /* The second scan attempts to assign sections to segments. */
7145 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7146 i < num_segments;
7147 i++, segment++)
7148 {
7149 unsigned int section_count;
7150 asection **sections;
7151 asection *output_section;
7152 unsigned int isec;
7153 asection *matching_lma;
7154 asection *suggested_lma;
7155 unsigned int j;
7156 size_t amt;
7157 asection *first_section;
7158
7159 if (segment->p_type == PT_NULL)
7160 continue;
7161
7162 first_section = NULL;
7163 /* Compute how many sections might be placed into this segment. */
7164 for (section = ibfd->sections, section_count = 0;
7165 section != NULL;
7166 section = section->next)
7167 {
7168 /* Find the first section in the input segment, which may be
7169 removed from the corresponding output segment. */
7170 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb))
7171 {
7172 if (first_section == NULL)
7173 first_section = section;
7174 if (section->output_section != NULL)
7175 ++section_count;
7176 }
7177 }
7178
7179 /* Allocate a segment map big enough to contain
7180 all of the sections we have selected. */
7181 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7182 amt += section_count * sizeof (asection *);
7183 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7184 if (map == NULL)
7185 return false;
7186
7187 /* Initialise the fields of the segment map. Default to
7188 using the physical address of the segment in the input BFD. */
7189 map->next = NULL;
7190 map->p_type = segment->p_type;
7191 map->p_flags = segment->p_flags;
7192 map->p_flags_valid = 1;
7193
7194 if (map->p_type == PT_LOAD
7195 && (ibfd->flags & D_PAGED) != 0
7196 && maxpagesize > 1
7197 && segment->p_align > 1)
7198 {
7199 map->p_align = segment->p_align;
7200 if (segment->p_align > maxpagesize)
7201 map->p_align = maxpagesize;
7202 map->p_align_valid = 1;
7203 }
7204
7205 /* If the first section in the input segment is removed, there is
7206 no need to preserve segment physical address in the corresponding
7207 output segment. */
7208 if (!first_section || first_section->output_section != NULL)
7209 {
7210 map->p_paddr = segment->p_paddr;
7211 map->p_paddr_valid = p_paddr_valid;
7212 }
7213
7214 /* Determine if this segment contains the ELF file header
7215 and if it contains the program headers themselves. */
7216 map->includes_filehdr = (segment->p_offset == 0
7217 && segment->p_filesz >= iehdr->e_ehsize);
7218 map->includes_phdrs = 0;
7219
7220 if (!phdr_included || segment->p_type != PT_LOAD)
7221 {
7222 map->includes_phdrs =
7223 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7224 && (segment->p_offset + segment->p_filesz
7225 >= ((bfd_vma) iehdr->e_phoff
7226 + iehdr->e_phnum * iehdr->e_phentsize)));
7227
7228 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7229 phdr_included = true;
7230 }
7231
7232 if (section_count == 0)
7233 {
7234 /* Special segments, such as the PT_PHDR segment, may contain
7235 no sections, but ordinary, loadable segments should contain
7236 something. They are allowed by the ELF spec however, so only
7237 a warning is produced.
7238 There is however the valid use case of embedded systems which
7239 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7240 flash memory with zeros. No warning is shown for that case. */
7241 if (segment->p_type == PT_LOAD
7242 && (segment->p_filesz > 0 || segment->p_memsz == 0))
7243 /* xgettext:c-format */
7244 _bfd_error_handler
7245 (_("%pB: warning: empty loadable segment detected"
7246 " at vaddr=%#" PRIx64 ", is this intentional?"),
7247 ibfd, (uint64_t) segment->p_vaddr);
7248
7249 map->p_vaddr_offset = segment->p_vaddr / opb;
7250 map->count = 0;
7251 *pointer_to_map = map;
7252 pointer_to_map = &map->next;
7253
7254 continue;
7255 }
7256
7257 /* Now scan the sections in the input BFD again and attempt
7258 to add their corresponding output sections to the segment map.
7259 The problem here is how to handle an output section which has
7260 been moved (ie had its LMA changed). There are four possibilities:
7261
7262 1. None of the sections have been moved.
7263 In this case we can continue to use the segment LMA from the
7264 input BFD.
7265
7266 2. All of the sections have been moved by the same amount.
7267 In this case we can change the segment's LMA to match the LMA
7268 of the first section.
7269
7270 3. Some of the sections have been moved, others have not.
7271 In this case those sections which have not been moved can be
7272 placed in the current segment which will have to have its size,
7273 and possibly its LMA changed, and a new segment or segments will
7274 have to be created to contain the other sections.
7275
7276 4. The sections have been moved, but not by the same amount.
7277 In this case we can change the segment's LMA to match the LMA
7278 of the first section and we will have to create a new segment
7279 or segments to contain the other sections.
7280
7281 In order to save time, we allocate an array to hold the section
7282 pointers that we are interested in. As these sections get assigned
7283 to a segment, they are removed from this array. */
7284
7285 amt = section_count * sizeof (asection *);
7286 sections = (asection **) bfd_malloc (amt);
7287 if (sections == NULL)
7288 return false;
7289
7290 /* Step One: Scan for segment vs section LMA conflicts.
7291 Also add the sections to the section array allocated above.
7292 Also add the sections to the current segment. In the common
7293 case, where the sections have not been moved, this means that
7294 we have completely filled the segment, and there is nothing
7295 more to do. */
7296 isec = 0;
7297 matching_lma = NULL;
7298 suggested_lma = NULL;
7299
7300 for (section = first_section, j = 0;
7301 section != NULL;
7302 section = section->next)
7303 {
7304 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb))
7305 {
7306 output_section = section->output_section;
7307
7308 sections[j++] = section;
7309
7310 /* The Solaris native linker always sets p_paddr to 0.
7311 We try to catch that case here, and set it to the
7312 correct value. Note - some backends require that
7313 p_paddr be left as zero. */
7314 if (!p_paddr_valid
7315 && segment->p_vaddr != 0
7316 && !bed->want_p_paddr_set_to_zero
7317 && isec == 0
7318 && output_section->lma != 0
7319 && (align_power (segment->p_vaddr
7320 + (map->includes_filehdr
7321 ? iehdr->e_ehsize : 0)
7322 + (map->includes_phdrs
7323 ? iehdr->e_phnum * iehdr->e_phentsize
7324 : 0),
7325 output_section->alignment_power * opb)
7326 == (output_section->vma * opb)))
7327 map->p_paddr = segment->p_vaddr;
7328
7329 /* Match up the physical address of the segment with the
7330 LMA address of the output section. */
7331 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7332 opb)
7333 || IS_COREFILE_NOTE (segment, section)
7334 || (bed->want_p_paddr_set_to_zero
7335 && IS_CONTAINED_BY_VMA (output_section, segment, opb)))
7336 {
7337 if (matching_lma == NULL
7338 || output_section->lma < matching_lma->lma)
7339 matching_lma = output_section;
7340
7341 /* We assume that if the section fits within the segment
7342 then it does not overlap any other section within that
7343 segment. */
7344 map->sections[isec++] = output_section;
7345 }
7346 else if (suggested_lma == NULL)
7347 suggested_lma = output_section;
7348
7349 if (j == section_count)
7350 break;
7351 }
7352 }
7353
7354 BFD_ASSERT (j == section_count);
7355
7356 /* Step Two: Adjust the physical address of the current segment,
7357 if necessary. */
7358 if (isec == section_count)
7359 {
7360 /* All of the sections fitted within the segment as currently
7361 specified. This is the default case. Add the segment to
7362 the list of built segments and carry on to process the next
7363 program header in the input BFD. */
7364 map->count = section_count;
7365 *pointer_to_map = map;
7366 pointer_to_map = &map->next;
7367
7368 if (p_paddr_valid
7369 && !bed->want_p_paddr_set_to_zero)
7370 {
7371 bfd_vma hdr_size = 0;
7372 if (map->includes_filehdr)
7373 hdr_size = iehdr->e_ehsize;
7374 if (map->includes_phdrs)
7375 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7376
7377 /* Account for padding before the first section in the
7378 segment. */
7379 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7380 - matching_lma->lma);
7381 }
7382
7383 free (sections);
7384 continue;
7385 }
7386 else
7387 {
7388 /* Change the current segment's physical address to match
7389 the LMA of the first section that fitted, or if no
7390 section fitted, the first section. */
7391 if (matching_lma == NULL)
7392 matching_lma = suggested_lma;
7393
7394 map->p_paddr = matching_lma->lma * opb;
7395
7396 /* Offset the segment physical address from the lma
7397 to allow for space taken up by elf headers. */
7398 if (map->includes_phdrs)
7399 {
7400 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7401
7402 /* iehdr->e_phnum is just an estimate of the number
7403 of program headers that we will need. Make a note
7404 here of the number we used and the segment we chose
7405 to hold these headers, so that we can adjust the
7406 offset when we know the correct value. */
7407 phdr_adjust_num = iehdr->e_phnum;
7408 phdr_adjust_seg = map;
7409 }
7410
7411 if (map->includes_filehdr)
7412 {
7413 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7414 map->p_paddr -= iehdr->e_ehsize;
7415 /* We've subtracted off the size of headers from the
7416 first section lma, but there may have been some
7417 alignment padding before that section too. Try to
7418 account for that by adjusting the segment lma down to
7419 the same alignment. */
7420 if (segment->p_align != 0 && segment->p_align < align)
7421 align = segment->p_align;
7422 map->p_paddr &= -(align * opb);
7423 }
7424 }
7425
7426 /* Step Three: Loop over the sections again, this time assigning
7427 those that fit to the current segment and removing them from the
7428 sections array; but making sure not to leave large gaps. Once all
7429 possible sections have been assigned to the current segment it is
7430 added to the list of built segments and if sections still remain
7431 to be assigned, a new segment is constructed before repeating
7432 the loop. */
7433 isec = 0;
7434 do
7435 {
7436 map->count = 0;
7437 suggested_lma = NULL;
7438
7439 /* Fill the current segment with sections that fit. */
7440 for (j = 0; j < section_count; j++)
7441 {
7442 section = sections[j];
7443
7444 if (section == NULL)
7445 continue;
7446
7447 output_section = section->output_section;
7448
7449 BFD_ASSERT (output_section != NULL);
7450
7451 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr,
7452 opb)
7453 || IS_COREFILE_NOTE (segment, section))
7454 {
7455 if (map->count == 0)
7456 {
7457 /* If the first section in a segment does not start at
7458 the beginning of the segment, then something is
7459 wrong. */
7460 if (align_power (map->p_paddr
7461 + (map->includes_filehdr
7462 ? iehdr->e_ehsize : 0)
7463 + (map->includes_phdrs
7464 ? iehdr->e_phnum * iehdr->e_phentsize
7465 : 0),
7466 output_section->alignment_power * opb)
7467 != output_section->lma * opb)
7468 goto sorry;
7469 }
7470 else
7471 {
7472 asection *prev_sec;
7473
7474 prev_sec = map->sections[map->count - 1];
7475
7476 /* If the gap between the end of the previous section
7477 and the start of this section is more than
7478 maxpagesize then we need to start a new segment. */
7479 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7480 maxpagesize)
7481 < BFD_ALIGN (output_section->lma, maxpagesize))
7482 || (prev_sec->lma + prev_sec->size
7483 > output_section->lma))
7484 {
7485 if (suggested_lma == NULL)
7486 suggested_lma = output_section;
7487
7488 continue;
7489 }
7490 }
7491
7492 map->sections[map->count++] = output_section;
7493 ++isec;
7494 sections[j] = NULL;
7495 if (segment->p_type == PT_LOAD)
7496 section->segment_mark = true;
7497 }
7498 else if (suggested_lma == NULL)
7499 suggested_lma = output_section;
7500 }
7501
7502 /* PR 23932. A corrupt input file may contain sections that cannot
7503 be assigned to any segment - because for example they have a
7504 negative size - or segments that do not contain any sections.
7505 But there are also valid reasons why a segment can be empty.
7506 So allow a count of zero. */
7507
7508 /* Add the current segment to the list of built segments. */
7509 *pointer_to_map = map;
7510 pointer_to_map = &map->next;
7511
7512 if (isec < section_count)
7513 {
7514 /* We still have not allocated all of the sections to
7515 segments. Create a new segment here, initialise it
7516 and carry on looping. */
7517 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7518 amt += section_count * sizeof (asection *);
7519 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7520 if (map == NULL)
7521 {
7522 free (sections);
7523 return false;
7524 }
7525
7526 /* Initialise the fields of the segment map. Set the physical
7527 physical address to the LMA of the first section that has
7528 not yet been assigned. */
7529 map->next = NULL;
7530 map->p_type = segment->p_type;
7531 map->p_flags = segment->p_flags;
7532 map->p_flags_valid = 1;
7533 map->p_paddr = suggested_lma->lma * opb;
7534 map->p_paddr_valid = p_paddr_valid;
7535 map->includes_filehdr = 0;
7536 map->includes_phdrs = 0;
7537 }
7538
7539 continue;
7540 sorry:
7541 bfd_set_error (bfd_error_sorry);
7542 free (sections);
7543 return false;
7544 }
7545 while (isec < section_count);
7546
7547 free (sections);
7548 }
7549
7550 elf_seg_map (obfd) = map_first;
7551
7552 /* If we had to estimate the number of program headers that were
7553 going to be needed, then check our estimate now and adjust
7554 the offset if necessary. */
7555 if (phdr_adjust_seg != NULL)
7556 {
7557 unsigned int count;
7558
7559 for (count = 0, map = map_first; map != NULL; map = map->next)
7560 count++;
7561
7562 if (count > phdr_adjust_num)
7563 phdr_adjust_seg->p_paddr
7564 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7565
7566 for (map = map_first; map != NULL; map = map->next)
7567 if (map->p_type == PT_PHDR)
7568 {
7569 bfd_vma adjust
7570 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7571 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7572 break;
7573 }
7574 }
7575
7576 #undef SEGMENT_END
7577 #undef SECTION_SIZE
7578 #undef IS_CONTAINED_BY_VMA
7579 #undef IS_CONTAINED_BY_LMA
7580 #undef IS_NOTE
7581 #undef IS_COREFILE_NOTE
7582 #undef IS_SOLARIS_PT_INTERP
7583 #undef IS_SECTION_IN_INPUT_SEGMENT
7584 #undef INCLUDE_SECTION_IN_SEGMENT
7585 #undef SEGMENT_AFTER_SEGMENT
7586 #undef SEGMENT_OVERLAPS
7587 return true;
7588 }
7589
7590 /* Return true if p_align in the ELF program header in ABFD is valid. */
7591
7592 static bool
elf_is_p_align_valid(bfd * abfd)7593 elf_is_p_align_valid (bfd *abfd)
7594 {
7595 unsigned int i;
7596 Elf_Internal_Phdr *segment;
7597 unsigned int num_segments;
7598 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7599 bfd_size_type maxpagesize = bed->maxpagesize;
7600 bfd_size_type p_align = bed->p_align;
7601
7602 /* Return true if the default p_align value isn't set or the maximum
7603 page size is the same as the minimum page size. */
7604 if (p_align == 0 || maxpagesize == bed->minpagesize)
7605 return true;
7606
7607 /* When the default p_align value is set, p_align may be set to the
7608 default p_align value while segments are aligned to the maximum
7609 page size. In this case, the input p_align will be ignored and
7610 the maximum page size will be used to align the output segments. */
7611 segment = elf_tdata (abfd)->phdr;
7612 num_segments = elf_elfheader (abfd)->e_phnum;
7613 for (i = 0; i < num_segments; i++, segment++)
7614 if (segment->p_type == PT_LOAD
7615 && (segment->p_align != p_align
7616 || vma_page_aligned_bias (segment->p_vaddr,
7617 segment->p_offset,
7618 maxpagesize) != 0))
7619 return true;
7620
7621 return false;
7622 }
7623
7624 /* Copy ELF program header information. */
7625
7626 static bool
copy_elf_program_header(bfd * ibfd,bfd * obfd)7627 copy_elf_program_header (bfd *ibfd, bfd *obfd)
7628 {
7629 Elf_Internal_Ehdr *iehdr;
7630 struct elf_segment_map *map;
7631 struct elf_segment_map *map_first;
7632 struct elf_segment_map **pointer_to_map;
7633 Elf_Internal_Phdr *segment;
7634 unsigned int i;
7635 unsigned int num_segments;
7636 bool phdr_included = false;
7637 bool p_paddr_valid;
7638 bool p_palign_valid;
7639 unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7640
7641 iehdr = elf_elfheader (ibfd);
7642
7643 map_first = NULL;
7644 pointer_to_map = &map_first;
7645
7646 /* If all the segment p_paddr fields are zero, don't set
7647 map->p_paddr_valid. */
7648 p_paddr_valid = false;
7649 num_segments = elf_elfheader (ibfd)->e_phnum;
7650 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7651 i < num_segments;
7652 i++, segment++)
7653 if (segment->p_paddr != 0)
7654 {
7655 p_paddr_valid = true;
7656 break;
7657 }
7658
7659 p_palign_valid = elf_is_p_align_valid (ibfd);
7660
7661 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7662 i < num_segments;
7663 i++, segment++)
7664 {
7665 asection *section;
7666 unsigned int section_count;
7667 size_t amt;
7668 Elf_Internal_Shdr *this_hdr;
7669 asection *first_section = NULL;
7670 asection *lowest_section;
7671
7672 /* Compute how many sections are in this segment. */
7673 for (section = ibfd->sections, section_count = 0;
7674 section != NULL;
7675 section = section->next)
7676 {
7677 this_hdr = &(elf_section_data(section)->this_hdr);
7678 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7679 {
7680 if (first_section == NULL)
7681 first_section = section;
7682 section_count++;
7683 }
7684 }
7685
7686 /* Allocate a segment map big enough to contain
7687 all of the sections we have selected. */
7688 amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7689 amt += section_count * sizeof (asection *);
7690 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7691 if (map == NULL)
7692 return false;
7693
7694 /* Initialize the fields of the output segment map with the
7695 input segment. */
7696 map->next = NULL;
7697 map->p_type = segment->p_type;
7698 map->p_flags = segment->p_flags;
7699 map->p_flags_valid = 1;
7700 map->p_paddr = segment->p_paddr;
7701 map->p_paddr_valid = p_paddr_valid;
7702 map->p_align = segment->p_align;
7703 /* Keep p_align of PT_GNU_STACK for stack alignment. */
7704 map->p_align_valid = (map->p_type == PT_GNU_STACK
7705 || p_palign_valid);
7706 map->p_vaddr_offset = 0;
7707
7708 if (map->p_type == PT_GNU_RELRO
7709 || map->p_type == PT_GNU_STACK)
7710 {
7711 /* The PT_GNU_RELRO segment may contain the first a few
7712 bytes in the .got.plt section even if the whole .got.plt
7713 section isn't in the PT_GNU_RELRO segment. We won't
7714 change the size of the PT_GNU_RELRO segment.
7715 Similarly, PT_GNU_STACK size is significant on uclinux
7716 systems. */
7717 map->p_size = segment->p_memsz;
7718 map->p_size_valid = 1;
7719 }
7720
7721 /* Determine if this segment contains the ELF file header
7722 and if it contains the program headers themselves. */
7723 map->includes_filehdr = (segment->p_offset == 0
7724 && segment->p_filesz >= iehdr->e_ehsize);
7725
7726 map->includes_phdrs = 0;
7727 if (! phdr_included || segment->p_type != PT_LOAD)
7728 {
7729 map->includes_phdrs =
7730 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7731 && (segment->p_offset + segment->p_filesz
7732 >= ((bfd_vma) iehdr->e_phoff
7733 + iehdr->e_phnum * iehdr->e_phentsize)));
7734
7735 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7736 phdr_included = true;
7737 }
7738
7739 lowest_section = NULL;
7740 if (section_count != 0)
7741 {
7742 unsigned int isec = 0;
7743
7744 for (section = first_section;
7745 section != NULL;
7746 section = section->next)
7747 {
7748 this_hdr = &(elf_section_data(section)->this_hdr);
7749 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7750 {
7751 map->sections[isec++] = section->output_section;
7752 if ((section->flags & SEC_ALLOC) != 0)
7753 {
7754 bfd_vma seg_off;
7755
7756 if (lowest_section == NULL
7757 || section->lma < lowest_section->lma)
7758 lowest_section = section;
7759
7760 /* Section lmas are set up from PT_LOAD header
7761 p_paddr in _bfd_elf_make_section_from_shdr.
7762 If this header has a p_paddr that disagrees
7763 with the section lma, flag the p_paddr as
7764 invalid. */
7765 if ((section->flags & SEC_LOAD) != 0)
7766 seg_off = this_hdr->sh_offset - segment->p_offset;
7767 else
7768 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7769 if (section->lma * opb - segment->p_paddr != seg_off)
7770 map->p_paddr_valid = false;
7771 }
7772 if (isec == section_count)
7773 break;
7774 }
7775 }
7776 }
7777
7778 if (section_count == 0)
7779 map->p_vaddr_offset = segment->p_vaddr / opb;
7780 else if (map->p_paddr_valid)
7781 {
7782 /* Account for padding before the first section in the segment. */
7783 bfd_vma hdr_size = 0;
7784 if (map->includes_filehdr)
7785 hdr_size = iehdr->e_ehsize;
7786 if (map->includes_phdrs)
7787 hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7788
7789 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7790 - (lowest_section ? lowest_section->lma : 0));
7791 }
7792
7793 map->count = section_count;
7794 *pointer_to_map = map;
7795 pointer_to_map = &map->next;
7796 }
7797
7798 elf_seg_map (obfd) = map_first;
7799 return true;
7800 }
7801
7802 /* Copy private BFD data. This copies or rewrites ELF program header
7803 information. */
7804
7805 static bool
copy_private_bfd_data(bfd * ibfd,bfd * obfd)7806 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7807 {
7808 bfd_vma maxpagesize;
7809
7810 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7811 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7812 return true;
7813
7814 if (elf_tdata (ibfd)->phdr == NULL)
7815 return true;
7816
7817 if (ibfd->xvec == obfd->xvec)
7818 {
7819 /* Check to see if any sections in the input BFD
7820 covered by ELF program header have changed. */
7821 Elf_Internal_Phdr *segment;
7822 asection *section, *osec;
7823 unsigned int i, num_segments;
7824 Elf_Internal_Shdr *this_hdr;
7825 const struct elf_backend_data *bed;
7826
7827 bed = get_elf_backend_data (ibfd);
7828
7829 /* Regenerate the segment map if p_paddr is set to 0. */
7830 if (bed->want_p_paddr_set_to_zero)
7831 goto rewrite;
7832
7833 /* Initialize the segment mark field. */
7834 for (section = obfd->sections; section != NULL;
7835 section = section->next)
7836 section->segment_mark = false;
7837
7838 num_segments = elf_elfheader (ibfd)->e_phnum;
7839 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7840 i < num_segments;
7841 i++, segment++)
7842 {
7843 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7844 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7845 which severly confuses things, so always regenerate the segment
7846 map in this case. */
7847 if (segment->p_paddr == 0
7848 && segment->p_memsz == 0
7849 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
7850 goto rewrite;
7851
7852 for (section = ibfd->sections;
7853 section != NULL; section = section->next)
7854 {
7855 /* We mark the output section so that we know it comes
7856 from the input BFD. */
7857 osec = section->output_section;
7858 if (osec)
7859 osec->segment_mark = true;
7860
7861 /* Check if this section is covered by the segment. */
7862 this_hdr = &(elf_section_data(section)->this_hdr);
7863 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
7864 {
7865 /* FIXME: Check if its output section is changed or
7866 removed. What else do we need to check? */
7867 if (osec == NULL
7868 || section->flags != osec->flags
7869 || section->lma != osec->lma
7870 || section->vma != osec->vma
7871 || section->size != osec->size
7872 || section->rawsize != osec->rawsize
7873 || section->alignment_power != osec->alignment_power)
7874 goto rewrite;
7875 }
7876 }
7877 }
7878
7879 /* Check to see if any output section do not come from the
7880 input BFD. */
7881 for (section = obfd->sections; section != NULL;
7882 section = section->next)
7883 {
7884 if (!section->segment_mark)
7885 goto rewrite;
7886 else
7887 section->segment_mark = false;
7888 }
7889
7890 return copy_elf_program_header (ibfd, obfd);
7891 }
7892
7893 rewrite:
7894 maxpagesize = 0;
7895 if (ibfd->xvec == obfd->xvec)
7896 {
7897 /* When rewriting program header, set the output maxpagesize to
7898 the maximum alignment of input PT_LOAD segments. */
7899 Elf_Internal_Phdr *segment;
7900 unsigned int i;
7901 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7902
7903 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7904 i < num_segments;
7905 i++, segment++)
7906 if (segment->p_type == PT_LOAD
7907 && maxpagesize < segment->p_align)
7908 {
7909 /* PR 17512: file: f17299af. */
7910 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
7911 /* xgettext:c-format */
7912 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7913 PRIx64 " is too large"),
7914 ibfd, (uint64_t) segment->p_align);
7915 else
7916 maxpagesize = segment->p_align;
7917 }
7918 }
7919 if (maxpagesize == 0)
7920 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
7921
7922 return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
7923 }
7924
7925 /* Initialize private output section information from input section. */
7926
7927 bool
_bfd_elf_init_private_section_data(bfd * ibfd,asection * isec,bfd * obfd,asection * osec,struct bfd_link_info * link_info)7928 _bfd_elf_init_private_section_data (bfd *ibfd,
7929 asection *isec,
7930 bfd *obfd,
7931 asection *osec,
7932 struct bfd_link_info *link_info)
7933
7934 {
7935 Elf_Internal_Shdr *ihdr, *ohdr;
7936 bool final_link = (link_info != NULL
7937 && !bfd_link_relocatable (link_info));
7938
7939 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7940 || obfd->xvec->flavour != bfd_target_elf_flavour)
7941 return true;
7942
7943 BFD_ASSERT (elf_section_data (osec) != NULL);
7944
7945 /* If this is a known ABI section, ELF section type and flags may
7946 have been set up when OSEC was created. For normal sections we
7947 allow the user to override the type and flags other than
7948 SHF_MASKOS and SHF_MASKPROC. */
7949 if (elf_section_type (osec) == SHT_PROGBITS
7950 || elf_section_type (osec) == SHT_NOTE
7951 || elf_section_type (osec) == SHT_NOBITS)
7952 elf_section_type (osec) = SHT_NULL;
7953 /* For objcopy and relocatable link, copy the ELF section type from
7954 the input file if the BFD section flags are the same. (If they
7955 are different the user may be doing something like
7956 "objcopy --set-section-flags .text=alloc,data".) For a final
7957 link allow some flags that the linker clears to differ. */
7958 if (elf_section_type (osec) == SHT_NULL
7959 && (osec->flags == isec->flags
7960 || (final_link
7961 && ((osec->flags ^ isec->flags)
7962 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
7963 elf_section_type (osec) = elf_section_type (isec);
7964
7965 /* FIXME: Is this correct for all OS/PROC specific flags? */
7966 elf_section_flags (osec) = (elf_section_flags (isec)
7967 & (SHF_MASKOS | SHF_MASKPROC));
7968
7969 /* Copy sh_info from input for mbind section. */
7970 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
7971 && elf_section_flags (isec) & SHF_GNU_MBIND)
7972 elf_section_data (osec)->this_hdr.sh_info
7973 = elf_section_data (isec)->this_hdr.sh_info;
7974
7975 /* Set things up for objcopy and relocatable link. The output
7976 SHT_GROUP section will have its elf_next_in_group pointing back
7977 to the input group members. Ignore linker created group section.
7978 See elfNN_ia64_object_p in elfxx-ia64.c. */
7979 if ((link_info == NULL
7980 || !link_info->resolve_section_groups)
7981 && (elf_sec_group (isec) == NULL
7982 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
7983 {
7984 if (elf_section_flags (isec) & SHF_GROUP)
7985 elf_section_flags (osec) |= SHF_GROUP;
7986 elf_next_in_group (osec) = elf_next_in_group (isec);
7987 elf_section_data (osec)->group = elf_section_data (isec)->group;
7988 }
7989
7990 /* If not decompress, preserve SHF_COMPRESSED. */
7991 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7992 elf_section_flags (osec) |= (elf_section_flags (isec)
7993 & SHF_COMPRESSED);
7994
7995 ihdr = &elf_section_data (isec)->this_hdr;
7996
7997 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7998 don't use the output section of the linked-to section since it
7999 may be NULL at this point. */
8000 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8001 {
8002 ohdr = &elf_section_data (osec)->this_hdr;
8003 ohdr->sh_flags |= SHF_LINK_ORDER;
8004 elf_linked_to_section (osec) = elf_linked_to_section (isec);
8005 }
8006
8007 osec->use_rela_p = isec->use_rela_p;
8008
8009 return true;
8010 }
8011
8012 /* Copy private section information. This copies over the entsize
8013 field, and sometimes the info field. */
8014
8015 bool
_bfd_elf_copy_private_section_data(bfd * ibfd,asection * isec,bfd * obfd,asection * osec)8016 _bfd_elf_copy_private_section_data (bfd *ibfd,
8017 asection *isec,
8018 bfd *obfd,
8019 asection *osec)
8020 {
8021 Elf_Internal_Shdr *ihdr, *ohdr;
8022
8023 if (ibfd->xvec->flavour != bfd_target_elf_flavour
8024 || obfd->xvec->flavour != bfd_target_elf_flavour)
8025 return true;
8026
8027 ihdr = &elf_section_data (isec)->this_hdr;
8028 ohdr = &elf_section_data (osec)->this_hdr;
8029
8030 ohdr->sh_entsize = ihdr->sh_entsize;
8031
8032 if (ihdr->sh_type == SHT_SYMTAB
8033 || ihdr->sh_type == SHT_DYNSYM
8034 || ihdr->sh_type == SHT_GNU_verneed
8035 || ihdr->sh_type == SHT_GNU_verdef)
8036 ohdr->sh_info = ihdr->sh_info;
8037
8038 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
8039 NULL);
8040 }
8041
8042 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8043 necessary if we are removing either the SHT_GROUP section or any of
8044 the group member sections. DISCARDED is the value that a section's
8045 output_section has if the section will be discarded, NULL when this
8046 function is called from objcopy, bfd_abs_section_ptr when called
8047 from the linker. */
8048
8049 bool
_bfd_elf_fixup_group_sections(bfd * ibfd,asection * discarded)8050 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8051 {
8052 asection *isec;
8053
8054 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8055 if (elf_section_type (isec) == SHT_GROUP)
8056 {
8057 asection *first = elf_next_in_group (isec);
8058 asection *s = first;
8059 bfd_size_type removed = 0;
8060
8061 while (s != NULL)
8062 {
8063 /* If this member section is being output but the
8064 SHT_GROUP section is not, then clear the group info
8065 set up by _bfd_elf_copy_private_section_data. */
8066 if (s->output_section != discarded
8067 && isec->output_section == discarded)
8068 {
8069 elf_section_flags (s->output_section) &= ~SHF_GROUP;
8070 elf_group_name (s->output_section) = NULL;
8071 }
8072 else
8073 {
8074 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8075 if (s->output_section == discarded
8076 && isec->output_section != discarded)
8077 {
8078 /* Conversely, if the member section is not being
8079 output but the SHT_GROUP section is, then adjust
8080 its size. */
8081 removed += 4;
8082 if (elf_sec->rel.hdr != NULL
8083 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8084 removed += 4;
8085 if (elf_sec->rela.hdr != NULL
8086 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8087 removed += 4;
8088 }
8089 else
8090 {
8091 /* Also adjust for zero-sized relocation member
8092 section. */
8093 if (elf_sec->rel.hdr != NULL
8094 && elf_sec->rel.hdr->sh_size == 0)
8095 removed += 4;
8096 if (elf_sec->rela.hdr != NULL
8097 && elf_sec->rela.hdr->sh_size == 0)
8098 removed += 4;
8099 }
8100 }
8101 s = elf_next_in_group (s);
8102 if (s == first)
8103 break;
8104 }
8105 if (removed != 0)
8106 {
8107 if (discarded != NULL)
8108 {
8109 /* If we've been called for ld -r, then we need to
8110 adjust the input section size. */
8111 if (isec->rawsize == 0)
8112 isec->rawsize = isec->size;
8113 isec->size = isec->rawsize - removed;
8114 if (isec->size <= 4)
8115 {
8116 isec->size = 0;
8117 isec->flags |= SEC_EXCLUDE;
8118 }
8119 }
8120 else if (isec->output_section != NULL)
8121 {
8122 /* Adjust the output section size when called from
8123 objcopy. */
8124 isec->output_section->size -= removed;
8125 if (isec->output_section->size <= 4)
8126 {
8127 isec->output_section->size = 0;
8128 isec->output_section->flags |= SEC_EXCLUDE;
8129 }
8130 }
8131 }
8132 }
8133
8134 return true;
8135 }
8136
8137 /* Copy private header information. */
8138
8139 bool
_bfd_elf_copy_private_header_data(bfd * ibfd,bfd * obfd)8140 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8141 {
8142 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8143 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8144 return true;
8145
8146 /* Copy over private BFD data if it has not already been copied.
8147 This must be done here, rather than in the copy_private_bfd_data
8148 entry point, because the latter is called after the section
8149 contents have been set, which means that the program headers have
8150 already been worked out. */
8151 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8152 {
8153 if (! copy_private_bfd_data (ibfd, obfd))
8154 return false;
8155 }
8156
8157 return _bfd_elf_fixup_group_sections (ibfd, NULL);
8158 }
8159
8160 /* Copy private symbol information. If this symbol is in a section
8161 which we did not map into a BFD section, try to map the section
8162 index correctly. We use special macro definitions for the mapped
8163 section indices; these definitions are interpreted by the
8164 swap_out_syms function. */
8165
8166 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8167 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8168 #define MAP_STRTAB (SHN_HIOS + 3)
8169 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8170 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8171
8172 bool
_bfd_elf_copy_private_symbol_data(bfd * ibfd,asymbol * isymarg,bfd * obfd,asymbol * osymarg)8173 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
8174 asymbol *isymarg,
8175 bfd *obfd,
8176 asymbol *osymarg)
8177 {
8178 elf_symbol_type *isym, *osym;
8179
8180 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
8181 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
8182 return true;
8183
8184 isym = elf_symbol_from (isymarg);
8185 osym = elf_symbol_from (osymarg);
8186
8187 if (isym != NULL
8188 && isym->internal_elf_sym.st_shndx != 0
8189 && osym != NULL
8190 && bfd_is_abs_section (isym->symbol.section))
8191 {
8192 unsigned int shndx;
8193
8194 shndx = isym->internal_elf_sym.st_shndx;
8195 if (shndx == elf_onesymtab (ibfd))
8196 shndx = MAP_ONESYMTAB;
8197 else if (shndx == elf_dynsymtab (ibfd))
8198 shndx = MAP_DYNSYMTAB;
8199 else if (shndx == elf_strtab_sec (ibfd))
8200 shndx = MAP_STRTAB;
8201 else if (shndx == elf_shstrtab_sec (ibfd))
8202 shndx = MAP_SHSTRTAB;
8203 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8204 shndx = MAP_SYM_SHNDX;
8205 osym->internal_elf_sym.st_shndx = shndx;
8206 }
8207
8208 return true;
8209 }
8210
8211 /* Swap out the symbols. */
8212
8213 static bool
swap_out_syms(bfd * abfd,struct elf_strtab_hash ** sttp,int relocatable_p,struct bfd_link_info * info)8214 swap_out_syms (bfd *abfd,
8215 struct elf_strtab_hash **sttp,
8216 int relocatable_p,
8217 struct bfd_link_info *info)
8218 {
8219 const struct elf_backend_data *bed;
8220 unsigned int symcount;
8221 asymbol **syms;
8222 struct elf_strtab_hash *stt;
8223 Elf_Internal_Shdr *symtab_hdr;
8224 Elf_Internal_Shdr *symtab_shndx_hdr;
8225 Elf_Internal_Shdr *symstrtab_hdr;
8226 struct elf_sym_strtab *symstrtab;
8227 bfd_byte *outbound_syms;
8228 bfd_byte *outbound_shndx;
8229 unsigned long outbound_syms_index;
8230 unsigned int idx;
8231 unsigned int num_locals;
8232 size_t amt;
8233 bool name_local_sections;
8234
8235 if (!elf_map_symbols (abfd, &num_locals))
8236 return false;
8237
8238 /* Dump out the symtabs. */
8239 stt = _bfd_elf_strtab_init ();
8240 if (stt == NULL)
8241 return false;
8242
8243 bed = get_elf_backend_data (abfd);
8244 symcount = bfd_get_symcount (abfd);
8245 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8246 symtab_hdr->sh_type = SHT_SYMTAB;
8247 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8248 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8249 symtab_hdr->sh_info = num_locals + 1;
8250 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8251
8252 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8253 symstrtab_hdr->sh_type = SHT_STRTAB;
8254
8255 /* Allocate buffer to swap out the .strtab section. */
8256 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8257 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8258 {
8259 bfd_set_error (bfd_error_no_memory);
8260 _bfd_elf_strtab_free (stt);
8261 return false;
8262 }
8263
8264 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8265 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
8266 {
8267 error_no_mem:
8268 bfd_set_error (bfd_error_no_memory);
8269 error_return:
8270 free (symstrtab);
8271 _bfd_elf_strtab_free (stt);
8272 return false;
8273 }
8274 symtab_hdr->contents = outbound_syms;
8275 outbound_syms_index = 0;
8276
8277 outbound_shndx = NULL;
8278
8279 if (elf_symtab_shndx_list (abfd))
8280 {
8281 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8282 if (symtab_shndx_hdr->sh_name != 0)
8283 {
8284 if (_bfd_mul_overflow (symcount + 1,
8285 sizeof (Elf_External_Sym_Shndx), &amt))
8286 goto error_no_mem;
8287 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt);
8288 if (outbound_shndx == NULL)
8289 goto error_return;
8290
8291 symtab_shndx_hdr->contents = outbound_shndx;
8292 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8293 symtab_shndx_hdr->sh_size = amt;
8294 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8295 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8296 }
8297 /* FIXME: What about any other headers in the list ? */
8298 }
8299
8300 /* Now generate the data (for "contents"). */
8301 {
8302 /* Fill in zeroth symbol and swap it out. */
8303 Elf_Internal_Sym sym;
8304 sym.st_name = 0;
8305 sym.st_value = 0;
8306 sym.st_size = 0;
8307 sym.st_info = 0;
8308 sym.st_other = 0;
8309 sym.st_shndx = SHN_UNDEF;
8310 sym.st_target_internal = 0;
8311 symstrtab[0].sym = sym;
8312 symstrtab[0].dest_index = outbound_syms_index;
8313 outbound_syms_index++;
8314 }
8315
8316 name_local_sections
8317 = (bed->elf_backend_name_local_section_symbols
8318 && bed->elf_backend_name_local_section_symbols (abfd));
8319
8320 syms = bfd_get_outsymbols (abfd);
8321 for (idx = 0; idx < symcount;)
8322 {
8323 Elf_Internal_Sym sym;
8324 bfd_vma value = syms[idx]->value;
8325 elf_symbol_type *type_ptr;
8326 flagword flags = syms[idx]->flags;
8327 int type;
8328
8329 if (!name_local_sections
8330 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8331 {
8332 /* Local section symbols have no name. */
8333 sym.st_name = (unsigned long) -1;
8334 }
8335 else
8336 {
8337 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8338 to get the final offset for st_name. */
8339 sym.st_name
8340 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
8341 false);
8342 if (sym.st_name == (unsigned long) -1)
8343 goto error_return;
8344 }
8345
8346 type_ptr = elf_symbol_from (syms[idx]);
8347
8348 if ((flags & BSF_SECTION_SYM) == 0
8349 && bfd_is_com_section (syms[idx]->section))
8350 {
8351 /* ELF common symbols put the alignment into the `value' field,
8352 and the size into the `size' field. This is backwards from
8353 how BFD handles it, so reverse it here. */
8354 sym.st_size = value;
8355 if (type_ptr == NULL
8356 || type_ptr->internal_elf_sym.st_value == 0)
8357 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8358 else
8359 sym.st_value = type_ptr->internal_elf_sym.st_value;
8360 sym.st_shndx = _bfd_elf_section_from_bfd_section
8361 (abfd, syms[idx]->section);
8362 }
8363 else
8364 {
8365 asection *sec = syms[idx]->section;
8366 unsigned int shndx;
8367
8368 if (sec->output_section)
8369 {
8370 value += sec->output_offset;
8371 sec = sec->output_section;
8372 }
8373
8374 /* Don't add in the section vma for relocatable output. */
8375 if (! relocatable_p)
8376 value += sec->vma;
8377 sym.st_value = value;
8378 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8379
8380 if (bfd_is_abs_section (sec)
8381 && type_ptr != NULL
8382 && type_ptr->internal_elf_sym.st_shndx != 0)
8383 {
8384 /* This symbol is in a real ELF section which we did
8385 not create as a BFD section. Undo the mapping done
8386 by copy_private_symbol_data. */
8387 shndx = type_ptr->internal_elf_sym.st_shndx;
8388 switch (shndx)
8389 {
8390 case MAP_ONESYMTAB:
8391 shndx = elf_onesymtab (abfd);
8392 break;
8393 case MAP_DYNSYMTAB:
8394 shndx = elf_dynsymtab (abfd);
8395 break;
8396 case MAP_STRTAB:
8397 shndx = elf_strtab_sec (abfd);
8398 break;
8399 case MAP_SHSTRTAB:
8400 shndx = elf_shstrtab_sec (abfd);
8401 break;
8402 case MAP_SYM_SHNDX:
8403 if (elf_symtab_shndx_list (abfd))
8404 shndx = elf_symtab_shndx_list (abfd)->ndx;
8405 break;
8406 case SHN_COMMON:
8407 case SHN_ABS:
8408 shndx = SHN_ABS;
8409 break;
8410 default:
8411 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8412 {
8413 if (bed->symbol_section_index)
8414 shndx = bed->symbol_section_index (abfd, type_ptr);
8415 /* Otherwise just leave the index alone. */
8416 }
8417 else
8418 {
8419 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8420 _bfd_error_handler (_("%pB: \
8421 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8422 abfd, shndx);
8423 shndx = SHN_ABS;
8424 }
8425 break;
8426 }
8427 }
8428 else
8429 {
8430 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8431
8432 if (shndx == SHN_BAD)
8433 {
8434 asection *sec2;
8435
8436 /* Writing this would be a hell of a lot easier if
8437 we had some decent documentation on bfd, and
8438 knew what to expect of the library, and what to
8439 demand of applications. For example, it
8440 appears that `objcopy' might not set the
8441 section of a symbol to be a section that is
8442 actually in the output file. */
8443 sec2 = bfd_get_section_by_name (abfd, sec->name);
8444 if (sec2 != NULL)
8445 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8446 if (shndx == SHN_BAD)
8447 {
8448 /* xgettext:c-format */
8449 _bfd_error_handler
8450 (_("unable to find equivalent output section"
8451 " for symbol '%s' from section '%s'"),
8452 syms[idx]->name ? syms[idx]->name : "<Local sym>",
8453 sec->name);
8454 bfd_set_error (bfd_error_invalid_operation);
8455 goto error_return;
8456 }
8457 }
8458 }
8459
8460 sym.st_shndx = shndx;
8461 }
8462
8463 if ((flags & BSF_THREAD_LOCAL) != 0)
8464 type = STT_TLS;
8465 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8466 type = STT_GNU_IFUNC;
8467 else if ((flags & BSF_FUNCTION) != 0)
8468 type = STT_FUNC;
8469 else if ((flags & BSF_OBJECT) != 0)
8470 type = STT_OBJECT;
8471 else if ((flags & BSF_RELC) != 0)
8472 type = STT_RELC;
8473 else if ((flags & BSF_SRELC) != 0)
8474 type = STT_SRELC;
8475 else
8476 type = STT_NOTYPE;
8477
8478 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8479 type = STT_TLS;
8480
8481 /* Processor-specific types. */
8482 if (type_ptr != NULL
8483 && bed->elf_backend_get_symbol_type)
8484 type = ((*bed->elf_backend_get_symbol_type)
8485 (&type_ptr->internal_elf_sym, type));
8486
8487 if (flags & BSF_SECTION_SYM)
8488 {
8489 if (flags & BSF_GLOBAL)
8490 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8491 else
8492 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8493 }
8494 else if (bfd_is_com_section (syms[idx]->section))
8495 {
8496 if (type != STT_TLS)
8497 {
8498 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8499 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8500 ? STT_COMMON : STT_OBJECT);
8501 else
8502 type = ((flags & BSF_ELF_COMMON) != 0
8503 ? STT_COMMON : STT_OBJECT);
8504 }
8505 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8506 }
8507 else if (bfd_is_und_section (syms[idx]->section))
8508 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
8509 ? STB_WEAK
8510 : STB_GLOBAL),
8511 type);
8512 else if (flags & BSF_FILE)
8513 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
8514 else
8515 {
8516 int bind = STB_LOCAL;
8517
8518 if (flags & BSF_LOCAL)
8519 bind = STB_LOCAL;
8520 else if (flags & BSF_GNU_UNIQUE)
8521 bind = STB_GNU_UNIQUE;
8522 else if (flags & BSF_WEAK)
8523 bind = STB_WEAK;
8524 else if (flags & BSF_GLOBAL)
8525 bind = STB_GLOBAL;
8526
8527 sym.st_info = ELF_ST_INFO (bind, type);
8528 }
8529
8530 if (type_ptr != NULL)
8531 {
8532 sym.st_other = type_ptr->internal_elf_sym.st_other;
8533 sym.st_target_internal
8534 = type_ptr->internal_elf_sym.st_target_internal;
8535 }
8536 else
8537 {
8538 sym.st_other = 0;
8539 sym.st_target_internal = 0;
8540 }
8541
8542 idx++;
8543 symstrtab[idx].sym = sym;
8544 symstrtab[idx].dest_index = outbound_syms_index;
8545
8546 outbound_syms_index++;
8547 }
8548
8549 /* Finalize the .strtab section. */
8550 _bfd_elf_strtab_finalize (stt);
8551
8552 /* Swap out the .strtab section. */
8553 for (idx = 0; idx <= symcount; idx++)
8554 {
8555 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8556 if (elfsym->sym.st_name == (unsigned long) -1)
8557 elfsym->sym.st_name = 0;
8558 else
8559 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8560 elfsym->sym.st_name);
8561 if (info && info->callbacks->ctf_new_symbol)
8562 info->callbacks->ctf_new_symbol (elfsym->dest_index,
8563 &elfsym->sym);
8564
8565 /* Inform the linker of the addition of this symbol. */
8566
8567 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8568 (outbound_syms
8569 + (elfsym->dest_index
8570 * bed->s->sizeof_sym)),
8571 NPTR_ADD (outbound_shndx,
8572 (elfsym->dest_index
8573 * sizeof (Elf_External_Sym_Shndx))));
8574 }
8575 free (symstrtab);
8576
8577 *sttp = stt;
8578 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
8579 symstrtab_hdr->sh_type = SHT_STRTAB;
8580 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
8581 symstrtab_hdr->sh_addr = 0;
8582 symstrtab_hdr->sh_entsize = 0;
8583 symstrtab_hdr->sh_link = 0;
8584 symstrtab_hdr->sh_info = 0;
8585 symstrtab_hdr->sh_addralign = 1;
8586
8587 return true;
8588 }
8589
8590 /* Return the number of bytes required to hold the symtab vector.
8591
8592 Note that we base it on the count plus 1, since we will null terminate
8593 the vector allocated based on this size. However, the ELF symbol table
8594 always has a dummy entry as symbol #0, so it ends up even. */
8595
8596 long
_bfd_elf_get_symtab_upper_bound(bfd * abfd)8597 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
8598 {
8599 bfd_size_type symcount;
8600 long symtab_size;
8601 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8602
8603 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8604 if (symcount > LONG_MAX / sizeof (asymbol *))
8605 {
8606 bfd_set_error (bfd_error_file_too_big);
8607 return -1;
8608 }
8609 symtab_size = symcount * (sizeof (asymbol *));
8610 if (symcount == 0)
8611 symtab_size = sizeof (asymbol *);
8612 else if (!bfd_write_p (abfd))
8613 {
8614 ufile_ptr filesize = bfd_get_file_size (abfd);
8615
8616 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8617 {
8618 bfd_set_error (bfd_error_file_truncated);
8619 return -1;
8620 }
8621 }
8622
8623 return symtab_size;
8624 }
8625
8626 long
_bfd_elf_get_dynamic_symtab_upper_bound(bfd * abfd)8627 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
8628 {
8629 bfd_size_type symcount;
8630 long symtab_size;
8631 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8632
8633 if (elf_dynsymtab (abfd) == 0)
8634 {
8635 bfd_set_error (bfd_error_invalid_operation);
8636 return -1;
8637 }
8638
8639 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
8640 if (symcount > LONG_MAX / sizeof (asymbol *))
8641 {
8642 bfd_set_error (bfd_error_file_too_big);
8643 return -1;
8644 }
8645 symtab_size = symcount * (sizeof (asymbol *));
8646 if (symcount == 0)
8647 symtab_size = sizeof (asymbol *);
8648 else if (!bfd_write_p (abfd))
8649 {
8650 ufile_ptr filesize = bfd_get_file_size (abfd);
8651
8652 if (filesize != 0 && (unsigned long) symtab_size > filesize)
8653 {
8654 bfd_set_error (bfd_error_file_truncated);
8655 return -1;
8656 }
8657 }
8658
8659 return symtab_size;
8660 }
8661
8662 long
_bfd_elf_get_reloc_upper_bound(bfd * abfd,sec_ptr asect)8663 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
8664 {
8665 if (asect->reloc_count != 0 && !bfd_write_p (abfd))
8666 {
8667 /* Sanity check reloc section size. */
8668 struct bfd_elf_section_data *d = elf_section_data (asect);
8669 Elf_Internal_Shdr *rel_hdr = &d->this_hdr;
8670 bfd_size_type ext_rel_size = rel_hdr->sh_size;
8671 ufile_ptr filesize = bfd_get_file_size (abfd);
8672
8673 if (filesize != 0 && ext_rel_size > filesize)
8674 {
8675 bfd_set_error (bfd_error_file_truncated);
8676 return -1;
8677 }
8678 }
8679
8680 #if SIZEOF_LONG == SIZEOF_INT
8681 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
8682 {
8683 bfd_set_error (bfd_error_file_too_big);
8684 return -1;
8685 }
8686 #endif
8687 return (asect->reloc_count + 1L) * sizeof (arelent *);
8688 }
8689
8690 /* Canonicalize the relocs. */
8691
8692 long
_bfd_elf_canonicalize_reloc(bfd * abfd,sec_ptr section,arelent ** relptr,asymbol ** symbols)8693 _bfd_elf_canonicalize_reloc (bfd *abfd,
8694 sec_ptr section,
8695 arelent **relptr,
8696 asymbol **symbols)
8697 {
8698 arelent *tblptr;
8699 unsigned int i;
8700 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8701
8702 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
8703 return -1;
8704
8705 tblptr = section->relocation;
8706 for (i = 0; i < section->reloc_count; i++)
8707 *relptr++ = tblptr++;
8708
8709 *relptr = NULL;
8710
8711 return section->reloc_count;
8712 }
8713
8714 long
_bfd_elf_canonicalize_symtab(bfd * abfd,asymbol ** allocation)8715 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
8716 {
8717 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8718 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
8719
8720 if (symcount >= 0)
8721 abfd->symcount = symcount;
8722 return symcount;
8723 }
8724
8725 long
_bfd_elf_canonicalize_dynamic_symtab(bfd * abfd,asymbol ** allocation)8726 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8727 asymbol **allocation)
8728 {
8729 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8730 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
8731
8732 if (symcount >= 0)
8733 abfd->dynsymcount = symcount;
8734 return symcount;
8735 }
8736
8737 /* Return the size required for the dynamic reloc entries. Any loadable
8738 section that was actually installed in the BFD, and has type SHT_REL
8739 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8740 dynamic reloc section. */
8741
8742 long
_bfd_elf_get_dynamic_reloc_upper_bound(bfd * abfd)8743 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
8744 {
8745 bfd_size_type count, ext_rel_size;
8746 asection *s;
8747
8748 if (elf_dynsymtab (abfd) == 0)
8749 {
8750 bfd_set_error (bfd_error_invalid_operation);
8751 return -1;
8752 }
8753
8754 count = 1;
8755 ext_rel_size = 0;
8756 for (s = abfd->sections; s != NULL; s = s->next)
8757 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8758 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8759 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8760 {
8761 ext_rel_size += s->size;
8762 if (ext_rel_size < s->size)
8763 {
8764 bfd_set_error (bfd_error_file_truncated);
8765 return -1;
8766 }
8767 count += s->size / elf_section_data (s)->this_hdr.sh_entsize;
8768 if (count > LONG_MAX / sizeof (arelent *))
8769 {
8770 bfd_set_error (bfd_error_file_too_big);
8771 return -1;
8772 }
8773 }
8774 if (count > 1 && !bfd_write_p (abfd))
8775 {
8776 /* Sanity check reloc section sizes. */
8777 ufile_ptr filesize = bfd_get_file_size (abfd);
8778 if (filesize != 0 && ext_rel_size > filesize)
8779 {
8780 bfd_set_error (bfd_error_file_truncated);
8781 return -1;
8782 }
8783 }
8784 return count * sizeof (arelent *);
8785 }
8786
8787 /* Canonicalize the dynamic relocation entries. Note that we return the
8788 dynamic relocations as a single block, although they are actually
8789 associated with particular sections; the interface, which was
8790 designed for SunOS style shared libraries, expects that there is only
8791 one set of dynamic relocs. Any loadable section that was actually
8792 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8793 dynamic symbol table, is considered to be a dynamic reloc section. */
8794
8795 long
_bfd_elf_canonicalize_dynamic_reloc(bfd * abfd,arelent ** storage,asymbol ** syms)8796 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8797 arelent **storage,
8798 asymbol **syms)
8799 {
8800 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
8801 asection *s;
8802 long ret;
8803
8804 if (elf_dynsymtab (abfd) == 0)
8805 {
8806 bfd_set_error (bfd_error_invalid_operation);
8807 return -1;
8808 }
8809
8810 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8811 ret = 0;
8812 for (s = abfd->sections; s != NULL; s = s->next)
8813 {
8814 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
8815 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8816 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8817 {
8818 arelent *p;
8819 long count, i;
8820
8821 if (! (*slurp_relocs) (abfd, s, syms, true))
8822 return -1;
8823 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
8824 p = s->relocation;
8825 for (i = 0; i < count; i++)
8826 *storage++ = p++;
8827 ret += count;
8828 }
8829 }
8830
8831 *storage = NULL;
8832
8833 return ret;
8834 }
8835
8836 /* Read in the version information. */
8837
8838 bool
_bfd_elf_slurp_version_tables(bfd * abfd,bool default_imported_symver)8839 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
8840 {
8841 bfd_byte *contents = NULL;
8842 unsigned int freeidx = 0;
8843 size_t amt;
8844
8845 if (elf_dynverref (abfd) != 0)
8846 {
8847 Elf_Internal_Shdr *hdr;
8848 Elf_External_Verneed *everneed;
8849 Elf_Internal_Verneed *iverneed;
8850 unsigned int i;
8851 bfd_byte *contents_end;
8852
8853 hdr = &elf_tdata (abfd)->dynverref_hdr;
8854
8855 if (hdr->sh_info == 0
8856 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
8857 {
8858 error_return_bad_verref:
8859 _bfd_error_handler
8860 (_("%pB: .gnu.version_r invalid entry"), abfd);
8861 bfd_set_error (bfd_error_bad_value);
8862 error_return_verref:
8863 elf_tdata (abfd)->verref = NULL;
8864 elf_tdata (abfd)->cverrefs = 0;
8865 goto error_return;
8866 }
8867
8868 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
8869 goto error_return_verref;
8870 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
8871 if (contents == NULL)
8872 goto error_return_verref;
8873
8874 if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt))
8875 {
8876 bfd_set_error (bfd_error_file_too_big);
8877 goto error_return_verref;
8878 }
8879 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt);
8880 if (elf_tdata (abfd)->verref == NULL)
8881 goto error_return_verref;
8882
8883 BFD_ASSERT (sizeof (Elf_External_Verneed)
8884 == sizeof (Elf_External_Vernaux));
8885 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
8886 everneed = (Elf_External_Verneed *) contents;
8887 iverneed = elf_tdata (abfd)->verref;
8888 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8889 {
8890 Elf_External_Vernaux *evernaux;
8891 Elf_Internal_Vernaux *ivernaux;
8892 unsigned int j;
8893
8894 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8895
8896 iverneed->vn_bfd = abfd;
8897
8898 iverneed->vn_filename =
8899 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8900 iverneed->vn_file);
8901 if (iverneed->vn_filename == NULL)
8902 goto error_return_bad_verref;
8903
8904 if (iverneed->vn_cnt == 0)
8905 iverneed->vn_auxptr = NULL;
8906 else
8907 {
8908 if (_bfd_mul_overflow (iverneed->vn_cnt,
8909 sizeof (Elf_Internal_Vernaux), &amt))
8910 {
8911 bfd_set_error (bfd_error_file_too_big);
8912 goto error_return_verref;
8913 }
8914 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
8915 bfd_alloc (abfd, amt);
8916 if (iverneed->vn_auxptr == NULL)
8917 goto error_return_verref;
8918 }
8919
8920 if (iverneed->vn_aux
8921 > (size_t) (contents_end - (bfd_byte *) everneed))
8922 goto error_return_bad_verref;
8923
8924 evernaux = ((Elf_External_Vernaux *)
8925 ((bfd_byte *) everneed + iverneed->vn_aux));
8926 ivernaux = iverneed->vn_auxptr;
8927 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8928 {
8929 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8930
8931 ivernaux->vna_nodename =
8932 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8933 ivernaux->vna_name);
8934 if (ivernaux->vna_nodename == NULL)
8935 goto error_return_bad_verref;
8936
8937 if (ivernaux->vna_other > freeidx)
8938 freeidx = ivernaux->vna_other;
8939
8940 ivernaux->vna_nextptr = NULL;
8941 if (ivernaux->vna_next == 0)
8942 {
8943 iverneed->vn_cnt = j + 1;
8944 break;
8945 }
8946 if (j + 1 < iverneed->vn_cnt)
8947 ivernaux->vna_nextptr = ivernaux + 1;
8948
8949 if (ivernaux->vna_next
8950 > (size_t) (contents_end - (bfd_byte *) evernaux))
8951 goto error_return_bad_verref;
8952
8953 evernaux = ((Elf_External_Vernaux *)
8954 ((bfd_byte *) evernaux + ivernaux->vna_next));
8955 }
8956
8957 iverneed->vn_nextref = NULL;
8958 if (iverneed->vn_next == 0)
8959 break;
8960 if (i + 1 < hdr->sh_info)
8961 iverneed->vn_nextref = iverneed + 1;
8962
8963 if (iverneed->vn_next
8964 > (size_t) (contents_end - (bfd_byte *) everneed))
8965 goto error_return_bad_verref;
8966
8967 everneed = ((Elf_External_Verneed *)
8968 ((bfd_byte *) everneed + iverneed->vn_next));
8969 }
8970 elf_tdata (abfd)->cverrefs = i;
8971
8972 free (contents);
8973 contents = NULL;
8974 }
8975
8976 if (elf_dynverdef (abfd) != 0)
8977 {
8978 Elf_Internal_Shdr *hdr;
8979 Elf_External_Verdef *everdef;
8980 Elf_Internal_Verdef *iverdef;
8981 Elf_Internal_Verdef *iverdefarr;
8982 Elf_Internal_Verdef iverdefmem;
8983 unsigned int i;
8984 unsigned int maxidx;
8985 bfd_byte *contents_end_def, *contents_end_aux;
8986
8987 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8988
8989 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8990 {
8991 error_return_bad_verdef:
8992 _bfd_error_handler
8993 (_("%pB: .gnu.version_d invalid entry"), abfd);
8994 bfd_set_error (bfd_error_bad_value);
8995 error_return_verdef:
8996 elf_tdata (abfd)->verdef = NULL;
8997 elf_tdata (abfd)->cverdefs = 0;
8998 goto error_return;
8999 }
9000
9001 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9002 goto error_return_verdef;
9003 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size);
9004 if (contents == NULL)
9005 goto error_return_verdef;
9006
9007 BFD_ASSERT (sizeof (Elf_External_Verdef)
9008 >= sizeof (Elf_External_Verdaux));
9009 contents_end_def = contents + hdr->sh_size
9010 - sizeof (Elf_External_Verdef);
9011 contents_end_aux = contents + hdr->sh_size
9012 - sizeof (Elf_External_Verdaux);
9013
9014 /* We know the number of entries in the section but not the maximum
9015 index. Therefore we have to run through all entries and find
9016 the maximum. */
9017 everdef = (Elf_External_Verdef *) contents;
9018 maxidx = 0;
9019 for (i = 0; i < hdr->sh_info; ++i)
9020 {
9021 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9022
9023 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9024 goto error_return_bad_verdef;
9025 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9026 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9027
9028 if (iverdefmem.vd_next == 0)
9029 break;
9030
9031 if (iverdefmem.vd_next
9032 > (size_t) (contents_end_def - (bfd_byte *) everdef))
9033 goto error_return_bad_verdef;
9034
9035 everdef = ((Elf_External_Verdef *)
9036 ((bfd_byte *) everdef + iverdefmem.vd_next));
9037 }
9038
9039 if (default_imported_symver)
9040 {
9041 if (freeidx > maxidx)
9042 maxidx = ++freeidx;
9043 else
9044 freeidx = ++maxidx;
9045 }
9046 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9047 {
9048 bfd_set_error (bfd_error_file_too_big);
9049 goto error_return_verdef;
9050 }
9051 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9052 if (elf_tdata (abfd)->verdef == NULL)
9053 goto error_return_verdef;
9054
9055 elf_tdata (abfd)->cverdefs = maxidx;
9056
9057 everdef = (Elf_External_Verdef *) contents;
9058 iverdefarr = elf_tdata (abfd)->verdef;
9059 for (i = 0; i < hdr->sh_info; i++)
9060 {
9061 Elf_External_Verdaux *everdaux;
9062 Elf_Internal_Verdaux *iverdaux;
9063 unsigned int j;
9064
9065 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9066
9067 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9068 goto error_return_bad_verdef;
9069
9070 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9071 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9072
9073 iverdef->vd_bfd = abfd;
9074
9075 if (iverdef->vd_cnt == 0)
9076 iverdef->vd_auxptr = NULL;
9077 else
9078 {
9079 if (_bfd_mul_overflow (iverdef->vd_cnt,
9080 sizeof (Elf_Internal_Verdaux), &amt))
9081 {
9082 bfd_set_error (bfd_error_file_too_big);
9083 goto error_return_verdef;
9084 }
9085 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9086 bfd_alloc (abfd, amt);
9087 if (iverdef->vd_auxptr == NULL)
9088 goto error_return_verdef;
9089 }
9090
9091 if (iverdef->vd_aux
9092 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9093 goto error_return_bad_verdef;
9094
9095 everdaux = ((Elf_External_Verdaux *)
9096 ((bfd_byte *) everdef + iverdef->vd_aux));
9097 iverdaux = iverdef->vd_auxptr;
9098 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9099 {
9100 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9101
9102 iverdaux->vda_nodename =
9103 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9104 iverdaux->vda_name);
9105 if (iverdaux->vda_nodename == NULL)
9106 goto error_return_bad_verdef;
9107
9108 iverdaux->vda_nextptr = NULL;
9109 if (iverdaux->vda_next == 0)
9110 {
9111 iverdef->vd_cnt = j + 1;
9112 break;
9113 }
9114 if (j + 1 < iverdef->vd_cnt)
9115 iverdaux->vda_nextptr = iverdaux + 1;
9116
9117 if (iverdaux->vda_next
9118 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9119 goto error_return_bad_verdef;
9120
9121 everdaux = ((Elf_External_Verdaux *)
9122 ((bfd_byte *) everdaux + iverdaux->vda_next));
9123 }
9124
9125 iverdef->vd_nodename = NULL;
9126 if (iverdef->vd_cnt)
9127 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9128
9129 iverdef->vd_nextdef = NULL;
9130 if (iverdef->vd_next == 0)
9131 break;
9132 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9133 iverdef->vd_nextdef = iverdef + 1;
9134
9135 everdef = ((Elf_External_Verdef *)
9136 ((bfd_byte *) everdef + iverdef->vd_next));
9137 }
9138
9139 free (contents);
9140 contents = NULL;
9141 }
9142 else if (default_imported_symver)
9143 {
9144 if (freeidx < 3)
9145 freeidx = 3;
9146 else
9147 freeidx++;
9148
9149 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9150 {
9151 bfd_set_error (bfd_error_file_too_big);
9152 goto error_return;
9153 }
9154 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9155 if (elf_tdata (abfd)->verdef == NULL)
9156 goto error_return;
9157
9158 elf_tdata (abfd)->cverdefs = freeidx;
9159 }
9160
9161 /* Create a default version based on the soname. */
9162 if (default_imported_symver)
9163 {
9164 Elf_Internal_Verdef *iverdef;
9165 Elf_Internal_Verdaux *iverdaux;
9166
9167 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9168
9169 iverdef->vd_version = VER_DEF_CURRENT;
9170 iverdef->vd_flags = 0;
9171 iverdef->vd_ndx = freeidx;
9172 iverdef->vd_cnt = 1;
9173
9174 iverdef->vd_bfd = abfd;
9175
9176 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9177 if (iverdef->vd_nodename == NULL)
9178 goto error_return_verdef;
9179 iverdef->vd_nextdef = NULL;
9180 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9181 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9182 if (iverdef->vd_auxptr == NULL)
9183 goto error_return_verdef;
9184
9185 iverdaux = iverdef->vd_auxptr;
9186 iverdaux->vda_nodename = iverdef->vd_nodename;
9187 }
9188
9189 return true;
9190
9191 error_return:
9192 free (contents);
9193 return false;
9194 }
9195
9196 asymbol *
_bfd_elf_make_empty_symbol(bfd * abfd)9197 _bfd_elf_make_empty_symbol (bfd *abfd)
9198 {
9199 elf_symbol_type *newsym;
9200
9201 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9202 if (!newsym)
9203 return NULL;
9204 newsym->symbol.the_bfd = abfd;
9205 return &newsym->symbol;
9206 }
9207
9208 void
_bfd_elf_get_symbol_info(bfd * abfd ATTRIBUTE_UNUSED,asymbol * symbol,symbol_info * ret)9209 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9210 asymbol *symbol,
9211 symbol_info *ret)
9212 {
9213 bfd_symbol_info (symbol, ret);
9214 }
9215
9216 /* Return whether a symbol name implies a local symbol. Most targets
9217 use this function for the is_local_label_name entry point, but some
9218 override it. */
9219
9220 bool
_bfd_elf_is_local_label_name(bfd * abfd ATTRIBUTE_UNUSED,const char * name)9221 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9222 const char *name)
9223 {
9224 /* Normal local symbols start with ``.L''. */
9225 if (name[0] == '.' && name[1] == 'L')
9226 return true;
9227
9228 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9229 DWARF debugging symbols starting with ``..''. */
9230 if (name[0] == '.' && name[1] == '.')
9231 return true;
9232
9233 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9234 emitting DWARF debugging output. I suspect this is actually a
9235 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9236 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9237 underscore to be emitted on some ELF targets). For ease of use,
9238 we treat such symbols as local. */
9239 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9240 return true;
9241
9242 /* Treat assembler generated fake symbols, dollar local labels and
9243 forward-backward labels (aka local labels) as locals.
9244 These labels have the form:
9245
9246 L0^A.* (fake symbols)
9247
9248 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9249
9250 Versions which start with .L will have already been matched above,
9251 so we only need to match the rest. */
9252 if (name[0] == 'L' && ISDIGIT (name[1]))
9253 {
9254 bool ret = false;
9255 const char * p;
9256 char c;
9257
9258 for (p = name + 2; (c = *p); p++)
9259 {
9260 if (c == 1 || c == 2)
9261 {
9262 if (c == 1 && p == name + 2)
9263 /* A fake symbol. */
9264 return true;
9265
9266 /* FIXME: We are being paranoid here and treating symbols like
9267 L0^Bfoo as if there were non-local, on the grounds that the
9268 assembler will never generate them. But can any symbol
9269 containing an ASCII value in the range 1-31 ever be anything
9270 other than some kind of local ? */
9271 ret = true;
9272 }
9273
9274 if (! ISDIGIT (c))
9275 {
9276 ret = false;
9277 break;
9278 }
9279 }
9280 return ret;
9281 }
9282
9283 return false;
9284 }
9285
9286 alent *
_bfd_elf_get_lineno(bfd * abfd ATTRIBUTE_UNUSED,asymbol * symbol ATTRIBUTE_UNUSED)9287 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9288 asymbol *symbol ATTRIBUTE_UNUSED)
9289 {
9290 abort ();
9291 return NULL;
9292 }
9293
9294 bool
_bfd_elf_set_arch_mach(bfd * abfd,enum bfd_architecture arch,unsigned long machine)9295 _bfd_elf_set_arch_mach (bfd *abfd,
9296 enum bfd_architecture arch,
9297 unsigned long machine)
9298 {
9299 /* If this isn't the right architecture for this backend, and this
9300 isn't the generic backend, fail. */
9301 if (arch != get_elf_backend_data (abfd)->arch
9302 && arch != bfd_arch_unknown
9303 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9304 return false;
9305
9306 return bfd_default_set_arch_mach (abfd, arch, machine);
9307 }
9308
9309 /* Find the nearest line to a particular section and offset,
9310 for error reporting. */
9311
9312 bool
_bfd_elf_find_nearest_line(bfd * abfd,asymbol ** symbols,asection * section,bfd_vma offset,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * line_ptr,unsigned int * discriminator_ptr)9313 _bfd_elf_find_nearest_line (bfd *abfd,
9314 asymbol **symbols,
9315 asection *section,
9316 bfd_vma offset,
9317 const char **filename_ptr,
9318 const char **functionname_ptr,
9319 unsigned int *line_ptr,
9320 unsigned int *discriminator_ptr)
9321 {
9322 bool found;
9323
9324 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
9325 filename_ptr, functionname_ptr,
9326 line_ptr, discriminator_ptr,
9327 dwarf_debug_sections,
9328 &elf_tdata (abfd)->dwarf2_find_line_info))
9329 return true;
9330
9331 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9332 filename_ptr, functionname_ptr, line_ptr))
9333 {
9334 if (!*functionname_ptr)
9335 _bfd_elf_find_function (abfd, symbols, section, offset,
9336 *filename_ptr ? NULL : filename_ptr,
9337 functionname_ptr);
9338 return true;
9339 }
9340
9341 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9342 &found, filename_ptr,
9343 functionname_ptr, line_ptr,
9344 &elf_tdata (abfd)->line_info))
9345 return false;
9346 if (found && (*functionname_ptr || *line_ptr))
9347 return true;
9348
9349 if (symbols == NULL)
9350 return false;
9351
9352 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9353 filename_ptr, functionname_ptr))
9354 return false;
9355
9356 *line_ptr = 0;
9357 return true;
9358 }
9359
9360 /* Find the line for a symbol. */
9361
9362 bool
_bfd_elf_find_line(bfd * abfd,asymbol ** symbols,asymbol * symbol,const char ** filename_ptr,unsigned int * line_ptr)9363 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9364 const char **filename_ptr, unsigned int *line_ptr)
9365 {
9366 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9367 filename_ptr, NULL, line_ptr, NULL,
9368 dwarf_debug_sections,
9369 &elf_tdata (abfd)->dwarf2_find_line_info);
9370 }
9371
9372 /* After a call to bfd_find_nearest_line, successive calls to
9373 bfd_find_inliner_info can be used to get source information about
9374 each level of function inlining that terminated at the address
9375 passed to bfd_find_nearest_line. Currently this is only supported
9376 for DWARF2 with appropriate DWARF3 extensions. */
9377
9378 bool
_bfd_elf_find_inliner_info(bfd * abfd,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * line_ptr)9379 _bfd_elf_find_inliner_info (bfd *abfd,
9380 const char **filename_ptr,
9381 const char **functionname_ptr,
9382 unsigned int *line_ptr)
9383 {
9384 bool found;
9385 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9386 functionname_ptr, line_ptr,
9387 & elf_tdata (abfd)->dwarf2_find_line_info);
9388 return found;
9389 }
9390
9391 int
_bfd_elf_sizeof_headers(bfd * abfd,struct bfd_link_info * info)9392 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9393 {
9394 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9395 int ret = bed->s->sizeof_ehdr;
9396
9397 if (!bfd_link_relocatable (info))
9398 {
9399 bfd_size_type phdr_size = elf_program_header_size (abfd);
9400
9401 if (phdr_size == (bfd_size_type) -1)
9402 {
9403 struct elf_segment_map *m;
9404
9405 phdr_size = 0;
9406 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
9407 phdr_size += bed->s->sizeof_phdr;
9408
9409 if (phdr_size == 0)
9410 phdr_size = get_program_header_size (abfd, info);
9411 }
9412
9413 elf_program_header_size (abfd) = phdr_size;
9414 ret += phdr_size;
9415 }
9416
9417 return ret;
9418 }
9419
9420 bool
_bfd_elf_set_section_contents(bfd * abfd,sec_ptr section,const void * location,file_ptr offset,bfd_size_type count)9421 _bfd_elf_set_section_contents (bfd *abfd,
9422 sec_ptr section,
9423 const void *location,
9424 file_ptr offset,
9425 bfd_size_type count)
9426 {
9427 Elf_Internal_Shdr *hdr;
9428
9429 if (! abfd->output_has_begun
9430 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
9431 return false;
9432
9433 if (!count)
9434 return true;
9435
9436 hdr = &elf_section_data (section)->this_hdr;
9437 if (hdr->sh_offset == (file_ptr) -1)
9438 {
9439 unsigned char *contents;
9440
9441 if (bfd_section_is_ctf (section))
9442 /* Nothing to do with this section: the contents are generated
9443 later. */
9444 return true;
9445
9446 if ((section->flags & SEC_ELF_COMPRESS) == 0)
9447 {
9448 _bfd_error_handler
9449 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9450 abfd, section);
9451 bfd_set_error (bfd_error_invalid_operation);
9452 return false;
9453 }
9454
9455 if ((offset + count) > hdr->sh_size)
9456 {
9457 _bfd_error_handler
9458 (_("%pB:%pA: error: attempting to write over the end of the section"),
9459 abfd, section);
9460
9461 bfd_set_error (bfd_error_invalid_operation);
9462 return false;
9463 }
9464
9465 contents = hdr->contents;
9466 if (contents == NULL)
9467 {
9468 _bfd_error_handler
9469 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9470 abfd, section);
9471
9472 bfd_set_error (bfd_error_invalid_operation);
9473 return false;
9474 }
9475
9476 memcpy (contents + offset, location, count);
9477 return true;
9478 }
9479
9480 return _bfd_generic_set_section_contents (abfd, section,
9481 location, offset, count);
9482 }
9483
9484 bool
_bfd_elf_no_info_to_howto(bfd * abfd ATTRIBUTE_UNUSED,arelent * cache_ptr ATTRIBUTE_UNUSED,Elf_Internal_Rela * dst ATTRIBUTE_UNUSED)9485 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
9486 arelent *cache_ptr ATTRIBUTE_UNUSED,
9487 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
9488 {
9489 abort ();
9490 return false;
9491 }
9492
9493 /* Try to convert a non-ELF reloc into an ELF one. */
9494
9495 bool
_bfd_elf_validate_reloc(bfd * abfd,arelent * areloc)9496 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
9497 {
9498 /* Check whether we really have an ELF howto. */
9499
9500 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
9501 {
9502 bfd_reloc_code_real_type code;
9503 reloc_howto_type *howto;
9504
9505 /* Alien reloc: Try to determine its type to replace it with an
9506 equivalent ELF reloc. */
9507
9508 if (areloc->howto->pc_relative)
9509 {
9510 switch (areloc->howto->bitsize)
9511 {
9512 case 8:
9513 code = BFD_RELOC_8_PCREL;
9514 break;
9515 case 12:
9516 code = BFD_RELOC_12_PCREL;
9517 break;
9518 case 16:
9519 code = BFD_RELOC_16_PCREL;
9520 break;
9521 case 24:
9522 code = BFD_RELOC_24_PCREL;
9523 break;
9524 case 32:
9525 code = BFD_RELOC_32_PCREL;
9526 break;
9527 case 64:
9528 code = BFD_RELOC_64_PCREL;
9529 break;
9530 default:
9531 goto fail;
9532 }
9533
9534 howto = bfd_reloc_type_lookup (abfd, code);
9535
9536 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
9537 {
9538 if (howto->pcrel_offset)
9539 areloc->addend += areloc->address;
9540 else
9541 areloc->addend -= areloc->address; /* addend is unsigned!! */
9542 }
9543 }
9544 else
9545 {
9546 switch (areloc->howto->bitsize)
9547 {
9548 case 8:
9549 code = BFD_RELOC_8;
9550 break;
9551 case 14:
9552 code = BFD_RELOC_14;
9553 break;
9554 case 16:
9555 code = BFD_RELOC_16;
9556 break;
9557 case 26:
9558 code = BFD_RELOC_26;
9559 break;
9560 case 32:
9561 code = BFD_RELOC_32;
9562 break;
9563 case 64:
9564 code = BFD_RELOC_64;
9565 break;
9566 default:
9567 goto fail;
9568 }
9569
9570 howto = bfd_reloc_type_lookup (abfd, code);
9571 }
9572
9573 if (howto)
9574 areloc->howto = howto;
9575 else
9576 goto fail;
9577 }
9578
9579 return true;
9580
9581 fail:
9582 /* xgettext:c-format */
9583 _bfd_error_handler (_("%pB: %s unsupported"),
9584 abfd, areloc->howto->name);
9585 bfd_set_error (bfd_error_sorry);
9586 return false;
9587 }
9588
9589 bool
_bfd_elf_close_and_cleanup(bfd * abfd)9590 _bfd_elf_close_and_cleanup (bfd *abfd)
9591 {
9592 struct elf_obj_tdata *tdata = elf_tdata (abfd);
9593 if (tdata != NULL
9594 && (bfd_get_format (abfd) == bfd_object
9595 || bfd_get_format (abfd) == bfd_core))
9596 {
9597 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
9598 _bfd_elf_strtab_free (elf_shstrtab (abfd));
9599 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
9600 }
9601
9602 return _bfd_generic_close_and_cleanup (abfd);
9603 }
9604
9605 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9606 in the relocation's offset. Thus we cannot allow any sort of sanity
9607 range-checking to interfere. There is nothing else to do in processing
9608 this reloc. */
9609
9610 bfd_reloc_status_type
_bfd_elf_rel_vtable_reloc_fn(bfd * abfd ATTRIBUTE_UNUSED,arelent * re ATTRIBUTE_UNUSED,struct bfd_symbol * symbol ATTRIBUTE_UNUSED,void * data ATTRIBUTE_UNUSED,asection * is ATTRIBUTE_UNUSED,bfd * obfd ATTRIBUTE_UNUSED,char ** errmsg ATTRIBUTE_UNUSED)9611 _bfd_elf_rel_vtable_reloc_fn
9612 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
9613 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
9614 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
9615 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
9616 {
9617 return bfd_reloc_ok;
9618 }
9619
9620 /* Elf core file support. Much of this only works on native
9621 toolchains, since we rely on knowing the
9622 machine-dependent procfs structure in order to pick
9623 out details about the corefile. */
9624
9625 #ifdef HAVE_SYS_PROCFS_H
9626 # include <sys/procfs.h>
9627 #endif
9628
9629 /* Return a PID that identifies a "thread" for threaded cores, or the
9630 PID of the main process for non-threaded cores. */
9631
9632 static int
elfcore_make_pid(bfd * abfd)9633 elfcore_make_pid (bfd *abfd)
9634 {
9635 int pid;
9636
9637 pid = elf_tdata (abfd)->core->lwpid;
9638 if (pid == 0)
9639 pid = elf_tdata (abfd)->core->pid;
9640
9641 return pid;
9642 }
9643
9644 /* If there isn't a section called NAME, make one, using
9645 data from SECT. Note, this function will generate a
9646 reference to NAME, so you shouldn't deallocate or
9647 overwrite it. */
9648
9649 static bool
elfcore_maybe_make_sect(bfd * abfd,char * name,asection * sect)9650 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
9651 {
9652 asection *sect2;
9653
9654 if (bfd_get_section_by_name (abfd, name) != NULL)
9655 return true;
9656
9657 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
9658 if (sect2 == NULL)
9659 return false;
9660
9661 sect2->size = sect->size;
9662 sect2->filepos = sect->filepos;
9663 sect2->alignment_power = sect->alignment_power;
9664 return true;
9665 }
9666
9667 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9668 actually creates up to two pseudosections:
9669 - For the single-threaded case, a section named NAME, unless
9670 such a section already exists.
9671 - For the multi-threaded case, a section named "NAME/PID", where
9672 PID is elfcore_make_pid (abfd).
9673 Both pseudosections have identical contents. */
9674 bool
_bfd_elfcore_make_pseudosection(bfd * abfd,char * name,size_t size,ufile_ptr filepos)9675 _bfd_elfcore_make_pseudosection (bfd *abfd,
9676 char *name,
9677 size_t size,
9678 ufile_ptr filepos)
9679 {
9680 char buf[100];
9681 char *threaded_name;
9682 size_t len;
9683 asection *sect;
9684
9685 /* Build the section name. */
9686
9687 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
9688 len = strlen (buf) + 1;
9689 threaded_name = (char *) bfd_alloc (abfd, len);
9690 if (threaded_name == NULL)
9691 return false;
9692 memcpy (threaded_name, buf, len);
9693
9694 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9695 SEC_HAS_CONTENTS);
9696 if (sect == NULL)
9697 return false;
9698 sect->size = size;
9699 sect->filepos = filepos;
9700 sect->alignment_power = 2;
9701
9702 return elfcore_maybe_make_sect (abfd, name, sect);
9703 }
9704
9705 static bool
elfcore_make_auxv_note_section(bfd * abfd,Elf_Internal_Note * note,size_t offs)9706 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
9707 size_t offs)
9708 {
9709 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9710 SEC_HAS_CONTENTS);
9711
9712 if (sect == NULL)
9713 return false;
9714
9715 sect->size = note->descsz - offs;
9716 sect->filepos = note->descpos + offs;
9717 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9718
9719 return true;
9720 }
9721
9722 /* prstatus_t exists on:
9723 solaris 2.5+
9724 linux 2.[01] + glibc
9725 unixware 4.2
9726 */
9727
9728 #if defined (HAVE_PRSTATUS_T)
9729
9730 static bool
elfcore_grok_prstatus(bfd * abfd,Elf_Internal_Note * note)9731 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
9732 {
9733 size_t size;
9734 int offset;
9735
9736 if (note->descsz == sizeof (prstatus_t))
9737 {
9738 prstatus_t prstat;
9739
9740 size = sizeof (prstat.pr_reg);
9741 offset = offsetof (prstatus_t, pr_reg);
9742 memcpy (&prstat, note->descdata, sizeof (prstat));
9743
9744 /* Do not overwrite the core signal if it
9745 has already been set by another thread. */
9746 if (elf_tdata (abfd)->core->signal == 0)
9747 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9748 if (elf_tdata (abfd)->core->pid == 0)
9749 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9750
9751 /* pr_who exists on:
9752 solaris 2.5+
9753 unixware 4.2
9754 pr_who doesn't exist on:
9755 linux 2.[01]
9756 */
9757 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9758 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9759 #else
9760 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9761 #endif
9762 }
9763 #if defined (HAVE_PRSTATUS32_T)
9764 else if (note->descsz == sizeof (prstatus32_t))
9765 {
9766 /* 64-bit host, 32-bit corefile */
9767 prstatus32_t prstat;
9768
9769 size = sizeof (prstat.pr_reg);
9770 offset = offsetof (prstatus32_t, pr_reg);
9771 memcpy (&prstat, note->descdata, sizeof (prstat));
9772
9773 /* Do not overwrite the core signal if it
9774 has already been set by another thread. */
9775 if (elf_tdata (abfd)->core->signal == 0)
9776 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9777 if (elf_tdata (abfd)->core->pid == 0)
9778 elf_tdata (abfd)->core->pid = prstat.pr_pid;
9779
9780 /* pr_who exists on:
9781 solaris 2.5+
9782 unixware 4.2
9783 pr_who doesn't exist on:
9784 linux 2.[01]
9785 */
9786 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9787 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
9788 #else
9789 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
9790 #endif
9791 }
9792 #endif /* HAVE_PRSTATUS32_T */
9793 else
9794 {
9795 /* Fail - we don't know how to handle any other
9796 note size (ie. data object type). */
9797 return true;
9798 }
9799
9800 /* Make a ".reg/999" section and a ".reg" section. */
9801 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9802 size, note->descpos + offset);
9803 }
9804 #endif /* defined (HAVE_PRSTATUS_T) */
9805
9806 /* Create a pseudosection containing the exact contents of NOTE. */
9807 static bool
elfcore_make_note_pseudosection(bfd * abfd,char * name,Elf_Internal_Note * note)9808 elfcore_make_note_pseudosection (bfd *abfd,
9809 char *name,
9810 Elf_Internal_Note *note)
9811 {
9812 return _bfd_elfcore_make_pseudosection (abfd, name,
9813 note->descsz, note->descpos);
9814 }
9815
9816 /* There isn't a consistent prfpregset_t across platforms,
9817 but it doesn't matter, because we don't have to pick this
9818 data structure apart. */
9819
9820 static bool
elfcore_grok_prfpreg(bfd * abfd,Elf_Internal_Note * note)9821 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
9822 {
9823 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9824 }
9825
9826 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9827 type of NT_PRXFPREG. Just include the whole note's contents
9828 literally. */
9829
9830 static bool
elfcore_grok_prxfpreg(bfd * abfd,Elf_Internal_Note * note)9831 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
9832 {
9833 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9834 }
9835
9836 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9837 with a note type of NT_X86_XSTATE. Just include the whole note's
9838 contents literally. */
9839
9840 static bool
elfcore_grok_xstatereg(bfd * abfd,Elf_Internal_Note * note)9841 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9842 {
9843 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9844 }
9845
9846 static bool
elfcore_grok_ppc_vmx(bfd * abfd,Elf_Internal_Note * note)9847 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9848 {
9849 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9850 }
9851
9852 static bool
elfcore_grok_ppc_vsx(bfd * abfd,Elf_Internal_Note * note)9853 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9854 {
9855 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9856 }
9857
9858 static bool
elfcore_grok_ppc_tar(bfd * abfd,Elf_Internal_Note * note)9859 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
9860 {
9861 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note);
9862 }
9863
9864 static bool
elfcore_grok_ppc_ppr(bfd * abfd,Elf_Internal_Note * note)9865 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
9866 {
9867 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note);
9868 }
9869
9870 static bool
elfcore_grok_ppc_dscr(bfd * abfd,Elf_Internal_Note * note)9871 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
9872 {
9873 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note);
9874 }
9875
9876 static bool
elfcore_grok_ppc_ebb(bfd * abfd,Elf_Internal_Note * note)9877 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
9878 {
9879 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note);
9880 }
9881
9882 static bool
elfcore_grok_ppc_pmu(bfd * abfd,Elf_Internal_Note * note)9883 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
9884 {
9885 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note);
9886 }
9887
9888 static bool
elfcore_grok_ppc_tm_cgpr(bfd * abfd,Elf_Internal_Note * note)9889 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
9890 {
9891 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note);
9892 }
9893
9894 static bool
elfcore_grok_ppc_tm_cfpr(bfd * abfd,Elf_Internal_Note * note)9895 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
9896 {
9897 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note);
9898 }
9899
9900 static bool
elfcore_grok_ppc_tm_cvmx(bfd * abfd,Elf_Internal_Note * note)9901 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
9902 {
9903 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note);
9904 }
9905
9906 static bool
elfcore_grok_ppc_tm_cvsx(bfd * abfd,Elf_Internal_Note * note)9907 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
9908 {
9909 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note);
9910 }
9911
9912 static bool
elfcore_grok_ppc_tm_spr(bfd * abfd,Elf_Internal_Note * note)9913 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
9914 {
9915 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note);
9916 }
9917
9918 static bool
elfcore_grok_ppc_tm_ctar(bfd * abfd,Elf_Internal_Note * note)9919 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
9920 {
9921 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note);
9922 }
9923
9924 static bool
elfcore_grok_ppc_tm_cppr(bfd * abfd,Elf_Internal_Note * note)9925 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
9926 {
9927 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note);
9928 }
9929
9930 static bool
elfcore_grok_ppc_tm_cdscr(bfd * abfd,Elf_Internal_Note * note)9931 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
9932 {
9933 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note);
9934 }
9935
9936 static bool
elfcore_grok_s390_high_gprs(bfd * abfd,Elf_Internal_Note * note)9937 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9938 {
9939 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9940 }
9941
9942 static bool
elfcore_grok_s390_timer(bfd * abfd,Elf_Internal_Note * note)9943 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9944 {
9945 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9946 }
9947
9948 static bool
elfcore_grok_s390_todcmp(bfd * abfd,Elf_Internal_Note * note)9949 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9950 {
9951 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9952 }
9953
9954 static bool
elfcore_grok_s390_todpreg(bfd * abfd,Elf_Internal_Note * note)9955 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9956 {
9957 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9958 }
9959
9960 static bool
elfcore_grok_s390_ctrs(bfd * abfd,Elf_Internal_Note * note)9961 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9962 {
9963 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9964 }
9965
9966 static bool
elfcore_grok_s390_prefix(bfd * abfd,Elf_Internal_Note * note)9967 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9968 {
9969 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9970 }
9971
9972 static bool
elfcore_grok_s390_last_break(bfd * abfd,Elf_Internal_Note * note)9973 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9974 {
9975 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9976 }
9977
9978 static bool
elfcore_grok_s390_system_call(bfd * abfd,Elf_Internal_Note * note)9979 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9980 {
9981 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9982 }
9983
9984 static bool
elfcore_grok_s390_tdb(bfd * abfd,Elf_Internal_Note * note)9985 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9986 {
9987 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9988 }
9989
9990 static bool
elfcore_grok_s390_vxrs_low(bfd * abfd,Elf_Internal_Note * note)9991 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9992 {
9993 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9994 }
9995
9996 static bool
elfcore_grok_s390_vxrs_high(bfd * abfd,Elf_Internal_Note * note)9997 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9998 {
9999 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
10000 }
10001
10002 static bool
elfcore_grok_s390_gs_cb(bfd * abfd,Elf_Internal_Note * note)10003 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10004 {
10005 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
10006 }
10007
10008 static bool
elfcore_grok_s390_gs_bc(bfd * abfd,Elf_Internal_Note * note)10009 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10010 {
10011 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
10012 }
10013
10014 static bool
elfcore_grok_arm_vfp(bfd * abfd,Elf_Internal_Note * note)10015 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10016 {
10017 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
10018 }
10019
10020 static bool
elfcore_grok_aarch_tls(bfd * abfd,Elf_Internal_Note * note)10021 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10022 {
10023 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
10024 }
10025
10026 static bool
elfcore_grok_aarch_hw_break(bfd * abfd,Elf_Internal_Note * note)10027 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10028 {
10029 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
10030 }
10031
10032 static bool
elfcore_grok_aarch_hw_watch(bfd * abfd,Elf_Internal_Note * note)10033 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10034 {
10035 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
10036 }
10037
10038 static bool
elfcore_grok_aarch_sve(bfd * abfd,Elf_Internal_Note * note)10039 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10040 {
10041 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note);
10042 }
10043
10044 static bool
elfcore_grok_aarch_pauth(bfd * abfd,Elf_Internal_Note * note)10045 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10046 {
10047 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note);
10048 }
10049
10050 static bool
elfcore_grok_aarch_mte(bfd * abfd,Elf_Internal_Note * note)10051 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10052 {
10053 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte",
10054 note);
10055 }
10056
10057 static bool
elfcore_grok_arc_v2(bfd * abfd,Elf_Internal_Note * note)10058 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10059 {
10060 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
10061 }
10062
10063 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if
10064 successful otherwise, return FALSE. */
10065
10066 static bool
elfcore_grok_riscv_csr(bfd * abfd,Elf_Internal_Note * note)10067 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10068 {
10069 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
10070 }
10071
10072 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if
10073 successful otherwise, return FALSE. */
10074
10075 static bool
elfcore_grok_gdb_tdesc(bfd * abfd,Elf_Internal_Note * note)10076 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10077 {
10078 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10079 }
10080
10081 static bool
elfcore_grok_loongarch_cpucfg(bfd * abfd,Elf_Internal_Note * note)10082 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10083 {
10084 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note);
10085 }
10086
10087 static bool
elfcore_grok_loongarch_lbt(bfd * abfd,Elf_Internal_Note * note)10088 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10089 {
10090 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note);
10091 }
10092
10093 static bool
elfcore_grok_loongarch_lsx(bfd * abfd,Elf_Internal_Note * note)10094 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10095 {
10096 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note);
10097 }
10098
10099 static bool
elfcore_grok_loongarch_lasx(bfd * abfd,Elf_Internal_Note * note)10100 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10101 {
10102 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note);
10103 }
10104
10105 #if defined (HAVE_PRPSINFO_T)
10106 typedef prpsinfo_t elfcore_psinfo_t;
10107 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
10108 typedef prpsinfo32_t elfcore_psinfo32_t;
10109 #endif
10110 #endif
10111
10112 #if defined (HAVE_PSINFO_T)
10113 typedef psinfo_t elfcore_psinfo_t;
10114 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
10115 typedef psinfo32_t elfcore_psinfo32_t;
10116 #endif
10117 #endif
10118
10119 /* return a malloc'ed copy of a string at START which is at
10120 most MAX bytes long, possibly without a terminating '\0'.
10121 the copy will always have a terminating '\0'. */
10122
10123 char *
_bfd_elfcore_strndup(bfd * abfd,char * start,size_t max)10124 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10125 {
10126 char *dups;
10127 char *end = (char *) memchr (start, '\0', max);
10128 size_t len;
10129
10130 if (end == NULL)
10131 len = max;
10132 else
10133 len = end - start;
10134
10135 dups = (char *) bfd_alloc (abfd, len + 1);
10136 if (dups == NULL)
10137 return NULL;
10138
10139 memcpy (dups, start, len);
10140 dups[len] = '\0';
10141
10142 return dups;
10143 }
10144
10145 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10146 static bool
elfcore_grok_psinfo(bfd * abfd,Elf_Internal_Note * note)10147 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10148 {
10149 if (note->descsz == sizeof (elfcore_psinfo_t))
10150 {
10151 elfcore_psinfo_t psinfo;
10152
10153 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10154
10155 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10156 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10157 #endif
10158 elf_tdata (abfd)->core->program
10159 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10160 sizeof (psinfo.pr_fname));
10161
10162 elf_tdata (abfd)->core->command
10163 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10164 sizeof (psinfo.pr_psargs));
10165 }
10166 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10167 else if (note->descsz == sizeof (elfcore_psinfo32_t))
10168 {
10169 /* 64-bit host, 32-bit corefile */
10170 elfcore_psinfo32_t psinfo;
10171
10172 memcpy (&psinfo, note->descdata, sizeof (psinfo));
10173
10174 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10175 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10176 #endif
10177 elf_tdata (abfd)->core->program
10178 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10179 sizeof (psinfo.pr_fname));
10180
10181 elf_tdata (abfd)->core->command
10182 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10183 sizeof (psinfo.pr_psargs));
10184 }
10185 #endif
10186
10187 else
10188 {
10189 /* Fail - we don't know how to handle any other
10190 note size (ie. data object type). */
10191 return true;
10192 }
10193
10194 /* Note that for some reason, a spurious space is tacked
10195 onto the end of the args in some (at least one anyway)
10196 implementations, so strip it off if it exists. */
10197
10198 {
10199 char *command = elf_tdata (abfd)->core->command;
10200 int n = strlen (command);
10201
10202 if (0 < n && command[n - 1] == ' ')
10203 command[n - 1] = '\0';
10204 }
10205
10206 return true;
10207 }
10208 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10209
10210 #if defined (HAVE_PSTATUS_T)
10211 static bool
elfcore_grok_pstatus(bfd * abfd,Elf_Internal_Note * note)10212 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10213 {
10214 if (note->descsz == sizeof (pstatus_t)
10215 #if defined (HAVE_PXSTATUS_T)
10216 || note->descsz == sizeof (pxstatus_t)
10217 #endif
10218 )
10219 {
10220 pstatus_t pstat;
10221
10222 memcpy (&pstat, note->descdata, sizeof (pstat));
10223
10224 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10225 }
10226 #if defined (HAVE_PSTATUS32_T)
10227 else if (note->descsz == sizeof (pstatus32_t))
10228 {
10229 /* 64-bit host, 32-bit corefile */
10230 pstatus32_t pstat;
10231
10232 memcpy (&pstat, note->descdata, sizeof (pstat));
10233
10234 elf_tdata (abfd)->core->pid = pstat.pr_pid;
10235 }
10236 #endif
10237 /* Could grab some more details from the "representative"
10238 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10239 NT_LWPSTATUS note, presumably. */
10240
10241 return true;
10242 }
10243 #endif /* defined (HAVE_PSTATUS_T) */
10244
10245 #if defined (HAVE_LWPSTATUS_T)
10246 static bool
elfcore_grok_lwpstatus(bfd * abfd,Elf_Internal_Note * note)10247 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10248 {
10249 lwpstatus_t lwpstat;
10250 char buf[100];
10251 char *name;
10252 size_t len;
10253 asection *sect;
10254
10255 if (note->descsz != sizeof (lwpstat)
10256 #if defined (HAVE_LWPXSTATUS_T)
10257 && note->descsz != sizeof (lwpxstatus_t)
10258 #endif
10259 )
10260 return true;
10261
10262 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10263
10264 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10265 /* Do not overwrite the core signal if it has already been set by
10266 another thread. */
10267 if (elf_tdata (abfd)->core->signal == 0)
10268 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10269
10270 /* Make a ".reg/999" section. */
10271
10272 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
10273 len = strlen (buf) + 1;
10274 name = bfd_alloc (abfd, len);
10275 if (name == NULL)
10276 return false;
10277 memcpy (name, buf, len);
10278
10279 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10280 if (sect == NULL)
10281 return false;
10282
10283 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10284 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10285 sect->filepos = note->descpos
10286 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10287 #endif
10288
10289 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10290 sect->size = sizeof (lwpstat.pr_reg);
10291 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10292 #endif
10293
10294 sect->alignment_power = 2;
10295
10296 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
10297 return false;
10298
10299 /* Make a ".reg2/999" section */
10300
10301 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
10302 len = strlen (buf) + 1;
10303 name = bfd_alloc (abfd, len);
10304 if (name == NULL)
10305 return false;
10306 memcpy (name, buf, len);
10307
10308 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10309 if (sect == NULL)
10310 return false;
10311
10312 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10313 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
10314 sect->filepos = note->descpos
10315 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
10316 #endif
10317
10318 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10319 sect->size = sizeof (lwpstat.pr_fpreg);
10320 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
10321 #endif
10322
10323 sect->alignment_power = 2;
10324
10325 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
10326 }
10327 #endif /* defined (HAVE_LWPSTATUS_T) */
10328
10329 /* These constants, and the structure offsets used below, are defined by
10330 Cygwin's core_dump.h */
10331 #define NOTE_INFO_PROCESS 1
10332 #define NOTE_INFO_THREAD 2
10333 #define NOTE_INFO_MODULE 3
10334 #define NOTE_INFO_MODULE64 4
10335
10336 static bool
elfcore_grok_win32pstatus(bfd * abfd,Elf_Internal_Note * note)10337 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
10338 {
10339 char buf[30];
10340 char *name;
10341 size_t len;
10342 unsigned int name_size;
10343 asection *sect;
10344 unsigned int type;
10345 int is_active_thread;
10346 bfd_vma base_addr;
10347
10348 if (note->descsz < 4)
10349 return true;
10350
10351 if (! startswith (note->namedata, "win32"))
10352 return true;
10353
10354 type = bfd_get_32 (abfd, note->descdata);
10355
10356 struct
10357 {
10358 const char *type_name;
10359 unsigned long min_size;
10360 } size_check[] =
10361 {
10362 { "NOTE_INFO_PROCESS", 12 },
10363 { "NOTE_INFO_THREAD", 12 },
10364 { "NOTE_INFO_MODULE", 12 },
10365 { "NOTE_INFO_MODULE64", 16 },
10366 };
10367
10368 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
10369 return true;
10370
10371 if (note->descsz < size_check[type - 1].min_size)
10372 {
10373 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes is too small"),
10374 abfd, size_check[type - 1].type_name, note->descsz);
10375 return true;
10376 }
10377
10378 switch (type)
10379 {
10380 case NOTE_INFO_PROCESS:
10381 /* FIXME: need to add ->core->command. */
10382 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
10383 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
10384 break;
10385
10386 case NOTE_INFO_THREAD:
10387 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10388 structure. */
10389 /* thread_info.tid */
10390 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
10391
10392 len = strlen (buf) + 1;
10393 name = (char *) bfd_alloc (abfd, len);
10394 if (name == NULL)
10395 return false;
10396
10397 memcpy (name, buf, len);
10398
10399 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10400 if (sect == NULL)
10401 return false;
10402
10403 /* sizeof (thread_info.thread_context) */
10404 sect->size = note->descsz - 12;
10405 /* offsetof (thread_info.thread_context) */
10406 sect->filepos = note->descpos + 12;
10407 sect->alignment_power = 2;
10408
10409 /* thread_info.is_active_thread */
10410 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
10411
10412 if (is_active_thread)
10413 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
10414 return false;
10415 break;
10416
10417 case NOTE_INFO_MODULE:
10418 case NOTE_INFO_MODULE64:
10419 /* Make a ".module/xxxxxxxx" section. */
10420 if (type == NOTE_INFO_MODULE)
10421 {
10422 /* module_info.base_address */
10423 base_addr = bfd_get_32 (abfd, note->descdata + 4);
10424 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
10425 /* module_info.module_name_size */
10426 name_size = bfd_get_32 (abfd, note->descdata + 8);
10427 }
10428 else /* NOTE_INFO_MODULE64 */
10429 {
10430 /* module_info.base_address */
10431 base_addr = bfd_get_64 (abfd, note->descdata + 4);
10432 sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
10433 /* module_info.module_name_size */
10434 name_size = bfd_get_32 (abfd, note->descdata + 12);
10435 }
10436
10437 len = strlen (buf) + 1;
10438 name = (char *) bfd_alloc (abfd, len);
10439 if (name == NULL)
10440 return false;
10441
10442 memcpy (name, buf, len);
10443
10444 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10445
10446 if (sect == NULL)
10447 return false;
10448
10449 if (note->descsz < 12 + name_size)
10450 {
10451 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu is too small to contain a name of size %u"),
10452 abfd, note->descsz, name_size);
10453 return true;
10454 }
10455
10456 sect->size = note->descsz;
10457 sect->filepos = note->descpos;
10458 sect->alignment_power = 2;
10459 break;
10460
10461 default:
10462 return true;
10463 }
10464
10465 return true;
10466 }
10467
10468 static bool
elfcore_grok_note(bfd * abfd,Elf_Internal_Note * note)10469 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
10470 {
10471 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10472
10473 switch (note->type)
10474 {
10475 default:
10476 return true;
10477
10478 case NT_PRSTATUS:
10479 if (bed->elf_backend_grok_prstatus)
10480 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
10481 return true;
10482 #if defined (HAVE_PRSTATUS_T)
10483 return elfcore_grok_prstatus (abfd, note);
10484 #else
10485 return true;
10486 #endif
10487
10488 #if defined (HAVE_PSTATUS_T)
10489 case NT_PSTATUS:
10490 return elfcore_grok_pstatus (abfd, note);
10491 #endif
10492
10493 #if defined (HAVE_LWPSTATUS_T)
10494 case NT_LWPSTATUS:
10495 return elfcore_grok_lwpstatus (abfd, note);
10496 #endif
10497
10498 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
10499 return elfcore_grok_prfpreg (abfd, note);
10500
10501 case NT_WIN32PSTATUS:
10502 return elfcore_grok_win32pstatus (abfd, note);
10503
10504 case NT_PRXFPREG: /* Linux SSE extension */
10505 if (note->namesz == 6
10506 && strcmp (note->namedata, "LINUX") == 0)
10507 return elfcore_grok_prxfpreg (abfd, note);
10508 else
10509 return true;
10510
10511 case NT_X86_XSTATE: /* Linux XSAVE extension */
10512 if (note->namesz == 6
10513 && strcmp (note->namedata, "LINUX") == 0)
10514 return elfcore_grok_xstatereg (abfd, note);
10515 else
10516 return true;
10517
10518 case NT_PPC_VMX:
10519 if (note->namesz == 6
10520 && strcmp (note->namedata, "LINUX") == 0)
10521 return elfcore_grok_ppc_vmx (abfd, note);
10522 else
10523 return true;
10524
10525 case NT_PPC_VSX:
10526 if (note->namesz == 6
10527 && strcmp (note->namedata, "LINUX") == 0)
10528 return elfcore_grok_ppc_vsx (abfd, note);
10529 else
10530 return true;
10531
10532 case NT_PPC_TAR:
10533 if (note->namesz == 6
10534 && strcmp (note->namedata, "LINUX") == 0)
10535 return elfcore_grok_ppc_tar (abfd, note);
10536 else
10537 return true;
10538
10539 case NT_PPC_PPR:
10540 if (note->namesz == 6
10541 && strcmp (note->namedata, "LINUX") == 0)
10542 return elfcore_grok_ppc_ppr (abfd, note);
10543 else
10544 return true;
10545
10546 case NT_PPC_DSCR:
10547 if (note->namesz == 6
10548 && strcmp (note->namedata, "LINUX") == 0)
10549 return elfcore_grok_ppc_dscr (abfd, note);
10550 else
10551 return true;
10552
10553 case NT_PPC_EBB:
10554 if (note->namesz == 6
10555 && strcmp (note->namedata, "LINUX") == 0)
10556 return elfcore_grok_ppc_ebb (abfd, note);
10557 else
10558 return true;
10559
10560 case NT_PPC_PMU:
10561 if (note->namesz == 6
10562 && strcmp (note->namedata, "LINUX") == 0)
10563 return elfcore_grok_ppc_pmu (abfd, note);
10564 else
10565 return true;
10566
10567 case NT_PPC_TM_CGPR:
10568 if (note->namesz == 6
10569 && strcmp (note->namedata, "LINUX") == 0)
10570 return elfcore_grok_ppc_tm_cgpr (abfd, note);
10571 else
10572 return true;
10573
10574 case NT_PPC_TM_CFPR:
10575 if (note->namesz == 6
10576 && strcmp (note->namedata, "LINUX") == 0)
10577 return elfcore_grok_ppc_tm_cfpr (abfd, note);
10578 else
10579 return true;
10580
10581 case NT_PPC_TM_CVMX:
10582 if (note->namesz == 6
10583 && strcmp (note->namedata, "LINUX") == 0)
10584 return elfcore_grok_ppc_tm_cvmx (abfd, note);
10585 else
10586 return true;
10587
10588 case NT_PPC_TM_CVSX:
10589 if (note->namesz == 6
10590 && strcmp (note->namedata, "LINUX") == 0)
10591 return elfcore_grok_ppc_tm_cvsx (abfd, note);
10592 else
10593 return true;
10594
10595 case NT_PPC_TM_SPR:
10596 if (note->namesz == 6
10597 && strcmp (note->namedata, "LINUX") == 0)
10598 return elfcore_grok_ppc_tm_spr (abfd, note);
10599 else
10600 return true;
10601
10602 case NT_PPC_TM_CTAR:
10603 if (note->namesz == 6
10604 && strcmp (note->namedata, "LINUX") == 0)
10605 return elfcore_grok_ppc_tm_ctar (abfd, note);
10606 else
10607 return true;
10608
10609 case NT_PPC_TM_CPPR:
10610 if (note->namesz == 6
10611 && strcmp (note->namedata, "LINUX") == 0)
10612 return elfcore_grok_ppc_tm_cppr (abfd, note);
10613 else
10614 return true;
10615
10616 case NT_PPC_TM_CDSCR:
10617 if (note->namesz == 6
10618 && strcmp (note->namedata, "LINUX") == 0)
10619 return elfcore_grok_ppc_tm_cdscr (abfd, note);
10620 else
10621 return true;
10622
10623 case NT_S390_HIGH_GPRS:
10624 if (note->namesz == 6
10625 && strcmp (note->namedata, "LINUX") == 0)
10626 return elfcore_grok_s390_high_gprs (abfd, note);
10627 else
10628 return true;
10629
10630 case NT_S390_TIMER:
10631 if (note->namesz == 6
10632 && strcmp (note->namedata, "LINUX") == 0)
10633 return elfcore_grok_s390_timer (abfd, note);
10634 else
10635 return true;
10636
10637 case NT_S390_TODCMP:
10638 if (note->namesz == 6
10639 && strcmp (note->namedata, "LINUX") == 0)
10640 return elfcore_grok_s390_todcmp (abfd, note);
10641 else
10642 return true;
10643
10644 case NT_S390_TODPREG:
10645 if (note->namesz == 6
10646 && strcmp (note->namedata, "LINUX") == 0)
10647 return elfcore_grok_s390_todpreg (abfd, note);
10648 else
10649 return true;
10650
10651 case NT_S390_CTRS:
10652 if (note->namesz == 6
10653 && strcmp (note->namedata, "LINUX") == 0)
10654 return elfcore_grok_s390_ctrs (abfd, note);
10655 else
10656 return true;
10657
10658 case NT_S390_PREFIX:
10659 if (note->namesz == 6
10660 && strcmp (note->namedata, "LINUX") == 0)
10661 return elfcore_grok_s390_prefix (abfd, note);
10662 else
10663 return true;
10664
10665 case NT_S390_LAST_BREAK:
10666 if (note->namesz == 6
10667 && strcmp (note->namedata, "LINUX") == 0)
10668 return elfcore_grok_s390_last_break (abfd, note);
10669 else
10670 return true;
10671
10672 case NT_S390_SYSTEM_CALL:
10673 if (note->namesz == 6
10674 && strcmp (note->namedata, "LINUX") == 0)
10675 return elfcore_grok_s390_system_call (abfd, note);
10676 else
10677 return true;
10678
10679 case NT_S390_TDB:
10680 if (note->namesz == 6
10681 && strcmp (note->namedata, "LINUX") == 0)
10682 return elfcore_grok_s390_tdb (abfd, note);
10683 else
10684 return true;
10685
10686 case NT_S390_VXRS_LOW:
10687 if (note->namesz == 6
10688 && strcmp (note->namedata, "LINUX") == 0)
10689 return elfcore_grok_s390_vxrs_low (abfd, note);
10690 else
10691 return true;
10692
10693 case NT_S390_VXRS_HIGH:
10694 if (note->namesz == 6
10695 && strcmp (note->namedata, "LINUX") == 0)
10696 return elfcore_grok_s390_vxrs_high (abfd, note);
10697 else
10698 return true;
10699
10700 case NT_S390_GS_CB:
10701 if (note->namesz == 6
10702 && strcmp (note->namedata, "LINUX") == 0)
10703 return elfcore_grok_s390_gs_cb (abfd, note);
10704 else
10705 return true;
10706
10707 case NT_S390_GS_BC:
10708 if (note->namesz == 6
10709 && strcmp (note->namedata, "LINUX") == 0)
10710 return elfcore_grok_s390_gs_bc (abfd, note);
10711 else
10712 return true;
10713
10714 case NT_ARC_V2:
10715 if (note->namesz == 6
10716 && strcmp (note->namedata, "LINUX") == 0)
10717 return elfcore_grok_arc_v2 (abfd, note);
10718 else
10719 return true;
10720
10721 case NT_ARM_VFP:
10722 if (note->namesz == 6
10723 && strcmp (note->namedata, "LINUX") == 0)
10724 return elfcore_grok_arm_vfp (abfd, note);
10725 else
10726 return true;
10727
10728 case NT_ARM_TLS:
10729 if (note->namesz == 6
10730 && strcmp (note->namedata, "LINUX") == 0)
10731 return elfcore_grok_aarch_tls (abfd, note);
10732 else
10733 return true;
10734
10735 case NT_ARM_HW_BREAK:
10736 if (note->namesz == 6
10737 && strcmp (note->namedata, "LINUX") == 0)
10738 return elfcore_grok_aarch_hw_break (abfd, note);
10739 else
10740 return true;
10741
10742 case NT_ARM_HW_WATCH:
10743 if (note->namesz == 6
10744 && strcmp (note->namedata, "LINUX") == 0)
10745 return elfcore_grok_aarch_hw_watch (abfd, note);
10746 else
10747 return true;
10748
10749 case NT_ARM_SVE:
10750 if (note->namesz == 6
10751 && strcmp (note->namedata, "LINUX") == 0)
10752 return elfcore_grok_aarch_sve (abfd, note);
10753 else
10754 return true;
10755
10756 case NT_ARM_PAC_MASK:
10757 if (note->namesz == 6
10758 && strcmp (note->namedata, "LINUX") == 0)
10759 return elfcore_grok_aarch_pauth (abfd, note);
10760 else
10761 return true;
10762
10763 case NT_ARM_TAGGED_ADDR_CTRL:
10764 if (note->namesz == 6
10765 && strcmp (note->namedata, "LINUX") == 0)
10766 return elfcore_grok_aarch_mte (abfd, note);
10767 else
10768 return true;
10769
10770 case NT_GDB_TDESC:
10771 if (note->namesz == 4
10772 && strcmp (note->namedata, "GDB") == 0)
10773 return elfcore_grok_gdb_tdesc (abfd, note);
10774 else
10775 return true;
10776
10777 case NT_RISCV_CSR:
10778 if (note->namesz == 4
10779 && strcmp (note->namedata, "GDB") == 0)
10780 return elfcore_grok_riscv_csr (abfd, note);
10781 else
10782 return true;
10783
10784 case NT_LARCH_CPUCFG:
10785 if (note->namesz == 6
10786 && strcmp (note->namedata, "LINUX") == 0)
10787 return elfcore_grok_loongarch_cpucfg (abfd, note);
10788 else
10789 return true;
10790
10791 case NT_LARCH_LBT:
10792 if (note->namesz == 6
10793 && strcmp (note->namedata, "LINUX") == 0)
10794 return elfcore_grok_loongarch_lbt (abfd, note);
10795 else
10796 return true;
10797
10798 case NT_LARCH_LSX:
10799 if (note->namesz == 6
10800 && strcmp (note->namedata, "LINUX") == 0)
10801 return elfcore_grok_loongarch_lsx (abfd, note);
10802 else
10803 return true;
10804
10805 case NT_LARCH_LASX:
10806 if (note->namesz == 6
10807 && strcmp (note->namedata, "LINUX") == 0)
10808 return elfcore_grok_loongarch_lasx (abfd, note);
10809 else
10810 return true;
10811
10812 case NT_PRPSINFO:
10813 case NT_PSINFO:
10814 if (bed->elf_backend_grok_psinfo)
10815 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
10816 return true;
10817 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10818 return elfcore_grok_psinfo (abfd, note);
10819 #else
10820 return true;
10821 #endif
10822
10823 case NT_AUXV:
10824 return elfcore_make_auxv_note_section (abfd, note, 0);
10825
10826 case NT_FILE:
10827 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
10828 note);
10829
10830 case NT_SIGINFO:
10831 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
10832 note);
10833
10834 }
10835 }
10836
10837 static bool
elfobj_grok_gnu_build_id(bfd * abfd,Elf_Internal_Note * note)10838 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
10839 {
10840 struct bfd_build_id* build_id;
10841
10842 if (note->descsz == 0)
10843 return false;
10844
10845 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
10846 if (build_id == NULL)
10847 return false;
10848
10849 build_id->size = note->descsz;
10850 memcpy (build_id->data, note->descdata, note->descsz);
10851 abfd->build_id = build_id;
10852
10853 return true;
10854 }
10855
10856 static bool
elfobj_grok_gnu_note(bfd * abfd,Elf_Internal_Note * note)10857 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
10858 {
10859 switch (note->type)
10860 {
10861 default:
10862 return true;
10863
10864 case NT_GNU_PROPERTY_TYPE_0:
10865 return _bfd_elf_parse_gnu_properties (abfd, note);
10866
10867 case NT_GNU_BUILD_ID:
10868 return elfobj_grok_gnu_build_id (abfd, note);
10869 }
10870 }
10871
10872 static bool
elfobj_grok_stapsdt_note_1(bfd * abfd,Elf_Internal_Note * note)10873 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
10874 {
10875 struct sdt_note *cur =
10876 (struct sdt_note *) bfd_alloc (abfd,
10877 sizeof (struct sdt_note) + note->descsz);
10878
10879 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
10880 cur->size = (bfd_size_type) note->descsz;
10881 memcpy (cur->data, note->descdata, note->descsz);
10882
10883 elf_tdata (abfd)->sdt_note_head = cur;
10884
10885 return true;
10886 }
10887
10888 static bool
elfobj_grok_stapsdt_note(bfd * abfd,Elf_Internal_Note * note)10889 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
10890 {
10891 switch (note->type)
10892 {
10893 case NT_STAPSDT:
10894 return elfobj_grok_stapsdt_note_1 (abfd, note);
10895
10896 default:
10897 return true;
10898 }
10899 }
10900
10901 static bool
elfcore_grok_freebsd_psinfo(bfd * abfd,Elf_Internal_Note * note)10902 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
10903 {
10904 size_t offset;
10905
10906 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10907 {
10908 case ELFCLASS32:
10909 if (note->descsz < 108)
10910 return false;
10911 break;
10912
10913 case ELFCLASS64:
10914 if (note->descsz < 120)
10915 return false;
10916 break;
10917
10918 default:
10919 return false;
10920 }
10921
10922 /* Check for version 1 in pr_version. */
10923 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10924 return false;
10925
10926 offset = 4;
10927
10928 /* Skip over pr_psinfosz. */
10929 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10930 offset += 4;
10931 else
10932 {
10933 offset += 4; /* Padding before pr_psinfosz. */
10934 offset += 8;
10935 }
10936
10937 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10938 elf_tdata (abfd)->core->program
10939 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
10940 offset += 17;
10941
10942 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10943 elf_tdata (abfd)->core->command
10944 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
10945 offset += 81;
10946
10947 /* Padding before pr_pid. */
10948 offset += 2;
10949
10950 /* The pr_pid field was added in version "1a". */
10951 if (note->descsz < offset + 4)
10952 return true;
10953
10954 elf_tdata (abfd)->core->pid
10955 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10956
10957 return true;
10958 }
10959
10960 static bool
elfcore_grok_freebsd_prstatus(bfd * abfd,Elf_Internal_Note * note)10961 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
10962 {
10963 size_t offset;
10964 size_t size;
10965 size_t min_size;
10966
10967 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10968 Also compute minimum size of this note. */
10969 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
10970 {
10971 case ELFCLASS32:
10972 offset = 4 + 4;
10973 min_size = offset + (4 * 2) + 4 + 4 + 4;
10974 break;
10975
10976 case ELFCLASS64:
10977 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
10978 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
10979 break;
10980
10981 default:
10982 return false;
10983 }
10984
10985 if (note->descsz < min_size)
10986 return false;
10987
10988 /* Check for version 1 in pr_version. */
10989 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
10990 return false;
10991
10992 /* Extract size of pr_reg from pr_gregsetsz. */
10993 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10994 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
10995 {
10996 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
10997 offset += 4 * 2;
10998 }
10999 else
11000 {
11001 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11002 offset += 8 * 2;
11003 }
11004
11005 /* Skip over pr_osreldate. */
11006 offset += 4;
11007
11008 /* Read signal from pr_cursig. */
11009 if (elf_tdata (abfd)->core->signal == 0)
11010 elf_tdata (abfd)->core->signal
11011 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11012 offset += 4;
11013
11014 /* Read TID from pr_pid. */
11015 elf_tdata (abfd)->core->lwpid
11016 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11017 offset += 4;
11018
11019 /* Padding before pr_reg. */
11020 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11021 offset += 4;
11022
11023 /* Make sure that there is enough data remaining in the note. */
11024 if ((note->descsz - offset) < size)
11025 return false;
11026
11027 /* Make a ".reg/999" section and a ".reg" section. */
11028 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
11029 size, note->descpos + offset);
11030 }
11031
11032 static bool
elfcore_grok_freebsd_note(bfd * abfd,Elf_Internal_Note * note)11033 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11034 {
11035 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11036
11037 switch (note->type)
11038 {
11039 case NT_PRSTATUS:
11040 if (bed->elf_backend_grok_freebsd_prstatus)
11041 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11042 return true;
11043 return elfcore_grok_freebsd_prstatus (abfd, note);
11044
11045 case NT_FPREGSET:
11046 return elfcore_grok_prfpreg (abfd, note);
11047
11048 case NT_PRPSINFO:
11049 return elfcore_grok_freebsd_psinfo (abfd, note);
11050
11051 case NT_FREEBSD_THRMISC:
11052 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11053
11054 case NT_FREEBSD_PROCSTAT_PROC:
11055 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11056 note);
11057
11058 case NT_FREEBSD_PROCSTAT_FILES:
11059 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11060 note);
11061
11062 case NT_FREEBSD_PROCSTAT_VMMAP:
11063 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11064 note);
11065
11066 case NT_FREEBSD_PROCSTAT_AUXV:
11067 return elfcore_make_auxv_note_section (abfd, note, 4);
11068
11069 case NT_FREEBSD_X86_SEGBASES:
11070 return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
11071
11072 case NT_X86_XSTATE:
11073 return elfcore_grok_xstatereg (abfd, note);
11074
11075 case NT_FREEBSD_PTLWPINFO:
11076 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11077 note);
11078
11079 case NT_ARM_TLS:
11080 return elfcore_grok_aarch_tls (abfd, note);
11081
11082 case NT_ARM_VFP:
11083 return elfcore_grok_arm_vfp (abfd, note);
11084
11085 default:
11086 return true;
11087 }
11088 }
11089
11090 static bool
elfcore_netbsd_get_lwpid(Elf_Internal_Note * note,int * lwpidp)11091 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11092 {
11093 char *cp;
11094
11095 cp = strchr (note->namedata, '@');
11096 if (cp != NULL)
11097 {
11098 *lwpidp = atoi(cp + 1);
11099 return true;
11100 }
11101 return false;
11102 }
11103
11104 static bool
elfcore_grok_netbsd_procinfo(bfd * abfd,Elf_Internal_Note * note)11105 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11106 {
11107 if (note->descsz <= 0x7c + 31)
11108 return false;
11109
11110 /* Signal number at offset 0x08. */
11111 elf_tdata (abfd)->core->signal
11112 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11113
11114 /* Process ID at offset 0x50. */
11115 elf_tdata (abfd)->core->pid
11116 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11117
11118 /* Command name at 0x7c (max 32 bytes, including nul). */
11119 elf_tdata (abfd)->core->command
11120 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11121
11122 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11123 note);
11124 }
11125
11126 static bool
elfcore_grok_netbsd_note(bfd * abfd,Elf_Internal_Note * note)11127 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11128 {
11129 int lwp;
11130
11131 if (elfcore_netbsd_get_lwpid (note, &lwp))
11132 elf_tdata (abfd)->core->lwpid = lwp;
11133
11134 switch (note->type)
11135 {
11136 case NT_NETBSDCORE_PROCINFO:
11137 /* NetBSD-specific core "procinfo". Note that we expect to
11138 find this note before any of the others, which is fine,
11139 since the kernel writes this note out first when it
11140 creates a core file. */
11141 return elfcore_grok_netbsd_procinfo (abfd, note);
11142 case NT_NETBSDCORE_AUXV:
11143 /* NetBSD-specific Elf Auxiliary Vector data. */
11144 return elfcore_make_auxv_note_section (abfd, note, 0);
11145 case NT_NETBSDCORE_LWPSTATUS:
11146 return elfcore_make_note_pseudosection (abfd,
11147 ".note.netbsdcore.lwpstatus",
11148 note);
11149 default:
11150 break;
11151 }
11152
11153 /* As of March 2020 there are no other machine-independent notes
11154 defined for NetBSD core files. If the note type is less
11155 than the start of the machine-dependent note types, we don't
11156 understand it. */
11157
11158 if (note->type < NT_NETBSDCORE_FIRSTMACH)
11159 return true;
11160
11161
11162 switch (bfd_get_arch (abfd))
11163 {
11164 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11165 PT_GETFPREGS == mach+2. */
11166
11167 case bfd_arch_aarch64:
11168 case bfd_arch_alpha:
11169 case bfd_arch_sparc:
11170 switch (note->type)
11171 {
11172 case NT_NETBSDCORE_FIRSTMACH+0:
11173 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11174
11175 case NT_NETBSDCORE_FIRSTMACH+2:
11176 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11177
11178 default:
11179 return true;
11180 }
11181
11182 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11183 There's also old PT___GETREGS40 == mach + 1 for old reg
11184 structure which lacks GBR. */
11185
11186 case bfd_arch_sh:
11187 switch (note->type)
11188 {
11189 case NT_NETBSDCORE_FIRSTMACH+3:
11190 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11191
11192 case NT_NETBSDCORE_FIRSTMACH+5:
11193 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11194
11195 default:
11196 return true;
11197 }
11198
11199 /* On all other arch's, PT_GETREGS == mach+1 and
11200 PT_GETFPREGS == mach+3. */
11201
11202 default:
11203 switch (note->type)
11204 {
11205 case NT_NETBSDCORE_FIRSTMACH+1:
11206 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11207
11208 case NT_NETBSDCORE_FIRSTMACH+3:
11209 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11210
11211 default:
11212 return true;
11213 }
11214 }
11215 /* NOTREACHED */
11216 }
11217
11218 static bool
elfcore_grok_openbsd_procinfo(bfd * abfd,Elf_Internal_Note * note)11219 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11220 {
11221 if (note->descsz <= 0x48 + 31)
11222 return false;
11223
11224 /* Signal number at offset 0x08. */
11225 elf_tdata (abfd)->core->signal
11226 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11227
11228 /* Process ID at offset 0x20. */
11229 elf_tdata (abfd)->core->pid
11230 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11231
11232 /* Command name at 0x48 (max 32 bytes, including nul). */
11233 elf_tdata (abfd)->core->command
11234 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11235
11236 return true;
11237 }
11238
11239 /* Processes Solaris's process status note.
11240 sig_off ~ offsetof(prstatus_t, pr_cursig)
11241 pid_off ~ offsetof(prstatus_t, pr_pid)
11242 lwpid_off ~ offsetof(prstatus_t, pr_who)
11243 gregset_size ~ sizeof(gregset_t)
11244 gregset_offset ~ offsetof(prstatus_t, pr_reg) */
11245
11246 static bool
elfcore_grok_solaris_prstatus(bfd * abfd,Elf_Internal_Note * note,int sig_off,int pid_off,int lwpid_off,size_t gregset_size,size_t gregset_offset)11247 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11248 int pid_off, int lwpid_off, size_t gregset_size,
11249 size_t gregset_offset)
11250 {
11251 asection *sect = NULL;
11252 elf_tdata (abfd)->core->signal
11253 = bfd_get_16 (abfd, note->descdata + sig_off);
11254 elf_tdata (abfd)->core->pid
11255 = bfd_get_32 (abfd, note->descdata + pid_off);
11256 elf_tdata (abfd)->core->lwpid
11257 = bfd_get_32 (abfd, note->descdata + lwpid_off);
11258
11259 sect = bfd_get_section_by_name (abfd, ".reg");
11260 if (sect != NULL)
11261 sect->size = gregset_size;
11262
11263 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11264 note->descpos + gregset_offset);
11265 }
11266
11267 /* Gets program and arguments from a core.
11268 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11269 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */
11270
11271 static bool
elfcore_grok_solaris_info(bfd * abfd,Elf_Internal_Note * note,int prog_off,int comm_off)11272 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11273 int prog_off, int comm_off)
11274 {
11275 elf_tdata (abfd)->core->program
11276 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11277 elf_tdata (abfd)->core->command
11278 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11279
11280 return true;
11281 }
11282
11283 /* Processes Solaris's LWP status note.
11284 gregset_size ~ sizeof(gregset_t)
11285 gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11286 fpregset_size ~ sizeof(fpregset_t)
11287 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */
11288
11289 static bool
elfcore_grok_solaris_lwpstatus(bfd * abfd,Elf_Internal_Note * note,size_t gregset_size,int gregset_off,size_t fpregset_size,int fpregset_off)11290 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11291 size_t gregset_size, int gregset_off,
11292 size_t fpregset_size, int fpregset_off)
11293 {
11294 asection *sect = NULL;
11295 char reg2_section_name[16] = { 0 };
11296
11297 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2",
11298 elf_tdata (abfd)->core->lwpid);
11299
11300 /* offsetof(lwpstatus_t, pr_lwpid) */
11301 elf_tdata (abfd)->core->lwpid
11302 = bfd_get_32 (abfd, note->descdata + 4);
11303 /* offsetof(lwpstatus_t, pr_cursig) */
11304 elf_tdata (abfd)->core->signal
11305 = bfd_get_16 (abfd, note->descdata + 12);
11306
11307 sect = bfd_get_section_by_name (abfd, ".reg");
11308 if (sect != NULL)
11309 sect->size = gregset_size;
11310 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size,
11311 note->descpos + gregset_off))
11312 return false;
11313
11314 sect = bfd_get_section_by_name (abfd, reg2_section_name);
11315 if (sect != NULL)
11316 {
11317 sect->size = fpregset_size;
11318 sect->filepos = note->descpos + fpregset_off;
11319 sect->alignment_power = 2;
11320 }
11321 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size,
11322 note->descpos + fpregset_off))
11323 return false;
11324
11325 return true;
11326 }
11327
11328 static bool
elfcore_grok_solaris_note_impl(bfd * abfd,Elf_Internal_Note * note)11329 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11330 {
11331 if (note == NULL)
11332 return false;
11333
11334 /* core files are identified as 32- or 64-bit, SPARC or x86,
11335 by the size of the descsz which matches the sizeof()
11336 the type appropriate for that note type (e.g., prstatus_t for
11337 SOLARIS_NT_PRSTATUS) for the corresponding architecture
11338 on Solaris. The core file bitness may differ from the bitness of
11339 gdb itself, so fixed values are used instead of sizeof().
11340 Appropriate fixed offsets are also used to obtain data from
11341 the note. */
11342
11343 switch ((int) note->type)
11344 {
11345 case SOLARIS_NT_PRSTATUS:
11346 switch (note->descsz)
11347 {
11348 case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11349 return elfcore_grok_solaris_prstatus(abfd, note,
11350 136, 216, 308, 152, 356);
11351 case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11352 return elfcore_grok_solaris_prstatus(abfd, note,
11353 264, 360, 520, 304, 600);
11354 case 432: /* sizeof(prstatus_t) Intel 32-bit */
11355 return elfcore_grok_solaris_prstatus(abfd, note,
11356 136, 216, 308, 76, 356);
11357 case 824: /* sizeof(prstatus_t) Intel 64-bit */
11358 return elfcore_grok_solaris_prstatus(abfd, note,
11359 264, 360, 520, 224, 600);
11360 default:
11361 return true;
11362 }
11363
11364 case SOLARIS_NT_PSINFO:
11365 case SOLARIS_NT_PRPSINFO:
11366 switch (note->descsz)
11367 {
11368 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11369 return elfcore_grok_solaris_info(abfd, note, 84, 100);
11370 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11371 return elfcore_grok_solaris_info(abfd, note, 120, 136);
11372 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11373 return elfcore_grok_solaris_info(abfd, note, 88, 104);
11374 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11375 return elfcore_grok_solaris_info(abfd, note, 136, 152);
11376 default:
11377 return true;
11378 }
11379
11380 case SOLARIS_NT_LWPSTATUS:
11381 switch (note->descsz)
11382 {
11383 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11384 return elfcore_grok_solaris_lwpstatus(abfd, note,
11385 152, 344, 400, 496);
11386 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11387 return elfcore_grok_solaris_lwpstatus(abfd, note,
11388 304, 544, 544, 848);
11389 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11390 return elfcore_grok_solaris_lwpstatus(abfd, note,
11391 76, 344, 380, 420);
11392 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11393 return elfcore_grok_solaris_lwpstatus(abfd, note,
11394 224, 544, 528, 768);
11395 default:
11396 return true;
11397 }
11398
11399 case SOLARIS_NT_LWPSINFO:
11400 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11401 if (note->descsz == 128 || note->descsz == 152)
11402 elf_tdata (abfd)->core->lwpid =
11403 bfd_get_32 (abfd, note->descdata + 4);
11404 break;
11405
11406 default:
11407 break;
11408 }
11409
11410 return true;
11411 }
11412
11413 /* For name starting with "CORE" this may be either a Solaris
11414 core file or a gdb-generated core file. Do Solaris-specific
11415 processing on selected note types first with
11416 elfcore_grok_solaris_note(), then process the note
11417 in elfcore_grok_note(). */
11418
11419 static bool
elfcore_grok_solaris_note(bfd * abfd,Elf_Internal_Note * note)11420 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
11421 {
11422 if (!elfcore_grok_solaris_note_impl (abfd, note))
11423 return false;
11424
11425 return elfcore_grok_note (abfd, note);
11426 }
11427
11428 static bool
elfcore_grok_openbsd_note(bfd * abfd,Elf_Internal_Note * note)11429 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11430 {
11431 if (note->type == NT_OPENBSD_PROCINFO)
11432 return elfcore_grok_openbsd_procinfo (abfd, note);
11433
11434 if (note->type == NT_OPENBSD_REGS)
11435 return elfcore_make_note_pseudosection (abfd, ".reg", note);
11436
11437 if (note->type == NT_OPENBSD_FPREGS)
11438 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
11439
11440 if (note->type == NT_OPENBSD_XFPREGS)
11441 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
11442
11443 if (note->type == NT_OPENBSD_AUXV)
11444 return elfcore_make_auxv_note_section (abfd, note, 0);
11445
11446 if (note->type == NT_OPENBSD_WCOOKIE)
11447 {
11448 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11449 SEC_HAS_CONTENTS);
11450
11451 if (sect == NULL)
11452 return false;
11453 sect->size = note->descsz;
11454 sect->filepos = note->descpos;
11455 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11456
11457 return true;
11458 }
11459
11460 return true;
11461 }
11462
11463 static bool
elfcore_grok_nto_status(bfd * abfd,Elf_Internal_Note * note,long * tid)11464 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
11465 {
11466 void *ddata = note->descdata;
11467 char buf[100];
11468 char *name;
11469 asection *sect;
11470 short sig;
11471 unsigned flags;
11472
11473 if (note->descsz < 16)
11474 return false;
11475
11476 /* nto_procfs_status 'pid' field is at offset 0. */
11477 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
11478
11479 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11480 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11481
11482 /* nto_procfs_status 'flags' field is at offset 8. */
11483 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
11484
11485 /* nto_procfs_status 'what' field is at offset 14. */
11486 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11487 {
11488 elf_tdata (abfd)->core->signal = sig;
11489 elf_tdata (abfd)->core->lwpid = *tid;
11490 }
11491
11492 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11493 do not come from signals so we make sure we set the current
11494 thread just in case. */
11495 if (flags & 0x00000080)
11496 elf_tdata (abfd)->core->lwpid = *tid;
11497
11498 /* Make a ".qnx_core_status/%d" section. */
11499 sprintf (buf, ".qnx_core_status/%ld", *tid);
11500
11501 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11502 if (name == NULL)
11503 return false;
11504 strcpy (name, buf);
11505
11506 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11507 if (sect == NULL)
11508 return false;
11509
11510 sect->size = note->descsz;
11511 sect->filepos = note->descpos;
11512 sect->alignment_power = 2;
11513
11514 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11515 }
11516
11517 static bool
elfcore_grok_nto_regs(bfd * abfd,Elf_Internal_Note * note,long tid,char * base)11518 elfcore_grok_nto_regs (bfd *abfd,
11519 Elf_Internal_Note *note,
11520 long tid,
11521 char *base)
11522 {
11523 char buf[100];
11524 char *name;
11525 asection *sect;
11526
11527 /* Make a "(base)/%d" section. */
11528 sprintf (buf, "%s/%ld", base, tid);
11529
11530 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11531 if (name == NULL)
11532 return false;
11533 strcpy (name, buf);
11534
11535 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11536 if (sect == NULL)
11537 return false;
11538
11539 sect->size = note->descsz;
11540 sect->filepos = note->descpos;
11541 sect->alignment_power = 2;
11542
11543 /* This is the current thread. */
11544 if (elf_tdata (abfd)->core->lwpid == tid)
11545 return elfcore_maybe_make_sect (abfd, base, sect);
11546
11547 return true;
11548 }
11549
11550 #define BFD_QNT_CORE_INFO 7
11551 #define BFD_QNT_CORE_STATUS 8
11552 #define BFD_QNT_CORE_GREG 9
11553 #define BFD_QNT_CORE_FPREG 10
11554
11555 static bool
elfcore_grok_nto_note(bfd * abfd,Elf_Internal_Note * note)11556 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
11557 {
11558 /* Every GREG section has a STATUS section before it. Store the
11559 tid from the previous call to pass down to the next gregs
11560 function. */
11561 static long tid = 1;
11562
11563 switch (note->type)
11564 {
11565 case BFD_QNT_CORE_INFO:
11566 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
11567 case BFD_QNT_CORE_STATUS:
11568 return elfcore_grok_nto_status (abfd, note, &tid);
11569 case BFD_QNT_CORE_GREG:
11570 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
11571 case BFD_QNT_CORE_FPREG:
11572 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
11573 default:
11574 return true;
11575 }
11576 }
11577
11578 static bool
elfcore_grok_spu_note(bfd * abfd,Elf_Internal_Note * note)11579 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
11580 {
11581 char *name;
11582 asection *sect;
11583 size_t len;
11584
11585 /* Use note name as section name. */
11586 len = note->namesz;
11587 name = (char *) bfd_alloc (abfd, len);
11588 if (name == NULL)
11589 return false;
11590 memcpy (name, note->namedata, len);
11591 name[len - 1] = '\0';
11592
11593 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11594 if (sect == NULL)
11595 return false;
11596
11597 sect->size = note->descsz;
11598 sect->filepos = note->descpos;
11599 sect->alignment_power = 1;
11600
11601 return true;
11602 }
11603
11604 /* Function: elfcore_write_note
11605
11606 Inputs:
11607 buffer to hold note, and current size of buffer
11608 name of note
11609 type of note
11610 data for note
11611 size of data for note
11612
11613 Writes note to end of buffer. ELF64 notes are written exactly as
11614 for ELF32, despite the current (as of 2006) ELF gabi specifying
11615 that they ought to have 8-byte namesz and descsz field, and have
11616 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11617
11618 Return:
11619 Pointer to realloc'd buffer, *BUFSIZ updated. */
11620
11621 char *
elfcore_write_note(bfd * abfd,char * buf,int * bufsiz,const char * name,int type,const void * input,int size)11622 elfcore_write_note (bfd *abfd,
11623 char *buf,
11624 int *bufsiz,
11625 const char *name,
11626 int type,
11627 const void *input,
11628 int size)
11629 {
11630 Elf_External_Note *xnp;
11631 size_t namesz;
11632 size_t newspace;
11633 char *dest;
11634
11635 namesz = 0;
11636 if (name != NULL)
11637 namesz = strlen (name) + 1;
11638
11639 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
11640
11641 buf = (char *) realloc (buf, *bufsiz + newspace);
11642 if (buf == NULL)
11643 return buf;
11644 dest = buf + *bufsiz;
11645 *bufsiz += newspace;
11646 xnp = (Elf_External_Note *) dest;
11647 H_PUT_32 (abfd, namesz, xnp->namesz);
11648 H_PUT_32 (abfd, size, xnp->descsz);
11649 H_PUT_32 (abfd, type, xnp->type);
11650 dest = xnp->name;
11651 if (name != NULL)
11652 {
11653 memcpy (dest, name, namesz);
11654 dest += namesz;
11655 while (namesz & 3)
11656 {
11657 *dest++ = '\0';
11658 ++namesz;
11659 }
11660 }
11661 memcpy (dest, input, size);
11662 dest += size;
11663 while (size & 3)
11664 {
11665 *dest++ = '\0';
11666 ++size;
11667 }
11668 return buf;
11669 }
11670
11671 /* gcc-8 warns (*) on all the strncpy calls in this function about
11672 possible string truncation. The "truncation" is not a bug. We
11673 have an external representation of structs with fields that are not
11674 necessarily NULL terminated and corresponding internal
11675 representation fields that are one larger so that they can always
11676 be NULL terminated.
11677 gcc versions between 4.2 and 4.6 do not allow pragma control of
11678 diagnostics inside functions, giving a hard error if you try to use
11679 the finer control available with later versions.
11680 gcc prior to 4.2 warns about diagnostic push and pop.
11681 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11682 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11683 (*) Depending on your system header files! */
11684 #if GCC_VERSION >= 8000
11685 # pragma GCC diagnostic push
11686 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11687 #endif
11688 char *
elfcore_write_prpsinfo(bfd * abfd,char * buf,int * bufsiz,const char * fname,const char * psargs)11689 elfcore_write_prpsinfo (bfd *abfd,
11690 char *buf,
11691 int *bufsiz,
11692 const char *fname,
11693 const char *psargs)
11694 {
11695 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11696
11697 if (bed->elf_backend_write_core_note != NULL)
11698 {
11699 char *ret;
11700 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11701 NT_PRPSINFO, fname, psargs);
11702 if (ret != NULL)
11703 return ret;
11704 }
11705
11706 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11707 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11708 if (bed->s->elfclass == ELFCLASS32)
11709 {
11710 # if defined (HAVE_PSINFO32_T)
11711 psinfo32_t data;
11712 int note_type = NT_PSINFO;
11713 # else
11714 prpsinfo32_t data;
11715 int note_type = NT_PRPSINFO;
11716 # endif
11717
11718 memset (&data, 0, sizeof (data));
11719 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11720 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11721 return elfcore_write_note (abfd, buf, bufsiz,
11722 "CORE", note_type, &data, sizeof (data));
11723 }
11724 else
11725 # endif
11726 {
11727 # if defined (HAVE_PSINFO_T)
11728 psinfo_t data;
11729 int note_type = NT_PSINFO;
11730 # else
11731 prpsinfo_t data;
11732 int note_type = NT_PRPSINFO;
11733 # endif
11734
11735 memset (&data, 0, sizeof (data));
11736 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
11737 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
11738 return elfcore_write_note (abfd, buf, bufsiz,
11739 "CORE", note_type, &data, sizeof (data));
11740 }
11741 #endif /* PSINFO_T or PRPSINFO_T */
11742
11743 free (buf);
11744 return NULL;
11745 }
11746 #if GCC_VERSION >= 8000
11747 # pragma GCC diagnostic pop
11748 #endif
11749
11750 char *
elfcore_write_linux_prpsinfo32(bfd * abfd,char * buf,int * bufsiz,const struct elf_internal_linux_prpsinfo * prpsinfo)11751 elfcore_write_linux_prpsinfo32
11752 (bfd *abfd, char *buf, int *bufsiz,
11753 const struct elf_internal_linux_prpsinfo *prpsinfo)
11754 {
11755 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
11756 {
11757 struct elf_external_linux_prpsinfo32_ugid16 data;
11758
11759 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
11760 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11761 &data, sizeof (data));
11762 }
11763 else
11764 {
11765 struct elf_external_linux_prpsinfo32_ugid32 data;
11766
11767 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
11768 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
11769 &data, sizeof (data));
11770 }
11771 }
11772
11773 char *
elfcore_write_linux_prpsinfo64(bfd * abfd,char * buf,int * bufsiz,const struct elf_internal_linux_prpsinfo * prpsinfo)11774 elfcore_write_linux_prpsinfo64
11775 (bfd *abfd, char *buf, int *bufsiz,
11776 const struct elf_internal_linux_prpsinfo *prpsinfo)
11777 {
11778 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
11779 {
11780 struct elf_external_linux_prpsinfo64_ugid16 data;
11781
11782 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
11783 return elfcore_write_note (abfd, buf, bufsiz,
11784 "CORE", NT_PRPSINFO, &data, sizeof (data));
11785 }
11786 else
11787 {
11788 struct elf_external_linux_prpsinfo64_ugid32 data;
11789
11790 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
11791 return elfcore_write_note (abfd, buf, bufsiz,
11792 "CORE", NT_PRPSINFO, &data, sizeof (data));
11793 }
11794 }
11795
11796 char *
elfcore_write_prstatus(bfd * abfd,char * buf,int * bufsiz,long pid,int cursig,const void * gregs)11797 elfcore_write_prstatus (bfd *abfd,
11798 char *buf,
11799 int *bufsiz,
11800 long pid,
11801 int cursig,
11802 const void *gregs)
11803 {
11804 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11805
11806 if (bed->elf_backend_write_core_note != NULL)
11807 {
11808 char *ret;
11809 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
11810 NT_PRSTATUS,
11811 pid, cursig, gregs);
11812 if (ret != NULL)
11813 return ret;
11814 }
11815
11816 #if defined (HAVE_PRSTATUS_T)
11817 #if defined (HAVE_PRSTATUS32_T)
11818 if (bed->s->elfclass == ELFCLASS32)
11819 {
11820 prstatus32_t prstat;
11821
11822 memset (&prstat, 0, sizeof (prstat));
11823 prstat.pr_pid = pid;
11824 prstat.pr_cursig = cursig;
11825 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11826 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11827 NT_PRSTATUS, &prstat, sizeof (prstat));
11828 }
11829 else
11830 #endif
11831 {
11832 prstatus_t prstat;
11833
11834 memset (&prstat, 0, sizeof (prstat));
11835 prstat.pr_pid = pid;
11836 prstat.pr_cursig = cursig;
11837 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
11838 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
11839 NT_PRSTATUS, &prstat, sizeof (prstat));
11840 }
11841 #endif /* HAVE_PRSTATUS_T */
11842
11843 free (buf);
11844 return NULL;
11845 }
11846
11847 #if defined (HAVE_LWPSTATUS_T)
11848 char *
elfcore_write_lwpstatus(bfd * abfd,char * buf,int * bufsiz,long pid,int cursig,const void * gregs)11849 elfcore_write_lwpstatus (bfd *abfd,
11850 char *buf,
11851 int *bufsiz,
11852 long pid,
11853 int cursig,
11854 const void *gregs)
11855 {
11856 lwpstatus_t lwpstat;
11857 const char *note_name = "CORE";
11858
11859 memset (&lwpstat, 0, sizeof (lwpstat));
11860 lwpstat.pr_lwpid = pid >> 16;
11861 lwpstat.pr_cursig = cursig;
11862 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11863 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
11864 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11865 #if !defined(gregs)
11866 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
11867 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
11868 #else
11869 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
11870 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
11871 #endif
11872 #endif
11873 return elfcore_write_note (abfd, buf, bufsiz, note_name,
11874 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
11875 }
11876 #endif /* HAVE_LWPSTATUS_T */
11877
11878 #if defined (HAVE_PSTATUS_T)
11879 char *
elfcore_write_pstatus(bfd * abfd,char * buf,int * bufsiz,long pid,int cursig ATTRIBUTE_UNUSED,const void * gregs ATTRIBUTE_UNUSED)11880 elfcore_write_pstatus (bfd *abfd,
11881 char *buf,
11882 int *bufsiz,
11883 long pid,
11884 int cursig ATTRIBUTE_UNUSED,
11885 const void *gregs ATTRIBUTE_UNUSED)
11886 {
11887 const char *note_name = "CORE";
11888 #if defined (HAVE_PSTATUS32_T)
11889 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11890
11891 if (bed->s->elfclass == ELFCLASS32)
11892 {
11893 pstatus32_t pstat;
11894
11895 memset (&pstat, 0, sizeof (pstat));
11896 pstat.pr_pid = pid & 0xffff;
11897 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11898 NT_PSTATUS, &pstat, sizeof (pstat));
11899 return buf;
11900 }
11901 else
11902 #endif
11903 {
11904 pstatus_t pstat;
11905
11906 memset (&pstat, 0, sizeof (pstat));
11907 pstat.pr_pid = pid & 0xffff;
11908 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
11909 NT_PSTATUS, &pstat, sizeof (pstat));
11910 return buf;
11911 }
11912 }
11913 #endif /* HAVE_PSTATUS_T */
11914
11915 char *
elfcore_write_prfpreg(bfd * abfd,char * buf,int * bufsiz,const void * fpregs,int size)11916 elfcore_write_prfpreg (bfd *abfd,
11917 char *buf,
11918 int *bufsiz,
11919 const void *fpregs,
11920 int size)
11921 {
11922 const char *note_name = "CORE";
11923 return elfcore_write_note (abfd, buf, bufsiz,
11924 note_name, NT_FPREGSET, fpregs, size);
11925 }
11926
11927 char *
elfcore_write_prxfpreg(bfd * abfd,char * buf,int * bufsiz,const void * xfpregs,int size)11928 elfcore_write_prxfpreg (bfd *abfd,
11929 char *buf,
11930 int *bufsiz,
11931 const void *xfpregs,
11932 int size)
11933 {
11934 char *note_name = "LINUX";
11935 return elfcore_write_note (abfd, buf, bufsiz,
11936 note_name, NT_PRXFPREG, xfpregs, size);
11937 }
11938
11939 char *
elfcore_write_xstatereg(bfd * abfd,char * buf,int * bufsiz,const void * xfpregs,int size)11940 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
11941 const void *xfpregs, int size)
11942 {
11943 char *note_name;
11944 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
11945 note_name = "FreeBSD";
11946 else
11947 note_name = "LINUX";
11948 return elfcore_write_note (abfd, buf, bufsiz,
11949 note_name, NT_X86_XSTATE, xfpregs, size);
11950 }
11951
11952 char *
elfcore_write_x86_segbases(bfd * abfd,char * buf,int * bufsiz,const void * regs,int size)11953 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
11954 const void *regs, int size)
11955 {
11956 char *note_name = "FreeBSD";
11957 return elfcore_write_note (abfd, buf, bufsiz,
11958 note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
11959 }
11960
11961 char *
elfcore_write_ppc_vmx(bfd * abfd,char * buf,int * bufsiz,const void * ppc_vmx,int size)11962 elfcore_write_ppc_vmx (bfd *abfd,
11963 char *buf,
11964 int *bufsiz,
11965 const void *ppc_vmx,
11966 int size)
11967 {
11968 char *note_name = "LINUX";
11969 return elfcore_write_note (abfd, buf, bufsiz,
11970 note_name, NT_PPC_VMX, ppc_vmx, size);
11971 }
11972
11973 char *
elfcore_write_ppc_vsx(bfd * abfd,char * buf,int * bufsiz,const void * ppc_vsx,int size)11974 elfcore_write_ppc_vsx (bfd *abfd,
11975 char *buf,
11976 int *bufsiz,
11977 const void *ppc_vsx,
11978 int size)
11979 {
11980 char *note_name = "LINUX";
11981 return elfcore_write_note (abfd, buf, bufsiz,
11982 note_name, NT_PPC_VSX, ppc_vsx, size);
11983 }
11984
11985 char *
elfcore_write_ppc_tar(bfd * abfd,char * buf,int * bufsiz,const void * ppc_tar,int size)11986 elfcore_write_ppc_tar (bfd *abfd,
11987 char *buf,
11988 int *bufsiz,
11989 const void *ppc_tar,
11990 int size)
11991 {
11992 char *note_name = "LINUX";
11993 return elfcore_write_note (abfd, buf, bufsiz,
11994 note_name, NT_PPC_TAR, ppc_tar, size);
11995 }
11996
11997 char *
elfcore_write_ppc_ppr(bfd * abfd,char * buf,int * bufsiz,const void * ppc_ppr,int size)11998 elfcore_write_ppc_ppr (bfd *abfd,
11999 char *buf,
12000 int *bufsiz,
12001 const void *ppc_ppr,
12002 int size)
12003 {
12004 char *note_name = "LINUX";
12005 return elfcore_write_note (abfd, buf, bufsiz,
12006 note_name, NT_PPC_PPR, ppc_ppr, size);
12007 }
12008
12009 char *
elfcore_write_ppc_dscr(bfd * abfd,char * buf,int * bufsiz,const void * ppc_dscr,int size)12010 elfcore_write_ppc_dscr (bfd *abfd,
12011 char *buf,
12012 int *bufsiz,
12013 const void *ppc_dscr,
12014 int size)
12015 {
12016 char *note_name = "LINUX";
12017 return elfcore_write_note (abfd, buf, bufsiz,
12018 note_name, NT_PPC_DSCR, ppc_dscr, size);
12019 }
12020
12021 char *
elfcore_write_ppc_ebb(bfd * abfd,char * buf,int * bufsiz,const void * ppc_ebb,int size)12022 elfcore_write_ppc_ebb (bfd *abfd,
12023 char *buf,
12024 int *bufsiz,
12025 const void *ppc_ebb,
12026 int size)
12027 {
12028 char *note_name = "LINUX";
12029 return elfcore_write_note (abfd, buf, bufsiz,
12030 note_name, NT_PPC_EBB, ppc_ebb, size);
12031 }
12032
12033 char *
elfcore_write_ppc_pmu(bfd * abfd,char * buf,int * bufsiz,const void * ppc_pmu,int size)12034 elfcore_write_ppc_pmu (bfd *abfd,
12035 char *buf,
12036 int *bufsiz,
12037 const void *ppc_pmu,
12038 int size)
12039 {
12040 char *note_name = "LINUX";
12041 return elfcore_write_note (abfd, buf, bufsiz,
12042 note_name, NT_PPC_PMU, ppc_pmu, size);
12043 }
12044
12045 char *
elfcore_write_ppc_tm_cgpr(bfd * abfd,char * buf,int * bufsiz,const void * ppc_tm_cgpr,int size)12046 elfcore_write_ppc_tm_cgpr (bfd *abfd,
12047 char *buf,
12048 int *bufsiz,
12049 const void *ppc_tm_cgpr,
12050 int size)
12051 {
12052 char *note_name = "LINUX";
12053 return elfcore_write_note (abfd, buf, bufsiz,
12054 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size);
12055 }
12056
12057 char *
elfcore_write_ppc_tm_cfpr(bfd * abfd,char * buf,int * bufsiz,const void * ppc_tm_cfpr,int size)12058 elfcore_write_ppc_tm_cfpr (bfd *abfd,
12059 char *buf,
12060 int *bufsiz,
12061 const void *ppc_tm_cfpr,
12062 int size)
12063 {
12064 char *note_name = "LINUX";
12065 return elfcore_write_note (abfd, buf, bufsiz,
12066 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size);
12067 }
12068
12069 char *
elfcore_write_ppc_tm_cvmx(bfd * abfd,char * buf,int * bufsiz,const void * ppc_tm_cvmx,int size)12070 elfcore_write_ppc_tm_cvmx (bfd *abfd,
12071 char *buf,
12072 int *bufsiz,
12073 const void *ppc_tm_cvmx,
12074 int size)
12075 {
12076 char *note_name = "LINUX";
12077 return elfcore_write_note (abfd, buf, bufsiz,
12078 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size);
12079 }
12080
12081 char *
elfcore_write_ppc_tm_cvsx(bfd * abfd,char * buf,int * bufsiz,const void * ppc_tm_cvsx,int size)12082 elfcore_write_ppc_tm_cvsx (bfd *abfd,
12083 char *buf,
12084 int *bufsiz,
12085 const void *ppc_tm_cvsx,
12086 int size)
12087 {
12088 char *note_name = "LINUX";
12089 return elfcore_write_note (abfd, buf, bufsiz,
12090 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size);
12091 }
12092
12093 char *
elfcore_write_ppc_tm_spr(bfd * abfd,char * buf,int * bufsiz,const void * ppc_tm_spr,int size)12094 elfcore_write_ppc_tm_spr (bfd *abfd,
12095 char *buf,
12096 int *bufsiz,
12097 const void *ppc_tm_spr,
12098 int size)
12099 {
12100 char *note_name = "LINUX";
12101 return elfcore_write_note (abfd, buf, bufsiz,
12102 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size);
12103 }
12104
12105 char *
elfcore_write_ppc_tm_ctar(bfd * abfd,char * buf,int * bufsiz,const void * ppc_tm_ctar,int size)12106 elfcore_write_ppc_tm_ctar (bfd *abfd,
12107 char *buf,
12108 int *bufsiz,
12109 const void *ppc_tm_ctar,
12110 int size)
12111 {
12112 char *note_name = "LINUX";
12113 return elfcore_write_note (abfd, buf, bufsiz,
12114 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size);
12115 }
12116
12117 char *
elfcore_write_ppc_tm_cppr(bfd * abfd,char * buf,int * bufsiz,const void * ppc_tm_cppr,int size)12118 elfcore_write_ppc_tm_cppr (bfd *abfd,
12119 char *buf,
12120 int *bufsiz,
12121 const void *ppc_tm_cppr,
12122 int size)
12123 {
12124 char *note_name = "LINUX";
12125 return elfcore_write_note (abfd, buf, bufsiz,
12126 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size);
12127 }
12128
12129 char *
elfcore_write_ppc_tm_cdscr(bfd * abfd,char * buf,int * bufsiz,const void * ppc_tm_cdscr,int size)12130 elfcore_write_ppc_tm_cdscr (bfd *abfd,
12131 char *buf,
12132 int *bufsiz,
12133 const void *ppc_tm_cdscr,
12134 int size)
12135 {
12136 char *note_name = "LINUX";
12137 return elfcore_write_note (abfd, buf, bufsiz,
12138 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size);
12139 }
12140
12141 static char *
elfcore_write_s390_high_gprs(bfd * abfd,char * buf,int * bufsiz,const void * s390_high_gprs,int size)12142 elfcore_write_s390_high_gprs (bfd *abfd,
12143 char *buf,
12144 int *bufsiz,
12145 const void *s390_high_gprs,
12146 int size)
12147 {
12148 char *note_name = "LINUX";
12149 return elfcore_write_note (abfd, buf, bufsiz,
12150 note_name, NT_S390_HIGH_GPRS,
12151 s390_high_gprs, size);
12152 }
12153
12154 char *
elfcore_write_s390_timer(bfd * abfd,char * buf,int * bufsiz,const void * s390_timer,int size)12155 elfcore_write_s390_timer (bfd *abfd,
12156 char *buf,
12157 int *bufsiz,
12158 const void *s390_timer,
12159 int size)
12160 {
12161 char *note_name = "LINUX";
12162 return elfcore_write_note (abfd, buf, bufsiz,
12163 note_name, NT_S390_TIMER, s390_timer, size);
12164 }
12165
12166 char *
elfcore_write_s390_todcmp(bfd * abfd,char * buf,int * bufsiz,const void * s390_todcmp,int size)12167 elfcore_write_s390_todcmp (bfd *abfd,
12168 char *buf,
12169 int *bufsiz,
12170 const void *s390_todcmp,
12171 int size)
12172 {
12173 char *note_name = "LINUX";
12174 return elfcore_write_note (abfd, buf, bufsiz,
12175 note_name, NT_S390_TODCMP, s390_todcmp, size);
12176 }
12177
12178 char *
elfcore_write_s390_todpreg(bfd * abfd,char * buf,int * bufsiz,const void * s390_todpreg,int size)12179 elfcore_write_s390_todpreg (bfd *abfd,
12180 char *buf,
12181 int *bufsiz,
12182 const void *s390_todpreg,
12183 int size)
12184 {
12185 char *note_name = "LINUX";
12186 return elfcore_write_note (abfd, buf, bufsiz,
12187 note_name, NT_S390_TODPREG, s390_todpreg, size);
12188 }
12189
12190 char *
elfcore_write_s390_ctrs(bfd * abfd,char * buf,int * bufsiz,const void * s390_ctrs,int size)12191 elfcore_write_s390_ctrs (bfd *abfd,
12192 char *buf,
12193 int *bufsiz,
12194 const void *s390_ctrs,
12195 int size)
12196 {
12197 char *note_name = "LINUX";
12198 return elfcore_write_note (abfd, buf, bufsiz,
12199 note_name, NT_S390_CTRS, s390_ctrs, size);
12200 }
12201
12202 char *
elfcore_write_s390_prefix(bfd * abfd,char * buf,int * bufsiz,const void * s390_prefix,int size)12203 elfcore_write_s390_prefix (bfd *abfd,
12204 char *buf,
12205 int *bufsiz,
12206 const void *s390_prefix,
12207 int size)
12208 {
12209 char *note_name = "LINUX";
12210 return elfcore_write_note (abfd, buf, bufsiz,
12211 note_name, NT_S390_PREFIX, s390_prefix, size);
12212 }
12213
12214 char *
elfcore_write_s390_last_break(bfd * abfd,char * buf,int * bufsiz,const void * s390_last_break,int size)12215 elfcore_write_s390_last_break (bfd *abfd,
12216 char *buf,
12217 int *bufsiz,
12218 const void *s390_last_break,
12219 int size)
12220 {
12221 char *note_name = "LINUX";
12222 return elfcore_write_note (abfd, buf, bufsiz,
12223 note_name, NT_S390_LAST_BREAK,
12224 s390_last_break, size);
12225 }
12226
12227 char *
elfcore_write_s390_system_call(bfd * abfd,char * buf,int * bufsiz,const void * s390_system_call,int size)12228 elfcore_write_s390_system_call (bfd *abfd,
12229 char *buf,
12230 int *bufsiz,
12231 const void *s390_system_call,
12232 int size)
12233 {
12234 char *note_name = "LINUX";
12235 return elfcore_write_note (abfd, buf, bufsiz,
12236 note_name, NT_S390_SYSTEM_CALL,
12237 s390_system_call, size);
12238 }
12239
12240 char *
elfcore_write_s390_tdb(bfd * abfd,char * buf,int * bufsiz,const void * s390_tdb,int size)12241 elfcore_write_s390_tdb (bfd *abfd,
12242 char *buf,
12243 int *bufsiz,
12244 const void *s390_tdb,
12245 int size)
12246 {
12247 char *note_name = "LINUX";
12248 return elfcore_write_note (abfd, buf, bufsiz,
12249 note_name, NT_S390_TDB, s390_tdb, size);
12250 }
12251
12252 char *
elfcore_write_s390_vxrs_low(bfd * abfd,char * buf,int * bufsiz,const void * s390_vxrs_low,int size)12253 elfcore_write_s390_vxrs_low (bfd *abfd,
12254 char *buf,
12255 int *bufsiz,
12256 const void *s390_vxrs_low,
12257 int size)
12258 {
12259 char *note_name = "LINUX";
12260 return elfcore_write_note (abfd, buf, bufsiz,
12261 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
12262 }
12263
12264 char *
elfcore_write_s390_vxrs_high(bfd * abfd,char * buf,int * bufsiz,const void * s390_vxrs_high,int size)12265 elfcore_write_s390_vxrs_high (bfd *abfd,
12266 char *buf,
12267 int *bufsiz,
12268 const void *s390_vxrs_high,
12269 int size)
12270 {
12271 char *note_name = "LINUX";
12272 return elfcore_write_note (abfd, buf, bufsiz,
12273 note_name, NT_S390_VXRS_HIGH,
12274 s390_vxrs_high, size);
12275 }
12276
12277 char *
elfcore_write_s390_gs_cb(bfd * abfd,char * buf,int * bufsiz,const void * s390_gs_cb,int size)12278 elfcore_write_s390_gs_cb (bfd *abfd,
12279 char *buf,
12280 int *bufsiz,
12281 const void *s390_gs_cb,
12282 int size)
12283 {
12284 char *note_name = "LINUX";
12285 return elfcore_write_note (abfd, buf, bufsiz,
12286 note_name, NT_S390_GS_CB,
12287 s390_gs_cb, size);
12288 }
12289
12290 char *
elfcore_write_s390_gs_bc(bfd * abfd,char * buf,int * bufsiz,const void * s390_gs_bc,int size)12291 elfcore_write_s390_gs_bc (bfd *abfd,
12292 char *buf,
12293 int *bufsiz,
12294 const void *s390_gs_bc,
12295 int size)
12296 {
12297 char *note_name = "LINUX";
12298 return elfcore_write_note (abfd, buf, bufsiz,
12299 note_name, NT_S390_GS_BC,
12300 s390_gs_bc, size);
12301 }
12302
12303 char *
elfcore_write_arm_vfp(bfd * abfd,char * buf,int * bufsiz,const void * arm_vfp,int size)12304 elfcore_write_arm_vfp (bfd *abfd,
12305 char *buf,
12306 int *bufsiz,
12307 const void *arm_vfp,
12308 int size)
12309 {
12310 char *note_name = "LINUX";
12311 return elfcore_write_note (abfd, buf, bufsiz,
12312 note_name, NT_ARM_VFP, arm_vfp, size);
12313 }
12314
12315 char *
elfcore_write_aarch_tls(bfd * abfd,char * buf,int * bufsiz,const void * aarch_tls,int size)12316 elfcore_write_aarch_tls (bfd *abfd,
12317 char *buf,
12318 int *bufsiz,
12319 const void *aarch_tls,
12320 int size)
12321 {
12322 char *note_name = "LINUX";
12323 return elfcore_write_note (abfd, buf, bufsiz,
12324 note_name, NT_ARM_TLS, aarch_tls, size);
12325 }
12326
12327 char *
elfcore_write_aarch_hw_break(bfd * abfd,char * buf,int * bufsiz,const void * aarch_hw_break,int size)12328 elfcore_write_aarch_hw_break (bfd *abfd,
12329 char *buf,
12330 int *bufsiz,
12331 const void *aarch_hw_break,
12332 int size)
12333 {
12334 char *note_name = "LINUX";
12335 return elfcore_write_note (abfd, buf, bufsiz,
12336 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
12337 }
12338
12339 char *
elfcore_write_aarch_hw_watch(bfd * abfd,char * buf,int * bufsiz,const void * aarch_hw_watch,int size)12340 elfcore_write_aarch_hw_watch (bfd *abfd,
12341 char *buf,
12342 int *bufsiz,
12343 const void *aarch_hw_watch,
12344 int size)
12345 {
12346 char *note_name = "LINUX";
12347 return elfcore_write_note (abfd, buf, bufsiz,
12348 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
12349 }
12350
12351 char *
elfcore_write_aarch_sve(bfd * abfd,char * buf,int * bufsiz,const void * aarch_sve,int size)12352 elfcore_write_aarch_sve (bfd *abfd,
12353 char *buf,
12354 int *bufsiz,
12355 const void *aarch_sve,
12356 int size)
12357 {
12358 char *note_name = "LINUX";
12359 return elfcore_write_note (abfd, buf, bufsiz,
12360 note_name, NT_ARM_SVE, aarch_sve, size);
12361 }
12362
12363 char *
elfcore_write_aarch_pauth(bfd * abfd,char * buf,int * bufsiz,const void * aarch_pauth,int size)12364 elfcore_write_aarch_pauth (bfd *abfd,
12365 char *buf,
12366 int *bufsiz,
12367 const void *aarch_pauth,
12368 int size)
12369 {
12370 char *note_name = "LINUX";
12371 return elfcore_write_note (abfd, buf, bufsiz,
12372 note_name, NT_ARM_PAC_MASK, aarch_pauth, size);
12373 }
12374
12375 char *
elfcore_write_aarch_mte(bfd * abfd,char * buf,int * bufsiz,const void * aarch_mte,int size)12376 elfcore_write_aarch_mte (bfd *abfd,
12377 char *buf,
12378 int *bufsiz,
12379 const void *aarch_mte,
12380 int size)
12381 {
12382 char *note_name = "LINUX";
12383 return elfcore_write_note (abfd, buf, bufsiz,
12384 note_name, NT_ARM_TAGGED_ADDR_CTRL,
12385 aarch_mte,
12386 size);
12387 }
12388
12389 char *
elfcore_write_arc_v2(bfd * abfd,char * buf,int * bufsiz,const void * arc_v2,int size)12390 elfcore_write_arc_v2 (bfd *abfd,
12391 char *buf,
12392 int *bufsiz,
12393 const void *arc_v2,
12394 int size)
12395 {
12396 char *note_name = "LINUX";
12397 return elfcore_write_note (abfd, buf, bufsiz,
12398 note_name, NT_ARC_V2, arc_v2, size);
12399 }
12400
12401 char *
elfcore_write_loongarch_cpucfg(bfd * abfd,char * buf,int * bufsiz,const void * loongarch_cpucfg,int size)12402 elfcore_write_loongarch_cpucfg (bfd *abfd,
12403 char *buf,
12404 int *bufsiz,
12405 const void *loongarch_cpucfg,
12406 int size)
12407 {
12408 char *note_name = "LINUX";
12409 return elfcore_write_note (abfd, buf, bufsiz,
12410 note_name, NT_LARCH_CPUCFG,
12411 loongarch_cpucfg, size);
12412 }
12413
12414 char *
elfcore_write_loongarch_lbt(bfd * abfd,char * buf,int * bufsiz,const void * loongarch_lbt,int size)12415 elfcore_write_loongarch_lbt (bfd *abfd,
12416 char *buf,
12417 int *bufsiz,
12418 const void *loongarch_lbt,
12419 int size)
12420 {
12421 char *note_name = "LINUX";
12422 return elfcore_write_note (abfd, buf, bufsiz,
12423 note_name, NT_LARCH_LBT, loongarch_lbt, size);
12424 }
12425
12426 char *
elfcore_write_loongarch_lsx(bfd * abfd,char * buf,int * bufsiz,const void * loongarch_lsx,int size)12427 elfcore_write_loongarch_lsx (bfd *abfd,
12428 char *buf,
12429 int *bufsiz,
12430 const void *loongarch_lsx,
12431 int size)
12432 {
12433 char *note_name = "LINUX";
12434 return elfcore_write_note (abfd, buf, bufsiz,
12435 note_name, NT_LARCH_LSX, loongarch_lsx, size);
12436 }
12437
12438 char *
elfcore_write_loongarch_lasx(bfd * abfd,char * buf,int * bufsiz,const void * loongarch_lasx,int size)12439 elfcore_write_loongarch_lasx (bfd *abfd,
12440 char *buf,
12441 int *bufsiz,
12442 const void *loongarch_lasx,
12443 int size)
12444 {
12445 char *note_name = "LINUX";
12446 return elfcore_write_note (abfd, buf, bufsiz,
12447 note_name, NT_LARCH_LASX, loongarch_lasx, size);
12448 }
12449
12450 /* Write the buffer of csr values in CSRS (length SIZE) into the note
12451 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being
12452 written into. Return a pointer to the new start of the note buffer, to
12453 replace BUF which may no longer be valid. */
12454
12455 char *
elfcore_write_riscv_csr(bfd * abfd,char * buf,int * bufsiz,const void * csrs,int size)12456 elfcore_write_riscv_csr (bfd *abfd,
12457 char *buf,
12458 int *bufsiz,
12459 const void *csrs,
12460 int size)
12461 {
12462 const char *note_name = "GDB";
12463 return elfcore_write_note (abfd, buf, bufsiz,
12464 note_name, NT_RISCV_CSR, csrs, size);
12465 }
12466
12467 /* Write the target description (a string) pointed to by TDESC, length
12468 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the
12469 note is being written into. Return a pointer to the new start of the
12470 note buffer, to replace BUF which may no longer be valid. */
12471
12472 char *
elfcore_write_gdb_tdesc(bfd * abfd,char * buf,int * bufsiz,const void * tdesc,int size)12473 elfcore_write_gdb_tdesc (bfd *abfd,
12474 char *buf,
12475 int *bufsiz,
12476 const void *tdesc,
12477 int size)
12478 {
12479 const char *note_name = "GDB";
12480 return elfcore_write_note (abfd, buf, bufsiz,
12481 note_name, NT_GDB_TDESC, tdesc, size);
12482 }
12483
12484 char *
elfcore_write_register_note(bfd * abfd,char * buf,int * bufsiz,const char * section,const void * data,int size)12485 elfcore_write_register_note (bfd *abfd,
12486 char *buf,
12487 int *bufsiz,
12488 const char *section,
12489 const void *data,
12490 int size)
12491 {
12492 if (strcmp (section, ".reg2") == 0)
12493 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
12494 if (strcmp (section, ".reg-xfp") == 0)
12495 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
12496 if (strcmp (section, ".reg-xstate") == 0)
12497 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
12498 if (strcmp (section, ".reg-x86-segbases") == 0)
12499 return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
12500 if (strcmp (section, ".reg-ppc-vmx") == 0)
12501 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
12502 if (strcmp (section, ".reg-ppc-vsx") == 0)
12503 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
12504 if (strcmp (section, ".reg-ppc-tar") == 0)
12505 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size);
12506 if (strcmp (section, ".reg-ppc-ppr") == 0)
12507 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size);
12508 if (strcmp (section, ".reg-ppc-dscr") == 0)
12509 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size);
12510 if (strcmp (section, ".reg-ppc-ebb") == 0)
12511 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size);
12512 if (strcmp (section, ".reg-ppc-pmu") == 0)
12513 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size);
12514 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0)
12515 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size);
12516 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0)
12517 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size);
12518 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0)
12519 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size);
12520 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0)
12521 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size);
12522 if (strcmp (section, ".reg-ppc-tm-spr") == 0)
12523 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size);
12524 if (strcmp (section, ".reg-ppc-tm-ctar") == 0)
12525 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size);
12526 if (strcmp (section, ".reg-ppc-tm-cppr") == 0)
12527 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size);
12528 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0)
12529 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size);
12530 if (strcmp (section, ".reg-s390-high-gprs") == 0)
12531 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
12532 if (strcmp (section, ".reg-s390-timer") == 0)
12533 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
12534 if (strcmp (section, ".reg-s390-todcmp") == 0)
12535 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
12536 if (strcmp (section, ".reg-s390-todpreg") == 0)
12537 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
12538 if (strcmp (section, ".reg-s390-ctrs") == 0)
12539 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
12540 if (strcmp (section, ".reg-s390-prefix") == 0)
12541 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
12542 if (strcmp (section, ".reg-s390-last-break") == 0)
12543 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
12544 if (strcmp (section, ".reg-s390-system-call") == 0)
12545 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
12546 if (strcmp (section, ".reg-s390-tdb") == 0)
12547 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
12548 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
12549 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
12550 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
12551 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
12552 if (strcmp (section, ".reg-s390-gs-cb") == 0)
12553 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
12554 if (strcmp (section, ".reg-s390-gs-bc") == 0)
12555 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
12556 if (strcmp (section, ".reg-arm-vfp") == 0)
12557 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
12558 if (strcmp (section, ".reg-aarch-tls") == 0)
12559 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
12560 if (strcmp (section, ".reg-aarch-hw-break") == 0)
12561 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
12562 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
12563 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
12564 if (strcmp (section, ".reg-aarch-sve") == 0)
12565 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size);
12566 if (strcmp (section, ".reg-aarch-pauth") == 0)
12567 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size);
12568 if (strcmp (section, ".reg-aarch-mte") == 0)
12569 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size);
12570 if (strcmp (section, ".reg-arc-v2") == 0)
12571 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
12572 if (strcmp (section, ".gdb-tdesc") == 0)
12573 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
12574 if (strcmp (section, ".reg-riscv-csr") == 0)
12575 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
12576 if (strcmp (section, ".reg-loongarch-cpucfg") == 0)
12577 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size);
12578 if (strcmp (section, ".reg-loongarch-lbt") == 0)
12579 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size);
12580 if (strcmp (section, ".reg-loongarch-lsx") == 0)
12581 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
12582 if (strcmp (section, ".reg-loongarch-lasx") == 0)
12583 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
12584 return NULL;
12585 }
12586
12587 char *
elfcore_write_file_note(bfd * obfd,char * note_data,int * note_size,const void * buf,int bufsiz)12588 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
12589 const void *buf, int bufsiz)
12590 {
12591 return elfcore_write_note (obfd, note_data, note_size,
12592 "CORE", NT_FILE, buf, bufsiz);
12593 }
12594
12595 static bool
elf_parse_notes(bfd * abfd,char * buf,size_t size,file_ptr offset,size_t align)12596 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
12597 size_t align)
12598 {
12599 char *p;
12600
12601 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12602 gABI specifies that PT_NOTE alignment should be aligned to 4
12603 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12604 align is less than 4, we use 4 byte alignment. */
12605 if (align < 4)
12606 align = 4;
12607 if (align != 4 && align != 8)
12608 return false;
12609
12610 p = buf;
12611 while (p < buf + size)
12612 {
12613 Elf_External_Note *xnp = (Elf_External_Note *) p;
12614 Elf_Internal_Note in;
12615
12616 if (offsetof (Elf_External_Note, name) > buf - p + size)
12617 return false;
12618
12619 in.type = H_GET_32 (abfd, xnp->type);
12620
12621 in.namesz = H_GET_32 (abfd, xnp->namesz);
12622 in.namedata = xnp->name;
12623 if (in.namesz > buf - in.namedata + size)
12624 return false;
12625
12626 in.descsz = H_GET_32 (abfd, xnp->descsz);
12627 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
12628 in.descpos = offset + (in.descdata - buf);
12629 if (in.descsz != 0
12630 && (in.descdata >= buf + size
12631 || in.descsz > buf - in.descdata + size))
12632 return false;
12633
12634 switch (bfd_get_format (abfd))
12635 {
12636 default:
12637 return true;
12638
12639 case bfd_core:
12640 {
12641 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
12642 struct
12643 {
12644 const char * string;
12645 size_t len;
12646 bool (*func) (bfd *, Elf_Internal_Note *);
12647 }
12648 grokers[] =
12649 {
12650 GROKER_ELEMENT ("", elfcore_grok_note),
12651 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
12652 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
12653 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
12654 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
12655 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
12656 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
12657 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note)
12658 };
12659 #undef GROKER_ELEMENT
12660 int i;
12661
12662 for (i = ARRAY_SIZE (grokers); i--;)
12663 {
12664 if (in.namesz >= grokers[i].len
12665 && strncmp (in.namedata, grokers[i].string,
12666 grokers[i].len) == 0)
12667 {
12668 if (! grokers[i].func (abfd, & in))
12669 return false;
12670 break;
12671 }
12672 }
12673 break;
12674 }
12675
12676 case bfd_object:
12677 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
12678 {
12679 if (! elfobj_grok_gnu_note (abfd, &in))
12680 return false;
12681 }
12682 else if (in.namesz == sizeof "stapsdt"
12683 && strcmp (in.namedata, "stapsdt") == 0)
12684 {
12685 if (! elfobj_grok_stapsdt_note (abfd, &in))
12686 return false;
12687 }
12688 break;
12689 }
12690
12691 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
12692 }
12693
12694 return true;
12695 }
12696
12697 bool
elf_read_notes(bfd * abfd,file_ptr offset,bfd_size_type size,size_t align)12698 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
12699 size_t align)
12700 {
12701 char *buf;
12702
12703 if (size == 0 || (size + 1) == 0)
12704 return true;
12705
12706 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
12707 return false;
12708
12709 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
12710 if (buf == NULL)
12711 return false;
12712
12713 /* PR 17512: file: ec08f814
12714 0-termintate the buffer so that string searches will not overflow. */
12715 buf[size] = 0;
12716
12717 if (!elf_parse_notes (abfd, buf, size, offset, align))
12718 {
12719 free (buf);
12720 return false;
12721 }
12722
12723 free (buf);
12724 return true;
12725 }
12726
12727 /* Providing external access to the ELF program header table. */
12728
12729 /* Return an upper bound on the number of bytes required to store a
12730 copy of ABFD's program header table entries. Return -1 if an error
12731 occurs; bfd_get_error will return an appropriate code. */
12732
12733 long
bfd_get_elf_phdr_upper_bound(bfd * abfd)12734 bfd_get_elf_phdr_upper_bound (bfd *abfd)
12735 {
12736 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12737 {
12738 bfd_set_error (bfd_error_wrong_format);
12739 return -1;
12740 }
12741
12742 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
12743 }
12744
12745 /* Copy ABFD's program header table entries to *PHDRS. The entries
12746 will be stored as an array of Elf_Internal_Phdr structures, as
12747 defined in include/elf/internal.h. To find out how large the
12748 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12749
12750 Return the number of program header table entries read, or -1 if an
12751 error occurs; bfd_get_error will return an appropriate code. */
12752
12753 int
bfd_get_elf_phdrs(bfd * abfd,void * phdrs)12754 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
12755 {
12756 int num_phdrs;
12757
12758 if (abfd->xvec->flavour != bfd_target_elf_flavour)
12759 {
12760 bfd_set_error (bfd_error_wrong_format);
12761 return -1;
12762 }
12763
12764 num_phdrs = elf_elfheader (abfd)->e_phnum;
12765 if (num_phdrs != 0)
12766 memcpy (phdrs, elf_tdata (abfd)->phdr,
12767 num_phdrs * sizeof (Elf_Internal_Phdr));
12768
12769 return num_phdrs;
12770 }
12771
12772 enum elf_reloc_type_class
_bfd_elf_reloc_type_class(const struct bfd_link_info * info ATTRIBUTE_UNUSED,const asection * rel_sec ATTRIBUTE_UNUSED,const Elf_Internal_Rela * rela ATTRIBUTE_UNUSED)12773 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12774 const asection *rel_sec ATTRIBUTE_UNUSED,
12775 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
12776 {
12777 return reloc_class_normal;
12778 }
12779
12780 /* For RELA architectures, return the relocation value for a
12781 relocation against a local symbol. */
12782
12783 bfd_vma
_bfd_elf_rela_local_sym(bfd * abfd,Elf_Internal_Sym * sym,asection ** psec,Elf_Internal_Rela * rel)12784 _bfd_elf_rela_local_sym (bfd *abfd,
12785 Elf_Internal_Sym *sym,
12786 asection **psec,
12787 Elf_Internal_Rela *rel)
12788 {
12789 asection *sec = *psec;
12790 bfd_vma relocation;
12791
12792 relocation = (sec->output_section->vma
12793 + sec->output_offset
12794 + sym->st_value);
12795 if ((sec->flags & SEC_MERGE)
12796 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
12797 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
12798 {
12799 rel->r_addend =
12800 _bfd_merged_section_offset (abfd, psec,
12801 elf_section_data (sec)->sec_info,
12802 sym->st_value + rel->r_addend);
12803 if (sec != *psec)
12804 {
12805 /* If we have changed the section, and our original section is
12806 marked with SEC_EXCLUDE, it means that the original
12807 SEC_MERGE section has been completely subsumed in some
12808 other SEC_MERGE section. In this case, we need to leave
12809 some info around for --emit-relocs. */
12810 if ((sec->flags & SEC_EXCLUDE) != 0)
12811 sec->kept_section = *psec;
12812 sec = *psec;
12813 }
12814 rel->r_addend -= relocation;
12815 rel->r_addend += sec->output_section->vma + sec->output_offset;
12816 }
12817 return relocation;
12818 }
12819
12820 bfd_vma
_bfd_elf_rel_local_sym(bfd * abfd,Elf_Internal_Sym * sym,asection ** psec,bfd_vma addend)12821 _bfd_elf_rel_local_sym (bfd *abfd,
12822 Elf_Internal_Sym *sym,
12823 asection **psec,
12824 bfd_vma addend)
12825 {
12826 asection *sec = *psec;
12827
12828 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
12829 return sym->st_value + addend;
12830
12831 return _bfd_merged_section_offset (abfd, psec,
12832 elf_section_data (sec)->sec_info,
12833 sym->st_value + addend);
12834 }
12835
12836 /* Adjust an address within a section. Given OFFSET within SEC, return
12837 the new offset within the section, based upon changes made to the
12838 section. Returns -1 if the offset is now invalid.
12839 The offset (in abnd out) is in target sized bytes, however big a
12840 byte may be. */
12841
12842 bfd_vma
_bfd_elf_section_offset(bfd * abfd,struct bfd_link_info * info,asection * sec,bfd_vma offset)12843 _bfd_elf_section_offset (bfd *abfd,
12844 struct bfd_link_info *info,
12845 asection *sec,
12846 bfd_vma offset)
12847 {
12848 switch (sec->sec_info_type)
12849 {
12850 case SEC_INFO_TYPE_STABS:
12851 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
12852 offset);
12853 case SEC_INFO_TYPE_EH_FRAME:
12854 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
12855
12856 default:
12857 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
12858 {
12859 /* Reverse the offset. */
12860 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12861 bfd_size_type address_size = bed->s->arch_size / 8;
12862
12863 /* address_size and sec->size are in octets. Convert
12864 to bytes before subtracting the original offset. */
12865 offset = ((sec->size - address_size)
12866 / bfd_octets_per_byte (abfd, sec) - offset);
12867 }
12868 return offset;
12869 }
12870 }
12871
12872 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12873 reconstruct an ELF file by reading the segments out of remote memory
12874 based on the ELF file header at EHDR_VMA and the ELF program headers it
12875 points to. If not null, *LOADBASEP is filled in with the difference
12876 between the VMAs from which the segments were read, and the VMAs the
12877 file headers (and hence BFD's idea of each section's VMA) put them at.
12878
12879 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12880 remote memory at target address VMA into the local buffer at MYADDR; it
12881 should return zero on success or an `errno' code on failure. TEMPL must
12882 be a BFD for an ELF target with the word size and byte order found in
12883 the remote memory. */
12884
12885 bfd *
bfd_elf_bfd_from_remote_memory(bfd * templ,bfd_vma ehdr_vma,bfd_size_type size,bfd_vma * loadbasep,int (* target_read_memory)(bfd_vma,bfd_byte *,bfd_size_type))12886 bfd_elf_bfd_from_remote_memory
12887 (bfd *templ,
12888 bfd_vma ehdr_vma,
12889 bfd_size_type size,
12890 bfd_vma *loadbasep,
12891 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
12892 {
12893 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
12894 (templ, ehdr_vma, size, loadbasep, target_read_memory);
12895 }
12896
12897 long
_bfd_elf_get_synthetic_symtab(bfd * abfd,long symcount ATTRIBUTE_UNUSED,asymbol ** syms ATTRIBUTE_UNUSED,long dynsymcount,asymbol ** dynsyms,asymbol ** ret)12898 _bfd_elf_get_synthetic_symtab (bfd *abfd,
12899 long symcount ATTRIBUTE_UNUSED,
12900 asymbol **syms ATTRIBUTE_UNUSED,
12901 long dynsymcount,
12902 asymbol **dynsyms,
12903 asymbol **ret)
12904 {
12905 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12906 asection *relplt;
12907 asymbol *s;
12908 const char *relplt_name;
12909 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
12910 arelent *p;
12911 long count, i, n;
12912 size_t size;
12913 Elf_Internal_Shdr *hdr;
12914 char *names;
12915 asection *plt;
12916
12917 *ret = NULL;
12918
12919 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
12920 return 0;
12921
12922 if (dynsymcount <= 0)
12923 return 0;
12924
12925 if (!bed->plt_sym_val)
12926 return 0;
12927
12928 relplt_name = bed->relplt_name;
12929 if (relplt_name == NULL)
12930 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
12931 relplt = bfd_get_section_by_name (abfd, relplt_name);
12932 if (relplt == NULL)
12933 return 0;
12934
12935 hdr = &elf_section_data (relplt)->this_hdr;
12936 if (hdr->sh_link != elf_dynsymtab (abfd)
12937 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
12938 return 0;
12939
12940 plt = bfd_get_section_by_name (abfd, ".plt");
12941 if (plt == NULL)
12942 return 0;
12943
12944 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
12945 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
12946 return -1;
12947
12948 count = relplt->size / hdr->sh_entsize;
12949 size = count * sizeof (asymbol);
12950 p = relplt->relocation;
12951 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12952 {
12953 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
12954 if (p->addend != 0)
12955 {
12956 #ifdef BFD64
12957 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
12958 #else
12959 size += sizeof ("+0x") - 1 + 8;
12960 #endif
12961 }
12962 }
12963
12964 s = *ret = (asymbol *) bfd_malloc (size);
12965 if (s == NULL)
12966 return -1;
12967
12968 names = (char *) (s + count);
12969 p = relplt->relocation;
12970 n = 0;
12971 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
12972 {
12973 size_t len;
12974 bfd_vma addr;
12975
12976 addr = bed->plt_sym_val (i, plt, p);
12977 if (addr == (bfd_vma) -1)
12978 continue;
12979
12980 *s = **p->sym_ptr_ptr;
12981 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12982 we are defining a symbol, ensure one of them is set. */
12983 if ((s->flags & BSF_LOCAL) == 0)
12984 s->flags |= BSF_GLOBAL;
12985 s->flags |= BSF_SYNTHETIC;
12986 s->section = plt;
12987 s->value = addr - plt->vma;
12988 s->name = names;
12989 s->udata.p = NULL;
12990 len = strlen ((*p->sym_ptr_ptr)->name);
12991 memcpy (names, (*p->sym_ptr_ptr)->name, len);
12992 names += len;
12993 if (p->addend != 0)
12994 {
12995 char buf[30], *a;
12996
12997 memcpy (names, "+0x", sizeof ("+0x") - 1);
12998 names += sizeof ("+0x") - 1;
12999 bfd_sprintf_vma (abfd, buf, p->addend);
13000 for (a = buf; *a == '0'; ++a)
13001 ;
13002 len = strlen (a);
13003 memcpy (names, a, len);
13004 names += len;
13005 }
13006 memcpy (names, "@plt", sizeof ("@plt"));
13007 names += sizeof ("@plt");
13008 ++s, ++n;
13009 }
13010
13011 return n;
13012 }
13013
13014 /* It is only used by x86-64 so far.
13015 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
13016 but current usage would allow all of _bfd_std_section to be zero. */
13017 static const asymbol lcomm_sym
13018 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
13019 asection _bfd_elf_large_com_section
13020 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
13021 "LARGE_COMMON", 0, SEC_IS_COMMON);
13022
13023 bool
_bfd_elf_final_write_processing(bfd * abfd)13024 _bfd_elf_final_write_processing (bfd *abfd)
13025 {
13026 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
13027
13028 i_ehdrp = elf_elfheader (abfd);
13029
13030 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13031 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
13032
13033 /* Set the osabi field to ELFOSABI_GNU if the binary contains
13034 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13035 or STB_GNU_UNIQUE binding. */
13036 if (elf_tdata (abfd)->has_gnu_osabi != 0)
13037 {
13038 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13039 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13040 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13041 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13042 {
13043 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13044 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13045 "and FreeBSD targets"));
13046 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13047 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13048 "only by GNU and FreeBSD targets"));
13049 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13050 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13051 "only by GNU and FreeBSD targets"));
13052 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13053 _bfd_error_handler (_("GNU_RETAIN section is supported "
13054 "only by GNU and FreeBSD targets"));
13055 bfd_set_error (bfd_error_sorry);
13056 return false;
13057 }
13058 }
13059 return true;
13060 }
13061
13062
13063 /* Return TRUE for ELF symbol types that represent functions.
13064 This is the default version of this function, which is sufficient for
13065 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
13066
13067 bool
_bfd_elf_is_function_type(unsigned int type)13068 _bfd_elf_is_function_type (unsigned int type)
13069 {
13070 return (type == STT_FUNC
13071 || type == STT_GNU_IFUNC);
13072 }
13073
13074 /* If the ELF symbol SYM might be a function in SEC, return the
13075 function size and set *CODE_OFF to the function's entry point,
13076 otherwise return zero. */
13077
13078 bfd_size_type
_bfd_elf_maybe_function_sym(const asymbol * sym,asection * sec,bfd_vma * code_off)13079 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13080 bfd_vma *code_off)
13081 {
13082 bfd_size_type size;
13083 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13084
13085 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13086 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13087 || sym->section != sec)
13088 return 0;
13089
13090 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13091
13092 /* In theory we should check that the symbol's type satisfies
13093 _bfd_elf_is_function_type(), but there are some function-like
13094 symbols which would fail this test. (eg _start). Instead
13095 we check for hidden, local, notype symbols with zero size.
13096 This type of symbol is generated by the annobin plugin for gcc
13097 and clang, and should not be considered to be a function symbol. */
13098 if (size == 0
13099 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13100 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13101 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13102 return 0;
13103
13104 *code_off = sym->value;
13105 /* Do not return 0 for the function's size. */
13106 return size ? size : 1;
13107 }
13108
13109 /* Set to non-zero to enable some debug messages. */
13110 #define DEBUG_SECONDARY_RELOCS 0
13111
13112 /* An internal-to-the-bfd-library only section type
13113 used to indicate a cached secondary reloc section. */
13114 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
13115
13116 /* Create a BFD section to hold a secondary reloc section. */
13117
13118 bool
_bfd_elf_init_secondary_reloc_section(bfd * abfd,Elf_Internal_Shdr * hdr,const char * name,unsigned int shindex)13119 _bfd_elf_init_secondary_reloc_section (bfd * abfd,
13120 Elf_Internal_Shdr *hdr,
13121 const char * name,
13122 unsigned int shindex)
13123 {
13124 /* We only support RELA secondary relocs. */
13125 if (hdr->sh_type != SHT_RELA)
13126 return false;
13127
13128 #if DEBUG_SECONDARY_RELOCS
13129 fprintf (stderr, "secondary reloc section %s encountered\n", name);
13130 #endif
13131 hdr->sh_type = SHT_SECONDARY_RELOC;
13132 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13133 }
13134
13135 /* Read in any secondary relocs associated with SEC. */
13136
13137 bool
_bfd_elf_slurp_secondary_reloc_section(bfd * abfd,asection * sec,asymbol ** symbols,bool dynamic)13138 _bfd_elf_slurp_secondary_reloc_section (bfd * abfd,
13139 asection * sec,
13140 asymbol ** symbols,
13141 bool dynamic)
13142 {
13143 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13144 asection * relsec;
13145 bool result = true;
13146 bfd_vma (*r_sym) (bfd_vma);
13147
13148 #if BFD_DEFAULT_TARGET_SIZE > 32
13149 if (bfd_arch_bits_per_address (abfd) != 32)
13150 r_sym = elf64_r_sym;
13151 else
13152 #endif
13153 r_sym = elf32_r_sym;
13154
13155 if (!elf_section_data (sec)->has_secondary_relocs)
13156 return true;
13157
13158 /* Discover if there are any secondary reloc sections
13159 associated with SEC. */
13160 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13161 {
13162 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13163
13164 if (hdr->sh_type == SHT_SECONDARY_RELOC
13165 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13166 && (hdr->sh_entsize == ebd->s->sizeof_rel
13167 || hdr->sh_entsize == ebd->s->sizeof_rela))
13168 {
13169 bfd_byte * native_relocs;
13170 bfd_byte * native_reloc;
13171 arelent * internal_relocs;
13172 arelent * internal_reloc;
13173 unsigned int i;
13174 unsigned int entsize;
13175 unsigned int symcount;
13176 unsigned int reloc_count;
13177 size_t amt;
13178
13179 if (ebd->elf_info_to_howto == NULL)
13180 return false;
13181
13182 #if DEBUG_SECONDARY_RELOCS
13183 fprintf (stderr, "read secondary relocs for %s from %s\n",
13184 sec->name, relsec->name);
13185 #endif
13186 entsize = hdr->sh_entsize;
13187
13188 native_relocs = bfd_malloc (hdr->sh_size);
13189 if (native_relocs == NULL)
13190 {
13191 result = false;
13192 continue;
13193 }
13194
13195 reloc_count = NUM_SHDR_ENTRIES (hdr);
13196 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13197 {
13198 free (native_relocs);
13199 bfd_set_error (bfd_error_file_too_big);
13200 result = false;
13201 continue;
13202 }
13203
13204 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13205 if (internal_relocs == NULL)
13206 {
13207 free (native_relocs);
13208 result = false;
13209 continue;
13210 }
13211
13212 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13213 || (bfd_bread (native_relocs, hdr->sh_size, abfd)
13214 != hdr->sh_size))
13215 {
13216 free (native_relocs);
13217 /* The internal_relocs will be freed when
13218 the memory for the bfd is released. */
13219 result = false;
13220 continue;
13221 }
13222
13223 if (dynamic)
13224 symcount = bfd_get_dynamic_symcount (abfd);
13225 else
13226 symcount = bfd_get_symcount (abfd);
13227
13228 for (i = 0, internal_reloc = internal_relocs,
13229 native_reloc = native_relocs;
13230 i < reloc_count;
13231 i++, internal_reloc++, native_reloc += entsize)
13232 {
13233 bool res;
13234 Elf_Internal_Rela rela;
13235
13236 if (entsize == ebd->s->sizeof_rel)
13237 ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13238 else /* entsize == ebd->s->sizeof_rela */
13239 ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13240
13241 /* The address of an ELF reloc is section relative for an object
13242 file, and absolute for an executable file or shared library.
13243 The address of a normal BFD reloc is always section relative,
13244 and the address of a dynamic reloc is absolute.. */
13245 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13246 internal_reloc->address = rela.r_offset;
13247 else
13248 internal_reloc->address = rela.r_offset - sec->vma;
13249
13250 if (r_sym (rela.r_info) == STN_UNDEF)
13251 {
13252 /* FIXME: This and the error case below mean that we
13253 have a symbol on relocs that is not elf_symbol_type. */
13254 internal_reloc->sym_ptr_ptr =
13255 bfd_abs_section_ptr->symbol_ptr_ptr;
13256 }
13257 else if (r_sym (rela.r_info) > symcount)
13258 {
13259 _bfd_error_handler
13260 /* xgettext:c-format */
13261 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
13262 abfd, sec, i, (long) r_sym (rela.r_info));
13263 bfd_set_error (bfd_error_bad_value);
13264 internal_reloc->sym_ptr_ptr =
13265 bfd_abs_section_ptr->symbol_ptr_ptr;
13266 result = false;
13267 }
13268 else
13269 {
13270 asymbol **ps;
13271
13272 ps = symbols + r_sym (rela.r_info) - 1;
13273 internal_reloc->sym_ptr_ptr = ps;
13274 /* Make sure that this symbol is not removed by strip. */
13275 (*ps)->flags |= BSF_KEEP;
13276 }
13277
13278 internal_reloc->addend = rela.r_addend;
13279
13280 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13281 if (! res || internal_reloc->howto == NULL)
13282 {
13283 #if DEBUG_SECONDARY_RELOCS
13284 fprintf (stderr, "there is no howto associated with reloc %lx\n",
13285 rela.r_info);
13286 #endif
13287 result = false;
13288 }
13289 }
13290
13291 free (native_relocs);
13292 /* Store the internal relocs. */
13293 elf_section_data (relsec)->sec_info = internal_relocs;
13294 }
13295 }
13296
13297 return result;
13298 }
13299
13300 /* Set the ELF section header fields of an output secondary reloc section. */
13301
13302 bool
_bfd_elf_copy_special_section_fields(const bfd * ibfd ATTRIBUTE_UNUSED,bfd * obfd ATTRIBUTE_UNUSED,const Elf_Internal_Shdr * isection,Elf_Internal_Shdr * osection)13303 _bfd_elf_copy_special_section_fields (const bfd * ibfd ATTRIBUTE_UNUSED,
13304 bfd * obfd ATTRIBUTE_UNUSED,
13305 const Elf_Internal_Shdr * isection,
13306 Elf_Internal_Shdr * osection)
13307 {
13308 asection * isec;
13309 asection * osec;
13310 struct bfd_elf_section_data * esd;
13311
13312 if (isection == NULL)
13313 return false;
13314
13315 if (isection->sh_type != SHT_SECONDARY_RELOC)
13316 return true;
13317
13318 isec = isection->bfd_section;
13319 if (isec == NULL)
13320 return false;
13321
13322 osec = osection->bfd_section;
13323 if (osec == NULL)
13324 return false;
13325
13326 esd = elf_section_data (osec);
13327 BFD_ASSERT (esd->sec_info == NULL);
13328 esd->sec_info = elf_section_data (isec)->sec_info;
13329 osection->sh_type = SHT_RELA;
13330 osection->sh_link = elf_onesymtab (obfd);
13331 if (osection->sh_link == 0)
13332 {
13333 /* There is no symbol table - we are hosed... */
13334 _bfd_error_handler
13335 /* xgettext:c-format */
13336 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
13337 obfd, osec);
13338 bfd_set_error (bfd_error_bad_value);
13339 return false;
13340 }
13341
13342 /* Find the output section that corresponds to the isection's sh_info link. */
13343 if (isection->sh_info == 0
13344 || isection->sh_info >= elf_numsections (ibfd))
13345 {
13346 _bfd_error_handler
13347 /* xgettext:c-format */
13348 (_("%pB(%pA): info section index is invalid"),
13349 obfd, osec);
13350 bfd_set_error (bfd_error_bad_value);
13351 return false;
13352 }
13353
13354 isection = elf_elfsections (ibfd)[isection->sh_info];
13355
13356 if (isection == NULL
13357 || isection->bfd_section == NULL
13358 || isection->bfd_section->output_section == NULL)
13359 {
13360 _bfd_error_handler
13361 /* xgettext:c-format */
13362 (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
13363 obfd, osec);
13364 bfd_set_error (bfd_error_bad_value);
13365 return false;
13366 }
13367
13368 esd = elf_section_data (isection->bfd_section->output_section);
13369 BFD_ASSERT (esd != NULL);
13370 osection->sh_info = esd->this_idx;
13371 esd->has_secondary_relocs = true;
13372 #if DEBUG_SECONDARY_RELOCS
13373 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13374 osec->name, osection->sh_link, osection->sh_info);
13375 fprintf (stderr, "mark section %s as having secondary relocs\n",
13376 bfd_section_name (isection->bfd_section->output_section));
13377 #endif
13378
13379 return true;
13380 }
13381
13382 /* Write out a secondary reloc section.
13383
13384 FIXME: Currently this function can result in a serious performance penalty
13385 for files with secondary relocs and lots of sections. The proper way to
13386 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13387 relocs together and then to have this function just walk that chain. */
13388
13389 bool
_bfd_elf_write_secondary_reloc_section(bfd * abfd,asection * sec)13390 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
13391 {
13392 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
13393 bfd_vma addr_offset;
13394 asection * relsec;
13395 bfd_vma (*r_info) (bfd_vma, bfd_vma);
13396 bool result = true;
13397
13398 if (sec == NULL)
13399 return false;
13400
13401 #if BFD_DEFAULT_TARGET_SIZE > 32
13402 if (bfd_arch_bits_per_address (abfd) != 32)
13403 r_info = elf64_r_info;
13404 else
13405 #endif
13406 r_info = elf32_r_info;
13407
13408 /* The address of an ELF reloc is section relative for an object
13409 file, and absolute for an executable file or shared library.
13410 The address of a BFD reloc is always section relative. */
13411 addr_offset = 0;
13412 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13413 addr_offset = sec->vma;
13414
13415 /* Discover if there are any secondary reloc sections
13416 associated with SEC. */
13417 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13418 {
13419 const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13420 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13421
13422 if (hdr->sh_type == SHT_RELA
13423 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13424 {
13425 asymbol * last_sym;
13426 int last_sym_idx;
13427 unsigned int reloc_count;
13428 unsigned int idx;
13429 unsigned int entsize;
13430 arelent * src_irel;
13431 bfd_byte * dst_rela;
13432
13433 if (hdr->contents != NULL)
13434 {
13435 _bfd_error_handler
13436 /* xgettext:c-format */
13437 (_("%pB(%pA): error: secondary reloc section processed twice"),
13438 abfd, relsec);
13439 bfd_set_error (bfd_error_bad_value);
13440 result = false;
13441 continue;
13442 }
13443
13444 entsize = hdr->sh_entsize;
13445 if (entsize == 0)
13446 {
13447 _bfd_error_handler
13448 /* xgettext:c-format */
13449 (_("%pB(%pA): error: secondary reloc section has zero sized entries"),
13450 abfd, relsec);
13451 bfd_set_error (bfd_error_bad_value);
13452 result = false;
13453 continue;
13454 }
13455 else if (entsize != ebd->s->sizeof_rel
13456 && entsize != ebd->s->sizeof_rela)
13457 {
13458 _bfd_error_handler
13459 /* xgettext:c-format */
13460 (_("%pB(%pA): error: secondary reloc section has non-standard sized entries"),
13461 abfd, relsec);
13462 bfd_set_error (bfd_error_bad_value);
13463 result = false;
13464 continue;
13465 }
13466
13467 reloc_count = hdr->sh_size / entsize;
13468 if (reloc_count <= 0)
13469 {
13470 _bfd_error_handler
13471 /* xgettext:c-format */
13472 (_("%pB(%pA): error: secondary reloc section is empty!"),
13473 abfd, relsec);
13474 bfd_set_error (bfd_error_bad_value);
13475 result = false;
13476 continue;
13477 }
13478
13479 hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13480 if (hdr->contents == NULL)
13481 continue;
13482
13483 #if DEBUG_SECONDARY_RELOCS
13484 fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13485 reloc_count, sec->name, relsec->name);
13486 #endif
13487 last_sym = NULL;
13488 last_sym_idx = 0;
13489 dst_rela = hdr->contents;
13490 src_irel = (arelent *) esd->sec_info;
13491 if (src_irel == NULL)
13492 {
13493 _bfd_error_handler
13494 /* xgettext:c-format */
13495 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
13496 abfd, relsec);
13497 bfd_set_error (bfd_error_bad_value);
13498 result = false;
13499 continue;
13500 }
13501
13502 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
13503 {
13504 Elf_Internal_Rela src_rela;
13505 arelent *ptr;
13506 asymbol *sym;
13507 int n;
13508
13509 ptr = src_irel + idx;
13510 if (ptr == NULL)
13511 {
13512 _bfd_error_handler
13513 /* xgettext:c-format */
13514 (_("%pB(%pA): error: reloc table entry %u is empty"),
13515 abfd, relsec, idx);
13516 bfd_set_error (bfd_error_bad_value);
13517 result = false;
13518 break;
13519 }
13520
13521 if (ptr->sym_ptr_ptr == NULL)
13522 {
13523 /* FIXME: Is this an error ? */
13524 n = 0;
13525 }
13526 else
13527 {
13528 sym = *ptr->sym_ptr_ptr;
13529
13530 if (sym == last_sym)
13531 n = last_sym_idx;
13532 else
13533 {
13534 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
13535 if (n < 0)
13536 {
13537 _bfd_error_handler
13538 /* xgettext:c-format */
13539 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
13540 abfd, relsec, idx);
13541 bfd_set_error (bfd_error_bad_value);
13542 result = false;
13543 n = 0;
13544 }
13545
13546 last_sym = sym;
13547 last_sym_idx = n;
13548 }
13549
13550 if (sym->the_bfd != NULL
13551 && sym->the_bfd->xvec != abfd->xvec
13552 && ! _bfd_elf_validate_reloc (abfd, ptr))
13553 {
13554 _bfd_error_handler
13555 /* xgettext:c-format */
13556 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
13557 abfd, relsec, idx);
13558 bfd_set_error (bfd_error_bad_value);
13559 result = false;
13560 n = 0;
13561 }
13562 }
13563
13564 src_rela.r_offset = ptr->address + addr_offset;
13565 if (ptr->howto == NULL)
13566 {
13567 _bfd_error_handler
13568 /* xgettext:c-format */
13569 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
13570 abfd, relsec, idx);
13571 bfd_set_error (bfd_error_bad_value);
13572 result = false;
13573 src_rela.r_info = r_info (0, 0);
13574 }
13575 else
13576 src_rela.r_info = r_info (n, ptr->howto->type);
13577 src_rela.r_addend = ptr->addend;
13578
13579 if (entsize == ebd->s->sizeof_rel)
13580 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
13581 else /* entsize == ebd->s->sizeof_rela */
13582 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
13583 }
13584 }
13585 }
13586
13587 return result;
13588 }
13589