Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 | 2 | |
b3adc24a | 3 | Copyright (C) 1993-2020 Free Software Foundation, Inc. |
252b5132 | 4 | |
5e8d7549 | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
5e8d7549 NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
5e8d7549 | 10 | (at your option) any later version. |
252b5132 | 11 | |
5e8d7549 NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
5e8d7549 | 17 | You should have received a copy of the GNU General Public License |
b34976b6 | 18 | along with this program; if not, write to the Free Software |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
21 | ||
252b5132 | 22 | |
1b74d094 BW |
23 | /* |
24 | SECTION | |
252b5132 RH |
25 | ELF backends |
26 | ||
27 | BFD support for ELF formats is being worked on. | |
28 | Currently, the best supported back ends are for sparc and i386 | |
29 | (running svr4 or Solaris 2). | |
30 | ||
31 | Documentation of the internals of the support code still needs | |
32 | to be written. The code is changing quickly enough that we | |
661a3fd4 | 33 | haven't bothered yet. */ |
252b5132 | 34 | |
7ee38065 MS |
35 | /* For sparc64-cross-sparc32. */ |
36 | #define _SYSCALL32 | |
252b5132 | 37 | #include "sysdep.h" |
3a551c7a | 38 | #include <limits.h> |
3db64b00 | 39 | #include "bfd.h" |
252b5132 RH |
40 | #include "bfdlink.h" |
41 | #include "libbfd.h" | |
42 | #define ARCH_SIZE 0 | |
43 | #include "elf-bfd.h" | |
e0e8c97f | 44 | #include "libiberty.h" |
ff59fc36 | 45 | #include "safe-ctype.h" |
de64ce13 | 46 | #include "elf-linux-core.h" |
252b5132 | 47 | |
8bc7f138 L |
48 | #ifdef CORE_HEADER |
49 | #include CORE_HEADER | |
50 | #endif | |
51 | ||
217aa764 | 52 | static int elf_sort_sections (const void *, const void *); |
c84fca4d | 53 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); |
ef10c3ac | 54 | static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ; |
718175fa | 55 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, |
276da9b3 | 56 | file_ptr offset, size_t align); |
50b2bdb7 | 57 | |
252b5132 RH |
58 | /* Swap version information in and out. The version information is |
59 | currently size independent. If that ever changes, this code will | |
60 | need to move into elfcode.h. */ | |
61 | ||
62 | /* Swap in a Verdef structure. */ | |
63 | ||
64 | void | |
217aa764 AM |
65 | _bfd_elf_swap_verdef_in (bfd *abfd, |
66 | const Elf_External_Verdef *src, | |
67 | Elf_Internal_Verdef *dst) | |
252b5132 | 68 | { |
dc810e39 AM |
69 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
70 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
71 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
72 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
73 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
74 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
75 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
76 | } |
77 | ||
78 | /* Swap out a Verdef structure. */ | |
79 | ||
80 | void | |
217aa764 AM |
81 | _bfd_elf_swap_verdef_out (bfd *abfd, |
82 | const Elf_Internal_Verdef *src, | |
83 | Elf_External_Verdef *dst) | |
252b5132 | 84 | { |
dc810e39 AM |
85 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
86 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
87 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
88 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
89 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
90 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
91 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
92 | } |
93 | ||
94 | /* Swap in a Verdaux structure. */ | |
95 | ||
96 | void | |
217aa764 AM |
97 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
98 | const Elf_External_Verdaux *src, | |
99 | Elf_Internal_Verdaux *dst) | |
252b5132 | 100 | { |
dc810e39 AM |
101 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
102 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
103 | } |
104 | ||
105 | /* Swap out a Verdaux structure. */ | |
106 | ||
107 | void | |
217aa764 AM |
108 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
109 | const Elf_Internal_Verdaux *src, | |
110 | Elf_External_Verdaux *dst) | |
252b5132 | 111 | { |
dc810e39 AM |
112 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
113 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
114 | } |
115 | ||
116 | /* Swap in a Verneed structure. */ | |
117 | ||
118 | void | |
217aa764 AM |
119 | _bfd_elf_swap_verneed_in (bfd *abfd, |
120 | const Elf_External_Verneed *src, | |
121 | Elf_Internal_Verneed *dst) | |
252b5132 | 122 | { |
dc810e39 AM |
123 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
124 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
125 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
126 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
127 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
128 | } |
129 | ||
130 | /* Swap out a Verneed structure. */ | |
131 | ||
132 | void | |
217aa764 AM |
133 | _bfd_elf_swap_verneed_out (bfd *abfd, |
134 | const Elf_Internal_Verneed *src, | |
135 | Elf_External_Verneed *dst) | |
252b5132 | 136 | { |
dc810e39 AM |
137 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
138 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
139 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
140 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
141 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
142 | } |
143 | ||
144 | /* Swap in a Vernaux structure. */ | |
145 | ||
146 | void | |
217aa764 AM |
147 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
148 | const Elf_External_Vernaux *src, | |
149 | Elf_Internal_Vernaux *dst) | |
252b5132 | 150 | { |
dc810e39 AM |
151 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
152 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
153 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
154 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
155 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
156 | } |
157 | ||
158 | /* Swap out a Vernaux structure. */ | |
159 | ||
160 | void | |
217aa764 AM |
161 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
162 | const Elf_Internal_Vernaux *src, | |
163 | Elf_External_Vernaux *dst) | |
252b5132 | 164 | { |
dc810e39 AM |
165 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
166 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
167 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
168 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
169 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
170 | } |
171 | ||
172 | /* Swap in a Versym structure. */ | |
173 | ||
174 | void | |
217aa764 AM |
175 | _bfd_elf_swap_versym_in (bfd *abfd, |
176 | const Elf_External_Versym *src, | |
177 | Elf_Internal_Versym *dst) | |
252b5132 | 178 | { |
dc810e39 | 179 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
180 | } |
181 | ||
182 | /* Swap out a Versym structure. */ | |
183 | ||
184 | void | |
217aa764 AM |
185 | _bfd_elf_swap_versym_out (bfd *abfd, |
186 | const Elf_Internal_Versym *src, | |
187 | Elf_External_Versym *dst) | |
252b5132 | 188 | { |
dc810e39 | 189 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
190 | } |
191 | ||
192 | /* Standard ELF hash function. Do not change this function; you will | |
193 | cause invalid hash tables to be generated. */ | |
3a99b017 | 194 | |
252b5132 | 195 | unsigned long |
217aa764 | 196 | bfd_elf_hash (const char *namearg) |
252b5132 | 197 | { |
3a99b017 | 198 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
199 | unsigned long h = 0; |
200 | unsigned long g; | |
201 | int ch; | |
202 | ||
203 | while ((ch = *name++) != '\0') | |
204 | { | |
205 | h = (h << 4) + ch; | |
206 | if ((g = (h & 0xf0000000)) != 0) | |
207 | { | |
208 | h ^= g >> 24; | |
209 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
210 | this case and on some machines one insn instead of two. */ | |
211 | h ^= g; | |
212 | } | |
213 | } | |
32dfa85d | 214 | return h & 0xffffffff; |
252b5132 RH |
215 | } |
216 | ||
fdc90cb4 JJ |
217 | /* DT_GNU_HASH hash function. Do not change this function; you will |
218 | cause invalid hash tables to be generated. */ | |
219 | ||
220 | unsigned long | |
221 | bfd_elf_gnu_hash (const char *namearg) | |
222 | { | |
223 | const unsigned char *name = (const unsigned char *) namearg; | |
224 | unsigned long h = 5381; | |
225 | unsigned char ch; | |
226 | ||
227 | while ((ch = *name++) != '\0') | |
228 | h = (h << 5) + h + ch; | |
229 | return h & 0xffffffff; | |
230 | } | |
231 | ||
0c8d6e5c AM |
232 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
233 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 234 | bfd_boolean |
0c8d6e5c | 235 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd | 236 | size_t object_size, |
4dfe6ac6 | 237 | enum elf_target_id object_id) |
252b5132 | 238 | { |
0ffa91dd NC |
239 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
240 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
241 | if (abfd->tdata.any == NULL) | |
242 | return FALSE; | |
252b5132 | 243 | |
0ffa91dd | 244 | elf_object_id (abfd) = object_id; |
c0355132 AM |
245 | if (abfd->direction != read_direction) |
246 | { | |
247 | struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); | |
248 | if (o == NULL) | |
249 | return FALSE; | |
250 | elf_tdata (abfd)->o = o; | |
251 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
252 | } | |
b34976b6 | 253 | return TRUE; |
252b5132 RH |
254 | } |
255 | ||
0ffa91dd NC |
256 | |
257 | bfd_boolean | |
ae95ffa6 | 258 | bfd_elf_make_object (bfd *abfd) |
0ffa91dd | 259 | { |
ae95ffa6 | 260 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
0ffa91dd | 261 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), |
ae95ffa6 | 262 | bed->target_id); |
0ffa91dd NC |
263 | } |
264 | ||
b34976b6 | 265 | bfd_boolean |
217aa764 | 266 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 267 | { |
c044fabd | 268 | /* I think this can be done just like an object file. */ |
228e534f AM |
269 | if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) |
270 | return FALSE; | |
271 | elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); | |
272 | return elf_tdata (abfd)->core != NULL; | |
252b5132 RH |
273 | } |
274 | ||
6d5944fc | 275 | char * |
217aa764 | 276 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
277 | { |
278 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 279 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
280 | file_ptr offset; |
281 | bfd_size_type shstrtabsize; | |
252b5132 RH |
282 | |
283 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
284 | if (i_shdrp == 0 |
285 | || shindex >= elf_numsections (abfd) | |
286 | || i_shdrp[shindex] == 0) | |
f075ee0c | 287 | return NULL; |
252b5132 | 288 | |
f075ee0c | 289 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
290 | if (shstrtab == NULL) |
291 | { | |
c044fabd | 292 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
293 | offset = i_shdrp[shindex]->sh_offset; |
294 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
295 | |
296 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
297 | in case the string table is not terminated. */ | |
3471d59d | 298 | if (shstrtabsize + 1 <= 1 |
06614111 | 299 | || bfd_seek (abfd, offset, SEEK_SET) != 0 |
2bb3687b AM |
300 | || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1, |
301 | shstrtabsize)) == NULL) | |
302 | { | |
3471d59d CC |
303 | /* Once we've failed to read it, make sure we don't keep |
304 | trying. Otherwise, we'll keep allocating space for | |
305 | the string table over and over. */ | |
306 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
307 | } |
308 | else | |
309 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 310 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 311 | } |
f075ee0c | 312 | return (char *) shstrtab; |
252b5132 RH |
313 | } |
314 | ||
315 | char * | |
217aa764 AM |
316 | bfd_elf_string_from_elf_section (bfd *abfd, |
317 | unsigned int shindex, | |
318 | unsigned int strindex) | |
252b5132 RH |
319 | { |
320 | Elf_Internal_Shdr *hdr; | |
321 | ||
322 | if (strindex == 0) | |
323 | return ""; | |
324 | ||
74f2e02b AM |
325 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
326 | return NULL; | |
327 | ||
252b5132 RH |
328 | hdr = elf_elfsections (abfd)[shindex]; |
329 | ||
06614111 NC |
330 | if (hdr->contents == NULL) |
331 | { | |
332 | if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) | |
333 | { | |
334 | /* PR 17512: file: f057ec89. */ | |
695344c0 | 335 | /* xgettext:c-format */ |
871b3ab2 | 336 | _bfd_error_handler (_("%pB: attempt to load strings from" |
63a5468a | 337 | " a non-string section (number %d)"), |
06614111 NC |
338 | abfd, shindex); |
339 | return NULL; | |
340 | } | |
b1fa9dd6 | 341 | |
06614111 NC |
342 | if (bfd_elf_get_str_section (abfd, shindex) == NULL) |
343 | return NULL; | |
344 | } | |
eed5def8 NC |
345 | else |
346 | { | |
347 | /* PR 24273: The string section's contents may have already | |
348 | been loaded elsewhere, eg because a corrupt file has the | |
349 | string section index in the ELF header pointing at a group | |
350 | section. So be paranoid, and test that the last byte of | |
351 | the section is zero. */ | |
352 | if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0) | |
353 | return NULL; | |
354 | } | |
252b5132 RH |
355 | |
356 | if (strindex >= hdr->sh_size) | |
357 | { | |
1b3a8575 | 358 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
4eca0228 | 359 | _bfd_error_handler |
695344c0 | 360 | /* xgettext:c-format */ |
2dcf00ce AM |
361 | (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"), |
362 | abfd, strindex, (uint64_t) hdr->sh_size, | |
1b3a8575 | 363 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 364 | ? ".shstrtab" |
1b3a8575 | 365 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 366 | return NULL; |
252b5132 RH |
367 | } |
368 | ||
369 | return ((char *) hdr->contents) + strindex; | |
370 | } | |
371 | ||
6cdc0ccc AM |
372 | /* Read and convert symbols to internal format. |
373 | SYMCOUNT specifies the number of symbols to read, starting from | |
374 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
375 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
376 | symbols, and symbol section index extensions, respectively. |
377 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
378 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
379 | |
380 | Elf_Internal_Sym * | |
217aa764 AM |
381 | bfd_elf_get_elf_syms (bfd *ibfd, |
382 | Elf_Internal_Shdr *symtab_hdr, | |
383 | size_t symcount, | |
384 | size_t symoffset, | |
385 | Elf_Internal_Sym *intsym_buf, | |
386 | void *extsym_buf, | |
387 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
388 | { |
389 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 390 | void *alloc_ext; |
df622259 | 391 | const bfd_byte *esym; |
6cdc0ccc AM |
392 | Elf_External_Sym_Shndx *alloc_extshndx; |
393 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 394 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
395 | Elf_Internal_Sym *isym; |
396 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 397 | const struct elf_backend_data *bed; |
6cdc0ccc | 398 | size_t extsym_size; |
1f4361a7 | 399 | size_t amt; |
6cdc0ccc AM |
400 | file_ptr pos; |
401 | ||
e44a2c9c AM |
402 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
403 | abort (); | |
404 | ||
6cdc0ccc AM |
405 | if (symcount == 0) |
406 | return intsym_buf; | |
407 | ||
408 | /* Normal syms might have section extension entries. */ | |
409 | shndx_hdr = NULL; | |
6a40cf0c NC |
410 | if (elf_symtab_shndx_list (ibfd) != NULL) |
411 | { | |
412 | elf_section_list * entry; | |
413 | Elf_Internal_Shdr **sections = elf_elfsections (ibfd); | |
414 | ||
415 | /* Find an index section that is linked to this symtab section. */ | |
416 | for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) | |
315350be NC |
417 | { |
418 | /* PR 20063. */ | |
419 | if (entry->hdr.sh_link >= elf_numsections (ibfd)) | |
420 | continue; | |
421 | ||
422 | if (sections[entry->hdr.sh_link] == symtab_hdr) | |
423 | { | |
424 | shndx_hdr = & entry->hdr; | |
425 | break; | |
426 | }; | |
427 | } | |
6a40cf0c NC |
428 | |
429 | if (shndx_hdr == NULL) | |
430 | { | |
431 | if (symtab_hdr == & elf_symtab_hdr (ibfd)) | |
432 | /* Not really accurate, but this was how the old code used to work. */ | |
433 | shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr; | |
434 | /* Otherwise we do nothing. The assumption is that | |
435 | the index table will not be needed. */ | |
436 | } | |
437 | } | |
6cdc0ccc AM |
438 | |
439 | /* Read the symbols. */ | |
440 | alloc_ext = NULL; | |
441 | alloc_extshndx = NULL; | |
4dd07732 | 442 | alloc_intsym = NULL; |
6cdc0ccc AM |
443 | bed = get_elf_backend_data (ibfd); |
444 | extsym_size = bed->s->sizeof_sym; | |
1f4361a7 AM |
445 | if (_bfd_mul_overflow (symcount, extsym_size, &amt)) |
446 | { | |
447 | bfd_set_error (bfd_error_file_too_big); | |
448 | intsym_buf = NULL; | |
449 | goto out; | |
450 | } | |
6cdc0ccc AM |
451 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; |
452 | if (extsym_buf == NULL) | |
453 | { | |
1f4361a7 | 454 | alloc_ext = bfd_malloc (amt); |
6cdc0ccc AM |
455 | extsym_buf = alloc_ext; |
456 | } | |
457 | if (extsym_buf == NULL | |
458 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
459 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
460 | { | |
461 | intsym_buf = NULL; | |
462 | goto out; | |
463 | } | |
464 | ||
465 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
466 | extshndx_buf = NULL; | |
467 | else | |
468 | { | |
1f4361a7 AM |
469 | if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt)) |
470 | { | |
471 | bfd_set_error (bfd_error_file_too_big); | |
472 | intsym_buf = NULL; | |
473 | goto out; | |
474 | } | |
6cdc0ccc AM |
475 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); |
476 | if (extshndx_buf == NULL) | |
477 | { | |
1f4361a7 | 478 | alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
6cdc0ccc AM |
479 | extshndx_buf = alloc_extshndx; |
480 | } | |
481 | if (extshndx_buf == NULL | |
482 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
483 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
484 | { | |
485 | intsym_buf = NULL; | |
486 | goto out; | |
487 | } | |
488 | } | |
489 | ||
490 | if (intsym_buf == NULL) | |
491 | { | |
1f4361a7 AM |
492 | if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt)) |
493 | { | |
494 | bfd_set_error (bfd_error_file_too_big); | |
495 | goto out; | |
496 | } | |
497 | alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt); | |
4dd07732 | 498 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
499 | if (intsym_buf == NULL) |
500 | goto out; | |
501 | } | |
502 | ||
503 | /* Convert the symbols to internal form. */ | |
504 | isymend = intsym_buf + symcount; | |
a50b1753 | 505 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
07d6d2b8 | 506 | shndx = extshndx_buf; |
6cdc0ccc AM |
507 | isym < isymend; |
508 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
509 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
510 | { | |
511 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
695344c0 | 512 | /* xgettext:c-format */ |
871b3ab2 | 513 | _bfd_error_handler (_("%pB symbol number %lu references" |
63a5468a | 514 | " nonexistent SHT_SYMTAB_SHNDX section"), |
4eca0228 | 515 | ibfd, (unsigned long) symoffset); |
4dd07732 AM |
516 | if (alloc_intsym != NULL) |
517 | free (alloc_intsym); | |
8384fb8f AM |
518 | intsym_buf = NULL; |
519 | goto out; | |
520 | } | |
6cdc0ccc AM |
521 | |
522 | out: | |
523 | if (alloc_ext != NULL) | |
524 | free (alloc_ext); | |
525 | if (alloc_extshndx != NULL) | |
526 | free (alloc_extshndx); | |
527 | ||
528 | return intsym_buf; | |
529 | } | |
530 | ||
5cab59f6 AM |
531 | /* Look up a symbol name. */ |
532 | const char * | |
be8dd2ca AM |
533 | bfd_elf_sym_name (bfd *abfd, |
534 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
535 | Elf_Internal_Sym *isym, |
536 | asection *sym_sec) | |
5cab59f6 | 537 | { |
26c61ae5 | 538 | const char *name; |
5cab59f6 | 539 | unsigned int iname = isym->st_name; |
be8dd2ca | 540 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 541 | |
138f35cc JJ |
542 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
543 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 544 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
545 | { |
546 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
547 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
548 | } | |
549 | ||
26c61ae5 L |
550 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
551 | if (name == NULL) | |
552 | name = "(null)"; | |
553 | else if (sym_sec && *name == '\0') | |
fd361982 | 554 | name = bfd_section_name (sym_sec); |
26c61ae5 L |
555 | |
556 | return name; | |
5cab59f6 AM |
557 | } |
558 | ||
dbb410c3 AM |
559 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
560 | sections. The first element is the flags, the rest are section | |
561 | pointers. */ | |
562 | ||
563 | typedef union elf_internal_group { | |
564 | Elf_Internal_Shdr *shdr; | |
565 | unsigned int flags; | |
566 | } Elf_Internal_Group; | |
567 | ||
b885599b AM |
568 | /* Return the name of the group signature symbol. Why isn't the |
569 | signature just a string? */ | |
570 | ||
571 | static const char * | |
217aa764 | 572 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 573 | { |
9dce4196 | 574 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
575 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
576 | Elf_External_Sym_Shndx eshndx; | |
577 | Elf_Internal_Sym isym; | |
b885599b | 578 | |
13792e9d L |
579 | /* First we need to ensure the symbol table is available. Make sure |
580 | that it is a symbol table section. */ | |
4fbb74a6 AM |
581 | if (ghdr->sh_link >= elf_numsections (abfd)) |
582 | return NULL; | |
13792e9d L |
583 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
584 | if (hdr->sh_type != SHT_SYMTAB | |
585 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
586 | return NULL; |
587 | ||
9dce4196 AM |
588 | /* Go read the symbol. */ |
589 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
590 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
591 | &isym, esym, &eshndx) == NULL) | |
b885599b | 592 | return NULL; |
9dce4196 | 593 | |
26c61ae5 | 594 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
595 | } |
596 | ||
dbb410c3 AM |
597 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
598 | ||
b34976b6 | 599 | static bfd_boolean |
217aa764 | 600 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
601 | { |
602 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
603 | ||
604 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
605 | is set to -1 if there are no SHT_GROUP sections. */ | |
606 | if (num_group == 0) | |
607 | { | |
608 | unsigned int i, shnum; | |
609 | ||
610 | /* First count the number of groups. If we have a SHT_GROUP | |
611 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 612 | shnum = elf_numsections (abfd); |
dbb410c3 | 613 | num_group = 0; |
08a40648 | 614 | |
44534af3 | 615 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ |
1783205a | 616 | ( (shdr)->sh_type == SHT_GROUP \ |
44534af3 | 617 | && (shdr)->sh_size >= minsize \ |
1783205a NC |
618 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ |
619 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 620 | |
dbb410c3 AM |
621 | for (i = 0; i < shnum; i++) |
622 | { | |
623 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 624 | |
44534af3 | 625 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 AM |
626 | num_group += 1; |
627 | } | |
628 | ||
629 | if (num_group == 0) | |
20dbb49d L |
630 | { |
631 | num_group = (unsigned) -1; | |
632 | elf_tdata (abfd)->num_group = num_group; | |
ce497010 | 633 | elf_tdata (abfd)->group_sect_ptr = NULL; |
20dbb49d L |
634 | } |
635 | else | |
dbb410c3 AM |
636 | { |
637 | /* We keep a list of elf section headers for group sections, | |
638 | so we can find them quickly. */ | |
1f4361a7 | 639 | size_t amt; |
d0fb9a8d | 640 | |
20dbb49d | 641 | elf_tdata (abfd)->num_group = num_group; |
1f4361a7 AM |
642 | amt = num_group * sizeof (Elf_Internal_Shdr *); |
643 | elf_tdata (abfd)->group_sect_ptr | |
644 | = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt); | |
dbb410c3 | 645 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 646 | return FALSE; |
dbb410c3 | 647 | num_group = 0; |
ce497010 | 648 | |
dbb410c3 AM |
649 | for (i = 0; i < shnum; i++) |
650 | { | |
651 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 652 | |
44534af3 | 653 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 | 654 | { |
973ffd63 | 655 | unsigned char *src; |
dbb410c3 AM |
656 | Elf_Internal_Group *dest; |
657 | ||
07d6d2b8 AM |
658 | /* Make sure the group section has a BFD section |
659 | attached to it. */ | |
660 | if (!bfd_section_from_shdr (abfd, i)) | |
661 | return FALSE; | |
662 | ||
dbb410c3 AM |
663 | /* Add to list of sections. */ |
664 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
665 | num_group += 1; | |
666 | ||
667 | /* Read the raw contents. */ | |
1f4361a7 AM |
668 | BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0); |
669 | shdr->contents = NULL; | |
670 | if (_bfd_mul_overflow (shdr->sh_size, | |
671 | sizeof (*dest) / 4, &amt) | |
1f4361a7 | 672 | || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 |
2bb3687b AM |
673 | || !(shdr->contents |
674 | = _bfd_alloc_and_read (abfd, amt, shdr->sh_size))) | |
493a3386 NC |
675 | { |
676 | _bfd_error_handler | |
695344c0 | 677 | /* xgettext:c-format */ |
871b3ab2 | 678 | (_("%pB: invalid size field in group section" |
2dcf00ce AM |
679 | " header: %#" PRIx64 ""), |
680 | abfd, (uint64_t) shdr->sh_size); | |
493a3386 NC |
681 | bfd_set_error (bfd_error_bad_value); |
682 | -- num_group; | |
493a3386 NC |
683 | continue; |
684 | } | |
708d7d0d | 685 | |
dbb410c3 AM |
686 | /* Translate raw contents, a flag word followed by an |
687 | array of elf section indices all in target byte order, | |
688 | to the flag word followed by an array of elf section | |
689 | pointers. */ | |
690 | src = shdr->contents + shdr->sh_size; | |
691 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
06614111 | 692 | |
dbb410c3 AM |
693 | while (1) |
694 | { | |
695 | unsigned int idx; | |
696 | ||
697 | src -= 4; | |
698 | --dest; | |
699 | idx = H_GET_32 (abfd, src); | |
700 | if (src == shdr->contents) | |
701 | { | |
327301a4 | 702 | dest->shdr = NULL; |
dbb410c3 | 703 | dest->flags = idx; |
b885599b AM |
704 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
705 | shdr->bfd_section->flags | |
706 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
707 | break; |
708 | } | |
4bba0fb1 | 709 | if (idx < shnum) |
bae363f1 L |
710 | { |
711 | dest->shdr = elf_elfsections (abfd)[idx]; | |
712 | /* PR binutils/23199: All sections in a | |
713 | section group should be marked with | |
714 | SHF_GROUP. But some tools generate | |
715 | broken objects without SHF_GROUP. Fix | |
716 | them up here. */ | |
717 | dest->shdr->sh_flags |= SHF_GROUP; | |
718 | } | |
4bba0fb1 AM |
719 | if (idx >= shnum |
720 | || dest->shdr->sh_type == SHT_GROUP) | |
dbb410c3 | 721 | { |
4eca0228 | 722 | _bfd_error_handler |
4bba0fb1 AM |
723 | (_("%pB: invalid entry in SHT_GROUP section [%u]"), |
724 | abfd, i); | |
725 | dest->shdr = NULL; | |
dbb410c3 | 726 | } |
dbb410c3 AM |
727 | } |
728 | } | |
729 | } | |
493a3386 NC |
730 | |
731 | /* PR 17510: Corrupt binaries might contain invalid groups. */ | |
732 | if (num_group != (unsigned) elf_tdata (abfd)->num_group) | |
733 | { | |
734 | elf_tdata (abfd)->num_group = num_group; | |
735 | ||
736 | /* If all groups are invalid then fail. */ | |
737 | if (num_group == 0) | |
738 | { | |
739 | elf_tdata (abfd)->group_sect_ptr = NULL; | |
740 | elf_tdata (abfd)->num_group = num_group = -1; | |
4eca0228 | 741 | _bfd_error_handler |
871b3ab2 | 742 | (_("%pB: no valid group sections found"), abfd); |
493a3386 NC |
743 | bfd_set_error (bfd_error_bad_value); |
744 | } | |
745 | } | |
dbb410c3 AM |
746 | } |
747 | } | |
748 | ||
749 | if (num_group != (unsigned) -1) | |
750 | { | |
564e11c9 JW |
751 | unsigned int search_offset = elf_tdata (abfd)->group_search_offset; |
752 | unsigned int j; | |
dbb410c3 | 753 | |
564e11c9 | 754 | for (j = 0; j < num_group; j++) |
dbb410c3 | 755 | { |
564e11c9 JW |
756 | /* Begin search from previous found group. */ |
757 | unsigned i = (j + search_offset) % num_group; | |
758 | ||
dbb410c3 | 759 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; |
ce497010 | 760 | Elf_Internal_Group *idx; |
0c54f692 | 761 | bfd_size_type n_elt; |
ce497010 NC |
762 | |
763 | if (shdr == NULL) | |
764 | continue; | |
765 | ||
766 | idx = (Elf_Internal_Group *) shdr->contents; | |
0c54f692 NC |
767 | if (idx == NULL || shdr->sh_size < 4) |
768 | { | |
769 | /* See PR 21957 for a reproducer. */ | |
770 | /* xgettext:c-format */ | |
871b3ab2 | 771 | _bfd_error_handler (_("%pB: group section '%pA' has no contents"), |
0c54f692 NC |
772 | abfd, shdr->bfd_section); |
773 | elf_tdata (abfd)->group_sect_ptr[i] = NULL; | |
774 | bfd_set_error (bfd_error_bad_value); | |
775 | return FALSE; | |
776 | } | |
ce497010 | 777 | n_elt = shdr->sh_size / 4; |
dbb410c3 AM |
778 | |
779 | /* Look through this group's sections to see if current | |
780 | section is a member. */ | |
781 | while (--n_elt != 0) | |
782 | if ((++idx)->shdr == hdr) | |
783 | { | |
e0e8c97f | 784 | asection *s = NULL; |
dbb410c3 AM |
785 | |
786 | /* We are a member of this group. Go looking through | |
787 | other members to see if any others are linked via | |
788 | next_in_group. */ | |
789 | idx = (Elf_Internal_Group *) shdr->contents; | |
790 | n_elt = shdr->sh_size / 4; | |
791 | while (--n_elt != 0) | |
4bba0fb1 AM |
792 | if ((++idx)->shdr != NULL |
793 | && (s = idx->shdr->bfd_section) != NULL | |
945906ff | 794 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
795 | break; |
796 | if (n_elt != 0) | |
797 | { | |
dbb410c3 AM |
798 | /* Snarf the group name from other member, and |
799 | insert current section in circular list. */ | |
945906ff AM |
800 | elf_group_name (newsect) = elf_group_name (s); |
801 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
802 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
803 | } |
804 | else | |
805 | { | |
dbb410c3 AM |
806 | const char *gname; |
807 | ||
b885599b AM |
808 | gname = group_signature (abfd, shdr); |
809 | if (gname == NULL) | |
b34976b6 | 810 | return FALSE; |
945906ff | 811 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
812 | |
813 | /* Start a circular list with one element. */ | |
945906ff | 814 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 815 | } |
b885599b | 816 | |
9dce4196 AM |
817 | /* If the group section has been created, point to the |
818 | new member. */ | |
dbb410c3 | 819 | if (shdr->bfd_section != NULL) |
945906ff | 820 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 821 | |
564e11c9 JW |
822 | elf_tdata (abfd)->group_search_offset = i; |
823 | j = num_group - 1; | |
dbb410c3 AM |
824 | break; |
825 | } | |
826 | } | |
827 | } | |
828 | ||
945906ff | 829 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 830 | { |
695344c0 | 831 | /* xgettext:c-format */ |
871b3ab2 | 832 | _bfd_error_handler (_("%pB: no group info for section '%pA'"), |
4eca0228 | 833 | abfd, newsect); |
493a3386 | 834 | return FALSE; |
dbb410c3 | 835 | } |
b34976b6 | 836 | return TRUE; |
dbb410c3 AM |
837 | } |
838 | ||
3d7f7666 | 839 | bfd_boolean |
dd863624 | 840 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
841 | { |
842 | unsigned int i; | |
843 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
844 | bfd_boolean result = TRUE; | |
dd863624 L |
845 | asection *s; |
846 | ||
847 | /* Process SHF_LINK_ORDER. */ | |
848 | for (s = abfd->sections; s != NULL; s = s->next) | |
849 | { | |
850 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
851 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
852 | { | |
853 | unsigned int elfsec = this_hdr->sh_link; | |
854 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
855 | not set the sh_link or sh_info fields. Hence we could | |
856 | get the situation where elfsec is 0. */ | |
857 | if (elfsec == 0) | |
858 | { | |
4fbb74a6 | 859 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
a859124d AM |
860 | bed->link_order_error_handler |
861 | /* xgettext:c-format */ | |
862 | (_("%pB: warning: sh_link not set for section `%pA'"), | |
863 | abfd, s); | |
dd863624 L |
864 | } |
865 | else | |
866 | { | |
91d6fa6a | 867 | asection *linksec = NULL; |
25bbc984 | 868 | |
4fbb74a6 AM |
869 | if (elfsec < elf_numsections (abfd)) |
870 | { | |
871 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 872 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 873 | } |
25bbc984 L |
874 | |
875 | /* PR 1991, 2008: | |
876 | Some strip/objcopy may leave an incorrect value in | |
877 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 878 | if (linksec == NULL) |
25bbc984 | 879 | { |
4eca0228 | 880 | _bfd_error_handler |
695344c0 | 881 | /* xgettext:c-format */ |
871b3ab2 | 882 | (_("%pB: sh_link [%d] in section `%pA' is incorrect"), |
c08bb8dd | 883 | s->owner, elfsec, s); |
25bbc984 L |
884 | result = FALSE; |
885 | } | |
886 | ||
91d6fa6a | 887 | elf_linked_to_section (s) = linksec; |
dd863624 L |
888 | } |
889 | } | |
53720c49 AM |
890 | else if (this_hdr->sh_type == SHT_GROUP |
891 | && elf_next_in_group (s) == NULL) | |
892 | { | |
4eca0228 | 893 | _bfd_error_handler |
695344c0 | 894 | /* xgettext:c-format */ |
871b3ab2 | 895 | (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"), |
53720c49 AM |
896 | abfd, elf_section_data (s)->this_idx); |
897 | result = FALSE; | |
898 | } | |
dd863624 | 899 | } |
3d7f7666 | 900 | |
dd863624 | 901 | /* Process section groups. */ |
3d7f7666 L |
902 | if (num_group == (unsigned) -1) |
903 | return result; | |
904 | ||
905 | for (i = 0; i < num_group; i++) | |
906 | { | |
907 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
4b0e8a5f NC |
908 | Elf_Internal_Group *idx; |
909 | unsigned int n_elt; | |
3d7f7666 | 910 | |
4b0e8a5f NC |
911 | /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */ |
912 | if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) | |
913 | { | |
4eca0228 | 914 | _bfd_error_handler |
695344c0 | 915 | /* xgettext:c-format */ |
871b3ab2 | 916 | (_("%pB: section group entry number %u is corrupt"), |
4b0e8a5f NC |
917 | abfd, i); |
918 | result = FALSE; | |
919 | continue; | |
920 | } | |
921 | ||
922 | idx = (Elf_Internal_Group *) shdr->contents; | |
923 | n_elt = shdr->sh_size / 4; | |
1b786873 | 924 | |
3d7f7666 | 925 | while (--n_elt != 0) |
24d3e51b NC |
926 | { |
927 | ++ idx; | |
928 | ||
929 | if (idx->shdr == NULL) | |
930 | continue; | |
931 | else if (idx->shdr->bfd_section) | |
932 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
db4677b8 AM |
933 | else if (idx->shdr->sh_type != SHT_RELA |
934 | && idx->shdr->sh_type != SHT_REL) | |
24d3e51b NC |
935 | { |
936 | /* There are some unknown sections in the group. */ | |
937 | _bfd_error_handler | |
938 | /* xgettext:c-format */ | |
871b3ab2 | 939 | (_("%pB: unknown type [%#x] section `%s' in group [%pA]"), |
24d3e51b NC |
940 | abfd, |
941 | idx->shdr->sh_type, | |
942 | bfd_elf_string_from_elf_section (abfd, | |
943 | (elf_elfheader (abfd) | |
944 | ->e_shstrndx), | |
945 | idx->shdr->sh_name), | |
946 | shdr->bfd_section); | |
947 | result = FALSE; | |
948 | } | |
949 | } | |
3d7f7666 | 950 | } |
24d3e51b | 951 | |
3d7f7666 L |
952 | return result; |
953 | } | |
954 | ||
72adc230 AM |
955 | bfd_boolean |
956 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
957 | { | |
958 | return elf_next_in_group (sec) != NULL; | |
959 | } | |
960 | ||
cb7f4b29 AM |
961 | const char * |
962 | bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
963 | { | |
964 | if (elf_sec_group (sec) != NULL) | |
965 | return elf_group_name (sec); | |
966 | return NULL; | |
967 | } | |
968 | ||
f6fe1ccd L |
969 | static char * |
970 | convert_debug_to_zdebug (bfd *abfd, const char *name) | |
971 | { | |
972 | unsigned int len = strlen (name); | |
973 | char *new_name = bfd_alloc (abfd, len + 2); | |
974 | if (new_name == NULL) | |
975 | return NULL; | |
976 | new_name[0] = '.'; | |
977 | new_name[1] = 'z'; | |
978 | memcpy (new_name + 2, name + 1, len); | |
979 | return new_name; | |
980 | } | |
981 | ||
982 | static char * | |
983 | convert_zdebug_to_debug (bfd *abfd, const char *name) | |
984 | { | |
985 | unsigned int len = strlen (name); | |
986 | char *new_name = bfd_alloc (abfd, len); | |
987 | if (new_name == NULL) | |
988 | return NULL; | |
989 | new_name[0] = '.'; | |
990 | memcpy (new_name + 1, name + 2, len - 1); | |
991 | return new_name; | |
992 | } | |
993 | ||
cc5277b1 ML |
994 | /* This a copy of lto_section defined in GCC (lto-streamer.h). */ |
995 | ||
996 | struct lto_section | |
997 | { | |
998 | int16_t major_version; | |
999 | int16_t minor_version; | |
1000 | unsigned char slim_object; | |
1001 | ||
1002 | /* Flags is a private field that is not defined publicly. */ | |
1003 | uint16_t flags; | |
1004 | }; | |
1005 | ||
252b5132 RH |
1006 | /* Make a BFD section from an ELF section. We store a pointer to the |
1007 | BFD section in the bfd_section field of the header. */ | |
1008 | ||
b34976b6 | 1009 | bfd_boolean |
217aa764 AM |
1010 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
1011 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
1012 | const char *name, |
1013 | int shindex) | |
252b5132 RH |
1014 | { |
1015 | asection *newsect; | |
1016 | flagword flags; | |
9c5bfbb7 | 1017 | const struct elf_backend_data *bed; |
502794d4 | 1018 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
252b5132 RH |
1019 | |
1020 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 1021 | return TRUE; |
252b5132 RH |
1022 | |
1023 | newsect = bfd_make_section_anyway (abfd, name); | |
1024 | if (newsect == NULL) | |
b34976b6 | 1025 | return FALSE; |
252b5132 | 1026 | |
1829f4b2 AM |
1027 | hdr->bfd_section = newsect; |
1028 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 1029 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 1030 | |
2f89ff8d L |
1031 | /* Always use the real type/flags. */ |
1032 | elf_section_type (newsect) = hdr->sh_type; | |
1033 | elf_section_flags (newsect) = hdr->sh_flags; | |
1034 | ||
252b5132 RH |
1035 | newsect->filepos = hdr->sh_offset; |
1036 | ||
252b5132 RH |
1037 | flags = SEC_NO_FLAGS; |
1038 | if (hdr->sh_type != SHT_NOBITS) | |
1039 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 1040 | if (hdr->sh_type == SHT_GROUP) |
7bdf4127 | 1041 | flags |= SEC_GROUP; |
252b5132 RH |
1042 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
1043 | { | |
1044 | flags |= SEC_ALLOC; | |
1045 | if (hdr->sh_type != SHT_NOBITS) | |
1046 | flags |= SEC_LOAD; | |
1047 | } | |
1048 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
1049 | flags |= SEC_READONLY; | |
1050 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
1051 | flags |= SEC_CODE; | |
1052 | else if ((flags & SEC_LOAD) != 0) | |
1053 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
1054 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
1055 | { | |
1056 | flags |= SEC_MERGE; | |
1057 | newsect->entsize = hdr->sh_entsize; | |
f5fa8ca2 | 1058 | } |
84865015 NC |
1059 | if ((hdr->sh_flags & SHF_STRINGS) != 0) |
1060 | flags |= SEC_STRINGS; | |
dbb410c3 AM |
1061 | if (hdr->sh_flags & SHF_GROUP) |
1062 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 1063 | return FALSE; |
13ae64f3 JJ |
1064 | if ((hdr->sh_flags & SHF_TLS) != 0) |
1065 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
1066 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
1067 | flags |= SEC_EXCLUDE; | |
252b5132 | 1068 | |
df3a023b AM |
1069 | switch (elf_elfheader (abfd)->e_ident[EI_OSABI]) |
1070 | { | |
1071 | /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE, | |
1072 | but binutils as of 2019-07-23 did not set the EI_OSABI header | |
1073 | byte. */ | |
1074 | case ELFOSABI_NONE: | |
1075 | case ELFOSABI_GNU: | |
1076 | case ELFOSABI_FREEBSD: | |
1077 | if ((hdr->sh_flags & SHF_GNU_MBIND) != 0) | |
1078 | elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind; | |
1079 | break; | |
1080 | } | |
1081 | ||
3d2b39cf | 1082 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 1083 | { |
3d2b39cf L |
1084 | /* The debugging sections appear to be recognized only by name, |
1085 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
1086 | if (name [0] == '.') |
1087 | { | |
bb294208 AM |
1088 | if (strncmp (name, ".debug", 6) == 0 |
1089 | || strncmp (name, ".gnu.linkonce.wi.", 17) == 0 | |
1090 | || strncmp (name, ".zdebug", 7) == 0) | |
1091 | flags |= SEC_DEBUGGING | SEC_ELF_OCTETS; | |
1092 | else if (strncmp (name, GNU_BUILD_ATTRS_SECTION_NAME, 21) == 0 | |
1093 | || strncmp (name, ".note.gnu", 9) == 0) | |
502794d4 CE |
1094 | { |
1095 | flags |= SEC_ELF_OCTETS; | |
1096 | opb = 1; | |
1097 | } | |
bb294208 AM |
1098 | else if (strncmp (name, ".line", 5) == 0 |
1099 | || strncmp (name, ".stab", 5) == 0 | |
1100 | || strcmp (name, ".gdb_index") == 0) | |
3d2b39cf L |
1101 | flags |= SEC_DEBUGGING; |
1102 | } | |
1103 | } | |
252b5132 | 1104 | |
502794d4 CE |
1105 | if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb) |
1106 | || !bfd_set_section_size (newsect, hdr->sh_size) | |
1107 | || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign))) | |
1108 | return FALSE; | |
1109 | ||
252b5132 RH |
1110 | /* As a GNU extension, if the name begins with .gnu.linkonce, we |
1111 | only link a single copy of the section. This is used to support | |
1112 | g++. g++ will emit each template expansion in its own section. | |
1113 | The symbols will be defined as weak, so that multiple definitions | |
1114 | are permitted. The GNU linker extension is to actually discard | |
1115 | all but one of the sections. */ | |
0112cd26 | 1116 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 1117 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
1118 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
1119 | ||
8c803a2d AM |
1120 | if (!bfd_set_section_flags (newsect, flags)) |
1121 | return FALSE; | |
1122 | ||
fa152c49 JW |
1123 | bed = get_elf_backend_data (abfd); |
1124 | if (bed->elf_backend_section_flags) | |
8c803a2d | 1125 | if (!bed->elf_backend_section_flags (hdr)) |
b34976b6 | 1126 | return FALSE; |
fa152c49 | 1127 | |
718175fa JK |
1128 | /* We do not parse the PT_NOTE segments as we are interested even in the |
1129 | separate debug info files which may have the segments offsets corrupted. | |
1130 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
1131 | if (hdr->sh_type == SHT_NOTE) | |
1132 | { | |
baea7ef1 | 1133 | bfd_byte *contents; |
718175fa | 1134 | |
baea7ef1 | 1135 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
1136 | return FALSE; |
1137 | ||
276da9b3 L |
1138 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, |
1139 | hdr->sh_offset, hdr->sh_addralign); | |
718175fa JK |
1140 | free (contents); |
1141 | } | |
1142 | ||
8c803a2d | 1143 | if ((newsect->flags & SEC_ALLOC) != 0) |
252b5132 RH |
1144 | { |
1145 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
1146 | unsigned int i, nload; |
1147 | ||
1148 | /* Some ELF linkers produce binaries with all the program header | |
1149 | p_paddr fields zero. If we have such a binary with more than | |
1150 | one PT_LOAD header, then leave the section lma equal to vma | |
1151 | so that we don't create sections with overlapping lma. */ | |
1152 | phdr = elf_tdata (abfd)->phdr; | |
1153 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1154 | if (phdr->p_paddr != 0) | |
1155 | break; | |
1156 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
1157 | ++nload; | |
1158 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1159 | return TRUE; | |
252b5132 | 1160 | |
252b5132 RH |
1161 | phdr = elf_tdata (abfd)->phdr; |
1162 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1163 | { | |
86b2281f AM |
1164 | if (((phdr->p_type == PT_LOAD |
1165 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1166 | || phdr->p_type == PT_TLS) | |
9a83a553 | 1167 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 1168 | { |
8c803a2d | 1169 | if ((newsect->flags & SEC_LOAD) == 0) |
88967714 | 1170 | newsect->lma = (phdr->p_paddr |
502794d4 | 1171 | + hdr->sh_addr - phdr->p_vaddr) / opb; |
88967714 AM |
1172 | else |
1173 | /* We used to use the same adjustment for SEC_LOAD | |
1174 | sections, but that doesn't work if the segment | |
1175 | is packed with code from multiple VMAs. | |
1176 | Instead we calculate the section LMA based on | |
1177 | the segment LMA. It is assumed that the | |
1178 | segment will contain sections with contiguous | |
1179 | LMAs, even if the VMAs are not. */ | |
1180 | newsect->lma = (phdr->p_paddr | |
502794d4 | 1181 | + hdr->sh_offset - phdr->p_offset) / opb; |
88967714 AM |
1182 | |
1183 | /* With contiguous segments, we can't tell from file | |
1184 | offsets whether a section with zero size should | |
1185 | be placed at the end of one segment or the | |
1186 | beginning of the next. Decide based on vaddr. */ | |
1187 | if (hdr->sh_addr >= phdr->p_vaddr | |
1188 | && (hdr->sh_addr + hdr->sh_size | |
1189 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1190 | break; | |
252b5132 RH |
1191 | } |
1192 | } | |
1193 | } | |
1194 | ||
4a114e3e L |
1195 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1196 | .zdebug_*, after the section flags is set. */ | |
8c803a2d | 1197 | if ((newsect->flags & SEC_DEBUGGING) |
4a114e3e L |
1198 | && ((name[1] == 'd' && name[6] == '_') |
1199 | || (name[1] == 'z' && name[7] == '_'))) | |
1200 | { | |
1201 | enum { nothing, compress, decompress } action = nothing; | |
151411f8 | 1202 | int compression_header_size; |
dab394de | 1203 | bfd_size_type uncompressed_size; |
4207142d | 1204 | unsigned int uncompressed_align_power; |
151411f8 L |
1205 | bfd_boolean compressed |
1206 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
dab394de | 1207 | &compression_header_size, |
4207142d MW |
1208 | &uncompressed_size, |
1209 | &uncompressed_align_power); | |
151411f8 | 1210 | if (compressed) |
4a114e3e L |
1211 | { |
1212 | /* Compressed section. Check if we should decompress. */ | |
1213 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1214 | action = decompress; | |
1215 | } | |
151411f8 L |
1216 | |
1217 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1218 | section. Check if we should compress. */ | |
1219 | if (action == nothing) | |
4a114e3e | 1220 | { |
151411f8 L |
1221 | if (newsect->size != 0 |
1222 | && (abfd->flags & BFD_COMPRESS) | |
1223 | && compression_header_size >= 0 | |
dab394de | 1224 | && uncompressed_size > 0 |
151411f8 L |
1225 | && (!compressed |
1226 | || ((compression_header_size > 0) | |
1227 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
4a114e3e | 1228 | action = compress; |
151411f8 L |
1229 | else |
1230 | return TRUE; | |
4a114e3e L |
1231 | } |
1232 | ||
151411f8 | 1233 | if (action == compress) |
4a114e3e | 1234 | { |
4a114e3e L |
1235 | if (!bfd_init_section_compress_status (abfd, newsect)) |
1236 | { | |
4eca0228 | 1237 | _bfd_error_handler |
695344c0 | 1238 | /* xgettext:c-format */ |
871b3ab2 | 1239 | (_("%pB: unable to initialize compress status for section %s"), |
4a114e3e L |
1240 | abfd, name); |
1241 | return FALSE; | |
1242 | } | |
151411f8 L |
1243 | } |
1244 | else | |
1245 | { | |
4a114e3e L |
1246 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
1247 | { | |
4eca0228 | 1248 | _bfd_error_handler |
695344c0 | 1249 | /* xgettext:c-format */ |
871b3ab2 | 1250 | (_("%pB: unable to initialize decompress status for section %s"), |
4a114e3e L |
1251 | abfd, name); |
1252 | return FALSE; | |
1253 | } | |
151411f8 L |
1254 | } |
1255 | ||
f6fe1ccd | 1256 | if (abfd->is_linker_input) |
151411f8 | 1257 | { |
f6fe1ccd L |
1258 | if (name[1] == 'z' |
1259 | && (action == decompress | |
1260 | || (action == compress | |
1261 | && (abfd->flags & BFD_COMPRESS_GABI) != 0))) | |
4e011fb5 | 1262 | { |
f6fe1ccd L |
1263 | /* Convert section name from .zdebug_* to .debug_* so |
1264 | that linker will consider this section as a debug | |
1265 | section. */ | |
1266 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
151411f8 L |
1267 | if (new_name == NULL) |
1268 | return FALSE; | |
fd361982 | 1269 | bfd_rename_section (newsect, new_name); |
151411f8 | 1270 | } |
4a114e3e | 1271 | } |
f6fe1ccd L |
1272 | else |
1273 | /* For objdump, don't rename the section. For objcopy, delay | |
1274 | section rename to elf_fake_sections. */ | |
1275 | newsect->flags |= SEC_ELF_RENAME; | |
4a114e3e L |
1276 | } |
1277 | ||
cc5277b1 ML |
1278 | /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information |
1279 | section. */ | |
1280 | const char *lto_section_name = ".gnu.lto_.lto."; | |
1281 | if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0) | |
1282 | { | |
1283 | struct lto_section lsection; | |
1284 | if (bfd_get_section_contents (abfd, newsect, &lsection, 0, | |
1285 | sizeof (struct lto_section))) | |
1286 | abfd->lto_slim_object = lsection.slim_object; | |
1287 | } | |
1288 | ||
b34976b6 | 1289 | return TRUE; |
252b5132 RH |
1290 | } |
1291 | ||
84865015 NC |
1292 | const char *const bfd_elf_section_type_names[] = |
1293 | { | |
252b5132 RH |
1294 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", |
1295 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1296 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1297 | }; | |
1298 | ||
1049f94e | 1299 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1300 | output, and the reloc is against an external symbol, and nothing |
1301 | has given us any additional addend, the resulting reloc will also | |
1302 | be against the same symbol. In such a case, we don't want to | |
1303 | change anything about the way the reloc is handled, since it will | |
1304 | all be done at final link time. Rather than put special case code | |
1305 | into bfd_perform_relocation, all the reloc types use this howto | |
1306 | function. It just short circuits the reloc if producing | |
1049f94e | 1307 | relocatable output against an external symbol. */ |
252b5132 | 1308 | |
252b5132 | 1309 | bfd_reloc_status_type |
217aa764 AM |
1310 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1311 | arelent *reloc_entry, | |
1312 | asymbol *symbol, | |
1313 | void *data ATTRIBUTE_UNUSED, | |
1314 | asection *input_section, | |
1315 | bfd *output_bfd, | |
1316 | char **error_message ATTRIBUTE_UNUSED) | |
1317 | { | |
1318 | if (output_bfd != NULL | |
252b5132 RH |
1319 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1320 | && (! reloc_entry->howto->partial_inplace | |
1321 | || reloc_entry->addend == 0)) | |
1322 | { | |
1323 | reloc_entry->address += input_section->output_offset; | |
1324 | return bfd_reloc_ok; | |
1325 | } | |
1326 | ||
1327 | return bfd_reloc_continue; | |
1328 | } | |
1329 | \f | |
84865015 NC |
1330 | /* Returns TRUE if section A matches section B. |
1331 | Names, addresses and links may be different, but everything else | |
1332 | should be the same. */ | |
1333 | ||
1334 | static bfd_boolean | |
5522f910 NC |
1335 | section_match (const Elf_Internal_Shdr * a, |
1336 | const Elf_Internal_Shdr * b) | |
84865015 | 1337 | { |
ac85e67c AM |
1338 | if (a->sh_type != b->sh_type |
1339 | || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0 | |
1340 | || a->sh_addralign != b->sh_addralign | |
1341 | || a->sh_entsize != b->sh_entsize) | |
1342 | return FALSE; | |
1343 | if (a->sh_type == SHT_SYMTAB | |
1344 | || a->sh_type == SHT_STRTAB) | |
1345 | return TRUE; | |
1346 | return a->sh_size == b->sh_size; | |
84865015 NC |
1347 | } |
1348 | ||
1349 | /* Find a section in OBFD that has the same characteristics | |
1350 | as IHEADER. Return the index of this section or SHN_UNDEF if | |
1351 | none can be found. Check's section HINT first, as this is likely | |
1352 | to be the correct section. */ | |
1353 | ||
1354 | static unsigned int | |
5cc4ca83 ST |
1355 | find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader, |
1356 | const unsigned int hint) | |
84865015 NC |
1357 | { |
1358 | Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); | |
1359 | unsigned int i; | |
1360 | ||
a55c9876 NC |
1361 | BFD_ASSERT (iheader != NULL); |
1362 | ||
1363 | /* See PR 20922 for a reproducer of the NULL test. */ | |
5cc4ca83 ST |
1364 | if (hint < elf_numsections (obfd) |
1365 | && oheaders[hint] != NULL | |
a55c9876 | 1366 | && section_match (oheaders[hint], iheader)) |
84865015 NC |
1367 | return hint; |
1368 | ||
1369 | for (i = 1; i < elf_numsections (obfd); i++) | |
1370 | { | |
1371 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
1372 | ||
a55c9876 NC |
1373 | if (oheader == NULL) |
1374 | continue; | |
84865015 NC |
1375 | if (section_match (oheader, iheader)) |
1376 | /* FIXME: Do we care if there is a potential for | |
1377 | multiple matches ? */ | |
1378 | return i; | |
1379 | } | |
1380 | ||
1381 | return SHN_UNDEF; | |
1382 | } | |
1383 | ||
5522f910 NC |
1384 | /* PR 19938: Attempt to set the ELF section header fields of an OS or |
1385 | Processor specific section, based upon a matching input section. | |
1386 | Returns TRUE upon success, FALSE otherwise. */ | |
07d6d2b8 | 1387 | |
5522f910 NC |
1388 | static bfd_boolean |
1389 | copy_special_section_fields (const bfd *ibfd, | |
1390 | bfd *obfd, | |
1391 | const Elf_Internal_Shdr *iheader, | |
1392 | Elf_Internal_Shdr *oheader, | |
1393 | const unsigned int secnum) | |
1394 | { | |
1395 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
1396 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); | |
1397 | bfd_boolean changed = FALSE; | |
1398 | unsigned int sh_link; | |
1399 | ||
1400 | if (oheader->sh_type == SHT_NOBITS) | |
1401 | { | |
1402 | /* This is a feature for objcopy --only-keep-debug: | |
1403 | When a section's type is changed to NOBITS, we preserve | |
1404 | the sh_link and sh_info fields so that they can be | |
1405 | matched up with the original. | |
1406 | ||
1407 | Note: Strictly speaking these assignments are wrong. | |
1408 | The sh_link and sh_info fields should point to the | |
1409 | relevent sections in the output BFD, which may not be in | |
1410 | the same location as they were in the input BFD. But | |
1411 | the whole point of this action is to preserve the | |
1412 | original values of the sh_link and sh_info fields, so | |
1413 | that they can be matched up with the section headers in | |
1414 | the original file. So strictly speaking we may be | |
1415 | creating an invalid ELF file, but it is only for a file | |
1416 | that just contains debug info and only for sections | |
1417 | without any contents. */ | |
1418 | if (oheader->sh_link == 0) | |
1419 | oheader->sh_link = iheader->sh_link; | |
1420 | if (oheader->sh_info == 0) | |
1421 | oheader->sh_info = iheader->sh_info; | |
1422 | return TRUE; | |
1423 | } | |
1424 | ||
1425 | /* Allow the target a chance to decide how these fields should be set. */ | |
a859124d AM |
1426 | if (bed->elf_backend_copy_special_section_fields (ibfd, obfd, |
1427 | iheader, oheader)) | |
5522f910 NC |
1428 | return TRUE; |
1429 | ||
1430 | /* We have an iheader which might match oheader, and which has non-zero | |
1431 | sh_info and/or sh_link fields. Attempt to follow those links and find | |
1432 | the section in the output bfd which corresponds to the linked section | |
1433 | in the input bfd. */ | |
1434 | if (iheader->sh_link != SHN_UNDEF) | |
1435 | { | |
4f3ca05b NC |
1436 | /* See PR 20931 for a reproducer. */ |
1437 | if (iheader->sh_link >= elf_numsections (ibfd)) | |
1438 | { | |
76cfced5 | 1439 | _bfd_error_handler |
4f3ca05b | 1440 | /* xgettext:c-format */ |
9793eb77 | 1441 | (_("%pB: invalid sh_link field (%d) in section number %d"), |
4f3ca05b NC |
1442 | ibfd, iheader->sh_link, secnum); |
1443 | return FALSE; | |
1444 | } | |
1445 | ||
5522f910 NC |
1446 | sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); |
1447 | if (sh_link != SHN_UNDEF) | |
1448 | { | |
1449 | oheader->sh_link = sh_link; | |
1450 | changed = TRUE; | |
1451 | } | |
1452 | else | |
1453 | /* FIXME: Should we install iheader->sh_link | |
1454 | if we could not find a match ? */ | |
76cfced5 | 1455 | _bfd_error_handler |
695344c0 | 1456 | /* xgettext:c-format */ |
9793eb77 | 1457 | (_("%pB: failed to find link section for section %d"), obfd, secnum); |
5522f910 NC |
1458 | } |
1459 | ||
1460 | if (iheader->sh_info) | |
1461 | { | |
1462 | /* The sh_info field can hold arbitrary information, but if the | |
1463 | SHF_LINK_INFO flag is set then it should be interpreted as a | |
1464 | section index. */ | |
1465 | if (iheader->sh_flags & SHF_INFO_LINK) | |
1466 | { | |
1467 | sh_link = find_link (obfd, iheaders[iheader->sh_info], | |
1468 | iheader->sh_info); | |
1469 | if (sh_link != SHN_UNDEF) | |
1470 | oheader->sh_flags |= SHF_INFO_LINK; | |
1471 | } | |
1472 | else | |
1473 | /* No idea what it means - just copy it. */ | |
1474 | sh_link = iheader->sh_info; | |
1475 | ||
1476 | if (sh_link != SHN_UNDEF) | |
1477 | { | |
1478 | oheader->sh_info = sh_link; | |
1479 | changed = TRUE; | |
1480 | } | |
1481 | else | |
76cfced5 | 1482 | _bfd_error_handler |
695344c0 | 1483 | /* xgettext:c-format */ |
9793eb77 | 1484 | (_("%pB: failed to find info section for section %d"), obfd, secnum); |
5522f910 NC |
1485 | } |
1486 | ||
1487 | return changed; | |
1488 | } | |
07d6d2b8 | 1489 | |
0ac4564e L |
1490 | /* Copy the program header and other data from one object module to |
1491 | another. */ | |
252b5132 | 1492 | |
b34976b6 | 1493 | bfd_boolean |
217aa764 | 1494 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 | 1495 | { |
5522f910 NC |
1496 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); |
1497 | Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); | |
1498 | const struct elf_backend_data *bed; | |
84865015 NC |
1499 | unsigned int i; |
1500 | ||
2d502050 | 1501 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
84865015 | 1502 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
b34976b6 | 1503 | return TRUE; |
2d502050 | 1504 | |
57b828ef L |
1505 | if (!elf_flags_init (obfd)) |
1506 | { | |
1507 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1508 | elf_flags_init (obfd) = TRUE; | |
1509 | } | |
2d502050 | 1510 | |
0ac4564e | 1511 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1512 | |
1513 | /* Also copy the EI_OSABI field. */ | |
1514 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1515 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 | 1516 | |
5522f910 NC |
1517 | /* If set, copy the EI_ABIVERSION field. */ |
1518 | if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) | |
1519 | elf_elfheader (obfd)->e_ident[EI_ABIVERSION] | |
1520 | = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; | |
07d6d2b8 | 1521 | |
104d59d1 JM |
1522 | /* Copy object attributes. */ |
1523 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
63b9bbb7 | 1524 | |
84865015 NC |
1525 | if (iheaders == NULL || oheaders == NULL) |
1526 | return TRUE; | |
63b9bbb7 | 1527 | |
5522f910 NC |
1528 | bed = get_elf_backend_data (obfd); |
1529 | ||
1530 | /* Possibly copy other fields in the section header. */ | |
84865015 | 1531 | for (i = 1; i < elf_numsections (obfd); i++) |
63b9bbb7 | 1532 | { |
84865015 NC |
1533 | unsigned int j; |
1534 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
63b9bbb7 | 1535 | |
5522f910 NC |
1536 | /* Ignore ordinary sections. SHT_NOBITS sections are considered however |
1537 | because of a special case need for generating separate debug info | |
1538 | files. See below for more details. */ | |
84865015 NC |
1539 | if (oheader == NULL |
1540 | || (oheader->sh_type != SHT_NOBITS | |
5522f910 NC |
1541 | && oheader->sh_type < SHT_LOOS)) |
1542 | continue; | |
1543 | ||
1544 | /* Ignore empty sections, and sections whose | |
1545 | fields have already been initialised. */ | |
1546 | if (oheader->sh_size == 0 | |
84865015 NC |
1547 | || (oheader->sh_info != 0 && oheader->sh_link != 0)) |
1548 | continue; | |
63b9bbb7 | 1549 | |
84865015 | 1550 | /* Scan for the matching section in the input bfd. |
5522f910 NC |
1551 | First we try for a direct mapping between the input and output sections. */ |
1552 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1553 | { | |
1554 | const Elf_Internal_Shdr * iheader = iheaders[j]; | |
1555 | ||
1556 | if (iheader == NULL) | |
1557 | continue; | |
1558 | ||
1559 | if (oheader->bfd_section != NULL | |
1560 | && iheader->bfd_section != NULL | |
1561 | && iheader->bfd_section->output_section != NULL | |
1562 | && iheader->bfd_section->output_section == oheader->bfd_section) | |
1563 | { | |
1564 | /* We have found a connection from the input section to the | |
1565 | output section. Attempt to copy the header fields. If | |
1566 | this fails then do not try any further sections - there | |
1567 | should only be a one-to-one mapping between input and output. */ | |
1568 | if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) | |
1569 | j = elf_numsections (ibfd); | |
1570 | break; | |
1571 | } | |
1572 | } | |
1573 | ||
1574 | if (j < elf_numsections (ibfd)) | |
1575 | continue; | |
1576 | ||
1577 | /* That failed. So try to deduce the corresponding input section. | |
84865015 NC |
1578 | Unfortunately we cannot compare names as the output string table |
1579 | is empty, so instead we check size, address and type. */ | |
1580 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1581 | { | |
5522f910 | 1582 | const Elf_Internal_Shdr * iheader = iheaders[j]; |
84865015 | 1583 | |
5522f910 NC |
1584 | if (iheader == NULL) |
1585 | continue; | |
1586 | ||
1587 | /* Try matching fields in the input section's header. | |
1588 | Since --only-keep-debug turns all non-debug sections into | |
84865015 NC |
1589 | SHT_NOBITS sections, the output SHT_NOBITS type matches any |
1590 | input type. */ | |
1591 | if ((oheader->sh_type == SHT_NOBITS | |
1592 | || iheader->sh_type == oheader->sh_type) | |
5522f910 NC |
1593 | && (iheader->sh_flags & ~ SHF_INFO_LINK) |
1594 | == (oheader->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1595 | && iheader->sh_addralign == oheader->sh_addralign |
1596 | && iheader->sh_entsize == oheader->sh_entsize | |
1597 | && iheader->sh_size == oheader->sh_size | |
1598 | && iheader->sh_addr == oheader->sh_addr | |
1599 | && (iheader->sh_info != oheader->sh_info | |
1600 | || iheader->sh_link != oheader->sh_link)) | |
63b9bbb7 | 1601 | { |
5522f910 NC |
1602 | if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) |
1603 | break; | |
63b9bbb7 NC |
1604 | } |
1605 | } | |
5522f910 NC |
1606 | |
1607 | if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) | |
1608 | { | |
1609 | /* Final attempt. Call the backend copy function | |
1610 | with a NULL input section. */ | |
a859124d AM |
1611 | (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd, |
1612 | NULL, oheader); | |
5522f910 | 1613 | } |
63b9bbb7 NC |
1614 | } |
1615 | ||
b34976b6 | 1616 | return TRUE; |
2d502050 L |
1617 | } |
1618 | ||
cedc298e L |
1619 | static const char * |
1620 | get_segment_type (unsigned int p_type) | |
1621 | { | |
1622 | const char *pt; | |
1623 | switch (p_type) | |
1624 | { | |
1625 | case PT_NULL: pt = "NULL"; break; | |
1626 | case PT_LOAD: pt = "LOAD"; break; | |
1627 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1628 | case PT_INTERP: pt = "INTERP"; break; | |
1629 | case PT_NOTE: pt = "NOTE"; break; | |
1630 | case PT_SHLIB: pt = "SHLIB"; break; | |
1631 | case PT_PHDR: pt = "PHDR"; break; | |
1632 | case PT_TLS: pt = "TLS"; break; | |
1633 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1634 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1635 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1636 | default: pt = NULL; break; | |
1637 | } | |
1638 | return pt; | |
1639 | } | |
1640 | ||
f0b79d91 L |
1641 | /* Print out the program headers. */ |
1642 | ||
b34976b6 | 1643 | bfd_boolean |
217aa764 | 1644 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1645 | { |
a50b1753 | 1646 | FILE *f = (FILE *) farg; |
252b5132 RH |
1647 | Elf_Internal_Phdr *p; |
1648 | asection *s; | |
1649 | bfd_byte *dynbuf = NULL; | |
1650 | ||
1651 | p = elf_tdata (abfd)->phdr; | |
1652 | if (p != NULL) | |
1653 | { | |
1654 | unsigned int i, c; | |
1655 | ||
1656 | fprintf (f, _("\nProgram Header:\n")); | |
1657 | c = elf_elfheader (abfd)->e_phnum; | |
1658 | for (i = 0; i < c; i++, p++) | |
1659 | { | |
cedc298e | 1660 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1661 | char buf[20]; |
1662 | ||
cedc298e | 1663 | if (pt == NULL) |
252b5132 | 1664 | { |
cedc298e L |
1665 | sprintf (buf, "0x%lx", p->p_type); |
1666 | pt = buf; | |
252b5132 | 1667 | } |
dc810e39 | 1668 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1669 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1670 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1671 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1672 | fprintf (f, " paddr 0x"); |
60b89a18 | 1673 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1674 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1675 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1676 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1677 | fprintf (f, " memsz 0x"); |
60b89a18 | 1678 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1679 | fprintf (f, " flags %c%c%c", |
1680 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1681 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1682 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1683 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1684 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1685 | fprintf (f, "\n"); |
1686 | } | |
1687 | } | |
1688 | ||
1689 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1690 | if (s != NULL) | |
1691 | { | |
cb33740c | 1692 | unsigned int elfsec; |
dc810e39 | 1693 | unsigned long shlink; |
252b5132 RH |
1694 | bfd_byte *extdyn, *extdynend; |
1695 | size_t extdynsize; | |
217aa764 | 1696 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1697 | |
1698 | fprintf (f, _("\nDynamic Section:\n")); | |
1699 | ||
eea6121a | 1700 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1701 | goto error_return; |
1702 | ||
1703 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1704 | if (elfsec == SHN_BAD) |
252b5132 | 1705 | goto error_return; |
dc810e39 | 1706 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1707 | |
1708 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1709 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1710 | ||
1711 | extdyn = dynbuf; | |
06614111 NC |
1712 | /* PR 17512: file: 6f427532. */ |
1713 | if (s->size < extdynsize) | |
1714 | goto error_return; | |
eea6121a | 1715 | extdynend = extdyn + s->size; |
1036838a | 1716 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
07d6d2b8 | 1717 | Fix range check. */ |
1036838a | 1718 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) |
252b5132 RH |
1719 | { |
1720 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1721 | const char *name = ""; |
252b5132 | 1722 | char ab[20]; |
b34976b6 | 1723 | bfd_boolean stringp; |
ad9563d6 | 1724 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1725 | |
217aa764 | 1726 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1727 | |
1728 | if (dyn.d_tag == DT_NULL) | |
1729 | break; | |
1730 | ||
b34976b6 | 1731 | stringp = FALSE; |
252b5132 RH |
1732 | switch (dyn.d_tag) |
1733 | { | |
1734 | default: | |
ad9563d6 CM |
1735 | if (bed->elf_backend_get_target_dtag) |
1736 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1737 | ||
1738 | if (!strcmp (name, "")) | |
1739 | { | |
cd9af601 | 1740 | sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag); |
ad9563d6 CM |
1741 | name = ab; |
1742 | } | |
252b5132 RH |
1743 | break; |
1744 | ||
b34976b6 | 1745 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1746 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1747 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1748 | case DT_HASH: name = "HASH"; break; | |
1749 | case DT_STRTAB: name = "STRTAB"; break; | |
1750 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1751 | case DT_RELA: name = "RELA"; break; | |
1752 | case DT_RELASZ: name = "RELASZ"; break; | |
1753 | case DT_RELAENT: name = "RELAENT"; break; | |
1754 | case DT_STRSZ: name = "STRSZ"; break; | |
1755 | case DT_SYMENT: name = "SYMENT"; break; | |
1756 | case DT_INIT: name = "INIT"; break; | |
1757 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1758 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1759 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1760 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1761 | case DT_REL: name = "REL"; break; | |
1762 | case DT_RELSZ: name = "RELSZ"; break; | |
1763 | case DT_RELENT: name = "RELENT"; break; | |
1764 | case DT_PLTREL: name = "PLTREL"; break; | |
1765 | case DT_DEBUG: name = "DEBUG"; break; | |
1766 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1767 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1768 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1769 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1770 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1771 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1772 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1773 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1774 | case DT_FLAGS: name = "FLAGS"; break; |
1775 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1776 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1777 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1778 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1779 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1780 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1781 | case DT_FEATURE: name = "FEATURE"; break; | |
1782 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1783 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1784 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1785 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1786 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1787 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1788 | case DT_PLTPAD: name = "PLTPAD"; break; |
1789 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1790 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1791 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1792 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1793 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1794 | case DT_VERSYM: name = "VERSYM"; break; |
1795 | case DT_VERDEF: name = "VERDEF"; break; | |
1796 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1797 | case DT_VERNEED: name = "VERNEED"; break; | |
1798 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1799 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1800 | case DT_USED: name = "USED"; break; |
b34976b6 | 1801 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1802 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1803 | } |
1804 | ||
ad9563d6 | 1805 | fprintf (f, " %-20s ", name); |
252b5132 | 1806 | if (! stringp) |
a1f3c56e AN |
1807 | { |
1808 | fprintf (f, "0x"); | |
1809 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1810 | } | |
252b5132 RH |
1811 | else |
1812 | { | |
1813 | const char *string; | |
dc810e39 | 1814 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1815 | |
dc810e39 | 1816 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1817 | if (string == NULL) |
1818 | goto error_return; | |
1819 | fprintf (f, "%s", string); | |
1820 | } | |
1821 | fprintf (f, "\n"); | |
1822 | } | |
1823 | ||
1824 | free (dynbuf); | |
1825 | dynbuf = NULL; | |
1826 | } | |
1827 | ||
1828 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1829 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1830 | { | |
fc0e6df6 | 1831 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1832 | return FALSE; |
252b5132 RH |
1833 | } |
1834 | ||
1835 | if (elf_dynverdef (abfd) != 0) | |
1836 | { | |
1837 | Elf_Internal_Verdef *t; | |
1838 | ||
1839 | fprintf (f, _("\nVersion definitions:\n")); | |
1840 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1841 | { | |
1842 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1843 | t->vd_flags, t->vd_hash, |
1844 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1845 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1846 | { |
1847 | Elf_Internal_Verdaux *a; | |
1848 | ||
1849 | fprintf (f, "\t"); | |
1850 | for (a = t->vd_auxptr->vda_nextptr; | |
1851 | a != NULL; | |
1852 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1853 | fprintf (f, "%s ", |
1854 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1855 | fprintf (f, "\n"); |
1856 | } | |
1857 | } | |
1858 | } | |
1859 | ||
1860 | if (elf_dynverref (abfd) != 0) | |
1861 | { | |
1862 | Elf_Internal_Verneed *t; | |
1863 | ||
1864 | fprintf (f, _("\nVersion References:\n")); | |
1865 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1866 | { | |
1867 | Elf_Internal_Vernaux *a; | |
1868 | ||
d0fb9a8d JJ |
1869 | fprintf (f, _(" required from %s:\n"), |
1870 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1871 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1872 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1873 | a->vna_flags, a->vna_other, |
1874 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1875 | } |
1876 | } | |
1877 | ||
b34976b6 | 1878 | return TRUE; |
252b5132 RH |
1879 | |
1880 | error_return: | |
1881 | if (dynbuf != NULL) | |
1882 | free (dynbuf); | |
b34976b6 | 1883 | return FALSE; |
252b5132 RH |
1884 | } |
1885 | ||
7e6e972f L |
1886 | /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE |
1887 | and return symbol version for symbol version itself. */ | |
bb4d2ac2 L |
1888 | |
1889 | const char * | |
1081065c L |
1890 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1891 | bfd_boolean base_p, | |
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))) | |
7e6e972f | 1909 | version_string = base_p ? "Base" : ""; |
bb4d2ac2 | 1910 | else if (vernum <= elf_tdata (abfd)->cverdefs) |
7e6e972f L |
1911 | { |
1912 | const char *nodename | |
1913 | = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
8d55d10a AM |
1914 | version_string = ""; |
1915 | if (base_p | |
1916 | || nodename == NULL | |
1917 | || symbol->name == NULL | |
1918 | || strcmp (symbol->name, nodename) != 0) | |
1919 | version_string = nodename; | |
7e6e972f | 1920 | } |
bb4d2ac2 L |
1921 | else |
1922 | { | |
1923 | Elf_Internal_Verneed *t; | |
1924 | ||
7a815dd5 | 1925 | version_string = _("<corrupt>"); |
bb4d2ac2 L |
1926 | for (t = elf_tdata (abfd)->verref; |
1927 | t != NULL; | |
1928 | t = t->vn_nextref) | |
1929 | { | |
1930 | Elf_Internal_Vernaux *a; | |
1931 | ||
1932 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1933 | { | |
1934 | if (a->vna_other == vernum) | |
1935 | { | |
1936 | version_string = a->vna_nodename; | |
1937 | break; | |
1938 | } | |
1939 | } | |
1940 | } | |
1941 | } | |
1942 | } | |
1943 | return version_string; | |
1944 | } | |
1945 | ||
252b5132 RH |
1946 | /* Display ELF-specific fields of a symbol. */ |
1947 | ||
1948 | void | |
217aa764 AM |
1949 | bfd_elf_print_symbol (bfd *abfd, |
1950 | void *filep, | |
1951 | asymbol *symbol, | |
1952 | bfd_print_symbol_type how) | |
252b5132 | 1953 | { |
a50b1753 | 1954 | FILE *file = (FILE *) filep; |
252b5132 RH |
1955 | switch (how) |
1956 | { | |
1957 | case bfd_print_symbol_name: | |
1958 | fprintf (file, "%s", symbol->name); | |
1959 | break; | |
1960 | case bfd_print_symbol_more: | |
1961 | fprintf (file, "elf "); | |
60b89a18 | 1962 | bfd_fprintf_vma (abfd, file, symbol->value); |
cd9af601 | 1963 | fprintf (file, " %x", symbol->flags); |
252b5132 RH |
1964 | break; |
1965 | case bfd_print_symbol_all: | |
1966 | { | |
4e8a9624 AM |
1967 | const char *section_name; |
1968 | const char *name = NULL; | |
9c5bfbb7 | 1969 | const struct elf_backend_data *bed; |
7a13edea | 1970 | unsigned char st_other; |
dbb410c3 | 1971 | bfd_vma val; |
bb4d2ac2 L |
1972 | const char *version_string; |
1973 | bfd_boolean hidden; | |
c044fabd | 1974 | |
252b5132 | 1975 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1976 | |
1977 | bed = get_elf_backend_data (abfd); | |
1978 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1979 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1980 | |
1981 | if (name == NULL) | |
1982 | { | |
7ee38065 | 1983 | name = symbol->name; |
217aa764 | 1984 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1985 | } |
1986 | ||
252b5132 RH |
1987 | fprintf (file, " %s\t", section_name); |
1988 | /* Print the "other" value for a symbol. For common symbols, | |
1989 | we've already printed the size; now print the alignment. | |
1990 | For other symbols, we have no specified alignment, and | |
1991 | we've printed the address; now print the size. */ | |
dcf6c779 | 1992 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1993 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1994 | else | |
1995 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1996 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1997 | |
1998 | /* If we have version information, print it. */ | |
60bb06bc L |
1999 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
2000 | symbol, | |
1081065c | 2001 | TRUE, |
60bb06bc | 2002 | &hidden); |
bb4d2ac2 | 2003 | if (version_string) |
252b5132 | 2004 | { |
bb4d2ac2 | 2005 | if (!hidden) |
252b5132 RH |
2006 | fprintf (file, " %-11s", version_string); |
2007 | else | |
2008 | { | |
2009 | int i; | |
2010 | ||
2011 | fprintf (file, " (%s)", version_string); | |
2012 | for (i = 10 - strlen (version_string); i > 0; --i) | |
2013 | putc (' ', file); | |
2014 | } | |
2015 | } | |
2016 | ||
2017 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 2018 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 2019 | |
7a13edea NC |
2020 | switch (st_other) |
2021 | { | |
2022 | case 0: break; | |
2023 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
2024 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
2025 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
2026 | default: | |
2027 | /* Some other non-defined flags are also present, so print | |
2028 | everything hex. */ | |
2029 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
2030 | } | |
252b5132 | 2031 | |
587ff49e | 2032 | fprintf (file, " %s", name); |
252b5132 RH |
2033 | } |
2034 | break; | |
2035 | } | |
2036 | } | |
252b5132 RH |
2037 | \f |
2038 | /* ELF .o/exec file reading */ | |
2039 | ||
c044fabd | 2040 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 2041 | |
b34976b6 | 2042 | bfd_boolean |
217aa764 | 2043 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 2044 | { |
4fbb74a6 AM |
2045 | Elf_Internal_Shdr *hdr; |
2046 | Elf_Internal_Ehdr *ehdr; | |
2047 | const struct elf_backend_data *bed; | |
90937f86 | 2048 | const char *name; |
bf67003b NC |
2049 | bfd_boolean ret = TRUE; |
2050 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 2051 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 2052 | static unsigned int nesting = 0; |
252b5132 | 2053 | |
4fbb74a6 AM |
2054 | if (shindex >= elf_numsections (abfd)) |
2055 | return FALSE; | |
2056 | ||
bf67003b NC |
2057 | if (++ nesting > 3) |
2058 | { | |
2059 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
67ce483b | 2060 | sections, with each the string indices pointing to the next in the |
bf67003b NC |
2061 | loop. Detect this here, by refusing to load a section that we are |
2062 | already in the process of loading. We only trigger this test if | |
2063 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
2064 | can expect to recurse at least once. |
2065 | ||
2066 | FIXME: It would be better if this array was attached to the bfd, | |
2067 | rather than being held in a static pointer. */ | |
2068 | ||
2069 | if (sections_being_created_abfd != abfd) | |
2070 | sections_being_created = NULL; | |
bf67003b NC |
2071 | if (sections_being_created == NULL) |
2072 | { | |
446f7ed5 AM |
2073 | size_t amt = elf_numsections (abfd) * sizeof (bfd_boolean); |
2074 | sections_being_created = (bfd_boolean *) bfd_zalloc (abfd, amt); | |
96d3b80f AM |
2075 | if (sections_being_created == NULL) |
2076 | return FALSE; | |
5a4b0ccc | 2077 | sections_being_created_abfd = abfd; |
bf67003b NC |
2078 | } |
2079 | if (sections_being_created [shindex]) | |
2080 | { | |
4eca0228 | 2081 | _bfd_error_handler |
871b3ab2 | 2082 | (_("%pB: warning: loop in section dependencies detected"), abfd); |
bf67003b NC |
2083 | return FALSE; |
2084 | } | |
2085 | sections_being_created [shindex] = TRUE; | |
2086 | } | |
2087 | ||
4fbb74a6 AM |
2088 | hdr = elf_elfsections (abfd)[shindex]; |
2089 | ehdr = elf_elfheader (abfd); | |
2090 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 2091 | hdr->sh_name); |
933d961a | 2092 | if (name == NULL) |
bf67003b | 2093 | goto fail; |
252b5132 | 2094 | |
4fbb74a6 | 2095 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
2096 | switch (hdr->sh_type) |
2097 | { | |
2098 | case SHT_NULL: | |
2099 | /* Inactive section. Throw it away. */ | |
bf67003b | 2100 | goto success; |
252b5132 | 2101 | |
bf67003b NC |
2102 | case SHT_PROGBITS: /* Normal section with contents. */ |
2103 | case SHT_NOBITS: /* .bss section. */ | |
2104 | case SHT_HASH: /* .hash section. */ | |
2105 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
2106 | case SHT_INIT_ARRAY: /* .init_array section. */ |
2107 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
2108 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 2109 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 2110 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
2111 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2112 | goto success; | |
252b5132 | 2113 | |
797fc050 | 2114 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 2115 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2116 | goto fail; |
2117 | ||
cfcac11d NC |
2118 | if (hdr->sh_link > elf_numsections (abfd)) |
2119 | { | |
caa83f8b | 2120 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
2121 | field set to SHN_BEFORE or SHN_AFTER. */ |
2122 | switch (bfd_get_arch (abfd)) | |
2123 | { | |
caa83f8b | 2124 | case bfd_arch_i386: |
cfcac11d NC |
2125 | case bfd_arch_sparc: |
2126 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
2127 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
2128 | break; | |
2129 | /* Otherwise fall through. */ | |
2130 | default: | |
bf67003b | 2131 | goto fail; |
cfcac11d NC |
2132 | } |
2133 | } | |
2134 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 2135 | goto fail; |
cfcac11d | 2136 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
2137 | { |
2138 | Elf_Internal_Shdr *dynsymhdr; | |
2139 | ||
2140 | /* The shared libraries distributed with hpux11 have a bogus | |
2141 | sh_link field for the ".dynamic" section. Find the | |
2142 | string table for the ".dynsym" section instead. */ | |
2143 | if (elf_dynsymtab (abfd) != 0) | |
2144 | { | |
2145 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
2146 | hdr->sh_link = dynsymhdr->sh_link; | |
2147 | } | |
2148 | else | |
2149 | { | |
2150 | unsigned int i, num_sec; | |
2151 | ||
2152 | num_sec = elf_numsections (abfd); | |
2153 | for (i = 1; i < num_sec; i++) | |
2154 | { | |
2155 | dynsymhdr = elf_elfsections (abfd)[i]; | |
2156 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
2157 | { | |
2158 | hdr->sh_link = dynsymhdr->sh_link; | |
2159 | break; | |
2160 | } | |
2161 | } | |
2162 | } | |
2163 | } | |
bf67003b | 2164 | goto success; |
797fc050 | 2165 | |
bf67003b | 2166 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 2167 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 2168 | goto success; |
252b5132 | 2169 | |
a50b2160 | 2170 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2171 | goto fail; |
2172 | ||
3337c1e5 | 2173 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
2174 | { |
2175 | if (hdr->sh_size != 0) | |
bf67003b | 2176 | goto fail; |
eee3b786 AM |
2177 | /* Some assemblers erroneously set sh_info to one with a |
2178 | zero sh_size. ld sees this as a global symbol count | |
2179 | of (unsigned) -1. Fix it here. */ | |
2180 | hdr->sh_info = 0; | |
bf67003b | 2181 | goto success; |
eee3b786 | 2182 | } |
bf67003b | 2183 | |
16ad13ec NC |
2184 | /* PR 18854: A binary might contain more than one symbol table. |
2185 | Unusual, but possible. Warn, but continue. */ | |
2186 | if (elf_onesymtab (abfd) != 0) | |
2187 | { | |
4eca0228 | 2188 | _bfd_error_handler |
695344c0 | 2189 | /* xgettext:c-format */ |
871b3ab2 | 2190 | (_("%pB: warning: multiple symbol tables detected" |
63a5468a | 2191 | " - ignoring the table in section %u"), |
16ad13ec NC |
2192 | abfd, shindex); |
2193 | goto success; | |
2194 | } | |
252b5132 | 2195 | elf_onesymtab (abfd) = shindex; |
6a40cf0c NC |
2196 | elf_symtab_hdr (abfd) = *hdr; |
2197 | elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); | |
252b5132 RH |
2198 | abfd->flags |= HAS_SYMS; |
2199 | ||
2200 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
2201 | SHF_ALLOC is set, and this is a shared object, then we also |
2202 | treat this section as a BFD section. We can not base the | |
2203 | decision purely on SHF_ALLOC, because that flag is sometimes | |
2204 | set in a relocatable object file, which would confuse the | |
2205 | linker. */ | |
252b5132 RH |
2206 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
2207 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
2208 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2209 | shindex)) | |
bf67003b | 2210 | goto fail; |
252b5132 | 2211 | |
1b3a8575 AM |
2212 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
2213 | can't read symbols without that section loaded as well. It | |
2214 | is most likely specified by the next section header. */ | |
6a40cf0c NC |
2215 | { |
2216 | elf_section_list * entry; | |
2217 | unsigned int i, num_sec; | |
1b3a8575 | 2218 | |
6a40cf0c NC |
2219 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2220 | if (entry->hdr.sh_link == shindex) | |
2221 | goto success; | |
2222 | ||
2223 | num_sec = elf_numsections (abfd); | |
2224 | for (i = shindex + 1; i < num_sec; i++) | |
2225 | { | |
2226 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2227 | ||
2228 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
2229 | && hdr2->sh_link == shindex) | |
2230 | break; | |
2231 | } | |
2232 | ||
2233 | if (i == num_sec) | |
2234 | for (i = 1; i < shindex; i++) | |
1b3a8575 AM |
2235 | { |
2236 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
6a40cf0c | 2237 | |
1b3a8575 AM |
2238 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX |
2239 | && hdr2->sh_link == shindex) | |
2240 | break; | |
2241 | } | |
6a40cf0c NC |
2242 | |
2243 | if (i != shindex) | |
2244 | ret = bfd_section_from_shdr (abfd, i); | |
2245 | /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ | |
2246 | goto success; | |
2247 | } | |
252b5132 | 2248 | |
bf67003b | 2249 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 2250 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 2251 | goto success; |
252b5132 | 2252 | |
a50b2160 | 2253 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2254 | goto fail; |
2255 | ||
eee3b786 AM |
2256 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
2257 | { | |
2258 | if (hdr->sh_size != 0) | |
bf67003b NC |
2259 | goto fail; |
2260 | ||
eee3b786 AM |
2261 | /* Some linkers erroneously set sh_info to one with a |
2262 | zero sh_size. ld sees this as a global symbol count | |
2263 | of (unsigned) -1. Fix it here. */ | |
2264 | hdr->sh_info = 0; | |
bf67003b | 2265 | goto success; |
eee3b786 | 2266 | } |
bf67003b | 2267 | |
16ad13ec NC |
2268 | /* PR 18854: A binary might contain more than one dynamic symbol table. |
2269 | Unusual, but possible. Warn, but continue. */ | |
2270 | if (elf_dynsymtab (abfd) != 0) | |
2271 | { | |
4eca0228 | 2272 | _bfd_error_handler |
695344c0 | 2273 | /* xgettext:c-format */ |
871b3ab2 | 2274 | (_("%pB: warning: multiple dynamic symbol tables detected" |
63a5468a | 2275 | " - ignoring the table in section %u"), |
16ad13ec NC |
2276 | abfd, shindex); |
2277 | goto success; | |
2278 | } | |
252b5132 RH |
2279 | elf_dynsymtab (abfd) = shindex; |
2280 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
2281 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2282 | abfd->flags |= HAS_SYMS; | |
2283 | ||
2284 | /* Besides being a symbol table, we also treat this as a regular | |
2285 | section, so that objcopy can handle it. */ | |
bf67003b NC |
2286 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2287 | goto success; | |
252b5132 | 2288 | |
bf67003b | 2289 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
6a40cf0c NC |
2290 | { |
2291 | elf_section_list * entry; | |
9ad5cbcf | 2292 | |
6a40cf0c NC |
2293 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2294 | if (entry->ndx == shindex) | |
2295 | goto success; | |
07d6d2b8 | 2296 | |
7a6e0d89 | 2297 | entry = bfd_alloc (abfd, sizeof (*entry)); |
6a40cf0c NC |
2298 | if (entry == NULL) |
2299 | goto fail; | |
2300 | entry->ndx = shindex; | |
2301 | entry->hdr = * hdr; | |
2302 | entry->next = elf_symtab_shndx_list (abfd); | |
2303 | elf_symtab_shndx_list (abfd) = entry; | |
2304 | elf_elfsections (abfd)[shindex] = & entry->hdr; | |
2305 | goto success; | |
2306 | } | |
9ad5cbcf | 2307 | |
bf67003b | 2308 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 2309 | if (hdr->bfd_section != NULL) |
bf67003b NC |
2310 | goto success; |
2311 | ||
252b5132 RH |
2312 | if (ehdr->e_shstrndx == shindex) |
2313 | { | |
2314 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
2315 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 2316 | goto success; |
252b5132 | 2317 | } |
bf67003b | 2318 | |
1b3a8575 AM |
2319 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
2320 | { | |
2321 | symtab_strtab: | |
2322 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
2323 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 2324 | goto success; |
1b3a8575 | 2325 | } |
bf67003b | 2326 | |
1b3a8575 AM |
2327 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
2328 | { | |
2329 | dynsymtab_strtab: | |
2330 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
2331 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
2332 | elf_elfsections (abfd)[shindex] = hdr; | |
2333 | /* We also treat this as a regular section, so that objcopy | |
2334 | can handle it. */ | |
bf67003b NC |
2335 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2336 | shindex); | |
2337 | goto success; | |
1b3a8575 | 2338 | } |
252b5132 | 2339 | |
1b3a8575 AM |
2340 | /* If the string table isn't one of the above, then treat it as a |
2341 | regular section. We need to scan all the headers to be sure, | |
2342 | just in case this strtab section appeared before the above. */ | |
2343 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2344 | { | |
2345 | unsigned int i, num_sec; | |
252b5132 | 2346 | |
1b3a8575 AM |
2347 | num_sec = elf_numsections (abfd); |
2348 | for (i = 1; i < num_sec; i++) | |
2349 | { | |
2350 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2351 | if (hdr2->sh_link == shindex) | |
2352 | { | |
933d961a JJ |
2353 | /* Prevent endless recursion on broken objects. */ |
2354 | if (i == shindex) | |
bf67003b | 2355 | goto fail; |
1b3a8575 | 2356 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 2357 | goto fail; |
1b3a8575 AM |
2358 | if (elf_onesymtab (abfd) == i) |
2359 | goto symtab_strtab; | |
2360 | if (elf_dynsymtab (abfd) == i) | |
2361 | goto dynsymtab_strtab; | |
2362 | } | |
2363 | } | |
2364 | } | |
bf67003b NC |
2365 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2366 | goto success; | |
252b5132 RH |
2367 | |
2368 | case SHT_REL: | |
2369 | case SHT_RELA: | |
2370 | /* *These* do a lot of work -- but build no sections! */ | |
2371 | { | |
2372 | asection *target_sect; | |
d4730f92 | 2373 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 2374 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 | 2375 | struct bfd_elf_section_data *esdt; |
252b5132 | 2376 | |
aa2ca951 JJ |
2377 | if (hdr->sh_entsize |
2378 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 2379 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 2380 | goto fail; |
a50b2160 | 2381 | |
03ae5f59 | 2382 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 2383 | if (hdr->sh_link >= num_sec) |
03ae5f59 | 2384 | { |
4eca0228 | 2385 | _bfd_error_handler |
695344c0 | 2386 | /* xgettext:c-format */ |
871b3ab2 | 2387 | (_("%pB: invalid link %u for reloc section %s (index %u)"), |
4eca0228 | 2388 | abfd, hdr->sh_link, name, shindex); |
bf67003b NC |
2389 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2390 | shindex); | |
2391 | goto success; | |
03ae5f59 ILT |
2392 | } |
2393 | ||
252b5132 RH |
2394 | /* For some incomprehensible reason Oracle distributes |
2395 | libraries for Solaris in which some of the objects have | |
2396 | bogus sh_link fields. It would be nice if we could just | |
2397 | reject them, but, unfortunately, some people need to use | |
2398 | them. We scan through the section headers; if we find only | |
2399 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
2400 | to it. I hope this doesn't break anything. |
2401 | ||
2402 | Don't do it on executable nor shared library. */ | |
2403 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
2404 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
2405 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
2406 | { | |
9ad5cbcf | 2407 | unsigned int scan; |
252b5132 RH |
2408 | int found; |
2409 | ||
2410 | found = 0; | |
9ad5cbcf | 2411 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2412 | { |
2413 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2414 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2415 | { | |
2416 | if (found != 0) | |
2417 | { | |
2418 | found = 0; | |
2419 | break; | |
2420 | } | |
2421 | found = scan; | |
2422 | } | |
2423 | } | |
2424 | if (found != 0) | |
2425 | hdr->sh_link = found; | |
2426 | } | |
2427 | ||
2428 | /* Get the symbol table. */ | |
1b3a8575 AM |
2429 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2430 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2431 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 2432 | goto fail; |
252b5132 | 2433 | |
a4bcd733 AM |
2434 | /* If this is an alloc section in an executable or shared |
2435 | library, or the reloc section does not use the main symbol | |
2436 | table we don't treat it as a reloc section. BFD can't | |
2437 | adequately represent such a section, so at least for now, | |
2438 | we don't try. We just present it as a normal section. We | |
2439 | also can't use it as a reloc section if it points to the | |
2440 | null section, an invalid section, another reloc section, or | |
2441 | its sh_link points to the null section. */ | |
2442 | if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0 | |
2443 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
83b89087 | 2444 | || hdr->sh_link == SHN_UNDEF |
a4bcd733 | 2445 | || hdr->sh_link != elf_onesymtab (abfd) |
185ef66d | 2446 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2447 | || hdr->sh_info >= num_sec |
2448 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2449 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2450 | { |
2451 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2452 | shindex); | |
2453 | goto success; | |
2454 | } | |
252b5132 RH |
2455 | |
2456 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2457 | goto fail; |
2458 | ||
252b5132 RH |
2459 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2460 | if (target_sect == NULL) | |
bf67003b | 2461 | goto fail; |
252b5132 | 2462 | |
d4730f92 BS |
2463 | esdt = elf_section_data (target_sect); |
2464 | if (hdr->sh_type == SHT_RELA) | |
2465 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2466 | else |
d4730f92 BS |
2467 | p_hdr = &esdt->rel.hdr; |
2468 | ||
a7ba3896 NC |
2469 | /* PR 17512: file: 0b4f81b7. |
2470 | Also see PR 24456, for a file which deliberately has two reloc | |
2471 | sections. */ | |
06614111 | 2472 | if (*p_hdr != NULL) |
a7ba3896 | 2473 | { |
a859124d | 2474 | if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex)) |
a8e14f4c NC |
2475 | { |
2476 | _bfd_error_handler | |
2477 | /* xgettext:c-format */ | |
a859124d AM |
2478 | (_("%pB: warning: secondary relocation section '%s' " |
2479 | "for section %pA found - ignoring"), | |
a8e14f4c NC |
2480 | abfd, name, target_sect); |
2481 | } | |
a7ba3896 NC |
2482 | goto success; |
2483 | } | |
a8e14f4c | 2484 | |
ef53be89 | 2485 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); |
d4730f92 | 2486 | if (hdr2 == NULL) |
bf67003b | 2487 | goto fail; |
252b5132 | 2488 | *hdr2 = *hdr; |
d4730f92 | 2489 | *p_hdr = hdr2; |
252b5132 | 2490 | elf_elfsections (abfd)[shindex] = hdr2; |
056bafd4 MR |
2491 | target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr) |
2492 | * bed->s->int_rels_per_ext_rel); | |
252b5132 RH |
2493 | target_sect->flags |= SEC_RELOC; |
2494 | target_sect->relocation = NULL; | |
2495 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2496 | /* In the section to which the relocations apply, mark whether |
2497 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2498 | if (hdr->sh_size != 0) |
d4730f92 BS |
2499 | { |
2500 | if (hdr->sh_type == SHT_RELA) | |
2501 | target_sect->use_rela_p = 1; | |
2502 | } | |
252b5132 | 2503 | abfd->flags |= HAS_RELOC; |
bf67003b | 2504 | goto success; |
252b5132 | 2505 | } |
252b5132 RH |
2506 | |
2507 | case SHT_GNU_verdef: | |
2508 | elf_dynverdef (abfd) = shindex; | |
2509 | elf_tdata (abfd)->dynverdef_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_versym: | |
a50b2160 | 2514 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2515 | goto fail; |
2516 | ||
252b5132 RH |
2517 | elf_dynversym (abfd) = shindex; |
2518 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2519 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2520 | goto success; | |
252b5132 RH |
2521 | |
2522 | case SHT_GNU_verneed: | |
2523 | elf_dynverref (abfd) = shindex; | |
2524 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2525 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2526 | goto success; | |
252b5132 RH |
2527 | |
2528 | case SHT_SHLIB: | |
bf67003b | 2529 | goto success; |
252b5132 | 2530 | |
dbb410c3 | 2531 | case SHT_GROUP: |
44534af3 | 2532 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2533 | goto fail; |
2534 | ||
6dc132d9 | 2535 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2536 | goto fail; |
2537 | ||
bf67003b | 2538 | goto success; |
dbb410c3 | 2539 | |
252b5132 | 2540 | default: |
104d59d1 JM |
2541 | /* Possibly an attributes section. */ |
2542 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2543 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2544 | { | |
2545 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2546 | goto fail; |
104d59d1 | 2547 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2548 | goto success; |
104d59d1 JM |
2549 | } |
2550 | ||
252b5132 | 2551 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2552 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2553 | goto success; |
3eb70a79 L |
2554 | |
2555 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2556 | { | |
2557 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2558 | /* FIXME: How to properly handle allocated section reserved | |
2559 | for applications? */ | |
4eca0228 | 2560 | _bfd_error_handler |
695344c0 | 2561 | /* xgettext:c-format */ |
871b3ab2 | 2562 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2563 | abfd, hdr->sh_type, name); |
3eb70a79 | 2564 | else |
bf67003b NC |
2565 | { |
2566 | /* Allow sections reserved for applications. */ | |
2567 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2568 | shindex); | |
2569 | goto success; | |
2570 | } | |
3eb70a79 L |
2571 | } |
2572 | else if (hdr->sh_type >= SHT_LOPROC | |
2573 | && hdr->sh_type <= SHT_HIPROC) | |
2574 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2575 | _bfd_error_handler |
695344c0 | 2576 | /* xgettext:c-format */ |
871b3ab2 | 2577 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2578 | abfd, hdr->sh_type, name); |
3eb70a79 | 2579 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) |
ff15b240 NC |
2580 | { |
2581 | /* Unrecognised OS-specific sections. */ | |
2582 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2583 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2584 | required to correctly process the section and the file should |
ff15b240 | 2585 | be rejected with an error message. */ |
4eca0228 | 2586 | _bfd_error_handler |
695344c0 | 2587 | /* xgettext:c-format */ |
871b3ab2 | 2588 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2589 | abfd, hdr->sh_type, name); |
ff15b240 | 2590 | else |
bf67003b NC |
2591 | { |
2592 | /* Otherwise it should be processed. */ | |
2593 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2594 | goto success; | |
2595 | } | |
ff15b240 | 2596 | } |
3eb70a79 L |
2597 | else |
2598 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2599 | _bfd_error_handler |
695344c0 | 2600 | /* xgettext:c-format */ |
871b3ab2 | 2601 | (_("%pB: unknown type [%#x] section `%s'"), |
76cfced5 | 2602 | abfd, hdr->sh_type, name); |
3eb70a79 | 2603 | |
bf67003b | 2604 | goto fail; |
252b5132 RH |
2605 | } |
2606 | ||
bf67003b NC |
2607 | fail: |
2608 | ret = FALSE; | |
2609 | success: | |
e5b470e2 | 2610 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2611 | sections_being_created [shindex] = FALSE; |
2612 | if (-- nesting == 0) | |
5a4b0ccc NC |
2613 | { |
2614 | sections_being_created = NULL; | |
2615 | sections_being_created_abfd = abfd; | |
2616 | } | |
bf67003b | 2617 | return ret; |
252b5132 RH |
2618 | } |
2619 | ||
87d72d41 | 2620 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2621 | |
87d72d41 AM |
2622 | Elf_Internal_Sym * |
2623 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2624 | bfd *abfd, | |
2625 | unsigned long r_symndx) | |
ec338859 | 2626 | { |
ec338859 AM |
2627 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2628 | ||
a5d1b3b5 AM |
2629 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2630 | { | |
2631 | Elf_Internal_Shdr *symtab_hdr; | |
2632 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2633 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2634 | |
a5d1b3b5 AM |
2635 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2636 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2637 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2638 | return NULL; |
9ad5cbcf | 2639 | |
a5d1b3b5 AM |
2640 | if (cache->abfd != abfd) |
2641 | { | |
2642 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2643 | cache->abfd = abfd; | |
2644 | } | |
2645 | cache->indx[ent] = r_symndx; | |
ec338859 | 2646 | } |
a5d1b3b5 | 2647 | |
87d72d41 | 2648 | return &cache->sym[ent]; |
ec338859 AM |
2649 | } |
2650 | ||
252b5132 RH |
2651 | /* Given an ELF section number, retrieve the corresponding BFD |
2652 | section. */ | |
2653 | ||
2654 | asection * | |
91d6fa6a | 2655 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2656 | { |
91d6fa6a | 2657 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2658 | return NULL; |
91d6fa6a | 2659 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2660 | } |
2661 | ||
b35d266b | 2662 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2663 | { |
0112cd26 | 2664 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 | 2665 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2666 | }; |
2667 | ||
b35d266b | 2668 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2669 | { |
0112cd26 | 2670 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
1ff6de03 | 2671 | { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 | 2672 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2673 | }; |
2674 | ||
b35d266b | 2675 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2676 | { |
07d6d2b8 AM |
2677 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2678 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2679 | /* There are more DWARF sections than these, but they needn't be added here |
2680 | unless you have to cope with broken compilers that don't emit section | |
2681 | attributes or you want to help the user writing assembler. */ | |
07d6d2b8 AM |
2682 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2683 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2684 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2685 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
0112cd26 | 2686 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 AM |
2687 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, |
2688 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2689 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2690 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2691 | }; |
2692 | ||
b35d266b | 2693 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2694 | { |
07d6d2b8 | 2695 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
6f9dbcd4 | 2696 | { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 | 2697 | { NULL, 0 , 0, 0, 0 } |
7f4d3958 L |
2698 | }; |
2699 | ||
b35d266b | 2700 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2701 | { |
0112cd26 | 2702 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2703 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
2704 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
2705 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
0112cd26 NC |
2706 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, |
2707 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
07d6d2b8 AM |
2708 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, |
2709 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2710 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2711 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2712 | }; |
2713 | ||
b35d266b | 2714 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2715 | { |
07d6d2b8 AM |
2716 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2717 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2718 | }; |
2719 | ||
b35d266b | 2720 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2721 | { |
07d6d2b8 | 2722 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
6f9dbcd4 | 2723 | { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2724 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, |
2725 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2726 | }; |
2727 | ||
b35d266b | 2728 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2729 | { |
0112cd26 | 2730 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 | 2731 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2732 | }; |
2733 | ||
b35d266b | 2734 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2735 | { |
0112cd26 | 2736 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
07d6d2b8 AM |
2737 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, |
2738 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2739 | }; |
2740 | ||
b35d266b | 2741 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2742 | { |
6f9dbcd4 | 2743 | { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
07d6d2b8 AM |
2744 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2745 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2746 | }; |
2747 | ||
b35d266b | 2748 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2749 | { |
0112cd26 NC |
2750 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2751 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
07d6d2b8 AM |
2752 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, |
2753 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2754 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2755 | }; |
2756 | ||
b35d266b | 2757 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2758 | { |
0112cd26 NC |
2759 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2760 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2761 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2762 | /* See struct bfd_elf_special_section declaration for the semantics of |
2763 | this special case where .prefix_length != strlen (.prefix). */ | |
2764 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
07d6d2b8 | 2765 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2766 | }; |
2767 | ||
b35d266b | 2768 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2769 | { |
07d6d2b8 AM |
2770 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2771 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
0112cd26 | 2772 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, |
07d6d2b8 | 2773 | { NULL, 0, 0, 0, 0 } |
7f4d3958 L |
2774 | }; |
2775 | ||
1b315056 CS |
2776 | static const struct bfd_elf_special_section special_sections_z[] = |
2777 | { | |
07d6d2b8 AM |
2778 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, |
2779 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
1b315056 CS |
2780 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, |
2781 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
07d6d2b8 | 2782 | { NULL, 0, 0, 0, 0 } |
1b315056 CS |
2783 | }; |
2784 | ||
e4c93b56 | 2785 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2786 | { |
7f4d3958 | 2787 | special_sections_b, /* 'b' */ |
98ece1b3 | 2788 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2789 | special_sections_d, /* 'd' */ |
2790 | NULL, /* 'e' */ | |
2791 | special_sections_f, /* 'f' */ | |
2792 | special_sections_g, /* 'g' */ | |
2793 | special_sections_h, /* 'h' */ | |
2794 | special_sections_i, /* 'i' */ | |
2795 | NULL, /* 'j' */ | |
2796 | NULL, /* 'k' */ | |
2797 | special_sections_l, /* 'l' */ | |
2798 | NULL, /* 'm' */ | |
2799 | special_sections_n, /* 'n' */ | |
2800 | NULL, /* 'o' */ | |
2801 | special_sections_p, /* 'p' */ | |
2802 | NULL, /* 'q' */ | |
2803 | special_sections_r, /* 'r' */ | |
2804 | special_sections_s, /* 's' */ | |
2805 | special_sections_t, /* 't' */ | |
1b315056 CS |
2806 | NULL, /* 'u' */ |
2807 | NULL, /* 'v' */ | |
2808 | NULL, /* 'w' */ | |
2809 | NULL, /* 'x' */ | |
2810 | NULL, /* 'y' */ | |
2811 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2812 | }; |
2813 | ||
551b43fd AM |
2814 | const struct bfd_elf_special_section * |
2815 | _bfd_elf_get_special_section (const char *name, | |
2816 | const struct bfd_elf_special_section *spec, | |
2817 | unsigned int rela) | |
2f89ff8d L |
2818 | { |
2819 | int i; | |
7f4d3958 | 2820 | int len; |
7f4d3958 | 2821 | |
551b43fd | 2822 | len = strlen (name); |
7f4d3958 | 2823 | |
551b43fd | 2824 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2825 | { |
2826 | int suffix_len; | |
551b43fd | 2827 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2828 | |
2829 | if (len < prefix_len) | |
2830 | continue; | |
551b43fd | 2831 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2832 | continue; |
2833 | ||
551b43fd | 2834 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2835 | if (suffix_len <= 0) |
2836 | { | |
2837 | if (name[prefix_len] != 0) | |
2838 | { | |
2839 | if (suffix_len == 0) | |
2840 | continue; | |
2841 | if (name[prefix_len] != '.' | |
2842 | && (suffix_len == -2 | |
551b43fd | 2843 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2844 | continue; |
2845 | } | |
2846 | } | |
2847 | else | |
2848 | { | |
2849 | if (len < prefix_len + suffix_len) | |
2850 | continue; | |
2851 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2852 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2853 | suffix_len) != 0) |
2854 | continue; | |
2855 | } | |
551b43fd | 2856 | return &spec[i]; |
7dcb9820 | 2857 | } |
2f89ff8d L |
2858 | |
2859 | return NULL; | |
2860 | } | |
2861 | ||
7dcb9820 | 2862 | const struct bfd_elf_special_section * |
29ef7005 | 2863 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2864 | { |
551b43fd AM |
2865 | int i; |
2866 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2867 | const struct elf_backend_data *bed; |
2f89ff8d L |
2868 | |
2869 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2870 | if (sec->name == NULL) |
2871 | return NULL; | |
2f89ff8d | 2872 | |
29ef7005 L |
2873 | bed = get_elf_backend_data (abfd); |
2874 | spec = bed->special_sections; | |
2875 | if (spec) | |
2876 | { | |
2877 | spec = _bfd_elf_get_special_section (sec->name, | |
2878 | bed->special_sections, | |
2879 | sec->use_rela_p); | |
2880 | if (spec != NULL) | |
2881 | return spec; | |
2882 | } | |
2883 | ||
551b43fd AM |
2884 | if (sec->name[0] != '.') |
2885 | return NULL; | |
2f89ff8d | 2886 | |
551b43fd | 2887 | i = sec->name[1] - 'b'; |
1b315056 | 2888 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2889 | return NULL; |
2890 | ||
2891 | spec = special_sections[i]; | |
2f89ff8d | 2892 | |
551b43fd AM |
2893 | if (spec == NULL) |
2894 | return NULL; | |
2895 | ||
2896 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2897 | } |
2898 | ||
b34976b6 | 2899 | bfd_boolean |
217aa764 | 2900 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2901 | { |
2902 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2903 | const struct elf_backend_data *bed; |
7dcb9820 | 2904 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2905 | |
f0abc2a1 AM |
2906 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2907 | if (sdata == NULL) | |
2908 | { | |
a50b1753 | 2909 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
07d6d2b8 | 2910 | sizeof (*sdata)); |
f0abc2a1 AM |
2911 | if (sdata == NULL) |
2912 | return FALSE; | |
217aa764 | 2913 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2914 | } |
bf572ba0 | 2915 | |
551b43fd AM |
2916 | /* Indicate whether or not this section should use RELA relocations. */ |
2917 | bed = get_elf_backend_data (abfd); | |
2918 | sec->use_rela_p = bed->default_use_rela_p; | |
2919 | ||
8c803a2d AM |
2920 | /* Set up ELF section type and flags for newly created sections, if |
2921 | there is an ABI mandated section. */ | |
2922 | ssect = (*bed->get_sec_type_attr) (abfd, sec); | |
2923 | if (ssect != NULL) | |
2f89ff8d | 2924 | { |
8c803a2d AM |
2925 | elf_section_type (sec) = ssect->type; |
2926 | elf_section_flags (sec) = ssect->attr; | |
2f89ff8d L |
2927 | } |
2928 | ||
f592407e | 2929 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2930 | } |
2931 | ||
2932 | /* Create a new bfd section from an ELF program header. | |
2933 | ||
2934 | Since program segments have no names, we generate a synthetic name | |
2935 | of the form segment<NUM>, where NUM is generally the index in the | |
2936 | program header table. For segments that are split (see below) we | |
2937 | generate the names segment<NUM>a and segment<NUM>b. | |
2938 | ||
2939 | Note that some program segments may have a file size that is different than | |
2940 | (less than) the memory size. All this means is that at execution the | |
2941 | system must allocate the amount of memory specified by the memory size, | |
2942 | but only initialize it with the first "file size" bytes read from the | |
2943 | file. This would occur for example, with program segments consisting | |
2944 | of combined data+bss. | |
2945 | ||
2946 | To handle the above situation, this routine generates TWO bfd sections | |
2947 | for the single program segment. The first has the length specified by | |
2948 | the file size of the segment, and the second has the length specified | |
2949 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2950 | into its initialized and uninitialized parts. |
252b5132 RH |
2951 | |
2952 | */ | |
2953 | ||
b34976b6 | 2954 | bfd_boolean |
217aa764 AM |
2955 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2956 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2957 | int hdr_index, |
a50b1753 | 2958 | const char *type_name) |
252b5132 RH |
2959 | { |
2960 | asection *newsect; | |
2961 | char *name; | |
2962 | char namebuf[64]; | |
d4c88bbb | 2963 | size_t len; |
252b5132 | 2964 | int split; |
502794d4 | 2965 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
252b5132 RH |
2966 | |
2967 | split = ((hdr->p_memsz > 0) | |
2968 | && (hdr->p_filesz > 0) | |
2969 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2970 | |
2971 | if (hdr->p_filesz > 0) | |
252b5132 | 2972 | { |
91d6fa6a | 2973 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2974 | len = strlen (namebuf) + 1; |
a50b1753 | 2975 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2976 | if (!name) |
2977 | return FALSE; | |
2978 | memcpy (name, namebuf, len); | |
2979 | newsect = bfd_make_section (abfd, name); | |
2980 | if (newsect == NULL) | |
2981 | return FALSE; | |
502794d4 CE |
2982 | newsect->vma = hdr->p_vaddr / opb; |
2983 | newsect->lma = hdr->p_paddr / opb; | |
d5191d0c AM |
2984 | newsect->size = hdr->p_filesz; |
2985 | newsect->filepos = hdr->p_offset; | |
2986 | newsect->flags |= SEC_HAS_CONTENTS; | |
2987 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2988 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2989 | { |
d5191d0c AM |
2990 | newsect->flags |= SEC_ALLOC; |
2991 | newsect->flags |= SEC_LOAD; | |
2992 | if (hdr->p_flags & PF_X) | |
2993 | { | |
2994 | /* FIXME: all we known is that it has execute PERMISSION, | |
2995 | may be data. */ | |
2996 | newsect->flags |= SEC_CODE; | |
2997 | } | |
2998 | } | |
2999 | if (!(hdr->p_flags & PF_W)) | |
3000 | { | |
3001 | newsect->flags |= SEC_READONLY; | |
252b5132 | 3002 | } |
252b5132 RH |
3003 | } |
3004 | ||
d5191d0c | 3005 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 3006 | { |
d5191d0c AM |
3007 | bfd_vma align; |
3008 | ||
91d6fa6a | 3009 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 3010 | len = strlen (namebuf) + 1; |
a50b1753 | 3011 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 3012 | if (!name) |
b34976b6 | 3013 | return FALSE; |
d4c88bbb | 3014 | memcpy (name, namebuf, len); |
252b5132 RH |
3015 | newsect = bfd_make_section (abfd, name); |
3016 | if (newsect == NULL) | |
b34976b6 | 3017 | return FALSE; |
502794d4 CE |
3018 | newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb; |
3019 | newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb; | |
eea6121a | 3020 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
3021 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
3022 | align = newsect->vma & -newsect->vma; | |
3023 | if (align == 0 || align > hdr->p_align) | |
3024 | align = hdr->p_align; | |
3025 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
3026 | if (hdr->p_type == PT_LOAD) |
3027 | { | |
d5191d0c AM |
3028 | /* Hack for gdb. Segments that have not been modified do |
3029 | not have their contents written to a core file, on the | |
3030 | assumption that a debugger can find the contents in the | |
3031 | executable. We flag this case by setting the fake | |
3032 | section size to zero. Note that "real" bss sections will | |
3033 | always have their contents dumped to the core file. */ | |
3034 | if (bfd_get_format (abfd) == bfd_core) | |
3035 | newsect->size = 0; | |
252b5132 RH |
3036 | newsect->flags |= SEC_ALLOC; |
3037 | if (hdr->p_flags & PF_X) | |
3038 | newsect->flags |= SEC_CODE; | |
3039 | } | |
3040 | if (!(hdr->p_flags & PF_W)) | |
3041 | newsect->flags |= SEC_READONLY; | |
3042 | } | |
3043 | ||
b34976b6 | 3044 | return TRUE; |
252b5132 RH |
3045 | } |
3046 | ||
864619bb KS |
3047 | static bfd_boolean |
3048 | _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset) | |
3049 | { | |
3050 | /* The return value is ignored. Build-ids are considered optional. */ | |
3051 | if (templ->xvec->flavour == bfd_target_elf_flavour) | |
3052 | return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id) | |
3053 | (templ, offset); | |
3054 | return FALSE; | |
3055 | } | |
3056 | ||
b34976b6 | 3057 | bfd_boolean |
91d6fa6a | 3058 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 3059 | { |
9c5bfbb7 | 3060 | const struct elf_backend_data *bed; |
20cfcaae NC |
3061 | |
3062 | switch (hdr->p_type) | |
3063 | { | |
3064 | case PT_NULL: | |
91d6fa6a | 3065 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
3066 | |
3067 | case PT_LOAD: | |
864619bb KS |
3068 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load")) |
3069 | return FALSE; | |
3070 | if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL) | |
3071 | _bfd_elf_core_find_build_id (abfd, hdr->p_offset); | |
3072 | return TRUE; | |
20cfcaae NC |
3073 | |
3074 | case PT_DYNAMIC: | |
91d6fa6a | 3075 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
3076 | |
3077 | case PT_INTERP: | |
91d6fa6a | 3078 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
3079 | |
3080 | case PT_NOTE: | |
91d6fa6a | 3081 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 3082 | return FALSE; |
276da9b3 L |
3083 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz, |
3084 | hdr->p_align)) | |
b34976b6 AM |
3085 | return FALSE; |
3086 | return TRUE; | |
20cfcaae NC |
3087 | |
3088 | case PT_SHLIB: | |
91d6fa6a | 3089 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
3090 | |
3091 | case PT_PHDR: | |
91d6fa6a | 3092 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 3093 | |
811072d8 | 3094 | case PT_GNU_EH_FRAME: |
91d6fa6a | 3095 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
3096 | "eh_frame_hdr"); |
3097 | ||
2b05f1b7 | 3098 | case PT_GNU_STACK: |
91d6fa6a | 3099 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 3100 | |
8c37241b | 3101 | case PT_GNU_RELRO: |
91d6fa6a | 3102 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 3103 | |
20cfcaae | 3104 | default: |
8c1acd09 | 3105 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 3106 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 3107 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
3108 | } |
3109 | } | |
3110 | ||
d4730f92 BS |
3111 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
3112 | REL or RELA. */ | |
3113 | ||
3114 | Elf_Internal_Shdr * | |
3115 | _bfd_elf_single_rel_hdr (asection *sec) | |
3116 | { | |
3117 | if (elf_section_data (sec)->rel.hdr) | |
3118 | { | |
3119 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
3120 | return elf_section_data (sec)->rel.hdr; | |
3121 | } | |
3122 | else | |
3123 | return elf_section_data (sec)->rela.hdr; | |
3124 | } | |
3125 | ||
3e19fb8f L |
3126 | static bfd_boolean |
3127 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
3128 | Elf_Internal_Shdr *rel_hdr, | |
3129 | const char *sec_name, | |
3130 | bfd_boolean use_rela_p) | |
3131 | { | |
3132 | char *name = (char *) bfd_alloc (abfd, | |
3133 | sizeof ".rela" + strlen (sec_name)); | |
3134 | if (name == NULL) | |
3135 | return FALSE; | |
3136 | ||
3137 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
3138 | rel_hdr->sh_name = | |
3139 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
3140 | FALSE); | |
3141 | if (rel_hdr->sh_name == (unsigned int) -1) | |
3142 | return FALSE; | |
3143 | ||
3144 | return TRUE; | |
3145 | } | |
3146 | ||
d4730f92 BS |
3147 | /* Allocate and initialize a section-header for a new reloc section, |
3148 | containing relocations against ASECT. It is stored in RELDATA. If | |
3149 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
3150 | relocations. */ | |
23bc299b | 3151 | |
5d13b3b3 | 3152 | static bfd_boolean |
217aa764 | 3153 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 3154 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 3155 | const char *sec_name, |
3e19fb8f L |
3156 | bfd_boolean use_rela_p, |
3157 | bfd_boolean delay_st_name_p) | |
23bc299b | 3158 | { |
d4730f92 | 3159 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 3160 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3161 | |
d4730f92 | 3162 | BFD_ASSERT (reldata->hdr == NULL); |
ef53be89 | 3163 | rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); |
d4730f92 | 3164 | reldata->hdr = rel_hdr; |
23bc299b | 3165 | |
3e19fb8f L |
3166 | if (delay_st_name_p) |
3167 | rel_hdr->sh_name = (unsigned int) -1; | |
3168 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
3169 | use_rela_p)) | |
b34976b6 | 3170 | return FALSE; |
23bc299b MM |
3171 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
3172 | rel_hdr->sh_entsize = (use_rela_p | |
3173 | ? bed->s->sizeof_rela | |
3174 | : bed->s->sizeof_rel); | |
72de5009 | 3175 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
28e07a05 | 3176 | rel_hdr->sh_flags = 0; |
23bc299b MM |
3177 | rel_hdr->sh_addr = 0; |
3178 | rel_hdr->sh_size = 0; | |
3179 | rel_hdr->sh_offset = 0; | |
3180 | ||
b34976b6 | 3181 | return TRUE; |
23bc299b MM |
3182 | } |
3183 | ||
94be91de JB |
3184 | /* Return the default section type based on the passed in section flags. */ |
3185 | ||
3186 | int | |
3187 | bfd_elf_get_default_section_type (flagword flags) | |
3188 | { | |
0e41bebb | 3189 | if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0 |
2e76e85a | 3190 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
3191 | return SHT_NOBITS; |
3192 | return SHT_PROGBITS; | |
3193 | } | |
3194 | ||
d4730f92 BS |
3195 | struct fake_section_arg |
3196 | { | |
3197 | struct bfd_link_info *link_info; | |
3198 | bfd_boolean failed; | |
3199 | }; | |
3200 | ||
252b5132 RH |
3201 | /* Set up an ELF internal section header for a section. */ |
3202 | ||
252b5132 | 3203 | static void |
d4730f92 | 3204 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 3205 | { |
d4730f92 | 3206 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 3207 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3208 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 3209 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 3210 | unsigned int sh_type; |
0ce398f1 | 3211 | const char *name = asect->name; |
3e19fb8f | 3212 | bfd_boolean delay_st_name_p = FALSE; |
233bf4f8 | 3213 | bfd_vma mask; |
252b5132 | 3214 | |
d4730f92 | 3215 | if (arg->failed) |
252b5132 RH |
3216 | { |
3217 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 3218 | loop. */ |
252b5132 RH |
3219 | return; |
3220 | } | |
3221 | ||
d4730f92 | 3222 | this_hdr = &esd->this_hdr; |
252b5132 | 3223 | |
f6fe1ccd | 3224 | if (arg->link_info) |
0ce398f1 | 3225 | { |
f6fe1ccd L |
3226 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
3227 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
3228 | && (asect->flags & SEC_DEBUGGING) | |
3229 | && name[1] == 'd' | |
3230 | && name[6] == '_') | |
3231 | { | |
3232 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
3233 | compressed. */ | |
3234 | asect->flags |= SEC_ELF_COMPRESS; | |
dd905818 | 3235 | /* If this section will be compressed, delay adding section |
3e19fb8f L |
3236 | name to section name section after it is compressed in |
3237 | _bfd_elf_assign_file_positions_for_non_load. */ | |
3238 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
3239 | } |
3240 | } | |
3241 | else if ((asect->flags & SEC_ELF_RENAME)) | |
3242 | { | |
3243 | /* objcopy: rename output DWARF debug section. */ | |
3244 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
3245 | { | |
3246 | /* When we decompress or compress with SHF_COMPRESSED, | |
3247 | convert section name from .zdebug_* to .debug_* if | |
3248 | needed. */ | |
3249 | if (name[1] == 'z') | |
3250 | { | |
3251 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
3252 | if (new_name == NULL) | |
3253 | { | |
3254 | arg->failed = TRUE; | |
3255 | return; | |
3256 | } | |
3257 | name = new_name; | |
3258 | } | |
3259 | } | |
3260 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 3261 | { |
f6fe1ccd L |
3262 | /* PR binutils/18087: Compression does not always make a |
3263 | section smaller. So only rename the section when | |
3264 | compression has actually taken place. If input section | |
3265 | name is .zdebug_*, we should never compress it again. */ | |
3266 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
3267 | if (new_name == NULL) |
3268 | { | |
3269 | arg->failed = TRUE; | |
3270 | return; | |
3271 | } | |
f6fe1ccd L |
3272 | BFD_ASSERT (name[1] != 'z'); |
3273 | name = new_name; | |
0ce398f1 L |
3274 | } |
3275 | } | |
3276 | ||
3e19fb8f L |
3277 | if (delay_st_name_p) |
3278 | this_hdr->sh_name = (unsigned int) -1; | |
3279 | else | |
252b5132 | 3280 | { |
3e19fb8f L |
3281 | this_hdr->sh_name |
3282 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
3283 | name, FALSE); | |
3284 | if (this_hdr->sh_name == (unsigned int) -1) | |
3285 | { | |
3286 | arg->failed = TRUE; | |
3287 | return; | |
3288 | } | |
252b5132 RH |
3289 | } |
3290 | ||
a4d8e49b | 3291 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
3292 | |
3293 | if ((asect->flags & SEC_ALLOC) != 0 | |
3294 | || asect->user_set_vma) | |
502794d4 | 3295 | this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect); |
252b5132 RH |
3296 | else |
3297 | this_hdr->sh_addr = 0; | |
3298 | ||
3299 | this_hdr->sh_offset = 0; | |
eea6121a | 3300 | this_hdr->sh_size = asect->size; |
252b5132 | 3301 | this_hdr->sh_link = 0; |
c86934ce NC |
3302 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
3303 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
3304 | { | |
4eca0228 | 3305 | _bfd_error_handler |
695344c0 | 3306 | /* xgettext:c-format */ |
9793eb77 | 3307 | (_("%pB: error: alignment power %d of section `%pA' is too big"), |
c08bb8dd | 3308 | abfd, asect->alignment_power, asect); |
c86934ce NC |
3309 | arg->failed = TRUE; |
3310 | return; | |
3311 | } | |
233bf4f8 AM |
3312 | /* Set sh_addralign to the highest power of two given by alignment |
3313 | consistent with the section VMA. Linker scripts can force VMA. */ | |
3314 | mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr; | |
3315 | this_hdr->sh_addralign = mask & -mask; | |
252b5132 RH |
3316 | /* The sh_entsize and sh_info fields may have been set already by |
3317 | copy_private_section_data. */ | |
3318 | ||
3319 | this_hdr->bfd_section = asect; | |
3320 | this_hdr->contents = NULL; | |
3321 | ||
3cddba1e L |
3322 | /* If the section type is unspecified, we set it based on |
3323 | asect->flags. */ | |
98ece1b3 AM |
3324 | if ((asect->flags & SEC_GROUP) != 0) |
3325 | sh_type = SHT_GROUP; | |
98ece1b3 | 3326 | else |
94be91de | 3327 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 3328 | |
3cddba1e | 3329 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
3330 | this_hdr->sh_type = sh_type; |
3331 | else if (this_hdr->sh_type == SHT_NOBITS | |
3332 | && sh_type == SHT_PROGBITS | |
3333 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 3334 | { |
98ece1b3 AM |
3335 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
3336 | allow the link to proceed. This can happen when users link | |
3337 | non-bss input sections to bss output sections, or emit data | |
3338 | to a bss output section via a linker script. */ | |
4eca0228 | 3339 | _bfd_error_handler |
871b3ab2 | 3340 | (_("warning: section `%pA' type changed to PROGBITS"), asect); |
98ece1b3 | 3341 | this_hdr->sh_type = sh_type; |
3cddba1e L |
3342 | } |
3343 | ||
2f89ff8d | 3344 | switch (this_hdr->sh_type) |
252b5132 | 3345 | { |
2f89ff8d | 3346 | default: |
2f89ff8d L |
3347 | break; |
3348 | ||
3349 | case SHT_STRTAB: | |
2f89ff8d L |
3350 | case SHT_NOTE: |
3351 | case SHT_NOBITS: | |
3352 | case SHT_PROGBITS: | |
3353 | break; | |
606851fb AM |
3354 | |
3355 | case SHT_INIT_ARRAY: | |
3356 | case SHT_FINI_ARRAY: | |
3357 | case SHT_PREINIT_ARRAY: | |
3358 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
3359 | break; | |
2f89ff8d L |
3360 | |
3361 | case SHT_HASH: | |
c7ac6ff8 | 3362 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 3363 | break; |
5de3bf90 | 3364 | |
2f89ff8d | 3365 | case SHT_DYNSYM: |
252b5132 | 3366 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
3367 | break; |
3368 | ||
3369 | case SHT_DYNAMIC: | |
252b5132 | 3370 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
3371 | break; |
3372 | ||
3373 | case SHT_RELA: | |
3374 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
3375 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
3376 | break; | |
3377 | ||
3378 | case SHT_REL: | |
3379 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
3380 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
3381 | break; | |
3382 | ||
3383 | case SHT_GNU_versym: | |
252b5132 | 3384 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
3385 | break; |
3386 | ||
3387 | case SHT_GNU_verdef: | |
252b5132 RH |
3388 | this_hdr->sh_entsize = 0; |
3389 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3390 | cverdefs. The linker will set cverdefs, but sh_info will be |
3391 | zero. */ | |
252b5132 RH |
3392 | if (this_hdr->sh_info == 0) |
3393 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
3394 | else | |
3395 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
3396 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
3397 | break; |
3398 | ||
3399 | case SHT_GNU_verneed: | |
252b5132 RH |
3400 | this_hdr->sh_entsize = 0; |
3401 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3402 | cverrefs. The linker will set cverrefs, but sh_info will be |
3403 | zero. */ | |
252b5132 RH |
3404 | if (this_hdr->sh_info == 0) |
3405 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
3406 | else | |
3407 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
3408 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
3409 | break; |
3410 | ||
3411 | case SHT_GROUP: | |
1783205a | 3412 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 3413 | break; |
fdc90cb4 JJ |
3414 | |
3415 | case SHT_GNU_HASH: | |
3416 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
3417 | break; | |
dbb410c3 | 3418 | } |
252b5132 RH |
3419 | |
3420 | if ((asect->flags & SEC_ALLOC) != 0) | |
3421 | this_hdr->sh_flags |= SHF_ALLOC; | |
3422 | if ((asect->flags & SEC_READONLY) == 0) | |
3423 | this_hdr->sh_flags |= SHF_WRITE; | |
3424 | if ((asect->flags & SEC_CODE) != 0) | |
3425 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
3426 | if ((asect->flags & SEC_MERGE) != 0) |
3427 | { | |
3428 | this_hdr->sh_flags |= SHF_MERGE; | |
3429 | this_hdr->sh_entsize = asect->entsize; | |
f5fa8ca2 | 3430 | } |
84865015 NC |
3431 | if ((asect->flags & SEC_STRINGS) != 0) |
3432 | this_hdr->sh_flags |= SHF_STRINGS; | |
1126897b | 3433 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 3434 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 3435 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
3436 | { |
3437 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
3438 | if (asect->size == 0 |
3439 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 3440 | { |
3a800eb9 | 3441 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 3442 | |
704afa60 | 3443 | this_hdr->sh_size = 0; |
3a800eb9 AM |
3444 | if (o != NULL) |
3445 | { | |
704afa60 | 3446 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
3447 | if (this_hdr->sh_size != 0) |
3448 | this_hdr->sh_type = SHT_NOBITS; | |
3449 | } | |
704afa60 JJ |
3450 | } |
3451 | } | |
18ae9cc1 L |
3452 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3453 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3454 | |
d4730f92 BS |
3455 | /* If the section has relocs, set up a section header for the |
3456 | SHT_REL[A] section. If two relocation sections are required for | |
3457 | this section, it is up to the processor-specific back-end to | |
3458 | create the other. */ | |
3459 | if ((asect->flags & SEC_RELOC) != 0) | |
3460 | { | |
3461 | /* When doing a relocatable link, create both REL and RELA sections if | |
3462 | needed. */ | |
3463 | if (arg->link_info | |
3464 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3465 | && esd->rel.count + esd->rela.count > 0 | |
0e1862bb L |
3466 | && (bfd_link_relocatable (arg->link_info) |
3467 | || arg->link_info->emitrelocations)) | |
d4730f92 BS |
3468 | { |
3469 | if (esd->rel.count && esd->rel.hdr == NULL | |
28e07a05 | 3470 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, |
db4677b8 | 3471 | FALSE, delay_st_name_p)) |
d4730f92 BS |
3472 | { |
3473 | arg->failed = TRUE; | |
3474 | return; | |
3475 | } | |
3476 | if (esd->rela.count && esd->rela.hdr == NULL | |
28e07a05 | 3477 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, |
db4677b8 | 3478 | TRUE, delay_st_name_p)) |
d4730f92 BS |
3479 | { |
3480 | arg->failed = TRUE; | |
3481 | return; | |
3482 | } | |
3483 | } | |
3484 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3485 | (asect->use_rela_p | |
3486 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3487 | name, |
3e19fb8f L |
3488 | asect->use_rela_p, |
3489 | delay_st_name_p)) | |
db4677b8 | 3490 | { |
d4730f92 | 3491 | arg->failed = TRUE; |
db4677b8 AM |
3492 | return; |
3493 | } | |
d4730f92 BS |
3494 | } |
3495 | ||
252b5132 | 3496 | /* Check for processor-specific section types. */ |
0414f35b | 3497 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3498 | if (bed->elf_backend_fake_sections |
3499 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
db4677b8 AM |
3500 | { |
3501 | arg->failed = TRUE; | |
3502 | return; | |
3503 | } | |
252b5132 | 3504 | |
42bb2e33 | 3505 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3506 | { |
3507 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3508 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3509 | this_hdr->sh_type = sh_type; |
3510 | } | |
252b5132 RH |
3511 | } |
3512 | ||
bcacc0f5 AM |
3513 | /* Fill in the contents of a SHT_GROUP section. Called from |
3514 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3515 | when ELF targets use the generic linker, ld. Called for ld -r | |
3516 | from bfd_elf_final_link. */ | |
dbb410c3 | 3517 | |
1126897b | 3518 | void |
217aa764 | 3519 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3520 | { |
a50b1753 | 3521 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3522 | asection *elt, *first; |
dbb410c3 | 3523 | unsigned char *loc; |
b34976b6 | 3524 | bfd_boolean gas; |
dbb410c3 | 3525 | |
7e4111ad L |
3526 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3527 | elfxx-ia64.c. */ | |
ce5aecf8 AM |
3528 | if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP |
3529 | || sec->size == 0 | |
dbb410c3 AM |
3530 | || *failedptr) |
3531 | return; | |
3532 | ||
bcacc0f5 AM |
3533 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3534 | { | |
3535 | unsigned long symindx = 0; | |
3536 | ||
3537 | /* elf_group_id will have been set up by objcopy and the | |
3538 | generic linker. */ | |
3539 | if (elf_group_id (sec) != NULL) | |
3540 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3541 | |
bcacc0f5 AM |
3542 | if (symindx == 0) |
3543 | { | |
3544 | /* If called from the assembler, swap_out_syms will have set up | |
6a541707 NC |
3545 | elf_section_syms. |
3546 | PR 25699: A corrupt input file could contain bogus group info. */ | |
3547 | if (elf_section_syms (abfd) == NULL) | |
3548 | { | |
3549 | *failedptr = TRUE; | |
3550 | return; | |
3551 | } | |
bcacc0f5 AM |
3552 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; |
3553 | } | |
3554 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3555 | } | |
3556 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3557 | { |
bcacc0f5 AM |
3558 | /* The ELF backend linker sets sh_info to -2 when the group |
3559 | signature symbol is global, and thus the index can't be | |
3560 | set until all local symbols are output. */ | |
53720c49 AM |
3561 | asection *igroup; |
3562 | struct bfd_elf_section_data *sec_data; | |
3563 | unsigned long symndx; | |
3564 | unsigned long extsymoff; | |
bcacc0f5 AM |
3565 | struct elf_link_hash_entry *h; |
3566 | ||
53720c49 AM |
3567 | /* The point of this little dance to the first SHF_GROUP section |
3568 | then back to the SHT_GROUP section is that this gets us to | |
3569 | the SHT_GROUP in the input object. */ | |
3570 | igroup = elf_sec_group (elf_next_in_group (sec)); | |
3571 | sec_data = elf_section_data (igroup); | |
3572 | symndx = sec_data->this_hdr.sh_info; | |
3573 | extsymoff = 0; | |
bcacc0f5 AM |
3574 | if (!elf_bad_symtab (igroup->owner)) |
3575 | { | |
3576 | Elf_Internal_Shdr *symtab_hdr; | |
3577 | ||
3578 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3579 | extsymoff = symtab_hdr->sh_info; | |
3580 | } | |
3581 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3582 | while (h->root.type == bfd_link_hash_indirect | |
3583 | || h->root.type == bfd_link_hash_warning) | |
3584 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3585 | ||
3586 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3587 | } |
dbb410c3 | 3588 | |
1126897b | 3589 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3590 | gas = TRUE; |
dbb410c3 AM |
3591 | if (sec->contents == NULL) |
3592 | { | |
b34976b6 | 3593 | gas = FALSE; |
a50b1753 | 3594 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3595 | |
3596 | /* Arrange for the section to be written out. */ | |
3597 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3598 | if (sec->contents == NULL) |
3599 | { | |
b34976b6 | 3600 | *failedptr = TRUE; |
dbb410c3 AM |
3601 | return; |
3602 | } | |
3603 | } | |
3604 | ||
eea6121a | 3605 | loc = sec->contents + sec->size; |
dbb410c3 | 3606 | |
9dce4196 AM |
3607 | /* Get the pointer to the first section in the group that gas |
3608 | squirreled away here. objcopy arranges for this to be set to the | |
3609 | start of the input section group. */ | |
3610 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3611 | |
3612 | /* First element is a flag word. Rest of section is elf section | |
3613 | indices for all the sections of the group. Write them backwards | |
3614 | just to keep the group in the same order as given in .section | |
3615 | directives, not that it matters. */ | |
3616 | while (elt != NULL) | |
3617 | { | |
9dce4196 | 3618 | asection *s; |
9dce4196 | 3619 | |
9dce4196 | 3620 | s = elt; |
415f38a6 AM |
3621 | if (!gas) |
3622 | s = s->output_section; | |
3623 | if (s != NULL | |
3624 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3625 | { |
db4677b8 | 3626 | struct bfd_elf_section_data *elf_sec = elf_section_data (s); |
28e07a05 AM |
3627 | struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt); |
3628 | ||
3629 | if (elf_sec->rel.hdr != NULL | |
3630 | && (gas | |
3631 | || (input_elf_sec->rel.hdr != NULL | |
3632 | && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)) | |
db4677b8 | 3633 | { |
28e07a05 | 3634 | elf_sec->rel.hdr->sh_flags |= SHF_GROUP; |
db4677b8 AM |
3635 | loc -= 4; |
3636 | H_PUT_32 (abfd, elf_sec->rel.idx, loc); | |
3637 | } | |
28e07a05 AM |
3638 | if (elf_sec->rela.hdr != NULL |
3639 | && (gas | |
3640 | || (input_elf_sec->rela.hdr != NULL | |
3641 | && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)) | |
db4677b8 | 3642 | { |
28e07a05 | 3643 | elf_sec->rela.hdr->sh_flags |= SHF_GROUP; |
db4677b8 AM |
3644 | loc -= 4; |
3645 | H_PUT_32 (abfd, elf_sec->rela.idx, loc); | |
3646 | } | |
01e1a5bc | 3647 | loc -= 4; |
db4677b8 | 3648 | H_PUT_32 (abfd, elf_sec->this_idx, loc); |
01e1a5bc | 3649 | } |
945906ff | 3650 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3651 | if (elt == first) |
3652 | break; | |
dbb410c3 AM |
3653 | } |
3654 | ||
7bdf4127 AB |
3655 | loc -= 4; |
3656 | BFD_ASSERT (loc == sec->contents); | |
dbb410c3 | 3657 | |
9dce4196 | 3658 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3659 | } |
3660 | ||
bce964aa AM |
3661 | /* Given NAME, the name of a relocation section stripped of its |
3662 | .rel/.rela prefix, return the section in ABFD to which the | |
3663 | relocations apply. */ | |
bd53a53a L |
3664 | |
3665 | asection * | |
bce964aa AM |
3666 | _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name) |
3667 | { | |
3668 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3669 | section likely apply to .got.plt or .got section. */ | |
3670 | if (get_elf_backend_data (abfd)->want_got_plt | |
3671 | && strcmp (name, ".plt") == 0) | |
3672 | { | |
3673 | asection *sec; | |
3674 | ||
3675 | name = ".got.plt"; | |
3676 | sec = bfd_get_section_by_name (abfd, name); | |
3677 | if (sec != NULL) | |
3678 | return sec; | |
3679 | name = ".got"; | |
3680 | } | |
3681 | ||
3682 | return bfd_get_section_by_name (abfd, name); | |
3683 | } | |
3684 | ||
3685 | /* Return the section to which RELOC_SEC applies. */ | |
3686 | ||
3687 | static asection * | |
3688 | elf_get_reloc_section (asection *reloc_sec) | |
bd53a53a L |
3689 | { |
3690 | const char *name; | |
3691 | unsigned int type; | |
3692 | bfd *abfd; | |
bce964aa | 3693 | const struct elf_backend_data *bed; |
bd53a53a L |
3694 | |
3695 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3696 | if (type != SHT_REL && type != SHT_RELA) | |
3697 | return NULL; | |
3698 | ||
3699 | /* We look up the section the relocs apply to by name. */ | |
3700 | name = reloc_sec->name; | |
bce964aa AM |
3701 | if (strncmp (name, ".rel", 4) != 0) |
3702 | return NULL; | |
3703 | name += 4; | |
3704 | if (type == SHT_RELA && *name++ != 'a') | |
3705 | return NULL; | |
bd53a53a | 3706 | |
bd53a53a | 3707 | abfd = reloc_sec->owner; |
bce964aa AM |
3708 | bed = get_elf_backend_data (abfd); |
3709 | return bed->get_reloc_section (abfd, name); | |
bd53a53a L |
3710 | } |
3711 | ||
252b5132 RH |
3712 | /* Assign all ELF section numbers. The dummy first section is handled here |
3713 | too. The link/info pointers for the standard section types are filled | |
3714 | in here too, while we're at it. */ | |
3715 | ||
b34976b6 | 3716 | static bfd_boolean |
da9f89d4 | 3717 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3718 | { |
3719 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3720 | asection *sec; | |
3e19fb8f | 3721 | unsigned int section_number; |
252b5132 | 3722 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3723 | struct bfd_elf_section_data *d; |
3516e984 | 3724 | bfd_boolean need_symtab; |
446f7ed5 | 3725 | size_t amt; |
252b5132 RH |
3726 | |
3727 | section_number = 1; | |
3728 | ||
2b0f7ef9 JJ |
3729 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3730 | ||
da9f89d4 | 3731 | /* SHT_GROUP sections are in relocatable files only. */ |
7bdf4127 | 3732 | if (link_info == NULL || !link_info->resolve_section_groups) |
252b5132 | 3733 | { |
ef53be89 | 3734 | size_t reloc_count = 0; |
14f2c699 | 3735 | |
da9f89d4 | 3736 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3737 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3738 | { |
5daa8fe7 | 3739 | d = elf_section_data (sec); |
da9f89d4 L |
3740 | |
3741 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3742 | { |
5daa8fe7 | 3743 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3744 | { |
3745 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3746 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3747 | abfd->section_count--; |
da9f89d4 | 3748 | } |
08a40648 | 3749 | else |
4fbb74a6 | 3750 | d->this_idx = section_number++; |
da9f89d4 | 3751 | } |
14f2c699 L |
3752 | |
3753 | /* Count relocations. */ | |
3754 | reloc_count += sec->reloc_count; | |
47cc2cf5 | 3755 | } |
14f2c699 L |
3756 | |
3757 | /* Clear HAS_RELOC if there are no relocations. */ | |
3758 | if (reloc_count == 0) | |
3759 | abfd->flags &= ~HAS_RELOC; | |
47cc2cf5 PB |
3760 | } |
3761 | ||
3762 | for (sec = abfd->sections; sec; sec = sec->next) | |
3763 | { | |
3764 | d = elf_section_data (sec); | |
3765 | ||
3766 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3767 | d->this_idx = section_number++; |
3e19fb8f L |
3768 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3769 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3770 | if (d->rel.hdr) |
2b0f7ef9 | 3771 | { |
d4730f92 | 3772 | d->rel.idx = section_number++; |
3e19fb8f L |
3773 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3774 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3775 | } |
d4730f92 BS |
3776 | else |
3777 | d->rel.idx = 0; | |
23bc299b | 3778 | |
d4730f92 | 3779 | if (d->rela.hdr) |
2b0f7ef9 | 3780 | { |
d4730f92 | 3781 | d->rela.idx = section_number++; |
3e19fb8f L |
3782 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
3783 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3784 | } |
23bc299b | 3785 | else |
d4730f92 | 3786 | d->rela.idx = 0; |
252b5132 RH |
3787 | } |
3788 | ||
3516e984 L |
3789 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3790 | || (link_info == NULL | |
3791 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3792 | == HAS_RELOC))); | |
3793 | if (need_symtab) | |
252b5132 | 3794 | { |
12bd6957 | 3795 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3796 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3797 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3798 | { |
7a6e0d89 | 3799 | elf_section_list *entry; |
6a40cf0c NC |
3800 | |
3801 | BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); | |
3802 | ||
7a6e0d89 | 3803 | entry = bfd_zalloc (abfd, sizeof (*entry)); |
6a40cf0c NC |
3804 | entry->ndx = section_number++; |
3805 | elf_symtab_shndx_list (abfd) = entry; | |
3806 | entry->hdr.sh_name | |
9ad5cbcf | 3807 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 3808 | ".symtab_shndx", FALSE); |
6a40cf0c | 3809 | if (entry->hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3810 | return FALSE; |
9ad5cbcf | 3811 | } |
12bd6957 | 3812 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3813 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3814 | } |
3815 | ||
dd905818 NC |
3816 | elf_shstrtab_sec (abfd) = section_number++; |
3817 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); | |
3818 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); | |
3819 | ||
1c52a645 L |
3820 | if (section_number >= SHN_LORESERVE) |
3821 | { | |
695344c0 | 3822 | /* xgettext:c-format */ |
871b3ab2 | 3823 | _bfd_error_handler (_("%pB: too many sections: %u"), |
1c52a645 L |
3824 | abfd, section_number); |
3825 | return FALSE; | |
3826 | } | |
3827 | ||
9ad5cbcf | 3828 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3829 | elf_elfheader (abfd)->e_shnum = section_number; |
3830 | ||
3831 | /* Set up the list of section header pointers, in agreement with the | |
3832 | indices. */ | |
446f7ed5 AM |
3833 | amt = section_number * sizeof (Elf_Internal_Shdr *); |
3834 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt); | |
252b5132 | 3835 | if (i_shdrp == NULL) |
b34976b6 | 3836 | return FALSE; |
252b5132 | 3837 | |
a50b1753 | 3838 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
07d6d2b8 | 3839 | sizeof (Elf_Internal_Shdr)); |
252b5132 RH |
3840 | if (i_shdrp[0] == NULL) |
3841 | { | |
3842 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3843 | return FALSE; |
252b5132 | 3844 | } |
252b5132 RH |
3845 | |
3846 | elf_elfsections (abfd) = i_shdrp; | |
3847 | ||
12bd6957 | 3848 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3849 | if (need_symtab) |
252b5132 | 3850 | { |
12bd6957 | 3851 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3852 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3853 | { |
6a40cf0c NC |
3854 | elf_section_list * entry = elf_symtab_shndx_list (abfd); |
3855 | BFD_ASSERT (entry != NULL); | |
3856 | i_shdrp[entry->ndx] = & entry->hdr; | |
3857 | entry->hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3858 | } |
12bd6957 AM |
3859 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3860 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3861 | } |
38ce5b11 | 3862 | |
252b5132 RH |
3863 | for (sec = abfd->sections; sec; sec = sec->next) |
3864 | { | |
252b5132 | 3865 | asection *s; |
252b5132 | 3866 | |
91d6fa6a NC |
3867 | d = elf_section_data (sec); |
3868 | ||
252b5132 | 3869 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3870 | if (d->rel.idx != 0) |
3871 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3872 | if (d->rela.idx != 0) | |
3873 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3874 | |
3875 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3876 | ||
3877 | /* sh_link of a reloc section is the section index of the symbol | |
3878 | table. sh_info is the section index of the section to which | |
3879 | the relocation entries apply. */ | |
d4730f92 | 3880 | if (d->rel.idx != 0) |
252b5132 | 3881 | { |
12bd6957 | 3882 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3883 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3884 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3885 | } |
d4730f92 | 3886 | if (d->rela.idx != 0) |
23bc299b | 3887 | { |
12bd6957 | 3888 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3889 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3890 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3891 | } |
252b5132 | 3892 | |
38ce5b11 L |
3893 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3894 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3895 | { | |
3896 | s = elf_linked_to_section (sec); | |
3897 | if (s) | |
38ce5b11 | 3898 | { |
f2876037 | 3899 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3900 | if (link_info != NULL) |
38ce5b11 | 3901 | { |
f2876037 | 3902 | /* Check discarded linkonce section. */ |
dbaa2011 | 3903 | if (discarded_section (s)) |
38ce5b11 | 3904 | { |
ccd2ec6a | 3905 | asection *kept; |
4eca0228 | 3906 | _bfd_error_handler |
695344c0 | 3907 | /* xgettext:c-format */ |
871b3ab2 AM |
3908 | (_("%pB: sh_link of section `%pA' points to" |
3909 | " discarded section `%pA' of `%pB'"), | |
ccd2ec6a L |
3910 | abfd, d->this_hdr.bfd_section, |
3911 | s, s->owner); | |
3912 | /* Point to the kept section if it has the same | |
3913 | size as the discarded one. */ | |
c0f00686 | 3914 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3915 | if (kept == NULL) |
185d09ad | 3916 | { |
ccd2ec6a L |
3917 | bfd_set_error (bfd_error_bad_value); |
3918 | return FALSE; | |
185d09ad | 3919 | } |
ccd2ec6a | 3920 | s = kept; |
38ce5b11 | 3921 | } |
e424ecc8 | 3922 | |
ccd2ec6a L |
3923 | s = s->output_section; |
3924 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3925 | } |
f2876037 L |
3926 | else |
3927 | { | |
3928 | /* Handle objcopy. */ | |
3929 | if (s->output_section == NULL) | |
3930 | { | |
4eca0228 | 3931 | _bfd_error_handler |
695344c0 | 3932 | /* xgettext:c-format */ |
871b3ab2 AM |
3933 | (_("%pB: sh_link of section `%pA' points to" |
3934 | " removed section `%pA' of `%pB'"), | |
f2876037 L |
3935 | abfd, d->this_hdr.bfd_section, s, s->owner); |
3936 | bfd_set_error (bfd_error_bad_value); | |
3937 | return FALSE; | |
3938 | } | |
3939 | s = s->output_section; | |
3940 | } | |
ccd2ec6a L |
3941 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3942 | } | |
3943 | else | |
3944 | { | |
3945 | /* PR 290: | |
3946 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3947 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3948 | sh_info fields. Hence we could get the situation | |
08a40648 | 3949 | where s is NULL. */ |
ccd2ec6a L |
3950 | const struct elf_backend_data *bed |
3951 | = get_elf_backend_data (abfd); | |
a859124d AM |
3952 | bed->link_order_error_handler |
3953 | /* xgettext:c-format */ | |
3954 | (_("%pB: warning: sh_link not set for section `%pA'"), | |
3955 | abfd, sec); | |
38ce5b11 L |
3956 | } |
3957 | } | |
3958 | ||
252b5132 RH |
3959 | switch (d->this_hdr.sh_type) |
3960 | { | |
3961 | case SHT_REL: | |
3962 | case SHT_RELA: | |
3963 | /* A reloc section which we are treating as a normal BFD | |
3964 | section. sh_link is the section index of the symbol | |
3965 | table. sh_info is the section index of the section to | |
3966 | which the relocation entries apply. We assume that an | |
3967 | allocated reloc section uses the dynamic symbol table. | |
3968 | FIXME: How can we be sure? */ | |
3969 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3970 | if (s != NULL) | |
3971 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3972 | ||
bce964aa | 3973 | s = elf_get_reloc_section (sec); |
252b5132 | 3974 | if (s != NULL) |
9ef5d938 L |
3975 | { |
3976 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3977 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3978 | } | |
252b5132 RH |
3979 | break; |
3980 | ||
3981 | case SHT_STRTAB: | |
3982 | /* We assume that a section named .stab*str is a stabs | |
3983 | string section. We look for a section with the same name | |
3984 | but without the trailing ``str'', and set its sh_link | |
3985 | field to point to this section. */ | |
0112cd26 | 3986 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3987 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3988 | { | |
3989 | size_t len; | |
3990 | char *alc; | |
3991 | ||
3992 | len = strlen (sec->name); | |
a50b1753 | 3993 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3994 | if (alc == NULL) |
b34976b6 | 3995 | return FALSE; |
d4c88bbb | 3996 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3997 | alc[len - 3] = '\0'; |
3998 | s = bfd_get_section_by_name (abfd, alc); | |
3999 | free (alc); | |
4000 | if (s != NULL) | |
4001 | { | |
4002 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
4003 | ||
4004 | /* This is a .stab section. */ | |
34ca5531 | 4005 | elf_section_data (s)->this_hdr.sh_entsize = 12; |
252b5132 RH |
4006 | } |
4007 | } | |
4008 | break; | |
4009 | ||
4010 | case SHT_DYNAMIC: | |
4011 | case SHT_DYNSYM: | |
4012 | case SHT_GNU_verneed: | |
4013 | case SHT_GNU_verdef: | |
4014 | /* sh_link is the section header index of the string table | |
4015 | used for the dynamic entries, or the symbol table, or the | |
4016 | version strings. */ | |
4017 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
4018 | if (s != NULL) | |
4019 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
4020 | break; | |
4021 | ||
7f1204bb JJ |
4022 | case SHT_GNU_LIBLIST: |
4023 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
4024 | list used for the dynamic entries, or the symbol table, or |
4025 | the version strings. */ | |
7f1204bb JJ |
4026 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
4027 | ? ".dynstr" : ".gnu.libstr"); | |
4028 | if (s != NULL) | |
4029 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
4030 | break; | |
4031 | ||
252b5132 | 4032 | case SHT_HASH: |
fdc90cb4 | 4033 | case SHT_GNU_HASH: |
252b5132 RH |
4034 | case SHT_GNU_versym: |
4035 | /* sh_link is the section header index of the symbol table | |
4036 | this hash table or version table is for. */ | |
4037 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
4038 | if (s != NULL) | |
4039 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
4040 | break; | |
dbb410c3 AM |
4041 | |
4042 | case SHT_GROUP: | |
12bd6957 | 4043 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
4044 | } |
4045 | } | |
4046 | ||
3e19fb8f L |
4047 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
4048 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
4049 | debug section name from .debug_* to .zdebug_* if needed. */ | |
4050 | ||
b34976b6 | 4051 | return TRUE; |
252b5132 RH |
4052 | } |
4053 | ||
5372391b | 4054 | static bfd_boolean |
217aa764 | 4055 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
4056 | { |
4057 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 4058 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
4059 | if (bed->elf_backend_sym_is_global) |
4060 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 4061 | |
e47bf690 | 4062 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
e6f7f6d1 AM |
4063 | || bfd_is_und_section (bfd_asymbol_section (sym)) |
4064 | || bfd_is_com_section (bfd_asymbol_section (sym))); | |
252b5132 RH |
4065 | } |
4066 | ||
76359541 TP |
4067 | /* Filter global symbols of ABFD to include in the import library. All |
4068 | SYMCOUNT symbols of ABFD can be examined from their pointers in | |
4069 | SYMS. Pointers of symbols to keep should be stored contiguously at | |
4070 | the beginning of that array. | |
4071 | ||
4072 | Returns the number of symbols to keep. */ | |
4073 | ||
4074 | unsigned int | |
4075 | _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, | |
4076 | asymbol **syms, long symcount) | |
4077 | { | |
4078 | long src_count, dst_count = 0; | |
4079 | ||
4080 | for (src_count = 0; src_count < symcount; src_count++) | |
4081 | { | |
4082 | asymbol *sym = syms[src_count]; | |
4083 | char *name = (char *) bfd_asymbol_name (sym); | |
4084 | struct bfd_link_hash_entry *h; | |
4085 | ||
4086 | if (!sym_is_global (abfd, sym)) | |
4087 | continue; | |
4088 | ||
4089 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE); | |
5df1bc57 AM |
4090 | if (h == NULL) |
4091 | continue; | |
76359541 TP |
4092 | if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) |
4093 | continue; | |
76359541 TP |
4094 | if (h->linker_def || h->ldscript_def) |
4095 | continue; | |
4096 | ||
4097 | syms[dst_count++] = sym; | |
4098 | } | |
4099 | ||
4100 | syms[dst_count] = NULL; | |
4101 | ||
4102 | return dst_count; | |
4103 | } | |
4104 | ||
5372391b | 4105 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 4106 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
4107 | |
4108 | static bfd_boolean | |
4109 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
4110 | { | |
c6d8cab4 L |
4111 | elf_symbol_type *type_ptr; |
4112 | ||
db0c309f NC |
4113 | if (sym == NULL) |
4114 | return FALSE; | |
4115 | ||
c6d8cab4 L |
4116 | if ((sym->flags & BSF_SECTION_SYM) == 0) |
4117 | return FALSE; | |
4118 | ||
db0c309f NC |
4119 | if (sym->section == NULL) |
4120 | return TRUE; | |
4121 | ||
c6d8cab4 L |
4122 | type_ptr = elf_symbol_from (abfd, sym); |
4123 | return ((type_ptr != NULL | |
4124 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
4125 | && bfd_is_abs_section (sym->section)) | |
4126 | || !(sym->section->owner == abfd | |
db0c309f NC |
4127 | || (sym->section->output_section != NULL |
4128 | && sym->section->output_section->owner == abfd | |
2633a79c AM |
4129 | && sym->section->output_offset == 0) |
4130 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
4131 | } |
4132 | ||
2633a79c AM |
4133 | /* Map symbol from it's internal number to the external number, moving |
4134 | all local symbols to be at the head of the list. */ | |
4135 | ||
b34976b6 | 4136 | static bfd_boolean |
12bd6957 | 4137 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 4138 | { |
dc810e39 | 4139 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
4140 | asymbol **syms = bfd_get_outsymbols (abfd); |
4141 | asymbol **sect_syms; | |
dc810e39 AM |
4142 | unsigned int num_locals = 0; |
4143 | unsigned int num_globals = 0; | |
4144 | unsigned int num_locals2 = 0; | |
4145 | unsigned int num_globals2 = 0; | |
7292b3ac | 4146 | unsigned int max_index = 0; |
dc810e39 | 4147 | unsigned int idx; |
252b5132 RH |
4148 | asection *asect; |
4149 | asymbol **new_syms; | |
446f7ed5 | 4150 | size_t amt; |
252b5132 RH |
4151 | |
4152 | #ifdef DEBUG | |
4153 | fprintf (stderr, "elf_map_symbols\n"); | |
4154 | fflush (stderr); | |
4155 | #endif | |
4156 | ||
252b5132 RH |
4157 | for (asect = abfd->sections; asect; asect = asect->next) |
4158 | { | |
4159 | if (max_index < asect->index) | |
4160 | max_index = asect->index; | |
4161 | } | |
4162 | ||
4163 | max_index++; | |
446f7ed5 AM |
4164 | amt = max_index * sizeof (asymbol *); |
4165 | sect_syms = (asymbol **) bfd_zalloc (abfd, amt); | |
252b5132 | 4166 | if (sect_syms == NULL) |
b34976b6 | 4167 | return FALSE; |
252b5132 | 4168 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 4169 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 4170 | |
079e9a2f AM |
4171 | /* Init sect_syms entries for any section symbols we have already |
4172 | decided to output. */ | |
252b5132 RH |
4173 | for (idx = 0; idx < symcount; idx++) |
4174 | { | |
dc810e39 | 4175 | asymbol *sym = syms[idx]; |
c044fabd | 4176 | |
252b5132 | 4177 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 4178 | && sym->value == 0 |
2633a79c AM |
4179 | && !ignore_section_sym (abfd, sym) |
4180 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 4181 | { |
5372391b | 4182 | asection *sec = sym->section; |
252b5132 | 4183 | |
5372391b AM |
4184 | if (sec->owner != abfd) |
4185 | sec = sec->output_section; | |
252b5132 | 4186 | |
5372391b | 4187 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
4188 | } |
4189 | } | |
4190 | ||
252b5132 RH |
4191 | /* Classify all of the symbols. */ |
4192 | for (idx = 0; idx < symcount; idx++) | |
4193 | { | |
2633a79c | 4194 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 4195 | num_globals++; |
2633a79c AM |
4196 | else if (!ignore_section_sym (abfd, syms[idx])) |
4197 | num_locals++; | |
252b5132 | 4198 | } |
079e9a2f | 4199 | |
5372391b | 4200 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
4201 | sections will already have a section symbol in outsymbols, but |
4202 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
4203 | at least in that case. */ | |
252b5132 RH |
4204 | for (asect = abfd->sections; asect; asect = asect->next) |
4205 | { | |
079e9a2f | 4206 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4207 | { |
079e9a2f | 4208 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
4209 | num_locals++; |
4210 | else | |
4211 | num_globals++; | |
252b5132 RH |
4212 | } |
4213 | } | |
4214 | ||
4215 | /* Now sort the symbols so the local symbols are first. */ | |
446f7ed5 AM |
4216 | amt = (num_locals + num_globals) * sizeof (asymbol *); |
4217 | new_syms = (asymbol **) bfd_alloc (abfd, amt); | |
252b5132 | 4218 | if (new_syms == NULL) |
b34976b6 | 4219 | return FALSE; |
252b5132 RH |
4220 | |
4221 | for (idx = 0; idx < symcount; idx++) | |
4222 | { | |
4223 | asymbol *sym = syms[idx]; | |
dc810e39 | 4224 | unsigned int i; |
252b5132 | 4225 | |
2633a79c AM |
4226 | if (sym_is_global (abfd, sym)) |
4227 | i = num_locals + num_globals2++; | |
4228 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
4229 | i = num_locals2++; |
4230 | else | |
2633a79c | 4231 | continue; |
252b5132 RH |
4232 | new_syms[i] = sym; |
4233 | sym->udata.i = i + 1; | |
4234 | } | |
4235 | for (asect = abfd->sections; asect; asect = asect->next) | |
4236 | { | |
079e9a2f | 4237 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4238 | { |
079e9a2f | 4239 | asymbol *sym = asect->symbol; |
dc810e39 | 4240 | unsigned int i; |
252b5132 | 4241 | |
079e9a2f | 4242 | sect_syms[asect->index] = sym; |
252b5132 RH |
4243 | if (!sym_is_global (abfd, sym)) |
4244 | i = num_locals2++; | |
4245 | else | |
4246 | i = num_locals + num_globals2++; | |
4247 | new_syms[i] = sym; | |
4248 | sym->udata.i = i + 1; | |
4249 | } | |
4250 | } | |
4251 | ||
4252 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
4253 | ||
12bd6957 | 4254 | *pnum_locals = num_locals; |
b34976b6 | 4255 | return TRUE; |
252b5132 RH |
4256 | } |
4257 | ||
4258 | /* Align to the maximum file alignment that could be required for any | |
4259 | ELF data structure. */ | |
4260 | ||
268b6b39 | 4261 | static inline file_ptr |
217aa764 | 4262 | align_file_position (file_ptr off, int align) |
252b5132 RH |
4263 | { |
4264 | return (off + align - 1) & ~(align - 1); | |
4265 | } | |
4266 | ||
4267 | /* Assign a file position to a section, optionally aligning to the | |
4268 | required section alignment. */ | |
4269 | ||
217aa764 AM |
4270 | file_ptr |
4271 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
4272 | file_ptr offset, | |
4273 | bfd_boolean align) | |
252b5132 | 4274 | { |
72de5009 AM |
4275 | if (align && i_shdrp->sh_addralign > 1) |
4276 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
4277 | i_shdrp->sh_offset = offset; |
4278 | if (i_shdrp->bfd_section != NULL) | |
4279 | i_shdrp->bfd_section->filepos = offset; | |
4280 | if (i_shdrp->sh_type != SHT_NOBITS) | |
4281 | offset += i_shdrp->sh_size; | |
4282 | return offset; | |
4283 | } | |
4284 | ||
4285 | /* Compute the file positions we are going to put the sections at, and | |
4286 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
4287 | is not NULL, this is being called by the ELF backend linker. */ | |
4288 | ||
b34976b6 | 4289 | bfd_boolean |
217aa764 AM |
4290 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
4291 | struct bfd_link_info *link_info) | |
252b5132 | 4292 | { |
9c5bfbb7 | 4293 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 4294 | struct fake_section_arg fsargs; |
b34976b6 | 4295 | bfd_boolean failed; |
ef10c3ac | 4296 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 4297 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 4298 | bfd_boolean need_symtab; |
252b5132 RH |
4299 | |
4300 | if (abfd->output_has_begun) | |
b34976b6 | 4301 | return TRUE; |
252b5132 RH |
4302 | |
4303 | /* Do any elf backend specific processing first. */ | |
4304 | if (bed->elf_backend_begin_write_processing) | |
4305 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
4306 | ||
ed7e9d0b | 4307 | if (!(*bed->elf_backend_init_file_header) (abfd, link_info)) |
b34976b6 | 4308 | return FALSE; |
252b5132 | 4309 | |
d4730f92 BS |
4310 | fsargs.failed = FALSE; |
4311 | fsargs.link_info = link_info; | |
4312 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
4313 | if (fsargs.failed) | |
b34976b6 | 4314 | return FALSE; |
252b5132 | 4315 | |
da9f89d4 | 4316 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 4317 | return FALSE; |
252b5132 RH |
4318 | |
4319 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
4320 | need_symtab = (link_info == NULL |
4321 | && (bfd_get_symcount (abfd) > 0 | |
4322 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
4323 | == HAS_RELOC))); | |
4324 | if (need_symtab) | |
252b5132 RH |
4325 | { |
4326 | /* Non-zero if doing a relocatable link. */ | |
4327 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
4328 | ||
4329 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 4330 | return FALSE; |
252b5132 RH |
4331 | } |
4332 | ||
d4730f92 | 4333 | failed = FALSE; |
1126897b | 4334 | if (link_info == NULL) |
dbb410c3 | 4335 | { |
1126897b | 4336 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 4337 | if (failed) |
b34976b6 | 4338 | return FALSE; |
dbb410c3 AM |
4339 | } |
4340 | ||
252b5132 | 4341 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
ed7e9d0b | 4342 | /* sh_name was set in init_file_header. */ |
252b5132 | 4343 | shstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 4344 | shstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
252b5132 | 4345 | shstrtab_hdr->sh_addr = 0; |
946748d5 | 4346 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4347 | shstrtab_hdr->sh_entsize = 0; |
4348 | shstrtab_hdr->sh_link = 0; | |
4349 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 4350 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4351 | shstrtab_hdr->sh_addralign = 1; |
4352 | ||
c84fca4d | 4353 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 4354 | return FALSE; |
252b5132 | 4355 | |
3516e984 | 4356 | if (need_symtab) |
252b5132 RH |
4357 | { |
4358 | file_ptr off; | |
4359 | Elf_Internal_Shdr *hdr; | |
4360 | ||
12bd6957 | 4361 | off = elf_next_file_pos (abfd); |
252b5132 | 4362 | |
6a40cf0c | 4363 | hdr = & elf_symtab_hdr (abfd); |
b34976b6 | 4364 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4365 | |
6a40cf0c NC |
4366 | if (elf_symtab_shndx_list (abfd) != NULL) |
4367 | { | |
4368 | hdr = & elf_symtab_shndx_list (abfd)->hdr; | |
4369 | if (hdr->sh_size != 0) | |
4370 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4371 | /* FIXME: What about other symtab_shndx sections in the list ? */ | |
4372 | } | |
9ad5cbcf | 4373 | |
252b5132 | 4374 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 4375 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4376 | |
12bd6957 | 4377 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
4378 | |
4379 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 4380 | out. */ |
252b5132 | 4381 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 4382 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 4383 | return FALSE; |
ef10c3ac | 4384 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
4385 | } |
4386 | ||
b34976b6 | 4387 | abfd->output_has_begun = TRUE; |
252b5132 | 4388 | |
b34976b6 | 4389 | return TRUE; |
252b5132 RH |
4390 | } |
4391 | ||
8ded5a0f AM |
4392 | /* Make an initial estimate of the size of the program header. If we |
4393 | get the number wrong here, we'll redo section placement. */ | |
4394 | ||
4395 | static bfd_size_type | |
4396 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
4397 | { | |
4398 | size_t segs; | |
4399 | asection *s; | |
2b05f1b7 | 4400 | const struct elf_backend_data *bed; |
8ded5a0f AM |
4401 | |
4402 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
4403 | and one for data. */ | |
4404 | segs = 2; | |
4405 | ||
4406 | s = bfd_get_section_by_name (abfd, ".interp"); | |
1b9e270b | 4407 | if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) |
8ded5a0f AM |
4408 | { |
4409 | /* If we have a loadable interpreter section, we need a | |
4410 | PT_INTERP segment. In this case, assume we also need a | |
4411 | PT_PHDR segment, although that may not be true for all | |
4412 | targets. */ | |
e9a38e0f | 4413 | segs += 2; |
8ded5a0f AM |
4414 | } |
4415 | ||
4416 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
4417 | { | |
4418 | /* We need a PT_DYNAMIC segment. */ | |
4419 | ++segs; | |
f210dcff | 4420 | } |
08a40648 | 4421 | |
ceae84aa | 4422 | if (info != NULL && info->relro) |
f210dcff L |
4423 | { |
4424 | /* We need a PT_GNU_RELRO segment. */ | |
4425 | ++segs; | |
8ded5a0f AM |
4426 | } |
4427 | ||
12bd6957 | 4428 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
4429 | { |
4430 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4431 | ++segs; | |
4432 | } | |
4433 | ||
12bd6957 | 4434 | if (elf_stack_flags (abfd)) |
8ded5a0f | 4435 | { |
2b05f1b7 L |
4436 | /* We need a PT_GNU_STACK segment. */ |
4437 | ++segs; | |
4438 | } | |
94b11780 | 4439 | |
0a59decb L |
4440 | s = bfd_get_section_by_name (abfd, |
4441 | NOTE_GNU_PROPERTY_SECTION_NAME); | |
4442 | if (s != NULL && s->size != 0) | |
4443 | { | |
4444 | /* We need a PT_GNU_PROPERTY segment. */ | |
4445 | ++segs; | |
4446 | } | |
4447 | ||
2b05f1b7 L |
4448 | for (s = abfd->sections; s != NULL; s = s->next) |
4449 | { | |
8ded5a0f | 4450 | if ((s->flags & SEC_LOAD) != 0 |
23e463ed | 4451 | && elf_section_type (s) == SHT_NOTE) |
8ded5a0f | 4452 | { |
23e463ed | 4453 | unsigned int alignment_power; |
8ded5a0f AM |
4454 | /* We need a PT_NOTE segment. */ |
4455 | ++segs; | |
23e463ed L |
4456 | /* Try to create just one PT_NOTE segment for all adjacent |
4457 | loadable SHT_NOTE sections. gABI requires that within a | |
4458 | PT_NOTE segment (and also inside of each SHT_NOTE section) | |
4459 | each note should have the same alignment. So we check | |
4460 | whether the sections are correctly aligned. */ | |
4461 | alignment_power = s->alignment_power; | |
4462 | while (s->next != NULL | |
4463 | && s->next->alignment_power == alignment_power | |
4464 | && (s->next->flags & SEC_LOAD) != 0 | |
4465 | && elf_section_type (s->next) == SHT_NOTE) | |
4466 | s = s->next; | |
8ded5a0f AM |
4467 | } |
4468 | } | |
4469 | ||
4470 | for (s = abfd->sections; s != NULL; s = s->next) | |
4471 | { | |
4472 | if (s->flags & SEC_THREAD_LOCAL) | |
4473 | { | |
4474 | /* We need a PT_TLS segment. */ | |
4475 | ++segs; | |
4476 | break; | |
4477 | } | |
4478 | } | |
4479 | ||
2b05f1b7 | 4480 | bed = get_elf_backend_data (abfd); |
a91e1603 | 4481 | |
df3a023b AM |
4482 | if ((abfd->flags & D_PAGED) != 0 |
4483 | && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) | |
4484 | { | |
4485 | /* Add a PT_GNU_MBIND segment for each mbind section. */ | |
4486 | unsigned int page_align_power = bfd_log2 (bed->commonpagesize); | |
4487 | for (s = abfd->sections; s != NULL; s = s->next) | |
4488 | if (elf_section_flags (s) & SHF_GNU_MBIND) | |
4489 | { | |
4490 | if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM) | |
4491 | { | |
4492 | _bfd_error_handler | |
4493 | /* xgettext:c-format */ | |
4494 | (_("%pB: GNU_MBIND section `%pA' has invalid " | |
4495 | "sh_info field: %d"), | |
4496 | abfd, s, elf_section_data (s)->this_hdr.sh_info); | |
4497 | continue; | |
4498 | } | |
4499 | /* Align mbind section to page size. */ | |
4500 | if (s->alignment_power < page_align_power) | |
4501 | s->alignment_power = page_align_power; | |
4502 | segs ++; | |
4503 | } | |
4504 | } | |
4505 | ||
4506 | /* Let the backend count up any program headers it might need. */ | |
4507 | if (bed->elf_backend_additional_program_headers) | |
8ded5a0f AM |
4508 | { |
4509 | int a; | |
4510 | ||
4511 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
4512 | if (a == -1) | |
4513 | abort (); | |
4514 | segs += a; | |
4515 | } | |
4516 | ||
4517 | return segs * bed->s->sizeof_phdr; | |
4518 | } | |
4519 | ||
2ea37f1c NC |
4520 | /* Find the segment that contains the output_section of section. */ |
4521 | ||
4522 | Elf_Internal_Phdr * | |
4523 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
4524 | { | |
4525 | struct elf_segment_map *m; | |
4526 | Elf_Internal_Phdr *p; | |
4527 | ||
12bd6957 | 4528 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
4529 | m != NULL; |
4530 | m = m->next, p++) | |
4531 | { | |
4532 | int i; | |
4533 | ||
4534 | for (i = m->count - 1; i >= 0; i--) | |
4535 | if (m->sections[i] == section) | |
4536 | return p; | |
4537 | } | |
4538 | ||
4539 | return NULL; | |
4540 | } | |
4541 | ||
252b5132 RH |
4542 | /* Create a mapping from a set of sections to a program segment. */ |
4543 | ||
217aa764 AM |
4544 | static struct elf_segment_map * |
4545 | make_mapping (bfd *abfd, | |
4546 | asection **sections, | |
4547 | unsigned int from, | |
4548 | unsigned int to, | |
4549 | bfd_boolean phdr) | |
252b5132 RH |
4550 | { |
4551 | struct elf_segment_map *m; | |
4552 | unsigned int i; | |
4553 | asection **hdrpp; | |
986f0783 | 4554 | size_t amt; |
252b5132 | 4555 | |
00bee008 AM |
4556 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
4557 | amt += (to - from) * sizeof (asection *); | |
a50b1753 | 4558 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4559 | if (m == NULL) |
4560 | return NULL; | |
4561 | m->next = NULL; | |
4562 | m->p_type = PT_LOAD; | |
4563 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
4564 | m->sections[i - from] = *hdrpp; | |
4565 | m->count = to - from; | |
4566 | ||
4567 | if (from == 0 && phdr) | |
4568 | { | |
4569 | /* Include the headers in the first PT_LOAD segment. */ | |
4570 | m->includes_filehdr = 1; | |
4571 | m->includes_phdrs = 1; | |
4572 | } | |
4573 | ||
4574 | return m; | |
4575 | } | |
4576 | ||
229fcec5 MM |
4577 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
4578 | on failure. */ | |
4579 | ||
4580 | struct elf_segment_map * | |
4581 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
4582 | { | |
4583 | struct elf_segment_map *m; | |
4584 | ||
a50b1753 | 4585 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
07d6d2b8 | 4586 | sizeof (struct elf_segment_map)); |
229fcec5 MM |
4587 | if (m == NULL) |
4588 | return NULL; | |
4589 | m->next = NULL; | |
4590 | m->p_type = PT_DYNAMIC; | |
4591 | m->count = 1; | |
4592 | m->sections[0] = dynsec; | |
08a40648 | 4593 | |
229fcec5 MM |
4594 | return m; |
4595 | } | |
4596 | ||
8ded5a0f | 4597 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 4598 | |
b34976b6 | 4599 | static bfd_boolean |
3dea8fca AM |
4600 | elf_modify_segment_map (bfd *abfd, |
4601 | struct bfd_link_info *info, | |
4602 | bfd_boolean remove_empty_load) | |
252b5132 | 4603 | { |
252e386e | 4604 | struct elf_segment_map **m; |
8ded5a0f | 4605 | const struct elf_backend_data *bed; |
252b5132 | 4606 | |
8ded5a0f AM |
4607 | /* The placement algorithm assumes that non allocated sections are |
4608 | not in PT_LOAD segments. We ensure this here by removing such | |
4609 | sections from the segment map. We also remove excluded | |
252e386e AM |
4610 | sections. Finally, any PT_LOAD segment without sections is |
4611 | removed. */ | |
12bd6957 | 4612 | m = &elf_seg_map (abfd); |
252e386e | 4613 | while (*m) |
8ded5a0f AM |
4614 | { |
4615 | unsigned int i, new_count; | |
252b5132 | 4616 | |
252e386e | 4617 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4618 | { |
252e386e AM |
4619 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4620 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4621 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4622 | { |
252e386e AM |
4623 | (*m)->sections[new_count] = (*m)->sections[i]; |
4624 | new_count++; | |
8ded5a0f AM |
4625 | } |
4626 | } | |
252e386e | 4627 | (*m)->count = new_count; |
252b5132 | 4628 | |
1a9ccd70 NC |
4629 | if (remove_empty_load |
4630 | && (*m)->p_type == PT_LOAD | |
4631 | && (*m)->count == 0 | |
4632 | && !(*m)->includes_phdrs) | |
252e386e AM |
4633 | *m = (*m)->next; |
4634 | else | |
4635 | m = &(*m)->next; | |
8ded5a0f | 4636 | } |
252b5132 | 4637 | |
8ded5a0f AM |
4638 | bed = get_elf_backend_data (abfd); |
4639 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4640 | { |
252e386e | 4641 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4642 | return FALSE; |
252b5132 | 4643 | } |
252b5132 | 4644 | |
8ded5a0f AM |
4645 | return TRUE; |
4646 | } | |
252b5132 | 4647 | |
dbc88fc1 AM |
4648 | #define IS_TBSS(s) \ |
4649 | ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL) | |
4650 | ||
8ded5a0f | 4651 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4652 | |
8ded5a0f AM |
4653 | bfd_boolean |
4654 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4655 | { | |
4656 | unsigned int count; | |
4657 | struct elf_segment_map *m; | |
4658 | asection **sections = NULL; | |
4659 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4660 | bfd_boolean no_user_phdrs; |
252b5132 | 4661 | |
12bd6957 | 4662 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4663 | |
4664 | if (info != NULL) | |
4665 | info->user_phdrs = !no_user_phdrs; | |
4666 | ||
3dea8fca | 4667 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4668 | { |
8ded5a0f AM |
4669 | asection *s; |
4670 | unsigned int i; | |
4671 | struct elf_segment_map *mfirst; | |
4672 | struct elf_segment_map **pm; | |
4673 | asection *last_hdr; | |
4674 | bfd_vma last_size; | |
00bee008 | 4675 | unsigned int hdr_index; |
8ded5a0f AM |
4676 | bfd_vma maxpagesize; |
4677 | asection **hdrpp; | |
64029e93 | 4678 | bfd_boolean phdr_in_segment; |
8ded5a0f | 4679 | bfd_boolean writable; |
2888249f | 4680 | bfd_boolean executable; |
446f7ed5 | 4681 | unsigned int tls_count = 0; |
8ded5a0f | 4682 | asection *first_tls = NULL; |
a91e1603 | 4683 | asection *first_mbind = NULL; |
8ded5a0f | 4684 | asection *dynsec, *eh_frame_hdr; |
446f7ed5 | 4685 | size_t amt; |
66631823 CE |
4686 | bfd_vma addr_mask, wrap_to = 0; /* Bytes. */ |
4687 | bfd_size_type phdr_size; /* Octets/bytes. */ | |
502794d4 | 4688 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
252b5132 | 4689 | |
8ded5a0f | 4690 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4691 | |
446f7ed5 AM |
4692 | amt = bfd_count_sections (abfd) * sizeof (asection *); |
4693 | sections = (asection **) bfd_malloc (amt); | |
8ded5a0f | 4694 | if (sections == NULL) |
252b5132 | 4695 | goto error_return; |
252b5132 | 4696 | |
8d06853e AM |
4697 | /* Calculate top address, avoiding undefined behaviour of shift |
4698 | left operator when shift count is equal to size of type | |
4699 | being shifted. */ | |
4700 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4701 | addr_mask = (addr_mask << 1) + 1; | |
4702 | ||
8ded5a0f AM |
4703 | i = 0; |
4704 | for (s = abfd->sections; s != NULL; s = s->next) | |
4705 | { | |
4706 | if ((s->flags & SEC_ALLOC) != 0) | |
4707 | { | |
48db3297 AM |
4708 | /* target_index is unused until bfd_elf_final_link |
4709 | starts output of section symbols. Use it to make | |
4710 | qsort stable. */ | |
4711 | s->target_index = i; | |
8ded5a0f AM |
4712 | sections[i] = s; |
4713 | ++i; | |
8d06853e | 4714 | /* A wrapping section potentially clashes with header. */ |
66631823 CE |
4715 | if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask)) |
4716 | wrap_to = (s->lma + s->size / opb) & addr_mask; | |
8ded5a0f AM |
4717 | } |
4718 | } | |
4719 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4720 | count = i; | |
252b5132 | 4721 | |
8ded5a0f | 4722 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4723 | |
64029e93 AM |
4724 | phdr_size = elf_program_header_size (abfd); |
4725 | if (phdr_size == (bfd_size_type) -1) | |
4726 | phdr_size = get_program_header_size (abfd, info); | |
4727 | phdr_size += bed->s->sizeof_ehdr; | |
502794d4 CE |
4728 | /* phdr_size is compared to LMA values which are in bytes. */ |
4729 | phdr_size /= opb; | |
64029e93 AM |
4730 | maxpagesize = bed->maxpagesize; |
4731 | if (maxpagesize == 0) | |
4732 | maxpagesize = 1; | |
4733 | phdr_in_segment = info != NULL && info->load_phdrs; | |
4734 | if (count != 0 | |
4735 | && (((sections[0]->lma & addr_mask) & (maxpagesize - 1)) | |
4736 | >= (phdr_size & (maxpagesize - 1)))) | |
4737 | /* For compatibility with old scripts that may not be using | |
4738 | SIZEOF_HEADERS, add headers when it looks like space has | |
4739 | been left for them. */ | |
4740 | phdr_in_segment = TRUE; | |
252b5132 | 4741 | |
64029e93 | 4742 | /* Build the mapping. */ |
8ded5a0f AM |
4743 | mfirst = NULL; |
4744 | pm = &mfirst; | |
252b5132 | 4745 | |
8ded5a0f AM |
4746 | /* If we have a .interp section, then create a PT_PHDR segment for |
4747 | the program headers and a PT_INTERP segment for the .interp | |
4748 | section. */ | |
4749 | s = bfd_get_section_by_name (abfd, ".interp"); | |
1b9e270b | 4750 | if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) |
8ded5a0f AM |
4751 | { |
4752 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4753 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4754 | if (m == NULL) |
4755 | goto error_return; | |
4756 | m->next = NULL; | |
4757 | m->p_type = PT_PHDR; | |
f882209d | 4758 | m->p_flags = PF_R; |
8ded5a0f AM |
4759 | m->p_flags_valid = 1; |
4760 | m->includes_phdrs = 1; | |
64029e93 | 4761 | phdr_in_segment = TRUE; |
8ded5a0f AM |
4762 | *pm = m; |
4763 | pm = &m->next; | |
252b5132 | 4764 | |
8ded5a0f | 4765 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4766 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4767 | if (m == NULL) |
4768 | goto error_return; | |
4769 | m->next = NULL; | |
4770 | m->p_type = PT_INTERP; | |
4771 | m->count = 1; | |
4772 | m->sections[0] = s; | |
4773 | ||
4774 | *pm = m; | |
4775 | pm = &m->next; | |
252b5132 | 4776 | } |
8ded5a0f AM |
4777 | |
4778 | /* Look through the sections. We put sections in the same program | |
4779 | segment when the start of the second section can be placed within | |
4780 | a few bytes of the end of the first section. */ | |
4781 | last_hdr = NULL; | |
4782 | last_size = 0; | |
00bee008 | 4783 | hdr_index = 0; |
8ded5a0f | 4784 | writable = FALSE; |
2888249f | 4785 | executable = FALSE; |
8ded5a0f AM |
4786 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); |
4787 | if (dynsec != NULL | |
4788 | && (dynsec->flags & SEC_LOAD) == 0) | |
4789 | dynsec = NULL; | |
4790 | ||
64029e93 AM |
4791 | if ((abfd->flags & D_PAGED) == 0) |
4792 | phdr_in_segment = FALSE; | |
4793 | ||
8ded5a0f AM |
4794 | /* Deal with -Ttext or something similar such that the first section |
4795 | is not adjacent to the program headers. This is an | |
4796 | approximation, since at this point we don't know exactly how many | |
4797 | program headers we will need. */ | |
64029e93 | 4798 | if (phdr_in_segment && count > 0) |
252b5132 | 4799 | { |
66631823 | 4800 | bfd_vma phdr_lma; /* Bytes. */ |
64029e93 AM |
4801 | bfd_boolean separate_phdr = FALSE; |
4802 | ||
4803 | phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize; | |
4804 | if (info != NULL | |
4805 | && info->separate_code | |
4806 | && (sections[0]->flags & SEC_CODE) != 0) | |
1a9ccd70 | 4807 | { |
64029e93 AM |
4808 | /* If data sections should be separate from code and |
4809 | thus not executable, and the first section is | |
4810 | executable then put the file and program headers in | |
4811 | their own PT_LOAD. */ | |
4812 | separate_phdr = TRUE; | |
4813 | if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize) | |
4814 | == (sections[0]->lma & addr_mask & -maxpagesize))) | |
4815 | { | |
4816 | /* The file and program headers are currently on the | |
4817 | same page as the first section. Put them on the | |
4818 | previous page if we can. */ | |
4819 | if (phdr_lma >= maxpagesize) | |
4820 | phdr_lma -= maxpagesize; | |
4821 | else | |
4822 | separate_phdr = FALSE; | |
4823 | } | |
4824 | } | |
4825 | if ((sections[0]->lma & addr_mask) < phdr_lma | |
4826 | || (sections[0]->lma & addr_mask) < phdr_size) | |
4827 | /* If file and program headers would be placed at the end | |
4828 | of memory then it's probably better to omit them. */ | |
4829 | phdr_in_segment = FALSE; | |
4830 | else if (phdr_lma < wrap_to) | |
4831 | /* If a section wraps around to where we'll be placing | |
4832 | file and program headers, then the headers will be | |
4833 | overwritten. */ | |
4834 | phdr_in_segment = FALSE; | |
4835 | else if (separate_phdr) | |
4836 | { | |
4837 | m = make_mapping (abfd, sections, 0, 0, phdr_in_segment); | |
4838 | if (m == NULL) | |
4839 | goto error_return; | |
66631823 | 4840 | m->p_paddr = phdr_lma * opb; |
64029e93 AM |
4841 | m->p_vaddr_offset |
4842 | = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize; | |
4843 | m->p_paddr_valid = 1; | |
4844 | *pm = m; | |
4845 | pm = &m->next; | |
4846 | phdr_in_segment = FALSE; | |
1a9ccd70 | 4847 | } |
252b5132 RH |
4848 | } |
4849 | ||
8ded5a0f | 4850 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4851 | { |
8ded5a0f AM |
4852 | asection *hdr; |
4853 | bfd_boolean new_segment; | |
4854 | ||
4855 | hdr = *hdrpp; | |
4856 | ||
4857 | /* See if this section and the last one will fit in the same | |
4858 | segment. */ | |
4859 | ||
4860 | if (last_hdr == NULL) | |
4861 | { | |
4862 | /* If we don't have a segment yet, then we don't need a new | |
4863 | one (we build the last one after this loop). */ | |
4864 | new_segment = FALSE; | |
4865 | } | |
4866 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4867 | { | |
4868 | /* If this section has a different relation between the | |
4869 | virtual address and the load address, then we need a new | |
4870 | segment. */ | |
4871 | new_segment = TRUE; | |
4872 | } | |
b5599592 AM |
4873 | else if (hdr->lma < last_hdr->lma + last_size |
4874 | || last_hdr->lma + last_size < last_hdr->lma) | |
4875 | { | |
4876 | /* If this section has a load address that makes it overlap | |
4877 | the previous section, then we need a new segment. */ | |
4878 | new_segment = TRUE; | |
4879 | } | |
76cb3a89 AM |
4880 | else if ((abfd->flags & D_PAGED) != 0 |
4881 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4882 | == (hdr->lma & -maxpagesize))) | |
4883 | { | |
4884 | /* If we are demand paged then we can't map two disk | |
4885 | pages onto the same memory page. */ | |
4886 | new_segment = FALSE; | |
4887 | } | |
39948a60 NC |
4888 | /* In the next test we have to be careful when last_hdr->lma is close |
4889 | to the end of the address space. If the aligned address wraps | |
4890 | around to the start of the address space, then there are no more | |
4891 | pages left in memory and it is OK to assume that the current | |
4892 | section can be included in the current segment. */ | |
76cb3a89 AM |
4893 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) |
4894 | + maxpagesize > last_hdr->lma) | |
4895 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) | |
4896 | + maxpagesize <= hdr->lma)) | |
8ded5a0f AM |
4897 | { |
4898 | /* If putting this section in this segment would force us to | |
4899 | skip a page in the segment, then we need a new segment. */ | |
4900 | new_segment = TRUE; | |
4901 | } | |
4902 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
76cb3a89 | 4903 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) |
8ded5a0f | 4904 | { |
e5654c0f AM |
4905 | /* We don't want to put a loaded section after a |
4906 | nonloaded (ie. bss style) section in the same segment | |
4907 | as that will force the non-loaded section to be loaded. | |
76cb3a89 | 4908 | Consider .tbss sections as loaded for this purpose. */ |
8ded5a0f AM |
4909 | new_segment = TRUE; |
4910 | } | |
4911 | else if ((abfd->flags & D_PAGED) == 0) | |
4912 | { | |
4913 | /* If the file is not demand paged, which means that we | |
4914 | don't require the sections to be correctly aligned in the | |
4915 | file, then there is no other reason for a new segment. */ | |
4916 | new_segment = FALSE; | |
4917 | } | |
2888249f L |
4918 | else if (info != NULL |
4919 | && info->separate_code | |
4920 | && executable != ((hdr->flags & SEC_CODE) != 0)) | |
4921 | { | |
4922 | new_segment = TRUE; | |
4923 | } | |
8ded5a0f | 4924 | else if (! writable |
76cb3a89 | 4925 | && (hdr->flags & SEC_READONLY) == 0) |
8ded5a0f AM |
4926 | { |
4927 | /* We don't want to put a writable section in a read only | |
76cb3a89 | 4928 | segment. */ |
8ded5a0f AM |
4929 | new_segment = TRUE; |
4930 | } | |
4931 | else | |
4932 | { | |
4933 | /* Otherwise, we can use the same segment. */ | |
4934 | new_segment = FALSE; | |
4935 | } | |
4936 | ||
2889e75b | 4937 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4938 | if (last_hdr != NULL |
4939 | && info != NULL | |
4940 | && info->callbacks->override_segment_assignment != NULL) | |
4941 | new_segment | |
4942 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4943 | last_hdr, | |
4944 | new_segment); | |
2889e75b | 4945 | |
8ded5a0f AM |
4946 | if (! new_segment) |
4947 | { | |
4948 | if ((hdr->flags & SEC_READONLY) == 0) | |
4949 | writable = TRUE; | |
2888249f L |
4950 | if ((hdr->flags & SEC_CODE) != 0) |
4951 | executable = TRUE; | |
8ded5a0f AM |
4952 | last_hdr = hdr; |
4953 | /* .tbss sections effectively have zero size. */ | |
502794d4 | 4954 | last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; |
8ded5a0f AM |
4955 | continue; |
4956 | } | |
4957 | ||
4958 | /* We need a new program segment. We must create a new program | |
00bee008 | 4959 | header holding all the sections from hdr_index until hdr. */ |
8ded5a0f | 4960 | |
00bee008 | 4961 | m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); |
8ded5a0f AM |
4962 | if (m == NULL) |
4963 | goto error_return; | |
4964 | ||
4965 | *pm = m; | |
4966 | pm = &m->next; | |
4967 | ||
252b5132 | 4968 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4969 | writable = TRUE; |
8ded5a0f AM |
4970 | else |
4971 | writable = FALSE; | |
4972 | ||
2888249f L |
4973 | if ((hdr->flags & SEC_CODE) == 0) |
4974 | executable = FALSE; | |
4975 | else | |
4976 | executable = TRUE; | |
4977 | ||
baaff79e JJ |
4978 | last_hdr = hdr; |
4979 | /* .tbss sections effectively have zero size. */ | |
502794d4 | 4980 | last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; |
00bee008 | 4981 | hdr_index = i; |
8ded5a0f | 4982 | phdr_in_segment = FALSE; |
252b5132 RH |
4983 | } |
4984 | ||
86b2281f AM |
4985 | /* Create a final PT_LOAD program segment, but not if it's just |
4986 | for .tbss. */ | |
4987 | if (last_hdr != NULL | |
00bee008 | 4988 | && (i - hdr_index != 1 |
dbc88fc1 | 4989 | || !IS_TBSS (last_hdr))) |
8ded5a0f | 4990 | { |
00bee008 | 4991 | m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); |
8ded5a0f AM |
4992 | if (m == NULL) |
4993 | goto error_return; | |
252b5132 | 4994 | |
8ded5a0f AM |
4995 | *pm = m; |
4996 | pm = &m->next; | |
4997 | } | |
252b5132 | 4998 | |
8ded5a0f AM |
4999 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
5000 | if (dynsec != NULL) | |
5001 | { | |
5002 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
5003 | if (m == NULL) | |
5004 | goto error_return; | |
5005 | *pm = m; | |
5006 | pm = &m->next; | |
5007 | } | |
252b5132 | 5008 | |
23e463ed | 5009 | /* For each batch of consecutive loadable SHT_NOTE sections, |
1c5265b5 JJ |
5010 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, |
5011 | because if we link together nonloadable .note sections and | |
5012 | loadable .note sections, we will generate two .note sections | |
23e463ed | 5013 | in the output file. */ |
8ded5a0f AM |
5014 | for (s = abfd->sections; s != NULL; s = s->next) |
5015 | { | |
5016 | if ((s->flags & SEC_LOAD) != 0 | |
23e463ed | 5017 | && elf_section_type (s) == SHT_NOTE) |
8ded5a0f | 5018 | { |
1c5265b5 | 5019 | asection *s2; |
23e463ed | 5020 | unsigned int alignment_power = s->alignment_power; |
91d6fa6a NC |
5021 | |
5022 | count = 1; | |
23e463ed L |
5023 | for (s2 = s; s2->next != NULL; s2 = s2->next) |
5024 | { | |
5025 | if (s2->next->alignment_power == alignment_power | |
5026 | && (s2->next->flags & SEC_LOAD) != 0 | |
5027 | && elf_section_type (s2->next) == SHT_NOTE | |
66631823 | 5028 | && align_power (s2->lma + s2->size / opb, |
23e463ed L |
5029 | alignment_power) |
5030 | == s2->next->lma) | |
5031 | count++; | |
5032 | else | |
5033 | break; | |
5034 | } | |
00bee008 AM |
5035 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
5036 | amt += count * sizeof (asection *); | |
a50b1753 | 5037 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
5038 | if (m == NULL) |
5039 | goto error_return; | |
5040 | m->next = NULL; | |
5041 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
5042 | m->count = count; |
5043 | while (count > 1) | |
5044 | { | |
5045 | m->sections[m->count - count--] = s; | |
5046 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
5047 | s = s->next; | |
5048 | } | |
5049 | m->sections[m->count - 1] = s; | |
5050 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
5051 | *pm = m; |
5052 | pm = &m->next; | |
5053 | } | |
5054 | if (s->flags & SEC_THREAD_LOCAL) | |
5055 | { | |
5056 | if (! tls_count) | |
5057 | first_tls = s; | |
5058 | tls_count++; | |
5059 | } | |
a91e1603 L |
5060 | if (first_mbind == NULL |
5061 | && (elf_section_flags (s) & SHF_GNU_MBIND) != 0) | |
5062 | first_mbind = s; | |
8ded5a0f | 5063 | } |
252b5132 | 5064 | |
8ded5a0f AM |
5065 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
5066 | if (tls_count > 0) | |
5067 | { | |
00bee008 AM |
5068 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
5069 | amt += tls_count * sizeof (asection *); | |
a50b1753 | 5070 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
5071 | if (m == NULL) |
5072 | goto error_return; | |
5073 | m->next = NULL; | |
5074 | m->p_type = PT_TLS; | |
5075 | m->count = tls_count; | |
5076 | /* Mandated PF_R. */ | |
5077 | m->p_flags = PF_R; | |
5078 | m->p_flags_valid = 1; | |
d923cae0 | 5079 | s = first_tls; |
446f7ed5 | 5080 | for (i = 0; i < tls_count; ++i) |
8ded5a0f | 5081 | { |
d923cae0 L |
5082 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
5083 | { | |
5084 | _bfd_error_handler | |
871b3ab2 | 5085 | (_("%pB: TLS sections are not adjacent:"), abfd); |
d923cae0 L |
5086 | s = first_tls; |
5087 | i = 0; | |
446f7ed5 | 5088 | while (i < tls_count) |
d923cae0 L |
5089 | { |
5090 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
5091 | { | |
871b3ab2 | 5092 | _bfd_error_handler (_(" TLS: %pA"), s); |
d923cae0 L |
5093 | i++; |
5094 | } | |
5095 | else | |
871b3ab2 | 5096 | _bfd_error_handler (_(" non-TLS: %pA"), s); |
d923cae0 L |
5097 | s = s->next; |
5098 | } | |
5099 | bfd_set_error (bfd_error_bad_value); | |
5100 | goto error_return; | |
5101 | } | |
5102 | m->sections[i] = s; | |
5103 | s = s->next; | |
8ded5a0f | 5104 | } |
252b5132 | 5105 | |
8ded5a0f AM |
5106 | *pm = m; |
5107 | pm = &m->next; | |
5108 | } | |
252b5132 | 5109 | |
df3a023b AM |
5110 | if (first_mbind |
5111 | && (abfd->flags & D_PAGED) != 0 | |
5112 | && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) | |
a91e1603 L |
5113 | for (s = first_mbind; s != NULL; s = s->next) |
5114 | if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0 | |
df3a023b | 5115 | && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM) |
a91e1603 L |
5116 | { |
5117 | /* Mandated PF_R. */ | |
5118 | unsigned long p_flags = PF_R; | |
5119 | if ((s->flags & SEC_READONLY) == 0) | |
5120 | p_flags |= PF_W; | |
5121 | if ((s->flags & SEC_CODE) != 0) | |
5122 | p_flags |= PF_X; | |
5123 | ||
5124 | amt = sizeof (struct elf_segment_map) + sizeof (asection *); | |
5125 | m = bfd_zalloc (abfd, amt); | |
5126 | if (m == NULL) | |
5127 | goto error_return; | |
5128 | m->next = NULL; | |
5129 | m->p_type = (PT_GNU_MBIND_LO | |
5130 | + elf_section_data (s)->this_hdr.sh_info); | |
5131 | m->count = 1; | |
5132 | m->p_flags_valid = 1; | |
5133 | m->sections[0] = s; | |
5134 | m->p_flags = p_flags; | |
5135 | ||
5136 | *pm = m; | |
5137 | pm = &m->next; | |
5138 | } | |
5139 | ||
0a59decb L |
5140 | s = bfd_get_section_by_name (abfd, |
5141 | NOTE_GNU_PROPERTY_SECTION_NAME); | |
5142 | if (s != NULL && s->size != 0) | |
5143 | { | |
5144 | amt = sizeof (struct elf_segment_map) + sizeof (asection *); | |
5145 | m = bfd_zalloc (abfd, amt); | |
5146 | if (m == NULL) | |
5147 | goto error_return; | |
5148 | m->next = NULL; | |
5149 | m->p_type = PT_GNU_PROPERTY; | |
5150 | m->count = 1; | |
5151 | m->p_flags_valid = 1; | |
5152 | m->sections[0] = s; | |
5153 | m->p_flags = PF_R; | |
5154 | *pm = m; | |
5155 | pm = &m->next; | |
5156 | } | |
5157 | ||
8ded5a0f AM |
5158 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
5159 | segment. */ | |
12bd6957 | 5160 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
5161 | if (eh_frame_hdr != NULL |
5162 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 5163 | { |
dc810e39 | 5164 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 5165 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
5166 | if (m == NULL) |
5167 | goto error_return; | |
5168 | m->next = NULL; | |
8ded5a0f | 5169 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 5170 | m->count = 1; |
8ded5a0f | 5171 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
5172 | |
5173 | *pm = m; | |
5174 | pm = &m->next; | |
5175 | } | |
13ae64f3 | 5176 | |
12bd6957 | 5177 | if (elf_stack_flags (abfd)) |
13ae64f3 | 5178 | { |
8ded5a0f | 5179 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 5180 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
5181 | if (m == NULL) |
5182 | goto error_return; | |
5183 | m->next = NULL; | |
2b05f1b7 | 5184 | m->p_type = PT_GNU_STACK; |
12bd6957 | 5185 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 5186 | m->p_align = bed->stack_align; |
8ded5a0f | 5187 | m->p_flags_valid = 1; |
04c3a755 NS |
5188 | m->p_align_valid = m->p_align != 0; |
5189 | if (info->stacksize > 0) | |
5190 | { | |
5191 | m->p_size = info->stacksize; | |
5192 | m->p_size_valid = 1; | |
5193 | } | |
252b5132 | 5194 | |
8ded5a0f AM |
5195 | *pm = m; |
5196 | pm = &m->next; | |
5197 | } | |
65765700 | 5198 | |
ceae84aa | 5199 | if (info != NULL && info->relro) |
8ded5a0f | 5200 | { |
f210dcff L |
5201 | for (m = mfirst; m != NULL; m = m->next) |
5202 | { | |
3832a4d8 AM |
5203 | if (m->p_type == PT_LOAD |
5204 | && m->count != 0 | |
5205 | && m->sections[0]->vma >= info->relro_start | |
5206 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 5207 | { |
3832a4d8 AM |
5208 | i = m->count; |
5209 | while (--i != (unsigned) -1) | |
ec2e748a NC |
5210 | { |
5211 | if (m->sections[i]->size > 0 | |
5212 | && (m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5213 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5214 | break; | |
5215 | } | |
3832a4d8 | 5216 | |
43a8475c | 5217 | if (i != (unsigned) -1) |
f210dcff L |
5218 | break; |
5219 | } | |
be01b344 | 5220 | } |
f210dcff L |
5221 | |
5222 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
5223 | if (m != NULL) | |
5224 | { | |
5225 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 5226 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
5227 | if (m == NULL) |
5228 | goto error_return; | |
5229 | m->next = NULL; | |
5230 | m->p_type = PT_GNU_RELRO; | |
f210dcff L |
5231 | *pm = m; |
5232 | pm = &m->next; | |
5233 | } | |
8ded5a0f | 5234 | } |
9ee5e499 | 5235 | |
8ded5a0f | 5236 | free (sections); |
12bd6957 | 5237 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
5238 | } |
5239 | ||
3dea8fca | 5240 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 5241 | return FALSE; |
8c37241b | 5242 | |
12bd6957 | 5243 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 5244 | ++count; |
12bd6957 | 5245 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 5246 | |
b34976b6 | 5247 | return TRUE; |
252b5132 RH |
5248 | |
5249 | error_return: | |
5250 | if (sections != NULL) | |
5251 | free (sections); | |
b34976b6 | 5252 | return FALSE; |
252b5132 RH |
5253 | } |
5254 | ||
5255 | /* Sort sections by address. */ | |
5256 | ||
5257 | static int | |
217aa764 | 5258 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
5259 | { |
5260 | const asection *sec1 = *(const asection **) arg1; | |
5261 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 5262 | bfd_size_type size1, size2; |
252b5132 RH |
5263 | |
5264 | /* Sort by LMA first, since this is the address used to | |
5265 | place the section into a segment. */ | |
5266 | if (sec1->lma < sec2->lma) | |
5267 | return -1; | |
5268 | else if (sec1->lma > sec2->lma) | |
5269 | return 1; | |
5270 | ||
5271 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
5272 | the same, and this will do nothing. */ | |
5273 | if (sec1->vma < sec2->vma) | |
5274 | return -1; | |
5275 | else if (sec1->vma > sec2->vma) | |
5276 | return 1; | |
5277 | ||
5278 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
5279 | ||
07c6e936 | 5280 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
5281 | |
5282 | if (TOEND (sec1)) | |
5283 | { | |
48db3297 | 5284 | if (!TOEND (sec2)) |
252b5132 RH |
5285 | return 1; |
5286 | } | |
00a7cdc5 | 5287 | else if (TOEND (sec2)) |
252b5132 RH |
5288 | return -1; |
5289 | ||
5290 | #undef TOEND | |
5291 | ||
00a7cdc5 NC |
5292 | /* Sort by size, to put zero sized sections |
5293 | before others at the same address. */ | |
252b5132 | 5294 | |
eea6121a AM |
5295 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
5296 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
5297 | |
5298 | if (size1 < size2) | |
252b5132 | 5299 | return -1; |
eecdbe52 | 5300 | if (size1 > size2) |
252b5132 RH |
5301 | return 1; |
5302 | ||
5303 | return sec1->target_index - sec2->target_index; | |
5304 | } | |
5305 | ||
30fe1832 AM |
5306 | /* This qsort comparison functions sorts PT_LOAD segments first and |
5307 | by p_paddr, for assign_file_positions_for_load_sections. */ | |
5308 | ||
5309 | static int | |
5310 | elf_sort_segments (const void *arg1, const void *arg2) | |
5311 | { | |
5312 | const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1; | |
5313 | const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2; | |
5314 | ||
5315 | if (m1->p_type != m2->p_type) | |
5316 | { | |
5317 | if (m1->p_type == PT_NULL) | |
5318 | return 1; | |
5319 | if (m2->p_type == PT_NULL) | |
5320 | return -1; | |
5321 | return m1->p_type < m2->p_type ? -1 : 1; | |
5322 | } | |
5323 | if (m1->includes_filehdr != m2->includes_filehdr) | |
5324 | return m1->includes_filehdr ? -1 : 1; | |
5325 | if (m1->no_sort_lma != m2->no_sort_lma) | |
5326 | return m1->no_sort_lma ? -1 : 1; | |
5327 | if (m1->p_type == PT_LOAD && !m1->no_sort_lma) | |
5328 | { | |
4b3ecb3b | 5329 | bfd_vma lma1, lma2; /* Octets. */ |
30fe1832 AM |
5330 | lma1 = 0; |
5331 | if (m1->p_paddr_valid) | |
4b3ecb3b | 5332 | lma1 = m1->p_paddr; |
30fe1832 | 5333 | else if (m1->count != 0) |
4b3ecb3b AM |
5334 | { |
5335 | unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner, | |
5336 | m1->sections[0]); | |
5337 | lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb; | |
5338 | } | |
30fe1832 AM |
5339 | lma2 = 0; |
5340 | if (m2->p_paddr_valid) | |
4b3ecb3b | 5341 | lma2 = m2->p_paddr; |
30fe1832 | 5342 | else if (m2->count != 0) |
4b3ecb3b AM |
5343 | { |
5344 | unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner, | |
5345 | m2->sections[0]); | |
5346 | lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb; | |
5347 | } | |
30fe1832 AM |
5348 | if (lma1 != lma2) |
5349 | return lma1 < lma2 ? -1 : 1; | |
5350 | } | |
5351 | if (m1->idx != m2->idx) | |
5352 | return m1->idx < m2->idx ? -1 : 1; | |
5353 | return 0; | |
5354 | } | |
5355 | ||
340b6d91 AC |
5356 | /* Ian Lance Taylor writes: |
5357 | ||
5358 | We shouldn't be using % with a negative signed number. That's just | |
5359 | not good. We have to make sure either that the number is not | |
5360 | negative, or that the number has an unsigned type. When the types | |
5361 | are all the same size they wind up as unsigned. When file_ptr is a | |
5362 | larger signed type, the arithmetic winds up as signed long long, | |
5363 | which is wrong. | |
5364 | ||
5365 | What we're trying to say here is something like ``increase OFF by | |
5366 | the least amount that will cause it to be equal to the VMA modulo | |
5367 | the page size.'' */ | |
5368 | /* In other words, something like: | |
5369 | ||
5370 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
5371 | off_offset = off % bed->maxpagesize; | |
5372 | if (vma_offset < off_offset) | |
5373 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
5374 | else | |
5375 | adjustment = vma_offset - off_offset; | |
08a40648 | 5376 | |
de194d85 | 5377 | which can be collapsed into the expression below. */ |
340b6d91 AC |
5378 | |
5379 | static file_ptr | |
5380 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
5381 | { | |
dc9155b2 NC |
5382 | /* PR binutils/16199: Handle an alignment of zero. */ |
5383 | if (maxpagesize == 0) | |
5384 | maxpagesize = 1; | |
340b6d91 AC |
5385 | return ((vma - off) % maxpagesize); |
5386 | } | |
5387 | ||
6d33f217 L |
5388 | static void |
5389 | print_segment_map (const struct elf_segment_map *m) | |
5390 | { | |
5391 | unsigned int j; | |
5392 | const char *pt = get_segment_type (m->p_type); | |
5393 | char buf[32]; | |
5394 | ||
5395 | if (pt == NULL) | |
5396 | { | |
5397 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
5398 | sprintf (buf, "LOPROC+%7.7x", | |
5399 | (unsigned int) (m->p_type - PT_LOPROC)); | |
5400 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
5401 | sprintf (buf, "LOOS+%7.7x", | |
5402 | (unsigned int) (m->p_type - PT_LOOS)); | |
5403 | else | |
5404 | snprintf (buf, sizeof (buf), "%8.8x", | |
5405 | (unsigned int) m->p_type); | |
5406 | pt = buf; | |
5407 | } | |
4a97a0e5 | 5408 | fflush (stdout); |
6d33f217 L |
5409 | fprintf (stderr, "%s:", pt); |
5410 | for (j = 0; j < m->count; j++) | |
5411 | fprintf (stderr, " %s", m->sections [j]->name); | |
5412 | putc ('\n',stderr); | |
4a97a0e5 | 5413 | fflush (stderr); |
6d33f217 L |
5414 | } |
5415 | ||
32812159 AM |
5416 | static bfd_boolean |
5417 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
5418 | { | |
5419 | void *buf; | |
5420 | bfd_boolean ret; | |
5421 | ||
5422 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
5423 | return FALSE; | |
5424 | buf = bfd_zmalloc (len); | |
5425 | if (buf == NULL) | |
5426 | return FALSE; | |
5427 | ret = bfd_bwrite (buf, len, abfd) == len; | |
5428 | free (buf); | |
5429 | return ret; | |
5430 | } | |
5431 | ||
252b5132 RH |
5432 | /* Assign file positions to the sections based on the mapping from |
5433 | sections to segments. This function also sets up some fields in | |
f3520d2f | 5434 | the file header. */ |
252b5132 | 5435 | |
b34976b6 | 5436 | static bfd_boolean |
f3520d2f AM |
5437 | assign_file_positions_for_load_sections (bfd *abfd, |
5438 | struct bfd_link_info *link_info) | |
252b5132 RH |
5439 | { |
5440 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 5441 | struct elf_segment_map *m; |
30fe1832 | 5442 | struct elf_segment_map *phdr_load_seg; |
252b5132 | 5443 | Elf_Internal_Phdr *phdrs; |
252b5132 | 5444 | Elf_Internal_Phdr *p; |
502794d4 | 5445 | file_ptr off; /* Octets. */ |
3f570048 | 5446 | bfd_size_type maxpagesize; |
30fe1832 | 5447 | unsigned int alloc, actual; |
0920dee7 | 5448 | unsigned int i, j; |
30fe1832 | 5449 | struct elf_segment_map **sorted_seg_map; |
502794d4 | 5450 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
252b5132 | 5451 | |
e36284ab | 5452 | if (link_info == NULL |
ceae84aa | 5453 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 5454 | return FALSE; |
252b5132 | 5455 | |
8ded5a0f | 5456 | alloc = 0; |
12bd6957 | 5457 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
30fe1832 | 5458 | m->idx = alloc++; |
252b5132 | 5459 | |
82f2dbf7 NC |
5460 | if (alloc) |
5461 | { | |
5462 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
5463 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
5464 | } | |
5465 | else | |
5466 | { | |
5467 | /* PR binutils/12467. */ | |
5468 | elf_elfheader (abfd)->e_phoff = 0; | |
5469 | elf_elfheader (abfd)->e_phentsize = 0; | |
5470 | } | |
d324f6d6 | 5471 | |
8ded5a0f | 5472 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 5473 | |
12bd6957 | 5474 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
30fe1832 AM |
5475 | { |
5476 | actual = alloc; | |
5477 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
5478 | } | |
8ded5a0f | 5479 | else |
30fe1832 AM |
5480 | { |
5481 | actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr; | |
5482 | BFD_ASSERT (elf_program_header_size (abfd) | |
5483 | == actual * bed->s->sizeof_phdr); | |
5484 | BFD_ASSERT (actual >= alloc); | |
5485 | } | |
252b5132 RH |
5486 | |
5487 | if (alloc == 0) | |
f3520d2f | 5488 | { |
12bd6957 | 5489 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 5490 | return TRUE; |
f3520d2f | 5491 | } |
252b5132 | 5492 | |
12bd6957 | 5493 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
5494 | see assign_file_positions_except_relocs, so make sure we have |
5495 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
5496 | The variable alloc contains the computed need, while |
5497 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
5498 | layout. |
5499 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
5500 | where the layout is forced to according to a larger size in the | |
5501 | last iterations for the testcase ld-elf/header. */ | |
30fe1832 AM |
5502 | phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs) |
5503 | + alloc * sizeof (*sorted_seg_map))); | |
5504 | sorted_seg_map = (struct elf_segment_map **) (phdrs + actual); | |
f3520d2f | 5505 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 5506 | if (phdrs == NULL) |
b34976b6 | 5507 | return FALSE; |
252b5132 | 5508 | |
30fe1832 | 5509 | for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++) |
252b5132 | 5510 | { |
30fe1832 | 5511 | sorted_seg_map[j] = m; |
252b5132 | 5512 | /* If elf_segment_map is not from map_sections_to_segments, the |
08a40648 | 5513 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
5514 | not be done to the PT_NOTE section of a corefile, which may |
5515 | contain several pseudo-sections artificially created by bfd. | |
5516 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
5517 | if (m->count > 1 |
5518 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 5519 | && m->p_type == PT_NOTE)) |
48db3297 AM |
5520 | { |
5521 | for (i = 0; i < m->count; i++) | |
5522 | m->sections[i]->target_index = i; | |
5523 | qsort (m->sections, (size_t) m->count, sizeof (asection *), | |
5524 | elf_sort_sections); | |
5525 | } | |
30fe1832 AM |
5526 | } |
5527 | if (alloc > 1) | |
5528 | qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map), | |
5529 | elf_sort_segments); | |
5530 | ||
5531 | maxpagesize = 1; | |
5532 | if ((abfd->flags & D_PAGED) != 0) | |
5533 | maxpagesize = bed->maxpagesize; | |
5534 | ||
5535 | /* Sections must map to file offsets past the ELF file header. */ | |
5536 | off = bed->s->sizeof_ehdr; | |
5537 | /* And if one of the PT_LOAD headers doesn't include the program | |
5538 | headers then we'll be mapping program headers in the usual | |
5539 | position after the ELF file header. */ | |
5540 | phdr_load_seg = NULL; | |
5541 | for (j = 0; j < alloc; j++) | |
5542 | { | |
5543 | m = sorted_seg_map[j]; | |
5544 | if (m->p_type != PT_LOAD) | |
5545 | break; | |
5546 | if (m->includes_phdrs) | |
5547 | { | |
5548 | phdr_load_seg = m; | |
5549 | break; | |
5550 | } | |
5551 | } | |
5552 | if (phdr_load_seg == NULL) | |
5553 | off += actual * bed->s->sizeof_phdr; | |
5554 | ||
5555 | for (j = 0; j < alloc; j++) | |
5556 | { | |
5557 | asection **secpp; | |
502794d4 | 5558 | bfd_vma off_adjust; /* Octets. */ |
30fe1832 | 5559 | bfd_boolean no_contents; |
252b5132 | 5560 | |
b301b248 AM |
5561 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
5562 | number of sections with contents contributing to both p_filesz | |
5563 | and p_memsz, followed by a number of sections with no contents | |
5564 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 5565 | available file offset for PT_LOAD and PT_NOTE segments. */ |
30fe1832 AM |
5566 | m = sorted_seg_map[j]; |
5567 | p = phdrs + m->idx; | |
252b5132 | 5568 | p->p_type = m->p_type; |
28a7f3e7 | 5569 | p->p_flags = m->p_flags; |
252b5132 | 5570 | |
3f570048 | 5571 | if (m->count == 0) |
502794d4 | 5572 | p->p_vaddr = m->p_vaddr_offset * opb; |
3f570048 | 5573 | else |
502794d4 | 5574 | p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb; |
3f570048 AM |
5575 | |
5576 | if (m->p_paddr_valid) | |
5577 | p->p_paddr = m->p_paddr; | |
5578 | else if (m->count == 0) | |
5579 | p->p_paddr = 0; | |
5580 | else | |
502794d4 | 5581 | p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb; |
3f570048 AM |
5582 | |
5583 | if (p->p_type == PT_LOAD | |
5584 | && (abfd->flags & D_PAGED) != 0) | |
5585 | { | |
5586 | /* p_align in demand paged PT_LOAD segments effectively stores | |
5587 | the maximum page size. When copying an executable with | |
5588 | objcopy, we set m->p_align from the input file. Use this | |
5589 | value for maxpagesize rather than bed->maxpagesize, which | |
5590 | may be different. Note that we use maxpagesize for PT_TLS | |
5591 | segment alignment later in this function, so we are relying | |
5592 | on at least one PT_LOAD segment appearing before a PT_TLS | |
5593 | segment. */ | |
5594 | if (m->p_align_valid) | |
5595 | maxpagesize = m->p_align; | |
5596 | ||
5597 | p->p_align = maxpagesize; | |
5598 | } | |
3271a814 NS |
5599 | else if (m->p_align_valid) |
5600 | p->p_align = m->p_align; | |
e970b90a DJ |
5601 | else if (m->count == 0) |
5602 | p->p_align = 1 << bed->s->log_file_align; | |
30fe1832 AM |
5603 | |
5604 | if (m == phdr_load_seg) | |
5605 | { | |
5606 | if (!m->includes_filehdr) | |
5607 | p->p_offset = off; | |
5608 | off += actual * bed->s->sizeof_phdr; | |
5609 | } | |
3f570048 | 5610 | |
bf988460 AM |
5611 | no_contents = FALSE; |
5612 | off_adjust = 0; | |
252b5132 | 5613 | if (p->p_type == PT_LOAD |
b301b248 | 5614 | && m->count > 0) |
252b5132 | 5615 | { |
66631823 | 5616 | bfd_size_type align; /* Bytes. */ |
a49e53ed | 5617 | unsigned int align_power = 0; |
b301b248 | 5618 | |
3271a814 NS |
5619 | if (m->p_align_valid) |
5620 | align = p->p_align; | |
5621 | else | |
252b5132 | 5622 | { |
3271a814 NS |
5623 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5624 | { | |
5625 | unsigned int secalign; | |
08a40648 | 5626 | |
fd361982 | 5627 | secalign = bfd_section_alignment (*secpp); |
3271a814 NS |
5628 | if (secalign > align_power) |
5629 | align_power = secalign; | |
5630 | } | |
5631 | align = (bfd_size_type) 1 << align_power; | |
5632 | if (align < maxpagesize) | |
5633 | align = maxpagesize; | |
b301b248 | 5634 | } |
252b5132 | 5635 | |
02bf8d82 AM |
5636 | for (i = 0; i < m->count; i++) |
5637 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
5638 | /* If we aren't making room for this section, then | |
5639 | it must be SHT_NOBITS regardless of what we've | |
5640 | set via struct bfd_elf_special_section. */ | |
5641 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
5642 | ||
bf988460 | 5643 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
5644 | sections. */ |
5645 | no_contents = TRUE; | |
5646 | for (i = 0; i < m->count; i++) | |
5647 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
5648 | { | |
5649 | no_contents = FALSE; | |
5650 | break; | |
5651 | } | |
bf988460 | 5652 | |
66631823 | 5653 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb); |
a8c75b76 AM |
5654 | |
5655 | /* Broken hardware and/or kernel require that files do not | |
5656 | map the same page with different permissions on some hppa | |
5657 | processors. */ | |
30fe1832 AM |
5658 | if (j != 0 |
5659 | && (abfd->flags & D_PAGED) != 0 | |
a8c75b76 AM |
5660 | && bed->no_page_alias |
5661 | && (off & (maxpagesize - 1)) != 0 | |
502794d4 CE |
5662 | && ((off & -maxpagesize) |
5663 | == ((off + off_adjust) & -maxpagesize))) | |
a8c75b76 | 5664 | off_adjust += maxpagesize; |
bf988460 AM |
5665 | off += off_adjust; |
5666 | if (no_contents) | |
5667 | { | |
5668 | /* We shouldn't need to align the segment on disk since | |
5669 | the segment doesn't need file space, but the gABI | |
5670 | arguably requires the alignment and glibc ld.so | |
5671 | checks it. So to comply with the alignment | |
5672 | requirement but not waste file space, we adjust | |
5673 | p_offset for just this segment. (OFF_ADJUST is | |
5674 | subtracted from OFF later.) This may put p_offset | |
5675 | past the end of file, but that shouldn't matter. */ | |
5676 | } | |
5677 | else | |
5678 | off_adjust = 0; | |
252b5132 | 5679 | } |
b1a6d0b1 NC |
5680 | /* Make sure the .dynamic section is the first section in the |
5681 | PT_DYNAMIC segment. */ | |
5682 | else if (p->p_type == PT_DYNAMIC | |
5683 | && m->count > 1 | |
5684 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
5685 | { | |
5686 | _bfd_error_handler | |
871b3ab2 | 5687 | (_("%pB: The first section in the PT_DYNAMIC segment" |
63a5468a | 5688 | " is not the .dynamic section"), |
b301b248 | 5689 | abfd); |
b1a6d0b1 NC |
5690 | bfd_set_error (bfd_error_bad_value); |
5691 | return FALSE; | |
5692 | } | |
3f001e84 JK |
5693 | /* Set the note section type to SHT_NOTE. */ |
5694 | else if (p->p_type == PT_NOTE) | |
5695 | for (i = 0; i < m->count; i++) | |
5696 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 5697 | |
252b5132 RH |
5698 | if (m->includes_filehdr) |
5699 | { | |
bf988460 | 5700 | if (!m->p_flags_valid) |
252b5132 | 5701 | p->p_flags |= PF_R; |
252b5132 RH |
5702 | p->p_filesz = bed->s->sizeof_ehdr; |
5703 | p->p_memsz = bed->s->sizeof_ehdr; | |
30fe1832 | 5704 | if (p->p_type == PT_LOAD) |
252b5132 | 5705 | { |
30fe1832 | 5706 | if (m->count > 0) |
252b5132 | 5707 | { |
30fe1832 AM |
5708 | if (p->p_vaddr < (bfd_vma) off |
5709 | || (!m->p_paddr_valid | |
5710 | && p->p_paddr < (bfd_vma) off)) | |
5711 | { | |
5712 | _bfd_error_handler | |
5713 | (_("%pB: not enough room for program headers," | |
5714 | " try linking with -N"), | |
5715 | abfd); | |
5716 | bfd_set_error (bfd_error_bad_value); | |
5717 | return FALSE; | |
5718 | } | |
5719 | p->p_vaddr -= off; | |
5720 | if (!m->p_paddr_valid) | |
5721 | p->p_paddr -= off; | |
252b5132 | 5722 | } |
30fe1832 AM |
5723 | } |
5724 | else if (sorted_seg_map[0]->includes_filehdr) | |
5725 | { | |
5726 | Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx; | |
5727 | p->p_vaddr = filehdr->p_vaddr; | |
bf988460 | 5728 | if (!m->p_paddr_valid) |
30fe1832 | 5729 | p->p_paddr = filehdr->p_paddr; |
252b5132 | 5730 | } |
252b5132 RH |
5731 | } |
5732 | ||
5733 | if (m->includes_phdrs) | |
5734 | { | |
bf988460 | 5735 | if (!m->p_flags_valid) |
252b5132 | 5736 | p->p_flags |= PF_R; |
30fe1832 AM |
5737 | p->p_filesz += actual * bed->s->sizeof_phdr; |
5738 | p->p_memsz += actual * bed->s->sizeof_phdr; | |
f3520d2f | 5739 | if (!m->includes_filehdr) |
252b5132 | 5740 | { |
30fe1832 | 5741 | if (p->p_type == PT_LOAD) |
252b5132 | 5742 | { |
30fe1832 AM |
5743 | elf_elfheader (abfd)->e_phoff = p->p_offset; |
5744 | if (m->count > 0) | |
5745 | { | |
5746 | p->p_vaddr -= off - p->p_offset; | |
5747 | if (!m->p_paddr_valid) | |
5748 | p->p_paddr -= off - p->p_offset; | |
5749 | } | |
5750 | } | |
5751 | else if (phdr_load_seg != NULL) | |
5752 | { | |
5753 | Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx; | |
502794d4 | 5754 | bfd_vma phdr_off = 0; /* Octets. */ |
30fe1832 AM |
5755 | if (phdr_load_seg->includes_filehdr) |
5756 | phdr_off = bed->s->sizeof_ehdr; | |
5757 | p->p_vaddr = phdr->p_vaddr + phdr_off; | |
bf988460 | 5758 | if (!m->p_paddr_valid) |
30fe1832 AM |
5759 | p->p_paddr = phdr->p_paddr + phdr_off; |
5760 | p->p_offset = phdr->p_offset + phdr_off; | |
252b5132 | 5761 | } |
30fe1832 AM |
5762 | else |
5763 | p->p_offset = bed->s->sizeof_ehdr; | |
2b0bc088 | 5764 | } |
252b5132 RH |
5765 | } |
5766 | ||
5767 | if (p->p_type == PT_LOAD | |
5768 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
5769 | { | |
bf988460 | 5770 | if (!m->includes_filehdr && !m->includes_phdrs) |
0bc3450e AM |
5771 | { |
5772 | p->p_offset = off; | |
5773 | if (no_contents) | |
67641dd3 AM |
5774 | { |
5775 | /* Put meaningless p_offset for PT_LOAD segments | |
5776 | without file contents somewhere within the first | |
5777 | page, in an attempt to not point past EOF. */ | |
5778 | bfd_size_type align = maxpagesize; | |
5779 | if (align < p->p_align) | |
5780 | align = p->p_align; | |
5781 | if (align < 1) | |
5782 | align = 1; | |
5783 | p->p_offset = off % align; | |
5784 | } | |
0bc3450e | 5785 | } |
252b5132 RH |
5786 | else |
5787 | { | |
502794d4 | 5788 | file_ptr adjust; /* Octets. */ |
252b5132 RH |
5789 | |
5790 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
5791 | if (!no_contents) |
5792 | p->p_filesz += adjust; | |
252b5132 RH |
5793 | p->p_memsz += adjust; |
5794 | } | |
5795 | } | |
5796 | ||
1ea63fd2 AM |
5797 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
5798 | maps. Set filepos for sections in PT_LOAD segments, and in | |
5799 | core files, for sections in PT_NOTE segments. | |
5800 | assign_file_positions_for_non_load_sections will set filepos | |
5801 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
5802 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5803 | { | |
5804 | asection *sec; | |
252b5132 | 5805 | bfd_size_type align; |
627b32bc | 5806 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
5807 | |
5808 | sec = *secpp; | |
02bf8d82 | 5809 | this_hdr = &elf_section_data (sec)->this_hdr; |
fd361982 | 5810 | align = (bfd_size_type) 1 << bfd_section_alignment (sec); |
252b5132 | 5811 | |
88967714 AM |
5812 | if ((p->p_type == PT_LOAD |
5813 | || p->p_type == PT_TLS) | |
5814 | && (this_hdr->sh_type != SHT_NOBITS | |
5815 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
5816 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
5817 | || p->p_type == PT_TLS)))) | |
252b5132 | 5818 | { |
502794d4 CE |
5819 | bfd_vma p_start = p->p_paddr; /* Octets. */ |
5820 | bfd_vma p_end = p_start + p->p_memsz; /* Octets. */ | |
5821 | bfd_vma s_start = sec->lma * opb; /* Octets. */ | |
5822 | bfd_vma adjust = s_start - p_end; /* Octets. */ | |
252b5132 | 5823 | |
a2d1e028 L |
5824 | if (adjust != 0 |
5825 | && (s_start < p_end | |
5826 | || p_end < p_start)) | |
252b5132 | 5827 | { |
4eca0228 | 5828 | _bfd_error_handler |
695344c0 | 5829 | /* xgettext:c-format */ |
2dcf00ce | 5830 | (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64), |
502794d4 CE |
5831 | abfd, sec, (uint64_t) s_start / opb, |
5832 | (uint64_t) p_end / opb); | |
88967714 | 5833 | adjust = 0; |
502794d4 | 5834 | sec->lma = p_end / opb; |
1cfb7d1e | 5835 | } |
3ac9b6c9 | 5836 | p->p_memsz += adjust; |
1cfb7d1e | 5837 | |
d16e3d2e | 5838 | if (p->p_type == PT_LOAD) |
88967714 | 5839 | { |
d16e3d2e | 5840 | if (this_hdr->sh_type != SHT_NOBITS) |
32812159 | 5841 | { |
d16e3d2e | 5842 | off_adjust = 0; |
30fe1832 AM |
5843 | if (p->p_filesz + adjust < p->p_memsz) |
5844 | { | |
5845 | /* We have a PROGBITS section following NOBITS ones. | |
5846 | Allocate file space for the NOBITS section(s) and | |
5847 | zero it. */ | |
5848 | adjust = p->p_memsz - p->p_filesz; | |
5849 | if (!write_zeros (abfd, off, adjust)) | |
5850 | return FALSE; | |
5851 | } | |
d16e3d2e AM |
5852 | } |
5853 | /* We only adjust sh_offset in SHT_NOBITS sections | |
5854 | as would seem proper for their address when the | |
5855 | section is first in the segment. sh_offset | |
5856 | doesn't really have any significance for | |
5857 | SHT_NOBITS anyway, apart from a notional position | |
5858 | relative to other sections. Historically we | |
5859 | didn't bother with adjusting sh_offset and some | |
5860 | programs depend on it not being adjusted. See | |
5861 | pr12921 and pr25662. */ | |
5862 | if (this_hdr->sh_type != SHT_NOBITS || i == 0) | |
5863 | { | |
30fe1832 | 5864 | off += adjust; |
d16e3d2e AM |
5865 | if (this_hdr->sh_type == SHT_NOBITS) |
5866 | off_adjust += adjust; | |
32812159 | 5867 | } |
252b5132 | 5868 | } |
d16e3d2e AM |
5869 | if (this_hdr->sh_type != SHT_NOBITS) |
5870 | p->p_filesz += adjust; | |
252b5132 RH |
5871 | } |
5872 | ||
5873 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5874 | { | |
b301b248 AM |
5875 | /* The section at i == 0 is the one that actually contains |
5876 | everything. */ | |
4a938328 MS |
5877 | if (i == 0) |
5878 | { | |
627b32bc | 5879 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5880 | off += this_hdr->sh_size; |
5881 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5882 | p->p_memsz = 0; |
5883 | p->p_align = 1; | |
252b5132 | 5884 | } |
4a938328 | 5885 | else |
252b5132 | 5886 | { |
b301b248 | 5887 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5888 | sec->filepos = 0; |
eea6121a | 5889 | sec->size = 0; |
b301b248 AM |
5890 | sec->flags = 0; |
5891 | continue; | |
252b5132 | 5892 | } |
252b5132 RH |
5893 | } |
5894 | else | |
5895 | { | |
1e951488 | 5896 | if (p->p_type == PT_LOAD) |
b301b248 | 5897 | { |
1e951488 AM |
5898 | this_hdr->sh_offset = sec->filepos = off; |
5899 | if (this_hdr->sh_type != SHT_NOBITS) | |
5900 | off += this_hdr->sh_size; | |
5901 | } | |
5902 | else if (this_hdr->sh_type == SHT_NOBITS | |
5903 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5904 | && this_hdr->sh_offset == 0) | |
5905 | { | |
5906 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5907 | (See _bfd_elf_map_sections_to_segments "Create a | |
5908 | final PT_LOAD".) Set sh_offset to the value it | |
5909 | would have if we had created a zero p_filesz and | |
5910 | p_memsz PT_LOAD header for the section. This | |
5911 | also makes the PT_TLS header have the same | |
5912 | p_offset value. */ | |
5913 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5914 | off, align); | |
5915 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5916 | } |
252b5132 | 5917 | |
02bf8d82 | 5918 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5919 | { |
6a3cd2b4 | 5920 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5921 | /* A load section without SHF_ALLOC is something like |
5922 | a note section in a PT_NOTE segment. These take | |
5923 | file space but are not loaded into memory. */ | |
5924 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5925 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5926 | } |
6a3cd2b4 | 5927 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5928 | { |
6a3cd2b4 AM |
5929 | if (p->p_type == PT_TLS) |
5930 | p->p_memsz += this_hdr->sh_size; | |
5931 | ||
5932 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5933 | normal segments. */ | |
5934 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5935 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5936 | } |
5937 | ||
b10a8ae0 L |
5938 | if (align > p->p_align |
5939 | && !m->p_align_valid | |
5940 | && (p->p_type != PT_LOAD | |
5941 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5942 | p->p_align = align; |
5943 | } | |
5944 | ||
bf988460 | 5945 | if (!m->p_flags_valid) |
252b5132 RH |
5946 | { |
5947 | p->p_flags |= PF_R; | |
02bf8d82 | 5948 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5949 | p->p_flags |= PF_X; |
02bf8d82 | 5950 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5951 | p->p_flags |= PF_W; |
5952 | } | |
5953 | } | |
43a8475c | 5954 | |
bf988460 | 5955 | off -= off_adjust; |
0920dee7 | 5956 | |
30fe1832 AM |
5957 | /* PR ld/20815 - Check that the program header segment, if |
5958 | present, will be loaded into memory. */ | |
5959 | if (p->p_type == PT_PHDR | |
5960 | && phdr_load_seg == NULL | |
5961 | && !(bed->elf_backend_allow_non_load_phdr != NULL | |
5962 | && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc))) | |
5963 | { | |
5964 | /* The fix for this error is usually to edit the linker script being | |
5965 | used and set up the program headers manually. Either that or | |
5966 | leave room for the headers at the start of the SECTIONS. */ | |
5967 | _bfd_error_handler (_("%pB: error: PHDR segment not covered" | |
5968 | " by LOAD segment"), | |
5969 | abfd); | |
7b3c2715 AM |
5970 | if (link_info == NULL) |
5971 | return FALSE; | |
5972 | /* Arrange for the linker to exit with an error, deleting | |
5973 | the output file unless --noinhibit-exec is given. */ | |
5974 | link_info->callbacks->info ("%X"); | |
30fe1832 AM |
5975 | } |
5976 | ||
7c928300 AM |
5977 | /* Check that all sections are in a PT_LOAD segment. |
5978 | Don't check funky gdb generated core files. */ | |
5979 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5980 | { |
5981 | bfd_boolean check_vma = TRUE; | |
5982 | ||
5983 | for (i = 1; i < m->count; i++) | |
5984 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5985 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5986 | ->this_hdr), p) != 0 | |
5987 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5988 | ->this_hdr), p) != 0) | |
0920dee7 | 5989 | { |
9a83a553 AM |
5990 | /* Looks like we have overlays packed into the segment. */ |
5991 | check_vma = FALSE; | |
5992 | break; | |
0920dee7 | 5993 | } |
9a83a553 AM |
5994 | |
5995 | for (i = 0; i < m->count; i++) | |
5996 | { | |
5997 | Elf_Internal_Shdr *this_hdr; | |
5998 | asection *sec; | |
5999 | ||
6000 | sec = m->sections[i]; | |
6001 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
6002 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
6003 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 | 6004 | { |
4eca0228 | 6005 | _bfd_error_handler |
695344c0 | 6006 | /* xgettext:c-format */ |
871b3ab2 | 6007 | (_("%pB: section `%pA' can't be allocated in segment %d"), |
9a83a553 AM |
6008 | abfd, sec, j); |
6009 | print_segment_map (m); | |
6010 | } | |
6011 | } | |
6012 | } | |
252b5132 RH |
6013 | } |
6014 | ||
12bd6957 | 6015 | elf_next_file_pos (abfd) = off; |
30fe1832 AM |
6016 | |
6017 | if (link_info != NULL | |
6018 | && phdr_load_seg != NULL | |
6019 | && phdr_load_seg->includes_filehdr) | |
6020 | { | |
6021 | /* There is a segment that contains both the file headers and the | |
6022 | program headers, so provide a symbol __ehdr_start pointing there. | |
6023 | A program can use this to examine itself robustly. */ | |
6024 | ||
6025 | struct elf_link_hash_entry *hash | |
6026 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
6027 | FALSE, FALSE, TRUE); | |
6028 | /* If the symbol was referenced and not defined, define it. */ | |
6029 | if (hash != NULL | |
6030 | && (hash->root.type == bfd_link_hash_new | |
6031 | || hash->root.type == bfd_link_hash_undefined | |
6032 | || hash->root.type == bfd_link_hash_undefweak | |
6033 | || hash->root.type == bfd_link_hash_common)) | |
6034 | { | |
6035 | asection *s = NULL; | |
66631823 | 6036 | bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb; |
30fe1832 AM |
6037 | |
6038 | if (phdr_load_seg->count != 0) | |
6039 | /* The segment contains sections, so use the first one. */ | |
6040 | s = phdr_load_seg->sections[0]; | |
6041 | else | |
6042 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
6043 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) | |
6044 | if (m->p_type == PT_LOAD && m->count != 0) | |
6045 | { | |
6046 | s = m->sections[0]; | |
6047 | break; | |
6048 | } | |
6049 | ||
6050 | if (s != NULL) | |
6051 | { | |
6052 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
6053 | hash->root.u.def.section = s; | |
6054 | } | |
6055 | else | |
6056 | { | |
6057 | hash->root.u.def.value = filehdr_vaddr; | |
6058 | hash->root.u.def.section = bfd_abs_section_ptr; | |
6059 | } | |
6060 | ||
6061 | hash->root.type = bfd_link_hash_defined; | |
6062 | hash->def_regular = 1; | |
6063 | hash->non_elf = 0; | |
6064 | } | |
6065 | } | |
6066 | ||
f3520d2f AM |
6067 | return TRUE; |
6068 | } | |
6069 | ||
1faa385f NC |
6070 | /* Determine if a bfd is a debuginfo file. Unfortunately there |
6071 | is no defined method for detecting such files, so we have to | |
6072 | use heuristics instead. */ | |
6073 | ||
6074 | bfd_boolean | |
6075 | is_debuginfo_file (bfd *abfd) | |
6076 | { | |
6077 | if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
6078 | return FALSE; | |
6079 | ||
6080 | Elf_Internal_Shdr **start_headers = elf_elfsections (abfd); | |
6081 | Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd); | |
6082 | Elf_Internal_Shdr **headerp; | |
6083 | ||
6084 | for (headerp = start_headers; headerp < end_headers; headerp ++) | |
6085 | { | |
6086 | Elf_Internal_Shdr *header = * headerp; | |
6087 | ||
6088 | /* Debuginfo files do not have any allocated SHT_PROGBITS sections. | |
6089 | The only allocated sections are SHT_NOBITS or SHT_NOTES. */ | |
6090 | if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC | |
6091 | && header->sh_type != SHT_NOBITS | |
6092 | && header->sh_type != SHT_NOTE) | |
6093 | return FALSE; | |
6094 | } | |
6095 | ||
6096 | return TRUE; | |
6097 | } | |
6098 | ||
1ff6de03 NA |
6099 | /* Assign file positions for the other sections, except for compressed debugging |
6100 | and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */ | |
f3520d2f AM |
6101 | |
6102 | static bfd_boolean | |
6103 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
6104 | struct bfd_link_info *link_info) | |
6105 | { | |
6106 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
6107 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 6108 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
6109 | Elf_Internal_Phdr *phdrs; |
6110 | Elf_Internal_Phdr *p; | |
6111 | struct elf_segment_map *m; | |
f3520d2f | 6112 | file_ptr off; |
66631823 | 6113 | unsigned int opb = bfd_octets_per_byte (abfd, NULL); |
f3520d2f | 6114 | |
5c182d5f | 6115 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 6116 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 6117 | off = elf_next_file_pos (abfd); |
e06efbf1 | 6118 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 6119 | { |
5c182d5f AM |
6120 | Elf_Internal_Shdr *hdr; |
6121 | ||
6122 | hdr = *hdrpp; | |
6123 | if (hdr->bfd_section != NULL | |
252e386e AM |
6124 | && (hdr->bfd_section->filepos != 0 |
6125 | || (hdr->sh_type == SHT_NOBITS | |
6126 | && hdr->contents == NULL))) | |
627b32bc | 6127 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
6128 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
6129 | { | |
1faa385f NC |
6130 | if (hdr->sh_size != 0 |
6131 | /* PR 24717 - debuginfo files are known to be not strictly | |
6132 | compliant with the ELF standard. In particular they often | |
6133 | have .note.gnu.property sections that are outside of any | |
6134 | loadable segment. This is not a problem for such files, | |
6135 | so do not warn about them. */ | |
6136 | && ! is_debuginfo_file (abfd)) | |
4eca0228 | 6137 | _bfd_error_handler |
695344c0 | 6138 | /* xgettext:c-format */ |
871b3ab2 | 6139 | (_("%pB: warning: allocated section `%s' not in segment"), |
e8d2ba53 AM |
6140 | abfd, |
6141 | (hdr->bfd_section == NULL | |
6142 | ? "*unknown*" | |
6143 | : hdr->bfd_section->name)); | |
3ba71138 L |
6144 | /* We don't need to page align empty sections. */ |
6145 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
6146 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
6147 | bed->maxpagesize); | |
6148 | else | |
6149 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
6150 | hdr->sh_addralign); | |
6151 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
6152 | FALSE); | |
6153 | } | |
6154 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
6155 | && hdr->bfd_section == NULL) | |
1ff6de03 NA |
6156 | /* We don't know the offset of these sections yet: their size has |
6157 | not been decided. */ | |
0ce398f1 | 6158 | || (hdr->bfd_section != NULL |
1ff6de03 NA |
6159 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS |
6160 | || (bfd_section_is_ctf (hdr->bfd_section) | |
6161 | && abfd->is_linker_output))) | |
12bd6957 | 6162 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
6a40cf0c NC |
6163 | || (elf_symtab_shndx_list (abfd) != NULL |
6164 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
6165 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
6166 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
6167 | hdr->sh_offset = -1; |
6168 | else | |
6169 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f | 6170 | } |
30fe1832 | 6171 | elf_next_file_pos (abfd) = off; |
5c182d5f | 6172 | |
252b5132 RH |
6173 | /* Now that we have set the section file positions, we can set up |
6174 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f | 6175 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 6176 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 6177 | { |
129af99f | 6178 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 6179 | { |
66631823 | 6180 | bfd_vma start, end; /* Bytes. */ |
01f7e10c | 6181 | bfd_boolean ok; |
1ea63fd2 | 6182 | |
129af99f | 6183 | if (link_info != NULL) |
8c37241b | 6184 | { |
129af99f | 6185 | /* During linking the range of the RELRO segment is passed |
f2731e0c AM |
6186 | in link_info. Note that there may be padding between |
6187 | relro_start and the first RELRO section. */ | |
6188 | start = link_info->relro_start; | |
6189 | end = link_info->relro_end; | |
6190 | } | |
6191 | else if (m->count != 0) | |
6192 | { | |
6193 | if (!m->p_size_valid) | |
6194 | abort (); | |
6195 | start = m->sections[0]->vma; | |
66631823 | 6196 | end = start + m->p_size / opb; |
f2731e0c AM |
6197 | } |
6198 | else | |
6199 | { | |
6200 | start = 0; | |
6201 | end = 0; | |
6202 | } | |
6203 | ||
01f7e10c | 6204 | ok = FALSE; |
f2731e0c AM |
6205 | if (start < end) |
6206 | { | |
6207 | struct elf_segment_map *lm; | |
6208 | const Elf_Internal_Phdr *lp; | |
6209 | unsigned int i; | |
6210 | ||
6211 | /* Find a LOAD segment containing a section in the RELRO | |
6212 | segment. */ | |
12bd6957 | 6213 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
6214 | lm != NULL; |
6215 | lm = lm->next, lp++) | |
8c37241b JJ |
6216 | { |
6217 | if (lp->p_type == PT_LOAD | |
3146fac4 | 6218 | && lm->count != 0 |
dbc88fc1 AM |
6219 | && (lm->sections[lm->count - 1]->vma |
6220 | + (!IS_TBSS (lm->sections[lm->count - 1]) | |
66631823 | 6221 | ? lm->sections[lm->count - 1]->size / opb |
dbc88fc1 | 6222 | : 0)) > start |
f2731e0c | 6223 | && lm->sections[0]->vma < end) |
8c37241b JJ |
6224 | break; |
6225 | } | |
f2731e0c | 6226 | |
01f7e10c | 6227 | if (lm != NULL) |
129af99f | 6228 | { |
01f7e10c AM |
6229 | /* Find the section starting the RELRO segment. */ |
6230 | for (i = 0; i < lm->count; i++) | |
6231 | { | |
6232 | asection *s = lm->sections[i]; | |
6233 | if (s->vma >= start | |
6234 | && s->vma < end | |
6235 | && s->size != 0) | |
6236 | break; | |
6237 | } | |
6238 | ||
6239 | if (i < lm->count) | |
6240 | { | |
502794d4 CE |
6241 | p->p_vaddr = lm->sections[i]->vma * opb; |
6242 | p->p_paddr = lm->sections[i]->lma * opb; | |
01f7e10c | 6243 | p->p_offset = lm->sections[i]->filepos; |
66631823 | 6244 | p->p_memsz = end * opb - p->p_vaddr; |
01f7e10c AM |
6245 | p->p_filesz = p->p_memsz; |
6246 | ||
6247 | /* The RELRO segment typically ends a few bytes | |
6248 | into .got.plt but other layouts are possible. | |
6249 | In cases where the end does not match any | |
6250 | loaded section (for instance is in file | |
6251 | padding), trim p_filesz back to correspond to | |
6252 | the end of loaded section contents. */ | |
6253 | if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr) | |
6254 | p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr; | |
6255 | ||
6256 | /* Preserve the alignment and flags if they are | |
6257 | valid. The gold linker generates RW/4 for | |
6258 | the PT_GNU_RELRO section. It is better for | |
6259 | objcopy/strip to honor these attributes | |
6260 | otherwise gdb will choke when using separate | |
6261 | debug files. */ | |
6262 | if (!m->p_align_valid) | |
6263 | p->p_align = 1; | |
6264 | if (!m->p_flags_valid) | |
6265 | p->p_flags = PF_R; | |
6266 | ok = TRUE; | |
6267 | } | |
129af99f | 6268 | } |
b84a33b5 | 6269 | } |
01f7e10c AM |
6270 | if (link_info != NULL) |
6271 | BFD_ASSERT (ok); | |
6272 | if (!ok) | |
6273 | memset (p, 0, sizeof *p); | |
129af99f | 6274 | } |
04c3a755 NS |
6275 | else if (p->p_type == PT_GNU_STACK) |
6276 | { | |
6277 | if (m->p_size_valid) | |
6278 | p->p_memsz = m->p_size; | |
6279 | } | |
129af99f AS |
6280 | else if (m->count != 0) |
6281 | { | |
e06efbf1 | 6282 | unsigned int i; |
1a9ccd70 | 6283 | |
129af99f AS |
6284 | if (p->p_type != PT_LOAD |
6285 | && (p->p_type != PT_NOTE | |
6286 | || bfd_get_format (abfd) != bfd_core)) | |
6287 | { | |
1a9ccd70 NC |
6288 | /* A user specified segment layout may include a PHDR |
6289 | segment that overlaps with a LOAD segment... */ | |
6290 | if (p->p_type == PT_PHDR) | |
6291 | { | |
6292 | m->count = 0; | |
6293 | continue; | |
6294 | } | |
6295 | ||
c86934ce NC |
6296 | if (m->includes_filehdr || m->includes_phdrs) |
6297 | { | |
b1fa9dd6 | 6298 | /* PR 17512: file: 2195325e. */ |
4eca0228 | 6299 | _bfd_error_handler |
871b3ab2 | 6300 | (_("%pB: error: non-load segment %d includes file header " |
76cfced5 AM |
6301 | "and/or program header"), |
6302 | abfd, (int) (p - phdrs)); | |
c86934ce NC |
6303 | return FALSE; |
6304 | } | |
129af99f | 6305 | |
86b2281f | 6306 | p->p_filesz = 0; |
129af99f | 6307 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
6308 | for (i = m->count; i-- != 0;) |
6309 | { | |
6310 | asection *sect = m->sections[i]; | |
6311 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
6312 | if (hdr->sh_type != SHT_NOBITS) | |
6313 | { | |
6314 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
6315 | + hdr->sh_size); | |
6316 | break; | |
6317 | } | |
6318 | } | |
129af99f AS |
6319 | } |
6320 | } | |
252b5132 RH |
6321 | } |
6322 | ||
b34976b6 | 6323 | return TRUE; |
252b5132 RH |
6324 | } |
6325 | ||
6a40cf0c NC |
6326 | static elf_section_list * |
6327 | find_section_in_list (unsigned int i, elf_section_list * list) | |
6328 | { | |
6329 | for (;list != NULL; list = list->next) | |
6330 | if (list->ndx == i) | |
6331 | break; | |
6332 | return list; | |
6333 | } | |
6334 | ||
252b5132 RH |
6335 | /* Work out the file positions of all the sections. This is called by |
6336 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
6337 | VMAs must be known before this is called. | |
6338 | ||
e0638f70 | 6339 | Reloc sections come in two flavours: Those processed specially as |
1ff6de03 NA |
6340 | "side-channel" data attached to a section to which they apply, and those that |
6341 | bfd doesn't process as relocations. The latter sort are stored in a normal | |
6342 | bfd section by bfd_section_from_shdr. We don't consider the former sort | |
6343 | here, unless they form part of the loadable image. Reloc sections not | |
6344 | assigned here (and compressed debugging sections and CTF sections which | |
6345 | nothing else in the file can rely upon) will be handled later by | |
e0638f70 | 6346 | assign_file_positions_for_relocs. |
252b5132 RH |
6347 | |
6348 | We also don't set the positions of the .symtab and .strtab here. */ | |
6349 | ||
b34976b6 | 6350 | static bfd_boolean |
c84fca4d AO |
6351 | assign_file_positions_except_relocs (bfd *abfd, |
6352 | struct bfd_link_info *link_info) | |
252b5132 | 6353 | { |
5c182d5f AM |
6354 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
6355 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 6356 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6d6c25c8 | 6357 | unsigned int alloc; |
252b5132 RH |
6358 | |
6359 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
6360 | && bfd_get_format (abfd) != bfd_core) | |
6361 | { | |
5c182d5f AM |
6362 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
6363 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
6364 | Elf_Internal_Shdr **hdrpp; |
6365 | unsigned int i; | |
a485e98e | 6366 | file_ptr off; |
252b5132 RH |
6367 | |
6368 | /* Start after the ELF header. */ | |
6369 | off = i_ehdrp->e_ehsize; | |
6370 | ||
6371 | /* We are not creating an executable, which means that we are | |
6372 | not creating a program header, and that the actual order of | |
6373 | the sections in the file is unimportant. */ | |
9ad5cbcf | 6374 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
6375 | { |
6376 | Elf_Internal_Shdr *hdr; | |
6377 | ||
6378 | hdr = *hdrpp; | |
e0638f70 AM |
6379 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
6380 | && hdr->bfd_section == NULL) | |
1ff6de03 NA |
6381 | /* Do not assign offsets for these sections yet: we don't know |
6382 | their sizes. */ | |
0ce398f1 | 6383 | || (hdr->bfd_section != NULL |
1ff6de03 NA |
6384 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS |
6385 | || (bfd_section_is_ctf (hdr->bfd_section) | |
6386 | && abfd->is_linker_output))) | |
12bd6957 | 6387 | || i == elf_onesymtab (abfd) |
6a40cf0c NC |
6388 | || (elf_symtab_shndx_list (abfd) != NULL |
6389 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
6390 | || i == elf_strtab_sec (abfd) |
6391 | || i == elf_shstrtab_sec (abfd)) | |
252b5132 RH |
6392 | { |
6393 | hdr->sh_offset = -1; | |
252b5132 | 6394 | } |
9ad5cbcf | 6395 | else |
b34976b6 | 6396 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 6397 | } |
a485e98e AM |
6398 | |
6399 | elf_next_file_pos (abfd) = off; | |
6d6c25c8 | 6400 | elf_program_header_size (abfd) = 0; |
252b5132 RH |
6401 | } |
6402 | else | |
6403 | { | |
252b5132 | 6404 | /* Assign file positions for the loaded sections based on the |
08a40648 | 6405 | assignment of sections to segments. */ |
f3520d2f AM |
6406 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
6407 | return FALSE; | |
6408 | ||
6409 | /* And for non-load sections. */ | |
6410 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
6411 | return FALSE; | |
6d6c25c8 | 6412 | } |
f3520d2f | 6413 | |
6d6c25c8 AM |
6414 | if (!(*bed->elf_backend_modify_headers) (abfd, link_info)) |
6415 | return FALSE; | |
1a9ccd70 | 6416 | |
6d6c25c8 AM |
6417 | /* Write out the program headers. */ |
6418 | alloc = i_ehdrp->e_phnum; | |
6419 | if (alloc != 0) | |
6420 | { | |
30fe1832 | 6421 | if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0 |
cd584857 NC |
6422 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) |
6423 | return FALSE; | |
252b5132 RH |
6424 | } |
6425 | ||
b34976b6 | 6426 | return TRUE; |
252b5132 RH |
6427 | } |
6428 | ||
ed7e9d0b AM |
6429 | bfd_boolean |
6430 | _bfd_elf_init_file_header (bfd *abfd, | |
6431 | struct bfd_link_info *info ATTRIBUTE_UNUSED) | |
252b5132 | 6432 | { |
3d540e93 | 6433 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 6434 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 6435 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
6436 | |
6437 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 6438 | |
2b0f7ef9 | 6439 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 6440 | if (shstrtab == NULL) |
b34976b6 | 6441 | return FALSE; |
252b5132 RH |
6442 | |
6443 | elf_shstrtab (abfd) = shstrtab; | |
6444 | ||
6445 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
6446 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
6447 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
6448 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
6449 | ||
6450 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
6451 | i_ehdrp->e_ident[EI_DATA] = | |
6452 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
6453 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
6454 | ||
252b5132 RH |
6455 | if ((abfd->flags & DYNAMIC) != 0) |
6456 | i_ehdrp->e_type = ET_DYN; | |
6457 | else if ((abfd->flags & EXEC_P) != 0) | |
6458 | i_ehdrp->e_type = ET_EXEC; | |
6459 | else if (bfd_get_format (abfd) == bfd_core) | |
6460 | i_ehdrp->e_type = ET_CORE; | |
6461 | else | |
6462 | i_ehdrp->e_type = ET_REL; | |
6463 | ||
6464 | switch (bfd_get_arch (abfd)) | |
6465 | { | |
6466 | case bfd_arch_unknown: | |
6467 | i_ehdrp->e_machine = EM_NONE; | |
6468 | break; | |
aa4f99bb AO |
6469 | |
6470 | /* There used to be a long list of cases here, each one setting | |
6471 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
6472 | in the corresponding bfd definition. To avoid duplication, | |
6473 | the switch was removed. Machines that need special handling | |
6474 | can generally do it in elf_backend_final_write_processing(), | |
6475 | unless they need the information earlier than the final write. | |
6476 | Such need can generally be supplied by replacing the tests for | |
6477 | e_machine with the conditions used to determine it. */ | |
252b5132 | 6478 | default: |
9c5bfbb7 AM |
6479 | i_ehdrp->e_machine = bed->elf_machine_code; |
6480 | } | |
aa4f99bb | 6481 | |
252b5132 RH |
6482 | i_ehdrp->e_version = bed->s->ev_current; |
6483 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
6484 | ||
c044fabd | 6485 | /* No program header, for now. */ |
252b5132 RH |
6486 | i_ehdrp->e_phoff = 0; |
6487 | i_ehdrp->e_phentsize = 0; | |
6488 | i_ehdrp->e_phnum = 0; | |
6489 | ||
c044fabd | 6490 | /* Each bfd section is section header entry. */ |
252b5132 RH |
6491 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
6492 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
6493 | ||
252b5132 | 6494 | elf_tdata (abfd)->symtab_hdr.sh_name = |
b34976b6 | 6495 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 6496 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 6497 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 6498 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 6499 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 6500 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 6501 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 6502 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 6503 | return FALSE; |
252b5132 | 6504 | |
b34976b6 | 6505 | return TRUE; |
252b5132 RH |
6506 | } |
6507 | ||
6d6c25c8 AM |
6508 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. |
6509 | ||
6510 | FIXME: We used to have code here to sort the PT_LOAD segments into | |
6511 | ascending order, as per the ELF spec. But this breaks some programs, | |
6512 | including the Linux kernel. But really either the spec should be | |
6513 | changed or the programs updated. */ | |
6514 | ||
6515 | bfd_boolean | |
6516 | _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info) | |
6517 | { | |
6518 | if (link_info != NULL && bfd_link_pie (link_info)) | |
6519 | { | |
6520 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd); | |
6521 | unsigned int num_segments = i_ehdrp->e_phnum; | |
6522 | struct elf_obj_tdata *tdata = elf_tdata (obfd); | |
6523 | Elf_Internal_Phdr *segment = tdata->phdr; | |
6524 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
6525 | ||
6526 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
6527 | bfd_vma p_vaddr = (bfd_vma) -1; | |
6528 | for (; segment < end_segment; segment++) | |
6529 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
6530 | p_vaddr = segment->p_vaddr; | |
6531 | ||
6532 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
6533 | segments is non-zero. */ | |
6534 | if (p_vaddr) | |
6535 | i_ehdrp->e_type = ET_EXEC; | |
6536 | } | |
6537 | return TRUE; | |
6538 | } | |
6539 | ||
252b5132 | 6540 | /* Assign file positions for all the reloc sections which are not part |
a485e98e | 6541 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 6542 | |
0ce398f1 L |
6543 | static bfd_boolean |
6544 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
6545 | { |
6546 | file_ptr off; | |
e06efbf1 | 6547 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
3e19fb8f | 6548 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
6549 | Elf_Internal_Ehdr *i_ehdrp; |
6550 | const struct elf_backend_data *bed; | |
252b5132 | 6551 | |
12bd6957 | 6552 | off = elf_next_file_pos (abfd); |
252b5132 | 6553 | |
e06efbf1 L |
6554 | shdrpp = elf_elfsections (abfd); |
6555 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
6556 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 6557 | { |
252b5132 | 6558 | shdrp = *shdrpp; |
0ce398f1 L |
6559 | if (shdrp->sh_offset == -1) |
6560 | { | |
3e19fb8f | 6561 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
6562 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
6563 | || shdrp->sh_type == SHT_RELA); | |
1ff6de03 | 6564 | bfd_boolean is_ctf = sec && bfd_section_is_ctf (sec); |
0ce398f1 | 6565 | if (is_rel |
1ff6de03 | 6566 | || is_ctf |
3e19fb8f | 6567 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 | 6568 | { |
1ff6de03 | 6569 | if (!is_rel && !is_ctf) |
0ce398f1 | 6570 | { |
3e19fb8f L |
6571 | const char *name = sec->name; |
6572 | struct bfd_elf_section_data *d; | |
6573 | ||
0ce398f1 | 6574 | /* Compress DWARF debug sections. */ |
3e19fb8f | 6575 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
6576 | shdrp->contents)) |
6577 | return FALSE; | |
3e19fb8f L |
6578 | |
6579 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
6580 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
6581 | { | |
6582 | /* If section is compressed with zlib-gnu, convert | |
6583 | section name from .debug_* to .zdebug_*. */ | |
6584 | char *new_name | |
6585 | = convert_debug_to_zdebug (abfd, name); | |
6586 | if (new_name == NULL) | |
6587 | return FALSE; | |
6588 | name = new_name; | |
6589 | } | |
dd905818 | 6590 | /* Add section name to section name section. */ |
3e19fb8f L |
6591 | if (shdrp->sh_name != (unsigned int) -1) |
6592 | abort (); | |
6593 | shdrp->sh_name | |
6594 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
6595 | name, FALSE); | |
6596 | d = elf_section_data (sec); | |
6597 | ||
dd905818 | 6598 | /* Add reloc section name to section name section. */ |
3e19fb8f L |
6599 | if (d->rel.hdr |
6600 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6601 | d->rel.hdr, | |
6602 | name, FALSE)) | |
6603 | return FALSE; | |
6604 | if (d->rela.hdr | |
6605 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6606 | d->rela.hdr, | |
91cb26da | 6607 | name, TRUE)) |
3e19fb8f L |
6608 | return FALSE; |
6609 | ||
0ce398f1 | 6610 | /* Update section size and contents. */ |
3e19fb8f L |
6611 | shdrp->sh_size = sec->size; |
6612 | shdrp->contents = sec->contents; | |
0ce398f1 L |
6613 | shdrp->bfd_section->contents = NULL; |
6614 | } | |
1ff6de03 NA |
6615 | else if (is_ctf) |
6616 | { | |
6617 | /* Update section size and contents. */ | |
6618 | shdrp->sh_size = sec->size; | |
6619 | shdrp->contents = sec->contents; | |
6620 | } | |
6621 | ||
0ce398f1 L |
6622 | off = _bfd_elf_assign_file_position_for_section (shdrp, |
6623 | off, | |
6624 | TRUE); | |
6625 | } | |
6626 | } | |
252b5132 RH |
6627 | } |
6628 | ||
3e19fb8f L |
6629 | /* Place section name section after DWARF debug sections have been |
6630 | compressed. */ | |
6631 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
6632 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
6633 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
6634 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
6635 | ||
6636 | /* Place the section headers. */ | |
a485e98e AM |
6637 | i_ehdrp = elf_elfheader (abfd); |
6638 | bed = get_elf_backend_data (abfd); | |
6639 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
6640 | i_ehdrp->e_shoff = off; | |
6641 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 6642 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
6643 | |
6644 | return TRUE; | |
252b5132 RH |
6645 | } |
6646 | ||
b34976b6 | 6647 | bfd_boolean |
217aa764 | 6648 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 6649 | { |
9c5bfbb7 | 6650 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6651 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 6652 | bfd_boolean failed; |
9ad5cbcf | 6653 | unsigned int count, num_sec; |
30e8ee25 | 6654 | struct elf_obj_tdata *t; |
252b5132 RH |
6655 | |
6656 | if (! abfd->output_has_begun | |
217aa764 | 6657 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 6658 | return FALSE; |
db727370 JL |
6659 | /* Do not rewrite ELF data when the BFD has been opened for update. |
6660 | abfd->output_has_begun was set to TRUE on opening, so creation of new | |
6661 | sections, and modification of existing section sizes was restricted. | |
6662 | This means the ELF header, program headers and section headers can't have | |
6663 | changed. | |
6664 | If the contents of any sections has been modified, then those changes have | |
6665 | already been written to the BFD. */ | |
6666 | else if (abfd->direction == both_direction) | |
6667 | { | |
6668 | BFD_ASSERT (abfd->output_has_begun); | |
6669 | return TRUE; | |
6670 | } | |
252b5132 RH |
6671 | |
6672 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 6673 | |
b34976b6 | 6674 | failed = FALSE; |
252b5132 RH |
6675 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
6676 | if (failed) | |
b34976b6 | 6677 | return FALSE; |
252b5132 | 6678 | |
0ce398f1 L |
6679 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
6680 | return FALSE; | |
252b5132 | 6681 | |
c044fabd | 6682 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
6683 | num_sec = elf_numsections (abfd); |
6684 | for (count = 1; count < num_sec; count++) | |
252b5132 | 6685 | { |
3e19fb8f L |
6686 | i_shdrp[count]->sh_name |
6687 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
6688 | i_shdrp[count]->sh_name); | |
252b5132 | 6689 | if (bed->elf_backend_section_processing) |
75506100 MR |
6690 | if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count])) |
6691 | return FALSE; | |
252b5132 RH |
6692 | if (i_shdrp[count]->contents) |
6693 | { | |
dc810e39 AM |
6694 | bfd_size_type amt = i_shdrp[count]->sh_size; |
6695 | ||
252b5132 | 6696 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 6697 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 6698 | return FALSE; |
252b5132 RH |
6699 | } |
6700 | } | |
6701 | ||
6702 | /* Write out the section header names. */ | |
30e8ee25 | 6703 | t = elf_tdata (abfd); |
26ae6d5e | 6704 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 6705 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 6706 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 6707 | return FALSE; |
252b5132 | 6708 | |
cc364be6 AM |
6709 | if (!(*bed->elf_backend_final_write_processing) (abfd)) |
6710 | return FALSE; | |
252b5132 | 6711 | |
ff59fc36 RM |
6712 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
6713 | return FALSE; | |
6714 | ||
6715 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
6716 | if (t->o->build_id.after_write_object_contents != NULL) |
6717 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
6718 | |
6719 | return TRUE; | |
252b5132 RH |
6720 | } |
6721 | ||
b34976b6 | 6722 | bfd_boolean |
217aa764 | 6723 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 6724 | { |
c044fabd | 6725 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
6726 | return _bfd_elf_write_object_contents (abfd); |
6727 | } | |
c044fabd KH |
6728 | |
6729 | /* Given a section, search the header to find them. */ | |
6730 | ||
cb33740c | 6731 | unsigned int |
198beae2 | 6732 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 6733 | { |
9c5bfbb7 | 6734 | const struct elf_backend_data *bed; |
91d6fa6a | 6735 | unsigned int sec_index; |
252b5132 | 6736 | |
9ad5cbcf AM |
6737 | if (elf_section_data (asect) != NULL |
6738 | && elf_section_data (asect)->this_idx != 0) | |
6739 | return elf_section_data (asect)->this_idx; | |
6740 | ||
6741 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 6742 | sec_index = SHN_ABS; |
af746e92 | 6743 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 6744 | sec_index = SHN_COMMON; |
af746e92 | 6745 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 6746 | sec_index = SHN_UNDEF; |
af746e92 | 6747 | else |
91d6fa6a | 6748 | sec_index = SHN_BAD; |
252b5132 | 6749 | |
af746e92 | 6750 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
6751 | if (bed->elf_backend_section_from_bfd_section) |
6752 | { | |
91d6fa6a | 6753 | int retval = sec_index; |
9ad5cbcf | 6754 | |
af746e92 AM |
6755 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
6756 | return retval; | |
252b5132 RH |
6757 | } |
6758 | ||
91d6fa6a | 6759 | if (sec_index == SHN_BAD) |
af746e92 | 6760 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 6761 | |
91d6fa6a | 6762 | return sec_index; |
252b5132 RH |
6763 | } |
6764 | ||
6765 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
6766 | on error. */ | |
6767 | ||
6768 | int | |
217aa764 | 6769 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
6770 | { |
6771 | asymbol *asym_ptr = *asym_ptr_ptr; | |
6772 | int idx; | |
6773 | flagword flags = asym_ptr->flags; | |
6774 | ||
6775 | /* When gas creates relocations against local labels, it creates its | |
6776 | own symbol for the section, but does put the symbol into the | |
6777 | symbol chain, so udata is 0. When the linker is generating | |
6778 | relocatable output, this section symbol may be for one of the | |
6779 | input sections rather than the output section. */ | |
6780 | if (asym_ptr->udata.i == 0 | |
6781 | && (flags & BSF_SECTION_SYM) | |
6782 | && asym_ptr->section) | |
6783 | { | |
5372391b | 6784 | asection *sec; |
252b5132 RH |
6785 | int indx; |
6786 | ||
5372391b AM |
6787 | sec = asym_ptr->section; |
6788 | if (sec->owner != abfd && sec->output_section != NULL) | |
6789 | sec = sec->output_section; | |
6790 | if (sec->owner == abfd | |
6791 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 6792 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
6793 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
6794 | } | |
6795 | ||
6796 | idx = asym_ptr->udata.i; | |
6797 | ||
6798 | if (idx == 0) | |
6799 | { | |
6800 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 6801 | which is used in a relocation entry. */ |
4eca0228 | 6802 | _bfd_error_handler |
695344c0 | 6803 | /* xgettext:c-format */ |
871b3ab2 | 6804 | (_("%pB: symbol `%s' required but not present"), |
d003868e | 6805 | abfd, bfd_asymbol_name (asym_ptr)); |
252b5132 RH |
6806 | bfd_set_error (bfd_error_no_symbols); |
6807 | return -1; | |
6808 | } | |
6809 | ||
6810 | #if DEBUG & 4 | |
6811 | { | |
6812 | fprintf (stderr, | |
cd9af601 AM |
6813 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n", |
6814 | (long) asym_ptr, asym_ptr->name, idx, flags); | |
252b5132 RH |
6815 | fflush (stderr); |
6816 | } | |
6817 | #endif | |
6818 | ||
6819 | return idx; | |
6820 | } | |
6821 | ||
84d1d650 | 6822 | /* Rewrite program header information. */ |
252b5132 | 6823 | |
b34976b6 | 6824 | static bfd_boolean |
84d1d650 | 6825 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 6826 | { |
b34976b6 AM |
6827 | Elf_Internal_Ehdr *iehdr; |
6828 | struct elf_segment_map *map; | |
6829 | struct elf_segment_map *map_first; | |
6830 | struct elf_segment_map **pointer_to_map; | |
6831 | Elf_Internal_Phdr *segment; | |
6832 | asection *section; | |
6833 | unsigned int i; | |
6834 | unsigned int num_segments; | |
6835 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 6836 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
6837 | bfd_vma maxpagesize; |
6838 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
6839 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 6840 | const struct elf_backend_data *bed; |
502794d4 | 6841 | unsigned int opb = bfd_octets_per_byte (ibfd, NULL); |
bc67d8a6 | 6842 | |
caf47ea6 | 6843 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
6844 | iehdr = elf_elfheader (ibfd); |
6845 | ||
bc67d8a6 | 6846 | map_first = NULL; |
c044fabd | 6847 | pointer_to_map = &map_first; |
252b5132 RH |
6848 | |
6849 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
6850 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
6851 | ||
6852 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
6853 | #define SEGMENT_END(segment, start) \ |
6854 | (start + (segment->p_memsz > segment->p_filesz \ | |
6855 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 6856 | |
eecdbe52 JJ |
6857 | #define SECTION_SIZE(section, segment) \ |
6858 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
6859 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 6860 | ? section->size : 0) |
eecdbe52 | 6861 | |
b34976b6 | 6862 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6863 | the given segment. VMA addresses are compared. */ |
502794d4 CE |
6864 | #define IS_CONTAINED_BY_VMA(section, segment, opb) \ |
6865 | (section->vma * (opb) >= segment->p_vaddr \ | |
6866 | && (section->vma * (opb) + SECTION_SIZE (section, segment) \ | |
aecc8f8a | 6867 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 6868 | |
b34976b6 | 6869 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6870 | the given segment. LMA addresses are compared. */ |
502794d4 CE |
6871 | #define IS_CONTAINED_BY_LMA(section, segment, base, opb) \ |
6872 | (section->lma * (opb) >= base \ | |
6873 | && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \ | |
6874 | && (section->lma * (opb) + SECTION_SIZE (section, segment) \ | |
aecc8f8a | 6875 | <= SEGMENT_END (segment, base))) |
252b5132 | 6876 | |
0efc80c8 L |
6877 | /* Handle PT_NOTE segment. */ |
6878 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 6879 | (p->p_type == PT_NOTE \ |
0efc80c8 | 6880 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 6881 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6882 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6883 | <= p->p_offset + p->p_filesz)) |
252b5132 | 6884 | |
0efc80c8 L |
6885 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
6886 | etc. */ | |
6887 | #define IS_COREFILE_NOTE(p, s) \ | |
6888 | (IS_NOTE (p, s) \ | |
6889 | && bfd_get_format (ibfd) == bfd_core \ | |
6890 | && s->vma == 0 \ | |
6891 | && s->lma == 0) | |
6892 | ||
252b5132 RH |
6893 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
6894 | linker, which generates a PT_INTERP section with p_vaddr and | |
6895 | p_memsz set to 0. */ | |
aecc8f8a AM |
6896 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
6897 | (p->p_vaddr == 0 \ | |
6898 | && p->p_paddr == 0 \ | |
6899 | && p->p_memsz == 0 \ | |
6900 | && p->p_filesz > 0 \ | |
6901 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 6902 | && s->size > 0 \ |
aecc8f8a | 6903 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6904 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6905 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 6906 | |
bc67d8a6 NC |
6907 | /* Decide if the given section should be included in the given segment. |
6908 | A section will be included if: | |
f5ffc919 | 6909 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 6910 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 6911 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 6912 | segment. |
bc67d8a6 | 6913 | 3. There is an output section associated with it, |
eecdbe52 | 6914 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 6915 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 6916 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
6917 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
6918 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 6919 | (with the possible exception of .dynamic). */ |
502794d4 | 6920 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \ |
2b05f1b7 | 6921 | ((((segment->p_paddr \ |
502794d4 CE |
6922 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \ |
6923 | : IS_CONTAINED_BY_VMA (section, segment, opb)) \ | |
2b05f1b7 | 6924 | && (section->flags & SEC_ALLOC) != 0) \ |
0efc80c8 | 6925 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
6926 | && segment->p_type != PT_GNU_STACK \ |
6927 | && (segment->p_type != PT_TLS \ | |
6928 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
6929 | && (segment->p_type == PT_LOAD \ | |
6930 | || segment->p_type == PT_TLS \ | |
6931 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6932 | && (segment->p_type != PT_DYNAMIC \ | |
6933 | || SECTION_SIZE (section, segment) > 0 \ | |
6934 | || (segment->p_paddr \ | |
502794d4 CE |
6935 | ? segment->p_paddr != section->lma * (opb) \ |
6936 | : segment->p_vaddr != section->vma * (opb)) \ | |
fd361982 | 6937 | || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \ |
9933dc52 | 6938 | && (segment->p_type != PT_LOAD || !section->segment_mark)) |
bc67d8a6 | 6939 | |
9f17e2a6 L |
6940 | /* If the output section of a section in the input segment is NULL, |
6941 | it is removed from the corresponding output segment. */ | |
502794d4 CE |
6942 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \ |
6943 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \ | |
9f17e2a6 L |
6944 | && section->output_section != NULL) |
6945 | ||
b34976b6 | 6946 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
6947 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
6948 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
6949 | ||
6950 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
6951 | their VMA address ranges and their LMA address ranges overlap. | |
6952 | It is possible to have overlapping VMA ranges without overlapping LMA | |
6953 | ranges. RedBoot images for example can have both .data and .bss mapped | |
6954 | to the same VMA range, but with the .data section mapped to a different | |
6955 | LMA. */ | |
aecc8f8a | 6956 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 6957 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 6958 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 6959 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 6960 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
6961 | |
6962 | /* Initialise the segment mark field. */ | |
6963 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 6964 | section->segment_mark = FALSE; |
bc67d8a6 | 6965 | |
5c44b38e AM |
6966 | /* The Solaris linker creates program headers in which all the |
6967 | p_paddr fields are zero. When we try to objcopy or strip such a | |
6968 | file, we get confused. Check for this case, and if we find it | |
6969 | don't set the p_paddr_valid fields. */ | |
6970 | p_paddr_valid = FALSE; | |
6971 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6972 | i < num_segments; | |
6973 | i++, segment++) | |
6974 | if (segment->p_paddr != 0) | |
6975 | { | |
6976 | p_paddr_valid = TRUE; | |
6977 | break; | |
6978 | } | |
6979 | ||
252b5132 | 6980 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6981 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6982 | in the loadable segments. These can be created by weird |
aecc8f8a | 6983 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6984 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6985 | i < num_segments; | |
c044fabd | 6986 | i++, segment++) |
252b5132 | 6987 | { |
252b5132 | 6988 | unsigned int j; |
c044fabd | 6989 | Elf_Internal_Phdr *segment2; |
252b5132 | 6990 | |
aecc8f8a AM |
6991 | if (segment->p_type == PT_INTERP) |
6992 | for (section = ibfd->sections; section; section = section->next) | |
6993 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6994 | { | |
6995 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6996 | assignment code will work. */ |
502794d4 | 6997 | segment->p_vaddr = section->vma * opb; |
aecc8f8a AM |
6998 | break; |
6999 | } | |
7000 | ||
bc67d8a6 | 7001 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
7002 | { |
7003 | /* Remove PT_GNU_RELRO segment. */ | |
7004 | if (segment->p_type == PT_GNU_RELRO) | |
7005 | segment->p_type = PT_NULL; | |
7006 | continue; | |
7007 | } | |
c044fabd | 7008 | |
bc67d8a6 | 7009 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 7010 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
7011 | { |
7012 | bfd_signed_vma extra_length; | |
c044fabd | 7013 | |
bc67d8a6 | 7014 | if (segment2->p_type != PT_LOAD |
0067a569 | 7015 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 7016 | continue; |
c044fabd | 7017 | |
bc67d8a6 NC |
7018 | /* Merge the two segments together. */ |
7019 | if (segment2->p_vaddr < segment->p_vaddr) | |
7020 | { | |
c044fabd | 7021 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 7022 | SEGMENT. */ |
0067a569 AM |
7023 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
7024 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 7025 | |
bc67d8a6 NC |
7026 | if (extra_length > 0) |
7027 | { | |
0067a569 | 7028 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
7029 | segment2->p_filesz += extra_length; |
7030 | } | |
c044fabd | 7031 | |
bc67d8a6 | 7032 | segment->p_type = PT_NULL; |
c044fabd | 7033 | |
bc67d8a6 NC |
7034 | /* Since we have deleted P we must restart the outer loop. */ |
7035 | i = 0; | |
7036 | segment = elf_tdata (ibfd)->phdr; | |
7037 | break; | |
7038 | } | |
7039 | else | |
7040 | { | |
c044fabd | 7041 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 7042 | SEGMENT2. */ |
0067a569 AM |
7043 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
7044 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 7045 | |
bc67d8a6 NC |
7046 | if (extra_length > 0) |
7047 | { | |
0067a569 | 7048 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
7049 | segment->p_filesz += extra_length; |
7050 | } | |
c044fabd | 7051 | |
bc67d8a6 NC |
7052 | segment2->p_type = PT_NULL; |
7053 | } | |
7054 | } | |
7055 | } | |
c044fabd | 7056 | |
bc67d8a6 NC |
7057 | /* The second scan attempts to assign sections to segments. */ |
7058 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7059 | i < num_segments; | |
0067a569 | 7060 | i++, segment++) |
bc67d8a6 | 7061 | { |
0067a569 AM |
7062 | unsigned int section_count; |
7063 | asection **sections; | |
7064 | asection *output_section; | |
7065 | unsigned int isec; | |
9933dc52 AM |
7066 | asection *matching_lma; |
7067 | asection *suggested_lma; | |
0067a569 | 7068 | unsigned int j; |
446f7ed5 | 7069 | size_t amt; |
0067a569 | 7070 | asection *first_section; |
bc67d8a6 NC |
7071 | |
7072 | if (segment->p_type == PT_NULL) | |
7073 | continue; | |
c044fabd | 7074 | |
9f17e2a6 | 7075 | first_section = NULL; |
bc67d8a6 | 7076 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
7077 | for (section = ibfd->sections, section_count = 0; |
7078 | section != NULL; | |
7079 | section = section->next) | |
9f17e2a6 L |
7080 | { |
7081 | /* Find the first section in the input segment, which may be | |
7082 | removed from the corresponding output segment. */ | |
502794d4 | 7083 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb)) |
9f17e2a6 L |
7084 | { |
7085 | if (first_section == NULL) | |
7086 | first_section = section; | |
7087 | if (section->output_section != NULL) | |
7088 | ++section_count; | |
7089 | } | |
7090 | } | |
811072d8 | 7091 | |
b5f852ea NC |
7092 | /* Allocate a segment map big enough to contain |
7093 | all of the sections we have selected. */ | |
00bee008 | 7094 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
446f7ed5 | 7095 | amt += section_count * sizeof (asection *); |
a50b1753 | 7096 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 7097 | if (map == NULL) |
b34976b6 | 7098 | return FALSE; |
252b5132 RH |
7099 | |
7100 | /* Initialise the fields of the segment map. Default to | |
7101 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
7102 | map->next = NULL; |
7103 | map->p_type = segment->p_type; | |
7104 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 7105 | map->p_flags_valid = 1; |
55d55ac7 | 7106 | |
9f17e2a6 L |
7107 | /* If the first section in the input segment is removed, there is |
7108 | no need to preserve segment physical address in the corresponding | |
7109 | output segment. */ | |
945c025a | 7110 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
7111 | { |
7112 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 7113 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 7114 | } |
252b5132 RH |
7115 | |
7116 | /* Determine if this segment contains the ELF file header | |
7117 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
7118 | map->includes_filehdr = (segment->p_offset == 0 |
7119 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 7120 | map->includes_phdrs = 0; |
252b5132 | 7121 | |
0067a569 | 7122 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 7123 | { |
bc67d8a6 NC |
7124 | map->includes_phdrs = |
7125 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7126 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
7127 | >= ((bfd_vma) iehdr->e_phoff |
7128 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 7129 | |
bc67d8a6 | 7130 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 7131 | phdr_included = TRUE; |
252b5132 RH |
7132 | } |
7133 | ||
bc67d8a6 | 7134 | if (section_count == 0) |
252b5132 RH |
7135 | { |
7136 | /* Special segments, such as the PT_PHDR segment, may contain | |
7137 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 | 7138 | something. They are allowed by the ELF spec however, so only |
07d6d2b8 | 7139 | a warning is produced. |
f98450c6 NC |
7140 | There is however the valid use case of embedded systems which |
7141 | have segments with p_filesz of 0 and a p_memsz > 0 to initialize | |
7142 | flash memory with zeros. No warning is shown for that case. */ | |
7143 | if (segment->p_type == PT_LOAD | |
7144 | && (segment->p_filesz > 0 || segment->p_memsz == 0)) | |
7145 | /* xgettext:c-format */ | |
9793eb77 AM |
7146 | _bfd_error_handler |
7147 | (_("%pB: warning: empty loadable segment detected" | |
7148 | " at vaddr=%#" PRIx64 ", is this intentional?"), | |
7149 | ibfd, (uint64_t) segment->p_vaddr); | |
252b5132 | 7150 | |
502794d4 | 7151 | map->p_vaddr_offset = segment->p_vaddr / opb; |
bc67d8a6 | 7152 | map->count = 0; |
c044fabd KH |
7153 | *pointer_to_map = map; |
7154 | pointer_to_map = &map->next; | |
252b5132 RH |
7155 | |
7156 | continue; | |
7157 | } | |
7158 | ||
7159 | /* Now scan the sections in the input BFD again and attempt | |
7160 | to add their corresponding output sections to the segment map. | |
7161 | The problem here is how to handle an output section which has | |
7162 | been moved (ie had its LMA changed). There are four possibilities: | |
7163 | ||
7164 | 1. None of the sections have been moved. | |
7165 | In this case we can continue to use the segment LMA from the | |
7166 | input BFD. | |
7167 | ||
7168 | 2. All of the sections have been moved by the same amount. | |
7169 | In this case we can change the segment's LMA to match the LMA | |
7170 | of the first section. | |
7171 | ||
7172 | 3. Some of the sections have been moved, others have not. | |
7173 | In this case those sections which have not been moved can be | |
7174 | placed in the current segment which will have to have its size, | |
7175 | and possibly its LMA changed, and a new segment or segments will | |
7176 | have to be created to contain the other sections. | |
7177 | ||
b5f852ea | 7178 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
7179 | In this case we can change the segment's LMA to match the LMA |
7180 | of the first section and we will have to create a new segment | |
7181 | or segments to contain the other sections. | |
7182 | ||
7183 | In order to save time, we allocate an array to hold the section | |
7184 | pointers that we are interested in. As these sections get assigned | |
7185 | to a segment, they are removed from this array. */ | |
7186 | ||
446f7ed5 AM |
7187 | amt = section_count * sizeof (asection *); |
7188 | sections = (asection **) bfd_malloc (amt); | |
252b5132 | 7189 | if (sections == NULL) |
b34976b6 | 7190 | return FALSE; |
252b5132 RH |
7191 | |
7192 | /* Step One: Scan for segment vs section LMA conflicts. | |
7193 | Also add the sections to the section array allocated above. | |
7194 | Also add the sections to the current segment. In the common | |
7195 | case, where the sections have not been moved, this means that | |
7196 | we have completely filled the segment, and there is nothing | |
7197 | more to do. */ | |
252b5132 | 7198 | isec = 0; |
9933dc52 AM |
7199 | matching_lma = NULL; |
7200 | suggested_lma = NULL; | |
252b5132 | 7201 | |
461c4b2e | 7202 | for (section = first_section, j = 0; |
bc67d8a6 NC |
7203 | section != NULL; |
7204 | section = section->next) | |
252b5132 | 7205 | { |
502794d4 | 7206 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb)) |
c0f7859b | 7207 | { |
bc67d8a6 NC |
7208 | output_section = section->output_section; |
7209 | ||
0067a569 | 7210 | sections[j++] = section; |
252b5132 RH |
7211 | |
7212 | /* The Solaris native linker always sets p_paddr to 0. | |
7213 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
7214 | correct value. Note - some backends require that |
7215 | p_paddr be left as zero. */ | |
5c44b38e | 7216 | if (!p_paddr_valid |
4455705d | 7217 | && segment->p_vaddr != 0 |
0067a569 | 7218 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 7219 | && isec == 0 |
bc67d8a6 | 7220 | && output_section->lma != 0 |
9933dc52 AM |
7221 | && (align_power (segment->p_vaddr |
7222 | + (map->includes_filehdr | |
7223 | ? iehdr->e_ehsize : 0) | |
7224 | + (map->includes_phdrs | |
7225 | ? iehdr->e_phnum * iehdr->e_phentsize | |
7226 | : 0), | |
66631823 CE |
7227 | output_section->alignment_power * opb) |
7228 | == (output_section->vma * opb))) | |
bc67d8a6 | 7229 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
7230 | |
7231 | /* Match up the physical address of the segment with the | |
7232 | LMA address of the output section. */ | |
502794d4 CE |
7233 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr, |
7234 | opb) | |
5e8d7549 | 7235 | || IS_COREFILE_NOTE (segment, section) |
0067a569 | 7236 | || (bed->want_p_paddr_set_to_zero |
502794d4 | 7237 | && IS_CONTAINED_BY_VMA (output_section, segment, opb))) |
252b5132 | 7238 | { |
9933dc52 AM |
7239 | if (matching_lma == NULL |
7240 | || output_section->lma < matching_lma->lma) | |
7241 | matching_lma = output_section; | |
252b5132 RH |
7242 | |
7243 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 7244 | then it does not overlap any other section within that |
252b5132 | 7245 | segment. */ |
0067a569 AM |
7246 | map->sections[isec++] = output_section; |
7247 | } | |
9933dc52 AM |
7248 | else if (suggested_lma == NULL) |
7249 | suggested_lma = output_section; | |
147d51c2 L |
7250 | |
7251 | if (j == section_count) | |
7252 | break; | |
252b5132 RH |
7253 | } |
7254 | } | |
7255 | ||
bc67d8a6 | 7256 | BFD_ASSERT (j == section_count); |
252b5132 RH |
7257 | |
7258 | /* Step Two: Adjust the physical address of the current segment, | |
7259 | if necessary. */ | |
bc67d8a6 | 7260 | if (isec == section_count) |
252b5132 RH |
7261 | { |
7262 | /* All of the sections fitted within the segment as currently | |
7263 | specified. This is the default case. Add the segment to | |
7264 | the list of built segments and carry on to process the next | |
7265 | program header in the input BFD. */ | |
bc67d8a6 | 7266 | map->count = section_count; |
c044fabd KH |
7267 | *pointer_to_map = map; |
7268 | pointer_to_map = &map->next; | |
08a40648 | 7269 | |
5c44b38e | 7270 | if (p_paddr_valid |
30fe1832 AM |
7271 | && !bed->want_p_paddr_set_to_zero) |
7272 | { | |
7273 | bfd_vma hdr_size = 0; | |
7274 | if (map->includes_filehdr) | |
7275 | hdr_size = iehdr->e_ehsize; | |
7276 | if (map->includes_phdrs) | |
7277 | hdr_size += iehdr->e_phnum * iehdr->e_phentsize; | |
7278 | ||
7279 | /* Account for padding before the first section in the | |
7280 | segment. */ | |
502794d4 CE |
7281 | map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb |
7282 | - matching_lma->lma); | |
30fe1832 | 7283 | } |
08a40648 | 7284 | |
252b5132 RH |
7285 | free (sections); |
7286 | continue; | |
7287 | } | |
252b5132 RH |
7288 | else |
7289 | { | |
9933dc52 AM |
7290 | /* Change the current segment's physical address to match |
7291 | the LMA of the first section that fitted, or if no | |
7292 | section fitted, the first section. */ | |
7293 | if (matching_lma == NULL) | |
7294 | matching_lma = suggested_lma; | |
7295 | ||
66631823 | 7296 | map->p_paddr = matching_lma->lma * opb; |
72730e0c | 7297 | |
bc67d8a6 NC |
7298 | /* Offset the segment physical address from the lma |
7299 | to allow for space taken up by elf headers. */ | |
9933dc52 | 7300 | if (map->includes_phdrs) |
010c8431 | 7301 | { |
9933dc52 AM |
7302 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; |
7303 | ||
7304 | /* iehdr->e_phnum is just an estimate of the number | |
7305 | of program headers that we will need. Make a note | |
7306 | here of the number we used and the segment we chose | |
7307 | to hold these headers, so that we can adjust the | |
7308 | offset when we know the correct value. */ | |
7309 | phdr_adjust_num = iehdr->e_phnum; | |
7310 | phdr_adjust_seg = map; | |
010c8431 | 7311 | } |
252b5132 | 7312 | |
9933dc52 | 7313 | if (map->includes_filehdr) |
bc67d8a6 | 7314 | { |
9933dc52 AM |
7315 | bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power; |
7316 | map->p_paddr -= iehdr->e_ehsize; | |
7317 | /* We've subtracted off the size of headers from the | |
7318 | first section lma, but there may have been some | |
7319 | alignment padding before that section too. Try to | |
7320 | account for that by adjusting the segment lma down to | |
7321 | the same alignment. */ | |
7322 | if (segment->p_align != 0 && segment->p_align < align) | |
7323 | align = segment->p_align; | |
66631823 | 7324 | map->p_paddr &= -(align * opb); |
bc67d8a6 | 7325 | } |
252b5132 RH |
7326 | } |
7327 | ||
7328 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 7329 | those that fit to the current segment and removing them from the |
252b5132 RH |
7330 | sections array; but making sure not to leave large gaps. Once all |
7331 | possible sections have been assigned to the current segment it is | |
7332 | added to the list of built segments and if sections still remain | |
7333 | to be assigned, a new segment is constructed before repeating | |
7334 | the loop. */ | |
7335 | isec = 0; | |
7336 | do | |
7337 | { | |
bc67d8a6 | 7338 | map->count = 0; |
9933dc52 | 7339 | suggested_lma = NULL; |
252b5132 RH |
7340 | |
7341 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 7342 | for (j = 0; j < section_count; j++) |
252b5132 | 7343 | { |
bc67d8a6 | 7344 | section = sections[j]; |
252b5132 | 7345 | |
bc67d8a6 | 7346 | if (section == NULL) |
252b5132 RH |
7347 | continue; |
7348 | ||
bc67d8a6 | 7349 | output_section = section->output_section; |
252b5132 | 7350 | |
bc67d8a6 | 7351 | BFD_ASSERT (output_section != NULL); |
c044fabd | 7352 | |
502794d4 CE |
7353 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr, |
7354 | opb) | |
bc67d8a6 | 7355 | || IS_COREFILE_NOTE (segment, section)) |
252b5132 | 7356 | { |
bc67d8a6 | 7357 | if (map->count == 0) |
252b5132 RH |
7358 | { |
7359 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
7360 | the beginning of the segment, then something is |
7361 | wrong. */ | |
9933dc52 AM |
7362 | if (align_power (map->p_paddr |
7363 | + (map->includes_filehdr | |
7364 | ? iehdr->e_ehsize : 0) | |
7365 | + (map->includes_phdrs | |
7366 | ? iehdr->e_phnum * iehdr->e_phentsize | |
7367 | : 0), | |
66631823 CE |
7368 | output_section->alignment_power * opb) |
7369 | != output_section->lma * opb) | |
9aea1e31 | 7370 | goto sorry; |
252b5132 RH |
7371 | } |
7372 | else | |
7373 | { | |
0067a569 | 7374 | asection *prev_sec; |
252b5132 | 7375 | |
bc67d8a6 | 7376 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
7377 | |
7378 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
7379 | and the start of this section is more than |
7380 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 7381 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 7382 | maxpagesize) |
caf47ea6 | 7383 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 7384 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 7385 | > output_section->lma)) |
252b5132 | 7386 | { |
9933dc52 AM |
7387 | if (suggested_lma == NULL) |
7388 | suggested_lma = output_section; | |
252b5132 RH |
7389 | |
7390 | continue; | |
7391 | } | |
7392 | } | |
7393 | ||
bc67d8a6 | 7394 | map->sections[map->count++] = output_section; |
252b5132 RH |
7395 | ++isec; |
7396 | sections[j] = NULL; | |
9933dc52 AM |
7397 | if (segment->p_type == PT_LOAD) |
7398 | section->segment_mark = TRUE; | |
0067a569 | 7399 | } |
9933dc52 AM |
7400 | else if (suggested_lma == NULL) |
7401 | suggested_lma = output_section; | |
252b5132 RH |
7402 | } |
7403 | ||
beab4532 NC |
7404 | /* PR 23932. A corrupt input file may contain sections that cannot |
7405 | be assigned to any segment - because for example they have a | |
9984857c NC |
7406 | negative size - or segments that do not contain any sections. |
7407 | But there are also valid reasons why a segment can be empty. | |
7408 | So allow a count of zero. */ | |
252b5132 RH |
7409 | |
7410 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
7411 | *pointer_to_map = map; |
7412 | pointer_to_map = &map->next; | |
252b5132 | 7413 | |
bc67d8a6 | 7414 | if (isec < section_count) |
252b5132 RH |
7415 | { |
7416 | /* We still have not allocated all of the sections to | |
7417 | segments. Create a new segment here, initialise it | |
7418 | and carry on looping. */ | |
00bee008 | 7419 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
446f7ed5 | 7420 | amt += section_count * sizeof (asection *); |
5964fc3a | 7421 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 7422 | if (map == NULL) |
5ed6aba4 NC |
7423 | { |
7424 | free (sections); | |
7425 | return FALSE; | |
7426 | } | |
252b5132 RH |
7427 | |
7428 | /* Initialise the fields of the segment map. Set the physical | |
7429 | physical address to the LMA of the first section that has | |
7430 | not yet been assigned. */ | |
0067a569 AM |
7431 | map->next = NULL; |
7432 | map->p_type = segment->p_type; | |
7433 | map->p_flags = segment->p_flags; | |
7434 | map->p_flags_valid = 1; | |
66631823 | 7435 | map->p_paddr = suggested_lma->lma * opb; |
5c44b38e | 7436 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 7437 | map->includes_filehdr = 0; |
0067a569 | 7438 | map->includes_phdrs = 0; |
252b5132 | 7439 | } |
9984857c NC |
7440 | |
7441 | continue; | |
7442 | sorry: | |
7443 | bfd_set_error (bfd_error_sorry); | |
7444 | free (sections); | |
7445 | return FALSE; | |
252b5132 | 7446 | } |
bc67d8a6 | 7447 | while (isec < section_count); |
252b5132 RH |
7448 | |
7449 | free (sections); | |
7450 | } | |
7451 | ||
12bd6957 | 7452 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
7453 | |
7454 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 7455 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
7456 | the offset if necessary. */ |
7457 | if (phdr_adjust_seg != NULL) | |
7458 | { | |
7459 | unsigned int count; | |
c044fabd | 7460 | |
bc67d8a6 | 7461 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 7462 | count++; |
252b5132 | 7463 | |
bc67d8a6 NC |
7464 | if (count > phdr_adjust_num) |
7465 | phdr_adjust_seg->p_paddr | |
7466 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
9933dc52 AM |
7467 | |
7468 | for (map = map_first; map != NULL; map = map->next) | |
7469 | if (map->p_type == PT_PHDR) | |
7470 | { | |
7471 | bfd_vma adjust | |
7472 | = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0; | |
7473 | map->p_paddr = phdr_adjust_seg->p_paddr + adjust; | |
7474 | break; | |
7475 | } | |
bc67d8a6 | 7476 | } |
c044fabd | 7477 | |
bc67d8a6 | 7478 | #undef SEGMENT_END |
eecdbe52 | 7479 | #undef SECTION_SIZE |
bc67d8a6 NC |
7480 | #undef IS_CONTAINED_BY_VMA |
7481 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 7482 | #undef IS_NOTE |
252b5132 | 7483 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 7484 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 7485 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
7486 | #undef INCLUDE_SECTION_IN_SEGMENT |
7487 | #undef SEGMENT_AFTER_SEGMENT | |
7488 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 7489 | return TRUE; |
252b5132 RH |
7490 | } |
7491 | ||
84d1d650 L |
7492 | /* Copy ELF program header information. */ |
7493 | ||
7494 | static bfd_boolean | |
7495 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
7496 | { | |
7497 | Elf_Internal_Ehdr *iehdr; | |
7498 | struct elf_segment_map *map; | |
7499 | struct elf_segment_map *map_first; | |
7500 | struct elf_segment_map **pointer_to_map; | |
7501 | Elf_Internal_Phdr *segment; | |
7502 | unsigned int i; | |
7503 | unsigned int num_segments; | |
7504 | bfd_boolean phdr_included = FALSE; | |
88967714 | 7505 | bfd_boolean p_paddr_valid; |
502794d4 | 7506 | unsigned int opb = bfd_octets_per_byte (ibfd, NULL); |
84d1d650 L |
7507 | |
7508 | iehdr = elf_elfheader (ibfd); | |
7509 | ||
7510 | map_first = NULL; | |
7511 | pointer_to_map = &map_first; | |
7512 | ||
88967714 AM |
7513 | /* If all the segment p_paddr fields are zero, don't set |
7514 | map->p_paddr_valid. */ | |
7515 | p_paddr_valid = FALSE; | |
84d1d650 | 7516 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
7517 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7518 | i < num_segments; | |
7519 | i++, segment++) | |
7520 | if (segment->p_paddr != 0) | |
7521 | { | |
7522 | p_paddr_valid = TRUE; | |
7523 | break; | |
7524 | } | |
7525 | ||
84d1d650 L |
7526 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7527 | i < num_segments; | |
7528 | i++, segment++) | |
7529 | { | |
7530 | asection *section; | |
7531 | unsigned int section_count; | |
986f0783 | 7532 | size_t amt; |
84d1d650 | 7533 | Elf_Internal_Shdr *this_hdr; |
53020534 | 7534 | asection *first_section = NULL; |
a76e6f2f | 7535 | asection *lowest_section; |
84d1d650 | 7536 | |
84d1d650 L |
7537 | /* Compute how many sections are in this segment. */ |
7538 | for (section = ibfd->sections, section_count = 0; | |
7539 | section != NULL; | |
7540 | section = section->next) | |
7541 | { | |
7542 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7543 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 7544 | { |
a76e6f2f AM |
7545 | if (first_section == NULL) |
7546 | first_section = section; | |
3271a814 NS |
7547 | section_count++; |
7548 | } | |
84d1d650 L |
7549 | } |
7550 | ||
7551 | /* Allocate a segment map big enough to contain | |
7552 | all of the sections we have selected. */ | |
00bee008 | 7553 | amt = sizeof (struct elf_segment_map) - sizeof (asection *); |
986f0783 | 7554 | amt += section_count * sizeof (asection *); |
a50b1753 | 7555 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
7556 | if (map == NULL) |
7557 | return FALSE; | |
7558 | ||
7559 | /* Initialize the fields of the output segment map with the | |
7560 | input segment. */ | |
7561 | map->next = NULL; | |
7562 | map->p_type = segment->p_type; | |
7563 | map->p_flags = segment->p_flags; | |
7564 | map->p_flags_valid = 1; | |
7565 | map->p_paddr = segment->p_paddr; | |
88967714 | 7566 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
7567 | map->p_align = segment->p_align; |
7568 | map->p_align_valid = 1; | |
3271a814 | 7569 | map->p_vaddr_offset = 0; |
84d1d650 | 7570 | |
04c3a755 NS |
7571 | if (map->p_type == PT_GNU_RELRO |
7572 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
7573 | { |
7574 | /* The PT_GNU_RELRO segment may contain the first a few | |
7575 | bytes in the .got.plt section even if the whole .got.plt | |
7576 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
7577 | change the size of the PT_GNU_RELRO segment. |
7578 | Similarly, PT_GNU_STACK size is significant on uclinux | |
7579 | systems. */ | |
9433b9b1 | 7580 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
7581 | map->p_size_valid = 1; |
7582 | } | |
7583 | ||
84d1d650 L |
7584 | /* Determine if this segment contains the ELF file header |
7585 | and if it contains the program headers themselves. */ | |
7586 | map->includes_filehdr = (segment->p_offset == 0 | |
7587 | && segment->p_filesz >= iehdr->e_ehsize); | |
7588 | ||
7589 | map->includes_phdrs = 0; | |
7590 | if (! phdr_included || segment->p_type != PT_LOAD) | |
7591 | { | |
7592 | map->includes_phdrs = | |
7593 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7594 | && (segment->p_offset + segment->p_filesz | |
7595 | >= ((bfd_vma) iehdr->e_phoff | |
7596 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
7597 | ||
7598 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
7599 | phdr_included = TRUE; | |
7600 | } | |
7601 | ||
bbefd0a9 | 7602 | lowest_section = NULL; |
84d1d650 L |
7603 | if (section_count != 0) |
7604 | { | |
7605 | unsigned int isec = 0; | |
7606 | ||
53020534 | 7607 | for (section = first_section; |
84d1d650 L |
7608 | section != NULL; |
7609 | section = section->next) | |
7610 | { | |
7611 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7612 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
7613 | { |
7614 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
7615 | if ((section->flags & SEC_ALLOC) != 0) |
7616 | { | |
7617 | bfd_vma seg_off; | |
7618 | ||
bbefd0a9 AM |
7619 | if (lowest_section == NULL |
7620 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
7621 | lowest_section = section; |
7622 | ||
a76e6f2f AM |
7623 | /* Section lmas are set up from PT_LOAD header |
7624 | p_paddr in _bfd_elf_make_section_from_shdr. | |
7625 | If this header has a p_paddr that disagrees | |
7626 | with the section lma, flag the p_paddr as | |
7627 | invalid. */ | |
7628 | if ((section->flags & SEC_LOAD) != 0) | |
7629 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
7630 | else | |
7631 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
502794d4 | 7632 | if (section->lma * opb - segment->p_paddr != seg_off) |
a76e6f2f AM |
7633 | map->p_paddr_valid = FALSE; |
7634 | } | |
53020534 L |
7635 | if (isec == section_count) |
7636 | break; | |
7637 | } | |
84d1d650 L |
7638 | } |
7639 | } | |
7640 | ||
5d695627 | 7641 | if (section_count == 0) |
502794d4 | 7642 | map->p_vaddr_offset = segment->p_vaddr / opb; |
30fe1832 AM |
7643 | else if (map->p_paddr_valid) |
7644 | { | |
7645 | /* Account for padding before the first section in the segment. */ | |
7646 | bfd_vma hdr_size = 0; | |
7647 | if (map->includes_filehdr) | |
7648 | hdr_size = iehdr->e_ehsize; | |
7649 | if (map->includes_phdrs) | |
7650 | hdr_size += iehdr->e_phnum * iehdr->e_phentsize; | |
7651 | ||
502794d4 | 7652 | map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb |
30fe1832 AM |
7653 | - (lowest_section ? lowest_section->lma : 0)); |
7654 | } | |
a76e6f2f | 7655 | |
84d1d650 L |
7656 | map->count = section_count; |
7657 | *pointer_to_map = map; | |
7658 | pointer_to_map = &map->next; | |
7659 | } | |
7660 | ||
12bd6957 | 7661 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
7662 | return TRUE; |
7663 | } | |
7664 | ||
7665 | /* Copy private BFD data. This copies or rewrites ELF program header | |
7666 | information. */ | |
7667 | ||
7668 | static bfd_boolean | |
7669 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
7670 | { | |
84d1d650 L |
7671 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
7672 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7673 | return TRUE; | |
7674 | ||
7675 | if (elf_tdata (ibfd)->phdr == NULL) | |
7676 | return TRUE; | |
7677 | ||
7678 | if (ibfd->xvec == obfd->xvec) | |
7679 | { | |
cb3ff1e5 NC |
7680 | /* Check to see if any sections in the input BFD |
7681 | covered by ELF program header have changed. */ | |
d55ce4e2 | 7682 | Elf_Internal_Phdr *segment; |
84d1d650 L |
7683 | asection *section, *osec; |
7684 | unsigned int i, num_segments; | |
7685 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
7686 | const struct elf_backend_data *bed; |
7687 | ||
7688 | bed = get_elf_backend_data (ibfd); | |
7689 | ||
7690 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
7691 | if (bed->want_p_paddr_set_to_zero) | |
7692 | goto rewrite; | |
84d1d650 L |
7693 | |
7694 | /* Initialize the segment mark field. */ | |
7695 | for (section = obfd->sections; section != NULL; | |
7696 | section = section->next) | |
7697 | section->segment_mark = FALSE; | |
7698 | ||
7699 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
7700 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7701 | i < num_segments; | |
7702 | i++, segment++) | |
7703 | { | |
5f6999aa NC |
7704 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
7705 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
7706 | which severly confuses things, so always regenerate the segment | |
7707 | map in this case. */ | |
7708 | if (segment->p_paddr == 0 | |
7709 | && segment->p_memsz == 0 | |
7710 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 7711 | goto rewrite; |
5f6999aa | 7712 | |
84d1d650 L |
7713 | for (section = ibfd->sections; |
7714 | section != NULL; section = section->next) | |
7715 | { | |
7716 | /* We mark the output section so that we know it comes | |
7717 | from the input BFD. */ | |
7718 | osec = section->output_section; | |
7719 | if (osec) | |
7720 | osec->segment_mark = TRUE; | |
7721 | ||
7722 | /* Check if this section is covered by the segment. */ | |
7723 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7724 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
7725 | { |
7726 | /* FIXME: Check if its output section is changed or | |
7727 | removed. What else do we need to check? */ | |
7728 | if (osec == NULL | |
7729 | || section->flags != osec->flags | |
7730 | || section->lma != osec->lma | |
7731 | || section->vma != osec->vma | |
7732 | || section->size != osec->size | |
7733 | || section->rawsize != osec->rawsize | |
7734 | || section->alignment_power != osec->alignment_power) | |
7735 | goto rewrite; | |
7736 | } | |
7737 | } | |
7738 | } | |
7739 | ||
cb3ff1e5 | 7740 | /* Check to see if any output section do not come from the |
84d1d650 L |
7741 | input BFD. */ |
7742 | for (section = obfd->sections; section != NULL; | |
7743 | section = section->next) | |
7744 | { | |
535b785f | 7745 | if (!section->segment_mark) |
84d1d650 L |
7746 | goto rewrite; |
7747 | else | |
7748 | section->segment_mark = FALSE; | |
7749 | } | |
7750 | ||
7751 | return copy_elf_program_header (ibfd, obfd); | |
7752 | } | |
7753 | ||
dc1e8a47 | 7754 | rewrite: |
f1d85785 L |
7755 | if (ibfd->xvec == obfd->xvec) |
7756 | { | |
7757 | /* When rewriting program header, set the output maxpagesize to | |
7758 | the maximum alignment of input PT_LOAD segments. */ | |
7759 | Elf_Internal_Phdr *segment; | |
7760 | unsigned int i; | |
7761 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
7762 | bfd_vma maxpagesize = 0; | |
7763 | ||
7764 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7765 | i < num_segments; | |
7766 | i++, segment++) | |
7767 | if (segment->p_type == PT_LOAD | |
7768 | && maxpagesize < segment->p_align) | |
c86934ce NC |
7769 | { |
7770 | /* PR 17512: file: f17299af. */ | |
7771 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
695344c0 | 7772 | /* xgettext:c-format */ |
2dcf00ce AM |
7773 | _bfd_error_handler (_("%pB: warning: segment alignment of %#" |
7774 | PRIx64 " is too large"), | |
7775 | ibfd, (uint64_t) segment->p_align); | |
c86934ce NC |
7776 | else |
7777 | maxpagesize = segment->p_align; | |
7778 | } | |
f1d85785 L |
7779 | |
7780 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
7781 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
7782 | } | |
7783 | ||
84d1d650 L |
7784 | return rewrite_elf_program_header (ibfd, obfd); |
7785 | } | |
7786 | ||
ccd2ec6a L |
7787 | /* Initialize private output section information from input section. */ |
7788 | ||
7789 | bfd_boolean | |
7790 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
7791 | asection *isec, | |
7792 | bfd *obfd, | |
7793 | asection *osec, | |
7794 | struct bfd_link_info *link_info) | |
7795 | ||
7796 | { | |
7797 | Elf_Internal_Shdr *ihdr, *ohdr; | |
0e1862bb L |
7798 | bfd_boolean final_link = (link_info != NULL |
7799 | && !bfd_link_relocatable (link_info)); | |
ccd2ec6a L |
7800 | |
7801 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7802 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
7803 | return TRUE; | |
7804 | ||
ba85c43e NC |
7805 | BFD_ASSERT (elf_section_data (osec) != NULL); |
7806 | ||
8c803a2d AM |
7807 | /* If this is a known ABI section, ELF section type and flags may |
7808 | have been set up when OSEC was created. For normal sections we | |
7809 | allow the user to override the type and flags other than | |
7810 | SHF_MASKOS and SHF_MASKPROC. */ | |
7811 | if (elf_section_type (osec) == SHT_PROGBITS | |
7812 | || elf_section_type (osec) == SHT_NOTE | |
7813 | || elf_section_type (osec) == SHT_NOBITS) | |
7814 | elf_section_type (osec) = SHT_NULL; | |
7815 | /* For objcopy and relocatable link, copy the ELF section type from | |
7816 | the input file if the BFD section flags are the same. (If they | |
7817 | are different the user may be doing something like | |
7818 | "objcopy --set-section-flags .text=alloc,data".) For a final | |
7819 | link allow some flags that the linker clears to differ. */ | |
42bb2e33 | 7820 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
7821 | && (osec->flags == isec->flags |
7822 | || (final_link | |
7823 | && ((osec->flags ^ isec->flags) | |
0814be7d | 7824 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 7825 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
7826 | |
7827 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
8c803a2d AM |
7828 | elf_section_flags (osec) = (elf_section_flags (isec) |
7829 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a | 7830 | |
a91e1603 | 7831 | /* Copy sh_info from input for mbind section. */ |
df3a023b AM |
7832 | if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0 |
7833 | && elf_section_flags (isec) & SHF_GNU_MBIND) | |
a91e1603 L |
7834 | elf_section_data (osec)->this_hdr.sh_info |
7835 | = elf_section_data (isec)->this_hdr.sh_info; | |
7836 | ||
ccd2ec6a L |
7837 | /* Set things up for objcopy and relocatable link. The output |
7838 | SHT_GROUP section will have its elf_next_in_group pointing back | |
7839 | to the input group members. Ignore linker created group section. | |
7840 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
7bdf4127 AB |
7841 | if ((link_info == NULL |
7842 | || !link_info->resolve_section_groups) | |
7843 | && (elf_sec_group (isec) == NULL | |
7844 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)) | |
ccd2ec6a | 7845 | { |
7bdf4127 AB |
7846 | if (elf_section_flags (isec) & SHF_GROUP) |
7847 | elf_section_flags (osec) |= SHF_GROUP; | |
7848 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
7849 | elf_section_data (osec)->group = elf_section_data (isec)->group; | |
ccd2ec6a L |
7850 | } |
7851 | ||
7bdf4127 AB |
7852 | /* If not decompress, preserve SHF_COMPRESSED. */ |
7853 | if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0) | |
7854 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7855 | & SHF_COMPRESSED); | |
7856 | ||
ccd2ec6a L |
7857 | ihdr = &elf_section_data (isec)->this_hdr; |
7858 | ||
7859 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
7860 | don't use the output section of the linked-to section since it | |
7861 | may be NULL at this point. */ | |
7862 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
7863 | { | |
7864 | ohdr = &elf_section_data (osec)->this_hdr; | |
7865 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
7866 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
7867 | } | |
7868 | ||
7869 | osec->use_rela_p = isec->use_rela_p; | |
7870 | ||
7871 | return TRUE; | |
7872 | } | |
7873 | ||
252b5132 RH |
7874 | /* Copy private section information. This copies over the entsize |
7875 | field, and sometimes the info field. */ | |
7876 | ||
b34976b6 | 7877 | bfd_boolean |
217aa764 AM |
7878 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
7879 | asection *isec, | |
7880 | bfd *obfd, | |
7881 | asection *osec) | |
252b5132 RH |
7882 | { |
7883 | Elf_Internal_Shdr *ihdr, *ohdr; | |
7884 | ||
7885 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7886 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 7887 | return TRUE; |
252b5132 | 7888 | |
252b5132 RH |
7889 | ihdr = &elf_section_data (isec)->this_hdr; |
7890 | ohdr = &elf_section_data (osec)->this_hdr; | |
7891 | ||
7892 | ohdr->sh_entsize = ihdr->sh_entsize; | |
7893 | ||
7894 | if (ihdr->sh_type == SHT_SYMTAB | |
7895 | || ihdr->sh_type == SHT_DYNSYM | |
7896 | || ihdr->sh_type == SHT_GNU_verneed | |
7897 | || ihdr->sh_type == SHT_GNU_verdef) | |
7898 | ohdr->sh_info = ihdr->sh_info; | |
7899 | ||
ccd2ec6a L |
7900 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
7901 | NULL); | |
252b5132 RH |
7902 | } |
7903 | ||
d0bf826b AM |
7904 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
7905 | necessary if we are removing either the SHT_GROUP section or any of | |
7906 | the group member sections. DISCARDED is the value that a section's | |
7907 | output_section has if the section will be discarded, NULL when this | |
7908 | function is called from objcopy, bfd_abs_section_ptr when called | |
7909 | from the linker. */ | |
80fccad2 BW |
7910 | |
7911 | bfd_boolean | |
d0bf826b | 7912 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 7913 | { |
30288845 AM |
7914 | asection *isec; |
7915 | ||
30288845 | 7916 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 7917 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
7918 | { |
7919 | asection *first = elf_next_in_group (isec); | |
7920 | asection *s = first; | |
d0bf826b AM |
7921 | bfd_size_type removed = 0; |
7922 | ||
30288845 AM |
7923 | while (s != NULL) |
7924 | { | |
415f38a6 AM |
7925 | /* If this member section is being output but the |
7926 | SHT_GROUP section is not, then clear the group info | |
7927 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
7928 | if (s->output_section != discarded |
7929 | && isec->output_section == discarded) | |
30288845 AM |
7930 | { |
7931 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
7932 | elf_group_name (s->output_section) = NULL; | |
7933 | } | |
3349112e | 7934 | else |
6e5e9d58 AM |
7935 | { |
7936 | struct bfd_elf_section_data *elf_sec = elf_section_data (s); | |
3349112e L |
7937 | if (s->output_section == discarded |
7938 | && isec->output_section != discarded) | |
7939 | { | |
7940 | /* Conversely, if the member section is not being | |
7941 | output but the SHT_GROUP section is, then adjust | |
7942 | its size. */ | |
7943 | removed += 4; | |
7944 | if (elf_sec->rel.hdr != NULL | |
7945 | && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0) | |
7946 | removed += 4; | |
7947 | if (elf_sec->rela.hdr != NULL | |
7948 | && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0) | |
7949 | removed += 4; | |
7950 | } | |
7951 | else | |
7952 | { | |
7953 | /* Also adjust for zero-sized relocation member | |
7954 | section. */ | |
7955 | if (elf_sec->rel.hdr != NULL | |
7956 | && elf_sec->rel.hdr->sh_size == 0) | |
7957 | removed += 4; | |
7958 | if (elf_sec->rela.hdr != NULL | |
7959 | && elf_sec->rela.hdr->sh_size == 0) | |
7960 | removed += 4; | |
7961 | } | |
6e5e9d58 | 7962 | } |
30288845 AM |
7963 | s = elf_next_in_group (s); |
7964 | if (s == first) | |
7965 | break; | |
7966 | } | |
d0bf826b AM |
7967 | if (removed != 0) |
7968 | { | |
7969 | if (discarded != NULL) | |
7970 | { | |
7971 | /* If we've been called for ld -r, then we need to | |
6e5e9d58 | 7972 | adjust the input section size. */ |
d0bf826b AM |
7973 | if (isec->rawsize == 0) |
7974 | isec->rawsize = isec->size; | |
7975 | isec->size = isec->rawsize - removed; | |
6e5e9d58 AM |
7976 | if (isec->size <= 4) |
7977 | { | |
7978 | isec->size = 0; | |
7979 | isec->flags |= SEC_EXCLUDE; | |
7980 | } | |
d0bf826b AM |
7981 | } |
7982 | else | |
7983 | { | |
7984 | /* Adjust the output section size when called from | |
7985 | objcopy. */ | |
7986 | isec->output_section->size -= removed; | |
6e5e9d58 AM |
7987 | if (isec->output_section->size <= 4) |
7988 | { | |
7989 | isec->output_section->size = 0; | |
7990 | isec->output_section->flags |= SEC_EXCLUDE; | |
7991 | } | |
d0bf826b AM |
7992 | } |
7993 | } | |
30288845 AM |
7994 | } |
7995 | ||
80fccad2 BW |
7996 | return TRUE; |
7997 | } | |
7998 | ||
d0bf826b AM |
7999 | /* Copy private header information. */ |
8000 | ||
8001 | bfd_boolean | |
8002 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
8003 | { | |
8004 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
8005 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
8006 | return TRUE; | |
8007 | ||
8008 | /* Copy over private BFD data if it has not already been copied. | |
8009 | This must be done here, rather than in the copy_private_bfd_data | |
8010 | entry point, because the latter is called after the section | |
8011 | contents have been set, which means that the program headers have | |
8012 | already been worked out. */ | |
12bd6957 | 8013 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
8014 | { |
8015 | if (! copy_private_bfd_data (ibfd, obfd)) | |
8016 | return FALSE; | |
8017 | } | |
8018 | ||
8019 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
8020 | } | |
8021 | ||
252b5132 RH |
8022 | /* Copy private symbol information. If this symbol is in a section |
8023 | which we did not map into a BFD section, try to map the section | |
8024 | index correctly. We use special macro definitions for the mapped | |
8025 | section indices; these definitions are interpreted by the | |
8026 | swap_out_syms function. */ | |
8027 | ||
9ad5cbcf AM |
8028 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
8029 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
8030 | #define MAP_STRTAB (SHN_HIOS + 3) | |
8031 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
8032 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 8033 | |
b34976b6 | 8034 | bfd_boolean |
217aa764 AM |
8035 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
8036 | asymbol *isymarg, | |
8037 | bfd *obfd, | |
8038 | asymbol *osymarg) | |
252b5132 RH |
8039 | { |
8040 | elf_symbol_type *isym, *osym; | |
8041 | ||
8042 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
8043 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 8044 | return TRUE; |
252b5132 RH |
8045 | |
8046 | isym = elf_symbol_from (ibfd, isymarg); | |
8047 | osym = elf_symbol_from (obfd, osymarg); | |
8048 | ||
8049 | if (isym != NULL | |
8424d8f5 | 8050 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
8051 | && osym != NULL |
8052 | && bfd_is_abs_section (isym->symbol.section)) | |
8053 | { | |
8054 | unsigned int shndx; | |
8055 | ||
8056 | shndx = isym->internal_elf_sym.st_shndx; | |
8057 | if (shndx == elf_onesymtab (ibfd)) | |
8058 | shndx = MAP_ONESYMTAB; | |
8059 | else if (shndx == elf_dynsymtab (ibfd)) | |
8060 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 8061 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 8062 | shndx = MAP_STRTAB; |
12bd6957 | 8063 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 8064 | shndx = MAP_SHSTRTAB; |
6a40cf0c | 8065 | else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) |
9ad5cbcf | 8066 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
8067 | osym->internal_elf_sym.st_shndx = shndx; |
8068 | } | |
8069 | ||
b34976b6 | 8070 | return TRUE; |
252b5132 RH |
8071 | } |
8072 | ||
8073 | /* Swap out the symbols. */ | |
8074 | ||
b34976b6 | 8075 | static bfd_boolean |
217aa764 | 8076 | swap_out_syms (bfd *abfd, |
ef10c3ac | 8077 | struct elf_strtab_hash **sttp, |
217aa764 | 8078 | int relocatable_p) |
252b5132 | 8079 | { |
9c5bfbb7 | 8080 | const struct elf_backend_data *bed; |
1f4361a7 | 8081 | unsigned int symcount; |
079e9a2f | 8082 | asymbol **syms; |
ef10c3ac | 8083 | struct elf_strtab_hash *stt; |
079e9a2f | 8084 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 8085 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 8086 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 8087 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
8088 | bfd_byte *outbound_syms; |
8089 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
8090 | unsigned long outbound_syms_index; |
8091 | unsigned long outbound_shndx_index; | |
1f4361a7 | 8092 | unsigned int idx; |
12bd6957 | 8093 | unsigned int num_locals; |
1f4361a7 | 8094 | size_t amt; |
174fd7f9 | 8095 | bfd_boolean name_local_sections; |
252b5132 | 8096 | |
12bd6957 | 8097 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 8098 | return FALSE; |
252b5132 | 8099 | |
c044fabd | 8100 | /* Dump out the symtabs. */ |
ef10c3ac | 8101 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 8102 | if (stt == NULL) |
b34976b6 | 8103 | return FALSE; |
252b5132 | 8104 | |
079e9a2f AM |
8105 | bed = get_elf_backend_data (abfd); |
8106 | symcount = bfd_get_symcount (abfd); | |
8107 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
8108 | symtab_hdr->sh_type = SHT_SYMTAB; | |
8109 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
8110 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 8111 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 8112 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
8113 | |
8114 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
8115 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
8116 | ||
ef10c3ac | 8117 | /* Allocate buffer to swap out the .strtab section. */ |
1f4361a7 AM |
8118 | if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt) |
8119 | || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL) | |
ef10c3ac | 8120 | { |
1f4361a7 | 8121 | bfd_set_error (bfd_error_no_memory); |
ef10c3ac L |
8122 | _bfd_elf_strtab_free (stt); |
8123 | return FALSE; | |
8124 | } | |
8125 | ||
1f4361a7 AM |
8126 | if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt) |
8127 | || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL) | |
5ed6aba4 | 8128 | { |
1f4361a7 AM |
8129 | error_no_mem: |
8130 | bfd_set_error (bfd_error_no_memory); | |
8131 | error_return: | |
ef10c3ac | 8132 | free (symstrtab); |
1f4361a7 | 8133 | _bfd_elf_strtab_free (stt); |
5ed6aba4 NC |
8134 | return FALSE; |
8135 | } | |
217aa764 | 8136 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 8137 | outbound_syms_index = 0; |
252b5132 | 8138 | |
9ad5cbcf | 8139 | outbound_shndx = NULL; |
ef10c3ac | 8140 | outbound_shndx_index = 0; |
6a40cf0c NC |
8141 | |
8142 | if (elf_symtab_shndx_list (abfd)) | |
9ad5cbcf | 8143 | { |
6a40cf0c NC |
8144 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
8145 | if (symtab_shndx_hdr->sh_name != 0) | |
8146 | { | |
1f4361a7 AM |
8147 | if (_bfd_mul_overflow (symcount + 1, |
8148 | sizeof (Elf_External_Sym_Shndx), &amt)) | |
8149 | goto error_no_mem; | |
8150 | outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt); | |
6a40cf0c NC |
8151 | if (outbound_shndx == NULL) |
8152 | goto error_return; | |
5ed6aba4 | 8153 | |
6a40cf0c NC |
8154 | symtab_shndx_hdr->contents = outbound_shndx; |
8155 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
8156 | symtab_shndx_hdr->sh_size = amt; | |
8157 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
8158 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
8159 | } | |
8160 | /* FIXME: What about any other headers in the list ? */ | |
9ad5cbcf AM |
8161 | } |
8162 | ||
589e6347 | 8163 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
8164 | { |
8165 | /* Fill in zeroth symbol and swap it out. */ | |
8166 | Elf_Internal_Sym sym; | |
8167 | sym.st_name = 0; | |
8168 | sym.st_value = 0; | |
8169 | sym.st_size = 0; | |
8170 | sym.st_info = 0; | |
8171 | sym.st_other = 0; | |
8172 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 8173 | sym.st_target_internal = 0; |
ef10c3ac L |
8174 | symstrtab[0].sym = sym; |
8175 | symstrtab[0].dest_index = outbound_syms_index; | |
8176 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
8177 | outbound_syms_index++; | |
9ad5cbcf | 8178 | if (outbound_shndx != NULL) |
ef10c3ac | 8179 | outbound_shndx_index++; |
079e9a2f | 8180 | } |
252b5132 | 8181 | |
174fd7f9 RS |
8182 | name_local_sections |
8183 | = (bed->elf_backend_name_local_section_symbols | |
8184 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
8185 | ||
079e9a2f | 8186 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 8187 | for (idx = 0; idx < symcount;) |
252b5132 | 8188 | { |
252b5132 | 8189 | Elf_Internal_Sym sym; |
079e9a2f AM |
8190 | bfd_vma value = syms[idx]->value; |
8191 | elf_symbol_type *type_ptr; | |
8192 | flagword flags = syms[idx]->flags; | |
8193 | int type; | |
252b5132 | 8194 | |
174fd7f9 RS |
8195 | if (!name_local_sections |
8196 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
8197 | { |
8198 | /* Local section symbols have no name. */ | |
ef10c3ac | 8199 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
8200 | } |
8201 | else | |
8202 | { | |
ef10c3ac L |
8203 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
8204 | to get the final offset for st_name. */ | |
8205 | sym.st_name | |
8206 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
8207 | FALSE); | |
079e9a2f | 8208 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 8209 | goto error_return; |
079e9a2f | 8210 | } |
252b5132 | 8211 | |
079e9a2f | 8212 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 8213 | |
079e9a2f AM |
8214 | if ((flags & BSF_SECTION_SYM) == 0 |
8215 | && bfd_is_com_section (syms[idx]->section)) | |
8216 | { | |
8217 | /* ELF common symbols put the alignment into the `value' field, | |
8218 | and the size into the `size' field. This is backwards from | |
8219 | how BFD handles it, so reverse it here. */ | |
8220 | sym.st_size = value; | |
8221 | if (type_ptr == NULL | |
8222 | || type_ptr->internal_elf_sym.st_value == 0) | |
8223 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
8224 | else | |
8225 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
8226 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
8227 | (abfd, syms[idx]->section); | |
8228 | } | |
8229 | else | |
8230 | { | |
8231 | asection *sec = syms[idx]->section; | |
cb33740c | 8232 | unsigned int shndx; |
252b5132 | 8233 | |
079e9a2f AM |
8234 | if (sec->output_section) |
8235 | { | |
8236 | value += sec->output_offset; | |
8237 | sec = sec->output_section; | |
8238 | } | |
589e6347 | 8239 | |
079e9a2f AM |
8240 | /* Don't add in the section vma for relocatable output. */ |
8241 | if (! relocatable_p) | |
8242 | value += sec->vma; | |
8243 | sym.st_value = value; | |
8244 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
8245 | ||
8246 | if (bfd_is_abs_section (sec) | |
8247 | && type_ptr != NULL | |
8248 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
8249 | { | |
8250 | /* This symbol is in a real ELF section which we did | |
8251 | not create as a BFD section. Undo the mapping done | |
8252 | by copy_private_symbol_data. */ | |
8253 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
8254 | switch (shndx) | |
8255 | { | |
8256 | case MAP_ONESYMTAB: | |
8257 | shndx = elf_onesymtab (abfd); | |
8258 | break; | |
8259 | case MAP_DYNSYMTAB: | |
8260 | shndx = elf_dynsymtab (abfd); | |
8261 | break; | |
8262 | case MAP_STRTAB: | |
12bd6957 | 8263 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
8264 | break; |
8265 | case MAP_SHSTRTAB: | |
12bd6957 | 8266 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 8267 | break; |
9ad5cbcf | 8268 | case MAP_SYM_SHNDX: |
6a40cf0c NC |
8269 | if (elf_symtab_shndx_list (abfd)) |
8270 | shndx = elf_symtab_shndx_list (abfd)->ndx; | |
9ad5cbcf | 8271 | break; |
00e49dff NC |
8272 | case SHN_COMMON: |
8273 | case SHN_ABS: | |
15bc576a | 8274 | shndx = SHN_ABS; |
079e9a2f | 8275 | break; |
00e49dff NC |
8276 | default: |
8277 | if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS) | |
8278 | { | |
8279 | if (bed->symbol_section_index) | |
8280 | shndx = bed->symbol_section_index (abfd, type_ptr); | |
8281 | /* Otherwise just leave the index alone. */ | |
8282 | } | |
8283 | else | |
8284 | { | |
8285 | if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE) | |
8286 | _bfd_error_handler (_("%pB: \ | |
8287 | Unable to handle section index %x in ELF symbol. Using ABS instead."), | |
8288 | abfd, shndx); | |
8289 | shndx = SHN_ABS; | |
8290 | } | |
8291 | break; | |
079e9a2f AM |
8292 | } |
8293 | } | |
8294 | else | |
8295 | { | |
8296 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 8297 | |
cb33740c | 8298 | if (shndx == SHN_BAD) |
079e9a2f AM |
8299 | { |
8300 | asection *sec2; | |
8301 | ||
8302 | /* Writing this would be a hell of a lot easier if | |
8303 | we had some decent documentation on bfd, and | |
8304 | knew what to expect of the library, and what to | |
8305 | demand of applications. For example, it | |
8306 | appears that `objcopy' might not set the | |
8307 | section of a symbol to be a section that is | |
8308 | actually in the output file. */ | |
8309 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
5df1bc57 AM |
8310 | if (sec2 != NULL) |
8311 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
8312 | if (shndx == SHN_BAD) | |
589e6347 | 8313 | { |
695344c0 | 8314 | /* xgettext:c-format */ |
9793eb77 AM |
8315 | _bfd_error_handler |
8316 | (_("unable to find equivalent output section" | |
8317 | " for symbol '%s' from section '%s'"), | |
8318 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
8319 | sec->name); | |
811072d8 | 8320 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 8321 | goto error_return; |
589e6347 | 8322 | } |
079e9a2f AM |
8323 | } |
8324 | } | |
252b5132 | 8325 | |
079e9a2f AM |
8326 | sym.st_shndx = shndx; |
8327 | } | |
252b5132 | 8328 | |
13ae64f3 JJ |
8329 | if ((flags & BSF_THREAD_LOCAL) != 0) |
8330 | type = STT_TLS; | |
d8045f23 NC |
8331 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
8332 | type = STT_GNU_IFUNC; | |
13ae64f3 | 8333 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
8334 | type = STT_FUNC; |
8335 | else if ((flags & BSF_OBJECT) != 0) | |
8336 | type = STT_OBJECT; | |
d9352518 DB |
8337 | else if ((flags & BSF_RELC) != 0) |
8338 | type = STT_RELC; | |
8339 | else if ((flags & BSF_SRELC) != 0) | |
8340 | type = STT_SRELC; | |
079e9a2f AM |
8341 | else |
8342 | type = STT_NOTYPE; | |
252b5132 | 8343 | |
13ae64f3 JJ |
8344 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
8345 | type = STT_TLS; | |
8346 | ||
589e6347 | 8347 | /* Processor-specific types. */ |
079e9a2f AM |
8348 | if (type_ptr != NULL |
8349 | && bed->elf_backend_get_symbol_type) | |
8350 | type = ((*bed->elf_backend_get_symbol_type) | |
8351 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 8352 | |
079e9a2f AM |
8353 | if (flags & BSF_SECTION_SYM) |
8354 | { | |
8355 | if (flags & BSF_GLOBAL) | |
8356 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
8357 | else | |
8358 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
8359 | } | |
8360 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 8361 | { |
b8871f35 L |
8362 | if (type != STT_TLS) |
8363 | { | |
8364 | if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) | |
8365 | type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) | |
8366 | ? STT_COMMON : STT_OBJECT); | |
8367 | else | |
8368 | type = ((flags & BSF_ELF_COMMON) != 0 | |
8369 | ? STT_COMMON : STT_OBJECT); | |
8370 | } | |
8371 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
0a40daed | 8372 | } |
079e9a2f AM |
8373 | else if (bfd_is_und_section (syms[idx]->section)) |
8374 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
8375 | ? STB_WEAK | |
8376 | : STB_GLOBAL), | |
8377 | type); | |
8378 | else if (flags & BSF_FILE) | |
8379 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
8380 | else | |
8381 | { | |
8382 | int bind = STB_LOCAL; | |
252b5132 | 8383 | |
079e9a2f AM |
8384 | if (flags & BSF_LOCAL) |
8385 | bind = STB_LOCAL; | |
3e7a7d11 NC |
8386 | else if (flags & BSF_GNU_UNIQUE) |
8387 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
8388 | else if (flags & BSF_WEAK) |
8389 | bind = STB_WEAK; | |
8390 | else if (flags & BSF_GLOBAL) | |
8391 | bind = STB_GLOBAL; | |
252b5132 | 8392 | |
079e9a2f AM |
8393 | sym.st_info = ELF_ST_INFO (bind, type); |
8394 | } | |
252b5132 | 8395 | |
079e9a2f | 8396 | if (type_ptr != NULL) |
35fc36a8 RS |
8397 | { |
8398 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
8399 | sym.st_target_internal | |
8400 | = type_ptr->internal_elf_sym.st_target_internal; | |
8401 | } | |
079e9a2f | 8402 | else |
35fc36a8 RS |
8403 | { |
8404 | sym.st_other = 0; | |
8405 | sym.st_target_internal = 0; | |
8406 | } | |
252b5132 | 8407 | |
ef10c3ac L |
8408 | idx++; |
8409 | symstrtab[idx].sym = sym; | |
8410 | symstrtab[idx].dest_index = outbound_syms_index; | |
8411 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
8412 | ||
8413 | outbound_syms_index++; | |
9ad5cbcf | 8414 | if (outbound_shndx != NULL) |
ef10c3ac L |
8415 | outbound_shndx_index++; |
8416 | } | |
8417 | ||
8418 | /* Finalize the .strtab section. */ | |
8419 | _bfd_elf_strtab_finalize (stt); | |
8420 | ||
8421 | /* Swap out the .strtab section. */ | |
8422 | for (idx = 0; idx <= symcount; idx++) | |
8423 | { | |
8424 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
8425 | if (elfsym->sym.st_name == (unsigned long) -1) | |
8426 | elfsym->sym.st_name = 0; | |
8427 | else | |
8428 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
8429 | elfsym->sym.st_name); | |
8430 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
8431 | (outbound_syms | |
8432 | + (elfsym->dest_index | |
8433 | * bed->s->sizeof_sym)), | |
8434 | (outbound_shndx | |
8435 | + (elfsym->destshndx_index | |
8436 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 8437 | } |
ef10c3ac | 8438 | free (symstrtab); |
252b5132 | 8439 | |
079e9a2f | 8440 | *sttp = stt; |
ef10c3ac | 8441 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 8442 | symstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 8443 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
079e9a2f AM |
8444 | symstrtab_hdr->sh_addr = 0; |
8445 | symstrtab_hdr->sh_entsize = 0; | |
8446 | symstrtab_hdr->sh_link = 0; | |
8447 | symstrtab_hdr->sh_info = 0; | |
8448 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 8449 | |
b34976b6 | 8450 | return TRUE; |
252b5132 RH |
8451 | } |
8452 | ||
8453 | /* Return the number of bytes required to hold the symtab vector. | |
8454 | ||
8455 | Note that we base it on the count plus 1, since we will null terminate | |
8456 | the vector allocated based on this size. However, the ELF symbol table | |
8457 | always has a dummy entry as symbol #0, so it ends up even. */ | |
8458 | ||
8459 | long | |
217aa764 | 8460 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 | 8461 | { |
3a551c7a | 8462 | bfd_size_type symcount; |
252b5132 RH |
8463 | long symtab_size; |
8464 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
8465 | ||
8466 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3a551c7a AM |
8467 | if (symcount >= LONG_MAX / sizeof (asymbol *)) |
8468 | { | |
8469 | bfd_set_error (bfd_error_file_too_big); | |
8470 | return -1; | |
8471 | } | |
b99d1833 AM |
8472 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8473 | if (symcount > 0) | |
8474 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8475 | |
8476 | return symtab_size; | |
8477 | } | |
8478 | ||
8479 | long | |
217aa764 | 8480 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 | 8481 | { |
3a551c7a | 8482 | bfd_size_type symcount; |
252b5132 RH |
8483 | long symtab_size; |
8484 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
8485 | ||
8486 | if (elf_dynsymtab (abfd) == 0) | |
8487 | { | |
8488 | bfd_set_error (bfd_error_invalid_operation); | |
8489 | return -1; | |
8490 | } | |
8491 | ||
8492 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3a551c7a AM |
8493 | if (symcount >= LONG_MAX / sizeof (asymbol *)) |
8494 | { | |
8495 | bfd_set_error (bfd_error_file_too_big); | |
8496 | return -1; | |
8497 | } | |
b99d1833 AM |
8498 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8499 | if (symcount > 0) | |
8500 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8501 | |
8502 | return symtab_size; | |
8503 | } | |
8504 | ||
8505 | long | |
217aa764 AM |
8506 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
8507 | sec_ptr asect) | |
252b5132 | 8508 | { |
242a1159 | 8509 | #if SIZEOF_LONG == SIZEOF_INT |
7a6e0d89 AM |
8510 | if (asect->reloc_count >= LONG_MAX / sizeof (arelent *)) |
8511 | { | |
8512 | bfd_set_error (bfd_error_file_too_big); | |
8513 | return -1; | |
8514 | } | |
242a1159 | 8515 | #endif |
252b5132 RH |
8516 | return (asect->reloc_count + 1) * sizeof (arelent *); |
8517 | } | |
8518 | ||
8519 | /* Canonicalize the relocs. */ | |
8520 | ||
8521 | long | |
217aa764 AM |
8522 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
8523 | sec_ptr section, | |
8524 | arelent **relptr, | |
8525 | asymbol **symbols) | |
252b5132 RH |
8526 | { |
8527 | arelent *tblptr; | |
8528 | unsigned int i; | |
9c5bfbb7 | 8529 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 8530 | |
b34976b6 | 8531 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
8532 | return -1; |
8533 | ||
8534 | tblptr = section->relocation; | |
8535 | for (i = 0; i < section->reloc_count; i++) | |
8536 | *relptr++ = tblptr++; | |
8537 | ||
8538 | *relptr = NULL; | |
8539 | ||
8540 | return section->reloc_count; | |
8541 | } | |
8542 | ||
8543 | long | |
6cee3f79 | 8544 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 8545 | { |
9c5bfbb7 | 8546 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8547 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
8548 | |
8549 | if (symcount >= 0) | |
ed48ec2e | 8550 | abfd->symcount = symcount; |
252b5132 RH |
8551 | return symcount; |
8552 | } | |
8553 | ||
8554 | long | |
217aa764 AM |
8555 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
8556 | asymbol **allocation) | |
252b5132 | 8557 | { |
9c5bfbb7 | 8558 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8559 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
8560 | |
8561 | if (symcount >= 0) | |
ed48ec2e | 8562 | abfd->dynsymcount = symcount; |
1f70368c | 8563 | return symcount; |
252b5132 RH |
8564 | } |
8565 | ||
8615f3f2 AM |
8566 | /* Return the size required for the dynamic reloc entries. Any loadable |
8567 | section that was actually installed in the BFD, and has type SHT_REL | |
8568 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
8569 | dynamic reloc section. */ | |
252b5132 RH |
8570 | |
8571 | long | |
217aa764 | 8572 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 | 8573 | { |
3a551c7a | 8574 | bfd_size_type count; |
252b5132 RH |
8575 | asection *s; |
8576 | ||
8577 | if (elf_dynsymtab (abfd) == 0) | |
8578 | { | |
8579 | bfd_set_error (bfd_error_invalid_operation); | |
8580 | return -1; | |
8581 | } | |
8582 | ||
3a551c7a | 8583 | count = 1; |
252b5132 | 8584 | for (s = abfd->sections; s != NULL; s = s->next) |
266b05cf | 8585 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8586 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8587 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
3a551c7a AM |
8588 | { |
8589 | count += s->size / elf_section_data (s)->this_hdr.sh_entsize; | |
8590 | if (count > LONG_MAX / sizeof (arelent *)) | |
8591 | { | |
8592 | bfd_set_error (bfd_error_file_too_big); | |
8593 | return -1; | |
8594 | } | |
8595 | } | |
8596 | return count * sizeof (arelent *); | |
252b5132 RH |
8597 | } |
8598 | ||
8615f3f2 AM |
8599 | /* Canonicalize the dynamic relocation entries. Note that we return the |
8600 | dynamic relocations as a single block, although they are actually | |
8601 | associated with particular sections; the interface, which was | |
8602 | designed for SunOS style shared libraries, expects that there is only | |
8603 | one set of dynamic relocs. Any loadable section that was actually | |
8604 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
8605 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
8606 | |
8607 | long | |
217aa764 AM |
8608 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
8609 | arelent **storage, | |
8610 | asymbol **syms) | |
252b5132 | 8611 | { |
217aa764 | 8612 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
8613 | asection *s; |
8614 | long ret; | |
8615 | ||
8616 | if (elf_dynsymtab (abfd) == 0) | |
8617 | { | |
8618 | bfd_set_error (bfd_error_invalid_operation); | |
8619 | return -1; | |
8620 | } | |
8621 | ||
8622 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
8623 | ret = 0; | |
8624 | for (s = abfd->sections; s != NULL; s = s->next) | |
8625 | { | |
266b05cf | 8626 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8627 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8628 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
8629 | { | |
8630 | arelent *p; | |
8631 | long count, i; | |
8632 | ||
b34976b6 | 8633 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 8634 | return -1; |
eea6121a | 8635 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
8636 | p = s->relocation; |
8637 | for (i = 0; i < count; i++) | |
8638 | *storage++ = p++; | |
8639 | ret += count; | |
8640 | } | |
8641 | } | |
8642 | ||
8643 | *storage = NULL; | |
8644 | ||
8645 | return ret; | |
8646 | } | |
8647 | \f | |
8648 | /* Read in the version information. */ | |
8649 | ||
b34976b6 | 8650 | bfd_boolean |
fc0e6df6 | 8651 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
8652 | { |
8653 | bfd_byte *contents = NULL; | |
fc0e6df6 | 8654 | unsigned int freeidx = 0; |
1f4361a7 | 8655 | size_t amt; |
fc0e6df6 PB |
8656 | |
8657 | if (elf_dynverref (abfd) != 0) | |
8658 | { | |
8659 | Elf_Internal_Shdr *hdr; | |
8660 | Elf_External_Verneed *everneed; | |
8661 | Elf_Internal_Verneed *iverneed; | |
8662 | unsigned int i; | |
d0fb9a8d | 8663 | bfd_byte *contents_end; |
fc0e6df6 PB |
8664 | |
8665 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
8666 | ||
bd61e135 AM |
8667 | if (hdr->sh_info == 0 |
8668 | || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed)) | |
d0fb9a8d | 8669 | { |
dc1e8a47 | 8670 | error_return_bad_verref: |
4eca0228 | 8671 | _bfd_error_handler |
871b3ab2 | 8672 | (_("%pB: .gnu.version_r invalid entry"), abfd); |
601a03ba | 8673 | bfd_set_error (bfd_error_bad_value); |
dc1e8a47 | 8674 | error_return_verref: |
d0fb9a8d JJ |
8675 | elf_tdata (abfd)->verref = NULL; |
8676 | elf_tdata (abfd)->cverrefs = 0; | |
8677 | goto error_return; | |
8678 | } | |
601a03ba | 8679 | |
2bb3687b AM |
8680 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) |
8681 | goto error_return_verref; | |
8682 | contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size); | |
8683 | if (contents == NULL) | |
d0fb9a8d | 8684 | goto error_return_verref; |
fc0e6df6 | 8685 | |
1f4361a7 AM |
8686 | if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt)) |
8687 | { | |
8688 | bfd_set_error (bfd_error_file_too_big); | |
8689 | goto error_return_verref; | |
8690 | } | |
8691 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt); | |
601a03ba | 8692 | if (elf_tdata (abfd)->verref == NULL) |
d0fb9a8d JJ |
8693 | goto error_return_verref; |
8694 | ||
8695 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
8696 | == sizeof (Elf_External_Vernaux)); | |
8697 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
8698 | everneed = (Elf_External_Verneed *) contents; |
8699 | iverneed = elf_tdata (abfd)->verref; | |
8700 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
8701 | { | |
8702 | Elf_External_Vernaux *evernaux; | |
8703 | Elf_Internal_Vernaux *ivernaux; | |
8704 | unsigned int j; | |
8705 | ||
8706 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
8707 | ||
8708 | iverneed->vn_bfd = abfd; | |
8709 | ||
8710 | iverneed->vn_filename = | |
8711 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8712 | iverneed->vn_file); | |
8713 | if (iverneed->vn_filename == NULL) | |
601a03ba | 8714 | goto error_return_bad_verref; |
fc0e6df6 | 8715 | |
d0fb9a8d JJ |
8716 | if (iverneed->vn_cnt == 0) |
8717 | iverneed->vn_auxptr = NULL; | |
8718 | else | |
8719 | { | |
1f4361a7 AM |
8720 | if (_bfd_mul_overflow (iverneed->vn_cnt, |
8721 | sizeof (Elf_Internal_Vernaux), &amt)) | |
8722 | { | |
8723 | bfd_set_error (bfd_error_file_too_big); | |
8724 | goto error_return_verref; | |
8725 | } | |
a50b1753 | 8726 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
1f4361a7 | 8727 | bfd_alloc (abfd, amt); |
d0fb9a8d JJ |
8728 | if (iverneed->vn_auxptr == NULL) |
8729 | goto error_return_verref; | |
8730 | } | |
8731 | ||
8732 | if (iverneed->vn_aux | |
8733 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8734 | goto error_return_bad_verref; |
fc0e6df6 PB |
8735 | |
8736 | evernaux = ((Elf_External_Vernaux *) | |
8737 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
8738 | ivernaux = iverneed->vn_auxptr; | |
8739 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
8740 | { | |
8741 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
8742 | ||
8743 | ivernaux->vna_nodename = | |
8744 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8745 | ivernaux->vna_name); | |
8746 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 8747 | goto error_return_bad_verref; |
fc0e6df6 | 8748 | |
25ff461f AM |
8749 | if (ivernaux->vna_other > freeidx) |
8750 | freeidx = ivernaux->vna_other; | |
8751 | ||
8752 | ivernaux->vna_nextptr = NULL; | |
8753 | if (ivernaux->vna_next == 0) | |
8754 | { | |
8755 | iverneed->vn_cnt = j + 1; | |
8756 | break; | |
8757 | } | |
fc0e6df6 PB |
8758 | if (j + 1 < iverneed->vn_cnt) |
8759 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 8760 | |
d0fb9a8d JJ |
8761 | if (ivernaux->vna_next |
8762 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 8763 | goto error_return_bad_verref; |
d0fb9a8d | 8764 | |
fc0e6df6 PB |
8765 | evernaux = ((Elf_External_Vernaux *) |
8766 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
8767 | } |
8768 | ||
25ff461f AM |
8769 | iverneed->vn_nextref = NULL; |
8770 | if (iverneed->vn_next == 0) | |
8771 | break; | |
fc0e6df6 PB |
8772 | if (i + 1 < hdr->sh_info) |
8773 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 8774 | |
d0fb9a8d JJ |
8775 | if (iverneed->vn_next |
8776 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8777 | goto error_return_bad_verref; |
d0fb9a8d | 8778 | |
fc0e6df6 PB |
8779 | everneed = ((Elf_External_Verneed *) |
8780 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
8781 | } | |
25ff461f | 8782 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
8783 | |
8784 | free (contents); | |
8785 | contents = NULL; | |
8786 | } | |
252b5132 RH |
8787 | |
8788 | if (elf_dynverdef (abfd) != 0) | |
8789 | { | |
8790 | Elf_Internal_Shdr *hdr; | |
8791 | Elf_External_Verdef *everdef; | |
8792 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
8793 | Elf_Internal_Verdef *iverdefarr; |
8794 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 8795 | unsigned int i; |
062e2358 | 8796 | unsigned int maxidx; |
d0fb9a8d | 8797 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
8798 | |
8799 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
8800 | ||
601a03ba AM |
8801 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
8802 | { | |
8803 | error_return_bad_verdef: | |
4eca0228 | 8804 | _bfd_error_handler |
871b3ab2 | 8805 | (_("%pB: .gnu.version_d invalid entry"), abfd); |
601a03ba AM |
8806 | bfd_set_error (bfd_error_bad_value); |
8807 | error_return_verdef: | |
8808 | elf_tdata (abfd)->verdef = NULL; | |
8809 | elf_tdata (abfd)->cverdefs = 0; | |
8810 | goto error_return; | |
8811 | } | |
8812 | ||
2bb3687b | 8813 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) |
601a03ba | 8814 | goto error_return_verdef; |
2bb3687b AM |
8815 | contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size); |
8816 | if (contents == NULL) | |
601a03ba | 8817 | goto error_return_verdef; |
d0fb9a8d JJ |
8818 | |
8819 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
8820 | >= sizeof (Elf_External_Verdaux)); | |
8821 | contents_end_def = contents + hdr->sh_size | |
8822 | - sizeof (Elf_External_Verdef); | |
8823 | contents_end_aux = contents + hdr->sh_size | |
8824 | - sizeof (Elf_External_Verdaux); | |
8825 | ||
f631889e UD |
8826 | /* We know the number of entries in the section but not the maximum |
8827 | index. Therefore we have to run through all entries and find | |
8828 | the maximum. */ | |
252b5132 | 8829 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
8830 | maxidx = 0; |
8831 | for (i = 0; i < hdr->sh_info; ++i) | |
8832 | { | |
8833 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
8834 | ||
601a03ba AM |
8835 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
8836 | goto error_return_bad_verdef; | |
062e2358 AM |
8837 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
8838 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 8839 | |
25ff461f AM |
8840 | if (iverdefmem.vd_next == 0) |
8841 | break; | |
8842 | ||
d0fb9a8d JJ |
8843 | if (iverdefmem.vd_next |
8844 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 8845 | goto error_return_bad_verdef; |
d0fb9a8d | 8846 | |
f631889e UD |
8847 | everdef = ((Elf_External_Verdef *) |
8848 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
8849 | } | |
8850 | ||
fc0e6df6 PB |
8851 | if (default_imported_symver) |
8852 | { | |
8853 | if (freeidx > maxidx) | |
8854 | maxidx = ++freeidx; | |
8855 | else | |
8856 | freeidx = ++maxidx; | |
8857 | } | |
1f4361a7 AM |
8858 | if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt)) |
8859 | { | |
8860 | bfd_set_error (bfd_error_file_too_big); | |
8861 | goto error_return_verdef; | |
8862 | } | |
8863 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); | |
f631889e | 8864 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 8865 | goto error_return_verdef; |
f631889e UD |
8866 | |
8867 | elf_tdata (abfd)->cverdefs = maxidx; | |
8868 | ||
8869 | everdef = (Elf_External_Verdef *) contents; | |
8870 | iverdefarr = elf_tdata (abfd)->verdef; | |
8871 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
8872 | { |
8873 | Elf_External_Verdaux *everdaux; | |
8874 | Elf_Internal_Verdaux *iverdaux; | |
8875 | unsigned int j; | |
8876 | ||
f631889e UD |
8877 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
8878 | ||
d0fb9a8d | 8879 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 8880 | goto error_return_bad_verdef; |
d0fb9a8d | 8881 | |
f631889e | 8882 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
595bce75 | 8883 | memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); |
252b5132 RH |
8884 | |
8885 | iverdef->vd_bfd = abfd; | |
8886 | ||
d0fb9a8d JJ |
8887 | if (iverdef->vd_cnt == 0) |
8888 | iverdef->vd_auxptr = NULL; | |
8889 | else | |
8890 | { | |
1f4361a7 AM |
8891 | if (_bfd_mul_overflow (iverdef->vd_cnt, |
8892 | sizeof (Elf_Internal_Verdaux), &amt)) | |
8893 | { | |
8894 | bfd_set_error (bfd_error_file_too_big); | |
8895 | goto error_return_verdef; | |
8896 | } | |
a50b1753 | 8897 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
1f4361a7 | 8898 | bfd_alloc (abfd, amt); |
d0fb9a8d JJ |
8899 | if (iverdef->vd_auxptr == NULL) |
8900 | goto error_return_verdef; | |
8901 | } | |
8902 | ||
8903 | if (iverdef->vd_aux | |
8904 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 8905 | goto error_return_bad_verdef; |
252b5132 RH |
8906 | |
8907 | everdaux = ((Elf_External_Verdaux *) | |
8908 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
8909 | iverdaux = iverdef->vd_auxptr; | |
8910 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
8911 | { | |
8912 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
8913 | ||
8914 | iverdaux->vda_nodename = | |
8915 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8916 | iverdaux->vda_name); | |
8917 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 8918 | goto error_return_bad_verdef; |
252b5132 | 8919 | |
25ff461f AM |
8920 | iverdaux->vda_nextptr = NULL; |
8921 | if (iverdaux->vda_next == 0) | |
8922 | { | |
8923 | iverdef->vd_cnt = j + 1; | |
8924 | break; | |
8925 | } | |
252b5132 RH |
8926 | if (j + 1 < iverdef->vd_cnt) |
8927 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 8928 | |
d0fb9a8d JJ |
8929 | if (iverdaux->vda_next |
8930 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 8931 | goto error_return_bad_verdef; |
d0fb9a8d | 8932 | |
252b5132 RH |
8933 | everdaux = ((Elf_External_Verdaux *) |
8934 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
8935 | } | |
8936 | ||
595bce75 | 8937 | iverdef->vd_nodename = NULL; |
d0fb9a8d JJ |
8938 | if (iverdef->vd_cnt) |
8939 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 8940 | |
25ff461f AM |
8941 | iverdef->vd_nextdef = NULL; |
8942 | if (iverdef->vd_next == 0) | |
8943 | break; | |
d0fb9a8d | 8944 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 8945 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
8946 | |
8947 | everdef = ((Elf_External_Verdef *) | |
8948 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
8949 | } | |
8950 | ||
8951 | free (contents); | |
8952 | contents = NULL; | |
8953 | } | |
fc0e6df6 | 8954 | else if (default_imported_symver) |
252b5132 | 8955 | { |
fc0e6df6 PB |
8956 | if (freeidx < 3) |
8957 | freeidx = 3; | |
8958 | else | |
8959 | freeidx++; | |
252b5132 | 8960 | |
1f4361a7 AM |
8961 | if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt)) |
8962 | { | |
8963 | bfd_set_error (bfd_error_file_too_big); | |
8964 | goto error_return; | |
8965 | } | |
8966 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); | |
fc0e6df6 | 8967 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
8968 | goto error_return; |
8969 | ||
fc0e6df6 PB |
8970 | elf_tdata (abfd)->cverdefs = freeidx; |
8971 | } | |
252b5132 | 8972 | |
fc0e6df6 PB |
8973 | /* Create a default version based on the soname. */ |
8974 | if (default_imported_symver) | |
8975 | { | |
8976 | Elf_Internal_Verdef *iverdef; | |
8977 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 8978 | |
5bb3703f | 8979 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 8980 | |
fc0e6df6 PB |
8981 | iverdef->vd_version = VER_DEF_CURRENT; |
8982 | iverdef->vd_flags = 0; | |
8983 | iverdef->vd_ndx = freeidx; | |
8984 | iverdef->vd_cnt = 1; | |
252b5132 | 8985 | |
fc0e6df6 | 8986 | iverdef->vd_bfd = abfd; |
252b5132 | 8987 | |
fc0e6df6 PB |
8988 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
8989 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 8990 | goto error_return_verdef; |
fc0e6df6 | 8991 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
8992 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
8993 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
8994 | if (iverdef->vd_auxptr == NULL) |
8995 | goto error_return_verdef; | |
252b5132 | 8996 | |
fc0e6df6 PB |
8997 | iverdaux = iverdef->vd_auxptr; |
8998 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
8999 | } |
9000 | ||
b34976b6 | 9001 | return TRUE; |
252b5132 RH |
9002 | |
9003 | error_return: | |
5ed6aba4 | 9004 | if (contents != NULL) |
252b5132 | 9005 | free (contents); |
b34976b6 | 9006 | return FALSE; |
252b5132 RH |
9007 | } |
9008 | \f | |
9009 | asymbol * | |
217aa764 | 9010 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
9011 | { |
9012 | elf_symbol_type *newsym; | |
9013 | ||
7a6e0d89 | 9014 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym)); |
252b5132 RH |
9015 | if (!newsym) |
9016 | return NULL; | |
201159ec NC |
9017 | newsym->symbol.the_bfd = abfd; |
9018 | return &newsym->symbol; | |
252b5132 RH |
9019 | } |
9020 | ||
9021 | void | |
217aa764 AM |
9022 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
9023 | asymbol *symbol, | |
9024 | symbol_info *ret) | |
252b5132 RH |
9025 | { |
9026 | bfd_symbol_info (symbol, ret); | |
9027 | } | |
9028 | ||
9029 | /* Return whether a symbol name implies a local symbol. Most targets | |
9030 | use this function for the is_local_label_name entry point, but some | |
9031 | override it. */ | |
9032 | ||
b34976b6 | 9033 | bfd_boolean |
217aa764 AM |
9034 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
9035 | const char *name) | |
252b5132 RH |
9036 | { |
9037 | /* Normal local symbols start with ``.L''. */ | |
9038 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 9039 | return TRUE; |
252b5132 RH |
9040 | |
9041 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
9042 | DWARF debugging symbols starting with ``..''. */ | |
9043 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 9044 | return TRUE; |
252b5132 RH |
9045 | |
9046 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
9047 | emitting DWARF debugging output. I suspect this is actually a | |
9048 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
9049 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
9050 | underscore to be emitted on some ELF targets). For ease of use, | |
9051 | we treat such symbols as local. */ | |
9052 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 9053 | return TRUE; |
252b5132 | 9054 | |
b1fa9dd6 NC |
9055 | /* Treat assembler generated fake symbols, dollar local labels and |
9056 | forward-backward labels (aka local labels) as locals. | |
9057 | These labels have the form: | |
9058 | ||
07d6d2b8 | 9059 | L0^A.* (fake symbols) |
b1fa9dd6 NC |
9060 | |
9061 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
9062 | ||
9063 | Versions which start with .L will have already been matched above, | |
9064 | so we only need to match the rest. */ | |
9065 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
9066 | { | |
9067 | bfd_boolean ret = FALSE; | |
9068 | const char * p; | |
9069 | char c; | |
9070 | ||
9071 | for (p = name + 2; (c = *p); p++) | |
9072 | { | |
9073 | if (c == 1 || c == 2) | |
9074 | { | |
9075 | if (c == 1 && p == name + 2) | |
9076 | /* A fake symbol. */ | |
9077 | return TRUE; | |
9078 | ||
9079 | /* FIXME: We are being paranoid here and treating symbols like | |
9080 | L0^Bfoo as if there were non-local, on the grounds that the | |
9081 | assembler will never generate them. But can any symbol | |
9082 | containing an ASCII value in the range 1-31 ever be anything | |
9083 | other than some kind of local ? */ | |
9084 | ret = TRUE; | |
9085 | } | |
9086 | ||
9087 | if (! ISDIGIT (c)) | |
9088 | { | |
9089 | ret = FALSE; | |
9090 | break; | |
9091 | } | |
9092 | } | |
9093 | return ret; | |
9094 | } | |
ffa54770 | 9095 | |
b34976b6 | 9096 | return FALSE; |
252b5132 RH |
9097 | } |
9098 | ||
9099 | alent * | |
217aa764 AM |
9100 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
9101 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
9102 | { |
9103 | abort (); | |
9104 | return NULL; | |
9105 | } | |
9106 | ||
b34976b6 | 9107 | bfd_boolean |
217aa764 AM |
9108 | _bfd_elf_set_arch_mach (bfd *abfd, |
9109 | enum bfd_architecture arch, | |
9110 | unsigned long machine) | |
252b5132 RH |
9111 | { |
9112 | /* If this isn't the right architecture for this backend, and this | |
9113 | isn't the generic backend, fail. */ | |
9114 | if (arch != get_elf_backend_data (abfd)->arch | |
9115 | && arch != bfd_arch_unknown | |
9116 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 9117 | return FALSE; |
252b5132 RH |
9118 | |
9119 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
9120 | } | |
9121 | ||
d1fad7c6 NC |
9122 | /* Find the nearest line to a particular section and offset, |
9123 | for error reporting. */ | |
9124 | ||
b34976b6 | 9125 | bfd_boolean |
217aa764 | 9126 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 9127 | asymbol **symbols, |
fb167eb2 | 9128 | asection *section, |
217aa764 AM |
9129 | bfd_vma offset, |
9130 | const char **filename_ptr, | |
9131 | const char **functionname_ptr, | |
fb167eb2 AM |
9132 | unsigned int *line_ptr, |
9133 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 9134 | { |
b34976b6 | 9135 | bfd_boolean found; |
d1fad7c6 | 9136 | |
fb167eb2 | 9137 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 9138 | filename_ptr, functionname_ptr, |
fb167eb2 | 9139 | line_ptr, discriminator_ptr, |
9defd221 | 9140 | dwarf_debug_sections, |
e7679060 AM |
9141 | &elf_tdata (abfd)->dwarf2_find_line_info)) |
9142 | return TRUE; | |
9143 | ||
9144 | if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
9145 | filename_ptr, functionname_ptr, line_ptr)) | |
d1fad7c6 NC |
9146 | { |
9147 | if (!*functionname_ptr) | |
e00e8198 AM |
9148 | _bfd_elf_find_function (abfd, symbols, section, offset, |
9149 | *filename_ptr ? NULL : filename_ptr, | |
9150 | functionname_ptr); | |
b34976b6 | 9151 | return TRUE; |
d1fad7c6 NC |
9152 | } |
9153 | ||
9154 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
9155 | &found, filename_ptr, |
9156 | functionname_ptr, line_ptr, | |
9157 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 9158 | return FALSE; |
dc43ada5 | 9159 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 9160 | return TRUE; |
d1fad7c6 NC |
9161 | |
9162 | if (symbols == NULL) | |
b34976b6 | 9163 | return FALSE; |
d1fad7c6 | 9164 | |
e00e8198 AM |
9165 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
9166 | filename_ptr, functionname_ptr)) | |
b34976b6 | 9167 | return FALSE; |
d1fad7c6 | 9168 | |
252b5132 | 9169 | *line_ptr = 0; |
b34976b6 | 9170 | return TRUE; |
252b5132 RH |
9171 | } |
9172 | ||
5420f73d L |
9173 | /* Find the line for a symbol. */ |
9174 | ||
9175 | bfd_boolean | |
9176 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
9177 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 9178 | { |
fb167eb2 AM |
9179 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
9180 | filename_ptr, NULL, line_ptr, NULL, | |
9defd221 | 9181 | dwarf_debug_sections, |
fb167eb2 | 9182 | &elf_tdata (abfd)->dwarf2_find_line_info); |
5420f73d L |
9183 | } |
9184 | ||
4ab527b0 FF |
9185 | /* After a call to bfd_find_nearest_line, successive calls to |
9186 | bfd_find_inliner_info can be used to get source information about | |
9187 | each level of function inlining that terminated at the address | |
9188 | passed to bfd_find_nearest_line. Currently this is only supported | |
9189 | for DWARF2 with appropriate DWARF3 extensions. */ | |
9190 | ||
9191 | bfd_boolean | |
9192 | _bfd_elf_find_inliner_info (bfd *abfd, | |
9193 | const char **filename_ptr, | |
9194 | const char **functionname_ptr, | |
9195 | unsigned int *line_ptr) | |
9196 | { | |
9197 | bfd_boolean found; | |
9198 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
9199 | functionname_ptr, line_ptr, | |
9200 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
9201 | return found; | |
9202 | } | |
9203 | ||
252b5132 | 9204 | int |
a6b96beb | 9205 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 9206 | { |
8ded5a0f AM |
9207 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
9208 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 9209 | |
0e1862bb | 9210 | if (!bfd_link_relocatable (info)) |
8ded5a0f | 9211 | { |
12bd6957 | 9212 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 9213 | |
62d7a5f6 AM |
9214 | if (phdr_size == (bfd_size_type) -1) |
9215 | { | |
9216 | struct elf_segment_map *m; | |
9217 | ||
9218 | phdr_size = 0; | |
12bd6957 | 9219 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 9220 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 9221 | |
62d7a5f6 AM |
9222 | if (phdr_size == 0) |
9223 | phdr_size = get_program_header_size (abfd, info); | |
9224 | } | |
8ded5a0f | 9225 | |
12bd6957 | 9226 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
9227 | ret += phdr_size; |
9228 | } | |
9229 | ||
252b5132 RH |
9230 | return ret; |
9231 | } | |
9232 | ||
b34976b6 | 9233 | bfd_boolean |
217aa764 AM |
9234 | _bfd_elf_set_section_contents (bfd *abfd, |
9235 | sec_ptr section, | |
0f867abe | 9236 | const void *location, |
217aa764 AM |
9237 | file_ptr offset, |
9238 | bfd_size_type count) | |
252b5132 RH |
9239 | { |
9240 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 9241 | file_ptr pos; |
252b5132 RH |
9242 | |
9243 | if (! abfd->output_has_begun | |
217aa764 | 9244 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 9245 | return FALSE; |
252b5132 | 9246 | |
0ce398f1 L |
9247 | if (!count) |
9248 | return TRUE; | |
9249 | ||
252b5132 | 9250 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
9251 | if (hdr->sh_offset == (file_ptr) -1) |
9252 | { | |
a0dcf297 NC |
9253 | unsigned char *contents; |
9254 | ||
1ff6de03 NA |
9255 | if (bfd_section_is_ctf (section)) |
9256 | /* Nothing to do with this section: the contents are generated | |
9257 | later. */ | |
9258 | return TRUE; | |
9259 | ||
a0dcf297 NC |
9260 | if ((section->flags & SEC_ELF_COMPRESS) == 0) |
9261 | { | |
9262 | _bfd_error_handler | |
9263 | (_("%pB:%pA: error: attempting to write into an unallocated compressed section"), | |
9264 | abfd, section); | |
9265 | bfd_set_error (bfd_error_invalid_operation); | |
9266 | return FALSE; | |
9267 | } | |
9268 | ||
9269 | if ((offset + count) > hdr->sh_size) | |
9270 | { | |
9271 | _bfd_error_handler | |
9272 | (_("%pB:%pA: error: attempting to write over the end of the section"), | |
9273 | abfd, section); | |
9274 | ||
9275 | bfd_set_error (bfd_error_invalid_operation); | |
9276 | return FALSE; | |
9277 | } | |
9278 | ||
9279 | contents = hdr->contents; | |
9280 | if (contents == NULL) | |
9281 | { | |
9282 | _bfd_error_handler | |
9283 | (_("%pB:%pA: error: attempting to write section into an empty buffer"), | |
9284 | abfd, section); | |
9285 | ||
9286 | bfd_set_error (bfd_error_invalid_operation); | |
9287 | return FALSE; | |
9288 | } | |
9289 | ||
0ce398f1 L |
9290 | memcpy (contents + offset, location, count); |
9291 | return TRUE; | |
9292 | } | |
a0dcf297 | 9293 | |
dc810e39 AM |
9294 | pos = hdr->sh_offset + offset; |
9295 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
9296 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 9297 | return FALSE; |
252b5132 | 9298 | |
b34976b6 | 9299 | return TRUE; |
252b5132 RH |
9300 | } |
9301 | ||
f3185997 | 9302 | bfd_boolean |
217aa764 AM |
9303 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
9304 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
9305 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
9306 | { |
9307 | abort (); | |
f3185997 | 9308 | return FALSE; |
252b5132 RH |
9309 | } |
9310 | ||
252b5132 RH |
9311 | /* Try to convert a non-ELF reloc into an ELF one. */ |
9312 | ||
b34976b6 | 9313 | bfd_boolean |
217aa764 | 9314 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 9315 | { |
c044fabd | 9316 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
9317 | |
9318 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
9319 | { | |
9320 | bfd_reloc_code_real_type code; | |
9321 | reloc_howto_type *howto; | |
9322 | ||
9323 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 9324 | equivalent ELF reloc. */ |
252b5132 RH |
9325 | |
9326 | if (areloc->howto->pc_relative) | |
9327 | { | |
9328 | switch (areloc->howto->bitsize) | |
9329 | { | |
9330 | case 8: | |
9331 | code = BFD_RELOC_8_PCREL; | |
9332 | break; | |
9333 | case 12: | |
9334 | code = BFD_RELOC_12_PCREL; | |
9335 | break; | |
9336 | case 16: | |
9337 | code = BFD_RELOC_16_PCREL; | |
9338 | break; | |
9339 | case 24: | |
9340 | code = BFD_RELOC_24_PCREL; | |
9341 | break; | |
9342 | case 32: | |
9343 | code = BFD_RELOC_32_PCREL; | |
9344 | break; | |
9345 | case 64: | |
9346 | code = BFD_RELOC_64_PCREL; | |
9347 | break; | |
9348 | default: | |
9349 | goto fail; | |
9350 | } | |
9351 | ||
9352 | howto = bfd_reloc_type_lookup (abfd, code); | |
9353 | ||
94698d01 | 9354 | if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset) |
252b5132 RH |
9355 | { |
9356 | if (howto->pcrel_offset) | |
9357 | areloc->addend += areloc->address; | |
9358 | else | |
9359 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
9360 | } | |
9361 | } | |
9362 | else | |
9363 | { | |
9364 | switch (areloc->howto->bitsize) | |
9365 | { | |
9366 | case 8: | |
9367 | code = BFD_RELOC_8; | |
9368 | break; | |
9369 | case 14: | |
9370 | code = BFD_RELOC_14; | |
9371 | break; | |
9372 | case 16: | |
9373 | code = BFD_RELOC_16; | |
9374 | break; | |
9375 | case 26: | |
9376 | code = BFD_RELOC_26; | |
9377 | break; | |
9378 | case 32: | |
9379 | code = BFD_RELOC_32; | |
9380 | break; | |
9381 | case 64: | |
9382 | code = BFD_RELOC_64; | |
9383 | break; | |
9384 | default: | |
9385 | goto fail; | |
9386 | } | |
9387 | ||
9388 | howto = bfd_reloc_type_lookup (abfd, code); | |
9389 | } | |
9390 | ||
9391 | if (howto) | |
9392 | areloc->howto = howto; | |
9393 | else | |
9394 | goto fail; | |
9395 | } | |
9396 | ||
b34976b6 | 9397 | return TRUE; |
252b5132 RH |
9398 | |
9399 | fail: | |
0aa13fee AM |
9400 | /* xgettext:c-format */ |
9401 | _bfd_error_handler (_("%pB: %s unsupported"), | |
9402 | abfd, areloc->howto->name); | |
9aea1e31 | 9403 | bfd_set_error (bfd_error_sorry); |
b34976b6 | 9404 | return FALSE; |
252b5132 RH |
9405 | } |
9406 | ||
b34976b6 | 9407 | bfd_boolean |
217aa764 | 9408 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 9409 | { |
d9071b0c TG |
9410 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
9411 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 9412 | { |
c0355132 | 9413 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 9414 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 9415 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
9416 | } |
9417 | ||
9418 | return _bfd_generic_close_and_cleanup (abfd); | |
9419 | } | |
9420 | ||
9421 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
9422 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
9423 | range-checking to interfere. There is nothing else to do in processing | |
9424 | this reloc. */ | |
9425 | ||
9426 | bfd_reloc_status_type | |
217aa764 AM |
9427 | _bfd_elf_rel_vtable_reloc_fn |
9428 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 9429 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
9430 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
9431 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
9432 | { |
9433 | return bfd_reloc_ok; | |
9434 | } | |
252b5132 RH |
9435 | \f |
9436 | /* Elf core file support. Much of this only works on native | |
9437 | toolchains, since we rely on knowing the | |
9438 | machine-dependent procfs structure in order to pick | |
c044fabd | 9439 | out details about the corefile. */ |
252b5132 RH |
9440 | |
9441 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
9442 | /* Needed for new procfs interface on sparc-solaris. */ |
9443 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
9444 | # include <sys/procfs.h> |
9445 | #endif | |
9446 | ||
261b8d08 PA |
9447 | /* Return a PID that identifies a "thread" for threaded cores, or the |
9448 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
9449 | |
9450 | static int | |
217aa764 | 9451 | elfcore_make_pid (bfd *abfd) |
252b5132 | 9452 | { |
261b8d08 PA |
9453 | int pid; |
9454 | ||
228e534f | 9455 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 9456 | if (pid == 0) |
228e534f | 9457 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
9458 | |
9459 | return pid; | |
252b5132 RH |
9460 | } |
9461 | ||
252b5132 RH |
9462 | /* If there isn't a section called NAME, make one, using |
9463 | data from SECT. Note, this function will generate a | |
9464 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 9465 | overwrite it. */ |
252b5132 | 9466 | |
b34976b6 | 9467 | static bfd_boolean |
217aa764 | 9468 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 9469 | { |
c044fabd | 9470 | asection *sect2; |
252b5132 RH |
9471 | |
9472 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 9473 | return TRUE; |
252b5132 | 9474 | |
117ed4f8 | 9475 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 9476 | if (sect2 == NULL) |
b34976b6 | 9477 | return FALSE; |
252b5132 | 9478 | |
eea6121a | 9479 | sect2->size = sect->size; |
252b5132 | 9480 | sect2->filepos = sect->filepos; |
252b5132 | 9481 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 9482 | return TRUE; |
252b5132 RH |
9483 | } |
9484 | ||
bb0082d6 AM |
9485 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
9486 | actually creates up to two pseudosections: | |
9487 | - For the single-threaded case, a section named NAME, unless | |
9488 | such a section already exists. | |
9489 | - For the multi-threaded case, a section named "NAME/PID", where | |
9490 | PID is elfcore_make_pid (abfd). | |
24d3e51b | 9491 | Both pseudosections have identical contents. */ |
b34976b6 | 9492 | bfd_boolean |
217aa764 AM |
9493 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
9494 | char *name, | |
9495 | size_t size, | |
9496 | ufile_ptr filepos) | |
bb0082d6 AM |
9497 | { |
9498 | char buf[100]; | |
9499 | char *threaded_name; | |
d4c88bbb | 9500 | size_t len; |
bb0082d6 AM |
9501 | asection *sect; |
9502 | ||
9503 | /* Build the section name. */ | |
9504 | ||
9505 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 9506 | len = strlen (buf) + 1; |
a50b1753 | 9507 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 9508 | if (threaded_name == NULL) |
b34976b6 | 9509 | return FALSE; |
d4c88bbb | 9510 | memcpy (threaded_name, buf, len); |
bb0082d6 | 9511 | |
117ed4f8 AM |
9512 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
9513 | SEC_HAS_CONTENTS); | |
bb0082d6 | 9514 | if (sect == NULL) |
b34976b6 | 9515 | return FALSE; |
eea6121a | 9516 | sect->size = size; |
bb0082d6 | 9517 | sect->filepos = filepos; |
bb0082d6 AM |
9518 | sect->alignment_power = 2; |
9519 | ||
936e320b | 9520 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
9521 | } |
9522 | ||
58e07198 CZ |
9523 | static bfd_boolean |
9524 | elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note, | |
9525 | size_t offs) | |
9526 | { | |
9527 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9528 | SEC_HAS_CONTENTS); | |
9529 | ||
9530 | if (sect == NULL) | |
9531 | return FALSE; | |
9532 | ||
9533 | sect->size = note->descsz - offs; | |
9534 | sect->filepos = note->descpos + offs; | |
9535 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9536 | ||
9537 | return TRUE; | |
9538 | } | |
9539 | ||
252b5132 | 9540 | /* prstatus_t exists on: |
4a938328 | 9541 | solaris 2.5+ |
252b5132 RH |
9542 | linux 2.[01] + glibc |
9543 | unixware 4.2 | |
9544 | */ | |
9545 | ||
9546 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 9547 | |
b34976b6 | 9548 | static bfd_boolean |
217aa764 | 9549 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9550 | { |
eea6121a | 9551 | size_t size; |
7ee38065 | 9552 | int offset; |
252b5132 | 9553 | |
4a938328 MS |
9554 | if (note->descsz == sizeof (prstatus_t)) |
9555 | { | |
9556 | prstatus_t prstat; | |
252b5132 | 9557 | |
eea6121a | 9558 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9559 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 9560 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 9561 | |
fa49d224 NC |
9562 | /* Do not overwrite the core signal if it |
9563 | has already been set by another thread. */ | |
228e534f AM |
9564 | if (elf_tdata (abfd)->core->signal == 0) |
9565 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9566 | if (elf_tdata (abfd)->core->pid == 0) | |
9567 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 9568 | |
4a938328 MS |
9569 | /* pr_who exists on: |
9570 | solaris 2.5+ | |
9571 | unixware 4.2 | |
9572 | pr_who doesn't exist on: | |
9573 | linux 2.[01] | |
9574 | */ | |
252b5132 | 9575 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 9576 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9577 | #else |
228e534f | 9578 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 9579 | #endif |
4a938328 | 9580 | } |
7ee38065 | 9581 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
9582 | else if (note->descsz == sizeof (prstatus32_t)) |
9583 | { | |
9584 | /* 64-bit host, 32-bit corefile */ | |
9585 | prstatus32_t prstat; | |
9586 | ||
eea6121a | 9587 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9588 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
9589 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
9590 | ||
fa49d224 NC |
9591 | /* Do not overwrite the core signal if it |
9592 | has already been set by another thread. */ | |
228e534f AM |
9593 | if (elf_tdata (abfd)->core->signal == 0) |
9594 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9595 | if (elf_tdata (abfd)->core->pid == 0) | |
9596 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
9597 | |
9598 | /* pr_who exists on: | |
9599 | solaris 2.5+ | |
9600 | unixware 4.2 | |
9601 | pr_who doesn't exist on: | |
9602 | linux 2.[01] | |
9603 | */ | |
7ee38065 | 9604 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 9605 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9606 | #else |
228e534f | 9607 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
9608 | #endif |
9609 | } | |
7ee38065 | 9610 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
9611 | else |
9612 | { | |
9613 | /* Fail - we don't know how to handle any other | |
9614 | note size (ie. data object type). */ | |
b34976b6 | 9615 | return TRUE; |
4a938328 | 9616 | } |
252b5132 | 9617 | |
bb0082d6 | 9618 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 9619 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 9620 | size, note->descpos + offset); |
252b5132 RH |
9621 | } |
9622 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
9623 | ||
bb0082d6 | 9624 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 9625 | static bfd_boolean |
217aa764 AM |
9626 | elfcore_make_note_pseudosection (bfd *abfd, |
9627 | char *name, | |
9628 | Elf_Internal_Note *note) | |
252b5132 | 9629 | { |
936e320b AM |
9630 | return _bfd_elfcore_make_pseudosection (abfd, name, |
9631 | note->descsz, note->descpos); | |
252b5132 RH |
9632 | } |
9633 | ||
ff08c6bb JB |
9634 | /* There isn't a consistent prfpregset_t across platforms, |
9635 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
9636 | data structure apart. */ |
9637 | ||
b34976b6 | 9638 | static bfd_boolean |
217aa764 | 9639 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9640 | { |
9641 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9642 | } | |
9643 | ||
ff08c6bb | 9644 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 9645 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 9646 | literally. */ |
c044fabd | 9647 | |
b34976b6 | 9648 | static bfd_boolean |
217aa764 | 9649 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9650 | { |
9651 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9652 | } | |
9653 | ||
4339cae0 L |
9654 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
9655 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
9656 | contents literally. */ | |
9657 | ||
9658 | static bfd_boolean | |
9659 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
9660 | { | |
9661 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
9662 | } | |
9663 | ||
97753bd5 AM |
9664 | static bfd_boolean |
9665 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
9666 | { | |
9667 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
9668 | } | |
9669 | ||
89eeb0bc LM |
9670 | static bfd_boolean |
9671 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
9672 | { | |
9673 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
9674 | } | |
97753bd5 | 9675 | |
cb2366c1 EBM |
9676 | static bfd_boolean |
9677 | elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note) | |
9678 | { | |
9679 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note); | |
9680 | } | |
9681 | ||
9682 | static bfd_boolean | |
9683 | elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note) | |
9684 | { | |
9685 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note); | |
9686 | } | |
9687 | ||
9688 | static bfd_boolean | |
9689 | elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note) | |
9690 | { | |
9691 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note); | |
9692 | } | |
9693 | ||
9694 | static bfd_boolean | |
9695 | elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note) | |
9696 | { | |
9697 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note); | |
9698 | } | |
9699 | ||
9700 | static bfd_boolean | |
9701 | elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note) | |
9702 | { | |
9703 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note); | |
9704 | } | |
9705 | ||
9706 | static bfd_boolean | |
9707 | elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note) | |
9708 | { | |
9709 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note); | |
9710 | } | |
9711 | ||
9712 | static bfd_boolean | |
9713 | elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note) | |
9714 | { | |
9715 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note); | |
9716 | } | |
9717 | ||
9718 | static bfd_boolean | |
9719 | elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note) | |
9720 | { | |
9721 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note); | |
9722 | } | |
9723 | ||
9724 | static bfd_boolean | |
9725 | elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note) | |
9726 | { | |
9727 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note); | |
9728 | } | |
9729 | ||
9730 | static bfd_boolean | |
9731 | elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note) | |
9732 | { | |
9733 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note); | |
9734 | } | |
9735 | ||
9736 | static bfd_boolean | |
9737 | elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note) | |
9738 | { | |
9739 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note); | |
9740 | } | |
9741 | ||
9742 | static bfd_boolean | |
9743 | elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note) | |
9744 | { | |
9745 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note); | |
9746 | } | |
9747 | ||
9748 | static bfd_boolean | |
9749 | elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note) | |
9750 | { | |
9751 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note); | |
9752 | } | |
9753 | ||
0675e188 UW |
9754 | static bfd_boolean |
9755 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
9756 | { | |
9757 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
9758 | } | |
9759 | ||
d7eeb400 MS |
9760 | static bfd_boolean |
9761 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
9762 | { | |
9763 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
9764 | } | |
9765 | ||
9766 | static bfd_boolean | |
9767 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
9768 | { | |
9769 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
9770 | } | |
9771 | ||
9772 | static bfd_boolean | |
9773 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
9774 | { | |
9775 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
9776 | } | |
9777 | ||
9778 | static bfd_boolean | |
9779 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
9780 | { | |
9781 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
9782 | } | |
9783 | ||
9784 | static bfd_boolean | |
9785 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
9786 | { | |
9787 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
9788 | } | |
9789 | ||
355b81d9 UW |
9790 | static bfd_boolean |
9791 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
9792 | { | |
9793 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
9794 | } | |
9795 | ||
9796 | static bfd_boolean | |
9797 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
9798 | { | |
9799 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
9800 | } | |
9801 | ||
abb3f6cc NC |
9802 | static bfd_boolean |
9803 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
9804 | { | |
9805 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
9806 | } | |
9807 | ||
4ef9f41a AA |
9808 | static bfd_boolean |
9809 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
9810 | { | |
9811 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
9812 | } | |
9813 | ||
9814 | static bfd_boolean | |
9815 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
9816 | { | |
9817 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
9818 | } | |
9819 | ||
88ab90e8 AA |
9820 | static bfd_boolean |
9821 | elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note) | |
9822 | { | |
9823 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note); | |
9824 | } | |
9825 | ||
9826 | static bfd_boolean | |
9827 | elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note) | |
9828 | { | |
9829 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note); | |
9830 | } | |
9831 | ||
faa9a424 UW |
9832 | static bfd_boolean |
9833 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
9834 | { | |
9835 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
9836 | } | |
9837 | ||
652451f8 YZ |
9838 | static bfd_boolean |
9839 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
9840 | { | |
9841 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
9842 | } | |
9843 | ||
9844 | static bfd_boolean | |
9845 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
9846 | { | |
9847 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
9848 | } | |
9849 | ||
9850 | static bfd_boolean | |
9851 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
9852 | { | |
9853 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
9854 | } | |
9855 | ||
ad1cc4e4 AH |
9856 | static bfd_boolean |
9857 | elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note) | |
9858 | { | |
9859 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note); | |
9860 | } | |
9861 | ||
e6c3b5bf AH |
9862 | static bfd_boolean |
9863 | elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note) | |
9864 | { | |
9865 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note); | |
9866 | } | |
9867 | ||
27456742 AK |
9868 | static bfd_boolean |
9869 | elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note) | |
9870 | { | |
9871 | return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note); | |
9872 | } | |
9873 | ||
252b5132 | 9874 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 9875 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 9876 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9877 | typedef prpsinfo32_t elfcore_psinfo32_t; |
9878 | #endif | |
252b5132 RH |
9879 | #endif |
9880 | ||
9881 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 9882 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 9883 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9884 | typedef psinfo32_t elfcore_psinfo32_t; |
9885 | #endif | |
252b5132 RH |
9886 | #endif |
9887 | ||
252b5132 RH |
9888 | /* return a malloc'ed copy of a string at START which is at |
9889 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 9890 | the copy will always have a terminating '\0'. */ |
252b5132 | 9891 | |
936e320b | 9892 | char * |
217aa764 | 9893 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 9894 | { |
dc810e39 | 9895 | char *dups; |
a50b1753 | 9896 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 9897 | size_t len; |
252b5132 RH |
9898 | |
9899 | if (end == NULL) | |
9900 | len = max; | |
9901 | else | |
9902 | len = end - start; | |
9903 | ||
a50b1753 | 9904 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 9905 | if (dups == NULL) |
252b5132 RH |
9906 | return NULL; |
9907 | ||
dc810e39 AM |
9908 | memcpy (dups, start, len); |
9909 | dups[len] = '\0'; | |
252b5132 | 9910 | |
dc810e39 | 9911 | return dups; |
252b5132 RH |
9912 | } |
9913 | ||
bb0082d6 | 9914 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 9915 | static bfd_boolean |
217aa764 | 9916 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9917 | { |
4a938328 MS |
9918 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
9919 | { | |
9920 | elfcore_psinfo_t psinfo; | |
252b5132 | 9921 | |
7ee38065 | 9922 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9923 | |
335e41d4 | 9924 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 9925 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9926 | #endif |
228e534f | 9927 | elf_tdata (abfd)->core->program |
936e320b AM |
9928 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9929 | sizeof (psinfo.pr_fname)); | |
252b5132 | 9930 | |
228e534f | 9931 | elf_tdata (abfd)->core->command |
936e320b AM |
9932 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9933 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 9934 | } |
7ee38065 | 9935 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
9936 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
9937 | { | |
9938 | /* 64-bit host, 32-bit corefile */ | |
9939 | elfcore_psinfo32_t psinfo; | |
9940 | ||
7ee38065 | 9941 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9942 | |
335e41d4 | 9943 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 9944 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9945 | #endif |
228e534f | 9946 | elf_tdata (abfd)->core->program |
936e320b AM |
9947 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9948 | sizeof (psinfo.pr_fname)); | |
4a938328 | 9949 | |
228e534f | 9950 | elf_tdata (abfd)->core->command |
936e320b AM |
9951 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9952 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
9953 | } |
9954 | #endif | |
9955 | ||
9956 | else | |
9957 | { | |
9958 | /* Fail - we don't know how to handle any other | |
9959 | note size (ie. data object type). */ | |
b34976b6 | 9960 | return TRUE; |
4a938328 | 9961 | } |
252b5132 RH |
9962 | |
9963 | /* Note that for some reason, a spurious space is tacked | |
9964 | onto the end of the args in some (at least one anyway) | |
c044fabd | 9965 | implementations, so strip it off if it exists. */ |
252b5132 RH |
9966 | |
9967 | { | |
228e534f | 9968 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
9969 | int n = strlen (command); |
9970 | ||
9971 | if (0 < n && command[n - 1] == ' ') | |
9972 | command[n - 1] = '\0'; | |
9973 | } | |
9974 | ||
b34976b6 | 9975 | return TRUE; |
252b5132 RH |
9976 | } |
9977 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
9978 | ||
252b5132 | 9979 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 9980 | static bfd_boolean |
217aa764 | 9981 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9982 | { |
f572a39d AM |
9983 | if (note->descsz == sizeof (pstatus_t) |
9984 | #if defined (HAVE_PXSTATUS_T) | |
9985 | || note->descsz == sizeof (pxstatus_t) | |
9986 | #endif | |
9987 | ) | |
4a938328 MS |
9988 | { |
9989 | pstatus_t pstat; | |
252b5132 | 9990 | |
4a938328 | 9991 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9992 | |
228e534f | 9993 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 9994 | } |
7ee38065 | 9995 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
9996 | else if (note->descsz == sizeof (pstatus32_t)) |
9997 | { | |
9998 | /* 64-bit host, 32-bit corefile */ | |
9999 | pstatus32_t pstat; | |
252b5132 | 10000 | |
4a938328 | 10001 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 10002 | |
228e534f | 10003 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
10004 | } |
10005 | #endif | |
252b5132 RH |
10006 | /* Could grab some more details from the "representative" |
10007 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 10008 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 10009 | |
b34976b6 | 10010 | return TRUE; |
252b5132 RH |
10011 | } |
10012 | #endif /* defined (HAVE_PSTATUS_T) */ | |
10013 | ||
252b5132 | 10014 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 10015 | static bfd_boolean |
217aa764 | 10016 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
10017 | { |
10018 | lwpstatus_t lwpstat; | |
10019 | char buf[100]; | |
c044fabd | 10020 | char *name; |
d4c88bbb | 10021 | size_t len; |
c044fabd | 10022 | asection *sect; |
252b5132 | 10023 | |
f572a39d AM |
10024 | if (note->descsz != sizeof (lwpstat) |
10025 | #if defined (HAVE_LWPXSTATUS_T) | |
10026 | && note->descsz != sizeof (lwpxstatus_t) | |
10027 | #endif | |
10028 | ) | |
b34976b6 | 10029 | return TRUE; |
252b5132 RH |
10030 | |
10031 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
10032 | ||
228e534f | 10033 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
10034 | /* Do not overwrite the core signal if it has already been set by |
10035 | another thread. */ | |
228e534f AM |
10036 | if (elf_tdata (abfd)->core->signal == 0) |
10037 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 10038 | |
c044fabd | 10039 | /* Make a ".reg/999" section. */ |
252b5132 RH |
10040 | |
10041 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 10042 | len = strlen (buf) + 1; |
217aa764 | 10043 | name = bfd_alloc (abfd, len); |
252b5132 | 10044 | if (name == NULL) |
b34976b6 | 10045 | return FALSE; |
d4c88bbb | 10046 | memcpy (name, buf, len); |
252b5132 | 10047 | |
117ed4f8 | 10048 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 10049 | if (sect == NULL) |
b34976b6 | 10050 | return FALSE; |
252b5132 RH |
10051 | |
10052 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 10053 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
10054 | sect->filepos = note->descpos |
10055 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
10056 | #endif | |
10057 | ||
10058 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 10059 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
10060 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
10061 | #endif | |
10062 | ||
252b5132 RH |
10063 | sect->alignment_power = 2; |
10064 | ||
10065 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 10066 | return FALSE; |
252b5132 RH |
10067 | |
10068 | /* Make a ".reg2/999" section */ | |
10069 | ||
10070 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 10071 | len = strlen (buf) + 1; |
217aa764 | 10072 | name = bfd_alloc (abfd, len); |
252b5132 | 10073 | if (name == NULL) |
b34976b6 | 10074 | return FALSE; |
d4c88bbb | 10075 | memcpy (name, buf, len); |
252b5132 | 10076 | |
117ed4f8 | 10077 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 10078 | if (sect == NULL) |
b34976b6 | 10079 | return FALSE; |
252b5132 RH |
10080 | |
10081 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 10082 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
10083 | sect->filepos = note->descpos |
10084 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
10085 | #endif | |
10086 | ||
10087 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 10088 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
10089 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
10090 | #endif | |
10091 | ||
252b5132 RH |
10092 | sect->alignment_power = 2; |
10093 | ||
936e320b | 10094 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
10095 | } |
10096 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
10097 | ||
b34976b6 | 10098 | static bfd_boolean |
217aa764 | 10099 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
10100 | { |
10101 | char buf[30]; | |
c044fabd | 10102 | char *name; |
d4c88bbb | 10103 | size_t len; |
c044fabd | 10104 | asection *sect; |
4a6636fb PA |
10105 | int type; |
10106 | int is_active_thread; | |
10107 | bfd_vma base_addr; | |
16e9c715 | 10108 | |
4a6636fb | 10109 | if (note->descsz < 728) |
b34976b6 | 10110 | return TRUE; |
16e9c715 | 10111 | |
4a6636fb PA |
10112 | if (! CONST_STRNEQ (note->namedata, "win32")) |
10113 | return TRUE; | |
10114 | ||
10115 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 10116 | |
4a6636fb | 10117 | switch (type) |
16e9c715 | 10118 | { |
4a6636fb | 10119 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 10120 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 10121 | /* process_info.pid */ |
228e534f | 10122 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 10123 | /* process_info.signal */ |
228e534f | 10124 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 10125 | break; |
16e9c715 | 10126 | |
4a6636fb | 10127 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 10128 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
10129 | /* thread_info.tid */ |
10130 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 10131 | |
d4c88bbb | 10132 | len = strlen (buf) + 1; |
a50b1753 | 10133 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 10134 | if (name == NULL) |
b34976b6 | 10135 | return FALSE; |
c044fabd | 10136 | |
d4c88bbb | 10137 | memcpy (name, buf, len); |
16e9c715 | 10138 | |
117ed4f8 | 10139 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 10140 | if (sect == NULL) |
b34976b6 | 10141 | return FALSE; |
c044fabd | 10142 | |
4a6636fb PA |
10143 | /* sizeof (thread_info.thread_context) */ |
10144 | sect->size = 716; | |
10145 | /* offsetof (thread_info.thread_context) */ | |
10146 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
10147 | sect->alignment_power = 2; |
10148 | ||
4a6636fb PA |
10149 | /* thread_info.is_active_thread */ |
10150 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
10151 | ||
10152 | if (is_active_thread) | |
16e9c715 | 10153 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 10154 | return FALSE; |
16e9c715 NC |
10155 | break; |
10156 | ||
4a6636fb | 10157 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 10158 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
10159 | /* module_info.base_address */ |
10160 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 10161 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 10162 | |
d4c88bbb | 10163 | len = strlen (buf) + 1; |
a50b1753 | 10164 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 10165 | if (name == NULL) |
b34976b6 | 10166 | return FALSE; |
c044fabd | 10167 | |
d4c88bbb | 10168 | memcpy (name, buf, len); |
252b5132 | 10169 | |
117ed4f8 | 10170 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 10171 | |
16e9c715 | 10172 | if (sect == NULL) |
b34976b6 | 10173 | return FALSE; |
c044fabd | 10174 | |
eea6121a | 10175 | sect->size = note->descsz; |
16e9c715 | 10176 | sect->filepos = note->descpos; |
16e9c715 NC |
10177 | sect->alignment_power = 2; |
10178 | break; | |
10179 | ||
10180 | default: | |
b34976b6 | 10181 | return TRUE; |
16e9c715 NC |
10182 | } |
10183 | ||
b34976b6 | 10184 | return TRUE; |
16e9c715 | 10185 | } |
252b5132 | 10186 | |
b34976b6 | 10187 | static bfd_boolean |
217aa764 | 10188 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 10189 | { |
9c5bfbb7 | 10190 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 10191 | |
252b5132 RH |
10192 | switch (note->type) |
10193 | { | |
10194 | default: | |
b34976b6 | 10195 | return TRUE; |
252b5132 | 10196 | |
252b5132 | 10197 | case NT_PRSTATUS: |
bb0082d6 AM |
10198 | if (bed->elf_backend_grok_prstatus) |
10199 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 10200 | return TRUE; |
bb0082d6 | 10201 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 10202 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 10203 | #else |
b34976b6 | 10204 | return TRUE; |
252b5132 RH |
10205 | #endif |
10206 | ||
10207 | #if defined (HAVE_PSTATUS_T) | |
10208 | case NT_PSTATUS: | |
10209 | return elfcore_grok_pstatus (abfd, note); | |
10210 | #endif | |
10211 | ||
10212 | #if defined (HAVE_LWPSTATUS_T) | |
10213 | case NT_LWPSTATUS: | |
10214 | return elfcore_grok_lwpstatus (abfd, note); | |
10215 | #endif | |
10216 | ||
10217 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
10218 | return elfcore_grok_prfpreg (abfd, note); | |
10219 | ||
c044fabd | 10220 | case NT_WIN32PSTATUS: |
16e9c715 | 10221 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 10222 | |
c044fabd | 10223 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
10224 | if (note->namesz == 6 |
10225 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
10226 | return elfcore_grok_prxfpreg (abfd, note); |
10227 | else | |
b34976b6 | 10228 | return TRUE; |
ff08c6bb | 10229 | |
4339cae0 L |
10230 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
10231 | if (note->namesz == 6 | |
10232 | && strcmp (note->namedata, "LINUX") == 0) | |
10233 | return elfcore_grok_xstatereg (abfd, note); | |
10234 | else | |
10235 | return TRUE; | |
10236 | ||
97753bd5 AM |
10237 | case NT_PPC_VMX: |
10238 | if (note->namesz == 6 | |
10239 | && strcmp (note->namedata, "LINUX") == 0) | |
10240 | return elfcore_grok_ppc_vmx (abfd, note); | |
10241 | else | |
10242 | return TRUE; | |
10243 | ||
89eeb0bc LM |
10244 | case NT_PPC_VSX: |
10245 | if (note->namesz == 6 | |
07d6d2b8 AM |
10246 | && strcmp (note->namedata, "LINUX") == 0) |
10247 | return elfcore_grok_ppc_vsx (abfd, note); | |
89eeb0bc | 10248 | else |
07d6d2b8 | 10249 | return TRUE; |
89eeb0bc | 10250 | |
cb2366c1 EBM |
10251 | case NT_PPC_TAR: |
10252 | if (note->namesz == 6 | |
4b24dd1a AM |
10253 | && strcmp (note->namedata, "LINUX") == 0) |
10254 | return elfcore_grok_ppc_tar (abfd, note); | |
cb2366c1 | 10255 | else |
4b24dd1a | 10256 | return TRUE; |
cb2366c1 EBM |
10257 | |
10258 | case NT_PPC_PPR: | |
10259 | if (note->namesz == 6 | |
4b24dd1a AM |
10260 | && strcmp (note->namedata, "LINUX") == 0) |
10261 | return elfcore_grok_ppc_ppr (abfd, note); | |
cb2366c1 | 10262 | else |
4b24dd1a | 10263 | return TRUE; |
cb2366c1 EBM |
10264 | |
10265 | case NT_PPC_DSCR: | |
10266 | if (note->namesz == 6 | |
4b24dd1a AM |
10267 | && strcmp (note->namedata, "LINUX") == 0) |
10268 | return elfcore_grok_ppc_dscr (abfd, note); | |
cb2366c1 | 10269 | else |
4b24dd1a | 10270 | return TRUE; |
cb2366c1 EBM |
10271 | |
10272 | case NT_PPC_EBB: | |
10273 | if (note->namesz == 6 | |
4b24dd1a AM |
10274 | && strcmp (note->namedata, "LINUX") == 0) |
10275 | return elfcore_grok_ppc_ebb (abfd, note); | |
cb2366c1 | 10276 | else |
4b24dd1a | 10277 | return TRUE; |
cb2366c1 EBM |
10278 | |
10279 | case NT_PPC_PMU: | |
10280 | if (note->namesz == 6 | |
4b24dd1a AM |
10281 | && strcmp (note->namedata, "LINUX") == 0) |
10282 | return elfcore_grok_ppc_pmu (abfd, note); | |
cb2366c1 | 10283 | else |
4b24dd1a | 10284 | return TRUE; |
cb2366c1 EBM |
10285 | |
10286 | case NT_PPC_TM_CGPR: | |
10287 | if (note->namesz == 6 | |
4b24dd1a AM |
10288 | && strcmp (note->namedata, "LINUX") == 0) |
10289 | return elfcore_grok_ppc_tm_cgpr (abfd, note); | |
cb2366c1 | 10290 | else |
4b24dd1a | 10291 | return TRUE; |
cb2366c1 EBM |
10292 | |
10293 | case NT_PPC_TM_CFPR: | |
10294 | if (note->namesz == 6 | |
4b24dd1a AM |
10295 | && strcmp (note->namedata, "LINUX") == 0) |
10296 | return elfcore_grok_ppc_tm_cfpr (abfd, note); | |
cb2366c1 | 10297 | else |
4b24dd1a | 10298 | return TRUE; |
cb2366c1 EBM |
10299 | |
10300 | case NT_PPC_TM_CVMX: | |
10301 | if (note->namesz == 6 | |
4b24dd1a AM |
10302 | && strcmp (note->namedata, "LINUX") == 0) |
10303 | return elfcore_grok_ppc_tm_cvmx (abfd, note); | |
cb2366c1 | 10304 | else |
4b24dd1a | 10305 | return TRUE; |
cb2366c1 EBM |
10306 | |
10307 | case NT_PPC_TM_CVSX: | |
10308 | if (note->namesz == 6 | |
4b24dd1a AM |
10309 | && strcmp (note->namedata, "LINUX") == 0) |
10310 | return elfcore_grok_ppc_tm_cvsx (abfd, note); | |
cb2366c1 | 10311 | else |
4b24dd1a | 10312 | return TRUE; |
cb2366c1 EBM |
10313 | |
10314 | case NT_PPC_TM_SPR: | |
10315 | if (note->namesz == 6 | |
4b24dd1a AM |
10316 | && strcmp (note->namedata, "LINUX") == 0) |
10317 | return elfcore_grok_ppc_tm_spr (abfd, note); | |
cb2366c1 | 10318 | else |
4b24dd1a | 10319 | return TRUE; |
cb2366c1 EBM |
10320 | |
10321 | case NT_PPC_TM_CTAR: | |
10322 | if (note->namesz == 6 | |
4b24dd1a AM |
10323 | && strcmp (note->namedata, "LINUX") == 0) |
10324 | return elfcore_grok_ppc_tm_ctar (abfd, note); | |
cb2366c1 | 10325 | else |
4b24dd1a | 10326 | return TRUE; |
cb2366c1 EBM |
10327 | |
10328 | case NT_PPC_TM_CPPR: | |
10329 | if (note->namesz == 6 | |
4b24dd1a AM |
10330 | && strcmp (note->namedata, "LINUX") == 0) |
10331 | return elfcore_grok_ppc_tm_cppr (abfd, note); | |
cb2366c1 | 10332 | else |
4b24dd1a | 10333 | return TRUE; |
cb2366c1 EBM |
10334 | |
10335 | case NT_PPC_TM_CDSCR: | |
10336 | if (note->namesz == 6 | |
4b24dd1a AM |
10337 | && strcmp (note->namedata, "LINUX") == 0) |
10338 | return elfcore_grok_ppc_tm_cdscr (abfd, note); | |
cb2366c1 | 10339 | else |
4b24dd1a | 10340 | return TRUE; |
cb2366c1 | 10341 | |
0675e188 UW |
10342 | case NT_S390_HIGH_GPRS: |
10343 | if (note->namesz == 6 | |
07d6d2b8 AM |
10344 | && strcmp (note->namedata, "LINUX") == 0) |
10345 | return elfcore_grok_s390_high_gprs (abfd, note); | |
0675e188 | 10346 | else |
07d6d2b8 | 10347 | return TRUE; |
0675e188 | 10348 | |
d7eeb400 MS |
10349 | case NT_S390_TIMER: |
10350 | if (note->namesz == 6 | |
07d6d2b8 AM |
10351 | && strcmp (note->namedata, "LINUX") == 0) |
10352 | return elfcore_grok_s390_timer (abfd, note); | |
d7eeb400 | 10353 | else |
07d6d2b8 | 10354 | return TRUE; |
d7eeb400 MS |
10355 | |
10356 | case NT_S390_TODCMP: | |
10357 | if (note->namesz == 6 | |
07d6d2b8 AM |
10358 | && strcmp (note->namedata, "LINUX") == 0) |
10359 | return elfcore_grok_s390_todcmp (abfd, note); | |
d7eeb400 | 10360 | else |
07d6d2b8 | 10361 | return TRUE; |
d7eeb400 MS |
10362 | |
10363 | case NT_S390_TODPREG: | |
10364 | if (note->namesz == 6 | |
07d6d2b8 AM |
10365 | && strcmp (note->namedata, "LINUX") == 0) |
10366 | return elfcore_grok_s390_todpreg (abfd, note); | |
d7eeb400 | 10367 | else |
07d6d2b8 | 10368 | return TRUE; |
d7eeb400 MS |
10369 | |
10370 | case NT_S390_CTRS: | |
10371 | if (note->namesz == 6 | |
07d6d2b8 AM |
10372 | && strcmp (note->namedata, "LINUX") == 0) |
10373 | return elfcore_grok_s390_ctrs (abfd, note); | |
d7eeb400 | 10374 | else |
07d6d2b8 | 10375 | return TRUE; |
d7eeb400 MS |
10376 | |
10377 | case NT_S390_PREFIX: | |
10378 | if (note->namesz == 6 | |
07d6d2b8 AM |
10379 | && strcmp (note->namedata, "LINUX") == 0) |
10380 | return elfcore_grok_s390_prefix (abfd, note); | |
d7eeb400 | 10381 | else |
07d6d2b8 | 10382 | return TRUE; |
d7eeb400 | 10383 | |
355b81d9 UW |
10384 | case NT_S390_LAST_BREAK: |
10385 | if (note->namesz == 6 | |
07d6d2b8 AM |
10386 | && strcmp (note->namedata, "LINUX") == 0) |
10387 | return elfcore_grok_s390_last_break (abfd, note); | |
355b81d9 | 10388 | else |
07d6d2b8 | 10389 | return TRUE; |
355b81d9 UW |
10390 | |
10391 | case NT_S390_SYSTEM_CALL: | |
10392 | if (note->namesz == 6 | |
07d6d2b8 AM |
10393 | && strcmp (note->namedata, "LINUX") == 0) |
10394 | return elfcore_grok_s390_system_call (abfd, note); | |
355b81d9 | 10395 | else |
07d6d2b8 | 10396 | return TRUE; |
355b81d9 | 10397 | |
abb3f6cc NC |
10398 | case NT_S390_TDB: |
10399 | if (note->namesz == 6 | |
07d6d2b8 AM |
10400 | && strcmp (note->namedata, "LINUX") == 0) |
10401 | return elfcore_grok_s390_tdb (abfd, note); | |
abb3f6cc | 10402 | else |
07d6d2b8 | 10403 | return TRUE; |
abb3f6cc | 10404 | |
4ef9f41a AA |
10405 | case NT_S390_VXRS_LOW: |
10406 | if (note->namesz == 6 | |
10407 | && strcmp (note->namedata, "LINUX") == 0) | |
10408 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
10409 | else | |
10410 | return TRUE; | |
10411 | ||
10412 | case NT_S390_VXRS_HIGH: | |
10413 | if (note->namesz == 6 | |
10414 | && strcmp (note->namedata, "LINUX") == 0) | |
10415 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
10416 | else | |
10417 | return TRUE; | |
10418 | ||
88ab90e8 AA |
10419 | case NT_S390_GS_CB: |
10420 | if (note->namesz == 6 | |
10421 | && strcmp (note->namedata, "LINUX") == 0) | |
8fe09d74 | 10422 | return elfcore_grok_s390_gs_cb (abfd, note); |
88ab90e8 AA |
10423 | else |
10424 | return TRUE; | |
10425 | ||
10426 | case NT_S390_GS_BC: | |
10427 | if (note->namesz == 6 | |
10428 | && strcmp (note->namedata, "LINUX") == 0) | |
8fe09d74 | 10429 | return elfcore_grok_s390_gs_bc (abfd, note); |
88ab90e8 AA |
10430 | else |
10431 | return TRUE; | |
10432 | ||
27456742 AK |
10433 | case NT_ARC_V2: |
10434 | if (note->namesz == 6 | |
10435 | && strcmp (note->namedata, "LINUX") == 0) | |
10436 | return elfcore_grok_arc_v2 (abfd, note); | |
10437 | else | |
10438 | return TRUE; | |
10439 | ||
faa9a424 UW |
10440 | case NT_ARM_VFP: |
10441 | if (note->namesz == 6 | |
10442 | && strcmp (note->namedata, "LINUX") == 0) | |
10443 | return elfcore_grok_arm_vfp (abfd, note); | |
10444 | else | |
10445 | return TRUE; | |
10446 | ||
652451f8 YZ |
10447 | case NT_ARM_TLS: |
10448 | if (note->namesz == 6 | |
10449 | && strcmp (note->namedata, "LINUX") == 0) | |
10450 | return elfcore_grok_aarch_tls (abfd, note); | |
10451 | else | |
10452 | return TRUE; | |
10453 | ||
10454 | case NT_ARM_HW_BREAK: | |
10455 | if (note->namesz == 6 | |
10456 | && strcmp (note->namedata, "LINUX") == 0) | |
10457 | return elfcore_grok_aarch_hw_break (abfd, note); | |
10458 | else | |
10459 | return TRUE; | |
10460 | ||
10461 | case NT_ARM_HW_WATCH: | |
10462 | if (note->namesz == 6 | |
10463 | && strcmp (note->namedata, "LINUX") == 0) | |
10464 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
10465 | else | |
10466 | return TRUE; | |
10467 | ||
ad1cc4e4 AH |
10468 | case NT_ARM_SVE: |
10469 | if (note->namesz == 6 | |
10470 | && strcmp (note->namedata, "LINUX") == 0) | |
10471 | return elfcore_grok_aarch_sve (abfd, note); | |
10472 | else | |
10473 | return TRUE; | |
10474 | ||
e6c3b5bf AH |
10475 | case NT_ARM_PAC_MASK: |
10476 | if (note->namesz == 6 | |
10477 | && strcmp (note->namedata, "LINUX") == 0) | |
10478 | return elfcore_grok_aarch_pauth (abfd, note); | |
10479 | else | |
10480 | return TRUE; | |
10481 | ||
252b5132 RH |
10482 | case NT_PRPSINFO: |
10483 | case NT_PSINFO: | |
bb0082d6 AM |
10484 | if (bed->elf_backend_grok_psinfo) |
10485 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 10486 | return TRUE; |
bb0082d6 | 10487 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 10488 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 10489 | #else |
b34976b6 | 10490 | return TRUE; |
252b5132 | 10491 | #endif |
3333a7c3 RM |
10492 | |
10493 | case NT_AUXV: | |
58e07198 | 10494 | return elfcore_make_auxv_note_section (abfd, note, 0); |
9015683b | 10495 | |
451b7c33 TT |
10496 | case NT_FILE: |
10497 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
10498 | note); | |
10499 | ||
9015683b TT |
10500 | case NT_SIGINFO: |
10501 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
10502 | note); | |
5b2c414d | 10503 | |
252b5132 RH |
10504 | } |
10505 | } | |
10506 | ||
718175fa JK |
10507 | static bfd_boolean |
10508 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
10509 | { | |
c74f7d1c | 10510 | struct bfd_build_id* build_id; |
30e8ee25 AM |
10511 | |
10512 | if (note->descsz == 0) | |
10513 | return FALSE; | |
10514 | ||
c74f7d1c JT |
10515 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
10516 | if (build_id == NULL) | |
718175fa JK |
10517 | return FALSE; |
10518 | ||
c74f7d1c JT |
10519 | build_id->size = note->descsz; |
10520 | memcpy (build_id->data, note->descdata, note->descsz); | |
10521 | abfd->build_id = build_id; | |
718175fa JK |
10522 | |
10523 | return TRUE; | |
10524 | } | |
10525 | ||
10526 | static bfd_boolean | |
10527 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
10528 | { | |
10529 | switch (note->type) | |
10530 | { | |
10531 | default: | |
10532 | return TRUE; | |
10533 | ||
46bed679 L |
10534 | case NT_GNU_PROPERTY_TYPE_0: |
10535 | return _bfd_elf_parse_gnu_properties (abfd, note); | |
10536 | ||
718175fa JK |
10537 | case NT_GNU_BUILD_ID: |
10538 | return elfobj_grok_gnu_build_id (abfd, note); | |
10539 | } | |
10540 | } | |
10541 | ||
e21e5835 NC |
10542 | static bfd_boolean |
10543 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
10544 | { | |
10545 | struct sdt_note *cur = | |
7a6e0d89 AM |
10546 | (struct sdt_note *) bfd_alloc (abfd, |
10547 | sizeof (struct sdt_note) + note->descsz); | |
e21e5835 NC |
10548 | |
10549 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
10550 | cur->size = (bfd_size_type) note->descsz; | |
10551 | memcpy (cur->data, note->descdata, note->descsz); | |
10552 | ||
10553 | elf_tdata (abfd)->sdt_note_head = cur; | |
10554 | ||
10555 | return TRUE; | |
10556 | } | |
10557 | ||
10558 | static bfd_boolean | |
10559 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
10560 | { | |
10561 | switch (note->type) | |
10562 | { | |
10563 | case NT_STAPSDT: | |
10564 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
10565 | ||
10566 | default: | |
10567 | return TRUE; | |
10568 | } | |
10569 | } | |
10570 | ||
aa1ed4a9 JB |
10571 | static bfd_boolean |
10572 | elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
10573 | { | |
10574 | size_t offset; | |
10575 | ||
b5430a3c | 10576 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 10577 | { |
b5430a3c | 10578 | case ELFCLASS32: |
0064d223 JB |
10579 | if (note->descsz < 108) |
10580 | return FALSE; | |
aa1ed4a9 JB |
10581 | break; |
10582 | ||
b5430a3c | 10583 | case ELFCLASS64: |
0064d223 JB |
10584 | if (note->descsz < 120) |
10585 | return FALSE; | |
aa1ed4a9 JB |
10586 | break; |
10587 | ||
10588 | default: | |
10589 | return FALSE; | |
10590 | } | |
10591 | ||
0064d223 JB |
10592 | /* Check for version 1 in pr_version. */ |
10593 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
10594 | return FALSE; | |
80a04378 | 10595 | |
0064d223 JB |
10596 | offset = 4; |
10597 | ||
10598 | /* Skip over pr_psinfosz. */ | |
b5430a3c | 10599 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
0064d223 JB |
10600 | offset += 4; |
10601 | else | |
10602 | { | |
10603 | offset += 4; /* Padding before pr_psinfosz. */ | |
10604 | offset += 8; | |
10605 | } | |
10606 | ||
aa1ed4a9 JB |
10607 | /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ |
10608 | elf_tdata (abfd)->core->program | |
10609 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); | |
10610 | offset += 17; | |
10611 | ||
10612 | /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ | |
10613 | elf_tdata (abfd)->core->command | |
10614 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); | |
0064d223 JB |
10615 | offset += 81; |
10616 | ||
10617 | /* Padding before pr_pid. */ | |
10618 | offset += 2; | |
10619 | ||
10620 | /* The pr_pid field was added in version "1a". */ | |
10621 | if (note->descsz < offset + 4) | |
10622 | return TRUE; | |
10623 | ||
10624 | elf_tdata (abfd)->core->pid | |
10625 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
aa1ed4a9 JB |
10626 | |
10627 | return TRUE; | |
10628 | } | |
10629 | ||
10630 | static bfd_boolean | |
10631 | elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) | |
10632 | { | |
10633 | size_t offset; | |
10634 | size_t size; | |
24d3e51b | 10635 | size_t min_size; |
aa1ed4a9 | 10636 | |
24d3e51b NC |
10637 | /* Compute offset of pr_getregsz, skipping over pr_statussz. |
10638 | Also compute minimum size of this note. */ | |
b5430a3c | 10639 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 10640 | { |
b5430a3c | 10641 | case ELFCLASS32: |
24d3e51b NC |
10642 | offset = 4 + 4; |
10643 | min_size = offset + (4 * 2) + 4 + 4 + 4; | |
aa1ed4a9 JB |
10644 | break; |
10645 | ||
b5430a3c | 10646 | case ELFCLASS64: |
24d3e51b NC |
10647 | offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */ |
10648 | min_size = offset + (8 * 2) + 4 + 4 + 4 + 4; | |
aa1ed4a9 JB |
10649 | break; |
10650 | ||
10651 | default: | |
10652 | return FALSE; | |
10653 | } | |
10654 | ||
24d3e51b NC |
10655 | if (note->descsz < min_size) |
10656 | return FALSE; | |
10657 | ||
10658 | /* Check for version 1 in pr_version. */ | |
10659 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
10660 | return FALSE; | |
aa1ed4a9 | 10661 | |
24d3e51b NC |
10662 | /* Extract size of pr_reg from pr_gregsetsz. */ |
10663 | /* Skip over pr_gregsetsz and pr_fpregsetsz. */ | |
b5430a3c | 10664 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
24d3e51b NC |
10665 | { |
10666 | size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10667 | offset += 4 * 2; | |
10668 | } | |
b5430a3c | 10669 | else |
24d3e51b NC |
10670 | { |
10671 | size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); | |
10672 | offset += 8 * 2; | |
10673 | } | |
aa1ed4a9 | 10674 | |
24d3e51b | 10675 | /* Skip over pr_osreldate. */ |
aa1ed4a9 JB |
10676 | offset += 4; |
10677 | ||
24d3e51b | 10678 | /* Read signal from pr_cursig. */ |
aa1ed4a9 JB |
10679 | if (elf_tdata (abfd)->core->signal == 0) |
10680 | elf_tdata (abfd)->core->signal | |
10681 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10682 | offset += 4; | |
10683 | ||
24d3e51b | 10684 | /* Read TID from pr_pid. */ |
aa1ed4a9 JB |
10685 | elf_tdata (abfd)->core->lwpid |
10686 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
10687 | offset += 4; | |
10688 | ||
24d3e51b | 10689 | /* Padding before pr_reg. */ |
b5430a3c | 10690 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) |
aa1ed4a9 JB |
10691 | offset += 4; |
10692 | ||
24d3e51b NC |
10693 | /* Make sure that there is enough data remaining in the note. */ |
10694 | if ((note->descsz - offset) < size) | |
10695 | return FALSE; | |
10696 | ||
aa1ed4a9 JB |
10697 | /* Make a ".reg/999" section and a ".reg" section. */ |
10698 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
10699 | size, note->descpos + offset); | |
10700 | } | |
10701 | ||
10702 | static bfd_boolean | |
10703 | elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10704 | { | |
544c67cd JB |
10705 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10706 | ||
aa1ed4a9 JB |
10707 | switch (note->type) |
10708 | { | |
10709 | case NT_PRSTATUS: | |
544c67cd JB |
10710 | if (bed->elf_backend_grok_freebsd_prstatus) |
10711 | if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note)) | |
10712 | return TRUE; | |
aa1ed4a9 JB |
10713 | return elfcore_grok_freebsd_prstatus (abfd, note); |
10714 | ||
10715 | case NT_FPREGSET: | |
10716 | return elfcore_grok_prfpreg (abfd, note); | |
10717 | ||
10718 | case NT_PRPSINFO: | |
10719 | return elfcore_grok_freebsd_psinfo (abfd, note); | |
10720 | ||
10721 | case NT_FREEBSD_THRMISC: | |
10722 | if (note->namesz == 8) | |
10723 | return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); | |
10724 | else | |
10725 | return TRUE; | |
10726 | ||
ddb2bbcf JB |
10727 | case NT_FREEBSD_PROCSTAT_PROC: |
10728 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc", | |
10729 | note); | |
10730 | ||
10731 | case NT_FREEBSD_PROCSTAT_FILES: | |
10732 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files", | |
10733 | note); | |
10734 | ||
10735 | case NT_FREEBSD_PROCSTAT_VMMAP: | |
10736 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap", | |
10737 | note); | |
10738 | ||
3350c5f5 | 10739 | case NT_FREEBSD_PROCSTAT_AUXV: |
58e07198 | 10740 | return elfcore_make_auxv_note_section (abfd, note, 4); |
3350c5f5 | 10741 | |
aa1ed4a9 JB |
10742 | case NT_X86_XSTATE: |
10743 | if (note->namesz == 8) | |
10744 | return elfcore_grok_xstatereg (abfd, note); | |
10745 | else | |
10746 | return TRUE; | |
10747 | ||
e6f3b9c3 JB |
10748 | case NT_FREEBSD_PTLWPINFO: |
10749 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo", | |
10750 | note); | |
10751 | ||
6d5be5d6 JB |
10752 | case NT_ARM_VFP: |
10753 | return elfcore_grok_arm_vfp (abfd, note); | |
10754 | ||
aa1ed4a9 JB |
10755 | default: |
10756 | return TRUE; | |
10757 | } | |
10758 | } | |
10759 | ||
b34976b6 | 10760 | static bfd_boolean |
217aa764 | 10761 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
10762 | { |
10763 | char *cp; | |
10764 | ||
10765 | cp = strchr (note->namedata, '@'); | |
10766 | if (cp != NULL) | |
10767 | { | |
d2b64500 | 10768 | *lwpidp = atoi(cp + 1); |
b34976b6 | 10769 | return TRUE; |
50b2bdb7 | 10770 | } |
b34976b6 | 10771 | return FALSE; |
50b2bdb7 AM |
10772 | } |
10773 | ||
b34976b6 | 10774 | static bfd_boolean |
217aa764 | 10775 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 10776 | { |
80a04378 NC |
10777 | if (note->descsz <= 0x7c + 31) |
10778 | return FALSE; | |
10779 | ||
50b2bdb7 | 10780 | /* Signal number at offset 0x08. */ |
228e534f | 10781 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
10782 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10783 | ||
10784 | /* Process ID at offset 0x50. */ | |
228e534f | 10785 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
10786 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
10787 | ||
10788 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 10789 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
10790 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
10791 | ||
7720ba9f MK |
10792 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
10793 | note); | |
50b2bdb7 AM |
10794 | } |
10795 | ||
b34976b6 | 10796 | static bfd_boolean |
217aa764 | 10797 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
10798 | { |
10799 | int lwp; | |
10800 | ||
10801 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 10802 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 10803 | |
58e07198 | 10804 | switch (note->type) |
50b2bdb7 | 10805 | { |
58e07198 | 10806 | case NT_NETBSDCORE_PROCINFO: |
50b2bdb7 | 10807 | /* NetBSD-specific core "procinfo". Note that we expect to |
08a40648 AM |
10808 | find this note before any of the others, which is fine, |
10809 | since the kernel writes this note out first when it | |
10810 | creates a core file. */ | |
50b2bdb7 | 10811 | return elfcore_grok_netbsd_procinfo (abfd, note); |
58e07198 CZ |
10812 | #ifdef NT_NETBSDCORE_AUXV |
10813 | case NT_NETBSDCORE_AUXV: | |
10814 | /* NetBSD-specific Elf Auxiliary Vector data. */ | |
10815 | return elfcore_make_auxv_note_section (abfd, note, 4); | |
06d949ec KR |
10816 | #endif |
10817 | #ifdef NT_NETBSDCORE_LWPSTATUS | |
10818 | case NT_NETBSDCORE_LWPSTATUS: | |
10819 | return elfcore_make_note_pseudosection (abfd, | |
10820 | ".note.netbsdcore.lwpstatus", | |
10821 | note); | |
58e07198 CZ |
10822 | #endif |
10823 | default: | |
10824 | break; | |
50b2bdb7 AM |
10825 | } |
10826 | ||
06d949ec | 10827 | /* As of March 2020 there are no other machine-independent notes |
b4db1224 JT |
10828 | defined for NetBSD core files. If the note type is less |
10829 | than the start of the machine-dependent note types, we don't | |
10830 | understand it. */ | |
47d9a591 | 10831 | |
b4db1224 | 10832 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 10833 | return TRUE; |
50b2bdb7 AM |
10834 | |
10835 | ||
10836 | switch (bfd_get_arch (abfd)) | |
10837 | { | |
08a40648 AM |
10838 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
10839 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 | 10840 | |
015ec493 | 10841 | case bfd_arch_aarch64: |
50b2bdb7 AM |
10842 | case bfd_arch_alpha: |
10843 | case bfd_arch_sparc: | |
10844 | switch (note->type) | |
08a40648 AM |
10845 | { |
10846 | case NT_NETBSDCORE_FIRSTMACH+0: | |
10847 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10848 | |
08a40648 AM |
10849 | case NT_NETBSDCORE_FIRSTMACH+2: |
10850 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10851 | |
08a40648 AM |
10852 | default: |
10853 | return TRUE; | |
10854 | } | |
50b2bdb7 | 10855 | |
58e07198 CZ |
10856 | /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5. |
10857 | There's also old PT___GETREGS40 == mach + 1 for old reg | |
10858 | structure which lacks GBR. */ | |
10859 | ||
10860 | case bfd_arch_sh: | |
10861 | switch (note->type) | |
10862 | { | |
10863 | case NT_NETBSDCORE_FIRSTMACH+3: | |
10864 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
10865 | ||
10866 | case NT_NETBSDCORE_FIRSTMACH+5: | |
10867 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
10868 | ||
10869 | default: | |
10870 | return TRUE; | |
10871 | } | |
10872 | ||
08a40648 AM |
10873 | /* On all other arch's, PT_GETREGS == mach+1 and |
10874 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
10875 | |
10876 | default: | |
10877 | switch (note->type) | |
08a40648 AM |
10878 | { |
10879 | case NT_NETBSDCORE_FIRSTMACH+1: | |
10880 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10881 | |
08a40648 AM |
10882 | case NT_NETBSDCORE_FIRSTMACH+3: |
10883 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10884 | |
08a40648 AM |
10885 | default: |
10886 | return TRUE; | |
10887 | } | |
50b2bdb7 AM |
10888 | } |
10889 | /* NOTREACHED */ | |
10890 | } | |
10891 | ||
67cc5033 MK |
10892 | static bfd_boolean |
10893 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
10894 | { | |
80a04378 NC |
10895 | if (note->descsz <= 0x48 + 31) |
10896 | return FALSE; | |
10897 | ||
67cc5033 | 10898 | /* Signal number at offset 0x08. */ |
228e534f | 10899 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
10900 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10901 | ||
10902 | /* Process ID at offset 0x20. */ | |
228e534f | 10903 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
10904 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
10905 | ||
10906 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 10907 | elf_tdata (abfd)->core->command |
67cc5033 MK |
10908 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
10909 | ||
10910 | return TRUE; | |
10911 | } | |
10912 | ||
10913 | static bfd_boolean | |
10914 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10915 | { | |
10916 | if (note->type == NT_OPENBSD_PROCINFO) | |
10917 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
10918 | ||
10919 | if (note->type == NT_OPENBSD_REGS) | |
10920 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
10921 | ||
10922 | if (note->type == NT_OPENBSD_FPREGS) | |
10923 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
10924 | ||
10925 | if (note->type == NT_OPENBSD_XFPREGS) | |
10926 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
10927 | ||
10928 | if (note->type == NT_OPENBSD_AUXV) | |
58e07198 | 10929 | return elfcore_make_auxv_note_section (abfd, note, 0); |
67cc5033 MK |
10930 | |
10931 | if (note->type == NT_OPENBSD_WCOOKIE) | |
10932 | { | |
10933 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
10934 | SEC_HAS_CONTENTS); | |
10935 | ||
10936 | if (sect == NULL) | |
10937 | return FALSE; | |
10938 | sect->size = note->descsz; | |
10939 | sect->filepos = note->descpos; | |
10940 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10941 | ||
10942 | return TRUE; | |
10943 | } | |
10944 | ||
10945 | return TRUE; | |
10946 | } | |
10947 | ||
07c6e936 | 10948 | static bfd_boolean |
d3fd4074 | 10949 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
10950 | { |
10951 | void *ddata = note->descdata; | |
10952 | char buf[100]; | |
10953 | char *name; | |
10954 | asection *sect; | |
f8843e87 AM |
10955 | short sig; |
10956 | unsigned flags; | |
07c6e936 | 10957 | |
80a04378 NC |
10958 | if (note->descsz < 16) |
10959 | return FALSE; | |
10960 | ||
07c6e936 | 10961 | /* nto_procfs_status 'pid' field is at offset 0. */ |
228e534f | 10962 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 10963 | |
f8843e87 AM |
10964 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
10965 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
10966 | ||
10967 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
10968 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
10969 | |
10970 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
10971 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
10972 | { | |
228e534f AM |
10973 | elf_tdata (abfd)->core->signal = sig; |
10974 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 10975 | } |
07c6e936 | 10976 | |
f8843e87 AM |
10977 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
10978 | do not come from signals so we make sure we set the current | |
10979 | thread just in case. */ | |
10980 | if (flags & 0x00000080) | |
228e534f | 10981 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
10982 | |
10983 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 10984 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 10985 | |
a50b1753 | 10986 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10987 | if (name == NULL) |
10988 | return FALSE; | |
10989 | strcpy (name, buf); | |
10990 | ||
117ed4f8 | 10991 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10992 | if (sect == NULL) |
10993 | return FALSE; | |
10994 | ||
07d6d2b8 AM |
10995 | sect->size = note->descsz; |
10996 | sect->filepos = note->descpos; | |
07c6e936 NC |
10997 | sect->alignment_power = 2; |
10998 | ||
10999 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
11000 | } | |
11001 | ||
11002 | static bfd_boolean | |
d69f560c KW |
11003 | elfcore_grok_nto_regs (bfd *abfd, |
11004 | Elf_Internal_Note *note, | |
d3fd4074 | 11005 | long tid, |
d69f560c | 11006 | char *base) |
07c6e936 NC |
11007 | { |
11008 | char buf[100]; | |
11009 | char *name; | |
11010 | asection *sect; | |
11011 | ||
d69f560c | 11012 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 11013 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 11014 | |
a50b1753 | 11015 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
11016 | if (name == NULL) |
11017 | return FALSE; | |
11018 | strcpy (name, buf); | |
11019 | ||
117ed4f8 | 11020 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
11021 | if (sect == NULL) |
11022 | return FALSE; | |
11023 | ||
07d6d2b8 AM |
11024 | sect->size = note->descsz; |
11025 | sect->filepos = note->descpos; | |
07c6e936 NC |
11026 | sect->alignment_power = 2; |
11027 | ||
f8843e87 | 11028 | /* This is the current thread. */ |
228e534f | 11029 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 11030 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
11031 | |
11032 | return TRUE; | |
07c6e936 NC |
11033 | } |
11034 | ||
11035 | #define BFD_QNT_CORE_INFO 7 | |
11036 | #define BFD_QNT_CORE_STATUS 8 | |
11037 | #define BFD_QNT_CORE_GREG 9 | |
11038 | #define BFD_QNT_CORE_FPREG 10 | |
11039 | ||
11040 | static bfd_boolean | |
217aa764 | 11041 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
11042 | { |
11043 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 11044 | tid from the previous call to pass down to the next gregs |
07c6e936 | 11045 | function. */ |
d3fd4074 | 11046 | static long tid = 1; |
07c6e936 NC |
11047 | |
11048 | switch (note->type) | |
11049 | { | |
d69f560c KW |
11050 | case BFD_QNT_CORE_INFO: |
11051 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
11052 | case BFD_QNT_CORE_STATUS: | |
11053 | return elfcore_grok_nto_status (abfd, note, &tid); | |
11054 | case BFD_QNT_CORE_GREG: | |
11055 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
11056 | case BFD_QNT_CORE_FPREG: | |
11057 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
11058 | default: | |
11059 | return TRUE; | |
07c6e936 NC |
11060 | } |
11061 | } | |
11062 | ||
b15fa79e AM |
11063 | static bfd_boolean |
11064 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
11065 | { | |
11066 | char *name; | |
11067 | asection *sect; | |
11068 | size_t len; | |
11069 | ||
11070 | /* Use note name as section name. */ | |
11071 | len = note->namesz; | |
a50b1753 | 11072 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
11073 | if (name == NULL) |
11074 | return FALSE; | |
11075 | memcpy (name, note->namedata, len); | |
11076 | name[len - 1] = '\0'; | |
11077 | ||
11078 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
11079 | if (sect == NULL) | |
11080 | return FALSE; | |
11081 | ||
07d6d2b8 AM |
11082 | sect->size = note->descsz; |
11083 | sect->filepos = note->descpos; | |
b15fa79e AM |
11084 | sect->alignment_power = 1; |
11085 | ||
11086 | return TRUE; | |
11087 | } | |
11088 | ||
7c76fa91 MS |
11089 | /* Function: elfcore_write_note |
11090 | ||
47d9a591 | 11091 | Inputs: |
a39f3346 | 11092 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
11093 | name of note |
11094 | type of note | |
11095 | data for note | |
11096 | size of data for note | |
11097 | ||
a39f3346 AM |
11098 | Writes note to end of buffer. ELF64 notes are written exactly as |
11099 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
11100 | that they ought to have 8-byte namesz and descsz field, and have | |
11101 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
11102 | ||
7c76fa91 | 11103 | Return: |
a39f3346 | 11104 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
11105 | |
11106 | char * | |
a39f3346 | 11107 | elfcore_write_note (bfd *abfd, |
217aa764 | 11108 | char *buf, |
a39f3346 | 11109 | int *bufsiz, |
217aa764 | 11110 | const char *name, |
a39f3346 | 11111 | int type, |
217aa764 | 11112 | const void *input, |
a39f3346 | 11113 | int size) |
7c76fa91 MS |
11114 | { |
11115 | Elf_External_Note *xnp; | |
d4c88bbb | 11116 | size_t namesz; |
d4c88bbb | 11117 | size_t newspace; |
a39f3346 | 11118 | char *dest; |
7c76fa91 | 11119 | |
d4c88bbb | 11120 | namesz = 0; |
d4c88bbb | 11121 | if (name != NULL) |
a39f3346 | 11122 | namesz = strlen (name) + 1; |
d4c88bbb | 11123 | |
a39f3346 | 11124 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 11125 | |
a50b1753 | 11126 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
11127 | if (buf == NULL) |
11128 | return buf; | |
a39f3346 | 11129 | dest = buf + *bufsiz; |
7c76fa91 MS |
11130 | *bufsiz += newspace; |
11131 | xnp = (Elf_External_Note *) dest; | |
11132 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
11133 | H_PUT_32 (abfd, size, xnp->descsz); | |
11134 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
11135 | dest = xnp->name; |
11136 | if (name != NULL) | |
11137 | { | |
11138 | memcpy (dest, name, namesz); | |
11139 | dest += namesz; | |
a39f3346 | 11140 | while (namesz & 3) |
d4c88bbb AM |
11141 | { |
11142 | *dest++ = '\0'; | |
a39f3346 | 11143 | ++namesz; |
d4c88bbb AM |
11144 | } |
11145 | } | |
11146 | memcpy (dest, input, size); | |
a39f3346 AM |
11147 | dest += size; |
11148 | while (size & 3) | |
11149 | { | |
11150 | *dest++ = '\0'; | |
11151 | ++size; | |
11152 | } | |
11153 | return buf; | |
7c76fa91 MS |
11154 | } |
11155 | ||
602f1657 AM |
11156 | /* gcc-8 warns (*) on all the strncpy calls in this function about |
11157 | possible string truncation. The "truncation" is not a bug. We | |
11158 | have an external representation of structs with fields that are not | |
11159 | necessarily NULL terminated and corresponding internal | |
11160 | representation fields that are one larger so that they can always | |
11161 | be NULL terminated. | |
11162 | gcc versions between 4.2 and 4.6 do not allow pragma control of | |
11163 | diagnostics inside functions, giving a hard error if you try to use | |
11164 | the finer control available with later versions. | |
11165 | gcc prior to 4.2 warns about diagnostic push and pop. | |
11166 | gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown, | |
11167 | unless you also add #pragma GCC diagnostic ignored "-Wpragma". | |
11168 | (*) Depending on your system header files! */ | |
d99b4b92 | 11169 | #if GCC_VERSION >= 8000 |
602f1657 AM |
11170 | # pragma GCC diagnostic push |
11171 | # pragma GCC diagnostic ignored "-Wstringop-truncation" | |
d99b4b92 | 11172 | #endif |
7c76fa91 | 11173 | char * |
217aa764 AM |
11174 | elfcore_write_prpsinfo (bfd *abfd, |
11175 | char *buf, | |
11176 | int *bufsiz, | |
11177 | const char *fname, | |
11178 | const char *psargs) | |
7c76fa91 | 11179 | { |
183e98be AM |
11180 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11181 | ||
11182 | if (bed->elf_backend_write_core_note != NULL) | |
11183 | { | |
11184 | char *ret; | |
11185 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
11186 | NT_PRPSINFO, fname, psargs); | |
11187 | if (ret != NULL) | |
11188 | return ret; | |
11189 | } | |
7c76fa91 | 11190 | |
1f20dca5 | 11191 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
602f1657 | 11192 | # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
183e98be AM |
11193 | if (bed->s->elfclass == ELFCLASS32) |
11194 | { | |
602f1657 | 11195 | # if defined (HAVE_PSINFO32_T) |
183e98be AM |
11196 | psinfo32_t data; |
11197 | int note_type = NT_PSINFO; | |
602f1657 | 11198 | # else |
183e98be AM |
11199 | prpsinfo32_t data; |
11200 | int note_type = NT_PRPSINFO; | |
602f1657 | 11201 | # endif |
183e98be AM |
11202 | |
11203 | memset (&data, 0, sizeof (data)); | |
11204 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
11205 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
11206 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 11207 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
11208 | } |
11209 | else | |
602f1657 | 11210 | # endif |
183e98be | 11211 | { |
602f1657 | 11212 | # if defined (HAVE_PSINFO_T) |
183e98be AM |
11213 | psinfo_t data; |
11214 | int note_type = NT_PSINFO; | |
602f1657 | 11215 | # else |
183e98be AM |
11216 | prpsinfo_t data; |
11217 | int note_type = NT_PRPSINFO; | |
602f1657 | 11218 | # endif |
7c76fa91 | 11219 | |
183e98be AM |
11220 | memset (&data, 0, sizeof (data)); |
11221 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
11222 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
11223 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 11224 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 11225 | } |
7c76fa91 MS |
11226 | #endif /* PSINFO_T or PRPSINFO_T */ |
11227 | ||
1f20dca5 UW |
11228 | free (buf); |
11229 | return NULL; | |
11230 | } | |
d99b4b92 | 11231 | #if GCC_VERSION >= 8000 |
602f1657 | 11232 | # pragma GCC diagnostic pop |
d99b4b92 | 11233 | #endif |
1f20dca5 | 11234 | |
70a38d42 SDJ |
11235 | char * |
11236 | elfcore_write_linux_prpsinfo32 | |
11237 | (bfd *abfd, char *buf, int *bufsiz, | |
11238 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
11239 | { | |
a2f63b2e MR |
11240 | if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16) |
11241 | { | |
11242 | struct elf_external_linux_prpsinfo32_ugid16 data; | |
11243 | ||
11244 | swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data); | |
11245 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
11246 | &data, sizeof (data)); | |
11247 | } | |
11248 | else | |
11249 | { | |
11250 | struct elf_external_linux_prpsinfo32_ugid32 data; | |
70a38d42 | 11251 | |
a2f63b2e MR |
11252 | swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data); |
11253 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, | |
11254 | &data, sizeof (data)); | |
11255 | } | |
70a38d42 SDJ |
11256 | } |
11257 | ||
11258 | char * | |
11259 | elfcore_write_linux_prpsinfo64 | |
11260 | (bfd *abfd, char *buf, int *bufsiz, | |
11261 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
11262 | { | |
3c9a7b0d MR |
11263 | if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16) |
11264 | { | |
11265 | struct elf_external_linux_prpsinfo64_ugid16 data; | |
11266 | ||
11267 | swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data); | |
11268 | return elfcore_write_note (abfd, buf, bufsiz, | |
11269 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
11270 | } | |
11271 | else | |
11272 | { | |
11273 | struct elf_external_linux_prpsinfo64_ugid32 data; | |
70a38d42 | 11274 | |
3c9a7b0d MR |
11275 | swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data); |
11276 | return elfcore_write_note (abfd, buf, bufsiz, | |
11277 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
11278 | } | |
70a38d42 SDJ |
11279 | } |
11280 | ||
7c76fa91 | 11281 | char * |
217aa764 AM |
11282 | elfcore_write_prstatus (bfd *abfd, |
11283 | char *buf, | |
11284 | int *bufsiz, | |
11285 | long pid, | |
11286 | int cursig, | |
11287 | const void *gregs) | |
7c76fa91 | 11288 | { |
183e98be | 11289 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 11290 | |
183e98be AM |
11291 | if (bed->elf_backend_write_core_note != NULL) |
11292 | { | |
11293 | char *ret; | |
11294 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
11295 | NT_PRSTATUS, | |
11296 | pid, cursig, gregs); | |
11297 | if (ret != NULL) | |
11298 | return ret; | |
11299 | } | |
11300 | ||
1f20dca5 | 11301 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
11302 | #if defined (HAVE_PRSTATUS32_T) |
11303 | if (bed->s->elfclass == ELFCLASS32) | |
11304 | { | |
11305 | prstatus32_t prstat; | |
11306 | ||
11307 | memset (&prstat, 0, sizeof (prstat)); | |
11308 | prstat.pr_pid = pid; | |
11309 | prstat.pr_cursig = cursig; | |
11310 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 11311 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
11312 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
11313 | } | |
11314 | else | |
11315 | #endif | |
11316 | { | |
11317 | prstatus_t prstat; | |
11318 | ||
11319 | memset (&prstat, 0, sizeof (prstat)); | |
11320 | prstat.pr_pid = pid; | |
11321 | prstat.pr_cursig = cursig; | |
11322 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 11323 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
11324 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
11325 | } | |
7c76fa91 MS |
11326 | #endif /* HAVE_PRSTATUS_T */ |
11327 | ||
1f20dca5 UW |
11328 | free (buf); |
11329 | return NULL; | |
11330 | } | |
11331 | ||
51316059 MS |
11332 | #if defined (HAVE_LWPSTATUS_T) |
11333 | char * | |
217aa764 AM |
11334 | elfcore_write_lwpstatus (bfd *abfd, |
11335 | char *buf, | |
11336 | int *bufsiz, | |
11337 | long pid, | |
11338 | int cursig, | |
11339 | const void *gregs) | |
51316059 MS |
11340 | { |
11341 | lwpstatus_t lwpstat; | |
183e98be | 11342 | const char *note_name = "CORE"; |
51316059 MS |
11343 | |
11344 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
11345 | lwpstat.pr_lwpid = pid >> 16; | |
11346 | lwpstat.pr_cursig = cursig; | |
11347 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 11348 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
11349 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
11350 | #if !defined(gregs) | |
11351 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
11352 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
11353 | #else | |
11354 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
11355 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
11356 | #endif | |
11357 | #endif | |
47d9a591 | 11358 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
11359 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
11360 | } | |
11361 | #endif /* HAVE_LWPSTATUS_T */ | |
11362 | ||
7c76fa91 MS |
11363 | #if defined (HAVE_PSTATUS_T) |
11364 | char * | |
217aa764 AM |
11365 | elfcore_write_pstatus (bfd *abfd, |
11366 | char *buf, | |
11367 | int *bufsiz, | |
11368 | long pid, | |
6c10990d NC |
11369 | int cursig ATTRIBUTE_UNUSED, |
11370 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 11371 | { |
183e98be AM |
11372 | const char *note_name = "CORE"; |
11373 | #if defined (HAVE_PSTATUS32_T) | |
11374 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 11375 | |
183e98be AM |
11376 | if (bed->s->elfclass == ELFCLASS32) |
11377 | { | |
11378 | pstatus32_t pstat; | |
11379 | ||
11380 | memset (&pstat, 0, sizeof (pstat)); | |
11381 | pstat.pr_pid = pid & 0xffff; | |
11382 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
11383 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
11384 | return buf; | |
11385 | } | |
11386 | else | |
11387 | #endif | |
11388 | { | |
11389 | pstatus_t pstat; | |
11390 | ||
11391 | memset (&pstat, 0, sizeof (pstat)); | |
11392 | pstat.pr_pid = pid & 0xffff; | |
11393 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
11394 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
11395 | return buf; | |
11396 | } | |
7c76fa91 MS |
11397 | } |
11398 | #endif /* HAVE_PSTATUS_T */ | |
11399 | ||
11400 | char * | |
217aa764 AM |
11401 | elfcore_write_prfpreg (bfd *abfd, |
11402 | char *buf, | |
11403 | int *bufsiz, | |
11404 | const void *fpregs, | |
11405 | int size) | |
7c76fa91 | 11406 | { |
183e98be | 11407 | const char *note_name = "CORE"; |
47d9a591 | 11408 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
11409 | note_name, NT_FPREGSET, fpregs, size); |
11410 | } | |
11411 | ||
11412 | char * | |
217aa764 AM |
11413 | elfcore_write_prxfpreg (bfd *abfd, |
11414 | char *buf, | |
11415 | int *bufsiz, | |
11416 | const void *xfpregs, | |
11417 | int size) | |
7c76fa91 MS |
11418 | { |
11419 | char *note_name = "LINUX"; | |
47d9a591 | 11420 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
11421 | note_name, NT_PRXFPREG, xfpregs, size); |
11422 | } | |
11423 | ||
4339cae0 L |
11424 | char * |
11425 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
11426 | const void *xfpregs, int size) | |
11427 | { | |
97de3545 JB |
11428 | char *note_name; |
11429 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
11430 | note_name = "FreeBSD"; | |
11431 | else | |
11432 | note_name = "LINUX"; | |
4339cae0 L |
11433 | return elfcore_write_note (abfd, buf, bufsiz, |
11434 | note_name, NT_X86_XSTATE, xfpregs, size); | |
11435 | } | |
11436 | ||
97753bd5 AM |
11437 | char * |
11438 | elfcore_write_ppc_vmx (bfd *abfd, | |
11439 | char *buf, | |
11440 | int *bufsiz, | |
11441 | const void *ppc_vmx, | |
11442 | int size) | |
11443 | { | |
11444 | char *note_name = "LINUX"; | |
11445 | return elfcore_write_note (abfd, buf, bufsiz, | |
11446 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
11447 | } | |
11448 | ||
89eeb0bc LM |
11449 | char * |
11450 | elfcore_write_ppc_vsx (bfd *abfd, | |
07d6d2b8 AM |
11451 | char *buf, |
11452 | int *bufsiz, | |
11453 | const void *ppc_vsx, | |
11454 | int size) | |
89eeb0bc LM |
11455 | { |
11456 | char *note_name = "LINUX"; | |
11457 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11458 | note_name, NT_PPC_VSX, ppc_vsx, size); |
89eeb0bc LM |
11459 | } |
11460 | ||
cb2366c1 EBM |
11461 | char * |
11462 | elfcore_write_ppc_tar (bfd *abfd, | |
4b24dd1a AM |
11463 | char *buf, |
11464 | int *bufsiz, | |
11465 | const void *ppc_tar, | |
11466 | int size) | |
cb2366c1 EBM |
11467 | { |
11468 | char *note_name = "LINUX"; | |
11469 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11470 | note_name, NT_PPC_TAR, ppc_tar, size); |
cb2366c1 EBM |
11471 | } |
11472 | ||
11473 | char * | |
11474 | elfcore_write_ppc_ppr (bfd *abfd, | |
4b24dd1a AM |
11475 | char *buf, |
11476 | int *bufsiz, | |
11477 | const void *ppc_ppr, | |
11478 | int size) | |
cb2366c1 EBM |
11479 | { |
11480 | char *note_name = "LINUX"; | |
11481 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11482 | note_name, NT_PPC_PPR, ppc_ppr, size); |
cb2366c1 EBM |
11483 | } |
11484 | ||
11485 | char * | |
11486 | elfcore_write_ppc_dscr (bfd *abfd, | |
4b24dd1a AM |
11487 | char *buf, |
11488 | int *bufsiz, | |
11489 | const void *ppc_dscr, | |
11490 | int size) | |
cb2366c1 EBM |
11491 | { |
11492 | char *note_name = "LINUX"; | |
11493 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11494 | note_name, NT_PPC_DSCR, ppc_dscr, size); |
cb2366c1 EBM |
11495 | } |
11496 | ||
11497 | char * | |
11498 | elfcore_write_ppc_ebb (bfd *abfd, | |
4b24dd1a AM |
11499 | char *buf, |
11500 | int *bufsiz, | |
11501 | const void *ppc_ebb, | |
11502 | int size) | |
cb2366c1 EBM |
11503 | { |
11504 | char *note_name = "LINUX"; | |
11505 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11506 | note_name, NT_PPC_EBB, ppc_ebb, size); |
cb2366c1 EBM |
11507 | } |
11508 | ||
11509 | char * | |
11510 | elfcore_write_ppc_pmu (bfd *abfd, | |
4b24dd1a AM |
11511 | char *buf, |
11512 | int *bufsiz, | |
11513 | const void *ppc_pmu, | |
11514 | int size) | |
cb2366c1 EBM |
11515 | { |
11516 | char *note_name = "LINUX"; | |
11517 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11518 | note_name, NT_PPC_PMU, ppc_pmu, size); |
cb2366c1 EBM |
11519 | } |
11520 | ||
11521 | char * | |
11522 | elfcore_write_ppc_tm_cgpr (bfd *abfd, | |
4b24dd1a AM |
11523 | char *buf, |
11524 | int *bufsiz, | |
11525 | const void *ppc_tm_cgpr, | |
11526 | int size) | |
cb2366c1 EBM |
11527 | { |
11528 | char *note_name = "LINUX"; | |
11529 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11530 | note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size); |
cb2366c1 EBM |
11531 | } |
11532 | ||
11533 | char * | |
11534 | elfcore_write_ppc_tm_cfpr (bfd *abfd, | |
4b24dd1a AM |
11535 | char *buf, |
11536 | int *bufsiz, | |
11537 | const void *ppc_tm_cfpr, | |
11538 | int size) | |
cb2366c1 EBM |
11539 | { |
11540 | char *note_name = "LINUX"; | |
11541 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11542 | note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size); |
cb2366c1 EBM |
11543 | } |
11544 | ||
11545 | char * | |
11546 | elfcore_write_ppc_tm_cvmx (bfd *abfd, | |
4b24dd1a AM |
11547 | char *buf, |
11548 | int *bufsiz, | |
11549 | const void *ppc_tm_cvmx, | |
11550 | int size) | |
cb2366c1 EBM |
11551 | { |
11552 | char *note_name = "LINUX"; | |
11553 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11554 | note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size); |
cb2366c1 EBM |
11555 | } |
11556 | ||
11557 | char * | |
11558 | elfcore_write_ppc_tm_cvsx (bfd *abfd, | |
4b24dd1a AM |
11559 | char *buf, |
11560 | int *bufsiz, | |
11561 | const void *ppc_tm_cvsx, | |
11562 | int size) | |
cb2366c1 EBM |
11563 | { |
11564 | char *note_name = "LINUX"; | |
11565 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11566 | note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size); |
cb2366c1 EBM |
11567 | } |
11568 | ||
11569 | char * | |
11570 | elfcore_write_ppc_tm_spr (bfd *abfd, | |
4b24dd1a AM |
11571 | char *buf, |
11572 | int *bufsiz, | |
11573 | const void *ppc_tm_spr, | |
11574 | int size) | |
cb2366c1 EBM |
11575 | { |
11576 | char *note_name = "LINUX"; | |
11577 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11578 | note_name, NT_PPC_TM_SPR, ppc_tm_spr, size); |
cb2366c1 EBM |
11579 | } |
11580 | ||
11581 | char * | |
11582 | elfcore_write_ppc_tm_ctar (bfd *abfd, | |
4b24dd1a AM |
11583 | char *buf, |
11584 | int *bufsiz, | |
11585 | const void *ppc_tm_ctar, | |
11586 | int size) | |
cb2366c1 EBM |
11587 | { |
11588 | char *note_name = "LINUX"; | |
11589 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11590 | note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size); |
cb2366c1 EBM |
11591 | } |
11592 | ||
11593 | char * | |
11594 | elfcore_write_ppc_tm_cppr (bfd *abfd, | |
4b24dd1a AM |
11595 | char *buf, |
11596 | int *bufsiz, | |
11597 | const void *ppc_tm_cppr, | |
11598 | int size) | |
cb2366c1 EBM |
11599 | { |
11600 | char *note_name = "LINUX"; | |
11601 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11602 | note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size); |
cb2366c1 EBM |
11603 | } |
11604 | ||
11605 | char * | |
11606 | elfcore_write_ppc_tm_cdscr (bfd *abfd, | |
4b24dd1a AM |
11607 | char *buf, |
11608 | int *bufsiz, | |
11609 | const void *ppc_tm_cdscr, | |
11610 | int size) | |
cb2366c1 EBM |
11611 | { |
11612 | char *note_name = "LINUX"; | |
11613 | return elfcore_write_note (abfd, buf, bufsiz, | |
4b24dd1a | 11614 | note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size); |
cb2366c1 EBM |
11615 | } |
11616 | ||
0675e188 UW |
11617 | static char * |
11618 | elfcore_write_s390_high_gprs (bfd *abfd, | |
11619 | char *buf, | |
11620 | int *bufsiz, | |
11621 | const void *s390_high_gprs, | |
11622 | int size) | |
11623 | { | |
11624 | char *note_name = "LINUX"; | |
11625 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11626 | note_name, NT_S390_HIGH_GPRS, |
0675e188 UW |
11627 | s390_high_gprs, size); |
11628 | } | |
11629 | ||
d7eeb400 MS |
11630 | char * |
11631 | elfcore_write_s390_timer (bfd *abfd, | |
07d6d2b8 AM |
11632 | char *buf, |
11633 | int *bufsiz, | |
11634 | const void *s390_timer, | |
11635 | int size) | |
d7eeb400 MS |
11636 | { |
11637 | char *note_name = "LINUX"; | |
11638 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11639 | note_name, NT_S390_TIMER, s390_timer, size); |
d7eeb400 MS |
11640 | } |
11641 | ||
11642 | char * | |
11643 | elfcore_write_s390_todcmp (bfd *abfd, | |
07d6d2b8 AM |
11644 | char *buf, |
11645 | int *bufsiz, | |
11646 | const void *s390_todcmp, | |
11647 | int size) | |
d7eeb400 MS |
11648 | { |
11649 | char *note_name = "LINUX"; | |
11650 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11651 | note_name, NT_S390_TODCMP, s390_todcmp, size); |
d7eeb400 MS |
11652 | } |
11653 | ||
11654 | char * | |
11655 | elfcore_write_s390_todpreg (bfd *abfd, | |
07d6d2b8 AM |
11656 | char *buf, |
11657 | int *bufsiz, | |
11658 | const void *s390_todpreg, | |
11659 | int size) | |
d7eeb400 MS |
11660 | { |
11661 | char *note_name = "LINUX"; | |
11662 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11663 | note_name, NT_S390_TODPREG, s390_todpreg, size); |
d7eeb400 MS |
11664 | } |
11665 | ||
11666 | char * | |
11667 | elfcore_write_s390_ctrs (bfd *abfd, | |
07d6d2b8 AM |
11668 | char *buf, |
11669 | int *bufsiz, | |
11670 | const void *s390_ctrs, | |
11671 | int size) | |
d7eeb400 MS |
11672 | { |
11673 | char *note_name = "LINUX"; | |
11674 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11675 | note_name, NT_S390_CTRS, s390_ctrs, size); |
d7eeb400 MS |
11676 | } |
11677 | ||
11678 | char * | |
11679 | elfcore_write_s390_prefix (bfd *abfd, | |
07d6d2b8 AM |
11680 | char *buf, |
11681 | int *bufsiz, | |
11682 | const void *s390_prefix, | |
11683 | int size) | |
d7eeb400 MS |
11684 | { |
11685 | char *note_name = "LINUX"; | |
11686 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11687 | note_name, NT_S390_PREFIX, s390_prefix, size); |
d7eeb400 MS |
11688 | } |
11689 | ||
355b81d9 UW |
11690 | char * |
11691 | elfcore_write_s390_last_break (bfd *abfd, | |
11692 | char *buf, | |
11693 | int *bufsiz, | |
11694 | const void *s390_last_break, | |
11695 | int size) | |
11696 | { | |
11697 | char *note_name = "LINUX"; | |
11698 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11699 | note_name, NT_S390_LAST_BREAK, |
355b81d9 UW |
11700 | s390_last_break, size); |
11701 | } | |
11702 | ||
11703 | char * | |
11704 | elfcore_write_s390_system_call (bfd *abfd, | |
11705 | char *buf, | |
11706 | int *bufsiz, | |
11707 | const void *s390_system_call, | |
11708 | int size) | |
11709 | { | |
11710 | char *note_name = "LINUX"; | |
11711 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11712 | note_name, NT_S390_SYSTEM_CALL, |
355b81d9 UW |
11713 | s390_system_call, size); |
11714 | } | |
11715 | ||
abb3f6cc NC |
11716 | char * |
11717 | elfcore_write_s390_tdb (bfd *abfd, | |
11718 | char *buf, | |
11719 | int *bufsiz, | |
11720 | const void *s390_tdb, | |
11721 | int size) | |
11722 | { | |
11723 | char *note_name = "LINUX"; | |
11724 | return elfcore_write_note (abfd, buf, bufsiz, | |
07d6d2b8 | 11725 | note_name, NT_S390_TDB, s390_tdb, size); |
abb3f6cc NC |
11726 | } |
11727 | ||
4ef9f41a AA |
11728 | char * |
11729 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
11730 | char *buf, | |
11731 | int *bufsiz, | |
11732 | const void *s390_vxrs_low, | |
11733 | int size) | |
11734 | { | |
11735 | char *note_name = "LINUX"; | |
11736 | return elfcore_write_note (abfd, buf, bufsiz, | |
11737 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
11738 | } | |
11739 | ||
11740 | char * | |
11741 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
11742 | char *buf, | |
11743 | int *bufsiz, | |
11744 | const void *s390_vxrs_high, | |
11745 | int size) | |
11746 | { | |
11747 | char *note_name = "LINUX"; | |
11748 | return elfcore_write_note (abfd, buf, bufsiz, | |
11749 | note_name, NT_S390_VXRS_HIGH, | |
11750 | s390_vxrs_high, size); | |
11751 | } | |
11752 | ||
88ab90e8 AA |
11753 | char * |
11754 | elfcore_write_s390_gs_cb (bfd *abfd, | |
11755 | char *buf, | |
11756 | int *bufsiz, | |
11757 | const void *s390_gs_cb, | |
11758 | int size) | |
11759 | { | |
11760 | char *note_name = "LINUX"; | |
11761 | return elfcore_write_note (abfd, buf, bufsiz, | |
11762 | note_name, NT_S390_GS_CB, | |
11763 | s390_gs_cb, size); | |
11764 | } | |
11765 | ||
11766 | char * | |
11767 | elfcore_write_s390_gs_bc (bfd *abfd, | |
11768 | char *buf, | |
11769 | int *bufsiz, | |
11770 | const void *s390_gs_bc, | |
11771 | int size) | |
11772 | { | |
11773 | char *note_name = "LINUX"; | |
11774 | return elfcore_write_note (abfd, buf, bufsiz, | |
11775 | note_name, NT_S390_GS_BC, | |
11776 | s390_gs_bc, size); | |
11777 | } | |
11778 | ||
faa9a424 UW |
11779 | char * |
11780 | elfcore_write_arm_vfp (bfd *abfd, | |
11781 | char *buf, | |
11782 | int *bufsiz, | |
11783 | const void *arm_vfp, | |
11784 | int size) | |
11785 | { | |
11786 | char *note_name = "LINUX"; | |
11787 | return elfcore_write_note (abfd, buf, bufsiz, | |
11788 | note_name, NT_ARM_VFP, arm_vfp, size); | |
11789 | } | |
11790 | ||
652451f8 YZ |
11791 | char * |
11792 | elfcore_write_aarch_tls (bfd *abfd, | |
11793 | char *buf, | |
11794 | int *bufsiz, | |
11795 | const void *aarch_tls, | |
11796 | int size) | |
11797 | { | |
11798 | char *note_name = "LINUX"; | |
11799 | return elfcore_write_note (abfd, buf, bufsiz, | |
11800 | note_name, NT_ARM_TLS, aarch_tls, size); | |
11801 | } | |
11802 | ||
11803 | char * | |
11804 | elfcore_write_aarch_hw_break (bfd *abfd, | |
11805 | char *buf, | |
11806 | int *bufsiz, | |
11807 | const void *aarch_hw_break, | |
11808 | int size) | |
11809 | { | |
11810 | char *note_name = "LINUX"; | |
11811 | return elfcore_write_note (abfd, buf, bufsiz, | |
11812 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
11813 | } | |
11814 | ||
11815 | char * | |
11816 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
11817 | char *buf, | |
11818 | int *bufsiz, | |
11819 | const void *aarch_hw_watch, | |
11820 | int size) | |
11821 | { | |
11822 | char *note_name = "LINUX"; | |
11823 | return elfcore_write_note (abfd, buf, bufsiz, | |
11824 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
11825 | } | |
11826 | ||
ad1cc4e4 AH |
11827 | char * |
11828 | elfcore_write_aarch_sve (bfd *abfd, | |
11829 | char *buf, | |
11830 | int *bufsiz, | |
11831 | const void *aarch_sve, | |
11832 | int size) | |
11833 | { | |
11834 | char *note_name = "LINUX"; | |
11835 | return elfcore_write_note (abfd, buf, bufsiz, | |
11836 | note_name, NT_ARM_SVE, aarch_sve, size); | |
11837 | } | |
11838 | ||
e6c3b5bf AH |
11839 | char * |
11840 | elfcore_write_aarch_pauth (bfd *abfd, | |
11841 | char *buf, | |
11842 | int *bufsiz, | |
11843 | const void *aarch_pauth, | |
11844 | int size) | |
11845 | { | |
11846 | char *note_name = "LINUX"; | |
11847 | return elfcore_write_note (abfd, buf, bufsiz, | |
11848 | note_name, NT_ARM_PAC_MASK, aarch_pauth, size); | |
11849 | } | |
11850 | ||
27456742 AK |
11851 | char * |
11852 | elfcore_write_arc_v2 (bfd *abfd, | |
11853 | char *buf, | |
11854 | int *bufsiz, | |
11855 | const void *arc_v2, | |
11856 | int size) | |
11857 | { | |
11858 | char *note_name = "LINUX"; | |
11859 | return elfcore_write_note (abfd, buf, bufsiz, | |
11860 | note_name, NT_ARC_V2, arc_v2, size); | |
11861 | } | |
11862 | ||
bb864ac1 CES |
11863 | char * |
11864 | elfcore_write_register_note (bfd *abfd, | |
11865 | char *buf, | |
11866 | int *bufsiz, | |
11867 | const char *section, | |
11868 | const void *data, | |
11869 | int size) | |
11870 | { | |
11871 | if (strcmp (section, ".reg2") == 0) | |
11872 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
11873 | if (strcmp (section, ".reg-xfp") == 0) | |
11874 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
11875 | if (strcmp (section, ".reg-xstate") == 0) |
11876 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
11877 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
11878 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
11879 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
11880 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
cb2366c1 EBM |
11881 | if (strcmp (section, ".reg-ppc-tar") == 0) |
11882 | return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size); | |
11883 | if (strcmp (section, ".reg-ppc-ppr") == 0) | |
11884 | return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size); | |
11885 | if (strcmp (section, ".reg-ppc-dscr") == 0) | |
11886 | return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size); | |
11887 | if (strcmp (section, ".reg-ppc-ebb") == 0) | |
11888 | return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size); | |
11889 | if (strcmp (section, ".reg-ppc-pmu") == 0) | |
11890 | return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size); | |
11891 | if (strcmp (section, ".reg-ppc-tm-cgpr") == 0) | |
11892 | return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size); | |
11893 | if (strcmp (section, ".reg-ppc-tm-cfpr") == 0) | |
11894 | return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size); | |
11895 | if (strcmp (section, ".reg-ppc-tm-cvmx") == 0) | |
11896 | return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size); | |
11897 | if (strcmp (section, ".reg-ppc-tm-cvsx") == 0) | |
11898 | return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size); | |
11899 | if (strcmp (section, ".reg-ppc-tm-spr") == 0) | |
11900 | return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size); | |
11901 | if (strcmp (section, ".reg-ppc-tm-ctar") == 0) | |
11902 | return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size); | |
11903 | if (strcmp (section, ".reg-ppc-tm-cppr") == 0) | |
11904 | return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size); | |
11905 | if (strcmp (section, ".reg-ppc-tm-cdscr") == 0) | |
11906 | return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
11907 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
11908 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
11909 | if (strcmp (section, ".reg-s390-timer") == 0) |
11910 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
11911 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
11912 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
11913 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
11914 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
11915 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
11916 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
11917 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
11918 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
11919 | if (strcmp (section, ".reg-s390-last-break") == 0) |
11920 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
11921 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
11922 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
11923 | if (strcmp (section, ".reg-s390-tdb") == 0) |
11924 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
11925 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
11926 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
11927 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
11928 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
88ab90e8 AA |
11929 | if (strcmp (section, ".reg-s390-gs-cb") == 0) |
11930 | return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size); | |
11931 | if (strcmp (section, ".reg-s390-gs-bc") == 0) | |
11932 | return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
11933 | if (strcmp (section, ".reg-arm-vfp") == 0) |
11934 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
11935 | if (strcmp (section, ".reg-aarch-tls") == 0) |
11936 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
11937 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
11938 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
11939 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
11940 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
ad1cc4e4 AH |
11941 | if (strcmp (section, ".reg-aarch-sve") == 0) |
11942 | return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size); | |
e6c3b5bf AH |
11943 | if (strcmp (section, ".reg-aarch-pauth") == 0) |
11944 | return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size); | |
27456742 AK |
11945 | if (strcmp (section, ".reg-arc-v2") == 0) |
11946 | return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
11947 | return NULL; |
11948 | } | |
11949 | ||
b34976b6 | 11950 | static bfd_boolean |
276da9b3 L |
11951 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset, |
11952 | size_t align) | |
252b5132 | 11953 | { |
c044fabd | 11954 | char *p; |
252b5132 | 11955 | |
276da9b3 L |
11956 | /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1. |
11957 | gABI specifies that PT_NOTE alignment should be aligned to 4 | |
11958 | bytes for 32-bit objects and to 8 bytes for 64-bit objects. If | |
11959 | align is less than 4, we use 4 byte alignment. */ | |
11960 | if (align < 4) | |
11961 | align = 4; | |
ef135d43 NC |
11962 | if (align != 4 && align != 8) |
11963 | return FALSE; | |
276da9b3 | 11964 | |
252b5132 RH |
11965 | p = buf; |
11966 | while (p < buf + size) | |
11967 | { | |
c044fabd | 11968 | Elf_External_Note *xnp = (Elf_External_Note *) p; |
252b5132 RH |
11969 | Elf_Internal_Note in; |
11970 | ||
baea7ef1 AM |
11971 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
11972 | return FALSE; | |
11973 | ||
dc810e39 | 11974 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 11975 | |
dc810e39 | 11976 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 11977 | in.namedata = xnp->name; |
baea7ef1 AM |
11978 | if (in.namesz > buf - in.namedata + size) |
11979 | return FALSE; | |
252b5132 | 11980 | |
dc810e39 | 11981 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
276da9b3 | 11982 | in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align); |
252b5132 | 11983 | in.descpos = offset + (in.descdata - buf); |
baea7ef1 AM |
11984 | if (in.descsz != 0 |
11985 | && (in.descdata >= buf + size | |
11986 | || in.descsz > buf - in.descdata + size)) | |
11987 | return FALSE; | |
252b5132 | 11988 | |
718175fa | 11989 | switch (bfd_get_format (abfd)) |
07d6d2b8 | 11990 | { |
718175fa JK |
11991 | default: |
11992 | return TRUE; | |
11993 | ||
11994 | case bfd_core: | |
f64e188b | 11995 | { |
8acbedd6 | 11996 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 11997 | struct |
718175fa | 11998 | { |
f64e188b | 11999 | const char * string; |
8acbedd6 | 12000 | size_t len; |
f64e188b | 12001 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 12002 | } |
f64e188b | 12003 | grokers[] = |
b15fa79e | 12004 | { |
8acbedd6 | 12005 | GROKER_ELEMENT ("", elfcore_grok_note), |
aa1ed4a9 | 12006 | GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), |
8acbedd6 KS |
12007 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), |
12008 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
12009 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
864619bb KS |
12010 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note), |
12011 | GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note) | |
f64e188b | 12012 | }; |
8acbedd6 | 12013 | #undef GROKER_ELEMENT |
f64e188b NC |
12014 | int i; |
12015 | ||
12016 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
12017 | { |
12018 | if (in.namesz >= grokers[i].len | |
12019 | && strncmp (in.namedata, grokers[i].string, | |
12020 | grokers[i].len) == 0) | |
12021 | { | |
12022 | if (! grokers[i].func (abfd, & in)) | |
12023 | return FALSE; | |
12024 | break; | |
12025 | } | |
12026 | } | |
f64e188b NC |
12027 | break; |
12028 | } | |
718175fa JK |
12029 | |
12030 | case bfd_object: | |
12031 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
12032 | { | |
12033 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
12034 | return FALSE; | |
12035 | } | |
e21e5835 NC |
12036 | else if (in.namesz == sizeof "stapsdt" |
12037 | && strcmp (in.namedata, "stapsdt") == 0) | |
12038 | { | |
12039 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
12040 | return FALSE; | |
12041 | } | |
718175fa | 12042 | break; |
08a40648 | 12043 | } |
252b5132 | 12044 | |
276da9b3 | 12045 | p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align); |
252b5132 RH |
12046 | } |
12047 | ||
718175fa JK |
12048 | return TRUE; |
12049 | } | |
12050 | ||
864619bb | 12051 | bfd_boolean |
276da9b3 L |
12052 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size, |
12053 | size_t align) | |
718175fa JK |
12054 | { |
12055 | char *buf; | |
12056 | ||
957e1fc1 | 12057 | if (size == 0 || (size + 1) == 0) |
718175fa JK |
12058 | return TRUE; |
12059 | ||
12060 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
12061 | return FALSE; | |
12062 | ||
2bb3687b | 12063 | buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size); |
718175fa JK |
12064 | if (buf == NULL) |
12065 | return FALSE; | |
12066 | ||
f64e188b NC |
12067 | /* PR 17512: file: ec08f814 |
12068 | 0-termintate the buffer so that string searches will not overflow. */ | |
12069 | buf[size] = 0; | |
12070 | ||
2bb3687b | 12071 | if (!elf_parse_notes (abfd, buf, size, offset, align)) |
718175fa JK |
12072 | { |
12073 | free (buf); | |
12074 | return FALSE; | |
12075 | } | |
12076 | ||
252b5132 | 12077 | free (buf); |
b34976b6 | 12078 | return TRUE; |
252b5132 | 12079 | } |
98d8431c JB |
12080 | \f |
12081 | /* Providing external access to the ELF program header table. */ | |
12082 | ||
12083 | /* Return an upper bound on the number of bytes required to store a | |
12084 | copy of ABFD's program header table entries. Return -1 if an error | |
12085 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 12086 | |
98d8431c | 12087 | long |
217aa764 | 12088 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
12089 | { |
12090 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
12091 | { | |
12092 | bfd_set_error (bfd_error_wrong_format); | |
12093 | return -1; | |
12094 | } | |
12095 | ||
936e320b | 12096 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
12097 | } |
12098 | ||
98d8431c JB |
12099 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
12100 | will be stored as an array of Elf_Internal_Phdr structures, as | |
12101 | defined in include/elf/internal.h. To find out how large the | |
12102 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
12103 | ||
12104 | Return the number of program header table entries read, or -1 if an | |
12105 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 12106 | |
98d8431c | 12107 | int |
217aa764 | 12108 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
12109 | { |
12110 | int num_phdrs; | |
12111 | ||
12112 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
12113 | { | |
12114 | bfd_set_error (bfd_error_wrong_format); | |
12115 | return -1; | |
12116 | } | |
12117 | ||
12118 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
01bcaf63 TT |
12119 | if (num_phdrs != 0) |
12120 | memcpy (phdrs, elf_tdata (abfd)->phdr, | |
12121 | num_phdrs * sizeof (Elf_Internal_Phdr)); | |
98d8431c JB |
12122 | |
12123 | return num_phdrs; | |
12124 | } | |
ae4221d7 | 12125 | |
db6751f2 | 12126 | enum elf_reloc_type_class |
7e612e98 AM |
12127 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
12128 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
12129 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
12130 | { |
12131 | return reloc_class_normal; | |
12132 | } | |
f8df10f4 | 12133 | |
47d9a591 | 12134 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
12135 | relocation against a local symbol. */ |
12136 | ||
12137 | bfd_vma | |
217aa764 AM |
12138 | _bfd_elf_rela_local_sym (bfd *abfd, |
12139 | Elf_Internal_Sym *sym, | |
8517fae7 | 12140 | asection **psec, |
217aa764 | 12141 | Elf_Internal_Rela *rel) |
f8df10f4 | 12142 | { |
8517fae7 | 12143 | asection *sec = *psec; |
f8df10f4 JJ |
12144 | bfd_vma relocation; |
12145 | ||
6835821b AM |
12146 | relocation = (sec->output_section->vma |
12147 | + sec->output_offset | |
12148 | + sym->st_value); | |
f8df10f4 | 12149 | if ((sec->flags & SEC_MERGE) |
c629eae0 | 12150 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 12151 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 12152 | { |
f8df10f4 | 12153 | rel->r_addend = |
8517fae7 | 12154 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 12155 | elf_section_data (sec)->sec_info, |
753731ee AM |
12156 | sym->st_value + rel->r_addend); |
12157 | if (sec != *psec) | |
12158 | { | |
12159 | /* If we have changed the section, and our original section is | |
12160 | marked with SEC_EXCLUDE, it means that the original | |
12161 | SEC_MERGE section has been completely subsumed in some | |
12162 | other SEC_MERGE section. In this case, we need to leave | |
12163 | some info around for --emit-relocs. */ | |
12164 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
12165 | sec->kept_section = *psec; | |
12166 | sec = *psec; | |
12167 | } | |
8517fae7 AM |
12168 | rel->r_addend -= relocation; |
12169 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
12170 | } |
12171 | return relocation; | |
12172 | } | |
c629eae0 JJ |
12173 | |
12174 | bfd_vma | |
217aa764 AM |
12175 | _bfd_elf_rel_local_sym (bfd *abfd, |
12176 | Elf_Internal_Sym *sym, | |
12177 | asection **psec, | |
12178 | bfd_vma addend) | |
47d9a591 | 12179 | { |
c629eae0 JJ |
12180 | asection *sec = *psec; |
12181 | ||
6835821b | 12182 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
12183 | return sym->st_value + addend; |
12184 | ||
12185 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 12186 | elf_section_data (sec)->sec_info, |
753731ee | 12187 | sym->st_value + addend); |
c629eae0 JJ |
12188 | } |
12189 | ||
37b01f6a DG |
12190 | /* Adjust an address within a section. Given OFFSET within SEC, return |
12191 | the new offset within the section, based upon changes made to the | |
12192 | section. Returns -1 if the offset is now invalid. | |
12193 | The offset (in abnd out) is in target sized bytes, however big a | |
12194 | byte may be. */ | |
12195 | ||
c629eae0 | 12196 | bfd_vma |
217aa764 | 12197 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 12198 | struct bfd_link_info *info, |
217aa764 AM |
12199 | asection *sec, |
12200 | bfd_vma offset) | |
c629eae0 | 12201 | { |
68bfbfcc | 12202 | switch (sec->sec_info_type) |
65765700 | 12203 | { |
dbaa2011 | 12204 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
12205 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
12206 | offset); | |
dbaa2011 | 12207 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 12208 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
37b01f6a | 12209 | |
65765700 | 12210 | default: |
310fd250 L |
12211 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
12212 | { | |
37b01f6a | 12213 | /* Reverse the offset. */ |
310fd250 L |
12214 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
12215 | bfd_size_type address_size = bed->s->arch_size / 8; | |
37b01f6a DG |
12216 | |
12217 | /* address_size and sec->size are in octets. Convert | |
12218 | to bytes before subtracting the original offset. */ | |
61826503 | 12219 | offset = ((sec->size - address_size) |
bb294208 | 12220 | / bfd_octets_per_byte (abfd, sec) - offset); |
310fd250 | 12221 | } |
65765700 JJ |
12222 | return offset; |
12223 | } | |
c629eae0 | 12224 | } |
3333a7c3 RM |
12225 | \f |
12226 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
12227 | reconstruct an ELF file by reading the segments out of remote memory | |
12228 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
12229 | points to. If not null, *LOADBASEP is filled in with the difference | |
12230 | between the VMAs from which the segments were read, and the VMAs the | |
12231 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
12232 | ||
12233 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
12234 | remote memory at target address VMA into the local buffer at MYADDR; it | |
12235 | should return zero on success or an `errno' code on failure. TEMPL must | |
12236 | be a BFD for an ELF target with the word size and byte order found in | |
12237 | the remote memory. */ | |
12238 | ||
12239 | bfd * | |
217aa764 AM |
12240 | bfd_elf_bfd_from_remote_memory |
12241 | (bfd *templ, | |
12242 | bfd_vma ehdr_vma, | |
f0a5d95a | 12243 | bfd_size_type size, |
217aa764 | 12244 | bfd_vma *loadbasep, |
fe78531d | 12245 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
12246 | { |
12247 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 12248 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 12249 | } |
4c45e5c9 JJ |
12250 | \f |
12251 | long | |
c9727e01 AM |
12252 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
12253 | long symcount ATTRIBUTE_UNUSED, | |
12254 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 12255 | long dynsymcount, |
c9727e01 AM |
12256 | asymbol **dynsyms, |
12257 | asymbol **ret) | |
4c45e5c9 JJ |
12258 | { |
12259 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
12260 | asection *relplt; | |
12261 | asymbol *s; | |
12262 | const char *relplt_name; | |
12263 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
12264 | arelent *p; | |
12265 | long count, i, n; | |
12266 | size_t size; | |
12267 | Elf_Internal_Shdr *hdr; | |
12268 | char *names; | |
12269 | asection *plt; | |
12270 | ||
8615f3f2 AM |
12271 | *ret = NULL; |
12272 | ||
90e3cdf2 JJ |
12273 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
12274 | return 0; | |
12275 | ||
8615f3f2 AM |
12276 | if (dynsymcount <= 0) |
12277 | return 0; | |
12278 | ||
4c45e5c9 JJ |
12279 | if (!bed->plt_sym_val) |
12280 | return 0; | |
12281 | ||
12282 | relplt_name = bed->relplt_name; | |
12283 | if (relplt_name == NULL) | |
d35fd659 | 12284 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
12285 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
12286 | if (relplt == NULL) | |
12287 | return 0; | |
12288 | ||
12289 | hdr = &elf_section_data (relplt)->this_hdr; | |
12290 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
12291 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
12292 | return 0; | |
12293 | ||
12294 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
12295 | if (plt == NULL) | |
12296 | return 0; | |
12297 | ||
12298 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 12299 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
12300 | return -1; |
12301 | ||
eea6121a | 12302 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
12303 | size = count * sizeof (asymbol); |
12304 | p = relplt->relocation; | |
cb53bf42 | 12305 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
12306 | { |
12307 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
12308 | if (p->addend != 0) | |
12309 | { | |
12310 | #ifdef BFD64 | |
12311 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
12312 | #else | |
12313 | size += sizeof ("+0x") - 1 + 8; | |
12314 | #endif | |
12315 | } | |
12316 | } | |
4c45e5c9 | 12317 | |
a50b1753 | 12318 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
12319 | if (s == NULL) |
12320 | return -1; | |
12321 | ||
12322 | names = (char *) (s + count); | |
12323 | p = relplt->relocation; | |
12324 | n = 0; | |
cb53bf42 | 12325 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
12326 | { |
12327 | size_t len; | |
12328 | bfd_vma addr; | |
12329 | ||
12330 | addr = bed->plt_sym_val (i, plt, p); | |
12331 | if (addr == (bfd_vma) -1) | |
12332 | continue; | |
12333 | ||
12334 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
12335 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
12336 | we are defining a symbol, ensure one of them is set. */ | |
12337 | if ((s->flags & BSF_LOCAL) == 0) | |
12338 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 12339 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
12340 | s->section = plt; |
12341 | s->value = addr - plt->vma; | |
12342 | s->name = names; | |
8f39ba8e | 12343 | s->udata.p = NULL; |
4c45e5c9 JJ |
12344 | len = strlen ((*p->sym_ptr_ptr)->name); |
12345 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
12346 | names += len; | |
041de40d AM |
12347 | if (p->addend != 0) |
12348 | { | |
1d770845 | 12349 | char buf[30], *a; |
d324f6d6 | 12350 | |
041de40d AM |
12351 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
12352 | names += sizeof ("+0x") - 1; | |
1d770845 L |
12353 | bfd_sprintf_vma (abfd, buf, p->addend); |
12354 | for (a = buf; *a == '0'; ++a) | |
12355 | ; | |
12356 | len = strlen (a); | |
12357 | memcpy (names, a, len); | |
12358 | names += len; | |
041de40d | 12359 | } |
4c45e5c9 JJ |
12360 | memcpy (names, "@plt", sizeof ("@plt")); |
12361 | names += sizeof ("@plt"); | |
8f39ba8e | 12362 | ++s, ++n; |
4c45e5c9 JJ |
12363 | } |
12364 | ||
12365 | return n; | |
12366 | } | |
3d7f7666 | 12367 | |
821e6ff6 AM |
12368 | /* It is only used by x86-64 so far. |
12369 | ??? This repeats *COM* id of zero. sec->id is supposed to be unique, | |
7eacd66b AM |
12370 | but current usage would allow all of _bfd_std_section to be zero. */ |
12371 | static const asymbol lcomm_sym | |
12372 | = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section); | |
3b22753a | 12373 | asection _bfd_elf_large_com_section |
7eacd66b | 12374 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym, |
821e6ff6 | 12375 | "LARGE_COMMON", 0, SEC_IS_COMMON); |
ecca9871 | 12376 | |
cc364be6 AM |
12377 | bfd_boolean |
12378 | _bfd_elf_final_write_processing (bfd *abfd) | |
06f44071 AM |
12379 | { |
12380 | Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */ | |
d1036acb L |
12381 | |
12382 | i_ehdrp = elf_elfheader (abfd); | |
12383 | ||
06f44071 AM |
12384 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) |
12385 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 | 12386 | |
df3a023b AM |
12387 | /* Set the osabi field to ELFOSABI_GNU if the binary contains |
12388 | SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or | |
12389 | STB_GNU_UNIQUE binding. */ | |
cc364be6 AM |
12390 | if (elf_tdata (abfd)->has_gnu_osabi != 0) |
12391 | { | |
12392 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) | |
12393 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; | |
12394 | else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU | |
12395 | && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD) | |
12396 | { | |
12397 | if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) | |
12398 | _bfd_error_handler (_("GNU_MBIND section is unsupported")); | |
12399 | if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc) | |
12400 | _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported")); | |
12401 | if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique) | |
12402 | _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported")); | |
9aea1e31 | 12403 | bfd_set_error (bfd_error_sorry); |
cc364be6 AM |
12404 | return FALSE; |
12405 | } | |
12406 | } | |
12407 | return TRUE; | |
d1036acb | 12408 | } |
fcb93ecf PB |
12409 | |
12410 | ||
12411 | /* Return TRUE for ELF symbol types that represent functions. | |
12412 | This is the default version of this function, which is sufficient for | |
d8045f23 | 12413 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
12414 | |
12415 | bfd_boolean | |
12416 | _bfd_elf_is_function_type (unsigned int type) | |
12417 | { | |
d8045f23 NC |
12418 | return (type == STT_FUNC |
12419 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 12420 | } |
9f296da3 | 12421 | |
aef36ac1 AM |
12422 | /* If the ELF symbol SYM might be a function in SEC, return the |
12423 | function size and set *CODE_OFF to the function's entry point, | |
12424 | otherwise return zero. */ | |
9f296da3 | 12425 | |
aef36ac1 AM |
12426 | bfd_size_type |
12427 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
12428 | bfd_vma *code_off) | |
9f296da3 | 12429 | { |
aef36ac1 AM |
12430 | bfd_size_type size; |
12431 | ||
ff9e0f5b | 12432 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
12433 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
12434 | || sym->section != sec) | |
12435 | return 0; | |
ff9e0f5b | 12436 | |
ff9e0f5b | 12437 | *code_off = sym->value; |
aef36ac1 AM |
12438 | size = 0; |
12439 | if (!(sym->flags & BSF_SYNTHETIC)) | |
12440 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
12441 | if (size == 0) | |
12442 | size = 1; | |
12443 | return size; | |
9f296da3 | 12444 | } |
a8e14f4c NC |
12445 | |
12446 | /* Set to non-zero to enable some debug messages. */ | |
12447 | #define DEBUG_SECONDARY_RELOCS 0 | |
12448 | ||
12449 | /* An internal-to-the-bfd-library only section type | |
12450 | used to indicate a cached secondary reloc section. */ | |
12451 | #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA) | |
12452 | ||
12453 | /* Create a BFD section to hold a secondary reloc section. */ | |
12454 | ||
12455 | bfd_boolean | |
12456 | _bfd_elf_init_secondary_reloc_section (bfd * abfd, | |
12457 | Elf_Internal_Shdr *hdr, | |
12458 | const char * name, | |
12459 | unsigned int shindex) | |
12460 | { | |
12461 | /* We only support RELA secondary relocs. */ | |
12462 | if (hdr->sh_type != SHT_RELA) | |
12463 | return FALSE; | |
12464 | ||
12465 | #if DEBUG_SECONDARY_RELOCS | |
12466 | fprintf (stderr, "secondary reloc section %s encountered\n", name); | |
12467 | #endif | |
12468 | hdr->sh_type = SHT_SECONDARY_RELOC; | |
12469 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
12470 | } | |
12471 | ||
12472 | /* Read in any secondary relocs associated with SEC. */ | |
12473 | ||
12474 | bfd_boolean | |
12475 | _bfd_elf_slurp_secondary_reloc_section (bfd * abfd, | |
12476 | asection * sec, | |
12477 | asymbol ** symbols) | |
12478 | { | |
12479 | const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); | |
12480 | asection * relsec; | |
12481 | bfd_boolean result = TRUE; | |
12482 | bfd_vma (*r_sym) (bfd_vma); | |
12483 | ||
12484 | #if BFD_DEFAULT_TARGET_SIZE > 32 | |
12485 | if (bfd_arch_bits_per_address (abfd) != 32) | |
12486 | r_sym = elf64_r_sym; | |
12487 | else | |
12488 | #endif | |
12489 | r_sym = elf32_r_sym; | |
12490 | ||
12491 | /* Discover if there are any secondary reloc sections | |
12492 | associated with SEC. */ | |
12493 | for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) | |
12494 | { | |
12495 | Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr; | |
12496 | ||
12497 | if (hdr->sh_type == SHT_SECONDARY_RELOC | |
12498 | && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx) | |
12499 | { | |
12500 | bfd_byte * native_relocs; | |
12501 | bfd_byte * native_reloc; | |
12502 | arelent * internal_relocs; | |
12503 | arelent * internal_reloc; | |
12504 | unsigned int i; | |
12505 | unsigned int entsize; | |
12506 | unsigned int symcount; | |
12507 | unsigned int reloc_count; | |
12508 | size_t amt; | |
12509 | ||
12510 | if (ebd->elf_info_to_howto == NULL) | |
12511 | return FALSE; | |
12512 | ||
12513 | #if DEBUG_SECONDARY_RELOCS | |
12514 | fprintf (stderr, "read secondary relocs for %s from %s\n", | |
12515 | sec->name, relsec->name); | |
12516 | #endif | |
12517 | entsize = hdr->sh_entsize; | |
12518 | ||
12519 | native_relocs = bfd_malloc (hdr->sh_size); | |
12520 | if (native_relocs == NULL) | |
12521 | { | |
12522 | result = FALSE; | |
12523 | continue; | |
12524 | } | |
12525 | ||
12526 | reloc_count = NUM_SHDR_ENTRIES (hdr); | |
12527 | if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt)) | |
12528 | { | |
ecbbbdba | 12529 | free (native_relocs); |
a8e14f4c NC |
12530 | bfd_set_error (bfd_error_file_too_big); |
12531 | result = FALSE; | |
12532 | continue; | |
12533 | } | |
12534 | ||
12535 | internal_relocs = (arelent *) bfd_alloc (abfd, amt); | |
12536 | if (internal_relocs == NULL) | |
12537 | { | |
12538 | free (native_relocs); | |
12539 | result = FALSE; | |
12540 | continue; | |
12541 | } | |
12542 | ||
12543 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
12544 | || (bfd_bread (native_relocs, hdr->sh_size, abfd) | |
12545 | != hdr->sh_size)) | |
12546 | { | |
12547 | free (native_relocs); | |
ecbbbdba NC |
12548 | /* The internal_relocs will be freed when |
12549 | the memory for the bfd is released. */ | |
a8e14f4c NC |
12550 | result = FALSE; |
12551 | continue; | |
12552 | } | |
12553 | ||
12554 | symcount = bfd_get_symcount (abfd); | |
12555 | ||
12556 | for (i = 0, internal_reloc = internal_relocs, | |
12557 | native_reloc = native_relocs; | |
12558 | i < reloc_count; | |
12559 | i++, internal_reloc++, native_reloc += entsize) | |
12560 | { | |
12561 | bfd_boolean res; | |
12562 | Elf_Internal_Rela rela; | |
12563 | ||
12564 | ebd->s->swap_reloca_in (abfd, native_reloc, & rela); | |
12565 | ||
12566 | /* The address of an ELF reloc is section relative for an object | |
12567 | file, and absolute for an executable file or shared library. | |
12568 | The address of a normal BFD reloc is always section relative, | |
12569 | and the address of a dynamic reloc is absolute.. */ | |
12570 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) | |
12571 | internal_reloc->address = rela.r_offset; | |
12572 | else | |
12573 | internal_reloc->address = rela.r_offset - sec->vma; | |
12574 | ||
12575 | if (r_sym (rela.r_info) == STN_UNDEF) | |
12576 | { | |
12577 | /* FIXME: This and the error case below mean that we | |
12578 | have a symbol on relocs that is not elf_symbol_type. */ | |
12579 | internal_reloc->sym_ptr_ptr = | |
12580 | bfd_abs_section_ptr->symbol_ptr_ptr; | |
12581 | } | |
12582 | else if (r_sym (rela.r_info) > symcount) | |
12583 | { | |
12584 | _bfd_error_handler | |
12585 | /* xgettext:c-format */ | |
12586 | (_("%pB(%pA): relocation %d has invalid symbol index %ld"), | |
12587 | abfd, sec, i, (long) r_sym (rela.r_info)); | |
12588 | bfd_set_error (bfd_error_bad_value); | |
12589 | internal_reloc->sym_ptr_ptr = | |
12590 | bfd_abs_section_ptr->symbol_ptr_ptr; | |
12591 | result = FALSE; | |
12592 | } | |
12593 | else | |
12594 | { | |
12595 | asymbol **ps; | |
12596 | ||
12597 | ps = symbols + r_sym (rela.r_info) - 1; | |
12598 | ||
12599 | internal_reloc->sym_ptr_ptr = ps; | |
12600 | /* Make sure that this symbol is not removed by strip. */ | |
12601 | (*ps)->flags |= BSF_KEEP; | |
12602 | } | |
12603 | ||
12604 | internal_reloc->addend = rela.r_addend; | |
12605 | ||
12606 | res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela); | |
12607 | if (! res || internal_reloc->howto == NULL) | |
12608 | { | |
12609 | #if DEBUG_SECONDARY_RELOCS | |
12610 | fprintf (stderr, "there is no howto associated with reloc %lx\n", | |
12611 | rela.r_info); | |
12612 | #endif | |
12613 | result = FALSE; | |
12614 | } | |
12615 | } | |
12616 | ||
12617 | free (native_relocs); | |
12618 | /* Store the internal relocs. */ | |
12619 | elf_section_data (relsec)->sec_info = internal_relocs; | |
12620 | } | |
12621 | } | |
12622 | ||
12623 | return result; | |
12624 | } | |
12625 | ||
12626 | /* Set the ELF section header fields of an output secondary reloc section. */ | |
12627 | ||
12628 | bfd_boolean | |
12629 | _bfd_elf_copy_special_section_fields (const bfd * ibfd ATTRIBUTE_UNUSED, | |
12630 | bfd * obfd ATTRIBUTE_UNUSED, | |
12631 | const Elf_Internal_Shdr * isection, | |
12632 | Elf_Internal_Shdr * osection) | |
12633 | { | |
12634 | asection * isec; | |
12635 | asection * osec; | |
12636 | ||
12637 | if (isection == NULL) | |
12638 | return FALSE; | |
12639 | ||
12640 | if (isection->sh_type != SHT_SECONDARY_RELOC) | |
12641 | return TRUE; | |
12642 | ||
12643 | isec = isection->bfd_section; | |
12644 | if (isec == NULL) | |
12645 | return FALSE; | |
12646 | ||
12647 | osec = osection->bfd_section; | |
12648 | if (osec == NULL) | |
12649 | return FALSE; | |
12650 | ||
12651 | BFD_ASSERT (elf_section_data (osec)->sec_info == NULL); | |
12652 | elf_section_data (osec)->sec_info = elf_section_data (isec)->sec_info; | |
12653 | osection->sh_type = SHT_RELA; | |
12654 | osection->sh_link = elf_onesymtab (obfd); | |
12655 | if (osection->sh_link == 0) | |
12656 | { | |
12657 | /* There is no symbol table - we are hosed... */ | |
12658 | _bfd_error_handler | |
12659 | /* xgettext:c-format */ | |
12660 | (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"), | |
12661 | obfd, osec); | |
12662 | bfd_set_error (bfd_error_bad_value); | |
12663 | return FALSE; | |
12664 | } | |
12665 | ||
12666 | /* Find the output section that corresponds to the isection's sh_info link. */ | |
327ef784 NC |
12667 | if (isection->sh_info == 0 |
12668 | || isection->sh_info >= elf_numsections (ibfd)) | |
12669 | { | |
12670 | _bfd_error_handler | |
12671 | /* xgettext:c-format */ | |
12672 | (_("%pB(%pA): info section index is invalid"), | |
12673 | obfd, osec); | |
12674 | bfd_set_error (bfd_error_bad_value); | |
12675 | return FALSE; | |
12676 | } | |
12677 | ||
a8e14f4c NC |
12678 | isection = elf_elfsections (ibfd)[isection->sh_info]; |
12679 | ||
327ef784 NC |
12680 | if (isection == NULL |
12681 | || isection->bfd_section == NULL | |
12682 | || isection->bfd_section->output_section == NULL) | |
12683 | { | |
12684 | _bfd_error_handler | |
12685 | /* xgettext:c-format */ | |
12686 | (_("%pB(%pA): info section index cannot be set because the section is not in the output"), | |
12687 | obfd, osec); | |
12688 | bfd_set_error (bfd_error_bad_value); | |
12689 | return FALSE; | |
12690 | } | |
12691 | ||
a8e14f4c NC |
12692 | osection->sh_info = |
12693 | elf_section_data (isection->bfd_section->output_section)->this_idx; | |
12694 | ||
12695 | #if DEBUG_SECONDARY_RELOCS | |
12696 | fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n", | |
12697 | osec->name, osection->sh_link, osection->sh_info); | |
12698 | #endif | |
12699 | ||
12700 | return TRUE; | |
12701 | } | |
12702 | ||
12703 | /* Write out a secondary reloc section. */ | |
12704 | ||
12705 | bfd_boolean | |
12706 | _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec) | |
12707 | { | |
12708 | const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); | |
12709 | bfd_vma addr_offset; | |
12710 | asection * relsec; | |
12711 | bfd_vma (*r_info) (bfd_vma, bfd_vma); | |
ac4bf06c NC |
12712 | bfd_boolean result = TRUE; |
12713 | ||
12714 | if (sec == NULL) | |
12715 | return FALSE; | |
a8e14f4c NC |
12716 | |
12717 | #if BFD_DEFAULT_TARGET_SIZE > 32 | |
12718 | if (bfd_arch_bits_per_address (abfd) != 32) | |
12719 | r_info = elf64_r_info; | |
12720 | else | |
12721 | #endif | |
12722 | r_info = elf32_r_info; | |
12723 | ||
a8e14f4c NC |
12724 | /* The address of an ELF reloc is section relative for an object |
12725 | file, and absolute for an executable file or shared library. | |
12726 | The address of a BFD reloc is always section relative. */ | |
12727 | addr_offset = 0; | |
12728 | if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) | |
12729 | addr_offset = sec->vma; | |
12730 | ||
12731 | /* Discover if there are any secondary reloc sections | |
12732 | associated with SEC. */ | |
12733 | for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) | |
12734 | { | |
12735 | const struct bfd_elf_section_data * const esd = elf_section_data (relsec); | |
12736 | Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr; | |
12737 | ||
12738 | if (hdr->sh_type == SHT_RELA | |
12739 | && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx) | |
12740 | { | |
12741 | asymbol * last_sym; | |
12742 | int last_sym_idx; | |
12743 | unsigned int reloc_count; | |
12744 | unsigned int idx; | |
12745 | arelent * src_irel; | |
12746 | bfd_byte * dst_rela; | |
12747 | ||
ac4bf06c NC |
12748 | if (hdr->contents != NULL) |
12749 | { | |
12750 | _bfd_error_handler | |
12751 | /* xgettext:c-format */ | |
12752 | (_("%pB(%pA): error: secondary reloc section processed twice"), | |
12753 | abfd, relsec); | |
12754 | bfd_set_error (bfd_error_bad_value); | |
12755 | result = FALSE; | |
12756 | continue; | |
12757 | } | |
a8e14f4c NC |
12758 | |
12759 | reloc_count = hdr->sh_size / hdr->sh_entsize; | |
ac4bf06c NC |
12760 | if (reloc_count <= 0) |
12761 | { | |
12762 | _bfd_error_handler | |
12763 | /* xgettext:c-format */ | |
12764 | (_("%pB(%pA): error: secondary reloc section is empty!"), | |
12765 | abfd, relsec); | |
12766 | bfd_set_error (bfd_error_bad_value); | |
12767 | result = FALSE; | |
12768 | continue; | |
12769 | } | |
a8e14f4c NC |
12770 | |
12771 | hdr->contents = bfd_alloc (abfd, hdr->sh_size); | |
12772 | if (hdr->contents == NULL) | |
12773 | continue; | |
12774 | ||
12775 | #if DEBUG_SECONDARY_RELOCS | |
12776 | fprintf (stderr, "write %u secondary relocs for %s from %s\n", | |
12777 | reloc_count, sec->name, relsec->name); | |
12778 | #endif | |
12779 | last_sym = NULL; | |
12780 | last_sym_idx = 0; | |
12781 | dst_rela = hdr->contents; | |
12782 | src_irel = (arelent *) esd->sec_info; | |
ac4bf06c NC |
12783 | if (src_irel == NULL) |
12784 | { | |
12785 | _bfd_error_handler | |
12786 | /* xgettext:c-format */ | |
12787 | (_("%pB(%pA): error: internal relocs missing for secondary reloc section"), | |
12788 | abfd, relsec); | |
12789 | bfd_set_error (bfd_error_bad_value); | |
12790 | result = FALSE; | |
12791 | continue; | |
12792 | } | |
a8e14f4c NC |
12793 | |
12794 | for (idx = 0; idx < reloc_count; idx++, dst_rela += hdr->sh_entsize) | |
12795 | { | |
12796 | Elf_Internal_Rela src_rela; | |
12797 | arelent *ptr; | |
12798 | asymbol *sym; | |
12799 | int n; | |
12800 | ||
12801 | ptr = src_irel + idx; | |
ac4bf06c NC |
12802 | if (ptr == NULL) |
12803 | { | |
12804 | _bfd_error_handler | |
12805 | /* xgettext:c-format */ | |
12806 | (_("%pB(%pA): error: reloc table entry %u is empty"), | |
12807 | abfd, relsec, idx); | |
12808 | bfd_set_error (bfd_error_bad_value); | |
12809 | result = FALSE; | |
12810 | break; | |
12811 | } | |
a8e14f4c | 12812 | |
ac4bf06c NC |
12813 | if (ptr->sym_ptr_ptr == NULL) |
12814 | { | |
12815 | /* FIXME: Is this an error ? */ | |
12816 | n = 0; | |
12817 | } | |
a8e14f4c NC |
12818 | else |
12819 | { | |
ac4bf06c NC |
12820 | sym = *ptr->sym_ptr_ptr; |
12821 | ||
12822 | if (sym == last_sym) | |
12823 | n = last_sym_idx; | |
12824 | else | |
a8e14f4c | 12825 | { |
ac4bf06c NC |
12826 | n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym); |
12827 | if (n < 0) | |
12828 | { | |
12829 | _bfd_error_handler | |
12830 | /* xgettext:c-format */ | |
12831 | (_("%pB(%pA): error: secondary reloc %u references a missing symbol"), | |
12832 | abfd, relsec, idx); | |
12833 | bfd_set_error (bfd_error_bad_value); | |
12834 | result = FALSE; | |
12835 | n = 0; | |
12836 | } | |
12837 | ||
12838 | last_sym = sym; | |
12839 | last_sym_idx = n; | |
a8e14f4c | 12840 | } |
a8e14f4c | 12841 | |
ac4bf06c NC |
12842 | if (sym->the_bfd != NULL |
12843 | && sym->the_bfd->xvec != abfd->xvec | |
12844 | && ! _bfd_elf_validate_reloc (abfd, ptr)) | |
12845 | { | |
12846 | _bfd_error_handler | |
12847 | /* xgettext:c-format */ | |
12848 | (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"), | |
12849 | abfd, relsec, idx); | |
12850 | bfd_set_error (bfd_error_bad_value); | |
12851 | result = FALSE; | |
12852 | n = 0; | |
12853 | } | |
a8e14f4c NC |
12854 | } |
12855 | ||
ac4bf06c | 12856 | src_rela.r_offset = ptr->address + addr_offset; |
a8e14f4c NC |
12857 | if (ptr->howto == NULL) |
12858 | { | |
ac4bf06c NC |
12859 | _bfd_error_handler |
12860 | /* xgettext:c-format */ | |
12861 | (_("%pB(%pA): error: secondary reloc %u is of an unknown type"), | |
12862 | abfd, relsec, idx); | |
12863 | bfd_set_error (bfd_error_bad_value); | |
12864 | result = FALSE; | |
12865 | src_rela.r_info = r_info (0, 0); | |
a8e14f4c | 12866 | } |
ac4bf06c NC |
12867 | else |
12868 | src_rela.r_info = r_info (n, ptr->howto->type); | |
a8e14f4c NC |
12869 | src_rela.r_addend = ptr->addend; |
12870 | ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela); | |
12871 | } | |
12872 | } | |
12873 | } | |
12874 | ||
ac4bf06c | 12875 | return result; |
a8e14f4c | 12876 | } |