]>
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); |
dd863624 L |
860 | if (bed->link_order_error_handler) |
861 | bed->link_order_error_handler | |
695344c0 | 862 | /* xgettext:c-format */ |
871b3ab2 | 863 | (_("%pB: warning: sh_link not set for section `%pA'"), |
dd863624 L |
864 | abfd, s); |
865 | } | |
866 | else | |
867 | { | |
91d6fa6a | 868 | asection *linksec = NULL; |
25bbc984 | 869 | |
4fbb74a6 AM |
870 | if (elfsec < elf_numsections (abfd)) |
871 | { | |
872 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 873 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 874 | } |
25bbc984 L |
875 | |
876 | /* PR 1991, 2008: | |
877 | Some strip/objcopy may leave an incorrect value in | |
878 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 879 | if (linksec == NULL) |
25bbc984 | 880 | { |
4eca0228 | 881 | _bfd_error_handler |
695344c0 | 882 | /* xgettext:c-format */ |
871b3ab2 | 883 | (_("%pB: sh_link [%d] in section `%pA' is incorrect"), |
c08bb8dd | 884 | s->owner, elfsec, s); |
25bbc984 L |
885 | result = FALSE; |
886 | } | |
887 | ||
91d6fa6a | 888 | elf_linked_to_section (s) = linksec; |
dd863624 L |
889 | } |
890 | } | |
53720c49 AM |
891 | else if (this_hdr->sh_type == SHT_GROUP |
892 | && elf_next_in_group (s) == NULL) | |
893 | { | |
4eca0228 | 894 | _bfd_error_handler |
695344c0 | 895 | /* xgettext:c-format */ |
871b3ab2 | 896 | (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"), |
53720c49 AM |
897 | abfd, elf_section_data (s)->this_idx); |
898 | result = FALSE; | |
899 | } | |
dd863624 | 900 | } |
3d7f7666 | 901 | |
dd863624 | 902 | /* Process section groups. */ |
3d7f7666 L |
903 | if (num_group == (unsigned) -1) |
904 | return result; | |
905 | ||
906 | for (i = 0; i < num_group; i++) | |
907 | { | |
908 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
4b0e8a5f NC |
909 | Elf_Internal_Group *idx; |
910 | unsigned int n_elt; | |
3d7f7666 | 911 | |
4b0e8a5f NC |
912 | /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */ |
913 | if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) | |
914 | { | |
4eca0228 | 915 | _bfd_error_handler |
695344c0 | 916 | /* xgettext:c-format */ |
871b3ab2 | 917 | (_("%pB: section group entry number %u is corrupt"), |
4b0e8a5f NC |
918 | abfd, i); |
919 | result = FALSE; | |
920 | continue; | |
921 | } | |
922 | ||
923 | idx = (Elf_Internal_Group *) shdr->contents; | |
924 | n_elt = shdr->sh_size / 4; | |
1b786873 | 925 | |
3d7f7666 | 926 | while (--n_elt != 0) |
24d3e51b NC |
927 | { |
928 | ++ idx; | |
929 | ||
930 | if (idx->shdr == NULL) | |
931 | continue; | |
932 | else if (idx->shdr->bfd_section) | |
933 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
db4677b8 AM |
934 | else if (idx->shdr->sh_type != SHT_RELA |
935 | && idx->shdr->sh_type != SHT_REL) | |
24d3e51b NC |
936 | { |
937 | /* There are some unknown sections in the group. */ | |
938 | _bfd_error_handler | |
939 | /* xgettext:c-format */ | |
871b3ab2 | 940 | (_("%pB: unknown type [%#x] section `%s' in group [%pA]"), |
24d3e51b NC |
941 | abfd, |
942 | idx->shdr->sh_type, | |
943 | bfd_elf_string_from_elf_section (abfd, | |
944 | (elf_elfheader (abfd) | |
945 | ->e_shstrndx), | |
946 | idx->shdr->sh_name), | |
947 | shdr->bfd_section); | |
948 | result = FALSE; | |
949 | } | |
950 | } | |
3d7f7666 | 951 | } |
24d3e51b | 952 | |
3d7f7666 L |
953 | return result; |
954 | } | |
955 | ||
72adc230 AM |
956 | bfd_boolean |
957 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
958 | { | |
959 | return elf_next_in_group (sec) != NULL; | |
960 | } | |
961 | ||
cb7f4b29 AM |
962 | const char * |
963 | bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
964 | { | |
965 | if (elf_sec_group (sec) != NULL) | |
966 | return elf_group_name (sec); | |
967 | return NULL; | |
968 | } | |
969 | ||
f6fe1ccd L |
970 | static char * |
971 | convert_debug_to_zdebug (bfd *abfd, const char *name) | |
972 | { | |
973 | unsigned int len = strlen (name); | |
974 | char *new_name = bfd_alloc (abfd, len + 2); | |
975 | if (new_name == NULL) | |
976 | return NULL; | |
977 | new_name[0] = '.'; | |
978 | new_name[1] = 'z'; | |
979 | memcpy (new_name + 2, name + 1, len); | |
980 | return new_name; | |
981 | } | |
982 | ||
983 | static char * | |
984 | convert_zdebug_to_debug (bfd *abfd, const char *name) | |
985 | { | |
986 | unsigned int len = strlen (name); | |
987 | char *new_name = bfd_alloc (abfd, len); | |
988 | if (new_name == NULL) | |
989 | return NULL; | |
990 | new_name[0] = '.'; | |
991 | memcpy (new_name + 1, name + 2, len - 1); | |
992 | return new_name; | |
993 | } | |
994 | ||
cc5277b1 ML |
995 | /* This a copy of lto_section defined in GCC (lto-streamer.h). */ |
996 | ||
997 | struct lto_section | |
998 | { | |
999 | int16_t major_version; | |
1000 | int16_t minor_version; | |
1001 | unsigned char slim_object; | |
1002 | ||
1003 | /* Flags is a private field that is not defined publicly. */ | |
1004 | uint16_t flags; | |
1005 | }; | |
1006 | ||
252b5132 RH |
1007 | /* Make a BFD section from an ELF section. We store a pointer to the |
1008 | BFD section in the bfd_section field of the header. */ | |
1009 | ||
b34976b6 | 1010 | bfd_boolean |
217aa764 AM |
1011 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
1012 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
1013 | const char *name, |
1014 | int shindex) | |
252b5132 RH |
1015 | { |
1016 | asection *newsect; | |
1017 | flagword flags; | |
9c5bfbb7 | 1018 | const struct elf_backend_data *bed; |
502794d4 | 1019 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
252b5132 RH |
1020 | |
1021 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 1022 | return TRUE; |
252b5132 RH |
1023 | |
1024 | newsect = bfd_make_section_anyway (abfd, name); | |
1025 | if (newsect == NULL) | |
b34976b6 | 1026 | return FALSE; |
252b5132 | 1027 | |
1829f4b2 AM |
1028 | hdr->bfd_section = newsect; |
1029 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 1030 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 1031 | |
2f89ff8d L |
1032 | /* Always use the real type/flags. */ |
1033 | elf_section_type (newsect) = hdr->sh_type; | |
1034 | elf_section_flags (newsect) = hdr->sh_flags; | |
1035 | ||
252b5132 RH |
1036 | newsect->filepos = hdr->sh_offset; |
1037 | ||
252b5132 RH |
1038 | flags = SEC_NO_FLAGS; |
1039 | if (hdr->sh_type != SHT_NOBITS) | |
1040 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 1041 | if (hdr->sh_type == SHT_GROUP) |
7bdf4127 | 1042 | flags |= SEC_GROUP; |
252b5132 RH |
1043 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
1044 | { | |
1045 | flags |= SEC_ALLOC; | |
1046 | if (hdr->sh_type != SHT_NOBITS) | |
1047 | flags |= SEC_LOAD; | |
1048 | } | |
1049 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
1050 | flags |= SEC_READONLY; | |
1051 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
1052 | flags |= SEC_CODE; | |
1053 | else if ((flags & SEC_LOAD) != 0) | |
1054 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
1055 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
1056 | { | |
1057 | flags |= SEC_MERGE; | |
1058 | newsect->entsize = hdr->sh_entsize; | |
f5fa8ca2 | 1059 | } |
84865015 NC |
1060 | if ((hdr->sh_flags & SHF_STRINGS) != 0) |
1061 | flags |= SEC_STRINGS; | |
dbb410c3 AM |
1062 | if (hdr->sh_flags & SHF_GROUP) |
1063 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 1064 | return FALSE; |
13ae64f3 JJ |
1065 | if ((hdr->sh_flags & SHF_TLS) != 0) |
1066 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
1067 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
1068 | flags |= SEC_EXCLUDE; | |
252b5132 | 1069 | |
df3a023b AM |
1070 | switch (elf_elfheader (abfd)->e_ident[EI_OSABI]) |
1071 | { | |
1072 | /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE, | |
1073 | but binutils as of 2019-07-23 did not set the EI_OSABI header | |
1074 | byte. */ | |
1075 | case ELFOSABI_NONE: | |
1076 | case ELFOSABI_GNU: | |
1077 | case ELFOSABI_FREEBSD: | |
1078 | if ((hdr->sh_flags & SHF_GNU_MBIND) != 0) | |
1079 | elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind; | |
1080 | break; | |
1081 | } | |
1082 | ||
3d2b39cf | 1083 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 1084 | { |
3d2b39cf L |
1085 | /* The debugging sections appear to be recognized only by name, |
1086 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
1087 | if (name [0] == '.') |
1088 | { | |
bb294208 AM |
1089 | if (strncmp (name, ".debug", 6) == 0 |
1090 | || strncmp (name, ".gnu.linkonce.wi.", 17) == 0 | |
1091 | || strncmp (name, ".zdebug", 7) == 0) | |
1092 | flags |= SEC_DEBUGGING | SEC_ELF_OCTETS; | |
1093 | else if (strncmp (name, GNU_BUILD_ATTRS_SECTION_NAME, 21) == 0 | |
1094 | || strncmp (name, ".note.gnu", 9) == 0) | |
502794d4 CE |
1095 | { |
1096 | flags |= SEC_ELF_OCTETS; | |
1097 | opb = 1; | |
1098 | } | |
bb294208 AM |
1099 | else if (strncmp (name, ".line", 5) == 0 |
1100 | || strncmp (name, ".stab", 5) == 0 | |
1101 | || strcmp (name, ".gdb_index") == 0) | |
3d2b39cf L |
1102 | flags |= SEC_DEBUGGING; |
1103 | } | |
1104 | } | |
252b5132 | 1105 | |
502794d4 CE |
1106 | if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb) |
1107 | || !bfd_set_section_size (newsect, hdr->sh_size) | |
1108 | || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign))) | |
1109 | return FALSE; | |
1110 | ||
252b5132 RH |
1111 | /* As a GNU extension, if the name begins with .gnu.linkonce, we |
1112 | only link a single copy of the section. This is used to support | |
1113 | g++. g++ will emit each template expansion in its own section. | |
1114 | The symbols will be defined as weak, so that multiple definitions | |
1115 | are permitted. The GNU linker extension is to actually discard | |
1116 | all but one of the sections. */ | |
0112cd26 | 1117 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 1118 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
1119 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
1120 | ||
8c803a2d AM |
1121 | if (!bfd_set_section_flags (newsect, flags)) |
1122 | return FALSE; | |
1123 | ||
fa152c49 JW |
1124 | bed = get_elf_backend_data (abfd); |
1125 | if (bed->elf_backend_section_flags) | |
8c803a2d | 1126 | if (!bed->elf_backend_section_flags (hdr)) |
b34976b6 | 1127 | return FALSE; |
fa152c49 | 1128 | |
718175fa JK |
1129 | /* We do not parse the PT_NOTE segments as we are interested even in the |
1130 | separate debug info files which may have the segments offsets corrupted. | |
1131 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
1132 | if (hdr->sh_type == SHT_NOTE) | |
1133 | { | |
baea7ef1 | 1134 | bfd_byte *contents; |
718175fa | 1135 | |
baea7ef1 | 1136 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
1137 | return FALSE; |
1138 | ||
276da9b3 L |
1139 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, |
1140 | hdr->sh_offset, hdr->sh_addralign); | |
718175fa JK |
1141 | free (contents); |
1142 | } | |
1143 | ||
8c803a2d | 1144 | if ((newsect->flags & SEC_ALLOC) != 0) |
252b5132 RH |
1145 | { |
1146 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
1147 | unsigned int i, nload; |
1148 | ||
1149 | /* Some ELF linkers produce binaries with all the program header | |
1150 | p_paddr fields zero. If we have such a binary with more than | |
1151 | one PT_LOAD header, then leave the section lma equal to vma | |
1152 | so that we don't create sections with overlapping lma. */ | |
1153 | phdr = elf_tdata (abfd)->phdr; | |
1154 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1155 | if (phdr->p_paddr != 0) | |
1156 | break; | |
1157 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
1158 | ++nload; | |
1159 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1160 | return TRUE; | |
252b5132 | 1161 | |
252b5132 RH |
1162 | phdr = elf_tdata (abfd)->phdr; |
1163 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1164 | { | |
86b2281f AM |
1165 | if (((phdr->p_type == PT_LOAD |
1166 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1167 | || phdr->p_type == PT_TLS) | |
9a83a553 | 1168 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 1169 | { |
8c803a2d | 1170 | if ((newsect->flags & SEC_LOAD) == 0) |
88967714 | 1171 | newsect->lma = (phdr->p_paddr |
502794d4 | 1172 | + hdr->sh_addr - phdr->p_vaddr) / opb; |
88967714 AM |
1173 | else |
1174 | /* We used to use the same adjustment for SEC_LOAD | |
1175 | sections, but that doesn't work if the segment | |
1176 | is packed with code from multiple VMAs. | |
1177 | Instead we calculate the section LMA based on | |
1178 | the segment LMA. It is assumed that the | |
1179 | segment will contain sections with contiguous | |
1180 | LMAs, even if the VMAs are not. */ | |
1181 | newsect->lma = (phdr->p_paddr | |
502794d4 | 1182 | + hdr->sh_offset - phdr->p_offset) / opb; |
88967714 AM |
1183 | |
1184 | /* With contiguous segments, we can't tell from file | |
1185 | offsets whether a section with zero size should | |
1186 | be placed at the end of one segment or the | |
1187 | beginning of the next. Decide based on vaddr. */ | |
1188 | if (hdr->sh_addr >= phdr->p_vaddr | |
1189 | && (hdr->sh_addr + hdr->sh_size | |
1190 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1191 | break; | |
252b5132 RH |
1192 | } |
1193 | } | |
1194 | } | |
1195 | ||
4a114e3e L |
1196 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1197 | .zdebug_*, after the section flags is set. */ | |
8c803a2d | 1198 | if ((newsect->flags & SEC_DEBUGGING) |
4a114e3e L |
1199 | && ((name[1] == 'd' && name[6] == '_') |
1200 | || (name[1] == 'z' && name[7] == '_'))) | |
1201 | { | |
1202 | enum { nothing, compress, decompress } action = nothing; | |
151411f8 | 1203 | int compression_header_size; |
dab394de | 1204 | bfd_size_type uncompressed_size; |
4207142d | 1205 | unsigned int uncompressed_align_power; |
151411f8 L |
1206 | bfd_boolean compressed |
1207 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
dab394de | 1208 | &compression_header_size, |
4207142d MW |
1209 | &uncompressed_size, |
1210 | &uncompressed_align_power); | |
151411f8 | 1211 | if (compressed) |
4a114e3e L |
1212 | { |
1213 | /* Compressed section. Check if we should decompress. */ | |
1214 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1215 | action = decompress; | |
1216 | } | |
151411f8 L |
1217 | |
1218 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1219 | section. Check if we should compress. */ | |
1220 | if (action == nothing) | |
4a114e3e | 1221 | { |
151411f8 L |
1222 | if (newsect->size != 0 |
1223 | && (abfd->flags & BFD_COMPRESS) | |
1224 | && compression_header_size >= 0 | |
dab394de | 1225 | && uncompressed_size > 0 |
151411f8 L |
1226 | && (!compressed |
1227 | || ((compression_header_size > 0) | |
1228 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
4a114e3e | 1229 | action = compress; |
151411f8 L |
1230 | else |
1231 | return TRUE; | |
4a114e3e L |
1232 | } |
1233 | ||
151411f8 | 1234 | if (action == compress) |
4a114e3e | 1235 | { |
4a114e3e L |
1236 | if (!bfd_init_section_compress_status (abfd, newsect)) |
1237 | { | |
4eca0228 | 1238 | _bfd_error_handler |
695344c0 | 1239 | /* xgettext:c-format */ |
871b3ab2 | 1240 | (_("%pB: unable to initialize compress status for section %s"), |
4a114e3e L |
1241 | abfd, name); |
1242 | return FALSE; | |
1243 | } | |
151411f8 L |
1244 | } |
1245 | else | |
1246 | { | |
4a114e3e L |
1247 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
1248 | { | |
4eca0228 | 1249 | _bfd_error_handler |
695344c0 | 1250 | /* xgettext:c-format */ |
871b3ab2 | 1251 | (_("%pB: unable to initialize decompress status for section %s"), |
4a114e3e L |
1252 | abfd, name); |
1253 | return FALSE; | |
1254 | } | |
151411f8 L |
1255 | } |
1256 | ||
f6fe1ccd | 1257 | if (abfd->is_linker_input) |
151411f8 | 1258 | { |
f6fe1ccd L |
1259 | if (name[1] == 'z' |
1260 | && (action == decompress | |
1261 | || (action == compress | |
1262 | && (abfd->flags & BFD_COMPRESS_GABI) != 0))) | |
4e011fb5 | 1263 | { |
f6fe1ccd L |
1264 | /* Convert section name from .zdebug_* to .debug_* so |
1265 | that linker will consider this section as a debug | |
1266 | section. */ | |
1267 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
151411f8 L |
1268 | if (new_name == NULL) |
1269 | return FALSE; | |
fd361982 | 1270 | bfd_rename_section (newsect, new_name); |
151411f8 | 1271 | } |
4a114e3e | 1272 | } |
f6fe1ccd L |
1273 | else |
1274 | /* For objdump, don't rename the section. For objcopy, delay | |
1275 | section rename to elf_fake_sections. */ | |
1276 | newsect->flags |= SEC_ELF_RENAME; | |
4a114e3e L |
1277 | } |
1278 | ||
cc5277b1 ML |
1279 | /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information |
1280 | section. */ | |
1281 | const char *lto_section_name = ".gnu.lto_.lto."; | |
1282 | if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0) | |
1283 | { | |
1284 | struct lto_section lsection; | |
1285 | if (bfd_get_section_contents (abfd, newsect, &lsection, 0, | |
1286 | sizeof (struct lto_section))) | |
1287 | abfd->lto_slim_object = lsection.slim_object; | |
1288 | } | |
1289 | ||
b34976b6 | 1290 | return TRUE; |
252b5132 RH |
1291 | } |
1292 | ||
84865015 NC |
1293 | const char *const bfd_elf_section_type_names[] = |
1294 | { | |
252b5132 RH |
1295 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", |
1296 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1297 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1298 | }; | |
1299 | ||
1049f94e | 1300 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1301 | output, and the reloc is against an external symbol, and nothing |
1302 | has given us any additional addend, the resulting reloc will also | |
1303 | be against the same symbol. In such a case, we don't want to | |
1304 | change anything about the way the reloc is handled, since it will | |
1305 | all be done at final link time. Rather than put special case code | |
1306 | into bfd_perform_relocation, all the reloc types use this howto | |
1307 | function. It just short circuits the reloc if producing | |
1049f94e | 1308 | relocatable output against an external symbol. */ |
252b5132 | 1309 | |
252b5132 | 1310 | bfd_reloc_status_type |
217aa764 AM |
1311 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1312 | arelent *reloc_entry, | |
1313 | asymbol *symbol, | |
1314 | void *data ATTRIBUTE_UNUSED, | |
1315 | asection *input_section, | |
1316 | bfd *output_bfd, | |
1317 | char **error_message ATTRIBUTE_UNUSED) | |
1318 | { | |
1319 | if (output_bfd != NULL | |
252b5132 RH |
1320 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1321 | && (! reloc_entry->howto->partial_inplace | |
1322 | || reloc_entry->addend == 0)) | |
1323 | { | |
1324 | reloc_entry->address += input_section->output_offset; | |
1325 | return bfd_reloc_ok; | |
1326 | } | |
1327 | ||
1328 | return bfd_reloc_continue; | |
1329 | } | |
1330 | \f | |
84865015 NC |
1331 | /* Returns TRUE if section A matches section B. |
1332 | Names, addresses and links may be different, but everything else | |
1333 | should be the same. */ | |
1334 | ||
1335 | static bfd_boolean | |
5522f910 NC |
1336 | section_match (const Elf_Internal_Shdr * a, |
1337 | const Elf_Internal_Shdr * b) | |
84865015 | 1338 | { |
ac85e67c AM |
1339 | if (a->sh_type != b->sh_type |
1340 | || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0 | |
1341 | || a->sh_addralign != b->sh_addralign | |
1342 | || a->sh_entsize != b->sh_entsize) | |
1343 | return FALSE; | |
1344 | if (a->sh_type == SHT_SYMTAB | |
1345 | || a->sh_type == SHT_STRTAB) | |
1346 | return TRUE; | |
1347 | return a->sh_size == b->sh_size; | |
84865015 NC |
1348 | } |
1349 | ||
1350 | /* Find a section in OBFD that has the same characteristics | |
1351 | as IHEADER. Return the index of this section or SHN_UNDEF if | |
1352 | none can be found. Check's section HINT first, as this is likely | |
1353 | to be the correct section. */ | |
1354 | ||
1355 | static unsigned int | |
5cc4ca83 ST |
1356 | find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader, |
1357 | const unsigned int hint) | |
84865015 NC |
1358 | { |
1359 | Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); | |
1360 | unsigned int i; | |
1361 | ||
a55c9876 NC |
1362 | BFD_ASSERT (iheader != NULL); |
1363 | ||
1364 | /* See PR 20922 for a reproducer of the NULL test. */ | |
5cc4ca83 ST |
1365 | if (hint < elf_numsections (obfd) |
1366 | && oheaders[hint] != NULL | |
a55c9876 | 1367 | && section_match (oheaders[hint], iheader)) |
84865015 NC |
1368 | return hint; |
1369 | ||
1370 | for (i = 1; i < elf_numsections (obfd); i++) | |
1371 | { | |
1372 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
1373 | ||
a55c9876 NC |
1374 | if (oheader == NULL) |
1375 | continue; | |
84865015 NC |
1376 | if (section_match (oheader, iheader)) |
1377 | /* FIXME: Do we care if there is a potential for | |
1378 | multiple matches ? */ | |
1379 | return i; | |
1380 | } | |
1381 | ||
1382 | return SHN_UNDEF; | |
1383 | } | |
1384 | ||
5522f910 NC |
1385 | /* PR 19938: Attempt to set the ELF section header fields of an OS or |
1386 | Processor specific section, based upon a matching input section. | |
1387 | Returns TRUE upon success, FALSE otherwise. */ | |
07d6d2b8 | 1388 | |
5522f910 NC |
1389 | static bfd_boolean |
1390 | copy_special_section_fields (const bfd *ibfd, | |
1391 | bfd *obfd, | |
1392 | const Elf_Internal_Shdr *iheader, | |
1393 | Elf_Internal_Shdr *oheader, | |
1394 | const unsigned int secnum) | |
1395 | { | |
1396 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
1397 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); | |
1398 | bfd_boolean changed = FALSE; | |
1399 | unsigned int sh_link; | |
1400 | ||
1401 | if (oheader->sh_type == SHT_NOBITS) | |
1402 | { | |
1403 | /* This is a feature for objcopy --only-keep-debug: | |
1404 | When a section's type is changed to NOBITS, we preserve | |
1405 | the sh_link and sh_info fields so that they can be | |
1406 | matched up with the original. | |
1407 | ||
1408 | Note: Strictly speaking these assignments are wrong. | |
1409 | The sh_link and sh_info fields should point to the | |
1410 | relevent sections in the output BFD, which may not be in | |
1411 | the same location as they were in the input BFD. But | |
1412 | the whole point of this action is to preserve the | |
1413 | original values of the sh_link and sh_info fields, so | |
1414 | that they can be matched up with the section headers in | |
1415 | the original file. So strictly speaking we may be | |
1416 | creating an invalid ELF file, but it is only for a file | |
1417 | that just contains debug info and only for sections | |
1418 | without any contents. */ | |
1419 | if (oheader->sh_link == 0) | |
1420 | oheader->sh_link = iheader->sh_link; | |
1421 | if (oheader->sh_info == 0) | |
1422 | oheader->sh_info = iheader->sh_info; | |
1423 | return TRUE; | |
1424 | } | |
1425 | ||
1426 | /* Allow the target a chance to decide how these fields should be set. */ | |
1427 | if (bed->elf_backend_copy_special_section_fields != NULL | |
1428 | && bed->elf_backend_copy_special_section_fields | |
1429 | (ibfd, obfd, iheader, oheader)) | |
1430 | return TRUE; | |
1431 | ||
1432 | /* We have an iheader which might match oheader, and which has non-zero | |
1433 | sh_info and/or sh_link fields. Attempt to follow those links and find | |
1434 | the section in the output bfd which corresponds to the linked section | |
1435 | in the input bfd. */ | |
1436 | if (iheader->sh_link != SHN_UNDEF) | |
1437 | { | |
4f3ca05b NC |
1438 | /* See PR 20931 for a reproducer. */ |
1439 | if (iheader->sh_link >= elf_numsections (ibfd)) | |
1440 | { | |
76cfced5 | 1441 | _bfd_error_handler |
4f3ca05b | 1442 | /* xgettext:c-format */ |
9793eb77 | 1443 | (_("%pB: invalid sh_link field (%d) in section number %d"), |
4f3ca05b NC |
1444 | ibfd, iheader->sh_link, secnum); |
1445 | return FALSE; | |
1446 | } | |
1447 | ||
5522f910 NC |
1448 | sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); |
1449 | if (sh_link != SHN_UNDEF) | |
1450 | { | |
1451 | oheader->sh_link = sh_link; | |
1452 | changed = TRUE; | |
1453 | } | |
1454 | else | |
1455 | /* FIXME: Should we install iheader->sh_link | |
1456 | if we could not find a match ? */ | |
76cfced5 | 1457 | _bfd_error_handler |
695344c0 | 1458 | /* xgettext:c-format */ |
9793eb77 | 1459 | (_("%pB: failed to find link section for section %d"), obfd, secnum); |
5522f910 NC |
1460 | } |
1461 | ||
1462 | if (iheader->sh_info) | |
1463 | { | |
1464 | /* The sh_info field can hold arbitrary information, but if the | |
1465 | SHF_LINK_INFO flag is set then it should be interpreted as a | |
1466 | section index. */ | |
1467 | if (iheader->sh_flags & SHF_INFO_LINK) | |
1468 | { | |
1469 | sh_link = find_link (obfd, iheaders[iheader->sh_info], | |
1470 | iheader->sh_info); | |
1471 | if (sh_link != SHN_UNDEF) | |
1472 | oheader->sh_flags |= SHF_INFO_LINK; | |
1473 | } | |
1474 | else | |
1475 | /* No idea what it means - just copy it. */ | |
1476 | sh_link = iheader->sh_info; | |
1477 | ||
1478 | if (sh_link != SHN_UNDEF) | |
1479 | { | |
1480 | oheader->sh_info = sh_link; | |
1481 | changed = TRUE; | |
1482 | } | |
1483 | else | |
76cfced5 | 1484 | _bfd_error_handler |
695344c0 | 1485 | /* xgettext:c-format */ |
9793eb77 | 1486 | (_("%pB: failed to find info section for section %d"), obfd, secnum); |
5522f910 NC |
1487 | } |
1488 | ||
1489 | return changed; | |
1490 | } | |
07d6d2b8 | 1491 | |
0ac4564e L |
1492 | /* Copy the program header and other data from one object module to |
1493 | another. */ | |
252b5132 | 1494 | |
b34976b6 | 1495 | bfd_boolean |
217aa764 | 1496 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 | 1497 | { |
5522f910 NC |
1498 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); |
1499 | Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); | |
1500 | const struct elf_backend_data *bed; | |
84865015 NC |
1501 | unsigned int i; |
1502 | ||
2d502050 | 1503 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
84865015 | 1504 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
b34976b6 | 1505 | return TRUE; |
2d502050 | 1506 | |
57b828ef L |
1507 | if (!elf_flags_init (obfd)) |
1508 | { | |
1509 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1510 | elf_flags_init (obfd) = TRUE; | |
1511 | } | |
2d502050 | 1512 | |
0ac4564e | 1513 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1514 | |
1515 | /* Also copy the EI_OSABI field. */ | |
1516 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1517 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 | 1518 | |
5522f910 NC |
1519 | /* If set, copy the EI_ABIVERSION field. */ |
1520 | if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) | |
1521 | elf_elfheader (obfd)->e_ident[EI_ABIVERSION] | |
1522 | = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; | |
07d6d2b8 | 1523 | |
104d59d1 JM |
1524 | /* Copy object attributes. */ |
1525 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
63b9bbb7 | 1526 | |
84865015 NC |
1527 | if (iheaders == NULL || oheaders == NULL) |
1528 | return TRUE; | |
63b9bbb7 | 1529 | |
5522f910 NC |
1530 | bed = get_elf_backend_data (obfd); |
1531 | ||
1532 | /* Possibly copy other fields in the section header. */ | |
84865015 | 1533 | for (i = 1; i < elf_numsections (obfd); i++) |
63b9bbb7 | 1534 | { |
84865015 NC |
1535 | unsigned int j; |
1536 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
63b9bbb7 | 1537 | |
5522f910 NC |
1538 | /* Ignore ordinary sections. SHT_NOBITS sections are considered however |
1539 | because of a special case need for generating separate debug info | |
1540 | files. See below for more details. */ | |
84865015 NC |
1541 | if (oheader == NULL |
1542 | || (oheader->sh_type != SHT_NOBITS | |
5522f910 NC |
1543 | && oheader->sh_type < SHT_LOOS)) |
1544 | continue; | |
1545 | ||
1546 | /* Ignore empty sections, and sections whose | |
1547 | fields have already been initialised. */ | |
1548 | if (oheader->sh_size == 0 | |
84865015 NC |
1549 | || (oheader->sh_info != 0 && oheader->sh_link != 0)) |
1550 | continue; | |
63b9bbb7 | 1551 | |
84865015 | 1552 | /* Scan for the matching section in the input bfd. |
5522f910 NC |
1553 | First we try for a direct mapping between the input and output sections. */ |
1554 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1555 | { | |
1556 | const Elf_Internal_Shdr * iheader = iheaders[j]; | |
1557 | ||
1558 | if (iheader == NULL) | |
1559 | continue; | |
1560 | ||
1561 | if (oheader->bfd_section != NULL | |
1562 | && iheader->bfd_section != NULL | |
1563 | && iheader->bfd_section->output_section != NULL | |
1564 | && iheader->bfd_section->output_section == oheader->bfd_section) | |
1565 | { | |
1566 | /* We have found a connection from the input section to the | |
1567 | output section. Attempt to copy the header fields. If | |
1568 | this fails then do not try any further sections - there | |
1569 | should only be a one-to-one mapping between input and output. */ | |
1570 | if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) | |
1571 | j = elf_numsections (ibfd); | |
1572 | break; | |
1573 | } | |
1574 | } | |
1575 | ||
1576 | if (j < elf_numsections (ibfd)) | |
1577 | continue; | |
1578 | ||
1579 | /* That failed. So try to deduce the corresponding input section. | |
84865015 NC |
1580 | Unfortunately we cannot compare names as the output string table |
1581 | is empty, so instead we check size, address and type. */ | |
1582 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1583 | { | |
5522f910 | 1584 | const Elf_Internal_Shdr * iheader = iheaders[j]; |
84865015 | 1585 | |
5522f910 NC |
1586 | if (iheader == NULL) |
1587 | continue; | |
1588 | ||
1589 | /* Try matching fields in the input section's header. | |
1590 | Since --only-keep-debug turns all non-debug sections into | |
84865015 NC |
1591 | SHT_NOBITS sections, the output SHT_NOBITS type matches any |
1592 | input type. */ | |
1593 | if ((oheader->sh_type == SHT_NOBITS | |
1594 | || iheader->sh_type == oheader->sh_type) | |
5522f910 NC |
1595 | && (iheader->sh_flags & ~ SHF_INFO_LINK) |
1596 | == (oheader->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1597 | && iheader->sh_addralign == oheader->sh_addralign |
1598 | && iheader->sh_entsize == oheader->sh_entsize | |
1599 | && iheader->sh_size == oheader->sh_size | |
1600 | && iheader->sh_addr == oheader->sh_addr | |
1601 | && (iheader->sh_info != oheader->sh_info | |
1602 | || iheader->sh_link != oheader->sh_link)) | |
63b9bbb7 | 1603 | { |
5522f910 NC |
1604 | if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) |
1605 | break; | |
63b9bbb7 NC |
1606 | } |
1607 | } | |
5522f910 NC |
1608 | |
1609 | if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) | |
1610 | { | |
1611 | /* Final attempt. Call the backend copy function | |
1612 | with a NULL input section. */ | |
1613 | if (bed->elf_backend_copy_special_section_fields != NULL) | |
a8e14f4c | 1614 | (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader); |
5522f910 | 1615 | } |
63b9bbb7 NC |
1616 | } |
1617 | ||
b34976b6 | 1618 | return TRUE; |
2d502050 L |
1619 | } |
1620 | ||
cedc298e L |
1621 | static const char * |
1622 | get_segment_type (unsigned int p_type) | |
1623 | { | |
1624 | const char *pt; | |
1625 | switch (p_type) | |
1626 | { | |
1627 | case PT_NULL: pt = "NULL"; break; | |
1628 | case PT_LOAD: pt = "LOAD"; break; | |
1629 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1630 | case PT_INTERP: pt = "INTERP"; break; | |
1631 | case PT_NOTE: pt = "NOTE"; break; | |
1632 | case PT_SHLIB: pt = "SHLIB"; break; | |
1633 | case PT_PHDR: pt = "PHDR"; break; | |
1634 | case PT_TLS: pt = "TLS"; break; | |
1635 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1636 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1637 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1638 | default: pt = NULL; break; | |
1639 | } | |
1640 | return pt; | |
1641 | } | |
1642 | ||
f0b79d91 L |
1643 | /* Print out the program headers. */ |
1644 | ||
b34976b6 | 1645 | bfd_boolean |
217aa764 | 1646 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1647 | { |
a50b1753 | 1648 | FILE *f = (FILE *) farg; |
252b5132 RH |
1649 | Elf_Internal_Phdr *p; |
1650 | asection *s; | |
1651 | bfd_byte *dynbuf = NULL; | |
1652 | ||
1653 | p = elf_tdata (abfd)->phdr; | |
1654 | if (p != NULL) | |
1655 | { | |
1656 | unsigned int i, c; | |
1657 | ||
1658 | fprintf (f, _("\nProgram Header:\n")); | |
1659 | c = elf_elfheader (abfd)->e_phnum; | |
1660 | for (i = 0; i < c; i++, p++) | |
1661 | { | |
cedc298e | 1662 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1663 | char buf[20]; |
1664 | ||
cedc298e | 1665 | if (pt == NULL) |
252b5132 | 1666 | { |
cedc298e L |
1667 | sprintf (buf, "0x%lx", p->p_type); |
1668 | pt = buf; | |
252b5132 | 1669 | } |
dc810e39 | 1670 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1671 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1672 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1673 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1674 | fprintf (f, " paddr 0x"); |
60b89a18 | 1675 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1676 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1677 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1678 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1679 | fprintf (f, " memsz 0x"); |
60b89a18 | 1680 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1681 | fprintf (f, " flags %c%c%c", |
1682 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1683 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1684 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1685 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1686 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1687 | fprintf (f, "\n"); |
1688 | } | |
1689 | } | |
1690 | ||
1691 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1692 | if (s != NULL) | |
1693 | { | |
cb33740c | 1694 | unsigned int elfsec; |
dc810e39 | 1695 | unsigned long shlink; |
252b5132 RH |
1696 | bfd_byte *extdyn, *extdynend; |
1697 | size_t extdynsize; | |
217aa764 | 1698 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1699 | |
1700 | fprintf (f, _("\nDynamic Section:\n")); | |
1701 | ||
eea6121a | 1702 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1703 | goto error_return; |
1704 | ||
1705 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1706 | if (elfsec == SHN_BAD) |
252b5132 | 1707 | goto error_return; |
dc810e39 | 1708 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1709 | |
1710 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1711 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1712 | ||
1713 | extdyn = dynbuf; | |
06614111 NC |
1714 | /* PR 17512: file: 6f427532. */ |
1715 | if (s->size < extdynsize) | |
1716 | goto error_return; | |
eea6121a | 1717 | extdynend = extdyn + s->size; |
1036838a | 1718 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
07d6d2b8 | 1719 | Fix range check. */ |
1036838a | 1720 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) |
252b5132 RH |
1721 | { |
1722 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1723 | const char *name = ""; |
252b5132 | 1724 | char ab[20]; |
b34976b6 | 1725 | bfd_boolean stringp; |
ad9563d6 | 1726 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1727 | |
217aa764 | 1728 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1729 | |
1730 | if (dyn.d_tag == DT_NULL) | |
1731 | break; | |
1732 | ||
b34976b6 | 1733 | stringp = FALSE; |
252b5132 RH |
1734 | switch (dyn.d_tag) |
1735 | { | |
1736 | default: | |
ad9563d6 CM |
1737 | if (bed->elf_backend_get_target_dtag) |
1738 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1739 | ||
1740 | if (!strcmp (name, "")) | |
1741 | { | |
cd9af601 | 1742 | sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag); |
ad9563d6 CM |
1743 | name = ab; |
1744 | } | |
252b5132 RH |
1745 | break; |
1746 | ||
b34976b6 | 1747 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1748 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1749 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1750 | case DT_HASH: name = "HASH"; break; | |
1751 | case DT_STRTAB: name = "STRTAB"; break; | |
1752 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1753 | case DT_RELA: name = "RELA"; break; | |
1754 | case DT_RELASZ: name = "RELASZ"; break; | |
1755 | case DT_RELAENT: name = "RELAENT"; break; | |
1756 | case DT_STRSZ: name = "STRSZ"; break; | |
1757 | case DT_SYMENT: name = "SYMENT"; break; | |
1758 | case DT_INIT: name = "INIT"; break; | |
1759 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1760 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1761 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1762 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1763 | case DT_REL: name = "REL"; break; | |
1764 | case DT_RELSZ: name = "RELSZ"; break; | |
1765 | case DT_RELENT: name = "RELENT"; break; | |
1766 | case DT_PLTREL: name = "PLTREL"; break; | |
1767 | case DT_DEBUG: name = "DEBUG"; break; | |
1768 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1769 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1770 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1771 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1772 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1773 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1774 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1775 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1776 | case DT_FLAGS: name = "FLAGS"; break; |
1777 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1778 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1779 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1780 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1781 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1782 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1783 | case DT_FEATURE: name = "FEATURE"; break; | |
1784 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1785 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1786 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1787 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1788 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1789 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1790 | case DT_PLTPAD: name = "PLTPAD"; break; |
1791 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1792 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1793 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1794 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1795 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1796 | case DT_VERSYM: name = "VERSYM"; break; |
1797 | case DT_VERDEF: name = "VERDEF"; break; | |
1798 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1799 | case DT_VERNEED: name = "VERNEED"; break; | |
1800 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1801 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1802 | case DT_USED: name = "USED"; break; |
b34976b6 | 1803 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1804 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1805 | } |
1806 | ||
ad9563d6 | 1807 | fprintf (f, " %-20s ", name); |
252b5132 | 1808 | if (! stringp) |
a1f3c56e AN |
1809 | { |
1810 | fprintf (f, "0x"); | |
1811 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1812 | } | |
252b5132 RH |
1813 | else |
1814 | { | |
1815 | const char *string; | |
dc810e39 | 1816 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1817 | |
dc810e39 | 1818 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1819 | if (string == NULL) |
1820 | goto error_return; | |
1821 | fprintf (f, "%s", string); | |
1822 | } | |
1823 | fprintf (f, "\n"); | |
1824 | } | |
1825 | ||
1826 | free (dynbuf); | |
1827 | dynbuf = NULL; | |
1828 | } | |
1829 | ||
1830 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1831 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1832 | { | |
fc0e6df6 | 1833 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1834 | return FALSE; |
252b5132 RH |
1835 | } |
1836 | ||
1837 | if (elf_dynverdef (abfd) != 0) | |
1838 | { | |
1839 | Elf_Internal_Verdef *t; | |
1840 | ||
1841 | fprintf (f, _("\nVersion definitions:\n")); | |
1842 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1843 | { | |
1844 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1845 | t->vd_flags, t->vd_hash, |
1846 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1847 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1848 | { |
1849 | Elf_Internal_Verdaux *a; | |
1850 | ||
1851 | fprintf (f, "\t"); | |
1852 | for (a = t->vd_auxptr->vda_nextptr; | |
1853 | a != NULL; | |
1854 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1855 | fprintf (f, "%s ", |
1856 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1857 | fprintf (f, "\n"); |
1858 | } | |
1859 | } | |
1860 | } | |
1861 | ||
1862 | if (elf_dynverref (abfd) != 0) | |
1863 | { | |
1864 | Elf_Internal_Verneed *t; | |
1865 | ||
1866 | fprintf (f, _("\nVersion References:\n")); | |
1867 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1868 | { | |
1869 | Elf_Internal_Vernaux *a; | |
1870 | ||
d0fb9a8d JJ |
1871 | fprintf (f, _(" required from %s:\n"), |
1872 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1873 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1874 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1875 | a->vna_flags, a->vna_other, |
1876 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1877 | } |
1878 | } | |
1879 | ||
b34976b6 | 1880 | return TRUE; |
252b5132 RH |
1881 | |
1882 | error_return: | |
1883 | if (dynbuf != NULL) | |
1884 | free (dynbuf); | |
b34976b6 | 1885 | return FALSE; |
252b5132 RH |
1886 | } |
1887 | ||
bb4d2ac2 L |
1888 | /* Get version string. */ |
1889 | ||
1890 | const char * | |
60bb06bc L |
1891 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1892 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1893 | { |
1894 | const char *version_string = NULL; | |
1895 | if (elf_dynversym (abfd) != 0 | |
1896 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1897 | { | |
1898 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1899 | ||
1900 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1901 | vernum &= VERSYM_VERSION; | |
1902 | ||
1903 | if (vernum == 0) | |
1904 | version_string = ""; | |
1f6f5dba L |
1905 | else if (vernum == 1 |
1906 | && (vernum > elf_tdata (abfd)->cverdefs | |
1907 | || (elf_tdata (abfd)->verdef[0].vd_flags | |
1908 | == VER_FLG_BASE))) | |
bb4d2ac2 L |
1909 | version_string = "Base"; |
1910 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1911 | version_string = | |
1912 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1913 | else | |
1914 | { | |
1915 | Elf_Internal_Verneed *t; | |
1916 | ||
7a815dd5 | 1917 | version_string = _("<corrupt>"); |
bb4d2ac2 L |
1918 | for (t = elf_tdata (abfd)->verref; |
1919 | t != NULL; | |
1920 | t = t->vn_nextref) | |
1921 | { | |
1922 | Elf_Internal_Vernaux *a; | |
1923 | ||
1924 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1925 | { | |
1926 | if (a->vna_other == vernum) | |
1927 | { | |
1928 | version_string = a->vna_nodename; | |
1929 | break; | |
1930 | } | |
1931 | } | |
1932 | } | |
1933 | } | |
1934 | } | |
1935 | return version_string; | |
1936 | } | |
1937 | ||
252b5132 RH |
1938 | /* Display ELF-specific fields of a symbol. */ |
1939 | ||
1940 | void | |
217aa764 AM |
1941 | bfd_elf_print_symbol (bfd *abfd, |
1942 | void *filep, | |
1943 | asymbol *symbol, | |
1944 | bfd_print_symbol_type how) | |
252b5132 | 1945 | { |
a50b1753 | 1946 | FILE *file = (FILE *) filep; |
252b5132 RH |
1947 | switch (how) |
1948 | { | |
1949 | case bfd_print_symbol_name: | |
1950 | fprintf (file, "%s", symbol->name); | |
1951 | break; | |
1952 | case bfd_print_symbol_more: | |
1953 | fprintf (file, "elf "); | |
60b89a18 | 1954 | bfd_fprintf_vma (abfd, file, symbol->value); |
cd9af601 | 1955 | fprintf (file, " %x", symbol->flags); |
252b5132 RH |
1956 | break; |
1957 | case bfd_print_symbol_all: | |
1958 | { | |
4e8a9624 AM |
1959 | const char *section_name; |
1960 | const char *name = NULL; | |
9c5bfbb7 | 1961 | const struct elf_backend_data *bed; |
7a13edea | 1962 | unsigned char st_other; |
dbb410c3 | 1963 | bfd_vma val; |
bb4d2ac2 L |
1964 | const char *version_string; |
1965 | bfd_boolean hidden; | |
c044fabd | 1966 | |
252b5132 | 1967 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1968 | |
1969 | bed = get_elf_backend_data (abfd); | |
1970 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1971 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1972 | |
1973 | if (name == NULL) | |
1974 | { | |
7ee38065 | 1975 | name = symbol->name; |
217aa764 | 1976 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1977 | } |
1978 | ||
252b5132 RH |
1979 | fprintf (file, " %s\t", section_name); |
1980 | /* Print the "other" value for a symbol. For common symbols, | |
1981 | we've already printed the size; now print the alignment. | |
1982 | For other symbols, we have no specified alignment, and | |
1983 | we've printed the address; now print the size. */ | |
dcf6c779 | 1984 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1985 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1986 | else | |
1987 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1988 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1989 | |
1990 | /* If we have version information, print it. */ | |
60bb06bc L |
1991 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1992 | symbol, | |
1993 | &hidden); | |
bb4d2ac2 | 1994 | if (version_string) |
252b5132 | 1995 | { |
bb4d2ac2 | 1996 | if (!hidden) |
252b5132 RH |
1997 | fprintf (file, " %-11s", version_string); |
1998 | else | |
1999 | { | |
2000 | int i; | |
2001 | ||
2002 | fprintf (file, " (%s)", version_string); | |
2003 | for (i = 10 - strlen (version_string); i > 0; --i) | |
2004 | putc (' ', file); | |
2005 | } | |
2006 | } | |
2007 | ||
2008 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 2009 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 2010 | |
7a13edea NC |
2011 | switch (st_other) |
2012 | { | |
2013 | case 0: break; | |
2014 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
2015 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
2016 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
2017 | default: | |
2018 | /* Some other non-defined flags are also present, so print | |
2019 | everything hex. */ | |
2020 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
2021 | } | |
252b5132 | 2022 | |
587ff49e | 2023 | fprintf (file, " %s", name); |
252b5132 RH |
2024 | } |
2025 | break; | |
2026 | } | |
2027 | } | |
252b5132 RH |
2028 | \f |
2029 | /* ELF .o/exec file reading */ | |
2030 | ||
c044fabd | 2031 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 2032 | |
b34976b6 | 2033 | bfd_boolean |
217aa764 | 2034 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 2035 | { |
4fbb74a6 AM |
2036 | Elf_Internal_Shdr *hdr; |
2037 | Elf_Internal_Ehdr *ehdr; | |
2038 | const struct elf_backend_data *bed; | |
90937f86 | 2039 | const char *name; |
bf67003b NC |
2040 | bfd_boolean ret = TRUE; |
2041 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 2042 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 2043 | static unsigned int nesting = 0; |
252b5132 | 2044 | |
4fbb74a6 AM |
2045 | if (shindex >= elf_numsections (abfd)) |
2046 | return FALSE; | |
2047 | ||
bf67003b NC |
2048 | if (++ nesting > 3) |
2049 | { | |
2050 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
67ce483b | 2051 | sections, with each the string indices pointing to the next in the |
bf67003b NC |
2052 | loop. Detect this here, by refusing to load a section that we are |
2053 | already in the process of loading. We only trigger this test if | |
2054 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
2055 | can expect to recurse at least once. |
2056 | ||
2057 | FIXME: It would be better if this array was attached to the bfd, | |
2058 | rather than being held in a static pointer. */ | |
2059 | ||
2060 | if (sections_being_created_abfd != abfd) | |
2061 | sections_being_created = NULL; | |
bf67003b NC |
2062 | if (sections_being_created == NULL) |
2063 | { | |
446f7ed5 AM |
2064 | size_t amt = elf_numsections (abfd) * sizeof (bfd_boolean); |
2065 | sections_being_created = (bfd_boolean *) bfd_zalloc (abfd, amt); | |
96d3b80f AM |
2066 | if (sections_being_created == NULL) |
2067 | return FALSE; | |
5a4b0ccc | 2068 | sections_being_created_abfd = abfd; |
bf67003b NC |
2069 | } |
2070 | if (sections_being_created [shindex]) | |
2071 | { | |
4eca0228 | 2072 | _bfd_error_handler |
871b3ab2 | 2073 | (_("%pB: warning: loop in section dependencies detected"), abfd); |
bf67003b NC |
2074 | return FALSE; |
2075 | } | |
2076 | sections_being_created [shindex] = TRUE; | |
2077 | } | |
2078 | ||
4fbb74a6 AM |
2079 | hdr = elf_elfsections (abfd)[shindex]; |
2080 | ehdr = elf_elfheader (abfd); | |
2081 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 2082 | hdr->sh_name); |
933d961a | 2083 | if (name == NULL) |
bf67003b | 2084 | goto fail; |
252b5132 | 2085 | |
4fbb74a6 | 2086 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
2087 | switch (hdr->sh_type) |
2088 | { | |
2089 | case SHT_NULL: | |
2090 | /* Inactive section. Throw it away. */ | |
bf67003b | 2091 | goto success; |
252b5132 | 2092 | |
bf67003b NC |
2093 | case SHT_PROGBITS: /* Normal section with contents. */ |
2094 | case SHT_NOBITS: /* .bss section. */ | |
2095 | case SHT_HASH: /* .hash section. */ | |
2096 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
2097 | case SHT_INIT_ARRAY: /* .init_array section. */ |
2098 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
2099 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 2100 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 2101 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
2102 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2103 | goto success; | |
252b5132 | 2104 | |
797fc050 | 2105 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 2106 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2107 | goto fail; |
2108 | ||
cfcac11d NC |
2109 | if (hdr->sh_link > elf_numsections (abfd)) |
2110 | { | |
caa83f8b | 2111 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
2112 | field set to SHN_BEFORE or SHN_AFTER. */ |
2113 | switch (bfd_get_arch (abfd)) | |
2114 | { | |
caa83f8b | 2115 | case bfd_arch_i386: |
cfcac11d NC |
2116 | case bfd_arch_sparc: |
2117 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
2118 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
2119 | break; | |
2120 | /* Otherwise fall through. */ | |
2121 | default: | |
bf67003b | 2122 | goto fail; |
cfcac11d NC |
2123 | } |
2124 | } | |
2125 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 2126 | goto fail; |
cfcac11d | 2127 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
2128 | { |
2129 | Elf_Internal_Shdr *dynsymhdr; | |
2130 | ||
2131 | /* The shared libraries distributed with hpux11 have a bogus | |
2132 | sh_link field for the ".dynamic" section. Find the | |
2133 | string table for the ".dynsym" section instead. */ | |
2134 | if (elf_dynsymtab (abfd) != 0) | |
2135 | { | |
2136 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
2137 | hdr->sh_link = dynsymhdr->sh_link; | |
2138 | } | |
2139 | else | |
2140 | { | |
2141 | unsigned int i, num_sec; | |
2142 | ||
2143 | num_sec = elf_numsections (abfd); | |
2144 | for (i = 1; i < num_sec; i++) | |
2145 | { | |
2146 | dynsymhdr = elf_elfsections (abfd)[i]; | |
2147 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
2148 | { | |
2149 | hdr->sh_link = dynsymhdr->sh_link; | |
2150 | break; | |
2151 | } | |
2152 | } | |
2153 | } | |
2154 | } | |
bf67003b | 2155 | goto success; |
797fc050 | 2156 | |
bf67003b | 2157 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 2158 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 2159 | goto success; |
252b5132 | 2160 | |
a50b2160 | 2161 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2162 | goto fail; |
2163 | ||
3337c1e5 | 2164 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
2165 | { |
2166 | if (hdr->sh_size != 0) | |
bf67003b | 2167 | goto fail; |
eee3b786 AM |
2168 | /* Some assemblers erroneously set sh_info to one with a |
2169 | zero sh_size. ld sees this as a global symbol count | |
2170 | of (unsigned) -1. Fix it here. */ | |
2171 | hdr->sh_info = 0; | |
bf67003b | 2172 | goto success; |
eee3b786 | 2173 | } |
bf67003b | 2174 | |
16ad13ec NC |
2175 | /* PR 18854: A binary might contain more than one symbol table. |
2176 | Unusual, but possible. Warn, but continue. */ | |
2177 | if (elf_onesymtab (abfd) != 0) | |
2178 | { | |
4eca0228 | 2179 | _bfd_error_handler |
695344c0 | 2180 | /* xgettext:c-format */ |
871b3ab2 | 2181 | (_("%pB: warning: multiple symbol tables detected" |
63a5468a | 2182 | " - ignoring the table in section %u"), |
16ad13ec NC |
2183 | abfd, shindex); |
2184 | goto success; | |
2185 | } | |
252b5132 | 2186 | elf_onesymtab (abfd) = shindex; |
6a40cf0c NC |
2187 | elf_symtab_hdr (abfd) = *hdr; |
2188 | elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); | |
252b5132 RH |
2189 | abfd->flags |= HAS_SYMS; |
2190 | ||
2191 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
2192 | SHF_ALLOC is set, and this is a shared object, then we also |
2193 | treat this section as a BFD section. We can not base the | |
2194 | decision purely on SHF_ALLOC, because that flag is sometimes | |
2195 | set in a relocatable object file, which would confuse the | |
2196 | linker. */ | |
252b5132 RH |
2197 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
2198 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
2199 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2200 | shindex)) | |
bf67003b | 2201 | goto fail; |
252b5132 | 2202 | |
1b3a8575 AM |
2203 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
2204 | can't read symbols without that section loaded as well. It | |
2205 | is most likely specified by the next section header. */ | |
6a40cf0c NC |
2206 | { |
2207 | elf_section_list * entry; | |
2208 | unsigned int i, num_sec; | |
1b3a8575 | 2209 | |
6a40cf0c NC |
2210 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2211 | if (entry->hdr.sh_link == shindex) | |
2212 | goto success; | |
2213 | ||
2214 | num_sec = elf_numsections (abfd); | |
2215 | for (i = shindex + 1; i < num_sec; i++) | |
2216 | { | |
2217 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2218 | ||
2219 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
2220 | && hdr2->sh_link == shindex) | |
2221 | break; | |
2222 | } | |
2223 | ||
2224 | if (i == num_sec) | |
2225 | for (i = 1; i < shindex; i++) | |
1b3a8575 AM |
2226 | { |
2227 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
6a40cf0c | 2228 | |
1b3a8575 AM |
2229 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX |
2230 | && hdr2->sh_link == shindex) | |
2231 | break; | |
2232 | } | |
6a40cf0c NC |
2233 | |
2234 | if (i != shindex) | |
2235 | ret = bfd_section_from_shdr (abfd, i); | |
2236 | /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ | |
2237 | goto success; | |
2238 | } | |
252b5132 | 2239 | |
bf67003b | 2240 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 2241 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 2242 | goto success; |
252b5132 | 2243 | |
a50b2160 | 2244 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2245 | goto fail; |
2246 | ||
eee3b786 AM |
2247 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
2248 | { | |
2249 | if (hdr->sh_size != 0) | |
bf67003b NC |
2250 | goto fail; |
2251 | ||
eee3b786 AM |
2252 | /* Some linkers erroneously set sh_info to one with a |
2253 | zero sh_size. ld sees this as a global symbol count | |
2254 | of (unsigned) -1. Fix it here. */ | |
2255 | hdr->sh_info = 0; | |
bf67003b | 2256 | goto success; |
eee3b786 | 2257 | } |
bf67003b | 2258 | |
16ad13ec NC |
2259 | /* PR 18854: A binary might contain more than one dynamic symbol table. |
2260 | Unusual, but possible. Warn, but continue. */ | |
2261 | if (elf_dynsymtab (abfd) != 0) | |
2262 | { | |
4eca0228 | 2263 | _bfd_error_handler |
695344c0 | 2264 | /* xgettext:c-format */ |
871b3ab2 | 2265 | (_("%pB: warning: multiple dynamic symbol tables detected" |
63a5468a | 2266 | " - ignoring the table in section %u"), |
16ad13ec NC |
2267 | abfd, shindex); |
2268 | goto success; | |
2269 | } | |
252b5132 RH |
2270 | elf_dynsymtab (abfd) = shindex; |
2271 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
2272 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2273 | abfd->flags |= HAS_SYMS; | |
2274 | ||
2275 | /* Besides being a symbol table, we also treat this as a regular | |
2276 | section, so that objcopy can handle it. */ | |
bf67003b NC |
2277 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2278 | goto success; | |
252b5132 | 2279 | |
bf67003b | 2280 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
6a40cf0c NC |
2281 | { |
2282 | elf_section_list * entry; | |
9ad5cbcf | 2283 | |
6a40cf0c NC |
2284 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2285 | if (entry->ndx == shindex) | |
2286 | goto success; | |
07d6d2b8 | 2287 | |
7a6e0d89 | 2288 | entry = bfd_alloc (abfd, sizeof (*entry)); |
6a40cf0c NC |
2289 | if (entry == NULL) |
2290 | goto fail; | |
2291 | entry->ndx = shindex; | |
2292 | entry->hdr = * hdr; | |
2293 | entry->next = elf_symtab_shndx_list (abfd); | |
2294 | elf_symtab_shndx_list (abfd) = entry; | |
2295 | elf_elfsections (abfd)[shindex] = & entry->hdr; | |
2296 | goto success; | |
2297 | } | |
9ad5cbcf | 2298 | |
bf67003b | 2299 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 2300 | if (hdr->bfd_section != NULL) |
bf67003b NC |
2301 | goto success; |
2302 | ||
252b5132 RH |
2303 | if (ehdr->e_shstrndx == shindex) |
2304 | { | |
2305 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
2306 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 2307 | goto success; |
252b5132 | 2308 | } |
bf67003b | 2309 | |
1b3a8575 AM |
2310 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
2311 | { | |
2312 | symtab_strtab: | |
2313 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
2314 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 2315 | goto success; |
1b3a8575 | 2316 | } |
bf67003b | 2317 | |
1b3a8575 AM |
2318 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
2319 | { | |
2320 | dynsymtab_strtab: | |
2321 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
2322 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
2323 | elf_elfsections (abfd)[shindex] = hdr; | |
2324 | /* We also treat this as a regular section, so that objcopy | |
2325 | can handle it. */ | |
bf67003b NC |
2326 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2327 | shindex); | |
2328 | goto success; | |
1b3a8575 | 2329 | } |
252b5132 | 2330 | |
1b3a8575 AM |
2331 | /* If the string table isn't one of the above, then treat it as a |
2332 | regular section. We need to scan all the headers to be sure, | |
2333 | just in case this strtab section appeared before the above. */ | |
2334 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2335 | { | |
2336 | unsigned int i, num_sec; | |
252b5132 | 2337 | |
1b3a8575 AM |
2338 | num_sec = elf_numsections (abfd); |
2339 | for (i = 1; i < num_sec; i++) | |
2340 | { | |
2341 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2342 | if (hdr2->sh_link == shindex) | |
2343 | { | |
933d961a JJ |
2344 | /* Prevent endless recursion on broken objects. */ |
2345 | if (i == shindex) | |
bf67003b | 2346 | goto fail; |
1b3a8575 | 2347 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 2348 | goto fail; |
1b3a8575 AM |
2349 | if (elf_onesymtab (abfd) == i) |
2350 | goto symtab_strtab; | |
2351 | if (elf_dynsymtab (abfd) == i) | |
2352 | goto dynsymtab_strtab; | |
2353 | } | |
2354 | } | |
2355 | } | |
bf67003b NC |
2356 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2357 | goto success; | |
252b5132 RH |
2358 | |
2359 | case SHT_REL: | |
2360 | case SHT_RELA: | |
2361 | /* *These* do a lot of work -- but build no sections! */ | |
2362 | { | |
2363 | asection *target_sect; | |
d4730f92 | 2364 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 2365 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 | 2366 | struct bfd_elf_section_data *esdt; |
252b5132 | 2367 | |
aa2ca951 JJ |
2368 | if (hdr->sh_entsize |
2369 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 2370 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 2371 | goto fail; |
a50b2160 | 2372 | |
03ae5f59 | 2373 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 2374 | if (hdr->sh_link >= num_sec) |
03ae5f59 | 2375 | { |
4eca0228 | 2376 | _bfd_error_handler |
695344c0 | 2377 | /* xgettext:c-format */ |
871b3ab2 | 2378 | (_("%pB: invalid link %u for reloc section %s (index %u)"), |
4eca0228 | 2379 | abfd, hdr->sh_link, name, shindex); |
bf67003b NC |
2380 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2381 | shindex); | |
2382 | goto success; | |
03ae5f59 ILT |
2383 | } |
2384 | ||
252b5132 RH |
2385 | /* For some incomprehensible reason Oracle distributes |
2386 | libraries for Solaris in which some of the objects have | |
2387 | bogus sh_link fields. It would be nice if we could just | |
2388 | reject them, but, unfortunately, some people need to use | |
2389 | them. We scan through the section headers; if we find only | |
2390 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
2391 | to it. I hope this doesn't break anything. |
2392 | ||
2393 | Don't do it on executable nor shared library. */ | |
2394 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
2395 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
2396 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
2397 | { | |
9ad5cbcf | 2398 | unsigned int scan; |
252b5132 RH |
2399 | int found; |
2400 | ||
2401 | found = 0; | |
9ad5cbcf | 2402 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2403 | { |
2404 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2405 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2406 | { | |
2407 | if (found != 0) | |
2408 | { | |
2409 | found = 0; | |
2410 | break; | |
2411 | } | |
2412 | found = scan; | |
2413 | } | |
2414 | } | |
2415 | if (found != 0) | |
2416 | hdr->sh_link = found; | |
2417 | } | |
2418 | ||
2419 | /* Get the symbol table. */ | |
1b3a8575 AM |
2420 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2421 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2422 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 2423 | goto fail; |
252b5132 | 2424 | |
a4bcd733 AM |
2425 | /* If this is an alloc section in an executable or shared |
2426 | library, or the reloc section does not use the main symbol | |
2427 | table we don't treat it as a reloc section. BFD can't | |
2428 | adequately represent such a section, so at least for now, | |
2429 | we don't try. We just present it as a normal section. We | |
2430 | also can't use it as a reloc section if it points to the | |
2431 | null section, an invalid section, another reloc section, or | |
2432 | its sh_link points to the null section. */ | |
2433 | if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0 | |
2434 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
83b89087 | 2435 | || hdr->sh_link == SHN_UNDEF |
a4bcd733 | 2436 | || hdr->sh_link != elf_onesymtab (abfd) |
185ef66d | 2437 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2438 | || hdr->sh_info >= num_sec |
2439 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2440 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2441 | { |
2442 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2443 | shindex); | |
2444 | goto success; | |
2445 | } | |
252b5132 RH |
2446 | |
2447 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2448 | goto fail; |
2449 | ||
252b5132 RH |
2450 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2451 | if (target_sect == NULL) | |
bf67003b | 2452 | goto fail; |
252b5132 | 2453 | |
d4730f92 BS |
2454 | esdt = elf_section_data (target_sect); |
2455 | if (hdr->sh_type == SHT_RELA) | |
2456 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2457 | else |
d4730f92 BS |
2458 | p_hdr = &esdt->rel.hdr; |
2459 | ||
a7ba3896 NC |
2460 | /* PR 17512: file: 0b4f81b7. |
2461 | Also see PR 24456, for a file which deliberately has two reloc | |
2462 | sections. */ | |
06614111 | 2463 | if (*p_hdr != NULL) |
a7ba3896 | 2464 | { |
a8e14f4c NC |
2465 | if (bed->init_secondary_reloc_section == NULL |
2466 | || ! bed->init_secondary_reloc_section (abfd, hdr, name, shindex)) | |
2467 | { | |
2468 | _bfd_error_handler | |
2469 | /* xgettext:c-format */ | |
2470 | (_("%pB: warning: secondary relocation section '%s' for section %pA found - ignoring"), | |
2471 | abfd, name, target_sect); | |
2472 | } | |
a7ba3896 NC |
2473 | goto success; |
2474 | } | |
a8e14f4c | 2475 | |
ef53be89 | 2476 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); |
d4730f92 | 2477 | if (hdr2 == NULL) |
bf67003b | 2478 | goto fail; |
252b5132 | 2479 | *hdr2 = *hdr; |
d4730f92 | 2480 | *p_hdr = hdr2; |
252b5132 | 2481 | elf_elfsections (abfd)[shindex] = hdr2; |
056bafd4 MR |
2482 | target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr) |
2483 | * bed->s->int_rels_per_ext_rel); | |
252b5132 RH |
2484 | target_sect->flags |= SEC_RELOC; |
2485 | target_sect->relocation = NULL; | |
2486 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2487 | /* In the section to which the relocations apply, mark whether |
2488 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2489 | if (hdr->sh_size != 0) |
d4730f92 BS |
2490 | { |
2491 | if (hdr->sh_type == SHT_RELA) | |
2492 | target_sect->use_rela_p = 1; | |
2493 | } | |
252b5132 | 2494 | abfd->flags |= HAS_RELOC; |
bf67003b | 2495 | goto success; |
252b5132 | 2496 | } |
252b5132 RH |
2497 | |
2498 | case SHT_GNU_verdef: | |
2499 | elf_dynverdef (abfd) = shindex; | |
2500 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2501 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2502 | goto success; | |
252b5132 RH |
2503 | |
2504 | case SHT_GNU_versym: | |
a50b2160 | 2505 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2506 | goto fail; |
2507 | ||
252b5132 RH |
2508 | elf_dynversym (abfd) = shindex; |
2509 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2510 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2511 | goto success; | |
252b5132 RH |
2512 | |
2513 | case SHT_GNU_verneed: | |
2514 | elf_dynverref (abfd) = shindex; | |
2515 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2516 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2517 | goto success; | |
252b5132 RH |
2518 | |
2519 | case SHT_SHLIB: | |
bf67003b | 2520 | goto success; |
252b5132 | 2521 | |
dbb410c3 | 2522 | case SHT_GROUP: |
44534af3 | 2523 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2524 | goto fail; |
2525 | ||
6dc132d9 | 2526 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2527 | goto fail; |
2528 | ||
bf67003b | 2529 | goto success; |
dbb410c3 | 2530 | |
252b5132 | 2531 | default: |
104d59d1 JM |
2532 | /* Possibly an attributes section. */ |
2533 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2534 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2535 | { | |
2536 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2537 | goto fail; |
104d59d1 | 2538 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2539 | goto success; |
104d59d1 JM |
2540 | } |
2541 | ||
252b5132 | 2542 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2543 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2544 | goto success; |
3eb70a79 L |
2545 | |
2546 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2547 | { | |
2548 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2549 | /* FIXME: How to properly handle allocated section reserved | |
2550 | for applications? */ | |
4eca0228 | 2551 | _bfd_error_handler |
695344c0 | 2552 | /* xgettext:c-format */ |
871b3ab2 | 2553 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2554 | abfd, hdr->sh_type, name); |
3eb70a79 | 2555 | else |
bf67003b NC |
2556 | { |
2557 | /* Allow sections reserved for applications. */ | |
2558 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2559 | shindex); | |
2560 | goto success; | |
2561 | } | |
3eb70a79 L |
2562 | } |
2563 | else if (hdr->sh_type >= SHT_LOPROC | |
2564 | && hdr->sh_type <= SHT_HIPROC) | |
2565 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2566 | _bfd_error_handler |
695344c0 | 2567 | /* xgettext:c-format */ |
871b3ab2 | 2568 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2569 | abfd, hdr->sh_type, name); |
3eb70a79 | 2570 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) |
ff15b240 NC |
2571 | { |
2572 | /* Unrecognised OS-specific sections. */ | |
2573 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2574 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2575 | required to correctly process the section and the file should |
ff15b240 | 2576 | be rejected with an error message. */ |
4eca0228 | 2577 | _bfd_error_handler |
695344c0 | 2578 | /* xgettext:c-format */ |
871b3ab2 | 2579 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2580 | abfd, hdr->sh_type, name); |
ff15b240 | 2581 | else |
bf67003b NC |
2582 | { |
2583 | /* Otherwise it should be processed. */ | |
2584 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2585 | goto success; | |
2586 | } | |
ff15b240 | 2587 | } |
3eb70a79 L |
2588 | else |
2589 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2590 | _bfd_error_handler |
695344c0 | 2591 | /* xgettext:c-format */ |
871b3ab2 | 2592 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2593 | abfd, hdr->sh_type, name); |
3eb70a79 | 2594 | |
bf67003b | 2595 | goto fail; |
252b5132 RH |
2596 | } |
2597 | ||
bf67003b NC |
2598 | fail: |
2599 | ret = FALSE; | |
2600 | success: | |
e5b470e2 | 2601 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2602 | sections_being_created [shindex] = FALSE; |
2603 | if (-- nesting == 0) | |
5a4b0ccc NC |
2604 | { |
2605 | sections_being_created = NULL; | |
2606 | sections_being_created_abfd = abfd; | |
2607 | } | |
bf67003b | 2608 | return ret; |
252b5132 RH |
2609 | } |
2610 | ||
87d72d41 | 2611 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2612 | |
87d72d41 AM |
2613 | Elf_Internal_Sym * |
2614 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2615 | bfd *abfd, | |
2616 | unsigned long r_symndx) | |
ec338859 | 2617 | { |
ec338859 AM |
2618 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2619 | ||
a5d1b3b5 AM |
2620 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2621 | { | |
2622 | Elf_Internal_Shdr *symtab_hdr; | |
2623 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2624 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2625 | |
a5d1b3b5 AM |
2626 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2627 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2628 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2629 | return NULL; |
9ad5cbcf | 2630 | |
a5d1b3b5 AM |
2631 | if (cache->abfd != abfd) |
2632 | { | |
2633 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2634 | cache->abfd = abfd; | |
2635 | } | |
2636 | cache->indx[ent] = r_symndx; | |
ec338859 | 2637 | } |
a5d1b3b5 | 2638 | |
87d72d41 | 2639 | return &cache->sym[ent]; |
ec338859 AM |
2640 | } |
2641 | ||
252b5132 RH |
2642 | /* Given an ELF section number, retrieve the corresponding BFD |
2643 | section. */ | |
2644 | ||
2645 | asection * | |
91d6fa6a | 2646 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2647 | { |
91d6fa6a | 2648 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2649 | return NULL; |
91d6fa6a | 2650 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2651 | } |
2652 | ||
b35d266b | 2653 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2654 | { |
0112cd26 | 2655 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 | 2656 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2657 | }; |
2658 | ||
b35d266b | 2659 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2660 | { |
0112cd26 | 2661 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
1ff6de03 | 2662 | { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 | 2663 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2664 | }; |
2665 | ||
b35d266b | 2666 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2667 | { |
07d6d2b8 AM |
2668 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2669 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2670 | /* There are more DWARF sections than these, but they needn't be added here |
2671 | unless you have to cope with broken compilers that don't emit section | |
2672 | attributes or you want to help the user writing assembler. */ | |
07d6d2b8 AM |
2673 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2674 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2675 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2676 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
0112cd26 | 2677 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 AM |
2678 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, |
2679 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2680 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2681 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2682 | }; |
2683 | ||
b35d266b | 2684 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2685 | { |
07d6d2b8 | 2686 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
6f9dbcd4 | 2687 | { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 | 2688 | { NULL, 0 , 0, 0, 0 } |
7f4d3958 L |
2689 | }; |
2690 | ||
b35d266b | 2691 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2692 | { |
0112cd26 | 2693 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2694 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
2695 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2696 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
0112cd26 NC |
2697 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, |
2698 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
07d6d2b8 AM |
2699 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, |
2700 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2701 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2702 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2703 | }; |
2704 | ||
b35d266b | 2705 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2706 | { |
07d6d2b8 AM |
2707 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2708 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2709 | }; |
2710 | ||
b35d266b | 2711 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2712 | { |
07d6d2b8 | 2713 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
6f9dbcd4 | 2714 | { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2715 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, |
2716 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2717 | }; |
2718 | ||
b35d266b | 2719 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2720 | { |
0112cd26 | 2721 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 | 2722 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2723 | }; |
2724 | ||
b35d266b | 2725 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2726 | { |
0112cd26 | 2727 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 AM |
2728 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, |
2729 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2730 | }; |
2731 | ||
b35d266b | 2732 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2733 | { |
6f9dbcd4 | 2734 | { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2735 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2736 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2737 | }; |
2738 | ||
b35d266b | 2739 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2740 | { |
0112cd26 NC |
2741 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2742 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
07d6d2b8 AM |
2743 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, |
2744 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2745 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2746 | }; |
2747 | ||
b35d266b | 2748 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2749 | { |
0112cd26 NC |
2750 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2751 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2752 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2753 | /* See struct bfd_elf_special_section declaration for the semantics of |
2754 | this special case where .prefix_length != strlen (.prefix). */ | |
2755 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
07d6d2b8 | 2756 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2757 | }; |
2758 | ||
b35d266b | 2759 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2760 | { |
07d6d2b8 AM |
2761 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2762 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
0112cd26 | 2763 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, |
07d6d2b8 | 2764 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2765 | }; |
2766 | ||
1b315056 CS |
2767 | static const struct bfd_elf_special_section special_sections_z[] = |
2768 | { | |
07d6d2b8 AM |
2769 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, |
2770 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
1b315056 CS |
2771 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, |
2772 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
07d6d2b8 | 2773 | { NULL, 0, 0, 0, 0 } |
1b315056 CS |
2774 | }; |
2775 | ||
e4c93b56 | 2776 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2777 | { |
7f4d3958 | 2778 | special_sections_b, /* 'b' */ |
98ece1b3 | 2779 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2780 | special_sections_d, /* 'd' */ |
2781 | NULL, /* 'e' */ | |
2782 | special_sections_f, /* 'f' */ | |
2783 | special_sections_g, /* 'g' */ | |
2784 | special_sections_h, /* 'h' */ | |
2785 | special_sections_i, /* 'i' */ | |
2786 | NULL, /* 'j' */ | |
2787 | NULL, /* 'k' */ | |
2788 | special_sections_l, /* 'l' */ | |
2789 | NULL, /* 'm' */ | |
2790 | special_sections_n, /* 'n' */ | |
2791 | NULL, /* 'o' */ | |
2792 | special_sections_p, /* 'p' */ | |
2793 | NULL, /* 'q' */ | |
2794 | special_sections_r, /* 'r' */ | |
2795 | special_sections_s, /* 's' */ | |
2796 | special_sections_t, /* 't' */ | |
1b315056 CS |
2797 | NULL, /* 'u' */ |
2798 | NULL, /* 'v' */ | |
2799 | NULL, /* 'w' */ | |
2800 | NULL, /* 'x' */ | |
2801 | NULL, /* 'y' */ | |
2802 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2803 | }; |
2804 | ||
551b43fd AM |
2805 | const struct bfd_elf_special_section * |
2806 | _bfd_elf_get_special_section (const char *name, | |
2807 | const struct bfd_elf_special_section *spec, | |
2808 | unsigned int rela) | |
2f89ff8d L |
2809 | { |
2810 | int i; | |
7f4d3958 | 2811 | int len; |
7f4d3958 | 2812 | |
551b43fd | 2813 | len = strlen (name); |
7f4d3958 | 2814 | |
551b43fd | 2815 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2816 | { |
2817 | int suffix_len; | |
551b43fd | 2818 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2819 | |
2820 | if (len < prefix_len) | |
2821 | continue; | |
551b43fd | 2822 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2823 | continue; |
2824 | ||
551b43fd | 2825 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2826 | if (suffix_len <= 0) |
2827 | { | |
2828 | if (name[prefix_len] != 0) | |
2829 | { | |
2830 | if (suffix_len == 0) | |
2831 | continue; | |
2832 | if (name[prefix_len] != '.' | |
2833 | && (suffix_len == -2 | |
551b43fd | 2834 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2835 | continue; |
2836 | } | |
2837 | } | |
2838 | else | |
2839 | { | |
2840 | if (len < prefix_len + suffix_len) | |
2841 | continue; | |
2842 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2843 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2844 | suffix_len) != 0) |
2845 | continue; | |
2846 | } | |
551b43fd | 2847 | return &spec[i]; |
7dcb9820 | 2848 | } |
2f89ff8d L |
2849 | |
2850 | return NULL; | |
2851 | } | |
2852 | ||
7dcb9820 | 2853 | const struct bfd_elf_special_section * |
29ef7005 | 2854 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2855 | { |
551b43fd AM |
2856 | int i; |
2857 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2858 | const struct elf_backend_data *bed; |
2f89ff8d L |
2859 | |
2860 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2861 | if (sec->name == NULL) |
2862 | return NULL; | |
2f89ff8d | 2863 | |
29ef7005 L |
2864 | bed = get_elf_backend_data (abfd); |
2865 | spec = bed->special_sections; | |
2866 | if (spec) | |
2867 | { | |
2868 | spec = _bfd_elf_get_special_section (sec->name, | |
2869 | bed->special_sections, | |
2870 | sec->use_rela_p); | |
2871 | if (spec != NULL) | |
2872 | return spec; | |
2873 | } | |
2874 | ||
551b43fd AM |
2875 | if (sec->name[0] != '.') |
2876 | return NULL; | |
2f89ff8d | 2877 | |
551b43fd | 2878 | i = sec->name[1] - 'b'; |
1b315056 | 2879 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2880 | return NULL; |
2881 | ||
2882 | spec = special_sections[i]; | |
2f89ff8d | 2883 | |
551b43fd AM |
2884 | if (spec == NULL) |
2885 | return NULL; | |
2886 | ||
2887 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2888 | } |
2889 | ||
b34976b6 | 2890 | bfd_boolean |
217aa764 | 2891 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2892 | { |
2893 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2894 | const struct elf_backend_data *bed; |
7dcb9820 | 2895 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2896 | |
f0abc2a1 AM |
2897 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2898 | if (sdata == NULL) | |
2899 | { | |
a50b1753 | 2900 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
07d6d2b8 | 2901 | sizeof (*sdata)); |
f0abc2a1 AM |
2902 | if (sdata == NULL) |
2903 | return FALSE; | |
217aa764 | 2904 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2905 | } |
bf572ba0 | 2906 | |
551b43fd AM |
2907 | /* Indicate whether or not this section should use RELA relocations. */ |
2908 | bed = get_elf_backend_data (abfd); | |
2909 | sec->use_rela_p = bed->default_use_rela_p; | |
2910 | ||
8c803a2d AM |
2911 | /* Set up ELF section type and flags for newly created sections, if |
2912 | there is an ABI mandated section. */ | |
2913 | ssect = (*bed->get_sec_type_attr) (abfd, sec); | |
2914 | if (ssect != NULL) | |
2f89ff8d | 2915 | { |
8c803a2d AM |
2916 | elf_section_type (sec) = ssect->type; |
2917 | elf_section_flags (sec) = ssect->attr; | |
2f89ff8d L |
2918 | } |
2919 | ||
f592407e | 2920 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2921 | } |
2922 | ||
2923 | /* Create a new bfd section from an ELF program header. | |
2924 | ||
2925 | Since program segments have no names, we generate a synthetic name | |
2926 | of the form segment<NUM>, where NUM is generally the index in the | |
2927 | program header table. For segments that are split (see below) we | |
2928 | generate the names segment<NUM>a and segment<NUM>b. | |
2929 | ||
2930 | Note that some program segments may have a file size that is different than | |
2931 | (less than) the memory size. All this means is that at execution the | |
2932 | system must allocate the amount of memory specified by the memory size, | |
2933 | but only initialize it with the first "file size" bytes read from the | |
2934 | file. This would occur for example, with program segments consisting | |
2935 | of combined data+bss. | |
2936 | ||
2937 | To handle the above situation, this routine generates TWO bfd sections | |
2938 | for the single program segment. The first has the length specified by | |
2939 | the file size of the segment, and the second has the length specified | |
2940 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2941 | into its initialized and uninitialized parts. |
252b5132 RH |
2942 | |
2943 | */ | |
2944 | ||
b34976b6 | 2945 | bfd_boolean |
217aa764 AM |
2946 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2947 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2948 | int hdr_index, |
a50b1753 | 2949 | const char *type_name) |
252b5132 RH |
2950 | { |
2951 | asection *newsect; | |
2952 | char *name; | |
2953 | char namebuf[64]; | |
d4c88bbb | 2954 | size_t len; |
252b5132 | 2955 | int split; |
502794d4 | 2956 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
252b5132 RH |
2957 | |
2958 | split = ((hdr->p_memsz > 0) | |
2959 | && (hdr->p_filesz > 0) | |
2960 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2961 | |
2962 | if (hdr->p_filesz > 0) | |
252b5132 | 2963 | { |
91d6fa6a | 2964 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2965 | len = strlen (namebuf) + 1; |
a50b1753 | 2966 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2967 | if (!name) |
2968 | return FALSE; | |
2969 | memcpy (name, namebuf, len); | |
2970 | newsect = bfd_make_section (abfd, name); | |
2971 | if (newsect == NULL) | |
2972 | return FALSE; | |
502794d4 CE |
2973 | newsect->vma = hdr->p_vaddr / opb; |
2974 | newsect->lma = hdr->p_paddr / opb; | |
d5191d0c AM |
2975 | newsect->size = hdr->p_filesz; |
2976 | newsect->filepos = hdr->p_offset; | |
2977 | newsect->flags |= SEC_HAS_CONTENTS; | |
2978 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2979 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2980 | { |
d5191d0c AM |
2981 | newsect->flags |= SEC_ALLOC; |
2982 | newsect->flags |= SEC_LOAD; | |
2983 | if (hdr->p_flags & PF_X) | |
2984 | { | |
2985 | /* FIXME: all we known is that it has execute PERMISSION, | |
2986 | may be data. */ | |
2987 | newsect->flags |= SEC_CODE; | |
2988 | } | |
2989 | } | |
2990 | if (!(hdr->p_flags & PF_W)) | |
2991 | { | |
2992 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2993 | } |
252b5132 RH |
2994 | } |
2995 | ||
d5191d0c | 2996 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2997 | { |
d5191d0c AM |
2998 | bfd_vma align; |
2999 | ||
91d6fa6a | 3000 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 3001 | len = strlen (namebuf) + 1; |
a50b1753 | 3002 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 3003 | if (!name) |
b34976b6 | 3004 | return FALSE; |
d4c88bbb | 3005 | memcpy (name, namebuf, len); |
252b5132 RH |
3006 | newsect = bfd_make_section (abfd, name); |
3007 | if (newsect == NULL) | |
b34976b6 | 3008 | return FALSE; |
502794d4 CE |
3009 | newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb; |
3010 | newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb; | |
eea6121a | 3011 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
3012 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
3013 | align = newsect->vma & -newsect->vma; | |
3014 | if (align == 0 || align > hdr->p_align) | |
3015 | align = hdr->p_align; | |
3016 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
3017 | if (hdr->p_type == PT_LOAD) |
3018 | { | |
d5191d0c AM |
3019 | /* Hack for gdb. Segments that have not been modified do |
3020 | not have their contents written to a core file, on the | |
3021 | assumption that a debugger can find the contents in the | |
3022 | executable. We flag this case by setting the fake | |
3023 | section size to zero. Note that "real" bss sections will | |
3024 | always have their contents dumped to the core file. */ | |
3025 | if (bfd_get_format (abfd) == bfd_core) | |
3026 | newsect->size = 0; | |
252b5132 RH |
3027 | newsect->flags |= SEC_ALLOC; |
3028 | if (hdr->p_flags & PF_X) | |
3029 | newsect->flags |= SEC_CODE; | |
3030 | } | |
3031 | if (!(hdr->p_flags & PF_W)) | |
3032 | newsect->flags |= SEC_READONLY; | |
3033 | } | |
3034 | ||
b34976b6 | 3035 | return TRUE; |
252b5132 RH |
3036 | } |
3037 | ||
864619bb KS |
3038 | static bfd_boolean |
3039 | _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset) | |
3040 | { | |
3041 | /* The return value is ignored. Build-ids are considered optional. */ | |
3042 | if (templ->xvec->flavour == bfd_target_elf_flavour) | |
3043 | return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id) | |
3044 | (templ, offset); | |
3045 | return FALSE; | |
3046 | } | |
3047 | ||
b34976b6 | 3048 | bfd_boolean |
91d6fa6a | 3049 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 3050 | { |
9c5bfbb7 | 3051 | const struct elf_backend_data *bed; |
20cfcaae NC |
3052 | |
3053 | switch (hdr->p_type) | |
3054 | { | |
3055 | case PT_NULL: | |
91d6fa6a | 3056 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
3057 | |
3058 | case PT_LOAD: | |
864619bb KS |
3059 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load")) |
3060 | return FALSE; | |
3061 | if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL) | |
3062 | _bfd_elf_core_find_build_id (abfd, hdr->p_offset); | |
3063 | return TRUE; | |
20cfcaae NC |
3064 | |
3065 | case PT_DYNAMIC: | |
91d6fa6a | 3066 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
3067 | |
3068 | case PT_INTERP: | |
91d6fa6a | 3069 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
3070 | |
3071 | case PT_NOTE: | |
91d6fa6a | 3072 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 3073 | return FALSE; |
276da9b3 L |
3074 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz, |
3075 | hdr->p_align)) | |
b34976b6 AM |
3076 | return FALSE; |
3077 | return TRUE; | |
20cfcaae NC |
3078 | |
3079 | case PT_SHLIB: | |
91d6fa6a | 3080 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
3081 | |
3082 | case PT_PHDR: | |
91d6fa6a | 3083 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 3084 | |
811072d8 | 3085 | case PT_GNU_EH_FRAME: |
91d6fa6a | 3086 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
3087 | "eh_frame_hdr"); |
3088 | ||
2b05f1b7 | 3089 | case PT_GNU_STACK: |
91d6fa6a | 3090 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 3091 | |
8c37241b | 3092 | case PT_GNU_RELRO: |
91d6fa6a | 3093 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 3094 | |
20cfcaae | 3095 | default: |
8c1acd09 | 3096 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 3097 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 3098 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
3099 | } |
3100 | } | |
3101 | ||
d4730f92 BS |
3102 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
3103 | REL or RELA. */ | |
3104 | ||
3105 | Elf_Internal_Shdr * | |
3106 | _bfd_elf_single_rel_hdr (asection *sec) | |
3107 | { | |
3108 | if (elf_section_data (sec)->rel.hdr) | |
3109 | { | |
3110 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
3111 | return elf_section_data (sec)->rel.hdr; | |
3112 | } | |
3113 | else | |
3114 | return elf_section_data (sec)->rela.hdr; | |
3115 | } | |
3116 | ||
3e19fb8f L |
3117 | static bfd_boolean |
3118 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
3119 | Elf_Internal_Shdr *rel_hdr, | |
3120 | const char *sec_name, | |
3121 | bfd_boolean use_rela_p) | |
3122 | { | |
3123 | char *name = (char *) bfd_alloc (abfd, | |
3124 | sizeof ".rela" + strlen (sec_name)); | |
3125 | if (name == NULL) | |
3126 | return FALSE; | |
3127 | ||
3128 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
3129 | rel_hdr->sh_name = | |
3130 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
3131 | FALSE); | |
3132 | if (rel_hdr->sh_name == (unsigned int) -1) | |
3133 | return FALSE; | |
3134 | ||
3135 | return TRUE; | |
3136 | } | |
3137 | ||
d4730f92 BS |
3138 | /* Allocate and initialize a section-header for a new reloc section, |
3139 | containing relocations against ASECT. It is stored in RELDATA. If | |
3140 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
3141 | relocations. */ | |
23bc299b | 3142 | |
5d13b3b3 | 3143 | static bfd_boolean |
217aa764 | 3144 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 3145 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 3146 | const char *sec_name, |
3e19fb8f L |
3147 | bfd_boolean use_rela_p, |
3148 | bfd_boolean delay_st_name_p) | |
23bc299b | 3149 | { |
d4730f92 | 3150 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 3151 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3152 | |
d4730f92 | 3153 | BFD_ASSERT (reldata->hdr == NULL); |
ef53be89 | 3154 | rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); |
d4730f92 | 3155 | reldata->hdr = rel_hdr; |
23bc299b | 3156 | |
3e19fb8f L |
3157 | if (delay_st_name_p) |
3158 | rel_hdr->sh_name = (unsigned int) -1; | |
3159 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
3160 | use_rela_p)) | |
b34976b6 | 3161 | return FALSE; |
23bc299b MM |
3162 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
3163 | rel_hdr->sh_entsize = (use_rela_p | |
3164 | ? bed->s->sizeof_rela | |
3165 | : bed->s->sizeof_rel); | |
72de5009 | 3166 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
28e07a05 | 3167 | rel_hdr->sh_flags = 0; |
23bc299b MM |
3168 | rel_hdr->sh_addr = 0; |
3169 | rel_hdr->sh_size = 0; | |
3170 | rel_hdr->sh_offset = 0; | |
3171 | ||
b34976b6 | 3172 | return TRUE; |
23bc299b MM |
3173 | } |
3174 | ||
94be91de JB |
3175 | /* Return the default section type based on the passed in section flags. */ |
3176 | ||
3177 | int | |
3178 | bfd_elf_get_default_section_type (flagword flags) | |
3179 | { | |
0e41bebb | 3180 | if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0 |
2e76e85a | 3181 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
3182 | return SHT_NOBITS; |
3183 | return SHT_PROGBITS; | |
3184 | } | |
3185 | ||
d4730f92 BS |
3186 | struct fake_section_arg |
3187 | { | |
3188 | struct bfd_link_info *link_info; | |
3189 | bfd_boolean failed; | |
3190 | }; | |
3191 | ||
252b5132 RH |
3192 | /* Set up an ELF internal section header for a section. */ |
3193 | ||
252b5132 | 3194 | static void |
d4730f92 | 3195 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 3196 | { |
d4730f92 | 3197 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 3198 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3199 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 3200 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 3201 | unsigned int sh_type; |
0ce398f1 | 3202 | const char *name = asect->name; |
3e19fb8f | 3203 | bfd_boolean delay_st_name_p = FALSE; |
233bf4f8 | 3204 | bfd_vma mask; |
252b5132 | 3205 | |
d4730f92 | 3206 | if (arg->failed) |
252b5132 RH |
3207 | { |
3208 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 3209 | loop. */ |
252b5132 RH |
3210 | return; |
3211 | } | |
3212 | ||
d4730f92 | 3213 | this_hdr = &esd->this_hdr; |
252b5132 | 3214 | |
f6fe1ccd | 3215 | if (arg->link_info) |
0ce398f1 | 3216 | { |
f6fe1ccd L |
3217 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
3218 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
3219 | && (asect->flags & SEC_DEBUGGING) | |
3220 | && name[1] == 'd' | |
3221 | && name[6] == '_') | |
3222 | { | |
3223 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
3224 | compressed. */ | |
3225 | asect->flags |= SEC_ELF_COMPRESS; | |
dd905818 | 3226 | /* If this section will be compressed, delay adding section |
3e19fb8f L |
3227 | name to section name section after it is compressed in |
3228 | _bfd_elf_assign_file_positions_for_non_load. */ | |
3229 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
3230 | } |
3231 | } | |
3232 | else if ((asect->flags & SEC_ELF_RENAME)) | |
3233 | { | |
3234 | /* objcopy: rename output DWARF debug section. */ | |
3235 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
3236 | { | |
3237 | /* When we decompress or compress with SHF_COMPRESSED, | |
3238 | convert section name from .zdebug_* to .debug_* if | |
3239 | needed. */ | |
3240 | if (name[1] == 'z') | |
3241 | { | |
3242 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
3243 | if (new_name == NULL) | |
3244 | { | |
3245 | arg->failed = TRUE; | |
3246 | return; | |
3247 | } | |
3248 | name = new_name; | |
3249 | } | |
3250 | } | |
3251 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 3252 | { |
f6fe1ccd L |
3253 | /* PR binutils/18087: Compression does not always make a |
3254 | section smaller. So only rename the section when | |
3255 | compression has actually taken place. If input section | |
3256 | name is .zdebug_*, we should never compress it again. */ | |
3257 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
3258 | if (new_name == NULL) |
3259 | { | |
3260 | arg->failed = TRUE; | |
3261 | return; | |
3262 | } | |
f6fe1ccd L |
3263 | BFD_ASSERT (name[1] != 'z'); |
3264 | name = new_name; | |
0ce398f1 L |
3265 | } |
3266 | } | |
3267 | ||
3e19fb8f L |
3268 | if (delay_st_name_p) |
3269 | this_hdr->sh_name = (unsigned int) -1; | |
3270 | else | |
252b5132 | 3271 | { |
3e19fb8f L |
3272 | this_hdr->sh_name |
3273 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
3274 | name, FALSE); | |
3275 | if (this_hdr->sh_name == (unsigned int) -1) | |
3276 | { | |
3277 | arg->failed = TRUE; | |
3278 | return; | |
3279 | } | |
252b5132 RH |
3280 | } |
3281 | ||
a4d8e49b | 3282 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
3283 | |
3284 | if ((asect->flags & SEC_ALLOC) != 0 | |
3285 | || asect->user_set_vma) | |
502794d4 | 3286 | this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect); |
252b5132 RH |
3287 | else |
3288 | this_hdr->sh_addr = 0; | |
3289 | ||
3290 | this_hdr->sh_offset = 0; | |
eea6121a | 3291 | this_hdr->sh_size = asect->size; |
252b5132 | 3292 | this_hdr->sh_link = 0; |
c86934ce NC |
3293 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
3294 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
3295 | { | |
4eca0228 | 3296 | _bfd_error_handler |
695344c0 | 3297 | /* xgettext:c-format */ |
9793eb77 | 3298 | (_("%pB: error: alignment power %d of section `%pA' is too big"), |
c08bb8dd | 3299 | abfd, asect->alignment_power, asect); |
c86934ce NC |
3300 | arg->failed = TRUE; |
3301 | return; | |
3302 | } | |
233bf4f8 AM |
3303 | /* Set sh_addralign to the highest power of two given by alignment |
3304 | consistent with the section VMA. Linker scripts can force VMA. */ | |
3305 | mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr; | |
3306 | this_hdr->sh_addralign = mask & -mask; | |
252b5132 RH |
3307 | /* The sh_entsize and sh_info fields may have been set already by |
3308 | copy_private_section_data. */ | |
3309 | ||
3310 | this_hdr->bfd_section = asect; | |
3311 | this_hdr->contents = NULL; | |
3312 | ||
3cddba1e L |
3313 | /* If the section type is unspecified, we set it based on |
3314 | asect->flags. */ | |
98ece1b3 AM |
3315 | if ((asect->flags & SEC_GROUP) != 0) |
3316 | sh_type = SHT_GROUP; | |
98ece1b3 | 3317 | else |
94be91de | 3318 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 3319 | |
3cddba1e | 3320 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
3321 | this_hdr->sh_type = sh_type; |
3322 | else if (this_hdr->sh_type == SHT_NOBITS | |
3323 | && sh_type == SHT_PROGBITS | |
3324 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 3325 | { |
98ece1b3 AM |
3326 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
3327 | allow the link to proceed. This can happen when users link | |
3328 | non-bss input sections to bss output sections, or emit data | |
3329 | to a bss output section via a linker script. */ | |
4eca0228 | 3330 | _bfd_error_handler |
871b3ab2 | 3331 | (_("warning: section `%pA' type changed to PROGBITS"), asect); |
98ece1b3 | 3332 | this_hdr->sh_type = sh_type; |
3cddba1e L |
3333 | } |
3334 | ||
2f89ff8d | 3335 | switch (this_hdr->sh_type) |
252b5132 | 3336 | { |
2f89ff8d | 3337 | default: |
2f89ff8d L |
3338 | break; |
3339 | ||
3340 | case SHT_STRTAB: | |
2f89ff8d L |
3341 | case SHT_NOTE: |
3342 | case SHT_NOBITS: | |
3343 | case SHT_PROGBITS: | |
3344 | break; | |
606851fb AM |
3345 | |
3346 | case SHT_INIT_ARRAY: | |
3347 | case SHT_FINI_ARRAY: | |
3348 | case SHT_PREINIT_ARRAY: | |
3349 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
3350 | break; | |
2f89ff8d L |
3351 | |
3352 | case SHT_HASH: | |
c7ac6ff8 | 3353 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 3354 | break; |
5de3bf90 | 3355 | |
2f89ff8d | 3356 | case SHT_DYNSYM: |
252b5132 | 3357 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
3358 | break; |
3359 | ||
3360 | case SHT_DYNAMIC: | |
252b5132 | 3361 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
3362 | break; |
3363 | ||
3364 | case SHT_RELA: | |
3365 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
3366 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
3367 | break; | |
3368 | ||
3369 | case SHT_REL: | |
3370 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
3371 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
3372 | break; | |
3373 | ||
3374 | case SHT_GNU_versym: | |
252b5132 | 3375 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
3376 | break; |
3377 | ||
3378 | case SHT_GNU_verdef: | |
252b5132 RH |
3379 | this_hdr->sh_entsize = 0; |
3380 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3381 | cverdefs. The linker will set cverdefs, but sh_info will be |
3382 | zero. */ | |
252b5132 RH |
3383 | if (this_hdr->sh_info == 0) |
3384 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
3385 | else | |
3386 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
3387 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
3388 | break; |
3389 | ||
3390 | case SHT_GNU_verneed: | |
252b5132 RH |
3391 | this_hdr->sh_entsize = 0; |
3392 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3393 | cverrefs. The linker will set cverrefs, but sh_info will be |
3394 | zero. */ | |
252b5132 RH |
3395 | if (this_hdr->sh_info == 0) |
3396 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
3397 | else | |
3398 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
3399 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
3400 | break; |
3401 | ||
3402 | case SHT_GROUP: | |
1783205a | 3403 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 3404 | break; |
fdc90cb4 JJ |
3405 | |
3406 | case SHT_GNU_HASH: | |
3407 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
3408 | break; | |
dbb410c3 | 3409 | } |
252b5132 RH |
3410 | |
3411 | if ((asect->flags & SEC_ALLOC) != 0) | |
3412 | this_hdr->sh_flags |= SHF_ALLOC; | |
3413 | if ((asect->flags & SEC_READONLY) == 0) | |
3414 | this_hdr->sh_flags |= SHF_WRITE; | |
3415 | if ((asect->flags & SEC_CODE) != 0) | |
3416 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
3417 | if ((asect->flags & SEC_MERGE) != 0) |
3418 | { | |
3419 | this_hdr->sh_flags |= SHF_MERGE; | |
3420 | this_hdr->sh_entsize = asect->entsize; | |
f5fa8ca2 | 3421 | } |
84865015 NC |
3422 | if ((asect->flags & SEC_STRINGS) != 0) |
3423 | this_hdr->sh_flags |= SHF_STRINGS; | |
1126897b | 3424 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 3425 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 3426 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
3427 | { |
3428 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
3429 | if (asect->size == 0 |
3430 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 3431 | { |
3a800eb9 | 3432 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 3433 | |
704afa60 | 3434 | this_hdr->sh_size = 0; |
3a800eb9 AM |
3435 | if (o != NULL) |
3436 | { | |
704afa60 | 3437 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
3438 | if (this_hdr->sh_size != 0) |
3439 | this_hdr->sh_type = SHT_NOBITS; | |
3440 | } | |
704afa60 JJ |
3441 | } |
3442 | } | |
18ae9cc1 L |
3443 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3444 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3445 | |
d4730f92 BS |
3446 | /* If the section has relocs, set up a section header for the |
3447 | SHT_REL[A] section. If two relocation sections are required for | |
3448 | this section, it is up to the processor-specific back-end to | |
3449 | create the other. */ | |
3450 | if ((asect->flags & SEC_RELOC) != 0) | |
3451 | { | |
3452 | /* When doing a relocatable link, create both REL and RELA sections if | |
3453 | needed. */ | |
3454 | if (arg->link_info | |
3455 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3456 | && esd->rel.count + esd->rela.count > 0 | |
0e1862bb L |
3457 | && (bfd_link_relocatable (arg->link_info) |
3458 | || arg->link_info->emitrelocations)) | |
d4730f92 BS |
3459 | { |
3460 | if (esd->rel.count && esd->rel.hdr == NULL | |
28e07a05 | 3461 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, |
db4677b8 | 3462 | FALSE, delay_st_name_p)) |
d4730f92 BS |
3463 | { |
3464 | arg->failed = TRUE; | |
3465 | return; | |
3466 | } | |
3467 | if (esd->rela.count && esd->rela.hdr == NULL | |
28e07a05 | 3468 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, |
db4677b8 | 3469 | TRUE, delay_st_name_p)) |
d4730f92 BS |
3470 | { |
3471 | arg->failed = TRUE; | |
3472 | return; | |
3473 | } | |
3474 | } | |
3475 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3476 | (asect->use_rela_p | |
3477 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3478 | name, |
3e19fb8f L |
3479 | asect->use_rela_p, |
3480 | delay_st_name_p)) | |
db4677b8 | 3481 | { |
d4730f92 | 3482 | arg->failed = TRUE; |
db4677b8 AM |
3483 | return; |
3484 | } | |
d4730f92 BS |
3485 | } |
3486 | ||
252b5132 | 3487 | /* Check for processor-specific section types. */ |
0414f35b | 3488 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3489 | if (bed->elf_backend_fake_sections |
3490 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
db4677b8 AM |
3491 | { |
3492 | arg->failed = TRUE; | |
3493 | return; | |
3494 | } | |
252b5132 | 3495 | |
42bb2e33 | 3496 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3497 | { |
3498 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3499 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3500 | this_hdr->sh_type = sh_type; |
3501 | } | |
252b5132 RH |
3502 | } |
3503 | ||
bcacc0f5 AM |
3504 | /* Fill in the contents of a SHT_GROUP section. Called from |
3505 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3506 | when ELF targets use the generic linker, ld. Called for ld -r | |
3507 | from bfd_elf_final_link. */ | |
dbb410c3 | 3508 | |
1126897b | 3509 | void |
217aa764 | 3510 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3511 | { |
a50b1753 | 3512 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3513 | asection *elt, *first; |
dbb410c3 | 3514 | unsigned char *loc; |
b34976b6 | 3515 | bfd_boolean gas; |
dbb410c3 | 3516 | |
7e4111ad L |
3517 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3518 | elfxx-ia64.c. */ | |
ce5aecf8 AM |
3519 | if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP |
3520 | || sec->size == 0 | |
dbb410c3 AM |
3521 | || *failedptr) |
3522 | return; | |
3523 | ||
bcacc0f5 AM |
3524 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3525 | { | |
3526 | unsigned long symindx = 0; | |
3527 | ||
3528 | /* elf_group_id will have been set up by objcopy and the | |
3529 | generic linker. */ | |
3530 | if (elf_group_id (sec) != NULL) | |
3531 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3532 | |
bcacc0f5 AM |
3533 | if (symindx == 0) |
3534 | { | |
3535 | /* If called from the assembler, swap_out_syms will have set up | |
3536 | elf_section_syms. */ | |
3537 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3538 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3539 | } | |
3540 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3541 | } | |
3542 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3543 | { |
bcacc0f5 AM |
3544 | /* The ELF backend linker sets sh_info to -2 when the group |
3545 | signature symbol is global, and thus the index can't be | |
3546 | set until all local symbols are output. */ | |
53720c49 AM |
3547 | asection *igroup; |
3548 | struct bfd_elf_section_data *sec_data; | |
3549 | unsigned long symndx; | |
3550 | unsigned long extsymoff; | |
bcacc0f5 AM |
3551 | struct elf_link_hash_entry *h; |
3552 | ||
53720c49 AM |
3553 | /* The point of this little dance to the first SHF_GROUP section |
3554 | then back to the SHT_GROUP section is that this gets us to | |
3555 | the SHT_GROUP in the input object. */ | |
3556 | igroup = elf_sec_group (elf_next_in_group (sec)); | |
3557 | sec_data = elf_section_data (igroup); | |
3558 | symndx = sec_data->this_hdr.sh_info; | |
3559 | extsymoff = 0; | |
bcacc0f5 AM |
3560 | if (!elf_bad_symtab (igroup->owner)) |
3561 | { | |
3562 | Elf_Internal_Shdr *symtab_hdr; | |
3563 | ||
3564 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3565 | extsymoff = symtab_hdr->sh_info; | |
3566 | } | |
3567 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3568 | while (h->root.type == bfd_link_hash_indirect | |
3569 | || h->root.type == bfd_link_hash_warning) | |
3570 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3571 | ||
3572 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3573 | } |
dbb410c3 | 3574 | |
1126897b | 3575 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3576 | gas = TRUE; |
dbb410c3 AM |
3577 | if (sec->contents == NULL) |
3578 | { | |
b34976b6 | 3579 | gas = FALSE; |
a50b1753 | 3580 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3581 | |
3582 | /* Arrange for the section to be written out. */ | |
3583 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3584 | if (sec->contents == NULL) |
3585 | { | |
b34976b6 | 3586 | *failedptr = TRUE; |
dbb410c3 AM |
3587 | return; |
3588 | } | |
3589 | } | |
3590 | ||
eea6121a | 3591 | loc = sec->contents + sec->size; |
dbb410c3 | 3592 | |
9dce4196 AM |
3593 | /* Get the pointer to the first section in the group that gas |
3594 | squirreled away here. objcopy arranges for this to be set to the | |
3595 | start of the input section group. */ | |
3596 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3597 | |
3598 | /* First element is a flag word. Rest of section is elf section | |
3599 | indices for all the sections of the group. Write them backwards | |
3600 | just to keep the group in the same order as given in .section | |
3601 | directives, not that it matters. */ | |
3602 | while (elt != NULL) | |
3603 | { | |
9dce4196 | 3604 | asection *s; |
9dce4196 | 3605 | |
9dce4196 | 3606 | s = elt; |
415f38a6 AM |
3607 | if (!gas) |
3608 | s = s->output_section; | |
3609 | if (s != NULL | |
3610 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3611 | { |
db4677b8 | 3612 | struct bfd_elf_section_data *elf_sec = elf_section_data (s); |
28e07a05 AM |
3613 | struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt); |
3614 | ||
3615 | if (elf_sec->rel.hdr != NULL | |
3616 | && (gas | |
3617 | || (input_elf_sec->rel.hdr != NULL | |
3618 | && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)) | |
db4677b8 | 3619 | { |
28e07a05 | 3620 | elf_sec->rel.hdr->sh_flags |= SHF_GROUP; |
db4677b8 AM |
3621 | loc -= 4; |
3622 | H_PUT_32 (abfd, elf_sec->rel.idx, loc); | |
3623 | } | |
28e07a05 AM |
3624 | if (elf_sec->rela.hdr != NULL |
3625 | && (gas | |
3626 | || (input_elf_sec->rela.hdr != NULL | |
3627 | && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)) | |
db4677b8 | 3628 | { |
28e07a05 | 3629 | elf_sec->rela.hdr->sh_flags |= SHF_GROUP; |
db4677b8 AM |
3630 | loc -= 4; |
3631 | H_PUT_32 (abfd, elf_sec->rela.idx, loc); | |
3632 | } | |
01e1a5bc | 3633 | loc -= 4; |
db4677b8 | 3634 | H_PUT_32 (abfd, elf_sec->this_idx, loc); |
01e1a5bc | 3635 | } |
945906ff | 3636 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3637 | if (elt == first) |
3638 | break; | |
dbb410c3 AM |
3639 | } |
3640 | ||
7bdf4127 AB |
3641 | loc -= 4; |
3642 | BFD_ASSERT (loc == sec->contents); | |
dbb410c3 | 3643 | |
9dce4196 | 3644 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3645 | } |
3646 | ||
bce964aa AM |
3647 | /* Given NAME, the name of a relocation section stripped of its |
3648 | .rel/.rela prefix, return the section in ABFD to which the | |
3649 | relocations apply. */ | |
bd53a53a L |
3650 | |
3651 | asection * | |
bce964aa AM |
3652 | _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name) |
3653 | { | |
3654 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3655 | section likely apply to .got.plt or .got section. */ | |
3656 | if (get_elf_backend_data (abfd)->want_got_plt | |
3657 | && strcmp (name, ".plt") == 0) | |
3658 | { | |
3659 | asection *sec; | |
3660 | ||
3661 | name = ".got.plt"; | |
3662 | sec = bfd_get_section_by_name (abfd, name); | |
3663 | if (sec != NULL) | |
3664 | return sec; | |
3665 | name = ".got"; | |
3666 | } | |
3667 | ||
3668 | return bfd_get_section_by_name (abfd, name); | |
3669 | } | |
3670 | ||
3671 | /* Return the section to which RELOC_SEC applies. */ | |
3672 | ||
3673 | static asection * | |
3674 | elf_get_reloc_section (asection *reloc_sec) | |
bd53a53a L |
3675 | { |
3676 | const char *name; | |
3677 | unsigned int type; | |
3678 | bfd *abfd; | |
bce964aa | 3679 | const struct elf_backend_data *bed; |
bd53a53a L |
3680 | |
3681 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3682 | if (type != SHT_REL && type != SHT_RELA) | |
3683 | return NULL; | |
3684 | ||
3685 | /* We look up the section the relocs apply to by name. */ | |
3686 | name = reloc_sec->name; | |
bce964aa AM |
3687 | if (strncmp (name, ".rel", 4) != 0) |
3688 | return NULL; | |
3689 | name += 4; | |
3690 | if (type == SHT_RELA && *name++ != 'a') | |
3691 | return NULL; | |
bd53a53a | 3692 | |
bd53a53a | 3693 | abfd = reloc_sec->owner; |
bce964aa AM |
3694 | bed = get_elf_backend_data (abfd); |
3695 | return bed->get_reloc_section (abfd, name); | |
bd53a53a L |
3696 | } |
3697 | ||
252b5132 RH |
3698 | /* Assign all ELF section numbers. The dummy first section is handled here |
3699 | too. The link/info pointers for the standard section types are filled | |
3700 | in here too, while we're at it. */ | |
3701 | ||
b34976b6 | 3702 | static bfd_boolean |
da9f89d4 | 3703 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3704 | { |
3705 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3706 | asection *sec; | |
3e19fb8f | 3707 | unsigned int section_number; |
252b5132 | 3708 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3709 | struct bfd_elf_section_data *d; |
3516e984 | 3710 | bfd_boolean need_symtab; |
446f7ed5 | 3711 | size_t amt; |
252b5132 RH |
3712 | |
3713 | section_number = 1; | |
3714 | ||
2b0f7ef9 JJ |
3715 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3716 | ||
da9f89d4 | 3717 | /* SHT_GROUP sections are in relocatable files only. */ |
7bdf4127 | 3718 | if (link_info == NULL || !link_info->resolve_section_groups) |
252b5132 | 3719 | { |
ef53be89 | 3720 | size_t reloc_count = 0; |
14f2c699 | 3721 | |
da9f89d4 | 3722 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3723 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3724 | { |
5daa8fe7 | 3725 | d = elf_section_data (sec); |
da9f89d4 L |
3726 | |
3727 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3728 | { |
5daa8fe7 | 3729 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3730 | { |
3731 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3732 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3733 | abfd->section_count--; |
da9f89d4 | 3734 | } |
08a40648 | 3735 | else |
4fbb74a6 | 3736 | d->this_idx = section_number++; |
da9f89d4 | 3737 | } |
14f2c699 L |
3738 | |
3739 | /* Count relocations. */ | |
3740 | reloc_count += sec->reloc_count; | |
47cc2cf5 | 3741 | } |
14f2c699 L |
3742 | |
3743 | /* Clear HAS_RELOC if there are no relocations. */ | |
3744 | if (reloc_count == 0) | |
3745 | abfd->flags &= ~HAS_RELOC; | |
47cc2cf5 PB |
3746 | } |
3747 | ||
3748 | for (sec = abfd->sections; sec; sec = sec->next) | |
3749 | { | |
3750 | d = elf_section_data (sec); | |
3751 | ||
3752 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3753 | d->this_idx = section_number++; |
3e19fb8f L |
3754 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3755 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3756 | if (d->rel.hdr) |
2b0f7ef9 | 3757 | { |
d4730f92 | 3758 | d->rel.idx = section_number++; |
3e19fb8f L |
3759 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3760 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3761 | } |
d4730f92 BS |
3762 | else |
3763 | d->rel.idx = 0; | |
23bc299b | 3764 | |
d4730f92 | 3765 | if (d->rela.hdr) |
2b0f7ef9 | 3766 | { |
d4730f92 | 3767 | d->rela.idx = section_number++; |
3e19fb8f L |
3768 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
3769 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3770 | } |
23bc299b | 3771 | else |
d4730f92 | 3772 | d->rela.idx = 0; |
252b5132 RH |
3773 | } |
3774 | ||
3516e984 L |
3775 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3776 | || (link_info == NULL | |
3777 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3778 | == HAS_RELOC))); | |
3779 | if (need_symtab) | |
252b5132 | 3780 | { |
12bd6957 | 3781 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3782 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3783 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3784 | { |
7a6e0d89 | 3785 | elf_section_list *entry; |
6a40cf0c NC |
3786 | |
3787 | BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); | |
3788 | ||
7a6e0d89 | 3789 | entry = bfd_zalloc (abfd, sizeof (*entry)); |
6a40cf0c NC |
3790 | entry->ndx = section_number++; |
3791 | elf_symtab_shndx_list (abfd) = entry; | |
3792 | entry->hdr.sh_name | |
9ad5cbcf | 3793 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 3794 | ".symtab_shndx", FALSE); |
6a40cf0c | 3795 | if (entry->hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3796 | return FALSE; |
9ad5cbcf | 3797 | } |
12bd6957 | 3798 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3799 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3800 | } |
3801 | ||
dd905818 NC |
3802 | elf_shstrtab_sec (abfd) = section_number++; |
3803 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); | |
3804 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); | |
3805 | ||
1c52a645 L |
3806 | if (section_number >= SHN_LORESERVE) |
3807 | { | |
695344c0 | 3808 | /* xgettext:c-format */ |
871b3ab2 | 3809 | _bfd_error_handler (_("%pB: too many sections: %u"), |
1c52a645 L |
3810 | abfd, section_number); |
3811 | return FALSE; | |
3812 | } | |
3813 | ||
9ad5cbcf | 3814 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3815 | elf_elfheader (abfd)->e_shnum = section_number; |
3816 | ||
3817 | /* Set up the list of section header pointers, in agreement with the | |
3818 | indices. */ | |
446f7ed5 AM |
3819 | amt = section_number * sizeof (Elf_Internal_Shdr *); |
3820 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt); | |
252b5132 | 3821 | if (i_shdrp == NULL) |
b34976b6 | 3822 | return FALSE; |
252b5132 | 3823 | |
a50b1753 | 3824 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
07d6d2b8 | 3825 | sizeof (Elf_Internal_Shdr)); |
252b5132 RH |
3826 | if (i_shdrp[0] == NULL) |
3827 | { | |
3828 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3829 | return FALSE; |
252b5132 | 3830 | } |
252b5132 RH |
3831 | |
3832 | elf_elfsections (abfd) = i_shdrp; | |
3833 | ||
12bd6957 | 3834 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3835 | if (need_symtab) |
252b5132 | 3836 | { |
12bd6957 | 3837 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3838 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3839 | { |
6a40cf0c NC |
3840 | elf_section_list * entry = elf_symtab_shndx_list (abfd); |
3841 | BFD_ASSERT (entry != NULL); | |
3842 | i_shdrp[entry->ndx] = & entry->hdr; | |
3843 | entry->hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3844 | } |
12bd6957 AM |
3845 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3846 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3847 | } |
38ce5b11 | 3848 | |
252b5132 RH |
3849 | for (sec = abfd->sections; sec; sec = sec->next) |
3850 | { | |
252b5132 | 3851 | asection *s; |
252b5132 | 3852 | |
91d6fa6a NC |
3853 | d = elf_section_data (sec); |
3854 | ||
252b5132 | 3855 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3856 | if (d->rel.idx != 0) |
3857 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3858 | if (d->rela.idx != 0) | |
3859 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3860 | |
3861 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3862 | ||
3863 | /* sh_link of a reloc section is the section index of the symbol | |
3864 | table. sh_info is the section index of the section to which | |
3865 | the relocation entries apply. */ | |
d4730f92 | 3866 | if (d->rel.idx != 0) |
252b5132 | 3867 | { |
12bd6957 | 3868 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3869 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3870 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3871 | } |
d4730f92 | 3872 | if (d->rela.idx != 0) |
23bc299b | 3873 | { |
12bd6957 | 3874 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3875 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3876 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3877 | } |
252b5132 | 3878 | |
38ce5b11 L |
3879 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3880 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3881 | { | |
3882 | s = elf_linked_to_section (sec); | |
3883 | if (s) | |
38ce5b11 | 3884 | { |
f2876037 | 3885 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3886 | if (link_info != NULL) |
38ce5b11 | 3887 | { |
f2876037 | 3888 | /* Check discarded linkonce section. */ |
dbaa2011 | 3889 | if (discarded_section (s)) |
38ce5b11 | 3890 | { |
ccd2ec6a | 3891 | asection *kept; |
4eca0228 | 3892 | _bfd_error_handler |
695344c0 | 3893 | /* xgettext:c-format */ |
871b3ab2 AM |
3894 | (_("%pB: sh_link of section `%pA' points to" |
3895 | " discarded section `%pA' of `%pB'"), | |
ccd2ec6a L |
3896 | abfd, d->this_hdr.bfd_section, |
3897 | s, s->owner); | |
3898 | /* Point to the kept section if it has the same | |
3899 | size as the discarded one. */ | |
c0f00686 | 3900 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3901 | if (kept == NULL) |
185d09ad | 3902 | { |
ccd2ec6a L |
3903 | bfd_set_error (bfd_error_bad_value); |
3904 | return FALSE; | |
185d09ad | 3905 | } |
ccd2ec6a | 3906 | s = kept; |
38ce5b11 | 3907 | } |
e424ecc8 | 3908 | |
ccd2ec6a L |
3909 | s = s->output_section; |
3910 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3911 | } |
f2876037 L |
3912 | else |
3913 | { | |
3914 | /* Handle objcopy. */ | |
3915 | if (s->output_section == NULL) | |
3916 | { | |
4eca0228 | 3917 | _bfd_error_handler |
695344c0 | 3918 | /* xgettext:c-format */ |
871b3ab2 AM |
3919 | (_("%pB: sh_link of section `%pA' points to" |
3920 | " removed section `%pA' of `%pB'"), | |
f2876037 L |
3921 | abfd, d->this_hdr.bfd_section, s, s->owner); |
3922 | bfd_set_error (bfd_error_bad_value); | |
3923 | return FALSE; | |
3924 | } | |
3925 | s = s->output_section; | |
3926 | } | |
ccd2ec6a L |
3927 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3928 | } | |
3929 | else | |
3930 | { | |
3931 | /* PR 290: | |
3932 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3933 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3934 | sh_info fields. Hence we could get the situation | |
08a40648 | 3935 | where s is NULL. */ |
ccd2ec6a L |
3936 | const struct elf_backend_data *bed |
3937 | = get_elf_backend_data (abfd); | |
3938 | if (bed->link_order_error_handler) | |
3939 | bed->link_order_error_handler | |
695344c0 | 3940 | /* xgettext:c-format */ |
871b3ab2 | 3941 | (_("%pB: warning: sh_link not set for section `%pA'"), |
ccd2ec6a | 3942 | abfd, sec); |
38ce5b11 L |
3943 | } |
3944 | } | |
3945 | ||
252b5132 RH |
3946 | switch (d->this_hdr.sh_type) |
3947 | { | |
3948 | case SHT_REL: | |
3949 | case SHT_RELA: | |
3950 | /* A reloc section which we are treating as a normal BFD | |
3951 | section. sh_link is the section index of the symbol | |
3952 | table. sh_info is the section index of the section to | |
3953 | which the relocation entries apply. We assume that an | |
3954 | allocated reloc section uses the dynamic symbol table. | |
3955 | FIXME: How can we be sure? */ | |
3956 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3957 | if (s != NULL) | |
3958 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3959 | ||
bce964aa | 3960 | s = elf_get_reloc_section (sec); |
252b5132 | 3961 | if (s != NULL) |
9ef5d938 L |
3962 | { |
3963 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3964 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3965 | } | |
252b5132 RH |
3966 | break; |
3967 | ||
3968 | case SHT_STRTAB: | |
3969 | /* We assume that a section named .stab*str is a stabs | |
3970 | string section. We look for a section with the same name | |
3971 | but without the trailing ``str'', and set its sh_link | |
3972 | field to point to this section. */ | |
0112cd26 | 3973 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3974 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3975 | { | |
3976 | size_t len; | |
3977 | char *alc; | |
3978 | ||
3979 | len = strlen (sec->name); | |
a50b1753 | 3980 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3981 | if (alc == NULL) |
b34976b6 | 3982 | return FALSE; |
d4c88bbb | 3983 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3984 | alc[len - 3] = '\0'; |
3985 | s = bfd_get_section_by_name (abfd, alc); | |
3986 | free (alc); | |
3987 | if (s != NULL) | |
3988 | { | |
3989 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3990 | ||
3991 | /* This is a .stab section. */ | |
0594c12d AM |
3992 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3993 | elf_section_data (s)->this_hdr.sh_entsize | |
3994 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3995 | } |
3996 | } | |
3997 | break; | |
3998 | ||
3999 | case SHT_DYNAMIC: | |
4000 | case SHT_DYNSYM: | |
4001 | case SHT_GNU_verneed: | |
4002 | case SHT_GNU_verdef: | |
4003 | /* sh_link is the section header index of the string table | |
4004 | used for the dynamic entries, or the symbol table, or the | |
4005 | version strings. */ | |
4006 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
4007 | if (s != NULL) | |
4008 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
4009 | break; | |
4010 | ||
7f1204bb JJ |
4011 | case SHT_GNU_LIBLIST: |
4012 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
4013 | list used for the dynamic entries, or the symbol table, or |
4014 | the version strings. */ | |
7f1204bb JJ |
4015 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
4016 | ? ".dynstr" : ".gnu.libstr"); | |
4017 | if (s != NULL) | |
4018 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
4019 | break; | |
4020 | ||
252b5132 | 4021 | case SHT_HASH: |
fdc90cb4 | 4022 | case SHT_GNU_HASH: |
252b5132 RH |
4023 | case SHT_GNU_versym: |
4024 | /* sh_link is the section header index of the symbol table | |
4025 | this hash table or version table is for. */ | |
4026 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
4027 | if (s != NULL) | |
4028 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
4029 | break; | |
dbb410c3 AM |
4030 | |
4031 | case SHT_GROUP: | |
12bd6957 | 4032 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
4033 | } |
4034 | } | |
4035 | ||
3e19fb8f L |
4036 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
4037 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
4038 | debug section name from .debug_* to .zdebug_* if needed. */ | |
4039 | ||
b34976b6 | 4040 | return TRUE; |
252b5132 RH |
4041 | } |
4042 | ||
5372391b | 4043 | static bfd_boolean |
217aa764 | 4044 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
4045 | { |
4046 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 4047 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
4048 | if (bed->elf_backend_sym_is_global) |
4049 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 4050 | |
e47bf690 | 4051 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
e6f7f6d1 AM |
4052 | || bfd_is_und_section (bfd_asymbol_section (sym)) |
4053 | || bfd_is_com_section (bfd_asymbol_section (sym))); | |
252b5132 RH |
4054 | } |
4055 | ||
76359541 TP |
4056 | /* Filter global symbols of ABFD to include in the import library. All |
4057 | SYMCOUNT symbols of ABFD can be examined from their pointers in | |
4058 | SYMS. Pointers of symbols to keep should be stored contiguously at | |
4059 | the beginning of that array. | |
4060 | ||
4061 | Returns the number of symbols to keep. */ | |
4062 | ||
4063 | unsigned int | |
4064 | _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, | |
4065 | asymbol **syms, long symcount) | |
4066 | { | |
4067 | long src_count, dst_count = 0; | |
4068 | ||
4069 | for (src_count = 0; src_count < symcount; src_count++) | |
4070 | { | |
4071 | asymbol *sym = syms[src_count]; | |
4072 | char *name = (char *) bfd_asymbol_name (sym); | |
4073 | struct bfd_link_hash_entry *h; | |
4074 | ||
4075 | if (!sym_is_global (abfd, sym)) | |
4076 | continue; | |
4077 | ||
4078 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE); | |
5df1bc57 AM |
4079 | if (h == NULL) |
4080 | continue; | |
76359541 TP |
4081 | if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) |
4082 | continue; | |
76359541 TP |
4083 | if (h->linker_def || h->ldscript_def) |
4084 | continue; | |
4085 | ||
4086 | syms[dst_count++] = sym; | |
4087 | } | |
4088 | ||
4089 | syms[dst_count] = NULL; | |
4090 | ||
4091 | return dst_count; | |
4092 | } | |
4093 | ||
5372391b | 4094 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 4095 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
4096 | |
4097 | static bfd_boolean | |
4098 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
4099 | { | |
c6d8cab4 L |
4100 | elf_symbol_type *type_ptr; |
4101 | ||
db0c309f NC |
4102 | if (sym == NULL) |
4103 | return FALSE; | |
4104 | ||
c6d8cab4 L |
4105 | if ((sym->flags & BSF_SECTION_SYM) == 0) |
4106 | return FALSE; | |
4107 | ||
db0c309f NC |
4108 | if (sym->section == NULL) |
4109 | return TRUE; | |
4110 | ||
c6d8cab4 L |
4111 | type_ptr = elf_symbol_from (abfd, sym); |
4112 | return ((type_ptr != NULL | |
4113 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
4114 | && bfd_is_abs_section (sym->section)) | |
4115 | || !(sym->section->owner == abfd | |
db0c309f NC |
4116 | || (sym->section->output_section != NULL |
4117 | && sym->section->output_section->owner == abfd | |
2633a79c AM |
4118 | && sym->section->output_offset == 0) |
4119 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
4120 | } |
4121 | ||
2633a79c AM |
4122 | /* Map symbol from it's internal number to the external number, moving |
4123 | all local symbols to be at the head of the list. */ | |
4124 | ||
b34976b6 | 4125 | static bfd_boolean |
12bd6957 | 4126 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 4127 | { |
dc810e39 | 4128 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
4129 | asymbol **syms = bfd_get_outsymbols (abfd); |
4130 | asymbol **sect_syms; | |
dc810e39 AM |
4131 | unsigned int num_locals = 0; |
4132 | unsigned int num_globals = 0; | |
4133 | unsigned int num_locals2 = 0; | |
4134 | unsigned int num_globals2 = 0; | |
7292b3ac | 4135 | unsigned int max_index = 0; |
dc810e39 | 4136 | unsigned int idx; |
252b5132 RH |
4137 | asection *asect; |
4138 | asymbol **new_syms; | |
446f7ed5 | 4139 | size_t amt; |
252b5132 RH |
4140 | |
4141 | #ifdef DEBUG | |
4142 | fprintf (stderr, "elf_map_symbols\n"); | |
4143 | fflush (stderr); | |
4144 | #endif | |
4145 | ||
252b5132 RH |
4146 | for (asect = abfd->sections; asect; asect = asect->next) |
4147 | { | |
4148 | if (max_index < asect->index) | |
4149 | max_index = asect->index; | |
4150 | } | |
4151 | ||
4152 | max_index++; | |
446f7ed5 AM |
4153 | amt = max_index * sizeof (asymbol *); |
4154 | sect_syms = (asymbol **) bfd_zalloc (abfd, amt); | |
252b5132 | 4155 | if (sect_syms == NULL) |
b34976b6 | 4156 | return FALSE; |
252b5132 | 4157 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 4158 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 4159 | |
079e9a2f AM |
4160 | /* Init sect_syms entries for any section symbols we have already |
4161 | decided to output. */ | |
252b5132 RH |
4162 | for (idx = 0; idx < symcount; idx++) |
4163 | { | |
dc810e39 | 4164 | asymbol *sym = syms[idx]; |
c044fabd | 4165 | |
252b5132 | 4166 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 4167 | && sym->value == 0 |
2633a79c AM |
4168 | && !ignore_section_sym (abfd, sym) |
4169 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 4170 | { |
5372391b | 4171 | asection *sec = sym->section; |
252b5132 | 4172 | |
5372391b AM |
4173 | if (sec->owner != abfd) |
4174 | sec = sec->output_section; | |
252b5132 | 4175 | |
5372391b | 4176 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
4177 | } |
4178 | } | |
4179 | ||
252b5132 RH |
4180 | /* Classify all of the symbols. */ |
4181 | for (idx = 0; idx < symcount; idx++) | |
4182 | { | |
2633a79c | 4183 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 4184 | num_globals++; |
2633a79c AM |
4185 | else if (!ignore_section_sym (abfd, syms[idx])) |
4186 | num_locals++; | |
252b5132 | 4187 | } |
079e9a2f | 4188 | |
5372391b | 4189 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
4190 | sections will already have a section symbol in outsymbols, but |
4191 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
4192 | at least in that case. */ | |
252b5132 RH |
4193 | for (asect = abfd->sections; asect; asect = asect->next) |
4194 | { | |
079e9a2f | 4195 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4196 | { |
079e9a2f | 4197 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
4198 | num_locals++; |
4199 | else | |
4200 | num_globals++; | |
252b5132 RH |
4201 | } |
4202 | } | |
4203 | ||
4204 | /* Now sort the symbols so the local symbols are first. */ | |
446f7ed5 AM |
4205 | amt = (num_locals + num_globals) * sizeof (asymbol *); |
4206 | new_syms = (asymbol **) bfd_alloc (abfd, amt); | |
252b5132 | 4207 | if (new_syms == NULL) |
b34976b6 | 4208 | return FALSE; |
252b5132 RH |
4209 | |
4210 | for (idx = 0; idx < symcount; idx++) | |
4211 | { | |
4212 | asymbol *sym = syms[idx]; | |
dc810e39 | 4213 | unsigned int i; |
252b5132 | 4214 | |
2633a79c AM |
4215 | if (sym_is_global (abfd, sym)) |
4216 | i = num_locals + num_globals2++; | |
4217 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
4218 | i = num_locals2++; |
4219 | else | |
2633a79c | 4220 | continue; |
252b5132 RH |
4221 | new_syms[i] = sym; |
4222 | sym->udata.i = i + 1; | |
4223 | } | |
4224 | for (asect = abfd->sections; asect; asect = asect->next) | |
4225 | { | |
079e9a2f | 4226 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4227 | { |
079e9a2f | 4228 | asymbol *sym = asect->symbol; |
dc810e39 | 4229 | unsigned int i; |
252b5132 | 4230 | |
079e9a2f | 4231 | sect_syms[asect->index] = sym; |
252b5132 RH |
4232 | if (!sym_is_global (abfd, sym)) |
4233 | i = num_locals2++; | |
4234 | else | |
4235 | i = num_locals + num_globals2++; | |
4236 | new_syms[i] = sym; | |
4237 | sym->udata.i = i + 1; | |
4238 | } | |
4239 | } | |
4240 | ||
4241 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
4242 | ||
12bd6957 | 4243 | *pnum_locals = num_locals; |
b34976b6 | 4244 | return TRUE; |
252b5132 RH |
4245 | } |
4246 | ||
4247 | /* Align to the maximum file alignment that could be required for any | |
4248 | ELF data structure. */ | |
4249 | ||
268b6b39 | 4250 | static inline file_ptr |
217aa764 | 4251 | align_file_position (file_ptr off, int align) |
252b5132 RH |
4252 | { |
4253 | return (off + align - 1) & ~(align - 1); | |
4254 | } | |
4255 | ||
4256 | /* Assign a file position to a section, optionally aligning to the | |
4257 | required section alignment. */ | |
4258 | ||
217aa764 AM |
4259 | file_ptr |
4260 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
4261 | file_ptr offset, | |
4262 | bfd_boolean align) | |
252b5132 | 4263 | { |
72de5009 AM |
4264 | if (align && i_shdrp->sh_addralign > 1) |
4265 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
4266 | i_shdrp->sh_offset = offset; |
4267 | if (i_shdrp->bfd_section != NULL) | |
4268 | i_shdrp->bfd_section->filepos = offset; | |
4269 | if (i_shdrp->sh_type != SHT_NOBITS) | |
4270 | offset += i_shdrp->sh_size; | |
4271 | return offset; | |
4272 | } | |
4273 | ||
4274 | /* Compute the file positions we are going to put the sections at, and | |
4275 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
4276 | is not NULL, this is being called by the ELF backend linker. */ | |
4277 | ||
b34976b6 | 4278 | bfd_boolean |
217aa764 AM |
4279 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
4280 | struct bfd_link_info *link_info) | |
252b5132 | 4281 | { |
9c5bfbb7 | 4282 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 4283 | struct fake_section_arg fsargs; |
b34976b6 | 4284 | bfd_boolean failed; |
ef10c3ac | 4285 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 4286 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 4287 | bfd_boolean need_symtab; |
252b5132 RH |
4288 | |
4289 | if (abfd->output_has_begun) | |
b34976b6 | 4290 | return TRUE; |
252b5132 RH |
4291 | |
4292 | /* Do any elf backend specific processing first. */ | |
4293 | if (bed->elf_backend_begin_write_processing) | |
4294 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
4295 | ||
ed7e9d0b | 4296 | if (!(*bed->elf_backend_init_file_header) (abfd, link_info)) |
b34976b6 | 4297 | return FALSE; |
252b5132 | 4298 | |
d4730f92 BS |
4299 | fsargs.failed = FALSE; |
4300 | fsargs.link_info = link_info; | |
4301 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
4302 | if (fsargs.failed) | |
b34976b6 | 4303 | return FALSE; |
252b5132 | 4304 | |
da9f89d4 | 4305 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 4306 | return FALSE; |
252b5132 RH |
4307 | |
4308 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
4309 | need_symtab = (link_info == NULL |
4310 | && (bfd_get_symcount (abfd) > 0 | |
4311 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
4312 | == HAS_RELOC))); | |
4313 | if (need_symtab) | |
252b5132 RH |
4314 | { |
4315 | /* Non-zero if doing a relocatable link. */ | |
4316 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
4317 | ||
4318 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 4319 | return FALSE; |
252b5132 RH |
4320 | } |
4321 | ||
d4730f92 | 4322 | failed = FALSE; |
1126897b | 4323 | if (link_info == NULL) |
dbb410c3 | 4324 | { |
1126897b | 4325 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 4326 | if (failed) |
b34976b6 | 4327 | return FALSE; |
dbb410c3 AM |
4328 | } |
4329 | ||
252b5132 | 4330 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
ed7e9d0b | 4331 | /* sh_name was set in init_file_header. */ |
252b5132 | 4332 | shstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 4333 | shstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
252b5132 | 4334 | shstrtab_hdr->sh_addr = 0; |
946748d5 | 4335 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4336 | shstrtab_hdr->sh_entsize = 0; |
4337 | shstrtab_hdr->sh_link = 0; | |
4338 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 4339 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4340 | shstrtab_hdr->sh_addralign = 1; |
4341 | ||
c84fca4d | 4342 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 4343 | return FALSE; |
252b5132 | 4344 | |
3516e984 | 4345 | if (need_symtab) |
252b5132 RH |
4346 | { |
4347 | file_ptr off; | |
4348 | Elf_Internal_Shdr *hdr; | |
4349 | ||
12bd6957 | 4350 | off = elf_next_file_pos (abfd); |
252b5132 | 4351 | |
6a40cf0c | 4352 | hdr = & elf_symtab_hdr (abfd); |
b34976b6 | 4353 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4354 | |
6a40cf0c NC |
4355 | if (elf_symtab_shndx_list (abfd) != NULL) |
4356 | { | |
4357 | hdr = & elf_symtab_shndx_list (abfd)->hdr; | |
4358 | if (hdr->sh_size != 0) | |
4359 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4360 | /* FIXME: What about other symtab_shndx sections in the list ? */ | |
4361 | } | |
9ad5cbcf | 4362 | |
252b5132 | 4363 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 4364 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4365 | |
12bd6957 | 4366 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
4367 | |
4368 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 4369 | out. */ |
252b5132 | 4370 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 4371 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 4372 | return FALSE; |
ef10c3ac | 4373 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
4374 | } |
4375 | ||
b34976b6 | 4376 | abfd->output_has_begun = TRUE; |
252b5132 | 4377 | |
b34976b6 | 4378 | return TRUE; |
252b5132 RH |
4379 | } |
4380 | ||
8ded5a0f AM |
4381 | /* Make an initial estimate of the size of the program header. If we |
4382 | get the number wrong here, we'll redo section placement. */ | |
4383 | ||
4384 | static bfd_size_type | |
4385 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
4386 | { | |
4387 | size_t segs; | |
4388 | asection *s; | |
2b05f1b7 | 4389 | const struct elf_backend_data *bed; |
8ded5a0f AM |
4390 | |
4391 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
4392 | and one for data. */ | |
4393 | segs = 2; | |
4394 | ||
4395 | s = bfd_get_section_by_name (abfd, ".interp"); | |
1b9e270b | 4396 | if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) |
8ded5a0f AM |
4397 | { |
4398 | /* If we have a loadable interpreter section, we need a | |
4399 | PT_INTERP segment. In this case, assume we also need a | |
4400 | PT_PHDR segment, although that may not be true for all | |
4401 | targets. */ | |
e9a38e0f | 4402 | segs += 2; |
8ded5a0f AM |
4403 | } |
4404 | ||
4405 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
4406 | { | |
4407 | /* We need a PT_DYNAMIC segment. */ | |
4408 | ++segs; | |
f210dcff | 4409 | } |
08a40648 | 4410 | |
ceae84aa | 4411 | if (info != NULL && info->relro) |
f210dcff L |
4412 | { |
4413 | /* We need a PT_GNU_RELRO segment. */ | |
4414 | ++segs; | |
8ded5a0f AM |
4415 | } |
4416 | ||
12bd6957 | 4417 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
4418 | { |
4419 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4420 | ++segs; | |
4421 | } | |
4422 | ||
12bd6957 | 4423 | if (elf_stack_flags (abfd)) |
8ded5a0f | 4424 | { |
2b05f1b7 L |
4425 | /* We need a PT_GNU_STACK segment. */ |
4426 | ++segs; | |
4427 | } | |
94b11780 | 4428 | |
0a59decb L |
4429 | s = bfd_get_section_by_name (abfd, |
4430 | NOTE_GNU_PROPERTY_SECTION_NAME); | |
4431 | if (s != NULL && s->size != 0) | |
4432 | { | |
4433 | /* We need a PT_GNU_PROPERTY segment. */ | |
4434 | ++segs; | |
4435 | } | |
4436 | ||
2b05f1b7 L |
4437 | for (s = abfd->sections; s != NULL; s = s->next) |
4438 | { | |
8ded5a0f | 4439 | if ((s->flags & SEC_LOAD) != 0 |
23e463ed | 4440 | && elf_section_type (s) == SHT_NOTE) |
8ded5a0f | 4441 | { |
23e463ed | 4442 | unsigned int alignment_power; |
8ded5a0f AM |
4443 | /* We need a PT_NOTE segment. */ |
4444 | ++segs; | |
23e463ed L |
4445 | /* Try to create just one PT_NOTE segment for all adjacent |
4446 | loadable SHT_NOTE sections. gABI requires that within a | |
4447 | PT_NOTE segment (and also inside of each SHT_NOTE section) | |
4448 | each note should have the same alignment. So we check | |
4449 | whether the sections are correctly aligned. */ | |
4450 | alignment_power = s->alignment_power; | |
4451 | while (s->next != NULL | |
4452 | && s->next->alignment_power == alignment_power | |
4453 | && (s->next->flags & SEC_LOAD) != 0 | |
4454 | && elf_section_type (s->next) == SHT_NOTE) | |
4455 | s = s->next; | |
8ded5a0f AM |
4456 | } |
4457 | } | |
4458 | ||
4459 | for (s = abfd->sections; s != NULL; s = s->next) | |
4460 | { | |
4461 | if (s->flags & SEC_THREAD_LOCAL) | |
4462 | { | |
4463 | /* We need a PT_TLS segment. */ | |
4464 | ++segs; | |
4465 | break; | |
4466 | } | |
4467 | } | |
4468 | ||
2b05f1b7 | 4469 | bed = get_elf_backend_data (abfd); |
a91e1603 | 4470 | |
df3a023b AM |
4471 | if ((abfd->flags & D_PAGED) != 0 |
4472 | && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) | |
4473 | { | |
4474 | /* Add a PT_GNU_MBIND segment for each mbind section. */ | |
4475 | unsigned int page_align_power = bfd_log2 (bed->commonpagesize); | |
4476 | for (s = abfd->sections; s != NULL; s = s->next) | |
4477 | if (elf_section_flags (s) & SHF_GNU_MBIND) | |
4478 | { | |
4479 | if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM) | |
4480 | { | |
4481 | _bfd_error_handler | |
4482 | /* xgettext:c-format */ | |
4483 | (_("%pB: GNU_MBIND section `%pA' has invalid " | |
4484 | "sh_info field: %d"), | |
4485 | abfd, s, elf_section_data (s)->this_hdr.sh_info); | |
4486 | continue; | |
4487 | } | |
4488 | /* Align mbind section to page size. */ | |
4489 | if (s->alignment_power < page_align_power) | |
4490 | s->alignment_power = page_align_power; | |
4491 | segs ++; | |
4492 | } | |
4493 | } | |
4494 | ||
4495 | /* Let the backend count up any program headers it might need. */ | |
4496 | if (bed->elf_backend_additional_program_headers) | |
8ded5a0f AM |
4497 | { |
4498 | int a; | |
4499 | ||
4500 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
4501 | if (a == -1) | |
4502 | abort (); | |
4503 | segs += a; | |
4504 | } | |
4505 | ||
4506 | return segs * bed->s->sizeof_phdr; | |
4507 | } | |
4508 | ||
2ea37f1c NC |
4509 | /* Find the segment that contains the output_section of section. */ |
4510 | ||
4511 | Elf_Internal_Phdr * | |
4512 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
4513 | { | |
4514 | struct elf_segment_map *m; | |
4515 | Elf_Internal_Phdr *p; | |
4516 | ||
12bd6957 | 4517 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
4518 | m != NULL; |
4519 | m = m->next, p++) | |
4520 | { | |
4521 | int i; | |
4522 | ||
4523 | for (i = m->count - 1; i >= 0; i--) | |
4524 | if (m->sections[i] == section) | |
4525 | return p; | |
4526 | } | |
4527 | ||
4528 | return NULL; | |
4529 | } | |
4530 | ||
252b5132 RH |
4531 | /* Create a mapping from a set of sections to a program segment. */ |
4532 | ||
217aa764 AM |
4533 | static struct elf_segment_map * |
4534 | make_mapping (bfd *abfd, | |
4535 | asection **sections, | |
4536 | unsigned int from, | |
4537 | unsigned int to, | |
4538 | bfd_boolean phdr) | |
252b5132 RH |
4539 | { |
4540 | struct elf_segment_map *m; | |
4541 | unsigned int i; | |
4542 | asection **hdrpp; | |
986f0783 | 4543 | size_t amt; |
252b5132 | 4544 | |
00bee008 AM |
4545 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
4546 | amt += (to - from) * sizeof (asection *); | |
a50b1753 | 4547 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4548 | if (m == NULL) |
4549 | return NULL; | |
4550 | m->next = NULL; | |
4551 | m->p_type = PT_LOAD; | |
4552 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
4553 | m->sections[i - from] = *hdrpp; | |
4554 | m->count = to - from; | |
4555 | ||
4556 | if (from == 0 && phdr) | |
4557 | { | |
4558 | /* Include the headers in the first PT_LOAD segment. */ | |
4559 | m->includes_filehdr = 1; | |
4560 | m->includes_phdrs = 1; | |
4561 | } | |
4562 | ||
4563 | return m; | |
4564 | } | |
4565 | ||
229fcec5 MM |
4566 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
4567 | on failure. */ | |
4568 | ||
4569 | struct elf_segment_map * | |
4570 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
4571 | { | |
4572 | struct elf_segment_map *m; | |
4573 | ||
a50b1753 | 4574 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
07d6d2b8 | 4575 | sizeof (struct elf_segment_map)); |
229fcec5 MM |
4576 | if (m == NULL) |
4577 | return NULL; | |
4578 | m->next = NULL; | |
4579 | m->p_type = PT_DYNAMIC; | |
4580 | m->count = 1; | |
4581 | m->sections[0] = dynsec; | |
08a40648 | 4582 | |
229fcec5 MM |
4583 | return m; |
4584 | } | |
4585 | ||
8ded5a0f | 4586 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 4587 | |
b34976b6 | 4588 | static bfd_boolean |
3dea8fca AM |
4589 | elf_modify_segment_map (bfd *abfd, |
4590 | struct bfd_link_info *info, | |
4591 | bfd_boolean remove_empty_load) | |
252b5132 | 4592 | { |
252e386e | 4593 | struct elf_segment_map **m; |
8ded5a0f | 4594 | const struct elf_backend_data *bed; |
252b5132 | 4595 | |
8ded5a0f AM |
4596 | /* The placement algorithm assumes that non allocated sections are |
4597 | not in PT_LOAD segments. We ensure this here by removing such | |
4598 | sections from the segment map. We also remove excluded | |
252e386e AM |
4599 | sections. Finally, any PT_LOAD segment without sections is |
4600 | removed. */ | |
12bd6957 | 4601 | m = &elf_seg_map (abfd); |
252e386e | 4602 | while (*m) |
8ded5a0f AM |
4603 | { |
4604 | unsigned int i, new_count; | |
252b5132 | 4605 | |
252e386e | 4606 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4607 | { |
252e386e AM |
4608 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4609 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4610 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4611 | { |
252e386e AM |
4612 | (*m)->sections[new_count] = (*m)->sections[i]; |
4613 | new_count++; | |
8ded5a0f AM |
4614 | } |
4615 | } | |
252e386e | 4616 | (*m)->count = new_count; |
252b5132 | 4617 | |
1a9ccd70 NC |
4618 | if (remove_empty_load |
4619 | && (*m)->p_type == PT_LOAD | |
4620 | && (*m)->count == 0 | |
4621 | && !(*m)->includes_phdrs) | |
252e386e AM |
4622 | *m = (*m)->next; |
4623 | else | |
4624 | m = &(*m)->next; | |
8ded5a0f | 4625 | } |
252b5132 | 4626 | |
8ded5a0f AM |
4627 | bed = get_elf_backend_data (abfd); |
4628 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4629 | { |
252e386e | 4630 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4631 | return FALSE; |
252b5132 | 4632 | } |
252b5132 | 4633 | |
8ded5a0f AM |
4634 | return TRUE; |
4635 | } | |
252b5132 | 4636 | |
dbc88fc1 AM |
4637 | #define IS_TBSS(s) \ |
4638 | ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL) | |
4639 | ||
8ded5a0f | 4640 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4641 | |
8ded5a0f AM |
4642 | bfd_boolean |
4643 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4644 | { | |
4645 | unsigned int count; | |
4646 | struct elf_segment_map *m; | |
4647 | asection **sections = NULL; | |
4648 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4649 | bfd_boolean no_user_phdrs; |
252b5132 | 4650 | |
12bd6957 | 4651 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4652 | |
4653 | if (info != NULL) | |
4654 | info->user_phdrs = !no_user_phdrs; | |
4655 | ||
3dea8fca | 4656 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4657 | { |
8ded5a0f AM |
4658 | asection *s; |
4659 | unsigned int i; | |
4660 | struct elf_segment_map *mfirst; | |
4661 | struct elf_segment_map **pm; | |
4662 | asection *last_hdr; | |
4663 | bfd_vma last_size; | |
00bee008 | 4664 | unsigned int hdr_index; |
8ded5a0f AM |
4665 | bfd_vma maxpagesize; |
4666 | asection **hdrpp; | |
64029e93 | 4667 | bfd_boolean phdr_in_segment; |
8ded5a0f | 4668 | bfd_boolean writable; |
2888249f | 4669 | bfd_boolean executable; |
446f7ed5 | 4670 | unsigned int tls_count = 0; |
8ded5a0f | 4671 | asection *first_tls = NULL; |
a91e1603 | 4672 | asection *first_mbind = NULL; |
8ded5a0f | 4673 | asection *dynsec, *eh_frame_hdr; |
446f7ed5 | 4674 | size_t amt; |
66631823 CE |
4675 | bfd_vma addr_mask, wrap_to = 0; /* Bytes. */ |
4676 | bfd_size_type phdr_size; /* Octets/bytes. */ | |
502794d4 | 4677 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
252b5132 | 4678 | |
8ded5a0f | 4679 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4680 | |
446f7ed5 AM |
4681 | amt = bfd_count_sections (abfd) * sizeof (asection *); |
4682 | sections = (asection **) bfd_malloc (amt); | |
8ded5a0f | 4683 | if (sections == NULL) |
252b5132 | 4684 | goto error_return; |
252b5132 | 4685 | |
8d06853e AM |
4686 | /* Calculate top address, avoiding undefined behaviour of shift |
4687 | left operator when shift count is equal to size of type | |
4688 | being shifted. */ | |
4689 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4690 | addr_mask = (addr_mask << 1) + 1; | |
4691 | ||
8ded5a0f AM |
4692 | i = 0; |
4693 | for (s = abfd->sections; s != NULL; s = s->next) | |
4694 | { | |
4695 | if ((s->flags & SEC_ALLOC) != 0) | |
4696 | { | |
48db3297 AM |
4697 | /* target_index is unused until bfd_elf_final_link |
4698 | starts output of section symbols. Use it to make | |
4699 | qsort stable. */ | |
4700 | s->target_index = i; | |
8ded5a0f AM |
4701 | sections[i] = s; |
4702 | ++i; | |
8d06853e | 4703 | /* A wrapping section potentially clashes with header. */ |
66631823 CE |
4704 | if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask)) |
4705 | wrap_to = (s->lma + s->size / opb) & addr_mask; | |
8ded5a0f AM |
4706 | } |
4707 | } | |
4708 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4709 | count = i; | |
252b5132 | 4710 | |
8ded5a0f | 4711 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4712 | |
64029e93 AM |
4713 | phdr_size = elf_program_header_size (abfd); |
4714 | if (phdr_size == (bfd_size_type) -1) | |
4715 | phdr_size = get_program_header_size (abfd, info); | |
4716 | phdr_size += bed->s->sizeof_ehdr; | |
502794d4 CE |
4717 | /* phdr_size is compared to LMA values which are in bytes. */ |
4718 | phdr_size /= opb; | |
64029e93 AM |
4719 | maxpagesize = bed->maxpagesize; |
4720 | if (maxpagesize == 0) | |
4721 | maxpagesize = 1; | |
4722 | phdr_in_segment = info != NULL && info->load_phdrs; | |
4723 | if (count != 0 | |
4724 | && (((sections[0]->lma & addr_mask) & (maxpagesize - 1)) | |
4725 | >= (phdr_size & (maxpagesize - 1)))) | |
4726 | /* For compatibility with old scripts that may not be using | |
4727 | SIZEOF_HEADERS, add headers when it looks like space has | |
4728 | been left for them. */ | |
4729 | phdr_in_segment = TRUE; | |
252b5132 | 4730 | |
64029e93 | 4731 | /* Build the mapping. */ |
8ded5a0f AM |
4732 | mfirst = NULL; |
4733 | pm = &mfirst; | |
252b5132 | 4734 | |
8ded5a0f AM |
4735 | /* If we have a .interp section, then create a PT_PHDR segment for |
4736 | the program headers and a PT_INTERP segment for the .interp | |
4737 | section. */ | |
4738 | s = bfd_get_section_by_name (abfd, ".interp"); | |
1b9e270b | 4739 | if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) |
8ded5a0f AM |
4740 | { |
4741 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4742 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4743 | if (m == NULL) |
4744 | goto error_return; | |
4745 | m->next = NULL; | |
4746 | m->p_type = PT_PHDR; | |
f882209d | 4747 | m->p_flags = PF_R; |
8ded5a0f AM |
4748 | m->p_flags_valid = 1; |
4749 | m->includes_phdrs = 1; | |
64029e93 | 4750 | phdr_in_segment = TRUE; |
8ded5a0f AM |
4751 | *pm = m; |
4752 | pm = &m->next; | |
252b5132 | 4753 | |
8ded5a0f | 4754 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4755 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4756 | if (m == NULL) |
4757 | goto error_return; | |
4758 | m->next = NULL; | |
4759 | m->p_type = PT_INTERP; | |
4760 | m->count = 1; | |
4761 | m->sections[0] = s; | |
4762 | ||
4763 | *pm = m; | |
4764 | pm = &m->next; | |
252b5132 | 4765 | } |
8ded5a0f AM |
4766 | |
4767 | /* Look through the sections. We put sections in the same program | |
4768 | segment when the start of the second section can be placed within | |
4769 | a few bytes of the end of the first section. */ | |
4770 | last_hdr = NULL; | |
4771 | last_size = 0; | |
00bee008 | 4772 | hdr_index = 0; |
8ded5a0f | 4773 | writable = FALSE; |
2888249f | 4774 | executable = FALSE; |
8ded5a0f AM |
4775 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); |
4776 | if (dynsec != NULL | |
4777 | && (dynsec->flags & SEC_LOAD) == 0) | |
4778 | dynsec = NULL; | |
4779 | ||
64029e93 AM |
4780 | if ((abfd->flags & D_PAGED) == 0) |
4781 | phdr_in_segment = FALSE; | |
4782 | ||
8ded5a0f AM |
4783 | /* Deal with -Ttext or something similar such that the first section |
4784 | is not adjacent to the program headers. This is an | |
4785 | approximation, since at this point we don't know exactly how many | |
4786 | program headers we will need. */ | |
64029e93 | 4787 | if (phdr_in_segment && count > 0) |
252b5132 | 4788 | { |
66631823 | 4789 | bfd_vma phdr_lma; /* Bytes. */ |
64029e93 AM |
4790 | bfd_boolean separate_phdr = FALSE; |
4791 | ||
4792 | phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize; | |
4793 | if (info != NULL | |
4794 | && info->separate_code | |
4795 | && (sections[0]->flags & SEC_CODE) != 0) | |
1a9ccd70 | 4796 | { |
64029e93 AM |
4797 | /* If data sections should be separate from code and |
4798 | thus not executable, and the first section is | |
4799 | executable then put the file and program headers in | |
4800 | their own PT_LOAD. */ | |
4801 | separate_phdr = TRUE; | |
4802 | if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize) | |
4803 | == (sections[0]->lma & addr_mask & -maxpagesize))) | |
4804 | { | |
4805 | /* The file and program headers are currently on the | |
4806 | same page as the first section. Put them on the | |
4807 | previous page if we can. */ | |
4808 | if (phdr_lma >= maxpagesize) | |
4809 | phdr_lma -= maxpagesize; | |
4810 | else | |
4811 | separate_phdr = FALSE; | |
4812 | } | |
4813 | } | |
4814 | if ((sections[0]->lma & addr_mask) < phdr_lma | |
4815 | || (sections[0]->lma & addr_mask) < phdr_size) | |
4816 | /* If file and program headers would be placed at the end | |
4817 | of memory then it's probably better to omit them. */ | |
4818 | phdr_in_segment = FALSE; | |
4819 | else if (phdr_lma < wrap_to) | |
4820 | /* If a section wraps around to where we'll be placing | |
4821 | file and program headers, then the headers will be | |
4822 | overwritten. */ | |
4823 | phdr_in_segment = FALSE; | |
4824 | else if (separate_phdr) | |
4825 | { | |
4826 | m = make_mapping (abfd, sections, 0, 0, phdr_in_segment); | |
4827 | if (m == NULL) | |
4828 | goto error_return; | |
66631823 | 4829 | m->p_paddr = phdr_lma * opb; |
64029e93 AM |
4830 | m->p_vaddr_offset |
4831 | = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize; | |
4832 | m->p_paddr_valid = 1; | |
4833 | *pm = m; | |
4834 | pm = &m->next; | |
4835 | phdr_in_segment = FALSE; | |
1a9ccd70 | 4836 | } |
252b5132 RH |
4837 | } |
4838 | ||
8ded5a0f | 4839 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4840 | { |
8ded5a0f AM |
4841 | asection *hdr; |
4842 | bfd_boolean new_segment; | |
4843 | ||
4844 | hdr = *hdrpp; | |
4845 | ||
4846 | /* See if this section and the last one will fit in the same | |
4847 | segment. */ | |
4848 | ||
4849 | if (last_hdr == NULL) | |
4850 | { | |
4851 | /* If we don't have a segment yet, then we don't need a new | |
4852 | one (we build the last one after this loop). */ | |
4853 | new_segment = FALSE; | |
4854 | } | |
4855 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4856 | { | |
4857 | /* If this section has a different relation between the | |
4858 | virtual address and the load address, then we need a new | |
4859 | segment. */ | |
4860 | new_segment = TRUE; | |
4861 | } | |
b5599592 AM |
4862 | else if (hdr->lma < last_hdr->lma + last_size |
4863 | || last_hdr->lma + last_size < last_hdr->lma) | |
4864 | { | |
4865 | /* If this section has a load address that makes it overlap | |
4866 | the previous section, then we need a new segment. */ | |
4867 | new_segment = TRUE; | |
4868 | } | |
76cb3a89 AM |
4869 | else if ((abfd->flags & D_PAGED) != 0 |
4870 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4871 | == (hdr->lma & -maxpagesize))) | |
4872 | { | |
4873 | /* If we are demand paged then we can't map two disk | |
4874 | pages onto the same memory page. */ | |
4875 | new_segment = FALSE; | |
4876 | } | |
39948a60 NC |
4877 | /* In the next test we have to be careful when last_hdr->lma is close |
4878 | to the end of the address space. If the aligned address wraps | |
4879 | around to the start of the address space, then there are no more | |
4880 | pages left in memory and it is OK to assume that the current | |
4881 | section can be included in the current segment. */ | |
76cb3a89 AM |
4882 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) |
4883 | + maxpagesize > last_hdr->lma) | |
4884 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) | |
4885 | + maxpagesize <= hdr->lma)) | |
8ded5a0f AM |
4886 | { |
4887 | /* If putting this section in this segment would force us to | |
4888 | skip a page in the segment, then we need a new segment. */ | |
4889 | new_segment = TRUE; | |
4890 | } | |
4891 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
76cb3a89 | 4892 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) |
8ded5a0f | 4893 | { |
e5654c0f AM |
4894 | /* We don't want to put a loaded section after a |
4895 | nonloaded (ie. bss style) section in the same segment | |
4896 | as that will force the non-loaded section to be loaded. | |
76cb3a89 | 4897 | Consider .tbss sections as loaded for this purpose. */ |
8ded5a0f AM |
4898 | new_segment = TRUE; |
4899 | } | |
4900 | else if ((abfd->flags & D_PAGED) == 0) | |
4901 | { | |
4902 | /* If the file is not demand paged, which means that we | |
4903 | don't require the sections to be correctly aligned in the | |
4904 | file, then there is no other reason for a new segment. */ | |
4905 | new_segment = FALSE; | |
4906 | } | |
2888249f L |
4907 | else if (info != NULL |
4908 | && info->separate_code | |
4909 | && executable != ((hdr->flags & SEC_CODE) != 0)) | |
4910 | { | |
4911 | new_segment = TRUE; | |
4912 | } | |
8ded5a0f | 4913 | else if (! writable |
76cb3a89 | 4914 | && (hdr->flags & SEC_READONLY) == 0) |
8ded5a0f AM |
4915 | { |
4916 | /* We don't want to put a writable section in a read only | |
76cb3a89 | 4917 | segment. */ |
8ded5a0f AM |
4918 | new_segment = TRUE; |
4919 | } | |
4920 | else | |
4921 | { | |
4922 | /* Otherwise, we can use the same segment. */ | |
4923 | new_segment = FALSE; | |
4924 | } | |
4925 | ||
2889e75b | 4926 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4927 | if (last_hdr != NULL |
4928 | && info != NULL | |
4929 | && info->callbacks->override_segment_assignment != NULL) | |
4930 | new_segment | |
4931 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4932 | last_hdr, | |
4933 | new_segment); | |
2889e75b | 4934 | |
8ded5a0f AM |
4935 | if (! new_segment) |
4936 | { | |
4937 | if ((hdr->flags & SEC_READONLY) == 0) | |
4938 | writable = TRUE; | |
2888249f L |
4939 | if ((hdr->flags & SEC_CODE) != 0) |
4940 | executable = TRUE; | |
8ded5a0f AM |
4941 | last_hdr = hdr; |
4942 | /* .tbss sections effectively have zero size. */ | |
502794d4 | 4943 | last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; |
8ded5a0f AM |
4944 | continue; |
4945 | } | |
4946 | ||
4947 | /* We need a new program segment. We must create a new program | |
00bee008 | 4948 | header holding all the sections from hdr_index until hdr. */ |
8ded5a0f | 4949 | |
00bee008 | 4950 | m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); |
8ded5a0f AM |
4951 | if (m == NULL) |
4952 | goto error_return; | |
4953 | ||
4954 | *pm = m; | |
4955 | pm = &m->next; | |
4956 | ||
252b5132 | 4957 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4958 | writable = TRUE; |
8ded5a0f AM |
4959 | else |
4960 | writable = FALSE; | |
4961 | ||
2888249f L |
4962 | if ((hdr->flags & SEC_CODE) == 0) |
4963 | executable = FALSE; | |
4964 | else | |
4965 | executable = TRUE; | |
4966 | ||
baaff79e JJ |
4967 | last_hdr = hdr; |
4968 | /* .tbss sections effectively have zero size. */ | |
502794d4 | 4969 | last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; |
00bee008 | 4970 | hdr_index = i; |
8ded5a0f | 4971 | phdr_in_segment = FALSE; |
252b5132 RH |
4972 | } |
4973 | ||
86b2281f AM |
4974 | /* Create a final PT_LOAD program segment, but not if it's just |
4975 | for .tbss. */ | |
4976 | if (last_hdr != NULL | |
00bee008 | 4977 | && (i - hdr_index != 1 |
dbc88fc1 | 4978 | || !IS_TBSS (last_hdr))) |
8ded5a0f | 4979 | { |
00bee008 | 4980 | m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); |
8ded5a0f AM |
4981 | if (m == NULL) |
4982 | goto error_return; | |
252b5132 | 4983 | |
8ded5a0f AM |
4984 | *pm = m; |
4985 | pm = &m->next; | |
4986 | } | |
252b5132 | 4987 | |
8ded5a0f AM |
4988 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4989 | if (dynsec != NULL) | |
4990 | { | |
4991 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4992 | if (m == NULL) | |
4993 | goto error_return; | |
4994 | *pm = m; | |
4995 | pm = &m->next; | |
4996 | } | |
252b5132 | 4997 | |
23e463ed | 4998 | /* For each batch of consecutive loadable SHT_NOTE sections, |
1c5265b5 JJ |
4999 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, |
5000 | because if we link together nonloadable .note sections and | |
5001 | loadable .note sections, we will generate two .note sections | |
23e463ed | 5002 | in the output file. */ |
8ded5a0f AM |
5003 | for (s = abfd->sections; s != NULL; s = s->next) |
5004 | { | |
5005 | if ((s->flags & SEC_LOAD) != 0 | |
23e463ed | 5006 | && elf_section_type (s) == SHT_NOTE) |
8ded5a0f | 5007 | { |
1c5265b5 | 5008 | asection *s2; |
23e463ed | 5009 | unsigned int alignment_power = s->alignment_power; |
91d6fa6a NC |
5010 | |
5011 | count = 1; | |
23e463ed L |
5012 | for (s2 = s; s2->next != NULL; s2 = s2->next) |
5013 | { | |
5014 | if (s2->next->alignment_power == alignment_power | |
5015 | && (s2->next->flags & SEC_LOAD) != 0 | |
5016 | && elf_section_type (s2->next) == SHT_NOTE | |
66631823 | 5017 | && align_power (s2->lma + s2->size / opb, |
23e463ed L |
5018 | alignment_power) |
5019 | == s2->next->lma) | |
5020 | count++; | |
5021 | else | |
5022 | break; | |
5023 | } | |
00bee008 AM |
5024 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
5025 | amt += count * sizeof (asection *); | |
a50b1753 | 5026 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
5027 | if (m == NULL) |
5028 | goto error_return; | |
5029 | m->next = NULL; | |
5030 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
5031 | m->count = count; |
5032 | while (count > 1) | |
5033 | { | |
5034 | m->sections[m->count - count--] = s; | |
5035 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
5036 | s = s->next; | |
5037 | } | |
5038 | m->sections[m->count - 1] = s; | |
5039 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
5040 | *pm = m; |
5041 | pm = &m->next; | |
5042 | } | |
5043 | if (s->flags & SEC_THREAD_LOCAL) | |
5044 | { | |
5045 | if (! tls_count) | |
5046 | first_tls = s; | |
5047 | tls_count++; | |
5048 | } | |
a91e1603 L |
5049 | if (first_mbind == NULL |
5050 | && (elf_section_flags (s) & SHF_GNU_MBIND) != 0) | |
5051 | first_mbind = s; | |
8ded5a0f | 5052 | } |
252b5132 | 5053 | |
8ded5a0f AM |
5054 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
5055 | if (tls_count > 0) | |
5056 | { | |
00bee008 AM |
5057 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
5058 | amt += tls_count * sizeof (asection *); | |
a50b1753 | 5059 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
5060 | if (m == NULL) |
5061 | goto error_return; | |
5062 | m->next = NULL; | |
5063 | m->p_type = PT_TLS; | |
5064 | m->count = tls_count; | |
5065 | /* Mandated PF_R. */ | |
5066 | m->p_flags = PF_R; | |
5067 | m->p_flags_valid = 1; | |
d923cae0 | 5068 | s = first_tls; |
446f7ed5 | 5069 | for (i = 0; i < tls_count; ++i) |
8ded5a0f | 5070 | { |
d923cae0 L |
5071 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
5072 | { | |
5073 | _bfd_error_handler | |
871b3ab2 | 5074 | (_("%pB: TLS sections are not adjacent:"), abfd); |
d923cae0 L |
5075 | s = first_tls; |
5076 | i = 0; | |
446f7ed5 | 5077 | while (i < tls_count) |
d923cae0 L |
5078 | { |
5079 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
5080 | { | |
871b3ab2 | 5081 | _bfd_error_handler (_(" TLS: %pA"), s); |
d923cae0 L |
5082 | i++; |
5083 | } | |
5084 | else | |
871b3ab2 | 5085 | _bfd_error_handler (_(" non-TLS: %pA"), s); |
d923cae0 L |
5086 | s = s->next; |
5087 | } | |
5088 | bfd_set_error (bfd_error_bad_value); | |
5089 | goto error_return; | |
5090 | } | |
5091 | m->sections[i] = s; | |
5092 | s = s->next; | |
8ded5a0f | 5093 | } |
252b5132 | 5094 | |
8ded5a0f AM |
5095 | *pm = m; |
5096 | pm = &m->next; | |
5097 | } | |
252b5132 | 5098 | |
df3a023b AM |
5099 | if (first_mbind |
5100 | && (abfd->flags & D_PAGED) != 0 | |
5101 | && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) | |
a91e1603 L |
5102 | for (s = first_mbind; s != NULL; s = s->next) |
5103 | if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0 | |
df3a023b | 5104 | && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM) |
a91e1603 L |
5105 | { |
5106 | /* Mandated PF_R. */ | |
5107 | unsigned long p_flags = PF_R; | |
5108 | if ((s->flags & SEC_READONLY) == 0) | |
5109 | p_flags |= PF_W; | |
5110 | if ((s->flags & SEC_CODE) != 0) | |
5111 | p_flags |= PF_X; | |
5112 | ||
5113 | amt = sizeof (struct elf_segment_map) + sizeof (asection *); | |
5114 | m = bfd_zalloc (abfd, amt); | |
5115 | if (m == NULL) | |
5116 | goto error_return; | |
5117 | m->next = NULL; | |
5118 | m->p_type = (PT_GNU_MBIND_LO | |
5119 | + elf_section_data (s)->this_hdr.sh_info); | |
5120 | m->count = 1; | |
5121 | m->p_flags_valid = 1; | |
5122 | m->sections[0] = s; | |
5123 | m->p_flags = p_flags; | |
5124 | ||
5125 | *pm = m; | |
5126 | pm = &m->next; | |
5127 | } | |
5128 | ||
0a59decb L |
5129 | s = bfd_get_section_by_name (abfd, |
5130 | NOTE_GNU_PROPERTY_SECTION_NAME); | |
5131 | if (s != NULL && s->size != 0) | |
5132 | { | |
5133 | amt = sizeof (struct elf_segment_map) + sizeof (asection *); | |
5134 | m = bfd_zalloc (abfd, amt); | |
5135 | if (m == NULL) | |
5136 | goto error_return; | |
5137 | m->next = NULL; | |
5138 | m->p_type = PT_GNU_PROPERTY; | |
5139 | m->count = 1; | |
5140 | m->p_flags_valid = 1; | |
5141 | m->sections[0] = s; | |
5142 | m->p_flags = PF_R; | |
5143 | *pm = m; | |
5144 | pm = &m->next; | |
5145 | } | |
5146 | ||
8ded5a0f AM |
5147 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
5148 | segment. */ | |
12bd6957 | 5149 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
5150 | if (eh_frame_hdr != NULL |
5151 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 5152 | { |
dc810e39 | 5153 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 5154 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
5155 | if (m == NULL) |
5156 | goto error_return; | |
5157 | m->next = NULL; | |
8ded5a0f | 5158 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 5159 | m->count = 1; |
8ded5a0f | 5160 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
5161 | |
5162 | *pm = m; | |
5163 | pm = &m->next; | |
5164 | } | |
13ae64f3 | 5165 | |
12bd6957 | 5166 | if (elf_stack_flags (abfd)) |
13ae64f3 | 5167 | { |
8ded5a0f | 5168 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 5169 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
5170 | if (m == NULL) |
5171 | goto error_return; | |
5172 | m->next = NULL; | |
2b05f1b7 | 5173 | m->p_type = PT_GNU_STACK; |
12bd6957 | 5174 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 5175 | m->p_align = bed->stack_align; |
8ded5a0f | 5176 | m->p_flags_valid = 1; |
04c3a755 NS |
5177 | m->p_align_valid = m->p_align != 0; |
5178 | if (info->stacksize > 0) | |
5179 | { | |
5180 | m->p_size = info->stacksize; | |
5181 | m->p_size_valid = 1; | |
5182 | } | |
252b5132 | 5183 | |
8ded5a0f AM |
5184 | *pm = m; |
5185 | pm = &m->next; | |
5186 | } | |
65765700 | 5187 | |
ceae84aa | 5188 | if (info != NULL && info->relro) |
8ded5a0f | 5189 | { |
f210dcff L |
5190 | for (m = mfirst; m != NULL; m = m->next) |
5191 | { | |
3832a4d8 AM |
5192 | if (m->p_type == PT_LOAD |
5193 | && m->count != 0 | |
5194 | && m->sections[0]->vma >= info->relro_start | |
5195 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 5196 | { |
3832a4d8 AM |
5197 | i = m->count; |
5198 | while (--i != (unsigned) -1) | |
5199 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5200 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5201 | break; | |
5202 | ||
43a8475c | 5203 | if (i != (unsigned) -1) |
f210dcff L |
5204 | break; |
5205 | } | |
be01b344 | 5206 | } |
f210dcff L |
5207 | |
5208 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
5209 | if (m != NULL) | |
5210 | { | |
5211 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 5212 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
5213 | if (m == NULL) |
5214 | goto error_return; | |
5215 | m->next = NULL; | |
5216 | m->p_type = PT_GNU_RELRO; | |
f210dcff L |
5217 | *pm = m; |
5218 | pm = &m->next; | |
5219 | } | |
8ded5a0f | 5220 | } |
9ee5e499 | 5221 | |
8ded5a0f | 5222 | free (sections); |
12bd6957 | 5223 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
5224 | } |
5225 | ||
3dea8fca | 5226 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 5227 | return FALSE; |
8c37241b | 5228 | |
12bd6957 | 5229 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 5230 | ++count; |
12bd6957 | 5231 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 5232 | |
b34976b6 | 5233 | return TRUE; |
252b5132 RH |
5234 | |
5235 | error_return: | |
5236 | if (sections != NULL) | |
5237 | free (sections); | |
b34976b6 | 5238 | return FALSE; |
252b5132 RH |
5239 | } |
5240 | ||
5241 | /* Sort sections by address. */ | |
5242 | ||
5243 | static int | |
217aa764 | 5244 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
5245 | { |
5246 | const asection *sec1 = *(const asection **) arg1; | |
5247 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 5248 | bfd_size_type size1, size2; |
252b5132 RH |
5249 | |
5250 | /* Sort by LMA first, since this is the address used to | |
5251 | place the section into a segment. */ | |
5252 | if (sec1->lma < sec2->lma) | |
5253 | return -1; | |
5254 | else if (sec1->lma > sec2->lma) | |
5255 | return 1; | |
5256 | ||
5257 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
5258 | the same, and this will do nothing. */ | |
5259 | if (sec1->vma < sec2->vma) | |
5260 | return -1; | |
5261 | else if (sec1->vma > sec2->vma) | |
5262 | return 1; | |
5263 | ||
5264 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
5265 | ||
07c6e936 | 5266 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
5267 | |
5268 | if (TOEND (sec1)) | |
5269 | { | |
48db3297 | 5270 | if (!TOEND (sec2)) |
252b5132 RH |
5271 | return 1; |
5272 | } | |
00a7cdc5 | 5273 | else if (TOEND (sec2)) |
252b5132 RH |
5274 | return -1; |
5275 | ||
5276 | #undef TOEND | |
5277 | ||
00a7cdc5 NC |
5278 | /* Sort by size, to put zero sized sections |
5279 | before others at the same address. */ | |
252b5132 | 5280 | |
eea6121a AM |
5281 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
5282 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
5283 | |
5284 | if (size1 < size2) | |
252b5132 | 5285 | return -1; |
eecdbe52 | 5286 | if (size1 > size2) |
252b5132 RH |
5287 | return 1; |
5288 | ||
5289 | return sec1->target_index - sec2->target_index; | |
5290 | } | |
5291 | ||
30fe1832 AM |
5292 | /* This qsort comparison functions sorts PT_LOAD segments first and |
5293 | by p_paddr, for assign_file_positions_for_load_sections. */ | |
5294 | ||
5295 | static int | |
5296 | elf_sort_segments (const void *arg1, const void *arg2) | |
5297 | { | |
5298 | const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1; | |
5299 | const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2; | |
5300 | ||
5301 | if (m1->p_type != m2->p_type) | |
5302 | { | |
5303 | if (m1->p_type == PT_NULL) | |
5304 | return 1; | |
5305 | if (m2->p_type == PT_NULL) | |
5306 | return -1; | |
5307 | return m1->p_type < m2->p_type ? -1 : 1; | |
5308 | } | |
5309 | if (m1->includes_filehdr != m2->includes_filehdr) | |
5310 | return m1->includes_filehdr ? -1 : 1; | |
5311 | if (m1->no_sort_lma != m2->no_sort_lma) | |
5312 | return m1->no_sort_lma ? -1 : 1; | |
5313 | if (m1->p_type == PT_LOAD && !m1->no_sort_lma) | |
5314 | { | |
66631823 CE |
5315 | unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner, |
5316 | m1->sections[0]); | |
5317 | bfd_vma lma1, lma2; /* Bytes. */ | |
30fe1832 AM |
5318 | lma1 = 0; |
5319 | if (m1->p_paddr_valid) | |
66631823 | 5320 | lma1 = m1->p_paddr / opb; |
30fe1832 AM |
5321 | else if (m1->count != 0) |
5322 | lma1 = m1->sections[0]->lma + m1->p_vaddr_offset; | |
5323 | lma2 = 0; | |
5324 | if (m2->p_paddr_valid) | |
66631823 | 5325 | lma2 = m2->p_paddr / opb; |
30fe1832 AM |
5326 | else if (m2->count != 0) |
5327 | lma2 = m2->sections[0]->lma + m2->p_vaddr_offset; | |
5328 | if (lma1 != lma2) | |
5329 | return lma1 < lma2 ? -1 : 1; | |
5330 | } | |
5331 | if (m1->idx != m2->idx) | |
5332 | return m1->idx < m2->idx ? -1 : 1; | |
5333 | return 0; | |
5334 | } | |
5335 | ||
340b6d91 AC |
5336 | /* Ian Lance Taylor writes: |
5337 | ||
5338 | We shouldn't be using % with a negative signed number. That's just | |
5339 | not good. We have to make sure either that the number is not | |
5340 | negative, or that the number has an unsigned type. When the types | |
5341 | are all the same size they wind up as unsigned. When file_ptr is a | |
5342 | larger signed type, the arithmetic winds up as signed long long, | |
5343 | which is wrong. | |
5344 | ||
5345 | What we're trying to say here is something like ``increase OFF by | |
5346 | the least amount that will cause it to be equal to the VMA modulo | |
5347 | the page size.'' */ | |
5348 | /* In other words, something like: | |
5349 | ||
5350 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
5351 | off_offset = off % bed->maxpagesize; | |
5352 | if (vma_offset < off_offset) | |
5353 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
5354 | else | |
5355 | adjustment = vma_offset - off_offset; | |
08a40648 | 5356 | |
de194d85 | 5357 | which can be collapsed into the expression below. */ |
340b6d91 AC |
5358 | |
5359 | static file_ptr | |
5360 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
5361 | { | |
dc9155b2 NC |
5362 | /* PR binutils/16199: Handle an alignment of zero. */ |
5363 | if (maxpagesize == 0) | |
5364 | maxpagesize = 1; | |
340b6d91 AC |
5365 | return ((vma - off) % maxpagesize); |
5366 | } | |
5367 | ||
6d33f217 L |
5368 | static void |
5369 | print_segment_map (const struct elf_segment_map *m) | |
5370 | { | |
5371 | unsigned int j; | |
5372 | const char *pt = get_segment_type (m->p_type); | |
5373 | char buf[32]; | |
5374 | ||
5375 | if (pt == NULL) | |
5376 | { | |
5377 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
5378 | sprintf (buf, "LOPROC+%7.7x", | |
5379 | (unsigned int) (m->p_type - PT_LOPROC)); | |
5380 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
5381 | sprintf (buf, "LOOS+%7.7x", | |
5382 | (unsigned int) (m->p_type - PT_LOOS)); | |
5383 | else | |
5384 | snprintf (buf, sizeof (buf), "%8.8x", | |
5385 | (unsigned int) m->p_type); | |
5386 | pt = buf; | |
5387 | } | |
4a97a0e5 | 5388 | fflush (stdout); |
6d33f217 L |
5389 | fprintf (stderr, "%s:", pt); |
5390 | for (j = 0; j < m->count; j++) | |
5391 | fprintf (stderr, " %s", m->sections [j]->name); | |
5392 | putc ('\n',stderr); | |
4a97a0e5 | 5393 | fflush (stderr); |
6d33f217 L |
5394 | } |
5395 | ||
32812159 AM |
5396 | static bfd_boolean |
5397 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
5398 | { | |
5399 | void *buf; | |
5400 | bfd_boolean ret; | |
5401 | ||
5402 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
5403 | return FALSE; | |
5404 | buf = bfd_zmalloc (len); | |
5405 | if (buf == NULL) | |
5406 | return FALSE; | |
5407 | ret = bfd_bwrite (buf, len, abfd) == len; | |
5408 | free (buf); | |
5409 | return ret; | |
5410 | } | |
5411 | ||
252b5132 RH |
5412 | /* Assign file positions to the sections based on the mapping from |
5413 | sections to segments. This function also sets up some fields in | |
f3520d2f | 5414 | the file header. */ |
252b5132 | 5415 | |
b34976b6 | 5416 | static bfd_boolean |
f3520d2f AM |
5417 | assign_file_positions_for_load_sections (bfd *abfd, |
5418 | struct bfd_link_info *link_info) | |
252b5132 RH |
5419 | { |
5420 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 5421 | struct elf_segment_map *m; |
30fe1832 | 5422 | struct elf_segment_map *phdr_load_seg; |
252b5132 | 5423 | Elf_Internal_Phdr *phdrs; |
252b5132 | 5424 | Elf_Internal_Phdr *p; |
502794d4 | 5425 | file_ptr off; /* Octets. */ |
3f570048 | 5426 | bfd_size_type maxpagesize; |
30fe1832 | 5427 | unsigned int alloc, actual; |
0920dee7 | 5428 | unsigned int i, j; |
30fe1832 | 5429 | struct elf_segment_map **sorted_seg_map; |
502794d4 | 5430 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
252b5132 | 5431 | |
e36284ab | 5432 | if (link_info == NULL |
ceae84aa | 5433 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 5434 | return FALSE; |
252b5132 | 5435 | |
8ded5a0f | 5436 | alloc = 0; |
12bd6957 | 5437 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
30fe1832 | 5438 | m->idx = alloc++; |
252b5132 | 5439 | |
82f2dbf7 NC |
5440 | if (alloc) |
5441 | { | |
5442 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
5443 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
5444 | } | |
5445 | else | |
5446 | { | |
5447 | /* PR binutils/12467. */ | |
5448 | elf_elfheader (abfd)->e_phoff = 0; | |
5449 | elf_elfheader (abfd)->e_phentsize = 0; | |
5450 | } | |
d324f6d6 | 5451 | |
8ded5a0f | 5452 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 5453 | |
12bd6957 | 5454 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
30fe1832 AM |
5455 | { |
5456 | actual = alloc; | |
5457 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
5458 | } | |
8ded5a0f | 5459 | else |
30fe1832 AM |
5460 | { |
5461 | actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr; | |
5462 | BFD_ASSERT (elf_program_header_size (abfd) | |
5463 | == actual * bed->s->sizeof_phdr); | |
5464 | BFD_ASSERT (actual >= alloc); | |
5465 | } | |
252b5132 RH |
5466 | |
5467 | if (alloc == 0) | |
f3520d2f | 5468 | { |
12bd6957 | 5469 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 5470 | return TRUE; |
f3520d2f | 5471 | } |
252b5132 | 5472 | |
12bd6957 | 5473 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
5474 | see assign_file_positions_except_relocs, so make sure we have |
5475 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
5476 | The variable alloc contains the computed need, while |
5477 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
5478 | layout. |
5479 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
5480 | where the layout is forced to according to a larger size in the | |
5481 | last iterations for the testcase ld-elf/header. */ | |
30fe1832 AM |
5482 | phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs) |
5483 | + alloc * sizeof (*sorted_seg_map))); | |
5484 | sorted_seg_map = (struct elf_segment_map **) (phdrs + actual); | |
f3520d2f | 5485 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 5486 | if (phdrs == NULL) |
b34976b6 | 5487 | return FALSE; |
252b5132 | 5488 | |
30fe1832 | 5489 | for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++) |
252b5132 | 5490 | { |
30fe1832 | 5491 | sorted_seg_map[j] = m; |
252b5132 | 5492 | /* If elf_segment_map is not from map_sections_to_segments, the |
08a40648 | 5493 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
5494 | not be done to the PT_NOTE section of a corefile, which may |
5495 | contain several pseudo-sections artificially created by bfd. | |
5496 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
5497 | if (m->count > 1 |
5498 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 5499 | && m->p_type == PT_NOTE)) |
48db3297 AM |
5500 | { |
5501 | for (i = 0; i < m->count; i++) | |
5502 | m->sections[i]->target_index = i; | |
5503 | qsort (m->sections, (size_t) m->count, sizeof (asection *), | |
5504 | elf_sort_sections); | |
5505 | } | |
30fe1832 AM |
5506 | } |
5507 | if (alloc > 1) | |
5508 | qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map), | |
5509 | elf_sort_segments); | |
5510 | ||
5511 | maxpagesize = 1; | |
5512 | if ((abfd->flags & D_PAGED) != 0) | |
5513 | maxpagesize = bed->maxpagesize; | |
5514 | ||
5515 | /* Sections must map to file offsets past the ELF file header. */ | |
5516 | off = bed->s->sizeof_ehdr; | |
5517 | /* And if one of the PT_LOAD headers doesn't include the program | |
5518 | headers then we'll be mapping program headers in the usual | |
5519 | position after the ELF file header. */ | |
5520 | phdr_load_seg = NULL; | |
5521 | for (j = 0; j < alloc; j++) | |
5522 | { | |
5523 | m = sorted_seg_map[j]; | |
5524 | if (m->p_type != PT_LOAD) | |
5525 | break; | |
5526 | if (m->includes_phdrs) | |
5527 | { | |
5528 | phdr_load_seg = m; | |
5529 | break; | |
5530 | } | |
5531 | } | |
5532 | if (phdr_load_seg == NULL) | |
5533 | off += actual * bed->s->sizeof_phdr; | |
5534 | ||
5535 | for (j = 0; j < alloc; j++) | |
5536 | { | |
5537 | asection **secpp; | |
502794d4 | 5538 | bfd_vma off_adjust; /* Octets. */ |
30fe1832 | 5539 | bfd_boolean no_contents; |
252b5132 | 5540 | |
b301b248 AM |
5541 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
5542 | number of sections with contents contributing to both p_filesz | |
5543 | and p_memsz, followed by a number of sections with no contents | |
5544 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 5545 | available file offset for PT_LOAD and PT_NOTE segments. */ |
30fe1832 AM |
5546 | m = sorted_seg_map[j]; |
5547 | p = phdrs + m->idx; | |
252b5132 | 5548 | p->p_type = m->p_type; |
28a7f3e7 | 5549 | p->p_flags = m->p_flags; |
252b5132 | 5550 | |
3f570048 | 5551 | if (m->count == 0) |
502794d4 | 5552 | p->p_vaddr = m->p_vaddr_offset * opb; |
3f570048 | 5553 | else |
502794d4 | 5554 | p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb; |
3f570048 AM |
5555 | |
5556 | if (m->p_paddr_valid) | |
5557 | p->p_paddr = m->p_paddr; | |
5558 | else if (m->count == 0) | |
5559 | p->p_paddr = 0; | |
5560 | else | |
502794d4 | 5561 | p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb; |
3f570048 AM |
5562 | |
5563 | if (p->p_type == PT_LOAD | |
5564 | && (abfd->flags & D_PAGED) != 0) | |
5565 | { | |
5566 | /* p_align in demand paged PT_LOAD segments effectively stores | |
5567 | the maximum page size. When copying an executable with | |
5568 | objcopy, we set m->p_align from the input file. Use this | |
5569 | value for maxpagesize rather than bed->maxpagesize, which | |
5570 | may be different. Note that we use maxpagesize for PT_TLS | |
5571 | segment alignment later in this function, so we are relying | |
5572 | on at least one PT_LOAD segment appearing before a PT_TLS | |
5573 | segment. */ | |
5574 | if (m->p_align_valid) | |
5575 | maxpagesize = m->p_align; | |
5576 | ||
5577 | p->p_align = maxpagesize; | |
5578 | } | |
3271a814 NS |
5579 | else if (m->p_align_valid) |
5580 | p->p_align = m->p_align; | |
e970b90a DJ |
5581 | else if (m->count == 0) |
5582 | p->p_align = 1 << bed->s->log_file_align; | |
30fe1832 AM |
5583 | |
5584 | if (m == phdr_load_seg) | |
5585 | { | |
5586 | if (!m->includes_filehdr) | |
5587 | p->p_offset = off; | |
5588 | off += actual * bed->s->sizeof_phdr; | |
5589 | } | |
3f570048 | 5590 | |
bf988460 AM |
5591 | no_contents = FALSE; |
5592 | off_adjust = 0; | |
252b5132 | 5593 | if (p->p_type == PT_LOAD |
b301b248 | 5594 | && m->count > 0) |
252b5132 | 5595 | { |
66631823 | 5596 | bfd_size_type align; /* Bytes. */ |
a49e53ed | 5597 | unsigned int align_power = 0; |
b301b248 | 5598 | |
3271a814 NS |
5599 | if (m->p_align_valid) |
5600 | align = p->p_align; | |
5601 | else | |
252b5132 | 5602 | { |
3271a814 NS |
5603 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5604 | { | |
5605 | unsigned int secalign; | |
08a40648 | 5606 | |
fd361982 | 5607 | secalign = bfd_section_alignment (*secpp); |
3271a814 NS |
5608 | if (secalign > align_power) |
5609 | align_power = secalign; | |
5610 | } | |
5611 | align = (bfd_size_type) 1 << align_power; | |
5612 | if (align < maxpagesize) | |
5613 | align = maxpagesize; | |
b301b248 | 5614 | } |
252b5132 | 5615 | |
02bf8d82 AM |
5616 | for (i = 0; i < m->count; i++) |
5617 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
5618 | /* If we aren't making room for this section, then | |
5619 | it must be SHT_NOBITS regardless of what we've | |
5620 | set via struct bfd_elf_special_section. */ | |
5621 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
5622 | ||
bf988460 | 5623 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
5624 | sections. */ |
5625 | no_contents = TRUE; | |
5626 | for (i = 0; i < m->count; i++) | |
5627 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
5628 | { | |
5629 | no_contents = FALSE; | |
5630 | break; | |
5631 | } | |
bf988460 | 5632 | |
66631823 | 5633 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb); |
a8c75b76 AM |
5634 | |
5635 | /* Broken hardware and/or kernel require that files do not | |
5636 | map the same page with different permissions on some hppa | |
5637 | processors. */ | |
30fe1832 AM |
5638 | if (j != 0 |
5639 | && (abfd->flags & D_PAGED) != 0 | |
a8c75b76 AM |
5640 | && bed->no_page_alias |
5641 | && (off & (maxpagesize - 1)) != 0 | |
502794d4 CE |
5642 | && ((off & -maxpagesize) |
5643 | == ((off + off_adjust) & -maxpagesize))) | |
a8c75b76 | 5644 | off_adjust += maxpagesize; |
bf988460 AM |
5645 | off += off_adjust; |
5646 | if (no_contents) | |
5647 | { | |
5648 | /* We shouldn't need to align the segment on disk since | |
5649 | the segment doesn't need file space, but the gABI | |
5650 | arguably requires the alignment and glibc ld.so | |
5651 | checks it. So to comply with the alignment | |
5652 | requirement but not waste file space, we adjust | |
5653 | p_offset for just this segment. (OFF_ADJUST is | |
5654 | subtracted from OFF later.) This may put p_offset | |
5655 | past the end of file, but that shouldn't matter. */ | |
5656 | } | |
5657 | else | |
5658 | off_adjust = 0; | |
252b5132 | 5659 | } |
b1a6d0b1 NC |
5660 | /* Make sure the .dynamic section is the first section in the |
5661 | PT_DYNAMIC segment. */ | |
5662 | else if (p->p_type == PT_DYNAMIC | |
5663 | && m->count > 1 | |
5664 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
5665 | { | |
5666 | _bfd_error_handler | |
871b3ab2 | 5667 | (_("%pB: The first section in the PT_DYNAMIC segment" |
63a5468a | 5668 | " is not the .dynamic section"), |
b301b248 | 5669 | abfd); |
b1a6d0b1 NC |
5670 | bfd_set_error (bfd_error_bad_value); |
5671 | return FALSE; | |
5672 | } | |
3f001e84 JK |
5673 | /* Set the note section type to SHT_NOTE. */ |
5674 | else if (p->p_type == PT_NOTE) | |
5675 | for (i = 0; i < m->count; i++) | |
5676 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 5677 | |
252b5132 RH |
5678 | if (m->includes_filehdr) |
5679 | { | |
bf988460 | 5680 | if (!m->p_flags_valid) |
252b5132 | 5681 | p->p_flags |= PF_R; |
252b5132 RH |
5682 | p->p_filesz = bed->s->sizeof_ehdr; |
5683 | p->p_memsz = bed->s->sizeof_ehdr; | |
30fe1832 | 5684 | if (p->p_type == PT_LOAD) |
252b5132 | 5685 | { |
30fe1832 | 5686 | if (m->count > 0) |
252b5132 | 5687 | { |
30fe1832 AM |
5688 | if (p->p_vaddr < (bfd_vma) off |
5689 | || (!m->p_paddr_valid | |
5690 | && p->p_paddr < (bfd_vma) off)) | |
5691 | { | |
5692 | _bfd_error_handler | |
5693 | (_("%pB: not enough room for program headers," | |
5694 | " try linking with -N"), | |
5695 | abfd); | |
5696 | bfd_set_error (bfd_error_bad_value); | |
5697 | return FALSE; | |
5698 | } | |
5699 | p->p_vaddr -= off; | |
5700 | if (!m->p_paddr_valid) | |
5701 | p->p_paddr -= off; | |
252b5132 | 5702 | } |
30fe1832 AM |
5703 | } |
5704 | else if (sorted_seg_map[0]->includes_filehdr) | |
5705 | { | |
5706 | Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx; | |
5707 | p->p_vaddr = filehdr->p_vaddr; | |
bf988460 | 5708 | if (!m->p_paddr_valid) |
30fe1832 | 5709 | p->p_paddr = filehdr->p_paddr; |
252b5132 | 5710 | } |
252b5132 RH |
5711 | } |
5712 | ||
5713 | if (m->includes_phdrs) | |
5714 | { | |
bf988460 | 5715 | if (!m->p_flags_valid) |
252b5132 | 5716 | p->p_flags |= PF_R; |
30fe1832 AM |
5717 | p->p_filesz += actual * bed->s->sizeof_phdr; |
5718 | p->p_memsz += actual * bed->s->sizeof_phdr; | |
f3520d2f | 5719 | if (!m->includes_filehdr) |
252b5132 | 5720 | { |
30fe1832 | 5721 | if (p->p_type == PT_LOAD) |
252b5132 | 5722 | { |
30fe1832 AM |
5723 | elf_elfheader (abfd)->e_phoff = p->p_offset; |
5724 | if (m->count > 0) | |
5725 | { | |
5726 | p->p_vaddr -= off - p->p_offset; | |
5727 | if (!m->p_paddr_valid) | |
5728 | p->p_paddr -= off - p->p_offset; | |
5729 | } | |
5730 | } | |
5731 | else if (phdr_load_seg != NULL) | |
5732 | { | |
5733 | Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx; | |
502794d4 | 5734 | bfd_vma phdr_off = 0; /* Octets. */ |
30fe1832 AM |
5735 | if (phdr_load_seg->includes_filehdr) |
5736 | phdr_off = bed->s->sizeof_ehdr; | |
5737 | p->p_vaddr = phdr->p_vaddr + phdr_off; | |
bf988460 | 5738 | if (!m->p_paddr_valid) |
30fe1832 AM |
5739 | p->p_paddr = phdr->p_paddr + phdr_off; |
5740 | p->p_offset = phdr->p_offset + phdr_off; | |
252b5132 | 5741 | } |
30fe1832 AM |
5742 | else |
5743 | p->p_offset = bed->s->sizeof_ehdr; | |
2b0bc088 | 5744 | } |
252b5132 RH |
5745 | } |
5746 | ||
5747 | if (p->p_type == PT_LOAD | |
5748 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
5749 | { | |
bf988460 | 5750 | if (!m->includes_filehdr && !m->includes_phdrs) |
0bc3450e AM |
5751 | { |
5752 | p->p_offset = off; | |
5753 | if (no_contents) | |
67641dd3 AM |
5754 | { |
5755 | /* Put meaningless p_offset for PT_LOAD segments | |
5756 | without file contents somewhere within the first | |
5757 | page, in an attempt to not point past EOF. */ | |
5758 | bfd_size_type align = maxpagesize; | |
5759 | if (align < p->p_align) | |
5760 | align = p->p_align; | |
5761 | if (align < 1) | |
5762 | align = 1; | |
5763 | p->p_offset = off % align; | |
5764 | } | |
0bc3450e | 5765 | } |
252b5132 RH |
5766 | else |
5767 | { | |
502794d4 | 5768 | file_ptr adjust; /* Octets. */ |
252b5132 RH |
5769 | |
5770 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
5771 | if (!no_contents) |
5772 | p->p_filesz += adjust; | |
252b5132 RH |
5773 | p->p_memsz += adjust; |
5774 | } | |
5775 | } | |
5776 | ||
1ea63fd2 AM |
5777 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
5778 | maps. Set filepos for sections in PT_LOAD segments, and in | |
5779 | core files, for sections in PT_NOTE segments. | |
5780 | assign_file_positions_for_non_load_sections will set filepos | |
5781 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
5782 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5783 | { | |
5784 | asection *sec; | |
252b5132 | 5785 | bfd_size_type align; |
627b32bc | 5786 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
5787 | |
5788 | sec = *secpp; | |
02bf8d82 | 5789 | this_hdr = &elf_section_data (sec)->this_hdr; |
fd361982 | 5790 | align = (bfd_size_type) 1 << bfd_section_alignment (sec); |
252b5132 | 5791 | |
88967714 AM |
5792 | if ((p->p_type == PT_LOAD |
5793 | || p->p_type == PT_TLS) | |
5794 | && (this_hdr->sh_type != SHT_NOBITS | |
5795 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
5796 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
5797 | || p->p_type == PT_TLS)))) | |
252b5132 | 5798 | { |
502794d4 CE |
5799 | bfd_vma p_start = p->p_paddr; /* Octets. */ |
5800 | bfd_vma p_end = p_start + p->p_memsz; /* Octets. */ | |
5801 | bfd_vma s_start = sec->lma * opb; /* Octets. */ | |
5802 | bfd_vma adjust = s_start - p_end; /* Octets. */ | |
252b5132 | 5803 | |
a2d1e028 L |
5804 | if (adjust != 0 |
5805 | && (s_start < p_end | |
5806 | || p_end < p_start)) | |
252b5132 | 5807 | { |
4eca0228 | 5808 | _bfd_error_handler |
695344c0 | 5809 | /* xgettext:c-format */ |
2dcf00ce | 5810 | (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64), |
502794d4 CE |
5811 | abfd, sec, (uint64_t) s_start / opb, |
5812 | (uint64_t) p_end / opb); | |
88967714 | 5813 | adjust = 0; |
502794d4 | 5814 | sec->lma = p_end / opb; |
1cfb7d1e | 5815 | } |
3ac9b6c9 | 5816 | p->p_memsz += adjust; |
1cfb7d1e | 5817 | |
88967714 AM |
5818 | if (this_hdr->sh_type != SHT_NOBITS) |
5819 | { | |
30fe1832 | 5820 | if (p->p_type == PT_LOAD) |
32812159 | 5821 | { |
30fe1832 AM |
5822 | if (p->p_filesz + adjust < p->p_memsz) |
5823 | { | |
5824 | /* We have a PROGBITS section following NOBITS ones. | |
5825 | Allocate file space for the NOBITS section(s) and | |
5826 | zero it. */ | |
5827 | adjust = p->p_memsz - p->p_filesz; | |
5828 | if (!write_zeros (abfd, off, adjust)) | |
5829 | return FALSE; | |
5830 | } | |
5831 | off += adjust; | |
32812159 | 5832 | } |
88967714 | 5833 | p->p_filesz += adjust; |
252b5132 | 5834 | } |
252b5132 RH |
5835 | } |
5836 | ||
5837 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5838 | { | |
b301b248 AM |
5839 | /* The section at i == 0 is the one that actually contains |
5840 | everything. */ | |
4a938328 MS |
5841 | if (i == 0) |
5842 | { | |
627b32bc | 5843 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5844 | off += this_hdr->sh_size; |
5845 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5846 | p->p_memsz = 0; |
5847 | p->p_align = 1; | |
252b5132 | 5848 | } |
4a938328 | 5849 | else |
252b5132 | 5850 | { |
b301b248 | 5851 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5852 | sec->filepos = 0; |
eea6121a | 5853 | sec->size = 0; |
b301b248 AM |
5854 | sec->flags = 0; |
5855 | continue; | |
252b5132 | 5856 | } |
252b5132 RH |
5857 | } |
5858 | else | |
5859 | { | |
1e951488 | 5860 | if (p->p_type == PT_LOAD) |
b301b248 | 5861 | { |
1e951488 AM |
5862 | this_hdr->sh_offset = sec->filepos = off; |
5863 | if (this_hdr->sh_type != SHT_NOBITS) | |
5864 | off += this_hdr->sh_size; | |
5865 | } | |
5866 | else if (this_hdr->sh_type == SHT_NOBITS | |
5867 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5868 | && this_hdr->sh_offset == 0) | |
5869 | { | |
5870 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5871 | (See _bfd_elf_map_sections_to_segments "Create a | |
5872 | final PT_LOAD".) Set sh_offset to the value it | |
5873 | would have if we had created a zero p_filesz and | |
5874 | p_memsz PT_LOAD header for the section. This | |
5875 | also makes the PT_TLS header have the same | |
5876 | p_offset value. */ | |
5877 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5878 | off, align); | |
5879 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5880 | } |
252b5132 | 5881 | |
02bf8d82 | 5882 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5883 | { |
6a3cd2b4 | 5884 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5885 | /* A load section without SHF_ALLOC is something like |
5886 | a note section in a PT_NOTE segment. These take | |
5887 | file space but are not loaded into memory. */ | |
5888 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5889 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5890 | } |
6a3cd2b4 | 5891 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5892 | { |
6a3cd2b4 AM |
5893 | if (p->p_type == PT_TLS) |
5894 | p->p_memsz += this_hdr->sh_size; | |
5895 | ||
5896 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5897 | normal segments. */ | |
5898 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5899 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5900 | } |
5901 | ||
b10a8ae0 L |
5902 | if (align > p->p_align |
5903 | && !m->p_align_valid | |
5904 | && (p->p_type != PT_LOAD | |
5905 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5906 | p->p_align = align; |
5907 | } | |
5908 | ||
bf988460 | 5909 | if (!m->p_flags_valid) |
252b5132 RH |
5910 | { |
5911 | p->p_flags |= PF_R; | |
02bf8d82 | 5912 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5913 | p->p_flags |= PF_X; |
02bf8d82 | 5914 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5915 | p->p_flags |= PF_W; |
5916 | } | |
5917 | } | |
43a8475c | 5918 | |
bf988460 | 5919 | off -= off_adjust; |
0920dee7 | 5920 | |
30fe1832 AM |
5921 | /* PR ld/20815 - Check that the program header segment, if |
5922 | present, will be loaded into memory. */ | |
5923 | if (p->p_type == PT_PHDR | |
5924 | && phdr_load_seg == NULL | |
5925 | && !(bed->elf_backend_allow_non_load_phdr != NULL | |
5926 | && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc))) | |
5927 | { | |
5928 | /* The fix for this error is usually to edit the linker script being | |
5929 | used and set up the program headers manually. Either that or | |
5930 | leave room for the headers at the start of the SECTIONS. */ | |
5931 | _bfd_error_handler (_("%pB: error: PHDR segment not covered" | |
5932 | " by LOAD segment"), | |
5933 | abfd); | |
7b3c2715 AM |
5934 | if (link_info == NULL) |
5935 | return FALSE; | |
5936 | /* Arrange for the linker to exit with an error, deleting | |
5937 | the output file unless --noinhibit-exec is given. */ | |
5938 | link_info->callbacks->info ("%X"); | |
30fe1832 AM |
5939 | } |
5940 | ||
7c928300 AM |
5941 | /* Check that all sections are in a PT_LOAD segment. |
5942 | Don't check funky gdb generated core files. */ | |
5943 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5944 | { |
5945 | bfd_boolean check_vma = TRUE; | |
5946 | ||
5947 | for (i = 1; i < m->count; i++) | |
5948 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5949 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5950 | ->this_hdr), p) != 0 | |
5951 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5952 | ->this_hdr), p) != 0) | |
0920dee7 | 5953 | { |
9a83a553 AM |
5954 | /* Looks like we have overlays packed into the segment. */ |
5955 | check_vma = FALSE; | |
5956 | break; | |
0920dee7 | 5957 | } |
9a83a553 AM |
5958 | |
5959 | for (i = 0; i < m->count; i++) | |
5960 | { | |
5961 | Elf_Internal_Shdr *this_hdr; | |
5962 | asection *sec; | |
5963 | ||
5964 | sec = m->sections[i]; | |
5965 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5966 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5967 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 | 5968 | { |
4eca0228 | 5969 | _bfd_error_handler |
695344c0 | 5970 | /* xgettext:c-format */ |
871b3ab2 | 5971 | (_("%pB: section `%pA' can't be allocated in segment %d"), |
9a83a553 AM |
5972 | abfd, sec, j); |
5973 | print_segment_map (m); | |
5974 | } | |
5975 | } | |
5976 | } | |
252b5132 RH |
5977 | } |
5978 | ||
12bd6957 | 5979 | elf_next_file_pos (abfd) = off; |
30fe1832 AM |
5980 | |
5981 | if (link_info != NULL | |
5982 | && phdr_load_seg != NULL | |
5983 | && phdr_load_seg->includes_filehdr) | |
5984 | { | |
5985 | /* There is a segment that contains both the file headers and the | |
5986 | program headers, so provide a symbol __ehdr_start pointing there. | |
5987 | A program can use this to examine itself robustly. */ | |
5988 | ||
5989 | struct elf_link_hash_entry *hash | |
5990 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5991 | FALSE, FALSE, TRUE); | |
5992 | /* If the symbol was referenced and not defined, define it. */ | |
5993 | if (hash != NULL | |
5994 | && (hash->root.type == bfd_link_hash_new | |
5995 | || hash->root.type == bfd_link_hash_undefined | |
5996 | || hash->root.type == bfd_link_hash_undefweak | |
5997 | || hash->root.type == bfd_link_hash_common)) | |
5998 | { | |
5999 | asection *s = NULL; | |
66631823 | 6000 | bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb; |
30fe1832 AM |
6001 | |
6002 | if (phdr_load_seg->count != 0) | |
6003 | /* The segment contains sections, so use the first one. */ | |
6004 | s = phdr_load_seg->sections[0]; | |
6005 | else | |
6006 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
6007 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) | |
6008 | if (m->p_type == PT_LOAD && m->count != 0) | |
6009 | { | |
6010 | s = m->sections[0]; | |
6011 | break; | |
6012 | } | |
6013 | ||
6014 | if (s != NULL) | |
6015 | { | |
6016 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
6017 | hash->root.u.def.section = s; | |
6018 | } | |
6019 | else | |
6020 | { | |
6021 | hash->root.u.def.value = filehdr_vaddr; | |
6022 | hash->root.u.def.section = bfd_abs_section_ptr; | |
6023 | } | |
6024 | ||
6025 | hash->root.type = bfd_link_hash_defined; | |
6026 | hash->def_regular = 1; | |
6027 | hash->non_elf = 0; | |
6028 | } | |
6029 | } | |
6030 | ||
f3520d2f AM |
6031 | return TRUE; |
6032 | } | |
6033 | ||
1faa385f NC |
6034 | /* Determine if a bfd is a debuginfo file. Unfortunately there |
6035 | is no defined method for detecting such files, so we have to | |
6036 | use heuristics instead. */ | |
6037 | ||
6038 | bfd_boolean | |
6039 | is_debuginfo_file (bfd *abfd) | |
6040 | { | |
6041 | if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
6042 | return FALSE; | |
6043 | ||
6044 | Elf_Internal_Shdr **start_headers = elf_elfsections (abfd); | |
6045 | Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd); | |
6046 | Elf_Internal_Shdr **headerp; | |
6047 | ||
6048 | for (headerp = start_headers; headerp < end_headers; headerp ++) | |
6049 | { | |
6050 | Elf_Internal_Shdr *header = * headerp; | |
6051 | ||
6052 | /* Debuginfo files do not have any allocated SHT_PROGBITS sections. | |
6053 | The only allocated sections are SHT_NOBITS or SHT_NOTES. */ | |
6054 | if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC | |
6055 | && header->sh_type != SHT_NOBITS | |
6056 | && header->sh_type != SHT_NOTE) | |
6057 | return FALSE; | |
6058 | } | |
6059 | ||
6060 | return TRUE; | |
6061 | } | |
6062 | ||
1ff6de03 NA |
6063 | /* Assign file positions for the other sections, except for compressed debugging |
6064 | and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */ | |
f3520d2f AM |
6065 | |
6066 | static bfd_boolean | |
6067 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
6068 | struct bfd_link_info *link_info) | |
6069 | { | |
6070 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
6071 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 6072 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
6073 | Elf_Internal_Phdr *phdrs; |
6074 | Elf_Internal_Phdr *p; | |
6075 | struct elf_segment_map *m; | |
f3520d2f | 6076 | file_ptr off; |
66631823 | 6077 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
f3520d2f | 6078 | |
5c182d5f | 6079 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 6080 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 6081 | off = elf_next_file_pos (abfd); |
e06efbf1 | 6082 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 6083 | { |
5c182d5f AM |
6084 | Elf_Internal_Shdr *hdr; |
6085 | ||
6086 | hdr = *hdrpp; | |
6087 | if (hdr->bfd_section != NULL | |
252e386e AM |
6088 | && (hdr->bfd_section->filepos != 0 |
6089 | || (hdr->sh_type == SHT_NOBITS | |
6090 | && hdr->contents == NULL))) | |
627b32bc | 6091 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
6092 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
6093 | { | |
1faa385f NC |
6094 | if (hdr->sh_size != 0 |
6095 | /* PR 24717 - debuginfo files are known to be not strictly | |
6096 | compliant with the ELF standard. In particular they often | |
6097 | have .note.gnu.property sections that are outside of any | |
6098 | loadable segment. This is not a problem for such files, | |
6099 | so do not warn about them. */ | |
6100 | && ! is_debuginfo_file (abfd)) | |
4eca0228 | 6101 | _bfd_error_handler |
695344c0 | 6102 | /* xgettext:c-format */ |
871b3ab2 | 6103 | (_("%pB: warning: allocated section `%s' not in segment"), |
e8d2ba53 AM |
6104 | abfd, |
6105 | (hdr->bfd_section == NULL | |
6106 | ? "*unknown*" | |
6107 | : hdr->bfd_section->name)); | |
3ba71138 L |
6108 | /* We don't need to page align empty sections. */ |
6109 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
6110 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
6111 | bed->maxpagesize); | |
6112 | else | |
6113 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
6114 | hdr->sh_addralign); | |
6115 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
6116 | FALSE); | |
6117 | } | |
6118 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
6119 | && hdr->bfd_section == NULL) | |
1ff6de03 NA |
6120 | /* We don't know the offset of these sections yet: their size has |
6121 | not been decided. */ | |
0ce398f1 | 6122 | || (hdr->bfd_section != NULL |
1ff6de03 NA |
6123 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS |
6124 | || (bfd_section_is_ctf (hdr->bfd_section) | |
6125 | && abfd->is_linker_output))) | |
12bd6957 | 6126 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
6a40cf0c NC |
6127 | || (elf_symtab_shndx_list (abfd) != NULL |
6128 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
6129 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
6130 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
6131 | hdr->sh_offset = -1; |
6132 | else | |
6133 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f | 6134 | } |
30fe1832 | 6135 | elf_next_file_pos (abfd) = off; |
5c182d5f | 6136 | |
252b5132 RH |
6137 | /* Now that we have set the section file positions, we can set up |
6138 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f | 6139 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 6140 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 6141 | { |
129af99f | 6142 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 6143 | { |
66631823 | 6144 | bfd_vma start, end; /* Bytes. */ |
01f7e10c | 6145 | bfd_boolean ok; |
1ea63fd2 | 6146 | |
129af99f | 6147 | if (link_info != NULL) |
8c37241b | 6148 | { |
129af99f | 6149 | /* During linking the range of the RELRO segment is passed |
f2731e0c AM |
6150 | in link_info. Note that there may be padding between |
6151 | relro_start and the first RELRO section. */ | |
6152 | start = link_info->relro_start; | |
6153 | end = link_info->relro_end; | |
6154 | } | |
6155 | else if (m->count != 0) | |
6156 | { | |
6157 | if (!m->p_size_valid) | |
6158 | abort (); | |
6159 | start = m->sections[0]->vma; | |
66631823 | 6160 | end = start + m->p_size / opb; |
f2731e0c AM |
6161 | } |
6162 | else | |
6163 | { | |
6164 | start = 0; | |
6165 | end = 0; | |
6166 | } | |
6167 | ||
01f7e10c | 6168 | ok = FALSE; |
f2731e0c AM |
6169 | if (start < end) |
6170 | { | |
6171 | struct elf_segment_map *lm; | |
6172 | const Elf_Internal_Phdr *lp; | |
6173 | unsigned int i; | |
6174 | ||
6175 | /* Find a LOAD segment containing a section in the RELRO | |
6176 | segment. */ | |
12bd6957 | 6177 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
6178 | lm != NULL; |
6179 | lm = lm->next, lp++) | |
8c37241b JJ |
6180 | { |
6181 | if (lp->p_type == PT_LOAD | |
3146fac4 | 6182 | && lm->count != 0 |
dbc88fc1 AM |
6183 | && (lm->sections[lm->count - 1]->vma |
6184 | + (!IS_TBSS (lm->sections[lm->count - 1]) | |
66631823 | 6185 | ? lm->sections[lm->count - 1]->size / opb |
dbc88fc1 | 6186 | : 0)) > start |
f2731e0c | 6187 | && lm->sections[0]->vma < end) |
8c37241b JJ |
6188 | break; |
6189 | } | |
f2731e0c | 6190 | |
01f7e10c | 6191 | if (lm != NULL) |
129af99f | 6192 | { |
01f7e10c AM |
6193 | /* Find the section starting the RELRO segment. */ |
6194 | for (i = 0; i < lm->count; i++) | |
6195 | { | |
6196 | asection *s = lm->sections[i]; | |
6197 | if (s->vma >= start | |
6198 | && s->vma < end | |
6199 | && s->size != 0) | |
6200 | break; | |
6201 | } | |
6202 | ||
6203 | if (i < lm->count) | |
6204 | { | |
502794d4 CE |
6205 | p->p_vaddr = lm->sections[i]->vma * opb; |
6206 | p->p_paddr = lm->sections[i]->lma * opb; | |
01f7e10c | 6207 | p->p_offset = lm->sections[i]->filepos; |
66631823 | 6208 | p->p_memsz = end * opb - p->p_vaddr; |
01f7e10c AM |
6209 | p->p_filesz = p->p_memsz; |
6210 | ||
6211 | /* The RELRO segment typically ends a few bytes | |
6212 | into .got.plt but other layouts are possible. | |
6213 | In cases where the end does not match any | |
6214 | loaded section (for instance is in file | |
6215 | padding), trim p_filesz back to correspond to | |
6216 | the end of loaded section contents. */ | |
6217 | if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr) | |
6218 | p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr; | |
6219 | ||
6220 | /* Preserve the alignment and flags if they are | |
6221 | valid. The gold linker generates RW/4 for | |
6222 | the PT_GNU_RELRO section. It is better for | |
6223 | objcopy/strip to honor these attributes | |
6224 | otherwise gdb will choke when using separate | |
6225 | debug files. */ | |
6226 | if (!m->p_align_valid) | |
6227 | p->p_align = 1; | |
6228 | if (!m->p_flags_valid) | |
6229 | p->p_flags = PF_R; | |
6230 | ok = TRUE; | |
6231 | } | |
129af99f | 6232 | } |
b84a33b5 | 6233 | } |
01f7e10c AM |
6234 | if (link_info != NULL) |
6235 | BFD_ASSERT (ok); | |
6236 | if (!ok) | |
6237 | memset (p, 0, sizeof *p); | |
129af99f | 6238 | } |
04c3a755 NS |
6239 | else if (p->p_type == PT_GNU_STACK) |
6240 | { | |
6241 | if (m->p_size_valid) | |
6242 | p->p_memsz = m->p_size; | |
6243 | } | |
129af99f AS |
6244 | else if (m->count != 0) |
6245 | { | |
e06efbf1 | 6246 | unsigned int i; |
1a9ccd70 | 6247 | |
129af99f AS |
6248 | if (p->p_type != PT_LOAD |
6249 | && (p->p_type != PT_NOTE | |
6250 | || bfd_get_format (abfd) != bfd_core)) | |
6251 | { | |
1a9ccd70 NC |
6252 | /* A user specified segment layout may include a PHDR |
6253 | segment that overlaps with a LOAD segment... */ | |
6254 | if (p->p_type == PT_PHDR) | |
6255 | { | |
6256 | m->count = 0; | |
6257 | continue; | |
6258 | } | |
6259 | ||
c86934ce NC |
6260 | if (m->includes_filehdr || m->includes_phdrs) |
6261 | { | |
b1fa9dd6 | 6262 | /* PR 17512: file: 2195325e. */ |
4eca0228 | 6263 | _bfd_error_handler |
871b3ab2 | 6264 | (_("%pB: error: non-load segment %d includes file header " |
76cfced5 AM |
6265 | "and/or program header"), |
6266 | abfd, (int) (p - phdrs)); | |
c86934ce NC |
6267 | return FALSE; |
6268 | } | |
129af99f | 6269 | |
86b2281f | 6270 | p->p_filesz = 0; |
129af99f | 6271 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
6272 | for (i = m->count; i-- != 0;) |
6273 | { | |
6274 | asection *sect = m->sections[i]; | |
6275 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
6276 | if (hdr->sh_type != SHT_NOBITS) | |
6277 | { | |
6278 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
6279 | + hdr->sh_size); | |
6280 | break; | |
6281 | } | |
6282 | } | |
129af99f AS |
6283 | } |
6284 | } | |
252b5132 RH |
6285 | } |
6286 | ||
b34976b6 | 6287 | return TRUE; |
252b5132 RH |
6288 | } |
6289 | ||
6a40cf0c NC |
6290 | static elf_section_list * |
6291 | find_section_in_list (unsigned int i, elf_section_list * list) | |
6292 | { | |
6293 | for (;list != NULL; list = list->next) | |
6294 | if (list->ndx == i) | |
6295 | break; | |
6296 | return list; | |
6297 | } | |
6298 | ||
252b5132 RH |
6299 | /* Work out the file positions of all the sections. This is called by |
6300 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
6301 | VMAs must be known before this is called. | |
6302 | ||
e0638f70 | 6303 | Reloc sections come in two flavours: Those processed specially as |
1ff6de03 NA |
6304 | "side-channel" data attached to a section to which they apply, and those that |
6305 | bfd doesn't process as relocations. The latter sort are stored in a normal | |
6306 | bfd section by bfd_section_from_shdr. We don't consider the former sort | |
6307 | here, unless they form part of the loadable image. Reloc sections not | |
6308 | assigned here (and compressed debugging sections and CTF sections which | |
6309 | nothing else in the file can rely upon) will be handled later by | |
e0638f70 | 6310 | assign_file_positions_for_relocs. |
252b5132 RH |
6311 | |
6312 | We also don't set the positions of the .symtab and .strtab here. */ | |
6313 | ||
b34976b6 | 6314 | static bfd_boolean |
c84fca4d AO |
6315 | assign_file_positions_except_relocs (bfd *abfd, |
6316 | struct bfd_link_info *link_info) | |
252b5132 | 6317 | { |
5c182d5f AM |
6318 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
6319 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 6320 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6d6c25c8 | 6321 | unsigned int alloc; |
252b5132 RH |
6322 | |
6323 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
6324 | && bfd_get_format (abfd) != bfd_core) | |
6325 | { | |
5c182d5f AM |
6326 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
6327 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
6328 | Elf_Internal_Shdr **hdrpp; |
6329 | unsigned int i; | |
a485e98e | 6330 | file_ptr off; |
252b5132 RH |
6331 | |
6332 | /* Start after the ELF header. */ | |
6333 | off = i_ehdrp->e_ehsize; | |
6334 | ||
6335 | /* We are not creating an executable, which means that we are | |
6336 | not creating a program header, and that the actual order of | |
6337 | the sections in the file is unimportant. */ | |
9ad5cbcf | 6338 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
6339 | { |
6340 | Elf_Internal_Shdr *hdr; | |
6341 | ||
6342 | hdr = *hdrpp; | |
e0638f70 AM |
6343 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
6344 | && hdr->bfd_section == NULL) | |
1ff6de03 NA |
6345 | /* Do not assign offsets for these sections yet: we don't know |
6346 | their sizes. */ | |
0ce398f1 | 6347 | || (hdr->bfd_section != NULL |
1ff6de03 NA |
6348 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS |
6349 | || (bfd_section_is_ctf (hdr->bfd_section) | |
6350 | && abfd->is_linker_output))) | |
12bd6957 | 6351 | || i == elf_onesymtab (abfd) |
6a40cf0c NC |
6352 | || (elf_symtab_shndx_list (abfd) != NULL |
6353 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
6354 | || i == elf_strtab_sec (abfd) |
6355 | || i == elf_shstrtab_sec (abfd)) | |
252b5132 RH |
6356 | { |
6357 | hdr->sh_offset = -1; | |
252b5132 | 6358 | } |
9ad5cbcf | 6359 | else |
b34976b6 | 6360 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 6361 | } |
a485e98e AM |
6362 | |
6363 | elf_next_file_pos (abfd) = off; | |
6d6c25c8 | 6364 | elf_program_header_size (abfd) = 0; |
252b5132 RH |
6365 | } |
6366 | else | |
6367 | { | |
252b5132 | 6368 | /* Assign file positions for the loaded sections based on the |
08a40648 | 6369 | assignment of sections to segments. */ |
f3520d2f AM |
6370 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
6371 | return FALSE; | |
6372 | ||
6373 | /* And for non-load sections. */ | |
6374 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
6375 | return FALSE; | |
6d6c25c8 | 6376 | } |
f3520d2f | 6377 | |
6d6c25c8 AM |
6378 | if (!(*bed->elf_backend_modify_headers) (abfd, link_info)) |
6379 | return FALSE; | |
1a9ccd70 | 6380 | |
6d6c25c8 AM |
6381 | /* Write out the program headers. */ |
6382 | alloc = i_ehdrp->e_phnum; | |
6383 | if (alloc != 0) | |
6384 | { | |
30fe1832 | 6385 | if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0 |
cd584857 NC |
6386 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) |
6387 | return FALSE; | |
252b5132 RH |
6388 | } |
6389 | ||
b34976b6 | 6390 | return TRUE; |
252b5132 RH |
6391 | } |
6392 | ||
ed7e9d0b AM |
6393 | bfd_boolean |
6394 | _bfd_elf_init_file_header (bfd *abfd, | |
6395 | struct bfd_link_info *info ATTRIBUTE_UNUSED) | |
252b5132 | 6396 | { |
3d540e93 | 6397 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 6398 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 6399 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
6400 | |
6401 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 6402 | |
2b0f7ef9 | 6403 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 6404 | if (shstrtab == NULL) |
b34976b6 | 6405 | return FALSE; |
252b5132 RH |
6406 | |
6407 | elf_shstrtab (abfd) = shstrtab; | |
6408 | ||
6409 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
6410 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
6411 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
6412 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
6413 | ||
6414 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
6415 | i_ehdrp->e_ident[EI_DATA] = | |
6416 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
6417 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
6418 | ||
252b5132 RH |
6419 | if ((abfd->flags & DYNAMIC) != 0) |
6420 | i_ehdrp->e_type = ET_DYN; | |
6421 | else if ((abfd->flags & EXEC_P) != 0) | |
6422 | i_ehdrp->e_type = ET_EXEC; | |
6423 | else if (bfd_get_format (abfd) == bfd_core) | |
6424 | i_ehdrp->e_type = ET_CORE; | |
6425 | else | |
6426 | i_ehdrp->e_type = ET_REL; | |
6427 | ||
6428 | switch (bfd_get_arch (abfd)) | |
6429 | { | |
6430 | case bfd_arch_unknown: | |
6431 | i_ehdrp->e_machine = EM_NONE; | |
6432 | break; | |
aa4f99bb AO |
6433 | |
6434 | /* There used to be a long list of cases here, each one setting | |
6435 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
6436 | in the corresponding bfd definition. To avoid duplication, | |
6437 | the switch was removed. Machines that need special handling | |
6438 | can generally do it in elf_backend_final_write_processing(), | |
6439 | unless they need the information earlier than the final write. | |
6440 | Such need can generally be supplied by replacing the tests for | |
6441 | e_machine with the conditions used to determine it. */ | |
252b5132 | 6442 | default: |
9c5bfbb7 AM |
6443 | i_ehdrp->e_machine = bed->elf_machine_code; |
6444 | } | |
aa4f99bb | 6445 | |
252b5132 RH |
6446 | i_ehdrp->e_version = bed->s->ev_current; |
6447 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
6448 | ||
c044fabd | 6449 | /* No program header, for now. */ |
252b5132 RH |
6450 | i_ehdrp->e_phoff = 0; |
6451 | i_ehdrp->e_phentsize = 0; | |
6452 | i_ehdrp->e_phnum = 0; | |
6453 | ||
c044fabd | 6454 | /* Each bfd section is section header entry. */ |
252b5132 RH |
6455 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
6456 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
6457 | ||
252b5132 | 6458 | elf_tdata (abfd)->symtab_hdr.sh_name = |
b34976b6 | 6459 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 6460 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 6461 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 6462 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 6463 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 6464 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 6465 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 6466 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 6467 | return FALSE; |
252b5132 | 6468 | |
b34976b6 | 6469 | return TRUE; |
252b5132 RH |
6470 | } |
6471 | ||
6d6c25c8 AM |
6472 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. |
6473 | ||
6474 | FIXME: We used to have code here to sort the PT_LOAD segments into | |
6475 | ascending order, as per the ELF spec. But this breaks some programs, | |
6476 | including the Linux kernel. But really either the spec should be | |
6477 | changed or the programs updated. */ | |
6478 | ||
6479 | bfd_boolean | |
6480 | _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info) | |
6481 | { | |
6482 | if (link_info != NULL && bfd_link_pie (link_info)) | |
6483 | { | |
6484 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd); | |
6485 | unsigned int num_segments = i_ehdrp->e_phnum; | |
6486 | struct elf_obj_tdata *tdata = elf_tdata (obfd); | |
6487 | Elf_Internal_Phdr *segment = tdata->phdr; | |
6488 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
6489 | ||
6490 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
6491 | bfd_vma p_vaddr = (bfd_vma) -1; | |
6492 | for (; segment < end_segment; segment++) | |
6493 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
6494 | p_vaddr = segment->p_vaddr; | |
6495 | ||
6496 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
6497 | segments is non-zero. */ | |
6498 | if (p_vaddr) | |
6499 | i_ehdrp->e_type = ET_EXEC; | |
6500 | } | |
6501 | return TRUE; | |
6502 | } | |
6503 | ||
252b5132 | 6504 | /* Assign file positions for all the reloc sections which are not part |
a485e98e | 6505 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 6506 | |
0ce398f1 L |
6507 | static bfd_boolean |
6508 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
6509 | { |
6510 | file_ptr off; | |
e06efbf1 | 6511 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
3e19fb8f | 6512 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
6513 | Elf_Internal_Ehdr *i_ehdrp; |
6514 | const struct elf_backend_data *bed; | |
252b5132 | 6515 | |
12bd6957 | 6516 | off = elf_next_file_pos (abfd); |
252b5132 | 6517 | |
e06efbf1 L |
6518 | shdrpp = elf_elfsections (abfd); |
6519 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
6520 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 6521 | { |
252b5132 | 6522 | shdrp = *shdrpp; |
0ce398f1 L |
6523 | if (shdrp->sh_offset == -1) |
6524 | { | |
3e19fb8f | 6525 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
6526 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
6527 | || shdrp->sh_type == SHT_RELA); | |
1ff6de03 | 6528 | bfd_boolean is_ctf = sec && bfd_section_is_ctf (sec); |
0ce398f1 | 6529 | if (is_rel |
1ff6de03 | 6530 | || is_ctf |
3e19fb8f | 6531 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 | 6532 | { |
1ff6de03 | 6533 | if (!is_rel && !is_ctf) |
0ce398f1 | 6534 | { |
3e19fb8f L |
6535 | const char *name = sec->name; |
6536 | struct bfd_elf_section_data *d; | |
6537 | ||
0ce398f1 | 6538 | /* Compress DWARF debug sections. */ |
3e19fb8f | 6539 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
6540 | shdrp->contents)) |
6541 | return FALSE; | |
3e19fb8f L |
6542 | |
6543 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
6544 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
6545 | { | |
6546 | /* If section is compressed with zlib-gnu, convert | |
6547 | section name from .debug_* to .zdebug_*. */ | |
6548 | char *new_name | |
6549 | = convert_debug_to_zdebug (abfd, name); | |
6550 | if (new_name == NULL) | |
6551 | return FALSE; | |
6552 | name = new_name; | |
6553 | } | |
dd905818 | 6554 | /* Add section name to section name section. */ |
3e19fb8f L |
6555 | if (shdrp->sh_name != (unsigned int) -1) |
6556 | abort (); | |
6557 | shdrp->sh_name | |
6558 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
6559 | name, FALSE); | |
6560 | d = elf_section_data (sec); | |
6561 | ||
dd905818 | 6562 | /* Add reloc section name to section name section. */ |
3e19fb8f L |
6563 | if (d->rel.hdr |
6564 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6565 | d->rel.hdr, | |
6566 | name, FALSE)) | |
6567 | return FALSE; | |
6568 | if (d->rela.hdr | |
6569 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6570 | d->rela.hdr, | |
91cb26da | 6571 | name, TRUE)) |
3e19fb8f L |
6572 | return FALSE; |
6573 | ||
0ce398f1 | 6574 | /* Update section size and contents. */ |
3e19fb8f L |
6575 | shdrp->sh_size = sec->size; |
6576 | shdrp->contents = sec->contents; | |
0ce398f1 L |
6577 | shdrp->bfd_section->contents = NULL; |
6578 | } | |
1ff6de03 NA |
6579 | else if (is_ctf) |
6580 | { | |
6581 | /* Update section size and contents. */ | |
6582 | shdrp->sh_size = sec->size; | |
6583 | shdrp->contents = sec->contents; | |
6584 | } | |
6585 | ||
0ce398f1 L |
6586 | off = _bfd_elf_assign_file_position_for_section (shdrp, |
6587 | off, | |
6588 | TRUE); | |
6589 | } | |
6590 | } | |
252b5132 RH |
6591 | } |
6592 | ||
3e19fb8f L |
6593 | /* Place section name section after DWARF debug sections have been |
6594 | compressed. */ | |
6595 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
6596 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
6597 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
6598 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
6599 | ||
6600 | /* Place the section headers. */ | |
a485e98e AM |
6601 | i_ehdrp = elf_elfheader (abfd); |
6602 | bed = get_elf_backend_data (abfd); | |
6603 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
6604 | i_ehdrp->e_shoff = off; | |
6605 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 6606 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
6607 | |
6608 | return TRUE; | |
252b5132 RH |
6609 | } |
6610 | ||
b34976b6 | 6611 | bfd_boolean |
217aa764 | 6612 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 6613 | { |
9c5bfbb7 | 6614 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6615 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 6616 | bfd_boolean failed; |
9ad5cbcf | 6617 | unsigned int count, num_sec; |
30e8ee25 | 6618 | struct elf_obj_tdata *t; |
252b5132 RH |
6619 | |
6620 | if (! abfd->output_has_begun | |
217aa764 | 6621 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 6622 | return FALSE; |
db727370 JL |
6623 | /* Do not rewrite ELF data when the BFD has been opened for update. |
6624 | abfd->output_has_begun was set to TRUE on opening, so creation of new | |
6625 | sections, and modification of existing section sizes was restricted. | |
6626 | This means the ELF header, program headers and section headers can't have | |
6627 | changed. | |
6628 | If the contents of any sections has been modified, then those changes have | |
6629 | already been written to the BFD. */ | |
6630 | else if (abfd->direction == both_direction) | |
6631 | { | |
6632 | BFD_ASSERT (abfd->output_has_begun); | |
6633 | return TRUE; | |
6634 | } | |
252b5132 RH |
6635 | |
6636 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 6637 | |
b34976b6 | 6638 | failed = FALSE; |
252b5132 RH |
6639 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
6640 | if (failed) | |
b34976b6 | 6641 | return FALSE; |
252b5132 | 6642 | |
0ce398f1 L |
6643 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
6644 | return FALSE; | |
252b5132 | 6645 | |
c044fabd | 6646 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
6647 | num_sec = elf_numsections (abfd); |
6648 | for (count = 1; count < num_sec; count++) | |
252b5132 | 6649 | { |
3e19fb8f L |
6650 | i_shdrp[count]->sh_name |
6651 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
6652 | i_shdrp[count]->sh_name); | |
252b5132 | 6653 | if (bed->elf_backend_section_processing) |
75506100 MR |
6654 | if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count])) |
6655 | return FALSE; | |
252b5132 RH |
6656 | if (i_shdrp[count]->contents) |
6657 | { | |
dc810e39 AM |
6658 | bfd_size_type amt = i_shdrp[count]->sh_size; |
6659 | ||
252b5132 | 6660 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 6661 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 6662 | return FALSE; |
252b5132 RH |
6663 | } |
6664 | } | |
6665 | ||
6666 | /* Write out the section header names. */ | |
30e8ee25 | 6667 | t = elf_tdata (abfd); |
26ae6d5e | 6668 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 6669 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 6670 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 6671 | return FALSE; |
252b5132 | 6672 | |
cc364be6 AM |
6673 | if (!(*bed->elf_backend_final_write_processing) (abfd)) |
6674 | return FALSE; | |
252b5132 | 6675 | |
ff59fc36 RM |
6676 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
6677 | return FALSE; | |
6678 | ||
6679 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
6680 | if (t->o->build_id.after_write_object_contents != NULL) |
6681 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
6682 | |
6683 | return TRUE; | |
252b5132 RH |
6684 | } |
6685 | ||
b34976b6 | 6686 | bfd_boolean |
217aa764 | 6687 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 6688 | { |
c044fabd | 6689 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
6690 | return _bfd_elf_write_object_contents (abfd); |
6691 | } | |
c044fabd KH |
6692 | |
6693 | /* Given a section, search the header to find them. */ | |
6694 | ||
cb33740c | 6695 | unsigned int |
198beae2 | 6696 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 6697 | { |
9c5bfbb7 | 6698 | const struct elf_backend_data *bed; |
91d6fa6a | 6699 | unsigned int sec_index; |
252b5132 | 6700 | |
9ad5cbcf AM |
6701 | if (elf_section_data (asect) != NULL |
6702 | && elf_section_data (asect)->this_idx != 0) | |
6703 | return elf_section_data (asect)->this_idx; | |
6704 | ||
6705 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 6706 | sec_index = SHN_ABS; |
af746e92 | 6707 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 6708 | sec_index = SHN_COMMON; |
af746e92 | 6709 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 6710 | sec_index = SHN_UNDEF; |
af746e92 | 6711 | else |
91d6fa6a | 6712 | sec_index = SHN_BAD; |
252b5132 | 6713 | |
af746e92 | 6714 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
6715 | if (bed->elf_backend_section_from_bfd_section) |
6716 | { | |
91d6fa6a | 6717 | int retval = sec_index; |
9ad5cbcf | 6718 | |
af746e92 AM |
6719 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
6720 | return retval; | |
252b5132 RH |
6721 | } |
6722 | ||
91d6fa6a | 6723 | if (sec_index == SHN_BAD) |
af746e92 | 6724 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 6725 | |
91d6fa6a | 6726 | return sec_index; |
252b5132 RH |
6727 | } |
6728 | ||
6729 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
6730 | on error. */ | |
6731 | ||
6732 | int | |
217aa764 | 6733 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
6734 | { |
6735 | asymbol *asym_ptr = *asym_ptr_ptr; | |
6736 | int idx; | |
6737 | flagword flags = asym_ptr->flags; | |
6738 | ||
6739 | /* When gas creates relocations against local labels, it creates its | |
6740 | own symbol for the section, but does put the symbol into the | |
6741 | symbol chain, so udata is 0. When the linker is generating | |
6742 | relocatable output, this section symbol may be for one of the | |
6743 | input sections rather than the output section. */ | |
6744 | if (asym_ptr->udata.i == 0 | |
6745 | && (flags & BSF_SECTION_SYM) | |
6746 | && asym_ptr->section) | |
6747 | { | |
5372391b | 6748 | asection *sec; |
252b5132 RH |
6749 | int indx; |
6750 | ||
5372391b AM |
6751 | sec = asym_ptr->section; |
6752 | if (sec->owner != abfd && sec->output_section != NULL) | |
6753 | sec = sec->output_section; | |
6754 | if (sec->owner == abfd | |
6755 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 6756 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
6757 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
6758 | } | |
6759 | ||
6760 | idx = asym_ptr->udata.i; | |
6761 | ||
6762 | if (idx == 0) | |
6763 | { | |
6764 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 6765 | which is used in a relocation entry. */ |
4eca0228 | 6766 | _bfd_error_handler |
695344c0 | 6767 | /* xgettext:c-format */ |
871b3ab2 | 6768 | (_("%pB: symbol `%s' required but not present"), |
d003868e | 6769 | abfd, bfd_asymbol_name (asym_ptr)); |
252b5132 RH |
6770 | bfd_set_error (bfd_error_no_symbols); |
6771 | return -1; | |
6772 | } | |
6773 | ||
6774 | #if DEBUG & 4 | |
6775 | { | |
6776 | fprintf (stderr, | |
cd9af601 AM |
6777 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n", |
6778 | (long) asym_ptr, asym_ptr->name, idx, flags); | |
252b5132 RH |
6779 | fflush (stderr); |
6780 | } | |
6781 | #endif | |
6782 | ||
6783 | return idx; | |
6784 | } | |
6785 | ||
84d1d650 | 6786 | /* Rewrite program header information. */ |
252b5132 | 6787 | |
b34976b6 | 6788 | static bfd_boolean |
84d1d650 | 6789 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 6790 | { |
b34976b6 AM |
6791 | Elf_Internal_Ehdr *iehdr; |
6792 | struct elf_segment_map *map; | |
6793 | struct elf_segment_map *map_first; | |
6794 | struct elf_segment_map **pointer_to_map; | |
6795 | Elf_Internal_Phdr *segment; | |
6796 | asection *section; | |
6797 | unsigned int i; | |
6798 | unsigned int num_segments; | |
6799 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 6800 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
6801 | bfd_vma maxpagesize; |
6802 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
6803 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 6804 | const struct elf_backend_data *bed; |
502794d4 | 6805 | unsigned int opb = bfd_octets_per_byte (ibfd, NULL); |
bc67d8a6 | 6806 | |
caf47ea6 | 6807 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
6808 | iehdr = elf_elfheader (ibfd); |
6809 | ||
bc67d8a6 | 6810 | map_first = NULL; |
c044fabd | 6811 | pointer_to_map = &map_first; |
252b5132 RH |
6812 | |
6813 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
6814 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
6815 | ||
6816 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
6817 | #define SEGMENT_END(segment, start) \ |
6818 | (start + (segment->p_memsz > segment->p_filesz \ | |
6819 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 6820 | |
eecdbe52 JJ |
6821 | #define SECTION_SIZE(section, segment) \ |
6822 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
6823 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 6824 | ? section->size : 0) |
eecdbe52 | 6825 | |
b34976b6 | 6826 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6827 | the given segment. VMA addresses are compared. */ |
502794d4 CE |
6828 | #define IS_CONTAINED_BY_VMA(section, segment, opb) \ |
6829 | (section->vma * (opb) >= segment->p_vaddr \ | |
6830 | && (section->vma * (opb) + SECTION_SIZE (section, segment) \ | |
aecc8f8a | 6831 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 6832 | |
b34976b6 | 6833 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6834 | the given segment. LMA addresses are compared. */ |
502794d4 CE |
6835 | #define IS_CONTAINED_BY_LMA(section, segment, base, opb) \ |
6836 | (section->lma * (opb) >= base \ | |
6837 | && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \ | |
6838 | && (section->lma * (opb) + SECTION_SIZE (section, segment) \ | |
aecc8f8a | 6839 | <= SEGMENT_END (segment, base))) |
252b5132 | 6840 | |
0efc80c8 L |
6841 | /* Handle PT_NOTE segment. */ |
6842 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 6843 | (p->p_type == PT_NOTE \ |
0efc80c8 | 6844 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 6845 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6846 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6847 | <= p->p_offset + p->p_filesz)) |
252b5132 | 6848 | |
0efc80c8 L |
6849 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
6850 | etc. */ | |
6851 | #define IS_COREFILE_NOTE(p, s) \ | |
6852 | (IS_NOTE (p, s) \ | |
6853 | && bfd_get_format (ibfd) == bfd_core \ | |
6854 | && s->vma == 0 \ | |
6855 | && s->lma == 0) | |
6856 | ||
252b5132 RH |
6857 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
6858 | linker, which generates a PT_INTERP section with p_vaddr and | |
6859 | p_memsz set to 0. */ | |
aecc8f8a AM |
6860 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
6861 | (p->p_vaddr == 0 \ | |
6862 | && p->p_paddr == 0 \ | |
6863 | && p->p_memsz == 0 \ | |
6864 | && p->p_filesz > 0 \ | |
6865 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 6866 | && s->size > 0 \ |
aecc8f8a | 6867 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6868 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6869 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 6870 | |
bc67d8a6 NC |
6871 | /* Decide if the given section should be included in the given segment. |
6872 | A section will be included if: | |
f5ffc919 | 6873 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 6874 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 6875 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 6876 | segment. |
bc67d8a6 | 6877 | 3. There is an output section associated with it, |
eecdbe52 | 6878 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 6879 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 6880 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
6881 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
6882 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 6883 | (with the possible exception of .dynamic). */ |
502794d4 | 6884 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \ |
2b05f1b7 | 6885 | ((((segment->p_paddr \ |
502794d4 CE |
6886 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \ |
6887 | : IS_CONTAINED_BY_VMA (section, segment, opb)) \ | |
2b05f1b7 | 6888 | && (section->flags & SEC_ALLOC) != 0) \ |
0efc80c8 | 6889 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
6890 | && segment->p_type != PT_GNU_STACK \ |
6891 | && (segment->p_type != PT_TLS \ | |
6892 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
6893 | && (segment->p_type == PT_LOAD \ | |
6894 | || segment->p_type == PT_TLS \ | |
6895 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6896 | && (segment->p_type != PT_DYNAMIC \ | |
6897 | || SECTION_SIZE (section, segment) > 0 \ | |
6898 | || (segment->p_paddr \ | |
502794d4 CE |
6899 | ? segment->p_paddr != section->lma * (opb) \ |
6900 | : segment->p_vaddr != section->vma * (opb)) \ | |
fd361982 | 6901 | || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \ |
9933dc52 | 6902 | && (segment->p_type != PT_LOAD || !section->segment_mark)) |
bc67d8a6 | 6903 | |
9f17e2a6 L |
6904 | /* If the output section of a section in the input segment is NULL, |
6905 | it is removed from the corresponding output segment. */ | |
502794d4 CE |
6906 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \ |
6907 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \ | |
9f17e2a6 L |
6908 | && section->output_section != NULL) |
6909 | ||
b34976b6 | 6910 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
6911 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
6912 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
6913 | ||
6914 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
6915 | their VMA address ranges and their LMA address ranges overlap. | |
6916 | It is possible to have overlapping VMA ranges without overlapping LMA | |
6917 | ranges. RedBoot images for example can have both .data and .bss mapped | |
6918 | to the same VMA range, but with the .data section mapped to a different | |
6919 | LMA. */ | |
aecc8f8a | 6920 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 6921 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 6922 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 6923 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 6924 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
6925 | |
6926 | /* Initialise the segment mark field. */ | |
6927 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 6928 | section->segment_mark = FALSE; |
bc67d8a6 | 6929 | |
5c44b38e AM |
6930 | /* The Solaris linker creates program headers in which all the |
6931 | p_paddr fields are zero. When we try to objcopy or strip such a | |
6932 | file, we get confused. Check for this case, and if we find it | |
6933 | don't set the p_paddr_valid fields. */ | |
6934 | p_paddr_valid = FALSE; | |
6935 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6936 | i < num_segments; | |
6937 | i++, segment++) | |
6938 | if (segment->p_paddr != 0) | |
6939 | { | |
6940 | p_paddr_valid = TRUE; | |
6941 | break; | |
6942 | } | |
6943 | ||
252b5132 | 6944 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6945 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6946 | in the loadable segments. These can be created by weird |
aecc8f8a | 6947 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6948 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6949 | i < num_segments; | |
c044fabd | 6950 | i++, segment++) |
252b5132 | 6951 | { |
252b5132 | 6952 | unsigned int j; |
c044fabd | 6953 | Elf_Internal_Phdr *segment2; |
252b5132 | 6954 | |
aecc8f8a AM |
6955 | if (segment->p_type == PT_INTERP) |
6956 | for (section = ibfd->sections; section; section = section->next) | |
6957 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6958 | { | |
6959 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6960 | assignment code will work. */ |
502794d4 | 6961 | segment->p_vaddr = section->vma * opb; |
aecc8f8a AM |
6962 | break; |
6963 | } | |
6964 | ||
bc67d8a6 | 6965 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
6966 | { |
6967 | /* Remove PT_GNU_RELRO segment. */ | |
6968 | if (segment->p_type == PT_GNU_RELRO) | |
6969 | segment->p_type = PT_NULL; | |
6970 | continue; | |
6971 | } | |
c044fabd | 6972 | |
bc67d8a6 | 6973 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 6974 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
6975 | { |
6976 | bfd_signed_vma extra_length; | |
c044fabd | 6977 | |
bc67d8a6 | 6978 | if (segment2->p_type != PT_LOAD |
0067a569 | 6979 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6980 | continue; |
c044fabd | 6981 | |
bc67d8a6 NC |
6982 | /* Merge the two segments together. */ |
6983 | if (segment2->p_vaddr < segment->p_vaddr) | |
6984 | { | |
c044fabd | 6985 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6986 | SEGMENT. */ |
0067a569 AM |
6987 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6988 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6989 | |
bc67d8a6 NC |
6990 | if (extra_length > 0) |
6991 | { | |
0067a569 | 6992 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
6993 | segment2->p_filesz += extra_length; |
6994 | } | |
c044fabd | 6995 | |
bc67d8a6 | 6996 | segment->p_type = PT_NULL; |
c044fabd | 6997 | |
bc67d8a6 NC |
6998 | /* Since we have deleted P we must restart the outer loop. */ |
6999 | i = 0; | |
7000 | segment = elf_tdata (ibfd)->phdr; | |
7001 | break; | |
7002 | } | |
7003 | else | |
7004 | { | |
c044fabd | 7005 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 7006 | SEGMENT2. */ |
0067a569 AM |
7007 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
7008 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 7009 | |
bc67d8a6 NC |
7010 | if (extra_length > 0) |
7011 | { | |
0067a569 | 7012 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
7013 | segment->p_filesz += extra_length; |
7014 | } | |
c044fabd | 7015 | |
bc67d8a6 NC |
7016 | segment2->p_type = PT_NULL; |
7017 | } | |
7018 | } | |
7019 | } | |
c044fabd | 7020 | |
bc67d8a6 NC |
7021 | /* The second scan attempts to assign sections to segments. */ |
7022 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7023 | i < num_segments; | |
0067a569 | 7024 | i++, segment++) |
bc67d8a6 | 7025 | { |
0067a569 AM |
7026 | unsigned int section_count; |
7027 | asection **sections; | |
7028 | asection *output_section; | |
7029 | unsigned int isec; | |
9933dc52 AM |
7030 | asection *matching_lma; |
7031 | asection *suggested_lma; | |
0067a569 | 7032 | unsigned int j; |
446f7ed5 | 7033 | size_t amt; |
0067a569 | 7034 | asection *first_section; |
bc67d8a6 NC |
7035 | |
7036 | if (segment->p_type == PT_NULL) | |
7037 | continue; | |
c044fabd | 7038 | |
9f17e2a6 | 7039 | first_section = NULL; |
bc67d8a6 | 7040 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
7041 | for (section = ibfd->sections, section_count = 0; |
7042 | section != NULL; | |
7043 | section = section->next) | |
9f17e2a6 L |
7044 | { |
7045 | /* Find the first section in the input segment, which may be | |
7046 | removed from the corresponding output segment. */ | |
502794d4 | 7047 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb)) |
9f17e2a6 L |
7048 | { |
7049 | if (first_section == NULL) | |
7050 | first_section = section; | |
7051 | if (section->output_section != NULL) | |
7052 | ++section_count; | |
7053 | } | |
7054 | } | |
811072d8 | 7055 | |
b5f852ea NC |
7056 | /* Allocate a segment map big enough to contain |
7057 | all of the sections we have selected. */ | |
00bee008 | 7058 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
446f7ed5 | 7059 | amt += section_count * sizeof (asection *); |
a50b1753 | 7060 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 7061 | if (map == NULL) |
b34976b6 | 7062 | return FALSE; |
252b5132 RH |
7063 | |
7064 | /* Initialise the fields of the segment map. Default to | |
7065 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
7066 | map->next = NULL; |
7067 | map->p_type = segment->p_type; | |
7068 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 7069 | map->p_flags_valid = 1; |
55d55ac7 | 7070 | |
9f17e2a6 L |
7071 | /* If the first section in the input segment is removed, there is |
7072 | no need to preserve segment physical address in the corresponding | |
7073 | output segment. */ | |
945c025a | 7074 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
7075 | { |
7076 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 7077 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 7078 | } |
252b5132 RH |
7079 | |
7080 | /* Determine if this segment contains the ELF file header | |
7081 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
7082 | map->includes_filehdr = (segment->p_offset == 0 |
7083 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 7084 | map->includes_phdrs = 0; |
252b5132 | 7085 | |
0067a569 | 7086 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 7087 | { |
bc67d8a6 NC |
7088 | map->includes_phdrs = |
7089 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7090 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
7091 | >= ((bfd_vma) iehdr->e_phoff |
7092 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 7093 | |
bc67d8a6 | 7094 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 7095 | phdr_included = TRUE; |
252b5132 RH |
7096 | } |
7097 | ||
bc67d8a6 | 7098 | if (section_count == 0) |
252b5132 RH |
7099 | { |
7100 | /* Special segments, such as the PT_PHDR segment, may contain | |
7101 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 | 7102 | something. They are allowed by the ELF spec however, so only |
07d6d2b8 | 7103 | a warning is produced. |
f98450c6 NC |
7104 | There is however the valid use case of embedded systems which |
7105 | have segments with p_filesz of 0 and a p_memsz > 0 to initialize | |
7106 | flash memory with zeros. No warning is shown for that case. */ | |
7107 | if (segment->p_type == PT_LOAD | |
7108 | && (segment->p_filesz > 0 || segment->p_memsz == 0)) | |
7109 | /* xgettext:c-format */ | |
9793eb77 AM |
7110 | _bfd_error_handler |
7111 | (_("%pB: warning: empty loadable segment detected" | |
7112 | " at vaddr=%#" PRIx64 ", is this intentional?"), | |
7113 | ibfd, (uint64_t) segment->p_vaddr); | |
252b5132 | 7114 | |
502794d4 | 7115 | map->p_vaddr_offset = segment->p_vaddr / opb; |
bc67d8a6 | 7116 | map->count = 0; |
c044fabd KH |
7117 | *pointer_to_map = map; |
7118 | pointer_to_map = &map->next; | |
252b5132 RH |
7119 | |
7120 | continue; | |
7121 | } | |
7122 | ||
7123 | /* Now scan the sections in the input BFD again and attempt | |
7124 | to add their corresponding output sections to the segment map. | |
7125 | The problem here is how to handle an output section which has | |
7126 | been moved (ie had its LMA changed). There are four possibilities: | |
7127 | ||
7128 | 1. None of the sections have been moved. | |
7129 | In this case we can continue to use the segment LMA from the | |
7130 | input BFD. | |
7131 | ||
7132 | 2. All of the sections have been moved by the same amount. | |
7133 | In this case we can change the segment's LMA to match the LMA | |
7134 | of the first section. | |
7135 | ||
7136 | 3. Some of the sections have been moved, others have not. | |
7137 | In this case those sections which have not been moved can be | |
7138 | placed in the current segment which will have to have its size, | |
7139 | and possibly its LMA changed, and a new segment or segments will | |
7140 | have to be created to contain the other sections. | |
7141 | ||
b5f852ea | 7142 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
7143 | In this case we can change the segment's LMA to match the LMA |
7144 | of the first section and we will have to create a new segment | |
7145 | or segments to contain the other sections. | |
7146 | ||
7147 | In order to save time, we allocate an array to hold the section | |
7148 | pointers that we are interested in. As these sections get assigned | |
7149 | to a segment, they are removed from this array. */ | |
7150 | ||
446f7ed5 AM |
7151 | amt = section_count * sizeof (asection *); |
7152 | sections = (asection **) bfd_malloc (amt); | |
252b5132 | 7153 | if (sections == NULL) |
b34976b6 | 7154 | return FALSE; |
252b5132 RH |
7155 | |
7156 | /* Step One: Scan for segment vs section LMA conflicts. | |
7157 | Also add the sections to the section array allocated above. | |
7158 | Also add the sections to the current segment. In the common | |
7159 | case, where the sections have not been moved, this means that | |
7160 | we have completely filled the segment, and there is nothing | |
7161 | more to do. */ | |
252b5132 | 7162 | isec = 0; |
9933dc52 AM |
7163 | matching_lma = NULL; |
7164 | suggested_lma = NULL; | |
252b5132 | 7165 | |
461c4b2e | 7166 | for (section = first_section, j = 0; |
bc67d8a6 NC |
7167 | section != NULL; |
7168 | section = section->next) | |
252b5132 | 7169 | { |
502794d4 | 7170 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb)) |
c0f7859b | 7171 | { |
bc67d8a6 NC |
7172 | output_section = section->output_section; |
7173 | ||
0067a569 | 7174 | sections[j++] = section; |
252b5132 RH |
7175 | |
7176 | /* The Solaris native linker always sets p_paddr to 0. | |
7177 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
7178 | correct value. Note - some backends require that |
7179 | p_paddr be left as zero. */ | |
5c44b38e | 7180 | if (!p_paddr_valid |
4455705d | 7181 | && segment->p_vaddr != 0 |
0067a569 | 7182 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 7183 | && isec == 0 |
bc67d8a6 | 7184 | && output_section->lma != 0 |
9933dc52 AM |
7185 | && (align_power (segment->p_vaddr |
7186 | + (map->includes_filehdr | |
7187 | ? iehdr->e_ehsize : 0) | |
7188 | + (map->includes_phdrs | |
7189 | ? iehdr->e_phnum * iehdr->e_phentsize | |
7190 | : 0), | |
66631823 CE |
7191 | output_section->alignment_power * opb) |
7192 | == (output_section->vma * opb))) | |
bc67d8a6 | 7193 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
7194 | |
7195 | /* Match up the physical address of the segment with the | |
7196 | LMA address of the output section. */ | |
502794d4 CE |
7197 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr, |
7198 | opb) | |
5e8d7549 | 7199 | || IS_COREFILE_NOTE (segment, section) |
0067a569 | 7200 | || (bed->want_p_paddr_set_to_zero |
502794d4 | 7201 | && IS_CONTAINED_BY_VMA (output_section, segment, opb))) |
252b5132 | 7202 | { |
9933dc52 AM |
7203 | if (matching_lma == NULL |
7204 | || output_section->lma < matching_lma->lma) | |
7205 | matching_lma = output_section; | |
252b5132 RH |
7206 | |
7207 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 7208 | then it does not overlap any other section within that |
252b5132 | 7209 | segment. */ |
0067a569 AM |
7210 | map->sections[isec++] = output_section; |
7211 | } | |
9933dc52 AM |
7212 | else if (suggested_lma == NULL) |
7213 | suggested_lma = output_section; | |
147d51c2 L |
7214 | |
7215 | if (j == section_count) | |
7216 | break; | |
252b5132 RH |
7217 | } |
7218 | } | |
7219 | ||
bc67d8a6 | 7220 | BFD_ASSERT (j == section_count); |
252b5132 RH |
7221 | |
7222 | /* Step Two: Adjust the physical address of the current segment, | |
7223 | if necessary. */ | |
bc67d8a6 | 7224 | if (isec == section_count) |
252b5132 RH |
7225 | { |
7226 | /* All of the sections fitted within the segment as currently | |
7227 | specified. This is the default case. Add the segment to | |
7228 | the list of built segments and carry on to process the next | |
7229 | program header in the input BFD. */ | |
bc67d8a6 | 7230 | map->count = section_count; |
c044fabd KH |
7231 | *pointer_to_map = map; |
7232 | pointer_to_map = &map->next; | |
08a40648 | 7233 | |
5c44b38e | 7234 | if (p_paddr_valid |
30fe1832 AM |
7235 | && !bed->want_p_paddr_set_to_zero) |
7236 | { | |
7237 | bfd_vma hdr_size = 0; | |
7238 | if (map->includes_filehdr) | |
7239 | hdr_size = iehdr->e_ehsize; | |
7240 | if (map->includes_phdrs) | |
7241 | hdr_size += iehdr->e_phnum * iehdr->e_phentsize; | |
7242 | ||
7243 | /* Account for padding before the first section in the | |
7244 | segment. */ | |
502794d4 CE |
7245 | map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb |
7246 | - matching_lma->lma); | |
30fe1832 | 7247 | } |
08a40648 | 7248 | |
252b5132 RH |
7249 | free (sections); |
7250 | continue; | |
7251 | } | |
252b5132 RH |
7252 | else |
7253 | { | |
9933dc52 AM |
7254 | /* Change the current segment's physical address to match |
7255 | the LMA of the first section that fitted, or if no | |
7256 | section fitted, the first section. */ | |
7257 | if (matching_lma == NULL) | |
7258 | matching_lma = suggested_lma; | |
7259 | ||
66631823 | 7260 | map->p_paddr = matching_lma->lma * opb; |
72730e0c | 7261 | |
bc67d8a6 NC |
7262 | /* Offset the segment physical address from the lma |
7263 | to allow for space taken up by elf headers. */ | |
9933dc52 | 7264 | if (map->includes_phdrs) |
010c8431 | 7265 | { |
9933dc52 AM |
7266 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; |
7267 | ||
7268 | /* iehdr->e_phnum is just an estimate of the number | |
7269 | of program headers that we will need. Make a note | |
7270 | here of the number we used and the segment we chose | |
7271 | to hold these headers, so that we can adjust the | |
7272 | offset when we know the correct value. */ | |
7273 | phdr_adjust_num = iehdr->e_phnum; | |
7274 | phdr_adjust_seg = map; | |
010c8431 | 7275 | } |
252b5132 | 7276 | |
9933dc52 | 7277 | if (map->includes_filehdr) |
bc67d8a6 | 7278 | { |
9933dc52 AM |
7279 | bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power; |
7280 | map->p_paddr -= iehdr->e_ehsize; | |
7281 | /* We've subtracted off the size of headers from the | |
7282 | first section lma, but there may have been some | |
7283 | alignment padding before that section too. Try to | |
7284 | account for that by adjusting the segment lma down to | |
7285 | the same alignment. */ | |
7286 | if (segment->p_align != 0 && segment->p_align < align) | |
7287 | align = segment->p_align; | |
66631823 | 7288 | map->p_paddr &= -(align * opb); |
bc67d8a6 | 7289 | } |
252b5132 RH |
7290 | } |
7291 | ||
7292 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 7293 | those that fit to the current segment and removing them from the |
252b5132 RH |
7294 | sections array; but making sure not to leave large gaps. Once all |
7295 | possible sections have been assigned to the current segment it is | |
7296 | added to the list of built segments and if sections still remain | |
7297 | to be assigned, a new segment is constructed before repeating | |
7298 | the loop. */ | |
7299 | isec = 0; | |
7300 | do | |
7301 | { | |
bc67d8a6 | 7302 | map->count = 0; |
9933dc52 | 7303 | suggested_lma = NULL; |
252b5132 RH |
7304 | |
7305 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 7306 | for (j = 0; j < section_count; j++) |
252b5132 | 7307 | { |
bc67d8a6 | 7308 | section = sections[j]; |
252b5132 | 7309 | |
bc67d8a6 | 7310 | if (section == NULL) |
252b5132 RH |
7311 | continue; |
7312 | ||
bc67d8a6 | 7313 | output_section = section->output_section; |
252b5132 | 7314 | |
bc67d8a6 | 7315 | BFD_ASSERT (output_section != NULL); |
c044fabd | 7316 | |
502794d4 CE |
7317 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr, |
7318 | opb) | |
bc67d8a6 | 7319 | || IS_COREFILE_NOTE (segment, section)) |
252b5132 | 7320 | { |
bc67d8a6 | 7321 | if (map->count == 0) |
252b5132 RH |
7322 | { |
7323 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
7324 | the beginning of the segment, then something is |
7325 | wrong. */ | |
9933dc52 AM |
7326 | if (align_power (map->p_paddr |
7327 | + (map->includes_filehdr | |
7328 | ? iehdr->e_ehsize : 0) | |
7329 | + (map->includes_phdrs | |
7330 | ? iehdr->e_phnum * iehdr->e_phentsize | |
7331 | : 0), | |
66631823 CE |
7332 | output_section->alignment_power * opb) |
7333 | != output_section->lma * opb) | |
9aea1e31 | 7334 | goto sorry; |
252b5132 RH |
7335 | } |
7336 | else | |
7337 | { | |
0067a569 | 7338 | asection *prev_sec; |
252b5132 | 7339 | |
bc67d8a6 | 7340 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
7341 | |
7342 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
7343 | and the start of this section is more than |
7344 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 7345 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 7346 | maxpagesize) |
caf47ea6 | 7347 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 7348 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 7349 | > output_section->lma)) |
252b5132 | 7350 | { |
9933dc52 AM |
7351 | if (suggested_lma == NULL) |
7352 | suggested_lma = output_section; | |
252b5132 RH |
7353 | |
7354 | continue; | |
7355 | } | |
7356 | } | |
7357 | ||
bc67d8a6 | 7358 | map->sections[map->count++] = output_section; |
252b5132 RH |
7359 | ++isec; |
7360 | sections[j] = NULL; | |
9933dc52 AM |
7361 | if (segment->p_type == PT_LOAD) |
7362 | section->segment_mark = TRUE; | |
0067a569 | 7363 | } |
9933dc52 AM |
7364 | else if (suggested_lma == NULL) |
7365 | suggested_lma = output_section; | |
252b5132 RH |
7366 | } |
7367 | ||
beab4532 NC |
7368 | /* PR 23932. A corrupt input file may contain sections that cannot |
7369 | be assigned to any segment - because for example they have a | |
9984857c NC |
7370 | negative size - or segments that do not contain any sections. |
7371 | But there are also valid reasons why a segment can be empty. | |
7372 | So allow a count of zero. */ | |
252b5132 RH |
7373 | |
7374 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
7375 | *pointer_to_map = map; |
7376 | pointer_to_map = &map->next; | |
252b5132 | 7377 | |
bc67d8a6 | 7378 | if (isec < section_count) |
252b5132 RH |
7379 | { |
7380 | /* We still have not allocated all of the sections to | |
7381 | segments. Create a new segment here, initialise it | |
7382 | and carry on looping. */ | |
00bee008 | 7383 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
446f7ed5 | 7384 | amt += section_count * sizeof (asection *); |
5964fc3a | 7385 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 7386 | if (map == NULL) |
5ed6aba4 NC |
7387 | { |
7388 | free (sections); | |
7389 | return FALSE; | |
7390 | } | |
252b5132 RH |
7391 | |
7392 | /* Initialise the fields of the segment map. Set the physical | |
7393 | physical address to the LMA of the first section that has | |
7394 | not yet been assigned. */ | |
0067a569 AM |
7395 | map->next = NULL; |
7396 | map->p_type = segment->p_type; | |
7397 | map->p_flags = segment->p_flags; | |
7398 | map->p_flags_valid = 1; | |
66631823 | 7399 | map->p_paddr = suggested_lma->lma * opb; |
5c44b38e | 7400 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 7401 | map->includes_filehdr = 0; |
0067a569 | 7402 | map->includes_phdrs = 0; |
252b5132 | 7403 | } |
9984857c NC |
7404 | |
7405 | continue; | |
7406 | sorry: | |
7407 | bfd_set_error (bfd_error_sorry); | |
7408 | free (sections); | |
7409 | return FALSE; | |
252b5132 | 7410 | } |
bc67d8a6 | 7411 | while (isec < section_count); |
252b5132 RH |
7412 | |
7413 | free (sections); | |
7414 | } | |
7415 | ||
12bd6957 | 7416 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
7417 | |
7418 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 7419 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
7420 | the offset if necessary. */ |
7421 | if (phdr_adjust_seg != NULL) | |
7422 | { | |
7423 | unsigned int count; | |
c044fabd | 7424 | |
bc67d8a6 | 7425 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 7426 | count++; |
252b5132 | 7427 | |
bc67d8a6 NC |
7428 | if (count > phdr_adjust_num) |
7429 | phdr_adjust_seg->p_paddr | |
7430 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
9933dc52 AM |
7431 | |
7432 | for (map = map_first; map != NULL; map = map->next) | |
7433 | if (map->p_type == PT_PHDR) | |
7434 | { | |
7435 | bfd_vma adjust | |
7436 | = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0; | |
7437 | map->p_paddr = phdr_adjust_seg->p_paddr + adjust; | |
7438 | break; | |
7439 | } | |
bc67d8a6 | 7440 | } |
c044fabd | 7441 | |
bc67d8a6 | 7442 | #undef SEGMENT_END |
eecdbe52 | 7443 | #undef SECTION_SIZE |
bc67d8a6 NC |
7444 | #undef IS_CONTAINED_BY_VMA |
7445 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 7446 | #undef IS_NOTE |
252b5132 | 7447 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 7448 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 7449 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
7450 | #undef INCLUDE_SECTION_IN_SEGMENT |
7451 | #undef SEGMENT_AFTER_SEGMENT | |
7452 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 7453 | return TRUE; |
252b5132 RH |
7454 | } |
7455 | ||
84d1d650 L |
7456 | /* Copy ELF program header information. */ |
7457 | ||
7458 | static bfd_boolean | |
7459 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
7460 | { | |
7461 | Elf_Internal_Ehdr *iehdr; | |
7462 | struct elf_segment_map *map; | |
7463 | struct elf_segment_map *map_first; | |
7464 | struct elf_segment_map **pointer_to_map; | |
7465 | Elf_Internal_Phdr *segment; | |
7466 | unsigned int i; | |
7467 | unsigned int num_segments; | |
7468 | bfd_boolean phdr_included = FALSE; | |
88967714 | 7469 | bfd_boolean p_paddr_valid; |
502794d4 | 7470 | unsigned int opb = bfd_octets_per_byte (ibfd, NULL); |
84d1d650 L |
7471 | |
7472 | iehdr = elf_elfheader (ibfd); | |
7473 | ||
7474 | map_first = NULL; | |
7475 | pointer_to_map = &map_first; | |
7476 | ||
88967714 AM |
7477 | /* If all the segment p_paddr fields are zero, don't set |
7478 | map->p_paddr_valid. */ | |
7479 | p_paddr_valid = FALSE; | |
84d1d650 | 7480 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
7481 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7482 | i < num_segments; | |
7483 | i++, segment++) | |
7484 | if (segment->p_paddr != 0) | |
7485 | { | |
7486 | p_paddr_valid = TRUE; | |
7487 | break; | |
7488 | } | |
7489 | ||
84d1d650 L |
7490 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7491 | i < num_segments; | |
7492 | i++, segment++) | |
7493 | { | |
7494 | asection *section; | |
7495 | unsigned int section_count; | |
986f0783 | 7496 | size_t amt; |
84d1d650 | 7497 | Elf_Internal_Shdr *this_hdr; |
53020534 | 7498 | asection *first_section = NULL; |
a76e6f2f | 7499 | asection *lowest_section; |
84d1d650 | 7500 | |
84d1d650 L |
7501 | /* Compute how many sections are in this segment. */ |
7502 | for (section = ibfd->sections, section_count = 0; | |
7503 | section != NULL; | |
7504 | section = section->next) | |
7505 | { | |
7506 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7507 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 7508 | { |
a76e6f2f AM |
7509 | if (first_section == NULL) |
7510 | first_section = section; | |
3271a814 NS |
7511 | section_count++; |
7512 | } | |
84d1d650 L |
7513 | } |
7514 | ||
7515 | /* Allocate a segment map big enough to contain | |
7516 | all of the sections we have selected. */ | |
00bee008 | 7517 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
986f0783 | 7518 | amt += section_count * sizeof (asection *); |
a50b1753 | 7519 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
7520 | if (map == NULL) |
7521 | return FALSE; | |
7522 | ||
7523 | /* Initialize the fields of the output segment map with the | |
7524 | input segment. */ | |
7525 | map->next = NULL; | |
7526 | map->p_type = segment->p_type; | |
7527 | map->p_flags = segment->p_flags; | |
7528 | map->p_flags_valid = 1; | |
7529 | map->p_paddr = segment->p_paddr; | |
88967714 | 7530 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
7531 | map->p_align = segment->p_align; |
7532 | map->p_align_valid = 1; | |
3271a814 | 7533 | map->p_vaddr_offset = 0; |
84d1d650 | 7534 | |
04c3a755 NS |
7535 | if (map->p_type == PT_GNU_RELRO |
7536 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
7537 | { |
7538 | /* The PT_GNU_RELRO segment may contain the first a few | |
7539 | bytes in the .got.plt section even if the whole .got.plt | |
7540 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
7541 | change the size of the PT_GNU_RELRO segment. |
7542 | Similarly, PT_GNU_STACK size is significant on uclinux | |
7543 | systems. */ | |
9433b9b1 | 7544 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
7545 | map->p_size_valid = 1; |
7546 | } | |
7547 | ||
84d1d650 L |
7548 | /* Determine if this segment contains the ELF file header |
7549 | and if it contains the program headers themselves. */ | |
7550 | map->includes_filehdr = (segment->p_offset == 0 | |
7551 | && segment->p_filesz >= iehdr->e_ehsize); | |
7552 | ||
7553 | map->includes_phdrs = 0; | |
7554 | if (! phdr_included || segment->p_type != PT_LOAD) | |
7555 | { | |
7556 | map->includes_phdrs = | |
7557 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7558 | && (segment->p_offset + segment->p_filesz | |
7559 | >= ((bfd_vma) iehdr->e_phoff | |
7560 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
7561 | ||
7562 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
7563 | phdr_included = TRUE; | |
7564 | } | |
7565 | ||
bbefd0a9 | 7566 | lowest_section = NULL; |
84d1d650 L |
7567 | if (section_count != 0) |
7568 | { | |
7569 | unsigned int isec = 0; | |
7570 | ||
53020534 | 7571 | for (section = first_section; |
84d1d650 L |
7572 | section != NULL; |
7573 | section = section->next) | |
7574 | { | |
7575 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7576 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
7577 | { |
7578 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
7579 | if ((section->flags & SEC_ALLOC) != 0) |
7580 | { | |
7581 | bfd_vma seg_off; | |
7582 | ||
bbefd0a9 AM |
7583 | if (lowest_section == NULL |
7584 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
7585 | lowest_section = section; |
7586 | ||
a76e6f2f AM |
7587 | /* Section lmas are set up from PT_LOAD header |
7588 | p_paddr in _bfd_elf_make_section_from_shdr. | |
7589 | If this header has a p_paddr that disagrees | |
7590 | with the section lma, flag the p_paddr as | |
7591 | invalid. */ | |
7592 | if ((section->flags & SEC_LOAD) != 0) | |
7593 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
7594 | else | |
7595 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
502794d4 | 7596 | if (section->lma * opb - segment->p_paddr != seg_off) |
a76e6f2f AM |
7597 | map->p_paddr_valid = FALSE; |
7598 | } | |
53020534 L |
7599 | if (isec == section_count) |
7600 | break; | |
7601 | } | |
84d1d650 L |
7602 | } |
7603 | } | |
7604 | ||
5d695627 | 7605 | if (section_count == 0) |
502794d4 | 7606 | map->p_vaddr_offset = segment->p_vaddr / opb; |
30fe1832 AM |
7607 | else if (map->p_paddr_valid) |
7608 | { | |
7609 | /* Account for padding before the first section in the segment. */ | |
7610 | bfd_vma hdr_size = 0; | |
7611 | if (map->includes_filehdr) | |
7612 | hdr_size = iehdr->e_ehsize; | |
7613 | if (map->includes_phdrs) | |
7614 | hdr_size += iehdr->e_phnum * iehdr->e_phentsize; | |
7615 | ||
502794d4 | 7616 | map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb |
30fe1832 AM |
7617 | - (lowest_section ? lowest_section->lma : 0)); |
7618 | } | |
a76e6f2f | 7619 | |
84d1d650 L |
7620 | map->count = section_count; |
7621 | *pointer_to_map = map; | |
7622 | pointer_to_map = &map->next; | |
7623 | } | |
7624 | ||
12bd6957 | 7625 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
7626 | return TRUE; |
7627 | } | |
7628 | ||
7629 | /* Copy private BFD data. This copies or rewrites ELF program header | |
7630 | information. */ | |
7631 | ||
7632 | static bfd_boolean | |
7633 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
7634 | { | |
84d1d650 L |
7635 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
7636 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7637 | return TRUE; | |
7638 | ||
7639 | if (elf_tdata (ibfd)->phdr == NULL) | |
7640 | return TRUE; | |
7641 | ||
7642 | if (ibfd->xvec == obfd->xvec) | |
7643 | { | |
cb3ff1e5 NC |
7644 | /* Check to see if any sections in the input BFD |
7645 | covered by ELF program header have changed. */ | |
d55ce4e2 | 7646 | Elf_Internal_Phdr *segment; |
84d1d650 L |
7647 | asection *section, *osec; |
7648 | unsigned int i, num_segments; | |
7649 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
7650 | const struct elf_backend_data *bed; |
7651 | ||
7652 | bed = get_elf_backend_data (ibfd); | |
7653 | ||
7654 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
7655 | if (bed->want_p_paddr_set_to_zero) | |
7656 | goto rewrite; | |
84d1d650 L |
7657 | |
7658 | /* Initialize the segment mark field. */ | |
7659 | for (section = obfd->sections; section != NULL; | |
7660 | section = section->next) | |
7661 | section->segment_mark = FALSE; | |
7662 | ||
7663 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
7664 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7665 | i < num_segments; | |
7666 | i++, segment++) | |
7667 | { | |
5f6999aa NC |
7668 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
7669 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
7670 | which severly confuses things, so always regenerate the segment | |
7671 | map in this case. */ | |
7672 | if (segment->p_paddr == 0 | |
7673 | && segment->p_memsz == 0 | |
7674 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 7675 | goto rewrite; |
5f6999aa | 7676 | |
84d1d650 L |
7677 | for (section = ibfd->sections; |
7678 | section != NULL; section = section->next) | |
7679 | { | |
7680 | /* We mark the output section so that we know it comes | |
7681 | from the input BFD. */ | |
7682 | osec = section->output_section; | |
7683 | if (osec) | |
7684 | osec->segment_mark = TRUE; | |
7685 | ||
7686 | /* Check if this section is covered by the segment. */ | |
7687 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7688 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
7689 | { |
7690 | /* FIXME: Check if its output section is changed or | |
7691 | removed. What else do we need to check? */ | |
7692 | if (osec == NULL | |
7693 | || section->flags != osec->flags | |
7694 | || section->lma != osec->lma | |
7695 | || section->vma != osec->vma | |
7696 | || section->size != osec->size | |
7697 | || section->rawsize != osec->rawsize | |
7698 | || section->alignment_power != osec->alignment_power) | |
7699 | goto rewrite; | |
7700 | } | |
7701 | } | |
7702 | } | |
7703 | ||
cb3ff1e5 | 7704 | /* Check to see if any output section do not come from the |
84d1d650 L |
7705 | input BFD. */ |
7706 | for (section = obfd->sections; section != NULL; | |
7707 | section = section->next) | |
7708 | { | |
535b785f | 7709 | if (!section->segment_mark) |
84d1d650 L |
7710 | goto rewrite; |
7711 | else | |
7712 | section->segment_mark = FALSE; | |
7713 | } | |
7714 | ||
7715 | return copy_elf_program_header (ibfd, obfd); | |
7716 | } | |
7717 | ||
dc1e8a47 | 7718 | rewrite: |
f1d85785 L |
7719 | if (ibfd->xvec == obfd->xvec) |
7720 | { | |
7721 | /* When rewriting program header, set the output maxpagesize to | |
7722 | the maximum alignment of input PT_LOAD segments. */ | |
7723 | Elf_Internal_Phdr *segment; | |
7724 | unsigned int i; | |
7725 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
7726 | bfd_vma maxpagesize = 0; | |
7727 | ||
7728 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7729 | i < num_segments; | |
7730 | i++, segment++) | |
7731 | if (segment->p_type == PT_LOAD | |
7732 | && maxpagesize < segment->p_align) | |
c86934ce NC |
7733 | { |
7734 | /* PR 17512: file: f17299af. */ | |
7735 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
695344c0 | 7736 | /* xgettext:c-format */ |
2dcf00ce AM |
7737 | _bfd_error_handler (_("%pB: warning: segment alignment of %#" |
7738 | PRIx64 " is too large"), | |
7739 | ibfd, (uint64_t) segment->p_align); | |
c86934ce NC |
7740 | else |
7741 | maxpagesize = segment->p_align; | |
7742 | } | |
f1d85785 L |
7743 | |
7744 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
7745 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
7746 | } | |
7747 | ||
84d1d650 L |
7748 | return rewrite_elf_program_header (ibfd, obfd); |
7749 | } | |
7750 | ||
ccd2ec6a L |
7751 | /* Initialize private output section information from input section. */ |
7752 | ||
7753 | bfd_boolean | |
7754 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
7755 | asection *isec, | |
7756 | bfd *obfd, | |
7757 | asection *osec, | |
7758 | struct bfd_link_info *link_info) | |
7759 | ||
7760 | { | |
7761 | Elf_Internal_Shdr *ihdr, *ohdr; | |
0e1862bb L |
7762 | bfd_boolean final_link = (link_info != NULL |
7763 | && !bfd_link_relocatable (link_info)); | |
ccd2ec6a L |
7764 | |
7765 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7766 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
7767 | return TRUE; | |
7768 | ||
ba85c43e NC |
7769 | BFD_ASSERT (elf_section_data (osec) != NULL); |
7770 | ||
8c803a2d AM |
7771 | /* If this is a known ABI section, ELF section type and flags may |
7772 | have been set up when OSEC was created. For normal sections we | |
7773 | allow the user to override the type and flags other than | |
7774 | SHF_MASKOS and SHF_MASKPROC. */ | |
7775 | if (elf_section_type (osec) == SHT_PROGBITS | |
7776 | || elf_section_type (osec) == SHT_NOTE | |
7777 | || elf_section_type (osec) == SHT_NOBITS) | |
7778 | elf_section_type (osec) = SHT_NULL; | |
7779 | /* For objcopy and relocatable link, copy the ELF section type from | |
7780 | the input file if the BFD section flags are the same. (If they | |
7781 | are different the user may be doing something like | |
7782 | "objcopy --set-section-flags .text=alloc,data".) For a final | |
7783 | link allow some flags that the linker clears to differ. */ | |
42bb2e33 | 7784 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
7785 | && (osec->flags == isec->flags |
7786 | || (final_link | |
7787 | && ((osec->flags ^ isec->flags) | |
0814be7d | 7788 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 7789 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
7790 | |
7791 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
8c803a2d AM |
7792 | elf_section_flags (osec) = (elf_section_flags (isec) |
7793 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a | 7794 | |
a91e1603 | 7795 | /* Copy sh_info from input for mbind section. */ |
df3a023b AM |
7796 | if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0 |
7797 | && elf_section_flags (isec) & SHF_GNU_MBIND) | |
a91e1603 L |
7798 | elf_section_data (osec)->this_hdr.sh_info |
7799 | = elf_section_data (isec)->this_hdr.sh_info; | |
7800 | ||
ccd2ec6a L |
7801 | /* Set things up for objcopy and relocatable link. The output |
7802 | SHT_GROUP section will have its elf_next_in_group pointing back | |
7803 | to the input group members. Ignore linker created group section. | |
7804 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
7bdf4127 AB |
7805 | if ((link_info == NULL |
7806 | || !link_info->resolve_section_groups) | |
7807 | && (elf_sec_group (isec) == NULL | |
7808 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)) | |
ccd2ec6a | 7809 | { |
7bdf4127 AB |
7810 | if (elf_section_flags (isec) & SHF_GROUP) |
7811 | elf_section_flags (osec) |= SHF_GROUP; | |
7812 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
7813 | elf_section_data (osec)->group = elf_section_data (isec)->group; | |
ccd2ec6a L |
7814 | } |
7815 | ||
7bdf4127 AB |
7816 | /* If not decompress, preserve SHF_COMPRESSED. */ |
7817 | if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0) | |
7818 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7819 | & SHF_COMPRESSED); | |
7820 | ||
ccd2ec6a L |
7821 | ihdr = &elf_section_data (isec)->this_hdr; |
7822 | ||
7823 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
7824 | don't use the output section of the linked-to section since it | |
7825 | may be NULL at this point. */ | |
7826 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
7827 | { | |
7828 | ohdr = &elf_section_data (osec)->this_hdr; | |
7829 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
7830 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
7831 | } | |
7832 | ||
7833 | osec->use_rela_p = isec->use_rela_p; | |
7834 | ||
7835 | return TRUE; | |
7836 | } | |
7837 | ||
252b5132 RH |
7838 | /* Copy private section information. This copies over the entsize |
7839 | field, and sometimes the info field. */ | |
7840 | ||
b34976b6 | 7841 | bfd_boolean |
217aa764 AM |
7842 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
7843 | asection *isec, | |
7844 | bfd *obfd, | |
7845 | asection *osec) | |
252b5132 RH |
7846 | { |
7847 | Elf_Internal_Shdr *ihdr, *ohdr; | |
7848 | ||
7849 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7850 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 7851 | return TRUE; |
252b5132 | 7852 | |
252b5132 RH |
7853 | ihdr = &elf_section_data (isec)->this_hdr; |
7854 | ohdr = &elf_section_data (osec)->this_hdr; | |
7855 | ||
7856 | ohdr->sh_entsize = ihdr->sh_entsize; | |
7857 | ||
7858 | if (ihdr->sh_type == SHT_SYMTAB | |
7859 | || ihdr->sh_type == SHT_DYNSYM | |
7860 | || ihdr->sh_type == SHT_GNU_verneed | |
7861 | || ihdr->sh_type == SHT_GNU_verdef) | |
7862 | ohdr->sh_info = ihdr->sh_info; | |
7863 | ||
ccd2ec6a L |
7864 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
7865 | NULL); | |
252b5132 RH |
7866 | } |
7867 | ||
d0bf826b AM |
7868 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
7869 | necessary if we are removing either the SHT_GROUP section or any of | |
7870 | the group member sections. DISCARDED is the value that a section's | |
7871 | output_section has if the section will be discarded, NULL when this | |
7872 | function is called from objcopy, bfd_abs_section_ptr when called | |
7873 | from the linker. */ | |
80fccad2 BW |
7874 | |
7875 | bfd_boolean | |
d0bf826b | 7876 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 7877 | { |
30288845 AM |
7878 | asection *isec; |
7879 | ||
30288845 | 7880 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 7881 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
7882 | { |
7883 | asection *first = elf_next_in_group (isec); | |
7884 | asection *s = first; | |
d0bf826b AM |
7885 | bfd_size_type removed = 0; |
7886 | ||
30288845 AM |
7887 | while (s != NULL) |
7888 | { | |
415f38a6 AM |
7889 | /* If this member section is being output but the |
7890 | SHT_GROUP section is not, then clear the group info | |
7891 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
7892 | if (s->output_section != discarded |
7893 | && isec->output_section == discarded) | |
30288845 AM |
7894 | { |
7895 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
7896 | elf_group_name (s->output_section) = NULL; | |
7897 | } | |
415f38a6 AM |
7898 | /* Conversely, if the member section is not being output |
7899 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
7900 | else if (s->output_section == discarded |
7901 | && isec->output_section != discarded) | |
6e5e9d58 AM |
7902 | { |
7903 | struct bfd_elf_section_data *elf_sec = elf_section_data (s); | |
7904 | removed += 4; | |
7905 | if (elf_sec->rel.hdr != NULL | |
7906 | && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0) | |
7907 | removed += 4; | |
7908 | if (elf_sec->rela.hdr != NULL | |
7909 | && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0) | |
7910 | removed += 4; | |
7911 | } | |
30288845 AM |
7912 | s = elf_next_in_group (s); |
7913 | if (s == first) | |
7914 | break; | |
7915 | } | |
d0bf826b AM |
7916 | if (removed != 0) |
7917 | { | |
7918 | if (discarded != NULL) | |
7919 | { | |
7920 | /* If we've been called for ld -r, then we need to | |
6e5e9d58 | 7921 | adjust the input section size. */ |
d0bf826b AM |
7922 | if (isec->rawsize == 0) |
7923 | isec->rawsize = isec->size; | |
7924 | isec->size = isec->rawsize - removed; | |
6e5e9d58 AM |
7925 | if (isec->size <= 4) |
7926 | { | |
7927 | isec->size = 0; | |
7928 | isec->flags |= SEC_EXCLUDE; | |
7929 | } | |
d0bf826b AM |
7930 | } |
7931 | else | |
7932 | { | |
7933 | /* Adjust the output section size when called from | |
7934 | objcopy. */ | |
7935 | isec->output_section->size -= removed; | |
6e5e9d58 AM |
7936 | if (isec->output_section->size <= 4) |
7937 | { | |
7938 | isec->output_section->size = 0; | |
7939 | isec->output_section->flags |= SEC_EXCLUDE; | |
7940 | } | |
d0bf826b AM |
7941 | } |
7942 | } | |
30288845 AM |
7943 | } |
7944 | ||
80fccad2 BW |
7945 | return TRUE; |
7946 | } | |
7947 | ||
d0bf826b AM |
7948 | /* Copy private header information. */ |
7949 | ||
7950 | bfd_boolean | |
7951 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
7952 | { | |
7953 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7954 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7955 | return TRUE; | |
7956 | ||
7957 | /* Copy over private BFD data if it has not already been copied. | |
7958 | This must be done here, rather than in the copy_private_bfd_data | |
7959 | entry point, because the latter is called after the section | |
7960 | contents have been set, which means that the program headers have | |
7961 | already been worked out. */ | |
12bd6957 | 7962 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
7963 | { |
7964 | if (! copy_private_bfd_data (ibfd, obfd)) | |
7965 | return FALSE; | |
7966 | } | |
7967 | ||
7968 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
7969 | } | |
7970 | ||
252b5132 RH |
7971 | /* Copy private symbol information. If this symbol is in a section |
7972 | which we did not map into a BFD section, try to map the section | |
7973 | index correctly. We use special macro definitions for the mapped | |
7974 | section indices; these definitions are interpreted by the | |
7975 | swap_out_syms function. */ | |
7976 | ||
9ad5cbcf AM |
7977 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
7978 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
7979 | #define MAP_STRTAB (SHN_HIOS + 3) | |
7980 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
7981 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 7982 | |
b34976b6 | 7983 | bfd_boolean |
217aa764 AM |
7984 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
7985 | asymbol *isymarg, | |
7986 | bfd *obfd, | |
7987 | asymbol *osymarg) | |
252b5132 RH |
7988 | { |
7989 | elf_symbol_type *isym, *osym; | |
7990 | ||
7991 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7992 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 7993 | return TRUE; |
252b5132 RH |
7994 | |
7995 | isym = elf_symbol_from (ibfd, isymarg); | |
7996 | osym = elf_symbol_from (obfd, osymarg); | |
7997 | ||
7998 | if (isym != NULL | |
8424d8f5 | 7999 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
8000 | && osym != NULL |
8001 | && bfd_is_abs_section (isym->symbol.section)) | |
8002 | { | |
8003 | unsigned int shndx; | |
8004 | ||
8005 | shndx = isym->internal_elf_sym.st_shndx; | |
8006 | if (shndx == elf_onesymtab (ibfd)) | |
8007 | shndx = MAP_ONESYMTAB; | |
8008 | else if (shndx == elf_dynsymtab (ibfd)) | |
8009 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 8010 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 8011 | shndx = MAP_STRTAB; |
12bd6957 | 8012 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 8013 | shndx = MAP_SHSTRTAB; |
6a40cf0c | 8014 | else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) |
9ad5cbcf | 8015 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
8016 | osym->internal_elf_sym.st_shndx = shndx; |
8017 | } | |
8018 | ||
b34976b6 | 8019 | return TRUE; |
252b5132 RH |
8020 | } |
8021 | ||
8022 | /* Swap out the symbols. */ | |
8023 | ||
b34976b6 | 8024 | static bfd_boolean |
217aa764 | 8025 | swap_out_syms (bfd *abfd, |
ef10c3ac | 8026 | struct elf_strtab_hash **sttp, |
217aa764 | 8027 | int relocatable_p) |
252b5132 | 8028 | { |
9c5bfbb7 | 8029 | const struct elf_backend_data *bed; |
1f4361a7 | 8030 | unsigned int symcount; |
079e9a2f | 8031 | asymbol **syms; |
ef10c3ac | 8032 | struct elf_strtab_hash *stt; |
079e9a2f | 8033 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 8034 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 8035 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 8036 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
8037 | bfd_byte *outbound_syms; |
8038 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
8039 | unsigned long outbound_syms_index; |
8040 | unsigned long outbound_shndx_index; | |
1f4361a7 | 8041 | unsigned int idx; |
12bd6957 | 8042 | unsigned int num_locals; |
1f4361a7 | 8043 | size_t amt; |
174fd7f9 | 8044 | bfd_boolean name_local_sections; |
252b5132 | 8045 | |
12bd6957 | 8046 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 8047 | return FALSE; |
252b5132 | 8048 | |
c044fabd | 8049 | /* Dump out the symtabs. */ |
ef10c3ac | 8050 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 8051 | if (stt == NULL) |
b34976b6 | 8052 | return FALSE; |
252b5132 | 8053 | |
079e9a2f AM |
8054 | bed = get_elf_backend_data (abfd); |
8055 | symcount = bfd_get_symcount (abfd); | |
8056 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
8057 | symtab_hdr->sh_type = SHT_SYMTAB; | |
8058 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
8059 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 8060 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 8061 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
8062 | |
8063 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
8064 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
8065 | ||
ef10c3ac | 8066 | /* Allocate buffer to swap out the .strtab section. */ |
1f4361a7 AM |
8067 | if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt) |
8068 | || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL) | |
ef10c3ac | 8069 | { |
1f4361a7 | 8070 | bfd_set_error (bfd_error_no_memory); |
ef10c3ac L |
8071 | _bfd_elf_strtab_free (stt); |
8072 | return FALSE; | |
8073 | } | |
8074 | ||
1f4361a7 AM |
8075 | if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt) |
8076 | || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL) | |
5ed6aba4 | 8077 | { |
1f4361a7 AM |
8078 | error_no_mem: |
8079 | bfd_set_error (bfd_error_no_memory); | |
8080 | error_return: | |
ef10c3ac | 8081 | free (symstrtab); |
1f4361a7 | 8082 | _bfd_elf_strtab_free (stt); |
5ed6aba4 NC |
8083 | return FALSE; |
8084 | } | |
217aa764 | 8085 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 8086 | outbound_syms_index = 0; |
252b5132 | 8087 | |
9ad5cbcf | 8088 | outbound_shndx = NULL; |
ef10c3ac | 8089 | outbound_shndx_index = 0; |
6a40cf0c NC |
8090 | |
8091 | if (elf_symtab_shndx_list (abfd)) | |
9ad5cbcf | 8092 | { |
6a40cf0c NC |
8093 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
8094 | if (symtab_shndx_hdr->sh_name != 0) | |
8095 | { | |
1f4361a7 AM |
8096 | if (_bfd_mul_overflow (symcount + 1, |
8097 | sizeof (Elf_External_Sym_Shndx), &amt)) | |
8098 | goto error_no_mem; | |
8099 | outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt); | |
6a40cf0c NC |
8100 | if (outbound_shndx == NULL) |
8101 | goto error_return; | |
5ed6aba4 | 8102 | |
6a40cf0c NC |
8103 | symtab_shndx_hdr->contents = outbound_shndx; |
8104 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
8105 | symtab_shndx_hdr->sh_size = amt; | |
8106 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
8107 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
8108 | } | |
8109 | /* FIXME: What about any other headers in the list ? */ | |
9ad5cbcf AM |
8110 | } |
8111 | ||
589e6347 | 8112 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
8113 | { |
8114 | /* Fill in zeroth symbol and swap it out. */ | |
8115 | Elf_Internal_Sym sym; | |
8116 | sym.st_name = 0; | |
8117 | sym.st_value = 0; | |
8118 | sym.st_size = 0; | |
8119 | sym.st_info = 0; | |
8120 | sym.st_other = 0; | |
8121 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 8122 | sym.st_target_internal = 0; |
ef10c3ac L |
8123 | symstrtab[0].sym = sym; |
8124 | symstrtab[0].dest_index = outbound_syms_index; | |
8125 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
8126 | outbound_syms_index++; | |
9ad5cbcf | 8127 | if (outbound_shndx != NULL) |
ef10c3ac | 8128 | outbound_shndx_index++; |
079e9a2f | 8129 | } |
252b5132 | 8130 | |
174fd7f9 RS |
8131 | name_local_sections |
8132 | = (bed->elf_backend_name_local_section_symbols | |
8133 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
8134 | ||
079e9a2f | 8135 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 8136 | for (idx = 0; idx < symcount;) |
252b5132 | 8137 | { |
252b5132 | 8138 | Elf_Internal_Sym sym; |
079e9a2f AM |
8139 | bfd_vma value = syms[idx]->value; |
8140 | elf_symbol_type *type_ptr; | |
8141 | flagword flags = syms[idx]->flags; | |
8142 | int type; | |
252b5132 | 8143 | |
174fd7f9 RS |
8144 | if (!name_local_sections |
8145 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
8146 | { |
8147 | /* Local section symbols have no name. */ | |
ef10c3ac | 8148 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
8149 | } |
8150 | else | |
8151 | { | |
ef10c3ac L |
8152 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
8153 | to get the final offset for st_name. */ | |
8154 | sym.st_name | |
8155 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
8156 | FALSE); | |
079e9a2f | 8157 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 8158 | goto error_return; |
079e9a2f | 8159 | } |
252b5132 | 8160 | |
079e9a2f | 8161 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 8162 | |
079e9a2f AM |
8163 | if ((flags & BSF_SECTION_SYM) == 0 |
8164 | && bfd_is_com_section (syms[idx]->section)) | |
8165 | { | |
8166 | /* ELF common symbols put the alignment into the `value' field, | |
8167 | and the size into the `size' field. This is backwards from | |
8168 | how BFD handles it, so reverse it here. */ | |
8169 | sym.st_size = value; | |
8170 | if (type_ptr == NULL | |
8171 | || type_ptr->internal_elf_sym.st_value == 0) | |
8172 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
8173 | else | |
8174 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
8175 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
8176 | (abfd, syms[idx]->section); | |
8177 | } | |
8178 | else | |
8179 | { | |
8180 | asection *sec = syms[idx]->section; | |
cb33740c | 8181 | unsigned int shndx; |
252b5132 | 8182 | |
079e9a2f AM |
8183 | if (sec->output_section) |
8184 | { | |
8185 | value += sec->output_offset; | |
8186 | sec = sec->output_section; | |
8187 | } | |
589e6347 | 8188 | |
079e9a2f AM |
8189 | /* Don't add in the section vma for relocatable output. */ |
8190 | if (! relocatable_p) | |
8191 | value += sec->vma; | |
8192 | sym.st_value = value; | |
8193 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
8194 | ||
8195 | if (bfd_is_abs_section (sec) | |
8196 | && type_ptr != NULL | |
8197 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
8198 | { | |
8199 | /* This symbol is in a real ELF section which we did | |
8200 | not create as a BFD section. Undo the mapping done | |
8201 | by copy_private_symbol_data. */ | |
8202 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
8203 | switch (shndx) | |
8204 | { | |
8205 | case MAP_ONESYMTAB: | |
8206 | shndx = elf_onesymtab (abfd); | |
8207 | break; | |
8208 | case MAP_DYNSYMTAB: | |
8209 | shndx = elf_dynsymtab (abfd); | |
8210 | break; | |
8211 | case MAP_STRTAB: | |
12bd6957 | 8212 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
8213 | break; |
8214 | case MAP_SHSTRTAB: | |
12bd6957 | 8215 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 8216 | break; |
9ad5cbcf | 8217 | case MAP_SYM_SHNDX: |
6a40cf0c NC |
8218 | if (elf_symtab_shndx_list (abfd)) |
8219 | shndx = elf_symtab_shndx_list (abfd)->ndx; | |
9ad5cbcf | 8220 | break; |
00e49dff NC |
8221 | case SHN_COMMON: |
8222 | case SHN_ABS: | |
15bc576a | 8223 | shndx = SHN_ABS; |
079e9a2f | 8224 | break; |
00e49dff NC |
8225 | default: |
8226 | if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS) | |
8227 | { | |
8228 | if (bed->symbol_section_index) | |
8229 | shndx = bed->symbol_section_index (abfd, type_ptr); | |
8230 | /* Otherwise just leave the index alone. */ | |
8231 | } | |
8232 | else | |
8233 | { | |
8234 | if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE) | |
8235 | _bfd_error_handler (_("%pB: \ | |
8236 | Unable to handle section index %x in ELF symbol. Using ABS instead."), | |
8237 | abfd, shndx); | |
8238 | shndx = SHN_ABS; | |
8239 | } | |
8240 | break; | |
079e9a2f AM |
8241 | } |
8242 | } | |
8243 | else | |
8244 | { | |
8245 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 8246 | |
cb33740c | 8247 | if (shndx == SHN_BAD) |
079e9a2f AM |
8248 | { |
8249 | asection *sec2; | |
8250 | ||
8251 | /* Writing this would be a hell of a lot easier if | |
8252 | we had some decent documentation on bfd, and | |
8253 | knew what to expect of the library, and what to | |
8254 | demand of applications. For example, it | |
8255 | appears that `objcopy' might not set the | |
8256 | section of a symbol to be a section that is | |
8257 | actually in the output file. */ | |
8258 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
5df1bc57 AM |
8259 | if (sec2 != NULL) |
8260 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
8261 | if (shndx == SHN_BAD) | |
589e6347 | 8262 | { |
695344c0 | 8263 | /* xgettext:c-format */ |
9793eb77 AM |
8264 | _bfd_error_handler |
8265 | (_("unable to find equivalent output section" | |
8266 | " for symbol '%s' from section '%s'"), | |
8267 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
8268 | sec->name); | |
811072d8 | 8269 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 8270 | goto error_return; |
589e6347 | 8271 | } |
079e9a2f AM |
8272 | } |
8273 | } | |
252b5132 | 8274 | |
079e9a2f AM |
8275 | sym.st_shndx = shndx; |
8276 | } | |
252b5132 | 8277 | |
13ae64f3 JJ |
8278 | if ((flags & BSF_THREAD_LOCAL) != 0) |
8279 | type = STT_TLS; | |
d8045f23 NC |
8280 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
8281 | type = STT_GNU_IFUNC; | |
13ae64f3 | 8282 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
8283 | type = STT_FUNC; |
8284 | else if ((flags & BSF_OBJECT) != 0) | |
8285 | type = STT_OBJECT; | |
d9352518 DB |
8286 | else if ((flags & BSF_RELC) != 0) |
8287 | type = STT_RELC; | |
8288 | else if ((flags & BSF_SRELC) != 0) | |
8289 | type = STT_SRELC; | |
079e9a2f AM |
8290 | else |
8291 | type = STT_NOTYPE; | |
252b5132 | 8292 | |
13ae64f3 JJ |
8293 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
8294 | type = STT_TLS; | |
8295 | ||
589e6347 | 8296 | /* Processor-specific types. */ |
079e9a2f AM |
8297 | if (type_ptr != NULL |
8298 | && bed->elf_backend_get_symbol_type) | |
8299 | type = ((*bed->elf_backend_get_symbol_type) | |
8300 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 8301 | |
079e9a2f AM |
8302 | if (flags & BSF_SECTION_SYM) |
8303 | { | |
8304 | if (flags & BSF_GLOBAL) | |
8305 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
8306 | else | |
8307 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8308 | } | |
8309 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 8310 | { |
b8871f35 L |
8311 | if (type != STT_TLS) |
8312 | { | |
8313 | if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) | |
8314 | type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) | |
8315 | ? STT_COMMON : STT_OBJECT); | |
8316 | else | |
8317 | type = ((flags & BSF_ELF_COMMON) != 0 | |
8318 | ? STT_COMMON : STT_OBJECT); | |
8319 | } | |
8320 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
0a40daed | 8321 | } |
079e9a2f AM |
8322 | else if (bfd_is_und_section (syms[idx]->section)) |
8323 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
8324 | ? STB_WEAK | |
8325 | : STB_GLOBAL), | |
8326 | type); | |
8327 | else if (flags & BSF_FILE) | |
8328 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
8329 | else | |
8330 | { | |
8331 | int bind = STB_LOCAL; | |
252b5132 | 8332 | |
079e9a2f AM |
8333 | if (flags & BSF_LOCAL) |
8334 | bind = STB_LOCAL; | |
3e7a7d11 NC |
8335 | else if (flags & BSF_GNU_UNIQUE) |
8336 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
8337 | else if (flags & BSF_WEAK) |
8338 | bind = STB_WEAK; | |
8339 | else if (flags & BSF_GLOBAL) | |
8340 | bind = STB_GLOBAL; | |
252b5132 | 8341 | |
079e9a2f AM |
8342 | sym.st_info = ELF_ST_INFO (bind, type); |
8343 | } | |
252b5132 | 8344 | |
079e9a2f | 8345 | if (type_ptr != NULL) |
35fc36a8 RS |
8346 | { |
8347 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
8348 | sym.st_target_internal | |
8349 | = type_ptr->internal_elf_sym.st_target_internal; | |
8350 | } | |
079e9a2f | 8351 | else |
35fc36a8 RS |
8352 | { |
8353 | sym.st_other = 0; | |
8354 | sym.st_target_internal = 0; | |
8355 | } | |
252b5132 | 8356 | |
ef10c3ac L |
8357 | idx++; |
8358 | symstrtab[idx].sym = sym; | |
8359 | symstrtab[idx].dest_index = outbound_syms_index; | |
8360 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
8361 | ||
8362 | outbound_syms_index++; | |
9ad5cbcf | 8363 | if (outbound_shndx != NULL) |
ef10c3ac L |
8364 | outbound_shndx_index++; |
8365 | } | |
8366 | ||
8367 | /* Finalize the .strtab section. */ | |
8368 | _bfd_elf_strtab_finalize (stt); | |
8369 | ||
8370 | /* Swap out the .strtab section. */ | |
8371 | for (idx = 0; idx <= symcount; idx++) | |
8372 | { | |
8373 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
8374 | if (elfsym->sym.st_name == (unsigned long) -1) | |
8375 | elfsym->sym.st_name = 0; | |
8376 | else | |
8377 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
8378 | elfsym->sym.st_name); | |
8379 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
8380 | (outbound_syms | |
8381 | + (elfsym->dest_index | |
8382 | * bed->s->sizeof_sym)), | |
8383 | (outbound_shndx | |
8384 | + (elfsym->destshndx_index | |
8385 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 8386 | } |
ef10c3ac | 8387 | free (symstrtab); |
252b5132 | 8388 | |
079e9a2f | 8389 | *sttp = stt; |
ef10c3ac | 8390 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 8391 | symstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 8392 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
079e9a2f AM |
8393 | symstrtab_hdr->sh_addr = 0; |
8394 | symstrtab_hdr->sh_entsize = 0; | |
8395 | symstrtab_hdr->sh_link = 0; | |
8396 | symstrtab_hdr->sh_info = 0; | |
8397 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 8398 | |
b34976b6 | 8399 | return TRUE; |
252b5132 RH |
8400 | } |
8401 | ||
8402 | /* Return the number of bytes required to hold the symtab vector. | |
8403 | ||
8404 | Note that we base it on the count plus 1, since we will null terminate | |
8405 | the vector allocated based on this size. However, the ELF symbol table | |
8406 | always has a dummy entry as symbol #0, so it ends up even. */ | |
8407 | ||
8408 | long | |
217aa764 | 8409 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 | 8410 | { |
3a551c7a | 8411 | bfd_size_type symcount; |
252b5132 RH |
8412 | long symtab_size; |
8413 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
8414 | ||
8415 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3a551c7a AM |
8416 | if (symcount >= LONG_MAX / sizeof (asymbol *)) |
8417 | { | |
8418 | bfd_set_error (bfd_error_file_too_big); | |
8419 | return -1; | |
8420 | } | |
b99d1833 AM |
8421 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8422 | if (symcount > 0) | |
8423 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8424 | |
8425 | return symtab_size; | |
8426 | } | |
8427 | ||
8428 | long | |
217aa764 | 8429 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 | 8430 | { |
3a551c7a | 8431 | bfd_size_type symcount; |
252b5132 RH |
8432 | long symtab_size; |
8433 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
8434 | ||
8435 | if (elf_dynsymtab (abfd) == 0) | |
8436 | { | |
8437 | bfd_set_error (bfd_error_invalid_operation); | |
8438 | return -1; | |
8439 | } | |
8440 | ||
8441 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3a551c7a AM |
8442 | if (symcount >= LONG_MAX / sizeof (asymbol *)) |
8443 | { | |
8444 | bfd_set_error (bfd_error_file_too_big); | |
8445 | return -1; | |
8446 | } | |
b99d1833 AM |
8447 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8448 | if (symcount > 0) | |
8449 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8450 | |
8451 | return symtab_size; | |
8452 | } | |
8453 | ||
8454 | long | |
217aa764 AM |
8455 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
8456 | sec_ptr asect) | |
252b5132 | 8457 | { |
242a1159 | 8458 | #if SIZEOF_LONG == SIZEOF_INT |
7a6e0d89 AM |
8459 | if (asect->reloc_count >= LONG_MAX / sizeof (arelent *)) |
8460 | { | |
8461 | bfd_set_error (bfd_error_file_too_big); | |
8462 | return -1; | |
8463 | } | |
242a1159 | 8464 | #endif |
252b5132 RH |
8465 | return (asect->reloc_count + 1) * sizeof (arelent *); |
8466 | } | |
8467 | ||
8468 | /* Canonicalize the relocs. */ | |
8469 | ||
8470 | long | |
217aa764 AM |
8471 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
8472 | sec_ptr section, | |
8473 | arelent **relptr, | |
8474 | asymbol **symbols) | |
252b5132 RH |
8475 | { |
8476 | arelent *tblptr; | |
8477 | unsigned int i; | |
9c5bfbb7 | 8478 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 8479 | |
b34976b6 | 8480 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
8481 | return -1; |
8482 | ||
8483 | tblptr = section->relocation; | |
8484 | for (i = 0; i < section->reloc_count; i++) | |
8485 | *relptr++ = tblptr++; | |
8486 | ||
8487 | *relptr = NULL; | |
8488 | ||
8489 | return section->reloc_count; | |
8490 | } | |
8491 | ||
8492 | long | |
6cee3f79 | 8493 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 8494 | { |
9c5bfbb7 | 8495 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8496 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
8497 | |
8498 | if (symcount >= 0) | |
ed48ec2e | 8499 | abfd->symcount = symcount; |
252b5132 RH |
8500 | return symcount; |
8501 | } | |
8502 | ||
8503 | long | |
217aa764 AM |
8504 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
8505 | asymbol **allocation) | |
252b5132 | 8506 | { |
9c5bfbb7 | 8507 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8508 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
8509 | |
8510 | if (symcount >= 0) | |
ed48ec2e | 8511 | abfd->dynsymcount = symcount; |
1f70368c | 8512 | return symcount; |
252b5132 RH |
8513 | } |
8514 | ||
8615f3f2 AM |
8515 | /* Return the size required for the dynamic reloc entries. Any loadable |
8516 | section that was actually installed in the BFD, and has type SHT_REL | |
8517 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
8518 | dynamic reloc section. */ | |
252b5132 RH |
8519 | |
8520 | long | |
217aa764 | 8521 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 | 8522 | { |
3a551c7a | 8523 | bfd_size_type count; |
252b5132 RH |
8524 | asection *s; |
8525 | ||
8526 | if (elf_dynsymtab (abfd) == 0) | |
8527 | { | |
8528 | bfd_set_error (bfd_error_invalid_operation); | |
8529 | return -1; | |
8530 | } | |
8531 | ||
3a551c7a | 8532 | count = 1; |
252b5132 | 8533 | for (s = abfd->sections; s != NULL; s = s->next) |
266b05cf | 8534 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8535 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8536 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
3a551c7a AM |
8537 | { |
8538 | count += s->size / elf_section_data (s)->this_hdr.sh_entsize; | |
8539 | if (count > LONG_MAX / sizeof (arelent *)) | |
8540 | { | |
8541 | bfd_set_error (bfd_error_file_too_big); | |
8542 | return -1; | |
8543 | } | |
8544 | } | |
8545 | return count * sizeof (arelent *); | |
252b5132 RH |
8546 | } |
8547 | ||
8615f3f2 AM |
8548 | /* Canonicalize the dynamic relocation entries. Note that we return the |
8549 | dynamic relocations as a single block, although they are actually | |
8550 | associated with particular sections; the interface, which was | |
8551 | designed for SunOS style shared libraries, expects that there is only | |
8552 | one set of dynamic relocs. Any loadable section that was actually | |
8553 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
8554 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
8555 | |
8556 | long | |
217aa764 AM |
8557 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
8558 | arelent **storage, | |
8559 | asymbol **syms) | |
252b5132 | 8560 | { |
217aa764 | 8561 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
8562 | asection *s; |
8563 | long ret; | |
8564 | ||
8565 | if (elf_dynsymtab (abfd) == 0) | |
8566 | { | |
8567 | bfd_set_error (bfd_error_invalid_operation); | |
8568 | return -1; | |
8569 | } | |
8570 | ||
8571 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
8572 | ret = 0; | |
8573 | for (s = abfd->sections; s != NULL; s = s->next) | |
8574 | { | |
266b05cf | 8575 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8576 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8577 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
8578 | { | |
8579 | arelent *p; | |
8580 | long count, i; | |
8581 | ||
b34976b6 | 8582 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 8583 | return -1; |
eea6121a | 8584 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
8585 | p = s->relocation; |
8586 | for (i = 0; i < count; i++) | |
8587 | *storage++ = p++; | |
8588 | ret += count; | |
8589 | } | |
8590 | } | |
8591 | ||
8592 | *storage = NULL; | |
8593 | ||
8594 | return ret; | |
8595 | } | |
8596 | \f | |
8597 | /* Read in the version information. */ | |
8598 | ||
b34976b6 | 8599 | bfd_boolean |
fc0e6df6 | 8600 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
8601 | { |
8602 | bfd_byte *contents = NULL; | |
fc0e6df6 | 8603 | unsigned int freeidx = 0; |
1f4361a7 | 8604 | size_t amt; |
fc0e6df6 PB |
8605 | |
8606 | if (elf_dynverref (abfd) != 0) | |
8607 | { | |
8608 | Elf_Internal_Shdr *hdr; | |
8609 | Elf_External_Verneed *everneed; | |
8610 | Elf_Internal_Verneed *iverneed; | |
8611 | unsigned int i; | |
d0fb9a8d | 8612 | bfd_byte *contents_end; |
fc0e6df6 PB |
8613 | |
8614 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
8615 | ||
bd61e135 AM |
8616 | if (hdr->sh_info == 0 |
8617 | || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed)) | |
d0fb9a8d | 8618 | { |
dc1e8a47 | 8619 | error_return_bad_verref: |
4eca0228 | 8620 | _bfd_error_handler |
871b3ab2 | 8621 | (_("%pB: .gnu.version_r invalid entry"), abfd); |
601a03ba | 8622 | bfd_set_error (bfd_error_bad_value); |
dc1e8a47 | 8623 | error_return_verref: |
d0fb9a8d JJ |
8624 | elf_tdata (abfd)->verref = NULL; |
8625 | elf_tdata (abfd)->cverrefs = 0; | |
8626 | goto error_return; | |
8627 | } | |
601a03ba | 8628 | |
2bb3687b AM |
8629 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) |
8630 | goto error_return_verref; | |
8631 | contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size); | |
8632 | if (contents == NULL) | |
d0fb9a8d | 8633 | goto error_return_verref; |
fc0e6df6 | 8634 | |
1f4361a7 AM |
8635 | if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt)) |
8636 | { | |
8637 | bfd_set_error (bfd_error_file_too_big); | |
8638 | goto error_return_verref; | |
8639 | } | |
8640 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt); | |
601a03ba | 8641 | if (elf_tdata (abfd)->verref == NULL) |
d0fb9a8d JJ |
8642 | goto error_return_verref; |
8643 | ||
8644 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
8645 | == sizeof (Elf_External_Vernaux)); | |
8646 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
8647 | everneed = (Elf_External_Verneed *) contents; |
8648 | iverneed = elf_tdata (abfd)->verref; | |
8649 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
8650 | { | |
8651 | Elf_External_Vernaux *evernaux; | |
8652 | Elf_Internal_Vernaux *ivernaux; | |
8653 | unsigned int j; | |
8654 | ||
8655 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
8656 | ||
8657 | iverneed->vn_bfd = abfd; | |
8658 | ||
8659 | iverneed->vn_filename = | |
8660 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8661 | iverneed->vn_file); | |
8662 | if (iverneed->vn_filename == NULL) | |
601a03ba | 8663 | goto error_return_bad_verref; |
fc0e6df6 | 8664 | |
d0fb9a8d JJ |
8665 | if (iverneed->vn_cnt == 0) |
8666 | iverneed->vn_auxptr = NULL; | |
8667 | else | |
8668 | { | |
1f4361a7 AM |
8669 | if (_bfd_mul_overflow (iverneed->vn_cnt, |
8670 | sizeof (Elf_Internal_Vernaux), &amt)) | |
8671 | { | |
8672 | bfd_set_error (bfd_error_file_too_big); | |
8673 | goto error_return_verref; | |
8674 | } | |
a50b1753 | 8675 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
1f4361a7 | 8676 | bfd_alloc (abfd, amt); |
d0fb9a8d JJ |
8677 | if (iverneed->vn_auxptr == NULL) |
8678 | goto error_return_verref; | |
8679 | } | |
8680 | ||
8681 | if (iverneed->vn_aux | |
8682 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8683 | goto error_return_bad_verref; |
fc0e6df6 PB |
8684 | |
8685 | evernaux = ((Elf_External_Vernaux *) | |
8686 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
8687 | ivernaux = iverneed->vn_auxptr; | |
8688 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
8689 | { | |
8690 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
8691 | ||
8692 | ivernaux->vna_nodename = | |
8693 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8694 | ivernaux->vna_name); | |
8695 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 8696 | goto error_return_bad_verref; |
fc0e6df6 | 8697 | |
25ff461f AM |
8698 | if (ivernaux->vna_other > freeidx) |
8699 | freeidx = ivernaux->vna_other; | |
8700 | ||
8701 | ivernaux->vna_nextptr = NULL; | |
8702 | if (ivernaux->vna_next == 0) | |
8703 | { | |
8704 | iverneed->vn_cnt = j + 1; | |
8705 | break; | |
8706 | } | |
fc0e6df6 PB |
8707 | if (j + 1 < iverneed->vn_cnt) |
8708 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 8709 | |
d0fb9a8d JJ |
8710 | if (ivernaux->vna_next |
8711 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 8712 | goto error_return_bad_verref; |
d0fb9a8d | 8713 | |
fc0e6df6 PB |
8714 | evernaux = ((Elf_External_Vernaux *) |
8715 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
8716 | } |
8717 | ||
25ff461f AM |
8718 | iverneed->vn_nextref = NULL; |
8719 | if (iverneed->vn_next == 0) | |
8720 | break; | |
fc0e6df6 PB |
8721 | if (i + 1 < hdr->sh_info) |
8722 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 8723 | |
d0fb9a8d JJ |
8724 | if (iverneed->vn_next |
8725 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8726 | goto error_return_bad_verref; |
d0fb9a8d | 8727 | |
fc0e6df6 PB |
8728 | everneed = ((Elf_External_Verneed *) |
8729 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
8730 | } | |
25ff461f | 8731 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
8732 | |
8733 | free (contents); | |
8734 | contents = NULL; | |
8735 | } | |
252b5132 RH |
8736 | |
8737 | if (elf_dynverdef (abfd) != 0) | |
8738 | { | |
8739 | Elf_Internal_Shdr *hdr; | |
8740 | Elf_External_Verdef *everdef; | |
8741 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
8742 | Elf_Internal_Verdef *iverdefarr; |
8743 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 8744 | unsigned int i; |
062e2358 | 8745 | unsigned int maxidx; |
d0fb9a8d | 8746 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
8747 | |
8748 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
8749 | ||
601a03ba AM |
8750 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
8751 | { | |
8752 | error_return_bad_verdef: | |
4eca0228 | 8753 | _bfd_error_handler |
871b3ab2 | 8754 | (_("%pB: .gnu.version_d invalid entry"), abfd); |
601a03ba AM |
8755 | bfd_set_error (bfd_error_bad_value); |
8756 | error_return_verdef: | |
8757 | elf_tdata (abfd)->verdef = NULL; | |
8758 | elf_tdata (abfd)->cverdefs = 0; | |
8759 | goto error_return; | |
8760 | } | |
8761 | ||
2bb3687b | 8762 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) |
601a03ba | 8763 | goto error_return_verdef; |
2bb3687b AM |
8764 | contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size); |
8765 | if (contents == NULL) | |
601a03ba | 8766 | goto error_return_verdef; |
d0fb9a8d JJ |
8767 | |
8768 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
8769 | >= sizeof (Elf_External_Verdaux)); | |
8770 | contents_end_def = contents + hdr->sh_size | |
8771 | - sizeof (Elf_External_Verdef); | |
8772 | contents_end_aux = contents + hdr->sh_size | |
8773 | - sizeof (Elf_External_Verdaux); | |
8774 | ||
f631889e UD |
8775 | /* We know the number of entries in the section but not the maximum |
8776 | index. Therefore we have to run through all entries and find | |
8777 | the maximum. */ | |
252b5132 | 8778 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
8779 | maxidx = 0; |
8780 | for (i = 0; i < hdr->sh_info; ++i) | |
8781 | { | |
8782 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
8783 | ||
601a03ba AM |
8784 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
8785 | goto error_return_bad_verdef; | |
062e2358 AM |
8786 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
8787 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 8788 | |
25ff461f AM |
8789 | if (iverdefmem.vd_next == 0) |
8790 | break; | |
8791 | ||
d0fb9a8d JJ |
8792 | if (iverdefmem.vd_next |
8793 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 8794 | goto error_return_bad_verdef; |
d0fb9a8d | 8795 | |
f631889e UD |
8796 | everdef = ((Elf_External_Verdef *) |
8797 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
8798 | } | |
8799 | ||
fc0e6df6 PB |
8800 | if (default_imported_symver) |
8801 | { | |
8802 | if (freeidx > maxidx) | |
8803 | maxidx = ++freeidx; | |
8804 | else | |
8805 | freeidx = ++maxidx; | |
8806 | } | |
1f4361a7 AM |
8807 | if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt)) |
8808 | { | |
8809 | bfd_set_error (bfd_error_file_too_big); | |
8810 | goto error_return_verdef; | |
8811 | } | |
8812 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); | |
f631889e | 8813 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 8814 | goto error_return_verdef; |
f631889e UD |
8815 | |
8816 | elf_tdata (abfd)->cverdefs = maxidx; | |
8817 | ||
8818 | everdef = (Elf_External_Verdef *) contents; | |
8819 | iverdefarr = elf_tdata (abfd)->verdef; | |
8820 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
8821 | { |
8822 | Elf_External_Verdaux *everdaux; | |
8823 | Elf_Internal_Verdaux *iverdaux; | |
8824 | unsigned int j; | |
8825 | ||
f631889e UD |
8826 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
8827 | ||
d0fb9a8d | 8828 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 8829 | goto error_return_bad_verdef; |
d0fb9a8d | 8830 | |
f631889e | 8831 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
595bce75 | 8832 | memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); |
252b5132 RH |
8833 | |
8834 | iverdef->vd_bfd = abfd; | |
8835 | ||
d0fb9a8d JJ |
8836 | if (iverdef->vd_cnt == 0) |
8837 | iverdef->vd_auxptr = NULL; | |
8838 | else | |
8839 | { | |
1f4361a7 AM |
8840 | if (_bfd_mul_overflow (iverdef->vd_cnt, |
8841 | sizeof (Elf_Internal_Verdaux), &amt)) | |
8842 | { | |
8843 | bfd_set_error (bfd_error_file_too_big); | |
8844 | goto error_return_verdef; | |
8845 | } | |
a50b1753 | 8846 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
1f4361a7 | 8847 | bfd_alloc (abfd, amt); |
d0fb9a8d JJ |
8848 | if (iverdef->vd_auxptr == NULL) |
8849 | goto error_return_verdef; | |
8850 | } | |
8851 | ||
8852 | if (iverdef->vd_aux | |
8853 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 8854 | goto error_return_bad_verdef; |
252b5132 RH |
8855 | |
8856 | everdaux = ((Elf_External_Verdaux *) | |
8857 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
8858 | iverdaux = iverdef->vd_auxptr; | |
8859 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
8860 | { | |
8861 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
8862 | ||
8863 | iverdaux->vda_nodename = | |
8864 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8865 | iverdaux->vda_name); | |
8866 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 8867 | goto error_return_bad_verdef; |
252b5132 | 8868 | |
25ff461f AM |
8869 | iverdaux->vda_nextptr = NULL; |
8870 | if (iverdaux->vda_next == 0) | |
8871 | { | |
8872 | iverdef->vd_cnt = j + 1; | |
8873 | break; | |
8874 | } | |
252b5132 RH |
8875 | if (j + 1 < iverdef->vd_cnt) |
8876 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 8877 | |
d0fb9a8d JJ |
8878 | if (iverdaux->vda_next |
8879 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 8880 | goto error_return_bad_verdef; |
d0fb9a8d | 8881 | |
252b5132 RH |
8882 | everdaux = ((Elf_External_Verdaux *) |
8883 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
8884 | } | |
8885 | ||
595bce75 | 8886 | iverdef->vd_nodename = NULL; |
d0fb9a8d JJ |
8887 | if (iverdef->vd_cnt) |
8888 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 8889 | |
25ff461f AM |
8890 | iverdef->vd_nextdef = NULL; |
8891 | if (iverdef->vd_next == 0) | |
8892 | break; | |
d0fb9a8d | 8893 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 8894 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
8895 | |
8896 | everdef = ((Elf_External_Verdef *) | |
8897 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
8898 | } | |
8899 | ||
8900 | free (contents); | |
8901 | contents = NULL; | |
8902 | } | |
fc0e6df6 | 8903 | else if (default_imported_symver) |
252b5132 | 8904 | { |
fc0e6df6 PB |
8905 | if (freeidx < 3) |
8906 | freeidx = 3; | |
8907 | else | |
8908 | freeidx++; | |
252b5132 | 8909 | |
1f4361a7 AM |
8910 | if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt)) |
8911 | { | |
8912 | bfd_set_error (bfd_error_file_too_big); | |
8913 | goto error_return; | |
8914 | } | |
8915 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); | |
fc0e6df6 | 8916 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
8917 | goto error_return; |
8918 | ||
fc0e6df6 PB |
8919 | elf_tdata (abfd)->cverdefs = freeidx; |
8920 | } | |
252b5132 | 8921 | |
fc0e6df6 PB |
8922 | /* Create a default version based on the soname. */ |
8923 | if (default_imported_symver) | |
8924 | { | |
8925 | Elf_Internal_Verdef *iverdef; | |
8926 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 8927 | |
5bb3703f | 8928 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 8929 | |
fc0e6df6 PB |
8930 | iverdef->vd_version = VER_DEF_CURRENT; |
8931 | iverdef->vd_flags = 0; | |
8932 | iverdef->vd_ndx = freeidx; | |
8933 | iverdef->vd_cnt = 1; | |
252b5132 | 8934 | |
fc0e6df6 | 8935 | iverdef->vd_bfd = abfd; |
252b5132 | 8936 | |
fc0e6df6 PB |
8937 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
8938 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 8939 | goto error_return_verdef; |
fc0e6df6 | 8940 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
8941 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
8942 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
8943 | if (iverdef->vd_auxptr == NULL) |
8944 | goto error_return_verdef; | |
252b5132 | 8945 | |
fc0e6df6 PB |
8946 | iverdaux = iverdef->vd_auxptr; |
8947 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
8948 | } |
8949 | ||
b34976b6 | 8950 | return TRUE; |
252b5132 RH |
8951 | |
8952 | error_return: | |
5ed6aba4 | 8953 | if (contents != NULL) |
252b5132 | 8954 | free (contents); |
b34976b6 | 8955 | return FALSE; |
252b5132 RH |
8956 | } |
8957 | \f | |
8958 | asymbol * | |
217aa764 | 8959 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
8960 | { |
8961 | elf_symbol_type *newsym; | |
8962 | ||
7a6e0d89 | 8963 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym)); |
252b5132 RH |
8964 | if (!newsym) |
8965 | return NULL; | |
201159ec NC |
8966 | newsym->symbol.the_bfd = abfd; |
8967 | return &newsym->symbol; | |
252b5132 RH |
8968 | } |
8969 | ||
8970 | void | |
217aa764 AM |
8971 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
8972 | asymbol *symbol, | |
8973 | symbol_info *ret) | |
252b5132 RH |
8974 | { |
8975 | bfd_symbol_info (symbol, ret); | |
8976 | } | |
8977 | ||
8978 | /* Return whether a symbol name implies a local symbol. Most targets | |
8979 | use this function for the is_local_label_name entry point, but some | |
8980 | override it. */ | |
8981 | ||
b34976b6 | 8982 | bfd_boolean |
217aa764 AM |
8983 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
8984 | const char *name) | |
252b5132 RH |
8985 | { |
8986 | /* Normal local symbols start with ``.L''. */ | |
8987 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 8988 | return TRUE; |
252b5132 RH |
8989 | |
8990 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
8991 | DWARF debugging symbols starting with ``..''. */ | |
8992 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 8993 | return TRUE; |
252b5132 RH |
8994 | |
8995 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
8996 | emitting DWARF debugging output. I suspect this is actually a | |
8997 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
8998 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
8999 | underscore to be emitted on some ELF targets). For ease of use, | |
9000 | we treat such symbols as local. */ | |
9001 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 9002 | return TRUE; |
252b5132 | 9003 | |
b1fa9dd6 NC |
9004 | /* Treat assembler generated fake symbols, dollar local labels and |
9005 | forward-backward labels (aka local labels) as locals. | |
9006 | These labels have the form: | |
9007 | ||
07d6d2b8 | 9008 | L0^A.* (fake symbols) |
b1fa9dd6 NC |
9009 | |
9010 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
9011 | ||
9012 | Versions which start with .L will have already been matched above, | |
9013 | so we only need to match the rest. */ | |
9014 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
9015 | { | |
9016 | bfd_boolean ret = FALSE; | |
9017 | const char * p; | |
9018 | char c; | |
9019 | ||
9020 | for (p = name + 2; (c = *p); p++) | |
9021 | { | |
9022 | if (c == 1 || c == 2) | |
9023 | { | |
9024 | if (c == 1 && p == name + 2) | |
9025 | /* A fake symbol. */ | |
9026 | return TRUE; | |
9027 | ||
9028 | /* FIXME: We are being paranoid here and treating symbols like | |
9029 | L0^Bfoo as if there were non-local, on the grounds that the | |
9030 | assembler will never generate them. But can any symbol | |
9031 | containing an ASCII value in the range 1-31 ever be anything | |
9032 | other than some kind of local ? */ | |
9033 | ret = TRUE; | |
9034 | } | |
9035 | ||
9036 | if (! ISDIGIT (c)) | |
9037 | { | |
9038 | ret = FALSE; | |
9039 | break; | |
9040 | } | |
9041 | } | |
9042 | return ret; | |
9043 | } | |
ffa54770 | 9044 | |
b34976b6 | 9045 | return FALSE; |
252b5132 RH |
9046 | } |
9047 | ||
9048 | alent * | |
217aa764 AM |
9049 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
9050 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
9051 | { |
9052 | abort (); | |
9053 | return NULL; | |
9054 | } | |
9055 | ||
b34976b6 | 9056 | bfd_boolean |
217aa764 AM |
9057 | _bfd_elf_set_arch_mach (bfd *abfd, |
9058 | enum bfd_architecture arch, | |
9059 | unsigned long machine) | |
252b5132 RH |
9060 | { |
9061 | /* If this isn't the right architecture for this backend, and this | |
9062 | isn't the generic backend, fail. */ | |
9063 | if (arch != get_elf_backend_data (abfd)->arch | |
9064 | && arch != bfd_arch_unknown | |
9065 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 9066 | return FALSE; |
252b5132 RH |
9067 | |
9068 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
9069 | } | |
9070 | ||
d1fad7c6 NC |
9071 | /* Find the nearest line to a particular section and offset, |
9072 | for error reporting. */ | |
9073 | ||
b34976b6 | 9074 | bfd_boolean |
217aa764 | 9075 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 9076 | asymbol **symbols, |
fb167eb2 | 9077 | asection *section, |
217aa764 AM |
9078 | bfd_vma offset, |
9079 | const char **filename_ptr, | |
9080 | const char **functionname_ptr, | |
fb167eb2 AM |
9081 | unsigned int *line_ptr, |
9082 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 9083 | { |
b34976b6 | 9084 | bfd_boolean found; |
d1fad7c6 | 9085 | |
fb167eb2 | 9086 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 9087 | filename_ptr, functionname_ptr, |
fb167eb2 | 9088 | line_ptr, discriminator_ptr, |
9defd221 | 9089 | dwarf_debug_sections, |
e7679060 AM |
9090 | &elf_tdata (abfd)->dwarf2_find_line_info)) |
9091 | return TRUE; | |
9092 | ||
9093 | if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
9094 | filename_ptr, functionname_ptr, line_ptr)) | |
d1fad7c6 NC |
9095 | { |
9096 | if (!*functionname_ptr) | |
e00e8198 AM |
9097 | _bfd_elf_find_function (abfd, symbols, section, offset, |
9098 | *filename_ptr ? NULL : filename_ptr, | |
9099 | functionname_ptr); | |
b34976b6 | 9100 | return TRUE; |
d1fad7c6 NC |
9101 | } |
9102 | ||
9103 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
9104 | &found, filename_ptr, |
9105 | functionname_ptr, line_ptr, | |
9106 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 9107 | return FALSE; |
dc43ada5 | 9108 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 9109 | return TRUE; |
d1fad7c6 NC |
9110 | |
9111 | if (symbols == NULL) | |
b34976b6 | 9112 | return FALSE; |
d1fad7c6 | 9113 | |
e00e8198 AM |
9114 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
9115 | filename_ptr, functionname_ptr)) | |
b34976b6 | 9116 | return FALSE; |
d1fad7c6 | 9117 | |
252b5132 | 9118 | *line_ptr = 0; |
b34976b6 | 9119 | return TRUE; |
252b5132 RH |
9120 | } |
9121 | ||
5420f73d L |
9122 | /* Find the line for a symbol. */ |
9123 | ||
9124 | bfd_boolean | |
9125 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
9126 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 9127 | { |
fb167eb2 AM |
9128 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
9129 | filename_ptr, NULL, line_ptr, NULL, | |
9defd221 | 9130 | dwarf_debug_sections, |
fb167eb2 | 9131 | &elf_tdata (abfd)->dwarf2_find_line_info); |
5420f73d L |
9132 | } |
9133 | ||
4ab527b0 FF |
9134 | /* After a call to bfd_find_nearest_line, successive calls to |
9135 | bfd_find_inliner_info can be used to get source information about | |
9136 | each level of function inlining that terminated at the address | |
9137 | passed to bfd_find_nearest_line. Currently this is only supported | |
9138 | for DWARF2 with appropriate DWARF3 extensions. */ | |
9139 | ||
9140 | bfd_boolean | |
9141 | _bfd_elf_find_inliner_info (bfd *abfd, | |
9142 | const char **filename_ptr, | |
9143 | const char **functionname_ptr, | |
9144 | unsigned int *line_ptr) | |
9145 | { | |
9146 | bfd_boolean found; | |
9147 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
9148 | functionname_ptr, line_ptr, | |
9149 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
9150 | return found; | |
9151 | } | |
9152 | ||
252b5132 | 9153 | int |
a6b96beb | 9154 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 9155 | { |
8ded5a0f AM |
9156 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
9157 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 9158 | |
0e1862bb | 9159 | if (!bfd_link_relocatable (info)) |
8ded5a0f | 9160 | { |
12bd6957 | 9161 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 9162 | |
62d7a5f6 AM |
9163 | if (phdr_size == (bfd_size_type) -1) |
9164 | { | |
9165 | struct elf_segment_map *m; | |
9166 | ||
9167 | phdr_size = 0; | |
12bd6957 | 9168 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 9169 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 9170 | |
62d7a5f6 AM |
9171 | if (phdr_size == 0) |
9172 | phdr_size = get_program_header_size (abfd, info); | |
9173 | } | |
8ded5a0f | 9174 | |
12bd6957 | 9175 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
9176 | ret += phdr_size; |
9177 | } | |
9178 | ||
252b5132 RH |
9179 | return ret; |
9180 | } | |
9181 | ||
b34976b6 | 9182 | bfd_boolean |
217aa764 AM |
9183 | _bfd_elf_set_section_contents (bfd *abfd, |
9184 | sec_ptr section, | |
0f867abe | 9185 | const void *location, |
217aa764 AM |
9186 | file_ptr offset, |
9187 | bfd_size_type count) | |
252b5132 RH |
9188 | { |
9189 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 9190 | file_ptr pos; |
252b5132 RH |
9191 | |
9192 | if (! abfd->output_has_begun | |
217aa764 | 9193 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 9194 | return FALSE; |
252b5132 | 9195 | |
0ce398f1 L |
9196 | if (!count) |
9197 | return TRUE; | |
9198 | ||
252b5132 | 9199 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
9200 | if (hdr->sh_offset == (file_ptr) -1) |
9201 | { | |
a0dcf297 NC |
9202 | unsigned char *contents; |
9203 | ||
1ff6de03 NA |
9204 | if (bfd_section_is_ctf (section)) |
9205 | /* Nothing to do with this section: the contents are generated | |
9206 | later. */ | |
9207 | return TRUE; | |
9208 | ||
a0dcf297 NC |
9209 | if ((section->flags & SEC_ELF_COMPRESS) == 0) |
9210 | { | |
9211 | _bfd_error_handler | |
9212 | (_("%pB:%pA: error: attempting to write into an unallocated compressed section"), | |
9213 | abfd, section); | |
9214 | bfd_set_error (bfd_error_invalid_operation); | |
9215 | return FALSE; | |
9216 | } | |
9217 | ||
9218 | if ((offset + count) > hdr->sh_size) | |
9219 | { | |
9220 | _bfd_error_handler | |
9221 | (_("%pB:%pA: error: attempting to write over the end of the section"), | |
9222 | abfd, section); | |
9223 | ||
9224 | bfd_set_error (bfd_error_invalid_operation); | |
9225 | return FALSE; | |
9226 | } | |
9227 | ||
9228 | contents = hdr->contents; | |
9229 | if (contents == NULL) | |
9230 | { | |
9231 | _bfd_error_handler | |
9232 | (_("%pB:%pA: error: attempting to write section into an empty buffer"), | |
9233 | abfd, section); | |
9234 | ||
9235 | bfd_set_error (bfd_error_invalid_operation); | |
9236 | return FALSE; | |
9237 | } | |
9238 | ||
0ce398f1 L |
9239 | memcpy (contents + offset, location, count); |
9240 | return TRUE; | |
9241 | } | |
a0dcf297 | 9242 | |
dc810e39 AM |
9243 | pos = hdr->sh_offset + offset; |
9244 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
9245 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 9246 | return FALSE; |
252b5132 | 9247 | |
b34976b6 | 9248 | return TRUE; |
252b5132 RH |
9249 | } |
9250 | ||
f3185997 | 9251 | bfd_boolean |
217aa764 AM |
9252 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
9253 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
9254 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
9255 | { |
9256 | abort (); | |
f3185997 | 9257 | return FALSE; |
252b5132 RH |
9258 | } |
9259 | ||
252b5132 RH |
9260 | /* Try to convert a non-ELF reloc into an ELF one. */ |
9261 | ||
b34976b6 | 9262 | bfd_boolean |
217aa764 | 9263 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 9264 | { |
c044fabd | 9265 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
9266 | |
9267 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
9268 | { | |
9269 | bfd_reloc_code_real_type code; | |
9270 | reloc_howto_type *howto; | |
9271 | ||
9272 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 9273 | equivalent ELF reloc. */ |
252b5132 RH |
9274 | |
9275 | if (areloc->howto->pc_relative) | |
9276 | { | |
9277 | switch (areloc->howto->bitsize) | |
9278 | { | |
9279 | case 8: | |
9280 | code = BFD_RELOC_8_PCREL; | |
9281 | break; | |
9282 | case 12: | |
9283 | code = BFD_RELOC_12_PCREL; | |
9284 | break; | |
9285 | case 16: | |
9286 | code = BFD_RELOC_16_PCREL; | |
9287 | break; | |
9288 | case 24: | |
9289 | code = BFD_RELOC_24_PCREL; | |
9290 | break; | |
9291 | case 32: | |
9292 | code = BFD_RELOC_32_PCREL; | |
9293 | break; | |
9294 | case 64: | |
9295 | code = BFD_RELOC_64_PCREL; | |
9296 | break; | |
9297 | default: | |
9298 | goto fail; | |
9299 | } | |
9300 | ||
9301 | howto = bfd_reloc_type_lookup (abfd, code); | |
9302 | ||
94698d01 | 9303 | if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset) |
252b5132 RH |
9304 | { |
9305 | if (howto->pcrel_offset) | |
9306 | areloc->addend += areloc->address; | |
9307 | else | |
9308 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
9309 | } | |
9310 | } | |
9311 | else | |
9312 | { | |
9313 | switch (areloc->howto->bitsize) | |
9314 | { | |
9315 | case 8: | |
9316 | code = BFD_RELOC_8; | |
9317 | break; | |
9318 | case 14: | |
9319 | code = BFD_RELOC_14; | |
9320 | break; | |
9321 | case 16: | |
9322 | code = BFD_RELOC_16; | |
9323 | break; | |
9324 | case 26: | |
9325 | code = BFD_RELOC_26; | |
9326 | break; | |
9327 | case 32: | |
9328 | code = BFD_RELOC_32; | |
9329 | break; | |
9330 | case 64: | |
9331 | code = BFD_RELOC_64; | |
9332 | break; | |
9333 | default: | |
9334 | goto fail; | |
9335 | } | |
9336 | ||
9337 | howto = bfd_reloc_type_lookup (abfd, code); | |
9338 | } | |
9339 | ||
9340 | if (howto) | |
9341 | areloc->howto = howto; | |
9342 | else | |
9343 | goto fail; | |
9344 | } | |
9345 | ||
b34976b6 | 9346 | return TRUE; |
252b5132 RH |
9347 | |
9348 | fail: | |
0aa13fee AM |
9349 | /* xgettext:c-format */ |
9350 | _bfd_error_handler (_("%pB: %s unsupported"), | |
9351 | abfd, areloc->howto->name); | |
9aea1e31 | 9352 | bfd_set_error (bfd_error_sorry); |
b34976b6 | 9353 | return FALSE; |
252b5132 RH |
9354 | } |
9355 | ||
b34976b6 | 9356 | bfd_boolean |
217aa764 | 9357 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 9358 | { |
d9071b0c TG |
9359 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
9360 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 9361 | { |
c0355132 | 9362 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 9363 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 9364 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
9365 | } |
9366 | ||
9367 | return _bfd_generic_close_and_cleanup (abfd); | |
9368 | } | |
9369 | ||
9370 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
9371 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
9372 | range-checking to interfere. There is nothing else to do in processing | |
9373 | this reloc. */ | |
9374 | ||
9375 | bfd_reloc_status_type | |
217aa764 AM |
9376 | _bfd_elf_rel_vtable_reloc_fn |
9377 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 9378 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
9379 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
9380 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
9381 | { |
9382 | return bfd_reloc_ok; | |
9383 | } | |
252b5132 RH |
9384 | \f |
9385 | /* Elf core file support. Much of this only works on native | |
9386 | toolchains, since we rely on knowing the | |
9387 | machine-dependent procfs structure in order to pick | |
c044fabd | 9388 | out details about the corefile. */ |
252b5132 RH |
9389 | |
9390 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
9391 | /* Needed for new procfs interface on sparc-solaris. */ |
9392 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
9393 | # include <sys/procfs.h> |
9394 | #endif | |
9395 | ||
261b8d08 PA |
9396 | /* Return a PID that identifies a "thread" for threaded cores, or the |
9397 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
9398 | |
9399 | static int | |
217aa764 | 9400 | elfcore_make_pid (bfd *abfd) |
252b5132 | 9401 | { |
261b8d08 PA |
9402 | int pid; |
9403 | ||
228e534f | 9404 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 9405 | if (pid == 0) |
228e534f | 9406 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
9407 | |
9408 | return pid; | |
252b5132 RH |
9409 | } |
9410 | ||
252b5132 RH |
9411 | /* If there isn't a section called NAME, make one, using |
9412 | data from SECT. Note, this function will generate a | |
9413 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 9414 | overwrite it. */ |
252b5132 | 9415 | |
b34976b6 | 9416 | static bfd_boolean |
217aa764 | 9417 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 9418 | { |
c044fabd | 9419 | asection *sect2; |
252b5132 RH |
9420 | |
9421 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 9422 | return TRUE; |
252b5132 | 9423 | |
117ed4f8 | 9424 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 9425 | if (sect2 == NULL) |
b34976b6 | 9426 | return FALSE; |
252b5132 | 9427 | |
eea6121a | 9428 | sect2->size = sect->size; |
252b5132 | 9429 | sect2->filepos = sect->filepos; |
252b5132 | 9430 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 9431 | return TRUE; |
252b5132 RH |
9432 | } |
9433 | ||
bb0082d6 AM |
9434 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
9435 | actually creates up to two pseudosections: | |
9436 | - For the single-threaded case, a section named NAME, unless | |
9437 | such a section already exists. | |
9438 | - For the multi-threaded case, a section named "NAME/PID", where | |
9439 | PID is elfcore_make_pid (abfd). | |
24d3e51b | 9440 | Both pseudosections have identical contents. */ |
b34976b6 | 9441 | bfd_boolean |
217aa764 AM |
9442 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
9443 | char *name, | |
9444 | size_t size, | |
9445 | ufile_ptr filepos) | |
bb0082d6 AM |
9446 | { |
9447 | char buf[100]; | |
9448 | char *threaded_name; | |
d4c88bbb | 9449 | size_t len; |
bb0082d6 AM |
9450 | asection *sect; |
9451 | ||
9452 | /* Build the section name. */ | |
9453 | ||
9454 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 9455 | len = strlen (buf) + 1; |
a50b1753 | 9456 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 9457 | if (threaded_name == NULL) |
b34976b6 | 9458 | return FALSE; |
d4c88bbb | 9459 | memcpy (threaded_name, buf, len); |
bb0082d6 | 9460 | |
117ed4f8 AM |
9461 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
9462 | SEC_HAS_CONTENTS); | |
bb0082d6 | 9463 | if (sect == NULL) |
b34976b6 | 9464 | return FALSE; |
eea6121a | 9465 | sect->size = size; |
bb0082d6 | 9466 | sect->filepos = filepos; |
bb0082d6 AM |
9467 | sect->alignment_power = 2; |
9468 | ||
936e320b | 9469 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
9470 | } |
9471 | ||
58e07198 CZ |
9472 | static bfd_boolean |
9473 | elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note, | |
9474 | size_t offs) | |
9475 | { | |
9476 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9477 | SEC_HAS_CONTENTS); | |
9478 | ||
9479 | if (sect == NULL) | |
9480 | return FALSE; | |
9481 | ||
9482 | sect->size = note->descsz - offs; | |
9483 | sect->filepos = note->descpos + offs; | |
9484 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9485 | ||
9486 | return TRUE; | |
9487 | } | |
9488 | ||
252b5132 | 9489 | /* prstatus_t exists on: |
4a938328 | 9490 | solaris 2.5+ |
252b5132 RH |
9491 | linux 2.[01] + glibc |
9492 | unixware 4.2 | |
9493 | */ | |
9494 | ||
9495 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 9496 | |
b34976b6 | 9497 | static bfd_boolean |
217aa764 | 9498 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9499 | { |
eea6121a | 9500 | size_t size; |
7ee38065 | 9501 | int offset; |
252b5132 | 9502 | |
4a938328 MS |
9503 | if (note->descsz == sizeof (prstatus_t)) |
9504 | { | |
9505 | prstatus_t prstat; | |
252b5132 | 9506 | |
eea6121a | 9507 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9508 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 9509 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 9510 | |
fa49d224 NC |
9511 | /* Do not overwrite the core signal if it |
9512 | has already been set by another thread. */ | |
228e534f AM |
9513 | if (elf_tdata (abfd)->core->signal == 0) |
9514 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9515 | if (elf_tdata (abfd)->core->pid == 0) | |
9516 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 9517 | |
4a938328 MS |
9518 | /* pr_who exists on: |
9519 | solaris 2.5+ | |
9520 | unixware 4.2 | |
9521 | pr_who doesn't exist on: | |
9522 | linux 2.[01] | |
9523 | */ | |
252b5132 | 9524 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 9525 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9526 | #else |
228e534f | 9527 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 9528 | #endif |
4a938328 | 9529 | } |
7ee38065 | 9530 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
9531 | else if (note->descsz == sizeof (prstatus32_t)) |
9532 | { | |
9533 | /* 64-bit host, 32-bit corefile */ | |
9534 | prstatus32_t prstat; | |
9535 | ||
eea6121a | 9536 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9537 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
9538 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
9539 | ||
fa49d224 NC |
9540 | /* Do not overwrite the core signal if it |
9541 | has already been set by another thread. */ | |
228e534f AM |
9542 | if (elf_tdata (abfd)->core->signal == 0) |
9543 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9544 | if (elf_tdata (abfd)->core->pid == 0) | |
9545 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
9546 | |
9547 | /* pr_who exists on: | |
9548 | solaris 2.5+ | |
9549 | unixware 4.2 | |
9550 | pr_who doesn't exist on: | |
9551 | linux 2.[01] | |
9552 | */ | |
7ee38065 | 9553 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 9554 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9555 | #else |
228e534f | 9556 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
9557 | #endif |
9558 | } | |
7ee38065 | 9559 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
9560 | else |
9561 | { | |
9562 | /* Fail - we don't know how to handle any other | |
9563 | note size (ie. data object type). */ | |
b34976b6 | 9564 | return TRUE; |
4a938328 | 9565 | } |
252b5132 | 9566 | |
bb0082d6 | 9567 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 9568 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 9569 | size, note->descpos + offset); |
252b5132 RH |
9570 | } |
9571 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
9572 | ||
bb0082d6 | 9573 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 9574 | static bfd_boolean |
217aa764 AM |
9575 | elfcore_make_note_pseudosection (bfd *abfd, |
9576 | char *name, | |
9577 | Elf_Internal_Note *note) | |
252b5132 | 9578 | { |
936e320b AM |
9579 | return _bfd_elfcore_make_pseudosection (abfd, name, |
9580 | note->descsz, note->descpos); | |
252b5132 RH |
9581 | } |
9582 | ||
ff08c6bb JB |
9583 | /* There isn't a consistent prfpregset_t across platforms, |
9584 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
9585 | data structure apart. */ |
9586 | ||
b34976b6 | 9587 | static bfd_boolean |
217aa764 | 9588 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9589 | { |
9590 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9591 | } | |
9592 | ||
ff08c6bb | 9593 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 9594 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 9595 | literally. */ |
c044fabd | 9596 | |
b34976b6 | 9597 | static bfd_boolean |
217aa764 | 9598 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9599 | { |
9600 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9601 | } | |
9602 | ||
4339cae0 L |
9603 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
9604 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
9605 | contents literally. */ | |
9606 | ||
9607 | static bfd_boolean | |
9608 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
9609 | { | |
9610 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
9611 | } | |
9612 | ||
97753bd5 AM |
9613 | static bfd_boolean |
9614 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
9615 | { | |
9616 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
9617 | } | |
9618 | ||
89eeb0bc LM |
9619 | static bfd_boolean |
9620 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
9621 | { | |
9622 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
9623 | } | |
97753bd5 | 9624 | |
cb2366c1 EBM |
9625 | static bfd_boolean |
9626 | elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note) | |
9627 | { | |
9628 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note); | |
9629 | } | |
9630 | ||
9631 | static bfd_boolean | |
9632 | elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note) | |
9633 | { | |
9634 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note); | |
9635 | } | |
9636 | ||
9637 | static bfd_boolean | |
9638 | elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note) | |
9639 | { | |
9640 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note); | |
9641 | } | |
9642 | ||
9643 | static bfd_boolean | |
9644 | elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note) | |
9645 | { | |
9646 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note); | |
9647 | } | |
9648 | ||
9649 | static bfd_boolean | |
9650 | elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note) | |
9651 | { | |
9652 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note); | |
9653 | } | |
9654 | ||
9655 | static bfd_boolean | |
9656 | elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note) | |
9657 | { | |
9658 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note); | |
9659 | } | |
9660 | ||
9661 | static bfd_boolean | |
9662 | elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note) | |
9663 | { | |
9664 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note); | |
9665 | } | |
9666 | ||
9667 | static bfd_boolean | |
9668 | elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note) | |
9669 | { | |
9670 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note); | |
9671 | } | |
9672 | ||
9673 | static bfd_boolean | |
9674 | elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note) | |
9675 | { | |
9676 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note); | |
9677 | } | |
9678 | ||
9679 | static bfd_boolean | |
9680 | elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note) | |
9681 | { | |
9682 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note); | |
9683 | } | |
9684 | ||
9685 | static bfd_boolean | |
9686 | elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note) | |
9687 | { | |
9688 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note); | |
9689 | } | |
9690 | ||
9691 | static bfd_boolean | |
9692 | elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note) | |
9693 | { | |
9694 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note); | |
9695 | } | |
9696 | ||
9697 | static bfd_boolean | |
9698 | elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note) | |
9699 | { | |
9700 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note); | |
9701 | } | |
9702 | ||
0675e188 UW |
9703 | static bfd_boolean |
9704 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
9705 | { | |
9706 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
9707 | } | |
9708 | ||
d7eeb400 MS |
9709 | static bfd_boolean |
9710 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
9711 | { | |
9712 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
9713 | } | |
9714 | ||
9715 | static bfd_boolean | |
9716 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
9717 | { | |
9718 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
9719 | } | |
9720 | ||
9721 | static bfd_boolean | |
9722 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
9723 | { | |
9724 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
9725 | } | |
9726 | ||
9727 | static bfd_boolean | |
9728 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
9729 | { | |
9730 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
9731 | } | |
9732 | ||
9733 | static bfd_boolean | |
9734 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
9735 | { | |
9736 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
9737 | } | |
9738 | ||
355b81d9 UW |
9739 | static bfd_boolean |
9740 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
9741 | { | |
9742 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
9743 | } | |
9744 | ||
9745 | static bfd_boolean | |
9746 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
9747 | { | |
9748 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
9749 | } | |
9750 | ||
abb3f6cc NC |
9751 | static bfd_boolean |
9752 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
9753 | { | |
9754 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
9755 | } | |
9756 | ||
4ef9f41a AA |
9757 | static bfd_boolean |
9758 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
9759 | { | |
9760 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
9761 | } | |
9762 | ||
9763 | static bfd_boolean | |
9764 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
9765 | { | |
9766 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
9767 | } | |
9768 | ||
88ab90e8 AA |
9769 | static bfd_boolean |
9770 | elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note) | |
9771 | { | |
9772 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note); | |
9773 | } | |
9774 | ||
9775 | static bfd_boolean | |
9776 | elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note) | |
9777 | { | |
9778 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note); | |
9779 | } | |
9780 | ||
faa9a424 UW |
9781 | static bfd_boolean |
9782 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
9783 | { | |
9784 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
9785 | } | |
9786 | ||
652451f8 YZ |
9787 | static bfd_boolean |
9788 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
9789 | { | |
9790 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
9791 | } | |
9792 | ||
9793 | static bfd_boolean | |
9794 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
9795 | { | |
9796 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
9797 | } | |
9798 | ||
9799 | static bfd_boolean | |
9800 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
9801 | { | |
9802 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
9803 | } | |
9804 | ||
ad1cc4e4 AH |
9805 | static bfd_boolean |
9806 | elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note) | |
9807 | { | |
9808 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note); | |
9809 | } | |
9810 | ||
e6c3b5bf AH |
9811 | static bfd_boolean |
9812 | elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note) | |
9813 | { | |
9814 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note); | |
9815 | } | |
9816 | ||
252b5132 | 9817 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 9818 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 9819 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9820 | typedef prpsinfo32_t elfcore_psinfo32_t; |
9821 | #endif | |
252b5132 RH |
9822 | #endif |
9823 | ||
9824 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 9825 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 9826 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9827 | typedef psinfo32_t elfcore_psinfo32_t; |
9828 | #endif | |
252b5132 RH |
9829 | #endif |
9830 | ||
252b5132 RH |
9831 | /* return a malloc'ed copy of a string at START which is at |
9832 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 9833 | the copy will always have a terminating '\0'. */ |
252b5132 | 9834 | |
936e320b | 9835 | char * |
217aa764 | 9836 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 9837 | { |
dc810e39 | 9838 | char *dups; |
a50b1753 | 9839 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 9840 | size_t len; |
252b5132 RH |
9841 | |
9842 | if (end == NULL) | |
9843 | len = max; | |
9844 | else | |
9845 | len = end - start; | |
9846 | ||
a50b1753 | 9847 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 9848 | if (dups == NULL) |
252b5132 RH |
9849 | return NULL; |
9850 | ||
dc810e39 AM |
9851 | memcpy (dups, start, len); |
9852 | dups[len] = '\0'; | |
252b5132 | 9853 | |
dc810e39 | 9854 | return dups; |
252b5132 RH |
9855 | } |
9856 | ||
bb0082d6 | 9857 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 9858 | static bfd_boolean |
217aa764 | 9859 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9860 | { |
4a938328 MS |
9861 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
9862 | { | |
9863 | elfcore_psinfo_t psinfo; | |
252b5132 | 9864 | |
7ee38065 | 9865 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9866 | |
335e41d4 | 9867 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 9868 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9869 | #endif |
228e534f | 9870 | elf_tdata (abfd)->core->program |
936e320b AM |
9871 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9872 | sizeof (psinfo.pr_fname)); | |
252b5132 | 9873 | |
228e534f | 9874 | elf_tdata (abfd)->core->command |
936e320b AM |
9875 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9876 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 9877 | } |
7ee38065 | 9878 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
9879 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
9880 | { | |
9881 | /* 64-bit host, 32-bit corefile */ | |
9882 | elfcore_psinfo32_t psinfo; | |
9883 | ||
7ee38065 | 9884 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9885 | |
335e41d4 | 9886 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 9887 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9888 | #endif |
228e534f | 9889 | elf_tdata (abfd)->core->program |
936e320b AM |
9890 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9891 | sizeof (psinfo.pr_fname)); | |
4a938328 | 9892 | |
228e534f | 9893 | elf_tdata (abfd)->core->command |
936e320b AM |
9894 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9895 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
9896 | } |
9897 | #endif | |
9898 | ||
9899 | else | |
9900 | { | |
9901 | /* Fail - we don't know how to handle any other | |
9902 | note size (ie. data object type). */ | |
b34976b6 | 9903 | return TRUE; |
4a938328 | 9904 | } |
252b5132 RH |
9905 | |
9906 | /* Note that for some reason, a spurious space is tacked | |
9907 | onto the end of the args in some (at least one anyway) | |
c044fabd | 9908 | implementations, so strip it off if it exists. */ |
252b5132 RH |
9909 | |
9910 | { | |
228e534f | 9911 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
9912 | int n = strlen (command); |
9913 | ||
9914 | if (0 < n && command[n - 1] == ' ') | |
9915 | command[n - 1] = '\0'; | |
9916 | } | |
9917 | ||
b34976b6 | 9918 | return TRUE; |
252b5132 RH |
9919 | } |
9920 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
9921 | ||
252b5132 | 9922 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 9923 | static bfd_boolean |
217aa764 | 9924 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9925 | { |
f572a39d AM |
9926 | if (note->descsz == sizeof (pstatus_t) |
9927 | #if defined (HAVE_PXSTATUS_T) | |
9928 | || note->descsz == sizeof (pxstatus_t) | |
9929 | #endif | |
9930 | ) | |
4a938328 MS |
9931 | { |
9932 | pstatus_t pstat; | |
252b5132 | 9933 | |
4a938328 | 9934 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9935 | |
228e534f | 9936 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 9937 | } |
7ee38065 | 9938 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
9939 | else if (note->descsz == sizeof (pstatus32_t)) |
9940 | { | |
9941 | /* 64-bit host, 32-bit corefile */ | |
9942 | pstatus32_t pstat; | |
252b5132 | 9943 | |
4a938328 | 9944 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9945 | |
228e534f | 9946 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
9947 | } |
9948 | #endif | |
252b5132 RH |
9949 | /* Could grab some more details from the "representative" |
9950 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 9951 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 9952 | |
b34976b6 | 9953 | return TRUE; |
252b5132 RH |
9954 | } |
9955 | #endif /* defined (HAVE_PSTATUS_T) */ | |
9956 | ||
252b5132 | 9957 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 9958 | static bfd_boolean |
217aa764 | 9959 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
9960 | { |
9961 | lwpstatus_t lwpstat; | |
9962 | char buf[100]; | |
c044fabd | 9963 | char *name; |
d4c88bbb | 9964 | size_t len; |
c044fabd | 9965 | asection *sect; |
252b5132 | 9966 | |
f572a39d AM |
9967 | if (note->descsz != sizeof (lwpstat) |
9968 | #if defined (HAVE_LWPXSTATUS_T) | |
9969 | && note->descsz != sizeof (lwpxstatus_t) | |
9970 | #endif | |
9971 | ) | |
b34976b6 | 9972 | return TRUE; |
252b5132 RH |
9973 | |
9974 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
9975 | ||
228e534f | 9976 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
9977 | /* Do not overwrite the core signal if it has already been set by |
9978 | another thread. */ | |
228e534f AM |
9979 | if (elf_tdata (abfd)->core->signal == 0) |
9980 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 9981 | |
c044fabd | 9982 | /* Make a ".reg/999" section. */ |
252b5132 RH |
9983 | |
9984 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9985 | len = strlen (buf) + 1; |
217aa764 | 9986 | name = bfd_alloc (abfd, len); |
252b5132 | 9987 | if (name == NULL) |
b34976b6 | 9988 | return FALSE; |
d4c88bbb | 9989 | memcpy (name, buf, len); |
252b5132 | 9990 | |
117ed4f8 | 9991 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9992 | if (sect == NULL) |
b34976b6 | 9993 | return FALSE; |
252b5132 RH |
9994 | |
9995 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9996 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
9997 | sect->filepos = note->descpos |
9998 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
9999 | #endif | |
10000 | ||
10001 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 10002 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
10003 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
10004 | #endif | |
10005 | ||
252b5132 RH |
10006 | sect->alignment_power = 2; |
10007 | ||
10008 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 10009 | return FALSE; |
252b5132 RH |
10010 | |
10011 | /* Make a ".reg2/999" section */ | |
10012 | ||
10013 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 10014 | len = strlen (buf) + 1; |
217aa764 | 10015 | name = bfd_alloc (abfd, len); |
252b5132 | 10016 | if (name == NULL) |
b34976b6 | 10017 | return FALSE; |
d4c88bbb | 10018 | memcpy (name, buf, len); |
252b5132 | 10019 | |
117ed4f8 | 10020 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 10021 | if (sect == NULL) |
b34976b6 | 10022 | return FALSE; |
252b5132 RH |
10023 | |
10024 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 10025 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
10026 | sect->filepos = note->descpos |
10027 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
10028 | #endif | |
10029 | ||
10030 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 10031 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
10032 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
10033 | #endif | |
10034 | ||
252b5132 RH |
10035 | sect->alignment_power = 2; |
10036 | ||
936e320b | 10037 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
10038 | } |
10039 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
10040 | ||
b34976b6 | 10041 | static bfd_boolean |
217aa764 | 10042 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
10043 | { |
10044 | char buf[30]; | |
c044fabd | 10045 | char *name; |
d4c88bbb | 10046 | size_t len; |
c044fabd | 10047 | asection *sect; |
4a6636fb PA |
10048 | int type; |
10049 | int is_active_thread; | |
10050 | bfd_vma base_addr; | |
16e9c715 | 10051 | |
4a6636fb | 10052 | if (note->descsz < 728) |
b34976b6 | 10053 | return TRUE; |
16e9c715 | 10054 | |
4a6636fb PA |
10055 | if (! CONST_STRNEQ (note->namedata, "win32")) |
10056 | return TRUE; | |
10057 | ||
10058 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 10059 | |
4a6636fb | 10060 | switch (type) |
16e9c715 | 10061 | { |
4a6636fb | 10062 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 10063 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 10064 | /* process_info.pid */ |
228e534f | 10065 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 10066 | /* process_info.signal */ |
228e534f | 10067 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 10068 | break; |
16e9c715 | 10069 | |
4a6636fb | 10070 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 10071 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
10072 | /* thread_info.tid */ |
10073 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 10074 | |
d4c88bbb | 10075 | len = strlen (buf) + 1; |
a50b1753 | 10076 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 10077 | if (name == NULL) |
b34976b6 | 10078 | return FALSE; |
c044fabd | 10079 | |
d4c88bbb | 10080 | memcpy (name, buf, len); |
16e9c715 | 10081 | |
117ed4f8 | 10082 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 10083 | if (sect == NULL) |
b34976b6 | 10084 | return FALSE; |
c044fabd | 10085 | |
4a6636fb PA |
10086 | /* sizeof (thread_info.thread_context) */ |
10087 | sect->size = 716; | |
10088 | /* offsetof (thread_info.thread_context) */ | |
10089 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
10090 | sect->alignment_power = 2; |
10091 | ||
4a6636fb PA |
10092 | /* thread_info.is_active_thread */ |
10093 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
10094 | ||
10095 | if (is_active_thread) | |
16e9c715 | 10096 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 10097 | return FALSE; |
16e9c715 NC |
10098 | break; |
10099 | ||
4a6636fb | 10100 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 10101 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
10102 | /* module_info.base_address */ |
10103 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 10104 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 10105 | |
d4c88bbb | 10106 | len = strlen (buf) + 1; |
a50b1753 | 10107 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 10108 | if (name == NULL) |
b34976b6 | 10109 | return FALSE; |
c044fabd | 10110 | |
d4c88bbb | 10111 | memcpy (name, buf, len); |
252b5132 | 10112 | |
117ed4f8 | 10113 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 10114 | |
16e9c715 | 10115 | if (sect == NULL) |
b34976b6 | 10116 | return FALSE; |
c044fabd | 10117 | |
eea6121a | 10118 | sect->size = note->descsz; |
16e9c715 | 10119 | sect->filepos = note->descpos; |
16e9c715 NC |
10120 | sect->alignment_power = 2; |
10121 | break; | |
10122 | ||
10123 | default: | |
b34976b6 | 10124 | return TRUE; |
16e9c715 NC |
10125 | } |
10126 | ||
b34976b6 | 10127 | return TRUE; |
16e9c715 | 10128 | } |
252b5132 | 10129 | |
b34976b6 | 10130 | static bfd_boolean |
217aa764 | 10131 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 10132 | { |
9c5bfbb7 | 10133 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 10134 | |
252b5132 RH |
10135 | switch (note->type) |
10136 | { | |
10137 | default: | |
b34976b6 | 10138 | return TRUE; |
252b5132 | 10139 | |
252b5132 | 10140 | case NT_PRSTATUS: |
bb0082d6 AM |
10141 | if (bed->elf_backend_grok_prstatus) |
10142 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 10143 | return TRUE; |
bb0082d6 | 10144 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 10145 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 10146 | #else |
b34976b6 | 10147 | return TRUE; |
252b5132 RH |
10148 | #endif |
10149 | ||
10150 | #if defined (HAVE_PSTATUS_T) | |
10151 | case NT_PSTATUS: | |
10152 | return elfcore_grok_pstatus (abfd, note); | |
10153 | #endif | |
10154 | ||
10155 | #if defined (HAVE_LWPSTATUS_T) | |
10156 | case NT_LWPSTATUS: | |
10157 | return elfcore_grok_lwpstatus (abfd, note); | |
10158 | #endif | |
10159 | ||
10160 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
10161 | return elfcore_grok_prfpreg (abfd, note); | |
10162 | ||
c044fabd | 10163 | case NT_WIN32PSTATUS: |
16e9c715 | 10164 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 10165 | |
c044fabd | 10166 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
10167 | if (note->namesz == 6 |
10168 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
10169 | return elfcore_grok_prxfpreg (abfd, note); |
10170 | else | |
b34976b6 | 10171 | return TRUE; |
ff08c6bb | 10172 | |
4339cae0 L |
10173 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
10174 | if (note->namesz == 6 | |
10175 | && strcmp (note->namedata, "LINUX") == 0) | |
10176 | return elfcore_grok_xstatereg (abfd, note); | |
10177 | else | |
10178 | return TRUE; | |
10179 | ||
97753bd5 AM |
10180 | case NT_PPC_VMX: |
10181 | if (note->namesz == 6 | |
10182 | && strcmp (note->namedata, "LINUX") == 0) | |
10183 | return elfcore_grok_ppc_vmx (abfd, note); | |
10184 | else | |
10185 | return TRUE; | |
10186 | ||
89eeb0bc LM |
10187 | case NT_PPC_VSX: |
10188 | if (note->namesz == 6 | |
07d6d2b8 AM |
10189 | && strcmp (note->namedata, "LINUX") == 0) |
10190 | return elfcore_grok_ppc_vsx (abfd, note); | |
89eeb0bc | 10191 | else |
07d6d2b8 | 10192 | return TRUE; |
89eeb0bc | 10193 | |
cb2366c1 EBM |
10194 | case NT_PPC_TAR: |
10195 | if (note->namesz == 6 | |
4b24dd1a AM |
10196 | && strcmp (note->namedata, "LINUX") == 0) |
10197 | return elfcore_grok_ppc_tar (abfd, note); | |
cb2366c1 | 10198 | else |
4b24dd1a | 10199 | return TRUE; |
cb2366c1 EBM |
10200 | |
10201 | case NT_PPC_PPR: | |
10202 | if (note->namesz == 6 | |
4b24dd1a AM |
10203 | && strcmp (note->namedata, "LINUX") == 0) |
10204 | return elfcore_grok_ppc_ppr (abfd, note); | |
cb2366c1 | 10205 | else |
4b24dd1a | 10206 | return TRUE; |
cb2366c1 EBM |
10207 | |
10208 | case NT_PPC_DSCR: | |
10209 | if (note->namesz == 6 | |
4b24dd1a AM |
10210 | && strcmp (note->namedata, "LINUX") == 0) |
10211 | return elfcore_grok_ppc_dscr (abfd, note); | |
cb2366c1 | 10212 | else |
4b24dd1a | 10213 | return TRUE; |
cb2366c1 EBM |
10214 | |
10215 | case NT_PPC_EBB: | |
10216 | if (note->namesz == 6 | |
4b24dd1a AM |
10217 | && strcmp (note->namedata, "LINUX") == 0) |
10218 | return elfcore_grok_ppc_ebb (abfd, note); | |
cb2366c1 | 10219 | else |
4b24dd1a | 10220 | return TRUE; |
cb2366c1 EBM |
10221 | |
10222 | case NT_PPC_PMU: | |
10223 | if (note->namesz == 6 | |
4b24dd1a AM |
10224 | && strcmp (note->namedata, "LINUX") == 0) |
10225 | return elfcore_grok_ppc_pmu (abfd, note); | |
cb2366c1 | 10226 | else |
4b24dd1a | 10227 | return TRUE; |
cb2366c1 EBM |
10228 | |
10229 | case NT_PPC_TM_CGPR: | |
10230 | if (note->namesz == 6 | |
4b24dd1a AM |
10231 | && strcmp (note->namedata, "LINUX") == 0) |
10232 | return elfcore_grok_ppc_tm_cgpr (abfd, note); | |
cb2366c1 | 10233 | else |
4b24dd1a | 10234 | return TRUE; |
cb2366c1 EBM |
10235 | |
10236 | case NT_PPC_TM_CFPR: | |
10237 | if (note->namesz == 6 | |
4b24dd1a AM |
10238 | && strcmp (note->namedata, "LINUX") == 0) |
10239 | return elfcore_grok_ppc_tm_cfpr (abfd, note); | |
cb2366c1 | 10240 | else |
4b24dd1a | 10241 | return TRUE; |
cb2366c1 EBM |
10242 | |
10243 | case NT_PPC_TM_CVMX: | |
10244 | if (note->namesz == 6 | |
4b24dd1a AM |
10245 | && strcmp (note->namedata, "LINUX") == 0) |
10246 | return elfcore_grok_ppc_tm_cvmx (abfd, note); | |
cb2366c1 | 10247 | else |
4b24dd1a | 10248 | return TRUE; |
cb2366c1 EBM |
10249 | |
10250 | case NT_PPC_TM_CVSX: | |
10251 | if (note->namesz == 6 | |
4b24dd1a AM |
10252 | && strcmp (note->namedata, "LINUX") == 0) |
10253 | return elfcore_grok_ppc_tm_cvsx (abfd, note); | |
cb2366c1 | 10254 | else |
4b24dd1a | 10255 | return TRUE; |
cb2366c1 EBM |
10256 | |
10257 | case NT_PPC_TM_SPR: | |
10258 | if (note->namesz == 6 | |
4b24dd1a AM |
10259 | && strcmp (note->namedata, "LINUX") == 0) |
10260 | return elfcore_grok_ppc_tm_spr (abfd, note); | |
cb2366c1 | 10261 | else |
4b24dd1a | 10262 | return TRUE; |
cb2366c1 EBM |
10263 | |
10264 | case NT_PPC_TM_CTAR: | |
10265 | if (note->namesz == 6 | |
4b24dd1a AM |
10266 | && strcmp (note->namedata, "LINUX") == 0) |
10267 | return elfcore_grok_ppc_tm_ctar (abfd, note); | |
cb2366c1 | 10268 | else |
4b24dd1a | 10269 | return TRUE; |
cb2366c1 EBM |
10270 | |
10271 | case NT_PPC_TM_CPPR: | |
10272 | if (note->namesz == 6 | |
4b24dd1a AM |
10273 | && strcmp (note->namedata, "LINUX") == 0) |
10274 | return elfcore_grok_ppc_tm_cppr (abfd, note); | |
cb2366c1 | 10275 | else |
4b24dd1a | 10276 | return TRUE; |
cb2366c1 EBM |
10277 | |
10278 | case NT_PPC_TM_CDSCR: | |
10279 | if (note->namesz == 6 | |
4b24dd1a AM |
10280 | && strcmp (note->namedata, "LINUX") == 0) |
10281 | return elfcore_grok_ppc_tm_cdscr (abfd, note); | |
cb2366c1 | 10282 | else |
4b24dd1a | 10283 | return TRUE; |
cb2366c1 | 10284 | |
0675e188 UW |
10285 | case NT_S390_HIGH_GPRS: |
10286 | if (note->namesz == 6 | |
07d6d2b8 AM |
10287 | && strcmp (note->namedata, "LINUX") == 0) |
10288 | return elfcore_grok_s390_high_gprs (abfd, note); | |
0675e188 | 10289 | else |
07d6d2b8 | 10290 | return TRUE; |
0675e188 | 10291 | |
d7eeb400 MS |
10292 | case NT_S390_TIMER: |
10293 | if (note->namesz == 6 | |
07d6d2b8 AM |
10294 | && strcmp (note->namedata, "LINUX") == 0) |
10295 | return elfcore_grok_s390_timer (abfd, note); | |
d7eeb400 | 10296 | else |
07d6d2b8 | 10297 | return TRUE; |
d7eeb400 MS |
10298 | |
10299 | case NT_S390_TODCMP: | |
10300 | if (note->namesz == 6 | |
07d6d2b8 AM |
10301 | && strcmp (note->namedata, "LINUX") == 0) |
10302 | return elfcore_grok_s390_todcmp (abfd, note); | |
d7eeb400 | 10303 | else |
07d6d2b8 | 10304 | return TRUE; |
d7eeb400 MS |
10305 | |
10306 | case NT_S390_TODPREG: | |
10307 | if (note->namesz == 6 | |
07d6d2b8 AM |
10308 | && strcmp (note->namedata, "LINUX") == 0) |
10309 | return elfcore_grok_s390_todpreg (abfd, note); | |
d7eeb400 | 10310 | else |
07d6d2b8 | 10311 | return TRUE; |
d7eeb400 MS |
10312 | |
10313 | case NT_S390_CTRS: | |
10314 | if (note->namesz == 6 | |
07d6d2b8 AM |
10315 | && strcmp (note->namedata, "LINUX") == 0) |
10316 | return elfcore_grok_s390_ctrs (abfd, note); | |
d7eeb400 | 10317 | else |
07d6d2b8 | 10318 | return TRUE; |
d7eeb400 MS |
10319 | |
10320 | case NT_S390_PREFIX: | |
10321 | if (note->namesz == 6 | |
07d6d2b8 AM |
10322 | && strcmp (note->namedata, "LINUX") == 0) |
10323 | return elfcore_grok_s390_prefix (abfd, note); | |
d7eeb400 | 10324 | else |
07d6d2b8 | 10325 | return TRUE; |
d7eeb400 | 10326 | |
355b81d9 UW |
10327 | case NT_S390_LAST_BREAK: |
10328 | if (note->namesz == 6 | |
07d6d2b8 AM |
10329 | && strcmp (note->namedata, "LINUX") == 0) |
10330 | return elfcore_grok_s390_last_break (abfd, note); | |
355b81d9 | 10331 | else |
07d6d2b8 | 10332 | return TRUE; |
355b81d9 UW |
10333 | |
10334 | case NT_S390_SYSTEM_CALL: | |
10335 | if (note->namesz == 6 | |
07d6d2b8 AM |
10336 | && strcmp (note->namedata, "LINUX") == 0) |
10337 | return elfcore_grok_s390_system_call (abfd, note); | |
355b81d9 | 10338 | else |
07d6d2b8 | 10339 | return TRUE; |
355b81d9 | 10340 | |
abb3f6cc NC |
10341 | case NT_S390_TDB: |
10342 | if (note->namesz == 6 | |
07d6d2b8 AM |
10343 | && strcmp (note->namedata, "LINUX") == 0) |
10344 | return elfcore_grok_s390_tdb (abfd, note); | |
abb3f6cc | 10345 | else |
07d6d2b8 | 10346 | return TRUE; |
abb3f6cc | 10347 | |
4ef9f41a AA |
10348 | case NT_S390_VXRS_LOW: |
10349 | if (note->namesz == 6 | |
10350 | && strcmp (note->namedata, "LINUX") == 0) | |
10351 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
10352 | else | |
10353 | return TRUE; | |
10354 | ||
10355 | case NT_S390_VXRS_HIGH: | |
10356 | if (note->namesz == 6 | |
10357 | && strcmp (note->namedata, "LINUX") == 0) | |
10358 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
10359 | else | |
10360 | return TRUE; | |
10361 | ||
88ab90e8 AA |
10362 | case NT_S390_GS_CB: |
10363 | if (note->namesz == 6 | |
10364 | && strcmp (note->namedata, "LINUX") == 0) | |
8fe09d74 | 10365 | return elfcore_grok_s390_gs_cb (abfd, note); |
88ab90e8 AA |
10366 | else |
10367 | return TRUE; | |
10368 | ||
10369 | case NT_S390_GS_BC: | |
10370 | if (note->namesz == 6 | |
10371 | && strcmp (note->namedata, "LINUX") == 0) | |
8fe09d74 | 10372 | return elfcore_grok_s390_gs_bc (abfd, note); |
88ab90e8 AA |
10373 | else |
10374 | return TRUE; | |
10375 | ||
faa9a424 UW |
10376 | case NT_ARM_VFP: |
10377 | if (note->namesz == 6 | |
10378 | && strcmp (note->namedata, "LINUX") == 0) | |
10379 | return elfcore_grok_arm_vfp (abfd, note); | |
10380 | else | |
10381 | return TRUE; | |
10382 | ||
652451f8 YZ |
10383 | case NT_ARM_TLS: |
10384 | if (note->namesz == 6 | |
10385 | && strcmp (note->namedata, "LINUX") == 0) | |
10386 | return elfcore_grok_aarch_tls (abfd, note); | |
10387 | else | |
10388 | return TRUE; | |
10389 | ||
10390 | case NT_ARM_HW_BREAK: | |
10391 | if (note->namesz == 6 | |
10392 | && strcmp (note->namedata, "LINUX") == 0) | |
10393 | return elfcore_grok_aarch_hw_break (abfd, note); | |
10394 | else | |
10395 | return TRUE; | |
10396 | ||
10397 | case NT_ARM_HW_WATCH: | |
10398 | if (note->namesz == 6 | |
10399 | && strcmp (note->namedata, "LINUX") == 0) | |
10400 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
10401 | else | |
10402 | return TRUE; | |
10403 | ||
ad1cc4e4 AH |
10404 | case NT_ARM_SVE: |
10405 | if (note->namesz == 6 | |
10406 | && strcmp (note->namedata, "LINUX") == 0) | |
10407 | return elfcore_grok_aarch_sve (abfd, note); | |
10408 | else | |
10409 | return TRUE; | |
10410 | ||
e6c3b5bf AH |
10411 | case NT_ARM_PAC_MASK: |
10412 | if (note->namesz == 6 | |
10413 | && strcmp (note->namedata, "LINUX") == 0) | |
10414 | return elfcore_grok_aarch_pauth (abfd, note); | |
10415 | else | |
10416 | return TRUE; | |
10417 | ||
252b5132 RH |
10418 | case NT_PRPSINFO: |
10419 | case NT_PSINFO: | |
bb0082d6 AM |
10420 | if (bed->elf_backend_grok_psinfo) |
10421 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 10422 | return TRUE; |
bb0082d6 | 10423 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 10424 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 10425 | #else |
b34976b6 | 10426 | return TRUE; |
252b5132 | 10427 | #endif |
3333a7c3 RM |
10428 | |
10429 | case NT_AUXV: | |
58e07198 | 10430 | return elfcore_make_auxv_note_section (abfd, note, 0); |
9015683b | 10431 | |
451b7c33 TT |
10432 | case NT_FILE: |
10433 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
10434 | note); | |
10435 | ||
9015683b TT |
10436 | case NT_SIGINFO: |
10437 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
10438 | note); | |
5b2c414d | 10439 | |
252b5132 RH |
10440 | } |
10441 | } | |
10442 | ||
718175fa JK |
10443 | static bfd_boolean |
10444 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
10445 | { | |
c74f7d1c | 10446 | struct bfd_build_id* build_id; |
30e8ee25 AM |
10447 | |
10448 | if (note->descsz == 0) | |
10449 | return FALSE; | |
10450 | ||
c74f7d1c JT |
10451 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
10452 | if (build_id == NULL) | |
718175fa JK |
10453 | return FALSE; |
10454 | ||
c74f7d1c JT |
10455 | build_id->size = note->descsz; |
10456 | memcpy (build_id->data, note->descdata, note->descsz); | |
10457 | abfd->build_id = build_id; | |
718175fa JK |
10458 | |
10459 | return TRUE; | |
10460 | } | |
10461 | ||
10462 | static bfd_boolean | |
10463 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
10464 | { | |
10465 | switch (note->type) | |
10466 | { | |
10467 | default: | |
10468 | return TRUE; | |
10469 | ||
46bed679 L |
10470 | case NT_GNU_PROPERTY_TYPE_0: |
10471 | return _bfd_elf_parse_gnu_properties (abfd, note); | |
10472 | ||
718175fa JK |
10473 | case NT_GNU_BUILD_ID: |
10474 | return elfobj_grok_gnu_build_id (abfd, note); | |
10475 | } | |
10476 | } | |
10477 | ||
e21e5835 NC |
10478 | static bfd_boolean |
10479 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
10480 | { | |
10481 | struct sdt_note *cur = | |
7a6e0d89 AM |
10482 | (struct sdt_note *) bfd_alloc (abfd, |
10483 | sizeof (struct sdt_note) + note->descsz); | |
e21e5835 NC |
10484 | |
10485 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
10486 | cur->size = (bfd_size_type) note->descsz; | |
10487 | memcpy (cur->data, note->descdata, note->descsz); | |
10488 | ||
10489 | elf_tdata (abfd)->sdt_note_head = cur; | |
10490 | ||
10491 | return TRUE; | |
10492 | } | |
10493 | ||
10494 | static bfd_boolean | |
10495 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
10496 | { | |
10497 | switch (note->type) | |
10498 | { | |
10499 | case NT_STAPSDT: | |
10500 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
10501 | ||
10502 | default: | |
10503 | return TRUE; | |
10504 | } | |
10505 | } | |
10506 | ||
aa1ed4a9 JB |
10507 | static bfd_boolean |
10508 | elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
10509 | { | |
10510 | size_t offset; | |
10511 | ||
b5430a3c | 10512 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 10513 | { |
b5430a3c | 10514 | case ELFCLASS32: |
0064d223 JB |
10515 | if (note->descsz < 108) |
10516 | return FALSE; | |
aa1ed4a9 JB |
10517 | break; |
10518 | ||
b5430a3c | 10519 | case ELFCLASS64: |
0064d223 JB |
10520 | if (note->descsz < 120) |
10521 | return FALSE; | |
aa1ed4a9 JB |
10522 | break; |
10523 | ||
10524 | default: | |
10525 | return FALSE; | |
10526 | } | |
10527 | ||
0064d223 JB |
10528 | /* Check for version 1 in pr_version. */ |
10529 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
10530 | return FALSE; | |
80a04378 | 10531 | |
0064d223 JB |
10532 | offset = 4; |
10533 | ||
10534 | /* Skip over pr_psinfosz. */ | |
b5430a3c | 10535 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
0064d223 JB |
10536 | offset += 4; |
10537 | else | |
10538 | { | |
10539 | offset += 4; /* Padding before pr_psinfosz. */ | |
10540 | offset += 8; | |
10541 | } | |
10542 | ||
aa1ed4a9 JB |
10543 | /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ |
10544 | elf_tdata (abfd)->core->program | |
10545 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); | |
10546 | offset += 17; | |
10547 | ||
10548 | /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ | |
10549 | elf_tdata (abfd)->core->command | |
10550 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); | |
0064d223 JB |
10551 | offset += 81; |
10552 | ||
10553 | /* Padding before pr_pid. */ | |
10554 | offset += 2; | |
10555 | ||
10556 | /* The pr_pid field was added in version "1a". */ | |
10557 | if (note->descsz < offset + 4) | |
10558 | return TRUE; | |
10559 | ||
10560 | elf_tdata (abfd)->core->pid | |
10561 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
aa1ed4a9 JB |
10562 | |
10563 | return TRUE; | |
10564 | } | |
10565 | ||
10566 | static bfd_boolean | |
10567 | elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) | |
10568 | { | |
10569 | size_t offset; | |
10570 | size_t size; | |
24d3e51b | 10571 | size_t min_size; |
aa1ed4a9 | 10572 | |
24d3e51b NC |
10573 | /* Compute offset of pr_getregsz, skipping over pr_statussz. |
10574 | Also compute minimum size of this note. */ | |
b5430a3c | 10575 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 10576 | { |
b5430a3c | 10577 | case ELFCLASS32: |
24d3e51b NC |
10578 | offset = 4 + 4; |
10579 | min_size = offset + (4 * 2) + 4 + 4 + 4; | |
aa1ed4a9 JB |
10580 | break; |
10581 | ||
b5430a3c | 10582 | case ELFCLASS64: |
24d3e51b NC |
10583 | offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */ |
10584 | min_size = offset + (8 * 2) + 4 + 4 + 4 + 4; | |
aa1ed4a9 JB |
10585 | break; |
10586 | ||
10587 | default: | |
10588 | return FALSE; | |
10589 | } | |
10590 | ||
24d3e51b NC |
10591 | if (note->descsz < min_size) |
10592 | return FALSE; | |
10593 | ||
10594 | /* Check for version 1 in pr_version. */ | |
10595 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
10596 | return FALSE; | |
aa1ed4a9 | 10597 | |
24d3e51b NC |
10598 | /* Extract size of pr_reg from pr_gregsetsz. */ |
10599 | /* Skip over pr_gregsetsz and pr_fpregsetsz. */ | |
b5430a3c | 10600 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
24d3e51b NC |
10601 | { |
10602 | size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10603 | offset += 4 * 2; | |
10604 | } | |
b5430a3c | 10605 | else |
24d3e51b NC |
10606 | { |
10607 | size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); | |
10608 | offset += 8 * 2; | |
10609 | } | |
aa1ed4a9 | 10610 | |
24d3e51b | 10611 | /* Skip over pr_osreldate. */ |
aa1ed4a9 JB |
10612 | offset += 4; |
10613 | ||
24d3e51b | 10614 | /* Read signal from pr_cursig. */ |
aa1ed4a9 JB |
10615 | if (elf_tdata (abfd)->core->signal == 0) |
10616 | elf_tdata (abfd)->core->signal | |
10617 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10618 | offset += 4; | |
10619 | ||
24d3e51b | 10620 | /* Read TID from pr_pid. */ |
aa1ed4a9 JB |
10621 | elf_tdata (abfd)->core->lwpid |
10622 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10623 | offset += 4; | |
10624 | ||
24d3e51b | 10625 | /* Padding before pr_reg. */ |
b5430a3c | 10626 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) |
aa1ed4a9 JB |
10627 | offset += 4; |
10628 | ||
24d3e51b NC |
10629 | /* Make sure that there is enough data remaining in the note. */ |
10630 | if ((note->descsz - offset) < size) | |
10631 | return FALSE; | |
10632 | ||
aa1ed4a9 JB |
10633 | /* Make a ".reg/999" section and a ".reg" section. */ |
10634 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
10635 | size, note->descpos + offset); | |
10636 | } | |
10637 | ||
10638 | static bfd_boolean | |
10639 | elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10640 | { | |
544c67cd JB |
10641 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10642 | ||
aa1ed4a9 JB |
10643 | switch (note->type) |
10644 | { | |
10645 | case NT_PRSTATUS: | |
544c67cd JB |
10646 | if (bed->elf_backend_grok_freebsd_prstatus) |
10647 | if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note)) | |
10648 | return TRUE; | |
aa1ed4a9 JB |
10649 | return elfcore_grok_freebsd_prstatus (abfd, note); |
10650 | ||
10651 | case NT_FPREGSET: | |
10652 | return elfcore_grok_prfpreg (abfd, note); | |
10653 | ||
10654 | case NT_PRPSINFO: | |
10655 | return elfcore_grok_freebsd_psinfo (abfd, note); | |
10656 | ||
10657 | case NT_FREEBSD_THRMISC: | |
10658 | if (note->namesz == 8) | |
10659 | return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); | |
10660 | else | |
10661 | return TRUE; | |
10662 | ||
ddb2bbcf JB |
10663 | case NT_FREEBSD_PROCSTAT_PROC: |
10664 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc", | |
10665 | note); | |
10666 | ||
10667 | case NT_FREEBSD_PROCSTAT_FILES: | |
10668 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files", | |
10669 | note); | |
10670 | ||
10671 | case NT_FREEBSD_PROCSTAT_VMMAP: | |
10672 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap", | |
10673 | note); | |
10674 | ||
3350c5f5 | 10675 | case NT_FREEBSD_PROCSTAT_AUXV: |
58e07198 | 10676 | return elfcore_make_auxv_note_section (abfd, note, 4); |
3350c5f5 | 10677 | |
aa1ed4a9 JB |
10678 | case NT_X86_XSTATE: |
10679 | if (note->namesz == 8) | |
10680 | return elfcore_grok_xstatereg (abfd, note); | |
10681 | else | |
10682 | return TRUE; | |
10683 | ||
e6f3b9c3 JB |
10684 | case NT_FREEBSD_PTLWPINFO: |
10685 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo", | |
10686 | note); | |
10687 | ||
6d5be5d6 JB |
10688 | case NT_ARM_VFP: |
10689 | return elfcore_grok_arm_vfp (abfd, note); | |
10690 | ||
aa1ed4a9 JB |
10691 | default: |
10692 | return TRUE; | |
10693 | } | |
10694 | } | |
10695 | ||
b34976b6 | 10696 | static bfd_boolean |
217aa764 | 10697 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
10698 | { |
10699 | char *cp; | |
10700 | ||
10701 | cp = strchr (note->namedata, '@'); | |
10702 | if (cp != NULL) | |
10703 | { | |
d2b64500 | 10704 | *lwpidp = atoi(cp + 1); |
b34976b6 | 10705 | return TRUE; |
50b2bdb7 | 10706 | } |
b34976b6 | 10707 | return FALSE; |
50b2bdb7 AM |
10708 | } |
10709 | ||
b34976b6 | 10710 | static bfd_boolean |
217aa764 | 10711 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 10712 | { |
80a04378 NC |
10713 | if (note->descsz <= 0x7c + 31) |
10714 | return FALSE; | |
10715 | ||
50b2bdb7 | 10716 | /* Signal number at offset 0x08. */ |
228e534f | 10717 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
10718 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10719 | ||
10720 | /* Process ID at offset 0x50. */ | |
228e534f | 10721 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
10722 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
10723 | ||
10724 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 10725 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
10726 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
10727 | ||
7720ba9f MK |
10728 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
10729 | note); | |
50b2bdb7 AM |
10730 | } |
10731 | ||
b34976b6 | 10732 | static bfd_boolean |
217aa764 | 10733 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
10734 | { |
10735 | int lwp; | |
10736 | ||
10737 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 10738 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 10739 | |
58e07198 | 10740 | switch (note->type) |
50b2bdb7 | 10741 | { |
58e07198 | 10742 | case NT_NETBSDCORE_PROCINFO: |
50b2bdb7 | 10743 | /* NetBSD-specific core "procinfo". Note that we expect to |
08a40648 AM |
10744 | find this note before any of the others, which is fine, |
10745 | since the kernel writes this note out first when it | |
10746 | creates a core file. */ | |
50b2bdb7 | 10747 | return elfcore_grok_netbsd_procinfo (abfd, note); |
58e07198 CZ |
10748 | #ifdef NT_NETBSDCORE_AUXV |
10749 | case NT_NETBSDCORE_AUXV: | |
10750 | /* NetBSD-specific Elf Auxiliary Vector data. */ | |
10751 | return elfcore_make_auxv_note_section (abfd, note, 4); | |
10752 | #endif | |
10753 | default: | |
10754 | break; | |
50b2bdb7 AM |
10755 | } |
10756 | ||
58e07198 | 10757 | /* As of March 2017 there are no other machine-independent notes |
b4db1224 JT |
10758 | defined for NetBSD core files. If the note type is less |
10759 | than the start of the machine-dependent note types, we don't | |
10760 | understand it. */ | |
47d9a591 | 10761 | |
b4db1224 | 10762 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 10763 | return TRUE; |
50b2bdb7 AM |
10764 | |
10765 | ||
10766 | switch (bfd_get_arch (abfd)) | |
10767 | { | |
08a40648 AM |
10768 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
10769 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 | 10770 | |
015ec493 | 10771 | case bfd_arch_aarch64: |
50b2bdb7 AM |
10772 | case bfd_arch_alpha: |
10773 | case bfd_arch_sparc: | |
10774 | switch (note->type) | |
08a40648 AM |
10775 | { |
10776 | case NT_NETBSDCORE_FIRSTMACH+0: | |
10777 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10778 | |
08a40648 AM |
10779 | case NT_NETBSDCORE_FIRSTMACH+2: |
10780 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10781 | |
08a40648 AM |
10782 | default: |
10783 | return TRUE; | |
10784 | } | |
50b2bdb7 | 10785 | |
58e07198 CZ |
10786 | /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5. |
10787 | There's also old PT___GETREGS40 == mach + 1 for old reg | |
10788 | structure which lacks GBR. */ | |
10789 | ||
10790 | case bfd_arch_sh: | |
10791 | switch (note->type) | |
10792 | { | |
10793 | case NT_NETBSDCORE_FIRSTMACH+3: | |
10794 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
10795 | ||
10796 | case NT_NETBSDCORE_FIRSTMACH+5: | |
10797 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
10798 | ||
10799 | default: | |
10800 | return TRUE; | |
10801 | } | |
10802 | ||
08a40648 AM |
10803 | /* On all other arch's, PT_GETREGS == mach+1 and |
10804 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
10805 | |
10806 | default: | |
10807 | switch (note->type) | |
08a40648 AM |
10808 | { |
10809 | case NT_NETBSDCORE_FIRSTMACH+1: | |
10810 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10811 | |
08a40648 AM |
10812 | case NT_NETBSDCORE_FIRSTMACH+3: |
10813 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10814 | |
08a40648 AM |
10815 | default: |
10816 | return TRUE; | |
10817 | } | |
50b2bdb7 AM |
10818 | } |
10819 | /* NOTREACHED */ | |
10820 | } | |
10821 | ||
67cc5033 MK |
10822 | static bfd_boolean |
10823 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
10824 | { | |
80a04378 NC |
10825 | if (note->descsz <= 0x48 + 31) |
10826 | return FALSE; | |
10827 | ||
67cc5033 | 10828 | /* Signal number at offset 0x08. */ |
228e534f | 10829 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
10830 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10831 | ||
10832 | /* Process ID at offset 0x20. */ | |
228e534f | 10833 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
10834 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
10835 | ||
10836 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 10837 | elf_tdata (abfd)->core->command |
67cc5033 MK |
10838 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
10839 | ||
10840 | return TRUE; | |
10841 | } | |
10842 | ||
10843 | static bfd_boolean | |
10844 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10845 | { | |
10846 | if (note->type == NT_OPENBSD_PROCINFO) | |
10847 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
10848 | ||
10849 | if (note->type == NT_OPENBSD_REGS) | |
10850 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
10851 | ||
10852 | if (note->type == NT_OPENBSD_FPREGS) | |
10853 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
10854 | ||
10855 | if (note->type == NT_OPENBSD_XFPREGS) | |
10856 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
10857 | ||
10858 | if (note->type == NT_OPENBSD_AUXV) | |
58e07198 | 10859 | return elfcore_make_auxv_note_section (abfd, note, 0); |
67cc5033 MK |
10860 | |
10861 | if (note->type == NT_OPENBSD_WCOOKIE) | |
10862 | { | |
10863 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
10864 | SEC_HAS_CONTENTS); | |
10865 | ||
10866 | if (sect == NULL) | |
10867 | return FALSE; | |
10868 | sect->size = note->descsz; | |
10869 | sect->filepos = note->descpos; | |
10870 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10871 | ||
10872 | return TRUE; | |
10873 | } | |
10874 | ||
10875 | return TRUE; | |
10876 | } | |
10877 | ||
07c6e936 | 10878 | static bfd_boolean |
d3fd4074 | 10879 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
10880 | { |
10881 | void *ddata = note->descdata; | |
10882 | char buf[100]; | |
10883 | char *name; | |
10884 | asection *sect; | |
f8843e87 AM |
10885 | short sig; |
10886 | unsigned flags; | |
07c6e936 | 10887 | |
80a04378 NC |
10888 | if (note->descsz < 16) |
10889 | return FALSE; | |
10890 | ||
07c6e936 | 10891 | /* nto_procfs_status 'pid' field is at offset 0. */ |
228e534f | 10892 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 10893 | |
f8843e87 AM |
10894 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
10895 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
10896 | ||
10897 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
10898 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
10899 | |
10900 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
10901 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
10902 | { | |
228e534f AM |
10903 | elf_tdata (abfd)->core->signal = sig; |
10904 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 10905 | } |
07c6e936 | 10906 | |
f8843e87 AM |
10907 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
10908 | do not come from signals so we make sure we set the current | |
10909 | thread just in case. */ | |
10910 | if (flags & 0x00000080) | |
228e534f | 10911 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
10912 | |
10913 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 10914 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 10915 | |
a50b1753 | 10916 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10917 | if (name == NULL) |
10918 | return FALSE; | |
10919 | strcpy (name, buf); | |
10920 | ||
117ed4f8 | 10921 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10922 | if (sect == NULL) |
10923 | return FALSE; | |
10924 | ||
07d6d2b8 AM |
10925 | sect->size = note->descsz; |
10926 | sect->filepos = note->descpos; | |
07c6e936 NC |
10927 | sect->alignment_power = 2; |
10928 | ||
10929 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
10930 | } | |
10931 | ||
10932 | static bfd_boolean | |
d69f560c KW |
10933 | elfcore_grok_nto_regs (bfd *abfd, |
10934 | Elf_Internal_Note *note, | |
d3fd4074 | 10935 | long tid, |
d69f560c | 10936 | char *base) |
07c6e936 NC |
10937 | { |
10938 | char buf[100]; | |
10939 | char *name; | |
10940 | asection *sect; | |
10941 | ||
d69f560c | 10942 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 10943 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 10944 | |
a50b1753 | 10945 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10946 | if (name == NULL) |
10947 | return FALSE; | |
10948 | strcpy (name, buf); | |
10949 | ||
117ed4f8 | 10950 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10951 | if (sect == NULL) |
10952 | return FALSE; | |
10953 | ||
07d6d2b8 AM |
10954 | sect->size = note->descsz; |
10955 | sect->filepos = note->descpos; | |
07c6e936 NC |
10956 | sect->alignment_power = 2; |
10957 | ||
f8843e87 | 10958 | /* This is the current thread. */ |
228e534f | 10959 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 10960 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
10961 | |
10962 | return TRUE; | |
07c6e936 NC |
10963 | } |
10964 | ||
10965 | #define BFD_QNT_CORE_INFO 7 | |
10966 | #define BFD_QNT_CORE_STATUS 8 | |
10967 | #define BFD_QNT_CORE_GREG 9 | |
10968 | #define BFD_QNT_CORE_FPREG 10 | |
10969 | ||
10970 | static bfd_boolean | |
217aa764 | 10971 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
10972 | { |
10973 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 10974 | tid from the previous call to pass down to the next gregs |
07c6e936 | 10975 | function. */ |
d3fd4074 | 10976 | static long tid = 1; |
07c6e936 NC |
10977 | |
10978 | switch (note->type) | |
10979 | { | |
d69f560c KW |
10980 | case BFD_QNT_CORE_INFO: |
10981 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
10982 | case BFD_QNT_CORE_STATUS: | |
10983 | return elfcore_grok_nto_status (abfd, note, &tid); | |
10984 | case BFD_QNT_CORE_GREG: | |
10985 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
10986 | case BFD_QNT_CORE_FPREG: | |
10987 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
10988 | default: | |
10989 | return TRUE; | |
07c6e936 NC |
10990 | } |
10991 | } | |
10992 | ||
b15fa79e AM |
10993 | static bfd_boolean |
10994 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
10995 | { | |
10996 | char *name; | |
10997 | asection *sect; | |
10998 | size_t len; | |
10999 | ||
11000 | /* Use note name as section name. */ | |
11001 | len = note->namesz; | |
a50b1753 | 11002 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
11003 | if (name == NULL) |
11004 | return FALSE; | |
11005 | memcpy (name, note->namedata, len); | |
11006 | name[len - 1] = '\0'; | |
11007 | ||
11008 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
11009 | if (sect == NULL) | |
11010 | return FALSE; | |
11011 | ||
07d6d2b8 AM |
11012 | sect->size = note->descsz; |
11013 | sect->filepos = note->descpos; | |
b15fa79e AM |
11014 | sect->alignment_power = 1; |
11015 | ||
11016 | return TRUE; | |
11017 | } | |
11018 | ||
7c76fa91 MS |
11019 | /* Function: elfcore_write_note |
11020 | ||
47d9a591 | 11021 | Inputs: |
a39f3346 | 11022 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
11023 | name of note |
11024 | type of note | |
11025 | data for note | |
11026 | size of data for note | |
11027 | ||
a39f3346 AM |
11028 | Writes note to end of buffer. ELF64 notes are written exactly as |
11029 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
11030 | that they ought to have 8-byte namesz and descsz field, and have | |
11031 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
11032 | ||
7c76fa91 | 11033 | Return: |
a39f3346 | 11034 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
11035 | |
11036 | char * | |
a39f3346 | 11037 | elfcore_write_note (bfd *abfd, |
217aa764 | 11038 | char *buf, |
a39f3346 | 11039 | int *bufsiz, |
217aa764 | 11040 | const char *name, |
a39f3346 | 11041 | int type, |
217aa764 | 11042 | const void *input, |
a39f3346 | 11043 | int size) |
7c76fa91 MS |
11044 | { |
11045 | Elf_External_Note *xnp; | |
d4c88bbb | 11046 | size_t namesz; |
d4c88bbb | 11047 | size_t newspace; |
a39f3346 | 11048 | char *dest; |
7c76fa91 | 11049 | |
d4c88bbb | 11050 | namesz = 0; |
d4c88bbb | 11051 | if (name != NULL) |
a39f3346 | 11052 | namesz = strlen (name) + 1; |
d4c88bbb | 11053 | |
a39f3346 | 11054 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 11055 | |
a50b1753 | 11056 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
11057 | if (buf == NULL) |
11058 | return buf; | |
a39f3346 | 11059 | dest = buf + *bufsiz; |
7c76fa91 MS |
11060 | *bufsiz += newspace; |
11061 | xnp = (Elf_External_Note *) dest; | |
11062 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
11063 | H_PUT_32 (abfd, size, xnp->descsz); | |
11064 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
11065 | dest = xnp->name; |
11066 | if (name != NULL) | |
11067 | { | |
11068 | memcpy (dest, name, namesz); | |
11069 | dest += namesz; | |
a39f3346 | 11070 | while (namesz & 3) |
d4c88bbb AM |
11071 | { |
11072 | *dest++ = '\0'; | |
a39f3346 | 11073 | ++namesz; |
d4c88bbb AM |
11074 | } |
11075 | } | |
11076 | memcpy (dest, input, size); | |
a39f3346 AM |
11077 | dest += size; |
11078 | while (size & 3) | |
11079 | { | |
11080 | *dest++ = '\0'; | |
11081 | ++size; | |
11082 | } | |
11083 | return buf; | |
7c76fa91 MS |
11084 | } |
11085 | ||
602f1657 AM |
11086 | /* gcc-8 warns (*) on all the strncpy calls in this function about |
11087 | possible string truncation. The "truncation" is not a bug. We | |
11088 | have an external representation of structs with fields that are not | |
11089 | necessarily NULL terminated and corresponding internal | |
11090 | representation fields that are one larger so that they can always | |
11091 | be NULL terminated. | |
11092 | gcc versions between 4.2 and 4.6 do not allow pragma control of | |
11093 | diagnostics inside functions, giving a hard error if you try to use | |
11094 | the finer control available with later versions. | |
11095 | gcc prior to 4.2 warns about diagnostic push and pop. | |
11096 | gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown, | |
11097 | unless you also add #pragma GCC diagnostic ignored "-Wpragma". | |
11098 | (*) Depending on your system header files! */ | |
d99b4b92 | 11099 | #if GCC_VERSION >= 8000 |
602f1657 AM |
11100 | # pragma GCC diagnostic push |
11101 | # pragma GCC diagnostic ignored "-Wstringop-truncation" | |
d99b4b92 | 11102 | #endif |
7c76fa91 | 11103 | char * |
217aa764 AM |
11104 | elfcore_write_prpsinfo (bfd *abfd, |
11105 | char *buf, | |
11106 | int *bufsiz, | |
11107 | const char *fname, | |
11108 | const char *psargs) | |
7c76fa91 | 11109 | { |
183e98be AM |
11110 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11111 | ||
11112 | if (bed->elf_backend_write_core_note != NULL) | |
11113 | { | |
11114 | char *ret; | |
11115 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
11116 | NT_PRPSINFO, fname, psargs); | |
11117 | if (ret != NULL) | |
11118 | return ret; | |
11119 | } | |
7c76fa91 | 11120 | |
1f20dca5 | 11121 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
602f1657 | 11122 | # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
183e98be AM |
11123 | if (bed->s->elfclass == ELFCLASS32) |
11124 | { | |
602f1657 | 11125 | # if defined (HAVE_PSINFO32_T) |
183e98be AM |
11126 | psinfo32_t data; |
11127 | int note_type = NT_PSINFO; | |
602f1657 | 11128 | # else |
183e98be AM |
11129 | prpsinfo32_t data; |
11130 | int note_type = NT_PRPSINFO; | |
602f1657 | 11131 | # endif |
183e98be AM |
11132 | |
11133 | memset (&data, 0, sizeof (data)); | |
11134 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
11135 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
11136 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 11137 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
11138 | } |
11139 | else | |
602f1657 | 11140 | # endif |
183e98be | 11141 | { |
602f1657 | 11142 | # if defined (HAVE_PSINFO_T) |
183e98be AM |
11143 | psinfo_t data; |
11144 | int note_type = NT_PSINFO; | |
602f1657 | 11145 | # else |
183e98be AM |
11146 | prpsinfo_t data; |
11147 | int note_type = NT_PRPSINFO; | |
602f1657 | 11148 | # endif |
7c76fa91 | 11149 | |
183e98be AM |
11150 | memset (&data, 0, sizeof (data)); |
11151 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
11152 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
11153 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 11154 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 11155 | } |
7c76fa91 MS |
11156 | #endif /* PSINFO_T or PRPSINFO_T */ |
11157 | ||
1f20dca5 UW |
11158 | free (buf); |
11159 | return NULL; | |
11160 | } | |
d99b4b92 | 11161 | #if GCC_VERSION >= 8000 |
602f1657 | 11162 | # pragma GCC diagnostic pop |
d99b4b92 | 11163 | #endif |
1f20dca5 | 11164 | |
70a38d42 SDJ |
11165 | char * |
11166 | elfcore_write_linux_prpsinfo32 | |
11167 | (bfd *abfd, char *buf, int *bufsiz, | |
11168 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
11169 | { | |
a2f63b2e MR |
11170 | if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16) |
11171 | { | |
11172 | struct elf_external_linux_prpsinfo32_ugid16 data; | |
11173 | ||
11174 | swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data); | |
11175 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
11176 | &data, sizeof (data)); | |
11177 | } | |
11178 | else | |
11179 | { | |
11180 | struct elf_external_linux_prpsinfo32_ugid32 data; | |
70a38d42 | 11181 | |
a2f63b2e MR |
11182 | swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data); |
11183 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
11184 | &data, sizeof (data)); | |
11185 | } | |
70a38d42 SDJ |
11186 | } |
11187 | ||
11188 | char * | |
11189 | elfcore_write_linux_prpsinfo64 | |
11190 | (bfd *abfd, char *buf, int *bufsiz, | |
11191 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
11192 | { | |
3c9a7b0d MR |
11193 | if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16) |
11194 | { | |
11195 | struct elf_external_linux_prpsinfo64_ugid16 data; | |
11196 | ||
11197 | swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data); | |
11198 | return elfcore_write_note (abfd, buf, bufsiz, | |
11199 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
11200 | } | |
11201 | else | |
11202 | { | |
11203 | struct elf_external_linux_prpsinfo64_ugid32 data; | |
70a38d42 | 11204 | |
3c9a7b0d MR |
11205 | swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data); |
11206 | return elfcore_write_note (abfd, buf, bufsiz, | |
11207 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
11208 | } | |
70a38d42 SDJ |
11209 | } |
11210 | ||
7c76fa91 | 11211 | char * |
217aa764 AM |
11212 | elfcore_write_prstatus (bfd *abfd, |
11213 | char *buf, | |
11214 | int *bufsiz, | |
11215 | long pid, | |
11216 | int cursig, | |
11217 | const void *gregs) | |
7c76fa91 | 11218 | { |
183e98be | 11219 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 11220 | |
183e98be AM |
11221 | if (bed->elf_backend_write_core_note != NULL) |
11222 | { | |
11223 | char *ret; | |
11224 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
11225 | NT_PRSTATUS, | |
11226 | pid, cursig, gregs); | |
11227 | if (ret != NULL) | |
11228 | return ret; | |
11229 | } | |
11230 | ||
1f20dca5 | 11231 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
11232 | #if defined (HAVE_PRSTATUS32_T) |
11233 | if (bed->s->elfclass == ELFCLASS32) | |
11234 | { | |
11235 | prstatus32_t prstat; | |
11236 | ||
11237 | memset (&prstat, 0, sizeof (prstat)); | |
11238 | prstat.pr_pid = pid; | |
11239 | prstat.pr_cursig = cursig; | |
11240 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 11241 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
11242 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
11243 | } | |
11244 | else | |
11245 | #endif | |
11246 | { | |
11247 | prstatus_t prstat; | |
11248 | ||
11249 | memset (&prstat, 0, sizeof (prstat)); | |
11250 | prstat.pr_pid = pid; | |
11251 | prstat.pr_cursig = cursig; | |
11252 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 11253 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
11254 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
11255 | } | |
7c76fa91 MS |
11256 | #endif /* HAVE_PRSTATUS_T */ |
11257 | ||
1f20dca5 UW |
11258 | free (buf); |
11259 | return NULL; | |
11260 | } | |
11261 | ||
51316059 MS |
11262 | #if defined (HAVE_LWPSTATUS_T) |
11263 | char * | |
217aa764 AM |
11264 | elfcore_write_lwpstatus (bfd *abfd, |
11265 | char *buf, | |
11266 | int *bufsiz, | |
11267 | long pid, | |
11268 | int cursig, | |
11269 | const void *gregs) | |
51316059 MS |
11270 | { |
11271 | lwpstatus_t lwpstat; | |
183e98be | 11272 | const char *note_name = "CORE"; |
51316059 MS |
11273 | |
11274 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
11275 | lwpstat.pr_lwpid = pid >> 16; | |
11276 | lwpstat.pr_cursig = cursig; | |
11277 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 11278 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
11279 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
11280 | #if !defined(gregs) | |
11281 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
11282 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
11283 | #else | |
11284 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
11285 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
11286 | #endif | |
11287 | #endif | |
47d9a591 | 11288 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
11289 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
11290 | } | |
11291 | #endif /* HAVE_LWPSTATUS_T */ | |
11292 | ||
7c76fa91 MS |
11293 | #if defined (HAVE_PSTATUS_T) |
11294 | char * | |
217aa764 AM |
11295 | elfcore_write_pstatus (bfd *abfd, |
11296 | char *buf, | |
11297 | int *bufsiz, | |
11298 | long pid, | |
6c10990d NC |
11299 | int cursig ATTRIBUTE_UNUSED, |
11300 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 11301 | { |
183e98be AM |
11302 | const char *note_name = "CORE"; |
11303 | #if defined (HAVE_PSTATUS32_T) | |
11304 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 11305 | |
183e98be AM |
11306 | if (bed->s->elfclass == ELFCLASS32) |
11307 | { | |
11308 | pstatus32_t pstat; | |
11309 | ||
11310 | memset (&pstat, 0, sizeof (pstat)); | |
11311 | pstat.pr_pid = pid & 0xffff; | |
11312 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
11313 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
11314 | return buf; | |
11315 | } | |
11316 | else | |
11317 | #endif | |
11318 | { | |
11319 | pstatus_t pstat; | |
11320 | ||
11321 | memset (&pstat, 0, sizeof (pstat)); | |
11322 | pstat.pr_pid = pid & 0xffff; | |
11323 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
11324 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
11325 | return buf; | |
11326 | } | |
7c76fa91 MS |
11327 | } |
11328 | #endif /* HAVE_PSTATUS_T */ | |
11329 | ||
11330 | char * | |
217aa764 AM |
11331 | elfcore_write_prfpreg (bfd *abfd, |
11332 | char *buf, | |
11333 | int *bufsiz, | |
11334 | const void *fpregs, | |
11335 | int size) | |
7c76fa91 | 11336 | { |
183e98be | 11337 | const char *note_name = "CORE"; |
47d9a591 | 11338 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
11339 | note_name, NT_FPREGSET, fpregs, size); |
11340 | } | |
11341 | ||
11342 | char * | |
217aa764 AM |
11343 | elfcore_write_prxfpreg (bfd *abfd, |
11344 | char *buf, | |
11345 | int *bufsiz, | |
11346 | const void *xfpregs, | |
11347 | int size) | |
7c76fa91 MS |
11348 | { |
11349 | char *note_name = "LINUX"; | |
47d9a591 | 11350 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
11351 | note_name, NT_PRXFPREG, xfpregs, size); |
11352 | } | |
11353 | ||
4339cae0 L |
11354 | char * |
11355 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
11356 | const void *xfpregs, int size) | |
11357 | { | |
97de3545 JB |
11358 | char *note_name; |
11359 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
11360 | note_name = "FreeBSD"; | |
11361 | else | |
11362 | note_name = "LINUX"; | |
4339cae0 L |
11363 | return elfcore_write_note (abfd, buf, bufsiz, |
11364 | note_name, NT_X86_XSTATE, xfpregs, size); | |
11365 | } | |
11366 | ||
97753bd5 AM |
11367 | char * |
11368 | elfcore_write_ppc_vmx (bfd *abfd, | |
11369 | char *buf, | |
11370 | int *bufsiz, | |
11371 | const void *ppc_vmx, | |
11372 | int size) | |
11373 | { | |
11374 | char *note_name = "LINUX"; | |
11375 | return elfcore_write_note (abfd, buf, bufsiz, | |
11376 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
11377 | } | |
11378 | ||
89eeb0bc LM |
11379 | char * |
11380 | elfcore_write_ppc_vsx (bfd *abfd, | |
07d6d2b8 AM |
11381 | char *buf, |
11382 | int *bufsiz, | |
11383 | const void *ppc_vsx, | |
11384 | int size) | |
89eeb0bc LM |
11385 | { |
11386 | char *note_name = "LINUX"; | |
11387 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11388 | note_name, NT_PPC_VSX, ppc_vsx, size); |
89eeb0bc LM |
11389 | } |
11390 | ||
cb2366c1 EBM |
11391 | char * |
11392 | elfcore_write_ppc_tar (bfd *abfd, | |
4b24dd1a AM |
11393 | char *buf, |
11394 | int *bufsiz, | |
11395 | const void *ppc_tar, | |
11396 | int size) | |
cb2366c1 EBM |
11397 | { |
11398 | char *note_name = "LINUX"; | |
11399 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11400 | note_name, NT_PPC_TAR, ppc_tar, size); |
cb2366c1 EBM |
11401 | } |
11402 | ||
11403 | char * | |
11404 | elfcore_write_ppc_ppr (bfd *abfd, | |
4b24dd1a AM |
11405 | char *buf, |
11406 | int *bufsiz, | |
11407 | const void *ppc_ppr, | |
11408 | int size) | |
cb2366c1 EBM |
11409 | { |
11410 | char *note_name = "LINUX"; | |
11411 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11412 | note_name, NT_PPC_PPR, ppc_ppr, size); |
cb2366c1 EBM |
11413 | } |
11414 | ||
11415 | char * | |
11416 | elfcore_write_ppc_dscr (bfd *abfd, | |
4b24dd1a AM |
11417 | char *buf, |
11418 | int *bufsiz, | |
11419 | const void *ppc_dscr, | |
11420 | int size) | |
cb2366c1 EBM |
11421 | { |
11422 | char *note_name = "LINUX"; | |
11423 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11424 | note_name, NT_PPC_DSCR, ppc_dscr, size); |
cb2366c1 EBM |
11425 | } |
11426 | ||
11427 | char * | |
11428 | elfcore_write_ppc_ebb (bfd *abfd, | |
4b24dd1a AM |
11429 | char *buf, |
11430 | int *bufsiz, | |
11431 | const void *ppc_ebb, | |
11432 | int size) | |
cb2366c1 EBM |
11433 | { |
11434 | char *note_name = "LINUX"; | |
11435 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11436 | note_name, NT_PPC_EBB, ppc_ebb, size); |
cb2366c1 EBM |
11437 | } |
11438 | ||
11439 | char * | |
11440 | elfcore_write_ppc_pmu (bfd *abfd, | |
4b24dd1a AM |
11441 | char *buf, |
11442 | int *bufsiz, | |
11443 | const void *ppc_pmu, | |
11444 | int size) | |
cb2366c1 EBM |
11445 | { |
11446 | char *note_name = "LINUX"; | |
11447 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11448 | note_name, NT_PPC_PMU, ppc_pmu, size); |
cb2366c1 EBM |
11449 | } |
11450 | ||
11451 | char * | |
11452 | elfcore_write_ppc_tm_cgpr (bfd *abfd, | |
4b24dd1a AM |
11453 | char *buf, |
11454 | int *bufsiz, | |
11455 | const void *ppc_tm_cgpr, | |
11456 | int size) | |
cb2366c1 EBM |
11457 | { |
11458 | char *note_name = "LINUX"; | |
11459 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11460 | note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size); |
cb2366c1 EBM |
11461 | } |
11462 | ||
11463 | char * | |
11464 | elfcore_write_ppc_tm_cfpr (bfd *abfd, | |
4b24dd1a AM |
11465 | char *buf, |
11466 | int *bufsiz, | |
11467 | const void *ppc_tm_cfpr, | |
11468 | int size) | |
cb2366c1 EBM |
11469 | { |
11470 | char *note_name = "LINUX"; | |
11471 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11472 | note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size); |
cb2366c1 EBM |
11473 | } |
11474 | ||
11475 | char * | |
11476 | elfcore_write_ppc_tm_cvmx (bfd *abfd, | |
4b24dd1a AM |
11477 | char *buf, |
11478 | int *bufsiz, | |
11479 | const void *ppc_tm_cvmx, | |
11480 | int size) | |
cb2366c1 EBM |
11481 | { |
11482 | char *note_name = "LINUX"; | |
11483 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11484 | note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size); |
cb2366c1 EBM |
11485 | } |
11486 | ||
11487 | char * | |
11488 | elfcore_write_ppc_tm_cvsx (bfd *abfd, | |
4b24dd1a AM |
11489 | char *buf, |
11490 | int *bufsiz, | |
11491 | const void *ppc_tm_cvsx, | |
11492 | int size) | |
cb2366c1 EBM |
11493 | { |
11494 | char *note_name = "LINUX"; | |
11495 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11496 | note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size); |
cb2366c1 EBM |
11497 | } |
11498 | ||
11499 | char * | |
11500 | elfcore_write_ppc_tm_spr (bfd *abfd, | |
4b24dd1a AM |
11501 | char *buf, |
11502 | int *bufsiz, | |
11503 | const void *ppc_tm_spr, | |
11504 | int size) | |
cb2366c1 EBM |
11505 | { |
11506 | char *note_name = "LINUX"; | |
11507 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11508 | note_name, NT_PPC_TM_SPR, ppc_tm_spr, size); |
cb2366c1 EBM |
11509 | } |
11510 | ||
11511 | char * | |
11512 | elfcore_write_ppc_tm_ctar (bfd *abfd, | |
4b24dd1a AM |
11513 | char *buf, |
11514 | int *bufsiz, | |
11515 | const void *ppc_tm_ctar, | |
11516 | int size) | |
cb2366c1 EBM |
11517 | { |
11518 | char *note_name = "LINUX"; | |
11519 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11520 | note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size); |
cb2366c1 EBM |
11521 | } |
11522 | ||
11523 | char * | |
11524 | elfcore_write_ppc_tm_cppr (bfd *abfd, | |
4b24dd1a AM |
11525 | char *buf, |
11526 | int *bufsiz, | |
11527 | const void *ppc_tm_cppr, | |
11528 | int size) | |
cb2366c1 EBM |
11529 | { |
11530 | char *note_name = "LINUX"; | |
11531 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11532 | note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size); |
cb2366c1 EBM |
11533 | } |
11534 | ||
11535 | char * | |
11536 | elfcore_write_ppc_tm_cdscr (bfd *abfd, | |
4b24dd1a AM |
11537 | char *buf, |
11538 | int *bufsiz, | |
11539 | const void *ppc_tm_cdscr, | |
11540 | int size) | |
cb2366c1 EBM |
11541 | { |
11542 | char *note_name = "LINUX"; | |
11543 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11544 | note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size); |
cb2366c1 EBM |
11545 | } |
11546 | ||
0675e188 UW |
11547 | static char * |
11548 | elfcore_write_s390_high_gprs (bfd *abfd, | |
11549 | char *buf, | |
11550 | int *bufsiz, | |
11551 | const void *s390_high_gprs, | |
11552 | int size) | |
11553 | { | |
11554 | char *note_name = "LINUX"; | |
11555 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11556 | note_name, NT_S390_HIGH_GPRS, |
0675e188 UW |
11557 | s390_high_gprs, size); |
11558 | } | |
11559 | ||
d7eeb400 MS |
11560 | char * |
11561 | elfcore_write_s390_timer (bfd *abfd, | |
07d6d2b8 AM |
11562 | char *buf, |
11563 | int *bufsiz, | |
11564 | const void *s390_timer, | |
11565 | int size) | |
d7eeb400 MS |
11566 | { |
11567 | char *note_name = "LINUX"; | |
11568 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11569 | note_name, NT_S390_TIMER, s390_timer, size); |
d7eeb400 MS |
11570 | } |
11571 | ||
11572 | char * | |
11573 | elfcore_write_s390_todcmp (bfd *abfd, | |
07d6d2b8 AM |
11574 | char *buf, |
11575 | int *bufsiz, | |
11576 | const void *s390_todcmp, | |
11577 | int size) | |
d7eeb400 MS |
11578 | { |
11579 | char *note_name = "LINUX"; | |
11580 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11581 | note_name, NT_S390_TODCMP, s390_todcmp, size); |
d7eeb400 MS |
11582 | } |
11583 | ||
11584 | char * | |
11585 | elfcore_write_s390_todpreg (bfd *abfd, | |
07d6d2b8 AM |
11586 | char *buf, |
11587 | int *bufsiz, | |
11588 | const void *s390_todpreg, | |
11589 | int size) | |
d7eeb400 MS |
11590 | { |
11591 | char *note_name = "LINUX"; | |
11592 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11593 | note_name, NT_S390_TODPREG, s390_todpreg, size); |
d7eeb400 MS |
11594 | } |
11595 | ||
11596 | char * | |
11597 | elfcore_write_s390_ctrs (bfd *abfd, | |
07d6d2b8 AM |
11598 | char *buf, |
11599 | int *bufsiz, | |
11600 | const void *s390_ctrs, | |
11601 | int size) | |
d7eeb400 MS |
11602 | { |
11603 | char *note_name = "LINUX"; | |
11604 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11605 | note_name, NT_S390_CTRS, s390_ctrs, size); |
d7eeb400 MS |
11606 | } |
11607 | ||
11608 | char * | |
11609 | elfcore_write_s390_prefix (bfd *abfd, | |
07d6d2b8 AM |
11610 | char *buf, |
11611 | int *bufsiz, | |
11612 | const void *s390_prefix, | |
11613 | int size) | |
d7eeb400 MS |
11614 | { |
11615 | char *note_name = "LINUX"; | |
11616 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11617 | note_name, NT_S390_PREFIX, s390_prefix, size); |
d7eeb400 MS |
11618 | } |
11619 | ||
355b81d9 UW |
11620 | char * |
11621 | elfcore_write_s390_last_break (bfd *abfd, | |
11622 | char *buf, | |
11623 | int *bufsiz, | |
11624 | const void *s390_last_break, | |
11625 | int size) | |
11626 | { | |
11627 | char *note_name = "LINUX"; | |
11628 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11629 | note_name, NT_S390_LAST_BREAK, |
355b81d9 UW |
11630 | s390_last_break, size); |
11631 | } | |
11632 | ||
11633 | char * | |
11634 | elfcore_write_s390_system_call (bfd *abfd, | |
11635 | char *buf, | |
11636 | int *bufsiz, | |
11637 | const void *s390_system_call, | |
11638 | int size) | |
11639 | { | |
11640 | char *note_name = "LINUX"; | |
11641 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11642 | note_name, NT_S390_SYSTEM_CALL, |
355b81d9 UW |
11643 | s390_system_call, size); |
11644 | } | |
11645 | ||
abb3f6cc NC |
11646 | char * |
11647 | elfcore_write_s390_tdb (bfd *abfd, | |
11648 | char *buf, | |
11649 | int *bufsiz, | |
11650 | const void *s390_tdb, | |
11651 | int size) | |
11652 | { | |
11653 | char *note_name = "LINUX"; | |
11654 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11655 | note_name, NT_S390_TDB, s390_tdb, size); |
abb3f6cc NC |
11656 | } |
11657 | ||
4ef9f41a AA |
11658 | char * |
11659 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
11660 | char *buf, | |
11661 | int *bufsiz, | |
11662 | const void *s390_vxrs_low, | |
11663 | int size) | |
11664 | { | |
11665 | char *note_name = "LINUX"; | |
11666 | return elfcore_write_note (abfd, buf, bufsiz, | |
11667 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
11668 | } | |
11669 | ||
11670 | char * | |
11671 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
11672 | char *buf, | |
11673 | int *bufsiz, | |
11674 | const void *s390_vxrs_high, | |
11675 | int size) | |
11676 | { | |
11677 | char *note_name = "LINUX"; | |
11678 | return elfcore_write_note (abfd, buf, bufsiz, | |
11679 | note_name, NT_S390_VXRS_HIGH, | |
11680 | s390_vxrs_high, size); | |
11681 | } | |
11682 | ||
88ab90e8 AA |
11683 | char * |
11684 | elfcore_write_s390_gs_cb (bfd *abfd, | |
11685 | char *buf, | |
11686 | int *bufsiz, | |
11687 | const void *s390_gs_cb, | |
11688 | int size) | |
11689 | { | |
11690 | char *note_name = "LINUX"; | |
11691 | return elfcore_write_note (abfd, buf, bufsiz, | |
11692 | note_name, NT_S390_GS_CB, | |
11693 | s390_gs_cb, size); | |
11694 | } | |
11695 | ||
11696 | char * | |
11697 | elfcore_write_s390_gs_bc (bfd *abfd, | |
11698 | char *buf, | |
11699 | int *bufsiz, | |
11700 | const void *s390_gs_bc, | |
11701 | int size) | |
11702 | { | |
11703 | char *note_name = "LINUX"; | |
11704 | return elfcore_write_note (abfd, buf, bufsiz, | |
11705 | note_name, NT_S390_GS_BC, | |
11706 | s390_gs_bc, size); | |
11707 | } | |
11708 | ||
faa9a424 UW |
11709 | char * |
11710 | elfcore_write_arm_vfp (bfd *abfd, | |
11711 | char *buf, | |
11712 | int *bufsiz, | |
11713 | const void *arm_vfp, | |
11714 | int size) | |
11715 | { | |
11716 | char *note_name = "LINUX"; | |
11717 | return elfcore_write_note (abfd, buf, bufsiz, | |
11718 | note_name, NT_ARM_VFP, arm_vfp, size); | |
11719 | } | |
11720 | ||
652451f8 YZ |
11721 | char * |
11722 | elfcore_write_aarch_tls (bfd *abfd, | |
11723 | char *buf, | |
11724 | int *bufsiz, | |
11725 | const void *aarch_tls, | |
11726 | int size) | |
11727 | { | |
11728 | char *note_name = "LINUX"; | |
11729 | return elfcore_write_note (abfd, buf, bufsiz, | |
11730 | note_name, NT_ARM_TLS, aarch_tls, size); | |
11731 | } | |
11732 | ||
11733 | char * | |
11734 | elfcore_write_aarch_hw_break (bfd *abfd, | |
11735 | char *buf, | |
11736 | int *bufsiz, | |
11737 | const void *aarch_hw_break, | |
11738 | int size) | |
11739 | { | |
11740 | char *note_name = "LINUX"; | |
11741 | return elfcore_write_note (abfd, buf, bufsiz, | |
11742 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
11743 | } | |
11744 | ||
11745 | char * | |
11746 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
11747 | char *buf, | |
11748 | int *bufsiz, | |
11749 | const void *aarch_hw_watch, | |
11750 | int size) | |
11751 | { | |
11752 | char *note_name = "LINUX"; | |
11753 | return elfcore_write_note (abfd, buf, bufsiz, | |
11754 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
11755 | } | |
11756 | ||
ad1cc4e4 AH |
11757 | char * |
11758 | elfcore_write_aarch_sve (bfd *abfd, | |
11759 | char *buf, | |
11760 | int *bufsiz, | |
11761 | const void *aarch_sve, | |
11762 | int size) | |
11763 | { | |
11764 | char *note_name = "LINUX"; | |
11765 | return elfcore_write_note (abfd, buf, bufsiz, | |
11766 | note_name, NT_ARM_SVE, aarch_sve, size); | |
11767 | } | |
11768 | ||
e6c3b5bf AH |
11769 | char * |
11770 | elfcore_write_aarch_pauth (bfd *abfd, | |
11771 | char *buf, | |
11772 | int *bufsiz, | |
11773 | const void *aarch_pauth, | |
11774 | int size) | |
11775 | { | |
11776 | char *note_name = "LINUX"; | |
11777 | return elfcore_write_note (abfd, buf, bufsiz, | |
11778 | note_name, NT_ARM_PAC_MASK, aarch_pauth, size); | |
11779 | } | |
11780 | ||
bb864ac1 CES |
11781 | char * |
11782 | elfcore_write_register_note (bfd *abfd, | |
11783 | char *buf, | |
11784 | int *bufsiz, | |
11785 | const char *section, | |
11786 | const void *data, | |
11787 | int size) | |
11788 | { | |
11789 | if (strcmp (section, ".reg2") == 0) | |
11790 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
11791 | if (strcmp (section, ".reg-xfp") == 0) | |
11792 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
11793 | if (strcmp (section, ".reg-xstate") == 0) |
11794 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
11795 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
11796 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
11797 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
11798 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
cb2366c1 EBM |
11799 | if (strcmp (section, ".reg-ppc-tar") == 0) |
11800 | return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size); | |
11801 | if (strcmp (section, ".reg-ppc-ppr") == 0) | |
11802 | return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size); | |
11803 | if (strcmp (section, ".reg-ppc-dscr") == 0) | |
11804 | return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size); | |
11805 | if (strcmp (section, ".reg-ppc-ebb") == 0) | |
11806 | return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size); | |
11807 | if (strcmp (section, ".reg-ppc-pmu") == 0) | |
11808 | return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size); | |
11809 | if (strcmp (section, ".reg-ppc-tm-cgpr") == 0) | |
11810 | return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size); | |
11811 | if (strcmp (section, ".reg-ppc-tm-cfpr") == 0) | |
11812 | return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size); | |
11813 | if (strcmp (section, ".reg-ppc-tm-cvmx") == 0) | |
11814 | return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size); | |
11815 | if (strcmp (section, ".reg-ppc-tm-cvsx") == 0) | |
11816 | return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size); | |
11817 | if (strcmp (section, ".reg-ppc-tm-spr") == 0) | |
11818 | return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size); | |
11819 | if (strcmp (section, ".reg-ppc-tm-ctar") == 0) | |
11820 | return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size); | |
11821 | if (strcmp (section, ".reg-ppc-tm-cppr") == 0) | |
11822 | return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size); | |
11823 | if (strcmp (section, ".reg-ppc-tm-cdscr") == 0) | |
11824 | return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
11825 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
11826 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
11827 | if (strcmp (section, ".reg-s390-timer") == 0) |
11828 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
11829 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
11830 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
11831 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
11832 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
11833 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
11834 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
11835 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
11836 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
11837 | if (strcmp (section, ".reg-s390-last-break") == 0) |
11838 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
11839 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
11840 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
11841 | if (strcmp (section, ".reg-s390-tdb") == 0) |
11842 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
11843 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
11844 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
11845 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
11846 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
88ab90e8 AA |
11847 | if (strcmp (section, ".reg-s390-gs-cb") == 0) |
11848 | return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size); | |
11849 | if (strcmp (section, ".reg-s390-gs-bc") == 0) | |
11850 | return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
11851 | if (strcmp (section, ".reg-arm-vfp") == 0) |
11852 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
11853 | if (strcmp (section, ".reg-aarch-tls") == 0) |
11854 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
11855 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
11856 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
11857 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
11858 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
ad1cc4e4 AH |
11859 | if (strcmp (section, ".reg-aarch-sve") == 0) |
11860 | return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size); | |
e6c3b5bf AH |
11861 | if (strcmp (section, ".reg-aarch-pauth") == 0) |
11862 | return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
11863 | return NULL; |
11864 | } | |
11865 | ||
b34976b6 | 11866 | static bfd_boolean |
276da9b3 L |
11867 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset, |
11868 | size_t align) | |
252b5132 | 11869 | { |
c044fabd | 11870 | char *p; |
252b5132 | 11871 | |
276da9b3 L |
11872 | /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1. |
11873 | gABI specifies that PT_NOTE alignment should be aligned to 4 | |
11874 | bytes for 32-bit objects and to 8 bytes for 64-bit objects. If | |
11875 | align is less than 4, we use 4 byte alignment. */ | |
11876 | if (align < 4) | |
11877 | align = 4; | |
ef135d43 NC |
11878 | if (align != 4 && align != 8) |
11879 | return FALSE; | |
276da9b3 | 11880 | |
252b5132 RH |
11881 | p = buf; |
11882 | while (p < buf + size) | |
11883 | { | |
c044fabd | 11884 | Elf_External_Note *xnp = (Elf_External_Note *) p; |
252b5132 RH |
11885 | Elf_Internal_Note in; |
11886 | ||
baea7ef1 AM |
11887 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
11888 | return FALSE; | |
11889 | ||
dc810e39 | 11890 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 11891 | |
dc810e39 | 11892 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 11893 | in.namedata = xnp->name; |
baea7ef1 AM |
11894 | if (in.namesz > buf - in.namedata + size) |
11895 | return FALSE; | |
252b5132 | 11896 | |
dc810e39 | 11897 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
276da9b3 | 11898 | in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align); |
252b5132 | 11899 | in.descpos = offset + (in.descdata - buf); |
baea7ef1 AM |
11900 | if (in.descsz != 0 |
11901 | && (in.descdata >= buf + size | |
11902 | || in.descsz > buf - in.descdata + size)) | |
11903 | return FALSE; | |
252b5132 | 11904 | |
718175fa | 11905 | switch (bfd_get_format (abfd)) |
07d6d2b8 | 11906 | { |
718175fa JK |
11907 | default: |
11908 | return TRUE; | |
11909 | ||
11910 | case bfd_core: | |
f64e188b | 11911 | { |
8acbedd6 | 11912 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 11913 | struct |
718175fa | 11914 | { |
f64e188b | 11915 | const char * string; |
8acbedd6 | 11916 | size_t len; |
f64e188b | 11917 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 11918 | } |
f64e188b | 11919 | grokers[] = |
b15fa79e | 11920 | { |
8acbedd6 | 11921 | GROKER_ELEMENT ("", elfcore_grok_note), |
aa1ed4a9 | 11922 | GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), |
8acbedd6 KS |
11923 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), |
11924 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
11925 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
864619bb KS |
11926 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note), |
11927 | GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note) | |
f64e188b | 11928 | }; |
8acbedd6 | 11929 | #undef GROKER_ELEMENT |
f64e188b NC |
11930 | int i; |
11931 | ||
11932 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
11933 | { |
11934 | if (in.namesz >= grokers[i].len | |
11935 | && strncmp (in.namedata, grokers[i].string, | |
11936 | grokers[i].len) == 0) | |
11937 | { | |
11938 | if (! grokers[i].func (abfd, & in)) | |
11939 | return FALSE; | |
11940 | break; | |
11941 | } | |
11942 | } | |
f64e188b NC |
11943 | break; |
11944 | } | |
718175fa JK |
11945 | |
11946 | case bfd_object: | |
11947 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
11948 | { | |
11949 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
11950 | return FALSE; | |
11951 | } | |
e21e5835 NC |
11952 | else if (in.namesz == sizeof "stapsdt" |
11953 | && strcmp (in.namedata, "stapsdt") == 0) | |
11954 | { | |
11955 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
11956 | return FALSE; | |
11957 | } | |
718175fa | 11958 | break; |
08a40648 | 11959 | } |
252b5132 | 11960 | |
276da9b3 | 11961 | p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align); |
252b5132 RH |
11962 | } |
11963 | ||
718175fa JK |
11964 | return TRUE; |
11965 | } | |
11966 | ||
864619bb | 11967 | bfd_boolean |
276da9b3 L |
11968 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size, |
11969 | size_t align) | |
718175fa JK |
11970 | { |
11971 | char *buf; | |
11972 | ||
957e1fc1 | 11973 | if (size == 0 || (size + 1) == 0) |
718175fa JK |
11974 | return TRUE; |
11975 | ||
11976 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
11977 | return FALSE; | |
11978 | ||
2bb3687b | 11979 | buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size); |
718175fa JK |
11980 | if (buf == NULL) |
11981 | return FALSE; | |
11982 | ||
f64e188b NC |
11983 | /* PR 17512: file: ec08f814 |
11984 | 0-termintate the buffer so that string searches will not overflow. */ | |
11985 | buf[size] = 0; | |
11986 | ||
2bb3687b | 11987 | if (!elf_parse_notes (abfd, buf, size, offset, align)) |
718175fa JK |
11988 | { |
11989 | free (buf); | |
11990 | return FALSE; | |
11991 | } | |
11992 | ||
252b5132 | 11993 | free (buf); |
b34976b6 | 11994 | return TRUE; |
252b5132 | 11995 | } |
98d8431c JB |
11996 | \f |
11997 | /* Providing external access to the ELF program header table. */ | |
11998 | ||
11999 | /* Return an upper bound on the number of bytes required to store a | |
12000 | copy of ABFD's program header table entries. Return -1 if an error | |
12001 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 12002 | |
98d8431c | 12003 | long |
217aa764 | 12004 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
12005 | { |
12006 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
12007 | { | |
12008 | bfd_set_error (bfd_error_wrong_format); | |
12009 | return -1; | |
12010 | } | |
12011 | ||
936e320b | 12012 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
12013 | } |
12014 | ||
98d8431c JB |
12015 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
12016 | will be stored as an array of Elf_Internal_Phdr structures, as | |
12017 | defined in include/elf/internal.h. To find out how large the | |
12018 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
12019 | ||
12020 | Return the number of program header table entries read, or -1 if an | |
12021 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 12022 | |
98d8431c | 12023 | int |
217aa764 | 12024 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
12025 | { |
12026 | int num_phdrs; | |
12027 | ||
12028 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
12029 | { | |
12030 | bfd_set_error (bfd_error_wrong_format); | |
12031 | return -1; | |
12032 | } | |
12033 | ||
12034 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
01bcaf63 TT |
12035 | if (num_phdrs != 0) |
12036 | memcpy (phdrs, elf_tdata (abfd)->phdr, | |
12037 | num_phdrs * sizeof (Elf_Internal_Phdr)); | |
98d8431c JB |
12038 | |
12039 | return num_phdrs; | |
12040 | } | |
ae4221d7 | 12041 | |
db6751f2 | 12042 | enum elf_reloc_type_class |
7e612e98 AM |
12043 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
12044 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
12045 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
12046 | { |
12047 | return reloc_class_normal; | |
12048 | } | |
f8df10f4 | 12049 | |
47d9a591 | 12050 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
12051 | relocation against a local symbol. */ |
12052 | ||
12053 | bfd_vma | |
217aa764 AM |
12054 | _bfd_elf_rela_local_sym (bfd *abfd, |
12055 | Elf_Internal_Sym *sym, | |
8517fae7 | 12056 | asection **psec, |
217aa764 | 12057 | Elf_Internal_Rela *rel) |
f8df10f4 | 12058 | { |
8517fae7 | 12059 | asection *sec = *psec; |
f8df10f4 JJ |
12060 | bfd_vma relocation; |
12061 | ||
6835821b AM |
12062 | relocation = (sec->output_section->vma |
12063 | + sec->output_offset | |
12064 | + sym->st_value); | |
f8df10f4 | 12065 | if ((sec->flags & SEC_MERGE) |
c629eae0 | 12066 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 12067 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 12068 | { |
f8df10f4 | 12069 | rel->r_addend = |
8517fae7 | 12070 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 12071 | elf_section_data (sec)->sec_info, |
753731ee AM |
12072 | sym->st_value + rel->r_addend); |
12073 | if (sec != *psec) | |
12074 | { | |
12075 | /* If we have changed the section, and our original section is | |
12076 | marked with SEC_EXCLUDE, it means that the original | |
12077 | SEC_MERGE section has been completely subsumed in some | |
12078 | other SEC_MERGE section. In this case, we need to leave | |
12079 | some info around for --emit-relocs. */ | |
12080 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
12081 | sec->kept_section = *psec; | |
12082 | sec = *psec; | |
12083 | } | |
8517fae7 AM |
12084 | rel->r_addend -= relocation; |
12085 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
12086 | } |
12087 | return relocation; | |
12088 | } | |
c629eae0 JJ |
12089 | |
12090 | bfd_vma | |
217aa764 AM |
12091 | _bfd_elf_rel_local_sym (bfd *abfd, |
12092 | Elf_Internal_Sym *sym, | |
12093 | asection **psec, | |
12094 | bfd_vma addend) | |
47d9a591 | 12095 | { |
c629eae0 JJ |
12096 | asection *sec = *psec; |
12097 | ||
6835821b | 12098 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
12099 | return sym->st_value + addend; |
12100 | ||
12101 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 12102 | elf_section_data (sec)->sec_info, |
753731ee | 12103 | sym->st_value + addend); |
c629eae0 JJ |
12104 | } |
12105 | ||
37b01f6a DG |
12106 | /* Adjust an address within a section. Given OFFSET within SEC, return |
12107 | the new offset within the section, based upon changes made to the | |
12108 | section. Returns -1 if the offset is now invalid. | |
12109 | The offset (in abnd out) is in target sized bytes, however big a | |
12110 | byte may be. */ | |
12111 | ||
c629eae0 | 12112 | bfd_vma |
217aa764 | 12113 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 12114 | struct bfd_link_info *info, |
217aa764 AM |
12115 | asection *sec, |
12116 | bfd_vma offset) | |
c629eae0 | 12117 | { |
68bfbfcc | 12118 | switch (sec->sec_info_type) |
65765700 | 12119 | { |
dbaa2011 | 12120 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
12121 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
12122 | offset); | |
dbaa2011 | 12123 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 12124 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
37b01f6a | 12125 | |
65765700 | 12126 | default: |
310fd250 L |
12127 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
12128 | { | |
37b01f6a | 12129 | /* Reverse the offset. */ |
310fd250 L |
12130 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
12131 | bfd_size_type address_size = bed->s->arch_size / 8; | |
37b01f6a DG |
12132 | |
12133 | /* address_size and sec->size are in octets. Convert | |
12134 | to bytes before subtracting the original offset. */ | |
61826503 | 12135 | offset = ((sec->size - address_size) |
bb294208 | 12136 | / bfd_octets_per_byte (abfd, sec) - offset); |
310fd250 | 12137 | } |
65765700 JJ |
12138 | return offset; |
12139 | } | |
c629eae0 | 12140 | } |
3333a7c3 RM |
12141 | \f |
12142 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
12143 | reconstruct an ELF file by reading the segments out of remote memory | |
12144 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
12145 | points to. If not null, *LOADBASEP is filled in with the difference | |
12146 | between the VMAs from which the segments were read, and the VMAs the | |
12147 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
12148 | ||
12149 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
12150 | remote memory at target address VMA into the local buffer at MYADDR; it | |
12151 | should return zero on success or an `errno' code on failure. TEMPL must | |
12152 | be a BFD for an ELF target with the word size and byte order found in | |
12153 | the remote memory. */ | |
12154 | ||
12155 | bfd * | |
217aa764 AM |
12156 | bfd_elf_bfd_from_remote_memory |
12157 | (bfd *templ, | |
12158 | bfd_vma ehdr_vma, | |
f0a5d95a | 12159 | bfd_size_type size, |
217aa764 | 12160 | bfd_vma *loadbasep, |
fe78531d | 12161 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
12162 | { |
12163 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 12164 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 12165 | } |
4c45e5c9 JJ |
12166 | \f |
12167 | long | |
c9727e01 AM |
12168 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
12169 | long symcount ATTRIBUTE_UNUSED, | |
12170 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 12171 | long dynsymcount, |
c9727e01 AM |
12172 | asymbol **dynsyms, |
12173 | asymbol **ret) | |
4c45e5c9 JJ |
12174 | { |
12175 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
12176 | asection *relplt; | |
12177 | asymbol *s; | |
12178 | const char *relplt_name; | |
12179 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
12180 | arelent *p; | |
12181 | long count, i, n; | |
12182 | size_t size; | |
12183 | Elf_Internal_Shdr *hdr; | |
12184 | char *names; | |
12185 | asection *plt; | |
12186 | ||
8615f3f2 AM |
12187 | *ret = NULL; |
12188 | ||
90e3cdf2 JJ |
12189 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
12190 | return 0; | |
12191 | ||
8615f3f2 AM |
12192 | if (dynsymcount <= 0) |
12193 | return 0; | |
12194 | ||
4c45e5c9 JJ |
12195 | if (!bed->plt_sym_val) |
12196 | return 0; | |
12197 | ||
12198 | relplt_name = bed->relplt_name; | |
12199 | if (relplt_name == NULL) | |
d35fd659 | 12200 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
12201 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
12202 | if (relplt == NULL) | |
12203 | return 0; | |
12204 | ||
12205 | hdr = &elf_section_data (relplt)->this_hdr; | |
12206 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
12207 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
12208 | return 0; | |
12209 | ||
12210 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
12211 | if (plt == NULL) | |
12212 | return 0; | |
12213 | ||
12214 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 12215 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
12216 | return -1; |
12217 | ||
eea6121a | 12218 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
12219 | size = count * sizeof (asymbol); |
12220 | p = relplt->relocation; | |
cb53bf42 | 12221 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
12222 | { |
12223 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
12224 | if (p->addend != 0) | |
12225 | { | |
12226 | #ifdef BFD64 | |
12227 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
12228 | #else | |
12229 | size += sizeof ("+0x") - 1 + 8; | |
12230 | #endif | |
12231 | } | |
12232 | } | |
4c45e5c9 | 12233 | |
a50b1753 | 12234 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
12235 | if (s == NULL) |
12236 | return -1; | |
12237 | ||
12238 | names = (char *) (s + count); | |
12239 | p = relplt->relocation; | |
12240 | n = 0; | |
cb53bf42 | 12241 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
12242 | { |
12243 | size_t len; | |
12244 | bfd_vma addr; | |
12245 | ||
12246 | addr = bed->plt_sym_val (i, plt, p); | |
12247 | if (addr == (bfd_vma) -1) | |
12248 | continue; | |
12249 | ||
12250 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
12251 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
12252 | we are defining a symbol, ensure one of them is set. */ | |
12253 | if ((s->flags & BSF_LOCAL) == 0) | |
12254 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 12255 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
12256 | s->section = plt; |
12257 | s->value = addr - plt->vma; | |
12258 | s->name = names; | |
8f39ba8e | 12259 | s->udata.p = NULL; |
4c45e5c9 JJ |
12260 | len = strlen ((*p->sym_ptr_ptr)->name); |
12261 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
12262 | names += len; | |
041de40d AM |
12263 | if (p->addend != 0) |
12264 | { | |
1d770845 | 12265 | char buf[30], *a; |
d324f6d6 | 12266 | |
041de40d AM |
12267 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
12268 | names += sizeof ("+0x") - 1; | |
1d770845 L |
12269 | bfd_sprintf_vma (abfd, buf, p->addend); |
12270 | for (a = buf; *a == '0'; ++a) | |
12271 | ; | |
12272 | len = strlen (a); | |
12273 | memcpy (names, a, len); | |
12274 | names += len; | |
041de40d | 12275 | } |
4c45e5c9 JJ |
12276 | memcpy (names, "@plt", sizeof ("@plt")); |
12277 | names += sizeof ("@plt"); | |
8f39ba8e | 12278 | ++s, ++n; |
4c45e5c9 JJ |
12279 | } |
12280 | ||
12281 | return n; | |
12282 | } | |
3d7f7666 | 12283 | |
821e6ff6 AM |
12284 | /* It is only used by x86-64 so far. |
12285 | ??? This repeats *COM* id of zero. sec->id is supposed to be unique, | |
7eacd66b AM |
12286 | but current usage would allow all of _bfd_std_section to be zero. */ |
12287 | static const asymbol lcomm_sym | |
12288 | = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section); | |
3b22753a | 12289 | asection _bfd_elf_large_com_section |
7eacd66b | 12290 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym, |
821e6ff6 | 12291 | "LARGE_COMMON", 0, SEC_IS_COMMON); |
ecca9871 | 12292 | |
cc364be6 AM |
12293 | bfd_boolean |
12294 | _bfd_elf_final_write_processing (bfd *abfd) | |
06f44071 AM |
12295 | { |
12296 | Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */ | |
d1036acb L |
12297 | |
12298 | i_ehdrp = elf_elfheader (abfd); | |
12299 | ||
06f44071 AM |
12300 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) |
12301 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 | 12302 | |
df3a023b AM |
12303 | /* Set the osabi field to ELFOSABI_GNU if the binary contains |
12304 | SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or | |
12305 | STB_GNU_UNIQUE binding. */ | |
cc364be6 AM |
12306 | if (elf_tdata (abfd)->has_gnu_osabi != 0) |
12307 | { | |
12308 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) | |
12309 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; | |
12310 | else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU | |
12311 | && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD) | |
12312 | { | |
12313 | if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) | |
12314 | _bfd_error_handler (_("GNU_MBIND section is unsupported")); | |
12315 | if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc) | |
12316 | _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported")); | |
12317 | if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique) | |
12318 | _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported")); | |
9aea1e31 | 12319 | bfd_set_error (bfd_error_sorry); |
cc364be6 AM |
12320 | return FALSE; |
12321 | } | |
12322 | } | |
12323 | return TRUE; | |
d1036acb | 12324 | } |
fcb93ecf PB |
12325 | |
12326 | ||
12327 | /* Return TRUE for ELF symbol types that represent functions. | |
12328 | This is the default version of this function, which is sufficient for | |
d8045f23 | 12329 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
12330 | |
12331 | bfd_boolean | |
12332 | _bfd_elf_is_function_type (unsigned int type) | |
12333 | { | |
d8045f23 NC |
12334 | return (type == STT_FUNC |
12335 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 12336 | } |
9f296da3 | 12337 | |
aef36ac1 AM |
12338 | /* If the ELF symbol SYM might be a function in SEC, return the |
12339 | function size and set *CODE_OFF to the function's entry point, | |
12340 | otherwise return zero. */ | |
9f296da3 | 12341 | |
aef36ac1 AM |
12342 | bfd_size_type |
12343 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
12344 | bfd_vma *code_off) | |
9f296da3 | 12345 | { |
aef36ac1 AM |
12346 | bfd_size_type size; |
12347 | ||
ff9e0f5b | 12348 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
12349 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
12350 | || sym->section != sec) | |
12351 | return 0; | |
ff9e0f5b | 12352 | |
ff9e0f5b | 12353 | *code_off = sym->value; |
aef36ac1 AM |
12354 | size = 0; |
12355 | if (!(sym->flags & BSF_SYNTHETIC)) | |
12356 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
12357 | if (size == 0) | |
12358 | size = 1; | |
12359 | return size; | |
9f296da3 | 12360 | } |
a8e14f4c NC |
12361 | |
12362 | /* Set to non-zero to enable some debug messages. */ | |
12363 | #define DEBUG_SECONDARY_RELOCS 0 | |
12364 | ||
12365 | /* An internal-to-the-bfd-library only section type | |
12366 | used to indicate a cached secondary reloc section. */ | |
12367 | #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA) | |
12368 | ||
12369 | /* Create a BFD section to hold a secondary reloc section. */ | |
12370 | ||
12371 | bfd_boolean | |
12372 | _bfd_elf_init_secondary_reloc_section (bfd * abfd, | |
12373 | Elf_Internal_Shdr *hdr, | |
12374 | const char * name, | |
12375 | unsigned int shindex) | |
12376 | { | |
12377 | /* We only support RELA secondary relocs. */ | |
12378 | if (hdr->sh_type != SHT_RELA) | |
12379 | return FALSE; | |
12380 | ||
12381 | #if DEBUG_SECONDARY_RELOCS | |
12382 | fprintf (stderr, "secondary reloc section %s encountered\n", name); | |
12383 | #endif | |
12384 | hdr->sh_type = SHT_SECONDARY_RELOC; | |
12385 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
12386 | } | |
12387 | ||
12388 | /* Read in any secondary relocs associated with SEC. */ | |
12389 | ||
12390 | bfd_boolean | |
12391 | _bfd_elf_slurp_secondary_reloc_section (bfd * abfd, | |
12392 | asection * sec, | |
12393 | asymbol ** symbols) | |
12394 | { | |
12395 | const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); | |
12396 | asection * relsec; | |
12397 | bfd_boolean result = TRUE; | |
12398 | bfd_vma (*r_sym) (bfd_vma); | |
12399 | ||
12400 | #if BFD_DEFAULT_TARGET_SIZE > 32 | |
12401 | if (bfd_arch_bits_per_address (abfd) != 32) | |
12402 | r_sym = elf64_r_sym; | |
12403 | else | |
12404 | #endif | |
12405 | r_sym = elf32_r_sym; | |
12406 | ||
12407 | /* Discover if there are any secondary reloc sections | |
12408 | associated with SEC. */ | |
12409 | for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) | |
12410 | { | |
12411 | Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr; | |
12412 | ||
12413 | if (hdr->sh_type == SHT_SECONDARY_RELOC | |
12414 | && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx) | |
12415 | { | |
12416 | bfd_byte * native_relocs; | |
12417 | bfd_byte * native_reloc; | |
12418 | arelent * internal_relocs; | |
12419 | arelent * internal_reloc; | |
12420 | unsigned int i; | |
12421 | unsigned int entsize; | |
12422 | unsigned int symcount; | |
12423 | unsigned int reloc_count; | |
12424 | size_t amt; | |
12425 | ||
12426 | if (ebd->elf_info_to_howto == NULL) | |
12427 | return FALSE; | |
12428 | ||
12429 | #if DEBUG_SECONDARY_RELOCS | |
12430 | fprintf (stderr, "read secondary relocs for %s from %s\n", | |
12431 | sec->name, relsec->name); | |
12432 | #endif | |
12433 | entsize = hdr->sh_entsize; | |
12434 | ||
12435 | native_relocs = bfd_malloc (hdr->sh_size); | |
12436 | if (native_relocs == NULL) | |
12437 | { | |
12438 | result = FALSE; | |
12439 | continue; | |
12440 | } | |
12441 | ||
12442 | reloc_count = NUM_SHDR_ENTRIES (hdr); | |
12443 | if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt)) | |
12444 | { | |
12445 | bfd_set_error (bfd_error_file_too_big); | |
12446 | result = FALSE; | |
12447 | continue; | |
12448 | } | |
12449 | ||
12450 | internal_relocs = (arelent *) bfd_alloc (abfd, amt); | |
12451 | if (internal_relocs == NULL) | |
12452 | { | |
12453 | free (native_relocs); | |
12454 | result = FALSE; | |
12455 | continue; | |
12456 | } | |
12457 | ||
12458 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
12459 | || (bfd_bread (native_relocs, hdr->sh_size, abfd) | |
12460 | != hdr->sh_size)) | |
12461 | { | |
12462 | free (native_relocs); | |
12463 | free (internal_relocs); | |
12464 | result = FALSE; | |
12465 | continue; | |
12466 | } | |
12467 | ||
12468 | symcount = bfd_get_symcount (abfd); | |
12469 | ||
12470 | for (i = 0, internal_reloc = internal_relocs, | |
12471 | native_reloc = native_relocs; | |
12472 | i < reloc_count; | |
12473 | i++, internal_reloc++, native_reloc += entsize) | |
12474 | { | |
12475 | bfd_boolean res; | |
12476 | Elf_Internal_Rela rela; | |
12477 | ||
12478 | ebd->s->swap_reloca_in (abfd, native_reloc, & rela); | |
12479 | ||
12480 | /* The address of an ELF reloc is section relative for an object | |
12481 | file, and absolute for an executable file or shared library. | |
12482 | The address of a normal BFD reloc is always section relative, | |
12483 | and the address of a dynamic reloc is absolute.. */ | |
12484 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) | |
12485 | internal_reloc->address = rela.r_offset; | |
12486 | else | |
12487 | internal_reloc->address = rela.r_offset - sec->vma; | |
12488 | ||
12489 | if (r_sym (rela.r_info) == STN_UNDEF) | |
12490 | { | |
12491 | /* FIXME: This and the error case below mean that we | |
12492 | have a symbol on relocs that is not elf_symbol_type. */ | |
12493 | internal_reloc->sym_ptr_ptr = | |
12494 | bfd_abs_section_ptr->symbol_ptr_ptr; | |
12495 | } | |
12496 | else if (r_sym (rela.r_info) > symcount) | |
12497 | { | |
12498 | _bfd_error_handler | |
12499 | /* xgettext:c-format */ | |
12500 | (_("%pB(%pA): relocation %d has invalid symbol index %ld"), | |
12501 | abfd, sec, i, (long) r_sym (rela.r_info)); | |
12502 | bfd_set_error (bfd_error_bad_value); | |
12503 | internal_reloc->sym_ptr_ptr = | |
12504 | bfd_abs_section_ptr->symbol_ptr_ptr; | |
12505 | result = FALSE; | |
12506 | } | |
12507 | else | |
12508 | { | |
12509 | asymbol **ps; | |
12510 | ||
12511 | ps = symbols + r_sym (rela.r_info) - 1; | |
12512 | ||
12513 | internal_reloc->sym_ptr_ptr = ps; | |
12514 | /* Make sure that this symbol is not removed by strip. */ | |
12515 | (*ps)->flags |= BSF_KEEP; | |
12516 | } | |
12517 | ||
12518 | internal_reloc->addend = rela.r_addend; | |
12519 | ||
12520 | res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela); | |
12521 | if (! res || internal_reloc->howto == NULL) | |
12522 | { | |
12523 | #if DEBUG_SECONDARY_RELOCS | |
12524 | fprintf (stderr, "there is no howto associated with reloc %lx\n", | |
12525 | rela.r_info); | |
12526 | #endif | |
12527 | result = FALSE; | |
12528 | } | |
12529 | } | |
12530 | ||
12531 | free (native_relocs); | |
12532 | /* Store the internal relocs. */ | |
12533 | elf_section_data (relsec)->sec_info = internal_relocs; | |
12534 | } | |
12535 | } | |
12536 | ||
12537 | return result; | |
12538 | } | |
12539 | ||
12540 | /* Set the ELF section header fields of an output secondary reloc section. */ | |
12541 | ||
12542 | bfd_boolean | |
12543 | _bfd_elf_copy_special_section_fields (const bfd * ibfd ATTRIBUTE_UNUSED, | |
12544 | bfd * obfd ATTRIBUTE_UNUSED, | |
12545 | const Elf_Internal_Shdr * isection, | |
12546 | Elf_Internal_Shdr * osection) | |
12547 | { | |
12548 | asection * isec; | |
12549 | asection * osec; | |
12550 | ||
12551 | if (isection == NULL) | |
12552 | return FALSE; | |
12553 | ||
12554 | if (isection->sh_type != SHT_SECONDARY_RELOC) | |
12555 | return TRUE; | |
12556 | ||
12557 | isec = isection->bfd_section; | |
12558 | if (isec == NULL) | |
12559 | return FALSE; | |
12560 | ||
12561 | osec = osection->bfd_section; | |
12562 | if (osec == NULL) | |
12563 | return FALSE; | |
12564 | ||
12565 | BFD_ASSERT (elf_section_data (osec)->sec_info == NULL); | |
12566 | elf_section_data (osec)->sec_info = elf_section_data (isec)->sec_info; | |
12567 | osection->sh_type = SHT_RELA; | |
12568 | osection->sh_link = elf_onesymtab (obfd); | |
12569 | if (osection->sh_link == 0) | |
12570 | { | |
12571 | /* There is no symbol table - we are hosed... */ | |
12572 | _bfd_error_handler | |
12573 | /* xgettext:c-format */ | |
12574 | (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"), | |
12575 | obfd, osec); | |
12576 | bfd_set_error (bfd_error_bad_value); | |
12577 | return FALSE; | |
12578 | } | |
12579 | ||
12580 | /* Find the output section that corresponds to the isection's sh_info link. */ | |
12581 | BFD_ASSERT (isection->sh_info > 0 | |
12582 | && isection->sh_info < elf_numsections (ibfd)); | |
12583 | isection = elf_elfsections (ibfd)[isection->sh_info]; | |
12584 | ||
12585 | BFD_ASSERT (isection != NULL); | |
12586 | BFD_ASSERT (isection->bfd_section != NULL); | |
12587 | BFD_ASSERT (isection->bfd_section->output_section != NULL); | |
12588 | osection->sh_info = | |
12589 | elf_section_data (isection->bfd_section->output_section)->this_idx; | |
12590 | ||
12591 | #if DEBUG_SECONDARY_RELOCS | |
12592 | fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n", | |
12593 | osec->name, osection->sh_link, osection->sh_info); | |
12594 | #endif | |
12595 | ||
12596 | return TRUE; | |
12597 | } | |
12598 | ||
12599 | /* Write out a secondary reloc section. */ | |
12600 | ||
12601 | bfd_boolean | |
12602 | _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec) | |
12603 | { | |
12604 | const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); | |
12605 | bfd_vma addr_offset; | |
12606 | asection * relsec; | |
12607 | bfd_vma (*r_info) (bfd_vma, bfd_vma); | |
12608 | ||
12609 | #if BFD_DEFAULT_TARGET_SIZE > 32 | |
12610 | if (bfd_arch_bits_per_address (abfd) != 32) | |
12611 | r_info = elf64_r_info; | |
12612 | else | |
12613 | #endif | |
12614 | r_info = elf32_r_info; | |
12615 | ||
12616 | if (sec == NULL) | |
12617 | return FALSE; | |
12618 | ||
12619 | /* The address of an ELF reloc is section relative for an object | |
12620 | file, and absolute for an executable file or shared library. | |
12621 | The address of a BFD reloc is always section relative. */ | |
12622 | addr_offset = 0; | |
12623 | if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) | |
12624 | addr_offset = sec->vma; | |
12625 | ||
12626 | /* Discover if there are any secondary reloc sections | |
12627 | associated with SEC. */ | |
12628 | for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) | |
12629 | { | |
12630 | const struct bfd_elf_section_data * const esd = elf_section_data (relsec); | |
12631 | Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr; | |
12632 | ||
12633 | if (hdr->sh_type == SHT_RELA | |
12634 | && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx) | |
12635 | { | |
12636 | asymbol * last_sym; | |
12637 | int last_sym_idx; | |
12638 | unsigned int reloc_count; | |
12639 | unsigned int idx; | |
12640 | arelent * src_irel; | |
12641 | bfd_byte * dst_rela; | |
12642 | ||
12643 | BFD_ASSERT (hdr->contents == NULL); | |
12644 | ||
12645 | reloc_count = hdr->sh_size / hdr->sh_entsize; | |
12646 | BFD_ASSERT (reloc_count > 0); | |
12647 | ||
12648 | hdr->contents = bfd_alloc (abfd, hdr->sh_size); | |
12649 | if (hdr->contents == NULL) | |
12650 | continue; | |
12651 | ||
12652 | #if DEBUG_SECONDARY_RELOCS | |
12653 | fprintf (stderr, "write %u secondary relocs for %s from %s\n", | |
12654 | reloc_count, sec->name, relsec->name); | |
12655 | #endif | |
12656 | last_sym = NULL; | |
12657 | last_sym_idx = 0; | |
12658 | dst_rela = hdr->contents; | |
12659 | src_irel = (arelent *) esd->sec_info; | |
12660 | BFD_ASSERT (src_irel != NULL); | |
12661 | ||
12662 | for (idx = 0; idx < reloc_count; idx++, dst_rela += hdr->sh_entsize) | |
12663 | { | |
12664 | Elf_Internal_Rela src_rela; | |
12665 | arelent *ptr; | |
12666 | asymbol *sym; | |
12667 | int n; | |
12668 | ||
12669 | ptr = src_irel + idx; | |
12670 | sym = *ptr->sym_ptr_ptr; | |
12671 | ||
12672 | if (sym == last_sym) | |
12673 | n = last_sym_idx; | |
12674 | else | |
12675 | { | |
12676 | last_sym = sym; | |
12677 | n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym); | |
12678 | if (n < 0) | |
12679 | { | |
12680 | #if DEBUG_SECONDARY_RELOCS | |
12681 | fprintf (stderr, "failed to find symbol %s whilst rewriting relocs\n", | |
12682 | sym->name); | |
12683 | #endif | |
12684 | /* FIXME: Signal failure somehow. */ | |
12685 | n = 0; | |
12686 | } | |
12687 | last_sym_idx = n; | |
12688 | } | |
12689 | ||
12690 | if ((*ptr->sym_ptr_ptr)->the_bfd != NULL | |
12691 | && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec | |
12692 | && ! _bfd_elf_validate_reloc (abfd, ptr)) | |
12693 | { | |
12694 | #if DEBUG_SECONDARY_RELOCS | |
12695 | fprintf (stderr, "symbol %s is not in the output bfd\n", | |
12696 | sym->name); | |
12697 | #endif | |
12698 | /* FIXME: Signal failure somehow. */ | |
12699 | n = 0; | |
12700 | } | |
12701 | ||
12702 | if (ptr->howto == NULL) | |
12703 | { | |
12704 | #if DEBUG_SECONDARY_RELOCS | |
12705 | fprintf (stderr, "reloc for symbol %s does not have a howto associated with it\n", | |
12706 | sym->name); | |
12707 | #endif | |
12708 | /* FIXME: Signal failure somehow. */ | |
12709 | n = 0; | |
12710 | } | |
12711 | ||
12712 | src_rela.r_offset = ptr->address + addr_offset; | |
12713 | src_rela.r_info = r_info (n, ptr->howto->type); | |
12714 | src_rela.r_addend = ptr->addend; | |
12715 | ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela); | |
12716 | } | |
12717 | } | |
12718 | } | |
12719 | ||
12720 | return TRUE; | |
12721 | } |