Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 | 2 | |
b3adc24a | 3 | Copyright (C) 1993-2020 Free Software Foundation, Inc. |
252b5132 | 4 | |
5e8d7549 | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
5e8d7549 NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
5e8d7549 | 10 | (at your option) any later version. |
252b5132 | 11 | |
5e8d7549 NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
5e8d7549 | 17 | You should have received a copy of the GNU General Public License |
b34976b6 | 18 | along with this program; if not, write to the Free Software |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
21 | ||
252b5132 | 22 | |
1b74d094 BW |
23 | /* |
24 | SECTION | |
252b5132 RH |
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 | |
661a3fd4 | 33 | haven't bothered yet. */ |
252b5132 | 34 | |
7ee38065 MS |
35 | /* For sparc64-cross-sparc32. */ |
36 | #define _SYSCALL32 | |
252b5132 | 37 | #include "sysdep.h" |
3a551c7a | 38 | #include <limits.h> |
3db64b00 | 39 | #include "bfd.h" |
252b5132 RH |
40 | #include "bfdlink.h" |
41 | #include "libbfd.h" | |
42 | #define ARCH_SIZE 0 | |
43 | #include "elf-bfd.h" | |
e0e8c97f | 44 | #include "libiberty.h" |
ff59fc36 | 45 | #include "safe-ctype.h" |
de64ce13 | 46 | #include "elf-linux-core.h" |
252b5132 | 47 | |
8bc7f138 L |
48 | #ifdef CORE_HEADER |
49 | #include CORE_HEADER | |
50 | #endif | |
51 | ||
217aa764 | 52 | static int elf_sort_sections (const void *, const void *); |
c84fca4d | 53 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); |
ef10c3ac | 54 | static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ; |
718175fa | 55 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, |
276da9b3 | 56 | file_ptr offset, size_t align); |
50b2bdb7 | 57 | |
252b5132 RH |
58 | /* Swap version information in and out. The version information is |
59 | currently size independent. If that ever changes, this code will | |
60 | need to move into elfcode.h. */ | |
61 | ||
62 | /* Swap in a Verdef structure. */ | |
63 | ||
64 | void | |
217aa764 AM |
65 | _bfd_elf_swap_verdef_in (bfd *abfd, |
66 | const Elf_External_Verdef *src, | |
67 | Elf_Internal_Verdef *dst) | |
252b5132 | 68 | { |
dc810e39 AM |
69 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
70 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
71 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
72 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
73 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
74 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
75 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
76 | } |
77 | ||
78 | /* Swap out a Verdef structure. */ | |
79 | ||
80 | void | |
217aa764 AM |
81 | _bfd_elf_swap_verdef_out (bfd *abfd, |
82 | const Elf_Internal_Verdef *src, | |
83 | Elf_External_Verdef *dst) | |
252b5132 | 84 | { |
dc810e39 AM |
85 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
86 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
87 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
88 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
89 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
90 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
91 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
92 | } |
93 | ||
94 | /* Swap in a Verdaux structure. */ | |
95 | ||
96 | void | |
217aa764 AM |
97 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
98 | const Elf_External_Verdaux *src, | |
99 | Elf_Internal_Verdaux *dst) | |
252b5132 | 100 | { |
dc810e39 AM |
101 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
102 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
103 | } |
104 | ||
105 | /* Swap out a Verdaux structure. */ | |
106 | ||
107 | void | |
217aa764 AM |
108 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
109 | const Elf_Internal_Verdaux *src, | |
110 | Elf_External_Verdaux *dst) | |
252b5132 | 111 | { |
dc810e39 AM |
112 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
113 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
114 | } |
115 | ||
116 | /* Swap in a Verneed structure. */ | |
117 | ||
118 | void | |
217aa764 AM |
119 | _bfd_elf_swap_verneed_in (bfd *abfd, |
120 | const Elf_External_Verneed *src, | |
121 | Elf_Internal_Verneed *dst) | |
252b5132 | 122 | { |
dc810e39 AM |
123 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
124 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
125 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
126 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
127 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
128 | } |
129 | ||
130 | /* Swap out a Verneed structure. */ | |
131 | ||
132 | void | |
217aa764 AM |
133 | _bfd_elf_swap_verneed_out (bfd *abfd, |
134 | const Elf_Internal_Verneed *src, | |
135 | Elf_External_Verneed *dst) | |
252b5132 | 136 | { |
dc810e39 AM |
137 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
138 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
139 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
140 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
141 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
142 | } |
143 | ||
144 | /* Swap in a Vernaux structure. */ | |
145 | ||
146 | void | |
217aa764 AM |
147 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
148 | const Elf_External_Vernaux *src, | |
149 | Elf_Internal_Vernaux *dst) | |
252b5132 | 150 | { |
dc810e39 AM |
151 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
152 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
153 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
154 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
155 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
156 | } |
157 | ||
158 | /* Swap out a Vernaux structure. */ | |
159 | ||
160 | void | |
217aa764 AM |
161 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
162 | const Elf_Internal_Vernaux *src, | |
163 | Elf_External_Vernaux *dst) | |
252b5132 | 164 | { |
dc810e39 AM |
165 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
166 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
167 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
168 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
169 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
170 | } |
171 | ||
172 | /* Swap in a Versym structure. */ | |
173 | ||
174 | void | |
217aa764 AM |
175 | _bfd_elf_swap_versym_in (bfd *abfd, |
176 | const Elf_External_Versym *src, | |
177 | Elf_Internal_Versym *dst) | |
252b5132 | 178 | { |
dc810e39 | 179 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
180 | } |
181 | ||
182 | /* Swap out a Versym structure. */ | |
183 | ||
184 | void | |
217aa764 AM |
185 | _bfd_elf_swap_versym_out (bfd *abfd, |
186 | const Elf_Internal_Versym *src, | |
187 | Elf_External_Versym *dst) | |
252b5132 | 188 | { |
dc810e39 | 189 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
190 | } |
191 | ||
192 | /* Standard ELF hash function. Do not change this function; you will | |
193 | cause invalid hash tables to be generated. */ | |
3a99b017 | 194 | |
252b5132 | 195 | unsigned long |
217aa764 | 196 | bfd_elf_hash (const char *namearg) |
252b5132 | 197 | { |
3a99b017 | 198 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
199 | unsigned long h = 0; |
200 | unsigned long g; | |
201 | int ch; | |
202 | ||
203 | while ((ch = *name++) != '\0') | |
204 | { | |
205 | h = (h << 4) + ch; | |
206 | if ((g = (h & 0xf0000000)) != 0) | |
207 | { | |
208 | h ^= g >> 24; | |
209 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
210 | this case and on some machines one insn instead of two. */ | |
211 | h ^= g; | |
212 | } | |
213 | } | |
32dfa85d | 214 | return h & 0xffffffff; |
252b5132 RH |
215 | } |
216 | ||
fdc90cb4 JJ |
217 | /* DT_GNU_HASH hash function. Do not change this function; you will |
218 | cause invalid hash tables to be generated. */ | |
219 | ||
220 | unsigned long | |
221 | bfd_elf_gnu_hash (const char *namearg) | |
222 | { | |
223 | const unsigned char *name = (const unsigned char *) namearg; | |
224 | unsigned long h = 5381; | |
225 | unsigned char ch; | |
226 | ||
227 | while ((ch = *name++) != '\0') | |
228 | h = (h << 5) + h + ch; | |
229 | return h & 0xffffffff; | |
230 | } | |
231 | ||
0c8d6e5c AM |
232 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
233 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 234 | bfd_boolean |
0c8d6e5c | 235 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd | 236 | size_t object_size, |
4dfe6ac6 | 237 | enum elf_target_id object_id) |
252b5132 | 238 | { |
0ffa91dd NC |
239 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
240 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
241 | if (abfd->tdata.any == NULL) | |
242 | return FALSE; | |
252b5132 | 243 | |
0ffa91dd | 244 | elf_object_id (abfd) = object_id; |
c0355132 AM |
245 | if (abfd->direction != read_direction) |
246 | { | |
247 | struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); | |
248 | if (o == NULL) | |
249 | return FALSE; | |
250 | elf_tdata (abfd)->o = o; | |
251 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
252 | } | |
b34976b6 | 253 | return TRUE; |
252b5132 RH |
254 | } |
255 | ||
0ffa91dd NC |
256 | |
257 | bfd_boolean | |
ae95ffa6 | 258 | bfd_elf_make_object (bfd *abfd) |
0ffa91dd | 259 | { |
ae95ffa6 | 260 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
0ffa91dd | 261 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), |
ae95ffa6 | 262 | bed->target_id); |
0ffa91dd NC |
263 | } |
264 | ||
b34976b6 | 265 | bfd_boolean |
217aa764 | 266 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 267 | { |
c044fabd | 268 | /* I think this can be done just like an object file. */ |
228e534f AM |
269 | if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) |
270 | return FALSE; | |
271 | elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); | |
272 | return elf_tdata (abfd)->core != NULL; | |
252b5132 RH |
273 | } |
274 | ||
6d5944fc | 275 | char * |
217aa764 | 276 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
277 | { |
278 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 279 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
280 | file_ptr offset; |
281 | bfd_size_type shstrtabsize; | |
252b5132 RH |
282 | |
283 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
284 | if (i_shdrp == 0 |
285 | || shindex >= elf_numsections (abfd) | |
286 | || i_shdrp[shindex] == 0) | |
f075ee0c | 287 | return NULL; |
252b5132 | 288 | |
f075ee0c | 289 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
290 | if (shstrtab == NULL) |
291 | { | |
c044fabd | 292 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
293 | offset = i_shdrp[shindex]->sh_offset; |
294 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
295 | |
296 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
297 | in case the string table is not terminated. */ | |
3471d59d | 298 | if (shstrtabsize + 1 <= 1 |
06614111 | 299 | || bfd_seek (abfd, offset, SEEK_SET) != 0 |
2bb3687b AM |
300 | || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1, |
301 | shstrtabsize)) == NULL) | |
302 | { | |
3471d59d CC |
303 | /* Once we've failed to read it, make sure we don't keep |
304 | trying. Otherwise, we'll keep allocating space for | |
305 | the string table over and over. */ | |
306 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
307 | } |
308 | else | |
309 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 310 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 311 | } |
f075ee0c | 312 | return (char *) shstrtab; |
252b5132 RH |
313 | } |
314 | ||
315 | char * | |
217aa764 AM |
316 | bfd_elf_string_from_elf_section (bfd *abfd, |
317 | unsigned int shindex, | |
318 | unsigned int strindex) | |
252b5132 RH |
319 | { |
320 | Elf_Internal_Shdr *hdr; | |
321 | ||
322 | if (strindex == 0) | |
323 | return ""; | |
324 | ||
74f2e02b AM |
325 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
326 | return NULL; | |
327 | ||
252b5132 RH |
328 | hdr = elf_elfsections (abfd)[shindex]; |
329 | ||
06614111 NC |
330 | if (hdr->contents == NULL) |
331 | { | |
332 | if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) | |
333 | { | |
334 | /* PR 17512: file: f057ec89. */ | |
695344c0 | 335 | /* xgettext:c-format */ |
871b3ab2 | 336 | _bfd_error_handler (_("%pB: attempt to load strings from" |
63a5468a | 337 | " a non-string section (number %d)"), |
06614111 NC |
338 | abfd, shindex); |
339 | return NULL; | |
340 | } | |
b1fa9dd6 | 341 | |
06614111 NC |
342 | if (bfd_elf_get_str_section (abfd, shindex) == NULL) |
343 | return NULL; | |
344 | } | |
eed5def8 NC |
345 | else |
346 | { | |
347 | /* PR 24273: The string section's contents may have already | |
348 | been loaded elsewhere, eg because a corrupt file has the | |
349 | string section index in the ELF header pointing at a group | |
350 | section. So be paranoid, and test that the last byte of | |
351 | the section is zero. */ | |
352 | if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0) | |
353 | return NULL; | |
354 | } | |
252b5132 RH |
355 | |
356 | if (strindex >= hdr->sh_size) | |
357 | { | |
1b3a8575 | 358 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
4eca0228 | 359 | _bfd_error_handler |
695344c0 | 360 | /* xgettext:c-format */ |
2dcf00ce AM |
361 | (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"), |
362 | abfd, strindex, (uint64_t) hdr->sh_size, | |
1b3a8575 | 363 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 364 | ? ".shstrtab" |
1b3a8575 | 365 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 366 | return NULL; |
252b5132 RH |
367 | } |
368 | ||
369 | return ((char *) hdr->contents) + strindex; | |
370 | } | |
371 | ||
6cdc0ccc AM |
372 | /* Read and convert symbols to internal format. |
373 | SYMCOUNT specifies the number of symbols to read, starting from | |
374 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
375 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
376 | symbols, and symbol section index extensions, respectively. |
377 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
378 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
379 | |
380 | Elf_Internal_Sym * | |
217aa764 AM |
381 | bfd_elf_get_elf_syms (bfd *ibfd, |
382 | Elf_Internal_Shdr *symtab_hdr, | |
383 | size_t symcount, | |
384 | size_t symoffset, | |
385 | Elf_Internal_Sym *intsym_buf, | |
386 | void *extsym_buf, | |
387 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
388 | { |
389 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 390 | void *alloc_ext; |
df622259 | 391 | const bfd_byte *esym; |
6cdc0ccc AM |
392 | Elf_External_Sym_Shndx *alloc_extshndx; |
393 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 394 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
395 | Elf_Internal_Sym *isym; |
396 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 397 | const struct elf_backend_data *bed; |
6cdc0ccc | 398 | size_t extsym_size; |
1f4361a7 | 399 | size_t amt; |
6cdc0ccc AM |
400 | file_ptr pos; |
401 | ||
e44a2c9c AM |
402 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
403 | abort (); | |
404 | ||
6cdc0ccc AM |
405 | if (symcount == 0) |
406 | return intsym_buf; | |
407 | ||
408 | /* Normal syms might have section extension entries. */ | |
409 | shndx_hdr = NULL; | |
6a40cf0c NC |
410 | if (elf_symtab_shndx_list (ibfd) != NULL) |
411 | { | |
412 | elf_section_list * entry; | |
413 | Elf_Internal_Shdr **sections = elf_elfsections (ibfd); | |
414 | ||
415 | /* Find an index section that is linked to this symtab section. */ | |
416 | for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) | |
315350be NC |
417 | { |
418 | /* PR 20063. */ | |
419 | if (entry->hdr.sh_link >= elf_numsections (ibfd)) | |
420 | continue; | |
421 | ||
422 | if (sections[entry->hdr.sh_link] == symtab_hdr) | |
423 | { | |
424 | shndx_hdr = & entry->hdr; | |
425 | break; | |
426 | }; | |
427 | } | |
6a40cf0c NC |
428 | |
429 | if (shndx_hdr == NULL) | |
430 | { | |
431 | if (symtab_hdr == & elf_symtab_hdr (ibfd)) | |
432 | /* Not really accurate, but this was how the old code used to work. */ | |
433 | shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr; | |
434 | /* Otherwise we do nothing. The assumption is that | |
435 | the index table will not be needed. */ | |
436 | } | |
437 | } | |
6cdc0ccc AM |
438 | |
439 | /* Read the symbols. */ | |
440 | alloc_ext = NULL; | |
441 | alloc_extshndx = NULL; | |
4dd07732 | 442 | alloc_intsym = NULL; |
6cdc0ccc AM |
443 | bed = get_elf_backend_data (ibfd); |
444 | extsym_size = bed->s->sizeof_sym; | |
1f4361a7 AM |
445 | if (_bfd_mul_overflow (symcount, extsym_size, &amt)) |
446 | { | |
447 | bfd_set_error (bfd_error_file_too_big); | |
448 | intsym_buf = NULL; | |
449 | goto out; | |
450 | } | |
6cdc0ccc AM |
451 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; |
452 | if (extsym_buf == NULL) | |
453 | { | |
1f4361a7 | 454 | alloc_ext = bfd_malloc (amt); |
6cdc0ccc AM |
455 | extsym_buf = alloc_ext; |
456 | } | |
457 | if (extsym_buf == NULL | |
458 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
459 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
460 | { | |
461 | intsym_buf = NULL; | |
462 | goto out; | |
463 | } | |
464 | ||
465 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
466 | extshndx_buf = NULL; | |
467 | else | |
468 | { | |
1f4361a7 AM |
469 | if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt)) |
470 | { | |
471 | bfd_set_error (bfd_error_file_too_big); | |
472 | intsym_buf = NULL; | |
473 | goto out; | |
474 | } | |
6cdc0ccc AM |
475 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); |
476 | if (extshndx_buf == NULL) | |
477 | { | |
1f4361a7 | 478 | alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
6cdc0ccc AM |
479 | extshndx_buf = alloc_extshndx; |
480 | } | |
481 | if (extshndx_buf == NULL | |
482 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
483 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
484 | { | |
485 | intsym_buf = NULL; | |
486 | goto out; | |
487 | } | |
488 | } | |
489 | ||
490 | if (intsym_buf == NULL) | |
491 | { | |
1f4361a7 AM |
492 | if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt)) |
493 | { | |
494 | bfd_set_error (bfd_error_file_too_big); | |
495 | goto out; | |
496 | } | |
497 | alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt); | |
4dd07732 | 498 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
499 | if (intsym_buf == NULL) |
500 | goto out; | |
501 | } | |
502 | ||
503 | /* Convert the symbols to internal form. */ | |
504 | isymend = intsym_buf + symcount; | |
a50b1753 | 505 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
07d6d2b8 | 506 | shndx = extshndx_buf; |
6cdc0ccc AM |
507 | isym < isymend; |
508 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
509 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
510 | { | |
511 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
695344c0 | 512 | /* xgettext:c-format */ |
871b3ab2 | 513 | _bfd_error_handler (_("%pB symbol number %lu references" |
63a5468a | 514 | " nonexistent SHT_SYMTAB_SHNDX section"), |
4eca0228 | 515 | ibfd, (unsigned long) symoffset); |
4dd07732 AM |
516 | if (alloc_intsym != NULL) |
517 | free (alloc_intsym); | |
8384fb8f AM |
518 | intsym_buf = NULL; |
519 | goto out; | |
520 | } | |
6cdc0ccc AM |
521 | |
522 | out: | |
523 | if (alloc_ext != NULL) | |
524 | free (alloc_ext); | |
525 | if (alloc_extshndx != NULL) | |
526 | free (alloc_extshndx); | |
527 | ||
528 | return intsym_buf; | |
529 | } | |
530 | ||
5cab59f6 AM |
531 | /* Look up a symbol name. */ |
532 | const char * | |
be8dd2ca AM |
533 | bfd_elf_sym_name (bfd *abfd, |
534 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
535 | Elf_Internal_Sym *isym, |
536 | asection *sym_sec) | |
5cab59f6 | 537 | { |
26c61ae5 | 538 | const char *name; |
5cab59f6 | 539 | unsigned int iname = isym->st_name; |
be8dd2ca | 540 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 541 | |
138f35cc JJ |
542 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
543 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 544 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
545 | { |
546 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
547 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
548 | } | |
549 | ||
26c61ae5 L |
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') | |
fd361982 | 554 | name = bfd_section_name (sym_sec); |
26c61ae5 L |
555 | |
556 | return name; | |
5cab59f6 AM |
557 | } |
558 | ||
dbb410c3 AM |
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 | ||
b885599b AM |
568 | /* Return the name of the group signature symbol. Why isn't the |
569 | signature just a string? */ | |
570 | ||
571 | static const char * | |
217aa764 | 572 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 573 | { |
9dce4196 | 574 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
575 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
576 | Elf_External_Sym_Shndx eshndx; | |
577 | Elf_Internal_Sym isym; | |
b885599b | 578 | |
13792e9d L |
579 | /* First we need to ensure the symbol table is available. Make sure |
580 | that it is a symbol table section. */ | |
4fbb74a6 AM |
581 | if (ghdr->sh_link >= elf_numsections (abfd)) |
582 | return NULL; | |
13792e9d L |
583 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
584 | if (hdr->sh_type != SHT_SYMTAB | |
585 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
586 | return NULL; |
587 | ||
9dce4196 AM |
588 | /* Go read the symbol. */ |
589 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
590 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
591 | &isym, esym, &eshndx) == NULL) | |
b885599b | 592 | return NULL; |
9dce4196 | 593 | |
26c61ae5 | 594 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
595 | } |
596 | ||
dbb410c3 AM |
597 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
598 | ||
b34976b6 | 599 | static bfd_boolean |
217aa764 | 600 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
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. */ | |
9ad5cbcf | 612 | shnum = elf_numsections (abfd); |
dbb410c3 | 613 | num_group = 0; |
08a40648 | 614 | |
44534af3 | 615 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ |
1783205a | 616 | ( (shdr)->sh_type == SHT_GROUP \ |
44534af3 | 617 | && (shdr)->sh_size >= minsize \ |
1783205a NC |
618 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ |
619 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 620 | |
dbb410c3 AM |
621 | for (i = 0; i < shnum; i++) |
622 | { | |
623 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 624 | |
44534af3 | 625 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 AM |
626 | num_group += 1; |
627 | } | |
628 | ||
629 | if (num_group == 0) | |
20dbb49d L |
630 | { |
631 | num_group = (unsigned) -1; | |
632 | elf_tdata (abfd)->num_group = num_group; | |
ce497010 | 633 | elf_tdata (abfd)->group_sect_ptr = NULL; |
20dbb49d L |
634 | } |
635 | else | |
dbb410c3 AM |
636 | { |
637 | /* We keep a list of elf section headers for group sections, | |
638 | so we can find them quickly. */ | |
1f4361a7 | 639 | size_t amt; |
d0fb9a8d | 640 | |
20dbb49d | 641 | elf_tdata (abfd)->num_group = num_group; |
1f4361a7 AM |
642 | amt = num_group * sizeof (Elf_Internal_Shdr *); |
643 | elf_tdata (abfd)->group_sect_ptr | |
644 | = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt); | |
dbb410c3 | 645 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 646 | return FALSE; |
dbb410c3 | 647 | num_group = 0; |
ce497010 | 648 | |
dbb410c3 AM |
649 | for (i = 0; i < shnum; i++) |
650 | { | |
651 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 652 | |
44534af3 | 653 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 | 654 | { |
973ffd63 | 655 | unsigned char *src; |
dbb410c3 AM |
656 | Elf_Internal_Group *dest; |
657 | ||
07d6d2b8 AM |
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 | ||
dbb410c3 AM |
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. */ | |
1f4361a7 AM |
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) | |
1f4361a7 | 672 | || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 |
2bb3687b AM |
673 | || !(shdr->contents |
674 | = _bfd_alloc_and_read (abfd, amt, shdr->sh_size))) | |
493a3386 NC |
675 | { |
676 | _bfd_error_handler | |
695344c0 | 677 | /* xgettext:c-format */ |
871b3ab2 | 678 | (_("%pB: invalid size field in group section" |
2dcf00ce AM |
679 | " header: %#" PRIx64 ""), |
680 | abfd, (uint64_t) shdr->sh_size); | |
493a3386 NC |
681 | bfd_set_error (bfd_error_bad_value); |
682 | -- num_group; | |
493a3386 NC |
683 | continue; |
684 | } | |
708d7d0d | 685 | |
dbb410c3 AM |
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); | |
06614111 | 692 | |
dbb410c3 AM |
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 | { | |
327301a4 | 702 | dest->shdr = NULL; |
dbb410c3 | 703 | dest->flags = idx; |
b885599b AM |
704 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
705 | shdr->bfd_section->flags | |
706 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
707 | break; |
708 | } | |
4bba0fb1 | 709 | if (idx < shnum) |
bae363f1 L |
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 | } | |
4bba0fb1 AM |
719 | if (idx >= shnum |
720 | || dest->shdr->sh_type == SHT_GROUP) | |
dbb410c3 | 721 | { |
4eca0228 | 722 | _bfd_error_handler |
4bba0fb1 AM |
723 | (_("%pB: invalid entry in SHT_GROUP section [%u]"), |
724 | abfd, i); | |
725 | dest->shdr = NULL; | |
dbb410c3 | 726 | } |
dbb410c3 AM |
727 | } |
728 | } | |
729 | } | |
493a3386 NC |
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; | |
4eca0228 | 741 | _bfd_error_handler |
871b3ab2 | 742 | (_("%pB: no valid group sections found"), abfd); |
493a3386 NC |
743 | bfd_set_error (bfd_error_bad_value); |
744 | } | |
745 | } | |
dbb410c3 AM |
746 | } |
747 | } | |
748 | ||
749 | if (num_group != (unsigned) -1) | |
750 | { | |
564e11c9 JW |
751 | unsigned int search_offset = elf_tdata (abfd)->group_search_offset; |
752 | unsigned int j; | |
dbb410c3 | 753 | |
564e11c9 | 754 | for (j = 0; j < num_group; j++) |
dbb410c3 | 755 | { |
564e11c9 JW |
756 | /* Begin search from previous found group. */ |
757 | unsigned i = (j + search_offset) % num_group; | |
758 | ||
dbb410c3 | 759 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; |
ce497010 | 760 | Elf_Internal_Group *idx; |
0c54f692 | 761 | bfd_size_type n_elt; |
ce497010 NC |
762 | |
763 | if (shdr == NULL) | |
764 | continue; | |
765 | ||
766 | idx = (Elf_Internal_Group *) shdr->contents; | |
0c54f692 NC |
767 | if (idx == NULL || shdr->sh_size < 4) |
768 | { | |
769 | /* See PR 21957 for a reproducer. */ | |
770 | /* xgettext:c-format */ | |
871b3ab2 | 771 | _bfd_error_handler (_("%pB: group section '%pA' has no contents"), |
0c54f692 NC |
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 | } | |
ce497010 | 777 | n_elt = shdr->sh_size / 4; |
dbb410c3 AM |
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 | { | |
e0e8c97f | 784 | asection *s = NULL; |
dbb410c3 AM |
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) | |
4bba0fb1 AM |
792 | if ((++idx)->shdr != NULL |
793 | && (s = idx->shdr->bfd_section) != NULL | |
945906ff | 794 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
795 | break; |
796 | if (n_elt != 0) | |
797 | { | |
dbb410c3 AM |
798 | /* Snarf the group name from other member, and |
799 | insert current section in circular list. */ | |
945906ff AM |
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; | |
dbb410c3 AM |
803 | } |
804 | else | |
805 | { | |
dbb410c3 AM |
806 | const char *gname; |
807 | ||
b885599b AM |
808 | gname = group_signature (abfd, shdr); |
809 | if (gname == NULL) | |
b34976b6 | 810 | return FALSE; |
945906ff | 811 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
812 | |
813 | /* Start a circular list with one element. */ | |
945906ff | 814 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 815 | } |
b885599b | 816 | |
9dce4196 AM |
817 | /* If the group section has been created, point to the |
818 | new member. */ | |
dbb410c3 | 819 | if (shdr->bfd_section != NULL) |
945906ff | 820 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 821 | |
564e11c9 JW |
822 | elf_tdata (abfd)->group_search_offset = i; |
823 | j = num_group - 1; | |
dbb410c3 AM |
824 | break; |
825 | } | |
826 | } | |
827 | } | |
828 | ||
945906ff | 829 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 830 | { |
695344c0 | 831 | /* xgettext:c-format */ |
871b3ab2 | 832 | _bfd_error_handler (_("%pB: no group info for section '%pA'"), |
4eca0228 | 833 | abfd, newsect); |
493a3386 | 834 | return FALSE; |
dbb410c3 | 835 | } |
b34976b6 | 836 | return TRUE; |
dbb410c3 AM |
837 | } |
838 | ||
3d7f7666 | 839 | bfd_boolean |
dd863624 | 840 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
841 | { |
842 | unsigned int i; | |
843 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
844 | bfd_boolean result = TRUE; | |
dd863624 L |
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 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
855 | not set the sh_link or sh_info fields. Hence we could | |
856 | get the situation where elfsec is 0. */ | |
857 | if (elfsec == 0) | |
858 | { | |
4fbb74a6 | 859 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
a859124d AM |
860 | bed->link_order_error_handler |
861 | /* xgettext:c-format */ | |
862 | (_("%pB: warning: sh_link not set for section `%pA'"), | |
863 | abfd, s); | |
dd863624 L |
864 | } |
865 | else | |
866 | { | |
91d6fa6a | 867 | asection *linksec = NULL; |
25bbc984 | 868 | |
4fbb74a6 AM |
869 | if (elfsec < elf_numsections (abfd)) |
870 | { | |
871 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 872 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 873 | } |
25bbc984 L |
874 | |
875 | /* PR 1991, 2008: | |
876 | Some strip/objcopy may leave an incorrect value in | |
877 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 878 | if (linksec == NULL) |
25bbc984 | 879 | { |
4eca0228 | 880 | _bfd_error_handler |
695344c0 | 881 | /* xgettext:c-format */ |
871b3ab2 | 882 | (_("%pB: sh_link [%d] in section `%pA' is incorrect"), |
c08bb8dd | 883 | s->owner, elfsec, s); |
25bbc984 L |
884 | result = FALSE; |
885 | } | |
886 | ||
91d6fa6a | 887 | elf_linked_to_section (s) = linksec; |
dd863624 L |
888 | } |
889 | } | |
53720c49 AM |
890 | else if (this_hdr->sh_type == SHT_GROUP |
891 | && elf_next_in_group (s) == NULL) | |
892 | { | |
4eca0228 | 893 | _bfd_error_handler |
695344c0 | 894 | /* xgettext:c-format */ |
871b3ab2 | 895 | (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"), |
53720c49 AM |
896 | abfd, elf_section_data (s)->this_idx); |
897 | result = FALSE; | |
898 | } | |
dd863624 | 899 | } |
3d7f7666 | 900 | |
dd863624 | 901 | /* Process section groups. */ |
3d7f7666 L |
902 | if (num_group == (unsigned) -1) |
903 | return result; | |
904 | ||
905 | for (i = 0; i < num_group; i++) | |
906 | { | |
907 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
4b0e8a5f NC |
908 | Elf_Internal_Group *idx; |
909 | unsigned int n_elt; | |
3d7f7666 | 910 | |
4b0e8a5f NC |
911 | /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */ |
912 | if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) | |
913 | { | |
4eca0228 | 914 | _bfd_error_handler |
695344c0 | 915 | /* xgettext:c-format */ |
871b3ab2 | 916 | (_("%pB: section group entry number %u is corrupt"), |
4b0e8a5f NC |
917 | abfd, i); |
918 | result = FALSE; | |
919 | continue; | |
920 | } | |
921 | ||
922 | idx = (Elf_Internal_Group *) shdr->contents; | |
923 | n_elt = shdr->sh_size / 4; | |
1b786873 | 924 | |
3d7f7666 | 925 | while (--n_elt != 0) |
24d3e51b NC |
926 | { |
927 | ++ idx; | |
928 | ||
929 | if (idx->shdr == NULL) | |
930 | continue; | |
931 | else if (idx->shdr->bfd_section) | |
932 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
db4677b8 AM |
933 | else if (idx->shdr->sh_type != SHT_RELA |
934 | && idx->shdr->sh_type != SHT_REL) | |
24d3e51b NC |
935 | { |
936 | /* There are some unknown sections in the group. */ | |
937 | _bfd_error_handler | |
938 | /* xgettext:c-format */ | |
871b3ab2 | 939 | (_("%pB: unknown type [%#x] section `%s' in group [%pA]"), |
24d3e51b NC |
940 | abfd, |
941 | idx->shdr->sh_type, | |
942 | bfd_elf_string_from_elf_section (abfd, | |
943 | (elf_elfheader (abfd) | |
944 | ->e_shstrndx), | |
945 | idx->shdr->sh_name), | |
946 | shdr->bfd_section); | |
947 | result = FALSE; | |
948 | } | |
949 | } | |
3d7f7666 | 950 | } |
24d3e51b | 951 | |
3d7f7666 L |
952 | return result; |
953 | } | |
954 | ||
72adc230 AM |
955 | bfd_boolean |
956 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
957 | { | |
958 | return elf_next_in_group (sec) != NULL; | |
959 | } | |
960 | ||
cb7f4b29 AM |
961 | const char * |
962 | bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
963 | { | |
964 | if (elf_sec_group (sec) != NULL) | |
965 | return elf_group_name (sec); | |
966 | return NULL; | |
967 | } | |
968 | ||
f6fe1ccd L |
969 | static char * |
970 | convert_debug_to_zdebug (bfd *abfd, const char *name) | |
971 | { | |
972 | unsigned int len = strlen (name); | |
973 | char *new_name = bfd_alloc (abfd, len + 2); | |
974 | if (new_name == NULL) | |
975 | return NULL; | |
976 | new_name[0] = '.'; | |
977 | new_name[1] = 'z'; | |
978 | memcpy (new_name + 2, name + 1, len); | |
979 | return new_name; | |
980 | } | |
981 | ||
982 | static char * | |
983 | convert_zdebug_to_debug (bfd *abfd, const char *name) | |
984 | { | |
985 | unsigned int len = strlen (name); | |
986 | char *new_name = bfd_alloc (abfd, len); | |
987 | if (new_name == NULL) | |
988 | return NULL; | |
989 | new_name[0] = '.'; | |
990 | memcpy (new_name + 1, name + 2, len - 1); | |
991 | return new_name; | |
992 | } | |
993 | ||
cc5277b1 ML |
994 | /* This a copy of lto_section defined in GCC (lto-streamer.h). */ |
995 | ||
996 | struct lto_section | |
997 | { | |
998 | int16_t major_version; | |
999 | int16_t minor_version; | |
1000 | unsigned char slim_object; | |
1001 | ||
1002 | /* Flags is a private field that is not defined publicly. */ | |
1003 | uint16_t flags; | |
1004 | }; | |
1005 | ||
252b5132 RH |
1006 | /* Make a BFD section from an ELF section. We store a pointer to the |
1007 | BFD section in the bfd_section field of the header. */ | |
1008 | ||
b34976b6 | 1009 | bfd_boolean |
217aa764 AM |
1010 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
1011 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
1012 | const char *name, |
1013 | int shindex) | |
252b5132 RH |
1014 | { |
1015 | asection *newsect; | |
1016 | flagword flags; | |
9c5bfbb7 | 1017 | const struct elf_backend_data *bed; |
502794d4 | 1018 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
252b5132 RH |
1019 | |
1020 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 1021 | return TRUE; |
252b5132 RH |
1022 | |
1023 | newsect = bfd_make_section_anyway (abfd, name); | |
1024 | if (newsect == NULL) | |
b34976b6 | 1025 | return FALSE; |
252b5132 | 1026 | |
1829f4b2 AM |
1027 | hdr->bfd_section = newsect; |
1028 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 1029 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 1030 | |
2f89ff8d L |
1031 | /* Always use the real type/flags. */ |
1032 | elf_section_type (newsect) = hdr->sh_type; | |
1033 | elf_section_flags (newsect) = hdr->sh_flags; | |
1034 | ||
252b5132 RH |
1035 | newsect->filepos = hdr->sh_offset; |
1036 | ||
252b5132 RH |
1037 | flags = SEC_NO_FLAGS; |
1038 | if (hdr->sh_type != SHT_NOBITS) | |
1039 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 1040 | if (hdr->sh_type == SHT_GROUP) |
7bdf4127 | 1041 | flags |= SEC_GROUP; |
252b5132 RH |
1042 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
1043 | { | |
1044 | flags |= SEC_ALLOC; | |
1045 | if (hdr->sh_type != SHT_NOBITS) | |
1046 | flags |= SEC_LOAD; | |
1047 | } | |
1048 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
1049 | flags |= SEC_READONLY; | |
1050 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
1051 | flags |= SEC_CODE; | |
1052 | else if ((flags & SEC_LOAD) != 0) | |
1053 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
1054 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
1055 | { | |
1056 | flags |= SEC_MERGE; | |
1057 | newsect->entsize = hdr->sh_entsize; | |
f5fa8ca2 | 1058 | } |
84865015 NC |
1059 | if ((hdr->sh_flags & SHF_STRINGS) != 0) |
1060 | flags |= SEC_STRINGS; | |
dbb410c3 AM |
1061 | if (hdr->sh_flags & SHF_GROUP) |
1062 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 1063 | return FALSE; |
13ae64f3 JJ |
1064 | if ((hdr->sh_flags & SHF_TLS) != 0) |
1065 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
1066 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
1067 | flags |= SEC_EXCLUDE; | |
252b5132 | 1068 | |
df3a023b AM |
1069 | switch (elf_elfheader (abfd)->e_ident[EI_OSABI]) |
1070 | { | |
1071 | /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE, | |
1072 | but binutils as of 2019-07-23 did not set the EI_OSABI header | |
1073 | byte. */ | |
1074 | case ELFOSABI_NONE: | |
1075 | case ELFOSABI_GNU: | |
1076 | case ELFOSABI_FREEBSD: | |
1077 | if ((hdr->sh_flags & SHF_GNU_MBIND) != 0) | |
1078 | elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind; | |
1079 | break; | |
1080 | } | |
1081 | ||
3d2b39cf | 1082 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 1083 | { |
3d2b39cf L |
1084 | /* The debugging sections appear to be recognized only by name, |
1085 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
1086 | if (name [0] == '.') |
1087 | { | |
bb294208 AM |
1088 | if (strncmp (name, ".debug", 6) == 0 |
1089 | || strncmp (name, ".gnu.linkonce.wi.", 17) == 0 | |
1090 | || strncmp (name, ".zdebug", 7) == 0) | |
1091 | flags |= SEC_DEBUGGING | SEC_ELF_OCTETS; | |
1092 | else if (strncmp (name, GNU_BUILD_ATTRS_SECTION_NAME, 21) == 0 | |
1093 | || strncmp (name, ".note.gnu", 9) == 0) | |
502794d4 CE |
1094 | { |
1095 | flags |= SEC_ELF_OCTETS; | |
1096 | opb = 1; | |
1097 | } | |
bb294208 AM |
1098 | else if (strncmp (name, ".line", 5) == 0 |
1099 | || strncmp (name, ".stab", 5) == 0 | |
1100 | || strcmp (name, ".gdb_index") == 0) | |
3d2b39cf L |
1101 | flags |= SEC_DEBUGGING; |
1102 | } | |
1103 | } | |
252b5132 | 1104 | |
502794d4 CE |
1105 | if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb) |
1106 | || !bfd_set_section_size (newsect, hdr->sh_size) | |
1107 | || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign))) | |
1108 | return FALSE; | |
1109 | ||
252b5132 RH |
1110 | /* As a GNU extension, if the name begins with .gnu.linkonce, we |
1111 | only link a single copy of the section. This is used to support | |
1112 | g++. g++ will emit each template expansion in its own section. | |
1113 | The symbols will be defined as weak, so that multiple definitions | |
1114 | are permitted. The GNU linker extension is to actually discard | |
1115 | all but one of the sections. */ | |
0112cd26 | 1116 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 1117 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
1118 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
1119 | ||
8c803a2d AM |
1120 | if (!bfd_set_section_flags (newsect, flags)) |
1121 | return FALSE; | |
1122 | ||
fa152c49 JW |
1123 | bed = get_elf_backend_data (abfd); |
1124 | if (bed->elf_backend_section_flags) | |
8c803a2d | 1125 | if (!bed->elf_backend_section_flags (hdr)) |
b34976b6 | 1126 | return FALSE; |
fa152c49 | 1127 | |
718175fa JK |
1128 | /* We do not parse the PT_NOTE segments as we are interested even in the |
1129 | separate debug info files which may have the segments offsets corrupted. | |
1130 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
1131 | if (hdr->sh_type == SHT_NOTE) | |
1132 | { | |
baea7ef1 | 1133 | bfd_byte *contents; |
718175fa | 1134 | |
baea7ef1 | 1135 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
1136 | return FALSE; |
1137 | ||
276da9b3 L |
1138 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, |
1139 | hdr->sh_offset, hdr->sh_addralign); | |
718175fa JK |
1140 | free (contents); |
1141 | } | |
1142 | ||
8c803a2d | 1143 | if ((newsect->flags & SEC_ALLOC) != 0) |
252b5132 RH |
1144 | { |
1145 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
1146 | unsigned int i, nload; |
1147 | ||
1148 | /* Some ELF linkers produce binaries with all the program header | |
1149 | p_paddr fields zero. If we have such a binary with more than | |
1150 | one PT_LOAD header, then leave the section lma equal to vma | |
1151 | so that we don't create sections with overlapping lma. */ | |
1152 | phdr = elf_tdata (abfd)->phdr; | |
1153 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1154 | if (phdr->p_paddr != 0) | |
1155 | break; | |
1156 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
1157 | ++nload; | |
1158 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1159 | return TRUE; | |
252b5132 | 1160 | |
252b5132 RH |
1161 | phdr = elf_tdata (abfd)->phdr; |
1162 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1163 | { | |
86b2281f AM |
1164 | if (((phdr->p_type == PT_LOAD |
1165 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1166 | || phdr->p_type == PT_TLS) | |
9a83a553 | 1167 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 1168 | { |
8c803a2d | 1169 | if ((newsect->flags & SEC_LOAD) == 0) |
88967714 | 1170 | newsect->lma = (phdr->p_paddr |
502794d4 | 1171 | + hdr->sh_addr - phdr->p_vaddr) / opb; |
88967714 AM |
1172 | else |
1173 | /* We used to use the same adjustment for SEC_LOAD | |
1174 | sections, but that doesn't work if the segment | |
1175 | is packed with code from multiple VMAs. | |
1176 | Instead we calculate the section LMA based on | |
1177 | the segment LMA. It is assumed that the | |
1178 | segment will contain sections with contiguous | |
1179 | LMAs, even if the VMAs are not. */ | |
1180 | newsect->lma = (phdr->p_paddr | |
502794d4 | 1181 | + hdr->sh_offset - phdr->p_offset) / opb; |
88967714 AM |
1182 | |
1183 | /* With contiguous segments, we can't tell from file | |
1184 | offsets whether a section with zero size should | |
1185 | be placed at the end of one segment or the | |
1186 | beginning of the next. Decide based on vaddr. */ | |
1187 | if (hdr->sh_addr >= phdr->p_vaddr | |
1188 | && (hdr->sh_addr + hdr->sh_size | |
1189 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1190 | break; | |
252b5132 RH |
1191 | } |
1192 | } | |
1193 | } | |
1194 | ||
4a114e3e L |
1195 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1196 | .zdebug_*, after the section flags is set. */ | |
8c803a2d | 1197 | if ((newsect->flags & SEC_DEBUGGING) |
4a114e3e L |
1198 | && ((name[1] == 'd' && name[6] == '_') |
1199 | || (name[1] == 'z' && name[7] == '_'))) | |
1200 | { | |
1201 | enum { nothing, compress, decompress } action = nothing; | |
151411f8 | 1202 | int compression_header_size; |
dab394de | 1203 | bfd_size_type uncompressed_size; |
4207142d | 1204 | unsigned int uncompressed_align_power; |
151411f8 L |
1205 | bfd_boolean compressed |
1206 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
dab394de | 1207 | &compression_header_size, |
4207142d MW |
1208 | &uncompressed_size, |
1209 | &uncompressed_align_power); | |
151411f8 | 1210 | if (compressed) |
4a114e3e L |
1211 | { |
1212 | /* Compressed section. Check if we should decompress. */ | |
1213 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1214 | action = decompress; | |
1215 | } | |
151411f8 L |
1216 | |
1217 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1218 | section. Check if we should compress. */ | |
1219 | if (action == nothing) | |
4a114e3e | 1220 | { |
151411f8 L |
1221 | if (newsect->size != 0 |
1222 | && (abfd->flags & BFD_COMPRESS) | |
1223 | && compression_header_size >= 0 | |
dab394de | 1224 | && uncompressed_size > 0 |
151411f8 L |
1225 | && (!compressed |
1226 | || ((compression_header_size > 0) | |
1227 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
4a114e3e | 1228 | action = compress; |
151411f8 L |
1229 | else |
1230 | return TRUE; | |
4a114e3e L |
1231 | } |
1232 | ||
151411f8 | 1233 | if (action == compress) |
4a114e3e | 1234 | { |
4a114e3e L |
1235 | if (!bfd_init_section_compress_status (abfd, newsect)) |
1236 | { | |
4eca0228 | 1237 | _bfd_error_handler |
695344c0 | 1238 | /* xgettext:c-format */ |
871b3ab2 | 1239 | (_("%pB: unable to initialize compress status for section %s"), |
4a114e3e L |
1240 | abfd, name); |
1241 | return FALSE; | |
1242 | } | |
151411f8 L |
1243 | } |
1244 | else | |
1245 | { | |
4a114e3e L |
1246 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
1247 | { | |
4eca0228 | 1248 | _bfd_error_handler |
695344c0 | 1249 | /* xgettext:c-format */ |
871b3ab2 | 1250 | (_("%pB: unable to initialize decompress status for section %s"), |
4a114e3e L |
1251 | abfd, name); |
1252 | return FALSE; | |
1253 | } | |
151411f8 L |
1254 | } |
1255 | ||
f6fe1ccd | 1256 | if (abfd->is_linker_input) |
151411f8 | 1257 | { |
f6fe1ccd L |
1258 | if (name[1] == 'z' |
1259 | && (action == decompress | |
1260 | || (action == compress | |
1261 | && (abfd->flags & BFD_COMPRESS_GABI) != 0))) | |
4e011fb5 | 1262 | { |
f6fe1ccd L |
1263 | /* Convert section name from .zdebug_* to .debug_* so |
1264 | that linker will consider this section as a debug | |
1265 | section. */ | |
1266 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
151411f8 L |
1267 | if (new_name == NULL) |
1268 | return FALSE; | |
fd361982 | 1269 | bfd_rename_section (newsect, new_name); |
151411f8 | 1270 | } |
4a114e3e | 1271 | } |
f6fe1ccd L |
1272 | else |
1273 | /* For objdump, don't rename the section. For objcopy, delay | |
1274 | section rename to elf_fake_sections. */ | |
1275 | newsect->flags |= SEC_ELF_RENAME; | |
4a114e3e L |
1276 | } |
1277 | ||
cc5277b1 ML |
1278 | /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information |
1279 | section. */ | |
1280 | const char *lto_section_name = ".gnu.lto_.lto."; | |
1281 | if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0) | |
1282 | { | |
1283 | struct lto_section lsection; | |
1284 | if (bfd_get_section_contents (abfd, newsect, &lsection, 0, | |
1285 | sizeof (struct lto_section))) | |
1286 | abfd->lto_slim_object = lsection.slim_object; | |
1287 | } | |
1288 | ||
b34976b6 | 1289 | return TRUE; |
252b5132 RH |
1290 | } |
1291 | ||
84865015 NC |
1292 | const char *const bfd_elf_section_type_names[] = |
1293 | { | |
252b5132 RH |
1294 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", |
1295 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1296 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1297 | }; | |
1298 | ||
1049f94e | 1299 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1300 | output, and the reloc is against an external symbol, and nothing |
1301 | has given us any additional addend, the resulting reloc will also | |
1302 | be against the same symbol. In such a case, we don't want to | |
1303 | change anything about the way the reloc is handled, since it will | |
1304 | all be done at final link time. Rather than put special case code | |
1305 | into bfd_perform_relocation, all the reloc types use this howto | |
1306 | function. It just short circuits the reloc if producing | |
1049f94e | 1307 | relocatable output against an external symbol. */ |
252b5132 | 1308 | |
252b5132 | 1309 | bfd_reloc_status_type |
217aa764 AM |
1310 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1311 | arelent *reloc_entry, | |
1312 | asymbol *symbol, | |
1313 | void *data ATTRIBUTE_UNUSED, | |
1314 | asection *input_section, | |
1315 | bfd *output_bfd, | |
1316 | char **error_message ATTRIBUTE_UNUSED) | |
1317 | { | |
1318 | if (output_bfd != NULL | |
252b5132 RH |
1319 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1320 | && (! reloc_entry->howto->partial_inplace | |
1321 | || reloc_entry->addend == 0)) | |
1322 | { | |
1323 | reloc_entry->address += input_section->output_offset; | |
1324 | return bfd_reloc_ok; | |
1325 | } | |
1326 | ||
1327 | return bfd_reloc_continue; | |
1328 | } | |
1329 | \f | |
84865015 NC |
1330 | /* Returns TRUE if section A matches section B. |
1331 | Names, addresses and links may be different, but everything else | |
1332 | should be the same. */ | |
1333 | ||
1334 | static bfd_boolean | |
5522f910 NC |
1335 | section_match (const Elf_Internal_Shdr * a, |
1336 | const Elf_Internal_Shdr * b) | |
84865015 | 1337 | { |
ac85e67c AM |
1338 | if (a->sh_type != b->sh_type |
1339 | || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0 | |
1340 | || a->sh_addralign != b->sh_addralign | |
1341 | || a->sh_entsize != b->sh_entsize) | |
1342 | return FALSE; | |
1343 | if (a->sh_type == SHT_SYMTAB | |
1344 | || a->sh_type == SHT_STRTAB) | |
1345 | return TRUE; | |
1346 | return a->sh_size == b->sh_size; | |
84865015 NC |
1347 | } |
1348 | ||
1349 | /* Find a section in OBFD that has the same characteristics | |
1350 | as IHEADER. Return the index of this section or SHN_UNDEF if | |
1351 | none can be found. Check's section HINT first, as this is likely | |
1352 | to be the correct section. */ | |
1353 | ||
1354 | static unsigned int | |
5cc4ca83 ST |
1355 | find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader, |
1356 | const unsigned int hint) | |
84865015 NC |
1357 | { |
1358 | Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); | |
1359 | unsigned int i; | |
1360 | ||
a55c9876 NC |
1361 | BFD_ASSERT (iheader != NULL); |
1362 | ||
1363 | /* See PR 20922 for a reproducer of the NULL test. */ | |
5cc4ca83 ST |
1364 | if (hint < elf_numsections (obfd) |
1365 | && oheaders[hint] != NULL | |
a55c9876 | 1366 | && section_match (oheaders[hint], iheader)) |
84865015 NC |
1367 | return hint; |
1368 | ||
1369 | for (i = 1; i < elf_numsections (obfd); i++) | |
1370 | { | |
1371 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
1372 | ||
a55c9876 NC |
1373 | if (oheader == NULL) |
1374 | continue; | |
84865015 NC |
1375 | if (section_match (oheader, iheader)) |
1376 | /* FIXME: Do we care if there is a potential for | |
1377 | multiple matches ? */ | |
1378 | return i; | |
1379 | } | |
1380 | ||
1381 | return SHN_UNDEF; | |
1382 | } | |
1383 | ||
5522f910 NC |
1384 | /* PR 19938: Attempt to set the ELF section header fields of an OS or |
1385 | Processor specific section, based upon a matching input section. | |
1386 | Returns TRUE upon success, FALSE otherwise. */ | |
07d6d2b8 | 1387 | |
5522f910 NC |
1388 | static bfd_boolean |
1389 | copy_special_section_fields (const bfd *ibfd, | |
1390 | bfd *obfd, | |
1391 | const Elf_Internal_Shdr *iheader, | |
1392 | Elf_Internal_Shdr *oheader, | |
1393 | const unsigned int secnum) | |
1394 | { | |
1395 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
1396 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); | |
1397 | bfd_boolean changed = FALSE; | |
1398 | unsigned int sh_link; | |
1399 | ||
1400 | if (oheader->sh_type == SHT_NOBITS) | |
1401 | { | |
1402 | /* This is a feature for objcopy --only-keep-debug: | |
1403 | When a section's type is changed to NOBITS, we preserve | |
1404 | the sh_link and sh_info fields so that they can be | |
1405 | matched up with the original. | |
1406 | ||
1407 | Note: Strictly speaking these assignments are wrong. | |
1408 | The sh_link and sh_info fields should point to the | |
1409 | relevent sections in the output BFD, which may not be in | |
1410 | the same location as they were in the input BFD. But | |
1411 | the whole point of this action is to preserve the | |
1412 | original values of the sh_link and sh_info fields, so | |
1413 | that they can be matched up with the section headers in | |
1414 | the original file. So strictly speaking we may be | |
1415 | creating an invalid ELF file, but it is only for a file | |
1416 | that just contains debug info and only for sections | |
1417 | without any contents. */ | |
1418 | if (oheader->sh_link == 0) | |
1419 | oheader->sh_link = iheader->sh_link; | |
1420 | if (oheader->sh_info == 0) | |
1421 | oheader->sh_info = iheader->sh_info; | |
1422 | return TRUE; | |
1423 | } | |
1424 | ||
1425 | /* Allow the target a chance to decide how these fields should be set. */ | |
a859124d AM |
1426 | if (bed->elf_backend_copy_special_section_fields (ibfd, obfd, |
1427 | iheader, oheader)) | |
5522f910 NC |
1428 | return TRUE; |
1429 | ||
1430 | /* We have an iheader which might match oheader, and which has non-zero | |
1431 | sh_info and/or sh_link fields. Attempt to follow those links and find | |
1432 | the section in the output bfd which corresponds to the linked section | |
1433 | in the input bfd. */ | |
1434 | if (iheader->sh_link != SHN_UNDEF) | |
1435 | { | |
4f3ca05b NC |
1436 | /* See PR 20931 for a reproducer. */ |
1437 | if (iheader->sh_link >= elf_numsections (ibfd)) | |
1438 | { | |
76cfced5 | 1439 | _bfd_error_handler |
4f3ca05b | 1440 | /* xgettext:c-format */ |
9793eb77 | 1441 | (_("%pB: invalid sh_link field (%d) in section number %d"), |
4f3ca05b NC |
1442 | ibfd, iheader->sh_link, secnum); |
1443 | return FALSE; | |
1444 | } | |
1445 | ||
5522f910 NC |
1446 | sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); |
1447 | if (sh_link != SHN_UNDEF) | |
1448 | { | |
1449 | oheader->sh_link = sh_link; | |
1450 | changed = TRUE; | |
1451 | } | |
1452 | else | |
1453 | /* FIXME: Should we install iheader->sh_link | |
1454 | if we could not find a match ? */ | |
76cfced5 | 1455 | _bfd_error_handler |
695344c0 | 1456 | /* xgettext:c-format */ |
9793eb77 | 1457 | (_("%pB: failed to find link section for section %d"), obfd, secnum); |
5522f910 NC |
1458 | } |
1459 | ||
1460 | if (iheader->sh_info) | |
1461 | { | |
1462 | /* The sh_info field can hold arbitrary information, but if the | |
1463 | SHF_LINK_INFO flag is set then it should be interpreted as a | |
1464 | section index. */ | |
1465 | if (iheader->sh_flags & SHF_INFO_LINK) | |
1466 | { | |
1467 | sh_link = find_link (obfd, iheaders[iheader->sh_info], | |
1468 | iheader->sh_info); | |
1469 | if (sh_link != SHN_UNDEF) | |
1470 | oheader->sh_flags |= SHF_INFO_LINK; | |
1471 | } | |
1472 | else | |
1473 | /* No idea what it means - just copy it. */ | |
1474 | sh_link = iheader->sh_info; | |
1475 | ||
1476 | if (sh_link != SHN_UNDEF) | |
1477 | { | |
1478 | oheader->sh_info = sh_link; | |
1479 | changed = TRUE; | |
1480 | } | |
1481 | else | |
76cfced5 | 1482 | _bfd_error_handler |
695344c0 | 1483 | /* xgettext:c-format */ |
9793eb77 | 1484 | (_("%pB: failed to find info section for section %d"), obfd, secnum); |
5522f910 NC |
1485 | } |
1486 | ||
1487 | return changed; | |
1488 | } | |
07d6d2b8 | 1489 | |
0ac4564e L |
1490 | /* Copy the program header and other data from one object module to |
1491 | another. */ | |
252b5132 | 1492 | |
b34976b6 | 1493 | bfd_boolean |
217aa764 | 1494 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 | 1495 | { |
5522f910 NC |
1496 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); |
1497 | Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); | |
1498 | const struct elf_backend_data *bed; | |
84865015 NC |
1499 | unsigned int i; |
1500 | ||
2d502050 | 1501 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
84865015 | 1502 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
b34976b6 | 1503 | return TRUE; |
2d502050 | 1504 | |
57b828ef L |
1505 | if (!elf_flags_init (obfd)) |
1506 | { | |
1507 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1508 | elf_flags_init (obfd) = TRUE; | |
1509 | } | |
2d502050 | 1510 | |
0ac4564e | 1511 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1512 | |
1513 | /* Also copy the EI_OSABI field. */ | |
1514 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1515 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 | 1516 | |
5522f910 NC |
1517 | /* If set, copy the EI_ABIVERSION field. */ |
1518 | if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) | |
1519 | elf_elfheader (obfd)->e_ident[EI_ABIVERSION] | |
1520 | = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; | |
07d6d2b8 | 1521 | |
104d59d1 JM |
1522 | /* Copy object attributes. */ |
1523 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
63b9bbb7 | 1524 | |
84865015 NC |
1525 | if (iheaders == NULL || oheaders == NULL) |
1526 | return TRUE; | |
63b9bbb7 | 1527 | |
5522f910 NC |
1528 | bed = get_elf_backend_data (obfd); |
1529 | ||
1530 | /* Possibly copy other fields in the section header. */ | |
84865015 | 1531 | for (i = 1; i < elf_numsections (obfd); i++) |
63b9bbb7 | 1532 | { |
84865015 NC |
1533 | unsigned int j; |
1534 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
63b9bbb7 | 1535 | |
5522f910 NC |
1536 | /* Ignore ordinary sections. SHT_NOBITS sections are considered however |
1537 | because of a special case need for generating separate debug info | |
1538 | files. See below for more details. */ | |
84865015 NC |
1539 | if (oheader == NULL |
1540 | || (oheader->sh_type != SHT_NOBITS | |
5522f910 NC |
1541 | && oheader->sh_type < SHT_LOOS)) |
1542 | continue; | |
1543 | ||
1544 | /* Ignore empty sections, and sections whose | |
1545 | fields have already been initialised. */ | |
1546 | if (oheader->sh_size == 0 | |
84865015 NC |
1547 | || (oheader->sh_info != 0 && oheader->sh_link != 0)) |
1548 | continue; | |
63b9bbb7 | 1549 | |
84865015 | 1550 | /* Scan for the matching section in the input bfd. |
5522f910 NC |
1551 | First we try for a direct mapping between the input and output sections. */ |
1552 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1553 | { | |
1554 | const Elf_Internal_Shdr * iheader = iheaders[j]; | |
1555 | ||
1556 | if (iheader == NULL) | |
1557 | continue; | |
1558 | ||
1559 | if (oheader->bfd_section != NULL | |
1560 | && iheader->bfd_section != NULL | |
1561 | && iheader->bfd_section->output_section != NULL | |
1562 | && iheader->bfd_section->output_section == oheader->bfd_section) | |
1563 | { | |
1564 | /* We have found a connection from the input section to the | |
1565 | output section. Attempt to copy the header fields. If | |
1566 | this fails then do not try any further sections - there | |
1567 | should only be a one-to-one mapping between input and output. */ | |
1568 | if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) | |
1569 | j = elf_numsections (ibfd); | |
1570 | break; | |
1571 | } | |
1572 | } | |
1573 | ||
1574 | if (j < elf_numsections (ibfd)) | |
1575 | continue; | |
1576 | ||
1577 | /* That failed. So try to deduce the corresponding input section. | |
84865015 NC |
1578 | Unfortunately we cannot compare names as the output string table |
1579 | is empty, so instead we check size, address and type. */ | |
1580 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1581 | { | |
5522f910 | 1582 | const Elf_Internal_Shdr * iheader = iheaders[j]; |
84865015 | 1583 | |
5522f910 NC |
1584 | if (iheader == NULL) |
1585 | continue; | |
1586 | ||
1587 | /* Try matching fields in the input section's header. | |
1588 | Since --only-keep-debug turns all non-debug sections into | |
84865015 NC |
1589 | SHT_NOBITS sections, the output SHT_NOBITS type matches any |
1590 | input type. */ | |
1591 | if ((oheader->sh_type == SHT_NOBITS | |
1592 | || iheader->sh_type == oheader->sh_type) | |
5522f910 NC |
1593 | && (iheader->sh_flags & ~ SHF_INFO_LINK) |
1594 | == (oheader->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1595 | && iheader->sh_addralign == oheader->sh_addralign |
1596 | && iheader->sh_entsize == oheader->sh_entsize | |
1597 | && iheader->sh_size == oheader->sh_size | |
1598 | && iheader->sh_addr == oheader->sh_addr | |
1599 | && (iheader->sh_info != oheader->sh_info | |
1600 | || iheader->sh_link != oheader->sh_link)) | |
63b9bbb7 | 1601 | { |
5522f910 NC |
1602 | if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) |
1603 | break; | |
63b9bbb7 NC |
1604 | } |
1605 | } | |
5522f910 NC |
1606 | |
1607 | if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) | |
1608 | { | |
1609 | /* Final attempt. Call the backend copy function | |
1610 | with a NULL input section. */ | |
a859124d AM |
1611 | (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd, |
1612 | NULL, oheader); | |
5522f910 | 1613 | } |
63b9bbb7 NC |
1614 | } |
1615 | ||
b34976b6 | 1616 | return TRUE; |
2d502050 L |
1617 | } |
1618 | ||
cedc298e L |
1619 | static const char * |
1620 | get_segment_type (unsigned int p_type) | |
1621 | { | |
1622 | const char *pt; | |
1623 | switch (p_type) | |
1624 | { | |
1625 | case PT_NULL: pt = "NULL"; break; | |
1626 | case PT_LOAD: pt = "LOAD"; break; | |
1627 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1628 | case PT_INTERP: pt = "INTERP"; break; | |
1629 | case PT_NOTE: pt = "NOTE"; break; | |
1630 | case PT_SHLIB: pt = "SHLIB"; break; | |
1631 | case PT_PHDR: pt = "PHDR"; break; | |
1632 | case PT_TLS: pt = "TLS"; break; | |
1633 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1634 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1635 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1636 | default: pt = NULL; break; | |
1637 | } | |
1638 | return pt; | |
1639 | } | |
1640 | ||
f0b79d91 L |
1641 | /* Print out the program headers. */ |
1642 | ||
b34976b6 | 1643 | bfd_boolean |
217aa764 | 1644 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1645 | { |
a50b1753 | 1646 | FILE *f = (FILE *) farg; |
252b5132 RH |
1647 | Elf_Internal_Phdr *p; |
1648 | asection *s; | |
1649 | bfd_byte *dynbuf = NULL; | |
1650 | ||
1651 | p = elf_tdata (abfd)->phdr; | |
1652 | if (p != NULL) | |
1653 | { | |
1654 | unsigned int i, c; | |
1655 | ||
1656 | fprintf (f, _("\nProgram Header:\n")); | |
1657 | c = elf_elfheader (abfd)->e_phnum; | |
1658 | for (i = 0; i < c; i++, p++) | |
1659 | { | |
cedc298e | 1660 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1661 | char buf[20]; |
1662 | ||
cedc298e | 1663 | if (pt == NULL) |
252b5132 | 1664 | { |
cedc298e L |
1665 | sprintf (buf, "0x%lx", p->p_type); |
1666 | pt = buf; | |
252b5132 | 1667 | } |
dc810e39 | 1668 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1669 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1670 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1671 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1672 | fprintf (f, " paddr 0x"); |
60b89a18 | 1673 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1674 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1675 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1676 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1677 | fprintf (f, " memsz 0x"); |
60b89a18 | 1678 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1679 | fprintf (f, " flags %c%c%c", |
1680 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1681 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1682 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1683 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1684 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1685 | fprintf (f, "\n"); |
1686 | } | |
1687 | } | |
1688 | ||
1689 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1690 | if (s != NULL) | |
1691 | { | |
cb33740c | 1692 | unsigned int elfsec; |
dc810e39 | 1693 | unsigned long shlink; |
252b5132 RH |
1694 | bfd_byte *extdyn, *extdynend; |
1695 | size_t extdynsize; | |
217aa764 | 1696 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1697 | |
1698 | fprintf (f, _("\nDynamic Section:\n")); | |
1699 | ||
eea6121a | 1700 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1701 | goto error_return; |
1702 | ||
1703 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1704 | if (elfsec == SHN_BAD) |
252b5132 | 1705 | goto error_return; |
dc810e39 | 1706 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1707 | |
1708 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1709 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1710 | ||
1711 | extdyn = dynbuf; | |
06614111 NC |
1712 | /* PR 17512: file: 6f427532. */ |
1713 | if (s->size < extdynsize) | |
1714 | goto error_return; | |
eea6121a | 1715 | extdynend = extdyn + s->size; |
1036838a | 1716 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
07d6d2b8 | 1717 | Fix range check. */ |
1036838a | 1718 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) |
252b5132 RH |
1719 | { |
1720 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1721 | const char *name = ""; |
252b5132 | 1722 | char ab[20]; |
b34976b6 | 1723 | bfd_boolean stringp; |
ad9563d6 | 1724 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1725 | |
217aa764 | 1726 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1727 | |
1728 | if (dyn.d_tag == DT_NULL) | |
1729 | break; | |
1730 | ||
b34976b6 | 1731 | stringp = FALSE; |
252b5132 RH |
1732 | switch (dyn.d_tag) |
1733 | { | |
1734 | default: | |
ad9563d6 CM |
1735 | if (bed->elf_backend_get_target_dtag) |
1736 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1737 | ||
1738 | if (!strcmp (name, "")) | |
1739 | { | |
cd9af601 | 1740 | sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag); |
ad9563d6 CM |
1741 | name = ab; |
1742 | } | |
252b5132 RH |
1743 | break; |
1744 | ||
b34976b6 | 1745 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1746 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1747 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1748 | case DT_HASH: name = "HASH"; break; | |
1749 | case DT_STRTAB: name = "STRTAB"; break; | |
1750 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1751 | case DT_RELA: name = "RELA"; break; | |
1752 | case DT_RELASZ: name = "RELASZ"; break; | |
1753 | case DT_RELAENT: name = "RELAENT"; break; | |
1754 | case DT_STRSZ: name = "STRSZ"; break; | |
1755 | case DT_SYMENT: name = "SYMENT"; break; | |
1756 | case DT_INIT: name = "INIT"; break; | |
1757 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1758 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1759 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1760 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1761 | case DT_REL: name = "REL"; break; | |
1762 | case DT_RELSZ: name = "RELSZ"; break; | |
1763 | case DT_RELENT: name = "RELENT"; break; | |
1764 | case DT_PLTREL: name = "PLTREL"; break; | |
1765 | case DT_DEBUG: name = "DEBUG"; break; | |
1766 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1767 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1768 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1769 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1770 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1771 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1772 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1773 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1774 | case DT_FLAGS: name = "FLAGS"; break; |
1775 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1776 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1777 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1778 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1779 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1780 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1781 | case DT_FEATURE: name = "FEATURE"; break; | |
1782 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1783 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1784 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1785 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1786 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1787 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1788 | case DT_PLTPAD: name = "PLTPAD"; break; |
1789 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1790 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1791 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1792 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1793 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1794 | case DT_VERSYM: name = "VERSYM"; break; |
1795 | case DT_VERDEF: name = "VERDEF"; break; | |
1796 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1797 | case DT_VERNEED: name = "VERNEED"; break; | |
1798 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1799 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1800 | case DT_USED: name = "USED"; break; |
b34976b6 | 1801 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1802 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1803 | } |
1804 | ||
ad9563d6 | 1805 | fprintf (f, " %-20s ", name); |
252b5132 | 1806 | if (! stringp) |
a1f3c56e AN |
1807 | { |
1808 | fprintf (f, "0x"); | |
1809 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1810 | } | |
252b5132 RH |
1811 | else |
1812 | { | |
1813 | const char *string; | |
dc810e39 | 1814 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1815 | |
dc810e39 | 1816 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1817 | if (string == NULL) |
1818 | goto error_return; | |
1819 | fprintf (f, "%s", string); | |
1820 | } | |
1821 | fprintf (f, "\n"); | |
1822 | } | |
1823 | ||
1824 | free (dynbuf); | |
1825 | dynbuf = NULL; | |
1826 | } | |
1827 | ||
1828 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1829 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1830 | { | |
fc0e6df6 | 1831 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1832 | return FALSE; |
252b5132 RH |
1833 | } |
1834 | ||
1835 | if (elf_dynverdef (abfd) != 0) | |
1836 | { | |
1837 | Elf_Internal_Verdef *t; | |
1838 | ||
1839 | fprintf (f, _("\nVersion definitions:\n")); | |
1840 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1841 | { | |
1842 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1843 | t->vd_flags, t->vd_hash, |
1844 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1845 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1846 | { |
1847 | Elf_Internal_Verdaux *a; | |
1848 | ||
1849 | fprintf (f, "\t"); | |
1850 | for (a = t->vd_auxptr->vda_nextptr; | |
1851 | a != NULL; | |
1852 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1853 | fprintf (f, "%s ", |
1854 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1855 | fprintf (f, "\n"); |
1856 | } | |
1857 | } | |
1858 | } | |
1859 | ||
1860 | if (elf_dynverref (abfd) != 0) | |
1861 | { | |
1862 | Elf_Internal_Verneed *t; | |
1863 | ||
1864 | fprintf (f, _("\nVersion References:\n")); | |
1865 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1866 | { | |
1867 | Elf_Internal_Vernaux *a; | |
1868 | ||
d0fb9a8d JJ |
1869 | fprintf (f, _(" required from %s:\n"), |
1870 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1871 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1872 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1873 | a->vna_flags, a->vna_other, |
1874 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1875 | } |
1876 | } | |
1877 | ||
b34976b6 | 1878 | return TRUE; |
252b5132 RH |
1879 | |
1880 | error_return: | |
1881 | if (dynbuf != NULL) | |
1882 | free (dynbuf); | |
b34976b6 | 1883 | return FALSE; |
252b5132 RH |
1884 | } |
1885 | ||
bb4d2ac2 L |
1886 | /* Get version string. */ |
1887 | ||
1888 | const char * | |
60bb06bc L |
1889 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1890 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1891 | { |
1892 | const char *version_string = NULL; | |
1893 | if (elf_dynversym (abfd) != 0 | |
1894 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1895 | { | |
1896 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1897 | ||
1898 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1899 | vernum &= VERSYM_VERSION; | |
1900 | ||
1901 | if (vernum == 0) | |
1902 | version_string = ""; | |
1f6f5dba L |
1903 | else if (vernum == 1 |
1904 | && (vernum > elf_tdata (abfd)->cverdefs | |
1905 | || (elf_tdata (abfd)->verdef[0].vd_flags | |
1906 | == VER_FLG_BASE))) | |
bb4d2ac2 L |
1907 | version_string = "Base"; |
1908 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1909 | version_string = | |
1910 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1911 | else | |
1912 | { | |
1913 | Elf_Internal_Verneed *t; | |
1914 | ||
7a815dd5 | 1915 | version_string = _("<corrupt>"); |
bb4d2ac2 L |
1916 | for (t = elf_tdata (abfd)->verref; |
1917 | t != NULL; | |
1918 | t = t->vn_nextref) | |
1919 | { | |
1920 | Elf_Internal_Vernaux *a; | |
1921 | ||
1922 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1923 | { | |
1924 | if (a->vna_other == vernum) | |
1925 | { | |
1926 | version_string = a->vna_nodename; | |
1927 | break; | |
1928 | } | |
1929 | } | |
1930 | } | |
1931 | } | |
1932 | } | |
1933 | return version_string; | |
1934 | } | |
1935 | ||
252b5132 RH |
1936 | /* Display ELF-specific fields of a symbol. */ |
1937 | ||
1938 | void | |
217aa764 AM |
1939 | bfd_elf_print_symbol (bfd *abfd, |
1940 | void *filep, | |
1941 | asymbol *symbol, | |
1942 | bfd_print_symbol_type how) | |
252b5132 | 1943 | { |
a50b1753 | 1944 | FILE *file = (FILE *) filep; |
252b5132 RH |
1945 | switch (how) |
1946 | { | |
1947 | case bfd_print_symbol_name: | |
1948 | fprintf (file, "%s", symbol->name); | |
1949 | break; | |
1950 | case bfd_print_symbol_more: | |
1951 | fprintf (file, "elf "); | |
60b89a18 | 1952 | bfd_fprintf_vma (abfd, file, symbol->value); |
cd9af601 | 1953 | fprintf (file, " %x", symbol->flags); |
252b5132 RH |
1954 | break; |
1955 | case bfd_print_symbol_all: | |
1956 | { | |
4e8a9624 AM |
1957 | const char *section_name; |
1958 | const char *name = NULL; | |
9c5bfbb7 | 1959 | const struct elf_backend_data *bed; |
7a13edea | 1960 | unsigned char st_other; |
dbb410c3 | 1961 | bfd_vma val; |
bb4d2ac2 L |
1962 | const char *version_string; |
1963 | bfd_boolean hidden; | |
c044fabd | 1964 | |
252b5132 | 1965 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1966 | |
1967 | bed = get_elf_backend_data (abfd); | |
1968 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1969 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1970 | |
1971 | if (name == NULL) | |
1972 | { | |
7ee38065 | 1973 | name = symbol->name; |
217aa764 | 1974 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1975 | } |
1976 | ||
252b5132 RH |
1977 | fprintf (file, " %s\t", section_name); |
1978 | /* Print the "other" value for a symbol. For common symbols, | |
1979 | we've already printed the size; now print the alignment. | |
1980 | For other symbols, we have no specified alignment, and | |
1981 | we've printed the address; now print the size. */ | |
dcf6c779 | 1982 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1983 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1984 | else | |
1985 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1986 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1987 | |
1988 | /* If we have version information, print it. */ | |
60bb06bc L |
1989 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1990 | symbol, | |
1991 | &hidden); | |
bb4d2ac2 | 1992 | if (version_string) |
252b5132 | 1993 | { |
bb4d2ac2 | 1994 | if (!hidden) |
252b5132 RH |
1995 | fprintf (file, " %-11s", version_string); |
1996 | else | |
1997 | { | |
1998 | int i; | |
1999 | ||
2000 | fprintf (file, " (%s)", version_string); | |
2001 | for (i = 10 - strlen (version_string); i > 0; --i) | |
2002 | putc (' ', file); | |
2003 | } | |
2004 | } | |
2005 | ||
2006 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 2007 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 2008 | |
7a13edea NC |
2009 | switch (st_other) |
2010 | { | |
2011 | case 0: break; | |
2012 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
2013 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
2014 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
2015 | default: | |
2016 | /* Some other non-defined flags are also present, so print | |
2017 | everything hex. */ | |
2018 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
2019 | } | |
252b5132 | 2020 | |
587ff49e | 2021 | fprintf (file, " %s", name); |
252b5132 RH |
2022 | } |
2023 | break; | |
2024 | } | |
2025 | } | |
252b5132 RH |
2026 | \f |
2027 | /* ELF .o/exec file reading */ | |
2028 | ||
c044fabd | 2029 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 2030 | |
b34976b6 | 2031 | bfd_boolean |
217aa764 | 2032 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 2033 | { |
4fbb74a6 AM |
2034 | Elf_Internal_Shdr *hdr; |
2035 | Elf_Internal_Ehdr *ehdr; | |
2036 | const struct elf_backend_data *bed; | |
90937f86 | 2037 | const char *name; |
bf67003b NC |
2038 | bfd_boolean ret = TRUE; |
2039 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 2040 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 2041 | static unsigned int nesting = 0; |
252b5132 | 2042 | |
4fbb74a6 AM |
2043 | if (shindex >= elf_numsections (abfd)) |
2044 | return FALSE; | |
2045 | ||
bf67003b NC |
2046 | if (++ nesting > 3) |
2047 | { | |
2048 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
67ce483b | 2049 | sections, with each the string indices pointing to the next in the |
bf67003b NC |
2050 | loop. Detect this here, by refusing to load a section that we are |
2051 | already in the process of loading. We only trigger this test if | |
2052 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
2053 | can expect to recurse at least once. |
2054 | ||
2055 | FIXME: It would be better if this array was attached to the bfd, | |
2056 | rather than being held in a static pointer. */ | |
2057 | ||
2058 | if (sections_being_created_abfd != abfd) | |
2059 | sections_being_created = NULL; | |
bf67003b NC |
2060 | if (sections_being_created == NULL) |
2061 | { | |
446f7ed5 AM |
2062 | size_t amt = elf_numsections (abfd) * sizeof (bfd_boolean); |
2063 | sections_being_created = (bfd_boolean *) bfd_zalloc (abfd, amt); | |
96d3b80f AM |
2064 | if (sections_being_created == NULL) |
2065 | return FALSE; | |
5a4b0ccc | 2066 | sections_being_created_abfd = abfd; |
bf67003b NC |
2067 | } |
2068 | if (sections_being_created [shindex]) | |
2069 | { | |
4eca0228 | 2070 | _bfd_error_handler |
871b3ab2 | 2071 | (_("%pB: warning: loop in section dependencies detected"), abfd); |
bf67003b NC |
2072 | return FALSE; |
2073 | } | |
2074 | sections_being_created [shindex] = TRUE; | |
2075 | } | |
2076 | ||
4fbb74a6 AM |
2077 | hdr = elf_elfsections (abfd)[shindex]; |
2078 | ehdr = elf_elfheader (abfd); | |
2079 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 2080 | hdr->sh_name); |
933d961a | 2081 | if (name == NULL) |
bf67003b | 2082 | goto fail; |
252b5132 | 2083 | |
4fbb74a6 | 2084 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
2085 | switch (hdr->sh_type) |
2086 | { | |
2087 | case SHT_NULL: | |
2088 | /* Inactive section. Throw it away. */ | |
bf67003b | 2089 | goto success; |
252b5132 | 2090 | |
bf67003b NC |
2091 | case SHT_PROGBITS: /* Normal section with contents. */ |
2092 | case SHT_NOBITS: /* .bss section. */ | |
2093 | case SHT_HASH: /* .hash section. */ | |
2094 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
2095 | case SHT_INIT_ARRAY: /* .init_array section. */ |
2096 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
2097 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 2098 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 2099 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
2100 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2101 | goto success; | |
252b5132 | 2102 | |
797fc050 | 2103 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 2104 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2105 | goto fail; |
2106 | ||
cfcac11d NC |
2107 | if (hdr->sh_link > elf_numsections (abfd)) |
2108 | { | |
caa83f8b | 2109 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
2110 | field set to SHN_BEFORE or SHN_AFTER. */ |
2111 | switch (bfd_get_arch (abfd)) | |
2112 | { | |
caa83f8b | 2113 | case bfd_arch_i386: |
cfcac11d NC |
2114 | case bfd_arch_sparc: |
2115 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
2116 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
2117 | break; | |
2118 | /* Otherwise fall through. */ | |
2119 | default: | |
bf67003b | 2120 | goto fail; |
cfcac11d NC |
2121 | } |
2122 | } | |
2123 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 2124 | goto fail; |
cfcac11d | 2125 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
2126 | { |
2127 | Elf_Internal_Shdr *dynsymhdr; | |
2128 | ||
2129 | /* The shared libraries distributed with hpux11 have a bogus | |
2130 | sh_link field for the ".dynamic" section. Find the | |
2131 | string table for the ".dynsym" section instead. */ | |
2132 | if (elf_dynsymtab (abfd) != 0) | |
2133 | { | |
2134 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
2135 | hdr->sh_link = dynsymhdr->sh_link; | |
2136 | } | |
2137 | else | |
2138 | { | |
2139 | unsigned int i, num_sec; | |
2140 | ||
2141 | num_sec = elf_numsections (abfd); | |
2142 | for (i = 1; i < num_sec; i++) | |
2143 | { | |
2144 | dynsymhdr = elf_elfsections (abfd)[i]; | |
2145 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
2146 | { | |
2147 | hdr->sh_link = dynsymhdr->sh_link; | |
2148 | break; | |
2149 | } | |
2150 | } | |
2151 | } | |
2152 | } | |
bf67003b | 2153 | goto success; |
797fc050 | 2154 | |
bf67003b | 2155 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 2156 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 2157 | goto success; |
252b5132 | 2158 | |
a50b2160 | 2159 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2160 | goto fail; |
2161 | ||
3337c1e5 | 2162 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
2163 | { |
2164 | if (hdr->sh_size != 0) | |
bf67003b | 2165 | goto fail; |
eee3b786 AM |
2166 | /* Some assemblers erroneously set sh_info to one with a |
2167 | zero sh_size. ld sees this as a global symbol count | |
2168 | of (unsigned) -1. Fix it here. */ | |
2169 | hdr->sh_info = 0; | |
bf67003b | 2170 | goto success; |
eee3b786 | 2171 | } |
bf67003b | 2172 | |
16ad13ec NC |
2173 | /* PR 18854: A binary might contain more than one symbol table. |
2174 | Unusual, but possible. Warn, but continue. */ | |
2175 | if (elf_onesymtab (abfd) != 0) | |
2176 | { | |
4eca0228 | 2177 | _bfd_error_handler |
695344c0 | 2178 | /* xgettext:c-format */ |
871b3ab2 | 2179 | (_("%pB: warning: multiple symbol tables detected" |
63a5468a | 2180 | " - ignoring the table in section %u"), |
16ad13ec NC |
2181 | abfd, shindex); |
2182 | goto success; | |
2183 | } | |
252b5132 | 2184 | elf_onesymtab (abfd) = shindex; |
6a40cf0c NC |
2185 | elf_symtab_hdr (abfd) = *hdr; |
2186 | elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); | |
252b5132 RH |
2187 | abfd->flags |= HAS_SYMS; |
2188 | ||
2189 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
2190 | SHF_ALLOC is set, and this is a shared object, then we also |
2191 | treat this section as a BFD section. We can not base the | |
2192 | decision purely on SHF_ALLOC, because that flag is sometimes | |
2193 | set in a relocatable object file, which would confuse the | |
2194 | linker. */ | |
252b5132 RH |
2195 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
2196 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
2197 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2198 | shindex)) | |
bf67003b | 2199 | goto fail; |
252b5132 | 2200 | |
1b3a8575 AM |
2201 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
2202 | can't read symbols without that section loaded as well. It | |
2203 | is most likely specified by the next section header. */ | |
6a40cf0c NC |
2204 | { |
2205 | elf_section_list * entry; | |
2206 | unsigned int i, num_sec; | |
1b3a8575 | 2207 | |
6a40cf0c NC |
2208 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2209 | if (entry->hdr.sh_link == shindex) | |
2210 | goto success; | |
2211 | ||
2212 | num_sec = elf_numsections (abfd); | |
2213 | for (i = shindex + 1; i < num_sec; i++) | |
2214 | { | |
2215 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2216 | ||
2217 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
2218 | && hdr2->sh_link == shindex) | |
2219 | break; | |
2220 | } | |
2221 | ||
2222 | if (i == num_sec) | |
2223 | for (i = 1; i < shindex; i++) | |
1b3a8575 AM |
2224 | { |
2225 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
6a40cf0c | 2226 | |
1b3a8575 AM |
2227 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX |
2228 | && hdr2->sh_link == shindex) | |
2229 | break; | |
2230 | } | |
6a40cf0c NC |
2231 | |
2232 | if (i != shindex) | |
2233 | ret = bfd_section_from_shdr (abfd, i); | |
2234 | /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ | |
2235 | goto success; | |
2236 | } | |
252b5132 | 2237 | |
bf67003b | 2238 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 2239 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 2240 | goto success; |
252b5132 | 2241 | |
a50b2160 | 2242 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2243 | goto fail; |
2244 | ||
eee3b786 AM |
2245 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
2246 | { | |
2247 | if (hdr->sh_size != 0) | |
bf67003b NC |
2248 | goto fail; |
2249 | ||
eee3b786 AM |
2250 | /* Some linkers erroneously set sh_info to one with a |
2251 | zero sh_size. ld sees this as a global symbol count | |
2252 | of (unsigned) -1. Fix it here. */ | |
2253 | hdr->sh_info = 0; | |
bf67003b | 2254 | goto success; |
eee3b786 | 2255 | } |
bf67003b | 2256 | |
16ad13ec NC |
2257 | /* PR 18854: A binary might contain more than one dynamic symbol table. |
2258 | Unusual, but possible. Warn, but continue. */ | |
2259 | if (elf_dynsymtab (abfd) != 0) | |
2260 | { | |
4eca0228 | 2261 | _bfd_error_handler |
695344c0 | 2262 | /* xgettext:c-format */ |
871b3ab2 | 2263 | (_("%pB: warning: multiple dynamic symbol tables detected" |
63a5468a | 2264 | " - ignoring the table in section %u"), |
16ad13ec NC |
2265 | abfd, shindex); |
2266 | goto success; | |
2267 | } | |
252b5132 RH |
2268 | elf_dynsymtab (abfd) = shindex; |
2269 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
2270 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2271 | abfd->flags |= HAS_SYMS; | |
2272 | ||
2273 | /* Besides being a symbol table, we also treat this as a regular | |
2274 | section, so that objcopy can handle it. */ | |
bf67003b NC |
2275 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2276 | goto success; | |
252b5132 | 2277 | |
bf67003b | 2278 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
6a40cf0c NC |
2279 | { |
2280 | elf_section_list * entry; | |
9ad5cbcf | 2281 | |
6a40cf0c NC |
2282 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2283 | if (entry->ndx == shindex) | |
2284 | goto success; | |
07d6d2b8 | 2285 | |
7a6e0d89 | 2286 | entry = bfd_alloc (abfd, sizeof (*entry)); |
6a40cf0c NC |
2287 | if (entry == NULL) |
2288 | goto fail; | |
2289 | entry->ndx = shindex; | |
2290 | entry->hdr = * hdr; | |
2291 | entry->next = elf_symtab_shndx_list (abfd); | |
2292 | elf_symtab_shndx_list (abfd) = entry; | |
2293 | elf_elfsections (abfd)[shindex] = & entry->hdr; | |
2294 | goto success; | |
2295 | } | |
9ad5cbcf | 2296 | |
bf67003b | 2297 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 2298 | if (hdr->bfd_section != NULL) |
bf67003b NC |
2299 | goto success; |
2300 | ||
252b5132 RH |
2301 | if (ehdr->e_shstrndx == shindex) |
2302 | { | |
2303 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
2304 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 2305 | goto success; |
252b5132 | 2306 | } |
bf67003b | 2307 | |
1b3a8575 AM |
2308 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
2309 | { | |
2310 | symtab_strtab: | |
2311 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
2312 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 2313 | goto success; |
1b3a8575 | 2314 | } |
bf67003b | 2315 | |
1b3a8575 AM |
2316 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
2317 | { | |
2318 | dynsymtab_strtab: | |
2319 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
2320 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
2321 | elf_elfsections (abfd)[shindex] = hdr; | |
2322 | /* We also treat this as a regular section, so that objcopy | |
2323 | can handle it. */ | |
bf67003b NC |
2324 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2325 | shindex); | |
2326 | goto success; | |
1b3a8575 | 2327 | } |
252b5132 | 2328 | |
1b3a8575 AM |
2329 | /* If the string table isn't one of the above, then treat it as a |
2330 | regular section. We need to scan all the headers to be sure, | |
2331 | just in case this strtab section appeared before the above. */ | |
2332 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2333 | { | |
2334 | unsigned int i, num_sec; | |
252b5132 | 2335 | |
1b3a8575 AM |
2336 | num_sec = elf_numsections (abfd); |
2337 | for (i = 1; i < num_sec; i++) | |
2338 | { | |
2339 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2340 | if (hdr2->sh_link == shindex) | |
2341 | { | |
933d961a JJ |
2342 | /* Prevent endless recursion on broken objects. */ |
2343 | if (i == shindex) | |
bf67003b | 2344 | goto fail; |
1b3a8575 | 2345 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 2346 | goto fail; |
1b3a8575 AM |
2347 | if (elf_onesymtab (abfd) == i) |
2348 | goto symtab_strtab; | |
2349 | if (elf_dynsymtab (abfd) == i) | |
2350 | goto dynsymtab_strtab; | |
2351 | } | |
2352 | } | |
2353 | } | |
bf67003b NC |
2354 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2355 | goto success; | |
252b5132 RH |
2356 | |
2357 | case SHT_REL: | |
2358 | case SHT_RELA: | |
2359 | /* *These* do a lot of work -- but build no sections! */ | |
2360 | { | |
2361 | asection *target_sect; | |
d4730f92 | 2362 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 2363 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 | 2364 | struct bfd_elf_section_data *esdt; |
252b5132 | 2365 | |
aa2ca951 JJ |
2366 | if (hdr->sh_entsize |
2367 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 2368 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 2369 | goto fail; |
a50b2160 | 2370 | |
03ae5f59 | 2371 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 2372 | if (hdr->sh_link >= num_sec) |
03ae5f59 | 2373 | { |
4eca0228 | 2374 | _bfd_error_handler |
695344c0 | 2375 | /* xgettext:c-format */ |
871b3ab2 | 2376 | (_("%pB: invalid link %u for reloc section %s (index %u)"), |
4eca0228 | 2377 | abfd, hdr->sh_link, name, shindex); |
bf67003b NC |
2378 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2379 | shindex); | |
2380 | goto success; | |
03ae5f59 ILT |
2381 | } |
2382 | ||
252b5132 RH |
2383 | /* For some incomprehensible reason Oracle distributes |
2384 | libraries for Solaris in which some of the objects have | |
2385 | bogus sh_link fields. It would be nice if we could just | |
2386 | reject them, but, unfortunately, some people need to use | |
2387 | them. We scan through the section headers; if we find only | |
2388 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
2389 | to it. I hope this doesn't break anything. |
2390 | ||
2391 | Don't do it on executable nor shared library. */ | |
2392 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
2393 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
2394 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
2395 | { | |
9ad5cbcf | 2396 | unsigned int scan; |
252b5132 RH |
2397 | int found; |
2398 | ||
2399 | found = 0; | |
9ad5cbcf | 2400 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2401 | { |
2402 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2403 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2404 | { | |
2405 | if (found != 0) | |
2406 | { | |
2407 | found = 0; | |
2408 | break; | |
2409 | } | |
2410 | found = scan; | |
2411 | } | |
2412 | } | |
2413 | if (found != 0) | |
2414 | hdr->sh_link = found; | |
2415 | } | |
2416 | ||
2417 | /* Get the symbol table. */ | |
1b3a8575 AM |
2418 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2419 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2420 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 2421 | goto fail; |
252b5132 | 2422 | |
a4bcd733 AM |
2423 | /* If this is an alloc section in an executable or shared |
2424 | library, or the reloc section does not use the main symbol | |
2425 | table we don't treat it as a reloc section. BFD can't | |
2426 | adequately represent such a section, so at least for now, | |
2427 | we don't try. We just present it as a normal section. We | |
2428 | also can't use it as a reloc section if it points to the | |
2429 | null section, an invalid section, another reloc section, or | |
2430 | its sh_link points to the null section. */ | |
2431 | if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0 | |
2432 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
83b89087 | 2433 | || hdr->sh_link == SHN_UNDEF |
a4bcd733 | 2434 | || hdr->sh_link != elf_onesymtab (abfd) |
185ef66d | 2435 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2436 | || hdr->sh_info >= num_sec |
2437 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2438 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2439 | { |
2440 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2441 | shindex); | |
2442 | goto success; | |
2443 | } | |
252b5132 RH |
2444 | |
2445 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2446 | goto fail; |
2447 | ||
252b5132 RH |
2448 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2449 | if (target_sect == NULL) | |
bf67003b | 2450 | goto fail; |
252b5132 | 2451 | |
d4730f92 BS |
2452 | esdt = elf_section_data (target_sect); |
2453 | if (hdr->sh_type == SHT_RELA) | |
2454 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2455 | else |
d4730f92 BS |
2456 | p_hdr = &esdt->rel.hdr; |
2457 | ||
a7ba3896 NC |
2458 | /* PR 17512: file: 0b4f81b7. |
2459 | Also see PR 24456, for a file which deliberately has two reloc | |
2460 | sections. */ | |
06614111 | 2461 | if (*p_hdr != NULL) |
a7ba3896 | 2462 | { |
a859124d | 2463 | if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex)) |
a8e14f4c NC |
2464 | { |
2465 | _bfd_error_handler | |
2466 | /* xgettext:c-format */ | |
a859124d AM |
2467 | (_("%pB: warning: secondary relocation section '%s' " |
2468 | "for section %pA found - ignoring"), | |
a8e14f4c NC |
2469 | abfd, name, target_sect); |
2470 | } | |
a7ba3896 NC |
2471 | goto success; |
2472 | } | |
a8e14f4c | 2473 | |
ef53be89 | 2474 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); |
d4730f92 | 2475 | if (hdr2 == NULL) |
bf67003b | 2476 | goto fail; |
252b5132 | 2477 | *hdr2 = *hdr; |
d4730f92 | 2478 | *p_hdr = hdr2; |
252b5132 | 2479 | elf_elfsections (abfd)[shindex] = hdr2; |
056bafd4 MR |
2480 | target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr) |
2481 | * bed->s->int_rels_per_ext_rel); | |
252b5132 RH |
2482 | target_sect->flags |= SEC_RELOC; |
2483 | target_sect->relocation = NULL; | |
2484 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2485 | /* In the section to which the relocations apply, mark whether |
2486 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2487 | if (hdr->sh_size != 0) |
d4730f92 BS |
2488 | { |
2489 | if (hdr->sh_type == SHT_RELA) | |
2490 | target_sect->use_rela_p = 1; | |
2491 | } | |
252b5132 | 2492 | abfd->flags |= HAS_RELOC; |
bf67003b | 2493 | goto success; |
252b5132 | 2494 | } |
252b5132 RH |
2495 | |
2496 | case SHT_GNU_verdef: | |
2497 | elf_dynverdef (abfd) = shindex; | |
2498 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2499 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2500 | goto success; | |
252b5132 RH |
2501 | |
2502 | case SHT_GNU_versym: | |
a50b2160 | 2503 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2504 | goto fail; |
2505 | ||
252b5132 RH |
2506 | elf_dynversym (abfd) = shindex; |
2507 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2508 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2509 | goto success; | |
252b5132 RH |
2510 | |
2511 | case SHT_GNU_verneed: | |
2512 | elf_dynverref (abfd) = shindex; | |
2513 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2514 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2515 | goto success; | |
252b5132 RH |
2516 | |
2517 | case SHT_SHLIB: | |
bf67003b | 2518 | goto success; |
252b5132 | 2519 | |
dbb410c3 | 2520 | case SHT_GROUP: |
44534af3 | 2521 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2522 | goto fail; |
2523 | ||
6dc132d9 | 2524 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2525 | goto fail; |
2526 | ||
bf67003b | 2527 | goto success; |
dbb410c3 | 2528 | |
252b5132 | 2529 | default: |
104d59d1 JM |
2530 | /* Possibly an attributes section. */ |
2531 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2532 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2533 | { | |
2534 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2535 | goto fail; |
104d59d1 | 2536 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2537 | goto success; |
104d59d1 JM |
2538 | } |
2539 | ||
252b5132 | 2540 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2541 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2542 | goto success; |
3eb70a79 L |
2543 | |
2544 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2545 | { | |
2546 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2547 | /* FIXME: How to properly handle allocated section reserved | |
2548 | for applications? */ | |
4eca0228 | 2549 | _bfd_error_handler |
695344c0 | 2550 | /* xgettext:c-format */ |
871b3ab2 | 2551 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2552 | abfd, hdr->sh_type, name); |
3eb70a79 | 2553 | else |
bf67003b NC |
2554 | { |
2555 | /* Allow sections reserved for applications. */ | |
2556 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2557 | shindex); | |
2558 | goto success; | |
2559 | } | |
3eb70a79 L |
2560 | } |
2561 | else if (hdr->sh_type >= SHT_LOPROC | |
2562 | && hdr->sh_type <= SHT_HIPROC) | |
2563 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2564 | _bfd_error_handler |
695344c0 | 2565 | /* xgettext:c-format */ |
871b3ab2 | 2566 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2567 | abfd, hdr->sh_type, name); |
3eb70a79 | 2568 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) |
ff15b240 NC |
2569 | { |
2570 | /* Unrecognised OS-specific sections. */ | |
2571 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2572 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2573 | required to correctly process the section and the file should |
ff15b240 | 2574 | be rejected with an error message. */ |
4eca0228 | 2575 | _bfd_error_handler |
695344c0 | 2576 | /* xgettext:c-format */ |
871b3ab2 | 2577 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2578 | abfd, hdr->sh_type, name); |
ff15b240 | 2579 | else |
bf67003b NC |
2580 | { |
2581 | /* Otherwise it should be processed. */ | |
2582 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2583 | goto success; | |
2584 | } | |
ff15b240 | 2585 | } |
3eb70a79 L |
2586 | else |
2587 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2588 | _bfd_error_handler |
695344c0 | 2589 | /* xgettext:c-format */ |
871b3ab2 | 2590 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2591 | abfd, hdr->sh_type, name); |
3eb70a79 | 2592 | |
bf67003b | 2593 | goto fail; |
252b5132 RH |
2594 | } |
2595 | ||
bf67003b NC |
2596 | fail: |
2597 | ret = FALSE; | |
2598 | success: | |
e5b470e2 | 2599 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2600 | sections_being_created [shindex] = FALSE; |
2601 | if (-- nesting == 0) | |
5a4b0ccc NC |
2602 | { |
2603 | sections_being_created = NULL; | |
2604 | sections_being_created_abfd = abfd; | |
2605 | } | |
bf67003b | 2606 | return ret; |
252b5132 RH |
2607 | } |
2608 | ||
87d72d41 | 2609 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2610 | |
87d72d41 AM |
2611 | Elf_Internal_Sym * |
2612 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2613 | bfd *abfd, | |
2614 | unsigned long r_symndx) | |
ec338859 | 2615 | { |
ec338859 AM |
2616 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2617 | ||
a5d1b3b5 AM |
2618 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2619 | { | |
2620 | Elf_Internal_Shdr *symtab_hdr; | |
2621 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2622 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2623 | |
a5d1b3b5 AM |
2624 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2625 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2626 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2627 | return NULL; |
9ad5cbcf | 2628 | |
a5d1b3b5 AM |
2629 | if (cache->abfd != abfd) |
2630 | { | |
2631 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2632 | cache->abfd = abfd; | |
2633 | } | |
2634 | cache->indx[ent] = r_symndx; | |
ec338859 | 2635 | } |
a5d1b3b5 | 2636 | |
87d72d41 | 2637 | return &cache->sym[ent]; |
ec338859 AM |
2638 | } |
2639 | ||
252b5132 RH |
2640 | /* Given an ELF section number, retrieve the corresponding BFD |
2641 | section. */ | |
2642 | ||
2643 | asection * | |
91d6fa6a | 2644 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2645 | { |
91d6fa6a | 2646 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2647 | return NULL; |
91d6fa6a | 2648 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2649 | } |
2650 | ||
b35d266b | 2651 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2652 | { |
0112cd26 | 2653 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 | 2654 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2655 | }; |
2656 | ||
b35d266b | 2657 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2658 | { |
0112cd26 | 2659 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
1ff6de03 | 2660 | { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 | 2661 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2662 | }; |
2663 | ||
b35d266b | 2664 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2665 | { |
07d6d2b8 AM |
2666 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2667 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2668 | /* There are more DWARF sections than these, but they needn't be added here |
2669 | unless you have to cope with broken compilers that don't emit section | |
2670 | attributes or you want to help the user writing assembler. */ | |
07d6d2b8 AM |
2671 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2672 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2673 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2674 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
0112cd26 | 2675 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 AM |
2676 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, |
2677 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2678 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2679 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2680 | }; |
2681 | ||
b35d266b | 2682 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2683 | { |
07d6d2b8 | 2684 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
6f9dbcd4 | 2685 | { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 | 2686 | { NULL, 0 , 0, 0, 0 } |
7f4d3958 L |
2687 | }; |
2688 | ||
b35d266b | 2689 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2690 | { |
0112cd26 | 2691 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2692 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
2693 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2694 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
0112cd26 NC |
2695 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, |
2696 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
07d6d2b8 AM |
2697 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, |
2698 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2699 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2700 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2701 | }; |
2702 | ||
b35d266b | 2703 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2704 | { |
07d6d2b8 AM |
2705 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2706 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2707 | }; |
2708 | ||
b35d266b | 2709 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2710 | { |
07d6d2b8 | 2711 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
6f9dbcd4 | 2712 | { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2713 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, |
2714 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2715 | }; |
2716 | ||
b35d266b | 2717 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2718 | { |
0112cd26 | 2719 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 | 2720 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2721 | }; |
2722 | ||
b35d266b | 2723 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2724 | { |
0112cd26 | 2725 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 AM |
2726 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, |
2727 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2728 | }; |
2729 | ||
b35d266b | 2730 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2731 | { |
6f9dbcd4 | 2732 | { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2733 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2734 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2735 | }; |
2736 | ||
b35d266b | 2737 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2738 | { |
0112cd26 NC |
2739 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2740 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
07d6d2b8 AM |
2741 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, |
2742 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2743 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2744 | }; |
2745 | ||
b35d266b | 2746 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2747 | { |
0112cd26 NC |
2748 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2749 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2750 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2751 | /* See struct bfd_elf_special_section declaration for the semantics of |
2752 | this special case where .prefix_length != strlen (.prefix). */ | |
2753 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
07d6d2b8 | 2754 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2755 | }; |
2756 | ||
b35d266b | 2757 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2758 | { |
07d6d2b8 AM |
2759 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2760 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
0112cd26 | 2761 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, |
07d6d2b8 | 2762 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2763 | }; |
2764 | ||
1b315056 CS |
2765 | static const struct bfd_elf_special_section special_sections_z[] = |
2766 | { | |
07d6d2b8 AM |
2767 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, |
2768 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
1b315056 CS |
2769 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, |
2770 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
07d6d2b8 | 2771 | { NULL, 0, 0, 0, 0 } |
1b315056 CS |
2772 | }; |
2773 | ||
e4c93b56 | 2774 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2775 | { |
7f4d3958 | 2776 | special_sections_b, /* 'b' */ |
98ece1b3 | 2777 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2778 | special_sections_d, /* 'd' */ |
2779 | NULL, /* 'e' */ | |
2780 | special_sections_f, /* 'f' */ | |
2781 | special_sections_g, /* 'g' */ | |
2782 | special_sections_h, /* 'h' */ | |
2783 | special_sections_i, /* 'i' */ | |
2784 | NULL, /* 'j' */ | |
2785 | NULL, /* 'k' */ | |
2786 | special_sections_l, /* 'l' */ | |
2787 | NULL, /* 'm' */ | |
2788 | special_sections_n, /* 'n' */ | |
2789 | NULL, /* 'o' */ | |
2790 | special_sections_p, /* 'p' */ | |
2791 | NULL, /* 'q' */ | |
2792 | special_sections_r, /* 'r' */ | |
2793 | special_sections_s, /* 's' */ | |
2794 | special_sections_t, /* 't' */ | |
1b315056 CS |
2795 | NULL, /* 'u' */ |
2796 | NULL, /* 'v' */ | |
2797 | NULL, /* 'w' */ | |
2798 | NULL, /* 'x' */ | |
2799 | NULL, /* 'y' */ | |
2800 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2801 | }; |
2802 | ||
551b43fd AM |
2803 | const struct bfd_elf_special_section * |
2804 | _bfd_elf_get_special_section (const char *name, | |
2805 | const struct bfd_elf_special_section *spec, | |
2806 | unsigned int rela) | |
2f89ff8d L |
2807 | { |
2808 | int i; | |
7f4d3958 | 2809 | int len; |
7f4d3958 | 2810 | |
551b43fd | 2811 | len = strlen (name); |
7f4d3958 | 2812 | |
551b43fd | 2813 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2814 | { |
2815 | int suffix_len; | |
551b43fd | 2816 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2817 | |
2818 | if (len < prefix_len) | |
2819 | continue; | |
551b43fd | 2820 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2821 | continue; |
2822 | ||
551b43fd | 2823 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2824 | if (suffix_len <= 0) |
2825 | { | |
2826 | if (name[prefix_len] != 0) | |
2827 | { | |
2828 | if (suffix_len == 0) | |
2829 | continue; | |
2830 | if (name[prefix_len] != '.' | |
2831 | && (suffix_len == -2 | |
551b43fd | 2832 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2833 | continue; |
2834 | } | |
2835 | } | |
2836 | else | |
2837 | { | |
2838 | if (len < prefix_len + suffix_len) | |
2839 | continue; | |
2840 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2841 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2842 | suffix_len) != 0) |
2843 | continue; | |
2844 | } | |
551b43fd | 2845 | return &spec[i]; |
7dcb9820 | 2846 | } |
2f89ff8d L |
2847 | |
2848 | return NULL; | |
2849 | } | |
2850 | ||
7dcb9820 | 2851 | const struct bfd_elf_special_section * |
29ef7005 | 2852 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2853 | { |
551b43fd AM |
2854 | int i; |
2855 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2856 | const struct elf_backend_data *bed; |
2f89ff8d L |
2857 | |
2858 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2859 | if (sec->name == NULL) |
2860 | return NULL; | |
2f89ff8d | 2861 | |
29ef7005 L |
2862 | bed = get_elf_backend_data (abfd); |
2863 | spec = bed->special_sections; | |
2864 | if (spec) | |
2865 | { | |
2866 | spec = _bfd_elf_get_special_section (sec->name, | |
2867 | bed->special_sections, | |
2868 | sec->use_rela_p); | |
2869 | if (spec != NULL) | |
2870 | return spec; | |
2871 | } | |
2872 | ||
551b43fd AM |
2873 | if (sec->name[0] != '.') |
2874 | return NULL; | |
2f89ff8d | 2875 | |
551b43fd | 2876 | i = sec->name[1] - 'b'; |
1b315056 | 2877 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2878 | return NULL; |
2879 | ||
2880 | spec = special_sections[i]; | |
2f89ff8d | 2881 | |
551b43fd AM |
2882 | if (spec == NULL) |
2883 | return NULL; | |
2884 | ||
2885 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2886 | } |
2887 | ||
b34976b6 | 2888 | bfd_boolean |
217aa764 | 2889 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2890 | { |
2891 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2892 | const struct elf_backend_data *bed; |
7dcb9820 | 2893 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2894 | |
f0abc2a1 AM |
2895 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2896 | if (sdata == NULL) | |
2897 | { | |
a50b1753 | 2898 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
07d6d2b8 | 2899 | sizeof (*sdata)); |
f0abc2a1 AM |
2900 | if (sdata == NULL) |
2901 | return FALSE; | |
217aa764 | 2902 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2903 | } |
bf572ba0 | 2904 | |
551b43fd AM |
2905 | /* Indicate whether or not this section should use RELA relocations. */ |
2906 | bed = get_elf_backend_data (abfd); | |
2907 | sec->use_rela_p = bed->default_use_rela_p; | |
2908 | ||
8c803a2d AM |
2909 | /* Set up ELF section type and flags for newly created sections, if |
2910 | there is an ABI mandated section. */ | |
2911 | ssect = (*bed->get_sec_type_attr) (abfd, sec); | |
2912 | if (ssect != NULL) | |
2f89ff8d | 2913 | { |
8c803a2d AM |
2914 | elf_section_type (sec) = ssect->type; |
2915 | elf_section_flags (sec) = ssect->attr; | |
2f89ff8d L |
2916 | } |
2917 | ||
f592407e | 2918 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2919 | } |
2920 | ||
2921 | /* Create a new bfd section from an ELF program header. | |
2922 | ||
2923 | Since program segments have no names, we generate a synthetic name | |
2924 | of the form segment<NUM>, where NUM is generally the index in the | |
2925 | program header table. For segments that are split (see below) we | |
2926 | generate the names segment<NUM>a and segment<NUM>b. | |
2927 | ||
2928 | Note that some program segments may have a file size that is different than | |
2929 | (less than) the memory size. All this means is that at execution the | |
2930 | system must allocate the amount of memory specified by the memory size, | |
2931 | but only initialize it with the first "file size" bytes read from the | |
2932 | file. This would occur for example, with program segments consisting | |
2933 | of combined data+bss. | |
2934 | ||
2935 | To handle the above situation, this routine generates TWO bfd sections | |
2936 | for the single program segment. The first has the length specified by | |
2937 | the file size of the segment, and the second has the length specified | |
2938 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2939 | into its initialized and uninitialized parts. |
252b5132 RH |
2940 | |
2941 | */ | |
2942 | ||
b34976b6 | 2943 | bfd_boolean |
217aa764 AM |
2944 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2945 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2946 | int hdr_index, |
a50b1753 | 2947 | const char *type_name) |
252b5132 RH |
2948 | { |
2949 | asection *newsect; | |
2950 | char *name; | |
2951 | char namebuf[64]; | |
d4c88bbb | 2952 | size_t len; |
252b5132 | 2953 | int split; |
502794d4 | 2954 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
252b5132 RH |
2955 | |
2956 | split = ((hdr->p_memsz > 0) | |
2957 | && (hdr->p_filesz > 0) | |
2958 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2959 | |
2960 | if (hdr->p_filesz > 0) | |
252b5132 | 2961 | { |
91d6fa6a | 2962 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2963 | len = strlen (namebuf) + 1; |
a50b1753 | 2964 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2965 | if (!name) |
2966 | return FALSE; | |
2967 | memcpy (name, namebuf, len); | |
2968 | newsect = bfd_make_section (abfd, name); | |
2969 | if (newsect == NULL) | |
2970 | return FALSE; | |
502794d4 CE |
2971 | newsect->vma = hdr->p_vaddr / opb; |
2972 | newsect->lma = hdr->p_paddr / opb; | |
d5191d0c AM |
2973 | newsect->size = hdr->p_filesz; |
2974 | newsect->filepos = hdr->p_offset; | |
2975 | newsect->flags |= SEC_HAS_CONTENTS; | |
2976 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2977 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2978 | { |
d5191d0c AM |
2979 | newsect->flags |= SEC_ALLOC; |
2980 | newsect->flags |= SEC_LOAD; | |
2981 | if (hdr->p_flags & PF_X) | |
2982 | { | |
2983 | /* FIXME: all we known is that it has execute PERMISSION, | |
2984 | may be data. */ | |
2985 | newsect->flags |= SEC_CODE; | |
2986 | } | |
2987 | } | |
2988 | if (!(hdr->p_flags & PF_W)) | |
2989 | { | |
2990 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2991 | } |
252b5132 RH |
2992 | } |
2993 | ||
d5191d0c | 2994 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2995 | { |
d5191d0c AM |
2996 | bfd_vma align; |
2997 | ||
91d6fa6a | 2998 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2999 | len = strlen (namebuf) + 1; |
a50b1753 | 3000 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 3001 | if (!name) |
b34976b6 | 3002 | return FALSE; |
d4c88bbb | 3003 | memcpy (name, namebuf, len); |
252b5132 RH |
3004 | newsect = bfd_make_section (abfd, name); |
3005 | if (newsect == NULL) | |
b34976b6 | 3006 | return FALSE; |
502794d4 CE |
3007 | newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb; |
3008 | newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb; | |
eea6121a | 3009 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
3010 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
3011 | align = newsect->vma & -newsect->vma; | |
3012 | if (align == 0 || align > hdr->p_align) | |
3013 | align = hdr->p_align; | |
3014 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
3015 | if (hdr->p_type == PT_LOAD) |
3016 | { | |
d5191d0c AM |
3017 | /* Hack for gdb. Segments that have not been modified do |
3018 | not have their contents written to a core file, on the | |
3019 | assumption that a debugger can find the contents in the | |
3020 | executable. We flag this case by setting the fake | |
3021 | section size to zero. Note that "real" bss sections will | |
3022 | always have their contents dumped to the core file. */ | |
3023 | if (bfd_get_format (abfd) == bfd_core) | |
3024 | newsect->size = 0; | |
252b5132 RH |
3025 | newsect->flags |= SEC_ALLOC; |
3026 | if (hdr->p_flags & PF_X) | |
3027 | newsect->flags |= SEC_CODE; | |
3028 | } | |
3029 | if (!(hdr->p_flags & PF_W)) | |
3030 | newsect->flags |= SEC_READONLY; | |
3031 | } | |
3032 | ||
b34976b6 | 3033 | return TRUE; |
252b5132 RH |
3034 | } |
3035 | ||
864619bb KS |
3036 | static bfd_boolean |
3037 | _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset) | |
3038 | { | |
3039 | /* The return value is ignored. Build-ids are considered optional. */ | |
3040 | if (templ->xvec->flavour == bfd_target_elf_flavour) | |
3041 | return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id) | |
3042 | (templ, offset); | |
3043 | return FALSE; | |
3044 | } | |
3045 | ||
b34976b6 | 3046 | bfd_boolean |
91d6fa6a | 3047 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 3048 | { |
9c5bfbb7 | 3049 | const struct elf_backend_data *bed; |
20cfcaae NC |
3050 | |
3051 | switch (hdr->p_type) | |
3052 | { | |
3053 | case PT_NULL: | |
91d6fa6a | 3054 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
3055 | |
3056 | case PT_LOAD: | |
864619bb KS |
3057 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load")) |
3058 | return FALSE; | |
3059 | if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL) | |
3060 | _bfd_elf_core_find_build_id (abfd, hdr->p_offset); | |
3061 | return TRUE; | |
20cfcaae NC |
3062 | |
3063 | case PT_DYNAMIC: | |
91d6fa6a | 3064 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
3065 | |
3066 | case PT_INTERP: | |
91d6fa6a | 3067 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
3068 | |
3069 | case PT_NOTE: | |
91d6fa6a | 3070 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 3071 | return FALSE; |
276da9b3 L |
3072 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz, |
3073 | hdr->p_align)) | |
b34976b6 AM |
3074 | return FALSE; |
3075 | return TRUE; | |
20cfcaae NC |
3076 | |
3077 | case PT_SHLIB: | |
91d6fa6a | 3078 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
3079 | |
3080 | case PT_PHDR: | |
91d6fa6a | 3081 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 3082 | |
811072d8 | 3083 | case PT_GNU_EH_FRAME: |
91d6fa6a | 3084 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
3085 | "eh_frame_hdr"); |
3086 | ||
2b05f1b7 | 3087 | case PT_GNU_STACK: |
91d6fa6a | 3088 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 3089 | |
8c37241b | 3090 | case PT_GNU_RELRO: |
91d6fa6a | 3091 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 3092 | |
20cfcaae | 3093 | default: |
8c1acd09 | 3094 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 3095 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 3096 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
3097 | } |
3098 | } | |
3099 | ||
d4730f92 BS |
3100 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
3101 | REL or RELA. */ | |
3102 | ||
3103 | Elf_Internal_Shdr * | |
3104 | _bfd_elf_single_rel_hdr (asection *sec) | |
3105 | { | |
3106 | if (elf_section_data (sec)->rel.hdr) | |
3107 | { | |
3108 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
3109 | return elf_section_data (sec)->rel.hdr; | |
3110 | } | |
3111 | else | |
3112 | return elf_section_data (sec)->rela.hdr; | |
3113 | } | |
3114 | ||
3e19fb8f L |
3115 | static bfd_boolean |
3116 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
3117 | Elf_Internal_Shdr *rel_hdr, | |
3118 | const char *sec_name, | |
3119 | bfd_boolean use_rela_p) | |
3120 | { | |
3121 | char *name = (char *) bfd_alloc (abfd, | |
3122 | sizeof ".rela" + strlen (sec_name)); | |
3123 | if (name == NULL) | |
3124 | return FALSE; | |
3125 | ||
3126 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
3127 | rel_hdr->sh_name = | |
3128 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
3129 | FALSE); | |
3130 | if (rel_hdr->sh_name == (unsigned int) -1) | |
3131 | return FALSE; | |
3132 | ||
3133 | return TRUE; | |
3134 | } | |
3135 | ||
d4730f92 BS |
3136 | /* Allocate and initialize a section-header for a new reloc section, |
3137 | containing relocations against ASECT. It is stored in RELDATA. If | |
3138 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
3139 | relocations. */ | |
23bc299b | 3140 | |
5d13b3b3 | 3141 | static bfd_boolean |
217aa764 | 3142 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 3143 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 3144 | const char *sec_name, |
3e19fb8f L |
3145 | bfd_boolean use_rela_p, |
3146 | bfd_boolean delay_st_name_p) | |
23bc299b | 3147 | { |
d4730f92 | 3148 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 3149 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3150 | |
d4730f92 | 3151 | BFD_ASSERT (reldata->hdr == NULL); |
ef53be89 | 3152 | rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); |
d4730f92 | 3153 | reldata->hdr = rel_hdr; |
23bc299b | 3154 | |
3e19fb8f L |
3155 | if (delay_st_name_p) |
3156 | rel_hdr->sh_name = (unsigned int) -1; | |
3157 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
3158 | use_rela_p)) | |
b34976b6 | 3159 | return FALSE; |
23bc299b MM |
3160 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
3161 | rel_hdr->sh_entsize = (use_rela_p | |
3162 | ? bed->s->sizeof_rela | |
3163 | : bed->s->sizeof_rel); | |
72de5009 | 3164 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
28e07a05 | 3165 | rel_hdr->sh_flags = 0; |
23bc299b MM |
3166 | rel_hdr->sh_addr = 0; |
3167 | rel_hdr->sh_size = 0; | |
3168 | rel_hdr->sh_offset = 0; | |
3169 | ||
b34976b6 | 3170 | return TRUE; |
23bc299b MM |
3171 | } |
3172 | ||
94be91de JB |
3173 | /* Return the default section type based on the passed in section flags. */ |
3174 | ||
3175 | int | |
3176 | bfd_elf_get_default_section_type (flagword flags) | |
3177 | { | |
0e41bebb | 3178 | if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0 |
2e76e85a | 3179 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
3180 | return SHT_NOBITS; |
3181 | return SHT_PROGBITS; | |
3182 | } | |
3183 | ||
d4730f92 BS |
3184 | struct fake_section_arg |
3185 | { | |
3186 | struct bfd_link_info *link_info; | |
3187 | bfd_boolean failed; | |
3188 | }; | |
3189 | ||
252b5132 RH |
3190 | /* Set up an ELF internal section header for a section. */ |
3191 | ||
252b5132 | 3192 | static void |
d4730f92 | 3193 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 3194 | { |
d4730f92 | 3195 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 3196 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3197 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 3198 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 3199 | unsigned int sh_type; |
0ce398f1 | 3200 | const char *name = asect->name; |
3e19fb8f | 3201 | bfd_boolean delay_st_name_p = FALSE; |
233bf4f8 | 3202 | bfd_vma mask; |
252b5132 | 3203 | |
d4730f92 | 3204 | if (arg->failed) |
252b5132 RH |
3205 | { |
3206 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 3207 | loop. */ |
252b5132 RH |
3208 | return; |
3209 | } | |
3210 | ||
d4730f92 | 3211 | this_hdr = &esd->this_hdr; |
252b5132 | 3212 | |
f6fe1ccd | 3213 | if (arg->link_info) |
0ce398f1 | 3214 | { |
f6fe1ccd L |
3215 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
3216 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
3217 | && (asect->flags & SEC_DEBUGGING) | |
3218 | && name[1] == 'd' | |
3219 | && name[6] == '_') | |
3220 | { | |
3221 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
3222 | compressed. */ | |
3223 | asect->flags |= SEC_ELF_COMPRESS; | |
dd905818 | 3224 | /* If this section will be compressed, delay adding section |
3e19fb8f L |
3225 | name to section name section after it is compressed in |
3226 | _bfd_elf_assign_file_positions_for_non_load. */ | |
3227 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
3228 | } |
3229 | } | |
3230 | else if ((asect->flags & SEC_ELF_RENAME)) | |
3231 | { | |
3232 | /* objcopy: rename output DWARF debug section. */ | |
3233 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
3234 | { | |
3235 | /* When we decompress or compress with SHF_COMPRESSED, | |
3236 | convert section name from .zdebug_* to .debug_* if | |
3237 | needed. */ | |
3238 | if (name[1] == 'z') | |
3239 | { | |
3240 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
3241 | if (new_name == NULL) | |
3242 | { | |
3243 | arg->failed = TRUE; | |
3244 | return; | |
3245 | } | |
3246 | name = new_name; | |
3247 | } | |
3248 | } | |
3249 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 3250 | { |
f6fe1ccd L |
3251 | /* PR binutils/18087: Compression does not always make a |
3252 | section smaller. So only rename the section when | |
3253 | compression has actually taken place. If input section | |
3254 | name is .zdebug_*, we should never compress it again. */ | |
3255 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
3256 | if (new_name == NULL) |
3257 | { | |
3258 | arg->failed = TRUE; | |
3259 | return; | |
3260 | } | |
f6fe1ccd L |
3261 | BFD_ASSERT (name[1] != 'z'); |
3262 | name = new_name; | |
0ce398f1 L |
3263 | } |
3264 | } | |
3265 | ||
3e19fb8f L |
3266 | if (delay_st_name_p) |
3267 | this_hdr->sh_name = (unsigned int) -1; | |
3268 | else | |
252b5132 | 3269 | { |
3e19fb8f L |
3270 | this_hdr->sh_name |
3271 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
3272 | name, FALSE); | |
3273 | if (this_hdr->sh_name == (unsigned int) -1) | |
3274 | { | |
3275 | arg->failed = TRUE; | |
3276 | return; | |
3277 | } | |
252b5132 RH |
3278 | } |
3279 | ||
a4d8e49b | 3280 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
3281 | |
3282 | if ((asect->flags & SEC_ALLOC) != 0 | |
3283 | || asect->user_set_vma) | |
502794d4 | 3284 | this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect); |
252b5132 RH |
3285 | else |
3286 | this_hdr->sh_addr = 0; | |
3287 | ||
3288 | this_hdr->sh_offset = 0; | |
eea6121a | 3289 | this_hdr->sh_size = asect->size; |
252b5132 | 3290 | this_hdr->sh_link = 0; |
c86934ce NC |
3291 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
3292 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
3293 | { | |
4eca0228 | 3294 | _bfd_error_handler |
695344c0 | 3295 | /* xgettext:c-format */ |
9793eb77 | 3296 | (_("%pB: error: alignment power %d of section `%pA' is too big"), |
c08bb8dd | 3297 | abfd, asect->alignment_power, asect); |
c86934ce NC |
3298 | arg->failed = TRUE; |
3299 | return; | |
3300 | } | |
233bf4f8 AM |
3301 | /* Set sh_addralign to the highest power of two given by alignment |
3302 | consistent with the section VMA. Linker scripts can force VMA. */ | |
3303 | mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr; | |
3304 | this_hdr->sh_addralign = mask & -mask; | |
252b5132 RH |
3305 | /* The sh_entsize and sh_info fields may have been set already by |
3306 | copy_private_section_data. */ | |
3307 | ||
3308 | this_hdr->bfd_section = asect; | |
3309 | this_hdr->contents = NULL; | |
3310 | ||
3cddba1e L |
3311 | /* If the section type is unspecified, we set it based on |
3312 | asect->flags. */ | |
98ece1b3 AM |
3313 | if ((asect->flags & SEC_GROUP) != 0) |
3314 | sh_type = SHT_GROUP; | |
98ece1b3 | 3315 | else |
94be91de | 3316 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 3317 | |
3cddba1e | 3318 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
3319 | this_hdr->sh_type = sh_type; |
3320 | else if (this_hdr->sh_type == SHT_NOBITS | |
3321 | && sh_type == SHT_PROGBITS | |
3322 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 3323 | { |
98ece1b3 AM |
3324 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
3325 | allow the link to proceed. This can happen when users link | |
3326 | non-bss input sections to bss output sections, or emit data | |
3327 | to a bss output section via a linker script. */ | |
4eca0228 | 3328 | _bfd_error_handler |
871b3ab2 | 3329 | (_("warning: section `%pA' type changed to PROGBITS"), asect); |
98ece1b3 | 3330 | this_hdr->sh_type = sh_type; |
3cddba1e L |
3331 | } |
3332 | ||
2f89ff8d | 3333 | switch (this_hdr->sh_type) |
252b5132 | 3334 | { |
2f89ff8d | 3335 | default: |
2f89ff8d L |
3336 | break; |
3337 | ||
3338 | case SHT_STRTAB: | |
2f89ff8d L |
3339 | case SHT_NOTE: |
3340 | case SHT_NOBITS: | |
3341 | case SHT_PROGBITS: | |
3342 | break; | |
606851fb AM |
3343 | |
3344 | case SHT_INIT_ARRAY: | |
3345 | case SHT_FINI_ARRAY: | |
3346 | case SHT_PREINIT_ARRAY: | |
3347 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
3348 | break; | |
2f89ff8d L |
3349 | |
3350 | case SHT_HASH: | |
c7ac6ff8 | 3351 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 3352 | break; |
5de3bf90 | 3353 | |
2f89ff8d | 3354 | case SHT_DYNSYM: |
252b5132 | 3355 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
3356 | break; |
3357 | ||
3358 | case SHT_DYNAMIC: | |
252b5132 | 3359 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
3360 | break; |
3361 | ||
3362 | case SHT_RELA: | |
3363 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
3364 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
3365 | break; | |
3366 | ||
3367 | case SHT_REL: | |
3368 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
3369 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
3370 | break; | |
3371 | ||
3372 | case SHT_GNU_versym: | |
252b5132 | 3373 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
3374 | break; |
3375 | ||
3376 | case SHT_GNU_verdef: | |
252b5132 RH |
3377 | this_hdr->sh_entsize = 0; |
3378 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3379 | cverdefs. The linker will set cverdefs, but sh_info will be |
3380 | zero. */ | |
252b5132 RH |
3381 | if (this_hdr->sh_info == 0) |
3382 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
3383 | else | |
3384 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
3385 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
3386 | break; |
3387 | ||
3388 | case SHT_GNU_verneed: | |
252b5132 RH |
3389 | this_hdr->sh_entsize = 0; |
3390 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3391 | cverrefs. The linker will set cverrefs, but sh_info will be |
3392 | zero. */ | |
252b5132 RH |
3393 | if (this_hdr->sh_info == 0) |
3394 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
3395 | else | |
3396 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
3397 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
3398 | break; |
3399 | ||
3400 | case SHT_GROUP: | |
1783205a | 3401 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 3402 | break; |
fdc90cb4 JJ |
3403 | |
3404 | case SHT_GNU_HASH: | |
3405 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
3406 | break; | |
dbb410c3 | 3407 | } |
252b5132 RH |
3408 | |
3409 | if ((asect->flags & SEC_ALLOC) != 0) | |
3410 | this_hdr->sh_flags |= SHF_ALLOC; | |
3411 | if ((asect->flags & SEC_READONLY) == 0) | |
3412 | this_hdr->sh_flags |= SHF_WRITE; | |
3413 | if ((asect->flags & SEC_CODE) != 0) | |
3414 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
3415 | if ((asect->flags & SEC_MERGE) != 0) |
3416 | { | |
3417 | this_hdr->sh_flags |= SHF_MERGE; | |
3418 | this_hdr->sh_entsize = asect->entsize; | |
f5fa8ca2 | 3419 | } |
84865015 NC |
3420 | if ((asect->flags & SEC_STRINGS) != 0) |
3421 | this_hdr->sh_flags |= SHF_STRINGS; | |
1126897b | 3422 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 3423 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 3424 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
3425 | { |
3426 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
3427 | if (asect->size == 0 |
3428 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 3429 | { |
3a800eb9 | 3430 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 3431 | |
704afa60 | 3432 | this_hdr->sh_size = 0; |
3a800eb9 AM |
3433 | if (o != NULL) |
3434 | { | |
704afa60 | 3435 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
3436 | if (this_hdr->sh_size != 0) |
3437 | this_hdr->sh_type = SHT_NOBITS; | |
3438 | } | |
704afa60 JJ |
3439 | } |
3440 | } | |
18ae9cc1 L |
3441 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3442 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3443 | |
d4730f92 BS |
3444 | /* If the section has relocs, set up a section header for the |
3445 | SHT_REL[A] section. If two relocation sections are required for | |
3446 | this section, it is up to the processor-specific back-end to | |
3447 | create the other. */ | |
3448 | if ((asect->flags & SEC_RELOC) != 0) | |
3449 | { | |
3450 | /* When doing a relocatable link, create both REL and RELA sections if | |
3451 | needed. */ | |
3452 | if (arg->link_info | |
3453 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3454 | && esd->rel.count + esd->rela.count > 0 | |
0e1862bb L |
3455 | && (bfd_link_relocatable (arg->link_info) |
3456 | || arg->link_info->emitrelocations)) | |
d4730f92 BS |
3457 | { |
3458 | if (esd->rel.count && esd->rel.hdr == NULL | |
28e07a05 | 3459 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, |
db4677b8 | 3460 | FALSE, delay_st_name_p)) |
d4730f92 BS |
3461 | { |
3462 | arg->failed = TRUE; | |
3463 | return; | |
3464 | } | |
3465 | if (esd->rela.count && esd->rela.hdr == NULL | |
28e07a05 | 3466 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, |
db4677b8 | 3467 | TRUE, delay_st_name_p)) |
d4730f92 BS |
3468 | { |
3469 | arg->failed = TRUE; | |
3470 | return; | |
3471 | } | |
3472 | } | |
3473 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3474 | (asect->use_rela_p | |
3475 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3476 | name, |
3e19fb8f L |
3477 | asect->use_rela_p, |
3478 | delay_st_name_p)) | |
db4677b8 | 3479 | { |
d4730f92 | 3480 | arg->failed = TRUE; |
db4677b8 AM |
3481 | return; |
3482 | } | |
d4730f92 BS |
3483 | } |
3484 | ||
252b5132 | 3485 | /* Check for processor-specific section types. */ |
0414f35b | 3486 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3487 | if (bed->elf_backend_fake_sections |
3488 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
db4677b8 AM |
3489 | { |
3490 | arg->failed = TRUE; | |
3491 | return; | |
3492 | } | |
252b5132 | 3493 | |
42bb2e33 | 3494 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3495 | { |
3496 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3497 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3498 | this_hdr->sh_type = sh_type; |
3499 | } | |
252b5132 RH |
3500 | } |
3501 | ||
bcacc0f5 AM |
3502 | /* Fill in the contents of a SHT_GROUP section. Called from |
3503 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3504 | when ELF targets use the generic linker, ld. Called for ld -r | |
3505 | from bfd_elf_final_link. */ | |
dbb410c3 | 3506 | |
1126897b | 3507 | void |
217aa764 | 3508 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3509 | { |
a50b1753 | 3510 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3511 | asection *elt, *first; |
dbb410c3 | 3512 | unsigned char *loc; |
b34976b6 | 3513 | bfd_boolean gas; |
dbb410c3 | 3514 | |
7e4111ad L |
3515 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3516 | elfxx-ia64.c. */ | |
ce5aecf8 AM |
3517 | if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP |
3518 | || sec->size == 0 | |
dbb410c3 AM |
3519 | || *failedptr) |
3520 | return; | |
3521 | ||
bcacc0f5 AM |
3522 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3523 | { | |
3524 | unsigned long symindx = 0; | |
3525 | ||
3526 | /* elf_group_id will have been set up by objcopy and the | |
3527 | generic linker. */ | |
3528 | if (elf_group_id (sec) != NULL) | |
3529 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3530 | |
bcacc0f5 AM |
3531 | if (symindx == 0) |
3532 | { | |
3533 | /* If called from the assembler, swap_out_syms will have set up | |
6a541707 NC |
3534 | elf_section_syms. |
3535 | PR 25699: A corrupt input file could contain bogus group info. */ | |
3536 | if (elf_section_syms (abfd) == NULL) | |
3537 | { | |
3538 | *failedptr = TRUE; | |
3539 | return; | |
3540 | } | |
bcacc0f5 AM |
3541 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; |
3542 | } | |
3543 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3544 | } | |
3545 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3546 | { |
bcacc0f5 AM |
3547 | /* The ELF backend linker sets sh_info to -2 when the group |
3548 | signature symbol is global, and thus the index can't be | |
3549 | set until all local symbols are output. */ | |
53720c49 AM |
3550 | asection *igroup; |
3551 | struct bfd_elf_section_data *sec_data; | |
3552 | unsigned long symndx; | |
3553 | unsigned long extsymoff; | |
bcacc0f5 AM |
3554 | struct elf_link_hash_entry *h; |
3555 | ||
53720c49 AM |
3556 | /* The point of this little dance to the first SHF_GROUP section |
3557 | then back to the SHT_GROUP section is that this gets us to | |
3558 | the SHT_GROUP in the input object. */ | |
3559 | igroup = elf_sec_group (elf_next_in_group (sec)); | |
3560 | sec_data = elf_section_data (igroup); | |
3561 | symndx = sec_data->this_hdr.sh_info; | |
3562 | extsymoff = 0; | |
bcacc0f5 AM |
3563 | if (!elf_bad_symtab (igroup->owner)) |
3564 | { | |
3565 | Elf_Internal_Shdr *symtab_hdr; | |
3566 | ||
3567 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3568 | extsymoff = symtab_hdr->sh_info; | |
3569 | } | |
3570 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3571 | while (h->root.type == bfd_link_hash_indirect | |
3572 | || h->root.type == bfd_link_hash_warning) | |
3573 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3574 | ||
3575 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3576 | } |
dbb410c3 | 3577 | |
1126897b | 3578 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3579 | gas = TRUE; |
dbb410c3 AM |
3580 | if (sec->contents == NULL) |
3581 | { | |
b34976b6 | 3582 | gas = FALSE; |
a50b1753 | 3583 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3584 | |
3585 | /* Arrange for the section to be written out. */ | |
3586 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3587 | if (sec->contents == NULL) |
3588 | { | |
b34976b6 | 3589 | *failedptr = TRUE; |
dbb410c3 AM |
3590 | return; |
3591 | } | |
3592 | } | |
3593 | ||
eea6121a | 3594 | loc = sec->contents + sec->size; |
dbb410c3 | 3595 | |
9dce4196 AM |
3596 | /* Get the pointer to the first section in the group that gas |
3597 | squirreled away here. objcopy arranges for this to be set to the | |
3598 | start of the input section group. */ | |
3599 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3600 | |
3601 | /* First element is a flag word. Rest of section is elf section | |
3602 | indices for all the sections of the group. Write them backwards | |
3603 | just to keep the group in the same order as given in .section | |
3604 | directives, not that it matters. */ | |
3605 | while (elt != NULL) | |
3606 | { | |
9dce4196 | 3607 | asection *s; |
9dce4196 | 3608 | |
9dce4196 | 3609 | s = elt; |
415f38a6 AM |
3610 | if (!gas) |
3611 | s = s->output_section; | |
3612 | if (s != NULL | |
3613 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3614 | { |
db4677b8 | 3615 | struct bfd_elf_section_data *elf_sec = elf_section_data (s); |
28e07a05 AM |
3616 | struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt); |
3617 | ||
3618 | if (elf_sec->rel.hdr != NULL | |
3619 | && (gas | |
3620 | || (input_elf_sec->rel.hdr != NULL | |
3621 | && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)) | |
db4677b8 | 3622 | { |
28e07a05 | 3623 | elf_sec->rel.hdr->sh_flags |= SHF_GROUP; |
db4677b8 AM |
3624 | loc -= 4; |
3625 | H_PUT_32 (abfd, elf_sec->rel.idx, loc); | |
3626 | } | |
28e07a05 AM |
3627 | if (elf_sec->rela.hdr != NULL |
3628 | && (gas | |
3629 | || (input_elf_sec->rela.hdr != NULL | |
3630 | && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)) | |
db4677b8 | 3631 | { |
28e07a05 | 3632 | elf_sec->rela.hdr->sh_flags |= SHF_GROUP; |
db4677b8 AM |
3633 | loc -= 4; |
3634 | H_PUT_32 (abfd, elf_sec->rela.idx, loc); | |
3635 | } | |
01e1a5bc | 3636 | loc -= 4; |
db4677b8 | 3637 | H_PUT_32 (abfd, elf_sec->this_idx, loc); |
01e1a5bc | 3638 | } |
945906ff | 3639 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3640 | if (elt == first) |
3641 | break; | |
dbb410c3 AM |
3642 | } |
3643 | ||
7bdf4127 AB |
3644 | loc -= 4; |
3645 | BFD_ASSERT (loc == sec->contents); | |
dbb410c3 | 3646 | |
9dce4196 | 3647 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3648 | } |
3649 | ||
bce964aa AM |
3650 | /* Given NAME, the name of a relocation section stripped of its |
3651 | .rel/.rela prefix, return the section in ABFD to which the | |
3652 | relocations apply. */ | |
bd53a53a L |
3653 | |
3654 | asection * | |
bce964aa AM |
3655 | _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name) |
3656 | { | |
3657 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3658 | section likely apply to .got.plt or .got section. */ | |
3659 | if (get_elf_backend_data (abfd)->want_got_plt | |
3660 | && strcmp (name, ".plt") == 0) | |
3661 | { | |
3662 | asection *sec; | |
3663 | ||
3664 | name = ".got.plt"; | |
3665 | sec = bfd_get_section_by_name (abfd, name); | |
3666 | if (sec != NULL) | |
3667 | return sec; | |
3668 | name = ".got"; | |
3669 | } | |
3670 | ||
3671 | return bfd_get_section_by_name (abfd, name); | |
3672 | } | |
3673 | ||
3674 | /* Return the section to which RELOC_SEC applies. */ | |
3675 | ||
3676 | static asection * | |
3677 | elf_get_reloc_section (asection *reloc_sec) | |
bd53a53a L |
3678 | { |
3679 | const char *name; | |
3680 | unsigned int type; | |
3681 | bfd *abfd; | |
bce964aa | 3682 | const struct elf_backend_data *bed; |
bd53a53a L |
3683 | |
3684 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3685 | if (type != SHT_REL && type != SHT_RELA) | |
3686 | return NULL; | |
3687 | ||
3688 | /* We look up the section the relocs apply to by name. */ | |
3689 | name = reloc_sec->name; | |
bce964aa AM |
3690 | if (strncmp (name, ".rel", 4) != 0) |
3691 | return NULL; | |
3692 | name += 4; | |
3693 | if (type == SHT_RELA && *name++ != 'a') | |
3694 | return NULL; | |
bd53a53a | 3695 | |
bd53a53a | 3696 | abfd = reloc_sec->owner; |
bce964aa AM |
3697 | bed = get_elf_backend_data (abfd); |
3698 | return bed->get_reloc_section (abfd, name); | |
bd53a53a L |
3699 | } |
3700 | ||
252b5132 RH |
3701 | /* Assign all ELF section numbers. The dummy first section is handled here |
3702 | too. The link/info pointers for the standard section types are filled | |
3703 | in here too, while we're at it. */ | |
3704 | ||
b34976b6 | 3705 | static bfd_boolean |
da9f89d4 | 3706 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3707 | { |
3708 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3709 | asection *sec; | |
3e19fb8f | 3710 | unsigned int section_number; |
252b5132 | 3711 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3712 | struct bfd_elf_section_data *d; |
3516e984 | 3713 | bfd_boolean need_symtab; |
446f7ed5 | 3714 | size_t amt; |
252b5132 RH |
3715 | |
3716 | section_number = 1; | |
3717 | ||
2b0f7ef9 JJ |
3718 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3719 | ||
da9f89d4 | 3720 | /* SHT_GROUP sections are in relocatable files only. */ |
7bdf4127 | 3721 | if (link_info == NULL || !link_info->resolve_section_groups) |
252b5132 | 3722 | { |
ef53be89 | 3723 | size_t reloc_count = 0; |
14f2c699 | 3724 | |
da9f89d4 | 3725 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3726 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3727 | { |
5daa8fe7 | 3728 | d = elf_section_data (sec); |
da9f89d4 L |
3729 | |
3730 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3731 | { |
5daa8fe7 | 3732 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3733 | { |
3734 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3735 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3736 | abfd->section_count--; |
da9f89d4 | 3737 | } |
08a40648 | 3738 | else |
4fbb74a6 | 3739 | d->this_idx = section_number++; |
da9f89d4 | 3740 | } |
14f2c699 L |
3741 | |
3742 | /* Count relocations. */ | |
3743 | reloc_count += sec->reloc_count; | |
47cc2cf5 | 3744 | } |
14f2c699 L |
3745 | |
3746 | /* Clear HAS_RELOC if there are no relocations. */ | |
3747 | if (reloc_count == 0) | |
3748 | abfd->flags &= ~HAS_RELOC; | |
47cc2cf5 PB |
3749 | } |
3750 | ||
3751 | for (sec = abfd->sections; sec; sec = sec->next) | |
3752 | { | |
3753 | d = elf_section_data (sec); | |
3754 | ||
3755 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3756 | d->this_idx = section_number++; |
3e19fb8f L |
3757 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3758 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3759 | if (d->rel.hdr) |
2b0f7ef9 | 3760 | { |
d4730f92 | 3761 | d->rel.idx = section_number++; |
3e19fb8f L |
3762 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3763 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3764 | } |
d4730f92 BS |
3765 | else |
3766 | d->rel.idx = 0; | |
23bc299b | 3767 | |
d4730f92 | 3768 | if (d->rela.hdr) |
2b0f7ef9 | 3769 | { |
d4730f92 | 3770 | d->rela.idx = section_number++; |
3e19fb8f L |
3771 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
3772 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3773 | } |
23bc299b | 3774 | else |
d4730f92 | 3775 | d->rela.idx = 0; |
252b5132 RH |
3776 | } |
3777 | ||
3516e984 L |
3778 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3779 | || (link_info == NULL | |
3780 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3781 | == HAS_RELOC))); | |
3782 | if (need_symtab) | |
252b5132 | 3783 | { |
12bd6957 | 3784 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3785 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3786 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3787 | { |
7a6e0d89 | 3788 | elf_section_list *entry; |
6a40cf0c NC |
3789 | |
3790 | BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); | |
3791 | ||
7a6e0d89 | 3792 | entry = bfd_zalloc (abfd, sizeof (*entry)); |
6a40cf0c NC |
3793 | entry->ndx = section_number++; |
3794 | elf_symtab_shndx_list (abfd) = entry; | |
3795 | entry->hdr.sh_name | |
9ad5cbcf | 3796 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 3797 | ".symtab_shndx", FALSE); |
6a40cf0c | 3798 | if (entry->hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3799 | return FALSE; |
9ad5cbcf | 3800 | } |
12bd6957 | 3801 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3802 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3803 | } |
3804 | ||
dd905818 NC |
3805 | elf_shstrtab_sec (abfd) = section_number++; |
3806 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); | |
3807 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); | |
3808 | ||
1c52a645 L |
3809 | if (section_number >= SHN_LORESERVE) |
3810 | { | |
695344c0 | 3811 | /* xgettext:c-format */ |
871b3ab2 | 3812 | _bfd_error_handler (_("%pB: too many sections: %u"), |
1c52a645 L |
3813 | abfd, section_number); |
3814 | return FALSE; | |
3815 | } | |
3816 | ||
9ad5cbcf | 3817 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3818 | elf_elfheader (abfd)->e_shnum = section_number; |
3819 | ||
3820 | /* Set up the list of section header pointers, in agreement with the | |
3821 | indices. */ | |
446f7ed5 AM |
3822 | amt = section_number * sizeof (Elf_Internal_Shdr *); |
3823 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt); | |
252b5132 | 3824 | if (i_shdrp == NULL) |
b34976b6 | 3825 | return FALSE; |
252b5132 | 3826 | |
a50b1753 | 3827 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
07d6d2b8 | 3828 | sizeof (Elf_Internal_Shdr)); |
252b5132 RH |
3829 | if (i_shdrp[0] == NULL) |
3830 | { | |
3831 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3832 | return FALSE; |
252b5132 | 3833 | } |
252b5132 RH |
3834 | |
3835 | elf_elfsections (abfd) = i_shdrp; | |
3836 | ||
12bd6957 | 3837 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3838 | if (need_symtab) |
252b5132 | 3839 | { |
12bd6957 | 3840 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3841 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3842 | { |
6a40cf0c NC |
3843 | elf_section_list * entry = elf_symtab_shndx_list (abfd); |
3844 | BFD_ASSERT (entry != NULL); | |
3845 | i_shdrp[entry->ndx] = & entry->hdr; | |
3846 | entry->hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3847 | } |
12bd6957 AM |
3848 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3849 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3850 | } |
38ce5b11 | 3851 | |
252b5132 RH |
3852 | for (sec = abfd->sections; sec; sec = sec->next) |
3853 | { | |
252b5132 | 3854 | asection *s; |
252b5132 | 3855 | |
91d6fa6a NC |
3856 | d = elf_section_data (sec); |
3857 | ||
252b5132 | 3858 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3859 | if (d->rel.idx != 0) |
3860 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3861 | if (d->rela.idx != 0) | |
3862 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3863 | |
3864 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3865 | ||
3866 | /* sh_link of a reloc section is the section index of the symbol | |
3867 | table. sh_info is the section index of the section to which | |
3868 | the relocation entries apply. */ | |
d4730f92 | 3869 | if (d->rel.idx != 0) |
252b5132 | 3870 | { |
12bd6957 | 3871 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3872 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3873 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3874 | } |
d4730f92 | 3875 | if (d->rela.idx != 0) |
23bc299b | 3876 | { |
12bd6957 | 3877 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3878 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3879 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3880 | } |
252b5132 | 3881 | |
38ce5b11 L |
3882 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3883 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3884 | { | |
3885 | s = elf_linked_to_section (sec); | |
3886 | if (s) | |
38ce5b11 | 3887 | { |
f2876037 | 3888 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3889 | if (link_info != NULL) |
38ce5b11 | 3890 | { |
f2876037 | 3891 | /* Check discarded linkonce section. */ |
dbaa2011 | 3892 | if (discarded_section (s)) |
38ce5b11 | 3893 | { |
ccd2ec6a | 3894 | asection *kept; |
4eca0228 | 3895 | _bfd_error_handler |
695344c0 | 3896 | /* xgettext:c-format */ |
871b3ab2 AM |
3897 | (_("%pB: sh_link of section `%pA' points to" |
3898 | " discarded section `%pA' of `%pB'"), | |
ccd2ec6a L |
3899 | abfd, d->this_hdr.bfd_section, |
3900 | s, s->owner); | |
3901 | /* Point to the kept section if it has the same | |
3902 | size as the discarded one. */ | |
c0f00686 | 3903 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3904 | if (kept == NULL) |
185d09ad | 3905 | { |
ccd2ec6a L |
3906 | bfd_set_error (bfd_error_bad_value); |
3907 | return FALSE; | |
185d09ad | 3908 | } |
ccd2ec6a | 3909 | s = kept; |
38ce5b11 | 3910 | } |
e424ecc8 | 3911 | |
ccd2ec6a L |
3912 | s = s->output_section; |
3913 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3914 | } |
f2876037 L |
3915 | else |
3916 | { | |
3917 | /* Handle objcopy. */ | |
3918 | if (s->output_section == NULL) | |
3919 | { | |
4eca0228 | 3920 | _bfd_error_handler |
695344c0 | 3921 | /* xgettext:c-format */ |
871b3ab2 AM |
3922 | (_("%pB: sh_link of section `%pA' points to" |
3923 | " removed section `%pA' of `%pB'"), | |
f2876037 L |
3924 | abfd, d->this_hdr.bfd_section, s, s->owner); |
3925 | bfd_set_error (bfd_error_bad_value); | |
3926 | return FALSE; | |
3927 | } | |
3928 | s = s->output_section; | |
3929 | } | |
ccd2ec6a L |
3930 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3931 | } | |
3932 | else | |
3933 | { | |
3934 | /* PR 290: | |
3935 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3936 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3937 | sh_info fields. Hence we could get the situation | |
08a40648 | 3938 | where s is NULL. */ |
ccd2ec6a L |
3939 | const struct elf_backend_data *bed |
3940 | = get_elf_backend_data (abfd); | |
a859124d AM |
3941 | bed->link_order_error_handler |
3942 | /* xgettext:c-format */ | |
3943 | (_("%pB: warning: sh_link not set for section `%pA'"), | |
3944 | abfd, sec); | |
38ce5b11 L |
3945 | } |
3946 | } | |
3947 | ||
252b5132 RH |
3948 | switch (d->this_hdr.sh_type) |
3949 | { | |
3950 | case SHT_REL: | |
3951 | case SHT_RELA: | |
3952 | /* A reloc section which we are treating as a normal BFD | |
3953 | section. sh_link is the section index of the symbol | |
3954 | table. sh_info is the section index of the section to | |
3955 | which the relocation entries apply. We assume that an | |
3956 | allocated reloc section uses the dynamic symbol table. | |
3957 | FIXME: How can we be sure? */ | |
3958 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3959 | if (s != NULL) | |
3960 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3961 | ||
bce964aa | 3962 | s = elf_get_reloc_section (sec); |
252b5132 | 3963 | if (s != NULL) |
9ef5d938 L |
3964 | { |
3965 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3966 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3967 | } | |
252b5132 RH |
3968 | break; |
3969 | ||
3970 | case SHT_STRTAB: | |
3971 | /* We assume that a section named .stab*str is a stabs | |
3972 | string section. We look for a section with the same name | |
3973 | but without the trailing ``str'', and set its sh_link | |
3974 | field to point to this section. */ | |
0112cd26 | 3975 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3976 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3977 | { | |
3978 | size_t len; | |
3979 | char *alc; | |
3980 | ||
3981 | len = strlen (sec->name); | |
a50b1753 | 3982 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3983 | if (alc == NULL) |
b34976b6 | 3984 | return FALSE; |
d4c88bbb | 3985 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3986 | alc[len - 3] = '\0'; |
3987 | s = bfd_get_section_by_name (abfd, alc); | |
3988 | free (alc); | |
3989 | if (s != NULL) | |
3990 | { | |
3991 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3992 | ||
3993 | /* This is a .stab section. */ | |
0594c12d AM |
3994 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3995 | elf_section_data (s)->this_hdr.sh_entsize | |
3996 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3997 | } |
3998 | } | |
3999 | break; | |
4000 | ||
4001 | case SHT_DYNAMIC: | |
4002 | case SHT_DYNSYM: | |
4003 | case SHT_GNU_verneed: | |
4004 | case SHT_GNU_verdef: | |
4005 | /* sh_link is the section header index of the string table | |
4006 | used for the dynamic entries, or the symbol table, or the | |
4007 | version strings. */ | |
4008 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
4009 | if (s != NULL) | |
4010 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
4011 | break; | |
4012 | ||
7f1204bb JJ |
4013 | case SHT_GNU_LIBLIST: |
4014 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
4015 | list used for the dynamic entries, or the symbol table, or |
4016 | the version strings. */ | |
7f1204bb JJ |
4017 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
4018 | ? ".dynstr" : ".gnu.libstr"); | |
4019 | if (s != NULL) | |
4020 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
4021 | break; | |
4022 | ||
252b5132 | 4023 | case SHT_HASH: |
fdc90cb4 | 4024 | case SHT_GNU_HASH: |
252b5132 RH |
4025 | case SHT_GNU_versym: |
4026 | /* sh_link is the section header index of the symbol table | |
4027 | this hash table or version table is for. */ | |
4028 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
4029 | if (s != NULL) | |
4030 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
4031 | break; | |
dbb410c3 AM |
4032 | |
4033 | case SHT_GROUP: | |
12bd6957 | 4034 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
4035 | } |
4036 | } | |
4037 | ||
3e19fb8f L |
4038 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
4039 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
4040 | debug section name from .debug_* to .zdebug_* if needed. */ | |
4041 | ||
b34976b6 | 4042 | return TRUE; |
252b5132 RH |
4043 | } |
4044 | ||
5372391b | 4045 | static bfd_boolean |
217aa764 | 4046 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
4047 | { |
4048 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 4049 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
4050 | if (bed->elf_backend_sym_is_global) |
4051 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 4052 | |
e47bf690 | 4053 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
e6f7f6d1 AM |
4054 | || bfd_is_und_section (bfd_asymbol_section (sym)) |
4055 | || bfd_is_com_section (bfd_asymbol_section (sym))); | |
252b5132 RH |
4056 | } |
4057 | ||
76359541 TP |
4058 | /* Filter global symbols of ABFD to include in the import library. All |
4059 | SYMCOUNT symbols of ABFD can be examined from their pointers in | |
4060 | SYMS. Pointers of symbols to keep should be stored contiguously at | |
4061 | the beginning of that array. | |
4062 | ||
4063 | Returns the number of symbols to keep. */ | |
4064 | ||
4065 | unsigned int | |
4066 | _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, | |
4067 | asymbol **syms, long symcount) | |
4068 | { | |
4069 | long src_count, dst_count = 0; | |
4070 | ||
4071 | for (src_count = 0; src_count < symcount; src_count++) | |
4072 | { | |
4073 | asymbol *sym = syms[src_count]; | |
4074 | char *name = (char *) bfd_asymbol_name (sym); | |
4075 | struct bfd_link_hash_entry *h; | |
4076 | ||
4077 | if (!sym_is_global (abfd, sym)) | |
4078 | continue; | |
4079 | ||
4080 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE); | |
5df1bc57 AM |
4081 | if (h == NULL) |
4082 | continue; | |
76359541 TP |
4083 | if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) |
4084 | continue; | |
76359541 TP |
4085 | if (h->linker_def || h->ldscript_def) |
4086 | continue; | |
4087 | ||
4088 | syms[dst_count++] = sym; | |
4089 | } | |
4090 | ||
4091 | syms[dst_count] = NULL; | |
4092 | ||
4093 | return dst_count; | |
4094 | } | |
4095 | ||
5372391b | 4096 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 4097 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
4098 | |
4099 | static bfd_boolean | |
4100 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
4101 | { | |
c6d8cab4 L |
4102 | elf_symbol_type *type_ptr; |
4103 | ||
db0c309f NC |
4104 | if (sym == NULL) |
4105 | return FALSE; | |
4106 | ||
c6d8cab4 L |
4107 | if ((sym->flags & BSF_SECTION_SYM) == 0) |
4108 | return FALSE; | |
4109 | ||
db0c309f NC |
4110 | if (sym->section == NULL) |
4111 | return TRUE; | |
4112 | ||
c6d8cab4 L |
4113 | type_ptr = elf_symbol_from (abfd, sym); |
4114 | return ((type_ptr != NULL | |
4115 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
4116 | && bfd_is_abs_section (sym->section)) | |
4117 | || !(sym->section->owner == abfd | |
db0c309f NC |
4118 | || (sym->section->output_section != NULL |
4119 | && sym->section->output_section->owner == abfd | |
2633a79c AM |
4120 | && sym->section->output_offset == 0) |
4121 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
4122 | } |
4123 | ||
2633a79c AM |
4124 | /* Map symbol from it's internal number to the external number, moving |
4125 | all local symbols to be at the head of the list. */ | |
4126 | ||
b34976b6 | 4127 | static bfd_boolean |
12bd6957 | 4128 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 4129 | { |
dc810e39 | 4130 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
4131 | asymbol **syms = bfd_get_outsymbols (abfd); |
4132 | asymbol **sect_syms; | |
dc810e39 AM |
4133 | unsigned int num_locals = 0; |
4134 | unsigned int num_globals = 0; | |
4135 | unsigned int num_locals2 = 0; | |
4136 | unsigned int num_globals2 = 0; | |
7292b3ac | 4137 | unsigned int max_index = 0; |
dc810e39 | 4138 | unsigned int idx; |
252b5132 RH |
4139 | asection *asect; |
4140 | asymbol **new_syms; | |
446f7ed5 | 4141 | size_t amt; |
252b5132 RH |
4142 | |
4143 | #ifdef DEBUG | |
4144 | fprintf (stderr, "elf_map_symbols\n"); | |
4145 | fflush (stderr); | |
4146 | #endif | |
4147 | ||
252b5132 RH |
4148 | for (asect = abfd->sections; asect; asect = asect->next) |
4149 | { | |
4150 | if (max_index < asect->index) | |
4151 | max_index = asect->index; | |
4152 | } | |
4153 | ||
4154 | max_index++; | |
446f7ed5 AM |
4155 | amt = max_index * sizeof (asymbol *); |
4156 | sect_syms = (asymbol **) bfd_zalloc (abfd, amt); | |
252b5132 | 4157 | if (sect_syms == NULL) |
b34976b6 | 4158 | return FALSE; |
252b5132 | 4159 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 4160 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 4161 | |
079e9a2f AM |
4162 | /* Init sect_syms entries for any section symbols we have already |
4163 | decided to output. */ | |
252b5132 RH |
4164 | for (idx = 0; idx < symcount; idx++) |
4165 | { | |
dc810e39 | 4166 | asymbol *sym = syms[idx]; |
c044fabd | 4167 | |
252b5132 | 4168 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 4169 | && sym->value == 0 |
2633a79c AM |
4170 | && !ignore_section_sym (abfd, sym) |
4171 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 4172 | { |
5372391b | 4173 | asection *sec = sym->section; |
252b5132 | 4174 | |
5372391b AM |
4175 | if (sec->owner != abfd) |
4176 | sec = sec->output_section; | |
252b5132 | 4177 | |
5372391b | 4178 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
4179 | } |
4180 | } | |
4181 | ||
252b5132 RH |
4182 | /* Classify all of the symbols. */ |
4183 | for (idx = 0; idx < symcount; idx++) | |
4184 | { | |
2633a79c | 4185 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 4186 | num_globals++; |
2633a79c AM |
4187 | else if (!ignore_section_sym (abfd, syms[idx])) |
4188 | num_locals++; | |
252b5132 | 4189 | } |
079e9a2f | 4190 | |
5372391b | 4191 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
4192 | sections will already have a section symbol in outsymbols, but |
4193 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
4194 | at least in that case. */ | |
252b5132 RH |
4195 | for (asect = abfd->sections; asect; asect = asect->next) |
4196 | { | |
079e9a2f | 4197 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4198 | { |
079e9a2f | 4199 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
4200 | num_locals++; |
4201 | else | |
4202 | num_globals++; | |
252b5132 RH |
4203 | } |
4204 | } | |
4205 | ||
4206 | /* Now sort the symbols so the local symbols are first. */ | |
446f7ed5 AM |
4207 | amt = (num_locals + num_globals) * sizeof (asymbol *); |
4208 | new_syms = (asymbol **) bfd_alloc (abfd, amt); | |
252b5132 | 4209 | if (new_syms == NULL) |
b34976b6 | 4210 | return FALSE; |
252b5132 RH |
4211 | |
4212 | for (idx = 0; idx < symcount; idx++) | |
4213 | { | |
4214 | asymbol *sym = syms[idx]; | |
dc810e39 | 4215 | unsigned int i; |
252b5132 | 4216 | |
2633a79c AM |
4217 | if (sym_is_global (abfd, sym)) |
4218 | i = num_locals + num_globals2++; | |
4219 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
4220 | i = num_locals2++; |
4221 | else | |
2633a79c | 4222 | continue; |
252b5132 RH |
4223 | new_syms[i] = sym; |
4224 | sym->udata.i = i + 1; | |
4225 | } | |
4226 | for (asect = abfd->sections; asect; asect = asect->next) | |
4227 | { | |
079e9a2f | 4228 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4229 | { |
079e9a2f | 4230 | asymbol *sym = asect->symbol; |
dc810e39 | 4231 | unsigned int i; |
252b5132 | 4232 | |
079e9a2f | 4233 | sect_syms[asect->index] = sym; |
252b5132 RH |
4234 | if (!sym_is_global (abfd, sym)) |
4235 | i = num_locals2++; | |
4236 | else | |
4237 | i = num_locals + num_globals2++; | |
4238 | new_syms[i] = sym; | |
4239 | sym->udata.i = i + 1; | |
4240 | } | |
4241 | } | |
4242 | ||
4243 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
4244 | ||
12bd6957 | 4245 | *pnum_locals = num_locals; |
b34976b6 | 4246 | return TRUE; |
252b5132 RH |
4247 | } |
4248 | ||
4249 | /* Align to the maximum file alignment that could be required for any | |
4250 | ELF data structure. */ | |
4251 | ||
268b6b39 | 4252 | static inline file_ptr |
217aa764 | 4253 | align_file_position (file_ptr off, int align) |
252b5132 RH |
4254 | { |
4255 | return (off + align - 1) & ~(align - 1); | |
4256 | } | |
4257 | ||
4258 | /* Assign a file position to a section, optionally aligning to the | |
4259 | required section alignment. */ | |
4260 | ||
217aa764 AM |
4261 | file_ptr |
4262 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
4263 | file_ptr offset, | |
4264 | bfd_boolean align) | |
252b5132 | 4265 | { |
72de5009 AM |
4266 | if (align && i_shdrp->sh_addralign > 1) |
4267 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
4268 | i_shdrp->sh_offset = offset; |
4269 | if (i_shdrp->bfd_section != NULL) | |
4270 | i_shdrp->bfd_section->filepos = offset; | |
4271 | if (i_shdrp->sh_type != SHT_NOBITS) | |
4272 | offset += i_shdrp->sh_size; | |
4273 | return offset; | |
4274 | } | |
4275 | ||
4276 | /* Compute the file positions we are going to put the sections at, and | |
4277 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
4278 | is not NULL, this is being called by the ELF backend linker. */ | |
4279 | ||
b34976b6 | 4280 | bfd_boolean |
217aa764 AM |
4281 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
4282 | struct bfd_link_info *link_info) | |
252b5132 | 4283 | { |
9c5bfbb7 | 4284 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 4285 | struct fake_section_arg fsargs; |
b34976b6 | 4286 | bfd_boolean failed; |
ef10c3ac | 4287 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 4288 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 4289 | bfd_boolean need_symtab; |
252b5132 RH |
4290 | |
4291 | if (abfd->output_has_begun) | |
b34976b6 | 4292 | return TRUE; |
252b5132 RH |
4293 | |
4294 | /* Do any elf backend specific processing first. */ | |
4295 | if (bed->elf_backend_begin_write_processing) | |
4296 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
4297 | ||
ed7e9d0b | 4298 | if (!(*bed->elf_backend_init_file_header) (abfd, link_info)) |
b34976b6 | 4299 | return FALSE; |
252b5132 | 4300 | |
d4730f92 BS |
4301 | fsargs.failed = FALSE; |
4302 | fsargs.link_info = link_info; | |
4303 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
4304 | if (fsargs.failed) | |
b34976b6 | 4305 | return FALSE; |
252b5132 | 4306 | |
da9f89d4 | 4307 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 4308 | return FALSE; |
252b5132 RH |
4309 | |
4310 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
4311 | need_symtab = (link_info == NULL |
4312 | && (bfd_get_symcount (abfd) > 0 | |
4313 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
4314 | == HAS_RELOC))); | |
4315 | if (need_symtab) | |
252b5132 RH |
4316 | { |
4317 | /* Non-zero if doing a relocatable link. */ | |
4318 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
4319 | ||
4320 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 4321 | return FALSE; |
252b5132 RH |
4322 | } |
4323 | ||
d4730f92 | 4324 | failed = FALSE; |
1126897b | 4325 | if (link_info == NULL) |
dbb410c3 | 4326 | { |
1126897b | 4327 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 4328 | if (failed) |
b34976b6 | 4329 | return FALSE; |
dbb410c3 AM |
4330 | } |
4331 | ||
252b5132 | 4332 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
ed7e9d0b | 4333 | /* sh_name was set in init_file_header. */ |
252b5132 | 4334 | shstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 4335 | shstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
252b5132 | 4336 | shstrtab_hdr->sh_addr = 0; |
946748d5 | 4337 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4338 | shstrtab_hdr->sh_entsize = 0; |
4339 | shstrtab_hdr->sh_link = 0; | |
4340 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 4341 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4342 | shstrtab_hdr->sh_addralign = 1; |
4343 | ||
c84fca4d | 4344 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 4345 | return FALSE; |
252b5132 | 4346 | |
3516e984 | 4347 | if (need_symtab) |
252b5132 RH |
4348 | { |
4349 | file_ptr off; | |
4350 | Elf_Internal_Shdr *hdr; | |
4351 | ||
12bd6957 | 4352 | off = elf_next_file_pos (abfd); |
252b5132 | 4353 | |
6a40cf0c | 4354 | hdr = & elf_symtab_hdr (abfd); |
b34976b6 | 4355 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4356 | |
6a40cf0c NC |
4357 | if (elf_symtab_shndx_list (abfd) != NULL) |
4358 | { | |
4359 | hdr = & elf_symtab_shndx_list (abfd)->hdr; | |
4360 | if (hdr->sh_size != 0) | |
4361 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4362 | /* FIXME: What about other symtab_shndx sections in the list ? */ | |
4363 | } | |
9ad5cbcf | 4364 | |
252b5132 | 4365 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 4366 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4367 | |
12bd6957 | 4368 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
4369 | |
4370 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 4371 | out. */ |
252b5132 | 4372 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 4373 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 4374 | return FALSE; |
ef10c3ac | 4375 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
4376 | } |
4377 | ||
b34976b6 | 4378 | abfd->output_has_begun = TRUE; |
252b5132 | 4379 | |
b34976b6 | 4380 | return TRUE; |
252b5132 RH |
4381 | } |
4382 | ||
8ded5a0f AM |
4383 | /* Make an initial estimate of the size of the program header. If we |
4384 | get the number wrong here, we'll redo section placement. */ | |
4385 | ||
4386 | static bfd_size_type | |
4387 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
4388 | { | |
4389 | size_t segs; | |
4390 | asection *s; | |
2b05f1b7 | 4391 | const struct elf_backend_data *bed; |
8ded5a0f AM |
4392 | |
4393 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
4394 | and one for data. */ | |
4395 | segs = 2; | |
4396 | ||
4397 | s = bfd_get_section_by_name (abfd, ".interp"); | |
1b9e270b | 4398 | if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) |
8ded5a0f AM |
4399 | { |
4400 | /* If we have a loadable interpreter section, we need a | |
4401 | PT_INTERP segment. In this case, assume we also need a | |
4402 | PT_PHDR segment, although that may not be true for all | |
4403 | targets. */ | |
e9a38e0f | 4404 | segs += 2; |
8ded5a0f AM |
4405 | } |
4406 | ||
4407 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
4408 | { | |
4409 | /* We need a PT_DYNAMIC segment. */ | |
4410 | ++segs; | |
f210dcff | 4411 | } |
08a40648 | 4412 | |
ceae84aa | 4413 | if (info != NULL && info->relro) |
f210dcff L |
4414 | { |
4415 | /* We need a PT_GNU_RELRO segment. */ | |
4416 | ++segs; | |
8ded5a0f AM |
4417 | } |
4418 | ||
12bd6957 | 4419 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
4420 | { |
4421 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4422 | ++segs; | |
4423 | } | |
4424 | ||
12bd6957 | 4425 | if (elf_stack_flags (abfd)) |
8ded5a0f | 4426 | { |
2b05f1b7 L |
4427 | /* We need a PT_GNU_STACK segment. */ |
4428 | ++segs; | |
4429 | } | |
94b11780 | 4430 | |
0a59decb L |
4431 | s = bfd_get_section_by_name (abfd, |
4432 | NOTE_GNU_PROPERTY_SECTION_NAME); | |
4433 | if (s != NULL && s->size != 0) | |
4434 | { | |
4435 | /* We need a PT_GNU_PROPERTY segment. */ | |
4436 | ++segs; | |
4437 | } | |
4438 | ||
2b05f1b7 L |
4439 | for (s = abfd->sections; s != NULL; s = s->next) |
4440 | { | |
8ded5a0f | 4441 | if ((s->flags & SEC_LOAD) != 0 |
23e463ed | 4442 | && elf_section_type (s) == SHT_NOTE) |
8ded5a0f | 4443 | { |
23e463ed | 4444 | unsigned int alignment_power; |
8ded5a0f AM |
4445 | /* We need a PT_NOTE segment. */ |
4446 | ++segs; | |
23e463ed L |
4447 | /* Try to create just one PT_NOTE segment for all adjacent |
4448 | loadable SHT_NOTE sections. gABI requires that within a | |
4449 | PT_NOTE segment (and also inside of each SHT_NOTE section) | |
4450 | each note should have the same alignment. So we check | |
4451 | whether the sections are correctly aligned. */ | |
4452 | alignment_power = s->alignment_power; | |
4453 | while (s->next != NULL | |
4454 | && s->next->alignment_power == alignment_power | |
4455 | && (s->next->flags & SEC_LOAD) != 0 | |
4456 | && elf_section_type (s->next) == SHT_NOTE) | |
4457 | s = s->next; | |
8ded5a0f AM |
4458 | } |
4459 | } | |
4460 | ||
4461 | for (s = abfd->sections; s != NULL; s = s->next) | |
4462 | { | |
4463 | if (s->flags & SEC_THREAD_LOCAL) | |
4464 | { | |
4465 | /* We need a PT_TLS segment. */ | |
4466 | ++segs; | |
4467 | break; | |
4468 | } | |
4469 | } | |
4470 | ||
2b05f1b7 | 4471 | bed = get_elf_backend_data (abfd); |
a91e1603 | 4472 | |
df3a023b AM |
4473 | if ((abfd->flags & D_PAGED) != 0 |
4474 | && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) | |
4475 | { | |
4476 | /* Add a PT_GNU_MBIND segment for each mbind section. */ | |
4477 | unsigned int page_align_power = bfd_log2 (bed->commonpagesize); | |
4478 | for (s = abfd->sections; s != NULL; s = s->next) | |
4479 | if (elf_section_flags (s) & SHF_GNU_MBIND) | |
4480 | { | |
4481 | if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM) | |
4482 | { | |
4483 | _bfd_error_handler | |
4484 | /* xgettext:c-format */ | |
4485 | (_("%pB: GNU_MBIND section `%pA' has invalid " | |
4486 | "sh_info field: %d"), | |
4487 | abfd, s, elf_section_data (s)->this_hdr.sh_info); | |
4488 | continue; | |
4489 | } | |
4490 | /* Align mbind section to page size. */ | |
4491 | if (s->alignment_power < page_align_power) | |
4492 | s->alignment_power = page_align_power; | |
4493 | segs ++; | |
4494 | } | |
4495 | } | |
4496 | ||
4497 | /* Let the backend count up any program headers it might need. */ | |
4498 | if (bed->elf_backend_additional_program_headers) | |
8ded5a0f AM |
4499 | { |
4500 | int a; | |
4501 | ||
4502 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
4503 | if (a == -1) | |
4504 | abort (); | |
4505 | segs += a; | |
4506 | } | |
4507 | ||
4508 | return segs * bed->s->sizeof_phdr; | |
4509 | } | |
4510 | ||
2ea37f1c NC |
4511 | /* Find the segment that contains the output_section of section. */ |
4512 | ||
4513 | Elf_Internal_Phdr * | |
4514 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
4515 | { | |
4516 | struct elf_segment_map *m; | |
4517 | Elf_Internal_Phdr *p; | |
4518 | ||
12bd6957 | 4519 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
4520 | m != NULL; |
4521 | m = m->next, p++) | |
4522 | { | |
4523 | int i; | |
4524 | ||
4525 | for (i = m->count - 1; i >= 0; i--) | |
4526 | if (m->sections[i] == section) | |
4527 | return p; | |
4528 | } | |
4529 | ||
4530 | return NULL; | |
4531 | } | |
4532 | ||
252b5132 RH |
4533 | /* Create a mapping from a set of sections to a program segment. */ |
4534 | ||
217aa764 AM |
4535 | static struct elf_segment_map * |
4536 | make_mapping (bfd *abfd, | |
4537 | asection **sections, | |
4538 | unsigned int from, | |
4539 | unsigned int to, | |
4540 | bfd_boolean phdr) | |
252b5132 RH |
4541 | { |
4542 | struct elf_segment_map *m; | |
4543 | unsigned int i; | |
4544 | asection **hdrpp; | |
986f0783 | 4545 | size_t amt; |
252b5132 | 4546 | |
00bee008 AM |
4547 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
4548 | amt += (to - from) * sizeof (asection *); | |
a50b1753 | 4549 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4550 | if (m == NULL) |
4551 | return NULL; | |
4552 | m->next = NULL; | |
4553 | m->p_type = PT_LOAD; | |
4554 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
4555 | m->sections[i - from] = *hdrpp; | |
4556 | m->count = to - from; | |
4557 | ||
4558 | if (from == 0 && phdr) | |
4559 | { | |
4560 | /* Include the headers in the first PT_LOAD segment. */ | |
4561 | m->includes_filehdr = 1; | |
4562 | m->includes_phdrs = 1; | |
4563 | } | |
4564 | ||
4565 | return m; | |
4566 | } | |
4567 | ||
229fcec5 MM |
4568 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
4569 | on failure. */ | |
4570 | ||
4571 | struct elf_segment_map * | |
4572 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
4573 | { | |
4574 | struct elf_segment_map *m; | |
4575 | ||
a50b1753 | 4576 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
07d6d2b8 | 4577 | sizeof (struct elf_segment_map)); |
229fcec5 MM |
4578 | if (m == NULL) |
4579 | return NULL; | |
4580 | m->next = NULL; | |
4581 | m->p_type = PT_DYNAMIC; | |
4582 | m->count = 1; | |
4583 | m->sections[0] = dynsec; | |
08a40648 | 4584 | |
229fcec5 MM |
4585 | return m; |
4586 | } | |
4587 | ||
8ded5a0f | 4588 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 4589 | |
b34976b6 | 4590 | static bfd_boolean |
3dea8fca AM |
4591 | elf_modify_segment_map (bfd *abfd, |
4592 | struct bfd_link_info *info, | |
4593 | bfd_boolean remove_empty_load) | |
252b5132 | 4594 | { |
252e386e | 4595 | struct elf_segment_map **m; |
8ded5a0f | 4596 | const struct elf_backend_data *bed; |
252b5132 | 4597 | |
8ded5a0f AM |
4598 | /* The placement algorithm assumes that non allocated sections are |
4599 | not in PT_LOAD segments. We ensure this here by removing such | |
4600 | sections from the segment map. We also remove excluded | |
252e386e AM |
4601 | sections. Finally, any PT_LOAD segment without sections is |
4602 | removed. */ | |
12bd6957 | 4603 | m = &elf_seg_map (abfd); |
252e386e | 4604 | while (*m) |
8ded5a0f AM |
4605 | { |
4606 | unsigned int i, new_count; | |
252b5132 | 4607 | |
252e386e | 4608 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4609 | { |
252e386e AM |
4610 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4611 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4612 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4613 | { |
252e386e AM |
4614 | (*m)->sections[new_count] = (*m)->sections[i]; |
4615 | new_count++; | |
8ded5a0f AM |
4616 | } |
4617 | } | |
252e386e | 4618 | (*m)->count = new_count; |
252b5132 | 4619 | |
1a9ccd70 NC |
4620 | if (remove_empty_load |
4621 | && (*m)->p_type == PT_LOAD | |
4622 | && (*m)->count == 0 | |
4623 | && !(*m)->includes_phdrs) | |
252e386e AM |
4624 | *m = (*m)->next; |
4625 | else | |
4626 | m = &(*m)->next; | |
8ded5a0f | 4627 | } |
252b5132 | 4628 | |
8ded5a0f AM |
4629 | bed = get_elf_backend_data (abfd); |
4630 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4631 | { |
252e386e | 4632 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4633 | return FALSE; |
252b5132 | 4634 | } |
252b5132 | 4635 | |
8ded5a0f AM |
4636 | return TRUE; |
4637 | } | |
252b5132 | 4638 | |
dbc88fc1 AM |
4639 | #define IS_TBSS(s) \ |
4640 | ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL) | |
4641 | ||
8ded5a0f | 4642 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4643 | |
8ded5a0f AM |
4644 | bfd_boolean |
4645 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4646 | { | |
4647 | unsigned int count; | |
4648 | struct elf_segment_map *m; | |
4649 | asection **sections = NULL; | |
4650 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4651 | bfd_boolean no_user_phdrs; |
252b5132 | 4652 | |
12bd6957 | 4653 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4654 | |
4655 | if (info != NULL) | |
4656 | info->user_phdrs = !no_user_phdrs; | |
4657 | ||
3dea8fca | 4658 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4659 | { |
8ded5a0f AM |
4660 | asection *s; |
4661 | unsigned int i; | |
4662 | struct elf_segment_map *mfirst; | |
4663 | struct elf_segment_map **pm; | |
4664 | asection *last_hdr; | |
4665 | bfd_vma last_size; | |
00bee008 | 4666 | unsigned int hdr_index; |
8ded5a0f AM |
4667 | bfd_vma maxpagesize; |
4668 | asection **hdrpp; | |
64029e93 | 4669 | bfd_boolean phdr_in_segment; |
8ded5a0f | 4670 | bfd_boolean writable; |
2888249f | 4671 | bfd_boolean executable; |
446f7ed5 | 4672 | unsigned int tls_count = 0; |
8ded5a0f | 4673 | asection *first_tls = NULL; |
a91e1603 | 4674 | asection *first_mbind = NULL; |
8ded5a0f | 4675 | asection *dynsec, *eh_frame_hdr; |
446f7ed5 | 4676 | size_t amt; |
66631823 CE |
4677 | bfd_vma addr_mask, wrap_to = 0; /* Bytes. */ |
4678 | bfd_size_type phdr_size; /* Octets/bytes. */ | |
502794d4 | 4679 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
252b5132 | 4680 | |
8ded5a0f | 4681 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4682 | |
446f7ed5 AM |
4683 | amt = bfd_count_sections (abfd) * sizeof (asection *); |
4684 | sections = (asection **) bfd_malloc (amt); | |
8ded5a0f | 4685 | if (sections == NULL) |
252b5132 | 4686 | goto error_return; |
252b5132 | 4687 | |
8d06853e AM |
4688 | /* Calculate top address, avoiding undefined behaviour of shift |
4689 | left operator when shift count is equal to size of type | |
4690 | being shifted. */ | |
4691 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4692 | addr_mask = (addr_mask << 1) + 1; | |
4693 | ||
8ded5a0f AM |
4694 | i = 0; |
4695 | for (s = abfd->sections; s != NULL; s = s->next) | |
4696 | { | |
4697 | if ((s->flags & SEC_ALLOC) != 0) | |
4698 | { | |
48db3297 AM |
4699 | /* target_index is unused until bfd_elf_final_link |
4700 | starts output of section symbols. Use it to make | |
4701 | qsort stable. */ | |
4702 | s->target_index = i; | |
8ded5a0f AM |
4703 | sections[i] = s; |
4704 | ++i; | |
8d06853e | 4705 | /* A wrapping section potentially clashes with header. */ |
66631823 CE |
4706 | if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask)) |
4707 | wrap_to = (s->lma + s->size / opb) & addr_mask; | |
8ded5a0f AM |
4708 | } |
4709 | } | |
4710 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4711 | count = i; | |
252b5132 | 4712 | |
8ded5a0f | 4713 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4714 | |
64029e93 AM |
4715 | phdr_size = elf_program_header_size (abfd); |
4716 | if (phdr_size == (bfd_size_type) -1) | |
4717 | phdr_size = get_program_header_size (abfd, info); | |
4718 | phdr_size += bed->s->sizeof_ehdr; | |
502794d4 CE |
4719 | /* phdr_size is compared to LMA values which are in bytes. */ |
4720 | phdr_size /= opb; | |
64029e93 AM |
4721 | maxpagesize = bed->maxpagesize; |
4722 | if (maxpagesize == 0) | |
4723 | maxpagesize = 1; | |
4724 | phdr_in_segment = info != NULL && info->load_phdrs; | |
4725 | if (count != 0 | |
4726 | && (((sections[0]->lma & addr_mask) & (maxpagesize - 1)) | |
4727 | >= (phdr_size & (maxpagesize - 1)))) | |
4728 | /* For compatibility with old scripts that may not be using | |
4729 | SIZEOF_HEADERS, add headers when it looks like space has | |
4730 | been left for them. */ | |
4731 | phdr_in_segment = TRUE; | |
252b5132 | 4732 | |
64029e93 | 4733 | /* Build the mapping. */ |
8ded5a0f AM |
4734 | mfirst = NULL; |
4735 | pm = &mfirst; | |
252b5132 | 4736 | |
8ded5a0f AM |
4737 | /* If we have a .interp section, then create a PT_PHDR segment for |
4738 | the program headers and a PT_INTERP segment for the .interp | |
4739 | section. */ | |
4740 | s = bfd_get_section_by_name (abfd, ".interp"); | |
1b9e270b | 4741 | if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) |
8ded5a0f AM |
4742 | { |
4743 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4744 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4745 | if (m == NULL) |
4746 | goto error_return; | |
4747 | m->next = NULL; | |
4748 | m->p_type = PT_PHDR; | |
f882209d | 4749 | m->p_flags = PF_R; |
8ded5a0f AM |
4750 | m->p_flags_valid = 1; |
4751 | m->includes_phdrs = 1; | |
64029e93 | 4752 | phdr_in_segment = TRUE; |
8ded5a0f AM |
4753 | *pm = m; |
4754 | pm = &m->next; | |
252b5132 | 4755 | |
8ded5a0f | 4756 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4757 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4758 | if (m == NULL) |
4759 | goto error_return; | |
4760 | m->next = NULL; | |
4761 | m->p_type = PT_INTERP; | |
4762 | m->count = 1; | |
4763 | m->sections[0] = s; | |
4764 | ||
4765 | *pm = m; | |
4766 | pm = &m->next; | |
252b5132 | 4767 | } |
8ded5a0f AM |
4768 | |
4769 | /* Look through the sections. We put sections in the same program | |
4770 | segment when the start of the second section can be placed within | |
4771 | a few bytes of the end of the first section. */ | |
4772 | last_hdr = NULL; | |
4773 | last_size = 0; | |
00bee008 | 4774 | hdr_index = 0; |
8ded5a0f | 4775 | writable = FALSE; |
2888249f | 4776 | executable = FALSE; |
8ded5a0f AM |
4777 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); |
4778 | if (dynsec != NULL | |
4779 | && (dynsec->flags & SEC_LOAD) == 0) | |
4780 | dynsec = NULL; | |
4781 | ||
64029e93 AM |
4782 | if ((abfd->flags & D_PAGED) == 0) |
4783 | phdr_in_segment = FALSE; | |
4784 | ||
8ded5a0f AM |
4785 | /* Deal with -Ttext or something similar such that the first section |
4786 | is not adjacent to the program headers. This is an | |
4787 | approximation, since at this point we don't know exactly how many | |
4788 | program headers we will need. */ | |
64029e93 | 4789 | if (phdr_in_segment && count > 0) |
252b5132 | 4790 | { |
66631823 | 4791 | bfd_vma phdr_lma; /* Bytes. */ |
64029e93 AM |
4792 | bfd_boolean separate_phdr = FALSE; |
4793 | ||
4794 | phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize; | |
4795 | if (info != NULL | |
4796 | && info->separate_code | |
4797 | && (sections[0]->flags & SEC_CODE) != 0) | |
1a9ccd70 | 4798 | { |
64029e93 AM |
4799 | /* If data sections should be separate from code and |
4800 | thus not executable, and the first section is | |
4801 | executable then put the file and program headers in | |
4802 | their own PT_LOAD. */ | |
4803 | separate_phdr = TRUE; | |
4804 | if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize) | |
4805 | == (sections[0]->lma & addr_mask & -maxpagesize))) | |
4806 | { | |
4807 | /* The file and program headers are currently on the | |
4808 | same page as the first section. Put them on the | |
4809 | previous page if we can. */ | |
4810 | if (phdr_lma >= maxpagesize) | |
4811 | phdr_lma -= maxpagesize; | |
4812 | else | |
4813 | separate_phdr = FALSE; | |
4814 | } | |
4815 | } | |
4816 | if ((sections[0]->lma & addr_mask) < phdr_lma | |
4817 | || (sections[0]->lma & addr_mask) < phdr_size) | |
4818 | /* If file and program headers would be placed at the end | |
4819 | of memory then it's probably better to omit them. */ | |
4820 | phdr_in_segment = FALSE; | |
4821 | else if (phdr_lma < wrap_to) | |
4822 | /* If a section wraps around to where we'll be placing | |
4823 | file and program headers, then the headers will be | |
4824 | overwritten. */ | |
4825 | phdr_in_segment = FALSE; | |
4826 | else if (separate_phdr) | |
4827 | { | |
4828 | m = make_mapping (abfd, sections, 0, 0, phdr_in_segment); | |
4829 | if (m == NULL) | |
4830 | goto error_return; | |
66631823 | 4831 | m->p_paddr = phdr_lma * opb; |
64029e93 AM |
4832 | m->p_vaddr_offset |
4833 | = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize; | |
4834 | m->p_paddr_valid = 1; | |
4835 | *pm = m; | |
4836 | pm = &m->next; | |
4837 | phdr_in_segment = FALSE; | |
1a9ccd70 | 4838 | } |
252b5132 RH |
4839 | } |
4840 | ||
8ded5a0f | 4841 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4842 | { |
8ded5a0f AM |
4843 | asection *hdr; |
4844 | bfd_boolean new_segment; | |
4845 | ||
4846 | hdr = *hdrpp; | |
4847 | ||
4848 | /* See if this section and the last one will fit in the same | |
4849 | segment. */ | |
4850 | ||
4851 | if (last_hdr == NULL) | |
4852 | { | |
4853 | /* If we don't have a segment yet, then we don't need a new | |
4854 | one (we build the last one after this loop). */ | |
4855 | new_segment = FALSE; | |
4856 | } | |
4857 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4858 | { | |
4859 | /* If this section has a different relation between the | |
4860 | virtual address and the load address, then we need a new | |
4861 | segment. */ | |
4862 | new_segment = TRUE; | |
4863 | } | |
b5599592 AM |
4864 | else if (hdr->lma < last_hdr->lma + last_size |
4865 | || last_hdr->lma + last_size < last_hdr->lma) | |
4866 | { | |
4867 | /* If this section has a load address that makes it overlap | |
4868 | the previous section, then we need a new segment. */ | |
4869 | new_segment = TRUE; | |
4870 | } | |
76cb3a89 AM |
4871 | else if ((abfd->flags & D_PAGED) != 0 |
4872 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4873 | == (hdr->lma & -maxpagesize))) | |
4874 | { | |
4875 | /* If we are demand paged then we can't map two disk | |
4876 | pages onto the same memory page. */ | |
4877 | new_segment = FALSE; | |
4878 | } | |
39948a60 NC |
4879 | /* In the next test we have to be careful when last_hdr->lma is close |
4880 | to the end of the address space. If the aligned address wraps | |
4881 | around to the start of the address space, then there are no more | |
4882 | pages left in memory and it is OK to assume that the current | |
4883 | section can be included in the current segment. */ | |
76cb3a89 AM |
4884 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) |
4885 | + maxpagesize > last_hdr->lma) | |
4886 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) | |
4887 | + maxpagesize <= hdr->lma)) | |
8ded5a0f AM |
4888 | { |
4889 | /* If putting this section in this segment would force us to | |
4890 | skip a page in the segment, then we need a new segment. */ | |
4891 | new_segment = TRUE; | |
4892 | } | |
4893 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
76cb3a89 | 4894 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) |
8ded5a0f | 4895 | { |
e5654c0f AM |
4896 | /* We don't want to put a loaded section after a |
4897 | nonloaded (ie. bss style) section in the same segment | |
4898 | as that will force the non-loaded section to be loaded. | |
76cb3a89 | 4899 | Consider .tbss sections as loaded for this purpose. */ |
8ded5a0f AM |
4900 | new_segment = TRUE; |
4901 | } | |
4902 | else if ((abfd->flags & D_PAGED) == 0) | |
4903 | { | |
4904 | /* If the file is not demand paged, which means that we | |
4905 | don't require the sections to be correctly aligned in the | |
4906 | file, then there is no other reason for a new segment. */ | |
4907 | new_segment = FALSE; | |
4908 | } | |
2888249f L |
4909 | else if (info != NULL |
4910 | && info->separate_code | |
4911 | && executable != ((hdr->flags & SEC_CODE) != 0)) | |
4912 | { | |
4913 | new_segment = TRUE; | |
4914 | } | |
8ded5a0f | 4915 | else if (! writable |
76cb3a89 | 4916 | && (hdr->flags & SEC_READONLY) == 0) |
8ded5a0f AM |
4917 | { |
4918 | /* We don't want to put a writable section in a read only | |
76cb3a89 | 4919 | segment. */ |
8ded5a0f AM |
4920 | new_segment = TRUE; |
4921 | } | |
4922 | else | |
4923 | { | |
4924 | /* Otherwise, we can use the same segment. */ | |
4925 | new_segment = FALSE; | |
4926 | } | |
4927 | ||
2889e75b | 4928 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4929 | if (last_hdr != NULL |
4930 | && info != NULL | |
4931 | && info->callbacks->override_segment_assignment != NULL) | |
4932 | new_segment | |
4933 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4934 | last_hdr, | |
4935 | new_segment); | |
2889e75b | 4936 | |
8ded5a0f AM |
4937 | if (! new_segment) |
4938 | { | |
4939 | if ((hdr->flags & SEC_READONLY) == 0) | |
4940 | writable = TRUE; | |
2888249f L |
4941 | if ((hdr->flags & SEC_CODE) != 0) |
4942 | executable = TRUE; | |
8ded5a0f AM |
4943 | last_hdr = hdr; |
4944 | /* .tbss sections effectively have zero size. */ | |
502794d4 | 4945 | last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; |
8ded5a0f AM |
4946 | continue; |
4947 | } | |
4948 | ||
4949 | /* We need a new program segment. We must create a new program | |
00bee008 | 4950 | header holding all the sections from hdr_index until hdr. */ |
8ded5a0f | 4951 | |
00bee008 | 4952 | m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); |
8ded5a0f AM |
4953 | if (m == NULL) |
4954 | goto error_return; | |
4955 | ||
4956 | *pm = m; | |
4957 | pm = &m->next; | |
4958 | ||
252b5132 | 4959 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4960 | writable = TRUE; |
8ded5a0f AM |
4961 | else |
4962 | writable = FALSE; | |
4963 | ||
2888249f L |
4964 | if ((hdr->flags & SEC_CODE) == 0) |
4965 | executable = FALSE; | |
4966 | else | |
4967 | executable = TRUE; | |
4968 | ||
baaff79e JJ |
4969 | last_hdr = hdr; |
4970 | /* .tbss sections effectively have zero size. */ | |
502794d4 | 4971 | last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; |
00bee008 | 4972 | hdr_index = i; |
8ded5a0f | 4973 | phdr_in_segment = FALSE; |
252b5132 RH |
4974 | } |
4975 | ||
86b2281f AM |
4976 | /* Create a final PT_LOAD program segment, but not if it's just |
4977 | for .tbss. */ | |
4978 | if (last_hdr != NULL | |
00bee008 | 4979 | && (i - hdr_index != 1 |
dbc88fc1 | 4980 | || !IS_TBSS (last_hdr))) |
8ded5a0f | 4981 | { |
00bee008 | 4982 | m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); |
8ded5a0f AM |
4983 | if (m == NULL) |
4984 | goto error_return; | |
252b5132 | 4985 | |
8ded5a0f AM |
4986 | *pm = m; |
4987 | pm = &m->next; | |
4988 | } | |
252b5132 | 4989 | |
8ded5a0f AM |
4990 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4991 | if (dynsec != NULL) | |
4992 | { | |
4993 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4994 | if (m == NULL) | |
4995 | goto error_return; | |
4996 | *pm = m; | |
4997 | pm = &m->next; | |
4998 | } | |
252b5132 | 4999 | |
23e463ed | 5000 | /* For each batch of consecutive loadable SHT_NOTE sections, |
1c5265b5 JJ |
5001 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, |
5002 | because if we link together nonloadable .note sections and | |
5003 | loadable .note sections, we will generate two .note sections | |
23e463ed | 5004 | in the output file. */ |
8ded5a0f AM |
5005 | for (s = abfd->sections; s != NULL; s = s->next) |
5006 | { | |
5007 | if ((s->flags & SEC_LOAD) != 0 | |
23e463ed | 5008 | && elf_section_type (s) == SHT_NOTE) |
8ded5a0f | 5009 | { |
1c5265b5 | 5010 | asection *s2; |
23e463ed | 5011 | unsigned int alignment_power = s->alignment_power; |
91d6fa6a NC |
5012 | |
5013 | count = 1; | |
23e463ed L |
5014 | for (s2 = s; s2->next != NULL; s2 = s2->next) |
5015 | { | |
5016 | if (s2->next->alignment_power == alignment_power | |
5017 | && (s2->next->flags & SEC_LOAD) != 0 | |
5018 | && elf_section_type (s2->next) == SHT_NOTE | |
66631823 | 5019 | && align_power (s2->lma + s2->size / opb, |
23e463ed L |
5020 | alignment_power) |
5021 | == s2->next->lma) | |
5022 | count++; | |
5023 | else | |
5024 | break; | |
5025 | } | |
00bee008 AM |
5026 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
5027 | amt += count * sizeof (asection *); | |
a50b1753 | 5028 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
5029 | if (m == NULL) |
5030 | goto error_return; | |
5031 | m->next = NULL; | |
5032 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
5033 | m->count = count; |
5034 | while (count > 1) | |
5035 | { | |
5036 | m->sections[m->count - count--] = s; | |
5037 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
5038 | s = s->next; | |
5039 | } | |
5040 | m->sections[m->count - 1] = s; | |
5041 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
5042 | *pm = m; |
5043 | pm = &m->next; | |
5044 | } | |
5045 | if (s->flags & SEC_THREAD_LOCAL) | |
5046 | { | |
5047 | if (! tls_count) | |
5048 | first_tls = s; | |
5049 | tls_count++; | |
5050 | } | |
a91e1603 L |
5051 | if (first_mbind == NULL |
5052 | && (elf_section_flags (s) & SHF_GNU_MBIND) != 0) | |
5053 | first_mbind = s; | |
8ded5a0f | 5054 | } |
252b5132 | 5055 | |
8ded5a0f AM |
5056 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
5057 | if (tls_count > 0) | |
5058 | { | |
00bee008 AM |
5059 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
5060 | amt += tls_count * sizeof (asection *); | |
a50b1753 | 5061 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
5062 | if (m == NULL) |
5063 | goto error_return; | |
5064 | m->next = NULL; | |
5065 | m->p_type = PT_TLS; | |
5066 | m->count = tls_count; | |
5067 | /* Mandated PF_R. */ | |
5068 | m->p_flags = PF_R; | |
5069 | m->p_flags_valid = 1; | |
d923cae0 | 5070 | s = first_tls; |
446f7ed5 | 5071 | for (i = 0; i < tls_count; ++i) |
8ded5a0f | 5072 | { |
d923cae0 L |
5073 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
5074 | { | |
5075 | _bfd_error_handler | |
871b3ab2 | 5076 | (_("%pB: TLS sections are not adjacent:"), abfd); |
d923cae0 L |
5077 | s = first_tls; |
5078 | i = 0; | |
446f7ed5 | 5079 | while (i < tls_count) |
d923cae0 L |
5080 | { |
5081 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
5082 | { | |
871b3ab2 | 5083 | _bfd_error_handler (_(" TLS: %pA"), s); |
d923cae0 L |
5084 | i++; |
5085 | } | |
5086 | else | |
871b3ab2 | 5087 | _bfd_error_handler (_(" non-TLS: %pA"), s); |
d923cae0 L |
5088 | s = s->next; |
5089 | } | |
5090 | bfd_set_error (bfd_error_bad_value); | |
5091 | goto error_return; | |
5092 | } | |
5093 | m->sections[i] = s; | |
5094 | s = s->next; | |
8ded5a0f | 5095 | } |
252b5132 | 5096 | |
8ded5a0f AM |
5097 | *pm = m; |
5098 | pm = &m->next; | |
5099 | } | |
252b5132 | 5100 | |
df3a023b AM |
5101 | if (first_mbind |
5102 | && (abfd->flags & D_PAGED) != 0 | |
5103 | && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) | |
a91e1603 L |
5104 | for (s = first_mbind; s != NULL; s = s->next) |
5105 | if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0 | |
df3a023b | 5106 | && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM) |
a91e1603 L |
5107 | { |
5108 | /* Mandated PF_R. */ | |
5109 | unsigned long p_flags = PF_R; | |
5110 | if ((s->flags & SEC_READONLY) == 0) | |
5111 | p_flags |= PF_W; | |
5112 | if ((s->flags & SEC_CODE) != 0) | |
5113 | p_flags |= PF_X; | |
5114 | ||
5115 | amt = sizeof (struct elf_segment_map) + sizeof (asection *); | |
5116 | m = bfd_zalloc (abfd, amt); | |
5117 | if (m == NULL) | |
5118 | goto error_return; | |
5119 | m->next = NULL; | |
5120 | m->p_type = (PT_GNU_MBIND_LO | |
5121 | + elf_section_data (s)->this_hdr.sh_info); | |
5122 | m->count = 1; | |
5123 | m->p_flags_valid = 1; | |
5124 | m->sections[0] = s; | |
5125 | m->p_flags = p_flags; | |
5126 | ||
5127 | *pm = m; | |
5128 | pm = &m->next; | |
5129 | } | |
5130 | ||
0a59decb L |
5131 | s = bfd_get_section_by_name (abfd, |
5132 | NOTE_GNU_PROPERTY_SECTION_NAME); | |
5133 | if (s != NULL && s->size != 0) | |
5134 | { | |
5135 | amt = sizeof (struct elf_segment_map) + sizeof (asection *); | |
5136 | m = bfd_zalloc (abfd, amt); | |
5137 | if (m == NULL) | |
5138 | goto error_return; | |
5139 | m->next = NULL; | |
5140 | m->p_type = PT_GNU_PROPERTY; | |
5141 | m->count = 1; | |
5142 | m->p_flags_valid = 1; | |
5143 | m->sections[0] = s; | |
5144 | m->p_flags = PF_R; | |
5145 | *pm = m; | |
5146 | pm = &m->next; | |
5147 | } | |
5148 | ||
8ded5a0f AM |
5149 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
5150 | segment. */ | |
12bd6957 | 5151 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
5152 | if (eh_frame_hdr != NULL |
5153 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 5154 | { |
dc810e39 | 5155 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 5156 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
5157 | if (m == NULL) |
5158 | goto error_return; | |
5159 | m->next = NULL; | |
8ded5a0f | 5160 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 5161 | m->count = 1; |
8ded5a0f | 5162 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
5163 | |
5164 | *pm = m; | |
5165 | pm = &m->next; | |
5166 | } | |
13ae64f3 | 5167 | |
12bd6957 | 5168 | if (elf_stack_flags (abfd)) |
13ae64f3 | 5169 | { |
8ded5a0f | 5170 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 5171 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
5172 | if (m == NULL) |
5173 | goto error_return; | |
5174 | m->next = NULL; | |
2b05f1b7 | 5175 | m->p_type = PT_GNU_STACK; |
12bd6957 | 5176 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 5177 | m->p_align = bed->stack_align; |
8ded5a0f | 5178 | m->p_flags_valid = 1; |
04c3a755 NS |
5179 | m->p_align_valid = m->p_align != 0; |
5180 | if (info->stacksize > 0) | |
5181 | { | |
5182 | m->p_size = info->stacksize; | |
5183 | m->p_size_valid = 1; | |
5184 | } | |
252b5132 | 5185 | |
8ded5a0f AM |
5186 | *pm = m; |
5187 | pm = &m->next; | |
5188 | } | |
65765700 | 5189 | |
ceae84aa | 5190 | if (info != NULL && info->relro) |
8ded5a0f | 5191 | { |
f210dcff L |
5192 | for (m = mfirst; m != NULL; m = m->next) |
5193 | { | |
3832a4d8 AM |
5194 | if (m->p_type == PT_LOAD |
5195 | && m->count != 0 | |
5196 | && m->sections[0]->vma >= info->relro_start | |
5197 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 5198 | { |
3832a4d8 AM |
5199 | i = m->count; |
5200 | while (--i != (unsigned) -1) | |
5201 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5202 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5203 | break; | |
5204 | ||
43a8475c | 5205 | if (i != (unsigned) -1) |
f210dcff L |
5206 | break; |
5207 | } | |
be01b344 | 5208 | } |
f210dcff L |
5209 | |
5210 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
5211 | if (m != NULL) | |
5212 | { | |
5213 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 5214 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
5215 | if (m == NULL) |
5216 | goto error_return; | |
5217 | m->next = NULL; | |
5218 | m->p_type = PT_GNU_RELRO; | |
f210dcff L |
5219 | *pm = m; |
5220 | pm = &m->next; | |
5221 | } | |
8ded5a0f | 5222 | } |
9ee5e499 | 5223 | |
8ded5a0f | 5224 | free (sections); |
12bd6957 | 5225 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
5226 | } |
5227 | ||
3dea8fca | 5228 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 5229 | return FALSE; |
8c37241b | 5230 | |
12bd6957 | 5231 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 5232 | ++count; |
12bd6957 | 5233 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 5234 | |
b34976b6 | 5235 | return TRUE; |
252b5132 RH |
5236 | |
5237 | error_return: | |
5238 | if (sections != NULL) | |
5239 | free (sections); | |
b34976b6 | 5240 | return FALSE; |
252b5132 RH |
5241 | } |
5242 | ||
5243 | /* Sort sections by address. */ | |
5244 | ||
5245 | static int | |
217aa764 | 5246 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
5247 | { |
5248 | const asection *sec1 = *(const asection **) arg1; | |
5249 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 5250 | bfd_size_type size1, size2; |
252b5132 RH |
5251 | |
5252 | /* Sort by LMA first, since this is the address used to | |
5253 | place the section into a segment. */ | |
5254 | if (sec1->lma < sec2->lma) | |
5255 | return -1; | |
5256 | else if (sec1->lma > sec2->lma) | |
5257 | return 1; | |
5258 | ||
5259 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
5260 | the same, and this will do nothing. */ | |
5261 | if (sec1->vma < sec2->vma) | |
5262 | return -1; | |
5263 | else if (sec1->vma > sec2->vma) | |
5264 | return 1; | |
5265 | ||
5266 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
5267 | ||
07c6e936 | 5268 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
5269 | |
5270 | if (TOEND (sec1)) | |
5271 | { | |
48db3297 | 5272 | if (!TOEND (sec2)) |
252b5132 RH |
5273 | return 1; |
5274 | } | |
00a7cdc5 | 5275 | else if (TOEND (sec2)) |
252b5132 RH |
5276 | return -1; |
5277 | ||
5278 | #undef TOEND | |
5279 | ||
00a7cdc5 NC |
5280 | /* Sort by size, to put zero sized sections |
5281 | before others at the same address. */ | |
252b5132 | 5282 | |
eea6121a AM |
5283 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
5284 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
5285 | |
5286 | if (size1 < size2) | |
252b5132 | 5287 | return -1; |
eecdbe52 | 5288 | if (size1 > size2) |
252b5132 RH |
5289 | return 1; |
5290 | ||
5291 | return sec1->target_index - sec2->target_index; | |
5292 | } | |
5293 | ||
30fe1832 AM |
5294 | /* This qsort comparison functions sorts PT_LOAD segments first and |
5295 | by p_paddr, for assign_file_positions_for_load_sections. */ | |
5296 | ||
5297 | static int | |
5298 | elf_sort_segments (const void *arg1, const void *arg2) | |
5299 | { | |
5300 | const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1; | |
5301 | const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2; | |
5302 | ||
5303 | if (m1->p_type != m2->p_type) | |
5304 | { | |
5305 | if (m1->p_type == PT_NULL) | |
5306 | return 1; | |
5307 | if (m2->p_type == PT_NULL) | |
5308 | return -1; | |
5309 | return m1->p_type < m2->p_type ? -1 : 1; | |
5310 | } | |
5311 | if (m1->includes_filehdr != m2->includes_filehdr) | |
5312 | return m1->includes_filehdr ? -1 : 1; | |
5313 | if (m1->no_sort_lma != m2->no_sort_lma) | |
5314 | return m1->no_sort_lma ? -1 : 1; | |
5315 | if (m1->p_type == PT_LOAD && !m1->no_sort_lma) | |
5316 | { | |
4b3ecb3b | 5317 | bfd_vma lma1, lma2; /* Octets. */ |
30fe1832 AM |
5318 | lma1 = 0; |
5319 | if (m1->p_paddr_valid) | |
4b3ecb3b | 5320 | lma1 = m1->p_paddr; |
30fe1832 | 5321 | else if (m1->count != 0) |
4b3ecb3b AM |
5322 | { |
5323 | unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner, | |
5324 | m1->sections[0]); | |
5325 | lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb; | |
5326 | } | |
30fe1832 AM |
5327 | lma2 = 0; |
5328 | if (m2->p_paddr_valid) | |
4b3ecb3b | 5329 | lma2 = m2->p_paddr; |
30fe1832 | 5330 | else if (m2->count != 0) |
4b3ecb3b AM |
5331 | { |
5332 | unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner, | |
5333 | m2->sections[0]); | |
5334 | lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb; | |
5335 | } | |
30fe1832 AM |
5336 | if (lma1 != lma2) |
5337 | return lma1 < lma2 ? -1 : 1; | |
5338 | } | |
5339 | if (m1->idx != m2->idx) | |
5340 | return m1->idx < m2->idx ? -1 : 1; | |
5341 | return 0; | |
5342 | } | |
5343 | ||
340b6d91 AC |
5344 | /* Ian Lance Taylor writes: |
5345 | ||
5346 | We shouldn't be using % with a negative signed number. That's just | |
5347 | not good. We have to make sure either that the number is not | |
5348 | negative, or that the number has an unsigned type. When the types | |
5349 | are all the same size they wind up as unsigned. When file_ptr is a | |
5350 | larger signed type, the arithmetic winds up as signed long long, | |
5351 | which is wrong. | |
5352 | ||
5353 | What we're trying to say here is something like ``increase OFF by | |
5354 | the least amount that will cause it to be equal to the VMA modulo | |
5355 | the page size.'' */ | |
5356 | /* In other words, something like: | |
5357 | ||
5358 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
5359 | off_offset = off % bed->maxpagesize; | |
5360 | if (vma_offset < off_offset) | |
5361 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
5362 | else | |
5363 | adjustment = vma_offset - off_offset; | |
08a40648 | 5364 | |
de194d85 | 5365 | which can be collapsed into the expression below. */ |
340b6d91 AC |
5366 | |
5367 | static file_ptr | |
5368 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
5369 | { | |
dc9155b2 NC |
5370 | /* PR binutils/16199: Handle an alignment of zero. */ |
5371 | if (maxpagesize == 0) | |
5372 | maxpagesize = 1; | |
340b6d91 AC |
5373 | return ((vma - off) % maxpagesize); |
5374 | } | |
5375 | ||
6d33f217 L |
5376 | static void |
5377 | print_segment_map (const struct elf_segment_map *m) | |
5378 | { | |
5379 | unsigned int j; | |
5380 | const char *pt = get_segment_type (m->p_type); | |
5381 | char buf[32]; | |
5382 | ||
5383 | if (pt == NULL) | |
5384 | { | |
5385 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
5386 | sprintf (buf, "LOPROC+%7.7x", | |
5387 | (unsigned int) (m->p_type - PT_LOPROC)); | |
5388 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
5389 | sprintf (buf, "LOOS+%7.7x", | |
5390 | (unsigned int) (m->p_type - PT_LOOS)); | |
5391 | else | |
5392 | snprintf (buf, sizeof (buf), "%8.8x", | |
5393 | (unsigned int) m->p_type); | |
5394 | pt = buf; | |
5395 | } | |
4a97a0e5 | 5396 | fflush (stdout); |
6d33f217 L |
5397 | fprintf (stderr, "%s:", pt); |
5398 | for (j = 0; j < m->count; j++) | |
5399 | fprintf (stderr, " %s", m->sections [j]->name); | |
5400 | putc ('\n',stderr); | |
4a97a0e5 | 5401 | fflush (stderr); |
6d33f217 L |
5402 | } |
5403 | ||
32812159 AM |
5404 | static bfd_boolean |
5405 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
5406 | { | |
5407 | void *buf; | |
5408 | bfd_boolean ret; | |
5409 | ||
5410 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
5411 | return FALSE; | |
5412 | buf = bfd_zmalloc (len); | |
5413 | if (buf == NULL) | |
5414 | return FALSE; | |
5415 | ret = bfd_bwrite (buf, len, abfd) == len; | |
5416 | free (buf); | |
5417 | return ret; | |
5418 | } | |
5419 | ||
252b5132 RH |
5420 | /* Assign file positions to the sections based on the mapping from |
5421 | sections to segments. This function also sets up some fields in | |
f3520d2f | 5422 | the file header. */ |
252b5132 | 5423 | |
b34976b6 | 5424 | static bfd_boolean |
f3520d2f AM |
5425 | assign_file_positions_for_load_sections (bfd *abfd, |
5426 | struct bfd_link_info *link_info) | |
252b5132 RH |
5427 | { |
5428 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 5429 | struct elf_segment_map *m; |
30fe1832 | 5430 | struct elf_segment_map *phdr_load_seg; |
252b5132 | 5431 | Elf_Internal_Phdr *phdrs; |
252b5132 | 5432 | Elf_Internal_Phdr *p; |
502794d4 | 5433 | file_ptr off; /* Octets. */ |
3f570048 | 5434 | bfd_size_type maxpagesize; |
30fe1832 | 5435 | unsigned int alloc, actual; |
0920dee7 | 5436 | unsigned int i, j; |
30fe1832 | 5437 | struct elf_segment_map **sorted_seg_map; |
502794d4 | 5438 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
252b5132 | 5439 | |
e36284ab | 5440 | if (link_info == NULL |
ceae84aa | 5441 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 5442 | return FALSE; |
252b5132 | 5443 | |
8ded5a0f | 5444 | alloc = 0; |
12bd6957 | 5445 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
30fe1832 | 5446 | m->idx = alloc++; |
252b5132 | 5447 | |
82f2dbf7 NC |
5448 | if (alloc) |
5449 | { | |
5450 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
5451 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
5452 | } | |
5453 | else | |
5454 | { | |
5455 | /* PR binutils/12467. */ | |
5456 | elf_elfheader (abfd)->e_phoff = 0; | |
5457 | elf_elfheader (abfd)->e_phentsize = 0; | |
5458 | } | |
d324f6d6 | 5459 | |
8ded5a0f | 5460 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 5461 | |
12bd6957 | 5462 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
30fe1832 AM |
5463 | { |
5464 | actual = alloc; | |
5465 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
5466 | } | |
8ded5a0f | 5467 | else |
30fe1832 AM |
5468 | { |
5469 | actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr; | |
5470 | BFD_ASSERT (elf_program_header_size (abfd) | |
5471 | == actual * bed->s->sizeof_phdr); | |
5472 | BFD_ASSERT (actual >= alloc); | |
5473 | } | |
252b5132 RH |
5474 | |
5475 | if (alloc == 0) | |
f3520d2f | 5476 | { |
12bd6957 | 5477 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 5478 | return TRUE; |
f3520d2f | 5479 | } |
252b5132 | 5480 | |
12bd6957 | 5481 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
5482 | see assign_file_positions_except_relocs, so make sure we have |
5483 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
5484 | The variable alloc contains the computed need, while |
5485 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
5486 | layout. |
5487 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
5488 | where the layout is forced to according to a larger size in the | |
5489 | last iterations for the testcase ld-elf/header. */ | |
30fe1832 AM |
5490 | phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs) |
5491 | + alloc * sizeof (*sorted_seg_map))); | |
5492 | sorted_seg_map = (struct elf_segment_map **) (phdrs + actual); | |
f3520d2f | 5493 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 5494 | if (phdrs == NULL) |
b34976b6 | 5495 | return FALSE; |
252b5132 | 5496 | |
30fe1832 | 5497 | for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++) |
252b5132 | 5498 | { |
30fe1832 | 5499 | sorted_seg_map[j] = m; |
252b5132 | 5500 | /* If elf_segment_map is not from map_sections_to_segments, the |
08a40648 | 5501 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
5502 | not be done to the PT_NOTE section of a corefile, which may |
5503 | contain several pseudo-sections artificially created by bfd. | |
5504 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
5505 | if (m->count > 1 |
5506 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 5507 | && m->p_type == PT_NOTE)) |
48db3297 AM |
5508 | { |
5509 | for (i = 0; i < m->count; i++) | |
5510 | m->sections[i]->target_index = i; | |
5511 | qsort (m->sections, (size_t) m->count, sizeof (asection *), | |
5512 | elf_sort_sections); | |
5513 | } | |
30fe1832 AM |
5514 | } |
5515 | if (alloc > 1) | |
5516 | qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map), | |
5517 | elf_sort_segments); | |
5518 | ||
5519 | maxpagesize = 1; | |
5520 | if ((abfd->flags & D_PAGED) != 0) | |
5521 | maxpagesize = bed->maxpagesize; | |
5522 | ||
5523 | /* Sections must map to file offsets past the ELF file header. */ | |
5524 | off = bed->s->sizeof_ehdr; | |
5525 | /* And if one of the PT_LOAD headers doesn't include the program | |
5526 | headers then we'll be mapping program headers in the usual | |
5527 | position after the ELF file header. */ | |
5528 | phdr_load_seg = NULL; | |
5529 | for (j = 0; j < alloc; j++) | |
5530 | { | |
5531 | m = sorted_seg_map[j]; | |
5532 | if (m->p_type != PT_LOAD) | |
5533 | break; | |
5534 | if (m->includes_phdrs) | |
5535 | { | |
5536 | phdr_load_seg = m; | |
5537 | break; | |
5538 | } | |
5539 | } | |
5540 | if (phdr_load_seg == NULL) | |
5541 | off += actual * bed->s->sizeof_phdr; | |
5542 | ||
5543 | for (j = 0; j < alloc; j++) | |
5544 | { | |
5545 | asection **secpp; | |
502794d4 | 5546 | bfd_vma off_adjust; /* Octets. */ |
30fe1832 | 5547 | bfd_boolean no_contents; |
252b5132 | 5548 | |
b301b248 AM |
5549 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
5550 | number of sections with contents contributing to both p_filesz | |
5551 | and p_memsz, followed by a number of sections with no contents | |
5552 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 5553 | available file offset for PT_LOAD and PT_NOTE segments. */ |
30fe1832 AM |
5554 | m = sorted_seg_map[j]; |
5555 | p = phdrs + m->idx; | |
252b5132 | 5556 | p->p_type = m->p_type; |
28a7f3e7 | 5557 | p->p_flags = m->p_flags; |
252b5132 | 5558 | |
3f570048 | 5559 | if (m->count == 0) |
502794d4 | 5560 | p->p_vaddr = m->p_vaddr_offset * opb; |
3f570048 | 5561 | else |
502794d4 | 5562 | p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb; |
3f570048 AM |
5563 | |
5564 | if (m->p_paddr_valid) | |
5565 | p->p_paddr = m->p_paddr; | |
5566 | else if (m->count == 0) | |
5567 | p->p_paddr = 0; | |
5568 | else | |
502794d4 | 5569 | p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb; |
3f570048 AM |
5570 | |
5571 | if (p->p_type == PT_LOAD | |
5572 | && (abfd->flags & D_PAGED) != 0) | |
5573 | { | |
5574 | /* p_align in demand paged PT_LOAD segments effectively stores | |
5575 | the maximum page size. When copying an executable with | |
5576 | objcopy, we set m->p_align from the input file. Use this | |
5577 | value for maxpagesize rather than bed->maxpagesize, which | |
5578 | may be different. Note that we use maxpagesize for PT_TLS | |
5579 | segment alignment later in this function, so we are relying | |
5580 | on at least one PT_LOAD segment appearing before a PT_TLS | |
5581 | segment. */ | |
5582 | if (m->p_align_valid) | |
5583 | maxpagesize = m->p_align; | |
5584 | ||
5585 | p->p_align = maxpagesize; | |
5586 | } | |
3271a814 NS |
5587 | else if (m->p_align_valid) |
5588 | p->p_align = m->p_align; | |
e970b90a DJ |
5589 | else if (m->count == 0) |
5590 | p->p_align = 1 << bed->s->log_file_align; | |
30fe1832 AM |
5591 | |
5592 | if (m == phdr_load_seg) | |
5593 | { | |
5594 | if (!m->includes_filehdr) | |
5595 | p->p_offset = off; | |
5596 | off += actual * bed->s->sizeof_phdr; | |
5597 | } | |
3f570048 | 5598 | |
bf988460 AM |
5599 | no_contents = FALSE; |
5600 | off_adjust = 0; | |
252b5132 | 5601 | if (p->p_type == PT_LOAD |
b301b248 | 5602 | && m->count > 0) |
252b5132 | 5603 | { |
66631823 | 5604 | bfd_size_type align; /* Bytes. */ |
a49e53ed | 5605 | unsigned int align_power = 0; |
b301b248 | 5606 | |
3271a814 NS |
5607 | if (m->p_align_valid) |
5608 | align = p->p_align; | |
5609 | else | |
252b5132 | 5610 | { |
3271a814 NS |
5611 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5612 | { | |
5613 | unsigned int secalign; | |
08a40648 | 5614 | |
fd361982 | 5615 | secalign = bfd_section_alignment (*secpp); |
3271a814 NS |
5616 | if (secalign > align_power) |
5617 | align_power = secalign; | |
5618 | } | |
5619 | align = (bfd_size_type) 1 << align_power; | |
5620 | if (align < maxpagesize) | |
5621 | align = maxpagesize; | |
b301b248 | 5622 | } |
252b5132 | 5623 | |
02bf8d82 AM |
5624 | for (i = 0; i < m->count; i++) |
5625 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
5626 | /* If we aren't making room for this section, then | |
5627 | it must be SHT_NOBITS regardless of what we've | |
5628 | set via struct bfd_elf_special_section. */ | |
5629 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
5630 | ||
bf988460 | 5631 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
5632 | sections. */ |
5633 | no_contents = TRUE; | |
5634 | for (i = 0; i < m->count; i++) | |
5635 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
5636 | { | |
5637 | no_contents = FALSE; | |
5638 | break; | |
5639 | } | |
bf988460 | 5640 | |
66631823 | 5641 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb); |
a8c75b76 AM |
5642 | |
5643 | /* Broken hardware and/or kernel require that files do not | |
5644 | map the same page with different permissions on some hppa | |
5645 | processors. */ | |
30fe1832 AM |
5646 | if (j != 0 |
5647 | && (abfd->flags & D_PAGED) != 0 | |
a8c75b76 AM |
5648 | && bed->no_page_alias |
5649 | && (off & (maxpagesize - 1)) != 0 | |
502794d4 CE |
5650 | && ((off & -maxpagesize) |
5651 | == ((off + off_adjust) & -maxpagesize))) | |
a8c75b76 | 5652 | off_adjust += maxpagesize; |
bf988460 AM |
5653 | off += off_adjust; |
5654 | if (no_contents) | |
5655 | { | |
5656 | /* We shouldn't need to align the segment on disk since | |
5657 | the segment doesn't need file space, but the gABI | |
5658 | arguably requires the alignment and glibc ld.so | |
5659 | checks it. So to comply with the alignment | |
5660 | requirement but not waste file space, we adjust | |
5661 | p_offset for just this segment. (OFF_ADJUST is | |
5662 | subtracted from OFF later.) This may put p_offset | |
5663 | past the end of file, but that shouldn't matter. */ | |
5664 | } | |
5665 | else | |
5666 | off_adjust = 0; | |
252b5132 | 5667 | } |
b1a6d0b1 NC |
5668 | /* Make sure the .dynamic section is the first section in the |
5669 | PT_DYNAMIC segment. */ | |
5670 | else if (p->p_type == PT_DYNAMIC | |
5671 | && m->count > 1 | |
5672 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
5673 | { | |
5674 | _bfd_error_handler | |
871b3ab2 | 5675 | (_("%pB: The first section in the PT_DYNAMIC segment" |
63a5468a | 5676 | " is not the .dynamic section"), |
b301b248 | 5677 | abfd); |
b1a6d0b1 NC |
5678 | bfd_set_error (bfd_error_bad_value); |
5679 | return FALSE; | |
5680 | } | |
3f001e84 JK |
5681 | /* Set the note section type to SHT_NOTE. */ |
5682 | else if (p->p_type == PT_NOTE) | |
5683 | for (i = 0; i < m->count; i++) | |
5684 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 5685 | |
252b5132 RH |
5686 | if (m->includes_filehdr) |
5687 | { | |
bf988460 | 5688 | if (!m->p_flags_valid) |
252b5132 | 5689 | p->p_flags |= PF_R; |
252b5132 RH |
5690 | p->p_filesz = bed->s->sizeof_ehdr; |
5691 | p->p_memsz = bed->s->sizeof_ehdr; | |
30fe1832 | 5692 | if (p->p_type == PT_LOAD) |
252b5132 | 5693 | { |
30fe1832 | 5694 | if (m->count > 0) |
252b5132 | 5695 | { |
30fe1832 AM |
5696 | if (p->p_vaddr < (bfd_vma) off |
5697 | || (!m->p_paddr_valid | |
5698 | && p->p_paddr < (bfd_vma) off)) | |
5699 | { | |
5700 | _bfd_error_handler | |
5701 | (_("%pB: not enough room for program headers," | |
5702 | " try linking with -N"), | |
5703 | abfd); | |
5704 | bfd_set_error (bfd_error_bad_value); | |
5705 | return FALSE; | |
5706 | } | |
5707 | p->p_vaddr -= off; | |
5708 | if (!m->p_paddr_valid) | |
5709 | p->p_paddr -= off; | |
252b5132 | 5710 | } |
30fe1832 AM |
5711 | } |
5712 | else if (sorted_seg_map[0]->includes_filehdr) | |
5713 | { | |
5714 | Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx; | |
5715 | p->p_vaddr = filehdr->p_vaddr; | |
bf988460 | 5716 | if (!m->p_paddr_valid) |
30fe1832 | 5717 | p->p_paddr = filehdr->p_paddr; |
252b5132 | 5718 | } |
252b5132 RH |
5719 | } |
5720 | ||
5721 | if (m->includes_phdrs) | |
5722 | { | |
bf988460 | 5723 | if (!m->p_flags_valid) |
252b5132 | 5724 | p->p_flags |= PF_R; |
30fe1832 AM |
5725 | p->p_filesz += actual * bed->s->sizeof_phdr; |
5726 | p->p_memsz += actual * bed->s->sizeof_phdr; | |
f3520d2f | 5727 | if (!m->includes_filehdr) |
252b5132 | 5728 | { |
30fe1832 | 5729 | if (p->p_type == PT_LOAD) |
252b5132 | 5730 | { |
30fe1832 AM |
5731 | elf_elfheader (abfd)->e_phoff = p->p_offset; |
5732 | if (m->count > 0) | |
5733 | { | |
5734 | p->p_vaddr -= off - p->p_offset; | |
5735 | if (!m->p_paddr_valid) | |
5736 | p->p_paddr -= off - p->p_offset; | |
5737 | } | |
5738 | } | |
5739 | else if (phdr_load_seg != NULL) | |
5740 | { | |
5741 | Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx; | |
502794d4 | 5742 | bfd_vma phdr_off = 0; /* Octets. */ |
30fe1832 AM |
5743 | if (phdr_load_seg->includes_filehdr) |
5744 | phdr_off = bed->s->sizeof_ehdr; | |
5745 | p->p_vaddr = phdr->p_vaddr + phdr_off; | |
bf988460 | 5746 | if (!m->p_paddr_valid) |
30fe1832 AM |
5747 | p->p_paddr = phdr->p_paddr + phdr_off; |
5748 | p->p_offset = phdr->p_offset + phdr_off; | |
252b5132 | 5749 | } |
30fe1832 AM |
5750 | else |
5751 | p->p_offset = bed->s->sizeof_ehdr; | |
2b0bc088 | 5752 | } |
252b5132 RH |
5753 | } |
5754 | ||
5755 | if (p->p_type == PT_LOAD | |
5756 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
5757 | { | |
bf988460 | 5758 | if (!m->includes_filehdr && !m->includes_phdrs) |
0bc3450e AM |
5759 | { |
5760 | p->p_offset = off; | |
5761 | if (no_contents) | |
67641dd3 AM |
5762 | { |
5763 | /* Put meaningless p_offset for PT_LOAD segments | |
5764 | without file contents somewhere within the first | |
5765 | page, in an attempt to not point past EOF. */ | |
5766 | bfd_size_type align = maxpagesize; | |
5767 | if (align < p->p_align) | |
5768 | align = p->p_align; | |
5769 | if (align < 1) | |
5770 | align = 1; | |
5771 | p->p_offset = off % align; | |
5772 | } | |
0bc3450e | 5773 | } |
252b5132 RH |
5774 | else |
5775 | { | |
502794d4 | 5776 | file_ptr adjust; /* Octets. */ |
252b5132 RH |
5777 | |
5778 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
5779 | if (!no_contents) |
5780 | p->p_filesz += adjust; | |
252b5132 RH |
5781 | p->p_memsz += adjust; |
5782 | } | |
5783 | } | |
5784 | ||
1ea63fd2 AM |
5785 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
5786 | maps. Set filepos for sections in PT_LOAD segments, and in | |
5787 | core files, for sections in PT_NOTE segments. | |
5788 | assign_file_positions_for_non_load_sections will set filepos | |
5789 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
5790 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5791 | { | |
5792 | asection *sec; | |
252b5132 | 5793 | bfd_size_type align; |
627b32bc | 5794 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
5795 | |
5796 | sec = *secpp; | |
02bf8d82 | 5797 | this_hdr = &elf_section_data (sec)->this_hdr; |
fd361982 | 5798 | align = (bfd_size_type) 1 << bfd_section_alignment (sec); |
252b5132 | 5799 | |
88967714 AM |
5800 | if ((p->p_type == PT_LOAD |
5801 | || p->p_type == PT_TLS) | |
5802 | && (this_hdr->sh_type != SHT_NOBITS | |
5803 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
5804 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
5805 | || p->p_type == PT_TLS)))) | |
252b5132 | 5806 | { |
502794d4 CE |
5807 | bfd_vma p_start = p->p_paddr; /* Octets. */ |
5808 | bfd_vma p_end = p_start + p->p_memsz; /* Octets. */ | |
5809 | bfd_vma s_start = sec->lma * opb; /* Octets. */ | |
5810 | bfd_vma adjust = s_start - p_end; /* Octets. */ | |
252b5132 | 5811 | |
a2d1e028 L |
5812 | if (adjust != 0 |
5813 | && (s_start < p_end | |
5814 | || p_end < p_start)) | |
252b5132 | 5815 | { |
4eca0228 | 5816 | _bfd_error_handler |
695344c0 | 5817 | /* xgettext:c-format */ |
2dcf00ce | 5818 | (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64), |
502794d4 CE |
5819 | abfd, sec, (uint64_t) s_start / opb, |
5820 | (uint64_t) p_end / opb); | |
88967714 | 5821 | adjust = 0; |
502794d4 | 5822 | sec->lma = p_end / opb; |
1cfb7d1e | 5823 | } |
3ac9b6c9 | 5824 | p->p_memsz += adjust; |
1cfb7d1e | 5825 | |
88967714 AM |
5826 | if (this_hdr->sh_type != SHT_NOBITS) |
5827 | { | |
30fe1832 | 5828 | if (p->p_type == PT_LOAD) |
32812159 | 5829 | { |
30fe1832 AM |
5830 | if (p->p_filesz + adjust < p->p_memsz) |
5831 | { | |
5832 | /* We have a PROGBITS section following NOBITS ones. | |
5833 | Allocate file space for the NOBITS section(s) and | |
5834 | zero it. */ | |
5835 | adjust = p->p_memsz - p->p_filesz; | |
5836 | if (!write_zeros (abfd, off, adjust)) | |
5837 | return FALSE; | |
5838 | } | |
5839 | off += adjust; | |
32812159 | 5840 | } |
88967714 | 5841 | p->p_filesz += adjust; |
252b5132 | 5842 | } |
252b5132 RH |
5843 | } |
5844 | ||
5845 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5846 | { | |
b301b248 AM |
5847 | /* The section at i == 0 is the one that actually contains |
5848 | everything. */ | |
4a938328 MS |
5849 | if (i == 0) |
5850 | { | |
627b32bc | 5851 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5852 | off += this_hdr->sh_size; |
5853 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5854 | p->p_memsz = 0; |
5855 | p->p_align = 1; | |
252b5132 | 5856 | } |
4a938328 | 5857 | else |
252b5132 | 5858 | { |
b301b248 | 5859 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5860 | sec->filepos = 0; |
eea6121a | 5861 | sec->size = 0; |
b301b248 AM |
5862 | sec->flags = 0; |
5863 | continue; | |
252b5132 | 5864 | } |
252b5132 RH |
5865 | } |
5866 | else | |
5867 | { | |
1e951488 | 5868 | if (p->p_type == PT_LOAD) |
b301b248 | 5869 | { |
1e951488 AM |
5870 | this_hdr->sh_offset = sec->filepos = off; |
5871 | if (this_hdr->sh_type != SHT_NOBITS) | |
5872 | off += this_hdr->sh_size; | |
5873 | } | |
5874 | else if (this_hdr->sh_type == SHT_NOBITS | |
5875 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5876 | && this_hdr->sh_offset == 0) | |
5877 | { | |
5878 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5879 | (See _bfd_elf_map_sections_to_segments "Create a | |
5880 | final PT_LOAD".) Set sh_offset to the value it | |
5881 | would have if we had created a zero p_filesz and | |
5882 | p_memsz PT_LOAD header for the section. This | |
5883 | also makes the PT_TLS header have the same | |
5884 | p_offset value. */ | |
5885 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5886 | off, align); | |
5887 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5888 | } |
252b5132 | 5889 | |
02bf8d82 | 5890 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5891 | { |
6a3cd2b4 | 5892 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5893 | /* A load section without SHF_ALLOC is something like |
5894 | a note section in a PT_NOTE segment. These take | |
5895 | file space but are not loaded into memory. */ | |
5896 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5897 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5898 | } |
6a3cd2b4 | 5899 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5900 | { |
6a3cd2b4 AM |
5901 | if (p->p_type == PT_TLS) |
5902 | p->p_memsz += this_hdr->sh_size; | |
5903 | ||
5904 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5905 | normal segments. */ | |
5906 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5907 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5908 | } |
5909 | ||
b10a8ae0 L |
5910 | if (align > p->p_align |
5911 | && !m->p_align_valid | |
5912 | && (p->p_type != PT_LOAD | |
5913 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5914 | p->p_align = align; |
5915 | } | |
5916 | ||
bf988460 | 5917 | if (!m->p_flags_valid) |
252b5132 RH |
5918 | { |
5919 | p->p_flags |= PF_R; | |
02bf8d82 | 5920 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5921 | p->p_flags |= PF_X; |
02bf8d82 | 5922 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5923 | p->p_flags |= PF_W; |
5924 | } | |
5925 | } | |
43a8475c | 5926 | |
bf988460 | 5927 | off -= off_adjust; |
0920dee7 | 5928 | |
30fe1832 AM |
5929 | /* PR ld/20815 - Check that the program header segment, if |
5930 | present, will be loaded into memory. */ | |
5931 | if (p->p_type == PT_PHDR | |
5932 | && phdr_load_seg == NULL | |
5933 | && !(bed->elf_backend_allow_non_load_phdr != NULL | |
5934 | && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc))) | |
5935 | { | |
5936 | /* The fix for this error is usually to edit the linker script being | |
5937 | used and set up the program headers manually. Either that or | |
5938 | leave room for the headers at the start of the SECTIONS. */ | |
5939 | _bfd_error_handler (_("%pB: error: PHDR segment not covered" | |
5940 | " by LOAD segment"), | |
5941 | abfd); | |
7b3c2715 AM |
5942 | if (link_info == NULL) |
5943 | return FALSE; | |
5944 | /* Arrange for the linker to exit with an error, deleting | |
5945 | the output file unless --noinhibit-exec is given. */ | |
5946 | link_info->callbacks->info ("%X"); | |
30fe1832 AM |
5947 | } |
5948 | ||
7c928300 AM |
5949 | /* Check that all sections are in a PT_LOAD segment. |
5950 | Don't check funky gdb generated core files. */ | |
5951 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5952 | { |
5953 | bfd_boolean check_vma = TRUE; | |
5954 | ||
5955 | for (i = 1; i < m->count; i++) | |
5956 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5957 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5958 | ->this_hdr), p) != 0 | |
5959 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5960 | ->this_hdr), p) != 0) | |
0920dee7 | 5961 | { |
9a83a553 AM |
5962 | /* Looks like we have overlays packed into the segment. */ |
5963 | check_vma = FALSE; | |
5964 | break; | |
0920dee7 | 5965 | } |
9a83a553 AM |
5966 | |
5967 | for (i = 0; i < m->count; i++) | |
5968 | { | |
5969 | Elf_Internal_Shdr *this_hdr; | |
5970 | asection *sec; | |
5971 | ||
5972 | sec = m->sections[i]; | |
5973 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5974 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5975 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 | 5976 | { |
4eca0228 | 5977 | _bfd_error_handler |
695344c0 | 5978 | /* xgettext:c-format */ |
871b3ab2 | 5979 | (_("%pB: section `%pA' can't be allocated in segment %d"), |
9a83a553 AM |
5980 | abfd, sec, j); |
5981 | print_segment_map (m); | |
5982 | } | |
5983 | } | |
5984 | } | |
252b5132 RH |
5985 | } |
5986 | ||
12bd6957 | 5987 | elf_next_file_pos (abfd) = off; |
30fe1832 AM |
5988 | |
5989 | if (link_info != NULL | |
5990 | && phdr_load_seg != NULL | |
5991 | && phdr_load_seg->includes_filehdr) | |
5992 | { | |
5993 | /* There is a segment that contains both the file headers and the | |
5994 | program headers, so provide a symbol __ehdr_start pointing there. | |
5995 | A program can use this to examine itself robustly. */ | |
5996 | ||
5997 | struct elf_link_hash_entry *hash | |
5998 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5999 | FALSE, FALSE, TRUE); | |
6000 | /* If the symbol was referenced and not defined, define it. */ | |
6001 | if (hash != NULL | |
6002 | && (hash->root.type == bfd_link_hash_new | |
6003 | || hash->root.type == bfd_link_hash_undefined | |
6004 | || hash->root.type == bfd_link_hash_undefweak | |
6005 | || hash->root.type == bfd_link_hash_common)) | |
6006 | { | |
6007 | asection *s = NULL; | |
66631823 | 6008 | bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb; |
30fe1832 AM |
6009 | |
6010 | if (phdr_load_seg->count != 0) | |
6011 | /* The segment contains sections, so use the first one. */ | |
6012 | s = phdr_load_seg->sections[0]; | |
6013 | else | |
6014 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
6015 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) | |
6016 | if (m->p_type == PT_LOAD && m->count != 0) | |
6017 | { | |
6018 | s = m->sections[0]; | |
6019 | break; | |
6020 | } | |
6021 | ||
6022 | if (s != NULL) | |
6023 | { | |
6024 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
6025 | hash->root.u.def.section = s; | |
6026 | } | |
6027 | else | |
6028 | { | |
6029 | hash->root.u.def.value = filehdr_vaddr; | |
6030 | hash->root.u.def.section = bfd_abs_section_ptr; | |
6031 | } | |
6032 | ||
6033 | hash->root.type = bfd_link_hash_defined; | |
6034 | hash->def_regular = 1; | |
6035 | hash->non_elf = 0; | |
6036 | } | |
6037 | } | |
6038 | ||
f3520d2f AM |
6039 | return TRUE; |
6040 | } | |
6041 | ||
1faa385f NC |
6042 | /* Determine if a bfd is a debuginfo file. Unfortunately there |
6043 | is no defined method for detecting such files, so we have to | |
6044 | use heuristics instead. */ | |
6045 | ||
6046 | bfd_boolean | |
6047 | is_debuginfo_file (bfd *abfd) | |
6048 | { | |
6049 | if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
6050 | return FALSE; | |
6051 | ||
6052 | Elf_Internal_Shdr **start_headers = elf_elfsections (abfd); | |
6053 | Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd); | |
6054 | Elf_Internal_Shdr **headerp; | |
6055 | ||
6056 | for (headerp = start_headers; headerp < end_headers; headerp ++) | |
6057 | { | |
6058 | Elf_Internal_Shdr *header = * headerp; | |
6059 | ||
6060 | /* Debuginfo files do not have any allocated SHT_PROGBITS sections. | |
6061 | The only allocated sections are SHT_NOBITS or SHT_NOTES. */ | |
6062 | if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC | |
6063 | && header->sh_type != SHT_NOBITS | |
6064 | && header->sh_type != SHT_NOTE) | |
6065 | return FALSE; | |
6066 | } | |
6067 | ||
6068 | return TRUE; | |
6069 | } | |
6070 | ||
1ff6de03 NA |
6071 | /* Assign file positions for the other sections, except for compressed debugging |
6072 | and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */ | |
f3520d2f AM |
6073 | |
6074 | static bfd_boolean | |
6075 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
6076 | struct bfd_link_info *link_info) | |
6077 | { | |
6078 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
6079 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 6080 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
6081 | Elf_Internal_Phdr *phdrs; |
6082 | Elf_Internal_Phdr *p; | |
6083 | struct elf_segment_map *m; | |
f3520d2f | 6084 | file_ptr off; |
66631823 | 6085 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
f3520d2f | 6086 | |
5c182d5f | 6087 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 6088 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 6089 | off = elf_next_file_pos (abfd); |
e06efbf1 | 6090 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 6091 | { |
5c182d5f AM |
6092 | Elf_Internal_Shdr *hdr; |
6093 | ||
6094 | hdr = *hdrpp; | |
6095 | if (hdr->bfd_section != NULL | |
252e386e AM |
6096 | && (hdr->bfd_section->filepos != 0 |
6097 | || (hdr->sh_type == SHT_NOBITS | |
6098 | && hdr->contents == NULL))) | |
627b32bc | 6099 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
6100 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
6101 | { | |
1faa385f NC |
6102 | if (hdr->sh_size != 0 |
6103 | /* PR 24717 - debuginfo files are known to be not strictly | |
6104 | compliant with the ELF standard. In particular they often | |
6105 | have .note.gnu.property sections that are outside of any | |
6106 | loadable segment. This is not a problem for such files, | |
6107 | so do not warn about them. */ | |
6108 | && ! is_debuginfo_file (abfd)) | |
4eca0228 | 6109 | _bfd_error_handler |
695344c0 | 6110 | /* xgettext:c-format */ |
871b3ab2 | 6111 | (_("%pB: warning: allocated section `%s' not in segment"), |
e8d2ba53 AM |
6112 | abfd, |
6113 | (hdr->bfd_section == NULL | |
6114 | ? "*unknown*" | |
6115 | : hdr->bfd_section->name)); | |
3ba71138 L |
6116 | /* We don't need to page align empty sections. */ |
6117 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
6118 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
6119 | bed->maxpagesize); | |
6120 | else | |
6121 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
6122 | hdr->sh_addralign); | |
6123 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
6124 | FALSE); | |
6125 | } | |
6126 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
6127 | && hdr->bfd_section == NULL) | |
1ff6de03 NA |
6128 | /* We don't know the offset of these sections yet: their size has |
6129 | not been decided. */ | |
0ce398f1 | 6130 | || (hdr->bfd_section != NULL |
1ff6de03 NA |
6131 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS |
6132 | || (bfd_section_is_ctf (hdr->bfd_section) | |
6133 | && abfd->is_linker_output))) | |
12bd6957 | 6134 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
6a40cf0c NC |
6135 | || (elf_symtab_shndx_list (abfd) != NULL |
6136 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
6137 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
6138 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
6139 | hdr->sh_offset = -1; |
6140 | else | |
6141 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f | 6142 | } |
30fe1832 | 6143 | elf_next_file_pos (abfd) = off; |
5c182d5f | 6144 | |
252b5132 RH |
6145 | /* Now that we have set the section file positions, we can set up |
6146 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f | 6147 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 6148 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 6149 | { |
129af99f | 6150 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 6151 | { |
66631823 | 6152 | bfd_vma start, end; /* Bytes. */ |
01f7e10c | 6153 | bfd_boolean ok; |
1ea63fd2 | 6154 | |
129af99f | 6155 | if (link_info != NULL) |
8c37241b | 6156 | { |
129af99f | 6157 | /* During linking the range of the RELRO segment is passed |
f2731e0c AM |
6158 | in link_info. Note that there may be padding between |
6159 | relro_start and the first RELRO section. */ | |
6160 | start = link_info->relro_start; | |
6161 | end = link_info->relro_end; | |
6162 | } | |
6163 | else if (m->count != 0) | |
6164 | { | |
6165 | if (!m->p_size_valid) | |
6166 | abort (); | |
6167 | start = m->sections[0]->vma; | |
66631823 | 6168 | end = start + m->p_size / opb; |
f2731e0c AM |
6169 | } |
6170 | else | |
6171 | { | |
6172 | start = 0; | |
6173 | end = 0; | |
6174 | } | |
6175 | ||
01f7e10c | 6176 | ok = FALSE; |
f2731e0c AM |
6177 | if (start < end) |
6178 | { | |
6179 | struct elf_segment_map *lm; | |
6180 | const Elf_Internal_Phdr *lp; | |
6181 | unsigned int i; | |
6182 | ||
6183 | /* Find a LOAD segment containing a section in the RELRO | |
6184 | segment. */ | |
12bd6957 | 6185 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
6186 | lm != NULL; |
6187 | lm = lm->next, lp++) | |
8c37241b JJ |
6188 | { |
6189 | if (lp->p_type == PT_LOAD | |
3146fac4 | 6190 | && lm->count != 0 |
dbc88fc1 AM |
6191 | && (lm->sections[lm->count - 1]->vma |
6192 | + (!IS_TBSS (lm->sections[lm->count - 1]) | |
66631823 | 6193 | ? lm->sections[lm->count - 1]->size / opb |
dbc88fc1 | 6194 | : 0)) > start |
f2731e0c | 6195 | && lm->sections[0]->vma < end) |
8c37241b JJ |
6196 | break; |
6197 | } | |
f2731e0c | 6198 | |
01f7e10c | 6199 | if (lm != NULL) |
129af99f | 6200 | { |
01f7e10c AM |
6201 | /* Find the section starting the RELRO segment. */ |
6202 | for (i = 0; i < lm->count; i++) | |
6203 | { | |
6204 | asection *s = lm->sections[i]; | |
6205 | if (s->vma >= start | |
6206 | && s->vma < end | |
6207 | && s->size != 0) | |
6208 | break; | |
6209 | } | |
6210 | ||
6211 | if (i < lm->count) | |
6212 | { | |
502794d4 CE |
6213 | p->p_vaddr = lm->sections[i]->vma * opb; |
6214 | p->p_paddr = lm->sections[i]->lma * opb; | |
01f7e10c | 6215 | p->p_offset = lm->sections[i]->filepos; |
66631823 | 6216 | p->p_memsz = end * opb - p->p_vaddr; |
01f7e10c AM |
6217 | p->p_filesz = p->p_memsz; |
6218 | ||
6219 | /* The RELRO segment typically ends a few bytes | |
6220 | into .got.plt but other layouts are possible. | |
6221 | In cases where the end does not match any | |
6222 | loaded section (for instance is in file | |
6223 | padding), trim p_filesz back to correspond to | |
6224 | the end of loaded section contents. */ | |
6225 | if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr) | |
6226 | p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr; | |
6227 | ||
6228 | /* Preserve the alignment and flags if they are | |
6229 | valid. The gold linker generates RW/4 for | |
6230 | the PT_GNU_RELRO section. It is better for | |
6231 | objcopy/strip to honor these attributes | |
6232 | otherwise gdb will choke when using separate | |
6233 | debug files. */ | |
6234 | if (!m->p_align_valid) | |
6235 | p->p_align = 1; | |
6236 | if (!m->p_flags_valid) | |
6237 | p->p_flags = PF_R; | |
6238 | ok = TRUE; | |
6239 | } | |
129af99f | 6240 | } |
b84a33b5 | 6241 | } |
01f7e10c AM |
6242 | if (link_info != NULL) |
6243 | BFD_ASSERT (ok); | |
6244 | if (!ok) | |
6245 | memset (p, 0, sizeof *p); | |
129af99f | 6246 | } |
04c3a755 NS |
6247 | else if (p->p_type == PT_GNU_STACK) |
6248 | { | |
6249 | if (m->p_size_valid) | |
6250 | p->p_memsz = m->p_size; | |
6251 | } | |
129af99f AS |
6252 | else if (m->count != 0) |
6253 | { | |
e06efbf1 | 6254 | unsigned int i; |
1a9ccd70 | 6255 | |
129af99f AS |
6256 | if (p->p_type != PT_LOAD |
6257 | && (p->p_type != PT_NOTE | |
6258 | || bfd_get_format (abfd) != bfd_core)) | |
6259 | { | |
1a9ccd70 NC |
6260 | /* A user specified segment layout may include a PHDR |
6261 | segment that overlaps with a LOAD segment... */ | |
6262 | if (p->p_type == PT_PHDR) | |
6263 | { | |
6264 | m->count = 0; | |
6265 | continue; | |
6266 | } | |
6267 | ||
c86934ce NC |
6268 | if (m->includes_filehdr || m->includes_phdrs) |
6269 | { | |
b1fa9dd6 | 6270 | /* PR 17512: file: 2195325e. */ |
4eca0228 | 6271 | _bfd_error_handler |
871b3ab2 | 6272 | (_("%pB: error: non-load segment %d includes file header " |
76cfced5 AM |
6273 | "and/or program header"), |
6274 | abfd, (int) (p - phdrs)); | |
c86934ce NC |
6275 | return FALSE; |
6276 | } | |
129af99f | 6277 | |
86b2281f | 6278 | p->p_filesz = 0; |
129af99f | 6279 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
6280 | for (i = m->count; i-- != 0;) |
6281 | { | |
6282 | asection *sect = m->sections[i]; | |
6283 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
6284 | if (hdr->sh_type != SHT_NOBITS) | |
6285 | { | |
6286 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
6287 | + hdr->sh_size); | |
6288 | break; | |
6289 | } | |
6290 | } | |
129af99f AS |
6291 | } |
6292 | } | |
252b5132 RH |
6293 | } |
6294 | ||
b34976b6 | 6295 | return TRUE; |
252b5132 RH |
6296 | } |
6297 | ||
6a40cf0c NC |
6298 | static elf_section_list * |
6299 | find_section_in_list (unsigned int i, elf_section_list * list) | |
6300 | { | |
6301 | for (;list != NULL; list = list->next) | |
6302 | if (list->ndx == i) | |
6303 | break; | |
6304 | return list; | |
6305 | } | |
6306 | ||
252b5132 RH |
6307 | /* Work out the file positions of all the sections. This is called by |
6308 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
6309 | VMAs must be known before this is called. | |
6310 | ||
e0638f70 | 6311 | Reloc sections come in two flavours: Those processed specially as |
1ff6de03 NA |
6312 | "side-channel" data attached to a section to which they apply, and those that |
6313 | bfd doesn't process as relocations. The latter sort are stored in a normal | |
6314 | bfd section by bfd_section_from_shdr. We don't consider the former sort | |
6315 | here, unless they form part of the loadable image. Reloc sections not | |
6316 | assigned here (and compressed debugging sections and CTF sections which | |
6317 | nothing else in the file can rely upon) will be handled later by | |
e0638f70 | 6318 | assign_file_positions_for_relocs. |
252b5132 RH |
6319 | |
6320 | We also don't set the positions of the .symtab and .strtab here. */ | |
6321 | ||
b34976b6 | 6322 | static bfd_boolean |
c84fca4d AO |
6323 | assign_file_positions_except_relocs (bfd *abfd, |
6324 | struct bfd_link_info *link_info) | |
252b5132 | 6325 | { |
5c182d5f AM |
6326 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
6327 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 6328 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6d6c25c8 | 6329 | unsigned int alloc; |
252b5132 RH |
6330 | |
6331 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
6332 | && bfd_get_format (abfd) != bfd_core) | |
6333 | { | |
5c182d5f AM |
6334 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
6335 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
6336 | Elf_Internal_Shdr **hdrpp; |
6337 | unsigned int i; | |
a485e98e | 6338 | file_ptr off; |
252b5132 RH |
6339 | |
6340 | /* Start after the ELF header. */ | |
6341 | off = i_ehdrp->e_ehsize; | |
6342 | ||
6343 | /* We are not creating an executable, which means that we are | |
6344 | not creating a program header, and that the actual order of | |
6345 | the sections in the file is unimportant. */ | |
9ad5cbcf | 6346 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
6347 | { |
6348 | Elf_Internal_Shdr *hdr; | |
6349 | ||
6350 | hdr = *hdrpp; | |
e0638f70 AM |
6351 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
6352 | && hdr->bfd_section == NULL) | |
1ff6de03 NA |
6353 | /* Do not assign offsets for these sections yet: we don't know |
6354 | their sizes. */ | |
0ce398f1 | 6355 | || (hdr->bfd_section != NULL |
1ff6de03 NA |
6356 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS |
6357 | || (bfd_section_is_ctf (hdr->bfd_section) | |
6358 | && abfd->is_linker_output))) | |
12bd6957 | 6359 | || i == elf_onesymtab (abfd) |
6a40cf0c NC |
6360 | || (elf_symtab_shndx_list (abfd) != NULL |
6361 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
6362 | || i == elf_strtab_sec (abfd) |
6363 | || i == elf_shstrtab_sec (abfd)) | |
252b5132 RH |
6364 | { |
6365 | hdr->sh_offset = -1; | |
252b5132 | 6366 | } |
9ad5cbcf | 6367 | else |
b34976b6 | 6368 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 6369 | } |
a485e98e AM |
6370 | |
6371 | elf_next_file_pos (abfd) = off; | |
6d6c25c8 | 6372 | elf_program_header_size (abfd) = 0; |
252b5132 RH |
6373 | } |
6374 | else | |
6375 | { | |
252b5132 | 6376 | /* Assign file positions for the loaded sections based on the |
08a40648 | 6377 | assignment of sections to segments. */ |
f3520d2f AM |
6378 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
6379 | return FALSE; | |
6380 | ||
6381 | /* And for non-load sections. */ | |
6382 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
6383 | return FALSE; | |
6d6c25c8 | 6384 | } |
f3520d2f | 6385 | |
6d6c25c8 AM |
6386 | if (!(*bed->elf_backend_modify_headers) (abfd, link_info)) |
6387 | return FALSE; | |
1a9ccd70 | 6388 | |
6d6c25c8 AM |
6389 | /* Write out the program headers. */ |
6390 | alloc = i_ehdrp->e_phnum; | |
6391 | if (alloc != 0) | |
6392 | { | |
30fe1832 | 6393 | if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0 |
cd584857 NC |
6394 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) |
6395 | return FALSE; | |
252b5132 RH |
6396 | } |
6397 | ||
b34976b6 | 6398 | return TRUE; |
252b5132 RH |
6399 | } |
6400 | ||
ed7e9d0b AM |
6401 | bfd_boolean |
6402 | _bfd_elf_init_file_header (bfd *abfd, | |
6403 | struct bfd_link_info *info ATTRIBUTE_UNUSED) | |
252b5132 | 6404 | { |
3d540e93 | 6405 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 6406 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 6407 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
6408 | |
6409 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 6410 | |
2b0f7ef9 | 6411 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 6412 | if (shstrtab == NULL) |
b34976b6 | 6413 | return FALSE; |
252b5132 RH |
6414 | |
6415 | elf_shstrtab (abfd) = shstrtab; | |
6416 | ||
6417 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
6418 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
6419 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
6420 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
6421 | ||
6422 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
6423 | i_ehdrp->e_ident[EI_DATA] = | |
6424 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
6425 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
6426 | ||
252b5132 RH |
6427 | if ((abfd->flags & DYNAMIC) != 0) |
6428 | i_ehdrp->e_type = ET_DYN; | |
6429 | else if ((abfd->flags & EXEC_P) != 0) | |
6430 | i_ehdrp->e_type = ET_EXEC; | |
6431 | else if (bfd_get_format (abfd) == bfd_core) | |
6432 | i_ehdrp->e_type = ET_CORE; | |
6433 | else | |
6434 | i_ehdrp->e_type = ET_REL; | |
6435 | ||
6436 | switch (bfd_get_arch (abfd)) | |
6437 | { | |
6438 | case bfd_arch_unknown: | |
6439 | i_ehdrp->e_machine = EM_NONE; | |
6440 | break; | |
aa4f99bb AO |
6441 | |
6442 | /* There used to be a long list of cases here, each one setting | |
6443 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
6444 | in the corresponding bfd definition. To avoid duplication, | |
6445 | the switch was removed. Machines that need special handling | |
6446 | can generally do it in elf_backend_final_write_processing(), | |
6447 | unless they need the information earlier than the final write. | |
6448 | Such need can generally be supplied by replacing the tests for | |
6449 | e_machine with the conditions used to determine it. */ | |
252b5132 | 6450 | default: |
9c5bfbb7 AM |
6451 | i_ehdrp->e_machine = bed->elf_machine_code; |
6452 | } | |
aa4f99bb | 6453 | |
252b5132 RH |
6454 | i_ehdrp->e_version = bed->s->ev_current; |
6455 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
6456 | ||
c044fabd | 6457 | /* No program header, for now. */ |
252b5132 RH |
6458 | i_ehdrp->e_phoff = 0; |
6459 | i_ehdrp->e_phentsize = 0; | |
6460 | i_ehdrp->e_phnum = 0; | |
6461 | ||
c044fabd | 6462 | /* Each bfd section is section header entry. */ |
252b5132 RH |
6463 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
6464 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
6465 | ||
252b5132 | 6466 | elf_tdata (abfd)->symtab_hdr.sh_name = |
b34976b6 | 6467 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 6468 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 6469 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 6470 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 6471 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 6472 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 6473 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 6474 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 6475 | return FALSE; |
252b5132 | 6476 | |
b34976b6 | 6477 | return TRUE; |
252b5132 RH |
6478 | } |
6479 | ||
6d6c25c8 AM |
6480 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. |
6481 | ||
6482 | FIXME: We used to have code here to sort the PT_LOAD segments into | |
6483 | ascending order, as per the ELF spec. But this breaks some programs, | |
6484 | including the Linux kernel. But really either the spec should be | |
6485 | changed or the programs updated. */ | |
6486 | ||
6487 | bfd_boolean | |
6488 | _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info) | |
6489 | { | |
6490 | if (link_info != NULL && bfd_link_pie (link_info)) | |
6491 | { | |
6492 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd); | |
6493 | unsigned int num_segments = i_ehdrp->e_phnum; | |
6494 | struct elf_obj_tdata *tdata = elf_tdata (obfd); | |
6495 | Elf_Internal_Phdr *segment = tdata->phdr; | |
6496 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
6497 | ||
6498 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
6499 | bfd_vma p_vaddr = (bfd_vma) -1; | |
6500 | for (; segment < end_segment; segment++) | |
6501 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
6502 | p_vaddr = segment->p_vaddr; | |
6503 | ||
6504 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
6505 | segments is non-zero. */ | |
6506 | if (p_vaddr) | |
6507 | i_ehdrp->e_type = ET_EXEC; | |
6508 | } | |
6509 | return TRUE; | |
6510 | } | |
6511 | ||
252b5132 | 6512 | /* Assign file positions for all the reloc sections which are not part |
a485e98e | 6513 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 6514 | |
0ce398f1 L |
6515 | static bfd_boolean |
6516 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
6517 | { |
6518 | file_ptr off; | |
e06efbf1 | 6519 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
3e19fb8f | 6520 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
6521 | Elf_Internal_Ehdr *i_ehdrp; |
6522 | const struct elf_backend_data *bed; | |
252b5132 | 6523 | |
12bd6957 | 6524 | off = elf_next_file_pos (abfd); |
252b5132 | 6525 | |
e06efbf1 L |
6526 | shdrpp = elf_elfsections (abfd); |
6527 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
6528 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 6529 | { |
252b5132 | 6530 | shdrp = *shdrpp; |
0ce398f1 L |
6531 | if (shdrp->sh_offset == -1) |
6532 | { | |
3e19fb8f | 6533 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
6534 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
6535 | || shdrp->sh_type == SHT_RELA); | |
1ff6de03 | 6536 | bfd_boolean is_ctf = sec && bfd_section_is_ctf (sec); |
0ce398f1 | 6537 | if (is_rel |
1ff6de03 | 6538 | || is_ctf |
3e19fb8f | 6539 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 | 6540 | { |
1ff6de03 | 6541 | if (!is_rel && !is_ctf) |
0ce398f1 | 6542 | { |
3e19fb8f L |
6543 | const char *name = sec->name; |
6544 | struct bfd_elf_section_data *d; | |
6545 | ||
0ce398f1 | 6546 | /* Compress DWARF debug sections. */ |
3e19fb8f | 6547 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
6548 | shdrp->contents)) |
6549 | return FALSE; | |
3e19fb8f L |
6550 | |
6551 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
6552 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
6553 | { | |
6554 | /* If section is compressed with zlib-gnu, convert | |
6555 | section name from .debug_* to .zdebug_*. */ | |
6556 | char *new_name | |
6557 | = convert_debug_to_zdebug (abfd, name); | |
6558 | if (new_name == NULL) | |
6559 | return FALSE; | |
6560 | name = new_name; | |
6561 | } | |
dd905818 | 6562 | /* Add section name to section name section. */ |
3e19fb8f L |
6563 | if (shdrp->sh_name != (unsigned int) -1) |
6564 | abort (); | |
6565 | shdrp->sh_name | |
6566 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
6567 | name, FALSE); | |
6568 | d = elf_section_data (sec); | |
6569 | ||
dd905818 | 6570 | /* Add reloc section name to section name section. */ |
3e19fb8f L |
6571 | if (d->rel.hdr |
6572 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6573 | d->rel.hdr, | |
6574 | name, FALSE)) | |
6575 | return FALSE; | |
6576 | if (d->rela.hdr | |
6577 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6578 | d->rela.hdr, | |
91cb26da | 6579 | name, TRUE)) |
3e19fb8f L |
6580 | return FALSE; |
6581 | ||
0ce398f1 | 6582 | /* Update section size and contents. */ |
3e19fb8f L |
6583 | shdrp->sh_size = sec->size; |
6584 | shdrp->contents = sec->contents; | |
0ce398f1 L |
6585 | shdrp->bfd_section->contents = NULL; |
6586 | } | |
1ff6de03 NA |
6587 | else if (is_ctf) |
6588 | { | |
6589 | /* Update section size and contents. */ | |
6590 | shdrp->sh_size = sec->size; | |
6591 | shdrp->contents = sec->contents; | |
6592 | } | |
6593 | ||
0ce398f1 L |
6594 | off = _bfd_elf_assign_file_position_for_section (shdrp, |
6595 | off, | |
6596 | TRUE); | |
6597 | } | |
6598 | } | |
252b5132 RH |
6599 | } |
6600 | ||
3e19fb8f L |
6601 | /* Place section name section after DWARF debug sections have been |
6602 | compressed. */ | |
6603 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
6604 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
6605 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
6606 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
6607 | ||
6608 | /* Place the section headers. */ | |
a485e98e AM |
6609 | i_ehdrp = elf_elfheader (abfd); |
6610 | bed = get_elf_backend_data (abfd); | |
6611 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
6612 | i_ehdrp->e_shoff = off; | |
6613 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 6614 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
6615 | |
6616 | return TRUE; | |
252b5132 RH |
6617 | } |
6618 | ||
b34976b6 | 6619 | bfd_boolean |
217aa764 | 6620 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 6621 | { |
9c5bfbb7 | 6622 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6623 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 6624 | bfd_boolean failed; |
9ad5cbcf | 6625 | unsigned int count, num_sec; |
30e8ee25 | 6626 | struct elf_obj_tdata *t; |
252b5132 RH |
6627 | |
6628 | if (! abfd->output_has_begun | |
217aa764 | 6629 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 6630 | return FALSE; |
db727370 JL |
6631 | /* Do not rewrite ELF data when the BFD has been opened for update. |
6632 | abfd->output_has_begun was set to TRUE on opening, so creation of new | |
6633 | sections, and modification of existing section sizes was restricted. | |
6634 | This means the ELF header, program headers and section headers can't have | |
6635 | changed. | |
6636 | If the contents of any sections has been modified, then those changes have | |
6637 | already been written to the BFD. */ | |
6638 | else if (abfd->direction == both_direction) | |
6639 | { | |
6640 | BFD_ASSERT (abfd->output_has_begun); | |
6641 | return TRUE; | |
6642 | } | |
252b5132 RH |
6643 | |
6644 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 6645 | |
b34976b6 | 6646 | failed = FALSE; |
252b5132 RH |
6647 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
6648 | if (failed) | |
b34976b6 | 6649 | return FALSE; |
252b5132 | 6650 | |
0ce398f1 L |
6651 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
6652 | return FALSE; | |
252b5132 | 6653 | |
c044fabd | 6654 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
6655 | num_sec = elf_numsections (abfd); |
6656 | for (count = 1; count < num_sec; count++) | |
252b5132 | 6657 | { |
3e19fb8f L |
6658 | i_shdrp[count]->sh_name |
6659 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
6660 | i_shdrp[count]->sh_name); | |
252b5132 | 6661 | if (bed->elf_backend_section_processing) |
75506100 MR |
6662 | if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count])) |
6663 | return FALSE; | |
252b5132 RH |
6664 | if (i_shdrp[count]->contents) |
6665 | { | |
dc810e39 AM |
6666 | bfd_size_type amt = i_shdrp[count]->sh_size; |
6667 | ||
252b5132 | 6668 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 6669 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 6670 | return FALSE; |
252b5132 RH |
6671 | } |
6672 | } | |
6673 | ||
6674 | /* Write out the section header names. */ | |
30e8ee25 | 6675 | t = elf_tdata (abfd); |
26ae6d5e | 6676 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 6677 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 6678 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 6679 | return FALSE; |
252b5132 | 6680 | |
cc364be6 AM |
6681 | if (!(*bed->elf_backend_final_write_processing) (abfd)) |
6682 | return FALSE; | |
252b5132 | 6683 | |
ff59fc36 RM |
6684 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
6685 | return FALSE; | |
6686 | ||
6687 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
6688 | if (t->o->build_id.after_write_object_contents != NULL) |
6689 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
6690 | |
6691 | return TRUE; | |
252b5132 RH |
6692 | } |
6693 | ||
b34976b6 | 6694 | bfd_boolean |
217aa764 | 6695 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 6696 | { |
c044fabd | 6697 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
6698 | return _bfd_elf_write_object_contents (abfd); |
6699 | } | |
c044fabd KH |
6700 | |
6701 | /* Given a section, search the header to find them. */ | |
6702 | ||
cb33740c | 6703 | unsigned int |
198beae2 | 6704 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 6705 | { |
9c5bfbb7 | 6706 | const struct elf_backend_data *bed; |
91d6fa6a | 6707 | unsigned int sec_index; |
252b5132 | 6708 | |
9ad5cbcf AM |
6709 | if (elf_section_data (asect) != NULL |
6710 | && elf_section_data (asect)->this_idx != 0) | |
6711 | return elf_section_data (asect)->this_idx; | |
6712 | ||
6713 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 6714 | sec_index = SHN_ABS; |
af746e92 | 6715 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 6716 | sec_index = SHN_COMMON; |
af746e92 | 6717 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 6718 | sec_index = SHN_UNDEF; |
af746e92 | 6719 | else |
91d6fa6a | 6720 | sec_index = SHN_BAD; |
252b5132 | 6721 | |
af746e92 | 6722 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
6723 | if (bed->elf_backend_section_from_bfd_section) |
6724 | { | |
91d6fa6a | 6725 | int retval = sec_index; |
9ad5cbcf | 6726 | |
af746e92 AM |
6727 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
6728 | return retval; | |
252b5132 RH |
6729 | } |
6730 | ||
91d6fa6a | 6731 | if (sec_index == SHN_BAD) |
af746e92 | 6732 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 6733 | |
91d6fa6a | 6734 | return sec_index; |
252b5132 RH |
6735 | } |
6736 | ||
6737 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
6738 | on error. */ | |
6739 | ||
6740 | int | |
217aa764 | 6741 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
6742 | { |
6743 | asymbol *asym_ptr = *asym_ptr_ptr; | |
6744 | int idx; | |
6745 | flagword flags = asym_ptr->flags; | |
6746 | ||
6747 | /* When gas creates relocations against local labels, it creates its | |
6748 | own symbol for the section, but does put the symbol into the | |
6749 | symbol chain, so udata is 0. When the linker is generating | |
6750 | relocatable output, this section symbol may be for one of the | |
6751 | input sections rather than the output section. */ | |
6752 | if (asym_ptr->udata.i == 0 | |
6753 | && (flags & BSF_SECTION_SYM) | |
6754 | && asym_ptr->section) | |
6755 | { | |
5372391b | 6756 | asection *sec; |
252b5132 RH |
6757 | int indx; |
6758 | ||
5372391b AM |
6759 | sec = asym_ptr->section; |
6760 | if (sec->owner != abfd && sec->output_section != NULL) | |
6761 | sec = sec->output_section; | |
6762 | if (sec->owner == abfd | |
6763 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 6764 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
6765 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
6766 | } | |
6767 | ||
6768 | idx = asym_ptr->udata.i; | |
6769 | ||
6770 | if (idx == 0) | |
6771 | { | |
6772 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 6773 | which is used in a relocation entry. */ |
4eca0228 | 6774 | _bfd_error_handler |
695344c0 | 6775 | /* xgettext:c-format */ |
871b3ab2 | 6776 | (_("%pB: symbol `%s' required but not present"), |
d003868e | 6777 | abfd, bfd_asymbol_name (asym_ptr)); |
252b5132 RH |
6778 | bfd_set_error (bfd_error_no_symbols); |
6779 | return -1; | |
6780 | } | |
6781 | ||
6782 | #if DEBUG & 4 | |
6783 | { | |
6784 | fprintf (stderr, | |
cd9af601 AM |
6785 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n", |
6786 | (long) asym_ptr, asym_ptr->name, idx, flags); | |
252b5132 RH |
6787 | fflush (stderr); |
6788 | } | |
6789 | #endif | |
6790 | ||
6791 | return idx; | |
6792 | } | |
6793 | ||
84d1d650 | 6794 | /* Rewrite program header information. */ |
252b5132 | 6795 | |
b34976b6 | 6796 | static bfd_boolean |
84d1d650 | 6797 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 6798 | { |
b34976b6 AM |
6799 | Elf_Internal_Ehdr *iehdr; |
6800 | struct elf_segment_map *map; | |
6801 | struct elf_segment_map *map_first; | |
6802 | struct elf_segment_map **pointer_to_map; | |
6803 | Elf_Internal_Phdr *segment; | |
6804 | asection *section; | |
6805 | unsigned int i; | |
6806 | unsigned int num_segments; | |
6807 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 6808 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
6809 | bfd_vma maxpagesize; |
6810 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
6811 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 6812 | const struct elf_backend_data *bed; |
502794d4 | 6813 | unsigned int opb = bfd_octets_per_byte (ibfd, NULL); |
bc67d8a6 | 6814 | |
caf47ea6 | 6815 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
6816 | iehdr = elf_elfheader (ibfd); |
6817 | ||
bc67d8a6 | 6818 | map_first = NULL; |
c044fabd | 6819 | pointer_to_map = &map_first; |
252b5132 RH |
6820 | |
6821 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
6822 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
6823 | ||
6824 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
6825 | #define SEGMENT_END(segment, start) \ |
6826 | (start + (segment->p_memsz > segment->p_filesz \ | |
6827 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 6828 | |
eecdbe52 JJ |
6829 | #define SECTION_SIZE(section, segment) \ |
6830 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
6831 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 6832 | ? section->size : 0) |
eecdbe52 | 6833 | |
b34976b6 | 6834 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6835 | the given segment. VMA addresses are compared. */ |
502794d4 CE |
6836 | #define IS_CONTAINED_BY_VMA(section, segment, opb) \ |
6837 | (section->vma * (opb) >= segment->p_vaddr \ | |
6838 | && (section->vma * (opb) + SECTION_SIZE (section, segment) \ | |
aecc8f8a | 6839 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 6840 | |
b34976b6 | 6841 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6842 | the given segment. LMA addresses are compared. */ |
502794d4 CE |
6843 | #define IS_CONTAINED_BY_LMA(section, segment, base, opb) \ |
6844 | (section->lma * (opb) >= base \ | |
6845 | && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \ | |
6846 | && (section->lma * (opb) + SECTION_SIZE (section, segment) \ | |
aecc8f8a | 6847 | <= SEGMENT_END (segment, base))) |
252b5132 | 6848 | |
0efc80c8 L |
6849 | /* Handle PT_NOTE segment. */ |
6850 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 6851 | (p->p_type == PT_NOTE \ |
0efc80c8 | 6852 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 6853 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6854 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6855 | <= p->p_offset + p->p_filesz)) |
252b5132 | 6856 | |
0efc80c8 L |
6857 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
6858 | etc. */ | |
6859 | #define IS_COREFILE_NOTE(p, s) \ | |
6860 | (IS_NOTE (p, s) \ | |
6861 | && bfd_get_format (ibfd) == bfd_core \ | |
6862 | && s->vma == 0 \ | |
6863 | && s->lma == 0) | |
6864 | ||
252b5132 RH |
6865 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
6866 | linker, which generates a PT_INTERP section with p_vaddr and | |
6867 | p_memsz set to 0. */ | |
aecc8f8a AM |
6868 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
6869 | (p->p_vaddr == 0 \ | |
6870 | && p->p_paddr == 0 \ | |
6871 | && p->p_memsz == 0 \ | |
6872 | && p->p_filesz > 0 \ | |
6873 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 6874 | && s->size > 0 \ |
aecc8f8a | 6875 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6876 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6877 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 6878 | |
bc67d8a6 NC |
6879 | /* Decide if the given section should be included in the given segment. |
6880 | A section will be included if: | |
f5ffc919 | 6881 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 6882 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 6883 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 6884 | segment. |
bc67d8a6 | 6885 | 3. There is an output section associated with it, |
eecdbe52 | 6886 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 6887 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 6888 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
6889 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
6890 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 6891 | (with the possible exception of .dynamic). */ |
502794d4 | 6892 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \ |
2b05f1b7 | 6893 | ((((segment->p_paddr \ |
502794d4 CE |
6894 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \ |
6895 | : IS_CONTAINED_BY_VMA (section, segment, opb)) \ | |
2b05f1b7 | 6896 | && (section->flags & SEC_ALLOC) != 0) \ |
0efc80c8 | 6897 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
6898 | && segment->p_type != PT_GNU_STACK \ |
6899 | && (segment->p_type != PT_TLS \ | |
6900 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
6901 | && (segment->p_type == PT_LOAD \ | |
6902 | || segment->p_type == PT_TLS \ | |
6903 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6904 | && (segment->p_type != PT_DYNAMIC \ | |
6905 | || SECTION_SIZE (section, segment) > 0 \ | |
6906 | || (segment->p_paddr \ | |
502794d4 CE |
6907 | ? segment->p_paddr != section->lma * (opb) \ |
6908 | : segment->p_vaddr != section->vma * (opb)) \ | |
fd361982 | 6909 | || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \ |
9933dc52 | 6910 | && (segment->p_type != PT_LOAD || !section->segment_mark)) |
bc67d8a6 | 6911 | |
9f17e2a6 L |
6912 | /* If the output section of a section in the input segment is NULL, |
6913 | it is removed from the corresponding output segment. */ | |
502794d4 CE |
6914 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \ |
6915 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \ | |
9f17e2a6 L |
6916 | && section->output_section != NULL) |
6917 | ||
b34976b6 | 6918 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
6919 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
6920 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
6921 | ||
6922 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
6923 | their VMA address ranges and their LMA address ranges overlap. | |
6924 | It is possible to have overlapping VMA ranges without overlapping LMA | |
6925 | ranges. RedBoot images for example can have both .data and .bss mapped | |
6926 | to the same VMA range, but with the .data section mapped to a different | |
6927 | LMA. */ | |
aecc8f8a | 6928 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 6929 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 6930 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 6931 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 6932 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
6933 | |
6934 | /* Initialise the segment mark field. */ | |
6935 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 6936 | section->segment_mark = FALSE; |
bc67d8a6 | 6937 | |
5c44b38e AM |
6938 | /* The Solaris linker creates program headers in which all the |
6939 | p_paddr fields are zero. When we try to objcopy or strip such a | |
6940 | file, we get confused. Check for this case, and if we find it | |
6941 | don't set the p_paddr_valid fields. */ | |
6942 | p_paddr_valid = FALSE; | |
6943 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6944 | i < num_segments; | |
6945 | i++, segment++) | |
6946 | if (segment->p_paddr != 0) | |
6947 | { | |
6948 | p_paddr_valid = TRUE; | |
6949 | break; | |
6950 | } | |
6951 | ||
252b5132 | 6952 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6953 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6954 | in the loadable segments. These can be created by weird |
aecc8f8a | 6955 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6956 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6957 | i < num_segments; | |
c044fabd | 6958 | i++, segment++) |
252b5132 | 6959 | { |
252b5132 | 6960 | unsigned int j; |
c044fabd | 6961 | Elf_Internal_Phdr *segment2; |
252b5132 | 6962 | |
aecc8f8a AM |
6963 | if (segment->p_type == PT_INTERP) |
6964 | for (section = ibfd->sections; section; section = section->next) | |
6965 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6966 | { | |
6967 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6968 | assignment code will work. */ |
502794d4 | 6969 | segment->p_vaddr = section->vma * opb; |
aecc8f8a AM |
6970 | break; |
6971 | } | |
6972 | ||
bc67d8a6 | 6973 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
6974 | { |
6975 | /* Remove PT_GNU_RELRO segment. */ | |
6976 | if (segment->p_type == PT_GNU_RELRO) | |
6977 | segment->p_type = PT_NULL; | |
6978 | continue; | |
6979 | } | |
c044fabd | 6980 | |
bc67d8a6 | 6981 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 6982 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
6983 | { |
6984 | bfd_signed_vma extra_length; | |
c044fabd | 6985 | |
bc67d8a6 | 6986 | if (segment2->p_type != PT_LOAD |
0067a569 | 6987 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6988 | continue; |
c044fabd | 6989 | |
bc67d8a6 NC |
6990 | /* Merge the two segments together. */ |
6991 | if (segment2->p_vaddr < segment->p_vaddr) | |
6992 | { | |
c044fabd | 6993 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6994 | SEGMENT. */ |
0067a569 AM |
6995 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6996 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6997 | |
bc67d8a6 NC |
6998 | if (extra_length > 0) |
6999 | { | |
0067a569 | 7000 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
7001 | segment2->p_filesz += extra_length; |
7002 | } | |
c044fabd | 7003 | |
bc67d8a6 | 7004 | segment->p_type = PT_NULL; |
c044fabd | 7005 | |
bc67d8a6 NC |
7006 | /* Since we have deleted P we must restart the outer loop. */ |
7007 | i = 0; | |
7008 | segment = elf_tdata (ibfd)->phdr; | |
7009 | break; | |
7010 | } | |
7011 | else | |
7012 | { | |
c044fabd | 7013 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 7014 | SEGMENT2. */ |
0067a569 AM |
7015 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
7016 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 7017 | |
bc67d8a6 NC |
7018 | if (extra_length > 0) |
7019 | { | |
0067a569 | 7020 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
7021 | segment->p_filesz += extra_length; |
7022 | } | |
c044fabd | 7023 | |
bc67d8a6 NC |
7024 | segment2->p_type = PT_NULL; |
7025 | } | |
7026 | } | |
7027 | } | |
c044fabd | 7028 | |
bc67d8a6 NC |
7029 | /* The second scan attempts to assign sections to segments. */ |
7030 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7031 | i < num_segments; | |
0067a569 | 7032 | i++, segment++) |
bc67d8a6 | 7033 | { |
0067a569 AM |
7034 | unsigned int section_count; |
7035 | asection **sections; | |
7036 | asection *output_section; | |
7037 | unsigned int isec; | |
9933dc52 AM |
7038 | asection *matching_lma; |
7039 | asection *suggested_lma; | |
0067a569 | 7040 | unsigned int j; |
446f7ed5 | 7041 | size_t amt; |
0067a569 | 7042 | asection *first_section; |
bc67d8a6 NC |
7043 | |
7044 | if (segment->p_type == PT_NULL) | |
7045 | continue; | |
c044fabd | 7046 | |
9f17e2a6 | 7047 | first_section = NULL; |
bc67d8a6 | 7048 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
7049 | for (section = ibfd->sections, section_count = 0; |
7050 | section != NULL; | |
7051 | section = section->next) | |
9f17e2a6 L |
7052 | { |
7053 | /* Find the first section in the input segment, which may be | |
7054 | removed from the corresponding output segment. */ | |
502794d4 | 7055 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb)) |
9f17e2a6 L |
7056 | { |
7057 | if (first_section == NULL) | |
7058 | first_section = section; | |
7059 | if (section->output_section != NULL) | |
7060 | ++section_count; | |
7061 | } | |
7062 | } | |
811072d8 | 7063 | |
b5f852ea NC |
7064 | /* Allocate a segment map big enough to contain |
7065 | all of the sections we have selected. */ | |
00bee008 | 7066 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
446f7ed5 | 7067 | amt += section_count * sizeof (asection *); |
a50b1753 | 7068 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 7069 | if (map == NULL) |
b34976b6 | 7070 | return FALSE; |
252b5132 RH |
7071 | |
7072 | /* Initialise the fields of the segment map. Default to | |
7073 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
7074 | map->next = NULL; |
7075 | map->p_type = segment->p_type; | |
7076 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 7077 | map->p_flags_valid = 1; |
55d55ac7 | 7078 | |
9f17e2a6 L |
7079 | /* If the first section in the input segment is removed, there is |
7080 | no need to preserve segment physical address in the corresponding | |
7081 | output segment. */ | |
945c025a | 7082 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
7083 | { |
7084 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 7085 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 7086 | } |
252b5132 RH |
7087 | |
7088 | /* Determine if this segment contains the ELF file header | |
7089 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
7090 | map->includes_filehdr = (segment->p_offset == 0 |
7091 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 7092 | map->includes_phdrs = 0; |
252b5132 | 7093 | |
0067a569 | 7094 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 7095 | { |
bc67d8a6 NC |
7096 | map->includes_phdrs = |
7097 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7098 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
7099 | >= ((bfd_vma) iehdr->e_phoff |
7100 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 7101 | |
bc67d8a6 | 7102 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 7103 | phdr_included = TRUE; |
252b5132 RH |
7104 | } |
7105 | ||
bc67d8a6 | 7106 | if (section_count == 0) |
252b5132 RH |
7107 | { |
7108 | /* Special segments, such as the PT_PHDR segment, may contain | |
7109 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 | 7110 | something. They are allowed by the ELF spec however, so only |
07d6d2b8 | 7111 | a warning is produced. |
f98450c6 NC |
7112 | There is however the valid use case of embedded systems which |
7113 | have segments with p_filesz of 0 and a p_memsz > 0 to initialize | |
7114 | flash memory with zeros. No warning is shown for that case. */ | |
7115 | if (segment->p_type == PT_LOAD | |
7116 | && (segment->p_filesz > 0 || segment->p_memsz == 0)) | |
7117 | /* xgettext:c-format */ | |
9793eb77 AM |
7118 | _bfd_error_handler |
7119 | (_("%pB: warning: empty loadable segment detected" | |
7120 | " at vaddr=%#" PRIx64 ", is this intentional?"), | |
7121 | ibfd, (uint64_t) segment->p_vaddr); | |
252b5132 | 7122 | |
502794d4 | 7123 | map->p_vaddr_offset = segment->p_vaddr / opb; |
bc67d8a6 | 7124 | map->count = 0; |
c044fabd KH |
7125 | *pointer_to_map = map; |
7126 | pointer_to_map = &map->next; | |
252b5132 RH |
7127 | |
7128 | continue; | |
7129 | } | |
7130 | ||
7131 | /* Now scan the sections in the input BFD again and attempt | |
7132 | to add their corresponding output sections to the segment map. | |
7133 | The problem here is how to handle an output section which has | |
7134 | been moved (ie had its LMA changed). There are four possibilities: | |
7135 | ||
7136 | 1. None of the sections have been moved. | |
7137 | In this case we can continue to use the segment LMA from the | |
7138 | input BFD. | |
7139 | ||
7140 | 2. All of the sections have been moved by the same amount. | |
7141 | In this case we can change the segment's LMA to match the LMA | |
7142 | of the first section. | |
7143 | ||
7144 | 3. Some of the sections have been moved, others have not. | |
7145 | In this case those sections which have not been moved can be | |
7146 | placed in the current segment which will have to have its size, | |
7147 | and possibly its LMA changed, and a new segment or segments will | |
7148 | have to be created to contain the other sections. | |
7149 | ||
b5f852ea | 7150 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
7151 | In this case we can change the segment's LMA to match the LMA |
7152 | of the first section and we will have to create a new segment | |
7153 | or segments to contain the other sections. | |
7154 | ||
7155 | In order to save time, we allocate an array to hold the section | |
7156 | pointers that we are interested in. As these sections get assigned | |
7157 | to a segment, they are removed from this array. */ | |
7158 | ||
446f7ed5 AM |
7159 | amt = section_count * sizeof (asection *); |
7160 | sections = (asection **) bfd_malloc (amt); | |
252b5132 | 7161 | if (sections == NULL) |
b34976b6 | 7162 | return FALSE; |
252b5132 RH |
7163 | |
7164 | /* Step One: Scan for segment vs section LMA conflicts. | |
7165 | Also add the sections to the section array allocated above. | |
7166 | Also add the sections to the current segment. In the common | |
7167 | case, where the sections have not been moved, this means that | |
7168 | we have completely filled the segment, and there is nothing | |
7169 | more to do. */ | |
252b5132 | 7170 | isec = 0; |
9933dc52 AM |
7171 | matching_lma = NULL; |
7172 | suggested_lma = NULL; | |
252b5132 | 7173 | |
461c4b2e | 7174 | for (section = first_section, j = 0; |
bc67d8a6 NC |
7175 | section != NULL; |
7176 | section = section->next) | |
252b5132 | 7177 | { |
502794d4 | 7178 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb)) |
c0f7859b | 7179 | { |
bc67d8a6 NC |
7180 | output_section = section->output_section; |
7181 | ||
0067a569 | 7182 | sections[j++] = section; |
252b5132 RH |
7183 | |
7184 | /* The Solaris native linker always sets p_paddr to 0. | |
7185 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
7186 | correct value. Note - some backends require that |
7187 | p_paddr be left as zero. */ | |
5c44b38e | 7188 | if (!p_paddr_valid |
4455705d | 7189 | && segment->p_vaddr != 0 |
0067a569 | 7190 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 7191 | && isec == 0 |
bc67d8a6 | 7192 | && output_section->lma != 0 |
9933dc52 AM |
7193 | && (align_power (segment->p_vaddr |
7194 | + (map->includes_filehdr | |
7195 | ? iehdr->e_ehsize : 0) | |
7196 | + (map->includes_phdrs | |
7197 | ? iehdr->e_phnum * iehdr->e_phentsize | |
7198 | : 0), | |
66631823 CE |
7199 | output_section->alignment_power * opb) |
7200 | == (output_section->vma * opb))) | |
bc67d8a6 | 7201 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
7202 | |
7203 | /* Match up the physical address of the segment with the | |
7204 | LMA address of the output section. */ | |
502794d4 CE |
7205 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr, |
7206 | opb) | |
5e8d7549 | 7207 | || IS_COREFILE_NOTE (segment, section) |
0067a569 | 7208 | || (bed->want_p_paddr_set_to_zero |
502794d4 | 7209 | && IS_CONTAINED_BY_VMA (output_section, segment, opb))) |
252b5132 | 7210 | { |
9933dc52 AM |
7211 | if (matching_lma == NULL |
7212 | || output_section->lma < matching_lma->lma) | |
7213 | matching_lma = output_section; | |
252b5132 RH |
7214 | |
7215 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 7216 | then it does not overlap any other section within that |
252b5132 | 7217 | segment. */ |
0067a569 AM |
7218 | map->sections[isec++] = output_section; |
7219 | } | |
9933dc52 AM |
7220 | else if (suggested_lma == NULL) |
7221 | suggested_lma = output_section; | |
147d51c2 L |
7222 | |
7223 | if (j == section_count) | |
7224 | break; | |
252b5132 RH |
7225 | } |
7226 | } | |
7227 | ||
bc67d8a6 | 7228 | BFD_ASSERT (j == section_count); |
252b5132 RH |
7229 | |
7230 | /* Step Two: Adjust the physical address of the current segment, | |
7231 | if necessary. */ | |
bc67d8a6 | 7232 | if (isec == section_count) |
252b5132 RH |
7233 | { |
7234 | /* All of the sections fitted within the segment as currently | |
7235 | specified. This is the default case. Add the segment to | |
7236 | the list of built segments and carry on to process the next | |
7237 | program header in the input BFD. */ | |
bc67d8a6 | 7238 | map->count = section_count; |
c044fabd KH |
7239 | *pointer_to_map = map; |
7240 | pointer_to_map = &map->next; | |
08a40648 | 7241 | |
5c44b38e | 7242 | if (p_paddr_valid |
30fe1832 AM |
7243 | && !bed->want_p_paddr_set_to_zero) |
7244 | { | |
7245 | bfd_vma hdr_size = 0; | |
7246 | if (map->includes_filehdr) | |
7247 | hdr_size = iehdr->e_ehsize; | |
7248 | if (map->includes_phdrs) | |
7249 | hdr_size += iehdr->e_phnum * iehdr->e_phentsize; | |
7250 | ||
7251 | /* Account for padding before the first section in the | |
7252 | segment. */ | |
502794d4 CE |
7253 | map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb |
7254 | - matching_lma->lma); | |
30fe1832 | 7255 | } |
08a40648 | 7256 | |
252b5132 RH |
7257 | free (sections); |
7258 | continue; | |
7259 | } | |
252b5132 RH |
7260 | else |
7261 | { | |
9933dc52 AM |
7262 | /* Change the current segment's physical address to match |
7263 | the LMA of the first section that fitted, or if no | |
7264 | section fitted, the first section. */ | |
7265 | if (matching_lma == NULL) | |
7266 | matching_lma = suggested_lma; | |
7267 | ||
66631823 | 7268 | map->p_paddr = matching_lma->lma * opb; |
72730e0c | 7269 | |
bc67d8a6 NC |
7270 | /* Offset the segment physical address from the lma |
7271 | to allow for space taken up by elf headers. */ | |
9933dc52 | 7272 | if (map->includes_phdrs) |
010c8431 | 7273 | { |
9933dc52 AM |
7274 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; |
7275 | ||
7276 | /* iehdr->e_phnum is just an estimate of the number | |
7277 | of program headers that we will need. Make a note | |
7278 | here of the number we used and the segment we chose | |
7279 | to hold these headers, so that we can adjust the | |
7280 | offset when we know the correct value. */ | |
7281 | phdr_adjust_num = iehdr->e_phnum; | |
7282 | phdr_adjust_seg = map; | |
010c8431 | 7283 | } |
252b5132 | 7284 | |
9933dc52 | 7285 | if (map->includes_filehdr) |
bc67d8a6 | 7286 | { |
9933dc52 AM |
7287 | bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power; |
7288 | map->p_paddr -= iehdr->e_ehsize; | |
7289 | /* We've subtracted off the size of headers from the | |
7290 | first section lma, but there may have been some | |
7291 | alignment padding before that section too. Try to | |
7292 | account for that by adjusting the segment lma down to | |
7293 | the same alignment. */ | |
7294 | if (segment->p_align != 0 && segment->p_align < align) | |
7295 | align = segment->p_align; | |
66631823 | 7296 | map->p_paddr &= -(align * opb); |
bc67d8a6 | 7297 | } |
252b5132 RH |
7298 | } |
7299 | ||
7300 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 7301 | those that fit to the current segment and removing them from the |
252b5132 RH |
7302 | sections array; but making sure not to leave large gaps. Once all |
7303 | possible sections have been assigned to the current segment it is | |
7304 | added to the list of built segments and if sections still remain | |
7305 | to be assigned, a new segment is constructed before repeating | |
7306 | the loop. */ | |
7307 | isec = 0; | |
7308 | do | |
7309 | { | |
bc67d8a6 | 7310 | map->count = 0; |
9933dc52 | 7311 | suggested_lma = NULL; |
252b5132 RH |
7312 | |
7313 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 7314 | for (j = 0; j < section_count; j++) |
252b5132 | 7315 | { |
bc67d8a6 | 7316 | section = sections[j]; |
252b5132 | 7317 | |
bc67d8a6 | 7318 | if (section == NULL) |
252b5132 RH |
7319 | continue; |
7320 | ||
bc67d8a6 | 7321 | output_section = section->output_section; |
252b5132 | 7322 | |
bc67d8a6 | 7323 | BFD_ASSERT (output_section != NULL); |
c044fabd | 7324 | |
502794d4 CE |
7325 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr, |
7326 | opb) | |
bc67d8a6 | 7327 | || IS_COREFILE_NOTE (segment, section)) |
252b5132 | 7328 | { |
bc67d8a6 | 7329 | if (map->count == 0) |
252b5132 RH |
7330 | { |
7331 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
7332 | the beginning of the segment, then something is |
7333 | wrong. */ | |
9933dc52 AM |
7334 | if (align_power (map->p_paddr |
7335 | + (map->includes_filehdr | |
7336 | ? iehdr->e_ehsize : 0) | |
7337 | + (map->includes_phdrs | |
7338 | ? iehdr->e_phnum * iehdr->e_phentsize | |
7339 | : 0), | |
66631823 CE |
7340 | output_section->alignment_power * opb) |
7341 | != output_section->lma * opb) | |
9aea1e31 | 7342 | goto sorry; |
252b5132 RH |
7343 | } |
7344 | else | |
7345 | { | |
0067a569 | 7346 | asection *prev_sec; |
252b5132 | 7347 | |
bc67d8a6 | 7348 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
7349 | |
7350 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
7351 | and the start of this section is more than |
7352 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 7353 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 7354 | maxpagesize) |
caf47ea6 | 7355 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 7356 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 7357 | > output_section->lma)) |
252b5132 | 7358 | { |
9933dc52 AM |
7359 | if (suggested_lma == NULL) |
7360 | suggested_lma = output_section; | |
252b5132 RH |
7361 | |
7362 | continue; | |
7363 | } | |
7364 | } | |
7365 | ||
bc67d8a6 | 7366 | map->sections[map->count++] = output_section; |
252b5132 RH |
7367 | ++isec; |
7368 | sections[j] = NULL; | |
9933dc52 AM |
7369 | if (segment->p_type == PT_LOAD) |
7370 | section->segment_mark = TRUE; | |
0067a569 | 7371 | } |
9933dc52 AM |
7372 | else if (suggested_lma == NULL) |
7373 | suggested_lma = output_section; | |
252b5132 RH |
7374 | } |
7375 | ||
beab4532 NC |
7376 | /* PR 23932. A corrupt input file may contain sections that cannot |
7377 | be assigned to any segment - because for example they have a | |
9984857c NC |
7378 | negative size - or segments that do not contain any sections. |
7379 | But there are also valid reasons why a segment can be empty. | |
7380 | So allow a count of zero. */ | |
252b5132 RH |
7381 | |
7382 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
7383 | *pointer_to_map = map; |
7384 | pointer_to_map = &map->next; | |
252b5132 | 7385 | |
bc67d8a6 | 7386 | if (isec < section_count) |
252b5132 RH |
7387 | { |
7388 | /* We still have not allocated all of the sections to | |
7389 | segments. Create a new segment here, initialise it | |
7390 | and carry on looping. */ | |
00bee008 | 7391 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
446f7ed5 | 7392 | amt += section_count * sizeof (asection *); |
5964fc3a | 7393 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 7394 | if (map == NULL) |
5ed6aba4 NC |
7395 | { |
7396 | free (sections); | |
7397 | return FALSE; | |
7398 | } | |
252b5132 RH |
7399 | |
7400 | /* Initialise the fields of the segment map. Set the physical | |
7401 | physical address to the LMA of the first section that has | |
7402 | not yet been assigned. */ | |
0067a569 AM |
7403 | map->next = NULL; |
7404 | map->p_type = segment->p_type; | |
7405 | map->p_flags = segment->p_flags; | |
7406 | map->p_flags_valid = 1; | |
66631823 | 7407 | map->p_paddr = suggested_lma->lma * opb; |
5c44b38e | 7408 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 7409 | map->includes_filehdr = 0; |
0067a569 | 7410 | map->includes_phdrs = 0; |
252b5132 | 7411 | } |
9984857c NC |
7412 | |
7413 | continue; | |
7414 | sorry: | |
7415 | bfd_set_error (bfd_error_sorry); | |
7416 | free (sections); | |
7417 | return FALSE; | |
252b5132 | 7418 | } |
bc67d8a6 | 7419 | while (isec < section_count); |
252b5132 RH |
7420 | |
7421 | free (sections); | |
7422 | } | |
7423 | ||
12bd6957 | 7424 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
7425 | |
7426 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 7427 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
7428 | the offset if necessary. */ |
7429 | if (phdr_adjust_seg != NULL) | |
7430 | { | |
7431 | unsigned int count; | |
c044fabd | 7432 | |
bc67d8a6 | 7433 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 7434 | count++; |
252b5132 | 7435 | |
bc67d8a6 NC |
7436 | if (count > phdr_adjust_num) |
7437 | phdr_adjust_seg->p_paddr | |
7438 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
9933dc52 AM |
7439 | |
7440 | for (map = map_first; map != NULL; map = map->next) | |
7441 | if (map->p_type == PT_PHDR) | |
7442 | { | |
7443 | bfd_vma adjust | |
7444 | = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0; | |
7445 | map->p_paddr = phdr_adjust_seg->p_paddr + adjust; | |
7446 | break; | |
7447 | } | |
bc67d8a6 | 7448 | } |
c044fabd | 7449 | |
bc67d8a6 | 7450 | #undef SEGMENT_END |
eecdbe52 | 7451 | #undef SECTION_SIZE |
bc67d8a6 NC |
7452 | #undef IS_CONTAINED_BY_VMA |
7453 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 7454 | #undef IS_NOTE |
252b5132 | 7455 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 7456 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 7457 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
7458 | #undef INCLUDE_SECTION_IN_SEGMENT |
7459 | #undef SEGMENT_AFTER_SEGMENT | |
7460 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 7461 | return TRUE; |
252b5132 RH |
7462 | } |
7463 | ||
84d1d650 L |
7464 | /* Copy ELF program header information. */ |
7465 | ||
7466 | static bfd_boolean | |
7467 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
7468 | { | |
7469 | Elf_Internal_Ehdr *iehdr; | |
7470 | struct elf_segment_map *map; | |
7471 | struct elf_segment_map *map_first; | |
7472 | struct elf_segment_map **pointer_to_map; | |
7473 | Elf_Internal_Phdr *segment; | |
7474 | unsigned int i; | |
7475 | unsigned int num_segments; | |
7476 | bfd_boolean phdr_included = FALSE; | |
88967714 | 7477 | bfd_boolean p_paddr_valid; |
502794d4 | 7478 | unsigned int opb = bfd_octets_per_byte (ibfd, NULL); |
84d1d650 L |
7479 | |
7480 | iehdr = elf_elfheader (ibfd); | |
7481 | ||
7482 | map_first = NULL; | |
7483 | pointer_to_map = &map_first; | |
7484 | ||
88967714 AM |
7485 | /* If all the segment p_paddr fields are zero, don't set |
7486 | map->p_paddr_valid. */ | |
7487 | p_paddr_valid = FALSE; | |
84d1d650 | 7488 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
7489 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7490 | i < num_segments; | |
7491 | i++, segment++) | |
7492 | if (segment->p_paddr != 0) | |
7493 | { | |
7494 | p_paddr_valid = TRUE; | |
7495 | break; | |
7496 | } | |
7497 | ||
84d1d650 L |
7498 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7499 | i < num_segments; | |
7500 | i++, segment++) | |
7501 | { | |
7502 | asection *section; | |
7503 | unsigned int section_count; | |
986f0783 | 7504 | size_t amt; |
84d1d650 | 7505 | Elf_Internal_Shdr *this_hdr; |
53020534 | 7506 | asection *first_section = NULL; |
a76e6f2f | 7507 | asection *lowest_section; |
84d1d650 | 7508 | |
84d1d650 L |
7509 | /* Compute how many sections are in this segment. */ |
7510 | for (section = ibfd->sections, section_count = 0; | |
7511 | section != NULL; | |
7512 | section = section->next) | |
7513 | { | |
7514 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7515 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 7516 | { |
a76e6f2f AM |
7517 | if (first_section == NULL) |
7518 | first_section = section; | |
3271a814 NS |
7519 | section_count++; |
7520 | } | |
84d1d650 L |
7521 | } |
7522 | ||
7523 | /* Allocate a segment map big enough to contain | |
7524 | all of the sections we have selected. */ | |
00bee008 | 7525 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
986f0783 | 7526 | amt += section_count * sizeof (asection *); |
a50b1753 | 7527 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
7528 | if (map == NULL) |
7529 | return FALSE; | |
7530 | ||
7531 | /* Initialize the fields of the output segment map with the | |
7532 | input segment. */ | |
7533 | map->next = NULL; | |
7534 | map->p_type = segment->p_type; | |
7535 | map->p_flags = segment->p_flags; | |
7536 | map->p_flags_valid = 1; | |
7537 | map->p_paddr = segment->p_paddr; | |
88967714 | 7538 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
7539 | map->p_align = segment->p_align; |
7540 | map->p_align_valid = 1; | |
3271a814 | 7541 | map->p_vaddr_offset = 0; |
84d1d650 | 7542 | |
04c3a755 NS |
7543 | if (map->p_type == PT_GNU_RELRO |
7544 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
7545 | { |
7546 | /* The PT_GNU_RELRO segment may contain the first a few | |
7547 | bytes in the .got.plt section even if the whole .got.plt | |
7548 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
7549 | change the size of the PT_GNU_RELRO segment. |
7550 | Similarly, PT_GNU_STACK size is significant on uclinux | |
7551 | systems. */ | |
9433b9b1 | 7552 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
7553 | map->p_size_valid = 1; |
7554 | } | |
7555 | ||
84d1d650 L |
7556 | /* Determine if this segment contains the ELF file header |
7557 | and if it contains the program headers themselves. */ | |
7558 | map->includes_filehdr = (segment->p_offset == 0 | |
7559 | && segment->p_filesz >= iehdr->e_ehsize); | |
7560 | ||
7561 | map->includes_phdrs = 0; | |
7562 | if (! phdr_included || segment->p_type != PT_LOAD) | |
7563 | { | |
7564 | map->includes_phdrs = | |
7565 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7566 | && (segment->p_offset + segment->p_filesz | |
7567 | >= ((bfd_vma) iehdr->e_phoff | |
7568 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
7569 | ||
7570 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
7571 | phdr_included = TRUE; | |
7572 | } | |
7573 | ||
bbefd0a9 | 7574 | lowest_section = NULL; |
84d1d650 L |
7575 | if (section_count != 0) |
7576 | { | |
7577 | unsigned int isec = 0; | |
7578 | ||
53020534 | 7579 | for (section = first_section; |
84d1d650 L |
7580 | section != NULL; |
7581 | section = section->next) | |
7582 | { | |
7583 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7584 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
7585 | { |
7586 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
7587 | if ((section->flags & SEC_ALLOC) != 0) |
7588 | { | |
7589 | bfd_vma seg_off; | |
7590 | ||
bbefd0a9 AM |
7591 | if (lowest_section == NULL |
7592 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
7593 | lowest_section = section; |
7594 | ||
a76e6f2f AM |
7595 | /* Section lmas are set up from PT_LOAD header |
7596 | p_paddr in _bfd_elf_make_section_from_shdr. | |
7597 | If this header has a p_paddr that disagrees | |
7598 | with the section lma, flag the p_paddr as | |
7599 | invalid. */ | |
7600 | if ((section->flags & SEC_LOAD) != 0) | |
7601 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
7602 | else | |
7603 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
502794d4 | 7604 | if (section->lma * opb - segment->p_paddr != seg_off) |
a76e6f2f AM |
7605 | map->p_paddr_valid = FALSE; |
7606 | } | |
53020534 L |
7607 | if (isec == section_count) |
7608 | break; | |
7609 | } | |
84d1d650 L |
7610 | } |
7611 | } | |
7612 | ||
5d695627 | 7613 | if (section_count == 0) |
502794d4 | 7614 | map->p_vaddr_offset = segment->p_vaddr / opb; |
30fe1832 AM |
7615 | else if (map->p_paddr_valid) |
7616 | { | |
7617 | /* Account for padding before the first section in the segment. */ | |
7618 | bfd_vma hdr_size = 0; | |
7619 | if (map->includes_filehdr) | |
7620 | hdr_size = iehdr->e_ehsize; | |
7621 | if (map->includes_phdrs) | |
7622 | hdr_size += iehdr->e_phnum * iehdr->e_phentsize; | |
7623 | ||
502794d4 | 7624 | map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb |
30fe1832 AM |
7625 | - (lowest_section ? lowest_section->lma : 0)); |
7626 | } | |
a76e6f2f | 7627 | |
84d1d650 L |
7628 | map->count = section_count; |
7629 | *pointer_to_map = map; | |
7630 | pointer_to_map = &map->next; | |
7631 | } | |
7632 | ||
12bd6957 | 7633 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
7634 | return TRUE; |
7635 | } | |
7636 | ||
7637 | /* Copy private BFD data. This copies or rewrites ELF program header | |
7638 | information. */ | |
7639 | ||
7640 | static bfd_boolean | |
7641 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
7642 | { | |
84d1d650 L |
7643 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
7644 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7645 | return TRUE; | |
7646 | ||
7647 | if (elf_tdata (ibfd)->phdr == NULL) | |
7648 | return TRUE; | |
7649 | ||
7650 | if (ibfd->xvec == obfd->xvec) | |
7651 | { | |
cb3ff1e5 NC |
7652 | /* Check to see if any sections in the input BFD |
7653 | covered by ELF program header have changed. */ | |
d55ce4e2 | 7654 | Elf_Internal_Phdr *segment; |
84d1d650 L |
7655 | asection *section, *osec; |
7656 | unsigned int i, num_segments; | |
7657 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
7658 | const struct elf_backend_data *bed; |
7659 | ||
7660 | bed = get_elf_backend_data (ibfd); | |
7661 | ||
7662 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
7663 | if (bed->want_p_paddr_set_to_zero) | |
7664 | goto rewrite; | |
84d1d650 L |
7665 | |
7666 | /* Initialize the segment mark field. */ | |
7667 | for (section = obfd->sections; section != NULL; | |
7668 | section = section->next) | |
7669 | section->segment_mark = FALSE; | |
7670 | ||
7671 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
7672 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7673 | i < num_segments; | |
7674 | i++, segment++) | |
7675 | { | |
5f6999aa NC |
7676 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
7677 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
7678 | which severly confuses things, so always regenerate the segment | |
7679 | map in this case. */ | |
7680 | if (segment->p_paddr == 0 | |
7681 | && segment->p_memsz == 0 | |
7682 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 7683 | goto rewrite; |
5f6999aa | 7684 | |
84d1d650 L |
7685 | for (section = ibfd->sections; |
7686 | section != NULL; section = section->next) | |
7687 | { | |
7688 | /* We mark the output section so that we know it comes | |
7689 | from the input BFD. */ | |
7690 | osec = section->output_section; | |
7691 | if (osec) | |
7692 | osec->segment_mark = TRUE; | |
7693 | ||
7694 | /* Check if this section is covered by the segment. */ | |
7695 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7696 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
7697 | { |
7698 | /* FIXME: Check if its output section is changed or | |
7699 | removed. What else do we need to check? */ | |
7700 | if (osec == NULL | |
7701 | || section->flags != osec->flags | |
7702 | || section->lma != osec->lma | |
7703 | || section->vma != osec->vma | |
7704 | || section->size != osec->size | |
7705 | || section->rawsize != osec->rawsize | |
7706 | || section->alignment_power != osec->alignment_power) | |
7707 | goto rewrite; | |
7708 | } | |
7709 | } | |
7710 | } | |
7711 | ||
cb3ff1e5 | 7712 | /* Check to see if any output section do not come from the |
84d1d650 L |
7713 | input BFD. */ |
7714 | for (section = obfd->sections; section != NULL; | |
7715 | section = section->next) | |
7716 | { | |
535b785f | 7717 | if (!section->segment_mark) |
84d1d650 L |
7718 | goto rewrite; |
7719 | else | |
7720 | section->segment_mark = FALSE; | |
7721 | } | |
7722 | ||
7723 | return copy_elf_program_header (ibfd, obfd); | |
7724 | } | |
7725 | ||
dc1e8a47 | 7726 | rewrite: |
f1d85785 L |
7727 | if (ibfd->xvec == obfd->xvec) |
7728 | { | |
7729 | /* When rewriting program header, set the output maxpagesize to | |
7730 | the maximum alignment of input PT_LOAD segments. */ | |
7731 | Elf_Internal_Phdr *segment; | |
7732 | unsigned int i; | |
7733 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
7734 | bfd_vma maxpagesize = 0; | |
7735 | ||
7736 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7737 | i < num_segments; | |
7738 | i++, segment++) | |
7739 | if (segment->p_type == PT_LOAD | |
7740 | && maxpagesize < segment->p_align) | |
c86934ce NC |
7741 | { |
7742 | /* PR 17512: file: f17299af. */ | |
7743 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
695344c0 | 7744 | /* xgettext:c-format */ |
2dcf00ce AM |
7745 | _bfd_error_handler (_("%pB: warning: segment alignment of %#" |
7746 | PRIx64 " is too large"), | |
7747 | ibfd, (uint64_t) segment->p_align); | |
c86934ce NC |
7748 | else |
7749 | maxpagesize = segment->p_align; | |
7750 | } | |
f1d85785 L |
7751 | |
7752 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
7753 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
7754 | } | |
7755 | ||
84d1d650 L |
7756 | return rewrite_elf_program_header (ibfd, obfd); |
7757 | } | |
7758 | ||
ccd2ec6a L |
7759 | /* Initialize private output section information from input section. */ |
7760 | ||
7761 | bfd_boolean | |
7762 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
7763 | asection *isec, | |
7764 | bfd *obfd, | |
7765 | asection *osec, | |
7766 | struct bfd_link_info *link_info) | |
7767 | ||
7768 | { | |
7769 | Elf_Internal_Shdr *ihdr, *ohdr; | |
0e1862bb L |
7770 | bfd_boolean final_link = (link_info != NULL |
7771 | && !bfd_link_relocatable (link_info)); | |
ccd2ec6a L |
7772 | |
7773 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7774 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
7775 | return TRUE; | |
7776 | ||
ba85c43e NC |
7777 | BFD_ASSERT (elf_section_data (osec) != NULL); |
7778 | ||
8c803a2d AM |
7779 | /* If this is a known ABI section, ELF section type and flags may |
7780 | have been set up when OSEC was created. For normal sections we | |
7781 | allow the user to override the type and flags other than | |
7782 | SHF_MASKOS and SHF_MASKPROC. */ | |
7783 | if (elf_section_type (osec) == SHT_PROGBITS | |
7784 | || elf_section_type (osec) == SHT_NOTE | |
7785 | || elf_section_type (osec) == SHT_NOBITS) | |
7786 | elf_section_type (osec) = SHT_NULL; | |
7787 | /* For objcopy and relocatable link, copy the ELF section type from | |
7788 | the input file if the BFD section flags are the same. (If they | |
7789 | are different the user may be doing something like | |
7790 | "objcopy --set-section-flags .text=alloc,data".) For a final | |
7791 | link allow some flags that the linker clears to differ. */ | |
42bb2e33 | 7792 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
7793 | && (osec->flags == isec->flags |
7794 | || (final_link | |
7795 | && ((osec->flags ^ isec->flags) | |
0814be7d | 7796 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 7797 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
7798 | |
7799 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
8c803a2d AM |
7800 | elf_section_flags (osec) = (elf_section_flags (isec) |
7801 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a | 7802 | |
a91e1603 | 7803 | /* Copy sh_info from input for mbind section. */ |
df3a023b AM |
7804 | if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0 |
7805 | && elf_section_flags (isec) & SHF_GNU_MBIND) | |
a91e1603 L |
7806 | elf_section_data (osec)->this_hdr.sh_info |
7807 | = elf_section_data (isec)->this_hdr.sh_info; | |
7808 | ||
ccd2ec6a L |
7809 | /* Set things up for objcopy and relocatable link. The output |
7810 | SHT_GROUP section will have its elf_next_in_group pointing back | |
7811 | to the input group members. Ignore linker created group section. | |
7812 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
7bdf4127 AB |
7813 | if ((link_info == NULL |
7814 | || !link_info->resolve_section_groups) | |
7815 | && (elf_sec_group (isec) == NULL | |
7816 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)) | |
ccd2ec6a | 7817 | { |
7bdf4127 AB |
7818 | if (elf_section_flags (isec) & SHF_GROUP) |
7819 | elf_section_flags (osec) |= SHF_GROUP; | |
7820 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
7821 | elf_section_data (osec)->group = elf_section_data (isec)->group; | |
ccd2ec6a L |
7822 | } |
7823 | ||
7bdf4127 AB |
7824 | /* If not decompress, preserve SHF_COMPRESSED. */ |
7825 | if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0) | |
7826 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7827 | & SHF_COMPRESSED); | |
7828 | ||
ccd2ec6a L |
7829 | ihdr = &elf_section_data (isec)->this_hdr; |
7830 | ||
7831 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
7832 | don't use the output section of the linked-to section since it | |
7833 | may be NULL at this point. */ | |
7834 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
7835 | { | |
7836 | ohdr = &elf_section_data (osec)->this_hdr; | |
7837 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
7838 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
7839 | } | |
7840 | ||
7841 | osec->use_rela_p = isec->use_rela_p; | |
7842 | ||
7843 | return TRUE; | |
7844 | } | |
7845 | ||
252b5132 RH |
7846 | /* Copy private section information. This copies over the entsize |
7847 | field, and sometimes the info field. */ | |
7848 | ||
b34976b6 | 7849 | bfd_boolean |
217aa764 AM |
7850 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
7851 | asection *isec, | |
7852 | bfd *obfd, | |
7853 | asection *osec) | |
252b5132 RH |
7854 | { |
7855 | Elf_Internal_Shdr *ihdr, *ohdr; | |
7856 | ||
7857 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7858 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 7859 | return TRUE; |
252b5132 | 7860 | |
252b5132 RH |
7861 | ihdr = &elf_section_data (isec)->this_hdr; |
7862 | ohdr = &elf_section_data (osec)->this_hdr; | |
7863 | ||
7864 | ohdr->sh_entsize = ihdr->sh_entsize; | |
7865 | ||
7866 | if (ihdr->sh_type == SHT_SYMTAB | |
7867 | || ihdr->sh_type == SHT_DYNSYM | |
7868 | || ihdr->sh_type == SHT_GNU_verneed | |
7869 | || ihdr->sh_type == SHT_GNU_verdef) | |
7870 | ohdr->sh_info = ihdr->sh_info; | |
7871 | ||
ccd2ec6a L |
7872 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
7873 | NULL); | |
252b5132 RH |
7874 | } |
7875 | ||
d0bf826b AM |
7876 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
7877 | necessary if we are removing either the SHT_GROUP section or any of | |
7878 | the group member sections. DISCARDED is the value that a section's | |
7879 | output_section has if the section will be discarded, NULL when this | |
7880 | function is called from objcopy, bfd_abs_section_ptr when called | |
7881 | from the linker. */ | |
80fccad2 BW |
7882 | |
7883 | bfd_boolean | |
d0bf826b | 7884 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 7885 | { |
30288845 AM |
7886 | asection *isec; |
7887 | ||
30288845 | 7888 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 7889 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
7890 | { |
7891 | asection *first = elf_next_in_group (isec); | |
7892 | asection *s = first; | |
d0bf826b AM |
7893 | bfd_size_type removed = 0; |
7894 | ||
30288845 AM |
7895 | while (s != NULL) |
7896 | { | |
415f38a6 AM |
7897 | /* If this member section is being output but the |
7898 | SHT_GROUP section is not, then clear the group info | |
7899 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
7900 | if (s->output_section != discarded |
7901 | && isec->output_section == discarded) | |
30288845 AM |
7902 | { |
7903 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
7904 | elf_group_name (s->output_section) = NULL; | |
7905 | } | |
415f38a6 AM |
7906 | /* Conversely, if the member section is not being output |
7907 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
7908 | else if (s->output_section == discarded |
7909 | && isec->output_section != discarded) | |
6e5e9d58 AM |
7910 | { |
7911 | struct bfd_elf_section_data *elf_sec = elf_section_data (s); | |
7912 | removed += 4; | |
7913 | if (elf_sec->rel.hdr != NULL | |
7914 | && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0) | |
7915 | removed += 4; | |
7916 | if (elf_sec->rela.hdr != NULL | |
7917 | && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0) | |
7918 | removed += 4; | |
7919 | } | |
30288845 AM |
7920 | s = elf_next_in_group (s); |
7921 | if (s == first) | |
7922 | break; | |
7923 | } | |
d0bf826b AM |
7924 | if (removed != 0) |
7925 | { | |
7926 | if (discarded != NULL) | |
7927 | { | |
7928 | /* If we've been called for ld -r, then we need to | |
6e5e9d58 | 7929 | adjust the input section size. */ |
d0bf826b AM |
7930 | if (isec->rawsize == 0) |
7931 | isec->rawsize = isec->size; | |
7932 | isec->size = isec->rawsize - removed; | |
6e5e9d58 AM |
7933 | if (isec->size <= 4) |
7934 | { | |
7935 | isec->size = 0; | |
7936 | isec->flags |= SEC_EXCLUDE; | |
7937 | } | |
d0bf826b AM |
7938 | } |
7939 | else | |
7940 | { | |
7941 | /* Adjust the output section size when called from | |
7942 | objcopy. */ | |
7943 | isec->output_section->size -= removed; | |
6e5e9d58 AM |
7944 | if (isec->output_section->size <= 4) |
7945 | { | |
7946 | isec->output_section->size = 0; | |
7947 | isec->output_section->flags |= SEC_EXCLUDE; | |
7948 | } | |
d0bf826b AM |
7949 | } |
7950 | } | |
30288845 AM |
7951 | } |
7952 | ||
80fccad2 BW |
7953 | return TRUE; |
7954 | } | |
7955 | ||
d0bf826b AM |
7956 | /* Copy private header information. */ |
7957 | ||
7958 | bfd_boolean | |
7959 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
7960 | { | |
7961 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7962 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7963 | return TRUE; | |
7964 | ||
7965 | /* Copy over private BFD data if it has not already been copied. | |
7966 | This must be done here, rather than in the copy_private_bfd_data | |
7967 | entry point, because the latter is called after the section | |
7968 | contents have been set, which means that the program headers have | |
7969 | already been worked out. */ | |
12bd6957 | 7970 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
7971 | { |
7972 | if (! copy_private_bfd_data (ibfd, obfd)) | |
7973 | return FALSE; | |
7974 | } | |
7975 | ||
7976 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
7977 | } | |
7978 | ||
252b5132 RH |
7979 | /* Copy private symbol information. If this symbol is in a section |
7980 | which we did not map into a BFD section, try to map the section | |
7981 | index correctly. We use special macro definitions for the mapped | |
7982 | section indices; these definitions are interpreted by the | |
7983 | swap_out_syms function. */ | |
7984 | ||
9ad5cbcf AM |
7985 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
7986 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
7987 | #define MAP_STRTAB (SHN_HIOS + 3) | |
7988 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
7989 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 7990 | |
b34976b6 | 7991 | bfd_boolean |
217aa764 AM |
7992 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
7993 | asymbol *isymarg, | |
7994 | bfd *obfd, | |
7995 | asymbol *osymarg) | |
252b5132 RH |
7996 | { |
7997 | elf_symbol_type *isym, *osym; | |
7998 | ||
7999 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
8000 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 8001 | return TRUE; |
252b5132 RH |
8002 | |
8003 | isym = elf_symbol_from (ibfd, isymarg); | |
8004 | osym = elf_symbol_from (obfd, osymarg); | |
8005 | ||
8006 | if (isym != NULL | |
8424d8f5 | 8007 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
8008 | && osym != NULL |
8009 | && bfd_is_abs_section (isym->symbol.section)) | |
8010 | { | |
8011 | unsigned int shndx; | |
8012 | ||
8013 | shndx = isym->internal_elf_sym.st_shndx; | |
8014 | if (shndx == elf_onesymtab (ibfd)) | |
8015 | shndx = MAP_ONESYMTAB; | |
8016 | else if (shndx == elf_dynsymtab (ibfd)) | |
8017 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 8018 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 8019 | shndx = MAP_STRTAB; |
12bd6957 | 8020 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 8021 | shndx = MAP_SHSTRTAB; |
6a40cf0c | 8022 | else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) |
9ad5cbcf | 8023 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
8024 | osym->internal_elf_sym.st_shndx = shndx; |
8025 | } | |
8026 | ||
b34976b6 | 8027 | return TRUE; |
252b5132 RH |
8028 | } |
8029 | ||
8030 | /* Swap out the symbols. */ | |
8031 | ||
b34976b6 | 8032 | static bfd_boolean |
217aa764 | 8033 | swap_out_syms (bfd *abfd, |
ef10c3ac | 8034 | struct elf_strtab_hash **sttp, |
217aa764 | 8035 | int relocatable_p) |
252b5132 | 8036 | { |
9c5bfbb7 | 8037 | const struct elf_backend_data *bed; |
1f4361a7 | 8038 | unsigned int symcount; |
079e9a2f | 8039 | asymbol **syms; |
ef10c3ac | 8040 | struct elf_strtab_hash *stt; |
079e9a2f | 8041 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 8042 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 8043 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 8044 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
8045 | bfd_byte *outbound_syms; |
8046 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
8047 | unsigned long outbound_syms_index; |
8048 | unsigned long outbound_shndx_index; | |
1f4361a7 | 8049 | unsigned int idx; |
12bd6957 | 8050 | unsigned int num_locals; |
1f4361a7 | 8051 | size_t amt; |
174fd7f9 | 8052 | bfd_boolean name_local_sections; |
252b5132 | 8053 | |
12bd6957 | 8054 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 8055 | return FALSE; |
252b5132 | 8056 | |
c044fabd | 8057 | /* Dump out the symtabs. */ |
ef10c3ac | 8058 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 8059 | if (stt == NULL) |
b34976b6 | 8060 | return FALSE; |
252b5132 | 8061 | |
079e9a2f AM |
8062 | bed = get_elf_backend_data (abfd); |
8063 | symcount = bfd_get_symcount (abfd); | |
8064 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
8065 | symtab_hdr->sh_type = SHT_SYMTAB; | |
8066 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
8067 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 8068 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 8069 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
8070 | |
8071 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
8072 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
8073 | ||
ef10c3ac | 8074 | /* Allocate buffer to swap out the .strtab section. */ |
1f4361a7 AM |
8075 | if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt) |
8076 | || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL) | |
ef10c3ac | 8077 | { |
1f4361a7 | 8078 | bfd_set_error (bfd_error_no_memory); |
ef10c3ac L |
8079 | _bfd_elf_strtab_free (stt); |
8080 | return FALSE; | |
8081 | } | |
8082 | ||
1f4361a7 AM |
8083 | if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt) |
8084 | || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL) | |
5ed6aba4 | 8085 | { |
1f4361a7 AM |
8086 | error_no_mem: |
8087 | bfd_set_error (bfd_error_no_memory); | |
8088 | error_return: | |
ef10c3ac | 8089 | free (symstrtab); |
1f4361a7 | 8090 | _bfd_elf_strtab_free (stt); |
5ed6aba4 NC |
8091 | return FALSE; |
8092 | } | |
217aa764 | 8093 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 8094 | outbound_syms_index = 0; |
252b5132 | 8095 | |
9ad5cbcf | 8096 | outbound_shndx = NULL; |
ef10c3ac | 8097 | outbound_shndx_index = 0; |
6a40cf0c NC |
8098 | |
8099 | if (elf_symtab_shndx_list (abfd)) | |
9ad5cbcf | 8100 | { |
6a40cf0c NC |
8101 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
8102 | if (symtab_shndx_hdr->sh_name != 0) | |
8103 | { | |
1f4361a7 AM |
8104 | if (_bfd_mul_overflow (symcount + 1, |
8105 | sizeof (Elf_External_Sym_Shndx), &amt)) | |
8106 | goto error_no_mem; | |
8107 | outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt); | |
6a40cf0c NC |
8108 | if (outbound_shndx == NULL) |
8109 | goto error_return; | |
5ed6aba4 | 8110 | |
6a40cf0c NC |
8111 | symtab_shndx_hdr->contents = outbound_shndx; |
8112 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
8113 | symtab_shndx_hdr->sh_size = amt; | |
8114 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
8115 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
8116 | } | |
8117 | /* FIXME: What about any other headers in the list ? */ | |
9ad5cbcf AM |
8118 | } |
8119 | ||
589e6347 | 8120 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
8121 | { |
8122 | /* Fill in zeroth symbol and swap it out. */ | |
8123 | Elf_Internal_Sym sym; | |
8124 | sym.st_name = 0; | |
8125 | sym.st_value = 0; | |
8126 | sym.st_size = 0; | |
8127 | sym.st_info = 0; | |
8128 | sym.st_other = 0; | |
8129 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 8130 | sym.st_target_internal = 0; |
ef10c3ac L |
8131 | symstrtab[0].sym = sym; |
8132 | symstrtab[0].dest_index = outbound_syms_index; | |
8133 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
8134 | outbound_syms_index++; | |
9ad5cbcf | 8135 | if (outbound_shndx != NULL) |
ef10c3ac | 8136 | outbound_shndx_index++; |
079e9a2f | 8137 | } |
252b5132 | 8138 | |
174fd7f9 RS |
8139 | name_local_sections |
8140 | = (bed->elf_backend_name_local_section_symbols | |
8141 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
8142 | ||
079e9a2f | 8143 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 8144 | for (idx = 0; idx < symcount;) |
252b5132 | 8145 | { |
252b5132 | 8146 | Elf_Internal_Sym sym; |
079e9a2f AM |
8147 | bfd_vma value = syms[idx]->value; |
8148 | elf_symbol_type *type_ptr; | |
8149 | flagword flags = syms[idx]->flags; | |
8150 | int type; | |
252b5132 | 8151 | |
174fd7f9 RS |
8152 | if (!name_local_sections |
8153 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
8154 | { |
8155 | /* Local section symbols have no name. */ | |
ef10c3ac | 8156 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
8157 | } |
8158 | else | |
8159 | { | |
ef10c3ac L |
8160 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
8161 | to get the final offset for st_name. */ | |
8162 | sym.st_name | |
8163 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
8164 | FALSE); | |
079e9a2f | 8165 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 8166 | goto error_return; |
079e9a2f | 8167 | } |
252b5132 | 8168 | |
079e9a2f | 8169 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 8170 | |
079e9a2f AM |
8171 | if ((flags & BSF_SECTION_SYM) == 0 |
8172 | && bfd_is_com_section (syms[idx]->section)) | |
8173 | { | |
8174 | /* ELF common symbols put the alignment into the `value' field, | |
8175 | and the size into the `size' field. This is backwards from | |
8176 | how BFD handles it, so reverse it here. */ | |
8177 | sym.st_size = value; | |
8178 | if (type_ptr == NULL | |
8179 | || type_ptr->internal_elf_sym.st_value == 0) | |
8180 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
8181 | else | |
8182 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
8183 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
8184 | (abfd, syms[idx]->section); | |
8185 | } | |
8186 | else | |
8187 | { | |
8188 | asection *sec = syms[idx]->section; | |
cb33740c | 8189 | unsigned int shndx; |
252b5132 | 8190 | |
079e9a2f AM |
8191 | if (sec->output_section) |
8192 | { | |
8193 | value += sec->output_offset; | |
8194 | sec = sec->output_section; | |
8195 | } | |
589e6347 | 8196 | |
079e9a2f AM |
8197 | /* Don't add in the section vma for relocatable output. */ |
8198 | if (! relocatable_p) | |
8199 | value += sec->vma; | |
8200 | sym.st_value = value; | |
8201 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
8202 | ||
8203 | if (bfd_is_abs_section (sec) | |
8204 | && type_ptr != NULL | |
8205 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
8206 | { | |
8207 | /* This symbol is in a real ELF section which we did | |
8208 | not create as a BFD section. Undo the mapping done | |
8209 | by copy_private_symbol_data. */ | |
8210 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
8211 | switch (shndx) | |
8212 | { | |
8213 | case MAP_ONESYMTAB: | |
8214 | shndx = elf_onesymtab (abfd); | |
8215 | break; | |
8216 | case MAP_DYNSYMTAB: | |
8217 | shndx = elf_dynsymtab (abfd); | |
8218 | break; | |
8219 | case MAP_STRTAB: | |
12bd6957 | 8220 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
8221 | break; |
8222 | case MAP_SHSTRTAB: | |
12bd6957 | 8223 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 8224 | break; |
9ad5cbcf | 8225 | case MAP_SYM_SHNDX: |
6a40cf0c NC |
8226 | if (elf_symtab_shndx_list (abfd)) |
8227 | shndx = elf_symtab_shndx_list (abfd)->ndx; | |
9ad5cbcf | 8228 | break; |
00e49dff NC |
8229 | case SHN_COMMON: |
8230 | case SHN_ABS: | |
15bc576a | 8231 | shndx = SHN_ABS; |
079e9a2f | 8232 | break; |
00e49dff NC |
8233 | default: |
8234 | if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS) | |
8235 | { | |
8236 | if (bed->symbol_section_index) | |
8237 | shndx = bed->symbol_section_index (abfd, type_ptr); | |
8238 | /* Otherwise just leave the index alone. */ | |
8239 | } | |
8240 | else | |
8241 | { | |
8242 | if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE) | |
8243 | _bfd_error_handler (_("%pB: \ | |
8244 | Unable to handle section index %x in ELF symbol. Using ABS instead."), | |
8245 | abfd, shndx); | |
8246 | shndx = SHN_ABS; | |
8247 | } | |
8248 | break; | |
079e9a2f AM |
8249 | } |
8250 | } | |
8251 | else | |
8252 | { | |
8253 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 8254 | |
cb33740c | 8255 | if (shndx == SHN_BAD) |
079e9a2f AM |
8256 | { |
8257 | asection *sec2; | |
8258 | ||
8259 | /* Writing this would be a hell of a lot easier if | |
8260 | we had some decent documentation on bfd, and | |
8261 | knew what to expect of the library, and what to | |
8262 | demand of applications. For example, it | |
8263 | appears that `objcopy' might not set the | |
8264 | section of a symbol to be a section that is | |
8265 | actually in the output file. */ | |
8266 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
5df1bc57 AM |
8267 | if (sec2 != NULL) |
8268 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
8269 | if (shndx == SHN_BAD) | |
589e6347 | 8270 | { |
695344c0 | 8271 | /* xgettext:c-format */ |
9793eb77 AM |
8272 | _bfd_error_handler |
8273 | (_("unable to find equivalent output section" | |
8274 | " for symbol '%s' from section '%s'"), | |
8275 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
8276 | sec->name); | |
811072d8 | 8277 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 8278 | goto error_return; |
589e6347 | 8279 | } |
079e9a2f AM |
8280 | } |
8281 | } | |
252b5132 | 8282 | |
079e9a2f AM |
8283 | sym.st_shndx = shndx; |
8284 | } | |
252b5132 | 8285 | |
13ae64f3 JJ |
8286 | if ((flags & BSF_THREAD_LOCAL) != 0) |
8287 | type = STT_TLS; | |
d8045f23 NC |
8288 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
8289 | type = STT_GNU_IFUNC; | |
13ae64f3 | 8290 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
8291 | type = STT_FUNC; |
8292 | else if ((flags & BSF_OBJECT) != 0) | |
8293 | type = STT_OBJECT; | |
d9352518 DB |
8294 | else if ((flags & BSF_RELC) != 0) |
8295 | type = STT_RELC; | |
8296 | else if ((flags & BSF_SRELC) != 0) | |
8297 | type = STT_SRELC; | |
079e9a2f AM |
8298 | else |
8299 | type = STT_NOTYPE; | |
252b5132 | 8300 | |
13ae64f3 JJ |
8301 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
8302 | type = STT_TLS; | |
8303 | ||
589e6347 | 8304 | /* Processor-specific types. */ |
079e9a2f AM |
8305 | if (type_ptr != NULL |
8306 | && bed->elf_backend_get_symbol_type) | |
8307 | type = ((*bed->elf_backend_get_symbol_type) | |
8308 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 8309 | |
079e9a2f AM |
8310 | if (flags & BSF_SECTION_SYM) |
8311 | { | |
8312 | if (flags & BSF_GLOBAL) | |
8313 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
8314 | else | |
8315 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8316 | } | |
8317 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 8318 | { |
b8871f35 L |
8319 | if (type != STT_TLS) |
8320 | { | |
8321 | if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) | |
8322 | type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) | |
8323 | ? STT_COMMON : STT_OBJECT); | |
8324 | else | |
8325 | type = ((flags & BSF_ELF_COMMON) != 0 | |
8326 | ? STT_COMMON : STT_OBJECT); | |
8327 | } | |
8328 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
0a40daed | 8329 | } |
079e9a2f AM |
8330 | else if (bfd_is_und_section (syms[idx]->section)) |
8331 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
8332 | ? STB_WEAK | |
8333 | : STB_GLOBAL), | |
8334 | type); | |
8335 | else if (flags & BSF_FILE) | |
8336 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
8337 | else | |
8338 | { | |
8339 | int bind = STB_LOCAL; | |
252b5132 | 8340 | |
079e9a2f AM |
8341 | if (flags & BSF_LOCAL) |
8342 | bind = STB_LOCAL; | |
3e7a7d11 NC |
8343 | else if (flags & BSF_GNU_UNIQUE) |
8344 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
8345 | else if (flags & BSF_WEAK) |
8346 | bind = STB_WEAK; | |
8347 | else if (flags & BSF_GLOBAL) | |
8348 | bind = STB_GLOBAL; | |
252b5132 | 8349 | |
079e9a2f AM |
8350 | sym.st_info = ELF_ST_INFO (bind, type); |
8351 | } | |
252b5132 | 8352 | |
079e9a2f | 8353 | if (type_ptr != NULL) |
35fc36a8 RS |
8354 | { |
8355 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
8356 | sym.st_target_internal | |
8357 | = type_ptr->internal_elf_sym.st_target_internal; | |
8358 | } | |
079e9a2f | 8359 | else |
35fc36a8 RS |
8360 | { |
8361 | sym.st_other = 0; | |
8362 | sym.st_target_internal = 0; | |
8363 | } | |
252b5132 | 8364 | |
ef10c3ac L |
8365 | idx++; |
8366 | symstrtab[idx].sym = sym; | |
8367 | symstrtab[idx].dest_index = outbound_syms_index; | |
8368 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
8369 | ||
8370 | outbound_syms_index++; | |
9ad5cbcf | 8371 | if (outbound_shndx != NULL) |
ef10c3ac L |
8372 | outbound_shndx_index++; |
8373 | } | |
8374 | ||
8375 | /* Finalize the .strtab section. */ | |
8376 | _bfd_elf_strtab_finalize (stt); | |
8377 | ||
8378 | /* Swap out the .strtab section. */ | |
8379 | for (idx = 0; idx <= symcount; idx++) | |
8380 | { | |
8381 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
8382 | if (elfsym->sym.st_name == (unsigned long) -1) | |
8383 | elfsym->sym.st_name = 0; | |
8384 | else | |
8385 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
8386 | elfsym->sym.st_name); | |
8387 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
8388 | (outbound_syms | |
8389 | + (elfsym->dest_index | |
8390 | * bed->s->sizeof_sym)), | |
8391 | (outbound_shndx | |
8392 | + (elfsym->destshndx_index | |
8393 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 8394 | } |
ef10c3ac | 8395 | free (symstrtab); |
252b5132 | 8396 | |
079e9a2f | 8397 | *sttp = stt; |
ef10c3ac | 8398 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 8399 | symstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 8400 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
079e9a2f AM |
8401 | symstrtab_hdr->sh_addr = 0; |
8402 | symstrtab_hdr->sh_entsize = 0; | |
8403 | symstrtab_hdr->sh_link = 0; | |
8404 | symstrtab_hdr->sh_info = 0; | |
8405 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 8406 | |
b34976b6 | 8407 | return TRUE; |
252b5132 RH |
8408 | } |
8409 | ||
8410 | /* Return the number of bytes required to hold the symtab vector. | |
8411 | ||
8412 | Note that we base it on the count plus 1, since we will null terminate | |
8413 | the vector allocated based on this size. However, the ELF symbol table | |
8414 | always has a dummy entry as symbol #0, so it ends up even. */ | |
8415 | ||
8416 | long | |
217aa764 | 8417 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 | 8418 | { |
3a551c7a | 8419 | bfd_size_type symcount; |
252b5132 RH |
8420 | long symtab_size; |
8421 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
8422 | ||
8423 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3a551c7a AM |
8424 | if (symcount >= LONG_MAX / sizeof (asymbol *)) |
8425 | { | |
8426 | bfd_set_error (bfd_error_file_too_big); | |
8427 | return -1; | |
8428 | } | |
b99d1833 AM |
8429 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8430 | if (symcount > 0) | |
8431 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8432 | |
8433 | return symtab_size; | |
8434 | } | |
8435 | ||
8436 | long | |
217aa764 | 8437 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 | 8438 | { |
3a551c7a | 8439 | bfd_size_type symcount; |
252b5132 RH |
8440 | long symtab_size; |
8441 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
8442 | ||
8443 | if (elf_dynsymtab (abfd) == 0) | |
8444 | { | |
8445 | bfd_set_error (bfd_error_invalid_operation); | |
8446 | return -1; | |
8447 | } | |
8448 | ||
8449 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3a551c7a AM |
8450 | if (symcount >= LONG_MAX / sizeof (asymbol *)) |
8451 | { | |
8452 | bfd_set_error (bfd_error_file_too_big); | |
8453 | return -1; | |
8454 | } | |
b99d1833 AM |
8455 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8456 | if (symcount > 0) | |
8457 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8458 | |
8459 | return symtab_size; | |
8460 | } | |
8461 | ||
8462 | long | |
217aa764 AM |
8463 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
8464 | sec_ptr asect) | |
252b5132 | 8465 | { |
242a1159 | 8466 | #if SIZEOF_LONG == SIZEOF_INT |
7a6e0d89 AM |
8467 | if (asect->reloc_count >= LONG_MAX / sizeof (arelent *)) |
8468 | { | |
8469 | bfd_set_error (bfd_error_file_too_big); | |
8470 | return -1; | |
8471 | } | |
242a1159 | 8472 | #endif |
252b5132 RH |
8473 | return (asect->reloc_count + 1) * sizeof (arelent *); |
8474 | } | |
8475 | ||
8476 | /* Canonicalize the relocs. */ | |
8477 | ||
8478 | long | |
217aa764 AM |
8479 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
8480 | sec_ptr section, | |
8481 | arelent **relptr, | |
8482 | asymbol **symbols) | |
252b5132 RH |
8483 | { |
8484 | arelent *tblptr; | |
8485 | unsigned int i; | |
9c5bfbb7 | 8486 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 8487 | |
b34976b6 | 8488 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
8489 | return -1; |
8490 | ||
8491 | tblptr = section->relocation; | |
8492 | for (i = 0; i < section->reloc_count; i++) | |
8493 | *relptr++ = tblptr++; | |
8494 | ||
8495 | *relptr = NULL; | |
8496 | ||
8497 | return section->reloc_count; | |
8498 | } | |
8499 | ||
8500 | long | |
6cee3f79 | 8501 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 8502 | { |
9c5bfbb7 | 8503 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8504 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
8505 | |
8506 | if (symcount >= 0) | |
ed48ec2e | 8507 | abfd->symcount = symcount; |
252b5132 RH |
8508 | return symcount; |
8509 | } | |
8510 | ||
8511 | long | |
217aa764 AM |
8512 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
8513 | asymbol **allocation) | |
252b5132 | 8514 | { |
9c5bfbb7 | 8515 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8516 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
8517 | |
8518 | if (symcount >= 0) | |
ed48ec2e | 8519 | abfd->dynsymcount = symcount; |
1f70368c | 8520 | return symcount; |
252b5132 RH |
8521 | } |
8522 | ||
8615f3f2 AM |
8523 | /* Return the size required for the dynamic reloc entries. Any loadable |
8524 | section that was actually installed in the BFD, and has type SHT_REL | |
8525 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
8526 | dynamic reloc section. */ | |
252b5132 RH |
8527 | |
8528 | long | |
217aa764 | 8529 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 | 8530 | { |
3a551c7a | 8531 | bfd_size_type count; |
252b5132 RH |
8532 | asection *s; |
8533 | ||
8534 | if (elf_dynsymtab (abfd) == 0) | |
8535 | { | |
8536 | bfd_set_error (bfd_error_invalid_operation); | |
8537 | return -1; | |
8538 | } | |
8539 | ||
3a551c7a | 8540 | count = 1; |
252b5132 | 8541 | for (s = abfd->sections; s != NULL; s = s->next) |
266b05cf | 8542 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8543 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8544 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
3a551c7a AM |
8545 | { |
8546 | count += s->size / elf_section_data (s)->this_hdr.sh_entsize; | |
8547 | if (count > LONG_MAX / sizeof (arelent *)) | |
8548 | { | |
8549 | bfd_set_error (bfd_error_file_too_big); | |
8550 | return -1; | |
8551 | } | |
8552 | } | |
8553 | return count * sizeof (arelent *); | |
252b5132 RH |
8554 | } |
8555 | ||
8615f3f2 AM |
8556 | /* Canonicalize the dynamic relocation entries. Note that we return the |
8557 | dynamic relocations as a single block, although they are actually | |
8558 | associated with particular sections; the interface, which was | |
8559 | designed for SunOS style shared libraries, expects that there is only | |
8560 | one set of dynamic relocs. Any loadable section that was actually | |
8561 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
8562 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
8563 | |
8564 | long | |
217aa764 AM |
8565 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
8566 | arelent **storage, | |
8567 | asymbol **syms) | |
252b5132 | 8568 | { |
217aa764 | 8569 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
8570 | asection *s; |
8571 | long ret; | |
8572 | ||
8573 | if (elf_dynsymtab (abfd) == 0) | |
8574 | { | |
8575 | bfd_set_error (bfd_error_invalid_operation); | |
8576 | return -1; | |
8577 | } | |
8578 | ||
8579 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
8580 | ret = 0; | |
8581 | for (s = abfd->sections; s != NULL; s = s->next) | |
8582 | { | |
266b05cf | 8583 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8584 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8585 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
8586 | { | |
8587 | arelent *p; | |
8588 | long count, i; | |
8589 | ||
b34976b6 | 8590 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 8591 | return -1; |
eea6121a | 8592 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
8593 | p = s->relocation; |
8594 | for (i = 0; i < count; i++) | |
8595 | *storage++ = p++; | |
8596 | ret += count; | |
8597 | } | |
8598 | } | |
8599 | ||
8600 | *storage = NULL; | |
8601 | ||
8602 | return ret; | |
8603 | } | |
8604 | \f | |
8605 | /* Read in the version information. */ | |
8606 | ||
b34976b6 | 8607 | bfd_boolean |
fc0e6df6 | 8608 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
8609 | { |
8610 | bfd_byte *contents = NULL; | |
fc0e6df6 | 8611 | unsigned int freeidx = 0; |
1f4361a7 | 8612 | size_t amt; |
fc0e6df6 PB |
8613 | |
8614 | if (elf_dynverref (abfd) != 0) | |
8615 | { | |
8616 | Elf_Internal_Shdr *hdr; | |
8617 | Elf_External_Verneed *everneed; | |
8618 | Elf_Internal_Verneed *iverneed; | |
8619 | unsigned int i; | |
d0fb9a8d | 8620 | bfd_byte *contents_end; |
fc0e6df6 PB |
8621 | |
8622 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
8623 | ||
bd61e135 AM |
8624 | if (hdr->sh_info == 0 |
8625 | || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed)) | |
d0fb9a8d | 8626 | { |
dc1e8a47 | 8627 | error_return_bad_verref: |
4eca0228 | 8628 | _bfd_error_handler |
871b3ab2 | 8629 | (_("%pB: .gnu.version_r invalid entry"), abfd); |
601a03ba | 8630 | bfd_set_error (bfd_error_bad_value); |
dc1e8a47 | 8631 | error_return_verref: |
d0fb9a8d JJ |
8632 | elf_tdata (abfd)->verref = NULL; |
8633 | elf_tdata (abfd)->cverrefs = 0; | |
8634 | goto error_return; | |
8635 | } | |
601a03ba | 8636 | |
2bb3687b AM |
8637 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) |
8638 | goto error_return_verref; | |
8639 | contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size); | |
8640 | if (contents == NULL) | |
d0fb9a8d | 8641 | goto error_return_verref; |
fc0e6df6 | 8642 | |
1f4361a7 AM |
8643 | if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt)) |
8644 | { | |
8645 | bfd_set_error (bfd_error_file_too_big); | |
8646 | goto error_return_verref; | |
8647 | } | |
8648 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt); | |
601a03ba | 8649 | if (elf_tdata (abfd)->verref == NULL) |
d0fb9a8d JJ |
8650 | goto error_return_verref; |
8651 | ||
8652 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
8653 | == sizeof (Elf_External_Vernaux)); | |
8654 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
8655 | everneed = (Elf_External_Verneed *) contents; |
8656 | iverneed = elf_tdata (abfd)->verref; | |
8657 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
8658 | { | |
8659 | Elf_External_Vernaux *evernaux; | |
8660 | Elf_Internal_Vernaux *ivernaux; | |
8661 | unsigned int j; | |
8662 | ||
8663 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
8664 | ||
8665 | iverneed->vn_bfd = abfd; | |
8666 | ||
8667 | iverneed->vn_filename = | |
8668 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8669 | iverneed->vn_file); | |
8670 | if (iverneed->vn_filename == NULL) | |
601a03ba | 8671 | goto error_return_bad_verref; |
fc0e6df6 | 8672 | |
d0fb9a8d JJ |
8673 | if (iverneed->vn_cnt == 0) |
8674 | iverneed->vn_auxptr = NULL; | |
8675 | else | |
8676 | { | |
1f4361a7 AM |
8677 | if (_bfd_mul_overflow (iverneed->vn_cnt, |
8678 | sizeof (Elf_Internal_Vernaux), &amt)) | |
8679 | { | |
8680 | bfd_set_error (bfd_error_file_too_big); | |
8681 | goto error_return_verref; | |
8682 | } | |
a50b1753 | 8683 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
1f4361a7 | 8684 | bfd_alloc (abfd, amt); |
d0fb9a8d JJ |
8685 | if (iverneed->vn_auxptr == NULL) |
8686 | goto error_return_verref; | |
8687 | } | |
8688 | ||
8689 | if (iverneed->vn_aux | |
8690 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8691 | goto error_return_bad_verref; |
fc0e6df6 PB |
8692 | |
8693 | evernaux = ((Elf_External_Vernaux *) | |
8694 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
8695 | ivernaux = iverneed->vn_auxptr; | |
8696 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
8697 | { | |
8698 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
8699 | ||
8700 | ivernaux->vna_nodename = | |
8701 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8702 | ivernaux->vna_name); | |
8703 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 8704 | goto error_return_bad_verref; |
fc0e6df6 | 8705 | |
25ff461f AM |
8706 | if (ivernaux->vna_other > freeidx) |
8707 | freeidx = ivernaux->vna_other; | |
8708 | ||
8709 | ivernaux->vna_nextptr = NULL; | |
8710 | if (ivernaux->vna_next == 0) | |
8711 | { | |
8712 | iverneed->vn_cnt = j + 1; | |
8713 | break; | |
8714 | } | |
fc0e6df6 PB |
8715 | if (j + 1 < iverneed->vn_cnt) |
8716 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 8717 | |
d0fb9a8d JJ |
8718 | if (ivernaux->vna_next |
8719 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 8720 | goto error_return_bad_verref; |
d0fb9a8d | 8721 | |
fc0e6df6 PB |
8722 | evernaux = ((Elf_External_Vernaux *) |
8723 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
8724 | } |
8725 | ||
25ff461f AM |
8726 | iverneed->vn_nextref = NULL; |
8727 | if (iverneed->vn_next == 0) | |
8728 | break; | |
fc0e6df6 PB |
8729 | if (i + 1 < hdr->sh_info) |
8730 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 8731 | |
d0fb9a8d JJ |
8732 | if (iverneed->vn_next |
8733 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8734 | goto error_return_bad_verref; |
d0fb9a8d | 8735 | |
fc0e6df6 PB |
8736 | everneed = ((Elf_External_Verneed *) |
8737 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
8738 | } | |
25ff461f | 8739 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
8740 | |
8741 | free (contents); | |
8742 | contents = NULL; | |
8743 | } | |
252b5132 RH |
8744 | |
8745 | if (elf_dynverdef (abfd) != 0) | |
8746 | { | |
8747 | Elf_Internal_Shdr *hdr; | |
8748 | Elf_External_Verdef *everdef; | |
8749 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
8750 | Elf_Internal_Verdef *iverdefarr; |
8751 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 8752 | unsigned int i; |
062e2358 | 8753 | unsigned int maxidx; |
d0fb9a8d | 8754 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
8755 | |
8756 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
8757 | ||
601a03ba AM |
8758 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
8759 | { | |
8760 | error_return_bad_verdef: | |
4eca0228 | 8761 | _bfd_error_handler |
871b3ab2 | 8762 | (_("%pB: .gnu.version_d invalid entry"), abfd); |
601a03ba AM |
8763 | bfd_set_error (bfd_error_bad_value); |
8764 | error_return_verdef: | |
8765 | elf_tdata (abfd)->verdef = NULL; | |
8766 | elf_tdata (abfd)->cverdefs = 0; | |
8767 | goto error_return; | |
8768 | } | |
8769 | ||
2bb3687b | 8770 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) |
601a03ba | 8771 | goto error_return_verdef; |
2bb3687b AM |
8772 | contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size); |
8773 | if (contents == NULL) | |
601a03ba | 8774 | goto error_return_verdef; |
d0fb9a8d JJ |
8775 | |
8776 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
8777 | >= sizeof (Elf_External_Verdaux)); | |
8778 | contents_end_def = contents + hdr->sh_size | |
8779 | - sizeof (Elf_External_Verdef); | |
8780 | contents_end_aux = contents + hdr->sh_size | |
8781 | - sizeof (Elf_External_Verdaux); | |
8782 | ||
f631889e UD |
8783 | /* We know the number of entries in the section but not the maximum |
8784 | index. Therefore we have to run through all entries and find | |
8785 | the maximum. */ | |
252b5132 | 8786 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
8787 | maxidx = 0; |
8788 | for (i = 0; i < hdr->sh_info; ++i) | |
8789 | { | |
8790 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
8791 | ||
601a03ba AM |
8792 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
8793 | goto error_return_bad_verdef; | |
062e2358 AM |
8794 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
8795 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 8796 | |
25ff461f AM |
8797 | if (iverdefmem.vd_next == 0) |
8798 | break; | |
8799 | ||
d0fb9a8d JJ |
8800 | if (iverdefmem.vd_next |
8801 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 8802 | goto error_return_bad_verdef; |
d0fb9a8d | 8803 | |
f631889e UD |
8804 | everdef = ((Elf_External_Verdef *) |
8805 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
8806 | } | |
8807 | ||
fc0e6df6 PB |
8808 | if (default_imported_symver) |
8809 | { | |
8810 | if (freeidx > maxidx) | |
8811 | maxidx = ++freeidx; | |
8812 | else | |
8813 | freeidx = ++maxidx; | |
8814 | } | |
1f4361a7 AM |
8815 | if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt)) |
8816 | { | |
8817 | bfd_set_error (bfd_error_file_too_big); | |
8818 | goto error_return_verdef; | |
8819 | } | |
8820 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); | |
f631889e | 8821 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 8822 | goto error_return_verdef; |
f631889e UD |
8823 | |
8824 | elf_tdata (abfd)->cverdefs = maxidx; | |
8825 | ||
8826 | everdef = (Elf_External_Verdef *) contents; | |
8827 | iverdefarr = elf_tdata (abfd)->verdef; | |
8828 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
8829 | { |
8830 | Elf_External_Verdaux *everdaux; | |
8831 | Elf_Internal_Verdaux *iverdaux; | |
8832 | unsigned int j; | |
8833 | ||
f631889e UD |
8834 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
8835 | ||
d0fb9a8d | 8836 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 8837 | goto error_return_bad_verdef; |
d0fb9a8d | 8838 | |
f631889e | 8839 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
595bce75 | 8840 | memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); |
252b5132 RH |
8841 | |
8842 | iverdef->vd_bfd = abfd; | |
8843 | ||
d0fb9a8d JJ |
8844 | if (iverdef->vd_cnt == 0) |
8845 | iverdef->vd_auxptr = NULL; | |
8846 | else | |
8847 | { | |
1f4361a7 AM |
8848 | if (_bfd_mul_overflow (iverdef->vd_cnt, |
8849 | sizeof (Elf_Internal_Verdaux), &amt)) | |
8850 | { | |
8851 | bfd_set_error (bfd_error_file_too_big); | |
8852 | goto error_return_verdef; | |
8853 | } | |
a50b1753 | 8854 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
1f4361a7 | 8855 | bfd_alloc (abfd, amt); |
d0fb9a8d JJ |
8856 | if (iverdef->vd_auxptr == NULL) |
8857 | goto error_return_verdef; | |
8858 | } | |
8859 | ||
8860 | if (iverdef->vd_aux | |
8861 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 8862 | goto error_return_bad_verdef; |
252b5132 RH |
8863 | |
8864 | everdaux = ((Elf_External_Verdaux *) | |
8865 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
8866 | iverdaux = iverdef->vd_auxptr; | |
8867 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
8868 | { | |
8869 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
8870 | ||
8871 | iverdaux->vda_nodename = | |
8872 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8873 | iverdaux->vda_name); | |
8874 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 8875 | goto error_return_bad_verdef; |
252b5132 | 8876 | |
25ff461f AM |
8877 | iverdaux->vda_nextptr = NULL; |
8878 | if (iverdaux->vda_next == 0) | |
8879 | { | |
8880 | iverdef->vd_cnt = j + 1; | |
8881 | break; | |
8882 | } | |
252b5132 RH |
8883 | if (j + 1 < iverdef->vd_cnt) |
8884 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 8885 | |
d0fb9a8d JJ |
8886 | if (iverdaux->vda_next |
8887 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 8888 | goto error_return_bad_verdef; |
d0fb9a8d | 8889 | |
252b5132 RH |
8890 | everdaux = ((Elf_External_Verdaux *) |
8891 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
8892 | } | |
8893 | ||
595bce75 | 8894 | iverdef->vd_nodename = NULL; |
d0fb9a8d JJ |
8895 | if (iverdef->vd_cnt) |
8896 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 8897 | |
25ff461f AM |
8898 | iverdef->vd_nextdef = NULL; |
8899 | if (iverdef->vd_next == 0) | |
8900 | break; | |
d0fb9a8d | 8901 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 8902 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
8903 | |
8904 | everdef = ((Elf_External_Verdef *) | |
8905 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
8906 | } | |
8907 | ||
8908 | free (contents); | |
8909 | contents = NULL; | |
8910 | } | |
fc0e6df6 | 8911 | else if (default_imported_symver) |
252b5132 | 8912 | { |
fc0e6df6 PB |
8913 | if (freeidx < 3) |
8914 | freeidx = 3; | |
8915 | else | |
8916 | freeidx++; | |
252b5132 | 8917 | |
1f4361a7 AM |
8918 | if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt)) |
8919 | { | |
8920 | bfd_set_error (bfd_error_file_too_big); | |
8921 | goto error_return; | |
8922 | } | |
8923 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); | |
fc0e6df6 | 8924 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
8925 | goto error_return; |
8926 | ||
fc0e6df6 PB |
8927 | elf_tdata (abfd)->cverdefs = freeidx; |
8928 | } | |
252b5132 | 8929 | |
fc0e6df6 PB |
8930 | /* Create a default version based on the soname. */ |
8931 | if (default_imported_symver) | |
8932 | { | |
8933 | Elf_Internal_Verdef *iverdef; | |
8934 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 8935 | |
5bb3703f | 8936 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 8937 | |
fc0e6df6 PB |
8938 | iverdef->vd_version = VER_DEF_CURRENT; |
8939 | iverdef->vd_flags = 0; | |
8940 | iverdef->vd_ndx = freeidx; | |
8941 | iverdef->vd_cnt = 1; | |
252b5132 | 8942 | |
fc0e6df6 | 8943 | iverdef->vd_bfd = abfd; |
252b5132 | 8944 | |
fc0e6df6 PB |
8945 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
8946 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 8947 | goto error_return_verdef; |
fc0e6df6 | 8948 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
8949 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
8950 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
8951 | if (iverdef->vd_auxptr == NULL) |
8952 | goto error_return_verdef; | |
252b5132 | 8953 | |
fc0e6df6 PB |
8954 | iverdaux = iverdef->vd_auxptr; |
8955 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
8956 | } |
8957 | ||
b34976b6 | 8958 | return TRUE; |
252b5132 RH |
8959 | |
8960 | error_return: | |
5ed6aba4 | 8961 | if (contents != NULL) |
252b5132 | 8962 | free (contents); |
b34976b6 | 8963 | return FALSE; |
252b5132 RH |
8964 | } |
8965 | \f | |
8966 | asymbol * | |
217aa764 | 8967 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
8968 | { |
8969 | elf_symbol_type *newsym; | |
8970 | ||
7a6e0d89 | 8971 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym)); |
252b5132 RH |
8972 | if (!newsym) |
8973 | return NULL; | |
201159ec NC |
8974 | newsym->symbol.the_bfd = abfd; |
8975 | return &newsym->symbol; | |
252b5132 RH |
8976 | } |
8977 | ||
8978 | void | |
217aa764 AM |
8979 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
8980 | asymbol *symbol, | |
8981 | symbol_info *ret) | |
252b5132 RH |
8982 | { |
8983 | bfd_symbol_info (symbol, ret); | |
8984 | } | |
8985 | ||
8986 | /* Return whether a symbol name implies a local symbol. Most targets | |
8987 | use this function for the is_local_label_name entry point, but some | |
8988 | override it. */ | |
8989 | ||
b34976b6 | 8990 | bfd_boolean |
217aa764 AM |
8991 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
8992 | const char *name) | |
252b5132 RH |
8993 | { |
8994 | /* Normal local symbols start with ``.L''. */ | |
8995 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 8996 | return TRUE; |
252b5132 RH |
8997 | |
8998 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
8999 | DWARF debugging symbols starting with ``..''. */ | |
9000 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 9001 | return TRUE; |
252b5132 RH |
9002 | |
9003 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
9004 | emitting DWARF debugging output. I suspect this is actually a | |
9005 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
9006 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
9007 | underscore to be emitted on some ELF targets). For ease of use, | |
9008 | we treat such symbols as local. */ | |
9009 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 9010 | return TRUE; |
252b5132 | 9011 | |
b1fa9dd6 NC |
9012 | /* Treat assembler generated fake symbols, dollar local labels and |
9013 | forward-backward labels (aka local labels) as locals. | |
9014 | These labels have the form: | |
9015 | ||
07d6d2b8 | 9016 | L0^A.* (fake symbols) |
b1fa9dd6 NC |
9017 | |
9018 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
9019 | ||
9020 | Versions which start with .L will have already been matched above, | |
9021 | so we only need to match the rest. */ | |
9022 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
9023 | { | |
9024 | bfd_boolean ret = FALSE; | |
9025 | const char * p; | |
9026 | char c; | |
9027 | ||
9028 | for (p = name + 2; (c = *p); p++) | |
9029 | { | |
9030 | if (c == 1 || c == 2) | |
9031 | { | |
9032 | if (c == 1 && p == name + 2) | |
9033 | /* A fake symbol. */ | |
9034 | return TRUE; | |
9035 | ||
9036 | /* FIXME: We are being paranoid here and treating symbols like | |
9037 | L0^Bfoo as if there were non-local, on the grounds that the | |
9038 | assembler will never generate them. But can any symbol | |
9039 | containing an ASCII value in the range 1-31 ever be anything | |
9040 | other than some kind of local ? */ | |
9041 | ret = TRUE; | |
9042 | } | |
9043 | ||
9044 | if (! ISDIGIT (c)) | |
9045 | { | |
9046 | ret = FALSE; | |
9047 | break; | |
9048 | } | |
9049 | } | |
9050 | return ret; | |
9051 | } | |
ffa54770 | 9052 | |
b34976b6 | 9053 | return FALSE; |
252b5132 RH |
9054 | } |
9055 | ||
9056 | alent * | |
217aa764 AM |
9057 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
9058 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
9059 | { |
9060 | abort (); | |
9061 | return NULL; | |
9062 | } | |
9063 | ||
b34976b6 | 9064 | bfd_boolean |
217aa764 AM |
9065 | _bfd_elf_set_arch_mach (bfd *abfd, |
9066 | enum bfd_architecture arch, | |
9067 | unsigned long machine) | |
252b5132 RH |
9068 | { |
9069 | /* If this isn't the right architecture for this backend, and this | |
9070 | isn't the generic backend, fail. */ | |
9071 | if (arch != get_elf_backend_data (abfd)->arch | |
9072 | && arch != bfd_arch_unknown | |
9073 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 9074 | return FALSE; |
252b5132 RH |
9075 | |
9076 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
9077 | } | |
9078 | ||
d1fad7c6 NC |
9079 | /* Find the nearest line to a particular section and offset, |
9080 | for error reporting. */ | |
9081 | ||
b34976b6 | 9082 | bfd_boolean |
217aa764 | 9083 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 9084 | asymbol **symbols, |
fb167eb2 | 9085 | asection *section, |
217aa764 AM |
9086 | bfd_vma offset, |
9087 | const char **filename_ptr, | |
9088 | const char **functionname_ptr, | |
fb167eb2 AM |
9089 | unsigned int *line_ptr, |
9090 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 9091 | { |
b34976b6 | 9092 | bfd_boolean found; |
d1fad7c6 | 9093 | |
fb167eb2 | 9094 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 9095 | filename_ptr, functionname_ptr, |
fb167eb2 | 9096 | line_ptr, discriminator_ptr, |
9defd221 | 9097 | dwarf_debug_sections, |
e7679060 AM |
9098 | &elf_tdata (abfd)->dwarf2_find_line_info)) |
9099 | return TRUE; | |
9100 | ||
9101 | if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
9102 | filename_ptr, functionname_ptr, line_ptr)) | |
d1fad7c6 NC |
9103 | { |
9104 | if (!*functionname_ptr) | |
e00e8198 AM |
9105 | _bfd_elf_find_function (abfd, symbols, section, offset, |
9106 | *filename_ptr ? NULL : filename_ptr, | |
9107 | functionname_ptr); | |
b34976b6 | 9108 | return TRUE; |
d1fad7c6 NC |
9109 | } |
9110 | ||
9111 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
9112 | &found, filename_ptr, |
9113 | functionname_ptr, line_ptr, | |
9114 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 9115 | return FALSE; |
dc43ada5 | 9116 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 9117 | return TRUE; |
d1fad7c6 NC |
9118 | |
9119 | if (symbols == NULL) | |
b34976b6 | 9120 | return FALSE; |
d1fad7c6 | 9121 | |
e00e8198 AM |
9122 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
9123 | filename_ptr, functionname_ptr)) | |
b34976b6 | 9124 | return FALSE; |
d1fad7c6 | 9125 | |
252b5132 | 9126 | *line_ptr = 0; |
b34976b6 | 9127 | return TRUE; |
252b5132 RH |
9128 | } |
9129 | ||
5420f73d L |
9130 | /* Find the line for a symbol. */ |
9131 | ||
9132 | bfd_boolean | |
9133 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
9134 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 9135 | { |
fb167eb2 AM |
9136 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
9137 | filename_ptr, NULL, line_ptr, NULL, | |
9defd221 | 9138 | dwarf_debug_sections, |
fb167eb2 | 9139 | &elf_tdata (abfd)->dwarf2_find_line_info); |
5420f73d L |
9140 | } |
9141 | ||
4ab527b0 FF |
9142 | /* After a call to bfd_find_nearest_line, successive calls to |
9143 | bfd_find_inliner_info can be used to get source information about | |
9144 | each level of function inlining that terminated at the address | |
9145 | passed to bfd_find_nearest_line. Currently this is only supported | |
9146 | for DWARF2 with appropriate DWARF3 extensions. */ | |
9147 | ||
9148 | bfd_boolean | |
9149 | _bfd_elf_find_inliner_info (bfd *abfd, | |
9150 | const char **filename_ptr, | |
9151 | const char **functionname_ptr, | |
9152 | unsigned int *line_ptr) | |
9153 | { | |
9154 | bfd_boolean found; | |
9155 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
9156 | functionname_ptr, line_ptr, | |
9157 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
9158 | return found; | |
9159 | } | |
9160 | ||
252b5132 | 9161 | int |
a6b96beb | 9162 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 9163 | { |
8ded5a0f AM |
9164 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
9165 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 9166 | |
0e1862bb | 9167 | if (!bfd_link_relocatable (info)) |
8ded5a0f | 9168 | { |
12bd6957 | 9169 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 9170 | |
62d7a5f6 AM |
9171 | if (phdr_size == (bfd_size_type) -1) |
9172 | { | |
9173 | struct elf_segment_map *m; | |
9174 | ||
9175 | phdr_size = 0; | |
12bd6957 | 9176 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 9177 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 9178 | |
62d7a5f6 AM |
9179 | if (phdr_size == 0) |
9180 | phdr_size = get_program_header_size (abfd, info); | |
9181 | } | |
8ded5a0f | 9182 | |
12bd6957 | 9183 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
9184 | ret += phdr_size; |
9185 | } | |
9186 | ||
252b5132 RH |
9187 | return ret; |
9188 | } | |
9189 | ||
b34976b6 | 9190 | bfd_boolean |
217aa764 AM |
9191 | _bfd_elf_set_section_contents (bfd *abfd, |
9192 | sec_ptr section, | |
0f867abe | 9193 | const void *location, |
217aa764 AM |
9194 | file_ptr offset, |
9195 | bfd_size_type count) | |
252b5132 RH |
9196 | { |
9197 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 9198 | file_ptr pos; |
252b5132 RH |
9199 | |
9200 | if (! abfd->output_has_begun | |
217aa764 | 9201 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 9202 | return FALSE; |
252b5132 | 9203 | |
0ce398f1 L |
9204 | if (!count) |
9205 | return TRUE; | |
9206 | ||
252b5132 | 9207 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
9208 | if (hdr->sh_offset == (file_ptr) -1) |
9209 | { | |
a0dcf297 NC |
9210 | unsigned char *contents; |
9211 | ||
1ff6de03 NA |
9212 | if (bfd_section_is_ctf (section)) |
9213 | /* Nothing to do with this section: the contents are generated | |
9214 | later. */ | |
9215 | return TRUE; | |
9216 | ||
a0dcf297 NC |
9217 | if ((section->flags & SEC_ELF_COMPRESS) == 0) |
9218 | { | |
9219 | _bfd_error_handler | |
9220 | (_("%pB:%pA: error: attempting to write into an unallocated compressed section"), | |
9221 | abfd, section); | |
9222 | bfd_set_error (bfd_error_invalid_operation); | |
9223 | return FALSE; | |
9224 | } | |
9225 | ||
9226 | if ((offset + count) > hdr->sh_size) | |
9227 | { | |
9228 | _bfd_error_handler | |
9229 | (_("%pB:%pA: error: attempting to write over the end of the section"), | |
9230 | abfd, section); | |
9231 | ||
9232 | bfd_set_error (bfd_error_invalid_operation); | |
9233 | return FALSE; | |
9234 | } | |
9235 | ||
9236 | contents = hdr->contents; | |
9237 | if (contents == NULL) | |
9238 | { | |
9239 | _bfd_error_handler | |
9240 | (_("%pB:%pA: error: attempting to write section into an empty buffer"), | |
9241 | abfd, section); | |
9242 | ||
9243 | bfd_set_error (bfd_error_invalid_operation); | |
9244 | return FALSE; | |
9245 | } | |
9246 | ||
0ce398f1 L |
9247 | memcpy (contents + offset, location, count); |
9248 | return TRUE; | |
9249 | } | |
a0dcf297 | 9250 | |
dc810e39 AM |
9251 | pos = hdr->sh_offset + offset; |
9252 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
9253 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 9254 | return FALSE; |
252b5132 | 9255 | |
b34976b6 | 9256 | return TRUE; |
252b5132 RH |
9257 | } |
9258 | ||
f3185997 | 9259 | bfd_boolean |
217aa764 AM |
9260 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
9261 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
9262 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
9263 | { |
9264 | abort (); | |
f3185997 | 9265 | return FALSE; |
252b5132 RH |
9266 | } |
9267 | ||
252b5132 RH |
9268 | /* Try to convert a non-ELF reloc into an ELF one. */ |
9269 | ||
b34976b6 | 9270 | bfd_boolean |
217aa764 | 9271 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 9272 | { |
c044fabd | 9273 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
9274 | |
9275 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
9276 | { | |
9277 | bfd_reloc_code_real_type code; | |
9278 | reloc_howto_type *howto; | |
9279 | ||
9280 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 9281 | equivalent ELF reloc. */ |
252b5132 RH |
9282 | |
9283 | if (areloc->howto->pc_relative) | |
9284 | { | |
9285 | switch (areloc->howto->bitsize) | |
9286 | { | |
9287 | case 8: | |
9288 | code = BFD_RELOC_8_PCREL; | |
9289 | break; | |
9290 | case 12: | |
9291 | code = BFD_RELOC_12_PCREL; | |
9292 | break; | |
9293 | case 16: | |
9294 | code = BFD_RELOC_16_PCREL; | |
9295 | break; | |
9296 | case 24: | |
9297 | code = BFD_RELOC_24_PCREL; | |
9298 | break; | |
9299 | case 32: | |
9300 | code = BFD_RELOC_32_PCREL; | |
9301 | break; | |
9302 | case 64: | |
9303 | code = BFD_RELOC_64_PCREL; | |
9304 | break; | |
9305 | default: | |
9306 | goto fail; | |
9307 | } | |
9308 | ||
9309 | howto = bfd_reloc_type_lookup (abfd, code); | |
9310 | ||
94698d01 | 9311 | if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset) |
252b5132 RH |
9312 | { |
9313 | if (howto->pcrel_offset) | |
9314 | areloc->addend += areloc->address; | |
9315 | else | |
9316 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
9317 | } | |
9318 | } | |
9319 | else | |
9320 | { | |
9321 | switch (areloc->howto->bitsize) | |
9322 | { | |
9323 | case 8: | |
9324 | code = BFD_RELOC_8; | |
9325 | break; | |
9326 | case 14: | |
9327 | code = BFD_RELOC_14; | |
9328 | break; | |
9329 | case 16: | |
9330 | code = BFD_RELOC_16; | |
9331 | break; | |
9332 | case 26: | |
9333 | code = BFD_RELOC_26; | |
9334 | break; | |
9335 | case 32: | |
9336 | code = BFD_RELOC_32; | |
9337 | break; | |
9338 | case 64: | |
9339 | code = BFD_RELOC_64; | |
9340 | break; | |
9341 | default: | |
9342 | goto fail; | |
9343 | } | |
9344 | ||
9345 | howto = bfd_reloc_type_lookup (abfd, code); | |
9346 | } | |
9347 | ||
9348 | if (howto) | |
9349 | areloc->howto = howto; | |
9350 | else | |
9351 | goto fail; | |
9352 | } | |
9353 | ||
b34976b6 | 9354 | return TRUE; |
252b5132 RH |
9355 | |
9356 | fail: | |
0aa13fee AM |
9357 | /* xgettext:c-format */ |
9358 | _bfd_error_handler (_("%pB: %s unsupported"), | |
9359 | abfd, areloc->howto->name); | |
9aea1e31 | 9360 | bfd_set_error (bfd_error_sorry); |
b34976b6 | 9361 | return FALSE; |
252b5132 RH |
9362 | } |
9363 | ||
b34976b6 | 9364 | bfd_boolean |
217aa764 | 9365 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 9366 | { |
d9071b0c TG |
9367 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
9368 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 9369 | { |
c0355132 | 9370 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 9371 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 9372 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
9373 | } |
9374 | ||
9375 | return _bfd_generic_close_and_cleanup (abfd); | |
9376 | } | |
9377 | ||
9378 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
9379 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
9380 | range-checking to interfere. There is nothing else to do in processing | |
9381 | this reloc. */ | |
9382 | ||
9383 | bfd_reloc_status_type | |
217aa764 AM |
9384 | _bfd_elf_rel_vtable_reloc_fn |
9385 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 9386 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
9387 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
9388 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
9389 | { |
9390 | return bfd_reloc_ok; | |
9391 | } | |
252b5132 RH |
9392 | \f |
9393 | /* Elf core file support. Much of this only works on native | |
9394 | toolchains, since we rely on knowing the | |
9395 | machine-dependent procfs structure in order to pick | |
c044fabd | 9396 | out details about the corefile. */ |
252b5132 RH |
9397 | |
9398 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
9399 | /* Needed for new procfs interface on sparc-solaris. */ |
9400 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
9401 | # include <sys/procfs.h> |
9402 | #endif | |
9403 | ||
261b8d08 PA |
9404 | /* Return a PID that identifies a "thread" for threaded cores, or the |
9405 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
9406 | |
9407 | static int | |
217aa764 | 9408 | elfcore_make_pid (bfd *abfd) |
252b5132 | 9409 | { |
261b8d08 PA |
9410 | int pid; |
9411 | ||
228e534f | 9412 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 9413 | if (pid == 0) |
228e534f | 9414 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
9415 | |
9416 | return pid; | |
252b5132 RH |
9417 | } |
9418 | ||
252b5132 RH |
9419 | /* If there isn't a section called NAME, make one, using |
9420 | data from SECT. Note, this function will generate a | |
9421 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 9422 | overwrite it. */ |
252b5132 | 9423 | |
b34976b6 | 9424 | static bfd_boolean |
217aa764 | 9425 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 9426 | { |
c044fabd | 9427 | asection *sect2; |
252b5132 RH |
9428 | |
9429 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 9430 | return TRUE; |
252b5132 | 9431 | |
117ed4f8 | 9432 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 9433 | if (sect2 == NULL) |
b34976b6 | 9434 | return FALSE; |
252b5132 | 9435 | |
eea6121a | 9436 | sect2->size = sect->size; |
252b5132 | 9437 | sect2->filepos = sect->filepos; |
252b5132 | 9438 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 9439 | return TRUE; |
252b5132 RH |
9440 | } |
9441 | ||
bb0082d6 AM |
9442 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
9443 | actually creates up to two pseudosections: | |
9444 | - For the single-threaded case, a section named NAME, unless | |
9445 | such a section already exists. | |
9446 | - For the multi-threaded case, a section named "NAME/PID", where | |
9447 | PID is elfcore_make_pid (abfd). | |
24d3e51b | 9448 | Both pseudosections have identical contents. */ |
b34976b6 | 9449 | bfd_boolean |
217aa764 AM |
9450 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
9451 | char *name, | |
9452 | size_t size, | |
9453 | ufile_ptr filepos) | |
bb0082d6 AM |
9454 | { |
9455 | char buf[100]; | |
9456 | char *threaded_name; | |
d4c88bbb | 9457 | size_t len; |
bb0082d6 AM |
9458 | asection *sect; |
9459 | ||
9460 | /* Build the section name. */ | |
9461 | ||
9462 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 9463 | len = strlen (buf) + 1; |
a50b1753 | 9464 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 9465 | if (threaded_name == NULL) |
b34976b6 | 9466 | return FALSE; |
d4c88bbb | 9467 | memcpy (threaded_name, buf, len); |
bb0082d6 | 9468 | |
117ed4f8 AM |
9469 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
9470 | SEC_HAS_CONTENTS); | |
bb0082d6 | 9471 | if (sect == NULL) |
b34976b6 | 9472 | return FALSE; |
eea6121a | 9473 | sect->size = size; |
bb0082d6 | 9474 | sect->filepos = filepos; |
bb0082d6 AM |
9475 | sect->alignment_power = 2; |
9476 | ||
936e320b | 9477 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
9478 | } |
9479 | ||
58e07198 CZ |
9480 | static bfd_boolean |
9481 | elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note, | |
9482 | size_t offs) | |
9483 | { | |
9484 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9485 | SEC_HAS_CONTENTS); | |
9486 | ||
9487 | if (sect == NULL) | |
9488 | return FALSE; | |
9489 | ||
9490 | sect->size = note->descsz - offs; | |
9491 | sect->filepos = note->descpos + offs; | |
9492 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9493 | ||
9494 | return TRUE; | |
9495 | } | |
9496 | ||
252b5132 | 9497 | /* prstatus_t exists on: |
4a938328 | 9498 | solaris 2.5+ |
252b5132 RH |
9499 | linux 2.[01] + glibc |
9500 | unixware 4.2 | |
9501 | */ | |
9502 | ||
9503 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 9504 | |
b34976b6 | 9505 | static bfd_boolean |
217aa764 | 9506 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9507 | { |
eea6121a | 9508 | size_t size; |
7ee38065 | 9509 | int offset; |
252b5132 | 9510 | |
4a938328 MS |
9511 | if (note->descsz == sizeof (prstatus_t)) |
9512 | { | |
9513 | prstatus_t prstat; | |
252b5132 | 9514 | |
eea6121a | 9515 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9516 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 9517 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 9518 | |
fa49d224 NC |
9519 | /* Do not overwrite the core signal if it |
9520 | has already been set by another thread. */ | |
228e534f AM |
9521 | if (elf_tdata (abfd)->core->signal == 0) |
9522 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9523 | if (elf_tdata (abfd)->core->pid == 0) | |
9524 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 9525 | |
4a938328 MS |
9526 | /* pr_who exists on: |
9527 | solaris 2.5+ | |
9528 | unixware 4.2 | |
9529 | pr_who doesn't exist on: | |
9530 | linux 2.[01] | |
9531 | */ | |
252b5132 | 9532 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 9533 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9534 | #else |
228e534f | 9535 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 9536 | #endif |
4a938328 | 9537 | } |
7ee38065 | 9538 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
9539 | else if (note->descsz == sizeof (prstatus32_t)) |
9540 | { | |
9541 | /* 64-bit host, 32-bit corefile */ | |
9542 | prstatus32_t prstat; | |
9543 | ||
eea6121a | 9544 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9545 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
9546 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
9547 | ||
fa49d224 NC |
9548 | /* Do not overwrite the core signal if it |
9549 | has already been set by another thread. */ | |
228e534f AM |
9550 | if (elf_tdata (abfd)->core->signal == 0) |
9551 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9552 | if (elf_tdata (abfd)->core->pid == 0) | |
9553 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
9554 | |
9555 | /* pr_who exists on: | |
9556 | solaris 2.5+ | |
9557 | unixware 4.2 | |
9558 | pr_who doesn't exist on: | |
9559 | linux 2.[01] | |
9560 | */ | |
7ee38065 | 9561 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 9562 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9563 | #else |
228e534f | 9564 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
9565 | #endif |
9566 | } | |
7ee38065 | 9567 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
9568 | else |
9569 | { | |
9570 | /* Fail - we don't know how to handle any other | |
9571 | note size (ie. data object type). */ | |
b34976b6 | 9572 | return TRUE; |
4a938328 | 9573 | } |
252b5132 | 9574 | |
bb0082d6 | 9575 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 9576 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 9577 | size, note->descpos + offset); |
252b5132 RH |
9578 | } |
9579 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
9580 | ||
bb0082d6 | 9581 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 9582 | static bfd_boolean |
217aa764 AM |
9583 | elfcore_make_note_pseudosection (bfd *abfd, |
9584 | char *name, | |
9585 | Elf_Internal_Note *note) | |
252b5132 | 9586 | { |
936e320b AM |
9587 | return _bfd_elfcore_make_pseudosection (abfd, name, |
9588 | note->descsz, note->descpos); | |
252b5132 RH |
9589 | } |
9590 | ||
ff08c6bb JB |
9591 | /* There isn't a consistent prfpregset_t across platforms, |
9592 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
9593 | data structure apart. */ |
9594 | ||
b34976b6 | 9595 | static bfd_boolean |
217aa764 | 9596 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9597 | { |
9598 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9599 | } | |
9600 | ||
ff08c6bb | 9601 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 9602 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 9603 | literally. */ |
c044fabd | 9604 | |
b34976b6 | 9605 | static bfd_boolean |
217aa764 | 9606 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9607 | { |
9608 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9609 | } | |
9610 | ||
4339cae0 L |
9611 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
9612 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
9613 | contents literally. */ | |
9614 | ||
9615 | static bfd_boolean | |
9616 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
9617 | { | |
9618 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
9619 | } | |
9620 | ||
97753bd5 AM |
9621 | static bfd_boolean |
9622 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
9623 | { | |
9624 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
9625 | } | |
9626 | ||
89eeb0bc LM |
9627 | static bfd_boolean |
9628 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
9629 | { | |
9630 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
9631 | } | |
97753bd5 | 9632 | |
cb2366c1 EBM |
9633 | static bfd_boolean |
9634 | elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note) | |
9635 | { | |
9636 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note); | |
9637 | } | |
9638 | ||
9639 | static bfd_boolean | |
9640 | elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note) | |
9641 | { | |
9642 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note); | |
9643 | } | |
9644 | ||
9645 | static bfd_boolean | |
9646 | elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note) | |
9647 | { | |
9648 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note); | |
9649 | } | |
9650 | ||
9651 | static bfd_boolean | |
9652 | elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note) | |
9653 | { | |
9654 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note); | |
9655 | } | |
9656 | ||
9657 | static bfd_boolean | |
9658 | elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note) | |
9659 | { | |
9660 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note); | |
9661 | } | |
9662 | ||
9663 | static bfd_boolean | |
9664 | elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note) | |
9665 | { | |
9666 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note); | |
9667 | } | |
9668 | ||
9669 | static bfd_boolean | |
9670 | elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note) | |
9671 | { | |
9672 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note); | |
9673 | } | |
9674 | ||
9675 | static bfd_boolean | |
9676 | elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note) | |
9677 | { | |
9678 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note); | |
9679 | } | |
9680 | ||
9681 | static bfd_boolean | |
9682 | elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note) | |
9683 | { | |
9684 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note); | |
9685 | } | |
9686 | ||
9687 | static bfd_boolean | |
9688 | elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note) | |
9689 | { | |
9690 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note); | |
9691 | } | |
9692 | ||
9693 | static bfd_boolean | |
9694 | elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note) | |
9695 | { | |
9696 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note); | |
9697 | } | |
9698 | ||
9699 | static bfd_boolean | |
9700 | elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note) | |
9701 | { | |
9702 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note); | |
9703 | } | |
9704 | ||
9705 | static bfd_boolean | |
9706 | elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note) | |
9707 | { | |
9708 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note); | |
9709 | } | |
9710 | ||
0675e188 UW |
9711 | static bfd_boolean |
9712 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
9713 | { | |
9714 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
9715 | } | |
9716 | ||
d7eeb400 MS |
9717 | static bfd_boolean |
9718 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
9719 | { | |
9720 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
9721 | } | |
9722 | ||
9723 | static bfd_boolean | |
9724 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
9725 | { | |
9726 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
9727 | } | |
9728 | ||
9729 | static bfd_boolean | |
9730 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
9731 | { | |
9732 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
9733 | } | |
9734 | ||
9735 | static bfd_boolean | |
9736 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
9737 | { | |
9738 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
9739 | } | |
9740 | ||
9741 | static bfd_boolean | |
9742 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
9743 | { | |
9744 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
9745 | } | |
9746 | ||
355b81d9 UW |
9747 | static bfd_boolean |
9748 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
9749 | { | |
9750 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
9751 | } | |
9752 | ||
9753 | static bfd_boolean | |
9754 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
9755 | { | |
9756 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
9757 | } | |
9758 | ||
abb3f6cc NC |
9759 | static bfd_boolean |
9760 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
9761 | { | |
9762 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
9763 | } | |
9764 | ||
4ef9f41a AA |
9765 | static bfd_boolean |
9766 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
9767 | { | |
9768 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
9769 | } | |
9770 | ||
9771 | static bfd_boolean | |
9772 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
9773 | { | |
9774 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
9775 | } | |
9776 | ||
88ab90e8 AA |
9777 | static bfd_boolean |
9778 | elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note) | |
9779 | { | |
9780 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note); | |
9781 | } | |
9782 | ||
9783 | static bfd_boolean | |
9784 | elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note) | |
9785 | { | |
9786 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note); | |
9787 | } | |
9788 | ||
faa9a424 UW |
9789 | static bfd_boolean |
9790 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
9791 | { | |
9792 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
9793 | } | |
9794 | ||
652451f8 YZ |
9795 | static bfd_boolean |
9796 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
9797 | { | |
9798 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
9799 | } | |
9800 | ||
9801 | static bfd_boolean | |
9802 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
9803 | { | |
9804 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
9805 | } | |
9806 | ||
9807 | static bfd_boolean | |
9808 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
9809 | { | |
9810 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
9811 | } | |
9812 | ||
ad1cc4e4 AH |
9813 | static bfd_boolean |
9814 | elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note) | |
9815 | { | |
9816 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note); | |
9817 | } | |
9818 | ||
e6c3b5bf AH |
9819 | static bfd_boolean |
9820 | elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note) | |
9821 | { | |
9822 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note); | |
9823 | } | |
9824 | ||
252b5132 | 9825 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 9826 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 9827 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9828 | typedef prpsinfo32_t elfcore_psinfo32_t; |
9829 | #endif | |
252b5132 RH |
9830 | #endif |
9831 | ||
9832 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 9833 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 9834 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9835 | typedef psinfo32_t elfcore_psinfo32_t; |
9836 | #endif | |
252b5132 RH |
9837 | #endif |
9838 | ||
252b5132 RH |
9839 | /* return a malloc'ed copy of a string at START which is at |
9840 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 9841 | the copy will always have a terminating '\0'. */ |
252b5132 | 9842 | |
936e320b | 9843 | char * |
217aa764 | 9844 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 9845 | { |
dc810e39 | 9846 | char *dups; |
a50b1753 | 9847 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 9848 | size_t len; |
252b5132 RH |
9849 | |
9850 | if (end == NULL) | |
9851 | len = max; | |
9852 | else | |
9853 | len = end - start; | |
9854 | ||
a50b1753 | 9855 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 9856 | if (dups == NULL) |
252b5132 RH |
9857 | return NULL; |
9858 | ||
dc810e39 AM |
9859 | memcpy (dups, start, len); |
9860 | dups[len] = '\0'; | |
252b5132 | 9861 | |
dc810e39 | 9862 | return dups; |
252b5132 RH |
9863 | } |
9864 | ||
bb0082d6 | 9865 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 9866 | static bfd_boolean |
217aa764 | 9867 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9868 | { |
4a938328 MS |
9869 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
9870 | { | |
9871 | elfcore_psinfo_t psinfo; | |
252b5132 | 9872 | |
7ee38065 | 9873 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9874 | |
335e41d4 | 9875 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 9876 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9877 | #endif |
228e534f | 9878 | elf_tdata (abfd)->core->program |
936e320b AM |
9879 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9880 | sizeof (psinfo.pr_fname)); | |
252b5132 | 9881 | |
228e534f | 9882 | elf_tdata (abfd)->core->command |
936e320b AM |
9883 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9884 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 9885 | } |
7ee38065 | 9886 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
9887 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
9888 | { | |
9889 | /* 64-bit host, 32-bit corefile */ | |
9890 | elfcore_psinfo32_t psinfo; | |
9891 | ||
7ee38065 | 9892 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9893 | |
335e41d4 | 9894 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 9895 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9896 | #endif |
228e534f | 9897 | elf_tdata (abfd)->core->program |
936e320b AM |
9898 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9899 | sizeof (psinfo.pr_fname)); | |
4a938328 | 9900 | |
228e534f | 9901 | elf_tdata (abfd)->core->command |
936e320b AM |
9902 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9903 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
9904 | } |
9905 | #endif | |
9906 | ||
9907 | else | |
9908 | { | |
9909 | /* Fail - we don't know how to handle any other | |
9910 | note size (ie. data object type). */ | |
b34976b6 | 9911 | return TRUE; |
4a938328 | 9912 | } |
252b5132 RH |
9913 | |
9914 | /* Note that for some reason, a spurious space is tacked | |
9915 | onto the end of the args in some (at least one anyway) | |
c044fabd | 9916 | implementations, so strip it off if it exists. */ |
252b5132 RH |
9917 | |
9918 | { | |
228e534f | 9919 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
9920 | int n = strlen (command); |
9921 | ||
9922 | if (0 < n && command[n - 1] == ' ') | |
9923 | command[n - 1] = '\0'; | |
9924 | } | |
9925 | ||
b34976b6 | 9926 | return TRUE; |
252b5132 RH |
9927 | } |
9928 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
9929 | ||
252b5132 | 9930 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 9931 | static bfd_boolean |
217aa764 | 9932 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9933 | { |
f572a39d AM |
9934 | if (note->descsz == sizeof (pstatus_t) |
9935 | #if defined (HAVE_PXSTATUS_T) | |
9936 | || note->descsz == sizeof (pxstatus_t) | |
9937 | #endif | |
9938 | ) | |
4a938328 MS |
9939 | { |
9940 | pstatus_t pstat; | |
252b5132 | 9941 | |
4a938328 | 9942 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9943 | |
228e534f | 9944 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 9945 | } |
7ee38065 | 9946 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
9947 | else if (note->descsz == sizeof (pstatus32_t)) |
9948 | { | |
9949 | /* 64-bit host, 32-bit corefile */ | |
9950 | pstatus32_t pstat; | |
252b5132 | 9951 | |
4a938328 | 9952 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9953 | |
228e534f | 9954 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
9955 | } |
9956 | #endif | |
252b5132 RH |
9957 | /* Could grab some more details from the "representative" |
9958 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 9959 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 9960 | |
b34976b6 | 9961 | return TRUE; |
252b5132 RH |
9962 | } |
9963 | #endif /* defined (HAVE_PSTATUS_T) */ | |
9964 | ||
252b5132 | 9965 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 9966 | static bfd_boolean |
217aa764 | 9967 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
9968 | { |
9969 | lwpstatus_t lwpstat; | |
9970 | char buf[100]; | |
c044fabd | 9971 | char *name; |
d4c88bbb | 9972 | size_t len; |
c044fabd | 9973 | asection *sect; |
252b5132 | 9974 | |
f572a39d AM |
9975 | if (note->descsz != sizeof (lwpstat) |
9976 | #if defined (HAVE_LWPXSTATUS_T) | |
9977 | && note->descsz != sizeof (lwpxstatus_t) | |
9978 | #endif | |
9979 | ) | |
b34976b6 | 9980 | return TRUE; |
252b5132 RH |
9981 | |
9982 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
9983 | ||
228e534f | 9984 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
9985 | /* Do not overwrite the core signal if it has already been set by |
9986 | another thread. */ | |
228e534f AM |
9987 | if (elf_tdata (abfd)->core->signal == 0) |
9988 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 9989 | |
c044fabd | 9990 | /* Make a ".reg/999" section. */ |
252b5132 RH |
9991 | |
9992 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9993 | len = strlen (buf) + 1; |
217aa764 | 9994 | name = bfd_alloc (abfd, len); |
252b5132 | 9995 | if (name == NULL) |
b34976b6 | 9996 | return FALSE; |
d4c88bbb | 9997 | memcpy (name, buf, len); |
252b5132 | 9998 | |
117ed4f8 | 9999 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 10000 | if (sect == NULL) |
b34976b6 | 10001 | return FALSE; |
252b5132 RH |
10002 | |
10003 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 10004 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
10005 | sect->filepos = note->descpos |
10006 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
10007 | #endif | |
10008 | ||
10009 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 10010 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
10011 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
10012 | #endif | |
10013 | ||
252b5132 RH |
10014 | sect->alignment_power = 2; |
10015 | ||
10016 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 10017 | return FALSE; |
252b5132 RH |
10018 | |
10019 | /* Make a ".reg2/999" section */ | |
10020 | ||
10021 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 10022 | len = strlen (buf) + 1; |
217aa764 | 10023 | name = bfd_alloc (abfd, len); |
252b5132 | 10024 | if (name == NULL) |
b34976b6 | 10025 | return FALSE; |
d4c88bbb | 10026 | memcpy (name, buf, len); |
252b5132 | 10027 | |
117ed4f8 | 10028 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 10029 | if (sect == NULL) |
b34976b6 | 10030 | return FALSE; |
252b5132 RH |
10031 | |
10032 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 10033 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
10034 | sect->filepos = note->descpos |
10035 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
10036 | #endif | |
10037 | ||
10038 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 10039 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
10040 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
10041 | #endif | |
10042 | ||
252b5132 RH |
10043 | sect->alignment_power = 2; |
10044 | ||
936e320b | 10045 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
10046 | } |
10047 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
10048 | ||
b34976b6 | 10049 | static bfd_boolean |
217aa764 | 10050 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
10051 | { |
10052 | char buf[30]; | |
c044fabd | 10053 | char *name; |
d4c88bbb | 10054 | size_t len; |
c044fabd | 10055 | asection *sect; |
4a6636fb PA |
10056 | int type; |
10057 | int is_active_thread; | |
10058 | bfd_vma base_addr; | |
16e9c715 | 10059 | |
4a6636fb | 10060 | if (note->descsz < 728) |
b34976b6 | 10061 | return TRUE; |
16e9c715 | 10062 | |
4a6636fb PA |
10063 | if (! CONST_STRNEQ (note->namedata, "win32")) |
10064 | return TRUE; | |
10065 | ||
10066 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 10067 | |
4a6636fb | 10068 | switch (type) |
16e9c715 | 10069 | { |
4a6636fb | 10070 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 10071 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 10072 | /* process_info.pid */ |
228e534f | 10073 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 10074 | /* process_info.signal */ |
228e534f | 10075 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 10076 | break; |
16e9c715 | 10077 | |
4a6636fb | 10078 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 10079 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
10080 | /* thread_info.tid */ |
10081 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 10082 | |
d4c88bbb | 10083 | len = strlen (buf) + 1; |
a50b1753 | 10084 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 10085 | if (name == NULL) |
b34976b6 | 10086 | return FALSE; |
c044fabd | 10087 | |
d4c88bbb | 10088 | memcpy (name, buf, len); |
16e9c715 | 10089 | |
117ed4f8 | 10090 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 10091 | if (sect == NULL) |
b34976b6 | 10092 | return FALSE; |
c044fabd | 10093 | |
4a6636fb PA |
10094 | /* sizeof (thread_info.thread_context) */ |
10095 | sect->size = 716; | |
10096 | /* offsetof (thread_info.thread_context) */ | |
10097 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
10098 | sect->alignment_power = 2; |
10099 | ||
4a6636fb PA |
10100 | /* thread_info.is_active_thread */ |
10101 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
10102 | ||
10103 | if (is_active_thread) | |
16e9c715 | 10104 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 10105 | return FALSE; |
16e9c715 NC |
10106 | break; |
10107 | ||
4a6636fb | 10108 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 10109 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
10110 | /* module_info.base_address */ |
10111 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 10112 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 10113 | |
d4c88bbb | 10114 | len = strlen (buf) + 1; |
a50b1753 | 10115 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 10116 | if (name == NULL) |
b34976b6 | 10117 | return FALSE; |
c044fabd | 10118 | |
d4c88bbb | 10119 | memcpy (name, buf, len); |
252b5132 | 10120 | |
117ed4f8 | 10121 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 10122 | |
16e9c715 | 10123 | if (sect == NULL) |
b34976b6 | 10124 | return FALSE; |
c044fabd | 10125 | |
eea6121a | 10126 | sect->size = note->descsz; |
16e9c715 | 10127 | sect->filepos = note->descpos; |
16e9c715 NC |
10128 | sect->alignment_power = 2; |
10129 | break; | |
10130 | ||
10131 | default: | |
b34976b6 | 10132 | return TRUE; |
16e9c715 NC |
10133 | } |
10134 | ||
b34976b6 | 10135 | return TRUE; |
16e9c715 | 10136 | } |
252b5132 | 10137 | |
b34976b6 | 10138 | static bfd_boolean |
217aa764 | 10139 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 10140 | { |
9c5bfbb7 | 10141 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 10142 | |
252b5132 RH |
10143 | switch (note->type) |
10144 | { | |
10145 | default: | |
b34976b6 | 10146 | return TRUE; |
252b5132 | 10147 | |
252b5132 | 10148 | case NT_PRSTATUS: |
bb0082d6 AM |
10149 | if (bed->elf_backend_grok_prstatus) |
10150 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 10151 | return TRUE; |
bb0082d6 | 10152 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 10153 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 10154 | #else |
b34976b6 | 10155 | return TRUE; |
252b5132 RH |
10156 | #endif |
10157 | ||
10158 | #if defined (HAVE_PSTATUS_T) | |
10159 | case NT_PSTATUS: | |
10160 | return elfcore_grok_pstatus (abfd, note); | |
10161 | #endif | |
10162 | ||
10163 | #if defined (HAVE_LWPSTATUS_T) | |
10164 | case NT_LWPSTATUS: | |
10165 | return elfcore_grok_lwpstatus (abfd, note); | |
10166 | #endif | |
10167 | ||
10168 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
10169 | return elfcore_grok_prfpreg (abfd, note); | |
10170 | ||
c044fabd | 10171 | case NT_WIN32PSTATUS: |
16e9c715 | 10172 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 10173 | |
c044fabd | 10174 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
10175 | if (note->namesz == 6 |
10176 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
10177 | return elfcore_grok_prxfpreg (abfd, note); |
10178 | else | |
b34976b6 | 10179 | return TRUE; |
ff08c6bb | 10180 | |
4339cae0 L |
10181 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
10182 | if (note->namesz == 6 | |
10183 | && strcmp (note->namedata, "LINUX") == 0) | |
10184 | return elfcore_grok_xstatereg (abfd, note); | |
10185 | else | |
10186 | return TRUE; | |
10187 | ||
97753bd5 AM |
10188 | case NT_PPC_VMX: |
10189 | if (note->namesz == 6 | |
10190 | && strcmp (note->namedata, "LINUX") == 0) | |
10191 | return elfcore_grok_ppc_vmx (abfd, note); | |
10192 | else | |
10193 | return TRUE; | |
10194 | ||
89eeb0bc LM |
10195 | case NT_PPC_VSX: |
10196 | if (note->namesz == 6 | |
07d6d2b8 AM |
10197 | && strcmp (note->namedata, "LINUX") == 0) |
10198 | return elfcore_grok_ppc_vsx (abfd, note); | |
89eeb0bc | 10199 | else |
07d6d2b8 | 10200 | return TRUE; |
89eeb0bc | 10201 | |
cb2366c1 EBM |
10202 | case NT_PPC_TAR: |
10203 | if (note->namesz == 6 | |
4b24dd1a AM |
10204 | && strcmp (note->namedata, "LINUX") == 0) |
10205 | return elfcore_grok_ppc_tar (abfd, note); | |
cb2366c1 | 10206 | else |
4b24dd1a | 10207 | return TRUE; |
cb2366c1 EBM |
10208 | |
10209 | case NT_PPC_PPR: | |
10210 | if (note->namesz == 6 | |
4b24dd1a AM |
10211 | && strcmp (note->namedata, "LINUX") == 0) |
10212 | return elfcore_grok_ppc_ppr (abfd, note); | |
cb2366c1 | 10213 | else |
4b24dd1a | 10214 | return TRUE; |
cb2366c1 EBM |
10215 | |
10216 | case NT_PPC_DSCR: | |
10217 | if (note->namesz == 6 | |
4b24dd1a AM |
10218 | && strcmp (note->namedata, "LINUX") == 0) |
10219 | return elfcore_grok_ppc_dscr (abfd, note); | |
cb2366c1 | 10220 | else |
4b24dd1a | 10221 | return TRUE; |
cb2366c1 EBM |
10222 | |
10223 | case NT_PPC_EBB: | |
10224 | if (note->namesz == 6 | |
4b24dd1a AM |
10225 | && strcmp (note->namedata, "LINUX") == 0) |
10226 | return elfcore_grok_ppc_ebb (abfd, note); | |
cb2366c1 | 10227 | else |
4b24dd1a | 10228 | return TRUE; |
cb2366c1 EBM |
10229 | |
10230 | case NT_PPC_PMU: | |
10231 | if (note->namesz == 6 | |
4b24dd1a AM |
10232 | && strcmp (note->namedata, "LINUX") == 0) |
10233 | return elfcore_grok_ppc_pmu (abfd, note); | |
cb2366c1 | 10234 | else |
4b24dd1a | 10235 | return TRUE; |
cb2366c1 EBM |
10236 | |
10237 | case NT_PPC_TM_CGPR: | |
10238 | if (note->namesz == 6 | |
4b24dd1a AM |
10239 | && strcmp (note->namedata, "LINUX") == 0) |
10240 | return elfcore_grok_ppc_tm_cgpr (abfd, note); | |
cb2366c1 | 10241 | else |
4b24dd1a | 10242 | return TRUE; |
cb2366c1 EBM |
10243 | |
10244 | case NT_PPC_TM_CFPR: | |
10245 | if (note->namesz == 6 | |
4b24dd1a AM |
10246 | && strcmp (note->namedata, "LINUX") == 0) |
10247 | return elfcore_grok_ppc_tm_cfpr (abfd, note); | |
cb2366c1 | 10248 | else |
4b24dd1a | 10249 | return TRUE; |
cb2366c1 EBM |
10250 | |
10251 | case NT_PPC_TM_CVMX: | |
10252 | if (note->namesz == 6 | |
4b24dd1a AM |
10253 | && strcmp (note->namedata, "LINUX") == 0) |
10254 | return elfcore_grok_ppc_tm_cvmx (abfd, note); | |
cb2366c1 | 10255 | else |
4b24dd1a | 10256 | return TRUE; |
cb2366c1 EBM |
10257 | |
10258 | case NT_PPC_TM_CVSX: | |
10259 | if (note->namesz == 6 | |
4b24dd1a AM |
10260 | && strcmp (note->namedata, "LINUX") == 0) |
10261 | return elfcore_grok_ppc_tm_cvsx (abfd, note); | |
cb2366c1 | 10262 | else |
4b24dd1a | 10263 | return TRUE; |
cb2366c1 EBM |
10264 | |
10265 | case NT_PPC_TM_SPR: | |
10266 | if (note->namesz == 6 | |
4b24dd1a AM |
10267 | && strcmp (note->namedata, "LINUX") == 0) |
10268 | return elfcore_grok_ppc_tm_spr (abfd, note); | |
cb2366c1 | 10269 | else |
4b24dd1a | 10270 | return TRUE; |
cb2366c1 EBM |
10271 | |
10272 | case NT_PPC_TM_CTAR: | |
10273 | if (note->namesz == 6 | |
4b24dd1a AM |
10274 | && strcmp (note->namedata, "LINUX") == 0) |
10275 | return elfcore_grok_ppc_tm_ctar (abfd, note); | |
cb2366c1 | 10276 | else |
4b24dd1a | 10277 | return TRUE; |
cb2366c1 EBM |
10278 | |
10279 | case NT_PPC_TM_CPPR: | |
10280 | if (note->namesz == 6 | |
4b24dd1a AM |
10281 | && strcmp (note->namedata, "LINUX") == 0) |
10282 | return elfcore_grok_ppc_tm_cppr (abfd, note); | |
cb2366c1 | 10283 | else |
4b24dd1a | 10284 | return TRUE; |
cb2366c1 EBM |
10285 | |
10286 | case NT_PPC_TM_CDSCR: | |
10287 | if (note->namesz == 6 | |
4b24dd1a AM |
10288 | && strcmp (note->namedata, "LINUX") == 0) |
10289 | return elfcore_grok_ppc_tm_cdscr (abfd, note); | |
cb2366c1 | 10290 | else |
4b24dd1a | 10291 | return TRUE; |
cb2366c1 | 10292 | |
0675e188 UW |
10293 | case NT_S390_HIGH_GPRS: |
10294 | if (note->namesz == 6 | |
07d6d2b8 AM |
10295 | && strcmp (note->namedata, "LINUX") == 0) |
10296 | return elfcore_grok_s390_high_gprs (abfd, note); | |
0675e188 | 10297 | else |
07d6d2b8 | 10298 | return TRUE; |
0675e188 | 10299 | |
d7eeb400 MS |
10300 | case NT_S390_TIMER: |
10301 | if (note->namesz == 6 | |
07d6d2b8 AM |
10302 | && strcmp (note->namedata, "LINUX") == 0) |
10303 | return elfcore_grok_s390_timer (abfd, note); | |
d7eeb400 | 10304 | else |
07d6d2b8 | 10305 | return TRUE; |
d7eeb400 MS |
10306 | |
10307 | case NT_S390_TODCMP: | |
10308 | if (note->namesz == 6 | |
07d6d2b8 AM |
10309 | && strcmp (note->namedata, "LINUX") == 0) |
10310 | return elfcore_grok_s390_todcmp (abfd, note); | |
d7eeb400 | 10311 | else |
07d6d2b8 | 10312 | return TRUE; |
d7eeb400 MS |
10313 | |
10314 | case NT_S390_TODPREG: | |
10315 | if (note->namesz == 6 | |
07d6d2b8 AM |
10316 | && strcmp (note->namedata, "LINUX") == 0) |
10317 | return elfcore_grok_s390_todpreg (abfd, note); | |
d7eeb400 | 10318 | else |
07d6d2b8 | 10319 | return TRUE; |
d7eeb400 MS |
10320 | |
10321 | case NT_S390_CTRS: | |
10322 | if (note->namesz == 6 | |
07d6d2b8 AM |
10323 | && strcmp (note->namedata, "LINUX") == 0) |
10324 | return elfcore_grok_s390_ctrs (abfd, note); | |
d7eeb400 | 10325 | else |
07d6d2b8 | 10326 | return TRUE; |
d7eeb400 MS |
10327 | |
10328 | case NT_S390_PREFIX: | |
10329 | if (note->namesz == 6 | |
07d6d2b8 AM |
10330 | && strcmp (note->namedata, "LINUX") == 0) |
10331 | return elfcore_grok_s390_prefix (abfd, note); | |
d7eeb400 | 10332 | else |
07d6d2b8 | 10333 | return TRUE; |
d7eeb400 | 10334 | |
355b81d9 UW |
10335 | case NT_S390_LAST_BREAK: |
10336 | if (note->namesz == 6 | |
07d6d2b8 AM |
10337 | && strcmp (note->namedata, "LINUX") == 0) |
10338 | return elfcore_grok_s390_last_break (abfd, note); | |
355b81d9 | 10339 | else |
07d6d2b8 | 10340 | return TRUE; |
355b81d9 UW |
10341 | |
10342 | case NT_S390_SYSTEM_CALL: | |
10343 | if (note->namesz == 6 | |
07d6d2b8 AM |
10344 | && strcmp (note->namedata, "LINUX") == 0) |
10345 | return elfcore_grok_s390_system_call (abfd, note); | |
355b81d9 | 10346 | else |
07d6d2b8 | 10347 | return TRUE; |
355b81d9 | 10348 | |
abb3f6cc NC |
10349 | case NT_S390_TDB: |
10350 | if (note->namesz == 6 | |
07d6d2b8 AM |
10351 | && strcmp (note->namedata, "LINUX") == 0) |
10352 | return elfcore_grok_s390_tdb (abfd, note); | |
abb3f6cc | 10353 | else |
07d6d2b8 | 10354 | return TRUE; |
abb3f6cc | 10355 | |
4ef9f41a AA |
10356 | case NT_S390_VXRS_LOW: |
10357 | if (note->namesz == 6 | |
10358 | && strcmp (note->namedata, "LINUX") == 0) | |
10359 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
10360 | else | |
10361 | return TRUE; | |
10362 | ||
10363 | case NT_S390_VXRS_HIGH: | |
10364 | if (note->namesz == 6 | |
10365 | && strcmp (note->namedata, "LINUX") == 0) | |
10366 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
10367 | else | |
10368 | return TRUE; | |
10369 | ||
88ab90e8 AA |
10370 | case NT_S390_GS_CB: |
10371 | if (note->namesz == 6 | |
10372 | && strcmp (note->namedata, "LINUX") == 0) | |
8fe09d74 | 10373 | return elfcore_grok_s390_gs_cb (abfd, note); |
88ab90e8 AA |
10374 | else |
10375 | return TRUE; | |
10376 | ||
10377 | case NT_S390_GS_BC: | |
10378 | if (note->namesz == 6 | |
10379 | && strcmp (note->namedata, "LINUX") == 0) | |
8fe09d74 | 10380 | return elfcore_grok_s390_gs_bc (abfd, note); |
88ab90e8 AA |
10381 | else |
10382 | return TRUE; | |
10383 | ||
faa9a424 UW |
10384 | case NT_ARM_VFP: |
10385 | if (note->namesz == 6 | |
10386 | && strcmp (note->namedata, "LINUX") == 0) | |
10387 | return elfcore_grok_arm_vfp (abfd, note); | |
10388 | else | |
10389 | return TRUE; | |
10390 | ||
652451f8 YZ |
10391 | case NT_ARM_TLS: |
10392 | if (note->namesz == 6 | |
10393 | && strcmp (note->namedata, "LINUX") == 0) | |
10394 | return elfcore_grok_aarch_tls (abfd, note); | |
10395 | else | |
10396 | return TRUE; | |
10397 | ||
10398 | case NT_ARM_HW_BREAK: | |
10399 | if (note->namesz == 6 | |
10400 | && strcmp (note->namedata, "LINUX") == 0) | |
10401 | return elfcore_grok_aarch_hw_break (abfd, note); | |
10402 | else | |
10403 | return TRUE; | |
10404 | ||
10405 | case NT_ARM_HW_WATCH: | |
10406 | if (note->namesz == 6 | |
10407 | && strcmp (note->namedata, "LINUX") == 0) | |
10408 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
10409 | else | |
10410 | return TRUE; | |
10411 | ||
ad1cc4e4 AH |
10412 | case NT_ARM_SVE: |
10413 | if (note->namesz == 6 | |
10414 | && strcmp (note->namedata, "LINUX") == 0) | |
10415 | return elfcore_grok_aarch_sve (abfd, note); | |
10416 | else | |
10417 | return TRUE; | |
10418 | ||
e6c3b5bf AH |
10419 | case NT_ARM_PAC_MASK: |
10420 | if (note->namesz == 6 | |
10421 | && strcmp (note->namedata, "LINUX") == 0) | |
10422 | return elfcore_grok_aarch_pauth (abfd, note); | |
10423 | else | |
10424 | return TRUE; | |
10425 | ||
252b5132 RH |
10426 | case NT_PRPSINFO: |
10427 | case NT_PSINFO: | |
bb0082d6 AM |
10428 | if (bed->elf_backend_grok_psinfo) |
10429 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 10430 | return TRUE; |
bb0082d6 | 10431 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 10432 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 10433 | #else |
b34976b6 | 10434 | return TRUE; |
252b5132 | 10435 | #endif |
3333a7c3 RM |
10436 | |
10437 | case NT_AUXV: | |
58e07198 | 10438 | return elfcore_make_auxv_note_section (abfd, note, 0); |
9015683b | 10439 | |
451b7c33 TT |
10440 | case NT_FILE: |
10441 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
10442 | note); | |
10443 | ||
9015683b TT |
10444 | case NT_SIGINFO: |
10445 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
10446 | note); | |
5b2c414d | 10447 | |
252b5132 RH |
10448 | } |
10449 | } | |
10450 | ||
718175fa JK |
10451 | static bfd_boolean |
10452 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
10453 | { | |
c74f7d1c | 10454 | struct bfd_build_id* build_id; |
30e8ee25 AM |
10455 | |
10456 | if (note->descsz == 0) | |
10457 | return FALSE; | |
10458 | ||
c74f7d1c JT |
10459 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
10460 | if (build_id == NULL) | |
718175fa JK |
10461 | return FALSE; |
10462 | ||
c74f7d1c JT |
10463 | build_id->size = note->descsz; |
10464 | memcpy (build_id->data, note->descdata, note->descsz); | |
10465 | abfd->build_id = build_id; | |
718175fa JK |
10466 | |
10467 | return TRUE; | |
10468 | } | |
10469 | ||
10470 | static bfd_boolean | |
10471 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
10472 | { | |
10473 | switch (note->type) | |
10474 | { | |
10475 | default: | |
10476 | return TRUE; | |
10477 | ||
46bed679 L |
10478 | case NT_GNU_PROPERTY_TYPE_0: |
10479 | return _bfd_elf_parse_gnu_properties (abfd, note); | |
10480 | ||
718175fa JK |
10481 | case NT_GNU_BUILD_ID: |
10482 | return elfobj_grok_gnu_build_id (abfd, note); | |
10483 | } | |
10484 | } | |
10485 | ||
e21e5835 NC |
10486 | static bfd_boolean |
10487 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
10488 | { | |
10489 | struct sdt_note *cur = | |
7a6e0d89 AM |
10490 | (struct sdt_note *) bfd_alloc (abfd, |
10491 | sizeof (struct sdt_note) + note->descsz); | |
e21e5835 NC |
10492 | |
10493 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
10494 | cur->size = (bfd_size_type) note->descsz; | |
10495 | memcpy (cur->data, note->descdata, note->descsz); | |
10496 | ||
10497 | elf_tdata (abfd)->sdt_note_head = cur; | |
10498 | ||
10499 | return TRUE; | |
10500 | } | |
10501 | ||
10502 | static bfd_boolean | |
10503 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
10504 | { | |
10505 | switch (note->type) | |
10506 | { | |
10507 | case NT_STAPSDT: | |
10508 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
10509 | ||
10510 | default: | |
10511 | return TRUE; | |
10512 | } | |
10513 | } | |
10514 | ||
aa1ed4a9 JB |
10515 | static bfd_boolean |
10516 | elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
10517 | { | |
10518 | size_t offset; | |
10519 | ||
b5430a3c | 10520 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 10521 | { |
b5430a3c | 10522 | case ELFCLASS32: |
0064d223 JB |
10523 | if (note->descsz < 108) |
10524 | return FALSE; | |
aa1ed4a9 JB |
10525 | break; |
10526 | ||
b5430a3c | 10527 | case ELFCLASS64: |
0064d223 JB |
10528 | if (note->descsz < 120) |
10529 | return FALSE; | |
aa1ed4a9 JB |
10530 | break; |
10531 | ||
10532 | default: | |
10533 | return FALSE; | |
10534 | } | |
10535 | ||
0064d223 JB |
10536 | /* Check for version 1 in pr_version. */ |
10537 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
10538 | return FALSE; | |
80a04378 | 10539 | |
0064d223 JB |
10540 | offset = 4; |
10541 | ||
10542 | /* Skip over pr_psinfosz. */ | |
b5430a3c | 10543 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
0064d223 JB |
10544 | offset += 4; |
10545 | else | |
10546 | { | |
10547 | offset += 4; /* Padding before pr_psinfosz. */ | |
10548 | offset += 8; | |
10549 | } | |
10550 | ||
aa1ed4a9 JB |
10551 | /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ |
10552 | elf_tdata (abfd)->core->program | |
10553 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); | |
10554 | offset += 17; | |
10555 | ||
10556 | /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ | |
10557 | elf_tdata (abfd)->core->command | |
10558 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); | |
0064d223 JB |
10559 | offset += 81; |
10560 | ||
10561 | /* Padding before pr_pid. */ | |
10562 | offset += 2; | |
10563 | ||
10564 | /* The pr_pid field was added in version "1a". */ | |
10565 | if (note->descsz < offset + 4) | |
10566 | return TRUE; | |
10567 | ||
10568 | elf_tdata (abfd)->core->pid | |
10569 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
aa1ed4a9 JB |
10570 | |
10571 | return TRUE; | |
10572 | } | |
10573 | ||
10574 | static bfd_boolean | |
10575 | elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) | |
10576 | { | |
10577 | size_t offset; | |
10578 | size_t size; | |
24d3e51b | 10579 | size_t min_size; |
aa1ed4a9 | 10580 | |
24d3e51b NC |
10581 | /* Compute offset of pr_getregsz, skipping over pr_statussz. |
10582 | Also compute minimum size of this note. */ | |
b5430a3c | 10583 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 10584 | { |
b5430a3c | 10585 | case ELFCLASS32: |
24d3e51b NC |
10586 | offset = 4 + 4; |
10587 | min_size = offset + (4 * 2) + 4 + 4 + 4; | |
aa1ed4a9 JB |
10588 | break; |
10589 | ||
b5430a3c | 10590 | case ELFCLASS64: |
24d3e51b NC |
10591 | offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */ |
10592 | min_size = offset + (8 * 2) + 4 + 4 + 4 + 4; | |
aa1ed4a9 JB |
10593 | break; |
10594 | ||
10595 | default: | |
10596 | return FALSE; | |
10597 | } | |
10598 | ||
24d3e51b NC |
10599 | if (note->descsz < min_size) |
10600 | return FALSE; | |
10601 | ||
10602 | /* Check for version 1 in pr_version. */ | |
10603 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
10604 | return FALSE; | |
aa1ed4a9 | 10605 | |
24d3e51b NC |
10606 | /* Extract size of pr_reg from pr_gregsetsz. */ |
10607 | /* Skip over pr_gregsetsz and pr_fpregsetsz. */ | |
b5430a3c | 10608 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
24d3e51b NC |
10609 | { |
10610 | size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10611 | offset += 4 * 2; | |
10612 | } | |
b5430a3c | 10613 | else |
24d3e51b NC |
10614 | { |
10615 | size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); | |
10616 | offset += 8 * 2; | |
10617 | } | |
aa1ed4a9 | 10618 | |
24d3e51b | 10619 | /* Skip over pr_osreldate. */ |
aa1ed4a9 JB |
10620 | offset += 4; |
10621 | ||
24d3e51b | 10622 | /* Read signal from pr_cursig. */ |
aa1ed4a9 JB |
10623 | if (elf_tdata (abfd)->core->signal == 0) |
10624 | elf_tdata (abfd)->core->signal | |
10625 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10626 | offset += 4; | |
10627 | ||
24d3e51b | 10628 | /* Read TID from pr_pid. */ |
aa1ed4a9 JB |
10629 | elf_tdata (abfd)->core->lwpid |
10630 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10631 | offset += 4; | |
10632 | ||
24d3e51b | 10633 | /* Padding before pr_reg. */ |
b5430a3c | 10634 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) |
aa1ed4a9 JB |
10635 | offset += 4; |
10636 | ||
24d3e51b NC |
10637 | /* Make sure that there is enough data remaining in the note. */ |
10638 | if ((note->descsz - offset) < size) | |
10639 | return FALSE; | |
10640 | ||
aa1ed4a9 JB |
10641 | /* Make a ".reg/999" section and a ".reg" section. */ |
10642 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
10643 | size, note->descpos + offset); | |
10644 | } | |
10645 | ||
10646 | static bfd_boolean | |
10647 | elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10648 | { | |
544c67cd JB |
10649 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10650 | ||
aa1ed4a9 JB |
10651 | switch (note->type) |
10652 | { | |
10653 | case NT_PRSTATUS: | |
544c67cd JB |
10654 | if (bed->elf_backend_grok_freebsd_prstatus) |
10655 | if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note)) | |
10656 | return TRUE; | |
aa1ed4a9 JB |
10657 | return elfcore_grok_freebsd_prstatus (abfd, note); |
10658 | ||
10659 | case NT_FPREGSET: | |
10660 | return elfcore_grok_prfpreg (abfd, note); | |
10661 | ||
10662 | case NT_PRPSINFO: | |
10663 | return elfcore_grok_freebsd_psinfo (abfd, note); | |
10664 | ||
10665 | case NT_FREEBSD_THRMISC: | |
10666 | if (note->namesz == 8) | |
10667 | return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); | |
10668 | else | |
10669 | return TRUE; | |
10670 | ||
ddb2bbcf JB |
10671 | case NT_FREEBSD_PROCSTAT_PROC: |
10672 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc", | |
10673 | note); | |
10674 | ||
10675 | case NT_FREEBSD_PROCSTAT_FILES: | |
10676 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files", | |
10677 | note); | |
10678 | ||
10679 | case NT_FREEBSD_PROCSTAT_VMMAP: | |
10680 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap", | |
10681 | note); | |
10682 | ||
3350c5f5 | 10683 | case NT_FREEBSD_PROCSTAT_AUXV: |
58e07198 | 10684 | return elfcore_make_auxv_note_section (abfd, note, 4); |
3350c5f5 | 10685 | |
aa1ed4a9 JB |
10686 | case NT_X86_XSTATE: |
10687 | if (note->namesz == 8) | |
10688 | return elfcore_grok_xstatereg (abfd, note); | |
10689 | else | |
10690 | return TRUE; | |
10691 | ||
e6f3b9c3 JB |
10692 | case NT_FREEBSD_PTLWPINFO: |
10693 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo", | |
10694 | note); | |
10695 | ||
6d5be5d6 JB |
10696 | case NT_ARM_VFP: |
10697 | return elfcore_grok_arm_vfp (abfd, note); | |
10698 | ||
aa1ed4a9 JB |
10699 | default: |
10700 | return TRUE; | |
10701 | } | |
10702 | } | |
10703 | ||
b34976b6 | 10704 | static bfd_boolean |
217aa764 | 10705 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
10706 | { |
10707 | char *cp; | |
10708 | ||
10709 | cp = strchr (note->namedata, '@'); | |
10710 | if (cp != NULL) | |
10711 | { | |
d2b64500 | 10712 | *lwpidp = atoi(cp + 1); |
b34976b6 | 10713 | return TRUE; |
50b2bdb7 | 10714 | } |
b34976b6 | 10715 | return FALSE; |
50b2bdb7 AM |
10716 | } |
10717 | ||
b34976b6 | 10718 | static bfd_boolean |
217aa764 | 10719 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 10720 | { |
80a04378 NC |
10721 | if (note->descsz <= 0x7c + 31) |
10722 | return FALSE; | |
10723 | ||
50b2bdb7 | 10724 | /* Signal number at offset 0x08. */ |
228e534f | 10725 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
10726 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10727 | ||
10728 | /* Process ID at offset 0x50. */ | |
228e534f | 10729 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
10730 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
10731 | ||
10732 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 10733 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
10734 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
10735 | ||
7720ba9f MK |
10736 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
10737 | note); | |
50b2bdb7 AM |
10738 | } |
10739 | ||
b34976b6 | 10740 | static bfd_boolean |
217aa764 | 10741 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
10742 | { |
10743 | int lwp; | |
10744 | ||
10745 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 10746 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 10747 | |
58e07198 | 10748 | switch (note->type) |
50b2bdb7 | 10749 | { |
58e07198 | 10750 | case NT_NETBSDCORE_PROCINFO: |
50b2bdb7 | 10751 | /* NetBSD-specific core "procinfo". Note that we expect to |
08a40648 AM |
10752 | find this note before any of the others, which is fine, |
10753 | since the kernel writes this note out first when it | |
10754 | creates a core file. */ | |
50b2bdb7 | 10755 | return elfcore_grok_netbsd_procinfo (abfd, note); |
58e07198 CZ |
10756 | #ifdef NT_NETBSDCORE_AUXV |
10757 | case NT_NETBSDCORE_AUXV: | |
10758 | /* NetBSD-specific Elf Auxiliary Vector data. */ | |
10759 | return elfcore_make_auxv_note_section (abfd, note, 4); | |
06d949ec KR |
10760 | #endif |
10761 | #ifdef NT_NETBSDCORE_LWPSTATUS | |
10762 | case NT_NETBSDCORE_LWPSTATUS: | |
10763 | return elfcore_make_note_pseudosection (abfd, | |
10764 | ".note.netbsdcore.lwpstatus", | |
10765 | note); | |
58e07198 CZ |
10766 | #endif |
10767 | default: | |
10768 | break; | |
50b2bdb7 AM |
10769 | } |
10770 | ||
06d949ec | 10771 | /* As of March 2020 there are no other machine-independent notes |
b4db1224 JT |
10772 | defined for NetBSD core files. If the note type is less |
10773 | than the start of the machine-dependent note types, we don't | |
10774 | understand it. */ | |
47d9a591 | 10775 | |
b4db1224 | 10776 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 10777 | return TRUE; |
50b2bdb7 AM |
10778 | |
10779 | ||
10780 | switch (bfd_get_arch (abfd)) | |
10781 | { | |
08a40648 AM |
10782 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
10783 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 | 10784 | |
015ec493 | 10785 | case bfd_arch_aarch64: |
50b2bdb7 AM |
10786 | case bfd_arch_alpha: |
10787 | case bfd_arch_sparc: | |
10788 | switch (note->type) | |
08a40648 AM |
10789 | { |
10790 | case NT_NETBSDCORE_FIRSTMACH+0: | |
10791 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10792 | |
08a40648 AM |
10793 | case NT_NETBSDCORE_FIRSTMACH+2: |
10794 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10795 | |
08a40648 AM |
10796 | default: |
10797 | return TRUE; | |
10798 | } | |
50b2bdb7 | 10799 | |
58e07198 CZ |
10800 | /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5. |
10801 | There's also old PT___GETREGS40 == mach + 1 for old reg | |
10802 | structure which lacks GBR. */ | |
10803 | ||
10804 | case bfd_arch_sh: | |
10805 | switch (note->type) | |
10806 | { | |
10807 | case NT_NETBSDCORE_FIRSTMACH+3: | |
10808 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
10809 | ||
10810 | case NT_NETBSDCORE_FIRSTMACH+5: | |
10811 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
10812 | ||
10813 | default: | |
10814 | return TRUE; | |
10815 | } | |
10816 | ||
08a40648 AM |
10817 | /* On all other arch's, PT_GETREGS == mach+1 and |
10818 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
10819 | |
10820 | default: | |
10821 | switch (note->type) | |
08a40648 AM |
10822 | { |
10823 | case NT_NETBSDCORE_FIRSTMACH+1: | |
10824 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10825 | |
08a40648 AM |
10826 | case NT_NETBSDCORE_FIRSTMACH+3: |
10827 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10828 | |
08a40648 AM |
10829 | default: |
10830 | return TRUE; | |
10831 | } | |
50b2bdb7 AM |
10832 | } |
10833 | /* NOTREACHED */ | |
10834 | } | |
10835 | ||
67cc5033 MK |
10836 | static bfd_boolean |
10837 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
10838 | { | |
80a04378 NC |
10839 | if (note->descsz <= 0x48 + 31) |
10840 | return FALSE; | |
10841 | ||
67cc5033 | 10842 | /* Signal number at offset 0x08. */ |
228e534f | 10843 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
10844 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10845 | ||
10846 | /* Process ID at offset 0x20. */ | |
228e534f | 10847 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
10848 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
10849 | ||
10850 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 10851 | elf_tdata (abfd)->core->command |
67cc5033 MK |
10852 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
10853 | ||
10854 | return TRUE; | |
10855 | } | |
10856 | ||
10857 | static bfd_boolean | |
10858 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10859 | { | |
10860 | if (note->type == NT_OPENBSD_PROCINFO) | |
10861 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
10862 | ||
10863 | if (note->type == NT_OPENBSD_REGS) | |
10864 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
10865 | ||
10866 | if (note->type == NT_OPENBSD_FPREGS) | |
10867 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
10868 | ||
10869 | if (note->type == NT_OPENBSD_XFPREGS) | |
10870 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
10871 | ||
10872 | if (note->type == NT_OPENBSD_AUXV) | |
58e07198 | 10873 | return elfcore_make_auxv_note_section (abfd, note, 0); |
67cc5033 MK |
10874 | |
10875 | if (note->type == NT_OPENBSD_WCOOKIE) | |
10876 | { | |
10877 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
10878 | SEC_HAS_CONTENTS); | |
10879 | ||
10880 | if (sect == NULL) | |
10881 | return FALSE; | |
10882 | sect->size = note->descsz; | |
10883 | sect->filepos = note->descpos; | |
10884 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10885 | ||
10886 | return TRUE; | |
10887 | } | |
10888 | ||
10889 | return TRUE; | |
10890 | } | |
10891 | ||
07c6e936 | 10892 | static bfd_boolean |
d3fd4074 | 10893 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
10894 | { |
10895 | void *ddata = note->descdata; | |
10896 | char buf[100]; | |
10897 | char *name; | |
10898 | asection *sect; | |
f8843e87 AM |
10899 | short sig; |
10900 | unsigned flags; | |
07c6e936 | 10901 | |
80a04378 NC |
10902 | if (note->descsz < 16) |
10903 | return FALSE; | |
10904 | ||
07c6e936 | 10905 | /* nto_procfs_status 'pid' field is at offset 0. */ |
228e534f | 10906 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 10907 | |
f8843e87 AM |
10908 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
10909 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
10910 | ||
10911 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
10912 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
10913 | |
10914 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
10915 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
10916 | { | |
228e534f AM |
10917 | elf_tdata (abfd)->core->signal = sig; |
10918 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 10919 | } |
07c6e936 | 10920 | |
f8843e87 AM |
10921 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
10922 | do not come from signals so we make sure we set the current | |
10923 | thread just in case. */ | |
10924 | if (flags & 0x00000080) | |
228e534f | 10925 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
10926 | |
10927 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 10928 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 10929 | |
a50b1753 | 10930 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10931 | if (name == NULL) |
10932 | return FALSE; | |
10933 | strcpy (name, buf); | |
10934 | ||
117ed4f8 | 10935 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10936 | if (sect == NULL) |
10937 | return FALSE; | |
10938 | ||
07d6d2b8 AM |
10939 | sect->size = note->descsz; |
10940 | sect->filepos = note->descpos; | |
07c6e936 NC |
10941 | sect->alignment_power = 2; |
10942 | ||
10943 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
10944 | } | |
10945 | ||
10946 | static bfd_boolean | |
d69f560c KW |
10947 | elfcore_grok_nto_regs (bfd *abfd, |
10948 | Elf_Internal_Note *note, | |
d3fd4074 | 10949 | long tid, |
d69f560c | 10950 | char *base) |
07c6e936 NC |
10951 | { |
10952 | char buf[100]; | |
10953 | char *name; | |
10954 | asection *sect; | |
10955 | ||
d69f560c | 10956 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 10957 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 10958 | |
a50b1753 | 10959 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10960 | if (name == NULL) |
10961 | return FALSE; | |
10962 | strcpy (name, buf); | |
10963 | ||
117ed4f8 | 10964 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10965 | if (sect == NULL) |
10966 | return FALSE; | |
10967 | ||
07d6d2b8 AM |
10968 | sect->size = note->descsz; |
10969 | sect->filepos = note->descpos; | |
07c6e936 NC |
10970 | sect->alignment_power = 2; |
10971 | ||
f8843e87 | 10972 | /* This is the current thread. */ |
228e534f | 10973 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 10974 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
10975 | |
10976 | return TRUE; | |
07c6e936 NC |
10977 | } |
10978 | ||
10979 | #define BFD_QNT_CORE_INFO 7 | |
10980 | #define BFD_QNT_CORE_STATUS 8 | |
10981 | #define BFD_QNT_CORE_GREG 9 | |
10982 | #define BFD_QNT_CORE_FPREG 10 | |
10983 | ||
10984 | static bfd_boolean | |
217aa764 | 10985 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
10986 | { |
10987 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 10988 | tid from the previous call to pass down to the next gregs |
07c6e936 | 10989 | function. */ |
d3fd4074 | 10990 | static long tid = 1; |
07c6e936 NC |
10991 | |
10992 | switch (note->type) | |
10993 | { | |
d69f560c KW |
10994 | case BFD_QNT_CORE_INFO: |
10995 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
10996 | case BFD_QNT_CORE_STATUS: | |
10997 | return elfcore_grok_nto_status (abfd, note, &tid); | |
10998 | case BFD_QNT_CORE_GREG: | |
10999 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
11000 | case BFD_QNT_CORE_FPREG: | |
11001 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
11002 | default: | |
11003 | return TRUE; | |
07c6e936 NC |
11004 | } |
11005 | } | |
11006 | ||
b15fa79e AM |
11007 | static bfd_boolean |
11008 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
11009 | { | |
11010 | char *name; | |
11011 | asection *sect; | |
11012 | size_t len; | |
11013 | ||
11014 | /* Use note name as section name. */ | |
11015 | len = note->namesz; | |
a50b1753 | 11016 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
11017 | if (name == NULL) |
11018 | return FALSE; | |
11019 | memcpy (name, note->namedata, len); | |
11020 | name[len - 1] = '\0'; | |
11021 | ||
11022 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
11023 | if (sect == NULL) | |
11024 | return FALSE; | |
11025 | ||
07d6d2b8 AM |
11026 | sect->size = note->descsz; |
11027 | sect->filepos = note->descpos; | |
b15fa79e AM |
11028 | sect->alignment_power = 1; |
11029 | ||
11030 | return TRUE; | |
11031 | } | |
11032 | ||
7c76fa91 MS |
11033 | /* Function: elfcore_write_note |
11034 | ||
47d9a591 | 11035 | Inputs: |
a39f3346 | 11036 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
11037 | name of note |
11038 | type of note | |
11039 | data for note | |
11040 | size of data for note | |
11041 | ||
a39f3346 AM |
11042 | Writes note to end of buffer. ELF64 notes are written exactly as |
11043 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
11044 | that they ought to have 8-byte namesz and descsz field, and have | |
11045 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
11046 | ||
7c76fa91 | 11047 | Return: |
a39f3346 | 11048 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
11049 | |
11050 | char * | |
a39f3346 | 11051 | elfcore_write_note (bfd *abfd, |
217aa764 | 11052 | char *buf, |
a39f3346 | 11053 | int *bufsiz, |
217aa764 | 11054 | const char *name, |
a39f3346 | 11055 | int type, |
217aa764 | 11056 | const void *input, |
a39f3346 | 11057 | int size) |
7c76fa91 MS |
11058 | { |
11059 | Elf_External_Note *xnp; | |
d4c88bbb | 11060 | size_t namesz; |
d4c88bbb | 11061 | size_t newspace; |
a39f3346 | 11062 | char *dest; |
7c76fa91 | 11063 | |
d4c88bbb | 11064 | namesz = 0; |
d4c88bbb | 11065 | if (name != NULL) |
a39f3346 | 11066 | namesz = strlen (name) + 1; |
d4c88bbb | 11067 | |
a39f3346 | 11068 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 11069 | |
a50b1753 | 11070 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
11071 | if (buf == NULL) |
11072 | return buf; | |
a39f3346 | 11073 | dest = buf + *bufsiz; |
7c76fa91 MS |
11074 | *bufsiz += newspace; |
11075 | xnp = (Elf_External_Note *) dest; | |
11076 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
11077 | H_PUT_32 (abfd, size, xnp->descsz); | |
11078 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
11079 | dest = xnp->name; |
11080 | if (name != NULL) | |
11081 | { | |
11082 | memcpy (dest, name, namesz); | |
11083 | dest += namesz; | |
a39f3346 | 11084 | while (namesz & 3) |
d4c88bbb AM |
11085 | { |
11086 | *dest++ = '\0'; | |
a39f3346 | 11087 | ++namesz; |
d4c88bbb AM |
11088 | } |
11089 | } | |
11090 | memcpy (dest, input, size); | |
a39f3346 AM |
11091 | dest += size; |
11092 | while (size & 3) | |
11093 | { | |
11094 | *dest++ = '\0'; | |
11095 | ++size; | |
11096 | } | |
11097 | return buf; | |
7c76fa91 MS |
11098 | } |
11099 | ||
602f1657 AM |
11100 | /* gcc-8 warns (*) on all the strncpy calls in this function about |
11101 | possible string truncation. The "truncation" is not a bug. We | |
11102 | have an external representation of structs with fields that are not | |
11103 | necessarily NULL terminated and corresponding internal | |
11104 | representation fields that are one larger so that they can always | |
11105 | be NULL terminated. | |
11106 | gcc versions between 4.2 and 4.6 do not allow pragma control of | |
11107 | diagnostics inside functions, giving a hard error if you try to use | |
11108 | the finer control available with later versions. | |
11109 | gcc prior to 4.2 warns about diagnostic push and pop. | |
11110 | gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown, | |
11111 | unless you also add #pragma GCC diagnostic ignored "-Wpragma". | |
11112 | (*) Depending on your system header files! */ | |
d99b4b92 | 11113 | #if GCC_VERSION >= 8000 |
602f1657 AM |
11114 | # pragma GCC diagnostic push |
11115 | # pragma GCC diagnostic ignored "-Wstringop-truncation" | |
d99b4b92 | 11116 | #endif |
7c76fa91 | 11117 | char * |
217aa764 AM |
11118 | elfcore_write_prpsinfo (bfd *abfd, |
11119 | char *buf, | |
11120 | int *bufsiz, | |
11121 | const char *fname, | |
11122 | const char *psargs) | |
7c76fa91 | 11123 | { |
183e98be AM |
11124 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11125 | ||
11126 | if (bed->elf_backend_write_core_note != NULL) | |
11127 | { | |
11128 | char *ret; | |
11129 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
11130 | NT_PRPSINFO, fname, psargs); | |
11131 | if (ret != NULL) | |
11132 | return ret; | |
11133 | } | |
7c76fa91 | 11134 | |
1f20dca5 | 11135 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
602f1657 | 11136 | # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
183e98be AM |
11137 | if (bed->s->elfclass == ELFCLASS32) |
11138 | { | |
602f1657 | 11139 | # if defined (HAVE_PSINFO32_T) |
183e98be AM |
11140 | psinfo32_t data; |
11141 | int note_type = NT_PSINFO; | |
602f1657 | 11142 | # else |
183e98be AM |
11143 | prpsinfo32_t data; |
11144 | int note_type = NT_PRPSINFO; | |
602f1657 | 11145 | # endif |
183e98be AM |
11146 | |
11147 | memset (&data, 0, sizeof (data)); | |
11148 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
11149 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
11150 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 11151 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
11152 | } |
11153 | else | |
602f1657 | 11154 | # endif |
183e98be | 11155 | { |
602f1657 | 11156 | # if defined (HAVE_PSINFO_T) |
183e98be AM |
11157 | psinfo_t data; |
11158 | int note_type = NT_PSINFO; | |
602f1657 | 11159 | # else |
183e98be AM |
11160 | prpsinfo_t data; |
11161 | int note_type = NT_PRPSINFO; | |
602f1657 | 11162 | # endif |
7c76fa91 | 11163 | |
183e98be AM |
11164 | memset (&data, 0, sizeof (data)); |
11165 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
11166 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
11167 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 11168 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 11169 | } |
7c76fa91 MS |
11170 | #endif /* PSINFO_T or PRPSINFO_T */ |
11171 | ||
1f20dca5 UW |
11172 | free (buf); |
11173 | return NULL; | |
11174 | } | |
d99b4b92 | 11175 | #if GCC_VERSION >= 8000 |
602f1657 | 11176 | # pragma GCC diagnostic pop |
d99b4b92 | 11177 | #endif |
1f20dca5 | 11178 | |
70a38d42 SDJ |
11179 | char * |
11180 | elfcore_write_linux_prpsinfo32 | |
11181 | (bfd *abfd, char *buf, int *bufsiz, | |
11182 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
11183 | { | |
a2f63b2e MR |
11184 | if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16) |
11185 | { | |
11186 | struct elf_external_linux_prpsinfo32_ugid16 data; | |
11187 | ||
11188 | swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data); | |
11189 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
11190 | &data, sizeof (data)); | |
11191 | } | |
11192 | else | |
11193 | { | |
11194 | struct elf_external_linux_prpsinfo32_ugid32 data; | |
70a38d42 | 11195 | |
a2f63b2e MR |
11196 | swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data); |
11197 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
11198 | &data, sizeof (data)); | |
11199 | } | |
70a38d42 SDJ |
11200 | } |
11201 | ||
11202 | char * | |
11203 | elfcore_write_linux_prpsinfo64 | |
11204 | (bfd *abfd, char *buf, int *bufsiz, | |
11205 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
11206 | { | |
3c9a7b0d MR |
11207 | if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16) |
11208 | { | |
11209 | struct elf_external_linux_prpsinfo64_ugid16 data; | |
11210 | ||
11211 | swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data); | |
11212 | return elfcore_write_note (abfd, buf, bufsiz, | |
11213 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
11214 | } | |
11215 | else | |
11216 | { | |
11217 | struct elf_external_linux_prpsinfo64_ugid32 data; | |
70a38d42 | 11218 | |
3c9a7b0d MR |
11219 | swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data); |
11220 | return elfcore_write_note (abfd, buf, bufsiz, | |
11221 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
11222 | } | |
70a38d42 SDJ |
11223 | } |
11224 | ||
7c76fa91 | 11225 | char * |
217aa764 AM |
11226 | elfcore_write_prstatus (bfd *abfd, |
11227 | char *buf, | |
11228 | int *bufsiz, | |
11229 | long pid, | |
11230 | int cursig, | |
11231 | const void *gregs) | |
7c76fa91 | 11232 | { |
183e98be | 11233 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 11234 | |
183e98be AM |
11235 | if (bed->elf_backend_write_core_note != NULL) |
11236 | { | |
11237 | char *ret; | |
11238 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
11239 | NT_PRSTATUS, | |
11240 | pid, cursig, gregs); | |
11241 | if (ret != NULL) | |
11242 | return ret; | |
11243 | } | |
11244 | ||
1f20dca5 | 11245 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
11246 | #if defined (HAVE_PRSTATUS32_T) |
11247 | if (bed->s->elfclass == ELFCLASS32) | |
11248 | { | |
11249 | prstatus32_t prstat; | |
11250 | ||
11251 | memset (&prstat, 0, sizeof (prstat)); | |
11252 | prstat.pr_pid = pid; | |
11253 | prstat.pr_cursig = cursig; | |
11254 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 11255 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
11256 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
11257 | } | |
11258 | else | |
11259 | #endif | |
11260 | { | |
11261 | prstatus_t prstat; | |
11262 | ||
11263 | memset (&prstat, 0, sizeof (prstat)); | |
11264 | prstat.pr_pid = pid; | |
11265 | prstat.pr_cursig = cursig; | |
11266 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 11267 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
11268 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
11269 | } | |
7c76fa91 MS |
11270 | #endif /* HAVE_PRSTATUS_T */ |
11271 | ||
1f20dca5 UW |
11272 | free (buf); |
11273 | return NULL; | |
11274 | } | |
11275 | ||
51316059 MS |
11276 | #if defined (HAVE_LWPSTATUS_T) |
11277 | char * | |
217aa764 AM |
11278 | elfcore_write_lwpstatus (bfd *abfd, |
11279 | char *buf, | |
11280 | int *bufsiz, | |
11281 | long pid, | |
11282 | int cursig, | |
11283 | const void *gregs) | |
51316059 MS |
11284 | { |
11285 | lwpstatus_t lwpstat; | |
183e98be | 11286 | const char *note_name = "CORE"; |
51316059 MS |
11287 | |
11288 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
11289 | lwpstat.pr_lwpid = pid >> 16; | |
11290 | lwpstat.pr_cursig = cursig; | |
11291 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 11292 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
11293 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
11294 | #if !defined(gregs) | |
11295 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
11296 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
11297 | #else | |
11298 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
11299 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
11300 | #endif | |
11301 | #endif | |
47d9a591 | 11302 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
11303 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
11304 | } | |
11305 | #endif /* HAVE_LWPSTATUS_T */ | |
11306 | ||
7c76fa91 MS |
11307 | #if defined (HAVE_PSTATUS_T) |
11308 | char * | |
217aa764 AM |
11309 | elfcore_write_pstatus (bfd *abfd, |
11310 | char *buf, | |
11311 | int *bufsiz, | |
11312 | long pid, | |
6c10990d NC |
11313 | int cursig ATTRIBUTE_UNUSED, |
11314 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 11315 | { |
183e98be AM |
11316 | const char *note_name = "CORE"; |
11317 | #if defined (HAVE_PSTATUS32_T) | |
11318 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 11319 | |
183e98be AM |
11320 | if (bed->s->elfclass == ELFCLASS32) |
11321 | { | |
11322 | pstatus32_t pstat; | |
11323 | ||
11324 | memset (&pstat, 0, sizeof (pstat)); | |
11325 | pstat.pr_pid = pid & 0xffff; | |
11326 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
11327 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
11328 | return buf; | |
11329 | } | |
11330 | else | |
11331 | #endif | |
11332 | { | |
11333 | pstatus_t pstat; | |
11334 | ||
11335 | memset (&pstat, 0, sizeof (pstat)); | |
11336 | pstat.pr_pid = pid & 0xffff; | |
11337 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
11338 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
11339 | return buf; | |
11340 | } | |
7c76fa91 MS |
11341 | } |
11342 | #endif /* HAVE_PSTATUS_T */ | |
11343 | ||
11344 | char * | |
217aa764 AM |
11345 | elfcore_write_prfpreg (bfd *abfd, |
11346 | char *buf, | |
11347 | int *bufsiz, | |
11348 | const void *fpregs, | |
11349 | int size) | |
7c76fa91 | 11350 | { |
183e98be | 11351 | const char *note_name = "CORE"; |
47d9a591 | 11352 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
11353 | note_name, NT_FPREGSET, fpregs, size); |
11354 | } | |
11355 | ||
11356 | char * | |
217aa764 AM |
11357 | elfcore_write_prxfpreg (bfd *abfd, |
11358 | char *buf, | |
11359 | int *bufsiz, | |
11360 | const void *xfpregs, | |
11361 | int size) | |
7c76fa91 MS |
11362 | { |
11363 | char *note_name = "LINUX"; | |
47d9a591 | 11364 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
11365 | note_name, NT_PRXFPREG, xfpregs, size); |
11366 | } | |
11367 | ||
4339cae0 L |
11368 | char * |
11369 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
11370 | const void *xfpregs, int size) | |
11371 | { | |
97de3545 JB |
11372 | char *note_name; |
11373 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
11374 | note_name = "FreeBSD"; | |
11375 | else | |
11376 | note_name = "LINUX"; | |
4339cae0 L |
11377 | return elfcore_write_note (abfd, buf, bufsiz, |
11378 | note_name, NT_X86_XSTATE, xfpregs, size); | |
11379 | } | |
11380 | ||
97753bd5 AM |
11381 | char * |
11382 | elfcore_write_ppc_vmx (bfd *abfd, | |
11383 | char *buf, | |
11384 | int *bufsiz, | |
11385 | const void *ppc_vmx, | |
11386 | int size) | |
11387 | { | |
11388 | char *note_name = "LINUX"; | |
11389 | return elfcore_write_note (abfd, buf, bufsiz, | |
11390 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
11391 | } | |
11392 | ||
89eeb0bc LM |
11393 | char * |
11394 | elfcore_write_ppc_vsx (bfd *abfd, | |
07d6d2b8 AM |
11395 | char *buf, |
11396 | int *bufsiz, | |
11397 | const void *ppc_vsx, | |
11398 | int size) | |
89eeb0bc LM |
11399 | { |
11400 | char *note_name = "LINUX"; | |
11401 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11402 | note_name, NT_PPC_VSX, ppc_vsx, size); |
89eeb0bc LM |
11403 | } |
11404 | ||
cb2366c1 EBM |
11405 | char * |
11406 | elfcore_write_ppc_tar (bfd *abfd, | |
4b24dd1a AM |
11407 | char *buf, |
11408 | int *bufsiz, | |
11409 | const void *ppc_tar, | |
11410 | int size) | |
cb2366c1 EBM |
11411 | { |
11412 | char *note_name = "LINUX"; | |
11413 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11414 | note_name, NT_PPC_TAR, ppc_tar, size); |
cb2366c1 EBM |
11415 | } |
11416 | ||
11417 | char * | |
11418 | elfcore_write_ppc_ppr (bfd *abfd, | |
4b24dd1a AM |
11419 | char *buf, |
11420 | int *bufsiz, | |
11421 | const void *ppc_ppr, | |
11422 | int size) | |
cb2366c1 EBM |
11423 | { |
11424 | char *note_name = "LINUX"; | |
11425 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11426 | note_name, NT_PPC_PPR, ppc_ppr, size); |
cb2366c1 EBM |
11427 | } |
11428 | ||
11429 | char * | |
11430 | elfcore_write_ppc_dscr (bfd *abfd, | |
4b24dd1a AM |
11431 | char *buf, |
11432 | int *bufsiz, | |
11433 | const void *ppc_dscr, | |
11434 | int size) | |
cb2366c1 EBM |
11435 | { |
11436 | char *note_name = "LINUX"; | |
11437 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11438 | note_name, NT_PPC_DSCR, ppc_dscr, size); |
cb2366c1 EBM |
11439 | } |
11440 | ||
11441 | char * | |
11442 | elfcore_write_ppc_ebb (bfd *abfd, | |
4b24dd1a AM |
11443 | char *buf, |
11444 | int *bufsiz, | |
11445 | const void *ppc_ebb, | |
11446 | int size) | |
cb2366c1 EBM |
11447 | { |
11448 | char *note_name = "LINUX"; | |
11449 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11450 | note_name, NT_PPC_EBB, ppc_ebb, size); |
cb2366c1 EBM |
11451 | } |
11452 | ||
11453 | char * | |
11454 | elfcore_write_ppc_pmu (bfd *abfd, | |
4b24dd1a AM |
11455 | char *buf, |
11456 | int *bufsiz, | |
11457 | const void *ppc_pmu, | |
11458 | int size) | |
cb2366c1 EBM |
11459 | { |
11460 | char *note_name = "LINUX"; | |
11461 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11462 | note_name, NT_PPC_PMU, ppc_pmu, size); |
cb2366c1 EBM |
11463 | } |
11464 | ||
11465 | char * | |
11466 | elfcore_write_ppc_tm_cgpr (bfd *abfd, | |
4b24dd1a AM |
11467 | char *buf, |
11468 | int *bufsiz, | |
11469 | const void *ppc_tm_cgpr, | |
11470 | int size) | |
cb2366c1 EBM |
11471 | { |
11472 | char *note_name = "LINUX"; | |
11473 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11474 | note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size); |
cb2366c1 EBM |
11475 | } |
11476 | ||
11477 | char * | |
11478 | elfcore_write_ppc_tm_cfpr (bfd *abfd, | |
4b24dd1a AM |
11479 | char *buf, |
11480 | int *bufsiz, | |
11481 | const void *ppc_tm_cfpr, | |
11482 | int size) | |
cb2366c1 EBM |
11483 | { |
11484 | char *note_name = "LINUX"; | |
11485 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11486 | note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size); |
cb2366c1 EBM |
11487 | } |
11488 | ||
11489 | char * | |
11490 | elfcore_write_ppc_tm_cvmx (bfd *abfd, | |
4b24dd1a AM |
11491 | char *buf, |
11492 | int *bufsiz, | |
11493 | const void *ppc_tm_cvmx, | |
11494 | int size) | |
cb2366c1 EBM |
11495 | { |
11496 | char *note_name = "LINUX"; | |
11497 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11498 | note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size); |
cb2366c1 EBM |
11499 | } |
11500 | ||
11501 | char * | |
11502 | elfcore_write_ppc_tm_cvsx (bfd *abfd, | |
4b24dd1a AM |
11503 | char *buf, |
11504 | int *bufsiz, | |
11505 | const void *ppc_tm_cvsx, | |
11506 | int size) | |
cb2366c1 EBM |
11507 | { |
11508 | char *note_name = "LINUX"; | |
11509 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11510 | note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size); |
cb2366c1 EBM |
11511 | } |
11512 | ||
11513 | char * | |
11514 | elfcore_write_ppc_tm_spr (bfd *abfd, | |
4b24dd1a AM |
11515 | char *buf, |
11516 | int *bufsiz, | |
11517 | const void *ppc_tm_spr, | |
11518 | int size) | |
cb2366c1 EBM |
11519 | { |
11520 | char *note_name = "LINUX"; | |
11521 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11522 | note_name, NT_PPC_TM_SPR, ppc_tm_spr, size); |
cb2366c1 EBM |
11523 | } |
11524 | ||
11525 | char * | |
11526 | elfcore_write_ppc_tm_ctar (bfd *abfd, | |
4b24dd1a AM |
11527 | char *buf, |
11528 | int *bufsiz, | |
11529 | const void *ppc_tm_ctar, | |
11530 | int size) | |
cb2366c1 EBM |
11531 | { |
11532 | char *note_name = "LINUX"; | |
11533 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11534 | note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size); |
cb2366c1 EBM |
11535 | } |
11536 | ||
11537 | char * | |
11538 | elfcore_write_ppc_tm_cppr (bfd *abfd, | |
4b24dd1a AM |
11539 | char *buf, |
11540 | int *bufsiz, | |
11541 | const void *ppc_tm_cppr, | |
11542 | int size) | |
cb2366c1 EBM |
11543 | { |
11544 | char *note_name = "LINUX"; | |
11545 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11546 | note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size); |
cb2366c1 EBM |
11547 | } |
11548 | ||
11549 | char * | |
11550 | elfcore_write_ppc_tm_cdscr (bfd *abfd, | |
4b24dd1a AM |
11551 | char *buf, |
11552 | int *bufsiz, | |
11553 | const void *ppc_tm_cdscr, | |
11554 | int size) | |
cb2366c1 EBM |
11555 | { |
11556 | char *note_name = "LINUX"; | |
11557 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11558 | note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size); |
cb2366c1 EBM |
11559 | } |
11560 | ||
0675e188 UW |
11561 | static char * |
11562 | elfcore_write_s390_high_gprs (bfd *abfd, | |
11563 | char *buf, | |
11564 | int *bufsiz, | |
11565 | const void *s390_high_gprs, | |
11566 | int size) | |
11567 | { | |
11568 | char *note_name = "LINUX"; | |
11569 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11570 | note_name, NT_S390_HIGH_GPRS, |
0675e188 UW |
11571 | s390_high_gprs, size); |
11572 | } | |
11573 | ||
d7eeb400 MS |
11574 | char * |
11575 | elfcore_write_s390_timer (bfd *abfd, | |
07d6d2b8 AM |
11576 | char *buf, |
11577 | int *bufsiz, | |
11578 | const void *s390_timer, | |
11579 | int size) | |
d7eeb400 MS |
11580 | { |
11581 | char *note_name = "LINUX"; | |
11582 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11583 | note_name, NT_S390_TIMER, s390_timer, size); |
d7eeb400 MS |
11584 | } |
11585 | ||
11586 | char * | |
11587 | elfcore_write_s390_todcmp (bfd *abfd, | |
07d6d2b8 AM |
11588 | char *buf, |
11589 | int *bufsiz, | |
11590 | const void *s390_todcmp, | |
11591 | int size) | |
d7eeb400 MS |
11592 | { |
11593 | char *note_name = "LINUX"; | |
11594 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11595 | note_name, NT_S390_TODCMP, s390_todcmp, size); |
d7eeb400 MS |
11596 | } |
11597 | ||
11598 | char * | |
11599 | elfcore_write_s390_todpreg (bfd *abfd, | |
07d6d2b8 AM |
11600 | char *buf, |
11601 | int *bufsiz, | |
11602 | const void *s390_todpreg, | |
11603 | int size) | |
d7eeb400 MS |
11604 | { |
11605 | char *note_name = "LINUX"; | |
11606 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11607 | note_name, NT_S390_TODPREG, s390_todpreg, size); |
d7eeb400 MS |
11608 | } |
11609 | ||
11610 | char * | |
11611 | elfcore_write_s390_ctrs (bfd *abfd, | |
07d6d2b8 AM |
11612 | char *buf, |
11613 | int *bufsiz, | |
11614 | const void *s390_ctrs, | |
11615 | int size) | |
d7eeb400 MS |
11616 | { |
11617 | char *note_name = "LINUX"; | |
11618 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11619 | note_name, NT_S390_CTRS, s390_ctrs, size); |
d7eeb400 MS |
11620 | } |
11621 | ||
11622 | char * | |
11623 | elfcore_write_s390_prefix (bfd *abfd, | |
07d6d2b8 AM |
11624 | char *buf, |
11625 | int *bufsiz, | |
11626 | const void *s390_prefix, | |
11627 | int size) | |
d7eeb400 MS |
11628 | { |
11629 | char *note_name = "LINUX"; | |
11630 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11631 | note_name, NT_S390_PREFIX, s390_prefix, size); |
d7eeb400 MS |
11632 | } |
11633 | ||
355b81d9 UW |
11634 | char * |
11635 | elfcore_write_s390_last_break (bfd *abfd, | |
11636 | char *buf, | |
11637 | int *bufsiz, | |
11638 | const void *s390_last_break, | |
11639 | int size) | |
11640 | { | |
11641 | char *note_name = "LINUX"; | |
11642 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11643 | note_name, NT_S390_LAST_BREAK, |
355b81d9 UW |
11644 | s390_last_break, size); |
11645 | } | |
11646 | ||
11647 | char * | |
11648 | elfcore_write_s390_system_call (bfd *abfd, | |
11649 | char *buf, | |
11650 | int *bufsiz, | |
11651 | const void *s390_system_call, | |
11652 | int size) | |
11653 | { | |
11654 | char *note_name = "LINUX"; | |
11655 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11656 | note_name, NT_S390_SYSTEM_CALL, |
355b81d9 UW |
11657 | s390_system_call, size); |
11658 | } | |
11659 | ||
abb3f6cc NC |
11660 | char * |
11661 | elfcore_write_s390_tdb (bfd *abfd, | |
11662 | char *buf, | |
11663 | int *bufsiz, | |
11664 | const void *s390_tdb, | |
11665 | int size) | |
11666 | { | |
11667 | char *note_name = "LINUX"; | |
11668 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11669 | note_name, NT_S390_TDB, s390_tdb, size); |
abb3f6cc NC |
11670 | } |
11671 | ||
4ef9f41a AA |
11672 | char * |
11673 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
11674 | char *buf, | |
11675 | int *bufsiz, | |
11676 | const void *s390_vxrs_low, | |
11677 | int size) | |
11678 | { | |
11679 | char *note_name = "LINUX"; | |
11680 | return elfcore_write_note (abfd, buf, bufsiz, | |
11681 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
11682 | } | |
11683 | ||
11684 | char * | |
11685 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
11686 | char *buf, | |
11687 | int *bufsiz, | |
11688 | const void *s390_vxrs_high, | |
11689 | int size) | |
11690 | { | |
11691 | char *note_name = "LINUX"; | |
11692 | return elfcore_write_note (abfd, buf, bufsiz, | |
11693 | note_name, NT_S390_VXRS_HIGH, | |
11694 | s390_vxrs_high, size); | |
11695 | } | |
11696 | ||
88ab90e8 AA |
11697 | char * |
11698 | elfcore_write_s390_gs_cb (bfd *abfd, | |
11699 | char *buf, | |
11700 | int *bufsiz, | |
11701 | const void *s390_gs_cb, | |
11702 | int size) | |
11703 | { | |
11704 | char *note_name = "LINUX"; | |
11705 | return elfcore_write_note (abfd, buf, bufsiz, | |
11706 | note_name, NT_S390_GS_CB, | |
11707 | s390_gs_cb, size); | |
11708 | } | |
11709 | ||
11710 | char * | |
11711 | elfcore_write_s390_gs_bc (bfd *abfd, | |
11712 | char *buf, | |
11713 | int *bufsiz, | |
11714 | const void *s390_gs_bc, | |
11715 | int size) | |
11716 | { | |
11717 | char *note_name = "LINUX"; | |
11718 | return elfcore_write_note (abfd, buf, bufsiz, | |
11719 | note_name, NT_S390_GS_BC, | |
11720 | s390_gs_bc, size); | |
11721 | } | |
11722 | ||
faa9a424 UW |
11723 | char * |
11724 | elfcore_write_arm_vfp (bfd *abfd, | |
11725 | char *buf, | |
11726 | int *bufsiz, | |
11727 | const void *arm_vfp, | |
11728 | int size) | |
11729 | { | |
11730 | char *note_name = "LINUX"; | |
11731 | return elfcore_write_note (abfd, buf, bufsiz, | |
11732 | note_name, NT_ARM_VFP, arm_vfp, size); | |
11733 | } | |
11734 | ||
652451f8 YZ |
11735 | char * |
11736 | elfcore_write_aarch_tls (bfd *abfd, | |
11737 | char *buf, | |
11738 | int *bufsiz, | |
11739 | const void *aarch_tls, | |
11740 | int size) | |
11741 | { | |
11742 | char *note_name = "LINUX"; | |
11743 | return elfcore_write_note (abfd, buf, bufsiz, | |
11744 | note_name, NT_ARM_TLS, aarch_tls, size); | |
11745 | } | |
11746 | ||
11747 | char * | |
11748 | elfcore_write_aarch_hw_break (bfd *abfd, | |
11749 | char *buf, | |
11750 | int *bufsiz, | |
11751 | const void *aarch_hw_break, | |
11752 | int size) | |
11753 | { | |
11754 | char *note_name = "LINUX"; | |
11755 | return elfcore_write_note (abfd, buf, bufsiz, | |
11756 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
11757 | } | |
11758 | ||
11759 | char * | |
11760 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
11761 | char *buf, | |
11762 | int *bufsiz, | |
11763 | const void *aarch_hw_watch, | |
11764 | int size) | |
11765 | { | |
11766 | char *note_name = "LINUX"; | |
11767 | return elfcore_write_note (abfd, buf, bufsiz, | |
11768 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
11769 | } | |
11770 | ||
ad1cc4e4 AH |
11771 | char * |
11772 | elfcore_write_aarch_sve (bfd *abfd, | |
11773 | char *buf, | |
11774 | int *bufsiz, | |
11775 | const void *aarch_sve, | |
11776 | int size) | |
11777 | { | |
11778 | char *note_name = "LINUX"; | |
11779 | return elfcore_write_note (abfd, buf, bufsiz, | |
11780 | note_name, NT_ARM_SVE, aarch_sve, size); | |
11781 | } | |
11782 | ||
e6c3b5bf AH |
11783 | char * |
11784 | elfcore_write_aarch_pauth (bfd *abfd, | |
11785 | char *buf, | |
11786 | int *bufsiz, | |
11787 | const void *aarch_pauth, | |
11788 | int size) | |
11789 | { | |
11790 | char *note_name = "LINUX"; | |
11791 | return elfcore_write_note (abfd, buf, bufsiz, | |
11792 | note_name, NT_ARM_PAC_MASK, aarch_pauth, size); | |
11793 | } | |
11794 | ||
bb864ac1 CES |
11795 | char * |
11796 | elfcore_write_register_note (bfd *abfd, | |
11797 | char *buf, | |
11798 | int *bufsiz, | |
11799 | const char *section, | |
11800 | const void *data, | |
11801 | int size) | |
11802 | { | |
11803 | if (strcmp (section, ".reg2") == 0) | |
11804 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
11805 | if (strcmp (section, ".reg-xfp") == 0) | |
11806 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
11807 | if (strcmp (section, ".reg-xstate") == 0) |
11808 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
11809 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
11810 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
11811 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
11812 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
cb2366c1 EBM |
11813 | if (strcmp (section, ".reg-ppc-tar") == 0) |
11814 | return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size); | |
11815 | if (strcmp (section, ".reg-ppc-ppr") == 0) | |
11816 | return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size); | |
11817 | if (strcmp (section, ".reg-ppc-dscr") == 0) | |
11818 | return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size); | |
11819 | if (strcmp (section, ".reg-ppc-ebb") == 0) | |
11820 | return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size); | |
11821 | if (strcmp (section, ".reg-ppc-pmu") == 0) | |
11822 | return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size); | |
11823 | if (strcmp (section, ".reg-ppc-tm-cgpr") == 0) | |
11824 | return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size); | |
11825 | if (strcmp (section, ".reg-ppc-tm-cfpr") == 0) | |
11826 | return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size); | |
11827 | if (strcmp (section, ".reg-ppc-tm-cvmx") == 0) | |
11828 | return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size); | |
11829 | if (strcmp (section, ".reg-ppc-tm-cvsx") == 0) | |
11830 | return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size); | |
11831 | if (strcmp (section, ".reg-ppc-tm-spr") == 0) | |
11832 | return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size); | |
11833 | if (strcmp (section, ".reg-ppc-tm-ctar") == 0) | |
11834 | return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size); | |
11835 | if (strcmp (section, ".reg-ppc-tm-cppr") == 0) | |
11836 | return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size); | |
11837 | if (strcmp (section, ".reg-ppc-tm-cdscr") == 0) | |
11838 | return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
11839 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
11840 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
11841 | if (strcmp (section, ".reg-s390-timer") == 0) |
11842 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
11843 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
11844 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
11845 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
11846 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
11847 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
11848 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
11849 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
11850 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
11851 | if (strcmp (section, ".reg-s390-last-break") == 0) |
11852 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
11853 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
11854 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
11855 | if (strcmp (section, ".reg-s390-tdb") == 0) |
11856 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
11857 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
11858 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
11859 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
11860 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
88ab90e8 AA |
11861 | if (strcmp (section, ".reg-s390-gs-cb") == 0) |
11862 | return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size); | |
11863 | if (strcmp (section, ".reg-s390-gs-bc") == 0) | |
11864 | return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
11865 | if (strcmp (section, ".reg-arm-vfp") == 0) |
11866 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
11867 | if (strcmp (section, ".reg-aarch-tls") == 0) |
11868 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
11869 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
11870 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
11871 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
11872 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
ad1cc4e4 AH |
11873 | if (strcmp (section, ".reg-aarch-sve") == 0) |
11874 | return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size); | |
e6c3b5bf AH |
11875 | if (strcmp (section, ".reg-aarch-pauth") == 0) |
11876 | return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
11877 | return NULL; |
11878 | } | |
11879 | ||
b34976b6 | 11880 | static bfd_boolean |
276da9b3 L |
11881 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset, |
11882 | size_t align) | |
252b5132 | 11883 | { |
c044fabd | 11884 | char *p; |
252b5132 | 11885 | |
276da9b3 L |
11886 | /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1. |
11887 | gABI specifies that PT_NOTE alignment should be aligned to 4 | |
11888 | bytes for 32-bit objects and to 8 bytes for 64-bit objects. If | |
11889 | align is less than 4, we use 4 byte alignment. */ | |
11890 | if (align < 4) | |
11891 | align = 4; | |
ef135d43 NC |
11892 | if (align != 4 && align != 8) |
11893 | return FALSE; | |
276da9b3 | 11894 | |
252b5132 RH |
11895 | p = buf; |
11896 | while (p < buf + size) | |
11897 | { | |
c044fabd | 11898 | Elf_External_Note *xnp = (Elf_External_Note *) p; |
252b5132 RH |
11899 | Elf_Internal_Note in; |
11900 | ||
baea7ef1 AM |
11901 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
11902 | return FALSE; | |
11903 | ||
dc810e39 | 11904 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 11905 | |
dc810e39 | 11906 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 11907 | in.namedata = xnp->name; |
baea7ef1 AM |
11908 | if (in.namesz > buf - in.namedata + size) |
11909 | return FALSE; | |
252b5132 | 11910 | |
dc810e39 | 11911 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
276da9b3 | 11912 | in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align); |
252b5132 | 11913 | in.descpos = offset + (in.descdata - buf); |
baea7ef1 AM |
11914 | if (in.descsz != 0 |
11915 | && (in.descdata >= buf + size | |
11916 | || in.descsz > buf - in.descdata + size)) | |
11917 | return FALSE; | |
252b5132 | 11918 | |
718175fa | 11919 | switch (bfd_get_format (abfd)) |
07d6d2b8 | 11920 | { |
718175fa JK |
11921 | default: |
11922 | return TRUE; | |
11923 | ||
11924 | case bfd_core: | |
f64e188b | 11925 | { |
8acbedd6 | 11926 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 11927 | struct |
718175fa | 11928 | { |
f64e188b | 11929 | const char * string; |
8acbedd6 | 11930 | size_t len; |
f64e188b | 11931 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 11932 | } |
f64e188b | 11933 | grokers[] = |
b15fa79e | 11934 | { |
8acbedd6 | 11935 | GROKER_ELEMENT ("", elfcore_grok_note), |
aa1ed4a9 | 11936 | GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), |
8acbedd6 KS |
11937 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), |
11938 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
11939 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
864619bb KS |
11940 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note), |
11941 | GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note) | |
f64e188b | 11942 | }; |
8acbedd6 | 11943 | #undef GROKER_ELEMENT |
f64e188b NC |
11944 | int i; |
11945 | ||
11946 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
11947 | { |
11948 | if (in.namesz >= grokers[i].len | |
11949 | && strncmp (in.namedata, grokers[i].string, | |
11950 | grokers[i].len) == 0) | |
11951 | { | |
11952 | if (! grokers[i].func (abfd, & in)) | |
11953 | return FALSE; | |
11954 | break; | |
11955 | } | |
11956 | } | |
f64e188b NC |
11957 | break; |
11958 | } | |
718175fa JK |
11959 | |
11960 | case bfd_object: | |
11961 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
11962 | { | |
11963 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
11964 | return FALSE; | |
11965 | } | |
e21e5835 NC |
11966 | else if (in.namesz == sizeof "stapsdt" |
11967 | && strcmp (in.namedata, "stapsdt") == 0) | |
11968 | { | |
11969 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
11970 | return FALSE; | |
11971 | } | |
718175fa | 11972 | break; |
08a40648 | 11973 | } |
252b5132 | 11974 | |
276da9b3 | 11975 | p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align); |
252b5132 RH |
11976 | } |
11977 | ||
718175fa JK |
11978 | return TRUE; |
11979 | } | |
11980 | ||
864619bb | 11981 | bfd_boolean |
276da9b3 L |
11982 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size, |
11983 | size_t align) | |
718175fa JK |
11984 | { |
11985 | char *buf; | |
11986 | ||
957e1fc1 | 11987 | if (size == 0 || (size + 1) == 0) |
718175fa JK |
11988 | return TRUE; |
11989 | ||
11990 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
11991 | return FALSE; | |
11992 | ||
2bb3687b | 11993 | buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size); |
718175fa JK |
11994 | if (buf == NULL) |
11995 | return FALSE; | |
11996 | ||
f64e188b NC |
11997 | /* PR 17512: file: ec08f814 |
11998 | 0-termintate the buffer so that string searches will not overflow. */ | |
11999 | buf[size] = 0; | |
12000 | ||
2bb3687b | 12001 | if (!elf_parse_notes (abfd, buf, size, offset, align)) |
718175fa JK |
12002 | { |
12003 | free (buf); | |
12004 | return FALSE; | |
12005 | } | |
12006 | ||
252b5132 | 12007 | free (buf); |
b34976b6 | 12008 | return TRUE; |
252b5132 | 12009 | } |
98d8431c JB |
12010 | \f |
12011 | /* Providing external access to the ELF program header table. */ | |
12012 | ||
12013 | /* Return an upper bound on the number of bytes required to store a | |
12014 | copy of ABFD's program header table entries. Return -1 if an error | |
12015 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 12016 | |
98d8431c | 12017 | long |
217aa764 | 12018 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
12019 | { |
12020 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
12021 | { | |
12022 | bfd_set_error (bfd_error_wrong_format); | |
12023 | return -1; | |
12024 | } | |
12025 | ||
936e320b | 12026 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
12027 | } |
12028 | ||
98d8431c JB |
12029 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
12030 | will be stored as an array of Elf_Internal_Phdr structures, as | |
12031 | defined in include/elf/internal.h. To find out how large the | |
12032 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
12033 | ||
12034 | Return the number of program header table entries read, or -1 if an | |
12035 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 12036 | |
98d8431c | 12037 | int |
217aa764 | 12038 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
12039 | { |
12040 | int num_phdrs; | |
12041 | ||
12042 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
12043 | { | |
12044 | bfd_set_error (bfd_error_wrong_format); | |
12045 | return -1; | |
12046 | } | |
12047 | ||
12048 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
01bcaf63 TT |
12049 | if (num_phdrs != 0) |
12050 | memcpy (phdrs, elf_tdata (abfd)->phdr, | |
12051 | num_phdrs * sizeof (Elf_Internal_Phdr)); | |
98d8431c JB |
12052 | |
12053 | return num_phdrs; | |
12054 | } | |
ae4221d7 | 12055 | |
db6751f2 | 12056 | enum elf_reloc_type_class |
7e612e98 AM |
12057 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
12058 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
12059 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
12060 | { |
12061 | return reloc_class_normal; | |
12062 | } | |
f8df10f4 | 12063 | |
47d9a591 | 12064 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
12065 | relocation against a local symbol. */ |
12066 | ||
12067 | bfd_vma | |
217aa764 AM |
12068 | _bfd_elf_rela_local_sym (bfd *abfd, |
12069 | Elf_Internal_Sym *sym, | |
8517fae7 | 12070 | asection **psec, |
217aa764 | 12071 | Elf_Internal_Rela *rel) |
f8df10f4 | 12072 | { |
8517fae7 | 12073 | asection *sec = *psec; |
f8df10f4 JJ |
12074 | bfd_vma relocation; |
12075 | ||
6835821b AM |
12076 | relocation = (sec->output_section->vma |
12077 | + sec->output_offset | |
12078 | + sym->st_value); | |
f8df10f4 | 12079 | if ((sec->flags & SEC_MERGE) |
c629eae0 | 12080 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 12081 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 12082 | { |
f8df10f4 | 12083 | rel->r_addend = |
8517fae7 | 12084 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 12085 | elf_section_data (sec)->sec_info, |
753731ee AM |
12086 | sym->st_value + rel->r_addend); |
12087 | if (sec != *psec) | |
12088 | { | |
12089 | /* If we have changed the section, and our original section is | |
12090 | marked with SEC_EXCLUDE, it means that the original | |
12091 | SEC_MERGE section has been completely subsumed in some | |
12092 | other SEC_MERGE section. In this case, we need to leave | |
12093 | some info around for --emit-relocs. */ | |
12094 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
12095 | sec->kept_section = *psec; | |
12096 | sec = *psec; | |
12097 | } | |
8517fae7 AM |
12098 | rel->r_addend -= relocation; |
12099 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
12100 | } |
12101 | return relocation; | |
12102 | } | |
c629eae0 JJ |
12103 | |
12104 | bfd_vma | |
217aa764 AM |
12105 | _bfd_elf_rel_local_sym (bfd *abfd, |
12106 | Elf_Internal_Sym *sym, | |
12107 | asection **psec, | |
12108 | bfd_vma addend) | |
47d9a591 | 12109 | { |
c629eae0 JJ |
12110 | asection *sec = *psec; |
12111 | ||
6835821b | 12112 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
12113 | return sym->st_value + addend; |
12114 | ||
12115 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 12116 | elf_section_data (sec)->sec_info, |
753731ee | 12117 | sym->st_value + addend); |
c629eae0 JJ |
12118 | } |
12119 | ||
37b01f6a DG |
12120 | /* Adjust an address within a section. Given OFFSET within SEC, return |
12121 | the new offset within the section, based upon changes made to the | |
12122 | section. Returns -1 if the offset is now invalid. | |
12123 | The offset (in abnd out) is in target sized bytes, however big a | |
12124 | byte may be. */ | |
12125 | ||
c629eae0 | 12126 | bfd_vma |
217aa764 | 12127 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 12128 | struct bfd_link_info *info, |
217aa764 AM |
12129 | asection *sec, |
12130 | bfd_vma offset) | |
c629eae0 | 12131 | { |
68bfbfcc | 12132 | switch (sec->sec_info_type) |
65765700 | 12133 | { |
dbaa2011 | 12134 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
12135 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
12136 | offset); | |
dbaa2011 | 12137 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 12138 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
37b01f6a | 12139 | |
65765700 | 12140 | default: |
310fd250 L |
12141 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
12142 | { | |
37b01f6a | 12143 | /* Reverse the offset. */ |
310fd250 L |
12144 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
12145 | bfd_size_type address_size = bed->s->arch_size / 8; | |
37b01f6a DG |
12146 | |
12147 | /* address_size and sec->size are in octets. Convert | |
12148 | to bytes before subtracting the original offset. */ | |
61826503 | 12149 | offset = ((sec->size - address_size) |
bb294208 | 12150 | / bfd_octets_per_byte (abfd, sec) - offset); |
310fd250 | 12151 | } |
65765700 JJ |
12152 | return offset; |
12153 | } | |
c629eae0 | 12154 | } |
3333a7c3 RM |
12155 | \f |
12156 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
12157 | reconstruct an ELF file by reading the segments out of remote memory | |
12158 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
12159 | points to. If not null, *LOADBASEP is filled in with the difference | |
12160 | between the VMAs from which the segments were read, and the VMAs the | |
12161 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
12162 | ||
12163 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
12164 | remote memory at target address VMA into the local buffer at MYADDR; it | |
12165 | should return zero on success or an `errno' code on failure. TEMPL must | |
12166 | be a BFD for an ELF target with the word size and byte order found in | |
12167 | the remote memory. */ | |
12168 | ||
12169 | bfd * | |
217aa764 AM |
12170 | bfd_elf_bfd_from_remote_memory |
12171 | (bfd *templ, | |
12172 | bfd_vma ehdr_vma, | |
f0a5d95a | 12173 | bfd_size_type size, |
217aa764 | 12174 | bfd_vma *loadbasep, |
fe78531d | 12175 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
12176 | { |
12177 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 12178 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 12179 | } |
4c45e5c9 JJ |
12180 | \f |
12181 | long | |
c9727e01 AM |
12182 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
12183 | long symcount ATTRIBUTE_UNUSED, | |
12184 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 12185 | long dynsymcount, |
c9727e01 AM |
12186 | asymbol **dynsyms, |
12187 | asymbol **ret) | |
4c45e5c9 JJ |
12188 | { |
12189 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
12190 | asection *relplt; | |
12191 | asymbol *s; | |
12192 | const char *relplt_name; | |
12193 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
12194 | arelent *p; | |
12195 | long count, i, n; | |
12196 | size_t size; | |
12197 | Elf_Internal_Shdr *hdr; | |
12198 | char *names; | |
12199 | asection *plt; | |
12200 | ||
8615f3f2 AM |
12201 | *ret = NULL; |
12202 | ||
90e3cdf2 JJ |
12203 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
12204 | return 0; | |
12205 | ||
8615f3f2 AM |
12206 | if (dynsymcount <= 0) |
12207 | return 0; | |
12208 | ||
4c45e5c9 JJ |
12209 | if (!bed->plt_sym_val) |
12210 | return 0; | |
12211 | ||
12212 | relplt_name = bed->relplt_name; | |
12213 | if (relplt_name == NULL) | |
d35fd659 | 12214 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
12215 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
12216 | if (relplt == NULL) | |
12217 | return 0; | |
12218 | ||
12219 | hdr = &elf_section_data (relplt)->this_hdr; | |
12220 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
12221 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
12222 | return 0; | |
12223 | ||
12224 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
12225 | if (plt == NULL) | |
12226 | return 0; | |
12227 | ||
12228 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 12229 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
12230 | return -1; |
12231 | ||
eea6121a | 12232 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
12233 | size = count * sizeof (asymbol); |
12234 | p = relplt->relocation; | |
cb53bf42 | 12235 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
12236 | { |
12237 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
12238 | if (p->addend != 0) | |
12239 | { | |
12240 | #ifdef BFD64 | |
12241 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
12242 | #else | |
12243 | size += sizeof ("+0x") - 1 + 8; | |
12244 | #endif | |
12245 | } | |
12246 | } | |
4c45e5c9 | 12247 | |
a50b1753 | 12248 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
12249 | if (s == NULL) |
12250 | return -1; | |
12251 | ||
12252 | names = (char *) (s + count); | |
12253 | p = relplt->relocation; | |
12254 | n = 0; | |
cb53bf42 | 12255 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
12256 | { |
12257 | size_t len; | |
12258 | bfd_vma addr; | |
12259 | ||
12260 | addr = bed->plt_sym_val (i, plt, p); | |
12261 | if (addr == (bfd_vma) -1) | |
12262 | continue; | |
12263 | ||
12264 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
12265 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
12266 | we are defining a symbol, ensure one of them is set. */ | |
12267 | if ((s->flags & BSF_LOCAL) == 0) | |
12268 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 12269 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
12270 | s->section = plt; |
12271 | s->value = addr - plt->vma; | |
12272 | s->name = names; | |
8f39ba8e | 12273 | s->udata.p = NULL; |
4c45e5c9 JJ |
12274 | len = strlen ((*p->sym_ptr_ptr)->name); |
12275 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
12276 | names += len; | |
041de40d AM |
12277 | if (p->addend != 0) |
12278 | { | |
1d770845 | 12279 | char buf[30], *a; |
d324f6d6 | 12280 | |
041de40d AM |
12281 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
12282 | names += sizeof ("+0x") - 1; | |
1d770845 L |
12283 | bfd_sprintf_vma (abfd, buf, p->addend); |
12284 | for (a = buf; *a == '0'; ++a) | |
12285 | ; | |
12286 | len = strlen (a); | |
12287 | memcpy (names, a, len); | |
12288 | names += len; | |
041de40d | 12289 | } |
4c45e5c9 JJ |
12290 | memcpy (names, "@plt", sizeof ("@plt")); |
12291 | names += sizeof ("@plt"); | |
8f39ba8e | 12292 | ++s, ++n; |
4c45e5c9 JJ |
12293 | } |
12294 | ||
12295 | return n; | |
12296 | } | |
3d7f7666 | 12297 | |
821e6ff6 AM |
12298 | /* It is only used by x86-64 so far. |
12299 | ??? This repeats *COM* id of zero. sec->id is supposed to be unique, | |
7eacd66b AM |
12300 | but current usage would allow all of _bfd_std_section to be zero. */ |
12301 | static const asymbol lcomm_sym | |
12302 | = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section); | |
3b22753a | 12303 | asection _bfd_elf_large_com_section |
7eacd66b | 12304 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym, |
821e6ff6 | 12305 | "LARGE_COMMON", 0, SEC_IS_COMMON); |
ecca9871 | 12306 | |
cc364be6 AM |
12307 | bfd_boolean |
12308 | _bfd_elf_final_write_processing (bfd *abfd) | |
06f44071 AM |
12309 | { |
12310 | Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */ | |
d1036acb L |
12311 | |
12312 | i_ehdrp = elf_elfheader (abfd); | |
12313 | ||
06f44071 AM |
12314 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) |
12315 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 | 12316 | |
df3a023b AM |
12317 | /* Set the osabi field to ELFOSABI_GNU if the binary contains |
12318 | SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or | |
12319 | STB_GNU_UNIQUE binding. */ | |
cc364be6 AM |
12320 | if (elf_tdata (abfd)->has_gnu_osabi != 0) |
12321 | { | |
12322 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) | |
12323 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; | |
12324 | else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU | |
12325 | && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD) | |
12326 | { | |
12327 | if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) | |
12328 | _bfd_error_handler (_("GNU_MBIND section is unsupported")); | |
12329 | if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc) | |
12330 | _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported")); | |
12331 | if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique) | |
12332 | _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported")); | |
9aea1e31 | 12333 | bfd_set_error (bfd_error_sorry); |
cc364be6 AM |
12334 | return FALSE; |
12335 | } | |
12336 | } | |
12337 | return TRUE; | |
d1036acb | 12338 | } |
fcb93ecf PB |
12339 | |
12340 | ||
12341 | /* Return TRUE for ELF symbol types that represent functions. | |
12342 | This is the default version of this function, which is sufficient for | |
d8045f23 | 12343 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
12344 | |
12345 | bfd_boolean | |
12346 | _bfd_elf_is_function_type (unsigned int type) | |
12347 | { | |
d8045f23 NC |
12348 | return (type == STT_FUNC |
12349 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 12350 | } |
9f296da3 | 12351 | |
aef36ac1 AM |
12352 | /* If the ELF symbol SYM might be a function in SEC, return the |
12353 | function size and set *CODE_OFF to the function's entry point, | |
12354 | otherwise return zero. */ | |
9f296da3 | 12355 | |
aef36ac1 AM |
12356 | bfd_size_type |
12357 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
12358 | bfd_vma *code_off) | |
9f296da3 | 12359 | { |
aef36ac1 AM |
12360 | bfd_size_type size; |
12361 | ||
ff9e0f5b | 12362 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
12363 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
12364 | || sym->section != sec) | |
12365 | return 0; | |
ff9e0f5b | 12366 | |
ff9e0f5b | 12367 | *code_off = sym->value; |
aef36ac1 AM |
12368 | size = 0; |
12369 | if (!(sym->flags & BSF_SYNTHETIC)) | |
12370 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
12371 | if (size == 0) | |
12372 | size = 1; | |
12373 | return size; | |
9f296da3 | 12374 | } |
a8e14f4c NC |
12375 | |
12376 | /* Set to non-zero to enable some debug messages. */ | |
12377 | #define DEBUG_SECONDARY_RELOCS 0 | |
12378 | ||
12379 | /* An internal-to-the-bfd-library only section type | |
12380 | used to indicate a cached secondary reloc section. */ | |
12381 | #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA) | |
12382 | ||
12383 | /* Create a BFD section to hold a secondary reloc section. */ | |
12384 | ||
12385 | bfd_boolean | |
12386 | _bfd_elf_init_secondary_reloc_section (bfd * abfd, | |
12387 | Elf_Internal_Shdr *hdr, | |
12388 | const char * name, | |
12389 | unsigned int shindex) | |
12390 | { | |
12391 | /* We only support RELA secondary relocs. */ | |
12392 | if (hdr->sh_type != SHT_RELA) | |
12393 | return FALSE; | |
12394 | ||
12395 | #if DEBUG_SECONDARY_RELOCS | |
12396 | fprintf (stderr, "secondary reloc section %s encountered\n", name); | |
12397 | #endif | |
12398 | hdr->sh_type = SHT_SECONDARY_RELOC; | |
12399 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
12400 | } | |
12401 | ||
12402 | /* Read in any secondary relocs associated with SEC. */ | |
12403 | ||
12404 | bfd_boolean | |
12405 | _bfd_elf_slurp_secondary_reloc_section (bfd * abfd, | |
12406 | asection * sec, | |
12407 | asymbol ** symbols) | |
12408 | { | |
12409 | const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); | |
12410 | asection * relsec; | |
12411 | bfd_boolean result = TRUE; | |
12412 | bfd_vma (*r_sym) (bfd_vma); | |
12413 | ||
12414 | #if BFD_DEFAULT_TARGET_SIZE > 32 | |
12415 | if (bfd_arch_bits_per_address (abfd) != 32) | |
12416 | r_sym = elf64_r_sym; | |
12417 | else | |
12418 | #endif | |
12419 | r_sym = elf32_r_sym; | |
12420 | ||
12421 | /* Discover if there are any secondary reloc sections | |
12422 | associated with SEC. */ | |
12423 | for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) | |
12424 | { | |
12425 | Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr; | |
12426 | ||
12427 | if (hdr->sh_type == SHT_SECONDARY_RELOC | |
12428 | && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx) | |
12429 | { | |
12430 | bfd_byte * native_relocs; | |
12431 | bfd_byte * native_reloc; | |
12432 | arelent * internal_relocs; | |
12433 | arelent * internal_reloc; | |
12434 | unsigned int i; | |
12435 | unsigned int entsize; | |
12436 | unsigned int symcount; | |
12437 | unsigned int reloc_count; | |
12438 | size_t amt; | |
12439 | ||
12440 | if (ebd->elf_info_to_howto == NULL) | |
12441 | return FALSE; | |
12442 | ||
12443 | #if DEBUG_SECONDARY_RELOCS | |
12444 | fprintf (stderr, "read secondary relocs for %s from %s\n", | |
12445 | sec->name, relsec->name); | |
12446 | #endif | |
12447 | entsize = hdr->sh_entsize; | |
12448 | ||
12449 | native_relocs = bfd_malloc (hdr->sh_size); | |
12450 | if (native_relocs == NULL) | |
12451 | { | |
12452 | result = FALSE; | |
12453 | continue; | |
12454 | } | |
12455 | ||
12456 | reloc_count = NUM_SHDR_ENTRIES (hdr); | |
12457 | if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt)) | |
12458 | { | |
ecbbbdba | 12459 | free (native_relocs); |
a8e14f4c NC |
12460 | bfd_set_error (bfd_error_file_too_big); |
12461 | result = FALSE; | |
12462 | continue; | |
12463 | } | |
12464 | ||
12465 | internal_relocs = (arelent *) bfd_alloc (abfd, amt); | |
12466 | if (internal_relocs == NULL) | |
12467 | { | |
12468 | free (native_relocs); | |
12469 | result = FALSE; | |
12470 | continue; | |
12471 | } | |
12472 | ||
12473 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
12474 | || (bfd_bread (native_relocs, hdr->sh_size, abfd) | |
12475 | != hdr->sh_size)) | |
12476 | { | |
12477 | free (native_relocs); | |
ecbbbdba NC |
12478 | /* The internal_relocs will be freed when |
12479 | the memory for the bfd is released. */ | |
a8e14f4c NC |
12480 | result = FALSE; |
12481 | continue; | |
12482 | } | |
12483 | ||
12484 | symcount = bfd_get_symcount (abfd); | |
12485 | ||
12486 | for (i = 0, internal_reloc = internal_relocs, | |
12487 | native_reloc = native_relocs; | |
12488 | i < reloc_count; | |
12489 | i++, internal_reloc++, native_reloc += entsize) | |
12490 | { | |
12491 | bfd_boolean res; | |
12492 | Elf_Internal_Rela rela; | |
12493 | ||
12494 | ebd->s->swap_reloca_in (abfd, native_reloc, & rela); | |
12495 | ||
12496 | /* The address of an ELF reloc is section relative for an object | |
12497 | file, and absolute for an executable file or shared library. | |
12498 | The address of a normal BFD reloc is always section relative, | |
12499 | and the address of a dynamic reloc is absolute.. */ | |
12500 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) | |
12501 | internal_reloc->address = rela.r_offset; | |
12502 | else | |
12503 | internal_reloc->address = rela.r_offset - sec->vma; | |
12504 | ||
12505 | if (r_sym (rela.r_info) == STN_UNDEF) | |
12506 | { | |
12507 | /* FIXME: This and the error case below mean that we | |
12508 | have a symbol on relocs that is not elf_symbol_type. */ | |
12509 | internal_reloc->sym_ptr_ptr = | |
12510 | bfd_abs_section_ptr->symbol_ptr_ptr; | |
12511 | } | |
12512 | else if (r_sym (rela.r_info) > symcount) | |
12513 | { | |
12514 | _bfd_error_handler | |
12515 | /* xgettext:c-format */ | |
12516 | (_("%pB(%pA): relocation %d has invalid symbol index %ld"), | |
12517 | abfd, sec, i, (long) r_sym (rela.r_info)); | |
12518 | bfd_set_error (bfd_error_bad_value); | |
12519 | internal_reloc->sym_ptr_ptr = | |
12520 | bfd_abs_section_ptr->symbol_ptr_ptr; | |
12521 | result = FALSE; | |
12522 | } | |
12523 | else | |
12524 | { | |
12525 | asymbol **ps; | |
12526 | ||
12527 | ps = symbols + r_sym (rela.r_info) - 1; | |
12528 | ||
12529 | internal_reloc->sym_ptr_ptr = ps; | |
12530 | /* Make sure that this symbol is not removed by strip. */ | |
12531 | (*ps)->flags |= BSF_KEEP; | |
12532 | } | |
12533 | ||
12534 | internal_reloc->addend = rela.r_addend; | |
12535 | ||
12536 | res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela); | |
12537 | if (! res || internal_reloc->howto == NULL) | |
12538 | { | |
12539 | #if DEBUG_SECONDARY_RELOCS | |
12540 | fprintf (stderr, "there is no howto associated with reloc %lx\n", | |
12541 | rela.r_info); | |
12542 | #endif | |
12543 | result = FALSE; | |
12544 | } | |
12545 | } | |
12546 | ||
12547 | free (native_relocs); | |
12548 | /* Store the internal relocs. */ | |
12549 | elf_section_data (relsec)->sec_info = internal_relocs; | |
12550 | } | |
12551 | } | |
12552 | ||
12553 | return result; | |
12554 | } | |
12555 | ||
12556 | /* Set the ELF section header fields of an output secondary reloc section. */ | |
12557 | ||
12558 | bfd_boolean | |
12559 | _bfd_elf_copy_special_section_fields (const bfd * ibfd ATTRIBUTE_UNUSED, | |
12560 | bfd * obfd ATTRIBUTE_UNUSED, | |
12561 | const Elf_Internal_Shdr * isection, | |
12562 | Elf_Internal_Shdr * osection) | |
12563 | { | |
12564 | asection * isec; | |
12565 | asection * osec; | |
12566 | ||
12567 | if (isection == NULL) | |
12568 | return FALSE; | |
12569 | ||
12570 | if (isection->sh_type != SHT_SECONDARY_RELOC) | |
12571 | return TRUE; | |
12572 | ||
12573 | isec = isection->bfd_section; | |
12574 | if (isec == NULL) | |
12575 | return FALSE; | |
12576 | ||
12577 | osec = osection->bfd_section; | |
12578 | if (osec == NULL) | |
12579 | return FALSE; | |
12580 | ||
12581 | BFD_ASSERT (elf_section_data (osec)->sec_info == NULL); | |
12582 | elf_section_data (osec)->sec_info = elf_section_data (isec)->sec_info; | |
12583 | osection->sh_type = SHT_RELA; | |
12584 | osection->sh_link = elf_onesymtab (obfd); | |
12585 | if (osection->sh_link == 0) | |
12586 | { | |
12587 | /* There is no symbol table - we are hosed... */ | |
12588 | _bfd_error_handler | |
12589 | /* xgettext:c-format */ | |
12590 | (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"), | |
12591 | obfd, osec); | |
12592 | bfd_set_error (bfd_error_bad_value); | |
12593 | return FALSE; | |
12594 | } | |
12595 | ||
12596 | /* Find the output section that corresponds to the isection's sh_info link. */ | |
327ef784 NC |
12597 | if (isection->sh_info == 0 |
12598 | || isection->sh_info >= elf_numsections (ibfd)) | |
12599 | { | |
12600 | _bfd_error_handler | |
12601 | /* xgettext:c-format */ | |
12602 | (_("%pB(%pA): info section index is invalid"), | |
12603 | obfd, osec); | |
12604 | bfd_set_error (bfd_error_bad_value); | |
12605 | return FALSE; | |
12606 | } | |
12607 | ||
a8e14f4c NC |
12608 | isection = elf_elfsections (ibfd)[isection->sh_info]; |
12609 | ||
327ef784 NC |
12610 | if (isection == NULL |
12611 | || isection->bfd_section == NULL | |
12612 | || isection->bfd_section->output_section == NULL) | |
12613 | { | |
12614 | _bfd_error_handler | |
12615 | /* xgettext:c-format */ | |
12616 | (_("%pB(%pA): info section index cannot be set because the section is not in the output"), | |
12617 | obfd, osec); | |
12618 | bfd_set_error (bfd_error_bad_value); | |
12619 | return FALSE; | |
12620 | } | |
12621 | ||
a8e14f4c NC |
12622 | osection->sh_info = |
12623 | elf_section_data (isection->bfd_section->output_section)->this_idx; | |
12624 | ||
12625 | #if DEBUG_SECONDARY_RELOCS | |
12626 | fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n", | |
12627 | osec->name, osection->sh_link, osection->sh_info); | |
12628 | #endif | |
12629 | ||
12630 | return TRUE; | |
12631 | } | |
12632 | ||
12633 | /* Write out a secondary reloc section. */ | |
12634 | ||
12635 | bfd_boolean | |
12636 | _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec) | |
12637 | { | |
12638 | const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); | |
12639 | bfd_vma addr_offset; | |
12640 | asection * relsec; | |
12641 | bfd_vma (*r_info) (bfd_vma, bfd_vma); | |
ac4bf06c NC |
12642 | bfd_boolean result = TRUE; |
12643 | ||
12644 | if (sec == NULL) | |
12645 | return FALSE; | |
a8e14f4c NC |
12646 | |
12647 | #if BFD_DEFAULT_TARGET_SIZE > 32 | |
12648 | if (bfd_arch_bits_per_address (abfd) != 32) | |
12649 | r_info = elf64_r_info; | |
12650 | else | |
12651 | #endif | |
12652 | r_info = elf32_r_info; | |
12653 | ||
a8e14f4c NC |
12654 | /* The address of an ELF reloc is section relative for an object |
12655 | file, and absolute for an executable file or shared library. | |
12656 | The address of a BFD reloc is always section relative. */ | |
12657 | addr_offset = 0; | |
12658 | if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) | |
12659 | addr_offset = sec->vma; | |
12660 | ||
12661 | /* Discover if there are any secondary reloc sections | |
12662 | associated with SEC. */ | |
12663 | for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) | |
12664 | { | |
12665 | const struct bfd_elf_section_data * const esd = elf_section_data (relsec); | |
12666 | Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr; | |
12667 | ||
12668 | if (hdr->sh_type == SHT_RELA | |
12669 | && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx) | |
12670 | { | |
12671 | asymbol * last_sym; | |
12672 | int last_sym_idx; | |
12673 | unsigned int reloc_count; | |
12674 | unsigned int idx; | |
12675 | arelent * src_irel; | |
12676 | bfd_byte * dst_rela; | |
12677 | ||
ac4bf06c NC |
12678 | if (hdr->contents != NULL) |
12679 | { | |
12680 | _bfd_error_handler | |
12681 | /* xgettext:c-format */ | |
12682 | (_("%pB(%pA): error: secondary reloc section processed twice"), | |
12683 | abfd, relsec); | |
12684 | bfd_set_error (bfd_error_bad_value); | |
12685 | result = FALSE; | |
12686 | continue; | |
12687 | } | |
a8e14f4c NC |
12688 | |
12689 | reloc_count = hdr->sh_size / hdr->sh_entsize; | |
ac4bf06c NC |
12690 | if (reloc_count <= 0) |
12691 | { | |
12692 | _bfd_error_handler | |
12693 | /* xgettext:c-format */ | |
12694 | (_("%pB(%pA): error: secondary reloc section is empty!"), | |
12695 | abfd, relsec); | |
12696 | bfd_set_error (bfd_error_bad_value); | |
12697 | result = FALSE; | |
12698 | continue; | |
12699 | } | |
a8e14f4c NC |
12700 | |
12701 | hdr->contents = bfd_alloc (abfd, hdr->sh_size); | |
12702 | if (hdr->contents == NULL) | |
12703 | continue; | |
12704 | ||
12705 | #if DEBUG_SECONDARY_RELOCS | |
12706 | fprintf (stderr, "write %u secondary relocs for %s from %s\n", | |
12707 | reloc_count, sec->name, relsec->name); | |
12708 | #endif | |
12709 | last_sym = NULL; | |
12710 | last_sym_idx = 0; | |
12711 | dst_rela = hdr->contents; | |
12712 | src_irel = (arelent *) esd->sec_info; | |
ac4bf06c NC |
12713 | if (src_irel == NULL) |
12714 | { | |
12715 | _bfd_error_handler | |
12716 | /* xgettext:c-format */ | |
12717 | (_("%pB(%pA): error: internal relocs missing for secondary reloc section"), | |
12718 | abfd, relsec); | |
12719 | bfd_set_error (bfd_error_bad_value); | |
12720 | result = FALSE; | |
12721 | continue; | |
12722 | } | |
a8e14f4c NC |
12723 | |
12724 | for (idx = 0; idx < reloc_count; idx++, dst_rela += hdr->sh_entsize) | |
12725 | { | |
12726 | Elf_Internal_Rela src_rela; | |
12727 | arelent *ptr; | |
12728 | asymbol *sym; | |
12729 | int n; | |
12730 | ||
12731 | ptr = src_irel + idx; | |
ac4bf06c NC |
12732 | if (ptr == NULL) |
12733 | { | |
12734 | _bfd_error_handler | |
12735 | /* xgettext:c-format */ | |
12736 | (_("%pB(%pA): error: reloc table entry %u is empty"), | |
12737 | abfd, relsec, idx); | |
12738 | bfd_set_error (bfd_error_bad_value); | |
12739 | result = FALSE; | |
12740 | break; | |
12741 | } | |
a8e14f4c | 12742 | |
ac4bf06c NC |
12743 | if (ptr->sym_ptr_ptr == NULL) |
12744 | { | |
12745 | /* FIXME: Is this an error ? */ | |
12746 | n = 0; | |
12747 | } | |
a8e14f4c NC |
12748 | else |
12749 | { | |
ac4bf06c NC |
12750 | sym = *ptr->sym_ptr_ptr; |
12751 | ||
12752 | if (sym == last_sym) | |
12753 | n = last_sym_idx; | |
12754 | else | |
a8e14f4c | 12755 | { |
ac4bf06c NC |
12756 | n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym); |
12757 | if (n < 0) | |
12758 | { | |
12759 | _bfd_error_handler | |
12760 | /* xgettext:c-format */ | |
12761 | (_("%pB(%pA): error: secondary reloc %u references a missing symbol"), | |
12762 | abfd, relsec, idx); | |
12763 | bfd_set_error (bfd_error_bad_value); | |
12764 | result = FALSE; | |
12765 | n = 0; | |
12766 | } | |
12767 | ||
12768 | last_sym = sym; | |
12769 | last_sym_idx = n; | |
a8e14f4c | 12770 | } |
a8e14f4c | 12771 | |
ac4bf06c NC |
12772 | if (sym->the_bfd != NULL |
12773 | && sym->the_bfd->xvec != abfd->xvec | |
12774 | && ! _bfd_elf_validate_reloc (abfd, ptr)) | |
12775 | { | |
12776 | _bfd_error_handler | |
12777 | /* xgettext:c-format */ | |
12778 | (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"), | |
12779 | abfd, relsec, idx); | |
12780 | bfd_set_error (bfd_error_bad_value); | |
12781 | result = FALSE; | |
12782 | n = 0; | |
12783 | } | |
a8e14f4c NC |
12784 | } |
12785 | ||
ac4bf06c | 12786 | src_rela.r_offset = ptr->address + addr_offset; |
a8e14f4c NC |
12787 | if (ptr->howto == NULL) |
12788 | { | |
ac4bf06c NC |
12789 | _bfd_error_handler |
12790 | /* xgettext:c-format */ | |
12791 | (_("%pB(%pA): error: secondary reloc %u is of an unknown type"), | |
12792 | abfd, relsec, idx); | |
12793 | bfd_set_error (bfd_error_bad_value); | |
12794 | result = FALSE; | |
12795 | src_rela.r_info = r_info (0, 0); | |
a8e14f4c | 12796 | } |
ac4bf06c NC |
12797 | else |
12798 | src_rela.r_info = r_info (n, ptr->howto->type); | |
a8e14f4c NC |
12799 | src_rela.r_addend = ptr->addend; |
12800 | ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela); | |
12801 | } | |
12802 | } | |
12803 | } | |
12804 | ||
ac4bf06c | 12805 | return result; |
a8e14f4c | 12806 | } |